uspoof.cpp 29 KB

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