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