1 /* 2 *************************************************************************** 3 * Copyright (C) 2008-2012, 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 144 struct USpoofChecker; 145 typedef 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 */ 154 typedef 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 /** Check that an identifier contains only characters from a 192 * single script (plus chars from the common and inherited scripts.) 193 * Applies to checks of a single identifier check only. 194 */ 195 USPOOF_SINGLE_SCRIPT = 16, 196 197 /** Check an identifier for the presence of invisible characters, 198 * such as zero-width spaces, or character sequences that are 199 * likely not to display, such as multiple occurrences of the same 200 * non-spacing mark. This check does not test the input string as a whole 201 * for conformance to any particular syntax for identifiers. 202 */ 203 USPOOF_INVISIBLE = 32, 204 205 /** Check that an identifier contains only characters from a specified set 206 * of acceptable characters. See uspoof_setAllowedChars() and 207 * uspoof_setAllowedLocales(). 208 */ 209 USPOOF_CHAR_LIMIT = 64, 210 211 USPOOF_ALL_CHECKS = 0x7f 212 } USpoofChecks; 213 214 215 /** 216 * Create a Unicode Spoof Checker, configured to perform all 217 * checks except for USPOOF_LOCALE_LIMIT and USPOOF_CHAR_LIMIT. 218 * Note that additional checks may be added in the future, 219 * resulting in the changes to the default checking behavior. 220 * 221 * @param status The error code, set if this function encounters a problem. 222 * @return the newly created Spoof Checker 223 * @stable ICU 4.2 224 */ 225 U_STABLE USpoofChecker * U_EXPORT2 226 uspoof_open(UErrorCode *status); 227 228 229 /** 230 * Open a Spoof checker from its serialized from, stored in 32-bit-aligned memory. 231 * Inverse of uspoof_serialize(). 232 * The memory containing the serialized data must remain valid and unchanged 233 * as long as the spoof checker, or any cloned copies of the spoof checker, 234 * are in use. Ownership of the memory remains with the caller. 235 * The spoof checker (and any clones) must be closed prior to deleting the 236 * serialized data. 237 * 238 * @param data a pointer to 32-bit-aligned memory containing the serialized form of spoof data 239 * @param length the number of bytes available at data; 240 * can be more than necessary 241 * @param pActualLength receives the actual number of bytes at data taken up by the data; 242 * can be NULL 243 * @param pErrorCode ICU error code 244 * @return the spoof checker. 245 * 246 * @see uspoof_open 247 * @see uspoof_serialize 248 * @stable ICU 4.2 249 */ 250 U_STABLE USpoofChecker * U_EXPORT2 251 uspoof_openFromSerialized(const void *data, int32_t length, int32_t *pActualLength, 252 UErrorCode *pErrorCode); 253 254 /** 255 * Open a Spoof Checker from the source form of the spoof data. 256 * The Three inputs correspond to the Unicode data files confusables.txt 257 * confusablesWholeScript.txt and xidmdifications.txt as described in 258 * Unicode UAX 39. The syntax of the source data is as described in UAX 39 for 259 * these files, and the content of these files is acceptable input. 260 * 261 * The character encoding of the (char *) input text is UTF-8. 262 * 263 * @param confusables a pointer to the confusable characters definitions, 264 * as found in file confusables.txt from unicode.org. 265 * @param confusablesLen The length of the confusables text, or -1 if the 266 * input string is zero terminated. 267 * @param confusablesWholeScript 268 * a pointer to the whole script confusables definitions, 269 * as found in the file confusablesWholeScript.txt from unicode.org. 270 * @param confusablesWholeScriptLen The length of the whole script confusables text, or 271 * -1 if the input string is zero terminated. 272 * @param errType In the event of an error in the input, indicates 273 * which of the input files contains the error. 274 * The value is one of USPOOF_SINGLE_SCRIPT_CONFUSABLE or 275 * USPOOF_WHOLE_SCRIPT_CONFUSABLE, or 276 * zero if no errors are found. 277 * @param pe In the event of an error in the input, receives the position 278 * in the input text (line, offset) of the error. 279 * @param status an in/out ICU UErrorCode. Among the possible errors is 280 * U_PARSE_ERROR, which is used to report syntax errors 281 * in the input. 282 * @return A spoof checker that uses the rules from the input files. 283 * @stable ICU 4.2 284 */ 285 U_STABLE USpoofChecker * U_EXPORT2 286 uspoof_openFromSource(const char *confusables, int32_t confusablesLen, 287 const char *confusablesWholeScript, int32_t confusablesWholeScriptLen, 288 int32_t *errType, UParseError *pe, UErrorCode *status); 289 290 291 /** 292 * Close a Spoof Checker, freeing any memory that was being held by 293 * its implementation. 294 * @stable ICU 4.2 295 */ 296 U_STABLE void U_EXPORT2 297 uspoof_close(USpoofChecker *sc); 298 299 #if U_SHOW_CPLUSPLUS_API 300 301 U_NAMESPACE_BEGIN 302 303 /** 304 * \class LocalUSpoofCheckerPointer 305 * "Smart pointer" class, closes a USpoofChecker via uspoof_close(). 306 * For most methods see the LocalPointerBase base class. 307 * 308 * @see LocalPointerBase 309 * @see LocalPointer 310 * @stable ICU 4.4 311 */ 312 U_DEFINE_LOCAL_OPEN_POINTER(LocalUSpoofCheckerPointer, USpoofChecker, uspoof_close); 313 314 U_NAMESPACE_END 315 316 #endif 317 318 /** 319 * Clone a Spoof Checker. The clone will be set to perform the same checks 320 * as the original source. 321 * 322 * @param sc The source USpoofChecker 323 * @param status The error code, set if this function encounters a problem. 324 * @return 325 * @stable ICU 4.2 326 */ 327 U_STABLE USpoofChecker * U_EXPORT2 328 uspoof_clone(const USpoofChecker *sc, UErrorCode *status); 329 330 331 /** 332 * Specify the set of checks that will be performed by the check 333 * functions of this Spoof Checker. 334 * 335 * @param sc The USpoofChecker 336 * @param checks The set of checks that this spoof checker will perform. 337 * The value is a bit set, obtained by OR-ing together 338 * values from enum USpoofChecks. 339 * @param status The error code, set if this function encounters a problem. 340 * @stable ICU 4.2 341 * 342 */ 343 U_STABLE void U_EXPORT2 344 uspoof_setChecks(USpoofChecker *sc, int32_t checks, UErrorCode *status); 345 346 /** 347 * Get the set of checks that this Spoof Checker has been configured to perform. 348 * 349 * @param sc The USpoofChecker 350 * @param status The error code, set if this function encounters a problem. 351 * @return The set of checks that this spoof checker will perform. 352 * The value is a bit set, obtained by OR-ing together 353 * values from enum USpoofChecks. 354 * @stable ICU 4.2 355 * 356 */ 357 U_STABLE int32_t U_EXPORT2 358 uspoof_getChecks(const USpoofChecker *sc, UErrorCode *status); 359 360 /** 361 * Limit characters that are acceptable in identifiers being checked to those 362 * normally used with the languages associated with the specified locales. 363 * Any previously specified list of locales is replaced by the new settings. 364 * 365 * A set of languages is determined from the locale(s), and 366 * from those a set of acceptable Unicode scripts is determined. 367 * Characters from this set of scripts, along with characters from 368 * the "common" and "inherited" Unicode Script categories 369 * will be permitted. 370 * 371 * Supplying an empty string removes all restrictions; 372 * characters from any script will be allowed. 373 * 374 * The USPOOF_CHAR_LIMIT test is automatically enabled for this 375 * USpoofChecker when calling this function with a non-empty list 376 * of locales. 377 * 378 * The Unicode Set of characters that will be allowed is accessible 379 * via the uspoof_getAllowedChars() function. uspoof_setAllowedLocales() 380 * will <i>replace</i> any previously applied set of allowed characters. 381 * 382 * Adjustments, such as additions or deletions of certain classes of characters, 383 * can be made to the result of uspoof_setAllowedLocales() by 384 * fetching the resulting set with uspoof_getAllowedChars(), 385 * manipulating it with the Unicode Set API, then resetting the 386 * spoof detectors limits with uspoof_setAllowedChars() 387 * 388 * @param sc The USpoofChecker 389 * @param localesList A list list of locales, from which the language 390 * and associated script are extracted. The locales 391 * are comma-separated if there is more than one. 392 * White space may not appear within an individual locale, 393 * but is ignored otherwise. 394 * The locales are syntactically like those from the 395 * HTTP Accept-Language header. 396 * If the localesList is empty, no restrictions will be placed on 397 * the allowed characters. 398 * 399 * @param status The error code, set if this function encounters a problem. 400 * @stable ICU 4.2 401 */ 402 U_STABLE void U_EXPORT2 403 uspoof_setAllowedLocales(USpoofChecker *sc, const char *localesList, UErrorCode *status); 404 405 /** 406 * Get a list of locales for the scripts that are acceptable in strings 407 * to be checked. If no limitations on scripts have been specified, 408 * an empty string will be returned. 409 * 410 * uspoof_setAllowedChars() will reset the list of allowed to be empty. 411 * 412 * The format of the returned list is the same as that supplied to 413 * uspoof_setAllowedLocales(), but returned list may not be identical 414 * to the originally specified string; the string may be reformatted, 415 * and information other than languages from 416 * the originally specified locales may be omitted. 417 * 418 * @param sc The USpoofChecker 419 * @param status The error code, set if this function encounters a problem. 420 * @return A string containing a list of locales corresponding 421 * to the acceptable scripts, formatted like an 422 * HTTP Accept Language value. 423 * 424 * @stable ICU 4.2 425 */ 426 U_STABLE const char * U_EXPORT2 427 uspoof_getAllowedLocales(USpoofChecker *sc, UErrorCode *status); 428 429 430 /** 431 * Limit the acceptable characters to those specified by a Unicode Set. 432 * Any previously specified character limit is 433 * is replaced by the new settings. This includes limits on 434 * characters that were set with the uspoof_setAllowedLocales() function. 435 * 436 * The USPOOF_CHAR_LIMIT test is automatically enabled for this 437 * USpoofChecker by this function. 438 * 439 * @param sc The USpoofChecker 440 * @param chars A Unicode Set containing the list of 441 * characters that are permitted. Ownership of the set 442 * remains with the caller. The incoming set is cloned by 443 * this function, so there are no restrictions on modifying 444 * or deleting the USet after calling this function. 445 * @param status The error code, set if this function encounters a problem. 446 * @stable ICU 4.2 447 */ 448 U_STABLE void U_EXPORT2 449 uspoof_setAllowedChars(USpoofChecker *sc, const USet *chars, UErrorCode *status); 450 451 452 /** 453 * Get a USet for the characters permitted in an identifier. 454 * This corresponds to the limits imposed by the Set Allowed Characters 455 * functions. Limitations imposed by other checks will not be 456 * reflected in the set returned by this function. 457 * 458 * The returned set will be frozen, meaning that it cannot be modified 459 * by the caller. 460 * 461 * Ownership of the returned set remains with the Spoof Detector. The 462 * returned set will become invalid if the spoof detector is closed, 463 * or if a new set of allowed characters is specified. 464 * 465 * 466 * @param sc The USpoofChecker 467 * @param status The error code, set if this function encounters a problem. 468 * @return A USet containing the characters that are permitted by 469 * the USPOOF_CHAR_LIMIT test. 470 * @stable ICU 4.2 471 */ 472 U_STABLE const USet * U_EXPORT2 473 uspoof_getAllowedChars(const USpoofChecker *sc, UErrorCode *status); 474 475 476 #if U_SHOW_CPLUSPLUS_API 477 /** 478 * Limit the acceptable characters to those specified by a Unicode Set. 479 * Any previously specified character limit is 480 * is replaced by the new settings. This includes limits on 481 * characters that were set with the uspoof_setAllowedLocales() function. 482 * 483 * The USPOOF_CHAR_LIMIT test is automatically enabled for this 484 * USoofChecker by this function. 485 * 486 * @param sc The USpoofChecker 487 * @param chars A Unicode Set containing the list of 488 * characters that are permitted. Ownership of the set 489 * remains with the caller. The incoming set is cloned by 490 * this function, so there are no restrictions on modifying 491 * or deleting the USet after calling this function. 492 * @param status The error code, set if this function encounters a problem. 493 * @stable ICU 4.2 494 */ 495 U_STABLE void U_EXPORT2 496 uspoof_setAllowedUnicodeSet(USpoofChecker *sc, const icu::UnicodeSet *chars, UErrorCode *status); 497 498 499 /** 500 * Get a UnicodeSet for the characters permitted in an identifier. 501 * This corresponds to the limits imposed by the Set Allowed Characters / 502 * UnicodeSet functions. Limitations imposed by other checks will not be 503 * reflected in the set returned by this function. 504 * 505 * The returned set will be frozen, meaning that it cannot be modified 506 * by the caller. 507 * 508 * Ownership of the returned set remains with the Spoof Detector. The 509 * returned set will become invalid if the spoof detector is closed, 510 * or if a new set of allowed characters is specified. 511 * 512 * 513 * @param sc The USpoofChecker 514 * @param status The error code, set if this function encounters a problem. 515 * @return A UnicodeSet containing the characters that are permitted by 516 * the USPOOF_CHAR_LIMIT test. 517 * @stable ICU 4.2 518 */ 519 U_STABLE const icu::UnicodeSet * U_EXPORT2 520 uspoof_getAllowedUnicodeSet(const USpoofChecker *sc, UErrorCode *status); 521 #endif 522 523 524 /** 525 * Check the specified string for possible security issues. 526 * The text to be checked will typically be an identifier of some sort. 527 * The set of checks to be performed is specified with uspoof_setChecks(). 528 * 529 * @param sc The USpoofChecker 530 * @param text The string to be checked for possible security issues, 531 * in UTF-16 format. 532 * @param length the length of the string to be checked, expressed in 533 * 16 bit UTF-16 code units, or -1 if the string is 534 * zero terminated. 535 * @param position An out parameter that receives the index of the 536 * first string position that fails the allowed character 537 * limitation checks. 538 * This parameter may be null if the position information 539 * is not needed. 540 * If the string passes the requested checks the 541 * parameter value will not be set. 542 * @param status The error code, set if an error occurred while attempting to 543 * perform the check. 544 * Spoofing or security issues detected with the input string are 545 * not reported here, but through the function's return value. 546 * @return An integer value with bits set for any potential security 547 * or spoofing issues detected. The bits are defined by 548 * enum USpoofChecks. Zero is returned if no issues 549 * are found with the input string. 550 * @stable ICU 4.2 551 */ 552 U_STABLE int32_t U_EXPORT2 553 uspoof_check(const USpoofChecker *sc, 554 const UChar *text, int32_t length, 555 int32_t *position, 556 UErrorCode *status); 557 558 559 /** 560 * Check the specified string for possible security issues. 561 * The text to be checked will typically be an identifier of some sort. 562 * The set of checks to be performed is specified with uspoof_setChecks(). 563 * 564 * @param sc The USpoofChecker 565 * @param text A UTF-8 string to be checked for possible security issues. 566 * @param length the length of the string to be checked, or -1 if the string is 567 * zero terminated. 568 * @param position An out parameter that receives the index of the 569 * first string position that fails the allowed character 570 * limitation checks. 571 * This parameter may be null if the position information 572 * is not needed. 573 * If the string passes the requested checks the 574 * parameter value will not be set. 575 * @param status The error code, set if an error occurred while attempting to 576 * perform the check. 577 * Spoofing or security issues detected with the input string are 578 * not reported here, but through the function's return value. 579 * If the input contains invalid UTF-8 sequences, 580 * a status of U_INVALID_CHAR_FOUND will be returned. 581 * @return An integer value with bits set for any potential security 582 * or spoofing issues detected. The bits are defined by 583 * enum USpoofChecks. Zero is returned if no issues 584 * are found with the input string. 585 * @stable ICU 4.2 586 */ 587 U_STABLE int32_t U_EXPORT2 588 uspoof_checkUTF8(const USpoofChecker *sc, 589 const char *text, int32_t length, 590 int32_t *position, 591 UErrorCode *status); 592 593 594 #if U_SHOW_CPLUSPLUS_API 595 /** 596 * Check the specified string for possible security issues. 597 * The text to be checked will typically be an identifier of some sort. 598 * The set of checks to be performed is specified with uspoof_setChecks(). 599 * 600 * @param sc The USpoofChecker 601 * @param text A UnicodeString to be checked for possible security issues. 602 * @param position An out parameter that receives the index of the 603 * first string position that fails the allowed character 604 * limitation checks. 605 * This parameter may be null if the position information 606 * is not needed. 607 * If the string passes the requested checks the 608 * parameter value will not be set. 609 * @param status The error code, set if an error occurred while attempting to 610 * perform the check. 611 * Spoofing or security issues detected with the input string are 612 * not reported here, but through the function's return value. 613 614 * @return An integer value with bits set for any potential security 615 * or spoofing issues detected. The bits are defined by 616 * enum USpoofChecks. Zero is returned if no issues 617 * are found with the input string. 618 * @stable ICU 4.2 619 */ 620 U_STABLE int32_t U_EXPORT2 621 uspoof_checkUnicodeString(const USpoofChecker *sc, 622 const icu::UnicodeString &text, 623 int32_t *position, 624 UErrorCode *status); 625 626 #endif 627 628 629 /** 630 * Check the whether two specified strings are visually confusable. 631 * The types of confusability to be tested - single script, mixed script, 632 * or whole script - are determined by the check options set for the 633 * USpoofChecker. 634 * 635 * The tests to be performed are controlled by the flags 636 * USPOOF_SINGLE_SCRIPT_CONFUSABLE 637 * USPOOF_MIXED_SCRIPT_CONFUSABLE 638 * USPOOF_WHOLE_SCRIPT_CONFUSABLE 639 * At least one of these tests must be selected. 640 * 641 * USPOOF_ANY_CASE is a modifier for the tests. Select it if the identifiers 642 * may be of mixed case. 643 * If identifiers are case folded for comparison and 644 * display to the user, do not select the USPOOF_ANY_CASE option. 645 * 646 * 647 * @param sc The USpoofChecker 648 * @param s1 The first of the two strings to be compared for 649 * confusability. The strings are in UTF-16 format. 650 * @param length1 the length of the first string, expressed in 651 * 16 bit UTF-16 code units, or -1 if the string is 652 * zero terminated. 653 * @param s2 The second of the two strings to be compared for 654 * confusability. The strings are in UTF-16 format. 655 * @param length2 The length of the second string, expressed in 656 * 16 bit UTF-16 code units, or -1 if the string is 657 * zero terminated. 658 * @param status The error code, set if an error occurred while attempting to 659 * perform the check. 660 * Confusability of the strings is not reported here, 661 * but through this function's return value. 662 * @return An integer value with bit(s) set corresponding to 663 * the type of confusability found, as defined by 664 * enum USpoofChecks. Zero is returned if the strings 665 * are not confusable. 666 * @stable ICU 4.2 667 */ 668 U_STABLE int32_t U_EXPORT2 669 uspoof_areConfusable(const USpoofChecker *sc, 670 const UChar *s1, int32_t length1, 671 const UChar *s2, int32_t length2, 672 UErrorCode *status); 673 674 675 676 /** 677 * Check the whether two specified strings are visually confusable. 678 * The types of confusability to be tested - single script, mixed script, 679 * or whole script - are determined by the check options set for the 680 * USpoofChecker. 681 * 682 * @param sc The USpoofChecker 683 * @param s1 The first of the two strings to be compared for 684 * confusability. The strings are in UTF-8 format. 685 * @param length1 the length of the first string, in bytes, or -1 686 * if the string is zero terminated. 687 * @param s2 The second of the two strings to be compared for 688 * confusability. The strings are in UTF-18 format. 689 * @param length2 The length of the second string in bytes, or -1 690 * if the string is zero terminated. 691 * @param status The error code, set if an error occurred while attempting to 692 * perform the check. 693 * Confusability of the strings is not reported here, 694 * but through this function's return value. 695 * @return An integer value with bit(s) set corresponding to 696 * the type of confusability found, as defined by 697 * enum USpoofChecks. Zero is returned if the strings 698 * are not confusable. 699 * @stable ICU 4.2 700 */ 701 U_STABLE int32_t U_EXPORT2 702 uspoof_areConfusableUTF8(const USpoofChecker *sc, 703 const char *s1, int32_t length1, 704 const char *s2, int32_t length2, 705 UErrorCode *status); 706 707 708 709 710 #if U_SHOW_CPLUSPLUS_API 711 /** 712 * Check the whether two specified strings are visually confusable. 713 * The types of confusability to be tested - single script, mixed script, 714 * or whole script - are determined by the check options set for the 715 * USpoofChecker. 716 * 717 * @param sc The USpoofChecker 718 * @param s1 The first of the two strings to be compared for 719 * confusability. The strings are in UTF-8 format. 720 * @param s2 The second of the two strings to be compared for 721 * confusability. The strings are in UTF-18 format. 722 * @param status The error code, set if an error occurred while attempting to 723 * perform the check. 724 * Confusability of the strings is not reported here, 725 * but through this function's return value. 726 * @return An integer value with bit(s) set corresponding to 727 * the type of confusability found, as defined by 728 * enum USpoofChecks. Zero is returned if the strings 729 * are not confusable. 730 * @stable ICU 4.2 731 */ 732 U_STABLE int32_t U_EXPORT2 733 uspoof_areConfusableUnicodeString(const USpoofChecker *sc, 734 const icu::UnicodeString &s1, 735 const icu::UnicodeString &s2, 736 UErrorCode *status); 737 #endif 738 739 740 /** 741 * Get the "skeleton" for an identifier string. 742 * Skeletons are a transformation of the input string; 743 * Two strings are confusable if their skeletons are identical. 744 * See Unicode UAX 39 for additional information. 745 * 746 * Using skeletons directly makes it possible to quickly check 747 * whether an identifier is confusable with any of some large 748 * set of existing identifiers, by creating an efficiently 749 * searchable collection of the skeletons. 750 * 751 * @param sc The USpoofChecker 752 * @param type The type of skeleton, corresponding to which 753 * of the Unicode confusable data tables to use. 754 * The default is Mixed-Script, Lowercase. 755 * Allowed options are USPOOF_SINGLE_SCRIPT_CONFUSABLE and 756 * USPOOF_ANY_CASE_CONFUSABLE. The two flags may be ORed. 757 * @param s The input string whose skeleton will be computed. 758 * @param length The length of the input string, expressed in 16 bit 759 * UTF-16 code units, or -1 if the string is zero terminated. 760 * @param dest The output buffer, to receive the skeleton string. 761 * @param destCapacity The length of the output buffer, in 16 bit units. 762 * The destCapacity may be zero, in which case the function will 763 * return the actual length of the skeleton. 764 * @param status The error code, set if an error occurred while attempting to 765 * perform the check. 766 * @return The length of the skeleton string. The returned length 767 * is always that of the complete skeleton, even when the 768 * supplied buffer is too small (or of zero length) 769 * 770 * @stable ICU 4.2 771 */ 772 U_STABLE int32_t U_EXPORT2 773 uspoof_getSkeleton(const USpoofChecker *sc, 774 uint32_t type, 775 const UChar *s, int32_t length, 776 UChar *dest, int32_t destCapacity, 777 UErrorCode *status); 778 779 /** 780 * Get the "skeleton" for an identifier string. 781 * Skeletons are a transformation of the input string; 782 * Two strings are confusable if their skeletons are identical. 783 * See Unicode UAX 39 for additional information. 784 * 785 * Using skeletons directly makes it possible to quickly check 786 * whether an identifier is confusable with any of some large 787 * set of existing identifiers, by creating an efficiently 788 * searchable collection of the skeletons. 789 * 790 * @param sc The USpoofChecker 791 * @param type The type of skeleton, corresponding to which 792 * of the Unicode confusable data tables to use. 793 * The default is Mixed-Script, Lowercase. 794 * Allowed options are USPOOF_SINGLE_SCRIPT_CONFUSABLE and 795 * USPOOF_ANY_CASE. The two flags may be ORed. 796 * @param s The UTF-8 format input string whose skeleton will be computed. 797 * @param length The length of the input string, in bytes, 798 * or -1 if the string is zero terminated. 799 * @param dest The output buffer, to receive the skeleton string. 800 * @param destCapacity The length of the output buffer, in bytes. 801 * The destCapacity may be zero, in which case the function will 802 * return the actual length of the skeleton. 803 * @param status The error code, set if an error occurred while attempting to 804 * perform the check. Possible Errors include U_INVALID_CHAR_FOUND 805 * for invalid UTF-8 sequences, and 806 * U_BUFFER_OVERFLOW_ERROR if the destination buffer is too small 807 * to hold the complete skeleton. 808 * @return The length of the skeleton string, in bytes. The returned length 809 * is always that of the complete skeleton, even when the 810 * supplied buffer is too small (or of zero length) 811 * 812 * @stable ICU 4.2 813 */ 814 U_STABLE int32_t U_EXPORT2 815 uspoof_getSkeletonUTF8(const USpoofChecker *sc, 816 uint32_t type, 817 const char *s, int32_t length, 818 char *dest, int32_t destCapacity, 819 UErrorCode *status); 820 821 #if U_SHOW_CPLUSPLUS_API 822 /** 823 * Get the "skeleton" for an identifier string. 824 * Skeletons are a transformation of the input string; 825 * Two strings are confusable if their skeletons are identical. 826 * See Unicode UAX 39 for additional information. 827 * 828 * Using skeletons directly makes it possible to quickly check 829 * whether an identifier is confusable with any of some large 830 * set of existing identifiers, by creating an efficiently 831 * searchable collection of the skeletons. 832 * 833 * @param sc The USpoofChecker. 834 * @param type The type of skeleton, corresponding to which 835 * of the Unicode confusable data tables to use. 836 * The default is Mixed-Script, Lowercase. 837 * Allowed options are USPOOF_SINGLE_SCRIPT_CONFUSABLE and 838 * USPOOF_ANY_CASE_CONFUSABLE. The two flags may be ORed. 839 * @param s The input string whose skeleton will be computed. 840 * @param dest The output string, to receive the skeleton string. 841 * @param status The error code, set if an error occurred while attempting to 842 * perform the check. 843 * @return A reference to the destination (skeleton) string. 844 * 845 * @stable ICU 4.2 846 */ 847 U_I18N_API icu::UnicodeString & U_EXPORT2 848 uspoof_getSkeletonUnicodeString(const USpoofChecker *sc, 849 uint32_t type, 850 const icu::UnicodeString &s, 851 icu::UnicodeString &dest, 852 UErrorCode *status); 853 #endif /* U_SHOW_CPLUSPLUS_API */ 854 855 856 /** 857 * Serialize the data for a spoof detector into a chunk of memory. 858 * The flattened spoof detection tables can later be used to efficiently 859 * instantiate a new Spoof Detector. 860 * 861 * @param sc the Spoof Detector whose data is to be serialized. 862 * @param data a pointer to 32-bit-aligned memory to be filled with the data, 863 * can be NULL if capacity==0 864 * @param capacity the number of bytes available at data, 865 * or 0 for preflighting 866 * @param status an in/out ICU UErrorCode; possible errors include: 867 * - U_BUFFER_OVERFLOW_ERROR if the data storage block is too small for serialization 868 * - U_ILLEGAL_ARGUMENT_ERROR the data or capacity parameters are bad 869 * @return the number of bytes written or needed for the spoof data 870 * 871 * @see utrie2_openFromSerialized() 872 * @stable ICU 4.2 873 */ 874 U_STABLE int32_t U_EXPORT2 875 uspoof_serialize(USpoofChecker *sc, 876 void *data, int32_t capacity, 877 UErrorCode *status); 878 879 880 #endif 881 882 #endif /* USPOOF_H */ 883