#include "unicode/utypes.h"
#if !UCONFIG_NO_CONVERSION
#include "unicode/ucnv.h"
#include "unicode/udata.h"
#include "umutex.h"
#include "uarrsort.h"
#include "uassert.h"
#include "udataswp.h"
#include "cstring.h"
#include "cmemory.h"
#include "ucnv_io.h"
#include "uenumimp.h"
#include "ucln_cmn.h"
typedef struct UAliasContext {
uint32_t listOffset;
uint32_t listIdx;
} UAliasContext;
static const char DATA_NAME[] = "cnvalias";
static const char DATA_TYPE[] = "icu";
static UDataMemory *gAliasData=nullptr;
static icu::UInitOnce gAliasDataInitOnce {};
enum {
tocLengthIndex=0,
converterListIndex=1,
tagListIndex=2,
aliasListIndex=3,
untaggedConvArrayIndex=4,
taggedAliasArrayIndex=5,
taggedAliasListsIndex=6,
tableOptionsIndex=7,
stringTableIndex=8,
normalizedStringTableIndex=9,
offsetsCount,
minTocLength=8
};
static const UConverterAliasOptions defaultTableOptions = {
UCNV_IO_UNNORMALIZED,
0
};
static UConverterAlias gMainTable;
#define GET_STRING …
#define GET_NORMALIZED_STRING …
static UBool U_CALLCONV
isAcceptable(void * ,
const char * , const char * ,
const UDataInfo *pInfo) {
return
pInfo->size>=20 &&
pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
pInfo->charsetFamily==U_CHARSET_FAMILY &&
pInfo->dataFormat[0]==0x43 &&
pInfo->dataFormat[1]==0x76 &&
pInfo->dataFormat[2]==0x41 &&
pInfo->dataFormat[3]==0x6c &&
pInfo->formatVersion[0]==3;
}
static UBool U_CALLCONV ucnv_io_cleanup()
{
if (gAliasData) {
udata_close(gAliasData);
gAliasData = nullptr;
}
gAliasDataInitOnce.reset();
uprv_memset(&gMainTable, 0, sizeof(gMainTable));
return true;
}
static void U_CALLCONV initAliasData(UErrorCode &errCode) {
UDataMemory *data;
const uint16_t *table;
const uint32_t *sectionSizes;
uint32_t tableStart;
uint32_t currOffset;
ucln_common_registerCleanup(UCLN_COMMON_UCNV_IO, ucnv_io_cleanup);
U_ASSERT(gAliasData == nullptr);
data = udata_openChoice(nullptr, DATA_TYPE, DATA_NAME, isAcceptable, nullptr, &errCode);
if(U_FAILURE(errCode)) {
return;
}
sectionSizes = static_cast<const uint32_t*>(udata_getMemory(data));
table = reinterpret_cast<const uint16_t*>(sectionSizes);
tableStart = sectionSizes[0];
if (tableStart < minTocLength) {
errCode = U_INVALID_FORMAT_ERROR;
udata_close(data);
return;
}
gAliasData = data;
gMainTable.converterListSize = sectionSizes[1];
gMainTable.tagListSize = sectionSizes[2];
gMainTable.aliasListSize = sectionSizes[3];
gMainTable.untaggedConvArraySize = sectionSizes[4];
gMainTable.taggedAliasArraySize = sectionSizes[5];
gMainTable.taggedAliasListsSize = sectionSizes[6];
gMainTable.optionTableSize = sectionSizes[7];
gMainTable.stringTableSize = sectionSizes[8];
if (tableStart > 8) {
gMainTable.normalizedStringTableSize = sectionSizes[9];
}
currOffset = tableStart * (sizeof(uint32_t)/sizeof(uint16_t)) + (sizeof(uint32_t)/sizeof(uint16_t));
gMainTable.converterList = table + currOffset;
currOffset += gMainTable.converterListSize;
gMainTable.tagList = table + currOffset;
currOffset += gMainTable.tagListSize;
gMainTable.aliasList = table + currOffset;
currOffset += gMainTable.aliasListSize;
gMainTable.untaggedConvArray = table + currOffset;
currOffset += gMainTable.untaggedConvArraySize;
gMainTable.taggedAliasArray = table + currOffset;
currOffset += gMainTable.taggedAliasArraySize;
gMainTable.taggedAliasLists = table + currOffset;
currOffset += gMainTable.taggedAliasListsSize;
if (gMainTable.optionTableSize > 0
&& reinterpret_cast<const UConverterAliasOptions*>(table + currOffset)->stringNormalizationType < UCNV_IO_NORM_TYPE_COUNT)
{
gMainTable.optionTable = reinterpret_cast<const UConverterAliasOptions*>(table + currOffset);
}
else {
gMainTable.optionTable = &defaultTableOptions;
}
currOffset += gMainTable.optionTableSize;
gMainTable.stringTable = table + currOffset;
currOffset += gMainTable.stringTableSize;
gMainTable.normalizedStringTable = ((gMainTable.optionTable->stringNormalizationType == UCNV_IO_UNNORMALIZED)
? gMainTable.stringTable : (table + currOffset));
}
static UBool
haveAliasData(UErrorCode *pErrorCode) {
umtx_initOnce(gAliasDataInitOnce, &initAliasData, *pErrorCode);
return U_SUCCESS(*pErrorCode);
}
static inline UBool
isAlias(const char *alias, UErrorCode *pErrorCode) {
if(alias==nullptr) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return false;
}
return *alias != 0;
}
static uint32_t getTagNumber(const char *tagname) {
if (gMainTable.tagList) {
uint32_t tagNum;
for (tagNum = 0; tagNum < gMainTable.tagListSize; tagNum++) {
if (!uprv_stricmp(GET_STRING(gMainTable.tagList[tagNum]), tagname)) {
return tagNum;
}
}
}
return UINT32_MAX;
}
enum {
UIGNORE,
ZERO,
NONZERO,
MINLETTER
};
static const uint8_t asciiTypes[128] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, 0, 0, 0, 0, 0, 0,
0, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0, 0, 0, 0, 0,
0, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0, 0, 0, 0, 0
};
#define GET_ASCII_TYPE …
static const uint8_t ebcdicTypes[128] = {
0, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0, 0, 0, 0, 0, 0,
0, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0, 0, 0, 0, 0, 0,
0, 0, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0, 0, 0, 0, 0, 0,
0, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0, 0, 0, 0, 0, 0,
0, 0, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0, 0, 0, 0, 0, 0,
ZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, 0, 0, 0, 0, 0, 0
};
#define GET_EBCDIC_TYPE …
#if U_CHARSET_FAMILY==U_ASCII_FAMILY
#define GET_CHAR_TYPE …
#elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
#define GET_CHAR_TYPE …
#else
# error U_CHARSET_FAMILY is not valid
#endif
U_CAPI char * U_CALLCONV
ucnv_io_stripASCIIForCompare(char *dst, const char *name) {
char *dstItr = dst;
uint8_t type, nextType;
char c1;
UBool afterDigit = false;
while ((c1 = *name++) != 0) {
type = GET_ASCII_TYPE(c1);
switch (type) {
case UIGNORE:
afterDigit = false;
continue;
case ZERO:
if (!afterDigit) {
nextType = GET_ASCII_TYPE(*name);
if (nextType == ZERO || nextType == NONZERO) {
continue;
}
}
break;
case NONZERO:
afterDigit = true;
break;
default:
c1 = (char)type;
afterDigit = false;
break;
}
*dstItr++ = c1;
}
*dstItr = 0;
return dst;
}
U_CAPI char * U_CALLCONV
ucnv_io_stripEBCDICForCompare(char *dst, const char *name) {
char *dstItr = dst;
uint8_t type, nextType;
char c1;
UBool afterDigit = false;
while ((c1 = *name++) != 0) {
type = GET_EBCDIC_TYPE(c1);
switch (type) {
case UIGNORE:
afterDigit = false;
continue;
case ZERO:
if (!afterDigit) {
nextType = GET_EBCDIC_TYPE(*name);
if (nextType == ZERO || nextType == NONZERO) {
continue;
}
}
break;
case NONZERO:
afterDigit = true;
break;
default:
c1 = (char)type;
afterDigit = false;
break;
}
*dstItr++ = c1;
}
*dstItr = 0;
return dst;
}
U_CAPI int U_EXPORT2
ucnv_compareNames(const char *name1, const char *name2) {
int rc;
uint8_t type, nextType;
char c1, c2;
UBool afterDigit1 = false, afterDigit2 = false;
for (;;) {
while ((c1 = *name1++) != 0) {
type = GET_CHAR_TYPE(c1);
switch (type) {
case UIGNORE:
afterDigit1 = false;
continue;
case ZERO:
if (!afterDigit1) {
nextType = GET_CHAR_TYPE(*name1);
if (nextType == ZERO || nextType == NONZERO) {
continue;
}
}
break;
case NONZERO:
afterDigit1 = true;
break;
default:
c1 = (char)type;
afterDigit1 = false;
break;
}
break;
}
while ((c2 = *name2++) != 0) {
type = GET_CHAR_TYPE(c2);
switch (type) {
case UIGNORE:
afterDigit2 = false;
continue;
case ZERO:
if (!afterDigit2) {
nextType = GET_CHAR_TYPE(*name2);
if (nextType == ZERO || nextType == NONZERO) {
continue;
}
}
break;
case NONZERO:
afterDigit2 = true;
break;
default:
c2 = (char)type;
afterDigit2 = false;
break;
}
break;
}
if ((c1|c2)==0) {
return 0;
}
rc = (int)(unsigned char)c1 - (int)(unsigned char)c2;
if (rc != 0) {
return rc;
}
}
}
static inline uint32_t
findConverter(const char *alias, UBool *containsOption, UErrorCode *pErrorCode) {
uint32_t mid, start, limit;
uint32_t lastMid;
int result;
int isUnnormalized = (gMainTable.optionTable->stringNormalizationType == UCNV_IO_UNNORMALIZED);
char strippedName[UCNV_MAX_CONVERTER_NAME_LENGTH];
if (!isUnnormalized) {
if (uprv_strlen(alias) >= UCNV_MAX_CONVERTER_NAME_LENGTH) {
*pErrorCode = U_BUFFER_OVERFLOW_ERROR;
return UINT32_MAX;
}
ucnv_io_stripForCompare(strippedName, alias);
alias = strippedName;
}
start = 0;
limit = gMainTable.untaggedConvArraySize;
mid = limit;
lastMid = UINT32_MAX;
for (;;) {
mid = (start + limit) / 2;
if (lastMid == mid) {
break;
}
lastMid = mid;
if (isUnnormalized) {
result = ucnv_compareNames(alias, GET_STRING(gMainTable.aliasList[mid]));
}
else {
result = uprv_strcmp(alias, GET_NORMALIZED_STRING(gMainTable.aliasList[mid]));
}
if (result < 0) {
limit = mid;
} else if (result > 0) {
start = mid;
} else {
if (gMainTable.untaggedConvArray[mid] & UCNV_AMBIGUOUS_ALIAS_MAP_BIT) {
*pErrorCode = U_AMBIGUOUS_ALIAS_WARNING;
}
if (containsOption) {
UBool containsCnvOptionInfo = static_cast<UBool>(gMainTable.optionTable->containsCnvOptionInfo);
*containsOption = static_cast<UBool>((containsCnvOptionInfo
&& ((gMainTable.untaggedConvArray[mid] & UCNV_CONTAINS_OPTION_BIT) != 0))
|| !containsCnvOptionInfo);
}
return gMainTable.untaggedConvArray[mid] & UCNV_CONVERTER_INDEX_MASK;
}
}
return UINT32_MAX;
}
static inline UBool
isAliasInList(const char *alias, uint32_t listOffset) {
if (listOffset) {
uint32_t currAlias;
uint32_t listCount = gMainTable.taggedAliasLists[listOffset];
const uint16_t *currList = gMainTable.taggedAliasLists + listOffset + 1;
for (currAlias = 0; currAlias < listCount; currAlias++) {
if (currList[currAlias]
&& ucnv_compareNames(alias, GET_STRING(currList[currAlias]))==0)
{
return true;
}
}
}
return false;
}
static uint32_t
findTaggedAliasListsOffset(const char *alias, const char *standard, UErrorCode *pErrorCode) {
uint32_t idx;
uint32_t listOffset;
uint32_t convNum;
UErrorCode myErr = U_ZERO_ERROR;
uint32_t tagNum = getTagNumber(standard);
convNum = findConverter(alias, nullptr, &myErr);
if (myErr != U_ZERO_ERROR) {
*pErrorCode = myErr;
}
if (tagNum < (gMainTable.tagListSize - UCNV_NUM_HIDDEN_TAGS) && convNum < gMainTable.converterListSize) {
listOffset = gMainTable.taggedAliasArray[tagNum*gMainTable.converterListSize + convNum];
if (listOffset && gMainTable.taggedAliasLists[listOffset + 1]) {
return listOffset;
}
if (myErr == U_AMBIGUOUS_ALIAS_WARNING) {
for (idx = 0; idx < gMainTable.taggedAliasArraySize; idx++) {
listOffset = gMainTable.taggedAliasArray[idx];
if (listOffset && isAliasInList(alias, listOffset)) {
uint32_t currTagNum = idx/gMainTable.converterListSize;
uint32_t currConvNum = (idx - currTagNum*gMainTable.converterListSize);
uint32_t tempListOffset = gMainTable.taggedAliasArray[tagNum*gMainTable.converterListSize + currConvNum];
if (tempListOffset && gMainTable.taggedAliasLists[tempListOffset + 1]) {
return tempListOffset;
}
}
}
}
return 0;
}
return UINT32_MAX;
}
static uint32_t
findTaggedConverterNum(const char *alias, const char *standard, UErrorCode *pErrorCode) {
uint32_t idx;
uint32_t listOffset;
uint32_t convNum;
UErrorCode myErr = U_ZERO_ERROR;
uint32_t tagNum = getTagNumber(standard);
convNum = findConverter(alias, nullptr, &myErr);
if (myErr != U_ZERO_ERROR) {
*pErrorCode = myErr;
}
if (tagNum < (gMainTable.tagListSize - UCNV_NUM_HIDDEN_TAGS) && convNum < gMainTable.converterListSize) {
listOffset = gMainTable.taggedAliasArray[tagNum*gMainTable.converterListSize + convNum];
if (listOffset && isAliasInList(alias, listOffset)) {
return convNum;
}
if (myErr == U_AMBIGUOUS_ALIAS_WARNING) {
uint32_t convStart = (tagNum)*gMainTable.converterListSize;
uint32_t convLimit = (tagNum+1)*gMainTable.converterListSize;
for (idx = convStart; idx < convLimit; idx++) {
listOffset = gMainTable.taggedAliasArray[idx];
if (listOffset && isAliasInList(alias, listOffset)) {
return idx-convStart;
}
}
}
}
return UINT32_MAX;
}
U_CAPI const char *
ucnv_io_getConverterName(const char *alias, UBool *containsOption, UErrorCode *pErrorCode) {
const char *aliasTmp = alias;
int32_t i = 0;
for (i = 0; i < 2; i++) {
if (i == 1) {
if (aliasTmp[0] == 'x' && aliasTmp[1] == '-') {
aliasTmp = aliasTmp+2;
} else {
break;
}
}
if(haveAliasData(pErrorCode) && isAlias(aliasTmp, pErrorCode)) {
uint32_t convNum = findConverter(aliasTmp, containsOption, pErrorCode);
if (convNum < gMainTable.converterListSize) {
return GET_STRING(gMainTable.converterList[convNum]);
}
} else {
break;
}
}
return nullptr;
}
U_CDECL_BEGIN
static int32_t U_CALLCONV
ucnv_io_countStandardAliases(UEnumeration *enumerator, UErrorCode * ) {
int32_t value = 0;
UAliasContext *myContext = (UAliasContext *)(enumerator->context);
uint32_t listOffset = myContext->listOffset;
if (listOffset) {
value = gMainTable.taggedAliasLists[listOffset];
}
return value;
}
static const char * U_CALLCONV
ucnv_io_nextStandardAliases(UEnumeration *enumerator,
int32_t* resultLength,
UErrorCode * )
{
UAliasContext *myContext = (UAliasContext *)(enumerator->context);
uint32_t listOffset = myContext->listOffset;
if (listOffset) {
uint32_t listCount = gMainTable.taggedAliasLists[listOffset];
const uint16_t *currList = gMainTable.taggedAliasLists + listOffset + 1;
if (myContext->listIdx < listCount) {
const char *myStr = GET_STRING(currList[myContext->listIdx++]);
if (resultLength) {
*resultLength = (int32_t)uprv_strlen(myStr);
}
return myStr;
}
}
if (resultLength) {
*resultLength = 0;
}
return nullptr;
}
static void U_CALLCONV
ucnv_io_resetStandardAliases(UEnumeration *enumerator, UErrorCode * ) {
((UAliasContext *)(enumerator->context))->listIdx = 0;
}
static void U_CALLCONV
ucnv_io_closeUEnumeration(UEnumeration *enumerator) {
uprv_free(enumerator->context);
uprv_free(enumerator);
}
U_CDECL_END
static const UEnumeration gEnumAliases = {
nullptr,
nullptr,
ucnv_io_closeUEnumeration,
ucnv_io_countStandardAliases,
uenum_unextDefault,
ucnv_io_nextStandardAliases,
ucnv_io_resetStandardAliases
};
U_CAPI UEnumeration * U_EXPORT2
ucnv_openStandardNames(const char *convName,
const char *standard,
UErrorCode *pErrorCode)
{
UEnumeration *myEnum = nullptr;
if (haveAliasData(pErrorCode) && isAlias(convName, pErrorCode)) {
uint32_t listOffset = findTaggedAliasListsOffset(convName, standard, pErrorCode);
if (listOffset < gMainTable.taggedAliasListsSize) {
UAliasContext *myContext;
myEnum = static_cast<UEnumeration *>(uprv_malloc(sizeof(UEnumeration)));
if (myEnum == nullptr) {
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
return nullptr;
}
uprv_memcpy(myEnum, &gEnumAliases, sizeof(UEnumeration));
myContext = static_cast<UAliasContext *>(uprv_malloc(sizeof(UAliasContext)));
if (myContext == nullptr) {
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
uprv_free(myEnum);
return nullptr;
}
myContext->listOffset = listOffset;
myContext->listIdx = 0;
myEnum->context = myContext;
}
}
return myEnum;
}
static uint16_t
ucnv_io_countAliases(const char *alias, UErrorCode *pErrorCode) {
if(haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
uint32_t convNum = findConverter(alias, nullptr, pErrorCode);
if (convNum < gMainTable.converterListSize) {
int32_t listOffset = gMainTable.taggedAliasArray[(gMainTable.tagListSize - 1)*gMainTable.converterListSize + convNum];
if (listOffset) {
return gMainTable.taggedAliasLists[listOffset];
}
}
}
return 0;
}
static uint16_t
ucnv_io_getAliases(const char *alias, uint16_t start, const char **aliases, UErrorCode *pErrorCode) {
if(haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
uint32_t currAlias;
uint32_t convNum = findConverter(alias, nullptr, pErrorCode);
if (convNum < gMainTable.converterListSize) {
int32_t listOffset = gMainTable.taggedAliasArray[(gMainTable.tagListSize - 1)*gMainTable.converterListSize + convNum];
if (listOffset) {
uint32_t listCount = gMainTable.taggedAliasLists[listOffset];
const uint16_t *currList = gMainTable.taggedAliasLists + listOffset + 1;
for (currAlias = start; currAlias < listCount; currAlias++) {
aliases[currAlias] = GET_STRING(currList[currAlias]);
}
}
}
}
return 0;
}
static const char *
ucnv_io_getAlias(const char *alias, uint16_t n, UErrorCode *pErrorCode) {
if(haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
uint32_t convNum = findConverter(alias, nullptr, pErrorCode);
if (convNum < gMainTable.converterListSize) {
int32_t listOffset = gMainTable.taggedAliasArray[(gMainTable.tagListSize - 1)*gMainTable.converterListSize + convNum];
if (listOffset) {
uint32_t listCount = gMainTable.taggedAliasLists[listOffset];
const uint16_t *currList = gMainTable.taggedAliasLists + listOffset + 1;
if (n < listCount) {
return GET_STRING(currList[n]);
}
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
}
}
}
return nullptr;
}
static uint16_t
ucnv_io_countStandards(UErrorCode *pErrorCode) {
if (haveAliasData(pErrorCode)) {
return static_cast<uint16_t>(gMainTable.tagListSize - UCNV_NUM_HIDDEN_TAGS);
}
return 0;
}
U_CAPI const char * U_EXPORT2
ucnv_getStandard(uint16_t n, UErrorCode *pErrorCode) {
if (haveAliasData(pErrorCode)) {
if (n < gMainTable.tagListSize - UCNV_NUM_HIDDEN_TAGS) {
return GET_STRING(gMainTable.tagList[n]);
}
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
}
return nullptr;
}
U_CAPI const char * U_EXPORT2
ucnv_getStandardName(const char *alias, const char *standard, UErrorCode *pErrorCode) {
if (haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
uint32_t listOffset = findTaggedAliasListsOffset(alias, standard, pErrorCode);
if (0 < listOffset && listOffset < gMainTable.taggedAliasListsSize) {
const uint16_t *currList = gMainTable.taggedAliasLists + listOffset + 1;
if (currList[0]) {
return GET_STRING(currList[0]);
}
}
}
return nullptr;
}
U_CAPI uint16_t U_EXPORT2
ucnv_countAliases(const char *alias, UErrorCode *pErrorCode)
{
return ucnv_io_countAliases(alias, pErrorCode);
}
U_CAPI const char* U_EXPORT2
ucnv_getAlias(const char *alias, uint16_t n, UErrorCode *pErrorCode)
{
return ucnv_io_getAlias(alias, n, pErrorCode);
}
U_CAPI void U_EXPORT2
ucnv_getAliases(const char *alias, const char **aliases, UErrorCode *pErrorCode)
{
ucnv_io_getAliases(alias, 0, aliases, pErrorCode);
}
U_CAPI uint16_t U_EXPORT2
ucnv_countStandards()
{
UErrorCode err = U_ZERO_ERROR;
return ucnv_io_countStandards(&err);
}
U_CAPI const char * U_EXPORT2
ucnv_getCanonicalName(const char *alias, const char *standard, UErrorCode *pErrorCode) {
if (haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
uint32_t convNum = findTaggedConverterNum(alias, standard, pErrorCode);
if (convNum < gMainTable.converterListSize) {
return GET_STRING(gMainTable.converterList[convNum]);
}
}
return nullptr;
}
U_CDECL_BEGIN
static int32_t U_CALLCONV
ucnv_io_countAllConverters(UEnumeration * , UErrorCode * ) {
return gMainTable.converterListSize;
}
static const char * U_CALLCONV
ucnv_io_nextAllConverters(UEnumeration *enumerator,
int32_t* resultLength,
UErrorCode * )
{
uint16_t *myContext = (uint16_t *)(enumerator->context);
if (*myContext < gMainTable.converterListSize) {
const char *myStr = GET_STRING(gMainTable.converterList[(*myContext)++]);
if (resultLength) {
*resultLength = (int32_t)uprv_strlen(myStr);
}
return myStr;
}
if (resultLength) {
*resultLength = 0;
}
return nullptr;
}
static void U_CALLCONV
ucnv_io_resetAllConverters(UEnumeration *enumerator, UErrorCode * ) {
*((uint16_t *)(enumerator->context)) = 0;
}
U_CDECL_END
static const UEnumeration gEnumAllConverters = {
nullptr,
nullptr,
ucnv_io_closeUEnumeration,
ucnv_io_countAllConverters,
uenum_unextDefault,
ucnv_io_nextAllConverters,
ucnv_io_resetAllConverters
};
U_CAPI UEnumeration * U_EXPORT2
ucnv_openAllNames(UErrorCode *pErrorCode) {
UEnumeration *myEnum = nullptr;
if (haveAliasData(pErrorCode)) {
uint16_t *myContext;
myEnum = static_cast<UEnumeration *>(uprv_malloc(sizeof(UEnumeration)));
if (myEnum == nullptr) {
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
return nullptr;
}
uprv_memcpy(myEnum, &gEnumAllConverters, sizeof(UEnumeration));
myContext = static_cast<uint16_t *>(uprv_malloc(sizeof(uint16_t)));
if (myContext == nullptr) {
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
uprv_free(myEnum);
return nullptr;
}
*myContext = 0;
myEnum->context = myContext;
}
return myEnum;
}
U_CAPI uint16_t
ucnv_io_countKnownConverters(UErrorCode *pErrorCode) {
if (haveAliasData(pErrorCode)) {
return (uint16_t)gMainTable.converterListSize;
}
return 0;
}
U_CDECL_BEGIN
typedef char * U_CALLCONV StripForCompareFn(char *dst, const char *name);
U_CDECL_END
typedef struct TempRow {
uint16_t strIndex, sortIndex;
} TempRow;
typedef struct TempAliasTable {
const char *chars;
TempRow *rows;
uint16_t *resort;
StripForCompareFn *stripForCompare;
} TempAliasTable;
enum {
STACK_ROW_CAPACITY=500
};
static int32_t U_CALLCONV
io_compareRows(const void *context, const void *left, const void *right) {
char strippedLeft[UCNV_MAX_CONVERTER_NAME_LENGTH],
strippedRight[UCNV_MAX_CONVERTER_NAME_LENGTH];
TempAliasTable *tempTable=(TempAliasTable *)context;
const char *chars=tempTable->chars;
return static_cast<int32_t>(uprv_strcmp(
tempTable->stripForCompare(strippedLeft, chars + 2 * static_cast<const TempRow*>(left)->strIndex),
tempTable->stripForCompare(strippedRight, chars + 2 * static_cast<const TempRow*>(right)->strIndex)));
}
U_CAPI int32_t U_EXPORT2
ucnv_swapAliases(const UDataSwapper *ds,
const void *inData, int32_t length, void *outData,
UErrorCode *pErrorCode) {
const UDataInfo *pInfo;
int32_t headerSize;
const uint16_t *inTable;
const uint32_t *inSectionSizes;
uint32_t toc[offsetsCount];
uint32_t offsets[offsetsCount];
uint32_t i, count, tocLength, topOffset;
TempRow rows[STACK_ROW_CAPACITY];
uint16_t resort[STACK_ROW_CAPACITY];
TempAliasTable tempTable;
headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
return 0;
}
pInfo=(const UDataInfo *)((const char *)inData+4);
if(!(
pInfo->dataFormat[0]==0x43 &&
pInfo->dataFormat[1]==0x76 &&
pInfo->dataFormat[2]==0x41 &&
pInfo->dataFormat[3]==0x6c &&
pInfo->formatVersion[0]==3
)) {
udata_printError(ds, "ucnv_swapAliases(): data format %02x.%02x.%02x.%02x (format version %02x) is not an alias table\n",
pInfo->dataFormat[0], pInfo->dataFormat[1],
pInfo->dataFormat[2], pInfo->dataFormat[3],
pInfo->formatVersion[0]);
*pErrorCode=U_UNSUPPORTED_ERROR;
return 0;
}
if(length>=0 && (length-headerSize)<4*(1+minTocLength)) {
udata_printError(ds, "ucnv_swapAliases(): too few bytes (%d after header) for an alias table\n",
length-headerSize);
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
inSectionSizes=(const uint32_t *)((const char *)inData+headerSize);
inTable=(const uint16_t *)inSectionSizes;
uprv_memset(toc, 0, sizeof(toc));
toc[tocLengthIndex]=tocLength=ds->readUInt32(inSectionSizes[tocLengthIndex]);
if(tocLength<minTocLength || offsetsCount<=tocLength) {
udata_printError(ds, "ucnv_swapAliases(): table of contents contains unsupported number of sections (%u sections)\n", tocLength);
*pErrorCode=U_INVALID_FORMAT_ERROR;
return 0;
}
for(i=converterListIndex; i<=tocLength; ++i) {
toc[i]=ds->readUInt32(inSectionSizes[i]);
}
uprv_memset(offsets, 0, sizeof(offsets));
offsets[converterListIndex]=2*(1+tocLength);
for(i=tagListIndex; i<=tocLength; ++i) {
offsets[i]=offsets[i-1]+toc[i-1];
}
topOffset=offsets[i-1]+toc[i-1];
if(length>=0) {
uint16_t *outTable;
const uint16_t *p, *p2;
uint16_t *q, *q2;
uint16_t oldIndex;
if((length-headerSize)<(2*(int32_t)topOffset)) {
udata_printError(ds, "ucnv_swapAliases(): too few bytes (%d after header) for an alias table\n",
length-headerSize);
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
outTable=(uint16_t *)((char *)outData+headerSize);
ds->swapArray32(ds, inTable, 4*(1+tocLength), outTable, pErrorCode);
ds->swapInvChars(ds, inTable+offsets[stringTableIndex], 2*(int32_t)(toc[stringTableIndex]+toc[normalizedStringTableIndex]),
outTable+offsets[stringTableIndex], pErrorCode);
if(U_FAILURE(*pErrorCode)) {
udata_printError(ds, "ucnv_swapAliases().swapInvChars(charset names) failed\n");
return 0;
}
if(ds->inCharset==ds->outCharset) {
ds->swapArray16(ds,
inTable+offsets[converterListIndex],
2*(int32_t)(offsets[stringTableIndex]-offsets[converterListIndex]),
outTable+offsets[converterListIndex],
pErrorCode);
} else {
count=toc[aliasListIndex];
tempTable.chars=(const char *)(outTable+offsets[stringTableIndex]);
if(count<=STACK_ROW_CAPACITY) {
tempTable.rows=rows;
tempTable.resort=resort;
} else {
tempTable.rows=(TempRow *)uprv_malloc(count*sizeof(TempRow)+count*2);
if(tempTable.rows==nullptr) {
udata_printError(ds, "ucnv_swapAliases(): unable to allocate memory for sorting tables (max length: %u)\n",
count);
*pErrorCode=U_MEMORY_ALLOCATION_ERROR;
return 0;
}
tempTable.resort=(uint16_t *)(tempTable.rows+count);
}
if(ds->outCharset==U_ASCII_FAMILY) {
tempTable.stripForCompare=ucnv_io_stripASCIIForCompare;
} else {
tempTable.stripForCompare=ucnv_io_stripEBCDICForCompare;
}
p=inTable+offsets[aliasListIndex];
q=outTable+offsets[aliasListIndex];
p2=inTable+offsets[untaggedConvArrayIndex];
q2=outTable+offsets[untaggedConvArrayIndex];
for(i=0; i<count; ++i) {
tempTable.rows[i].strIndex=ds->readUInt16(p[i]);
tempTable.rows[i].sortIndex=(uint16_t)i;
}
uprv_sortArray(tempTable.rows, (int32_t)count, sizeof(TempRow),
io_compareRows, &tempTable,
false, pErrorCode);
if(U_SUCCESS(*pErrorCode)) {
if(p!=q) {
for(i=0; i<count; ++i) {
oldIndex=tempTable.rows[i].sortIndex;
ds->swapArray16(ds, p+oldIndex, 2, q+i, pErrorCode);
ds->swapArray16(ds, p2+oldIndex, 2, q2+i, pErrorCode);
}
} else {
uint16_t *r=tempTable.resort;
for(i=0; i<count; ++i) {
oldIndex=tempTable.rows[i].sortIndex;
ds->swapArray16(ds, p+oldIndex, 2, r+i, pErrorCode);
}
uprv_memcpy(q, r, 2*(size_t)count);
for(i=0; i<count; ++i) {
oldIndex=tempTable.rows[i].sortIndex;
ds->swapArray16(ds, p2+oldIndex, 2, r+i, pErrorCode);
}
uprv_memcpy(q2, r, 2*(size_t)count);
}
}
if(tempTable.rows!=rows) {
uprv_free(tempTable.rows);
}
if(U_FAILURE(*pErrorCode)) {
udata_printError(ds, "ucnv_swapAliases().uprv_sortArray(%u items) failed\n",
count);
return 0;
}
ds->swapArray16(ds,
inTable+offsets[converterListIndex],
2*(int32_t)(offsets[aliasListIndex]-offsets[converterListIndex]),
outTable+offsets[converterListIndex],
pErrorCode);
ds->swapArray16(ds,
inTable+offsets[taggedAliasArrayIndex],
2*(int32_t)(offsets[stringTableIndex]-offsets[taggedAliasArrayIndex]),
outTable+offsets[taggedAliasArrayIndex],
pErrorCode);
}
}
return headerSize+2*(int32_t)topOffset;
}
#endif