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