2 ******************************************************************************
4 * Copyright (C) 1999-2012, 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"
40 /***********************************************************************
42 * Notes on the organization of the ICU data implementation
44 * All of the public API is defined in udata.h
46 * The implementation is split into several files...
48 * - udata.c (this file) contains higher level code that knows about
49 * the search paths for locating data, caching opened data, etc.
51 * - umapfile.c contains the low level platform-specific code for actually loading
52 * (memory mapping, file reading, whatever) data into memory.
54 * - ucmndata.c deals with the tables of contents of ICU data items within
55 * an ICU common format data file. The implementation includes
56 * an abstract interface and support for multiple TOC formats.
57 * All knowledge of any specific TOC format is encapsulated here.
59 * - udatamem.c has code for managing UDataMemory structs. These are little
60 * descriptor objects for blocks of memory holding ICU data of
64 /* configuration ---------------------------------------------------------- */
66 /* If you are excruciatingly bored turn this on .. */
67 /* #define UDATA_DEBUG 1 */
69 #if defined(UDATA_DEBUG)
73 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
78 * Forward declarations
80 static UDataMemory
*udata_findCachedData(const char *path
);
82 /***********************************************************************
84 * static (Global) data
86 ************************************************************************/
89 * Pointers to the common ICU data.
91 * We store multiple pointers to ICU data packages and iterate through them
92 * when looking for a data item.
94 * It is possible to combine this with dependency inversion:
95 * One or more data package libraries may export
96 * functions that each return a pointer to their piece of the ICU data,
97 * and this file would import them as weak functions, without a
98 * strong linker dependency from the common library on the data library.
100 * Then we can have applications depend on only that part of ICU's data
101 * that they really need, reducing the size of binaries that take advantage
104 static UDataMemory
*gCommonICUDataArray
[10] = { NULL
};
106 static UBool gHaveTriedToLoadCommonData
= FALSE
; /* See extendICUData(). */
108 static UHashtable
*gCommonDataCache
= NULL
; /* Global hash table of opened ICU data files. */
110 static UDataFileAccess gDataFileAccess
= UDATA_DEFAULT_ACCESS
;
112 static UBool U_CALLCONV
117 if (gCommonDataCache
) { /* Delete the cache of user data mappings. */
118 uhash_close(gCommonDataCache
); /* Table owns the contents, and will delete them. */
119 gCommonDataCache
= NULL
; /* Cleanup is not thread safe. */
122 for (i
= 0; i
< LENGTHOF(gCommonICUDataArray
) && gCommonICUDataArray
[i
] != NULL
; ++i
) {
123 udata_close(gCommonICUDataArray
[i
]);
124 gCommonICUDataArray
[i
] = NULL
;
126 gHaveTriedToLoadCommonData
= FALSE
;
128 return TRUE
; /* Everything was cleaned up */
131 static UBool U_CALLCONV
132 findCommonICUDataByName(const char *inBasename
)
137 UDataMemory
*pData
= udata_findCachedData(inBasename
);
141 for (i
= 0; i
< LENGTHOF(gCommonICUDataArray
); ++i
) {
142 if ((gCommonICUDataArray
[i
] != NULL
) && (gCommonICUDataArray
[i
]->pHeader
== pData
->pHeader
)) {
143 /* The data pointer is already in the array. */
154 * setCommonICUData. Set a UDataMemory to be the global ICU Data
157 setCommonICUData(UDataMemory
*pData
, /* The new common data. Belongs to caller, we copy it. */
158 UBool warn
, /* If true, set USING_DEFAULT warning if ICUData was */
159 /* changed by another thread before we got to it. */
162 UDataMemory
*newCommonData
= UDataMemory_createNewInstance(pErr
);
164 UBool didUpdate
= FALSE
;
165 if (U_FAILURE(*pErr
)) {
169 /* For the assignment, other threads must cleanly see either the old */
170 /* or the new, not some partially initialized new. The old can not be */
171 /* deleted - someone may still have a pointer to it lying around in */
173 UDatamemory_assign(newCommonData
, pData
);
175 for (i
= 0; i
< LENGTHOF(gCommonICUDataArray
); ++i
) {
176 if (gCommonICUDataArray
[i
] == NULL
) {
177 gCommonICUDataArray
[i
] = newCommonData
;
178 ucln_common_registerCleanup(UCLN_COMMON_UDATA
, udata_cleanup
);
181 } else if (gCommonICUDataArray
[i
]->pHeader
== pData
->pHeader
) {
182 /* The same data pointer is already in the array. */
188 if (i
== LENGTHOF(gCommonICUDataArray
) && warn
) {
189 *pErr
= U_USING_DEFAULT_WARNING
;
192 uprv_free(newCommonData
);
198 setCommonICUDataPointer(const void *pData
, UBool
/*warn*/, UErrorCode
*pErrorCode
) {
200 UDataMemory_init(&tData
);
201 tData
.pHeader
= (const DataHeader
*)pData
;
202 udata_checkCommonData(&tData
, pErrorCode
);
203 return setCommonICUData(&tData
, FALSE
, pErrorCode
);
207 findBasename(const char *path
) {
208 const char *basename
=uprv_strrchr(path
, U_FILE_SEP_CHAR
);
218 packageNameFromPath(const char *path
)
220 if((path
== NULL
) || (*path
== 0)) {
221 return U_ICUDATA_NAME
;
224 path
= findBasename(path
);
226 if((path
== NULL
) || (*path
== 0)) {
227 return U_ICUDATA_NAME
;
234 /*----------------------------------------------------------------------*
236 * Cache for common data *
237 * Functions for looking up or adding entries to a cache of *
238 * data that has been previously opened. Avoids a potentially *
239 * expensive operation of re-opening the data for subsequent *
242 * Data remains cached for the duration of the process. *
244 *----------------------------------------------------------------------*/
246 typedef struct DataCacheElement
{
254 * Deleter function for DataCacheElements.
255 * udata cleanup function closes the hash table; hash table in turn calls back to
256 * here for each entry.
258 static void U_CALLCONV
DataCacheElement_deleter(void *pDCEl
) {
259 DataCacheElement
*p
= (DataCacheElement
*)pDCEl
;
260 udata_close(p
->item
); /* unmaps storage */
261 uprv_free(p
->name
); /* delete the hash key string. */
262 uprv_free(pDCEl
); /* delete 'this' */
265 /* udata_getCacheHashTable()
266 * Get the hash table used to store the data cache entries.
267 * Lazy create it if it doesn't yet exist.
269 static UHashtable
*udata_getHashTable() {
270 UErrorCode err
= U_ZERO_ERROR
;
271 UBool cacheIsInitialized
;
272 UHashtable
*tHT
= NULL
;
274 UMTX_CHECK(NULL
, (gCommonDataCache
!= NULL
), cacheIsInitialized
);
276 if (cacheIsInitialized
) {
277 return gCommonDataCache
;
280 tHT
= uhash_open(uhash_hashChars
, uhash_compareChars
, NULL
, &err
);
281 /* Check for null pointer. */
283 return NULL
; /* TODO: Handle this error better. */
285 uhash_setValueDeleter(tHT
, DataCacheElement_deleter
);
288 if (gCommonDataCache
== NULL
) {
289 gCommonDataCache
= tHT
;
291 ucln_common_registerCleanup(UCLN_COMMON_UDATA
, udata_cleanup
);
298 if (U_FAILURE(err
)) {
299 return NULL
; /* TODO: handle this error better. */
301 return gCommonDataCache
;
306 static UDataMemory
*udata_findCachedData(const char *path
)
309 UDataMemory
*retVal
= NULL
;
310 DataCacheElement
*el
;
311 const char *baseName
;
313 baseName
= findBasename(path
); /* Cache remembers only the base name, not the full path. */
314 htable
= udata_getHashTable();
316 el
= (DataCacheElement
*)uhash_get(htable
, baseName
);
322 fprintf(stderr
, "Cache: [%s] -> %p\n", baseName
, retVal
);
328 static UDataMemory
*udata_cacheDataItem(const char *path
, UDataMemory
*item
, UErrorCode
*pErr
) {
329 DataCacheElement
*newElement
;
330 const char *baseName
;
333 DataCacheElement
*oldValue
= NULL
;
334 UErrorCode subErr
= U_ZERO_ERROR
;
336 if (U_FAILURE(*pErr
)) {
340 /* Create a new DataCacheElement - the thingy we store in the hash table -
341 * and copy the supplied path and UDataMemoryItems into it.
343 newElement
= (DataCacheElement
*)uprv_malloc(sizeof(DataCacheElement
));
344 if (newElement
== NULL
) {
345 *pErr
= U_MEMORY_ALLOCATION_ERROR
;
348 newElement
->item
= UDataMemory_createNewInstance(pErr
);
349 if (U_FAILURE(*pErr
)) {
350 uprv_free(newElement
);
353 UDatamemory_assign(newElement
->item
, item
);
355 baseName
= findBasename(path
);
356 nameLen
= (int32_t)uprv_strlen(baseName
);
357 newElement
->name
= (char *)uprv_malloc(nameLen
+1);
358 if (newElement
->name
== NULL
) {
359 *pErr
= U_MEMORY_ALLOCATION_ERROR
;
360 uprv_free(newElement
->item
);
361 uprv_free(newElement
);
364 uprv_strcpy(newElement
->name
, baseName
);
366 /* Stick the new DataCacheElement into the hash table.
368 htable
= udata_getHashTable();
370 oldValue
= (DataCacheElement
*)uhash_get(htable
, path
);
371 if (oldValue
!= NULL
) {
372 subErr
= U_USING_DEFAULT_WARNING
;
377 newElement
->name
, /* Key */
378 newElement
, /* Value */
384 fprintf(stderr
, "Cache: [%s] <<< %p : %s. vFunc=%p\n", newElement
->name
,
385 newElement
->item
, u_errorName(subErr
), newElement
->item
->vFuncs
);
388 if (subErr
== U_USING_DEFAULT_WARNING
|| U_FAILURE(subErr
)) {
389 *pErr
= subErr
; /* copy sub err unto fillin ONLY if something happens. */
390 uprv_free(newElement
->name
);
391 uprv_free(newElement
->item
);
392 uprv_free(newElement
);
393 return oldValue
? oldValue
->item
: NULL
;
396 return newElement
->item
;
399 /*----------------------------------------------------------------------*==============
401 * Path management. Could be shared with other tools/etc if need be *
404 *----------------------------------------------------------------------*/
406 #define U_DATA_PATHITER_BUFSIZ 128 /* Size of local buffer for paths */
407 /* Overflow causes malloc of larger buf */
411 class UDataPathIterator
414 UDataPathIterator(const char *path
, const char *pkg
,
415 const char *item
, const char *suffix
, UBool doCheckLastFour
,
416 UErrorCode
*pErrorCode
);
417 const char *next(UErrorCode
*pErrorCode
);
420 const char *path
; /* working path (u_icudata_Dir) */
421 const char *nextPath
; /* path following this one */
422 const char *basename
; /* item's basename (icudt22e_mt.res)*/
423 const char *suffix
; /* item suffix (can be null) */
425 uint32_t basenameLen
; /* length of basename */
427 CharString itemPath
; /* path passed in with item name */
428 CharString pathBuffer
; /* output path for this it'ion */
429 CharString packageStub
; /* example: "/icudt28b". Will ignore that leaf in set paths. */
431 UBool checkLastFour
; /* if TRUE then allow paths such as '/foo/myapp.dat'
432 * to match, checks last 4 chars of suffix with
433 * last 4 of path, then previous chars. */
437 * @param iter The iterator to be initialized. Its current state does not matter.
438 * @param path The full pathname to be iterated over. If NULL, defaults to U_ICUDATA_NAME
439 * @param pkg Package which is being searched for, ex "icudt28l". Will ignore leave directories such as /icudt28l
440 * @param item Item to be searched for. Can include full path, such as /a/b/foo.dat
441 * @param suffix Optional item suffix, if not-null (ex. ".dat") then 'path' can contain 'item' explicitly.
442 * Ex: 'stuff.dat' would be found in '/a/foo:/tmp/stuff.dat:/bar/baz' as item #2.
443 * '/blarg/stuff.dat' would also be found.
445 UDataPathIterator::UDataPathIterator(const char *inPath
, const char *pkg
,
446 const char *item
, const char *inSuffix
, UBool doCheckLastFour
,
447 UErrorCode
*pErrorCode
)
450 fprintf(stderr
, "SUFFIX1=%s PATH=%s\n", inSuffix
, inPath
);
454 path
= u_getDataDirectory();
461 packageStub
.append(U_FILE_SEP_CHAR
, *pErrorCode
).append(pkg
, *pErrorCode
);
463 fprintf(stderr
, "STUB=%s [%d]\n", packageStub
.data(), packageStub
.length());
468 basename
= findBasename(item
);
469 basenameLen
= (int32_t)uprv_strlen(basename
);
472 if(basename
== item
) {
475 itemPath
.append(item
, (int32_t)(basename
-item
), *pErrorCode
);
476 nextPath
= itemPath
.data();
479 fprintf(stderr
, "SUFFIX=%s [%p]\n", inSuffix
, inSuffix
);
483 if(inSuffix
!= NULL
) {
489 checkLastFour
= doCheckLastFour
;
491 /* pathBuffer will hold the output path strings returned by this iterator */
494 fprintf(stderr
, "%p: init %s -> [path=%s], [base=%s], [suff=%s], [itempath=%s], [nextpath=%s], [checklast4=%s]\n",
502 checkLastFour
?"TRUE":"false");
507 * Get the next path on the list.
509 * @param iter The Iter to be used
510 * @param len If set, pointer to the length of the returned path, for convenience.
511 * @return Pointer to the next path segment, or NULL if there are no more.
513 const char *UDataPathIterator::next(UErrorCode
*pErrorCode
)
515 if(U_FAILURE(*pErrorCode
)) {
519 const char *currentPath
= NULL
;
521 const char *pathBasename
;
525 if( nextPath
== NULL
) {
528 currentPath
= nextPath
;
530 if(nextPath
== itemPath
.data()) { /* we were processing item's path. */
531 nextPath
= path
; /* start with regular path next tm. */
532 pathLen
= (int32_t)uprv_strlen(currentPath
);
534 /* fix up next for next time */
535 nextPath
= uprv_strchr(currentPath
, U_PATH_SEP_CHAR
);
536 if(nextPath
== NULL
) {
537 /* segment: entire path */
538 pathLen
= (int32_t)uprv_strlen(currentPath
);
540 /* segment: until next segment */
541 pathLen
= (int32_t)(nextPath
- currentPath
);
552 fprintf(stderr
, "rest of path (IDD) = %s\n", currentPath
);
553 fprintf(stderr
, " ");
556 for(qqq
=0;qqq
<pathLen
;qqq
++)
558 fprintf(stderr
, " ");
561 fprintf(stderr
, "^\n");
564 pathBuffer
.clear().append(currentPath
, pathLen
, *pErrorCode
);
566 /* check for .dat files */
567 pathBasename
= findBasename(pathBuffer
.data());
569 if(checkLastFour
== TRUE
&&
571 uprv_strncmp(pathBuffer
.data() +(pathLen
-4), suffix
, 4)==0 && /* suffix matches */
572 uprv_strncmp(findBasename(pathBuffer
.data()), basename
, basenameLen
)==0 && /* base matches */
573 uprv_strlen(pathBasename
)==(basenameLen
+4)) { /* base+suffix = full len */
576 fprintf(stderr
, "Have %s file on the path: %s\n", suffix
, pathBuffer
.data());
581 { /* regular dir path */
582 if(pathBuffer
[pathLen
-1] != U_FILE_SEP_CHAR
) {
584 uprv_strncmp(pathBuffer
.data()+(pathLen
-4), ".dat", 4) == 0)
587 fprintf(stderr
, "skipping non-directory .dat file %s\n", pathBuffer
.data());
592 /* Check if it is a directory with the same name as our package */
593 if(!packageStub
.isEmpty() &&
594 (pathLen
> packageStub
.length()) &&
595 !uprv_strcmp(pathBuffer
.data() + pathLen
- packageStub
.length(), packageStub
.data())) {
597 fprintf(stderr
, "Found stub %s (will add package %s of len %d)\n", packageStub
.data(), basename
, basenameLen
);
599 pathBuffer
.truncate(pathLen
- packageStub
.length());
601 pathBuffer
.append(U_FILE_SEP_CHAR
, *pErrorCode
);
605 pathBuffer
.append(packageStub
.data()+1, packageStub
.length()-1, *pErrorCode
);
607 if(*suffix
) /* tack on suffix */
609 pathBuffer
.append(suffix
, *pErrorCode
);
614 fprintf(stderr
, " --> %s\n", pathBuffer
.data());
617 return pathBuffer
.data();
621 /* fell way off the end */
627 /* ==================================================================================*/
630 /*----------------------------------------------------------------------*
632 * Add a static reference to the common data library *
633 * Unless overridden by an explicit udata_setCommonData, this will be *
636 *----------------------------------------------------------------------*/
637 extern "C" const DataHeader U_DATA_API U_ICUDATA_ENTRY_POINT
;
640 * This would be a good place for weak-linkage declarations of
641 * partial-data-library access functions where each returns a pointer
642 * to its data package, if it is linked in.
645 extern const void *uprv_getICUData_collation(void) ATTRIBUTE_WEAK;
646 extern const void *uprv_getICUData_conversion(void) ATTRIBUTE_WEAK;
649 /*----------------------------------------------------------------------*
651 * openCommonData Attempt to open a common format (.dat) file *
652 * Map it into memory (if it's not there already) *
653 * and return a UDataMemory object for it. *
655 * If the requested data is already open and cached *
656 * just return the cached UDataMem object. *
658 *----------------------------------------------------------------------*/
660 openCommonData(const char *path
, /* Path from OpenChoice? */
661 int32_t commonDataIndex
, /* ICU Data (index >= 0) if path == NULL */
662 UErrorCode
*pErrorCode
)
665 const char *pathBuffer
;
666 const char *inBasename
;
668 if (U_FAILURE(*pErrorCode
)) {
672 UDataMemory_init(&tData
);
674 /* ??????? TODO revisit this */
675 if (commonDataIndex
>= 0) {
676 /* "mini-cache" for common ICU data */
677 if(commonDataIndex
>= LENGTHOF(gCommonICUDataArray
)) {
680 if(gCommonICUDataArray
[commonDataIndex
] == NULL
) {
682 for(i
= 0; i
< commonDataIndex
; ++i
) {
683 if(gCommonICUDataArray
[i
]->pHeader
== &U_ICUDATA_ENTRY_POINT
) {
684 /* The linked-in data is already in the list. */
689 /* Add the linked-in data to the list. */
691 * This is where we would check and call weakly linked partial-data-library
695 if (uprv_getICUData_collation) {
696 setCommonICUDataPointer(uprv_getICUData_collation(), FALSE, pErrorCode);
698 if (uprv_getICUData_conversion) {
699 setCommonICUDataPointer(uprv_getICUData_conversion(), FALSE, pErrorCode);
702 setCommonICUDataPointer(&U_ICUDATA_ENTRY_POINT
, FALSE
, pErrorCode
);
704 return gCommonICUDataArray
[commonDataIndex
];
708 /* request is NOT for ICU Data. */
710 /* Find the base name portion of the supplied path. */
711 /* inBasename will be left pointing somewhere within the original path string. */
712 inBasename
= findBasename(path
);
714 fprintf(stderr
, "inBasename = %s\n", inBasename
);
718 /* no basename. This will happen if the original path was a directory name, */
719 /* like "a/b/c/". (Fallback to separate files will still work.) */
721 fprintf(stderr
, "ocd: no basename in %s, bailing.\n", path
);
723 *pErrorCode
=U_FILE_ACCESS_ERROR
;
727 /* Is the requested common data file already open and cached? */
728 /* Note that the cache is keyed by the base name only. The rest of the path, */
729 /* if any, is not considered. */
731 UDataMemory
*dataToReturn
= udata_findCachedData(inBasename
);
732 if (dataToReturn
!= NULL
) {
737 /* Requested item is not in the cache.
738 * Hunt it down, trying all the path locations
741 UDataPathIterator
iter(u_getDataDirectory(), inBasename
, path
, ".dat", TRUE
, pErrorCode
);
743 while((UDataMemory_isLoaded(&tData
)==FALSE
) && (pathBuffer
= iter
.next(pErrorCode
)) != NULL
)
746 fprintf(stderr
, "ocd: trying path %s - ", pathBuffer
);
748 uprv_mapFile(&tData
, pathBuffer
);
750 fprintf(stderr
, "%s\n", UDataMemory_isLoaded(&tData
)?"LOADED":"not loaded");
754 #if defined(OS390_STUBDATA) && defined(OS390BATCH)
755 if (!UDataMemory_isLoaded(&tData
)) {
756 char ourPathBuffer
[1024];
757 /* One more chance, for extendCommonData() */
758 uprv_strncpy(ourPathBuffer
, path
, 1019);
759 ourPathBuffer
[1019]=0;
760 uprv_strcat(ourPathBuffer
, ".dat");
761 uprv_mapFile(&tData
, ourPathBuffer
);
765 if (!UDataMemory_isLoaded(&tData
)) {
767 *pErrorCode
=U_FILE_ACCESS_ERROR
;
771 /* we have mapped a file, check its header */
772 udata_checkCommonData(&tData
, pErrorCode
);
775 /* Cache the UDataMemory struct for this .dat file,
776 * so we won't need to hunt it down and map it again next time
777 * something is needed from it. */
778 return udata_cacheDataItem(inBasename
, &tData
, pErrorCode
);
782 /*----------------------------------------------------------------------*
784 * extendICUData If the full set of ICU data was not loaded at *
785 * program startup, load it now. This function will *
786 * be called when the lookup of an ICU data item in *
787 * the common ICU data fails. *
789 * return true if new data is loaded, false otherwise.*
791 *----------------------------------------------------------------------*/
792 static UBool
extendICUData(UErrorCode
*pErr
)
795 UDataMemory copyPData
;
796 UBool didUpdate
= FALSE
;
799 * There is a chance for a race condition here.
800 * Normally, ICU data is loaded from a DLL or via mmap() and
801 * setCommonICUData() will detect if the same address is set twice.
802 * If ICU is built with data loading via fread() then the address will
803 * be different each time the common data is loaded and we may add
804 * multiple copies of the data.
805 * In this case, use a mutex to prevent the race.
806 * Use a specific mutex to avoid nested locks of the global mutex.
808 #if MAP_IMPLEMENTATION==MAP_STDIO
809 static UMutex extendICUDataMutex
= U_MUTEX_INITIALIZER
;
810 umtx_lock(&extendICUDataMutex
);
812 if(!gHaveTriedToLoadCommonData
) {
813 /* See if we can explicitly open a .dat file for the ICUData. */
814 pData
= openCommonData(
815 U_ICUDATA_NAME
, /* "icudt20l" , for example. */
816 -1, /* Pretend we're not opening ICUData */
819 /* How about if there is no pData, eh... */
821 UDataMemory_init(©PData
);
823 UDatamemory_assign(©PData
, pData
);
824 copyPData
.map
= 0; /* The mapping for this data is owned by the hash table */
825 copyPData
.mapAddr
= 0; /* which will unmap it when ICU is shut down. */
826 /* CommonICUData is also unmapped when ICU is shut down.*/
827 /* To avoid unmapping the data twice, zero out the map */
828 /* fields in the UDataMemory that we're assigning */
829 /* to CommonICUData. */
831 didUpdate
= /* no longer using this result */
832 setCommonICUData(©PData
,/* The new common data. */
833 FALSE
, /* No warnings if write didn't happen */
834 pErr
); /* setCommonICUData honors errors; NOP if error set */
837 gHaveTriedToLoadCommonData
= TRUE
;
840 didUpdate
= findCommonICUDataByName(U_ICUDATA_NAME
); /* Return 'true' when a racing writes out the extended */
841 /* data after another thread has failed to see it (in openCommonData), so */
842 /* extended data can be examined. */
843 /* Also handles a race through here before gHaveTriedToLoadCommonData is set. */
845 #if MAP_IMPLEMENTATION==MAP_STDIO
846 umtx_unlock(&extendICUDataMutex
);
848 return didUpdate
; /* Return true if ICUData pointer was updated. */
849 /* (Could potentialy have been done by another thread racing */
850 /* us through here, but that's fine, we still return true */
851 /* so that current thread will also examine extended data. */
854 /*----------------------------------------------------------------------*
856 * udata_setCommonData *
858 *----------------------------------------------------------------------*/
859 U_CAPI
void U_EXPORT2
860 udata_setCommonData(const void *data
, UErrorCode
*pErrorCode
) {
861 UDataMemory dataMemory
;
863 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
868 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
872 /* set the data pointer and test for validity */
873 UDataMemory_init(&dataMemory
);
874 UDataMemory_setData(&dataMemory
, data
);
875 udata_checkCommonData(&dataMemory
, pErrorCode
);
876 if (U_FAILURE(*pErrorCode
)) {return;}
878 /* we have good data */
879 /* Set it up as the ICU Common Data. */
880 setCommonICUData(&dataMemory
, TRUE
, pErrorCode
);
883 /*---------------------------------------------------------------------------
887 *---------------------------------------------------------------------------- */
888 U_CAPI
void U_EXPORT2
889 udata_setAppData(const char *path
, const void *data
, UErrorCode
*err
)
893 if(err
==NULL
|| U_FAILURE(*err
)) {
897 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
901 UDataMemory_init(&udm
);
902 UDataMemory_setData(&udm
, data
);
903 udata_checkCommonData(&udm
, err
);
904 udata_cacheDataItem(path
, &udm
, err
);
907 /*----------------------------------------------------------------------------*
909 * checkDataItem Given a freshly located/loaded data item, either *
910 * an entry in a common file or a separately loaded file, *
911 * sanity check its header, and see if the data is *
912 * acceptable to the app. *
913 * If the data is good, create and return a UDataMemory *
914 * object that can be returned to the application. *
915 * Return NULL on any sort of failure. *
917 *----------------------------------------------------------------------------*/
921 const DataHeader
*pHeader
, /* The data item to be checked. */
922 UDataMemoryIsAcceptable
*isAcceptable
, /* App's call-back function */
923 void *context
, /* pass-thru param for above. */
924 const char *type
, /* pass-thru param for above. */
925 const char *name
, /* pass-thru param for above. */
926 UErrorCode
*nonFatalErr
, /* Error code if this data was not acceptable */
927 /* but openChoice should continue with */
928 /* trying to get data from fallback path. */
929 UErrorCode
*fatalErr
/* Bad error, caller should return immediately */
932 UDataMemory
*rDataMem
= NULL
; /* the new UDataMemory, to be returned. */
934 if (U_FAILURE(*fatalErr
)) {
938 if(pHeader
->dataHeader
.magic1
==0xda &&
939 pHeader
->dataHeader
.magic2
==0x27 &&
940 (isAcceptable
==NULL
|| isAcceptable(context
, type
, name
, &pHeader
->info
))
942 rDataMem
=UDataMemory_createNewInstance(fatalErr
);
943 if (U_FAILURE(*fatalErr
)) {
946 rDataMem
->pHeader
= pHeader
;
948 /* the data is not acceptable, look further */
949 /* If we eventually find something good, this errorcode will be */
951 *nonFatalErr
=U_INVALID_FORMAT_ERROR
;
957 * @return 0 if not loaded, 1 if loaded or err
959 static UDataMemory
*doLoadFromIndividualFiles(const char *pkgName
,
960 const char *dataPath
, const char *tocEntryPathSuffix
,
961 /* following arguments are the same as doOpenChoice itself */
962 const char *path
, const char *type
, const char *name
,
963 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
964 UErrorCode
*subErrorCode
,
965 UErrorCode
*pErrorCode
)
967 const char *pathBuffer
;
968 UDataMemory dataMemory
;
969 UDataMemory
*pEntryData
;
971 /* look in ind. files: package\nam.typ ========================= */
972 /* init path iterator for individual files */
973 UDataPathIterator
iter(dataPath
, pkgName
, path
, tocEntryPathSuffix
, FALSE
, pErrorCode
);
975 while((pathBuffer
= iter
.next(pErrorCode
)))
978 fprintf(stderr
, "UDATA: trying individual file %s\n", pathBuffer
);
980 if(uprv_mapFile(&dataMemory
, pathBuffer
))
982 pEntryData
= checkDataItem(dataMemory
.pHeader
, isAcceptable
, context
, type
, name
, subErrorCode
, pErrorCode
);
983 if (pEntryData
!= NULL
) {
985 * Hand off ownership of the backing memory to the user's UDataMemory.
987 pEntryData
->mapAddr
= dataMemory
.mapAddr
;
988 pEntryData
->map
= dataMemory
.map
;
991 fprintf(stderr
, "** Mapped file: %s\n", pathBuffer
);
996 /* the data is not acceptable, or some error occured. Either way, unmap the memory */
997 udata_close(&dataMemory
);
999 /* If we had a nasty error, bail out completely. */
1000 if (U_FAILURE(*pErrorCode
)) {
1004 /* Otherwise remember that we found data but didn't like it for some reason */
1005 *subErrorCode
=U_INVALID_FORMAT_ERROR
;
1008 fprintf(stderr
, "%s\n", UDataMemory_isLoaded(&dataMemory
)?"LOADED":"not loaded");
1015 * @return 0 if not loaded, 1 if loaded or err
1017 static UDataMemory
*doLoadFromCommonData(UBool isICUData
, const char * /*pkgName*/,
1018 const char * /*dataPath*/, const char * /*tocEntryPathSuffix*/, const char *tocEntryName
,
1019 /* following arguments are the same as doOpenChoice itself */
1020 const char *path
, const char *type
, const char *name
,
1021 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
1022 UErrorCode
*subErrorCode
,
1023 UErrorCode
*pErrorCode
)
1025 UDataMemory
*pEntryData
;
1026 const DataHeader
*pHeader
;
1027 UDataMemory
*pCommonData
;
1028 int32_t commonDataIndex
;
1029 UBool checkedExtendedICUData
= FALSE
;
1030 /* try to get common data. The loop is for platforms such as the 390 that do
1031 * not initially load the full set of ICU data. If the lookup of an ICU data item
1032 * fails, the full (but slower to load) set is loaded, the and the loop repeats,
1033 * trying the lookup again. Once the full set of ICU data is loaded, the loop wont
1034 * repeat because the full set will be checked the first time through.
1036 * The loop also handles the fallback to a .dat file if the application linked
1037 * to the stub data library rather than a real library.
1039 for (commonDataIndex
= isICUData
? 0 : -1;;) {
1040 pCommonData
=openCommonData(path
, commonDataIndex
, subErrorCode
); /** search for pkg **/
1042 if(U_SUCCESS(*subErrorCode
) && pCommonData
!=NULL
) {
1045 /* look up the data piece in the common data */
1046 pHeader
=pCommonData
->vFuncs
->Lookup(pCommonData
, tocEntryName
, &length
, subErrorCode
);
1048 fprintf(stderr
, "%s: pHeader=%p - %s\n", tocEntryName
, pHeader
, u_errorName(*subErrorCode
));
1052 pEntryData
= checkDataItem(pHeader
, isAcceptable
, context
, type
, name
, subErrorCode
, pErrorCode
);
1054 fprintf(stderr
, "pEntryData=%p\n", pEntryData
);
1056 if (U_FAILURE(*pErrorCode
)) {
1059 if (pEntryData
!= NULL
) {
1060 pEntryData
->length
= length
;
1065 /* Data wasn't found. If we were looking for an ICUData item and there is
1066 * more data available, load it and try again,
1067 * otherwise break out of this loop. */
1070 } else if (pCommonData
!= NULL
) {
1071 ++commonDataIndex
; /* try the next data package */
1072 } else if ((!checkedExtendedICUData
) && extendICUData(subErrorCode
)) {
1073 checkedExtendedICUData
= TRUE
;
1074 /* try this data package slot again: it changed from NULL to non-NULL */
1082 * A note on the ownership of Mapped Memory
1084 * For common format files, ownership resides with the UDataMemory object
1085 * that lives in the cache of opened common data. These UDataMemorys are private
1086 * to the udata implementation, and are never seen directly by users.
1088 * The UDataMemory objects returned to users will have the address of some desired
1089 * data within the mapped region, but they wont have the mapping info itself, and thus
1090 * won't cause anything to be removed from memory when they are closed.
1092 * For individual data files, the UDataMemory returned to the user holds the
1093 * information necessary to unmap the data on close. If the user independently
1094 * opens the same data file twice, two completely independent mappings will be made.
1095 * (There is no cache of opened data items from individual files, only a cache of
1096 * opened Common Data files, that is, files containing a collection of data items.)
1098 * For common data passed in from the user via udata_setAppData() or
1099 * udata_setCommonData(), ownership remains with the user.
1101 * UDataMemory objects themselves, as opposed to the memory they describe,
1102 * can be anywhere - heap, stack/local or global.
1103 * They have a flag to indicate when they're heap allocated and thus
1104 * must be deleted when closed.
1108 /*----------------------------------------------------------------------------*
1110 * main data loading functions *
1112 *----------------------------------------------------------------------------*/
1113 static UDataMemory
*
1114 doOpenChoice(const char *path
, const char *type
, const char *name
,
1115 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
1116 UErrorCode
*pErrorCode
)
1118 UDataMemory
*retVal
= NULL
;
1120 const char *dataPath
;
1122 int32_t tocEntrySuffixIndex
;
1123 const char *tocEntryPathSuffix
;
1124 UErrorCode subErrorCode
=U_ZERO_ERROR
;
1125 const char *treeChar
;
1127 UBool isICUData
= FALSE
;
1130 /* Is this path ICU data? */
1132 !strcmp(path
, U_ICUDATA_ALIAS
) || /* "ICUDATA" */
1133 !uprv_strncmp(path
, U_ICUDATA_NAME U_TREE_SEPARATOR_STRING
, /* "icudt26e-" */
1134 uprv_strlen(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING
)) ||
1135 !uprv_strncmp(path
, U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING
, /* "ICUDATA-" */
1136 uprv_strlen(U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING
))) {
1140 #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) /* Windows: try "foo\bar" and "foo/bar" */
1141 /* remap from alternate path char to the main one */
1142 CharString altSepPath
;
1144 if(uprv_strchr(path
,U_FILE_ALT_SEP_CHAR
) != NULL
) {
1145 altSepPath
.append(path
, *pErrorCode
);
1147 while((p
=uprv_strchr(altSepPath
.data(), U_FILE_ALT_SEP_CHAR
))) {
1148 *p
= U_FILE_SEP_CHAR
;
1150 #if defined (UDATA_DEBUG)
1151 fprintf(stderr
, "Changed path from [%s] to [%s]\n", path
, altSepPath
.s
);
1153 path
= altSepPath
.data();
1158 CharString tocEntryName
; /* entry name in tree format. ex: 'icudt28b/coll/ar.res' */
1159 CharString tocEntryPath
; /* entry name in path format. ex: 'icudt28b\\coll\\ar.res' */
1162 CharString treeName
;
1164 /* ======= Set up strings */
1166 pkgName
.append(U_ICUDATA_NAME
, *pErrorCode
);
1170 pkg
= uprv_strrchr(path
, U_FILE_SEP_CHAR
);
1171 first
= uprv_strchr(path
, U_FILE_SEP_CHAR
);
1172 if(uprv_pathIsAbsolute(path
) || (pkg
!= first
)) { /* more than one slash in the path- not a tree name */
1173 /* see if this is an /absolute/path/to/package path */
1175 pkgName
.append(pkg
+1, *pErrorCode
);
1177 pkgName
.append(path
, *pErrorCode
);
1180 treeChar
= uprv_strchr(path
, U_TREE_SEPARATOR
);
1182 treeName
.append(treeChar
+1, *pErrorCode
); /* following '-' */
1184 pkgName
.append(U_ICUDATA_NAME
, *pErrorCode
);
1186 pkgName
.append(path
, (int32_t)(treeChar
-path
), *pErrorCode
);
1187 if (first
== NULL
) {
1189 This user data has no path, but there is a tree name.
1190 Look up the correct path from the data cache later.
1192 path
= pkgName
.data();
1197 pkgName
.append(U_ICUDATA_NAME
, *pErrorCode
);
1199 pkgName
.append(path
, *pErrorCode
);
1206 fprintf(stderr
, " P=%s T=%s\n", pkgName
.data(), treeName
.data());
1209 /* setting up the entry name and file name
1210 * Make up a full name by appending the type to the supplied
1211 * name, assuming that a type was supplied.
1214 /* prepend the package */
1215 tocEntryName
.append(pkgName
, *pErrorCode
);
1216 tocEntryPath
.append(pkgName
, *pErrorCode
);
1217 tocEntrySuffixIndex
= tocEntryName
.length();
1219 if(!treeName
.isEmpty()) {
1220 tocEntryName
.append(U_TREE_ENTRY_SEP_CHAR
, *pErrorCode
).append(treeName
, *pErrorCode
);
1221 tocEntryPath
.append(U_FILE_SEP_CHAR
, *pErrorCode
).append(treeName
, *pErrorCode
);
1224 tocEntryName
.append(U_TREE_ENTRY_SEP_CHAR
, *pErrorCode
).append(name
, *pErrorCode
);
1225 tocEntryPath
.append(U_FILE_SEP_CHAR
, *pErrorCode
).append(name
, *pErrorCode
);
1226 if(type
!=NULL
&& *type
!=0) {
1227 tocEntryName
.append(".", *pErrorCode
).append(type
, *pErrorCode
);
1228 tocEntryPath
.append(".", *pErrorCode
).append(type
, *pErrorCode
);
1230 tocEntryPathSuffix
= tocEntryPath
.data()+tocEntrySuffixIndex
; /* suffix starts here */
1233 fprintf(stderr
, " tocEntryName = %s\n", tocEntryName
.data());
1234 fprintf(stderr
, " tocEntryPath = %s\n", tocEntryName
.data());
1238 path
= COMMON_DATA_NAME
; /* "icudt26e" */
1241 /************************ Begin loop looking for ind. files ***************/
1243 fprintf(stderr
, "IND: inBasename = %s, pkg=%s\n", "(n/a)", packageNameFromPath(path
));
1246 /* End of dealing with a null basename */
1247 dataPath
= u_getDataDirectory();
1249 /**** COMMON PACKAGE - only if packages are first. */
1250 if(gDataFileAccess
== UDATA_PACKAGES_FIRST
) {
1252 fprintf(stderr
, "Trying packages (UDATA_PACKAGES_FIRST)\n");
1255 retVal
= doLoadFromCommonData(isICUData
,
1256 pkgName
.data(), dataPath
, tocEntryPathSuffix
, tocEntryName
.data(),
1257 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1258 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1263 /**** INDIVIDUAL FILES */
1264 if((gDataFileAccess
==UDATA_PACKAGES_FIRST
) ||
1265 (gDataFileAccess
==UDATA_FILES_FIRST
)) {
1267 fprintf(stderr
, "Trying individual files\n");
1269 /* Check to make sure that there is a dataPath to iterate over */
1270 if ((dataPath
&& *dataPath
) || !isICUData
) {
1271 retVal
= doLoadFromIndividualFiles(pkgName
.data(), dataPath
, tocEntryPathSuffix
,
1272 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1273 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1279 /**** COMMON PACKAGE */
1280 if((gDataFileAccess
==UDATA_ONLY_PACKAGES
) ||
1281 (gDataFileAccess
==UDATA_FILES_FIRST
)) {
1283 fprintf(stderr
, "Trying packages (UDATA_ONLY_PACKAGES || UDATA_FILES_FIRST)\n");
1285 retVal
= doLoadFromCommonData(isICUData
,
1286 pkgName
.data(), dataPath
, tocEntryPathSuffix
, tocEntryName
.data(),
1287 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1288 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1293 /* Load from DLL. If we haven't attempted package load, we also haven't had any chance to
1294 try a DLL (static or setCommonData/etc) load.
1295 If we ever have a "UDATA_ONLY_FILES", add it to the or list here. */
1296 if(gDataFileAccess
==UDATA_NO_FILES
) {
1298 fprintf(stderr
, "Trying common data (UDATA_NO_FILES)\n");
1300 retVal
= doLoadFromCommonData(isICUData
,
1301 pkgName
.data(), "", tocEntryPathSuffix
, tocEntryName
.data(),
1302 path
, type
, name
, isAcceptable
, context
, &subErrorCode
, pErrorCode
);
1303 if((retVal
!= NULL
) || U_FAILURE(*pErrorCode
)) {
1308 /* data not found */
1309 if(U_SUCCESS(*pErrorCode
)) {
1310 if(U_SUCCESS(subErrorCode
)) {
1311 /* file not found */
1312 *pErrorCode
=U_FILE_ACCESS_ERROR
;
1314 /* entry point not found or rejected */
1315 *pErrorCode
=subErrorCode
;
1323 /* API ---------------------------------------------------------------------- */
1325 U_CAPI UDataMemory
* U_EXPORT2
1326 udata_open(const char *path
, const char *type
, const char *name
,
1327 UErrorCode
*pErrorCode
) {
1329 fprintf(stderr
, "udata_open(): Opening: %s : %s . %s\n", (path
?path
:"NULL"), name
, type
);
1333 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1335 } else if(name
==NULL
|| *name
==0) {
1336 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1339 return doOpenChoice(path
, type
, name
, NULL
, NULL
, pErrorCode
);
1345 U_CAPI UDataMemory
* U_EXPORT2
1346 udata_openChoice(const char *path
, const char *type
, const char *name
,
1347 UDataMemoryIsAcceptable
*isAcceptable
, void *context
,
1348 UErrorCode
*pErrorCode
) {
1350 fprintf(stderr
, "udata_openChoice(): Opening: %s : %s . %s\n", (path
?path
:"NULL"), name
, type
);
1353 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1355 } else if(name
==NULL
|| *name
==0 || isAcceptable
==NULL
) {
1356 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1359 return doOpenChoice(path
, type
, name
, isAcceptable
, context
, pErrorCode
);
1365 U_CAPI
void U_EXPORT2
1366 udata_getInfo(UDataMemory
*pData
, UDataInfo
*pInfo
) {
1368 if(pData
!=NULL
&& pData
->pHeader
!=NULL
) {
1369 const UDataInfo
*info
=&pData
->pHeader
->info
;
1370 uint16_t dataInfoSize
=udata_getInfoSize(info
);
1371 if(pInfo
->size
>dataInfoSize
) {
1372 pInfo
->size
=dataInfoSize
;
1374 uprv_memcpy((uint16_t *)pInfo
+1, (const uint16_t *)info
+1, pInfo
->size
-2);
1375 if(info
->isBigEndian
!=U_IS_BIG_ENDIAN
) {
1376 /* opposite endianness */
1377 uint16_t x
=info
->reservedWord
;
1378 pInfo
->reservedWord
=(uint16_t)((x
<<8)|(x
>>8));
1387 U_CAPI
void U_EXPORT2
udata_setFileAccess(UDataFileAccess access
, UErrorCode
* /*status*/)
1389 gDataFileAccess
= access
;