Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | ****************************************************************************** |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 2000-2013, International Business Machines |
michael@0 | 5 | * Corporation and others. All Rights Reserved. |
michael@0 | 6 | * |
michael@0 | 7 | ****************************************************************************** |
michael@0 | 8 | * file name: ucnvmbcs.h |
michael@0 | 9 | * encoding: US-ASCII |
michael@0 | 10 | * tab size: 8 (not used) |
michael@0 | 11 | * indentation:4 |
michael@0 | 12 | * |
michael@0 | 13 | * created on: 2000jul07 |
michael@0 | 14 | * created by: Markus W. Scherer |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | #ifndef __UCNVMBCS_H__ |
michael@0 | 18 | #define __UCNVMBCS_H__ |
michael@0 | 19 | |
michael@0 | 20 | #include "unicode/utypes.h" |
michael@0 | 21 | |
michael@0 | 22 | #if !UCONFIG_NO_CONVERSION |
michael@0 | 23 | |
michael@0 | 24 | #include "unicode/ucnv.h" |
michael@0 | 25 | #include "ucnv_cnv.h" |
michael@0 | 26 | #include "ucnv_ext.h" |
michael@0 | 27 | |
michael@0 | 28 | /** |
michael@0 | 29 | * ICU conversion (.cnv) data file structure, following the usual UDataInfo |
michael@0 | 30 | * header. |
michael@0 | 31 | * |
michael@0 | 32 | * Format version: 6.2 |
michael@0 | 33 | * |
michael@0 | 34 | * struct UConverterStaticData -- struct containing the converter name, IBM CCSID, |
michael@0 | 35 | * min/max bytes per character, etc. |
michael@0 | 36 | * see ucnv_bld.h |
michael@0 | 37 | * |
michael@0 | 38 | * -------------------- |
michael@0 | 39 | * |
michael@0 | 40 | * The static data is followed by conversionType-specific data structures. |
michael@0 | 41 | * At the moment, there are only variations of MBCS converters. They all have |
michael@0 | 42 | * the same toUnicode structures, while the fromUnicode structures for SBCS |
michael@0 | 43 | * differ from those for other MBCS-style converters. |
michael@0 | 44 | * |
michael@0 | 45 | * _MBCSHeader.version 5 is optional and not backward-compatible |
michael@0 | 46 | * (as usual for changes in the major version field). |
michael@0 | 47 | * |
michael@0 | 48 | * Versions 5.m work like versions 4.m except: |
michael@0 | 49 | * - The _MBCSHeader has variable length (and is always longer than in version 4). |
michael@0 | 50 | * See the struct _MBCSHeader further description below. |
michael@0 | 51 | * - There is a set of flags which indicate further incompatible changes. |
michael@0 | 52 | * (Reader code must reject the file if it does not recognize them all.) |
michael@0 | 53 | * - In particular, one of these flags indicates that most of the fromUnicode |
michael@0 | 54 | * data is missing and must be reconstituted from the toUnicode data |
michael@0 | 55 | * and from the utf8Friendly mbcsIndex at load time. |
michael@0 | 56 | * (This only works with a utf8Friendly table.) |
michael@0 | 57 | * In this case, makeconv may increase maxFastUChar automatically to U+FFFF. |
michael@0 | 58 | * |
michael@0 | 59 | * The first of these versions is 5.3, which is like 4.3 except for the differences above. |
michael@0 | 60 | * |
michael@0 | 61 | * When possible, makeconv continues to generate version 4.m files. |
michael@0 | 62 | * |
michael@0 | 63 | * _MBCSHeader.version 5.4/4.4 supports "good one-way" mappings (|4) |
michael@0 | 64 | * in the extension tables (fromUTableValues bit 30). See ucnv_ext.h for details. |
michael@0 | 65 | * |
michael@0 | 66 | * _MBCSHeader.version 4.3 optionally modifies the fromUnicode data structures |
michael@0 | 67 | * slightly and optionally adds a table for conversion to MBCS (non-SBCS) |
michael@0 | 68 | * charsets. |
michael@0 | 69 | * |
michael@0 | 70 | * The modifications are to make the data utf8Friendly. Not every 4.3 file |
michael@0 | 71 | * file contains utf8Friendly data. |
michael@0 | 72 | * It is utf8Friendly if _MBCSHeader.version[2]!=0. |
michael@0 | 73 | * In this case, the data structures are utf8Friendly up to the code point |
michael@0 | 74 | * maxFastUChar=((_MBCSHeader.version[2]<<8)|0xff) |
michael@0 | 75 | * |
michael@0 | 76 | * A utf8Friendly file has fromUnicode stage 3 entries for code points up to |
michael@0 | 77 | * maxFastUChar allocated in blocks of 64 for indexing with the 6 bits from |
michael@0 | 78 | * a UTF-8 trail byte. ASCII is allocated linearly with 128 contiguous entries. |
michael@0 | 79 | * |
michael@0 | 80 | * In addition, a utf8Friendly MBCS file contains an additional |
michael@0 | 81 | * uint16_t mbcsIndex[(maxFastUChar+1)>>6]; |
michael@0 | 82 | * which replaces the stage 1 and 2 tables for indexing with bits from the |
michael@0 | 83 | * UTF-8 lead byte and middle trail byte. Unlike the older MBCS stage 2 table, |
michael@0 | 84 | * the mbcsIndex does not contain roundtrip flags. Therefore, all fallbacks |
michael@0 | 85 | * from code points up to maxFastUChar (and roundtrips to 0x00) are moved to |
michael@0 | 86 | * the extension data structure. This also allows for faster roundtrip |
michael@0 | 87 | * conversion from UTF-16. |
michael@0 | 88 | * |
michael@0 | 89 | * SBCS files do not contain an additional sbcsIndex[] array because the |
michael@0 | 90 | * proportional size increase would be noticeable, but the runtime |
michael@0 | 91 | * code builds one for the code point range for which the runtime conversion |
michael@0 | 92 | * code is optimized. |
michael@0 | 93 | * |
michael@0 | 94 | * For SBCS, maxFastUChar should be at least U+0FFF. The initial makeconv |
michael@0 | 95 | * implementation sets it to U+1FFF. Because the sbcsIndex is not stored in |
michael@0 | 96 | * the file, a larger maxFastUChar only affects stage 3 block allocation size |
michael@0 | 97 | * and is free in empty blocks. (Larger blocks with sparse contents cause larger |
michael@0 | 98 | * files.) U+1FFF includes almost all of the small scripts. |
michael@0 | 99 | * U+0FFF covers UTF-8 two-byte sequences and three-byte sequences starting with |
michael@0 | 100 | * 0xe0. This includes most scripts with legacy SBCS charsets. |
michael@0 | 101 | * The initial runtime implementation using 4.3 files only builds an sbcsIndex |
michael@0 | 102 | * for code points up to U+0FFF. |
michael@0 | 103 | * |
michael@0 | 104 | * For MBCS, maxFastUChar should be at least U+D7FF (=initial value). |
michael@0 | 105 | * This boundary is convenient because practically all of the commonly used |
michael@0 | 106 | * characters are below it, and because it is the boundary to surrogate |
michael@0 | 107 | * code points, above which special handling is necessary anyway. |
michael@0 | 108 | * (Surrogate pair assembly for UTF-16, validity checking for UTF-8.) |
michael@0 | 109 | * |
michael@0 | 110 | * maxFastUChar could be up to U+FFFF to cover the whole BMP, which could be |
michael@0 | 111 | * useful especially for conversion from UTF-8 when the input can be assumed |
michael@0 | 112 | * to be valid, because the surrogate range would then not have to be |
michael@0 | 113 | * checked. |
michael@0 | 114 | * (With maxFastUChar=0xffff, makeconv would have to check for mbcsIndex value |
michael@0 | 115 | * overflow because with the all-unassigned block 0 and nearly full mappings |
michael@0 | 116 | * from the BMP it is theoretically possible that an index into stage 3 |
michael@0 | 117 | * exceeds 16 bits.) |
michael@0 | 118 | * |
michael@0 | 119 | * _MBCSHeader.version 4.2 adds an optional conversion extension data structure. |
michael@0 | 120 | * If it is present, then an ICU version reading header versions 4.0 or 4.1 |
michael@0 | 121 | * will be able to use the base table and ignore the extension. |
michael@0 | 122 | * |
michael@0 | 123 | * The unicodeMask in the static data is part of the base table data structure. |
michael@0 | 124 | * Especially, the UCNV_HAS_SUPPLEMENTARY flag determines the length of the |
michael@0 | 125 | * fromUnicode stage 1 array. |
michael@0 | 126 | * The static data unicodeMask refers only to the base table's properties if |
michael@0 | 127 | * a base table is included. |
michael@0 | 128 | * In an extension-only file, the static data unicodeMask is 0. |
michael@0 | 129 | * The extension data indexes have a separate field with the unicodeMask flags. |
michael@0 | 130 | * |
michael@0 | 131 | * MBCS-style data structure following the static data. |
michael@0 | 132 | * Offsets are counted in bytes from the beginning of the MBCS header structure. |
michael@0 | 133 | * Details about usage in comments in ucnvmbcs.c. |
michael@0 | 134 | * |
michael@0 | 135 | * struct _MBCSHeader (see the definition in this header file below) |
michael@0 | 136 | * contains 32-bit fields as follows: |
michael@0 | 137 | * 8 values: |
michael@0 | 138 | * 0 uint8_t[4] MBCS version in UVersionInfo format (currently 4.3.x.0) |
michael@0 | 139 | * 1 uint32_t countStates |
michael@0 | 140 | * 2 uint32_t countToUFallbacks |
michael@0 | 141 | * 3 uint32_t offsetToUCodeUnits |
michael@0 | 142 | * 4 uint32_t offsetFromUTable |
michael@0 | 143 | * 5 uint32_t offsetFromUBytes |
michael@0 | 144 | * 6 uint32_t flags, bits: |
michael@0 | 145 | * 31.. 8 offsetExtension -- _MBCSHeader.version 4.2 (ICU 2.8) and higher |
michael@0 | 146 | * 0 for older versions and if |
michael@0 | 147 | * there is not extension structure |
michael@0 | 148 | * 7.. 0 outputType |
michael@0 | 149 | * 7 uint32_t fromUBytesLength -- _MBCSHeader.version 4.1 (ICU 2.4) and higher |
michael@0 | 150 | * counts bytes in fromUBytes[] |
michael@0 | 151 | * |
michael@0 | 152 | * New and required in version 5: |
michael@0 | 153 | * 8 uint32_t options, bits: |
michael@0 | 154 | * 31..16 reserved for flags that can be added without breaking |
michael@0 | 155 | * backward compatibility |
michael@0 | 156 | * 15.. 6 reserved for flags whose addition will break |
michael@0 | 157 | * backward compatibility |
michael@0 | 158 | * 6 MBCS_OPT_FROM_U -- if set, |
michael@0 | 159 | * then most of the fromUnicode data is omitted; |
michael@0 | 160 | * fullStage2Length is present and the missing |
michael@0 | 161 | * bottom part of stage 2 must be reconstituted from |
michael@0 | 162 | * the toUnicode data; |
michael@0 | 163 | * stage 3 is missing completely as well; |
michael@0 | 164 | * not used for SBCS tables |
michael@0 | 165 | * 5.. 0 length of the _MBCSHeader (number of uint32_t) |
michael@0 | 166 | * |
michael@0 | 167 | * New and optional in version 5: |
michael@0 | 168 | * 9 uint32_t fullStage2Length: used if MBCS_OPT_FROM_U is set |
michael@0 | 169 | * specifies the full length of stage 2 |
michael@0 | 170 | * including the omitted part |
michael@0 | 171 | * |
michael@0 | 172 | * if(outputType==MBCS_OUTPUT_EXT_ONLY) { |
michael@0 | 173 | * -- base table name for extension-only table |
michael@0 | 174 | * char baseTableName[variable]; -- with NUL plus padding for 4-alignment |
michael@0 | 175 | * |
michael@0 | 176 | * -- all _MBCSHeader fields except for version and flags are 0 |
michael@0 | 177 | * } else { |
michael@0 | 178 | * -- normal base table with optional extension |
michael@0 | 179 | * |
michael@0 | 180 | * int32_t stateTable[countStates][256]; |
michael@0 | 181 | * |
michael@0 | 182 | * struct _MBCSToUFallback { (fallbacks are sorted by offset) |
michael@0 | 183 | * uint32_t offset; |
michael@0 | 184 | * UChar32 codePoint; |
michael@0 | 185 | * } toUFallbacks[countToUFallbacks]; |
michael@0 | 186 | * |
michael@0 | 187 | * uint16_t unicodeCodeUnits[(offsetFromUTable-offsetToUCodeUnits)/2]; |
michael@0 | 188 | * (padded to an even number of units) |
michael@0 | 189 | * |
michael@0 | 190 | * -- stage 1 tables |
michael@0 | 191 | * if(staticData.unicodeMask&UCNV_HAS_SUPPLEMENTARY) { |
michael@0 | 192 | * -- stage 1 table for all of Unicode |
michael@0 | 193 | * uint16_t fromUTable[0x440]; (32-bit-aligned) |
michael@0 | 194 | * } else { |
michael@0 | 195 | * -- BMP-only tables have a smaller stage 1 table |
michael@0 | 196 | * uint16_t fromUTable[0x40]; (32-bit-aligned) |
michael@0 | 197 | * } |
michael@0 | 198 | * |
michael@0 | 199 | * -- stage 2 tables |
michael@0 | 200 | * length determined by top of stage 1 and bottom of stage 3 tables |
michael@0 | 201 | * if(outputType==MBCS_OUTPUT_1) { |
michael@0 | 202 | * -- SBCS: pure indexes |
michael@0 | 203 | * uint16_t stage 2 indexes[?]; |
michael@0 | 204 | * } else { |
michael@0 | 205 | * -- DBCS, MBCS, EBCDIC_STATEFUL, ...: roundtrip flags and indexes |
michael@0 | 206 | * uint32_t stage 2 flags and indexes[?]; |
michael@0 | 207 | * if(options&MBCS_OPT_NO_FROM_U) { |
michael@0 | 208 | * stage 2 really has length fullStage2Length |
michael@0 | 209 | * and the omitted lower part must be reconstituted from |
michael@0 | 210 | * the toUnicode data |
michael@0 | 211 | * } |
michael@0 | 212 | * } |
michael@0 | 213 | * |
michael@0 | 214 | * -- stage 3 tables with byte results |
michael@0 | 215 | * if(outputType==MBCS_OUTPUT_1) { |
michael@0 | 216 | * -- SBCS: each 16-bit result contains flags and the result byte, see ucnvmbcs.c |
michael@0 | 217 | * uint16_t fromUBytes[fromUBytesLength/2]; |
michael@0 | 218 | * } else if(!(options&MBCS_OPT_NO_FROM_U)) { |
michael@0 | 219 | * -- DBCS, MBCS, EBCDIC_STATEFUL, ... 2/3/4 bytes result, see ucnvmbcs.c |
michael@0 | 220 | * uint8_t fromUBytes[fromUBytesLength]; or |
michael@0 | 221 | * uint16_t fromUBytes[fromUBytesLength/2]; or |
michael@0 | 222 | * uint32_t fromUBytes[fromUBytesLength/4]; |
michael@0 | 223 | * } else { |
michael@0 | 224 | * fromUBytes[] must be reconstituted from the toUnicode data |
michael@0 | 225 | * } |
michael@0 | 226 | * |
michael@0 | 227 | * -- optional utf8Friendly mbcsIndex -- _MBCSHeader.version 4.3 (ICU 3.8) and higher |
michael@0 | 228 | * if(outputType!=MBCS_OUTPUT_1 && |
michael@0 | 229 | * _MBCSHeader.version[1]>=3 && |
michael@0 | 230 | * (maxFastUChar=_MBCSHeader.version[2])!=0 |
michael@0 | 231 | * ) { |
michael@0 | 232 | * maxFastUChar=(maxFastUChar<<8)|0xff; |
michael@0 | 233 | * uint16_t mbcsIndex[(maxFastUChar+1)>>6]; |
michael@0 | 234 | * } |
michael@0 | 235 | * } |
michael@0 | 236 | * |
michael@0 | 237 | * -- extension table, details see ucnv_ext.h |
michael@0 | 238 | * int32_t indexes[>=32]; ... |
michael@0 | 239 | */ |
michael@0 | 240 | |
michael@0 | 241 | /* MBCS converter data and state -------------------------------------------- */ |
michael@0 | 242 | |
michael@0 | 243 | enum { |
michael@0 | 244 | MBCS_MAX_STATE_COUNT=128 |
michael@0 | 245 | }; |
michael@0 | 246 | |
michael@0 | 247 | /** |
michael@0 | 248 | * MBCS action codes for conversions to Unicode. |
michael@0 | 249 | * These values are in bits 23..20 of the state table entries. |
michael@0 | 250 | */ |
michael@0 | 251 | enum { |
michael@0 | 252 | MBCS_STATE_VALID_DIRECT_16, |
michael@0 | 253 | MBCS_STATE_VALID_DIRECT_20, |
michael@0 | 254 | |
michael@0 | 255 | MBCS_STATE_FALLBACK_DIRECT_16, |
michael@0 | 256 | MBCS_STATE_FALLBACK_DIRECT_20, |
michael@0 | 257 | |
michael@0 | 258 | MBCS_STATE_VALID_16, |
michael@0 | 259 | MBCS_STATE_VALID_16_PAIR, |
michael@0 | 260 | |
michael@0 | 261 | MBCS_STATE_UNASSIGNED, |
michael@0 | 262 | MBCS_STATE_ILLEGAL, |
michael@0 | 263 | |
michael@0 | 264 | MBCS_STATE_CHANGE_ONLY |
michael@0 | 265 | }; |
michael@0 | 266 | |
michael@0 | 267 | /* Macros for state table entries */ |
michael@0 | 268 | #define MBCS_ENTRY_TRANSITION(state, offset) (int32_t)(((int32_t)(state)<<24L)|(offset)) |
michael@0 | 269 | #define MBCS_ENTRY_TRANSITION_SET_OFFSET(entry, offset) (int32_t)(((entry)&0xff000000)|(offset)) |
michael@0 | 270 | #define MBCS_ENTRY_TRANSITION_ADD_OFFSET(entry, offset) (int32_t)((entry)+(offset)) |
michael@0 | 271 | |
michael@0 | 272 | #define MBCS_ENTRY_FINAL(state, action, value) (int32_t)(0x80000000|((int32_t)(state)<<24L)|((action)<<20L)|(value)) |
michael@0 | 273 | #define MBCS_ENTRY_SET_FINAL(entry) (int32_t)((entry)|0x80000000) |
michael@0 | 274 | #define MBCS_ENTRY_FINAL_SET_ACTION(entry, action) (int32_t)(((entry)&0xff0fffff)|((int32_t)(action)<<20L)) |
michael@0 | 275 | #define MBCS_ENTRY_FINAL_SET_VALUE(entry, value) (int32_t)(((entry)&0xfff00000)|(value)) |
michael@0 | 276 | #define MBCS_ENTRY_FINAL_SET_ACTION_VALUE(entry, action, value) (int32_t)(((entry)&0xff000000)|((int32_t)(action)<<20L)|(value)) |
michael@0 | 277 | |
michael@0 | 278 | #define MBCS_ENTRY_SET_STATE(entry, state) (int32_t)(((entry)&0x80ffffff)|((int32_t)(state)<<24L)) |
michael@0 | 279 | |
michael@0 | 280 | #define MBCS_ENTRY_STATE(entry) ((((uint32_t)entry)>>24)&0x7f) |
michael@0 | 281 | |
michael@0 | 282 | #define MBCS_ENTRY_IS_TRANSITION(entry) ((entry)>=0) |
michael@0 | 283 | #define MBCS_ENTRY_IS_FINAL(entry) ((entry)<0) |
michael@0 | 284 | |
michael@0 | 285 | #define MBCS_ENTRY_TRANSITION_STATE(entry) (((uint32_t)entry)>>24) |
michael@0 | 286 | #define MBCS_ENTRY_TRANSITION_OFFSET(entry) ((entry)&0xffffff) |
michael@0 | 287 | |
michael@0 | 288 | #define MBCS_ENTRY_FINAL_STATE(entry) ((((uint32_t)entry)>>24)&0x7f) |
michael@0 | 289 | #define MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry) ((entry)<(int32_t)0x80100000) |
michael@0 | 290 | #define MBCS_ENTRY_FINAL_ACTION(entry) ((((uint32_t)entry)>>20)&0xf) |
michael@0 | 291 | #define MBCS_ENTRY_FINAL_VALUE(entry) ((entry)&0xfffff) |
michael@0 | 292 | #define MBCS_ENTRY_FINAL_VALUE_16(entry) (uint16_t)(entry) |
michael@0 | 293 | |
michael@0 | 294 | #define IS_ASCII_ROUNDTRIP(b, asciiRoundtrips) (((asciiRoundtrips) & (1<<((b)>>2)))!=0) |
michael@0 | 295 | |
michael@0 | 296 | /* single-byte fromUnicode: get the 16-bit result word */ |
michael@0 | 297 | #define MBCS_SINGLE_RESULT_FROM_U(table, results, c) (results)[ (table)[ (table)[(c)>>10] +(((c)>>4)&0x3f) ] +((c)&0xf) ] |
michael@0 | 298 | |
michael@0 | 299 | /* single-byte fromUnicode using the sbcsIndex */ |
michael@0 | 300 | #define SBCS_RESULT_FROM_LOW_BMP(table, results, c) (results)[ (table)[(c)>>6] +((c)&0x3f) ] |
michael@0 | 301 | |
michael@0 | 302 | /* single-byte fromUTF8 using the sbcsIndex; l and t must be masked externally; can be l=0 and t<=0x7f */ |
michael@0 | 303 | #define SBCS_RESULT_FROM_UTF8(table, results, l, t) (results)[ (table)[l] +(t) ] |
michael@0 | 304 | |
michael@0 | 305 | /* multi-byte fromUnicode: get the 32-bit stage 2 entry */ |
michael@0 | 306 | #define MBCS_STAGE_2_FROM_U(table, c) ((const uint32_t *)(table))[ (table)[(c)>>10] +(((c)>>4)&0x3f) ] |
michael@0 | 307 | #define MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c) ( ((stage2Entry) & ((uint32_t)1<< (16+((c)&0xf)) )) !=0) |
michael@0 | 308 | |
michael@0 | 309 | #define MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c) ((uint16_t *)(bytes))[16*(uint32_t)(uint16_t)(stage2Entry)+((c)&0xf)] |
michael@0 | 310 | #define MBCS_VALUE_4_FROM_STAGE_2(bytes, stage2Entry, c) ((uint32_t *)(bytes))[16*(uint32_t)(uint16_t)(stage2Entry)+((c)&0xf)] |
michael@0 | 311 | |
michael@0 | 312 | #define MBCS_POINTER_3_FROM_STAGE_2(bytes, stage2Entry, c) ((bytes)+(16*(uint32_t)(uint16_t)(stage2Entry)+((c)&0xf))*3) |
michael@0 | 313 | |
michael@0 | 314 | /* double-byte fromUnicode using the mbcsIndex */ |
michael@0 | 315 | #define DBCS_RESULT_FROM_MOST_BMP(table, results, c) (results)[ (table)[(c)>>6] +((c)&0x3f) ] |
michael@0 | 316 | |
michael@0 | 317 | /* double-byte fromUTF8 using the mbcsIndex; l and t1 combined into lt1; lt1 and t2 must be masked externally */ |
michael@0 | 318 | #define DBCS_RESULT_FROM_UTF8(table, results, lt1, t2) (results)[ (table)[lt1] +(t2) ] |
michael@0 | 319 | |
michael@0 | 320 | |
michael@0 | 321 | /** |
michael@0 | 322 | * MBCS output types for conversions from Unicode. |
michael@0 | 323 | * These per-converter types determine the storage method in stage 3 of the lookup table, |
michael@0 | 324 | * mostly how many bytes are stored per entry. |
michael@0 | 325 | */ |
michael@0 | 326 | enum { |
michael@0 | 327 | MBCS_OUTPUT_1, /* 0 */ |
michael@0 | 328 | MBCS_OUTPUT_2, /* 1 */ |
michael@0 | 329 | MBCS_OUTPUT_3, /* 2 */ |
michael@0 | 330 | MBCS_OUTPUT_4, /* 3 */ |
michael@0 | 331 | |
michael@0 | 332 | MBCS_OUTPUT_3_EUC=8, /* 8 */ |
michael@0 | 333 | MBCS_OUTPUT_4_EUC, /* 9 */ |
michael@0 | 334 | |
michael@0 | 335 | MBCS_OUTPUT_2_SISO=12, /* c */ |
michael@0 | 336 | MBCS_OUTPUT_2_HZ, /* d */ |
michael@0 | 337 | |
michael@0 | 338 | MBCS_OUTPUT_EXT_ONLY, /* e */ |
michael@0 | 339 | |
michael@0 | 340 | MBCS_OUTPUT_COUNT, |
michael@0 | 341 | |
michael@0 | 342 | MBCS_OUTPUT_DBCS_ONLY=0xdb /* runtime-only type for DBCS-only handling of SISO tables */ |
michael@0 | 343 | }; |
michael@0 | 344 | |
michael@0 | 345 | /** |
michael@0 | 346 | * Fallbacks to Unicode are stored outside the normal state table and code point structures |
michael@0 | 347 | * in a vector of items of this type. They are sorted by offset. |
michael@0 | 348 | */ |
michael@0 | 349 | typedef struct { |
michael@0 | 350 | uint32_t offset; |
michael@0 | 351 | UChar32 codePoint; |
michael@0 | 352 | } _MBCSToUFallback; |
michael@0 | 353 | |
michael@0 | 354 | /** Constants for fast and UTF-8-friendly conversion. */ |
michael@0 | 355 | enum { |
michael@0 | 356 | SBCS_FAST_MAX=0x0fff, /* maximum code point with UTF-8-friendly SBCS runtime code, see makeconv SBCS_UTF8_MAX */ |
michael@0 | 357 | SBCS_FAST_LIMIT=SBCS_FAST_MAX+1, /* =0x1000 */ |
michael@0 | 358 | MBCS_FAST_MAX=0xd7ff, /* maximum code point with UTF-8-friendly MBCS runtime code, see makeconv MBCS_UTF8_MAX */ |
michael@0 | 359 | MBCS_FAST_LIMIT=MBCS_FAST_MAX+1 /* =0xd800 */ |
michael@0 | 360 | }; |
michael@0 | 361 | |
michael@0 | 362 | /** |
michael@0 | 363 | * This is the MBCS part of the UConverterTable union (a runtime data structure). |
michael@0 | 364 | * It keeps all the per-converter data and points into the loaded mapping tables. |
michael@0 | 365 | * |
michael@0 | 366 | * utf8Friendly data structures added with _MBCSHeader.version 4.3 |
michael@0 | 367 | */ |
michael@0 | 368 | typedef struct UConverterMBCSTable { |
michael@0 | 369 | /* toUnicode */ |
michael@0 | 370 | uint8_t countStates, dbcsOnlyState, stateTableOwned; |
michael@0 | 371 | uint32_t countToUFallbacks; |
michael@0 | 372 | |
michael@0 | 373 | const int32_t (*stateTable)/*[countStates]*/[256]; |
michael@0 | 374 | int32_t (*swapLFNLStateTable)/*[countStates]*/[256]; /* for swaplfnl */ |
michael@0 | 375 | const uint16_t *unicodeCodeUnits/*[countUnicodeResults]*/; |
michael@0 | 376 | const _MBCSToUFallback *toUFallbacks; |
michael@0 | 377 | |
michael@0 | 378 | /* fromUnicode */ |
michael@0 | 379 | const uint16_t *fromUnicodeTable; |
michael@0 | 380 | const uint16_t *mbcsIndex; /* for fast conversion from most of BMP to MBCS (utf8Friendly data) */ |
michael@0 | 381 | uint16_t sbcsIndex[SBCS_FAST_LIMIT>>6]; /* for fast conversion from low BMP to SBCS (utf8Friendly data) */ |
michael@0 | 382 | const uint8_t *fromUnicodeBytes; |
michael@0 | 383 | uint8_t *swapLFNLFromUnicodeBytes; /* for swaplfnl */ |
michael@0 | 384 | uint32_t fromUBytesLength; |
michael@0 | 385 | uint8_t outputType, unicodeMask; |
michael@0 | 386 | UBool utf8Friendly; /* for utf8Friendly data */ |
michael@0 | 387 | UChar maxFastUChar; /* for utf8Friendly data */ |
michael@0 | 388 | |
michael@0 | 389 | /* roundtrips */ |
michael@0 | 390 | uint32_t asciiRoundtrips; |
michael@0 | 391 | |
michael@0 | 392 | /* reconstituted data that was omitted from the .cnv file */ |
michael@0 | 393 | uint8_t *reconstitutedData; |
michael@0 | 394 | |
michael@0 | 395 | /* converter name for swaplfnl */ |
michael@0 | 396 | char *swapLFNLName; |
michael@0 | 397 | |
michael@0 | 398 | /* extension data */ |
michael@0 | 399 | struct UConverterSharedData *baseSharedData; |
michael@0 | 400 | const int32_t *extIndexes; |
michael@0 | 401 | } UConverterMBCSTable; |
michael@0 | 402 | |
michael@0 | 403 | #define UCNV_MBCS_TABLE_INITIALIZER { \ |
michael@0 | 404 | /* toUnicode */ \ |
michael@0 | 405 | 0, 0, 0, \ |
michael@0 | 406 | 0, \ |
michael@0 | 407 | \ |
michael@0 | 408 | NULL, \ |
michael@0 | 409 | NULL, \ |
michael@0 | 410 | NULL, \ |
michael@0 | 411 | NULL, \ |
michael@0 | 412 | \ |
michael@0 | 413 | /* fromUnicode */ \ |
michael@0 | 414 | NULL, \ |
michael@0 | 415 | NULL, \ |
michael@0 | 416 | { 0 }, \ |
michael@0 | 417 | NULL, \ |
michael@0 | 418 | NULL, \ |
michael@0 | 419 | 0, \ |
michael@0 | 420 | 0, 0, \ |
michael@0 | 421 | FALSE, \ |
michael@0 | 422 | 0, \ |
michael@0 | 423 | \ |
michael@0 | 424 | /* roundtrips */ \ |
michael@0 | 425 | 0, \ |
michael@0 | 426 | \ |
michael@0 | 427 | /* reconstituted data that was omitted from the .cnv file */ \ |
michael@0 | 428 | NULL, \ |
michael@0 | 429 | \ |
michael@0 | 430 | /* converter name for swaplfnl */ \ |
michael@0 | 431 | NULL, \ |
michael@0 | 432 | \ |
michael@0 | 433 | /* extension data */ \ |
michael@0 | 434 | NULL, \ |
michael@0 | 435 | NULL \ |
michael@0 | 436 | } |
michael@0 | 437 | |
michael@0 | 438 | enum { |
michael@0 | 439 | MBCS_OPT_LENGTH_MASK=0x3f, |
michael@0 | 440 | MBCS_OPT_NO_FROM_U=0x40, |
michael@0 | 441 | /* |
michael@0 | 442 | * If any of the following options bits are set, |
michael@0 | 443 | * then the file must be rejected. |
michael@0 | 444 | */ |
michael@0 | 445 | MBCS_OPT_INCOMPATIBLE_MASK=0xffc0, |
michael@0 | 446 | /* |
michael@0 | 447 | * Remove bits from this mask as more options are recognized |
michael@0 | 448 | * by all implementations that use this constant. |
michael@0 | 449 | */ |
michael@0 | 450 | MBCS_OPT_UNKNOWN_INCOMPATIBLE_MASK=0xff80 |
michael@0 | 451 | }; |
michael@0 | 452 | |
michael@0 | 453 | enum { |
michael@0 | 454 | MBCS_HEADER_V4_LENGTH=8, |
michael@0 | 455 | MBCS_HEADER_V5_MIN_LENGTH=9 |
michael@0 | 456 | }; |
michael@0 | 457 | |
michael@0 | 458 | /** |
michael@0 | 459 | * MBCS data header. See data format description above. |
michael@0 | 460 | */ |
michael@0 | 461 | typedef struct { |
michael@0 | 462 | UVersionInfo version; |
michael@0 | 463 | uint32_t countStates, |
michael@0 | 464 | countToUFallbacks, |
michael@0 | 465 | offsetToUCodeUnits, |
michael@0 | 466 | offsetFromUTable, |
michael@0 | 467 | offsetFromUBytes, |
michael@0 | 468 | flags, |
michael@0 | 469 | fromUBytesLength; |
michael@0 | 470 | |
michael@0 | 471 | /* new and required in version 5 */ |
michael@0 | 472 | uint32_t options; |
michael@0 | 473 | |
michael@0 | 474 | /* new and optional in version 5; used if options&MBCS_OPT_NO_FROM_U */ |
michael@0 | 475 | uint32_t fullStage2Length; /* number of 32-bit units */ |
michael@0 | 476 | } _MBCSHeader; |
michael@0 | 477 | |
michael@0 | 478 | #define UCNV_MBCS_HEADER_INITIALIZER { { 0 }, 0, 0, 0, 0, 0, 0, 0, 0, 0 } |
michael@0 | 479 | |
michael@0 | 480 | /* |
michael@0 | 481 | * This is a simple version of _MBCSGetNextUChar() that is used |
michael@0 | 482 | * by other converter implementations. |
michael@0 | 483 | * It only returns an "assigned" result if it consumes the entire input. |
michael@0 | 484 | * It does not use state from the converter, nor error codes. |
michael@0 | 485 | * It does not handle the EBCDIC swaplfnl option (set in UConverter). |
michael@0 | 486 | * It handles conversion extensions but not GB 18030. |
michael@0 | 487 | * |
michael@0 | 488 | * Return value: |
michael@0 | 489 | * U+fffe unassigned |
michael@0 | 490 | * U+ffff illegal |
michael@0 | 491 | * otherwise the Unicode code point |
michael@0 | 492 | */ |
michael@0 | 493 | U_CFUNC UChar32 |
michael@0 | 494 | ucnv_MBCSSimpleGetNextUChar(UConverterSharedData *sharedData, |
michael@0 | 495 | const char *source, int32_t length, |
michael@0 | 496 | UBool useFallback); |
michael@0 | 497 | |
michael@0 | 498 | /** |
michael@0 | 499 | * This version of _MBCSSimpleGetNextUChar() is optimized for single-byte, single-state codepages. |
michael@0 | 500 | * It does not handle the EBCDIC swaplfnl option (set in UConverter). |
michael@0 | 501 | * It does not handle conversion extensions (_extToU()). |
michael@0 | 502 | */ |
michael@0 | 503 | U_CFUNC UChar32 |
michael@0 | 504 | ucnv_MBCSSingleSimpleGetNextUChar(UConverterSharedData *sharedData, |
michael@0 | 505 | uint8_t b, UBool useFallback); |
michael@0 | 506 | |
michael@0 | 507 | /** |
michael@0 | 508 | * This macro version of _MBCSSingleSimpleGetNextUChar() gets a code point from a byte. |
michael@0 | 509 | * It works for single-byte, single-state codepages that only map |
michael@0 | 510 | * to and from BMP code points, and it always |
michael@0 | 511 | * returns fallback values. |
michael@0 | 512 | */ |
michael@0 | 513 | #define _MBCS_SINGLE_SIMPLE_GET_NEXT_BMP(sharedData, b) \ |
michael@0 | 514 | (UChar)MBCS_ENTRY_FINAL_VALUE_16((sharedData)->mbcs.stateTable[0][(uint8_t)(b)]) |
michael@0 | 515 | |
michael@0 | 516 | /** |
michael@0 | 517 | * This is an internal function that allows other converter implementations |
michael@0 | 518 | * to check whether a byte is a lead byte. |
michael@0 | 519 | */ |
michael@0 | 520 | U_CFUNC UBool |
michael@0 | 521 | ucnv_MBCSIsLeadByte(UConverterSharedData *sharedData, char byte); |
michael@0 | 522 | |
michael@0 | 523 | /** This is a macro version of _MBCSIsLeadByte(). */ |
michael@0 | 524 | #define _MBCS_IS_LEAD_BYTE(sharedData, byte) \ |
michael@0 | 525 | (UBool)MBCS_ENTRY_IS_TRANSITION((sharedData)->mbcs.stateTable[0][(uint8_t)(byte)]) |
michael@0 | 526 | |
michael@0 | 527 | /* |
michael@0 | 528 | * This is another simple conversion function for internal use by other |
michael@0 | 529 | * conversion implementations. |
michael@0 | 530 | * It does not use the converter state nor call callbacks. |
michael@0 | 531 | * It does not handle the EBCDIC swaplfnl option (set in UConverter). |
michael@0 | 532 | * It handles conversion extensions but not GB 18030. |
michael@0 | 533 | * |
michael@0 | 534 | * It converts one single Unicode code point into codepage bytes, encoded |
michael@0 | 535 | * as one 32-bit value. The function returns the number of bytes in *pValue: |
michael@0 | 536 | * 1..4 the number of bytes in *pValue |
michael@0 | 537 | * 0 unassigned (*pValue undefined) |
michael@0 | 538 | * -1 illegal (currently not used, *pValue undefined) |
michael@0 | 539 | * |
michael@0 | 540 | * *pValue will contain the resulting bytes with the last byte in bits 7..0, |
michael@0 | 541 | * the second to last byte in bits 15..8, etc. |
michael@0 | 542 | * Currently, the function assumes but does not check that 0<=c<=0x10ffff. |
michael@0 | 543 | */ |
michael@0 | 544 | U_CFUNC int32_t |
michael@0 | 545 | ucnv_MBCSFromUChar32(UConverterSharedData *sharedData, |
michael@0 | 546 | UChar32 c, uint32_t *pValue, |
michael@0 | 547 | UBool useFallback); |
michael@0 | 548 | |
michael@0 | 549 | /** |
michael@0 | 550 | * This version of _MBCSFromUChar32() is optimized for single-byte codepages. |
michael@0 | 551 | * It does not handle the EBCDIC swaplfnl option (set in UConverter). |
michael@0 | 552 | * |
michael@0 | 553 | * It returns the codepage byte for the code point, or -1 if it is unassigned. |
michael@0 | 554 | */ |
michael@0 | 555 | U_CFUNC int32_t |
michael@0 | 556 | ucnv_MBCSSingleFromUChar32(UConverterSharedData *sharedData, |
michael@0 | 557 | UChar32 c, |
michael@0 | 558 | UBool useFallback); |
michael@0 | 559 | |
michael@0 | 560 | /** |
michael@0 | 561 | * SBCS, DBCS, and EBCDIC_STATEFUL are replaced by MBCS, but |
michael@0 | 562 | * we cheat a little about the type, returning the old types if appropriate. |
michael@0 | 563 | */ |
michael@0 | 564 | U_CFUNC UConverterType |
michael@0 | 565 | ucnv_MBCSGetType(const UConverter* converter); |
michael@0 | 566 | |
michael@0 | 567 | U_CFUNC void |
michael@0 | 568 | ucnv_MBCSFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs, |
michael@0 | 569 | UErrorCode *pErrorCode); |
michael@0 | 570 | U_CFUNC void |
michael@0 | 571 | ucnv_MBCSToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs, |
michael@0 | 572 | UErrorCode *pErrorCode); |
michael@0 | 573 | |
michael@0 | 574 | /* |
michael@0 | 575 | * Internal function returning a UnicodeSet for toUnicode() conversion. |
michael@0 | 576 | * Currently only used for ISO-2022-CN, and only handles roundtrip mappings. |
michael@0 | 577 | * In the future, if we add support for fallback sets, this function |
michael@0 | 578 | * needs to be updated. |
michael@0 | 579 | * Handles extensions. |
michael@0 | 580 | * Does not empty the set first. |
michael@0 | 581 | */ |
michael@0 | 582 | U_CFUNC void |
michael@0 | 583 | ucnv_MBCSGetUnicodeSetForUnicode(const UConverterSharedData *sharedData, |
michael@0 | 584 | const USetAdder *sa, |
michael@0 | 585 | UConverterUnicodeSet which, |
michael@0 | 586 | UErrorCode *pErrorCode); |
michael@0 | 587 | |
michael@0 | 588 | /* |
michael@0 | 589 | * Same as ucnv_MBCSGetUnicodeSetForUnicode() but |
michael@0 | 590 | * the set can be filtered by encoding scheme. |
michael@0 | 591 | * Used by stateful converters which share regular conversion tables |
michael@0 | 592 | * but only use a subset of their mappings. |
michael@0 | 593 | */ |
michael@0 | 594 | U_CFUNC void |
michael@0 | 595 | ucnv_MBCSGetFilteredUnicodeSetForUnicode(const UConverterSharedData *sharedData, |
michael@0 | 596 | const USetAdder *sa, |
michael@0 | 597 | UConverterUnicodeSet which, |
michael@0 | 598 | UConverterSetFilter filter, |
michael@0 | 599 | UErrorCode *pErrorCode); |
michael@0 | 600 | |
michael@0 | 601 | #endif |
michael@0 | 602 | |
michael@0 | 603 | #endif |