2 ******************************************************************************
4 * Copyright (C) 2000-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
8 * file name: ucnvmbcs.c
10 * tab size: 8 (not used)
13 * created on: 2000jul03
14 * created by: Markus W. Scherer
16 * The current code in this file replaces the previous implementation
17 * of conversion code from multi-byte codepages to Unicode and back.
18 * This implementation supports the following:
19 * - legacy variable-length codepages with up to 4 bytes per character
20 * - all Unicode code points (up to 0x10ffff)
21 * - efficient distinction of unassigned vs. illegal byte sequences
22 * - it is possible in fromUnicode() to directly deal with simple
23 * stateful encodings (used for EBCDIC_STATEFUL)
24 * - it is possible to convert Unicode code points
25 * to a single zero byte (but not as a fallback except for SBCS)
27 * Remaining limitations in fromUnicode:
28 * - byte sequences must not have leading zero bytes
29 * - except for SBCS codepages: no fallback mapping from Unicode to a zero byte
30 * - limitation to up to 4 bytes per character
32 * ICU 2.8 (late 2003) adds a secondary data structure which lifts some of these
33 * limitations and adds m:n character mappings and other features.
34 * See ucnv_ext.h for details.
38 * 5/6/2001 Ram Moved MBCS_SINGLE_RESULT_FROM_U,MBCS_STAGE_2_FROM_U,
39 * MBCS_VALUE_2_FROM_STAGE_2, MBCS_VALUE_4_FROM_STAGE_2
40 * macros to ucnvmbcs.h file
43 #include "unicode/utypes.h"
45 #if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION
47 #include "unicode/ucnv.h"
48 #include "unicode/ucnv_cb.h"
49 #include "unicode/udata.h"
50 #include "unicode/uset.h"
59 /* control optimizations according to the platform */
60 #define MBCS_UNROLL_SINGLE_TO_BMP 1
61 #define MBCS_UNROLL_SINGLE_FROM_BMP 0
64 * _MBCSHeader versions 4.2
65 * (Note that the _MBCSHeader version is in addition to the converter formatVersion.)
67 * Change from version 4.1:
68 * - Added an optional extension table structure at the end of the .cnv file.
69 * It is present if the upper bits of the header flags field contains a non-zero
71 * Files that contain only a conversion table and no base table
72 * use the special outputType MBCS_OUTPUT_EXT_ONLY.
73 * These contain the base table name between the MBCS header and the extension
76 * Change from version 4.0:
77 * - Replace header.reserved with header.fromUBytesLength so that all
78 * fields in the data have length.
80 * Changes from version 3 (for performance improvements):
81 * - new bit distribution for state table entries
82 * - reordered action codes
83 * - new data structure for single-byte fromUnicode
84 * + stage 2 only contains indexes
85 * + stage 3 stores 16 bits per character with classification bits 15..8
86 * - no multiplier for stage 1 entries
87 * - stage 2 for non-single-byte codepages contains the index and the flags in
89 * - 2-byte and 4-byte fromUnicode results are stored directly as 16/32-bit integers
91 * For more details about old versions of the MBCS data structure, see
92 * the corresponding versions of this file.
94 * Converting stateless codepage data ---------------------------------------***
95 * (or codepage data with simple states) to Unicode.
97 * Data structure and algorithm for converting from complex legacy codepages
98 * to Unicode. (Designed before 2000-may-22.)
100 * The basic idea is that the structure of legacy codepages can be described
102 * When reading a byte stream, each input byte causes a state transition.
103 * Some transitions result in the output of a code point, some result in
104 * "unassigned" or "illegal" output.
105 * This is used here for character conversion.
107 * The data structure begins with a state table consisting of a row
108 * per state, with 256 entries (columns) per row for each possible input
110 * Each entry is 32 bits wide, with two formats distinguished by
111 * the sign bit (bit 31):
113 * One format for transitional entries (bit 31 not set) for non-final bytes, and
114 * one format for final entries (bit 31 set).
115 * Both formats contain the number of the next state in the same bit
117 * State 0 is the initial state.
119 * Most of the time, the offset values of subsequent states are added
120 * up to a scalar value. This value will eventually be the index of
121 * the Unicode code point in a table that follows the state table.
122 * The effect is that the code points for final state table rows
123 * are contiguous. The code points of final state rows follow each other
124 * in the order of the references to those final states by previous
127 * For some terminal states, the offset is itself the output Unicode
128 * code point (16 bits for a BMP code point or 20 bits for a supplementary
129 * code point (stored as code point minus 0x10000 so that 20 bits are enough).
130 * For others, the code point in the Unicode table is stored with either
131 * one or two code units: one for BMP code points, two for a pair of
133 * All code points for a final state entry take up the same number of code
134 * units, regardless of whether they all actually _use_ the same number
135 * of code units. This is necessary for simple array access.
137 * An additional feature comes in with what in ICU is called "fallback"
140 * In addition to round-trippable, precise, 1:1 mappings, there are often
141 * mappings defined between similar, though not the same, characters.
142 * Typically, such mappings occur only in fromUnicode mapping tables because
143 * Unicode has a superset repertoire of most other codepages. However, it
144 * is possible to provide such mappings in the toUnicode tables, too.
145 * In this case, the fallback mappings are partly integrated into the
146 * general state tables because the structure of the encoding includes their
148 * For final entries in an initial state, fallback mappings are stored in
149 * the entry itself like with roundtrip mappings.
150 * For other final entries, they are stored in the code units table if
151 * the entry is for a pair of code units.
152 * For single-unit results in the code units table, there is no space to
153 * alternatively hold a fallback mapping; in this case, the code unit
154 * is stored as U+fffe (unassigned), and the fallback mapping needs to
155 * be looked up by the scalar offset value in a separate table.
157 * "Unassigned" state entries really mean "structurally unassigned",
158 * i.e., such a byte sequence will never have a mapping result.
160 * The interpretation of the bits in each entry is as follows:
162 * Bit 31 not set, not a terminal entry ("transitional"):
164 * 23..0 offset delta, to be added up
166 * Bit 31 set, terminal ("final") entry:
167 * 30..24 next state (regardless of action code)
168 * 23..20 action code:
169 * action codes 0 and 1 result in precise-mapping Unicode code points
170 * 0 valid byte sequence
172 * 15..0 16-bit Unicode BMP code point
173 * never U+fffe or U+ffff
174 * 1 valid byte sequence
175 * 19..0 20-bit Unicode supplementary code point
176 * never U+fffe or U+ffff
178 * action codes 2 and 3 result in fallback (unidirectional-mapping) Unicode code points
179 * 2 valid byte sequence (fallback)
181 * 15..0 16-bit Unicode BMP code point as fallback result
182 * 3 valid byte sequence (fallback)
183 * 19..0 20-bit Unicode supplementary code point as fallback result
185 * action codes 4 and 5 may result in roundtrip/fallback/unassigned/illegal results
186 * depending on the code units they result in
187 * 4 valid byte sequence
189 * 8..0 final offset delta
190 * pointing to one 16-bit code unit which may be
191 * fffe unassigned -- look for a fallback for this offset
193 * 5 valid byte sequence
195 * 8..0 final offset delta
196 * pointing to two 16-bit code units
197 * (typically UTF-16 surrogates)
198 * the result depends on the first code unit as follows:
199 * 0000..d7ff roundtrip BMP code point (1st alone)
200 * d800..dbff roundtrip surrogate pair (1st, 2nd)
201 * dc00..dfff fallback surrogate pair (1st-400, 2nd)
202 * e000 roundtrip BMP code point (2nd alone)
203 * e001 fallback BMP code point (2nd alone)
206 * (the final offset deltas are at most 255 * 2,
207 * times 2 because of storing code unit pairs)
209 * 6 unassigned byte sequence
211 * 15..0 16-bit Unicode BMP code point U+fffe (new with version 2)
212 * this does not contain a final offset delta because the main
213 * purpose of this action code is to save scalar offset values;
214 * therefore, fallback values cannot be assigned to byte
215 * sequences that result in this action code
216 * 7 illegal byte sequence
218 * 15..0 16-bit Unicode BMP code point U+ffff (new with version 2)
219 * 8 state change only
221 * useful for state changes in simple stateful encodings,
222 * at Shift-In/Shift-Out codes
225 * 9..15 reserved for future use
226 * current implementations will only perform a state change
227 * and ignore bits 19..0
229 * An encoding with contiguous ranges of unassigned byte sequences, like
230 * Shift-JIS and especially EUC-TW, can be stored efficiently by having
231 * at least two states for the trail bytes:
232 * One trail byte state that results in code points, and one that only
233 * has "unassigned" and "illegal" terminal states.
235 * Note: partly by accident, this data structure supports simple stateless
236 * encodings without any additional logic.
237 * Currently, only simple Shift-In/Shift-Out schemes are handled with
238 * appropriate state tables (especially EBCDIC_STATEFUL!).
240 * MBCS version 2 added:
241 * unassigned and illegal action codes have U+fffe and U+ffff
242 * instead of unused bits; this is useful for _MBCS_SINGLE_SIMPLE_GET_NEXT_BMP()
244 * Converting from Unicode to codepage bytes --------------------------------***
246 * The conversion data structure for fromUnicode is designed for the known
247 * structure of Unicode. It maps from 21-bit code points (0..0x10ffff) to
248 * a sequence of 1..4 bytes, in addition to a flag that indicates if there is
249 * a roundtrip mapping.
251 * The lookup is done with a 3-stage trie, using 11/6/4 bits for stage 1/2/3
252 * like in the character properties table.
253 * The beginning of the trie is at offsetFromUTable, the beginning of stage 3
254 * with the resulting bytes is at offsetFromUBytes.
256 * Beginning with version 4, single-byte codepages have a significantly different
257 * trie compared to other codepages.
258 * In all cases, the entry in stage 1 is directly the index of the block of
259 * 64 entries in stage 2.
261 * Single-byte lookup:
263 * Stage 2 only contains 16-bit indexes directly to the 16-blocks in stage 3.
264 * Stage 3 contains one 16-bit word per result:
265 * Bits 15..8 indicate the kind of result:
267 * c fallback result from private-use code point
268 * 8 fallback result from other code points
270 * Bits 7..0 contain the codepage byte. A zero byte is always possible.
274 * Stage 2 contains a 32-bit word for each 16-block in stage 3:
275 * Bits 31..16 contain flags for which stage 3 entries contain roundtrip results
276 * test: MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c)
277 * If this test is false, then a non-zero result will be interpreted as
278 * a fallback mapping.
279 * Bits 15..0 contain the index to stage 3, which must be multiplied by 16*(bytes per char)
281 * Stage 3 contains 2, 3, or 4 bytes per result.
282 * 2 or 4 bytes are stored as uint16_t/uint32_t in platform endianness,
283 * while 3 bytes are stored as bytes in big-endian order.
284 * Leading zero bytes are ignored, and the number of bytes is counted.
285 * A zero byte mapping result is possible as a roundtrip result.
286 * For some output types, the actual result is processed from this;
287 * see ucnv_MBCSFromUnicodeWithOffsets().
289 * Note that stage 1 always contains 0x440=1088 entries (0x440==0x110000>>10),
290 * or (version 3 and up) for BMP-only codepages, it contains 64 entries.
292 * In version 3, stage 2 blocks may overlap by multiples of the multiplier
294 * In version 4, stage 2 blocks (and for single-byte codepages, stage 3 blocks)
295 * may overlap by any number of entries.
297 * MBCS version 2 added:
298 * the converter checks for known output types, which allows
299 * adding new ones without crashing an unaware converter
303 /* GB 18030 data ------------------------------------------------------------ */
305 /* helper macros for linear values for GB 18030 four-byte sequences */
306 #define LINEAR_18030(a, b, c, d) ((((a)*10+(b))*126L+(c))*10L+(d))
308 #define LINEAR_18030_BASE LINEAR_18030(0x81, 0x30, 0x81, 0x30)
310 #define LINEAR(x) LINEAR_18030(x>>24, (x>>16)&0xff, (x>>8)&0xff, x&0xff)
313 * Some ranges of GB 18030 where both the Unicode code points and the
314 * GB four-byte sequences are contiguous and are handled algorithmically by
315 * the special callback functions below.
316 * The values are start & end of Unicode & GB codes.
318 * Note that single surrogates are not mapped by GB 18030
319 * as of the re-released mapping tables from 2000-nov-30.
321 static const uint32_t
322 gb18030Ranges
[13][4]={
323 {0x10000, 0x10FFFF, LINEAR(0x90308130), LINEAR(0xE3329A35)},
324 {0x9FA6, 0xD7FF, LINEAR(0x82358F33), LINEAR(0x8336C738)},
325 {0x0452, 0x200F, LINEAR(0x8130D330), LINEAR(0x8136A531)},
326 {0xE865, 0xF92B, LINEAR(0x8336D030), LINEAR(0x84308534)},
327 {0x2643, 0x2E80, LINEAR(0x8137A839), LINEAR(0x8138FD38)},
328 {0xFA2A, 0xFE2F, LINEAR(0x84309C38), LINEAR(0x84318537)},
329 {0x3CE1, 0x4055, LINEAR(0x8231D438), LINEAR(0x8232AF32)},
330 {0x361B, 0x3917, LINEAR(0x8230A633), LINEAR(0x8230F237)},
331 {0x49B8, 0x4C76, LINEAR(0x8234A131), LINEAR(0x8234E733)},
332 {0x4160, 0x4336, LINEAR(0x8232C937), LINEAR(0x8232F837)},
333 {0x478E, 0x4946, LINEAR(0x8233E838), LINEAR(0x82349638)},
334 {0x44D7, 0x464B, LINEAR(0x8233A339), LINEAR(0x8233C931)},
335 {0xFFE6, 0xFFFF, LINEAR(0x8431A234), LINEAR(0x8431A439)}
338 /* bit flag for UConverter.options indicating GB 18030 special handling */
339 #define _MBCS_OPTION_GB18030 0x8000
341 /* Miscellaneous ------------------------------------------------------------ */
343 /* similar to ucnv_MBCSGetNextUChar() but recursive */
345 _getUnicodeSetForBytes(const UConverterSharedData
*sharedData
,
346 const int32_t (*stateTable
)[256], const uint16_t *unicodeCodeUnits
,
348 UConverterUnicodeSet which
,
349 uint8_t state
, uint32_t offset
, int32_t lowByte
, int32_t highByte
,
351 UErrorCode
*pErrorCode
) {
354 for(b
=lowByte
; b
<=highByte
; ++b
) {
355 entry
=stateTable
[state
][b
];
356 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
357 _getUnicodeSetForBytes(
358 sharedData
, stateTable
, unicodeCodeUnits
,
360 (uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
),
361 offset
+MBCS_ENTRY_TRANSITION_OFFSET(entry
),
366 int32_t rowOffset
=offset
;
372 * An if-else-if chain provides more reliable performance for
373 * the most common cases compared to a switch.
375 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
376 if(action
==MBCS_STATE_VALID_DIRECT_16
) {
377 /* output BMP code point */
378 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
379 } else if(action
==MBCS_STATE_VALID_16
) {
380 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
381 c
=unicodeCodeUnits
[offset
];
383 /* output BMP code point */
387 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
388 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
389 c
=unicodeCodeUnits
[offset
++];
391 /* output BMP code point below 0xd800 */
392 } else if(c
<=0xdbff) {
393 /* output roundtrip or fallback supplementary code point */
394 c
=((c
&0x3ff)<<10)+unicodeCodeUnits
[offset
]+(0x10000-0xdc00);
395 } else if(c
==0xe000) {
396 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
397 c
=unicodeCodeUnits
[offset
];
401 } else if(action
==MBCS_STATE_VALID_DIRECT_20
) {
402 /* output supplementary code point */
403 c
=(UChar32
)(MBCS_ENTRY_FINAL_VALUE(entry
)+0x10000);
415 * Internal function returning a UnicodeSet for toUnicode() conversion.
416 * Currently only used for ISO-2022-CN, and only handles roundtrip mappings.
417 * In the future, if we add support for reverse-fallback sets, this function
418 * needs to be updated, and called for each initial state.
419 * Does not currently handle extensions.
420 * Does not empty the set first.
423 ucnv_MBCSGetUnicodeSetForBytes(const UConverterSharedData
*sharedData
,
425 UConverterUnicodeSet which
,
426 uint8_t state
, int32_t lowByte
, int32_t highByte
,
427 UErrorCode
*pErrorCode
) {
428 _getUnicodeSetForBytes(
429 sharedData
, sharedData
->mbcs
.stateTable
, sharedData
->mbcs
.unicodeCodeUnits
,
431 state
, 0, lowByte
, highByte
,
436 ucnv_MBCSGetUnicodeSetForUnicode(const UConverterSharedData
*sharedData
,
438 UConverterUnicodeSet which
,
439 UErrorCode
*pErrorCode
) {
440 const UConverterMBCSTable
*mbcsTable
;
441 const uint16_t *table
;
444 uint16_t st1
, maxStage1
, st2
;
448 /* enumerate the from-Unicode trie table */
449 mbcsTable
=&sharedData
->mbcs
;
450 table
=mbcsTable
->fromUnicodeTable
;
451 if(mbcsTable
->unicodeMask
&UCNV_HAS_SUPPLEMENTARY
) {
457 c
=0; /* keep track of the current code point while enumerating */
459 if(mbcsTable
->outputType
==MBCS_OUTPUT_1
) {
460 const uint16_t *stage2
, *stage3
, *results
;
462 results
=(const uint16_t *)mbcsTable
->fromUnicodeBytes
;
464 for(st1
=0; st1
<maxStage1
; ++st1
) {
468 for(st2
=0; st2
<64; ++st2
) {
469 if((st3
=stage2
[st2
])!=0) {
470 /* read the stage 3 block */
474 * Add code points for which the roundtrip flag is set.
475 * Once we get a set for fallback mappings, we have to use
476 * a threshold variable with a value of 0x800.
477 * See ucnv_MBCSSingleFromBMPWithOffsets() and
478 * MBCS_SINGLE_RESULT_FROM_U() for details.
481 if(*stage3
++>=0xf00) {
484 } while((++c
&0xf)!=0);
486 c
+=16; /* empty stage 3 block */
490 c
+=1024; /* empty stage 2 block */
493 } else if(mbcsTable
->outputType
==MBCS_OUTPUT_DBCS_ONLY
) {
494 /* ignore single-byte results */
495 const uint32_t *stage2
;
496 const uint16_t *stage3
, *results
;
498 results
=(const uint16_t *)mbcsTable
->fromUnicodeBytes
;
500 for(st1
=0; st1
<maxStage1
; ++st1
) {
502 if(st2
>(maxStage1
>>1)) {
503 stage2
=(const uint32_t *)table
+st2
;
504 for(st2
=0; st2
<64; ++st2
) {
505 if((st3
=stage2
[st2
])!=0) {
506 /* read the stage 3 block */
507 stage3
=results
+16*(uint32_t)(uint16_t)st3
;
509 /* get the roundtrip flags for the stage 3 block */
513 * Add code points for which the roundtrip flag is set.
514 * Once we get a set for fallback mappings, we have to check
515 * non-roundtrip stage 3 results for whether they are 0.
516 * See ucnv_MBCSFromUnicodeWithOffsets() for details.
518 * Ignore single-byte results (<0x100).
521 if((st3
&1)!=0 && *stage3
>=0x100) {
526 } while((++c
&0xf)!=0);
528 c
+=16; /* empty stage 3 block */
532 c
+=1024; /* empty stage 2 block */
536 const uint32_t *stage2
;
538 for(st1
=0; st1
<maxStage1
; ++st1
) {
540 if(st2
>(maxStage1
>>1)) {
541 stage2
=(const uint32_t *)table
+st2
;
542 for(st2
=0; st2
<64; ++st2
) {
543 if((st3
=stage2
[st2
])!=0) {
544 /* get the roundtrip flags for the stage 3 block */
548 * Add code points for which the roundtrip flag is set.
549 * Once we get a set for fallback mappings, we have to check
550 * non-roundtrip stage 3 results for whether they are 0.
551 * See ucnv_MBCSFromUnicodeWithOffsets() for details.
558 } while((++c
&0xf)!=0);
560 c
+=16; /* empty stage 3 block */
564 c
+=1024; /* empty stage 2 block */
569 ucnv_extGetUnicodeSet(sharedData
, sa
, which
, pErrorCode
);
573 ucnv_MBCSGetUnicodeSet(const UConverter
*cnv
,
575 UConverterUnicodeSet which
,
576 UErrorCode
*pErrorCode
) {
577 if(cnv
->options
&_MBCS_OPTION_GB18030
) {
578 sa
->addRange(sa
->set
, 0, 0xd7ff);
579 sa
->addRange(sa
->set
, 0xe000, 0x10ffff);
581 ucnv_MBCSGetUnicodeSetForUnicode(cnv
->sharedData
, sa
, which
, pErrorCode
);
585 /* conversion extensions for input not in the main table -------------------- */
588 * Hardcoded extension handling for GB 18030.
589 * Definition of LINEAR macros and gb18030Ranges see near the beginning of the file.
591 * In the future, conversion extensions may handle m:n mappings and delta tables,
592 * see http://oss.software.ibm.com/cvs/icu/~checkout~/icuhtml/design/conversion/conversion_extensions.html
594 * If an input character cannot be mapped, then these functions set an error
595 * code. The framework will then call the callback function.
599 * @return if(U_FAILURE) return the code point for cnv->fromUChar32
600 * else return 0 after output has been written to the target
603 _extFromU(UConverter
*cnv
, const UConverterSharedData
*sharedData
,
605 const UChar
**source
, const UChar
*sourceLimit
,
606 char **target
, const char *targetLimit
,
607 int32_t **offsets
, int32_t sourceIndex
,
609 UErrorCode
*pErrorCode
) {
612 cnv
->useSubChar1
=FALSE
;
614 if( (cx
=sharedData
->mbcs
.extIndexes
)!=NULL
&&
615 ucnv_extInitialMatchFromU(
617 cp
, source
, sourceLimit
,
619 offsets
, sourceIndex
,
623 return 0; /* an extension mapping handled the input */
627 if((cnv
->options
&_MBCS_OPTION_GB18030
)!=0) {
628 const uint32_t *range
;
631 range
=gb18030Ranges
[0];
632 for(i
=0; i
<sizeof(gb18030Ranges
)/sizeof(gb18030Ranges
[0]); range
+=4, ++i
) {
633 if(range
[0]<=(uint32_t)cp
&& (uint32_t)cp
<=range
[1]) {
634 /* found the Unicode code point, output the four-byte sequence for it */
638 /* get the linear value of the first GB 18030 code in this range */
639 linear
=range
[2]-LINEAR_18030_BASE
;
641 /* add the offset from the beginning of the range */
642 linear
+=((uint32_t)cp
-range
[0]);
644 /* turn this into a four-byte sequence */
645 bytes
[3]=(char)(0x30+linear%10
); linear
/=10;
646 bytes
[2]=(char)(0x81+linear%126
); linear
/=126;
647 bytes
[1]=(char)(0x30+linear%10
); linear
/=10;
648 bytes
[0]=(char)(0x81+linear
);
650 /* output this sequence */
651 ucnv_fromUWriteBytes(cnv
,
652 bytes
, 4, target
, targetLimit
,
653 offsets
, sourceIndex
, pErrorCode
);
660 *pErrorCode
=U_INVALID_CHAR_FOUND
;
665 * Input sequence: cnv->toUBytes[0..length[
666 * @return if(U_FAILURE) return the length (toULength, byteIndex) for the input
667 * else return 0 after output has been written to the target
670 _extToU(UConverter
*cnv
, const UConverterSharedData
*sharedData
,
672 const char **source
, const char *sourceLimit
,
673 UChar
**target
, const UChar
*targetLimit
,
674 int32_t **offsets
, int32_t sourceIndex
,
676 UErrorCode
*pErrorCode
) {
679 if( (cx
=sharedData
->mbcs
.extIndexes
)!=NULL
&&
680 ucnv_extInitialMatchToU(
682 length
, source
, sourceLimit
,
684 offsets
, sourceIndex
,
688 return 0; /* an extension mapping handled the input */
692 if(length
==4 && (cnv
->options
&_MBCS_OPTION_GB18030
)!=0) {
693 const uint32_t *range
;
697 linear
=LINEAR_18030(cnv
->toUBytes
[0], cnv
->toUBytes
[1], cnv
->toUBytes
[2], cnv
->toUBytes
[3]);
698 range
=gb18030Ranges
[0];
699 for(i
=0; i
<sizeof(gb18030Ranges
)/sizeof(gb18030Ranges
[0]); range
+=4, ++i
) {
700 if(range
[2]<=linear
&& linear
<=range
[3]) {
701 /* found the sequence, output the Unicode code point for it */
702 *pErrorCode
=U_ZERO_ERROR
;
704 /* add the linear difference between the input and start sequences to the start code point */
705 linear
=range
[0]+(linear
-range
[2]);
707 /* output this code point */
708 ucnv_toUWriteCodePoint(cnv
, linear
, target
, targetLimit
, offsets
, sourceIndex
, pErrorCode
);
716 *pErrorCode
=U_INVALID_CHAR_FOUND
;
720 /* EBCDIC swap LF<->NL ------------------------------------------------------ */
723 * This code modifies a standard EBCDIC<->Unicode mapping table for
724 * OS/390 (z/OS) Unix System Services (Open Edition).
725 * The difference is in the mapping of Line Feed and New Line control codes:
726 * Standard EBCDIC maps
731 * but OS/390 USS EBCDIC swaps the control codes for LF and NL,
737 * This code modifies a loaded standard EBCDIC<->Unicode mapping table
738 * by copying it into allocated memory and swapping the LF and NL values.
739 * It allows to support the same EBCDIC charset in both versions without
740 * duplicating the entire installed table.
743 /* standard EBCDIC codes */
744 #define EBCDIC_LF 0x25
745 #define EBCDIC_NL 0x15
747 /* standard EBCDIC codes with roundtrip flag as stored in Unicode-to-single-byte tables */
748 #define EBCDIC_RT_LF 0xf25
749 #define EBCDIC_RT_NL 0xf15
751 /* Unicode code points */
756 _EBCDICSwapLFNL(UConverterSharedData
*sharedData
, UErrorCode
*pErrorCode
) {
757 UConverterMBCSTable
*mbcsTable
;
759 const uint16_t *table
, *results
;
760 const uint8_t *bytes
;
762 int32_t (*newStateTable
)[256];
763 uint16_t *newResults
;
767 uint32_t stage2Entry
;
768 uint32_t size
, sizeofFromUBytes
;
770 mbcsTable
=&sharedData
->mbcs
;
772 table
=mbcsTable
->fromUnicodeTable
;
773 bytes
=mbcsTable
->fromUnicodeBytes
;
774 results
=(const uint16_t *)bytes
;
777 * Check that this is an EBCDIC table with SBCS portion -
778 * SBCS or EBCDIC_STATEFUL with standard EBCDIC LF and NL mappings.
780 * If not, ignore the option. Options are always ignored if they do not apply.
783 (mbcsTable
->outputType
==MBCS_OUTPUT_1
|| mbcsTable
->outputType
==MBCS_OUTPUT_2_SISO
) &&
784 mbcsTable
->stateTable
[0][EBCDIC_LF
]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_LF
) &&
785 mbcsTable
->stateTable
[0][EBCDIC_NL
]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_NL
)
790 if(mbcsTable
->outputType
==MBCS_OUTPUT_1
) {
792 EBCDIC_RT_LF
==MBCS_SINGLE_RESULT_FROM_U(table
, results
, U_LF
) &&
793 EBCDIC_RT_NL
==MBCS_SINGLE_RESULT_FROM_U(table
, results
, U_NL
)
797 } else /* MBCS_OUTPUT_2_SISO */ {
798 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_LF
);
800 MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, U_LF
)!=0 &&
801 EBCDIC_LF
==MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, U_LF
)
806 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_NL
);
808 MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, U_NL
)!=0 &&
809 EBCDIC_NL
==MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, U_NL
)
815 if(mbcsTable
->fromUBytesLength
>0) {
817 * We _know_ the number of bytes in the fromUnicodeBytes array
818 * starting with header.version 4.1.
820 sizeofFromUBytes
=mbcsTable
->fromUBytesLength
;
824 * There used to be code to enumerate the fromUnicode
825 * trie and find the highest entry, but it was removed in ICU 3.2
826 * because it was not tested and caused a low code coverage number.
827 * See Jitterbug 3674.
828 * This affects only some .cnv file formats with a header.version
829 * below 4.1, and only when swaplfnl is requested.
831 * ucnvmbcs.c revision 1.99 is the last one with the
832 * ucnv_MBCSSizeofFromUBytes() function.
834 *pErrorCode
=U_INVALID_FORMAT_ERROR
;
839 * The table has an appropriate format.
841 * - a modified to-Unicode state table
842 * - a modified from-Unicode output array
843 * - a converter name string with the swap option appended
846 mbcsTable
->countStates
*1024+
848 UCNV_MAX_CONVERTER_NAME_LENGTH
+20;
849 p
=(uint8_t *)uprv_malloc(size
);
851 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
855 /* copy and modify the to-Unicode state table */
856 newStateTable
=(int32_t (*)[256])p
;
857 uprv_memcpy(newStateTable
, mbcsTable
->stateTable
, mbcsTable
->countStates
*1024);
859 newStateTable
[0][EBCDIC_LF
]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_NL
);
860 newStateTable
[0][EBCDIC_NL
]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16
, U_LF
);
862 /* copy and modify the from-Unicode result table */
863 newResults
=(uint16_t *)newStateTable
[mbcsTable
->countStates
];
864 uprv_memcpy(newResults
, bytes
, sizeofFromUBytes
);
866 /* conveniently, the table access macros work on the left side of expressions */
867 if(mbcsTable
->outputType
==MBCS_OUTPUT_1
) {
868 MBCS_SINGLE_RESULT_FROM_U(table
, newResults
, U_LF
)=EBCDIC_RT_NL
;
869 MBCS_SINGLE_RESULT_FROM_U(table
, newResults
, U_NL
)=EBCDIC_RT_LF
;
870 } else /* MBCS_OUTPUT_2_SISO */ {
871 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_LF
);
872 MBCS_VALUE_2_FROM_STAGE_2(newResults
, stage2Entry
, U_LF
)=EBCDIC_NL
;
874 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, U_NL
);
875 MBCS_VALUE_2_FROM_STAGE_2(newResults
, stage2Entry
, U_NL
)=EBCDIC_LF
;
878 /* set the canonical converter name */
879 name
=(char *)newResults
+sizeofFromUBytes
;
880 uprv_strcpy(name
, sharedData
->staticData
->name
);
881 uprv_strcat(name
, UCNV_SWAP_LFNL_OPTION_STRING
);
883 /* set the pointers */
885 if(mbcsTable
->swapLFNLStateTable
==NULL
) {
886 mbcsTable
->swapLFNLStateTable
=newStateTable
;
887 mbcsTable
->swapLFNLFromUnicodeBytes
=(uint8_t *)newResults
;
888 mbcsTable
->swapLFNLName
=name
;
894 /* release the allocated memory if another thread beat us to it */
895 if(newStateTable
!=NULL
) {
896 uprv_free(newStateTable
);
901 /* MBCS setup functions ----------------------------------------------------- */
904 ucnv_MBCSLoad(UConverterSharedData
*sharedData
,
905 UConverterLoadArgs
*pArgs
,
907 UErrorCode
*pErrorCode
) {
909 UConverterMBCSTable
*mbcsTable
=&sharedData
->mbcs
;
910 _MBCSHeader
*header
=(_MBCSHeader
*)raw
;
913 if(header
->version
[0]!=4) {
914 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
918 mbcsTable
->outputType
=(uint8_t)header
->flags
;
920 /* extension data, header version 4.2 and higher */
921 offset
=header
->flags
>>8;
923 mbcsTable
->extIndexes
=(const int32_t *)(raw
+offset
);
926 if(mbcsTable
->outputType
==MBCS_OUTPUT_EXT_ONLY
) {
927 UConverterLoadArgs args
={ 0 };
928 UConverterSharedData
*baseSharedData
;
929 const int32_t *extIndexes
;
930 const char *baseName
;
932 /* extension-only file, load the base table and set values appropriately */
933 if((extIndexes
=mbcsTable
->extIndexes
)==NULL
) {
934 /* extension-only file without extension */
935 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
939 if(pArgs
->nestedLoads
!=1) {
940 /* an extension table must not be loaded as a base table */
941 *pErrorCode
=U_INVALID_TABLE_FILE
;
945 /* load the base table */
946 baseName
=(const char *)(header
+1);
947 if(0==uprv_strcmp(baseName
, sharedData
->staticData
->name
)) {
948 /* forbid loading this same extension-only file */
949 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
953 /* TODO parse package name out of the prefix of the base name in the extension .cnv file? */
954 args
.size
=sizeof(UConverterLoadArgs
);
956 args
.reserved
=pArgs
->reserved
;
957 args
.options
=pArgs
->options
;
960 baseSharedData
=ucnv_load(&args
, pErrorCode
);
961 if(U_FAILURE(*pErrorCode
)) {
964 if( baseSharedData
->staticData
->conversionType
!=UCNV_MBCS
||
965 baseSharedData
->mbcs
.baseSharedData
!=NULL
967 ucnv_unload(baseSharedData
);
968 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
972 /* copy the base table data */
973 uprv_memcpy(mbcsTable
, &baseSharedData
->mbcs
, sizeof(UConverterMBCSTable
));
975 /* overwrite values with relevant ones for the extension converter */
976 mbcsTable
->baseSharedData
=baseSharedData
;
977 mbcsTable
->extIndexes
=extIndexes
;
980 * It would be possible to share the swapLFNL data with a base converter,
981 * but the generated name would have to be different, and the memory
982 * would have to be free'd only once.
983 * It is easier to just create the data for the extension converter
984 * separately when it is requested.
986 mbcsTable
->swapLFNLStateTable
=NULL
;
987 mbcsTable
->swapLFNLFromUnicodeBytes
=NULL
;
988 mbcsTable
->swapLFNLName
=NULL
;
991 * Set a special, runtime-only outputType if the extension converter
992 * is a DBCS version of a base converter that also maps single bytes.
994 if( sharedData
->staticData
->conversionType
==UCNV_DBCS
||
995 (sharedData
->staticData
->conversionType
==UCNV_MBCS
&&
996 sharedData
->staticData
->minBytesPerChar
>=2)
998 if(baseSharedData
->mbcs
.outputType
==MBCS_OUTPUT_2_SISO
) {
999 /* the base converter is SI/SO-stateful */
1002 /* get the dbcs state from the state table entry for SO=0x0e */
1003 entry
=mbcsTable
->stateTable
[0][0xe];
1004 if( MBCS_ENTRY_IS_FINAL(entry
) &&
1005 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_CHANGE_ONLY
&&
1006 MBCS_ENTRY_FINAL_STATE(entry
)!=0
1008 mbcsTable
->dbcsOnlyState
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
);
1010 mbcsTable
->outputType
=MBCS_OUTPUT_DBCS_ONLY
;
1013 baseSharedData
->staticData
->conversionType
==UCNV_MBCS
&&
1014 baseSharedData
->staticData
->minBytesPerChar
==1 &&
1015 baseSharedData
->staticData
->maxBytesPerChar
==2 &&
1016 mbcsTable
->countStates
<=127
1018 /* non-stateful base converter, need to modify the state table */
1019 int32_t (*newStateTable
)[256];
1023 /* allocate a new state table and copy the base state table contents */
1024 count
=mbcsTable
->countStates
;
1025 newStateTable
=(int32_t (*)[256])uprv_malloc((count
+1)*1024);
1026 if(newStateTable
==NULL
) {
1027 ucnv_unload(baseSharedData
);
1028 *pErrorCode
=U_MEMORY_ALLOCATION_ERROR
;
1032 uprv_memcpy(newStateTable
, mbcsTable
->stateTable
, count
*1024);
1034 /* change all final single-byte entries to go to a new all-illegal state */
1035 state
=newStateTable
[0];
1036 for(i
=0; i
<256; ++i
) {
1037 if(MBCS_ENTRY_IS_FINAL(state
[i
])) {
1038 state
[i
]=MBCS_ENTRY_TRANSITION(count
, 0);
1042 /* build the new all-illegal state */
1043 state
=newStateTable
[count
];
1044 for(i
=0; i
<256; ++i
) {
1045 state
[i
]=MBCS_ENTRY_FINAL(0, MBCS_STATE_ILLEGAL
, 0);
1047 mbcsTable
->stateTable
=(const int32_t (*)[256])newStateTable
;
1048 mbcsTable
->countStates
=(uint8_t)(count
+1);
1049 mbcsTable
->stateTableOwned
=TRUE
;
1051 mbcsTable
->outputType
=MBCS_OUTPUT_DBCS_ONLY
;
1056 * unlike below for files with base tables, do not get the unicodeMask
1057 * from the sharedData; instead, use the base table's unicodeMask,
1058 * which we copied in the memcpy above;
1059 * this is necessary because the static data unicodeMask, especially
1060 * the UCNV_HAS_SUPPLEMENTARY flag, is part of the base table data
1063 /* conversion file with a base table; an additional extension table is optional */
1064 /* make sure that the output type is known */
1065 switch(mbcsTable
->outputType
) {
1070 case MBCS_OUTPUT_3_EUC
:
1071 case MBCS_OUTPUT_4_EUC
:
1072 case MBCS_OUTPUT_2_SISO
:
1076 *pErrorCode
=U_INVALID_TABLE_FORMAT
;
1080 mbcsTable
->countStates
=(uint8_t)header
->countStates
;
1081 mbcsTable
->countToUFallbacks
=header
->countToUFallbacks
;
1082 mbcsTable
->stateTable
=(const int32_t (*)[256])(raw
+sizeof(_MBCSHeader
));
1083 mbcsTable
->toUFallbacks
=(const _MBCSToUFallback
*)(mbcsTable
->stateTable
+header
->countStates
);
1084 mbcsTable
->unicodeCodeUnits
=(const uint16_t *)(raw
+header
->offsetToUCodeUnits
);
1086 mbcsTable
->fromUnicodeTable
=(const uint16_t *)(raw
+header
->offsetFromUTable
);
1087 mbcsTable
->fromUnicodeBytes
=(const uint8_t *)(raw
+header
->offsetFromUBytes
);
1088 mbcsTable
->fromUBytesLength
=header
->fromUBytesLength
;
1091 * converter versions 6.1 and up contain a unicodeMask that is
1092 * used here to select the most efficient function implementations
1094 info
.size
=sizeof(UDataInfo
);
1095 udata_getInfo((UDataMemory
*)sharedData
->dataMemory
, &info
);
1096 if(info
.formatVersion
[0]>6 || (info
.formatVersion
[0]==6 && info
.formatVersion
[1]>=1)) {
1097 /* mask off possible future extensions to be safe */
1098 mbcsTable
->unicodeMask
=(uint8_t)(sharedData
->staticData
->unicodeMask
&3);
1100 /* for older versions, assume worst case: contains anything possible (prevent over-optimizations) */
1101 mbcsTable
->unicodeMask
=UCNV_HAS_SUPPLEMENTARY
|UCNV_HAS_SURROGATES
;
1107 ucnv_MBCSUnload(UConverterSharedData
*sharedData
) {
1108 UConverterMBCSTable
*mbcsTable
=&sharedData
->mbcs
;
1110 if(mbcsTable
->swapLFNLStateTable
!=NULL
) {
1111 uprv_free(mbcsTable
->swapLFNLStateTable
);
1113 if(mbcsTable
->stateTableOwned
) {
1114 uprv_free((void *)mbcsTable
->stateTable
);
1116 if(mbcsTable
->baseSharedData
!=NULL
) {
1117 ucnv_unload(mbcsTable
->baseSharedData
);
1122 ucnv_MBCSOpen(UConverter
*cnv
,
1126 UErrorCode
*pErrorCode
) {
1127 UConverterMBCSTable
*mbcsTable
;
1128 const int32_t *extIndexes
;
1130 int8_t maxBytesPerUChar
;
1132 mbcsTable
=&cnv
->sharedData
->mbcs
;
1133 outputType
=mbcsTable
->outputType
;
1135 if(outputType
==MBCS_OUTPUT_DBCS_ONLY
) {
1136 /* the swaplfnl option does not apply, remove it */
1137 cnv
->options
=options
&=~UCNV_OPTION_SWAP_LFNL
;
1140 if((options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
1141 /* do this because double-checked locking is broken */
1145 isCached
=mbcsTable
->swapLFNLStateTable
!=NULL
;
1149 if(!_EBCDICSwapLFNL(cnv
->sharedData
, pErrorCode
)) {
1150 if(U_FAILURE(*pErrorCode
)) {
1151 return; /* something went wrong */
1154 /* the option does not apply, remove it */
1155 cnv
->options
=options
&=~UCNV_OPTION_SWAP_LFNL
;
1160 if(uprv_strstr(name
, "18030")!=NULL
) {
1161 if(uprv_strstr(name
, "gb18030")!=NULL
|| uprv_strstr(name
, "GB18030")!=NULL
) {
1162 /* set a flag for GB 18030 mode, which changes the callback behavior */
1163 cnv
->options
|=_MBCS_OPTION_GB18030
;
1167 /* fix maxBytesPerUChar depending on outputType and options etc. */
1168 if(outputType
==MBCS_OUTPUT_2_SISO
) {
1169 cnv
->maxBytesPerUChar
=3; /* SO+DBCS */
1172 extIndexes
=mbcsTable
->extIndexes
;
1173 if(extIndexes
!=NULL
) {
1174 maxBytesPerUChar
=(int8_t)UCNV_GET_MAX_BYTES_PER_UCHAR(extIndexes
);
1175 if(outputType
==MBCS_OUTPUT_2_SISO
) {
1176 ++maxBytesPerUChar
; /* SO + multiple DBCS */
1179 if(maxBytesPerUChar
>cnv
->maxBytesPerUChar
) {
1180 cnv
->maxBytesPerUChar
=maxBytesPerUChar
;
1186 * documentation of UConverter fields used for status
1187 * all of these fields are (re)set to 0 by ucnv_bld.c and ucnv_reset()
1191 cnv
->toUnicodeStatus
=0; /* offset */
1192 cnv
->mode
=0; /* state */
1193 cnv
->toULength
=0; /* byteIndex */
1197 cnv
->fromUnicodeStatus
=1; /* prevLength */
1202 ucnv_MBCSGetName(const UConverter
*cnv
) {
1203 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0 && cnv
->sharedData
->mbcs
.swapLFNLName
!=NULL
) {
1204 return cnv
->sharedData
->mbcs
.swapLFNLName
;
1206 return cnv
->sharedData
->staticData
->name
;
1210 /* MBCS-to-Unicode conversion functions ------------------------------------- */
1213 ucnv_MBCSGetFallback(UConverterMBCSTable
*mbcsTable
, uint32_t offset
) {
1214 const _MBCSToUFallback
*toUFallbacks
;
1215 uint32_t i
, start
, limit
;
1217 limit
=mbcsTable
->countToUFallbacks
;
1219 /* do a binary search for the fallback mapping */
1220 toUFallbacks
=mbcsTable
->toUFallbacks
;
1222 while(start
<limit
-1) {
1224 if(offset
<toUFallbacks
[i
].offset
) {
1231 /* did we really find it? */
1232 if(offset
==toUFallbacks
[start
].offset
) {
1233 return toUFallbacks
[start
].codePoint
;
1240 /* This version of ucnv_MBCSToUnicodeWithOffsets() is optimized for single-byte, single-state codepages. */
1242 ucnv_MBCSSingleToUnicodeWithOffsets(UConverterToUnicodeArgs
*pArgs
,
1243 UErrorCode
*pErrorCode
) {
1245 const uint8_t *source
, *sourceLimit
;
1247 const UChar
*targetLimit
;
1250 const int32_t (*stateTable
)[256];
1252 int32_t sourceIndex
;
1258 /* set up the local pointers */
1259 cnv
=pArgs
->converter
;
1260 source
=(const uint8_t *)pArgs
->source
;
1261 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
1262 target
=pArgs
->target
;
1263 targetLimit
=pArgs
->targetLimit
;
1264 offsets
=pArgs
->offsets
;
1266 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
1267 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
1269 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
1272 /* sourceIndex=-1 if the current character began in the previous buffer */
1275 /* conversion loop */
1276 while(source
<sourceLimit
) {
1278 * This following test is to see if available input would overflow the output.
1279 * It does not catch output of more than one code unit that
1280 * overflows as a result of a surrogate pair or callback output
1281 * from the last source byte.
1282 * Therefore, those situations also test for overflows and will
1283 * then break the loop, too.
1285 if(target
>=targetLimit
) {
1286 /* target is full */
1287 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1291 entry
=stateTable
[0][*source
++];
1292 /* MBCS_ENTRY_IS_FINAL(entry) */
1294 /* test the most common case first */
1295 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
1296 /* output BMP code point */
1297 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1299 *offsets
++=sourceIndex
;
1302 /* normal end of action codes: prepare for a new character */
1308 * An if-else-if chain provides more reliable performance for
1309 * the most common cases compared to a switch.
1311 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
1312 if(action
==MBCS_STATE_VALID_DIRECT_20
||
1313 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
1315 entry
=MBCS_ENTRY_FINAL_VALUE(entry
);
1316 /* output surrogate pair */
1317 *target
++=(UChar
)(0xd800|(UChar
)(entry
>>10));
1319 *offsets
++=sourceIndex
;
1321 c
=(UChar
)(0xdc00|(UChar
)(entry
&0x3ff));
1322 if(target
<targetLimit
) {
1325 *offsets
++=sourceIndex
;
1328 /* target overflow */
1329 cnv
->UCharErrorBuffer
[0]=c
;
1330 cnv
->UCharErrorBufferLength
=1;
1331 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1337 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
1338 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
1339 /* output BMP code point */
1340 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1342 *offsets
++=sourceIndex
;
1348 } else if(action
==MBCS_STATE_UNASSIGNED
) {
1349 /* just fall through */
1350 } else if(action
==MBCS_STATE_ILLEGAL
) {
1351 /* callback(illegal) */
1352 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
1354 /* reserved, must never occur */
1359 if(U_FAILURE(*pErrorCode
)) {
1360 /* callback(illegal) */
1362 } else /* unassigned sequences indicated with byteIndex>0 */ {
1363 /* try an extension mapping */
1364 pArgs
->source
=(const char *)source
;
1365 cnv
->toUBytes
[0]=*(source
-1);
1366 cnv
->toULength
=_extToU(cnv
, cnv
->sharedData
,
1367 1, (const char **)&source
, (const char *)sourceLimit
,
1368 &target
, targetLimit
,
1369 &offsets
, sourceIndex
,
1372 sourceIndex
+=1+(int32_t)(source
-(const uint8_t *)pArgs
->source
);
1374 if(U_FAILURE(*pErrorCode
)) {
1375 /* not mappable or buffer overflow */
1381 /* write back the updated pointers */
1382 pArgs
->source
=(const char *)source
;
1383 pArgs
->target
=target
;
1384 pArgs
->offsets
=offsets
;
1388 * This version of ucnv_MBCSSingleToUnicodeWithOffsets() is optimized for single-byte, single-state codepages
1389 * that only map to and from the BMP.
1390 * In addition to single-byte optimizations, the offset calculations
1391 * become much easier.
1394 ucnv_MBCSSingleToBMPWithOffsets(UConverterToUnicodeArgs
*pArgs
,
1395 UErrorCode
*pErrorCode
) {
1397 const uint8_t *source
, *sourceLimit
, *lastSource
;
1399 int32_t targetCapacity
, length
;
1402 const int32_t (*stateTable
)[256];
1404 int32_t sourceIndex
;
1409 /* set up the local pointers */
1410 cnv
=pArgs
->converter
;
1411 source
=(const uint8_t *)pArgs
->source
;
1412 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
1413 target
=pArgs
->target
;
1414 targetCapacity
=pArgs
->targetLimit
-pArgs
->target
;
1415 offsets
=pArgs
->offsets
;
1417 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
1418 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
1420 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
1423 /* sourceIndex=-1 if the current character began in the previous buffer */
1428 * since the conversion here is 1:1 UChar:uint8_t, we need only one counter
1429 * for the minimum of the sourceLength and targetCapacity
1431 length
=sourceLimit
-source
;
1432 if(length
<targetCapacity
) {
1433 targetCapacity
=length
;
1436 #if MBCS_UNROLL_SINGLE_TO_BMP
1437 /* unrolling makes it faster on Pentium III/Windows 2000 */
1438 /* unroll the loop with the most common case */
1440 if(targetCapacity
>=16) {
1441 int32_t count
, loops
, oredEntries
;
1443 loops
=count
=targetCapacity
>>4;
1445 oredEntries
=entry
=stateTable
[0][*source
++];
1446 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1447 oredEntries
|=entry
=stateTable
[0][*source
++];
1448 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1449 oredEntries
|=entry
=stateTable
[0][*source
++];
1450 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1451 oredEntries
|=entry
=stateTable
[0][*source
++];
1452 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1453 oredEntries
|=entry
=stateTable
[0][*source
++];
1454 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1455 oredEntries
|=entry
=stateTable
[0][*source
++];
1456 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1457 oredEntries
|=entry
=stateTable
[0][*source
++];
1458 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1459 oredEntries
|=entry
=stateTable
[0][*source
++];
1460 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1461 oredEntries
|=entry
=stateTable
[0][*source
++];
1462 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1463 oredEntries
|=entry
=stateTable
[0][*source
++];
1464 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1465 oredEntries
|=entry
=stateTable
[0][*source
++];
1466 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1467 oredEntries
|=entry
=stateTable
[0][*source
++];
1468 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1469 oredEntries
|=entry
=stateTable
[0][*source
++];
1470 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1471 oredEntries
|=entry
=stateTable
[0][*source
++];
1472 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1473 oredEntries
|=entry
=stateTable
[0][*source
++];
1474 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1475 oredEntries
|=entry
=stateTable
[0][*source
++];
1476 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1478 /* were all 16 entries really valid? */
1479 if(!MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(oredEntries
)) {
1480 /* no, return to the first of these 16 */
1487 targetCapacity
-=16*count
;
1490 lastSource
+=16*count
;
1492 *offsets
++=sourceIndex
++;
1493 *offsets
++=sourceIndex
++;
1494 *offsets
++=sourceIndex
++;
1495 *offsets
++=sourceIndex
++;
1496 *offsets
++=sourceIndex
++;
1497 *offsets
++=sourceIndex
++;
1498 *offsets
++=sourceIndex
++;
1499 *offsets
++=sourceIndex
++;
1500 *offsets
++=sourceIndex
++;
1501 *offsets
++=sourceIndex
++;
1502 *offsets
++=sourceIndex
++;
1503 *offsets
++=sourceIndex
++;
1504 *offsets
++=sourceIndex
++;
1505 *offsets
++=sourceIndex
++;
1506 *offsets
++=sourceIndex
++;
1507 *offsets
++=sourceIndex
++;
1514 /* conversion loop */
1515 while(targetCapacity
>0) {
1516 entry
=stateTable
[0][*source
++];
1517 /* MBCS_ENTRY_IS_FINAL(entry) */
1519 /* test the most common case first */
1520 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
1521 /* output BMP code point */
1522 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1528 * An if-else-if chain provides more reliable performance for
1529 * the most common cases compared to a switch.
1531 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
1532 if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
1533 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
1534 /* output BMP code point */
1535 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1539 } else if(action
==MBCS_STATE_UNASSIGNED
) {
1540 /* just fall through */
1541 } else if(action
==MBCS_STATE_ILLEGAL
) {
1542 /* callback(illegal) */
1543 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
1545 /* reserved, must never occur */
1549 /* set offsets since the start or the last extension */
1551 int32_t count
=(int32_t)(source
-lastSource
);
1553 /* predecrement: do not set the offset for the callback-causing character */
1555 *offsets
++=sourceIndex
++;
1557 /* offset and sourceIndex are now set for the current character */
1560 if(U_FAILURE(*pErrorCode
)) {
1561 /* callback(illegal) */
1563 } else /* unassigned sequences indicated with byteIndex>0 */ {
1564 /* try an extension mapping */
1566 cnv
->toUBytes
[0]=*(source
-1);
1567 cnv
->toULength
=_extToU(cnv
, cnv
->sharedData
,
1568 1, (const char **)&source
, (const char *)sourceLimit
,
1569 &target
, target
+targetCapacity
,
1570 &offsets
, sourceIndex
,
1573 sourceIndex
+=1+(int32_t)(source
-lastSource
);
1575 if(U_FAILURE(*pErrorCode
)) {
1576 /* not mappable or buffer overflow */
1580 /* recalculate the targetCapacity after an extension mapping */
1581 targetCapacity
=pArgs
->targetLimit
-target
;
1582 length
=sourceLimit
-source
;
1583 if(length
<targetCapacity
) {
1584 targetCapacity
=length
;
1588 #if MBCS_UNROLL_SINGLE_TO_BMP
1589 /* unrolling makes it faster on Pentium III/Windows 2000 */
1594 if(U_SUCCESS(*pErrorCode
) && source
<sourceLimit
&& target
>=pArgs
->targetLimit
) {
1595 /* target is full */
1596 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1599 /* set offsets since the start or the last callback */
1601 size_t count
=source
-lastSource
;
1603 *offsets
++=sourceIndex
++;
1608 /* write back the updated pointers */
1609 pArgs
->source
=(const char *)source
;
1610 pArgs
->target
=target
;
1611 pArgs
->offsets
=offsets
;
1615 ucnv_MBCSToUnicodeWithOffsets(UConverterToUnicodeArgs
*pArgs
,
1616 UErrorCode
*pErrorCode
) {
1618 const uint8_t *source
, *sourceLimit
;
1620 const UChar
*targetLimit
;
1623 const int32_t (*stateTable
)[256];
1624 const uint16_t *unicodeCodeUnits
;
1631 int32_t sourceIndex
, nextSourceIndex
;
1637 /* use optimized function if possible */
1638 cnv
=pArgs
->converter
;
1640 if(cnv
->preToULength
>0) {
1642 * pass sourceIndex=-1 because we continue from an earlier buffer
1643 * in the future, this may change with continuous offsets
1645 ucnv_extContinueMatchToU(cnv
, pArgs
, -1, pErrorCode
);
1647 if(U_FAILURE(*pErrorCode
) || cnv
->preToULength
<0) {
1652 if(cnv
->sharedData
->mbcs
.countStates
==1) {
1653 if(!(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
1654 ucnv_MBCSSingleToBMPWithOffsets(pArgs
, pErrorCode
);
1656 ucnv_MBCSSingleToUnicodeWithOffsets(pArgs
, pErrorCode
);
1661 /* set up the local pointers */
1662 source
=(const uint8_t *)pArgs
->source
;
1663 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
1664 target
=pArgs
->target
;
1665 targetLimit
=pArgs
->targetLimit
;
1666 offsets
=pArgs
->offsets
;
1668 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
1669 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
1671 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
1673 unicodeCodeUnits
=cnv
->sharedData
->mbcs
.unicodeCodeUnits
;
1675 /* get the converter state from UConverter */
1676 offset
=cnv
->toUnicodeStatus
;
1677 byteIndex
=cnv
->toULength
;
1678 bytes
=cnv
->toUBytes
;
1681 * if we are in the SBCS state for a DBCS-only converter,
1682 * then load the DBCS state from the MBCS data
1683 * (dbcsOnlyState==0 if it is not a DBCS-only converter)
1685 if((state
=(uint8_t)(cnv
->mode
))==0) {
1686 state
=cnv
->sharedData
->mbcs
.dbcsOnlyState
;
1689 /* sourceIndex=-1 if the current character began in the previous buffer */
1690 sourceIndex
=byteIndex
==0 ? 0 : -1;
1693 /* conversion loop */
1694 while(source
<sourceLimit
) {
1696 * This following test is to see if available input would overflow the output.
1697 * It does not catch output of more than one code unit that
1698 * overflows as a result of a surrogate pair or callback output
1699 * from the last source byte.
1700 * Therefore, those situations also test for overflows and will
1701 * then break the loop, too.
1703 if(target
>=targetLimit
) {
1704 /* target is full */
1705 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1710 /* optimized loop for 1/2-byte input and BMP output */
1713 entry
=stateTable
[state
][*source
];
1714 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
1715 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
1716 offset
=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
1719 if( source
<sourceLimit
&&
1720 MBCS_ENTRY_IS_FINAL(entry
=stateTable
[state
][*source
]) &&
1721 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_VALID_16
&&
1722 (c
=unicodeCodeUnits
[offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
)])<0xfffe
1726 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
1729 /* set the state and leave the optimized loop */
1730 bytes
[0]=*(source
-1);
1735 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
1736 /* output BMP code point */
1738 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1739 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
1741 /* leave the optimized loop */
1745 } while(source
<sourceLimit
&& target
<targetLimit
);
1746 } else /* offsets!=NULL */ {
1748 entry
=stateTable
[state
][*source
];
1749 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
1750 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
1751 offset
=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
1754 if( source
<sourceLimit
&&
1755 MBCS_ENTRY_IS_FINAL(entry
=stateTable
[state
][*source
]) &&
1756 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_VALID_16
&&
1757 (c
=unicodeCodeUnits
[offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
)])<0xfffe
1762 *offsets
++=sourceIndex
;
1763 sourceIndex
=(nextSourceIndex
+=2);
1765 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
1768 /* set the state and leave the optimized loop */
1770 bytes
[0]=*(source
-1);
1775 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
1776 /* output BMP code point */
1778 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1780 *offsets
++=sourceIndex
;
1781 sourceIndex
=++nextSourceIndex
;
1783 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
1785 /* leave the optimized loop */
1789 } while(source
<sourceLimit
&& target
<targetLimit
);
1793 * these tests and break statements could be put inside the loop
1794 * if C had "break outerLoop" like Java
1796 if(source
>=sourceLimit
) {
1799 if(target
>=targetLimit
) {
1800 /* target is full */
1801 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1806 bytes
[byteIndex
++]=*source
++;
1807 } else /* byteIndex>0 */ {
1809 entry
=stateTable
[state
][bytes
[byteIndex
++]=*source
++];
1812 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
1813 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
1814 offset
+=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
1818 /* save the previous state for proper extension mapping with SI/SO-stateful converters */
1821 /* set the next state early so that we can reuse the entry variable */
1822 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
1825 * An if-else-if chain provides more reliable performance for
1826 * the most common cases compared to a switch.
1828 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
1829 if(action
==MBCS_STATE_VALID_16
) {
1830 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
1831 c
=unicodeCodeUnits
[offset
];
1833 /* output BMP code point */
1836 *offsets
++=sourceIndex
;
1839 } else if(c
==0xfffe) {
1840 if(UCNV_TO_U_USE_FALLBACK(cnv
) && (entry
=(int32_t)ucnv_MBCSGetFallback(&cnv
->sharedData
->mbcs
, offset
))!=0xfffe) {
1841 /* output fallback BMP code point */
1842 *target
++=(UChar
)entry
;
1844 *offsets
++=sourceIndex
;
1849 /* callback(illegal) */
1850 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
1852 } else if(action
==MBCS_STATE_VALID_DIRECT_16
) {
1853 /* output BMP code point */
1854 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1856 *offsets
++=sourceIndex
;
1859 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
1860 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
1861 c
=unicodeCodeUnits
[offset
++];
1863 /* output BMP code point below 0xd800 */
1866 *offsets
++=sourceIndex
;
1869 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? c
<=0xdfff : c
<=0xdbff) {
1870 /* output roundtrip or fallback surrogate pair */
1871 *target
++=(UChar
)(c
&0xdbff);
1873 *offsets
++=sourceIndex
;
1876 if(target
<targetLimit
) {
1877 *target
++=unicodeCodeUnits
[offset
];
1879 *offsets
++=sourceIndex
;
1882 /* target overflow */
1883 cnv
->UCharErrorBuffer
[0]=unicodeCodeUnits
[offset
];
1884 cnv
->UCharErrorBufferLength
=1;
1885 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1890 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? (c
&0xfffe)==0xe000 : c
==0xe000) {
1891 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
1892 *target
++=unicodeCodeUnits
[offset
];
1894 *offsets
++=sourceIndex
;
1897 } else if(c
==0xffff) {
1898 /* callback(illegal) */
1899 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
1901 } else if(action
==MBCS_STATE_VALID_DIRECT_20
||
1902 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
1904 entry
=MBCS_ENTRY_FINAL_VALUE(entry
);
1905 /* output surrogate pair */
1906 *target
++=(UChar
)(0xd800|(UChar
)(entry
>>10));
1908 *offsets
++=sourceIndex
;
1911 c
=(UChar
)(0xdc00|(UChar
)(entry
&0x3ff));
1912 if(target
<targetLimit
) {
1915 *offsets
++=sourceIndex
;
1918 /* target overflow */
1919 cnv
->UCharErrorBuffer
[0]=c
;
1920 cnv
->UCharErrorBufferLength
=1;
1921 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
1926 } else if(action
==MBCS_STATE_CHANGE_ONLY
) {
1928 * This serves as a state change without any output.
1929 * It is useful for reading simple stateful encodings,
1930 * for example using just Shift-In/Shift-Out codes.
1931 * The 21 unused bits may later be used for more sophisticated
1932 * state transitions.
1934 if(cnv
->sharedData
->mbcs
.dbcsOnlyState
==0) {
1937 /* SI/SO are illegal for DBCS-only conversion */
1938 state
=(uint8_t)(cnv
->mode
); /* restore the previous state */
1940 /* callback(illegal) */
1941 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
1943 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
1944 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
1945 /* output BMP code point */
1946 *target
++=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
1948 *offsets
++=sourceIndex
;
1952 } else if(action
==MBCS_STATE_UNASSIGNED
) {
1953 /* just fall through */
1954 } else if(action
==MBCS_STATE_ILLEGAL
) {
1955 /* callback(illegal) */
1956 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
1958 /* reserved, must never occur */
1962 /* end of action codes: prepare for a new character */
1966 sourceIndex
=nextSourceIndex
;
1967 } else if(U_FAILURE(*pErrorCode
)) {
1968 /* callback(illegal) */
1970 } else /* unassigned sequences indicated with byteIndex>0 */ {
1971 /* try an extension mapping */
1972 pArgs
->source
=(const char *)source
;
1973 byteIndex
=_extToU(cnv
, cnv
->sharedData
,
1974 byteIndex
, (const char **)&source
, (const char *)sourceLimit
,
1975 &target
, targetLimit
,
1976 &offsets
, sourceIndex
,
1979 sourceIndex
=nextSourceIndex
+(int32_t)(source
-(const uint8_t *)pArgs
->source
);
1981 if(U_FAILURE(*pErrorCode
)) {
1982 /* not mappable or buffer overflow */
1988 /* set the converter state back into UConverter */
1989 cnv
->toUnicodeStatus
=offset
;
1991 cnv
->toULength
=byteIndex
;
1993 /* write back the updated pointers */
1994 pArgs
->source
=(const char *)source
;
1995 pArgs
->target
=target
;
1996 pArgs
->offsets
=offsets
;
2000 * This version of ucnv_MBCSGetNextUChar() is optimized for single-byte, single-state codepages.
2001 * We still need a conversion loop in case we find reserved action codes, which are to be ignored.
2004 ucnv_MBCSSingleGetNextUChar(UConverterToUnicodeArgs
*pArgs
,
2005 UErrorCode
*pErrorCode
) {
2007 const int32_t (*stateTable
)[256];
2008 const uint8_t *source
, *sourceLimit
;
2013 /* set up the local pointers */
2014 cnv
=pArgs
->converter
;
2015 source
=(const uint8_t *)pArgs
->source
;
2016 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
2017 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2018 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
2020 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
2023 /* conversion loop */
2024 while(source
<sourceLimit
) {
2025 entry
=stateTable
[0][*source
++];
2026 /* MBCS_ENTRY_IS_FINAL(entry) */
2028 /* write back the updated pointer early so that we can return directly */
2029 pArgs
->source
=(const char *)source
;
2031 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
2032 /* output BMP code point */
2033 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2037 * An if-else-if chain provides more reliable performance for
2038 * the most common cases compared to a switch.
2040 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2041 if( action
==MBCS_STATE_VALID_DIRECT_20
||
2042 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
2044 /* output supplementary code point */
2045 return (UChar32
)(MBCS_ENTRY_FINAL_VALUE(entry
)+0x10000);
2046 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2047 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
2048 /* output BMP code point */
2049 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2051 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2052 /* just fall through */
2053 } else if(action
==MBCS_STATE_ILLEGAL
) {
2054 /* callback(illegal) */
2055 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2057 /* reserved, must never occur */
2061 if(U_FAILURE(*pErrorCode
)) {
2062 /* callback(illegal) */
2064 } else /* unassigned sequence */ {
2065 /* defer to the generic implementation */
2066 pArgs
->source
=(const char *)source
-1;
2067 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
2071 /* no output because of empty input or only state changes */
2072 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
2077 * Version of _MBCSToUnicodeWithOffsets() optimized for single-character
2078 * conversion without offset handling.
2080 * When a character does not have a mapping to Unicode, then we return to the
2081 * generic ucnv_getNextUChar() code for extension/GB 18030 and error/callback
2083 * We also defer to the generic code in other complicated cases and have them
2084 * ultimately handled by _MBCSToUnicodeWithOffsets() itself.
2086 * All normal mappings and errors are handled here.
2089 ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs
*pArgs
,
2090 UErrorCode
*pErrorCode
) {
2092 const uint8_t *source
, *sourceLimit
, *lastSource
;
2094 const int32_t (*stateTable
)[256];
2095 const uint16_t *unicodeCodeUnits
;
2104 /* use optimized function if possible */
2105 cnv
=pArgs
->converter
;
2107 if(cnv
->preToULength
>0) {
2108 /* use the generic code in ucnv_getNextUChar() to continue with a partial match */
2109 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
2112 if(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SURROGATES
) {
2114 * Using the generic ucnv_getNextUChar() code lets us deal correctly
2115 * with the rare case of a codepage that maps single surrogates
2116 * without adding the complexity to this already complicated function here.
2118 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
2119 } else if(cnv
->sharedData
->mbcs
.countStates
==1) {
2120 return ucnv_MBCSSingleGetNextUChar(pArgs
, pErrorCode
);
2123 /* set up the local pointers */
2124 source
=lastSource
=(const uint8_t *)pArgs
->source
;
2125 sourceLimit
=(const uint8_t *)pArgs
->sourceLimit
;
2127 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2128 stateTable
=(const int32_t (*)[256])cnv
->sharedData
->mbcs
.swapLFNLStateTable
;
2130 stateTable
=cnv
->sharedData
->mbcs
.stateTable
;
2132 unicodeCodeUnits
=cnv
->sharedData
->mbcs
.unicodeCodeUnits
;
2134 /* get the converter state from UConverter */
2135 offset
=cnv
->toUnicodeStatus
;
2138 * if we are in the SBCS state for a DBCS-only converter,
2139 * then load the DBCS state from the MBCS data
2140 * (dbcsOnlyState==0 if it is not a DBCS-only converter)
2142 if((state
=(uint8_t)(cnv
->mode
))==0) {
2143 state
=cnv
->sharedData
->mbcs
.dbcsOnlyState
;
2146 /* conversion loop */
2148 while(source
<sourceLimit
) {
2149 entry
=stateTable
[state
][*source
++];
2150 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
2151 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
2152 offset
+=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
2154 /* optimization for 1/2-byte input and BMP output */
2155 if( source
<sourceLimit
&&
2156 MBCS_ENTRY_IS_FINAL(entry
=stateTable
[state
][*source
]) &&
2157 MBCS_ENTRY_FINAL_ACTION(entry
)==MBCS_STATE_VALID_16
&&
2158 (c
=unicodeCodeUnits
[offset
+MBCS_ENTRY_FINAL_VALUE_16(entry
)])<0xfffe
2161 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2162 /* output BMP code point */
2166 /* save the previous state for proper extension mapping with SI/SO-stateful converters */
2169 /* set the next state early so that we can reuse the entry variable */
2170 state
=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry
); /* typically 0 */
2173 * An if-else-if chain provides more reliable performance for
2174 * the most common cases compared to a switch.
2176 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2177 if(action
==MBCS_STATE_VALID_DIRECT_16
) {
2178 /* output BMP code point */
2179 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2181 } else if(action
==MBCS_STATE_VALID_16
) {
2182 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
2183 c
=unicodeCodeUnits
[offset
];
2185 /* output BMP code point */
2187 } else if(c
==0xfffe) {
2188 if(UCNV_TO_U_USE_FALLBACK(cnv
) && (c
=ucnv_MBCSGetFallback(&cnv
->sharedData
->mbcs
, offset
))!=0xfffe) {
2192 /* callback(illegal) */
2193 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2195 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
2196 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
2197 c
=unicodeCodeUnits
[offset
++];
2199 /* output BMP code point below 0xd800 */
2201 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? c
<=0xdfff : c
<=0xdbff) {
2202 /* output roundtrip or fallback supplementary code point */
2203 c
=((c
&0x3ff)<<10)+unicodeCodeUnits
[offset
]+(0x10000-0xdc00);
2205 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? (c
&0xfffe)==0xe000 : c
==0xe000) {
2206 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
2207 c
=unicodeCodeUnits
[offset
];
2209 } else if(c
==0xffff) {
2210 /* callback(illegal) */
2211 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2213 } else if(action
==MBCS_STATE_VALID_DIRECT_20
||
2214 (action
==MBCS_STATE_FALLBACK_DIRECT_20
&& UCNV_TO_U_USE_FALLBACK(cnv
))
2216 /* output supplementary code point */
2217 c
=(UChar32
)(MBCS_ENTRY_FINAL_VALUE(entry
)+0x10000);
2219 } else if(action
==MBCS_STATE_CHANGE_ONLY
) {
2221 * This serves as a state change without any output.
2222 * It is useful for reading simple stateful encodings,
2223 * for example using just Shift-In/Shift-Out codes.
2224 * The 21 unused bits may later be used for more sophisticated
2225 * state transitions.
2227 if(cnv
->sharedData
->mbcs
.dbcsOnlyState
!=0) {
2228 /* SI/SO are illegal for DBCS-only conversion */
2229 state
=(uint8_t)(cnv
->mode
); /* restore the previous state */
2231 /* callback(illegal) */
2232 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2234 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2235 if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
2236 /* output BMP code point */
2237 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2240 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2241 /* just fall through */
2242 } else if(action
==MBCS_STATE_ILLEGAL
) {
2243 /* callback(illegal) */
2244 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2246 /* reserved (must never occur), or only state change */
2252 /* end of action codes: prepare for a new character */
2255 if(U_FAILURE(*pErrorCode
)) {
2256 /* callback(illegal) */
2258 } else /* unassigned sequence */ {
2259 /* defer to the generic implementation */
2260 cnv
->toUnicodeStatus
=0;
2262 pArgs
->source
=(const char *)lastSource
;
2263 return UCNV_GET_NEXT_UCHAR_USE_TO_U
;
2269 if(U_SUCCESS(*pErrorCode
) && source
==sourceLimit
&& lastSource
<source
) {
2270 *pErrorCode
=U_TRUNCATED_CHAR_FOUND
;
2272 if(U_FAILURE(*pErrorCode
)) {
2273 /* incomplete character byte sequence */
2274 uint8_t *bytes
=cnv
->toUBytes
;
2275 cnv
->toULength
=(int8_t)(source
-lastSource
);
2277 *bytes
++=*lastSource
++;
2278 } while(lastSource
<source
);
2280 /* no output because of empty input or only state changes */
2281 *pErrorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
2286 /* set the converter state back into UConverter, ready for a new character */
2287 cnv
->toUnicodeStatus
=0;
2290 /* write back the updated pointer */
2291 pArgs
->source
=(const char *)source
;
2297 * Code disabled 2002dec09 (ICU 2.4) because it is not currently used in ICU. markus
2298 * Removal improves code coverage.
2301 * This version of ucnv_MBCSSimpleGetNextUChar() is optimized for single-byte, single-state codepages.
2302 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
2303 * It does not handle conversion extensions (_extToU()).
2306 ucnv_MBCSSingleSimpleGetNextUChar(UConverterSharedData
*sharedData
,
2307 uint8_t b
, UBool useFallback
) {
2311 entry
=sharedData
->mbcs
.stateTable
[0][b
];
2312 /* MBCS_ENTRY_IS_FINAL(entry) */
2314 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry
)) {
2315 /* output BMP code point */
2316 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2320 * An if-else-if chain provides more reliable performance for
2321 * the most common cases compared to a switch.
2323 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2324 if(action
==MBCS_STATE_VALID_DIRECT_20
) {
2325 /* output supplementary code point */
2326 return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
2327 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2328 if(!TO_U_USE_FALLBACK(useFallback
)) {
2331 /* output BMP code point */
2332 return (UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2333 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_20
) {
2334 if(!TO_U_USE_FALLBACK(useFallback
)) {
2337 /* output supplementary code point */
2338 return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
2339 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2341 } else if(action
==MBCS_STATE_ILLEGAL
) {
2344 /* reserved, must never occur */
2351 * This is a simple version of _MBCSGetNextUChar() that is used
2352 * by other converter implementations.
2353 * It only returns an "assigned" result if it consumes the entire input.
2354 * It does not use state from the converter, nor error codes.
2355 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
2356 * It handles conversion extensions but not GB 18030.
2361 * otherwise the Unicode code point
2364 ucnv_MBCSSimpleGetNextUChar(UConverterSharedData
*sharedData
,
2365 const char *source
, int32_t length
,
2366 UBool useFallback
) {
2367 const int32_t (*stateTable
)[256];
2368 const uint16_t *unicodeCodeUnits
;
2371 uint8_t state
, action
;
2377 /* no input at all: "illegal" */
2383 * Code disabled 2002dec09 (ICU 2.4) because it is not currently used in ICU. markus
2384 * TODO In future releases, verify that this function is never called for SBCS
2385 * conversions, i.e., that sharedData->mbcs.countStates==1 is still true.
2386 * Removal improves code coverage.
2388 /* use optimized function if possible */
2389 if(sharedData
->mbcs
.countStates
==1) {
2391 return ucnv_MBCSSingleSimpleGetNextUChar(sharedData
, (uint8_t)*source
, useFallback
);
2393 return 0xffff; /* illegal: more than a single byte for an SBCS converter */
2398 /* set up the local pointers */
2399 stateTable
=sharedData
->mbcs
.stateTable
;
2400 unicodeCodeUnits
=sharedData
->mbcs
.unicodeCodeUnits
;
2402 /* converter state */
2404 state
=sharedData
->mbcs
.dbcsOnlyState
;
2406 /* conversion loop */
2408 entry
=stateTable
[state
][(uint8_t)source
[i
++]];
2409 if(MBCS_ENTRY_IS_TRANSITION(entry
)) {
2410 state
=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry
);
2411 offset
+=MBCS_ENTRY_TRANSITION_OFFSET(entry
);
2414 return 0xffff; /* truncated character */
2418 * An if-else-if chain provides more reliable performance for
2419 * the most common cases compared to a switch.
2421 action
=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry
));
2422 if(action
==MBCS_STATE_VALID_16
) {
2423 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
2424 c
=unicodeCodeUnits
[offset
];
2427 } else if(UCNV_TO_U_USE_FALLBACK(cnv
)) {
2428 c
=ucnv_MBCSGetFallback(&sharedData
->mbcs
, offset
);
2429 /* else done with 0xfffe */
2432 } else if(action
==MBCS_STATE_VALID_DIRECT_16
) {
2433 /* output BMP code point */
2434 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2436 } else if(action
==MBCS_STATE_VALID_16_PAIR
) {
2437 offset
+=MBCS_ENTRY_FINAL_VALUE_16(entry
);
2438 c
=unicodeCodeUnits
[offset
++];
2440 /* output BMP code point below 0xd800 */
2441 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? c
<=0xdfff : c
<=0xdbff) {
2442 /* output roundtrip or fallback supplementary code point */
2443 c
=(UChar32
)(((c
&0x3ff)<<10)+unicodeCodeUnits
[offset
]+(0x10000-0xdc00));
2444 } else if(UCNV_TO_U_USE_FALLBACK(cnv
) ? (c
&0xfffe)==0xe000 : c
==0xe000) {
2445 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
2446 c
=unicodeCodeUnits
[offset
];
2447 } else if(c
==0xffff) {
2453 } else if(action
==MBCS_STATE_VALID_DIRECT_20
) {
2454 /* output supplementary code point */
2455 c
=0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
2457 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_16
) {
2458 if(!TO_U_USE_FALLBACK(useFallback
)) {
2462 /* output BMP code point */
2463 c
=(UChar
)MBCS_ENTRY_FINAL_VALUE_16(entry
);
2465 } else if(action
==MBCS_STATE_FALLBACK_DIRECT_20
) {
2466 if(!TO_U_USE_FALLBACK(useFallback
)) {
2470 /* output supplementary code point */
2471 c
=0x10000+MBCS_ENTRY_FINAL_VALUE(entry
);
2473 } else if(action
==MBCS_STATE_UNASSIGNED
) {
2479 * forbid MBCS_STATE_CHANGE_ONLY for this function,
2480 * and MBCS_STATE_ILLEGAL and reserved action codes
2487 /* illegal for this function: not all input consumed */
2492 /* try an extension mapping */
2493 const int32_t *cx
=sharedData
->mbcs
.extIndexes
;
2495 return ucnv_extSimpleMatchToU(cx
, source
, length
, useFallback
);
2502 /* MBCS-from-Unicode conversion functions ----------------------------------- */
2504 /* This version of ucnv_MBCSFromUnicodeWithOffsets() is optimized for double-byte codepages. */
2506 ucnv_MBCSDoubleFromUnicodeWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
2507 UErrorCode
*pErrorCode
) {
2509 const UChar
*source
, *sourceLimit
;
2511 int32_t targetCapacity
;
2514 const uint16_t *table
;
2515 const uint8_t *bytes
;
2519 int32_t sourceIndex
, nextSourceIndex
;
2521 uint32_t stage2Entry
;
2524 uint8_t unicodeMask
;
2526 /* use optimized function if possible */
2527 cnv
=pArgs
->converter
;
2528 unicodeMask
=cnv
->sharedData
->mbcs
.unicodeMask
;
2530 /* set up the local pointers */
2531 source
=pArgs
->source
;
2532 sourceLimit
=pArgs
->sourceLimit
;
2533 target
=(uint8_t *)pArgs
->target
;
2534 targetCapacity
=pArgs
->targetLimit
-pArgs
->target
;
2535 offsets
=pArgs
->offsets
;
2537 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
2538 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2539 bytes
=cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
2541 bytes
=cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
2544 /* get the converter state from UConverter */
2547 /* sourceIndex=-1 if the current character began in the previous buffer */
2548 sourceIndex
= c
==0 ? 0 : -1;
2551 /* conversion loop */
2552 if(c
!=0 && targetCapacity
>0) {
2556 while(source
<sourceLimit
) {
2558 * This following test is to see if available input would overflow the output.
2559 * It does not catch output of more than one byte that
2560 * overflows as a result of a multi-byte character or callback output
2561 * from the last source character.
2562 * Therefore, those situations also test for overflows and will
2563 * then break the loop, too.
2565 if(targetCapacity
>0) {
2567 * Get a correct Unicode code point:
2568 * a single UChar for a BMP code point or
2569 * a matched surrogate pair for a "supplementary code point".
2574 * This also tests if the codepage maps single surrogates.
2575 * If it does, then surrogates are not paired but mapped separately.
2576 * Note that in this case unmatched surrogates are not detected.
2578 if(UTF_IS_SURROGATE(c
) && !(unicodeMask
&UCNV_HAS_SURROGATES
)) {
2579 if(UTF_IS_SURROGATE_FIRST(c
)) {
2581 if(source
<sourceLimit
) {
2582 /* test the following code unit */
2583 UChar trail
=*source
;
2584 if(UTF_IS_SECOND_SURROGATE(trail
)) {
2587 c
=UTF16_GET_PAIR_VALUE(c
, trail
);
2588 if(!(unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
2589 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
2590 /* callback(unassigned) */
2593 /* convert this supplementary code point */
2594 /* exit this condition tree */
2596 /* this is an unmatched lead code unit (1st surrogate) */
2597 /* callback(illegal) */
2598 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2606 /* this is an unmatched trail code unit (2nd surrogate) */
2607 /* callback(illegal) */
2608 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2613 /* convert the Unicode code point in c into codepage bytes */
2614 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
2616 /* get the bytes and the length for the output */
2618 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
2625 /* is this code point assigned, or do we use fallbacks? */
2626 if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
) ||
2627 (UCNV_FROM_U_USE_FALLBACK(cnv
, c
) && value
!=0))
2630 * We allow a 0 byte output if the "assigned" bit is set for this entry.
2631 * There is no way with this data structure for fallback output
2632 * to be a zero byte.
2636 /* try an extension mapping */
2637 pArgs
->source
=source
;
2638 c
=_extFromU(cnv
, cnv
->sharedData
,
2639 c
, &source
, sourceLimit
,
2640 (char **)&target
, (char *)target
+targetCapacity
,
2641 &offsets
, sourceIndex
,
2644 nextSourceIndex
+=(int32_t)(source
-pArgs
->source
);
2646 if(U_FAILURE(*pErrorCode
)) {
2647 /* not mappable or buffer overflow */
2650 /* a mapping was written to the target, continue */
2652 /* recalculate the targetCapacity after an extension mapping */
2653 targetCapacity
=pArgs
->targetLimit
-(char *)target
;
2655 /* normal end of conversion: prepare for a new character */
2656 sourceIndex
=nextSourceIndex
;
2661 /* write the output character bytes from value and length */
2662 /* from the first if in the loop we know that targetCapacity>0 */
2664 /* this is easy because we know that there is enough space */
2665 *target
++=(uint8_t)value
;
2667 *offsets
++=sourceIndex
;
2670 } else /* length==2 */ {
2671 *target
++=(uint8_t)(value
>>8);
2672 if(2<=targetCapacity
) {
2673 *target
++=(uint8_t)value
;
2675 *offsets
++=sourceIndex
;
2676 *offsets
++=sourceIndex
;
2681 *offsets
++=sourceIndex
;
2683 cnv
->charErrorBuffer
[0]=(char)value
;
2684 cnv
->charErrorBufferLength
=1;
2686 /* target overflow */
2688 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2694 /* normal end of conversion: prepare for a new character */
2696 sourceIndex
=nextSourceIndex
;
2699 /* target is full */
2700 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2705 /* set the converter state back into UConverter */
2708 /* write back the updated pointers */
2709 pArgs
->source
=source
;
2710 pArgs
->target
=(char *)target
;
2711 pArgs
->offsets
=offsets
;
2714 /* This version of ucnv_MBCSFromUnicodeWithOffsets() is optimized for single-byte codepages. */
2716 ucnv_MBCSSingleFromUnicodeWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
2717 UErrorCode
*pErrorCode
) {
2719 const UChar
*source
, *sourceLimit
;
2721 int32_t targetCapacity
;
2724 const uint16_t *table
;
2725 const uint16_t *results
;
2729 int32_t sourceIndex
, nextSourceIndex
;
2731 uint16_t value
, minValue
;
2732 UBool hasSupplementary
;
2734 /* set up the local pointers */
2735 cnv
=pArgs
->converter
;
2736 source
=pArgs
->source
;
2737 sourceLimit
=pArgs
->sourceLimit
;
2738 target
=(uint8_t *)pArgs
->target
;
2739 targetCapacity
=pArgs
->targetLimit
-pArgs
->target
;
2740 offsets
=pArgs
->offsets
;
2742 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
2743 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2744 results
=(uint16_t *)cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
2746 results
=(uint16_t *)cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
2749 if(cnv
->useFallback
) {
2750 /* use all roundtrip and fallback results */
2753 /* use only roundtrips and fallbacks from private-use characters */
2756 hasSupplementary
=(UBool
)(cnv
->sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
);
2758 /* get the converter state from UConverter */
2761 /* sourceIndex=-1 if the current character began in the previous buffer */
2762 sourceIndex
= c
==0 ? 0 : -1;
2765 /* conversion loop */
2766 if(c
!=0 && targetCapacity
>0) {
2770 while(source
<sourceLimit
) {
2772 * This following test is to see if available input would overflow the output.
2773 * It does not catch output of more than one byte that
2774 * overflows as a result of a multi-byte character or callback output
2775 * from the last source character.
2776 * Therefore, those situations also test for overflows and will
2777 * then break the loop, too.
2779 if(targetCapacity
>0) {
2781 * Get a correct Unicode code point:
2782 * a single UChar for a BMP code point or
2783 * a matched surrogate pair for a "supplementary code point".
2787 if(UTF_IS_SURROGATE(c
)) {
2788 if(UTF_IS_SURROGATE_FIRST(c
)) {
2790 if(source
<sourceLimit
) {
2791 /* test the following code unit */
2792 UChar trail
=*source
;
2793 if(UTF_IS_SECOND_SURROGATE(trail
)) {
2796 c
=UTF16_GET_PAIR_VALUE(c
, trail
);
2797 if(!hasSupplementary
) {
2798 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
2799 /* callback(unassigned) */
2802 /* convert this supplementary code point */
2803 /* exit this condition tree */
2805 /* this is an unmatched lead code unit (1st surrogate) */
2806 /* callback(illegal) */
2807 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2815 /* this is an unmatched trail code unit (2nd surrogate) */
2816 /* callback(illegal) */
2817 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
2822 /* convert the Unicode code point in c into codepage bytes */
2823 value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
2825 /* is this code point assigned, or do we use fallbacks? */
2826 if(value
>=minValue
) {
2827 /* assigned, write the output character bytes from value and length */
2829 /* this is easy because we know that there is enough space */
2830 *target
++=(uint8_t)value
;
2832 *offsets
++=sourceIndex
;
2836 /* normal end of conversion: prepare for a new character */
2838 sourceIndex
=nextSourceIndex
;
2839 } else { /* unassigned */
2841 /* try an extension mapping */
2842 pArgs
->source
=source
;
2843 c
=_extFromU(cnv
, cnv
->sharedData
,
2844 c
, &source
, sourceLimit
,
2845 (char **)&target
, (char *)target
+targetCapacity
,
2846 &offsets
, sourceIndex
,
2849 nextSourceIndex
+=(int32_t)(source
-pArgs
->source
);
2851 if(U_FAILURE(*pErrorCode
)) {
2852 /* not mappable or buffer overflow */
2855 /* a mapping was written to the target, continue */
2857 /* recalculate the targetCapacity after an extension mapping */
2858 targetCapacity
=pArgs
->targetLimit
-(char *)target
;
2860 /* normal end of conversion: prepare for a new character */
2861 sourceIndex
=nextSourceIndex
;
2865 /* target is full */
2866 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
2871 /* set the converter state back into UConverter */
2874 /* write back the updated pointers */
2875 pArgs
->source
=source
;
2876 pArgs
->target
=(char *)target
;
2877 pArgs
->offsets
=offsets
;
2881 * This version of ucnv_MBCSFromUnicode() is optimized for single-byte codepages
2882 * that map only to and from the BMP.
2883 * In addition to single-byte/state optimizations, the offset calculations
2884 * become much easier.
2887 ucnv_MBCSSingleFromBMPWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
2888 UErrorCode
*pErrorCode
) {
2890 const UChar
*source
, *sourceLimit
, *lastSource
;
2892 int32_t targetCapacity
, length
;
2895 const uint16_t *table
;
2896 const uint16_t *results
;
2900 int32_t sourceIndex
;
2902 uint16_t value
, minValue
;
2904 /* set up the local pointers */
2905 cnv
=pArgs
->converter
;
2906 source
=pArgs
->source
;
2907 sourceLimit
=pArgs
->sourceLimit
;
2908 target
=(uint8_t *)pArgs
->target
;
2909 targetCapacity
=pArgs
->targetLimit
-pArgs
->target
;
2910 offsets
=pArgs
->offsets
;
2912 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
2913 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
2914 results
=(uint16_t *)cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
2916 results
=(uint16_t *)cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
2919 if(cnv
->useFallback
) {
2920 /* use all roundtrip and fallback results */
2923 /* use only roundtrips and fallbacks from private-use characters */
2927 /* get the converter state from UConverter */
2930 /* sourceIndex=-1 if the current character began in the previous buffer */
2931 sourceIndex
= c
==0 ? 0 : -1;
2935 * since the conversion here is 1:1 UChar:uint8_t, we need only one counter
2936 * for the minimum of the sourceLength and targetCapacity
2938 length
=sourceLimit
-source
;
2939 if(length
<targetCapacity
) {
2940 targetCapacity
=length
;
2943 /* conversion loop */
2944 if(c
!=0 && targetCapacity
>0) {
2948 #if MBCS_UNROLL_SINGLE_FROM_BMP
2949 /* unrolling makes it slower on Pentium III/Windows 2000?! */
2950 /* unroll the loop with the most common case */
2952 if(targetCapacity
>=4) {
2953 int32_t count
, loops
;
2954 uint16_t andedValues
;
2956 loops
=count
=targetCapacity
>>2;
2959 andedValues
=value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
2960 *target
++=(uint8_t)value
;
2962 andedValues
&=value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
2963 *target
++=(uint8_t)value
;
2965 andedValues
&=value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
2966 *target
++=(uint8_t)value
;
2968 andedValues
&=value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
2969 *target
++=(uint8_t)value
;
2971 /* were all 4 entries really valid? */
2972 if(andedValues
<minValue
) {
2973 /* no, return to the first of these 4 */
2980 targetCapacity
-=4*count
;
2983 lastSource
+=4*count
;
2985 *offsets
++=sourceIndex
++;
2986 *offsets
++=sourceIndex
++;
2987 *offsets
++=sourceIndex
++;
2988 *offsets
++=sourceIndex
++;
2997 while(targetCapacity
>0) {
2999 * Get a correct Unicode code point:
3000 * a single UChar for a BMP code point or
3001 * a matched surrogate pair for a "supplementary code point".
3005 * Do not immediately check for single surrogates:
3006 * Assume that they are unassigned and check for them in that case.
3007 * This speeds up the conversion of assigned characters.
3009 /* convert the Unicode code point in c into codepage bytes */
3010 value
=MBCS_SINGLE_RESULT_FROM_U(table
, results
, c
);
3012 /* is this code point assigned, or do we use fallbacks? */
3013 if(value
>=minValue
) {
3014 /* assigned, write the output character bytes from value and length */
3016 /* this is easy because we know that there is enough space */
3017 *target
++=(uint8_t)value
;
3020 /* normal end of conversion: prepare for a new character */
3023 } else if(!UTF_IS_SURROGATE(c
)) {
3024 /* normal, unassigned BMP character */
3025 } else if(UTF_IS_SURROGATE_FIRST(c
)) {
3027 if(source
<sourceLimit
) {
3028 /* test the following code unit */
3029 UChar trail
=*source
;
3030 if(UTF_IS_SECOND_SURROGATE(trail
)) {
3032 c
=UTF16_GET_PAIR_VALUE(c
, trail
);
3033 /* this codepage does not map supplementary code points */
3034 /* callback(unassigned) */
3036 /* this is an unmatched lead code unit (1st surrogate) */
3037 /* callback(illegal) */
3038 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3046 /* this is an unmatched trail code unit (2nd surrogate) */
3047 /* callback(illegal) */
3048 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3052 /* c does not have a mapping */
3054 /* get the number of code units for c to correctly advance sourceIndex */
3055 length
=U16_LENGTH(c
);
3057 /* set offsets since the start or the last extension */
3059 int32_t count
=(int32_t)(source
-lastSource
);
3061 /* do not set the offset for this character */
3065 *offsets
++=sourceIndex
++;
3068 /* offsets and sourceIndex are now set for the current character */
3071 /* try an extension mapping */
3073 c
=_extFromU(cnv
, cnv
->sharedData
,
3074 c
, &source
, sourceLimit
,
3075 (char **)&target
, (char *)target
+targetCapacity
,
3076 &offsets
, sourceIndex
,
3079 sourceIndex
+=length
+(int32_t)(source
-lastSource
);
3082 if(U_FAILURE(*pErrorCode
)) {
3083 /* not mappable or buffer overflow */
3086 /* a mapping was written to the target, continue */
3088 /* recalculate the targetCapacity after an extension mapping */
3089 targetCapacity
=pArgs
->targetLimit
-(char *)target
;
3090 length
=sourceLimit
-source
;
3091 if(length
<targetCapacity
) {
3092 targetCapacity
=length
;
3096 #if MBCS_UNROLL_SINGLE_FROM_BMP
3097 /* unrolling makes it slower on Pentium III/Windows 2000?! */
3102 if(U_SUCCESS(*pErrorCode
) && source
<sourceLimit
&& target
>=(uint8_t *)pArgs
->targetLimit
) {
3103 /* target is full */
3104 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
3107 /* set offsets since the start or the last callback */
3109 size_t count
=source
-lastSource
;
3111 *offsets
++=sourceIndex
++;
3116 /* set the converter state back into UConverter */
3119 /* write back the updated pointers */
3120 pArgs
->source
=source
;
3121 pArgs
->target
=(char *)target
;
3122 pArgs
->offsets
=offsets
;
3126 ucnv_MBCSFromUnicodeWithOffsets(UConverterFromUnicodeArgs
*pArgs
,
3127 UErrorCode
*pErrorCode
) {
3129 const UChar
*source
, *sourceLimit
;
3131 int32_t targetCapacity
;
3134 const uint16_t *table
;
3135 const uint8_t *p
, *bytes
;
3140 int32_t prevSourceIndex
, sourceIndex
, nextSourceIndex
;
3142 uint32_t stage2Entry
;
3144 int32_t length
, prevLength
;
3145 uint8_t unicodeMask
;
3147 cnv
=pArgs
->converter
;
3149 if(cnv
->preFromUFirstCP
>=0) {
3151 * pass sourceIndex=-1 because we continue from an earlier buffer
3152 * in the future, this may change with continuous offsets
3154 ucnv_extContinueMatchFromU(cnv
, pArgs
, -1, pErrorCode
);
3156 if(U_FAILURE(*pErrorCode
) || cnv
->preFromULength
<0) {
3161 /* use optimized function if possible */
3162 outputType
=cnv
->sharedData
->mbcs
.outputType
;
3163 unicodeMask
=cnv
->sharedData
->mbcs
.unicodeMask
;
3164 if(outputType
==MBCS_OUTPUT_1
&& !(unicodeMask
&UCNV_HAS_SURROGATES
)) {
3165 if(!(unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
3166 ucnv_MBCSSingleFromBMPWithOffsets(pArgs
, pErrorCode
);
3168 ucnv_MBCSSingleFromUnicodeWithOffsets(pArgs
, pErrorCode
);
3171 } else if(outputType
==MBCS_OUTPUT_2
) {
3172 ucnv_MBCSDoubleFromUnicodeWithOffsets(pArgs
, pErrorCode
);
3176 /* set up the local pointers */
3177 source
=pArgs
->source
;
3178 sourceLimit
=pArgs
->sourceLimit
;
3179 target
=(uint8_t *)pArgs
->target
;
3180 targetCapacity
=pArgs
->targetLimit
-pArgs
->target
;
3181 offsets
=pArgs
->offsets
;
3183 table
=cnv
->sharedData
->mbcs
.fromUnicodeTable
;
3185 if((cnv
->options
&UCNV_OPTION_SWAP_LFNL
)!=0) {
3186 bytes
=cnv
->sharedData
->mbcs
.swapLFNLFromUnicodeBytes
;
3188 bytes
=cnv
->sharedData
->mbcs
.fromUnicodeBytes
;
3191 /* get the converter state from UConverter */
3194 if(outputType
==MBCS_OUTPUT_2_SISO
) {
3195 prevLength
=cnv
->fromUnicodeStatus
;
3197 /* set the real value */
3201 /* prevent fromUnicodeStatus from being set to something non-0 */
3205 /* sourceIndex=-1 if the current character began in the previous buffer */
3207 sourceIndex
= c
==0 ? 0 : -1;
3210 /* conversion loop */
3212 * This is another piece of ugly code:
3213 * A goto into the loop if the converter state contains a first surrogate
3214 * from the previous function call.
3215 * It saves me to check in each loop iteration a check of if(c==0)
3216 * and duplicating the trail-surrogate-handling code in the else
3217 * branch of that check.
3218 * I could not find any other way to get around this other than
3219 * using a function call for the conversion and callback, which would
3220 * be even more inefficient.
3222 * Markus Scherer 2000-jul-19
3224 if(c
!=0 && targetCapacity
>0) {
3228 while(source
<sourceLimit
) {
3230 * This following test is to see if available input would overflow the output.
3231 * It does not catch output of more than one byte that
3232 * overflows as a result of a multi-byte character or callback output
3233 * from the last source character.
3234 * Therefore, those situations also test for overflows and will
3235 * then break the loop, too.
3237 if(targetCapacity
>0) {
3239 * Get a correct Unicode code point:
3240 * a single UChar for a BMP code point or
3241 * a matched surrogate pair for a "supplementary code point".
3246 * This also tests if the codepage maps single surrogates.
3247 * If it does, then surrogates are not paired but mapped separately.
3248 * Note that in this case unmatched surrogates are not detected.
3250 if(UTF_IS_SURROGATE(c
) && !(unicodeMask
&UCNV_HAS_SURROGATES
)) {
3251 if(UTF_IS_SURROGATE_FIRST(c
)) {
3253 if(source
<sourceLimit
) {
3254 /* test the following code unit */
3255 UChar trail
=*source
;
3256 if(UTF_IS_SECOND_SURROGATE(trail
)) {
3259 c
=UTF16_GET_PAIR_VALUE(c
, trail
);
3260 if(!(unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
3261 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
3262 cnv
->fromUnicodeStatus
=prevLength
; /* save the old state */
3263 /* callback(unassigned) */
3266 /* convert this supplementary code point */
3267 /* exit this condition tree */
3269 /* this is an unmatched lead code unit (1st surrogate) */
3270 /* callback(illegal) */
3271 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3279 /* this is an unmatched trail code unit (2nd surrogate) */
3280 /* callback(illegal) */
3281 *pErrorCode
=U_ILLEGAL_CHAR_FOUND
;
3286 /* convert the Unicode code point in c into codepage bytes */
3289 * The basic lookup is a triple-stage compact array (trie) lookup.
3290 * For details see the beginning of this file.
3292 * Single-byte codepages are handled with a different data structure
3293 * by _MBCSSingle... functions.
3295 * The result consists of a 32-bit value from stage 2 and
3296 * a pointer to as many bytes as are stored per character.
3297 * The pointer points to the character's bytes in stage 3.
3298 * Bits 15..0 of the stage 2 entry contain the stage 3 index
3299 * for that pointer, while bits 31..16 are flags for which of
3300 * the 16 characters in the block are roundtrip-assigned.
3302 * For 2-byte and 4-byte codepages, the bytes are stored as uint16_t
3303 * respectively as uint32_t, in the platform encoding.
3304 * For 3-byte codepages, the bytes are always stored in big-endian order.
3306 * For EUC encodings that use only either 0x8e or 0x8f as the first
3307 * byte of their longest byte sequences, the first two bytes in
3308 * this third stage indicate with their 7th bits whether these bytes
3309 * are to be written directly or actually need to be preceeded by
3310 * one of the two Single-Shift codes. With this, the third stage
3311 * stores one byte fewer per character than the actual maximum length of
3312 * EUC byte sequences.
3314 * Other than that, leading zero bytes are removed and the other
3315 * bytes output. A single zero byte may be output if the "assigned"
3316 * bit in stage 2 was on.
3317 * The data structure does not support zero byte output as a fallback,
3318 * and also does not allow output of leading zeros.
3320 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
3322 /* get the bytes and the length for the output */
3323 switch(outputType
) {
3325 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
3332 case MBCS_OUTPUT_2_SISO
:
3333 /* 1/2-byte stateful with Shift-In/Shift-Out */
3335 * Save the old state in the converter object
3336 * right here, then change the local prevLength state variable if necessary.
3337 * Then, if this character turns out to be unassigned or a fallback that
3338 * is not taken, the callback code must not save the new state in the converter
3339 * because the new state is for a character that is not output.
3340 * However, the callback must still restore the state from the converter
3341 * in case the callback function changed it for its output.
3343 cnv
->fromUnicodeStatus
=prevLength
; /* save the old state */
3344 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
3346 if(value
==0 && MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
)==0) {
3347 /* no mapping, leave value==0 */
3349 } else if(prevLength
<=1) {
3352 /* change from double-byte mode to single-byte */
3353 value
|=(uint32_t)UCNV_SI
<<8;
3361 /* change from single-byte mode to double-byte */
3362 value
|=(uint32_t)UCNV_SO
<<16;
3368 case MBCS_OUTPUT_DBCS_ONLY
:
3369 /* table with single-byte results, but only DBCS mappings used */
3370 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
3372 /* no mapping or SBCS result, not taken for DBCS-only */
3373 value
=stage2Entry
=0; /* stage2Entry=0 to reset roundtrip flags */
3380 p
=MBCS_POINTER_3_FROM_STAGE_2(bytes
, stage2Entry
, c
);
3381 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
3384 } else if(value
<=0xffff) {
3391 value
=MBCS_VALUE_4_FROM_STAGE_2(bytes
, stage2Entry
, c
);
3394 } else if(value
<=0xffff) {
3396 } else if(value
<=0xffffff) {
3402 case MBCS_OUTPUT_3_EUC
:
3403 value
=MBCS_VALUE_2_FROM_STAGE_2(bytes
, stage2Entry
, c
);
3404 /* EUC 16-bit fixed-length representation */
3407 } else if((value
&0x8000)==0) {
3410 } else if((value
&0x80)==0) {
3417 case MBCS_OUTPUT_4_EUC
:
3418 p
=MBCS_POINTER_3_FROM_STAGE_2(bytes
, stage2Entry
, c
);
3419 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
3420 /* EUC 16-bit fixed-length representation applied to the first two bytes */
3423 } else if(value
<=0xffff) {
3425 } else if((value
&0x800000)==0) {
3428 } else if((value
&0x8000)==0) {
3436 /* must not occur */
3438 * To avoid compiler warnings that value & length may be
3439 * used without having been initialized, we set them here.
3440 * In reality, this is unreachable code.
3441 * Not having a default branch also causes warnings with
3444 value
=stage2Entry
=0; /* stage2Entry=0 to reset roundtrip flags */
3449 /* is this code point assigned, or do we use fallbacks? */
3450 if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
)!=0 ||
3451 (UCNV_FROM_U_USE_FALLBACK(cnv
, c
) && value
!=0))
3454 * We allow a 0 byte output if the "assigned" bit is set for this entry.
3455 * There is no way with this data structure for fallback output
3456 * to be a zero byte.
3460 /* try an extension mapping */
3461 pArgs
->source
=source
;
3462 c
=_extFromU(cnv
, cnv
->sharedData
,
3463 c
, &source
, sourceLimit
,
3464 (char **)&target
, (char *)target
+targetCapacity
,
3465 &offsets
, sourceIndex
,
3468 nextSourceIndex
+=(int32_t)(source
-pArgs
->source
);
3469 prevLength
=cnv
->fromUnicodeStatus
; /* restore SISO state */
3471 if(U_FAILURE(*pErrorCode
)) {
3472 /* not mappable or buffer overflow */
3475 /* a mapping was written to the target, continue */
3477 /* recalculate the targetCapacity after an extension mapping */
3478 targetCapacity
=pArgs
->targetLimit
-(char *)target
;
3480 /* normal end of conversion: prepare for a new character */
3482 prevSourceIndex
=sourceIndex
;
3483 sourceIndex
=nextSourceIndex
;
3489 /* write the output character bytes from value and length */
3490 /* from the first if in the loop we know that targetCapacity>0 */
3491 if(length
<=targetCapacity
) {
3494 /* each branch falls through to the next one */
3496 *target
++=(uint8_t)(value
>>24);
3498 *target
++=(uint8_t)(value
>>16);
3500 *target
++=(uint8_t)(value
>>8);
3502 *target
++=(uint8_t)value
;
3504 /* will never occur */
3509 /* each branch falls through to the next one */
3511 *target
++=(uint8_t)(value
>>24);
3512 *offsets
++=sourceIndex
;
3514 *target
++=(uint8_t)(value
>>16);
3515 *offsets
++=sourceIndex
;
3517 *target
++=(uint8_t)(value
>>8);
3518 *offsets
++=sourceIndex
;
3520 *target
++=(uint8_t)value
;
3521 *offsets
++=sourceIndex
;
3523 /* will never occur */
3527 targetCapacity
-=length
;
3529 uint8_t *charErrorBuffer
;
3532 * We actually do this backwards here:
3533 * In order to save an intermediate variable, we output
3534 * first to the overflow buffer what does not fit into the
3537 /* we know that 1<=targetCapacity<length<=4 */
3538 length
-=targetCapacity
;
3539 charErrorBuffer
=(uint8_t *)cnv
->charErrorBuffer
;
3541 /* each branch falls through to the next one */
3543 *charErrorBuffer
++=(uint8_t)(value
>>16);
3545 *charErrorBuffer
++=(uint8_t)(value
>>8);
3547 *charErrorBuffer
=(uint8_t)value
;
3549 /* will never occur */
3552 cnv
->charErrorBufferLength
=(int8_t)length
;
3554 /* now output what fits into the regular target */
3555 value
>>=8*length
; /* length was reduced by targetCapacity */
3556 switch(targetCapacity
) {
3557 /* each branch falls through to the next one */
3559 *target
++=(uint8_t)(value
>>16);
3561 *offsets
++=sourceIndex
;
3564 *target
++=(uint8_t)(value
>>8);
3566 *offsets
++=sourceIndex
;
3569 *target
++=(uint8_t)value
;
3571 *offsets
++=sourceIndex
;
3574 /* will never occur */
3578 /* target overflow */
3580 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
3585 /* normal end of conversion: prepare for a new character */
3588 prevSourceIndex
=sourceIndex
;
3589 sourceIndex
=nextSourceIndex
;
3593 /* target is full */
3594 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
3600 * the end of the input stream and detection of truncated input
3601 * are handled by the framework, but for EBCDIC_STATEFUL conversion
3602 * we need to emit an SI at the very end
3606 * EBCDIC_STATEFUL in DBCS mode
3607 * end of input and no truncated input
3609 if( U_SUCCESS(*pErrorCode
) &&
3610 outputType
==MBCS_OUTPUT_2_SISO
&& prevLength
==2 &&
3611 pArgs
->flush
&& source
>=sourceLimit
&& c
==0
3613 /* EBCDIC_STATEFUL ending with DBCS: emit an SI to return the output stream to SBCS */
3614 if(targetCapacity
>0) {
3615 *target
++=(uint8_t)UCNV_SI
;
3617 /* set the last source character's index (sourceIndex points at sourceLimit now) */
3618 *offsets
++=prevSourceIndex
;
3621 /* target is full */
3622 cnv
->charErrorBuffer
[0]=(char)UCNV_SI
;
3623 cnv
->charErrorBufferLength
=1;
3624 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
3626 prevLength
=1; /* we switched into SBCS */
3629 /* set the converter state back into UConverter */
3631 cnv
->fromUnicodeStatus
=prevLength
;
3633 /* write back the updated pointers */
3634 pArgs
->source
=source
;
3635 pArgs
->target
=(char *)target
;
3636 pArgs
->offsets
=offsets
;
3640 * This is another simple conversion function for internal use by other
3641 * conversion implementations.
3642 * It does not use the converter state nor call callbacks.
3643 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
3644 * It handles conversion extensions but not GB 18030.
3646 * It converts one single Unicode code point into codepage bytes, encoded
3647 * as one 32-bit value. The function returns the number of bytes in *pValue:
3648 * 1..4 the number of bytes in *pValue
3649 * 0 unassigned (*pValue undefined)
3650 * -1 illegal (currently not used, *pValue undefined)
3652 * *pValue will contain the resulting bytes with the last byte in bits 7..0,
3653 * the second to last byte in bits 15..8, etc.
3654 * Currently, the function assumes but does not check that 0<=c<=0x10ffff.
3657 ucnv_MBCSFromUChar32(UConverterSharedData
*sharedData
,
3658 UChar32 c
, uint32_t *pValue
,
3659 UBool useFallback
) {
3661 const uint16_t *table
;
3663 /* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */
3666 uint32_t stage2Entry
;
3670 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
3671 if(c
<=0xffff || (sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
3672 table
=sharedData
->mbcs
.fromUnicodeTable
;
3674 /* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
3675 if(sharedData
->mbcs
.outputType
==MBCS_OUTPUT_1
) {
3676 value
=MBCS_SINGLE_RESULT_FROM_U(table
, (uint16_t *)sharedData
->mbcs
.fromUnicodeBytes
, c
);
3677 /* is this code point assigned, or do we use fallbacks? */
3678 if(useFallback
? value
>=0x800 : value
>=0xc00) {
3682 } else /* outputType!=MBCS_OUTPUT_1 */ {
3683 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
3685 /* get the bytes and the length for the output */
3686 switch(sharedData
->mbcs
.outputType
) {
3688 value
=MBCS_VALUE_2_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
3696 /* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */
3697 case MBCS_OUTPUT_DBCS_ONLY
:
3698 /* table with single-byte results, but only DBCS mappings used */
3699 value
=MBCS_VALUE_2_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
3701 /* no mapping or SBCS result, not taken for DBCS-only */
3702 value
=stage2Entry
=0; /* stage2Entry=0 to reset roundtrip flags */
3709 p
=MBCS_POINTER_3_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
3710 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
3713 } else if(value
<=0xffff) {
3720 value
=MBCS_VALUE_4_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
3723 } else if(value
<=0xffff) {
3725 } else if(value
<=0xffffff) {
3731 case MBCS_OUTPUT_3_EUC
:
3732 value
=MBCS_VALUE_2_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
3733 /* EUC 16-bit fixed-length representation */
3736 } else if((value
&0x8000)==0) {
3739 } else if((value
&0x80)==0) {
3746 case MBCS_OUTPUT_4_EUC
:
3747 p
=MBCS_POINTER_3_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
3748 value
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
3749 /* EUC 16-bit fixed-length representation applied to the first two bytes */
3752 } else if(value
<=0xffff) {
3754 } else if((value
&0x800000)==0) {
3757 } else if((value
&0x8000)==0) {
3766 /* must not occur */
3770 /* is this code point assigned, or do we use fallbacks? */
3771 if( MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry
, c
) ||
3772 (FROM_U_USE_FALLBACK(useFallback
, c
) && value
!=0)
3775 * We allow a 0 byte output if the "assigned" bit is set for this entry.
3776 * There is no way with this data structure for fallback output
3777 * to be a zero byte.
3786 cx
=sharedData
->mbcs
.extIndexes
;
3788 return ucnv_extSimpleMatchFromU(cx
, c
, pValue
, useFallback
);
3798 * This function has been moved to ucnv2022.c for inlining.
3799 * This implementation is here only for documentation purposes
3803 * This version of ucnv_MBCSFromUChar32() is optimized for single-byte codepages.
3804 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
3805 * It does not handle conversion extensions (_extFromU()).
3807 * It returns the codepage byte for the code point, or -1 if it is unassigned.
3810 ucnv_MBCSSingleFromUChar32(UConverterSharedData
*sharedData
,
3812 UBool useFallback
) {
3813 const uint16_t *table
;
3816 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
3817 if(c
>=0x10000 && !(sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
3821 /* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
3822 table
=sharedData
->mbcs
.fromUnicodeTable
;
3824 /* get the byte for the output */
3825 value
=MBCS_SINGLE_RESULT_FROM_U(table
, (uint16_t *)sharedData
->mbcs
.fromUnicodeBytes
, c
);
3826 /* is this code point assigned, or do we use fallbacks? */
3827 if(useFallback
? value
>=0x800 : value
>=0xc00) {
3835 /* miscellaneous ------------------------------------------------------------ */
3838 ucnv_MBCSGetStarters(const UConverter
* cnv
,
3839 UBool starters
[256],
3840 UErrorCode
*pErrorCode
) {
3841 const int32_t *state0
;
3844 state0
=cnv
->sharedData
->mbcs
.stateTable
[cnv
->sharedData
->mbcs
.dbcsOnlyState
];
3845 for(i
=0; i
<256; ++i
) {
3846 /* all bytes that cause a state transition from state 0 are lead bytes */
3847 starters
[i
]= (UBool
)MBCS_ENTRY_IS_TRANSITION(state0
[i
]);
3852 * This is an internal function that allows other converter implementations
3853 * to check whether a byte is a lead byte.
3856 ucnv_MBCSIsLeadByte(UConverterSharedData
*sharedData
, char byte
) {
3857 return (UBool
)MBCS_ENTRY_IS_TRANSITION(sharedData
->mbcs
.stateTable
[0][(uint8_t)byte
]);
3861 ucnv_MBCSWriteSub(UConverterFromUnicodeArgs
*pArgs
,
3862 int32_t offsetIndex
,
3863 UErrorCode
*pErrorCode
) {
3864 UConverter
*cnv
=pArgs
->converter
;
3869 /* first, select between subChar and subChar1 */
3870 if( cnv
->subChar1
!=0 &&
3871 (cnv
->sharedData
->mbcs
.extIndexes
!=NULL
?
3873 (cnv
->invalidUCharBuffer
[0]<=0xff))
3875 /* select subChar1 if it is set (not 0) and the unmappable Unicode code point is up to U+00ff (IBM MBCS behavior) */
3876 subchar
=(char *)&cnv
->subChar1
;
3879 /* select subChar in all other cases */
3880 subchar
=(char *)cnv
->subChar
;
3881 length
=cnv
->subCharLen
;
3884 /* reset the selector for the next code point */
3885 cnv
->useSubChar1
=FALSE
;
3887 switch(cnv
->sharedData
->mbcs
.outputType
) {
3888 case MBCS_OUTPUT_2_SISO
:
3891 /* fromUnicodeStatus contains prevLength */
3894 if(cnv
->fromUnicodeStatus
==2) {
3895 /* DBCS mode and SBCS sub char: change to SBCS */
3896 cnv
->fromUnicodeStatus
=1;
3902 if(cnv
->fromUnicodeStatus
<=1) {
3903 /* SBCS mode and DBCS sub char: change to DBCS */
3904 cnv
->fromUnicodeStatus
=2;
3911 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
3914 ucnv_cbFromUWriteBytes(pArgs
,
3915 buffer
, (int32_t)(p
-buffer
),
3916 offsetIndex
, pErrorCode
);
3919 ucnv_cbFromUWriteBytes(pArgs
,
3921 offsetIndex
, pErrorCode
);
3926 U_CFUNC UConverterType
3927 ucnv_MBCSGetType(const UConverter
* converter
) {
3928 /* SBCS, DBCS, and EBCDIC_STATEFUL are replaced by MBCS, but here we cheat a little */
3929 if(converter
->sharedData
->mbcs
.countStates
==1) {
3930 return (UConverterType
)UCNV_SBCS
;
3931 } else if((converter
->sharedData
->mbcs
.outputType
&0xff)==MBCS_OUTPUT_2_SISO
) {
3932 return (UConverterType
)UCNV_EBCDIC_STATEFUL
;
3933 } else if(converter
->sharedData
->staticData
->minBytesPerChar
==2 && converter
->sharedData
->staticData
->maxBytesPerChar
==2) {
3934 return (UConverterType
)UCNV_DBCS
;
3936 return (UConverterType
)UCNV_MBCS
;
3939 static const UConverterImpl _MBCSImpl
={
3949 ucnv_MBCSToUnicodeWithOffsets
,
3950 ucnv_MBCSToUnicodeWithOffsets
,
3951 ucnv_MBCSFromUnicodeWithOffsets
,
3952 ucnv_MBCSFromUnicodeWithOffsets
,
3953 ucnv_MBCSGetNextUChar
,
3955 ucnv_MBCSGetStarters
,
3959 ucnv_MBCSGetUnicodeSet
3963 /* Static data is in tools/makeconv/ucnvstat.c for data-based
3964 * converters. Be sure to update it as well.
3967 const UConverterSharedData _MBCSData
={
3968 sizeof(UConverterSharedData
), 1,
3969 NULL
, NULL
, NULL
, FALSE
, &_MBCSImpl
,
3973 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */