Coverage Report

Created: 2026-10-31 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/common/loadednormalizer2impl.cpp
Line
Count
Source
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, International Business Machines
6
* Corporation and others.  All Rights Reserved.
7
*******************************************************************************
8
* loadednormalizer2impl.cpp
9
*
10
* created on: 2014sep03
11
* created by: Markus W. Scherer
12
*/
13
14
#include "unicode/utypes.h"
15
16
#if !UCONFIG_NO_NORMALIZATION
17
18
#include "unicode/udata.h"
19
#include "unicode/localpointer.h"
20
#include "unicode/normalizer2.h"
21
#include "unicode/ucptrie.h"
22
#include "unicode/unistr.h"
23
#include "unicode/unorm.h"
24
#include "cstring.h"
25
#include "mutex.h"
26
#include "norm2allmodes.h"
27
#include "normalizer2impl.h"
28
#include "uassert.h"
29
#include "ucln_cmn.h"
30
#include "uhash.h"
31
32
U_NAMESPACE_BEGIN
33
34
class LoadedNormalizer2Impl : public Normalizer2Impl {
35
public:
36
9
    LoadedNormalizer2Impl() : memory(nullptr), ownedTrie(nullptr) {}
37
    virtual ~LoadedNormalizer2Impl();
38
39
    void load(const char *packageName, const char *name, UErrorCode &errorCode);
40
41
private:
42
    static UBool U_CALLCONV
43
    isAcceptable(void *context, const char *type, const char *name, const UDataInfo *pInfo);
44
45
    UDataMemory *memory;
46
    UCPTrie *ownedTrie;
47
};
48
49
0
LoadedNormalizer2Impl::~LoadedNormalizer2Impl() {
50
0
    udata_close(memory);
51
0
    ucptrie_close(ownedTrie);
52
0
}
53
54
UBool U_CALLCONV
55
LoadedNormalizer2Impl::isAcceptable(void * /*context*/,
56
                                    const char * /* type */, const char * /*name*/,
57
9
                                    const UDataInfo *pInfo) {
58
9
    if(
59
9
        pInfo->size>=20 &&
60
9
        pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
61
9
        pInfo->charsetFamily==U_CHARSET_FAMILY &&
62
9
        pInfo->dataFormat[0]==0x4e &&    /* dataFormat="Nrm2" */
63
9
        pInfo->dataFormat[1]==0x72 &&
64
9
        pInfo->dataFormat[2]==0x6d &&
65
9
        pInfo->dataFormat[3]==0x32 &&
66
9
        pInfo->formatVersion[0]==5
67
9
    ) {
68
        // Normalizer2Impl *me=(Normalizer2Impl *)context;
69
        // uprv_memcpy(me->dataVersion, pInfo->dataVersion, 4);
70
9
        return true;
71
9
    } else {
72
0
        return false;
73
0
    }
74
9
}
75
76
void
77
9
LoadedNormalizer2Impl::load(const char *packageName, const char *name, UErrorCode &errorCode) {
78
9
    if(U_FAILURE(errorCode)) {
79
0
        return;
80
0
    }
81
9
    memory=udata_openChoice(packageName, "nrm", name, isAcceptable, this, &errorCode);
82
9
    if(U_FAILURE(errorCode)) {
83
0
        return;
84
0
    }
85
9
    const uint8_t* inBytes = static_cast<const uint8_t*>(udata_getMemory(memory));
86
9
    const int32_t* inIndexes = reinterpret_cast<const int32_t*>(inBytes);
87
9
    int32_t indexesLength=inIndexes[IX_NORM_TRIE_OFFSET]/4;
88
9
    if(indexesLength<=IX_MIN_LCCC_CP) {
89
0
        errorCode=U_INVALID_FORMAT_ERROR;  // Not enough indexes.
90
0
        return;
91
0
    }
92
93
9
    int32_t offset=inIndexes[IX_NORM_TRIE_OFFSET];
94
9
    int32_t nextOffset=inIndexes[IX_EXTRA_DATA_OFFSET];
95
9
    ownedTrie=ucptrie_openFromBinary(UCPTRIE_TYPE_FAST, UCPTRIE_VALUE_BITS_16,
96
9
                                     inBytes+offset, nextOffset-offset, nullptr,
97
9
                                     &errorCode);
98
9
    if(U_FAILURE(errorCode)) {
99
0
        return;
100
0
    }
101
102
9
    offset=nextOffset;
103
9
    nextOffset=inIndexes[IX_SMALL_FCD_OFFSET];
104
9
    const uint16_t* inExtraData = reinterpret_cast<const uint16_t*>(inBytes + offset);
105
106
    // smallFCD: new in formatVersion 2
107
9
    offset=nextOffset;
108
9
    const uint8_t *inSmallFCD=inBytes+offset;
109
110
9
    init(inIndexes, ownedTrie, inExtraData, inSmallFCD);
111
9
}
112
113
// instance cache ---------------------------------------------------------- ***
114
115
Norm2AllModes *
116
Norm2AllModes::createInstance(const char *packageName,
117
                              const char *name,
118
9
                              UErrorCode &errorCode) {
119
9
    if(U_FAILURE(errorCode)) {
120
0
        return nullptr;
121
0
    }
122
9
    LoadedNormalizer2Impl *impl=new LoadedNormalizer2Impl;
123
9
    if(impl==nullptr) {
124
0
        errorCode=U_MEMORY_ALLOCATION_ERROR;
125
0
        return nullptr;
126
0
    }
127
9
    impl->load(packageName, name, errorCode);
128
9
    return createInstance(impl, errorCode);
129
9
}
130
131
U_CDECL_BEGIN
132
static UBool U_CALLCONV uprv_loaded_normalizer2_cleanup();
133
U_CDECL_END
134
135
#if !NORM2_HARDCODE_NFC_DATA
136
static Norm2AllModes *nfcSingleton;
137
static icu::UInitOnce nfcInitOnce {};
138
#endif
139
140
static Norm2AllModes *nfkcSingleton;
141
static icu::UInitOnce nfkcInitOnce {};
142
143
static Norm2AllModes *nfkc_cfSingleton;
144
static icu::UInitOnce nfkc_cfInitOnce {};
145
146
static Norm2AllModes *nfkc_scfSingleton;
147
static icu::UInitOnce nfkc_scfInitOnce {};
148
149
static UHashtable    *cache=nullptr;
150
151
// UInitOnce singleton initialization function
152
9
static void U_CALLCONV initSingletons(const char *what, UErrorCode &errorCode) {
153
#if !NORM2_HARDCODE_NFC_DATA
154
    if (uprv_strcmp(what, "nfc") == 0) {
155
        nfcSingleton    = Norm2AllModes::createInstance(nullptr, "nfc", errorCode);
156
    } else
157
#endif
158
9
    if (uprv_strcmp(what, "nfkc") == 0) {
159
5
        nfkcSingleton    = Norm2AllModes::createInstance(nullptr, "nfkc", errorCode);
160
5
    } else if (uprv_strcmp(what, "nfkc_cf") == 0) {
161
3
        nfkc_cfSingleton = Norm2AllModes::createInstance(nullptr, "nfkc_cf", errorCode);
162
3
    } else if (uprv_strcmp(what, "nfkc_scf") == 0) {
163
1
        nfkc_scfSingleton = Norm2AllModes::createInstance(nullptr, "nfkc_scf", errorCode);
164
1
    } else {
165
0
        UPRV_UNREACHABLE_EXIT;   // Unknown singleton
166
0
    }
167
9
    ucln_common_registerCleanup(UCLN_COMMON_LOADED_NORMALIZER2, uprv_loaded_normalizer2_cleanup);
168
9
}
169
170
U_CDECL_BEGIN
171
172
0
static void U_CALLCONV deleteNorm2AllModes(void *allModes) {
173
0
    delete (Norm2AllModes *)allModes;
174
0
}
175
176
0
static UBool U_CALLCONV uprv_loaded_normalizer2_cleanup() {
177
#if !NORM2_HARDCODE_NFC_DATA
178
    delete nfcSingleton;
179
    nfcSingleton = nullptr;
180
    nfcInitOnce.reset();
181
#endif
182
183
0
    delete nfkcSingleton;
184
0
    nfkcSingleton = nullptr;
185
0
    nfkcInitOnce.reset();
186
187
0
    delete nfkc_cfSingleton;
188
0
    nfkc_cfSingleton = nullptr;
189
0
    nfkc_cfInitOnce.reset();
190
191
0
    delete nfkc_scfSingleton;
192
0
    nfkc_scfSingleton = nullptr;
193
0
    nfkc_scfInitOnce.reset();
194
195
0
    uhash_close(cache);
196
0
    cache=nullptr;
197
0
    return true;
198
0
}
199
200
U_CDECL_END
201
202
#if !NORM2_HARDCODE_NFC_DATA
203
const Norm2AllModes *
204
Norm2AllModes::getNFCInstance(UErrorCode &errorCode) {
205
    if(U_FAILURE(errorCode)) { return nullptr; }
206
    umtx_initOnce(nfcInitOnce, &initSingletons, "nfc", errorCode);
207
    return nfcSingleton;
208
}
209
#endif
210
211
const Norm2AllModes *
212
34.3k
Norm2AllModes::getNFKCInstance(UErrorCode &errorCode) {
213
34.3k
    if(U_FAILURE(errorCode)) { return nullptr; }
214
34.3k
    umtx_initOnce(nfkcInitOnce, &initSingletons, "nfkc", errorCode);
215
34.3k
    return nfkcSingleton;
216
34.3k
}
217
218
const Norm2AllModes *
219
18.3k
Norm2AllModes::getNFKC_CFInstance(UErrorCode &errorCode) {
220
18.3k
    if(U_FAILURE(errorCode)) { return nullptr; }
221
18.3k
    umtx_initOnce(nfkc_cfInitOnce, &initSingletons, "nfkc_cf", errorCode);
222
18.3k
    return nfkc_cfSingleton;
223
18.3k
}
224
225
const Norm2AllModes *
226
704
Norm2AllModes::getNFKC_SCFInstance(UErrorCode &errorCode) {
227
704
    if(U_FAILURE(errorCode)) { return nullptr; }
228
704
    umtx_initOnce(nfkc_scfInitOnce, &initSingletons, "nfkc_scf", errorCode);
229
704
    return nfkc_scfSingleton;
230
704
}
231
232
#if !NORM2_HARDCODE_NFC_DATA
233
const Normalizer2 *
234
Normalizer2::getNFCInstance(UErrorCode &errorCode) {
235
    const Norm2AllModes *allModes=Norm2AllModes::getNFCInstance(errorCode);
236
    return allModes!=nullptr ? &allModes->comp : nullptr;
237
}
238
239
const Normalizer2 *
240
Normalizer2::getNFDInstance(UErrorCode &errorCode) {
241
    const Norm2AllModes *allModes=Norm2AllModes::getNFCInstance(errorCode);
242
    return allModes!=nullptr ? &allModes->decomp : nullptr;
243
}
244
245
const Normalizer2 *Normalizer2Factory::getFCDInstance(UErrorCode &errorCode) {
246
    const Norm2AllModes *allModes=Norm2AllModes::getNFCInstance(errorCode);
247
    return allModes!=nullptr ? &allModes->fcd : nullptr;
248
}
249
250
const Normalizer2 *Normalizer2Factory::getFCCInstance(UErrorCode &errorCode) {
251
    const Norm2AllModes *allModes=Norm2AllModes::getNFCInstance(errorCode);
252
    return allModes!=nullptr ? &allModes->fcc : nullptr;
253
}
254
255
const Normalizer2Impl *
256
Normalizer2Factory::getNFCImpl(UErrorCode &errorCode) {
257
    const Norm2AllModes *allModes=Norm2AllModes::getNFCInstance(errorCode);
258
    return allModes!=nullptr ? allModes->impl : nullptr;
259
}
260
#endif
261
262
const Normalizer2 *
263
17.0k
Normalizer2::getNFKCInstance(UErrorCode &errorCode) {
264
17.0k
    const Norm2AllModes *allModes=Norm2AllModes::getNFKCInstance(errorCode);
265
17.0k
    return allModes!=nullptr ? &allModes->comp : nullptr;
266
17.0k
}
267
268
const Normalizer2 *
269
17.2k
Normalizer2::getNFKDInstance(UErrorCode &errorCode) {
270
17.2k
    const Norm2AllModes *allModes=Norm2AllModes::getNFKCInstance(errorCode);
271
17.2k
    return allModes!=nullptr ? &allModes->decomp : nullptr;
272
17.2k
}
273
274
const Normalizer2 *
275
645
Normalizer2::getNFKCCasefoldInstance(UErrorCode &errorCode) {
276
645
    const Norm2AllModes *allModes=Norm2AllModes::getNFKC_CFInstance(errorCode);
277
645
    return allModes!=nullptr ? &allModes->comp : nullptr;
278
645
}
279
280
const Normalizer2 *
281
704
Normalizer2::getNFKCSimpleCasefoldInstance(UErrorCode &errorCode) {
282
704
    const Norm2AllModes *allModes=Norm2AllModes::getNFKC_SCFInstance(errorCode);
283
704
    return allModes!=nullptr ? &allModes->comp : nullptr;
284
704
}
285
286
const Normalizer2 *
287
Normalizer2::getInstance(const char *packageName,
288
                         const char *name,
289
                         UNormalization2Mode mode,
290
0
                         UErrorCode &errorCode) {
291
0
    if(U_FAILURE(errorCode)) {
292
0
        return nullptr;
293
0
    }
294
0
    if(name==nullptr || *name==0) {
295
0
        errorCode=U_ILLEGAL_ARGUMENT_ERROR;
296
0
        return nullptr;
297
0
    }
298
0
    const Norm2AllModes *allModes=nullptr;
299
0
    if(packageName==nullptr) {
300
0
        if(0==uprv_strcmp(name, "nfc")) {
301
0
            allModes=Norm2AllModes::getNFCInstance(errorCode);
302
0
        } else if(0==uprv_strcmp(name, "nfkc")) {
303
0
            allModes=Norm2AllModes::getNFKCInstance(errorCode);
304
0
        } else if(0==uprv_strcmp(name, "nfkc_cf")) {
305
0
            allModes=Norm2AllModes::getNFKC_CFInstance(errorCode);
306
0
        } else if(0==uprv_strcmp(name, "nfkc_scf")) {
307
0
            allModes=Norm2AllModes::getNFKC_SCFInstance(errorCode);
308
0
        }
309
0
    }
310
0
    if(allModes==nullptr && U_SUCCESS(errorCode)) {
311
0
        {
312
0
            Mutex lock;
313
0
            if(cache!=nullptr) {
314
0
                allModes = static_cast<Norm2AllModes*>(uhash_get(cache, name));
315
0
            }
316
0
        }
317
0
        if(allModes==nullptr) {
318
0
            ucln_common_registerCleanup(UCLN_COMMON_LOADED_NORMALIZER2, uprv_loaded_normalizer2_cleanup);
319
0
            LocalPointer<Norm2AllModes> localAllModes(
320
0
                Norm2AllModes::createInstance(packageName, name, errorCode));
321
0
            if(U_SUCCESS(errorCode)) {
322
0
                Mutex lock;
323
0
                if(cache==nullptr) {
324
0
                    cache=uhash_open(uhash_hashChars, uhash_compareChars, nullptr, &errorCode);
325
0
                    if(U_FAILURE(errorCode)) {
326
0
                        return nullptr;
327
0
                    }
328
0
                    uhash_setKeyDeleter(cache, uprv_free);
329
0
                    uhash_setValueDeleter(cache, deleteNorm2AllModes);
330
0
                }
331
0
                void *temp=uhash_get(cache, name);
332
0
                if(temp==nullptr) {
333
0
                    int32_t keyLength= static_cast<int32_t>(uprv_strlen(name)+1);
334
0
                    char* nameCopy = static_cast<char*>(uprv_malloc(keyLength));
335
0
                    if(nameCopy==nullptr) {
336
0
                        errorCode=U_MEMORY_ALLOCATION_ERROR;
337
0
                        return nullptr;
338
0
                    }
339
0
                    uprv_memcpy(nameCopy, name, keyLength);
340
0
                    allModes=localAllModes.getAlias();
341
0
                    uhash_put(cache, nameCopy, localAllModes.orphan(), &errorCode);
342
0
                } else {
343
                    // race condition
344
0
                    allModes = static_cast<Norm2AllModes*>(temp);
345
0
                }
346
0
            }
347
0
        }
348
0
    }
349
0
    if(allModes!=nullptr && U_SUCCESS(errorCode)) {
350
0
        switch(mode) {
351
0
        case UNORM2_COMPOSE:
352
0
            return &allModes->comp;
353
0
        case UNORM2_DECOMPOSE:
354
0
            return &allModes->decomp;
355
0
        case UNORM2_FCD:
356
0
            return &allModes->fcd;
357
0
        case UNORM2_COMPOSE_CONTIGUOUS:
358
0
            return &allModes->fcc;
359
0
        default:
360
0
            break;  // do nothing
361
0
        }
362
0
    }
363
0
    return nullptr;
364
0
}
365
366
const Normalizer2 *
367
1.87M
Normalizer2Factory::getInstance(UNormalizationMode mode, UErrorCode &errorCode) {
368
1.87M
    if(U_FAILURE(errorCode)) {
369
0
        return nullptr;
370
0
    }
371
1.87M
    switch(mode) {
372
1.83M
    case UNORM_NFD:
373
1.83M
        return Normalizer2::getNFDInstance(errorCode);
374
16.7k
    case UNORM_NFKD:
375
16.7k
        return Normalizer2::getNFKDInstance(errorCode);
376
8.47k
    case UNORM_NFC:
377
8.47k
        return Normalizer2::getNFCInstance(errorCode);
378
16.5k
    case UNORM_NFKC:
379
16.5k
        return Normalizer2::getNFKCInstance(errorCode);
380
0
    case UNORM_FCD:
381
0
        return getFCDInstance(errorCode);
382
0
    default:  // UNORM_NONE
383
0
        return getNoopInstance(errorCode);
384
1.87M
    }
385
1.87M
}
386
387
const Normalizer2Impl *
388
1
Normalizer2Factory::getNFKCImpl(UErrorCode &errorCode) {
389
1
    const Norm2AllModes *allModes=Norm2AllModes::getNFKCInstance(errorCode);
390
1
    return allModes!=nullptr ? allModes->impl : nullptr;
391
1
}
392
393
const Normalizer2Impl *
394
17.7k
Normalizer2Factory::getNFKC_CFImpl(UErrorCode &errorCode) {
395
17.7k
    const Norm2AllModes *allModes=Norm2AllModes::getNFKC_CFInstance(errorCode);
396
17.7k
    return allModes!=nullptr ? allModes->impl : nullptr;
397
17.7k
}
398
399
U_NAMESPACE_END
400
401
// C API ------------------------------------------------------------------- ***
402
403
U_NAMESPACE_USE
404
405
U_CAPI const UNormalizer2 * U_EXPORT2
406
0
unorm2_getNFKCInstance(UErrorCode *pErrorCode) {
407
0
    return (const UNormalizer2 *)Normalizer2::getNFKCInstance(*pErrorCode);
408
0
}
409
410
U_CAPI const UNormalizer2 * U_EXPORT2
411
0
unorm2_getNFKDInstance(UErrorCode *pErrorCode) {
412
0
    return (const UNormalizer2 *)Normalizer2::getNFKDInstance(*pErrorCode);
413
0
}
414
415
U_CAPI const UNormalizer2 * U_EXPORT2
416
0
unorm2_getNFKCCasefoldInstance(UErrorCode *pErrorCode) {
417
0
    return (const UNormalizer2 *)Normalizer2::getNFKCCasefoldInstance(*pErrorCode);
418
0
}
419
420
U_CAPI const UNormalizer2 * U_EXPORT2
421
0
unorm2_getNFKCSimpleCasefoldInstance(UErrorCode *pErrorCode) {
422
0
    return (const UNormalizer2 *)Normalizer2::getNFKCSimpleCasefoldInstance(*pErrorCode);
423
0
}
424
425
U_CAPI const UNormalizer2 * U_EXPORT2
426
unorm2_getInstance(const char *packageName,
427
                   const char *name,
428
                   UNormalization2Mode mode,
429
0
                   UErrorCode *pErrorCode) {
430
0
    return (const UNormalizer2 *)Normalizer2::getInstance(packageName, name, mode, *pErrorCode);
431
0
}
432
433
U_CFUNC UNormalizationCheckResult
434
1.85M
unorm_getQuickCheck(UChar32 c, UNormalizationMode mode) {
435
1.85M
    if(mode<=UNORM_NONE || UNORM_FCD<=mode) {
436
0
        return UNORM_YES;
437
0
    }
438
1.85M
    UErrorCode errorCode=U_ZERO_ERROR;
439
1.85M
    const Normalizer2 *norm2=Normalizer2Factory::getInstance(mode, errorCode);
440
1.85M
    if(U_SUCCESS(errorCode)) {
441
1.85M
        return ((const Normalizer2WithImpl *)norm2)->getQuickCheck(c);
442
1.85M
    } else {
443
0
        return UNORM_MAYBE;
444
0
    }
445
1.85M
}
446
447
#endif  // !UCONFIG_NO_NORMALIZATION