Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/icu/source/i18n/uspoof.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
* Copyright (C) 2008-2015, International Business Machines Corporation
6
* and others. All Rights Reserved.
7
***************************************************************************
8
*   file name:  uspoof.cpp
9
*   encoding:   UTF-8
10
*   tab size:   8 (not used)
11
*   indentation:4
12
*
13
*   created on: 2008Feb13
14
*   created by: Andy Heninger
15
*
16
*   Unicode Spoof Detection
17
*/
18
#include "unicode/utypes.h"
19
#include "unicode/normalizer2.h"
20
#include "unicode/uspoof.h"
21
#include "unicode/ustring.h"
22
#include "unicode/utf16.h"
23
#include "cmemory.h"
24
#include "cstring.h"
25
#include "mutex.h"
26
#include "scriptset.h"
27
#include "uassert.h"
28
#include "ucln_in.h"
29
#include "uspoof_impl.h"
30
#include "umutex.h"
31
32
33
#if !UCONFIG_NO_NORMALIZATION
34
35
U_NAMESPACE_USE
36
37
38
//
39
// Static Objects used by the spoof impl, their thread safe initialization and their cleanup.
40
//
41
static UnicodeSet *gInclusionSet = NULL;
42
static UnicodeSet *gRecommendedSet = NULL;
43
static const Normalizer2 *gNfdNormalizer = NULL;
44
static UInitOnce gSpoofInitStaticsOnce = U_INITONCE_INITIALIZER;
45
46
static UBool U_CALLCONV
47
0
uspoof_cleanup(void) {
48
0
    delete gInclusionSet;
49
0
    gInclusionSet = NULL;
50
0
    delete gRecommendedSet;
51
0
    gRecommendedSet = NULL;
52
0
    gNfdNormalizer = NULL;
53
0
    gSpoofInitStaticsOnce.reset();
54
0
    return TRUE;
55
0
}
56
57
0
static void U_CALLCONV initializeStatics(UErrorCode &status) {
58
0
    static const char16_t *inclusionPat =
59
0
        u"['\\-.\\:\\u00B7\\u0375\\u058A\\u05F3\\u05F4\\u06FD\\u06FE\\u0F0B\\u200C"
60
0
        u"\\u200D\\u2010\\u2019\\u2027\\u30A0\\u30FB]";
61
0
    gInclusionSet = new UnicodeSet(UnicodeString(inclusionPat), status);
62
0
    if (gInclusionSet == NULL) {
63
0
        status = U_MEMORY_ALLOCATION_ERROR;
64
0
        return;
65
0
    }
66
0
    gInclusionSet->freeze();
67
0
68
0
    // Note: data from IdentifierStatus.txt & IdentifierType.txt
69
0
    // There is tooling to generate this constant in the unicodetools project:
70
0
    //      org.unicode.text.tools.RecommendedSetGenerator
71
0
    // It will print the Java and C++ code to the console for easy copy-paste into this file.
72
0
    // Note: concatenated string constants do not work with UNICODE_STRING_SIMPLE on all platforms.
73
0
    static const char16_t *recommendedPat =
74
0
        u"[0-9A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u0131\\u0134-\\u013E"
75
0
        u"\\u0141-\\u0148\\u014A-\\u017E\\u018F\\u01A0\\u01A1\\u01AF\\u01B0\\u01CD-"
76
0
        u"\\u01DC\\u01DE-\\u01E3\\u01E6-\\u01F0\\u01F4\\u01F5\\u01F8-\\u021B\\u021E"
77
0
        u"\\u021F\\u0226-\\u0233\\u0259\\u02BB\\u02BC\\u02EC\\u0300-\\u0304\\u0306-"
78
0
        u"\\u030C\\u030F-\\u0311\\u0313\\u0314\\u031B\\u0323-\\u0328\\u032D\\u032E"
79
0
        u"\\u0330\\u0331\\u0335\\u0338\\u0339\\u0342\\u0345\\u037B-\\u037D\\u0386"
80
0
        u"\\u0388-\\u038A\\u038C\\u038E-\\u03A1\\u03A3-\\u03CE\\u03FC-\\u045F\\u048A-"
81
0
        u"\\u0529\\u052E\\u052F\\u0531-\\u0556\\u0559\\u0560-\\u0586\\u0588\\u05B4"
82
0
        u"\\u05D0-\\u05EA\\u05EF-\\u05F2\\u0620-\\u063F\\u0641-\\u0655\\u0660-\\u0669"
83
0
        u"\\u0670-\\u0672\\u0674\\u0679-\\u068D\\u068F-\\u06D3\\u06D5\\u06E5\\u06E6"
84
0
        u"\\u06EE-\\u06FC\\u06FF\\u0750-\\u07B1\\u08A0-\\u08AC\\u08B2\\u08B6-\\u08BD"
85
0
        u"\\u0901-\\u094D\\u094F\\u0950\\u0956\\u0957\\u0960-\\u0963\\u0966-\\u096F"
86
0
        u"\\u0971-\\u0977\\u0979-\\u097F\\u0981-\\u0983\\u0985-\\u098C\\u098F\\u0990"
87
0
        u"\\u0993-\\u09A8\\u09AA-\\u09B0\\u09B2\\u09B6-\\u09B9\\u09BC-\\u09C4\\u09C7"
88
0
        u"\\u09C8\\u09CB-\\u09CE\\u09D7\\u09E0-\\u09E3\\u09E6-\\u09F1\\u09FC\\u09FE"
89
0
        u"\\u0A01-\\u0A03\\u0A05-\\u0A0A\\u0A0F\\u0A10\\u0A13-\\u0A28\\u0A2A-\\u0A30"
90
0
        u"\\u0A32\\u0A35\\u0A38\\u0A39\\u0A3C\\u0A3E-\\u0A42\\u0A47\\u0A48\\u0A4B-"
91
0
        u"\\u0A4D\\u0A5C\\u0A66-\\u0A74\\u0A81-\\u0A83\\u0A85-\\u0A8D\\u0A8F-\\u0A91"
92
0
        u"\\u0A93-\\u0AA8\\u0AAA-\\u0AB0\\u0AB2\\u0AB3\\u0AB5-\\u0AB9\\u0ABC-\\u0AC5"
93
0
        u"\\u0AC7-\\u0AC9\\u0ACB-\\u0ACD\\u0AD0\\u0AE0-\\u0AE3\\u0AE6-\\u0AEF\\u0AFA-"
94
0
        u"\\u0AFF\\u0B01-\\u0B03\\u0B05-\\u0B0C\\u0B0F\\u0B10\\u0B13-\\u0B28\\u0B2A-"
95
0
        u"\\u0B30\\u0B32\\u0B33\\u0B35-\\u0B39\\u0B3C-\\u0B43\\u0B47\\u0B48\\u0B4B-"
96
0
        u"\\u0B4D\\u0B56\\u0B57\\u0B5F-\\u0B61\\u0B66-\\u0B6F\\u0B71\\u0B82\\u0B83"
97
0
        u"\\u0B85-\\u0B8A\\u0B8E-\\u0B90\\u0B92-\\u0B95\\u0B99\\u0B9A\\u0B9C\\u0B9E"
98
0
        u"\\u0B9F\\u0BA3\\u0BA4\\u0BA8-\\u0BAA\\u0BAE-\\u0BB9\\u0BBE-\\u0BC2\\u0BC6-"
99
0
        u"\\u0BC8\\u0BCA-\\u0BCD\\u0BD0\\u0BD7\\u0BE6-\\u0BEF\\u0C01-\\u0C0C\\u0C0E-"
100
0
        u"\\u0C10\\u0C12-\\u0C28\\u0C2A-\\u0C33\\u0C35-\\u0C39\\u0C3D-\\u0C44\\u0C46-"
101
0
        u"\\u0C48\\u0C4A-\\u0C4D\\u0C55\\u0C56\\u0C60\\u0C61\\u0C66-\\u0C6F\\u0C80"
102
0
        u"\\u0C82\\u0C83\\u0C85-\\u0C8C\\u0C8E-\\u0C90\\u0C92-\\u0CA8\\u0CAA-\\u0CB3"
103
0
        u"\\u0CB5-\\u0CB9\\u0CBC-\\u0CC4\\u0CC6-\\u0CC8\\u0CCA-\\u0CCD\\u0CD5\\u0CD6"
104
0
        u"\\u0CE0-\\u0CE3\\u0CE6-\\u0CEF\\u0CF1\\u0CF2\\u0D00\\u0D02\\u0D03\\u0D05-"
105
0
        u"\\u0D0C\\u0D0E-\\u0D10\\u0D12-\\u0D43\\u0D46-\\u0D48\\u0D4A-\\u0D4E\\u0D54-"
106
0
        u"\\u0D57\\u0D60\\u0D61\\u0D66-\\u0D6F\\u0D7A-\\u0D7F\\u0D82\\u0D83\\u0D85-"
107
0
        u"\\u0D8E\\u0D91-\\u0D96\\u0D9A-\\u0DA5\\u0DA7-\\u0DB1\\u0DB3-\\u0DBB\\u0DBD"
108
0
        u"\\u0DC0-\\u0DC6\\u0DCA\\u0DCF-\\u0DD4\\u0DD6\\u0DD8-\\u0DDE\\u0DF2\\u0E01-"
109
0
        u"\\u0E32\\u0E34-\\u0E3A\\u0E40-\\u0E4E\\u0E50-\\u0E59\\u0E81\\u0E82\\u0E84"
110
0
        u"\\u0E87\\u0E88\\u0E8A\\u0E8D\\u0E94-\\u0E97\\u0E99-\\u0E9F\\u0EA1-\\u0EA3"
111
0
        u"\\u0EA5\\u0EA7\\u0EAA\\u0EAB\\u0EAD-\\u0EB2\\u0EB4-\\u0EB9\\u0EBB-\\u0EBD"
112
0
        u"\\u0EC0-\\u0EC4\\u0EC6\\u0EC8-\\u0ECD\\u0ED0-\\u0ED9\\u0EDE\\u0EDF\\u0F00"
113
0
        u"\\u0F20-\\u0F29\\u0F35\\u0F37\\u0F3E-\\u0F42\\u0F44-\\u0F47\\u0F49-\\u0F4C"
114
0
        u"\\u0F4E-\\u0F51\\u0F53-\\u0F56\\u0F58-\\u0F5B\\u0F5D-\\u0F68\\u0F6A-\\u0F6C"
115
0
        u"\\u0F71\\u0F72\\u0F74\\u0F7A-\\u0F80\\u0F82-\\u0F84\\u0F86-\\u0F92\\u0F94-"
116
0
        u"\\u0F97\\u0F99-\\u0F9C\\u0F9E-\\u0FA1\\u0FA3-\\u0FA6\\u0FA8-\\u0FAB\\u0FAD-"
117
0
        u"\\u0FB8\\u0FBA-\\u0FBC\\u0FC6\\u1000-\\u1049\\u1050-\\u109D\\u10C7\\u10CD"
118
0
        u"\\u10D0-\\u10F0\\u10F7-\\u10FA\\u10FD-\\u10FF\\u1200-\\u1248\\u124A-\\u124D"
119
0
        u"\\u1250-\\u1256\\u1258\\u125A-\\u125D\\u1260-\\u1288\\u128A-\\u128D\\u1290-"
120
0
        u"\\u12B0\\u12B2-\\u12B5\\u12B8-\\u12BE\\u12C0\\u12C2-\\u12C5\\u12C8-\\u12D6"
121
0
        u"\\u12D8-\\u1310\\u1312-\\u1315\\u1318-\\u135A\\u135D-\\u135F\\u1380-\\u138F"
122
0
        u"\\u1780-\\u17A2\\u17A5-\\u17A7\\u17A9-\\u17B3\\u17B6-\\u17CA\\u17D2\\u17D7"
123
0
        u"\\u17DC\\u17E0-\\u17E9\\u1C80-\\u1C88\\u1C90-\\u1CBA\\u1CBD-\\u1CBF\\u1E00-"
124
0
        u"\\u1E99\\u1E9E\\u1EA0-\\u1EF9\\u1F00-\\u1F15\\u1F18-\\u1F1D\\u1F20-\\u1F45"
125
0
        u"\\u1F48-\\u1F4D\\u1F50-\\u1F57\\u1F59\\u1F5B\\u1F5D\\u1F5F-\\u1F70\\u1F72"
126
0
        u"\\u1F74\\u1F76\\u1F78\\u1F7A\\u1F7C\\u1F80-\\u1FB4\\u1FB6-\\u1FBA\\u1FBC"
127
0
        u"\\u1FC2-\\u1FC4\\u1FC6-\\u1FC8\\u1FCA\\u1FCC\\u1FD0-\\u1FD2\\u1FD6-\\u1FDA"
128
0
        u"\\u1FE0-\\u1FE2\\u1FE4-\\u1FEA\\u1FEC\\u1FF2-\\u1FF4\\u1FF6-\\u1FF8\\u1FFA"
129
0
        u"\\u1FFC\\u2D27\\u2D2D\\u2D80-\\u2D96\\u2DA0-\\u2DA6\\u2DA8-\\u2DAE\\u2DB0-"
130
0
        u"\\u2DB6\\u2DB8-\\u2DBE\\u2DC0-\\u2DC6\\u2DC8-\\u2DCE\\u2DD0-\\u2DD6\\u2DD8-"
131
0
        u"\\u2DDE\\u3005-\\u3007\\u3041-\\u3096\\u3099\\u309A\\u309D\\u309E\\u30A1-"
132
0
        u"\\u30FA\\u30FC-\\u30FE\\u3105-\\u312F\\u31A0-\\u31BA\\u3400-\\u4DB5\\u4E00-"
133
0
        u"\\u9FEF\\uA660\\uA661\\uA674-\\uA67B\\uA67F\\uA69F\\uA717-\\uA71F\\uA788"
134
0
        u"\\uA78D\\uA78E\\uA790-\\uA793\\uA7A0-\\uA7AA\\uA7AE\\uA7AF\\uA7B8\\uA7B9"
135
0
        u"\\uA7FA\\uA9E7-\\uA9FE\\uAA60-\\uAA76\\uAA7A-\\uAA7F\\uAB01-\\uAB06\\uAB09-"
136
0
        u"\\uAB0E\\uAB11-\\uAB16\\uAB20-\\uAB26\\uAB28-\\uAB2E\\uAC00-\\uD7A3\\uFA0E"
137
0
        u"\\uFA0F\\uFA11\\uFA13\\uFA14\\uFA1F\\uFA21\\uFA23\\uFA24\\uFA27-\\uFA29"
138
0
        u"\\U0001133B\\U0001B002-\\U0001B11E\\U00020000-\\U0002A6D6\\U0002A700-"
139
0
        u"\\U0002B734\\U0002B740-\\U0002B81D\\U0002B820-\\U0002CEA1\\U0002CEB0-"
140
0
        u"\\U0002EBE0]";
141
0
142
0
    gRecommendedSet = new UnicodeSet(UnicodeString(recommendedPat), status);
143
0
    if (gRecommendedSet == NULL) {
144
0
        status = U_MEMORY_ALLOCATION_ERROR;
145
0
        delete gInclusionSet;
146
0
        return;
147
0
    }
148
0
    gRecommendedSet->freeze();
149
0
    gNfdNormalizer = Normalizer2::getNFDInstance(status);
150
0
    ucln_i18n_registerCleanup(UCLN_I18N_SPOOF, uspoof_cleanup);
151
0
}
152
153
0
U_CFUNC void uspoof_internalInitStatics(UErrorCode *status) {
154
0
    umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
155
0
}
156
157
U_CAPI USpoofChecker * U_EXPORT2
158
0
uspoof_open(UErrorCode *status) {
159
0
    umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
160
0
    if (U_FAILURE(*status)) {
161
0
        return NULL;
162
0
    }
163
0
    SpoofImpl *si = new SpoofImpl(*status);
164
0
    if (si == NULL) {
165
0
        *status = U_MEMORY_ALLOCATION_ERROR;
166
0
        return NULL;
167
0
    }
168
0
    if (U_FAILURE(*status)) {
169
0
        delete si;
170
0
        return NULL;
171
0
    }
172
0
    return si->asUSpoofChecker();
173
0
}
174
175
176
U_CAPI USpoofChecker * U_EXPORT2
177
uspoof_openFromSerialized(const void *data, int32_t length, int32_t *pActualLength,
178
0
                          UErrorCode *status) {
179
0
    if (U_FAILURE(*status)) {
180
0
        return NULL;
181
0
    }
182
0
183
0
    if (data == NULL) {
184
0
        *status = U_ILLEGAL_ARGUMENT_ERROR;
185
0
        return NULL;
186
0
    }
187
0
188
0
    umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
189
0
    if (U_FAILURE(*status))
190
0
    {
191
0
        return NULL;
192
0
    }
193
0
194
0
    SpoofData *sd = new SpoofData(data, length, *status);
195
0
    if (sd == NULL) {
196
0
        *status = U_MEMORY_ALLOCATION_ERROR;
197
0
        return NULL;
198
0
    }
199
0
200
0
    if (U_FAILURE(*status)) {
201
0
        delete sd;
202
0
        return NULL;
203
0
    }
204
0
205
0
    SpoofImpl *si = new SpoofImpl(sd, *status);
206
0
    if (si == NULL) {
207
0
        *status = U_MEMORY_ALLOCATION_ERROR;
208
0
        delete sd; // explicit delete as the destructor for si won't be called.
209
0
        return NULL;
210
0
    }
211
0
212
0
    if (U_FAILURE(*status)) {
213
0
        delete si; // no delete for sd, as the si destructor will delete it.
214
0
        return NULL;
215
0
    }
216
0
217
0
    if (pActualLength != NULL) {
218
0
        *pActualLength = sd->size();
219
0
    }
220
0
    return si->asUSpoofChecker();
221
0
}
222
223
224
U_CAPI USpoofChecker * U_EXPORT2
225
0
uspoof_clone(const USpoofChecker *sc, UErrorCode *status) {
226
0
    const SpoofImpl *src = SpoofImpl::validateThis(sc, *status);
227
0
    if (src == NULL) {
228
0
        return NULL;
229
0
    }
230
0
    SpoofImpl *result = new SpoofImpl(*src, *status);   // copy constructor
231
0
    if (result == NULL) {
232
0
        *status = U_MEMORY_ALLOCATION_ERROR;
233
0
        return NULL;
234
0
    }
235
0
    if (U_FAILURE(*status)) {
236
0
        delete result;
237
0
        result = NULL;
238
0
    }
239
0
    return result->asUSpoofChecker();
240
0
}
241
242
243
U_CAPI void U_EXPORT2
244
0
uspoof_close(USpoofChecker *sc) {
245
0
    UErrorCode status = U_ZERO_ERROR;
246
0
    SpoofImpl *This = SpoofImpl::validateThis(sc, status);
247
0
    delete This;
248
0
}
249
250
251
U_CAPI void U_EXPORT2
252
0
uspoof_setChecks(USpoofChecker *sc, int32_t checks, UErrorCode *status) {
253
0
    SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
254
0
    if (This == NULL) {
255
0
        return;
256
0
    }
257
0
258
0
    // Verify that the requested checks are all ones (bits) that 
259
0
    //   are acceptable, known values.
260
0
    if (checks & ~(USPOOF_ALL_CHECKS | USPOOF_AUX_INFO)) {
261
0
        *status = U_ILLEGAL_ARGUMENT_ERROR; 
262
0
        return;
263
0
    }
264
0
265
0
    This->fChecks = checks;
266
0
}
267
268
269
U_CAPI int32_t U_EXPORT2
270
0
uspoof_getChecks(const USpoofChecker *sc, UErrorCode *status) {
271
0
    const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
272
0
    if (This == NULL) {
273
0
        return 0;
274
0
    }
275
0
    return This->fChecks;
276
0
}
277
278
U_CAPI void U_EXPORT2
279
0
uspoof_setRestrictionLevel(USpoofChecker *sc, URestrictionLevel restrictionLevel) {
280
0
    UErrorCode status = U_ZERO_ERROR;
281
0
    SpoofImpl *This = SpoofImpl::validateThis(sc, status);
282
0
    if (This != NULL) {
283
0
        This->fRestrictionLevel = restrictionLevel;
284
0
        This->fChecks |= USPOOF_RESTRICTION_LEVEL;
285
0
    }
286
0
}
287
288
U_CAPI URestrictionLevel U_EXPORT2
289
0
uspoof_getRestrictionLevel(const USpoofChecker *sc) {
290
0
    UErrorCode status = U_ZERO_ERROR;
291
0
    const SpoofImpl *This = SpoofImpl::validateThis(sc, status);
292
0
    if (This == NULL) {
293
0
        return USPOOF_UNRESTRICTIVE;
294
0
    }
295
0
    return This->fRestrictionLevel;
296
0
}
297
298
U_CAPI void U_EXPORT2
299
0
uspoof_setAllowedLocales(USpoofChecker *sc, const char *localesList, UErrorCode *status) {
300
0
    SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
301
0
    if (This == NULL) {
302
0
        return;
303
0
    }
304
0
    This->setAllowedLocales(localesList, *status);
305
0
}
306
307
U_CAPI const char * U_EXPORT2
308
0
uspoof_getAllowedLocales(USpoofChecker *sc, UErrorCode *status) {
309
0
    SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
310
0
    if (This == NULL) {
311
0
        return NULL;
312
0
    }
313
0
    return This->getAllowedLocales(*status);
314
0
}
315
316
317
U_CAPI const USet * U_EXPORT2
318
0
uspoof_getAllowedChars(const USpoofChecker *sc, UErrorCode *status) {
319
0
    const UnicodeSet *result = uspoof_getAllowedUnicodeSet(sc, status);
320
0
    return result->toUSet();
321
0
}
322
323
U_CAPI const UnicodeSet * U_EXPORT2
324
0
uspoof_getAllowedUnicodeSet(const USpoofChecker *sc, UErrorCode *status) {
325
0
    const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
326
0
    if (This == NULL) {
327
0
        return NULL;
328
0
    }
329
0
    return This->fAllowedCharsSet;
330
0
}
331
332
333
U_CAPI void U_EXPORT2
334
0
uspoof_setAllowedChars(USpoofChecker *sc, const USet *chars, UErrorCode *status) {
335
0
    const UnicodeSet *set = UnicodeSet::fromUSet(chars);
336
0
    uspoof_setAllowedUnicodeSet(sc, set, status);
337
0
}
338
339
340
U_CAPI void U_EXPORT2
341
0
uspoof_setAllowedUnicodeSet(USpoofChecker *sc, const UnicodeSet *chars, UErrorCode *status) {
342
0
    SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
343
0
    if (This == NULL) {
344
0
        return;
345
0
    }
346
0
    if (chars->isBogus()) {
347
0
        *status = U_ILLEGAL_ARGUMENT_ERROR;
348
0
        return;
349
0
    }
350
0
    UnicodeSet *clonedSet = static_cast<UnicodeSet *>(chars->clone());
351
0
    if (clonedSet == NULL || clonedSet->isBogus()) {
352
0
        *status = U_MEMORY_ALLOCATION_ERROR;
353
0
        return;
354
0
    }
355
0
    clonedSet->freeze();
356
0
    delete This->fAllowedCharsSet;
357
0
    This->fAllowedCharsSet = clonedSet;
358
0
    This->fChecks |= USPOOF_CHAR_LIMIT;
359
0
}
360
361
362
U_CAPI int32_t U_EXPORT2
363
uspoof_check(const USpoofChecker *sc,
364
             const UChar *id, int32_t length,
365
             int32_t *position,
366
0
             UErrorCode *status) {
367
0
368
0
    // Backwards compatibility:
369
0
    if (position != NULL) {
370
0
        *position = 0;
371
0
    }
372
0
373
0
    // Delegate to uspoof_check2
374
0
    return uspoof_check2(sc, id, length, NULL, status);
375
0
}
376
377
378
U_CAPI int32_t U_EXPORT2
379
uspoof_check2(const USpoofChecker *sc,
380
    const UChar* id, int32_t length,
381
    USpoofCheckResult* checkResult,
382
0
    UErrorCode *status) {
383
0
384
0
    const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
385
0
    if (This == NULL) {
386
0
        return 0;
387
0
    }
388
0
    if (length < -1) {
389
0
        *status = U_ILLEGAL_ARGUMENT_ERROR;
390
0
        return 0;
391
0
    }
392
0
    UnicodeString idStr((length == -1), id, length);  // Aliasing constructor.
393
0
    int32_t result = uspoof_check2UnicodeString(sc, idStr, checkResult, status);
394
0
    return result;
395
0
}
396
397
398
U_CAPI int32_t U_EXPORT2
399
uspoof_checkUTF8(const USpoofChecker *sc,
400
                 const char *id, int32_t length,
401
                 int32_t *position,
402
0
                 UErrorCode *status) {
403
0
404
0
    // Backwards compatibility:
405
0
    if (position != NULL) {
406
0
        *position = 0;
407
0
    }
408
0
409
0
    // Delegate to uspoof_check2
410
0
    return uspoof_check2UTF8(sc, id, length, NULL, status);
411
0
}
412
413
414
U_CAPI int32_t U_EXPORT2
415
uspoof_check2UTF8(const USpoofChecker *sc,
416
    const char *id, int32_t length,
417
    USpoofCheckResult* checkResult,
418
0
    UErrorCode *status) {
419
0
420
0
    if (U_FAILURE(*status)) {
421
0
        return 0;
422
0
    }
423
0
    UnicodeString idStr = UnicodeString::fromUTF8(StringPiece(id, length>=0 ? length : static_cast<int32_t>(uprv_strlen(id))));
424
0
    int32_t result = uspoof_check2UnicodeString(sc, idStr, checkResult, status);
425
0
    return result;
426
0
}
427
428
429
U_CAPI int32_t U_EXPORT2
430
uspoof_areConfusable(const USpoofChecker *sc,
431
                     const UChar *id1, int32_t length1,
432
                     const UChar *id2, int32_t length2,
433
0
                     UErrorCode *status) {
434
0
    SpoofImpl::validateThis(sc, *status);
435
0
    if (U_FAILURE(*status)) {
436
0
        return 0;
437
0
    }
438
0
    if (length1 < -1 || length2 < -1) {
439
0
        *status = U_ILLEGAL_ARGUMENT_ERROR;
440
0
        return 0;
441
0
    }
442
0
        
443
0
    UnicodeString id1Str((length1==-1), id1, length1);  // Aliasing constructor
444
0
    UnicodeString id2Str((length2==-1), id2, length2);  // Aliasing constructor
445
0
    return uspoof_areConfusableUnicodeString(sc, id1Str, id2Str, status);
446
0
}
447
448
449
U_CAPI int32_t U_EXPORT2
450
uspoof_areConfusableUTF8(const USpoofChecker *sc,
451
                         const char *id1, int32_t length1,
452
                         const char *id2, int32_t length2,
453
0
                         UErrorCode *status) {
454
0
    SpoofImpl::validateThis(sc, *status);
455
0
    if (U_FAILURE(*status)) {
456
0
        return 0;
457
0
    }
458
0
    if (length1 < -1 || length2 < -1) {
459
0
        *status = U_ILLEGAL_ARGUMENT_ERROR;
460
0
        return 0;
461
0
    }
462
0
    UnicodeString id1Str = UnicodeString::fromUTF8(StringPiece(id1, length1>=0? length1 : static_cast<int32_t>(uprv_strlen(id1))));
463
0
    UnicodeString id2Str = UnicodeString::fromUTF8(StringPiece(id2, length2>=0? length2 : static_cast<int32_t>(uprv_strlen(id2))));
464
0
    int32_t results = uspoof_areConfusableUnicodeString(sc, id1Str, id2Str, status);
465
0
    return results;
466
0
}
467
 
468
469
U_CAPI int32_t U_EXPORT2
470
uspoof_areConfusableUnicodeString(const USpoofChecker *sc,
471
                                  const icu::UnicodeString &id1,
472
                                  const icu::UnicodeString &id2,
473
0
                                  UErrorCode *status) {
474
0
    const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
475
0
    if (U_FAILURE(*status)) {
476
0
        return 0;
477
0
    }
478
0
    //
479
0
    // See section 4 of UAX 39 for the algorithm for checking whether two strings are confusable,
480
0
    //   and for definitions of the types (single, whole, mixed-script) of confusables.
481
0
    
482
0
    // We only care about a few of the check flags.  Ignore the others.
483
0
    // If no tests relavant to this function have been specified, return an error.
484
0
    // TODO:  is this really the right thing to do?  It's probably an error on the caller's part,
485
0
    //        but logically we would just return 0 (no error).
486
0
    if ((This->fChecks & USPOOF_CONFUSABLE) == 0) {
487
0
        *status = U_INVALID_STATE_ERROR;
488
0
        return 0;
489
0
    }
490
0
491
0
    // Compute the skeletons and check for confusability.
492
0
    UnicodeString id1Skeleton;
493
0
    uspoof_getSkeletonUnicodeString(sc, 0 /* deprecated */, id1, id1Skeleton, status);
494
0
    UnicodeString id2Skeleton;
495
0
    uspoof_getSkeletonUnicodeString(sc, 0 /* deprecated */, id2, id2Skeleton, status);
496
0
    if (U_FAILURE(*status)) { return 0; }
497
0
    if (id1Skeleton != id2Skeleton) {
498
0
        return 0;
499
0
    }
500
0
501
0
    // If we get here, the strings are confusable.  Now we just need to set the flags for the appropriate classes
502
0
    // of confusables according to UTS 39 section 4.
503
0
    // Start by computing the resolved script sets of id1 and id2.
504
0
    ScriptSet id1RSS;
505
0
    This->getResolvedScriptSet(id1, id1RSS, *status);
506
0
    ScriptSet id2RSS;
507
0
    This->getResolvedScriptSet(id2, id2RSS, *status);
508
0
509
0
    // Turn on all applicable flags
510
0
    int32_t result = 0;
511
0
    if (id1RSS.intersects(id2RSS)) {
512
0
        result |= USPOOF_SINGLE_SCRIPT_CONFUSABLE;
513
0
    } else {
514
0
        result |= USPOOF_MIXED_SCRIPT_CONFUSABLE;
515
0
        if (!id1RSS.isEmpty() && !id2RSS.isEmpty()) {
516
0
            result |= USPOOF_WHOLE_SCRIPT_CONFUSABLE;
517
0
        }
518
0
    }
519
0
520
0
    // Turn off flags that the user doesn't want
521
0
    if ((This->fChecks & USPOOF_SINGLE_SCRIPT_CONFUSABLE) == 0) {
522
0
        result &= ~USPOOF_SINGLE_SCRIPT_CONFUSABLE;
523
0
    }
524
0
    if ((This->fChecks & USPOOF_MIXED_SCRIPT_CONFUSABLE) == 0) {
525
0
        result &= ~USPOOF_MIXED_SCRIPT_CONFUSABLE;
526
0
    }
527
0
    if ((This->fChecks & USPOOF_WHOLE_SCRIPT_CONFUSABLE) == 0) {
528
0
        result &= ~USPOOF_WHOLE_SCRIPT_CONFUSABLE;
529
0
    }
530
0
531
0
    return result;
532
0
}
533
534
535
U_CAPI int32_t U_EXPORT2
536
uspoof_checkUnicodeString(const USpoofChecker *sc,
537
                          const icu::UnicodeString &id,
538
                          int32_t *position,
539
0
                          UErrorCode *status) {
540
0
541
0
    // Backwards compatibility:
542
0
    if (position != NULL) {
543
0
        *position = 0;
544
0
    }
545
0
546
0
    // Delegate to uspoof_check2
547
0
    return uspoof_check2UnicodeString(sc, id, NULL, status);
548
0
}
549
550
0
int32_t checkImpl(const SpoofImpl* This, const UnicodeString& id, CheckResult* checkResult, UErrorCode* status) {
551
0
    U_ASSERT(This != NULL);
552
0
    U_ASSERT(checkResult != NULL);
553
0
    checkResult->clear();
554
0
    int32_t result = 0;
555
0
556
0
    if (0 != (This->fChecks & USPOOF_RESTRICTION_LEVEL)) {
557
0
        URestrictionLevel idRestrictionLevel = This->getRestrictionLevel(id, *status);
558
0
        if (idRestrictionLevel > This->fRestrictionLevel) {
559
0
            result |= USPOOF_RESTRICTION_LEVEL;
560
0
        }
561
0
        checkResult->fRestrictionLevel = idRestrictionLevel;
562
0
    }
563
0
564
0
    if (0 != (This->fChecks & USPOOF_MIXED_NUMBERS)) {
565
0
        UnicodeSet numerics;
566
0
        This->getNumerics(id, numerics, *status);
567
0
        if (numerics.size() > 1) {
568
0
            result |= USPOOF_MIXED_NUMBERS;
569
0
        }
570
0
        checkResult->fNumerics = numerics;  // UnicodeSet::operator=
571
0
    }
572
0
573
0
    if (0 != (This->fChecks & USPOOF_HIDDEN_OVERLAY)) {
574
0
        int32_t index = This->findHiddenOverlay(id, *status);
575
0
        if (index != -1) {
576
0
            result |= USPOOF_HIDDEN_OVERLAY;
577
0
        }
578
0
    }
579
0
580
0
581
0
    if (0 != (This->fChecks & USPOOF_CHAR_LIMIT)) {
582
0
        int32_t i;
583
0
        UChar32 c;
584
0
        int32_t length = id.length();
585
0
        for (i=0; i<length ;) {
586
0
            c = id.char32At(i);
587
0
            i += U16_LENGTH(c);
588
0
            if (!This->fAllowedCharsSet->contains(c)) {
589
0
                result |= USPOOF_CHAR_LIMIT;
590
0
                break;
591
0
            }
592
0
        }
593
0
    }
594
0
595
0
    if (0 != (This->fChecks & USPOOF_INVISIBLE)) {
596
0
        // This check needs to be done on NFD input
597
0
        UnicodeString nfdText;
598
0
        gNfdNormalizer->normalize(id, nfdText, *status);
599
0
        int32_t nfdLength = nfdText.length();
600
0
601
0
        // scan for more than one occurence of the same non-spacing mark
602
0
        // in a sequence of non-spacing marks.
603
0
        int32_t     i;
604
0
        UChar32     c;
605
0
        UChar32     firstNonspacingMark = 0;
606
0
        UBool       haveMultipleMarks = FALSE;  
607
0
        UnicodeSet  marksSeenSoFar;   // Set of combining marks in a single combining sequence.
608
0
        
609
0
        for (i=0; i<nfdLength ;) {
610
0
            c = nfdText.char32At(i);
611
0
            i += U16_LENGTH(c);
612
0
            if (u_charType(c) != U_NON_SPACING_MARK) {
613
0
                firstNonspacingMark = 0;
614
0
                if (haveMultipleMarks) {
615
0
                    marksSeenSoFar.clear();
616
0
                    haveMultipleMarks = FALSE;
617
0
                }
618
0
                continue;
619
0
            }
620
0
            if (firstNonspacingMark == 0) {
621
0
                firstNonspacingMark = c;
622
0
                continue;
623
0
            }
624
0
            if (!haveMultipleMarks) {
625
0
                marksSeenSoFar.add(firstNonspacingMark);
626
0
                haveMultipleMarks = TRUE;
627
0
            }
628
0
            if (marksSeenSoFar.contains(c)) {
629
0
                // report the error, and stop scanning.
630
0
                // No need to find more than the first failure.
631
0
                result |= USPOOF_INVISIBLE;
632
0
                break;
633
0
            }
634
0
            marksSeenSoFar.add(c);
635
0
        }
636
0
    }
637
0
638
0
    checkResult->fChecks = result;
639
0
    return checkResult->toCombinedBitmask(This->fChecks);
640
0
}
641
642
U_CAPI int32_t U_EXPORT2
643
uspoof_check2UnicodeString(const USpoofChecker *sc,
644
                          const icu::UnicodeString &id,
645
                          USpoofCheckResult* checkResult,
646
0
                          UErrorCode *status) {
647
0
    const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
648
0
    if (This == NULL) {
649
0
        return FALSE;
650
0
    }
651
0
652
0
    if (checkResult != NULL) {
653
0
        CheckResult* ThisCheckResult = CheckResult::validateThis(checkResult, *status);
654
0
        if (ThisCheckResult == NULL) {
655
0
            return FALSE;
656
0
        }
657
0
        return checkImpl(This, id, ThisCheckResult, status);
658
0
    } else {
659
0
        // Stack-allocate the checkResult since this method doesn't return it
660
0
        CheckResult stackCheckResult;
661
0
        return checkImpl(This, id, &stackCheckResult, status);
662
0
    }
663
0
}
664
665
666
U_CAPI int32_t U_EXPORT2
667
uspoof_getSkeleton(const USpoofChecker *sc,
668
                   uint32_t type,
669
                   const UChar *id,  int32_t length,
670
                   UChar *dest, int32_t destCapacity,
671
0
                   UErrorCode *status) {
672
0
673
0
    SpoofImpl::validateThis(sc, *status);
674
0
    if (U_FAILURE(*status)) {
675
0
        return 0;
676
0
    }
677
0
    if (length<-1 || destCapacity<0 || (destCapacity==0 && dest!=NULL)) {
678
0
        *status = U_ILLEGAL_ARGUMENT_ERROR;
679
0
        return 0;
680
0
    }
681
0
682
0
    UnicodeString idStr((length==-1), id, length);  // Aliasing constructor
683
0
    UnicodeString destStr;
684
0
    uspoof_getSkeletonUnicodeString(sc, type, idStr, destStr, status);
685
0
    destStr.extract(dest, destCapacity, *status);
686
0
    return destStr.length();
687
0
}
688
689
690
691
U_I18N_API UnicodeString &  U_EXPORT2
692
uspoof_getSkeletonUnicodeString(const USpoofChecker *sc,
693
                                uint32_t /*type*/,
694
                                const UnicodeString &id,
695
                                UnicodeString &dest,
696
0
                                UErrorCode *status) {
697
0
    const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
698
0
    if (U_FAILURE(*status)) {
699
0
        return dest;
700
0
    }
701
0
702
0
    UnicodeString nfdId;
703
0
    gNfdNormalizer->normalize(id, nfdId, *status);
704
0
705
0
    // Apply the skeleton mapping to the NFD normalized input string
706
0
    // Accumulate the skeleton, possibly unnormalized, in a UnicodeString.
707
0
    int32_t inputIndex = 0;
708
0
    UnicodeString skelStr;
709
0
    int32_t normalizedLen = nfdId.length();
710
0
    for (inputIndex=0; inputIndex < normalizedLen; ) {
711
0
        UChar32 c = nfdId.char32At(inputIndex);
712
0
        inputIndex += U16_LENGTH(c);
713
0
        This->fSpoofData->confusableLookup(c, skelStr);
714
0
    }
715
0
716
0
    gNfdNormalizer->normalize(skelStr, dest, *status);
717
0
    return dest;
718
0
}
719
720
721
U_CAPI int32_t U_EXPORT2
722
uspoof_getSkeletonUTF8(const USpoofChecker *sc,
723
                       uint32_t type,
724
                       const char *id,  int32_t length,
725
                       char *dest, int32_t destCapacity,
726
0
                       UErrorCode *status) {
727
0
    SpoofImpl::validateThis(sc, *status);
728
0
    if (U_FAILURE(*status)) {
729
0
        return 0;
730
0
    }
731
0
    if (length<-1 || destCapacity<0 || (destCapacity==0 && dest!=NULL)) {
732
0
        *status = U_ILLEGAL_ARGUMENT_ERROR;
733
0
        return 0;
734
0
    }
735
0
736
0
    UnicodeString srcStr = UnicodeString::fromUTF8(StringPiece(id, length>=0 ? length : static_cast<int32_t>(uprv_strlen(id))));
737
0
    UnicodeString destStr;
738
0
    uspoof_getSkeletonUnicodeString(sc, type, srcStr, destStr, status);
739
0
    if (U_FAILURE(*status)) {
740
0
        return 0;
741
0
    }
742
0
743
0
    int32_t lengthInUTF8 = 0;
744
0
    u_strToUTF8(dest, destCapacity, &lengthInUTF8,
745
0
                destStr.getBuffer(), destStr.length(), status);
746
0
    return lengthInUTF8;
747
0
}
748
749
750
U_CAPI int32_t U_EXPORT2
751
0
uspoof_serialize(USpoofChecker *sc,void *buf, int32_t capacity, UErrorCode *status) {
752
0
    SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
753
0
    if (This == NULL) {
754
0
        U_ASSERT(U_FAILURE(*status));
755
0
        return 0;
756
0
    }
757
0
758
0
    return This->fSpoofData->serialize(buf, capacity, *status);
759
0
}
760
761
U_CAPI const USet * U_EXPORT2
762
0
uspoof_getInclusionSet(UErrorCode *status) {
763
0
    umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
764
0
    return gInclusionSet->toUSet();
765
0
}
766
767
U_CAPI const USet * U_EXPORT2
768
0
uspoof_getRecommendedSet(UErrorCode *status) {
769
0
    umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
770
0
    return gRecommendedSet->toUSet();
771
0
}
772
773
U_I18N_API const UnicodeSet * U_EXPORT2
774
0
uspoof_getInclusionUnicodeSet(UErrorCode *status) {
775
0
    umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
776
0
    return gInclusionSet;
777
0
}
778
779
U_I18N_API const UnicodeSet * U_EXPORT2
780
0
uspoof_getRecommendedUnicodeSet(UErrorCode *status) {
781
0
    umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
782
0
    return gRecommendedSet;
783
0
}
784
785
//------------------
786
// CheckResult APIs
787
//------------------
788
789
U_CAPI USpoofCheckResult* U_EXPORT2
790
0
uspoof_openCheckResult(UErrorCode *status) {
791
0
    CheckResult* checkResult = new CheckResult();
792
0
    if (checkResult == NULL) {
793
0
        *status = U_MEMORY_ALLOCATION_ERROR;
794
0
        return NULL;
795
0
    }
796
0
    return checkResult->asUSpoofCheckResult();
797
0
}
798
799
U_CAPI void U_EXPORT2
800
0
uspoof_closeCheckResult(USpoofCheckResult* checkResult) {
801
0
    UErrorCode status = U_ZERO_ERROR;
802
0
    CheckResult* This = CheckResult::validateThis(checkResult, status);
803
0
    delete This;
804
0
}
805
806
U_CAPI int32_t U_EXPORT2
807
0
uspoof_getCheckResultChecks(const USpoofCheckResult *checkResult, UErrorCode *status) {
808
0
    const CheckResult* This = CheckResult::validateThis(checkResult, *status);
809
0
    if (U_FAILURE(*status)) { return 0; }
810
0
    return This->fChecks;
811
0
}
812
813
U_CAPI URestrictionLevel U_EXPORT2
814
0
uspoof_getCheckResultRestrictionLevel(const USpoofCheckResult *checkResult, UErrorCode *status) {
815
0
    const CheckResult* This = CheckResult::validateThis(checkResult, *status);
816
0
    if (U_FAILURE(*status)) { return USPOOF_UNRESTRICTIVE; }
817
0
    return This->fRestrictionLevel;
818
0
}
819
820
U_CAPI const USet* U_EXPORT2
821
0
uspoof_getCheckResultNumerics(const USpoofCheckResult *checkResult, UErrorCode *status) {
822
0
    const CheckResult* This = CheckResult::validateThis(checkResult, *status);
823
0
    if (U_FAILURE(*status)) { return NULL; }
824
0
    return This->fNumerics.toUSet();
825
0
}
826
827
828
829
#endif // !UCONFIG_NO_NORMALIZATION