Home | History | Annotate | Download | only in common
      1 /*
      2 ******************************************************************************
      3 *
      4 *   Copyright (C) 1999-2015, International Business Machines
      5 *   Corporation and others.  All Rights Reserved.
      6 *
      7 ******************************************************************************
      8 *   file name:  udata.cpp
      9 *   encoding:   US-ASCII
     10 *   tab size:   8 (not used)
     11 *   indentation:4
     12 *
     13 *   created on: 1999oct25
     14 *   created by: Markus W. Scherer
     15 */
     16 
     17 #include "unicode/utypes.h"  /* U_PLATFORM etc. */
     18 
     19 #ifdef __GNUC__
     20 /* if gcc
     21 #define ATTRIBUTE_WEAK __attribute__ ((weak))
     22 might have to #include some other header
     23 */
     24 #endif
     25 
     26 #include "unicode/putil.h"
     27 #include "unicode/udata.h"
     28 #include "unicode/uversion.h"
     29 #include "charstr.h"
     30 #include "cmemory.h"
     31 #include "cstring.h"
     32 #include "mutex.h"
     33 #include "putilimp.h"
     34 #include "uassert.h"
     35 #include "ucln_cmn.h"
     36 #include "ucmndata.h"
     37 #include "udatamem.h"
     38 #include "uhash.h"
     39 #include "umapfile.h"
     40 #include "umutex.h"
     41 
     42 /***********************************************************************
     43 *
     44 *   Notes on the organization of the ICU data implementation
     45 *
     46 *      All of the public API is defined in udata.h
     47 *
     48 *      The implementation is split into several files...
     49 *
     50 *         - udata.c  (this file) contains higher level code that knows about
     51 *                     the search paths for locating data, caching opened data, etc.
     52 *
     53 *         - umapfile.c  contains the low level platform-specific code for actually loading
     54 *                     (memory mapping, file reading, whatever) data into memory.
     55 *
     56 *         - ucmndata.c  deals with the tables of contents of ICU data items within
     57 *                     an ICU common format data file.  The implementation includes
     58 *                     an abstract interface and support for multiple TOC formats.
     59 *                     All knowledge of any specific TOC format is encapsulated here.
     60 *
     61 *         - udatamem.c has code for managing UDataMemory structs.  These are little
     62 *                     descriptor objects for blocks of memory holding ICU data of
     63 *                     various types.
     64 */
     65 
     66 /* configuration ---------------------------------------------------------- */
     67 
     68 /* If you are excruciatingly bored turn this on .. */
     69 /* #define UDATA_DEBUG 1 */
     70 
     71 #if defined(UDATA_DEBUG)
     72 #   include <stdio.h>
     73 #endif
     74 
     75 U_NAMESPACE_USE
     76 
     77 /*
     78  *  Forward declarations
     79  */
     80 static UDataMemory *udata_findCachedData(const char *path);
     81 
     82 /***********************************************************************
     83 *
     84 *    static (Global) data
     85 *
     86 ************************************************************************/
     87 
     88 /*
     89  * Pointers to the common ICU data.
     90  *
     91  * We store multiple pointers to ICU data packages and iterate through them
     92  * when looking for a data item.
     93  *
     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.
     99  *
    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
    102  * of this.
    103  */
    104 static UDataMemory *gCommonICUDataArray[10] = { NULL };   // Access protected by icu global mutex.
    105 
    106 static u_atomic_int32_t gHaveTriedToLoadCommonData = ATOMIC_INT32_T_INITIALIZER(0);  //  See extendICUData().
    107 
    108 static UHashtable  *gCommonDataCache = NULL;  /* Global hash table of opened ICU data files.  */
    109 static icu::UInitOnce gCommonDataCacheInitOnce = U_INITONCE_INITIALIZER;
    110 
    111 static UDataFileAccess  gDataFileAccess = UDATA_DEFAULT_ACCESS;  // Access not synchronized.
    112                                                                  // Modifying is documented as thread-unsafe.
    113 
    114 static UBool U_CALLCONV
    115 udata_cleanup(void)
    116 {
    117     int32_t i;
    118 
    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.                */
    122     }
    123     gCommonDataCacheInitOnce.reset();
    124 
    125     for (i = 0; i < UPRV_LENGTHOF(gCommonICUDataArray) && gCommonICUDataArray[i] != NULL; ++i) {
    126         udata_close(gCommonICUDataArray[i]);
    127         gCommonICUDataArray[i] = NULL;
    128     }
    129     gHaveTriedToLoadCommonData = 0;
    130 
    131     return TRUE;                   /* Everything was cleaned up */
    132 }
    133 
    134 static UBool U_CALLCONV
    135 findCommonICUDataByName(const char *inBasename)
    136 {
    137     UBool found = FALSE;
    138     int32_t i;
    139 
    140     UDataMemory  *pData = udata_findCachedData(inBasename);
    141     if (pData == NULL)
    142         return FALSE;
    143 
    144     {
    145         Mutex lock;
    146         for (i = 0; i < UPRV_LENGTHOF(gCommonICUDataArray); ++i) {
    147             if ((gCommonICUDataArray[i] != NULL) && (gCommonICUDataArray[i]->pHeader == pData->pHeader)) {
    148                 /* The data pointer is already in the array. */
    149                 found = TRUE;
    150                 break;
    151             }
    152         }
    153     }
    154     return found;
    155 }
    156 
    157 
    158 /*
    159  * setCommonICUData.   Set a UDataMemory to be the global ICU Data
    160  */
    161 static UBool
    162 setCommonICUData(UDataMemory *pData,     /*  The new common data.  Belongs to caller, we copy it. */
    163                  UBool       warn,       /*  If true, set USING_DEFAULT warning if ICUData was    */
    164                                          /*    changed by another thread before we got to it.     */
    165                  UErrorCode *pErr)
    166 {
    167     UDataMemory  *newCommonData = UDataMemory_createNewInstance(pErr);
    168     int32_t i;
    169     UBool didUpdate = FALSE;
    170     if (U_FAILURE(*pErr)) {
    171         return FALSE;
    172     }
    173 
    174     /*  For the assignment, other threads must cleanly see either the old            */
    175     /*    or the new, not some partially initialized new.  The old can not be        */
    176     /*    deleted - someone may still have a pointer to it lying around in           */
    177     /*    their locals.                                                              */
    178     UDatamemory_assign(newCommonData, pData);
    179     umtx_lock(NULL);
    180     for (i = 0; i < UPRV_LENGTHOF(gCommonICUDataArray); ++i) {
    181         if (gCommonICUDataArray[i] == NULL) {
    182             gCommonICUDataArray[i] = newCommonData;
    183             didUpdate = TRUE;
    184             break;
    185         } else if (gCommonICUDataArray[i]->pHeader == pData->pHeader) {
    186             /* The same data pointer is already in the array. */
    187             break;
    188         }
    189     }
    190     umtx_unlock(NULL);
    191 
    192     if (i == UPRV_LENGTHOF(gCommonICUDataArray) && warn) {
    193         *pErr = U_USING_DEFAULT_WARNING;
    194     }
    195     if (didUpdate) {
    196         ucln_common_registerCleanup(UCLN_COMMON_UDATA, udata_cleanup);
    197     } else {
    198         uprv_free(newCommonData);
    199     }
    200     return didUpdate;
    201 }
    202 
    203 static UBool
    204 setCommonICUDataPointer(const void *pData, UBool /*warn*/, UErrorCode *pErrorCode) {
    205     UDataMemory tData;
    206     UDataMemory_init(&tData);
    207     UDataMemory_setData(&tData, pData);
    208     udata_checkCommonData(&tData, pErrorCode);
    209     return setCommonICUData(&tData, FALSE, pErrorCode);
    210 }
    211 
    212 static const char *
    213 findBasename(const char *path) {
    214     const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR);
    215     if(basename==NULL) {
    216         return path;
    217     } else {
    218         return basename+1;
    219     }
    220 }
    221 
    222 #ifdef UDATA_DEBUG
    223 static const char *
    224 packageNameFromPath(const char *path)
    225 {
    226     if((path == NULL) || (*path == 0)) {
    227         return U_ICUDATA_NAME;
    228     }
    229 
    230     path = findBasename(path);
    231 
    232     if((path == NULL) || (*path == 0)) {
    233         return U_ICUDATA_NAME;
    234     }
    235 
    236     return path;
    237 }
    238 #endif
    239 
    240 /*----------------------------------------------------------------------*
    241  *                                                                      *
    242  *   Cache for common data                                              *
    243  *      Functions for looking up or adding entries to a cache of        *
    244  *      data that has been previously opened.  Avoids a potentially     *
    245  *      expensive operation of re-opening the data for subsequent       *
    246  *      uses.                                                           *
    247  *                                                                      *
    248  *      Data remains cached for the duration of the process.            *
    249  *                                                                      *
    250  *----------------------------------------------------------------------*/
    251 
    252 typedef struct DataCacheElement {
    253     char          *name;
    254     UDataMemory   *item;
    255 } DataCacheElement;
    256 
    257 
    258 
    259 /*
    260  * Deleter function for DataCacheElements.
    261  *         udata cleanup function closes the hash table; hash table in turn calls back to
    262  *         here for each entry.
    263  */
    264 static void U_CALLCONV DataCacheElement_deleter(void *pDCEl) {
    265     DataCacheElement *p = (DataCacheElement *)pDCEl;
    266     udata_close(p->item);              /* unmaps storage */
    267     uprv_free(p->name);                /* delete the hash key string. */
    268     uprv_free(pDCEl);                  /* delete 'this'          */
    269 }
    270 
    271 static void udata_initHashTable() {
    272     UErrorCode err = U_ZERO_ERROR;
    273     U_ASSERT(gCommonDataCache == NULL);
    274     gCommonDataCache = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &err);
    275     if (U_FAILURE(err)) {
    276         // TODO: handle errors better.
    277         gCommonDataCache = NULL;
    278     }
    279     if (gCommonDataCache != NULL) {
    280         uhash_setValueDeleter(gCommonDataCache, DataCacheElement_deleter);
    281         ucln_common_registerCleanup(UCLN_COMMON_UDATA, udata_cleanup);
    282     }
    283 }
    284 
    285  /*   udata_getCacheHashTable()
    286   *     Get the hash table used to store the data cache entries.
    287   *     Lazy create it if it doesn't yet exist.
    288   */
    289 static UHashtable *udata_getHashTable() {
    290     umtx_initOnce(gCommonDataCacheInitOnce, &udata_initHashTable);
    291     return gCommonDataCache;
    292 }
    293 
    294 
    295 
    296 static UDataMemory *udata_findCachedData(const char *path)
    297 {
    298     UHashtable        *htable;
    299     UDataMemory       *retVal = NULL;
    300     DataCacheElement  *el;
    301     const char        *baseName;
    302 
    303     baseName = findBasename(path);   /* Cache remembers only the base name, not the full path. */
    304     htable = udata_getHashTable();
    305     umtx_lock(NULL);
    306     el = (DataCacheElement *)uhash_get(htable, baseName);
    307     umtx_unlock(NULL);
    308     if (el != NULL) {
    309         retVal = el->item;
    310     }
    311 #ifdef UDATA_DEBUG
    312     fprintf(stderr, "Cache: [%s] -> %p\n", baseName, retVal);
    313 #endif
    314     return retVal;
    315 }
    316 
    317 
    318 static UDataMemory *udata_cacheDataItem(const char *path, UDataMemory *item, UErrorCode *pErr) {
    319     DataCacheElement *newElement;
    320     const char       *baseName;
    321     int32_t           nameLen;
    322     UHashtable       *htable;
    323     DataCacheElement *oldValue = NULL;
    324     UErrorCode        subErr = U_ZERO_ERROR;
    325 
    326     if (U_FAILURE(*pErr)) {
    327         return NULL;
    328     }
    329 
    330     /* Create a new DataCacheElement - the thingy we store in the hash table -
    331      * and copy the supplied path and UDataMemoryItems into it.
    332      */
    333     newElement = (DataCacheElement *)uprv_malloc(sizeof(DataCacheElement));
    334     if (newElement == NULL) {
    335         *pErr = U_MEMORY_ALLOCATION_ERROR;
    336         return NULL;
    337     }
    338     newElement->item = UDataMemory_createNewInstance(pErr);
    339     if (U_FAILURE(*pErr)) {
    340         uprv_free(newElement);
    341         return NULL;
    342     }
    343     UDatamemory_assign(newElement->item, item);
    344 
    345     baseName = findBasename(path);
    346     nameLen = (int32_t)uprv_strlen(baseName);
    347     newElement->name = (char *)uprv_malloc(nameLen+1);
    348     if (newElement->name == NULL) {
    349         *pErr = U_MEMORY_ALLOCATION_ERROR;
    350         uprv_free(newElement->item);
    351         uprv_free(newElement);
    352         return NULL;
    353     }
    354     uprv_strcpy(newElement->name, baseName);
    355 
    356     /* Stick the new DataCacheElement into the hash table.
    357     */
    358     htable = udata_getHashTable();
    359     umtx_lock(NULL);
    360     oldValue = (DataCacheElement *)uhash_get(htable, path);
    361     if (oldValue != NULL) {
    362         subErr = U_USING_DEFAULT_WARNING;
    363     }
    364     else {
    365         uhash_put(
    366             htable,
    367             newElement->name,               /* Key   */
    368             newElement,                     /* Value */
    369             &subErr);
    370     }
    371     umtx_unlock(NULL);
    372 
    373 #ifdef UDATA_DEBUG
    374     fprintf(stderr, "Cache: [%s] <<< %p : %s. vFunc=%p\n", newElement->name,
    375     newElement->item, u_errorName(subErr), newElement->item->vFuncs);
    376 #endif
    377 
    378     if (subErr == U_USING_DEFAULT_WARNING || U_FAILURE(subErr)) {
    379         *pErr = subErr; /* copy sub err unto fillin ONLY if something happens. */
    380         uprv_free(newElement->name);
    381         uprv_free(newElement->item);
    382         uprv_free(newElement);
    383         return oldValue ? oldValue->item : NULL;
    384     }
    385 
    386     return newElement->item;
    387 }
    388 
    389 /*----------------------------------------------------------------------*==============
    390  *                                                                      *
    391  *  Path management.  Could be shared with other tools/etc if need be   *
    392  * later on.                                                            *
    393  *                                                                      *
    394  *----------------------------------------------------------------------*/
    395 
    396 #define U_DATA_PATHITER_BUFSIZ  128        /* Size of local buffer for paths         */
    397                                            /*   Overflow causes malloc of larger buf */
    398 
    399 U_NAMESPACE_BEGIN
    400 
    401 class UDataPathIterator
    402 {
    403 public:
    404     UDataPathIterator(const char *path, const char *pkg,
    405                       const char *item, const char *suffix, UBool doCheckLastFour,
    406                       UErrorCode *pErrorCode);
    407     const char *next(UErrorCode *pErrorCode);
    408 
    409 private:
    410     const char *path;                              /* working path (u_icudata_Dir) */
    411     const char *nextPath;                          /* path following this one */
    412     const char *basename;                          /* item's basename (icudt22e_mt.res)*/
    413     const char *suffix;                            /* item suffix (can be null) */
    414 
    415     uint32_t    basenameLen;                       /* length of basename */
    416 
    417     CharString  itemPath;                          /* path passed in with item name */
    418     CharString  pathBuffer;                        /* output path for this it'ion */
    419     CharString  packageStub;                       /* example:  "/icudt28b". Will ignore that leaf in set paths. */
    420 
    421     UBool       checkLastFour;                     /* if TRUE then allow paths such as '/foo/myapp.dat'
    422                                                     * to match, checks last 4 chars of suffix with
    423                                                     * last 4 of path, then previous chars. */
    424 };
    425 
    426 /**
    427  * @param iter  The iterator to be initialized. Its current state does not matter.
    428  * @param path  The full pathname to be iterated over.  If NULL, defaults to U_ICUDATA_NAME
    429  * @param pkg   Package which is being searched for, ex "icudt28l".  Will ignore leave directories such as /icudt28l
    430  * @param item  Item to be searched for.  Can include full path, such as /a/b/foo.dat
    431  * @param suffix  Optional item suffix, if not-null (ex. ".dat") then 'path' can contain 'item' explicitly.
    432  *               Ex:   'stuff.dat' would be found in '/a/foo:/tmp/stuff.dat:/bar/baz' as item #2.
    433  *                     '/blarg/stuff.dat' would also be found.
    434  */
    435 UDataPathIterator::UDataPathIterator(const char *inPath, const char *pkg,
    436                                      const char *item, const char *inSuffix, UBool doCheckLastFour,
    437                                      UErrorCode *pErrorCode)
    438 {
    439 #ifdef UDATA_DEBUG
    440         fprintf(stderr, "SUFFIX1=%s PATH=%s\n", inSuffix, inPath);
    441 #endif
    442     /** Path **/
    443     if(inPath == NULL) {
    444         path = u_getDataDirectory();
    445     } else {
    446         path = inPath;
    447     }
    448 
    449     /** Package **/
    450     if(pkg != NULL) {
    451       packageStub.append(U_FILE_SEP_CHAR, *pErrorCode).append(pkg, *pErrorCode);
    452 #ifdef UDATA_DEBUG
    453       fprintf(stderr, "STUB=%s [%d]\n", packageStub.data(), packageStub.length());
    454 #endif
    455     }
    456 
    457     /** Item **/
    458     basename = findBasename(item);
    459     basenameLen = (int32_t)uprv_strlen(basename);
    460 
    461     /** Item path **/
    462     if(basename == item) {
    463         nextPath = path;
    464     } else {
    465         itemPath.append(item, (int32_t)(basename-item), *pErrorCode);
    466         nextPath = itemPath.data();
    467     }
    468 #ifdef UDATA_DEBUG
    469     fprintf(stderr, "SUFFIX=%s [%p]\n", inSuffix, inSuffix);
    470 #endif
    471 
    472     /** Suffix  **/
    473     if(inSuffix != NULL) {
    474         suffix = inSuffix;
    475     } else {
    476         suffix = "";
    477     }
    478 
    479     checkLastFour = doCheckLastFour;
    480 
    481     /* pathBuffer will hold the output path strings returned by this iterator */
    482 
    483 #ifdef UDATA_DEBUG
    484     fprintf(stderr, "%p: init %s -> [path=%s], [base=%s], [suff=%s], [itempath=%s], [nextpath=%s], [checklast4=%s]\n",
    485             iter,
    486             item,
    487             path,
    488             basename,
    489             suffix,
    490             itemPath.data(),
    491             nextPath,
    492             checkLastFour?"TRUE":"false");
    493 #endif
    494 }
    495 
    496 /**
    497  * Get the next path on the list.
    498  *
    499  * @param iter The Iter to be used
    500  * @param len  If set, pointer to the length of the returned path, for convenience.
    501  * @return Pointer to the next path segment, or NULL if there are no more.
    502  */
    503 const char *UDataPathIterator::next(UErrorCode *pErrorCode)
    504 {
    505     if(U_FAILURE(*pErrorCode)) {
    506         return NULL;
    507     }
    508 
    509     const char *currentPath = NULL;
    510     int32_t     pathLen = 0;
    511     const char *pathBasename;
    512 
    513     do
    514     {
    515         if( nextPath == NULL ) {
    516             break;
    517         }
    518         currentPath = nextPath;
    519 
    520         if(nextPath == itemPath.data()) { /* we were processing item's path. */
    521             nextPath = path; /* start with regular path next tm. */
    522             pathLen = (int32_t)uprv_strlen(currentPath);
    523         } else {
    524             /* fix up next for next time */
    525             nextPath = uprv_strchr(currentPath, U_PATH_SEP_CHAR);
    526             if(nextPath == NULL) {
    527                 /* segment: entire path */
    528                 pathLen = (int32_t)uprv_strlen(currentPath);
    529             } else {
    530                 /* segment: until next segment */
    531                 pathLen = (int32_t)(nextPath - currentPath);
    532                 /* skip divider */
    533                 nextPath ++;
    534             }
    535         }
    536 
    537         if(pathLen == 0) {
    538             continue;
    539         }
    540 
    541 #ifdef UDATA_DEBUG
    542         fprintf(stderr, "rest of path (IDD) = %s\n", currentPath);
    543         fprintf(stderr, "                     ");
    544         {
    545             uint32_t qqq;
    546             for(qqq=0;qqq<pathLen;qqq++)
    547             {
    548                 fprintf(stderr, " ");
    549             }
    550 
    551             fprintf(stderr, "^\n");
    552         }
    553 #endif
    554         pathBuffer.clear().append(currentPath, pathLen, *pErrorCode);
    555 
    556         /* check for .dat files */
    557         pathBasename = findBasename(pathBuffer.data());
    558 
    559         if(checkLastFour == TRUE &&
    560            (pathLen>=4) &&
    561            uprv_strncmp(pathBuffer.data() +(pathLen-4), suffix, 4)==0 && /* suffix matches */
    562            uprv_strncmp(findBasename(pathBuffer.data()), basename, basenameLen)==0  && /* base matches */
    563            uprv_strlen(pathBasename)==(basenameLen+4)) { /* base+suffix = full len */
    564 
    565 #ifdef UDATA_DEBUG
    566             fprintf(stderr, "Have %s file on the path: %s\n", suffix, pathBuffer.data());
    567 #endif
    568             /* do nothing */
    569         }
    570         else
    571         {       /* regular dir path */
    572             if(pathBuffer[pathLen-1] != U_FILE_SEP_CHAR) {
    573                 if((pathLen>=4) &&
    574                    uprv_strncmp(pathBuffer.data()+(pathLen-4), ".dat", 4) == 0)
    575                 {
    576 #ifdef UDATA_DEBUG
    577                     fprintf(stderr, "skipping non-directory .dat file %s\n", pathBuffer.data());
    578 #endif
    579                     continue;
    580                 }
    581 
    582                 /* Check if it is a directory with the same name as our package */
    583                 if(!packageStub.isEmpty() &&
    584                    (pathLen > packageStub.length()) &&
    585                    !uprv_strcmp(pathBuffer.data() + pathLen - packageStub.length(), packageStub.data())) {
    586 #ifdef UDATA_DEBUG
    587                   fprintf(stderr, "Found stub %s (will add package %s of len %d)\n", packageStub.data(), basename, basenameLen);
    588 #endif
    589                   pathBuffer.truncate(pathLen - packageStub.length());
    590                 }
    591                 pathBuffer.append(U_FILE_SEP_CHAR, *pErrorCode);
    592             }
    593 
    594             /* + basename */
    595             pathBuffer.append(packageStub.data()+1, packageStub.length()-1, *pErrorCode);
    596 
    597             if(*suffix)  /* tack on suffix */
    598             {
    599                 pathBuffer.append(suffix, *pErrorCode);
    600             }
    601         }
    602 
    603 #ifdef UDATA_DEBUG
    604         fprintf(stderr, " -->  %s\n", pathBuffer.data());
    605 #endif
    606 
    607         return pathBuffer.data();
    608 
    609     } while(path);
    610 
    611     /* fell way off the end */
    612     return NULL;
    613 }
    614 
    615 U_NAMESPACE_END
    616 
    617 /* ==================================================================================*/
    618 
    619 
    620 /*----------------------------------------------------------------------*
    621  *                                                                      *
    622  *  Add a static reference to the common data  library                  *
    623  *   Unless overridden by an explicit udata_setCommonData, this will be *
    624  *      our common data.                                                *
    625  *                                                                      *
    626  *----------------------------------------------------------------------*/
    627 extern "C" const DataHeader U_DATA_API U_ICUDATA_ENTRY_POINT;
    628 
    629 /*
    630  * This would be a good place for weak-linkage declarations of
    631  * partial-data-library access functions where each returns a pointer
    632  * to its data package, if it is linked in.
    633  */
    634 /*
    635 extern const void *uprv_getICUData_collation(void) ATTRIBUTE_WEAK;
    636 extern const void *uprv_getICUData_conversion(void) ATTRIBUTE_WEAK;
    637 */
    638 
    639 /*----------------------------------------------------------------------*
    640  *                                                                      *
    641  *   openCommonData   Attempt to open a common format (.dat) file       *
    642  *                    Map it into memory (if it's not there already)    *
    643  *                    and return a UDataMemory object for it.           *
    644  *                                                                      *
    645  *                    If the requested data is already open and cached  *
    646  *                       just return the cached UDataMem object.        *
    647  *                                                                      *
    648  *----------------------------------------------------------------------*/
    649 static UDataMemory *
    650 openCommonData(const char *path,          /*  Path from OpenChoice?          */
    651                int32_t commonDataIndex,   /*  ICU Data (index >= 0) if path == NULL */
    652                UErrorCode *pErrorCode)
    653 {
    654     UDataMemory tData;
    655     const char *pathBuffer;
    656     const char *inBasename;
    657 
    658     if (U_FAILURE(*pErrorCode)) {
    659         return NULL;
    660     }
    661 
    662     UDataMemory_init(&tData);
    663 
    664     /* ??????? TODO revisit this */
    665     if (commonDataIndex >= 0) {
    666         /* "mini-cache" for common ICU data */
    667         if(commonDataIndex >= UPRV_LENGTHOF(gCommonICUDataArray)) {
    668             return NULL;
    669         }
    670         {
    671             Mutex lock;
    672             if(gCommonICUDataArray[commonDataIndex] != NULL) {
    673                 return gCommonICUDataArray[commonDataIndex];
    674             }
    675             int32_t i;
    676             for(i = 0; i < commonDataIndex; ++i) {
    677                 if(gCommonICUDataArray[i]->pHeader == &U_ICUDATA_ENTRY_POINT) {
    678                     /* The linked-in data is already in the list. */
    679                     return NULL;
    680                 }
    681             }
    682         }
    683 
    684         /* Add the linked-in data to the list. */
    685         /*
    686          * This is where we would check and call weakly linked partial-data-library
    687          * access functions.
    688          */
    689         /*
    690         if (uprv_getICUData_collation) {
    691             setCommonICUDataPointer(uprv_getICUData_collation(), FALSE, pErrorCode);
    692         }
    693         if (uprv_getICUData_conversion) {
    694             setCommonICUDataPointer(uprv_getICUData_conversion(), FALSE, pErrorCode);
    695         }
    696         */
    697         setCommonICUDataPointer(&U_ICUDATA_ENTRY_POINT, FALSE, pErrorCode);
    698         {
    699             Mutex lock;
    700             return gCommonICUDataArray[commonDataIndex];
    701         }
    702     }
    703 
    704 
    705     /* request is NOT for ICU Data.  */
    706 
    707     /* Find the base name portion of the supplied path.   */
    708     /*   inBasename will be left pointing somewhere within the original path string.      */
    709     inBasename = findBasename(path);
    710 #ifdef UDATA_DEBUG
    711     fprintf(stderr, "inBasename = %s\n", inBasename);
    712 #endif
    713 
    714     if(*inBasename==0) {
    715         /* no basename.     This will happen if the original path was a directory name,   */
    716         /*    like  "a/b/c/".   (Fallback to separate files will still work.)             */
    717 #ifdef UDATA_DEBUG
    718         fprintf(stderr, "ocd: no basename in %s, bailing.\n", path);
    719 #endif
    720         *pErrorCode=U_FILE_ACCESS_ERROR;
    721         return NULL;
    722     }
    723 
    724    /* Is the requested common data file already open and cached?                     */
    725    /*   Note that the cache is keyed by the base name only.  The rest of the path,   */
    726    /*     if any, is not considered.                                                 */
    727    {
    728         UDataMemory  *dataToReturn = udata_findCachedData(inBasename);
    729         if (dataToReturn != NULL) {
    730             return dataToReturn;
    731         }
    732     }
    733 
    734     /* Requested item is not in the cache.
    735      * Hunt it down, trying all the path locations
    736      */
    737 
    738     UDataPathIterator iter(u_getDataDirectory(), inBasename, path, ".dat", TRUE, pErrorCode);
    739 
    740     while((UDataMemory_isLoaded(&tData)==FALSE) && (pathBuffer = iter.next(pErrorCode)) != NULL)
    741     {
    742 #ifdef UDATA_DEBUG
    743         fprintf(stderr, "ocd: trying path %s - ", pathBuffer);
    744 #endif
    745         uprv_mapFile(&tData, pathBuffer);
    746 #ifdef UDATA_DEBUG
    747         fprintf(stderr, "%s\n", UDataMemory_isLoaded(&tData)?"LOADED":"not loaded");
    748 #endif
    749     }
    750 
    751 #if defined(OS390_STUBDATA) && defined(OS390BATCH)
    752     if (!UDataMemory_isLoaded(&tData)) {
    753         char ourPathBuffer[1024];
    754         /* One more chance, for extendCommonData() */
    755         uprv_strncpy(ourPathBuffer, path, 1019);
    756         ourPathBuffer[1019]=0;
    757         uprv_strcat(ourPathBuffer, ".dat");
    758         uprv_mapFile(&tData, ourPathBuffer);
    759     }
    760 #endif
    761 
    762     if (!UDataMemory_isLoaded(&tData)) {
    763         /* no common data */
    764         *pErrorCode=U_FILE_ACCESS_ERROR;
    765         return NULL;
    766     }
    767 
    768     /* we have mapped a file, check its header */
    769     udata_checkCommonData(&tData, pErrorCode);
    770 
    771 
    772     /* Cache the UDataMemory struct for this .dat file,
    773      *   so we won't need to hunt it down and map it again next time
    774      *   something is needed from it.                */
    775     return udata_cacheDataItem(inBasename, &tData, pErrorCode);
    776 }
    777 
    778 
    779 /*----------------------------------------------------------------------*
    780  *                                                                      *
    781  *   extendICUData   If the full set of ICU data was not loaded at      *
    782  *                   program startup, load it now.  This function will  *
    783  *                   be called when the lookup of an ICU data item in   *
    784  *                   the common ICU data fails.                         *
    785  *                                                                      *
    786  *                   return true if new data is loaded, false otherwise.*
    787  *                                                                      *
    788  *----------------------------------------------------------------------*/
    789 static UBool extendICUData(UErrorCode *pErr)
    790 {
    791     UDataMemory   *pData;
    792     UDataMemory   copyPData;
    793     UBool         didUpdate = FALSE;
    794 
    795     /*
    796      * There is a chance for a race condition here.
    797      * Normally, ICU data is loaded from a DLL or via mmap() and
    798      * setCommonICUData() will detect if the same address is set twice.
    799      * If ICU is built with data loading via fread() then the address will
    800      * be different each time the common data is loaded and we may add
    801      * multiple copies of the data.
    802      * In this case, use a mutex to prevent the race.
    803      * Use a specific mutex to avoid nested locks of the global mutex.
    804      */
    805 #if MAP_IMPLEMENTATION==MAP_STDIO
    806     static UMutex extendICUDataMutex = U_MUTEX_INITIALIZER;
    807     umtx_lock(&extendICUDataMutex);
    808 #endif
    809     if(!umtx_loadAcquire(gHaveTriedToLoadCommonData)) {
    810         /* See if we can explicitly open a .dat file for the ICUData. */
    811         pData = openCommonData(
    812                    U_ICUDATA_NAME,            /*  "icudt20l" , for example.          */
    813                    -1,                        /*  Pretend we're not opening ICUData  */
    814                    pErr);
    815 
    816         /* How about if there is no pData, eh... */
    817 
    818        UDataMemory_init(&copyPData);
    819        if(pData != NULL) {
    820           UDatamemory_assign(&copyPData, pData);
    821           copyPData.map = 0;              /* The mapping for this data is owned by the hash table */
    822           copyPData.mapAddr = 0;          /*   which will unmap it when ICU is shut down.         */
    823                                           /* CommonICUData is also unmapped when ICU is shut down.*/
    824                                           /* To avoid unmapping the data twice, zero out the map  */
    825                                           /*   fields in the UDataMemory that we're assigning     */
    826                                           /*   to CommonICUData.                                  */
    827 
    828           didUpdate = /* no longer using this result */
    829               setCommonICUData(&copyPData,/*  The new common data.                                */
    830                        FALSE,             /*  No warnings if write didn't happen                  */
    831                        pErr);             /*  setCommonICUData honors errors; NOP if error set    */
    832         }
    833 
    834         umtx_storeRelease(gHaveTriedToLoadCommonData, 1);
    835     }
    836 
    837     didUpdate = findCommonICUDataByName(U_ICUDATA_NAME);  /* Return 'true' when a racing writes out the extended                        */
    838                                                           /* data after another thread has failed to see it (in openCommonData), so     */
    839                                                           /* extended data can be examined.                                             */
    840                                                           /* Also handles a race through here before gHaveTriedToLoadCommonData is set. */
    841 
    842 #if MAP_IMPLEMENTATION==MAP_STDIO
    843     umtx_unlock(&extendICUDataMutex);
    844 #endif
    845     return didUpdate;               /* Return true if ICUData pointer was updated.   */
    846                                     /*   (Could potentialy have been done by another thread racing */
    847                                     /*   us through here, but that's fine, we still return true    */
    848                                     /*   so that current thread will also examine extended data.   */
    849 }
    850 
    851 /*----------------------------------------------------------------------*
    852  *                                                                      *
    853  *   udata_setCommonData                                                *
    854  *                                                                      *
    855  *----------------------------------------------------------------------*/
    856 U_CAPI void U_EXPORT2
    857 udata_setCommonData(const void *data, UErrorCode *pErrorCode) {
    858     UDataMemory dataMemory;
    859 
    860     if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
    861         return;
    862     }
    863 
    864     if(data==NULL) {
    865         *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
    866         return;
    867     }
    868 
    869     /* set the data pointer and test for validity */
    870     UDataMemory_init(&dataMemory);
    871     UDataMemory_setData(&dataMemory, data);
    872     udata_checkCommonData(&dataMemory, pErrorCode);
    873     if (U_FAILURE(*pErrorCode)) {return;}
    874 
    875     /* we have good data */
    876     /* Set it up as the ICU Common Data.  */
    877     setCommonICUData(&dataMemory, TRUE, pErrorCode);
    878 }
    879 
    880 /*---------------------------------------------------------------------------
    881  *
    882  *  udata_setAppData
    883  *
    884  *---------------------------------------------------------------------------- */
    885 U_CAPI void U_EXPORT2
    886 udata_setAppData(const char *path, const void *data, UErrorCode *err)
    887 {
    888     UDataMemory     udm;
    889 
    890     if(err==NULL || U_FAILURE(*err)) {
    891         return;
    892     }
    893     if(data==NULL) {
    894         *err=U_ILLEGAL_ARGUMENT_ERROR;
    895         return;
    896     }
    897 
    898     UDataMemory_init(&udm);
    899     UDataMemory_setData(&udm, data);
    900     udata_checkCommonData(&udm, err);
    901     udata_cacheDataItem(path, &udm, err);
    902 }
    903 
    904 /*----------------------------------------------------------------------------*
    905  *                                                                            *
    906  *  checkDataItem     Given a freshly located/loaded data item, either        *
    907  *                    an entry in a common file or a separately loaded file,  *
    908  *                    sanity check its header, and see if the data is         *
    909  *                    acceptable to the app.                                  *
    910  *                    If the data is good, create and return a UDataMemory    *
    911  *                    object that can be returned to the application.         *
    912  *                    Return NULL on any sort of failure.                     *
    913  *                                                                            *
    914  *----------------------------------------------------------------------------*/
    915 static UDataMemory *
    916 checkDataItem
    917 (
    918  const DataHeader         *pHeader,         /* The data item to be checked.                */
    919  UDataMemoryIsAcceptable  *isAcceptable,    /* App's call-back function                    */
    920  void                     *context,         /*   pass-thru param for above.                */
    921  const char               *type,            /*   pass-thru param for above.                */
    922  const char               *name,            /*   pass-thru param for above.                */
    923  UErrorCode               *nonFatalErr,     /* Error code if this data was not acceptable  */
    924                                             /*   but openChoice should continue with       */
    925                                             /*   trying to get data from fallback path.    */
    926  UErrorCode               *fatalErr         /* Bad error, caller should return immediately */
    927  )
    928 {
    929     UDataMemory  *rDataMem = NULL;          /* the new UDataMemory, to be returned.        */
    930 
    931     if (U_FAILURE(*fatalErr)) {
    932         return NULL;
    933     }
    934 
    935     if(pHeader->dataHeader.magic1==0xda &&
    936         pHeader->dataHeader.magic2==0x27 &&
    937         (isAcceptable==NULL || isAcceptable(context, type, name, &pHeader->info))
    938     ) {
    939         rDataMem=UDataMemory_createNewInstance(fatalErr);
    940         if (U_FAILURE(*fatalErr)) {
    941             return NULL;
    942         }
    943         rDataMem->pHeader = pHeader;
    944     } else {
    945         /* the data is not acceptable, look further */
    946         /* If we eventually find something good, this errorcode will be */
    947         /*    cleared out.                                              */
    948         *nonFatalErr=U_INVALID_FORMAT_ERROR;
    949     }
    950     return rDataMem;
    951 }
    952 
    953 /**
    954  * @return 0 if not loaded, 1 if loaded or err
    955  */
    956 static UDataMemory *doLoadFromIndividualFiles(const char *pkgName,
    957         const char *dataPath, const char *tocEntryPathSuffix,
    958             /* following arguments are the same as doOpenChoice itself */
    959             const char *path, const char *type, const char *name,
    960              UDataMemoryIsAcceptable *isAcceptable, void *context,
    961              UErrorCode *subErrorCode,
    962              UErrorCode *pErrorCode)
    963 {
    964     const char         *pathBuffer;
    965     UDataMemory         dataMemory;
    966     UDataMemory *pEntryData;
    967 
    968     /* look in ind. files: package\nam.typ  ========================= */
    969     /* init path iterator for individual files */
    970     UDataPathIterator iter(dataPath, pkgName, path, tocEntryPathSuffix, FALSE, pErrorCode);
    971 
    972     while((pathBuffer = iter.next(pErrorCode)))
    973     {
    974 #ifdef UDATA_DEBUG
    975         fprintf(stderr, "UDATA: trying individual file %s\n", pathBuffer);
    976 #endif
    977         if(uprv_mapFile(&dataMemory, pathBuffer))
    978         {
    979             pEntryData = checkDataItem(dataMemory.pHeader, isAcceptable, context, type, name, subErrorCode, pErrorCode);
    980             if (pEntryData != NULL) {
    981                 /* Data is good.
    982                 *  Hand off ownership of the backing memory to the user's UDataMemory.
    983                 *  and return it.   */
    984                 pEntryData->mapAddr = dataMemory.mapAddr;
    985                 pEntryData->map     = dataMemory.map;
    986 
    987 #ifdef UDATA_DEBUG
    988                 fprintf(stderr, "** Mapped file: %s\n", pathBuffer);
    989 #endif
    990                 return pEntryData;
    991             }
    992 
    993             /* the data is not acceptable, or some error occured.  Either way, unmap the memory */
    994             udata_close(&dataMemory);
    995 
    996             /* If we had a nasty error, bail out completely.  */
    997             if (U_FAILURE(*pErrorCode)) {
    998                 return NULL;
    999             }
   1000 
   1001             /* Otherwise remember that we found data but didn't like it for some reason  */
   1002             *subErrorCode=U_INVALID_FORMAT_ERROR;
   1003         }
   1004 #ifdef UDATA_DEBUG
   1005         fprintf(stderr, "%s\n", UDataMemory_isLoaded(&dataMemory)?"LOADED":"not loaded");
   1006 #endif
   1007     }
   1008     return NULL;
   1009 }
   1010 
   1011 /**
   1012  * @return 0 if not loaded, 1 if loaded or err
   1013  */
   1014 static UDataMemory *doLoadFromCommonData(UBool isICUData, const char * /*pkgName*/,
   1015         const char * /*dataPath*/, const char * /*tocEntryPathSuffix*/, const char *tocEntryName,
   1016             /* following arguments are the same as doOpenChoice itself */
   1017             const char *path, const char *type, const char *name,
   1018              UDataMemoryIsAcceptable *isAcceptable, void *context,
   1019              UErrorCode *subErrorCode,
   1020              UErrorCode *pErrorCode)
   1021 {
   1022     UDataMemory        *pEntryData;
   1023     const DataHeader   *pHeader;
   1024     UDataMemory        *pCommonData;
   1025     int32_t            commonDataIndex;
   1026     UBool              checkedExtendedICUData = FALSE;
   1027     /* try to get common data.  The loop is for platforms such as the 390 that do
   1028      *  not initially load the full set of ICU data.  If the lookup of an ICU data item
   1029      *  fails, the full (but slower to load) set is loaded, the and the loop repeats,
   1030      *  trying the lookup again.  Once the full set of ICU data is loaded, the loop wont
   1031      *  repeat because the full set will be checked the first time through.
   1032      *
   1033      *  The loop also handles the fallback to a .dat file if the application linked
   1034      *   to the stub data library rather than a real library.
   1035      */
   1036     for (commonDataIndex = isICUData ? 0 : -1;;) {
   1037         pCommonData=openCommonData(path, commonDataIndex, subErrorCode); /** search for pkg **/
   1038 
   1039         if(U_SUCCESS(*subErrorCode) && pCommonData!=NULL) {
   1040             int32_t length;
   1041 
   1042             /* look up the data piece in the common data */
   1043             pHeader=pCommonData->vFuncs->Lookup(pCommonData, tocEntryName, &length, subErrorCode);
   1044 #ifdef UDATA_DEBUG
   1045             fprintf(stderr, "%s: pHeader=%p - %s\n", tocEntryName, pHeader, u_errorName(*subErrorCode));
   1046 #endif
   1047 
   1048             if(pHeader!=NULL) {
   1049                 pEntryData = checkDataItem(pHeader, isAcceptable, context, type, name, subErrorCode, pErrorCode);
   1050 #ifdef UDATA_DEBUG
   1051                 fprintf(stderr, "pEntryData=%p\n", pEntryData);
   1052 #endif
   1053                 if (U_FAILURE(*pErrorCode)) {
   1054                     return NULL;
   1055                 }
   1056                 if (pEntryData != NULL) {
   1057                     pEntryData->length = length;
   1058                     return pEntryData;
   1059                 }
   1060             }
   1061         }
   1062         /* Data wasn't found.  If we were looking for an ICUData item and there is
   1063          * more data available, load it and try again,
   1064          * otherwise break out of this loop. */
   1065         if (!isICUData) {
   1066             return NULL;
   1067         } else if (pCommonData != NULL) {
   1068             ++commonDataIndex;  /* try the next data package */
   1069         } else if ((!checkedExtendedICUData) && extendICUData(subErrorCode)) {
   1070             checkedExtendedICUData = TRUE;
   1071             /* try this data package slot again: it changed from NULL to non-NULL */
   1072         } else {
   1073             return NULL;
   1074         }
   1075     }
   1076 }
   1077 
   1078 /*
   1079  * Identify the Time Zone resources that are subject to special override data loading.
   1080  */
   1081 static UBool isTimeZoneFile(const char *name, const char *type) {
   1082     return ((uprv_strcmp(type, "res") == 0) &&
   1083             (uprv_strcmp(name, "zoneinfo64") == 0 ||
   1084              uprv_strcmp(name, "timezoneTypes") == 0 ||
   1085              uprv_strcmp(name, "windowsZones") == 0 ||
   1086              uprv_strcmp(name, "metaZones") == 0));
   1087 }
   1088 
   1089 /*
   1090  *  A note on the ownership of Mapped Memory
   1091  *
   1092  *  For common format files, ownership resides with the UDataMemory object
   1093  *    that lives in the cache of opened common data.  These UDataMemorys are private
   1094  *    to the udata implementation, and are never seen directly by users.
   1095  *
   1096  *    The UDataMemory objects returned to users will have the address of some desired
   1097  *    data within the mapped region, but they wont have the mapping info itself, and thus
   1098  *    won't cause anything to be removed from memory when they are closed.
   1099  *
   1100  *  For individual data files, the UDataMemory returned to the user holds the
   1101  *  information necessary to unmap the data on close.  If the user independently
   1102  *  opens the same data file twice, two completely independent mappings will be made.
   1103  *  (There is no cache of opened data items from individual files, only a cache of
   1104  *   opened Common Data files, that is, files containing a collection of data items.)
   1105  *
   1106  *  For common data passed in from the user via udata_setAppData() or
   1107  *  udata_setCommonData(), ownership remains with the user.
   1108  *
   1109  *  UDataMemory objects themselves, as opposed to the memory they describe,
   1110  *  can be anywhere - heap, stack/local or global.
   1111  *  They have a flag to indicate when they're heap allocated and thus
   1112  *  must be deleted when closed.
   1113  */
   1114 
   1115 
   1116 /*----------------------------------------------------------------------------*
   1117  *                                                                            *
   1118  * main data loading functions                                                *
   1119  *                                                                            *
   1120  *----------------------------------------------------------------------------*/
   1121 static UDataMemory *
   1122 doOpenChoice(const char *path, const char *type, const char *name,
   1123              UDataMemoryIsAcceptable *isAcceptable, void *context,
   1124              UErrorCode *pErrorCode)
   1125 {
   1126     UDataMemory         *retVal = NULL;
   1127 
   1128     const char         *dataPath;
   1129 
   1130     int32_t             tocEntrySuffixIndex;
   1131     const char         *tocEntryPathSuffix;
   1132     UErrorCode          subErrorCode=U_ZERO_ERROR;
   1133     const char         *treeChar;
   1134 
   1135     UBool               isICUData = FALSE;
   1136 
   1137 
   1138     /* Is this path ICU data? */
   1139     if(path == NULL ||
   1140        !strcmp(path, U_ICUDATA_ALIAS) ||  /* "ICUDATA" */
   1141        !uprv_strncmp(path, U_ICUDATA_NAME U_TREE_SEPARATOR_STRING, /* "icudt26e-" */
   1142                      uprv_strlen(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING)) ||
   1143        !uprv_strncmp(path, U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING, /* "ICUDATA-" */
   1144                      uprv_strlen(U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING))) {
   1145       isICUData = TRUE;
   1146     }
   1147 
   1148 #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR)  /* Windows:  try "foo\bar" and "foo/bar" */
   1149     /* remap from alternate path char to the main one */
   1150     CharString altSepPath;
   1151     if(path) {
   1152         if(uprv_strchr(path,U_FILE_ALT_SEP_CHAR) != NULL) {
   1153             altSepPath.append(path, *pErrorCode);
   1154             char *p;
   1155             while((p=uprv_strchr(altSepPath.data(), U_FILE_ALT_SEP_CHAR))) {
   1156                 *p = U_FILE_SEP_CHAR;
   1157             }
   1158 #if defined (UDATA_DEBUG)
   1159             fprintf(stderr, "Changed path from [%s] to [%s]\n", path, altSepPath.s);
   1160 #endif
   1161             path = altSepPath.data();
   1162         }
   1163     }
   1164 #endif
   1165 
   1166     CharString tocEntryName; /* entry name in tree format. ex:  'icudt28b/coll/ar.res' */
   1167     CharString tocEntryPath; /* entry name in path format. ex:  'icudt28b\\coll\\ar.res' */
   1168 
   1169     CharString pkgName;
   1170     CharString treeName;
   1171 
   1172     /* ======= Set up strings */
   1173     if(path==NULL) {
   1174         pkgName.append(U_ICUDATA_NAME, *pErrorCode);
   1175     } else {
   1176         const char *pkg;
   1177         const char *first;
   1178         pkg = uprv_strrchr(path, U_FILE_SEP_CHAR);
   1179         first = uprv_strchr(path, U_FILE_SEP_CHAR);
   1180         if(uprv_pathIsAbsolute(path) || (pkg != first)) { /* more than one slash in the path- not a tree name */
   1181             /* see if this is an /absolute/path/to/package  path */
   1182             if(pkg) {
   1183                 pkgName.append(pkg+1, *pErrorCode);
   1184             } else {
   1185                 pkgName.append(path, *pErrorCode);
   1186             }
   1187         } else {
   1188             treeChar = uprv_strchr(path, U_TREE_SEPARATOR);
   1189             if(treeChar) {
   1190                 treeName.append(treeChar+1, *pErrorCode); /* following '-' */
   1191                 if(isICUData) {
   1192                     pkgName.append(U_ICUDATA_NAME, *pErrorCode);
   1193                 } else {
   1194                     pkgName.append(path, (int32_t)(treeChar-path), *pErrorCode);
   1195                     if (first == NULL) {
   1196                         /*
   1197                         This user data has no path, but there is a tree name.
   1198                         Look up the correct path from the data cache later.
   1199                         */
   1200                         path = pkgName.data();
   1201                     }
   1202                 }
   1203             } else {
   1204                 if(isICUData) {
   1205                     pkgName.append(U_ICUDATA_NAME, *pErrorCode);
   1206                 } else {
   1207                     pkgName.append(path, *pErrorCode);
   1208                 }
   1209             }
   1210         }
   1211     }
   1212 
   1213 #ifdef UDATA_DEBUG
   1214     fprintf(stderr, " P=%s T=%s\n", pkgName.data(), treeName.data());
   1215 #endif
   1216 
   1217     /* setting up the entry name and file name
   1218      * Make up a full name by appending the type to the supplied
   1219      *  name, assuming that a type was supplied.
   1220      */
   1221 
   1222     /* prepend the package */
   1223     tocEntryName.append(pkgName, *pErrorCode);
   1224     tocEntryPath.append(pkgName, *pErrorCode);
   1225     tocEntrySuffixIndex = tocEntryName.length();
   1226 
   1227     if(!treeName.isEmpty()) {
   1228         tocEntryName.append(U_TREE_ENTRY_SEP_CHAR, *pErrorCode).append(treeName, *pErrorCode);
   1229         tocEntryPath.append(U_FILE_SEP_CHAR, *pErrorCode).append(treeName, *pErrorCode);
   1230     }
   1231 
   1232     tocEntryName.append(U_TREE_ENTRY_SEP_CHAR, *pErrorCode).append(name, *pErrorCode);
   1233     tocEntryPath.append(U_FILE_SEP_CHAR, *pErrorCode).append(name, *pErrorCode);
   1234     if(type!=NULL && *type!=0) {
   1235         tocEntryName.append(".", *pErrorCode).append(type, *pErrorCode);
   1236         tocEntryPath.append(".", *pErrorCode).append(type, *pErrorCode);
   1237     }
   1238     tocEntryPathSuffix = tocEntryPath.data()+tocEntrySuffixIndex; /* suffix starts here */
   1239 
   1240 #ifdef UDATA_DEBUG
   1241     fprintf(stderr, " tocEntryName = %s\n", tocEntryName.data());
   1242     fprintf(stderr, " tocEntryPath = %s\n", tocEntryName.data());
   1243 #endif
   1244 
   1245     if(path == NULL) {
   1246         path = COMMON_DATA_NAME; /* "icudt26e" */
   1247     }
   1248 
   1249     /************************ Begin loop looking for ind. files ***************/
   1250 #ifdef UDATA_DEBUG
   1251     fprintf(stderr, "IND: inBasename = %s, pkg=%s\n", "(n/a)", packageNameFromPath(path));
   1252 #endif
   1253 
   1254     /* End of dealing with a null basename */
   1255     dataPath = u_getDataDirectory();
   1256 
   1257     /****    Time zone individual files override  */
   1258     if (isTimeZoneFile(name, type) && isICUData) {
   1259         const char *tzFilesDir = u_getTimeZoneFilesDirectory(pErrorCode);
   1260         if (tzFilesDir[0] != 0) {
   1261 #ifdef UDATA_DEBUG
   1262             fprintf(stderr, "Trying Time Zone Files directory = %s\n", tzFilesDir);
   1263 #endif
   1264             retVal = doLoadFromIndividualFiles(/* pkgName.data() */ "", tzFilesDir, tocEntryPathSuffix,
   1265                             /* path */ "", type, name, isAcceptable, context, &subErrorCode, pErrorCode);
   1266             if((retVal != NULL) || U_FAILURE(*pErrorCode)) {
   1267                 return retVal;
   1268             }
   1269         }
   1270     }
   1271 
   1272     /****    COMMON PACKAGE  - only if packages are first. */
   1273     if(gDataFileAccess == UDATA_PACKAGES_FIRST) {
   1274 #ifdef UDATA_DEBUG
   1275         fprintf(stderr, "Trying packages (UDATA_PACKAGES_FIRST)\n");
   1276 #endif
   1277         /* #2 */
   1278         retVal = doLoadFromCommonData(isICUData,
   1279                             pkgName.data(), dataPath, tocEntryPathSuffix, tocEntryName.data(),
   1280                             path, type, name, isAcceptable, context, &subErrorCode, pErrorCode);
   1281         if((retVal != NULL) || U_FAILURE(*pErrorCode)) {
   1282             return retVal;
   1283         }
   1284     }
   1285 
   1286     /****    INDIVIDUAL FILES  */
   1287     if((gDataFileAccess==UDATA_PACKAGES_FIRST) ||
   1288        (gDataFileAccess==UDATA_FILES_FIRST)) {
   1289 #ifdef UDATA_DEBUG
   1290         fprintf(stderr, "Trying individual files\n");
   1291 #endif
   1292         /* Check to make sure that there is a dataPath to iterate over */
   1293         if ((dataPath && *dataPath) || !isICUData) {
   1294             retVal = doLoadFromIndividualFiles(pkgName.data(), dataPath, tocEntryPathSuffix,
   1295                             path, type, name, isAcceptable, context, &subErrorCode, pErrorCode);
   1296             if((retVal != NULL) || U_FAILURE(*pErrorCode)) {
   1297                 return retVal;
   1298             }
   1299         }
   1300     }
   1301 
   1302     /****    COMMON PACKAGE  */
   1303     if((gDataFileAccess==UDATA_ONLY_PACKAGES) ||
   1304        (gDataFileAccess==UDATA_FILES_FIRST)) {
   1305 #ifdef UDATA_DEBUG
   1306         fprintf(stderr, "Trying packages (UDATA_ONLY_PACKAGES || UDATA_FILES_FIRST)\n");
   1307 #endif
   1308         retVal = doLoadFromCommonData(isICUData,
   1309                             pkgName.data(), dataPath, tocEntryPathSuffix, tocEntryName.data(),
   1310                             path, type, name, isAcceptable, context, &subErrorCode, pErrorCode);
   1311         if((retVal != NULL) || U_FAILURE(*pErrorCode)) {
   1312             return retVal;
   1313         }
   1314     }
   1315 
   1316     /* Load from DLL.  If we haven't attempted package load, we also haven't had any chance to
   1317         try a DLL (static or setCommonData/etc)  load.
   1318          If we ever have a "UDATA_ONLY_FILES", add it to the or list here.  */
   1319     if(gDataFileAccess==UDATA_NO_FILES) {
   1320 #ifdef UDATA_DEBUG
   1321         fprintf(stderr, "Trying common data (UDATA_NO_FILES)\n");
   1322 #endif
   1323         retVal = doLoadFromCommonData(isICUData,
   1324                             pkgName.data(), "", tocEntryPathSuffix, tocEntryName.data(),
   1325                             path, type, name, isAcceptable, context, &subErrorCode, pErrorCode);
   1326         if((retVal != NULL) || U_FAILURE(*pErrorCode)) {
   1327             return retVal;
   1328         }
   1329     }
   1330 
   1331     /* data not found */
   1332     if(U_SUCCESS(*pErrorCode)) {
   1333         if(U_SUCCESS(subErrorCode)) {
   1334             /* file not found */
   1335             *pErrorCode=U_FILE_ACCESS_ERROR;
   1336         } else {
   1337             /* entry point not found or rejected */
   1338             *pErrorCode=subErrorCode;
   1339         }
   1340     }
   1341     return retVal;
   1342 }
   1343 
   1344 
   1345 
   1346 /* API ---------------------------------------------------------------------- */
   1347 
   1348 U_CAPI UDataMemory * U_EXPORT2
   1349 udata_open(const char *path, const char *type, const char *name,
   1350            UErrorCode *pErrorCode) {
   1351 #ifdef UDATA_DEBUG
   1352   fprintf(stderr, "udata_open(): Opening: %s : %s . %s\n", (path?path:"NULL"), name, type);
   1353     fflush(stderr);
   1354 #endif
   1355 
   1356     if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
   1357         return NULL;
   1358     } else if(name==NULL || *name==0) {
   1359         *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
   1360         return NULL;
   1361     } else {
   1362         return doOpenChoice(path, type, name, NULL, NULL, pErrorCode);
   1363     }
   1364 }
   1365 
   1366 
   1367 
   1368 U_CAPI UDataMemory * U_EXPORT2
   1369 udata_openChoice(const char *path, const char *type, const char *name,
   1370                  UDataMemoryIsAcceptable *isAcceptable, void *context,
   1371                  UErrorCode *pErrorCode) {
   1372 #ifdef UDATA_DEBUG
   1373   fprintf(stderr, "udata_openChoice(): Opening: %s : %s . %s\n", (path?path:"NULL"), name, type);
   1374 #endif
   1375 
   1376     if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
   1377         return NULL;
   1378     } else if(name==NULL || *name==0 || isAcceptable==NULL) {
   1379         *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
   1380         return NULL;
   1381     } else {
   1382         return doOpenChoice(path, type, name, isAcceptable, context, pErrorCode);
   1383     }
   1384 }
   1385 
   1386 
   1387 
   1388 U_CAPI void U_EXPORT2
   1389 udata_getInfo(UDataMemory *pData, UDataInfo *pInfo) {
   1390     if(pInfo!=NULL) {
   1391         if(pData!=NULL && pData->pHeader!=NULL) {
   1392             const UDataInfo *info=&pData->pHeader->info;
   1393             uint16_t dataInfoSize=udata_getInfoSize(info);
   1394             if(pInfo->size>dataInfoSize) {
   1395                 pInfo->size=dataInfoSize;
   1396             }
   1397             uprv_memcpy((uint16_t *)pInfo+1, (const uint16_t *)info+1, pInfo->size-2);
   1398             if(info->isBigEndian!=U_IS_BIG_ENDIAN) {
   1399                 /* opposite endianness */
   1400                 uint16_t x=info->reservedWord;
   1401                 pInfo->reservedWord=(uint16_t)((x<<8)|(x>>8));
   1402             }
   1403         } else {
   1404             pInfo->size=0;
   1405         }
   1406     }
   1407 }
   1408 
   1409 
   1410 U_CAPI void U_EXPORT2 udata_setFileAccess(UDataFileAccess access, UErrorCode * /*status*/)
   1411 {
   1412     // Note: this function is documented as not thread safe.
   1413     gDataFileAccess = access;
   1414 }
   1415