michael@0: /* michael@0: *************************************************************************** michael@0: * Copyright (C) 2008-2013, International Business Machines Corporation michael@0: * and others. All Rights Reserved. michael@0: *************************************************************************** michael@0: * file name: uspoof.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2008Feb13 michael@0: * created by: Andy Heninger michael@0: * michael@0: * Unicode Spoof Detection michael@0: */ michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/normalizer2.h" michael@0: #include "unicode/uspoof.h" michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/utf16.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "identifier_info.h" michael@0: #include "mutex.h" michael@0: #include "scriptset.h" michael@0: #include "uassert.h" michael@0: #include "ucln_in.h" michael@0: #include "uspoof_impl.h" michael@0: #include "umutex.h" michael@0: michael@0: michael@0: #if !UCONFIG_NO_NORMALIZATION michael@0: michael@0: U_NAMESPACE_USE michael@0: michael@0: michael@0: // michael@0: // Static Objects used by the spoof impl, their thread safe initialization and their cleanup. michael@0: // michael@0: static UnicodeSet *gInclusionSet = NULL; michael@0: static UnicodeSet *gRecommendedSet = NULL; michael@0: static const Normalizer2 *gNfdNormalizer = NULL; michael@0: static UMutex gInitMutex = U_MUTEX_INITIALIZER; michael@0: michael@0: static UBool U_CALLCONV michael@0: uspoof_cleanup(void) { michael@0: delete gInclusionSet; michael@0: gInclusionSet = NULL; michael@0: delete gRecommendedSet; michael@0: gRecommendedSet = NULL; michael@0: gNfdNormalizer = NULL; michael@0: return TRUE; michael@0: } michael@0: michael@0: static void initializeStatics() { michael@0: Mutex m(&gInitMutex); michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: if (gInclusionSet == NULL) { michael@0: gInclusionSet = new UnicodeSet(UNICODE_STRING_SIMPLE("[\ michael@0: \\-.\\u00B7\\u05F3\\u05F4\\u0F0B\\u200C\\u200D\\u2019]"), status); michael@0: gRecommendedSet = new UnicodeSet(UNICODE_STRING_SIMPLE("[\ michael@0: [0-z\\u00C0-\\u017E\\u01A0\\u01A1\\u01AF\\u01B0\\u01CD-\ michael@0: \\u01DC\\u01DE-\\u01E3\\u01E6-\\u01F5\\u01F8-\\u021B\\u021E\ michael@0: \\u021F\\u0226-\\u0233\\u02BB\\u02BC\\u02EC\\u0300-\\u0304\ michael@0: \\u0306-\\u030C\\u030F-\\u0311\\u0313\\u0314\\u031B\\u0323-\ michael@0: \\u0328\\u032D\\u032E\\u0330\\u0331\\u0335\\u0338\\u0339\ michael@0: \\u0342-\\u0345\\u037B-\\u03CE\\u03FC-\\u045F\\u048A-\\u0525\ michael@0: \\u0531-\\u0586\\u05D0-\\u05F2\\u0621-\\u063F\\u0641-\\u0655\ michael@0: \\u0660-\\u0669\\u0670-\\u068D\\u068F-\\u06D5\\u06E5\\u06E6\ michael@0: \\u06EE-\\u06FF\\u0750-\\u07B1\\u0901-\\u0939\\u093C-\\u094D\ michael@0: \\u0950\\u0960-\\u0972\\u0979-\\u0A4D\\u0A5C-\\u0A74\\u0A81-\ michael@0: \\u0B43\\u0B47-\\u0B61\\u0B66-\\u0C56\\u0C60\\u0C61\\u0C66-\ michael@0: \\u0CD6\\u0CE0-\\u0CEF\\u0D02-\\u0D28\\u0D2A-\\u0D39\\u0D3D-\ michael@0: \\u0D43\\u0D46-\\u0D4D\\u0D57-\\u0D61\\u0D66-\\u0D8E\\u0D91-\ michael@0: \\u0DA5\\u0DA7-\\u0DDE\\u0DF2\\u0E01-\\u0ED9\\u0F00\\u0F20-\ michael@0: \\u0F8B\\u0F90-\\u109D\\u10D0-\\u10F0\\u10F7-\\u10FA\\u1200-\ michael@0: \\u135A\\u135F\\u1380-\\u138F\\u1401-\\u167F\\u1780-\\u17A2\ michael@0: \\u17A5-\\u17A7\\u17A9-\\u17B3\\u17B6-\\u17CA\\u17D2\\u17D7-\ michael@0: \\u17DC\\u17E0-\\u17E9\\u1810-\\u18A8\\u18AA-\\u18F5\\u1E00-\ michael@0: \\u1E99\\u1F00-\\u1FFC\\u2D30-\\u2D65\\u2D80-\\u2DDE\\u3005-\ michael@0: \\u3007\\u3041-\\u31B7\\u3400-\\u9FCB\\uA000-\\uA48C\\uA67F\ michael@0: \\uA717-\\uA71F\\uA788\\uAA60-\\uAA7B\\uAC00-\\uD7A3\\uFA0E-\ michael@0: \\uFA29\\U00020000-\ michael@0: \\U0002B734]-[[:Cn:][:nfkcqc=n:][:XIDC=n:]]]"), status); michael@0: gNfdNormalizer = Normalizer2::getNFDInstance(status); michael@0: } michael@0: ucln_i18n_registerCleanup(UCLN_I18N_SPOOF, uspoof_cleanup); michael@0: michael@0: return; michael@0: } michael@0: michael@0: michael@0: U_CAPI USpoofChecker * U_EXPORT2 michael@0: uspoof_open(UErrorCode *status) { michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: initializeStatics(); michael@0: SpoofImpl *si = new SpoofImpl(SpoofData::getDefault(*status), *status); michael@0: if (U_FAILURE(*status)) { michael@0: delete si; michael@0: si = NULL; michael@0: } michael@0: return reinterpret_cast(si); michael@0: } michael@0: michael@0: michael@0: U_CAPI USpoofChecker * U_EXPORT2 michael@0: uspoof_openFromSerialized(const void *data, int32_t length, int32_t *pActualLength, michael@0: UErrorCode *status) { michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: initializeStatics(); michael@0: SpoofData *sd = new SpoofData(data, length, *status); michael@0: SpoofImpl *si = new SpoofImpl(sd, *status); michael@0: if (U_FAILURE(*status)) { michael@0: delete sd; michael@0: delete si; michael@0: return NULL; michael@0: } michael@0: if (sd == NULL || si == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: delete sd; michael@0: delete si; michael@0: return NULL; michael@0: } michael@0: michael@0: if (pActualLength != NULL) { michael@0: *pActualLength = sd->fRawData->fLength; michael@0: } michael@0: return reinterpret_cast(si); michael@0: } michael@0: michael@0: michael@0: U_CAPI USpoofChecker * U_EXPORT2 michael@0: uspoof_clone(const USpoofChecker *sc, UErrorCode *status) { michael@0: const SpoofImpl *src = SpoofImpl::validateThis(sc, *status); michael@0: if (src == NULL) { michael@0: return NULL; michael@0: } michael@0: SpoofImpl *result = new SpoofImpl(*src, *status); // copy constructor michael@0: if (U_FAILURE(*status)) { michael@0: delete result; michael@0: result = NULL; michael@0: } michael@0: return reinterpret_cast(result); michael@0: } michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uspoof_close(USpoofChecker *sc) { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: SpoofImpl *This = SpoofImpl::validateThis(sc, status); michael@0: delete This; michael@0: } michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uspoof_setChecks(USpoofChecker *sc, int32_t checks, UErrorCode *status) { michael@0: SpoofImpl *This = SpoofImpl::validateThis(sc, *status); michael@0: if (This == NULL) { michael@0: return; michael@0: } michael@0: michael@0: // Verify that the requested checks are all ones (bits) that michael@0: // are acceptable, known values. michael@0: if (checks & ~USPOOF_ALL_CHECKS) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return; michael@0: } michael@0: michael@0: This->fChecks = checks; michael@0: } michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uspoof_getChecks(const USpoofChecker *sc, UErrorCode *status) { michael@0: const SpoofImpl *This = SpoofImpl::validateThis(sc, *status); michael@0: if (This == NULL) { michael@0: return 0; michael@0: } michael@0: return This->fChecks; michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uspoof_setRestrictionLevel(USpoofChecker *sc, URestrictionLevel restrictionLevel) { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: SpoofImpl *This = SpoofImpl::validateThis(sc, status); michael@0: if (This != NULL) { michael@0: This->fRestrictionLevel = restrictionLevel; michael@0: } michael@0: } michael@0: michael@0: U_CAPI URestrictionLevel U_EXPORT2 michael@0: uspoof_getRestrictionLevel(const USpoofChecker *sc) { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: const SpoofImpl *This = SpoofImpl::validateThis(sc, status); michael@0: if (This == NULL) { michael@0: return USPOOF_UNRESTRICTIVE; michael@0: } michael@0: return This->fRestrictionLevel; michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uspoof_setAllowedLocales(USpoofChecker *sc, const char *localesList, UErrorCode *status) { michael@0: SpoofImpl *This = SpoofImpl::validateThis(sc, *status); michael@0: if (This == NULL) { michael@0: return; michael@0: } michael@0: This->setAllowedLocales(localesList, *status); michael@0: } michael@0: michael@0: U_CAPI const char * U_EXPORT2 michael@0: uspoof_getAllowedLocales(USpoofChecker *sc, UErrorCode *status) { michael@0: SpoofImpl *This = SpoofImpl::validateThis(sc, *status); michael@0: if (This == NULL) { michael@0: return NULL; michael@0: } michael@0: return This->getAllowedLocales(*status); michael@0: } michael@0: michael@0: michael@0: U_CAPI const USet * U_EXPORT2 michael@0: uspoof_getAllowedChars(const USpoofChecker *sc, UErrorCode *status) { michael@0: const UnicodeSet *result = uspoof_getAllowedUnicodeSet(sc, status); michael@0: return result->toUSet(); michael@0: } michael@0: michael@0: U_CAPI const UnicodeSet * U_EXPORT2 michael@0: uspoof_getAllowedUnicodeSet(const USpoofChecker *sc, UErrorCode *status) { michael@0: const SpoofImpl *This = SpoofImpl::validateThis(sc, *status); michael@0: if (This == NULL) { michael@0: return NULL; michael@0: } michael@0: return This->fAllowedCharsSet; michael@0: } michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uspoof_setAllowedChars(USpoofChecker *sc, const USet *chars, UErrorCode *status) { michael@0: const UnicodeSet *set = UnicodeSet::fromUSet(chars); michael@0: uspoof_setAllowedUnicodeSet(sc, set, status); michael@0: } michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uspoof_setAllowedUnicodeSet(USpoofChecker *sc, const UnicodeSet *chars, UErrorCode *status) { michael@0: SpoofImpl *This = SpoofImpl::validateThis(sc, *status); michael@0: if (This == NULL) { michael@0: return; michael@0: } michael@0: if (chars->isBogus()) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return; michael@0: } michael@0: UnicodeSet *clonedSet = static_cast(chars->clone()); michael@0: if (clonedSet == NULL || clonedSet->isBogus()) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: clonedSet->freeze(); michael@0: delete This->fAllowedCharsSet; michael@0: This->fAllowedCharsSet = clonedSet; michael@0: This->fChecks |= USPOOF_CHAR_LIMIT; michael@0: } michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uspoof_check(const USpoofChecker *sc, michael@0: const UChar *id, int32_t length, michael@0: int32_t *position, michael@0: UErrorCode *status) { michael@0: michael@0: const SpoofImpl *This = SpoofImpl::validateThis(sc, *status); michael@0: if (This == NULL) { michael@0: return 0; michael@0: } michael@0: if (length < -1) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: UnicodeString idStr((length == -1), id, length); // Aliasing constructor. michael@0: int32_t result = uspoof_checkUnicodeString(sc, idStr, position, status); michael@0: return result; michael@0: } michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uspoof_checkUTF8(const USpoofChecker *sc, michael@0: const char *id, int32_t length, michael@0: int32_t *position, michael@0: UErrorCode *status) { michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: UnicodeString idStr = UnicodeString::fromUTF8(StringPiece(id, length>=0 ? length : uprv_strlen(id))); michael@0: int32_t result = uspoof_checkUnicodeString(sc, idStr, position, status); michael@0: return result; michael@0: } michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uspoof_areConfusable(const USpoofChecker *sc, michael@0: const UChar *id1, int32_t length1, michael@0: const UChar *id2, int32_t length2, michael@0: UErrorCode *status) { michael@0: SpoofImpl::validateThis(sc, *status); michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: if (length1 < -1 || length2 < -1) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: UnicodeString id1Str((length1==-1), id1, length1); // Aliasing constructor michael@0: UnicodeString id2Str((length2==-1), id2, length2); // Aliasing constructor michael@0: return uspoof_areConfusableUnicodeString(sc, id1Str, id2Str, status); michael@0: } michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uspoof_areConfusableUTF8(const USpoofChecker *sc, michael@0: const char *id1, int32_t length1, michael@0: const char *id2, int32_t length2, michael@0: UErrorCode *status) { michael@0: SpoofImpl::validateThis(sc, *status); michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: if (length1 < -1 || length2 < -1) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: UnicodeString id1Str = UnicodeString::fromUTF8(StringPiece(id1, length1>=0? length1 : uprv_strlen(id1))); michael@0: UnicodeString id2Str = UnicodeString::fromUTF8(StringPiece(id2, length2>=0? length2 : uprv_strlen(id2))); michael@0: int32_t results = uspoof_areConfusableUnicodeString(sc, id1Str, id2Str, status); michael@0: return results; michael@0: } michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uspoof_areConfusableUnicodeString(const USpoofChecker *sc, michael@0: const icu::UnicodeString &id1, michael@0: const icu::UnicodeString &id2, michael@0: UErrorCode *status) { michael@0: const SpoofImpl *This = SpoofImpl::validateThis(sc, *status); michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: // michael@0: // See section 4 of UAX 39 for the algorithm for checking whether two strings are confusable, michael@0: // and for definitions of the types (single, whole, mixed-script) of confusables. michael@0: michael@0: // We only care about a few of the check flags. Ignore the others. michael@0: // If no tests relavant to this function have been specified, return an error. michael@0: // TODO: is this really the right thing to do? It's probably an error on the caller's part, michael@0: // but logically we would just return 0 (no error). michael@0: if ((This->fChecks & (USPOOF_SINGLE_SCRIPT_CONFUSABLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | michael@0: USPOOF_WHOLE_SCRIPT_CONFUSABLE)) == 0) { michael@0: *status = U_INVALID_STATE_ERROR; michael@0: return 0; michael@0: } michael@0: int32_t flagsForSkeleton = This->fChecks & USPOOF_ANY_CASE; michael@0: michael@0: int32_t result = 0; michael@0: IdentifierInfo *identifierInfo = This->getIdentifierInfo(*status); michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: identifierInfo->setIdentifier(id1, *status); michael@0: int32_t id1ScriptCount = identifierInfo->getScriptCount(); michael@0: identifierInfo->setIdentifier(id2, *status); michael@0: int32_t id2ScriptCount = identifierInfo->getScriptCount(); michael@0: This->releaseIdentifierInfo(identifierInfo); michael@0: identifierInfo = NULL; michael@0: michael@0: if (This->fChecks & USPOOF_SINGLE_SCRIPT_CONFUSABLE) { michael@0: UnicodeString id1Skeleton; michael@0: UnicodeString id2Skeleton; michael@0: if (id1ScriptCount <= 1 && id2ScriptCount <= 1) { michael@0: flagsForSkeleton |= USPOOF_SINGLE_SCRIPT_CONFUSABLE; michael@0: uspoof_getSkeletonUnicodeString(sc, flagsForSkeleton, id1, id1Skeleton, status); michael@0: uspoof_getSkeletonUnicodeString(sc, flagsForSkeleton, id2, id2Skeleton, status); michael@0: if (id1Skeleton == id2Skeleton) { michael@0: result |= USPOOF_SINGLE_SCRIPT_CONFUSABLE; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (result & USPOOF_SINGLE_SCRIPT_CONFUSABLE) { michael@0: // If the two inputs are single script confusable they cannot also be michael@0: // mixed or whole script confusable, according to the UAX39 definitions. michael@0: // So we can skip those tests. michael@0: return result; michael@0: } michael@0: michael@0: // Two identifiers are whole script confusable if each is of a single script michael@0: // and they are mixed script confusable. michael@0: UBool possiblyWholeScriptConfusables = michael@0: id1ScriptCount <= 1 && id2ScriptCount <= 1 && (This->fChecks & USPOOF_WHOLE_SCRIPT_CONFUSABLE); michael@0: michael@0: // michael@0: // Mixed Script Check michael@0: // michael@0: if ((This->fChecks & USPOOF_MIXED_SCRIPT_CONFUSABLE) || possiblyWholeScriptConfusables ) { michael@0: // For getSkeleton(), resetting the USPOOF_SINGLE_SCRIPT_CONFUSABLE flag will get us michael@0: // the mixed script table skeleton, which is what we want. michael@0: // The Any Case / Lower Case bit in the skelton flags was set at the top of the function. michael@0: UnicodeString id1Skeleton; michael@0: UnicodeString id2Skeleton; michael@0: flagsForSkeleton &= ~USPOOF_SINGLE_SCRIPT_CONFUSABLE; michael@0: uspoof_getSkeletonUnicodeString(sc, flagsForSkeleton, id1, id1Skeleton, status); michael@0: uspoof_getSkeletonUnicodeString(sc, flagsForSkeleton, id2, id2Skeleton, status); michael@0: if (id1Skeleton == id2Skeleton) { michael@0: result |= USPOOF_MIXED_SCRIPT_CONFUSABLE; michael@0: if (possiblyWholeScriptConfusables) { michael@0: result |= USPOOF_WHOLE_SCRIPT_CONFUSABLE; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uspoof_checkUnicodeString(const USpoofChecker *sc, michael@0: const icu::UnicodeString &id, michael@0: int32_t *position, michael@0: UErrorCode *status) { michael@0: const SpoofImpl *This = SpoofImpl::validateThis(sc, *status); michael@0: if (This == NULL) { michael@0: return 0; michael@0: } michael@0: int32_t result = 0; michael@0: michael@0: IdentifierInfo *identifierInfo = NULL; michael@0: if ((This->fChecks) & (USPOOF_RESTRICTION_LEVEL | USPOOF_MIXED_NUMBERS)) { michael@0: identifierInfo = This->getIdentifierInfo(*status); michael@0: if (U_FAILURE(*status)) { michael@0: goto cleanupAndReturn; michael@0: } michael@0: identifierInfo->setIdentifier(id, *status); michael@0: identifierInfo->setIdentifierProfile(*This->fAllowedCharsSet); michael@0: } michael@0: michael@0: michael@0: if ((This->fChecks) & USPOOF_RESTRICTION_LEVEL) { michael@0: URestrictionLevel idRestrictionLevel = identifierInfo->getRestrictionLevel(*status); michael@0: if (idRestrictionLevel > This->fRestrictionLevel) { michael@0: result |= USPOOF_RESTRICTION_LEVEL; michael@0: } michael@0: if (This->fChecks & USPOOF_AUX_INFO) { michael@0: result |= idRestrictionLevel; michael@0: } michael@0: } michael@0: michael@0: if ((This->fChecks) & USPOOF_MIXED_NUMBERS) { michael@0: const UnicodeSet *numerics = identifierInfo->getNumerics(); michael@0: if (numerics->size() > 1) { michael@0: result |= USPOOF_MIXED_NUMBERS; michael@0: } michael@0: michael@0: // TODO: ICU4J returns the UnicodeSet of the numerics found in the identifier. michael@0: // We have no easy way to do the same in C. michael@0: // if (checkResult != null) { michael@0: // checkResult.numerics = numerics; michael@0: // } michael@0: } michael@0: michael@0: michael@0: if (This->fChecks & (USPOOF_CHAR_LIMIT)) { michael@0: int32_t i; michael@0: UChar32 c; michael@0: int32_t length = id.length(); michael@0: for (i=0; ifAllowedCharsSet->contains(c)) { michael@0: result |= USPOOF_CHAR_LIMIT; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (This->fChecks & michael@0: (USPOOF_WHOLE_SCRIPT_CONFUSABLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_INVISIBLE)) { michael@0: // These are the checks that need to be done on NFD input michael@0: UnicodeString nfdText; michael@0: gNfdNormalizer->normalize(id, nfdText, *status); michael@0: int32_t nfdLength = nfdText.length(); michael@0: michael@0: if (This->fChecks & USPOOF_INVISIBLE) { michael@0: michael@0: // scan for more than one occurence of the same non-spacing mark michael@0: // in a sequence of non-spacing marks. michael@0: int32_t i; michael@0: UChar32 c; michael@0: UChar32 firstNonspacingMark = 0; michael@0: UBool haveMultipleMarks = FALSE; michael@0: UnicodeSet marksSeenSoFar; // Set of combining marks in a single combining sequence. michael@0: michael@0: for (i=0; ifChecks & (USPOOF_WHOLE_SCRIPT_CONFUSABLE | USPOOF_MIXED_SCRIPT_CONFUSABLE)) { michael@0: // The basic test is the same for both whole and mixed script confusables. michael@0: // Compute the set of scripts that every input character has a confusable in. michael@0: // For this computation an input character is always considered to be michael@0: // confusable with itself in its own script. michael@0: // michael@0: // If the number of such scripts is two or more, and the input consisted of michael@0: // characters all from a single script, we have a whole script confusable. michael@0: // (The two scripts will be the original script and the one that is confusable) michael@0: // michael@0: // If the number of such scripts >= one, and the original input contained characters from michael@0: // more than one script, we have a mixed script confusable. (We can transform michael@0: // some of the characters, and end up with a visually similar string all in michael@0: // one script.) michael@0: michael@0: if (identifierInfo == NULL) { michael@0: identifierInfo = This->getIdentifierInfo(*status); michael@0: if (U_FAILURE(*status)) { michael@0: goto cleanupAndReturn; michael@0: } michael@0: identifierInfo->setIdentifier(id, *status); michael@0: } michael@0: michael@0: int32_t scriptCount = identifierInfo->getScriptCount(); michael@0: michael@0: ScriptSet scripts; michael@0: This->wholeScriptCheck(nfdText, &scripts, *status); michael@0: int32_t confusableScriptCount = scripts.countMembers(); michael@0: //printf("confusableScriptCount = %d\n", confusableScriptCount); michael@0: michael@0: if ((This->fChecks & USPOOF_WHOLE_SCRIPT_CONFUSABLE) && michael@0: confusableScriptCount >= 2 && michael@0: scriptCount == 1) { michael@0: result |= USPOOF_WHOLE_SCRIPT_CONFUSABLE; michael@0: } michael@0: michael@0: if ((This->fChecks & USPOOF_MIXED_SCRIPT_CONFUSABLE) && michael@0: confusableScriptCount >= 1 && michael@0: scriptCount > 1) { michael@0: result |= USPOOF_MIXED_SCRIPT_CONFUSABLE; michael@0: } michael@0: } michael@0: } michael@0: michael@0: cleanupAndReturn: michael@0: This->releaseIdentifierInfo(identifierInfo); michael@0: if (position != NULL) { michael@0: *position = 0; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uspoof_getSkeleton(const USpoofChecker *sc, michael@0: uint32_t type, michael@0: const UChar *id, int32_t length, michael@0: UChar *dest, int32_t destCapacity, michael@0: UErrorCode *status) { michael@0: michael@0: SpoofImpl::validateThis(sc, *status); michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: if (length<-1 || destCapacity<0 || (destCapacity==0 && dest!=NULL)) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: UnicodeString idStr((length==-1), id, length); // Aliasing constructor michael@0: UnicodeString destStr; michael@0: uspoof_getSkeletonUnicodeString(sc, type, idStr, destStr, status); michael@0: destStr.extract(dest, destCapacity, *status); michael@0: return destStr.length(); michael@0: } michael@0: michael@0: michael@0: michael@0: U_I18N_API UnicodeString & U_EXPORT2 michael@0: uspoof_getSkeletonUnicodeString(const USpoofChecker *sc, michael@0: uint32_t type, michael@0: const UnicodeString &id, michael@0: UnicodeString &dest, michael@0: UErrorCode *status) { michael@0: const SpoofImpl *This = SpoofImpl::validateThis(sc, *status); michael@0: if (U_FAILURE(*status)) { michael@0: return dest; michael@0: } michael@0: michael@0: int32_t tableMask = 0; michael@0: switch (type) { michael@0: case 0: michael@0: tableMask = USPOOF_ML_TABLE_FLAG; michael@0: break; michael@0: case USPOOF_SINGLE_SCRIPT_CONFUSABLE: michael@0: tableMask = USPOOF_SL_TABLE_FLAG; michael@0: break; michael@0: case USPOOF_ANY_CASE: michael@0: tableMask = USPOOF_MA_TABLE_FLAG; michael@0: break; michael@0: case USPOOF_SINGLE_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE: michael@0: tableMask = USPOOF_SA_TABLE_FLAG; michael@0: break; michael@0: default: michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return dest; michael@0: } michael@0: michael@0: UnicodeString nfdId; michael@0: gNfdNormalizer->normalize(id, nfdId, *status); michael@0: michael@0: // Apply the skeleton mapping to the NFD normalized input string michael@0: // Accumulate the skeleton, possibly unnormalized, in a UnicodeString. michael@0: int32_t inputIndex = 0; michael@0: UnicodeString skelStr; michael@0: int32_t normalizedLen = nfdId.length(); michael@0: for (inputIndex=0; inputIndex < normalizedLen; ) { michael@0: UChar32 c = nfdId.char32At(inputIndex); michael@0: inputIndex += U16_LENGTH(c); michael@0: This->confusableLookup(c, tableMask, skelStr); michael@0: } michael@0: michael@0: gNfdNormalizer->normalize(skelStr, dest, *status); michael@0: return dest; michael@0: } michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uspoof_getSkeletonUTF8(const USpoofChecker *sc, michael@0: uint32_t type, michael@0: const char *id, int32_t length, michael@0: char *dest, int32_t destCapacity, michael@0: UErrorCode *status) { michael@0: SpoofImpl::validateThis(sc, *status); michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: if (length<-1 || destCapacity<0 || (destCapacity==0 && dest!=NULL)) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: UnicodeString srcStr = UnicodeString::fromUTF8(StringPiece(id, length>=0 ? length : uprv_strlen(id))); michael@0: UnicodeString destStr; michael@0: uspoof_getSkeletonUnicodeString(sc, type, srcStr, destStr, status); michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: michael@0: int32_t lengthInUTF8 = 0; michael@0: u_strToUTF8(dest, destCapacity, &lengthInUTF8, michael@0: destStr.getBuffer(), destStr.length(), status); michael@0: return lengthInUTF8; michael@0: } michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: uspoof_serialize(USpoofChecker *sc,void *buf, int32_t capacity, UErrorCode *status) { michael@0: SpoofImpl *This = SpoofImpl::validateThis(sc, *status); michael@0: if (This == NULL) { michael@0: U_ASSERT(U_FAILURE(*status)); michael@0: return 0; michael@0: } michael@0: int32_t dataSize = This->fSpoofData->fRawData->fLength; michael@0: if (capacity < dataSize) { michael@0: *status = U_BUFFER_OVERFLOW_ERROR; michael@0: return dataSize; michael@0: } michael@0: uprv_memcpy(buf, This->fSpoofData->fRawData, dataSize); michael@0: return dataSize; michael@0: } michael@0: michael@0: U_CAPI const USet * U_EXPORT2 michael@0: uspoof_getInclusionSet(UErrorCode *) { michael@0: initializeStatics(); michael@0: return gInclusionSet->toUSet(); michael@0: } michael@0: michael@0: U_CAPI const USet * U_EXPORT2 michael@0: uspoof_getRecommendedSet(UErrorCode *) { michael@0: initializeStatics(); michael@0: return gRecommendedSet->toUSet(); michael@0: } michael@0: michael@0: U_I18N_API const UnicodeSet * U_EXPORT2 michael@0: uspoof_getInclusionUnicodeSet(UErrorCode *) { michael@0: initializeStatics(); michael@0: return gInclusionSet; michael@0: } michael@0: michael@0: U_I18N_API const UnicodeSet * U_EXPORT2 michael@0: uspoof_getRecommendedUnicodeSet(UErrorCode *) { michael@0: initializeStatics(); michael@0: return gRecommendedSet; michael@0: } michael@0: michael@0: michael@0: michael@0: #endif // !UCONFIG_NO_NORMALIZATION