michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 2003-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: usprep.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2003jul2 michael@0: * created by: Ram Viswanadha michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_IDNA michael@0: michael@0: #include "unicode/usprep.h" michael@0: michael@0: #include "unicode/unorm.h" michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/uchar.h" michael@0: #include "unicode/uversion.h" michael@0: #include "umutex.h" michael@0: #include "cmemory.h" michael@0: #include "sprpimpl.h" michael@0: #include "ustr_imp.h" michael@0: #include "uhash.h" michael@0: #include "cstring.h" michael@0: #include "udataswp.h" michael@0: #include "ucln_cmn.h" michael@0: #include "ubidi_props.h" michael@0: michael@0: U_NAMESPACE_USE michael@0: michael@0: U_CDECL_BEGIN michael@0: michael@0: /* michael@0: Static cache for already opened StringPrep profiles michael@0: */ michael@0: static UHashtable *SHARED_DATA_HASHTABLE = NULL; michael@0: static icu::UInitOnce gSharedDataInitOnce; michael@0: michael@0: static UMutex usprepMutex = U_MUTEX_INITIALIZER; michael@0: michael@0: /* format version of spp file */ michael@0: //static uint8_t formatVersion[4]={ 0, 0, 0, 0 }; michael@0: michael@0: /* the Unicode version of the sprep data */ michael@0: static UVersionInfo dataVersion={ 0, 0, 0, 0 }; michael@0: michael@0: /* Profile names must be aligned to UStringPrepProfileType */ michael@0: static const char * const PROFILE_NAMES[] = { michael@0: "rfc3491", /* USPREP_RFC3491_NAMEPREP */ michael@0: "rfc3530cs", /* USPREP_RFC3530_NFS4_CS_PREP */ michael@0: "rfc3530csci", /* USPREP_RFC3530_NFS4_CS_PREP_CI */ michael@0: "rfc3491", /* USPREP_RFC3530_NSF4_CIS_PREP */ michael@0: "rfc3530mixp", /* USPREP_RFC3530_NSF4_MIXED_PREP_PREFIX */ michael@0: "rfc3491", /* USPREP_RFC3530_NSF4_MIXED_PREP_SUFFIX */ michael@0: "rfc3722", /* USPREP_RFC3722_ISCSI */ michael@0: "rfc3920node", /* USPREP_RFC3920_NODEPREP */ michael@0: "rfc3920res", /* USPREP_RFC3920_RESOURCEPREP */ michael@0: "rfc4011", /* USPREP_RFC4011_MIB */ michael@0: "rfc4013", /* USPREP_RFC4013_SASLPREP */ michael@0: "rfc4505", /* USPREP_RFC4505_TRACE */ michael@0: "rfc4518", /* USPREP_RFC4518_LDAP */ michael@0: "rfc4518ci", /* USPREP_RFC4518_LDAP_CI */ michael@0: }; michael@0: michael@0: static UBool U_CALLCONV michael@0: isSPrepAcceptable(void * /* context */, michael@0: const char * /* type */, michael@0: const char * /* name */, michael@0: const UDataInfo *pInfo) { michael@0: if( michael@0: pInfo->size>=20 && michael@0: pInfo->isBigEndian==U_IS_BIG_ENDIAN && michael@0: pInfo->charsetFamily==U_CHARSET_FAMILY && michael@0: pInfo->dataFormat[0]==0x53 && /* dataFormat="SPRP" */ michael@0: pInfo->dataFormat[1]==0x50 && michael@0: pInfo->dataFormat[2]==0x52 && michael@0: pInfo->dataFormat[3]==0x50 && michael@0: pInfo->formatVersion[0]==3 && michael@0: pInfo->formatVersion[2]==UTRIE_SHIFT && michael@0: pInfo->formatVersion[3]==UTRIE_INDEX_SHIFT michael@0: ) { michael@0: //uprv_memcpy(formatVersion, pInfo->formatVersion, 4); michael@0: uprv_memcpy(dataVersion, pInfo->dataVersion, 4); michael@0: return TRUE; michael@0: } else { michael@0: return FALSE; michael@0: } michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: getSPrepFoldingOffset(uint32_t data) { michael@0: michael@0: return (int32_t)data; michael@0: michael@0: } michael@0: michael@0: /* hashes an entry */ michael@0: static int32_t U_CALLCONV michael@0: hashEntry(const UHashTok parm) { michael@0: UStringPrepKey *b = (UStringPrepKey *)parm.pointer; michael@0: UHashTok namekey, pathkey; michael@0: namekey.pointer = b->name; michael@0: pathkey.pointer = b->path; michael@0: return uhash_hashChars(namekey)+37*uhash_hashChars(pathkey); michael@0: } michael@0: michael@0: /* compares two entries */ michael@0: static UBool U_CALLCONV michael@0: compareEntries(const UHashTok p1, const UHashTok p2) { michael@0: UStringPrepKey *b1 = (UStringPrepKey *)p1.pointer; michael@0: UStringPrepKey *b2 = (UStringPrepKey *)p2.pointer; michael@0: UHashTok name1, name2, path1, path2; michael@0: name1.pointer = b1->name; michael@0: name2.pointer = b2->name; michael@0: path1.pointer = b1->path; michael@0: path2.pointer = b2->path; michael@0: return ((UBool)(uhash_compareChars(name1, name2) & michael@0: uhash_compareChars(path1, path2))); michael@0: } michael@0: michael@0: static void michael@0: usprep_unload(UStringPrepProfile* data){ michael@0: udata_close(data->sprepData); michael@0: } michael@0: michael@0: static int32_t michael@0: usprep_internal_flushCache(UBool noRefCount){ michael@0: UStringPrepProfile *profile = NULL; michael@0: UStringPrepKey *key = NULL; michael@0: int32_t pos = -1; michael@0: int32_t deletedNum = 0; michael@0: const UHashElement *e; michael@0: michael@0: /* michael@0: * if shared data hasn't even been lazy evaluated yet michael@0: * return 0 michael@0: */ michael@0: umtx_lock(&usprepMutex); michael@0: if (SHARED_DATA_HASHTABLE == NULL) { michael@0: umtx_unlock(&usprepMutex); michael@0: return 0; michael@0: } michael@0: michael@0: /*creates an enumeration to iterate through every element in the table */ michael@0: while ((e = uhash_nextElement(SHARED_DATA_HASHTABLE, &pos)) != NULL) michael@0: { michael@0: profile = (UStringPrepProfile *) e->value.pointer; michael@0: key = (UStringPrepKey *) e->key.pointer; michael@0: michael@0: if ((noRefCount== FALSE && profile->refCount == 0) || michael@0: noRefCount== TRUE) { michael@0: deletedNum++; michael@0: uhash_removeElement(SHARED_DATA_HASHTABLE, e); michael@0: michael@0: /* unload the data */ michael@0: usprep_unload(profile); michael@0: michael@0: if(key->name != NULL) { michael@0: uprv_free(key->name); michael@0: key->name=NULL; michael@0: } michael@0: if(key->path != NULL) { michael@0: uprv_free(key->path); michael@0: key->path=NULL; michael@0: } michael@0: uprv_free(profile); michael@0: uprv_free(key); michael@0: } michael@0: michael@0: } michael@0: umtx_unlock(&usprepMutex); michael@0: michael@0: return deletedNum; michael@0: } michael@0: michael@0: /* Works just like ucnv_flushCache() michael@0: static int32_t michael@0: usprep_flushCache(){ michael@0: return usprep_internal_flushCache(FALSE); michael@0: } michael@0: */ michael@0: michael@0: static UBool U_CALLCONV usprep_cleanup(void){ michael@0: if (SHARED_DATA_HASHTABLE != NULL) { michael@0: usprep_internal_flushCache(TRUE); michael@0: if (SHARED_DATA_HASHTABLE != NULL && uhash_count(SHARED_DATA_HASHTABLE) == 0) { michael@0: uhash_close(SHARED_DATA_HASHTABLE); michael@0: SHARED_DATA_HASHTABLE = NULL; michael@0: } michael@0: } michael@0: gSharedDataInitOnce.reset(); michael@0: return (SHARED_DATA_HASHTABLE == NULL); michael@0: } michael@0: U_CDECL_END michael@0: michael@0: michael@0: /** Initializes the cache for resources */ michael@0: static void U_CALLCONV michael@0: createCache(UErrorCode &status) { michael@0: SHARED_DATA_HASHTABLE = uhash_open(hashEntry, compareEntries, NULL, &status); michael@0: if (U_FAILURE(status)) { michael@0: SHARED_DATA_HASHTABLE = NULL; michael@0: } michael@0: ucln_common_registerCleanup(UCLN_COMMON_USPREP, usprep_cleanup); michael@0: } michael@0: michael@0: static void michael@0: initCache(UErrorCode *status) { michael@0: umtx_initOnce(gSharedDataInitOnce, &createCache, *status); michael@0: } michael@0: michael@0: static UBool U_CALLCONV michael@0: loadData(UStringPrepProfile* profile, michael@0: const char* path, michael@0: const char* name, michael@0: const char* type, michael@0: UErrorCode* errorCode) { michael@0: /* load Unicode SPREP data from file */ michael@0: UTrie _sprepTrie={ 0,0,0,0,0,0,0 }; michael@0: UDataMemory *dataMemory; michael@0: const int32_t *p=NULL; michael@0: const uint8_t *pb; michael@0: UVersionInfo normUnicodeVersion; michael@0: int32_t normUniVer, sprepUniVer, normCorrVer; michael@0: michael@0: if(errorCode==NULL || U_FAILURE(*errorCode)) { michael@0: return 0; michael@0: } michael@0: michael@0: /* open the data outside the mutex block */ michael@0: //TODO: change the path michael@0: dataMemory=udata_openChoice(path, type, name, isSPrepAcceptable, NULL, errorCode); michael@0: if(U_FAILURE(*errorCode)) { michael@0: return FALSE; michael@0: } michael@0: michael@0: p=(const int32_t *)udata_getMemory(dataMemory); michael@0: pb=(const uint8_t *)(p+_SPREP_INDEX_TOP); michael@0: utrie_unserialize(&_sprepTrie, pb, p[_SPREP_INDEX_TRIE_SIZE], errorCode); michael@0: _sprepTrie.getFoldingOffset=getSPrepFoldingOffset; michael@0: michael@0: michael@0: if(U_FAILURE(*errorCode)) { michael@0: udata_close(dataMemory); michael@0: return FALSE; michael@0: } michael@0: michael@0: /* in the mutex block, set the data for this process */ michael@0: umtx_lock(&usprepMutex); michael@0: if(profile->sprepData==NULL) { michael@0: profile->sprepData=dataMemory; michael@0: dataMemory=NULL; michael@0: uprv_memcpy(&profile->indexes, p, sizeof(profile->indexes)); michael@0: uprv_memcpy(&profile->sprepTrie, &_sprepTrie, sizeof(UTrie)); michael@0: } else { michael@0: p=(const int32_t *)udata_getMemory(profile->sprepData); michael@0: } michael@0: umtx_unlock(&usprepMutex); michael@0: /* initialize some variables */ michael@0: profile->mappingData=(uint16_t *)((uint8_t *)(p+_SPREP_INDEX_TOP)+profile->indexes[_SPREP_INDEX_TRIE_SIZE]); michael@0: michael@0: u_getUnicodeVersion(normUnicodeVersion); michael@0: normUniVer = (normUnicodeVersion[0] << 24) + (normUnicodeVersion[1] << 16) + michael@0: (normUnicodeVersion[2] << 8 ) + (normUnicodeVersion[3]); michael@0: sprepUniVer = (dataVersion[0] << 24) + (dataVersion[1] << 16) + michael@0: (dataVersion[2] << 8 ) + (dataVersion[3]); michael@0: normCorrVer = profile->indexes[_SPREP_NORM_CORRECTNS_LAST_UNI_VERSION]; michael@0: michael@0: if(U_FAILURE(*errorCode)){ michael@0: udata_close(dataMemory); michael@0: return FALSE; michael@0: } michael@0: if( normUniVer < sprepUniVer && /* the Unicode version of SPREP file must be less than the Unicode Vesion of the normalization data */ michael@0: normUniVer < normCorrVer && /* the Unicode version of the NormalizationCorrections.txt file should be less than the Unicode Vesion of the normalization data */ michael@0: ((profile->indexes[_SPREP_OPTIONS] & _SPREP_NORMALIZATION_ON) > 0) /* normalization turned on*/ michael@0: ){ michael@0: *errorCode = U_INVALID_FORMAT_ERROR; michael@0: udata_close(dataMemory); michael@0: return FALSE; michael@0: } michael@0: profile->isDataLoaded = TRUE; michael@0: michael@0: /* if a different thread set it first, then close the extra data */ michael@0: if(dataMemory!=NULL) { michael@0: udata_close(dataMemory); /* NULL if it was set correctly */ michael@0: } michael@0: michael@0: michael@0: return profile->isDataLoaded; michael@0: } michael@0: michael@0: static UStringPrepProfile* michael@0: usprep_getProfile(const char* path, michael@0: const char* name, michael@0: UErrorCode *status){ michael@0: michael@0: UStringPrepProfile* profile = NULL; michael@0: michael@0: initCache(status); michael@0: michael@0: if(U_FAILURE(*status)){ michael@0: return NULL; michael@0: } michael@0: michael@0: UStringPrepKey stackKey; michael@0: /* michael@0: * const is cast way to save malloc, strcpy and free calls michael@0: * we use the passed in pointers for fetching the data from the michael@0: * hash table which is safe michael@0: */ michael@0: stackKey.name = (char*) name; michael@0: stackKey.path = (char*) path; michael@0: michael@0: /* fetch the data from the cache */ michael@0: umtx_lock(&usprepMutex); michael@0: profile = (UStringPrepProfile*) (uhash_get(SHARED_DATA_HASHTABLE,&stackKey)); michael@0: if(profile != NULL) { michael@0: profile->refCount++; michael@0: } michael@0: umtx_unlock(&usprepMutex); michael@0: michael@0: if(profile == NULL) { michael@0: /* else load the data and put the data in the cache */ michael@0: LocalMemory newProfile; michael@0: if(newProfile.allocateInsteadAndReset() == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: /* load the data */ michael@0: if(!loadData(newProfile.getAlias(), path, name, _SPREP_DATA_TYPE, status) || U_FAILURE(*status) ){ michael@0: return NULL; michael@0: } michael@0: michael@0: /* get the options */ michael@0: newProfile->doNFKC = (UBool)((newProfile->indexes[_SPREP_OPTIONS] & _SPREP_NORMALIZATION_ON) > 0); michael@0: newProfile->checkBiDi = (UBool)((newProfile->indexes[_SPREP_OPTIONS] & _SPREP_CHECK_BIDI_ON) > 0); michael@0: michael@0: if(newProfile->checkBiDi) { michael@0: newProfile->bdp = ubidi_getSingleton(); michael@0: } michael@0: michael@0: LocalMemory key; michael@0: LocalMemory keyName; michael@0: LocalMemory keyPath; michael@0: if( key.allocateInsteadAndReset() == NULL || michael@0: keyName.allocateInsteadAndCopy(uprv_strlen(name)+1) == NULL || michael@0: (path != NULL && michael@0: keyPath.allocateInsteadAndCopy(uprv_strlen(path)+1) == NULL) michael@0: ) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: usprep_unload(newProfile.getAlias()); michael@0: return NULL; michael@0: } michael@0: michael@0: umtx_lock(&usprepMutex); michael@0: // If another thread already inserted the same key/value, refcount and cleanup our thread data michael@0: profile = (UStringPrepProfile*) (uhash_get(SHARED_DATA_HASHTABLE,&stackKey)); michael@0: if(profile != NULL) { michael@0: profile->refCount++; michael@0: usprep_unload(newProfile.getAlias()); michael@0: } michael@0: else { michael@0: /* initialize the key members */ michael@0: key->name = keyName.orphan(); michael@0: uprv_strcpy(key->name, name); michael@0: if(path != NULL){ michael@0: key->path = keyPath.orphan(); michael@0: uprv_strcpy(key->path, path); michael@0: } michael@0: profile = newProfile.orphan(); michael@0: michael@0: /* add the data object to the cache */ michael@0: profile->refCount = 1; michael@0: uhash_put(SHARED_DATA_HASHTABLE, key.orphan(), profile, status); michael@0: } michael@0: umtx_unlock(&usprepMutex); michael@0: } michael@0: michael@0: return profile; michael@0: } michael@0: michael@0: U_CAPI UStringPrepProfile* U_EXPORT2 michael@0: usprep_open(const char* path, michael@0: const char* name, michael@0: UErrorCode* status){ michael@0: michael@0: if(status == NULL || U_FAILURE(*status)){ michael@0: return NULL; michael@0: } michael@0: michael@0: /* initialize the profile struct members */ michael@0: return usprep_getProfile(path,name,status); michael@0: } michael@0: michael@0: U_CAPI UStringPrepProfile* U_EXPORT2 michael@0: usprep_openByType(UStringPrepProfileType type, michael@0: UErrorCode* status) { michael@0: if(status == NULL || U_FAILURE(*status)){ michael@0: return NULL; michael@0: } michael@0: int32_t index = (int32_t)type; michael@0: if (index < 0 || index >= (int32_t)(sizeof(PROFILE_NAMES)/sizeof(PROFILE_NAMES[0]))) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: return usprep_open(NULL, PROFILE_NAMES[index], status); michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: usprep_close(UStringPrepProfile* profile){ michael@0: if(profile==NULL){ michael@0: return; michael@0: } michael@0: michael@0: umtx_lock(&usprepMutex); michael@0: /* decrement the ref count*/ michael@0: if(profile->refCount > 0){ michael@0: profile->refCount--; michael@0: } michael@0: umtx_unlock(&usprepMutex); michael@0: michael@0: } michael@0: michael@0: U_CFUNC void michael@0: uprv_syntaxError(const UChar* rules, michael@0: int32_t pos, michael@0: int32_t rulesLen, michael@0: UParseError* parseError){ michael@0: if(parseError == NULL){ michael@0: return; michael@0: } michael@0: parseError->offset = pos; michael@0: parseError->line = 0 ; // we are not using line numbers michael@0: michael@0: // for pre-context michael@0: int32_t start = (pos < U_PARSE_CONTEXT_LEN)? 0 : (pos - (U_PARSE_CONTEXT_LEN-1)); michael@0: int32_t limit = pos; michael@0: michael@0: u_memcpy(parseError->preContext,rules+start,limit-start); michael@0: //null terminate the buffer michael@0: parseError->preContext[limit-start] = 0; michael@0: michael@0: // for post-context; include error rules[pos] michael@0: start = pos; michael@0: limit = start + (U_PARSE_CONTEXT_LEN-1); michael@0: if (limit > rulesLen) { michael@0: limit = rulesLen; michael@0: } michael@0: if (start < rulesLen) { michael@0: u_memcpy(parseError->postContext,rules+start,limit-start); michael@0: } michael@0: //null terminate the buffer michael@0: parseError->postContext[limit-start]= 0; michael@0: } michael@0: michael@0: michael@0: static inline UStringPrepType michael@0: getValues(uint16_t trieWord, int16_t& value, UBool& isIndex){ michael@0: michael@0: UStringPrepType type; michael@0: if(trieWord == 0){ michael@0: /* michael@0: * Initial value stored in the mapping table michael@0: * just return USPREP_TYPE_LIMIT .. so that michael@0: * the source codepoint is copied to the destination michael@0: */ michael@0: type = USPREP_TYPE_LIMIT; michael@0: isIndex =FALSE; michael@0: value = 0; michael@0: }else if(trieWord >= _SPREP_TYPE_THRESHOLD){ michael@0: type = (UStringPrepType) (trieWord - _SPREP_TYPE_THRESHOLD); michael@0: isIndex =FALSE; michael@0: value = 0; michael@0: }else{ michael@0: /* get the type */ michael@0: type = USPREP_MAP; michael@0: /* ascertain if the value is index or delta */ michael@0: if(trieWord & 0x02){ michael@0: isIndex = TRUE; michael@0: value = trieWord >> 2; //mask off the lower 2 bits and shift michael@0: }else{ michael@0: isIndex = FALSE; michael@0: value = (int16_t)trieWord; michael@0: value = (value >> 2); michael@0: } michael@0: michael@0: if((trieWord>>2) == _SPREP_MAX_INDEX_VALUE){ michael@0: type = USPREP_DELETE; michael@0: isIndex =FALSE; michael@0: value = 0; michael@0: } michael@0: } michael@0: return type; michael@0: } michael@0: michael@0: michael@0: michael@0: static int32_t michael@0: usprep_map( const UStringPrepProfile* profile, michael@0: const UChar* src, int32_t srcLength, michael@0: UChar* dest, int32_t destCapacity, michael@0: int32_t options, michael@0: UParseError* parseError, michael@0: UErrorCode* status ){ michael@0: michael@0: uint16_t result; michael@0: int32_t destIndex=0; michael@0: int32_t srcIndex; michael@0: UBool allowUnassigned = (UBool) ((options & USPREP_ALLOW_UNASSIGNED)>0); michael@0: UStringPrepType type; michael@0: int16_t value; michael@0: UBool isIndex; michael@0: const int32_t* indexes = profile->indexes; michael@0: michael@0: // no error checking the caller check for error and arguments michael@0: // no string length check the caller finds out the string length michael@0: michael@0: for(srcIndex=0;srcIndexsprepTrie,ch,result); michael@0: michael@0: type = getValues(result, value, isIndex); michael@0: michael@0: // check if the source codepoint is unassigned michael@0: if(type == USPREP_UNASSIGNED && allowUnassigned == FALSE){ michael@0: michael@0: uprv_syntaxError(src,srcIndex-U16_LENGTH(ch), srcLength,parseError); michael@0: *status = U_STRINGPREP_UNASSIGNED_ERROR; michael@0: return 0; michael@0: michael@0: }else if(type == USPREP_MAP){ michael@0: michael@0: int32_t index, length; michael@0: michael@0: if(isIndex){ michael@0: index = value; michael@0: if(index >= indexes[_SPREP_ONE_UCHAR_MAPPING_INDEX_START] && michael@0: index < indexes[_SPREP_TWO_UCHARS_MAPPING_INDEX_START]){ michael@0: length = 1; michael@0: }else if(index >= indexes[_SPREP_TWO_UCHARS_MAPPING_INDEX_START] && michael@0: index < indexes[_SPREP_THREE_UCHARS_MAPPING_INDEX_START]){ michael@0: length = 2; michael@0: }else if(index >= indexes[_SPREP_THREE_UCHARS_MAPPING_INDEX_START] && michael@0: index < indexes[_SPREP_FOUR_UCHARS_MAPPING_INDEX_START]){ michael@0: length = 3; michael@0: }else{ michael@0: length = profile->mappingData[index++]; michael@0: michael@0: } michael@0: michael@0: /* copy mapping to destination */ michael@0: for(int32_t i=0; i< length; i++){ michael@0: if(destIndex < destCapacity ){ michael@0: dest[destIndex] = profile->mappingData[index+i]; michael@0: } michael@0: destIndex++; /* for pre-flighting */ michael@0: } michael@0: continue; michael@0: }else{ michael@0: // subtract the delta to arrive at the code point michael@0: ch -= value; michael@0: } michael@0: michael@0: }else if(type==USPREP_DELETE){ michael@0: // just consume the codepoint and contine michael@0: continue; michael@0: } michael@0: //copy the code point into destination michael@0: if(ch <= 0xFFFF){ michael@0: if(destIndex < destCapacity ){ michael@0: dest[destIndex] = (UChar)ch; michael@0: } michael@0: destIndex++; michael@0: }else{ michael@0: if(destIndex+1 < destCapacity ){ michael@0: dest[destIndex] = U16_LEAD(ch); michael@0: dest[destIndex+1] = U16_TRAIL(ch); michael@0: } michael@0: destIndex +=2; michael@0: } michael@0: michael@0: } michael@0: michael@0: return u_terminateUChars(dest, destCapacity, destIndex, status); michael@0: } michael@0: michael@0: michael@0: static int32_t michael@0: usprep_normalize( const UChar* src, int32_t srcLength, michael@0: UChar* dest, int32_t destCapacity, michael@0: UErrorCode* status ){ michael@0: return unorm_normalize( michael@0: src, srcLength, michael@0: UNORM_NFKC, UNORM_UNICODE_3_2, michael@0: dest, destCapacity, michael@0: status); michael@0: } michael@0: michael@0: michael@0: /* michael@0: 1) Map -- For each character in the input, check if it has a mapping michael@0: and, if so, replace it with its mapping. michael@0: michael@0: 2) Normalize -- Possibly normalize the result of step 1 using Unicode michael@0: normalization. michael@0: michael@0: 3) Prohibit -- Check for any characters that are not allowed in the michael@0: output. If any are found, return an error. michael@0: michael@0: 4) Check bidi -- Possibly check for right-to-left characters, and if michael@0: any are found, make sure that the whole string satisfies the michael@0: requirements for bidirectional strings. If the string does not michael@0: satisfy the requirements for bidirectional strings, return an michael@0: error. michael@0: [Unicode3.2] defines several bidirectional categories; each character michael@0: has one bidirectional category assigned to it. For the purposes of michael@0: the requirements below, an "RandALCat character" is a character that michael@0: has Unicode bidirectional categories "R" or "AL"; an "LCat character" michael@0: is a character that has Unicode bidirectional category "L". Note michael@0: michael@0: michael@0: that there are many characters which fall in neither of the above michael@0: definitions; Latin digits ( through ) are examples of michael@0: this because they have bidirectional category "EN". michael@0: michael@0: In any profile that specifies bidirectional character handling, all michael@0: three of the following requirements MUST be met: michael@0: michael@0: 1) The characters in section 5.8 MUST be prohibited. michael@0: michael@0: 2) If a string contains any RandALCat character, the string MUST NOT michael@0: contain any LCat character. michael@0: michael@0: 3) If a string contains any RandALCat character, a RandALCat michael@0: character MUST be the first character of the string, and a michael@0: RandALCat character MUST be the last character of the string. michael@0: */ michael@0: michael@0: #define MAX_STACK_BUFFER_SIZE 300 michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: usprep_prepare( const UStringPrepProfile* profile, michael@0: const UChar* src, int32_t srcLength, michael@0: UChar* dest, int32_t destCapacity, michael@0: int32_t options, michael@0: UParseError* parseError, michael@0: UErrorCode* status ){ michael@0: michael@0: // check error status michael@0: if(status == NULL || U_FAILURE(*status)){ michael@0: return 0; michael@0: } michael@0: michael@0: //check arguments michael@0: if(profile==NULL || src==NULL || srcLength<-1 || (dest==NULL && destCapacity!=0)) { michael@0: *status=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: UChar b1Stack[MAX_STACK_BUFFER_SIZE], b2Stack[MAX_STACK_BUFFER_SIZE]; michael@0: UChar *b1 = b1Stack, *b2 = b2Stack; michael@0: int32_t b1Len, b2Len=0, michael@0: b1Capacity = MAX_STACK_BUFFER_SIZE , michael@0: b2Capacity = MAX_STACK_BUFFER_SIZE; michael@0: uint16_t result; michael@0: int32_t b2Index = 0; michael@0: UCharDirection direction=U_CHAR_DIRECTION_COUNT, firstCharDir=U_CHAR_DIRECTION_COUNT; michael@0: UBool leftToRight=FALSE, rightToLeft=FALSE; michael@0: int32_t rtlPos =-1, ltrPos =-1; michael@0: michael@0: //get the string length michael@0: if(srcLength == -1){ michael@0: srcLength = u_strlen(src); michael@0: } michael@0: // map michael@0: b1Len = usprep_map(profile, src, srcLength, b1, b1Capacity, options, parseError, status); michael@0: michael@0: if(*status == U_BUFFER_OVERFLOW_ERROR){ michael@0: // redo processing of string michael@0: /* we do not have enough room so grow the buffer*/ michael@0: b1 = (UChar*) uprv_malloc(b1Len * U_SIZEOF_UCHAR); michael@0: if(b1==NULL){ michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto CLEANUP; michael@0: } michael@0: michael@0: *status = U_ZERO_ERROR; // reset error michael@0: michael@0: b1Len = usprep_map(profile, src, srcLength, b1, b1Len, options, parseError, status); michael@0: michael@0: } michael@0: michael@0: // normalize michael@0: if(profile->doNFKC == TRUE){ michael@0: b2Len = usprep_normalize(b1,b1Len, b2,b2Capacity,status); michael@0: michael@0: if(*status == U_BUFFER_OVERFLOW_ERROR){ michael@0: // redo processing of string michael@0: /* we do not have enough room so grow the buffer*/ michael@0: b2 = (UChar*) uprv_malloc(b2Len * U_SIZEOF_UCHAR); michael@0: if(b2==NULL){ michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto CLEANUP; michael@0: } michael@0: michael@0: *status = U_ZERO_ERROR; // reset error michael@0: michael@0: b2Len = usprep_normalize(b1,b1Len, b2,b2Len,status); michael@0: michael@0: } michael@0: michael@0: }else{ michael@0: b2 = b1; michael@0: b2Len = b1Len; michael@0: } michael@0: michael@0: michael@0: if(U_FAILURE(*status)){ michael@0: goto CLEANUP; michael@0: } michael@0: michael@0: UChar32 ch; michael@0: UStringPrepType type; michael@0: int16_t value; michael@0: UBool isIndex; michael@0: michael@0: // Prohibit and checkBiDi in one pass michael@0: for(b2Index=0; b2IndexsprepTrie,ch,result); michael@0: michael@0: type = getValues(result, value, isIndex); michael@0: michael@0: if( type == USPREP_PROHIBITED || michael@0: ((result < _SPREP_TYPE_THRESHOLD) && (result & 0x01) /* first bit says it the code point is prohibited*/) michael@0: ){ michael@0: *status = U_STRINGPREP_PROHIBITED_ERROR; michael@0: uprv_syntaxError(b1, b2Index-U16_LENGTH(ch), b2Len, parseError); michael@0: goto CLEANUP; michael@0: } michael@0: michael@0: if(profile->checkBiDi) { michael@0: direction = ubidi_getClass(profile->bdp, ch); michael@0: if(firstCharDir == U_CHAR_DIRECTION_COUNT){ michael@0: firstCharDir = direction; michael@0: } michael@0: if(direction == U_LEFT_TO_RIGHT){ michael@0: leftToRight = TRUE; michael@0: ltrPos = b2Index-1; michael@0: } michael@0: if(direction == U_RIGHT_TO_LEFT || direction == U_RIGHT_TO_LEFT_ARABIC){ michael@0: rightToLeft = TRUE; michael@0: rtlPos = b2Index-1; michael@0: } michael@0: } michael@0: } michael@0: if(profile->checkBiDi == TRUE){ michael@0: // satisfy 2 michael@0: if( leftToRight == TRUE && rightToLeft == TRUE){ michael@0: *status = U_STRINGPREP_CHECK_BIDI_ERROR; michael@0: uprv_syntaxError(b2,(rtlPos>ltrPos) ? rtlPos : ltrPos, b2Len, parseError); michael@0: goto CLEANUP; michael@0: } michael@0: michael@0: //satisfy 3 michael@0: if( rightToLeft == TRUE && michael@0: !((firstCharDir == U_RIGHT_TO_LEFT || firstCharDir == U_RIGHT_TO_LEFT_ARABIC) && michael@0: (direction == U_RIGHT_TO_LEFT || direction == U_RIGHT_TO_LEFT_ARABIC)) michael@0: ){ michael@0: *status = U_STRINGPREP_CHECK_BIDI_ERROR; michael@0: uprv_syntaxError(b2, rtlPos, b2Len, parseError); michael@0: return FALSE; michael@0: } michael@0: } michael@0: if(b2Len>0 && b2Len <= destCapacity){ michael@0: uprv_memmove(dest,b2, b2Len*U_SIZEOF_UCHAR); michael@0: } michael@0: michael@0: CLEANUP: michael@0: if(b1!=b1Stack){ michael@0: uprv_free(b1); michael@0: b1=NULL; michael@0: } michael@0: michael@0: if(b2!=b1Stack && b2!=b2Stack && b2!=b1 /* b1 should not be freed twice */){ michael@0: uprv_free(b2); michael@0: b2=NULL; michael@0: } michael@0: return u_terminateUChars(dest, destCapacity, b2Len, status); michael@0: } michael@0: michael@0: michael@0: /* data swapping ------------------------------------------------------------ */ michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: usprep_swap(const UDataSwapper *ds, michael@0: const void *inData, int32_t length, void *outData, michael@0: UErrorCode *pErrorCode) { michael@0: const UDataInfo *pInfo; michael@0: int32_t headerSize; michael@0: michael@0: const uint8_t *inBytes; michael@0: uint8_t *outBytes; michael@0: michael@0: const int32_t *inIndexes; michael@0: int32_t indexes[16]; michael@0: michael@0: int32_t i, offset, count, size; michael@0: michael@0: /* udata_swapDataHeader checks the arguments */ michael@0: headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode); michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: michael@0: /* check data format and format version */ michael@0: pInfo=(const UDataInfo *)((const char *)inData+4); michael@0: if(!( michael@0: pInfo->dataFormat[0]==0x53 && /* dataFormat="SPRP" */ michael@0: pInfo->dataFormat[1]==0x50 && michael@0: pInfo->dataFormat[2]==0x52 && michael@0: pInfo->dataFormat[3]==0x50 && michael@0: pInfo->formatVersion[0]==3 michael@0: )) { michael@0: udata_printError(ds, "usprep_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as StringPrep .spp data\n", michael@0: pInfo->dataFormat[0], pInfo->dataFormat[1], michael@0: pInfo->dataFormat[2], pInfo->dataFormat[3], michael@0: pInfo->formatVersion[0]); michael@0: *pErrorCode=U_UNSUPPORTED_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: inBytes=(const uint8_t *)inData+headerSize; michael@0: outBytes=(uint8_t *)outData+headerSize; michael@0: michael@0: inIndexes=(const int32_t *)inBytes; michael@0: michael@0: if(length>=0) { michael@0: length-=headerSize; michael@0: if(length<16*4) { michael@0: udata_printError(ds, "usprep_swap(): too few bytes (%d after header) for StringPrep .spp data\n", michael@0: length); michael@0: *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: /* read the first 16 indexes (ICU 2.8/format version 3: _SPREP_INDEX_TOP==16, might grow) */ michael@0: for(i=0; i<16; ++i) { michael@0: indexes[i]=udata_readInt32(ds, inIndexes[i]); michael@0: } michael@0: michael@0: /* calculate the total length of the data */ michael@0: size= michael@0: 16*4+ /* size of indexes[] */ michael@0: indexes[_SPREP_INDEX_TRIE_SIZE]+ michael@0: indexes[_SPREP_INDEX_MAPPING_DATA_SIZE]; michael@0: michael@0: if(length>=0) { michael@0: if(lengthswapArray32(ds, inBytes, count, outBytes, pErrorCode); michael@0: offset+=count; michael@0: michael@0: /* swap the UTrie */ michael@0: count=indexes[_SPREP_INDEX_TRIE_SIZE]; michael@0: utrie_swap(ds, inBytes+offset, count, outBytes+offset, pErrorCode); michael@0: offset+=count; michael@0: michael@0: /* swap the uint16_t mappingTable[] */ michael@0: count=indexes[_SPREP_INDEX_MAPPING_DATA_SIZE]; michael@0: ds->swapArray16(ds, inBytes+offset, count, outBytes+offset, pErrorCode); michael@0: offset+=count; michael@0: } michael@0: michael@0: return headerSize+size; michael@0: } michael@0: michael@0: #endif /* #if !UCONFIG_NO_IDNA */