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