Bug Summary

File:root/firefox-clang/intl/icu/source/common/udata.cpp
Warning:line 849, column 11
Value stored to 'didUpdate' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name udata.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/config/external/icu/common -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/config/external/icu/common -resource-dir /usr/lib/llvm-21/lib/clang/21 -include /root/firefox-clang/config/gcc_hidden.h -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/system_wrappers -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D _GLIBCXX_ASSERTIONS -D DEBUG=1 -D U_COMMON_IMPLEMENTATION -D _LIBCPP_DISABLE_DEPRECATION_WARNINGS -D U_USING_ICU_NAMESPACE=0 -D U_NO_DEFAULT_INCLUDE_UTF_HEADERS=1 -D U_HIDE_OBSOLETE_UTF_OLD_H=1 -D UCONFIG_NO_LEGACY_CONVERSION -D UCONFIG_NO_TRANSLITERATION -D UCONFIG_NO_REGULAR_EXPRESSIONS -D UCONFIG_NO_BREAK_ITERATION -D UCONFIG_NO_IDNA -D UCONFIG_NO_MF2 -D U_CHARSET_IS_UTF8 -D UNISTR_FROM_CHAR_EXPLICIT=explicit -D UNISTR_FROM_STRING_EXPLICIT=explicit -D U_ENABLE_DYLOAD=0 -D U_DEBUG=1 -I /root/firefox-clang/config/external/icu/common -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/config/external/icu/common -I /root/firefox-clang/intl/icu/source/i18n -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/x86_64-linux-gnu/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/backward -internal-isystem /usr/lib/llvm-21/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-error=pessimizing-move -Wno-error=large-by-value-copy=128 -Wno-error=implicit-int-float-conversion -Wno-error=thread-safety-analysis -Wno-error=tautological-type-limit-compare -Wno-invalid-offsetof -Wno-range-loop-analysis -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-deprecated-this-capture -Wno-inline-new-delete -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-vla-cxx-extension -Wno-unknown-warning-option -Wno-comma -Wno-implicit-const-int-float-conversion -Wno-macro-redefined -Wno-microsoft-include -Wno-tautological-unsigned-enum-zero-compare -Wno-unreachable-code-loop-increment -Wno-unreachable-code-return -fdeprecated-macro -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fno-sized-deallocation -fno-aligned-allocation -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2025-06-27-100320-3286336-1 -x c++ /root/firefox-clang/intl/icu/source/common/udata.cpp
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/*
4******************************************************************************
5*
6* Copyright (C) 1999-2016, International Business Machines
7* Corporation and others. All Rights Reserved.
8*
9******************************************************************************
10* file name: udata.cpp
11* encoding: UTF-8
12* tab size: 8 (not used)
13* indentation:4
14*
15* created on: 1999oct25
16* created by: Markus W. Scherer
17*/
18
19#include "unicode/utypes.h" /* U_PLATFORM etc. */
20
21#ifdef __GNUC__4
22/* if gcc
23#define ATTRIBUTE_WEAK __attribute__ ((weak))
24might 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
78U_NAMESPACE_USEusing namespace icu_77;
79
80/*
81 * Forward declarations
82 */
83static 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 */
107static UDataMemory *gCommonICUDataArray[10] = { nullptr }; // Access protected by icu global mutex.
108
109static u_atomic_int32_t gHaveTriedToLoadCommonData {0}; // See extendICUData().
110
111static UHashtable *gCommonDataCache = nullptr; /* Global hash table of opened ICU data files. */
112static icu::UInitOnce gCommonDataCacheInitOnce {};
113
114#if !defined(ICU_DATA_DIR_WINDOWS)
115static 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.
119static UDataFileAccess gDataFileAccess = UDATA_NO_FILES;
120#endif
121
122static UBool U_CALLCONV
123udata_cleanup()
124{
125 int32_t i;
126
127 if (gCommonDataCache) { /* Delete the cache of user data mappings. */
128 uhash_closeuhash_close_77(gCommonDataCache); /* Table owns the contents, and will delete them. */
129 gCommonDataCache = nullptr; /* Cleanup is not thread safe. */
130 }
131 gCommonDataCacheInitOnce.reset();
132
133 for (i = 0; i < UPRV_LENGTHOF(gCommonICUDataArray)(int32_t)(sizeof(gCommonICUDataArray)/sizeof((gCommonICUDataArray
)[0]))
&& gCommonICUDataArray[i] != nullptr; ++i) {
134 udata_closeudata_close_77(gCommonICUDataArray[i]);
135 gCommonICUDataArray[i] = nullptr;
136 }
137 gHaveTriedToLoadCommonData = 0;
138
139 return true; /* Everything was cleaned up */
140}
141
142static UBool U_CALLCONV
143findCommonICUDataByName(const char *inBasename, UErrorCode &err)
144{
145 UBool found = false;
146 int32_t i;
147
148 UDataMemory *pData = udata_findCachedData(inBasename, err);
149 if (U_FAILURE(err) || pData == nullptr)
150 return false;
151
152 {
153 Mutex lock;
154 for (i = 0; i < UPRV_LENGTHOF(gCommonICUDataArray)(int32_t)(sizeof(gCommonICUDataArray)/sizeof((gCommonICUDataArray
)[0]))
; ++i) {
155 if ((gCommonICUDataArray[i] != nullptr) && (gCommonICUDataArray[i]->pHeader == pData->pHeader)) {
156 /* The data pointer is already in the array. */
157 found = true;
158 break;
159 }
160 }
161 }
162 return found;
163}
164
165
166/*
167 * setCommonICUData. Set a UDataMemory to be the global ICU Data
168 */
169static UBool
170setCommonICUData(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{
175 UDataMemory *newCommonData = UDataMemory_createNewInstanceUDataMemory_createNewInstance_77(pErr);
176 int32_t i;
177 UBool didUpdate = false;
178 if (U_FAILURE(*pErr)) {
179 return false;
180 }
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 UDatamemory_assignUDatamemory_assign_77(newCommonData, pData);
187 umtx_lockumtx_lock_77(nullptr);
188 for (i = 0; i < UPRV_LENGTHOF(gCommonICUDataArray)(int32_t)(sizeof(gCommonICUDataArray)/sizeof((gCommonICUDataArray
)[0]))
; ++i) {
189 if (gCommonICUDataArray[i] == nullptr) {
190 gCommonICUDataArray[i] = newCommonData;
191 didUpdate = true;
192 break;
193 } else if (gCommonICUDataArray[i]->pHeader == pData->pHeader) {
194 /* The same data pointer is already in the array. */
195 break;
196 }
197 }
198 umtx_unlockumtx_unlock_77(nullptr);
199
200 if (i == UPRV_LENGTHOF(gCommonICUDataArray)(int32_t)(sizeof(gCommonICUDataArray)/sizeof((gCommonICUDataArray
)[0]))
&& warn) {
201 *pErr = U_USING_DEFAULT_WARNING;
202 }
203 if (didUpdate) {
204 ucln_common_registerCleanupucln_common_registerCleanup_77(UCLN_COMMON_UDATA, udata_cleanup);
205 } else {
206 uprv_freeuprv_free_77(newCommonData);
207 }
208 return didUpdate;
209}
210
211#if !defined(ICU_DATA_DIR_WINDOWS)
212
213static UBool
214setCommonICUDataPointer(const void *pData, UBool /*warn*/, UErrorCode *pErrorCode) {
215 UDataMemory tData;
216 UDataMemory_initUDataMemory_init_77(&tData);
217 UDataMemory_setDataUDataMemory_setData_77(&tData, pData);
218 udata_checkCommonDataudata_checkCommonData_77(&tData, pErrorCode);
219 return setCommonICUData(&tData, false, pErrorCode);
220}
221
222#endif
223
224static const char *
225findBasename(const char *path) {
226 const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR):: strrchr(path, '/');
227 if(basename==nullptr) {
228 return path;
229 } else {
230 return basename+1;
231 }
232}
233
234#ifdef UDATA_DEBUG
235static const char *
236packageNameFromPath(const char *path)
237{
238 if((path == nullptr) || (*path == 0)) {
239 return U_ICUDATA_NAME"icudt" "77" "l";
240 }
241
242 path = findBasename(path);
243
244 if((path == nullptr) || (*path == 0)) {
245 return U_ICUDATA_NAME"icudt" "77" "l";
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
264typedef 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 */
276static void U_CALLCONV DataCacheElement_deleter(void *pDCEl) {
277 DataCacheElement* p = static_cast<DataCacheElement*>(pDCEl);
278 udata_closeudata_close_77(p->item); /* unmaps storage */
279 uprv_freeuprv_free_77(p->name); /* delete the hash key string. */
280 uprv_freeuprv_free_77(pDCEl); /* delete 'this' */
281}
282
283static void U_CALLCONV udata_initHashTable(UErrorCode &err) {
284 U_ASSERT(gCommonDataCache == nullptr)(static_cast <bool> (gCommonDataCache == nullptr) ? void
(0) : __assert_fail ("gCommonDataCache == nullptr", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
285 gCommonDataCache = uhash_openuhash_open_77(uhash_hashCharsuhash_hashChars_77, uhash_compareCharsuhash_compareChars_77, nullptr, &err);
286 if (U_FAILURE(err)) {
287 return;
288 }
289 U_ASSERT(gCommonDataCache != nullptr)(static_cast <bool> (gCommonDataCache != nullptr) ? void
(0) : __assert_fail ("gCommonDataCache != nullptr", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
290 uhash_setValueDeleteruhash_setValueDeleter_77(gCommonDataCache, DataCacheElement_deleter);
291 ucln_common_registerCleanupucln_common_registerCleanup_77(UCLN_COMMON_UDATA, udata_cleanup);
292}
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 */
298static UHashtable *udata_getHashTable(UErrorCode &err) {
299 umtx_initOnce(gCommonDataCacheInitOnce, &udata_initHashTable, err);
300 return gCommonDataCache;
301}
302
303
304
305static UDataMemory *udata_findCachedData(const char *path, UErrorCode &err)
306{
307 UHashtable *htable;
308 UDataMemory *retVal = nullptr;
309 DataCacheElement *el;
310 const char *baseName;
311
312 htable = udata_getHashTable(err);
313 if (U_FAILURE(err)) {
314 return nullptr;
315 }
316
317 baseName = findBasename(path); /* Cache remembers only the base name, not the full path. */
318 umtx_lockumtx_lock_77(nullptr);
319 el = static_cast<DataCacheElement*>(uhash_getuhash_get_77(htable, baseName));
320 umtx_unlockumtx_unlock_77(nullptr);
321 if (el != nullptr) {
322 retVal = el->item;
323 }
324#ifdef UDATA_DEBUG
325 fprintf(stderrstderr, "Cache: [%s] -> %p\n", baseName, (void*) retVal);
326#endif
327 return retVal;
328}
329
330
331static UDataMemory *udata_cacheDataItem(const char *path, UDataMemory *item, UErrorCode *pErr) {
332 DataCacheElement *newElement;
333 const char *baseName;
334 int32_t nameLen;
335 UHashtable *htable;
336 DataCacheElement *oldValue = nullptr;
337 UErrorCode subErr = U_ZERO_ERROR;
338
339 htable = udata_getHashTable(*pErr);
340 if (U_FAILURE(*pErr)) {
341 return nullptr;
342 }
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 newElement = static_cast<DataCacheElement*>(uprv_mallocuprv_malloc_77(sizeof(DataCacheElement)));
348 if (newElement == nullptr) {
349 *pErr = U_MEMORY_ALLOCATION_ERROR;
350 return nullptr;
351 }
352 newElement->item = UDataMemory_createNewInstanceUDataMemory_createNewInstance_77(pErr);
353 if (U_FAILURE(*pErr)) {
354 uprv_freeuprv_free_77(newElement);
355 return nullptr;
356 }
357 UDatamemory_assignUDatamemory_assign_77(newElement->item, item);
358
359 baseName = findBasename(path);
360 nameLen = static_cast<int32_t>(uprv_strlen(baseName):: strlen(baseName));
361 newElement->name = static_cast<char*>(uprv_mallocuprv_malloc_77(nameLen + 1));
362 if (newElement->name == nullptr) {
363 *pErr = U_MEMORY_ALLOCATION_ERROR;
364 uprv_freeuprv_free_77(newElement->item);
365 uprv_freeuprv_free_77(newElement);
366 return nullptr;
367 }
368 uprv_strcpy(newElement->name, baseName):: strcpy(newElement->name, baseName);
369
370 /* Stick the new DataCacheElement into the hash table.
371 */
372 umtx_lockumtx_lock_77(nullptr);
373 oldValue = static_cast<DataCacheElement*>(uhash_getuhash_get_77(htable, path));
374 if (oldValue != nullptr) {
375 subErr = U_USING_DEFAULT_WARNING;
376 }
377 else {
378 uhash_putuhash_put_77(
379 htable,
380 newElement->name, /* Key */
381 newElement, /* Value */
382 &subErr);
383 }
384 umtx_unlockumtx_unlock_77(nullptr);
385
386#ifdef UDATA_DEBUG
387 fprintf(stderrstderr, "Cache: [%s] <<< %p : %s. vFunc=%p\n", newElement->name,
388 (void*) newElement->item, u_errorNameu_errorName_77(subErr), (void*) newElement->item->vFuncs);
389#endif
390
391 if (subErr == U_USING_DEFAULT_WARNING || U_FAILURE(subErr)) {
392 *pErr = subErr; /* copy sub err unto fillin ONLY if something happens. */
393 uprv_freeuprv_free_77(newElement->name);
394 uprv_freeuprv_free_77(newElement->item);
395 uprv_freeuprv_free_77(newElement);
396 return oldValue ? oldValue->item : nullptr;
397 }
398
399 return newElement->item;
400}
401
402/*----------------------------------------------------------------------*==============
403 * *
404 * Path management. Could be shared with other tools/etc if need be *
405 * later on. *
406 * *
407 *----------------------------------------------------------------------*/
408
409U_NAMESPACE_BEGINnamespace icu_77 {
410
411class UDataPathIterator
412{
413public:
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
419private:
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 nullptr, 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 */
448UDataPathIterator::UDataPathIterator(const char *inPath, const char *pkg,
449 const char *item, const char *inSuffix, UBool doCheckLastFour,
450 UErrorCode *pErrorCode)
451{
452#ifdef UDATA_DEBUG
453 fprintf(stderrstderr, "SUFFIX1=%s PATH=%s\n", inSuffix, inPath);
454#endif
455 /** Path **/
456 if(inPath == nullptr) {
457 path = u_getDataDirectoryu_getDataDirectory_77();
458 } else {
459 path = inPath;
460 }
461
462 /** Package **/
463 if(pkg != nullptr) {
464 packageStub.append(U_FILE_SEP_CHAR'/', *pErrorCode).append(pkg, *pErrorCode);
465#ifdef UDATA_DEBUG
466 fprintf(stderrstderr, "STUB=%s [%d]\n", packageStub.data(), packageStub.length());
467#endif
468 }
469
470 /** Item **/
471 basename = findBasename(item);
472 basenameLen = static_cast<int32_t>(uprv_strlen(basename):: strlen(basename));
473
474 /** Item path **/
475 if(basename == item) {
476 nextPath = path;
477 } else {
478 itemPath.append(item, static_cast<int32_t>(basename - item), *pErrorCode);
479 nextPath = itemPath.data();
480 }
481#ifdef UDATA_DEBUG
482 fprintf(stderrstderr, "SUFFIX=%s [%p]\n", inSuffix, (void*) inSuffix);
483#endif
484
485 /** Suffix **/
486 if(inSuffix != nullptr) {
487 suffix = inSuffix;
488 } else {
489 suffix = "";
490 }
491
492 checkLastFour = doCheckLastFour;
493
494 /* pathBuffer will hold the output path strings returned by this iterator */
495
496#ifdef UDATA_DEBUG
497 fprintf(stderrstderr, "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}
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 nullptr if there are no more.
514 */
515const char *UDataPathIterator::next(UErrorCode *pErrorCode)
516{
517 if(U_FAILURE(*pErrorCode)) {
518 return nullptr;
519 }
520
521 const char *currentPath = nullptr;
522 int32_t pathLen = 0;
523 const char *pathBasename;
524
525 do
526 {
527 if( nextPath == nullptr ) {
528 break;
529 }
530 currentPath = nextPath;
531
532 if(nextPath == itemPath.data()) { /* we were processing item's path. */
533 nextPath = path; /* start with regular path next tm. */
534 pathLen = static_cast<int32_t>(uprv_strlen(currentPath):: strlen(currentPath));
535 } else {
536 /* fix up next for next time */
537 nextPath = uprv_strchr(currentPath, U_PATH_SEP_CHAR):: strchr(currentPath, ':');
538 if(nextPath == nullptr) {
539 /* segment: entire path */
540 pathLen = static_cast<int32_t>(uprv_strlen(currentPath):: strlen(currentPath));
541 } else {
542 /* segment: until next segment */
543 pathLen = static_cast<int32_t>(nextPath - currentPath);
544 /* skip divider */
545 nextPath ++;
546 }
547 }
548
549 if(pathLen == 0) {
550 continue;
551 }
552
553#ifdef UDATA_DEBUG
554 fprintf(stderrstderr, "rest of path (IDD) = %s\n", currentPath);
555 fprintf(stderrstderr, " ");
556 {
557 int32_t qqq;
558 for(qqq=0;qqq<pathLen;qqq++)
559 {
560 fprintf(stderrstderr, " ");
561 }
562
563 fprintf(stderrstderr, "^\n");
564 }
565#endif
566 pathBuffer.clear().append(currentPath, pathLen, *pErrorCode);
567
568 /* check for .dat files */
569 pathBasename = findBasename(pathBuffer.data());
570
571 if(checkLastFour &&
572 (pathLen>=4) &&
573 uprv_strncmp(pathBuffer.data() +(pathLen-4), suffix.data(), 4):: strncmp(pathBuffer.data() +(pathLen-4), suffix.data(), 4)==0 && /* suffix matches */
574 uprv_strncmp(findBasename(pathBuffer.data()), basename, basenameLen):: strncmp(findBasename(pathBuffer.data()), basename, basenameLen
)
==0 && /* base matches */
575 uprv_strlen(pathBasename):: strlen(pathBasename)==(basenameLen+4)) { /* base+suffix = full len */
576
577#ifdef UDATA_DEBUG
578 fprintf(stderrstderr, "Have %s file on the path: %s\n", suffix.data(), pathBuffer.data());
579#endif
580 /* do nothing */
581 }
582 else
583 { /* regular dir path */
584 if(pathBuffer[pathLen-1] != U_FILE_SEP_CHAR'/') {
585 if((pathLen>=4) &&
586 uprv_strncmp(pathBuffer.data()+(pathLen-4), ".dat", 4):: strncmp(pathBuffer.data()+(pathLen-4), ".dat", 4) == 0)
587 {
588#ifdef UDATA_DEBUG
589 fprintf(stderrstderr, "skipping non-directory .dat file %s\n", pathBuffer.data());
590#endif
591 continue;
592 }
593
594 /* Check if it is a directory with the same name as our package */
595 if(!packageStub.isEmpty() &&
596 (pathLen > packageStub.length()) &&
597 !uprv_strcmp(pathBuffer.data() + pathLen - packageStub.length(), packageStub.data()):: strcmp(pathBuffer.data() + pathLen - packageStub.length(),
packageStub.data())
) {
598#ifdef UDATA_DEBUG
599 fprintf(stderrstderr, "Found stub %s (will add package %s of len %d)\n", packageStub.data(), basename, basenameLen);
600#endif
601 pathBuffer.truncate(pathLen - packageStub.length());
602 }
603 pathBuffer.append(U_FILE_SEP_CHAR'/', *pErrorCode);
604 }
605
606 /* + basename */
607 pathBuffer.append(packageStub.data()+1, packageStub.length()-1, *pErrorCode);
608
609 if (!suffix.empty()) /* tack on suffix */
610 {
611 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 pathBuffer.ensureEndsWithFileSeparator(*pErrorCode);
615 }
616 pathBuffer.append(suffix, *pErrorCode);
617 }
618 }
619
620#ifdef UDATA_DEBUG
621 fprintf(stderrstderr, " --> %s\n", pathBuffer.data());
622#endif
623
624 return pathBuffer.data();
625
626 } while(path);
627
628 /* fell way off the end */
629 return nullptr;
630}
631
632U_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.
646extern "C" const DataHeader U_DATA_API U_ICUDATA_ENTRY_POINTicudt77_dat;
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/*
655extern const void *uprv_getICUData_collation() ATTRIBUTE_WEAK;
656extern const void *uprv_getICUData_conversion() 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 *----------------------------------------------------------------------*/
669static UDataMemory *
670openCommonData(const char *path, /* Path from OpenChoice? */
671 int32_t commonDataIndex, /* ICU Data (index >= 0) if path == nullptr */
672 UErrorCode *pErrorCode)
673{
674 UDataMemory tData;
675 const char *pathBuffer;
676 const char *inBasename;
677
678 if (U_FAILURE(*pErrorCode)) {
679 return nullptr;
680 }
681
682 UDataMemory_initUDataMemory_init_77(&tData);
683
684 /* ??????? TODO revisit this */
685 if (commonDataIndex >= 0) {
686 /* "mini-cache" for common ICU data */
687 if(commonDataIndex >= UPRV_LENGTHOF(gCommonICUDataArray)(int32_t)(sizeof(gCommonICUDataArray)/sizeof((gCommonICUDataArray
)[0]))
) {
688 return nullptr;
689 }
690 {
691 Mutex lock;
692 if(gCommonICUDataArray[commonDataIndex] != nullptr) {
693 return gCommonICUDataArray[commonDataIndex];
694 }
695#if !defined(ICU_DATA_DIR_WINDOWS)
696// When using the Windows system data, we expect only a single data file.
697 int32_t i;
698 for(i = 0; i < commonDataIndex; ++i) {
699 if(gCommonICUDataArray[i]->pHeader == &U_ICUDATA_ENTRY_POINTicudt77_dat) {
700 /* The linked-in data is already in the list. */
701 return nullptr;
702 }
703 }
704#endif
705 }
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#if !defined(ICU_DATA_DIR_WINDOWS)
721// When using the Windows system data, we expect only a single data file.
722 setCommonICUDataPointer(&U_ICUDATA_ENTRY_POINTicudt77_dat, false, pErrorCode);
723 {
724 Mutex lock;
725 return gCommonICUDataArray[commonDataIndex];
726 }
727#endif
728 }
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 inBasename = findBasename(path);
736#ifdef UDATA_DEBUG
737 fprintf(stderrstderr, "inBasename = %s\n", inBasename);
738#endif
739
740 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(stderrstderr, "ocd: no basename in %s, bailing.\n", path);
745#endif
746 if (U_SUCCESS(*pErrorCode)) {
747 *pErrorCode=U_FILE_ACCESS_ERROR;
748 }
749 return nullptr;
750 }
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 UDataMemory *dataToReturn = udata_findCachedData(inBasename, *pErrorCode);
756 if (dataToReturn != nullptr || U_FAILURE(*pErrorCode)) {
757 return dataToReturn;
758 }
759
760 /* Requested item is not in the cache.
761 * Hunt it down, trying all the path locations
762 */
763
764 UDataPathIterator iter(u_getDataDirectoryu_getDataDirectory_77(), inBasename, path, ".dat", true, pErrorCode);
765
766 while ((UDataMemory_isLoadedUDataMemory_isLoaded_77(&tData)==false) && (pathBuffer = iter.next(pErrorCode)) != nullptr)
767 {
768#ifdef UDATA_DEBUG
769 fprintf(stderrstderr, "ocd: trying path %s - ", pathBuffer);
770#endif
771 uprv_mapFileuprv_mapFile_77(&tData, pathBuffer, pErrorCode);
772#ifdef UDATA_DEBUG
773 fprintf(stderrstderr, "%s\n", UDataMemory_isLoadedUDataMemory_isLoaded_77(&tData)?"LOADED":"not loaded");
774#endif
775 }
776 if (U_FAILURE(*pErrorCode)) {
777 return nullptr;
778 }
779
780 if (U_FAILURE(*pErrorCode)) {
781 return nullptr;
782 }
783 if (!UDataMemory_isLoadedUDataMemory_isLoaded_77(&tData)) {
784 /* no common data */
785 *pErrorCode=U_FILE_ACCESS_ERROR;
786 return nullptr;
787 }
788
789 /* we have mapped a file, check its header */
790 udata_checkCommonDataudata_checkCommonData_77(&tData, pErrorCode);
791
792
793 /* Cache the UDataMemory struct for this .dat file,
794 * so we won't need to hunt it down and map it again next time
795 * something is needed from it. */
796 return udata_cacheDataItem(inBasename, &tData, pErrorCode);
797}
798
799
800/*----------------------------------------------------------------------*
801 * *
802 * extendICUData If the full set of ICU data was not loaded at *
803 * program startup, load it now. This function will *
804 * be called when the lookup of an ICU data item in *
805 * the common ICU data fails. *
806 * *
807 * return true if new data is loaded, false otherwise.*
808 * *
809 *----------------------------------------------------------------------*/
810static UBool extendICUData(UErrorCode *pErr)
811{
812 UDataMemory *pData;
813 UDataMemory copyPData;
814 UBool didUpdate = false;
815
816 /*
817 * There is a chance for a race condition here.
818 * Normally, ICU data is loaded from a DLL or via mmap() and
819 * setCommonICUData() will detect if the same address is set twice.
820 * If ICU is built with data loading via fread() then the address will
821 * be different each time the common data is loaded and we may add
822 * multiple copies of the data.
823 * In this case, use a mutex to prevent the race.
824 * Use a specific mutex to avoid nested locks of the global mutex.
825 */
826#if MAP_IMPLEMENTATION2==MAP_STDIO3
827 static UMutex extendICUDataMutex;
828 umtx_lockumtx_lock_77(&extendICUDataMutex);
829#endif
830 if(!umtx_loadAcquire(gHaveTriedToLoadCommonData)) {
831 /* See if we can explicitly open a .dat file for the ICUData. */
832 pData = openCommonData(
833 U_ICUDATA_NAME"icudt" "77" "l", /* "icudt20l" , for example. */
834 -1, /* Pretend we're not opening ICUData */
835 pErr);
836
837 /* How about if there is no pData, eh... */
838
839 UDataMemory_initUDataMemory_init_77(&copyPData);
840 if(pData != nullptr) {
841 UDatamemory_assignUDatamemory_assign_77(&copyPData, pData);
842 copyPData.map = nullptr; /* The mapping for this data is owned by the hash table */
843 copyPData.mapAddr = nullptr; /* which will unmap it when ICU is shut down. */
844 /* CommonICUData is also unmapped when ICU is shut down.*/
845 /* To avoid unmapping the data twice, zero out the map */
846 /* fields in the UDataMemory that we're assigning */
847 /* to CommonICUData. */
848
849 didUpdate = /* no longer using this result */
Value stored to 'didUpdate' is never read
850 setCommonICUData(&copyPData,/* The new common data. */
851 false, /* No warnings if write didn't happen */
852 pErr); /* setCommonICUData honors errors; NOP if error set */
853 }
854
855 umtx_storeRelease(gHaveTriedToLoadCommonData, 1);
856 }
857
858 didUpdate = findCommonICUDataByName(U_ICUDATA_NAME"icudt" "77" "l", *pErr); /* Return 'true' when a racing writes out the extended */
859 /* data after another thread has failed to see it (in openCommonData), so */
860 /* extended data can be examined. */
861 /* Also handles a race through here before gHaveTriedToLoadCommonData is set. */
862
863#if MAP_IMPLEMENTATION2==MAP_STDIO3
864 umtx_unlockumtx_unlock_77(&extendICUDataMutex);
865#endif
866 return didUpdate; /* Return true if ICUData pointer was updated. */
867 /* (Could potentially have been done by another thread racing */
868 /* us through here, but that's fine, we still return true */
869 /* so that current thread will also examine extended data. */
870}
871
872/*----------------------------------------------------------------------*
873 * *
874 * udata_setCommonData *
875 * *
876 *----------------------------------------------------------------------*/
877U_CAPIextern "C" void U_EXPORT2
878udata_setCommonDataudata_setCommonData_77(const void *data, UErrorCode *pErrorCode) {
879 UDataMemory dataMemory;
880
881 if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
882 return;
883 }
884
885 if(data==nullptr) {
886 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
887 return;
888 }
889
890 /* set the data pointer and test for validity */
891 UDataMemory_initUDataMemory_init_77(&dataMemory);
892 UDataMemory_setDataUDataMemory_setData_77(&dataMemory, data);
893 udata_checkCommonDataudata_checkCommonData_77(&dataMemory, pErrorCode);
894 if (U_FAILURE(*pErrorCode)) {return;}
895
896 /* we have good data */
897 /* Set it up as the ICU Common Data. */
898 setCommonICUData(&dataMemory, true, pErrorCode);
899}
900
901/*---------------------------------------------------------------------------
902 *
903 * udata_setAppData
904 *
905 *---------------------------------------------------------------------------- */
906U_CAPIextern "C" void U_EXPORT2
907udata_setAppDataudata_setAppData_77(const char *path, const void *data, UErrorCode *err)
908{
909 UDataMemory udm;
910
911 if(err==nullptr || U_FAILURE(*err)) {
912 return;
913 }
914 if(data==nullptr) {
915 *err=U_ILLEGAL_ARGUMENT_ERROR;
916 return;
917 }
918
919 UDataMemory_initUDataMemory_init_77(&udm);
920 UDataMemory_setDataUDataMemory_setData_77(&udm, data);
921 udata_checkCommonDataudata_checkCommonData_77(&udm, err);
922 udata_cacheDataItem(path, &udm, err);
923}
924
925/*----------------------------------------------------------------------------*
926 * *
927 * checkDataItem Given a freshly located/loaded data item, either *
928 * an entry in a common file or a separately loaded file, *
929 * sanity check its header, and see if the data is *
930 * acceptable to the app. *
931 * If the data is good, create and return a UDataMemory *
932 * object that can be returned to the application. *
933 * Return nullptr on any sort of failure. *
934 * *
935 *----------------------------------------------------------------------------*/
936static UDataMemory *
937checkDataItem
938(
939 const DataHeader *pHeader, /* The data item to be checked. */
940 UDataMemoryIsAcceptable *isAcceptable, /* App's call-back function */
941 void *context, /* pass-thru param for above. */
942 const char *type, /* pass-thru param for above. */
943 const char *name, /* pass-thru param for above. */
944 UErrorCode *nonFatalErr, /* Error code if this data was not acceptable */
945 /* but openChoice should continue with */
946 /* trying to get data from fallback path. */
947 UErrorCode *fatalErr /* Bad error, caller should return immediately */
948 )
949{
950 UDataMemory *rDataMem = nullptr; /* the new UDataMemory, to be returned. */
951
952 if (U_FAILURE(*fatalErr)) {
953 return nullptr;
954 }
955
956 if(pHeader->dataHeader.magic1==0xda &&
957 pHeader->dataHeader.magic2==0x27 &&
958 (isAcceptable==nullptr || isAcceptable(context, type, name, &pHeader->info))
959 ) {
960 rDataMem=UDataMemory_createNewInstanceUDataMemory_createNewInstance_77(fatalErr);
961 if (U_FAILURE(*fatalErr)) {
962 return nullptr;
963 }
964 rDataMem->pHeader = pHeader;
965 } else {
966 /* the data is not acceptable, look further */
967 /* If we eventually find something good, this errorcode will be */
968 /* cleared out. */
969 *nonFatalErr=U_INVALID_FORMAT_ERROR;
970 }
971 return rDataMem;
972}
973
974/**
975 * @return 0 if not loaded, 1 if loaded or err
976 */
977static UDataMemory *doLoadFromIndividualFiles(const char *pkgName,
978 const char *dataPath, const char *tocEntryPathSuffix,
979 /* following arguments are the same as doOpenChoice itself */
980 const char *path, const char *type, const char *name,
981 UDataMemoryIsAcceptable *isAcceptable, void *context,
982 UErrorCode *subErrorCode,
983 UErrorCode *pErrorCode)
984{
985 const char *pathBuffer;
986 UDataMemory dataMemory;
987 UDataMemory *pEntryData;
988
989 /* look in ind. files: package\nam.typ ========================= */
990 /* init path iterator for individual files */
991 UDataPathIterator iter(dataPath, pkgName, path, tocEntryPathSuffix, false, pErrorCode);
992
993 while ((pathBuffer = iter.next(pErrorCode)) != nullptr)
994 {
995#ifdef UDATA_DEBUG
996 fprintf(stderrstderr, "UDATA: trying individual file %s\n", pathBuffer);
997#endif
998 if (uprv_mapFileuprv_mapFile_77(&dataMemory, pathBuffer, pErrorCode))
999 {
1000 pEntryData = checkDataItem(dataMemory.pHeader, isAcceptable, context, type, name, subErrorCode, pErrorCode);
1001 if (pEntryData != nullptr) {
1002 /* Data is good.
1003 * Hand off ownership of the backing memory to the user's UDataMemory.
1004 * and return it. */
1005 pEntryData->mapAddr = dataMemory.mapAddr;
1006 pEntryData->map = dataMemory.map;
1007
1008#ifdef UDATA_DEBUG
1009 fprintf(stderrstderr, "** Mapped file: %s\n", pathBuffer);
1010#endif
1011 return pEntryData;
1012 }
1013
1014 /* the data is not acceptable, or some error occurred. Either way, unmap the memory */
1015 udata_closeudata_close_77(&dataMemory);
1016
1017 /* If we had a nasty error, bail out completely. */
1018 if (U_FAILURE(*pErrorCode)) {
1019 return nullptr;
1020 }
1021
1022 /* Otherwise remember that we found data but didn't like it for some reason */
1023 *subErrorCode=U_INVALID_FORMAT_ERROR;
1024 }
1025#ifdef UDATA_DEBUG
1026 fprintf(stderrstderr, "%s\n", UDataMemory_isLoadedUDataMemory_isLoaded_77(&dataMemory)?"LOADED":"not loaded");
1027#endif
1028 }
1029 return nullptr;
1030}
1031
1032/**
1033 * @return 0 if not loaded, 1 if loaded or err
1034 */
1035static UDataMemory *doLoadFromCommonData(UBool isICUData, const char * /*pkgName*/,
1036 const char * /*dataPath*/, const char * /*tocEntryPathSuffix*/, const char *tocEntryName,
1037 /* following arguments are the same as doOpenChoice itself */
1038 const char *path, const char *type, const char *name,
1039 UDataMemoryIsAcceptable *isAcceptable, void *context,
1040 UErrorCode *subErrorCode,
1041 UErrorCode *pErrorCode)
1042{
1043 UDataMemory *pEntryData;
1044 const DataHeader *pHeader;
1045 UDataMemory *pCommonData;
1046 int32_t commonDataIndex;
1047 UBool checkedExtendedICUData = false;
1048 /* try to get common data. The loop is for platforms such as the 390 that do
1049 * not initially load the full set of ICU data. If the lookup of an ICU data item
1050 * fails, the full (but slower to load) set is loaded, the and the loop repeats,
1051 * trying the lookup again. Once the full set of ICU data is loaded, the loop wont
1052 * repeat because the full set will be checked the first time through.
1053 *
1054 * The loop also handles the fallback to a .dat file if the application linked
1055 * to the stub data library rather than a real library.
1056 */
1057 for (commonDataIndex = isICUData ? 0 : -1;;) {
1058 pCommonData=openCommonData(path, commonDataIndex, subErrorCode); /** search for pkg **/
1059
1060 if(U_SUCCESS(*subErrorCode) && pCommonData!=nullptr) {
1061 int32_t length;
1062
1063 /* look up the data piece in the common data */
1064 pHeader=pCommonData->vFuncs->Lookup(pCommonData, tocEntryName, &length, subErrorCode);
1065#ifdef UDATA_DEBUG
1066 fprintf(stderrstderr, "%s: pHeader=%p - %s\n", tocEntryName, (void*) pHeader, u_errorNameu_errorName_77(*subErrorCode));
1067#endif
1068
1069 if(pHeader!=nullptr) {
1070 pEntryData = checkDataItem(pHeader, isAcceptable, context, type, name, subErrorCode, pErrorCode);
1071#ifdef UDATA_DEBUG
1072 fprintf(stderrstderr, "pEntryData=%p\n", (void*) pEntryData);
1073#endif
1074 if (U_FAILURE(*pErrorCode)) {
1075 return nullptr;
1076 }
1077 if (pEntryData != nullptr) {
1078 pEntryData->length = length;
1079 return pEntryData;
1080 }
1081 }
1082 }
1083 // If we failed due to being out-of-memory, then stop early and report the error.
1084 if (*subErrorCode == U_MEMORY_ALLOCATION_ERROR) {
1085 *pErrorCode = *subErrorCode;
1086 return nullptr;
1087 }
1088 /* Data wasn't found. If we were looking for an ICUData item and there is
1089 * more data available, load it and try again,
1090 * otherwise break out of this loop. */
1091 if (!isICUData) {
1092 return nullptr;
1093 } else if (pCommonData != nullptr) {
1094 ++commonDataIndex; /* try the next data package */
1095 } else if ((!checkedExtendedICUData) && extendICUData(subErrorCode)) {
1096 checkedExtendedICUData = true;
1097 /* try this data package slot again: it changed from nullptr to non-nullptr */
1098 } else {
1099 return nullptr;
1100 }
1101 }
1102}
1103
1104/*
1105 * Identify the Time Zone resources that are subject to special override data loading.
1106 */
1107static UBool isTimeZoneFile(const char *name, const char *type) {
1108 return ((uprv_strcmp(type, "res"):: strcmp(type, "res") == 0) &&
1109 (uprv_strcmp(name, "zoneinfo64"):: strcmp(name, "zoneinfo64") == 0 ||
1110 uprv_strcmp(name, "timezoneTypes"):: strcmp(name, "timezoneTypes") == 0 ||
1111 uprv_strcmp(name, "windowsZones"):: strcmp(name, "windowsZones") == 0 ||
1112 uprv_strcmp(name, "metaZones"):: strcmp(name, "metaZones") == 0));
1113}
1114
1115/*
1116 * A note on the ownership of Mapped Memory
1117 *
1118 * For common format files, ownership resides with the UDataMemory object
1119 * that lives in the cache of opened common data. These UDataMemorys are private
1120 * to the udata implementation, and are never seen directly by users.
1121 *
1122 * The UDataMemory objects returned to users will have the address of some desired
1123 * data within the mapped region, but they wont have the mapping info itself, and thus
1124 * won't cause anything to be removed from memory when they are closed.
1125 *
1126 * For individual data files, the UDataMemory returned to the user holds the
1127 * information necessary to unmap the data on close. If the user independently
1128 * opens the same data file twice, two completely independent mappings will be made.
1129 * (There is no cache of opened data items from individual files, only a cache of
1130 * opened Common Data files, that is, files containing a collection of data items.)
1131 *
1132 * For common data passed in from the user via udata_setAppData() or
1133 * udata_setCommonData(), ownership remains with the user.
1134 *
1135 * UDataMemory objects themselves, as opposed to the memory they describe,
1136 * can be anywhere - heap, stack/local or global.
1137 * They have a flag to indicate when they're heap allocated and thus
1138 * must be deleted when closed.
1139 */
1140
1141
1142/*----------------------------------------------------------------------------*
1143 * *
1144 * main data loading functions *
1145 * *
1146 *----------------------------------------------------------------------------*/
1147static UDataMemory *
1148doOpenChoice(const char *path, const char *type, const char *name,
1149 UDataMemoryIsAcceptable *isAcceptable, void *context,
1150 UErrorCode *pErrorCode)
1151{
1152 UDataMemory *retVal = nullptr;
1153
1154 const char *dataPath;
1155
1156 int32_t tocEntrySuffixIndex;
1157 const char *tocEntryPathSuffix;
1158 UErrorCode subErrorCode=U_ZERO_ERROR;
1159 const char *treeChar;
1160
1161 UBool isICUData = false;
1162
1163
1164 FileTracer::traceOpen(path, type, name);
1165
1166
1167 /* Is this path ICU data? */
1168 if(path == nullptr ||
1169 !strcmp(path, U_ICUDATA_ALIAS"ICUDATA") || /* "ICUDATA" */
1170 !uprv_strncmp(path, U_ICUDATA_NAME U_TREE_SEPARATOR_STRING, /* "icudt26e-" */:: strncmp(path, "icudt" "77" "l" "-", :: strlen("icudt" "77"
"l" "-"))
1171 uprv_strlen(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING)):: strncmp(path, "icudt" "77" "l" "-", :: strlen("icudt" "77"
"l" "-"))
||
1172 !uprv_strncmp(path, U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING, /* "ICUDATA-" */:: strncmp(path, "ICUDATA" "-", :: strlen("ICUDATA" "-"))
1173 uprv_strlen(U_ICUDATA_ALIAS U_TREE_SEPARATOR_STRING)):: strncmp(path, "ICUDATA" "-", :: strlen("ICUDATA" "-"))) {
1174 isICUData = true;
1175 }
1176
1177#if (U_FILE_SEP_CHAR'/' != U_FILE_ALT_SEP_CHAR'/') /* Windows: try "foo\bar" and "foo/bar" */
1178 /* remap from alternate path char to the main one */
1179 CharString altSepPath;
1180 if(path) {
1181 if(uprv_strchr(path,U_FILE_ALT_SEP_CHAR):: strchr(path, '/') != nullptr) {
1182 altSepPath.append(path, *pErrorCode);
1183 char *p;
1184 while ((p = uprv_strchr(altSepPath.data(), U_FILE_ALT_SEP_CHAR):: strchr(altSepPath.data(), '/')) != nullptr) {
1185 *p = U_FILE_SEP_CHAR'/';
1186 }
1187#if defined (UDATA_DEBUG)
1188 fprintf(stderrstderr, "Changed path from [%s] to [%s]\n", path, altSepPath.data());
1189#endif
1190 path = altSepPath.data();
1191 }
1192 }
1193#endif
1194
1195 CharString tocEntryName; /* entry name in tree format. ex: 'icudt28b/coll/ar.res' */
1196 CharString tocEntryPath; /* entry name in path format. ex: 'icudt28b\\coll\\ar.res' */
1197
1198 CharString pkgName;
1199 CharString treeName;
1200
1201 /* ======= Set up strings */
1202 if(path==nullptr) {
1203 pkgName.append(U_ICUDATA_NAME"icudt" "77" "l", *pErrorCode);
1204 } else {
1205 const char *pkg;
1206 const char *first;
1207 pkg = uprv_strrchr(path, U_FILE_SEP_CHAR):: strrchr(path, '/');
1208 first = uprv_strchr(path, U_FILE_SEP_CHAR):: strchr(path, '/');
1209 if(uprv_pathIsAbsoluteuprv_pathIsAbsolute_77(path) || (pkg != first)) { /* more than one slash in the path- not a tree name */
1210 /* see if this is an /absolute/path/to/package path */
1211 if(pkg) {
1212 pkgName.append(pkg+1, *pErrorCode);
1213 } else {
1214 pkgName.append(path, *pErrorCode);
1215 }
1216 } else {
1217 treeChar = uprv_strchr(path, U_TREE_SEPARATOR):: strchr(path, '-');
1218 if(treeChar) {
1219 treeName.append(treeChar+1, *pErrorCode); /* following '-' */
1220 if(isICUData) {
1221 pkgName.append(U_ICUDATA_NAME"icudt" "77" "l", *pErrorCode);
1222 } else {
1223 pkgName.append(path, static_cast<int32_t>(treeChar - path), *pErrorCode);
1224 if (first == nullptr) {
1225 /*
1226 This user data has no path, but there is a tree name.
1227 Look up the correct path from the data cache later.
1228 */
1229 path = pkgName.data();
1230 }
1231 }
1232 } else {
1233 if(isICUData) {
1234 pkgName.append(U_ICUDATA_NAME"icudt" "77" "l", *pErrorCode);
1235 } else {
1236 pkgName.append(path, *pErrorCode);
1237 }
1238 }
1239 }
1240 }
1241
1242#ifdef UDATA_DEBUG
1243 fprintf(stderrstderr, " P=%s T=%s\n", pkgName.data(), treeName.data());
1244#endif
1245
1246 /* setting up the entry name and file name
1247 * Make up a full name by appending the type to the supplied
1248 * name, assuming that a type was supplied.
1249 */
1250
1251 /* prepend the package */
1252 tocEntryName.append(pkgName, *pErrorCode);
1253 tocEntryPath.append(pkgName, *pErrorCode);
1254 tocEntrySuffixIndex = tocEntryName.length();
1255
1256 if(!treeName.isEmpty()) {
1257 tocEntryName.append(U_TREE_ENTRY_SEP_CHAR'/', *pErrorCode).append(treeName, *pErrorCode);
1258 tocEntryPath.append(U_FILE_SEP_CHAR'/', *pErrorCode).append(treeName, *pErrorCode);
1259 }
1260
1261 tocEntryName.append(U_TREE_ENTRY_SEP_CHAR'/', *pErrorCode).append(name, *pErrorCode);
1262 tocEntryPath.append(U_FILE_SEP_CHAR'/', *pErrorCode).append(name, *pErrorCode);
1263 if(type!=nullptr && *type!=0) {
1264 tocEntryName.append(".", *pErrorCode).append(type, *pErrorCode);
1265 tocEntryPath.append(".", *pErrorCode).append(type, *pErrorCode);
1266 }
1267 // The +1 is for the U_FILE_SEP_CHAR that is always appended above.
1268 tocEntryPathSuffix = tocEntryPath.data() + tocEntrySuffixIndex + 1; /* suffix starts here */
1269
1270#ifdef UDATA_DEBUG
1271 fprintf(stderrstderr, " tocEntryName = %s\n", tocEntryName.data());
1272 fprintf(stderrstderr, " tocEntryPath = %s\n", tocEntryName.data());
1273#endif
1274
1275#if !defined(ICU_DATA_DIR_WINDOWS)
1276 if(path == nullptr) {
1277 path = COMMON_DATA_NAME"icudt" "77" "l"; /* "icudt26e" */
1278 }
1279#else
1280 // When using the Windows system data, we expects only a single data file.
1281 path = COMMON_DATA_NAME"icudt" "77" "l"; /* "icudt26e" */
1282#endif
1283
1284 /************************ Begin loop looking for ind. files ***************/
1285#ifdef UDATA_DEBUG
1286 fprintf(stderrstderr, "IND: inBasename = %s, pkg=%s\n", "(n/a)", packageNameFromPath(path));
1287#endif
1288
1289 /* End of dealing with a null basename */
1290 dataPath = u_getDataDirectoryu_getDataDirectory_77();
1291
1292 /**** Time zone individual files override */
1293 if (isICUData && isTimeZoneFile(name, type)) {
1294 const char *tzFilesDir = u_getTimeZoneFilesDirectoryu_getTimeZoneFilesDirectory_77(pErrorCode);
1295 if (tzFilesDir[0] != 0) {
1296#ifdef UDATA_DEBUG
1297 fprintf(stderrstderr, "Trying Time Zone Files directory = %s\n", tzFilesDir);
1298#endif
1299 retVal = doLoadFromIndividualFiles(/* pkgName.data() */ "", tzFilesDir, tocEntryPathSuffix,
1300 /* path */ "", type, name, isAcceptable, context, &subErrorCode, pErrorCode);
1301 if((retVal != nullptr) || U_FAILURE(*pErrorCode)) {
1302 return retVal;
1303 }
1304 }
1305 }
1306
1307 /**** COMMON PACKAGE - only if packages are first. */
1308 if(gDataFileAccess == UDATA_PACKAGES_FIRST) {
1309#ifdef UDATA_DEBUG
1310 fprintf(stderrstderr, "Trying packages (UDATA_PACKAGES_FIRST)\n");
1311#endif
1312 /* #2 */
1313 retVal = doLoadFromCommonData(isICUData,
1314 pkgName.data(), dataPath, tocEntryPathSuffix, tocEntryName.data(),
1315 path, type, name, isAcceptable, context, &subErrorCode, pErrorCode);
1316 if((retVal != nullptr) || U_FAILURE(*pErrorCode)) {
1317 return retVal;
1318 }
1319 }
1320
1321 /**** INDIVIDUAL FILES */
1322 if((gDataFileAccess==UDATA_PACKAGES_FIRST) ||
1323 (gDataFileAccess==UDATA_FILES_FIRST)) {
1324#ifdef UDATA_DEBUG
1325 fprintf(stderrstderr, "Trying individual files\n");
1326#endif
1327 /* Check to make sure that there is a dataPath to iterate over */
1328 if ((dataPath && *dataPath) || !isICUData) {
1329 retVal = doLoadFromIndividualFiles(pkgName.data(), dataPath, tocEntryPathSuffix,
1330 path, type, name, isAcceptable, context, &subErrorCode, pErrorCode);
1331 if((retVal != nullptr) || U_FAILURE(*pErrorCode)) {
1332 return retVal;
1333 }
1334 }
1335 }
1336
1337 /**** COMMON PACKAGE */
1338 if((gDataFileAccess==UDATA_ONLY_PACKAGES) ||
1339 (gDataFileAccess==UDATA_FILES_FIRST)) {
1340#ifdef UDATA_DEBUG
1341 fprintf(stderrstderr, "Trying packages (UDATA_ONLY_PACKAGES || UDATA_FILES_FIRST)\n");
1342#endif
1343 retVal = doLoadFromCommonData(isICUData,
1344 pkgName.data(), dataPath, tocEntryPathSuffix, tocEntryName.data(),
1345 path, type, name, isAcceptable, context, &subErrorCode, pErrorCode);
1346 if((retVal != nullptr) || U_FAILURE(*pErrorCode)) {
1347 return retVal;
1348 }
1349 }
1350
1351 /* Load from DLL. If we haven't attempted package load, we also haven't had any chance to
1352 try a DLL (static or setCommonData/etc) load.
1353 If we ever have a "UDATA_ONLY_FILES", add it to the or list here. */
1354 if(gDataFileAccess==UDATA_NO_FILES) {
1355#ifdef UDATA_DEBUG
1356 fprintf(stderrstderr, "Trying common data (UDATA_NO_FILES)\n");
1357#endif
1358 retVal = doLoadFromCommonData(isICUData,
1359 pkgName.data(), "", tocEntryPathSuffix, tocEntryName.data(),
1360 path, type, name, isAcceptable, context, &subErrorCode, pErrorCode);
1361 if((retVal != nullptr) || U_FAILURE(*pErrorCode)) {
1362 return retVal;
1363 }
1364 }
1365
1366 /* data not found */
1367 if(U_SUCCESS(*pErrorCode)) {
1368 if(U_SUCCESS(subErrorCode)) {
1369 /* file not found */
1370 *pErrorCode=U_FILE_ACCESS_ERROR;
1371 } else {
1372 /* entry point not found or rejected */
1373 *pErrorCode=subErrorCode;
1374 }
1375 }
1376 return retVal;
1377}
1378
1379
1380
1381/* API ---------------------------------------------------------------------- */
1382
1383U_CAPIextern "C" UDataMemory * U_EXPORT2
1384udata_openudata_open_77(const char *path, const char *type, const char *name,
1385 UErrorCode *pErrorCode) {
1386#ifdef UDATA_DEBUG
1387 fprintf(stderrstderr, "udata_open(): Opening: %s : %s . %s\n", (path?path:"nullptr"), name, type);
1388 fflush(stderrstderr);
1389#endif
1390
1391 if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
1392 return nullptr;
1393 } else if(name==nullptr || *name==0) {
1394 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1395 return nullptr;
1396 } else {
1397 return doOpenChoice(path, type, name, nullptr, nullptr, pErrorCode);
1398 }
1399}
1400
1401
1402
1403U_CAPIextern "C" UDataMemory * U_EXPORT2
1404udata_openChoiceudata_openChoice_77(const char *path, const char *type, const char *name,
1405 UDataMemoryIsAcceptable *isAcceptable, void *context,
1406 UErrorCode *pErrorCode) {
1407#ifdef UDATA_DEBUG
1408 fprintf(stderrstderr, "udata_openChoice(): Opening: %s : %s . %s\n", (path?path:"nullptr"), name, type);
1409#endif
1410
1411 if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
1412 return nullptr;
1413 } else if(name==nullptr || *name==0 || isAcceptable==nullptr) {
1414 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1415 return nullptr;
1416 } else {
1417 return doOpenChoice(path, type, name, isAcceptable, context, pErrorCode);
1418 }
1419}
1420
1421
1422
1423U_CAPIextern "C" void U_EXPORT2
1424udata_getInfoudata_getInfo_77(UDataMemory *pData, UDataInfo *pInfo) {
1425 if(pInfo!=nullptr) {
1426 if(pData!=nullptr && pData->pHeader!=nullptr) {
1427 const UDataInfo *info=&pData->pHeader->info;
1428 uint16_t dataInfoSize=udata_getInfoSizeudata_getInfoSize_77(info);
1429 if(pInfo->size>dataInfoSize) {
1430 pInfo->size=dataInfoSize;
1431 }
1432 uprv_memcpy((uint16_t *)pInfo+1, (const uint16_t *)info+1, pInfo->size-2)do { clang diagnostic push clang diagnostic ignored "-Waddress"
(static_cast <bool> ((uint16_t *)pInfo+1 != __null) ?
void (0) : __assert_fail ("(uint16_t *)pInfo+1 != __null", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__)); (
static_cast <bool> ((const uint16_t *)info+1 != __null)
? void (0) : __assert_fail ("(const uint16_t *)info+1 != __null"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); clang diagnostic pop :: memcpy((uint16_t *)pInfo+1, (const
uint16_t *)info+1, pInfo->size-2); } while (false)
;
1433 if(info->isBigEndian!=U_IS_BIG_ENDIAN(1234 == 4321)) {
1434 /* opposite endianness */
1435 uint16_t x=info->reservedWord;
1436 pInfo->reservedWord=(uint16_t)((x<<8)|(x>>8));
1437 }
1438 } else {
1439 pInfo->size=0;
1440 }
1441 }
1442}
1443
1444
1445U_CAPIextern "C" void U_EXPORT2 udata_setFileAccessudata_setFileAccess_77(UDataFileAccess access, UErrorCode * /*status*/)
1446{
1447 // Note: this function is documented as not thread safe.
1448 gDataFileAccess = access;
1449}