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