uspoof.h 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  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-2016, International Business Machines Corporation
  6. * and others. All Rights Reserved.
  7. ***************************************************************************
  8. * file name: uspoof.h
  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. #ifndef USPOOF_H
  19. #define USPOOF_H
  20. #include "unicode/ubidi.h"
  21. #include "unicode/utypes.h"
  22. #include "unicode/uset.h"
  23. #include "unicode/parseerr.h"
  24. #if !UCONFIG_NO_NORMALIZATION
  25. #if U_SHOW_CPLUSPLUS_API
  26. #include "unicode/localpointer.h"
  27. #include "unicode/unistr.h"
  28. #include "unicode/uniset.h"
  29. #endif
  30. /**
  31. * \file
  32. * \brief C API: Unicode Security and Spoofing Detection
  33. *
  34. * <p>
  35. * This class, based on <a href="http://unicode.org/reports/tr36">Unicode Technical Report #36</a> and
  36. * <a href="http://unicode.org/reports/tr39">Unicode Technical Standard #39</a>, has two main functions:
  37. *
  38. * <ol>
  39. * <li>Checking whether two strings are visually <em>confusable</em> with each other, such as "Harvest" and
  40. * &quot;&Eta;arvest&quot;, where the second string starts with the Greek capital letter Eta.</li>
  41. * <li>Checking whether an individual string is likely to be an attempt at confusing the reader (<em>spoof
  42. * detection</em>), such as "paypal" with some Latin characters substituted with Cyrillic look-alikes.</li>
  43. * </ol>
  44. *
  45. * <p>
  46. * Although originally designed as a method for flagging suspicious identifier strings such as URLs,
  47. * <code>USpoofChecker</code> has a number of other practical use cases, such as preventing attempts to evade bad-word
  48. * content filters.
  49. *
  50. * <p>
  51. * The functions of this class are exposed as C API, with a handful of syntactical conveniences for C++.
  52. *
  53. * <h2>Confusables</h2>
  54. *
  55. * <p>
  56. * The following example shows how to use <code>USpoofChecker</code> to check for confusability between two strings:
  57. *
  58. * \code{.c}
  59. * UErrorCode status = U_ZERO_ERROR;
  60. * UChar* str1 = (UChar*) u"Harvest";
  61. * UChar* str2 = (UChar*) u"\u0397arvest"; // with U+0397 GREEK CAPITAL LETTER ETA
  62. *
  63. * USpoofChecker* sc = uspoof_open(&status);
  64. * uspoof_setChecks(sc, USPOOF_CONFUSABLE, &status);
  65. *
  66. * int32_t bitmask = uspoof_areConfusable(sc, str1, -1, str2, -1, &status);
  67. * UBool result = bitmask != 0;
  68. * // areConfusable: 1 (status: U_ZERO_ERROR)
  69. * printf("areConfusable: %d (status: %s)\n", result, u_errorName(status));
  70. * uspoof_close(sc);
  71. * \endcode
  72. *
  73. * <p>
  74. * The call to {@link uspoof_open} creates a <code>USpoofChecker</code> object; the call to {@link uspoof_setChecks}
  75. * enables confusable checking and disables all other checks; the call to {@link uspoof_areConfusable} performs the
  76. * confusability test; and the following line extracts the result out of the return value. For best performance,
  77. * the instance should be created once (e.g., upon application startup), and the efficient
  78. * {@link uspoof_areConfusable} method can be used at runtime.
  79. *
  80. * If the paragraph direction used to display the strings is known, the bidi function should be used instead:
  81. *
  82. * \code{.c}
  83. * UErrorCode status = U_ZERO_ERROR;
  84. * // These strings look identical when rendered in a left-to-right context.
  85. * // They look distinct in a right-to-left context.
  86. * UChar* str1 = (UChar*) u"A1\u05D0"; // A1א
  87. * UChar* str2 = (UChar*) u"A\u05D01"; // Aא1
  88. *
  89. * USpoofChecker* sc = uspoof_open(&status);
  90. * uspoof_setChecks(sc, USPOOF_CONFUSABLE, &status);
  91. *
  92. * int32_t bitmask = uspoof_areBidiConfusable(sc, UBIDI_LTR, str1, -1, str2, -1, &status);
  93. * UBool result = bitmask != 0;
  94. * // areBidiConfusable: 1 (status: U_ZERO_ERROR)
  95. * printf("areBidiConfusable: %d (status: %s)\n", result, u_errorName(status));
  96. * uspoof_close(sc);
  97. * \endcode
  98. *
  99. * <p>
  100. * The type {@link LocalUSpoofCheckerPointer} is exposed for C++ programmers. It will automatically call
  101. * {@link uspoof_close} when the object goes out of scope:
  102. *
  103. * \code{.cpp}
  104. * UErrorCode status = U_ZERO_ERROR;
  105. * LocalUSpoofCheckerPointer sc(uspoof_open(&status));
  106. * uspoof_setChecks(sc.getAlias(), USPOOF_CONFUSABLE, &status);
  107. * // ...
  108. * \endcode
  109. *
  110. * UTS 39 defines two strings to be <em>confusable</em> if they map to the same <em>skeleton string</em>. A skeleton can
  111. * be thought of as a "hash code". {@link uspoof_getSkeleton} computes the skeleton for a particular string, so
  112. * the following snippet is equivalent to the example above:
  113. *
  114. * \code{.c}
  115. * UErrorCode status = U_ZERO_ERROR;
  116. * UChar* str1 = (UChar*) u"Harvest";
  117. * UChar* str2 = (UChar*) u"\u0397arvest"; // with U+0397 GREEK CAPITAL LETTER ETA
  118. *
  119. * USpoofChecker* sc = uspoof_open(&status);
  120. * uspoof_setChecks(sc, USPOOF_CONFUSABLE, &status);
  121. *
  122. * // Get skeleton 1
  123. * int32_t skel1Len = uspoof_getSkeleton(sc, 0, str1, -1, NULL, 0, &status);
  124. * UChar* skel1 = (UChar*) malloc(++skel1Len * sizeof(UChar));
  125. * status = U_ZERO_ERROR;
  126. * uspoof_getSkeleton(sc, 0, str1, -1, skel1, skel1Len, &status);
  127. *
  128. * // Get skeleton 2
  129. * int32_t skel2Len = uspoof_getSkeleton(sc, 0, str2, -1, NULL, 0, &status);
  130. * UChar* skel2 = (UChar*) malloc(++skel2Len * sizeof(UChar));
  131. * status = U_ZERO_ERROR;
  132. * uspoof_getSkeleton(sc, 0, str2, -1, skel2, skel2Len, &status);
  133. *
  134. * // Are the skeletons the same?
  135. * UBool result = u_strcmp(skel1, skel2) == 0;
  136. * // areConfusable: 1 (status: U_ZERO_ERROR)
  137. * printf("areConfusable: %d (status: %s)\n", result, u_errorName(status));
  138. * uspoof_close(sc);
  139. * free(skel1);
  140. * free(skel2);
  141. * \endcode
  142. *
  143. * If you need to check if a string is confusable with any string in a dictionary of many strings, rather than calling
  144. * {@link uspoof_areConfusable} many times in a loop, {@link uspoof_getSkeleton} can be used instead, as shown below:
  145. *
  146. * \code{.c}
  147. * UErrorCode status = U_ZERO_ERROR;
  148. * #define DICTIONARY_LENGTH 2
  149. * UChar* dictionary[DICTIONARY_LENGTH] = { (UChar*) u"lorem", (UChar*) u"ipsum" };
  150. * UChar* skeletons[DICTIONARY_LENGTH];
  151. * UChar* str = (UChar*) u"1orern";
  152. *
  153. * // Setup:
  154. * USpoofChecker* sc = uspoof_open(&status);
  155. * uspoof_setChecks(sc, USPOOF_CONFUSABLE, &status);
  156. * for (size_t i=0; i<DICTIONARY_LENGTH; i++) {
  157. * UChar* word = dictionary[i];
  158. * int32_t len = uspoof_getSkeleton(sc, 0, word, -1, NULL, 0, &status);
  159. * skeletons[i] = (UChar*) malloc(++len * sizeof(UChar));
  160. * status = U_ZERO_ERROR;
  161. * uspoof_getSkeleton(sc, 0, word, -1, skeletons[i], len, &status);
  162. * }
  163. *
  164. * // Live Check:
  165. * {
  166. * int32_t len = uspoof_getSkeleton(sc, 0, str, -1, NULL, 0, &status);
  167. * UChar* skel = (UChar*) malloc(++len * sizeof(UChar));
  168. * status = U_ZERO_ERROR;
  169. * uspoof_getSkeleton(sc, 0, str, -1, skel, len, &status);
  170. * UBool result = false;
  171. * for (size_t i=0; i<DICTIONARY_LENGTH; i++) {
  172. * result = u_strcmp(skel, skeletons[i]) == 0;
  173. * if (result == true) { break; }
  174. * }
  175. * // Has confusable in dictionary: 1 (status: U_ZERO_ERROR)
  176. * printf("Has confusable in dictionary: %d (status: %s)\n", result, u_errorName(status));
  177. * free(skel);
  178. * }
  179. *
  180. * for (size_t i=0; i<DICTIONARY_LENGTH; i++) {
  181. * free(skeletons[i]);
  182. * }
  183. * uspoof_close(sc);
  184. * \endcode
  185. *
  186. * <b>Note:</b> Since the Unicode confusables mapping table is frequently updated, confusable skeletons are <em>not</em>
  187. * guaranteed to be the same between ICU releases. We therefore recommend that you always compute confusable skeletons
  188. * at runtime and do not rely on creating a permanent, or difficult to update, database of skeletons.
  189. *
  190. * <h2>Spoof Detection</h2>
  191. *
  192. * The following snippet shows a minimal example of using <code>USpoofChecker</code> to perform spoof detection on a
  193. * string:
  194. *
  195. * \code{.c}
  196. * UErrorCode status = U_ZERO_ERROR;
  197. * UChar* str = (UChar*) u"p\u0430ypal"; // with U+0430 CYRILLIC SMALL LETTER A
  198. *
  199. * // Get the default set of allowable characters:
  200. * USet* allowed = uset_openEmpty();
  201. * uset_addAll(allowed, uspoof_getRecommendedSet(&status));
  202. * uset_addAll(allowed, uspoof_getInclusionSet(&status));
  203. *
  204. * USpoofChecker* sc = uspoof_open(&status);
  205. * uspoof_setAllowedChars(sc, allowed, &status);
  206. * uspoof_setRestrictionLevel(sc, USPOOF_MODERATELY_RESTRICTIVE);
  207. *
  208. * int32_t bitmask = uspoof_check(sc, str, -1, NULL, &status);
  209. * UBool result = bitmask != 0;
  210. * // fails checks: 1 (status: U_ZERO_ERROR)
  211. * printf("fails checks: %d (status: %s)\n", result, u_errorName(status));
  212. * uspoof_close(sc);
  213. * uset_close(allowed);
  214. * \endcode
  215. *
  216. * As in the case for confusability checking, it is good practice to create one <code>USpoofChecker</code> instance at
  217. * startup, and call the cheaper {@link uspoof_check} online. We specify the set of
  218. * allowed characters to be those with type RECOMMENDED or INCLUSION, according to the recommendation in UTS 39.
  219. *
  220. * In addition to {@link uspoof_check}, the function {@link uspoof_checkUTF8} is exposed for UTF8-encoded char* strings,
  221. * and {@link uspoof_checkUnicodeString} is exposed for C++ programmers.
  222. *
  223. * If the {@link USPOOF_AUX_INFO} check is enabled, a limited amount of information on why a string failed the checks
  224. * is available in the returned bitmask. For complete information, use the {@link uspoof_check2} class of functions
  225. * with a {@link USpoofCheckResult} parameter:
  226. *
  227. * \code{.c}
  228. * UErrorCode status = U_ZERO_ERROR;
  229. * UChar* str = (UChar*) u"p\u0430ypal"; // with U+0430 CYRILLIC SMALL LETTER A
  230. *
  231. * // Get the default set of allowable characters:
  232. * USet* allowed = uset_openEmpty();
  233. * uset_addAll(allowed, uspoof_getRecommendedSet(&status));
  234. * uset_addAll(allowed, uspoof_getInclusionSet(&status));
  235. *
  236. * USpoofChecker* sc = uspoof_open(&status);
  237. * uspoof_setAllowedChars(sc, allowed, &status);
  238. * uspoof_setRestrictionLevel(sc, USPOOF_MODERATELY_RESTRICTIVE);
  239. *
  240. * USpoofCheckResult* checkResult = uspoof_openCheckResult(&status);
  241. * int32_t bitmask = uspoof_check2(sc, str, -1, checkResult, &status);
  242. *
  243. * int32_t failures1 = bitmask;
  244. * int32_t failures2 = uspoof_getCheckResultChecks(checkResult, &status);
  245. * assert(failures1 == failures2);
  246. * // checks that failed: 0x00000010 (status: U_ZERO_ERROR)
  247. * printf("checks that failed: %#010x (status: %s)\n", failures1, u_errorName(status));
  248. *
  249. * // Cleanup:
  250. * uspoof_close(sc);
  251. * uset_close(allowed);
  252. * uspoof_closeCheckResult(checkResult);
  253. * \endcode
  254. *
  255. * C++ users can take advantage of a few syntactical conveniences. The following snippet is functionally
  256. * equivalent to the one above:
  257. *
  258. * \code{.cpp}
  259. * UErrorCode status = U_ZERO_ERROR;
  260. * UnicodeString str((UChar*) u"p\u0430ypal"); // with U+0430 CYRILLIC SMALL LETTER A
  261. *
  262. * // Get the default set of allowable characters:
  263. * UnicodeSet allowed;
  264. * allowed.addAll(*uspoof_getRecommendedUnicodeSet(&status));
  265. * allowed.addAll(*uspoof_getInclusionUnicodeSet(&status));
  266. *
  267. * LocalUSpoofCheckerPointer sc(uspoof_open(&status));
  268. * uspoof_setAllowedChars(sc.getAlias(), allowed.toUSet(), &status);
  269. * uspoof_setRestrictionLevel(sc.getAlias(), USPOOF_MODERATELY_RESTRICTIVE);
  270. *
  271. * LocalUSpoofCheckResultPointer checkResult(uspoof_openCheckResult(&status));
  272. * int32_t bitmask = uspoof_check2UnicodeString(sc.getAlias(), str, checkResult.getAlias(), &status);
  273. *
  274. * int32_t failures1 = bitmask;
  275. * int32_t failures2 = uspoof_getCheckResultChecks(checkResult.getAlias(), &status);
  276. * assert(failures1 == failures2);
  277. * // checks that failed: 0x00000010 (status: U_ZERO_ERROR)
  278. * printf("checks that failed: %#010x (status: %s)\n", failures1, u_errorName(status));
  279. *
  280. * // Explicit cleanup not necessary.
  281. * \endcode
  282. *
  283. * The return value is a bitmask of the checks that failed. In this case, there was one check that failed:
  284. * {@link USPOOF_RESTRICTION_LEVEL}, corresponding to the fifth bit (16). The possible checks are:
  285. *
  286. * <ul>
  287. * <li><code>RESTRICTION_LEVEL</code>: flags strings that violate the
  288. * <a href="http://unicode.org/reports/tr39/#Restriction_Level_Detection">Restriction Level</a> test as specified in UTS
  289. * 39; in most cases, this means flagging strings that contain characters from multiple different scripts.</li>
  290. * <li><code>INVISIBLE</code>: flags strings that contain invisible characters, such as zero-width spaces, or character
  291. * sequences that are likely not to display, such as multiple occurrences of the same non-spacing mark.</li>
  292. * <li><code>CHAR_LIMIT</code>: flags strings that contain characters outside of a specified set of acceptable
  293. * characters. See {@link uspoof_setAllowedChars} and {@link uspoof_setAllowedLocales}.</li>
  294. * <li><code>MIXED_NUMBERS</code>: flags strings that contain digits from multiple different numbering systems.</li>
  295. * </ul>
  296. *
  297. * <p>
  298. * These checks can be enabled independently of each other. For example, if you were interested in checking for only the
  299. * INVISIBLE and MIXED_NUMBERS conditions, you could do:
  300. *
  301. * \code{.c}
  302. * UErrorCode status = U_ZERO_ERROR;
  303. * UChar* str = (UChar*) u"8\u09EA"; // 8 mixed with U+09EA BENGALI DIGIT FOUR
  304. *
  305. * USpoofChecker* sc = uspoof_open(&status);
  306. * uspoof_setChecks(sc, USPOOF_INVISIBLE | USPOOF_MIXED_NUMBERS, &status);
  307. *
  308. * int32_t bitmask = uspoof_check2(sc, str, -1, NULL, &status);
  309. * UBool result = bitmask != 0;
  310. * // fails checks: 1 (status: U_ZERO_ERROR)
  311. * printf("fails checks: %d (status: %s)\n", result, u_errorName(status));
  312. * uspoof_close(sc);
  313. * \endcode
  314. *
  315. * Here is an example in C++ showing how to compute the restriction level of a string:
  316. *
  317. * \code{.cpp}
  318. * UErrorCode status = U_ZERO_ERROR;
  319. * UnicodeString str((UChar*) u"p\u0430ypal"); // with U+0430 CYRILLIC SMALL LETTER A
  320. *
  321. * // Get the default set of allowable characters:
  322. * UnicodeSet allowed;
  323. * allowed.addAll(*uspoof_getRecommendedUnicodeSet(&status));
  324. * allowed.addAll(*uspoof_getInclusionUnicodeSet(&status));
  325. *
  326. * LocalUSpoofCheckerPointer sc(uspoof_open(&status));
  327. * uspoof_setAllowedChars(sc.getAlias(), allowed.toUSet(), &status);
  328. * uspoof_setRestrictionLevel(sc.getAlias(), USPOOF_MODERATELY_RESTRICTIVE);
  329. * uspoof_setChecks(sc.getAlias(), USPOOF_RESTRICTION_LEVEL | USPOOF_AUX_INFO, &status);
  330. *
  331. * LocalUSpoofCheckResultPointer checkResult(uspoof_openCheckResult(&status));
  332. * int32_t bitmask = uspoof_check2UnicodeString(sc.getAlias(), str, checkResult.getAlias(), &status);
  333. *
  334. * URestrictionLevel restrictionLevel = uspoof_getCheckResultRestrictionLevel(checkResult.getAlias(), &status);
  335. * // Since USPOOF_AUX_INFO was enabled, the restriction level is also available in the upper bits of the bitmask:
  336. * assert((restrictionLevel & bitmask) == restrictionLevel);
  337. * // Restriction level: 0x50000000 (status: U_ZERO_ERROR)
  338. * printf("Restriction level: %#010x (status: %s)\n", restrictionLevel, u_errorName(status));
  339. * \endcode
  340. *
  341. * The code '0x50000000' corresponds to the restriction level USPOOF_MINIMALLY_RESTRICTIVE. Since
  342. * USPOOF_MINIMALLY_RESTRICTIVE is weaker than USPOOF_MODERATELY_RESTRICTIVE, the string fails the check.
  343. *
  344. * <b>Note:</b> The Restriction Level is the most powerful of the checks. The full logic is documented in
  345. * <a href="http://unicode.org/reports/tr39/#Restriction_Level_Detection">UTS 39</a>, but the basic idea is that strings
  346. * are restricted to contain characters from only a single script, <em>except</em> that most scripts are allowed to have
  347. * Latin characters interspersed. Although the default restriction level is <code>HIGHLY_RESTRICTIVE</code>, it is
  348. * recommended that users set their restriction level to <code>MODERATELY_RESTRICTIVE</code>, which allows Latin mixed
  349. * with all other scripts except Cyrillic, Greek, and Cherokee, with which it is often confusable. For more details on
  350. * the levels, see UTS 39 or {@link URestrictionLevel}. The Restriction Level test is aware of the set of
  351. * allowed characters set in {@link uspoof_setAllowedChars}. Note that characters which have script code
  352. * COMMON or INHERITED, such as numbers and punctuation, are ignored when computing whether a string has multiple
  353. * scripts.
  354. *
  355. * <h2>Advanced bidirectional usage</h2>
  356. * If the paragraph direction with which the identifiers will be displayed is not known, there are
  357. * multiple options for confusable detection depending on the circumstances.
  358. *
  359. * <p>
  360. * In some circumstances, the only concern is confusion between identifiers displayed with the same
  361. * paragraph direction.
  362. *
  363. * <p>
  364. * An example is the case where identifiers are usernames prefixed with the @ symbol.
  365. * That symbol will appear to the left in a left-to-right context, and to the right in a
  366. * right-to-left context, so that an identifier displayed in a left-to-right context can never be
  367. * confused with an identifier displayed in a right-to-left context:
  368. * <ul>
  369. * <li>
  370. * The usernames "A1א" (A one aleph) and "Aא1" (A aleph 1)
  371. * would be considered confusable, since they both appear as \@A1א in a left-to-right context, and the
  372. * usernames "אA_1" (aleph A underscore one) and "א1_A" (aleph one underscore A) would be considered
  373. * confusable, since they both appear as A_1א@ in a right-to-left context.
  374. * </li>
  375. * <li>
  376. * The username "Mark_" would not be considered confusable with the username "_Mark",
  377. * even though the latter would appear as Mark_@ in a right-to-left context, and the
  378. * former as \@Mark_ in a left-to-right context.
  379. * </li>
  380. * </ul>
  381. * <p>
  382. * In that case, the caller should check for both LTR-confusability and RTL-confusability:
  383. *
  384. * \code{.cpp}
  385. * bool confusableInEitherDirection =
  386. * uspoof_areBidiConfusableUnicodeString(sc, UBIDI_LTR, id1, id2, &status) ||
  387. * uspoof_areBidiConfusableUnicodeString(sc, UBIDI_RTL, id1, id2, &status);
  388. * \endcode
  389. *
  390. * If the bidiSkeleton is used, the LTR and RTL skeleta should be kept separately and compared, LTR
  391. * with LTR and RTL with RTL.
  392. *
  393. * <p>
  394. * In cases where confusability between the visual appearances of an identifier displayed in a
  395. * left-to-right context with another identifier displayed in a right-to-left context is a concern,
  396. * the LTR skeleton of one can be compared with the RTL skeleton of the other. However, this
  397. * very broad definition of confusability may have unexpected results; for instance, it treats the
  398. * ASCII identifiers "Mark_" and "_Mark" as confusable.
  399. *
  400. * <h2>Additional Information</h2>
  401. *
  402. * A <code>USpoofChecker</code> instance may be used repeatedly to perform checks on any number of identifiers.
  403. *
  404. * <b>Thread Safety:</b> The test functions for checking a single identifier, or for testing whether
  405. * two identifiers are possible confusable, are thread safe. They may called concurrently, from multiple threads,
  406. * using the same USpoofChecker instance.
  407. *
  408. * More generally, the standard ICU thread safety rules apply: functions that take a const USpoofChecker parameter are
  409. * thread safe. Those that take a non-const USpoofChecker are not thread safe..
  410. *
  411. * @stable ICU 4.6
  412. */
  413. U_CDECL_BEGIN
  414. struct USpoofChecker;
  415. /**
  416. * @stable ICU 4.2
  417. */
  418. typedef struct USpoofChecker USpoofChecker; /**< typedef for C of USpoofChecker */
  419. struct USpoofCheckResult;
  420. /**
  421. * @see uspoof_openCheckResult
  422. * @stable ICU 58
  423. */
  424. typedef struct USpoofCheckResult USpoofCheckResult;
  425. /**
  426. * Enum for the kinds of checks that USpoofChecker can perform.
  427. * These enum values are used both to select the set of checks that
  428. * will be performed, and to report results from the check function.
  429. *
  430. * @stable ICU 4.2
  431. */
  432. typedef enum USpoofChecks {
  433. /**
  434. * When performing the two-string {@link uspoof_areConfusable} test, this flag in the return value indicates
  435. * that the two strings are visually confusable and that they are from the same script, according to UTS 39 section
  436. * 4.
  437. *
  438. * @see uspoof_areConfusable
  439. * @stable ICU 4.2
  440. */
  441. USPOOF_SINGLE_SCRIPT_CONFUSABLE = 1,
  442. /**
  443. * When performing the two-string {@link uspoof_areConfusable} test, this flag in the return value indicates
  444. * that the two strings are visually confusable and that they are <b>not</b> from the same script, according to UTS
  445. * 39 section 4.
  446. *
  447. * @see uspoof_areConfusable
  448. * @stable ICU 4.2
  449. */
  450. USPOOF_MIXED_SCRIPT_CONFUSABLE = 2,
  451. /**
  452. * When performing the two-string {@link uspoof_areConfusable} test, this flag in the return value indicates
  453. * that the two strings are visually confusable and that they are not from the same script but both of them are
  454. * single-script strings, according to UTS 39 section 4.
  455. *
  456. * @see uspoof_areConfusable
  457. * @stable ICU 4.2
  458. */
  459. USPOOF_WHOLE_SCRIPT_CONFUSABLE = 4,
  460. /**
  461. * Enable this flag in {@link uspoof_setChecks} to turn on all types of confusables. You may set
  462. * the checks to some subset of SINGLE_SCRIPT_CONFUSABLE, MIXED_SCRIPT_CONFUSABLE, or WHOLE_SCRIPT_CONFUSABLE to
  463. * make {@link uspoof_areConfusable} return only those types of confusables.
  464. *
  465. * @see uspoof_areConfusable
  466. * @see uspoof_getSkeleton
  467. * @stable ICU 58
  468. */
  469. USPOOF_CONFUSABLE = USPOOF_SINGLE_SCRIPT_CONFUSABLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_WHOLE_SCRIPT_CONFUSABLE,
  470. #ifndef U_HIDE_DEPRECATED_API
  471. /**
  472. * This flag is deprecated and no longer affects the behavior of SpoofChecker.
  473. *
  474. * @deprecated ICU 58 Any case confusable mappings were removed from UTS 39; the corresponding ICU API was deprecated.
  475. */
  476. USPOOF_ANY_CASE = 8,
  477. #endif /* U_HIDE_DEPRECATED_API */
  478. /**
  479. * Check that an identifier is no looser than the specified RestrictionLevel.
  480. * The default if {@link uspoof_setRestrictionLevel} is not called is HIGHLY_RESTRICTIVE.
  481. *
  482. * If USPOOF_AUX_INFO is enabled the actual restriction level of the
  483. * identifier being tested will also be returned by uspoof_check().
  484. *
  485. * @see URestrictionLevel
  486. * @see uspoof_setRestrictionLevel
  487. * @see USPOOF_AUX_INFO
  488. *
  489. * @stable ICU 51
  490. */
  491. USPOOF_RESTRICTION_LEVEL = 16,
  492. #ifndef U_HIDE_DEPRECATED_API
  493. /** Check that an identifier contains only characters from a
  494. * single script (plus chars from the common and inherited scripts.)
  495. * Applies to checks of a single identifier check only.
  496. * @deprecated ICU 51 Use RESTRICTION_LEVEL instead.
  497. */
  498. USPOOF_SINGLE_SCRIPT = USPOOF_RESTRICTION_LEVEL,
  499. #endif /* U_HIDE_DEPRECATED_API */
  500. /** Check an identifier for the presence of invisible characters,
  501. * such as zero-width spaces, or character sequences that are
  502. * likely not to display, such as multiple occurrences of the same
  503. * non-spacing mark. This check does not test the input string as a whole
  504. * for conformance to any particular syntax for identifiers.
  505. */
  506. USPOOF_INVISIBLE = 32,
  507. /** Check that an identifier contains only characters from a specified set
  508. * of acceptable characters. See {@link uspoof_setAllowedChars} and
  509. * {@link uspoof_setAllowedLocales}. Note that a string that fails this check
  510. * will also fail the {@link USPOOF_RESTRICTION_LEVEL} check.
  511. */
  512. USPOOF_CHAR_LIMIT = 64,
  513. /**
  514. * Check that an identifier does not mix numbers from different numbering systems.
  515. * For more information, see UTS 39 section 5.3.
  516. *
  517. * @stable ICU 51
  518. */
  519. USPOOF_MIXED_NUMBERS = 128,
  520. /**
  521. * Check that an identifier does not have a combining character following a character in which that
  522. * combining character would be hidden; for example 'i' followed by a U+0307 combining dot.
  523. *
  524. * More specifically, the following characters are forbidden from preceding a U+0307:
  525. * <ul>
  526. * <li>Those with the Soft_Dotted Unicode property (which includes 'i' and 'j')</li>
  527. * <li>Latin lowercase letter 'l'</li>
  528. * <li>Dotless 'i' and 'j' ('ı' and 'ȷ', U+0131 and U+0237)</li>
  529. * <li>Any character whose confusable prototype ends with such a character
  530. * (Soft_Dotted, 'l', 'ı', or 'ȷ')</li>
  531. * </ul>
  532. * In addition, combining characters are allowed between the above characters and U+0307 except those
  533. * with combining class 0 or combining class "Above" (230, same class as U+0307).
  534. *
  535. * This list and the number of combing characters considered by this check may grow over time.
  536. *
  537. * @stable ICU 62
  538. */
  539. USPOOF_HIDDEN_OVERLAY = 256,
  540. /**
  541. * Enable all spoof checks.
  542. *
  543. * @stable ICU 4.6
  544. */
  545. USPOOF_ALL_CHECKS = 0xFFFF,
  546. /**
  547. * Enable the return of auxiliary (non-error) information in the
  548. * upper bits of the check results value.
  549. *
  550. * If this "check" is not enabled, the results of {@link uspoof_check} will be
  551. * zero when an identifier passes all of the enabled checks.
  552. *
  553. * If this "check" is enabled, (uspoof_check() & {@link USPOOF_ALL_CHECKS}) will
  554. * be zero when an identifier passes all checks.
  555. *
  556. * @stable ICU 51
  557. */
  558. USPOOF_AUX_INFO = 0x40000000
  559. } USpoofChecks;
  560. /**
  561. * Constants from UTS #39 for use in {@link uspoof_setRestrictionLevel}, and
  562. * for returned identifier restriction levels in check results.
  563. *
  564. * @stable ICU 51
  565. *
  566. * @see uspoof_setRestrictionLevel
  567. * @see uspoof_check
  568. */
  569. typedef enum URestrictionLevel {
  570. /**
  571. * All characters in the string are in the identifier profile and all characters in the string are in the
  572. * ASCII range.
  573. *
  574. * @stable ICU 51
  575. */
  576. USPOOF_ASCII = 0x10000000,
  577. /**
  578. * The string classifies as ASCII-Only, or all characters in the string are in the identifier profile and
  579. * the string is single-script, according to the definition in UTS 39 section 5.1.
  580. *
  581. * @stable ICU 53
  582. */
  583. USPOOF_SINGLE_SCRIPT_RESTRICTIVE = 0x20000000,
  584. /**
  585. * The string classifies as Single Script, or all characters in the string are in the identifier profile and
  586. * the string is covered by any of the following sets of scripts, according to the definition in UTS 39
  587. * section 5.1:
  588. * <ul>
  589. * <li>Latin + Han + Bopomofo (or equivalently: Latn + Hanb)</li>
  590. * <li>Latin + Han + Hiragana + Katakana (or equivalently: Latn + Jpan)</li>
  591. * <li>Latin + Han + Hangul (or equivalently: Latn +Kore)</li>
  592. * </ul>
  593. * This is the default restriction in ICU.
  594. *
  595. * @stable ICU 51
  596. */
  597. USPOOF_HIGHLY_RESTRICTIVE = 0x30000000,
  598. /**
  599. * The string classifies as Highly Restrictive, or all characters in the string are in the identifier profile
  600. * and the string is covered by Latin and any one other Recommended or Aspirational script, except Cyrillic,
  601. * Greek, and Cherokee.
  602. *
  603. * @stable ICU 51
  604. */
  605. USPOOF_MODERATELY_RESTRICTIVE = 0x40000000,
  606. /**
  607. * All characters in the string are in the identifier profile. Allow arbitrary mixtures of scripts.
  608. *
  609. * @stable ICU 51
  610. */
  611. USPOOF_MINIMALLY_RESTRICTIVE = 0x50000000,
  612. /**
  613. * Any valid identifiers, including characters outside of the Identifier Profile.
  614. *
  615. * @stable ICU 51
  616. */
  617. USPOOF_UNRESTRICTIVE = 0x60000000,
  618. /**
  619. * Mask for selecting the Restriction Level bits from the return value of {@link uspoof_check}.
  620. *
  621. * @stable ICU 53
  622. */
  623. USPOOF_RESTRICTION_LEVEL_MASK = 0x7F000000,
  624. #ifndef U_HIDE_INTERNAL_API
  625. /**
  626. * An undefined restriction level.
  627. * @internal
  628. */
  629. USPOOF_UNDEFINED_RESTRICTIVE = -1
  630. #endif /* U_HIDE_INTERNAL_API */
  631. } URestrictionLevel;
  632. /**
  633. * Create a Unicode Spoof Checker, configured to perform all
  634. * checks except for USPOOF_LOCALE_LIMIT and USPOOF_CHAR_LIMIT.
  635. * Note that additional checks may be added in the future,
  636. * resulting in the changes to the default checking behavior.
  637. *
  638. * @param status The error code, set if this function encounters a problem.
  639. * @return the newly created Spoof Checker
  640. * @stable ICU 4.2
  641. */
  642. U_CAPI USpoofChecker * U_EXPORT2
  643. uspoof_open(UErrorCode *status);
  644. /**
  645. * Open a Spoof checker from its serialized form, stored in 32-bit-aligned memory.
  646. * Inverse of uspoof_serialize().
  647. * The memory containing the serialized data must remain valid and unchanged
  648. * as long as the spoof checker, or any cloned copies of the spoof checker,
  649. * are in use. Ownership of the memory remains with the caller.
  650. * The spoof checker (and any clones) must be closed prior to deleting the
  651. * serialized data.
  652. *
  653. * @param data a pointer to 32-bit-aligned memory containing the serialized form of spoof data
  654. * @param length the number of bytes available at data;
  655. * can be more than necessary
  656. * @param pActualLength receives the actual number of bytes at data taken up by the data;
  657. * can be NULL
  658. * @param pErrorCode ICU error code
  659. * @return the spoof checker.
  660. *
  661. * @see uspoof_open
  662. * @see uspoof_serialize
  663. * @stable ICU 4.2
  664. */
  665. U_CAPI USpoofChecker * U_EXPORT2
  666. uspoof_openFromSerialized(const void *data, int32_t length, int32_t *pActualLength,
  667. UErrorCode *pErrorCode);
  668. /**
  669. * Open a Spoof Checker from the source form of the spoof data.
  670. * The input corresponds to the Unicode data file confusables.txt
  671. * as described in Unicode Technical Standard #39. The syntax of the source data
  672. * is as described in UTS #39 for this file, and the content of
  673. * this file is acceptable input.
  674. *
  675. * The character encoding of the (char *) input text is UTF-8.
  676. *
  677. * @param confusables a pointer to the confusable characters definitions,
  678. * as found in file confusables.txt from unicode.org.
  679. * @param confusablesLen The length of the confusables text, or -1 if the
  680. * input string is zero terminated.
  681. * @param confusablesWholeScript
  682. * Deprecated in ICU 58. No longer used.
  683. * @param confusablesWholeScriptLen
  684. * Deprecated in ICU 58. No longer used.
  685. * @param errType In the event of an error in the input, indicates
  686. * which of the input files contains the error.
  687. * The value is one of USPOOF_SINGLE_SCRIPT_CONFUSABLE or
  688. * USPOOF_WHOLE_SCRIPT_CONFUSABLE, or
  689. * zero if no errors are found.
  690. * @param pe In the event of an error in the input, receives the position
  691. * in the input text (line, offset) of the error.
  692. * @param status an in/out ICU UErrorCode. Among the possible errors is
  693. * U_PARSE_ERROR, which is used to report syntax errors
  694. * in the input.
  695. * @return A spoof checker that uses the rules from the input files.
  696. * @stable ICU 4.2
  697. */
  698. U_CAPI USpoofChecker * U_EXPORT2
  699. uspoof_openFromSource(const char *confusables, int32_t confusablesLen,
  700. const char *confusablesWholeScript, int32_t confusablesWholeScriptLen,
  701. int32_t *errType, UParseError *pe, UErrorCode *status);
  702. /**
  703. * Close a Spoof Checker, freeing any memory that was being held by
  704. * its implementation.
  705. * @stable ICU 4.2
  706. */
  707. U_CAPI void U_EXPORT2
  708. uspoof_close(USpoofChecker *sc);
  709. /**
  710. * Clone a Spoof Checker. The clone will be set to perform the same checks
  711. * as the original source.
  712. *
  713. * @param sc The source USpoofChecker
  714. * @param status The error code, set if this function encounters a problem.
  715. * @return
  716. * @stable ICU 4.2
  717. */
  718. U_CAPI USpoofChecker * U_EXPORT2
  719. uspoof_clone(const USpoofChecker *sc, UErrorCode *status);
  720. /**
  721. * Specify the bitmask of checks that will be performed by {@link uspoof_check}. Calling this method
  722. * overwrites any checks that may have already been enabled. By default, all checks are enabled.
  723. *
  724. * To enable specific checks and disable all others,
  725. * OR together only the bit constants for the desired checks.
  726. * For example, to fail strings containing characters outside of
  727. * the set specified by {@link uspoof_setAllowedChars} and
  728. * also strings that contain digits from mixed numbering systems:
  729. *
  730. * <pre>
  731. * {@code
  732. * uspoof_setChecks(USPOOF_CHAR_LIMIT | USPOOF_MIXED_NUMBERS);
  733. * }
  734. * </pre>
  735. *
  736. * To disable specific checks and enable all others,
  737. * start with ALL_CHECKS and "AND away" the not-desired checks.
  738. * For example, if you are not planning to use the {@link uspoof_areConfusable} functionality,
  739. * it is good practice to disable the CONFUSABLE check:
  740. *
  741. * <pre>
  742. * {@code
  743. * uspoof_setChecks(USPOOF_ALL_CHECKS & ~USPOOF_CONFUSABLE);
  744. * }
  745. * </pre>
  746. *
  747. * Note that methods such as {@link uspoof_setAllowedChars}, {@link uspoof_setAllowedLocales}, and
  748. * {@link uspoof_setRestrictionLevel} will enable certain checks when called. Those methods will OR the check they
  749. * enable onto the existing bitmask specified by this method. For more details, see the documentation of those
  750. * methods.
  751. *
  752. * @param sc The USpoofChecker
  753. * @param checks The set of checks that this spoof checker will perform.
  754. * The value is a bit set, obtained by OR-ing together
  755. * values from enum USpoofChecks.
  756. * @param status The error code, set if this function encounters a problem.
  757. * @stable ICU 4.2
  758. *
  759. */
  760. U_CAPI void U_EXPORT2
  761. uspoof_setChecks(USpoofChecker *sc, int32_t checks, UErrorCode *status);
  762. /**
  763. * Get the set of checks that this Spoof Checker has been configured to perform.
  764. *
  765. * @param sc The USpoofChecker
  766. * @param status The error code, set if this function encounters a problem.
  767. * @return The set of checks that this spoof checker will perform.
  768. * The value is a bit set, obtained by OR-ing together
  769. * values from enum USpoofChecks.
  770. * @stable ICU 4.2
  771. *
  772. */
  773. U_CAPI int32_t U_EXPORT2
  774. uspoof_getChecks(const USpoofChecker *sc, UErrorCode *status);
  775. /**
  776. * Set the loosest restriction level allowed for strings. The default if this is not called is
  777. * {@link USPOOF_HIGHLY_RESTRICTIVE}. Calling this method enables the {@link USPOOF_RESTRICTION_LEVEL} and
  778. * {@link USPOOF_MIXED_NUMBERS} checks, corresponding to Sections 5.1 and 5.2 of UTS 39. To customize which checks are
  779. * to be performed by {@link uspoof_check}, see {@link uspoof_setChecks}.
  780. *
  781. * @param sc The USpoofChecker
  782. * @param restrictionLevel The loosest restriction level allowed.
  783. * @see URestrictionLevel
  784. * @stable ICU 51
  785. */
  786. U_CAPI void U_EXPORT2
  787. uspoof_setRestrictionLevel(USpoofChecker *sc, URestrictionLevel restrictionLevel);
  788. /**
  789. * Get the Restriction Level that will be tested if the checks include {@link USPOOF_RESTRICTION_LEVEL}.
  790. *
  791. * @return The restriction level
  792. * @see URestrictionLevel
  793. * @stable ICU 51
  794. */
  795. U_CAPI URestrictionLevel U_EXPORT2
  796. uspoof_getRestrictionLevel(const USpoofChecker *sc);
  797. /**
  798. * Limit characters that are acceptable in identifiers being checked to those
  799. * normally used with the languages associated with the specified locales.
  800. * Any previously specified list of locales is replaced by the new settings.
  801. *
  802. * A set of languages is determined from the locale(s), and
  803. * from those a set of acceptable Unicode scripts is determined.
  804. * Characters from this set of scripts, along with characters from
  805. * the "common" and "inherited" Unicode Script categories
  806. * will be permitted.
  807. *
  808. * Supplying an empty string removes all restrictions;
  809. * characters from any script will be allowed.
  810. *
  811. * The {@link USPOOF_CHAR_LIMIT} test is automatically enabled for this
  812. * USpoofChecker when calling this function with a non-empty list
  813. * of locales.
  814. *
  815. * The Unicode Set of characters that will be allowed is accessible
  816. * via the uspoof_getAllowedChars() function. uspoof_setAllowedLocales()
  817. * will <i>replace</i> any previously applied set of allowed characters.
  818. *
  819. * Adjustments, such as additions or deletions of certain classes of characters,
  820. * can be made to the result of uspoof_setAllowedLocales() by
  821. * fetching the resulting set with uspoof_getAllowedChars(),
  822. * manipulating it with the Unicode Set API, then resetting the
  823. * spoof detectors limits with uspoof_setAllowedChars().
  824. *
  825. * @param sc The USpoofChecker
  826. * @param localesList A list list of locales, from which the language
  827. * and associated script are extracted. The locales
  828. * are comma-separated if there is more than one.
  829. * White space may not appear within an individual locale,
  830. * but is ignored otherwise.
  831. * The locales are syntactically like those from the
  832. * HTTP Accept-Language header.
  833. * If the localesList is empty, no restrictions will be placed on
  834. * the allowed characters.
  835. *
  836. * @param status The error code, set if this function encounters a problem.
  837. * @stable ICU 4.2
  838. */
  839. U_CAPI void U_EXPORT2
  840. uspoof_setAllowedLocales(USpoofChecker *sc, const char *localesList, UErrorCode *status);
  841. /**
  842. * Get a list of locales for the scripts that are acceptable in strings
  843. * to be checked. If no limitations on scripts have been specified,
  844. * an empty string will be returned.
  845. *
  846. * uspoof_setAllowedChars() will reset the list of allowed to be empty.
  847. *
  848. * The format of the returned list is the same as that supplied to
  849. * uspoof_setAllowedLocales(), but returned list may not be identical
  850. * to the originally specified string; the string may be reformatted,
  851. * and information other than languages from
  852. * the originally specified locales may be omitted.
  853. *
  854. * @param sc The USpoofChecker
  855. * @param status The error code, set if this function encounters a problem.
  856. * @return A string containing a list of locales corresponding
  857. * to the acceptable scripts, formatted like an
  858. * HTTP Accept Language value.
  859. *
  860. * @stable ICU 4.2
  861. */
  862. U_CAPI const char * U_EXPORT2
  863. uspoof_getAllowedLocales(USpoofChecker *sc, UErrorCode *status);
  864. /**
  865. * Limit the acceptable characters to those specified by a Unicode Set.
  866. * Any previously specified character limit is
  867. * is replaced by the new settings. This includes limits on
  868. * characters that were set with the uspoof_setAllowedLocales() function.
  869. *
  870. * The USPOOF_CHAR_LIMIT test is automatically enabled for this
  871. * USpoofChecker by this function.
  872. *
  873. * @param sc The USpoofChecker
  874. * @param chars A Unicode Set containing the list of
  875. * characters that are permitted. Ownership of the set
  876. * remains with the caller. The incoming set is cloned by
  877. * this function, so there are no restrictions on modifying
  878. * or deleting the USet after calling this function.
  879. * @param status The error code, set if this function encounters a problem.
  880. * @stable ICU 4.2
  881. */
  882. U_CAPI void U_EXPORT2
  883. uspoof_setAllowedChars(USpoofChecker *sc, const USet *chars, UErrorCode *status);
  884. /**
  885. * Get a USet for the characters permitted in an identifier.
  886. * This corresponds to the limits imposed by the Set Allowed Characters
  887. * functions. Limitations imposed by other checks will not be
  888. * reflected in the set returned by this function.
  889. *
  890. * The returned set will be frozen, meaning that it cannot be modified
  891. * by the caller.
  892. *
  893. * Ownership of the returned set remains with the Spoof Detector. The
  894. * returned set will become invalid if the spoof detector is closed,
  895. * or if a new set of allowed characters is specified.
  896. *
  897. *
  898. * @param sc The USpoofChecker
  899. * @param status The error code, set if this function encounters a problem.
  900. * @return A USet containing the characters that are permitted by
  901. * the USPOOF_CHAR_LIMIT test.
  902. * @stable ICU 4.2
  903. */
  904. U_CAPI const USet * U_EXPORT2
  905. uspoof_getAllowedChars(const USpoofChecker *sc, UErrorCode *status);
  906. /**
  907. * Check the specified string for possible security issues.
  908. * The text to be checked will typically be an identifier of some sort.
  909. * The set of checks to be performed is specified with uspoof_setChecks().
  910. *
  911. * \note
  912. * Consider using the newer API, {@link uspoof_check2}, instead.
  913. * The newer API exposes additional information from the check procedure
  914. * and is otherwise identical to this method.
  915. *
  916. * @param sc The USpoofChecker
  917. * @param id The identifier to be checked for possible security issues,
  918. * in UTF-16 format.
  919. * @param length the length of the string to be checked, expressed in
  920. * 16 bit UTF-16 code units, or -1 if the string is
  921. * zero terminated.
  922. * @param position Deprecated in ICU 51. Always returns zero.
  923. * Originally, an out parameter for the index of the first
  924. * string position that failed a check.
  925. * This parameter may be NULL.
  926. * @param status The error code, set if an error occurred while attempting to
  927. * perform the check.
  928. * Spoofing or security issues detected with the input string are
  929. * not reported here, but through the function's return value.
  930. * @return An integer value with bits set for any potential security
  931. * or spoofing issues detected. The bits are defined by
  932. * enum USpoofChecks. (returned_value & USPOOF_ALL_CHECKS)
  933. * will be zero if the input string passes all of the
  934. * enabled checks.
  935. * @see uspoof_check2
  936. * @stable ICU 4.2
  937. */
  938. U_CAPI int32_t U_EXPORT2
  939. uspoof_check(const USpoofChecker *sc,
  940. const UChar *id, int32_t length,
  941. int32_t *position,
  942. UErrorCode *status);
  943. /**
  944. * Check the specified string for possible security issues.
  945. * The text to be checked will typically be an identifier of some sort.
  946. * The set of checks to be performed is specified with uspoof_setChecks().
  947. *
  948. * \note
  949. * Consider using the newer API, {@link uspoof_check2UTF8}, instead.
  950. * The newer API exposes additional information from the check procedure
  951. * and is otherwise identical to this method.
  952. *
  953. * @param sc The USpoofChecker
  954. * @param id A identifier to be checked for possible security issues, in UTF8 format.
  955. * @param length the length of the string to be checked, or -1 if the string is
  956. * zero terminated.
  957. * @param position Deprecated in ICU 51. Always returns zero.
  958. * Originally, an out parameter for the index of the first
  959. * string position that failed a check.
  960. * This parameter may be NULL.
  961. * @param status The error code, set if an error occurred while attempting to
  962. * perform the check.
  963. * Spoofing or security issues detected with the input string are
  964. * not reported here, but through the function's return value.
  965. * If the input contains invalid UTF-8 sequences,
  966. * a status of U_INVALID_CHAR_FOUND will be returned.
  967. * @return An integer value with bits set for any potential security
  968. * or spoofing issues detected. The bits are defined by
  969. * enum USpoofChecks. (returned_value & USPOOF_ALL_CHECKS)
  970. * will be zero if the input string passes all of the
  971. * enabled checks.
  972. * @see uspoof_check2UTF8
  973. * @stable ICU 4.2
  974. */
  975. U_CAPI int32_t U_EXPORT2
  976. uspoof_checkUTF8(const USpoofChecker *sc,
  977. const char *id, int32_t length,
  978. int32_t *position,
  979. UErrorCode *status);
  980. /**
  981. * Check the specified string for possible security issues.
  982. * The text to be checked will typically be an identifier of some sort.
  983. * The set of checks to be performed is specified with uspoof_setChecks().
  984. *
  985. * @param sc The USpoofChecker
  986. * @param id The identifier to be checked for possible security issues,
  987. * in UTF-16 format.
  988. * @param length the length of the string to be checked, or -1 if the string is
  989. * zero terminated.
  990. * @param checkResult An instance of USpoofCheckResult to be filled with
  991. * details about the identifier. Can be NULL.
  992. * @param status The error code, set if an error occurred while attempting to
  993. * perform the check.
  994. * Spoofing or security issues detected with the input string are
  995. * not reported here, but through the function's return value.
  996. * @return An integer value with bits set for any potential security
  997. * or spoofing issues detected. The bits are defined by
  998. * enum USpoofChecks. (returned_value & USPOOF_ALL_CHECKS)
  999. * will be zero if the input string passes all of the
  1000. * enabled checks. Any information in this bitmask will be
  1001. * consistent with the information saved in the optional
  1002. * checkResult parameter.
  1003. * @see uspoof_openCheckResult
  1004. * @see uspoof_check2UTF8
  1005. * @see uspoof_check2UnicodeString
  1006. * @stable ICU 58
  1007. */
  1008. U_CAPI int32_t U_EXPORT2
  1009. uspoof_check2(const USpoofChecker *sc,
  1010. const UChar* id, int32_t length,
  1011. USpoofCheckResult* checkResult,
  1012. UErrorCode *status);
  1013. /**
  1014. * Check the specified string for possible security issues.
  1015. * The text to be checked will typically be an identifier of some sort.
  1016. * The set of checks to be performed is specified with uspoof_setChecks().
  1017. *
  1018. * This version of {@link uspoof_check} accepts a USpoofCheckResult, which
  1019. * returns additional information about the identifier. For more
  1020. * information, see {@link uspoof_openCheckResult}.
  1021. *
  1022. * @param sc The USpoofChecker
  1023. * @param id A identifier to be checked for possible security issues, in UTF8 format.
  1024. * @param length the length of the string to be checked, or -1 if the string is
  1025. * zero terminated.
  1026. * @param checkResult An instance of USpoofCheckResult to be filled with
  1027. * details about the identifier. Can be NULL.
  1028. * @param status The error code, set if an error occurred while attempting to
  1029. * perform the check.
  1030. * Spoofing or security issues detected with the input string are
  1031. * not reported here, but through the function's return value.
  1032. * @return An integer value with bits set for any potential security
  1033. * or spoofing issues detected. The bits are defined by
  1034. * enum USpoofChecks. (returned_value & USPOOF_ALL_CHECKS)
  1035. * will be zero if the input string passes all of the
  1036. * enabled checks. Any information in this bitmask will be
  1037. * consistent with the information saved in the optional
  1038. * checkResult parameter.
  1039. * @see uspoof_openCheckResult
  1040. * @see uspoof_check2
  1041. * @see uspoof_check2UnicodeString
  1042. * @stable ICU 58
  1043. */
  1044. U_CAPI int32_t U_EXPORT2
  1045. uspoof_check2UTF8(const USpoofChecker *sc,
  1046. const char *id, int32_t length,
  1047. USpoofCheckResult* checkResult,
  1048. UErrorCode *status);
  1049. /**
  1050. * Create a USpoofCheckResult, used by the {@link uspoof_check2} class of functions to return
  1051. * information about the identifier. Information includes:
  1052. * <ul>
  1053. * <li>A bitmask of the checks that failed</li>
  1054. * <li>The identifier's restriction level (UTS 39 section 5.2)</li>
  1055. * <li>The set of numerics in the string (UTS 39 section 5.3)</li>
  1056. * </ul>
  1057. * The data held in a USpoofCheckResult is cleared whenever it is passed into a new call
  1058. * of {@link uspoof_check2}.
  1059. *
  1060. * @param status The error code, set if this function encounters a problem.
  1061. * @return the newly created USpoofCheckResult
  1062. * @see uspoof_check2
  1063. * @see uspoof_check2UTF8
  1064. * @see uspoof_check2UnicodeString
  1065. * @stable ICU 58
  1066. */
  1067. U_CAPI USpoofCheckResult* U_EXPORT2
  1068. uspoof_openCheckResult(UErrorCode *status);
  1069. /**
  1070. * Close a USpoofCheckResult, freeing any memory that was being held by
  1071. * its implementation.
  1072. *
  1073. * @param checkResult The instance of USpoofCheckResult to close
  1074. * @stable ICU 58
  1075. */
  1076. U_CAPI void U_EXPORT2
  1077. uspoof_closeCheckResult(USpoofCheckResult *checkResult);
  1078. /**
  1079. * Indicates which of the spoof check(s) have failed. The value is a bitwise OR of the constants for the tests
  1080. * in question: USPOOF_RESTRICTION_LEVEL, USPOOF_CHAR_LIMIT, and so on.
  1081. *
  1082. * @param checkResult The instance of USpoofCheckResult created by {@link uspoof_openCheckResult}
  1083. * @param status The error code, set if an error occurred.
  1084. * @return An integer value with bits set for any potential security
  1085. * or spoofing issues detected. The bits are defined by
  1086. * enum USpoofChecks. (returned_value & USPOOF_ALL_CHECKS)
  1087. * will be zero if the input string passes all of the
  1088. * enabled checks.
  1089. * @see uspoof_setChecks
  1090. * @stable ICU 58
  1091. */
  1092. U_CAPI int32_t U_EXPORT2
  1093. uspoof_getCheckResultChecks(const USpoofCheckResult *checkResult, UErrorCode *status);
  1094. /**
  1095. * Gets the restriction level that the text meets, if the USPOOF_RESTRICTION_LEVEL check
  1096. * was enabled; otherwise, undefined.
  1097. *
  1098. * @param checkResult The instance of USpoofCheckResult created by {@link uspoof_openCheckResult}
  1099. * @param status The error code, set if an error occurred.
  1100. * @return The restriction level contained in the USpoofCheckResult
  1101. * @see uspoof_setRestrictionLevel
  1102. * @stable ICU 58
  1103. */
  1104. U_CAPI URestrictionLevel U_EXPORT2
  1105. uspoof_getCheckResultRestrictionLevel(const USpoofCheckResult *checkResult, UErrorCode *status);
  1106. /**
  1107. * Gets the set of numerics found in the string, if the USPOOF_MIXED_NUMBERS check was enabled;
  1108. * otherwise, undefined. The set will contain the zero digit from each decimal number system found
  1109. * in the input string. Ownership of the returned USet remains with the USpoofCheckResult.
  1110. * The USet will be free'd when {@link uspoof_closeCheckResult} is called.
  1111. *
  1112. * @param checkResult The instance of USpoofCheckResult created by {@link uspoof_openCheckResult}
  1113. * @return The set of numerics contained in the USpoofCheckResult
  1114. * @param status The error code, set if an error occurred.
  1115. * @stable ICU 58
  1116. */
  1117. U_CAPI const USet* U_EXPORT2
  1118. uspoof_getCheckResultNumerics(const USpoofCheckResult *checkResult, UErrorCode *status);
  1119. /**
  1120. * Check whether two specified strings are visually confusable.
  1121. *
  1122. * If the strings are confusable, the return value will be nonzero, as long as
  1123. * {@link USPOOF_CONFUSABLE} was enabled in uspoof_setChecks().
  1124. *
  1125. * The bits in the return value correspond to flags for each of the classes of
  1126. * confusables applicable to the two input strings. According to UTS 39
  1127. * section 4, the possible flags are:
  1128. *
  1129. * <ul>
  1130. * <li>{@link USPOOF_SINGLE_SCRIPT_CONFUSABLE}</li>
  1131. * <li>{@link USPOOF_MIXED_SCRIPT_CONFUSABLE}</li>
  1132. * <li>{@link USPOOF_WHOLE_SCRIPT_CONFUSABLE}</li>
  1133. * </ul>
  1134. *
  1135. * If one or more of the above flags were not listed in uspoof_setChecks(), this
  1136. * function will never report that class of confusable. The check
  1137. * {@link USPOOF_CONFUSABLE} enables all three flags.
  1138. *
  1139. *
  1140. * @param sc The USpoofChecker
  1141. * @param id1 The first of the two identifiers to be compared for
  1142. * confusability. The strings are in UTF-16 format.
  1143. * @param length1 the length of the first identifier, expressed in
  1144. * 16 bit UTF-16 code units, or -1 if the string is
  1145. * nul terminated.
  1146. * @param id2 The second of the two identifiers to be compared for
  1147. * confusability. The identifiers are in UTF-16 format.
  1148. * @param length2 The length of the second identifiers, expressed in
  1149. * 16 bit UTF-16 code units, or -1 if the string is
  1150. * nul terminated.
  1151. * @param status The error code, set if an error occurred while attempting to
  1152. * perform the check.
  1153. * Confusability of the identifiers is not reported here,
  1154. * but through this function's return value.
  1155. * @return An integer value with bit(s) set corresponding to
  1156. * the type of confusability found, as defined by
  1157. * enum USpoofChecks. Zero is returned if the identifiers
  1158. * are not confusable.
  1159. *
  1160. * @stable ICU 4.2
  1161. */
  1162. U_CAPI int32_t U_EXPORT2
  1163. uspoof_areConfusable(const USpoofChecker *sc,
  1164. const UChar *id1, int32_t length1,
  1165. const UChar *id2, int32_t length2,
  1166. UErrorCode *status);
  1167. #ifndef U_HIDE_DRAFT_API
  1168. /**
  1169. * Check whether two specified strings are visually confusable when
  1170. * displayed in a context with the given paragraph direction.
  1171. *
  1172. * If the strings are confusable, the return value will be nonzero, as long as
  1173. * {@link USPOOF_CONFUSABLE} was enabled in uspoof_setChecks().
  1174. *
  1175. * The bits in the return value correspond to flags for each of the classes of
  1176. * confusables applicable to the two input strings. According to UTS 39
  1177. * section 4, the possible flags are:
  1178. *
  1179. * <ul>
  1180. * <li>{@link USPOOF_SINGLE_SCRIPT_CONFUSABLE}</li>
  1181. * <li>{@link USPOOF_MIXED_SCRIPT_CONFUSABLE}</li>
  1182. * <li>{@link USPOOF_WHOLE_SCRIPT_CONFUSABLE}</li>
  1183. * </ul>
  1184. *
  1185. * If one or more of the above flags were not listed in uspoof_setChecks(), this
  1186. * function will never report that class of confusable. The check
  1187. * {@link USPOOF_CONFUSABLE} enables all three flags.
  1188. *
  1189. *
  1190. * @param sc The USpoofChecker
  1191. * @param direction The paragraph direction with which the identifiers are
  1192. * displayed. Must be either UBIDI_LTR or UBIDI_RTL.
  1193. * @param id1 The first of the two identifiers to be compared for
  1194. * confusability. The strings are in UTF-16 format.
  1195. * @param length1 the length of the first identifier, expressed in
  1196. * 16 bit UTF-16 code units, or -1 if the string is
  1197. * nul terminated.
  1198. * @param id2 The second of the two identifiers to be compared for
  1199. * confusability. The identifiers are in UTF-16 format.
  1200. * @param length2 The length of the second identifiers, expressed in
  1201. * 16 bit UTF-16 code units, or -1 if the string is
  1202. * nul terminated.
  1203. * @param status The error code, set if an error occurred while attempting to
  1204. * perform the check.
  1205. * Confusability of the identifiers is not reported here,
  1206. * but through this function's return value.
  1207. * @return An integer value with bit(s) set corresponding to
  1208. * the type of confusability found, as defined by
  1209. * enum USpoofChecks. Zero is returned if the identifiers
  1210. * are not confusable.
  1211. *
  1212. * @draft ICU 74
  1213. */
  1214. U_CAPI uint32_t U_EXPORT2 uspoof_areBidiConfusable(const USpoofChecker *sc, UBiDiDirection direction,
  1215. const UChar *id1, int32_t length1,
  1216. const UChar *id2, int32_t length2,
  1217. UErrorCode *status);
  1218. #endif /* U_HIDE_DRAFT_API */
  1219. /**
  1220. * A version of {@link uspoof_areConfusable} accepting strings in UTF-8 format.
  1221. *
  1222. * @param sc The USpoofChecker
  1223. * @param id1 The first of the two identifiers to be compared for
  1224. * confusability. The strings are in UTF-8 format.
  1225. * @param length1 the length of the first identifiers, in bytes, or -1
  1226. * if the string is nul terminated.
  1227. * @param id2 The second of the two identifiers to be compared for
  1228. * confusability. The strings are in UTF-8 format.
  1229. * @param length2 The length of the second string in bytes, or -1
  1230. * if the string is nul terminated.
  1231. * @param status The error code, set if an error occurred while attempting to
  1232. * perform the check.
  1233. * Confusability of the strings is not reported here,
  1234. * but through this function's return value.
  1235. * @return An integer value with bit(s) set corresponding to
  1236. * the type of confusability found, as defined by
  1237. * enum USpoofChecks. Zero is returned if the strings
  1238. * are not confusable.
  1239. *
  1240. * @stable ICU 4.2
  1241. *
  1242. * @see uspoof_areConfusable
  1243. */
  1244. U_CAPI int32_t U_EXPORT2
  1245. uspoof_areConfusableUTF8(const USpoofChecker *sc,
  1246. const char *id1, int32_t length1,
  1247. const char *id2, int32_t length2,
  1248. UErrorCode *status);
  1249. #ifndef U_HIDE_DRAFT_API
  1250. /**
  1251. * A version of {@link uspoof_areBidiConfusable} accepting strings in UTF-8 format.
  1252. *
  1253. * @param sc The USpoofChecker
  1254. * @param direction The paragraph direction with which the identifiers are
  1255. * displayed. Must be either UBIDI_LTR or UBIDI_RTL.
  1256. * @param id1 The first of the two identifiers to be compared for
  1257. * confusability. The strings are in UTF-8 format.
  1258. * @param length1 the length of the first identifiers, in bytes, or -1
  1259. * if the string is nul terminated.
  1260. * @param id2 The second of the two identifiers to be compared for
  1261. * confusability. The strings are in UTF-8 format.
  1262. * @param length2 The length of the second string in bytes, or -1
  1263. * if the string is nul terminated.
  1264. * @param status The error code, set if an error occurred while attempting to
  1265. * perform the check.
  1266. * Confusability of the strings is not reported here,
  1267. * but through this function's return value.
  1268. * @return An integer value with bit(s) set corresponding to
  1269. * the type of confusability found, as defined by
  1270. * enum USpoofChecks. Zero is returned if the strings
  1271. * are not confusable.
  1272. *
  1273. * @draft ICU 74
  1274. *
  1275. * @see uspoof_areBidiConfusable
  1276. */
  1277. U_CAPI uint32_t U_EXPORT2 uspoof_areBidiConfusableUTF8(const USpoofChecker *sc, UBiDiDirection direction,
  1278. const char *id1, int32_t length1,
  1279. const char *id2, int32_t length2,
  1280. UErrorCode *status);
  1281. #endif /* U_HIDE_DRAFT_API */
  1282. /**
  1283. * Get the "skeleton" for an identifier.
  1284. * Skeletons are a transformation of the input identifier;
  1285. * Two identifiers are confusable if their skeletons are identical.
  1286. * See Unicode Technical Standard #39 for additional information.
  1287. *
  1288. * Using skeletons directly makes it possible to quickly check
  1289. * whether an identifier is confusable with any of some large
  1290. * set of existing identifiers, by creating an efficiently
  1291. * searchable collection of the skeletons.
  1292. *
  1293. * @param sc The USpoofChecker
  1294. * @param type Deprecated in ICU 58. You may pass any number.
  1295. * Originally, controlled which of the Unicode confusable data
  1296. * tables to use.
  1297. * @param id The input identifier whose skeleton will be computed.
  1298. * @param length The length of the input identifier, expressed in 16 bit
  1299. * UTF-16 code units, or -1 if the string is zero terminated.
  1300. * @param dest The output buffer, to receive the skeleton string.
  1301. * @param destCapacity The length of the output buffer, in 16 bit units.
  1302. * The destCapacity may be zero, in which case the function will
  1303. * return the actual length of the skeleton.
  1304. * @param status The error code, set if an error occurred while attempting to
  1305. * perform the check.
  1306. * @return The length of the skeleton string. The returned length
  1307. * is always that of the complete skeleton, even when the
  1308. * supplied buffer is too small (or of zero length)
  1309. *
  1310. * @stable ICU 4.2
  1311. * @see uspoof_areConfusable
  1312. */
  1313. U_CAPI int32_t U_EXPORT2
  1314. uspoof_getSkeleton(const USpoofChecker *sc,
  1315. uint32_t type,
  1316. const UChar *id, int32_t length,
  1317. UChar *dest, int32_t destCapacity,
  1318. UErrorCode *status);
  1319. #ifndef U_HIDE_DRAFT_API
  1320. /**
  1321. * Get the "bidiSkeleton" for an identifier and a direction.
  1322. * Skeletons are a transformation of the input identifier;
  1323. * Two identifiers are LTR-confusable if their LTR bidiSkeletons are identical;
  1324. * they are RTL-confusable if their RTL bidiSkeletons are identical.
  1325. * See Unicode Technical Standard #39 for additional information:
  1326. * https://www.unicode.org/reports/tr39/#Confusable_Detection.
  1327. *
  1328. * Using skeletons directly makes it possible to quickly check
  1329. * whether an identifier is confusable with any of some large
  1330. * set of existing identifiers, by creating an efficiently
  1331. * searchable collection of the skeletons.
  1332. *
  1333. * @param sc The USpoofChecker.
  1334. * @param direction The context direction with which the identifier will be
  1335. * displayed. Must be either UBIDI_LTR or UBIDI_RTL.
  1336. * @param id The input identifier whose skeleton will be computed.
  1337. * @param length The length of the input identifier, expressed in 16 bit
  1338. * UTF-16 code units, or -1 if the string is zero terminated.
  1339. * @param dest The output buffer, to receive the skeleton string.
  1340. * @param destCapacity The length of the output buffer, in 16 bit units.
  1341. * The destCapacity may be zero, in which case the function will
  1342. * return the actual length of the skeleton.
  1343. * @param status The error code, set if an error occurred while attempting to
  1344. * perform the check.
  1345. * @return The length of the skeleton string. The returned length
  1346. * is always that of the complete skeleton, even when the
  1347. * supplied buffer is too small (or of zero length)
  1348. *
  1349. * @draft ICU 74
  1350. * @see uspoof_areBidiConfusable
  1351. */
  1352. U_CAPI int32_t U_EXPORT2 uspoof_getBidiSkeleton(const USpoofChecker *sc,
  1353. UBiDiDirection direction,
  1354. const UChar *id, int32_t length,
  1355. UChar *dest, int32_t destCapacity, UErrorCode *status);
  1356. #endif /* U_HIDE_DRAFT_API */
  1357. /**
  1358. * Get the "skeleton" for an identifier.
  1359. * Skeletons are a transformation of the input identifier;
  1360. * Two identifiers are confusable if their skeletons are identical.
  1361. * See Unicode Technical Standard #39 for additional information.
  1362. *
  1363. * Using skeletons directly makes it possible to quickly check
  1364. * whether an identifier is confusable with any of some large
  1365. * set of existing identifiers, by creating an efficiently
  1366. * searchable collection of the skeletons.
  1367. *
  1368. * @param sc The USpoofChecker
  1369. * @param type Deprecated in ICU 58. You may pass any number.
  1370. * Originally, controlled which of the Unicode confusable data
  1371. * tables to use.
  1372. * @param id The UTF-8 format identifier whose skeleton will be computed.
  1373. * @param length The length of the input string, in bytes,
  1374. * or -1 if the string is zero terminated.
  1375. * @param dest The output buffer, to receive the skeleton string.
  1376. * @param destCapacity The length of the output buffer, in bytes.
  1377. * The destCapacity may be zero, in which case the function will
  1378. * return the actual length of the skeleton.
  1379. * @param status The error code, set if an error occurred while attempting to
  1380. * perform the check. Possible Errors include U_INVALID_CHAR_FOUND
  1381. * for invalid UTF-8 sequences, and
  1382. * U_BUFFER_OVERFLOW_ERROR if the destination buffer is too small
  1383. * to hold the complete skeleton.
  1384. * @return The length of the skeleton string, in bytes. The returned length
  1385. * is always that of the complete skeleton, even when the
  1386. * supplied buffer is too small (or of zero length)
  1387. *
  1388. * @stable ICU 4.2
  1389. */
  1390. U_CAPI int32_t U_EXPORT2
  1391. uspoof_getSkeletonUTF8(const USpoofChecker *sc,
  1392. uint32_t type,
  1393. const char *id, int32_t length,
  1394. char *dest, int32_t destCapacity,
  1395. UErrorCode *status);
  1396. #ifndef U_HIDE_DRAFT_API
  1397. /**
  1398. * Get the "bidiSkeleton" for an identifier and a direction.
  1399. * Skeletons are a transformation of the input identifier;
  1400. * Two identifiers are LTR-confusable if their LTR bidiSkeletons are identical;
  1401. * they are RTL-confusable if their RTL bidiSkeletons are identical.
  1402. * See Unicode Technical Standard #39 for additional information:
  1403. * https://www.unicode.org/reports/tr39/#Confusable_Detection.
  1404. *
  1405. * Using skeletons directly makes it possible to quickly check
  1406. * whether an identifier is confusable with any of some large
  1407. * set of existing identifiers, by creating an efficiently
  1408. * searchable collection of the skeletons.
  1409. *
  1410. * @param sc The USpoofChecker
  1411. * @param direction The context direction with which the identifier will be
  1412. * displayed. Must be either UBIDI_LTR or UBIDI_RTL.
  1413. * @param id The UTF-8 format identifier whose skeleton will be computed.
  1414. * @param length The length of the input string, in bytes,
  1415. * or -1 if the string is zero terminated.
  1416. * @param dest The output buffer, to receive the skeleton string.
  1417. * @param destCapacity The length of the output buffer, in bytes.
  1418. * The destCapacity may be zero, in which case the function will
  1419. * return the actual length of the skeleton.
  1420. * @param status The error code, set if an error occurred while attempting to
  1421. * perform the check. Possible Errors include U_INVALID_CHAR_FOUND
  1422. * for invalid UTF-8 sequences, and
  1423. * U_BUFFER_OVERFLOW_ERROR if the destination buffer is too small
  1424. * to hold the complete skeleton.
  1425. * @return The length of the skeleton string, in bytes. The returned length
  1426. * is always that of the complete skeleton, even when the
  1427. * supplied buffer is too small (or of zero length)
  1428. *
  1429. * @draft ICU 74
  1430. */
  1431. U_CAPI int32_t U_EXPORT2 uspoof_getBidiSkeletonUTF8(const USpoofChecker *sc, UBiDiDirection direction,
  1432. const char *id, int32_t length, char *dest,
  1433. int32_t destCapacity, UErrorCode *status);
  1434. #endif /* U_HIDE_DRAFT_API */
  1435. /**
  1436. * Get the set of Candidate Characters for Inclusion in Identifiers, as defined
  1437. * in http://unicode.org/Public/security/latest/xidmodifications.txt
  1438. * and documented in http://www.unicode.org/reports/tr39/, Unicode Security Mechanisms.
  1439. *
  1440. * The returned set is frozen. Ownership of the set remains with the ICU library; it must not
  1441. * be deleted by the caller.
  1442. *
  1443. * @param status The error code, set if a problem occurs while creating the set.
  1444. *
  1445. * @stable ICU 51
  1446. */
  1447. U_CAPI const USet * U_EXPORT2
  1448. uspoof_getInclusionSet(UErrorCode *status);
  1449. /**
  1450. * Get the set of characters from Recommended Scripts for Inclusion in Identifiers, as defined
  1451. * in http://unicode.org/Public/security/latest/xidmodifications.txt
  1452. * and documented in http://www.unicode.org/reports/tr39/, Unicode Security Mechanisms.
  1453. *
  1454. * The returned set is frozen. Ownership of the set remains with the ICU library; it must not
  1455. * be deleted by the caller.
  1456. *
  1457. * @param status The error code, set if a problem occurs while creating the set.
  1458. *
  1459. * @stable ICU 51
  1460. */
  1461. U_CAPI const USet * U_EXPORT2
  1462. uspoof_getRecommendedSet(UErrorCode *status);
  1463. /**
  1464. * Serialize the data for a spoof detector into a chunk of memory.
  1465. * The flattened spoof detection tables can later be used to efficiently
  1466. * instantiate a new Spoof Detector.
  1467. *
  1468. * The serialized spoof checker includes only the data compiled from the
  1469. * Unicode data tables by uspoof_openFromSource(); it does not include
  1470. * include any other state or configuration that may have been set.
  1471. *
  1472. * @param sc the Spoof Detector whose data is to be serialized.
  1473. * @param data a pointer to 32-bit-aligned memory to be filled with the data,
  1474. * can be NULL if capacity==0
  1475. * @param capacity the number of bytes available at data,
  1476. * or 0 for preflighting
  1477. * @param status an in/out ICU UErrorCode; possible errors include:
  1478. * - U_BUFFER_OVERFLOW_ERROR if the data storage block is too small for serialization
  1479. * - U_ILLEGAL_ARGUMENT_ERROR the data or capacity parameters are bad
  1480. * @return the number of bytes written or needed for the spoof data
  1481. *
  1482. * @see utrie2_openFromSerialized()
  1483. * @stable ICU 4.2
  1484. */
  1485. U_CAPI int32_t U_EXPORT2
  1486. uspoof_serialize(USpoofChecker *sc,
  1487. void *data, int32_t capacity,
  1488. UErrorCode *status);
  1489. U_CDECL_END
  1490. #if U_SHOW_CPLUSPLUS_API
  1491. U_NAMESPACE_BEGIN
  1492. /**
  1493. * \class LocalUSpoofCheckerPointer
  1494. * "Smart pointer" class, closes a USpoofChecker via uspoof_close().
  1495. * For most methods see the LocalPointerBase base class.
  1496. *
  1497. * @see LocalPointerBase
  1498. * @see LocalPointer
  1499. * @stable ICU 4.4
  1500. */
  1501. /**
  1502. * \cond
  1503. * Note: Doxygen is giving a bogus warning on this U_DEFINE_LOCAL_OPEN_POINTER.
  1504. * For now, suppress with a Doxygen cond
  1505. */
  1506. U_DEFINE_LOCAL_OPEN_POINTER(LocalUSpoofCheckerPointer, USpoofChecker, uspoof_close);
  1507. /** \endcond */
  1508. /**
  1509. * \class LocalUSpoofCheckResultPointer
  1510. * "Smart pointer" class, closes a USpoofCheckResult via `uspoof_closeCheckResult()`.
  1511. * For most methods see the LocalPointerBase base class.
  1512. *
  1513. * @see LocalPointerBase
  1514. * @see LocalPointer
  1515. * @stable ICU 58
  1516. */
  1517. /**
  1518. * \cond
  1519. * Note: Doxygen is giving a bogus warning on this U_DEFINE_LOCAL_OPEN_POINTER.
  1520. * For now, suppress with a Doxygen cond
  1521. */
  1522. U_DEFINE_LOCAL_OPEN_POINTER(LocalUSpoofCheckResultPointer, USpoofCheckResult, uspoof_closeCheckResult);
  1523. /** \endcond */
  1524. U_NAMESPACE_END
  1525. /**
  1526. * Limit the acceptable characters to those specified by a Unicode Set.
  1527. * Any previously specified character limit is
  1528. * is replaced by the new settings. This includes limits on
  1529. * characters that were set with the uspoof_setAllowedLocales() function.
  1530. *
  1531. * The USPOOF_CHAR_LIMIT test is automatically enabled for this
  1532. * USoofChecker by this function.
  1533. *
  1534. * @param sc The USpoofChecker
  1535. * @param chars A Unicode Set containing the list of
  1536. * characters that are permitted. Ownership of the set
  1537. * remains with the caller. The incoming set is cloned by
  1538. * this function, so there are no restrictions on modifying
  1539. * or deleting the UnicodeSet after calling this function.
  1540. * @param status The error code, set if this function encounters a problem.
  1541. * @stable ICU 4.2
  1542. */
  1543. U_CAPI void U_EXPORT2
  1544. uspoof_setAllowedUnicodeSet(USpoofChecker *sc, const icu::UnicodeSet *chars, UErrorCode *status);
  1545. /**
  1546. * Get a UnicodeSet for the characters permitted in an identifier.
  1547. * This corresponds to the limits imposed by the Set Allowed Characters /
  1548. * UnicodeSet functions. Limitations imposed by other checks will not be
  1549. * reflected in the set returned by this function.
  1550. *
  1551. * The returned set will be frozen, meaning that it cannot be modified
  1552. * by the caller.
  1553. *
  1554. * Ownership of the returned set remains with the Spoof Detector. The
  1555. * returned set will become invalid if the spoof detector is closed,
  1556. * or if a new set of allowed characters is specified.
  1557. *
  1558. *
  1559. * @param sc The USpoofChecker
  1560. * @param status The error code, set if this function encounters a problem.
  1561. * @return A UnicodeSet containing the characters that are permitted by
  1562. * the USPOOF_CHAR_LIMIT test.
  1563. * @stable ICU 4.2
  1564. */
  1565. U_CAPI const icu::UnicodeSet * U_EXPORT2
  1566. uspoof_getAllowedUnicodeSet(const USpoofChecker *sc, UErrorCode *status);
  1567. /**
  1568. * Check the specified string for possible security issues.
  1569. * The text to be checked will typically be an identifier of some sort.
  1570. * The set of checks to be performed is specified with uspoof_setChecks().
  1571. *
  1572. * \note
  1573. * Consider using the newer API, {@link uspoof_check2UnicodeString}, instead.
  1574. * The newer API exposes additional information from the check procedure
  1575. * and is otherwise identical to this method.
  1576. *
  1577. * @param sc The USpoofChecker
  1578. * @param id A identifier to be checked for possible security issues.
  1579. * @param position Deprecated in ICU 51. Always returns zero.
  1580. * Originally, an out parameter for the index of the first
  1581. * string position that failed a check.
  1582. * This parameter may be nullptr.
  1583. * @param status The error code, set if an error occurred while attempting to
  1584. * perform the check.
  1585. * Spoofing or security issues detected with the input string are
  1586. * not reported here, but through the function's return value.
  1587. * @return An integer value with bits set for any potential security
  1588. * or spoofing issues detected. The bits are defined by
  1589. * enum USpoofChecks. (returned_value & USPOOF_ALL_CHECKS)
  1590. * will be zero if the input string passes all of the
  1591. * enabled checks.
  1592. * @see uspoof_check2UnicodeString
  1593. * @stable ICU 4.2
  1594. */
  1595. U_CAPI int32_t U_EXPORT2
  1596. uspoof_checkUnicodeString(const USpoofChecker *sc,
  1597. const icu::UnicodeString &id,
  1598. int32_t *position,
  1599. UErrorCode *status);
  1600. /**
  1601. * Check the specified string for possible security issues.
  1602. * The text to be checked will typically be an identifier of some sort.
  1603. * The set of checks to be performed is specified with uspoof_setChecks().
  1604. *
  1605. * @param sc The USpoofChecker
  1606. * @param id A identifier to be checked for possible security issues.
  1607. * @param checkResult An instance of USpoofCheckResult to be filled with
  1608. * details about the identifier. Can be nullptr.
  1609. * @param status The error code, set if an error occurred while attempting to
  1610. * perform the check.
  1611. * Spoofing or security issues detected with the input string are
  1612. * not reported here, but through the function's return value.
  1613. * @return An integer value with bits set for any potential security
  1614. * or spoofing issues detected. The bits are defined by
  1615. * enum USpoofChecks. (returned_value & USPOOF_ALL_CHECKS)
  1616. * will be zero if the input string passes all of the
  1617. * enabled checks. Any information in this bitmask will be
  1618. * consistent with the information saved in the optional
  1619. * checkResult parameter.
  1620. * @see uspoof_openCheckResult
  1621. * @see uspoof_check2
  1622. * @see uspoof_check2UTF8
  1623. * @stable ICU 58
  1624. */
  1625. U_CAPI int32_t U_EXPORT2
  1626. uspoof_check2UnicodeString(const USpoofChecker *sc,
  1627. const icu::UnicodeString &id,
  1628. USpoofCheckResult* checkResult,
  1629. UErrorCode *status);
  1630. /**
  1631. * A version of {@link uspoof_areConfusable} accepting UnicodeStrings.
  1632. *
  1633. * @param sc The USpoofChecker
  1634. * @param s1 The first of the two identifiers to be compared for
  1635. * confusability. The strings are in UTF-8 format.
  1636. * @param s2 The second of the two identifiers to be compared for
  1637. * confusability. The strings are in UTF-8 format.
  1638. * @param status The error code, set if an error occurred while attempting to
  1639. * perform the check.
  1640. * Confusability of the identifiers is not reported here,
  1641. * but through this function's return value.
  1642. * @return An integer value with bit(s) set corresponding to
  1643. * the type of confusability found, as defined by
  1644. * enum USpoofChecks. Zero is returned if the identifiers
  1645. * are not confusable.
  1646. *
  1647. * @stable ICU 4.2
  1648. *
  1649. * @see uspoof_areConfusable
  1650. */
  1651. U_CAPI int32_t U_EXPORT2
  1652. uspoof_areConfusableUnicodeString(const USpoofChecker *sc,
  1653. const icu::UnicodeString &s1,
  1654. const icu::UnicodeString &s2,
  1655. UErrorCode *status);
  1656. #ifndef U_HIDE_DRAFT_API
  1657. /**
  1658. * A version of {@link uspoof_areBidiConfusable} accepting UnicodeStrings.
  1659. *
  1660. * @param sc The USpoofChecker
  1661. * @param direction The paragraph direction with which the identifiers are
  1662. * displayed. Must be either UBIDI_LTR or UBIDI_RTL.
  1663. * @param s1 The first of the two identifiers to be compared for
  1664. * confusability. The strings are in UTF-8 format.
  1665. * @param s2 The second of the two identifiers to be compared for
  1666. * confusability. The strings are in UTF-8 format.
  1667. * @param status The error code, set if an error occurred while attempting to
  1668. * perform the check.
  1669. * Confusability of the identifiers is not reported here,
  1670. * but through this function's return value.
  1671. * @return An integer value with bit(s) set corresponding to
  1672. * the type of confusability found, as defined by
  1673. * enum USpoofChecks. Zero is returned if the identifiers
  1674. * are not confusable.
  1675. *
  1676. * @draft ICU 74
  1677. *
  1678. * @see uspoof_areBidiConfusable
  1679. */
  1680. U_CAPI uint32_t U_EXPORT2 uspoof_areBidiConfusableUnicodeString(const USpoofChecker *sc,
  1681. UBiDiDirection direction,
  1682. const icu::UnicodeString &s1,
  1683. const icu::UnicodeString &s2,
  1684. UErrorCode *status);
  1685. #endif /* U_HIDE_DRAFT_API */
  1686. /**
  1687. * Get the "skeleton" for an identifier.
  1688. * Skeletons are a transformation of the input identifier;
  1689. * Two identifiers are confusable if their skeletons are identical.
  1690. * See Unicode Technical Standard #39 for additional information.
  1691. *
  1692. * Using skeletons directly makes it possible to quickly check
  1693. * whether an identifier is confusable with any of some large
  1694. * set of existing identifiers, by creating an efficiently
  1695. * searchable collection of the skeletons.
  1696. *
  1697. * @param sc The USpoofChecker.
  1698. * @param type Deprecated in ICU 58. You may pass any number.
  1699. * Originally, controlled which of the Unicode confusable data
  1700. * tables to use.
  1701. * @param id The input identifier whose skeleton will be computed.
  1702. * @param dest The output identifier, to receive the skeleton string.
  1703. * @param status The error code, set if an error occurred while attempting to
  1704. * perform the check.
  1705. * @return A reference to the destination (skeleton) string.
  1706. *
  1707. * @stable ICU 4.2
  1708. */
  1709. U_I18N_API icu::UnicodeString & U_EXPORT2
  1710. uspoof_getSkeletonUnicodeString(const USpoofChecker *sc,
  1711. uint32_t type,
  1712. const icu::UnicodeString &id,
  1713. icu::UnicodeString &dest,
  1714. UErrorCode *status);
  1715. #ifndef U_HIDE_DRAFT_API
  1716. /**
  1717. * Get the "bidiSkeleton" for an identifier and a direction.
  1718. * Skeletons are a transformation of the input identifier;
  1719. * Two identifiers are LTR-confusable if their LTR bidiSkeletons are identical;
  1720. * they are RTL-confusable if their RTL bidiSkeletons are identical.
  1721. * See Unicode Technical Standard #39 for additional information.
  1722. * https://www.unicode.org/reports/tr39/#Confusable_Detection.
  1723. *
  1724. * Using skeletons directly makes it possible to quickly check
  1725. * whether an identifier is confusable with any of some large
  1726. * set of existing identifiers, by creating an efficiently
  1727. * searchable collection of the skeletons.
  1728. *
  1729. * @param sc The USpoofChecker.
  1730. * @param direction The context direction with which the identifier will be
  1731. * displayed. Must be either UBIDI_LTR or UBIDI_RTL.
  1732. * @param id The input identifier whose bidiSkeleton will be computed.
  1733. * @param dest The output identifier, to receive the skeleton string.
  1734. * @param status The error code, set if an error occurred while attempting to
  1735. * perform the check.
  1736. * @return A reference to the destination (skeleton) string.
  1737. *
  1738. * @draft ICU 74
  1739. */
  1740. U_I18N_API icu::UnicodeString &U_EXPORT2 uspoof_getBidiSkeletonUnicodeString(
  1741. const USpoofChecker *sc, UBiDiDirection direction, const icu::UnicodeString &id,
  1742. icu::UnicodeString &dest, UErrorCode *status);
  1743. #endif /* U_HIDE_DRAFT_API */
  1744. /**
  1745. * Get the set of Candidate Characters for Inclusion in Identifiers, as defined
  1746. * in http://unicode.org/Public/security/latest/xidmodifications.txt
  1747. * and documented in http://www.unicode.org/reports/tr39/, Unicode Security Mechanisms.
  1748. *
  1749. * The returned set is frozen. Ownership of the set remains with the ICU library; it must not
  1750. * be deleted by the caller.
  1751. *
  1752. * @param status The error code, set if a problem occurs while creating the set.
  1753. *
  1754. * @stable ICU 51
  1755. */
  1756. U_CAPI const icu::UnicodeSet * U_EXPORT2
  1757. uspoof_getInclusionUnicodeSet(UErrorCode *status);
  1758. /**
  1759. * Get the set of characters from Recommended Scripts for Inclusion in Identifiers, as defined
  1760. * in http://unicode.org/Public/security/latest/xidmodifications.txt
  1761. * and documented in http://www.unicode.org/reports/tr39/, Unicode Security Mechanisms.
  1762. *
  1763. * The returned set is frozen. Ownership of the set remains with the ICU library; it must not
  1764. * be deleted by the caller.
  1765. *
  1766. * @param status The error code, set if a problem occurs while creating the set.
  1767. *
  1768. * @stable ICU 51
  1769. */
  1770. U_CAPI const icu::UnicodeSet * U_EXPORT2
  1771. uspoof_getRecommendedUnicodeSet(UErrorCode *status);
  1772. #endif /* U_SHOW_CPLUSPLUS_API */
  1773. #endif /* UCONFIG_NO_NORMALIZATION */
  1774. #endif /* USPOOF_H */