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