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