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