// © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /* ****************************************************************************** * * Copyright (C) 1999-2016, International Business Machines * Corporation and others. All Rights Reserved. * ****************************************************************************** * file name: udata.cpp * encoding: UTF-8 * tab size: 8 (not used) * indentation:4 * * created on: 1999oct25 * created by: Markus W. Scherer */ #include "unicode/utypes.h" /* U_PLATFORM etc. */ #ifdef __GNUC__ /* if gcc #define ATTRIBUTE_WEAK __attribute__ ((weak)) might have to #include some other header */ #endif #include "unicode/putil.h" #include "unicode/udata.h" #include "unicode/uversion.h" #include "charstr.h" #include "cmemory.h" #include "cstring.h" #include "mutex.h" #include "putilimp.h" #include "restrace.h" #include "uassert.h" #include "ucln_cmn.h" #include "ucmndata.h" #include "udatamem.h" #include "uhash.h" #include "umapfile.h" #include "umutex.h" /*********************************************************************** * * Notes on the organization of the ICU data implementation * * All of the public API is defined in udata.h * * The implementation is split into several files... * * - udata.c (this file) contains higher level code that knows about * the search paths for locating data, caching opened data, etc. * * - umapfile.c contains the low level platform-specific code for actually loading * (memory mapping, file reading, whatever) data into memory. * * - ucmndata.c deals with the tables of contents of ICU data items within * an ICU common format data file. The implementation includes * an abstract interface and support for multiple TOC formats. * All knowledge of any specific TOC format is encapsulated here. * * - udatamem.c has code for managing UDataMemory structs. These are little * descriptor objects for blocks of memory holding ICU data of * various types. */ /* configuration ---------------------------------------------------------- */ /* If you are excruciatingly bored turn this on .. */ /* #define UDATA_DEBUG 1 */ #if defined(UDATA_DEBUG) # include <stdio.h> #endif U_NAMESPACE_USE /* * Forward declarations */ static UDataMemory *udata_findCachedData(const char *path, UErrorCode &err); /*********************************************************************** * * static (Global) data * ************************************************************************/ /* * Pointers to the common ICU data. * * We store multiple pointers to ICU data packages and iterate through them * when looking for a data item. * * It is possible to combine this with dependency inversion: * One or more data package libraries may export * functions that each return a pointer to their piece of the ICU data, * and this file would import them as weak functions, without a * strong linker dependency from the common library on the data library. * * Then we can have applications depend on only that part of ICU's data * that they really need, reducing the size of binaries that take advantage * of this. */ static UDataMemory *gCommonICUDataArray[10] = …; // Access protected by icu global mutex. static u_atomic_int32_t gHaveTriedToLoadCommonData { … }; // See extendICUData(). static UHashtable *gCommonDataCache = …; /* Global hash table of opened ICU data files. */ static icu::UInitOnce gCommonDataCacheInitOnce { … }; #if !defined(ICU_DATA_DIR_WINDOWS) static UDataFileAccess gDataFileAccess = …; // Access not synchronized. // Modifying is documented as thread-unsafe. #else // If we are using the Windows data directory, then look in one spot only. static UDataFileAccess gDataFileAccess = UDATA_NO_FILES; #endif static UBool U_CALLCONV udata_cleanup() { … } static UBool U_CALLCONV findCommonICUDataByName(const char *inBasename, UErrorCode &err) { … } /* * setCommonICUData. Set a UDataMemory to be the global ICU Data */ static UBool setCommonICUData(UDataMemory *pData, /* The new common data. Belongs to caller, we copy it. */ UBool warn, /* If true, set USING_DEFAULT warning if ICUData was */ /* changed by another thread before we got to it. */ UErrorCode *pErr) { … } #if !defined(ICU_DATA_DIR_WINDOWS) static UBool setCommonICUDataPointer(const void *pData, UBool /*warn*/, UErrorCode *pErrorCode) { … } #endif static const char * findBasename(const char *path) { … } #ifdef UDATA_DEBUG static const char * packageNameFromPath(const char *path) { if((path == nullptr) || (*path == 0)) { return U_ICUDATA_NAME; } path = findBasename(path); if((path == nullptr) || (*path == 0)) { return U_ICUDATA_NAME; } return path; } #endif /*----------------------------------------------------------------------* * * * Cache for common data * * Functions for looking up or adding entries to a cache of * * data that has been previously opened. Avoids a potentially * * expensive operation of re-opening the data for subsequent * * uses. * * * * Data remains cached for the duration of the process. * * * *----------------------------------------------------------------------*/ DataCacheElement; /* * Deleter function for DataCacheElements. * udata cleanup function closes the hash table; hash table in turn calls back to * here for each entry. */ static void U_CALLCONV DataCacheElement_deleter(void *pDCEl) { … } static void U_CALLCONV udata_initHashTable(UErrorCode &err) { … } /* udata_getCacheHashTable() * Get the hash table used to store the data cache entries. * Lazy create it if it doesn't yet exist. */ static UHashtable *udata_getHashTable(UErrorCode &err) { … } static UDataMemory *udata_findCachedData(const char *path, UErrorCode &err) { … } static UDataMemory *udata_cacheDataItem(const char *path, UDataMemory *item, UErrorCode *pErr) { … } /*----------------------------------------------------------------------*============== * * * Path management. Could be shared with other tools/etc if need be * * later on. * * * *----------------------------------------------------------------------*/ U_NAMESPACE_BEGIN class UDataPathIterator { … }; /** * @param iter The iterator to be initialized. Its current state does not matter. * @param inPath The full pathname to be iterated over. If nullptr, defaults to U_ICUDATA_NAME * @param pkg Package which is being searched for, ex "icudt28l". Will ignore leaf directories such as /icudt28l * @param item Item to be searched for. Can include full path, such as /a/b/foo.dat * @param inSuffix Optional item suffix, if not-null (ex. ".dat") then 'path' can contain 'item' explicitly. * Ex: 'stuff.dat' would be found in '/a/foo:/tmp/stuff.dat:/bar/baz' as item #2. * '/blarg/stuff.dat' would also be found. * Note: inSuffix may also be the 'item' being searched for as well, (ex: "ibm-5348_P100-1997.cnv"), in which case * the 'item' parameter is often the same as pkg. (Though sometimes might have a tree part as well, ex: "icudt62l-curr"). */ UDataPathIterator::UDataPathIterator(const char *inPath, const char *pkg, const char *item, const char *inSuffix, UBool doCheckLastFour, UErrorCode *pErrorCode) { … } /** * Get the next path on the list. * * @param iter The Iter to be used * @param len If set, pointer to the length of the returned path, for convenience. * @return Pointer to the next path segment, or nullptr if there are no more. */ const char *UDataPathIterator::next(UErrorCode *pErrorCode) { … } U_NAMESPACE_END /* ==================================================================================*/ /*----------------------------------------------------------------------* * * * Add a static reference to the common data library * * Unless overridden by an explicit udata_setCommonData, this will be * * our common data. * * * *----------------------------------------------------------------------*/ #if !defined(ICU_DATA_DIR_WINDOWS) // When using the Windows system data, we expect only a single data file. extern "C" const DataHeader U_DATA_API U_ICUDATA_ENTRY_POINT; #endif /* * This would be a good place for weak-linkage declarations of * partial-data-library access functions where each returns a pointer * to its data package, if it is linked in. */ /* extern const void *uprv_getICUData_collation() ATTRIBUTE_WEAK; extern const void *uprv_getICUData_conversion() ATTRIBUTE_WEAK; */ /*----------------------------------------------------------------------* * * * openCommonData Attempt to open a common format (.dat) file * * Map it into memory (if it's not there already) * * and return a UDataMemory object for it. * * * * If the requested data is already open and cached * * just return the cached UDataMem object. * * * *----------------------------------------------------------------------*/ static UDataMemory * openCommonData(const char *path, /* Path from OpenChoice? */ int32_t commonDataIndex, /* ICU Data (index >= 0) if path == nullptr */ UErrorCode *pErrorCode) { … } /*----------------------------------------------------------------------* * * * extendICUData If the full set of ICU data was not loaded at * * program startup, load it now. This function will * * be called when the lookup of an ICU data item in * * the common ICU data fails. * * * * return true if new data is loaded, false otherwise.* * * *----------------------------------------------------------------------*/ static UBool extendICUData(UErrorCode *pErr) { … } /*----------------------------------------------------------------------* * * * udata_setCommonData * * * *----------------------------------------------------------------------*/ U_CAPI void U_EXPORT2 udata_setCommonData(const void *data, UErrorCode *pErrorCode) { … } /*--------------------------------------------------------------------------- * * udata_setAppData * *---------------------------------------------------------------------------- */ U_CAPI void U_EXPORT2 udata_setAppData(const char *path, const void *data, UErrorCode *err) { … } /*----------------------------------------------------------------------------* * * * checkDataItem Given a freshly located/loaded data item, either * * an entry in a common file or a separately loaded file, * * sanity check its header, and see if the data is * * acceptable to the app. * * If the data is good, create and return a UDataMemory * * object that can be returned to the application. * * Return nullptr on any sort of failure. * * * *----------------------------------------------------------------------------*/ static UDataMemory * checkDataItem ( const DataHeader *pHeader, /* The data item to be checked. */ UDataMemoryIsAcceptable *isAcceptable, /* App's call-back function */ void *context, /* pass-thru param for above. */ const char *type, /* pass-thru param for above. */ const char *name, /* pass-thru param for above. */ UErrorCode *nonFatalErr, /* Error code if this data was not acceptable */ /* but openChoice should continue with */ /* trying to get data from fallback path. */ UErrorCode *fatalErr /* Bad error, caller should return immediately */ ) { … } /** * @return 0 if not loaded, 1 if loaded or err */ static UDataMemory *doLoadFromIndividualFiles(const char *pkgName, const char *dataPath, const char *tocEntryPathSuffix, /* following arguments are the same as doOpenChoice itself */ const char *path, const char *type, const char *name, UDataMemoryIsAcceptable *isAcceptable, void *context, UErrorCode *subErrorCode, UErrorCode *pErrorCode) { … } /** * @return 0 if not loaded, 1 if loaded or err */ static UDataMemory *doLoadFromCommonData(UBool isICUData, const char * /*pkgName*/, const char * /*dataPath*/, const char * /*tocEntryPathSuffix*/, const char *tocEntryName, /* following arguments are the same as doOpenChoice itself */ const char *path, const char *type, const char *name, UDataMemoryIsAcceptable *isAcceptable, void *context, UErrorCode *subErrorCode, UErrorCode *pErrorCode) { … } /* * Identify the Time Zone resources that are subject to special override data loading. */ static UBool isTimeZoneFile(const char *name, const char *type) { … } /* * A note on the ownership of Mapped Memory * * For common format files, ownership resides with the UDataMemory object * that lives in the cache of opened common data. These UDataMemorys are private * to the udata implementation, and are never seen directly by users. * * The UDataMemory objects returned to users will have the address of some desired * data within the mapped region, but they wont have the mapping info itself, and thus * won't cause anything to be removed from memory when they are closed. * * For individual data files, the UDataMemory returned to the user holds the * information necessary to unmap the data on close. If the user independently * opens the same data file twice, two completely independent mappings will be made. * (There is no cache of opened data items from individual files, only a cache of * opened Common Data files, that is, files containing a collection of data items.) * * For common data passed in from the user via udata_setAppData() or * udata_setCommonData(), ownership remains with the user. * * UDataMemory objects themselves, as opposed to the memory they describe, * can be anywhere - heap, stack/local or global. * They have a flag to indicate when they're heap allocated and thus * must be deleted when closed. */ /*----------------------------------------------------------------------------* * * * main data loading functions * * * *----------------------------------------------------------------------------*/ static UDataMemory * doOpenChoice(const char *path, const char *type, const char *name, UDataMemoryIsAcceptable *isAcceptable, void *context, UErrorCode *pErrorCode) { … } /* API ---------------------------------------------------------------------- */ U_CAPI UDataMemory * U_EXPORT2 udata_open(const char *path, const char *type, const char *name, UErrorCode *pErrorCode) { … } U_CAPI UDataMemory * U_EXPORT2 udata_openChoice(const char *path, const char *type, const char *name, UDataMemoryIsAcceptable *isAcceptable, void *context, UErrorCode *pErrorCode) { … } U_CAPI void U_EXPORT2 udata_getInfo(UDataMemory *pData, UDataInfo *pInfo) { … } U_CAPI void U_EXPORT2 udata_setFileAccess(UDataFileAccess access, UErrorCode * /*status*/) { … }