2 ******************************************************************************
4 * Copyright (C) 2000-2012, 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"
51 #include "unicode/utf8.h"
52 #include "unicode/utf16.h"
61 /* control optimizations according to the platform */
62 #define MBCS_UNROLL_SINGLE_TO_BMP 1
63 #define MBCS_UNROLL_SINGLE_FROM_BMP 0
66 * _MBCSHeader versions 5.3 & 4.3
67 * (Note that the _MBCSHeader version is in addition to the converter formatVersion.)
69 * This version is optional. Version 5 is used for incompatible data format changes.
70 * makeconv will continue to generate version 4 files if possible.
72 * Changes from version 4:
74 * The main difference is an additional _MBCSHeader field with
75 * - the length (number of uint32_t) of the _MBCSHeader
76 * - flags for further incompatible data format changes
77 * - flags for further, backward compatible data format changes
79 * The MBCS_OPT_FROM_U flag indicates that most of the fromUnicode data is omitted from
80 * the file and needs to be reconstituted at load time.
81 * This requires a utf8Friendly format with an additional mbcsIndex table for fast
82 * (and UTF-8-friendly) fromUnicode conversion for Unicode code points up to maxFastUChar.
83 * (For details about these structures see below, and see ucnvmbcs.h.)
85 * utf8Friendly also implies that the fromUnicode mappings are stored in ascending order
86 * of the Unicode code points. (This requires that the .ucm file has the |0 etc.
87 * precision markers for all mappings.)
89 * All fallbacks have been moved to the extension table, leaving only roundtrips in the
90 * omitted data that can be reconstituted from the toUnicode data.
92 * Of the stage 2 table, the part corresponding to maxFastUChar and below is omitted.
93 * With only roundtrip mappings in the base fromUnicode data, this part is fully
94 * redundant with the mbcsIndex and will be reconstituted from that (also using the
95 * stage 1 table which contains the information about how stage 2 was compacted).
97 * The rest of the stage 2 table, the part for code points above maxFastUChar,
98 * is stored in the file and will be appended to the reconstituted part.
100 * The entire fromUBytes array is omitted from the file and will be reconstitued.
101 * This is done by enumerating all toUnicode roundtrip mappings, performing
102 * each mapping (using the stage 1 and reconstituted stage 2 tables) and
103 * writing instead of reading the byte values.
105 * _MBCSHeader version 4.3
107 * Change from version 4.2:
108 * - Optional utf8Friendly data structures, with 64-entry stage 3 block
109 * allocation for parts of the BMP, and an additional mbcsIndex in non-SBCS
110 * files which can be used instead of stages 1 & 2.
111 * Faster lookups for roundtrips from most commonly used characters,
112 * and lookups from UTF-8 byte sequences with a natural bit distribution.
113 * See ucnvmbcs.h for more details.
115 * Change from version 4.1:
116 * - Added an optional extension table structure at the end of the .cnv file.
117 * It is present if the upper bits of the header flags field contains a non-zero
119 * Files that contain only a conversion table and no base table
120 * use the special outputType MBCS_OUTPUT_EXT_ONLY.
121 * These contain the base table name between the MBCS header and the extension
124 * Change from version 4.0:
125 * - Replace header.reserved with header.fromUBytesLength so that all
126 * fields in the data have length.
128 * Changes from version 3 (for performance improvements):
129 * - new bit distribution for state table entries
130 * - reordered action codes
131 * - new data structure for single-byte fromUnicode
132 * + stage 2 only contains indexes
133 * + stage 3 stores 16 bits per character with classification bits 15..8
134 * - no multiplier for stage 1 entries
135 * - stage 2 for non-single-byte codepages contains the index and the flags in
137 * - 2-byte and 4-byte fromUnicode results are stored directly as 16/32-bit integers
139 * For more details about old versions of the MBCS data structure, see
140 * the corresponding versions of this file.
142 * Converting stateless codepage data ---------------------------------------***
143 * (or codepage data with simple states) to Unicode.
145 * Data structure and algorithm for converting from complex legacy codepages
146 * to Unicode. (Designed before 2000-may-22.)
148 * The basic idea is that the structure of legacy codepages can be described
150 * When reading a byte stream, each input byte causes a state transition.
151 * Some transitions result in the output of a code point, some result in
152 * "unassigned" or "illegal" output.
153 * This is used here for character conversion.
155 * The data structure begins with a state table consisting of a row
156 * per state, with 256 entries (columns) per row for each possible input
158 * Each entry is 32 bits wide, with two formats distinguished by
159 * the sign bit (bit 31):
161 * One format for transitional entries (bit 31 not set) for non-final bytes, and
162 * one format for final entries (bit 31 set).
163 * Both formats contain the number of the next state in the same bit
165 * State 0 is the initial state.
167 * Most of the time, the offset values of subsequent states are added
168 * up to a scalar value. This value will eventually be the index of
169 * the Unicode code point in a table that follows the state table.
170 * The effect is that the code points for final state table rows
171 * are contiguous. The code points of final state rows follow each other
172 * in the order of the references to those final states by previous
175 * For some terminal states, the offset is itself the output Unicode
176 * code point (16 bits for a BMP code point or 20 bits for a supplementary
177 * code point (stored as code point minus 0x10000 so that 20 bits are enough).
178 * For others, the code point in the Unicode table is stored with either
179 * one or two code units: one for BMP code points, two for a pair of
181 * All code points for a final state entry take up the same number of code
182 * units, regardless of whether they all actually _use_ the same number
183 * of code units. This is necessary for simple array access.
185 * An additional feature comes in with what in ICU is called "fallback"
188 * In addition to round-trippable, precise, 1:1 mappings, there are often
189 * mappings defined between similar, though not the same, characters.
190 * Typically, such mappings occur only in fromUnicode mapping tables because
191 * Unicode has a superset repertoire of most other codepages. However, it
192 * is possible to provide such mappings in the toUnicode tables, too.
193 * In this case, the fallback mappings are partly integrated into the
194 * general state tables because the structure of the encoding includes their
196 * For final entries in an initial state, fallback mappings are stored in
197 * the entry itself like with roundtrip mappings.
198 * For other final entries, they are stored in the code units table if
199 * the entry is for a pair of code units.
200 * For single-unit results in the code units table, there is no space to
201 * alternatively hold a fallback mapping; in this case, the code unit
202 * is stored as U+fffe (unassigned), and the fallback mapping needs to
203 * be looked up by the scalar offset value in a separate table.
205 * "Unassigned" state entries really mean "structurally unassigned",
206 * i.e., such a byte sequence will never have a mapping result.
208 * The interpretation of the bits in each entry is as follows:
210 * Bit 31 not set, not a terminal entry ("transitional"):
212 * 23..0 offset delta, to be added up
214 * Bit 31 set, terminal ("final") entry:
215 * 30..24 next state (regardless of action code)
216 * 23..20 action code:
217 * action codes 0 and 1 result in precise-mapping Unicode code points
218 * 0 valid byte sequence
220 * 15..0 16-bit Unicode BMP code point
221 * never U+fffe or U+ffff
222 * 1 valid byte sequence
223 * 19..0 20-bit Unicode supplementary code point
224 * never U+fffe or U+ffff
226 * action codes 2 and 3 result in fallback (unidirectional-mapping) Unicode code points
227 * 2 valid byte sequence (fallback)
229 * 15..0 16-bit Unicode BMP code point as fallback result
230 * 3 valid byte sequence (fallback)
231 * 19..0 20-bit Unicode supplementary code point as fallback result
233 * action codes 4 and 5 may result in roundtrip/fallback/unassigned/illegal results
234 * depending on the code units they result in
235 * 4 valid byte sequence
237 * 8..0 final offset delta
238 * pointing to one 16-bit code unit which may be
239 * fffe unassigned -- look for a fallback for this offset
241 * 5 valid byte sequence
243 * 8..0 final offset delta
244 * pointing to two 16-bit code units
245 * (typically UTF-16 surrogates)
246 * the result depends on the first code unit as follows:
247 * 0000..d7ff roundtrip BMP code point (1st alone)
248 * d800..dbff roundtrip surrogate pair (1st, 2nd)
249 * dc00..dfff fallback surrogate pair (1st-400, 2nd)
250 * e000 roundtrip BMP code point (2nd alone)
251 * e001 fallback BMP code point (2nd alone)
254 * (the final offset deltas are at most 255 * 2,
255 * times 2 because of storing code unit pairs)
257 * 6 unassigned byte sequence
259 * 15..0 16-bit Unicode BMP code point U+fffe (new with version 2)
260 * this does not contain a final offset delta because the main
261 * purpose of this action code is to save scalar offset values;
262 * therefore, fallback values cannot be assigned to byte
263 * sequences that result in this action code
264 * 7 illegal byte sequence
266 * 15..0 16-bit Unicode BMP code point U+ffff (new with version 2)
267 * 8 state change only
269 * useful for state changes in simple stateful encodings,
270 * at Shift-In/Shift-Out codes
273 * 9..15 reserved for future use
274 * current implementations will only perform a state change
275 * and ignore bits 19..0
277 * An encoding with contiguous ranges of unassigned byte sequences, like
278 * Shift-JIS and especially EUC-TW, can be stored efficiently by having
279 * at least two states for the trail bytes:
280 * One trail byte state that results in code points, and one that only
281 * has "unassigned" and "illegal" terminal states.
283 * Note: partly by accident, this data structure supports simple stateful
284 * encodings without any additional logic.
285 * Currently, only simple Shift-In/Shift-Out schemes are handled with
286 * appropriate state tables (especially EBCDIC_STATEFUL!).
288 * MBCS version 2 added:
289 * unassigned and illegal action codes have U+fffe and U+ffff
290 * instead of unused bits; this is useful for _MBCS_SINGLE_SIMPLE_GET_NEXT_BMP()
292 * Converting from Unicode to codepage bytes --------------------------------***
294 * The conversion data structure for fromUnicode is designed for the known
295 * structure of Unicode. It maps from 21-bit code points (0..0x10ffff) to
296 * a sequence of 1..4 bytes, in addition to a flag that indicates if there is
297 * a roundtrip mapping.
299 * The lookup is done with a 3-stage trie, using 11/6/4 bits for stage 1/2/3
300 * like in the character properties table.
301 * The beginning of the trie is at offsetFromUTable, the beginning of stage 3
302 * with the resulting bytes is at offsetFromUBytes.
304 * Beginning with version 4, single-byte codepages have a significantly different
305 * trie compared to other codepages.
306 * In all cases, the entry in stage 1 is directly the index of the block of
307 * 64 entries in stage 2.
309 * Single-byte lookup:
311 * Stage 2 only contains 16-bit indexes directly to the 16-blocks in stage 3.
312 * Stage 3 contains one 16-bit word per result:
313 * Bits 15..8 indicate the kind of result:
315 * c fallback result from private-use code point
316 * 8 fallback result from other code points
318 * Bits 7..0 contain the codepage byte. A zero byte is always possible.
320 * In version 4.3, the runtime code can build an sbcsIndex for a utf8Friendly
321 * file. For 2-byte UTF-8 byte sequences and some 3-byte sequences the lookup
322 * becomes a 2-stage (single-index) trie lookup with 6 bits for stage 3.
323 * ASCII code points can be looked up with a linear array access into stage 3.
324 * See maxFastUChar and other details in ucnvmbcs.h.
328 * Stage 2 contains a 32-bit word for each 16-block in stage 3:
329 * Bits 31..16 contain flags for which stage 3 entries contain roundtrip results
330 * test: MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c)
331 * If this test is false, then a non-zero result will be interpreted as
332 * a fallback mapping.
333 * Bits 15..0 contain the index to stage 3, which must be multiplied by 16*(bytes per char)
335 * Stage 3 contains 2, 3, or 4 bytes per result.
336 * 2 or 4 bytes are stored as uint16_t/uint32_t in platform endianness,
337 * while 3 bytes are stored as bytes in big-endian order.
338 * Leading zero bytes are ignored, and the number of bytes is counted.
339 * A zero byte mapping result is possible as a roundtrip result.
340 * For some output types, the actual result is processed from this;
341 * see ucnv_MBCSFromUnicodeWithOffsets().
343 * Note that stage 1 always contains 0x440=1088 entries (0x440==0x110000>>10),
344 * or (version 3 and up) for BMP-only codepages, it contains 64 entries.
346 * In version 4.3, a utf8Friendly file contains an mbcsIndex table.
347 * For 2-byte UTF-8 byte sequences and most 3-byte sequences the lookup
348 * becomes a 2-stage (single-index) trie lookup with 6 bits for stage 3.
349 * ASCII code points can be looked up with a linear array access into stage 3.
350 * See maxFastUChar, mbcsIndex and other details in ucnvmbcs.h.
352 * In version 3, stage 2 blocks may overlap by multiples of the multiplier
354 * In version 4, stage 2 blocks (and for single-byte codepages, stage 3 blocks)
355 * may overlap by any number of entries.
357 * MBCS version 2 added:
358 * the converter checks for known output types, which allows
359 * adding new ones without crashing an unaware converter
362 static const UConverterImpl _SBCSUTF8Impl
;
363 static const UConverterImpl _DBCSUTF8Impl
;
365 /* GB 18030 data ------------------------------------------------------------ */
367 /* helper macros for linear values for GB 18030 four-byte sequences */
368 #define LINEAR_18030(a, b, c, d) ((((a)*10+(b))*126L+(c))*10L+(d))
370 #define LINEAR_18030_BASE LINEAR_18030(0x81, 0x30, 0x81, 0x30)
372 #define LINEAR(x) LINEAR_18030(x>>24, (x>>16)&0xff, (x>>8)&0xff, x&0xff)
375 * Some ranges of GB 18030 where both the Unicode code points and the
376 * GB four-byte sequences are contiguous and are handled algorithmically by
377 * the special callback functions below.
378 * The values are start & end of Unicode & GB codes.
380 * Note that single surrogates are not mapped by GB 18030
381 * as of the re-released mapping tables from 2000-nov-30.
383 static const uint32_t
384 gb18030Ranges
[14][4]={
385 {0x10000, 0x10FFFF, LINEAR(0x90308130), LINEAR(0xE3329A35)},
386 {0x9FA6, 0xD7FF, LINEAR(0x82358F33), LINEAR(0x8336C738)},
387 {0x0452, 0x1E3E, LINEAR(0x8130D330), LINEAR(0x8135F436)},
388 {0x1E40, 0x200F, LINEAR(0x8135F438), LINEAR(0x8136A531)},
389 {0xE865, 0xF92B, LINEAR(0x8336D030), LINEAR(0x84308534)},
390 {0x2643, 0x2E80, LINEAR(0x8137A839), LINEAR(0x8138FD38)},
391 {0xFA2A, 0xFE2F, LINEAR(0x84309C38), LINEAR(0x84318537)},
392 {0x3CE1, 0x4055, LINEAR(0x8231D438), LINEAR(0x8232AF32)},
393 {0x361B, 0x3917, LINEAR(0x8230A633), LINEAR(0x8230F237)},
394 {0x49B8, 0x4C76, LINEAR(0x8234A131), LINEAR(0x8234E733)},
395 {0x4160, 0x4336, LINEAR(0x8232C937), LINEAR(0x8232F837)},
396 {0x478E, 0x4946, LINEAR(0x8233E838), LINEAR(0x82349638)},
397 {0x44D7, 0x464B, LINEAR(0x8233A339), LINEAR(0x8233C931)},
398 {0xFFE6, 0xFFFF, LINEAR(0x8431A234), LINEAR(0x8431A439)}
401 /* bit flag for UConverter.options indicating GB 18030 special handling */
402 #define _MBCS_OPTION_GB18030 0x8000
404 /* bit flag for UConverter.options indicating KEIS,JEF,JIF special handling */
405 #define _MBCS_OPTION_KEIS 0x01000
406 #define _MBCS_OPTION_JEF 0x02000
407 #define _MBCS_OPTION_JIPS 0x04000
409 #define KEIS_SO_CHAR_1 0x0A
410 #define KEIS_SO_CHAR_2 0x42
411 #define KEIS_SI_CHAR_1 0x0A
412 #define KEIS_SI_CHAR_2 0x41
414 #define JEF_SO_CHAR 0x28
415 #define JEF_SI_CHAR 0x29
417 #define JIPS_SO_CHAR_1 0x1A
418 #define JIPS_SO_CHAR_2 0x70
419 #define JIPS_SI_CHAR_1 0x1A
420 #define JIPS_SI_CHAR_2 0x71
426 typedef enum SISO_Option SISO_Option
;
428 static int32_t getSISOBytes(SISO_Option option
, uint32_t cnvOption
, uint8_t *value
) {
429 int32_t SISOLength
= 0;
433 if ((cnvOption
&_MBCS_OPTION_KEIS
)!=0) {
434 value
[0] = KEIS_SI_CHAR_1
;
435 value
[1] = KEIS_SI_CHAR_2
;
437 } else if ((cnvOption
&_MBCS_OPTION_JEF
)!=0) {
438 value
[0] = JEF_SI_CHAR
;
440 } else if ((cnvOption
&_MBCS_OPTION_JIPS
)!=0) {
441 value
[0] = JIPS_SI_CHAR_1
;
442 value
[1] = JIPS_SI_CHAR_2
;
450 if ((cnvOption
&_MBCS_OPTION_KEIS
)!=0) {
451 value
[0] = KEIS_SO_CHAR_1
;
452 value
[1] = KEIS_SO_CHAR_2
;
454 } else if ((cnvOption
&_MBCS_OPTION_JEF
)!=0) {
455 value
[0] = JEF_SO_CHAR
;
457 } else if ((cnvOption
&_MBCS_OPTION_JIPS
)!=0) {
458 value
[0] = JIPS_SO_CHAR_1
;
459 value
[1] = JIPS_SO_CHAR_2
;
467 /* Should never happen. */
474 /* Miscellaneous ------------------------------------------------------------ */
477 * Callback from ucnv_MBCSEnumToUnicode(), takes 32 mappings from
478 * consecutive sequences of bytes, starting from the one encoded in value,
479 * to Unicode code points. (Multiple mappings to reduce per-function call overhead.)
480 * Does not currently support m:n mappings or reverse fallbacks.
481 * This function will not be called for sequences of bytes with leading zeros.
483 * @param context an opaque pointer, as passed into ucnv_MBCSEnumToUnicode()
484 * @param value contains 1..4 bytes of the first byte sequence, right-aligned
485 * @param codePoints resulting Unicode code points, or negative if a byte sequence does
486 * not map to anything
487 * @return TRUE to continue enumeration, FALSE to stop
489 typedef UBool U_CALLCONV
490 UConverterEnumToUCallback(const void *context
, uint32_t value
, UChar32 codePoints
[32]);
492 /* similar to ucnv_MBCSGetNextUChar() but recursive */
494 enumToU(UConverterMBCSTable
*mbcsTable
, int8_t stateProps
[],
495 int32_t state
, uint32_t offset
,
497 UConverterEnumToUCallback
*callback
, const void *context
,
498 UErrorCode
*pErrorCode
) {
499 UChar32 codePoints
[32];
501 const uint16_t *unicodeCodeUnits
;
502 UChar32 anyCodePoints
;
505 row
=mbcsTable
->stateTable
[state
];
506 unicodeCodeUnits
=mbcsTable
->unicodeCodeUnits
;
509 anyCodePoints
=-1; /* becomes non-negative if there is a mapping */
511 b
=(stateProps
[state
]&0x38)<<2;
512 if(b
==0 && stateProps
[state
]>=0x40) {
513 /* skip byte sequences with leading zeros because they are not stored in the fromUnicode table */
514 codePoints
[0]=U_SENTINEL
;
517 limit
=((stateProps
[state
]&7)+1)<<5;
519 int32_t entry
=row
[b
];
520 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
521 int32_t nextState
=MBCS_ENTRY_TRANSITION_STATE(entry
);
522 if(stateProps
[nextState
]>=0) {
523 /* recurse to a state with non-ignorable actions */
525 mbcsTable
, stateProps
, nextState
,
526 offset
+MBCS_ENTRY_TRANSITION_OFFSET(entry
),
533 codePoints
[b
&0x1f]=U_SENTINEL
;
539 * An if-else-if chain provides more reliable performance for
540 * the most common cases compared to a switch.
542 action
=MBCS_ENTRY_FINAL_ACTION(entry
);
543 if(action
==MBCS_STATE_VALID_DIRECT_16
) {
544 /* output BMP code point */
545 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
546 } else if(action
==MBCS_STATE_VALID_16
) {
547 int32_t finalOffset
=offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
);
548 c
=unicodeCodeUnits
[finalOffset
];
550 /* output BMP code point */
554 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
555 int32_t finalOffset
=offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
);
556 c
=unicodeCodeUnits
[finalOffset
++];
558 /* output BMP code point below 0xd800 */
559 } else if(c
<=0xdbff) {
560 /* output roundtrip or fallback supplementary code point */
561 c
=((c
&0x3ff)<<10)+unicodeCodeUnits
[finalOffset
]+(0x10000-0xdc00);
562 } else if(c
==0xe000) {
563 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
564 c
=unicodeCodeUnits
[finalOffset
];
568 } else if(action
==MBCS_STATE_VALID_DIRECT_20
) {
569 /* output supplementary code point */
570 c
=(UChar32
)(MBCS_ENTRY_FINAL_VALUE(entry
)+0x10000);
575 codePoints
[b
&0x1f]=c
;
578 if(((++b
)&0x1f)==0) {
579 if(anyCodePoints
>=0) {
580 if(!callback(context
, value
|(uint32_t)(b
-0x20), codePoints
)) {
591 * Only called if stateProps[state]==-1.
592 * A recursive call may do stateProps[state]|=0x40 if this state is the target of an
593 * MBCS_STATE_CHANGE_ONLY.
596 getStateProp(const int32_t (*stateTable
)[256], int8_t stateProps
[], int state
) {
598 int32_t min
, max
, entry
, nextState
;
600 row
=stateTable
[state
];
603 /* find first non-ignorable state */
606 nextState
=MBCS_ENTRY_STATE(entry
);
607 if(stateProps
[nextState
]==-1) {
608 getStateProp(stateTable
, stateProps
, nextState
);
610 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
611 if(stateProps
[nextState
]>=0) {
614 } else if(MBCS_ENTRY_FINAL_ACTION(entry
)<MBCS_STATE_UNASSIGNED
) {
618 stateProps
[state
]=-0x40; /* (int8_t)0xc0 */
619 return stateProps
[state
];
622 stateProps
[state
]|=(int8_t)((min
>>5)<<3);
624 /* find last non-ignorable state */
625 for(max
=0xff; min
<max
; --max
) {
627 nextState
=MBCS_ENTRY_STATE(entry
);
628 if(stateProps
[nextState
]==-1) {
629 getStateProp(stateTable
, stateProps
, nextState
);
631 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
632 if(stateProps
[nextState
]>=0) {
635 } else if(MBCS_ENTRY_FINAL_ACTION(entry
)<MBCS_STATE_UNASSIGNED
) {
639 stateProps
[state
]|=(int8_t)(max
>>5);
641 /* recurse further and collect direct-state information */
644 nextState
=MBCS_ENTRY_STATE(entry
);
645 if(stateProps
[nextState
]==-1) {
646 getStateProp(stateTable
, stateProps
, nextState
);
648 if(MBCS_ENTRY_IS_FINAL(entry
)) {
649 stateProps
[nextState
]|=0x40;
650 if(MBCS_ENTRY_FINAL_ACTION(entry
)<=MBCS_STATE_FALLBACK_DIRECT_20
) {
651 stateProps
[state
]|=0x40;
656 return stateProps
[state
];
660 * Internal function enumerating the toUnicode data of an MBCS converter.
661 * Currently only used for reconstituting data for a MBCS_OPT_NO_FROM_U
662 * table, but could also be used for a future ucnv_getUnicodeSet() option
663 * that includes reverse fallbacks (after updating this function's implementation).
664 * Currently only handles roundtrip mappings.
665 * Does not currently handle extensions.
668 ucnv_MBCSEnumToUnicode(UConverterMBCSTable
*mbcsTable
,
669 UConverterEnumToUCallback
*callback
, const void *context
,
670 UErrorCode
*pErrorCode
) {
672 * Properties for each state, to speed up the enumeration.
673 * Ignorable actions are unassigned/illegal/state-change-only:
674 * They do not lead to mappings.
677 * 1 direct/initial state (stateful converters have multiple)
678 * 0 non-initial state with transitions or with non-ignorable result actions
679 * -1 final state with only ignorable actions
682 * The lowest byte value with non-ignorable actions is
683 * value<<5 (rounded down).
686 * The highest byte value with non-ignorable actions is
687 * (value<<5)&0x1f (rounded up).
689 int8_t stateProps
[MBCS_MAX_STATE_COUNT
];
692 uprv_memset(stateProps
, -1, sizeof(stateProps
));
694 /* recurse from state 0 and set all stateProps */
695 getStateProp(mbcsTable
->stateTable
, stateProps
, 0);
697 for(state
=0; state
<mbcsTable
->countStates
; ++state
) {
698 /*if(stateProps[state]==-1) {
699 printf("unused/unreachable <icu:state> %d\n", state);
701 if(stateProps
[state
]>=0x40) {
702 /* start from each direct state */
704 mbcsTable
, stateProps
, state
, 0, 0,
712 ucnv_MBCSGetFilteredUnicodeSetForUnicode(const UConverterSharedData
*sharedData
,
714 UConverterUnicodeSet which
,
715 UConverterSetFilter filter
,
716 UErrorCode
*pErrorCode
) {
717 const UConverterMBCSTable
*mbcsTable
;
718 const uint16_t *table
;
721 uint16_t st1
, maxStage1
, st2
;
725 /* enumerate the from-Unicode trie table */
726 mbcsTable
=&sharedData
->mbcs
;
727 table
=mbcsTable
->fromUnicodeTable
;
728 if(mbcsTable
->unicodeMask
&UCNV_HAS_SUPPLEMENTARY
) {
734 c
=0; /* keep track of the current code point while enumerating */
736 if(mbcsTable
->outputType
==MBCS_OUTPUT_1
) {
737 const uint16_t *stage2
, *stage3
, *results
;
740 results
=(const uint16_t *)mbcsTable
->fromUnicodeBytes
;
743 * Set a threshold variable for selecting which mappings to use.
744 * See ucnv_MBCSSingleFromBMPWithOffsets() and
745 * MBCS_SINGLE_RESULT_FROM_U() for details.
747 if(which
==UCNV_ROUNDTRIP_SET
) {
748 /* use only roundtrips */
750 } else /* UCNV_ROUNDTRIP_AND_FALLBACK_SET */ {
751 /* use all roundtrip and fallback results */
755 for(st1
=0; st1
<maxStage1
; ++st1
) {
759 for(st2
=0; st2
<64; ++st2
) {
760 if((st3
=stage2
[st2
])!=0) {
761 /* read the stage 3 block */
765 if(*stage3
++>=minValue
) {
768 } while((++c
&0xf)!=0);
770 c
+=16; /* empty stage 3 block */
774 c
+=1024; /* empty stage 2 block */
778 const uint32_t *stage2
;
779 const uint8_t *stage3
, *bytes
;
780 uint32_t st3Multiplier
;
784 bytes
=mbcsTable
->fromUnicodeBytes
;
786 useFallback
=(UBool
)(which
==UCNV_ROUNDTRIP_AND_FALLBACK_SET
);
788 switch(mbcsTable
->outputType
) {
790 case MBCS_OUTPUT_4_EUC
:
801 for(st1
=0; st1
<maxStage1
; ++st1
) {
803 if(st2
>(maxStage1
>>1)) {
804 stage2
=(const uint32_t *)table
+st2
;
805 for(st2
=0; st2
<64; ++st2
) {
806 if((st3
=stage2
[st2
])!=0) {
807 /* read the stage 3 block */
808 stage3
=bytes
+st3Multiplier
*16*(uint32_t)(uint16_t)st3
;
810 /* get the roundtrip flags for the stage 3 block */
814 * Add code points for which the roundtrip flag is set,
815 * or which map to non-zero bytes if we use fallbacks.
816 * See ucnv_MBCSFromUnicodeWithOffsets() for details.
819 case UCNV_SET_FILTER_NONE
:
823 stage3
+=st3Multiplier
;
824 } else if(useFallback
) {
826 switch(st3Multiplier
) {
829 case 3: /*fall through*/
831 case 2: /*fall through*/
832 b
|=stage3
[0]|stage3
[1];
842 } while((++c
&0xf)!=0);
844 case UCNV_SET_FILTER_DBCS_ONLY
:
845 /* Ignore single-byte results (<0x100). */
847 if(((st3
&1)!=0 || useFallback
) && *((const uint16_t *)stage3
)>=0x100) {
851 stage3
+=2; /* +=st3Multiplier */
852 } while((++c
&0xf)!=0);
854 case UCNV_SET_FILTER_2022_CN
:
855 /* Only add code points that map to CNS 11643 planes 1 & 2 for non-EXT ISO-2022-CN. */
857 if(((st3
&1)!=0 || useFallback
) && ((value
=*stage3
)==0x81 || value
==0x82)) {
861 stage3
+=3; /* +=st3Multiplier */
862 } while((++c
&0xf)!=0);
864 case UCNV_SET_FILTER_SJIS
:
865 /* Only add code points that map to Shift-JIS codes corresponding to JIS X 0208. */
867 if(((st3
&1)!=0 || useFallback
) && (value
=*((const uint16_t *)stage3
))>=0x8140 && value
<=0xeffc) {
871 stage3
+=2; /* +=st3Multiplier */
872 } while((++c
&0xf)!=0);
874 case UCNV_SET_FILTER_GR94DBCS
:
875 /* Only add code points that map to ISO 2022 GR 94 DBCS codes (each byte A1..FE). */
877 if( ((st3
&1)!=0 || useFallback
) &&
878 (uint16_t)((value
=*((const uint16_t *)stage3
)) - 0xa1a1)<=(0xfefe - 0xa1a1) &&
879 (uint8_t)(value
-0xa1)<=(0xfe - 0xa1)
884 stage3
+=2; /* +=st3Multiplier */
885 } while((++c
&0xf)!=0);
887 case UCNV_SET_FILTER_HZ
:
888 /* Only add code points that are suitable for HZ DBCS (lead byte A1..FD). */
890 if( ((st3
&1)!=0 || useFallback
) &&
891 (uint16_t)((value
=*((const uint16_t *)stage3
))-0xa1a1)<=(0xfdfe - 0xa1a1) &&
892 (uint8_t)(value
-0xa1)<=(0xfe - 0xa1)
897 stage3
+=2; /* +=st3Multiplier */
898 } while((++c
&0xf)!=0);
901 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
905 c
+=16; /* empty stage 3 block */
909 c
+=1024; /* empty stage 2 block */
914 ucnv_extGetUnicodeSet(sharedData
, sa
, which
, filter
, pErrorCode
);
918 ucnv_MBCSGetUnicodeSetForUnicode(const UConverterSharedData
*sharedData
,
920 UConverterUnicodeSet which
,
921 UErrorCode
*pErrorCode
) {
922 ucnv_MBCSGetFilteredUnicodeSetForUnicode(
923 sharedData
, sa
, which
,
924 sharedData
->mbcs
.outputType
==MBCS_OUTPUT_DBCS_ONLY
?
925 UCNV_SET_FILTER_DBCS_ONLY
:
926 UCNV_SET_FILTER_NONE
,
931 ucnv_MBCSGetUnicodeSet(const UConverter
*cnv
,
933 UConverterUnicodeSet which
,
934 UErrorCode
*pErrorCode
) {
935 if(cnv
->options
&_MBCS_OPTION_GB18030
) {
936 sa
->addRange(sa
->set
, 0, 0xd7ff);
937 sa
->addRange(sa
->set
, 0xe000, 0x10ffff);
939 ucnv_MBCSGetUnicodeSetForUnicode(cnv
->sharedData
, sa
, which
, pErrorCode
);
943 /* conversion extensions for input not in the main table -------------------- */
946 * Hardcoded extension handling for GB 18030.
947 * Definition of LINEAR macros and gb18030Ranges see near the beginning of the file.
949 * In the future, conversion extensions may handle m:n mappings and delta tables,
950 * see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/conversion/conversion_extensions.html
952 * If an input character cannot be mapped, then these functions set an error
953 * code. The framework will then call the callback function.
957 * @return if(U_FAILURE) return the code point for cnv->fromUChar32
958 * else return 0 after output has been written to the target
961 _extFromU(UConverter
*cnv
, const UConverterSharedData
*sharedData
,
963 const UChar
**source
, const UChar
*sourceLimit
,
964 uint8_t **target
, const uint8_t *targetLimit
,
965 int32_t **offsets
, int32_t sourceIndex
,
967 UErrorCode
*pErrorCode
) {
970 cnv
->useSubChar1
=FALSE
;
972 if( (cx
=sharedData
->mbcs
.extIndexes
)!=NULL
&&
973 ucnv_extInitialMatchFromU(
975 cp
, source
, sourceLimit
,
976 (char **)target
, (char *)targetLimit
,
977 offsets
, sourceIndex
,
981 return 0; /* an extension mapping handled the input */
985 if((cnv
->options
&_MBCS_OPTION_GB18030
)!=0) {
986 const uint32_t *range
;
989 range
=gb18030Ranges
[0];
990 for(i
=0; i
<sizeof(gb18030Ranges
)/sizeof(gb18030Ranges
[0]); range
+=4, ++i
) {
991 if(range
[0]<=(uint32_t)cp
&& (uint32_t)cp
<=range
[1]) {
992 /* found the Unicode code point, output the four-byte sequence for it */
996 /* get the linear value of the first GB 18030 code in this range */
997 linear
=range
[2]-LINEAR_18030_BASE
;
999 /* add the offset from the beginning of the range */
1000 linear
+=((uint32_t)cp
-range
[0]);
1002 /* turn this into a four-byte sequence */
1003 bytes
[3]=(char)(0x30+linear%10
); linear
/=10;
1004 bytes
[2]=(char)(0x81+linear%126
); linear
/=126;
1005 bytes
[1]=(char)(0x30+linear%10
); linear
/=10;
1006 bytes
[0]=(char)(0x81+linear
);
1008 /* output this sequence */
1009 ucnv_fromUWriteBytes(cnv
,
1010 bytes
, 4, (char **)target
, (char *)targetLimit
,
1011 offsets
, sourceIndex
, pErrorCode
);
1018 *pErrorCode
=U_INVALID_CHAR_FOUND
;
1023 * Input sequence: cnv->toUBytes[0..length[
1024 * @return if(U_FAILURE) return the length (toULength, byteIndex) for the input
1025 * else return 0 after output has been written to the target
1028 _extToU(UConverter
*cnv
, const UConverterSharedData
*sharedData
,
1030 const uint8_t **source
, const uint8_t *sourceLimit
,
1031 UChar
**target
, const UChar
*targetLimit
,
1032 int32_t **offsets
, int32_t sourceIndex
,
1034 UErrorCode
*pErrorCode
) {
1037 if( (cx
=sharedData
->mbcs
.extIndexes
)!=NULL
&&
1038 ucnv_extInitialMatchToU(
1040 length
, (const char **)source
, (const char *)sourceLimit
,
1041 target
, targetLimit
,
1042 offsets
, sourceIndex
,
1046 return 0; /* an extension mapping handled the input */
1050 if(length
==4 && (cnv
->options
&_MBCS_OPTION_GB18030
)!=0) {
1051 const uint32_t *range
;
1055 linear
=LINEAR_18030(cnv
->toUBytes
[0], cnv
->toUBytes
[1], cnv
->toUBytes
[2], cnv
->toUBytes
[3]);
1056 range
=gb18030Ranges
[0];
1057 for(i
=0; i
<sizeof(gb18030Ranges
)/sizeof(gb18030Ranges
[0]); range
+=4, ++i
) {
1058 if(range
[2]<=linear
&& linear
<=range
[3]) {
1059 /* found the sequence, output the Unicode code point for it */
1060 *pErrorCode
=U_ZERO_ERROR
;
1062 /* add the linear difference between the input and start sequences to the start code point */
1063 linear
=range
[0]+(linear
-range
[2]);
1065 /* output this code point */
1066 ucnv_toUWriteCodePoint(cnv
, linear
, target
, targetLimit
, offsets
, sourceIndex
, pErrorCode
);
1074 *pErrorCode
=U_INVALID_CHAR_FOUND
;
1078 /* EBCDIC swap LF<->NL ------------------------------------------------------ */
1081 * This code modifies a standard EBCDIC<->Unicode mapping table for
1082 * OS/390 (z/OS) Unix System Services (Open Edition).
1083 * The difference is in the mapping of Line Feed and New Line control codes:
1084 * Standard EBCDIC maps
1089 * but OS/390 USS EBCDIC swaps the control codes for LF and NL,
1095 * This code modifies a loaded standard EBCDIC<->Unicode mapping table
1096 * by copying it into allocated memory and swapping the LF and NL values.
1097 * It allows to support the same EBCDIC charset in both versions without
1098 * duplicating the entire installed table.
1101 /* standard EBCDIC codes */
1102 #define EBCDIC_LF 0x25
1103 #define EBCDIC_NL 0x15
1105 /* standard EBCDIC codes with roundtrip flag as stored in Unicode-to-single-byte tables */
1106 #define EBCDIC_RT_LF 0xf25
1107 #define EBCDIC_RT_NL 0xf15
1109 /* Unicode code points */
1114 _EBCDICSwapLFNL(UConverterSharedData
*sharedData
, UErrorCode
*pErrorCode
) {
1115 UConverterMBCSTable
*mbcsTable
;
1117 const uint16_t *table
, *results
;
1118 const uint8_t *bytes
;
1120 int32_t (*newStateTable
)[256];
1121 uint16_t *newResults
;
1125 uint32_t stage2Entry
;
1126 uint32_t size
, sizeofFromUBytes
;
1128 mbcsTable
=&sharedData
->mbcs
;
1130 table
=mbcsTable
->fromUnicodeTable
;
1131 bytes
=mbcsTable
->fromUnicodeBytes
;
1132 results
=(const uint16_t *)bytes
;
1135 * Check that this is an EBCDIC table with SBCS portion -
1136 * SBCS or EBCDIC_STATEFUL with standard EBCDIC LF and NL mappings.
1138 * If not, ignore the option. Options are always ignored if they do not apply.
1141 (mbcsTable
->outputType
==MBCS_OUTPUT_1
|| mbcsTable
->outputType
==MBCS_OUTPUT_2_SISO
) &&
1142 mbcsTable
->stateTable
[0][EBCDIC_LF
]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_LF
) &&
1143 mbcsTable
->stateTable
[0][EBCDIC_NL
]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_NL
)
1148 if(mbcsTable
->outputType
==MBCS_OUTPUT_1
) {
1150 EBCDIC_RT_LF
==MBCS_SINGLE_RESULT_FROM_U(table
, results
, U_LF
) &&
1151 EBCDIC_RT_NL
==MBCS_SINGLE_RESULT_FROM_U(table
, results
, U_NL
)
1155 } else /* MBCS_OUTPUT_2_SISO */ {
1156 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_LF
);
1158 MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, U_LF
)!=0 &&
1159 EBCDIC_LF
==MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, U_LF
)
1164 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_NL
);
1166 MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, U_NL
)!=0 &&
1167 EBCDIC_NL
==MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, U_NL
)
1173 if(mbcsTable
->fromUBytesLength
>0) {
1175 * We _know_ the number of bytes in the fromUnicodeBytes array
1176 * starting with header.version 4.1.
1178 sizeofFromUBytes
=mbcsTable
->fromUBytesLength
;
1182 * There used to be code to enumerate the fromUnicode
1183 * trie and find the highest entry, but it was removed in ICU 3.2
1184 * because it was not tested and caused a low code coverage number.
1185 * See Jitterbug 3674.
1186 * This affects only some .cnv file formats with a header.version
1187 * below 4.1, and only when swaplfnl is requested.
1189 * ucnvmbcs.c revision 1.99 is the last one with the
1190 * ucnv_MBCSSizeofFromUBytes() function.
1192 *pErrorCode
=U_INVALID_FORMAT_ERROR
;
1197 * The table has an appropriate format.
1198 * Allocate and build
1199 * - a modified to-Unicode state table
1200 * - a modified from-Unicode output array
1201 * - a converter name string with the swap option appended
1204 mbcsTable
->countStates
*1024+
1206 UCNV_MAX_CONVERTER_NAME_LENGTH
+20;
1207 p
=(uint8_t *)uprv_malloc(size
);
1209 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1213 /* copy and modify the to-Unicode state table */
1214 newStateTable
=(int32_t (*)[256])p
;
1215 uprv_memcpy(newStateTable
, mbcsTable
->stateTable
, mbcsTable
->countStates
*1024);
1217 newStateTable
[0][EBCDIC_LF
]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_NL
);
1218 newStateTable
[0][EBCDIC_NL
]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_LF
);
1220 /* copy and modify the from-Unicode result table */
1221 newResults
=(uint16_t *)newStateTable
[mbcsTable
->countStates
];
1222 uprv_memcpy(newResults
, bytes
, sizeofFromUBytes
);
1224 /* conveniently, the table access macros work on the left side of expressions */
1225 if(mbcsTable
->outputType
==MBCS_OUTPUT_1
) {
1226 MBCS_SINGLE_RESULT_FROM_U(table
, newResults
, U_LF
)=EBCDIC_RT_NL
;
1227 MBCS_SINGLE_RESULT_FROM_U(table
, newResults
, U_NL
)=EBCDIC_RT_LF
;
1228 } else /* MBCS_OUTPUT_2_SISO */ {
1229 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_LF
);
1230 MBCS_VALUE_2_FROM_STAGE_2(newResults
, stage2Entry
, U_LF
)=EBCDIC_NL
;
1232 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_NL
);
1233 MBCS_VALUE_2_FROM_STAGE_2(newResults
, stage2Entry
, U_NL
)=EBCDIC_LF
;
1236 /* set the canonical converter name */
1237 name
=(char *)newResults
+sizeofFromUBytes
;
1238 uprv_strcpy(name
, sharedData
->staticData
->name
);
1239 uprv_strcat(name
, UCNV_SWAP_LFNL_OPTION_STRING
);
1241 /* set the pointers */
1243 if(mbcsTable
->swapLFNLStateTable
==NULL
) {
1244 mbcsTable
->swapLFNLStateTable
=newStateTable
;
1245 mbcsTable
->swapLFNLFromUnicodeBytes
=(uint8_t *)newResults
;
1246 mbcsTable
->swapLFNLName
=name
;
1252 /* release the allocated memory if another thread beat us to it */
1253 if(newStateTable
!=NULL
) {
1254 uprv_free(newStateTable
);
1259 /* reconstitute omitted fromUnicode data ------------------------------------ */
1261 /* for details, compare with genmbcs.c MBCSAddFromUnicode() and transformEUC() */
1262 static UBool U_CALLCONV
1263 writeStage3Roundtrip(const void *context
, uint32_t value
, UChar32 codePoints
[32]) {
1264 UConverterMBCSTable
*mbcsTable
=(UConverterMBCSTable
*)context
;
1265 const uint16_t *table
;
1271 table
=mbcsTable
->fromUnicodeTable
;
1272 bytes
=(uint8_t *)mbcsTable
->fromUnicodeBytes
;
1274 /* for EUC outputTypes, modify the value like genmbcs.c's transformEUC() */
1275 switch(mbcsTable
->outputType
) {
1276 case MBCS_OUTPUT_3_EUC
:
1278 /* short sequences are stored directly */
1279 /* code set 0 or 1 */
1280 } else if(value
<=0x8effff) {
1283 } else /* first byte is 0x8f */ {
1288 case MBCS_OUTPUT_4_EUC
:
1289 if(value
<=0xffffff) {
1290 /* short sequences are stored directly */
1291 /* code set 0 or 1 */
1292 } else if(value
<=0x8effffff) {
1295 } else /* first byte is 0x8f */ {
1304 for(i
=0; i
<=0x1f; ++value
, ++i
) {
1310 /* locate the stage 2 & 3 data */
1311 stage2
=((uint32_t *)table
)+table
[c
>>10]+((c
>>4)&0x3f);
1313 st3
=(int32_t)(uint16_t)*stage2
*16+(c
&0xf);
1315 /* write the codepage bytes into stage 3 */
1316 switch(mbcsTable
->outputType
) {
1318 case MBCS_OUTPUT_4_EUC
:
1320 p
[0]=(uint8_t)(value
>>16);
1321 p
[1]=(uint8_t)(value
>>8);
1322 p
[2]=(uint8_t)value
;
1325 ((uint32_t *)p
)[st3
]=value
;
1328 /* 2 bytes per character */
1329 ((uint16_t *)p
)[st3
]=(uint16_t)value
;
1333 /* set the roundtrip flag */
1334 *stage2
|=(1UL<<(16+(c
&0xf)));
1340 reconstituteData(UConverterMBCSTable
*mbcsTable
,
1341 uint32_t stage1Length
, uint32_t stage2Length
,
1342 uint32_t fullStage2Length
, /* lengths are numbers of units, not bytes */
1343 UErrorCode
*pErrorCode
) {
1347 uint32_t dataLength
=stage1Length
*2+fullStage2Length
*4+mbcsTable
->fromUBytesLength
;
1348 mbcsTable
->reconstitutedData
=(uint8_t *)uprv_malloc(dataLength
);
1349 if(mbcsTable
->reconstitutedData
==NULL
) {
1350 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1353 uprv_memset(mbcsTable
->reconstitutedData
, 0, dataLength
);
1355 /* copy existing data and reroute the pointers */
1356 stage1
=(uint16_t *)mbcsTable
->reconstitutedData
;
1357 uprv_memcpy(stage1
, mbcsTable
->fromUnicodeTable
, stage1Length
*2);
1359 stage2
=(uint32_t *)(stage1
+stage1Length
);
1360 uprv_memcpy(stage2
+(fullStage2Length
-stage2Length
),
1361 mbcsTable
->fromUnicodeTable
+stage1Length
,
1364 mbcsTable
->fromUnicodeTable
=stage1
;
1365 mbcsTable
->fromUnicodeBytes
=bytes
=(uint8_t *)(stage2
+fullStage2Length
);
1367 /* indexes into stage 2 count from the bottom of the fromUnicodeTable */
1368 stage2
=(uint32_t *)stage1
;
1370 /* reconstitute the initial part of stage 2 from the mbcsIndex */
1372 int32_t stageUTF8Length
=((int32_t)mbcsTable
->maxFastUChar
+1)>>6;
1373 int32_t stageUTF8Index
=0;
1374 int32_t st1
, st2
, st3
, i
;
1376 for(st1
=0; stageUTF8Index
<stageUTF8Length
; ++st1
) {
1378 if(st2
!=stage1Length
/2) {
1379 /* each stage 2 block has 64 entries corresponding to 16 entries in the mbcsIndex */
1380 for(i
=0; i
<16; ++i
) {
1381 st3
=mbcsTable
->mbcsIndex
[stageUTF8Index
++];
1383 /* an stage 2 entry's index is per stage 3 16-block, not per stage 3 entry */
1386 * 4 stage 2 entries point to 4 consecutive stage 3 16-blocks which are
1387 * allocated together as a single 64-block for access from the mbcsIndex
1389 stage2
[st2
++]=st3
++;
1390 stage2
[st2
++]=st3
++;
1391 stage2
[st2
++]=st3
++;
1394 /* no stage 3 block, skip */
1399 /* no stage 2 block, skip */
1405 /* reconstitute fromUnicodeBytes with roundtrips from toUnicode data */
1406 ucnv_MBCSEnumToUnicode(mbcsTable
, writeStage3Roundtrip
, mbcsTable
, pErrorCode
);
1409 /* MBCS setup functions ----------------------------------------------------- */
1412 ucnv_MBCSLoad(UConverterSharedData
*sharedData
,
1413 UConverterLoadArgs
*pArgs
,
1415 UErrorCode
*pErrorCode
) {
1417 UConverterMBCSTable
*mbcsTable
=&sharedData
->mbcs
;
1418 _MBCSHeader
*header
=(_MBCSHeader
*)raw
;
1420 uint32_t headerLength
;
1421 UBool noFromU
=FALSE
;
1423 if(header
->version
[0]==4) {
1424 headerLength
=MBCS_HEADER_V4_LENGTH
;
1425 } else if(header
->version
[0]==5 && header
->version
[1]>=3 &&
1426 (header
->options
&MBCS_OPT_UNKNOWN_INCOMPATIBLE_MASK
)==0) {
1427 headerLength
=header
->options
&MBCS_OPT_LENGTH_MASK
;
1428 noFromU
=(UBool
)((header
->options
&MBCS_OPT_NO_FROM_U
)!=0);
1430 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1434 mbcsTable
->outputType
=(uint8_t)header
->flags
;
1435 if(noFromU
&& mbcsTable
->outputType
==MBCS_OUTPUT_1
) {
1436 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1440 /* extension data, header version 4.2 and higher */
1441 offset
=header
->flags
>>8;
1443 mbcsTable
->extIndexes
=(const int32_t *)(raw
+offset
);
1446 if(mbcsTable
->outputType
==MBCS_OUTPUT_EXT_ONLY
) {
1447 UConverterLoadArgs args
={ 0 };
1448 UConverterSharedData
*baseSharedData
;
1449 const int32_t *extIndexes
;
1450 const char *baseName
;
1452 /* extension-only file, load the base table and set values appropriately */
1453 if((extIndexes
=mbcsTable
->extIndexes
)==NULL
) {
1454 /* extension-only file without extension */
1455 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1459 if(pArgs
->nestedLoads
!=1) {
1460 /* an extension table must not be loaded as a base table */
1461 *pErrorCode
=U_INVALID_TABLE_FILE
;
1465 /* load the base table */
1466 baseName
=(const char *)header
+headerLength
*4;
1467 if(0==uprv_strcmp(baseName
, sharedData
->staticData
->name
)) {
1468 /* forbid loading this same extension-only file */
1469 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1473 /* TODO parse package name out of the prefix of the base name in the extension .cnv file? */
1474 args
.size
=sizeof(UConverterLoadArgs
);
1476 args
.onlyTestIsLoadable
=pArgs
->onlyTestIsLoadable
;
1477 args
.reserved
=pArgs
->reserved
;
1478 args
.options
=pArgs
->options
;
1479 args
.pkg
=pArgs
->pkg
;
1481 baseSharedData
=ucnv_load(&args
, pErrorCode
);
1482 if(U_FAILURE(*pErrorCode
)) {
1485 if( baseSharedData
->staticData
->conversionType
!=UCNV_MBCS
||
1486 baseSharedData
->mbcs
.baseSharedData
!=NULL
1488 ucnv_unload(baseSharedData
);
1489 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1492 if(pArgs
->onlyTestIsLoadable
) {
1494 * Exit as soon as we know that we can load the converter
1495 * and the format is valid and supported.
1496 * The worst that can happen in the following code is a memory
1499 ucnv_unload(baseSharedData
);
1503 /* copy the base table data */
1504 uprv_memcpy(mbcsTable
, &baseSharedData
->mbcs
, sizeof(UConverterMBCSTable
));
1506 /* overwrite values with relevant ones for the extension converter */
1507 mbcsTable
->baseSharedData
=baseSharedData
;
1508 mbcsTable
->extIndexes
=extIndexes
;
1511 * It would be possible to share the swapLFNL data with a base converter,
1512 * but the generated name would have to be different, and the memory
1513 * would have to be free'd only once.
1514 * It is easier to just create the data for the extension converter
1515 * separately when it is requested.
1517 mbcsTable
->swapLFNLStateTable
=NULL
;
1518 mbcsTable
->swapLFNLFromUnicodeBytes
=NULL
;
1519 mbcsTable
->swapLFNLName
=NULL
;
1522 * The reconstitutedData must be deleted only when the base converter
1525 mbcsTable
->reconstitutedData
=NULL
;
1528 * Set a special, runtime-only outputType if the extension converter
1529 * is a DBCS version of a base converter that also maps single bytes.
1531 if( sharedData
->staticData
->conversionType
==UCNV_DBCS
||
1532 (sharedData
->staticData
->conversionType
==UCNV_MBCS
&&
1533 sharedData
->staticData
->minBytesPerChar
>=2)
1535 if(baseSharedData
->mbcs
.outputType
==MBCS_OUTPUT_2_SISO
) {
1536 /* the base converter is SI/SO-stateful */
1539 /* get the dbcs state from the state table entry for SO=0x0e */
1540 entry
=mbcsTable
->stateTable
[0][0xe];
1541 if( MBCS_ENTRY_IS_FINAL(entry
) &&
1542 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_CHANGE_ONLY
&&
1543 MBCS_ENTRY_FINAL_STATE(entry
)!=0
1545 mbcsTable
->dbcsOnlyState
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
);
1547 mbcsTable
->outputType
=MBCS_OUTPUT_DBCS_ONLY
;
1550 baseSharedData
->staticData
->conversionType
==UCNV_MBCS
&&
1551 baseSharedData
->staticData
->minBytesPerChar
==1 &&
1552 baseSharedData
->staticData
->maxBytesPerChar
==2 &&
1553 mbcsTable
->countStates
<=127
1555 /* non-stateful base converter, need to modify the state table */
1556 int32_t (*newStateTable
)[256];
1560 /* allocate a new state table and copy the base state table contents */
1561 count
=mbcsTable
->countStates
;
1562 newStateTable
=(int32_t (*)[256])uprv_malloc((count
+1)*1024);
1563 if(newStateTable
==NULL
) {
1564 ucnv_unload(baseSharedData
);
1565 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1569 uprv_memcpy(newStateTable
, mbcsTable
->stateTable
, count
*1024);
1571 /* change all final single-byte entries to go to a new all-illegal state */
1572 state
=newStateTable
[0];
1573 for(i
=0; i
<256; ++i
) {
1574 if(MBCS_ENTRY_IS_FINAL(state
[i
])) {
1575 state
[i
]=MBCS_ENTRY_TRANSITION(count
, 0);
1579 /* build the new all-illegal state */
1580 state
=newStateTable
[count
];
1581 for(i
=0; i
<256; ++i
) {
1582 state
[i
]=MBCS_ENTRY_FINAL(0, MBCS_STATE_ILLEGAL
, 0);
1584 mbcsTable
->stateTable
=(const int32_t (*)[256])newStateTable
;
1585 mbcsTable
->countStates
=(uint8_t)(count
+1);
1586 mbcsTable
->stateTableOwned
=TRUE
;
1588 mbcsTable
->outputType
=MBCS_OUTPUT_DBCS_ONLY
;
1593 * unlike below for files with base tables, do not get the unicodeMask
1594 * from the sharedData; instead, use the base table's unicodeMask,
1595 * which we copied in the memcpy above;
1596 * this is necessary because the static data unicodeMask, especially
1597 * the UCNV_HAS_SUPPLEMENTARY flag, is part of the base table data
1600 /* conversion file with a base table; an additional extension table is optional */
1601 /* make sure that the output type is known */
1602 switch(mbcsTable
->outputType
) {
1607 case MBCS_OUTPUT_3_EUC
:
1608 case MBCS_OUTPUT_4_EUC
:
1609 case MBCS_OUTPUT_2_SISO
:
1613 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1616 if(pArgs
->onlyTestIsLoadable
) {
1618 * Exit as soon as we know that we can load the converter
1619 * and the format is valid and supported.
1620 * The worst that can happen in the following code is a memory
1626 mbcsTable
->countStates
=(uint8_t)header
->countStates
;
1627 mbcsTable
->countToUFallbacks
=header
->countToUFallbacks
;
1628 mbcsTable
->stateTable
=(const int32_t (*)[256])(raw
+headerLength
*4);
1629 mbcsTable
->toUFallbacks
=(const _MBCSToUFallback
*)(mbcsTable
->stateTable
+header
->countStates
);
1630 mbcsTable
->unicodeCodeUnits
=(const uint16_t *)(raw
+header
->offsetToUCodeUnits
);
1632 mbcsTable
->fromUnicodeTable
=(const uint16_t *)(raw
+header
->offsetFromUTable
);
1633 mbcsTable
->fromUnicodeBytes
=(const uint8_t *)(raw
+header
->offsetFromUBytes
);
1634 mbcsTable
->fromUBytesLength
=header
->fromUBytesLength
;
1637 * converter versions 6.1 and up contain a unicodeMask that is
1638 * used here to select the most efficient function implementations
1640 info
.size
=sizeof(UDataInfo
);
1641 udata_getInfo((UDataMemory
*)sharedData
->dataMemory
, &info
);
1642 if(info
.formatVersion
[0]>6 || (info
.formatVersion
[0]==6 && info
.formatVersion
[1]>=1)) {
1643 /* mask off possible future extensions to be safe */
1644 mbcsTable
->unicodeMask
=(uint8_t)(sharedData
->staticData
->unicodeMask
&3);
1646 /* for older versions, assume worst case: contains anything possible (prevent over-optimizations) */
1647 mbcsTable
->unicodeMask
=UCNV_HAS_SUPPLEMENTARY
|UCNV_HAS_SURROGATES
;
1651 * _MBCSHeader.version 4.3 adds utf8Friendly data structures.
1652 * Check for the header version, SBCS vs. MBCS, and for whether the
1653 * data structures are optimized for code points as high as what the
1654 * runtime code is designed for.
1655 * The implementation does not handle mapping tables with entries for
1656 * unpaired surrogates.
1658 if( header
->version
[1]>=3 &&
1659 (mbcsTable
->unicodeMask
&UCNV_HAS_SURROGATES
)==0 &&
1660 (mbcsTable
->countStates
==1 ?
1661 (header
->version
[2]>=(SBCS_FAST_MAX
>>8)) :
1662 (header
->version
[2]>=(MBCS_FAST_MAX
>>8))
1665 mbcsTable
->utf8Friendly
=TRUE
;
1667 if(mbcsTable
->countStates
==1) {
1669 * SBCS: Stage 3 is allocated in 64-entry blocks for U+0000..SBCS_FAST_MAX or higher.
1670 * Build a table with indexes to each block, to be used instead of
1671 * the regular stage 1/2 table.
1674 for(i
=0; i
<(SBCS_FAST_LIMIT
>>6); ++i
) {
1675 mbcsTable
->sbcsIndex
[i
]=mbcsTable
->fromUnicodeTable
[mbcsTable
->fromUnicodeTable
[i
>>4]+((i
<<2)&0x3c)];
1677 /* set SBCS_FAST_MAX to reflect the reach of sbcsIndex[] even if header->version[2]>(SBCS_FAST_MAX>>8) */
1678 mbcsTable
->maxFastUChar
=SBCS_FAST_MAX
;
1681 * MBCS: Stage 3 is allocated in 64-entry blocks for U+0000..MBCS_FAST_MAX or higher.
1682 * The .cnv file is prebuilt with an additional stage table with indexes
1685 mbcsTable
->mbcsIndex
=(const uint16_t *)
1686 (mbcsTable
->fromUnicodeBytes
+
1687 (noFromU
? 0 : mbcsTable
->fromUBytesLength
));
1688 mbcsTable
->maxFastUChar
=(((UChar
)header
->version
[2])<<8)|0xff;
1692 /* calculate a bit set of 4 ASCII characters per bit that round-trip to ASCII bytes */
1694 uint32_t asciiRoundtrips
=0xffffffff;
1697 for(i
=0; i
<0x80; ++i
) {
1698 if(mbcsTable
->stateTable
[0][i
]!=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, i
)) {
1699 asciiRoundtrips
&=~((uint32_t)1<<(i
>>2));
1702 mbcsTable
->asciiRoundtrips
=asciiRoundtrips
;
1706 uint32_t stage1Length
=
1707 mbcsTable
->unicodeMask
&UCNV_HAS_SUPPLEMENTARY
?
1709 uint32_t stage2Length
=
1710 (header
->offsetFromUBytes
-header
->offsetFromUTable
)/4-
1712 reconstituteData(mbcsTable
, stage1Length
, stage2Length
, header
->fullStage2Length
, pErrorCode
);
1716 /* Set the impl pointer here so that it is set for both extension-only and base tables. */
1717 if(mbcsTable
->utf8Friendly
) {
1718 if(mbcsTable
->countStates
==1) {
1719 sharedData
->impl
=&_SBCSUTF8Impl
;
1721 if(mbcsTable
->outputType
==MBCS_OUTPUT_2
) {
1722 sharedData
->impl
=&_DBCSUTF8Impl
;
1727 if(mbcsTable
->outputType
==MBCS_OUTPUT_DBCS_ONLY
|| mbcsTable
->outputType
==MBCS_OUTPUT_2_SISO
) {
1729 * MBCS_OUTPUT_DBCS_ONLY: No SBCS mappings, therefore ASCII does not roundtrip.
1730 * MBCS_OUTPUT_2_SISO: Bypass the ASCII fastpath to handle prevLength correctly.
1732 mbcsTable
->asciiRoundtrips
=0;
1737 ucnv_MBCSUnload(UConverterSharedData
*sharedData
) {
1738 UConverterMBCSTable
*mbcsTable
=&sharedData
->mbcs
;
1740 if(mbcsTable
->swapLFNLStateTable
!=NULL
) {
1741 uprv_free(mbcsTable
->swapLFNLStateTable
);
1743 if(mbcsTable
->stateTableOwned
) {
1744 uprv_free((void *)mbcsTable
->stateTable
);
1746 if(mbcsTable
->baseSharedData
!=NULL
) {
1747 ucnv_unload(mbcsTable
->baseSharedData
);
1749 if(mbcsTable
->reconstitutedData
!=NULL
) {
1750 uprv_free(mbcsTable
->reconstitutedData
);
1755 ucnv_MBCSOpen(UConverter
*cnv
,
1756 UConverterLoadArgs
*pArgs
,
1757 UErrorCode
*pErrorCode
) {
1758 UConverterMBCSTable
*mbcsTable
;
1759 const int32_t *extIndexes
;
1761 int8_t maxBytesPerUChar
;
1763 if(pArgs
->onlyTestIsLoadable
) {
1767 mbcsTable
=&cnv
->sharedData
->mbcs
;
1768 outputType
=mbcsTable
->outputType
;
1770 if(outputType
==MBCS_OUTPUT_DBCS_ONLY
) {
1771 /* the swaplfnl option does not apply, remove it */
1772 cnv
->options
=pArgs
->options
&=~UCNV_OPTION_SWAP_LFNL
;
1775 if((pArgs
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
1776 /* do this because double-checked locking is broken */
1780 isCached
=mbcsTable
->swapLFNLStateTable
!=NULL
;
1784 if(!_EBCDICSwapLFNL(cnv
->sharedData
, pErrorCode
)) {
1785 if(U_FAILURE(*pErrorCode
)) {
1786 return; /* something went wrong */
1789 /* the option does not apply, remove it */
1790 cnv
->options
=pArgs
->options
&=~UCNV_OPTION_SWAP_LFNL
;
1795 if(uprv_strstr(pArgs
->name
, "18030")!=NULL
) {
1796 if(uprv_strstr(pArgs
->name
, "gb18030")!=NULL
|| uprv_strstr(pArgs
->name
, "GB18030")!=NULL
) {
1797 /* set a flag for GB 18030 mode, which changes the callback behavior */
1798 cnv
->options
|=_MBCS_OPTION_GB18030
;
1800 } else if((uprv_strstr(pArgs
->name
, "KEIS")!=NULL
) || (uprv_strstr(pArgs
->name
, "keis")!=NULL
)) {
1801 /* set a flag for KEIS converter, which changes the SI/SO character sequence */
1802 cnv
->options
|=_MBCS_OPTION_KEIS
;
1803 } else if((uprv_strstr(pArgs
->name
, "JEF")!=NULL
) || (uprv_strstr(pArgs
->name
, "jef")!=NULL
)) {
1804 /* set a flag for JEF converter, which changes the SI/SO character sequence */
1805 cnv
->options
|=_MBCS_OPTION_JEF
;
1806 } else if((uprv_strstr(pArgs
->name
, "JIPS")!=NULL
) || (uprv_strstr(pArgs
->name
, "jips")!=NULL
)) {
1807 /* set a flag for JIPS converter, which changes the SI/SO character sequence */
1808 cnv
->options
|=_MBCS_OPTION_JIPS
;
1811 /* fix maxBytesPerUChar depending on outputType and options etc. */
1812 if(outputType
==MBCS_OUTPUT_2_SISO
) {
1813 cnv
->maxBytesPerUChar
=3; /* SO+DBCS */
1816 extIndexes
=mbcsTable
->extIndexes
;
1817 if(extIndexes
!=NULL
) {
1818 maxBytesPerUChar
=(int8_t)UCNV_GET_MAX_BYTES_PER_UCHAR(extIndexes
);
1819 if(outputType
==MBCS_OUTPUT_2_SISO
) {
1820 ++maxBytesPerUChar
; /* SO + multiple DBCS */
1823 if(maxBytesPerUChar
>cnv
->maxBytesPerUChar
) {
1824 cnv
->maxBytesPerUChar
=maxBytesPerUChar
;
1830 * documentation of UConverter fields used for status
1831 * all of these fields are (re)set to 0 by ucnv_bld.c and ucnv_reset()
1835 cnv
->toUnicodeStatus
=0; /* offset */
1836 cnv
->mode
=0; /* state */
1837 cnv
->toULength
=0; /* byteIndex */
1841 cnv
->fromUnicodeStatus
=1; /* prevLength */
1846 ucnv_MBCSGetName(const UConverter
*cnv
) {
1847 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0 && cnv
->sharedData
->mbcs
.swapLFNLName
!=NULL
) {
1848 return cnv
->sharedData
->mbcs
.swapLFNLName
;
1850 return cnv
->sharedData
->staticData
->name
;
1854 /* MBCS-to-Unicode conversion functions ------------------------------------- */
1857 ucnv_MBCSGetFallback(UConverterMBCSTable
*mbcsTable
, uint32_t offset
) {
1858 const _MBCSToUFallback
*toUFallbacks
;
1859 uint32_t i
, start
, limit
;
1861 limit
=mbcsTable
->countToUFallbacks
;
1863 /* do a binary search for the fallback mapping */
1864 toUFallbacks
=mbcsTable
->toUFallbacks
;
1866 while(start
<limit
-1) {
1868 if(offset
<toUFallbacks
[i
].offset
) {
1875 /* did we really find it? */
1876 if(offset
==toUFallbacks
[start
].offset
) {
1877 return toUFallbacks
[start
].codePoint
;
1884 /* This version of ucnv_MBCSToUnicodeWithOffsets() is optimized for single-byte, single-state codepages. */
1886 ucnv_MBCSSingleToUnicodeWithOffsets(UConverterToUnicodeArgs
*pArgs
,
1887 UErrorCode
*pErrorCode
) {
1889 const uint8_t *source
, *sourceLimit
;
1891 const UChar
*targetLimit
;
1894 const int32_t (*stateTable
)[256];
1896 int32_t sourceIndex
;
1902 /* set up the local pointers */
1903 cnv
=pArgs
->converter
;
1904 source
=(const uint8_t *)pArgs
->source
;
1905 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
1906 target
=pArgs
->target
;
1907 targetLimit
=pArgs
->targetLimit
;
1908 offsets
=pArgs
->offsets
;
1910 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
1911 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
1913 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
1916 /* sourceIndex=-1 if the current character began in the previous buffer */
1919 /* conversion loop */
1920 while(source
<sourceLimit
) {
1922 * This following test is to see if available input would overflow the output.
1923 * It does not catch output of more than one code unit that
1924 * overflows as a result of a surrogate pair or callback output
1925 * from the last source byte.
1926 * Therefore, those situations also test for overflows and will
1927 * then break the loop, too.
1929 if(target
>=targetLimit
) {
1930 /* target is full */
1931 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1935 entry
=stateTable
[0][*source
++];
1936 /* MBCS_ENTRY_IS_FINAL(entry) */
1938 /* test the most common case first */
1939 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
1940 /* output BMP code point */
1941 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1943 *offsets
++=sourceIndex
;
1946 /* normal end of action codes: prepare for a new character */
1952 * An if-else-if chain provides more reliable performance for
1953 * the most common cases compared to a switch.
1955 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
1956 if(action
==MBCS_STATE_VALID_DIRECT_20
||
1957 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
1959 entry
=MBCS_ENTRY_FINAL_VALUE(entry
);
1960 /* output surrogate pair */
1961 *target
++=(UChar
)(0xd800|(UChar
)(entry
>>10));
1963 *offsets
++=sourceIndex
;
1965 c
=(UChar
)(0xdc00|(UChar
)(entry
&0x3ff));
1966 if(target
<targetLimit
) {
1969 *offsets
++=sourceIndex
;
1972 /* target overflow */
1973 cnv
->UCharErrorBuffer
[0]=c
;
1974 cnv
->UCharErrorBufferLength
=1;
1975 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1981 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
1982 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
1983 /* output BMP code point */
1984 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1986 *offsets
++=sourceIndex
;
1992 } else if(action
==MBCS_STATE_UNASSIGNED
) {
1993 /* just fall through */
1994 } else if(action
==MBCS_STATE_ILLEGAL
) {
1995 /* callback(illegal) */
1996 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
1998 /* reserved, must never occur */
2003 if(U_FAILURE(*pErrorCode
)) {
2004 /* callback(illegal) */
2006 } else /* unassigned sequences indicated with byteIndex>0 */ {
2007 /* try an extension mapping */
2008 pArgs
->source
=(const char *)source
;
2009 cnv
->toUBytes
[0]=*(source
-1);
2010 cnv
->toULength
=_extToU(cnv
, cnv
->sharedData
,
2011 1, &source
, sourceLimit
,
2012 &target
, targetLimit
,
2013 &offsets
, sourceIndex
,
2016 sourceIndex
+=1+(int32_t)(source
-(const uint8_t *)pArgs
->source
);
2018 if(U_FAILURE(*pErrorCode
)) {
2019 /* not mappable or buffer overflow */
2025 /* write back the updated pointers */
2026 pArgs
->source
=(const char *)source
;
2027 pArgs
->target
=target
;
2028 pArgs
->offsets
=offsets
;
2032 * This version of ucnv_MBCSSingleToUnicodeWithOffsets() is optimized for single-byte, single-state codepages
2033 * that only map to and from the BMP.
2034 * In addition to single-byte optimizations, the offset calculations
2035 * become much easier.
2038 ucnv_MBCSSingleToBMPWithOffsets(UConverterToUnicodeArgs
*pArgs
,
2039 UErrorCode
*pErrorCode
) {
2041 const uint8_t *source
, *sourceLimit
, *lastSource
;
2043 int32_t targetCapacity
, length
;
2046 const int32_t (*stateTable
)[256];
2048 int32_t sourceIndex
;
2053 /* set up the local pointers */
2054 cnv
=pArgs
->converter
;
2055 source
=(const uint8_t *)pArgs
->source
;
2056 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
2057 target
=pArgs
->target
;
2058 targetCapacity
=(int32_t)(pArgs
->targetLimit
-pArgs
->target
);
2059 offsets
=pArgs
->offsets
;
2061 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2062 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
2064 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
2067 /* sourceIndex=-1 if the current character began in the previous buffer */
2072 * since the conversion here is 1:1 UChar:uint8_t, we need only one counter
2073 * for the minimum of the sourceLength and targetCapacity
2075 length
=(int32_t)(sourceLimit
-source
);
2076 if(length
<targetCapacity
) {
2077 targetCapacity
=length
;
2080 #if MBCS_UNROLL_SINGLE_TO_BMP
2081 /* unrolling makes it faster on Pentium III/Windows 2000 */
2082 /* unroll the loop with the most common case */
2084 if(targetCapacity
>=16) {
2085 int32_t count
, loops
, oredEntries
;
2087 loops
=count
=targetCapacity
>>4;
2089 oredEntries
=entry
=stateTable
[0][*source
++];
2090 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2091 oredEntries
|=entry
=stateTable
[0][*source
++];
2092 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2093 oredEntries
|=entry
=stateTable
[0][*source
++];
2094 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2095 oredEntries
|=entry
=stateTable
[0][*source
++];
2096 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2097 oredEntries
|=entry
=stateTable
[0][*source
++];
2098 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2099 oredEntries
|=entry
=stateTable
[0][*source
++];
2100 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2101 oredEntries
|=entry
=stateTable
[0][*source
++];
2102 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2103 oredEntries
|=entry
=stateTable
[0][*source
++];
2104 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2105 oredEntries
|=entry
=stateTable
[0][*source
++];
2106 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2107 oredEntries
|=entry
=stateTable
[0][*source
++];
2108 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2109 oredEntries
|=entry
=stateTable
[0][*source
++];
2110 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2111 oredEntries
|=entry
=stateTable
[0][*source
++];
2112 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2113 oredEntries
|=entry
=stateTable
[0][*source
++];
2114 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2115 oredEntries
|=entry
=stateTable
[0][*source
++];
2116 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2117 oredEntries
|=entry
=stateTable
[0][*source
++];
2118 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2119 oredEntries
|=entry
=stateTable
[0][*source
++];
2120 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2122 /* were all 16 entries really valid? */
2123 if(!MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(oredEntries
)) {
2124 /* no, return to the first of these 16 */
2131 targetCapacity
-=16*count
;
2134 lastSource
+=16*count
;
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
++;
2149 *offsets
++=sourceIndex
++;
2150 *offsets
++=sourceIndex
++;
2151 *offsets
++=sourceIndex
++;
2158 /* conversion loop */
2159 while(targetCapacity
> 0 && source
< sourceLimit
) {
2160 entry
=stateTable
[0][*source
++];
2161 /* MBCS_ENTRY_IS_FINAL(entry) */
2163 /* test the most common case first */
2164 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
2165 /* output BMP code point */
2166 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2172 * An if-else-if chain provides more reliable performance for
2173 * the most common cases compared to a switch.
2175 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2176 if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2177 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
2178 /* output BMP code point */
2179 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2183 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2184 /* just fall through */
2185 } else if(action
==MBCS_STATE_ILLEGAL
) {
2186 /* callback(illegal) */
2187 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2189 /* reserved, must never occur */
2193 /* set offsets since the start or the last extension */
2195 int32_t count
=(int32_t)(source
-lastSource
);
2197 /* predecrement: do not set the offset for the callback-causing character */
2199 *offsets
++=sourceIndex
++;
2201 /* offset and sourceIndex are now set for the current character */
2204 if(U_FAILURE(*pErrorCode
)) {
2205 /* callback(illegal) */
2207 } else /* unassigned sequences indicated with byteIndex>0 */ {
2208 /* try an extension mapping */
2210 cnv
->toUBytes
[0]=*(source
-1);
2211 cnv
->toULength
=_extToU(cnv
, cnv
->sharedData
,
2212 1, &source
, sourceLimit
,
2213 &target
, pArgs
->targetLimit
,
2214 &offsets
, sourceIndex
,
2217 sourceIndex
+=1+(int32_t)(source
-lastSource
);
2219 if(U_FAILURE(*pErrorCode
)) {
2220 /* not mappable or buffer overflow */
2224 /* recalculate the targetCapacity after an extension mapping */
2225 targetCapacity
=(int32_t)(pArgs
->targetLimit
-target
);
2226 length
=(int32_t)(sourceLimit
-source
);
2227 if(length
<targetCapacity
) {
2228 targetCapacity
=length
;
2232 #if MBCS_UNROLL_SINGLE_TO_BMP
2233 /* unrolling makes it faster on Pentium III/Windows 2000 */
2238 if(U_SUCCESS(*pErrorCode
) && source
<sourceLimit
&& target
>=pArgs
->targetLimit
) {
2239 /* target is full */
2240 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2243 /* set offsets since the start or the last callback */
2245 size_t count
=source
-lastSource
;
2247 *offsets
++=sourceIndex
++;
2252 /* write back the updated pointers */
2253 pArgs
->source
=(const char *)source
;
2254 pArgs
->target
=target
;
2255 pArgs
->offsets
=offsets
;
2259 hasValidTrailBytes(const int32_t (*stateTable
)[256], uint8_t state
) {
2260 const int32_t *row
=stateTable
[state
];
2262 /* First test for final entries in this state for some commonly valid byte values. */
2264 if( !MBCS_ENTRY_IS_TRANSITION(entry
) &&
2265 MBCS_ENTRY_FINAL_ACTION(entry
)!=MBCS_STATE_ILLEGAL
2270 if( !MBCS_ENTRY_IS_TRANSITION(entry
) &&
2271 MBCS_ENTRY_FINAL_ACTION(entry
)!=MBCS_STATE_ILLEGAL
2275 /* Then test for final entries in this state. */
2276 for(b
=0; b
<=0xff; ++b
) {
2278 if( !MBCS_ENTRY_IS_TRANSITION(entry
) &&
2279 MBCS_ENTRY_FINAL_ACTION(entry
)!=MBCS_STATE_ILLEGAL
2284 /* Then recurse for transition entries. */
2285 for(b
=0; b
<=0xff; ++b
) {
2287 if( MBCS_ENTRY_IS_TRANSITION(entry
) &&
2288 hasValidTrailBytes(stateTable
, (uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
))
2297 * Is byte b a single/lead byte in this state?
2298 * Recurse for transition states, because here we don't want to say that
2299 * b is a lead byte if all byte sequences that start with b are illegal.
2302 isSingleOrLead(const int32_t (*stateTable
)[256], uint8_t state
, UBool isDBCSOnly
, uint8_t b
) {
2303 const int32_t *row
=stateTable
[state
];
2304 int32_t entry
=row
[b
];
2305 if(MBCS_ENTRY_IS_TRANSITION(entry
)) { /* lead byte */
2306 return hasValidTrailBytes(stateTable
, (uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
));
2308 uint8_t action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2309 if(action
==MBCS_STATE_CHANGE_ONLY
&& isDBCSOnly
) {
2310 return FALSE
; /* SI/SO are illegal for DBCS-only conversion */
2312 return action
!=MBCS_STATE_ILLEGAL
;
2318 ucnv_MBCSToUnicodeWithOffsets(UConverterToUnicodeArgs
*pArgs
,
2319 UErrorCode
*pErrorCode
) {
2321 const uint8_t *source
, *sourceLimit
;
2323 const UChar
*targetLimit
;
2326 const int32_t (*stateTable
)[256];
2327 const uint16_t *unicodeCodeUnits
;
2334 int32_t sourceIndex
, nextSourceIndex
;
2340 /* use optimized function if possible */
2341 cnv
=pArgs
->converter
;
2343 if(cnv
->preToULength
>0) {
2345 * pass sourceIndex=-1 because we continue from an earlier buffer
2346 * in the future, this may change with continuous offsets
2348 ucnv_extContinueMatchToU(cnv
, pArgs
, -1, pErrorCode
);
2350 if(U_FAILURE(*pErrorCode
) || cnv
->preToULength
<0) {
2355 if(cnv
->sharedData
->mbcs
.countStates
==1) {
2356 if(!(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
2357 ucnv_MBCSSingleToBMPWithOffsets(pArgs
, pErrorCode
);
2359 ucnv_MBCSSingleToUnicodeWithOffsets(pArgs
, pErrorCode
);
2364 /* set up the local pointers */
2365 source
=(const uint8_t *)pArgs
->source
;
2366 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
2367 target
=pArgs
->target
;
2368 targetLimit
=pArgs
->targetLimit
;
2369 offsets
=pArgs
->offsets
;
2371 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2372 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
2374 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
2376 unicodeCodeUnits
=cnv
->sharedData
->mbcs
.unicodeCodeUnits
;
2378 /* get the converter state from UConverter */
2379 offset
=cnv
->toUnicodeStatus
;
2380 byteIndex
=cnv
->toULength
;
2381 bytes
=cnv
->toUBytes
;
2384 * if we are in the SBCS state for a DBCS-only converter,
2385 * then load the DBCS state from the MBCS data
2386 * (dbcsOnlyState==0 if it is not a DBCS-only converter)
2388 if((state
=(uint8_t)(cnv
->mode
))==0) {
2389 state
=cnv
->sharedData
->mbcs
.dbcsOnlyState
;
2392 /* sourceIndex=-1 if the current character began in the previous buffer */
2393 sourceIndex
=byteIndex
==0 ? 0 : -1;
2396 /* conversion loop */
2397 while(source
<sourceLimit
) {
2399 * This following test is to see if available input would overflow the output.
2400 * It does not catch output of more than one code unit that
2401 * overflows as a result of a surrogate pair or callback output
2402 * from the last source byte.
2403 * Therefore, those situations also test for overflows and will
2404 * then break the loop, too.
2406 if(target
>=targetLimit
) {
2407 /* target is full */
2408 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2413 /* optimized loop for 1/2-byte input and BMP output */
2416 entry
=stateTable
[state
][*source
];
2417 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
2418 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
2419 offset
=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
2422 if( source
<sourceLimit
&&
2423 MBCS_ENTRY_IS_FINAL(entry
=stateTable
[state
][*source
]) &&
2424 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_VALID_16
&&
2425 (c
=unicodeCodeUnits
[offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
)])<0xfffe
2429 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2432 /* set the state and leave the optimized loop */
2433 bytes
[0]=*(source
-1);
2438 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
2439 /* output BMP code point */
2441 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2442 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2444 /* leave the optimized loop */
2448 } while(source
<sourceLimit
&& target
<targetLimit
);
2449 } else /* offsets!=NULL */ {
2451 entry
=stateTable
[state
][*source
];
2452 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
2453 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
2454 offset
=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
2457 if( source
<sourceLimit
&&
2458 MBCS_ENTRY_IS_FINAL(entry
=stateTable
[state
][*source
]) &&
2459 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_VALID_16
&&
2460 (c
=unicodeCodeUnits
[offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
)])<0xfffe
2465 *offsets
++=sourceIndex
;
2466 sourceIndex
=(nextSourceIndex
+=2);
2468 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2471 /* set the state and leave the optimized loop */
2473 bytes
[0]=*(source
-1);
2478 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
2479 /* output BMP code point */
2481 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2483 *offsets
++=sourceIndex
;
2484 sourceIndex
=++nextSourceIndex
;
2486 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2488 /* leave the optimized loop */
2492 } while(source
<sourceLimit
&& target
<targetLimit
);
2496 * these tests and break statements could be put inside the loop
2497 * if C had "break outerLoop" like Java
2499 if(source
>=sourceLimit
) {
2502 if(target
>=targetLimit
) {
2503 /* target is full */
2504 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2509 bytes
[byteIndex
++]=*source
++;
2510 } else /* byteIndex>0 */ {
2512 entry
=stateTable
[state
][bytes
[byteIndex
++]=*source
++];
2515 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
2516 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
2517 offset
+=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
2521 /* save the previous state for proper extension mapping with SI/SO-stateful converters */
2524 /* set the next state early so that we can reuse the entry variable */
2525 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2528 * An if-else-if chain provides more reliable performance for
2529 * the most common cases compared to a switch.
2531 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2532 if(action
==MBCS_STATE_VALID_16
) {
2533 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
2534 c
=unicodeCodeUnits
[offset
];
2536 /* output BMP code point */
2539 *offsets
++=sourceIndex
;
2542 } else if(c
==0xfffe) {
2543 if(UCNV_TO_U_USE_FALLBACK(cnv
) && (entry
=(int32_t)ucnv_MBCSGetFallback(&cnv
->sharedData
->mbcs
, offset
))!=0xfffe) {
2544 /* output fallback BMP code point */
2545 *target
++=(UChar
)entry
;
2547 *offsets
++=sourceIndex
;
2552 /* callback(illegal) */
2553 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2555 } else if(action
==MBCS_STATE_VALID_DIRECT_16
) {
2556 /* output BMP code point */
2557 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2559 *offsets
++=sourceIndex
;
2562 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
2563 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
2564 c
=unicodeCodeUnits
[offset
++];
2566 /* output BMP code point below 0xd800 */
2569 *offsets
++=sourceIndex
;
2572 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? c
<=0xdfff : c
<=0xdbff) {
2573 /* output roundtrip or fallback surrogate pair */
2574 *target
++=(UChar
)(c
&0xdbff);
2576 *offsets
++=sourceIndex
;
2579 if(target
<targetLimit
) {
2580 *target
++=unicodeCodeUnits
[offset
];
2582 *offsets
++=sourceIndex
;
2585 /* target overflow */
2586 cnv
->UCharErrorBuffer
[0]=unicodeCodeUnits
[offset
];
2587 cnv
->UCharErrorBufferLength
=1;
2588 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2593 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? (c
&0xfffe)==0xe000 : c
==0xe000) {
2594 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
2595 *target
++=unicodeCodeUnits
[offset
];
2597 *offsets
++=sourceIndex
;
2600 } else if(c
==0xffff) {
2601 /* callback(illegal) */
2602 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2604 } else if(action
==MBCS_STATE_VALID_DIRECT_20
||
2605 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
2607 entry
=MBCS_ENTRY_FINAL_VALUE(entry
);
2608 /* output surrogate pair */
2609 *target
++=(UChar
)(0xd800|(UChar
)(entry
>>10));
2611 *offsets
++=sourceIndex
;
2614 c
=(UChar
)(0xdc00|(UChar
)(entry
&0x3ff));
2615 if(target
<targetLimit
) {
2618 *offsets
++=sourceIndex
;
2621 /* target overflow */
2622 cnv
->UCharErrorBuffer
[0]=c
;
2623 cnv
->UCharErrorBufferLength
=1;
2624 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2629 } else if(action
==MBCS_STATE_CHANGE_ONLY
) {
2631 * This serves as a state change without any output.
2632 * It is useful for reading simple stateful encodings,
2633 * for example using just Shift-In/Shift-Out codes.
2634 * The 21 unused bits may later be used for more sophisticated
2635 * state transitions.
2637 if(cnv
->sharedData
->mbcs
.dbcsOnlyState
==0) {
2640 /* SI/SO are illegal for DBCS-only conversion */
2641 state
=(uint8_t)(cnv
->mode
); /* restore the previous state */
2643 /* callback(illegal) */
2644 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2646 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2647 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
2648 /* output BMP code point */
2649 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2651 *offsets
++=sourceIndex
;
2655 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2656 /* just fall through */
2657 } else if(action
==MBCS_STATE_ILLEGAL
) {
2658 /* callback(illegal) */
2659 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2661 /* reserved, must never occur */
2665 /* end of action codes: prepare for a new character */
2669 sourceIndex
=nextSourceIndex
;
2670 } else if(U_FAILURE(*pErrorCode
)) {
2671 /* callback(illegal) */
2674 * Ticket 5691: consistent illegal sequences:
2675 * - We include at least the first byte in the illegal sequence.
2676 * - If any of the non-initial bytes could be the start of a character,
2677 * we stop the illegal sequence before the first one of those.
2679 UBool isDBCSOnly
=(UBool
)(cnv
->sharedData
->mbcs
.dbcsOnlyState
!=0);
2682 i
<byteIndex
&& !isSingleOrLead(stateTable
, state
, isDBCSOnly
, bytes
[i
]);
2685 /* Back out some bytes. */
2686 int8_t backOutDistance
=byteIndex
-i
;
2687 int32_t bytesFromThisBuffer
=(int32_t)(source
-(const uint8_t *)pArgs
->source
);
2688 byteIndex
=i
; /* length of reported illegal byte sequence */
2689 if(backOutDistance
<=bytesFromThisBuffer
) {
2690 source
-=backOutDistance
;
2692 /* Back out bytes from the previous buffer: Need to replay them. */
2693 cnv
->preToULength
=(int8_t)(bytesFromThisBuffer
-backOutDistance
);
2694 /* preToULength is negative! */
2695 uprv_memcpy(cnv
->preToU
, bytes
+i
, -cnv
->preToULength
);
2696 source
=(const uint8_t *)pArgs
->source
;
2701 } else /* unassigned sequences indicated with byteIndex>0 */ {
2702 /* try an extension mapping */
2703 pArgs
->source
=(const char *)source
;
2704 byteIndex
=_extToU(cnv
, cnv
->sharedData
,
2705 byteIndex
, &source
, sourceLimit
,
2706 &target
, targetLimit
,
2707 &offsets
, sourceIndex
,
2710 sourceIndex
=nextSourceIndex
+=(int32_t)(source
-(const uint8_t *)pArgs
->source
);
2712 if(U_FAILURE(*pErrorCode
)) {
2713 /* not mappable or buffer overflow */
2719 /* set the converter state back into UConverter */
2720 cnv
->toUnicodeStatus
=offset
;
2722 cnv
->toULength
=byteIndex
;
2724 /* write back the updated pointers */
2725 pArgs
->source
=(const char *)source
;
2726 pArgs
->target
=target
;
2727 pArgs
->offsets
=offsets
;
2731 * This version of ucnv_MBCSGetNextUChar() is optimized for single-byte, single-state codepages.
2732 * We still need a conversion loop in case we find reserved action codes, which are to be ignored.
2735 ucnv_MBCSSingleGetNextUChar(UConverterToUnicodeArgs
*pArgs
,
2736 UErrorCode
*pErrorCode
) {
2738 const int32_t (*stateTable
)[256];
2739 const uint8_t *source
, *sourceLimit
;
2744 /* set up the local pointers */
2745 cnv
=pArgs
->converter
;
2746 source
=(const uint8_t *)pArgs
->source
;
2747 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
2748 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2749 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
2751 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
2754 /* conversion loop */
2755 while(source
<sourceLimit
) {
2756 entry
=stateTable
[0][*source
++];
2757 /* MBCS_ENTRY_IS_FINAL(entry) */
2759 /* write back the updated pointer early so that we can return directly */
2760 pArgs
->source
=(const char *)source
;
2762 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
2763 /* output BMP code point */
2764 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2768 * An if-else-if chain provides more reliable performance for
2769 * the most common cases compared to a switch.
2771 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2772 if( action
==MBCS_STATE_VALID_DIRECT_20
||
2773 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
2775 /* output supplementary code point */
2776 return (UChar32
)(MBCS_ENTRY_FINAL_VALUE(entry
)+0x10000);
2777 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2778 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
2779 /* output BMP code point */
2780 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2782 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2783 /* just fall through */
2784 } else if(action
==MBCS_STATE_ILLEGAL
) {
2785 /* callback(illegal) */
2786 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2788 /* reserved, must never occur */
2792 if(U_FAILURE(*pErrorCode
)) {
2793 /* callback(illegal) */
2795 } else /* unassigned sequence */ {
2796 /* defer to the generic implementation */
2797 pArgs
->source
=(const char *)source
-1;
2798 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
2802 /* no output because of empty input or only state changes */
2803 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
2808 * Version of _MBCSToUnicodeWithOffsets() optimized for single-character
2809 * conversion without offset handling.
2811 * When a character does not have a mapping to Unicode, then we return to the
2812 * generic ucnv_getNextUChar() code for extension/GB 18030 and error/callback
2814 * We also defer to the generic code in other complicated cases and have them
2815 * ultimately handled by _MBCSToUnicodeWithOffsets() itself.
2817 * All normal mappings and errors are handled here.
2820 ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs
*pArgs
,
2821 UErrorCode
*pErrorCode
) {
2823 const uint8_t *source
, *sourceLimit
, *lastSource
;
2825 const int32_t (*stateTable
)[256];
2826 const uint16_t *unicodeCodeUnits
;
2835 /* use optimized function if possible */
2836 cnv
=pArgs
->converter
;
2838 if(cnv
->preToULength
>0) {
2839 /* use the generic code in ucnv_getNextUChar() to continue with a partial match */
2840 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
2843 if(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SURROGATES
) {
2845 * Using the generic ucnv_getNextUChar() code lets us deal correctly
2846 * with the rare case of a codepage that maps single surrogates
2847 * without adding the complexity to this already complicated function here.
2849 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
2850 } else if(cnv
->sharedData
->mbcs
.countStates
==1) {
2851 return ucnv_MBCSSingleGetNextUChar(pArgs
, pErrorCode
);
2854 /* set up the local pointers */
2855 source
=lastSource
=(const uint8_t *)pArgs
->source
;
2856 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
2858 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2859 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
2861 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
2863 unicodeCodeUnits
=cnv
->sharedData
->mbcs
.unicodeCodeUnits
;
2865 /* get the converter state from UConverter */
2866 offset
=cnv
->toUnicodeStatus
;
2869 * if we are in the SBCS state for a DBCS-only converter,
2870 * then load the DBCS state from the MBCS data
2871 * (dbcsOnlyState==0 if it is not a DBCS-only converter)
2873 if((state
=(uint8_t)(cnv
->mode
))==0) {
2874 state
=cnv
->sharedData
->mbcs
.dbcsOnlyState
;
2877 /* conversion loop */
2879 while(source
<sourceLimit
) {
2880 entry
=stateTable
[state
][*source
++];
2881 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
2882 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
2883 offset
+=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
2885 /* optimization for 1/2-byte input and BMP output */
2886 if( source
<sourceLimit
&&
2887 MBCS_ENTRY_IS_FINAL(entry
=stateTable
[state
][*source
]) &&
2888 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_VALID_16
&&
2889 (c
=unicodeCodeUnits
[offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
)])<0xfffe
2892 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2893 /* output BMP code point */
2897 /* save the previous state for proper extension mapping with SI/SO-stateful converters */
2900 /* set the next state early so that we can reuse the entry variable */
2901 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2904 * An if-else-if chain provides more reliable performance for
2905 * the most common cases compared to a switch.
2907 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2908 if(action
==MBCS_STATE_VALID_DIRECT_16
) {
2909 /* output BMP code point */
2910 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2912 } else if(action
==MBCS_STATE_VALID_16
) {
2913 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
2914 c
=unicodeCodeUnits
[offset
];
2916 /* output BMP code point */
2918 } else if(c
==0xfffe) {
2919 if(UCNV_TO_U_USE_FALLBACK(cnv
) && (c
=ucnv_MBCSGetFallback(&cnv
->sharedData
->mbcs
, offset
))!=0xfffe) {
2923 /* callback(illegal) */
2924 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2926 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
2927 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
2928 c
=unicodeCodeUnits
[offset
++];
2930 /* output BMP code point below 0xd800 */
2932 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? c
<=0xdfff : c
<=0xdbff) {
2933 /* output roundtrip or fallback supplementary code point */
2934 c
=((c
&0x3ff)<<10)+unicodeCodeUnits
[offset
]+(0x10000-0xdc00);
2936 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? (c
&0xfffe)==0xe000 : c
==0xe000) {
2937 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
2938 c
=unicodeCodeUnits
[offset
];
2940 } else if(c
==0xffff) {
2941 /* callback(illegal) */
2942 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2944 } else if(action
==MBCS_STATE_VALID_DIRECT_20
||
2945 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
2947 /* output supplementary code point */
2948 c
=(UChar32
)(MBCS_ENTRY_FINAL_VALUE(entry
)+0x10000);
2950 } else if(action
==MBCS_STATE_CHANGE_ONLY
) {
2952 * This serves as a state change without any output.
2953 * It is useful for reading simple stateful encodings,
2954 * for example using just Shift-In/Shift-Out codes.
2955 * The 21 unused bits may later be used for more sophisticated
2956 * state transitions.
2958 if(cnv
->sharedData
->mbcs
.dbcsOnlyState
!=0) {
2959 /* SI/SO are illegal for DBCS-only conversion */
2960 state
=(uint8_t)(cnv
->mode
); /* restore the previous state */
2962 /* callback(illegal) */
2963 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2965 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2966 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
2967 /* output BMP code point */
2968 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2971 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2972 /* just fall through */
2973 } else if(action
==MBCS_STATE_ILLEGAL
) {
2974 /* callback(illegal) */
2975 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2977 /* reserved (must never occur), or only state change */
2983 /* end of action codes: prepare for a new character */
2986 if(U_FAILURE(*pErrorCode
)) {
2987 /* callback(illegal) */
2989 } else /* unassigned sequence */ {
2990 /* defer to the generic implementation */
2991 cnv
->toUnicodeStatus
=0;
2993 pArgs
->source
=(const char *)lastSource
;
2994 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
3000 if(U_SUCCESS(*pErrorCode
) && source
==sourceLimit
&& lastSource
<source
) {
3001 /* incomplete character byte sequence */
3002 uint8_t *bytes
=cnv
->toUBytes
;
3003 cnv
->toULength
=(int8_t)(source
-lastSource
);
3005 *bytes
++=*lastSource
++;
3006 } while(lastSource
<source
);
3007 *pErrorCode
=U_TRUNCATED_CHAR_FOUND
;
3008 } else if(U_FAILURE(*pErrorCode
)) {
3009 /* callback(illegal) */
3011 * Ticket 5691: consistent illegal sequences:
3012 * - We include at least the first byte in the illegal sequence.
3013 * - If any of the non-initial bytes could be the start of a character,
3014 * we stop the illegal sequence before the first one of those.
3016 UBool isDBCSOnly
=(UBool
)(cnv
->sharedData
->mbcs
.dbcsOnlyState
!=0);
3017 uint8_t *bytes
=cnv
->toUBytes
;
3018 *bytes
++=*lastSource
++; /* first byte */
3019 if(lastSource
==source
) {
3021 } else /* lastSource<source: multi-byte character */ {
3024 lastSource
<source
&& !isSingleOrLead(stateTable
, state
, isDBCSOnly
, *lastSource
);
3027 *bytes
++=*lastSource
++;
3033 /* no output because of empty input or only state changes */
3034 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
3039 /* set the converter state back into UConverter, ready for a new character */
3040 cnv
->toUnicodeStatus
=0;
3043 /* write back the updated pointer */
3044 pArgs
->source
=(const char *)source
;
3050 * Code disabled 2002dec09 (ICU 2.4) because it is not currently used in ICU. markus
3051 * Removal improves code coverage.
3054 * This version of ucnv_MBCSSimpleGetNextUChar() is optimized for single-byte, single-state codepages.
3055 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
3056 * It does not handle conversion extensions (_extToU()).
3059 ucnv_MBCSSingleSimpleGetNextUChar(UConverterSharedData
*sharedData
,
3060 uint8_t b
, UBool useFallback
) {
3064 entry
=sharedData
->mbcs
.stateTable
[0][b
];
3065 /* MBCS_ENTRY_IS_FINAL(entry) */
3067 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
3068 /* output BMP code point */
3069 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
3073 * An if-else-if chain provides more reliable performance for
3074 * the most common cases compared to a switch.
3076 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
3077 if(action
==MBCS_STATE_VALID_DIRECT_20
) {
3078 /* output supplementary code point */
3079 return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
3080 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
3081 if(!TO_U_USE_FALLBACK(useFallback
)) {
3084 /* output BMP code point */
3085 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
3086 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_20
) {
3087 if(!TO_U_USE_FALLBACK(useFallback
)) {
3090 /* output supplementary code point */
3091 return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
3092 } else if(action
==MBCS_STATE_UNASSIGNED
) {
3094 } else if(action
==MBCS_STATE_ILLEGAL
) {
3097 /* reserved, must never occur */
3104 * This is a simple version of _MBCSGetNextUChar() that is used
3105 * by other converter implementations.
3106 * It only returns an "assigned" result if it consumes the entire input.
3107 * It does not use state from the converter, nor error codes.
3108 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
3109 * It handles conversion extensions but not GB 18030.
3114 * otherwise the Unicode code point
3117 ucnv_MBCSSimpleGetNextUChar(UConverterSharedData
*sharedData
,
3118 const char *source
, int32_t length
,
3119 UBool useFallback
) {
3120 const int32_t (*stateTable
)[256];
3121 const uint16_t *unicodeCodeUnits
;
3124 uint8_t state
, action
;
3130 /* no input at all: "illegal" */
3136 * Code disabled 2002dec09 (ICU 2.4) because it is not currently used in ICU. markus
3137 * TODO In future releases, verify that this function is never called for SBCS
3138 * conversions, i.e., that sharedData->mbcs.countStates==1 is still true.
3139 * Removal improves code coverage.
3141 /* use optimized function if possible */
3142 if(sharedData
->mbcs
.countStates
==1) {
3144 return ucnv_MBCSSingleSimpleGetNextUChar(sharedData
, (uint8_t)*source
, useFallback
);
3146 return 0xffff; /* illegal: more than a single byte for an SBCS converter */
3151 /* set up the local pointers */
3152 stateTable
=sharedData
->mbcs
.stateTable
;
3153 unicodeCodeUnits
=sharedData
->mbcs
.unicodeCodeUnits
;
3155 /* converter state */
3157 state
=sharedData
->mbcs
.dbcsOnlyState
;
3159 /* conversion loop */
3161 entry
=stateTable
[state
][(uint8_t)source
[i
++]];
3162 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
3163 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
3164 offset
+=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
3167 return 0xffff; /* truncated character */
3171 * An if-else-if chain provides more reliable performance for
3172 * the most common cases compared to a switch.
3174 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
3175 if(action
==MBCS_STATE_VALID_16
) {
3176 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
3177 c
=unicodeCodeUnits
[offset
];
3180 } else if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
3181 c
=ucnv_MBCSGetFallback(&sharedData
->mbcs
, offset
);
3182 /* else done with 0xfffe */
3185 } else if(action
==MBCS_STATE_VALID_DIRECT_16
) {
3186 /* output BMP code point */
3187 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
3189 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
3190 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
3191 c
=unicodeCodeUnits
[offset
++];
3193 /* output BMP code point below 0xd800 */
3194 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? c
<=0xdfff : c
<=0xdbff) {
3195 /* output roundtrip or fallback supplementary code point */
3196 c
=(UChar32
)(((c
&0x3ff)<<10)+unicodeCodeUnits
[offset
]+(0x10000-0xdc00));
3197 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? (c
&0xfffe)==0xe000 : c
==0xe000) {
3198 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
3199 c
=unicodeCodeUnits
[offset
];
3200 } else if(c
==0xffff) {
3206 } else if(action
==MBCS_STATE_VALID_DIRECT_20
) {
3207 /* output supplementary code point */
3208 c
=0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
3210 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
3211 if(!TO_U_USE_FALLBACK(useFallback
)) {
3215 /* output BMP code point */
3216 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
3218 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_20
) {
3219 if(!TO_U_USE_FALLBACK(useFallback
)) {
3223 /* output supplementary code point */
3224 c
=0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
3226 } else if(action
==MBCS_STATE_UNASSIGNED
) {
3232 * forbid MBCS_STATE_CHANGE_ONLY for this function,
3233 * and MBCS_STATE_ILLEGAL and reserved action codes
3240 /* illegal for this function: not all input consumed */
3245 /* try an extension mapping */
3246 const int32_t *cx
=sharedData
->mbcs
.extIndexes
;
3248 return ucnv_extSimpleMatchToU(cx
, source
, length
, useFallback
);
3255 /* MBCS-from-Unicode conversion functions ----------------------------------- */
3257 /* This version of ucnv_MBCSFromUnicodeWithOffsets() is optimized for double-byte codepages. */
3259 ucnv_MBCSDoubleFromUnicodeWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
3260 UErrorCode
*pErrorCode
) {
3262 const UChar
*source
, *sourceLimit
;
3264 int32_t targetCapacity
;
3267 const uint16_t *table
;
3268 const uint16_t *mbcsIndex
;
3269 const uint8_t *bytes
;
3273 int32_t sourceIndex
, nextSourceIndex
;
3275 uint32_t stage2Entry
;
3276 uint32_t asciiRoundtrips
;
3278 uint8_t unicodeMask
;
3280 /* use optimized function if possible */
3281 cnv
=pArgs
->converter
;
3282 unicodeMask
=cnv
->sharedData
->mbcs
.unicodeMask
;
3284 /* set up the local pointers */
3285 source
=pArgs
->source
;
3286 sourceLimit
=pArgs
->sourceLimit
;
3287 target
=(uint8_t *)pArgs
->target
;
3288 targetCapacity
=(int32_t)(pArgs
->targetLimit
-pArgs
->target
);
3289 offsets
=pArgs
->offsets
;
3291 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
3292 mbcsIndex
=cnv
->sharedData
->mbcs
.mbcsIndex
;
3293 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
3294 bytes
=cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
3296 bytes
=cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
3298 asciiRoundtrips
=cnv
->sharedData
->mbcs
.asciiRoundtrips
;
3300 /* get the converter state from UConverter */
3303 /* sourceIndex=-1 if the current character began in the previous buffer */
3304 sourceIndex
= c
==0 ? 0 : -1;
3307 /* conversion loop */
3308 if(c
!=0 && targetCapacity
>0) {
3312 while(source
<sourceLimit
) {
3314 * This following test is to see if available input would overflow the output.
3315 * It does not catch output of more than one byte that
3316 * overflows as a result of a multi-byte character or callback output
3317 * from the last source character.
3318 * Therefore, those situations also test for overflows and will
3319 * then break the loop, too.
3321 if(targetCapacity
>0) {
3323 * Get a correct Unicode code point:
3324 * a single UChar for a BMP code point or
3325 * a matched surrogate pair for a "supplementary code point".
3329 if(c
<=0x7f && IS_ASCII_ROUNDTRIP(c
, asciiRoundtrips
)) {
3330 *target
++=(uint8_t)c
;
3332 *offsets
++=sourceIndex
;
3333 sourceIndex
=nextSourceIndex
;
3340 * utf8Friendly table: Test for <=0xd7ff rather than <=MBCS_FAST_MAX
3341 * to avoid dealing with surrogates.
3342 * MBCS_FAST_MAX must be >=0xd7ff.
3345 value
=DBCS_RESULT_FROM_MOST_BMP(mbcsIndex
, (const uint16_t *)bytes
, c
);
3346 /* There are only roundtrips (!=0) and no-mapping (==0) entries. */
3350 /* output the value */
3353 * This also tests if the codepage maps single surrogates.
3354 * If it does, then surrogates are not paired but mapped separately.
3355 * Note that in this case unmatched surrogates are not detected.
3357 if(U16_IS_SURROGATE(c
) && !(unicodeMask
&UCNV_HAS_SURROGATES
)) {
3358 if(U16_IS_SURROGATE_LEAD(c
)) {
3360 if(source
<sourceLimit
) {
3361 /* test the following code unit */
3362 UChar trail
=*source
;
3363 if(U16_IS_TRAIL(trail
)) {
3366 c
=U16_GET_SUPPLEMENTARY(c
, trail
);
3367 if(!(unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
3368 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
3369 /* callback(unassigned) */
3372 /* convert this supplementary code point */
3373 /* exit this condition tree */
3375 /* this is an unmatched lead code unit (1st surrogate) */
3376 /* callback(illegal) */
3377 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3385 /* this is an unmatched trail code unit (2nd surrogate) */
3386 /* callback(illegal) */
3387 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3392 /* convert the Unicode code point in c into codepage bytes */
3393 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
3395 /* get the bytes and the length for the output */
3397 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
3399 /* is this code point assigned, or do we use fallbacks? */
3400 if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
) ||
3401 (UCNV_FROM_U_USE_FALLBACK(cnv
, c
) && value
!=0))
3404 * We allow a 0 byte output if the "assigned" bit is set for this entry.
3405 * There is no way with this data structure for fallback output
3406 * to be a zero byte.
3410 /* try an extension mapping */
3411 pArgs
->source
=source
;
3412 c
=_extFromU(cnv
, cnv
->sharedData
,
3413 c
, &source
, sourceLimit
,
3414 &target
, target
+targetCapacity
,
3415 &offsets
, sourceIndex
,
3418 nextSourceIndex
+=(int32_t)(source
-pArgs
->source
);
3420 if(U_FAILURE(*pErrorCode
)) {
3421 /* not mappable or buffer overflow */
3424 /* a mapping was written to the target, continue */
3426 /* recalculate the targetCapacity after an extension mapping */
3427 targetCapacity
=(int32_t)(pArgs
->targetLimit
-(char *)target
);
3429 /* normal end of conversion: prepare for a new character */
3430 sourceIndex
=nextSourceIndex
;
3436 /* write the output character bytes from value and length */
3437 /* from the first if in the loop we know that targetCapacity>0 */
3439 /* this is easy because we know that there is enough space */
3440 *target
++=(uint8_t)value
;
3442 *offsets
++=sourceIndex
;
3445 } else /* length==2 */ {
3446 *target
++=(uint8_t)(value
>>8);
3447 if(2<=targetCapacity
) {
3448 *target
++=(uint8_t)value
;
3450 *offsets
++=sourceIndex
;
3451 *offsets
++=sourceIndex
;
3456 *offsets
++=sourceIndex
;
3458 cnv
->charErrorBuffer
[0]=(char)value
;
3459 cnv
->charErrorBufferLength
=1;
3461 /* target overflow */
3463 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
3469 /* normal end of conversion: prepare for a new character */
3471 sourceIndex
=nextSourceIndex
;
3474 /* target is full */
3475 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
3480 /* set the converter state back into UConverter */
3483 /* write back the updated pointers */
3484 pArgs
->source
=source
;
3485 pArgs
->target
=(char *)target
;
3486 pArgs
->offsets
=offsets
;
3489 /* This version of ucnv_MBCSFromUnicodeWithOffsets() is optimized for single-byte codepages. */
3491 ucnv_MBCSSingleFromUnicodeWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
3492 UErrorCode
*pErrorCode
) {
3494 const UChar
*source
, *sourceLimit
;
3496 int32_t targetCapacity
;
3499 const uint16_t *table
;
3500 const uint16_t *results
;
3504 int32_t sourceIndex
, nextSourceIndex
;
3506 uint16_t value
, minValue
;
3507 UBool hasSupplementary
;
3509 /* set up the local pointers */
3510 cnv
=pArgs
->converter
;
3511 source
=pArgs
->source
;
3512 sourceLimit
=pArgs
->sourceLimit
;
3513 target
=(uint8_t *)pArgs
->target
;
3514 targetCapacity
=(int32_t)(pArgs
->targetLimit
-pArgs
->target
);
3515 offsets
=pArgs
->offsets
;
3517 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
3518 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
3519 results
=(uint16_t *)cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
3521 results
=(uint16_t *)cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
3524 if(cnv
->useFallback
) {
3525 /* use all roundtrip and fallback results */
3528 /* use only roundtrips and fallbacks from private-use characters */
3531 hasSupplementary
=(UBool
)(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
);
3533 /* get the converter state from UConverter */
3536 /* sourceIndex=-1 if the current character began in the previous buffer */
3537 sourceIndex
= c
==0 ? 0 : -1;
3540 /* conversion loop */
3541 if(c
!=0 && targetCapacity
>0) {
3545 while(source
<sourceLimit
) {
3547 * This following test is to see if available input would overflow the output.
3548 * It does not catch output of more than one byte that
3549 * overflows as a result of a multi-byte character or callback output
3550 * from the last source character.
3551 * Therefore, those situations also test for overflows and will
3552 * then break the loop, too.
3554 if(targetCapacity
>0) {
3556 * Get a correct Unicode code point:
3557 * a single UChar for a BMP code point or
3558 * a matched surrogate pair for a "supplementary code point".
3562 if(U16_IS_SURROGATE(c
)) {
3563 if(U16_IS_SURROGATE_LEAD(c
)) {
3565 if(source
<sourceLimit
) {
3566 /* test the following code unit */
3567 UChar trail
=*source
;
3568 if(U16_IS_TRAIL(trail
)) {
3571 c
=U16_GET_SUPPLEMENTARY(c
, trail
);
3572 if(!hasSupplementary
) {
3573 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
3574 /* callback(unassigned) */
3577 /* convert this supplementary code point */
3578 /* exit this condition tree */
3580 /* this is an unmatched lead code unit (1st surrogate) */
3581 /* callback(illegal) */
3582 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3590 /* this is an unmatched trail code unit (2nd surrogate) */
3591 /* callback(illegal) */
3592 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3597 /* convert the Unicode code point in c into codepage bytes */
3598 value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3600 /* is this code point assigned, or do we use fallbacks? */
3601 if(value
>=minValue
) {
3602 /* assigned, write the output character bytes from value and length */
3604 /* this is easy because we know that there is enough space */
3605 *target
++=(uint8_t)value
;
3607 *offsets
++=sourceIndex
;
3611 /* normal end of conversion: prepare for a new character */
3613 sourceIndex
=nextSourceIndex
;
3614 } else { /* unassigned */
3616 /* try an extension mapping */
3617 pArgs
->source
=source
;
3618 c
=_extFromU(cnv
, cnv
->sharedData
,
3619 c
, &source
, sourceLimit
,
3620 &target
, target
+targetCapacity
,
3621 &offsets
, sourceIndex
,
3624 nextSourceIndex
+=(int32_t)(source
-pArgs
->source
);
3626 if(U_FAILURE(*pErrorCode
)) {
3627 /* not mappable or buffer overflow */
3630 /* a mapping was written to the target, continue */
3632 /* recalculate the targetCapacity after an extension mapping */
3633 targetCapacity
=(int32_t)(pArgs
->targetLimit
-(char *)target
);
3635 /* normal end of conversion: prepare for a new character */
3636 sourceIndex
=nextSourceIndex
;
3640 /* target is full */
3641 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
3646 /* set the converter state back into UConverter */
3649 /* write back the updated pointers */
3650 pArgs
->source
=source
;
3651 pArgs
->target
=(char *)target
;
3652 pArgs
->offsets
=offsets
;
3656 * This version of ucnv_MBCSFromUnicode() is optimized for single-byte codepages
3657 * that map only to and from the BMP.
3658 * In addition to single-byte/state optimizations, the offset calculations
3659 * become much easier.
3660 * It would be possible to use the sbcsIndex for UTF-8-friendly tables,
3661 * but measurements have shown that this diminishes performance
3662 * in more cases than it improves it.
3663 * See SVN revision 21013 (2007-feb-06) for the last version with #if switches
3664 * for various MBCS and SBCS optimizations.
3667 ucnv_MBCSSingleFromBMPWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
3668 UErrorCode
*pErrorCode
) {
3670 const UChar
*source
, *sourceLimit
, *lastSource
;
3672 int32_t targetCapacity
, length
;
3675 const uint16_t *table
;
3676 const uint16_t *results
;
3680 int32_t sourceIndex
;
3682 uint32_t asciiRoundtrips
;
3683 uint16_t value
, minValue
;
3685 /* set up the local pointers */
3686 cnv
=pArgs
->converter
;
3687 source
=pArgs
->source
;
3688 sourceLimit
=pArgs
->sourceLimit
;
3689 target
=(uint8_t *)pArgs
->target
;
3690 targetCapacity
=(int32_t)(pArgs
->targetLimit
-pArgs
->target
);
3691 offsets
=pArgs
->offsets
;
3693 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
3694 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
3695 results
=(uint16_t *)cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
3697 results
=(uint16_t *)cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
3699 asciiRoundtrips
=cnv
->sharedData
->mbcs
.asciiRoundtrips
;
3701 if(cnv
->useFallback
) {
3702 /* use all roundtrip and fallback results */
3705 /* use only roundtrips and fallbacks from private-use characters */
3709 /* get the converter state from UConverter */
3712 /* sourceIndex=-1 if the current character began in the previous buffer */
3713 sourceIndex
= c
==0 ? 0 : -1;
3717 * since the conversion here is 1:1 UChar:uint8_t, we need only one counter
3718 * for the minimum of the sourceLength and targetCapacity
3720 length
=(int32_t)(sourceLimit
-source
);
3721 if(length
<targetCapacity
) {
3722 targetCapacity
=length
;
3725 /* conversion loop */
3726 if(c
!=0 && targetCapacity
>0) {
3730 #if MBCS_UNROLL_SINGLE_FROM_BMP
3731 /* unrolling makes it slower on Pentium III/Windows 2000?! */
3732 /* unroll the loop with the most common case */
3734 if(targetCapacity
>=4) {
3735 int32_t count
, loops
;
3736 uint16_t andedValues
;
3738 loops
=count
=targetCapacity
>>2;
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 andedValues
&=value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3751 *target
++=(uint8_t)value
;
3753 /* were all 4 entries really valid? */
3754 if(andedValues
<minValue
) {
3755 /* no, return to the first of these 4 */
3762 targetCapacity
-=4*count
;
3765 lastSource
+=4*count
;
3767 *offsets
++=sourceIndex
++;
3768 *offsets
++=sourceIndex
++;
3769 *offsets
++=sourceIndex
++;
3770 *offsets
++=sourceIndex
++;
3779 while(targetCapacity
>0) {
3781 * Get a correct Unicode code point:
3782 * a single UChar for a BMP code point or
3783 * a matched surrogate pair for a "supplementary code point".
3787 * Do not immediately check for single surrogates:
3788 * Assume that they are unassigned and check for them in that case.
3789 * This speeds up the conversion of assigned characters.
3791 /* convert the Unicode code point in c into codepage bytes */
3792 if(c
<=0x7f && IS_ASCII_ROUNDTRIP(c
, asciiRoundtrips
)) {
3793 *target
++=(uint8_t)c
;
3798 value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3799 /* is this code point assigned, or do we use fallbacks? */
3800 if(value
>=minValue
) {
3801 /* assigned, write the output character bytes from value and length */
3803 /* this is easy because we know that there is enough space */
3804 *target
++=(uint8_t)value
;
3807 /* normal end of conversion: prepare for a new character */
3810 } else if(!U16_IS_SURROGATE(c
)) {
3811 /* normal, unassigned BMP character */
3812 } else if(U16_IS_SURROGATE_LEAD(c
)) {
3814 if(source
<sourceLimit
) {
3815 /* test the following code unit */
3816 UChar trail
=*source
;
3817 if(U16_IS_TRAIL(trail
)) {
3819 c
=U16_GET_SUPPLEMENTARY(c
, trail
);
3820 /* this codepage does not map supplementary code points */
3821 /* callback(unassigned) */
3823 /* this is an unmatched lead code unit (1st surrogate) */
3824 /* callback(illegal) */
3825 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3831 *pErrorCode
=U_TRUNCATED_CHAR_FOUND
;
3836 /* this is an unmatched trail code unit (2nd surrogate) */
3837 /* callback(illegal) */
3838 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3842 /* c does not have a mapping */
3844 /* get the number of code units for c to correctly advance sourceIndex */
3845 length
=U16_LENGTH(c
);
3847 /* set offsets since the start or the last extension */
3849 int32_t count
=(int32_t)(source
-lastSource
);
3851 /* do not set the offset for this character */
3855 *offsets
++=sourceIndex
++;
3858 /* offsets and sourceIndex are now set for the current character */
3861 /* try an extension mapping */
3863 c
=_extFromU(cnv
, cnv
->sharedData
,
3864 c
, &source
, sourceLimit
,
3865 &target
, (const uint8_t *)(pArgs
->targetLimit
),
3866 &offsets
, sourceIndex
,
3869 sourceIndex
+=length
+(int32_t)(source
-lastSource
);
3872 if(U_FAILURE(*pErrorCode
)) {
3873 /* not mappable or buffer overflow */
3876 /* a mapping was written to the target, continue */
3878 /* recalculate the targetCapacity after an extension mapping */
3879 targetCapacity
=(int32_t)(pArgs
->targetLimit
-(char *)target
);
3880 length
=(int32_t)(sourceLimit
-source
);
3881 if(length
<targetCapacity
) {
3882 targetCapacity
=length
;
3886 #if MBCS_UNROLL_SINGLE_FROM_BMP
3887 /* unrolling makes it slower on Pentium III/Windows 2000?! */
3892 if(U_SUCCESS(*pErrorCode
) && source
<sourceLimit
&& target
>=(uint8_t *)pArgs
->targetLimit
) {
3893 /* target is full */
3894 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
3897 /* set offsets since the start or the last callback */
3899 size_t count
=source
-lastSource
;
3900 if (count
> 0 && *pErrorCode
== U_TRUNCATED_CHAR_FOUND
) {
3902 Caller gave us a partial supplementary character,
3903 which this function couldn't convert in any case.
3904 The callback will handle the offset.
3909 *offsets
++=sourceIndex
++;
3914 /* set the converter state back into UConverter */
3917 /* write back the updated pointers */
3918 pArgs
->source
=source
;
3919 pArgs
->target
=(char *)target
;
3920 pArgs
->offsets
=offsets
;
3924 ucnv_MBCSFromUnicodeWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
3925 UErrorCode
*pErrorCode
) {
3927 const UChar
*source
, *sourceLimit
;
3929 int32_t targetCapacity
;
3932 const uint16_t *table
;
3933 const uint16_t *mbcsIndex
;
3934 const uint8_t *p
, *bytes
;
3939 int32_t prevSourceIndex
, sourceIndex
, nextSourceIndex
;
3941 uint32_t stage2Entry
;
3942 uint32_t asciiRoundtrips
;
3944 uint8_t si_value
[2] = {0, 0};
3945 uint8_t so_value
[2] = {0, 0};
3946 uint8_t si_value_length
, so_value_length
;
3947 int32_t length
= 0, prevLength
;
3948 uint8_t unicodeMask
;
3950 cnv
=pArgs
->converter
;
3952 if(cnv
->preFromUFirstCP
>=0) {
3954 * pass sourceIndex=-1 because we continue from an earlier buffer
3955 * in the future, this may change with continuous offsets
3957 ucnv_extContinueMatchFromU(cnv
, pArgs
, -1, pErrorCode
);
3959 if(U_FAILURE(*pErrorCode
) || cnv
->preFromULength
<0) {
3964 /* use optimized function if possible */
3965 outputType
=cnv
->sharedData
->mbcs
.outputType
;
3966 unicodeMask
=cnv
->sharedData
->mbcs
.unicodeMask
;
3967 if(outputType
==MBCS_OUTPUT_1
&& !(unicodeMask
&UCNV_HAS_SURROGATES
)) {
3968 if(!(unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
3969 ucnv_MBCSSingleFromBMPWithOffsets(pArgs
, pErrorCode
);
3971 ucnv_MBCSSingleFromUnicodeWithOffsets(pArgs
, pErrorCode
);
3974 } else if(outputType
==MBCS_OUTPUT_2
&& cnv
->sharedData
->mbcs
.utf8Friendly
) {
3975 ucnv_MBCSDoubleFromUnicodeWithOffsets(pArgs
, pErrorCode
);
3979 /* set up the local pointers */
3980 source
=pArgs
->source
;
3981 sourceLimit
=pArgs
->sourceLimit
;
3982 target
=(uint8_t *)pArgs
->target
;
3983 targetCapacity
=(int32_t)(pArgs
->targetLimit
-pArgs
->target
);
3984 offsets
=pArgs
->offsets
;
3986 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
3987 if(cnv
->sharedData
->mbcs
.utf8Friendly
) {
3988 mbcsIndex
=cnv
->sharedData
->mbcs
.mbcsIndex
;
3992 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
3993 bytes
=cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
3995 bytes
=cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
3997 asciiRoundtrips
=cnv
->sharedData
->mbcs
.asciiRoundtrips
;
3999 /* get the converter state from UConverter */
4002 if(outputType
==MBCS_OUTPUT_2_SISO
) {
4003 prevLength
=cnv
->fromUnicodeStatus
;
4005 /* set the real value */
4009 /* prevent fromUnicodeStatus from being set to something non-0 */
4013 /* sourceIndex=-1 if the current character began in the previous buffer */
4015 sourceIndex
= c
==0 ? 0 : -1;
4018 /* Get the SI/SO character for the converter */
4019 si_value_length
= getSISOBytes(SI
, cnv
->options
, si_value
);
4020 so_value_length
= getSISOBytes(SO
, cnv
->options
, so_value
);
4022 /* conversion loop */
4024 * This is another piece of ugly code:
4025 * A goto into the loop if the converter state contains a first surrogate
4026 * from the previous function call.
4027 * It saves me to check in each loop iteration a check of if(c==0)
4028 * and duplicating the trail-surrogate-handling code in the else
4029 * branch of that check.
4030 * I could not find any other way to get around this other than
4031 * using a function call for the conversion and callback, which would
4032 * be even more inefficient.
4034 * Markus Scherer 2000-jul-19
4036 if(c
!=0 && targetCapacity
>0) {
4040 while(source
<sourceLimit
) {
4042 * This following test is to see if available input would overflow the output.
4043 * It does not catch output of more than one byte that
4044 * overflows as a result of a multi-byte character or callback output
4045 * from the last source character.
4046 * Therefore, those situations also test for overflows and will
4047 * then break the loop, too.
4049 if(targetCapacity
>0) {
4051 * Get a correct Unicode code point:
4052 * a single UChar for a BMP code point or
4053 * a matched surrogate pair for a "supplementary code point".
4057 if(c
<=0x7f && IS_ASCII_ROUNDTRIP(c
, asciiRoundtrips
)) {
4058 *target
++=(uint8_t)c
;
4060 *offsets
++=sourceIndex
;
4061 prevSourceIndex
=sourceIndex
;
4062 sourceIndex
=nextSourceIndex
;
4069 * utf8Friendly table: Test for <=0xd7ff rather than <=MBCS_FAST_MAX
4070 * to avoid dealing with surrogates.
4071 * MBCS_FAST_MAX must be >=0xd7ff.
4073 if(c
<=0xd7ff && mbcsIndex
!=NULL
) {
4074 value
=mbcsIndex
[c
>>6];
4076 /* get the bytes and the length for the output (copied from below and adapted for utf8Friendly data) */
4077 /* There are only roundtrips (!=0) and no-mapping (==0) entries. */
4078 switch(outputType
) {
4080 value
=((const uint16_t *)bytes
)[value
+(c
&0x3f)];
4091 case MBCS_OUTPUT_2_SISO
:
4092 /* 1/2-byte stateful with Shift-In/Shift-Out */
4094 * Save the old state in the converter object
4095 * right here, then change the local prevLength state variable if necessary.
4096 * Then, if this character turns out to be unassigned or a fallback that
4097 * is not taken, the callback code must not save the new state in the converter
4098 * because the new state is for a character that is not output.
4099 * However, the callback must still restore the state from the converter
4100 * in case the callback function changed it for its output.
4102 cnv
->fromUnicodeStatus
=prevLength
; /* save the old state */
4103 value
=((const uint16_t *)bytes
)[value
+(c
&0x3f)];
4107 } else if(prevLength
<=1) {
4110 /* change from double-byte mode to single-byte */
4111 if (si_value_length
== 1) {
4112 value
|=(uint32_t)si_value
[0]<<8;
4114 } else if (si_value_length
== 2) {
4115 value
|=(uint32_t)si_value
[1]<<8;
4116 value
|=(uint32_t)si_value
[0]<<16;
4125 /* change from single-byte mode to double-byte */
4126 if (so_value_length
== 1) {
4127 value
|=(uint32_t)so_value
[0]<<16;
4129 } else if (so_value_length
== 2) {
4130 value
|=(uint32_t)so_value
[1]<<16;
4131 value
|=(uint32_t)so_value
[0]<<24;
4138 case MBCS_OUTPUT_DBCS_ONLY
:
4139 /* table with single-byte results, but only DBCS mappings used */
4140 value
=((const uint16_t *)bytes
)[value
+(c
&0x3f)];
4142 /* no mapping or SBCS result, not taken for DBCS-only */
4149 p
=bytes
+(value
+(c
&0x3f))*3;
4150 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4157 } else if(value
<=0xffff) {
4164 value
=((const uint32_t *)bytes
)[value
+(c
&0x3f)];
4171 } else if(value
<=0xffff) {
4173 } else if(value
<=0xffffff) {
4179 case MBCS_OUTPUT_3_EUC
:
4180 value
=((const uint16_t *)bytes
)[value
+(c
&0x3f)];
4181 /* EUC 16-bit fixed-length representation */
4188 } else if((value
&0x8000)==0) {
4191 } else if((value
&0x80)==0) {
4198 case MBCS_OUTPUT_4_EUC
:
4199 p
=bytes
+(value
+(c
&0x3f))*3;
4200 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4201 /* EUC 16-bit fixed-length representation applied to the first two bytes */
4208 } else if(value
<=0xffff) {
4210 } else if((value
&0x800000)==0) {
4213 } else if((value
&0x8000)==0) {
4221 /* must not occur */
4223 * To avoid compiler warnings that value & length may be
4224 * used without having been initialized, we set them here.
4225 * In reality, this is unreachable code.
4226 * Not having a default branch also causes warnings with
4233 /* output the value */
4236 * This also tests if the codepage maps single surrogates.
4237 * If it does, then surrogates are not paired but mapped separately.
4238 * Note that in this case unmatched surrogates are not detected.
4240 if(U16_IS_SURROGATE(c
) && !(unicodeMask
&UCNV_HAS_SURROGATES
)) {
4241 if(U16_IS_SURROGATE_LEAD(c
)) {
4243 if(source
<sourceLimit
) {
4244 /* test the following code unit */
4245 UChar trail
=*source
;
4246 if(U16_IS_TRAIL(trail
)) {
4249 c
=U16_GET_SUPPLEMENTARY(c
, trail
);
4250 if(!(unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
4251 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
4252 cnv
->fromUnicodeStatus
=prevLength
; /* save the old state */
4253 /* callback(unassigned) */
4256 /* convert this supplementary code point */
4257 /* exit this condition tree */
4259 /* this is an unmatched lead code unit (1st surrogate) */
4260 /* callback(illegal) */
4261 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
4269 /* this is an unmatched trail code unit (2nd surrogate) */
4270 /* callback(illegal) */
4271 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
4276 /* convert the Unicode code point in c into codepage bytes */
4279 * The basic lookup is a triple-stage compact array (trie) lookup.
4280 * For details see the beginning of this file.
4282 * Single-byte codepages are handled with a different data structure
4283 * by _MBCSSingle... functions.
4285 * The result consists of a 32-bit value from stage 2 and
4286 * a pointer to as many bytes as are stored per character.
4287 * The pointer points to the character's bytes in stage 3.
4288 * Bits 15..0 of the stage 2 entry contain the stage 3 index
4289 * for that pointer, while bits 31..16 are flags for which of
4290 * the 16 characters in the block are roundtrip-assigned.
4292 * For 2-byte and 4-byte codepages, the bytes are stored as uint16_t
4293 * respectively as uint32_t, in the platform encoding.
4294 * For 3-byte codepages, the bytes are always stored in big-endian order.
4296 * For EUC encodings that use only either 0x8e or 0x8f as the first
4297 * byte of their longest byte sequences, the first two bytes in
4298 * this third stage indicate with their 7th bits whether these bytes
4299 * are to be written directly or actually need to be preceeded by
4300 * one of the two Single-Shift codes. With this, the third stage
4301 * stores one byte fewer per character than the actual maximum length of
4302 * EUC byte sequences.
4304 * Other than that, leading zero bytes are removed and the other
4305 * bytes output. A single zero byte may be output if the "assigned"
4306 * bit in stage 2 was on.
4307 * The data structure does not support zero byte output as a fallback,
4308 * and also does not allow output of leading zeros.
4310 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
4312 /* get the bytes and the length for the output */
4313 switch(outputType
) {
4315 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4322 case MBCS_OUTPUT_2_SISO
:
4323 /* 1/2-byte stateful with Shift-In/Shift-Out */
4325 * Save the old state in the converter object
4326 * right here, then change the local prevLength state variable if necessary.
4327 * Then, if this character turns out to be unassigned or a fallback that
4328 * is not taken, the callback code must not save the new state in the converter
4329 * because the new state is for a character that is not output.
4330 * However, the callback must still restore the state from the converter
4331 * in case the callback function changed it for its output.
4333 cnv
->fromUnicodeStatus
=prevLength
; /* save the old state */
4334 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4336 if(value
==0 && MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
)==0) {
4337 /* no mapping, leave value==0 */
4339 } else if(prevLength
<=1) {
4342 /* change from double-byte mode to single-byte */
4343 if (si_value_length
== 1) {
4344 value
|=(uint32_t)si_value
[0]<<8;
4346 } else if (si_value_length
== 2) {
4347 value
|=(uint32_t)si_value
[1]<<8;
4348 value
|=(uint32_t)si_value
[0]<<16;
4357 /* change from single-byte mode to double-byte */
4358 if (so_value_length
== 1) {
4359 value
|=(uint32_t)so_value
[0]<<16;
4361 } else if (so_value_length
== 2) {
4362 value
|=(uint32_t)so_value
[1]<<16;
4363 value
|=(uint32_t)so_value
[0]<<24;
4370 case MBCS_OUTPUT_DBCS_ONLY
:
4371 /* table with single-byte results, but only DBCS mappings used */
4372 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4374 /* no mapping or SBCS result, not taken for DBCS-only */
4375 value
=stage2Entry
=0; /* stage2Entry=0 to reset roundtrip flags */
4382 p
=MBCS_POINTER_3_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4383 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4386 } else if(value
<=0xffff) {
4393 value
=MBCS_VALUE_4_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4396 } else if(value
<=0xffff) {
4398 } else if(value
<=0xffffff) {
4404 case MBCS_OUTPUT_3_EUC
:
4405 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4406 /* EUC 16-bit fixed-length representation */
4409 } else if((value
&0x8000)==0) {
4412 } else if((value
&0x80)==0) {
4419 case MBCS_OUTPUT_4_EUC
:
4420 p
=MBCS_POINTER_3_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4421 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4422 /* EUC 16-bit fixed-length representation applied to the first two bytes */
4425 } else if(value
<=0xffff) {
4427 } else if((value
&0x800000)==0) {
4430 } else if((value
&0x8000)==0) {
4438 /* must not occur */
4440 * To avoid compiler warnings that value & length may be
4441 * used without having been initialized, we set them here.
4442 * In reality, this is unreachable code.
4443 * Not having a default branch also causes warnings with
4446 value
=stage2Entry
=0; /* stage2Entry=0 to reset roundtrip flags */
4451 /* is this code point assigned, or do we use fallbacks? */
4452 if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
)!=0 ||
4453 (UCNV_FROM_U_USE_FALLBACK(cnv
, c
) && value
!=0))
4456 * We allow a 0 byte output if the "assigned" bit is set for this entry.
4457 * There is no way with this data structure for fallback output
4458 * to be a zero byte.
4462 /* try an extension mapping */
4463 pArgs
->source
=source
;
4464 c
=_extFromU(cnv
, cnv
->sharedData
,
4465 c
, &source
, sourceLimit
,
4466 &target
, target
+targetCapacity
,
4467 &offsets
, sourceIndex
,
4470 nextSourceIndex
+=(int32_t)(source
-pArgs
->source
);
4471 prevLength
=cnv
->fromUnicodeStatus
; /* restore SISO state */
4473 if(U_FAILURE(*pErrorCode
)) {
4474 /* not mappable or buffer overflow */
4477 /* a mapping was written to the target, continue */
4479 /* recalculate the targetCapacity after an extension mapping */
4480 targetCapacity
=(int32_t)(pArgs
->targetLimit
-(char *)target
);
4482 /* normal end of conversion: prepare for a new character */
4484 prevSourceIndex
=sourceIndex
;
4485 sourceIndex
=nextSourceIndex
;
4492 /* write the output character bytes from value and length */
4493 /* from the first if in the loop we know that targetCapacity>0 */
4494 if(length
<=targetCapacity
) {
4497 /* each branch falls through to the next one */
4499 *target
++=(uint8_t)(value
>>24);
4500 case 3: /*fall through*/
4501 *target
++=(uint8_t)(value
>>16);
4502 case 2: /*fall through*/
4503 *target
++=(uint8_t)(value
>>8);
4504 case 1: /*fall through*/
4505 *target
++=(uint8_t)value
;
4507 /* will never occur */
4512 /* each branch falls through to the next one */
4514 *target
++=(uint8_t)(value
>>24);
4515 *offsets
++=sourceIndex
;
4516 case 3: /*fall through*/
4517 *target
++=(uint8_t)(value
>>16);
4518 *offsets
++=sourceIndex
;
4519 case 2: /*fall through*/
4520 *target
++=(uint8_t)(value
>>8);
4521 *offsets
++=sourceIndex
;
4522 case 1: /*fall through*/
4523 *target
++=(uint8_t)value
;
4524 *offsets
++=sourceIndex
;
4526 /* will never occur */
4530 targetCapacity
-=length
;
4532 uint8_t *charErrorBuffer
;
4535 * We actually do this backwards here:
4536 * In order to save an intermediate variable, we output
4537 * first to the overflow buffer what does not fit into the
4540 /* we know that 1<=targetCapacity<length<=4 */
4541 length
-=targetCapacity
;
4542 charErrorBuffer
=(uint8_t *)cnv
->charErrorBuffer
;
4544 /* each branch falls through to the next one */
4546 *charErrorBuffer
++=(uint8_t)(value
>>16);
4547 case 2: /*fall through*/
4548 *charErrorBuffer
++=(uint8_t)(value
>>8);
4549 case 1: /*fall through*/
4550 *charErrorBuffer
=(uint8_t)value
;
4552 /* will never occur */
4555 cnv
->charErrorBufferLength
=(int8_t)length
;
4557 /* now output what fits into the regular target */
4558 value
>>=8*length
; /* length was reduced by targetCapacity */
4559 switch(targetCapacity
) {
4560 /* each branch falls through to the next one */
4562 *target
++=(uint8_t)(value
>>16);
4564 *offsets
++=sourceIndex
;
4566 case 2: /*fall through*/
4567 *target
++=(uint8_t)(value
>>8);
4569 *offsets
++=sourceIndex
;
4571 case 1: /*fall through*/
4572 *target
++=(uint8_t)value
;
4574 *offsets
++=sourceIndex
;
4577 /* will never occur */
4581 /* target overflow */
4583 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
4588 /* normal end of conversion: prepare for a new character */
4591 prevSourceIndex
=sourceIndex
;
4592 sourceIndex
=nextSourceIndex
;
4596 /* target is full */
4597 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
4603 * the end of the input stream and detection of truncated input
4604 * are handled by the framework, but for EBCDIC_STATEFUL conversion
4605 * we need to emit an SI at the very end
4609 * EBCDIC_STATEFUL in DBCS mode
4610 * end of input and no truncated input
4612 if( U_SUCCESS(*pErrorCode
) &&
4613 outputType
==MBCS_OUTPUT_2_SISO
&& prevLength
==2 &&
4614 pArgs
->flush
&& source
>=sourceLimit
&& c
==0
4616 /* EBCDIC_STATEFUL ending with DBCS: emit an SI to return the output stream to SBCS */
4617 if(targetCapacity
>0) {
4618 *target
++=(uint8_t)si_value
[0];
4619 if (si_value_length
== 2) {
4620 if (targetCapacity
<2) {
4621 cnv
->charErrorBuffer
[0]=(uint8_t)si_value
[1];
4622 cnv
->charErrorBufferLength
=1;
4623 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
4625 *target
++=(uint8_t)si_value
[1];
4629 /* set the last source character's index (sourceIndex points at sourceLimit now) */
4630 *offsets
++=prevSourceIndex
;
4633 /* target is full */
4634 cnv
->charErrorBuffer
[0]=(uint8_t)si_value
[0];
4635 if (si_value_length
== 2) {
4636 cnv
->charErrorBuffer
[1]=(uint8_t)si_value
[1];
4638 cnv
->charErrorBufferLength
=si_value_length
;
4639 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
4641 prevLength
=1; /* we switched into SBCS */
4644 /* set the converter state back into UConverter */
4646 cnv
->fromUnicodeStatus
=prevLength
;
4648 /* write back the updated pointers */
4649 pArgs
->source
=source
;
4650 pArgs
->target
=(char *)target
;
4651 pArgs
->offsets
=offsets
;
4655 * This is another simple conversion function for internal use by other
4656 * conversion implementations.
4657 * It does not use the converter state nor call callbacks.
4658 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
4659 * It handles conversion extensions but not GB 18030.
4661 * It converts one single Unicode code point into codepage bytes, encoded
4662 * as one 32-bit value. The function returns the number of bytes in *pValue:
4663 * 1..4 the number of bytes in *pValue
4664 * 0 unassigned (*pValue undefined)
4665 * -1 illegal (currently not used, *pValue undefined)
4667 * *pValue will contain the resulting bytes with the last byte in bits 7..0,
4668 * the second to last byte in bits 15..8, etc.
4669 * Currently, the function assumes but does not check that 0<=c<=0x10ffff.
4672 ucnv_MBCSFromUChar32(UConverterSharedData
*sharedData
,
4673 UChar32 c
, uint32_t *pValue
,
4674 UBool useFallback
) {
4676 const uint16_t *table
;
4678 /* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */
4681 uint32_t stage2Entry
;
4685 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
4686 if(c
<=0xffff || (sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
4687 table
=sharedData
->mbcs
.fromUnicodeTable
;
4689 /* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
4690 if(sharedData
->mbcs
.outputType
==MBCS_OUTPUT_1
) {
4691 value
=MBCS_SINGLE_RESULT_FROM_U(table
, (uint16_t *)sharedData
->mbcs
.fromUnicodeBytes
, c
);
4692 /* is this code point assigned, or do we use fallbacks? */
4693 if(useFallback
? value
>=0x800 : value
>=0xc00) {
4697 } else /* outputType!=MBCS_OUTPUT_1 */ {
4698 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
4700 /* get the bytes and the length for the output */
4701 switch(sharedData
->mbcs
.outputType
) {
4703 value
=MBCS_VALUE_2_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4711 /* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */
4712 case MBCS_OUTPUT_DBCS_ONLY
:
4713 /* table with single-byte results, but only DBCS mappings used */
4714 value
=MBCS_VALUE_2_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4716 /* no mapping or SBCS result, not taken for DBCS-only */
4717 value
=stage2Entry
=0; /* stage2Entry=0 to reset roundtrip flags */
4724 p
=MBCS_POINTER_3_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4725 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4728 } else if(value
<=0xffff) {
4735 value
=MBCS_VALUE_4_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4738 } else if(value
<=0xffff) {
4740 } else if(value
<=0xffffff) {
4746 case MBCS_OUTPUT_3_EUC
:
4747 value
=MBCS_VALUE_2_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4748 /* EUC 16-bit fixed-length representation */
4751 } else if((value
&0x8000)==0) {
4754 } else if((value
&0x80)==0) {
4761 case MBCS_OUTPUT_4_EUC
:
4762 p
=MBCS_POINTER_3_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4763 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4764 /* EUC 16-bit fixed-length representation applied to the first two bytes */
4767 } else if(value
<=0xffff) {
4769 } else if((value
&0x800000)==0) {
4772 } else if((value
&0x8000)==0) {
4781 /* must not occur */
4785 /* is this code point assigned, or do we use fallbacks? */
4786 if( MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
) ||
4787 (FROM_U_USE_FALLBACK(useFallback
, c
) && value
!=0)
4790 * We allow a 0 byte output if the "assigned" bit is set for this entry.
4791 * There is no way with this data structure for fallback output
4792 * to be a zero byte.
4801 cx
=sharedData
->mbcs
.extIndexes
;
4803 length
=ucnv_extSimpleMatchFromU(cx
, c
, pValue
, useFallback
);
4804 return length
>=0 ? length
: -length
; /* return abs(length); */
4814 * This function has been moved to ucnv2022.c for inlining.
4815 * This implementation is here only for documentation purposes
4819 * This version of ucnv_MBCSFromUChar32() is optimized for single-byte codepages.
4820 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
4821 * It does not handle conversion extensions (_extFromU()).
4823 * It returns the codepage byte for the code point, or -1 if it is unassigned.
4826 ucnv_MBCSSingleFromUChar32(UConverterSharedData
*sharedData
,
4828 UBool useFallback
) {
4829 const uint16_t *table
;
4832 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
4833 if(c
>=0x10000 && !(sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
4837 /* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
4838 table
=sharedData
->mbcs
.fromUnicodeTable
;
4840 /* get the byte for the output */
4841 value
=MBCS_SINGLE_RESULT_FROM_U(table
, (uint16_t *)sharedData
->mbcs
.fromUnicodeBytes
, c
);
4842 /* is this code point assigned, or do we use fallbacks? */
4843 if(useFallback
? value
>=0x800 : value
>=0xc00) {
4851 /* MBCS-from-UTF-8 conversion functions ------------------------------------- */
4853 /* minimum code point values for n-byte UTF-8 sequences, n=0..4 */
4854 static const UChar32
4855 utf8_minLegal
[5]={ 0, 0, 0x80, 0x800, 0x10000 };
4857 /* offsets for n-byte UTF-8 sequences that were calculated with ((lead<<6)+trail)<<6+trail... */
4858 static const UChar32
4859 utf8_offsets
[7]={ 0, 0, 0x3080, 0xE2080, 0x3C82080 };
4862 ucnv_SBCSFromUTF8(UConverterFromUnicodeArgs
*pFromUArgs
,
4863 UConverterToUnicodeArgs
*pToUArgs
,
4864 UErrorCode
*pErrorCode
) {
4865 UConverter
*utf8
, *cnv
;
4866 const uint8_t *source
, *sourceLimit
;
4868 int32_t targetCapacity
;
4870 const uint16_t *table
, *sbcsIndex
;
4871 const uint16_t *results
;
4873 int8_t oldToULength
, toULength
, toULimit
;
4878 uint32_t asciiRoundtrips
;
4879 uint16_t value
, minValue
;
4880 UBool hasSupplementary
;
4882 /* set up the local pointers */
4883 utf8
=pToUArgs
->converter
;
4884 cnv
=pFromUArgs
->converter
;
4885 source
=(uint8_t *)pToUArgs
->source
;
4886 sourceLimit
=(uint8_t *)pToUArgs
->sourceLimit
;
4887 target
=(uint8_t *)pFromUArgs
->target
;
4888 targetCapacity
=(int32_t)(pFromUArgs
->targetLimit
-pFromUArgs
->target
);
4890 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
4891 sbcsIndex
=cnv
->sharedData
->mbcs
.sbcsIndex
;
4892 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
4893 results
=(uint16_t *)cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
4895 results
=(uint16_t *)cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
4897 asciiRoundtrips
=cnv
->sharedData
->mbcs
.asciiRoundtrips
;
4899 if(cnv
->useFallback
) {
4900 /* use all roundtrip and fallback results */
4903 /* use only roundtrips and fallbacks from private-use characters */
4906 hasSupplementary
=(UBool
)(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
);
4908 /* get the converter state from the UTF-8 UConverter */
4909 c
=(UChar32
)utf8
->toUnicodeStatus
;
4911 toULength
=oldToULength
=utf8
->toULength
;
4912 toULimit
=(int8_t)utf8
->mode
;
4914 toULength
=oldToULength
=toULimit
=0;
4918 * Make sure that the last byte sequence before sourceLimit is complete
4919 * or runs into a lead byte.
4920 * Do not go back into the bytes that will be read for finishing a partial
4921 * sequence from the previous buffer.
4922 * In the conversion loop compare source with sourceLimit only once
4923 * per multi-byte character.
4928 length
=(int32_t)(sourceLimit
-source
) - (toULimit
-oldToULength
);
4929 for(i
=0; i
<3 && i
<length
;) {
4930 b
=*(sourceLimit
-i
-1);
4931 if(U8_IS_TRAIL(b
)) {
4934 if(i
<utf8_countTrailBytes
[b
]) {
4935 /* exit the conversion loop before the lead byte if there are not enough trail bytes for it */
4943 if(c
!=0 && targetCapacity
>0) {
4944 utf8
->toUnicodeStatus
=0;
4948 * Note: We could avoid the goto by duplicating some of the moreBytes
4949 * code, but only up to the point of collecting a complete UTF-8
4950 * sequence; then recurse for the toUBytes[toULength]
4951 * and then continue with normal conversion.
4953 * If so, move this code to just after initializing the minimum
4954 * set of local variables for reading the UTF-8 input
4955 * (utf8, source, target, limits but not cnv, table, minValue, etc.).
4957 * Potential advantages:
4959 * - oldToULength could become a local variable in just those code blocks
4960 * that deal with buffer boundaries
4961 * - possibly faster if the goto prevents some compiler optimizations
4962 * (this would need measuring to confirm)
4964 * - code duplication
4968 /* conversion loop */
4969 while(source
<sourceLimit
) {
4970 if(targetCapacity
>0) {
4974 if(IS_ASCII_ROUNDTRIP(b
, asciiRoundtrips
)) {
4975 *target
++=(uint8_t)b
;
4980 value
=SBCS_RESULT_FROM_UTF8(sbcsIndex
, results
, 0, c
);
4984 if( /* handle U+0080..U+07FF inline */
4986 (t1
=(uint8_t)(*source
-0x80)) <= 0x3f
4990 value
=SBCS_RESULT_FROM_UTF8(sbcsIndex
, results
, c
, t1
);
4991 if(value
>=minValue
) {
4992 *target
++=(uint8_t)value
;
5001 } else if(b
==0xe0) {
5002 if( /* handle U+0800..U+0FFF inline */
5003 (t1
=(uint8_t)(source
[0]-0x80)) <= 0x3f && t1
>= 0x20 &&
5004 (t2
=(uint8_t)(source
[1]-0x80)) <= 0x3f
5008 value
=SBCS_RESULT_FROM_UTF8(sbcsIndex
, results
, c
, t2
);
5009 if(value
>=minValue
) {
5010 *target
++=(uint8_t)value
;
5024 /* handle "complicated" and error cases, and continuing partial characters */
5027 toULimit
=utf8_countTrailBytes
[b
]+1;
5030 while(toULength
<toULimit
) {
5032 * The sourceLimit may have been adjusted before the conversion loop
5033 * to stop before a truncated sequence.
5034 * Here we need to use the real limit in case we have two truncated
5035 * sequences at the end.
5038 if(source
<(uint8_t *)pToUArgs
->sourceLimit
) {
5040 if(U8_IS_TRAIL(b
)) {
5045 break; /* sequence too short, stop with toULength<toULimit */
5048 /* store the partial UTF-8 character, compatible with the regular UTF-8 converter */
5049 source
-=(toULength
-oldToULength
);
5050 while(oldToULength
<toULength
) {
5051 utf8
->toUBytes
[oldToULength
++]=*source
++;
5053 utf8
->toUnicodeStatus
=c
;
5054 utf8
->toULength
=toULength
;
5055 utf8
->mode
=toULimit
;
5056 pToUArgs
->source
=(char *)source
;
5057 pFromUArgs
->target
=(char *)target
;
5062 if( toULength
==toULimit
&& /* consumed all trail bytes */
5063 (toULength
==3 || toULength
==2) && /* BMP */
5064 (c
-=utf8_offsets
[toULength
])>=utf8_minLegal
[toULength
] &&
5065 (c
<=0xd7ff || 0xe000<=c
) /* not a surrogate */
5067 value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
5069 toULength
==toULimit
&& toULength
==4 &&
5070 (0x10000<=(c
-=utf8_offsets
[4]) && c
<=0x10ffff)
5072 /* supplementary code point */
5073 if(!hasSupplementary
) {
5074 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
5077 value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
5080 /* error handling: illegal UTF-8 byte sequence */
5081 source
-=(toULength
-oldToULength
);
5082 while(oldToULength
<toULength
) {
5083 utf8
->toUBytes
[oldToULength
++]=*source
++;
5085 utf8
->toULength
=toULength
;
5086 pToUArgs
->source
=(char *)source
;
5087 pFromUArgs
->target
=(char *)target
;
5088 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
5094 if(value
>=minValue
) {
5095 /* output the mapping for c */
5096 *target
++=(uint8_t)value
;
5099 /* value<minValue means c is unassigned (unmappable) */
5101 * Try an extension mapping.
5102 * Pass in no source because we don't have UTF-16 input.
5103 * If we have a partial match on c, we will return and revert
5104 * to UTF-8->UTF-16->charset conversion.
5106 static const UChar nul
=0;
5107 const UChar
*noSource
=&nul
;
5108 c
=_extFromU(cnv
, cnv
->sharedData
,
5109 c
, &noSource
, noSource
,
5110 &target
, target
+targetCapacity
,
5115 if(U_FAILURE(*pErrorCode
)) {
5116 /* not mappable or buffer overflow */
5119 } else if(cnv
->preFromUFirstCP
>=0) {
5121 * Partial match, return and revert to pivoting.
5122 * In normal from-UTF-16 conversion, we would just continue
5123 * but then exit the loop because the extension match would
5124 * have consumed the source.
5128 /* a mapping was written to the target, continue */
5130 /* recalculate the targetCapacity after an extension mapping */
5131 targetCapacity
=(int32_t)(pFromUArgs
->targetLimit
-(char *)target
);
5135 /* target is full */
5136 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
5142 * The sourceLimit may have been adjusted before the conversion loop
5143 * to stop before a truncated sequence.
5144 * If so, then collect the truncated sequence now.
5146 if(U_SUCCESS(*pErrorCode
) && source
<(sourceLimit
=(uint8_t *)pToUArgs
->sourceLimit
)) {
5147 c
=utf8
->toUBytes
[0]=b
=*source
++;
5149 toULimit
=utf8_countTrailBytes
[b
]+1;
5150 while(source
<sourceLimit
) {
5151 utf8
->toUBytes
[toULength
++]=b
=*source
++;
5154 utf8
->toUnicodeStatus
=c
;
5155 utf8
->toULength
=toULength
;
5156 utf8
->mode
=toULimit
;
5159 /* write back the updated pointers */
5160 pToUArgs
->source
=(char *)source
;
5161 pFromUArgs
->target
=(char *)target
;
5165 ucnv_DBCSFromUTF8(UConverterFromUnicodeArgs
*pFromUArgs
,
5166 UConverterToUnicodeArgs
*pToUArgs
,
5167 UErrorCode
*pErrorCode
) {
5168 UConverter
*utf8
, *cnv
;
5169 const uint8_t *source
, *sourceLimit
;
5171 int32_t targetCapacity
;
5173 const uint16_t *table
, *mbcsIndex
;
5174 const uint16_t *results
;
5176 int8_t oldToULength
, toULength
, toULimit
;
5181 uint32_t stage2Entry
;
5182 uint32_t asciiRoundtrips
;
5184 UBool hasSupplementary
;
5186 /* set up the local pointers */
5187 utf8
=pToUArgs
->converter
;
5188 cnv
=pFromUArgs
->converter
;
5189 source
=(uint8_t *)pToUArgs
->source
;
5190 sourceLimit
=(uint8_t *)pToUArgs
->sourceLimit
;
5191 target
=(uint8_t *)pFromUArgs
->target
;
5192 targetCapacity
=(int32_t)(pFromUArgs
->targetLimit
-pFromUArgs
->target
);
5194 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
5195 mbcsIndex
=cnv
->sharedData
->mbcs
.mbcsIndex
;
5196 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
5197 results
=(uint16_t *)cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
5199 results
=(uint16_t *)cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
5201 asciiRoundtrips
=cnv
->sharedData
->mbcs
.asciiRoundtrips
;
5203 hasSupplementary
=(UBool
)(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
);
5205 /* get the converter state from the UTF-8 UConverter */
5206 c
=(UChar32
)utf8
->toUnicodeStatus
;
5208 toULength
=oldToULength
=utf8
->toULength
;
5209 toULimit
=(int8_t)utf8
->mode
;
5211 toULength
=oldToULength
=toULimit
=0;
5215 * Make sure that the last byte sequence before sourceLimit is complete
5216 * or runs into a lead byte.
5217 * Do not go back into the bytes that will be read for finishing a partial
5218 * sequence from the previous buffer.
5219 * In the conversion loop compare source with sourceLimit only once
5220 * per multi-byte character.
5225 length
=(int32_t)(sourceLimit
-source
) - (toULimit
-oldToULength
);
5226 for(i
=0; i
<3 && i
<length
;) {
5227 b
=*(sourceLimit
-i
-1);
5228 if(U8_IS_TRAIL(b
)) {
5231 if(i
<utf8_countTrailBytes
[b
]) {
5232 /* exit the conversion loop before the lead byte if there are not enough trail bytes for it */
5240 if(c
!=0 && targetCapacity
>0) {
5241 utf8
->toUnicodeStatus
=0;
5244 /* See note in ucnv_SBCSFromUTF8() about this goto. */
5247 /* conversion loop */
5248 while(source
<sourceLimit
) {
5249 if(targetCapacity
>0) {
5253 if(IS_ASCII_ROUNDTRIP(b
, asciiRoundtrips
)) {
5258 value
=DBCS_RESULT_FROM_UTF8(mbcsIndex
, results
, 0, b
);
5266 if( /* handle U+1000..U+D7FF inline */
5267 (((t1
=(uint8_t)(source
[0]-0x80), b
<0xed) && (t1
<= 0x3f)) ||
5268 (b
==0xed && (t1
<= 0x1f))) &&
5269 (t2
=(uint8_t)(source
[1]-0x80)) <= 0x3f
5273 value
=DBCS_RESULT_FROM_UTF8(mbcsIndex
, results
, c
, t2
);
5282 if( /* handle U+0080..U+07FF inline */
5284 (t1
=(uint8_t)(*source
-0x80)) <= 0x3f
5288 value
=DBCS_RESULT_FROM_UTF8(mbcsIndex
, results
, c
, t1
);
5301 /* handle "complicated" and error cases, and continuing partial characters */
5304 toULimit
=utf8_countTrailBytes
[b
]+1;
5307 while(toULength
<toULimit
) {
5309 * The sourceLimit may have been adjusted before the conversion loop
5310 * to stop before a truncated sequence.
5311 * Here we need to use the real limit in case we have two truncated
5312 * sequences at the end.
5315 if(source
<(uint8_t *)pToUArgs
->sourceLimit
) {
5317 if(U8_IS_TRAIL(b
)) {
5322 break; /* sequence too short, stop with toULength<toULimit */
5325 /* store the partial UTF-8 character, compatible with the regular UTF-8 converter */
5326 source
-=(toULength
-oldToULength
);
5327 while(oldToULength
<toULength
) {
5328 utf8
->toUBytes
[oldToULength
++]=*source
++;
5330 utf8
->toUnicodeStatus
=c
;
5331 utf8
->toULength
=toULength
;
5332 utf8
->mode
=toULimit
;
5333 pToUArgs
->source
=(char *)source
;
5334 pFromUArgs
->target
=(char *)target
;
5339 if( toULength
==toULimit
&& /* consumed all trail bytes */
5340 (toULength
==3 || toULength
==2) && /* BMP */
5341 (c
-=utf8_offsets
[toULength
])>=utf8_minLegal
[toULength
] &&
5342 (c
<=0xd7ff || 0xe000<=c
) /* not a surrogate */
5344 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
5346 toULength
==toULimit
&& toULength
==4 &&
5347 (0x10000<=(c
-=utf8_offsets
[4]) && c
<=0x10ffff)
5349 /* supplementary code point */
5350 if(!hasSupplementary
) {
5351 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
5354 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
5357 /* error handling: illegal UTF-8 byte sequence */
5358 source
-=(toULength
-oldToULength
);
5359 while(oldToULength
<toULength
) {
5360 utf8
->toUBytes
[oldToULength
++]=*source
++;
5362 utf8
->toULength
=toULength
;
5363 pToUArgs
->source
=(char *)source
;
5364 pFromUArgs
->target
=(char *)target
;
5365 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
5369 /* get the bytes and the length for the output */
5371 value
=MBCS_VALUE_2_FROM_STAGE_2(results
, stage2Entry
, c
);
5373 /* is this code point assigned, or do we use fallbacks? */
5374 if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
) ||
5375 (UCNV_FROM_U_USE_FALLBACK(cnv
, c
) && value
!=0))
5382 /* write the output character bytes from value and length */
5383 /* from the first if in the loop we know that targetCapacity>0 */
5385 /* this is easy because we know that there is enough space */
5386 *target
++=(uint8_t)value
;
5388 } else /* length==2 */ {
5389 *target
++=(uint8_t)(value
>>8);
5390 if(2<=targetCapacity
) {
5391 *target
++=(uint8_t)value
;
5394 cnv
->charErrorBuffer
[0]=(char)value
;
5395 cnv
->charErrorBufferLength
=1;
5397 /* target overflow */
5398 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
5407 * Try an extension mapping.
5408 * Pass in no source because we don't have UTF-16 input.
5409 * If we have a partial match on c, we will return and revert
5410 * to UTF-8->UTF-16->charset conversion.
5412 static const UChar nul
=0;
5413 const UChar
*noSource
=&nul
;
5414 c
=_extFromU(cnv
, cnv
->sharedData
,
5415 c
, &noSource
, noSource
,
5416 &target
, target
+targetCapacity
,
5421 if(U_FAILURE(*pErrorCode
)) {
5422 /* not mappable or buffer overflow */
5425 } else if(cnv
->preFromUFirstCP
>=0) {
5427 * Partial match, return and revert to pivoting.
5428 * In normal from-UTF-16 conversion, we would just continue
5429 * but then exit the loop because the extension match would
5430 * have consumed the source.
5434 /* a mapping was written to the target, continue */
5436 /* recalculate the targetCapacity after an extension mapping */
5437 targetCapacity
=(int32_t)(pFromUArgs
->targetLimit
-(char *)target
);
5442 /* target is full */
5443 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
5449 * The sourceLimit may have been adjusted before the conversion loop
5450 * to stop before a truncated sequence.
5451 * If so, then collect the truncated sequence now.
5453 if(U_SUCCESS(*pErrorCode
) && source
<(sourceLimit
=(uint8_t *)pToUArgs
->sourceLimit
)) {
5454 c
=utf8
->toUBytes
[0]=b
=*source
++;
5456 toULimit
=utf8_countTrailBytes
[b
]+1;
5457 while(source
<sourceLimit
) {
5458 utf8
->toUBytes
[toULength
++]=b
=*source
++;
5461 utf8
->toUnicodeStatus
=c
;
5462 utf8
->toULength
=toULength
;
5463 utf8
->mode
=toULimit
;
5466 /* write back the updated pointers */
5467 pToUArgs
->source
=(char *)source
;
5468 pFromUArgs
->target
=(char *)target
;
5471 /* miscellaneous ------------------------------------------------------------ */
5474 ucnv_MBCSGetStarters(const UConverter
* cnv
,
5475 UBool starters
[256],
5476 UErrorCode
*pErrorCode
) {
5477 const int32_t *state0
;
5480 state0
=cnv
->sharedData
->mbcs
.stateTable
[cnv
->sharedData
->mbcs
.dbcsOnlyState
];
5481 for(i
=0; i
<256; ++i
) {
5482 /* all bytes that cause a state transition from state 0 are lead bytes */
5483 starters
[i
]= (UBool
)MBCS_ENTRY_IS_TRANSITION(state0
[i
]);
5488 * This is an internal function that allows other converter implementations
5489 * to check whether a byte is a lead byte.
5492 ucnv_MBCSIsLeadByte(UConverterSharedData
*sharedData
, char byte
) {
5493 return (UBool
)MBCS_ENTRY_IS_TRANSITION(sharedData
->mbcs
.stateTable
[0][(uint8_t)byte
]);
5497 ucnv_MBCSWriteSub(UConverterFromUnicodeArgs
*pArgs
,
5498 int32_t offsetIndex
,
5499 UErrorCode
*pErrorCode
) {
5500 UConverter
*cnv
=pArgs
->converter
;
5505 /* first, select between subChar and subChar1 */
5506 if( cnv
->subChar1
!=0 &&
5507 (cnv
->sharedData
->mbcs
.extIndexes
!=NULL
?
5509 (cnv
->invalidUCharBuffer
[0]<=0xff))
5511 /* select subChar1 if it is set (not 0) and the unmappable Unicode code point is up to U+00ff (IBM MBCS behavior) */
5512 subchar
=(char *)&cnv
->subChar1
;
5515 /* select subChar in all other cases */
5516 subchar
=(char *)cnv
->subChars
;
5517 length
=cnv
->subCharLen
;
5520 /* reset the selector for the next code point */
5521 cnv
->useSubChar1
=FALSE
;
5523 if (cnv
->sharedData
->mbcs
.outputType
== MBCS_OUTPUT_2_SISO
) {
5526 /* fromUnicodeStatus contains prevLength */
5529 if(cnv
->fromUnicodeStatus
==2) {
5530 /* DBCS mode and SBCS sub char: change to SBCS */
5531 cnv
->fromUnicodeStatus
=1;
5537 if(cnv
->fromUnicodeStatus
<=1) {
5538 /* SBCS mode and DBCS sub char: change to DBCS */
5539 cnv
->fromUnicodeStatus
=2;
5546 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
5550 length
=(int32_t)(p
-buffer
);
5553 ucnv_cbFromUWriteBytes(pArgs
, subchar
, length
, offsetIndex
, pErrorCode
);
5556 U_CFUNC UConverterType
5557 ucnv_MBCSGetType(const UConverter
* converter
) {
5558 /* SBCS, DBCS, and EBCDIC_STATEFUL are replaced by MBCS, but here we cheat a little */
5559 if(converter
->sharedData
->mbcs
.countStates
==1) {
5560 return (UConverterType
)UCNV_SBCS
;
5561 } else if((converter
->sharedData
->mbcs
.outputType
&0xff)==MBCS_OUTPUT_2_SISO
) {
5562 return (UConverterType
)UCNV_EBCDIC_STATEFUL
;
5563 } else if(converter
->sharedData
->staticData
->minBytesPerChar
==2 && converter
->sharedData
->staticData
->maxBytesPerChar
==2) {
5564 return (UConverterType
)UCNV_DBCS
;
5566 return (UConverterType
)UCNV_MBCS
;
5569 static const UConverterImpl _SBCSUTF8Impl
={
5579 ucnv_MBCSToUnicodeWithOffsets
,
5580 ucnv_MBCSToUnicodeWithOffsets
,
5581 ucnv_MBCSFromUnicodeWithOffsets
,
5582 ucnv_MBCSFromUnicodeWithOffsets
,
5583 ucnv_MBCSGetNextUChar
,
5585 ucnv_MBCSGetStarters
,
5589 ucnv_MBCSGetUnicodeSet
,
5595 static const UConverterImpl _DBCSUTF8Impl
={
5605 ucnv_MBCSToUnicodeWithOffsets
,
5606 ucnv_MBCSToUnicodeWithOffsets
,
5607 ucnv_MBCSFromUnicodeWithOffsets
,
5608 ucnv_MBCSFromUnicodeWithOffsets
,
5609 ucnv_MBCSGetNextUChar
,
5611 ucnv_MBCSGetStarters
,
5615 ucnv_MBCSGetUnicodeSet
,
5621 static const UConverterImpl _MBCSImpl
={
5631 ucnv_MBCSToUnicodeWithOffsets
,
5632 ucnv_MBCSToUnicodeWithOffsets
,
5633 ucnv_MBCSFromUnicodeWithOffsets
,
5634 ucnv_MBCSFromUnicodeWithOffsets
,
5635 ucnv_MBCSGetNextUChar
,
5637 ucnv_MBCSGetStarters
,
5641 ucnv_MBCSGetUnicodeSet
5645 /* Static data is in tools/makeconv/ucnvstat.c for data-based
5646 * converters. Be sure to update it as well.
5649 const UConverterSharedData _MBCSData
={
5650 sizeof(UConverterSharedData
), 1,
5651 NULL
, NULL
, NULL
, FALSE
, &_MBCSImpl
,
5655 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */