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