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