2 ******************************************************************************
4 * Copyright (C) 2000-2010, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
8 * file name: ucnvmbcs.c
10 * tab size: 8 (not used)
13 * created on: 2000jul03
14 * created by: Markus W. Scherer
16 * The current code in this file replaces the previous implementation
17 * of conversion code from multi-byte codepages to Unicode and back.
18 * This implementation supports the following:
19 * - legacy variable-length codepages with up to 4 bytes per character
20 * - all Unicode code points (up to 0x10ffff)
21 * - efficient distinction of unassigned vs. illegal byte sequences
22 * - it is possible in fromUnicode() to directly deal with simple
23 * stateful encodings (used for EBCDIC_STATEFUL)
24 * - it is possible to convert Unicode code points
25 * to a single zero byte (but not as a fallback except for SBCS)
27 * Remaining limitations in fromUnicode:
28 * - byte sequences must not have leading zero bytes
29 * - except for SBCS codepages: no fallback mapping from Unicode to a zero byte
30 * - limitation to up to 4 bytes per character
32 * ICU 2.8 (late 2003) adds a secondary data structure which lifts some of these
33 * limitations and adds m:n character mappings and other features.
34 * See ucnv_ext.h for details.
38 * 5/6/2001 Ram Moved MBCS_SINGLE_RESULT_FROM_U,MBCS_STAGE_2_FROM_U,
39 * MBCS_VALUE_2_FROM_STAGE_2, MBCS_VALUE_4_FROM_STAGE_2
40 * macros to ucnvmbcs.h file
43 #include "unicode/utypes.h"
45 #if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION
47 #include "unicode/ucnv.h"
48 #include "unicode/ucnv_cb.h"
49 #include "unicode/udata.h"
50 #include "unicode/uset.h"
59 /* control optimizations according to the platform */
60 #define MBCS_UNROLL_SINGLE_TO_BMP 1
61 #define MBCS_UNROLL_SINGLE_FROM_BMP 0
64 * _MBCSHeader versions 5.3 & 4.3
65 * (Note that the _MBCSHeader version is in addition to the converter formatVersion.)
67 * This version is optional. Version 5 is used for incompatible data format changes.
68 * makeconv will continue to generate version 4 files if possible.
70 * Changes from version 4:
72 * The main difference is an additional _MBCSHeader field with
73 * - the length (number of uint32_t) of the _MBCSHeader
74 * - flags for further incompatible data format changes
75 * - flags for further, backward compatible data format changes
77 * The MBCS_OPT_FROM_U flag indicates that most of the fromUnicode data is omitted from
78 * the file and needs to be reconstituted at load time.
79 * This requires a utf8Friendly format with an additional mbcsIndex table for fast
80 * (and UTF-8-friendly) fromUnicode conversion for Unicode code points up to maxFastUChar.
81 * (For details about these structures see below, and see ucnvmbcs.h.)
83 * utf8Friendly also implies that the fromUnicode mappings are stored in ascending order
84 * of the Unicode code points. (This requires that the .ucm file has the |0 etc.
85 * precision markers for all mappings.)
87 * All fallbacks have been moved to the extension table, leaving only roundtrips in the
88 * omitted data that can be reconstituted from the toUnicode data.
90 * Of the stage 2 table, the part corresponding to maxFastUChar and below is omitted.
91 * With only roundtrip mappings in the base fromUnicode data, this part is fully
92 * redundant with the mbcsIndex and will be reconstituted from that (also using the
93 * stage 1 table which contains the information about how stage 2 was compacted).
95 * The rest of the stage 2 table, the part for code points above maxFastUChar,
96 * is stored in the file and will be appended to the reconstituted part.
98 * The entire fromUBytes array is omitted from the file and will be reconstitued.
99 * This is done by enumerating all toUnicode roundtrip mappings, performing
100 * each mapping (using the stage 1 and reconstituted stage 2 tables) and
101 * writing instead of reading the byte values.
103 * _MBCSHeader version 4.3
105 * Change from version 4.2:
106 * - Optional utf8Friendly data structures, with 64-entry stage 3 block
107 * allocation for parts of the BMP, and an additional mbcsIndex in non-SBCS
108 * files which can be used instead of stages 1 & 2.
109 * Faster lookups for roundtrips from most commonly used characters,
110 * and lookups from UTF-8 byte sequences with a natural bit distribution.
111 * See ucnvmbcs.h for more details.
113 * Change from version 4.1:
114 * - Added an optional extension table structure at the end of the .cnv file.
115 * It is present if the upper bits of the header flags field contains a non-zero
117 * Files that contain only a conversion table and no base table
118 * use the special outputType MBCS_OUTPUT_EXT_ONLY.
119 * These contain the base table name between the MBCS header and the extension
122 * Change from version 4.0:
123 * - Replace header.reserved with header.fromUBytesLength so that all
124 * fields in the data have length.
126 * Changes from version 3 (for performance improvements):
127 * - new bit distribution for state table entries
128 * - reordered action codes
129 * - new data structure for single-byte fromUnicode
130 * + stage 2 only contains indexes
131 * + stage 3 stores 16 bits per character with classification bits 15..8
132 * - no multiplier for stage 1 entries
133 * - stage 2 for non-single-byte codepages contains the index and the flags in
135 * - 2-byte and 4-byte fromUnicode results are stored directly as 16/32-bit integers
137 * For more details about old versions of the MBCS data structure, see
138 * the corresponding versions of this file.
140 * Converting stateless codepage data ---------------------------------------***
141 * (or codepage data with simple states) to Unicode.
143 * Data structure and algorithm for converting from complex legacy codepages
144 * to Unicode. (Designed before 2000-may-22.)
146 * The basic idea is that the structure of legacy codepages can be described
148 * When reading a byte stream, each input byte causes a state transition.
149 * Some transitions result in the output of a code point, some result in
150 * "unassigned" or "illegal" output.
151 * This is used here for character conversion.
153 * The data structure begins with a state table consisting of a row
154 * per state, with 256 entries (columns) per row for each possible input
156 * Each entry is 32 bits wide, with two formats distinguished by
157 * the sign bit (bit 31):
159 * One format for transitional entries (bit 31 not set) for non-final bytes, and
160 * one format for final entries (bit 31 set).
161 * Both formats contain the number of the next state in the same bit
163 * State 0 is the initial state.
165 * Most of the time, the offset values of subsequent states are added
166 * up to a scalar value. This value will eventually be the index of
167 * the Unicode code point in a table that follows the state table.
168 * The effect is that the code points for final state table rows
169 * are contiguous. The code points of final state rows follow each other
170 * in the order of the references to those final states by previous
173 * For some terminal states, the offset is itself the output Unicode
174 * code point (16 bits for a BMP code point or 20 bits for a supplementary
175 * code point (stored as code point minus 0x10000 so that 20 bits are enough).
176 * For others, the code point in the Unicode table is stored with either
177 * one or two code units: one for BMP code points, two for a pair of
179 * All code points for a final state entry take up the same number of code
180 * units, regardless of whether they all actually _use_ the same number
181 * of code units. This is necessary for simple array access.
183 * An additional feature comes in with what in ICU is called "fallback"
186 * In addition to round-trippable, precise, 1:1 mappings, there are often
187 * mappings defined between similar, though not the same, characters.
188 * Typically, such mappings occur only in fromUnicode mapping tables because
189 * Unicode has a superset repertoire of most other codepages. However, it
190 * is possible to provide such mappings in the toUnicode tables, too.
191 * In this case, the fallback mappings are partly integrated into the
192 * general state tables because the structure of the encoding includes their
194 * For final entries in an initial state, fallback mappings are stored in
195 * the entry itself like with roundtrip mappings.
196 * For other final entries, they are stored in the code units table if
197 * the entry is for a pair of code units.
198 * For single-unit results in the code units table, there is no space to
199 * alternatively hold a fallback mapping; in this case, the code unit
200 * is stored as U+fffe (unassigned), and the fallback mapping needs to
201 * be looked up by the scalar offset value in a separate table.
203 * "Unassigned" state entries really mean "structurally unassigned",
204 * i.e., such a byte sequence will never have a mapping result.
206 * The interpretation of the bits in each entry is as follows:
208 * Bit 31 not set, not a terminal entry ("transitional"):
210 * 23..0 offset delta, to be added up
212 * Bit 31 set, terminal ("final") entry:
213 * 30..24 next state (regardless of action code)
214 * 23..20 action code:
215 * action codes 0 and 1 result in precise-mapping Unicode code points
216 * 0 valid byte sequence
218 * 15..0 16-bit Unicode BMP code point
219 * never U+fffe or U+ffff
220 * 1 valid byte sequence
221 * 19..0 20-bit Unicode supplementary code point
222 * never U+fffe or U+ffff
224 * action codes 2 and 3 result in fallback (unidirectional-mapping) Unicode code points
225 * 2 valid byte sequence (fallback)
227 * 15..0 16-bit Unicode BMP code point as fallback result
228 * 3 valid byte sequence (fallback)
229 * 19..0 20-bit Unicode supplementary code point as fallback result
231 * action codes 4 and 5 may result in roundtrip/fallback/unassigned/illegal results
232 * depending on the code units they result in
233 * 4 valid byte sequence
235 * 8..0 final offset delta
236 * pointing to one 16-bit code unit which may be
237 * fffe unassigned -- look for a fallback for this offset
239 * 5 valid byte sequence
241 * 8..0 final offset delta
242 * pointing to two 16-bit code units
243 * (typically UTF-16 surrogates)
244 * the result depends on the first code unit as follows:
245 * 0000..d7ff roundtrip BMP code point (1st alone)
246 * d800..dbff roundtrip surrogate pair (1st, 2nd)
247 * dc00..dfff fallback surrogate pair (1st-400, 2nd)
248 * e000 roundtrip BMP code point (2nd alone)
249 * e001 fallback BMP code point (2nd alone)
252 * (the final offset deltas are at most 255 * 2,
253 * times 2 because of storing code unit pairs)
255 * 6 unassigned byte sequence
257 * 15..0 16-bit Unicode BMP code point U+fffe (new with version 2)
258 * this does not contain a final offset delta because the main
259 * purpose of this action code is to save scalar offset values;
260 * therefore, fallback values cannot be assigned to byte
261 * sequences that result in this action code
262 * 7 illegal byte sequence
264 * 15..0 16-bit Unicode BMP code point U+ffff (new with version 2)
265 * 8 state change only
267 * useful for state changes in simple stateful encodings,
268 * at Shift-In/Shift-Out codes
271 * 9..15 reserved for future use
272 * current implementations will only perform a state change
273 * and ignore bits 19..0
275 * An encoding with contiguous ranges of unassigned byte sequences, like
276 * Shift-JIS and especially EUC-TW, can be stored efficiently by having
277 * at least two states for the trail bytes:
278 * One trail byte state that results in code points, and one that only
279 * has "unassigned" and "illegal" terminal states.
281 * Note: partly by accident, this data structure supports simple stateful
282 * encodings without any additional logic.
283 * Currently, only simple Shift-In/Shift-Out schemes are handled with
284 * appropriate state tables (especially EBCDIC_STATEFUL!).
286 * MBCS version 2 added:
287 * unassigned and illegal action codes have U+fffe and U+ffff
288 * instead of unused bits; this is useful for _MBCS_SINGLE_SIMPLE_GET_NEXT_BMP()
290 * Converting from Unicode to codepage bytes --------------------------------***
292 * The conversion data structure for fromUnicode is designed for the known
293 * structure of Unicode. It maps from 21-bit code points (0..0x10ffff) to
294 * a sequence of 1..4 bytes, in addition to a flag that indicates if there is
295 * a roundtrip mapping.
297 * The lookup is done with a 3-stage trie, using 11/6/4 bits for stage 1/2/3
298 * like in the character properties table.
299 * The beginning of the trie is at offsetFromUTable, the beginning of stage 3
300 * with the resulting bytes is at offsetFromUBytes.
302 * Beginning with version 4, single-byte codepages have a significantly different
303 * trie compared to other codepages.
304 * In all cases, the entry in stage 1 is directly the index of the block of
305 * 64 entries in stage 2.
307 * Single-byte lookup:
309 * Stage 2 only contains 16-bit indexes directly to the 16-blocks in stage 3.
310 * Stage 3 contains one 16-bit word per result:
311 * Bits 15..8 indicate the kind of result:
313 * c fallback result from private-use code point
314 * 8 fallback result from other code points
316 * Bits 7..0 contain the codepage byte. A zero byte is always possible.
318 * In version 4.3, the runtime code can build an sbcsIndex for a utf8Friendly
319 * file. For 2-byte UTF-8 byte sequences and some 3-byte sequences the lookup
320 * becomes a 2-stage (single-index) trie lookup with 6 bits for stage 3.
321 * ASCII code points can be looked up with a linear array access into stage 3.
322 * See maxFastUChar and other details in ucnvmbcs.h.
326 * Stage 2 contains a 32-bit word for each 16-block in stage 3:
327 * Bits 31..16 contain flags for which stage 3 entries contain roundtrip results
328 * test: MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c)
329 * If this test is false, then a non-zero result will be interpreted as
330 * a fallback mapping.
331 * Bits 15..0 contain the index to stage 3, which must be multiplied by 16*(bytes per char)
333 * Stage 3 contains 2, 3, or 4 bytes per result.
334 * 2 or 4 bytes are stored as uint16_t/uint32_t in platform endianness,
335 * while 3 bytes are stored as bytes in big-endian order.
336 * Leading zero bytes are ignored, and the number of bytes is counted.
337 * A zero byte mapping result is possible as a roundtrip result.
338 * For some output types, the actual result is processed from this;
339 * see ucnv_MBCSFromUnicodeWithOffsets().
341 * Note that stage 1 always contains 0x440=1088 entries (0x440==0x110000>>10),
342 * or (version 3 and up) for BMP-only codepages, it contains 64 entries.
344 * In version 4.3, a utf8Friendly file contains an mbcsIndex table.
345 * For 2-byte UTF-8 byte sequences and most 3-byte sequences the lookup
346 * becomes a 2-stage (single-index) trie lookup with 6 bits for stage 3.
347 * ASCII code points can be looked up with a linear array access into stage 3.
348 * See maxFastUChar, mbcsIndex and other details in ucnvmbcs.h.
350 * In version 3, stage 2 blocks may overlap by multiples of the multiplier
352 * In version 4, stage 2 blocks (and for single-byte codepages, stage 3 blocks)
353 * may overlap by any number of entries.
355 * MBCS version 2 added:
356 * the converter checks for known output types, which allows
357 * adding new ones without crashing an unaware converter
360 static const UConverterImpl _SBCSUTF8Impl
;
361 static const UConverterImpl _DBCSUTF8Impl
;
363 /* GB 18030 data ------------------------------------------------------------ */
365 /* helper macros for linear values for GB 18030 four-byte sequences */
366 #define LINEAR_18030(a, b, c, d) ((((a)*10+(b))*126L+(c))*10L+(d))
368 #define LINEAR_18030_BASE LINEAR_18030(0x81, 0x30, 0x81, 0x30)
370 #define LINEAR(x) LINEAR_18030(x>>24, (x>>16)&0xff, (x>>8)&0xff, x&0xff)
373 * Some ranges of GB 18030 where both the Unicode code points and the
374 * GB four-byte sequences are contiguous and are handled algorithmically by
375 * the special callback functions below.
376 * The values are start & end of Unicode & GB codes.
378 * Note that single surrogates are not mapped by GB 18030
379 * as of the re-released mapping tables from 2000-nov-30.
381 static const uint32_t
382 gb18030Ranges
[13][4]={
383 {0x10000, 0x10FFFF, LINEAR(0x90308130), LINEAR(0xE3329A35)},
384 {0x9FA6, 0xD7FF, LINEAR(0x82358F33), LINEAR(0x8336C738)},
385 {0x0452, 0x200F, LINEAR(0x8130D330), LINEAR(0x8136A531)},
386 {0xE865, 0xF92B, LINEAR(0x8336D030), LINEAR(0x84308534)},
387 {0x2643, 0x2E80, LINEAR(0x8137A839), LINEAR(0x8138FD38)},
388 {0xFA2A, 0xFE2F, LINEAR(0x84309C38), LINEAR(0x84318537)},
389 {0x3CE1, 0x4055, LINEAR(0x8231D438), LINEAR(0x8232AF32)},
390 {0x361B, 0x3917, LINEAR(0x8230A633), LINEAR(0x8230F237)},
391 {0x49B8, 0x4C76, LINEAR(0x8234A131), LINEAR(0x8234E733)},
392 {0x4160, 0x4336, LINEAR(0x8232C937), LINEAR(0x8232F837)},
393 {0x478E, 0x4946, LINEAR(0x8233E838), LINEAR(0x82349638)},
394 {0x44D7, 0x464B, LINEAR(0x8233A339), LINEAR(0x8233C931)},
395 {0xFFE6, 0xFFFF, LINEAR(0x8431A234), LINEAR(0x8431A439)}
398 /* bit flag for UConverter.options indicating GB 18030 special handling */
399 #define _MBCS_OPTION_GB18030 0x8000
401 /* bit flag for UConverter.options indicating KEIS,JEF,JIF special handling */
402 #define _MBCS_OPTION_KEIS 0x01000
403 #define _MBCS_OPTION_JEF 0x02000
404 #define _MBCS_OPTION_JIPS 0x04000
406 #define KEIS_SO_CHAR_1 0x0A
407 #define KEIS_SO_CHAR_2 0x42
408 #define KEIS_SI_CHAR_1 0x0A
409 #define KEIS_SI_CHAR_2 0x41
411 #define JEF_SO_CHAR 0x28
412 #define JEF_SI_CHAR 0x29
414 #define JIPS_SO_CHAR_1 0x1A
415 #define JIPS_SO_CHAR_2 0x70
416 #define JIPS_SI_CHAR_1 0x1A
417 #define JIPS_SI_CHAR_2 0x71
423 typedef enum SISO_Option SISO_Option
;
425 static int32_t getSISOBytes(SISO_Option option
, uint32_t cnvOption
, uint8_t *value
) {
426 int32_t SISOLength
= 0;
430 if ((cnvOption
&_MBCS_OPTION_KEIS
)!=0) {
431 value
[0] = KEIS_SI_CHAR_1
;
432 value
[1] = KEIS_SI_CHAR_2
;
434 } else if ((cnvOption
&_MBCS_OPTION_JEF
)!=0) {
435 value
[0] = JEF_SI_CHAR
;
437 } else if ((cnvOption
&_MBCS_OPTION_JIPS
)!=0) {
438 value
[0] = JIPS_SI_CHAR_1
;
439 value
[1] = JIPS_SI_CHAR_2
;
447 if ((cnvOption
&_MBCS_OPTION_KEIS
)!=0) {
448 value
[0] = KEIS_SO_CHAR_1
;
449 value
[1] = KEIS_SO_CHAR_2
;
451 } else if ((cnvOption
&_MBCS_OPTION_JEF
)!=0) {
452 value
[0] = JEF_SO_CHAR
;
454 } else if ((cnvOption
&_MBCS_OPTION_JIPS
)!=0) {
455 value
[0] = JIPS_SO_CHAR_1
;
456 value
[1] = JIPS_SO_CHAR_2
;
464 /* Should never happen. */
471 /* Miscellaneous ------------------------------------------------------------ */
474 * Callback from ucnv_MBCSEnumToUnicode(), takes 32 mappings from
475 * consecutive sequences of bytes, starting from the one encoded in value,
476 * to Unicode code points. (Multiple mappings to reduce per-function call overhead.)
477 * Does not currently support m:n mappings or reverse fallbacks.
478 * This function will not be called for sequences of bytes with leading zeros.
480 * @param context an opaque pointer, as passed into ucnv_MBCSEnumToUnicode()
481 * @param value contains 1..4 bytes of the first byte sequence, right-aligned
482 * @param codePoints resulting Unicode code points, or negative if a byte sequence does
483 * not map to anything
484 * @return TRUE to continue enumeration, FALSE to stop
486 typedef UBool U_CALLCONV
487 UConverterEnumToUCallback(const void *context
, uint32_t value
, UChar32 codePoints
[32]);
489 /* similar to ucnv_MBCSGetNextUChar() but recursive */
491 enumToU(UConverterMBCSTable
*mbcsTable
, int8_t stateProps
[],
492 int32_t state
, uint32_t offset
,
494 UConverterEnumToUCallback
*callback
, const void *context
,
495 UErrorCode
*pErrorCode
) {
496 UChar32 codePoints
[32];
498 const uint16_t *unicodeCodeUnits
;
499 UChar32 anyCodePoints
;
502 row
=mbcsTable
->stateTable
[state
];
503 unicodeCodeUnits
=mbcsTable
->unicodeCodeUnits
;
506 anyCodePoints
=-1; /* becomes non-negative if there is a mapping */
508 b
=(stateProps
[state
]&0x38)<<2;
509 if(b
==0 && stateProps
[state
]>=0x40) {
510 /* skip byte sequences with leading zeros because they are not stored in the fromUnicode table */
511 codePoints
[0]=U_SENTINEL
;
514 limit
=((stateProps
[state
]&7)+1)<<5;
516 int32_t entry
=row
[b
];
517 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
518 int32_t nextState
=MBCS_ENTRY_TRANSITION_STATE(entry
);
519 if(stateProps
[nextState
]>=0) {
520 /* recurse to a state with non-ignorable actions */
522 mbcsTable
, stateProps
, nextState
,
523 offset
+MBCS_ENTRY_TRANSITION_OFFSET(entry
),
530 codePoints
[b
&0x1f]=U_SENTINEL
;
536 * An if-else-if chain provides more reliable performance for
537 * the most common cases compared to a switch.
539 action
=MBCS_ENTRY_FINAL_ACTION(entry
);
540 if(action
==MBCS_STATE_VALID_DIRECT_16
) {
541 /* output BMP code point */
542 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
543 } else if(action
==MBCS_STATE_VALID_16
) {
544 int32_t finalOffset
=offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
);
545 c
=unicodeCodeUnits
[finalOffset
];
547 /* output BMP code point */
551 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
552 int32_t finalOffset
=offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
);
553 c
=unicodeCodeUnits
[finalOffset
++];
555 /* output BMP code point below 0xd800 */
556 } else if(c
<=0xdbff) {
557 /* output roundtrip or fallback supplementary code point */
558 c
=((c
&0x3ff)<<10)+unicodeCodeUnits
[finalOffset
]+(0x10000-0xdc00);
559 } else if(c
==0xe000) {
560 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
561 c
=unicodeCodeUnits
[finalOffset
];
565 } else if(action
==MBCS_STATE_VALID_DIRECT_20
) {
566 /* output supplementary code point */
567 c
=(UChar32
)(MBCS_ENTRY_FINAL_VALUE(entry
)+0x10000);
572 codePoints
[b
&0x1f]=c
;
575 if(((++b
)&0x1f)==0) {
576 if(anyCodePoints
>=0) {
577 if(!callback(context
, value
|(uint32_t)(b
-0x20), codePoints
)) {
588 * Only called if stateProps[state]==-1.
589 * A recursive call may do stateProps[state]|=0x40 if this state is the target of an
590 * MBCS_STATE_CHANGE_ONLY.
593 getStateProp(const int32_t (*stateTable
)[256], int8_t stateProps
[], int state
) {
595 int32_t min
, max
, entry
, nextState
;
597 row
=stateTable
[state
];
600 /* find first non-ignorable state */
603 nextState
=MBCS_ENTRY_STATE(entry
);
604 if(stateProps
[nextState
]==-1) {
605 getStateProp(stateTable
, stateProps
, nextState
);
607 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
608 if(stateProps
[nextState
]>=0) {
611 } else if(MBCS_ENTRY_FINAL_ACTION(entry
)<MBCS_STATE_UNASSIGNED
) {
615 stateProps
[state
]=-0x40; /* (int8_t)0xc0 */
616 return stateProps
[state
];
619 stateProps
[state
]|=(int8_t)((min
>>5)<<3);
621 /* find last non-ignorable state */
622 for(max
=0xff; min
<max
; --max
) {
624 nextState
=MBCS_ENTRY_STATE(entry
);
625 if(stateProps
[nextState
]==-1) {
626 getStateProp(stateTable
, stateProps
, nextState
);
628 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
629 if(stateProps
[nextState
]>=0) {
632 } else if(MBCS_ENTRY_FINAL_ACTION(entry
)<MBCS_STATE_UNASSIGNED
) {
636 stateProps
[state
]|=(int8_t)(max
>>5);
638 /* recurse further and collect direct-state information */
641 nextState
=MBCS_ENTRY_STATE(entry
);
642 if(stateProps
[nextState
]==-1) {
643 getStateProp(stateTable
, stateProps
, nextState
);
645 if(MBCS_ENTRY_IS_FINAL(entry
)) {
646 stateProps
[nextState
]|=0x40;
647 if(MBCS_ENTRY_FINAL_ACTION(entry
)<=MBCS_STATE_FALLBACK_DIRECT_20
) {
648 stateProps
[state
]|=0x40;
653 return stateProps
[state
];
657 * Internal function enumerating the toUnicode data of an MBCS converter.
658 * Currently only used for reconstituting data for a MBCS_OPT_NO_FROM_U
659 * table, but could also be used for a future ucnv_getUnicodeSet() option
660 * that includes reverse fallbacks (after updating this function's implementation).
661 * Currently only handles roundtrip mappings.
662 * Does not currently handle extensions.
665 ucnv_MBCSEnumToUnicode(UConverterMBCSTable
*mbcsTable
,
666 UConverterEnumToUCallback
*callback
, const void *context
,
667 UErrorCode
*pErrorCode
) {
669 * Properties for each state, to speed up the enumeration.
670 * Ignorable actions are unassigned/illegal/state-change-only:
671 * They do not lead to mappings.
674 * 1 direct/initial state (stateful converters have multiple)
675 * 0 non-initial state with transitions or with non-ignorable result actions
676 * -1 final state with only ignorable actions
679 * The lowest byte value with non-ignorable actions is
680 * value<<5 (rounded down).
683 * The highest byte value with non-ignorable actions is
684 * (value<<5)&0x1f (rounded up).
686 int8_t stateProps
[MBCS_MAX_STATE_COUNT
];
689 uprv_memset(stateProps
, -1, sizeof(stateProps
));
691 /* recurse from state 0 and set all stateProps */
692 getStateProp(mbcsTable
->stateTable
, stateProps
, 0);
694 for(state
=0; state
<mbcsTable
->countStates
; ++state
) {
695 /*if(stateProps[state]==-1) {
696 printf("unused/unreachable <icu:state> %d\n", state);
698 if(stateProps
[state
]>=0x40) {
699 /* start from each direct state */
701 mbcsTable
, stateProps
, state
, 0, 0,
709 ucnv_MBCSGetFilteredUnicodeSetForUnicode(const UConverterSharedData
*sharedData
,
711 UConverterUnicodeSet which
,
712 UConverterSetFilter filter
,
713 UErrorCode
*pErrorCode
) {
714 const UConverterMBCSTable
*mbcsTable
;
715 const uint16_t *table
;
718 uint16_t st1
, maxStage1
, st2
;
722 /* enumerate the from-Unicode trie table */
723 mbcsTable
=&sharedData
->mbcs
;
724 table
=mbcsTable
->fromUnicodeTable
;
725 if(mbcsTable
->unicodeMask
&UCNV_HAS_SUPPLEMENTARY
) {
731 c
=0; /* keep track of the current code point while enumerating */
733 if(mbcsTable
->outputType
==MBCS_OUTPUT_1
) {
734 const uint16_t *stage2
, *stage3
, *results
;
737 results
=(const uint16_t *)mbcsTable
->fromUnicodeBytes
;
740 * Set a threshold variable for selecting which mappings to use.
741 * See ucnv_MBCSSingleFromBMPWithOffsets() and
742 * MBCS_SINGLE_RESULT_FROM_U() for details.
744 if(which
==UCNV_ROUNDTRIP_SET
) {
745 /* use only roundtrips */
747 } else /* UCNV_ROUNDTRIP_AND_FALLBACK_SET */ {
748 /* use all roundtrip and fallback results */
752 for(st1
=0; st1
<maxStage1
; ++st1
) {
756 for(st2
=0; st2
<64; ++st2
) {
757 if((st3
=stage2
[st2
])!=0) {
758 /* read the stage 3 block */
762 if(*stage3
++>=minValue
) {
765 } while((++c
&0xf)!=0);
767 c
+=16; /* empty stage 3 block */
771 c
+=1024; /* empty stage 2 block */
775 const uint32_t *stage2
;
776 const uint8_t *stage3
, *bytes
;
777 uint32_t st3Multiplier
;
781 bytes
=mbcsTable
->fromUnicodeBytes
;
783 useFallback
=(UBool
)(which
==UCNV_ROUNDTRIP_AND_FALLBACK_SET
);
785 switch(mbcsTable
->outputType
) {
787 case MBCS_OUTPUT_4_EUC
:
798 for(st1
=0; st1
<maxStage1
; ++st1
) {
800 if(st2
>(maxStage1
>>1)) {
801 stage2
=(const uint32_t *)table
+st2
;
802 for(st2
=0; st2
<64; ++st2
) {
803 if((st3
=stage2
[st2
])!=0) {
804 /* read the stage 3 block */
805 stage3
=bytes
+st3Multiplier
*16*(uint32_t)(uint16_t)st3
;
807 /* get the roundtrip flags for the stage 3 block */
811 * Add code points for which the roundtrip flag is set,
812 * or which map to non-zero bytes if we use fallbacks.
813 * See ucnv_MBCSFromUnicodeWithOffsets() for details.
816 case UCNV_SET_FILTER_NONE
:
820 stage3
+=st3Multiplier
;
821 } else if(useFallback
) {
823 switch(st3Multiplier
) {
829 b
|=stage3
[0]|stage3
[1];
839 } while((++c
&0xf)!=0);
841 case UCNV_SET_FILTER_DBCS_ONLY
:
842 /* Ignore single-byte results (<0x100). */
844 if(((st3
&1)!=0 || useFallback
) && *((const uint16_t *)stage3
)>=0x100) {
848 stage3
+=2; /* +=st3Multiplier */
849 } while((++c
&0xf)!=0);
851 case UCNV_SET_FILTER_2022_CN
:
852 /* Only add code points that map to CNS 11643 planes 1 & 2 for non-EXT ISO-2022-CN. */
854 if(((st3
&1)!=0 || useFallback
) && ((value
=*stage3
)==0x81 || value
==0x82)) {
858 stage3
+=3; /* +=st3Multiplier */
859 } while((++c
&0xf)!=0);
861 case UCNV_SET_FILTER_SJIS
:
862 /* Only add code points that map to Shift-JIS codes corresponding to JIS X 0208. */
864 if(((st3
&1)!=0 || useFallback
) && (value
=*((const uint16_t *)stage3
))>=0x8140 && value
<=0xeffc) {
868 stage3
+=2; /* +=st3Multiplier */
869 } while((++c
&0xf)!=0);
871 case UCNV_SET_FILTER_GR94DBCS
:
872 /* Only add code points that map to ISO 2022 GR 94 DBCS codes (each byte A1..FE). */
874 if( ((st3
&1)!=0 || useFallback
) &&
875 (uint16_t)((value
=*((const uint16_t *)stage3
)) - 0xa1a1)<=(0xfefe - 0xa1a1) &&
876 (uint8_t)(value
-0xa1)<=(0xfe - 0xa1)
881 stage3
+=2; /* +=st3Multiplier */
882 } while((++c
&0xf)!=0);
884 case UCNV_SET_FILTER_HZ
:
885 /* Only add code points that are suitable for HZ DBCS (lead byte A1..FD). */
887 if( ((st3
&1)!=0 || useFallback
) &&
888 (uint16_t)((value
=*((const uint16_t *)stage3
))-0xa1a1)<=(0xfdfe - 0xa1a1) &&
889 (uint8_t)(value
-0xa1)<=(0xfe - 0xa1)
894 stage3
+=2; /* +=st3Multiplier */
895 } while((++c
&0xf)!=0);
898 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
902 c
+=16; /* empty stage 3 block */
906 c
+=1024; /* empty stage 2 block */
911 ucnv_extGetUnicodeSet(sharedData
, sa
, which
, filter
, pErrorCode
);
915 ucnv_MBCSGetUnicodeSetForUnicode(const UConverterSharedData
*sharedData
,
917 UConverterUnicodeSet which
,
918 UErrorCode
*pErrorCode
) {
919 ucnv_MBCSGetFilteredUnicodeSetForUnicode(
920 sharedData
, sa
, which
,
921 sharedData
->mbcs
.outputType
==MBCS_OUTPUT_DBCS_ONLY
?
922 UCNV_SET_FILTER_DBCS_ONLY
:
923 UCNV_SET_FILTER_NONE
,
928 ucnv_MBCSGetUnicodeSet(const UConverter
*cnv
,
930 UConverterUnicodeSet which
,
931 UErrorCode
*pErrorCode
) {
932 if(cnv
->options
&_MBCS_OPTION_GB18030
) {
933 sa
->addRange(sa
->set
, 0, 0xd7ff);
934 sa
->addRange(sa
->set
, 0xe000, 0x10ffff);
936 ucnv_MBCSGetUnicodeSetForUnicode(cnv
->sharedData
, sa
, which
, pErrorCode
);
940 /* conversion extensions for input not in the main table -------------------- */
943 * Hardcoded extension handling for GB 18030.
944 * Definition of LINEAR macros and gb18030Ranges see near the beginning of the file.
946 * In the future, conversion extensions may handle m:n mappings and delta tables,
947 * see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/conversion/conversion_extensions.html
949 * If an input character cannot be mapped, then these functions set an error
950 * code. The framework will then call the callback function.
954 * @return if(U_FAILURE) return the code point for cnv->fromUChar32
955 * else return 0 after output has been written to the target
958 _extFromU(UConverter
*cnv
, const UConverterSharedData
*sharedData
,
960 const UChar
**source
, const UChar
*sourceLimit
,
961 uint8_t **target
, const uint8_t *targetLimit
,
962 int32_t **offsets
, int32_t sourceIndex
,
964 UErrorCode
*pErrorCode
) {
967 cnv
->useSubChar1
=FALSE
;
969 if( (cx
=sharedData
->mbcs
.extIndexes
)!=NULL
&&
970 ucnv_extInitialMatchFromU(
972 cp
, source
, sourceLimit
,
973 (char **)target
, (char *)targetLimit
,
974 offsets
, sourceIndex
,
978 return 0; /* an extension mapping handled the input */
982 if((cnv
->options
&_MBCS_OPTION_GB18030
)!=0) {
983 const uint32_t *range
;
986 range
=gb18030Ranges
[0];
987 for(i
=0; i
<sizeof(gb18030Ranges
)/sizeof(gb18030Ranges
[0]); range
+=4, ++i
) {
988 if(range
[0]<=(uint32_t)cp
&& (uint32_t)cp
<=range
[1]) {
989 /* found the Unicode code point, output the four-byte sequence for it */
993 /* get the linear value of the first GB 18030 code in this range */
994 linear
=range
[2]-LINEAR_18030_BASE
;
996 /* add the offset from the beginning of the range */
997 linear
+=((uint32_t)cp
-range
[0]);
999 /* turn this into a four-byte sequence */
1000 bytes
[3]=(char)(0x30+linear%10
); linear
/=10;
1001 bytes
[2]=(char)(0x81+linear%126
); linear
/=126;
1002 bytes
[1]=(char)(0x30+linear%10
); linear
/=10;
1003 bytes
[0]=(char)(0x81+linear
);
1005 /* output this sequence */
1006 ucnv_fromUWriteBytes(cnv
,
1007 bytes
, 4, (char **)target
, (char *)targetLimit
,
1008 offsets
, sourceIndex
, pErrorCode
);
1015 *pErrorCode
=U_INVALID_CHAR_FOUND
;
1020 * Input sequence: cnv->toUBytes[0..length[
1021 * @return if(U_FAILURE) return the length (toULength, byteIndex) for the input
1022 * else return 0 after output has been written to the target
1025 _extToU(UConverter
*cnv
, const UConverterSharedData
*sharedData
,
1027 const uint8_t **source
, const uint8_t *sourceLimit
,
1028 UChar
**target
, const UChar
*targetLimit
,
1029 int32_t **offsets
, int32_t sourceIndex
,
1031 UErrorCode
*pErrorCode
) {
1034 if( (cx
=sharedData
->mbcs
.extIndexes
)!=NULL
&&
1035 ucnv_extInitialMatchToU(
1037 length
, (const char **)source
, (const char *)sourceLimit
,
1038 target
, targetLimit
,
1039 offsets
, sourceIndex
,
1043 return 0; /* an extension mapping handled the input */
1047 if(length
==4 && (cnv
->options
&_MBCS_OPTION_GB18030
)!=0) {
1048 const uint32_t *range
;
1052 linear
=LINEAR_18030(cnv
->toUBytes
[0], cnv
->toUBytes
[1], cnv
->toUBytes
[2], cnv
->toUBytes
[3]);
1053 range
=gb18030Ranges
[0];
1054 for(i
=0; i
<sizeof(gb18030Ranges
)/sizeof(gb18030Ranges
[0]); range
+=4, ++i
) {
1055 if(range
[2]<=linear
&& linear
<=range
[3]) {
1056 /* found the sequence, output the Unicode code point for it */
1057 *pErrorCode
=U_ZERO_ERROR
;
1059 /* add the linear difference between the input and start sequences to the start code point */
1060 linear
=range
[0]+(linear
-range
[2]);
1062 /* output this code point */
1063 ucnv_toUWriteCodePoint(cnv
, linear
, target
, targetLimit
, offsets
, sourceIndex
, pErrorCode
);
1071 *pErrorCode
=U_INVALID_CHAR_FOUND
;
1075 /* EBCDIC swap LF<->NL ------------------------------------------------------ */
1078 * This code modifies a standard EBCDIC<->Unicode mapping table for
1079 * OS/390 (z/OS) Unix System Services (Open Edition).
1080 * The difference is in the mapping of Line Feed and New Line control codes:
1081 * Standard EBCDIC maps
1086 * but OS/390 USS EBCDIC swaps the control codes for LF and NL,
1092 * This code modifies a loaded standard EBCDIC<->Unicode mapping table
1093 * by copying it into allocated memory and swapping the LF and NL values.
1094 * It allows to support the same EBCDIC charset in both versions without
1095 * duplicating the entire installed table.
1098 /* standard EBCDIC codes */
1099 #define EBCDIC_LF 0x25
1100 #define EBCDIC_NL 0x15
1102 /* standard EBCDIC codes with roundtrip flag as stored in Unicode-to-single-byte tables */
1103 #define EBCDIC_RT_LF 0xf25
1104 #define EBCDIC_RT_NL 0xf15
1106 /* Unicode code points */
1111 _EBCDICSwapLFNL(UConverterSharedData
*sharedData
, UErrorCode
*pErrorCode
) {
1112 UConverterMBCSTable
*mbcsTable
;
1114 const uint16_t *table
, *results
;
1115 const uint8_t *bytes
;
1117 int32_t (*newStateTable
)[256];
1118 uint16_t *newResults
;
1122 uint32_t stage2Entry
;
1123 uint32_t size
, sizeofFromUBytes
;
1125 mbcsTable
=&sharedData
->mbcs
;
1127 table
=mbcsTable
->fromUnicodeTable
;
1128 bytes
=mbcsTable
->fromUnicodeBytes
;
1129 results
=(const uint16_t *)bytes
;
1132 * Check that this is an EBCDIC table with SBCS portion -
1133 * SBCS or EBCDIC_STATEFUL with standard EBCDIC LF and NL mappings.
1135 * If not, ignore the option. Options are always ignored if they do not apply.
1138 (mbcsTable
->outputType
==MBCS_OUTPUT_1
|| mbcsTable
->outputType
==MBCS_OUTPUT_2_SISO
) &&
1139 mbcsTable
->stateTable
[0][EBCDIC_LF
]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_LF
) &&
1140 mbcsTable
->stateTable
[0][EBCDIC_NL
]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_NL
)
1145 if(mbcsTable
->outputType
==MBCS_OUTPUT_1
) {
1147 EBCDIC_RT_LF
==MBCS_SINGLE_RESULT_FROM_U(table
, results
, U_LF
) &&
1148 EBCDIC_RT_NL
==MBCS_SINGLE_RESULT_FROM_U(table
, results
, U_NL
)
1152 } else /* MBCS_OUTPUT_2_SISO */ {
1153 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_LF
);
1155 MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, U_LF
)!=0 &&
1156 EBCDIC_LF
==MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, U_LF
)
1161 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_NL
);
1163 MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, U_NL
)!=0 &&
1164 EBCDIC_NL
==MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, U_NL
)
1170 if(mbcsTable
->fromUBytesLength
>0) {
1172 * We _know_ the number of bytes in the fromUnicodeBytes array
1173 * starting with header.version 4.1.
1175 sizeofFromUBytes
=mbcsTable
->fromUBytesLength
;
1179 * There used to be code to enumerate the fromUnicode
1180 * trie and find the highest entry, but it was removed in ICU 3.2
1181 * because it was not tested and caused a low code coverage number.
1182 * See Jitterbug 3674.
1183 * This affects only some .cnv file formats with a header.version
1184 * below 4.1, and only when swaplfnl is requested.
1186 * ucnvmbcs.c revision 1.99 is the last one with the
1187 * ucnv_MBCSSizeofFromUBytes() function.
1189 *pErrorCode
=U_INVALID_FORMAT_ERROR
;
1194 * The table has an appropriate format.
1195 * Allocate and build
1196 * - a modified to-Unicode state table
1197 * - a modified from-Unicode output array
1198 * - a converter name string with the swap option appended
1201 mbcsTable
->countStates
*1024+
1203 UCNV_MAX_CONVERTER_NAME_LENGTH
+20;
1204 p
=(uint8_t *)uprv_malloc(size
);
1206 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1210 /* copy and modify the to-Unicode state table */
1211 newStateTable
=(int32_t (*)[256])p
;
1212 uprv_memcpy(newStateTable
, mbcsTable
->stateTable
, mbcsTable
->countStates
*1024);
1214 newStateTable
[0][EBCDIC_LF
]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_NL
);
1215 newStateTable
[0][EBCDIC_NL
]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_LF
);
1217 /* copy and modify the from-Unicode result table */
1218 newResults
=(uint16_t *)newStateTable
[mbcsTable
->countStates
];
1219 uprv_memcpy(newResults
, bytes
, sizeofFromUBytes
);
1221 /* conveniently, the table access macros work on the left side of expressions */
1222 if(mbcsTable
->outputType
==MBCS_OUTPUT_1
) {
1223 MBCS_SINGLE_RESULT_FROM_U(table
, newResults
, U_LF
)=EBCDIC_RT_NL
;
1224 MBCS_SINGLE_RESULT_FROM_U(table
, newResults
, U_NL
)=EBCDIC_RT_LF
;
1225 } else /* MBCS_OUTPUT_2_SISO */ {
1226 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_LF
);
1227 MBCS_VALUE_2_FROM_STAGE_2(newResults
, stage2Entry
, U_LF
)=EBCDIC_NL
;
1229 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_NL
);
1230 MBCS_VALUE_2_FROM_STAGE_2(newResults
, stage2Entry
, U_NL
)=EBCDIC_LF
;
1233 /* set the canonical converter name */
1234 name
=(char *)newResults
+sizeofFromUBytes
;
1235 uprv_strcpy(name
, sharedData
->staticData
->name
);
1236 uprv_strcat(name
, UCNV_SWAP_LFNL_OPTION_STRING
);
1238 /* set the pointers */
1240 if(mbcsTable
->swapLFNLStateTable
==NULL
) {
1241 mbcsTable
->swapLFNLStateTable
=newStateTable
;
1242 mbcsTable
->swapLFNLFromUnicodeBytes
=(uint8_t *)newResults
;
1243 mbcsTable
->swapLFNLName
=name
;
1249 /* release the allocated memory if another thread beat us to it */
1250 if(newStateTable
!=NULL
) {
1251 uprv_free(newStateTable
);
1256 /* reconstitute omitted fromUnicode data ------------------------------------ */
1258 /* for details, compare with genmbcs.c MBCSAddFromUnicode() and transformEUC() */
1259 static UBool U_CALLCONV
1260 writeStage3Roundtrip(const void *context
, uint32_t value
, UChar32 codePoints
[32]) {
1261 UConverterMBCSTable
*mbcsTable
=(UConverterMBCSTable
*)context
;
1262 const uint16_t *table
;
1268 table
=mbcsTable
->fromUnicodeTable
;
1269 bytes
=(uint8_t *)mbcsTable
->fromUnicodeBytes
;
1271 /* for EUC outputTypes, modify the value like genmbcs.c's transformEUC() */
1272 switch(mbcsTable
->outputType
) {
1273 case MBCS_OUTPUT_3_EUC
:
1275 /* short sequences are stored directly */
1276 /* code set 0 or 1 */
1277 } else if(value
<=0x8effff) {
1280 } else /* first byte is 0x8f */ {
1285 case MBCS_OUTPUT_4_EUC
:
1286 if(value
<=0xffffff) {
1287 /* short sequences are stored directly */
1288 /* code set 0 or 1 */
1289 } else if(value
<=0x8effffff) {
1292 } else /* first byte is 0x8f */ {
1301 for(i
=0; i
<=0x1f; ++value
, ++i
) {
1307 /* locate the stage 2 & 3 data */
1308 stage2
=((uint32_t *)table
)+table
[c
>>10]+((c
>>4)&0x3f);
1310 st3
=(int32_t)(uint16_t)*stage2
*16+(c
&0xf);
1312 /* write the codepage bytes into stage 3 */
1313 switch(mbcsTable
->outputType
) {
1315 case MBCS_OUTPUT_4_EUC
:
1317 p
[0]=(uint8_t)(value
>>16);
1318 p
[1]=(uint8_t)(value
>>8);
1319 p
[2]=(uint8_t)value
;
1322 ((uint32_t *)p
)[st3
]=value
;
1325 /* 2 bytes per character */
1326 ((uint16_t *)p
)[st3
]=(uint16_t)value
;
1330 /* set the roundtrip flag */
1331 *stage2
|=(1UL<<(16+(c
&0xf)));
1337 reconstituteData(UConverterMBCSTable
*mbcsTable
,
1338 uint32_t stage1Length
, uint32_t stage2Length
,
1339 uint32_t fullStage2Length
, /* lengths are numbers of units, not bytes */
1340 UErrorCode
*pErrorCode
) {
1344 uint32_t dataLength
=stage1Length
*2+fullStage2Length
*4+mbcsTable
->fromUBytesLength
;
1345 mbcsTable
->reconstitutedData
=(uint8_t *)uprv_malloc(dataLength
);
1346 if(mbcsTable
->reconstitutedData
==NULL
) {
1347 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1350 uprv_memset(mbcsTable
->reconstitutedData
, 0, dataLength
);
1352 /* copy existing data and reroute the pointers */
1353 stage1
=(uint16_t *)mbcsTable
->reconstitutedData
;
1354 uprv_memcpy(stage1
, mbcsTable
->fromUnicodeTable
, stage1Length
*2);
1356 stage2
=(uint32_t *)(stage1
+stage1Length
);
1357 uprv_memcpy(stage2
+(fullStage2Length
-stage2Length
),
1358 mbcsTable
->fromUnicodeTable
+stage1Length
,
1361 mbcsTable
->fromUnicodeTable
=stage1
;
1362 mbcsTable
->fromUnicodeBytes
=bytes
=(uint8_t *)(stage2
+fullStage2Length
);
1364 /* indexes into stage 2 count from the bottom of the fromUnicodeTable */
1365 stage2
=(uint32_t *)stage1
;
1367 /* reconstitute the initial part of stage 2 from the mbcsIndex */
1369 int32_t stageUTF8Length
=((int32_t)mbcsTable
->maxFastUChar
+1)>>6;
1370 int32_t stageUTF8Index
=0;
1371 int32_t st1
, st2
, st3
, i
;
1373 for(st1
=0; stageUTF8Index
<stageUTF8Length
; ++st1
) {
1375 if(st2
!=stage1Length
/2) {
1376 /* each stage 2 block has 64 entries corresponding to 16 entries in the mbcsIndex */
1377 for(i
=0; i
<16; ++i
) {
1378 st3
=mbcsTable
->mbcsIndex
[stageUTF8Index
++];
1380 /* an stage 2 entry's index is per stage 3 16-block, not per stage 3 entry */
1383 * 4 stage 2 entries point to 4 consecutive stage 3 16-blocks which are
1384 * allocated together as a single 64-block for access from the mbcsIndex
1386 stage2
[st2
++]=st3
++;
1387 stage2
[st2
++]=st3
++;
1388 stage2
[st2
++]=st3
++;
1391 /* no stage 3 block, skip */
1396 /* no stage 2 block, skip */
1402 /* reconstitute fromUnicodeBytes with roundtrips from toUnicode data */
1403 ucnv_MBCSEnumToUnicode(mbcsTable
, writeStage3Roundtrip
, mbcsTable
, pErrorCode
);
1406 /* MBCS setup functions ----------------------------------------------------- */
1409 ucnv_MBCSLoad(UConverterSharedData
*sharedData
,
1410 UConverterLoadArgs
*pArgs
,
1412 UErrorCode
*pErrorCode
) {
1414 UConverterMBCSTable
*mbcsTable
=&sharedData
->mbcs
;
1415 _MBCSHeader
*header
=(_MBCSHeader
*)raw
;
1417 uint32_t headerLength
;
1418 UBool noFromU
=FALSE
;
1420 if(header
->version
[0]==4) {
1421 headerLength
=MBCS_HEADER_V4_LENGTH
;
1422 } else if(header
->version
[0]==5 && header
->version
[1]>=3 &&
1423 (header
->options
&MBCS_OPT_UNKNOWN_INCOMPATIBLE_MASK
)==0) {
1424 headerLength
=header
->options
&MBCS_OPT_LENGTH_MASK
;
1425 noFromU
=(UBool
)((header
->options
&MBCS_OPT_NO_FROM_U
)!=0);
1427 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1431 mbcsTable
->outputType
=(uint8_t)header
->flags
;
1432 if(noFromU
&& mbcsTable
->outputType
==MBCS_OUTPUT_1
) {
1433 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1437 /* extension data, header version 4.2 and higher */
1438 offset
=header
->flags
>>8;
1440 mbcsTable
->extIndexes
=(const int32_t *)(raw
+offset
);
1443 if(mbcsTable
->outputType
==MBCS_OUTPUT_EXT_ONLY
) {
1444 UConverterLoadArgs args
={ 0 };
1445 UConverterSharedData
*baseSharedData
;
1446 const int32_t *extIndexes
;
1447 const char *baseName
;
1449 /* extension-only file, load the base table and set values appropriately */
1450 if((extIndexes
=mbcsTable
->extIndexes
)==NULL
) {
1451 /* extension-only file without extension */
1452 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1456 if(pArgs
->nestedLoads
!=1) {
1457 /* an extension table must not be loaded as a base table */
1458 *pErrorCode
=U_INVALID_TABLE_FILE
;
1462 /* load the base table */
1463 baseName
=(const char *)header
+headerLength
*4;
1464 if(0==uprv_strcmp(baseName
, sharedData
->staticData
->name
)) {
1465 /* forbid loading this same extension-only file */
1466 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1470 /* TODO parse package name out of the prefix of the base name in the extension .cnv file? */
1471 args
.size
=sizeof(UConverterLoadArgs
);
1473 args
.onlyTestIsLoadable
=pArgs
->onlyTestIsLoadable
;
1474 args
.reserved
=pArgs
->reserved
;
1475 args
.options
=pArgs
->options
;
1476 args
.pkg
=pArgs
->pkg
;
1478 baseSharedData
=ucnv_load(&args
, pErrorCode
);
1479 if(U_FAILURE(*pErrorCode
)) {
1482 if( baseSharedData
->staticData
->conversionType
!=UCNV_MBCS
||
1483 baseSharedData
->mbcs
.baseSharedData
!=NULL
1485 ucnv_unload(baseSharedData
);
1486 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1489 if(pArgs
->onlyTestIsLoadable
) {
1491 * Exit as soon as we know that we can load the converter
1492 * and the format is valid and supported.
1493 * The worst that can happen in the following code is a memory
1496 ucnv_unload(baseSharedData
);
1500 /* copy the base table data */
1501 uprv_memcpy(mbcsTable
, &baseSharedData
->mbcs
, sizeof(UConverterMBCSTable
));
1503 /* overwrite values with relevant ones for the extension converter */
1504 mbcsTable
->baseSharedData
=baseSharedData
;
1505 mbcsTable
->extIndexes
=extIndexes
;
1508 * It would be possible to share the swapLFNL data with a base converter,
1509 * but the generated name would have to be different, and the memory
1510 * would have to be free'd only once.
1511 * It is easier to just create the data for the extension converter
1512 * separately when it is requested.
1514 mbcsTable
->swapLFNLStateTable
=NULL
;
1515 mbcsTable
->swapLFNLFromUnicodeBytes
=NULL
;
1516 mbcsTable
->swapLFNLName
=NULL
;
1519 * The reconstitutedData must be deleted only when the base converter
1522 mbcsTable
->reconstitutedData
=NULL
;
1525 * Set a special, runtime-only outputType if the extension converter
1526 * is a DBCS version of a base converter that also maps single bytes.
1528 if( sharedData
->staticData
->conversionType
==UCNV_DBCS
||
1529 (sharedData
->staticData
->conversionType
==UCNV_MBCS
&&
1530 sharedData
->staticData
->minBytesPerChar
>=2)
1532 if(baseSharedData
->mbcs
.outputType
==MBCS_OUTPUT_2_SISO
) {
1533 /* the base converter is SI/SO-stateful */
1536 /* get the dbcs state from the state table entry for SO=0x0e */
1537 entry
=mbcsTable
->stateTable
[0][0xe];
1538 if( MBCS_ENTRY_IS_FINAL(entry
) &&
1539 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_CHANGE_ONLY
&&
1540 MBCS_ENTRY_FINAL_STATE(entry
)!=0
1542 mbcsTable
->dbcsOnlyState
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
);
1544 mbcsTable
->outputType
=MBCS_OUTPUT_DBCS_ONLY
;
1547 baseSharedData
->staticData
->conversionType
==UCNV_MBCS
&&
1548 baseSharedData
->staticData
->minBytesPerChar
==1 &&
1549 baseSharedData
->staticData
->maxBytesPerChar
==2 &&
1550 mbcsTable
->countStates
<=127
1552 /* non-stateful base converter, need to modify the state table */
1553 int32_t (*newStateTable
)[256];
1557 /* allocate a new state table and copy the base state table contents */
1558 count
=mbcsTable
->countStates
;
1559 newStateTable
=(int32_t (*)[256])uprv_malloc((count
+1)*1024);
1560 if(newStateTable
==NULL
) {
1561 ucnv_unload(baseSharedData
);
1562 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1566 uprv_memcpy(newStateTable
, mbcsTable
->stateTable
, count
*1024);
1568 /* change all final single-byte entries to go to a new all-illegal state */
1569 state
=newStateTable
[0];
1570 for(i
=0; i
<256; ++i
) {
1571 if(MBCS_ENTRY_IS_FINAL(state
[i
])) {
1572 state
[i
]=MBCS_ENTRY_TRANSITION(count
, 0);
1576 /* build the new all-illegal state */
1577 state
=newStateTable
[count
];
1578 for(i
=0; i
<256; ++i
) {
1579 state
[i
]=MBCS_ENTRY_FINAL(0, MBCS_STATE_ILLEGAL
, 0);
1581 mbcsTable
->stateTable
=(const int32_t (*)[256])newStateTable
;
1582 mbcsTable
->countStates
=(uint8_t)(count
+1);
1583 mbcsTable
->stateTableOwned
=TRUE
;
1585 mbcsTable
->outputType
=MBCS_OUTPUT_DBCS_ONLY
;
1590 * unlike below for files with base tables, do not get the unicodeMask
1591 * from the sharedData; instead, use the base table's unicodeMask,
1592 * which we copied in the memcpy above;
1593 * this is necessary because the static data unicodeMask, especially
1594 * the UCNV_HAS_SUPPLEMENTARY flag, is part of the base table data
1597 /* conversion file with a base table; an additional extension table is optional */
1598 /* make sure that the output type is known */
1599 switch(mbcsTable
->outputType
) {
1604 case MBCS_OUTPUT_3_EUC
:
1605 case MBCS_OUTPUT_4_EUC
:
1606 case MBCS_OUTPUT_2_SISO
:
1610 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1613 if(pArgs
->onlyTestIsLoadable
) {
1615 * Exit as soon as we know that we can load the converter
1616 * and the format is valid and supported.
1617 * The worst that can happen in the following code is a memory
1623 mbcsTable
->countStates
=(uint8_t)header
->countStates
;
1624 mbcsTable
->countToUFallbacks
=header
->countToUFallbacks
;
1625 mbcsTable
->stateTable
=(const int32_t (*)[256])(raw
+headerLength
*4);
1626 mbcsTable
->toUFallbacks
=(const _MBCSToUFallback
*)(mbcsTable
->stateTable
+header
->countStates
);
1627 mbcsTable
->unicodeCodeUnits
=(const uint16_t *)(raw
+header
->offsetToUCodeUnits
);
1629 mbcsTable
->fromUnicodeTable
=(const uint16_t *)(raw
+header
->offsetFromUTable
);
1630 mbcsTable
->fromUnicodeBytes
=(const uint8_t *)(raw
+header
->offsetFromUBytes
);
1631 mbcsTable
->fromUBytesLength
=header
->fromUBytesLength
;
1634 * converter versions 6.1 and up contain a unicodeMask that is
1635 * used here to select the most efficient function implementations
1637 info
.size
=sizeof(UDataInfo
);
1638 udata_getInfo((UDataMemory
*)sharedData
->dataMemory
, &info
);
1639 if(info
.formatVersion
[0]>6 || (info
.formatVersion
[0]==6 && info
.formatVersion
[1]>=1)) {
1640 /* mask off possible future extensions to be safe */
1641 mbcsTable
->unicodeMask
=(uint8_t)(sharedData
->staticData
->unicodeMask
&3);
1643 /* for older versions, assume worst case: contains anything possible (prevent over-optimizations) */
1644 mbcsTable
->unicodeMask
=UCNV_HAS_SUPPLEMENTARY
|UCNV_HAS_SURROGATES
;
1648 * _MBCSHeader.version 4.3 adds utf8Friendly data structures.
1649 * Check for the header version, SBCS vs. MBCS, and for whether the
1650 * data structures are optimized for code points as high as what the
1651 * runtime code is designed for.
1652 * The implementation does not handle mapping tables with entries for
1653 * unpaired surrogates.
1655 if( header
->version
[1]>=3 &&
1656 (mbcsTable
->unicodeMask
&UCNV_HAS_SURROGATES
)==0 &&
1657 (mbcsTable
->countStates
==1 ?
1658 (header
->version
[2]>=(SBCS_FAST_MAX
>>8)) :
1659 (header
->version
[2]>=(MBCS_FAST_MAX
>>8))
1662 mbcsTable
->utf8Friendly
=TRUE
;
1664 if(mbcsTable
->countStates
==1) {
1666 * SBCS: Stage 3 is allocated in 64-entry blocks for U+0000..SBCS_FAST_MAX or higher.
1667 * Build a table with indexes to each block, to be used instead of
1668 * the regular stage 1/2 table.
1671 for(i
=0; i
<(SBCS_FAST_LIMIT
>>6); ++i
) {
1672 mbcsTable
->sbcsIndex
[i
]=mbcsTable
->fromUnicodeTable
[mbcsTable
->fromUnicodeTable
[i
>>4]+((i
<<2)&0x3c)];
1674 /* set SBCS_FAST_MAX to reflect the reach of sbcsIndex[] even if header->version[2]>(SBCS_FAST_MAX>>8) */
1675 mbcsTable
->maxFastUChar
=SBCS_FAST_MAX
;
1678 * MBCS: Stage 3 is allocated in 64-entry blocks for U+0000..MBCS_FAST_MAX or higher.
1679 * The .cnv file is prebuilt with an additional stage table with indexes
1682 mbcsTable
->mbcsIndex
=(const uint16_t *)
1683 (mbcsTable
->fromUnicodeBytes
+
1684 (noFromU
? 0 : mbcsTable
->fromUBytesLength
));
1685 mbcsTable
->maxFastUChar
=(((UChar
)header
->version
[2])<<8)|0xff;
1689 /* calculate a bit set of 4 ASCII characters per bit that round-trip to ASCII bytes */
1691 uint32_t asciiRoundtrips
=0xffffffff;
1694 for(i
=0; i
<0x80; ++i
) {
1695 if(mbcsTable
->stateTable
[0][i
]!=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, i
)) {
1696 asciiRoundtrips
&=~((uint32_t)1<<(i
>>2));
1699 mbcsTable
->asciiRoundtrips
=asciiRoundtrips
;
1703 uint32_t stage1Length
=
1704 mbcsTable
->unicodeMask
&UCNV_HAS_SUPPLEMENTARY
?
1706 uint32_t stage2Length
=
1707 (header
->offsetFromUBytes
-header
->offsetFromUTable
)/4-
1709 reconstituteData(mbcsTable
, stage1Length
, stage2Length
, header
->fullStage2Length
, pErrorCode
);
1713 /* Set the impl pointer here so that it is set for both extension-only and base tables. */
1714 if(mbcsTable
->utf8Friendly
) {
1715 if(mbcsTable
->countStates
==1) {
1716 sharedData
->impl
=&_SBCSUTF8Impl
;
1718 if(mbcsTable
->outputType
==MBCS_OUTPUT_2
) {
1719 sharedData
->impl
=&_DBCSUTF8Impl
;
1724 if(mbcsTable
->outputType
==MBCS_OUTPUT_DBCS_ONLY
|| mbcsTable
->outputType
==MBCS_OUTPUT_2_SISO
) {
1726 * MBCS_OUTPUT_DBCS_ONLY: No SBCS mappings, therefore ASCII does not roundtrip.
1727 * MBCS_OUTPUT_2_SISO: Bypass the ASCII fastpath to handle prevLength correctly.
1729 mbcsTable
->asciiRoundtrips
=0;
1734 ucnv_MBCSUnload(UConverterSharedData
*sharedData
) {
1735 UConverterMBCSTable
*mbcsTable
=&sharedData
->mbcs
;
1737 if(mbcsTable
->swapLFNLStateTable
!=NULL
) {
1738 uprv_free(mbcsTable
->swapLFNLStateTable
);
1740 if(mbcsTable
->stateTableOwned
) {
1741 uprv_free((void *)mbcsTable
->stateTable
);
1743 if(mbcsTable
->baseSharedData
!=NULL
) {
1744 ucnv_unload(mbcsTable
->baseSharedData
);
1746 if(mbcsTable
->reconstitutedData
!=NULL
) {
1747 uprv_free(mbcsTable
->reconstitutedData
);
1752 ucnv_MBCSOpen(UConverter
*cnv
,
1753 UConverterLoadArgs
*pArgs
,
1754 UErrorCode
*pErrorCode
) {
1755 UConverterMBCSTable
*mbcsTable
;
1756 const int32_t *extIndexes
;
1758 int8_t maxBytesPerUChar
;
1760 if(pArgs
->onlyTestIsLoadable
) {
1764 mbcsTable
=&cnv
->sharedData
->mbcs
;
1765 outputType
=mbcsTable
->outputType
;
1767 if(outputType
==MBCS_OUTPUT_DBCS_ONLY
) {
1768 /* the swaplfnl option does not apply, remove it */
1769 cnv
->options
=pArgs
->options
&=~UCNV_OPTION_SWAP_LFNL
;
1772 if((pArgs
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
1773 /* do this because double-checked locking is broken */
1777 isCached
=mbcsTable
->swapLFNLStateTable
!=NULL
;
1781 if(!_EBCDICSwapLFNL(cnv
->sharedData
, pErrorCode
)) {
1782 if(U_FAILURE(*pErrorCode
)) {
1783 return; /* something went wrong */
1786 /* the option does not apply, remove it */
1787 cnv
->options
=pArgs
->options
&=~UCNV_OPTION_SWAP_LFNL
;
1792 if(uprv_strstr(pArgs
->name
, "18030")!=NULL
) {
1793 if(uprv_strstr(pArgs
->name
, "gb18030")!=NULL
|| uprv_strstr(pArgs
->name
, "GB18030")!=NULL
) {
1794 /* set a flag for GB 18030 mode, which changes the callback behavior */
1795 cnv
->options
|=_MBCS_OPTION_GB18030
;
1797 } else if((uprv_strstr(pArgs
->name
, "KEIS")!=NULL
) || (uprv_strstr(pArgs
->name
, "keis")!=NULL
)) {
1798 /* set a flag for KEIS converter, which changes the SI/SO character sequence */
1799 cnv
->options
|=_MBCS_OPTION_KEIS
;
1800 } else if((uprv_strstr(pArgs
->name
, "JEF")!=NULL
) || (uprv_strstr(pArgs
->name
, "jef")!=NULL
)) {
1801 /* set a flag for JEF converter, which changes the SI/SO character sequence */
1802 cnv
->options
|=_MBCS_OPTION_JEF
;
1803 } else if((uprv_strstr(pArgs
->name
, "JIPS")!=NULL
) || (uprv_strstr(pArgs
->name
, "jips")!=NULL
)) {
1804 /* set a flag for JIPS converter, which changes the SI/SO character sequence */
1805 cnv
->options
|=_MBCS_OPTION_JIPS
;
1808 /* fix maxBytesPerUChar depending on outputType and options etc. */
1809 if(outputType
==MBCS_OUTPUT_2_SISO
) {
1810 cnv
->maxBytesPerUChar
=3; /* SO+DBCS */
1813 extIndexes
=mbcsTable
->extIndexes
;
1814 if(extIndexes
!=NULL
) {
1815 maxBytesPerUChar
=(int8_t)UCNV_GET_MAX_BYTES_PER_UCHAR(extIndexes
);
1816 if(outputType
==MBCS_OUTPUT_2_SISO
) {
1817 ++maxBytesPerUChar
; /* SO + multiple DBCS */
1820 if(maxBytesPerUChar
>cnv
->maxBytesPerUChar
) {
1821 cnv
->maxBytesPerUChar
=maxBytesPerUChar
;
1827 * documentation of UConverter fields used for status
1828 * all of these fields are (re)set to 0 by ucnv_bld.c and ucnv_reset()
1832 cnv
->toUnicodeStatus
=0; /* offset */
1833 cnv
->mode
=0; /* state */
1834 cnv
->toULength
=0; /* byteIndex */
1838 cnv
->fromUnicodeStatus
=1; /* prevLength */
1843 ucnv_MBCSGetName(const UConverter
*cnv
) {
1844 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0 && cnv
->sharedData
->mbcs
.swapLFNLName
!=NULL
) {
1845 return cnv
->sharedData
->mbcs
.swapLFNLName
;
1847 return cnv
->sharedData
->staticData
->name
;
1851 /* MBCS-to-Unicode conversion functions ------------------------------------- */
1854 ucnv_MBCSGetFallback(UConverterMBCSTable
*mbcsTable
, uint32_t offset
) {
1855 const _MBCSToUFallback
*toUFallbacks
;
1856 uint32_t i
, start
, limit
;
1858 limit
=mbcsTable
->countToUFallbacks
;
1860 /* do a binary search for the fallback mapping */
1861 toUFallbacks
=mbcsTable
->toUFallbacks
;
1863 while(start
<limit
-1) {
1865 if(offset
<toUFallbacks
[i
].offset
) {
1872 /* did we really find it? */
1873 if(offset
==toUFallbacks
[start
].offset
) {
1874 return toUFallbacks
[start
].codePoint
;
1881 /* This version of ucnv_MBCSToUnicodeWithOffsets() is optimized for single-byte, single-state codepages. */
1883 ucnv_MBCSSingleToUnicodeWithOffsets(UConverterToUnicodeArgs
*pArgs
,
1884 UErrorCode
*pErrorCode
) {
1886 const uint8_t *source
, *sourceLimit
;
1888 const UChar
*targetLimit
;
1891 const int32_t (*stateTable
)[256];
1893 int32_t sourceIndex
;
1899 /* set up the local pointers */
1900 cnv
=pArgs
->converter
;
1901 source
=(const uint8_t *)pArgs
->source
;
1902 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
1903 target
=pArgs
->target
;
1904 targetLimit
=pArgs
->targetLimit
;
1905 offsets
=pArgs
->offsets
;
1907 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
1908 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
1910 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
1913 /* sourceIndex=-1 if the current character began in the previous buffer */
1916 /* conversion loop */
1917 while(source
<sourceLimit
) {
1919 * This following test is to see if available input would overflow the output.
1920 * It does not catch output of more than one code unit that
1921 * overflows as a result of a surrogate pair or callback output
1922 * from the last source byte.
1923 * Therefore, those situations also test for overflows and will
1924 * then break the loop, too.
1926 if(target
>=targetLimit
) {
1927 /* target is full */
1928 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1932 entry
=stateTable
[0][*source
++];
1933 /* MBCS_ENTRY_IS_FINAL(entry) */
1935 /* test the most common case first */
1936 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
1937 /* output BMP code point */
1938 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1940 *offsets
++=sourceIndex
;
1943 /* normal end of action codes: prepare for a new character */
1949 * An if-else-if chain provides more reliable performance for
1950 * the most common cases compared to a switch.
1952 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
1953 if(action
==MBCS_STATE_VALID_DIRECT_20
||
1954 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
1956 entry
=MBCS_ENTRY_FINAL_VALUE(entry
);
1957 /* output surrogate pair */
1958 *target
++=(UChar
)(0xd800|(UChar
)(entry
>>10));
1960 *offsets
++=sourceIndex
;
1962 c
=(UChar
)(0xdc00|(UChar
)(entry
&0x3ff));
1963 if(target
<targetLimit
) {
1966 *offsets
++=sourceIndex
;
1969 /* target overflow */
1970 cnv
->UCharErrorBuffer
[0]=c
;
1971 cnv
->UCharErrorBufferLength
=1;
1972 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1978 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
1979 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
1980 /* output BMP code point */
1981 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1983 *offsets
++=sourceIndex
;
1989 } else if(action
==MBCS_STATE_UNASSIGNED
) {
1990 /* just fall through */
1991 } else if(action
==MBCS_STATE_ILLEGAL
) {
1992 /* callback(illegal) */
1993 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
1995 /* reserved, must never occur */
2000 if(U_FAILURE(*pErrorCode
)) {
2001 /* callback(illegal) */
2003 } else /* unassigned sequences indicated with byteIndex>0 */ {
2004 /* try an extension mapping */
2005 pArgs
->source
=(const char *)source
;
2006 cnv
->toUBytes
[0]=*(source
-1);
2007 cnv
->toULength
=_extToU(cnv
, cnv
->sharedData
,
2008 1, &source
, sourceLimit
,
2009 &target
, targetLimit
,
2010 &offsets
, sourceIndex
,
2013 sourceIndex
+=1+(int32_t)(source
-(const uint8_t *)pArgs
->source
);
2015 if(U_FAILURE(*pErrorCode
)) {
2016 /* not mappable or buffer overflow */
2022 /* write back the updated pointers */
2023 pArgs
->source
=(const char *)source
;
2024 pArgs
->target
=target
;
2025 pArgs
->offsets
=offsets
;
2029 * This version of ucnv_MBCSSingleToUnicodeWithOffsets() is optimized for single-byte, single-state codepages
2030 * that only map to and from the BMP.
2031 * In addition to single-byte optimizations, the offset calculations
2032 * become much easier.
2035 ucnv_MBCSSingleToBMPWithOffsets(UConverterToUnicodeArgs
*pArgs
,
2036 UErrorCode
*pErrorCode
) {
2038 const uint8_t *source
, *sourceLimit
, *lastSource
;
2040 int32_t targetCapacity
, length
;
2043 const int32_t (*stateTable
)[256];
2045 int32_t sourceIndex
;
2050 /* set up the local pointers */
2051 cnv
=pArgs
->converter
;
2052 source
=(const uint8_t *)pArgs
->source
;
2053 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
2054 target
=pArgs
->target
;
2055 targetCapacity
=(int32_t)(pArgs
->targetLimit
-pArgs
->target
);
2056 offsets
=pArgs
->offsets
;
2058 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2059 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
2061 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
2064 /* sourceIndex=-1 if the current character began in the previous buffer */
2069 * since the conversion here is 1:1 UChar:uint8_t, we need only one counter
2070 * for the minimum of the sourceLength and targetCapacity
2072 length
=(int32_t)(sourceLimit
-source
);
2073 if(length
<targetCapacity
) {
2074 targetCapacity
=length
;
2077 #if MBCS_UNROLL_SINGLE_TO_BMP
2078 /* unrolling makes it faster on Pentium III/Windows 2000 */
2079 /* unroll the loop with the most common case */
2081 if(targetCapacity
>=16) {
2082 int32_t count
, loops
, oredEntries
;
2084 loops
=count
=targetCapacity
>>4;
2086 oredEntries
=entry
=stateTable
[0][*source
++];
2087 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2088 oredEntries
|=entry
=stateTable
[0][*source
++];
2089 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2090 oredEntries
|=entry
=stateTable
[0][*source
++];
2091 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2092 oredEntries
|=entry
=stateTable
[0][*source
++];
2093 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2094 oredEntries
|=entry
=stateTable
[0][*source
++];
2095 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2096 oredEntries
|=entry
=stateTable
[0][*source
++];
2097 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2098 oredEntries
|=entry
=stateTable
[0][*source
++];
2099 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2100 oredEntries
|=entry
=stateTable
[0][*source
++];
2101 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2102 oredEntries
|=entry
=stateTable
[0][*source
++];
2103 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2104 oredEntries
|=entry
=stateTable
[0][*source
++];
2105 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2106 oredEntries
|=entry
=stateTable
[0][*source
++];
2107 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2108 oredEntries
|=entry
=stateTable
[0][*source
++];
2109 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2110 oredEntries
|=entry
=stateTable
[0][*source
++];
2111 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2112 oredEntries
|=entry
=stateTable
[0][*source
++];
2113 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2114 oredEntries
|=entry
=stateTable
[0][*source
++];
2115 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2116 oredEntries
|=entry
=stateTable
[0][*source
++];
2117 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2119 /* were all 16 entries really valid? */
2120 if(!MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(oredEntries
)) {
2121 /* no, return to the first of these 16 */
2128 targetCapacity
-=16*count
;
2131 lastSource
+=16*count
;
2133 *offsets
++=sourceIndex
++;
2134 *offsets
++=sourceIndex
++;
2135 *offsets
++=sourceIndex
++;
2136 *offsets
++=sourceIndex
++;
2137 *offsets
++=sourceIndex
++;
2138 *offsets
++=sourceIndex
++;
2139 *offsets
++=sourceIndex
++;
2140 *offsets
++=sourceIndex
++;
2141 *offsets
++=sourceIndex
++;
2142 *offsets
++=sourceIndex
++;
2143 *offsets
++=sourceIndex
++;
2144 *offsets
++=sourceIndex
++;
2145 *offsets
++=sourceIndex
++;
2146 *offsets
++=sourceIndex
++;
2147 *offsets
++=sourceIndex
++;
2148 *offsets
++=sourceIndex
++;
2155 /* conversion loop */
2156 while(targetCapacity
> 0 && source
< sourceLimit
) {
2157 entry
=stateTable
[0][*source
++];
2158 /* MBCS_ENTRY_IS_FINAL(entry) */
2160 /* test the most common case first */
2161 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
2162 /* output BMP code point */
2163 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2169 * An if-else-if chain provides more reliable performance for
2170 * the most common cases compared to a switch.
2172 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2173 if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2174 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
2175 /* output BMP code point */
2176 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2180 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2181 /* just fall through */
2182 } else if(action
==MBCS_STATE_ILLEGAL
) {
2183 /* callback(illegal) */
2184 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2186 /* reserved, must never occur */
2190 /* set offsets since the start or the last extension */
2192 int32_t count
=(int32_t)(source
-lastSource
);
2194 /* predecrement: do not set the offset for the callback-causing character */
2196 *offsets
++=sourceIndex
++;
2198 /* offset and sourceIndex are now set for the current character */
2201 if(U_FAILURE(*pErrorCode
)) {
2202 /* callback(illegal) */
2204 } else /* unassigned sequences indicated with byteIndex>0 */ {
2205 /* try an extension mapping */
2207 cnv
->toUBytes
[0]=*(source
-1);
2208 cnv
->toULength
=_extToU(cnv
, cnv
->sharedData
,
2209 1, &source
, sourceLimit
,
2210 &target
, pArgs
->targetLimit
,
2211 &offsets
, sourceIndex
,
2214 sourceIndex
+=1+(int32_t)(source
-lastSource
);
2216 if(U_FAILURE(*pErrorCode
)) {
2217 /* not mappable or buffer overflow */
2221 /* recalculate the targetCapacity after an extension mapping */
2222 targetCapacity
=(int32_t)(pArgs
->targetLimit
-target
);
2223 length
=(int32_t)(sourceLimit
-source
);
2224 if(length
<targetCapacity
) {
2225 targetCapacity
=length
;
2229 #if MBCS_UNROLL_SINGLE_TO_BMP
2230 /* unrolling makes it faster on Pentium III/Windows 2000 */
2235 if(U_SUCCESS(*pErrorCode
) && source
<sourceLimit
&& target
>=pArgs
->targetLimit
) {
2236 /* target is full */
2237 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2240 /* set offsets since the start or the last callback */
2242 size_t count
=source
-lastSource
;
2244 *offsets
++=sourceIndex
++;
2249 /* write back the updated pointers */
2250 pArgs
->source
=(const char *)source
;
2251 pArgs
->target
=target
;
2252 pArgs
->offsets
=offsets
;
2256 hasValidTrailBytes(const int32_t (*stateTable
)[256], uint8_t state
) {
2257 const int32_t *row
=stateTable
[state
];
2259 /* First test for final entries in this state for some commonly valid byte values. */
2261 if( !MBCS_ENTRY_IS_TRANSITION(entry
) &&
2262 MBCS_ENTRY_FINAL_ACTION(entry
)!=MBCS_STATE_ILLEGAL
2267 if( !MBCS_ENTRY_IS_TRANSITION(entry
) &&
2268 MBCS_ENTRY_FINAL_ACTION(entry
)!=MBCS_STATE_ILLEGAL
2272 /* Then test for final entries in this state. */
2273 for(b
=0; b
<=0xff; ++b
) {
2275 if( !MBCS_ENTRY_IS_TRANSITION(entry
) &&
2276 MBCS_ENTRY_FINAL_ACTION(entry
)!=MBCS_STATE_ILLEGAL
2281 /* Then recurse for transition entries. */
2282 for(b
=0; b
<=0xff; ++b
) {
2284 if( MBCS_ENTRY_IS_TRANSITION(entry
) &&
2285 hasValidTrailBytes(stateTable
, (uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
))
2294 * Is byte b a single/lead byte in this state?
2295 * Recurse for transition states, because here we don't want to say that
2296 * b is a lead byte if all byte sequences that start with b are illegal.
2299 isSingleOrLead(const int32_t (*stateTable
)[256], uint8_t state
, UBool isDBCSOnly
, uint8_t b
) {
2300 const int32_t *row
=stateTable
[state
];
2301 int32_t entry
=row
[b
];
2302 if(MBCS_ENTRY_IS_TRANSITION(entry
)) { /* lead byte */
2303 return hasValidTrailBytes(stateTable
, (uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
));
2305 uint8_t action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2306 if(action
==MBCS_STATE_CHANGE_ONLY
&& isDBCSOnly
) {
2307 return FALSE
; /* SI/SO are illegal for DBCS-only conversion */
2309 return action
!=MBCS_STATE_ILLEGAL
;
2315 ucnv_MBCSToUnicodeWithOffsets(UConverterToUnicodeArgs
*pArgs
,
2316 UErrorCode
*pErrorCode
) {
2318 const uint8_t *source
, *sourceLimit
;
2320 const UChar
*targetLimit
;
2323 const int32_t (*stateTable
)[256];
2324 const uint16_t *unicodeCodeUnits
;
2331 int32_t sourceIndex
, nextSourceIndex
;
2337 /* use optimized function if possible */
2338 cnv
=pArgs
->converter
;
2340 if(cnv
->preToULength
>0) {
2342 * pass sourceIndex=-1 because we continue from an earlier buffer
2343 * in the future, this may change with continuous offsets
2345 ucnv_extContinueMatchToU(cnv
, pArgs
, -1, pErrorCode
);
2347 if(U_FAILURE(*pErrorCode
) || cnv
->preToULength
<0) {
2352 if(cnv
->sharedData
->mbcs
.countStates
==1) {
2353 if(!(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
2354 ucnv_MBCSSingleToBMPWithOffsets(pArgs
, pErrorCode
);
2356 ucnv_MBCSSingleToUnicodeWithOffsets(pArgs
, pErrorCode
);
2361 /* set up the local pointers */
2362 source
=(const uint8_t *)pArgs
->source
;
2363 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
2364 target
=pArgs
->target
;
2365 targetLimit
=pArgs
->targetLimit
;
2366 offsets
=pArgs
->offsets
;
2368 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2369 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
2371 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
2373 unicodeCodeUnits
=cnv
->sharedData
->mbcs
.unicodeCodeUnits
;
2375 /* get the converter state from UConverter */
2376 offset
=cnv
->toUnicodeStatus
;
2377 byteIndex
=cnv
->toULength
;
2378 bytes
=cnv
->toUBytes
;
2381 * if we are in the SBCS state for a DBCS-only converter,
2382 * then load the DBCS state from the MBCS data
2383 * (dbcsOnlyState==0 if it is not a DBCS-only converter)
2385 if((state
=(uint8_t)(cnv
->mode
))==0) {
2386 state
=cnv
->sharedData
->mbcs
.dbcsOnlyState
;
2389 /* sourceIndex=-1 if the current character began in the previous buffer */
2390 sourceIndex
=byteIndex
==0 ? 0 : -1;
2393 /* conversion loop */
2394 while(source
<sourceLimit
) {
2396 * This following test is to see if available input would overflow the output.
2397 * It does not catch output of more than one code unit that
2398 * overflows as a result of a surrogate pair or callback output
2399 * from the last source byte.
2400 * Therefore, those situations also test for overflows and will
2401 * then break the loop, too.
2403 if(target
>=targetLimit
) {
2404 /* target is full */
2405 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2410 /* optimized loop for 1/2-byte input and BMP output */
2413 entry
=stateTable
[state
][*source
];
2414 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
2415 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
2416 offset
=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
2419 if( source
<sourceLimit
&&
2420 MBCS_ENTRY_IS_FINAL(entry
=stateTable
[state
][*source
]) &&
2421 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_VALID_16
&&
2422 (c
=unicodeCodeUnits
[offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
)])<0xfffe
2426 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2429 /* set the state and leave the optimized loop */
2430 bytes
[0]=*(source
-1);
2435 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
2436 /* output BMP code point */
2438 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2439 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2441 /* leave the optimized loop */
2445 } while(source
<sourceLimit
&& target
<targetLimit
);
2446 } else /* offsets!=NULL */ {
2448 entry
=stateTable
[state
][*source
];
2449 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
2450 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
2451 offset
=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
2454 if( source
<sourceLimit
&&
2455 MBCS_ENTRY_IS_FINAL(entry
=stateTable
[state
][*source
]) &&
2456 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_VALID_16
&&
2457 (c
=unicodeCodeUnits
[offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
)])<0xfffe
2462 *offsets
++=sourceIndex
;
2463 sourceIndex
=(nextSourceIndex
+=2);
2465 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2468 /* set the state and leave the optimized loop */
2470 bytes
[0]=*(source
-1);
2475 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
2476 /* output BMP code point */
2478 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2480 *offsets
++=sourceIndex
;
2481 sourceIndex
=++nextSourceIndex
;
2483 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2485 /* leave the optimized loop */
2489 } while(source
<sourceLimit
&& target
<targetLimit
);
2493 * these tests and break statements could be put inside the loop
2494 * if C had "break outerLoop" like Java
2496 if(source
>=sourceLimit
) {
2499 if(target
>=targetLimit
) {
2500 /* target is full */
2501 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2506 bytes
[byteIndex
++]=*source
++;
2507 } else /* byteIndex>0 */ {
2509 entry
=stateTable
[state
][bytes
[byteIndex
++]=*source
++];
2512 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
2513 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
2514 offset
+=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
2518 /* save the previous state for proper extension mapping with SI/SO-stateful converters */
2521 /* set the next state early so that we can reuse the entry variable */
2522 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2525 * An if-else-if chain provides more reliable performance for
2526 * the most common cases compared to a switch.
2528 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2529 if(action
==MBCS_STATE_VALID_16
) {
2530 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
2531 c
=unicodeCodeUnits
[offset
];
2533 /* output BMP code point */
2536 *offsets
++=sourceIndex
;
2539 } else if(c
==0xfffe) {
2540 if(UCNV_TO_U_USE_FALLBACK(cnv
) && (entry
=(int32_t)ucnv_MBCSGetFallback(&cnv
->sharedData
->mbcs
, offset
))!=0xfffe) {
2541 /* output fallback BMP code point */
2542 *target
++=(UChar
)entry
;
2544 *offsets
++=sourceIndex
;
2549 /* callback(illegal) */
2550 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2552 } else if(action
==MBCS_STATE_VALID_DIRECT_16
) {
2553 /* output BMP code point */
2554 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2556 *offsets
++=sourceIndex
;
2559 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
2560 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
2561 c
=unicodeCodeUnits
[offset
++];
2563 /* output BMP code point below 0xd800 */
2566 *offsets
++=sourceIndex
;
2569 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? c
<=0xdfff : c
<=0xdbff) {
2570 /* output roundtrip or fallback surrogate pair */
2571 *target
++=(UChar
)(c
&0xdbff);
2573 *offsets
++=sourceIndex
;
2576 if(target
<targetLimit
) {
2577 *target
++=unicodeCodeUnits
[offset
];
2579 *offsets
++=sourceIndex
;
2582 /* target overflow */
2583 cnv
->UCharErrorBuffer
[0]=unicodeCodeUnits
[offset
];
2584 cnv
->UCharErrorBufferLength
=1;
2585 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2590 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? (c
&0xfffe)==0xe000 : c
==0xe000) {
2591 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
2592 *target
++=unicodeCodeUnits
[offset
];
2594 *offsets
++=sourceIndex
;
2597 } else if(c
==0xffff) {
2598 /* callback(illegal) */
2599 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2601 } else if(action
==MBCS_STATE_VALID_DIRECT_20
||
2602 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
2604 entry
=MBCS_ENTRY_FINAL_VALUE(entry
);
2605 /* output surrogate pair */
2606 *target
++=(UChar
)(0xd800|(UChar
)(entry
>>10));
2608 *offsets
++=sourceIndex
;
2611 c
=(UChar
)(0xdc00|(UChar
)(entry
&0x3ff));
2612 if(target
<targetLimit
) {
2615 *offsets
++=sourceIndex
;
2618 /* target overflow */
2619 cnv
->UCharErrorBuffer
[0]=c
;
2620 cnv
->UCharErrorBufferLength
=1;
2621 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2626 } else if(action
==MBCS_STATE_CHANGE_ONLY
) {
2628 * This serves as a state change without any output.
2629 * It is useful for reading simple stateful encodings,
2630 * for example using just Shift-In/Shift-Out codes.
2631 * The 21 unused bits may later be used for more sophisticated
2632 * state transitions.
2634 if(cnv
->sharedData
->mbcs
.dbcsOnlyState
==0) {
2637 /* SI/SO are illegal for DBCS-only conversion */
2638 state
=(uint8_t)(cnv
->mode
); /* restore the previous state */
2640 /* callback(illegal) */
2641 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2643 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2644 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
2645 /* output BMP code point */
2646 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2648 *offsets
++=sourceIndex
;
2652 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2653 /* just fall through */
2654 } else if(action
==MBCS_STATE_ILLEGAL
) {
2655 /* callback(illegal) */
2656 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2658 /* reserved, must never occur */
2662 /* end of action codes: prepare for a new character */
2666 sourceIndex
=nextSourceIndex
;
2667 } else if(U_FAILURE(*pErrorCode
)) {
2668 /* callback(illegal) */
2671 * Ticket 5691: consistent illegal sequences:
2672 * - We include at least the first byte in the illegal sequence.
2673 * - If any of the non-initial bytes could be the start of a character,
2674 * we stop the illegal sequence before the first one of those.
2676 UBool isDBCSOnly
=(UBool
)(cnv
->sharedData
->mbcs
.dbcsOnlyState
!=0);
2679 i
<byteIndex
&& !isSingleOrLead(stateTable
, state
, isDBCSOnly
, bytes
[i
]);
2682 /* Back out some bytes. */
2683 int8_t backOutDistance
=byteIndex
-i
;
2684 int32_t bytesFromThisBuffer
=(int32_t)(source
-(const uint8_t *)pArgs
->source
);
2685 byteIndex
=i
; /* length of reported illegal byte sequence */
2686 if(backOutDistance
<=bytesFromThisBuffer
) {
2687 source
-=backOutDistance
;
2689 /* Back out bytes from the previous buffer: Need to replay them. */
2690 cnv
->preToULength
=(int8_t)(bytesFromThisBuffer
-backOutDistance
);
2691 /* preToULength is negative! */
2692 uprv_memcpy(cnv
->preToU
, bytes
+i
, -cnv
->preToULength
);
2693 source
=(const uint8_t *)pArgs
->source
;
2698 } else /* unassigned sequences indicated with byteIndex>0 */ {
2699 /* try an extension mapping */
2700 pArgs
->source
=(const char *)source
;
2701 byteIndex
=_extToU(cnv
, cnv
->sharedData
,
2702 byteIndex
, &source
, sourceLimit
,
2703 &target
, targetLimit
,
2704 &offsets
, sourceIndex
,
2707 sourceIndex
=nextSourceIndex
+=(int32_t)(source
-(const uint8_t *)pArgs
->source
);
2709 if(U_FAILURE(*pErrorCode
)) {
2710 /* not mappable or buffer overflow */
2716 /* set the converter state back into UConverter */
2717 cnv
->toUnicodeStatus
=offset
;
2719 cnv
->toULength
=byteIndex
;
2721 /* write back the updated pointers */
2722 pArgs
->source
=(const char *)source
;
2723 pArgs
->target
=target
;
2724 pArgs
->offsets
=offsets
;
2728 * This version of ucnv_MBCSGetNextUChar() is optimized for single-byte, single-state codepages.
2729 * We still need a conversion loop in case we find reserved action codes, which are to be ignored.
2732 ucnv_MBCSSingleGetNextUChar(UConverterToUnicodeArgs
*pArgs
,
2733 UErrorCode
*pErrorCode
) {
2735 const int32_t (*stateTable
)[256];
2736 const uint8_t *source
, *sourceLimit
;
2741 /* set up the local pointers */
2742 cnv
=pArgs
->converter
;
2743 source
=(const uint8_t *)pArgs
->source
;
2744 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
2745 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2746 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
2748 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
2751 /* conversion loop */
2752 while(source
<sourceLimit
) {
2753 entry
=stateTable
[0][*source
++];
2754 /* MBCS_ENTRY_IS_FINAL(entry) */
2756 /* write back the updated pointer early so that we can return directly */
2757 pArgs
->source
=(const char *)source
;
2759 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
2760 /* output BMP code point */
2761 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2765 * An if-else-if chain provides more reliable performance for
2766 * the most common cases compared to a switch.
2768 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2769 if( action
==MBCS_STATE_VALID_DIRECT_20
||
2770 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
2772 /* output supplementary code point */
2773 return (UChar32
)(MBCS_ENTRY_FINAL_VALUE(entry
)+0x10000);
2774 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2775 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
2776 /* output BMP code point */
2777 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2779 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2780 /* just fall through */
2781 } else if(action
==MBCS_STATE_ILLEGAL
) {
2782 /* callback(illegal) */
2783 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2785 /* reserved, must never occur */
2789 if(U_FAILURE(*pErrorCode
)) {
2790 /* callback(illegal) */
2792 } else /* unassigned sequence */ {
2793 /* defer to the generic implementation */
2794 pArgs
->source
=(const char *)source
-1;
2795 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
2799 /* no output because of empty input or only state changes */
2800 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
2805 * Version of _MBCSToUnicodeWithOffsets() optimized for single-character
2806 * conversion without offset handling.
2808 * When a character does not have a mapping to Unicode, then we return to the
2809 * generic ucnv_getNextUChar() code for extension/GB 18030 and error/callback
2811 * We also defer to the generic code in other complicated cases and have them
2812 * ultimately handled by _MBCSToUnicodeWithOffsets() itself.
2814 * All normal mappings and errors are handled here.
2817 ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs
*pArgs
,
2818 UErrorCode
*pErrorCode
) {
2820 const uint8_t *source
, *sourceLimit
, *lastSource
;
2822 const int32_t (*stateTable
)[256];
2823 const uint16_t *unicodeCodeUnits
;
2832 /* use optimized function if possible */
2833 cnv
=pArgs
->converter
;
2835 if(cnv
->preToULength
>0) {
2836 /* use the generic code in ucnv_getNextUChar() to continue with a partial match */
2837 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
2840 if(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SURROGATES
) {
2842 * Using the generic ucnv_getNextUChar() code lets us deal correctly
2843 * with the rare case of a codepage that maps single surrogates
2844 * without adding the complexity to this already complicated function here.
2846 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
2847 } else if(cnv
->sharedData
->mbcs
.countStates
==1) {
2848 return ucnv_MBCSSingleGetNextUChar(pArgs
, pErrorCode
);
2851 /* set up the local pointers */
2852 source
=lastSource
=(const uint8_t *)pArgs
->source
;
2853 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
2855 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2856 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
2858 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
2860 unicodeCodeUnits
=cnv
->sharedData
->mbcs
.unicodeCodeUnits
;
2862 /* get the converter state from UConverter */
2863 offset
=cnv
->toUnicodeStatus
;
2866 * if we are in the SBCS state for a DBCS-only converter,
2867 * then load the DBCS state from the MBCS data
2868 * (dbcsOnlyState==0 if it is not a DBCS-only converter)
2870 if((state
=(uint8_t)(cnv
->mode
))==0) {
2871 state
=cnv
->sharedData
->mbcs
.dbcsOnlyState
;
2874 /* conversion loop */
2876 while(source
<sourceLimit
) {
2877 entry
=stateTable
[state
][*source
++];
2878 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
2879 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
2880 offset
+=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
2882 /* optimization for 1/2-byte input and BMP output */
2883 if( source
<sourceLimit
&&
2884 MBCS_ENTRY_IS_FINAL(entry
=stateTable
[state
][*source
]) &&
2885 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_VALID_16
&&
2886 (c
=unicodeCodeUnits
[offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
)])<0xfffe
2889 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2890 /* output BMP code point */
2894 /* save the previous state for proper extension mapping with SI/SO-stateful converters */
2897 /* set the next state early so that we can reuse the entry variable */
2898 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2901 * An if-else-if chain provides more reliable performance for
2902 * the most common cases compared to a switch.
2904 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2905 if(action
==MBCS_STATE_VALID_DIRECT_16
) {
2906 /* output BMP code point */
2907 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2909 } else if(action
==MBCS_STATE_VALID_16
) {
2910 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
2911 c
=unicodeCodeUnits
[offset
];
2913 /* output BMP code point */
2915 } else if(c
==0xfffe) {
2916 if(UCNV_TO_U_USE_FALLBACK(cnv
) && (c
=ucnv_MBCSGetFallback(&cnv
->sharedData
->mbcs
, offset
))!=0xfffe) {
2920 /* callback(illegal) */
2921 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2923 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
2924 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
2925 c
=unicodeCodeUnits
[offset
++];
2927 /* output BMP code point below 0xd800 */
2929 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? c
<=0xdfff : c
<=0xdbff) {
2930 /* output roundtrip or fallback supplementary code point */
2931 c
=((c
&0x3ff)<<10)+unicodeCodeUnits
[offset
]+(0x10000-0xdc00);
2933 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? (c
&0xfffe)==0xe000 : c
==0xe000) {
2934 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
2935 c
=unicodeCodeUnits
[offset
];
2937 } else if(c
==0xffff) {
2938 /* callback(illegal) */
2939 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2941 } else if(action
==MBCS_STATE_VALID_DIRECT_20
||
2942 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
2944 /* output supplementary code point */
2945 c
=(UChar32
)(MBCS_ENTRY_FINAL_VALUE(entry
)+0x10000);
2947 } else if(action
==MBCS_STATE_CHANGE_ONLY
) {
2949 * This serves as a state change without any output.
2950 * It is useful for reading simple stateful encodings,
2951 * for example using just Shift-In/Shift-Out codes.
2952 * The 21 unused bits may later be used for more sophisticated
2953 * state transitions.
2955 if(cnv
->sharedData
->mbcs
.dbcsOnlyState
!=0) {
2956 /* SI/SO are illegal for DBCS-only conversion */
2957 state
=(uint8_t)(cnv
->mode
); /* restore the previous state */
2959 /* callback(illegal) */
2960 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2962 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2963 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
2964 /* output BMP code point */
2965 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2968 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2969 /* just fall through */
2970 } else if(action
==MBCS_STATE_ILLEGAL
) {
2971 /* callback(illegal) */
2972 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2974 /* reserved (must never occur), or only state change */
2980 /* end of action codes: prepare for a new character */
2983 if(U_FAILURE(*pErrorCode
)) {
2984 /* callback(illegal) */
2986 } else /* unassigned sequence */ {
2987 /* defer to the generic implementation */
2988 cnv
->toUnicodeStatus
=0;
2990 pArgs
->source
=(const char *)lastSource
;
2991 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
2997 if(U_SUCCESS(*pErrorCode
) && source
==sourceLimit
&& lastSource
<source
) {
2998 /* incomplete character byte sequence */
2999 uint8_t *bytes
=cnv
->toUBytes
;
3000 cnv
->toULength
=(int8_t)(source
-lastSource
);
3002 *bytes
++=*lastSource
++;
3003 } while(lastSource
<source
);
3004 *pErrorCode
=U_TRUNCATED_CHAR_FOUND
;
3005 } else if(U_FAILURE(*pErrorCode
)) {
3006 /* callback(illegal) */
3008 * Ticket 5691: consistent illegal sequences:
3009 * - We include at least the first byte in the illegal sequence.
3010 * - If any of the non-initial bytes could be the start of a character,
3011 * we stop the illegal sequence before the first one of those.
3013 UBool isDBCSOnly
=(UBool
)(cnv
->sharedData
->mbcs
.dbcsOnlyState
!=0);
3014 uint8_t *bytes
=cnv
->toUBytes
;
3015 *bytes
++=*lastSource
++; /* first byte */
3016 if(lastSource
==source
) {
3018 } else /* lastSource<source: multi-byte character */ {
3021 lastSource
<source
&& !isSingleOrLead(stateTable
, state
, isDBCSOnly
, *lastSource
);
3024 *bytes
++=*lastSource
++;
3030 /* no output because of empty input or only state changes */
3031 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
3036 /* set the converter state back into UConverter, ready for a new character */
3037 cnv
->toUnicodeStatus
=0;
3040 /* write back the updated pointer */
3041 pArgs
->source
=(const char *)source
;
3047 * Code disabled 2002dec09 (ICU 2.4) because it is not currently used in ICU. markus
3048 * Removal improves code coverage.
3051 * This version of ucnv_MBCSSimpleGetNextUChar() is optimized for single-byte, single-state codepages.
3052 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
3053 * It does not handle conversion extensions (_extToU()).
3056 ucnv_MBCSSingleSimpleGetNextUChar(UConverterSharedData
*sharedData
,
3057 uint8_t b
, UBool useFallback
) {
3061 entry
=sharedData
->mbcs
.stateTable
[0][b
];
3062 /* MBCS_ENTRY_IS_FINAL(entry) */
3064 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
3065 /* output BMP code point */
3066 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
3070 * An if-else-if chain provides more reliable performance for
3071 * the most common cases compared to a switch.
3073 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
3074 if(action
==MBCS_STATE_VALID_DIRECT_20
) {
3075 /* output supplementary code point */
3076 return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
3077 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
3078 if(!TO_U_USE_FALLBACK(useFallback
)) {
3081 /* output BMP code point */
3082 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
3083 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_20
) {
3084 if(!TO_U_USE_FALLBACK(useFallback
)) {
3087 /* output supplementary code point */
3088 return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
3089 } else if(action
==MBCS_STATE_UNASSIGNED
) {
3091 } else if(action
==MBCS_STATE_ILLEGAL
) {
3094 /* reserved, must never occur */
3101 * This is a simple version of _MBCSGetNextUChar() that is used
3102 * by other converter implementations.
3103 * It only returns an "assigned" result if it consumes the entire input.
3104 * It does not use state from the converter, nor error codes.
3105 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
3106 * It handles conversion extensions but not GB 18030.
3111 * otherwise the Unicode code point
3114 ucnv_MBCSSimpleGetNextUChar(UConverterSharedData
*sharedData
,
3115 const char *source
, int32_t length
,
3116 UBool useFallback
) {
3117 const int32_t (*stateTable
)[256];
3118 const uint16_t *unicodeCodeUnits
;
3121 uint8_t state
, action
;
3127 /* no input at all: "illegal" */
3133 * Code disabled 2002dec09 (ICU 2.4) because it is not currently used in ICU. markus
3134 * TODO In future releases, verify that this function is never called for SBCS
3135 * conversions, i.e., that sharedData->mbcs.countStates==1 is still true.
3136 * Removal improves code coverage.
3138 /* use optimized function if possible */
3139 if(sharedData
->mbcs
.countStates
==1) {
3141 return ucnv_MBCSSingleSimpleGetNextUChar(sharedData
, (uint8_t)*source
, useFallback
);
3143 return 0xffff; /* illegal: more than a single byte for an SBCS converter */
3148 /* set up the local pointers */
3149 stateTable
=sharedData
->mbcs
.stateTable
;
3150 unicodeCodeUnits
=sharedData
->mbcs
.unicodeCodeUnits
;
3152 /* converter state */
3154 state
=sharedData
->mbcs
.dbcsOnlyState
;
3156 /* conversion loop */
3158 entry
=stateTable
[state
][(uint8_t)source
[i
++]];
3159 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
3160 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
3161 offset
+=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
3164 return 0xffff; /* truncated character */
3168 * An if-else-if chain provides more reliable performance for
3169 * the most common cases compared to a switch.
3171 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
3172 if(action
==MBCS_STATE_VALID_16
) {
3173 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
3174 c
=unicodeCodeUnits
[offset
];
3177 } else if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
3178 c
=ucnv_MBCSGetFallback(&sharedData
->mbcs
, offset
);
3179 /* else done with 0xfffe */
3182 } else if(action
==MBCS_STATE_VALID_DIRECT_16
) {
3183 /* output BMP code point */
3184 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
3186 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
3187 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
3188 c
=unicodeCodeUnits
[offset
++];
3190 /* output BMP code point below 0xd800 */
3191 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? c
<=0xdfff : c
<=0xdbff) {
3192 /* output roundtrip or fallback supplementary code point */
3193 c
=(UChar32
)(((c
&0x3ff)<<10)+unicodeCodeUnits
[offset
]+(0x10000-0xdc00));
3194 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? (c
&0xfffe)==0xe000 : c
==0xe000) {
3195 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
3196 c
=unicodeCodeUnits
[offset
];
3197 } else if(c
==0xffff) {
3203 } else if(action
==MBCS_STATE_VALID_DIRECT_20
) {
3204 /* output supplementary code point */
3205 c
=0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
3207 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
3208 if(!TO_U_USE_FALLBACK(useFallback
)) {
3212 /* output BMP code point */
3213 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
3215 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_20
) {
3216 if(!TO_U_USE_FALLBACK(useFallback
)) {
3220 /* output supplementary code point */
3221 c
=0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
3223 } else if(action
==MBCS_STATE_UNASSIGNED
) {
3229 * forbid MBCS_STATE_CHANGE_ONLY for this function,
3230 * and MBCS_STATE_ILLEGAL and reserved action codes
3237 /* illegal for this function: not all input consumed */
3242 /* try an extension mapping */
3243 const int32_t *cx
=sharedData
->mbcs
.extIndexes
;
3245 return ucnv_extSimpleMatchToU(cx
, source
, length
, useFallback
);
3252 /* MBCS-from-Unicode conversion functions ----------------------------------- */
3254 /* This version of ucnv_MBCSFromUnicodeWithOffsets() is optimized for double-byte codepages. */
3256 ucnv_MBCSDoubleFromUnicodeWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
3257 UErrorCode
*pErrorCode
) {
3259 const UChar
*source
, *sourceLimit
;
3261 int32_t targetCapacity
;
3264 const uint16_t *table
;
3265 const uint16_t *mbcsIndex
;
3266 const uint8_t *bytes
;
3270 int32_t sourceIndex
, nextSourceIndex
;
3272 uint32_t stage2Entry
;
3273 uint32_t asciiRoundtrips
;
3275 uint8_t unicodeMask
;
3277 /* use optimized function if possible */
3278 cnv
=pArgs
->converter
;
3279 unicodeMask
=cnv
->sharedData
->mbcs
.unicodeMask
;
3281 /* set up the local pointers */
3282 source
=pArgs
->source
;
3283 sourceLimit
=pArgs
->sourceLimit
;
3284 target
=(uint8_t *)pArgs
->target
;
3285 targetCapacity
=(int32_t)(pArgs
->targetLimit
-pArgs
->target
);
3286 offsets
=pArgs
->offsets
;
3288 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
3289 mbcsIndex
=cnv
->sharedData
->mbcs
.mbcsIndex
;
3290 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
3291 bytes
=cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
3293 bytes
=cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
3295 asciiRoundtrips
=cnv
->sharedData
->mbcs
.asciiRoundtrips
;
3297 /* get the converter state from UConverter */
3300 /* sourceIndex=-1 if the current character began in the previous buffer */
3301 sourceIndex
= c
==0 ? 0 : -1;
3304 /* conversion loop */
3305 if(c
!=0 && targetCapacity
>0) {
3309 while(source
<sourceLimit
) {
3311 * This following test is to see if available input would overflow the output.
3312 * It does not catch output of more than one byte that
3313 * overflows as a result of a multi-byte character or callback output
3314 * from the last source character.
3315 * Therefore, those situations also test for overflows and will
3316 * then break the loop, too.
3318 if(targetCapacity
>0) {
3320 * Get a correct Unicode code point:
3321 * a single UChar for a BMP code point or
3322 * a matched surrogate pair for a "supplementary code point".
3326 if(c
<=0x7f && IS_ASCII_ROUNDTRIP(c
, asciiRoundtrips
)) {
3327 *target
++=(uint8_t)c
;
3329 *offsets
++=sourceIndex
;
3330 sourceIndex
=nextSourceIndex
;
3337 * utf8Friendly table: Test for <=0xd7ff rather than <=MBCS_FAST_MAX
3338 * to avoid dealing with surrogates.
3339 * MBCS_FAST_MAX must be >=0xd7ff.
3342 value
=DBCS_RESULT_FROM_MOST_BMP(mbcsIndex
, (const uint16_t *)bytes
, c
);
3343 /* There are only roundtrips (!=0) and no-mapping (==0) entries. */
3347 /* output the value */
3350 * This also tests if the codepage maps single surrogates.
3351 * If it does, then surrogates are not paired but mapped separately.
3352 * Note that in this case unmatched surrogates are not detected.
3354 if(UTF_IS_SURROGATE(c
) && !(unicodeMask
&UCNV_HAS_SURROGATES
)) {
3355 if(UTF_IS_SURROGATE_FIRST(c
)) {
3357 if(source
<sourceLimit
) {
3358 /* test the following code unit */
3359 UChar trail
=*source
;
3360 if(UTF_IS_SECOND_SURROGATE(trail
)) {
3363 c
=UTF16_GET_PAIR_VALUE(c
, trail
);
3364 if(!(unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
3365 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
3366 /* callback(unassigned) */
3369 /* convert this supplementary code point */
3370 /* exit this condition tree */
3372 /* this is an unmatched lead code unit (1st surrogate) */
3373 /* callback(illegal) */
3374 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3382 /* this is an unmatched trail code unit (2nd surrogate) */
3383 /* callback(illegal) */
3384 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3389 /* convert the Unicode code point in c into codepage bytes */
3390 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
3392 /* get the bytes and the length for the output */
3394 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
3396 /* is this code point assigned, or do we use fallbacks? */
3397 if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
) ||
3398 (UCNV_FROM_U_USE_FALLBACK(cnv
, c
) && value
!=0))
3401 * We allow a 0 byte output if the "assigned" bit is set for this entry.
3402 * There is no way with this data structure for fallback output
3403 * to be a zero byte.
3407 /* try an extension mapping */
3408 pArgs
->source
=source
;
3409 c
=_extFromU(cnv
, cnv
->sharedData
,
3410 c
, &source
, sourceLimit
,
3411 &target
, target
+targetCapacity
,
3412 &offsets
, sourceIndex
,
3415 nextSourceIndex
+=(int32_t)(source
-pArgs
->source
);
3417 if(U_FAILURE(*pErrorCode
)) {
3418 /* not mappable or buffer overflow */
3421 /* a mapping was written to the target, continue */
3423 /* recalculate the targetCapacity after an extension mapping */
3424 targetCapacity
=(int32_t)(pArgs
->targetLimit
-(char *)target
);
3426 /* normal end of conversion: prepare for a new character */
3427 sourceIndex
=nextSourceIndex
;
3433 /* write the output character bytes from value and length */
3434 /* from the first if in the loop we know that targetCapacity>0 */
3436 /* this is easy because we know that there is enough space */
3437 *target
++=(uint8_t)value
;
3439 *offsets
++=sourceIndex
;
3442 } else /* length==2 */ {
3443 *target
++=(uint8_t)(value
>>8);
3444 if(2<=targetCapacity
) {
3445 *target
++=(uint8_t)value
;
3447 *offsets
++=sourceIndex
;
3448 *offsets
++=sourceIndex
;
3453 *offsets
++=sourceIndex
;
3455 cnv
->charErrorBuffer
[0]=(char)value
;
3456 cnv
->charErrorBufferLength
=1;
3458 /* target overflow */
3460 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
3466 /* normal end of conversion: prepare for a new character */
3468 sourceIndex
=nextSourceIndex
;
3471 /* target is full */
3472 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
3477 /* set the converter state back into UConverter */
3480 /* write back the updated pointers */
3481 pArgs
->source
=source
;
3482 pArgs
->target
=(char *)target
;
3483 pArgs
->offsets
=offsets
;
3486 /* This version of ucnv_MBCSFromUnicodeWithOffsets() is optimized for single-byte codepages. */
3488 ucnv_MBCSSingleFromUnicodeWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
3489 UErrorCode
*pErrorCode
) {
3491 const UChar
*source
, *sourceLimit
;
3493 int32_t targetCapacity
;
3496 const uint16_t *table
;
3497 const uint16_t *results
;
3501 int32_t sourceIndex
, nextSourceIndex
;
3503 uint16_t value
, minValue
;
3504 UBool hasSupplementary
;
3506 /* set up the local pointers */
3507 cnv
=pArgs
->converter
;
3508 source
=pArgs
->source
;
3509 sourceLimit
=pArgs
->sourceLimit
;
3510 target
=(uint8_t *)pArgs
->target
;
3511 targetCapacity
=(int32_t)(pArgs
->targetLimit
-pArgs
->target
);
3512 offsets
=pArgs
->offsets
;
3514 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
3515 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
3516 results
=(uint16_t *)cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
3518 results
=(uint16_t *)cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
3521 if(cnv
->useFallback
) {
3522 /* use all roundtrip and fallback results */
3525 /* use only roundtrips and fallbacks from private-use characters */
3528 hasSupplementary
=(UBool
)(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
);
3530 /* get the converter state from UConverter */
3533 /* sourceIndex=-1 if the current character began in the previous buffer */
3534 sourceIndex
= c
==0 ? 0 : -1;
3537 /* conversion loop */
3538 if(c
!=0 && targetCapacity
>0) {
3542 while(source
<sourceLimit
) {
3544 * This following test is to see if available input would overflow the output.
3545 * It does not catch output of more than one byte that
3546 * overflows as a result of a multi-byte character or callback output
3547 * from the last source character.
3548 * Therefore, those situations also test for overflows and will
3549 * then break the loop, too.
3551 if(targetCapacity
>0) {
3553 * Get a correct Unicode code point:
3554 * a single UChar for a BMP code point or
3555 * a matched surrogate pair for a "supplementary code point".
3559 if(UTF_IS_SURROGATE(c
)) {
3560 if(UTF_IS_SURROGATE_FIRST(c
)) {
3562 if(source
<sourceLimit
) {
3563 /* test the following code unit */
3564 UChar trail
=*source
;
3565 if(UTF_IS_SECOND_SURROGATE(trail
)) {
3568 c
=UTF16_GET_PAIR_VALUE(c
, trail
);
3569 if(!hasSupplementary
) {
3570 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
3571 /* callback(unassigned) */
3574 /* convert this supplementary code point */
3575 /* exit this condition tree */
3577 /* this is an unmatched lead code unit (1st surrogate) */
3578 /* callback(illegal) */
3579 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3587 /* this is an unmatched trail code unit (2nd surrogate) */
3588 /* callback(illegal) */
3589 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3594 /* convert the Unicode code point in c into codepage bytes */
3595 value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3597 /* is this code point assigned, or do we use fallbacks? */
3598 if(value
>=minValue
) {
3599 /* assigned, write the output character bytes from value and length */
3601 /* this is easy because we know that there is enough space */
3602 *target
++=(uint8_t)value
;
3604 *offsets
++=sourceIndex
;
3608 /* normal end of conversion: prepare for a new character */
3610 sourceIndex
=nextSourceIndex
;
3611 } else { /* unassigned */
3613 /* try an extension mapping */
3614 pArgs
->source
=source
;
3615 c
=_extFromU(cnv
, cnv
->sharedData
,
3616 c
, &source
, sourceLimit
,
3617 &target
, target
+targetCapacity
,
3618 &offsets
, sourceIndex
,
3621 nextSourceIndex
+=(int32_t)(source
-pArgs
->source
);
3623 if(U_FAILURE(*pErrorCode
)) {
3624 /* not mappable or buffer overflow */
3627 /* a mapping was written to the target, continue */
3629 /* recalculate the targetCapacity after an extension mapping */
3630 targetCapacity
=(int32_t)(pArgs
->targetLimit
-(char *)target
);
3632 /* normal end of conversion: prepare for a new character */
3633 sourceIndex
=nextSourceIndex
;
3637 /* target is full */
3638 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
3643 /* set the converter state back into UConverter */
3646 /* write back the updated pointers */
3647 pArgs
->source
=source
;
3648 pArgs
->target
=(char *)target
;
3649 pArgs
->offsets
=offsets
;
3653 * This version of ucnv_MBCSFromUnicode() is optimized for single-byte codepages
3654 * that map only to and from the BMP.
3655 * In addition to single-byte/state optimizations, the offset calculations
3656 * become much easier.
3657 * It would be possible to use the sbcsIndex for UTF-8-friendly tables,
3658 * but measurements have shown that this diminishes performance
3659 * in more cases than it improves it.
3660 * See SVN revision 21013 (2007-feb-06) for the last version with #if switches
3661 * for various MBCS and SBCS optimizations.
3664 ucnv_MBCSSingleFromBMPWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
3665 UErrorCode
*pErrorCode
) {
3667 const UChar
*source
, *sourceLimit
, *lastSource
;
3669 int32_t targetCapacity
, length
;
3672 const uint16_t *table
;
3673 const uint16_t *results
;
3677 int32_t sourceIndex
;
3679 uint32_t asciiRoundtrips
;
3680 uint16_t value
, minValue
;
3682 /* set up the local pointers */
3683 cnv
=pArgs
->converter
;
3684 source
=pArgs
->source
;
3685 sourceLimit
=pArgs
->sourceLimit
;
3686 target
=(uint8_t *)pArgs
->target
;
3687 targetCapacity
=(int32_t)(pArgs
->targetLimit
-pArgs
->target
);
3688 offsets
=pArgs
->offsets
;
3690 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
3691 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
3692 results
=(uint16_t *)cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
3694 results
=(uint16_t *)cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
3696 asciiRoundtrips
=cnv
->sharedData
->mbcs
.asciiRoundtrips
;
3698 if(cnv
->useFallback
) {
3699 /* use all roundtrip and fallback results */
3702 /* use only roundtrips and fallbacks from private-use characters */
3706 /* get the converter state from UConverter */
3709 /* sourceIndex=-1 if the current character began in the previous buffer */
3710 sourceIndex
= c
==0 ? 0 : -1;
3714 * since the conversion here is 1:1 UChar:uint8_t, we need only one counter
3715 * for the minimum of the sourceLength and targetCapacity
3717 length
=(int32_t)(sourceLimit
-source
);
3718 if(length
<targetCapacity
) {
3719 targetCapacity
=length
;
3722 /* conversion loop */
3723 if(c
!=0 && targetCapacity
>0) {
3727 #if MBCS_UNROLL_SINGLE_FROM_BMP
3728 /* unrolling makes it slower on Pentium III/Windows 2000?! */
3729 /* unroll the loop with the most common case */
3731 if(targetCapacity
>=4) {
3732 int32_t count
, loops
;
3733 uint16_t andedValues
;
3735 loops
=count
=targetCapacity
>>2;
3738 andedValues
=value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3739 *target
++=(uint8_t)value
;
3741 andedValues
&=value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3742 *target
++=(uint8_t)value
;
3744 andedValues
&=value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3745 *target
++=(uint8_t)value
;
3747 andedValues
&=value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3748 *target
++=(uint8_t)value
;
3750 /* were all 4 entries really valid? */
3751 if(andedValues
<minValue
) {
3752 /* no, return to the first of these 4 */
3759 targetCapacity
-=4*count
;
3762 lastSource
+=4*count
;
3764 *offsets
++=sourceIndex
++;
3765 *offsets
++=sourceIndex
++;
3766 *offsets
++=sourceIndex
++;
3767 *offsets
++=sourceIndex
++;
3776 while(targetCapacity
>0) {
3778 * Get a correct Unicode code point:
3779 * a single UChar for a BMP code point or
3780 * a matched surrogate pair for a "supplementary code point".
3784 * Do not immediately check for single surrogates:
3785 * Assume that they are unassigned and check for them in that case.
3786 * This speeds up the conversion of assigned characters.
3788 /* convert the Unicode code point in c into codepage bytes */
3789 if(c
<=0x7f && IS_ASCII_ROUNDTRIP(c
, asciiRoundtrips
)) {
3790 *target
++=(uint8_t)c
;
3795 value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3796 /* is this code point assigned, or do we use fallbacks? */
3797 if(value
>=minValue
) {
3798 /* assigned, write the output character bytes from value and length */
3800 /* this is easy because we know that there is enough space */
3801 *target
++=(uint8_t)value
;
3804 /* normal end of conversion: prepare for a new character */
3807 } else if(!UTF_IS_SURROGATE(c
)) {
3808 /* normal, unassigned BMP character */
3809 } else if(UTF_IS_SURROGATE_FIRST(c
)) {
3811 if(source
<sourceLimit
) {
3812 /* test the following code unit */
3813 UChar trail
=*source
;
3814 if(UTF_IS_SECOND_SURROGATE(trail
)) {
3816 c
=UTF16_GET_PAIR_VALUE(c
, trail
);
3817 /* this codepage does not map supplementary code points */
3818 /* callback(unassigned) */
3820 /* this is an unmatched lead code unit (1st surrogate) */
3821 /* callback(illegal) */
3822 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3828 *pErrorCode
=U_TRUNCATED_CHAR_FOUND
;
3833 /* this is an unmatched trail code unit (2nd surrogate) */
3834 /* callback(illegal) */
3835 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3839 /* c does not have a mapping */
3841 /* get the number of code units for c to correctly advance sourceIndex */
3842 length
=U16_LENGTH(c
);
3844 /* set offsets since the start or the last extension */
3846 int32_t count
=(int32_t)(source
-lastSource
);
3848 /* do not set the offset for this character */
3852 *offsets
++=sourceIndex
++;
3855 /* offsets and sourceIndex are now set for the current character */
3858 /* try an extension mapping */
3860 c
=_extFromU(cnv
, cnv
->sharedData
,
3861 c
, &source
, sourceLimit
,
3862 &target
, (const uint8_t *)(pArgs
->targetLimit
),
3863 &offsets
, sourceIndex
,
3866 sourceIndex
+=length
+(int32_t)(source
-lastSource
);
3869 if(U_FAILURE(*pErrorCode
)) {
3870 /* not mappable or buffer overflow */
3873 /* a mapping was written to the target, continue */
3875 /* recalculate the targetCapacity after an extension mapping */
3876 targetCapacity
=(int32_t)(pArgs
->targetLimit
-(char *)target
);
3877 length
=(int32_t)(sourceLimit
-source
);
3878 if(length
<targetCapacity
) {
3879 targetCapacity
=length
;
3883 #if MBCS_UNROLL_SINGLE_FROM_BMP
3884 /* unrolling makes it slower on Pentium III/Windows 2000?! */
3889 if(U_SUCCESS(*pErrorCode
) && source
<sourceLimit
&& target
>=(uint8_t *)pArgs
->targetLimit
) {
3890 /* target is full */
3891 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
3894 /* set offsets since the start or the last callback */
3896 size_t count
=source
-lastSource
;
3897 if (count
> 0 && *pErrorCode
== U_TRUNCATED_CHAR_FOUND
) {
3899 Caller gave us a partial supplementary character,
3900 which this function couldn't convert in any case.
3901 The callback will handle the offset.
3906 *offsets
++=sourceIndex
++;
3911 /* set the converter state back into UConverter */
3914 /* write back the updated pointers */
3915 pArgs
->source
=source
;
3916 pArgs
->target
=(char *)target
;
3917 pArgs
->offsets
=offsets
;
3921 ucnv_MBCSFromUnicodeWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
3922 UErrorCode
*pErrorCode
) {
3924 const UChar
*source
, *sourceLimit
;
3926 int32_t targetCapacity
;
3929 const uint16_t *table
;
3930 const uint16_t *mbcsIndex
;
3931 const uint8_t *p
, *bytes
;
3936 int32_t prevSourceIndex
, sourceIndex
, nextSourceIndex
;
3938 uint32_t stage2Entry
;
3939 uint32_t asciiRoundtrips
;
3941 uint8_t si_value
[2] = {0, 0};
3942 uint8_t so_value
[2] = {0, 0};
3943 uint8_t si_value_length
, so_value_length
;
3944 int32_t length
= 0, prevLength
;
3945 uint8_t unicodeMask
;
3947 cnv
=pArgs
->converter
;
3949 if(cnv
->preFromUFirstCP
>=0) {
3951 * pass sourceIndex=-1 because we continue from an earlier buffer
3952 * in the future, this may change with continuous offsets
3954 ucnv_extContinueMatchFromU(cnv
, pArgs
, -1, pErrorCode
);
3956 if(U_FAILURE(*pErrorCode
) || cnv
->preFromULength
<0) {
3961 /* use optimized function if possible */
3962 outputType
=cnv
->sharedData
->mbcs
.outputType
;
3963 unicodeMask
=cnv
->sharedData
->mbcs
.unicodeMask
;
3964 if(outputType
==MBCS_OUTPUT_1
&& !(unicodeMask
&UCNV_HAS_SURROGATES
)) {
3965 if(!(unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
3966 ucnv_MBCSSingleFromBMPWithOffsets(pArgs
, pErrorCode
);
3968 ucnv_MBCSSingleFromUnicodeWithOffsets(pArgs
, pErrorCode
);
3971 } else if(outputType
==MBCS_OUTPUT_2
&& cnv
->sharedData
->mbcs
.utf8Friendly
) {
3972 ucnv_MBCSDoubleFromUnicodeWithOffsets(pArgs
, pErrorCode
);
3976 /* set up the local pointers */
3977 source
=pArgs
->source
;
3978 sourceLimit
=pArgs
->sourceLimit
;
3979 target
=(uint8_t *)pArgs
->target
;
3980 targetCapacity
=(int32_t)(pArgs
->targetLimit
-pArgs
->target
);
3981 offsets
=pArgs
->offsets
;
3983 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
3984 if(cnv
->sharedData
->mbcs
.utf8Friendly
) {
3985 mbcsIndex
=cnv
->sharedData
->mbcs
.mbcsIndex
;
3989 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
3990 bytes
=cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
3992 bytes
=cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
3994 asciiRoundtrips
=cnv
->sharedData
->mbcs
.asciiRoundtrips
;
3996 /* get the converter state from UConverter */
3999 if(outputType
==MBCS_OUTPUT_2_SISO
) {
4000 prevLength
=cnv
->fromUnicodeStatus
;
4002 /* set the real value */
4006 /* prevent fromUnicodeStatus from being set to something non-0 */
4010 /* sourceIndex=-1 if the current character began in the previous buffer */
4012 sourceIndex
= c
==0 ? 0 : -1;
4015 /* Get the SI/SO character for the converter */
4016 si_value_length
= getSISOBytes(SI
, cnv
->options
, si_value
);
4017 so_value_length
= getSISOBytes(SO
, cnv
->options
, so_value
);
4019 /* conversion loop */
4021 * This is another piece of ugly code:
4022 * A goto into the loop if the converter state contains a first surrogate
4023 * from the previous function call.
4024 * It saves me to check in each loop iteration a check of if(c==0)
4025 * and duplicating the trail-surrogate-handling code in the else
4026 * branch of that check.
4027 * I could not find any other way to get around this other than
4028 * using a function call for the conversion and callback, which would
4029 * be even more inefficient.
4031 * Markus Scherer 2000-jul-19
4033 if(c
!=0 && targetCapacity
>0) {
4037 while(source
<sourceLimit
) {
4039 * This following test is to see if available input would overflow the output.
4040 * It does not catch output of more than one byte that
4041 * overflows as a result of a multi-byte character or callback output
4042 * from the last source character.
4043 * Therefore, those situations also test for overflows and will
4044 * then break the loop, too.
4046 if(targetCapacity
>0) {
4048 * Get a correct Unicode code point:
4049 * a single UChar for a BMP code point or
4050 * a matched surrogate pair for a "supplementary code point".
4054 if(c
<=0x7f && IS_ASCII_ROUNDTRIP(c
, asciiRoundtrips
)) {
4055 *target
++=(uint8_t)c
;
4057 *offsets
++=sourceIndex
;
4058 prevSourceIndex
=sourceIndex
;
4059 sourceIndex
=nextSourceIndex
;
4066 * utf8Friendly table: Test for <=0xd7ff rather than <=MBCS_FAST_MAX
4067 * to avoid dealing with surrogates.
4068 * MBCS_FAST_MAX must be >=0xd7ff.
4070 if(c
<=0xd7ff && mbcsIndex
!=NULL
) {
4071 value
=mbcsIndex
[c
>>6];
4073 /* get the bytes and the length for the output (copied from below and adapted for utf8Friendly data) */
4074 /* There are only roundtrips (!=0) and no-mapping (==0) entries. */
4075 switch(outputType
) {
4077 value
=((const uint16_t *)bytes
)[value
+(c
&0x3f)];
4088 case MBCS_OUTPUT_2_SISO
:
4089 /* 1/2-byte stateful with Shift-In/Shift-Out */
4091 * Save the old state in the converter object
4092 * right here, then change the local prevLength state variable if necessary.
4093 * Then, if this character turns out to be unassigned or a fallback that
4094 * is not taken, the callback code must not save the new state in the converter
4095 * because the new state is for a character that is not output.
4096 * However, the callback must still restore the state from the converter
4097 * in case the callback function changed it for its output.
4099 cnv
->fromUnicodeStatus
=prevLength
; /* save the old state */
4100 value
=((const uint16_t *)bytes
)[value
+(c
&0x3f)];
4104 } else if(prevLength
<=1) {
4107 /* change from double-byte mode to single-byte */
4108 if (si_value_length
== 1) {
4109 value
|=(uint32_t)si_value
[0]<<8;
4111 } else if (si_value_length
== 2) {
4112 value
|=(uint32_t)si_value
[1]<<8;
4113 value
|=(uint32_t)si_value
[0]<<16;
4122 /* change from single-byte mode to double-byte */
4123 if (so_value_length
== 1) {
4124 value
|=(uint32_t)so_value
[0]<<16;
4126 } else if (so_value_length
== 2) {
4127 value
|=(uint32_t)so_value
[1]<<16;
4128 value
|=(uint32_t)so_value
[0]<<24;
4135 case MBCS_OUTPUT_DBCS_ONLY
:
4136 /* table with single-byte results, but only DBCS mappings used */
4137 value
=((const uint16_t *)bytes
)[value
+(c
&0x3f)];
4139 /* no mapping or SBCS result, not taken for DBCS-only */
4146 p
=bytes
+(value
+(c
&0x3f))*3;
4147 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4154 } else if(value
<=0xffff) {
4161 value
=((const uint32_t *)bytes
)[value
+(c
&0x3f)];
4168 } else if(value
<=0xffff) {
4170 } else if(value
<=0xffffff) {
4176 case MBCS_OUTPUT_3_EUC
:
4177 value
=((const uint16_t *)bytes
)[value
+(c
&0x3f)];
4178 /* EUC 16-bit fixed-length representation */
4185 } else if((value
&0x8000)==0) {
4188 } else if((value
&0x80)==0) {
4195 case MBCS_OUTPUT_4_EUC
:
4196 p
=bytes
+(value
+(c
&0x3f))*3;
4197 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4198 /* EUC 16-bit fixed-length representation applied to the first two bytes */
4205 } else if(value
<=0xffff) {
4207 } else if((value
&0x800000)==0) {
4210 } else if((value
&0x8000)==0) {
4218 /* must not occur */
4220 * To avoid compiler warnings that value & length may be
4221 * used without having been initialized, we set them here.
4222 * In reality, this is unreachable code.
4223 * Not having a default branch also causes warnings with
4230 /* output the value */
4233 * This also tests if the codepage maps single surrogates.
4234 * If it does, then surrogates are not paired but mapped separately.
4235 * Note that in this case unmatched surrogates are not detected.
4237 if(UTF_IS_SURROGATE(c
) && !(unicodeMask
&UCNV_HAS_SURROGATES
)) {
4238 if(UTF_IS_SURROGATE_FIRST(c
)) {
4240 if(source
<sourceLimit
) {
4241 /* test the following code unit */
4242 UChar trail
=*source
;
4243 if(UTF_IS_SECOND_SURROGATE(trail
)) {
4246 c
=UTF16_GET_PAIR_VALUE(c
, trail
);
4247 if(!(unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
4248 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
4249 cnv
->fromUnicodeStatus
=prevLength
; /* save the old state */
4250 /* callback(unassigned) */
4253 /* convert this supplementary code point */
4254 /* exit this condition tree */
4256 /* this is an unmatched lead code unit (1st surrogate) */
4257 /* callback(illegal) */
4258 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
4266 /* this is an unmatched trail code unit (2nd surrogate) */
4267 /* callback(illegal) */
4268 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
4273 /* convert the Unicode code point in c into codepage bytes */
4276 * The basic lookup is a triple-stage compact array (trie) lookup.
4277 * For details see the beginning of this file.
4279 * Single-byte codepages are handled with a different data structure
4280 * by _MBCSSingle... functions.
4282 * The result consists of a 32-bit value from stage 2 and
4283 * a pointer to as many bytes as are stored per character.
4284 * The pointer points to the character's bytes in stage 3.
4285 * Bits 15..0 of the stage 2 entry contain the stage 3 index
4286 * for that pointer, while bits 31..16 are flags for which of
4287 * the 16 characters in the block are roundtrip-assigned.
4289 * For 2-byte and 4-byte codepages, the bytes are stored as uint16_t
4290 * respectively as uint32_t, in the platform encoding.
4291 * For 3-byte codepages, the bytes are always stored in big-endian order.
4293 * For EUC encodings that use only either 0x8e or 0x8f as the first
4294 * byte of their longest byte sequences, the first two bytes in
4295 * this third stage indicate with their 7th bits whether these bytes
4296 * are to be written directly or actually need to be preceeded by
4297 * one of the two Single-Shift codes. With this, the third stage
4298 * stores one byte fewer per character than the actual maximum length of
4299 * EUC byte sequences.
4301 * Other than that, leading zero bytes are removed and the other
4302 * bytes output. A single zero byte may be output if the "assigned"
4303 * bit in stage 2 was on.
4304 * The data structure does not support zero byte output as a fallback,
4305 * and also does not allow output of leading zeros.
4307 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
4309 /* get the bytes and the length for the output */
4310 switch(outputType
) {
4312 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4319 case MBCS_OUTPUT_2_SISO
:
4320 /* 1/2-byte stateful with Shift-In/Shift-Out */
4322 * Save the old state in the converter object
4323 * right here, then change the local prevLength state variable if necessary.
4324 * Then, if this character turns out to be unassigned or a fallback that
4325 * is not taken, the callback code must not save the new state in the converter
4326 * because the new state is for a character that is not output.
4327 * However, the callback must still restore the state from the converter
4328 * in case the callback function changed it for its output.
4330 cnv
->fromUnicodeStatus
=prevLength
; /* save the old state */
4331 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4333 if(value
==0 && MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
)==0) {
4334 /* no mapping, leave value==0 */
4336 } else if(prevLength
<=1) {
4339 /* change from double-byte mode to single-byte */
4340 if (si_value_length
== 1) {
4341 value
|=(uint32_t)si_value
[0]<<8;
4343 } else if (si_value_length
== 2) {
4344 value
|=(uint32_t)si_value
[1]<<8;
4345 value
|=(uint32_t)si_value
[0]<<16;
4354 /* change from single-byte mode to double-byte */
4355 if (so_value_length
== 1) {
4356 value
|=(uint32_t)so_value
[0]<<16;
4358 } else if (so_value_length
== 2) {
4359 value
|=(uint32_t)so_value
[1]<<16;
4360 value
|=(uint32_t)so_value
[0]<<24;
4367 case MBCS_OUTPUT_DBCS_ONLY
:
4368 /* table with single-byte results, but only DBCS mappings used */
4369 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4371 /* no mapping or SBCS result, not taken for DBCS-only */
4372 value
=stage2Entry
=0; /* stage2Entry=0 to reset roundtrip flags */
4379 p
=MBCS_POINTER_3_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4380 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4383 } else if(value
<=0xffff) {
4390 value
=MBCS_VALUE_4_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4393 } else if(value
<=0xffff) {
4395 } else if(value
<=0xffffff) {
4401 case MBCS_OUTPUT_3_EUC
:
4402 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4403 /* EUC 16-bit fixed-length representation */
4406 } else if((value
&0x8000)==0) {
4409 } else if((value
&0x80)==0) {
4416 case MBCS_OUTPUT_4_EUC
:
4417 p
=MBCS_POINTER_3_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4418 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4419 /* EUC 16-bit fixed-length representation applied to the first two bytes */
4422 } else if(value
<=0xffff) {
4424 } else if((value
&0x800000)==0) {
4427 } else if((value
&0x8000)==0) {
4435 /* must not occur */
4437 * To avoid compiler warnings that value & length may be
4438 * used without having been initialized, we set them here.
4439 * In reality, this is unreachable code.
4440 * Not having a default branch also causes warnings with
4443 value
=stage2Entry
=0; /* stage2Entry=0 to reset roundtrip flags */
4448 /* is this code point assigned, or do we use fallbacks? */
4449 if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
)!=0 ||
4450 (UCNV_FROM_U_USE_FALLBACK(cnv
, c
) && value
!=0))
4453 * We allow a 0 byte output if the "assigned" bit is set for this entry.
4454 * There is no way with this data structure for fallback output
4455 * to be a zero byte.
4459 /* try an extension mapping */
4460 pArgs
->source
=source
;
4461 c
=_extFromU(cnv
, cnv
->sharedData
,
4462 c
, &source
, sourceLimit
,
4463 &target
, target
+targetCapacity
,
4464 &offsets
, sourceIndex
,
4467 nextSourceIndex
+=(int32_t)(source
-pArgs
->source
);
4468 prevLength
=cnv
->fromUnicodeStatus
; /* restore SISO state */
4470 if(U_FAILURE(*pErrorCode
)) {
4471 /* not mappable or buffer overflow */
4474 /* a mapping was written to the target, continue */
4476 /* recalculate the targetCapacity after an extension mapping */
4477 targetCapacity
=(int32_t)(pArgs
->targetLimit
-(char *)target
);
4479 /* normal end of conversion: prepare for a new character */
4481 prevSourceIndex
=sourceIndex
;
4482 sourceIndex
=nextSourceIndex
;
4489 /* write the output character bytes from value and length */
4490 /* from the first if in the loop we know that targetCapacity>0 */
4491 if(length
<=targetCapacity
) {
4494 /* each branch falls through to the next one */
4496 *target
++=(uint8_t)(value
>>24);
4498 *target
++=(uint8_t)(value
>>16);
4500 *target
++=(uint8_t)(value
>>8);
4502 *target
++=(uint8_t)value
;
4504 /* will never occur */
4509 /* each branch falls through to the next one */
4511 *target
++=(uint8_t)(value
>>24);
4512 *offsets
++=sourceIndex
;
4514 *target
++=(uint8_t)(value
>>16);
4515 *offsets
++=sourceIndex
;
4517 *target
++=(uint8_t)(value
>>8);
4518 *offsets
++=sourceIndex
;
4520 *target
++=(uint8_t)value
;
4521 *offsets
++=sourceIndex
;
4523 /* will never occur */
4527 targetCapacity
-=length
;
4529 uint8_t *charErrorBuffer
;
4532 * We actually do this backwards here:
4533 * In order to save an intermediate variable, we output
4534 * first to the overflow buffer what does not fit into the
4537 /* we know that 1<=targetCapacity<length<=4 */
4538 length
-=targetCapacity
;
4539 charErrorBuffer
=(uint8_t *)cnv
->charErrorBuffer
;
4541 /* each branch falls through to the next one */
4543 *charErrorBuffer
++=(uint8_t)(value
>>16);
4545 *charErrorBuffer
++=(uint8_t)(value
>>8);
4547 *charErrorBuffer
=(uint8_t)value
;
4549 /* will never occur */
4552 cnv
->charErrorBufferLength
=(int8_t)length
;
4554 /* now output what fits into the regular target */
4555 value
>>=8*length
; /* length was reduced by targetCapacity */
4556 switch(targetCapacity
) {
4557 /* each branch falls through to the next one */
4559 *target
++=(uint8_t)(value
>>16);
4561 *offsets
++=sourceIndex
;
4564 *target
++=(uint8_t)(value
>>8);
4566 *offsets
++=sourceIndex
;
4569 *target
++=(uint8_t)value
;
4571 *offsets
++=sourceIndex
;
4574 /* will never occur */
4578 /* target overflow */
4580 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
4585 /* normal end of conversion: prepare for a new character */
4588 prevSourceIndex
=sourceIndex
;
4589 sourceIndex
=nextSourceIndex
;
4593 /* target is full */
4594 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
4600 * the end of the input stream and detection of truncated input
4601 * are handled by the framework, but for EBCDIC_STATEFUL conversion
4602 * we need to emit an SI at the very end
4606 * EBCDIC_STATEFUL in DBCS mode
4607 * end of input and no truncated input
4609 if( U_SUCCESS(*pErrorCode
) &&
4610 outputType
==MBCS_OUTPUT_2_SISO
&& prevLength
==2 &&
4611 pArgs
->flush
&& source
>=sourceLimit
&& c
==0
4613 /* EBCDIC_STATEFUL ending with DBCS: emit an SI to return the output stream to SBCS */
4614 if(targetCapacity
>0) {
4615 *target
++=(uint8_t)si_value
[0];
4616 if (si_value_length
== 2) {
4617 if (targetCapacity
<2) {
4618 cnv
->charErrorBuffer
[0]=(uint8_t)si_value
[1];
4619 cnv
->charErrorBufferLength
=1;
4620 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
4622 *target
++=(uint8_t)si_value
[1];
4626 /* set the last source character's index (sourceIndex points at sourceLimit now) */
4627 *offsets
++=prevSourceIndex
;
4630 /* target is full */
4631 cnv
->charErrorBuffer
[0]=(uint8_t)si_value
[0];
4632 if (si_value_length
== 2) {
4633 cnv
->charErrorBuffer
[1]=(uint8_t)si_value
[1];
4635 cnv
->charErrorBufferLength
=si_value_length
;
4636 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
4638 prevLength
=1; /* we switched into SBCS */
4641 /* set the converter state back into UConverter */
4643 cnv
->fromUnicodeStatus
=prevLength
;
4645 /* write back the updated pointers */
4646 pArgs
->source
=source
;
4647 pArgs
->target
=(char *)target
;
4648 pArgs
->offsets
=offsets
;
4652 * This is another simple conversion function for internal use by other
4653 * conversion implementations.
4654 * It does not use the converter state nor call callbacks.
4655 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
4656 * It handles conversion extensions but not GB 18030.
4658 * It converts one single Unicode code point into codepage bytes, encoded
4659 * as one 32-bit value. The function returns the number of bytes in *pValue:
4660 * 1..4 the number of bytes in *pValue
4661 * 0 unassigned (*pValue undefined)
4662 * -1 illegal (currently not used, *pValue undefined)
4664 * *pValue will contain the resulting bytes with the last byte in bits 7..0,
4665 * the second to last byte in bits 15..8, etc.
4666 * Currently, the function assumes but does not check that 0<=c<=0x10ffff.
4669 ucnv_MBCSFromUChar32(UConverterSharedData
*sharedData
,
4670 UChar32 c
, uint32_t *pValue
,
4671 UBool useFallback
) {
4673 const uint16_t *table
;
4675 /* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */
4678 uint32_t stage2Entry
;
4682 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
4683 if(c
<=0xffff || (sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
4684 table
=sharedData
->mbcs
.fromUnicodeTable
;
4686 /* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
4687 if(sharedData
->mbcs
.outputType
==MBCS_OUTPUT_1
) {
4688 value
=MBCS_SINGLE_RESULT_FROM_U(table
, (uint16_t *)sharedData
->mbcs
.fromUnicodeBytes
, c
);
4689 /* is this code point assigned, or do we use fallbacks? */
4690 if(useFallback
? value
>=0x800 : value
>=0xc00) {
4694 } else /* outputType!=MBCS_OUTPUT_1 */ {
4695 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
4697 /* get the bytes and the length for the output */
4698 switch(sharedData
->mbcs
.outputType
) {
4700 value
=MBCS_VALUE_2_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4708 /* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */
4709 case MBCS_OUTPUT_DBCS_ONLY
:
4710 /* table with single-byte results, but only DBCS mappings used */
4711 value
=MBCS_VALUE_2_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4713 /* no mapping or SBCS result, not taken for DBCS-only */
4714 value
=stage2Entry
=0; /* stage2Entry=0 to reset roundtrip flags */
4721 p
=MBCS_POINTER_3_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4722 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4725 } else if(value
<=0xffff) {
4732 value
=MBCS_VALUE_4_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4735 } else if(value
<=0xffff) {
4737 } else if(value
<=0xffffff) {
4743 case MBCS_OUTPUT_3_EUC
:
4744 value
=MBCS_VALUE_2_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4745 /* EUC 16-bit fixed-length representation */
4748 } else if((value
&0x8000)==0) {
4751 } else if((value
&0x80)==0) {
4758 case MBCS_OUTPUT_4_EUC
:
4759 p
=MBCS_POINTER_3_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4760 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4761 /* EUC 16-bit fixed-length representation applied to the first two bytes */
4764 } else if(value
<=0xffff) {
4766 } else if((value
&0x800000)==0) {
4769 } else if((value
&0x8000)==0) {
4778 /* must not occur */
4782 /* is this code point assigned, or do we use fallbacks? */
4783 if( MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
) ||
4784 (FROM_U_USE_FALLBACK(useFallback
, c
) && value
!=0)
4787 * We allow a 0 byte output if the "assigned" bit is set for this entry.
4788 * There is no way with this data structure for fallback output
4789 * to be a zero byte.
4798 cx
=sharedData
->mbcs
.extIndexes
;
4800 length
=ucnv_extSimpleMatchFromU(cx
, c
, pValue
, useFallback
);
4801 return length
>=0 ? length
: -length
; /* return abs(length); */
4811 * This function has been moved to ucnv2022.c for inlining.
4812 * This implementation is here only for documentation purposes
4816 * This version of ucnv_MBCSFromUChar32() is optimized for single-byte codepages.
4817 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
4818 * It does not handle conversion extensions (_extFromU()).
4820 * It returns the codepage byte for the code point, or -1 if it is unassigned.
4823 ucnv_MBCSSingleFromUChar32(UConverterSharedData
*sharedData
,
4825 UBool useFallback
) {
4826 const uint16_t *table
;
4829 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
4830 if(c
>=0x10000 && !(sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
4834 /* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
4835 table
=sharedData
->mbcs
.fromUnicodeTable
;
4837 /* get the byte for the output */
4838 value
=MBCS_SINGLE_RESULT_FROM_U(table
, (uint16_t *)sharedData
->mbcs
.fromUnicodeBytes
, c
);
4839 /* is this code point assigned, or do we use fallbacks? */
4840 if(useFallback
? value
>=0x800 : value
>=0xc00) {
4848 /* MBCS-from-UTF-8 conversion functions ------------------------------------- */
4850 /* minimum code point values for n-byte UTF-8 sequences, n=0..4 */
4851 static const UChar32
4852 utf8_minLegal
[5]={ 0, 0, 0x80, 0x800, 0x10000 };
4854 /* offsets for n-byte UTF-8 sequences that were calculated with ((lead<<6)+trail)<<6+trail... */
4855 static const UChar32
4856 utf8_offsets
[7]={ 0, 0, 0x3080, 0xE2080, 0x3C82080 };
4859 ucnv_SBCSFromUTF8(UConverterFromUnicodeArgs
*pFromUArgs
,
4860 UConverterToUnicodeArgs
*pToUArgs
,
4861 UErrorCode
*pErrorCode
) {
4862 UConverter
*utf8
, *cnv
;
4863 const uint8_t *source
, *sourceLimit
;
4865 int32_t targetCapacity
;
4867 const uint16_t *table
, *sbcsIndex
;
4868 const uint16_t *results
;
4870 int8_t oldToULength
, toULength
, toULimit
;
4875 uint32_t asciiRoundtrips
;
4876 uint16_t value
, minValue
;
4877 UBool hasSupplementary
;
4879 /* set up the local pointers */
4880 utf8
=pToUArgs
->converter
;
4881 cnv
=pFromUArgs
->converter
;
4882 source
=(uint8_t *)pToUArgs
->source
;
4883 sourceLimit
=(uint8_t *)pToUArgs
->sourceLimit
;
4884 target
=(uint8_t *)pFromUArgs
->target
;
4885 targetCapacity
=(int32_t)(pFromUArgs
->targetLimit
-pFromUArgs
->target
);
4887 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
4888 sbcsIndex
=cnv
->sharedData
->mbcs
.sbcsIndex
;
4889 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
4890 results
=(uint16_t *)cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
4892 results
=(uint16_t *)cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
4894 asciiRoundtrips
=cnv
->sharedData
->mbcs
.asciiRoundtrips
;
4896 if(cnv
->useFallback
) {
4897 /* use all roundtrip and fallback results */
4900 /* use only roundtrips and fallbacks from private-use characters */
4903 hasSupplementary
=(UBool
)(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
);
4905 /* get the converter state from the UTF-8 UConverter */
4906 c
=(UChar32
)utf8
->toUnicodeStatus
;
4908 toULength
=oldToULength
=utf8
->toULength
;
4909 toULimit
=(int8_t)utf8
->mode
;
4911 toULength
=oldToULength
=toULimit
=0;
4915 * Make sure that the last byte sequence before sourceLimit is complete
4916 * or runs into a lead byte.
4917 * Do not go back into the bytes that will be read for finishing a partial
4918 * sequence from the previous buffer.
4919 * In the conversion loop compare source with sourceLimit only once
4920 * per multi-byte character.
4925 length
=(int32_t)(sourceLimit
-source
) - (toULimit
-oldToULength
);
4926 for(i
=0; i
<3 && i
<length
;) {
4927 b
=*(sourceLimit
-i
-1);
4928 if(U8_IS_TRAIL(b
)) {
4931 if(i
<utf8_countTrailBytes
[b
]) {
4932 /* exit the conversion loop before the lead byte if there are not enough trail bytes for it */
4940 if(c
!=0 && targetCapacity
>0) {
4941 utf8
->toUnicodeStatus
=0;
4945 * Note: We could avoid the goto by duplicating some of the moreBytes
4946 * code, but only up to the point of collecting a complete UTF-8
4947 * sequence; then recurse for the toUBytes[toULength]
4948 * and then continue with normal conversion.
4950 * If so, move this code to just after initializing the minimum
4951 * set of local variables for reading the UTF-8 input
4952 * (utf8, source, target, limits but not cnv, table, minValue, etc.).
4954 * Potential advantages:
4956 * - oldToULength could become a local variable in just those code blocks
4957 * that deal with buffer boundaries
4958 * - possibly faster if the goto prevents some compiler optimizations
4959 * (this would need measuring to confirm)
4961 * - code duplication
4965 /* conversion loop */
4966 while(source
<sourceLimit
) {
4967 if(targetCapacity
>0) {
4971 if(IS_ASCII_ROUNDTRIP(b
, asciiRoundtrips
)) {
4972 *target
++=(uint8_t)b
;
4977 value
=SBCS_RESULT_FROM_UTF8(sbcsIndex
, results
, 0, c
);
4981 if( /* handle U+0080..U+07FF inline */
4983 (t1
=(uint8_t)(*source
-0x80)) <= 0x3f
4987 value
=SBCS_RESULT_FROM_UTF8(sbcsIndex
, results
, c
, t1
);
4988 if(value
>=minValue
) {
4989 *target
++=(uint8_t)value
;
4998 } else if(b
==0xe0) {
4999 if( /* handle U+0800..U+0FFF inline */
5000 (t1
=(uint8_t)(source
[0]-0x80)) <= 0x3f && t1
>= 0x20 &&
5001 (t2
=(uint8_t)(source
[1]-0x80)) <= 0x3f
5005 value
=SBCS_RESULT_FROM_UTF8(sbcsIndex
, results
, c
, t2
);
5006 if(value
>=minValue
) {
5007 *target
++=(uint8_t)value
;
5021 /* handle "complicated" and error cases, and continuing partial characters */
5024 toULimit
=utf8_countTrailBytes
[b
]+1;
5027 while(toULength
<toULimit
) {
5029 * The sourceLimit may have been adjusted before the conversion loop
5030 * to stop before a truncated sequence.
5031 * Here we need to use the real limit in case we have two truncated
5032 * sequences at the end.
5035 if(source
<(uint8_t *)pToUArgs
->sourceLimit
) {
5037 if(U8_IS_TRAIL(b
)) {
5042 break; /* sequence too short, stop with toULength<toULimit */
5045 /* store the partial UTF-8 character, compatible with the regular UTF-8 converter */
5046 source
-=(toULength
-oldToULength
);
5047 while(oldToULength
<toULength
) {
5048 utf8
->toUBytes
[oldToULength
++]=*source
++;
5050 utf8
->toUnicodeStatus
=c
;
5051 utf8
->toULength
=toULength
;
5052 utf8
->mode
=toULimit
;
5053 pToUArgs
->source
=(char *)source
;
5054 pFromUArgs
->target
=(char *)target
;
5059 if( toULength
==toULimit
&& /* consumed all trail bytes */
5060 (toULength
==3 || toULength
==2) && /* BMP */
5061 (c
-=utf8_offsets
[toULength
])>=utf8_minLegal
[toULength
] &&
5062 (c
<=0xd7ff || 0xe000<=c
) /* not a surrogate */
5064 value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
5066 toULength
==toULimit
&& toULength
==4 &&
5067 (0x10000<=(c
-=utf8_offsets
[4]) && c
<=0x10ffff)
5069 /* supplementary code point */
5070 if(!hasSupplementary
) {
5071 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
5074 value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
5077 /* error handling: illegal UTF-8 byte sequence */
5078 source
-=(toULength
-oldToULength
);
5079 while(oldToULength
<toULength
) {
5080 utf8
->toUBytes
[oldToULength
++]=*source
++;
5082 utf8
->toULength
=toULength
;
5083 pToUArgs
->source
=(char *)source
;
5084 pFromUArgs
->target
=(char *)target
;
5085 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
5091 if(value
>=minValue
) {
5092 /* output the mapping for c */
5093 *target
++=(uint8_t)value
;
5096 /* value<minValue means c is unassigned (unmappable) */
5098 * Try an extension mapping.
5099 * Pass in no source because we don't have UTF-16 input.
5100 * If we have a partial match on c, we will return and revert
5101 * to UTF-8->UTF-16->charset conversion.
5103 static const UChar nul
=0;
5104 const UChar
*noSource
=&nul
;
5105 c
=_extFromU(cnv
, cnv
->sharedData
,
5106 c
, &noSource
, noSource
,
5107 &target
, target
+targetCapacity
,
5112 if(U_FAILURE(*pErrorCode
)) {
5113 /* not mappable or buffer overflow */
5116 } else if(cnv
->preFromUFirstCP
>=0) {
5118 * Partial match, return and revert to pivoting.
5119 * In normal from-UTF-16 conversion, we would just continue
5120 * but then exit the loop because the extension match would
5121 * have consumed the source.
5125 /* a mapping was written to the target, continue */
5127 /* recalculate the targetCapacity after an extension mapping */
5128 targetCapacity
=(int32_t)(pFromUArgs
->targetLimit
-(char *)target
);
5132 /* target is full */
5133 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
5139 * The sourceLimit may have been adjusted before the conversion loop
5140 * to stop before a truncated sequence.
5141 * If so, then collect the truncated sequence now.
5143 if(U_SUCCESS(*pErrorCode
) && source
<(sourceLimit
=(uint8_t *)pToUArgs
->sourceLimit
)) {
5144 c
=utf8
->toUBytes
[0]=b
=*source
++;
5146 toULimit
=utf8_countTrailBytes
[b
]+1;
5147 while(source
<sourceLimit
) {
5148 utf8
->toUBytes
[toULength
++]=b
=*source
++;
5151 utf8
->toUnicodeStatus
=c
;
5152 utf8
->toULength
=toULength
;
5153 utf8
->mode
=toULimit
;
5156 /* write back the updated pointers */
5157 pToUArgs
->source
=(char *)source
;
5158 pFromUArgs
->target
=(char *)target
;
5162 ucnv_DBCSFromUTF8(UConverterFromUnicodeArgs
*pFromUArgs
,
5163 UConverterToUnicodeArgs
*pToUArgs
,
5164 UErrorCode
*pErrorCode
) {
5165 UConverter
*utf8
, *cnv
;
5166 const uint8_t *source
, *sourceLimit
;
5168 int32_t targetCapacity
;
5170 const uint16_t *table
, *mbcsIndex
;
5171 const uint16_t *results
;
5173 int8_t oldToULength
, toULength
, toULimit
;
5178 uint32_t stage2Entry
;
5179 uint32_t asciiRoundtrips
;
5180 uint16_t value
, minValue
;
5181 UBool hasSupplementary
;
5183 /* set up the local pointers */
5184 utf8
=pToUArgs
->converter
;
5185 cnv
=pFromUArgs
->converter
;
5186 source
=(uint8_t *)pToUArgs
->source
;
5187 sourceLimit
=(uint8_t *)pToUArgs
->sourceLimit
;
5188 target
=(uint8_t *)pFromUArgs
->target
;
5189 targetCapacity
=(int32_t)(pFromUArgs
->targetLimit
-pFromUArgs
->target
);
5191 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
5192 mbcsIndex
=cnv
->sharedData
->mbcs
.mbcsIndex
;
5193 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
5194 results
=(uint16_t *)cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
5196 results
=(uint16_t *)cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
5198 asciiRoundtrips
=cnv
->sharedData
->mbcs
.asciiRoundtrips
;
5200 if(cnv
->useFallback
) {
5201 /* use all roundtrip and fallback results */
5204 /* use only roundtrips and fallbacks from private-use characters */
5207 hasSupplementary
=(UBool
)(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
);
5209 /* get the converter state from the UTF-8 UConverter */
5210 c
=(UChar32
)utf8
->toUnicodeStatus
;
5212 toULength
=oldToULength
=utf8
->toULength
;
5213 toULimit
=(int8_t)utf8
->mode
;
5215 toULength
=oldToULength
=toULimit
=0;
5219 * Make sure that the last byte sequence before sourceLimit is complete
5220 * or runs into a lead byte.
5221 * Do not go back into the bytes that will be read for finishing a partial
5222 * sequence from the previous buffer.
5223 * In the conversion loop compare source with sourceLimit only once
5224 * per multi-byte character.
5229 length
=(int32_t)(sourceLimit
-source
) - (toULimit
-oldToULength
);
5230 for(i
=0; i
<3 && i
<length
;) {
5231 b
=*(sourceLimit
-i
-1);
5232 if(U8_IS_TRAIL(b
)) {
5235 if(i
<utf8_countTrailBytes
[b
]) {
5236 /* exit the conversion loop before the lead byte if there are not enough trail bytes for it */
5244 if(c
!=0 && targetCapacity
>0) {
5245 utf8
->toUnicodeStatus
=0;
5248 /* See note in ucnv_SBCSFromUTF8() about this goto. */
5251 /* conversion loop */
5252 while(source
<sourceLimit
) {
5253 if(targetCapacity
>0) {
5257 if(IS_ASCII_ROUNDTRIP(b
, asciiRoundtrips
)) {
5262 value
=DBCS_RESULT_FROM_UTF8(mbcsIndex
, results
, 0, b
);
5270 if( /* handle U+1000..U+D7FF inline */
5271 (((t1
=(uint8_t)(source
[0]-0x80), b
<0xed) && (t1
<= 0x3f)) ||
5272 (b
==0xed && (t1
<= 0x1f))) &&
5273 (t2
=(uint8_t)(source
[1]-0x80)) <= 0x3f
5277 value
=DBCS_RESULT_FROM_UTF8(mbcsIndex
, results
, c
, t2
);
5286 if( /* handle U+0080..U+07FF inline */
5288 (t1
=(uint8_t)(*source
-0x80)) <= 0x3f
5292 value
=DBCS_RESULT_FROM_UTF8(mbcsIndex
, results
, c
, t1
);
5305 /* handle "complicated" and error cases, and continuing partial characters */
5308 toULimit
=utf8_countTrailBytes
[b
]+1;
5311 while(toULength
<toULimit
) {
5313 * The sourceLimit may have been adjusted before the conversion loop
5314 * to stop before a truncated sequence.
5315 * Here we need to use the real limit in case we have two truncated
5316 * sequences at the end.
5319 if(source
<(uint8_t *)pToUArgs
->sourceLimit
) {
5321 if(U8_IS_TRAIL(b
)) {
5326 break; /* sequence too short, stop with toULength<toULimit */
5329 /* store the partial UTF-8 character, compatible with the regular UTF-8 converter */
5330 source
-=(toULength
-oldToULength
);
5331 while(oldToULength
<toULength
) {
5332 utf8
->toUBytes
[oldToULength
++]=*source
++;
5334 utf8
->toUnicodeStatus
=c
;
5335 utf8
->toULength
=toULength
;
5336 utf8
->mode
=toULimit
;
5337 pToUArgs
->source
=(char *)source
;
5338 pFromUArgs
->target
=(char *)target
;
5343 if( toULength
==toULimit
&& /* consumed all trail bytes */
5344 (toULength
==3 || toULength
==2) && /* BMP */
5345 (c
-=utf8_offsets
[toULength
])>=utf8_minLegal
[toULength
] &&
5346 (c
<=0xd7ff || 0xe000<=c
) /* not a surrogate */
5348 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
5350 toULength
==toULimit
&& toULength
==4 &&
5351 (0x10000<=(c
-=utf8_offsets
[4]) && c
<=0x10ffff)
5353 /* supplementary code point */
5354 if(!hasSupplementary
) {
5355 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
5358 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
5361 /* error handling: illegal UTF-8 byte sequence */
5362 source
-=(toULength
-oldToULength
);
5363 while(oldToULength
<toULength
) {
5364 utf8
->toUBytes
[oldToULength
++]=*source
++;
5366 utf8
->toULength
=toULength
;
5367 pToUArgs
->source
=(char *)source
;
5368 pFromUArgs
->target
=(char *)target
;
5369 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
5373 /* get the bytes and the length for the output */
5375 value
=MBCS_VALUE_2_FROM_STAGE_2(results
, stage2Entry
, c
);
5377 /* is this code point assigned, or do we use fallbacks? */
5378 if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
) ||
5379 (UCNV_FROM_U_USE_FALLBACK(cnv
, c
) && value
!=0))
5386 /* write the output character bytes from value and length */
5387 /* from the first if in the loop we know that targetCapacity>0 */
5389 /* this is easy because we know that there is enough space */
5390 *target
++=(uint8_t)value
;
5392 } else /* length==2 */ {
5393 *target
++=(uint8_t)(value
>>8);
5394 if(2<=targetCapacity
) {
5395 *target
++=(uint8_t)value
;
5398 cnv
->charErrorBuffer
[0]=(char)value
;
5399 cnv
->charErrorBufferLength
=1;
5401 /* target overflow */
5402 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
5411 * Try an extension mapping.
5412 * Pass in no source because we don't have UTF-16 input.
5413 * If we have a partial match on c, we will return and revert
5414 * to UTF-8->UTF-16->charset conversion.
5416 static const UChar nul
=0;
5417 const UChar
*noSource
=&nul
;
5418 c
=_extFromU(cnv
, cnv
->sharedData
,
5419 c
, &noSource
, noSource
,
5420 &target
, target
+targetCapacity
,
5425 if(U_FAILURE(*pErrorCode
)) {
5426 /* not mappable or buffer overflow */
5429 } else if(cnv
->preFromUFirstCP
>=0) {
5431 * Partial match, return and revert to pivoting.
5432 * In normal from-UTF-16 conversion, we would just continue
5433 * but then exit the loop because the extension match would
5434 * have consumed the source.
5438 /* a mapping was written to the target, continue */
5440 /* recalculate the targetCapacity after an extension mapping */
5441 targetCapacity
=(int32_t)(pFromUArgs
->targetLimit
-(char *)target
);
5446 /* target is full */
5447 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
5453 * The sourceLimit may have been adjusted before the conversion loop
5454 * to stop before a truncated sequence.
5455 * If so, then collect the truncated sequence now.
5457 if(U_SUCCESS(*pErrorCode
) && source
<(sourceLimit
=(uint8_t *)pToUArgs
->sourceLimit
)) {
5458 c
=utf8
->toUBytes
[0]=b
=*source
++;
5460 toULimit
=utf8_countTrailBytes
[b
]+1;
5461 while(source
<sourceLimit
) {
5462 utf8
->toUBytes
[toULength
++]=b
=*source
++;
5465 utf8
->toUnicodeStatus
=c
;
5466 utf8
->toULength
=toULength
;
5467 utf8
->mode
=toULimit
;
5470 /* write back the updated pointers */
5471 pToUArgs
->source
=(char *)source
;
5472 pFromUArgs
->target
=(char *)target
;
5475 /* miscellaneous ------------------------------------------------------------ */
5478 ucnv_MBCSGetStarters(const UConverter
* cnv
,
5479 UBool starters
[256],
5480 UErrorCode
*pErrorCode
) {
5481 const int32_t *state0
;
5484 state0
=cnv
->sharedData
->mbcs
.stateTable
[cnv
->sharedData
->mbcs
.dbcsOnlyState
];
5485 for(i
=0; i
<256; ++i
) {
5486 /* all bytes that cause a state transition from state 0 are lead bytes */
5487 starters
[i
]= (UBool
)MBCS_ENTRY_IS_TRANSITION(state0
[i
]);
5492 * This is an internal function that allows other converter implementations
5493 * to check whether a byte is a lead byte.
5496 ucnv_MBCSIsLeadByte(UConverterSharedData
*sharedData
, char byte
) {
5497 return (UBool
)MBCS_ENTRY_IS_TRANSITION(sharedData
->mbcs
.stateTable
[0][(uint8_t)byte
]);
5501 ucnv_MBCSWriteSub(UConverterFromUnicodeArgs
*pArgs
,
5502 int32_t offsetIndex
,
5503 UErrorCode
*pErrorCode
) {
5504 UConverter
*cnv
=pArgs
->converter
;
5509 /* first, select between subChar and subChar1 */
5510 if( cnv
->subChar1
!=0 &&
5511 (cnv
->sharedData
->mbcs
.extIndexes
!=NULL
?
5513 (cnv
->invalidUCharBuffer
[0]<=0xff))
5515 /* select subChar1 if it is set (not 0) and the unmappable Unicode code point is up to U+00ff (IBM MBCS behavior) */
5516 subchar
=(char *)&cnv
->subChar1
;
5519 /* select subChar in all other cases */
5520 subchar
=(char *)cnv
->subChars
;
5521 length
=cnv
->subCharLen
;
5524 /* reset the selector for the next code point */
5525 cnv
->useSubChar1
=FALSE
;
5527 if (cnv
->sharedData
->mbcs
.outputType
== MBCS_OUTPUT_2_SISO
) {
5530 /* fromUnicodeStatus contains prevLength */
5533 if(cnv
->fromUnicodeStatus
==2) {
5534 /* DBCS mode and SBCS sub char: change to SBCS */
5535 cnv
->fromUnicodeStatus
=1;
5541 if(cnv
->fromUnicodeStatus
<=1) {
5542 /* SBCS mode and DBCS sub char: change to DBCS */
5543 cnv
->fromUnicodeStatus
=2;
5550 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
5554 length
=(int32_t)(p
-buffer
);
5557 ucnv_cbFromUWriteBytes(pArgs
, subchar
, length
, offsetIndex
, pErrorCode
);
5560 U_CFUNC UConverterType
5561 ucnv_MBCSGetType(const UConverter
* converter
) {
5562 /* SBCS, DBCS, and EBCDIC_STATEFUL are replaced by MBCS, but here we cheat a little */
5563 if(converter
->sharedData
->mbcs
.countStates
==1) {
5564 return (UConverterType
)UCNV_SBCS
;
5565 } else if((converter
->sharedData
->mbcs
.outputType
&0xff)==MBCS_OUTPUT_2_SISO
) {
5566 return (UConverterType
)UCNV_EBCDIC_STATEFUL
;
5567 } else if(converter
->sharedData
->staticData
->minBytesPerChar
==2 && converter
->sharedData
->staticData
->maxBytesPerChar
==2) {
5568 return (UConverterType
)UCNV_DBCS
;
5570 return (UConverterType
)UCNV_MBCS
;
5573 static const UConverterImpl _SBCSUTF8Impl
={
5583 ucnv_MBCSToUnicodeWithOffsets
,
5584 ucnv_MBCSToUnicodeWithOffsets
,
5585 ucnv_MBCSFromUnicodeWithOffsets
,
5586 ucnv_MBCSFromUnicodeWithOffsets
,
5587 ucnv_MBCSGetNextUChar
,
5589 ucnv_MBCSGetStarters
,
5593 ucnv_MBCSGetUnicodeSet
,
5599 static const UConverterImpl _DBCSUTF8Impl
={
5609 ucnv_MBCSToUnicodeWithOffsets
,
5610 ucnv_MBCSToUnicodeWithOffsets
,
5611 ucnv_MBCSFromUnicodeWithOffsets
,
5612 ucnv_MBCSFromUnicodeWithOffsets
,
5613 ucnv_MBCSGetNextUChar
,
5615 ucnv_MBCSGetStarters
,
5619 ucnv_MBCSGetUnicodeSet
,
5625 static const UConverterImpl _MBCSImpl
={
5635 ucnv_MBCSToUnicodeWithOffsets
,
5636 ucnv_MBCSToUnicodeWithOffsets
,
5637 ucnv_MBCSFromUnicodeWithOffsets
,
5638 ucnv_MBCSFromUnicodeWithOffsets
,
5639 ucnv_MBCSGetNextUChar
,
5641 ucnv_MBCSGetStarters
,
5645 ucnv_MBCSGetUnicodeSet
5649 /* Static data is in tools/makeconv/ucnvstat.c for data-based
5650 * converters. Be sure to update it as well.
5653 const UConverterSharedData _MBCSData
={
5654 sizeof(UConverterSharedData
), 1,
5655 NULL
, NULL
, NULL
, FALSE
, &_MBCSImpl
,
5659 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */