/*
********************************************************************
* COPYRIGHT:
- * Copyright (c) 1996-2004, International Business Machines Corporation and
+ * Copyright (c) 1996-2010, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************
*
#if !UCONFIG_NO_CONVERSION
+#include "unicode/putil.h"
#include "unicode/udata.h"
#include "unicode/ucnv.h"
#include "unicode/uloc.h"
/* Note: the global mutex is used for */
/* reference count updates. */
+static const char **gAvailableConverters = NULL;
+static uint16_t gAvailableConverterCount = 0;
+
+#if !U_CHARSET_IS_UTF8
+
+/* This contains the resolved converter name. So no further alias lookup is needed again. */
+static char gDefaultConverterNameBuffer[UCNV_MAX_CONVERTER_NAME_LENGTH + 1]; /* +1 for NULL */
+static const char *gDefaultConverterName = NULL;
+
+/*
+If the default converter is an algorithmic converter, this is the cached value.
+We don't cache a full UConverter and clone it because ucnv_clone doesn't have
+less overhead than an algorithmic open. We don't cache non-algorithmic converters
+because ucnv_flushCache must be able to unload the default converter and its table.
+*/
+static const UConverterSharedData *gDefaultAlgorithmicSharedData = NULL;
+
+/* Does gDefaultConverterName have a converter option and require extra parsing? */
+static UBool gDefaultConverterContainsOption;
+
+#endif /* !U_CHARSET_IS_UTF8 */
static const char DATA_TYPE[] = "cnv";
-/* ucnv_cleanup - delete all storage held by the converter cache, except any in use */
-/* by open converters. */
-/* Not thread safe. */
-/* Not supported API. Marked U_CAPI only for use by test programs. */
+static void
+ucnv_flushAvailableConverterCache() {
+ if (gAvailableConverters) {
+ umtx_lock(&cnvCacheMutex);
+ gAvailableConverterCount = 0;
+ uprv_free((char **)gAvailableConverters);
+ gAvailableConverters = NULL;
+ umtx_unlock(&cnvCacheMutex);
+ }
+}
+
+/* ucnv_cleanup - delete all storage held by the converter cache, except any */
+/* in use by open converters. */
+/* Not thread safe. */
+/* Not supported API. */
static UBool U_CALLCONV ucnv_cleanup(void) {
- if (SHARED_DATA_HASHTABLE != NULL) {
- ucnv_flushCache();
- if (SHARED_DATA_HASHTABLE != NULL && uhash_count(SHARED_DATA_HASHTABLE) == 0) {
- uhash_close(SHARED_DATA_HASHTABLE);
- SHARED_DATA_HASHTABLE = NULL;
- }
+ ucnv_flushCache();
+ if (SHARED_DATA_HASHTABLE != NULL && uhash_count(SHARED_DATA_HASHTABLE) == 0) {
+ uhash_close(SHARED_DATA_HASHTABLE);
+ SHARED_DATA_HASHTABLE = NULL;
}
- umtx_destroy(&cnvCacheMutex); /* Don't worry about destroying the mutex even */
- /* if the hash table still exists. The mutex */
- /* will lazily re-init itself if needed. */
+ /* Isn't called from flushCache because other threads may have preexisting references to the table. */
+ ucnv_flushAvailableConverterCache();
+
+#if !U_CHARSET_IS_UTF8
+ gDefaultConverterName = NULL;
+ gDefaultConverterNameBuffer[0] = 0;
+ gDefaultConverterContainsOption = FALSE;
+ gDefaultAlgorithmicSharedData = NULL;
+#endif
+
+ umtx_destroy(&cnvCacheMutex); /* Don't worry about destroying the mutex even */
+ /* if the hash table still exists. The mutex */
+ /* will lazily re-init itself if needed. */
return (SHARED_DATA_HASHTABLE == NULL);
}
static UBool U_CALLCONV
isCnvAcceptable(void *context,
- const char *type, const char *name,
- const UDataInfo *pInfo) {
+ const char *type, const char *name,
+ const UDataInfo *pInfo) {
return (UBool)(
pInfo->size>=20 &&
pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
UTRACE_ENTRY_OC(UTRACE_UCNV_LOAD);
- if (err == NULL || U_FAILURE (*err)) {
+ if (U_FAILURE (*err)) {
UTRACE_EXIT_STATUS(*err);
return NULL;
}
return sharedData;
}
-int32_t
-ucnv_copyPlatformString(char *platformString, UConverterPlatform pltfrm)
-{
- switch (pltfrm)
- {
- case UCNV_IBM:
- uprv_strcpy(platformString, "ibm-");
- return 4;
- case UCNV_UNKNOWN:
- break;
- }
-
- /* default to empty string */
- *platformString = 0;
- return 0;
-}
-
/*returns a converter type from a string
*/
static const UConverterSharedData *
return NULL;
}
+/*
+* Based on the number of known converters, this determines how many times larger
+* the shared data hash table should be. When on small platforms, or just a couple
+* of converters are used, this number should be 2. When memory is plentiful, or
+* when ucnv_countAvailable is ever used with a lot of available converters,
+* this should be 4.
+* Larger numbers reduce the number of hash collisions, but use more memory.
+*/
+#define UCNV_CACHE_LOAD_FACTOR 2
+
/* Puts the shared data in the static hashtable SHARED_DATA_HASHTABLE */
/* Will always be called with the cnvCacheMutex alrady being held */
/* by the calling function. */
if (SHARED_DATA_HASHTABLE == NULL)
{
- SHARED_DATA_HASHTABLE = uhash_openSize(uhash_hashChars, uhash_compareChars,
- ucnv_io_countAvailableAliases(&err),
+ SHARED_DATA_HASHTABLE = uhash_openSize(uhash_hashChars, uhash_compareChars, NULL,
+ ucnv_io_countKnownConverters(&err)*UCNV_CACHE_LOAD_FACTOR,
&err);
ucln_common_registerCleanup(UCLN_COMMON_UCNV, ucnv_cleanup);
{
return NULL;
}
- else
+ else if (!pArgs->onlyTestIsLoadable)
{
/* share it with other library clients */
ucnv_shareConverterData(mySharedConverterData);
void
ucnv_incrementRefCount(UConverterSharedData *sharedData)
{
+ /*
+ Checking whether it's an algorithic converter is okay
+ in multithreaded applications because the value never changes.
+ Don't check referenceCounter for any other value.
+ */
if(sharedData != NULL && sharedData->referenceCounter != ~0) {
umtx_lock(&cnvCacheMutex);
sharedData->referenceCounter++;
}
}
+/*
+ * *pPieces must be initialized.
+ * The name without options will be copied to pPieces->cnvName.
+ * The locale and options will be copied to pPieces only if present in inName,
+ * otherwise the existing values in pPieces remain.
+ * *pArgs will be set to the pPieces values.
+ */
static void
parseConverterOptions(const char *inName,
- char *cnvName,
- char *locale,
- uint32_t *pFlags,
+ UConverterNamePieces *pPieces,
+ UConverterLoadArgs *pArgs,
UErrorCode *err)
{
+ char *cnvName = pPieces->cnvName;
char c;
int32_t len = 0;
+ pArgs->name=inName;
+ pArgs->locale=pPieces->locale;
+ pArgs->options=pPieces->options;
+
/* copy the converter name itself to cnvName */
while((c=*inName)!=0 && c!=UCNV_OPTION_SEP_CHAR) {
if (++len>=UCNV_MAX_CONVERTER_NAME_LENGTH) {
*err = U_ILLEGAL_ARGUMENT_ERROR; /* bad name */
- *cnvName=0;
+ pPieces->cnvName[0]=0;
return;
}
*cnvName++=c;
inName++;
}
*cnvName=0;
+ pArgs->name=pPieces->cnvName;
/* parse options. No more name copying should occur. */
while((c=*inName)!=0) {
/* inName is behind an option separator */
if(uprv_strncmp(inName, "locale=", 7)==0) {
/* do not modify locale itself in case we have multiple locale options */
- char *dest=locale;
+ char *dest=pPieces->locale;
/* copy the locale option value */
inName+=7;
if(++len>=ULOC_FULLNAME_CAPACITY) {
*err=U_ILLEGAL_ARGUMENT_ERROR; /* bad name */
- *locale=0;
+ pPieces->locale[0]=0;
return;
}
}
*dest=0;
} else if(uprv_strncmp(inName, "version=", 8)==0) {
- /* copy the version option value into bits 3..0 of *pFlags */
+ /* copy the version option value into bits 3..0 of pPieces->options */
inName+=8;
c=*inName;
if(c==0) {
- *pFlags&=~UCNV_OPTION_VERSION;
+ pArgs->options=(pPieces->options&=~UCNV_OPTION_VERSION);
return;
} else if((uint8_t)(c-'0')<10) {
- *pFlags=(*pFlags&~UCNV_OPTION_VERSION)|(uint32_t)(c-'0');
+ pArgs->options=pPieces->options=(pPieces->options&~UCNV_OPTION_VERSION)|(uint32_t)(c-'0');
++inName;
}
} else if(uprv_strncmp(inName, "swaplfnl", 8)==0) {
inName+=8;
- *pFlags|=UCNV_OPTION_SWAP_LFNL;
+ pArgs->options=(pPieces->options|=UCNV_OPTION_SWAP_LFNL);
/* add processing for new options here with another } else if(uprv_strncmp(inName, "option-name=", XX)==0) { */
} else {
/* ignore any other options until we define some */
* -Call AlgorithmicConverter initializer (Data=FALSE, Cached=TRUE)
*/
UConverterSharedData *
-ucnv_loadSharedData(const char *converterName, UConverterLookupData *lookup, UErrorCode * err) {
- UConverterLookupData stackLookup;
+ucnv_loadSharedData(const char *converterName,
+ UConverterNamePieces *pPieces,
+ UConverterLoadArgs *pArgs,
+ UErrorCode * err) {
+ UConverterNamePieces stackPieces;
+ UConverterLoadArgs stackArgs;
UConverterSharedData *mySharedConverterData = NULL;
UErrorCode internalErrorCode = U_ZERO_ERROR;
+ UBool mayContainOption = TRUE;
+ UBool checkForAlgorithmic = TRUE;
if (U_FAILURE (*err)) {
return NULL;
}
- if(lookup == NULL) {
- lookup = &stackLookup;
+ if(pPieces == NULL) {
+ if(pArgs != NULL) {
+ /*
+ * Bad: We may set pArgs pointers to stackPieces fields
+ * which will be invalid after this function returns.
+ */
+ *err = U_INTERNAL_PROGRAM_ERROR;
+ return NULL;
+ }
+ pPieces = &stackPieces;
+ }
+ if(pArgs == NULL) {
+ uprv_memset(&stackArgs, 0, sizeof(stackArgs));
+ stackArgs.size = (int32_t)sizeof(stackArgs);
+ pArgs = &stackArgs;
}
- lookup->locale[0] = 0;
- lookup->options = 0;
+ pPieces->cnvName[0] = 0;
+ pPieces->locale[0] = 0;
+ pPieces->options = 0;
+
+ pArgs->name = converterName;
+ pArgs->locale = pPieces->locale;
+ pArgs->options = pPieces->options;
/* In case "name" is NULL we want to open the default converter. */
if (converterName == NULL) {
- lookup->realName = ucnv_io_getDefaultConverterName();
- if (lookup->realName == NULL) {
+#if U_CHARSET_IS_UTF8
+ pArgs->name = "UTF-8";
+ return (UConverterSharedData *)converterData[UCNV_UTF8];
+#else
+ /* Call ucnv_getDefaultName first to query the name from the OS. */
+ pArgs->name = ucnv_getDefaultName();
+ if (pArgs->name == NULL) {
*err = U_MISSING_RESOURCE_ERROR;
return NULL;
}
+ mySharedConverterData = (UConverterSharedData *)gDefaultAlgorithmicSharedData;
+ checkForAlgorithmic = FALSE;
+ mayContainOption = gDefaultConverterContainsOption;
/* the default converter name is already canonical */
- } else {
+#endif
+ }
+ else if(UCNV_FAST_IS_UTF8(converterName)) {
+ /* fastpath for UTF-8 */
+ pArgs->name = "UTF-8";
+ return (UConverterSharedData *)converterData[UCNV_UTF8];
+ }
+ else {
/* separate the converter name from the options */
- parseConverterOptions(converterName, lookup->cnvName, lookup->locale, &lookup->options, err);
+ parseConverterOptions(converterName, pPieces, pArgs, err);
if (U_FAILURE(*err)) {
/* Very bad name used. */
return NULL;
}
/* get the canonical converter name */
- lookup->realName = ucnv_io_getConverterName(lookup->cnvName, &internalErrorCode);
- if (U_FAILURE(internalErrorCode) || lookup->realName == NULL) {
+ pArgs->name = ucnv_io_getConverterName(pArgs->name, &mayContainOption, &internalErrorCode);
+ if (U_FAILURE(internalErrorCode) || pArgs->name == NULL) {
/*
* set the input name in case the converter was added
* without updating the alias table, or when there is no alias table
*/
- lookup->realName = lookup->cnvName;
+ pArgs->name = pPieces->cnvName;
}
}
/* separate the converter name from the options */
- if(lookup->realName != lookup->cnvName) {
- parseConverterOptions(lookup->realName, lookup->cnvName, lookup->locale, &lookup->options, err);
- lookup->realName = lookup->cnvName;
+ if(mayContainOption && pArgs->name != pPieces->cnvName) {
+ parseConverterOptions(pArgs->name, pPieces, pArgs, err);
}
/* get the shared data for an algorithmic converter, if it is one */
- mySharedConverterData = (UConverterSharedData *)getAlgorithmicTypeFromName(lookup->realName);
+ if (checkForAlgorithmic) {
+ mySharedConverterData = (UConverterSharedData *)getAlgorithmicTypeFromName(pArgs->name);
+ }
if (mySharedConverterData == NULL)
{
/* it is a data-based converter, get its shared data. */
/* converter data cache, and adding new entries to the cache */
/* to prevent other threads from modifying the cache during the */
/* process. */
- UConverterLoadArgs args={ 0 };
-
- args.size=sizeof(UConverterLoadArgs);
- args.nestedLoads=1;
- args.options=lookup->options;
- args.pkg=NULL;
- args.name=lookup->realName;
+ pArgs->nestedLoads=1;
+ pArgs->pkg=NULL;
umtx_lock(&cnvCacheMutex);
- mySharedConverterData = ucnv_load(&args, err);
+ mySharedConverterData = ucnv_load(pArgs, err);
umtx_unlock(&cnvCacheMutex);
if (U_FAILURE (*err) || (mySharedConverterData == NULL))
{
UConverter *
ucnv_createConverter(UConverter *myUConverter, const char *converterName, UErrorCode * err)
{
- UConverterLookupData stackLookup;
+ UConverterNamePieces stackPieces;
+ UConverterLoadArgs stackArgs={ (int32_t)sizeof(UConverterLoadArgs) };
UConverterSharedData *mySharedConverterData;
UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN);
if(U_SUCCESS(*err)) {
UTRACE_DATA1(UTRACE_OPEN_CLOSE, "open converter %s", converterName);
- mySharedConverterData = ucnv_loadSharedData(converterName, &stackLookup, err);
+ mySharedConverterData = ucnv_loadSharedData(converterName, &stackPieces, &stackArgs, err);
+
+ myUConverter = ucnv_createConverterFromSharedData(
+ myUConverter, mySharedConverterData,
+ &stackArgs,
+ err);
if(U_SUCCESS(*err)) {
- myUConverter = ucnv_createConverterFromSharedData(
- myUConverter, mySharedConverterData,
- stackLookup.realName, stackLookup.locale, stackLookup.options,
- err);
-
- if(U_SUCCESS(*err)) {
- UTRACE_EXIT_PTR_STATUS(myUConverter, *err);
- return myUConverter;
- } else {
- ucnv_unloadSharedDataIfReady(mySharedConverterData);
- }
+ UTRACE_EXIT_PTR_STATUS(myUConverter, *err);
+ return myUConverter;
}
}
return NULL;
}
+U_CFUNC UBool
+ucnv_canCreateConverter(const char *converterName, UErrorCode *err) {
+ UConverter myUConverter;
+ UConverterNamePieces stackPieces;
+ UConverterLoadArgs stackArgs={ (int32_t)sizeof(UConverterLoadArgs) };
+ UConverterSharedData *mySharedConverterData;
+
+ UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN);
+
+ if(U_SUCCESS(*err)) {
+ UTRACE_DATA1(UTRACE_OPEN_CLOSE, "test if can open converter %s", converterName);
+
+ stackArgs.onlyTestIsLoadable=TRUE;
+ mySharedConverterData = ucnv_loadSharedData(converterName, &stackPieces, &stackArgs, err);
+ ucnv_createConverterFromSharedData(
+ &myUConverter, mySharedConverterData,
+ &stackArgs,
+ err);
+ ucnv_unloadSharedDataIfReady(mySharedConverterData);
+ }
+
+ UTRACE_EXIT_STATUS(*err);
+ return U_SUCCESS(*err);
+}
+
UConverter *
ucnv_createAlgorithmicConverter(UConverter *myUConverter,
UConverterType type,
UErrorCode *err) {
UConverter *cnv;
const UConverterSharedData *sharedData;
- UBool isAlgorithmicConverter;
+ UConverterLoadArgs stackArgs={ (int32_t)sizeof(UConverterLoadArgs) };
UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_ALGORITHMIC);
UTRACE_DATA1(UTRACE_OPEN_CLOSE, "open algorithmic converter type %d", (int32_t)type);
}
sharedData = converterData[type];
- umtx_lock(&cnvCacheMutex);
- isAlgorithmicConverter = (UBool)(sharedData == NULL || sharedData->referenceCounter != ~0);
- umtx_unlock(&cnvCacheMutex);
- if (isAlgorithmicConverter) {
+ /*
+ Checking whether it's an algorithic converter is okay
+ in multithreaded applications because the value never changes.
+ Don't check referenceCounter for any other value.
+ */
+ if(sharedData == NULL || sharedData->referenceCounter != ~0) {
/* not a valid type, or not an algorithmic converter */
*err = U_ILLEGAL_ARGUMENT_ERROR;
UTRACE_EXIT_STATUS(U_ILLEGAL_ARGUMENT_ERROR);
return NULL;
}
- cnv = ucnv_createConverterFromSharedData(myUConverter, (UConverterSharedData *)sharedData, "",
- locale != NULL ? locale : "", options, err);
+ stackArgs.name = "";
+ stackArgs.options = options;
+ stackArgs.locale=locale;
+ cnv = ucnv_createConverterFromSharedData(
+ myUConverter, (UConverterSharedData *)sharedData,
+ &stackArgs, err);
UTRACE_EXIT_PTR_STATUS(cnv, *err);
return cnv;
UConverter*
ucnv_createConverterFromPackage(const char *packageName, const char *converterName, UErrorCode * err)
{
- char cnvName[UCNV_MAX_CONVERTER_NAME_LENGTH], locale[ULOC_FULLNAME_CAPACITY];
UConverter *myUConverter;
UConverterSharedData *mySharedConverterData;
-
- UConverterLoadArgs args={ 0 };
+ UConverterNamePieces stackPieces;
+ UConverterLoadArgs stackArgs={ (int32_t)sizeof(UConverterLoadArgs) };
UTRACE_ENTRY_OC(UTRACE_UCNV_OPEN_PACKAGE);
UTRACE_DATA2(UTRACE_OPEN_CLOSE, "open converter %s from package %s", converterName, packageName);
- args.size=sizeof(UConverterLoadArgs);
- args.nestedLoads=1;
- args.pkg=packageName;
-
/* first, get the options out of the converterName string */
- parseConverterOptions(converterName, cnvName, locale, &args.options, err);
+ stackPieces.cnvName[0] = 0;
+ stackPieces.locale[0] = 0;
+ stackPieces.options = 0;
+ parseConverterOptions(converterName, &stackPieces, &stackArgs, err);
if (U_FAILURE(*err)) {
/* Very bad name used. */
UTRACE_EXIT_STATUS(*err);
return NULL;
}
- args.name=cnvName;
+ stackArgs.nestedLoads=1;
+ stackArgs.pkg=packageName;
/* open the data, unflatten the shared structure */
- mySharedConverterData = createConverterFromFile(&args, err);
+ mySharedConverterData = createConverterFromFile(&stackArgs, err);
if (U_FAILURE(*err)) {
UTRACE_EXIT_STATUS(*err);
}
/* create the actual converter */
- myUConverter = ucnv_createConverterFromSharedData(NULL, mySharedConverterData, cnvName, locale, args.options, err);
+ myUConverter = ucnv_createConverterFromSharedData(NULL, mySharedConverterData, &stackArgs, err);
if (U_FAILURE(*err)) {
ucnv_close(myUConverter);
UConverter*
ucnv_createConverterFromSharedData(UConverter *myUConverter,
UConverterSharedData *mySharedConverterData,
- const char *realName, const char *locale, uint32_t options,
+ UConverterLoadArgs *pArgs,
UErrorCode *err)
{
UBool isCopyLocal;
+ if(U_FAILURE(*err)) {
+ ucnv_unloadSharedDataIfReady(mySharedConverterData);
+ return myUConverter;
+ }
if(myUConverter == NULL)
{
myUConverter = (UConverter *) uprv_malloc (sizeof (UConverter));
if(myUConverter == NULL)
{
*err = U_MEMORY_ALLOCATION_ERROR;
+ ucnv_unloadSharedDataIfReady(mySharedConverterData);
return NULL;
}
isCopyLocal = FALSE;
/* initialize the converter */
uprv_memset(myUConverter, 0, sizeof(UConverter));
myUConverter->isCopyLocal = isCopyLocal;
- myUConverter->isExtraLocal = FALSE;
+ /*myUConverter->isExtraLocal = FALSE;*/ /* Set by the memset call */
myUConverter->sharedData = mySharedConverterData;
- myUConverter->options = options;
- myUConverter->fromCharErrorBehaviour = (UConverterToUCallback) UCNV_TO_U_CALLBACK_SUBSTITUTE;
- myUConverter->fromUCharErrorBehaviour = (UConverterFromUCallback) UCNV_FROM_U_CALLBACK_SUBSTITUTE;
- myUConverter->toUnicodeStatus = myUConverter->sharedData->toUnicodeStatus;
- myUConverter->maxBytesPerUChar = myUConverter->sharedData->staticData->maxBytesPerChar;
- myUConverter->subChar1 = myUConverter->sharedData->staticData->subChar1;
- myUConverter->subCharLen = myUConverter->sharedData->staticData->subCharLen;
- uprv_memcpy (myUConverter->subChar, myUConverter->sharedData->staticData->subChar, myUConverter->subCharLen);
- myUConverter->preFromUFirstCP = U_SENTINEL;
-
- if(myUConverter != NULL && myUConverter->sharedData->impl->open != NULL) {
- myUConverter->sharedData->impl->open(myUConverter, realName, locale,options, err);
- if(U_FAILURE(*err)) {
+ myUConverter->options = pArgs->options;
+ if(!pArgs->onlyTestIsLoadable) {
+ myUConverter->preFromUFirstCP = U_SENTINEL;
+ myUConverter->fromCharErrorBehaviour = UCNV_TO_U_DEFAULT_CALLBACK;
+ myUConverter->fromUCharErrorBehaviour = UCNV_FROM_U_DEFAULT_CALLBACK;
+ myUConverter->toUnicodeStatus = mySharedConverterData->toUnicodeStatus;
+ myUConverter->maxBytesPerUChar = mySharedConverterData->staticData->maxBytesPerChar;
+ myUConverter->subChar1 = mySharedConverterData->staticData->subChar1;
+ myUConverter->subCharLen = mySharedConverterData->staticData->subCharLen;
+ myUConverter->subChars = (uint8_t *)myUConverter->subUChars;
+ uprv_memcpy(myUConverter->subChars, mySharedConverterData->staticData->subChar, myUConverter->subCharLen);
+ myUConverter->toUCallbackReason = UCNV_ILLEGAL; /* default reason to invoke (*fromCharErrorBehaviour) */
+ }
+
+ if(mySharedConverterData->impl->open != NULL) {
+ mySharedConverterData->impl->open(myUConverter, pArgs, err);
+ if(U_FAILURE(*err) && !pArgs->onlyTestIsLoadable) {
+ /* don't ucnv_close() if onlyTestIsLoadable because not fully initialized */
ucnv_close(myUConverter);
return NULL;
}
int32_t pos;
int32_t tableDeletedNum = 0;
const UHashElement *e;
- UErrorCode status = U_ILLEGAL_ARGUMENT_ERROR;
+ /*UErrorCode status = U_ILLEGAL_ARGUMENT_ERROR;*/
int32_t i, remaining;
UTRACE_ENTRY_OC(UTRACE_UCNV_FLUSH_CACHE);
/* Close the default converter without creating a new one so that everything will be flushed. */
- ucnv_close(u_getDefaultConverter(&status));
+ u_flushDefaultConverter();
/*if shared data hasn't even been lazy evaluated yet
* return 0
UTRACE_DATA1(UTRACE_INFO, "ucnv_flushCache() exits with %d converters remaining", remaining);
- ucnv_io_flushAvailableConverterCache();
-
UTRACE_EXIT_VALUE(tableDeletedNum);
return tableDeletedNum;
}
+/* available converters list --------------------------------------------------- */
+
+static UBool haveAvailableConverterList(UErrorCode *pErrorCode) {
+ int needInit;
+ UMTX_CHECK(&cnvCacheMutex, (gAvailableConverters == NULL), needInit);
+ if (needInit) {
+ UConverter tempConverter;
+ UEnumeration *allConvEnum = NULL;
+ uint16_t idx;
+ uint16_t localConverterCount;
+ uint16_t allConverterCount;
+ UErrorCode localStatus;
+ const char *converterName;
+ const char **localConverterList;
+
+ allConvEnum = ucnv_openAllNames(pErrorCode);
+ allConverterCount = uenum_count(allConvEnum, pErrorCode);
+ if (U_FAILURE(*pErrorCode)) {
+ return FALSE;
+ }
+
+ /* We can't have more than "*converterTable" converters to open */
+ localConverterList = (const char **) uprv_malloc(allConverterCount * sizeof(char*));
+ if (!localConverterList) {
+ *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
+ return FALSE;
+ }
+
+ /* Open the default converter to make sure that it has first dibs in the hash table. */
+ localStatus = U_ZERO_ERROR;
+ ucnv_close(ucnv_createConverter(&tempConverter, NULL, &localStatus));
+
+ localConverterCount = 0;
+
+ for (idx = 0; idx < allConverterCount; idx++) {
+ localStatus = U_ZERO_ERROR;
+ converterName = uenum_next(allConvEnum, NULL, &localStatus);
+ if (ucnv_canCreateConverter(converterName, &localStatus)) {
+ localConverterList[localConverterCount++] = converterName;
+ }
+ }
+ uenum_close(allConvEnum);
+
+ umtx_lock(&cnvCacheMutex);
+ if (gAvailableConverters == NULL) {
+ gAvailableConverterCount = localConverterCount;
+ gAvailableConverters = localConverterList;
+ ucln_common_registerCleanup(UCLN_COMMON_UCNV, ucnv_cleanup);
+ }
+ else {
+ uprv_free((char **)localConverterList);
+ }
+ umtx_unlock(&cnvCacheMutex);
+ }
+ return TRUE;
+}
+
+U_CFUNC uint16_t
+ucnv_bld_countAvailableConverters(UErrorCode *pErrorCode) {
+ if (haveAvailableConverterList(pErrorCode)) {
+ return gAvailableConverterCount;
+ }
+ return 0;
+}
+
+U_CFUNC const char *
+ucnv_bld_getAvailableConverter(uint16_t n, UErrorCode *pErrorCode) {
+ if (haveAvailableConverterList(pErrorCode)) {
+ if (n < gAvailableConverterCount) {
+ return gAvailableConverters[n];
+ }
+ *pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
+ }
+ return NULL;
+}
+
+/* default converter name --------------------------------------------------- */
+
+#if !U_CHARSET_IS_UTF8
+/*
+Copy the canonical converter name.
+ucnv_getDefaultName must be thread safe, which can call this function.
+
+ucnv_setDefaultName calls this function and it doesn't have to be
+thread safe because there is no reliable/safe way to reset the
+converter in use in all threads. If you did reset the converter, you
+would not be sure that retrieving a default converter for one string
+would be the same type of default converter for a successive string.
+Since the name is a returned via ucnv_getDefaultName without copying,
+you shouldn't be modifying or deleting the string from a separate thread.
+*/
+static U_INLINE void
+internalSetName(const char *name, UErrorCode *status) {
+ UConverterNamePieces stackPieces;
+ UConverterLoadArgs stackArgs={ (int32_t)sizeof(UConverterLoadArgs) };
+ int32_t length=(int32_t)(uprv_strlen(name));
+ UBool containsOption = (UBool)(uprv_strchr(name, UCNV_OPTION_SEP_CHAR) != NULL);
+ const UConverterSharedData *algorithmicSharedData;
+
+ stackArgs.name = name;
+ if(containsOption) {
+ stackPieces.cnvName[0] = 0;
+ stackPieces.locale[0] = 0;
+ stackPieces.options = 0;
+ parseConverterOptions(name, &stackPieces, &stackArgs, status);
+ if(U_FAILURE(*status)) {
+ return;
+ }
+ }
+ algorithmicSharedData = getAlgorithmicTypeFromName(stackArgs.name);
+
+ umtx_lock(&cnvCacheMutex);
+
+ gDefaultAlgorithmicSharedData = algorithmicSharedData;
+ gDefaultConverterContainsOption = containsOption;
+ uprv_memcpy(gDefaultConverterNameBuffer, name, length);
+ gDefaultConverterNameBuffer[length]=0;
+
+ /* gDefaultConverterName MUST be the last global var set by this function. */
+ /* It is the variable checked in ucnv_getDefaultName() to see if initialization is required. */
+ gDefaultConverterName = gDefaultConverterNameBuffer;
+
+ ucln_common_registerCleanup(UCLN_COMMON_UCNV, ucnv_cleanup);
+
+ umtx_unlock(&cnvCacheMutex);
+}
+#endif
+
+/*
+ * In order to be really thread-safe, the get function would have to take
+ * a buffer parameter and copy the current string inside a mutex block.
+ * This implementation only tries to be really thread-safe while
+ * setting the name.
+ * It assumes that setting a pointer is atomic.
+ */
+
+U_CAPI const char* U_EXPORT2
+ucnv_getDefaultName() {
+#if U_CHARSET_IS_UTF8
+ return "UTF-8";
+#else
+ /* local variable to be thread-safe */
+ const char *name;
+
+ /*
+ Multiple calls to ucnv_getDefaultName must be thread safe,
+ but ucnv_setDefaultName is not thread safe.
+ */
+ UMTX_CHECK(&cnvCacheMutex, gDefaultConverterName, name);
+ if(name==NULL) {
+ UErrorCode errorCode = U_ZERO_ERROR;
+ UConverter *cnv = NULL;
+
+ name = uprv_getDefaultCodepage();
+
+ /* if the name is there, test it out and get the canonical name with options */
+ if(name != NULL) {
+ cnv = ucnv_open(name, &errorCode);
+ if(U_SUCCESS(errorCode) && cnv != NULL) {
+ name = ucnv_getName(cnv, &errorCode);
+ }
+ }
+
+ if(name == NULL || name[0] == 0
+ || U_FAILURE(errorCode) || cnv == NULL
+ || uprv_strlen(name)>=sizeof(gDefaultConverterNameBuffer))
+ {
+ /* Panic time, let's use a fallback. */
+#if (U_CHARSET_FAMILY == U_ASCII_FAMILY)
+ name = "US-ASCII";
+ /* there is no 'algorithmic' converter for EBCDIC */
+#elif defined(OS390)
+ name = "ibm-1047_P100-1995" UCNV_SWAP_LFNL_OPTION_STRING;
+#else
+ name = "ibm-37_P100-1995";
+#endif
+ }
+
+ internalSetName(name, &errorCode);
+
+ /* The close may make the current name go away. */
+ ucnv_close(cnv);
+ }
+
+ return name;
+#endif
+}
+
+/*
+This function is not thread safe, and it can't be thread safe.
+See internalSetName or the API reference for details.
+*/
+U_CAPI void U_EXPORT2
+ucnv_setDefaultName(const char *converterName) {
+#if !U_CHARSET_IS_UTF8
+ if(converterName==NULL) {
+ /* reset to the default codepage */
+ gDefaultConverterName=NULL;
+ } else {
+ UErrorCode errorCode = U_ZERO_ERROR;
+ UConverter *cnv = NULL;
+ const char *name = NULL;
+
+ /* if the name is there, test it out and get the canonical name with options */
+ cnv = ucnv_open(converterName, &errorCode);
+ if(U_SUCCESS(errorCode) && cnv != NULL) {
+ name = ucnv_getName(cnv, &errorCode);
+ }
+
+ if(U_SUCCESS(errorCode) && name!=NULL) {
+ internalSetName(name, &errorCode);
+ }
+ /* else this converter is bad to use. Don't change it to a bad value. */
+
+ /* The close may make the current name go away. */
+ ucnv_close(cnv);
+
+ /* reset the converter cache */
+ u_flushDefaultConverter();
+ }
+#endif
+}
+
/* data swapping ------------------------------------------------------------ */
/* most of this might belong more properly into ucnvmbcs.c, but that is so large */
const _MBCSHeader *inMBCSHeader;
_MBCSHeader *outMBCSHeader;
_MBCSHeader mbcsHeader;
+ uint32_t mbcsHeaderLength;
+ UBool noFromU=FALSE;
+
uint8_t outputType;
+ int32_t maxFastUChar, mbcsIndexLength;
+
const int32_t *inExtIndexes;
int32_t extOffset;
ds->swapArray32(ds, &inStaticData->codepage, 4,
&outStaticData->codepage, pErrorCode);
- ds->swapInvChars(ds, inStaticData->name, uprv_strlen(inStaticData->name),
+ ds->swapInvChars(ds, inStaticData->name, (int32_t)uprv_strlen(inStaticData->name),
outStaticData->name, pErrorCode);
if(U_FAILURE(*pErrorCode)) {
- udata_printError(ds, "ucnv_swap(): error swapping converter name - %s\n",
- u_errorName(*pErrorCode));
+ udata_printError(ds, "ucnv_swap(): error swapping converter name\n");
return 0;
}
}
inMBCSHeader=(const _MBCSHeader *)inBytes;
outMBCSHeader=(_MBCSHeader *)outBytes;
- if(!(inMBCSHeader->version[0]==4 || inMBCSHeader->version[1]>=1)) {
+ if(0<=length && length<sizeof(_MBCSHeader)) {
+ udata_printError(ds, "ucnv_swap(): too few bytes (%d after headers) for an ICU MBCS .cnv conversion table\n",
+ length);
+ *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
+ return 0;
+ }
+ if(inMBCSHeader->version[0]==4 && inMBCSHeader->version[1]>=1) {
+ mbcsHeaderLength=MBCS_HEADER_V4_LENGTH;
+ } else if(inMBCSHeader->version[0]==5 && inMBCSHeader->version[1]>=3 &&
+ ((mbcsHeader.options=ds->readUInt32(inMBCSHeader->options))&
+ MBCS_OPT_UNKNOWN_INCOMPATIBLE_MASK)==0
+ ) {
+ mbcsHeaderLength=mbcsHeader.options&MBCS_OPT_LENGTH_MASK;
+ noFromU=(UBool)((mbcsHeader.options&MBCS_OPT_NO_FROM_U)!=0);
+ } else {
udata_printError(ds, "ucnv_swap(): unsupported _MBCSHeader.version %d.%d\n",
inMBCSHeader->version[0], inMBCSHeader->version[1]);
*pErrorCode=U_UNSUPPORTED_ERROR;
mbcsHeader.offsetFromUBytes= ds->readUInt32(inMBCSHeader->offsetFromUBytes);
mbcsHeader.flags= ds->readUInt32(inMBCSHeader->flags);
mbcsHeader.fromUBytesLength= ds->readUInt32(inMBCSHeader->fromUBytesLength);
+ /* mbcsHeader.options have been read above */
- extOffset=(int32_t)mbcsHeader.flags>>8;
+ extOffset=(int32_t)(mbcsHeader.flags>>8);
outputType=(uint8_t)mbcsHeader.flags;
+ if(noFromU && outputType==MBCS_OUTPUT_1) {
+ udata_printError(ds, "ucnv_swap(): unsupported combination of makeconv --small with SBCS\n");
+ *pErrorCode=U_UNSUPPORTED_ERROR;
+ return 0;
+ }
/* make sure that the output type is known */
switch(outputType) {
}
/* calculate the length of the MBCS data */
+
+ /*
+ * utf8Friendly MBCS files (mbcsHeader.version 4.3)
+ * contain an additional mbcsIndex table:
+ * uint16_t[(maxFastUChar+1)>>6];
+ * where maxFastUChar=((mbcsHeader.version[2]<<8)|0xff).
+ */
+ maxFastUChar=0;
+ mbcsIndexLength=0;
+ if( outputType!=MBCS_OUTPUT_EXT_ONLY && outputType!=MBCS_OUTPUT_1 &&
+ mbcsHeader.version[1]>=3 && (maxFastUChar=mbcsHeader.version[2])!=0
+ ) {
+ maxFastUChar=(maxFastUChar<<8)|0xff;
+ mbcsIndexLength=((maxFastUChar+1)>>6)*2; /* number of bytes */
+ }
+
if(extOffset==0) {
- size=(int32_t)(mbcsHeader.offsetFromUBytes+mbcsHeader.fromUBytesLength);
+ size=(int32_t)(mbcsHeader.offsetFromUBytes+mbcsIndexLength);
+ if(!noFromU) {
+ size+=(int32_t)mbcsHeader.fromUBytesLength;
+ }
/* avoid compiler warnings - not otherwise necessary, and the value does not matter */
inExtIndexes=NULL;
uprv_memcpy(outBytes, inBytes, size);
}
- /* swap the MBCSHeader */
- ds->swapArray32(ds, &inMBCSHeader->countStates, 7*4,
+ /* swap the MBCSHeader, except for the version field */
+ count=mbcsHeaderLength*4;
+ ds->swapArray32(ds, &inMBCSHeader->countStates, count-4,
&outMBCSHeader->countStates, pErrorCode);
if(outputType==MBCS_OUTPUT_EXT_ONLY) {
*/
/* swap the base name, between the header and the extension data */
- ds->swapInvChars(ds, inMBCSHeader+1, uprv_strlen((const char *)(inMBCSHeader+1)),
- outMBCSHeader+1, pErrorCode);
+ const char *inBaseName=(const char *)inBytes+count;
+ char *outBaseName=(char *)outBytes+count;
+ ds->swapInvChars(ds, inBaseName, (int32_t)uprv_strlen(inBaseName),
+ outBaseName, pErrorCode);
} else {
/* normal file with base table data */
/* swap the state table, 1kB per state */
- ds->swapArray32(ds, inMBCSHeader+1, (int32_t)(mbcsHeader.countStates*1024),
- outMBCSHeader+1, pErrorCode);
+ offset=count;
+ count=mbcsHeader.countStates*1024;
+ ds->swapArray32(ds, inBytes+offset, (int32_t)count,
+ outBytes+offset, pErrorCode);
/* swap the toUFallbacks[] */
- offset=sizeof(_MBCSHeader)+mbcsHeader.countStates*1024;
- ds->swapArray32(ds, inBytes+offset, (int32_t)(mbcsHeader.countToUFallbacks*8),
+ offset+=count;
+ count=mbcsHeader.countToUFallbacks*8;
+ ds->swapArray32(ds, inBytes+offset, (int32_t)count,
outBytes+offset, pErrorCode);
/* swap the unicodeCodeUnits[] */
/* stage 3/result bytes: sometimes uint16_t[] or uint32_t[] */
offset=mbcsHeader.offsetFromUBytes;
- count=mbcsHeader.fromUBytesLength;
+ count= noFromU ? 0 : mbcsHeader.fromUBytesLength;
switch(outputType) {
case MBCS_OUTPUT_2:
case MBCS_OUTPUT_3_EUC:
/* just uint8_t[], nothing to swap */
break;
}
+
+ if(mbcsIndexLength!=0) {
+ offset+=count;
+ count=mbcsIndexLength;
+ ds->swapArray16(ds, inBytes+offset, (int32_t)count,
+ outBytes+offset, pErrorCode);
+ }
}
}