1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 1999-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************
10 * file name: udata.cpp
12 * tab size: 8 (not used)
15 * created on: 1999oct25
16 * created by: Markus W. Scherer
19 #include "unicode/utypes.h" /* U_PLATFORM etc. */
23 #define ATTRIBUTE_WEAK __attribute__ ((weak))
24 might have to #include some other header
28 #include "unicode/putil.h"
29 #include "unicode/udata.h"
30 #include "unicode/uversion.h"
45 /***********************************************************************
47 * Notes on the organization of the ICU data implementation
49 * All of the public API is defined in udata.h
51 * The implementation is split into several files...
53 * - udata.c (this file) contains higher level code that knows about
54 * the search paths for locating data, caching opened data, etc.
56 * - umapfile.c contains the low level platform-specific code for actually loading
57 * (memory mapping, file reading, whatever) data into memory.
59 * - ucmndata.c deals with the tables of contents of ICU data items within
60 * an ICU common format data file. The implementation includes
61 * an abstract interface and support for multiple TOC formats.
62 * All knowledge of any specific TOC format is encapsulated here.
64 * - udatamem.c has code for managing UDataMemory structs. These are little
65 * descriptor objects for blocks of memory holding ICU data of
69 /* configuration ---------------------------------------------------------- */
71 /* If you are excruciatingly bored turn this on .. */
72 /* #define UDATA_DEBUG 1 */
73 /* For debugging use of timezone data in a separate file */
74 /* #define UDATA_TZFILES_DEBUG 1 */
76 #if defined(UDATA_DEBUG) || defined(UDATA_TZFILES_DEBUG)
83 * Forward declarations
85 static UDataMemory
*udata_findCachedData(const char *path
, UErrorCode
&err
);
87 /***********************************************************************
89 * static (Global) data
91 ************************************************************************/
94 * Pointers to the common ICU data.
96 * We store multiple pointers to ICU data packages and iterate through them
97 * when looking for a data item.
99 * It is possible to combine this with dependency inversion:
100 * One or more data package libraries may export
101 * functions that each return a pointer to their piece of the ICU data,
102 * and this file would import them as weak functions, without a
103 * strong linker dependency from the common library on the data library.
105 * Then we can have applications depend on only that part of ICU's data
106 * that they really need, reducing the size of binaries that take advantage
109 static UDataMemory
*gCommonICUDataArray
[10] = { NULL
}; // Access protected by icu global mutex.
111 static u_atomic_int32_t gHaveTriedToLoadCommonData
= ATOMIC_INT32_T_INITIALIZER(0); // See extendICUData().
113 static UHashtable
*gCommonDataCache
= NULL
; /* Global hash table of opened ICU data files. */
114 static icu::UInitOnce gCommonDataCacheInitOnce
= U_INITONCE_INITIALIZER
;
116 #if !defined(ICU_DATA_DIR_WINDOWS)
117 static UDataFileAccess gDataFileAccess
= UDATA_DEFAULT_ACCESS
; // Access not synchronized.
118 // Modifying is documented as thread-unsafe.
120 // If we are using the Windows data directory, then look in one spot only.
121 static UDataFileAccess gDataFileAccess
= UDATA_NO_FILES
;
124 static UBool U_CALLCONV
129 if (gCommonDataCache
) { /* Delete the cache of user data mappings. */
130 uhash_close(gCommonDataCache
); /* Table owns the contents, and will delete them. */
131 gCommonDataCache
= NULL
; /* Cleanup is not thread safe. */
133 gCommonDataCacheInitOnce
.reset();
135 for (i
= 0; i
< UPRV_LENGTHOF(gCommonICUDataArray
) && gCommonICUDataArray
[i
] != NULL
; ++i
) {
136 udata_close(gCommonICUDataArray
[i
]);
137 gCommonICUDataArray
[i
] = NULL
;
139 gHaveTriedToLoadCommonData
= 0;
141 return TRUE
; /* Everything was cleaned up */
144 static UBool U_CALLCONV
145 findCommonICUDataByName(const char *inBasename
, UErrorCode
&err
)
150 UDataMemory
*pData
= udata_findCachedData(inBasename
, err
);
151 if (U_FAILURE(err
) || pData
== NULL
)
156 for (i
= 0; i
< UPRV_LENGTHOF(gCommonICUDataArray
); ++i
) {
157 if ((gCommonICUDataArray
[i
] != NULL
) && (gCommonICUDataArray
[i
]->pHeader
== pData
->pHeader
)) {
158 /* The data pointer is already in the array. */
169 * setCommonICUData. Set a UDataMemory to be the global ICU Data
172 setCommonICUData(UDataMemory
*pData
, /* The new common data. Belongs to caller, we copy it. */
173 UBool warn
, /* If true, set USING_DEFAULT warning if ICUData was */
174 /* changed by another thread before we got to it. */
177 UDataMemory
*newCommonData
= UDataMemory_createNewInstance(pErr
);
179 UBool didUpdate
= FALSE
;
180 if (U_FAILURE(*pErr
)) {
184 /* For the assignment, other threads must cleanly see either the old */
185 /* or the new, not some partially initialized new. The old can not be */
186 /* deleted - someone may still have a pointer to it lying around in */
188 UDatamemory_assign(newCommonData
, pData
);
190 for (i
= 0; i
< UPRV_LENGTHOF(gCommonICUDataArray
); ++i
) {
191 if (gCommonICUDataArray
[i
] == NULL
) {
192 gCommonICUDataArray
[i
] = newCommonData
;
195 } else if (gCommonICUDataArray
[i
]->pHeader
== pData
->pHeader
) {
196 /* The same data pointer is already in the array. */
202 if (i
== UPRV_LENGTHOF(gCommonICUDataArray
) && warn
) {
203 *pErr
= U_USING_DEFAULT_WARNING
;
206 ucln_common_registerCleanup(UCLN_COMMON_UDATA
, udata_cleanup
);
208 uprv_free(newCommonData
);
213 #if !defined(ICU_DATA_DIR_WINDOWS)
216 setCommonICUDataPointer(const void *pData
, UBool
/*warn*/, UErrorCode
*pErrorCode
) {
218 UDataMemory_init(&tData
);
219 UDataMemory_setData(&tData
, pData
);
220 udata_checkCommonData(&tData
, pErrorCode
);
221 return setCommonICUData(&tData
, FALSE
, pErrorCode
);
227 findBasename(const char *path
) {
228 const char *basename
=uprv_strrchr(path
, U_FILE_SEP_CHAR
);
238 packageNameFromPath(const char *path
)
240 if((path
== NULL
) || (*path
== 0)) {
241 return U_ICUDATA_NAME
;
244 path
= findBasename(path
);
246 if((path
== NULL
) || (*path
== 0)) {
247 return U_ICUDATA_NAME
;
254 /*----------------------------------------------------------------------*
256 * Cache for common data *
257 * Functions for looking up or adding entries to a cache of *
258 * data that has been previously opened. Avoids a potentially *
259 * expensive operation of re-opening the data for subsequent *
262 * Data remains cached for the duration of the process. *
264 *----------------------------------------------------------------------*/
266 typedef struct DataCacheElement
{
274 * Deleter function for DataCacheElements.
275 * udata cleanup function closes the hash table; hash table in turn calls back to
276 * here for each entry.
278 static void U_CALLCONV
DataCacheElement_deleter(void *pDCEl
) {
279 DataCacheElement
*p
= (DataCacheElement
*)pDCEl
;
280 udata_close(p
->item
); /* unmaps storage */
281 uprv_free(p
->name
); /* delete the hash key string. */
282 uprv_free(pDCEl
); /* delete 'this' */
285 static void U_CALLCONV
udata_initHashTable(UErrorCode
&err
) {
286 U_ASSERT(gCommonDataCache
== NULL
);
287 gCommonDataCache
= uhash_open(uhash_hashChars
, uhash_compareChars
, NULL
, &err
);
288 if (U_FAILURE(err
)) {
291 U_ASSERT(gCommonDataCache
!= NULL
);
292 uhash_setValueDeleter(gCommonDataCache
, DataCacheElement_deleter
);
293 ucln_common_registerCleanup(UCLN_COMMON_UDATA
, udata_cleanup
);
296 /* udata_getCacheHashTable()
297 * Get the hash table used to store the data cache entries.
298 * Lazy create it if it doesn't yet exist.
300 static UHashtable
*udata_getHashTable(UErrorCode
&err
) {
301 umtx_initOnce(gCommonDataCacheInitOnce
, &udata_initHashTable
, err
);
302 return gCommonDataCache
;
307 static UDataMemory
*udata_findCachedData(const char *path
, UErrorCode
&err
)
310 UDataMemory
*retVal
= NULL
;
311 DataCacheElement
*el
;
312 const char *baseName
;
314 htable
= udata_getHashTable(err
);
315 if (U_FAILURE(err
)) {
319 baseName
= findBasename(path
); /* Cache remembers only the base name, not the full path. */
321 el
= (DataCacheElement
*)uhash_get(htable
, baseName
);
327 fprintf(stderr
, "Cache: [%s] -> %p\n", baseName
, (void*) retVal
);
333 static UDataMemory
*udata_cacheDataItem(const char *path
, UDataMemory
*item
, UErrorCode
*pErr
) {
334 DataCacheElement
*newElement
;
335 const char *baseName
;
338 DataCacheElement
*oldValue
= NULL
;
339 UErrorCode subErr
= U_ZERO_ERROR
;
341 htable
= udata_getHashTable(*pErr
);
342 if (U_FAILURE(*pErr
)) {
346 /* Create a new DataCacheElement - the thingy we store in the hash table -
347 * and copy the supplied path and UDataMemoryItems into it.
349 newElement
= (DataCacheElement
*)uprv_malloc(sizeof(DataCacheElement
));
350 if (newElement
== NULL
) {
351 *pErr
= U_MEMORY_ALLOCATION_ERROR
;
354 newElement
->item
= UDataMemory_createNewInstance(pErr
);
355 if (U_FAILURE(*pErr
)) {
356 uprv_free(newElement
);
359 UDatamemory_assign(newElement
->item
, item
);
361 baseName
= findBasename(path
);
362 nameLen
= (int32_t)uprv_strlen(baseName
);
363 newElement
->name
= (char *)uprv_malloc(nameLen
+1);
364 if (newElement
->name
== NULL
) {
365 *pErr
= U_MEMORY_ALLOCATION_ERROR
;
366 uprv_free(newElement
->item
);
367 uprv_free(newElement
);
370 uprv_strcpy(newElement
->name
, baseName
);
372 /* Stick the new DataCacheElement into the hash table.
375 oldValue
= (DataCacheElement
*)uhash_get(htable
, path
);
376 if (oldValue
!= NULL
) {
377 subErr
= U_USING_DEFAULT_WARNING
;
382 newElement
->name
, /* Key */
383 newElement
, /* Value */
389 fprintf(stderr
, "Cache: [%s] <<< %p : %s. vFunc=%p\n", newElement
->name
,
390 (void*) newElement
->item
, u_errorName(subErr
), (void*) newElement
->item
->vFuncs
);
393 if (subErr
== U_USING_DEFAULT_WARNING
|| U_FAILURE(subErr
)) {
394 *pErr
= subErr
; /* copy sub err unto fillin ONLY if something happens. */
395 uprv_free(newElement
->name
);
396 uprv_free(newElement
->item
);
397 uprv_free(newElement
);
398 return oldValue
? oldValue
->item
: NULL
;
401 return newElement
->item
;
404 /*----------------------------------------------------------------------*==============
406 * Path management. Could be shared with other tools/etc if need be *
409 *----------------------------------------------------------------------*/
413 class UDataPathIterator
416 UDataPathIterator(const char *path
, const char *pkg
,
417 const char *item
, const char *suffix
, UBool doCheckLastFour
,
418 UErrorCode
*pErrorCode
);
419 const char *next(UErrorCode
*pErrorCode
);
422 const char *path
; /* working path (u_icudata_Dir) */
423 const char *nextPath
; /* path following this one */
424 const char *basename
; /* item's basename (icudt22e_mt.res)*/
426 StringPiece suffix
; /* item suffix (can be null) */
428 uint32_t basenameLen
; /* length of basename */
430 CharString itemPath
; /* path passed in with item name */
431 CharString pathBuffer
; /* output path for this it'ion */
432 CharString packageStub
; /* example: "/icudt28b". Will ignore that leaf in set paths. */
434 UBool checkLastFour
; /* if TRUE then allow paths such as '/foo/myapp.dat'
435 * to match, checks last 4 chars of suffix with
436 * last 4 of path, then previous chars. */
440 * @param iter The iterator to be initialized. Its current state does not matter.
441 * @param inPath The full pathname to be iterated over. If NULL, defaults to U_ICUDATA_NAME
442 * @param pkg Package which is being searched for, ex "icudt28l". Will ignore leaf directories such as /icudt28l
443 * @param item Item to be searched for. Can include full path, such as /a/b/foo.dat
444 * @param inSuffix Optional item suffix, if not-null (ex. ".dat") then 'path' can contain 'item' explicitly.
445 * Ex: 'stuff.dat' would be found in '/a/foo:/tmp/stuff.dat:/bar/baz' as item #2.
446 * '/blarg/stuff.dat' would also be found.
447 * Note: inSuffix may also be the 'item' being searched for as well, (ex: "ibm-5348_P100-1997.cnv"), in which case
448 * the 'item' parameter is often the same as pkg. (Though sometimes might have a tree part as well, ex: "icudt62l-curr").
450 UDataPathIterator::UDataPathIterator(const char *inPath
, const char *pkg
,
451 const char *item
, const char *inSuffix
, UBool doCheckLastFour
,
452 UErrorCode
*pErrorCode
)
455 fprintf(stderr
, "SUFFIX1=%s PATH=%s\n", inSuffix
, inPath
);
459 path
= u_getDataDirectory();
466 packageStub
.append(U_FILE_SEP_CHAR
, *pErrorCode
).append(pkg
, *pErrorCode
);
468 fprintf(stderr
, "STUB=%s [%d]\n", packageStub
.data(), packageStub
.length());
473 basename
= findBasename(item
);
474 basenameLen
= (int32_t)uprv_strlen(basename
);
477 if(basename
== item
) {
480 itemPath
.append(item
, (int32_t)(basename
-item
), *pErrorCode
);
481 nextPath
= itemPath
.data();
484 fprintf(stderr
, "SUFFIX=%s [%p]\n", inSuffix
, (void*) inSuffix
);
488 if(inSuffix
!= NULL
) {
494 checkLastFour
= doCheckLastFour
;
496 /* pathBuffer will hold the output path strings returned by this iterator */
499 fprintf(stderr
, "0: init %s -> [path=%s], [base=%s], [suff=%s], [itempath=%s], [nextpath=%s], [checklast4=%s]\n",
506 checkLastFour
?"TRUE":"false");
511 * Get the next path on the list.
513 * @param iter The Iter to be used
514 * @param len If set, pointer to the length of the returned path, for convenience.
515 * @return Pointer to the next path segment, or NULL if there are no more.
517 const char *UDataPathIterator::next(UErrorCode
*pErrorCode
)
519 if(U_FAILURE(*pErrorCode
)) {
523 const char *currentPath
= NULL
;
525 const char *pathBasename
;
529 if( nextPath
== NULL
) {
532 currentPath
= nextPath
;
534 if(nextPath
== itemPath
.data()) { /* we were processing item's path. */
535 nextPath
= path
; /* start with regular path next tm. */
536 pathLen
= (int32_t)uprv_strlen(currentPath
);
538 /* fix up next for next time */
539 nextPath
= uprv_strchr(currentPath
, U_PATH_SEP_CHAR
);
540 if(nextPath
== NULL
) {
541 /* segment: entire path */
542 pathLen
= (int32_t)uprv_strlen(currentPath
);
544 /* segment: until next segment */
545 pathLen
= (int32_t)(nextPath
- currentPath
);
556 fprintf(stderr
, "rest of path (IDD) = %s\n", currentPath
);
557 fprintf(stderr
, " ");
560 for(qqq
=0;qqq
<pathLen
;qqq
++)
562 fprintf(stderr
, " ");
565 fprintf(stderr
, "^\n");
568 pathBuffer
.clear().append(currentPath
, pathLen
, *pErrorCode
);
570 /* check for .dat files */
571 pathBasename
= findBasename(pathBuffer
.data());
573 if(checkLastFour
== TRUE
&&
575 uprv_strncmp(pathBuffer
.data() +(pathLen
-4), suffix
.data(), 4)==0 && /* suffix matches */
576 uprv_strncmp(findBasename(pathBuffer
.data()), basename
, basenameLen
)==0 && /* base matches */
577 uprv_strlen(pathBasename
)==(basenameLen
+4)) { /* base+suffix = full len */
580 fprintf(stderr
, "Have %s file on the path: %s\n", suffix
.data(), pathBuffer
.data());
585 { /* regular dir path */
586 if(pathBuffer
[pathLen
-1] != U_FILE_SEP_CHAR
) {
588 uprv_strncmp(pathBuffer
.data()+(pathLen
-4), ".dat", 4) == 0)
591 fprintf(stderr
, "skipping non-directory .dat file %s\n", pathBuffer
.data());
596 /* Check if it is a directory with the same name as our package */
597 if(!packageStub
.isEmpty() &&
598 (pathLen
> packageStub
.length()) &&
599 !uprv_strcmp(pathBuffer
.data() + pathLen
- packageStub
.length(), packageStub
.data())) {
601 fprintf(stderr
, "Found stub %s (will add package %s of len %d)\n", packageStub
.data(), basename
, basenameLen
);
603 pathBuffer
.truncate(pathLen
- packageStub
.length());
605 pathBuffer
.append(U_FILE_SEP_CHAR
, *pErrorCode
);
609 pathBuffer
.append(packageStub
.data()+1, packageStub
.length()-1, *pErrorCode
);
611 if (!suffix
.empty()) /* tack on suffix */
613 if (suffix
.length() > 4) {
614 // If the suffix is actually an item ("ibm-5348_P100-1997.cnv") and not an extension (".res")
615 // then we need to ensure that the path ends with a separator.
616 pathBuffer
.ensureEndsWithFileSeparator(*pErrorCode
);
618 pathBuffer
.append(suffix
, *pErrorCode
);
623 fprintf(stderr
, " --> %s\n", pathBuffer
.data());
626 return pathBuffer
.data();
630 /* fell way off the end */
636 /* ==================================================================================*/
639 /*----------------------------------------------------------------------*
641 * Add a static reference to the common data library *
642 * Unless overridden by an explicit udata_setCommonData, this will be *
645 *----------------------------------------------------------------------*/
646 #if !defined(ICU_DATA_DIR_WINDOWS)
647 // When using the Windows system data, we expect only a single data file.
648 extern "C" const DataHeader U_DATA_API U_ICUDATA_ENTRY_POINT
;
652 * This would be a good place for weak-linkage declarations of
653 * partial-data-library access functions where each returns a pointer
654 * to its data package, if it is linked in.
657 extern const void *uprv_getICUData_collation(void) ATTRIBUTE_WEAK;
658 extern const void *uprv_getICUData_conversion(void) ATTRIBUTE_WEAK;
661 /*----------------------------------------------------------------------*
663 * openCommonData Attempt to open a common format (.dat) file *
664 * Map it into memory (if it's not there already) *
665 * and return a UDataMemory object for it. *
667 * If the requested data is already open and cached *
668 * just return the cached UDataMem object. *
670 *----------------------------------------------------------------------*/
672 openCommonData(const char *path
, /* Path from OpenChoice? */
673 int32_t commonDataIndex
, /* ICU Data (index >= 0) if path == NULL */
674 UErrorCode
*pErrorCode
)
677 const char *pathBuffer
;
678 const char *inBasename
;
680 if (U_FAILURE(*pErrorCode
)) {
684 UDataMemory_init(&tData
);
686 /* ??????? TODO revisit this */
687 if (commonDataIndex
>= 0) {
688 /* "mini-cache" for common ICU data */
689 if(commonDataIndex
>= UPRV_LENGTHOF(gCommonICUDataArray
)) {
694 if(gCommonICUDataArray
[commonDataIndex
] != NULL
) {
695 return gCommonICUDataArray
[commonDataIndex
];
697 #if !defined(ICU_DATA_DIR_WINDOWS)
698 // When using the Windows system data, we expect only a single data file.
700 for(i
= 0; i
< commonDataIndex
; ++i
) {
701 if(gCommonICUDataArray
[i
]->pHeader
== &U_ICUDATA_ENTRY_POINT
) {
702 /* The linked-in data is already in the list. */
709 /* Add the linked-in data to the list. */
711 * This is where we would check and call weakly linked partial-data-library
715 if (uprv_getICUData_collation) {
716 setCommonICUDataPointer(uprv_getICUData_collation(), FALSE, pErrorCode);
718 if (uprv_getICUData_conversion) {
719 setCommonICUDataPointer(uprv_getICUData_conversion(), FALSE, pErrorCode);
722 #if !defined(ICU_DATA_DIR_WINDOWS)
723 // When using the Windows system data, we expect only a single data file.
724 setCommonICUDataPointer(&U_ICUDATA_ENTRY_POINT
, FALSE
, pErrorCode
);
727 return gCommonICUDataArray
[commonDataIndex
];
733 /* request is NOT for ICU Data. */
735 /* Find the base name portion of the supplied path. */
736 /* inBasename will be left pointing somewhere within the original path string. */
737 inBasename
= findBasename(path
);
739 fprintf(stderr
, "inBasename = %s\n", inBasename
);
743 /* no basename. This will happen if the original path was a directory name, */
744 /* like "a/b/c/". (Fallback to separate files will still work.) */
746 fprintf(stderr
, "ocd: no basename in %s, bailing.\n", path
);
748 if (U_SUCCESS(*pErrorCode
)) {
749 *pErrorCode
=U_FILE_ACCESS_ERROR
;
754 /* Is the requested common data file already open and cached? */
755 /* Note that the cache is keyed by the base name only. The rest of the path, */
756 /* if any, is not considered. */
757 UDataMemory
*dataToReturn
= udata_findCachedData(inBasename
, *pErrorCode
);
758 if (dataToReturn
!= NULL
|| U_FAILURE(*pErrorCode
)) {
762 /* Requested item is not in the cache.
763 * Hunt it down, trying all the path locations
766 UDataPathIterator
iter(u_getDataDirectory(), inBasename
, path
, ".dat", TRUE
, pErrorCode
);
768 while ((UDataMemory_isLoaded(&tData
)==FALSE
) && (pathBuffer
= iter
.next(pErrorCode
)) != NULL
)
771 fprintf(stderr
, "ocd: trying path %s - ", pathBuffer
);
773 uprv_mapFile(&tData
, pathBuffer
, pErrorCode
);
775 fprintf(stderr
, "%s\n", UDataMemory_isLoaded(&tData
)?"LOADED":"not loaded");
778 if (U_FAILURE(*pErrorCode
)) {
782 #if defined(OS390_STUBDATA) && defined(OS390BATCH)
783 if (!UDataMemory_isLoaded(&tData
)) {
784 char ourPathBuffer
[1024];
785 /* One more chance, for extendCommonData() */
786 uprv_strncpy(ourPathBuffer
, path
, 1019);
787 ourPathBuffer
[1019]=0;
788 uprv_strcat(ourPathBuffer
, ".dat");
789 uprv_mapFile(&tData
, ourPathBuffer
, pErrorCode
);
793 if (U_FAILURE(*pErrorCode
)) {
796 if (!UDataMemory_isLoaded(&tData
)) {
798 *pErrorCode
=U_FILE_ACCESS_ERROR
;
802 /* we have mapped a file, check its header */
803 udata_checkCommonData(&tData
, pErrorCode
);
806 /* Cache the UDataMemory struct for this .dat file,
807 * so we won't need to hunt it down and map it again next time
808 * something is needed from it. */
809 return udata_cacheDataItem(inBasename
, &tData
, pErrorCode
);
813 /*----------------------------------------------------------------------*
815 * extendICUData If the full set of ICU data was not loaded at *
816 * program startup, load it now. This function will *
817 * be called when the lookup of an ICU data item in *
818 * the common ICU data fails. *
820 * return true if new data is loaded, false otherwise.*
822 *----------------------------------------------------------------------*/
823 static UBool
extendICUData(UErrorCode
*pErr
)
826 UDataMemory copyPData
;
827 UBool didUpdate
= FALSE
;
830 * There is a chance for a race condition here.
831 * Normally, ICU data is loaded from a DLL or via mmap() and
832 * setCommonICUData() will detect if the same address is set twice.
833 * If ICU is built with data loading via fread() then the address will
834 * be different each time the common data is loaded and we may add
835 * multiple copies of the data.
836 * In this case, use a mutex to prevent the race.
837 * Use a specific mutex to avoid nested locks of the global mutex.
839 #if MAP_IMPLEMENTATION==MAP_STDIO
840 static UMutex extendICUDataMutex
;
841 umtx_lock(&extendICUDataMutex
);
843 if(!umtx_loadAcquire(gHaveTriedToLoadCommonData
)) {
844 /* See if we can explicitly open a .dat file for the ICUData. */
845 pData
= openCommonData(
846 U_ICUDATA_NAME
, /* "icudt20l" , for example. */
847 -1, /* Pretend we're not opening ICUData */
850 /* How about if there is no pData, eh... */
852 UDataMemory_init(©PData
);
854 UDatamemory_assign(©PData
, pData
);
855 copyPData
.map
= 0; /* The mapping for this data is owned by the hash table */
856 copyPData
.mapAddr
= 0; /* which will unmap it when ICU is shut down. */
857 /* CommonICUData is also unmapped when ICU is shut down.*/
858 /* To avoid unmapping the data twice, zero out the map */
859 /* fields in the UDataMemory that we're assigning */
860 /* to CommonICUData. */
862 didUpdate
= /* no longer using this result */
863 setCommonICUData(©PData
,/* The new common data. */
864 FALSE
, /* No warnings if write didn't happen */
865 pErr
); /* setCommonICUData honors errors; NOP if error set */
868 umtx_storeRelease(gHaveTriedToLoadCommonData
, 1);
871 didUpdate
= findCommonICUDataByName(U_ICUDATA_NAME
, *pErr
); /* Return 'true' when a racing writes out the extended */
872 /* data after another thread has failed to see it (in openCommonData), so */
873 /* extended data can be examined. */
874 /* Also handles a race through here before gHaveTriedToLoadCommonData is set. */
876 #if MAP_IMPLEMENTATION==MAP_STDIO
877 umtx_unlock(&extendICUDataMutex
);
879 return didUpdate
; /* Return true if ICUData pointer was updated. */
880 /* (Could potentially have been done by another thread racing */
881 /* us through here, but that's fine, we still return true */
882 /* so that current thread will also examine extended data. */
885 /*----------------------------------------------------------------------*
887 * udata_setCommonData *
889 *----------------------------------------------------------------------*/
890 U_CAPI
void U_EXPORT2
891 udata_setCommonData(const void *data
, UErrorCode
*pErrorCode
) {
892 UDataMemory dataMemory
;
894 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
899 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
903 /* set the data pointer and test for validity */
904 UDataMemory_init(&dataMemory
);
905 UDataMemory_setData(&dataMemory
, data
);
906 udata_checkCommonData(&dataMemory
, pErrorCode
);
907 if (U_FAILURE(*pErrorCode
)) {return;}
909 /* we have good data */
910 /* Set it up as the ICU Common Data. */
911 setCommonICUData(&dataMemory
, TRUE
, pErrorCode
);
914 /*---------------------------------------------------------------------------
918 *---------------------------------------------------------------------------- */
919 U_CAPI
void U_EXPORT2
920 udata_setAppData(const char *path
, const void *data
, UErrorCode
*err
)
924 if(err
==NULL
|| U_FAILURE(*err
)) {
928 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
932 UDataMemory_init(&udm
);
933 UDataMemory_setData(&udm
, data
);
934 udata_checkCommonData(&udm
, err
);
935 udata_cacheDataItem(path
, &udm
, err
);
938 /*----------------------------------------------------------------------------*
940 * checkDataItem Given a freshly located/loaded data item, either *
941 * an entry in a common file or a separately loaded file, *
942 * sanity check its header, and see if the data is *
943 * acceptable to the app. *
944 * If the data is good, create and return a UDataMemory *
945 * object that can be returned to the application. *
946 * Return NULL on any sort of failure. *
948 *----------------------------------------------------------------------------*/
952 const DataHeader
*pHeader
, /* The data item to be checked. */
953 UDataMemoryIsAcceptable
*isAcceptable
, /* App's call-back function */
954 void *context
, /* pass-thru param for above. */
955 const char *type
, /* pass-thru param for above. */
956 const char *name
, /* pass-thru param for above. */
957 UErrorCode
*nonFatalErr
, /* Error code if this data was not acceptable */
958 /* but openChoice should continue with */
959 /* trying to get data from fallback path. */
960 UErrorCode
*fatalErr
/* Bad error, caller should return immediately */
963 UDataMemory
*rDataMem
= NULL
; /* the new UDataMemory, to be returned. */
965 if (U_FAILURE(*fatalErr
)) {
969 if(pHeader
->dataHeader
.magic1
==0xda &&
970 pHeader
->dataHeader
.magic2
==0x27 &&
971 (isAcceptable
==NULL
|| isAcceptable(context
, type
, name
, &pHeader
->info
))
973 rDataMem
=UDataMemory_createNewInstance(fatalErr
);
974 if (U_FAILURE(*fatalErr
)) {
977 rDataMem
->pHeader
= pHeader
;
979 /* the data is not acceptable, look further */
980 /* If we eventually find something good, this errorcode will be */
982 *nonFatalErr
=U_INVALID_FORMAT_ERROR
;
988 * @return 0 if not loaded, 1 if loaded or err
990 static UDataMemory
*doLoadFromIndividualFiles(const char *pkgName
,
991 const char *dataPath
, const char *tocEntryPathSuffix
,
992 /* following arguments are the same as doOpenChoice itself */
993 const char *path
, const char *type
, const char *name
,
994 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
995 UErrorCode
*subErrorCode
,
996 UErrorCode
*pErrorCode
)
998 const char *pathBuffer
;
999 UDataMemory dataMemory
;
1000 UDataMemory
*pEntryData
;
1002 /* look in ind. files: package\nam.typ ========================= */
1003 /* init path iterator for individual files */
1004 UDataPathIterator
iter(dataPath
, pkgName
, path
, tocEntryPathSuffix
, FALSE
, pErrorCode
);
1006 while ((pathBuffer
= iter
.next(pErrorCode
)) != NULL
)
1009 fprintf(stderr
, "UDATA: trying individual file %s\n", pathBuffer
);
1011 if (uprv_mapFile(&dataMemory
, pathBuffer
, pErrorCode
))
1013 pEntryData
= checkDataItem(dataMemory
.pHeader
, isAcceptable
, context
, type
, name
, subErrorCode
, pErrorCode
);
1014 if (pEntryData
!= NULL
) {
1016 * Hand off ownership of the backing memory to the user's UDataMemory.
1018 pEntryData
->mapAddr
= dataMemory
.mapAddr
;
1019 pEntryData
->map
= dataMemory
.map
;
1022 fprintf(stderr
, "** Mapped file: %s\n", pathBuffer
);
1027 /* the data is not acceptable, or some error occurred. Either way, unmap the memory */
1028 udata_close(&dataMemory
);
1030 /* If we had a nasty error, bail out completely. */
1031 if (U_FAILURE(*pErrorCode
)) {
1035 /* Otherwise remember that we found data but didn't like it for some reason */
1036 *subErrorCode
=U_INVALID_FORMAT_ERROR
;
1039 fprintf(stderr
, "%s\n", UDataMemory_isLoaded(&dataMemory
)?"LOADED":"not loaded");
1046 * @return 0 if not loaded, 1 if loaded or err
1048 static UDataMemory
*doLoadFromCommonData(UBool isICUData
, const char * /*pkgName*/,
1049 const char * /*dataPath*/, const char * /*tocEntryPathSuffix*/, const char *tocEntryName
,
1050 /* following arguments are the same as doOpenChoice itself */
1051 const char *path
, const char *type
, const char *name
,
1052 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
1053 UErrorCode
*subErrorCode
,
1054 UErrorCode
*pErrorCode
)
1056 UDataMemory
*pEntryData
;
1057 const DataHeader
*pHeader
;
1058 UDataMemory
*pCommonData
;
1059 int32_t commonDataIndex
;
1060 UBool checkedExtendedICUData
= FALSE
;
1061 /* try to get common data. The loop is for platforms such as the 390 that do
1062 * not initially load the full set of ICU data. If the lookup of an ICU data item
1063 * fails, the full (but slower to load) set is loaded, the and the loop repeats,
1064 * trying the lookup again. Once the full set of ICU data is loaded, the loop wont
1065 * repeat because the full set will be checked the first time through.
1067 * The loop also handles the fallback to a .dat file if the application linked
1068 * to the stub data library rather than a real library.
1070 for (commonDataIndex
= isICUData
? 0 : -1;;) {
1071 pCommonData
=openCommonData(path
, commonDataIndex
, subErrorCode
); /** search for pkg **/
1073 if(U_SUCCESS(*subErrorCode
) && pCommonData
!=NULL
) {
1076 /* look up the data piece in the common data */
1077 pHeader
=pCommonData
->vFuncs
->Lookup(pCommonData
, tocEntryName
, &length
, subErrorCode
);
1079 fprintf(stderr
, "%s: pHeader=%p - %s\n", tocEntryName
, (void*) pHeader
, u_errorName(*subErrorCode
));
1083 pEntryData
= checkDataItem(pHeader
, isAcceptable
, context
, type
, name
, subErrorCode
, pErrorCode
);
1085 fprintf(stderr
, "pEntryData=%p\n", (void*) pEntryData
);
1087 if (U_FAILURE(*pErrorCode
)) {
1090 if (pEntryData
!= NULL
) {
1091 pEntryData
->length
= length
;
1096 // If we failed due to being out-of-memory, then stop early and report the error.
1097 if (*subErrorCode
== U_MEMORY_ALLOCATION_ERROR
) {
1098 *pErrorCode
= *subErrorCode
;
1101 /* Data wasn't found. If we were looking for an ICUData item and there is
1102 * more data available, load it and try again,
1103 * otherwise break out of this loop. */
1106 } else if (pCommonData
!= NULL
) {
1107 ++commonDataIndex
; /* try the next data package */
1108 } else if ((!checkedExtendedICUData
) && extendICUData(subErrorCode
)) {
1109 checkedExtendedICUData
= TRUE
;
1110 /* try this data package slot again: it changed from NULL to non-NULL */
1118 * Identify the Time Zone resources that are subject to special override data loading.
1120 static UBool
isTimeZoneFile(const char *name
, const char *type
) {
1121 return ((uprv_strcmp(type
, "res") == 0) &&
1122 (uprv_strcmp(name
, "zoneinfo64") == 0 ||
1123 uprv_strcmp(name
, "timezoneTypes") == 0 ||
1124 uprv_strcmp(name
, "windowsZones") == 0 ||
1125 uprv_strcmp(name
, "metaZones") == 0));
1129 * A note on the ownership of Mapped Memory
1131 * For common format files, ownership resides with the UDataMemory object
1132 * that lives in the cache of opened common data. These UDataMemorys are private
1133 * to the udata implementation, and are never seen directly by users.
1135 * The UDataMemory objects returned to users will have the address of some desired
1136 * data within the mapped region, but they wont have the mapping info itself, and thus
1137 * won't cause anything to be removed from memory when they are closed.
1139 * For individual data files, the UDataMemory returned to the user holds the
1140 * information necessary to unmap the data on close. If the user independently
1141 * opens the same data file twice, two completely independent mappings will be made.
1142 * (There is no cache of opened data items from individual files, only a cache of
1143 * opened Common Data files, that is, files containing a collection of data items.)
1145 * For common data passed in from the user via udata_setAppData() or
1146 * udata_setCommonData(), ownership remains with the user.
1148 * UDataMemory objects themselves, as opposed to the memory they describe,
1149 * can be anywhere - heap, stack/local or global.
1150 * They have a flag to indicate when they're heap allocated and thus
1151 * must be deleted when closed.
1155 /*----------------------------------------------------------------------------*
1157 * main data loading functions *
1159 *----------------------------------------------------------------------------*/
1160 static UDataMemory
*
1161 doOpenChoice(const char *path
, const char *type
, const char *name
,
1162 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
1163 UErrorCode
*pErrorCode
)
1165 UDataMemory
*retVal
= NULL
;
1167 const char *dataPath
;
1169 int32_t tocEntrySuffixIndex
;
1170 const char *tocEntryPathSuffix
;
1171 UErrorCode subErrorCode
=U_ZERO_ERROR
;
1172 const char *treeChar
;
1174 UBool isICUData
= FALSE
;
1177 FileTracer::traceOpen(path
, type
, name
);
1180 /* Is this path ICU data? */
1182 !strcmp(path
, U_ICUDATA_ALIAS
) || /* "ICUDATA" */
1183 !uprv_strncmp(path
, U_ICUDATA_NAME U_TREE_SEPARATOR_STRING
, /* "icudt26e-" */
1184 uprv_strlen(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING
)) ||
1185 !uprv_strncmp(path
, U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING
, /* "ICUDATA-" */
1186 uprv_strlen(U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING
))) {
1190 #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) /* Windows: try "foo\bar" and "foo/bar" */
1191 /* remap from alternate path char to the main one */
1192 CharString altSepPath
;
1194 if(uprv_strchr(path
,U_FILE_ALT_SEP_CHAR
) != NULL
) {
1195 altSepPath
.append(path
, *pErrorCode
);
1197 while ((p
= uprv_strchr(altSepPath
.data(), U_FILE_ALT_SEP_CHAR
)) != NULL
) {
1198 *p
= U_FILE_SEP_CHAR
;
1200 #if defined (UDATA_DEBUG)
1201 fprintf(stderr
, "Changed path from [%s] to [%s]\n", path
, altSepPath
.s
);
1203 path
= altSepPath
.data();
1208 CharString tocEntryName
; /* entry name in tree format. ex: 'icudt28b/coll/ar.res' */
1209 CharString tocEntryPath
; /* entry name in path format. ex: 'icudt28b\\coll\\ar.res' */
1212 CharString treeName
;
1214 /* ======= Set up strings */
1216 pkgName
.append(U_ICUDATA_NAME
, *pErrorCode
);
1220 pkg
= uprv_strrchr(path
, U_FILE_SEP_CHAR
);
1221 first
= uprv_strchr(path
, U_FILE_SEP_CHAR
);
1222 if(uprv_pathIsAbsolute(path
) || (pkg
!= first
)) { /* more than one slash in the path- not a tree name */
1223 /* see if this is an /absolute/path/to/package path */
1225 pkgName
.append(pkg
+1, *pErrorCode
);
1227 pkgName
.append(path
, *pErrorCode
);
1230 treeChar
= uprv_strchr(path
, U_TREE_SEPARATOR
);
1232 treeName
.append(treeChar
+1, *pErrorCode
); /* following '-' */
1234 pkgName
.append(U_ICUDATA_NAME
, *pErrorCode
);
1236 pkgName
.append(path
, (int32_t)(treeChar
-path
), *pErrorCode
);
1237 if (first
== NULL
) {
1239 This user data has no path, but there is a tree name.
1240 Look up the correct path from the data cache later.
1242 path
= pkgName
.data();
1247 pkgName
.append(U_ICUDATA_NAME
, *pErrorCode
);
1249 pkgName
.append(path
, *pErrorCode
);
1256 fprintf(stderr
, " P=%s T=%s\n", pkgName
.data(), treeName
.data());
1259 /* setting up the entry name and file name
1260 * Make up a full name by appending the type to the supplied
1261 * name, assuming that a type was supplied.
1264 /* prepend the package */
1265 tocEntryName
.append(pkgName
, *pErrorCode
);
1266 tocEntryPath
.append(pkgName
, *pErrorCode
);
1267 tocEntrySuffixIndex
= tocEntryName
.length();
1269 if(!treeName
.isEmpty()) {
1270 tocEntryName
.append(U_TREE_ENTRY_SEP_CHAR
, *pErrorCode
).append(treeName
, *pErrorCode
);
1271 tocEntryPath
.append(U_FILE_SEP_CHAR
, *pErrorCode
).append(treeName
, *pErrorCode
);
1274 tocEntryName
.append(U_TREE_ENTRY_SEP_CHAR
, *pErrorCode
).append(name
, *pErrorCode
);
1275 tocEntryPath
.append(U_FILE_SEP_CHAR
, *pErrorCode
).append(name
, *pErrorCode
);
1276 if(type
!=NULL
&& *type
!=0) {
1277 tocEntryName
.append(".", *pErrorCode
).append(type
, *pErrorCode
);
1278 tocEntryPath
.append(".", *pErrorCode
).append(type
, *pErrorCode
);
1280 // The +1 is for the U_FILE_SEP_CHAR that is always appended above.
1281 tocEntryPathSuffix
= tocEntryPath
.data() + tocEntrySuffixIndex
+ 1; /* suffix starts here */
1284 fprintf(stderr
, " tocEntryName = %s\n", tocEntryName
.data());
1285 fprintf(stderr
, " tocEntryPath = %s\n", tocEntryName
.data());
1288 #if !defined(ICU_DATA_DIR_WINDOWS)
1290 path
= COMMON_DATA_NAME
; /* "icudt26e" */
1293 // When using the Windows system data, we expects only a single data file.
1294 path
= COMMON_DATA_NAME
; /* "icudt26e" */
1297 /************************ Begin loop looking for ind. files ***************/
1299 fprintf(stderr
, "IND: inBasename = %s, pkg=%s\n", "(n/a)", packageNameFromPath(path
));
1302 /* End of dealing with a null basename */
1303 dataPath
= u_getDataDirectory();
1305 /**** Time zone individual files override */
1306 if (isICUData
&& isTimeZoneFile(name
, type
)) {
1307 const char *tzFilesDir
= u_getTimeZoneFilesDirectory(pErrorCode
);
1308 if (tzFilesDir
[0] != 0) {
1310 fprintf(stderr
, "Trying Time Zone Files directory = %s\n", tzFilesDir
);
1312 #ifdef UDATA_TZFILES_DEBUG
1313 fprintf(stderr
, "# dOC U_TIMEZONE_FILES_DIR: %s\n", U_TIMEZONE_FILES_DIR
);
1316 #if defined(U_TIMEZONE_PACKAGE)
1317 // make tztocEntryName, like tocEntryName but with our package name
1318 UErrorCode tzpkgErrorCode
= U_ZERO_ERROR
;
1319 CharString tztocPkgPath
;
1320 tztocPkgPath
.append(tzFilesDir
, tzpkgErrorCode
);
1321 tztocPkgPath
.append(U_FILE_SEP_CHAR
, tzpkgErrorCode
).append(U_TIMEZONE_PACKAGE
, tzpkgErrorCode
);
1322 CharString tztocEntryName
;
1323 tztocEntryName
.append(U_TIMEZONE_PACKAGE
, tzpkgErrorCode
);
1324 if(!treeName
.isEmpty()) {
1325 tztocEntryName
.append(U_TREE_ENTRY_SEP_CHAR
, tzpkgErrorCode
).append(treeName
, tzpkgErrorCode
);
1327 tztocEntryName
.append(U_TREE_ENTRY_SEP_CHAR
, tzpkgErrorCode
).append(name
, tzpkgErrorCode
);
1328 if(type
!=NULL
&& *type
!=0) {
1329 tztocEntryName
.append(".", tzpkgErrorCode
).append(type
, tzpkgErrorCode
);
1331 #ifdef UDATA_TZFILES_DEBUG
1332 fprintf(stderr
, "# dOC tz pkg, doLoadFromCommonData start; U_TIMEZONE_PACKAGE: %s, tztocPkgPath.data(): %s, tztocEntryName.data(): %s, name: %s\n",
1333 U_TIMEZONE_PACKAGE
, tztocPkgPath
.data(), tztocEntryName
.data(), name
);
1335 retVal
= doLoadFromCommonData(FALSE
, "" /*ignored*/, "" /*ignored*/, "" /*ignored*/,
1336 tztocEntryName
.data(), // tocEntryName, like icutz44/zoneinfo64.res
1337 tztocPkgPath
.data(), // path = path to pkg, like /usr/share/icu/icutz44l
1338 type
, name
, isAcceptable
, context
, &subErrorCode
, &tzpkgErrorCode
);
1339 #ifdef UDATA_TZFILES_DEBUG
1340 fprintf(stderr
, "# dOC tz pkg, doLoadFromCommonData end; status %d, retVal %p\n", tzpkgErrorCode
, retVal
);
1342 if(U_SUCCESS(tzpkgErrorCode
) && retVal
!= NULL
) {
1345 #endif /* defined(U_TIMEZONE_PACKAGE) */
1346 // The following assumes any timezone resources in tzFilesDir are in individual .res files
1347 #ifdef UDATA_TZFILES_DEBUG
1348 fprintf(stderr
, "# dOC tz files, doLoadFromIndividualFiles start; tzFilesDir: %s, tocEntryPathSuffix: %s, name: %s\n",
1349 tzFilesDir
, tocEntryPathSuffix
, name
);
1351 retVal
= doLoadFromIndividualFiles(/* pkgName.data() */ "", tzFilesDir
, tocEntryPathSuffix
,
1352 /* path */ "", type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1353 #ifdef UDATA_TZFILES_DEBUG
1354 fprintf(stderr
, "# dOC tz files, doLoadFromIndividualFiles end; status %d, retVal %p\n", *pErrorCode
, retVal
);
1356 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1362 /**** COMMON PACKAGE - only if packages are first. */
1363 if(gDataFileAccess
== UDATA_PACKAGES_FIRST
) {
1365 fprintf(stderr
, "Trying packages (UDATA_PACKAGES_FIRST)\n");
1368 #ifdef UDATA_TZFILES_DEBUG
1369 if (isTimeZoneFile(name
, type
)) {
1370 fprintf(stderr
, "# dOC std common 1, doLoadFromCommonData start; U_TIMEZONE_PACKAGE: path: %s, tocEntryName.data(): %s, name: %s\n",
1371 path
, tocEntryName
.data(), name
);
1374 retVal
= doLoadFromCommonData(isICUData
,
1375 pkgName
.data(), dataPath
, tocEntryPathSuffix
, tocEntryName
.data(),
1376 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1377 #ifdef UDATA_TZFILES_DEBUG
1378 if (isTimeZoneFile(name
, type
)) {
1379 fprintf(stderr
, "# dOC std common 1, doLoadFromCommonData end; status %d, retVal %p\n", *pErrorCode
, retVal
);
1382 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1387 /**** INDIVIDUAL FILES */
1388 if((gDataFileAccess
==UDATA_PACKAGES_FIRST
) ||
1389 (gDataFileAccess
==UDATA_FILES_FIRST
)) {
1391 fprintf(stderr
, "Trying individual files\n");
1393 /* Check to make sure that there is a dataPath to iterate over */
1394 if ((dataPath
&& *dataPath
) || !isICUData
) {
1395 #ifdef UDATA_TZFILES_DEBUG
1396 if (isTimeZoneFile(name
, type
)) {
1397 fprintf(stderr
, "# dOC std indiv files, doLoadFromIndividualFiles start; dataPath: %s, tocEntryPathSuffix: %s, name: %s\n",
1398 dataPath
, tocEntryPathSuffix
, name
);
1401 retVal
= doLoadFromIndividualFiles(pkgName
.data(), dataPath
, tocEntryPathSuffix
,
1402 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1403 #ifdef UDATA_TZFILES_DEBUG
1404 if (isTimeZoneFile(name
, type
)) {
1405 fprintf(stderr
, "# dOC std indiv files, doLoadFromIndividualFiles end; status %d, retVal %p\n", *pErrorCode
, retVal
);
1408 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1414 /**** COMMON PACKAGE */
1415 if((gDataFileAccess
==UDATA_ONLY_PACKAGES
) ||
1416 (gDataFileAccess
==UDATA_FILES_FIRST
)) {
1418 fprintf(stderr
, "Trying packages (UDATA_ONLY_PACKAGES || UDATA_FILES_FIRST)\n");
1420 #ifdef UDATA_TZFILES_DEBUG
1421 if (isTimeZoneFile(name
, type
)) {
1422 fprintf(stderr
, "# dOC std common 2, doLoadFromCommonData start; U_TIMEZONE_PACKAGE: path: %s, tocEntryName.data(): %s, name: %s\n",
1423 path
, tocEntryName
.data(), name
);
1426 retVal
= doLoadFromCommonData(isICUData
,
1427 pkgName
.data(), dataPath
, tocEntryPathSuffix
, tocEntryName
.data(),
1428 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1429 #ifdef UDATA_TZFILES_DEBUG
1430 if (isTimeZoneFile(name
, type
)) {
1431 fprintf(stderr
, "# dOC std common 2, doLoadFromCommonData end; status %d, retVal %p\n", *pErrorCode
, retVal
);
1434 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1439 /* Load from DLL. If we haven't attempted package load, we also haven't had any chance to
1440 try a DLL (static or setCommonData/etc) load.
1441 If we ever have a "UDATA_ONLY_FILES", add it to the or list here. */
1442 if(gDataFileAccess
==UDATA_NO_FILES
) {
1444 fprintf(stderr
, "Trying common data (UDATA_NO_FILES)\n");
1446 retVal
= doLoadFromCommonData(isICUData
,
1447 pkgName
.data(), "", tocEntryPathSuffix
, tocEntryName
.data(),
1448 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1449 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1454 /* data not found */
1455 if(U_SUCCESS(*pErrorCode
)) {
1456 if(U_SUCCESS(subErrorCode
)) {
1457 /* file not found */
1458 *pErrorCode
=U_FILE_ACCESS_ERROR
;
1460 /* entry point not found or rejected */
1461 *pErrorCode
=subErrorCode
;
1469 /* API ---------------------------------------------------------------------- */
1471 U_CAPI UDataMemory
* U_EXPORT2
1472 udata_open(const char *path
, const char *type
, const char *name
,
1473 UErrorCode
*pErrorCode
) {
1475 fprintf(stderr
, "udata_open(): Opening: %s : %s . %s\n", (path
?path
:"NULL"), name
, type
);
1479 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1481 } else if(name
==NULL
|| *name
==0) {
1482 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1485 return doOpenChoice(path
, type
, name
, NULL
, NULL
, pErrorCode
);
1491 U_CAPI UDataMemory
* U_EXPORT2
1492 udata_openChoice(const char *path
, const char *type
, const char *name
,
1493 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
1494 UErrorCode
*pErrorCode
) {
1496 fprintf(stderr
, "udata_openChoice(): Opening: %s : %s . %s\n", (path
?path
:"NULL"), name
, type
);
1499 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1501 } else if(name
==NULL
|| *name
==0 || isAcceptable
==NULL
) {
1502 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1505 return doOpenChoice(path
, type
, name
, isAcceptable
, context
, pErrorCode
);
1511 U_CAPI
void U_EXPORT2
1512 udata_getInfo(UDataMemory
*pData
, UDataInfo
*pInfo
) {
1514 if(pData
!=NULL
&& pData
->pHeader
!=NULL
) {
1515 const UDataInfo
*info
=&pData
->pHeader
->info
;
1516 uint16_t dataInfoSize
=udata_getInfoSize(info
);
1517 if(pInfo
->size
>dataInfoSize
) {
1518 pInfo
->size
=dataInfoSize
;
1520 uprv_memcpy((uint16_t *)pInfo
+1, (const uint16_t *)info
+1, pInfo
->size
-2);
1521 if(info
->isBigEndian
!=U_IS_BIG_ENDIAN
) {
1522 /* opposite endianness */
1523 uint16_t x
=info
->reservedWord
;
1524 pInfo
->reservedWord
=(uint16_t)((x
<<8)|(x
>>8));
1533 U_CAPI
void U_EXPORT2
udata_setFileAccess(UDataFileAccess access
, UErrorCode
* /*status*/)
1535 // Note: this function is documented as not thread safe.
1536 gDataFileAccess
= access
;