]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/ucnvmbcs.c
ICU-491.11.2.tar.gz
[apple/icu.git] / icuSources / common / ucnvmbcs.c
1 /*
2 ******************************************************************************
3 *
4 * Copyright (C) 2000-2012, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 ******************************************************************************
8 * file name: ucnvmbcs.c
9 * encoding: US-ASCII
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 2000jul03
14 * created by: Markus W. Scherer
15 *
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)
26 *
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
31 *
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.
35 *
36 * Change history:
37 *
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
41 */
42
43 #include "unicode/utypes.h"
44
45 #if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION
46
47 #include "unicode/ucnv.h"
48 #include "unicode/ucnv_cb.h"
49 #include "unicode/udata.h"
50 #include "unicode/uset.h"
51 #include "unicode/utf8.h"
52 #include "unicode/utf16.h"
53 #include "ucnv_bld.h"
54 #include "ucnvmbcs.h"
55 #include "ucnv_ext.h"
56 #include "ucnv_cnv.h"
57 #include "umutex.h"
58 #include "cmemory.h"
59 #include "cstring.h"
60
61 /* control optimizations according to the platform */
62 #define MBCS_UNROLL_SINGLE_TO_BMP 1
63 #define MBCS_UNROLL_SINGLE_FROM_BMP 0
64
65 /*
66 * _MBCSHeader versions 5.3 & 4.3
67 * (Note that the _MBCSHeader version is in addition to the converter formatVersion.)
68 *
69 * This version is optional. Version 5 is used for incompatible data format changes.
70 * makeconv will continue to generate version 4 files if possible.
71 *
72 * Changes from version 4:
73 *
74 * The main difference is an additional _MBCSHeader field with
75 * - the length (number of uint32_t) of the _MBCSHeader
76 * - flags for further incompatible data format changes
77 * - flags for further, backward compatible data format changes
78 *
79 * The MBCS_OPT_FROM_U flag indicates that most of the fromUnicode data is omitted from
80 * the file and needs to be reconstituted at load time.
81 * This requires a utf8Friendly format with an additional mbcsIndex table for fast
82 * (and UTF-8-friendly) fromUnicode conversion for Unicode code points up to maxFastUChar.
83 * (For details about these structures see below, and see ucnvmbcs.h.)
84 *
85 * utf8Friendly also implies that the fromUnicode mappings are stored in ascending order
86 * of the Unicode code points. (This requires that the .ucm file has the |0 etc.
87 * precision markers for all mappings.)
88 *
89 * All fallbacks have been moved to the extension table, leaving only roundtrips in the
90 * omitted data that can be reconstituted from the toUnicode data.
91 *
92 * Of the stage 2 table, the part corresponding to maxFastUChar and below is omitted.
93 * With only roundtrip mappings in the base fromUnicode data, this part is fully
94 * redundant with the mbcsIndex and will be reconstituted from that (also using the
95 * stage 1 table which contains the information about how stage 2 was compacted).
96 *
97 * The rest of the stage 2 table, the part for code points above maxFastUChar,
98 * is stored in the file and will be appended to the reconstituted part.
99 *
100 * The entire fromUBytes array is omitted from the file and will be reconstitued.
101 * This is done by enumerating all toUnicode roundtrip mappings, performing
102 * each mapping (using the stage 1 and reconstituted stage 2 tables) and
103 * writing instead of reading the byte values.
104 *
105 * _MBCSHeader version 4.3
106 *
107 * Change from version 4.2:
108 * - Optional utf8Friendly data structures, with 64-entry stage 3 block
109 * allocation for parts of the BMP, and an additional mbcsIndex in non-SBCS
110 * files which can be used instead of stages 1 & 2.
111 * Faster lookups for roundtrips from most commonly used characters,
112 * and lookups from UTF-8 byte sequences with a natural bit distribution.
113 * See ucnvmbcs.h for more details.
114 *
115 * Change from version 4.1:
116 * - Added an optional extension table structure at the end of the .cnv file.
117 * It is present if the upper bits of the header flags field contains a non-zero
118 * byte offset to it.
119 * Files that contain only a conversion table and no base table
120 * use the special outputType MBCS_OUTPUT_EXT_ONLY.
121 * These contain the base table name between the MBCS header and the extension
122 * data.
123 *
124 * Change from version 4.0:
125 * - Replace header.reserved with header.fromUBytesLength so that all
126 * fields in the data have length.
127 *
128 * Changes from version 3 (for performance improvements):
129 * - new bit distribution for state table entries
130 * - reordered action codes
131 * - new data structure for single-byte fromUnicode
132 * + stage 2 only contains indexes
133 * + stage 3 stores 16 bits per character with classification bits 15..8
134 * - no multiplier for stage 1 entries
135 * - stage 2 for non-single-byte codepages contains the index and the flags in
136 * one 32-bit value
137 * - 2-byte and 4-byte fromUnicode results are stored directly as 16/32-bit integers
138 *
139 * For more details about old versions of the MBCS data structure, see
140 * the corresponding versions of this file.
141 *
142 * Converting stateless codepage data ---------------------------------------***
143 * (or codepage data with simple states) to Unicode.
144 *
145 * Data structure and algorithm for converting from complex legacy codepages
146 * to Unicode. (Designed before 2000-may-22.)
147 *
148 * The basic idea is that the structure of legacy codepages can be described
149 * with state tables.
150 * When reading a byte stream, each input byte causes a state transition.
151 * Some transitions result in the output of a code point, some result in
152 * "unassigned" or "illegal" output.
153 * This is used here for character conversion.
154 *
155 * The data structure begins with a state table consisting of a row
156 * per state, with 256 entries (columns) per row for each possible input
157 * byte value.
158 * Each entry is 32 bits wide, with two formats distinguished by
159 * the sign bit (bit 31):
160 *
161 * One format for transitional entries (bit 31 not set) for non-final bytes, and
162 * one format for final entries (bit 31 set).
163 * Both formats contain the number of the next state in the same bit
164 * positions.
165 * State 0 is the initial state.
166 *
167 * Most of the time, the offset values of subsequent states are added
168 * up to a scalar value. This value will eventually be the index of
169 * the Unicode code point in a table that follows the state table.
170 * The effect is that the code points for final state table rows
171 * are contiguous. The code points of final state rows follow each other
172 * in the order of the references to those final states by previous
173 * states, etc.
174 *
175 * For some terminal states, the offset is itself the output Unicode
176 * code point (16 bits for a BMP code point or 20 bits for a supplementary
177 * code point (stored as code point minus 0x10000 so that 20 bits are enough).
178 * For others, the code point in the Unicode table is stored with either
179 * one or two code units: one for BMP code points, two for a pair of
180 * surrogates.
181 * All code points for a final state entry take up the same number of code
182 * units, regardless of whether they all actually _use_ the same number
183 * of code units. This is necessary for simple array access.
184 *
185 * An additional feature comes in with what in ICU is called "fallback"
186 * mappings:
187 *
188 * In addition to round-trippable, precise, 1:1 mappings, there are often
189 * mappings defined between similar, though not the same, characters.
190 * Typically, such mappings occur only in fromUnicode mapping tables because
191 * Unicode has a superset repertoire of most other codepages. However, it
192 * is possible to provide such mappings in the toUnicode tables, too.
193 * In this case, the fallback mappings are partly integrated into the
194 * general state tables because the structure of the encoding includes their
195 * byte sequences.
196 * For final entries in an initial state, fallback mappings are stored in
197 * the entry itself like with roundtrip mappings.
198 * For other final entries, they are stored in the code units table if
199 * the entry is for a pair of code units.
200 * For single-unit results in the code units table, there is no space to
201 * alternatively hold a fallback mapping; in this case, the code unit
202 * is stored as U+fffe (unassigned), and the fallback mapping needs to
203 * be looked up by the scalar offset value in a separate table.
204 *
205 * "Unassigned" state entries really mean "structurally unassigned",
206 * i.e., such a byte sequence will never have a mapping result.
207 *
208 * The interpretation of the bits in each entry is as follows:
209 *
210 * Bit 31 not set, not a terminal entry ("transitional"):
211 * 30..24 next state
212 * 23..0 offset delta, to be added up
213 *
214 * Bit 31 set, terminal ("final") entry:
215 * 30..24 next state (regardless of action code)
216 * 23..20 action code:
217 * action codes 0 and 1 result in precise-mapping Unicode code points
218 * 0 valid byte sequence
219 * 19..16 not used, 0
220 * 15..0 16-bit Unicode BMP code point
221 * never U+fffe or U+ffff
222 * 1 valid byte sequence
223 * 19..0 20-bit Unicode supplementary code point
224 * never U+fffe or U+ffff
225 *
226 * action codes 2 and 3 result in fallback (unidirectional-mapping) Unicode code points
227 * 2 valid byte sequence (fallback)
228 * 19..16 not used, 0
229 * 15..0 16-bit Unicode BMP code point as fallback result
230 * 3 valid byte sequence (fallback)
231 * 19..0 20-bit Unicode supplementary code point as fallback result
232 *
233 * action codes 4 and 5 may result in roundtrip/fallback/unassigned/illegal results
234 * depending on the code units they result in
235 * 4 valid byte sequence
236 * 19..9 not used, 0
237 * 8..0 final offset delta
238 * pointing to one 16-bit code unit which may be
239 * fffe unassigned -- look for a fallback for this offset
240 * ffff illegal
241 * 5 valid byte sequence
242 * 19..9 not used, 0
243 * 8..0 final offset delta
244 * pointing to two 16-bit code units
245 * (typically UTF-16 surrogates)
246 * the result depends on the first code unit as follows:
247 * 0000..d7ff roundtrip BMP code point (1st alone)
248 * d800..dbff roundtrip surrogate pair (1st, 2nd)
249 * dc00..dfff fallback surrogate pair (1st-400, 2nd)
250 * e000 roundtrip BMP code point (2nd alone)
251 * e001 fallback BMP code point (2nd alone)
252 * fffe unassigned
253 * ffff illegal
254 * (the final offset deltas are at most 255 * 2,
255 * times 2 because of storing code unit pairs)
256 *
257 * 6 unassigned byte sequence
258 * 19..16 not used, 0
259 * 15..0 16-bit Unicode BMP code point U+fffe (new with version 2)
260 * this does not contain a final offset delta because the main
261 * purpose of this action code is to save scalar offset values;
262 * therefore, fallback values cannot be assigned to byte
263 * sequences that result in this action code
264 * 7 illegal byte sequence
265 * 19..16 not used, 0
266 * 15..0 16-bit Unicode BMP code point U+ffff (new with version 2)
267 * 8 state change only
268 * 19..0 not used, 0
269 * useful for state changes in simple stateful encodings,
270 * at Shift-In/Shift-Out codes
271 *
272 *
273 * 9..15 reserved for future use
274 * current implementations will only perform a state change
275 * and ignore bits 19..0
276 *
277 * An encoding with contiguous ranges of unassigned byte sequences, like
278 * Shift-JIS and especially EUC-TW, can be stored efficiently by having
279 * at least two states for the trail bytes:
280 * One trail byte state that results in code points, and one that only
281 * has "unassigned" and "illegal" terminal states.
282 *
283 * Note: partly by accident, this data structure supports simple stateful
284 * encodings without any additional logic.
285 * Currently, only simple Shift-In/Shift-Out schemes are handled with
286 * appropriate state tables (especially EBCDIC_STATEFUL!).
287 *
288 * MBCS version 2 added:
289 * unassigned and illegal action codes have U+fffe and U+ffff
290 * instead of unused bits; this is useful for _MBCS_SINGLE_SIMPLE_GET_NEXT_BMP()
291 *
292 * Converting from Unicode to codepage bytes --------------------------------***
293 *
294 * The conversion data structure for fromUnicode is designed for the known
295 * structure of Unicode. It maps from 21-bit code points (0..0x10ffff) to
296 * a sequence of 1..4 bytes, in addition to a flag that indicates if there is
297 * a roundtrip mapping.
298 *
299 * The lookup is done with a 3-stage trie, using 11/6/4 bits for stage 1/2/3
300 * like in the character properties table.
301 * The beginning of the trie is at offsetFromUTable, the beginning of stage 3
302 * with the resulting bytes is at offsetFromUBytes.
303 *
304 * Beginning with version 4, single-byte codepages have a significantly different
305 * trie compared to other codepages.
306 * In all cases, the entry in stage 1 is directly the index of the block of
307 * 64 entries in stage 2.
308 *
309 * Single-byte lookup:
310 *
311 * Stage 2 only contains 16-bit indexes directly to the 16-blocks in stage 3.
312 * Stage 3 contains one 16-bit word per result:
313 * Bits 15..8 indicate the kind of result:
314 * f roundtrip result
315 * c fallback result from private-use code point
316 * 8 fallback result from other code points
317 * 0 unassigned
318 * Bits 7..0 contain the codepage byte. A zero byte is always possible.
319 *
320 * In version 4.3, the runtime code can build an sbcsIndex for a utf8Friendly
321 * file. For 2-byte UTF-8 byte sequences and some 3-byte sequences the lookup
322 * becomes a 2-stage (single-index) trie lookup with 6 bits for stage 3.
323 * ASCII code points can be looked up with a linear array access into stage 3.
324 * See maxFastUChar and other details in ucnvmbcs.h.
325 *
326 * Multi-byte lookup:
327 *
328 * Stage 2 contains a 32-bit word for each 16-block in stage 3:
329 * Bits 31..16 contain flags for which stage 3 entries contain roundtrip results
330 * test: MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c)
331 * If this test is false, then a non-zero result will be interpreted as
332 * a fallback mapping.
333 * Bits 15..0 contain the index to stage 3, which must be multiplied by 16*(bytes per char)
334 *
335 * Stage 3 contains 2, 3, or 4 bytes per result.
336 * 2 or 4 bytes are stored as uint16_t/uint32_t in platform endianness,
337 * while 3 bytes are stored as bytes in big-endian order.
338 * Leading zero bytes are ignored, and the number of bytes is counted.
339 * A zero byte mapping result is possible as a roundtrip result.
340 * For some output types, the actual result is processed from this;
341 * see ucnv_MBCSFromUnicodeWithOffsets().
342 *
343 * Note that stage 1 always contains 0x440=1088 entries (0x440==0x110000>>10),
344 * or (version 3 and up) for BMP-only codepages, it contains 64 entries.
345 *
346 * In version 4.3, a utf8Friendly file contains an mbcsIndex table.
347 * For 2-byte UTF-8 byte sequences and most 3-byte sequences the lookup
348 * becomes a 2-stage (single-index) trie lookup with 6 bits for stage 3.
349 * ASCII code points can be looked up with a linear array access into stage 3.
350 * See maxFastUChar, mbcsIndex and other details in ucnvmbcs.h.
351 *
352 * In version 3, stage 2 blocks may overlap by multiples of the multiplier
353 * for compaction.
354 * In version 4, stage 2 blocks (and for single-byte codepages, stage 3 blocks)
355 * may overlap by any number of entries.
356 *
357 * MBCS version 2 added:
358 * the converter checks for known output types, which allows
359 * adding new ones without crashing an unaware converter
360 */
361
362 static const UConverterImpl _SBCSUTF8Impl;
363 static const UConverterImpl _DBCSUTF8Impl;
364
365 /* GB 18030 data ------------------------------------------------------------ */
366
367 /* helper macros for linear values for GB 18030 four-byte sequences */
368 #define LINEAR_18030(a, b, c, d) ((((a)*10+(b))*126L+(c))*10L+(d))
369
370 #define LINEAR_18030_BASE LINEAR_18030(0x81, 0x30, 0x81, 0x30)
371
372 #define LINEAR(x) LINEAR_18030(x>>24, (x>>16)&0xff, (x>>8)&0xff, x&0xff)
373
374 /*
375 * Some ranges of GB 18030 where both the Unicode code points and the
376 * GB four-byte sequences are contiguous and are handled algorithmically by
377 * the special callback functions below.
378 * The values are start & end of Unicode & GB codes.
379 *
380 * Note that single surrogates are not mapped by GB 18030
381 * as of the re-released mapping tables from 2000-nov-30.
382 */
383 static const uint32_t
384 gb18030Ranges[14][4]={
385 {0x10000, 0x10FFFF, LINEAR(0x90308130), LINEAR(0xE3329A35)},
386 {0x9FA6, 0xD7FF, LINEAR(0x82358F33), LINEAR(0x8336C738)},
387 {0x0452, 0x1E3E, LINEAR(0x8130D330), LINEAR(0x8135F436)},
388 {0x1E40, 0x200F, LINEAR(0x8135F438), LINEAR(0x8136A531)},
389 {0xE865, 0xF92B, LINEAR(0x8336D030), LINEAR(0x84308534)},
390 {0x2643, 0x2E80, LINEAR(0x8137A839), LINEAR(0x8138FD38)},
391 {0xFA2A, 0xFE2F, LINEAR(0x84309C38), LINEAR(0x84318537)},
392 {0x3CE1, 0x4055, LINEAR(0x8231D438), LINEAR(0x8232AF32)},
393 {0x361B, 0x3917, LINEAR(0x8230A633), LINEAR(0x8230F237)},
394 {0x49B8, 0x4C76, LINEAR(0x8234A131), LINEAR(0x8234E733)},
395 {0x4160, 0x4336, LINEAR(0x8232C937), LINEAR(0x8232F837)},
396 {0x478E, 0x4946, LINEAR(0x8233E838), LINEAR(0x82349638)},
397 {0x44D7, 0x464B, LINEAR(0x8233A339), LINEAR(0x8233C931)},
398 {0xFFE6, 0xFFFF, LINEAR(0x8431A234), LINEAR(0x8431A439)}
399 };
400
401 /* bit flag for UConverter.options indicating GB 18030 special handling */
402 #define _MBCS_OPTION_GB18030 0x8000
403
404 /* bit flag for UConverter.options indicating KEIS,JEF,JIF special handling */
405 #define _MBCS_OPTION_KEIS 0x01000
406 #define _MBCS_OPTION_JEF 0x02000
407 #define _MBCS_OPTION_JIPS 0x04000
408
409 #define KEIS_SO_CHAR_1 0x0A
410 #define KEIS_SO_CHAR_2 0x42
411 #define KEIS_SI_CHAR_1 0x0A
412 #define KEIS_SI_CHAR_2 0x41
413
414 #define JEF_SO_CHAR 0x28
415 #define JEF_SI_CHAR 0x29
416
417 #define JIPS_SO_CHAR_1 0x1A
418 #define JIPS_SO_CHAR_2 0x70
419 #define JIPS_SI_CHAR_1 0x1A
420 #define JIPS_SI_CHAR_2 0x71
421
422 enum SISO_Option {
423 SI,
424 SO
425 };
426 typedef enum SISO_Option SISO_Option;
427
428 static int32_t getSISOBytes(SISO_Option option, uint32_t cnvOption, uint8_t *value) {
429 int32_t SISOLength = 0;
430
431 switch (option) {
432 case SI:
433 if ((cnvOption&_MBCS_OPTION_KEIS)!=0) {
434 value[0] = KEIS_SI_CHAR_1;
435 value[1] = KEIS_SI_CHAR_2;
436 SISOLength = 2;
437 } else if ((cnvOption&_MBCS_OPTION_JEF)!=0) {
438 value[0] = JEF_SI_CHAR;
439 SISOLength = 1;
440 } else if ((cnvOption&_MBCS_OPTION_JIPS)!=0) {
441 value[0] = JIPS_SI_CHAR_1;
442 value[1] = JIPS_SI_CHAR_2;
443 SISOLength = 2;
444 } else {
445 value[0] = UCNV_SI;
446 SISOLength = 1;
447 }
448 break;
449 case SO:
450 if ((cnvOption&_MBCS_OPTION_KEIS)!=0) {
451 value[0] = KEIS_SO_CHAR_1;
452 value[1] = KEIS_SO_CHAR_2;
453 SISOLength = 2;
454 } else if ((cnvOption&_MBCS_OPTION_JEF)!=0) {
455 value[0] = JEF_SO_CHAR;
456 SISOLength = 1;
457 } else if ((cnvOption&_MBCS_OPTION_JIPS)!=0) {
458 value[0] = JIPS_SO_CHAR_1;
459 value[1] = JIPS_SO_CHAR_2;
460 SISOLength = 2;
461 } else {
462 value[0] = UCNV_SO;
463 SISOLength = 1;
464 }
465 break;
466 default:
467 /* Should never happen. */
468 break;
469 }
470
471 return SISOLength;
472 }
473
474 /* Miscellaneous ------------------------------------------------------------ */
475
476 /**
477 * Callback from ucnv_MBCSEnumToUnicode(), takes 32 mappings from
478 * consecutive sequences of bytes, starting from the one encoded in value,
479 * to Unicode code points. (Multiple mappings to reduce per-function call overhead.)
480 * Does not currently support m:n mappings or reverse fallbacks.
481 * This function will not be called for sequences of bytes with leading zeros.
482 *
483 * @param context an opaque pointer, as passed into ucnv_MBCSEnumToUnicode()
484 * @param value contains 1..4 bytes of the first byte sequence, right-aligned
485 * @param codePoints resulting Unicode code points, or negative if a byte sequence does
486 * not map to anything
487 * @return TRUE to continue enumeration, FALSE to stop
488 */
489 typedef UBool U_CALLCONV
490 UConverterEnumToUCallback(const void *context, uint32_t value, UChar32 codePoints[32]);
491
492 /* similar to ucnv_MBCSGetNextUChar() but recursive */
493 static UBool
494 enumToU(UConverterMBCSTable *mbcsTable, int8_t stateProps[],
495 int32_t state, uint32_t offset,
496 uint32_t value,
497 UConverterEnumToUCallback *callback, const void *context,
498 UErrorCode *pErrorCode) {
499 UChar32 codePoints[32];
500 const int32_t *row;
501 const uint16_t *unicodeCodeUnits;
502 UChar32 anyCodePoints;
503 int32_t b, limit;
504
505 row=mbcsTable->stateTable[state];
506 unicodeCodeUnits=mbcsTable->unicodeCodeUnits;
507
508 value<<=8;
509 anyCodePoints=-1; /* becomes non-negative if there is a mapping */
510
511 b=(stateProps[state]&0x38)<<2;
512 if(b==0 && stateProps[state]>=0x40) {
513 /* skip byte sequences with leading zeros because they are not stored in the fromUnicode table */
514 codePoints[0]=U_SENTINEL;
515 b=1;
516 }
517 limit=((stateProps[state]&7)+1)<<5;
518 while(b<limit) {
519 int32_t entry=row[b];
520 if(MBCS_ENTRY_IS_TRANSITION(entry)) {
521 int32_t nextState=MBCS_ENTRY_TRANSITION_STATE(entry);
522 if(stateProps[nextState]>=0) {
523 /* recurse to a state with non-ignorable actions */
524 if(!enumToU(
525 mbcsTable, stateProps, nextState,
526 offset+MBCS_ENTRY_TRANSITION_OFFSET(entry),
527 value|(uint32_t)b,
528 callback, context,
529 pErrorCode)) {
530 return FALSE;
531 }
532 }
533 codePoints[b&0x1f]=U_SENTINEL;
534 } else {
535 UChar32 c;
536 int32_t action;
537
538 /*
539 * An if-else-if chain provides more reliable performance for
540 * the most common cases compared to a switch.
541 */
542 action=MBCS_ENTRY_FINAL_ACTION(entry);
543 if(action==MBCS_STATE_VALID_DIRECT_16) {
544 /* output BMP code point */
545 c=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
546 } else if(action==MBCS_STATE_VALID_16) {
547 int32_t finalOffset=offset+MBCS_ENTRY_FINAL_VALUE_16(entry);
548 c=unicodeCodeUnits[finalOffset];
549 if(c<0xfffe) {
550 /* output BMP code point */
551 } else {
552 c=U_SENTINEL;
553 }
554 } else if(action==MBCS_STATE_VALID_16_PAIR) {
555 int32_t finalOffset=offset+MBCS_ENTRY_FINAL_VALUE_16(entry);
556 c=unicodeCodeUnits[finalOffset++];
557 if(c<0xd800) {
558 /* output BMP code point below 0xd800 */
559 } else if(c<=0xdbff) {
560 /* output roundtrip or fallback supplementary code point */
561 c=((c&0x3ff)<<10)+unicodeCodeUnits[finalOffset]+(0x10000-0xdc00);
562 } else if(c==0xe000) {
563 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
564 c=unicodeCodeUnits[finalOffset];
565 } else {
566 c=U_SENTINEL;
567 }
568 } else if(action==MBCS_STATE_VALID_DIRECT_20) {
569 /* output supplementary code point */
570 c=(UChar32)(MBCS_ENTRY_FINAL_VALUE(entry)+0x10000);
571 } else {
572 c=U_SENTINEL;
573 }
574
575 codePoints[b&0x1f]=c;
576 anyCodePoints&=c;
577 }
578 if(((++b)&0x1f)==0) {
579 if(anyCodePoints>=0) {
580 if(!callback(context, value|(uint32_t)(b-0x20), codePoints)) {
581 return FALSE;
582 }
583 anyCodePoints=-1;
584 }
585 }
586 }
587 return TRUE;
588 }
589
590 /*
591 * Only called if stateProps[state]==-1.
592 * A recursive call may do stateProps[state]|=0x40 if this state is the target of an
593 * MBCS_STATE_CHANGE_ONLY.
594 */
595 static int8_t
596 getStateProp(const int32_t (*stateTable)[256], int8_t stateProps[], int state) {
597 const int32_t *row;
598 int32_t min, max, entry, nextState;
599
600 row=stateTable[state];
601 stateProps[state]=0;
602
603 /* find first non-ignorable state */
604 for(min=0;; ++min) {
605 entry=row[min];
606 nextState=MBCS_ENTRY_STATE(entry);
607 if(stateProps[nextState]==-1) {
608 getStateProp(stateTable, stateProps, nextState);
609 }
610 if(MBCS_ENTRY_IS_TRANSITION(entry)) {
611 if(stateProps[nextState]>=0) {
612 break;
613 }
614 } else if(MBCS_ENTRY_FINAL_ACTION(entry)<MBCS_STATE_UNASSIGNED) {
615 break;
616 }
617 if(min==0xff) {
618 stateProps[state]=-0x40; /* (int8_t)0xc0 */
619 return stateProps[state];
620 }
621 }
622 stateProps[state]|=(int8_t)((min>>5)<<3);
623
624 /* find last non-ignorable state */
625 for(max=0xff; min<max; --max) {
626 entry=row[max];
627 nextState=MBCS_ENTRY_STATE(entry);
628 if(stateProps[nextState]==-1) {
629 getStateProp(stateTable, stateProps, nextState);
630 }
631 if(MBCS_ENTRY_IS_TRANSITION(entry)) {
632 if(stateProps[nextState]>=0) {
633 break;
634 }
635 } else if(MBCS_ENTRY_FINAL_ACTION(entry)<MBCS_STATE_UNASSIGNED) {
636 break;
637 }
638 }
639 stateProps[state]|=(int8_t)(max>>5);
640
641 /* recurse further and collect direct-state information */
642 while(min<=max) {
643 entry=row[min];
644 nextState=MBCS_ENTRY_STATE(entry);
645 if(stateProps[nextState]==-1) {
646 getStateProp(stateTable, stateProps, nextState);
647 }
648 if(MBCS_ENTRY_IS_FINAL(entry)) {
649 stateProps[nextState]|=0x40;
650 if(MBCS_ENTRY_FINAL_ACTION(entry)<=MBCS_STATE_FALLBACK_DIRECT_20) {
651 stateProps[state]|=0x40;
652 }
653 }
654 ++min;
655 }
656 return stateProps[state];
657 }
658
659 /*
660 * Internal function enumerating the toUnicode data of an MBCS converter.
661 * Currently only used for reconstituting data for a MBCS_OPT_NO_FROM_U
662 * table, but could also be used for a future ucnv_getUnicodeSet() option
663 * that includes reverse fallbacks (after updating this function's implementation).
664 * Currently only handles roundtrip mappings.
665 * Does not currently handle extensions.
666 */
667 static void
668 ucnv_MBCSEnumToUnicode(UConverterMBCSTable *mbcsTable,
669 UConverterEnumToUCallback *callback, const void *context,
670 UErrorCode *pErrorCode) {
671 /*
672 * Properties for each state, to speed up the enumeration.
673 * Ignorable actions are unassigned/illegal/state-change-only:
674 * They do not lead to mappings.
675 *
676 * Bits 7..6:
677 * 1 direct/initial state (stateful converters have multiple)
678 * 0 non-initial state with transitions or with non-ignorable result actions
679 * -1 final state with only ignorable actions
680 *
681 * Bits 5..3:
682 * The lowest byte value with non-ignorable actions is
683 * value<<5 (rounded down).
684 *
685 * Bits 2..0:
686 * The highest byte value with non-ignorable actions is
687 * (value<<5)&0x1f (rounded up).
688 */
689 int8_t stateProps[MBCS_MAX_STATE_COUNT];
690 int32_t state;
691
692 uprv_memset(stateProps, -1, sizeof(stateProps));
693
694 /* recurse from state 0 and set all stateProps */
695 getStateProp(mbcsTable->stateTable, stateProps, 0);
696
697 for(state=0; state<mbcsTable->countStates; ++state) {
698 /*if(stateProps[state]==-1) {
699 printf("unused/unreachable <icu:state> %d\n", state);
700 }*/
701 if(stateProps[state]>=0x40) {
702 /* start from each direct state */
703 enumToU(
704 mbcsTable, stateProps, state, 0, 0,
705 callback, context,
706 pErrorCode);
707 }
708 }
709 }
710
711 U_CFUNC void
712 ucnv_MBCSGetFilteredUnicodeSetForUnicode(const UConverterSharedData *sharedData,
713 const USetAdder *sa,
714 UConverterUnicodeSet which,
715 UConverterSetFilter filter,
716 UErrorCode *pErrorCode) {
717 const UConverterMBCSTable *mbcsTable;
718 const uint16_t *table;
719
720 uint32_t st3;
721 uint16_t st1, maxStage1, st2;
722
723 UChar32 c;
724
725 /* enumerate the from-Unicode trie table */
726 mbcsTable=&sharedData->mbcs;
727 table=mbcsTable->fromUnicodeTable;
728 if(mbcsTable->unicodeMask&UCNV_HAS_SUPPLEMENTARY) {
729 maxStage1=0x440;
730 } else {
731 maxStage1=0x40;
732 }
733
734 c=0; /* keep track of the current code point while enumerating */
735
736 if(mbcsTable->outputType==MBCS_OUTPUT_1) {
737 const uint16_t *stage2, *stage3, *results;
738 uint16_t minValue;
739
740 results=(const uint16_t *)mbcsTable->fromUnicodeBytes;
741
742 /*
743 * Set a threshold variable for selecting which mappings to use.
744 * See ucnv_MBCSSingleFromBMPWithOffsets() and
745 * MBCS_SINGLE_RESULT_FROM_U() for details.
746 */
747 if(which==UCNV_ROUNDTRIP_SET) {
748 /* use only roundtrips */
749 minValue=0xf00;
750 } else /* UCNV_ROUNDTRIP_AND_FALLBACK_SET */ {
751 /* use all roundtrip and fallback results */
752 minValue=0x800;
753 }
754
755 for(st1=0; st1<maxStage1; ++st1) {
756 st2=table[st1];
757 if(st2>maxStage1) {
758 stage2=table+st2;
759 for(st2=0; st2<64; ++st2) {
760 if((st3=stage2[st2])!=0) {
761 /* read the stage 3 block */
762 stage3=results+st3;
763
764 do {
765 if(*stage3++>=minValue) {
766 sa->add(sa->set, c);
767 }
768 } while((++c&0xf)!=0);
769 } else {
770 c+=16; /* empty stage 3 block */
771 }
772 }
773 } else {
774 c+=1024; /* empty stage 2 block */
775 }
776 }
777 } else {
778 const uint32_t *stage2;
779 const uint8_t *stage3, *bytes;
780 uint32_t st3Multiplier;
781 uint32_t value;
782 UBool useFallback;
783
784 bytes=mbcsTable->fromUnicodeBytes;
785
786 useFallback=(UBool)(which==UCNV_ROUNDTRIP_AND_FALLBACK_SET);
787
788 switch(mbcsTable->outputType) {
789 case MBCS_OUTPUT_3:
790 case MBCS_OUTPUT_4_EUC:
791 st3Multiplier=3;
792 break;
793 case MBCS_OUTPUT_4:
794 st3Multiplier=4;
795 break;
796 default:
797 st3Multiplier=2;
798 break;
799 }
800
801 for(st1=0; st1<maxStage1; ++st1) {
802 st2=table[st1];
803 if(st2>(maxStage1>>1)) {
804 stage2=(const uint32_t *)table+st2;
805 for(st2=0; st2<64; ++st2) {
806 if((st3=stage2[st2])!=0) {
807 /* read the stage 3 block */
808 stage3=bytes+st3Multiplier*16*(uint32_t)(uint16_t)st3;
809
810 /* get the roundtrip flags for the stage 3 block */
811 st3>>=16;
812
813 /*
814 * Add code points for which the roundtrip flag is set,
815 * or which map to non-zero bytes if we use fallbacks.
816 * See ucnv_MBCSFromUnicodeWithOffsets() for details.
817 */
818 switch(filter) {
819 case UCNV_SET_FILTER_NONE:
820 do {
821 if(st3&1) {
822 sa->add(sa->set, c);
823 stage3+=st3Multiplier;
824 } else if(useFallback) {
825 uint8_t b=0;
826 switch(st3Multiplier) {
827 case 4:
828 b|=*stage3++;
829 case 3: /*fall through*/
830 b|=*stage3++;
831 case 2: /*fall through*/
832 b|=stage3[0]|stage3[1];
833 stage3+=2;
834 default:
835 break;
836 }
837 if(b!=0) {
838 sa->add(sa->set, c);
839 }
840 }
841 st3>>=1;
842 } while((++c&0xf)!=0);
843 break;
844 case UCNV_SET_FILTER_DBCS_ONLY:
845 /* Ignore single-byte results (<0x100). */
846 do {
847 if(((st3&1)!=0 || useFallback) && *((const uint16_t *)stage3)>=0x100) {
848 sa->add(sa->set, c);
849 }
850 st3>>=1;
851 stage3+=2; /* +=st3Multiplier */
852 } while((++c&0xf)!=0);
853 break;
854 case UCNV_SET_FILTER_2022_CN:
855 /* Only add code points that map to CNS 11643 planes 1 & 2 for non-EXT ISO-2022-CN. */
856 do {
857 if(((st3&1)!=0 || useFallback) && ((value=*stage3)==0x81 || value==0x82)) {
858 sa->add(sa->set, c);
859 }
860 st3>>=1;
861 stage3+=3; /* +=st3Multiplier */
862 } while((++c&0xf)!=0);
863 break;
864 case UCNV_SET_FILTER_SJIS:
865 /* Only add code points that map to Shift-JIS codes corresponding to JIS X 0208. */
866 do {
867 if(((st3&1)!=0 || useFallback) && (value=*((const uint16_t *)stage3))>=0x8140 && value<=0xeffc) {
868 sa->add(sa->set, c);
869 }
870 st3>>=1;
871 stage3+=2; /* +=st3Multiplier */
872 } while((++c&0xf)!=0);
873 break;
874 case UCNV_SET_FILTER_GR94DBCS:
875 /* Only add code points that map to ISO 2022 GR 94 DBCS codes (each byte A1..FE). */
876 do {
877 if( ((st3&1)!=0 || useFallback) &&
878 (uint16_t)((value=*((const uint16_t *)stage3)) - 0xa1a1)<=(0xfefe - 0xa1a1) &&
879 (uint8_t)(value-0xa1)<=(0xfe - 0xa1)
880 ) {
881 sa->add(sa->set, c);
882 }
883 st3>>=1;
884 stage3+=2; /* +=st3Multiplier */
885 } while((++c&0xf)!=0);
886 break;
887 case UCNV_SET_FILTER_HZ:
888 /* Only add code points that are suitable for HZ DBCS (lead byte A1..FD). */
889 do {
890 if( ((st3&1)!=0 || useFallback) &&
891 (uint16_t)((value=*((const uint16_t *)stage3))-0xa1a1)<=(0xfdfe - 0xa1a1) &&
892 (uint8_t)(value-0xa1)<=(0xfe - 0xa1)
893 ) {
894 sa->add(sa->set, c);
895 }
896 st3>>=1;
897 stage3+=2; /* +=st3Multiplier */
898 } while((++c&0xf)!=0);
899 break;
900 default:
901 *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
902 return;
903 }
904 } else {
905 c+=16; /* empty stage 3 block */
906 }
907 }
908 } else {
909 c+=1024; /* empty stage 2 block */
910 }
911 }
912 }
913
914 ucnv_extGetUnicodeSet(sharedData, sa, which, filter, pErrorCode);
915 }
916
917 U_CFUNC void
918 ucnv_MBCSGetUnicodeSetForUnicode(const UConverterSharedData *sharedData,
919 const USetAdder *sa,
920 UConverterUnicodeSet which,
921 UErrorCode *pErrorCode) {
922 ucnv_MBCSGetFilteredUnicodeSetForUnicode(
923 sharedData, sa, which,
924 sharedData->mbcs.outputType==MBCS_OUTPUT_DBCS_ONLY ?
925 UCNV_SET_FILTER_DBCS_ONLY :
926 UCNV_SET_FILTER_NONE,
927 pErrorCode);
928 }
929
930 static void
931 ucnv_MBCSGetUnicodeSet(const UConverter *cnv,
932 const USetAdder *sa,
933 UConverterUnicodeSet which,
934 UErrorCode *pErrorCode) {
935 if(cnv->options&_MBCS_OPTION_GB18030) {
936 sa->addRange(sa->set, 0, 0xd7ff);
937 sa->addRange(sa->set, 0xe000, 0x10ffff);
938 } else {
939 ucnv_MBCSGetUnicodeSetForUnicode(cnv->sharedData, sa, which, pErrorCode);
940 }
941 }
942
943 /* conversion extensions for input not in the main table -------------------- */
944
945 /*
946 * Hardcoded extension handling for GB 18030.
947 * Definition of LINEAR macros and gb18030Ranges see near the beginning of the file.
948 *
949 * In the future, conversion extensions may handle m:n mappings and delta tables,
950 * see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/conversion/conversion_extensions.html
951 *
952 * If an input character cannot be mapped, then these functions set an error
953 * code. The framework will then call the callback function.
954 */
955
956 /*
957 * @return if(U_FAILURE) return the code point for cnv->fromUChar32
958 * else return 0 after output has been written to the target
959 */
960 static UChar32
961 _extFromU(UConverter *cnv, const UConverterSharedData *sharedData,
962 UChar32 cp,
963 const UChar **source, const UChar *sourceLimit,
964 uint8_t **target, const uint8_t *targetLimit,
965 int32_t **offsets, int32_t sourceIndex,
966 UBool flush,
967 UErrorCode *pErrorCode) {
968 const int32_t *cx;
969
970 cnv->useSubChar1=FALSE;
971
972 if( (cx=sharedData->mbcs.extIndexes)!=NULL &&
973 ucnv_extInitialMatchFromU(
974 cnv, cx,
975 cp, source, sourceLimit,
976 (char **)target, (char *)targetLimit,
977 offsets, sourceIndex,
978 flush,
979 pErrorCode)
980 ) {
981 return 0; /* an extension mapping handled the input */
982 }
983
984 /* GB 18030 */
985 if((cnv->options&_MBCS_OPTION_GB18030)!=0) {
986 const uint32_t *range;
987 int32_t i;
988
989 range=gb18030Ranges[0];
990 for(i=0; i<sizeof(gb18030Ranges)/sizeof(gb18030Ranges[0]); range+=4, ++i) {
991 if(range[0]<=(uint32_t)cp && (uint32_t)cp<=range[1]) {
992 /* found the Unicode code point, output the four-byte sequence for it */
993 uint32_t linear;
994 char bytes[4];
995
996 /* get the linear value of the first GB 18030 code in this range */
997 linear=range[2]-LINEAR_18030_BASE;
998
999 /* add the offset from the beginning of the range */
1000 linear+=((uint32_t)cp-range[0]);
1001
1002 /* turn this into a four-byte sequence */
1003 bytes[3]=(char)(0x30+linear%10); linear/=10;
1004 bytes[2]=(char)(0x81+linear%126); linear/=126;
1005 bytes[1]=(char)(0x30+linear%10); linear/=10;
1006 bytes[0]=(char)(0x81+linear);
1007
1008 /* output this sequence */
1009 ucnv_fromUWriteBytes(cnv,
1010 bytes, 4, (char **)target, (char *)targetLimit,
1011 offsets, sourceIndex, pErrorCode);
1012 return 0;
1013 }
1014 }
1015 }
1016
1017 /* no mapping */
1018 *pErrorCode=U_INVALID_CHAR_FOUND;
1019 return cp;
1020 }
1021
1022 /*
1023 * Input sequence: cnv->toUBytes[0..length[
1024 * @return if(U_FAILURE) return the length (toULength, byteIndex) for the input
1025 * else return 0 after output has been written to the target
1026 */
1027 static int8_t
1028 _extToU(UConverter *cnv, const UConverterSharedData *sharedData,
1029 int8_t length,
1030 const uint8_t **source, const uint8_t *sourceLimit,
1031 UChar **target, const UChar *targetLimit,
1032 int32_t **offsets, int32_t sourceIndex,
1033 UBool flush,
1034 UErrorCode *pErrorCode) {
1035 const int32_t *cx;
1036
1037 if( (cx=sharedData->mbcs.extIndexes)!=NULL &&
1038 ucnv_extInitialMatchToU(
1039 cnv, cx,
1040 length, (const char **)source, (const char *)sourceLimit,
1041 target, targetLimit,
1042 offsets, sourceIndex,
1043 flush,
1044 pErrorCode)
1045 ) {
1046 return 0; /* an extension mapping handled the input */
1047 }
1048
1049 /* GB 18030 */
1050 if(length==4 && (cnv->options&_MBCS_OPTION_GB18030)!=0) {
1051 const uint32_t *range;
1052 uint32_t linear;
1053 int32_t i;
1054
1055 linear=LINEAR_18030(cnv->toUBytes[0], cnv->toUBytes[1], cnv->toUBytes[2], cnv->toUBytes[3]);
1056 range=gb18030Ranges[0];
1057 for(i=0; i<sizeof(gb18030Ranges)/sizeof(gb18030Ranges[0]); range+=4, ++i) {
1058 if(range[2]<=linear && linear<=range[3]) {
1059 /* found the sequence, output the Unicode code point for it */
1060 *pErrorCode=U_ZERO_ERROR;
1061
1062 /* add the linear difference between the input and start sequences to the start code point */
1063 linear=range[0]+(linear-range[2]);
1064
1065 /* output this code point */
1066 ucnv_toUWriteCodePoint(cnv, linear, target, targetLimit, offsets, sourceIndex, pErrorCode);
1067
1068 return 0;
1069 }
1070 }
1071 }
1072
1073 /* no mapping */
1074 *pErrorCode=U_INVALID_CHAR_FOUND;
1075 return length;
1076 }
1077
1078 /* EBCDIC swap LF<->NL ------------------------------------------------------ */
1079
1080 /*
1081 * This code modifies a standard EBCDIC<->Unicode mapping table for
1082 * OS/390 (z/OS) Unix System Services (Open Edition).
1083 * The difference is in the mapping of Line Feed and New Line control codes:
1084 * Standard EBCDIC maps
1085 *
1086 * <U000A> \x25 |0
1087 * <U0085> \x15 |0
1088 *
1089 * but OS/390 USS EBCDIC swaps the control codes for LF and NL,
1090 * mapping
1091 *
1092 * <U000A> \x15 |0
1093 * <U0085> \x25 |0
1094 *
1095 * This code modifies a loaded standard EBCDIC<->Unicode mapping table
1096 * by copying it into allocated memory and swapping the LF and NL values.
1097 * It allows to support the same EBCDIC charset in both versions without
1098 * duplicating the entire installed table.
1099 */
1100
1101 /* standard EBCDIC codes */
1102 #define EBCDIC_LF 0x25
1103 #define EBCDIC_NL 0x15
1104
1105 /* standard EBCDIC codes with roundtrip flag as stored in Unicode-to-single-byte tables */
1106 #define EBCDIC_RT_LF 0xf25
1107 #define EBCDIC_RT_NL 0xf15
1108
1109 /* Unicode code points */
1110 #define U_LF 0x0a
1111 #define U_NL 0x85
1112
1113 static UBool
1114 _EBCDICSwapLFNL(UConverterSharedData *sharedData, UErrorCode *pErrorCode) {
1115 UConverterMBCSTable *mbcsTable;
1116
1117 const uint16_t *table, *results;
1118 const uint8_t *bytes;
1119
1120 int32_t (*newStateTable)[256];
1121 uint16_t *newResults;
1122 uint8_t *p;
1123 char *name;
1124
1125 uint32_t stage2Entry;
1126 uint32_t size, sizeofFromUBytes;
1127
1128 mbcsTable=&sharedData->mbcs;
1129
1130 table=mbcsTable->fromUnicodeTable;
1131 bytes=mbcsTable->fromUnicodeBytes;
1132 results=(const uint16_t *)bytes;
1133
1134 /*
1135 * Check that this is an EBCDIC table with SBCS portion -
1136 * SBCS or EBCDIC_STATEFUL with standard EBCDIC LF and NL mappings.
1137 *
1138 * If not, ignore the option. Options are always ignored if they do not apply.
1139 */
1140 if(!(
1141 (mbcsTable->outputType==MBCS_OUTPUT_1 || mbcsTable->outputType==MBCS_OUTPUT_2_SISO) &&
1142 mbcsTable->stateTable[0][EBCDIC_LF]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_LF) &&
1143 mbcsTable->stateTable[0][EBCDIC_NL]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_NL)
1144 )) {
1145 return FALSE;
1146 }
1147
1148 if(mbcsTable->outputType==MBCS_OUTPUT_1) {
1149 if(!(
1150 EBCDIC_RT_LF==MBCS_SINGLE_RESULT_FROM_U(table, results, U_LF) &&
1151 EBCDIC_RT_NL==MBCS_SINGLE_RESULT_FROM_U(table, results, U_NL)
1152 )) {
1153 return FALSE;
1154 }
1155 } else /* MBCS_OUTPUT_2_SISO */ {
1156 stage2Entry=MBCS_STAGE_2_FROM_U(table, U_LF);
1157 if(!(
1158 MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, U_LF)!=0 &&
1159 EBCDIC_LF==MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, U_LF)
1160 )) {
1161 return FALSE;
1162 }
1163
1164 stage2Entry=MBCS_STAGE_2_FROM_U(table, U_NL);
1165 if(!(
1166 MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, U_NL)!=0 &&
1167 EBCDIC_NL==MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, U_NL)
1168 )) {
1169 return FALSE;
1170 }
1171 }
1172
1173 if(mbcsTable->fromUBytesLength>0) {
1174 /*
1175 * We _know_ the number of bytes in the fromUnicodeBytes array
1176 * starting with header.version 4.1.
1177 */
1178 sizeofFromUBytes=mbcsTable->fromUBytesLength;
1179 } else {
1180 /*
1181 * Otherwise:
1182 * There used to be code to enumerate the fromUnicode
1183 * trie and find the highest entry, but it was removed in ICU 3.2
1184 * because it was not tested and caused a low code coverage number.
1185 * See Jitterbug 3674.
1186 * This affects only some .cnv file formats with a header.version
1187 * below 4.1, and only when swaplfnl is requested.
1188 *
1189 * ucnvmbcs.c revision 1.99 is the last one with the
1190 * ucnv_MBCSSizeofFromUBytes() function.
1191 */
1192 *pErrorCode=U_INVALID_FORMAT_ERROR;
1193 return FALSE;
1194 }
1195
1196 /*
1197 * The table has an appropriate format.
1198 * Allocate and build
1199 * - a modified to-Unicode state table
1200 * - a modified from-Unicode output array
1201 * - a converter name string with the swap option appended
1202 */
1203 size=
1204 mbcsTable->countStates*1024+
1205 sizeofFromUBytes+
1206 UCNV_MAX_CONVERTER_NAME_LENGTH+20;
1207 p=(uint8_t *)uprv_malloc(size);
1208 if(p==NULL) {
1209 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
1210 return FALSE;
1211 }
1212
1213 /* copy and modify the to-Unicode state table */
1214 newStateTable=(int32_t (*)[256])p;
1215 uprv_memcpy(newStateTable, mbcsTable->stateTable, mbcsTable->countStates*1024);
1216
1217 newStateTable[0][EBCDIC_LF]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_NL);
1218 newStateTable[0][EBCDIC_NL]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_LF);
1219
1220 /* copy and modify the from-Unicode result table */
1221 newResults=(uint16_t *)newStateTable[mbcsTable->countStates];
1222 uprv_memcpy(newResults, bytes, sizeofFromUBytes);
1223
1224 /* conveniently, the table access macros work on the left side of expressions */
1225 if(mbcsTable->outputType==MBCS_OUTPUT_1) {
1226 MBCS_SINGLE_RESULT_FROM_U(table, newResults, U_LF)=EBCDIC_RT_NL;
1227 MBCS_SINGLE_RESULT_FROM_U(table, newResults, U_NL)=EBCDIC_RT_LF;
1228 } else /* MBCS_OUTPUT_2_SISO */ {
1229 stage2Entry=MBCS_STAGE_2_FROM_U(table, U_LF);
1230 MBCS_VALUE_2_FROM_STAGE_2(newResults, stage2Entry, U_LF)=EBCDIC_NL;
1231
1232 stage2Entry=MBCS_STAGE_2_FROM_U(table, U_NL);
1233 MBCS_VALUE_2_FROM_STAGE_2(newResults, stage2Entry, U_NL)=EBCDIC_LF;
1234 }
1235
1236 /* set the canonical converter name */
1237 name=(char *)newResults+sizeofFromUBytes;
1238 uprv_strcpy(name, sharedData->staticData->name);
1239 uprv_strcat(name, UCNV_SWAP_LFNL_OPTION_STRING);
1240
1241 /* set the pointers */
1242 umtx_lock(NULL);
1243 if(mbcsTable->swapLFNLStateTable==NULL) {
1244 mbcsTable->swapLFNLStateTable=newStateTable;
1245 mbcsTable->swapLFNLFromUnicodeBytes=(uint8_t *)newResults;
1246 mbcsTable->swapLFNLName=name;
1247
1248 newStateTable=NULL;
1249 }
1250 umtx_unlock(NULL);
1251
1252 /* release the allocated memory if another thread beat us to it */
1253 if(newStateTable!=NULL) {
1254 uprv_free(newStateTable);
1255 }
1256 return TRUE;
1257 }
1258
1259 /* reconstitute omitted fromUnicode data ------------------------------------ */
1260
1261 /* for details, compare with genmbcs.c MBCSAddFromUnicode() and transformEUC() */
1262 static UBool U_CALLCONV
1263 writeStage3Roundtrip(const void *context, uint32_t value, UChar32 codePoints[32]) {
1264 UConverterMBCSTable *mbcsTable=(UConverterMBCSTable *)context;
1265 const uint16_t *table;
1266 uint32_t *stage2;
1267 uint8_t *bytes, *p;
1268 UChar32 c;
1269 int32_t i, st3;
1270
1271 table=mbcsTable->fromUnicodeTable;
1272 bytes=(uint8_t *)mbcsTable->fromUnicodeBytes;
1273
1274 /* for EUC outputTypes, modify the value like genmbcs.c's transformEUC() */
1275 switch(mbcsTable->outputType) {
1276 case MBCS_OUTPUT_3_EUC:
1277 if(value<=0xffff) {
1278 /* short sequences are stored directly */
1279 /* code set 0 or 1 */
1280 } else if(value<=0x8effff) {
1281 /* code set 2 */
1282 value&=0x7fff;
1283 } else /* first byte is 0x8f */ {
1284 /* code set 3 */
1285 value&=0xff7f;
1286 }
1287 break;
1288 case MBCS_OUTPUT_4_EUC:
1289 if(value<=0xffffff) {
1290 /* short sequences are stored directly */
1291 /* code set 0 or 1 */
1292 } else if(value<=0x8effffff) {
1293 /* code set 2 */
1294 value&=0x7fffff;
1295 } else /* first byte is 0x8f */ {
1296 /* code set 3 */
1297 value&=0xff7fff;
1298 }
1299 break;
1300 default:
1301 break;
1302 }
1303
1304 for(i=0; i<=0x1f; ++value, ++i) {
1305 c=codePoints[i];
1306 if(c<0) {
1307 continue;
1308 }
1309
1310 /* locate the stage 2 & 3 data */
1311 stage2=((uint32_t *)table)+table[c>>10]+((c>>4)&0x3f);
1312 p=bytes;
1313 st3=(int32_t)(uint16_t)*stage2*16+(c&0xf);
1314
1315 /* write the codepage bytes into stage 3 */
1316 switch(mbcsTable->outputType) {
1317 case MBCS_OUTPUT_3:
1318 case MBCS_OUTPUT_4_EUC:
1319 p+=st3*3;
1320 p[0]=(uint8_t)(value>>16);
1321 p[1]=(uint8_t)(value>>8);
1322 p[2]=(uint8_t)value;
1323 break;
1324 case MBCS_OUTPUT_4:
1325 ((uint32_t *)p)[st3]=value;
1326 break;
1327 default:
1328 /* 2 bytes per character */
1329 ((uint16_t *)p)[st3]=(uint16_t)value;
1330 break;
1331 }
1332
1333 /* set the roundtrip flag */
1334 *stage2|=(1UL<<(16+(c&0xf)));
1335 }
1336 return TRUE;
1337 }
1338
1339 static void
1340 reconstituteData(UConverterMBCSTable *mbcsTable,
1341 uint32_t stage1Length, uint32_t stage2Length,
1342 uint32_t fullStage2Length, /* lengths are numbers of units, not bytes */
1343 UErrorCode *pErrorCode) {
1344 uint16_t *stage1;
1345 uint32_t *stage2;
1346 uint8_t *bytes;
1347 uint32_t dataLength=stage1Length*2+fullStage2Length*4+mbcsTable->fromUBytesLength;
1348 mbcsTable->reconstitutedData=(uint8_t *)uprv_malloc(dataLength);
1349 if(mbcsTable->reconstitutedData==NULL) {
1350 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
1351 return;
1352 }
1353 uprv_memset(mbcsTable->reconstitutedData, 0, dataLength);
1354
1355 /* copy existing data and reroute the pointers */
1356 stage1=(uint16_t *)mbcsTable->reconstitutedData;
1357 uprv_memcpy(stage1, mbcsTable->fromUnicodeTable, stage1Length*2);
1358
1359 stage2=(uint32_t *)(stage1+stage1Length);
1360 uprv_memcpy(stage2+(fullStage2Length-stage2Length),
1361 mbcsTable->fromUnicodeTable+stage1Length,
1362 stage2Length*4);
1363
1364 mbcsTable->fromUnicodeTable=stage1;
1365 mbcsTable->fromUnicodeBytes=bytes=(uint8_t *)(stage2+fullStage2Length);
1366
1367 /* indexes into stage 2 count from the bottom of the fromUnicodeTable */
1368 stage2=(uint32_t *)stage1;
1369
1370 /* reconstitute the initial part of stage 2 from the mbcsIndex */
1371 {
1372 int32_t stageUTF8Length=((int32_t)mbcsTable->maxFastUChar+1)>>6;
1373 int32_t stageUTF8Index=0;
1374 int32_t st1, st2, st3, i;
1375
1376 for(st1=0; stageUTF8Index<stageUTF8Length; ++st1) {
1377 st2=stage1[st1];
1378 if(st2!=stage1Length/2) {
1379 /* each stage 2 block has 64 entries corresponding to 16 entries in the mbcsIndex */
1380 for(i=0; i<16; ++i) {
1381 st3=mbcsTable->mbcsIndex[stageUTF8Index++];
1382 if(st3!=0) {
1383 /* an stage 2 entry's index is per stage 3 16-block, not per stage 3 entry */
1384 st3>>=4;
1385 /*
1386 * 4 stage 2 entries point to 4 consecutive stage 3 16-blocks which are
1387 * allocated together as a single 64-block for access from the mbcsIndex
1388 */
1389 stage2[st2++]=st3++;
1390 stage2[st2++]=st3++;
1391 stage2[st2++]=st3++;
1392 stage2[st2++]=st3;
1393 } else {
1394 /* no stage 3 block, skip */
1395 st2+=4;
1396 }
1397 }
1398 } else {
1399 /* no stage 2 block, skip */
1400 stageUTF8Index+=16;
1401 }
1402 }
1403 }
1404
1405 /* reconstitute fromUnicodeBytes with roundtrips from toUnicode data */
1406 ucnv_MBCSEnumToUnicode(mbcsTable, writeStage3Roundtrip, mbcsTable, pErrorCode);
1407 }
1408
1409 /* MBCS setup functions ----------------------------------------------------- */
1410
1411 static void
1412 ucnv_MBCSLoad(UConverterSharedData *sharedData,
1413 UConverterLoadArgs *pArgs,
1414 const uint8_t *raw,
1415 UErrorCode *pErrorCode) {
1416 UDataInfo info;
1417 UConverterMBCSTable *mbcsTable=&sharedData->mbcs;
1418 _MBCSHeader *header=(_MBCSHeader *)raw;
1419 uint32_t offset;
1420 uint32_t headerLength;
1421 UBool noFromU=FALSE;
1422
1423 if(header->version[0]==4) {
1424 headerLength=MBCS_HEADER_V4_LENGTH;
1425 } else if(header->version[0]==5 && header->version[1]>=3 &&
1426 (header->options&MBCS_OPT_UNKNOWN_INCOMPATIBLE_MASK)==0) {
1427 headerLength=header->options&MBCS_OPT_LENGTH_MASK;
1428 noFromU=(UBool)((header->options&MBCS_OPT_NO_FROM_U)!=0);
1429 } else {
1430 *pErrorCode=U_INVALID_TABLE_FORMAT;
1431 return;
1432 }
1433
1434 mbcsTable->outputType=(uint8_t)header->flags;
1435 if(noFromU && mbcsTable->outputType==MBCS_OUTPUT_1) {
1436 *pErrorCode=U_INVALID_TABLE_FORMAT;
1437 return;
1438 }
1439
1440 /* extension data, header version 4.2 and higher */
1441 offset=header->flags>>8;
1442 if(offset!=0) {
1443 mbcsTable->extIndexes=(const int32_t *)(raw+offset);
1444 }
1445
1446 if(mbcsTable->outputType==MBCS_OUTPUT_EXT_ONLY) {
1447 UConverterLoadArgs args={ 0 };
1448 UConverterSharedData *baseSharedData;
1449 const int32_t *extIndexes;
1450 const char *baseName;
1451
1452 /* extension-only file, load the base table and set values appropriately */
1453 if((extIndexes=mbcsTable->extIndexes)==NULL) {
1454 /* extension-only file without extension */
1455 *pErrorCode=U_INVALID_TABLE_FORMAT;
1456 return;
1457 }
1458
1459 if(pArgs->nestedLoads!=1) {
1460 /* an extension table must not be loaded as a base table */
1461 *pErrorCode=U_INVALID_TABLE_FILE;
1462 return;
1463 }
1464
1465 /* load the base table */
1466 baseName=(const char *)header+headerLength*4;
1467 if(0==uprv_strcmp(baseName, sharedData->staticData->name)) {
1468 /* forbid loading this same extension-only file */
1469 *pErrorCode=U_INVALID_TABLE_FORMAT;
1470 return;
1471 }
1472
1473 /* TODO parse package name out of the prefix of the base name in the extension .cnv file? */
1474 args.size=sizeof(UConverterLoadArgs);
1475 args.nestedLoads=2;
1476 args.onlyTestIsLoadable=pArgs->onlyTestIsLoadable;
1477 args.reserved=pArgs->reserved;
1478 args.options=pArgs->options;
1479 args.pkg=pArgs->pkg;
1480 args.name=baseName;
1481 baseSharedData=ucnv_load(&args, pErrorCode);
1482 if(U_FAILURE(*pErrorCode)) {
1483 return;
1484 }
1485 if( baseSharedData->staticData->conversionType!=UCNV_MBCS ||
1486 baseSharedData->mbcs.baseSharedData!=NULL
1487 ) {
1488 ucnv_unload(baseSharedData);
1489 *pErrorCode=U_INVALID_TABLE_FORMAT;
1490 return;
1491 }
1492 if(pArgs->onlyTestIsLoadable) {
1493 /*
1494 * Exit as soon as we know that we can load the converter
1495 * and the format is valid and supported.
1496 * The worst that can happen in the following code is a memory
1497 * allocation error.
1498 */
1499 ucnv_unload(baseSharedData);
1500 return;
1501 }
1502
1503 /* copy the base table data */
1504 uprv_memcpy(mbcsTable, &baseSharedData->mbcs, sizeof(UConverterMBCSTable));
1505
1506 /* overwrite values with relevant ones for the extension converter */
1507 mbcsTable->baseSharedData=baseSharedData;
1508 mbcsTable->extIndexes=extIndexes;
1509
1510 /*
1511 * It would be possible to share the swapLFNL data with a base converter,
1512 * but the generated name would have to be different, and the memory
1513 * would have to be free'd only once.
1514 * It is easier to just create the data for the extension converter
1515 * separately when it is requested.
1516 */
1517 mbcsTable->swapLFNLStateTable=NULL;
1518 mbcsTable->swapLFNLFromUnicodeBytes=NULL;
1519 mbcsTable->swapLFNLName=NULL;
1520
1521 /*
1522 * The reconstitutedData must be deleted only when the base converter
1523 * is unloaded.
1524 */
1525 mbcsTable->reconstitutedData=NULL;
1526
1527 /*
1528 * Set a special, runtime-only outputType if the extension converter
1529 * is a DBCS version of a base converter that also maps single bytes.
1530 */
1531 if( sharedData->staticData->conversionType==UCNV_DBCS ||
1532 (sharedData->staticData->conversionType==UCNV_MBCS &&
1533 sharedData->staticData->minBytesPerChar>=2)
1534 ) {
1535 if(baseSharedData->mbcs.outputType==MBCS_OUTPUT_2_SISO) {
1536 /* the base converter is SI/SO-stateful */
1537 int32_t entry;
1538
1539 /* get the dbcs state from the state table entry for SO=0x0e */
1540 entry=mbcsTable->stateTable[0][0xe];
1541 if( MBCS_ENTRY_IS_FINAL(entry) &&
1542 MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_CHANGE_ONLY &&
1543 MBCS_ENTRY_FINAL_STATE(entry)!=0
1544 ) {
1545 mbcsTable->dbcsOnlyState=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry);
1546
1547 mbcsTable->outputType=MBCS_OUTPUT_DBCS_ONLY;
1548 }
1549 } else if(
1550 baseSharedData->staticData->conversionType==UCNV_MBCS &&
1551 baseSharedData->staticData->minBytesPerChar==1 &&
1552 baseSharedData->staticData->maxBytesPerChar==2 &&
1553 mbcsTable->countStates<=127
1554 ) {
1555 /* non-stateful base converter, need to modify the state table */
1556 int32_t (*newStateTable)[256];
1557 int32_t *state;
1558 int32_t i, count;
1559
1560 /* allocate a new state table and copy the base state table contents */
1561 count=mbcsTable->countStates;
1562 newStateTable=(int32_t (*)[256])uprv_malloc((count+1)*1024);
1563 if(newStateTable==NULL) {
1564 ucnv_unload(baseSharedData);
1565 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
1566 return;
1567 }
1568
1569 uprv_memcpy(newStateTable, mbcsTable->stateTable, count*1024);
1570
1571 /* change all final single-byte entries to go to a new all-illegal state */
1572 state=newStateTable[0];
1573 for(i=0; i<256; ++i) {
1574 if(MBCS_ENTRY_IS_FINAL(state[i])) {
1575 state[i]=MBCS_ENTRY_TRANSITION(count, 0);
1576 }
1577 }
1578
1579 /* build the new all-illegal state */
1580 state=newStateTable[count];
1581 for(i=0; i<256; ++i) {
1582 state[i]=MBCS_ENTRY_FINAL(0, MBCS_STATE_ILLEGAL, 0);
1583 }
1584 mbcsTable->stateTable=(const int32_t (*)[256])newStateTable;
1585 mbcsTable->countStates=(uint8_t)(count+1);
1586 mbcsTable->stateTableOwned=TRUE;
1587
1588 mbcsTable->outputType=MBCS_OUTPUT_DBCS_ONLY;
1589 }
1590 }
1591
1592 /*
1593 * unlike below for files with base tables, do not get the unicodeMask
1594 * from the sharedData; instead, use the base table's unicodeMask,
1595 * which we copied in the memcpy above;
1596 * this is necessary because the static data unicodeMask, especially
1597 * the UCNV_HAS_SUPPLEMENTARY flag, is part of the base table data
1598 */
1599 } else {
1600 /* conversion file with a base table; an additional extension table is optional */
1601 /* make sure that the output type is known */
1602 switch(mbcsTable->outputType) {
1603 case MBCS_OUTPUT_1:
1604 case MBCS_OUTPUT_2:
1605 case MBCS_OUTPUT_3:
1606 case MBCS_OUTPUT_4:
1607 case MBCS_OUTPUT_3_EUC:
1608 case MBCS_OUTPUT_4_EUC:
1609 case MBCS_OUTPUT_2_SISO:
1610 /* OK */
1611 break;
1612 default:
1613 *pErrorCode=U_INVALID_TABLE_FORMAT;
1614 return;
1615 }
1616 if(pArgs->onlyTestIsLoadable) {
1617 /*
1618 * Exit as soon as we know that we can load the converter
1619 * and the format is valid and supported.
1620 * The worst that can happen in the following code is a memory
1621 * allocation error.
1622 */
1623 return;
1624 }
1625
1626 mbcsTable->countStates=(uint8_t)header->countStates;
1627 mbcsTable->countToUFallbacks=header->countToUFallbacks;
1628 mbcsTable->stateTable=(const int32_t (*)[256])(raw+headerLength*4);
1629 mbcsTable->toUFallbacks=(const _MBCSToUFallback *)(mbcsTable->stateTable+header->countStates);
1630 mbcsTable->unicodeCodeUnits=(const uint16_t *)(raw+header->offsetToUCodeUnits);
1631
1632 mbcsTable->fromUnicodeTable=(const uint16_t *)(raw+header->offsetFromUTable);
1633 mbcsTable->fromUnicodeBytes=(const uint8_t *)(raw+header->offsetFromUBytes);
1634 mbcsTable->fromUBytesLength=header->fromUBytesLength;
1635
1636 /*
1637 * converter versions 6.1 and up contain a unicodeMask that is
1638 * used here to select the most efficient function implementations
1639 */
1640 info.size=sizeof(UDataInfo);
1641 udata_getInfo((UDataMemory *)sharedData->dataMemory, &info);
1642 if(info.formatVersion[0]>6 || (info.formatVersion[0]==6 && info.formatVersion[1]>=1)) {
1643 /* mask off possible future extensions to be safe */
1644 mbcsTable->unicodeMask=(uint8_t)(sharedData->staticData->unicodeMask&3);
1645 } else {
1646 /* for older versions, assume worst case: contains anything possible (prevent over-optimizations) */
1647 mbcsTable->unicodeMask=UCNV_HAS_SUPPLEMENTARY|UCNV_HAS_SURROGATES;
1648 }
1649
1650 /*
1651 * _MBCSHeader.version 4.3 adds utf8Friendly data structures.
1652 * Check for the header version, SBCS vs. MBCS, and for whether the
1653 * data structures are optimized for code points as high as what the
1654 * runtime code is designed for.
1655 * The implementation does not handle mapping tables with entries for
1656 * unpaired surrogates.
1657 */
1658 if( header->version[1]>=3 &&
1659 (mbcsTable->unicodeMask&UCNV_HAS_SURROGATES)==0 &&
1660 (mbcsTable->countStates==1 ?
1661 (header->version[2]>=(SBCS_FAST_MAX>>8)) :
1662 (header->version[2]>=(MBCS_FAST_MAX>>8))
1663 )
1664 ) {
1665 mbcsTable->utf8Friendly=TRUE;
1666
1667 if(mbcsTable->countStates==1) {
1668 /*
1669 * SBCS: Stage 3 is allocated in 64-entry blocks for U+0000..SBCS_FAST_MAX or higher.
1670 * Build a table with indexes to each block, to be used instead of
1671 * the regular stage 1/2 table.
1672 */
1673 int32_t i;
1674 for(i=0; i<(SBCS_FAST_LIMIT>>6); ++i) {
1675 mbcsTable->sbcsIndex[i]=mbcsTable->fromUnicodeTable[mbcsTable->fromUnicodeTable[i>>4]+((i<<2)&0x3c)];
1676 }
1677 /* set SBCS_FAST_MAX to reflect the reach of sbcsIndex[] even if header->version[2]>(SBCS_FAST_MAX>>8) */
1678 mbcsTable->maxFastUChar=SBCS_FAST_MAX;
1679 } else {
1680 /*
1681 * MBCS: Stage 3 is allocated in 64-entry blocks for U+0000..MBCS_FAST_MAX or higher.
1682 * The .cnv file is prebuilt with an additional stage table with indexes
1683 * to each block.
1684 */
1685 mbcsTable->mbcsIndex=(const uint16_t *)
1686 (mbcsTable->fromUnicodeBytes+
1687 (noFromU ? 0 : mbcsTable->fromUBytesLength));
1688 mbcsTable->maxFastUChar=(((UChar)header->version[2])<<8)|0xff;
1689 }
1690 }
1691
1692 /* calculate a bit set of 4 ASCII characters per bit that round-trip to ASCII bytes */
1693 {
1694 uint32_t asciiRoundtrips=0xffffffff;
1695 int32_t i;
1696
1697 for(i=0; i<0x80; ++i) {
1698 if(mbcsTable->stateTable[0][i]!=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, i)) {
1699 asciiRoundtrips&=~((uint32_t)1<<(i>>2));
1700 }
1701 }
1702 mbcsTable->asciiRoundtrips=asciiRoundtrips;
1703 }
1704
1705 if(noFromU) {
1706 uint32_t stage1Length=
1707 mbcsTable->unicodeMask&UCNV_HAS_SUPPLEMENTARY ?
1708 0x440 : 0x40;
1709 uint32_t stage2Length=
1710 (header->offsetFromUBytes-header->offsetFromUTable)/4-
1711 stage1Length/2;
1712 reconstituteData(mbcsTable, stage1Length, stage2Length, header->fullStage2Length, pErrorCode);
1713 }
1714 }
1715
1716 /* Set the impl pointer here so that it is set for both extension-only and base tables. */
1717 if(mbcsTable->utf8Friendly) {
1718 if(mbcsTable->countStates==1) {
1719 sharedData->impl=&_SBCSUTF8Impl;
1720 } else {
1721 if(mbcsTable->outputType==MBCS_OUTPUT_2) {
1722 sharedData->impl=&_DBCSUTF8Impl;
1723 }
1724 }
1725 }
1726
1727 if(mbcsTable->outputType==MBCS_OUTPUT_DBCS_ONLY || mbcsTable->outputType==MBCS_OUTPUT_2_SISO) {
1728 /*
1729 * MBCS_OUTPUT_DBCS_ONLY: No SBCS mappings, therefore ASCII does not roundtrip.
1730 * MBCS_OUTPUT_2_SISO: Bypass the ASCII fastpath to handle prevLength correctly.
1731 */
1732 mbcsTable->asciiRoundtrips=0;
1733 }
1734 }
1735
1736 static void
1737 ucnv_MBCSUnload(UConverterSharedData *sharedData) {
1738 UConverterMBCSTable *mbcsTable=&sharedData->mbcs;
1739
1740 if(mbcsTable->swapLFNLStateTable!=NULL) {
1741 uprv_free(mbcsTable->swapLFNLStateTable);
1742 }
1743 if(mbcsTable->stateTableOwned) {
1744 uprv_free((void *)mbcsTable->stateTable);
1745 }
1746 if(mbcsTable->baseSharedData!=NULL) {
1747 ucnv_unload(mbcsTable->baseSharedData);
1748 }
1749 if(mbcsTable->reconstitutedData!=NULL) {
1750 uprv_free(mbcsTable->reconstitutedData);
1751 }
1752 }
1753
1754 static void
1755 ucnv_MBCSOpen(UConverter *cnv,
1756 UConverterLoadArgs *pArgs,
1757 UErrorCode *pErrorCode) {
1758 UConverterMBCSTable *mbcsTable;
1759 const int32_t *extIndexes;
1760 uint8_t outputType;
1761 int8_t maxBytesPerUChar;
1762
1763 if(pArgs->onlyTestIsLoadable) {
1764 return;
1765 }
1766
1767 mbcsTable=&cnv->sharedData->mbcs;
1768 outputType=mbcsTable->outputType;
1769
1770 if(outputType==MBCS_OUTPUT_DBCS_ONLY) {
1771 /* the swaplfnl option does not apply, remove it */
1772 cnv->options=pArgs->options&=~UCNV_OPTION_SWAP_LFNL;
1773 }
1774
1775 if((pArgs->options&UCNV_OPTION_SWAP_LFNL)!=0) {
1776 /* do this because double-checked locking is broken */
1777 UBool isCached;
1778
1779 umtx_lock(NULL);
1780 isCached=mbcsTable->swapLFNLStateTable!=NULL;
1781 umtx_unlock(NULL);
1782
1783 if(!isCached) {
1784 if(!_EBCDICSwapLFNL(cnv->sharedData, pErrorCode)) {
1785 if(U_FAILURE(*pErrorCode)) {
1786 return; /* something went wrong */
1787 }
1788
1789 /* the option does not apply, remove it */
1790 cnv->options=pArgs->options&=~UCNV_OPTION_SWAP_LFNL;
1791 }
1792 }
1793 }
1794
1795 if(uprv_strstr(pArgs->name, "18030")!=NULL) {
1796 if(uprv_strstr(pArgs->name, "gb18030")!=NULL || uprv_strstr(pArgs->name, "GB18030")!=NULL) {
1797 /* set a flag for GB 18030 mode, which changes the callback behavior */
1798 cnv->options|=_MBCS_OPTION_GB18030;
1799 }
1800 } else if((uprv_strstr(pArgs->name, "KEIS")!=NULL) || (uprv_strstr(pArgs->name, "keis")!=NULL)) {
1801 /* set a flag for KEIS converter, which changes the SI/SO character sequence */
1802 cnv->options|=_MBCS_OPTION_KEIS;
1803 } else if((uprv_strstr(pArgs->name, "JEF")!=NULL) || (uprv_strstr(pArgs->name, "jef")!=NULL)) {
1804 /* set a flag for JEF converter, which changes the SI/SO character sequence */
1805 cnv->options|=_MBCS_OPTION_JEF;
1806 } else if((uprv_strstr(pArgs->name, "JIPS")!=NULL) || (uprv_strstr(pArgs->name, "jips")!=NULL)) {
1807 /* set a flag for JIPS converter, which changes the SI/SO character sequence */
1808 cnv->options|=_MBCS_OPTION_JIPS;
1809 }
1810
1811 /* fix maxBytesPerUChar depending on outputType and options etc. */
1812 if(outputType==MBCS_OUTPUT_2_SISO) {
1813 cnv->maxBytesPerUChar=3; /* SO+DBCS */
1814 }
1815
1816 extIndexes=mbcsTable->extIndexes;
1817 if(extIndexes!=NULL) {
1818 maxBytesPerUChar=(int8_t)UCNV_GET_MAX_BYTES_PER_UCHAR(extIndexes);
1819 if(outputType==MBCS_OUTPUT_2_SISO) {
1820 ++maxBytesPerUChar; /* SO + multiple DBCS */
1821 }
1822
1823 if(maxBytesPerUChar>cnv->maxBytesPerUChar) {
1824 cnv->maxBytesPerUChar=maxBytesPerUChar;
1825 }
1826 }
1827
1828 #if 0
1829 /*
1830 * documentation of UConverter fields used for status
1831 * all of these fields are (re)set to 0 by ucnv_bld.c and ucnv_reset()
1832 */
1833
1834 /* toUnicode */
1835 cnv->toUnicodeStatus=0; /* offset */
1836 cnv->mode=0; /* state */
1837 cnv->toULength=0; /* byteIndex */
1838
1839 /* fromUnicode */
1840 cnv->fromUChar32=0;
1841 cnv->fromUnicodeStatus=1; /* prevLength */
1842 #endif
1843 }
1844
1845 static const char *
1846 ucnv_MBCSGetName(const UConverter *cnv) {
1847 if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0 && cnv->sharedData->mbcs.swapLFNLName!=NULL) {
1848 return cnv->sharedData->mbcs.swapLFNLName;
1849 } else {
1850 return cnv->sharedData->staticData->name;
1851 }
1852 }
1853
1854 /* MBCS-to-Unicode conversion functions ------------------------------------- */
1855
1856 static UChar32
1857 ucnv_MBCSGetFallback(UConverterMBCSTable *mbcsTable, uint32_t offset) {
1858 const _MBCSToUFallback *toUFallbacks;
1859 uint32_t i, start, limit;
1860
1861 limit=mbcsTable->countToUFallbacks;
1862 if(limit>0) {
1863 /* do a binary search for the fallback mapping */
1864 toUFallbacks=mbcsTable->toUFallbacks;
1865 start=0;
1866 while(start<limit-1) {
1867 i=(start+limit)/2;
1868 if(offset<toUFallbacks[i].offset) {
1869 limit=i;
1870 } else {
1871 start=i;
1872 }
1873 }
1874
1875 /* did we really find it? */
1876 if(offset==toUFallbacks[start].offset) {
1877 return toUFallbacks[start].codePoint;
1878 }
1879 }
1880
1881 return 0xfffe;
1882 }
1883
1884 /* This version of ucnv_MBCSToUnicodeWithOffsets() is optimized for single-byte, single-state codepages. */
1885 static void
1886 ucnv_MBCSSingleToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs,
1887 UErrorCode *pErrorCode) {
1888 UConverter *cnv;
1889 const uint8_t *source, *sourceLimit;
1890 UChar *target;
1891 const UChar *targetLimit;
1892 int32_t *offsets;
1893
1894 const int32_t (*stateTable)[256];
1895
1896 int32_t sourceIndex;
1897
1898 int32_t entry;
1899 UChar c;
1900 uint8_t action;
1901
1902 /* set up the local pointers */
1903 cnv=pArgs->converter;
1904 source=(const uint8_t *)pArgs->source;
1905 sourceLimit=(const uint8_t *)pArgs->sourceLimit;
1906 target=pArgs->target;
1907 targetLimit=pArgs->targetLimit;
1908 offsets=pArgs->offsets;
1909
1910 if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
1911 stateTable=(const int32_t (*)[256])cnv->sharedData->mbcs.swapLFNLStateTable;
1912 } else {
1913 stateTable=cnv->sharedData->mbcs.stateTable;
1914 }
1915
1916 /* sourceIndex=-1 if the current character began in the previous buffer */
1917 sourceIndex=0;
1918
1919 /* conversion loop */
1920 while(source<sourceLimit) {
1921 /*
1922 * This following test is to see if available input would overflow the output.
1923 * It does not catch output of more than one code unit that
1924 * overflows as a result of a surrogate pair or callback output
1925 * from the last source byte.
1926 * Therefore, those situations also test for overflows and will
1927 * then break the loop, too.
1928 */
1929 if(target>=targetLimit) {
1930 /* target is full */
1931 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
1932 break;
1933 }
1934
1935 entry=stateTable[0][*source++];
1936 /* MBCS_ENTRY_IS_FINAL(entry) */
1937
1938 /* test the most common case first */
1939 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
1940 /* output BMP code point */
1941 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
1942 if(offsets!=NULL) {
1943 *offsets++=sourceIndex;
1944 }
1945
1946 /* normal end of action codes: prepare for a new character */
1947 ++sourceIndex;
1948 continue;
1949 }
1950
1951 /*
1952 * An if-else-if chain provides more reliable performance for
1953 * the most common cases compared to a switch.
1954 */
1955 action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
1956 if(action==MBCS_STATE_VALID_DIRECT_20 ||
1957 (action==MBCS_STATE_FALLBACK_DIRECT_20 && UCNV_TO_U_USE_FALLBACK(cnv))
1958 ) {
1959 entry=MBCS_ENTRY_FINAL_VALUE(entry);
1960 /* output surrogate pair */
1961 *target++=(UChar)(0xd800|(UChar)(entry>>10));
1962 if(offsets!=NULL) {
1963 *offsets++=sourceIndex;
1964 }
1965 c=(UChar)(0xdc00|(UChar)(entry&0x3ff));
1966 if(target<targetLimit) {
1967 *target++=c;
1968 if(offsets!=NULL) {
1969 *offsets++=sourceIndex;
1970 }
1971 } else {
1972 /* target overflow */
1973 cnv->UCharErrorBuffer[0]=c;
1974 cnv->UCharErrorBufferLength=1;
1975 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
1976 break;
1977 }
1978
1979 ++sourceIndex;
1980 continue;
1981 } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
1982 if(UCNV_TO_U_USE_FALLBACK(cnv)) {
1983 /* output BMP code point */
1984 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
1985 if(offsets!=NULL) {
1986 *offsets++=sourceIndex;
1987 }
1988
1989 ++sourceIndex;
1990 continue;
1991 }
1992 } else if(action==MBCS_STATE_UNASSIGNED) {
1993 /* just fall through */
1994 } else if(action==MBCS_STATE_ILLEGAL) {
1995 /* callback(illegal) */
1996 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
1997 } else {
1998 /* reserved, must never occur */
1999 ++sourceIndex;
2000 continue;
2001 }
2002
2003 if(U_FAILURE(*pErrorCode)) {
2004 /* callback(illegal) */
2005 break;
2006 } else /* unassigned sequences indicated with byteIndex>0 */ {
2007 /* try an extension mapping */
2008 pArgs->source=(const char *)source;
2009 cnv->toUBytes[0]=*(source-1);
2010 cnv->toULength=_extToU(cnv, cnv->sharedData,
2011 1, &source, sourceLimit,
2012 &target, targetLimit,
2013 &offsets, sourceIndex,
2014 pArgs->flush,
2015 pErrorCode);
2016 sourceIndex+=1+(int32_t)(source-(const uint8_t *)pArgs->source);
2017
2018 if(U_FAILURE(*pErrorCode)) {
2019 /* not mappable or buffer overflow */
2020 break;
2021 }
2022 }
2023 }
2024
2025 /* write back the updated pointers */
2026 pArgs->source=(const char *)source;
2027 pArgs->target=target;
2028 pArgs->offsets=offsets;
2029 }
2030
2031 /*
2032 * This version of ucnv_MBCSSingleToUnicodeWithOffsets() is optimized for single-byte, single-state codepages
2033 * that only map to and from the BMP.
2034 * In addition to single-byte optimizations, the offset calculations
2035 * become much easier.
2036 */
2037 static void
2038 ucnv_MBCSSingleToBMPWithOffsets(UConverterToUnicodeArgs *pArgs,
2039 UErrorCode *pErrorCode) {
2040 UConverter *cnv;
2041 const uint8_t *source, *sourceLimit, *lastSource;
2042 UChar *target;
2043 int32_t targetCapacity, length;
2044 int32_t *offsets;
2045
2046 const int32_t (*stateTable)[256];
2047
2048 int32_t sourceIndex;
2049
2050 int32_t entry;
2051 uint8_t action;
2052
2053 /* set up the local pointers */
2054 cnv=pArgs->converter;
2055 source=(const uint8_t *)pArgs->source;
2056 sourceLimit=(const uint8_t *)pArgs->sourceLimit;
2057 target=pArgs->target;
2058 targetCapacity=(int32_t)(pArgs->targetLimit-pArgs->target);
2059 offsets=pArgs->offsets;
2060
2061 if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
2062 stateTable=(const int32_t (*)[256])cnv->sharedData->mbcs.swapLFNLStateTable;
2063 } else {
2064 stateTable=cnv->sharedData->mbcs.stateTable;
2065 }
2066
2067 /* sourceIndex=-1 if the current character began in the previous buffer */
2068 sourceIndex=0;
2069 lastSource=source;
2070
2071 /*
2072 * since the conversion here is 1:1 UChar:uint8_t, we need only one counter
2073 * for the minimum of the sourceLength and targetCapacity
2074 */
2075 length=(int32_t)(sourceLimit-source);
2076 if(length<targetCapacity) {
2077 targetCapacity=length;
2078 }
2079
2080 #if MBCS_UNROLL_SINGLE_TO_BMP
2081 /* unrolling makes it faster on Pentium III/Windows 2000 */
2082 /* unroll the loop with the most common case */
2083 unrolled:
2084 if(targetCapacity>=16) {
2085 int32_t count, loops, oredEntries;
2086
2087 loops=count=targetCapacity>>4;
2088 do {
2089 oredEntries=entry=stateTable[0][*source++];
2090 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2091 oredEntries|=entry=stateTable[0][*source++];
2092 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2093 oredEntries|=entry=stateTable[0][*source++];
2094 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2095 oredEntries|=entry=stateTable[0][*source++];
2096 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2097 oredEntries|=entry=stateTable[0][*source++];
2098 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2099 oredEntries|=entry=stateTable[0][*source++];
2100 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2101 oredEntries|=entry=stateTable[0][*source++];
2102 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2103 oredEntries|=entry=stateTable[0][*source++];
2104 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2105 oredEntries|=entry=stateTable[0][*source++];
2106 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2107 oredEntries|=entry=stateTable[0][*source++];
2108 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2109 oredEntries|=entry=stateTable[0][*source++];
2110 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2111 oredEntries|=entry=stateTable[0][*source++];
2112 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2113 oredEntries|=entry=stateTable[0][*source++];
2114 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2115 oredEntries|=entry=stateTable[0][*source++];
2116 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2117 oredEntries|=entry=stateTable[0][*source++];
2118 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2119 oredEntries|=entry=stateTable[0][*source++];
2120 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2121
2122 /* were all 16 entries really valid? */
2123 if(!MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(oredEntries)) {
2124 /* no, return to the first of these 16 */
2125 source-=16;
2126 target-=16;
2127 break;
2128 }
2129 } while(--count>0);
2130 count=loops-count;
2131 targetCapacity-=16*count;
2132
2133 if(offsets!=NULL) {
2134 lastSource+=16*count;
2135 while(count>0) {
2136 *offsets++=sourceIndex++;
2137 *offsets++=sourceIndex++;
2138 *offsets++=sourceIndex++;
2139 *offsets++=sourceIndex++;
2140 *offsets++=sourceIndex++;
2141 *offsets++=sourceIndex++;
2142 *offsets++=sourceIndex++;
2143 *offsets++=sourceIndex++;
2144 *offsets++=sourceIndex++;
2145 *offsets++=sourceIndex++;
2146 *offsets++=sourceIndex++;
2147 *offsets++=sourceIndex++;
2148 *offsets++=sourceIndex++;
2149 *offsets++=sourceIndex++;
2150 *offsets++=sourceIndex++;
2151 *offsets++=sourceIndex++;
2152 --count;
2153 }
2154 }
2155 }
2156 #endif
2157
2158 /* conversion loop */
2159 while(targetCapacity > 0 && source < sourceLimit) {
2160 entry=stateTable[0][*source++];
2161 /* MBCS_ENTRY_IS_FINAL(entry) */
2162
2163 /* test the most common case first */
2164 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
2165 /* output BMP code point */
2166 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2167 --targetCapacity;
2168 continue;
2169 }
2170
2171 /*
2172 * An if-else-if chain provides more reliable performance for
2173 * the most common cases compared to a switch.
2174 */
2175 action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
2176 if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
2177 if(UCNV_TO_U_USE_FALLBACK(cnv)) {
2178 /* output BMP code point */
2179 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2180 --targetCapacity;
2181 continue;
2182 }
2183 } else if(action==MBCS_STATE_UNASSIGNED) {
2184 /* just fall through */
2185 } else if(action==MBCS_STATE_ILLEGAL) {
2186 /* callback(illegal) */
2187 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2188 } else {
2189 /* reserved, must never occur */
2190 continue;
2191 }
2192
2193 /* set offsets since the start or the last extension */
2194 if(offsets!=NULL) {
2195 int32_t count=(int32_t)(source-lastSource);
2196
2197 /* predecrement: do not set the offset for the callback-causing character */
2198 while(--count>0) {
2199 *offsets++=sourceIndex++;
2200 }
2201 /* offset and sourceIndex are now set for the current character */
2202 }
2203
2204 if(U_FAILURE(*pErrorCode)) {
2205 /* callback(illegal) */
2206 break;
2207 } else /* unassigned sequences indicated with byteIndex>0 */ {
2208 /* try an extension mapping */
2209 lastSource=source;
2210 cnv->toUBytes[0]=*(source-1);
2211 cnv->toULength=_extToU(cnv, cnv->sharedData,
2212 1, &source, sourceLimit,
2213 &target, pArgs->targetLimit,
2214 &offsets, sourceIndex,
2215 pArgs->flush,
2216 pErrorCode);
2217 sourceIndex+=1+(int32_t)(source-lastSource);
2218
2219 if(U_FAILURE(*pErrorCode)) {
2220 /* not mappable or buffer overflow */
2221 break;
2222 }
2223
2224 /* recalculate the targetCapacity after an extension mapping */
2225 targetCapacity=(int32_t)(pArgs->targetLimit-target);
2226 length=(int32_t)(sourceLimit-source);
2227 if(length<targetCapacity) {
2228 targetCapacity=length;
2229 }
2230 }
2231
2232 #if MBCS_UNROLL_SINGLE_TO_BMP
2233 /* unrolling makes it faster on Pentium III/Windows 2000 */
2234 goto unrolled;
2235 #endif
2236 }
2237
2238 if(U_SUCCESS(*pErrorCode) && source<sourceLimit && target>=pArgs->targetLimit) {
2239 /* target is full */
2240 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
2241 }
2242
2243 /* set offsets since the start or the last callback */
2244 if(offsets!=NULL) {
2245 size_t count=source-lastSource;
2246 while(count>0) {
2247 *offsets++=sourceIndex++;
2248 --count;
2249 }
2250 }
2251
2252 /* write back the updated pointers */
2253 pArgs->source=(const char *)source;
2254 pArgs->target=target;
2255 pArgs->offsets=offsets;
2256 }
2257
2258 static UBool
2259 hasValidTrailBytes(const int32_t (*stateTable)[256], uint8_t state) {
2260 const int32_t *row=stateTable[state];
2261 int32_t b, entry;
2262 /* First test for final entries in this state for some commonly valid byte values. */
2263 entry=row[0xa1];
2264 if( !MBCS_ENTRY_IS_TRANSITION(entry) &&
2265 MBCS_ENTRY_FINAL_ACTION(entry)!=MBCS_STATE_ILLEGAL
2266 ) {
2267 return TRUE;
2268 }
2269 entry=row[0x41];
2270 if( !MBCS_ENTRY_IS_TRANSITION(entry) &&
2271 MBCS_ENTRY_FINAL_ACTION(entry)!=MBCS_STATE_ILLEGAL
2272 ) {
2273 return TRUE;
2274 }
2275 /* Then test for final entries in this state. */
2276 for(b=0; b<=0xff; ++b) {
2277 entry=row[b];
2278 if( !MBCS_ENTRY_IS_TRANSITION(entry) &&
2279 MBCS_ENTRY_FINAL_ACTION(entry)!=MBCS_STATE_ILLEGAL
2280 ) {
2281 return TRUE;
2282 }
2283 }
2284 /* Then recurse for transition entries. */
2285 for(b=0; b<=0xff; ++b) {
2286 entry=row[b];
2287 if( MBCS_ENTRY_IS_TRANSITION(entry) &&
2288 hasValidTrailBytes(stateTable, (uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry))
2289 ) {
2290 return TRUE;
2291 }
2292 }
2293 return FALSE;
2294 }
2295
2296 /*
2297 * Is byte b a single/lead byte in this state?
2298 * Recurse for transition states, because here we don't want to say that
2299 * b is a lead byte if all byte sequences that start with b are illegal.
2300 */
2301 static UBool
2302 isSingleOrLead(const int32_t (*stateTable)[256], uint8_t state, UBool isDBCSOnly, uint8_t b) {
2303 const int32_t *row=stateTable[state];
2304 int32_t entry=row[b];
2305 if(MBCS_ENTRY_IS_TRANSITION(entry)) { /* lead byte */
2306 return hasValidTrailBytes(stateTable, (uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry));
2307 } else {
2308 uint8_t action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
2309 if(action==MBCS_STATE_CHANGE_ONLY && isDBCSOnly) {
2310 return FALSE; /* SI/SO are illegal for DBCS-only conversion */
2311 } else {
2312 return action!=MBCS_STATE_ILLEGAL;
2313 }
2314 }
2315 }
2316
2317 U_CFUNC void
2318 ucnv_MBCSToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs,
2319 UErrorCode *pErrorCode) {
2320 UConverter *cnv;
2321 const uint8_t *source, *sourceLimit;
2322 UChar *target;
2323 const UChar *targetLimit;
2324 int32_t *offsets;
2325
2326 const int32_t (*stateTable)[256];
2327 const uint16_t *unicodeCodeUnits;
2328
2329 uint32_t offset;
2330 uint8_t state;
2331 int8_t byteIndex;
2332 uint8_t *bytes;
2333
2334 int32_t sourceIndex, nextSourceIndex;
2335
2336 int32_t entry;
2337 UChar c;
2338 uint8_t action;
2339
2340 /* use optimized function if possible */
2341 cnv=pArgs->converter;
2342
2343 if(cnv->preToULength>0) {
2344 /*
2345 * pass sourceIndex=-1 because we continue from an earlier buffer
2346 * in the future, this may change with continuous offsets
2347 */
2348 ucnv_extContinueMatchToU(cnv, pArgs, -1, pErrorCode);
2349
2350 if(U_FAILURE(*pErrorCode) || cnv->preToULength<0) {
2351 return;
2352 }
2353 }
2354
2355 if(cnv->sharedData->mbcs.countStates==1) {
2356 if(!(cnv->sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
2357 ucnv_MBCSSingleToBMPWithOffsets(pArgs, pErrorCode);
2358 } else {
2359 ucnv_MBCSSingleToUnicodeWithOffsets(pArgs, pErrorCode);
2360 }
2361 return;
2362 }
2363
2364 /* set up the local pointers */
2365 source=(const uint8_t *)pArgs->source;
2366 sourceLimit=(const uint8_t *)pArgs->sourceLimit;
2367 target=pArgs->target;
2368 targetLimit=pArgs->targetLimit;
2369 offsets=pArgs->offsets;
2370
2371 if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
2372 stateTable=(const int32_t (*)[256])cnv->sharedData->mbcs.swapLFNLStateTable;
2373 } else {
2374 stateTable=cnv->sharedData->mbcs.stateTable;
2375 }
2376 unicodeCodeUnits=cnv->sharedData->mbcs.unicodeCodeUnits;
2377
2378 /* get the converter state from UConverter */
2379 offset=cnv->toUnicodeStatus;
2380 byteIndex=cnv->toULength;
2381 bytes=cnv->toUBytes;
2382
2383 /*
2384 * if we are in the SBCS state for a DBCS-only converter,
2385 * then load the DBCS state from the MBCS data
2386 * (dbcsOnlyState==0 if it is not a DBCS-only converter)
2387 */
2388 if((state=(uint8_t)(cnv->mode))==0) {
2389 state=cnv->sharedData->mbcs.dbcsOnlyState;
2390 }
2391
2392 /* sourceIndex=-1 if the current character began in the previous buffer */
2393 sourceIndex=byteIndex==0 ? 0 : -1;
2394 nextSourceIndex=0;
2395
2396 /* conversion loop */
2397 while(source<sourceLimit) {
2398 /*
2399 * This following test is to see if available input would overflow the output.
2400 * It does not catch output of more than one code unit that
2401 * overflows as a result of a surrogate pair or callback output
2402 * from the last source byte.
2403 * Therefore, those situations also test for overflows and will
2404 * then break the loop, too.
2405 */
2406 if(target>=targetLimit) {
2407 /* target is full */
2408 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
2409 break;
2410 }
2411
2412 if(byteIndex==0) {
2413 /* optimized loop for 1/2-byte input and BMP output */
2414 if(offsets==NULL) {
2415 do {
2416 entry=stateTable[state][*source];
2417 if(MBCS_ENTRY_IS_TRANSITION(entry)) {
2418 state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
2419 offset=MBCS_ENTRY_TRANSITION_OFFSET(entry);
2420
2421 ++source;
2422 if( source<sourceLimit &&
2423 MBCS_ENTRY_IS_FINAL(entry=stateTable[state][*source]) &&
2424 MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_VALID_16 &&
2425 (c=unicodeCodeUnits[offset+MBCS_ENTRY_FINAL_VALUE_16(entry)])<0xfffe
2426 ) {
2427 ++source;
2428 *target++=c;
2429 state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
2430 offset=0;
2431 } else {
2432 /* set the state and leave the optimized loop */
2433 bytes[0]=*(source-1);
2434 byteIndex=1;
2435 break;
2436 }
2437 } else {
2438 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
2439 /* output BMP code point */
2440 ++source;
2441 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2442 state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
2443 } else {
2444 /* leave the optimized loop */
2445 break;
2446 }
2447 }
2448 } while(source<sourceLimit && target<targetLimit);
2449 } else /* offsets!=NULL */ {
2450 do {
2451 entry=stateTable[state][*source];
2452 if(MBCS_ENTRY_IS_TRANSITION(entry)) {
2453 state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
2454 offset=MBCS_ENTRY_TRANSITION_OFFSET(entry);
2455
2456 ++source;
2457 if( source<sourceLimit &&
2458 MBCS_ENTRY_IS_FINAL(entry=stateTable[state][*source]) &&
2459 MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_VALID_16 &&
2460 (c=unicodeCodeUnits[offset+MBCS_ENTRY_FINAL_VALUE_16(entry)])<0xfffe
2461 ) {
2462 ++source;
2463 *target++=c;
2464 if(offsets!=NULL) {
2465 *offsets++=sourceIndex;
2466 sourceIndex=(nextSourceIndex+=2);
2467 }
2468 state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
2469 offset=0;
2470 } else {
2471 /* set the state and leave the optimized loop */
2472 ++nextSourceIndex;
2473 bytes[0]=*(source-1);
2474 byteIndex=1;
2475 break;
2476 }
2477 } else {
2478 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
2479 /* output BMP code point */
2480 ++source;
2481 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2482 if(offsets!=NULL) {
2483 *offsets++=sourceIndex;
2484 sourceIndex=++nextSourceIndex;
2485 }
2486 state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
2487 } else {
2488 /* leave the optimized loop */
2489 break;
2490 }
2491 }
2492 } while(source<sourceLimit && target<targetLimit);
2493 }
2494
2495 /*
2496 * these tests and break statements could be put inside the loop
2497 * if C had "break outerLoop" like Java
2498 */
2499 if(source>=sourceLimit) {
2500 break;
2501 }
2502 if(target>=targetLimit) {
2503 /* target is full */
2504 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
2505 break;
2506 }
2507
2508 ++nextSourceIndex;
2509 bytes[byteIndex++]=*source++;
2510 } else /* byteIndex>0 */ {
2511 ++nextSourceIndex;
2512 entry=stateTable[state][bytes[byteIndex++]=*source++];
2513 }
2514
2515 if(MBCS_ENTRY_IS_TRANSITION(entry)) {
2516 state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
2517 offset+=MBCS_ENTRY_TRANSITION_OFFSET(entry);
2518 continue;
2519 }
2520
2521 /* save the previous state for proper extension mapping with SI/SO-stateful converters */
2522 cnv->mode=state;
2523
2524 /* set the next state early so that we can reuse the entry variable */
2525 state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
2526
2527 /*
2528 * An if-else-if chain provides more reliable performance for
2529 * the most common cases compared to a switch.
2530 */
2531 action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
2532 if(action==MBCS_STATE_VALID_16) {
2533 offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
2534 c=unicodeCodeUnits[offset];
2535 if(c<0xfffe) {
2536 /* output BMP code point */
2537 *target++=c;
2538 if(offsets!=NULL) {
2539 *offsets++=sourceIndex;
2540 }
2541 byteIndex=0;
2542 } else if(c==0xfffe) {
2543 if(UCNV_TO_U_USE_FALLBACK(cnv) && (entry=(int32_t)ucnv_MBCSGetFallback(&cnv->sharedData->mbcs, offset))!=0xfffe) {
2544 /* output fallback BMP code point */
2545 *target++=(UChar)entry;
2546 if(offsets!=NULL) {
2547 *offsets++=sourceIndex;
2548 }
2549 byteIndex=0;
2550 }
2551 } else {
2552 /* callback(illegal) */
2553 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2554 }
2555 } else if(action==MBCS_STATE_VALID_DIRECT_16) {
2556 /* output BMP code point */
2557 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2558 if(offsets!=NULL) {
2559 *offsets++=sourceIndex;
2560 }
2561 byteIndex=0;
2562 } else if(action==MBCS_STATE_VALID_16_PAIR) {
2563 offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
2564 c=unicodeCodeUnits[offset++];
2565 if(c<0xd800) {
2566 /* output BMP code point below 0xd800 */
2567 *target++=c;
2568 if(offsets!=NULL) {
2569 *offsets++=sourceIndex;
2570 }
2571 byteIndex=0;
2572 } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? c<=0xdfff : c<=0xdbff) {
2573 /* output roundtrip or fallback surrogate pair */
2574 *target++=(UChar)(c&0xdbff);
2575 if(offsets!=NULL) {
2576 *offsets++=sourceIndex;
2577 }
2578 byteIndex=0;
2579 if(target<targetLimit) {
2580 *target++=unicodeCodeUnits[offset];
2581 if(offsets!=NULL) {
2582 *offsets++=sourceIndex;
2583 }
2584 } else {
2585 /* target overflow */
2586 cnv->UCharErrorBuffer[0]=unicodeCodeUnits[offset];
2587 cnv->UCharErrorBufferLength=1;
2588 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
2589
2590 offset=0;
2591 break;
2592 }
2593 } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? (c&0xfffe)==0xe000 : c==0xe000) {
2594 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
2595 *target++=unicodeCodeUnits[offset];
2596 if(offsets!=NULL) {
2597 *offsets++=sourceIndex;
2598 }
2599 byteIndex=0;
2600 } else if(c==0xffff) {
2601 /* callback(illegal) */
2602 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2603 }
2604 } else if(action==MBCS_STATE_VALID_DIRECT_20 ||
2605 (action==MBCS_STATE_FALLBACK_DIRECT_20 && UCNV_TO_U_USE_FALLBACK(cnv))
2606 ) {
2607 entry=MBCS_ENTRY_FINAL_VALUE(entry);
2608 /* output surrogate pair */
2609 *target++=(UChar)(0xd800|(UChar)(entry>>10));
2610 if(offsets!=NULL) {
2611 *offsets++=sourceIndex;
2612 }
2613 byteIndex=0;
2614 c=(UChar)(0xdc00|(UChar)(entry&0x3ff));
2615 if(target<targetLimit) {
2616 *target++=c;
2617 if(offsets!=NULL) {
2618 *offsets++=sourceIndex;
2619 }
2620 } else {
2621 /* target overflow */
2622 cnv->UCharErrorBuffer[0]=c;
2623 cnv->UCharErrorBufferLength=1;
2624 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
2625
2626 offset=0;
2627 break;
2628 }
2629 } else if(action==MBCS_STATE_CHANGE_ONLY) {
2630 /*
2631 * This serves as a state change without any output.
2632 * It is useful for reading simple stateful encodings,
2633 * for example using just Shift-In/Shift-Out codes.
2634 * The 21 unused bits may later be used for more sophisticated
2635 * state transitions.
2636 */
2637 if(cnv->sharedData->mbcs.dbcsOnlyState==0) {
2638 byteIndex=0;
2639 } else {
2640 /* SI/SO are illegal for DBCS-only conversion */
2641 state=(uint8_t)(cnv->mode); /* restore the previous state */
2642
2643 /* callback(illegal) */
2644 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2645 }
2646 } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
2647 if(UCNV_TO_U_USE_FALLBACK(cnv)) {
2648 /* output BMP code point */
2649 *target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2650 if(offsets!=NULL) {
2651 *offsets++=sourceIndex;
2652 }
2653 byteIndex=0;
2654 }
2655 } else if(action==MBCS_STATE_UNASSIGNED) {
2656 /* just fall through */
2657 } else if(action==MBCS_STATE_ILLEGAL) {
2658 /* callback(illegal) */
2659 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2660 } else {
2661 /* reserved, must never occur */
2662 byteIndex=0;
2663 }
2664
2665 /* end of action codes: prepare for a new character */
2666 offset=0;
2667
2668 if(byteIndex==0) {
2669 sourceIndex=nextSourceIndex;
2670 } else if(U_FAILURE(*pErrorCode)) {
2671 /* callback(illegal) */
2672 if(byteIndex>1) {
2673 /*
2674 * Ticket 5691: consistent illegal sequences:
2675 * - We include at least the first byte in the illegal sequence.
2676 * - If any of the non-initial bytes could be the start of a character,
2677 * we stop the illegal sequence before the first one of those.
2678 */
2679 UBool isDBCSOnly=(UBool)(cnv->sharedData->mbcs.dbcsOnlyState!=0);
2680 int8_t i;
2681 for(i=1;
2682 i<byteIndex && !isSingleOrLead(stateTable, state, isDBCSOnly, bytes[i]);
2683 ++i) {}
2684 if(i<byteIndex) {
2685 /* Back out some bytes. */
2686 int8_t backOutDistance=byteIndex-i;
2687 int32_t bytesFromThisBuffer=(int32_t)(source-(const uint8_t *)pArgs->source);
2688 byteIndex=i; /* length of reported illegal byte sequence */
2689 if(backOutDistance<=bytesFromThisBuffer) {
2690 source-=backOutDistance;
2691 } else {
2692 /* Back out bytes from the previous buffer: Need to replay them. */
2693 cnv->preToULength=(int8_t)(bytesFromThisBuffer-backOutDistance);
2694 /* preToULength is negative! */
2695 uprv_memcpy(cnv->preToU, bytes+i, -cnv->preToULength);
2696 source=(const uint8_t *)pArgs->source;
2697 }
2698 }
2699 }
2700 break;
2701 } else /* unassigned sequences indicated with byteIndex>0 */ {
2702 /* try an extension mapping */
2703 pArgs->source=(const char *)source;
2704 byteIndex=_extToU(cnv, cnv->sharedData,
2705 byteIndex, &source, sourceLimit,
2706 &target, targetLimit,
2707 &offsets, sourceIndex,
2708 pArgs->flush,
2709 pErrorCode);
2710 sourceIndex=nextSourceIndex+=(int32_t)(source-(const uint8_t *)pArgs->source);
2711
2712 if(U_FAILURE(*pErrorCode)) {
2713 /* not mappable or buffer overflow */
2714 break;
2715 }
2716 }
2717 }
2718
2719 /* set the converter state back into UConverter */
2720 cnv->toUnicodeStatus=offset;
2721 cnv->mode=state;
2722 cnv->toULength=byteIndex;
2723
2724 /* write back the updated pointers */
2725 pArgs->source=(const char *)source;
2726 pArgs->target=target;
2727 pArgs->offsets=offsets;
2728 }
2729
2730 /*
2731 * This version of ucnv_MBCSGetNextUChar() is optimized for single-byte, single-state codepages.
2732 * We still need a conversion loop in case we find reserved action codes, which are to be ignored.
2733 */
2734 static UChar32
2735 ucnv_MBCSSingleGetNextUChar(UConverterToUnicodeArgs *pArgs,
2736 UErrorCode *pErrorCode) {
2737 UConverter *cnv;
2738 const int32_t (*stateTable)[256];
2739 const uint8_t *source, *sourceLimit;
2740
2741 int32_t entry;
2742 uint8_t action;
2743
2744 /* set up the local pointers */
2745 cnv=pArgs->converter;
2746 source=(const uint8_t *)pArgs->source;
2747 sourceLimit=(const uint8_t *)pArgs->sourceLimit;
2748 if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
2749 stateTable=(const int32_t (*)[256])cnv->sharedData->mbcs.swapLFNLStateTable;
2750 } else {
2751 stateTable=cnv->sharedData->mbcs.stateTable;
2752 }
2753
2754 /* conversion loop */
2755 while(source<sourceLimit) {
2756 entry=stateTable[0][*source++];
2757 /* MBCS_ENTRY_IS_FINAL(entry) */
2758
2759 /* write back the updated pointer early so that we can return directly */
2760 pArgs->source=(const char *)source;
2761
2762 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
2763 /* output BMP code point */
2764 return (UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2765 }
2766
2767 /*
2768 * An if-else-if chain provides more reliable performance for
2769 * the most common cases compared to a switch.
2770 */
2771 action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
2772 if( action==MBCS_STATE_VALID_DIRECT_20 ||
2773 (action==MBCS_STATE_FALLBACK_DIRECT_20 && UCNV_TO_U_USE_FALLBACK(cnv))
2774 ) {
2775 /* output supplementary code point */
2776 return (UChar32)(MBCS_ENTRY_FINAL_VALUE(entry)+0x10000);
2777 } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
2778 if(UCNV_TO_U_USE_FALLBACK(cnv)) {
2779 /* output BMP code point */
2780 return (UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2781 }
2782 } else if(action==MBCS_STATE_UNASSIGNED) {
2783 /* just fall through */
2784 } else if(action==MBCS_STATE_ILLEGAL) {
2785 /* callback(illegal) */
2786 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2787 } else {
2788 /* reserved, must never occur */
2789 continue;
2790 }
2791
2792 if(U_FAILURE(*pErrorCode)) {
2793 /* callback(illegal) */
2794 break;
2795 } else /* unassigned sequence */ {
2796 /* defer to the generic implementation */
2797 pArgs->source=(const char *)source-1;
2798 return UCNV_GET_NEXT_UCHAR_USE_TO_U;
2799 }
2800 }
2801
2802 /* no output because of empty input or only state changes */
2803 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
2804 return 0xffff;
2805 }
2806
2807 /*
2808 * Version of _MBCSToUnicodeWithOffsets() optimized for single-character
2809 * conversion without offset handling.
2810 *
2811 * When a character does not have a mapping to Unicode, then we return to the
2812 * generic ucnv_getNextUChar() code for extension/GB 18030 and error/callback
2813 * handling.
2814 * We also defer to the generic code in other complicated cases and have them
2815 * ultimately handled by _MBCSToUnicodeWithOffsets() itself.
2816 *
2817 * All normal mappings and errors are handled here.
2818 */
2819 static UChar32
2820 ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs *pArgs,
2821 UErrorCode *pErrorCode) {
2822 UConverter *cnv;
2823 const uint8_t *source, *sourceLimit, *lastSource;
2824
2825 const int32_t (*stateTable)[256];
2826 const uint16_t *unicodeCodeUnits;
2827
2828 uint32_t offset;
2829 uint8_t state;
2830
2831 int32_t entry;
2832 UChar32 c;
2833 uint8_t action;
2834
2835 /* use optimized function if possible */
2836 cnv=pArgs->converter;
2837
2838 if(cnv->preToULength>0) {
2839 /* use the generic code in ucnv_getNextUChar() to continue with a partial match */
2840 return UCNV_GET_NEXT_UCHAR_USE_TO_U;
2841 }
2842
2843 if(cnv->sharedData->mbcs.unicodeMask&UCNV_HAS_SURROGATES) {
2844 /*
2845 * Using the generic ucnv_getNextUChar() code lets us deal correctly
2846 * with the rare case of a codepage that maps single surrogates
2847 * without adding the complexity to this already complicated function here.
2848 */
2849 return UCNV_GET_NEXT_UCHAR_USE_TO_U;
2850 } else if(cnv->sharedData->mbcs.countStates==1) {
2851 return ucnv_MBCSSingleGetNextUChar(pArgs, pErrorCode);
2852 }
2853
2854 /* set up the local pointers */
2855 source=lastSource=(const uint8_t *)pArgs->source;
2856 sourceLimit=(const uint8_t *)pArgs->sourceLimit;
2857
2858 if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
2859 stateTable=(const int32_t (*)[256])cnv->sharedData->mbcs.swapLFNLStateTable;
2860 } else {
2861 stateTable=cnv->sharedData->mbcs.stateTable;
2862 }
2863 unicodeCodeUnits=cnv->sharedData->mbcs.unicodeCodeUnits;
2864
2865 /* get the converter state from UConverter */
2866 offset=cnv->toUnicodeStatus;
2867
2868 /*
2869 * if we are in the SBCS state for a DBCS-only converter,
2870 * then load the DBCS state from the MBCS data
2871 * (dbcsOnlyState==0 if it is not a DBCS-only converter)
2872 */
2873 if((state=(uint8_t)(cnv->mode))==0) {
2874 state=cnv->sharedData->mbcs.dbcsOnlyState;
2875 }
2876
2877 /* conversion loop */
2878 c=U_SENTINEL;
2879 while(source<sourceLimit) {
2880 entry=stateTable[state][*source++];
2881 if(MBCS_ENTRY_IS_TRANSITION(entry)) {
2882 state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
2883 offset+=MBCS_ENTRY_TRANSITION_OFFSET(entry);
2884
2885 /* optimization for 1/2-byte input and BMP output */
2886 if( source<sourceLimit &&
2887 MBCS_ENTRY_IS_FINAL(entry=stateTable[state][*source]) &&
2888 MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_VALID_16 &&
2889 (c=unicodeCodeUnits[offset+MBCS_ENTRY_FINAL_VALUE_16(entry)])<0xfffe
2890 ) {
2891 ++source;
2892 state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
2893 /* output BMP code point */
2894 break;
2895 }
2896 } else {
2897 /* save the previous state for proper extension mapping with SI/SO-stateful converters */
2898 cnv->mode=state;
2899
2900 /* set the next state early so that we can reuse the entry variable */
2901 state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry); /* typically 0 */
2902
2903 /*
2904 * An if-else-if chain provides more reliable performance for
2905 * the most common cases compared to a switch.
2906 */
2907 action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
2908 if(action==MBCS_STATE_VALID_DIRECT_16) {
2909 /* output BMP code point */
2910 c=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2911 break;
2912 } else if(action==MBCS_STATE_VALID_16) {
2913 offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
2914 c=unicodeCodeUnits[offset];
2915 if(c<0xfffe) {
2916 /* output BMP code point */
2917 break;
2918 } else if(c==0xfffe) {
2919 if(UCNV_TO_U_USE_FALLBACK(cnv) && (c=ucnv_MBCSGetFallback(&cnv->sharedData->mbcs, offset))!=0xfffe) {
2920 break;
2921 }
2922 } else {
2923 /* callback(illegal) */
2924 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2925 }
2926 } else if(action==MBCS_STATE_VALID_16_PAIR) {
2927 offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
2928 c=unicodeCodeUnits[offset++];
2929 if(c<0xd800) {
2930 /* output BMP code point below 0xd800 */
2931 break;
2932 } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? c<=0xdfff : c<=0xdbff) {
2933 /* output roundtrip or fallback supplementary code point */
2934 c=((c&0x3ff)<<10)+unicodeCodeUnits[offset]+(0x10000-0xdc00);
2935 break;
2936 } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? (c&0xfffe)==0xe000 : c==0xe000) {
2937 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
2938 c=unicodeCodeUnits[offset];
2939 break;
2940 } else if(c==0xffff) {
2941 /* callback(illegal) */
2942 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2943 }
2944 } else if(action==MBCS_STATE_VALID_DIRECT_20 ||
2945 (action==MBCS_STATE_FALLBACK_DIRECT_20 && UCNV_TO_U_USE_FALLBACK(cnv))
2946 ) {
2947 /* output supplementary code point */
2948 c=(UChar32)(MBCS_ENTRY_FINAL_VALUE(entry)+0x10000);
2949 break;
2950 } else if(action==MBCS_STATE_CHANGE_ONLY) {
2951 /*
2952 * This serves as a state change without any output.
2953 * It is useful for reading simple stateful encodings,
2954 * for example using just Shift-In/Shift-Out codes.
2955 * The 21 unused bits may later be used for more sophisticated
2956 * state transitions.
2957 */
2958 if(cnv->sharedData->mbcs.dbcsOnlyState!=0) {
2959 /* SI/SO are illegal for DBCS-only conversion */
2960 state=(uint8_t)(cnv->mode); /* restore the previous state */
2961
2962 /* callback(illegal) */
2963 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2964 }
2965 } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
2966 if(UCNV_TO_U_USE_FALLBACK(cnv)) {
2967 /* output BMP code point */
2968 c=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
2969 break;
2970 }
2971 } else if(action==MBCS_STATE_UNASSIGNED) {
2972 /* just fall through */
2973 } else if(action==MBCS_STATE_ILLEGAL) {
2974 /* callback(illegal) */
2975 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
2976 } else {
2977 /* reserved (must never occur), or only state change */
2978 offset=0;
2979 lastSource=source;
2980 continue;
2981 }
2982
2983 /* end of action codes: prepare for a new character */
2984 offset=0;
2985
2986 if(U_FAILURE(*pErrorCode)) {
2987 /* callback(illegal) */
2988 break;
2989 } else /* unassigned sequence */ {
2990 /* defer to the generic implementation */
2991 cnv->toUnicodeStatus=0;
2992 cnv->mode=state;
2993 pArgs->source=(const char *)lastSource;
2994 return UCNV_GET_NEXT_UCHAR_USE_TO_U;
2995 }
2996 }
2997 }
2998
2999 if(c<0) {
3000 if(U_SUCCESS(*pErrorCode) && source==sourceLimit && lastSource<source) {
3001 /* incomplete character byte sequence */
3002 uint8_t *bytes=cnv->toUBytes;
3003 cnv->toULength=(int8_t)(source-lastSource);
3004 do {
3005 *bytes++=*lastSource++;
3006 } while(lastSource<source);
3007 *pErrorCode=U_TRUNCATED_CHAR_FOUND;
3008 } else if(U_FAILURE(*pErrorCode)) {
3009 /* callback(illegal) */
3010 /*
3011 * Ticket 5691: consistent illegal sequences:
3012 * - We include at least the first byte in the illegal sequence.
3013 * - If any of the non-initial bytes could be the start of a character,
3014 * we stop the illegal sequence before the first one of those.
3015 */
3016 UBool isDBCSOnly=(UBool)(cnv->sharedData->mbcs.dbcsOnlyState!=0);
3017 uint8_t *bytes=cnv->toUBytes;
3018 *bytes++=*lastSource++; /* first byte */
3019 if(lastSource==source) {
3020 cnv->toULength=1;
3021 } else /* lastSource<source: multi-byte character */ {
3022 int8_t i;
3023 for(i=1;
3024 lastSource<source && !isSingleOrLead(stateTable, state, isDBCSOnly, *lastSource);
3025 ++i
3026 ) {
3027 *bytes++=*lastSource++;
3028 }
3029 cnv->toULength=i;
3030 source=lastSource;
3031 }
3032 } else {
3033 /* no output because of empty input or only state changes */
3034 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
3035 }
3036 c=0xffff;
3037 }
3038
3039 /* set the converter state back into UConverter, ready for a new character */
3040 cnv->toUnicodeStatus=0;
3041 cnv->mode=state;
3042
3043 /* write back the updated pointer */
3044 pArgs->source=(const char *)source;
3045 return c;
3046 }
3047
3048 #if 0
3049 /*
3050 * Code disabled 2002dec09 (ICU 2.4) because it is not currently used in ICU. markus
3051 * Removal improves code coverage.
3052 */
3053 /**
3054 * This version of ucnv_MBCSSimpleGetNextUChar() is optimized for single-byte, single-state codepages.
3055 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
3056 * It does not handle conversion extensions (_extToU()).
3057 */
3058 U_CFUNC UChar32
3059 ucnv_MBCSSingleSimpleGetNextUChar(UConverterSharedData *sharedData,
3060 uint8_t b, UBool useFallback) {
3061 int32_t entry;
3062 uint8_t action;
3063
3064 entry=sharedData->mbcs.stateTable[0][b];
3065 /* MBCS_ENTRY_IS_FINAL(entry) */
3066
3067 if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
3068 /* output BMP code point */
3069 return (UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
3070 }
3071
3072 /*
3073 * An if-else-if chain provides more reliable performance for
3074 * the most common cases compared to a switch.
3075 */
3076 action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
3077 if(action==MBCS_STATE_VALID_DIRECT_20) {
3078 /* output supplementary code point */
3079 return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
3080 } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
3081 if(!TO_U_USE_FALLBACK(useFallback)) {
3082 return 0xfffe;
3083 }
3084 /* output BMP code point */
3085 return (UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
3086 } else if(action==MBCS_STATE_FALLBACK_DIRECT_20) {
3087 if(!TO_U_USE_FALLBACK(useFallback)) {
3088 return 0xfffe;
3089 }
3090 /* output supplementary code point */
3091 return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
3092 } else if(action==MBCS_STATE_UNASSIGNED) {
3093 return 0xfffe;
3094 } else if(action==MBCS_STATE_ILLEGAL) {
3095 return 0xffff;
3096 } else {
3097 /* reserved, must never occur */
3098 return 0xffff;
3099 }
3100 }
3101 #endif
3102
3103 /*
3104 * This is a simple version of _MBCSGetNextUChar() that is used
3105 * by other converter implementations.
3106 * It only returns an "assigned" result if it consumes the entire input.
3107 * It does not use state from the converter, nor error codes.
3108 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
3109 * It handles conversion extensions but not GB 18030.
3110 *
3111 * Return value:
3112 * U+fffe unassigned
3113 * U+ffff illegal
3114 * otherwise the Unicode code point
3115 */
3116 U_CFUNC UChar32
3117 ucnv_MBCSSimpleGetNextUChar(UConverterSharedData *sharedData,
3118 const char *source, int32_t length,
3119 UBool useFallback) {
3120 const int32_t (*stateTable)[256];
3121 const uint16_t *unicodeCodeUnits;
3122
3123 uint32_t offset;
3124 uint8_t state, action;
3125
3126 UChar32 c;
3127 int32_t i, entry;
3128
3129 if(length<=0) {
3130 /* no input at all: "illegal" */
3131 return 0xffff;
3132 }
3133
3134 #if 0
3135 /*
3136 * Code disabled 2002dec09 (ICU 2.4) because it is not currently used in ICU. markus
3137 * TODO In future releases, verify that this function is never called for SBCS
3138 * conversions, i.e., that sharedData->mbcs.countStates==1 is still true.
3139 * Removal improves code coverage.
3140 */
3141 /* use optimized function if possible */
3142 if(sharedData->mbcs.countStates==1) {
3143 if(length==1) {
3144 return ucnv_MBCSSingleSimpleGetNextUChar(sharedData, (uint8_t)*source, useFallback);
3145 } else {
3146 return 0xffff; /* illegal: more than a single byte for an SBCS converter */
3147 }
3148 }
3149 #endif
3150
3151 /* set up the local pointers */
3152 stateTable=sharedData->mbcs.stateTable;
3153 unicodeCodeUnits=sharedData->mbcs.unicodeCodeUnits;
3154
3155 /* converter state */
3156 offset=0;
3157 state=sharedData->mbcs.dbcsOnlyState;
3158
3159 /* conversion loop */
3160 for(i=0;;) {
3161 entry=stateTable[state][(uint8_t)source[i++]];
3162 if(MBCS_ENTRY_IS_TRANSITION(entry)) {
3163 state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
3164 offset+=MBCS_ENTRY_TRANSITION_OFFSET(entry);
3165
3166 if(i==length) {
3167 return 0xffff; /* truncated character */
3168 }
3169 } else {
3170 /*
3171 * An if-else-if chain provides more reliable performance for
3172 * the most common cases compared to a switch.
3173 */
3174 action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
3175 if(action==MBCS_STATE_VALID_16) {
3176 offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
3177 c=unicodeCodeUnits[offset];
3178 if(c!=0xfffe) {
3179 /* done */
3180 } else if(UCNV_TO_U_USE_FALLBACK(cnv)) {
3181 c=ucnv_MBCSGetFallback(&sharedData->mbcs, offset);
3182 /* else done with 0xfffe */
3183 }
3184 break;
3185 } else if(action==MBCS_STATE_VALID_DIRECT_16) {
3186 /* output BMP code point */
3187 c=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
3188 break;
3189 } else if(action==MBCS_STATE_VALID_16_PAIR) {
3190 offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
3191 c=unicodeCodeUnits[offset++];
3192 if(c<0xd800) {
3193 /* output BMP code point below 0xd800 */
3194 } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? c<=0xdfff : c<=0xdbff) {
3195 /* output roundtrip or fallback supplementary code point */
3196 c=(UChar32)(((c&0x3ff)<<10)+unicodeCodeUnits[offset]+(0x10000-0xdc00));
3197 } else if(UCNV_TO_U_USE_FALLBACK(cnv) ? (c&0xfffe)==0xe000 : c==0xe000) {
3198 /* output roundtrip BMP code point above 0xd800 or fallback BMP code point */
3199 c=unicodeCodeUnits[offset];
3200 } else if(c==0xffff) {
3201 return 0xffff;
3202 } else {
3203 c=0xfffe;
3204 }
3205 break;
3206 } else if(action==MBCS_STATE_VALID_DIRECT_20) {
3207 /* output supplementary code point */
3208 c=0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
3209 break;
3210 } else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
3211 if(!TO_U_USE_FALLBACK(useFallback)) {
3212 c=0xfffe;
3213 break;
3214 }
3215 /* output BMP code point */
3216 c=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
3217 break;
3218 } else if(action==MBCS_STATE_FALLBACK_DIRECT_20) {
3219 if(!TO_U_USE_FALLBACK(useFallback)) {
3220 c=0xfffe;
3221 break;
3222 }
3223 /* output supplementary code point */
3224 c=0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
3225 break;
3226 } else if(action==MBCS_STATE_UNASSIGNED) {
3227 c=0xfffe;
3228 break;
3229 }
3230
3231 /*
3232 * forbid MBCS_STATE_CHANGE_ONLY for this function,
3233 * and MBCS_STATE_ILLEGAL and reserved action codes
3234 */
3235 return 0xffff;
3236 }
3237 }
3238
3239 if(i!=length) {
3240 /* illegal for this function: not all input consumed */
3241 return 0xffff;
3242 }
3243
3244 if(c==0xfffe) {
3245 /* try an extension mapping */
3246 const int32_t *cx=sharedData->mbcs.extIndexes;
3247 if(cx!=NULL) {
3248 return ucnv_extSimpleMatchToU(cx, source, length, useFallback);
3249 }
3250 }
3251
3252 return c;
3253 }
3254
3255 /* MBCS-from-Unicode conversion functions ----------------------------------- */
3256
3257 /* This version of ucnv_MBCSFromUnicodeWithOffsets() is optimized for double-byte codepages. */
3258 static void
3259 ucnv_MBCSDoubleFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
3260 UErrorCode *pErrorCode) {
3261 UConverter *cnv;
3262 const UChar *source, *sourceLimit;
3263 uint8_t *target;
3264 int32_t targetCapacity;
3265 int32_t *offsets;
3266
3267 const uint16_t *table;
3268 const uint16_t *mbcsIndex;
3269 const uint8_t *bytes;
3270
3271 UChar32 c;
3272
3273 int32_t sourceIndex, nextSourceIndex;
3274
3275 uint32_t stage2Entry;
3276 uint32_t asciiRoundtrips;
3277 uint32_t value;
3278 uint8_t unicodeMask;
3279
3280 /* use optimized function if possible */
3281 cnv=pArgs->converter;
3282 unicodeMask=cnv->sharedData->mbcs.unicodeMask;
3283
3284 /* set up the local pointers */
3285 source=pArgs->source;
3286 sourceLimit=pArgs->sourceLimit;
3287 target=(uint8_t *)pArgs->target;
3288 targetCapacity=(int32_t)(pArgs->targetLimit-pArgs->target);
3289 offsets=pArgs->offsets;
3290
3291 table=cnv->sharedData->mbcs.fromUnicodeTable;
3292 mbcsIndex=cnv->sharedData->mbcs.mbcsIndex;
3293 if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
3294 bytes=cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes;
3295 } else {
3296 bytes=cnv->sharedData->mbcs.fromUnicodeBytes;
3297 }
3298 asciiRoundtrips=cnv->sharedData->mbcs.asciiRoundtrips;
3299
3300 /* get the converter state from UConverter */
3301 c=cnv->fromUChar32;
3302
3303 /* sourceIndex=-1 if the current character began in the previous buffer */
3304 sourceIndex= c==0 ? 0 : -1;
3305 nextSourceIndex=0;
3306
3307 /* conversion loop */
3308 if(c!=0 && targetCapacity>0) {
3309 goto getTrail;
3310 }
3311
3312 while(source<sourceLimit) {
3313 /*
3314 * This following test is to see if available input would overflow the output.
3315 * It does not catch output of more than one byte that
3316 * overflows as a result of a multi-byte character or callback output
3317 * from the last source character.
3318 * Therefore, those situations also test for overflows and will
3319 * then break the loop, too.
3320 */
3321 if(targetCapacity>0) {
3322 /*
3323 * Get a correct Unicode code point:
3324 * a single UChar for a BMP code point or
3325 * a matched surrogate pair for a "supplementary code point".
3326 */
3327 c=*source++;
3328 ++nextSourceIndex;
3329 if(c<=0x7f && IS_ASCII_ROUNDTRIP(c, asciiRoundtrips)) {
3330 *target++=(uint8_t)c;
3331 if(offsets!=NULL) {
3332 *offsets++=sourceIndex;
3333 sourceIndex=nextSourceIndex;
3334 }
3335 --targetCapacity;
3336 c=0;
3337 continue;
3338 }
3339 /*
3340 * utf8Friendly table: Test for <=0xd7ff rather than <=MBCS_FAST_MAX
3341 * to avoid dealing with surrogates.
3342 * MBCS_FAST_MAX must be >=0xd7ff.
3343 */
3344 if(c<=0xd7ff) {
3345 value=DBCS_RESULT_FROM_MOST_BMP(mbcsIndex, (const uint16_t *)bytes, c);
3346 /* There are only roundtrips (!=0) and no-mapping (==0) entries. */
3347 if(value==0) {
3348 goto unassigned;
3349 }
3350 /* output the value */
3351 } else {
3352 /*
3353 * This also tests if the codepage maps single surrogates.
3354 * If it does, then surrogates are not paired but mapped separately.
3355 * Note that in this case unmatched surrogates are not detected.
3356 */
3357 if(U16_IS_SURROGATE(c) && !(unicodeMask&UCNV_HAS_SURROGATES)) {
3358 if(U16_IS_SURROGATE_LEAD(c)) {
3359 getTrail:
3360 if(source<sourceLimit) {
3361 /* test the following code unit */
3362 UChar trail=*source;
3363 if(U16_IS_TRAIL(trail)) {
3364 ++source;
3365 ++nextSourceIndex;
3366 c=U16_GET_SUPPLEMENTARY(c, trail);
3367 if(!(unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
3368 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
3369 /* callback(unassigned) */
3370 goto unassigned;
3371 }
3372 /* convert this supplementary code point */
3373 /* exit this condition tree */
3374 } else {
3375 /* this is an unmatched lead code unit (1st surrogate) */
3376 /* callback(illegal) */
3377 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3378 break;
3379 }
3380 } else {
3381 /* no more input */
3382 break;
3383 }
3384 } else {
3385 /* this is an unmatched trail code unit (2nd surrogate) */
3386 /* callback(illegal) */
3387 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3388 break;
3389 }
3390 }
3391
3392 /* convert the Unicode code point in c into codepage bytes */
3393 stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
3394
3395 /* get the bytes and the length for the output */
3396 /* MBCS_OUTPUT_2 */
3397 value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
3398
3399 /* is this code point assigned, or do we use fallbacks? */
3400 if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c) ||
3401 (UCNV_FROM_U_USE_FALLBACK(cnv, c) && value!=0))
3402 ) {
3403 /*
3404 * We allow a 0 byte output if the "assigned" bit is set for this entry.
3405 * There is no way with this data structure for fallback output
3406 * to be a zero byte.
3407 */
3408
3409 unassigned:
3410 /* try an extension mapping */
3411 pArgs->source=source;
3412 c=_extFromU(cnv, cnv->sharedData,
3413 c, &source, sourceLimit,
3414 &target, target+targetCapacity,
3415 &offsets, sourceIndex,
3416 pArgs->flush,
3417 pErrorCode);
3418 nextSourceIndex+=(int32_t)(source-pArgs->source);
3419
3420 if(U_FAILURE(*pErrorCode)) {
3421 /* not mappable or buffer overflow */
3422 break;
3423 } else {
3424 /* a mapping was written to the target, continue */
3425
3426 /* recalculate the targetCapacity after an extension mapping */
3427 targetCapacity=(int32_t)(pArgs->targetLimit-(char *)target);
3428
3429 /* normal end of conversion: prepare for a new character */
3430 sourceIndex=nextSourceIndex;
3431 continue;
3432 }
3433 }
3434 }
3435
3436 /* write the output character bytes from value and length */
3437 /* from the first if in the loop we know that targetCapacity>0 */
3438 if(value<=0xff) {
3439 /* this is easy because we know that there is enough space */
3440 *target++=(uint8_t)value;
3441 if(offsets!=NULL) {
3442 *offsets++=sourceIndex;
3443 }
3444 --targetCapacity;
3445 } else /* length==2 */ {
3446 *target++=(uint8_t)(value>>8);
3447 if(2<=targetCapacity) {
3448 *target++=(uint8_t)value;
3449 if(offsets!=NULL) {
3450 *offsets++=sourceIndex;
3451 *offsets++=sourceIndex;
3452 }
3453 targetCapacity-=2;
3454 } else {
3455 if(offsets!=NULL) {
3456 *offsets++=sourceIndex;
3457 }
3458 cnv->charErrorBuffer[0]=(char)value;
3459 cnv->charErrorBufferLength=1;
3460
3461 /* target overflow */
3462 targetCapacity=0;
3463 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
3464 c=0;
3465 break;
3466 }
3467 }
3468
3469 /* normal end of conversion: prepare for a new character */
3470 c=0;
3471 sourceIndex=nextSourceIndex;
3472 continue;
3473 } else {
3474 /* target is full */
3475 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
3476 break;
3477 }
3478 }
3479
3480 /* set the converter state back into UConverter */
3481 cnv->fromUChar32=c;
3482
3483 /* write back the updated pointers */
3484 pArgs->source=source;
3485 pArgs->target=(char *)target;
3486 pArgs->offsets=offsets;
3487 }
3488
3489 /* This version of ucnv_MBCSFromUnicodeWithOffsets() is optimized for single-byte codepages. */
3490 static void
3491 ucnv_MBCSSingleFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
3492 UErrorCode *pErrorCode) {
3493 UConverter *cnv;
3494 const UChar *source, *sourceLimit;
3495 uint8_t *target;
3496 int32_t targetCapacity;
3497 int32_t *offsets;
3498
3499 const uint16_t *table;
3500 const uint16_t *results;
3501
3502 UChar32 c;
3503
3504 int32_t sourceIndex, nextSourceIndex;
3505
3506 uint16_t value, minValue;
3507 UBool hasSupplementary;
3508
3509 /* set up the local pointers */
3510 cnv=pArgs->converter;
3511 source=pArgs->source;
3512 sourceLimit=pArgs->sourceLimit;
3513 target=(uint8_t *)pArgs->target;
3514 targetCapacity=(int32_t)(pArgs->targetLimit-pArgs->target);
3515 offsets=pArgs->offsets;
3516
3517 table=cnv->sharedData->mbcs.fromUnicodeTable;
3518 if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
3519 results=(uint16_t *)cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes;
3520 } else {
3521 results=(uint16_t *)cnv->sharedData->mbcs.fromUnicodeBytes;
3522 }
3523
3524 if(cnv->useFallback) {
3525 /* use all roundtrip and fallback results */
3526 minValue=0x800;
3527 } else {
3528 /* use only roundtrips and fallbacks from private-use characters */
3529 minValue=0xc00;
3530 }
3531 hasSupplementary=(UBool)(cnv->sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY);
3532
3533 /* get the converter state from UConverter */
3534 c=cnv->fromUChar32;
3535
3536 /* sourceIndex=-1 if the current character began in the previous buffer */
3537 sourceIndex= c==0 ? 0 : -1;
3538 nextSourceIndex=0;
3539
3540 /* conversion loop */
3541 if(c!=0 && targetCapacity>0) {
3542 goto getTrail;
3543 }
3544
3545 while(source<sourceLimit) {
3546 /*
3547 * This following test is to see if available input would overflow the output.
3548 * It does not catch output of more than one byte that
3549 * overflows as a result of a multi-byte character or callback output
3550 * from the last source character.
3551 * Therefore, those situations also test for overflows and will
3552 * then break the loop, too.
3553 */
3554 if(targetCapacity>0) {
3555 /*
3556 * Get a correct Unicode code point:
3557 * a single UChar for a BMP code point or
3558 * a matched surrogate pair for a "supplementary code point".
3559 */
3560 c=*source++;
3561 ++nextSourceIndex;
3562 if(U16_IS_SURROGATE(c)) {
3563 if(U16_IS_SURROGATE_LEAD(c)) {
3564 getTrail:
3565 if(source<sourceLimit) {
3566 /* test the following code unit */
3567 UChar trail=*source;
3568 if(U16_IS_TRAIL(trail)) {
3569 ++source;
3570 ++nextSourceIndex;
3571 c=U16_GET_SUPPLEMENTARY(c, trail);
3572 if(!hasSupplementary) {
3573 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
3574 /* callback(unassigned) */
3575 goto unassigned;
3576 }
3577 /* convert this supplementary code point */
3578 /* exit this condition tree */
3579 } else {
3580 /* this is an unmatched lead code unit (1st surrogate) */
3581 /* callback(illegal) */
3582 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3583 break;
3584 }
3585 } else {
3586 /* no more input */
3587 break;
3588 }
3589 } else {
3590 /* this is an unmatched trail code unit (2nd surrogate) */
3591 /* callback(illegal) */
3592 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3593 break;
3594 }
3595 }
3596
3597 /* convert the Unicode code point in c into codepage bytes */
3598 value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
3599
3600 /* is this code point assigned, or do we use fallbacks? */
3601 if(value>=minValue) {
3602 /* assigned, write the output character bytes from value and length */
3603 /* length==1 */
3604 /* this is easy because we know that there is enough space */
3605 *target++=(uint8_t)value;
3606 if(offsets!=NULL) {
3607 *offsets++=sourceIndex;
3608 }
3609 --targetCapacity;
3610
3611 /* normal end of conversion: prepare for a new character */
3612 c=0;
3613 sourceIndex=nextSourceIndex;
3614 } else { /* unassigned */
3615 unassigned:
3616 /* try an extension mapping */
3617 pArgs->source=source;
3618 c=_extFromU(cnv, cnv->sharedData,
3619 c, &source, sourceLimit,
3620 &target, target+targetCapacity,
3621 &offsets, sourceIndex,
3622 pArgs->flush,
3623 pErrorCode);
3624 nextSourceIndex+=(int32_t)(source-pArgs->source);
3625
3626 if(U_FAILURE(*pErrorCode)) {
3627 /* not mappable or buffer overflow */
3628 break;
3629 } else {
3630 /* a mapping was written to the target, continue */
3631
3632 /* recalculate the targetCapacity after an extension mapping */
3633 targetCapacity=(int32_t)(pArgs->targetLimit-(char *)target);
3634
3635 /* normal end of conversion: prepare for a new character */
3636 sourceIndex=nextSourceIndex;
3637 }
3638 }
3639 } else {
3640 /* target is full */
3641 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
3642 break;
3643 }
3644 }
3645
3646 /* set the converter state back into UConverter */
3647 cnv->fromUChar32=c;
3648
3649 /* write back the updated pointers */
3650 pArgs->source=source;
3651 pArgs->target=(char *)target;
3652 pArgs->offsets=offsets;
3653 }
3654
3655 /*
3656 * This version of ucnv_MBCSFromUnicode() is optimized for single-byte codepages
3657 * that map only to and from the BMP.
3658 * In addition to single-byte/state optimizations, the offset calculations
3659 * become much easier.
3660 * It would be possible to use the sbcsIndex for UTF-8-friendly tables,
3661 * but measurements have shown that this diminishes performance
3662 * in more cases than it improves it.
3663 * See SVN revision 21013 (2007-feb-06) for the last version with #if switches
3664 * for various MBCS and SBCS optimizations.
3665 */
3666 static void
3667 ucnv_MBCSSingleFromBMPWithOffsets(UConverterFromUnicodeArgs *pArgs,
3668 UErrorCode *pErrorCode) {
3669 UConverter *cnv;
3670 const UChar *source, *sourceLimit, *lastSource;
3671 uint8_t *target;
3672 int32_t targetCapacity, length;
3673 int32_t *offsets;
3674
3675 const uint16_t *table;
3676 const uint16_t *results;
3677
3678 UChar32 c;
3679
3680 int32_t sourceIndex;
3681
3682 uint32_t asciiRoundtrips;
3683 uint16_t value, minValue;
3684
3685 /* set up the local pointers */
3686 cnv=pArgs->converter;
3687 source=pArgs->source;
3688 sourceLimit=pArgs->sourceLimit;
3689 target=(uint8_t *)pArgs->target;
3690 targetCapacity=(int32_t)(pArgs->targetLimit-pArgs->target);
3691 offsets=pArgs->offsets;
3692
3693 table=cnv->sharedData->mbcs.fromUnicodeTable;
3694 if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
3695 results=(uint16_t *)cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes;
3696 } else {
3697 results=(uint16_t *)cnv->sharedData->mbcs.fromUnicodeBytes;
3698 }
3699 asciiRoundtrips=cnv->sharedData->mbcs.asciiRoundtrips;
3700
3701 if(cnv->useFallback) {
3702 /* use all roundtrip and fallback results */
3703 minValue=0x800;
3704 } else {
3705 /* use only roundtrips and fallbacks from private-use characters */
3706 minValue=0xc00;
3707 }
3708
3709 /* get the converter state from UConverter */
3710 c=cnv->fromUChar32;
3711
3712 /* sourceIndex=-1 if the current character began in the previous buffer */
3713 sourceIndex= c==0 ? 0 : -1;
3714 lastSource=source;
3715
3716 /*
3717 * since the conversion here is 1:1 UChar:uint8_t, we need only one counter
3718 * for the minimum of the sourceLength and targetCapacity
3719 */
3720 length=(int32_t)(sourceLimit-source);
3721 if(length<targetCapacity) {
3722 targetCapacity=length;
3723 }
3724
3725 /* conversion loop */
3726 if(c!=0 && targetCapacity>0) {
3727 goto getTrail;
3728 }
3729
3730 #if MBCS_UNROLL_SINGLE_FROM_BMP
3731 /* unrolling makes it slower on Pentium III/Windows 2000?! */
3732 /* unroll the loop with the most common case */
3733 unrolled:
3734 if(targetCapacity>=4) {
3735 int32_t count, loops;
3736 uint16_t andedValues;
3737
3738 loops=count=targetCapacity>>2;
3739 do {
3740 c=*source++;
3741 andedValues=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
3742 *target++=(uint8_t)value;
3743 c=*source++;
3744 andedValues&=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
3745 *target++=(uint8_t)value;
3746 c=*source++;
3747 andedValues&=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
3748 *target++=(uint8_t)value;
3749 c=*source++;
3750 andedValues&=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
3751 *target++=(uint8_t)value;
3752
3753 /* were all 4 entries really valid? */
3754 if(andedValues<minValue) {
3755 /* no, return to the first of these 4 */
3756 source-=4;
3757 target-=4;
3758 break;
3759 }
3760 } while(--count>0);
3761 count=loops-count;
3762 targetCapacity-=4*count;
3763
3764 if(offsets!=NULL) {
3765 lastSource+=4*count;
3766 while(count>0) {
3767 *offsets++=sourceIndex++;
3768 *offsets++=sourceIndex++;
3769 *offsets++=sourceIndex++;
3770 *offsets++=sourceIndex++;
3771 --count;
3772 }
3773 }
3774
3775 c=0;
3776 }
3777 #endif
3778
3779 while(targetCapacity>0) {
3780 /*
3781 * Get a correct Unicode code point:
3782 * a single UChar for a BMP code point or
3783 * a matched surrogate pair for a "supplementary code point".
3784 */
3785 c=*source++;
3786 /*
3787 * Do not immediately check for single surrogates:
3788 * Assume that they are unassigned and check for them in that case.
3789 * This speeds up the conversion of assigned characters.
3790 */
3791 /* convert the Unicode code point in c into codepage bytes */
3792 if(c<=0x7f && IS_ASCII_ROUNDTRIP(c, asciiRoundtrips)) {
3793 *target++=(uint8_t)c;
3794 --targetCapacity;
3795 c=0;
3796 continue;
3797 }
3798 value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
3799 /* is this code point assigned, or do we use fallbacks? */
3800 if(value>=minValue) {
3801 /* assigned, write the output character bytes from value and length */
3802 /* length==1 */
3803 /* this is easy because we know that there is enough space */
3804 *target++=(uint8_t)value;
3805 --targetCapacity;
3806
3807 /* normal end of conversion: prepare for a new character */
3808 c=0;
3809 continue;
3810 } else if(!U16_IS_SURROGATE(c)) {
3811 /* normal, unassigned BMP character */
3812 } else if(U16_IS_SURROGATE_LEAD(c)) {
3813 getTrail:
3814 if(source<sourceLimit) {
3815 /* test the following code unit */
3816 UChar trail=*source;
3817 if(U16_IS_TRAIL(trail)) {
3818 ++source;
3819 c=U16_GET_SUPPLEMENTARY(c, trail);
3820 /* this codepage does not map supplementary code points */
3821 /* callback(unassigned) */
3822 } else {
3823 /* this is an unmatched lead code unit (1st surrogate) */
3824 /* callback(illegal) */
3825 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3826 break;
3827 }
3828 } else {
3829 /* no more input */
3830 if (pArgs->flush) {
3831 *pErrorCode=U_TRUNCATED_CHAR_FOUND;
3832 }
3833 break;
3834 }
3835 } else {
3836 /* this is an unmatched trail code unit (2nd surrogate) */
3837 /* callback(illegal) */
3838 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
3839 break;
3840 }
3841
3842 /* c does not have a mapping */
3843
3844 /* get the number of code units for c to correctly advance sourceIndex */
3845 length=U16_LENGTH(c);
3846
3847 /* set offsets since the start or the last extension */
3848 if(offsets!=NULL) {
3849 int32_t count=(int32_t)(source-lastSource);
3850
3851 /* do not set the offset for this character */
3852 count-=length;
3853
3854 while(count>0) {
3855 *offsets++=sourceIndex++;
3856 --count;
3857 }
3858 /* offsets and sourceIndex are now set for the current character */
3859 }
3860
3861 /* try an extension mapping */
3862 lastSource=source;
3863 c=_extFromU(cnv, cnv->sharedData,
3864 c, &source, sourceLimit,
3865 &target, (const uint8_t *)(pArgs->targetLimit),
3866 &offsets, sourceIndex,
3867 pArgs->flush,
3868 pErrorCode);
3869 sourceIndex+=length+(int32_t)(source-lastSource);
3870 lastSource=source;
3871
3872 if(U_FAILURE(*pErrorCode)) {
3873 /* not mappable or buffer overflow */
3874 break;
3875 } else {
3876 /* a mapping was written to the target, continue */
3877
3878 /* recalculate the targetCapacity after an extension mapping */
3879 targetCapacity=(int32_t)(pArgs->targetLimit-(char *)target);
3880 length=(int32_t)(sourceLimit-source);
3881 if(length<targetCapacity) {
3882 targetCapacity=length;
3883 }
3884 }
3885
3886 #if MBCS_UNROLL_SINGLE_FROM_BMP
3887 /* unrolling makes it slower on Pentium III/Windows 2000?! */
3888 goto unrolled;
3889 #endif
3890 }
3891
3892 if(U_SUCCESS(*pErrorCode) && source<sourceLimit && target>=(uint8_t *)pArgs->targetLimit) {
3893 /* target is full */
3894 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
3895 }
3896
3897 /* set offsets since the start or the last callback */
3898 if(offsets!=NULL) {
3899 size_t count=source-lastSource;
3900 if (count > 0 && *pErrorCode == U_TRUNCATED_CHAR_FOUND) {
3901 /*
3902 Caller gave us a partial supplementary character,
3903 which this function couldn't convert in any case.
3904 The callback will handle the offset.
3905 */
3906 count--;
3907 }
3908 while(count>0) {
3909 *offsets++=sourceIndex++;
3910 --count;
3911 }
3912 }
3913
3914 /* set the converter state back into UConverter */
3915 cnv->fromUChar32=c;
3916
3917 /* write back the updated pointers */
3918 pArgs->source=source;
3919 pArgs->target=(char *)target;
3920 pArgs->offsets=offsets;
3921 }
3922
3923 U_CFUNC void
3924 ucnv_MBCSFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
3925 UErrorCode *pErrorCode) {
3926 UConverter *cnv;
3927 const UChar *source, *sourceLimit;
3928 uint8_t *target;
3929 int32_t targetCapacity;
3930 int32_t *offsets;
3931
3932 const uint16_t *table;
3933 const uint16_t *mbcsIndex;
3934 const uint8_t *p, *bytes;
3935 uint8_t outputType;
3936
3937 UChar32 c;
3938
3939 int32_t prevSourceIndex, sourceIndex, nextSourceIndex;
3940
3941 uint32_t stage2Entry;
3942 uint32_t asciiRoundtrips;
3943 uint32_t value;
3944 uint8_t si_value[2] = {0, 0};
3945 uint8_t so_value[2] = {0, 0};
3946 uint8_t si_value_length, so_value_length;
3947 int32_t length = 0, prevLength;
3948 uint8_t unicodeMask;
3949
3950 cnv=pArgs->converter;
3951
3952 if(cnv->preFromUFirstCP>=0) {
3953 /*
3954 * pass sourceIndex=-1 because we continue from an earlier buffer
3955 * in the future, this may change with continuous offsets
3956 */
3957 ucnv_extContinueMatchFromU(cnv, pArgs, -1, pErrorCode);
3958
3959 if(U_FAILURE(*pErrorCode) || cnv->preFromULength<0) {
3960 return;
3961 }
3962 }
3963
3964 /* use optimized function if possible */
3965 outputType=cnv->sharedData->mbcs.outputType;
3966 unicodeMask=cnv->sharedData->mbcs.unicodeMask;
3967 if(outputType==MBCS_OUTPUT_1 && !(unicodeMask&UCNV_HAS_SURROGATES)) {
3968 if(!(unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
3969 ucnv_MBCSSingleFromBMPWithOffsets(pArgs, pErrorCode);
3970 } else {
3971 ucnv_MBCSSingleFromUnicodeWithOffsets(pArgs, pErrorCode);
3972 }
3973 return;
3974 } else if(outputType==MBCS_OUTPUT_2 && cnv->sharedData->mbcs.utf8Friendly) {
3975 ucnv_MBCSDoubleFromUnicodeWithOffsets(pArgs, pErrorCode);
3976 return;
3977 }
3978
3979 /* set up the local pointers */
3980 source=pArgs->source;
3981 sourceLimit=pArgs->sourceLimit;
3982 target=(uint8_t *)pArgs->target;
3983 targetCapacity=(int32_t)(pArgs->targetLimit-pArgs->target);
3984 offsets=pArgs->offsets;
3985
3986 table=cnv->sharedData->mbcs.fromUnicodeTable;
3987 if(cnv->sharedData->mbcs.utf8Friendly) {
3988 mbcsIndex=cnv->sharedData->mbcs.mbcsIndex;
3989 } else {
3990 mbcsIndex=NULL;
3991 }
3992 if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
3993 bytes=cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes;
3994 } else {
3995 bytes=cnv->sharedData->mbcs.fromUnicodeBytes;
3996 }
3997 asciiRoundtrips=cnv->sharedData->mbcs.asciiRoundtrips;
3998
3999 /* get the converter state from UConverter */
4000 c=cnv->fromUChar32;
4001
4002 if(outputType==MBCS_OUTPUT_2_SISO) {
4003 prevLength=cnv->fromUnicodeStatus;
4004 if(prevLength==0) {
4005 /* set the real value */
4006 prevLength=1;
4007 }
4008 } else {
4009 /* prevent fromUnicodeStatus from being set to something non-0 */
4010 prevLength=0;
4011 }
4012
4013 /* sourceIndex=-1 if the current character began in the previous buffer */
4014 prevSourceIndex=-1;
4015 sourceIndex= c==0 ? 0 : -1;
4016 nextSourceIndex=0;
4017
4018 /* Get the SI/SO character for the converter */
4019 si_value_length = getSISOBytes(SI, cnv->options, si_value);
4020 so_value_length = getSISOBytes(SO, cnv->options, so_value);
4021
4022 /* conversion loop */
4023 /*
4024 * This is another piece of ugly code:
4025 * A goto into the loop if the converter state contains a first surrogate
4026 * from the previous function call.
4027 * It saves me to check in each loop iteration a check of if(c==0)
4028 * and duplicating the trail-surrogate-handling code in the else
4029 * branch of that check.
4030 * I could not find any other way to get around this other than
4031 * using a function call for the conversion and callback, which would
4032 * be even more inefficient.
4033 *
4034 * Markus Scherer 2000-jul-19
4035 */
4036 if(c!=0 && targetCapacity>0) {
4037 goto getTrail;
4038 }
4039
4040 while(source<sourceLimit) {
4041 /*
4042 * This following test is to see if available input would overflow the output.
4043 * It does not catch output of more than one byte that
4044 * overflows as a result of a multi-byte character or callback output
4045 * from the last source character.
4046 * Therefore, those situations also test for overflows and will
4047 * then break the loop, too.
4048 */
4049 if(targetCapacity>0) {
4050 /*
4051 * Get a correct Unicode code point:
4052 * a single UChar for a BMP code point or
4053 * a matched surrogate pair for a "supplementary code point".
4054 */
4055 c=*source++;
4056 ++nextSourceIndex;
4057 if(c<=0x7f && IS_ASCII_ROUNDTRIP(c, asciiRoundtrips)) {
4058 *target++=(uint8_t)c;
4059 if(offsets!=NULL) {
4060 *offsets++=sourceIndex;
4061 prevSourceIndex=sourceIndex;
4062 sourceIndex=nextSourceIndex;
4063 }
4064 --targetCapacity;
4065 c=0;
4066 continue;
4067 }
4068 /*
4069 * utf8Friendly table: Test for <=0xd7ff rather than <=MBCS_FAST_MAX
4070 * to avoid dealing with surrogates.
4071 * MBCS_FAST_MAX must be >=0xd7ff.
4072 */
4073 if(c<=0xd7ff && mbcsIndex!=NULL) {
4074 value=mbcsIndex[c>>6];
4075
4076 /* get the bytes and the length for the output (copied from below and adapted for utf8Friendly data) */
4077 /* There are only roundtrips (!=0) and no-mapping (==0) entries. */
4078 switch(outputType) {
4079 case MBCS_OUTPUT_2:
4080 value=((const uint16_t *)bytes)[value +(c&0x3f)];
4081 if(value<=0xff) {
4082 if(value==0) {
4083 goto unassigned;
4084 } else {
4085 length=1;
4086 }
4087 } else {
4088 length=2;
4089 }
4090 break;
4091 case MBCS_OUTPUT_2_SISO:
4092 /* 1/2-byte stateful with Shift-In/Shift-Out */
4093 /*
4094 * Save the old state in the converter object
4095 * right here, then change the local prevLength state variable if necessary.
4096 * Then, if this character turns out to be unassigned or a fallback that
4097 * is not taken, the callback code must not save the new state in the converter
4098 * because the new state is for a character that is not output.
4099 * However, the callback must still restore the state from the converter
4100 * in case the callback function changed it for its output.
4101 */
4102 cnv->fromUnicodeStatus=prevLength; /* save the old state */
4103 value=((const uint16_t *)bytes)[value +(c&0x3f)];
4104 if(value<=0xff) {
4105 if(value==0) {
4106 goto unassigned;
4107 } else if(prevLength<=1) {
4108 length=1;
4109 } else {
4110 /* change from double-byte mode to single-byte */
4111 if (si_value_length == 1) {
4112 value|=(uint32_t)si_value[0]<<8;
4113 length = 2;
4114 } else if (si_value_length == 2) {
4115 value|=(uint32_t)si_value[1]<<8;
4116 value|=(uint32_t)si_value[0]<<16;
4117 length = 3;
4118 }
4119 prevLength=1;
4120 }
4121 } else {
4122 if(prevLength==2) {
4123 length=2;
4124 } else {
4125 /* change from single-byte mode to double-byte */
4126 if (so_value_length == 1) {
4127 value|=(uint32_t)so_value[0]<<16;
4128 length = 3;
4129 } else if (so_value_length == 2) {
4130 value|=(uint32_t)so_value[1]<<16;
4131 value|=(uint32_t)so_value[0]<<24;
4132 length = 4;
4133 }
4134 prevLength=2;
4135 }
4136 }
4137 break;
4138 case MBCS_OUTPUT_DBCS_ONLY:
4139 /* table with single-byte results, but only DBCS mappings used */
4140 value=((const uint16_t *)bytes)[value +(c&0x3f)];
4141 if(value<=0xff) {
4142 /* no mapping or SBCS result, not taken for DBCS-only */
4143 goto unassigned;
4144 } else {
4145 length=2;
4146 }
4147 break;
4148 case MBCS_OUTPUT_3:
4149 p=bytes+(value+(c&0x3f))*3;
4150 value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
4151 if(value<=0xff) {
4152 if(value==0) {
4153 goto unassigned;
4154 } else {
4155 length=1;
4156 }
4157 } else if(value<=0xffff) {
4158 length=2;
4159 } else {
4160 length=3;
4161 }
4162 break;
4163 case MBCS_OUTPUT_4:
4164 value=((const uint32_t *)bytes)[value +(c&0x3f)];
4165 if(value<=0xff) {
4166 if(value==0) {
4167 goto unassigned;
4168 } else {
4169 length=1;
4170 }
4171 } else if(value<=0xffff) {
4172 length=2;
4173 } else if(value<=0xffffff) {
4174 length=3;
4175 } else {
4176 length=4;
4177 }
4178 break;
4179 case MBCS_OUTPUT_3_EUC:
4180 value=((const uint16_t *)bytes)[value +(c&0x3f)];
4181 /* EUC 16-bit fixed-length representation */
4182 if(value<=0xff) {
4183 if(value==0) {
4184 goto unassigned;
4185 } else {
4186 length=1;
4187 }
4188 } else if((value&0x8000)==0) {
4189 value|=0x8e8000;
4190 length=3;
4191 } else if((value&0x80)==0) {
4192 value|=0x8f0080;
4193 length=3;
4194 } else {
4195 length=2;
4196 }
4197 break;
4198 case MBCS_OUTPUT_4_EUC:
4199 p=bytes+(value+(c&0x3f))*3;
4200 value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
4201 /* EUC 16-bit fixed-length representation applied to the first two bytes */
4202 if(value<=0xff) {
4203 if(value==0) {
4204 goto unassigned;
4205 } else {
4206 length=1;
4207 }
4208 } else if(value<=0xffff) {
4209 length=2;
4210 } else if((value&0x800000)==0) {
4211 value|=0x8e800000;
4212 length=4;
4213 } else if((value&0x8000)==0) {
4214 value|=0x8f008000;
4215 length=4;
4216 } else {
4217 length=3;
4218 }
4219 break;
4220 default:
4221 /* must not occur */
4222 /*
4223 * To avoid compiler warnings that value & length may be
4224 * used without having been initialized, we set them here.
4225 * In reality, this is unreachable code.
4226 * Not having a default branch also causes warnings with
4227 * some compilers.
4228 */
4229 value=0;
4230 length=0;
4231 break;
4232 }
4233 /* output the value */
4234 } else {
4235 /*
4236 * This also tests if the codepage maps single surrogates.
4237 * If it does, then surrogates are not paired but mapped separately.
4238 * Note that in this case unmatched surrogates are not detected.
4239 */
4240 if(U16_IS_SURROGATE(c) && !(unicodeMask&UCNV_HAS_SURROGATES)) {
4241 if(U16_IS_SURROGATE_LEAD(c)) {
4242 getTrail:
4243 if(source<sourceLimit) {
4244 /* test the following code unit */
4245 UChar trail=*source;
4246 if(U16_IS_TRAIL(trail)) {
4247 ++source;
4248 ++nextSourceIndex;
4249 c=U16_GET_SUPPLEMENTARY(c, trail);
4250 if(!(unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
4251 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
4252 cnv->fromUnicodeStatus=prevLength; /* save the old state */
4253 /* callback(unassigned) */
4254 goto unassigned;
4255 }
4256 /* convert this supplementary code point */
4257 /* exit this condition tree */
4258 } else {
4259 /* this is an unmatched lead code unit (1st surrogate) */
4260 /* callback(illegal) */
4261 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
4262 break;
4263 }
4264 } else {
4265 /* no more input */
4266 break;
4267 }
4268 } else {
4269 /* this is an unmatched trail code unit (2nd surrogate) */
4270 /* callback(illegal) */
4271 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
4272 break;
4273 }
4274 }
4275
4276 /* convert the Unicode code point in c into codepage bytes */
4277
4278 /*
4279 * The basic lookup is a triple-stage compact array (trie) lookup.
4280 * For details see the beginning of this file.
4281 *
4282 * Single-byte codepages are handled with a different data structure
4283 * by _MBCSSingle... functions.
4284 *
4285 * The result consists of a 32-bit value from stage 2 and
4286 * a pointer to as many bytes as are stored per character.
4287 * The pointer points to the character's bytes in stage 3.
4288 * Bits 15..0 of the stage 2 entry contain the stage 3 index
4289 * for that pointer, while bits 31..16 are flags for which of
4290 * the 16 characters in the block are roundtrip-assigned.
4291 *
4292 * For 2-byte and 4-byte codepages, the bytes are stored as uint16_t
4293 * respectively as uint32_t, in the platform encoding.
4294 * For 3-byte codepages, the bytes are always stored in big-endian order.
4295 *
4296 * For EUC encodings that use only either 0x8e or 0x8f as the first
4297 * byte of their longest byte sequences, the first two bytes in
4298 * this third stage indicate with their 7th bits whether these bytes
4299 * are to be written directly or actually need to be preceeded by
4300 * one of the two Single-Shift codes. With this, the third stage
4301 * stores one byte fewer per character than the actual maximum length of
4302 * EUC byte sequences.
4303 *
4304 * Other than that, leading zero bytes are removed and the other
4305 * bytes output. A single zero byte may be output if the "assigned"
4306 * bit in stage 2 was on.
4307 * The data structure does not support zero byte output as a fallback,
4308 * and also does not allow output of leading zeros.
4309 */
4310 stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
4311
4312 /* get the bytes and the length for the output */
4313 switch(outputType) {
4314 case MBCS_OUTPUT_2:
4315 value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
4316 if(value<=0xff) {
4317 length=1;
4318 } else {
4319 length=2;
4320 }
4321 break;
4322 case MBCS_OUTPUT_2_SISO:
4323 /* 1/2-byte stateful with Shift-In/Shift-Out */
4324 /*
4325 * Save the old state in the converter object
4326 * right here, then change the local prevLength state variable if necessary.
4327 * Then, if this character turns out to be unassigned or a fallback that
4328 * is not taken, the callback code must not save the new state in the converter
4329 * because the new state is for a character that is not output.
4330 * However, the callback must still restore the state from the converter
4331 * in case the callback function changed it for its output.
4332 */
4333 cnv->fromUnicodeStatus=prevLength; /* save the old state */
4334 value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
4335 if(value<=0xff) {
4336 if(value==0 && MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c)==0) {
4337 /* no mapping, leave value==0 */
4338 length=0;
4339 } else if(prevLength<=1) {
4340 length=1;
4341 } else {
4342 /* change from double-byte mode to single-byte */
4343 if (si_value_length == 1) {
4344 value|=(uint32_t)si_value[0]<<8;
4345 length = 2;
4346 } else if (si_value_length == 2) {
4347 value|=(uint32_t)si_value[1]<<8;
4348 value|=(uint32_t)si_value[0]<<16;
4349 length = 3;
4350 }
4351 prevLength=1;
4352 }
4353 } else {
4354 if(prevLength==2) {
4355 length=2;
4356 } else {
4357 /* change from single-byte mode to double-byte */
4358 if (so_value_length == 1) {
4359 value|=(uint32_t)so_value[0]<<16;
4360 length = 3;
4361 } else if (so_value_length == 2) {
4362 value|=(uint32_t)so_value[1]<<16;
4363 value|=(uint32_t)so_value[0]<<24;
4364 length = 4;
4365 }
4366 prevLength=2;
4367 }
4368 }
4369 break;
4370 case MBCS_OUTPUT_DBCS_ONLY:
4371 /* table with single-byte results, but only DBCS mappings used */
4372 value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
4373 if(value<=0xff) {
4374 /* no mapping or SBCS result, not taken for DBCS-only */
4375 value=stage2Entry=0; /* stage2Entry=0 to reset roundtrip flags */
4376 length=0;
4377 } else {
4378 length=2;
4379 }
4380 break;
4381 case MBCS_OUTPUT_3:
4382 p=MBCS_POINTER_3_FROM_STAGE_2(bytes, stage2Entry, c);
4383 value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
4384 if(value<=0xff) {
4385 length=1;
4386 } else if(value<=0xffff) {
4387 length=2;
4388 } else {
4389 length=3;
4390 }
4391 break;
4392 case MBCS_OUTPUT_4:
4393 value=MBCS_VALUE_4_FROM_STAGE_2(bytes, stage2Entry, c);
4394 if(value<=0xff) {
4395 length=1;
4396 } else if(value<=0xffff) {
4397 length=2;
4398 } else if(value<=0xffffff) {
4399 length=3;
4400 } else {
4401 length=4;
4402 }
4403 break;
4404 case MBCS_OUTPUT_3_EUC:
4405 value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
4406 /* EUC 16-bit fixed-length representation */
4407 if(value<=0xff) {
4408 length=1;
4409 } else if((value&0x8000)==0) {
4410 value|=0x8e8000;
4411 length=3;
4412 } else if((value&0x80)==0) {
4413 value|=0x8f0080;
4414 length=3;
4415 } else {
4416 length=2;
4417 }
4418 break;
4419 case MBCS_OUTPUT_4_EUC:
4420 p=MBCS_POINTER_3_FROM_STAGE_2(bytes, stage2Entry, c);
4421 value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
4422 /* EUC 16-bit fixed-length representation applied to the first two bytes */
4423 if(value<=0xff) {
4424 length=1;
4425 } else if(value<=0xffff) {
4426 length=2;
4427 } else if((value&0x800000)==0) {
4428 value|=0x8e800000;
4429 length=4;
4430 } else if((value&0x8000)==0) {
4431 value|=0x8f008000;
4432 length=4;
4433 } else {
4434 length=3;
4435 }
4436 break;
4437 default:
4438 /* must not occur */
4439 /*
4440 * To avoid compiler warnings that value & length may be
4441 * used without having been initialized, we set them here.
4442 * In reality, this is unreachable code.
4443 * Not having a default branch also causes warnings with
4444 * some compilers.
4445 */
4446 value=stage2Entry=0; /* stage2Entry=0 to reset roundtrip flags */
4447 length=0;
4448 break;
4449 }
4450
4451 /* is this code point assigned, or do we use fallbacks? */
4452 if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c)!=0 ||
4453 (UCNV_FROM_U_USE_FALLBACK(cnv, c) && value!=0))
4454 ) {
4455 /*
4456 * We allow a 0 byte output if the "assigned" bit is set for this entry.
4457 * There is no way with this data structure for fallback output
4458 * to be a zero byte.
4459 */
4460
4461 unassigned:
4462 /* try an extension mapping */
4463 pArgs->source=source;
4464 c=_extFromU(cnv, cnv->sharedData,
4465 c, &source, sourceLimit,
4466 &target, target+targetCapacity,
4467 &offsets, sourceIndex,
4468 pArgs->flush,
4469 pErrorCode);
4470 nextSourceIndex+=(int32_t)(source-pArgs->source);
4471 prevLength=cnv->fromUnicodeStatus; /* restore SISO state */
4472
4473 if(U_FAILURE(*pErrorCode)) {
4474 /* not mappable or buffer overflow */
4475 break;
4476 } else {
4477 /* a mapping was written to the target, continue */
4478
4479 /* recalculate the targetCapacity after an extension mapping */
4480 targetCapacity=(int32_t)(pArgs->targetLimit-(char *)target);
4481
4482 /* normal end of conversion: prepare for a new character */
4483 if(offsets!=NULL) {
4484 prevSourceIndex=sourceIndex;
4485 sourceIndex=nextSourceIndex;
4486 }
4487 continue;
4488 }
4489 }
4490 }
4491
4492 /* write the output character bytes from value and length */
4493 /* from the first if in the loop we know that targetCapacity>0 */
4494 if(length<=targetCapacity) {
4495 if(offsets==NULL) {
4496 switch(length) {
4497 /* each branch falls through to the next one */
4498 case 4:
4499 *target++=(uint8_t)(value>>24);
4500 case 3: /*fall through*/
4501 *target++=(uint8_t)(value>>16);
4502 case 2: /*fall through*/
4503 *target++=(uint8_t)(value>>8);
4504 case 1: /*fall through*/
4505 *target++=(uint8_t)value;
4506 default:
4507 /* will never occur */
4508 break;
4509 }
4510 } else {
4511 switch(length) {
4512 /* each branch falls through to the next one */
4513 case 4:
4514 *target++=(uint8_t)(value>>24);
4515 *offsets++=sourceIndex;
4516 case 3: /*fall through*/
4517 *target++=(uint8_t)(value>>16);
4518 *offsets++=sourceIndex;
4519 case 2: /*fall through*/
4520 *target++=(uint8_t)(value>>8);
4521 *offsets++=sourceIndex;
4522 case 1: /*fall through*/
4523 *target++=(uint8_t)value;
4524 *offsets++=sourceIndex;
4525 default:
4526 /* will never occur */
4527 break;
4528 }
4529 }
4530 targetCapacity-=length;
4531 } else {
4532 uint8_t *charErrorBuffer;
4533
4534 /*
4535 * We actually do this backwards here:
4536 * In order to save an intermediate variable, we output
4537 * first to the overflow buffer what does not fit into the
4538 * regular target.
4539 */
4540 /* we know that 1<=targetCapacity<length<=4 */
4541 length-=targetCapacity;
4542 charErrorBuffer=(uint8_t *)cnv->charErrorBuffer;
4543 switch(length) {
4544 /* each branch falls through to the next one */
4545 case 3:
4546 *charErrorBuffer++=(uint8_t)(value>>16);
4547 case 2: /*fall through*/
4548 *charErrorBuffer++=(uint8_t)(value>>8);
4549 case 1: /*fall through*/
4550 *charErrorBuffer=(uint8_t)value;
4551 default:
4552 /* will never occur */
4553 break;
4554 }
4555 cnv->charErrorBufferLength=(int8_t)length;
4556
4557 /* now output what fits into the regular target */
4558 value>>=8*length; /* length was reduced by targetCapacity */
4559 switch(targetCapacity) {
4560 /* each branch falls through to the next one */
4561 case 3:
4562 *target++=(uint8_t)(value>>16);
4563 if(offsets!=NULL) {
4564 *offsets++=sourceIndex;
4565 }
4566 case 2: /*fall through*/
4567 *target++=(uint8_t)(value>>8);
4568 if(offsets!=NULL) {
4569 *offsets++=sourceIndex;
4570 }
4571 case 1: /*fall through*/
4572 *target++=(uint8_t)value;
4573 if(offsets!=NULL) {
4574 *offsets++=sourceIndex;
4575 }
4576 default:
4577 /* will never occur */
4578 break;
4579 }
4580
4581 /* target overflow */
4582 targetCapacity=0;
4583 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
4584 c=0;
4585 break;
4586 }
4587
4588 /* normal end of conversion: prepare for a new character */
4589 c=0;
4590 if(offsets!=NULL) {
4591 prevSourceIndex=sourceIndex;
4592 sourceIndex=nextSourceIndex;
4593 }
4594 continue;
4595 } else {
4596 /* target is full */
4597 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
4598 break;
4599 }
4600 }
4601
4602 /*
4603 * the end of the input stream and detection of truncated input
4604 * are handled by the framework, but for EBCDIC_STATEFUL conversion
4605 * we need to emit an SI at the very end
4606 *
4607 * conditions:
4608 * successful
4609 * EBCDIC_STATEFUL in DBCS mode
4610 * end of input and no truncated input
4611 */
4612 if( U_SUCCESS(*pErrorCode) &&
4613 outputType==MBCS_OUTPUT_2_SISO && prevLength==2 &&
4614 pArgs->flush && source>=sourceLimit && c==0
4615 ) {
4616 /* EBCDIC_STATEFUL ending with DBCS: emit an SI to return the output stream to SBCS */
4617 if(targetCapacity>0) {
4618 *target++=(uint8_t)si_value[0];
4619 if (si_value_length == 2) {
4620 if (targetCapacity<2) {
4621 cnv->charErrorBuffer[0]=(uint8_t)si_value[1];
4622 cnv->charErrorBufferLength=1;
4623 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
4624 } else {
4625 *target++=(uint8_t)si_value[1];
4626 }
4627 }
4628 if(offsets!=NULL) {
4629 /* set the last source character's index (sourceIndex points at sourceLimit now) */
4630 *offsets++=prevSourceIndex;
4631 }
4632 } else {
4633 /* target is full */
4634 cnv->charErrorBuffer[0]=(uint8_t)si_value[0];
4635 if (si_value_length == 2) {
4636 cnv->charErrorBuffer[1]=(uint8_t)si_value[1];
4637 }
4638 cnv->charErrorBufferLength=si_value_length;
4639 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
4640 }
4641 prevLength=1; /* we switched into SBCS */
4642 }
4643
4644 /* set the converter state back into UConverter */
4645 cnv->fromUChar32=c;
4646 cnv->fromUnicodeStatus=prevLength;
4647
4648 /* write back the updated pointers */
4649 pArgs->source=source;
4650 pArgs->target=(char *)target;
4651 pArgs->offsets=offsets;
4652 }
4653
4654 /*
4655 * This is another simple conversion function for internal use by other
4656 * conversion implementations.
4657 * It does not use the converter state nor call callbacks.
4658 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
4659 * It handles conversion extensions but not GB 18030.
4660 *
4661 * It converts one single Unicode code point into codepage bytes, encoded
4662 * as one 32-bit value. The function returns the number of bytes in *pValue:
4663 * 1..4 the number of bytes in *pValue
4664 * 0 unassigned (*pValue undefined)
4665 * -1 illegal (currently not used, *pValue undefined)
4666 *
4667 * *pValue will contain the resulting bytes with the last byte in bits 7..0,
4668 * the second to last byte in bits 15..8, etc.
4669 * Currently, the function assumes but does not check that 0<=c<=0x10ffff.
4670 */
4671 U_CFUNC int32_t
4672 ucnv_MBCSFromUChar32(UConverterSharedData *sharedData,
4673 UChar32 c, uint32_t *pValue,
4674 UBool useFallback) {
4675 const int32_t *cx;
4676 const uint16_t *table;
4677 #if 0
4678 /* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */
4679 const uint8_t *p;
4680 #endif
4681 uint32_t stage2Entry;
4682 uint32_t value;
4683 int32_t length;
4684
4685 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
4686 if(c<=0xffff || (sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
4687 table=sharedData->mbcs.fromUnicodeTable;
4688
4689 /* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
4690 if(sharedData->mbcs.outputType==MBCS_OUTPUT_1) {
4691 value=MBCS_SINGLE_RESULT_FROM_U(table, (uint16_t *)sharedData->mbcs.fromUnicodeBytes, c);
4692 /* is this code point assigned, or do we use fallbacks? */
4693 if(useFallback ? value>=0x800 : value>=0xc00) {
4694 *pValue=value&0xff;
4695 return 1;
4696 }
4697 } else /* outputType!=MBCS_OUTPUT_1 */ {
4698 stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
4699
4700 /* get the bytes and the length for the output */
4701 switch(sharedData->mbcs.outputType) {
4702 case MBCS_OUTPUT_2:
4703 value=MBCS_VALUE_2_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
4704 if(value<=0xff) {
4705 length=1;
4706 } else {
4707 length=2;
4708 }
4709 break;
4710 #if 0
4711 /* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */
4712 case MBCS_OUTPUT_DBCS_ONLY:
4713 /* table with single-byte results, but only DBCS mappings used */
4714 value=MBCS_VALUE_2_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
4715 if(value<=0xff) {
4716 /* no mapping or SBCS result, not taken for DBCS-only */
4717 value=stage2Entry=0; /* stage2Entry=0 to reset roundtrip flags */
4718 length=0;
4719 } else {
4720 length=2;
4721 }
4722 break;
4723 case MBCS_OUTPUT_3:
4724 p=MBCS_POINTER_3_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
4725 value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
4726 if(value<=0xff) {
4727 length=1;
4728 } else if(value<=0xffff) {
4729 length=2;
4730 } else {
4731 length=3;
4732 }
4733 break;
4734 case MBCS_OUTPUT_4:
4735 value=MBCS_VALUE_4_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
4736 if(value<=0xff) {
4737 length=1;
4738 } else if(value<=0xffff) {
4739 length=2;
4740 } else if(value<=0xffffff) {
4741 length=3;
4742 } else {
4743 length=4;
4744 }
4745 break;
4746 case MBCS_OUTPUT_3_EUC:
4747 value=MBCS_VALUE_2_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
4748 /* EUC 16-bit fixed-length representation */
4749 if(value<=0xff) {
4750 length=1;
4751 } else if((value&0x8000)==0) {
4752 value|=0x8e8000;
4753 length=3;
4754 } else if((value&0x80)==0) {
4755 value|=0x8f0080;
4756 length=3;
4757 } else {
4758 length=2;
4759 }
4760 break;
4761 case MBCS_OUTPUT_4_EUC:
4762 p=MBCS_POINTER_3_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
4763 value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
4764 /* EUC 16-bit fixed-length representation applied to the first two bytes */
4765 if(value<=0xff) {
4766 length=1;
4767 } else if(value<=0xffff) {
4768 length=2;
4769 } else if((value&0x800000)==0) {
4770 value|=0x8e800000;
4771 length=4;
4772 } else if((value&0x8000)==0) {
4773 value|=0x8f008000;
4774 length=4;
4775 } else {
4776 length=3;
4777 }
4778 break;
4779 #endif
4780 default:
4781 /* must not occur */
4782 return -1;
4783 }
4784
4785 /* is this code point assigned, or do we use fallbacks? */
4786 if( MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c) ||
4787 (FROM_U_USE_FALLBACK(useFallback, c) && value!=0)
4788 ) {
4789 /*
4790 * We allow a 0 byte output if the "assigned" bit is set for this entry.
4791 * There is no way with this data structure for fallback output
4792 * to be a zero byte.
4793 */
4794 /* assigned */
4795 *pValue=value;
4796 return length;
4797 }
4798 }
4799 }
4800
4801 cx=sharedData->mbcs.extIndexes;
4802 if(cx!=NULL) {
4803 length=ucnv_extSimpleMatchFromU(cx, c, pValue, useFallback);
4804 return length>=0 ? length : -length; /* return abs(length); */
4805 }
4806
4807 /* unassigned */
4808 return 0;
4809 }
4810
4811
4812 #if 0
4813 /*
4814 * This function has been moved to ucnv2022.c for inlining.
4815 * This implementation is here only for documentation purposes
4816 */
4817
4818 /**
4819 * This version of ucnv_MBCSFromUChar32() is optimized for single-byte codepages.
4820 * It does not handle the EBCDIC swaplfnl option (set in UConverter).
4821 * It does not handle conversion extensions (_extFromU()).
4822 *
4823 * It returns the codepage byte for the code point, or -1 if it is unassigned.
4824 */
4825 U_CFUNC int32_t
4826 ucnv_MBCSSingleFromUChar32(UConverterSharedData *sharedData,
4827 UChar32 c,
4828 UBool useFallback) {
4829 const uint16_t *table;
4830 int32_t value;
4831
4832 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
4833 if(c>=0x10000 && !(sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
4834 return -1;
4835 }
4836
4837 /* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
4838 table=sharedData->mbcs.fromUnicodeTable;
4839
4840 /* get the byte for the output */
4841 value=MBCS_SINGLE_RESULT_FROM_U(table, (uint16_t *)sharedData->mbcs.fromUnicodeBytes, c);
4842 /* is this code point assigned, or do we use fallbacks? */
4843 if(useFallback ? value>=0x800 : value>=0xc00) {
4844 return value&0xff;
4845 } else {
4846 return -1;
4847 }
4848 }
4849 #endif
4850
4851 /* MBCS-from-UTF-8 conversion functions ------------------------------------- */
4852
4853 /* minimum code point values for n-byte UTF-8 sequences, n=0..4 */
4854 static const UChar32
4855 utf8_minLegal[5]={ 0, 0, 0x80, 0x800, 0x10000 };
4856
4857 /* offsets for n-byte UTF-8 sequences that were calculated with ((lead<<6)+trail)<<6+trail... */
4858 static const UChar32
4859 utf8_offsets[7]={ 0, 0, 0x3080, 0xE2080, 0x3C82080 };
4860
4861 static void
4862 ucnv_SBCSFromUTF8(UConverterFromUnicodeArgs *pFromUArgs,
4863 UConverterToUnicodeArgs *pToUArgs,
4864 UErrorCode *pErrorCode) {
4865 UConverter *utf8, *cnv;
4866 const uint8_t *source, *sourceLimit;
4867 uint8_t *target;
4868 int32_t targetCapacity;
4869
4870 const uint16_t *table, *sbcsIndex;
4871 const uint16_t *results;
4872
4873 int8_t oldToULength, toULength, toULimit;
4874
4875 UChar32 c;
4876 uint8_t b, t1, t2;
4877
4878 uint32_t asciiRoundtrips;
4879 uint16_t value, minValue;
4880 UBool hasSupplementary;
4881
4882 /* set up the local pointers */
4883 utf8=pToUArgs->converter;
4884 cnv=pFromUArgs->converter;
4885 source=(uint8_t *)pToUArgs->source;
4886 sourceLimit=(uint8_t *)pToUArgs->sourceLimit;
4887 target=(uint8_t *)pFromUArgs->target;
4888 targetCapacity=(int32_t)(pFromUArgs->targetLimit-pFromUArgs->target);
4889
4890 table=cnv->sharedData->mbcs.fromUnicodeTable;
4891 sbcsIndex=cnv->sharedData->mbcs.sbcsIndex;
4892 if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
4893 results=(uint16_t *)cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes;
4894 } else {
4895 results=(uint16_t *)cnv->sharedData->mbcs.fromUnicodeBytes;
4896 }
4897 asciiRoundtrips=cnv->sharedData->mbcs.asciiRoundtrips;
4898
4899 if(cnv->useFallback) {
4900 /* use all roundtrip and fallback results */
4901 minValue=0x800;
4902 } else {
4903 /* use only roundtrips and fallbacks from private-use characters */
4904 minValue=0xc00;
4905 }
4906 hasSupplementary=(UBool)(cnv->sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY);
4907
4908 /* get the converter state from the UTF-8 UConverter */
4909 c=(UChar32)utf8->toUnicodeStatus;
4910 if(c!=0) {
4911 toULength=oldToULength=utf8->toULength;
4912 toULimit=(int8_t)utf8->mode;
4913 } else {
4914 toULength=oldToULength=toULimit=0;
4915 }
4916
4917 /*
4918 * Make sure that the last byte sequence before sourceLimit is complete
4919 * or runs into a lead byte.
4920 * Do not go back into the bytes that will be read for finishing a partial
4921 * sequence from the previous buffer.
4922 * In the conversion loop compare source with sourceLimit only once
4923 * per multi-byte character.
4924 */
4925 {
4926 int32_t i, length;
4927
4928 length=(int32_t)(sourceLimit-source) - (toULimit-oldToULength);
4929 for(i=0; i<3 && i<length;) {
4930 b=*(sourceLimit-i-1);
4931 if(U8_IS_TRAIL(b)) {
4932 ++i;
4933 } else {
4934 if(i<utf8_countTrailBytes[b]) {
4935 /* exit the conversion loop before the lead byte if there are not enough trail bytes for it */
4936 sourceLimit-=i+1;
4937 }
4938 break;
4939 }
4940 }
4941 }
4942
4943 if(c!=0 && targetCapacity>0) {
4944 utf8->toUnicodeStatus=0;
4945 utf8->toULength=0;
4946 goto moreBytes;
4947 /*
4948 * Note: We could avoid the goto by duplicating some of the moreBytes
4949 * code, but only up to the point of collecting a complete UTF-8
4950 * sequence; then recurse for the toUBytes[toULength]
4951 * and then continue with normal conversion.
4952 *
4953 * If so, move this code to just after initializing the minimum
4954 * set of local variables for reading the UTF-8 input
4955 * (utf8, source, target, limits but not cnv, table, minValue, etc.).
4956 *
4957 * Potential advantages:
4958 * - avoid the goto
4959 * - oldToULength could become a local variable in just those code blocks
4960 * that deal with buffer boundaries
4961 * - possibly faster if the goto prevents some compiler optimizations
4962 * (this would need measuring to confirm)
4963 * Disadvantage:
4964 * - code duplication
4965 */
4966 }
4967
4968 /* conversion loop */
4969 while(source<sourceLimit) {
4970 if(targetCapacity>0) {
4971 b=*source++;
4972 if((int8_t)b>=0) {
4973 /* convert ASCII */
4974 if(IS_ASCII_ROUNDTRIP(b, asciiRoundtrips)) {
4975 *target++=(uint8_t)b;
4976 --targetCapacity;
4977 continue;
4978 } else {
4979 c=b;
4980 value=SBCS_RESULT_FROM_UTF8(sbcsIndex, results, 0, c);
4981 }
4982 } else {
4983 if(b<0xe0) {
4984 if( /* handle U+0080..U+07FF inline */
4985 b>=0xc2 &&
4986 (t1=(uint8_t)(*source-0x80)) <= 0x3f
4987 ) {
4988 c=b&0x1f;
4989 ++source;
4990 value=SBCS_RESULT_FROM_UTF8(sbcsIndex, results, c, t1);
4991 if(value>=minValue) {
4992 *target++=(uint8_t)value;
4993 --targetCapacity;
4994 continue;
4995 } else {
4996 c=(c<<6)|t1;
4997 }
4998 } else {
4999 c=-1;
5000 }
5001 } else if(b==0xe0) {
5002 if( /* handle U+0800..U+0FFF inline */
5003 (t1=(uint8_t)(source[0]-0x80)) <= 0x3f && t1 >= 0x20 &&
5004 (t2=(uint8_t)(source[1]-0x80)) <= 0x3f
5005 ) {
5006 c=t1;
5007 source+=2;
5008 value=SBCS_RESULT_FROM_UTF8(sbcsIndex, results, c, t2);
5009 if(value>=minValue) {
5010 *target++=(uint8_t)value;
5011 --targetCapacity;
5012 continue;
5013 } else {
5014 c=(c<<6)|t2;
5015 }
5016 } else {
5017 c=-1;
5018 }
5019 } else {
5020 c=-1;
5021 }
5022
5023 if(c<0) {
5024 /* handle "complicated" and error cases, and continuing partial characters */
5025 oldToULength=0;
5026 toULength=1;
5027 toULimit=utf8_countTrailBytes[b]+1;
5028 c=b;
5029 moreBytes:
5030 while(toULength<toULimit) {
5031 /*
5032 * The sourceLimit may have been adjusted before the conversion loop
5033 * to stop before a truncated sequence.
5034 * Here we need to use the real limit in case we have two truncated
5035 * sequences at the end.
5036 * See ticket #7492.
5037 */
5038 if(source<(uint8_t *)pToUArgs->sourceLimit) {
5039 b=*source;
5040 if(U8_IS_TRAIL(b)) {
5041 ++source;
5042 ++toULength;
5043 c=(c<<6)+b;
5044 } else {
5045 break; /* sequence too short, stop with toULength<toULimit */
5046 }
5047 } else {
5048 /* store the partial UTF-8 character, compatible with the regular UTF-8 converter */
5049 source-=(toULength-oldToULength);
5050 while(oldToULength<toULength) {
5051 utf8->toUBytes[oldToULength++]=*source++;
5052 }
5053 utf8->toUnicodeStatus=c;
5054 utf8->toULength=toULength;
5055 utf8->mode=toULimit;
5056 pToUArgs->source=(char *)source;
5057 pFromUArgs->target=(char *)target;
5058 return;
5059 }
5060 }
5061
5062 if( toULength==toULimit && /* consumed all trail bytes */
5063 (toULength==3 || toULength==2) && /* BMP */
5064 (c-=utf8_offsets[toULength])>=utf8_minLegal[toULength] &&
5065 (c<=0xd7ff || 0xe000<=c) /* not a surrogate */
5066 ) {
5067 value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
5068 } else if(
5069 toULength==toULimit && toULength==4 &&
5070 (0x10000<=(c-=utf8_offsets[4]) && c<=0x10ffff)
5071 ) {
5072 /* supplementary code point */
5073 if(!hasSupplementary) {
5074 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
5075 value=0;
5076 } else {
5077 value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
5078 }
5079 } else {
5080 /* error handling: illegal UTF-8 byte sequence */
5081 source-=(toULength-oldToULength);
5082 while(oldToULength<toULength) {
5083 utf8->toUBytes[oldToULength++]=*source++;
5084 }
5085 utf8->toULength=toULength;
5086 pToUArgs->source=(char *)source;
5087 pFromUArgs->target=(char *)target;
5088 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
5089 return;
5090 }
5091 }
5092 }
5093
5094 if(value>=minValue) {
5095 /* output the mapping for c */
5096 *target++=(uint8_t)value;
5097 --targetCapacity;
5098 } else {
5099 /* value<minValue means c is unassigned (unmappable) */
5100 /*
5101 * Try an extension mapping.
5102 * Pass in no source because we don't have UTF-16 input.
5103 * If we have a partial match on c, we will return and revert
5104 * to UTF-8->UTF-16->charset conversion.
5105 */
5106 static const UChar nul=0;
5107 const UChar *noSource=&nul;
5108 c=_extFromU(cnv, cnv->sharedData,
5109 c, &noSource, noSource,
5110 &target, target+targetCapacity,
5111 NULL, -1,
5112 pFromUArgs->flush,
5113 pErrorCode);
5114
5115 if(U_FAILURE(*pErrorCode)) {
5116 /* not mappable or buffer overflow */
5117 cnv->fromUChar32=c;
5118 break;
5119 } else if(cnv->preFromUFirstCP>=0) {
5120 /*
5121 * Partial match, return and revert to pivoting.
5122 * In normal from-UTF-16 conversion, we would just continue
5123 * but then exit the loop because the extension match would
5124 * have consumed the source.
5125 */
5126 break;
5127 } else {
5128 /* a mapping was written to the target, continue */
5129
5130 /* recalculate the targetCapacity after an extension mapping */
5131 targetCapacity=(int32_t)(pFromUArgs->targetLimit-(char *)target);
5132 }
5133 }
5134 } else {
5135 /* target is full */
5136 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
5137 break;
5138 }
5139 }
5140
5141 /*
5142 * The sourceLimit may have been adjusted before the conversion loop
5143 * to stop before a truncated sequence.
5144 * If so, then collect the truncated sequence now.
5145 */
5146 if(U_SUCCESS(*pErrorCode) && source<(sourceLimit=(uint8_t *)pToUArgs->sourceLimit)) {
5147 c=utf8->toUBytes[0]=b=*source++;
5148 toULength=1;
5149 toULimit=utf8_countTrailBytes[b]+1;
5150 while(source<sourceLimit) {
5151 utf8->toUBytes[toULength++]=b=*source++;
5152 c=(c<<6)+b;
5153 }
5154 utf8->toUnicodeStatus=c;
5155 utf8->toULength=toULength;
5156 utf8->mode=toULimit;
5157 }
5158
5159 /* write back the updated pointers */
5160 pToUArgs->source=(char *)source;
5161 pFromUArgs->target=(char *)target;
5162 }
5163
5164 static void
5165 ucnv_DBCSFromUTF8(UConverterFromUnicodeArgs *pFromUArgs,
5166 UConverterToUnicodeArgs *pToUArgs,
5167 UErrorCode *pErrorCode) {
5168 UConverter *utf8, *cnv;
5169 const uint8_t *source, *sourceLimit;
5170 uint8_t *target;
5171 int32_t targetCapacity;
5172
5173 const uint16_t *table, *mbcsIndex;
5174 const uint16_t *results;
5175
5176 int8_t oldToULength, toULength, toULimit;
5177
5178 UChar32 c;
5179 uint8_t b, t1, t2;
5180
5181 uint32_t stage2Entry;
5182 uint32_t asciiRoundtrips;
5183 uint16_t value;
5184 UBool hasSupplementary;
5185
5186 /* set up the local pointers */
5187 utf8=pToUArgs->converter;
5188 cnv=pFromUArgs->converter;
5189 source=(uint8_t *)pToUArgs->source;
5190 sourceLimit=(uint8_t *)pToUArgs->sourceLimit;
5191 target=(uint8_t *)pFromUArgs->target;
5192 targetCapacity=(int32_t)(pFromUArgs->targetLimit-pFromUArgs->target);
5193
5194 table=cnv->sharedData->mbcs.fromUnicodeTable;
5195 mbcsIndex=cnv->sharedData->mbcs.mbcsIndex;
5196 if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
5197 results=(uint16_t *)cnv->sharedData->mbcs.swapLFNLFromUnicodeBytes;
5198 } else {
5199 results=(uint16_t *)cnv->sharedData->mbcs.fromUnicodeBytes;
5200 }
5201 asciiRoundtrips=cnv->sharedData->mbcs.asciiRoundtrips;
5202
5203 hasSupplementary=(UBool)(cnv->sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY);
5204
5205 /* get the converter state from the UTF-8 UConverter */
5206 c=(UChar32)utf8->toUnicodeStatus;
5207 if(c!=0) {
5208 toULength=oldToULength=utf8->toULength;
5209 toULimit=(int8_t)utf8->mode;
5210 } else {
5211 toULength=oldToULength=toULimit=0;
5212 }
5213
5214 /*
5215 * Make sure that the last byte sequence before sourceLimit is complete
5216 * or runs into a lead byte.
5217 * Do not go back into the bytes that will be read for finishing a partial
5218 * sequence from the previous buffer.
5219 * In the conversion loop compare source with sourceLimit only once
5220 * per multi-byte character.
5221 */
5222 {
5223 int32_t i, length;
5224
5225 length=(int32_t)(sourceLimit-source) - (toULimit-oldToULength);
5226 for(i=0; i<3 && i<length;) {
5227 b=*(sourceLimit-i-1);
5228 if(U8_IS_TRAIL(b)) {
5229 ++i;
5230 } else {
5231 if(i<utf8_countTrailBytes[b]) {
5232 /* exit the conversion loop before the lead byte if there are not enough trail bytes for it */
5233 sourceLimit-=i+1;
5234 }
5235 break;
5236 }
5237 }
5238 }
5239
5240 if(c!=0 && targetCapacity>0) {
5241 utf8->toUnicodeStatus=0;
5242 utf8->toULength=0;
5243 goto moreBytes;
5244 /* See note in ucnv_SBCSFromUTF8() about this goto. */
5245 }
5246
5247 /* conversion loop */
5248 while(source<sourceLimit) {
5249 if(targetCapacity>0) {
5250 b=*source++;
5251 if((int8_t)b>=0) {
5252 /* convert ASCII */
5253 if(IS_ASCII_ROUNDTRIP(b, asciiRoundtrips)) {
5254 *target++=b;
5255 --targetCapacity;
5256 continue;
5257 } else {
5258 value=DBCS_RESULT_FROM_UTF8(mbcsIndex, results, 0, b);
5259 if(value==0) {
5260 c=b;
5261 goto unassigned;
5262 }
5263 }
5264 } else {
5265 if(b>0xe0) {
5266 if( /* handle U+1000..U+D7FF inline */
5267 (((t1=(uint8_t)(source[0]-0x80), b<0xed) && (t1 <= 0x3f)) ||
5268 (b==0xed && (t1 <= 0x1f))) &&
5269 (t2=(uint8_t)(source[1]-0x80)) <= 0x3f
5270 ) {
5271 c=((b&0xf)<<6)|t1;
5272 source+=2;
5273 value=DBCS_RESULT_FROM_UTF8(mbcsIndex, results, c, t2);
5274 if(value==0) {
5275 c=(c<<6)|t2;
5276 goto unassigned;
5277 }
5278 } else {
5279 c=-1;
5280 }
5281 } else if(b<0xe0) {
5282 if( /* handle U+0080..U+07FF inline */
5283 b>=0xc2 &&
5284 (t1=(uint8_t)(*source-0x80)) <= 0x3f
5285 ) {
5286 c=b&0x1f;
5287 ++source;
5288 value=DBCS_RESULT_FROM_UTF8(mbcsIndex, results, c, t1);
5289 if(value==0) {
5290 c=(c<<6)|t1;
5291 goto unassigned;
5292 }
5293 } else {
5294 c=-1;
5295 }
5296 } else {
5297 c=-1;
5298 }
5299
5300 if(c<0) {
5301 /* handle "complicated" and error cases, and continuing partial characters */
5302 oldToULength=0;
5303 toULength=1;
5304 toULimit=utf8_countTrailBytes[b]+1;
5305 c=b;
5306 moreBytes:
5307 while(toULength<toULimit) {
5308 /*
5309 * The sourceLimit may have been adjusted before the conversion loop
5310 * to stop before a truncated sequence.
5311 * Here we need to use the real limit in case we have two truncated
5312 * sequences at the end.
5313 * See ticket #7492.
5314 */
5315 if(source<(uint8_t *)pToUArgs->sourceLimit) {
5316 b=*source;
5317 if(U8_IS_TRAIL(b)) {
5318 ++source;
5319 ++toULength;
5320 c=(c<<6)+b;
5321 } else {
5322 break; /* sequence too short, stop with toULength<toULimit */
5323 }
5324 } else {
5325 /* store the partial UTF-8 character, compatible with the regular UTF-8 converter */
5326 source-=(toULength-oldToULength);
5327 while(oldToULength<toULength) {
5328 utf8->toUBytes[oldToULength++]=*source++;
5329 }
5330 utf8->toUnicodeStatus=c;
5331 utf8->toULength=toULength;
5332 utf8->mode=toULimit;
5333 pToUArgs->source=(char *)source;
5334 pFromUArgs->target=(char *)target;
5335 return;
5336 }
5337 }
5338
5339 if( toULength==toULimit && /* consumed all trail bytes */
5340 (toULength==3 || toULength==2) && /* BMP */
5341 (c-=utf8_offsets[toULength])>=utf8_minLegal[toULength] &&
5342 (c<=0xd7ff || 0xe000<=c) /* not a surrogate */
5343 ) {
5344 stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
5345 } else if(
5346 toULength==toULimit && toULength==4 &&
5347 (0x10000<=(c-=utf8_offsets[4]) && c<=0x10ffff)
5348 ) {
5349 /* supplementary code point */
5350 if(!hasSupplementary) {
5351 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
5352 stage2Entry=0;
5353 } else {
5354 stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
5355 }
5356 } else {
5357 /* error handling: illegal UTF-8 byte sequence */
5358 source-=(toULength-oldToULength);
5359 while(oldToULength<toULength) {
5360 utf8->toUBytes[oldToULength++]=*source++;
5361 }
5362 utf8->toULength=toULength;
5363 pToUArgs->source=(char *)source;
5364 pFromUArgs->target=(char *)target;
5365 *pErrorCode=U_ILLEGAL_CHAR_FOUND;
5366 return;
5367 }
5368
5369 /* get the bytes and the length for the output */
5370 /* MBCS_OUTPUT_2 */
5371 value=MBCS_VALUE_2_FROM_STAGE_2(results, stage2Entry, c);
5372
5373 /* is this code point assigned, or do we use fallbacks? */
5374 if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c) ||
5375 (UCNV_FROM_U_USE_FALLBACK(cnv, c) && value!=0))
5376 ) {
5377 goto unassigned;
5378 }
5379 }
5380 }
5381
5382 /* write the output character bytes from value and length */
5383 /* from the first if in the loop we know that targetCapacity>0 */
5384 if(value<=0xff) {
5385 /* this is easy because we know that there is enough space */
5386 *target++=(uint8_t)value;
5387 --targetCapacity;
5388 } else /* length==2 */ {
5389 *target++=(uint8_t)(value>>8);
5390 if(2<=targetCapacity) {
5391 *target++=(uint8_t)value;
5392 targetCapacity-=2;
5393 } else {
5394 cnv->charErrorBuffer[0]=(char)value;
5395 cnv->charErrorBufferLength=1;
5396
5397 /* target overflow */
5398 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
5399 break;
5400 }
5401 }
5402 continue;
5403
5404 unassigned:
5405 {
5406 /*
5407 * Try an extension mapping.
5408 * Pass in no source because we don't have UTF-16 input.
5409 * If we have a partial match on c, we will return and revert
5410 * to UTF-8->UTF-16->charset conversion.
5411 */
5412 static const UChar nul=0;
5413 const UChar *noSource=&nul;
5414 c=_extFromU(cnv, cnv->sharedData,
5415 c, &noSource, noSource,
5416 &target, target+targetCapacity,
5417 NULL, -1,
5418 pFromUArgs->flush,
5419 pErrorCode);
5420
5421 if(U_FAILURE(*pErrorCode)) {
5422 /* not mappable or buffer overflow */
5423 cnv->fromUChar32=c;
5424 break;
5425 } else if(cnv->preFromUFirstCP>=0) {
5426 /*
5427 * Partial match, return and revert to pivoting.
5428 * In normal from-UTF-16 conversion, we would just continue
5429 * but then exit the loop because the extension match would
5430 * have consumed the source.
5431 */
5432 break;
5433 } else {
5434 /* a mapping was written to the target, continue */
5435
5436 /* recalculate the targetCapacity after an extension mapping */
5437 targetCapacity=(int32_t)(pFromUArgs->targetLimit-(char *)target);
5438 continue;
5439 }
5440 }
5441 } else {
5442 /* target is full */
5443 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
5444 break;
5445 }
5446 }
5447
5448 /*
5449 * The sourceLimit may have been adjusted before the conversion loop
5450 * to stop before a truncated sequence.
5451 * If so, then collect the truncated sequence now.
5452 */
5453 if(U_SUCCESS(*pErrorCode) && source<(sourceLimit=(uint8_t *)pToUArgs->sourceLimit)) {
5454 c=utf8->toUBytes[0]=b=*source++;
5455 toULength=1;
5456 toULimit=utf8_countTrailBytes[b]+1;
5457 while(source<sourceLimit) {
5458 utf8->toUBytes[toULength++]=b=*source++;
5459 c=(c<<6)+b;
5460 }
5461 utf8->toUnicodeStatus=c;
5462 utf8->toULength=toULength;
5463 utf8->mode=toULimit;
5464 }
5465
5466 /* write back the updated pointers */
5467 pToUArgs->source=(char *)source;
5468 pFromUArgs->target=(char *)target;
5469 }
5470
5471 /* miscellaneous ------------------------------------------------------------ */
5472
5473 static void
5474 ucnv_MBCSGetStarters(const UConverter* cnv,
5475 UBool starters[256],
5476 UErrorCode *pErrorCode) {
5477 const int32_t *state0;
5478 int i;
5479
5480 state0=cnv->sharedData->mbcs.stateTable[cnv->sharedData->mbcs.dbcsOnlyState];
5481 for(i=0; i<256; ++i) {
5482 /* all bytes that cause a state transition from state 0 are lead bytes */
5483 starters[i]= (UBool)MBCS_ENTRY_IS_TRANSITION(state0[i]);
5484 }
5485 }
5486
5487 /*
5488 * This is an internal function that allows other converter implementations
5489 * to check whether a byte is a lead byte.
5490 */
5491 U_CFUNC UBool
5492 ucnv_MBCSIsLeadByte(UConverterSharedData *sharedData, char byte) {
5493 return (UBool)MBCS_ENTRY_IS_TRANSITION(sharedData->mbcs.stateTable[0][(uint8_t)byte]);
5494 }
5495
5496 static void
5497 ucnv_MBCSWriteSub(UConverterFromUnicodeArgs *pArgs,
5498 int32_t offsetIndex,
5499 UErrorCode *pErrorCode) {
5500 UConverter *cnv=pArgs->converter;
5501 char *p, *subchar;
5502 char buffer[4];
5503 int32_t length;
5504
5505 /* first, select between subChar and subChar1 */
5506 if( cnv->subChar1!=0 &&
5507 (cnv->sharedData->mbcs.extIndexes!=NULL ?
5508 cnv->useSubChar1 :
5509 (cnv->invalidUCharBuffer[0]<=0xff))
5510 ) {
5511 /* select subChar1 if it is set (not 0) and the unmappable Unicode code point is up to U+00ff (IBM MBCS behavior) */
5512 subchar=(char *)&cnv->subChar1;
5513 length=1;
5514 } else {
5515 /* select subChar in all other cases */
5516 subchar=(char *)cnv->subChars;
5517 length=cnv->subCharLen;
5518 }
5519
5520 /* reset the selector for the next code point */
5521 cnv->useSubChar1=FALSE;
5522
5523 if (cnv->sharedData->mbcs.outputType == MBCS_OUTPUT_2_SISO) {
5524 p=buffer;
5525
5526 /* fromUnicodeStatus contains prevLength */
5527 switch(length) {
5528 case 1:
5529 if(cnv->fromUnicodeStatus==2) {
5530 /* DBCS mode and SBCS sub char: change to SBCS */
5531 cnv->fromUnicodeStatus=1;
5532 *p++=UCNV_SI;
5533 }
5534 *p++=subchar[0];
5535 break;
5536 case 2:
5537 if(cnv->fromUnicodeStatus<=1) {
5538 /* SBCS mode and DBCS sub char: change to DBCS */
5539 cnv->fromUnicodeStatus=2;
5540 *p++=UCNV_SO;
5541 }
5542 *p++=subchar[0];
5543 *p++=subchar[1];
5544 break;
5545 default:
5546 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
5547 return;
5548 }
5549 subchar=buffer;
5550 length=(int32_t)(p-buffer);
5551 }
5552
5553 ucnv_cbFromUWriteBytes(pArgs, subchar, length, offsetIndex, pErrorCode);
5554 }
5555
5556 U_CFUNC UConverterType
5557 ucnv_MBCSGetType(const UConverter* converter) {
5558 /* SBCS, DBCS, and EBCDIC_STATEFUL are replaced by MBCS, but here we cheat a little */
5559 if(converter->sharedData->mbcs.countStates==1) {
5560 return (UConverterType)UCNV_SBCS;
5561 } else if((converter->sharedData->mbcs.outputType&0xff)==MBCS_OUTPUT_2_SISO) {
5562 return (UConverterType)UCNV_EBCDIC_STATEFUL;
5563 } else if(converter->sharedData->staticData->minBytesPerChar==2 && converter->sharedData->staticData->maxBytesPerChar==2) {
5564 return (UConverterType)UCNV_DBCS;
5565 }
5566 return (UConverterType)UCNV_MBCS;
5567 }
5568
5569 static const UConverterImpl _SBCSUTF8Impl={
5570 UCNV_MBCS,
5571
5572 ucnv_MBCSLoad,
5573 ucnv_MBCSUnload,
5574
5575 ucnv_MBCSOpen,
5576 NULL,
5577 NULL,
5578
5579 ucnv_MBCSToUnicodeWithOffsets,
5580 ucnv_MBCSToUnicodeWithOffsets,
5581 ucnv_MBCSFromUnicodeWithOffsets,
5582 ucnv_MBCSFromUnicodeWithOffsets,
5583 ucnv_MBCSGetNextUChar,
5584
5585 ucnv_MBCSGetStarters,
5586 ucnv_MBCSGetName,
5587 ucnv_MBCSWriteSub,
5588 NULL,
5589 ucnv_MBCSGetUnicodeSet,
5590
5591 NULL,
5592 ucnv_SBCSFromUTF8
5593 };
5594
5595 static const UConverterImpl _DBCSUTF8Impl={
5596 UCNV_MBCS,
5597
5598 ucnv_MBCSLoad,
5599 ucnv_MBCSUnload,
5600
5601 ucnv_MBCSOpen,
5602 NULL,
5603 NULL,
5604
5605 ucnv_MBCSToUnicodeWithOffsets,
5606 ucnv_MBCSToUnicodeWithOffsets,
5607 ucnv_MBCSFromUnicodeWithOffsets,
5608 ucnv_MBCSFromUnicodeWithOffsets,
5609 ucnv_MBCSGetNextUChar,
5610
5611 ucnv_MBCSGetStarters,
5612 ucnv_MBCSGetName,
5613 ucnv_MBCSWriteSub,
5614 NULL,
5615 ucnv_MBCSGetUnicodeSet,
5616
5617 NULL,
5618 ucnv_DBCSFromUTF8
5619 };
5620
5621 static const UConverterImpl _MBCSImpl={
5622 UCNV_MBCS,
5623
5624 ucnv_MBCSLoad,
5625 ucnv_MBCSUnload,
5626
5627 ucnv_MBCSOpen,
5628 NULL,
5629 NULL,
5630
5631 ucnv_MBCSToUnicodeWithOffsets,
5632 ucnv_MBCSToUnicodeWithOffsets,
5633 ucnv_MBCSFromUnicodeWithOffsets,
5634 ucnv_MBCSFromUnicodeWithOffsets,
5635 ucnv_MBCSGetNextUChar,
5636
5637 ucnv_MBCSGetStarters,
5638 ucnv_MBCSGetName,
5639 ucnv_MBCSWriteSub,
5640 NULL,
5641 ucnv_MBCSGetUnicodeSet
5642 };
5643
5644
5645 /* Static data is in tools/makeconv/ucnvstat.c for data-based
5646 * converters. Be sure to update it as well.
5647 */
5648
5649 const UConverterSharedData _MBCSData={
5650 sizeof(UConverterSharedData), 1,
5651 NULL, NULL, NULL, FALSE, &_MBCSImpl,
5652 0
5653 };
5654
5655 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */