Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/icu/source/common/udata.cpp
Line
Count
Source (jump to first uncovered line)
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
0
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
0
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
0
137
0
    return TRUE;                   /* Everything was cleaned up */
138
0
}
139
140
static UBool U_CALLCONV
141
findCommonICUDataByName(const char *inBasename, UErrorCode &err)
142
0
{
143
0
    UBool found = FALSE;
144
0
    int32_t i;
145
0
146
0
    UDataMemory  *pData = udata_findCachedData(inBasename, err);
147
0
    if (U_FAILURE(err) || pData == NULL)
148
0
        return FALSE;
149
0
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
0
                /* 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
0
}
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
3
{
173
3
    UDataMemory  *newCommonData = UDataMemory_createNewInstance(pErr);
174
3
    int32_t i;
175
3
    UBool didUpdate = FALSE;
176
3
    if (U_FAILURE(*pErr)) {
177
0
        return FALSE;
178
0
    }
179
3
180
3
    /*  For the assignment, other threads must cleanly see either the old            */
181
3
    /*    or the new, not some partially initialized new.  The old can not be        */
182
3
    /*    deleted - someone may still have a pointer to it lying around in           */
183
3
    /*    their locals.                                                              */
184
3
    UDatamemory_assign(newCommonData, pData);
185
3
    umtx_lock(NULL);
186
3
    for (i = 0; i < UPRV_LENGTHOF(gCommonICUDataArray); ++i) {
187
3
        if (gCommonICUDataArray[i] == NULL) {
188
3
            gCommonICUDataArray[i] = newCommonData;
189
3
            didUpdate = TRUE;
190
3
            break;
191
3
        } else if (gCommonICUDataArray[i]->pHeader == pData->pHeader) {
192
0
            /* The same data pointer is already in the array. */
193
0
            break;
194
0
        }
195
3
    }
196
3
    umtx_unlock(NULL);
197
3
198
3
    if (i == UPRV_LENGTHOF(gCommonICUDataArray) && warn) {
199
0
        *pErr = U_USING_DEFAULT_WARNING;
200
0
    }
201
3
    if (didUpdate) {
202
3
        ucln_common_registerCleanup(UCLN_COMMON_UDATA, udata_cleanup);
203
3
    } else {
204
0
        uprv_free(newCommonData);
205
0
    }
206
3
    return didUpdate;
207
3
}
208
209
#if U_PLATFORM_HAS_WINUWP_API == 0 
210
211
static UBool
212
3
setCommonICUDataPointer(const void *pData, UBool /*warn*/, UErrorCode *pErrorCode) {
213
3
    UDataMemory tData;
214
3
    UDataMemory_init(&tData);
215
3
    UDataMemory_setData(&tData, pData);
216
3
    udata_checkCommonData(&tData, pErrorCode);
217
3
    return setCommonICUData(&tData, FALSE, pErrorCode);
218
3
}
219
220
#endif
221
222
static const char *
223
0
findBasename(const char *path) {
224
0
    const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR);
225
0
    if(basename==NULL) {
226
0
        return path;
227
0
    } else {
228
0
        return basename+1;
229
0
    }
230
0
}
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
0
static void U_CALLCONV udata_initHashTable(UErrorCode &err) {
282
0
    U_ASSERT(gCommonDataCache == NULL);
283
0
    gCommonDataCache = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &err);
284
0
    if (U_FAILURE(err)) {
285
0
       return;
286
0
    }
287
0
    U_ASSERT(gCommonDataCache != NULL);
288
0
    uhash_setValueDeleter(gCommonDataCache, DataCacheElement_deleter);
289
0
    ucln_common_registerCleanup(UCLN_COMMON_UDATA, udata_cleanup);
290
0
}
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
0
static UHashtable *udata_getHashTable(UErrorCode &err) {
297
0
    umtx_initOnce(gCommonDataCacheInitOnce, &udata_initHashTable, err);
298
0
    return gCommonDataCache;
299
0
}
300
301
302
303
static UDataMemory *udata_findCachedData(const char *path, UErrorCode &err)
304
0
{
305
0
    UHashtable        *htable;
306
0
    UDataMemory       *retVal = NULL;
307
0
    DataCacheElement  *el;
308
0
    const char        *baseName;
309
0
310
0
    htable = udata_getHashTable(err);
311
0
    if (U_FAILURE(err)) {
312
0
        return NULL;
313
0
    }
314
0
315
0
    baseName = findBasename(path);   /* Cache remembers only the base name, not the full path. */
316
0
    umtx_lock(NULL);
317
0
    el = (DataCacheElement *)uhash_get(htable, baseName);
318
0
    umtx_unlock(NULL);
319
0
    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
    return retVal;
326
0
}
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
0
337
0
    htable = udata_getHashTable(*pErr);
338
0
    if (U_FAILURE(*pErr)) {
339
0
        return NULL;
340
0
    }
341
0
342
0
    /* Create a new DataCacheElement - the thingy we store in the hash table -
343
0
     * and copy the supplied path and UDataMemoryItems into it.
344
0
     */
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
0
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
0
368
0
    /* Stick the new DataCacheElement into the hash table.
369
0
    */
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
0
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
0
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
0
{
447
#ifdef UDATA_DEBUG
448
        fprintf(stderr, "SUFFIX1=%s PATH=%s\n", inSuffix, inPath);
449
#endif
450
    /** Path **/
451
0
    if(inPath == NULL) {
452
0
        path = u_getDataDirectory();
453
0
    } else {
454
0
        path = inPath;
455
0
    }
456
0
457
0
    /** Package **/
458
0
    if(pkg != NULL) {
459
0
      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
    }
464
0
465
0
    /** Item **/
466
0
    basename = findBasename(item);
467
0
    basenameLen = (int32_t)uprv_strlen(basename);
468
0
469
0
    /** Item path **/
470
0
    if(basename == item) {
471
0
        nextPath = path;
472
0
    } 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
0
    /** Suffix  **/
481
0
    if(inSuffix != NULL) {
482
0
        suffix = inSuffix;
483
0
    } else {
484
0
        suffix = "";
485
0
    }
486
0
487
0
    checkLastFour = doCheckLastFour;
488
0
489
0
    /* pathBuffer will hold the output path strings returned by this iterator */
490
0
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
}
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
0
{
513
0
    if(U_FAILURE(*pErrorCode)) {
514
0
        return NULL;
515
0
    }
516
0
517
0
    const char *currentPath = NULL;
518
0
    int32_t     pathLen = 0;
519
0
    const char *pathBasename;
520
0
521
0
    do
522
0
    {
523
0
        if( nextPath == NULL ) {
524
0
            break;
525
0
        }
526
0
        currentPath = nextPath;
527
0
528
0
        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
0
        } else {
532
0
            /* fix up next for next time */
533
0
            nextPath = uprv_strchr(currentPath, U_PATH_SEP_CHAR);
534
0
            if(nextPath == NULL) {
535
0
                /* segment: entire path */
536
0
                pathLen = (int32_t)uprv_strlen(currentPath); 
537
0
            } else {
538
0
                /* segment: until next segment */
539
0
                pathLen = (int32_t)(nextPath - currentPath);
540
0
                /* skip divider */
541
0
                nextPath ++;
542
0
            }
543
0
        }
544
0
545
0
        if(pathLen == 0) {
546
0
            continue;
547
0
        }
548
0
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
0
564
0
        /* check for .dat files */
565
0
        pathBasename = findBasename(pathBuffer.data());
566
0
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
0
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
                    continue;
588
0
                }
589
0
590
0
                /* 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
                  pathBuffer.truncate(pathLen - packageStub.length());
598
0
                }
599
0
                pathBuffer.append(U_FILE_SEP_CHAR, *pErrorCode);
600
0
            }
601
0
602
0
            /* + basename */
603
0
            pathBuffer.append(packageStub.data()+1, packageStub.length()-1, *pErrorCode);
604
0
605
0
            if(*suffix)  /* tack on suffix */
606
0
            {
607
0
                pathBuffer.append(suffix, *pErrorCode);
608
0
            }
609
0
        }
610
0
611
#ifdef UDATA_DEBUG
612
        fprintf(stderr, " -->  %s\n", pathBuffer.data());
613
#endif
614
615
0
        return pathBuffer.data();
616
0
617
0
    } while(path);
618
0
619
0
    /* fell way off the end */
620
0
    return NULL;
621
0
}
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
9
{
664
9
    UDataMemory tData;
665
9
    const char *pathBuffer;
666
9
    const char *inBasename;
667
9
668
9
    if (U_FAILURE(*pErrorCode)) {
669
0
        return NULL;
670
0
    }
671
9
672
9
    UDataMemory_init(&tData);
673
9
674
9
    /* ??????? TODO revisit this */ 
675
9
    if (commonDataIndex >= 0) {
676
9
        /* "mini-cache" for common ICU data */
677
9
        if(commonDataIndex >= UPRV_LENGTHOF(gCommonICUDataArray)) {
678
0
            return NULL;
679
0
        }
680
9
        {
681
9
            Mutex lock;
682
9
            if(gCommonICUDataArray[commonDataIndex] != NULL) {
683
6
                return gCommonICUDataArray[commonDataIndex];
684
6
            }
685
3
#if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time
686
3
            int32_t i;
687
3
            for(i = 0; i < commonDataIndex; ++i) {
688
0
                if(gCommonICUDataArray[i]->pHeader == &U_ICUDATA_ENTRY_POINT) {
689
0
                    /* The linked-in data is already in the list. */
690
0
                    return NULL;
691
0
                }
692
0
            }
693
3
#endif
694
3
        }
695
3
696
3
        /* Add the linked-in data to the list. */
697
3
        /*
698
3
         * This is where we would check and call weakly linked partial-data-library
699
3
         * access functions.
700
3
         */
701
3
        /*
702
3
        if (uprv_getICUData_collation) {
703
3
            setCommonICUDataPointer(uprv_getICUData_collation(), FALSE, pErrorCode);
704
3
        }
705
3
        if (uprv_getICUData_conversion) {
706
3
            setCommonICUDataPointer(uprv_getICUData_conversion(), FALSE, pErrorCode);
707
3
        }
708
3
        */
709
3
#if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time
710
3
        setCommonICUDataPointer(&U_ICUDATA_ENTRY_POINT, FALSE, pErrorCode);
711
3
        {
712
3
            Mutex lock;
713
3
            return gCommonICUDataArray[commonDataIndex];
714
0
        }
715
0
#endif
716
0
    }
717
0
718
0
719
0
    /* request is NOT for ICU Data.  */
720
0
721
0
    /* Find the base name portion of the supplied path.   */
722
0
    /*   inBasename will be left pointing somewhere within the original path string.      */
723
0
    inBasename = findBasename(path);
724
#ifdef UDATA_DEBUG
725
    fprintf(stderr, "inBasename = %s\n", inBasename);
726
#endif
727
728
0
    if(*inBasename==0) {
729
0
        /* no basename.     This will happen if the original path was a directory name,   */
730
0
        /*    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
0
740
0
   /* Is the requested common data file already open and cached?                     */
741
0
   /*   Note that the cache is keyed by the base name only.  The rest of the path,   */
742
0
   /*     if any, is not considered.                                                 */
743
0
    UDataMemory  *dataToReturn = udata_findCachedData(inBasename, *pErrorCode);
744
0
    if (dataToReturn != NULL || U_FAILURE(*pErrorCode)) {
745
0
        return dataToReturn;
746
0
    }
747
0
748
0
    /* Requested item is not in the cache.
749
0
     * Hunt it down, trying all the path locations
750
0
     */
751
0
752
0
    UDataPathIterator iter(u_getDataDirectory(), inBasename, path, ".dat", TRUE, pErrorCode);
753
0
754
0
    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
    }
764
0
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
0
    if (U_FAILURE(*pErrorCode)) {
777
0
        return NULL;
778
0
    }
779
0
    if (!UDataMemory_isLoaded(&tData)) {
780
0
        /* no common data */
781
0
        *pErrorCode=U_FILE_ACCESS_ERROR;
782
0
        return NULL;
783
0
    }
784
0
785
0
    /* we have mapped a file, check its header */
786
0
    udata_checkCommonData(&tData, pErrorCode);
787
0
788
0
789
0
    /* Cache the UDataMemory struct for this .dat file,
790
0
     *   so we won't need to hunt it down and map it again next time
791
0
     *   something is needed from it.                */
792
0
    return udata_cacheDataItem(inBasename, &tData, pErrorCode);
793
0
}
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
0
{
808
0
    UDataMemory   *pData;
809
0
    UDataMemory   copyPData;
810
0
    UBool         didUpdate = FALSE;
811
0
812
0
    /*
813
0
     * There is a chance for a race condition here.
814
0
     * Normally, ICU data is loaded from a DLL or via mmap() and
815
0
     * setCommonICUData() will detect if the same address is set twice.
816
0
     * If ICU is built with data loading via fread() then the address will
817
0
     * be different each time the common data is loaded and we may add
818
0
     * multiple copies of the data.
819
0
     * In this case, use a mutex to prevent the race.
820
0
     * Use a specific mutex to avoid nested locks of the global mutex.
821
0
     */
822
#if MAP_IMPLEMENTATION==MAP_STDIO
823
    static UMutex extendICUDataMutex = U_MUTEX_INITIALIZER;
824
    umtx_lock(&extendICUDataMutex);
825
#endif
826
0
    if(!umtx_loadAcquire(gHaveTriedToLoadCommonData)) {
827
0
        /* See if we can explicitly open a .dat file for the ICUData. */
828
0
        pData = openCommonData(
829
0
                   U_ICUDATA_NAME,            /*  "icudt20l" , for example.          */
830
0
                   -1,                        /*  Pretend we're not opening ICUData  */
831
0
                   pErr);
832
0
833
0
        /* How about if there is no pData, eh... */
834
0
835
0
       UDataMemory_init(&copyPData);
836
0
       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
0
                                          /* CommonICUData is also unmapped when ICU is shut down.*/
841
0
                                          /* To avoid unmapping the data twice, zero out the map  */
842
0
                                          /*   fields in the UDataMemory that we're assigning     */
843
0
                                          /*   to CommonICUData.                                  */
844
0
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
0
851
0
        umtx_storeRelease(gHaveTriedToLoadCommonData, 1);
852
0
    }
853
0
854
0
    didUpdate = findCommonICUDataByName(U_ICUDATA_NAME, *pErr);  /* Return 'true' when a racing writes out the extended                 */
855
0
                                                          /* data after another thread has failed to see it (in openCommonData), so     */
856
0
                                                          /* extended data can be examined.                                             */
857
0
                                                          /* Also handles a race through here before gHaveTriedToLoadCommonData is set. */
858
0
859
#if MAP_IMPLEMENTATION==MAP_STDIO
860
    umtx_unlock(&extendICUDataMutex);
861
#endif
862
    return didUpdate;               /* Return true if ICUData pointer was updated.   */
863
0
                                    /*   (Could potentialy have been done by another thread racing */
864
0
                                    /*   us through here, but that's fine, we still return true    */
865
0
                                    /*   so that current thread will also examine extended data.   */
866
0
}
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
0
877
0
    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
878
0
        return;
879
0
    }
880
0
881
0
    if(data==NULL) {
882
0
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
883
0
        return;
884
0
    }
885
0
886
0
    /* 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
0
892
0
    /* we have good data */
893
0
    /* 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
0
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
0
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
9
{
946
9
    UDataMemory  *rDataMem = NULL;          /* the new UDataMemory, to be returned.        */
947
9
948
9
    if (U_FAILURE(*fatalErr)) {
949
0
        return NULL;
950
0
    }
951
9
952
9
    if(pHeader->dataHeader.magic1==0xda &&
953
9
        pHeader->dataHeader.magic2==0x27 &&
954
9
        (isAcceptable==NULL || isAcceptable(context, type, name, &pHeader->info))
955
9
    ) {
956
9
        rDataMem=UDataMemory_createNewInstance(fatalErr);
957
9
        if (U_FAILURE(*fatalErr)) {
958
0
            return NULL;
959
0
        }
960
9
        rDataMem->pHeader = pHeader;
961
9
    } else {
962
0
        /* the data is not acceptable, look further */
963
0
        /* If we eventually find something good, this errorcode will be */
964
0
        /*    cleared out.                                              */
965
0
        *nonFatalErr=U_INVALID_FORMAT_ERROR;
966
0
    }
967
9
    return rDataMem;
968
9
}
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
0
985
0
    /* look in ind. files: package\nam.typ  ========================= */
986
0
    /* init path iterator for individual files */
987
0
    UDataPathIterator iter(dataPath, pkgName, path, tocEntryPathSuffix, FALSE, pErrorCode);
988
0
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
0
                /* Data is good.
999
0
                *  Hand off ownership of the backing memory to the user's UDataMemory.
1000
0
                *  and return it.   */
1001
0
                pEntryData->mapAddr = dataMemory.mapAddr;
1002
0
                pEntryData->map     = dataMemory.map;
1003
0
1004
#ifdef UDATA_DEBUG
1005
                fprintf(stderr, "** Mapped file: %s\n", pathBuffer);
1006
#endif
1007
                return pEntryData;
1008
0
            }
1009
0
1010
0
            /* the data is not acceptable, or some error occured.  Either way, unmap the memory */
1011
0
            udata_close(&dataMemory);
1012
0
1013
0
            /* If we had a nasty error, bail out completely.  */
1014
0
            if (U_FAILURE(*pErrorCode)) {
1015
0
                return NULL;
1016
0
            }
1017
0
1018
0
            /* 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
    }
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
9
{
1039
9
    UDataMemory        *pEntryData;
1040
9
    const DataHeader   *pHeader;
1041
9
    UDataMemory        *pCommonData;
1042
9
    int32_t            commonDataIndex;
1043
9
    UBool              checkedExtendedICUData = FALSE;
1044
9
    /* try to get common data.  The loop is for platforms such as the 390 that do
1045
9
     *  not initially load the full set of ICU data.  If the lookup of an ICU data item
1046
9
     *  fails, the full (but slower to load) set is loaded, the and the loop repeats,
1047
9
     *  trying the lookup again.  Once the full set of ICU data is loaded, the loop wont
1048
9
     *  repeat because the full set will be checked the first time through.
1049
9
     *
1050
9
     *  The loop also handles the fallback to a .dat file if the application linked
1051
9
     *   to the stub data library rather than a real library.
1052
9
     */
1053
9
    for (commonDataIndex = isICUData ? 0 : -1;;) {
1054
9
        pCommonData=openCommonData(path, commonDataIndex, subErrorCode); /** search for pkg **/
1055
9
1056
9
        if(U_SUCCESS(*subErrorCode) && pCommonData!=NULL) {
1057
9
            int32_t length;
1058
9
1059
9
            /* look up the data piece in the common data */
1060
9
            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
9
            if(pHeader!=NULL) {
1066
9
                pEntryData = checkDataItem(pHeader, isAcceptable, context, type, name, subErrorCode, pErrorCode);
1067
#ifdef UDATA_DEBUG
1068
                fprintf(stderr, "pEntryData=%p\n", pEntryData);
1069
#endif
1070
9
                if (U_FAILURE(*pErrorCode)) {
1071
0
                    return NULL;
1072
0
                }
1073
9
                if (pEntryData != NULL) {
1074
9
                    pEntryData->length = length;
1075
9
                    return pEntryData;
1076
9
                }
1077
0
            }
1078
9
        }
1079
0
        /* Data wasn't found.  If we were looking for an ICUData item and there is
1080
0
         * more data available, load it and try again,
1081
0
         * otherwise break out of this loop. */
1082
0
        if (!isICUData) {
1083
0
            return NULL;
1084
0
        } else if (pCommonData != NULL) {
1085
0
            ++commonDataIndex;  /* try the next data package */
1086
0
        } else if ((!checkedExtendedICUData) && extendICUData(subErrorCode)) {
1087
0
            checkedExtendedICUData = TRUE;
1088
0
            /* try this data package slot again: it changed from NULL to non-NULL */
1089
0
        } else {
1090
0
            return NULL;
1091
0
        }
1092
0
    }
1093
9
}
1094
1095
/*
1096
 * Identify the Time Zone resources that are subject to special override data loading.
1097
 */
1098
9
static UBool isTimeZoneFile(const char *name, const char *type) {
1099
9
    return ((uprv_strcmp(type, "res") == 0) &&
1100
9
            (uprv_strcmp(name, "zoneinfo64") == 0 ||
1101
3
             uprv_strcmp(name, "timezoneTypes") == 0 ||
1102
3
             uprv_strcmp(name, "windowsZones") == 0 ||
1103
3
             uprv_strcmp(name, "metaZones") == 0));
1104
9
}
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
9
{
1143
9
    UDataMemory         *retVal = NULL;
1144
9
1145
9
    const char         *dataPath;
1146
9
1147
9
    int32_t             tocEntrySuffixIndex;
1148
9
    const char         *tocEntryPathSuffix;
1149
9
    UErrorCode          subErrorCode=U_ZERO_ERROR;
1150
9
    const char         *treeChar;
1151
9
1152
9
    UBool               isICUData = FALSE;
1153
9
1154
9
1155
9
    /* Is this path ICU data? */
1156
9
    if(path == NULL ||
1157
9
       !strcmp(path, U_ICUDATA_ALIAS) ||  /* "ICUDATA" */
1158
9
       !uprv_strncmp(path, U_ICUDATA_NAME U_TREE_SEPARATOR_STRING, /* "icudt26e-" */
1159
9
                     uprv_strlen(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING)) ||  
1160
9
       !uprv_strncmp(path, U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING, /* "ICUDATA-" */
1161
9
                     uprv_strlen(U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING))) {
1162
9
      isICUData = TRUE;
1163
9
    }
1164
9
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
9
    CharString tocEntryName; /* entry name in tree format. ex:  'icudt28b/coll/ar.res' */
1184
9
    CharString tocEntryPath; /* entry name in path format. ex:  'icudt28b\\coll\\ar.res' */
1185
9
1186
9
    CharString pkgName;
1187
9
    CharString treeName;
1188
9
1189
9
    /* ======= Set up strings */
1190
9
    if(path==NULL) {
1191
9
        pkgName.append(U_ICUDATA_NAME, *pErrorCode);
1192
9
    } 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
0
            /* 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
0
                        /*
1214
0
                        This user data has no path, but there is a tree name.
1215
0
                        Look up the correct path from the data cache later.
1216
0
                        */
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
9
1230
#ifdef UDATA_DEBUG
1231
    fprintf(stderr, " P=%s T=%s\n", pkgName.data(), treeName.data());
1232
#endif
1233
1234
9
    /* setting up the entry name and file name 
1235
9
     * Make up a full name by appending the type to the supplied
1236
9
     *  name, assuming that a type was supplied.
1237
9
     */
1238
9
1239
9
    /* prepend the package */
1240
9
    tocEntryName.append(pkgName, *pErrorCode);
1241
9
    tocEntryPath.append(pkgName, *pErrorCode);
1242
9
    tocEntrySuffixIndex = tocEntryName.length();
1243
9
1244
9
    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
9
1249
9
    tocEntryName.append(U_TREE_ENTRY_SEP_CHAR, *pErrorCode).append(name, *pErrorCode);
1250
9
    tocEntryPath.append(U_FILE_SEP_CHAR, *pErrorCode).append(name, *pErrorCode);
1251
9
    if(type!=NULL && *type!=0) {
1252
9
        tocEntryName.append(".", *pErrorCode).append(type, *pErrorCode);
1253
9
        tocEntryPath.append(".", *pErrorCode).append(type, *pErrorCode);
1254
9
    }
1255
9
    tocEntryPathSuffix = tocEntryPath.data()+tocEntrySuffixIndex; /* suffix starts here */
1256
9
1257
#ifdef UDATA_DEBUG
1258
    fprintf(stderr, " tocEntryName = %s\n", tocEntryName.data());
1259
    fprintf(stderr, " tocEntryPath = %s\n", tocEntryName.data());
1260
#endif
1261
1262
9
#if U_PLATFORM_HAS_WINUWP_API == 0 // Windows UWP Platform does not support dll icu data at this time
1263
9
    if(path == NULL) {
1264
9
        path = COMMON_DATA_NAME; /* "icudt26e" */
1265
9
    }
1266
#else
1267
    // Windows UWP expects only a single data file.
1268
    path = COMMON_DATA_NAME; /* "icudt26e" */
1269
#endif
1270
1271
9
    /************************ 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
9
    /* End of dealing with a null basename */
1277
9
    dataPath = u_getDataDirectory();
1278
9
1279
9
    /****    Time zone individual files override  */
1280
9
    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
            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
9
        }
1292
0
    }
1293
9
1294
9
    /****    COMMON PACKAGE  - only if packages are first. */
1295
9
    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
9
    }
1307
9
1308
9
    /****    INDIVIDUAL FILES  */
1309
9
    if((gDataFileAccess==UDATA_PACKAGES_FIRST) ||
1310
9
       (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
9
        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
9
        }
1322
9
    }
1323
9
1324
9
    /****    COMMON PACKAGE  */
1325
9
    if((gDataFileAccess==UDATA_ONLY_PACKAGES) || 
1326
9
       (gDataFileAccess==UDATA_FILES_FIRST)) {
1327
#ifdef UDATA_DEBUG
1328
        fprintf(stderr, "Trying packages (UDATA_ONLY_PACKAGES || UDATA_FILES_FIRST)\n");
1329
#endif
1330
        retVal = doLoadFromCommonData(isICUData,
1331
9
                            pkgName.data(), dataPath, tocEntryPathSuffix, tocEntryName.data(),
1332
9
                            path, type, name, isAcceptable, context, &subErrorCode, pErrorCode);
1333
9
        if((retVal != NULL) || U_FAILURE(*pErrorCode)) {
1334
9
            return retVal;
1335
9
        }
1336
0
    }
1337
0
    
1338
0
    /* Load from DLL.  If we haven't attempted package load, we also haven't had any chance to
1339
0
        try a DLL (static or setCommonData/etc)  load.
1340
0
         If we ever have a "UDATA_ONLY_FILES", add it to the or list here.  */  
1341
0
    if(gDataFileAccess==UDATA_NO_FILES) {
1342
#ifdef UDATA_DEBUG
1343
        fprintf(stderr, "Trying common data (UDATA_NO_FILES)\n");
1344
#endif
1345
        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
0
1353
0
    /* data not found */
1354
0
    if(U_SUCCESS(*pErrorCode)) {
1355
0
        if(U_SUCCESS(subErrorCode)) {
1356
0
            /* file not found */
1357
0
            *pErrorCode=U_FILE_ACCESS_ERROR;
1358
0
        } else {
1359
0
            /* entry point not found or rejected */
1360
0
            *pErrorCode=subErrorCode;
1361
0
        }
1362
0
    }
1363
0
    return retVal;
1364
0
}
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
9
                 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
9
    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
1399
0
        return NULL;
1400
9
    } else if(name==NULL || *name==0 || isAcceptable==NULL) {
1401
0
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1402
0
        return NULL;
1403
9
    } else {
1404
9
        return doOpenChoice(path, type, name, isAcceptable, context, pErrorCode);
1405
9
    }
1406
9
}
1407
1408
1409
1410
U_CAPI void U_EXPORT2
1411
0
udata_getInfo(UDataMemory *pData, UDataInfo *pInfo) {
1412
0
    if(pInfo!=NULL) {
1413
0
        if(pData!=NULL && pData->pHeader!=NULL) {
1414
0
            const UDataInfo *info=&pData->pHeader->info;
1415
0
            uint16_t dataInfoSize=udata_getInfoSize(info);
1416
0
            if(pInfo->size>dataInfoSize) {
1417
0
                pInfo->size=dataInfoSize;
1418
0
            }
1419
0
            uprv_memcpy((uint16_t *)pInfo+1, (const uint16_t *)info+1, pInfo->size-2);
1420
0
            if(info->isBigEndian!=U_IS_BIG_ENDIAN) {
1421
0
                /* opposite endianness */
1422
0
                uint16_t x=info->reservedWord;
1423
0
                pInfo->reservedWord=(uint16_t)((x<<8)|(x>>8));
1424
0
            }
1425
0
        } else {
1426
0
            pInfo->size=0;
1427
0
        }
1428
0
    }
1429
0
}
1430
1431
1432
U_CAPI void U_EXPORT2 udata_setFileAccess(UDataFileAccess access, UErrorCode * /*status*/)
1433
0
{
1434
0
    // Note: this function is documented as not thread safe.
1435
0
    gDataFileAccess = access;
1436
0
}