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