intl/icu/source/common/ucnvmbcs.c

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial