Coverage Report

Created: 2026-03-12 06:42

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