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