X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/46f4442e9a5a4f3b98b7c1083586332f6a8a99a4..ef6cf650f4a75c3f97de06b51fa104f2069b9ea2:/icuSources/common/usprep.cpp diff --git a/icuSources/common/usprep.cpp b/icuSources/common/usprep.cpp index bd9d7cb1..486c7615 100644 --- a/icuSources/common/usprep.cpp +++ b/icuSources/common/usprep.cpp @@ -1,7 +1,7 @@ /* ******************************************************************************* * - * Copyright (C) 2003-2008, International Business Machines + * Copyright (C) 2003-2016, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* @@ -20,7 +20,7 @@ #include "unicode/usprep.h" -#include "unicode/unorm.h" +#include "unicode/normalizer2.h" #include "unicode/ustring.h" #include "unicode/uchar.h" #include "unicode/uversion.h" @@ -32,8 +32,10 @@ #include "cstring.h" #include "udataswp.h" #include "ucln_cmn.h" -#include "unormimp.h" #include "ubidi_props.h" +#include "uprops.h" + +U_NAMESPACE_USE U_CDECL_BEGIN @@ -41,15 +43,34 @@ U_CDECL_BEGIN Static cache for already opened StringPrep profiles */ static UHashtable *SHARED_DATA_HASHTABLE = NULL; +static icu::UInitOnce gSharedDataInitOnce; -static UMTX usprepMutex = NULL; +static UMutex usprepMutex = U_MUTEX_INITIALIZER; /* format version of spp file */ -static uint8_t formatVersion[4]={ 0, 0, 0, 0 }; +//static uint8_t formatVersion[4]={ 0, 0, 0, 0 }; /* the Unicode version of the sprep data */ static UVersionInfo dataVersion={ 0, 0, 0, 0 }; +/* Profile names must be aligned to UStringPrepProfileType */ +static const char * const PROFILE_NAMES[] = { + "rfc3491", /* USPREP_RFC3491_NAMEPREP */ + "rfc3530cs", /* USPREP_RFC3530_NFS4_CS_PREP */ + "rfc3530csci", /* USPREP_RFC3530_NFS4_CS_PREP_CI */ + "rfc3491", /* USPREP_RFC3530_NSF4_CIS_PREP */ + "rfc3530mixp", /* USPREP_RFC3530_NSF4_MIXED_PREP_PREFIX */ + "rfc3491", /* USPREP_RFC3530_NSF4_MIXED_PREP_SUFFIX */ + "rfc3722", /* USPREP_RFC3722_ISCSI */ + "rfc3920node", /* USPREP_RFC3920_NODEPREP */ + "rfc3920res", /* USPREP_RFC3920_RESOURCEPREP */ + "rfc4011", /* USPREP_RFC4011_MIB */ + "rfc4013", /* USPREP_RFC4013_SASLPREP */ + "rfc4505", /* USPREP_RFC4505_TRACE */ + "rfc4518", /* USPREP_RFC4518_LDAP */ + "rfc4518ci", /* USPREP_RFC4518_LDAP_CI */ +}; + static UBool U_CALLCONV isSPrepAcceptable(void * /* context */, const char * /* type */, @@ -67,7 +88,7 @@ isSPrepAcceptable(void * /* context */, pInfo->formatVersion[2]==UTRIE_SHIFT && pInfo->formatVersion[3]==UTRIE_INDEX_SHIFT ) { - uprv_memcpy(formatVersion, pInfo->formatVersion, 4); + //uprv_memcpy(formatVersion, pInfo->formatVersion, 4); uprv_memcpy(dataVersion, pInfo->dataVersion, 4); return TRUE; } else { @@ -115,7 +136,7 @@ static int32_t usprep_internal_flushCache(UBool noRefCount){ UStringPrepProfile *profile = NULL; UStringPrepKey *key = NULL; - int32_t pos = -1; + int32_t pos = UHASH_FIRST; int32_t deletedNum = 0; const UHashElement *e; @@ -176,39 +197,25 @@ static UBool U_CALLCONV usprep_cleanup(void){ SHARED_DATA_HASHTABLE = NULL; } } - - umtx_destroy(&usprepMutex); /* Don't worry about destroying the mutex even */ - /* if the hash table still exists. The mutex */ - /* will lazily re-init itself if needed. */ + gSharedDataInitOnce.reset(); return (SHARED_DATA_HASHTABLE == NULL); } U_CDECL_END -static void -usprep_init() { - umtx_init(&usprepMutex); -} /** Initializes the cache for resources */ +static void U_CALLCONV +createCache(UErrorCode &status) { + SHARED_DATA_HASHTABLE = uhash_open(hashEntry, compareEntries, NULL, &status); + if (U_FAILURE(status)) { + SHARED_DATA_HASHTABLE = NULL; + } + ucln_common_registerCleanup(UCLN_COMMON_USPREP, usprep_cleanup); +} + static void initCache(UErrorCode *status) { - UBool makeCache; - UMTX_CHECK(&usprepMutex, (SHARED_DATA_HASHTABLE == NULL), makeCache); - if(makeCache) { - UHashtable *newCache = uhash_open(hashEntry, compareEntries, NULL, status); - if (U_SUCCESS(*status)) { - umtx_lock(&usprepMutex); - if(SHARED_DATA_HASHTABLE == NULL) { - SHARED_DATA_HASHTABLE = newCache; - ucln_common_registerCleanup(UCLN_COMMON_USPREP, usprep_cleanup); - newCache = NULL; - } - umtx_unlock(&usprepMutex); - } - if(newCache != NULL) { - uhash_close(newCache); - } - } + umtx_initOnce(gSharedDataInitOnce, &createCache, *status); } static UBool U_CALLCONV @@ -261,7 +268,7 @@ loadData(UStringPrepProfile* profile, /* initialize some variables */ profile->mappingData=(uint16_t *)((uint8_t *)(p+_SPREP_INDEX_TOP)+profile->indexes[_SPREP_INDEX_TRIE_SIZE]); - unorm_getUnicodeVersion(&normUnicodeVersion, errorCode); + u_getUnicodeVersion(normUnicodeVersion); normUniVer = (normUnicodeVersion[0] << 24) + (normUnicodeVersion[1] << 16) + (normUnicodeVersion[2] << 8 ) + (normUnicodeVersion[3]); sprepUniVer = (dataVersion[0] << 24) + (dataVersion[1] << 16) + @@ -316,89 +323,68 @@ usprep_getProfile(const char* path, /* fetch the data from the cache */ umtx_lock(&usprepMutex); profile = (UStringPrepProfile*) (uhash_get(SHARED_DATA_HASHTABLE,&stackKey)); + if(profile != NULL) { + profile->refCount++; + } umtx_unlock(&usprepMutex); - if(profile == NULL){ - UStringPrepKey* key = (UStringPrepKey*) uprv_malloc(sizeof(UStringPrepKey)); - if(key == NULL){ - *status = U_MEMORY_ALLOCATION_ERROR; - return NULL; - } + if(profile == NULL) { /* else load the data and put the data in the cache */ - profile = (UStringPrepProfile*) uprv_malloc(sizeof(UStringPrepProfile)); - if(profile == NULL){ + LocalMemory newProfile; + if(newProfile.allocateInsteadAndReset() == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; - uprv_free(key); return NULL; } - /* initialize the data struct members */ - uprv_memset(profile->indexes,0,sizeof(profile->indexes)); - profile->mappingData = NULL; - profile->sprepData = NULL; - profile->refCount = 0; - - /* initialize the key memebers */ - key->name = (char*) uprv_malloc(uprv_strlen(name)+1); - if(key->name == NULL){ - *status = U_MEMORY_ALLOCATION_ERROR; - uprv_free(key); - uprv_free(profile); + /* load the data */ + if(!loadData(newProfile.getAlias(), path, name, _SPREP_DATA_TYPE, status) || U_FAILURE(*status) ){ return NULL; } - uprv_strcpy(key->name, name); - - key->path=NULL; + /* get the options */ + newProfile->doNFKC = (UBool)((newProfile->indexes[_SPREP_OPTIONS] & _SPREP_NORMALIZATION_ON) > 0); + newProfile->checkBiDi = (UBool)((newProfile->indexes[_SPREP_OPTIONS] & _SPREP_CHECK_BIDI_ON) > 0); - if(path != NULL){ - key->path = (char*) uprv_malloc(uprv_strlen(path)+1); - if(key->path == NULL){ - *status = U_MEMORY_ALLOCATION_ERROR; - uprv_free(key->name); - uprv_free(key); - uprv_free(profile); - return NULL; - } - uprv_strcpy(key->path, path); - } + if(newProfile->checkBiDi) { + newProfile->bdp = ubidi_getSingleton(); + } - /* load the data */ - if(!loadData(profile, path, name, _SPREP_DATA_TYPE, status) || U_FAILURE(*status) ){ - uprv_free(key->path); - uprv_free(key->name); - uprv_free(key); - uprv_free(profile); + LocalMemory key; + LocalMemory keyName; + LocalMemory keyPath; + if( key.allocateInsteadAndReset() == NULL || + keyName.allocateInsteadAndCopy(uprv_strlen(name)+1) == NULL || + (path != NULL && + keyPath.allocateInsteadAndCopy(uprv_strlen(path)+1) == NULL) + ) { + *status = U_MEMORY_ALLOCATION_ERROR; + usprep_unload(newProfile.getAlias()); return NULL; } - - /* get the options */ - profile->doNFKC = (UBool)((profile->indexes[_SPREP_OPTIONS] & _SPREP_NORMALIZATION_ON) > 0); - profile->checkBiDi = (UBool)((profile->indexes[_SPREP_OPTIONS] & _SPREP_CHECK_BIDI_ON) > 0); - if(profile->checkBiDi) { - profile->bdp = ubidi_getSingleton(status); - if(U_FAILURE(*status)) { - usprep_unload(profile); - uprv_free(key->path); - uprv_free(key->name); - uprv_free(key); - uprv_free(profile); - return NULL; - } - } else { - profile->bdp = NULL; - } - umtx_lock(&usprepMutex); - /* add the data object to the cache */ - uhash_put(SHARED_DATA_HASHTABLE, key, profile, status); + // If another thread already inserted the same key/value, refcount and cleanup our thread data + profile = (UStringPrepProfile*) (uhash_get(SHARED_DATA_HASHTABLE,&stackKey)); + if(profile != NULL) { + profile->refCount++; + usprep_unload(newProfile.getAlias()); + } + else { + /* initialize the key members */ + key->name = keyName.orphan(); + uprv_strcpy(key->name, name); + if(path != NULL){ + key->path = keyPath.orphan(); + uprv_strcpy(key->path, path); + } + profile = newProfile.orphan(); + + /* add the data object to the cache */ + profile->refCount = 1; + uhash_put(SHARED_DATA_HASHTABLE, key.orphan(), profile, status); + } umtx_unlock(&usprepMutex); } - umtx_lock(&usprepMutex); - /* increment the refcount */ - profile->refCount++; - umtx_unlock(&usprepMutex); return profile; } @@ -411,13 +397,25 @@ usprep_open(const char* path, if(status == NULL || U_FAILURE(*status)){ return NULL; } - /* initialize the mutex */ - usprep_init(); /* initialize the profile struct members */ return usprep_getProfile(path,name,status); } +U_CAPI UStringPrepProfile* U_EXPORT2 +usprep_openByType(UStringPrepProfileType type, + UErrorCode* status) { + if(status == NULL || U_FAILURE(*status)){ + return NULL; + } + int32_t index = (int32_t)type; + if (index < 0 || index >= UPRV_LENGTHOF(PROFILE_NAMES)) { + *status = U_ILLEGAL_ARGUMENT_ERROR; + return NULL; + } + return usprep_open(NULL, PROFILE_NAMES[index], status); +} + U_CAPI void U_EXPORT2 usprep_close(UStringPrepProfile* profile){ if(profile==NULL){ @@ -505,8 +503,7 @@ getValues(uint16_t trieWord, int16_t& value, UBool& isIndex){ return type; } - - +// TODO: change to writing to UnicodeString not UChar * static int32_t usprep_map( const UStringPrepProfile* profile, const UChar* src, int32_t srcLength, @@ -601,31 +598,7 @@ usprep_map( const UStringPrepProfile* profile, return u_terminateUChars(dest, destCapacity, destIndex, status); } - -static int32_t -usprep_normalize( const UChar* src, int32_t srcLength, - UChar* dest, int32_t destCapacity, - UErrorCode* status ){ - /* - * Option UNORM_BEFORE_PRI_29: - * - * IDNA as interpreted by IETF members (see unicode mailing list 2004H1) - * requires strict adherence to Unicode 3.2 normalization, - * including buggy composition from before fixing Public Review Issue #29. - * Note that this results in some valid but nonsensical text to be - * either corrupted or rejected, depending on the text. - * See http://www.unicode.org/review/resolved-pri.html#pri29 - * See unorm.cpp and cnormtst.c - */ - return unorm_normalize( - src, srcLength, - UNORM_NFKC, UNORM_UNICODE_3_2|UNORM_BEFORE_PRI_29, - dest, destCapacity, - status); -} - - - /* +/* 1) Map -- For each character in the input, check if it has a mapping and, if so, replace it with its mapping. @@ -663,10 +636,6 @@ usprep_normalize( const UChar* src, int32_t srcLength, character MUST be the first character of the string, and a RandALCat character MUST be the last character of the string. */ - -#define MAX_STACK_BUFFER_SIZE 300 - - U_CAPI int32_t U_EXPORT2 usprep_prepare( const UStringPrepProfile* profile, const UChar* src, int32_t srcLength, @@ -676,100 +645,91 @@ usprep_prepare( const UStringPrepProfile* profile, UErrorCode* status ){ // check error status - if(status == NULL || U_FAILURE(*status)){ + if(U_FAILURE(*status)){ return 0; } - + //check arguments - if(profile==NULL || src==NULL || srcLength<-1 || (dest==NULL && destCapacity!=0)) { + if(profile==NULL || + (src==NULL ? srcLength!=0 : srcLength<-1) || + (dest==NULL ? destCapacity!=0 : destCapacity<0)) { *status=U_ILLEGAL_ARGUMENT_ERROR; return 0; } - UChar b1Stack[MAX_STACK_BUFFER_SIZE], b2Stack[MAX_STACK_BUFFER_SIZE]; - UChar *b1 = b1Stack, *b2 = b2Stack; - int32_t b1Len, b2Len=0, - b1Capacity = MAX_STACK_BUFFER_SIZE , - b2Capacity = MAX_STACK_BUFFER_SIZE; - uint16_t result; - int32_t b2Index = 0; - UCharDirection direction=U_CHAR_DIRECTION_COUNT, firstCharDir=U_CHAR_DIRECTION_COUNT; - UBool leftToRight=FALSE, rightToLeft=FALSE; - int32_t rtlPos =-1, ltrPos =-1; - //get the string length - if(srcLength == -1){ + if(srcLength < 0){ srcLength = u_strlen(src); } // map - b1Len = usprep_map(profile, src, srcLength, b1, b1Capacity, options, parseError, status); + UnicodeString s1; + UChar *b1 = s1.getBuffer(srcLength); + if(b1==NULL){ + *status = U_MEMORY_ALLOCATION_ERROR; + return 0; + } + int32_t b1Len = usprep_map(profile, src, srcLength, + b1, s1.getCapacity(), options, parseError, status); + s1.releaseBuffer(U_SUCCESS(*status) ? b1Len : 0); if(*status == U_BUFFER_OVERFLOW_ERROR){ // redo processing of string /* we do not have enough room so grow the buffer*/ - b1 = (UChar*) uprv_malloc(b1Len * U_SIZEOF_UCHAR); + b1 = s1.getBuffer(b1Len); if(b1==NULL){ *status = U_MEMORY_ALLOCATION_ERROR; - goto CLEANUP; + return 0; } *status = U_ZERO_ERROR; // reset error - - b1Len = usprep_map(profile, src, srcLength, b1, b1Len, options, parseError, status); - + b1Len = usprep_map(profile, src, srcLength, + b1, s1.getCapacity(), options, parseError, status); + s1.releaseBuffer(U_SUCCESS(*status) ? b1Len : 0); + } + if(U_FAILURE(*status)){ + return 0; } // normalize - if(profile->doNFKC == TRUE){ - b2Len = usprep_normalize(b1,b1Len, b2,b2Capacity,status); - - if(*status == U_BUFFER_OVERFLOW_ERROR){ - // redo processing of string - /* we do not have enough room so grow the buffer*/ - b2 = (UChar*) uprv_malloc(b2Len * U_SIZEOF_UCHAR); - if(b2==NULL){ - *status = U_MEMORY_ALLOCATION_ERROR; - goto CLEANUP; - } - - *status = U_ZERO_ERROR; // reset error - - b2Len = usprep_normalize(b1,b1Len, b2,b2Len,status); - + UnicodeString s2; + if(profile->doNFKC){ + const Normalizer2 *n2 = Normalizer2::getNFKCInstance(*status); + FilteredNormalizer2 fn2(*n2, *uniset_getUnicode32Instance(*status)); + if(U_FAILURE(*status)){ + return 0; } - + fn2.normalize(s1, s2, *status); }else{ - b2 = b1; - b2Len = b1Len; + s2.fastCopyFrom(s1); } - - if(U_FAILURE(*status)){ - goto CLEANUP; + return 0; } - UChar32 ch; - UStringPrepType type; - int16_t value; - UBool isIndex; - // Prohibit and checkBiDi in one pass - for(b2Index=0; b2IndexsprepTrie,ch,result); - - type = getValues(result, value, isIndex); + + int16_t value; + UBool isIndex; + UStringPrepType type = getValues(result, value, isIndex); if( type == USPREP_PROHIBITED || ((result < _SPREP_TYPE_THRESHOLD) && (result & 0x01) /* first bit says it the code point is prohibited*/) ){ *status = U_STRINGPREP_PROHIBITED_ERROR; uprv_syntaxError(b1, b2Index-U16_LENGTH(ch), b2Len, parseError); - goto CLEANUP; + return 0; } if(profile->checkBiDi) { @@ -786,13 +746,13 @@ usprep_prepare( const UStringPrepProfile* profile, rtlPos = b2Index-1; } } - } + } if(profile->checkBiDi == TRUE){ // satisfy 2 if( leftToRight == TRUE && rightToLeft == TRUE){ *status = U_STRINGPREP_CHECK_BIDI_ERROR; uprv_syntaxError(b2,(rtlPos>ltrPos) ? rtlPos : ltrPos, b2Len, parseError); - goto CLEANUP; + return 0; } //satisfy 3 @@ -805,21 +765,7 @@ usprep_prepare( const UStringPrepProfile* profile, return FALSE; } } - if(b2Len>0 && b2Len <= destCapacity){ - uprv_memmove(dest,b2, b2Len*U_SIZEOF_UCHAR); - } - -CLEANUP: - if(b1!=b1Stack){ - uprv_free(b1); - b1=NULL; - } - - if(b2!=b1Stack && b2!=b2Stack && b2!=b1 /* b1 should not be freed twice */){ - uprv_free(b2); - b2=NULL; - } - return u_terminateUChars(dest, destCapacity, b2Len, status); + return s2.extract(dest, destCapacity, *status); } @@ -917,7 +863,7 @@ usprep_swap(const UDataSwapper *ds, /* swap the uint16_t mappingTable[] */ count=indexes[_SPREP_INDEX_MAPPING_DATA_SIZE]; ds->swapArray16(ds, inBytes+offset, count, outBytes+offset, pErrorCode); - offset+=count; + //offset+=count; } return headerSize+size;