intl/icu/source/common/ucnv_io.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/ucnv_io.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1347 @@
     1.4 +/*
     1.5 +******************************************************************************
     1.6 +*
     1.7 +*   Copyright (C) 1999-2013, International Business Machines
     1.8 +*   Corporation and others.  All Rights Reserved.
     1.9 +*
    1.10 +******************************************************************************
    1.11 +*
    1.12 +*
    1.13 +*  ucnv_io.cpp:
    1.14 +*  initializes global variables and defines functions pertaining to converter 
    1.15 +*  name resolution aspect of the conversion code.
    1.16 +*
    1.17 +*   new implementation:
    1.18 +*
    1.19 +*   created on: 1999nov22
    1.20 +*   created by: Markus W. Scherer
    1.21 +*
    1.22 +*   Use the binary cnvalias.icu (created from convrtrs.txt) to work
    1.23 +*   with aliases for converter names.
    1.24 +*
    1.25 +*   Date        Name        Description
    1.26 +*   11/22/1999  markus      Created
    1.27 +*   06/28/2002  grhoten     Major overhaul of the converter alias design.
    1.28 +*                           Now an alias can map to different converters
    1.29 +*                           depending on the specified standard.
    1.30 +*******************************************************************************
    1.31 +*/
    1.32 +
    1.33 +#include "unicode/utypes.h"
    1.34 +
    1.35 +#if !UCONFIG_NO_CONVERSION
    1.36 +
    1.37 +#include "unicode/ucnv.h"
    1.38 +#include "unicode/udata.h"
    1.39 +
    1.40 +#include "umutex.h"
    1.41 +#include "uarrsort.h"
    1.42 +#include "uassert.h"
    1.43 +#include "udataswp.h"
    1.44 +#include "cstring.h"
    1.45 +#include "cmemory.h"
    1.46 +#include "ucnv_io.h"
    1.47 +#include "uenumimp.h"
    1.48 +#include "ucln_cmn.h"
    1.49 +
    1.50 +/* Format of cnvalias.icu -----------------------------------------------------
    1.51 + *
    1.52 + * cnvalias.icu is a binary, memory-mappable form of convrtrs.txt.
    1.53 + * This binary form contains several tables. All indexes are to uint16_t
    1.54 + * units, and not to the bytes (uint8_t units). Addressing everything on
    1.55 + * 16-bit boundaries allows us to store more information with small index
    1.56 + * numbers, which are also 16-bit in size. The majority of the table (except
    1.57 + * the string table) are 16-bit numbers.
    1.58 + *
    1.59 + * First there is the size of the Table of Contents (TOC). The TOC
    1.60 + * entries contain the size of each section. In order to find the offset
    1.61 + * you just need to sum up the previous offsets.
    1.62 + * The TOC length and entries are an array of uint32_t values.
    1.63 + * The first section after the TOC starts immediately after the TOC.
    1.64 + *
    1.65 + * 1) This section contains a list of converters. This list contains indexes
    1.66 + * into the string table for the converter name. The index of this list is
    1.67 + * also used by other sections, which are mentioned later on.
    1.68 + * This list is not sorted.
    1.69 + *
    1.70 + * 2) This section contains a list of tags. This list contains indexes
    1.71 + * into the string table for the tag name. The index of this list is
    1.72 + * also used by other sections, which are mentioned later on.
    1.73 + * This list is in priority order of standards.
    1.74 + *
    1.75 + * 3) This section contains a list of sorted unique aliases. This
    1.76 + * list contains indexes into the string table for the alias name. The
    1.77 + * index of this list is also used by other sections, like the 4th section.
    1.78 + * The index for the 3rd and 4th section is used to get the
    1.79 + * alias -> converter name mapping. Section 3 and 4 form a two column table.
    1.80 + * Some of the most significant bits of each index may contain other
    1.81 + * information (see findConverter for details).
    1.82 + *
    1.83 + * 4) This section contains a list of mapped converter names. Consider this
    1.84 + * as a table that maps the 3rd section to the 1st section. This list contains
    1.85 + * indexes into the 1st section. The index of this list is the same index in
    1.86 + * the 3rd section. There is also some extra information in the high bits of
    1.87 + * each converter index in this table. Currently it's only used to say that
    1.88 + * an alias mapped to this converter is ambiguous. See UCNV_CONVERTER_INDEX_MASK
    1.89 + * and UCNV_AMBIGUOUS_ALIAS_MAP_BIT for more information. This section is
    1.90 + * the predigested form of the 5th section so that an alias lookup can be fast.
    1.91 + *
    1.92 + * 5) This section contains a 2D array with indexes to the 6th section. This
    1.93 + * section is the full form of all alias mappings. The column index is the
    1.94 + * index into the converter list (column header). The row index is the index
    1.95 + * to tag list (row header). This 2D array is the top part a 3D array. The
    1.96 + * third dimension is in the 6th section.
    1.97 + *
    1.98 + * 6) This is blob of variable length arrays. Each array starts with a size,
    1.99 + * and is followed by indexes to alias names in the string table. This is
   1.100 + * the third dimension to the section 5. No other section should be referencing
   1.101 + * this section.
   1.102 + *
   1.103 + * 7) Starting in ICU 3.6, this can be a UConverterAliasOptions struct. Its
   1.104 + * presence indicates that a section 9 exists. UConverterAliasOptions specifies
   1.105 + * what type of string normalization is used among other potential things in the
   1.106 + * future.
   1.107 + *
   1.108 + * 8) This is the string table. All strings are indexed on an even address.
   1.109 + * There are two reasons for this. First many chip architectures locate strings
   1.110 + * faster on even address boundaries. Second, since all indexes are 16-bit
   1.111 + * numbers, this string table can be 128KB in size instead of 64KB when we
   1.112 + * only have strings starting on an even address.
   1.113 + *
   1.114 + * 9) When present this is a set of prenormalized strings from section 8. This
   1.115 + * table contains normalized strings with the dashes and spaces stripped out,
   1.116 + * and all strings lowercased. In the future, the options in section 7 may state
   1.117 + * other types of normalization.
   1.118 + *
   1.119 + * Here is the concept of section 5 and 6. It's a 3D cube. Each tag
   1.120 + * has a unique alias among all converters. That same alias can
   1.121 + * be mentioned in other standards on different converters,
   1.122 + * but only one alias per tag can be unique.
   1.123 + *
   1.124 + *
   1.125 + *              Converter Names (Usually in TR22 form)
   1.126 + *           -------------------------------------------.
   1.127 + *     T    /                                          /|
   1.128 + *     a   /                                          / |
   1.129 + *     g  /                                          /  |
   1.130 + *     s /                                          /   |
   1.131 + *      /                                          /    |
   1.132 + *      ------------------------------------------/     |
   1.133 + *    A |                                         |     |
   1.134 + *    l |                                         |     |
   1.135 + *    i |                                         |    /
   1.136 + *    a |                                         |   /
   1.137 + *    s |                                         |  /
   1.138 + *    e |                                         | /
   1.139 + *    s |                                         |/
   1.140 + *      -------------------------------------------
   1.141 + *
   1.142 + *
   1.143 + *
   1.144 + * Here is what it really looks like. It's like swiss cheese.
   1.145 + * There are holes. Some converters aren't recognized by
   1.146 + * a standard, or they are really old converters that the
   1.147 + * standard doesn't recognize anymore.
   1.148 + *
   1.149 + *              Converter Names (Usually in TR22 form)
   1.150 + *           -------------------------------------------.
   1.151 + *     T    /##########################################/|
   1.152 + *     a   /     #            #                       /#
   1.153 + *     g  /  #      ##     ##     ### # ### ### ### #/
   1.154 + *     s / #             #####  ####        ##  ## #/#
   1.155 + *      / ### # # ##  #  #   #          ### # #   #/##
   1.156 + *      ------------------------------------------/# #
   1.157 + *    A |### # # ##  #  #   #          ### # #   #|# #
   1.158 + *    l |# # #    #     #               ## #     #|# #
   1.159 + *    i |# # #    #     #                #       #|#
   1.160 + *    a |#                                       #|#
   1.161 + *    s |                                        #|#
   1.162 + *    e
   1.163 + *    s
   1.164 + *
   1.165 + */
   1.166 +
   1.167 +/**
   1.168 + * Used by the UEnumeration API
   1.169 + */
   1.170 +typedef struct UAliasContext {
   1.171 +    uint32_t listOffset;
   1.172 +    uint32_t listIdx;
   1.173 +} UAliasContext;
   1.174 +
   1.175 +static const char DATA_NAME[] = "cnvalias";
   1.176 +static const char DATA_TYPE[] = "icu";
   1.177 +
   1.178 +static UDataMemory *gAliasData=NULL;
   1.179 +static icu::UInitOnce gAliasDataInitOnce = U_INITONCE_INITIALIZER;
   1.180 +
   1.181 +enum {
   1.182 +    tocLengthIndex=0,
   1.183 +    converterListIndex=1,
   1.184 +    tagListIndex=2,
   1.185 +    aliasListIndex=3,
   1.186 +    untaggedConvArrayIndex=4,
   1.187 +    taggedAliasArrayIndex=5,
   1.188 +    taggedAliasListsIndex=6,
   1.189 +    tableOptionsIndex=7,
   1.190 +    stringTableIndex=8,
   1.191 +    normalizedStringTableIndex=9,
   1.192 +    offsetsCount,    /* length of the swapper's temporary offsets[] */
   1.193 +    minTocLength=8 /* min. tocLength in the file, does not count the tocLengthIndex! */
   1.194 +};
   1.195 +
   1.196 +static const UConverterAliasOptions defaultTableOptions = {
   1.197 +    UCNV_IO_UNNORMALIZED,
   1.198 +    0 /* containsCnvOptionInfo */
   1.199 +};
   1.200 +static UConverterAlias gMainTable;
   1.201 +
   1.202 +#define GET_STRING(idx) (const char *)(gMainTable.stringTable + (idx))
   1.203 +#define GET_NORMALIZED_STRING(idx) (const char *)(gMainTable.normalizedStringTable + (idx))
   1.204 +
   1.205 +static UBool U_CALLCONV
   1.206 +isAcceptable(void * /*context*/,
   1.207 +             const char * /*type*/, const char * /*name*/,
   1.208 +             const UDataInfo *pInfo) {
   1.209 +    return (UBool)(
   1.210 +        pInfo->size>=20 &&
   1.211 +        pInfo->isBigEndian==U_IS_BIG_ENDIAN &&
   1.212 +        pInfo->charsetFamily==U_CHARSET_FAMILY &&
   1.213 +        pInfo->dataFormat[0]==0x43 &&   /* dataFormat="CvAl" */
   1.214 +        pInfo->dataFormat[1]==0x76 &&
   1.215 +        pInfo->dataFormat[2]==0x41 &&
   1.216 +        pInfo->dataFormat[3]==0x6c &&
   1.217 +        pInfo->formatVersion[0]==3);
   1.218 +}
   1.219 +
   1.220 +static UBool U_CALLCONV ucnv_io_cleanup(void)
   1.221 +{
   1.222 +    if (gAliasData) {
   1.223 +        udata_close(gAliasData);
   1.224 +        gAliasData = NULL;
   1.225 +    }
   1.226 +    gAliasDataInitOnce.reset();
   1.227 +
   1.228 +    uprv_memset(&gMainTable, 0, sizeof(gMainTable));
   1.229 +
   1.230 +    return TRUE;                   /* Everything was cleaned up */
   1.231 +}
   1.232 +
   1.233 +static void U_CALLCONV initAliasData(UErrorCode &errCode) {
   1.234 +    UDataMemory *data;
   1.235 +    const uint16_t *table;
   1.236 +    const uint32_t *sectionSizes;
   1.237 +    uint32_t tableStart;
   1.238 +    uint32_t currOffset;
   1.239 +
   1.240 +    ucln_common_registerCleanup(UCLN_COMMON_UCNV_IO, ucnv_io_cleanup);
   1.241 +
   1.242 +    U_ASSERT(gAliasData == NULL);
   1.243 +    data = udata_openChoice(NULL, DATA_TYPE, DATA_NAME, isAcceptable, NULL, &errCode);
   1.244 +    if(U_FAILURE(errCode)) {
   1.245 +        return;
   1.246 +    }
   1.247 +
   1.248 +    sectionSizes = (const uint32_t *)udata_getMemory(data);
   1.249 +    table = (const uint16_t *)sectionSizes;
   1.250 +
   1.251 +    tableStart      = sectionSizes[0];
   1.252 +    if (tableStart < minTocLength) {
   1.253 +        errCode = U_INVALID_FORMAT_ERROR;
   1.254 +        udata_close(data);
   1.255 +        return;
   1.256 +    }
   1.257 +    gAliasData = data;
   1.258 +
   1.259 +    gMainTable.converterListSize      = sectionSizes[1];
   1.260 +    gMainTable.tagListSize            = sectionSizes[2];
   1.261 +    gMainTable.aliasListSize          = sectionSizes[3];
   1.262 +    gMainTable.untaggedConvArraySize  = sectionSizes[4];
   1.263 +    gMainTable.taggedAliasArraySize   = sectionSizes[5];
   1.264 +    gMainTable.taggedAliasListsSize   = sectionSizes[6];
   1.265 +    gMainTable.optionTableSize        = sectionSizes[7];
   1.266 +    gMainTable.stringTableSize        = sectionSizes[8];
   1.267 +
   1.268 +    if (tableStart > 8) {
   1.269 +        gMainTable.normalizedStringTableSize = sectionSizes[9];
   1.270 +    }
   1.271 +
   1.272 +    currOffset = tableStart * (sizeof(uint32_t)/sizeof(uint16_t)) + (sizeof(uint32_t)/sizeof(uint16_t));
   1.273 +    gMainTable.converterList = table + currOffset;
   1.274 +
   1.275 +    currOffset += gMainTable.converterListSize;
   1.276 +    gMainTable.tagList = table + currOffset;
   1.277 +
   1.278 +    currOffset += gMainTable.tagListSize;
   1.279 +    gMainTable.aliasList = table + currOffset;
   1.280 +
   1.281 +    currOffset += gMainTable.aliasListSize;
   1.282 +    gMainTable.untaggedConvArray = table + currOffset;
   1.283 +
   1.284 +    currOffset += gMainTable.untaggedConvArraySize;
   1.285 +    gMainTable.taggedAliasArray = table + currOffset;
   1.286 +
   1.287 +    /* aliasLists is a 1's based array, but it has a padding character */
   1.288 +    currOffset += gMainTable.taggedAliasArraySize;
   1.289 +    gMainTable.taggedAliasLists = table + currOffset;
   1.290 +
   1.291 +    currOffset += gMainTable.taggedAliasListsSize;
   1.292 +    if (gMainTable.optionTableSize > 0
   1.293 +        && ((const UConverterAliasOptions *)(table + currOffset))->stringNormalizationType < UCNV_IO_NORM_TYPE_COUNT)
   1.294 +    {
   1.295 +        /* Faster table */
   1.296 +        gMainTable.optionTable = (const UConverterAliasOptions *)(table + currOffset);
   1.297 +    }
   1.298 +    else {
   1.299 +        /* Smaller table, or I can't handle this normalization mode!
   1.300 +        Use the original slower table lookup. */
   1.301 +        gMainTable.optionTable = &defaultTableOptions;
   1.302 +    }
   1.303 +
   1.304 +    currOffset += gMainTable.optionTableSize;
   1.305 +    gMainTable.stringTable = table + currOffset;
   1.306 +
   1.307 +    currOffset += gMainTable.stringTableSize;
   1.308 +    gMainTable.normalizedStringTable = ((gMainTable.optionTable->stringNormalizationType == UCNV_IO_UNNORMALIZED)
   1.309 +        ? gMainTable.stringTable : (table + currOffset));
   1.310 +}
   1.311 +
   1.312 +
   1.313 +static UBool
   1.314 +haveAliasData(UErrorCode *pErrorCode) {
   1.315 +    umtx_initOnce(gAliasDataInitOnce, &initAliasData, *pErrorCode);
   1.316 +    return U_SUCCESS(*pErrorCode);
   1.317 +}
   1.318 +
   1.319 +static inline UBool
   1.320 +isAlias(const char *alias, UErrorCode *pErrorCode) {
   1.321 +    if(alias==NULL) {
   1.322 +        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
   1.323 +        return FALSE;
   1.324 +    }
   1.325 +    return (UBool)(*alias!=0);
   1.326 +}
   1.327 +
   1.328 +static uint32_t getTagNumber(const char *tagname) {
   1.329 +    if (gMainTable.tagList) {
   1.330 +        uint32_t tagNum;
   1.331 +        for (tagNum = 0; tagNum < gMainTable.tagListSize; tagNum++) {
   1.332 +            if (!uprv_stricmp(GET_STRING(gMainTable.tagList[tagNum]), tagname)) {
   1.333 +                return tagNum;
   1.334 +            }
   1.335 +        }
   1.336 +    }
   1.337 +
   1.338 +    return UINT32_MAX;
   1.339 +}
   1.340 +
   1.341 +/* character types relevant for ucnv_compareNames() */
   1.342 +enum {
   1.343 +    UIGNORE,
   1.344 +    ZERO,
   1.345 +    NONZERO,
   1.346 +    MINLETTER /* any values from here on are lowercase letter mappings */
   1.347 +};
   1.348 +
   1.349 +/* character types for ASCII 00..7F */
   1.350 +static const uint8_t asciiTypes[128] = {
   1.351 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1.352 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1.353 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1.354 +    ZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, 0, 0, 0, 0, 0, 0,
   1.355 +    0, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
   1.356 +    0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0, 0, 0, 0, 0,
   1.357 +    0, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
   1.358 +    0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0, 0, 0, 0, 0
   1.359 +};
   1.360 +
   1.361 +#define GET_ASCII_TYPE(c) ((int8_t)(c) >= 0 ? asciiTypes[(uint8_t)c] : (uint8_t)UIGNORE)
   1.362 +
   1.363 +/* character types for EBCDIC 80..FF */
   1.364 +static const uint8_t ebcdicTypes[128] = {
   1.365 +    0,    0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0, 0, 0, 0, 0, 0,
   1.366 +    0,    0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0, 0, 0, 0, 0, 0,
   1.367 +    0,    0,    0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0, 0, 0, 0, 0, 0,
   1.368 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1.369 +    0,    0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0, 0, 0, 0, 0, 0,
   1.370 +    0,    0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0, 0, 0, 0, 0, 0,
   1.371 +    0,    0,    0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0, 0, 0, 0, 0, 0,
   1.372 +    ZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, NONZERO, 0, 0, 0, 0, 0, 0
   1.373 +};
   1.374 +
   1.375 +#define GET_EBCDIC_TYPE(c) ((int8_t)(c) < 0 ? ebcdicTypes[(c)&0x7f] : (uint8_t)UIGNORE)
   1.376 +
   1.377 +#if U_CHARSET_FAMILY==U_ASCII_FAMILY
   1.378 +#   define GET_CHAR_TYPE(c) GET_ASCII_TYPE(c)
   1.379 +#elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
   1.380 +#   define GET_CHAR_TYPE(c) GET_EBCDIC_TYPE(c)
   1.381 +#else
   1.382 +#   error U_CHARSET_FAMILY is not valid
   1.383 +#endif
   1.384 +
   1.385 +/* @see ucnv_compareNames */
   1.386 +U_CFUNC char * U_EXPORT2
   1.387 +ucnv_io_stripASCIIForCompare(char *dst, const char *name) {
   1.388 +    char *dstItr = dst;
   1.389 +    uint8_t type, nextType;
   1.390 +    char c1;
   1.391 +    UBool afterDigit = FALSE;
   1.392 +
   1.393 +    while ((c1 = *name++) != 0) {
   1.394 +        type = GET_ASCII_TYPE(c1);
   1.395 +        switch (type) {
   1.396 +        case UIGNORE:
   1.397 +            afterDigit = FALSE;
   1.398 +            continue; /* ignore all but letters and digits */
   1.399 +        case ZERO:
   1.400 +            if (!afterDigit) {
   1.401 +                nextType = GET_ASCII_TYPE(*name);
   1.402 +                if (nextType == ZERO || nextType == NONZERO) {
   1.403 +                    continue; /* ignore leading zero before another digit */
   1.404 +                }
   1.405 +            }
   1.406 +            break;
   1.407 +        case NONZERO:
   1.408 +            afterDigit = TRUE;
   1.409 +            break;
   1.410 +        default:
   1.411 +            c1 = (char)type; /* lowercased letter */
   1.412 +            afterDigit = FALSE;
   1.413 +            break;
   1.414 +        }
   1.415 +        *dstItr++ = c1;
   1.416 +    }
   1.417 +    *dstItr = 0;
   1.418 +    return dst;
   1.419 +}
   1.420 +
   1.421 +U_CFUNC char * U_EXPORT2
   1.422 +ucnv_io_stripEBCDICForCompare(char *dst, const char *name) {
   1.423 +    char *dstItr = dst;
   1.424 +    uint8_t type, nextType;
   1.425 +    char c1;
   1.426 +    UBool afterDigit = FALSE;
   1.427 +
   1.428 +    while ((c1 = *name++) != 0) {
   1.429 +        type = GET_EBCDIC_TYPE(c1);
   1.430 +        switch (type) {
   1.431 +        case UIGNORE:
   1.432 +            afterDigit = FALSE;
   1.433 +            continue; /* ignore all but letters and digits */
   1.434 +        case ZERO:
   1.435 +            if (!afterDigit) {
   1.436 +                nextType = GET_EBCDIC_TYPE(*name);
   1.437 +                if (nextType == ZERO || nextType == NONZERO) {
   1.438 +                    continue; /* ignore leading zero before another digit */
   1.439 +                }
   1.440 +            }
   1.441 +            break;
   1.442 +        case NONZERO:
   1.443 +            afterDigit = TRUE;
   1.444 +            break;
   1.445 +        default:
   1.446 +            c1 = (char)type; /* lowercased letter */
   1.447 +            afterDigit = FALSE;
   1.448 +            break;
   1.449 +        }
   1.450 +        *dstItr++ = c1;
   1.451 +    }
   1.452 +    *dstItr = 0;
   1.453 +    return dst;
   1.454 +}
   1.455 +
   1.456 +/**
   1.457 + * Do a fuzzy compare of two converter/alias names.
   1.458 + * The comparison is case-insensitive, ignores leading zeroes if they are not
   1.459 + * followed by further digits, and ignores all but letters and digits.
   1.460 + * Thus the strings "UTF-8", "utf_8", "u*T@f08" and "Utf 8" are exactly equivalent.
   1.461 + * See section 1.4, Charset Alias Matching in Unicode Technical Standard #22
   1.462 + * at http://www.unicode.org/reports/tr22/
   1.463 + *
   1.464 + * This is a symmetrical (commutative) operation; order of arguments
   1.465 + * is insignificant.  This is an important property for sorting the
   1.466 + * list (when the list is preprocessed into binary form) and for
   1.467 + * performing binary searches on it at run time.
   1.468 + *
   1.469 + * @param name1 a converter name or alias, zero-terminated
   1.470 + * @param name2 a converter name or alias, zero-terminated
   1.471 + * @return 0 if the names match, or a negative value if the name1
   1.472 + * lexically precedes name2, or a positive value if the name1
   1.473 + * lexically follows name2.
   1.474 + *
   1.475 + * @see ucnv_io_stripForCompare
   1.476 + */
   1.477 +U_CAPI int U_EXPORT2
   1.478 +ucnv_compareNames(const char *name1, const char *name2) {
   1.479 +    int rc;
   1.480 +    uint8_t type, nextType;
   1.481 +    char c1, c2;
   1.482 +    UBool afterDigit1 = FALSE, afterDigit2 = FALSE;
   1.483 +
   1.484 +    for (;;) {
   1.485 +        while ((c1 = *name1++) != 0) {
   1.486 +            type = GET_CHAR_TYPE(c1);
   1.487 +            switch (type) {
   1.488 +            case UIGNORE:
   1.489 +                afterDigit1 = FALSE;
   1.490 +                continue; /* ignore all but letters and digits */
   1.491 +            case ZERO:
   1.492 +                if (!afterDigit1) {
   1.493 +                    nextType = GET_CHAR_TYPE(*name1);
   1.494 +                    if (nextType == ZERO || nextType == NONZERO) {
   1.495 +                        continue; /* ignore leading zero before another digit */
   1.496 +                    }
   1.497 +                }
   1.498 +                break;
   1.499 +            case NONZERO:
   1.500 +                afterDigit1 = TRUE;
   1.501 +                break;
   1.502 +            default:
   1.503 +                c1 = (char)type; /* lowercased letter */
   1.504 +                afterDigit1 = FALSE;
   1.505 +                break;
   1.506 +            }
   1.507 +            break; /* deliver c1 */
   1.508 +        }
   1.509 +        while ((c2 = *name2++) != 0) {
   1.510 +            type = GET_CHAR_TYPE(c2);
   1.511 +            switch (type) {
   1.512 +            case UIGNORE:
   1.513 +                afterDigit2 = FALSE;
   1.514 +                continue; /* ignore all but letters and digits */
   1.515 +            case ZERO:
   1.516 +                if (!afterDigit2) {
   1.517 +                    nextType = GET_CHAR_TYPE(*name2);
   1.518 +                    if (nextType == ZERO || nextType == NONZERO) {
   1.519 +                        continue; /* ignore leading zero before another digit */
   1.520 +                    }
   1.521 +                }
   1.522 +                break;
   1.523 +            case NONZERO:
   1.524 +                afterDigit2 = TRUE;
   1.525 +                break;
   1.526 +            default:
   1.527 +                c2 = (char)type; /* lowercased letter */
   1.528 +                afterDigit2 = FALSE;
   1.529 +                break;
   1.530 +            }
   1.531 +            break; /* deliver c2 */
   1.532 +        }
   1.533 +
   1.534 +        /* If we reach the ends of both strings then they match */
   1.535 +        if ((c1|c2)==0) {
   1.536 +            return 0;
   1.537 +        }
   1.538 +
   1.539 +        /* Case-insensitive comparison */
   1.540 +        rc = (int)(unsigned char)c1 - (int)(unsigned char)c2;
   1.541 +        if (rc != 0) {
   1.542 +            return rc;
   1.543 +        }
   1.544 +    }
   1.545 +}
   1.546 +
   1.547 +/*
   1.548 + * search for an alias
   1.549 + * return the converter number index for gConverterList
   1.550 + */
   1.551 +static inline uint32_t
   1.552 +findConverter(const char *alias, UBool *containsOption, UErrorCode *pErrorCode) {
   1.553 +    uint32_t mid, start, limit;
   1.554 +    uint32_t lastMid;
   1.555 +    int result;
   1.556 +    int isUnnormalized = (gMainTable.optionTable->stringNormalizationType == UCNV_IO_UNNORMALIZED);
   1.557 +    char strippedName[UCNV_MAX_CONVERTER_NAME_LENGTH];
   1.558 +
   1.559 +    if (!isUnnormalized) {
   1.560 +        if (uprv_strlen(alias) >= UCNV_MAX_CONVERTER_NAME_LENGTH) {
   1.561 +            *pErrorCode = U_BUFFER_OVERFLOW_ERROR;
   1.562 +            return UINT32_MAX;
   1.563 +        }
   1.564 +
   1.565 +        /* Lower case and remove ignoreable characters. */
   1.566 +        ucnv_io_stripForCompare(strippedName, alias);
   1.567 +        alias = strippedName;
   1.568 +    }
   1.569 +
   1.570 +    /* do a binary search for the alias */
   1.571 +    start = 0;
   1.572 +    limit = gMainTable.untaggedConvArraySize;
   1.573 +    mid = limit;
   1.574 +    lastMid = UINT32_MAX;
   1.575 +
   1.576 +    for (;;) {
   1.577 +        mid = (uint32_t)((start + limit) / 2);
   1.578 +        if (lastMid == mid) {   /* Have we moved? */
   1.579 +            break;  /* We haven't moved, and it wasn't found. */
   1.580 +        }
   1.581 +        lastMid = mid;
   1.582 +        if (isUnnormalized) {
   1.583 +            result = ucnv_compareNames(alias, GET_STRING(gMainTable.aliasList[mid]));
   1.584 +        }
   1.585 +        else {
   1.586 +            result = uprv_strcmp(alias, GET_NORMALIZED_STRING(gMainTable.aliasList[mid]));
   1.587 +        }
   1.588 +
   1.589 +        if (result < 0) {
   1.590 +            limit = mid;
   1.591 +        } else if (result > 0) {
   1.592 +            start = mid;
   1.593 +        } else {
   1.594 +            /* Since the gencnval tool folds duplicates into one entry,
   1.595 +             * this alias in gAliasList is unique, but different standards
   1.596 +             * may map an alias to different converters.
   1.597 +             */
   1.598 +            if (gMainTable.untaggedConvArray[mid] & UCNV_AMBIGUOUS_ALIAS_MAP_BIT) {
   1.599 +                *pErrorCode = U_AMBIGUOUS_ALIAS_WARNING;
   1.600 +            }
   1.601 +            /* State whether the canonical converter name contains an option.
   1.602 +            This information is contained in this list in order to maintain backward & forward compatibility. */
   1.603 +            if (containsOption) {
   1.604 +                UBool containsCnvOptionInfo = (UBool)gMainTable.optionTable->containsCnvOptionInfo;
   1.605 +                *containsOption = (UBool)((containsCnvOptionInfo
   1.606 +                    && ((gMainTable.untaggedConvArray[mid] & UCNV_CONTAINS_OPTION_BIT) != 0))
   1.607 +                    || !containsCnvOptionInfo);
   1.608 +            }
   1.609 +            return gMainTable.untaggedConvArray[mid] & UCNV_CONVERTER_INDEX_MASK;
   1.610 +        }
   1.611 +    }
   1.612 +
   1.613 +    return UINT32_MAX;
   1.614 +}
   1.615 +
   1.616 +/*
   1.617 + * Is this alias in this list?
   1.618 + * alias and listOffset should be non-NULL.
   1.619 + */
   1.620 +static inline UBool
   1.621 +isAliasInList(const char *alias, uint32_t listOffset) {
   1.622 +    if (listOffset) {
   1.623 +        uint32_t currAlias;
   1.624 +        uint32_t listCount = gMainTable.taggedAliasLists[listOffset];
   1.625 +        /* +1 to skip listCount */
   1.626 +        const uint16_t *currList = gMainTable.taggedAliasLists + listOffset + 1;
   1.627 +        for (currAlias = 0; currAlias < listCount; currAlias++) {
   1.628 +            if (currList[currAlias]
   1.629 +                && ucnv_compareNames(alias, GET_STRING(currList[currAlias]))==0)
   1.630 +            {
   1.631 +                return TRUE;
   1.632 +            }
   1.633 +        }
   1.634 +    }
   1.635 +    return FALSE;
   1.636 +}
   1.637 +
   1.638 +/*
   1.639 + * Search for an standard name of an alias (what is the default name
   1.640 + * that this standard uses?)
   1.641 + * return the listOffset for gTaggedAliasLists. If it's 0,
   1.642 + * the it couldn't be found, but the parameters are valid.
   1.643 + */
   1.644 +static uint32_t
   1.645 +findTaggedAliasListsOffset(const char *alias, const char *standard, UErrorCode *pErrorCode) {
   1.646 +    uint32_t idx;
   1.647 +    uint32_t listOffset;
   1.648 +    uint32_t convNum;
   1.649 +    UErrorCode myErr = U_ZERO_ERROR;
   1.650 +    uint32_t tagNum = getTagNumber(standard);
   1.651 +
   1.652 +    /* Make a quick guess. Hopefully they used a TR22 canonical alias. */
   1.653 +    convNum = findConverter(alias, NULL, &myErr);
   1.654 +    if (myErr != U_ZERO_ERROR) {
   1.655 +        *pErrorCode = myErr;
   1.656 +    }
   1.657 +
   1.658 +    if (tagNum < (gMainTable.tagListSize - UCNV_NUM_HIDDEN_TAGS) && convNum < gMainTable.converterListSize) {
   1.659 +        listOffset = gMainTable.taggedAliasArray[tagNum*gMainTable.converterListSize + convNum];
   1.660 +        if (listOffset && gMainTable.taggedAliasLists[listOffset + 1]) {
   1.661 +            return listOffset;
   1.662 +        }
   1.663 +        if (myErr == U_AMBIGUOUS_ALIAS_WARNING) {
   1.664 +            /* Uh Oh! They used an ambiguous alias.
   1.665 +               We have to search the whole swiss cheese starting
   1.666 +               at the highest standard affinity.
   1.667 +               This may take a while.
   1.668 +            */
   1.669 +            for (idx = 0; idx < gMainTable.taggedAliasArraySize; idx++) {
   1.670 +                listOffset = gMainTable.taggedAliasArray[idx];
   1.671 +                if (listOffset && isAliasInList(alias, listOffset)) {
   1.672 +                    uint32_t currTagNum = idx/gMainTable.converterListSize;
   1.673 +                    uint32_t currConvNum = (idx - currTagNum*gMainTable.converterListSize);
   1.674 +                    uint32_t tempListOffset = gMainTable.taggedAliasArray[tagNum*gMainTable.converterListSize + currConvNum];
   1.675 +                    if (tempListOffset && gMainTable.taggedAliasLists[tempListOffset + 1]) {
   1.676 +                        return tempListOffset;
   1.677 +                    }
   1.678 +                    /* else keep on looking */
   1.679 +                    /* We could speed this up by starting on the next row
   1.680 +                       because an alias is unique per row, right now.
   1.681 +                       This would change if alias versioning appears. */
   1.682 +                }
   1.683 +            }
   1.684 +            /* The standard doesn't know about the alias */
   1.685 +        }
   1.686 +        /* else no default name */
   1.687 +        return 0;
   1.688 +    }
   1.689 +    /* else converter or tag not found */
   1.690 +
   1.691 +    return UINT32_MAX;
   1.692 +}
   1.693 +
   1.694 +/* Return the canonical name */
   1.695 +static uint32_t
   1.696 +findTaggedConverterNum(const char *alias, const char *standard, UErrorCode *pErrorCode) {
   1.697 +    uint32_t idx;
   1.698 +    uint32_t listOffset;
   1.699 +    uint32_t convNum;
   1.700 +    UErrorCode myErr = U_ZERO_ERROR;
   1.701 +    uint32_t tagNum = getTagNumber(standard);
   1.702 +
   1.703 +    /* Make a quick guess. Hopefully they used a TR22 canonical alias. */
   1.704 +    convNum = findConverter(alias, NULL, &myErr);
   1.705 +    if (myErr != U_ZERO_ERROR) {
   1.706 +        *pErrorCode = myErr;
   1.707 +    }
   1.708 +
   1.709 +    if (tagNum < (gMainTable.tagListSize - UCNV_NUM_HIDDEN_TAGS) && convNum < gMainTable.converterListSize) {
   1.710 +        listOffset = gMainTable.taggedAliasArray[tagNum*gMainTable.converterListSize + convNum];
   1.711 +        if (listOffset && isAliasInList(alias, listOffset)) {
   1.712 +            return convNum;
   1.713 +        }
   1.714 +        if (myErr == U_AMBIGUOUS_ALIAS_WARNING) {
   1.715 +            /* Uh Oh! They used an ambiguous alias.
   1.716 +               We have to search one slice of the swiss cheese.
   1.717 +               We search only in the requested tag, not the whole thing.
   1.718 +               This may take a while.
   1.719 +            */
   1.720 +            uint32_t convStart = (tagNum)*gMainTable.converterListSize;
   1.721 +            uint32_t convLimit = (tagNum+1)*gMainTable.converterListSize;
   1.722 +            for (idx = convStart; idx < convLimit; idx++) {
   1.723 +                listOffset = gMainTable.taggedAliasArray[idx];
   1.724 +                if (listOffset && isAliasInList(alias, listOffset)) {
   1.725 +                    return idx-convStart;
   1.726 +                }
   1.727 +            }
   1.728 +            /* The standard doesn't know about the alias */
   1.729 +        }
   1.730 +        /* else no canonical name */
   1.731 +    }
   1.732 +    /* else converter or tag not found */
   1.733 +
   1.734 +    return UINT32_MAX;
   1.735 +}
   1.736 +
   1.737 +
   1.738 +
   1.739 +U_CFUNC const char *
   1.740 +ucnv_io_getConverterName(const char *alias, UBool *containsOption, UErrorCode *pErrorCode) {
   1.741 +    const char *aliasTmp = alias;
   1.742 +    int32_t i = 0;
   1.743 +    for (i = 0; i < 2; i++) {
   1.744 +        if (i == 1) {
   1.745 +            /*
   1.746 +             * After the first unsuccess converter lookup, check to see if
   1.747 +             * the name begins with 'x-'. If it does, strip it off and try
   1.748 +             * again.  This behaviour is similar to how ICU4J does it.
   1.749 +             */
   1.750 +            if (aliasTmp[0] == 'x' || aliasTmp[1] == '-') {
   1.751 +                aliasTmp = aliasTmp+2;
   1.752 +            } else {
   1.753 +                break;
   1.754 +            }
   1.755 +        }
   1.756 +        if(haveAliasData(pErrorCode) && isAlias(aliasTmp, pErrorCode)) {
   1.757 +            uint32_t convNum = findConverter(aliasTmp, containsOption, pErrorCode);
   1.758 +            if (convNum < gMainTable.converterListSize) {
   1.759 +                return GET_STRING(gMainTable.converterList[convNum]);
   1.760 +            }
   1.761 +            /* else converter not found */
   1.762 +        } else {
   1.763 +            break;
   1.764 +        }
   1.765 +    }
   1.766 +
   1.767 +    return NULL;
   1.768 +}
   1.769 +
   1.770 +static int32_t U_CALLCONV
   1.771 +ucnv_io_countStandardAliases(UEnumeration *enumerator, UErrorCode * /*pErrorCode*/) {
   1.772 +    int32_t value = 0;
   1.773 +    UAliasContext *myContext = (UAliasContext *)(enumerator->context);
   1.774 +    uint32_t listOffset = myContext->listOffset;
   1.775 +
   1.776 +    if (listOffset) {
   1.777 +        value = gMainTable.taggedAliasLists[listOffset];
   1.778 +    }
   1.779 +    return value;
   1.780 +}
   1.781 +
   1.782 +static const char* U_CALLCONV
   1.783 +ucnv_io_nextStandardAliases(UEnumeration *enumerator,
   1.784 +                            int32_t* resultLength,
   1.785 +                            UErrorCode * /*pErrorCode*/)
   1.786 +{
   1.787 +    UAliasContext *myContext = (UAliasContext *)(enumerator->context);
   1.788 +    uint32_t listOffset = myContext->listOffset;
   1.789 +
   1.790 +    if (listOffset) {
   1.791 +        uint32_t listCount = gMainTable.taggedAliasLists[listOffset];
   1.792 +        const uint16_t *currList = gMainTable.taggedAliasLists + listOffset + 1;
   1.793 +
   1.794 +        if (myContext->listIdx < listCount) {
   1.795 +            const char *myStr = GET_STRING(currList[myContext->listIdx++]);
   1.796 +            if (resultLength) {
   1.797 +                *resultLength = (int32_t)uprv_strlen(myStr);
   1.798 +            }
   1.799 +            return myStr;
   1.800 +        }
   1.801 +    }
   1.802 +    /* Either we accessed a zero length list, or we enumerated too far. */
   1.803 +    if (resultLength) {
   1.804 +        *resultLength = 0;
   1.805 +    }
   1.806 +    return NULL;
   1.807 +}
   1.808 +
   1.809 +static void U_CALLCONV
   1.810 +ucnv_io_resetStandardAliases(UEnumeration *enumerator, UErrorCode * /*pErrorCode*/) {
   1.811 +    ((UAliasContext *)(enumerator->context))->listIdx = 0;
   1.812 +}
   1.813 +
   1.814 +static void U_CALLCONV
   1.815 +ucnv_io_closeUEnumeration(UEnumeration *enumerator) {
   1.816 +    uprv_free(enumerator->context);
   1.817 +    uprv_free(enumerator);
   1.818 +}
   1.819 +
   1.820 +/* Enumerate the aliases for the specified converter and standard tag */
   1.821 +static const UEnumeration gEnumAliases = {
   1.822 +    NULL,
   1.823 +    NULL,
   1.824 +    ucnv_io_closeUEnumeration,
   1.825 +    ucnv_io_countStandardAliases,
   1.826 +    uenum_unextDefault,
   1.827 +    ucnv_io_nextStandardAliases,
   1.828 +    ucnv_io_resetStandardAliases
   1.829 +};
   1.830 +
   1.831 +U_CAPI UEnumeration * U_EXPORT2
   1.832 +ucnv_openStandardNames(const char *convName,
   1.833 +                       const char *standard,
   1.834 +                       UErrorCode *pErrorCode)
   1.835 +{
   1.836 +    UEnumeration *myEnum = NULL;
   1.837 +    if (haveAliasData(pErrorCode) && isAlias(convName, pErrorCode)) {
   1.838 +        uint32_t listOffset = findTaggedAliasListsOffset(convName, standard, pErrorCode);
   1.839 +
   1.840 +        /* When listOffset == 0, we want to acknowledge that the
   1.841 +           converter name and standard are okay, but there
   1.842 +           is nothing to enumerate. */
   1.843 +        if (listOffset < gMainTable.taggedAliasListsSize) {
   1.844 +            UAliasContext *myContext;
   1.845 +
   1.846 +            myEnum = static_cast<UEnumeration *>(uprv_malloc(sizeof(UEnumeration)));
   1.847 +            if (myEnum == NULL) {
   1.848 +                *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
   1.849 +                return NULL;
   1.850 +            }
   1.851 +            uprv_memcpy(myEnum, &gEnumAliases, sizeof(UEnumeration));
   1.852 +            myContext = static_cast<UAliasContext *>(uprv_malloc(sizeof(UAliasContext)));
   1.853 +            if (myContext == NULL) {
   1.854 +                *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
   1.855 +                uprv_free(myEnum);
   1.856 +                return NULL;
   1.857 +            }
   1.858 +            myContext->listOffset = listOffset;
   1.859 +            myContext->listIdx = 0;
   1.860 +            myEnum->context = myContext;
   1.861 +        }
   1.862 +        /* else converter or tag not found */
   1.863 +    }
   1.864 +    return myEnum;
   1.865 +}
   1.866 +
   1.867 +static uint16_t
   1.868 +ucnv_io_countAliases(const char *alias, UErrorCode *pErrorCode) {
   1.869 +    if(haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
   1.870 +        uint32_t convNum = findConverter(alias, NULL, pErrorCode);
   1.871 +        if (convNum < gMainTable.converterListSize) {
   1.872 +            /* tagListNum - 1 is the ALL tag */
   1.873 +            int32_t listOffset = gMainTable.taggedAliasArray[(gMainTable.tagListSize - 1)*gMainTable.converterListSize + convNum];
   1.874 +
   1.875 +            if (listOffset) {
   1.876 +                return gMainTable.taggedAliasLists[listOffset];
   1.877 +            }
   1.878 +            /* else this shouldn't happen. internal program error */
   1.879 +        }
   1.880 +        /* else converter not found */
   1.881 +    }
   1.882 +    return 0;
   1.883 +}
   1.884 +
   1.885 +static uint16_t
   1.886 +ucnv_io_getAliases(const char *alias, uint16_t start, const char **aliases, UErrorCode *pErrorCode) {
   1.887 +    if(haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
   1.888 +        uint32_t currAlias;
   1.889 +        uint32_t convNum = findConverter(alias, NULL, pErrorCode);
   1.890 +        if (convNum < gMainTable.converterListSize) {
   1.891 +            /* tagListNum - 1 is the ALL tag */
   1.892 +            int32_t listOffset = gMainTable.taggedAliasArray[(gMainTable.tagListSize - 1)*gMainTable.converterListSize + convNum];
   1.893 +
   1.894 +            if (listOffset) {
   1.895 +                uint32_t listCount = gMainTable.taggedAliasLists[listOffset];
   1.896 +                /* +1 to skip listCount */
   1.897 +                const uint16_t *currList = gMainTable.taggedAliasLists + listOffset + 1;
   1.898 +
   1.899 +                for (currAlias = start; currAlias < listCount; currAlias++) {
   1.900 +                    aliases[currAlias] = GET_STRING(currList[currAlias]);
   1.901 +                }
   1.902 +            }
   1.903 +            /* else this shouldn't happen. internal program error */
   1.904 +        }
   1.905 +        /* else converter not found */
   1.906 +    }
   1.907 +    return 0;
   1.908 +}
   1.909 +
   1.910 +static const char *
   1.911 +ucnv_io_getAlias(const char *alias, uint16_t n, UErrorCode *pErrorCode) {
   1.912 +    if(haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
   1.913 +        uint32_t convNum = findConverter(alias, NULL, pErrorCode);
   1.914 +        if (convNum < gMainTable.converterListSize) {
   1.915 +            /* tagListNum - 1 is the ALL tag */
   1.916 +            int32_t listOffset = gMainTable.taggedAliasArray[(gMainTable.tagListSize - 1)*gMainTable.converterListSize + convNum];
   1.917 +
   1.918 +            if (listOffset) {
   1.919 +                uint32_t listCount = gMainTable.taggedAliasLists[listOffset];
   1.920 +                /* +1 to skip listCount */
   1.921 +                const uint16_t *currList = gMainTable.taggedAliasLists + listOffset + 1;
   1.922 +
   1.923 +                if (n < listCount)  {
   1.924 +                    return GET_STRING(currList[n]);
   1.925 +                }
   1.926 +                *pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
   1.927 +            }
   1.928 +            /* else this shouldn't happen. internal program error */
   1.929 +        }
   1.930 +        /* else converter not found */
   1.931 +    }
   1.932 +    return NULL;
   1.933 +}
   1.934 +
   1.935 +static uint16_t
   1.936 +ucnv_io_countStandards(UErrorCode *pErrorCode) {
   1.937 +    if (haveAliasData(pErrorCode)) {
   1.938 +        /* Don't include the empty list */
   1.939 +        return (uint16_t)(gMainTable.tagListSize - UCNV_NUM_HIDDEN_TAGS);
   1.940 +    }
   1.941 +
   1.942 +    return 0;
   1.943 +}
   1.944 +
   1.945 +U_CAPI const char * U_EXPORT2
   1.946 +ucnv_getStandard(uint16_t n, UErrorCode *pErrorCode) {
   1.947 +    if (haveAliasData(pErrorCode)) {
   1.948 +        if (n < gMainTable.tagListSize - UCNV_NUM_HIDDEN_TAGS) {
   1.949 +            return GET_STRING(gMainTable.tagList[n]);
   1.950 +        }
   1.951 +        *pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
   1.952 +    }
   1.953 +
   1.954 +    return NULL;
   1.955 +}
   1.956 +
   1.957 +U_CAPI const char * U_EXPORT2
   1.958 +ucnv_getStandardName(const char *alias, const char *standard, UErrorCode *pErrorCode) {
   1.959 +    if (haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
   1.960 +        uint32_t listOffset = findTaggedAliasListsOffset(alias, standard, pErrorCode);
   1.961 +
   1.962 +        if (0 < listOffset && listOffset < gMainTable.taggedAliasListsSize) {
   1.963 +            const uint16_t *currList = gMainTable.taggedAliasLists + listOffset + 1;
   1.964 +
   1.965 +            /* Get the preferred name from this list */
   1.966 +            if (currList[0]) {
   1.967 +                return GET_STRING(currList[0]);
   1.968 +            }
   1.969 +            /* else someone screwed up the alias table. */
   1.970 +            /* *pErrorCode = U_INVALID_FORMAT_ERROR */
   1.971 +        }
   1.972 +    }
   1.973 +
   1.974 +    return NULL;
   1.975 +}
   1.976 +
   1.977 +U_CAPI uint16_t U_EXPORT2
   1.978 +ucnv_countAliases(const char *alias, UErrorCode *pErrorCode)
   1.979 +{
   1.980 +    return ucnv_io_countAliases(alias, pErrorCode);
   1.981 +}
   1.982 +
   1.983 +
   1.984 +U_CAPI const char* U_EXPORT2
   1.985 +ucnv_getAlias(const char *alias, uint16_t n, UErrorCode *pErrorCode)
   1.986 +{
   1.987 +    return ucnv_io_getAlias(alias, n, pErrorCode);
   1.988 +}
   1.989 +
   1.990 +U_CAPI void U_EXPORT2
   1.991 +ucnv_getAliases(const char *alias, const char **aliases, UErrorCode *pErrorCode)
   1.992 +{
   1.993 +    ucnv_io_getAliases(alias, 0, aliases, pErrorCode);
   1.994 +}
   1.995 +
   1.996 +U_CAPI uint16_t U_EXPORT2
   1.997 +ucnv_countStandards(void)
   1.998 +{
   1.999 +    UErrorCode err = U_ZERO_ERROR;
  1.1000 +    return ucnv_io_countStandards(&err);
  1.1001 +}
  1.1002 +
  1.1003 +U_CAPI const char * U_EXPORT2
  1.1004 +ucnv_getCanonicalName(const char *alias, const char *standard, UErrorCode *pErrorCode) {
  1.1005 +    if (haveAliasData(pErrorCode) && isAlias(alias, pErrorCode)) {
  1.1006 +        uint32_t convNum = findTaggedConverterNum(alias, standard, pErrorCode);
  1.1007 +
  1.1008 +        if (convNum < gMainTable.converterListSize) {
  1.1009 +            return GET_STRING(gMainTable.converterList[convNum]);
  1.1010 +        }
  1.1011 +    }
  1.1012 +
  1.1013 +    return NULL;
  1.1014 +}
  1.1015 +
  1.1016 +static int32_t U_CALLCONV
  1.1017 +ucnv_io_countAllConverters(UEnumeration * /*enumerator*/, UErrorCode * /*pErrorCode*/) {
  1.1018 +    return gMainTable.converterListSize;
  1.1019 +}
  1.1020 +
  1.1021 +static const char* U_CALLCONV
  1.1022 +ucnv_io_nextAllConverters(UEnumeration *enumerator,
  1.1023 +                            int32_t* resultLength,
  1.1024 +                            UErrorCode * /*pErrorCode*/)
  1.1025 +{
  1.1026 +    uint16_t *myContext = (uint16_t *)(enumerator->context);
  1.1027 +
  1.1028 +    if (*myContext < gMainTable.converterListSize) {
  1.1029 +        const char *myStr = GET_STRING(gMainTable.converterList[(*myContext)++]);
  1.1030 +        if (resultLength) {
  1.1031 +            *resultLength = (int32_t)uprv_strlen(myStr);
  1.1032 +        }
  1.1033 +        return myStr;
  1.1034 +    }
  1.1035 +    /* Either we accessed a zero length list, or we enumerated too far. */
  1.1036 +    if (resultLength) {
  1.1037 +        *resultLength = 0;
  1.1038 +    }
  1.1039 +    return NULL;
  1.1040 +}
  1.1041 +
  1.1042 +static void U_CALLCONV
  1.1043 +ucnv_io_resetAllConverters(UEnumeration *enumerator, UErrorCode * /*pErrorCode*/) {
  1.1044 +    *((uint16_t *)(enumerator->context)) = 0;
  1.1045 +}
  1.1046 +
  1.1047 +static const UEnumeration gEnumAllConverters = {
  1.1048 +    NULL,
  1.1049 +    NULL,
  1.1050 +    ucnv_io_closeUEnumeration,
  1.1051 +    ucnv_io_countAllConverters,
  1.1052 +    uenum_unextDefault,
  1.1053 +    ucnv_io_nextAllConverters,
  1.1054 +    ucnv_io_resetAllConverters
  1.1055 +};
  1.1056 +
  1.1057 +U_CAPI UEnumeration * U_EXPORT2
  1.1058 +ucnv_openAllNames(UErrorCode *pErrorCode) {
  1.1059 +    UEnumeration *myEnum = NULL;
  1.1060 +    if (haveAliasData(pErrorCode)) {
  1.1061 +        uint16_t *myContext;
  1.1062 +
  1.1063 +        myEnum = static_cast<UEnumeration *>(uprv_malloc(sizeof(UEnumeration)));
  1.1064 +        if (myEnum == NULL) {
  1.1065 +            *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
  1.1066 +            return NULL;
  1.1067 +        }
  1.1068 +        uprv_memcpy(myEnum, &gEnumAllConverters, sizeof(UEnumeration));
  1.1069 +        myContext = static_cast<uint16_t *>(uprv_malloc(sizeof(uint16_t)));
  1.1070 +        if (myContext == NULL) {
  1.1071 +            *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
  1.1072 +            uprv_free(myEnum);
  1.1073 +            return NULL;
  1.1074 +        }
  1.1075 +        *myContext = 0;
  1.1076 +        myEnum->context = myContext;
  1.1077 +    }
  1.1078 +    return myEnum;
  1.1079 +}
  1.1080 +
  1.1081 +U_CFUNC uint16_t
  1.1082 +ucnv_io_countKnownConverters(UErrorCode *pErrorCode) {
  1.1083 +    if (haveAliasData(pErrorCode)) {
  1.1084 +        return (uint16_t)gMainTable.converterListSize;
  1.1085 +    }
  1.1086 +    return 0;
  1.1087 +}
  1.1088 +
  1.1089 +/* alias table swapping ----------------------------------------------------- */
  1.1090 +
  1.1091 +typedef char * U_CALLCONV StripForCompareFn(char *dst, const char *name);
  1.1092 +
  1.1093 +/*
  1.1094 + * row of a temporary array
  1.1095 + *
  1.1096 + * gets platform-endian charset string indexes and sorting indexes;
  1.1097 + * after sorting this array by strings, the actual arrays are permutated
  1.1098 + * according to the sorting indexes
  1.1099 + */
  1.1100 +typedef struct TempRow {
  1.1101 +    uint16_t strIndex, sortIndex;
  1.1102 +} TempRow;
  1.1103 +
  1.1104 +typedef struct TempAliasTable {
  1.1105 +    const char *chars;
  1.1106 +    TempRow *rows;
  1.1107 +    uint16_t *resort;
  1.1108 +    StripForCompareFn *stripForCompare;
  1.1109 +} TempAliasTable;
  1.1110 +
  1.1111 +enum {
  1.1112 +    STACK_ROW_CAPACITY=500
  1.1113 +};
  1.1114 +
  1.1115 +static int32_t
  1.1116 +io_compareRows(const void *context, const void *left, const void *right) {
  1.1117 +    char strippedLeft[UCNV_MAX_CONVERTER_NAME_LENGTH],
  1.1118 +         strippedRight[UCNV_MAX_CONVERTER_NAME_LENGTH];
  1.1119 +
  1.1120 +    TempAliasTable *tempTable=(TempAliasTable *)context;
  1.1121 +    const char *chars=tempTable->chars;
  1.1122 +
  1.1123 +    return (int32_t)uprv_strcmp(tempTable->stripForCompare(strippedLeft, chars+2*((const TempRow *)left)->strIndex),
  1.1124 +                                tempTable->stripForCompare(strippedRight, chars+2*((const TempRow *)right)->strIndex));
  1.1125 +}
  1.1126 +
  1.1127 +U_CAPI int32_t U_EXPORT2
  1.1128 +ucnv_swapAliases(const UDataSwapper *ds,
  1.1129 +                 const void *inData, int32_t length, void *outData,
  1.1130 +                 UErrorCode *pErrorCode) {
  1.1131 +    const UDataInfo *pInfo;
  1.1132 +    int32_t headerSize;
  1.1133 +
  1.1134 +    const uint16_t *inTable;
  1.1135 +    const uint32_t *inSectionSizes;
  1.1136 +    uint32_t toc[offsetsCount];
  1.1137 +    uint32_t offsets[offsetsCount]; /* 16-bit-addressed offsets from inTable/outTable */
  1.1138 +    uint32_t i, count, tocLength, topOffset;
  1.1139 +
  1.1140 +    TempRow rows[STACK_ROW_CAPACITY];
  1.1141 +    uint16_t resort[STACK_ROW_CAPACITY];
  1.1142 +    TempAliasTable tempTable;
  1.1143 +
  1.1144 +    /* udata_swapDataHeader checks the arguments */
  1.1145 +    headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
  1.1146 +    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
  1.1147 +        return 0;
  1.1148 +    }
  1.1149 +
  1.1150 +    /* check data format and format version */
  1.1151 +    pInfo=(const UDataInfo *)((const char *)inData+4);
  1.1152 +    if(!(
  1.1153 +        pInfo->dataFormat[0]==0x43 &&   /* dataFormat="CvAl" */
  1.1154 +        pInfo->dataFormat[1]==0x76 &&
  1.1155 +        pInfo->dataFormat[2]==0x41 &&
  1.1156 +        pInfo->dataFormat[3]==0x6c &&
  1.1157 +        pInfo->formatVersion[0]==3
  1.1158 +    )) {
  1.1159 +        udata_printError(ds, "ucnv_swapAliases(): data format %02x.%02x.%02x.%02x (format version %02x) is not an alias table\n",
  1.1160 +                         pInfo->dataFormat[0], pInfo->dataFormat[1],
  1.1161 +                         pInfo->dataFormat[2], pInfo->dataFormat[3],
  1.1162 +                         pInfo->formatVersion[0]);
  1.1163 +        *pErrorCode=U_UNSUPPORTED_ERROR;
  1.1164 +        return 0;
  1.1165 +    }
  1.1166 +
  1.1167 +    /* an alias table must contain at least the table of contents array */
  1.1168 +    if(length>=0 && (length-headerSize)<4*(1+minTocLength)) {
  1.1169 +        udata_printError(ds, "ucnv_swapAliases(): too few bytes (%d after header) for an alias table\n",
  1.1170 +                         length-headerSize);
  1.1171 +        *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
  1.1172 +        return 0;
  1.1173 +    }
  1.1174 +
  1.1175 +    inSectionSizes=(const uint32_t *)((const char *)inData+headerSize);
  1.1176 +    inTable=(const uint16_t *)inSectionSizes;
  1.1177 +    uprv_memset(toc, 0, sizeof(toc));
  1.1178 +    toc[tocLengthIndex]=tocLength=ds->readUInt32(inSectionSizes[tocLengthIndex]);
  1.1179 +    if(tocLength<minTocLength || offsetsCount<=tocLength) {
  1.1180 +        udata_printError(ds, "ucnv_swapAliases(): table of contents contains unsupported number of sections (%u sections)\n", tocLength);
  1.1181 +        *pErrorCode=U_INVALID_FORMAT_ERROR;
  1.1182 +        return 0;
  1.1183 +    }
  1.1184 +
  1.1185 +    /* read the known part of the table of contents */
  1.1186 +    for(i=converterListIndex; i<=tocLength; ++i) {
  1.1187 +        toc[i]=ds->readUInt32(inSectionSizes[i]);
  1.1188 +    }
  1.1189 +
  1.1190 +    /* compute offsets */
  1.1191 +    uprv_memset(offsets, 0, sizeof(offsets));
  1.1192 +    offsets[converterListIndex]=2*(1+tocLength); /* count two 16-bit units per toc entry */
  1.1193 +    for(i=tagListIndex; i<=tocLength; ++i) {
  1.1194 +        offsets[i]=offsets[i-1]+toc[i-1];
  1.1195 +    }
  1.1196 +
  1.1197 +    /* compute the overall size of the after-header data, in numbers of 16-bit units */
  1.1198 +    topOffset=offsets[i-1]+toc[i-1];
  1.1199 +
  1.1200 +    if(length>=0) {
  1.1201 +        uint16_t *outTable;
  1.1202 +        const uint16_t *p, *p2;
  1.1203 +        uint16_t *q, *q2;
  1.1204 +        uint16_t oldIndex;
  1.1205 +
  1.1206 +        if((length-headerSize)<(2*(int32_t)topOffset)) {
  1.1207 +            udata_printError(ds, "ucnv_swapAliases(): too few bytes (%d after header) for an alias table\n",
  1.1208 +                             length-headerSize);
  1.1209 +            *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
  1.1210 +            return 0;
  1.1211 +        }
  1.1212 +
  1.1213 +        outTable=(uint16_t *)((char *)outData+headerSize);
  1.1214 +
  1.1215 +        /* swap the entire table of contents */
  1.1216 +        ds->swapArray32(ds, inTable, 4*(1+tocLength), outTable, pErrorCode);
  1.1217 +
  1.1218 +        /* swap unormalized strings & normalized strings */
  1.1219 +        ds->swapInvChars(ds, inTable+offsets[stringTableIndex], 2*(int32_t)(toc[stringTableIndex]+toc[normalizedStringTableIndex]),
  1.1220 +                             outTable+offsets[stringTableIndex], pErrorCode);
  1.1221 +        if(U_FAILURE(*pErrorCode)) {
  1.1222 +            udata_printError(ds, "ucnv_swapAliases().swapInvChars(charset names) failed\n");
  1.1223 +            return 0;
  1.1224 +        }
  1.1225 +
  1.1226 +        if(ds->inCharset==ds->outCharset) {
  1.1227 +            /* no need to sort, just swap all 16-bit values together */
  1.1228 +            ds->swapArray16(ds,
  1.1229 +                            inTable+offsets[converterListIndex],
  1.1230 +                            2*(int32_t)(offsets[stringTableIndex]-offsets[converterListIndex]),
  1.1231 +                            outTable+offsets[converterListIndex],
  1.1232 +                            pErrorCode);
  1.1233 +        } else {
  1.1234 +            /* allocate the temporary table for sorting */
  1.1235 +            count=toc[aliasListIndex];
  1.1236 +
  1.1237 +            tempTable.chars=(const char *)(outTable+offsets[stringTableIndex]); /* sort by outCharset */
  1.1238 +
  1.1239 +            if(count<=STACK_ROW_CAPACITY) {
  1.1240 +                tempTable.rows=rows;
  1.1241 +                tempTable.resort=resort;
  1.1242 +            } else {
  1.1243 +                tempTable.rows=(TempRow *)uprv_malloc(count*sizeof(TempRow)+count*2);
  1.1244 +                if(tempTable.rows==NULL) {
  1.1245 +                    udata_printError(ds, "ucnv_swapAliases(): unable to allocate memory for sorting tables (max length: %u)\n",
  1.1246 +                                     count);
  1.1247 +                    *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
  1.1248 +                    return 0;
  1.1249 +                }
  1.1250 +                tempTable.resort=(uint16_t *)(tempTable.rows+count);
  1.1251 +            }
  1.1252 +
  1.1253 +            if(ds->outCharset==U_ASCII_FAMILY) {
  1.1254 +                tempTable.stripForCompare=ucnv_io_stripASCIIForCompare;
  1.1255 +            } else /* U_EBCDIC_FAMILY */ {
  1.1256 +                tempTable.stripForCompare=ucnv_io_stripEBCDICForCompare;
  1.1257 +            }
  1.1258 +
  1.1259 +            /*
  1.1260 +             * Sort unique aliases+mapped names.
  1.1261 +             *
  1.1262 +             * We need to sort the list again by outCharset strings because they
  1.1263 +             * sort differently for different charset families.
  1.1264 +             * First we set up a temporary table with the string indexes and
  1.1265 +             * sorting indexes and sort that.
  1.1266 +             * Then we permutate and copy/swap the actual values.
  1.1267 +             */
  1.1268 +            p=inTable+offsets[aliasListIndex];
  1.1269 +            q=outTable+offsets[aliasListIndex];
  1.1270 +
  1.1271 +            p2=inTable+offsets[untaggedConvArrayIndex];
  1.1272 +            q2=outTable+offsets[untaggedConvArrayIndex];
  1.1273 +
  1.1274 +            for(i=0; i<count; ++i) {
  1.1275 +                tempTable.rows[i].strIndex=ds->readUInt16(p[i]);
  1.1276 +                tempTable.rows[i].sortIndex=(uint16_t)i;
  1.1277 +            }
  1.1278 +
  1.1279 +            uprv_sortArray(tempTable.rows, (int32_t)count, sizeof(TempRow),
  1.1280 +                           io_compareRows, &tempTable,
  1.1281 +                           FALSE, pErrorCode);
  1.1282 +
  1.1283 +            if(U_SUCCESS(*pErrorCode)) {
  1.1284 +                /* copy/swap/permutate items */
  1.1285 +                if(p!=q) {
  1.1286 +                    for(i=0; i<count; ++i) {
  1.1287 +                        oldIndex=tempTable.rows[i].sortIndex;
  1.1288 +                        ds->swapArray16(ds, p+oldIndex, 2, q+i, pErrorCode);
  1.1289 +                        ds->swapArray16(ds, p2+oldIndex, 2, q2+i, pErrorCode);
  1.1290 +                    }
  1.1291 +                } else {
  1.1292 +                    /*
  1.1293 +                     * If we swap in-place, then the permutation must use another
  1.1294 +                     * temporary array (tempTable.resort)
  1.1295 +                     * before the results are copied to the outBundle.
  1.1296 +                     */
  1.1297 +                    uint16_t *r=tempTable.resort;
  1.1298 +
  1.1299 +                    for(i=0; i<count; ++i) {
  1.1300 +                        oldIndex=tempTable.rows[i].sortIndex;
  1.1301 +                        ds->swapArray16(ds, p+oldIndex, 2, r+i, pErrorCode);
  1.1302 +                    }
  1.1303 +                    uprv_memcpy(q, r, 2*count);
  1.1304 +
  1.1305 +                    for(i=0; i<count; ++i) {
  1.1306 +                        oldIndex=tempTable.rows[i].sortIndex;
  1.1307 +                        ds->swapArray16(ds, p2+oldIndex, 2, r+i, pErrorCode);
  1.1308 +                    }
  1.1309 +                    uprv_memcpy(q2, r, 2*count);
  1.1310 +                }
  1.1311 +            }
  1.1312 +
  1.1313 +            if(tempTable.rows!=rows) {
  1.1314 +                uprv_free(tempTable.rows);
  1.1315 +            }
  1.1316 +
  1.1317 +            if(U_FAILURE(*pErrorCode)) {
  1.1318 +                udata_printError(ds, "ucnv_swapAliases().uprv_sortArray(%u items) failed\n",
  1.1319 +                                 count);
  1.1320 +                return 0;
  1.1321 +            }
  1.1322 +
  1.1323 +            /* swap remaining 16-bit values */
  1.1324 +            ds->swapArray16(ds,
  1.1325 +                            inTable+offsets[converterListIndex],
  1.1326 +                            2*(int32_t)(offsets[aliasListIndex]-offsets[converterListIndex]),
  1.1327 +                            outTable+offsets[converterListIndex],
  1.1328 +                            pErrorCode);
  1.1329 +            ds->swapArray16(ds,
  1.1330 +                            inTable+offsets[taggedAliasArrayIndex],
  1.1331 +                            2*(int32_t)(offsets[stringTableIndex]-offsets[taggedAliasArrayIndex]),
  1.1332 +                            outTable+offsets[taggedAliasArrayIndex],
  1.1333 +                            pErrorCode);
  1.1334 +        }
  1.1335 +    }
  1.1336 +
  1.1337 +    return headerSize+2*(int32_t)topOffset;
  1.1338 +}
  1.1339 +
  1.1340 +#endif
  1.1341 +
  1.1342 +
  1.1343 +/*
  1.1344 + * Hey, Emacs, please set the following:
  1.1345 + *
  1.1346 + * Local Variables:
  1.1347 + * indent-tabs-mode: nil
  1.1348 + * End:
  1.1349 + *
  1.1350 + */

mercurial