2 ******************************************************************************
4 * Copyright (C) 1999-2013, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 1999oct25
14 * created by: Markus W. Scherer
17 #include "unicode/utypes.h" /* U_PLATFORM etc. */
21 #define ATTRIBUTE_WEAK __attribute__ ((weak))
22 might have to #include some other header
26 #include "unicode/putil.h"
27 #include "unicode/udata.h"
28 #include "unicode/uversion.h"
41 /***********************************************************************
43 * Notes on the organization of the ICU data implementation
45 * All of the public API is defined in udata.h
47 * The implementation is split into several files...
49 * - udata.c (this file) contains higher level code that knows about
50 * the search paths for locating data, caching opened data, etc.
52 * - umapfile.c contains the low level platform-specific code for actually loading
53 * (memory mapping, file reading, whatever) data into memory.
55 * - ucmndata.c deals with the tables of contents of ICU data items within
56 * an ICU common format data file. The implementation includes
57 * an abstract interface and support for multiple TOC formats.
58 * All knowledge of any specific TOC format is encapsulated here.
60 * - udatamem.c has code for managing UDataMemory structs. These are little
61 * descriptor objects for blocks of memory holding ICU data of
65 /* configuration ---------------------------------------------------------- */
67 /* If you are excruciatingly bored turn this on .. */
68 /* #define UDATA_DEBUG 1 */
70 #if defined(UDATA_DEBUG)
74 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
79 * Forward declarations
81 static UDataMemory
*udata_findCachedData(const char *path
);
83 /***********************************************************************
85 * static (Global) data
87 ************************************************************************/
90 * Pointers to the common ICU data.
92 * We store multiple pointers to ICU data packages and iterate through them
93 * when looking for a data item.
95 * It is possible to combine this with dependency inversion:
96 * One or more data package libraries may export
97 * functions that each return a pointer to their piece of the ICU data,
98 * and this file would import them as weak functions, without a
99 * strong linker dependency from the common library on the data library.
101 * Then we can have applications depend on only that part of ICU's data
102 * that they really need, reducing the size of binaries that take advantage
105 static UDataMemory
*gCommonICUDataArray
[10] = { NULL
};
107 static UBool gHaveTriedToLoadCommonData
= FALSE
; /* See extendICUData(). */
109 static UHashtable
*gCommonDataCache
= NULL
; /* Global hash table of opened ICU data files. */
110 static icu::UInitOnce gCommonDataCacheInitOnce
= U_INITONCE_INITIALIZER
;
112 static UDataFileAccess gDataFileAccess
= UDATA_DEFAULT_ACCESS
;
114 static UBool U_CALLCONV
119 if (gCommonDataCache
) { /* Delete the cache of user data mappings. */
120 uhash_close(gCommonDataCache
); /* Table owns the contents, and will delete them. */
121 gCommonDataCache
= NULL
; /* Cleanup is not thread safe. */
123 gCommonDataCacheInitOnce
.reset();
125 for (i
= 0; i
< LENGTHOF(gCommonICUDataArray
) && gCommonICUDataArray
[i
] != NULL
; ++i
) {
126 udata_close(gCommonICUDataArray
[i
]);
127 gCommonICUDataArray
[i
] = NULL
;
129 gHaveTriedToLoadCommonData
= FALSE
;
131 return TRUE
; /* Everything was cleaned up */
134 static UBool U_CALLCONV
135 findCommonICUDataByName(const char *inBasename
)
140 UDataMemory
*pData
= udata_findCachedData(inBasename
);
144 for (i
= 0; i
< LENGTHOF(gCommonICUDataArray
); ++i
) {
145 if ((gCommonICUDataArray
[i
] != NULL
) && (gCommonICUDataArray
[i
]->pHeader
== pData
->pHeader
)) {
146 /* The data pointer is already in the array. */
157 * setCommonICUData. Set a UDataMemory to be the global ICU Data
160 setCommonICUData(UDataMemory
*pData
, /* The new common data. Belongs to caller, we copy it. */
161 UBool warn
, /* If true, set USING_DEFAULT warning if ICUData was */
162 /* changed by another thread before we got to it. */
165 UDataMemory
*newCommonData
= UDataMemory_createNewInstance(pErr
);
167 UBool didUpdate
= FALSE
;
168 if (U_FAILURE(*pErr
)) {
172 /* For the assignment, other threads must cleanly see either the old */
173 /* or the new, not some partially initialized new. The old can not be */
174 /* deleted - someone may still have a pointer to it lying around in */
176 UDatamemory_assign(newCommonData
, pData
);
178 for (i
= 0; i
< LENGTHOF(gCommonICUDataArray
); ++i
) {
179 if (gCommonICUDataArray
[i
] == NULL
) {
180 gCommonICUDataArray
[i
] = newCommonData
;
181 ucln_common_registerCleanup(UCLN_COMMON_UDATA
, udata_cleanup
);
184 } else if (gCommonICUDataArray
[i
]->pHeader
== pData
->pHeader
) {
185 /* The same data pointer is already in the array. */
191 if (i
== LENGTHOF(gCommonICUDataArray
) && warn
) {
192 *pErr
= U_USING_DEFAULT_WARNING
;
195 uprv_free(newCommonData
);
201 setCommonICUDataPointer(const void *pData
, UBool
/*warn*/, UErrorCode
*pErrorCode
) {
203 UDataMemory_init(&tData
);
204 UDataMemory_setData(&tData
, pData
);
205 udata_checkCommonData(&tData
, pErrorCode
);
206 return setCommonICUData(&tData
, FALSE
, pErrorCode
);
210 findBasename(const char *path
) {
211 const char *basename
=uprv_strrchr(path
, U_FILE_SEP_CHAR
);
221 packageNameFromPath(const char *path
)
223 if((path
== NULL
) || (*path
== 0)) {
224 return U_ICUDATA_NAME
;
227 path
= findBasename(path
);
229 if((path
== NULL
) || (*path
== 0)) {
230 return U_ICUDATA_NAME
;
237 /*----------------------------------------------------------------------*
239 * Cache for common data *
240 * Functions for looking up or adding entries to a cache of *
241 * data that has been previously opened. Avoids a potentially *
242 * expensive operation of re-opening the data for subsequent *
245 * Data remains cached for the duration of the process. *
247 *----------------------------------------------------------------------*/
249 typedef struct DataCacheElement
{
257 * Deleter function for DataCacheElements.
258 * udata cleanup function closes the hash table; hash table in turn calls back to
259 * here for each entry.
261 static void U_CALLCONV
DataCacheElement_deleter(void *pDCEl
) {
262 DataCacheElement
*p
= (DataCacheElement
*)pDCEl
;
263 udata_close(p
->item
); /* unmaps storage */
264 uprv_free(p
->name
); /* delete the hash key string. */
265 uprv_free(pDCEl
); /* delete 'this' */
268 static void udata_initHashTable() {
269 UErrorCode err
= U_ZERO_ERROR
;
270 U_ASSERT(gCommonDataCache
== NULL
);
271 gCommonDataCache
= uhash_open(uhash_hashChars
, uhash_compareChars
, NULL
, &err
);
272 if (U_FAILURE(err
)) {
273 // TODO: handle errors better.
274 gCommonDataCache
= NULL
;
276 if (gCommonDataCache
!= NULL
) {
277 uhash_setValueDeleter(gCommonDataCache
, DataCacheElement_deleter
);
278 ucln_common_registerCleanup(UCLN_COMMON_UDATA
, udata_cleanup
);
282 /* udata_getCacheHashTable()
283 * Get the hash table used to store the data cache entries.
284 * Lazy create it if it doesn't yet exist.
286 static UHashtable
*udata_getHashTable() {
287 umtx_initOnce(gCommonDataCacheInitOnce
, &udata_initHashTable
);
288 return gCommonDataCache
;
293 static UDataMemory
*udata_findCachedData(const char *path
)
296 UDataMemory
*retVal
= NULL
;
297 DataCacheElement
*el
;
298 const char *baseName
;
300 baseName
= findBasename(path
); /* Cache remembers only the base name, not the full path. */
301 htable
= udata_getHashTable();
303 el
= (DataCacheElement
*)uhash_get(htable
, baseName
);
309 fprintf(stderr
, "Cache: [%s] -> %p\n", baseName
, retVal
);
315 static UDataMemory
*udata_cacheDataItem(const char *path
, UDataMemory
*item
, UErrorCode
*pErr
) {
316 DataCacheElement
*newElement
;
317 const char *baseName
;
320 DataCacheElement
*oldValue
= NULL
;
321 UErrorCode subErr
= U_ZERO_ERROR
;
323 if (U_FAILURE(*pErr
)) {
327 /* Create a new DataCacheElement - the thingy we store in the hash table -
328 * and copy the supplied path and UDataMemoryItems into it.
330 newElement
= (DataCacheElement
*)uprv_malloc(sizeof(DataCacheElement
));
331 if (newElement
== NULL
) {
332 *pErr
= U_MEMORY_ALLOCATION_ERROR
;
335 newElement
->item
= UDataMemory_createNewInstance(pErr
);
336 if (U_FAILURE(*pErr
)) {
337 uprv_free(newElement
);
340 UDatamemory_assign(newElement
->item
, item
);
342 baseName
= findBasename(path
);
343 nameLen
= (int32_t)uprv_strlen(baseName
);
344 newElement
->name
= (char *)uprv_malloc(nameLen
+1);
345 if (newElement
->name
== NULL
) {
346 *pErr
= U_MEMORY_ALLOCATION_ERROR
;
347 uprv_free(newElement
->item
);
348 uprv_free(newElement
);
351 uprv_strcpy(newElement
->name
, baseName
);
353 /* Stick the new DataCacheElement into the hash table.
355 htable
= udata_getHashTable();
357 oldValue
= (DataCacheElement
*)uhash_get(htable
, path
);
358 if (oldValue
!= NULL
) {
359 subErr
= U_USING_DEFAULT_WARNING
;
364 newElement
->name
, /* Key */
365 newElement
, /* Value */
371 fprintf(stderr
, "Cache: [%s] <<< %p : %s. vFunc=%p\n", newElement
->name
,
372 newElement
->item
, u_errorName(subErr
), newElement
->item
->vFuncs
);
375 if (subErr
== U_USING_DEFAULT_WARNING
|| U_FAILURE(subErr
)) {
376 *pErr
= subErr
; /* copy sub err unto fillin ONLY if something happens. */
377 uprv_free(newElement
->name
);
378 uprv_free(newElement
->item
);
379 uprv_free(newElement
);
380 return oldValue
? oldValue
->item
: NULL
;
383 return newElement
->item
;
386 /*----------------------------------------------------------------------*==============
388 * Path management. Could be shared with other tools/etc if need be *
391 *----------------------------------------------------------------------*/
393 #define U_DATA_PATHITER_BUFSIZ 128 /* Size of local buffer for paths */
394 /* Overflow causes malloc of larger buf */
398 class UDataPathIterator
401 UDataPathIterator(const char *path
, const char *pkg
,
402 const char *item
, const char *suffix
, UBool doCheckLastFour
,
403 UErrorCode
*pErrorCode
);
404 const char *next(UErrorCode
*pErrorCode
);
407 const char *path
; /* working path (u_icudata_Dir) */
408 const char *nextPath
; /* path following this one */
409 const char *basename
; /* item's basename (icudt22e_mt.res)*/
410 const char *suffix
; /* item suffix (can be null) */
412 uint32_t basenameLen
; /* length of basename */
414 CharString itemPath
; /* path passed in with item name */
415 CharString pathBuffer
; /* output path for this it'ion */
416 CharString packageStub
; /* example: "/icudt28b". Will ignore that leaf in set paths. */
418 UBool checkLastFour
; /* if TRUE then allow paths such as '/foo/myapp.dat'
419 * to match, checks last 4 chars of suffix with
420 * last 4 of path, then previous chars. */
424 * @param iter The iterator to be initialized. Its current state does not matter.
425 * @param path The full pathname to be iterated over. If NULL, defaults to U_ICUDATA_NAME
426 * @param pkg Package which is being searched for, ex "icudt28l". Will ignore leave directories such as /icudt28l
427 * @param item Item to be searched for. Can include full path, such as /a/b/foo.dat
428 * @param suffix Optional item suffix, if not-null (ex. ".dat") then 'path' can contain 'item' explicitly.
429 * Ex: 'stuff.dat' would be found in '/a/foo:/tmp/stuff.dat:/bar/baz' as item #2.
430 * '/blarg/stuff.dat' would also be found.
432 UDataPathIterator::UDataPathIterator(const char *inPath
, const char *pkg
,
433 const char *item
, const char *inSuffix
, UBool doCheckLastFour
,
434 UErrorCode
*pErrorCode
)
437 fprintf(stderr
, "SUFFIX1=%s PATH=%s\n", inSuffix
, inPath
);
441 path
= u_getDataDirectory();
448 packageStub
.append(U_FILE_SEP_CHAR
, *pErrorCode
).append(pkg
, *pErrorCode
);
450 fprintf(stderr
, "STUB=%s [%d]\n", packageStub
.data(), packageStub
.length());
455 basename
= findBasename(item
);
456 basenameLen
= (int32_t)uprv_strlen(basename
);
459 if(basename
== item
) {
462 itemPath
.append(item
, (int32_t)(basename
-item
), *pErrorCode
);
463 nextPath
= itemPath
.data();
466 fprintf(stderr
, "SUFFIX=%s [%p]\n", inSuffix
, inSuffix
);
470 if(inSuffix
!= NULL
) {
476 checkLastFour
= doCheckLastFour
;
478 /* pathBuffer will hold the output path strings returned by this iterator */
481 fprintf(stderr
, "%p: init %s -> [path=%s], [base=%s], [suff=%s], [itempath=%s], [nextpath=%s], [checklast4=%s]\n",
489 checkLastFour
?"TRUE":"false");
494 * Get the next path on the list.
496 * @param iter The Iter to be used
497 * @param len If set, pointer to the length of the returned path, for convenience.
498 * @return Pointer to the next path segment, or NULL if there are no more.
500 const char *UDataPathIterator::next(UErrorCode
*pErrorCode
)
502 if(U_FAILURE(*pErrorCode
)) {
506 const char *currentPath
= NULL
;
508 const char *pathBasename
;
512 if( nextPath
== NULL
) {
515 currentPath
= nextPath
;
517 if(nextPath
== itemPath
.data()) { /* we were processing item's path. */
518 nextPath
= path
; /* start with regular path next tm. */
519 pathLen
= (int32_t)uprv_strlen(currentPath
);
521 /* fix up next for next time */
522 nextPath
= uprv_strchr(currentPath
, U_PATH_SEP_CHAR
);
523 if(nextPath
== NULL
) {
524 /* segment: entire path */
525 pathLen
= (int32_t)uprv_strlen(currentPath
);
527 /* segment: until next segment */
528 pathLen
= (int32_t)(nextPath
- currentPath
);
539 fprintf(stderr
, "rest of path (IDD) = %s\n", currentPath
);
540 fprintf(stderr
, " ");
543 for(qqq
=0;qqq
<pathLen
;qqq
++)
545 fprintf(stderr
, " ");
548 fprintf(stderr
, "^\n");
551 pathBuffer
.clear().append(currentPath
, pathLen
, *pErrorCode
);
553 /* check for .dat files */
554 pathBasename
= findBasename(pathBuffer
.data());
556 if(checkLastFour
== TRUE
&&
558 uprv_strncmp(pathBuffer
.data() +(pathLen
-4), suffix
, 4)==0 && /* suffix matches */
559 uprv_strncmp(findBasename(pathBuffer
.data()), basename
, basenameLen
)==0 && /* base matches */
560 uprv_strlen(pathBasename
)==(basenameLen
+4)) { /* base+suffix = full len */
563 fprintf(stderr
, "Have %s file on the path: %s\n", suffix
, pathBuffer
.data());
568 { /* regular dir path */
569 if(pathBuffer
[pathLen
-1] != U_FILE_SEP_CHAR
) {
571 uprv_strncmp(pathBuffer
.data()+(pathLen
-4), ".dat", 4) == 0)
574 fprintf(stderr
, "skipping non-directory .dat file %s\n", pathBuffer
.data());
579 /* Check if it is a directory with the same name as our package */
580 if(!packageStub
.isEmpty() &&
581 (pathLen
> packageStub
.length()) &&
582 !uprv_strcmp(pathBuffer
.data() + pathLen
- packageStub
.length(), packageStub
.data())) {
584 fprintf(stderr
, "Found stub %s (will add package %s of len %d)\n", packageStub
.data(), basename
, basenameLen
);
586 pathBuffer
.truncate(pathLen
- packageStub
.length());
588 pathBuffer
.append(U_FILE_SEP_CHAR
, *pErrorCode
);
592 pathBuffer
.append(packageStub
.data()+1, packageStub
.length()-1, *pErrorCode
);
594 if(*suffix
) /* tack on suffix */
596 pathBuffer
.append(suffix
, *pErrorCode
);
601 fprintf(stderr
, " --> %s\n", pathBuffer
.data());
604 return pathBuffer
.data();
608 /* fell way off the end */
614 /* ==================================================================================*/
617 /*----------------------------------------------------------------------*
619 * Add a static reference to the common data library *
620 * Unless overridden by an explicit udata_setCommonData, this will be *
623 *----------------------------------------------------------------------*/
624 extern "C" const DataHeader U_DATA_API U_ICUDATA_ENTRY_POINT
;
627 * This would be a good place for weak-linkage declarations of
628 * partial-data-library access functions where each returns a pointer
629 * to its data package, if it is linked in.
632 extern const void *uprv_getICUData_collation(void) ATTRIBUTE_WEAK;
633 extern const void *uprv_getICUData_conversion(void) ATTRIBUTE_WEAK;
636 /*----------------------------------------------------------------------*
638 * openCommonData Attempt to open a common format (.dat) file *
639 * Map it into memory (if it's not there already) *
640 * and return a UDataMemory object for it. *
642 * If the requested data is already open and cached *
643 * just return the cached UDataMem object. *
645 *----------------------------------------------------------------------*/
647 openCommonData(const char *path
, /* Path from OpenChoice? */
648 int32_t commonDataIndex
, /* ICU Data (index >= 0) if path == NULL */
649 UErrorCode
*pErrorCode
)
652 const char *pathBuffer
;
653 const char *inBasename
;
655 if (U_FAILURE(*pErrorCode
)) {
659 UDataMemory_init(&tData
);
661 /* ??????? TODO revisit this */
662 if (commonDataIndex
>= 0) {
663 /* "mini-cache" for common ICU data */
664 if(commonDataIndex
>= LENGTHOF(gCommonICUDataArray
)) {
667 if(gCommonICUDataArray
[commonDataIndex
] == NULL
) {
669 for(i
= 0; i
< commonDataIndex
; ++i
) {
670 if(gCommonICUDataArray
[i
]->pHeader
== &U_ICUDATA_ENTRY_POINT
) {
671 /* The linked-in data is already in the list. */
676 /* Add the linked-in data to the list. */
678 * This is where we would check and call weakly linked partial-data-library
682 if (uprv_getICUData_collation) {
683 setCommonICUDataPointer(uprv_getICUData_collation(), FALSE, pErrorCode);
685 if (uprv_getICUData_conversion) {
686 setCommonICUDataPointer(uprv_getICUData_conversion(), FALSE, pErrorCode);
689 setCommonICUDataPointer(&U_ICUDATA_ENTRY_POINT
, FALSE
, pErrorCode
);
691 return gCommonICUDataArray
[commonDataIndex
];
695 /* request is NOT for ICU Data. */
697 /* Find the base name portion of the supplied path. */
698 /* inBasename will be left pointing somewhere within the original path string. */
699 inBasename
= findBasename(path
);
701 fprintf(stderr
, "inBasename = %s\n", inBasename
);
705 /* no basename. This will happen if the original path was a directory name, */
706 /* like "a/b/c/". (Fallback to separate files will still work.) */
708 fprintf(stderr
, "ocd: no basename in %s, bailing.\n", path
);
710 *pErrorCode
=U_FILE_ACCESS_ERROR
;
714 /* Is the requested common data file already open and cached? */
715 /* Note that the cache is keyed by the base name only. The rest of the path, */
716 /* if any, is not considered. */
718 UDataMemory
*dataToReturn
= udata_findCachedData(inBasename
);
719 if (dataToReturn
!= NULL
) {
724 /* Requested item is not in the cache.
725 * Hunt it down, trying all the path locations
728 UDataPathIterator
iter(u_getDataDirectory(), inBasename
, path
, ".dat", TRUE
, pErrorCode
);
730 while((UDataMemory_isLoaded(&tData
)==FALSE
) && (pathBuffer
= iter
.next(pErrorCode
)) != NULL
)
733 fprintf(stderr
, "ocd: trying path %s - ", pathBuffer
);
735 uprv_mapFile(&tData
, pathBuffer
);
737 fprintf(stderr
, "%s\n", UDataMemory_isLoaded(&tData
)?"LOADED":"not loaded");
741 #if defined(OS390_STUBDATA) && defined(OS390BATCH)
742 if (!UDataMemory_isLoaded(&tData
)) {
743 char ourPathBuffer
[1024];
744 /* One more chance, for extendCommonData() */
745 uprv_strncpy(ourPathBuffer
, path
, 1019);
746 ourPathBuffer
[1019]=0;
747 uprv_strcat(ourPathBuffer
, ".dat");
748 uprv_mapFile(&tData
, ourPathBuffer
);
752 if (!UDataMemory_isLoaded(&tData
)) {
754 *pErrorCode
=U_FILE_ACCESS_ERROR
;
758 /* we have mapped a file, check its header */
759 udata_checkCommonData(&tData
, pErrorCode
);
762 /* Cache the UDataMemory struct for this .dat file,
763 * so we won't need to hunt it down and map it again next time
764 * something is needed from it. */
765 return udata_cacheDataItem(inBasename
, &tData
, pErrorCode
);
769 /*----------------------------------------------------------------------*
771 * extendICUData If the full set of ICU data was not loaded at *
772 * program startup, load it now. This function will *
773 * be called when the lookup of an ICU data item in *
774 * the common ICU data fails. *
776 * return true if new data is loaded, false otherwise.*
778 *----------------------------------------------------------------------*/
779 static UBool
extendICUData(UErrorCode
*pErr
)
782 UDataMemory copyPData
;
783 UBool didUpdate
= FALSE
;
786 * There is a chance for a race condition here.
787 * Normally, ICU data is loaded from a DLL or via mmap() and
788 * setCommonICUData() will detect if the same address is set twice.
789 * If ICU is built with data loading via fread() then the address will
790 * be different each time the common data is loaded and we may add
791 * multiple copies of the data.
792 * In this case, use a mutex to prevent the race.
793 * Use a specific mutex to avoid nested locks of the global mutex.
795 #if MAP_IMPLEMENTATION==MAP_STDIO
796 static UMutex extendICUDataMutex
= U_MUTEX_INITIALIZER
;
797 umtx_lock(&extendICUDataMutex
);
799 if(!gHaveTriedToLoadCommonData
) {
800 /* See if we can explicitly open a .dat file for the ICUData. */
801 pData
= openCommonData(
802 U_ICUDATA_NAME
, /* "icudt20l" , for example. */
803 -1, /* Pretend we're not opening ICUData */
806 /* How about if there is no pData, eh... */
808 UDataMemory_init(©PData
);
810 UDatamemory_assign(©PData
, pData
);
811 copyPData
.map
= 0; /* The mapping for this data is owned by the hash table */
812 copyPData
.mapAddr
= 0; /* which will unmap it when ICU is shut down. */
813 /* CommonICUData is also unmapped when ICU is shut down.*/
814 /* To avoid unmapping the data twice, zero out the map */
815 /* fields in the UDataMemory that we're assigning */
816 /* to CommonICUData. */
818 didUpdate
= /* no longer using this result */
819 setCommonICUData(©PData
,/* The new common data. */
820 FALSE
, /* No warnings if write didn't happen */
821 pErr
); /* setCommonICUData honors errors; NOP if error set */
824 gHaveTriedToLoadCommonData
= TRUE
;
827 didUpdate
= findCommonICUDataByName(U_ICUDATA_NAME
); /* Return 'true' when a racing writes out the extended */
828 /* data after another thread has failed to see it (in openCommonData), so */
829 /* extended data can be examined. */
830 /* Also handles a race through here before gHaveTriedToLoadCommonData is set. */
832 #if MAP_IMPLEMENTATION==MAP_STDIO
833 umtx_unlock(&extendICUDataMutex
);
835 return didUpdate
; /* Return true if ICUData pointer was updated. */
836 /* (Could potentialy have been done by another thread racing */
837 /* us through here, but that's fine, we still return true */
838 /* so that current thread will also examine extended data. */
841 /*----------------------------------------------------------------------*
843 * udata_setCommonData *
845 *----------------------------------------------------------------------*/
846 U_CAPI
void U_EXPORT2
847 udata_setCommonData(const void *data
, UErrorCode
*pErrorCode
) {
848 UDataMemory dataMemory
;
850 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
855 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
859 /* set the data pointer and test for validity */
860 UDataMemory_init(&dataMemory
);
861 UDataMemory_setData(&dataMemory
, data
);
862 udata_checkCommonData(&dataMemory
, pErrorCode
);
863 if (U_FAILURE(*pErrorCode
)) {return;}
865 /* we have good data */
866 /* Set it up as the ICU Common Data. */
867 setCommonICUData(&dataMemory
, TRUE
, pErrorCode
);
870 /*---------------------------------------------------------------------------
874 *---------------------------------------------------------------------------- */
875 U_CAPI
void U_EXPORT2
876 udata_setAppData(const char *path
, const void *data
, UErrorCode
*err
)
880 if(err
==NULL
|| U_FAILURE(*err
)) {
884 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
888 UDataMemory_init(&udm
);
889 UDataMemory_setData(&udm
, data
);
890 udata_checkCommonData(&udm
, err
);
891 udata_cacheDataItem(path
, &udm
, err
);
894 /*----------------------------------------------------------------------------*
896 * checkDataItem Given a freshly located/loaded data item, either *
897 * an entry in a common file or a separately loaded file, *
898 * sanity check its header, and see if the data is *
899 * acceptable to the app. *
900 * If the data is good, create and return a UDataMemory *
901 * object that can be returned to the application. *
902 * Return NULL on any sort of failure. *
904 *----------------------------------------------------------------------------*/
908 const DataHeader
*pHeader
, /* The data item to be checked. */
909 UDataMemoryIsAcceptable
*isAcceptable
, /* App's call-back function */
910 void *context
, /* pass-thru param for above. */
911 const char *type
, /* pass-thru param for above. */
912 const char *name
, /* pass-thru param for above. */
913 UErrorCode
*nonFatalErr
, /* Error code if this data was not acceptable */
914 /* but openChoice should continue with */
915 /* trying to get data from fallback path. */
916 UErrorCode
*fatalErr
/* Bad error, caller should return immediately */
919 UDataMemory
*rDataMem
= NULL
; /* the new UDataMemory, to be returned. */
921 if (U_FAILURE(*fatalErr
)) {
925 if(pHeader
->dataHeader
.magic1
==0xda &&
926 pHeader
->dataHeader
.magic2
==0x27 &&
927 (isAcceptable
==NULL
|| isAcceptable(context
, type
, name
, &pHeader
->info
))
929 rDataMem
=UDataMemory_createNewInstance(fatalErr
);
930 if (U_FAILURE(*fatalErr
)) {
933 rDataMem
->pHeader
= pHeader
;
935 /* the data is not acceptable, look further */
936 /* If we eventually find something good, this errorcode will be */
938 *nonFatalErr
=U_INVALID_FORMAT_ERROR
;
944 * @return 0 if not loaded, 1 if loaded or err
946 static UDataMemory
*doLoadFromIndividualFiles(const char *pkgName
,
947 const char *dataPath
, const char *tocEntryPathSuffix
,
948 /* following arguments are the same as doOpenChoice itself */
949 const char *path
, const char *type
, const char *name
,
950 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
951 UErrorCode
*subErrorCode
,
952 UErrorCode
*pErrorCode
)
954 const char *pathBuffer
;
955 UDataMemory dataMemory
;
956 UDataMemory
*pEntryData
;
958 /* look in ind. files: package\nam.typ ========================= */
959 /* init path iterator for individual files */
960 UDataPathIterator
iter(dataPath
, pkgName
, path
, tocEntryPathSuffix
, FALSE
, pErrorCode
);
962 while((pathBuffer
= iter
.next(pErrorCode
)))
965 fprintf(stderr
, "UDATA: trying individual file %s\n", pathBuffer
);
967 if(uprv_mapFile(&dataMemory
, pathBuffer
))
969 pEntryData
= checkDataItem(dataMemory
.pHeader
, isAcceptable
, context
, type
, name
, subErrorCode
, pErrorCode
);
970 if (pEntryData
!= NULL
) {
972 * Hand off ownership of the backing memory to the user's UDataMemory.
974 pEntryData
->mapAddr
= dataMemory
.mapAddr
;
975 pEntryData
->map
= dataMemory
.map
;
978 fprintf(stderr
, "** Mapped file: %s\n", pathBuffer
);
983 /* the data is not acceptable, or some error occured. Either way, unmap the memory */
984 udata_close(&dataMemory
);
986 /* If we had a nasty error, bail out completely. */
987 if (U_FAILURE(*pErrorCode
)) {
991 /* Otherwise remember that we found data but didn't like it for some reason */
992 *subErrorCode
=U_INVALID_FORMAT_ERROR
;
995 fprintf(stderr
, "%s\n", UDataMemory_isLoaded(&dataMemory
)?"LOADED":"not loaded");
1002 * @return 0 if not loaded, 1 if loaded or err
1004 static UDataMemory
*doLoadFromCommonData(UBool isICUData
, const char * /*pkgName*/,
1005 const char * /*dataPath*/, const char * /*tocEntryPathSuffix*/, const char *tocEntryName
,
1006 /* following arguments are the same as doOpenChoice itself */
1007 const char *path
, const char *type
, const char *name
,
1008 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
1009 UErrorCode
*subErrorCode
,
1010 UErrorCode
*pErrorCode
)
1012 UDataMemory
*pEntryData
;
1013 const DataHeader
*pHeader
;
1014 UDataMemory
*pCommonData
;
1015 int32_t commonDataIndex
;
1016 UBool checkedExtendedICUData
= FALSE
;
1017 /* try to get common data. The loop is for platforms such as the 390 that do
1018 * not initially load the full set of ICU data. If the lookup of an ICU data item
1019 * fails, the full (but slower to load) set is loaded, the and the loop repeats,
1020 * trying the lookup again. Once the full set of ICU data is loaded, the loop wont
1021 * repeat because the full set will be checked the first time through.
1023 * The loop also handles the fallback to a .dat file if the application linked
1024 * to the stub data library rather than a real library.
1026 for (commonDataIndex
= isICUData
? 0 : -1;;) {
1027 pCommonData
=openCommonData(path
, commonDataIndex
, subErrorCode
); /** search for pkg **/
1029 if(U_SUCCESS(*subErrorCode
) && pCommonData
!=NULL
) {
1032 /* look up the data piece in the common data */
1033 pHeader
=pCommonData
->vFuncs
->Lookup(pCommonData
, tocEntryName
, &length
, subErrorCode
);
1035 fprintf(stderr
, "%s: pHeader=%p - %s\n", tocEntryName
, pHeader
, u_errorName(*subErrorCode
));
1039 pEntryData
= checkDataItem(pHeader
, isAcceptable
, context
, type
, name
, subErrorCode
, pErrorCode
);
1041 fprintf(stderr
, "pEntryData=%p\n", pEntryData
);
1043 if (U_FAILURE(*pErrorCode
)) {
1046 if (pEntryData
!= NULL
) {
1047 pEntryData
->length
= length
;
1052 /* Data wasn't found. If we were looking for an ICUData item and there is
1053 * more data available, load it and try again,
1054 * otherwise break out of this loop. */
1057 } else if (pCommonData
!= NULL
) {
1058 ++commonDataIndex
; /* try the next data package */
1059 } else if ((!checkedExtendedICUData
) && extendICUData(subErrorCode
)) {
1060 checkedExtendedICUData
= TRUE
;
1061 /* try this data package slot again: it changed from NULL to non-NULL */
1069 * A note on the ownership of Mapped Memory
1071 * For common format files, ownership resides with the UDataMemory object
1072 * that lives in the cache of opened common data. These UDataMemorys are private
1073 * to the udata implementation, and are never seen directly by users.
1075 * The UDataMemory objects returned to users will have the address of some desired
1076 * data within the mapped region, but they wont have the mapping info itself, and thus
1077 * won't cause anything to be removed from memory when they are closed.
1079 * For individual data files, the UDataMemory returned to the user holds the
1080 * information necessary to unmap the data on close. If the user independently
1081 * opens the same data file twice, two completely independent mappings will be made.
1082 * (There is no cache of opened data items from individual files, only a cache of
1083 * opened Common Data files, that is, files containing a collection of data items.)
1085 * For common data passed in from the user via udata_setAppData() or
1086 * udata_setCommonData(), ownership remains with the user.
1088 * UDataMemory objects themselves, as opposed to the memory they describe,
1089 * can be anywhere - heap, stack/local or global.
1090 * They have a flag to indicate when they're heap allocated and thus
1091 * must be deleted when closed.
1095 /*----------------------------------------------------------------------------*
1097 * main data loading functions *
1099 *----------------------------------------------------------------------------*/
1100 static UDataMemory
*
1101 doOpenChoice(const char *path
, const char *type
, const char *name
,
1102 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
1103 UErrorCode
*pErrorCode
)
1105 UDataMemory
*retVal
= NULL
;
1107 const char *dataPath
;
1109 int32_t tocEntrySuffixIndex
;
1110 const char *tocEntryPathSuffix
;
1111 UErrorCode subErrorCode
=U_ZERO_ERROR
;
1112 const char *treeChar
;
1114 UBool isICUData
= FALSE
;
1117 /* Is this path ICU data? */
1119 !strcmp(path
, U_ICUDATA_ALIAS
) || /* "ICUDATA" */
1120 !uprv_strncmp(path
, U_ICUDATA_NAME U_TREE_SEPARATOR_STRING
, /* "icudt26e-" */
1121 uprv_strlen(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING
)) ||
1122 !uprv_strncmp(path
, U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING
, /* "ICUDATA-" */
1123 uprv_strlen(U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING
))) {
1127 #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) /* Windows: try "foo\bar" and "foo/bar" */
1128 /* remap from alternate path char to the main one */
1129 CharString altSepPath
;
1131 if(uprv_strchr(path
,U_FILE_ALT_SEP_CHAR
) != NULL
) {
1132 altSepPath
.append(path
, *pErrorCode
);
1134 while((p
=uprv_strchr(altSepPath
.data(), U_FILE_ALT_SEP_CHAR
))) {
1135 *p
= U_FILE_SEP_CHAR
;
1137 #if defined (UDATA_DEBUG)
1138 fprintf(stderr
, "Changed path from [%s] to [%s]\n", path
, altSepPath
.s
);
1140 path
= altSepPath
.data();
1145 CharString tocEntryName
; /* entry name in tree format. ex: 'icudt28b/coll/ar.res' */
1146 CharString tocEntryPath
; /* entry name in path format. ex: 'icudt28b\\coll\\ar.res' */
1149 CharString treeName
;
1151 /* ======= Set up strings */
1153 pkgName
.append(U_ICUDATA_NAME
, *pErrorCode
);
1157 pkg
= uprv_strrchr(path
, U_FILE_SEP_CHAR
);
1158 first
= uprv_strchr(path
, U_FILE_SEP_CHAR
);
1159 if(uprv_pathIsAbsolute(path
) || (pkg
!= first
)) { /* more than one slash in the path- not a tree name */
1160 /* see if this is an /absolute/path/to/package path */
1162 pkgName
.append(pkg
+1, *pErrorCode
);
1164 pkgName
.append(path
, *pErrorCode
);
1167 treeChar
= uprv_strchr(path
, U_TREE_SEPARATOR
);
1169 treeName
.append(treeChar
+1, *pErrorCode
); /* following '-' */
1171 pkgName
.append(U_ICUDATA_NAME
, *pErrorCode
);
1173 pkgName
.append(path
, (int32_t)(treeChar
-path
), *pErrorCode
);
1174 if (first
== NULL
) {
1176 This user data has no path, but there is a tree name.
1177 Look up the correct path from the data cache later.
1179 path
= pkgName
.data();
1184 pkgName
.append(U_ICUDATA_NAME
, *pErrorCode
);
1186 pkgName
.append(path
, *pErrorCode
);
1193 fprintf(stderr
, " P=%s T=%s\n", pkgName
.data(), treeName
.data());
1196 /* setting up the entry name and file name
1197 * Make up a full name by appending the type to the supplied
1198 * name, assuming that a type was supplied.
1201 /* prepend the package */
1202 tocEntryName
.append(pkgName
, *pErrorCode
);
1203 tocEntryPath
.append(pkgName
, *pErrorCode
);
1204 tocEntrySuffixIndex
= tocEntryName
.length();
1206 if(!treeName
.isEmpty()) {
1207 tocEntryName
.append(U_TREE_ENTRY_SEP_CHAR
, *pErrorCode
).append(treeName
, *pErrorCode
);
1208 tocEntryPath
.append(U_FILE_SEP_CHAR
, *pErrorCode
).append(treeName
, *pErrorCode
);
1211 tocEntryName
.append(U_TREE_ENTRY_SEP_CHAR
, *pErrorCode
).append(name
, *pErrorCode
);
1212 tocEntryPath
.append(U_FILE_SEP_CHAR
, *pErrorCode
).append(name
, *pErrorCode
);
1213 if(type
!=NULL
&& *type
!=0) {
1214 tocEntryName
.append(".", *pErrorCode
).append(type
, *pErrorCode
);
1215 tocEntryPath
.append(".", *pErrorCode
).append(type
, *pErrorCode
);
1217 tocEntryPathSuffix
= tocEntryPath
.data()+tocEntrySuffixIndex
; /* suffix starts here */
1220 fprintf(stderr
, " tocEntryName = %s\n", tocEntryName
.data());
1221 fprintf(stderr
, " tocEntryPath = %s\n", tocEntryName
.data());
1225 path
= COMMON_DATA_NAME
; /* "icudt26e" */
1228 /************************ Begin loop looking for ind. files ***************/
1230 fprintf(stderr
, "IND: inBasename = %s, pkg=%s\n", "(n/a)", packageNameFromPath(path
));
1233 /* End of dealing with a null basename */
1234 dataPath
= u_getDataDirectory();
1236 /**** COMMON PACKAGE - only if packages are first. */
1237 if(gDataFileAccess
== UDATA_PACKAGES_FIRST
) {
1239 fprintf(stderr
, "Trying packages (UDATA_PACKAGES_FIRST)\n");
1242 retVal
= doLoadFromCommonData(isICUData
,
1243 pkgName
.data(), dataPath
, tocEntryPathSuffix
, tocEntryName
.data(),
1244 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1245 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1250 /**** INDIVIDUAL FILES */
1251 if((gDataFileAccess
==UDATA_PACKAGES_FIRST
) ||
1252 (gDataFileAccess
==UDATA_FILES_FIRST
)) {
1254 fprintf(stderr
, "Trying individual files\n");
1256 /* Check to make sure that there is a dataPath to iterate over */
1257 if ((dataPath
&& *dataPath
) || !isICUData
) {
1258 retVal
= doLoadFromIndividualFiles(pkgName
.data(), dataPath
, tocEntryPathSuffix
,
1259 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1260 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1266 /**** COMMON PACKAGE */
1267 if((gDataFileAccess
==UDATA_ONLY_PACKAGES
) ||
1268 (gDataFileAccess
==UDATA_FILES_FIRST
)) {
1270 fprintf(stderr
, "Trying packages (UDATA_ONLY_PACKAGES || UDATA_FILES_FIRST)\n");
1272 retVal
= doLoadFromCommonData(isICUData
,
1273 pkgName
.data(), dataPath
, tocEntryPathSuffix
, tocEntryName
.data(),
1274 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1275 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1280 /* Load from DLL. If we haven't attempted package load, we also haven't had any chance to
1281 try a DLL (static or setCommonData/etc) load.
1282 If we ever have a "UDATA_ONLY_FILES", add it to the or list here. */
1283 if(gDataFileAccess
==UDATA_NO_FILES
) {
1285 fprintf(stderr
, "Trying common data (UDATA_NO_FILES)\n");
1287 retVal
= doLoadFromCommonData(isICUData
,
1288 pkgName
.data(), "", tocEntryPathSuffix
, tocEntryName
.data(),
1289 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1290 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1295 /* data not found */
1296 if(U_SUCCESS(*pErrorCode
)) {
1297 if(U_SUCCESS(subErrorCode
)) {
1298 /* file not found */
1299 *pErrorCode
=U_FILE_ACCESS_ERROR
;
1301 /* entry point not found or rejected */
1302 *pErrorCode
=subErrorCode
;
1310 /* API ---------------------------------------------------------------------- */
1312 U_CAPI UDataMemory
* U_EXPORT2
1313 udata_open(const char *path
, const char *type
, const char *name
,
1314 UErrorCode
*pErrorCode
) {
1316 fprintf(stderr
, "udata_open(): Opening: %s : %s . %s\n", (path
?path
:"NULL"), name
, type
);
1320 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1322 } else if(name
==NULL
|| *name
==0) {
1323 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1326 return doOpenChoice(path
, type
, name
, NULL
, NULL
, pErrorCode
);
1332 U_CAPI UDataMemory
* U_EXPORT2
1333 udata_openChoice(const char *path
, const char *type
, const char *name
,
1334 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
1335 UErrorCode
*pErrorCode
) {
1337 fprintf(stderr
, "udata_openChoice(): Opening: %s : %s . %s\n", (path
?path
:"NULL"), name
, type
);
1340 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1342 } else if(name
==NULL
|| *name
==0 || isAcceptable
==NULL
) {
1343 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1346 return doOpenChoice(path
, type
, name
, isAcceptable
, context
, pErrorCode
);
1352 U_CAPI
void U_EXPORT2
1353 udata_getInfo(UDataMemory
*pData
, UDataInfo
*pInfo
) {
1355 if(pData
!=NULL
&& pData
->pHeader
!=NULL
) {
1356 const UDataInfo
*info
=&pData
->pHeader
->info
;
1357 uint16_t dataInfoSize
=udata_getInfoSize(info
);
1358 if(pInfo
->size
>dataInfoSize
) {
1359 pInfo
->size
=dataInfoSize
;
1361 uprv_memcpy((uint16_t *)pInfo
+1, (const uint16_t *)info
+1, pInfo
->size
-2);
1362 if(info
->isBigEndian
!=U_IS_BIG_ENDIAN
) {
1363 /* opposite endianness */
1364 uint16_t x
=info
->reservedWord
;
1365 pInfo
->reservedWord
=(uint16_t)((x
<<8)|(x
>>8));
1374 U_CAPI
void U_EXPORT2
udata_setFileAccess(UDataFileAccess access
, UErrorCode
* /*status*/)
1376 gDataFileAccess
= access
;