uspoof.h 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /*
  2. ***************************************************************************
  3. * Copyright (C) 2008-2015, International Business Machines Corporation
  4. * and others. All Rights Reserved.
  5. ***************************************************************************
  6. * file name: uspoof.h
  7. * encoding: US-ASCII
  8. * tab size: 8 (not used)
  9. * indentation:4
  10. *
  11. * created on: 2008Feb13
  12. * created by: Andy Heninger
  13. *
  14. * Unicode Spoof Detection
  15. */
  16. #ifndef USPOOF_H
  17. #define USPOOF_H
  18. #include "unicode/utypes.h"
  19. #include "unicode/uset.h"
  20. #include "unicode/parseerr.h"
  21. #include "unicode/localpointer.h"
  22. #if !UCONFIG_NO_NORMALIZATION
  23. #if U_SHOW_CPLUSPLUS_API
  24. #include "unicode/unistr.h"
  25. #include "unicode/uniset.h"
  26. #endif
  27. /**
  28. * \file
  29. * \brief Unicode Security and Spoofing Detection, C API.
  30. *
  31. * These functions are intended to check strings, typically
  32. * identifiers of some type, such as URLs, for the presence of
  33. * characters that are likely to be visually confusing -
  34. * for cases where the displayed form of an identifier may
  35. * not be what it appears to be.
  36. *
  37. * Unicode Technical Report #36, http://unicode.org/reports/tr36, and
  38. * Unicode Technical Standard #39, http://unicode.org/reports/tr39
  39. * "Unicode security considerations", give more background on
  40. * security an spoofing issues with Unicode identifiers.
  41. * The tests and checks provided by this module implement the recommendations
  42. * from those Unicode documents.
  43. *
  44. * The tests available on identifiers fall into two general categories:
  45. * -# Single identifier tests. Check whether an identifier is
  46. * potentially confusable with any other string, or is suspicious
  47. * for other reasons.
  48. * -# Two identifier tests. Check whether two specific identifiers are confusable.
  49. * This does not consider whether either of strings is potentially
  50. * confusable with any string other than the exact one specified.
  51. *
  52. * The steps to perform confusability testing are
  53. * -# Open a USpoofChecker.
  54. * -# Configure the USPoofChecker for the desired set of tests. The tests that will
  55. * be performed are specified by a set of USpoofChecks flags.
  56. * -# Perform the checks using the pre-configured USpoofChecker. The results indicate
  57. * which (if any) of the selected tests have identified possible problems with the identifier.
  58. * Results are reported as a set of USpoofChecks flags; this mirrors the form in which
  59. * the set of tests to perform was originally specified to the USpoofChecker.
  60. *
  61. * A USpoofChecker may be used repeatedly to perform checks on any number of identifiers.
  62. *
  63. * Thread Safety: The test functions for checking a single identifier, or for testing
  64. * whether two identifiers are possible confusable, are thread safe.
  65. * They may called concurrently, from multiple threads, using the same USpoofChecker instance.
  66. *
  67. * More generally, the standard ICU thread safety rules apply: functions that take a
  68. * const USpoofChecker parameter are thread safe. Those that take a non-const
  69. * USpoofChecier are not thread safe.
  70. *
  71. *
  72. * Descriptions of the available checks.
  73. *
  74. * When testing whether pairs of identifiers are confusable, with the uspoof_areConfusable()
  75. * family of functions, the relevant tests are
  76. *
  77. * -# USPOOF_SINGLE_SCRIPT_CONFUSABLE: All of the characters from the two identifiers are
  78. * from a single script, and the two identifiers are visually confusable.
  79. * -# USPOOF_MIXED_SCRIPT_CONFUSABLE: At least one of the identifiers contains characters
  80. * from more than one script, and the two identifiers are visually confusable.
  81. * -# USPOOF_WHOLE_SCRIPT_CONFUSABLE: Each of the two identifiers is of a single script, but
  82. * the two identifiers are from different scripts, and they are visually confusable.
  83. *
  84. * The safest approach is to enable all three of these checks as a group.
  85. *
  86. * USPOOF_ANY_CASE is a modifier for the above tests. If the identifiers being checked can
  87. * be of mixed case and are used in a case-sensitive manner, this option should be specified.
  88. *
  89. * If the identifiers being checked are used in a case-insensitive manner, and if they are
  90. * displayed to users in lower-case form only, the USPOOF_ANY_CASE option should not be
  91. * specified. Confusabality issues involving upper case letters will not be reported.
  92. *
  93. * When performing tests on a single identifier, with the uspoof_check() family of functions,
  94. * the relevant tests are:
  95. *
  96. * -# USPOOF_MIXED_SCRIPT_CONFUSABLE: the identifier contains characters from multiple
  97. * scripts, and there exists an identifier of a single script that is visually confusable.
  98. * -# USPOOF_WHOLE_SCRIPT_CONFUSABLE: the identifier consists of characters from a single
  99. * script, and there exists a visually confusable identifier.
  100. * The visually confusable identifier also consists of characters from a single script.
  101. * but not the same script as the identifier being checked.
  102. * -# USPOOF_ANY_CASE: modifies the mixed script and whole script confusables tests. If
  103. * specified, the checks will consider confusable characters of any case. If this flag is not
  104. * set, the test is performed assuming case folded identifiers.
  105. * -# USPOOF_SINGLE_SCRIPT: check that the identifier contains only characters from a
  106. * single script. (Characters from the 'common' and 'inherited' scripts are ignored.)
  107. * This is not a test for confusable identifiers
  108. * -# USPOOF_INVISIBLE: check an identifier for the presence of invisible characters,
  109. * such as zero-width spaces, or character sequences that are
  110. * likely not to display, such as multiple occurrences of the same
  111. * non-spacing mark. This check does not test the input string as a whole
  112. * for conformance to any particular syntax for identifiers.
  113. * -# USPOOF_CHAR_LIMIT: check that an identifier contains only characters from a specified set
  114. * of acceptable characters. See uspoof_setAllowedChars() and
  115. * uspoof_setAllowedLocales().
  116. *
  117. * Note on Scripts:
  118. * Characters from the Unicode Scripts "Common" and "Inherited" are ignored when considering
  119. * the script of an identifier. Common characters include digits and symbols that
  120. * are normally used with text from more than one script.
  121. *
  122. * Identifier Skeletons: A skeleton is a transformation of an identifier, such that
  123. * all identifiers that are confusable with each other have the same skeleton.
  124. * Using skeletons, it is possible to build a dictionary data structure for
  125. * a set of identifiers, and then quickly test whether a new identifier is
  126. * confusable with an identifier already in the set. The uspoof_getSkeleton()
  127. * family of functions will produce the skeleton from an identifier.
  128. *
  129. * Note that skeletons are not guaranteed to be stable between versions
  130. * of Unicode or ICU, so an applications should not rely on creating a permanent,
  131. * or difficult to update, database of skeletons. Instabilities result from
  132. * identifying new pairs or sequences of characters that are visually
  133. * confusable, and thus must be mapped to the same skeleton character(s).
  134. *
  135. * Skeletons are computed using the algorithm and data describe in Unicode UAX 39.
  136. * The latest proposed update, UAX 39 Version 8 draft 1, says "the tables SL, SA, and ML
  137. * were still problematic, and discouraged from use in [Uniocde] 7.0.
  138. * They were thus removed from version 8.0"
  139. *
  140. * In light of this, the default mapping data included with ICU 55 uses the
  141. * Unicode 7 MA (Multi script Any case) table data for the other type options
  142. * (Single Script, Any Case), (Single Script, Lower Case) and (Multi Script, Lower Case).
  143. */
  144. struct USpoofChecker;
  145. typedef struct USpoofChecker USpoofChecker; /**< typedef for C of USpoofChecker */
  146. /**
  147. * Enum for the kinds of checks that USpoofChecker can perform.
  148. * These enum values are used both to select the set of checks that
  149. * will be performed, and to report results from the check function.
  150. *
  151. * @stable ICU 4.2
  152. */
  153. typedef enum USpoofChecks {
  154. /** Single script confusable test.
  155. * When testing whether two identifiers are confusable, report that they are if
  156. * both are from the same script and they are visually confusable.
  157. * Note: this test is not applicable to a check of a single identifier.
  158. */
  159. USPOOF_SINGLE_SCRIPT_CONFUSABLE = 1,
  160. /** Mixed script confusable test.
  161. * When checking a single identifier, report a problem if
  162. * the identifier contains multiple scripts, and
  163. * is confusable with some other identifier in a single script
  164. * When testing whether two identifiers are confusable, report that they are if
  165. * the two IDs are visually confusable,
  166. * and at least one contains characters from more than one script.
  167. */
  168. USPOOF_MIXED_SCRIPT_CONFUSABLE = 2,
  169. /** Whole script confusable test.
  170. * When checking a single identifier, report a problem if
  171. * The identifier is of a single script, and
  172. * there exists a confusable identifier in another script.
  173. * When testing whether two identifiers are confusable, report that they are if
  174. * each is of a single script,
  175. * the scripts of the two identifiers are different, and
  176. * the identifiers are visually confusable.
  177. */
  178. USPOOF_WHOLE_SCRIPT_CONFUSABLE = 4,
  179. /** Any Case Modifier for confusable identifier tests.
  180. If specified, consider all characters, of any case, when looking for confusables.
  181. If USPOOF_ANY_CASE is not specified, identifiers being checked are assumed to have been
  182. case folded. Upper case confusable characters will not be checked.
  183. Selects between Lower Case Confusable and
  184. Any Case Confusable. */
  185. USPOOF_ANY_CASE = 8,
  186. /**
  187. * Check that an identifier is no looser than the specified RestrictionLevel.
  188. * The default if uspoof_setRestrctionLevel() is not called is HIGHLY_RESTRICTIVE.
  189. *
  190. * If USPOOF_AUX_INFO is enabled the actual restriction level of the
  191. * identifier being tested will also be returned by uspoof_check().
  192. *
  193. * @see URestrictionLevel
  194. * @see uspoof_setRestrictionLevel
  195. * @see USPOOF_AUX_INFO
  196. *
  197. * @stable ICU 51
  198. */
  199. USPOOF_RESTRICTION_LEVEL = 16,
  200. #ifndef U_HIDE_DEPRECATED_API
  201. /** Check that an identifier contains only characters from a
  202. * single script (plus chars from the common and inherited scripts.)
  203. * Applies to checks of a single identifier check only.
  204. * @deprecated ICU 51 Use RESTRICTION_LEVEL instead.
  205. */
  206. USPOOF_SINGLE_SCRIPT = USPOOF_RESTRICTION_LEVEL,
  207. #endif /* U_HIDE_DEPRECATED_API */
  208. /** Check an identifier for the presence of invisible characters,
  209. * such as zero-width spaces, or character sequences that are
  210. * likely not to display, such as multiple occurrences of the same
  211. * non-spacing mark. This check does not test the input string as a whole
  212. * for conformance to any particular syntax for identifiers.
  213. */
  214. USPOOF_INVISIBLE = 32,
  215. /** Check that an identifier contains only characters from a specified set
  216. * of acceptable characters. See uspoof_setAllowedChars() and
  217. * uspoof_setAllowedLocales().
  218. */
  219. USPOOF_CHAR_LIMIT = 64,
  220. /**
  221. * Check that an identifier does not include decimal digits from
  222. * more than one numbering system.
  223. *
  224. * @stable ICU 51
  225. */
  226. USPOOF_MIXED_NUMBERS = 128,
  227. /**
  228. * Enable all spoof checks.
  229. *
  230. * @stable ICU 4.6
  231. */
  232. USPOOF_ALL_CHECKS = 0xFFFF,
  233. /**
  234. * Enable the return of auxillary (non-error) information in the
  235. * upper bits of the check results value.
  236. *
  237. * If this "check" is not enabled, the results of uspoof_check() will be zero when an
  238. * identifier passes all of the enabled checks.
  239. *
  240. * If this "check" is enabled, (uspoof_check() & USPOOF_ALL_CHECKS) will be zero
  241. * when an identifier passes all checks.
  242. *
  243. * @stable ICU 51
  244. */
  245. USPOOF_AUX_INFO = 0x40000000
  246. } USpoofChecks;
  247. /**
  248. * Constants from UAX #39 for use in setRestrictionLevel(), and
  249. * for returned identifier restriction levels in check results.
  250. * @stable ICU 51
  251. */
  252. typedef enum URestrictionLevel {
  253. /**
  254. * Only ASCII characters: U+0000..U+007F
  255. *
  256. * @stable ICU 51
  257. */
  258. USPOOF_ASCII = 0x10000000,
  259. /**
  260. * All characters in each identifier must be from a single script.
  261. *
  262. * @stable ICU 53
  263. */
  264. USPOOF_SINGLE_SCRIPT_RESTRICTIVE = 0x20000000,
  265. /**
  266. * All characters in each identifier must be from a single script, or from the combinations: Latin + Han +
  267. * Hiragana + Katakana; Latin + Han + Bopomofo; or Latin + Han + Hangul. Note that this level will satisfy the
  268. * vast majority of Latin-script users; also that TR36 has ASCII instead of Latin.
  269. *
  270. * @stable ICU 51
  271. */
  272. USPOOF_HIGHLY_RESTRICTIVE = 0x30000000,
  273. /**
  274. * Allow Latin with other scripts except Cyrillic, Greek, Cherokee Otherwise, the same as Highly Restrictive
  275. *
  276. * @stable ICU 51
  277. */
  278. USPOOF_MODERATELY_RESTRICTIVE = 0x40000000,
  279. /**
  280. * Allow arbitrary mixtures of scripts. Otherwise, the same as Moderately Restrictive.
  281. *
  282. * @stable ICU 51
  283. */
  284. USPOOF_MINIMALLY_RESTRICTIVE = 0x50000000,
  285. /**
  286. * Any valid identifiers, including characters outside of the Identifier Profile.
  287. *
  288. * @stable ICU 51
  289. */
  290. USPOOF_UNRESTRICTIVE = 0x60000000,
  291. /**
  292. * Mask for selecting the Restriction Level bits from the return value of uspoof_check().
  293. *
  294. * @stable ICU 53
  295. */
  296. USPOOF_RESTRICTION_LEVEL_MASK = 0x7F000000
  297. } URestrictionLevel;
  298. /**
  299. * Create a Unicode Spoof Checker, configured to perform all
  300. * checks except for USPOOF_LOCALE_LIMIT and USPOOF_CHAR_LIMIT.
  301. * Note that additional checks may be added in the future,
  302. * resulting in the changes to the default checking behavior.
  303. *
  304. * @param status The error code, set if this function encounters a problem.
  305. * @return the newly created Spoof Checker
  306. * @stable ICU 4.2
  307. */
  308. U_STABLE USpoofChecker * U_EXPORT2
  309. uspoof_open(UErrorCode *status);
  310. /**
  311. * Open a Spoof checker from its serialized form, stored in 32-bit-aligned memory.
  312. * Inverse of uspoof_serialize().
  313. * The memory containing the serialized data must remain valid and unchanged
  314. * as long as the spoof checker, or any cloned copies of the spoof checker,
  315. * are in use. Ownership of the memory remains with the caller.
  316. * The spoof checker (and any clones) must be closed prior to deleting the
  317. * serialized data.
  318. *
  319. * @param data a pointer to 32-bit-aligned memory containing the serialized form of spoof data
  320. * @param length the number of bytes available at data;
  321. * can be more than necessary
  322. * @param pActualLength receives the actual number of bytes at data taken up by the data;
  323. * can be NULL
  324. * @param pErrorCode ICU error code
  325. * @return the spoof checker.
  326. *
  327. * @see uspoof_open
  328. * @see uspoof_serialize
  329. * @stable ICU 4.2
  330. */
  331. U_STABLE USpoofChecker * U_EXPORT2
  332. uspoof_openFromSerialized(const void *data, int32_t length, int32_t *pActualLength,
  333. UErrorCode *pErrorCode);
  334. /**
  335. * Open a Spoof Checker from the source form of the spoof data.
  336. * The two inputs correspond to the Unicode data files confusables.txt
  337. * and confusablesWholeScript.txt as described in Unicode UAX #39.
  338. * The syntax of the source data is as described in UAX #39 for
  339. * these files, and the content of these files is acceptable input.
  340. *
  341. * The character encoding of the (char *) input text is UTF-8.
  342. *
  343. * @param confusables a pointer to the confusable characters definitions,
  344. * as found in file confusables.txt from unicode.org.
  345. * @param confusablesLen The length of the confusables text, or -1 if the
  346. * input string is zero terminated.
  347. * @param confusablesWholeScript
  348. * a pointer to the whole script confusables definitions,
  349. * as found in the file confusablesWholeScript.txt from unicode.org.
  350. * @param confusablesWholeScriptLen The length of the whole script confusables text, or
  351. * -1 if the input string is zero terminated.
  352. * @param errType In the event of an error in the input, indicates
  353. * which of the input files contains the error.
  354. * The value is one of USPOOF_SINGLE_SCRIPT_CONFUSABLE or
  355. * USPOOF_WHOLE_SCRIPT_CONFUSABLE, or
  356. * zero if no errors are found.
  357. * @param pe In the event of an error in the input, receives the position
  358. * in the input text (line, offset) of the error.
  359. * @param status an in/out ICU UErrorCode. Among the possible errors is
  360. * U_PARSE_ERROR, which is used to report syntax errors
  361. * in the input.
  362. * @return A spoof checker that uses the rules from the input files.
  363. * @stable ICU 4.2
  364. */
  365. U_STABLE USpoofChecker * U_EXPORT2
  366. uspoof_openFromSource(const char *confusables, int32_t confusablesLen,
  367. const char *confusablesWholeScript, int32_t confusablesWholeScriptLen,
  368. int32_t *errType, UParseError *pe, UErrorCode *status);
  369. /**
  370. * Close a Spoof Checker, freeing any memory that was being held by
  371. * its implementation.
  372. * @stable ICU 4.2
  373. */
  374. U_STABLE void U_EXPORT2
  375. uspoof_close(USpoofChecker *sc);
  376. #if U_SHOW_CPLUSPLUS_API
  377. U_NAMESPACE_BEGIN
  378. /**
  379. * \class LocalUSpoofCheckerPointer
  380. * "Smart pointer" class, closes a USpoofChecker via uspoof_close().
  381. * For most methods see the LocalPointerBase base class.
  382. *
  383. * @see LocalPointerBase
  384. * @see LocalPointer
  385. * @stable ICU 4.4
  386. */
  387. U_DEFINE_LOCAL_OPEN_POINTER(LocalUSpoofCheckerPointer, USpoofChecker, uspoof_close);
  388. U_NAMESPACE_END
  389. #endif
  390. /**
  391. * Clone a Spoof Checker. The clone will be set to perform the same checks
  392. * as the original source.
  393. *
  394. * @param sc The source USpoofChecker
  395. * @param status The error code, set if this function encounters a problem.
  396. * @return
  397. * @stable ICU 4.2
  398. */
  399. U_STABLE USpoofChecker * U_EXPORT2
  400. uspoof_clone(const USpoofChecker *sc, UErrorCode *status);
  401. /**
  402. * Specify the set of checks that will be performed by the check
  403. * functions of this Spoof Checker.
  404. *
  405. * @param sc The USpoofChecker
  406. * @param checks The set of checks that this spoof checker will perform.
  407. * The value is a bit set, obtained by OR-ing together
  408. * values from enum USpoofChecks.
  409. * @param status The error code, set if this function encounters a problem.
  410. * @stable ICU 4.2
  411. *
  412. */
  413. U_STABLE void U_EXPORT2
  414. uspoof_setChecks(USpoofChecker *sc, int32_t checks, UErrorCode *status);
  415. /**
  416. * Get the set of checks that this Spoof Checker has been configured to perform.
  417. *
  418. * @param sc The USpoofChecker
  419. * @param status The error code, set if this function encounters a problem.
  420. * @return The set of checks that this spoof checker will perform.
  421. * The value is a bit set, obtained by OR-ing together
  422. * values from enum USpoofChecks.
  423. * @stable ICU 4.2
  424. *
  425. */
  426. U_STABLE int32_t U_EXPORT2
  427. uspoof_getChecks(const USpoofChecker *sc, UErrorCode *status);
  428. /**
  429. * Set the loosest restriction level allowed. The default if this function
  430. * is not called is HIGHLY_RESTRICTIVE.
  431. * Calling this function also enables the RESTRICTION_LEVEL check.
  432. * @param restrictionLevel The loosest restriction level allowed.
  433. * @see URestrictionLevel
  434. * @stable ICU 51
  435. */
  436. U_STABLE void U_EXPORT2
  437. uspoof_setRestrictionLevel(USpoofChecker *sc, URestrictionLevel restrictionLevel);
  438. /**
  439. * Get the Restriction Level that will be tested if the checks include RESTRICTION_LEVEL.
  440. *
  441. * @return The restriction level
  442. * @see URestrictionLevel
  443. * @stable ICU 51
  444. */
  445. U_STABLE URestrictionLevel U_EXPORT2
  446. uspoof_getRestrictionLevel(const USpoofChecker *sc);
  447. /**
  448. * Limit characters that are acceptable in identifiers being checked to those
  449. * normally used with the languages associated with the specified locales.
  450. * Any previously specified list of locales is replaced by the new settings.
  451. *
  452. * A set of languages is determined from the locale(s), and
  453. * from those a set of acceptable Unicode scripts is determined.
  454. * Characters from this set of scripts, along with characters from
  455. * the "common" and "inherited" Unicode Script categories
  456. * will be permitted.
  457. *
  458. * Supplying an empty string removes all restrictions;
  459. * characters from any script will be allowed.
  460. *
  461. * The USPOOF_CHAR_LIMIT test is automatically enabled for this
  462. * USpoofChecker when calling this function with a non-empty list
  463. * of locales.
  464. *
  465. * The Unicode Set of characters that will be allowed is accessible
  466. * via the uspoof_getAllowedChars() function. uspoof_setAllowedLocales()
  467. * will <i>replace</i> any previously applied set of allowed characters.
  468. *
  469. * Adjustments, such as additions or deletions of certain classes of characters,
  470. * can be made to the result of uspoof_setAllowedLocales() by
  471. * fetching the resulting set with uspoof_getAllowedChars(),
  472. * manipulating it with the Unicode Set API, then resetting the
  473. * spoof detectors limits with uspoof_setAllowedChars()
  474. *
  475. * @param sc The USpoofChecker
  476. * @param localesList A list list of locales, from which the language
  477. * and associated script are extracted. The locales
  478. * are comma-separated if there is more than one.
  479. * White space may not appear within an individual locale,
  480. * but is ignored otherwise.
  481. * The locales are syntactically like those from the
  482. * HTTP Accept-Language header.
  483. * If the localesList is empty, no restrictions will be placed on
  484. * the allowed characters.
  485. *
  486. * @param status The error code, set if this function encounters a problem.
  487. * @stable ICU 4.2
  488. */
  489. U_STABLE void U_EXPORT2
  490. uspoof_setAllowedLocales(USpoofChecker *sc, const char *localesList, UErrorCode *status);
  491. /**
  492. * Get a list of locales for the scripts that are acceptable in strings
  493. * to be checked. If no limitations on scripts have been specified,
  494. * an empty string will be returned.
  495. *
  496. * uspoof_setAllowedChars() will reset the list of allowed to be empty.
  497. *
  498. * The format of the returned list is the same as that supplied to
  499. * uspoof_setAllowedLocales(), but returned list may not be identical
  500. * to the originally specified string; the string may be reformatted,
  501. * and information other than languages from
  502. * the originally specified locales may be omitted.
  503. *
  504. * @param sc The USpoofChecker
  505. * @param status The error code, set if this function encounters a problem.
  506. * @return A string containing a list of locales corresponding
  507. * to the acceptable scripts, formatted like an
  508. * HTTP Accept Language value.
  509. *
  510. * @stable ICU 4.2
  511. */
  512. U_STABLE const char * U_EXPORT2
  513. uspoof_getAllowedLocales(USpoofChecker *sc, UErrorCode *status);
  514. /**
  515. * Limit the acceptable characters to those specified by a Unicode Set.
  516. * Any previously specified character limit is
  517. * is replaced by the new settings. This includes limits on
  518. * characters that were set with the uspoof_setAllowedLocales() function.
  519. *
  520. * The USPOOF_CHAR_LIMIT test is automatically enabled for this
  521. * USpoofChecker by this function.
  522. *
  523. * @param sc The USpoofChecker
  524. * @param chars A Unicode Set containing the list of
  525. * characters that are permitted. Ownership of the set
  526. * remains with the caller. The incoming set is cloned by
  527. * this function, so there are no restrictions on modifying
  528. * or deleting the USet after calling this function.
  529. * @param status The error code, set if this function encounters a problem.
  530. * @stable ICU 4.2
  531. */
  532. U_STABLE void U_EXPORT2
  533. uspoof_setAllowedChars(USpoofChecker *sc, const USet *chars, UErrorCode *status);
  534. /**
  535. * Get a USet for the characters permitted in an identifier.
  536. * This corresponds to the limits imposed by the Set Allowed Characters
  537. * functions. Limitations imposed by other checks will not be
  538. * reflected in the set returned by this function.
  539. *
  540. * The returned set will be frozen, meaning that it cannot be modified
  541. * by the caller.
  542. *
  543. * Ownership of the returned set remains with the Spoof Detector. The
  544. * returned set will become invalid if the spoof detector is closed,
  545. * or if a new set of allowed characters is specified.
  546. *
  547. *
  548. * @param sc The USpoofChecker
  549. * @param status The error code, set if this function encounters a problem.
  550. * @return A USet containing the characters that are permitted by
  551. * the USPOOF_CHAR_LIMIT test.
  552. * @stable ICU 4.2
  553. */
  554. U_STABLE const USet * U_EXPORT2
  555. uspoof_getAllowedChars(const USpoofChecker *sc, UErrorCode *status);
  556. #if U_SHOW_CPLUSPLUS_API
  557. /**
  558. * Limit the acceptable characters to those specified by a Unicode Set.
  559. * Any previously specified character limit is
  560. * is replaced by the new settings. This includes limits on
  561. * characters that were set with the uspoof_setAllowedLocales() function.
  562. *
  563. * The USPOOF_CHAR_LIMIT test is automatically enabled for this
  564. * USoofChecker by this function.
  565. *
  566. * @param sc The USpoofChecker
  567. * @param chars A Unicode Set containing the list of
  568. * characters that are permitted. Ownership of the set
  569. * remains with the caller. The incoming set is cloned by
  570. * this function, so there are no restrictions on modifying
  571. * or deleting the UnicodeSet after calling this function.
  572. * @param status The error code, set if this function encounters a problem.
  573. * @stable ICU 4.2
  574. */
  575. U_STABLE void U_EXPORT2
  576. uspoof_setAllowedUnicodeSet(USpoofChecker *sc, const icu::UnicodeSet *chars, UErrorCode *status);
  577. /**
  578. * Get a UnicodeSet for the characters permitted in an identifier.
  579. * This corresponds to the limits imposed by the Set Allowed Characters /
  580. * UnicodeSet functions. Limitations imposed by other checks will not be
  581. * reflected in the set returned by this function.
  582. *
  583. * The returned set will be frozen, meaning that it cannot be modified
  584. * by the caller.
  585. *
  586. * Ownership of the returned set remains with the Spoof Detector. The
  587. * returned set will become invalid if the spoof detector is closed,
  588. * or if a new set of allowed characters is specified.
  589. *
  590. *
  591. * @param sc The USpoofChecker
  592. * @param status The error code, set if this function encounters a problem.
  593. * @return A UnicodeSet containing the characters that are permitted by
  594. * the USPOOF_CHAR_LIMIT test.
  595. * @stable ICU 4.2
  596. */
  597. U_STABLE const icu::UnicodeSet * U_EXPORT2
  598. uspoof_getAllowedUnicodeSet(const USpoofChecker *sc, UErrorCode *status);
  599. #endif
  600. /**
  601. * Check the specified string for possible security issues.
  602. * The text to be checked will typically be an identifier of some sort.
  603. * The set of checks to be performed is specified with uspoof_setChecks().
  604. *
  605. * @param sc The USpoofChecker
  606. * @param id The identifier to be checked for possible security issues,
  607. * in UTF-16 format.
  608. * @param length the length of the string to be checked, expressed in
  609. * 16 bit UTF-16 code units, or -1 if the string is
  610. * zero terminated.
  611. * @param position An out parameter.
  612. * Originally, the index of the first string position that failed a check.
  613. * Now, always returns zero.
  614. * This parameter may be null.
  615. * @param status The error code, set if an error occurred while attempting to
  616. * perform the check.
  617. * Spoofing or security issues detected with the input string are
  618. * not reported here, but through the function's return value.
  619. * @return An integer value with bits set for any potential security
  620. * or spoofing issues detected. The bits are defined by
  621. * enum USpoofChecks. (returned_value & USPOOF_ALL_CHECKS)
  622. * will be zero if the input string passes all of the
  623. * enabled checks.
  624. * @stable ICU 4.2
  625. */
  626. U_STABLE int32_t U_EXPORT2
  627. uspoof_check(const USpoofChecker *sc,
  628. const UChar *id, int32_t length,
  629. int32_t *position,
  630. UErrorCode *status);
  631. /**
  632. * Check the specified string for possible security issues.
  633. * The text to be checked will typically be an identifier of some sort.
  634. * The set of checks to be performed is specified with uspoof_setChecks().
  635. *
  636. * @param sc The USpoofChecker
  637. * @param id A identifier to be checked for possible security issues, in UTF8 format.
  638. * @param length the length of the string to be checked, or -1 if the string is
  639. * zero terminated.
  640. * @param position An out parameter.
  641. * Originally, the index of the first string position that failed a check.
  642. * Now, always returns zero.
  643. * This parameter may be null.
  644. * @deprecated ICU 51
  645. * @param status The error code, set if an error occurred while attempting to
  646. * perform the check.
  647. * Spoofing or security issues detected with the input string are
  648. * not reported here, but through the function's return value.
  649. * If the input contains invalid UTF-8 sequences,
  650. * a status of U_INVALID_CHAR_FOUND will be returned.
  651. * @return An integer value with bits set for any potential security
  652. * or spoofing issues detected. The bits are defined by
  653. * enum USpoofChecks. (returned_value & USPOOF_ALL_CHECKS)
  654. * will be zero if the input string passes all of the
  655. * enabled checks.
  656. * @stable ICU 4.2
  657. */
  658. U_STABLE int32_t U_EXPORT2
  659. uspoof_checkUTF8(const USpoofChecker *sc,
  660. const char *id, int32_t length,
  661. int32_t *position,
  662. UErrorCode *status);
  663. #if U_SHOW_CPLUSPLUS_API
  664. /**
  665. * Check the specified string for possible security issues.
  666. * The text to be checked will typically be an identifier of some sort.
  667. * The set of checks to be performed is specified with uspoof_setChecks().
  668. *
  669. * @param sc The USpoofChecker
  670. * @param id A identifier to be checked for possible security issues.
  671. * @param position An out parameter.
  672. * Originally, the index of the first string position that failed a check.
  673. * Now, always returns zero.
  674. * This parameter may be null.
  675. * @deprecated ICU 51
  676. * @param status The error code, set if an error occurred while attempting to
  677. * perform the check.
  678. * Spoofing or security issues detected with the input string are
  679. * not reported here, but through the function's return value.
  680. * @return An integer value with bits set for any potential security
  681. * or spoofing issues detected. The bits are defined by
  682. * enum USpoofChecks. (returned_value & USPOOF_ALL_CHECKS)
  683. * will be zero if the input string passes all of the
  684. * enabled checks.
  685. * @stable ICU 4.2
  686. */
  687. U_STABLE int32_t U_EXPORT2
  688. uspoof_checkUnicodeString(const USpoofChecker *sc,
  689. const icu::UnicodeString &id,
  690. int32_t *position,
  691. UErrorCode *status);
  692. #endif
  693. /**
  694. * Check the whether two specified strings are visually confusable.
  695. * The types of confusability to be tested - single script, mixed script,
  696. * or whole script - are determined by the check options set for the
  697. * USpoofChecker.
  698. *
  699. * The tests to be performed are controlled by the flags
  700. * USPOOF_SINGLE_SCRIPT_CONFUSABLE
  701. * USPOOF_MIXED_SCRIPT_CONFUSABLE
  702. * USPOOF_WHOLE_SCRIPT_CONFUSABLE
  703. * At least one of these tests must be selected.
  704. *
  705. * USPOOF_ANY_CASE is a modifier for the tests. Select it if the identifiers
  706. * may be of mixed case.
  707. * If identifiers are case folded for comparison and
  708. * display to the user, do not select the USPOOF_ANY_CASE option.
  709. *
  710. *
  711. * @param sc The USpoofChecker
  712. * @param id1 The first of the two identifiers to be compared for
  713. * confusability. The strings are in UTF-16 format.
  714. * @param length1 the length of the first identifer, expressed in
  715. * 16 bit UTF-16 code units, or -1 if the string is
  716. * nul terminated.
  717. * @param id2 The second of the two identifiers to be compared for
  718. * confusability. The identifiers are in UTF-16 format.
  719. * @param length2 The length of the second identifiers, expressed in
  720. * 16 bit UTF-16 code units, or -1 if the string is
  721. * nul terminated.
  722. * @param status The error code, set if an error occurred while attempting to
  723. * perform the check.
  724. * Confusability of the identifiers is not reported here,
  725. * but through this function's return value.
  726. * @return An integer value with bit(s) set corresponding to
  727. * the type of confusability found, as defined by
  728. * enum USpoofChecks. Zero is returned if the identifiers
  729. * are not confusable.
  730. * @stable ICU 4.2
  731. */
  732. U_STABLE int32_t U_EXPORT2
  733. uspoof_areConfusable(const USpoofChecker *sc,
  734. const UChar *id1, int32_t length1,
  735. const UChar *id2, int32_t length2,
  736. UErrorCode *status);
  737. /**
  738. * Check the whether two specified strings are visually confusable.
  739. * The types of confusability to be tested - single script, mixed script,
  740. * or whole script - are determined by the check options set for the
  741. * USpoofChecker.
  742. *
  743. * @param sc The USpoofChecker
  744. * @param id1 The first of the two identifiers to be compared for
  745. * confusability. The strings are in UTF-8 format.
  746. * @param length1 the length of the first identifiers, in bytes, or -1
  747. * if the string is nul terminated.
  748. * @param id2 The second of the two identifiers to be compared for
  749. * confusability. The strings are in UTF-8 format.
  750. * @param length2 The length of the second string in bytes, or -1
  751. * if the string is nul terminated.
  752. * @param status The error code, set if an error occurred while attempting to
  753. * perform the check.
  754. * Confusability of the strings is not reported here,
  755. * but through this function's return value.
  756. * @return An integer value with bit(s) set corresponding to
  757. * the type of confusability found, as defined by
  758. * enum USpoofChecks. Zero is returned if the strings
  759. * are not confusable.
  760. * @stable ICU 4.2
  761. */
  762. U_STABLE int32_t U_EXPORT2
  763. uspoof_areConfusableUTF8(const USpoofChecker *sc,
  764. const char *id1, int32_t length1,
  765. const char *id2, int32_t length2,
  766. UErrorCode *status);
  767. #if U_SHOW_CPLUSPLUS_API
  768. /**
  769. * Check the whether two specified strings are visually confusable.
  770. * The types of confusability to be tested - single script, mixed script,
  771. * or whole script - are determined by the check options set for the
  772. * USpoofChecker.
  773. *
  774. * @param sc The USpoofChecker
  775. * @param s1 The first of the two identifiers to be compared for
  776. * confusability. The strings are in UTF-8 format.
  777. * @param s2 The second of the two identifiers to be compared for
  778. * confusability. The strings are in UTF-8 format.
  779. * @param status The error code, set if an error occurred while attempting to
  780. * perform the check.
  781. * Confusability of the identifiers is not reported here,
  782. * but through this function's return value.
  783. * @return An integer value with bit(s) set corresponding to
  784. * the type of confusability found, as defined by
  785. * enum USpoofChecks. Zero is returned if the identifiers
  786. * are not confusable.
  787. * @stable ICU 4.2
  788. */
  789. U_STABLE int32_t U_EXPORT2
  790. uspoof_areConfusableUnicodeString(const USpoofChecker *sc,
  791. const icu::UnicodeString &s1,
  792. const icu::UnicodeString &s2,
  793. UErrorCode *status);
  794. #endif
  795. /**
  796. * Get the "skeleton" for an identifier.
  797. * Skeletons are a transformation of the input identifier;
  798. * Two identifiers are confusable if their skeletons are identical.
  799. * See Unicode UAX #39 for additional information.
  800. *
  801. * Using skeletons directly makes it possible to quickly check
  802. * whether an identifier is confusable with any of some large
  803. * set of existing identifiers, by creating an efficiently
  804. * searchable collection of the skeletons.
  805. *
  806. * @param sc The USpoofChecker
  807. * @param type The type of skeleton, corresponding to which
  808. * of the Unicode confusable data tables to use.
  809. * The default is Mixed-Script, Lowercase.
  810. * Allowed options are USPOOF_SINGLE_SCRIPT_CONFUSABLE and
  811. * USPOOF_ANY_CASE. The two flags may be ORed.
  812. * @param id The input identifier whose skeleton will be computed.
  813. * @param length The length of the input identifier, expressed in 16 bit
  814. * UTF-16 code units, or -1 if the string is zero terminated.
  815. * @param dest The output buffer, to receive the skeleton string.
  816. * @param destCapacity The length of the output buffer, in 16 bit units.
  817. * The destCapacity may be zero, in which case the function will
  818. * return the actual length of the skeleton.
  819. * @param status The error code, set if an error occurred while attempting to
  820. * perform the check.
  821. * @return The length of the skeleton string. The returned length
  822. * is always that of the complete skeleton, even when the
  823. * supplied buffer is too small (or of zero length)
  824. *
  825. * @stable ICU 4.2
  826. */
  827. U_STABLE int32_t U_EXPORT2
  828. uspoof_getSkeleton(const USpoofChecker *sc,
  829. uint32_t type,
  830. const UChar *id, int32_t length,
  831. UChar *dest, int32_t destCapacity,
  832. UErrorCode *status);
  833. /**
  834. * Get the "skeleton" for an identifier.
  835. * Skeletons are a transformation of the input identifier;
  836. * Two identifiers are confusable if their skeletons are identical.
  837. * See Unicode UAX #39 for additional information.
  838. *
  839. * Using skeletons directly makes it possible to quickly check
  840. * whether an identifier is confusable with any of some large
  841. * set of existing identifiers, by creating an efficiently
  842. * searchable collection of the skeletons.
  843. *
  844. * @param sc The USpoofChecker
  845. * @param type The type of skeleton, corresponding to which
  846. * of the Unicode confusable data tables to use.
  847. * The default is Mixed-Script, Lowercase.
  848. * Allowed options are USPOOF_SINGLE_SCRIPT_CONFUSABLE and
  849. * USPOOF_ANY_CASE. The two flags may be ORed.
  850. * @param id The UTF-8 format identifier whose skeleton will be computed.
  851. * @param length The length of the input string, in bytes,
  852. * or -1 if the string is zero terminated.
  853. * @param dest The output buffer, to receive the skeleton string.
  854. * @param destCapacity The length of the output buffer, in bytes.
  855. * The destCapacity may be zero, in which case the function will
  856. * return the actual length of the skeleton.
  857. * @param status The error code, set if an error occurred while attempting to
  858. * perform the check. Possible Errors include U_INVALID_CHAR_FOUND
  859. * for invalid UTF-8 sequences, and
  860. * U_BUFFER_OVERFLOW_ERROR if the destination buffer is too small
  861. * to hold the complete skeleton.
  862. * @return The length of the skeleton string, in bytes. The returned length
  863. * is always that of the complete skeleton, even when the
  864. * supplied buffer is too small (or of zero length)
  865. *
  866. * @stable ICU 4.2
  867. */
  868. U_STABLE int32_t U_EXPORT2
  869. uspoof_getSkeletonUTF8(const USpoofChecker *sc,
  870. uint32_t type,
  871. const char *id, int32_t length,
  872. char *dest, int32_t destCapacity,
  873. UErrorCode *status);
  874. #if U_SHOW_CPLUSPLUS_API
  875. /**
  876. * Get the "skeleton" for an identifier.
  877. * Skeletons are a transformation of the input identifier;
  878. * Two identifiers are confusable if their skeletons are identical.
  879. * See Unicode UAX #39 for additional information.
  880. *
  881. * Using skeletons directly makes it possible to quickly check
  882. * whether an identifier is confusable with any of some large
  883. * set of existing identifiers, by creating an efficiently
  884. * searchable collection of the skeletons.
  885. *
  886. * @param sc The USpoofChecker.
  887. * @param type The type of skeleton, corresponding to which
  888. * of the Unicode confusable data tables to use.
  889. * The default is Mixed-Script, Lowercase.
  890. * Allowed options are USPOOF_SINGLE_SCRIPT_CONFUSABLE and
  891. * USPOOF_ANY_CASE. The two flags may be ORed.
  892. * @param id The input identifier whose skeleton will be computed.
  893. * @param dest The output identifier, to receive the skeleton string.
  894. * @param status The error code, set if an error occurred while attempting to
  895. * perform the check.
  896. * @return A reference to the destination (skeleton) string.
  897. *
  898. * @stable ICU 4.2
  899. */
  900. U_I18N_API icu::UnicodeString & U_EXPORT2
  901. uspoof_getSkeletonUnicodeString(const USpoofChecker *sc,
  902. uint32_t type,
  903. const icu::UnicodeString &id,
  904. icu::UnicodeString &dest,
  905. UErrorCode *status);
  906. #endif /* U_SHOW_CPLUSPLUS_API */
  907. /**
  908. * Get the set of Candidate Characters for Inclusion in Identifiers, as defined
  909. * in Unicode UAX #31, http://www.unicode.org/reports/tr31/#Table_Candidate_Characters_for_Inclusion_in_Identifiers
  910. *
  911. * The returned set is frozen. Ownership of the set remains with the ICU library; it must not
  912. * be deleted by the caller.
  913. *
  914. * @param status The error code, set if a problem occurs while creating the set.
  915. *
  916. * @stable ICU 51
  917. */
  918. U_STABLE const USet * U_EXPORT2
  919. uspoof_getInclusionSet(UErrorCode *status);
  920. /**
  921. * Get the set of characters from Recommended Scripts for Inclusion in Identifiers, as defined
  922. * in Unicode UAX #31, http://www.unicode.org/reports/tr31/#Table_Recommended_Scripts
  923. *
  924. * The returned set is frozen. Ownership of the set remains with the ICU library; it must not
  925. * be deleted by the caller.
  926. *
  927. * @param status The error code, set if a problem occurs while creating the set.
  928. *
  929. * @stable ICU 51
  930. */
  931. U_STABLE const USet * U_EXPORT2
  932. uspoof_getRecommendedSet(UErrorCode *status);
  933. #if U_SHOW_CPLUSPLUS_API
  934. /**
  935. * Get the set of Candidate Characters for Inclusion in Identifiers, as defined
  936. * in Unicode UAX #31, http://www.unicode.org/reports/tr31/#Table_Candidate_Characters_for_Inclusion_in_Identifiers
  937. *
  938. * The returned set is frozen. Ownership of the set remains with the ICU library; it must not
  939. * be deleted by the caller.
  940. *
  941. * @param status The error code, set if a problem occurs while creating the set.
  942. *
  943. * @stable ICU 51
  944. */
  945. U_STABLE const icu::UnicodeSet * U_EXPORT2
  946. uspoof_getInclusionUnicodeSet(UErrorCode *status);
  947. /**
  948. * Get the set of characters from Recommended Scripts for Inclusion in Identifiers, as defined
  949. * in Unicode UAX #31, http://www.unicode.org/reports/tr31/#Table_Recommended_Scripts
  950. *
  951. * The returned set is frozen. Ownership of the set remains with the ICU library; it must not
  952. * be deleted by the caller.
  953. *
  954. * @param status The error code, set if a problem occurs while creating the set.
  955. *
  956. * @stable ICU 51
  957. */
  958. U_STABLE const icu::UnicodeSet * U_EXPORT2
  959. uspoof_getRecommendedUnicodeSet(UErrorCode *status);
  960. #endif /* U_SHOW_CPLUSPLUS_API */
  961. /**
  962. * Serialize the data for a spoof detector into a chunk of memory.
  963. * The flattened spoof detection tables can later be used to efficiently
  964. * instantiate a new Spoof Detector.
  965. *
  966. * The serialized spoof checker includes only the data compiled from the
  967. * Unicode data tables by uspoof_openFromSource(); it does not include
  968. * include any other state or configuration that may have been set.
  969. *
  970. * @param sc the Spoof Detector whose data is to be serialized.
  971. * @param data a pointer to 32-bit-aligned memory to be filled with the data,
  972. * can be NULL if capacity==0
  973. * @param capacity the number of bytes available at data,
  974. * or 0 for preflighting
  975. * @param status an in/out ICU UErrorCode; possible errors include:
  976. * - U_BUFFER_OVERFLOW_ERROR if the data storage block is too small for serialization
  977. * - U_ILLEGAL_ARGUMENT_ERROR the data or capacity parameters are bad
  978. * @return the number of bytes written or needed for the spoof data
  979. *
  980. * @see utrie2_openFromSerialized()
  981. * @stable ICU 4.2
  982. */
  983. U_STABLE int32_t U_EXPORT2
  984. uspoof_serialize(USpoofChecker *sc,
  985. void *data, int32_t capacity,
  986. UErrorCode *status);
  987. #endif
  988. #endif /* USPOOF_H */