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