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