Debian Codegraph @

intl/icu/source/common/uloc_keytype.cpp

raw
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 **********************************************************************
5 * Copyright (C) 2014-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 */
9 #include <algorithm>
10 #include <optional>
11 #include <string_view>
12
13 #include "unicode/utypes.h"
14 #include "unicode/unistr.h"
15 #include "unicode/uobject.h"
16
17 #include "charstr.h"
18 #include "cmemory.h"
19 #include "cstring.h"
20 #include "uassert.h"
21 #include "ucln_cmn.h"
22 #include "uhash.h"
23 #include "ulocimp.h"
24 #include "umutex.h"
25 #include "uresimp.h"
26 #include "uvector.h"
27 #include "udataswp.h" /* for InvChar functions */
28
29 static UHashtable* gLocExtKeyMap = nullptr;
30 static icu::UInitOnce gLocExtKeyMapInitOnce {};
31
32 // bit flags for special types
33 typedef enum {
34 SPECIALTYPE_NONE = 0,
35 SPECIALTYPE_CODEPOINTS = 1,
36 SPECIALTYPE_REORDER_CODE = 2,
37 SPECIALTYPE_RG_KEY_VALUE = 4
38 } SpecialType;
39
40 struct LocExtKeyData : public icu::UMemory {
41 std::string_view legacyId;
42 std::string_view bcpId;
43 icu::LocalUHashtablePointer typeMap;
44 uint32_t specialTypes;
45 };
46
47 struct LocExtType : public icu::UMemory {
48 std::string_view legacyId;
49 std::string_view bcpId;
50 };
51
52 struct TypeAlias : public icu::UMemory {
53 std::string_view from;
54 };
55
56 static icu::MemoryPool<icu::CharString>* gKeyTypeStringPool = nullptr;
57 static icu::MemoryPool<LocExtKeyData>* gLocExtKeyDataEntries = nullptr;
58 static icu::MemoryPool<LocExtType>* gLocExtTypeEntries = nullptr;
59 static icu::MemoryPool<TypeAlias>* gTypeAliasEntries = nullptr;
60
61 U_CDECL_BEGIN
62
63 static UBool U_CALLCONV
64 uloc_key_type_cleanup() {
65 if (gLocExtKeyMap != nullptr) {
66 uhash_close(gLocExtKeyMap);
67 gLocExtKeyMap = nullptr;
68 }
69
70 delete gLocExtKeyDataEntries;
71 gLocExtKeyDataEntries = nullptr;
72
73 delete gLocExtTypeEntries;
74 gLocExtTypeEntries = nullptr;
75
76 delete gTypeAliasEntries;
77 gTypeAliasEntries = nullptr;
78
79 delete gKeyTypeStringPool;
80 gKeyTypeStringPool = nullptr;
81
82 gLocExtKeyMapInitOnce.reset();
83 return true;
84 }
85
86 U_CDECL_END
87
88 namespace {
89
90 void U_CALLCONV
91 initFromResourceBundle(UErrorCode& sts) {
92 U_NAMESPACE_USE
93 ucln_common_registerCleanup(UCLN_COMMON_LOCALE_KEY_TYPE, uloc_key_type_cleanup);
94
95 gLocExtKeyMap = uhash_open(uhash_hashIStringView, uhash_compareIStringView, nullptr, &sts);
96
97 LocalUResourceBundlePointer keyTypeDataRes(ures_openDirect(nullptr, "keyTypeData", &sts));
98 LocalUResourceBundlePointer keyMapRes(ures_getByKey(keyTypeDataRes.getAlias(), "keyMap", nullptr, &sts));
99 LocalUResourceBundlePointer typeMapRes(ures_getByKey(keyTypeDataRes.getAlias(), "typeMap", nullptr, &sts));
100
101 if (U_FAILURE(sts)) {
102 return;
103 }
104
105 UErrorCode tmpSts = U_ZERO_ERROR;
106 LocalUResourceBundlePointer typeAliasRes(ures_getByKey(keyTypeDataRes.getAlias(), "typeAlias", nullptr, &tmpSts));
107 tmpSts = U_ZERO_ERROR;
108 LocalUResourceBundlePointer bcpTypeAliasRes(ures_getByKey(keyTypeDataRes.getAlias(), "bcpTypeAlias", nullptr, &tmpSts));
109
110 // initialize pools storing dynamically allocated objects
111 gKeyTypeStringPool = new icu::MemoryPool<icu::CharString>;
112 if (gKeyTypeStringPool == nullptr) {
113 sts = U_MEMORY_ALLOCATION_ERROR;
114 return;
115 }
116 gLocExtKeyDataEntries = new icu::MemoryPool<LocExtKeyData>;
117 if (gLocExtKeyDataEntries == nullptr) {
118 sts = U_MEMORY_ALLOCATION_ERROR;
119 return;
120 }
121 gLocExtTypeEntries = new icu::MemoryPool<LocExtType>;
122 if (gLocExtTypeEntries == nullptr) {
123 sts = U_MEMORY_ALLOCATION_ERROR;
124 return;
125 }
126 gTypeAliasEntries = new icu::MemoryPool<TypeAlias>;
127 if (gTypeAliasEntries == nullptr) {
128 sts = U_MEMORY_ALLOCATION_ERROR;
129 return;
130 }
131
132 // iterate through keyMap resource
133 LocalUResourceBundlePointer keyMapEntry;
134
135 while (ures_hasNext(keyMapRes.getAlias())) {
136 keyMapEntry.adoptInstead(ures_getNextResource(keyMapRes.getAlias(), keyMapEntry.orphan(), &sts));
137 if (U_FAILURE(sts)) {
138 break;
139 }
140 const char* legacyKeyId = ures_getKey(keyMapEntry.getAlias());
141 UnicodeString uBcpKeyId = ures_getUnicodeString(keyMapEntry.getAlias(), &sts);
142 if (U_FAILURE(sts)) {
143 break;
144 }
145
146 // empty value indicates that BCP key is same with the legacy key.
147 const char* bcpKeyId = legacyKeyId;
148 if (!uBcpKeyId.isEmpty()) {
149 icu::CharString* bcpKeyIdBuf = gKeyTypeStringPool->create();
150 if (bcpKeyIdBuf == nullptr) {
151 sts = U_MEMORY_ALLOCATION_ERROR;
152 break;
153 }
154 bcpKeyIdBuf->appendInvariantChars(uBcpKeyId, sts);
155 if (U_FAILURE(sts)) {
156 break;
157 }
158 bcpKeyId = bcpKeyIdBuf->data();
159 }
160
161 bool isTZ = uprv_strcmp(legacyKeyId, "timezone") == 0;
162
163 UHashtable* typeDataMap = uhash_open(uhash_hashIStringView, uhash_compareIStringView, nullptr, &sts);
164 if (U_FAILURE(sts)) {
165 break;
166 }
167 uint32_t specialTypes = SPECIALTYPE_NONE;
168
169 LocalUResourceBundlePointer typeAliasResByKey;
170 LocalUResourceBundlePointer bcpTypeAliasResByKey;
171
172 if (typeAliasRes.isValid()) {
173 tmpSts = U_ZERO_ERROR;
174 typeAliasResByKey.adoptInstead(ures_getByKey(typeAliasRes.getAlias(), legacyKeyId, nullptr, &tmpSts));
175 if (U_FAILURE(tmpSts)) {
176 typeAliasResByKey.orphan();
177 }
178 }
179 if (bcpTypeAliasRes.isValid()) {
180 tmpSts = U_ZERO_ERROR;
181 bcpTypeAliasResByKey.adoptInstead(ures_getByKey(bcpTypeAliasRes.getAlias(), bcpKeyId, nullptr, &tmpSts));
182 if (U_FAILURE(tmpSts)) {
183 bcpTypeAliasResByKey.orphan();
184 }
185 }
186
187 // look up type map for the key, and walk through the mapping data
188 LocalUResourceBundlePointer typeMapResByKey(ures_getByKey(typeMapRes.getAlias(), legacyKeyId, nullptr, &sts));
189 if (U_FAILURE(sts)) {
190 // We fail here if typeMap does not have an entry corresponding to every entry in keyMap (should
191 // not happen for valid keyTypeData), or if ures_getByKeyfails fails for some other reason
192 // (e.g. data file cannot be loaded, using stubdata, over-aggressive data filtering has removed
193 // something like timezoneTypes.res, etc.). Error code is already set. See ICU-21669.
194 UPRV_UNREACHABLE_ASSERT;
195 } else {
196 LocalUResourceBundlePointer typeMapEntry;
197
198 while (ures_hasNext(typeMapResByKey.getAlias())) {
199 typeMapEntry.adoptInstead(ures_getNextResource(typeMapResByKey.getAlias(), typeMapEntry.orphan(), &sts));
200 if (U_FAILURE(sts)) {
201 break;
202 }
203 const char* legacyTypeId = ures_getKey(typeMapEntry.getAlias());
204
205 // special types
206 if (uprv_strcmp(legacyTypeId, "CODEPOINTS") == 0) {
207 specialTypes |= SPECIALTYPE_CODEPOINTS;
208 continue;
209 }
210 if (uprv_strcmp(legacyTypeId, "REORDER_CODE") == 0) {
211 specialTypes |= SPECIALTYPE_REORDER_CODE;
212 continue;
213 }
214 if (uprv_strcmp(legacyTypeId, "RG_KEY_VALUE") == 0) {
215 specialTypes |= SPECIALTYPE_RG_KEY_VALUE;
216 continue;
217 }
218
219 if (isTZ) {
220 // a timezone key uses a colon instead of a slash in the resource.
221 // e.g. America:Los_Angeles
222 if (uprv_strchr(legacyTypeId, ':') != nullptr) {
223 icu::CharString* legacyTypeIdBuf =
224 gKeyTypeStringPool->create(legacyTypeId, sts);
225 if (legacyTypeIdBuf == nullptr) {
226 sts = U_MEMORY_ALLOCATION_ERROR;
227 break;
228 }
229 if (U_FAILURE(sts)) {
230 break;
231 }
232 std::replace(
233 legacyTypeIdBuf->data(),
234 legacyTypeIdBuf->data() + legacyTypeIdBuf->length(),
235 ':', '/');
236 legacyTypeId = legacyTypeIdBuf->data();
237 }
238 }
239
240 UnicodeString uBcpTypeId = ures_getUnicodeString(typeMapEntry.getAlias(), &sts);
241 if (U_FAILURE(sts)) {
242 break;
243 }
244
245 // empty value indicates that BCP type is same with the legacy type.
246 const char* bcpTypeId = legacyTypeId;
247 if (!uBcpTypeId.isEmpty()) {
248 icu::CharString* bcpTypeIdBuf = gKeyTypeStringPool->create();
249 if (bcpTypeIdBuf == nullptr) {
250 sts = U_MEMORY_ALLOCATION_ERROR;
251 break;
252 }
253 bcpTypeIdBuf->appendInvariantChars(uBcpTypeId, sts);
254 if (U_FAILURE(sts)) {
255 break;
256 }
257 bcpTypeId = bcpTypeIdBuf->data();
258 }
259
260 // Note: legacy type value should never be
261 // equivalent to bcp type value of a different
262 // type under the same key. So we use a single
263 // map for lookup.
264 LocExtType* t = gLocExtTypeEntries->create();
265 if (t == nullptr) {
266 sts = U_MEMORY_ALLOCATION_ERROR;
267 break;
268 }
269 t->bcpId = bcpTypeId;
270 t->legacyId = legacyTypeId;
271
272 uhash_put(typeDataMap, &t->legacyId, t, &sts);
273 if (bcpTypeId != legacyTypeId) {
274 // different type value
275 uhash_put(typeDataMap, &t->bcpId, t, &sts);
276 }
277 if (U_FAILURE(sts)) {
278 break;
279 }
280
281 // also put aliases in the map
282 if (typeAliasResByKey.isValid()) {
283 LocalUResourceBundlePointer typeAliasDataEntry;
284
285 ures_resetIterator(typeAliasResByKey.getAlias());
286 while (ures_hasNext(typeAliasResByKey.getAlias()) && U_SUCCESS(sts)) {
287 int32_t toLen;
288 typeAliasDataEntry.adoptInstead(ures_getNextResource(typeAliasResByKey.getAlias(), typeAliasDataEntry.orphan(), &sts));
289 const char16_t* to = ures_getString(typeAliasDataEntry.getAlias(), &toLen, &sts);
290 if (U_FAILURE(sts)) {
291 break;
292 }
293 // check if this is an alias of canonical legacy type
294 if (uprv_compareInvWithUChar(
295 nullptr,
296 t->legacyId.data(),
297 static_cast<int32_t>(t->legacyId.size()),
298 to,
299 toLen) == 0) {
300 const char* from = ures_getKey(typeAliasDataEntry.getAlias());
301 TypeAlias* alias = gTypeAliasEntries->create(TypeAlias{{}, from});
302 if (isTZ) {
303 // replace colon with slash if necessary
304 if (uprv_strchr(from, ':') != nullptr) {
305 icu::CharString* fromBuf =
306 gKeyTypeStringPool->create(from, sts);
307 if (fromBuf == nullptr) {
308 sts = U_MEMORY_ALLOCATION_ERROR;
309 break;
310 }
311 if (U_FAILURE(sts)) {
312 break;
313 }
314 std::replace(
315 fromBuf->data(),
316 fromBuf->data() + fromBuf->length(),
317 ':', '/');
318 alias->from = fromBuf->toStringPiece();
319 }
320 }
321 uhash_put(typeDataMap, &alias->from, t, &sts);
322 }
323 }
324 if (U_FAILURE(sts)) {
325 break;
326 }
327 }
328
329 if (bcpTypeAliasResByKey.isValid()) {
330 LocalUResourceBundlePointer bcpTypeAliasDataEntry;
331
332 ures_resetIterator(bcpTypeAliasResByKey.getAlias());
333 while (ures_hasNext(bcpTypeAliasResByKey.getAlias()) && U_SUCCESS(sts)) {
334 int32_t toLen;
335 bcpTypeAliasDataEntry.adoptInstead(ures_getNextResource(bcpTypeAliasResByKey.getAlias(), bcpTypeAliasDataEntry.orphan(), &sts));
336 const char16_t* to = ures_getString(bcpTypeAliasDataEntry.getAlias(), &toLen, &sts);
337 if (U_FAILURE(sts)) {
338 break;
339 }
340 // check if this is an alias of bcp type
341 if (uprv_compareInvWithUChar(
342 nullptr,
343 t->bcpId.data(),
344 static_cast<int32_t>(t->bcpId.size()),
345 to,
346 toLen) == 0) {
347 const char* from = ures_getKey(bcpTypeAliasDataEntry.getAlias());
348 TypeAlias* alias = gTypeAliasEntries->create(TypeAlias{{}, from});
349 uhash_put(typeDataMap, &alias->from, t, &sts);
350 }
351 }
352 if (U_FAILURE(sts)) {
353 break;
354 }
355 }
356 }
357 }
358 if (U_FAILURE(sts)) {
359 break;
360 }
361
362 LocExtKeyData* keyData = gLocExtKeyDataEntries->create();
363 if (keyData == nullptr) {
364 sts = U_MEMORY_ALLOCATION_ERROR;
365 break;
366 }
367 keyData->bcpId = bcpKeyId;
368 keyData->legacyId = legacyKeyId;
369 keyData->specialTypes = specialTypes;
370 keyData->typeMap.adoptInstead(typeDataMap);
371
372 uhash_put(gLocExtKeyMap, &keyData->legacyId, keyData, &sts);
373 if (legacyKeyId != bcpKeyId) {
374 // different key value
375 uhash_put(gLocExtKeyMap, &keyData->bcpId, keyData, &sts);
376 }
377 if (U_FAILURE(sts)) {
378 break;
379 }
380 }
381 }
382
383 bool
384 init() {
385 UErrorCode sts = U_ZERO_ERROR;
386 umtx_initOnce(gLocExtKeyMapInitOnce, &initFromResourceBundle, sts);
387 if (U_FAILURE(sts)) {
388 return false;
389 }
390 return true;
391 }
392
393 bool
394 isSpecialTypeCodepoints(std::string_view val) {
395 int32_t subtagLen = 0;
396 for (char c : val) {
397 if (c == '-') {
398 if (subtagLen < 4 || subtagLen > 6) {
399 return false;
400 }
401 subtagLen = 0;
402 } else if ((c >= '0' && c <= '9') ||
403 (c >= 'A' && c <= 'F') || // A-F/a-f are contiguous
404 (c >= 'a' && c <= 'f')) { // also in EBCDIC
405 subtagLen++;
406 } else {
407 return false;
408 }
409 }
410 return subtagLen >= 4 && subtagLen <= 6;
411 }
412
413 bool
414 isSpecialTypeReorderCode(std::string_view val) {
415 int32_t subtagLen = 0;
416 for (char c : val) {
417 if (c == '-') {
418 if (subtagLen < 3 || subtagLen > 8) {
419 return false;
420 }
421 subtagLen = 0;
422 } else if (uprv_isASCIILetter(c)) {
423 subtagLen++;
424 } else {
425 return false;
426 }
427 }
428 return subtagLen >= 3 && subtagLen <= 8;
429 }
430
431 bool
432 isSpecialTypeRgKeyValue(std::string_view val) {
433 int32_t subtagLen = 0;
434 for (char c : val) {
435 if ((subtagLen < 2 && uprv_isASCIILetter(c)) ||
436 (subtagLen >= 2 && (c == 'Z' || c == 'z'))) {
437 subtagLen++;
438 } else {
439 return false;
440 }
441 }
442 return subtagLen == 6;
443 }
444
445 } // namespace
446
447 U_EXPORT std::optional<std::string_view>
448 ulocimp_toBcpKey(std::string_view key) {
449 if (!init()) {
450 return std::nullopt;
451 }
452
453 LocExtKeyData* keyData = static_cast<LocExtKeyData*>(uhash_get(gLocExtKeyMap, &key));
454 if (keyData != nullptr) {
455 return keyData->bcpId;
456 }
457
458 return std::nullopt;
459 }
460
461 U_EXPORT std::optional<std::string_view>
462 ulocimp_toLegacyKey(std::string_view key) {
463 if (!init()) {
464 return std::nullopt;
465 }
466
467 LocExtKeyData* keyData = static_cast<LocExtKeyData*>(uhash_get(gLocExtKeyMap, &key));
468 if (keyData != nullptr) {
469 return keyData->legacyId;
470 }
471
472 return std::nullopt;
473 }
474
475 U_EXPORT std::optional<std::string_view>
476 ulocimp_toBcpType(std::string_view key, std::string_view type) {
477 if (!init()) {
478 return std::nullopt;
479 }
480
481 LocExtKeyData* keyData = static_cast<LocExtKeyData*>(uhash_get(gLocExtKeyMap, &key));
482 if (keyData != nullptr) {
483 LocExtType* t = static_cast<LocExtType*>(uhash_get(keyData->typeMap.getAlias(), &type));
484 if (t != nullptr) {
485 return t->bcpId;
486 }
487 if (keyData->specialTypes != SPECIALTYPE_NONE) {
488 bool matched = false;
489 if (keyData->specialTypes & SPECIALTYPE_CODEPOINTS) {
490 matched = isSpecialTypeCodepoints(type);
491 }
492 if (!matched && keyData->specialTypes & SPECIALTYPE_REORDER_CODE) {
493 matched = isSpecialTypeReorderCode(type);
494 }
495 if (!matched && keyData->specialTypes & SPECIALTYPE_RG_KEY_VALUE) {
496 matched = isSpecialTypeRgKeyValue(type);
497 }
498 if (matched) {
499 return type;
500 }
501 }
502 }
503
504 return std::nullopt;
505 }
506
507
508 U_EXPORT std::optional<std::string_view>
509 ulocimp_toLegacyType(std::string_view key, std::string_view type) {
510 if (!init()) {
511 return std::nullopt;
512 }
513
514 LocExtKeyData* keyData = static_cast<LocExtKeyData*>(uhash_get(gLocExtKeyMap, &key));
515 if (keyData != nullptr) {
516 LocExtType* t = static_cast<LocExtType*>(uhash_get(keyData->typeMap.getAlias(), &type));
517 if (t != nullptr) {
518 return t->legacyId;
519 }
520 if (keyData->specialTypes != SPECIALTYPE_NONE) {
521 bool matched = false;
522 if (keyData->specialTypes & SPECIALTYPE_CODEPOINTS) {
523 matched = isSpecialTypeCodepoints(type);
524 }
525 if (!matched && keyData->specialTypes & SPECIALTYPE_REORDER_CODE) {
526 matched = isSpecialTypeReorderCode(type);
527 }
528 if (!matched && keyData->specialTypes & SPECIALTYPE_RG_KEY_VALUE) {
529 matched = isSpecialTypeRgKeyValue(type);
530 }
531 if (matched) {
532 return type;
533 }
534 }
535 }
536
537 return std::nullopt;
538 }