2 ******************************************************************************
4 * Copyright (C) 1999-2011, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
11 * initializes global variables and defines functions pertaining to converter
12 * name resolution aspect of the conversion code.
16 * created on: 1999nov22
17 * created by: Markus W. Scherer
19 * Use the binary cnvalias.icu (created from convrtrs.txt) to work
20 * with aliases for converter names.
22 * Date Name Description
23 * 11/22/1999 markus Created
24 * 06/28/2002 grhoten Major overhaul of the converter alias design.
25 * Now an alias can map to different converters
26 * depending on the specified standard.
27 *******************************************************************************
30 #include "unicode/utypes.h"
32 #if !UCONFIG_NO_CONVERSION
34 #include "unicode/ucnv.h"
35 #include "unicode/udata.h"
46 /* Format of cnvalias.icu -----------------------------------------------------
48 * cnvalias.icu is a binary, memory-mappable form of convrtrs.txt.
49 * This binary form contains several tables. All indexes are to uint16_t
50 * units, and not to the bytes (uint8_t units). Addressing everything on
51 * 16-bit boundaries allows us to store more information with small index
52 * numbers, which are also 16-bit in size. The majority of the table (except
53 * the string table) are 16-bit numbers.
55 * First there is the size of the Table of Contents (TOC). The TOC
56 * entries contain the size of each section. In order to find the offset
57 * you just need to sum up the previous offsets.
58 * The TOC length and entries are an array of uint32_t values.
59 * The first section after the TOC starts immediately after the TOC.
61 * 1) This section contains a list of converters. This list contains indexes
62 * into the string table for the converter name. The index of this list is
63 * also used by other sections, which are mentioned later on.
64 * This list is not sorted.
66 * 2) This section contains a list of tags. This list contains indexes
67 * into the string table for the tag name. The index of this list is
68 * also used by other sections, which are mentioned later on.
69 * This list is in priority order of standards.
71 * 3) This section contains a list of sorted unique aliases. This
72 * list contains indexes into the string table for the alias name. The
73 * index of this list is also used by other sections, like the 4th section.
74 * The index for the 3rd and 4th section is used to get the
75 * alias -> converter name mapping. Section 3 and 4 form a two column table.
76 * Some of the most significant bits of each index may contain other
77 * information (see findConverter for details).
79 * 4) This section contains a list of mapped converter names. Consider this
80 * as a table that maps the 3rd section to the 1st section. This list contains
81 * indexes into the 1st section. The index of this list is the same index in
82 * the 3rd section. There is also some extra information in the high bits of
83 * each converter index in this table. Currently it's only used to say that
84 * an alias mapped to this converter is ambiguous. See UCNV_CONVERTER_INDEX_MASK
85 * and UCNV_AMBIGUOUS_ALIAS_MAP_BIT for more information. This section is
86 * the predigested form of the 5th section so that an alias lookup can be fast.
88 * 5) This section contains a 2D array with indexes to the 6th section. This
89 * section is the full form of all alias mappings. The column index is the
90 * index into the converter list (column header). The row index is the index
91 * to tag list (row header). This 2D array is the top part a 3D array. The
92 * third dimension is in the 6th section.
94 * 6) This is blob of variable length arrays. Each array starts with a size,
95 * and is followed by indexes to alias names in the string table. This is
96 * the third dimension to the section 5. No other section should be referencing
99 * 7) Starting in ICU 3.6, this can be a UConverterAliasOptions struct. Its
100 * presence indicates that a section 9 exists. UConverterAliasOptions specifies
101 * what type of string normalization is used among other potential things in the
104 * 8) This is the string table. All strings are indexed on an even address.
105 * There are two reasons for this. First many chip architectures locate strings
106 * faster on even address boundaries. Second, since all indexes are 16-bit
107 * numbers, this string table can be 128KB in size instead of 64KB when we
108 * only have strings starting on an even address.
110 * 9) When present this is a set of prenormalized strings from section 8. This
111 * table contains normalized strings with the dashes and spaces stripped out,
112 * and all strings lowercased. In the future, the options in section 7 may state
113 * other types of normalization.
115 * Here is the concept of section 5 and 6. It's a 3D cube. Each tag
116 * has a unique alias among all converters. That same alias can
117 * be mentioned in other standards on different converters,
118 * but only one alias per tag can be unique.
121 * Converter Names (Usually in TR22 form)
122 * -------------------------------------------.
128 * ------------------------------------------/ |
136 * -------------------------------------------
140 * Here is what it really looks like. It's like swiss cheese.
141 * There are holes. Some converters aren't recognized by
142 * a standard, or they are really old converters that the
143 * standard doesn't recognize anymore.
145 * Converter Names (Usually in TR22 form)
146 * -------------------------------------------.
147 * T /##########################################/|
149 * g / # ## ## ### # ### ### ### #/
150 * s / # ##### #### ## ## #/#
151 * / ### # # ## # # # ### # # #/##
152 * ------------------------------------------/# #
153 * A |### # # ## # # # ### # # #|# #
154 * l |# # # # # ## # #|# #
164 * Used by the UEnumeration API
166 typedef struct UAliasContext
{
171 static const char DATA_NAME
[] = "cnvalias";
172 static const char DATA_TYPE
[] = "icu";
174 static UDataMemory
*gAliasData
=NULL
;
178 converterListIndex
=1,
181 untaggedConvArrayIndex
=4,
182 taggedAliasArrayIndex
=5,
183 taggedAliasListsIndex
=6,
186 normalizedStringTableIndex
=9,
187 offsetsCount
, /* length of the swapper's temporary offsets[] */
188 minTocLength
=8 /* min. tocLength in the file, does not count the tocLengthIndex! */
191 static const UConverterAliasOptions defaultTableOptions
= {
192 UCNV_IO_UNNORMALIZED
,
193 0 /* containsCnvOptionInfo */
195 static UConverterAlias gMainTable
;
197 #define GET_STRING(idx) (const char *)(gMainTable.stringTable + (idx))
198 #define GET_NORMALIZED_STRING(idx) (const char *)(gMainTable.normalizedStringTable + (idx))
200 static UBool U_CALLCONV
201 isAcceptable(void * /*context*/,
202 const char * /*type*/, const char * /*name*/,
203 const UDataInfo
*pInfo
) {
206 pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
&&
207 pInfo
->charsetFamily
==U_CHARSET_FAMILY
&&
208 pInfo
->dataFormat
[0]==0x43 && /* dataFormat="CvAl" */
209 pInfo
->dataFormat
[1]==0x76 &&
210 pInfo
->dataFormat
[2]==0x41 &&
211 pInfo
->dataFormat
[3]==0x6c &&
212 pInfo
->formatVersion
[0]==3);
215 static UBool U_CALLCONV
ucnv_io_cleanup(void)
218 udata_close(gAliasData
);
222 uprv_memset(&gMainTable
, 0, sizeof(gMainTable
));
224 return TRUE
; /* Everything was cleaned up */
228 haveAliasData(UErrorCode
*pErrorCode
) {
231 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
235 UMTX_CHECK(NULL
, (gAliasData
==NULL
), needInit
);
237 /* load converter alias data from file if necessary */
240 const uint16_t *table
;
241 const uint32_t *sectionSizes
;
245 data
= udata_openChoice(NULL
, DATA_TYPE
, DATA_NAME
, isAcceptable
, NULL
, pErrorCode
);
246 if(U_FAILURE(*pErrorCode
)) {
250 sectionSizes
= (const uint32_t *)udata_getMemory(data
);
251 table
= (const uint16_t *)sectionSizes
;
253 tableStart
= sectionSizes
[0];
254 if (tableStart
< minTocLength
) {
255 *pErrorCode
= U_INVALID_FORMAT_ERROR
;
261 if(gAliasData
==NULL
) {
262 gMainTable
.converterListSize
= sectionSizes
[1];
263 gMainTable
.tagListSize
= sectionSizes
[2];
264 gMainTable
.aliasListSize
= sectionSizes
[3];
265 gMainTable
.untaggedConvArraySize
= sectionSizes
[4];
266 gMainTable
.taggedAliasArraySize
= sectionSizes
[5];
267 gMainTable
.taggedAliasListsSize
= sectionSizes
[6];
268 gMainTable
.optionTableSize
= sectionSizes
[7];
269 gMainTable
.stringTableSize
= sectionSizes
[8];
271 if (tableStart
> 8) {
272 gMainTable
.normalizedStringTableSize
= sectionSizes
[9];
275 currOffset
= tableStart
* (sizeof(uint32_t)/sizeof(uint16_t)) + (sizeof(uint32_t)/sizeof(uint16_t));
276 gMainTable
.converterList
= table
+ currOffset
;
278 currOffset
+= gMainTable
.converterListSize
;
279 gMainTable
.tagList
= table
+ currOffset
;
281 currOffset
+= gMainTable
.tagListSize
;
282 gMainTable
.aliasList
= table
+ currOffset
;
284 currOffset
+= gMainTable
.aliasListSize
;
285 gMainTable
.untaggedConvArray
= table
+ currOffset
;
287 currOffset
+= gMainTable
.untaggedConvArraySize
;
288 gMainTable
.taggedAliasArray
= table
+ currOffset
;
290 /* aliasLists is a 1's based array, but it has a padding character */
291 currOffset
+= gMainTable
.taggedAliasArraySize
;
292 gMainTable
.taggedAliasLists
= table
+ currOffset
;
294 currOffset
+= gMainTable
.taggedAliasListsSize
;
295 if (gMainTable
.optionTableSize
> 0
296 && ((const UConverterAliasOptions
*)(table
+ currOffset
))->stringNormalizationType
< UCNV_IO_NORM_TYPE_COUNT
)
299 gMainTable
.optionTable
= (const UConverterAliasOptions
*)(table
+ currOffset
);
302 /* Smaller table, or I can't handle this normalization mode!
303 Use the original slower table lookup. */
304 gMainTable
.optionTable
= &defaultTableOptions
;
307 currOffset
+= gMainTable
.optionTableSize
;
308 gMainTable
.stringTable
= table
+ currOffset
;
310 currOffset
+= gMainTable
.stringTableSize
;
311 gMainTable
.normalizedStringTable
= ((gMainTable
.optionTable
->stringNormalizationType
== UCNV_IO_UNNORMALIZED
)
312 ? gMainTable
.stringTable
: (table
+ currOffset
));
314 ucln_common_registerCleanup(UCLN_COMMON_UCNV_IO
, ucnv_io_cleanup
);
321 /* if a different thread set it first, then close the extra data */
323 udata_close(data
); /* NULL if it was set correctly */
331 isAlias(const char *alias
, UErrorCode
*pErrorCode
) {
333 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
336 return (UBool
)(*alias
!=0);
339 static uint32_t getTagNumber(const char *tagname
) {
340 if (gMainTable
.tagList
) {
342 for (tagNum
= 0; tagNum
< gMainTable
.tagListSize
; tagNum
++) {
343 if (!uprv_stricmp(GET_STRING(gMainTable
.tagList
[tagNum
]), tagname
)) {
352 /* character types relevant for ucnv_compareNames() */
357 MINLETTER
/* any values from here on are lowercase letter mappings */
360 /* character types for ASCII 00..7F */
361 static const uint8_t asciiTypes
[128] = {
362 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
363 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
364 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
365 ZERO
, NONZERO
, NONZERO
, NONZERO
, NONZERO
, NONZERO
, NONZERO
, NONZERO
, NONZERO
, NONZERO
, 0, 0, 0, 0, 0, 0,
366 0, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
367 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0, 0, 0, 0, 0,
368 0, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
369 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0, 0, 0, 0, 0
372 #define GET_ASCII_TYPE(c) ((int8_t)(c) >= 0 ? asciiTypes[(uint8_t)c] : (uint8_t)IGNORE)
374 /* character types for EBCDIC 80..FF */
375 static const uint8_t ebcdicTypes
[128] = {
376 0, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0, 0, 0, 0, 0, 0,
377 0, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0, 0, 0, 0, 0, 0,
378 0, 0, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0, 0, 0, 0, 0, 0,
379 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
380 0, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0, 0, 0, 0, 0, 0,
381 0, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0, 0, 0, 0, 0, 0,
382 0, 0, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0, 0, 0, 0, 0, 0,
383 ZERO
, NONZERO
, NONZERO
, NONZERO
, NONZERO
, NONZERO
, NONZERO
, NONZERO
, NONZERO
, NONZERO
, 0, 0, 0, 0, 0, 0
386 #define GET_EBCDIC_TYPE(c) ((int8_t)(c) < 0 ? ebcdicTypes[(c)&0x7f] : (uint8_t)IGNORE)
388 #if U_CHARSET_FAMILY==U_ASCII_FAMILY
389 # define GET_CHAR_TYPE(c) GET_ASCII_TYPE(c)
390 #elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
391 # define GET_CHAR_TYPE(c) GET_EBCDIC_TYPE(c)
393 # error U_CHARSET_FAMILY is not valid
396 /* @see ucnv_compareNames */
397 U_CFUNC
char * U_EXPORT2
398 ucnv_io_stripASCIIForCompare(char *dst
, const char *name
) {
400 uint8_t type
, nextType
;
402 UBool afterDigit
= FALSE
;
404 while ((c1
= *name
++) != 0) {
405 type
= GET_ASCII_TYPE(c1
);
409 continue; /* ignore all but letters and digits */
412 nextType
= GET_ASCII_TYPE(*name
);
413 if (nextType
== ZERO
|| nextType
== NONZERO
) {
414 continue; /* ignore leading zero before another digit */
422 c1
= (char)type
; /* lowercased letter */
432 U_CFUNC
char * U_EXPORT2
433 ucnv_io_stripEBCDICForCompare(char *dst
, const char *name
) {
435 uint8_t type
, nextType
;
437 UBool afterDigit
= FALSE
;
439 while ((c1
= *name
++) != 0) {
440 type
= GET_EBCDIC_TYPE(c1
);
444 continue; /* ignore all but letters and digits */
447 nextType
= GET_EBCDIC_TYPE(*name
);
448 if (nextType
== ZERO
|| nextType
== NONZERO
) {
449 continue; /* ignore leading zero before another digit */
457 c1
= (char)type
; /* lowercased letter */
468 * Do a fuzzy compare of two converter/alias names.
469 * The comparison is case-insensitive, ignores leading zeroes if they are not
470 * followed by further digits, and ignores all but letters and digits.
471 * Thus the strings "UTF-8", "utf_8", "u*T@f08" and "Utf 8" are exactly equivalent.
472 * See section 1.4, Charset Alias Matching in Unicode Technical Standard #22
473 * at http://www.unicode.org/reports/tr22/
475 * This is a symmetrical (commutative) operation; order of arguments
476 * is insignificant. This is an important property for sorting the
477 * list (when the list is preprocessed into binary form) and for
478 * performing binary searches on it at run time.
480 * @param name1 a converter name or alias, zero-terminated
481 * @param name2 a converter name or alias, zero-terminated
482 * @return 0 if the names match, or a negative value if the name1
483 * lexically precedes name2, or a positive value if the name1
484 * lexically follows name2.
486 * @see ucnv_io_stripForCompare
489 ucnv_compareNames(const char *name1
, const char *name2
) {
491 uint8_t type
, nextType
;
493 UBool afterDigit1
= FALSE
, afterDigit2
= FALSE
;
496 while ((c1
= *name1
++) != 0) {
497 type
= GET_CHAR_TYPE(c1
);
501 continue; /* ignore all but letters and digits */
504 nextType
= GET_CHAR_TYPE(*name1
);
505 if (nextType
== ZERO
|| nextType
== NONZERO
) {
506 continue; /* ignore leading zero before another digit */
514 c1
= (char)type
; /* lowercased letter */
518 break; /* deliver c1 */
520 while ((c2
= *name2
++) != 0) {
521 type
= GET_CHAR_TYPE(c2
);
525 continue; /* ignore all but letters and digits */
528 nextType
= GET_CHAR_TYPE(*name2
);
529 if (nextType
== ZERO
|| nextType
== NONZERO
) {
530 continue; /* ignore leading zero before another digit */
538 c2
= (char)type
; /* lowercased letter */
542 break; /* deliver c2 */
545 /* If we reach the ends of both strings then they match */
550 /* Case-insensitive comparison */
551 rc
= (int)(unsigned char)c1
- (int)(unsigned char)c2
;
559 * search for an alias
560 * return the converter number index for gConverterList
562 static inline uint32_t
563 findConverter(const char *alias
, UBool
*containsOption
, UErrorCode
*pErrorCode
) {
564 uint32_t mid
, start
, limit
;
567 int isUnnormalized
= (gMainTable
.optionTable
->stringNormalizationType
== UCNV_IO_UNNORMALIZED
);
568 char strippedName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
570 if (!isUnnormalized
) {
571 if (uprv_strlen(alias
) >= UCNV_MAX_CONVERTER_NAME_LENGTH
) {
572 *pErrorCode
= U_BUFFER_OVERFLOW_ERROR
;
576 /* Lower case and remove ignoreable characters. */
577 ucnv_io_stripForCompare(strippedName
, alias
);
578 alias
= strippedName
;
581 /* do a binary search for the alias */
583 limit
= gMainTable
.untaggedConvArraySize
;
585 lastMid
= UINT32_MAX
;
588 mid
= (uint32_t)((start
+ limit
) / 2);
589 if (lastMid
== mid
) { /* Have we moved? */
590 break; /* We haven't moved, and it wasn't found. */
593 if (isUnnormalized
) {
594 result
= ucnv_compareNames(alias
, GET_STRING(gMainTable
.aliasList
[mid
]));
597 result
= uprv_strcmp(alias
, GET_NORMALIZED_STRING(gMainTable
.aliasList
[mid
]));
602 } else if (result
> 0) {
605 /* Since the gencnval tool folds duplicates into one entry,
606 * this alias in gAliasList is unique, but different standards
607 * may map an alias to different converters.
609 if (gMainTable
.untaggedConvArray
[mid
] & UCNV_AMBIGUOUS_ALIAS_MAP_BIT
) {
610 *pErrorCode
= U_AMBIGUOUS_ALIAS_WARNING
;
612 /* State whether the canonical converter name contains an option.
613 This information is contained in this list in order to maintain backward & forward compatibility. */
614 if (containsOption
) {
615 UBool containsCnvOptionInfo
= (UBool
)gMainTable
.optionTable
->containsCnvOptionInfo
;
616 *containsOption
= (UBool
)((containsCnvOptionInfo
617 && ((gMainTable
.untaggedConvArray
[mid
] & UCNV_CONTAINS_OPTION_BIT
) != 0))
618 || !containsCnvOptionInfo
);
620 return gMainTable
.untaggedConvArray
[mid
] & UCNV_CONVERTER_INDEX_MASK
;
628 * Is this alias in this list?
629 * alias and listOffset should be non-NULL.
632 isAliasInList(const char *alias
, uint32_t listOffset
) {
635 uint32_t listCount
= gMainTable
.taggedAliasLists
[listOffset
];
636 /* +1 to skip listCount */
637 const uint16_t *currList
= gMainTable
.taggedAliasLists
+ listOffset
+ 1;
638 for (currAlias
= 0; currAlias
< listCount
; currAlias
++) {
639 if (currList
[currAlias
]
640 && ucnv_compareNames(alias
, GET_STRING(currList
[currAlias
]))==0)
650 * Search for an standard name of an alias (what is the default name
651 * that this standard uses?)
652 * return the listOffset for gTaggedAliasLists. If it's 0,
653 * the it couldn't be found, but the parameters are valid.
656 findTaggedAliasListsOffset(const char *alias
, const char *standard
, UErrorCode
*pErrorCode
) {
660 UErrorCode myErr
= U_ZERO_ERROR
;
661 uint32_t tagNum
= getTagNumber(standard
);
663 /* Make a quick guess. Hopefully they used a TR22 canonical alias. */
664 convNum
= findConverter(alias
, NULL
, &myErr
);
665 if (myErr
!= U_ZERO_ERROR
) {
669 if (tagNum
< (gMainTable
.tagListSize
- UCNV_NUM_HIDDEN_TAGS
) && convNum
< gMainTable
.converterListSize
) {
670 listOffset
= gMainTable
.taggedAliasArray
[tagNum
*gMainTable
.converterListSize
+ convNum
];
671 if (listOffset
&& gMainTable
.taggedAliasLists
[listOffset
+ 1]) {
674 if (myErr
== U_AMBIGUOUS_ALIAS_WARNING
) {
675 /* Uh Oh! They used an ambiguous alias.
676 We have to search the whole swiss cheese starting
677 at the highest standard affinity.
678 This may take a while.
680 for (idx
= 0; idx
< gMainTable
.taggedAliasArraySize
; idx
++) {
681 listOffset
= gMainTable
.taggedAliasArray
[idx
];
682 if (listOffset
&& isAliasInList(alias
, listOffset
)) {
683 uint32_t currTagNum
= idx
/gMainTable
.converterListSize
;
684 uint32_t currConvNum
= (idx
- currTagNum
*gMainTable
.converterListSize
);
685 uint32_t tempListOffset
= gMainTable
.taggedAliasArray
[tagNum
*gMainTable
.converterListSize
+ currConvNum
];
686 if (tempListOffset
&& gMainTable
.taggedAliasLists
[tempListOffset
+ 1]) {
687 return tempListOffset
;
689 /* else keep on looking */
690 /* We could speed this up by starting on the next row
691 because an alias is unique per row, right now.
692 This would change if alias versioning appears. */
695 /* The standard doesn't know about the alias */
697 /* else no default name */
700 /* else converter or tag not found */
705 /* Return the canonical name */
707 findTaggedConverterNum(const char *alias
, const char *standard
, UErrorCode
*pErrorCode
) {
711 UErrorCode myErr
= U_ZERO_ERROR
;
712 uint32_t tagNum
= getTagNumber(standard
);
714 /* Make a quick guess. Hopefully they used a TR22 canonical alias. */
715 convNum
= findConverter(alias
, NULL
, &myErr
);
716 if (myErr
!= U_ZERO_ERROR
) {
720 if (tagNum
< (gMainTable
.tagListSize
- UCNV_NUM_HIDDEN_TAGS
) && convNum
< gMainTable
.converterListSize
) {
721 listOffset
= gMainTable
.taggedAliasArray
[tagNum
*gMainTable
.converterListSize
+ convNum
];
722 if (listOffset
&& isAliasInList(alias
, listOffset
)) {
725 if (myErr
== U_AMBIGUOUS_ALIAS_WARNING
) {
726 /* Uh Oh! They used an ambiguous alias.
727 We have to search one slice of the swiss cheese.
728 We search only in the requested tag, not the whole thing.
729 This may take a while.
731 uint32_t convStart
= (tagNum
)*gMainTable
.converterListSize
;
732 uint32_t convLimit
= (tagNum
+1)*gMainTable
.converterListSize
;
733 for (idx
= convStart
; idx
< convLimit
; idx
++) {
734 listOffset
= gMainTable
.taggedAliasArray
[idx
];
735 if (listOffset
&& isAliasInList(alias
, listOffset
)) {
736 return idx
-convStart
;
739 /* The standard doesn't know about the alias */
741 /* else no canonical name */
743 /* else converter or tag not found */
751 ucnv_io_getConverterName(const char *alias
, UBool
*containsOption
, UErrorCode
*pErrorCode
) {
752 const char *aliasTmp
= alias
;
754 for (i
= 0; i
< 2; i
++) {
757 * After the first unsuccess converter lookup, check to see if
758 * the name begins with 'x-'. If it does, strip it off and try
759 * again. This behaviour is similar to how ICU4J does it.
761 if (aliasTmp
[0] == 'x' || aliasTmp
[1] == '-') {
762 aliasTmp
= aliasTmp
+2;
767 if(haveAliasData(pErrorCode
) && isAlias(aliasTmp
, pErrorCode
)) {
768 uint32_t convNum
= findConverter(aliasTmp
, containsOption
, pErrorCode
);
769 if (convNum
< gMainTable
.converterListSize
) {
770 return GET_STRING(gMainTable
.converterList
[convNum
]);
772 /* else converter not found */
781 static int32_t U_CALLCONV
782 ucnv_io_countStandardAliases(UEnumeration
*enumerator
, UErrorCode
* /*pErrorCode*/) {
784 UAliasContext
*myContext
= (UAliasContext
*)(enumerator
->context
);
785 uint32_t listOffset
= myContext
->listOffset
;
788 value
= gMainTable
.taggedAliasLists
[listOffset
];
793 static const char* U_CALLCONV
794 ucnv_io_nextStandardAliases(UEnumeration
*enumerator
,
795 int32_t* resultLength
,
796 UErrorCode
* /*pErrorCode*/)
798 UAliasContext
*myContext
= (UAliasContext
*)(enumerator
->context
);
799 uint32_t listOffset
= myContext
->listOffset
;
802 uint32_t listCount
= gMainTable
.taggedAliasLists
[listOffset
];
803 const uint16_t *currList
= gMainTable
.taggedAliasLists
+ listOffset
+ 1;
805 if (myContext
->listIdx
< listCount
) {
806 const char *myStr
= GET_STRING(currList
[myContext
->listIdx
++]);
808 *resultLength
= (int32_t)uprv_strlen(myStr
);
813 /* Either we accessed a zero length list, or we enumerated too far. */
820 static void U_CALLCONV
821 ucnv_io_resetStandardAliases(UEnumeration
*enumerator
, UErrorCode
* /*pErrorCode*/) {
822 ((UAliasContext
*)(enumerator
->context
))->listIdx
= 0;
825 static void U_CALLCONV
826 ucnv_io_closeUEnumeration(UEnumeration
*enumerator
) {
827 uprv_free(enumerator
->context
);
828 uprv_free(enumerator
);
831 /* Enumerate the aliases for the specified converter and standard tag */
832 static const UEnumeration gEnumAliases
= {
835 ucnv_io_closeUEnumeration
,
836 ucnv_io_countStandardAliases
,
838 ucnv_io_nextStandardAliases
,
839 ucnv_io_resetStandardAliases
842 U_CAPI UEnumeration
* U_EXPORT2
843 ucnv_openStandardNames(const char *convName
,
844 const char *standard
,
845 UErrorCode
*pErrorCode
)
847 UEnumeration
*myEnum
= NULL
;
848 if (haveAliasData(pErrorCode
) && isAlias(convName
, pErrorCode
)) {
849 uint32_t listOffset
= findTaggedAliasListsOffset(convName
, standard
, pErrorCode
);
851 /* When listOffset == 0, we want to acknowledge that the
852 converter name and standard are okay, but there
853 is nothing to enumerate. */
854 if (listOffset
< gMainTable
.taggedAliasListsSize
) {
855 UAliasContext
*myContext
;
857 myEnum
= reinterpret_cast<UEnumeration
*>(uprv_malloc(sizeof(UEnumeration
)));
858 if (myEnum
== NULL
) {
859 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
862 uprv_memcpy(myEnum
, &gEnumAliases
, sizeof(UEnumeration
));
863 myContext
= reinterpret_cast<UAliasContext
*>(uprv_malloc(sizeof(UAliasContext
)));
864 if (myContext
== NULL
) {
865 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
869 myContext
->listOffset
= listOffset
;
870 myContext
->listIdx
= 0;
871 myEnum
->context
= myContext
;
873 /* else converter or tag not found */
879 ucnv_io_countAliases(const char *alias
, UErrorCode
*pErrorCode
) {
880 if(haveAliasData(pErrorCode
) && isAlias(alias
, pErrorCode
)) {
881 uint32_t convNum
= findConverter(alias
, NULL
, pErrorCode
);
882 if (convNum
< gMainTable
.converterListSize
) {
883 /* tagListNum - 1 is the ALL tag */
884 int32_t listOffset
= gMainTable
.taggedAliasArray
[(gMainTable
.tagListSize
- 1)*gMainTable
.converterListSize
+ convNum
];
887 return gMainTable
.taggedAliasLists
[listOffset
];
889 /* else this shouldn't happen. internal program error */
891 /* else converter not found */
897 ucnv_io_getAliases(const char *alias
, uint16_t start
, const char **aliases
, UErrorCode
*pErrorCode
) {
898 if(haveAliasData(pErrorCode
) && isAlias(alias
, pErrorCode
)) {
900 uint32_t convNum
= findConverter(alias
, NULL
, pErrorCode
);
901 if (convNum
< gMainTable
.converterListSize
) {
902 /* tagListNum - 1 is the ALL tag */
903 int32_t listOffset
= gMainTable
.taggedAliasArray
[(gMainTable
.tagListSize
- 1)*gMainTable
.converterListSize
+ convNum
];
906 uint32_t listCount
= gMainTable
.taggedAliasLists
[listOffset
];
907 /* +1 to skip listCount */
908 const uint16_t *currList
= gMainTable
.taggedAliasLists
+ listOffset
+ 1;
910 for (currAlias
= start
; currAlias
< listCount
; currAlias
++) {
911 aliases
[currAlias
] = GET_STRING(currList
[currAlias
]);
914 /* else this shouldn't happen. internal program error */
916 /* else converter not found */
922 ucnv_io_getAlias(const char *alias
, uint16_t n
, UErrorCode
*pErrorCode
) {
923 if(haveAliasData(pErrorCode
) && isAlias(alias
, pErrorCode
)) {
924 uint32_t convNum
= findConverter(alias
, NULL
, pErrorCode
);
925 if (convNum
< gMainTable
.converterListSize
) {
926 /* tagListNum - 1 is the ALL tag */
927 int32_t listOffset
= gMainTable
.taggedAliasArray
[(gMainTable
.tagListSize
- 1)*gMainTable
.converterListSize
+ convNum
];
930 uint32_t listCount
= gMainTable
.taggedAliasLists
[listOffset
];
931 /* +1 to skip listCount */
932 const uint16_t *currList
= gMainTable
.taggedAliasLists
+ listOffset
+ 1;
935 return GET_STRING(currList
[n
]);
937 *pErrorCode
= U_INDEX_OUTOFBOUNDS_ERROR
;
939 /* else this shouldn't happen. internal program error */
941 /* else converter not found */
947 ucnv_io_countStandards(UErrorCode
*pErrorCode
) {
948 if (haveAliasData(pErrorCode
)) {
949 /* Don't include the empty list */
950 return (uint16_t)(gMainTable
.tagListSize
- UCNV_NUM_HIDDEN_TAGS
);
956 U_CAPI
const char * U_EXPORT2
957 ucnv_getStandard(uint16_t n
, UErrorCode
*pErrorCode
) {
958 if (haveAliasData(pErrorCode
)) {
959 if (n
< gMainTable
.tagListSize
- UCNV_NUM_HIDDEN_TAGS
) {
960 return GET_STRING(gMainTable
.tagList
[n
]);
962 *pErrorCode
= U_INDEX_OUTOFBOUNDS_ERROR
;
968 U_CAPI
const char * U_EXPORT2
969 ucnv_getStandardName(const char *alias
, const char *standard
, UErrorCode
*pErrorCode
) {
970 if (haveAliasData(pErrorCode
) && isAlias(alias
, pErrorCode
)) {
971 uint32_t listOffset
= findTaggedAliasListsOffset(alias
, standard
, pErrorCode
);
973 if (0 < listOffset
&& listOffset
< gMainTable
.taggedAliasListsSize
) {
974 const uint16_t *currList
= gMainTable
.taggedAliasLists
+ listOffset
+ 1;
976 /* Get the preferred name from this list */
978 return GET_STRING(currList
[0]);
980 /* else someone screwed up the alias table. */
981 /* *pErrorCode = U_INVALID_FORMAT_ERROR */
988 U_CAPI
uint16_t U_EXPORT2
989 ucnv_countAliases(const char *alias
, UErrorCode
*pErrorCode
)
991 return ucnv_io_countAliases(alias
, pErrorCode
);
995 U_CAPI
const char* U_EXPORT2
996 ucnv_getAlias(const char *alias
, uint16_t n
, UErrorCode
*pErrorCode
)
998 return ucnv_io_getAlias(alias
, n
, pErrorCode
);
1001 U_CAPI
void U_EXPORT2
1002 ucnv_getAliases(const char *alias
, const char **aliases
, UErrorCode
*pErrorCode
)
1004 ucnv_io_getAliases(alias
, 0, aliases
, pErrorCode
);
1007 U_CAPI
uint16_t U_EXPORT2
1008 ucnv_countStandards(void)
1010 UErrorCode err
= U_ZERO_ERROR
;
1011 return ucnv_io_countStandards(&err
);
1014 U_CAPI
const char * U_EXPORT2
1015 ucnv_getCanonicalName(const char *alias
, const char *standard
, UErrorCode
*pErrorCode
) {
1016 if (haveAliasData(pErrorCode
) && isAlias(alias
, pErrorCode
)) {
1017 uint32_t convNum
= findTaggedConverterNum(alias
, standard
, pErrorCode
);
1019 if (convNum
< gMainTable
.converterListSize
) {
1020 return GET_STRING(gMainTable
.converterList
[convNum
]);
1027 static int32_t U_CALLCONV
1028 ucnv_io_countAllConverters(UEnumeration
* /*enumerator*/, UErrorCode
* /*pErrorCode*/) {
1029 return gMainTable
.converterListSize
;
1032 static const char* U_CALLCONV
1033 ucnv_io_nextAllConverters(UEnumeration
*enumerator
,
1034 int32_t* resultLength
,
1035 UErrorCode
* /*pErrorCode*/)
1037 uint16_t *myContext
= (uint16_t *)(enumerator
->context
);
1039 if (*myContext
< gMainTable
.converterListSize
) {
1040 const char *myStr
= GET_STRING(gMainTable
.converterList
[(*myContext
)++]);
1042 *resultLength
= (int32_t)uprv_strlen(myStr
);
1046 /* Either we accessed a zero length list, or we enumerated too far. */
1053 static void U_CALLCONV
1054 ucnv_io_resetAllConverters(UEnumeration
*enumerator
, UErrorCode
* /*pErrorCode*/) {
1055 *((uint16_t *)(enumerator
->context
)) = 0;
1058 static const UEnumeration gEnumAllConverters
= {
1061 ucnv_io_closeUEnumeration
,
1062 ucnv_io_countAllConverters
,
1064 ucnv_io_nextAllConverters
,
1065 ucnv_io_resetAllConverters
1068 U_CAPI UEnumeration
* U_EXPORT2
1069 ucnv_openAllNames(UErrorCode
*pErrorCode
) {
1070 UEnumeration
*myEnum
= NULL
;
1071 if (haveAliasData(pErrorCode
)) {
1072 uint16_t *myContext
;
1074 myEnum
= reinterpret_cast<UEnumeration
*>(uprv_malloc(sizeof(UEnumeration
)));
1075 if (myEnum
== NULL
) {
1076 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
1079 uprv_memcpy(myEnum
, &gEnumAllConverters
, sizeof(UEnumeration
));
1080 myContext
= reinterpret_cast<uint16_t *>(uprv_malloc(sizeof(uint16_t)));
1081 if (myContext
== NULL
) {
1082 *pErrorCode
= U_MEMORY_ALLOCATION_ERROR
;
1087 myEnum
->context
= myContext
;
1093 ucnv_io_countKnownConverters(UErrorCode
*pErrorCode
) {
1094 if (haveAliasData(pErrorCode
)) {
1095 return (uint16_t)gMainTable
.converterListSize
;
1100 /* alias table swapping ----------------------------------------------------- */
1102 typedef char * U_CALLCONV
StripForCompareFn(char *dst
, const char *name
);
1105 * row of a temporary array
1107 * gets platform-endian charset string indexes and sorting indexes;
1108 * after sorting this array by strings, the actual arrays are permutated
1109 * according to the sorting indexes
1111 typedef struct TempRow
{
1112 uint16_t strIndex
, sortIndex
;
1115 typedef struct TempAliasTable
{
1119 StripForCompareFn
*stripForCompare
;
1123 STACK_ROW_CAPACITY
=500
1127 io_compareRows(const void *context
, const void *left
, const void *right
) {
1128 char strippedLeft
[UCNV_MAX_CONVERTER_NAME_LENGTH
],
1129 strippedRight
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
1131 TempAliasTable
*tempTable
=(TempAliasTable
*)context
;
1132 const char *chars
=tempTable
->chars
;
1134 return (int32_t)uprv_strcmp(tempTable
->stripForCompare(strippedLeft
, chars
+2*((const TempRow
*)left
)->strIndex
),
1135 tempTable
->stripForCompare(strippedRight
, chars
+2*((const TempRow
*)right
)->strIndex
));
1138 U_CAPI
int32_t U_EXPORT2
1139 ucnv_swapAliases(const UDataSwapper
*ds
,
1140 const void *inData
, int32_t length
, void *outData
,
1141 UErrorCode
*pErrorCode
) {
1142 const UDataInfo
*pInfo
;
1145 const uint16_t *inTable
;
1146 const uint32_t *inSectionSizes
;
1147 uint32_t toc
[offsetsCount
];
1148 uint32_t offsets
[offsetsCount
]; /* 16-bit-addressed offsets from inTable/outTable */
1149 uint32_t i
, count
, tocLength
, topOffset
;
1151 TempRow rows
[STACK_ROW_CAPACITY
];
1152 uint16_t resort
[STACK_ROW_CAPACITY
];
1153 TempAliasTable tempTable
;
1155 /* udata_swapDataHeader checks the arguments */
1156 headerSize
=udata_swapDataHeader(ds
, inData
, length
, outData
, pErrorCode
);
1157 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1161 /* check data format and format version */
1162 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
1164 pInfo
->dataFormat
[0]==0x43 && /* dataFormat="CvAl" */
1165 pInfo
->dataFormat
[1]==0x76 &&
1166 pInfo
->dataFormat
[2]==0x41 &&
1167 pInfo
->dataFormat
[3]==0x6c &&
1168 pInfo
->formatVersion
[0]==3
1170 udata_printError(ds
, "ucnv_swapAliases(): data format %02x.%02x.%02x.%02x (format version %02x) is not an alias table\n",
1171 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
1172 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
1173 pInfo
->formatVersion
[0]);
1174 *pErrorCode
=U_UNSUPPORTED_ERROR
;
1178 /* an alias table must contain at least the table of contents array */
1179 if(length
>=0 && (length
-headerSize
)<4*(1+minTocLength
)) {
1180 udata_printError(ds
, "ucnv_swapAliases(): too few bytes (%d after header) for an alias table\n",
1182 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1186 inSectionSizes
=(const uint32_t *)((const char *)inData
+headerSize
);
1187 inTable
=(const uint16_t *)inSectionSizes
;
1188 uprv_memset(toc
, 0, sizeof(toc
));
1189 toc
[tocLengthIndex
]=tocLength
=ds
->readUInt32(inSectionSizes
[tocLengthIndex
]);
1190 if(tocLength
<minTocLength
|| offsetsCount
<=tocLength
) {
1191 udata_printError(ds
, "ucnv_swapAliases(): table of contents contains unsupported number of sections (%u sections)\n", tocLength
);
1192 *pErrorCode
=U_INVALID_FORMAT_ERROR
;
1196 /* read the known part of the table of contents */
1197 for(i
=converterListIndex
; i
<=tocLength
; ++i
) {
1198 toc
[i
]=ds
->readUInt32(inSectionSizes
[i
]);
1201 /* compute offsets */
1202 uprv_memset(offsets
, 0, sizeof(offsets
));
1203 offsets
[converterListIndex
]=2*(1+tocLength
); /* count two 16-bit units per toc entry */
1204 for(i
=tagListIndex
; i
<=tocLength
; ++i
) {
1205 offsets
[i
]=offsets
[i
-1]+toc
[i
-1];
1208 /* compute the overall size of the after-header data, in numbers of 16-bit units */
1209 topOffset
=offsets
[i
-1]+toc
[i
-1];
1213 const uint16_t *p
, *p2
;
1217 if((length
-headerSize
)<(2*(int32_t)topOffset
)) {
1218 udata_printError(ds
, "ucnv_swapAliases(): too few bytes (%d after header) for an alias table\n",
1220 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
1224 outTable
=(uint16_t *)((char *)outData
+headerSize
);
1226 /* swap the entire table of contents */
1227 ds
->swapArray32(ds
, inTable
, 4*(1+tocLength
), outTable
, pErrorCode
);
1229 /* swap unormalized strings & normalized strings */
1230 ds
->swapInvChars(ds
, inTable
+offsets
[stringTableIndex
], 2*(int32_t)(toc
[stringTableIndex
]+toc
[normalizedStringTableIndex
]),
1231 outTable
+offsets
[stringTableIndex
], pErrorCode
);
1232 if(U_FAILURE(*pErrorCode
)) {
1233 udata_printError(ds
, "ucnv_swapAliases().swapInvChars(charset names) failed\n");
1237 if(ds
->inCharset
==ds
->outCharset
) {
1238 /* no need to sort, just swap all 16-bit values together */
1240 inTable
+offsets
[converterListIndex
],
1241 2*(int32_t)(offsets
[stringTableIndex
]-offsets
[converterListIndex
]),
1242 outTable
+offsets
[converterListIndex
],
1245 /* allocate the temporary table for sorting */
1246 count
=toc
[aliasListIndex
];
1248 tempTable
.chars
=(const char *)(outTable
+offsets
[stringTableIndex
]); /* sort by outCharset */
1250 if(count
<=STACK_ROW_CAPACITY
) {
1251 tempTable
.rows
=rows
;
1252 tempTable
.resort
=resort
;
1254 tempTable
.rows
=(TempRow
*)uprv_malloc(count
*sizeof(TempRow
)+count
*2);
1255 if(tempTable
.rows
==NULL
) {
1256 udata_printError(ds
, "ucnv_swapAliases(): unable to allocate memory for sorting tables (max length: %u)\n",
1258 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1261 tempTable
.resort
=(uint16_t *)(tempTable
.rows
+count
);
1264 if(ds
->outCharset
==U_ASCII_FAMILY
) {
1265 tempTable
.stripForCompare
=ucnv_io_stripASCIIForCompare
;
1266 } else /* U_EBCDIC_FAMILY */ {
1267 tempTable
.stripForCompare
=ucnv_io_stripEBCDICForCompare
;
1271 * Sort unique aliases+mapped names.
1273 * We need to sort the list again by outCharset strings because they
1274 * sort differently for different charset families.
1275 * First we set up a temporary table with the string indexes and
1276 * sorting indexes and sort that.
1277 * Then we permutate and copy/swap the actual values.
1279 p
=inTable
+offsets
[aliasListIndex
];
1280 q
=outTable
+offsets
[aliasListIndex
];
1282 p2
=inTable
+offsets
[untaggedConvArrayIndex
];
1283 q2
=outTable
+offsets
[untaggedConvArrayIndex
];
1285 for(i
=0; i
<count
; ++i
) {
1286 tempTable
.rows
[i
].strIndex
=ds
->readUInt16(p
[i
]);
1287 tempTable
.rows
[i
].sortIndex
=(uint16_t)i
;
1290 uprv_sortArray(tempTable
.rows
, (int32_t)count
, sizeof(TempRow
),
1291 io_compareRows
, &tempTable
,
1294 if(U_SUCCESS(*pErrorCode
)) {
1295 /* copy/swap/permutate items */
1297 for(i
=0; i
<count
; ++i
) {
1298 oldIndex
=tempTable
.rows
[i
].sortIndex
;
1299 ds
->swapArray16(ds
, p
+oldIndex
, 2, q
+i
, pErrorCode
);
1300 ds
->swapArray16(ds
, p2
+oldIndex
, 2, q2
+i
, pErrorCode
);
1304 * If we swap in-place, then the permutation must use another
1305 * temporary array (tempTable.resort)
1306 * before the results are copied to the outBundle.
1308 uint16_t *r
=tempTable
.resort
;
1310 for(i
=0; i
<count
; ++i
) {
1311 oldIndex
=tempTable
.rows
[i
].sortIndex
;
1312 ds
->swapArray16(ds
, p
+oldIndex
, 2, r
+i
, pErrorCode
);
1314 uprv_memcpy(q
, r
, 2*count
);
1316 for(i
=0; i
<count
; ++i
) {
1317 oldIndex
=tempTable
.rows
[i
].sortIndex
;
1318 ds
->swapArray16(ds
, p2
+oldIndex
, 2, r
+i
, pErrorCode
);
1320 uprv_memcpy(q2
, r
, 2*count
);
1324 if(tempTable
.rows
!=rows
) {
1325 uprv_free(tempTable
.rows
);
1328 if(U_FAILURE(*pErrorCode
)) {
1329 udata_printError(ds
, "ucnv_swapAliases().uprv_sortArray(%u items) failed\n",
1334 /* swap remaining 16-bit values */
1336 inTable
+offsets
[converterListIndex
],
1337 2*(int32_t)(offsets
[aliasListIndex
]-offsets
[converterListIndex
]),
1338 outTable
+offsets
[converterListIndex
],
1341 inTable
+offsets
[taggedAliasArrayIndex
],
1342 2*(int32_t)(offsets
[stringTableIndex
]-offsets
[taggedAliasArrayIndex
]),
1343 outTable
+offsets
[taggedAliasArrayIndex
],
1348 return headerSize
+2*(int32_t)topOffset
;
1354 * Hey, Emacs, please set the following:
1357 * indent-tabs-mode: nil