Coverage Report

Created: 2023-06-07 07:17

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