2 ******************************************************************************
4 * Copyright (C) 2000-2016, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
8 * file name: ucnvmbcs.cpp
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
363 * Callback from ucnv_MBCSEnumToUnicode(), takes 32 mappings from
364 * consecutive sequences of bytes, starting from the one encoded in value,
365 * to Unicode code points. (Multiple mappings to reduce per-function call overhead.)
366 * Does not currently support m:n mappings or reverse fallbacks.
367 * This function will not be called for sequences of bytes with leading zeros.
369 * @param context an opaque pointer, as passed into ucnv_MBCSEnumToUnicode()
370 * @param value contains 1..4 bytes of the first byte sequence, right-aligned
371 * @param codePoints resulting Unicode code points, or negative if a byte sequence does
372 * not map to anything
373 * @return TRUE to continue enumeration, FALSE to stop
375 typedef UBool U_CALLCONV
376 UConverterEnumToUCallback(const void *context
, uint32_t value
, UChar32 codePoints
[32]);
379 ucnv_MBCSLoad(UConverterSharedData
*sharedData
,
380 UConverterLoadArgs
*pArgs
,
382 UErrorCode
*pErrorCode
);
385 ucnv_MBCSUnload(UConverterSharedData
*sharedData
);
388 ucnv_MBCSOpen(UConverter
*cnv
,
389 UConverterLoadArgs
*pArgs
,
390 UErrorCode
*pErrorCode
);
393 ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs
*pArgs
,
394 UErrorCode
*pErrorCode
);
397 ucnv_MBCSGetStarters(const UConverter
* cnv
,
399 UErrorCode
*pErrorCode
);
402 ucnv_MBCSGetName(const UConverter
*cnv
);
405 ucnv_MBCSWriteSub(UConverterFromUnicodeArgs
*pArgs
,
407 UErrorCode
*pErrorCode
);
410 ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs
*pArgs
,
411 UErrorCode
*pErrorCode
);
414 ucnv_SBCSFromUTF8(UConverterFromUnicodeArgs
*pFromUArgs
,
415 UConverterToUnicodeArgs
*pToUArgs
,
416 UErrorCode
*pErrorCode
);
419 ucnv_MBCSGetUnicodeSet(const UConverter
*cnv
,
421 UConverterUnicodeSet which
,
422 UErrorCode
*pErrorCode
);
425 ucnv_DBCSFromUTF8(UConverterFromUnicodeArgs
*pFromUArgs
,
426 UConverterToUnicodeArgs
*pToUArgs
,
427 UErrorCode
*pErrorCode
);
429 static const UConverterImpl _SBCSUTF8Impl
={
439 ucnv_MBCSToUnicodeWithOffsets
,
440 ucnv_MBCSToUnicodeWithOffsets
,
441 ucnv_MBCSFromUnicodeWithOffsets
,
442 ucnv_MBCSFromUnicodeWithOffsets
,
443 ucnv_MBCSGetNextUChar
,
445 ucnv_MBCSGetStarters
,
449 ucnv_MBCSGetUnicodeSet
,
455 static const UConverterImpl _DBCSUTF8Impl
={
465 ucnv_MBCSToUnicodeWithOffsets
,
466 ucnv_MBCSToUnicodeWithOffsets
,
467 ucnv_MBCSFromUnicodeWithOffsets
,
468 ucnv_MBCSFromUnicodeWithOffsets
,
469 ucnv_MBCSGetNextUChar
,
471 ucnv_MBCSGetStarters
,
475 ucnv_MBCSGetUnicodeSet
,
481 static const UConverterImpl _MBCSImpl
={
491 ucnv_MBCSToUnicodeWithOffsets
,
492 ucnv_MBCSToUnicodeWithOffsets
,
493 ucnv_MBCSFromUnicodeWithOffsets
,
494 ucnv_MBCSFromUnicodeWithOffsets
,
495 ucnv_MBCSGetNextUChar
,
497 ucnv_MBCSGetStarters
,
501 ucnv_MBCSGetUnicodeSet
,
507 /* Static data is in tools/makeconv/ucnvstat.c for data-based
508 * converters. Be sure to update it as well.
511 const UConverterSharedData _MBCSData
={
512 sizeof(UConverterSharedData
), 1,
513 NULL
, NULL
, FALSE
, TRUE
, &_MBCSImpl
,
514 0, UCNV_MBCS_TABLE_INITIALIZER
518 /* GB 18030 data ------------------------------------------------------------ */
520 /* helper macros for linear values for GB 18030 four-byte sequences */
521 #define LINEAR_18030(a, b, c, d) ((((a)*10+(b))*126L+(c))*10L+(d))
523 #define LINEAR_18030_BASE LINEAR_18030(0x81, 0x30, 0x81, 0x30)
525 #define LINEAR(x) LINEAR_18030(x>>24, (x>>16)&0xff, (x>>8)&0xff, x&0xff)
528 * Some ranges of GB 18030 where both the Unicode code points and the
529 * GB four-byte sequences are contiguous and are handled algorithmically by
530 * the special callback functions below.
531 * The values are start & end of Unicode & GB codes.
533 * Note that single surrogates are not mapped by GB 18030
534 * as of the re-released mapping tables from 2000-nov-30.
536 static const uint32_t
537 gb18030Ranges
[14][4]={
538 {0x10000, 0x10FFFF, LINEAR(0x90308130), LINEAR(0xE3329A35)},
539 {0x9FA6, 0xD7FF, LINEAR(0x82358F33), LINEAR(0x8336C738)},
540 {0x0452, 0x1E3E, LINEAR(0x8130D330), LINEAR(0x8135F436)},
541 {0x1E40, 0x200F, LINEAR(0x8135F438), LINEAR(0x8136A531)},
542 {0xE865, 0xF92B, LINEAR(0x8336D030), LINEAR(0x84308534)},
543 {0x2643, 0x2E80, LINEAR(0x8137A839), LINEAR(0x8138FD38)},
544 {0xFA2A, 0xFE2F, LINEAR(0x84309C38), LINEAR(0x84318537)},
545 {0x3CE1, 0x4055, LINEAR(0x8231D438), LINEAR(0x8232AF32)},
546 {0x361B, 0x3917, LINEAR(0x8230A633), LINEAR(0x8230F237)},
547 {0x49B8, 0x4C76, LINEAR(0x8234A131), LINEAR(0x8234E733)},
548 {0x4160, 0x4336, LINEAR(0x8232C937), LINEAR(0x8232F837)},
549 {0x478E, 0x4946, LINEAR(0x8233E838), LINEAR(0x82349638)},
550 {0x44D7, 0x464B, LINEAR(0x8233A339), LINEAR(0x8233C931)},
551 {0xFFE6, 0xFFFF, LINEAR(0x8431A234), LINEAR(0x8431A439)}
554 /* bit flag for UConverter.options indicating GB 18030 special handling */
555 #define _MBCS_OPTION_GB18030 0x8000
557 /* bit flag for UConverter.options indicating KEIS,JEF,JIF special handling */
558 #define _MBCS_OPTION_KEIS 0x01000
559 #define _MBCS_OPTION_JEF 0x02000
560 #define _MBCS_OPTION_JIPS 0x04000
562 #define KEIS_SO_CHAR_1 0x0A
563 #define KEIS_SO_CHAR_2 0x42
564 #define KEIS_SI_CHAR_1 0x0A
565 #define KEIS_SI_CHAR_2 0x41
567 #define JEF_SO_CHAR 0x28
568 #define JEF_SI_CHAR 0x29
570 #define JIPS_SO_CHAR_1 0x1A
571 #define JIPS_SO_CHAR_2 0x70
572 #define JIPS_SI_CHAR_1 0x1A
573 #define JIPS_SI_CHAR_2 0x71
579 typedef enum SISO_Option SISO_Option
;
581 static int32_t getSISOBytes(SISO_Option option
, uint32_t cnvOption
, uint8_t *value
) {
582 int32_t SISOLength
= 0;
586 if ((cnvOption
&_MBCS_OPTION_KEIS
)!=0) {
587 value
[0] = KEIS_SI_CHAR_1
;
588 value
[1] = KEIS_SI_CHAR_2
;
590 } else if ((cnvOption
&_MBCS_OPTION_JEF
)!=0) {
591 value
[0] = JEF_SI_CHAR
;
593 } else if ((cnvOption
&_MBCS_OPTION_JIPS
)!=0) {
594 value
[0] = JIPS_SI_CHAR_1
;
595 value
[1] = JIPS_SI_CHAR_2
;
603 if ((cnvOption
&_MBCS_OPTION_KEIS
)!=0) {
604 value
[0] = KEIS_SO_CHAR_1
;
605 value
[1] = KEIS_SO_CHAR_2
;
607 } else if ((cnvOption
&_MBCS_OPTION_JEF
)!=0) {
608 value
[0] = JEF_SO_CHAR
;
610 } else if ((cnvOption
&_MBCS_OPTION_JIPS
)!=0) {
611 value
[0] = JIPS_SO_CHAR_1
;
612 value
[1] = JIPS_SO_CHAR_2
;
620 /* Should never happen. */
627 /* Miscellaneous ------------------------------------------------------------ */
629 /* similar to ucnv_MBCSGetNextUChar() but recursive */
631 enumToU(UConverterMBCSTable
*mbcsTable
, int8_t stateProps
[],
632 int32_t state
, uint32_t offset
,
634 UConverterEnumToUCallback
*callback
, const void *context
,
635 UErrorCode
*pErrorCode
) {
636 UChar32 codePoints
[32];
638 const uint16_t *unicodeCodeUnits
;
639 UChar32 anyCodePoints
;
642 row
=mbcsTable
->stateTable
[state
];
643 unicodeCodeUnits
=mbcsTable
->unicodeCodeUnits
;
646 anyCodePoints
=-1; /* becomes non-negative if there is a mapping */
648 b
=(stateProps
[state
]&0x38)<<2;
649 if(b
==0 && stateProps
[state
]>=0x40) {
650 /* skip byte sequences with leading zeros because they are not stored in the fromUnicode table */
651 codePoints
[0]=U_SENTINEL
;
654 limit
=((stateProps
[state
]&7)+1)<<5;
656 int32_t entry
=row
[b
];
657 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
658 int32_t nextState
=MBCS_ENTRY_TRANSITION_STATE(entry
);
659 if(stateProps
[nextState
]>=0) {
660 /* recurse to a state with non-ignorable actions */
662 mbcsTable
, stateProps
, nextState
,
663 offset
+MBCS_ENTRY_TRANSITION_OFFSET(entry
),
670 codePoints
[b
&0x1f]=U_SENTINEL
;
676 * An if-else-if chain provides more reliable performance for
677 * the most common cases compared to a switch.
679 action
=MBCS_ENTRY_FINAL_ACTION(entry
);
680 if(action
==MBCS_STATE_VALID_DIRECT_16
) {
681 /* output BMP code point */
682 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
683 } else if(action
==MBCS_STATE_VALID_16
) {
684 int32_t finalOffset
=offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
);
685 c
=unicodeCodeUnits
[finalOffset
];
687 /* output BMP code point */
691 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
692 int32_t finalOffset
=offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
);
693 c
=unicodeCodeUnits
[finalOffset
++];
695 /* output BMP code point below 0xd800 */
696 } else if(c
<=0xdbff) {
697 /* output roundtrip or fallback supplementary code point */
698 c
=((c
&0x3ff)<<10)+unicodeCodeUnits
[finalOffset
]+(0x10000-0xdc00);
699 } else if(c
==0xe000) {
700 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
701 c
=unicodeCodeUnits
[finalOffset
];
705 } else if(action
==MBCS_STATE_VALID_DIRECT_20
) {
706 /* output supplementary code point */
707 c
=(UChar32
)(MBCS_ENTRY_FINAL_VALUE(entry
)+0x10000);
712 codePoints
[b
&0x1f]=c
;
715 if(((++b
)&0x1f)==0) {
716 if(anyCodePoints
>=0) {
717 if(!callback(context
, value
|(uint32_t)(b
-0x20), codePoints
)) {
728 * Only called if stateProps[state]==-1.
729 * A recursive call may do stateProps[state]|=0x40 if this state is the target of an
730 * MBCS_STATE_CHANGE_ONLY.
733 getStateProp(const int32_t (*stateTable
)[256], int8_t stateProps
[], int state
) {
735 int32_t min
, max
, entry
, nextState
;
737 row
=stateTable
[state
];
740 /* find first non-ignorable state */
743 nextState
=MBCS_ENTRY_STATE(entry
);
744 if(stateProps
[nextState
]==-1) {
745 getStateProp(stateTable
, stateProps
, nextState
);
747 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
748 if(stateProps
[nextState
]>=0) {
751 } else if(MBCS_ENTRY_FINAL_ACTION(entry
)<MBCS_STATE_UNASSIGNED
) {
755 stateProps
[state
]=-0x40; /* (int8_t)0xc0 */
756 return stateProps
[state
];
759 stateProps
[state
]|=(int8_t)((min
>>5)<<3);
761 /* find last non-ignorable state */
762 for(max
=0xff; min
<max
; --max
) {
764 nextState
=MBCS_ENTRY_STATE(entry
);
765 if(stateProps
[nextState
]==-1) {
766 getStateProp(stateTable
, stateProps
, nextState
);
768 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
769 if(stateProps
[nextState
]>=0) {
772 } else if(MBCS_ENTRY_FINAL_ACTION(entry
)<MBCS_STATE_UNASSIGNED
) {
776 stateProps
[state
]|=(int8_t)(max
>>5);
778 /* recurse further and collect direct-state information */
781 nextState
=MBCS_ENTRY_STATE(entry
);
782 if(stateProps
[nextState
]==-1) {
783 getStateProp(stateTable
, stateProps
, nextState
);
785 if(MBCS_ENTRY_IS_FINAL(entry
)) {
786 stateProps
[nextState
]|=0x40;
787 if(MBCS_ENTRY_FINAL_ACTION(entry
)<=MBCS_STATE_FALLBACK_DIRECT_20
) {
788 stateProps
[state
]|=0x40;
793 return stateProps
[state
];
797 * Internal function enumerating the toUnicode data of an MBCS converter.
798 * Currently only used for reconstituting data for a MBCS_OPT_NO_FROM_U
799 * table, but could also be used for a future ucnv_getUnicodeSet() option
800 * that includes reverse fallbacks (after updating this function's implementation).
801 * Currently only handles roundtrip mappings.
802 * Does not currently handle extensions.
805 ucnv_MBCSEnumToUnicode(UConverterMBCSTable
*mbcsTable
,
806 UConverterEnumToUCallback
*callback
, const void *context
,
807 UErrorCode
*pErrorCode
) {
809 * Properties for each state, to speed up the enumeration.
810 * Ignorable actions are unassigned/illegal/state-change-only:
811 * They do not lead to mappings.
814 * 1 direct/initial state (stateful converters have multiple)
815 * 0 non-initial state with transitions or with non-ignorable result actions
816 * -1 final state with only ignorable actions
819 * The lowest byte value with non-ignorable actions is
820 * value<<5 (rounded down).
823 * The highest byte value with non-ignorable actions is
824 * (value<<5)&0x1f (rounded up).
826 int8_t stateProps
[MBCS_MAX_STATE_COUNT
];
829 uprv_memset(stateProps
, -1, sizeof(stateProps
));
831 /* recurse from state 0 and set all stateProps */
832 getStateProp(mbcsTable
->stateTable
, stateProps
, 0);
834 for(state
=0; state
<mbcsTable
->countStates
; ++state
) {
835 /*if(stateProps[state]==-1) {
836 printf("unused/unreachable <icu:state> %d\n", state);
838 if(stateProps
[state
]>=0x40) {
839 /* start from each direct state */
841 mbcsTable
, stateProps
, state
, 0, 0,
849 ucnv_MBCSGetFilteredUnicodeSetForUnicode(const UConverterSharedData
*sharedData
,
851 UConverterUnicodeSet which
,
852 UConverterSetFilter filter
,
853 UErrorCode
*pErrorCode
) {
854 const UConverterMBCSTable
*mbcsTable
;
855 const uint16_t *table
;
858 uint16_t st1
, maxStage1
, st2
;
862 /* enumerate the from-Unicode trie table */
863 mbcsTable
=&sharedData
->mbcs
;
864 table
=mbcsTable
->fromUnicodeTable
;
865 if(mbcsTable
->unicodeMask
&UCNV_HAS_SUPPLEMENTARY
) {
871 c
=0; /* keep track of the current code point while enumerating */
873 if(mbcsTable
->outputType
==MBCS_OUTPUT_1
) {
874 const uint16_t *stage2
, *stage3
, *results
;
877 results
=(const uint16_t *)mbcsTable
->fromUnicodeBytes
;
880 * Set a threshold variable for selecting which mappings to use.
881 * See ucnv_MBCSSingleFromBMPWithOffsets() and
882 * MBCS_SINGLE_RESULT_FROM_U() for details.
884 if(which
==UCNV_ROUNDTRIP_SET
) {
885 /* use only roundtrips */
887 } else /* UCNV_ROUNDTRIP_AND_FALLBACK_SET */ {
888 /* use all roundtrip and fallback results */
892 for(st1
=0; st1
<maxStage1
; ++st1
) {
896 for(st2
=0; st2
<64; ++st2
) {
897 if((st3
=stage2
[st2
])!=0) {
898 /* read the stage 3 block */
902 if(*stage3
++>=minValue
) {
905 } while((++c
&0xf)!=0);
907 c
+=16; /* empty stage 3 block */
911 c
+=1024; /* empty stage 2 block */
915 const uint32_t *stage2
;
916 const uint8_t *stage3
, *bytes
;
917 uint32_t st3Multiplier
;
921 bytes
=mbcsTable
->fromUnicodeBytes
;
923 useFallback
=(UBool
)(which
==UCNV_ROUNDTRIP_AND_FALLBACK_SET
);
925 switch(mbcsTable
->outputType
) {
927 case MBCS_OUTPUT_4_EUC
:
938 for(st1
=0; st1
<maxStage1
; ++st1
) {
940 if(st2
>(maxStage1
>>1)) {
941 stage2
=(const uint32_t *)table
+st2
;
942 for(st2
=0; st2
<64; ++st2
) {
943 if((st3
=stage2
[st2
])!=0) {
944 /* read the stage 3 block */
945 stage3
=bytes
+st3Multiplier
*16*(uint32_t)(uint16_t)st3
;
947 /* get the roundtrip flags for the stage 3 block */
951 * Add code points for which the roundtrip flag is set,
952 * or which map to non-zero bytes if we use fallbacks.
953 * See ucnv_MBCSFromUnicodeWithOffsets() for details.
956 case UCNV_SET_FILTER_NONE
:
960 stage3
+=st3Multiplier
;
961 } else if(useFallback
) {
963 switch(st3Multiplier
) {
971 b
|=stage3
[0]|stage3
[1];
982 } while((++c
&0xf)!=0);
984 case UCNV_SET_FILTER_DBCS_ONLY
:
985 /* Ignore single-byte results (<0x100). */
987 if(((st3
&1)!=0 || useFallback
) && *((const uint16_t *)stage3
)>=0x100) {
991 stage3
+=2; /* +=st3Multiplier */
992 } while((++c
&0xf)!=0);
994 case UCNV_SET_FILTER_2022_CN
:
995 /* Only add code points that map to CNS 11643 planes 1 & 2 for non-EXT ISO-2022-CN. */
997 if(((st3
&1)!=0 || useFallback
) && ((value
=*stage3
)==0x81 || value
==0x82)) {
1001 stage3
+=3; /* +=st3Multiplier */
1002 } while((++c
&0xf)!=0);
1004 case UCNV_SET_FILTER_SJIS
:
1005 /* Only add code points that map to Shift-JIS codes corresponding to JIS X 0208. */
1007 if(((st3
&1)!=0 || useFallback
) && (value
=*((const uint16_t *)stage3
))>=0x8140 && value
<=0xeffc) {
1008 sa
->add(sa
->set
, c
);
1011 stage3
+=2; /* +=st3Multiplier */
1012 } while((++c
&0xf)!=0);
1014 case UCNV_SET_FILTER_GR94DBCS
:
1015 /* Only add code points that map to ISO 2022 GR 94 DBCS codes (each byte A1..FE). */
1017 if( ((st3
&1)!=0 || useFallback
) &&
1018 (uint16_t)((value
=*((const uint16_t *)stage3
)) - 0xa1a1)<=(0xfefe - 0xa1a1) &&
1019 (uint8_t)(value
-0xa1)<=(0xfe - 0xa1)
1021 sa
->add(sa
->set
, c
);
1024 stage3
+=2; /* +=st3Multiplier */
1025 } while((++c
&0xf)!=0);
1027 case UCNV_SET_FILTER_HZ
:
1028 /* Only add code points that are suitable for HZ DBCS (lead byte A1..FD). */
1030 if( ((st3
&1)!=0 || useFallback
) &&
1031 (uint16_t)((value
=*((const uint16_t *)stage3
))-0xa1a1)<=(0xfdfe - 0xa1a1) &&
1032 (uint8_t)(value
-0xa1)<=(0xfe - 0xa1)
1034 sa
->add(sa
->set
, c
);
1037 stage3
+=2; /* +=st3Multiplier */
1038 } while((++c
&0xf)!=0);
1041 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
1045 c
+=16; /* empty stage 3 block */
1049 c
+=1024; /* empty stage 2 block */
1054 ucnv_extGetUnicodeSet(sharedData
, sa
, which
, filter
, pErrorCode
);
1058 ucnv_MBCSGetUnicodeSetForUnicode(const UConverterSharedData
*sharedData
,
1059 const USetAdder
*sa
,
1060 UConverterUnicodeSet which
,
1061 UErrorCode
*pErrorCode
) {
1062 ucnv_MBCSGetFilteredUnicodeSetForUnicode(
1063 sharedData
, sa
, which
,
1064 sharedData
->mbcs
.outputType
==MBCS_OUTPUT_DBCS_ONLY
?
1065 UCNV_SET_FILTER_DBCS_ONLY
:
1066 UCNV_SET_FILTER_NONE
,
1071 ucnv_MBCSGetUnicodeSet(const UConverter
*cnv
,
1072 const USetAdder
*sa
,
1073 UConverterUnicodeSet which
,
1074 UErrorCode
*pErrorCode
) {
1075 if(cnv
->options
&_MBCS_OPTION_GB18030
) {
1076 sa
->addRange(sa
->set
, 0, 0xd7ff);
1077 sa
->addRange(sa
->set
, 0xe000, 0x10ffff);
1079 ucnv_MBCSGetUnicodeSetForUnicode(cnv
->sharedData
, sa
, which
, pErrorCode
);
1083 /* conversion extensions for input not in the main table -------------------- */
1086 * Hardcoded extension handling for GB 18030.
1087 * Definition of LINEAR macros and gb18030Ranges see near the beginning of the file.
1089 * In the future, conversion extensions may handle m:n mappings and delta tables,
1090 * see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/conversion/conversion_extensions.html
1092 * If an input character cannot be mapped, then these functions set an error
1093 * code. The framework will then call the callback function.
1097 * @return if(U_FAILURE) return the code point for cnv->fromUChar32
1098 * else return 0 after output has been written to the target
1101 _extFromU(UConverter
*cnv
, const UConverterSharedData
*sharedData
,
1103 const UChar
**source
, const UChar
*sourceLimit
,
1104 uint8_t **target
, const uint8_t *targetLimit
,
1105 int32_t **offsets
, int32_t sourceIndex
,
1107 UErrorCode
*pErrorCode
) {
1110 cnv
->useSubChar1
=FALSE
;
1112 if( (cx
=sharedData
->mbcs
.extIndexes
)!=NULL
&&
1113 ucnv_extInitialMatchFromU(
1115 cp
, source
, sourceLimit
,
1116 (char **)target
, (char *)targetLimit
,
1117 offsets
, sourceIndex
,
1121 return 0; /* an extension mapping handled the input */
1125 if((cnv
->options
&_MBCS_OPTION_GB18030
)!=0) {
1126 const uint32_t *range
;
1129 range
=gb18030Ranges
[0];
1130 for(i
=0; i
<UPRV_LENGTHOF(gb18030Ranges
); range
+=4, ++i
) {
1131 if(range
[0]<=(uint32_t)cp
&& (uint32_t)cp
<=range
[1]) {
1132 /* found the Unicode code point, output the four-byte sequence for it */
1136 /* get the linear value of the first GB 18030 code in this range */
1137 linear
=range
[2]-LINEAR_18030_BASE
;
1139 /* add the offset from the beginning of the range */
1140 linear
+=((uint32_t)cp
-range
[0]);
1142 /* turn this into a four-byte sequence */
1143 bytes
[3]=(char)(0x30+linear%10
); linear
/=10;
1144 bytes
[2]=(char)(0x81+linear%126
); linear
/=126;
1145 bytes
[1]=(char)(0x30+linear%10
); linear
/=10;
1146 bytes
[0]=(char)(0x81+linear
);
1148 /* output this sequence */
1149 ucnv_fromUWriteBytes(cnv
,
1150 bytes
, 4, (char **)target
, (char *)targetLimit
,
1151 offsets
, sourceIndex
, pErrorCode
);
1158 *pErrorCode
=U_INVALID_CHAR_FOUND
;
1163 * Input sequence: cnv->toUBytes[0..length[
1164 * @return if(U_FAILURE) return the length (toULength, byteIndex) for the input
1165 * else return 0 after output has been written to the target
1168 _extToU(UConverter
*cnv
, const UConverterSharedData
*sharedData
,
1170 const uint8_t **source
, const uint8_t *sourceLimit
,
1171 UChar
**target
, const UChar
*targetLimit
,
1172 int32_t **offsets
, int32_t sourceIndex
,
1174 UErrorCode
*pErrorCode
) {
1177 if( (cx
=sharedData
->mbcs
.extIndexes
)!=NULL
&&
1178 ucnv_extInitialMatchToU(
1180 length
, (const char **)source
, (const char *)sourceLimit
,
1181 target
, targetLimit
,
1182 offsets
, sourceIndex
,
1186 return 0; /* an extension mapping handled the input */
1190 if(length
==4 && (cnv
->options
&_MBCS_OPTION_GB18030
)!=0) {
1191 const uint32_t *range
;
1195 linear
=LINEAR_18030(cnv
->toUBytes
[0], cnv
->toUBytes
[1], cnv
->toUBytes
[2], cnv
->toUBytes
[3]);
1196 range
=gb18030Ranges
[0];
1197 for(i
=0; i
<UPRV_LENGTHOF(gb18030Ranges
); range
+=4, ++i
) {
1198 if(range
[2]<=linear
&& linear
<=range
[3]) {
1199 /* found the sequence, output the Unicode code point for it */
1200 *pErrorCode
=U_ZERO_ERROR
;
1202 /* add the linear difference between the input and start sequences to the start code point */
1203 linear
=range
[0]+(linear
-range
[2]);
1205 /* output this code point */
1206 ucnv_toUWriteCodePoint(cnv
, linear
, target
, targetLimit
, offsets
, sourceIndex
, pErrorCode
);
1214 *pErrorCode
=U_INVALID_CHAR_FOUND
;
1218 /* EBCDIC swap LF<->NL ------------------------------------------------------ */
1221 * This code modifies a standard EBCDIC<->Unicode mapping table for
1222 * OS/390 (z/OS) Unix System Services (Open Edition).
1223 * The difference is in the mapping of Line Feed and New Line control codes:
1224 * Standard EBCDIC maps
1229 * but OS/390 USS EBCDIC swaps the control codes for LF and NL,
1235 * This code modifies a loaded standard EBCDIC<->Unicode mapping table
1236 * by copying it into allocated memory and swapping the LF and NL values.
1237 * It allows to support the same EBCDIC charset in both versions without
1238 * duplicating the entire installed table.
1241 /* standard EBCDIC codes */
1242 #define EBCDIC_LF 0x25
1243 #define EBCDIC_NL 0x15
1245 /* standard EBCDIC codes with roundtrip flag as stored in Unicode-to-single-byte tables */
1246 #define EBCDIC_RT_LF 0xf25
1247 #define EBCDIC_RT_NL 0xf15
1249 /* Unicode code points */
1254 _EBCDICSwapLFNL(UConverterSharedData
*sharedData
, UErrorCode
*pErrorCode
) {
1255 UConverterMBCSTable
*mbcsTable
;
1257 const uint16_t *table
, *results
;
1258 const uint8_t *bytes
;
1260 int32_t (*newStateTable
)[256];
1261 uint16_t *newResults
;
1265 uint32_t stage2Entry
;
1266 uint32_t size
, sizeofFromUBytes
;
1268 mbcsTable
=&sharedData
->mbcs
;
1270 table
=mbcsTable
->fromUnicodeTable
;
1271 bytes
=mbcsTable
->fromUnicodeBytes
;
1272 results
=(const uint16_t *)bytes
;
1275 * Check that this is an EBCDIC table with SBCS portion -
1276 * SBCS or EBCDIC_STATEFUL with standard EBCDIC LF and NL mappings.
1278 * If not, ignore the option. Options are always ignored if they do not apply.
1281 (mbcsTable
->outputType
==MBCS_OUTPUT_1
|| mbcsTable
->outputType
==MBCS_OUTPUT_2_SISO
) &&
1282 mbcsTable
->stateTable
[0][EBCDIC_LF
]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_LF
) &&
1283 mbcsTable
->stateTable
[0][EBCDIC_NL
]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_NL
)
1288 if(mbcsTable
->outputType
==MBCS_OUTPUT_1
) {
1290 EBCDIC_RT_LF
==MBCS_SINGLE_RESULT_FROM_U(table
, results
, U_LF
) &&
1291 EBCDIC_RT_NL
==MBCS_SINGLE_RESULT_FROM_U(table
, results
, U_NL
)
1295 } else /* MBCS_OUTPUT_2_SISO */ {
1296 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_LF
);
1298 MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, U_LF
)!=0 &&
1299 EBCDIC_LF
==MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, U_LF
)
1304 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_NL
);
1306 MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, U_NL
)!=0 &&
1307 EBCDIC_NL
==MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, U_NL
)
1313 if(mbcsTable
->fromUBytesLength
>0) {
1315 * We _know_ the number of bytes in the fromUnicodeBytes array
1316 * starting with header.version 4.1.
1318 sizeofFromUBytes
=mbcsTable
->fromUBytesLength
;
1322 * There used to be code to enumerate the fromUnicode
1323 * trie and find the highest entry, but it was removed in ICU 3.2
1324 * because it was not tested and caused a low code coverage number.
1325 * See Jitterbug 3674.
1326 * This affects only some .cnv file formats with a header.version
1327 * below 4.1, and only when swaplfnl is requested.
1329 * ucnvmbcs.c revision 1.99 is the last one with the
1330 * ucnv_MBCSSizeofFromUBytes() function.
1332 *pErrorCode
=U_INVALID_FORMAT_ERROR
;
1337 * The table has an appropriate format.
1338 * Allocate and build
1339 * - a modified to-Unicode state table
1340 * - a modified from-Unicode output array
1341 * - a converter name string with the swap option appended
1344 mbcsTable
->countStates
*1024+
1346 UCNV_MAX_CONVERTER_NAME_LENGTH
+20;
1347 p
=(uint8_t *)uprv_malloc(size
);
1349 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1353 /* copy and modify the to-Unicode state table */
1354 newStateTable
=(int32_t (*)[256])p
;
1355 uprv_memcpy(newStateTable
, mbcsTable
->stateTable
, mbcsTable
->countStates
*1024);
1357 newStateTable
[0][EBCDIC_LF
]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_NL
);
1358 newStateTable
[0][EBCDIC_NL
]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_LF
);
1360 /* copy and modify the from-Unicode result table */
1361 newResults
=(uint16_t *)newStateTable
[mbcsTable
->countStates
];
1362 uprv_memcpy(newResults
, bytes
, sizeofFromUBytes
);
1364 /* conveniently, the table access macros work on the left side of expressions */
1365 if(mbcsTable
->outputType
==MBCS_OUTPUT_1
) {
1366 MBCS_SINGLE_RESULT_FROM_U(table
, newResults
, U_LF
)=EBCDIC_RT_NL
;
1367 MBCS_SINGLE_RESULT_FROM_U(table
, newResults
, U_NL
)=EBCDIC_RT_LF
;
1368 } else /* MBCS_OUTPUT_2_SISO */ {
1369 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_LF
);
1370 MBCS_VALUE_2_FROM_STAGE_2(newResults
, stage2Entry
, U_LF
)=EBCDIC_NL
;
1372 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_NL
);
1373 MBCS_VALUE_2_FROM_STAGE_2(newResults
, stage2Entry
, U_NL
)=EBCDIC_LF
;
1376 /* set the canonical converter name */
1377 name
=(char *)newResults
+sizeofFromUBytes
;
1378 uprv_strcpy(name
, sharedData
->staticData
->name
);
1379 uprv_strcat(name
, UCNV_SWAP_LFNL_OPTION_STRING
);
1381 /* set the pointers */
1383 if(mbcsTable
->swapLFNLStateTable
==NULL
) {
1384 mbcsTable
->swapLFNLStateTable
=newStateTable
;
1385 mbcsTable
->swapLFNLFromUnicodeBytes
=(uint8_t *)newResults
;
1386 mbcsTable
->swapLFNLName
=name
;
1392 /* release the allocated memory if another thread beat us to it */
1393 if(newStateTable
!=NULL
) {
1394 uprv_free(newStateTable
);
1399 /* reconstitute omitted fromUnicode data ------------------------------------ */
1401 /* for details, compare with genmbcs.c MBCSAddFromUnicode() and transformEUC() */
1402 static UBool U_CALLCONV
1403 writeStage3Roundtrip(const void *context
, uint32_t value
, UChar32 codePoints
[32]) {
1404 UConverterMBCSTable
*mbcsTable
=(UConverterMBCSTable
*)context
;
1405 const uint16_t *table
;
1411 table
=mbcsTable
->fromUnicodeTable
;
1412 bytes
=(uint8_t *)mbcsTable
->fromUnicodeBytes
;
1414 /* for EUC outputTypes, modify the value like genmbcs.c's transformEUC() */
1415 switch(mbcsTable
->outputType
) {
1416 case MBCS_OUTPUT_3_EUC
:
1418 /* short sequences are stored directly */
1419 /* code set 0 or 1 */
1420 } else if(value
<=0x8effff) {
1423 } else /* first byte is 0x8f */ {
1428 case MBCS_OUTPUT_4_EUC
:
1429 if(value
<=0xffffff) {
1430 /* short sequences are stored directly */
1431 /* code set 0 or 1 */
1432 } else if(value
<=0x8effffff) {
1435 } else /* first byte is 0x8f */ {
1444 for(i
=0; i
<=0x1f; ++value
, ++i
) {
1450 /* locate the stage 2 & 3 data */
1451 stage2
=((uint32_t *)table
)+table
[c
>>10]+((c
>>4)&0x3f);
1453 st3
=(int32_t)(uint16_t)*stage2
*16+(c
&0xf);
1455 /* write the codepage bytes into stage 3 */
1456 switch(mbcsTable
->outputType
) {
1458 case MBCS_OUTPUT_4_EUC
:
1460 p
[0]=(uint8_t)(value
>>16);
1461 p
[1]=(uint8_t)(value
>>8);
1462 p
[2]=(uint8_t)value
;
1465 ((uint32_t *)p
)[st3
]=value
;
1468 /* 2 bytes per character */
1469 ((uint16_t *)p
)[st3
]=(uint16_t)value
;
1473 /* set the roundtrip flag */
1474 *stage2
|=(1UL<<(16+(c
&0xf)));
1480 reconstituteData(UConverterMBCSTable
*mbcsTable
,
1481 uint32_t stage1Length
, uint32_t stage2Length
,
1482 uint32_t fullStage2Length
, /* lengths are numbers of units, not bytes */
1483 UErrorCode
*pErrorCode
) {
1486 uint32_t dataLength
=stage1Length
*2+fullStage2Length
*4+mbcsTable
->fromUBytesLength
;
1487 mbcsTable
->reconstitutedData
=(uint8_t *)uprv_malloc(dataLength
);
1488 if(mbcsTable
->reconstitutedData
==NULL
) {
1489 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1492 uprv_memset(mbcsTable
->reconstitutedData
, 0, dataLength
);
1494 /* copy existing data and reroute the pointers */
1495 stage1
=(uint16_t *)mbcsTable
->reconstitutedData
;
1496 uprv_memcpy(stage1
, mbcsTable
->fromUnicodeTable
, stage1Length
*2);
1498 stage2
=(uint32_t *)(stage1
+stage1Length
);
1499 uprv_memcpy(stage2
+(fullStage2Length
-stage2Length
),
1500 mbcsTable
->fromUnicodeTable
+stage1Length
,
1503 mbcsTable
->fromUnicodeTable
=stage1
;
1504 mbcsTable
->fromUnicodeBytes
=(uint8_t *)(stage2
+fullStage2Length
);
1506 /* indexes into stage 2 count from the bottom of the fromUnicodeTable */
1507 stage2
=(uint32_t *)stage1
;
1509 /* reconstitute the initial part of stage 2 from the mbcsIndex */
1511 int32_t stageUTF8Length
=((int32_t)mbcsTable
->maxFastUChar
+1)>>6;
1512 int32_t stageUTF8Index
=0;
1513 int32_t st1
, st2
, st3
, i
;
1515 for(st1
=0; stageUTF8Index
<stageUTF8Length
; ++st1
) {
1517 if(st2
!=(int32_t)stage1Length
/2) {
1518 /* each stage 2 block has 64 entries corresponding to 16 entries in the mbcsIndex */
1519 for(i
=0; i
<16; ++i
) {
1520 st3
=mbcsTable
->mbcsIndex
[stageUTF8Index
++];
1522 /* an stage 2 entry's index is per stage 3 16-block, not per stage 3 entry */
1525 * 4 stage 2 entries point to 4 consecutive stage 3 16-blocks which are
1526 * allocated together as a single 64-block for access from the mbcsIndex
1528 stage2
[st2
++]=st3
++;
1529 stage2
[st2
++]=st3
++;
1530 stage2
[st2
++]=st3
++;
1533 /* no stage 3 block, skip */
1538 /* no stage 2 block, skip */
1544 /* reconstitute fromUnicodeBytes with roundtrips from toUnicode data */
1545 ucnv_MBCSEnumToUnicode(mbcsTable
, writeStage3Roundtrip
, mbcsTable
, pErrorCode
);
1548 /* MBCS setup functions ----------------------------------------------------- */
1551 ucnv_MBCSLoad(UConverterSharedData
*sharedData
,
1552 UConverterLoadArgs
*pArgs
,
1554 UErrorCode
*pErrorCode
) {
1556 UConverterMBCSTable
*mbcsTable
=&sharedData
->mbcs
;
1557 _MBCSHeader
*header
=(_MBCSHeader
*)raw
;
1559 uint32_t headerLength
;
1560 UBool noFromU
=FALSE
;
1562 if(header
->version
[0]==4) {
1563 headerLength
=MBCS_HEADER_V4_LENGTH
;
1564 } else if(header
->version
[0]==5 && header
->version
[1]>=3 &&
1565 (header
->options
&MBCS_OPT_UNKNOWN_INCOMPATIBLE_MASK
)==0) {
1566 headerLength
=header
->options
&MBCS_OPT_LENGTH_MASK
;
1567 noFromU
=(UBool
)((header
->options
&MBCS_OPT_NO_FROM_U
)!=0);
1569 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1573 mbcsTable
->outputType
=(uint8_t)header
->flags
;
1574 if(noFromU
&& mbcsTable
->outputType
==MBCS_OUTPUT_1
) {
1575 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1579 /* extension data, header version 4.2 and higher */
1580 offset
=header
->flags
>>8;
1582 mbcsTable
->extIndexes
=(const int32_t *)(raw
+offset
);
1585 if(mbcsTable
->outputType
==MBCS_OUTPUT_EXT_ONLY
) {
1586 UConverterLoadArgs args
=UCNV_LOAD_ARGS_INITIALIZER
;
1587 UConverterSharedData
*baseSharedData
;
1588 const int32_t *extIndexes
;
1589 const char *baseName
;
1591 /* extension-only file, load the base table and set values appropriately */
1592 if((extIndexes
=mbcsTable
->extIndexes
)==NULL
) {
1593 /* extension-only file without extension */
1594 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1598 if(pArgs
->nestedLoads
!=1) {
1599 /* an extension table must not be loaded as a base table */
1600 *pErrorCode
=U_INVALID_TABLE_FILE
;
1604 /* load the base table */
1605 baseName
=(const char *)header
+headerLength
*4;
1606 if(0==uprv_strcmp(baseName
, sharedData
->staticData
->name
)) {
1607 /* forbid loading this same extension-only file */
1608 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1612 /* TODO parse package name out of the prefix of the base name in the extension .cnv file? */
1613 args
.size
=sizeof(UConverterLoadArgs
);
1615 args
.onlyTestIsLoadable
=pArgs
->onlyTestIsLoadable
;
1616 args
.reserved
=pArgs
->reserved
;
1617 args
.options
=pArgs
->options
;
1618 args
.pkg
=pArgs
->pkg
;
1620 baseSharedData
=ucnv_load(&args
, pErrorCode
);
1621 if(U_FAILURE(*pErrorCode
)) {
1624 if( baseSharedData
->staticData
->conversionType
!=UCNV_MBCS
||
1625 baseSharedData
->mbcs
.baseSharedData
!=NULL
1627 ucnv_unload(baseSharedData
);
1628 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1631 if(pArgs
->onlyTestIsLoadable
) {
1633 * Exit as soon as we know that we can load the converter
1634 * and the format is valid and supported.
1635 * The worst that can happen in the following code is a memory
1638 ucnv_unload(baseSharedData
);
1642 /* copy the base table data */
1643 uprv_memcpy(mbcsTable
, &baseSharedData
->mbcs
, sizeof(UConverterMBCSTable
));
1645 /* overwrite values with relevant ones for the extension converter */
1646 mbcsTable
->baseSharedData
=baseSharedData
;
1647 mbcsTable
->extIndexes
=extIndexes
;
1650 * It would be possible to share the swapLFNL data with a base converter,
1651 * but the generated name would have to be different, and the memory
1652 * would have to be free'd only once.
1653 * It is easier to just create the data for the extension converter
1654 * separately when it is requested.
1656 mbcsTable
->swapLFNLStateTable
=NULL
;
1657 mbcsTable
->swapLFNLFromUnicodeBytes
=NULL
;
1658 mbcsTable
->swapLFNLName
=NULL
;
1661 * The reconstitutedData must be deleted only when the base converter
1664 mbcsTable
->reconstitutedData
=NULL
;
1667 * Set a special, runtime-only outputType if the extension converter
1668 * is a DBCS version of a base converter that also maps single bytes.
1670 if( sharedData
->staticData
->conversionType
==UCNV_DBCS
||
1671 (sharedData
->staticData
->conversionType
==UCNV_MBCS
&&
1672 sharedData
->staticData
->minBytesPerChar
>=2)
1674 if(baseSharedData
->mbcs
.outputType
==MBCS_OUTPUT_2_SISO
) {
1675 /* the base converter is SI/SO-stateful */
1678 /* get the dbcs state from the state table entry for SO=0x0e */
1679 entry
=mbcsTable
->stateTable
[0][0xe];
1680 if( MBCS_ENTRY_IS_FINAL(entry
) &&
1681 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_CHANGE_ONLY
&&
1682 MBCS_ENTRY_FINAL_STATE(entry
)!=0
1684 mbcsTable
->dbcsOnlyState
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
);
1686 mbcsTable
->outputType
=MBCS_OUTPUT_DBCS_ONLY
;
1689 baseSharedData
->staticData
->conversionType
==UCNV_MBCS
&&
1690 baseSharedData
->staticData
->minBytesPerChar
==1 &&
1691 baseSharedData
->staticData
->maxBytesPerChar
==2 &&
1692 mbcsTable
->countStates
<=127
1694 /* non-stateful base converter, need to modify the state table */
1695 int32_t (*newStateTable
)[256];
1699 /* allocate a new state table and copy the base state table contents */
1700 count
=mbcsTable
->countStates
;
1701 newStateTable
=(int32_t (*)[256])uprv_malloc((count
+1)*1024);
1702 if(newStateTable
==NULL
) {
1703 ucnv_unload(baseSharedData
);
1704 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1708 uprv_memcpy(newStateTable
, mbcsTable
->stateTable
, count
*1024);
1710 /* change all final single-byte entries to go to a new all-illegal state */
1711 state
=newStateTable
[0];
1712 for(i
=0; i
<256; ++i
) {
1713 if(MBCS_ENTRY_IS_FINAL(state
[i
])) {
1714 state
[i
]=MBCS_ENTRY_TRANSITION(count
, 0);
1718 /* build the new all-illegal state */
1719 state
=newStateTable
[count
];
1720 for(i
=0; i
<256; ++i
) {
1721 state
[i
]=MBCS_ENTRY_FINAL(0, MBCS_STATE_ILLEGAL
, 0);
1723 mbcsTable
->stateTable
=(const int32_t (*)[256])newStateTable
;
1724 mbcsTable
->countStates
=(uint8_t)(count
+1);
1725 mbcsTable
->stateTableOwned
=TRUE
;
1727 mbcsTable
->outputType
=MBCS_OUTPUT_DBCS_ONLY
;
1732 * unlike below for files with base tables, do not get the unicodeMask
1733 * from the sharedData; instead, use the base table's unicodeMask,
1734 * which we copied in the memcpy above;
1735 * this is necessary because the static data unicodeMask, especially
1736 * the UCNV_HAS_SUPPLEMENTARY flag, is part of the base table data
1739 /* conversion file with a base table; an additional extension table is optional */
1740 /* make sure that the output type is known */
1741 switch(mbcsTable
->outputType
) {
1746 case MBCS_OUTPUT_3_EUC
:
1747 case MBCS_OUTPUT_4_EUC
:
1748 case MBCS_OUTPUT_2_SISO
:
1752 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1755 if(pArgs
->onlyTestIsLoadable
) {
1757 * Exit as soon as we know that we can load the converter
1758 * and the format is valid and supported.
1759 * The worst that can happen in the following code is a memory
1765 mbcsTable
->countStates
=(uint8_t)header
->countStates
;
1766 mbcsTable
->countToUFallbacks
=header
->countToUFallbacks
;
1767 mbcsTable
->stateTable
=(const int32_t (*)[256])(raw
+headerLength
*4);
1768 mbcsTable
->toUFallbacks
=(const _MBCSToUFallback
*)(mbcsTable
->stateTable
+header
->countStates
);
1769 mbcsTable
->unicodeCodeUnits
=(const uint16_t *)(raw
+header
->offsetToUCodeUnits
);
1771 mbcsTable
->fromUnicodeTable
=(const uint16_t *)(raw
+header
->offsetFromUTable
);
1772 mbcsTable
->fromUnicodeBytes
=(const uint8_t *)(raw
+header
->offsetFromUBytes
);
1773 mbcsTable
->fromUBytesLength
=header
->fromUBytesLength
;
1776 * converter versions 6.1 and up contain a unicodeMask that is
1777 * used here to select the most efficient function implementations
1779 info
.size
=sizeof(UDataInfo
);
1780 udata_getInfo((UDataMemory
*)sharedData
->dataMemory
, &info
);
1781 if(info
.formatVersion
[0]>6 || (info
.formatVersion
[0]==6 && info
.formatVersion
[1]>=1)) {
1782 /* mask off possible future extensions to be safe */
1783 mbcsTable
->unicodeMask
=(uint8_t)(sharedData
->staticData
->unicodeMask
&3);
1785 /* for older versions, assume worst case: contains anything possible (prevent over-optimizations) */
1786 mbcsTable
->unicodeMask
=UCNV_HAS_SUPPLEMENTARY
|UCNV_HAS_SURROGATES
;
1790 * _MBCSHeader.version 4.3 adds utf8Friendly data structures.
1791 * Check for the header version, SBCS vs. MBCS, and for whether the
1792 * data structures are optimized for code points as high as what the
1793 * runtime code is designed for.
1794 * The implementation does not handle mapping tables with entries for
1795 * unpaired surrogates.
1797 if( header
->version
[1]>=3 &&
1798 (mbcsTable
->unicodeMask
&UCNV_HAS_SURROGATES
)==0 &&
1799 (mbcsTable
->countStates
==1 ?
1800 (header
->version
[2]>=(SBCS_FAST_MAX
>>8)) :
1801 (header
->version
[2]>=(MBCS_FAST_MAX
>>8))
1804 mbcsTable
->utf8Friendly
=TRUE
;
1806 if(mbcsTable
->countStates
==1) {
1808 * SBCS: Stage 3 is allocated in 64-entry blocks for U+0000..SBCS_FAST_MAX or higher.
1809 * Build a table with indexes to each block, to be used instead of
1810 * the regular stage 1/2 table.
1813 for(i
=0; i
<(SBCS_FAST_LIMIT
>>6); ++i
) {
1814 mbcsTable
->sbcsIndex
[i
]=mbcsTable
->fromUnicodeTable
[mbcsTable
->fromUnicodeTable
[i
>>4]+((i
<<2)&0x3c)];
1816 /* set SBCS_FAST_MAX to reflect the reach of sbcsIndex[] even if header->version[2]>(SBCS_FAST_MAX>>8) */
1817 mbcsTable
->maxFastUChar
=SBCS_FAST_MAX
;
1820 * MBCS: Stage 3 is allocated in 64-entry blocks for U+0000..MBCS_FAST_MAX or higher.
1821 * The .cnv file is prebuilt with an additional stage table with indexes
1824 mbcsTable
->mbcsIndex
=(const uint16_t *)
1825 (mbcsTable
->fromUnicodeBytes
+
1826 (noFromU
? 0 : mbcsTable
->fromUBytesLength
));
1827 mbcsTable
->maxFastUChar
=(((UChar
)header
->version
[2])<<8)|0xff;
1831 /* calculate a bit set of 4 ASCII characters per bit that round-trip to ASCII bytes */
1833 uint32_t asciiRoundtrips
=0xffffffff;
1836 for(i
=0; i
<0x80; ++i
) {
1837 if(mbcsTable
->stateTable
[0][i
]!=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, i
)) {
1838 asciiRoundtrips
&=~((uint32_t)1<<(i
>>2));
1841 mbcsTable
->asciiRoundtrips
=asciiRoundtrips
;
1845 uint32_t stage1Length
=
1846 mbcsTable
->unicodeMask
&UCNV_HAS_SUPPLEMENTARY
?
1848 uint32_t stage2Length
=
1849 (header
->offsetFromUBytes
-header
->offsetFromUTable
)/4-
1851 reconstituteData(mbcsTable
, stage1Length
, stage2Length
, header
->fullStage2Length
, pErrorCode
);
1855 /* Set the impl pointer here so that it is set for both extension-only and base tables. */
1856 if(mbcsTable
->utf8Friendly
) {
1857 if(mbcsTable
->countStates
==1) {
1858 sharedData
->impl
=&_SBCSUTF8Impl
;
1860 if(mbcsTable
->outputType
==MBCS_OUTPUT_2
) {
1861 sharedData
->impl
=&_DBCSUTF8Impl
;
1866 if(mbcsTable
->outputType
==MBCS_OUTPUT_DBCS_ONLY
|| mbcsTable
->outputType
==MBCS_OUTPUT_2_SISO
) {
1868 * MBCS_OUTPUT_DBCS_ONLY: No SBCS mappings, therefore ASCII does not roundtrip.
1869 * MBCS_OUTPUT_2_SISO: Bypass the ASCII fastpath to handle prevLength correctly.
1871 mbcsTable
->asciiRoundtrips
=0;
1876 ucnv_MBCSUnload(UConverterSharedData
*sharedData
) {
1877 UConverterMBCSTable
*mbcsTable
=&sharedData
->mbcs
;
1879 if(mbcsTable
->swapLFNLStateTable
!=NULL
) {
1880 uprv_free(mbcsTable
->swapLFNLStateTable
);
1882 if(mbcsTable
->stateTableOwned
) {
1883 uprv_free((void *)mbcsTable
->stateTable
);
1885 if(mbcsTable
->baseSharedData
!=NULL
) {
1886 ucnv_unload(mbcsTable
->baseSharedData
);
1888 if(mbcsTable
->reconstitutedData
!=NULL
) {
1889 uprv_free(mbcsTable
->reconstitutedData
);
1894 ucnv_MBCSOpen(UConverter
*cnv
,
1895 UConverterLoadArgs
*pArgs
,
1896 UErrorCode
*pErrorCode
) {
1897 UConverterMBCSTable
*mbcsTable
;
1898 const int32_t *extIndexes
;
1900 int8_t maxBytesPerUChar
;
1902 if(pArgs
->onlyTestIsLoadable
) {
1906 mbcsTable
=&cnv
->sharedData
->mbcs
;
1907 outputType
=mbcsTable
->outputType
;
1909 if(outputType
==MBCS_OUTPUT_DBCS_ONLY
) {
1910 /* the swaplfnl option does not apply, remove it */
1911 cnv
->options
=pArgs
->options
&=~UCNV_OPTION_SWAP_LFNL
;
1914 if((pArgs
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
1915 /* do this because double-checked locking is broken */
1919 isCached
=mbcsTable
->swapLFNLStateTable
!=NULL
;
1923 if(!_EBCDICSwapLFNL(cnv
->sharedData
, pErrorCode
)) {
1924 if(U_FAILURE(*pErrorCode
)) {
1925 return; /* something went wrong */
1928 /* the option does not apply, remove it */
1929 cnv
->options
=pArgs
->options
&=~UCNV_OPTION_SWAP_LFNL
;
1934 if(uprv_strstr(pArgs
->name
, "18030")!=NULL
) {
1935 if(uprv_strstr(pArgs
->name
, "gb18030")!=NULL
|| uprv_strstr(pArgs
->name
, "GB18030")!=NULL
) {
1936 /* set a flag for GB 18030 mode, which changes the callback behavior */
1937 cnv
->options
|=_MBCS_OPTION_GB18030
;
1939 } else if((uprv_strstr(pArgs
->name
, "KEIS")!=NULL
) || (uprv_strstr(pArgs
->name
, "keis")!=NULL
)) {
1940 /* set a flag for KEIS converter, which changes the SI/SO character sequence */
1941 cnv
->options
|=_MBCS_OPTION_KEIS
;
1942 } else if((uprv_strstr(pArgs
->name
, "JEF")!=NULL
) || (uprv_strstr(pArgs
->name
, "jef")!=NULL
)) {
1943 /* set a flag for JEF converter, which changes the SI/SO character sequence */
1944 cnv
->options
|=_MBCS_OPTION_JEF
;
1945 } else if((uprv_strstr(pArgs
->name
, "JIPS")!=NULL
) || (uprv_strstr(pArgs
->name
, "jips")!=NULL
)) {
1946 /* set a flag for JIPS converter, which changes the SI/SO character sequence */
1947 cnv
->options
|=_MBCS_OPTION_JIPS
;
1950 /* fix maxBytesPerUChar depending on outputType and options etc. */
1951 if(outputType
==MBCS_OUTPUT_2_SISO
) {
1952 cnv
->maxBytesPerUChar
=3; /* SO+DBCS */
1955 extIndexes
=mbcsTable
->extIndexes
;
1956 if(extIndexes
!=NULL
) {
1957 maxBytesPerUChar
=(int8_t)UCNV_GET_MAX_BYTES_PER_UCHAR(extIndexes
);
1958 if(outputType
==MBCS_OUTPUT_2_SISO
) {
1959 ++maxBytesPerUChar
; /* SO + multiple DBCS */
1962 if(maxBytesPerUChar
>cnv
->maxBytesPerUChar
) {
1963 cnv
->maxBytesPerUChar
=maxBytesPerUChar
;
1969 * documentation of UConverter fields used for status
1970 * all of these fields are (re)set to 0 by ucnv_bld.c and ucnv_reset()
1974 cnv
->toUnicodeStatus
=0; /* offset */
1975 cnv
->mode
=0; /* state */
1976 cnv
->toULength
=0; /* byteIndex */
1980 cnv
->fromUnicodeStatus
=1; /* prevLength */
1985 ucnv_MBCSGetName(const UConverter
*cnv
) {
1986 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0 && cnv
->sharedData
->mbcs
.swapLFNLName
!=NULL
) {
1987 return cnv
->sharedData
->mbcs
.swapLFNLName
;
1989 return cnv
->sharedData
->staticData
->name
;
1993 /* MBCS-to-Unicode conversion functions ------------------------------------- */
1996 ucnv_MBCSGetFallback(UConverterMBCSTable
*mbcsTable
, uint32_t offset
) {
1997 const _MBCSToUFallback
*toUFallbacks
;
1998 uint32_t i
, start
, limit
;
2000 limit
=mbcsTable
->countToUFallbacks
;
2002 /* do a binary search for the fallback mapping */
2003 toUFallbacks
=mbcsTable
->toUFallbacks
;
2005 while(start
<limit
-1) {
2007 if(offset
<toUFallbacks
[i
].offset
) {
2014 /* did we really find it? */
2015 if(offset
==toUFallbacks
[start
].offset
) {
2016 return toUFallbacks
[start
].codePoint
;
2023 /* This version of ucnv_MBCSToUnicodeWithOffsets() is optimized for single-byte, single-state codepages. */
2025 ucnv_MBCSSingleToUnicodeWithOffsets(UConverterToUnicodeArgs
*pArgs
,
2026 UErrorCode
*pErrorCode
) {
2028 const uint8_t *source
, *sourceLimit
;
2030 const UChar
*targetLimit
;
2033 const int32_t (*stateTable
)[256];
2035 int32_t sourceIndex
;
2041 /* set up the local pointers */
2042 cnv
=pArgs
->converter
;
2043 source
=(const uint8_t *)pArgs
->source
;
2044 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
2045 target
=pArgs
->target
;
2046 targetLimit
=pArgs
->targetLimit
;
2047 offsets
=pArgs
->offsets
;
2049 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2050 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
2052 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
2055 /* sourceIndex=-1 if the current character began in the previous buffer */
2058 /* conversion loop */
2059 while(source
<sourceLimit
) {
2061 * This following test is to see if available input would overflow the output.
2062 * It does not catch output of more than one code unit that
2063 * overflows as a result of a surrogate pair or callback output
2064 * from the last source byte.
2065 * Therefore, those situations also test for overflows and will
2066 * then break the loop, too.
2068 if(target
>=targetLimit
) {
2069 /* target is full */
2070 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2074 entry
=stateTable
[0][*source
++];
2075 /* MBCS_ENTRY_IS_FINAL(entry) */
2077 /* test the most common case first */
2078 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
2079 /* output BMP code point */
2080 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2082 *offsets
++=sourceIndex
;
2085 /* normal end of action codes: prepare for a new character */
2091 * An if-else-if chain provides more reliable performance for
2092 * the most common cases compared to a switch.
2094 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2095 if(action
==MBCS_STATE_VALID_DIRECT_20
||
2096 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
2098 entry
=MBCS_ENTRY_FINAL_VALUE(entry
);
2099 /* output surrogate pair */
2100 *target
++=(UChar
)(0xd800|(UChar
)(entry
>>10));
2102 *offsets
++=sourceIndex
;
2104 c
=(UChar
)(0xdc00|(UChar
)(entry
&0x3ff));
2105 if(target
<targetLimit
) {
2108 *offsets
++=sourceIndex
;
2111 /* target overflow */
2112 cnv
->UCharErrorBuffer
[0]=c
;
2113 cnv
->UCharErrorBufferLength
=1;
2114 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2120 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2121 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
2122 /* output BMP code point */
2123 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2125 *offsets
++=sourceIndex
;
2131 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2132 /* just fall through */
2133 } else if(action
==MBCS_STATE_ILLEGAL
) {
2134 /* callback(illegal) */
2135 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2137 /* reserved, must never occur */
2142 if(U_FAILURE(*pErrorCode
)) {
2143 /* callback(illegal) */
2145 } else /* unassigned sequences indicated with byteIndex>0 */ {
2146 /* try an extension mapping */
2147 pArgs
->source
=(const char *)source
;
2148 cnv
->toUBytes
[0]=*(source
-1);
2149 cnv
->toULength
=_extToU(cnv
, cnv
->sharedData
,
2150 1, &source
, sourceLimit
,
2151 &target
, targetLimit
,
2152 &offsets
, sourceIndex
,
2155 sourceIndex
+=1+(int32_t)(source
-(const uint8_t *)pArgs
->source
);
2157 if(U_FAILURE(*pErrorCode
)) {
2158 /* not mappable or buffer overflow */
2164 /* write back the updated pointers */
2165 pArgs
->source
=(const char *)source
;
2166 pArgs
->target
=target
;
2167 pArgs
->offsets
=offsets
;
2171 * This version of ucnv_MBCSSingleToUnicodeWithOffsets() is optimized for single-byte, single-state codepages
2172 * that only map to and from the BMP.
2173 * In addition to single-byte optimizations, the offset calculations
2174 * become much easier.
2177 ucnv_MBCSSingleToBMPWithOffsets(UConverterToUnicodeArgs
*pArgs
,
2178 UErrorCode
*pErrorCode
) {
2180 const uint8_t *source
, *sourceLimit
, *lastSource
;
2182 int32_t targetCapacity
, length
;
2185 const int32_t (*stateTable
)[256];
2187 int32_t sourceIndex
;
2192 /* set up the local pointers */
2193 cnv
=pArgs
->converter
;
2194 source
=(const uint8_t *)pArgs
->source
;
2195 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
2196 target
=pArgs
->target
;
2197 targetCapacity
=(int32_t)(pArgs
->targetLimit
-pArgs
->target
);
2198 offsets
=pArgs
->offsets
;
2200 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2201 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
2203 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
2206 /* sourceIndex=-1 if the current character began in the previous buffer */
2211 * since the conversion here is 1:1 UChar:uint8_t, we need only one counter
2212 * for the minimum of the sourceLength and targetCapacity
2214 length
=(int32_t)(sourceLimit
-source
);
2215 if(length
<targetCapacity
) {
2216 targetCapacity
=length
;
2219 #if MBCS_UNROLL_SINGLE_TO_BMP
2220 /* unrolling makes it faster on Pentium III/Windows 2000 */
2221 /* unroll the loop with the most common case */
2223 if(targetCapacity
>=16) {
2224 int32_t count
, loops
, oredEntries
;
2226 loops
=count
=targetCapacity
>>4;
2228 oredEntries
=entry
=stateTable
[0][*source
++];
2229 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2230 oredEntries
|=entry
=stateTable
[0][*source
++];
2231 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2232 oredEntries
|=entry
=stateTable
[0][*source
++];
2233 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2234 oredEntries
|=entry
=stateTable
[0][*source
++];
2235 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2236 oredEntries
|=entry
=stateTable
[0][*source
++];
2237 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2238 oredEntries
|=entry
=stateTable
[0][*source
++];
2239 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2240 oredEntries
|=entry
=stateTable
[0][*source
++];
2241 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2242 oredEntries
|=entry
=stateTable
[0][*source
++];
2243 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2244 oredEntries
|=entry
=stateTable
[0][*source
++];
2245 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2246 oredEntries
|=entry
=stateTable
[0][*source
++];
2247 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2248 oredEntries
|=entry
=stateTable
[0][*source
++];
2249 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2250 oredEntries
|=entry
=stateTable
[0][*source
++];
2251 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2252 oredEntries
|=entry
=stateTable
[0][*source
++];
2253 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2254 oredEntries
|=entry
=stateTable
[0][*source
++];
2255 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2256 oredEntries
|=entry
=stateTable
[0][*source
++];
2257 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2258 oredEntries
|=entry
=stateTable
[0][*source
++];
2259 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2261 /* were all 16 entries really valid? */
2262 if(!MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(oredEntries
)) {
2263 /* no, return to the first of these 16 */
2270 targetCapacity
-=16*count
;
2273 lastSource
+=16*count
;
2275 *offsets
++=sourceIndex
++;
2276 *offsets
++=sourceIndex
++;
2277 *offsets
++=sourceIndex
++;
2278 *offsets
++=sourceIndex
++;
2279 *offsets
++=sourceIndex
++;
2280 *offsets
++=sourceIndex
++;
2281 *offsets
++=sourceIndex
++;
2282 *offsets
++=sourceIndex
++;
2283 *offsets
++=sourceIndex
++;
2284 *offsets
++=sourceIndex
++;
2285 *offsets
++=sourceIndex
++;
2286 *offsets
++=sourceIndex
++;
2287 *offsets
++=sourceIndex
++;
2288 *offsets
++=sourceIndex
++;
2289 *offsets
++=sourceIndex
++;
2290 *offsets
++=sourceIndex
++;
2297 /* conversion loop */
2298 while(targetCapacity
> 0 && source
< sourceLimit
) {
2299 entry
=stateTable
[0][*source
++];
2300 /* MBCS_ENTRY_IS_FINAL(entry) */
2302 /* test the most common case first */
2303 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
2304 /* output BMP code point */
2305 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2311 * An if-else-if chain provides more reliable performance for
2312 * the most common cases compared to a switch.
2314 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2315 if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2316 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
2317 /* output BMP code point */
2318 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2322 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2323 /* just fall through */
2324 } else if(action
==MBCS_STATE_ILLEGAL
) {
2325 /* callback(illegal) */
2326 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2328 /* reserved, must never occur */
2332 /* set offsets since the start or the last extension */
2334 int32_t count
=(int32_t)(source
-lastSource
);
2336 /* predecrement: do not set the offset for the callback-causing character */
2338 *offsets
++=sourceIndex
++;
2340 /* offset and sourceIndex are now set for the current character */
2343 if(U_FAILURE(*pErrorCode
)) {
2344 /* callback(illegal) */
2346 } else /* unassigned sequences indicated with byteIndex>0 */ {
2347 /* try an extension mapping */
2349 cnv
->toUBytes
[0]=*(source
-1);
2350 cnv
->toULength
=_extToU(cnv
, cnv
->sharedData
,
2351 1, &source
, sourceLimit
,
2352 &target
, pArgs
->targetLimit
,
2353 &offsets
, sourceIndex
,
2356 sourceIndex
+=1+(int32_t)(source
-lastSource
);
2358 if(U_FAILURE(*pErrorCode
)) {
2359 /* not mappable or buffer overflow */
2363 /* recalculate the targetCapacity after an extension mapping */
2364 targetCapacity
=(int32_t)(pArgs
->targetLimit
-target
);
2365 length
=(int32_t)(sourceLimit
-source
);
2366 if(length
<targetCapacity
) {
2367 targetCapacity
=length
;
2371 #if MBCS_UNROLL_SINGLE_TO_BMP
2372 /* unrolling makes it faster on Pentium III/Windows 2000 */
2377 if(U_SUCCESS(*pErrorCode
) && source
<sourceLimit
&& target
>=pArgs
->targetLimit
) {
2378 /* target is full */
2379 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2382 /* set offsets since the start or the last callback */
2384 size_t count
=source
-lastSource
;
2386 *offsets
++=sourceIndex
++;
2391 /* write back the updated pointers */
2392 pArgs
->source
=(const char *)source
;
2393 pArgs
->target
=target
;
2394 pArgs
->offsets
=offsets
;
2398 hasValidTrailBytes(const int32_t (*stateTable
)[256], uint8_t state
) {
2399 const int32_t *row
=stateTable
[state
];
2401 /* First test for final entries in this state for some commonly valid byte values. */
2403 if( !MBCS_ENTRY_IS_TRANSITION(entry
) &&
2404 MBCS_ENTRY_FINAL_ACTION(entry
)!=MBCS_STATE_ILLEGAL
2409 if( !MBCS_ENTRY_IS_TRANSITION(entry
) &&
2410 MBCS_ENTRY_FINAL_ACTION(entry
)!=MBCS_STATE_ILLEGAL
2414 /* Then test for final entries in this state. */
2415 for(b
=0; b
<=0xff; ++b
) {
2417 if( !MBCS_ENTRY_IS_TRANSITION(entry
) &&
2418 MBCS_ENTRY_FINAL_ACTION(entry
)!=MBCS_STATE_ILLEGAL
2423 /* Then recurse for transition entries. */
2424 for(b
=0; b
<=0xff; ++b
) {
2426 if( MBCS_ENTRY_IS_TRANSITION(entry
) &&
2427 hasValidTrailBytes(stateTable
, (uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
))
2436 * Is byte b a single/lead byte in this state?
2437 * Recurse for transition states, because here we don't want to say that
2438 * b is a lead byte if all byte sequences that start with b are illegal.
2441 isSingleOrLead(const int32_t (*stateTable
)[256], uint8_t state
, UBool isDBCSOnly
, uint8_t b
) {
2442 const int32_t *row
=stateTable
[state
];
2443 int32_t entry
=row
[b
];
2444 if(MBCS_ENTRY_IS_TRANSITION(entry
)) { /* lead byte */
2445 return hasValidTrailBytes(stateTable
, (uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
));
2447 uint8_t action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2448 if(action
==MBCS_STATE_CHANGE_ONLY
&& isDBCSOnly
) {
2449 return FALSE
; /* SI/SO are illegal for DBCS-only conversion */
2451 return action
!=MBCS_STATE_ILLEGAL
;
2457 ucnv_MBCSToUnicodeWithOffsets(UConverterToUnicodeArgs
*pArgs
,
2458 UErrorCode
*pErrorCode
) {
2460 const uint8_t *source
, *sourceLimit
;
2462 const UChar
*targetLimit
;
2465 const int32_t (*stateTable
)[256];
2466 const uint16_t *unicodeCodeUnits
;
2473 int32_t sourceIndex
, nextSourceIndex
;
2479 /* use optimized function if possible */
2480 cnv
=pArgs
->converter
;
2482 if(cnv
->preToULength
>0) {
2484 * pass sourceIndex=-1 because we continue from an earlier buffer
2485 * in the future, this may change with continuous offsets
2487 ucnv_extContinueMatchToU(cnv
, pArgs
, -1, pErrorCode
);
2489 if(U_FAILURE(*pErrorCode
) || cnv
->preToULength
<0) {
2494 if(cnv
->sharedData
->mbcs
.countStates
==1) {
2495 if(!(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
2496 ucnv_MBCSSingleToBMPWithOffsets(pArgs
, pErrorCode
);
2498 ucnv_MBCSSingleToUnicodeWithOffsets(pArgs
, pErrorCode
);
2503 /* set up the local pointers */
2504 source
=(const uint8_t *)pArgs
->source
;
2505 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
2506 target
=pArgs
->target
;
2507 targetLimit
=pArgs
->targetLimit
;
2508 offsets
=pArgs
->offsets
;
2510 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2511 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
2513 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
2515 unicodeCodeUnits
=cnv
->sharedData
->mbcs
.unicodeCodeUnits
;
2517 /* get the converter state from UConverter */
2518 offset
=cnv
->toUnicodeStatus
;
2519 byteIndex
=cnv
->toULength
;
2520 bytes
=cnv
->toUBytes
;
2523 * if we are in the SBCS state for a DBCS-only converter,
2524 * then load the DBCS state from the MBCS data
2525 * (dbcsOnlyState==0 if it is not a DBCS-only converter)
2527 if((state
=(uint8_t)(cnv
->mode
))==0) {
2528 state
=cnv
->sharedData
->mbcs
.dbcsOnlyState
;
2531 /* sourceIndex=-1 if the current character began in the previous buffer */
2532 sourceIndex
=byteIndex
==0 ? 0 : -1;
2535 /* conversion loop */
2536 while(source
<sourceLimit
) {
2538 * This following test is to see if available input would overflow the output.
2539 * It does not catch output of more than one code unit that
2540 * overflows as a result of a surrogate pair or callback output
2541 * from the last source byte.
2542 * Therefore, those situations also test for overflows and will
2543 * then break the loop, too.
2545 if(target
>=targetLimit
) {
2546 /* target is full */
2547 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2552 /* optimized loop for 1/2-byte input and BMP output */
2555 entry
=stateTable
[state
][*source
];
2556 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
2557 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
2558 offset
=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
2561 if( source
<sourceLimit
&&
2562 MBCS_ENTRY_IS_FINAL(entry
=stateTable
[state
][*source
]) &&
2563 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_VALID_16
&&
2564 (c
=unicodeCodeUnits
[offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
)])<0xfffe
2568 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2571 /* set the state and leave the optimized loop */
2572 bytes
[0]=*(source
-1);
2577 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
2578 /* output BMP code point */
2580 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2581 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2583 /* leave the optimized loop */
2587 } while(source
<sourceLimit
&& target
<targetLimit
);
2588 } else /* offsets!=NULL */ {
2590 entry
=stateTable
[state
][*source
];
2591 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
2592 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
2593 offset
=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
2596 if( source
<sourceLimit
&&
2597 MBCS_ENTRY_IS_FINAL(entry
=stateTable
[state
][*source
]) &&
2598 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_VALID_16
&&
2599 (c
=unicodeCodeUnits
[offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
)])<0xfffe
2604 *offsets
++=sourceIndex
;
2605 sourceIndex
=(nextSourceIndex
+=2);
2607 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2610 /* set the state and leave the optimized loop */
2612 bytes
[0]=*(source
-1);
2617 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
2618 /* output BMP code point */
2620 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2622 *offsets
++=sourceIndex
;
2623 sourceIndex
=++nextSourceIndex
;
2625 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2627 /* leave the optimized loop */
2631 } while(source
<sourceLimit
&& target
<targetLimit
);
2635 * these tests and break statements could be put inside the loop
2636 * if C had "break outerLoop" like Java
2638 if(source
>=sourceLimit
) {
2641 if(target
>=targetLimit
) {
2642 /* target is full */
2643 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2648 bytes
[byteIndex
++]=*source
++;
2649 } else /* byteIndex>0 */ {
2651 entry
=stateTable
[state
][bytes
[byteIndex
++]=*source
++];
2654 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
2655 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
2656 offset
+=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
2660 /* save the previous state for proper extension mapping with SI/SO-stateful converters */
2663 /* set the next state early so that we can reuse the entry variable */
2664 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2667 * An if-else-if chain provides more reliable performance for
2668 * the most common cases compared to a switch.
2670 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2671 if(action
==MBCS_STATE_VALID_16
) {
2672 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
2673 c
=unicodeCodeUnits
[offset
];
2675 /* output BMP code point */
2678 *offsets
++=sourceIndex
;
2681 } else if(c
==0xfffe) {
2682 if(UCNV_TO_U_USE_FALLBACK(cnv
) && (entry
=(int32_t)ucnv_MBCSGetFallback(&cnv
->sharedData
->mbcs
, offset
))!=0xfffe) {
2683 /* output fallback BMP code point */
2684 *target
++=(UChar
)entry
;
2686 *offsets
++=sourceIndex
;
2691 /* callback(illegal) */
2692 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2694 } else if(action
==MBCS_STATE_VALID_DIRECT_16
) {
2695 /* output BMP code point */
2696 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2698 *offsets
++=sourceIndex
;
2701 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
2702 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
2703 c
=unicodeCodeUnits
[offset
++];
2705 /* output BMP code point below 0xd800 */
2708 *offsets
++=sourceIndex
;
2711 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? c
<=0xdfff : c
<=0xdbff) {
2712 /* output roundtrip or fallback surrogate pair */
2713 *target
++=(UChar
)(c
&0xdbff);
2715 *offsets
++=sourceIndex
;
2718 if(target
<targetLimit
) {
2719 *target
++=unicodeCodeUnits
[offset
];
2721 *offsets
++=sourceIndex
;
2724 /* target overflow */
2725 cnv
->UCharErrorBuffer
[0]=unicodeCodeUnits
[offset
];
2726 cnv
->UCharErrorBufferLength
=1;
2727 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2732 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? (c
&0xfffe)==0xe000 : c
==0xe000) {
2733 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
2734 *target
++=unicodeCodeUnits
[offset
];
2736 *offsets
++=sourceIndex
;
2739 } else if(c
==0xffff) {
2740 /* callback(illegal) */
2741 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2743 } else if(action
==MBCS_STATE_VALID_DIRECT_20
||
2744 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
2746 entry
=MBCS_ENTRY_FINAL_VALUE(entry
);
2747 /* output surrogate pair */
2748 *target
++=(UChar
)(0xd800|(UChar
)(entry
>>10));
2750 *offsets
++=sourceIndex
;
2753 c
=(UChar
)(0xdc00|(UChar
)(entry
&0x3ff));
2754 if(target
<targetLimit
) {
2757 *offsets
++=sourceIndex
;
2760 /* target overflow */
2761 cnv
->UCharErrorBuffer
[0]=c
;
2762 cnv
->UCharErrorBufferLength
=1;
2763 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2768 } else if(action
==MBCS_STATE_CHANGE_ONLY
) {
2770 * This serves as a state change without any output.
2771 * It is useful for reading simple stateful encodings,
2772 * for example using just Shift-In/Shift-Out codes.
2773 * The 21 unused bits may later be used for more sophisticated
2774 * state transitions.
2776 if(cnv
->sharedData
->mbcs
.dbcsOnlyState
==0) {
2779 /* SI/SO are illegal for DBCS-only conversion */
2780 state
=(uint8_t)(cnv
->mode
); /* restore the previous state */
2782 /* callback(illegal) */
2783 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2785 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2786 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
2787 /* output BMP code point */
2788 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2790 *offsets
++=sourceIndex
;
2794 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2795 /* just fall through */
2796 } else if(action
==MBCS_STATE_ILLEGAL
) {
2797 /* callback(illegal) */
2798 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2800 /* reserved, must never occur */
2804 /* end of action codes: prepare for a new character */
2808 sourceIndex
=nextSourceIndex
;
2809 } else if(U_FAILURE(*pErrorCode
)) {
2810 /* callback(illegal) */
2813 * Ticket 5691: consistent illegal sequences:
2814 * - We include at least the first byte in the illegal sequence.
2815 * - If any of the non-initial bytes could be the start of a character,
2816 * we stop the illegal sequence before the first one of those.
2818 UBool isDBCSOnly
=(UBool
)(cnv
->sharedData
->mbcs
.dbcsOnlyState
!=0);
2821 i
<byteIndex
&& !isSingleOrLead(stateTable
, state
, isDBCSOnly
, bytes
[i
]);
2824 /* Back out some bytes. */
2825 int8_t backOutDistance
=byteIndex
-i
;
2826 int32_t bytesFromThisBuffer
=(int32_t)(source
-(const uint8_t *)pArgs
->source
);
2827 byteIndex
=i
; /* length of reported illegal byte sequence */
2828 if(backOutDistance
<=bytesFromThisBuffer
) {
2829 source
-=backOutDistance
;
2831 /* Back out bytes from the previous buffer: Need to replay them. */
2832 cnv
->preToULength
=(int8_t)(bytesFromThisBuffer
-backOutDistance
);
2833 /* preToULength is negative! */
2834 uprv_memcpy(cnv
->preToU
, bytes
+i
, -cnv
->preToULength
);
2835 source
=(const uint8_t *)pArgs
->source
;
2840 } else /* unassigned sequences indicated with byteIndex>0 */ {
2841 /* try an extension mapping */
2842 pArgs
->source
=(const char *)source
;
2843 byteIndex
=_extToU(cnv
, cnv
->sharedData
,
2844 byteIndex
, &source
, sourceLimit
,
2845 &target
, targetLimit
,
2846 &offsets
, sourceIndex
,
2849 sourceIndex
=nextSourceIndex
+=(int32_t)(source
-(const uint8_t *)pArgs
->source
);
2851 if(U_FAILURE(*pErrorCode
)) {
2852 /* not mappable or buffer overflow */
2858 /* set the converter state back into UConverter */
2859 cnv
->toUnicodeStatus
=offset
;
2861 cnv
->toULength
=byteIndex
;
2863 /* write back the updated pointers */
2864 pArgs
->source
=(const char *)source
;
2865 pArgs
->target
=target
;
2866 pArgs
->offsets
=offsets
;
2870 * This version of ucnv_MBCSGetNextUChar() is optimized for single-byte, single-state codepages.
2871 * We still need a conversion loop in case we find reserved action codes, which are to be ignored.
2874 ucnv_MBCSSingleGetNextUChar(UConverterToUnicodeArgs
*pArgs
,
2875 UErrorCode
*pErrorCode
) {
2877 const int32_t (*stateTable
)[256];
2878 const uint8_t *source
, *sourceLimit
;
2883 /* set up the local pointers */
2884 cnv
=pArgs
->converter
;
2885 source
=(const uint8_t *)pArgs
->source
;
2886 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
2887 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2888 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
2890 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
2893 /* conversion loop */
2894 while(source
<sourceLimit
) {
2895 entry
=stateTable
[0][*source
++];
2896 /* MBCS_ENTRY_IS_FINAL(entry) */
2898 /* write back the updated pointer early so that we can return directly */
2899 pArgs
->source
=(const char *)source
;
2901 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
2902 /* output BMP code point */
2903 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2907 * An if-else-if chain provides more reliable performance for
2908 * the most common cases compared to a switch.
2910 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2911 if( action
==MBCS_STATE_VALID_DIRECT_20
||
2912 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
2914 /* output supplementary code point */
2915 return (UChar32
)(MBCS_ENTRY_FINAL_VALUE(entry
)+0x10000);
2916 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2917 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
2918 /* output BMP code point */
2919 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2921 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2922 /* just fall through */
2923 } else if(action
==MBCS_STATE_ILLEGAL
) {
2924 /* callback(illegal) */
2925 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2927 /* reserved, must never occur */
2931 if(U_FAILURE(*pErrorCode
)) {
2932 /* callback(illegal) */
2934 } else /* unassigned sequence */ {
2935 /* defer to the generic implementation */
2936 pArgs
->source
=(const char *)source
-1;
2937 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
2941 /* no output because of empty input or only state changes */
2942 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
2947 * Version of _MBCSToUnicodeWithOffsets() optimized for single-character
2948 * conversion without offset handling.
2950 * When a character does not have a mapping to Unicode, then we return to the
2951 * generic ucnv_getNextUChar() code for extension/GB 18030 and error/callback
2953 * We also defer to the generic code in other complicated cases and have them
2954 * ultimately handled by _MBCSToUnicodeWithOffsets() itself.
2956 * All normal mappings and errors are handled here.
2959 ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs
*pArgs
,
2960 UErrorCode
*pErrorCode
) {
2962 const uint8_t *source
, *sourceLimit
, *lastSource
;
2964 const int32_t (*stateTable
)[256];
2965 const uint16_t *unicodeCodeUnits
;
2974 /* use optimized function if possible */
2975 cnv
=pArgs
->converter
;
2977 if(cnv
->preToULength
>0) {
2978 /* use the generic code in ucnv_getNextUChar() to continue with a partial match */
2979 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
2982 if(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SURROGATES
) {
2984 * Using the generic ucnv_getNextUChar() code lets us deal correctly
2985 * with the rare case of a codepage that maps single surrogates
2986 * without adding the complexity to this already complicated function here.
2988 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
2989 } else if(cnv
->sharedData
->mbcs
.countStates
==1) {
2990 return ucnv_MBCSSingleGetNextUChar(pArgs
, pErrorCode
);
2993 /* set up the local pointers */
2994 source
=lastSource
=(const uint8_t *)pArgs
->source
;
2995 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
2997 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2998 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
3000 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
3002 unicodeCodeUnits
=cnv
->sharedData
->mbcs
.unicodeCodeUnits
;
3004 /* get the converter state from UConverter */
3005 offset
=cnv
->toUnicodeStatus
;
3008 * if we are in the SBCS state for a DBCS-only converter,
3009 * then load the DBCS state from the MBCS data
3010 * (dbcsOnlyState==0 if it is not a DBCS-only converter)
3012 if((state
=(uint8_t)(cnv
->mode
))==0) {
3013 state
=cnv
->sharedData
->mbcs
.dbcsOnlyState
;
3016 /* conversion loop */
3018 while(source
<sourceLimit
) {
3019 entry
=stateTable
[state
][*source
++];
3020 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
3021 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
3022 offset
+=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
3024 /* optimization for 1/2-byte input and BMP output */
3025 if( source
<sourceLimit
&&
3026 MBCS_ENTRY_IS_FINAL(entry
=stateTable
[state
][*source
]) &&
3027 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_VALID_16
&&
3028 (c
=unicodeCodeUnits
[offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
)])<0xfffe
3031 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
3032 /* output BMP code point */
3036 /* save the previous state for proper extension mapping with SI/SO-stateful converters */
3039 /* set the next state early so that we can reuse the entry variable */
3040 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
3043 * An if-else-if chain provides more reliable performance for
3044 * the most common cases compared to a switch.
3046 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
3047 if(action
==MBCS_STATE_VALID_DIRECT_16
) {
3048 /* output BMP code point */
3049 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
3051 } else if(action
==MBCS_STATE_VALID_16
) {
3052 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
3053 c
=unicodeCodeUnits
[offset
];
3055 /* output BMP code point */
3057 } else if(c
==0xfffe) {
3058 if(UCNV_TO_U_USE_FALLBACK(cnv
) && (c
=ucnv_MBCSGetFallback(&cnv
->sharedData
->mbcs
, offset
))!=0xfffe) {
3062 /* callback(illegal) */
3063 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3065 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
3066 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
3067 c
=unicodeCodeUnits
[offset
++];
3069 /* output BMP code point below 0xd800 */
3071 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? c
<=0xdfff : c
<=0xdbff) {
3072 /* output roundtrip or fallback supplementary code point */
3073 c
=((c
&0x3ff)<<10)+unicodeCodeUnits
[offset
]+(0x10000-0xdc00);
3075 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? (c
&0xfffe)==0xe000 : c
==0xe000) {
3076 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
3077 c
=unicodeCodeUnits
[offset
];
3079 } else if(c
==0xffff) {
3080 /* callback(illegal) */
3081 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3083 } else if(action
==MBCS_STATE_VALID_DIRECT_20
||
3084 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
3086 /* output supplementary code point */
3087 c
=(UChar32
)(MBCS_ENTRY_FINAL_VALUE(entry
)+0x10000);
3089 } else if(action
==MBCS_STATE_CHANGE_ONLY
) {
3091 * This serves as a state change without any output.
3092 * It is useful for reading simple stateful encodings,
3093 * for example using just Shift-In/Shift-Out codes.
3094 * The 21 unused bits may later be used for more sophisticated
3095 * state transitions.
3097 if(cnv
->sharedData
->mbcs
.dbcsOnlyState
!=0) {
3098 /* SI/SO are illegal for DBCS-only conversion */
3099 state
=(uint8_t)(cnv
->mode
); /* restore the previous state */
3101 /* callback(illegal) */
3102 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3104 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
3105 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
3106 /* output BMP code point */
3107 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
3110 } else if(action
==MBCS_STATE_UNASSIGNED
) {
3111 /* just fall through */
3112 } else if(action
==MBCS_STATE_ILLEGAL
) {
3113 /* callback(illegal) */
3114 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3116 /* reserved (must never occur), or only state change */
3122 /* end of action codes: prepare for a new character */
3125 if(U_FAILURE(*pErrorCode
)) {
3126 /* callback(illegal) */
3128 } else /* unassigned sequence */ {
3129 /* defer to the generic implementation */
3130 cnv
->toUnicodeStatus
=0;
3132 pArgs
->source
=(const char *)lastSource
;
3133 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
3139 if(U_SUCCESS(*pErrorCode
) && source
==sourceLimit
&& lastSource
<source
) {
3140 /* incomplete character byte sequence */
3141 uint8_t *bytes
=cnv
->toUBytes
;
3142 cnv
->toULength
=(int8_t)(source
-lastSource
);
3144 *bytes
++=*lastSource
++;
3145 } while(lastSource
<source
);
3146 *pErrorCode
=U_TRUNCATED_CHAR_FOUND
;
3147 } else if(U_FAILURE(*pErrorCode
)) {
3148 /* callback(illegal) */
3150 * Ticket 5691: consistent illegal sequences:
3151 * - We include at least the first byte in the illegal sequence.
3152 * - If any of the non-initial bytes could be the start of a character,
3153 * we stop the illegal sequence before the first one of those.
3155 UBool isDBCSOnly
=(UBool
)(cnv
->sharedData
->mbcs
.dbcsOnlyState
!=0);
3156 uint8_t *bytes
=cnv
->toUBytes
;
3157 *bytes
++=*lastSource
++; /* first byte */
3158 if(lastSource
==source
) {
3160 } else /* lastSource<source: multi-byte character */ {
3163 lastSource
<source
&& !isSingleOrLead(stateTable
, state
, isDBCSOnly
, *lastSource
);
3166 *bytes
++=*lastSource
++;
3172 /* no output because of empty input or only state changes */
3173 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
3178 /* set the converter state back into UConverter, ready for a new character */
3179 cnv
->toUnicodeStatus
=0;
3182 /* write back the updated pointer */
3183 pArgs
->source
=(const char *)source
;
3189 * Code disabled 2002dec09 (ICU 2.4) because it is not currently used in ICU. markus
3190 * Removal improves code coverage.
3193 * This version of ucnv_MBCSSimpleGetNextUChar() is optimized for single-byte, single-state codepages.
3194 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
3195 * It does not handle conversion extensions (_extToU()).
3198 ucnv_MBCSSingleSimpleGetNextUChar(UConverterSharedData
*sharedData
,
3199 uint8_t b
, UBool useFallback
) {
3203 entry
=sharedData
->mbcs
.stateTable
[0][b
];
3204 /* MBCS_ENTRY_IS_FINAL(entry) */
3206 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
3207 /* output BMP code point */
3208 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
3212 * An if-else-if chain provides more reliable performance for
3213 * the most common cases compared to a switch.
3215 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
3216 if(action
==MBCS_STATE_VALID_DIRECT_20
) {
3217 /* output supplementary code point */
3218 return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
3219 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
3220 if(!TO_U_USE_FALLBACK(useFallback
)) {
3223 /* output BMP code point */
3224 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
3225 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_20
) {
3226 if(!TO_U_USE_FALLBACK(useFallback
)) {
3229 /* output supplementary code point */
3230 return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
3231 } else if(action
==MBCS_STATE_UNASSIGNED
) {
3233 } else if(action
==MBCS_STATE_ILLEGAL
) {
3236 /* reserved, must never occur */
3243 * This is a simple version of _MBCSGetNextUChar() that is used
3244 * by other converter implementations.
3245 * It only returns an "assigned" result if it consumes the entire input.
3246 * It does not use state from the converter, nor error codes.
3247 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
3248 * It handles conversion extensions but not GB 18030.
3253 * otherwise the Unicode code point
3256 ucnv_MBCSSimpleGetNextUChar(UConverterSharedData
*sharedData
,
3257 const char *source
, int32_t length
,
3258 UBool useFallback
) {
3259 const int32_t (*stateTable
)[256];
3260 const uint16_t *unicodeCodeUnits
;
3263 uint8_t state
, action
;
3269 /* no input at all: "illegal" */
3275 * Code disabled 2002dec09 (ICU 2.4) because it is not currently used in ICU. markus
3276 * TODO In future releases, verify that this function is never called for SBCS
3277 * conversions, i.e., that sharedData->mbcs.countStates==1 is still true.
3278 * Removal improves code coverage.
3280 /* use optimized function if possible */
3281 if(sharedData
->mbcs
.countStates
==1) {
3283 return ucnv_MBCSSingleSimpleGetNextUChar(sharedData
, (uint8_t)*source
, useFallback
);
3285 return 0xffff; /* illegal: more than a single byte for an SBCS converter */
3290 /* set up the local pointers */
3291 stateTable
=sharedData
->mbcs
.stateTable
;
3292 unicodeCodeUnits
=sharedData
->mbcs
.unicodeCodeUnits
;
3294 /* converter state */
3296 state
=sharedData
->mbcs
.dbcsOnlyState
;
3298 /* conversion loop */
3300 entry
=stateTable
[state
][(uint8_t)source
[i
++]];
3301 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
3302 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
3303 offset
+=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
3306 return 0xffff; /* truncated character */
3310 * An if-else-if chain provides more reliable performance for
3311 * the most common cases compared to a switch.
3313 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
3314 if(action
==MBCS_STATE_VALID_16
) {
3315 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
3316 c
=unicodeCodeUnits
[offset
];
3319 } else if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
3320 c
=ucnv_MBCSGetFallback(&sharedData
->mbcs
, offset
);
3321 /* else done with 0xfffe */
3324 } else if(action
==MBCS_STATE_VALID_DIRECT_16
) {
3325 /* output BMP code point */
3326 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
3328 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
3329 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
3330 c
=unicodeCodeUnits
[offset
++];
3332 /* output BMP code point below 0xd800 */
3333 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? c
<=0xdfff : c
<=0xdbff) {
3334 /* output roundtrip or fallback supplementary code point */
3335 c
=(UChar32
)(((c
&0x3ff)<<10)+unicodeCodeUnits
[offset
]+(0x10000-0xdc00));
3336 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? (c
&0xfffe)==0xe000 : c
==0xe000) {
3337 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
3338 c
=unicodeCodeUnits
[offset
];
3339 } else if(c
==0xffff) {
3345 } else if(action
==MBCS_STATE_VALID_DIRECT_20
) {
3346 /* output supplementary code point */
3347 c
=0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
3349 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
3350 if(!TO_U_USE_FALLBACK(useFallback
)) {
3354 /* output BMP code point */
3355 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
3357 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_20
) {
3358 if(!TO_U_USE_FALLBACK(useFallback
)) {
3362 /* output supplementary code point */
3363 c
=0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
3365 } else if(action
==MBCS_STATE_UNASSIGNED
) {
3371 * forbid MBCS_STATE_CHANGE_ONLY for this function,
3372 * and MBCS_STATE_ILLEGAL and reserved action codes
3379 /* illegal for this function: not all input consumed */
3384 /* try an extension mapping */
3385 const int32_t *cx
=sharedData
->mbcs
.extIndexes
;
3387 return ucnv_extSimpleMatchToU(cx
, source
, length
, useFallback
);
3394 /* MBCS-from-Unicode conversion functions ----------------------------------- */
3396 /* This version of ucnv_MBCSFromUnicodeWithOffsets() is optimized for double-byte codepages. */
3398 ucnv_MBCSDoubleFromUnicodeWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
3399 UErrorCode
*pErrorCode
) {
3401 const UChar
*source
, *sourceLimit
;
3403 int32_t targetCapacity
;
3406 const uint16_t *table
;
3407 const uint16_t *mbcsIndex
;
3408 const uint8_t *bytes
;
3412 int32_t sourceIndex
, nextSourceIndex
;
3414 uint32_t stage2Entry
;
3415 uint32_t asciiRoundtrips
;
3417 uint8_t unicodeMask
;
3419 /* use optimized function if possible */
3420 cnv
=pArgs
->converter
;
3421 unicodeMask
=cnv
->sharedData
->mbcs
.unicodeMask
;
3423 /* set up the local pointers */
3424 source
=pArgs
->source
;
3425 sourceLimit
=pArgs
->sourceLimit
;
3426 target
=(uint8_t *)pArgs
->target
;
3427 targetCapacity
=(int32_t)(pArgs
->targetLimit
-pArgs
->target
);
3428 offsets
=pArgs
->offsets
;
3430 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
3431 mbcsIndex
=cnv
->sharedData
->mbcs
.mbcsIndex
;
3432 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
3433 bytes
=cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
3435 bytes
=cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
3437 asciiRoundtrips
=cnv
->sharedData
->mbcs
.asciiRoundtrips
;
3439 /* get the converter state from UConverter */
3442 /* sourceIndex=-1 if the current character began in the previous buffer */
3443 sourceIndex
= c
==0 ? 0 : -1;
3446 /* conversion loop */
3447 if(c
!=0 && targetCapacity
>0) {
3451 while(source
<sourceLimit
) {
3453 * This following test is to see if available input would overflow the output.
3454 * It does not catch output of more than one byte that
3455 * overflows as a result of a multi-byte character or callback output
3456 * from the last source character.
3457 * Therefore, those situations also test for overflows and will
3458 * then break the loop, too.
3460 if(targetCapacity
>0) {
3462 * Get a correct Unicode code point:
3463 * a single UChar for a BMP code point or
3464 * a matched surrogate pair for a "supplementary code point".
3468 if(c
<=0x7f && IS_ASCII_ROUNDTRIP(c
, asciiRoundtrips
)) {
3469 *target
++=(uint8_t)c
;
3471 *offsets
++=sourceIndex
;
3472 sourceIndex
=nextSourceIndex
;
3479 * utf8Friendly table: Test for <=0xd7ff rather than <=MBCS_FAST_MAX
3480 * to avoid dealing with surrogates.
3481 * MBCS_FAST_MAX must be >=0xd7ff.
3484 value
=DBCS_RESULT_FROM_MOST_BMP(mbcsIndex
, (const uint16_t *)bytes
, c
);
3485 /* There are only roundtrips (!=0) and no-mapping (==0) entries. */
3489 /* output the value */
3492 * This also tests if the codepage maps single surrogates.
3493 * If it does, then surrogates are not paired but mapped separately.
3494 * Note that in this case unmatched surrogates are not detected.
3496 if(U16_IS_SURROGATE(c
) && !(unicodeMask
&UCNV_HAS_SURROGATES
)) {
3497 if(U16_IS_SURROGATE_LEAD(c
)) {
3499 if(source
<sourceLimit
) {
3500 /* test the following code unit */
3501 UChar trail
=*source
;
3502 if(U16_IS_TRAIL(trail
)) {
3505 c
=U16_GET_SUPPLEMENTARY(c
, trail
);
3506 if(!(unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
3507 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
3508 /* callback(unassigned) */
3511 /* convert this supplementary code point */
3512 /* exit this condition tree */
3514 /* this is an unmatched lead code unit (1st surrogate) */
3515 /* callback(illegal) */
3516 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3524 /* this is an unmatched trail code unit (2nd surrogate) */
3525 /* callback(illegal) */
3526 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3531 /* convert the Unicode code point in c into codepage bytes */
3532 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
3534 /* get the bytes and the length for the output */
3536 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
3538 /* is this code point assigned, or do we use fallbacks? */
3539 if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
) ||
3540 (UCNV_FROM_U_USE_FALLBACK(cnv
, c
) && value
!=0))
3543 * We allow a 0 byte output if the "assigned" bit is set for this entry.
3544 * There is no way with this data structure for fallback output
3545 * to be a zero byte.
3549 /* try an extension mapping */
3550 pArgs
->source
=source
;
3551 c
=_extFromU(cnv
, cnv
->sharedData
,
3552 c
, &source
, sourceLimit
,
3553 &target
, target
+targetCapacity
,
3554 &offsets
, sourceIndex
,
3557 nextSourceIndex
+=(int32_t)(source
-pArgs
->source
);
3559 if(U_FAILURE(*pErrorCode
)) {
3560 /* not mappable or buffer overflow */
3563 /* a mapping was written to the target, continue */
3565 /* recalculate the targetCapacity after an extension mapping */
3566 targetCapacity
=(int32_t)(pArgs
->targetLimit
-(char *)target
);
3568 /* normal end of conversion: prepare for a new character */
3569 sourceIndex
=nextSourceIndex
;
3575 /* write the output character bytes from value and length */
3576 /* from the first if in the loop we know that targetCapacity>0 */
3578 /* this is easy because we know that there is enough space */
3579 *target
++=(uint8_t)value
;
3581 *offsets
++=sourceIndex
;
3584 } else /* length==2 */ {
3585 *target
++=(uint8_t)(value
>>8);
3586 if(2<=targetCapacity
) {
3587 *target
++=(uint8_t)value
;
3589 *offsets
++=sourceIndex
;
3590 *offsets
++=sourceIndex
;
3595 *offsets
++=sourceIndex
;
3597 cnv
->charErrorBuffer
[0]=(char)value
;
3598 cnv
->charErrorBufferLength
=1;
3600 /* target overflow */
3602 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
3608 /* normal end of conversion: prepare for a new character */
3610 sourceIndex
=nextSourceIndex
;
3613 /* target is full */
3614 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
3619 /* set the converter state back into UConverter */
3622 /* write back the updated pointers */
3623 pArgs
->source
=source
;
3624 pArgs
->target
=(char *)target
;
3625 pArgs
->offsets
=offsets
;
3628 /* This version of ucnv_MBCSFromUnicodeWithOffsets() is optimized for single-byte codepages. */
3630 ucnv_MBCSSingleFromUnicodeWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
3631 UErrorCode
*pErrorCode
) {
3633 const UChar
*source
, *sourceLimit
;
3635 int32_t targetCapacity
;
3638 const uint16_t *table
;
3639 const uint16_t *results
;
3643 int32_t sourceIndex
, nextSourceIndex
;
3645 uint16_t value
, minValue
;
3646 UBool hasSupplementary
;
3648 /* set up the local pointers */
3649 cnv
=pArgs
->converter
;
3650 source
=pArgs
->source
;
3651 sourceLimit
=pArgs
->sourceLimit
;
3652 target
=(uint8_t *)pArgs
->target
;
3653 targetCapacity
=(int32_t)(pArgs
->targetLimit
-pArgs
->target
);
3654 offsets
=pArgs
->offsets
;
3656 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
3657 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
3658 results
=(uint16_t *)cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
3660 results
=(uint16_t *)cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
3663 if(cnv
->useFallback
) {
3664 /* use all roundtrip and fallback results */
3667 /* use only roundtrips and fallbacks from private-use characters */
3670 hasSupplementary
=(UBool
)(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
);
3672 /* get the converter state from UConverter */
3675 /* sourceIndex=-1 if the current character began in the previous buffer */
3676 sourceIndex
= c
==0 ? 0 : -1;
3679 /* conversion loop */
3680 if(c
!=0 && targetCapacity
>0) {
3684 while(source
<sourceLimit
) {
3686 * This following test is to see if available input would overflow the output.
3687 * It does not catch output of more than one byte that
3688 * overflows as a result of a multi-byte character or callback output
3689 * from the last source character.
3690 * Therefore, those situations also test for overflows and will
3691 * then break the loop, too.
3693 if(targetCapacity
>0) {
3695 * Get a correct Unicode code point:
3696 * a single UChar for a BMP code point or
3697 * a matched surrogate pair for a "supplementary code point".
3701 if(U16_IS_SURROGATE(c
)) {
3702 if(U16_IS_SURROGATE_LEAD(c
)) {
3704 if(source
<sourceLimit
) {
3705 /* test the following code unit */
3706 UChar trail
=*source
;
3707 if(U16_IS_TRAIL(trail
)) {
3710 c
=U16_GET_SUPPLEMENTARY(c
, trail
);
3711 if(!hasSupplementary
) {
3712 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
3713 /* callback(unassigned) */
3716 /* convert this supplementary code point */
3717 /* exit this condition tree */
3719 /* this is an unmatched lead code unit (1st surrogate) */
3720 /* callback(illegal) */
3721 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3729 /* this is an unmatched trail code unit (2nd surrogate) */
3730 /* callback(illegal) */
3731 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3736 /* convert the Unicode code point in c into codepage bytes */
3737 value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3739 /* is this code point assigned, or do we use fallbacks? */
3740 if(value
>=minValue
) {
3741 /* assigned, write the output character bytes from value and length */
3743 /* this is easy because we know that there is enough space */
3744 *target
++=(uint8_t)value
;
3746 *offsets
++=sourceIndex
;
3750 /* normal end of conversion: prepare for a new character */
3752 sourceIndex
=nextSourceIndex
;
3753 } else { /* unassigned */
3755 /* try an extension mapping */
3756 pArgs
->source
=source
;
3757 c
=_extFromU(cnv
, cnv
->sharedData
,
3758 c
, &source
, sourceLimit
,
3759 &target
, target
+targetCapacity
,
3760 &offsets
, sourceIndex
,
3763 nextSourceIndex
+=(int32_t)(source
-pArgs
->source
);
3765 if(U_FAILURE(*pErrorCode
)) {
3766 /* not mappable or buffer overflow */
3769 /* a mapping was written to the target, continue */
3771 /* recalculate the targetCapacity after an extension mapping */
3772 targetCapacity
=(int32_t)(pArgs
->targetLimit
-(char *)target
);
3774 /* normal end of conversion: prepare for a new character */
3775 sourceIndex
=nextSourceIndex
;
3779 /* target is full */
3780 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
3785 /* set the converter state back into UConverter */
3788 /* write back the updated pointers */
3789 pArgs
->source
=source
;
3790 pArgs
->target
=(char *)target
;
3791 pArgs
->offsets
=offsets
;
3795 * This version of ucnv_MBCSFromUnicode() is optimized for single-byte codepages
3796 * that map only to and from the BMP.
3797 * In addition to single-byte/state optimizations, the offset calculations
3798 * become much easier.
3799 * It would be possible to use the sbcsIndex for UTF-8-friendly tables,
3800 * but measurements have shown that this diminishes performance
3801 * in more cases than it improves it.
3802 * See SVN revision 21013 (2007-feb-06) for the last version with #if switches
3803 * for various MBCS and SBCS optimizations.
3806 ucnv_MBCSSingleFromBMPWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
3807 UErrorCode
*pErrorCode
) {
3809 const UChar
*source
, *sourceLimit
, *lastSource
;
3811 int32_t targetCapacity
, length
;
3814 const uint16_t *table
;
3815 const uint16_t *results
;
3819 int32_t sourceIndex
;
3821 uint32_t asciiRoundtrips
;
3822 uint16_t value
, minValue
;
3824 /* set up the local pointers */
3825 cnv
=pArgs
->converter
;
3826 source
=pArgs
->source
;
3827 sourceLimit
=pArgs
->sourceLimit
;
3828 target
=(uint8_t *)pArgs
->target
;
3829 targetCapacity
=(int32_t)(pArgs
->targetLimit
-pArgs
->target
);
3830 offsets
=pArgs
->offsets
;
3832 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
3833 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
3834 results
=(uint16_t *)cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
3836 results
=(uint16_t *)cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
3838 asciiRoundtrips
=cnv
->sharedData
->mbcs
.asciiRoundtrips
;
3840 if(cnv
->useFallback
) {
3841 /* use all roundtrip and fallback results */
3844 /* use only roundtrips and fallbacks from private-use characters */
3848 /* get the converter state from UConverter */
3851 /* sourceIndex=-1 if the current character began in the previous buffer */
3852 sourceIndex
= c
==0 ? 0 : -1;
3856 * since the conversion here is 1:1 UChar:uint8_t, we need only one counter
3857 * for the minimum of the sourceLength and targetCapacity
3859 length
=(int32_t)(sourceLimit
-source
);
3860 if(length
<targetCapacity
) {
3861 targetCapacity
=length
;
3864 /* conversion loop */
3865 if(c
!=0 && targetCapacity
>0) {
3869 #if MBCS_UNROLL_SINGLE_FROM_BMP
3870 /* unrolling makes it slower on Pentium III/Windows 2000?! */
3871 /* unroll the loop with the most common case */
3873 if(targetCapacity
>=4) {
3874 int32_t count
, loops
;
3875 uint16_t andedValues
;
3877 loops
=count
=targetCapacity
>>2;
3880 andedValues
=value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3881 *target
++=(uint8_t)value
;
3883 andedValues
&=value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3884 *target
++=(uint8_t)value
;
3886 andedValues
&=value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3887 *target
++=(uint8_t)value
;
3889 andedValues
&=value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3890 *target
++=(uint8_t)value
;
3892 /* were all 4 entries really valid? */
3893 if(andedValues
<minValue
) {
3894 /* no, return to the first of these 4 */
3901 targetCapacity
-=4*count
;
3904 lastSource
+=4*count
;
3906 *offsets
++=sourceIndex
++;
3907 *offsets
++=sourceIndex
++;
3908 *offsets
++=sourceIndex
++;
3909 *offsets
++=sourceIndex
++;
3918 while(targetCapacity
>0) {
3920 * Get a correct Unicode code point:
3921 * a single UChar for a BMP code point or
3922 * a matched surrogate pair for a "supplementary code point".
3926 * Do not immediately check for single surrogates:
3927 * Assume that they are unassigned and check for them in that case.
3928 * This speeds up the conversion of assigned characters.
3930 /* convert the Unicode code point in c into codepage bytes */
3931 if(c
<=0x7f && IS_ASCII_ROUNDTRIP(c
, asciiRoundtrips
)) {
3932 *target
++=(uint8_t)c
;
3937 value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3938 /* is this code point assigned, or do we use fallbacks? */
3939 if(value
>=minValue
) {
3940 /* assigned, write the output character bytes from value and length */
3942 /* this is easy because we know that there is enough space */
3943 *target
++=(uint8_t)value
;
3946 /* normal end of conversion: prepare for a new character */
3949 } else if(!U16_IS_SURROGATE(c
)) {
3950 /* normal, unassigned BMP character */
3951 } else if(U16_IS_SURROGATE_LEAD(c
)) {
3953 if(source
<sourceLimit
) {
3954 /* test the following code unit */
3955 UChar trail
=*source
;
3956 if(U16_IS_TRAIL(trail
)) {
3958 c
=U16_GET_SUPPLEMENTARY(c
, trail
);
3959 /* this codepage does not map supplementary code points */
3960 /* callback(unassigned) */
3962 /* this is an unmatched lead code unit (1st surrogate) */
3963 /* callback(illegal) */
3964 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3970 *pErrorCode
=U_TRUNCATED_CHAR_FOUND
;
3975 /* this is an unmatched trail code unit (2nd surrogate) */
3976 /* callback(illegal) */
3977 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3981 /* c does not have a mapping */
3983 /* get the number of code units for c to correctly advance sourceIndex */
3984 length
=U16_LENGTH(c
);
3986 /* set offsets since the start or the last extension */
3988 int32_t count
=(int32_t)(source
-lastSource
);
3990 /* do not set the offset for this character */
3994 *offsets
++=sourceIndex
++;
3997 /* offsets and sourceIndex are now set for the current character */
4000 /* try an extension mapping */
4002 c
=_extFromU(cnv
, cnv
->sharedData
,
4003 c
, &source
, sourceLimit
,
4004 &target
, (const uint8_t *)(pArgs
->targetLimit
),
4005 &offsets
, sourceIndex
,
4008 sourceIndex
+=length
+(int32_t)(source
-lastSource
);
4011 if(U_FAILURE(*pErrorCode
)) {
4012 /* not mappable or buffer overflow */
4015 /* a mapping was written to the target, continue */
4017 /* recalculate the targetCapacity after an extension mapping */
4018 targetCapacity
=(int32_t)(pArgs
->targetLimit
-(char *)target
);
4019 length
=(int32_t)(sourceLimit
-source
);
4020 if(length
<targetCapacity
) {
4021 targetCapacity
=length
;
4025 #if MBCS_UNROLL_SINGLE_FROM_BMP
4026 /* unrolling makes it slower on Pentium III/Windows 2000?! */
4031 if(U_SUCCESS(*pErrorCode
) && source
<sourceLimit
&& target
>=(uint8_t *)pArgs
->targetLimit
) {
4032 /* target is full */
4033 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
4036 /* set offsets since the start or the last callback */
4038 size_t count
=source
-lastSource
;
4039 if (count
> 0 && *pErrorCode
== U_TRUNCATED_CHAR_FOUND
) {
4041 Caller gave us a partial supplementary character,
4042 which this function couldn't convert in any case.
4043 The callback will handle the offset.
4048 *offsets
++=sourceIndex
++;
4053 /* set the converter state back into UConverter */
4056 /* write back the updated pointers */
4057 pArgs
->source
=source
;
4058 pArgs
->target
=(char *)target
;
4059 pArgs
->offsets
=offsets
;
4063 ucnv_MBCSFromUnicodeWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
4064 UErrorCode
*pErrorCode
) {
4066 const UChar
*source
, *sourceLimit
;
4068 int32_t targetCapacity
;
4071 const uint16_t *table
;
4072 const uint16_t *mbcsIndex
;
4073 const uint8_t *p
, *bytes
;
4078 int32_t prevSourceIndex
, sourceIndex
, nextSourceIndex
;
4080 uint32_t stage2Entry
;
4081 uint32_t asciiRoundtrips
;
4083 /* Shift-In and Shift-Out byte sequences differ by encoding scheme. */
4084 uint8_t siBytes
[2] = {0, 0};
4085 uint8_t soBytes
[2] = {0, 0};
4086 uint8_t siLength
, soLength
;
4087 int32_t length
= 0, prevLength
;
4088 uint8_t unicodeMask
;
4090 cnv
=pArgs
->converter
;
4092 if(cnv
->preFromUFirstCP
>=0) {
4094 * pass sourceIndex=-1 because we continue from an earlier buffer
4095 * in the future, this may change with continuous offsets
4097 ucnv_extContinueMatchFromU(cnv
, pArgs
, -1, pErrorCode
);
4099 if(U_FAILURE(*pErrorCode
) || cnv
->preFromULength
<0) {
4104 /* use optimized function if possible */
4105 outputType
=cnv
->sharedData
->mbcs
.outputType
;
4106 unicodeMask
=cnv
->sharedData
->mbcs
.unicodeMask
;
4107 if(outputType
==MBCS_OUTPUT_1
&& !(unicodeMask
&UCNV_HAS_SURROGATES
)) {
4108 if(!(unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
4109 ucnv_MBCSSingleFromBMPWithOffsets(pArgs
, pErrorCode
);
4111 ucnv_MBCSSingleFromUnicodeWithOffsets(pArgs
, pErrorCode
);
4114 } else if(outputType
==MBCS_OUTPUT_2
&& cnv
->sharedData
->mbcs
.utf8Friendly
) {
4115 ucnv_MBCSDoubleFromUnicodeWithOffsets(pArgs
, pErrorCode
);
4119 /* set up the local pointers */
4120 source
=pArgs
->source
;
4121 sourceLimit
=pArgs
->sourceLimit
;
4122 target
=(uint8_t *)pArgs
->target
;
4123 targetCapacity
=(int32_t)(pArgs
->targetLimit
-pArgs
->target
);
4124 offsets
=pArgs
->offsets
;
4126 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
4127 if(cnv
->sharedData
->mbcs
.utf8Friendly
) {
4128 mbcsIndex
=cnv
->sharedData
->mbcs
.mbcsIndex
;
4132 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
4133 bytes
=cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
4135 bytes
=cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
4137 asciiRoundtrips
=cnv
->sharedData
->mbcs
.asciiRoundtrips
;
4139 /* get the converter state from UConverter */
4142 if(outputType
==MBCS_OUTPUT_2_SISO
) {
4143 prevLength
=cnv
->fromUnicodeStatus
;
4145 /* set the real value */
4149 /* prevent fromUnicodeStatus from being set to something non-0 */
4153 /* sourceIndex=-1 if the current character began in the previous buffer */
4155 sourceIndex
= c
==0 ? 0 : -1;
4158 /* Get the SI/SO character for the converter */
4159 siLength
= getSISOBytes(SI
, cnv
->options
, siBytes
);
4160 soLength
= getSISOBytes(SO
, cnv
->options
, soBytes
);
4162 /* conversion loop */
4164 * This is another piece of ugly code:
4165 * A goto into the loop if the converter state contains a first surrogate
4166 * from the previous function call.
4167 * It saves me to check in each loop iteration a check of if(c==0)
4168 * and duplicating the trail-surrogate-handling code in the else
4169 * branch of that check.
4170 * I could not find any other way to get around this other than
4171 * using a function call for the conversion and callback, which would
4172 * be even more inefficient.
4174 * Markus Scherer 2000-jul-19
4176 if(c
!=0 && targetCapacity
>0) {
4180 while(source
<sourceLimit
) {
4182 * This following test is to see if available input would overflow the output.
4183 * It does not catch output of more than one byte that
4184 * overflows as a result of a multi-byte character or callback output
4185 * from the last source character.
4186 * Therefore, those situations also test for overflows and will
4187 * then break the loop, too.
4189 if(targetCapacity
>0) {
4191 * Get a correct Unicode code point:
4192 * a single UChar for a BMP code point or
4193 * a matched surrogate pair for a "supplementary code point".
4197 if(c
<=0x7f && IS_ASCII_ROUNDTRIP(c
, asciiRoundtrips
)) {
4198 *target
++=(uint8_t)c
;
4200 *offsets
++=sourceIndex
;
4201 prevSourceIndex
=sourceIndex
;
4202 sourceIndex
=nextSourceIndex
;
4209 * utf8Friendly table: Test for <=0xd7ff rather than <=MBCS_FAST_MAX
4210 * to avoid dealing with surrogates.
4211 * MBCS_FAST_MAX must be >=0xd7ff.
4213 if(c
<=0xd7ff && mbcsIndex
!=NULL
) {
4214 value
=mbcsIndex
[c
>>6];
4216 /* get the bytes and the length for the output (copied from below and adapted for utf8Friendly data) */
4217 /* There are only roundtrips (!=0) and no-mapping (==0) entries. */
4218 switch(outputType
) {
4220 value
=((const uint16_t *)bytes
)[value
+(c
&0x3f)];
4231 case MBCS_OUTPUT_2_SISO
:
4232 /* 1/2-byte stateful with Shift-In/Shift-Out */
4234 * Save the old state in the converter object
4235 * right here, then change the local prevLength state variable if necessary.
4236 * Then, if this character turns out to be unassigned or a fallback that
4237 * is not taken, the callback code must not save the new state in the converter
4238 * because the new state is for a character that is not output.
4239 * However, the callback must still restore the state from the converter
4240 * in case the callback function changed it for its output.
4242 cnv
->fromUnicodeStatus
=prevLength
; /* save the old state */
4243 value
=((const uint16_t *)bytes
)[value
+(c
&0x3f)];
4247 } else if(prevLength
<=1) {
4250 /* change from double-byte mode to single-byte */
4251 if (siLength
== 1) {
4252 value
|=(uint32_t)siBytes
[0]<<8;
4254 } else if (siLength
== 2) {
4255 value
|=(uint32_t)siBytes
[1]<<8;
4256 value
|=(uint32_t)siBytes
[0]<<16;
4265 /* change from single-byte mode to double-byte */
4266 if (soLength
== 1) {
4267 value
|=(uint32_t)soBytes
[0]<<16;
4269 } else if (soLength
== 2) {
4270 value
|=(uint32_t)soBytes
[1]<<16;
4271 value
|=(uint32_t)soBytes
[0]<<24;
4278 case MBCS_OUTPUT_DBCS_ONLY
:
4279 /* table with single-byte results, but only DBCS mappings used */
4280 value
=((const uint16_t *)bytes
)[value
+(c
&0x3f)];
4282 /* no mapping or SBCS result, not taken for DBCS-only */
4289 p
=bytes
+(value
+(c
&0x3f))*3;
4290 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4297 } else if(value
<=0xffff) {
4304 value
=((const uint32_t *)bytes
)[value
+(c
&0x3f)];
4311 } else if(value
<=0xffff) {
4313 } else if(value
<=0xffffff) {
4319 case MBCS_OUTPUT_3_EUC
:
4320 value
=((const uint16_t *)bytes
)[value
+(c
&0x3f)];
4321 /* EUC 16-bit fixed-length representation */
4328 } else if((value
&0x8000)==0) {
4331 } else if((value
&0x80)==0) {
4338 case MBCS_OUTPUT_4_EUC
:
4339 p
=bytes
+(value
+(c
&0x3f))*3;
4340 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4341 /* EUC 16-bit fixed-length representation applied to the first two bytes */
4348 } else if(value
<=0xffff) {
4350 } else if((value
&0x800000)==0) {
4353 } else if((value
&0x8000)==0) {
4361 /* must not occur */
4363 * To avoid compiler warnings that value & length may be
4364 * used without having been initialized, we set them here.
4365 * In reality, this is unreachable code.
4366 * Not having a default branch also causes warnings with
4373 /* output the value */
4376 * This also tests if the codepage maps single surrogates.
4377 * If it does, then surrogates are not paired but mapped separately.
4378 * Note that in this case unmatched surrogates are not detected.
4380 if(U16_IS_SURROGATE(c
) && !(unicodeMask
&UCNV_HAS_SURROGATES
)) {
4381 if(U16_IS_SURROGATE_LEAD(c
)) {
4383 if(source
<sourceLimit
) {
4384 /* test the following code unit */
4385 UChar trail
=*source
;
4386 if(U16_IS_TRAIL(trail
)) {
4389 c
=U16_GET_SUPPLEMENTARY(c
, trail
);
4390 if(!(unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
4391 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
4392 cnv
->fromUnicodeStatus
=prevLength
; /* save the old state */
4393 /* callback(unassigned) */
4396 /* convert this supplementary code point */
4397 /* exit this condition tree */
4399 /* this is an unmatched lead code unit (1st surrogate) */
4400 /* callback(illegal) */
4401 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
4409 /* this is an unmatched trail code unit (2nd surrogate) */
4410 /* callback(illegal) */
4411 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
4416 /* convert the Unicode code point in c into codepage bytes */
4419 * The basic lookup is a triple-stage compact array (trie) lookup.
4420 * For details see the beginning of this file.
4422 * Single-byte codepages are handled with a different data structure
4423 * by _MBCSSingle... functions.
4425 * The result consists of a 32-bit value from stage 2 and
4426 * a pointer to as many bytes as are stored per character.
4427 * The pointer points to the character's bytes in stage 3.
4428 * Bits 15..0 of the stage 2 entry contain the stage 3 index
4429 * for that pointer, while bits 31..16 are flags for which of
4430 * the 16 characters in the block are roundtrip-assigned.
4432 * For 2-byte and 4-byte codepages, the bytes are stored as uint16_t
4433 * respectively as uint32_t, in the platform encoding.
4434 * For 3-byte codepages, the bytes are always stored in big-endian order.
4436 * For EUC encodings that use only either 0x8e or 0x8f as the first
4437 * byte of their longest byte sequences, the first two bytes in
4438 * this third stage indicate with their 7th bits whether these bytes
4439 * are to be written directly or actually need to be preceeded by
4440 * one of the two Single-Shift codes. With this, the third stage
4441 * stores one byte fewer per character than the actual maximum length of
4442 * EUC byte sequences.
4444 * Other than that, leading zero bytes are removed and the other
4445 * bytes output. A single zero byte may be output if the "assigned"
4446 * bit in stage 2 was on.
4447 * The data structure does not support zero byte output as a fallback,
4448 * and also does not allow output of leading zeros.
4450 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
4452 /* get the bytes and the length for the output */
4453 switch(outputType
) {
4455 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4462 case MBCS_OUTPUT_2_SISO
:
4463 /* 1/2-byte stateful with Shift-In/Shift-Out */
4465 * Save the old state in the converter object
4466 * right here, then change the local prevLength state variable if necessary.
4467 * Then, if this character turns out to be unassigned or a fallback that
4468 * is not taken, the callback code must not save the new state in the converter
4469 * because the new state is for a character that is not output.
4470 * However, the callback must still restore the state from the converter
4471 * in case the callback function changed it for its output.
4473 cnv
->fromUnicodeStatus
=prevLength
; /* save the old state */
4474 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4476 if(value
==0 && MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
)==0) {
4477 /* no mapping, leave value==0 */
4479 } else if(prevLength
<=1) {
4482 /* change from double-byte mode to single-byte */
4483 if (siLength
== 1) {
4484 value
|=(uint32_t)siBytes
[0]<<8;
4486 } else if (siLength
== 2) {
4487 value
|=(uint32_t)siBytes
[1]<<8;
4488 value
|=(uint32_t)siBytes
[0]<<16;
4497 /* change from single-byte mode to double-byte */
4498 if (soLength
== 1) {
4499 value
|=(uint32_t)soBytes
[0]<<16;
4501 } else if (soLength
== 2) {
4502 value
|=(uint32_t)soBytes
[1]<<16;
4503 value
|=(uint32_t)soBytes
[0]<<24;
4510 case MBCS_OUTPUT_DBCS_ONLY
:
4511 /* table with single-byte results, but only DBCS mappings used */
4512 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4514 /* no mapping or SBCS result, not taken for DBCS-only */
4515 value
=stage2Entry
=0; /* stage2Entry=0 to reset roundtrip flags */
4522 p
=MBCS_POINTER_3_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4523 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4526 } else if(value
<=0xffff) {
4533 value
=MBCS_VALUE_4_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4536 } else if(value
<=0xffff) {
4538 } else if(value
<=0xffffff) {
4544 case MBCS_OUTPUT_3_EUC
:
4545 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4546 /* EUC 16-bit fixed-length representation */
4549 } else if((value
&0x8000)==0) {
4552 } else if((value
&0x80)==0) {
4559 case MBCS_OUTPUT_4_EUC
:
4560 p
=MBCS_POINTER_3_FROM_STAGE_2(bytes
, stage2Entry
, c
);
4561 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4562 /* EUC 16-bit fixed-length representation applied to the first two bytes */
4565 } else if(value
<=0xffff) {
4567 } else if((value
&0x800000)==0) {
4570 } else if((value
&0x8000)==0) {
4578 /* must not occur */
4580 * To avoid compiler warnings that value & length may be
4581 * used without having been initialized, we set them here.
4582 * In reality, this is unreachable code.
4583 * Not having a default branch also causes warnings with
4586 value
=stage2Entry
=0; /* stage2Entry=0 to reset roundtrip flags */
4591 /* is this code point assigned, or do we use fallbacks? */
4592 if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
)!=0 ||
4593 (UCNV_FROM_U_USE_FALLBACK(cnv
, c
) && value
!=0))
4596 * We allow a 0 byte output if the "assigned" bit is set for this entry.
4597 * There is no way with this data structure for fallback output
4598 * to be a zero byte.
4602 /* try an extension mapping */
4603 pArgs
->source
=source
;
4604 c
=_extFromU(cnv
, cnv
->sharedData
,
4605 c
, &source
, sourceLimit
,
4606 &target
, target
+targetCapacity
,
4607 &offsets
, sourceIndex
,
4610 nextSourceIndex
+=(int32_t)(source
-pArgs
->source
);
4611 prevLength
=cnv
->fromUnicodeStatus
; /* restore SISO state */
4613 if(U_FAILURE(*pErrorCode
)) {
4614 /* not mappable or buffer overflow */
4617 /* a mapping was written to the target, continue */
4619 /* recalculate the targetCapacity after an extension mapping */
4620 targetCapacity
=(int32_t)(pArgs
->targetLimit
-(char *)target
);
4622 /* normal end of conversion: prepare for a new character */
4624 prevSourceIndex
=sourceIndex
;
4625 sourceIndex
=nextSourceIndex
;
4632 /* write the output character bytes from value and length */
4633 /* from the first if in the loop we know that targetCapacity>0 */
4634 if(length
<=targetCapacity
) {
4637 /* each branch falls through to the next one */
4639 *target
++=(uint8_t)(value
>>24);
4642 *target
++=(uint8_t)(value
>>16);
4645 *target
++=(uint8_t)(value
>>8);
4648 *target
++=(uint8_t)value
;
4651 /* will never occur */
4656 /* each branch falls through to the next one */
4658 *target
++=(uint8_t)(value
>>24);
4659 *offsets
++=sourceIndex
;
4662 *target
++=(uint8_t)(value
>>16);
4663 *offsets
++=sourceIndex
;
4666 *target
++=(uint8_t)(value
>>8);
4667 *offsets
++=sourceIndex
;
4670 *target
++=(uint8_t)value
;
4671 *offsets
++=sourceIndex
;
4674 /* will never occur */
4678 targetCapacity
-=length
;
4680 uint8_t *charErrorBuffer
;
4683 * We actually do this backwards here:
4684 * In order to save an intermediate variable, we output
4685 * first to the overflow buffer what does not fit into the
4688 /* we know that 1<=targetCapacity<length<=4 */
4689 length
-=targetCapacity
;
4690 charErrorBuffer
=(uint8_t *)cnv
->charErrorBuffer
;
4692 /* each branch falls through to the next one */
4694 *charErrorBuffer
++=(uint8_t)(value
>>16);
4697 *charErrorBuffer
++=(uint8_t)(value
>>8);
4700 *charErrorBuffer
=(uint8_t)value
;
4703 /* will never occur */
4706 cnv
->charErrorBufferLength
=(int8_t)length
;
4708 /* now output what fits into the regular target */
4709 value
>>=8*length
; /* length was reduced by targetCapacity */
4710 switch(targetCapacity
) {
4711 /* each branch falls through to the next one */
4713 *target
++=(uint8_t)(value
>>16);
4715 *offsets
++=sourceIndex
;
4719 *target
++=(uint8_t)(value
>>8);
4721 *offsets
++=sourceIndex
;
4725 *target
++=(uint8_t)value
;
4727 *offsets
++=sourceIndex
;
4731 /* will never occur */
4735 /* target overflow */
4737 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
4742 /* normal end of conversion: prepare for a new character */
4745 prevSourceIndex
=sourceIndex
;
4746 sourceIndex
=nextSourceIndex
;
4750 /* target is full */
4751 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
4757 * the end of the input stream and detection of truncated input
4758 * are handled by the framework, but for EBCDIC_STATEFUL conversion
4759 * we need to emit an SI at the very end
4763 * EBCDIC_STATEFUL in DBCS mode
4764 * end of input and no truncated input
4766 if( U_SUCCESS(*pErrorCode
) &&
4767 outputType
==MBCS_OUTPUT_2_SISO
&& prevLength
==2 &&
4768 pArgs
->flush
&& source
>=sourceLimit
&& c
==0
4770 /* EBCDIC_STATEFUL ending with DBCS: emit an SI to return the output stream to SBCS */
4771 if(targetCapacity
>0) {
4772 *target
++=(uint8_t)siBytes
[0];
4773 if (siLength
== 2) {
4774 if (targetCapacity
<2) {
4775 cnv
->charErrorBuffer
[0]=(uint8_t)siBytes
[1];
4776 cnv
->charErrorBufferLength
=1;
4777 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
4779 *target
++=(uint8_t)siBytes
[1];
4783 /* set the last source character's index (sourceIndex points at sourceLimit now) */
4784 *offsets
++=prevSourceIndex
;
4787 /* target is full */
4788 cnv
->charErrorBuffer
[0]=(uint8_t)siBytes
[0];
4789 if (siLength
== 2) {
4790 cnv
->charErrorBuffer
[1]=(uint8_t)siBytes
[1];
4792 cnv
->charErrorBufferLength
=siLength
;
4793 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
4795 prevLength
=1; /* we switched into SBCS */
4798 /* set the converter state back into UConverter */
4800 cnv
->fromUnicodeStatus
=prevLength
;
4802 /* write back the updated pointers */
4803 pArgs
->source
=source
;
4804 pArgs
->target
=(char *)target
;
4805 pArgs
->offsets
=offsets
;
4809 * This is another simple conversion function for internal use by other
4810 * conversion implementations.
4811 * It does not use the converter state nor call callbacks.
4812 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
4813 * It handles conversion extensions but not GB 18030.
4815 * It converts one single Unicode code point into codepage bytes, encoded
4816 * as one 32-bit value. The function returns the number of bytes in *pValue:
4817 * 1..4 the number of bytes in *pValue
4818 * 0 unassigned (*pValue undefined)
4819 * -1 illegal (currently not used, *pValue undefined)
4821 * *pValue will contain the resulting bytes with the last byte in bits 7..0,
4822 * the second to last byte in bits 15..8, etc.
4823 * Currently, the function assumes but does not check that 0<=c<=0x10ffff.
4826 ucnv_MBCSFromUChar32(UConverterSharedData
*sharedData
,
4827 UChar32 c
, uint32_t *pValue
,
4828 UBool useFallback
) {
4830 const uint16_t *table
;
4832 /* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */
4835 uint32_t stage2Entry
;
4839 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
4840 if(c
<=0xffff || (sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
4841 table
=sharedData
->mbcs
.fromUnicodeTable
;
4843 /* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
4844 if(sharedData
->mbcs
.outputType
==MBCS_OUTPUT_1
) {
4845 value
=MBCS_SINGLE_RESULT_FROM_U(table
, (uint16_t *)sharedData
->mbcs
.fromUnicodeBytes
, c
);
4846 /* is this code point assigned, or do we use fallbacks? */
4847 if(useFallback
? value
>=0x800 : value
>=0xc00) {
4851 } else /* outputType!=MBCS_OUTPUT_1 */ {
4852 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
4854 /* get the bytes and the length for the output */
4855 switch(sharedData
->mbcs
.outputType
) {
4857 value
=MBCS_VALUE_2_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4865 /* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */
4866 case MBCS_OUTPUT_DBCS_ONLY
:
4867 /* table with single-byte results, but only DBCS mappings used */
4868 value
=MBCS_VALUE_2_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4870 /* no mapping or SBCS result, not taken for DBCS-only */
4871 value
=stage2Entry
=0; /* stage2Entry=0 to reset roundtrip flags */
4878 p
=MBCS_POINTER_3_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4879 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4882 } else if(value
<=0xffff) {
4889 value
=MBCS_VALUE_4_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4892 } else if(value
<=0xffff) {
4894 } else if(value
<=0xffffff) {
4900 case MBCS_OUTPUT_3_EUC
:
4901 value
=MBCS_VALUE_2_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4902 /* EUC 16-bit fixed-length representation */
4905 } else if((value
&0x8000)==0) {
4908 } else if((value
&0x80)==0) {
4915 case MBCS_OUTPUT_4_EUC
:
4916 p
=MBCS_POINTER_3_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
4917 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
4918 /* EUC 16-bit fixed-length representation applied to the first two bytes */
4921 } else if(value
<=0xffff) {
4923 } else if((value
&0x800000)==0) {
4926 } else if((value
&0x8000)==0) {
4935 /* must not occur */
4939 /* is this code point assigned, or do we use fallbacks? */
4940 if( MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
) ||
4941 (FROM_U_USE_FALLBACK(useFallback
, c
) && value
!=0)
4944 * We allow a 0 byte output if the "assigned" bit is set for this entry.
4945 * There is no way with this data structure for fallback output
4946 * to be a zero byte.
4955 cx
=sharedData
->mbcs
.extIndexes
;
4957 length
=ucnv_extSimpleMatchFromU(cx
, c
, pValue
, useFallback
);
4958 return length
>=0 ? length
: -length
; /* return abs(length); */
4968 * This function has been moved to ucnv2022.c for inlining.
4969 * This implementation is here only for documentation purposes
4973 * This version of ucnv_MBCSFromUChar32() is optimized for single-byte codepages.
4974 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
4975 * It does not handle conversion extensions (_extFromU()).
4977 * It returns the codepage byte for the code point, or -1 if it is unassigned.
4980 ucnv_MBCSSingleFromUChar32(UConverterSharedData
*sharedData
,
4982 UBool useFallback
) {
4983 const uint16_t *table
;
4986 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
4987 if(c
>=0x10000 && !(sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
4991 /* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
4992 table
=sharedData
->mbcs
.fromUnicodeTable
;
4994 /* get the byte for the output */
4995 value
=MBCS_SINGLE_RESULT_FROM_U(table
, (uint16_t *)sharedData
->mbcs
.fromUnicodeBytes
, c
);
4996 /* is this code point assigned, or do we use fallbacks? */
4997 if(useFallback
? value
>=0x800 : value
>=0xc00) {
5005 /* MBCS-from-UTF-8 conversion functions ------------------------------------- */
5007 /* minimum code point values for n-byte UTF-8 sequences, n=0..4 */
5008 static const UChar32
5009 utf8_minLegal
[5]={ 0, 0, 0x80, 0x800, 0x10000 };
5011 /* offsets for n-byte UTF-8 sequences that were calculated with ((lead<<6)+trail)<<6+trail... */
5012 static const UChar32
5013 utf8_offsets
[7]={ 0, 0, 0x3080, 0xE2080, 0x3C82080 };
5016 ucnv_SBCSFromUTF8(UConverterFromUnicodeArgs
*pFromUArgs
,
5017 UConverterToUnicodeArgs
*pToUArgs
,
5018 UErrorCode
*pErrorCode
) {
5019 UConverter
*utf8
, *cnv
;
5020 const uint8_t *source
, *sourceLimit
;
5022 int32_t targetCapacity
;
5024 const uint16_t *table
, *sbcsIndex
;
5025 const uint16_t *results
;
5027 int8_t oldToULength
, toULength
, toULimit
;
5032 uint32_t asciiRoundtrips
;
5033 uint16_t value
, minValue
;
5034 UBool hasSupplementary
;
5036 /* set up the local pointers */
5037 utf8
=pToUArgs
->converter
;
5038 cnv
=pFromUArgs
->converter
;
5039 source
=(uint8_t *)pToUArgs
->source
;
5040 sourceLimit
=(uint8_t *)pToUArgs
->sourceLimit
;
5041 target
=(uint8_t *)pFromUArgs
->target
;
5042 targetCapacity
=(int32_t)(pFromUArgs
->targetLimit
-pFromUArgs
->target
);
5044 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
5045 sbcsIndex
=cnv
->sharedData
->mbcs
.sbcsIndex
;
5046 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
5047 results
=(uint16_t *)cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
5049 results
=(uint16_t *)cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
5051 asciiRoundtrips
=cnv
->sharedData
->mbcs
.asciiRoundtrips
;
5053 if(cnv
->useFallback
) {
5054 /* use all roundtrip and fallback results */
5057 /* use only roundtrips and fallbacks from private-use characters */
5060 hasSupplementary
=(UBool
)(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
);
5062 /* get the converter state from the UTF-8 UConverter */
5063 c
=(UChar32
)utf8
->toUnicodeStatus
;
5065 toULength
=oldToULength
=utf8
->toULength
;
5066 toULimit
=(int8_t)utf8
->mode
;
5068 toULength
=oldToULength
=toULimit
=0;
5072 * Make sure that the last byte sequence before sourceLimit is complete
5073 * or runs into a lead byte.
5074 * Do not go back into the bytes that will be read for finishing a partial
5075 * sequence from the previous buffer.
5076 * In the conversion loop compare source with sourceLimit only once
5077 * per multi-byte character.
5082 length
=(int32_t)(sourceLimit
-source
) - (toULimit
-oldToULength
);
5083 for(i
=0; i
<3 && i
<length
;) {
5084 b
=*(sourceLimit
-i
-1);
5085 if(U8_IS_TRAIL(b
)) {
5088 if(i
<U8_COUNT_TRAIL_BYTES(b
)) {
5089 /* exit the conversion loop before the lead byte if there are not enough trail bytes for it */
5097 if(c
!=0 && targetCapacity
>0) {
5098 utf8
->toUnicodeStatus
=0;
5102 * Note: We could avoid the goto by duplicating some of the moreBytes
5103 * code, but only up to the point of collecting a complete UTF-8
5104 * sequence; then recurse for the toUBytes[toULength]
5105 * and then continue with normal conversion.
5107 * If so, move this code to just after initializing the minimum
5108 * set of local variables for reading the UTF-8 input
5109 * (utf8, source, target, limits but not cnv, table, minValue, etc.).
5111 * Potential advantages:
5113 * - oldToULength could become a local variable in just those code blocks
5114 * that deal with buffer boundaries
5115 * - possibly faster if the goto prevents some compiler optimizations
5116 * (this would need measuring to confirm)
5118 * - code duplication
5122 /* conversion loop */
5123 while(source
<sourceLimit
) {
5124 if(targetCapacity
>0) {
5128 if(IS_ASCII_ROUNDTRIP(b
, asciiRoundtrips
)) {
5129 *target
++=(uint8_t)b
;
5134 value
=SBCS_RESULT_FROM_UTF8(sbcsIndex
, results
, 0, c
);
5138 if( /* handle U+0080..U+07FF inline */
5140 (t1
=(uint8_t)(*source
-0x80)) <= 0x3f
5144 value
=SBCS_RESULT_FROM_UTF8(sbcsIndex
, results
, c
, t1
);
5145 if(value
>=minValue
) {
5146 *target
++=(uint8_t)value
;
5155 } else if(b
==0xe0) {
5156 if( /* handle U+0800..U+0FFF inline */
5157 (t1
=(uint8_t)(source
[0]-0x80)) <= 0x3f && t1
>= 0x20 &&
5158 (t2
=(uint8_t)(source
[1]-0x80)) <= 0x3f
5162 value
=SBCS_RESULT_FROM_UTF8(sbcsIndex
, results
, c
, t2
);
5163 if(value
>=minValue
) {
5164 *target
++=(uint8_t)value
;
5178 /* handle "complicated" and error cases, and continuing partial characters */
5181 toULimit
=U8_COUNT_TRAIL_BYTES(b
)+1;
5184 while(toULength
<toULimit
) {
5186 * The sourceLimit may have been adjusted before the conversion loop
5187 * to stop before a truncated sequence.
5188 * Here we need to use the real limit in case we have two truncated
5189 * sequences at the end.
5192 if(source
<(uint8_t *)pToUArgs
->sourceLimit
) {
5194 if(U8_IS_TRAIL(b
)) {
5199 break; /* sequence too short, stop with toULength<toULimit */
5202 /* store the partial UTF-8 character, compatible with the regular UTF-8 converter */
5203 source
-=(toULength
-oldToULength
);
5204 while(oldToULength
<toULength
) {
5205 utf8
->toUBytes
[oldToULength
++]=*source
++;
5207 utf8
->toUnicodeStatus
=c
;
5208 utf8
->toULength
=toULength
;
5209 utf8
->mode
=toULimit
;
5210 pToUArgs
->source
=(char *)source
;
5211 pFromUArgs
->target
=(char *)target
;
5216 if( toULength
==toULimit
&& /* consumed all trail bytes */
5217 (toULength
==3 || toULength
==2) && /* BMP */
5218 (c
-=utf8_offsets
[toULength
])>=utf8_minLegal
[toULength
] &&
5219 (c
<=0xd7ff || 0xe000<=c
) /* not a surrogate */
5221 value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
5223 toULength
==toULimit
&& toULength
==4 &&
5224 (0x10000<=(c
-=utf8_offsets
[4]) && c
<=0x10ffff)
5226 /* supplementary code point */
5227 if(!hasSupplementary
) {
5228 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
5231 value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
5234 /* error handling: illegal UTF-8 byte sequence */
5235 source
-=(toULength
-oldToULength
);
5236 while(oldToULength
<toULength
) {
5237 utf8
->toUBytes
[oldToULength
++]=*source
++;
5239 utf8
->toULength
=toULength
;
5240 pToUArgs
->source
=(char *)source
;
5241 pFromUArgs
->target
=(char *)target
;
5242 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
5248 if(value
>=minValue
) {
5249 /* output the mapping for c */
5250 *target
++=(uint8_t)value
;
5253 /* value<minValue means c is unassigned (unmappable) */
5255 * Try an extension mapping.
5256 * Pass in no source because we don't have UTF-16 input.
5257 * If we have a partial match on c, we will return and revert
5258 * to UTF-8->UTF-16->charset conversion.
5260 static const UChar nul
=0;
5261 const UChar
*noSource
=&nul
;
5262 c
=_extFromU(cnv
, cnv
->sharedData
,
5263 c
, &noSource
, noSource
,
5264 &target
, target
+targetCapacity
,
5269 if(U_FAILURE(*pErrorCode
)) {
5270 /* not mappable or buffer overflow */
5273 } else if(cnv
->preFromUFirstCP
>=0) {
5275 * Partial match, return and revert to pivoting.
5276 * In normal from-UTF-16 conversion, we would just continue
5277 * but then exit the loop because the extension match would
5278 * have consumed the source.
5280 *pErrorCode
=U_USING_DEFAULT_WARNING
;
5283 /* a mapping was written to the target, continue */
5285 /* recalculate the targetCapacity after an extension mapping */
5286 targetCapacity
=(int32_t)(pFromUArgs
->targetLimit
-(char *)target
);
5290 /* target is full */
5291 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
5297 * The sourceLimit may have been adjusted before the conversion loop
5298 * to stop before a truncated sequence.
5299 * If so, then collect the truncated sequence now.
5301 if(U_SUCCESS(*pErrorCode
) &&
5302 cnv
->preFromUFirstCP
<0 &&
5303 source
<(sourceLimit
=(uint8_t *)pToUArgs
->sourceLimit
)) {
5304 c
=utf8
->toUBytes
[0]=b
=*source
++;
5306 toULimit
=U8_COUNT_TRAIL_BYTES(b
)+1;
5307 while(source
<sourceLimit
) {
5308 utf8
->toUBytes
[toULength
++]=b
=*source
++;
5311 utf8
->toUnicodeStatus
=c
;
5312 utf8
->toULength
=toULength
;
5313 utf8
->mode
=toULimit
;
5316 /* write back the updated pointers */
5317 pToUArgs
->source
=(char *)source
;
5318 pFromUArgs
->target
=(char *)target
;
5322 ucnv_DBCSFromUTF8(UConverterFromUnicodeArgs
*pFromUArgs
,
5323 UConverterToUnicodeArgs
*pToUArgs
,
5324 UErrorCode
*pErrorCode
) {
5325 UConverter
*utf8
, *cnv
;
5326 const uint8_t *source
, *sourceLimit
;
5328 int32_t targetCapacity
;
5330 const uint16_t *table
, *mbcsIndex
;
5331 const uint16_t *results
;
5333 int8_t oldToULength
, toULength
, toULimit
;
5338 uint32_t stage2Entry
;
5339 uint32_t asciiRoundtrips
;
5341 UBool hasSupplementary
;
5343 /* set up the local pointers */
5344 utf8
=pToUArgs
->converter
;
5345 cnv
=pFromUArgs
->converter
;
5346 source
=(uint8_t *)pToUArgs
->source
;
5347 sourceLimit
=(uint8_t *)pToUArgs
->sourceLimit
;
5348 target
=(uint8_t *)pFromUArgs
->target
;
5349 targetCapacity
=(int32_t)(pFromUArgs
->targetLimit
-pFromUArgs
->target
);
5351 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
5352 mbcsIndex
=cnv
->sharedData
->mbcs
.mbcsIndex
;
5353 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
5354 results
=(uint16_t *)cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
5356 results
=(uint16_t *)cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
5358 asciiRoundtrips
=cnv
->sharedData
->mbcs
.asciiRoundtrips
;
5360 hasSupplementary
=(UBool
)(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
);
5362 /* get the converter state from the UTF-8 UConverter */
5363 c
=(UChar32
)utf8
->toUnicodeStatus
;
5365 toULength
=oldToULength
=utf8
->toULength
;
5366 toULimit
=(int8_t)utf8
->mode
;
5368 toULength
=oldToULength
=toULimit
=0;
5372 * Make sure that the last byte sequence before sourceLimit is complete
5373 * or runs into a lead byte.
5374 * Do not go back into the bytes that will be read for finishing a partial
5375 * sequence from the previous buffer.
5376 * In the conversion loop compare source with sourceLimit only once
5377 * per multi-byte character.
5382 length
=(int32_t)(sourceLimit
-source
) - (toULimit
-oldToULength
);
5383 for(i
=0; i
<3 && i
<length
;) {
5384 b
=*(sourceLimit
-i
-1);
5385 if(U8_IS_TRAIL(b
)) {
5388 if(i
<U8_COUNT_TRAIL_BYTES(b
)) {
5389 /* exit the conversion loop before the lead byte if there are not enough trail bytes for it */
5397 if(c
!=0 && targetCapacity
>0) {
5398 utf8
->toUnicodeStatus
=0;
5401 /* See note in ucnv_SBCSFromUTF8() about this goto. */
5404 /* conversion loop */
5405 while(source
<sourceLimit
) {
5406 if(targetCapacity
>0) {
5410 if(IS_ASCII_ROUNDTRIP(b
, asciiRoundtrips
)) {
5415 value
=DBCS_RESULT_FROM_UTF8(mbcsIndex
, results
, 0, b
);
5423 if( /* handle U+1000..U+D7FF inline */
5424 (((t1
=(uint8_t)(source
[0]-0x80), b
<0xed) && (t1
<= 0x3f)) ||
5425 (b
==0xed && (t1
<= 0x1f))) &&
5426 (t2
=(uint8_t)(source
[1]-0x80)) <= 0x3f
5430 value
=DBCS_RESULT_FROM_UTF8(mbcsIndex
, results
, c
, t2
);
5439 if( /* handle U+0080..U+07FF inline */
5441 (t1
=(uint8_t)(*source
-0x80)) <= 0x3f
5445 value
=DBCS_RESULT_FROM_UTF8(mbcsIndex
, results
, c
, t1
);
5458 /* handle "complicated" and error cases, and continuing partial characters */
5461 toULimit
=U8_COUNT_TRAIL_BYTES(b
)+1;
5464 while(toULength
<toULimit
) {
5466 * The sourceLimit may have been adjusted before the conversion loop
5467 * to stop before a truncated sequence.
5468 * Here we need to use the real limit in case we have two truncated
5469 * sequences at the end.
5472 if(source
<(uint8_t *)pToUArgs
->sourceLimit
) {
5474 if(U8_IS_TRAIL(b
)) {
5479 break; /* sequence too short, stop with toULength<toULimit */
5482 /* store the partial UTF-8 character, compatible with the regular UTF-8 converter */
5483 source
-=(toULength
-oldToULength
);
5484 while(oldToULength
<toULength
) {
5485 utf8
->toUBytes
[oldToULength
++]=*source
++;
5487 utf8
->toUnicodeStatus
=c
;
5488 utf8
->toULength
=toULength
;
5489 utf8
->mode
=toULimit
;
5490 pToUArgs
->source
=(char *)source
;
5491 pFromUArgs
->target
=(char *)target
;
5496 if( toULength
==toULimit
&& /* consumed all trail bytes */
5497 (toULength
==3 || toULength
==2) && /* BMP */
5498 (c
-=utf8_offsets
[toULength
])>=utf8_minLegal
[toULength
] &&
5499 (c
<=0xd7ff || 0xe000<=c
) /* not a surrogate */
5501 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
5503 toULength
==toULimit
&& toULength
==4 &&
5504 (0x10000<=(c
-=utf8_offsets
[4]) && c
<=0x10ffff)
5506 /* supplementary code point */
5507 if(!hasSupplementary
) {
5508 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
5511 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
5514 /* error handling: illegal UTF-8 byte sequence */
5515 source
-=(toULength
-oldToULength
);
5516 while(oldToULength
<toULength
) {
5517 utf8
->toUBytes
[oldToULength
++]=*source
++;
5519 utf8
->toULength
=toULength
;
5520 pToUArgs
->source
=(char *)source
;
5521 pFromUArgs
->target
=(char *)target
;
5522 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
5526 /* get the bytes and the length for the output */
5528 value
=MBCS_VALUE_2_FROM_STAGE_2(results
, stage2Entry
, c
);
5530 /* is this code point assigned, or do we use fallbacks? */
5531 if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
) ||
5532 (UCNV_FROM_U_USE_FALLBACK(cnv
, c
) && value
!=0))
5539 /* write the output character bytes from value and length */
5540 /* from the first if in the loop we know that targetCapacity>0 */
5542 /* this is easy because we know that there is enough space */
5543 *target
++=(uint8_t)value
;
5545 } else /* length==2 */ {
5546 *target
++=(uint8_t)(value
>>8);
5547 if(2<=targetCapacity
) {
5548 *target
++=(uint8_t)value
;
5551 cnv
->charErrorBuffer
[0]=(char)value
;
5552 cnv
->charErrorBufferLength
=1;
5554 /* target overflow */
5555 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
5564 * Try an extension mapping.
5565 * Pass in no source because we don't have UTF-16 input.
5566 * If we have a partial match on c, we will return and revert
5567 * to UTF-8->UTF-16->charset conversion.
5569 static const UChar nul
=0;
5570 const UChar
*noSource
=&nul
;
5571 c
=_extFromU(cnv
, cnv
->sharedData
,
5572 c
, &noSource
, noSource
,
5573 &target
, target
+targetCapacity
,
5578 if(U_FAILURE(*pErrorCode
)) {
5579 /* not mappable or buffer overflow */
5582 } else if(cnv
->preFromUFirstCP
>=0) {
5584 * Partial match, return and revert to pivoting.
5585 * In normal from-UTF-16 conversion, we would just continue
5586 * but then exit the loop because the extension match would
5587 * have consumed the source.
5589 *pErrorCode
=U_USING_DEFAULT_WARNING
;
5592 /* a mapping was written to the target, continue */
5594 /* recalculate the targetCapacity after an extension mapping */
5595 targetCapacity
=(int32_t)(pFromUArgs
->targetLimit
-(char *)target
);
5600 /* target is full */
5601 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
5607 * The sourceLimit may have been adjusted before the conversion loop
5608 * to stop before a truncated sequence.
5609 * If so, then collect the truncated sequence now.
5611 if(U_SUCCESS(*pErrorCode
) &&
5612 cnv
->preFromUFirstCP
<0 &&
5613 source
<(sourceLimit
=(uint8_t *)pToUArgs
->sourceLimit
)) {
5614 c
=utf8
->toUBytes
[0]=b
=*source
++;
5616 toULimit
=U8_COUNT_TRAIL_BYTES(b
)+1;
5617 while(source
<sourceLimit
) {
5618 utf8
->toUBytes
[toULength
++]=b
=*source
++;
5621 utf8
->toUnicodeStatus
=c
;
5622 utf8
->toULength
=toULength
;
5623 utf8
->mode
=toULimit
;
5626 /* write back the updated pointers */
5627 pToUArgs
->source
=(char *)source
;
5628 pFromUArgs
->target
=(char *)target
;
5631 /* miscellaneous ------------------------------------------------------------ */
5634 ucnv_MBCSGetStarters(const UConverter
* cnv
,
5635 UBool starters
[256],
5637 const int32_t *state0
;
5640 state0
=cnv
->sharedData
->mbcs
.stateTable
[cnv
->sharedData
->mbcs
.dbcsOnlyState
];
5641 for(i
=0; i
<256; ++i
) {
5642 /* all bytes that cause a state transition from state 0 are lead bytes */
5643 starters
[i
]= (UBool
)MBCS_ENTRY_IS_TRANSITION(state0
[i
]);
5648 * This is an internal function that allows other converter implementations
5649 * to check whether a byte is a lead byte.
5652 ucnv_MBCSIsLeadByte(UConverterSharedData
*sharedData
, char byte
) {
5653 return (UBool
)MBCS_ENTRY_IS_TRANSITION(sharedData
->mbcs
.stateTable
[0][(uint8_t)byte
]);
5657 ucnv_MBCSWriteSub(UConverterFromUnicodeArgs
*pArgs
,
5658 int32_t offsetIndex
,
5659 UErrorCode
*pErrorCode
) {
5660 UConverter
*cnv
=pArgs
->converter
;
5665 /* first, select between subChar and subChar1 */
5666 if( cnv
->subChar1
!=0 &&
5667 (cnv
->sharedData
->mbcs
.extIndexes
!=NULL
?
5669 (cnv
->invalidUCharBuffer
[0]<=0xff))
5671 /* select subChar1 if it is set (not 0) and the unmappable Unicode code point is up to U+00ff (IBM MBCS behavior) */
5672 subchar
=(char *)&cnv
->subChar1
;
5675 /* select subChar in all other cases */
5676 subchar
=(char *)cnv
->subChars
;
5677 length
=cnv
->subCharLen
;
5680 /* reset the selector for the next code point */
5681 cnv
->useSubChar1
=FALSE
;
5683 if (cnv
->sharedData
->mbcs
.outputType
== MBCS_OUTPUT_2_SISO
) {
5686 /* fromUnicodeStatus contains prevLength */
5689 if(cnv
->fromUnicodeStatus
==2) {
5690 /* DBCS mode and SBCS sub char: change to SBCS */
5691 cnv
->fromUnicodeStatus
=1;
5697 if(cnv
->fromUnicodeStatus
<=1) {
5698 /* SBCS mode and DBCS sub char: change to DBCS */
5699 cnv
->fromUnicodeStatus
=2;
5706 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
5710 length
=(int32_t)(p
-buffer
);
5713 ucnv_cbFromUWriteBytes(pArgs
, subchar
, length
, offsetIndex
, pErrorCode
);
5716 U_CFUNC UConverterType
5717 ucnv_MBCSGetType(const UConverter
* converter
) {
5718 /* SBCS, DBCS, and EBCDIC_STATEFUL are replaced by MBCS, but here we cheat a little */
5719 if(converter
->sharedData
->mbcs
.countStates
==1) {
5720 return (UConverterType
)UCNV_SBCS
;
5721 } else if((converter
->sharedData
->mbcs
.outputType
&0xff)==MBCS_OUTPUT_2_SISO
) {
5722 return (UConverterType
)UCNV_EBCDIC_STATEFUL
;
5723 } else if(converter
->sharedData
->staticData
->minBytesPerChar
==2 && converter
->sharedData
->staticData
->maxBytesPerChar
==2) {
5724 return (UConverterType
)UCNV_DBCS
;
5726 return (UConverterType
)UCNV_MBCS
;
5729 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */