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