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
35U_NAMESPACE_USE
36
37
38//
39// Static Objects used by the spoof impl, their thread safe initialization and their cleanup.
40//
41static UnicodeSet *gInclusionSet = NULL;
42static UnicodeSet *gRecommendedSet = NULL;
43static const Normalizer2 *gNfdNormalizer = NULL;
44static UInitOnce gSpoofInitStaticsOnce = U_INITONCE_INITIALIZER;
45
46namespace {
47
48UBool U_CALLCONV
49uspoof_cleanup(void) {
50 delete gInclusionSet;
51 gInclusionSet = NULL;
52 delete gRecommendedSet;
53 gRecommendedSet = NULL;
54 gNfdNormalizer = NULL;
55 gSpoofInitStaticsOnce.reset();
56 return TRUE;
57}
58
59void U_CALLCONV initializeStatics(UErrorCode &status) {
60 static const char16_t *inclusionPat =
61 u"['\\-.\\:\\u00B7\\u0375\\u058A\\u05F3\\u05F4\\u06FD\\u06FE\\u0F0B\\u200C"
62 u"\\u200D\\u2010\\u2019\\u2027\\u30A0\\u30FB]";
63 gInclusionSet = new UnicodeSet(UnicodeString(inclusionPat), status);
64 if (gInclusionSet == NULL) {
65 status = U_MEMORY_ALLOCATION_ERROR;
66 return;
67 }
68 gInclusionSet->freeze();
69
70 // Note: data from IdentifierStatus.txt & IdentifierType.txt
71 // There is tooling to generate this constant in the unicodetools project:
72 // org.unicode.text.tools.RecommendedSetGenerator
73 // It will print the Java and C++ code to the console for easy copy-paste into this file.
74 static const char16_t *recommendedPat =
75 u"[0-9A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u0131\\u0134-\\u013E"
76 u"\\u0141-\\u0148\\u014A-\\u017E\\u018F\\u01A0\\u01A1\\u01AF\\u01B0\\u01CD-"
77 u"\\u01DC\\u01DE-\\u01E3\\u01E6-\\u01F0\\u01F4\\u01F5\\u01F8-\\u021B\\u021E"
78 u"\\u021F\\u0226-\\u0233\\u0259\\u02BB\\u02BC\\u02EC\\u0300-\\u0304\\u0306-"
79 u"\\u030C\\u030F-\\u0311\\u0313\\u0314\\u031B\\u0323-\\u0328\\u032D\\u032E"
80 u"\\u0330\\u0331\\u0335\\u0338\\u0339\\u0342\\u0345\\u037B-\\u037D\\u0386"
81 u"\\u0388-\\u038A\\u038C\\u038E-\\u03A1\\u03A3-\\u03CE\\u03FC-\\u045F\\u048A-"
82 u"\\u04FF\\u0510-\\u0529\\u052E\\u052F\\u0531-\\u0556\\u0559\\u0561-\\u0586"
83 u"\\u05B4\\u05D0-\\u05EA\\u05EF-\\u05F2\\u0620-\\u063F\\u0641-\\u0655\\u0660-"
84 u"\\u0669\\u0670-\\u0672\\u0674\\u0679-\\u068D\\u068F-\\u06A0\\u06A2-\\u06D3"
85 u"\\u06D5\\u06E5\\u06E6\\u06EE-\\u06FC\\u06FF\\u0750-\\u07B1\\u08A0-\\u08AC"
86 u"\\u08B2\\u08B6-\\u08C7\\u0901-\\u094D\\u094F\\u0950\\u0956\\u0957\\u0960-"
87 u"\\u0963\\u0966-\\u096F\\u0971-\\u0977\\u0979-\\u097F\\u0981-\\u0983\\u0985-"
88 u"\\u098C\\u098F\\u0990\\u0993-\\u09A8\\u09AA-\\u09B0\\u09B2\\u09B6-\\u09B9"
89 u"\\u09BC-\\u09C4\\u09C7\\u09C8\\u09CB-\\u09CE\\u09D7\\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FAB"
117 u"\\u0FAD-\\u0FB8\\u0FBA-\\u0FBC\\u0FC6\\u1000-\\u1049\\u1050-\\u109D\\u10C7"
118 u"\\u10CD\\u10D0-\\u10F0\\u10F7-\\u10FA\\u10FD-\\u10FF\\u1200-\\u1248\\u124A-"
119 u"\\u124D\\u1250-\\u1256\\u1258\\u125A-\\u125D\\u1260-\\u1288\\u128A-\\u128D"
120 u"\\u1290-\\u12B0\\u12B2-\\u12B5\\u12B8-\\u12BE\\u12C0\\u12C2-\\u12C5\\u12C8-"
121 u"\\u12D6\\u12D8-\\u1310\\u1312-\\u1315\\u1318-\\u135A\\u135D-\\u135F\\u1380-"
122 u"\\u138F\\u1780-\\u17A2\\u17A5-\\u17A7\\u17A9-\\u17B3\\u17B6-\\u17CA\\u17D2"
123 u"\\u17D7\\u17DC\\u17E0-\\u17E9\\u1ABF\\u1AC0\\u1C90-\\u1CBA\\u1CBD-\\u1CBF"
124 u"\\u1E00-\\u1E99\\u1E9E\\u1EA0-\\u1EF9\\u1F00-\\u1F15\\u1F18-\\u1F1D\\u1F20-"
125 u"\\u1F45\\u1F48-\\u1F4D\\u1F50-\\u1F57\\u1F59\\u1F5B\\u1F5D\\u1F5F-\\u1F70"
126 u"\\u1F72\\u1F74\\u1F76\\u1F78\\u1F7A\\u1F7C\\u1F80-\\u1FB4\\u1FB6-\\u1FBA"
127 u"\\u1FBC\\u1FC2-\\u1FC4\\u1FC6-\\u1FC8\\u1FCA\\u1FCC\\u1FD0-\\u1FD2\\u1FD6-"
128 u"\\u1FDA\\u1FE0-\\u1FE2\\u1FE4-\\u1FEA\\u1FEC\\u1FF2-\\u1FF4\\u1FF6-\\u1FF8"
129 u"\\u1FFA\\u1FFC\\u2D27\\u2D2D\\u2D80-\\u2D96\\u2DA0-\\u2DA6\\u2DA8-\\u2DAE"
130 u"\\u2DB0-\\u2DB6\\u2DB8-\\u2DBE\\u2DC0-\\u2DC6\\u2DC8-\\u2DCE\\u2DD0-\\u2DD6"
131 u"\\u2DD8-\\u2DDE\\u3005-\\u3007\\u3041-\\u3096\\u3099\\u309A\\u309D\\u309E"
132 u"\\u30A1-\\u30FA\\u30FC-\\u30FE\\u3105-\\u312D\\u312F\\u31A0-\\u31BF\\u3400-"
133 u"\\u4DBF\\u4E00-\\u9FFC\\uA67F\\uA717-\\uA71F\\uA788\\uA78D\\uA792\\uA793"
134 u"\\uA7AA\\uA7AE\\uA7B8\\uA7B9\\uA7C2-\\uA7CA\\uA7F5\\uA7F6\\uA9E7-\\uA9FE"
135 u"\\uAA60-\\uAA76\\uAA7A-\\uAA7F\\uAB01-\\uAB06\\uAB09-\\uAB0E\\uAB11-\\uAB16"
136 u"\\uAB20-\\uAB26\\uAB28-\\uAB2E\\uAB66-\\uAB68\\uAC00-\\uD7A3\\uFA0E\\uFA0F"
137 u"\\uFA11\\uFA13\\uFA14\\uFA1F\\uFA21\\uFA23\\uFA24\\uFA27-\\uFA29\\U00011301"
138 u"\\U00011303\\U0001133B\\U0001133C\\U00016FF0\\U00016FF1\\U0001B150-"
139 u"\\U0001B152\\U0001B164-\\U0001B167\\U00020000-\\U0002A6DD\\U0002A700-"
140 u"\\U0002B734\\U0002B740-\\U0002B81D\\U0002B820-\\U0002CEA1\\U0002CEB0-"
141 u"\\U0002EBE0\\U00030000-\\U0003134A]";
142
143 gRecommendedSet = new UnicodeSet(UnicodeString(recommendedPat), status);
144 if (gRecommendedSet == NULL) {
145 status = U_MEMORY_ALLOCATION_ERROR;
146 delete gInclusionSet;
147 return;
148 }
149 gRecommendedSet->freeze();
150 gNfdNormalizer = Normalizer2::getNFDInstance(status);
151 ucln_i18n_registerCleanup(UCLN_I18N_SPOOF, uspoof_cleanup);
152}
153
154} // namespace
155
156U_CFUNC void uspoof_internalInitStatics(UErrorCode *status) {
157 umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
158}
159
160U_CAPI USpoofChecker * U_EXPORT2
161uspoof_open(UErrorCode *status) {
162 umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
163 if (U_FAILURE(*status)) {
164 return NULL;
165 }
166 SpoofImpl *si = new SpoofImpl(*status);
167 if (si == NULL) {
168 *status = U_MEMORY_ALLOCATION_ERROR;
169 return NULL;
170 }
171 if (U_FAILURE(*status)) {
172 delete si;
173 return NULL;
174 }
175 return si->asUSpoofChecker();
176}
177
178
179U_CAPI USpoofChecker * U_EXPORT2
180uspoof_openFromSerialized(const void *data, int32_t length, int32_t *pActualLength,
181 UErrorCode *status) {
182 if (U_FAILURE(*status)) {
183 return NULL;
184 }
185
186 if (data == NULL) {
187 *status = U_ILLEGAL_ARGUMENT_ERROR;
188 return NULL;
189 }
190
191 umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
192 if (U_FAILURE(*status))
193 {
194 return NULL;
195 }
196
197 SpoofData *sd = new SpoofData(data, length, *status);
198 if (sd == NULL) {
199 *status = U_MEMORY_ALLOCATION_ERROR;
200 return NULL;
201 }
202
203 if (U_FAILURE(*status)) {
204 delete sd;
205 return NULL;
206 }
207
208 SpoofImpl *si = new SpoofImpl(sd, *status);
209 if (si == NULL) {
210 *status = U_MEMORY_ALLOCATION_ERROR;
211 delete sd; // explicit delete as the destructor for si won't be called.
212 return NULL;
213 }
214
215 if (U_FAILURE(*status)) {
216 delete si; // no delete for sd, as the si destructor will delete it.
217 return NULL;
218 }
219
220 if (pActualLength != NULL) {
221 *pActualLength = sd->size();
222 }
223 return si->asUSpoofChecker();
224}
225
226
227U_CAPI USpoofChecker * U_EXPORT2
228uspoof_clone(const USpoofChecker *sc, UErrorCode *status) {
229 const SpoofImpl *src = SpoofImpl::validateThis(sc, *status);
230 if (src == NULL) {
231 return NULL;
232 }
233 SpoofImpl *result = new SpoofImpl(*src, *status); // copy constructor
234 if (result == NULL) {
235 *status = U_MEMORY_ALLOCATION_ERROR;
236 return NULL;
237 }
238 if (U_FAILURE(*status)) {
239 delete result;
240 result = NULL;
241 }
242 return result->asUSpoofChecker();
243}
244
245
246U_CAPI void U_EXPORT2
247uspoof_close(USpoofChecker *sc) {
248 UErrorCode status = U_ZERO_ERROR;
249 SpoofImpl *This = SpoofImpl::validateThis(sc, status);
250 delete This;
251}
252
253
254U_CAPI void U_EXPORT2
255uspoof_setChecks(USpoofChecker *sc, int32_t checks, UErrorCode *status) {
256 SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
257 if (This == NULL) {
258 return;
259 }
260
261 // Verify that the requested checks are all ones (bits) that
262 // are acceptable, known values.
263 if (checks & ~(USPOOF_ALL_CHECKS | USPOOF_AUX_INFO)) {
264 *status = U_ILLEGAL_ARGUMENT_ERROR;
265 return;
266 }
267
268 This->fChecks = checks;
269}
270
271
272U_CAPI int32_t U_EXPORT2
273uspoof_getChecks(const USpoofChecker *sc, UErrorCode *status) {
274 const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
275 if (This == NULL) {
276 return 0;
277 }
278 return This->fChecks;
279}
280
281U_CAPI void U_EXPORT2
282uspoof_setRestrictionLevel(USpoofChecker *sc, URestrictionLevel restrictionLevel) {
283 UErrorCode status = U_ZERO_ERROR;
284 SpoofImpl *This = SpoofImpl::validateThis(sc, status);
285 if (This != NULL) {
286 This->fRestrictionLevel = restrictionLevel;
287 This->fChecks |= USPOOF_RESTRICTION_LEVEL;
288 }
289}
290
291U_CAPI URestrictionLevel U_EXPORT2
292uspoof_getRestrictionLevel(const USpoofChecker *sc) {
293 UErrorCode status = U_ZERO_ERROR;
294 const SpoofImpl *This = SpoofImpl::validateThis(sc, status);
295 if (This == NULL) {
296 return USPOOF_UNRESTRICTIVE;
297 }
298 return This->fRestrictionLevel;
299}
300
301U_CAPI void U_EXPORT2
302uspoof_setAllowedLocales(USpoofChecker *sc, const char *localesList, UErrorCode *status) {
303 SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
304 if (This == NULL) {
305 return;
306 }
307 This->setAllowedLocales(localesList, *status);
308}
309
310U_CAPI const char * U_EXPORT2
311uspoof_getAllowedLocales(USpoofChecker *sc, UErrorCode *status) {
312 SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
313 if (This == NULL) {
314 return NULL;
315 }
316 return This->getAllowedLocales(*status);
317}
318
319
320U_CAPI const USet * U_EXPORT2
321uspoof_getAllowedChars(const USpoofChecker *sc, UErrorCode *status) {
322 const UnicodeSet *result = uspoof_getAllowedUnicodeSet(sc, status);
323 return result->toUSet();
324}
325
326U_CAPI const UnicodeSet * U_EXPORT2
327uspoof_getAllowedUnicodeSet(const USpoofChecker *sc, UErrorCode *status) {
328 const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
329 if (This == NULL) {
330 return NULL;
331 }
332 return This->fAllowedCharsSet;
333}
334
335
336U_CAPI void U_EXPORT2
337uspoof_setAllowedChars(USpoofChecker *sc, const USet *chars, UErrorCode *status) {
338 const UnicodeSet *set = UnicodeSet::fromUSet(chars);
339 uspoof_setAllowedUnicodeSet(sc, set, status);
340}
341
342
343U_CAPI void U_EXPORT2
344uspoof_setAllowedUnicodeSet(USpoofChecker *sc, const UnicodeSet *chars, UErrorCode *status) {
345 SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
346 if (This == NULL) {
347 return;
348 }
349 if (chars->isBogus()) {
350 *status = U_ILLEGAL_ARGUMENT_ERROR;
351 return;
352 }
353 UnicodeSet *clonedSet = chars->clone();
354 if (clonedSet == NULL || clonedSet->isBogus()) {
355 *status = U_MEMORY_ALLOCATION_ERROR;
356 return;
357 }
358 clonedSet->freeze();
359 delete This->fAllowedCharsSet;
360 This->fAllowedCharsSet = clonedSet;
361 This->fChecks |= USPOOF_CHAR_LIMIT;
362}
363
364
365U_CAPI int32_t U_EXPORT2
366uspoof_check(const USpoofChecker *sc,
367 const UChar *id, int32_t length,
368 int32_t *position,
369 UErrorCode *status) {
370
371 // Backwards compatibility:
372 if (position != NULL) {
373 *position = 0;
374 }
375
376 // Delegate to uspoof_check2
377 return uspoof_check2(sc, id, length, NULL, status);
378}
379
380
381U_CAPI int32_t U_EXPORT2
382uspoof_check2(const USpoofChecker *sc,
383 const UChar* id, int32_t length,
384 USpoofCheckResult* checkResult,
385 UErrorCode *status) {
386
387 const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
388 if (This == NULL) {
389 return 0;
390 }
391 if (length < -1) {
392 *status = U_ILLEGAL_ARGUMENT_ERROR;
393 return 0;
394 }
395 UnicodeString idStr((length == -1), id, length); // Aliasing constructor.
396 int32_t result = uspoof_check2UnicodeString(sc, idStr, checkResult, status);
397 return result;
398}
399
400
401U_CAPI int32_t U_EXPORT2
402uspoof_checkUTF8(const USpoofChecker *sc,
403 const char *id, int32_t length,
404 int32_t *position,
405 UErrorCode *status) {
406
407 // Backwards compatibility:
408 if (position != NULL) {
409 *position = 0;
410 }
411
412 // Delegate to uspoof_check2
413 return uspoof_check2UTF8(sc, id, length, NULL, status);
414}
415
416
417U_CAPI int32_t U_EXPORT2
418uspoof_check2UTF8(const USpoofChecker *sc,
419 const char *id, int32_t length,
420 USpoofCheckResult* checkResult,
421 UErrorCode *status) {
422
423 if (U_FAILURE(*status)) {
424 return 0;
425 }
426 UnicodeString idStr = UnicodeString::fromUTF8(StringPiece(id, length>=0 ? length : static_cast<int32_t>(uprv_strlen(id))));
427 int32_t result = uspoof_check2UnicodeString(sc, idStr, checkResult, status);
428 return result;
429}
430
431
432U_CAPI int32_t U_EXPORT2
433uspoof_areConfusable(const USpoofChecker *sc,
434 const UChar *id1, int32_t length1,
435 const UChar *id2, int32_t length2,
436 UErrorCode *status) {
437 SpoofImpl::validateThis(sc, *status);
438 if (U_FAILURE(*status)) {
439 return 0;
440 }
441 if (length1 < -1 || length2 < -1) {
442 *status = U_ILLEGAL_ARGUMENT_ERROR;
443 return 0;
444 }
445
446 UnicodeString id1Str((length1==-1), id1, length1); // Aliasing constructor
447 UnicodeString id2Str((length2==-1), id2, length2); // Aliasing constructor
448 return uspoof_areConfusableUnicodeString(sc, id1Str, id2Str, status);
449}
450
451
452U_CAPI int32_t U_EXPORT2
453uspoof_areConfusableUTF8(const USpoofChecker *sc,
454 const char *id1, int32_t length1,
455 const char *id2, int32_t length2,
456 UErrorCode *status) {
457 SpoofImpl::validateThis(sc, *status);
458 if (U_FAILURE(*status)) {
459 return 0;
460 }
461 if (length1 < -1 || length2 < -1) {
462 *status = U_ILLEGAL_ARGUMENT_ERROR;
463 return 0;
464 }
465 UnicodeString id1Str = UnicodeString::fromUTF8(StringPiece(id1, length1>=0? length1 : static_cast<int32_t>(uprv_strlen(id1))));
466 UnicodeString id2Str = UnicodeString::fromUTF8(StringPiece(id2, length2>=0? length2 : static_cast<int32_t>(uprv_strlen(id2))));
467 int32_t results = uspoof_areConfusableUnicodeString(sc, id1Str, id2Str, status);
468 return results;
469}
470
471
472U_CAPI int32_t U_EXPORT2
473uspoof_areConfusableUnicodeString(const USpoofChecker *sc,
474 const icu::UnicodeString &id1,
475 const icu::UnicodeString &id2,
476 UErrorCode *status) {
477 const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
478 if (U_FAILURE(*status)) {
479 return 0;
480 }
481 //
482 // See section 4 of UAX 39 for the algorithm for checking whether two strings are confusable,
483 // and for definitions of the types (single, whole, mixed-script) of confusables.
484
485 // We only care about a few of the check flags. Ignore the others.
486 // If no tests relavant to this function have been specified, return an error.
487 // TODO: is this really the right thing to do? It's probably an error on the caller's part,
488 // but logically we would just return 0 (no error).
489 if ((This->fChecks & USPOOF_CONFUSABLE) == 0) {
490 *status = U_INVALID_STATE_ERROR;
491 return 0;
492 }
493
494 // Compute the skeletons and check for confusability.
495 UnicodeString id1Skeleton;
496 uspoof_getSkeletonUnicodeString(sc, 0 /* deprecated */, id1, id1Skeleton, status);
497 UnicodeString id2Skeleton;
498 uspoof_getSkeletonUnicodeString(sc, 0 /* deprecated */, id2, id2Skeleton, status);
499 if (U_FAILURE(*status)) { return 0; }
500 if (id1Skeleton != id2Skeleton) {
501 return 0;
502 }
503
504 // If we get here, the strings are confusable. Now we just need to set the flags for the appropriate classes
505 // of confusables according to UTS 39 section 4.
506 // Start by computing the resolved script sets of id1 and id2.
507 ScriptSet id1RSS;
508 This->getResolvedScriptSet(id1, id1RSS, *status);
509 ScriptSet id2RSS;
510 This->getResolvedScriptSet(id2, id2RSS, *status);
511
512 // Turn on all applicable flags
513 int32_t result = 0;
514 if (id1RSS.intersects(id2RSS)) {
515 result |= USPOOF_SINGLE_SCRIPT_CONFUSABLE;
516 } else {
517 result |= USPOOF_MIXED_SCRIPT_CONFUSABLE;
518 if (!id1RSS.isEmpty() && !id2RSS.isEmpty()) {
519 result |= USPOOF_WHOLE_SCRIPT_CONFUSABLE;
520 }
521 }
522
523 // Turn off flags that the user doesn't want
524 if ((This->fChecks & USPOOF_SINGLE_SCRIPT_CONFUSABLE) == 0) {
525 result &= ~USPOOF_SINGLE_SCRIPT_CONFUSABLE;
526 }
527 if ((This->fChecks & USPOOF_MIXED_SCRIPT_CONFUSABLE) == 0) {
528 result &= ~USPOOF_MIXED_SCRIPT_CONFUSABLE;
529 }
530 if ((This->fChecks & USPOOF_WHOLE_SCRIPT_CONFUSABLE) == 0) {
531 result &= ~USPOOF_WHOLE_SCRIPT_CONFUSABLE;
532 }
533
534 return result;
535}
536
537
538U_CAPI int32_t U_EXPORT2
539uspoof_checkUnicodeString(const USpoofChecker *sc,
540 const icu::UnicodeString &id,
541 int32_t *position,
542 UErrorCode *status) {
543
544 // Backwards compatibility:
545 if (position != NULL) {
546 *position = 0;
547 }
548
549 // Delegate to uspoof_check2
550 return uspoof_check2UnicodeString(sc, id, NULL, status);
551}
552
553namespace {
554
555int32_t checkImpl(const SpoofImpl* This, const UnicodeString& id, CheckResult* checkResult, UErrorCode* status) {
556 U_ASSERT(This != NULL);
557 U_ASSERT(checkResult != NULL);
558 checkResult->clear();
559 int32_t result = 0;
560
561 if (0 != (This->fChecks & USPOOF_RESTRICTION_LEVEL)) {
562 URestrictionLevel idRestrictionLevel = This->getRestrictionLevel(id, *status);
563 if (idRestrictionLevel > This->fRestrictionLevel) {
564 result |= USPOOF_RESTRICTION_LEVEL;
565 }
566 checkResult->fRestrictionLevel = idRestrictionLevel;
567 }
568
569 if (0 != (This->fChecks & USPOOF_MIXED_NUMBERS)) {
570 UnicodeSet numerics;
571 This->getNumerics(id, numerics, *status);
572 if (numerics.size() > 1) {
573 result |= USPOOF_MIXED_NUMBERS;
574 }
575 checkResult->fNumerics = numerics; // UnicodeSet::operator=
576 }
577
578 if (0 != (This->fChecks & USPOOF_HIDDEN_OVERLAY)) {
579 int32_t index = This->findHiddenOverlay(id, *status);
580 if (index != -1) {
581 result |= USPOOF_HIDDEN_OVERLAY;
582 }
583 }
584
585
586 if (0 != (This->fChecks & USPOOF_CHAR_LIMIT)) {
587 int32_t i;
588 UChar32 c;
589 int32_t length = id.length();
590 for (i=0; i<length ;) {
591 c = id.char32At(i);
592 i += U16_LENGTH(c);
593 if (!This->fAllowedCharsSet->contains(c)) {
594 result |= USPOOF_CHAR_LIMIT;
595 break;
596 }
597 }
598 }
599
600 if (0 != (This->fChecks & USPOOF_INVISIBLE)) {
601 // This check needs to be done on NFD input
602 UnicodeString nfdText;
603 gNfdNormalizer->normalize(id, nfdText, *status);
604 int32_t nfdLength = nfdText.length();
605
606 // scan for more than one occurence of the same non-spacing mark
607 // in a sequence of non-spacing marks.
608 int32_t i;
609 UChar32 c;
610 UChar32 firstNonspacingMark = 0;
611 UBool haveMultipleMarks = FALSE;
612 UnicodeSet marksSeenSoFar; // Set of combining marks in a single combining sequence.
613
614 for (i=0; i<nfdLength ;) {
615 c = nfdText.char32At(i);
616 i += U16_LENGTH(c);
617 if (u_charType(c) != U_NON_SPACING_MARK) {
618 firstNonspacingMark = 0;
619 if (haveMultipleMarks) {
620 marksSeenSoFar.clear();
621 haveMultipleMarks = FALSE;
622 }
623 continue;
624 }
625 if (firstNonspacingMark == 0) {
626 firstNonspacingMark = c;
627 continue;
628 }
629 if (!haveMultipleMarks) {
630 marksSeenSoFar.add(firstNonspacingMark);
631 haveMultipleMarks = TRUE;
632 }
633 if (marksSeenSoFar.contains(c)) {
634 // report the error, and stop scanning.
635 // No need to find more than the first failure.
636 result |= USPOOF_INVISIBLE;
637 break;
638 }
639 marksSeenSoFar.add(c);
640 }
641 }
642
643 checkResult->fChecks = result;
644 return checkResult->toCombinedBitmask(This->fChecks);
645}
646
647} // namespace
648
649U_CAPI int32_t U_EXPORT2
650uspoof_check2UnicodeString(const USpoofChecker *sc,
651 const icu::UnicodeString &id,
652 USpoofCheckResult* checkResult,
653 UErrorCode *status) {
654 const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
655 if (This == NULL) {
656 return FALSE;
657 }
658
659 if (checkResult != NULL) {
660 CheckResult* ThisCheckResult = CheckResult::validateThis(checkResult, *status);
661 if (ThisCheckResult == NULL) {
662 return FALSE;
663 }
664 return checkImpl(This, id, ThisCheckResult, status);
665 } else {
666 // Stack-allocate the checkResult since this method doesn't return it
667 CheckResult stackCheckResult;
668 return checkImpl(This, id, &stackCheckResult, status);
669 }
670}
671
672
673U_CAPI int32_t U_EXPORT2
674uspoof_getSkeleton(const USpoofChecker *sc,
675 uint32_t type,
676 const UChar *id, int32_t length,
677 UChar *dest, int32_t destCapacity,
678 UErrorCode *status) {
679
680 SpoofImpl::validateThis(sc, *status);
681 if (U_FAILURE(*status)) {
682 return 0;
683 }
684 if (length<-1 || destCapacity<0 || (destCapacity==0 && dest!=NULL)) {
685 *status = U_ILLEGAL_ARGUMENT_ERROR;
686 return 0;
687 }
688
689 UnicodeString idStr((length==-1), id, length); // Aliasing constructor
690 UnicodeString destStr;
691 uspoof_getSkeletonUnicodeString(sc, type, idStr, destStr, status);
692 destStr.extract(dest, destCapacity, *status);
693 return destStr.length();
694}
695
696
697
698U_I18N_API UnicodeString & U_EXPORT2
699uspoof_getSkeletonUnicodeString(const USpoofChecker *sc,
700 uint32_t /*type*/,
701 const UnicodeString &id,
702 UnicodeString &dest,
703 UErrorCode *status) {
704 const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
705 if (U_FAILURE(*status)) {
706 return dest;
707 }
708
709 UnicodeString nfdId;
710 gNfdNormalizer->normalize(id, nfdId, *status);
711
712 // Apply the skeleton mapping to the NFD normalized input string
713 // Accumulate the skeleton, possibly unnormalized, in a UnicodeString.
714 int32_t inputIndex = 0;
715 UnicodeString skelStr;
716 int32_t normalizedLen = nfdId.length();
717 for (inputIndex=0; inputIndex < normalizedLen; ) {
718 UChar32 c = nfdId.char32At(inputIndex);
719 inputIndex += U16_LENGTH(c);
720 This->fSpoofData->confusableLookup(c, skelStr);
721 }
722
723 gNfdNormalizer->normalize(skelStr, dest, *status);
724 return dest;
725}
726
727
728U_CAPI int32_t U_EXPORT2
729uspoof_getSkeletonUTF8(const USpoofChecker *sc,
730 uint32_t type,
731 const char *id, int32_t length,
732 char *dest, int32_t destCapacity,
733 UErrorCode *status) {
734 SpoofImpl::validateThis(sc, *status);
735 if (U_FAILURE(*status)) {
736 return 0;
737 }
738 if (length<-1 || destCapacity<0 || (destCapacity==0 && dest!=NULL)) {
739 *status = U_ILLEGAL_ARGUMENT_ERROR;
740 return 0;
741 }
742
743 UnicodeString srcStr = UnicodeString::fromUTF8(StringPiece(id, length>=0 ? length : static_cast<int32_t>(uprv_strlen(id))));
744 UnicodeString destStr;
745 uspoof_getSkeletonUnicodeString(sc, type, srcStr, destStr, status);
746 if (U_FAILURE(*status)) {
747 return 0;
748 }
749
750 int32_t lengthInUTF8 = 0;
751 u_strToUTF8(dest, destCapacity, &lengthInUTF8,
752 destStr.getBuffer(), destStr.length(), status);
753 return lengthInUTF8;
754}
755
756
757U_CAPI int32_t U_EXPORT2
758uspoof_serialize(USpoofChecker *sc,void *buf, int32_t capacity, UErrorCode *status) {
759 SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
760 if (This == NULL) {
761 U_ASSERT(U_FAILURE(*status));
762 return 0;
763 }
764
765 return This->fSpoofData->serialize(buf, capacity, *status);
766}
767
768U_CAPI const USet * U_EXPORT2
769uspoof_getInclusionSet(UErrorCode *status) {
770 umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
771 return gInclusionSet->toUSet();
772}
773
774U_CAPI const USet * U_EXPORT2
775uspoof_getRecommendedSet(UErrorCode *status) {
776 umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
777 return gRecommendedSet->toUSet();
778}
779
780U_I18N_API const UnicodeSet * U_EXPORT2
781uspoof_getInclusionUnicodeSet(UErrorCode *status) {
782 umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
783 return gInclusionSet;
784}
785
786U_I18N_API const UnicodeSet * U_EXPORT2
787uspoof_getRecommendedUnicodeSet(UErrorCode *status) {
788 umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
789 return gRecommendedSet;
790}
791
792//------------------
793// CheckResult APIs
794//------------------
795
796U_CAPI USpoofCheckResult* U_EXPORT2
797uspoof_openCheckResult(UErrorCode *status) {
798 CheckResult* checkResult = new CheckResult();
799 if (checkResult == NULL) {
800 *status = U_MEMORY_ALLOCATION_ERROR;
801 return NULL;
802 }
803 return checkResult->asUSpoofCheckResult();
804}
805
806U_CAPI void U_EXPORT2
807uspoof_closeCheckResult(USpoofCheckResult* checkResult) {
808 UErrorCode status = U_ZERO_ERROR;
809 CheckResult* This = CheckResult::validateThis(checkResult, status);
810 delete This;
811}
812
813U_CAPI int32_t U_EXPORT2
814uspoof_getCheckResultChecks(const USpoofCheckResult *checkResult, UErrorCode *status) {
815 const CheckResult* This = CheckResult::validateThis(checkResult, *status);
816 if (U_FAILURE(*status)) { return 0; }
817 return This->fChecks;
818}
819
820U_CAPI URestrictionLevel U_EXPORT2
821uspoof_getCheckResultRestrictionLevel(const USpoofCheckResult *checkResult, UErrorCode *status) {
822 const CheckResult* This = CheckResult::validateThis(checkResult, *status);
823 if (U_FAILURE(*status)) { return USPOOF_UNRESTRICTIVE; }
824 return This->fRestrictionLevel;
825}
826
827U_CAPI const USet* U_EXPORT2
828uspoof_getCheckResultNumerics(const USpoofCheckResult *checkResult, UErrorCode *status) {
829 const CheckResult* This = CheckResult::validateThis(checkResult, *status);
830 if (U_FAILURE(*status)) { return NULL; }
831 return This->fNumerics.toUSet();
832}
833
834
835
836#endif // !UCONFIG_NO_NORMALIZATION
837