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