U_CDECL_BEGIN static void U_CALLCONV _HZOpen(UConverter *cnv, UConverterLoadArgs *pArgs, UErrorCode *errorCode){ UConverter *gbConverter; if(pArgs->onlyTestIsLoadable) { ucnv_canCreateConverter("GBK", errorCode); /* errorCode carries result */ return; } gbConverter = ucnv_open("GBK", errorCode); if(U_FAILURE(*errorCode)) { return; } cnv->toUnicodeStatus = 0; cnv->fromUnicodeStatus= 0; cnv->mode=0; cnv->fromUChar32=0x0000; cnv->extraInfo = uprv_calloc(1, sizeof(UConverterDataHZ)); if(cnv->extraInfo != NULL){ ((UConverterDataHZ*)cnv->extraInfo)->gbConverter = gbConverter; } else { ucnv_close(gbConverter); *errorCode = U_MEMORY_ALLOCATION_ERROR; return; } }
extern void init() { sprepTrie = (UNewTrie *)uprv_calloc(1, sizeof(UNewTrie)); /* initialize the two tries */ if(NULL==utrie_open(sprepTrie, NULL, MAX_DATA_LENGTH, 0, 0, FALSE)) { fprintf(stderr, "error: failed to initialize tries\n"); exit(U_MEMORY_ALLOCATION_ERROR); } }
U_DRAFT UBiDiTransform* U_EXPORT2 ubiditransform_open(UErrorCode *pErrorCode) { UBiDiTransform *pBiDiTransform = NULL; if (U_SUCCESS(*pErrorCode)) { pBiDiTransform = (UBiDiTransform*) uprv_calloc(1, sizeof(UBiDiTransform)); if (pBiDiTransform == NULL) { *pErrorCode = U_MEMORY_ALLOCATION_ERROR; } } return pBiDiTransform; }
/** * Main Windows time zone detection function. Returns the Windows * time zone, translated to an ICU time zone, or NULL upon failure. */ U_CFUNC const char* U_EXPORT2 uprv_detectWindowsTimeZone() { UErrorCode status = U_ZERO_ERROR; UResourceBundle* bundle = NULL; char* icuid = NULL; char apiStdName[MAX_LENGTH_ID]; char regStdName[MAX_LENGTH_ID]; char tmpid[MAX_LENGTH_ID]; int32_t len; int id; int errorCode; UChar ISOcodeW[3]; /* 2 letter iso code in UTF-16*/ char ISOcodeA[3]; /* 2 letter iso code in ansi */ LONG result; TZI tziKey; TZI tziReg; TIME_ZONE_INFORMATION apiTZI; BOOL isVistaOrHigher; BOOL tryPreVistaFallback; OSVERSIONINFO osVerInfo; /* Obtain TIME_ZONE_INFORMATION from the API, and then convert it to TZI. We could also interrogate the registry directly; we do this below if needed. */ uprv_memset(&apiTZI, 0, sizeof(apiTZI)); uprv_memset(&tziKey, 0, sizeof(tziKey)); uprv_memset(&tziReg, 0, sizeof(tziReg)); GetTimeZoneInformation(&apiTZI); tziKey.bias = apiTZI.Bias; uprv_memcpy((char *)&tziKey.standardDate, (char*)&apiTZI.StandardDate, sizeof(apiTZI.StandardDate)); uprv_memcpy((char *)&tziKey.daylightDate, (char*)&apiTZI.DaylightDate, sizeof(apiTZI.DaylightDate)); /* Convert the wchar_t* standard name to char* */ uprv_memset(apiStdName, 0, sizeof(apiStdName)); wcstombs(apiStdName, apiTZI.StandardName, MAX_LENGTH_ID); tmpid[0] = 0; id = GetUserGeoID(GEOCLASS_NATION); errorCode = GetGeoInfoW(id,GEO_ISO2,ISOcodeW,3,0); u_strToUTF8(ISOcodeA, 3, NULL, ISOcodeW, 3, &status); bundle = ures_openDirect(NULL, "windowsZones", &status); ures_getByKey(bundle, "mapTimezones", bundle, &status); /* Windows Vista+ provides us with a "TimeZoneKeyName" that is not localized and can be used to directly map a name in our bundle. Try to use that first if we're on Vista or higher */ uprv_memset(&osVerInfo, 0, sizeof(osVerInfo)); osVerInfo.dwOSVersionInfoSize = sizeof(osVerInfo); GetVersionEx(&osVerInfo); isVistaOrHigher = osVerInfo.dwMajorVersion >= 6; /* actually includes Windows Server 2008 as well, but don't worry about it */ tryPreVistaFallback = TRUE; if(isVistaOrHigher) { result = getTZKeyName(regStdName, sizeof(regStdName)); if(ERROR_SUCCESS == result) { UResourceBundle* winTZ = ures_getByKey(bundle, regStdName, NULL, &status); if(U_SUCCESS(status)) { const UChar* icuTZ = NULL; if (errorCode != 0) { icuTZ = ures_getStringByKey(winTZ, ISOcodeA, &len, &status); } if (errorCode==0 || icuTZ==NULL) { /* fallback to default "001" and reset status */ status = U_ZERO_ERROR; icuTZ = ures_getStringByKey(winTZ, "001", &len, &status); } if(U_SUCCESS(status)) { int index=0; while (! (*icuTZ == '\0' || *icuTZ ==' ')) { tmpid[index++]=(char)(*icuTZ++); /* safe to assume 'char' is ASCII compatible on windows */ } tmpid[index]='\0'; tryPreVistaFallback = FALSE; } } ures_close(winTZ); } } if(tryPreVistaFallback) { /* Note: We get the winid not from static tables but from resource bundle. */ while (U_SUCCESS(status) && ures_hasNext(bundle)) { UBool idFound = FALSE; const char* winid; UResourceBundle* winTZ = ures_getNextResource(bundle, NULL, &status); if (U_FAILURE(status)) { break; } winid = ures_getKey(winTZ); result = getTZI(winid, &tziReg); if (result == ERROR_SUCCESS) { /* Windows alters the DaylightBias in some situations. Using the bias and the rules suffices, so overwrite these unreliable fields. */ tziKey.standardBias = tziReg.standardBias; tziKey.daylightBias = tziReg.daylightBias; if (uprv_memcmp((char *)&tziKey, (char*)&tziReg, sizeof(tziKey)) == 0) { const UChar* icuTZ = NULL; if (errorCode != 0) { icuTZ = ures_getStringByKey(winTZ, ISOcodeA, &len, &status); } if (errorCode==0 || icuTZ==NULL) { /* fallback to default "001" and reset status */ status = U_ZERO_ERROR; icuTZ = ures_getStringByKey(winTZ, "001", &len, &status); } if (U_SUCCESS(status)) { /* Get the standard name from the registry key to compare with the one from Windows API call. */ uprv_memset(regStdName, 0, sizeof(regStdName)); result = getSTDName(winid, regStdName, sizeof(regStdName)); if (result == ERROR_SUCCESS) { if (uprv_strcmp(apiStdName, regStdName) == 0) { idFound = TRUE; } } /* tmpid buffer holds the ICU timezone ID corresponding to the timezone ID from Windows. * If none is found, tmpid buffer will contain a fallback ID (i.e. the time zone ID matching * the current time zone information) */ if (idFound || tmpid[0] == 0) { /* if icuTZ has more than one city, take only the first (i.e. terminate icuTZ at first space) */ int index=0; while (! (*icuTZ == '\0' || *icuTZ ==' ')) { tmpid[index++]=(char)(*icuTZ++); /* safe to assume 'char' is ASCII compatible on windows */ } tmpid[index]='\0'; } } } } ures_close(winTZ); if (idFound) { break; } } } /* * Copy the timezone ID to icuid to be returned. */ if (tmpid[0] != 0) { len = uprv_strlen(tmpid); icuid = (char*)uprv_calloc(len + 1, sizeof(char)); if (icuid != NULL) { uprv_strcpy(icuid, tmpid); } } ures_close(bundle); return icuid; }
/** * Main Windows time zone detection function. Returns the Windows * time zone, translated to an ICU time zone, or NULL upon failure. */ U_CFUNC const char* U_EXPORT2 uprv_detectWindowsTimeZone() { UErrorCode status = U_ZERO_ERROR; UResourceBundle* bundle = NULL; char* icuid = NULL; UChar apiStd[MAX_LENGTH_ID]; char apiStdName[MAX_LENGTH_ID]; char regStdName[MAX_LENGTH_ID]; char tmpid[MAX_LENGTH_ID]; int32_t len; int id; int errorCode; char ISOcode[3]; /* 2 letter iso code */ LONG result; TZI tziKey; TZI tziReg; TIME_ZONE_INFORMATION apiTZI; /* Obtain TIME_ZONE_INFORMATION from the API, and then convert it to TZI. We could also interrogate the registry directly; we do this below if needed. */ uprv_memset(&apiTZI, 0, sizeof(apiTZI)); uprv_memset(&tziKey, 0, sizeof(tziKey)); uprv_memset(&tziReg, 0, sizeof(tziReg)); GetTimeZoneInformation(&apiTZI); tziKey.bias = apiTZI.Bias; uprv_memcpy((char *)&tziKey.standardDate, (char*)&apiTZI.StandardDate, sizeof(apiTZI.StandardDate)); uprv_memcpy((char *)&tziKey.daylightDate, (char*)&apiTZI.DaylightDate, sizeof(apiTZI.DaylightDate)); /* Convert the wchar_t* standard name to char* */ uprv_memset(apiStdName, 0, sizeof(apiStdName)); u_strFromWCS(apiStd, MAX_LENGTH_ID, NULL, apiTZI.StandardName, -1, &status); u_austrncpy(apiStdName, apiStd, sizeof(apiStdName) - 1); tmpid[0] = 0; id = GetUserGeoID(GEOCLASS_NATION); errorCode = GetGeoInfoA(id,GEO_ISO2,ISOcode,3,0); bundle = ures_openDirect(NULL, "windowsZones", &status); ures_getByKey(bundle, "mapTimezones", bundle, &status); /* Note: We get the winid not from static tables but from resource bundle. */ while (U_SUCCESS(status) && ures_hasNext(bundle)) { UBool idFound = FALSE; const char* winid; UResourceBundle* winTZ = ures_getNextResource(bundle, NULL, &status); if (U_FAILURE(status)) { break; } winid = ures_getKey(winTZ); result = getTZI(winid, &tziReg); if (result == ERROR_SUCCESS) { /* Windows alters the DaylightBias in some situations. Using the bias and the rules suffices, so overwrite these unreliable fields. */ tziKey.standardBias = tziReg.standardBias; tziKey.daylightBias = tziReg.daylightBias; if (uprv_memcmp((char *)&tziKey, (char*)&tziReg, sizeof(tziKey)) == 0) { const UChar* icuTZ = NULL; if (errorCode != 0) { icuTZ = ures_getStringByKey(winTZ, ISOcode, &len, &status); } if (errorCode==0 || icuTZ==NULL) { /* fallback to default "001" and reset status */ status = U_ZERO_ERROR; icuTZ = ures_getStringByKey(winTZ, "001", &len, &status); } if (U_SUCCESS(status)) { /* Get the standard name from the registry key to compare with the one from Windows API call. */ uprv_memset(regStdName, 0, sizeof(regStdName)); result = getSTDName(winid, regStdName, sizeof(regStdName)); if (result == ERROR_SUCCESS) { if (uprv_strcmp(apiStdName, regStdName) == 0) { idFound = TRUE; } } /* tmpid buffer holds the ICU timezone ID corresponding to the timezone ID from Windows. * If none is found, tmpid buffer will contain a fallback ID (i.e. the time zone ID matching * the current time zone information) */ if (idFound || tmpid[0] == 0) { /* if icuTZ has more than one city, take only the first (i.e. terminate icuTZ at first space) */ int index=0; while (! (*icuTZ == '\0' || *icuTZ ==' ')) { tmpid[index++]=(char)(*icuTZ++); /* safe to assume 'char' is ASCII compatible on windows */ } tmpid[index]='\0'; } } } } ures_close(winTZ); if (idFound) { break; } } /* * Copy the timezone ID to icuid to be returned. */ if (tmpid[0] != 0) { len = uprv_strlen(tmpid); icuid = (char*)uprv_calloc(len + 1, sizeof(char)); if (icuid != NULL) { uprv_strcpy(icuid, tmpid); } } ures_close(bundle); return icuid; }
extern void storeMapping(uint32_t codepoint, uint32_t* mapping,int32_t length, UStringPrepType type, UErrorCode* status){ UChar* map = NULL; int16_t adjustedLen=0, i; uint16_t trieWord = 0; ValueStruct *value = NULL; uint32_t savedTrieWord = 0; /* initialize the hashtable */ if(hashTable==NULL){ hashTable = uhash_open(hashEntry, compareEntries, NULL, status); uhash_setValueDeleter(hashTable, valueDeleter); } /* figure out if the code point has type already stored */ savedTrieWord= utrie_get32(sprepTrie,codepoint,NULL); if(savedTrieWord!=0){ if((savedTrieWord- _SPREP_TYPE_THRESHOLD) == USPREP_PROHIBITED){ /* turn on the first bit in trie word */ trieWord += 0x01; }else{ /* * the codepoint has value something other than prohibited * and a mapping .. error! */ fprintf(stderr,"Type for codepoint \\U%08X already set!.\n", (int)codepoint); exit(U_ILLEGAL_ARGUMENT_ERROR); } } /* figure out the real length */ for(i=0; i<length; i++){ if(mapping[i] > 0xFFFF){ adjustedLen +=2; }else{ adjustedLen++; } } if(adjustedLen == 0){ trieWord = (uint16_t)(_SPREP_MAX_INDEX_VALUE << 2); /* make sure that the value of trieWord is less than the threshold */ if(trieWord < _SPREP_TYPE_THRESHOLD){ /* now set the value in the trie */ if(!utrie_set32(sprepTrie,codepoint,trieWord)){ fprintf(stderr,"Could not set the value for code point.\n"); exit(U_ILLEGAL_ARGUMENT_ERROR); } /* value is set so just return */ return; }else{ fprintf(stderr,"trieWord cannot contain value greater than threshold 0x%04X.\n",_SPREP_TYPE_THRESHOLD); exit(U_ILLEGAL_CHAR_FOUND); } } if(adjustedLen == 1){ /* calculate the delta */ int16_t delta = (int16_t)((int32_t)codepoint - (int16_t) mapping[0]); if(delta >= SPREP_DELTA_RANGE_NEGATIVE_LIMIT && delta <= SPREP_DELTA_RANGE_POSITIVE_LIMIT){ trieWord = delta << 2; /* make sure that the second bit is OFF */ if((trieWord & 0x02) != 0 ){ fprintf(stderr,"The second bit in the trie word is not zero while storing a delta.\n"); exit(U_INTERNAL_PROGRAM_ERROR); } /* make sure that the value of trieWord is less than the threshold */ if(trieWord < _SPREP_TYPE_THRESHOLD){ /* now set the value in the trie */ if(!utrie_set32(sprepTrie,codepoint,trieWord)){ fprintf(stderr,"Could not set the value for code point.\n"); exit(U_ILLEGAL_ARGUMENT_ERROR); } /* value is set so just return */ return; } } /* * if the delta is not in the given range or if the trieWord is larger than the threshold * just fall through for storing the mapping in the mapping table */ } map = (UChar*) uprv_calloc(adjustedLen + 1, U_SIZEOF_UCHAR); i=0; while(i<length){ if(mapping[i] <= 0xFFFF){ map[i] = (uint16_t)mapping[i]; }else{ map[i] = U16_LEAD(mapping[i]); map[i+1] = U16_TRAIL(mapping[i]); } i++; } value = (ValueStruct*) uprv_malloc(sizeof(ValueStruct)); value->mapping = map; value->type = type; value->length = adjustedLen; if(value->length > _SPREP_MAX_INDEX_TOP_LENGTH){ mappingDataCapacity++; } if(maxLength < value->length){ maxLength = value->length; } uhash_iput(hashTable,codepoint,value,status); mappingDataCapacity += adjustedLen; if(U_FAILURE(*status)){ fprintf(stderr, "Failed to put entries into the hastable. Error: %s\n", u_errorName(*status)); exit(*status); } }
static void storeMappingData(){ int32_t pos = -1; const UHashElement* element = NULL; ValueStruct* value = NULL; int32_t codepoint = 0; int32_t elementCount = 0; int32_t writtenElementCount = 0; int32_t mappingLength = 1; /* minimum mapping length */ int32_t oldMappingLength = 0; uint16_t trieWord =0; int32_t limitIndex = 0; if (hashTable == NULL) { return; } elementCount = uhash_count(hashTable); /*initialize the mapping data */ mappingData = (uint16_t*) uprv_calloc(mappingDataCapacity, U_SIZEOF_UCHAR); while(writtenElementCount < elementCount){ while( (element = uhash_nextElement(hashTable, &pos))!=NULL){ codepoint = element->key.integer; value = (ValueStruct*)element->value.pointer; /* store the start of indexes */ if(oldMappingLength != mappingLength){ /* Assume that index[] is used according to the enums defined */ if(oldMappingLength <=_SPREP_MAX_INDEX_TOP_LENGTH){ indexes[_SPREP_NORM_CORRECTNS_LAST_UNI_VERSION+mappingLength] = currentIndex; } if(oldMappingLength <= _SPREP_MAX_INDEX_TOP_LENGTH && mappingLength == _SPREP_MAX_INDEX_TOP_LENGTH +1){ limitIndex = currentIndex; } oldMappingLength = mappingLength; } if(value->length == mappingLength){ uint32_t savedTrieWord = 0; trieWord = currentIndex << 2; /* turn on the 2nd bit to signal that the following bits contain an index */ trieWord += 0x02; if(trieWord > _SPREP_TYPE_THRESHOLD){ fprintf(stderr,"trieWord cannot contain value greater than 0x%04X.\n",_SPREP_TYPE_THRESHOLD); exit(U_ILLEGAL_CHAR_FOUND); } /* figure out if the code point has type already stored */ savedTrieWord= utrie_get32(sprepTrie,codepoint,NULL); if(savedTrieWord!=0){ if((savedTrieWord- _SPREP_TYPE_THRESHOLD) == USPREP_PROHIBITED){ /* turn on the first bit in trie word */ trieWord += 0x01; }else{ /* * the codepoint has value something other than prohibited * and a mapping .. error! */ fprintf(stderr,"Type for codepoint \\U%08X already set!.\n", (int)codepoint); exit(U_ILLEGAL_ARGUMENT_ERROR); } } /* now set the value in the trie */ if(!utrie_set32(sprepTrie,codepoint,trieWord)){ fprintf(stderr,"Could not set the value for code point.\n"); exit(U_ILLEGAL_ARGUMENT_ERROR); } /* written the trie word for the codepoint... increment the count*/ writtenElementCount++; /* sanity check are we exceeding the max number allowed */ if(currentIndex+value->length+1 > _SPREP_MAX_INDEX_VALUE){ fprintf(stderr, "Too many entries in the mapping table %i. Maximum allowed is %i\n", currentIndex+value->length, _SPREP_MAX_INDEX_VALUE); exit(U_INDEX_OUTOFBOUNDS_ERROR); } /* copy the mapping data */ if(currentIndex+value->length+1 <= mappingDataCapacity){ /* write the length */ if(mappingLength > _SPREP_MAX_INDEX_TOP_LENGTH ){ /* the cast here is safe since we donot expect the length to be > 65535 */ mappingData[currentIndex++] = (uint16_t) mappingLength; } /* copy the contents to mappindData array */ uprv_memmove(mappingData+currentIndex, value->mapping, value->length*U_SIZEOF_UCHAR); currentIndex += value->length; }else{ /* realloc */ UChar* newMappingData = (uint16_t*) uprv_malloc(U_SIZEOF_UCHAR * mappingDataCapacity*2); if(newMappingData == NULL){ fprintf(stderr, "Could not realloc the mapping data!\n"); exit(U_MEMORY_ALLOCATION_ERROR); } uprv_memmove(newMappingData, mappingData, U_SIZEOF_UCHAR * mappingDataCapacity); mappingDataCapacity *= 2; uprv_free(mappingData); mappingData = newMappingData; /* write the length */ if(mappingLength > _SPREP_MAX_INDEX_TOP_LENGTH ){ /* the cast here is safe since we donot expect the length to be > 65535 */ mappingData[currentIndex++] = (uint16_t) mappingLength; } /* continue copying */ uprv_memmove(mappingData+currentIndex, value->mapping, value->length*U_SIZEOF_UCHAR); currentIndex += value->length; } } } mappingLength++; pos = -1; } /* set the last length for range check */ if(mappingLength <= _SPREP_MAX_INDEX_TOP_LENGTH){ indexes[_SPREP_NORM_CORRECTNS_LAST_UNI_VERSION+mappingLength] = currentIndex+1; }else{ indexes[_SPREP_FOUR_UCHARS_MAPPING_INDEX_START] = limitIndex; } }