2 **********************************************************************
3 * Copyright (C) 2000-2015, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * file name: ucnv2022.cpp
8 * tab size: 8 (not used)
11 * created on: 2000feb03
12 * created by: Markus W. Scherer
16 * 06/29/2000 helena Major rewrite of the callback APIs.
17 * 08/08/2000 Ram Included support for ISO-2022-JP-2
18 * Changed implementation of toUnicode
20 * 08/21/2000 Ram Added support for ISO-2022-KR
21 * 08/29/2000 Ram Seperated implementation of EBCDIC to
23 * 09/20/2000 Ram Added support for ISO-2022-CN
24 * Added implementations for getNextUChar()
25 * for specific 2022 country variants.
26 * 10/31/2000 Ram Implemented offsets logic functions
29 #include "unicode/utypes.h"
31 #if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION
33 #include "unicode/ucnv.h"
34 #include "unicode/uset.h"
35 #include "unicode/ucnv_err.h"
36 #include "unicode/ucnv_cb.h"
37 #include "unicode/utf16.h"
46 #ifdef U_ENABLE_GENERIC_ISO_2022
48 * I am disabling the generic ISO-2022 converter after proposing to do so on
49 * the icu mailing list two days ago.
52 * 1. It does not fully support the ISO-2022/ECMA-35 specification with all of
53 * its designation sequences, single shifts with return to the previous state,
54 * switch-with-no-return to UTF-16BE or similar, etc.
55 * This is unlike the language-specific variants like ISO-2022-JP which
56 * require a much smaller repertoire of ISO-2022 features.
57 * These variants continue to be supported.
58 * 2. I believe that no one is really using the generic ISO-2022 converter
59 * but rather always one of the language-specific variants.
60 * Note that ICU's generic ISO-2022 converter has always output one escape
61 * sequence followed by UTF-8 for the whole stream.
62 * 3. Switching between subcharsets is extremely slow, because each time
63 * the previous converter is closed and a new one opened,
64 * without any kind of caching, least-recently-used list, etc.
65 * 4. The code is currently buggy, and given the above it does not seem
66 * reasonable to spend the time on maintenance.
67 * 5. ISO-2022 subcharsets should normally be used with 7-bit byte encodings.
68 * This means, for example, that when ISO-8859-7 is designated, the following
69 * ISO-2022 bytes 00..7f should be interpreted as ISO-8859-7 bytes 80..ff.
70 * The ICU ISO-2022 converter does not handle this - and has no information
71 * about which subconverter would have to be shifted vs. which is designed
74 * Markus Scherer 2003-dec-03
78 #if !UCONFIG_ONLY_HTML_CONVERSION
79 static const char SHIFT_IN_STR
[] = "\x0F";
80 // static const char SHIFT_OUT_STR[] = "\x0E";
95 * 94-character sets with native byte values A1..FE are encoded in ISO 2022
96 * as bytes 21..7E. (Subtract 0x80.)
97 * 96-character sets with native byte values A0..FF are encoded in ISO 2022
98 * as bytes 20..7F. (Subtract 0x80.)
99 * Do not encode C1 control codes with native bytes 80..9F
100 * as bytes 00..1F (C0 control codes).
110 * ISO 2022 control codes must not be converted from Unicode
111 * because they would mess up the byte stream.
112 * The bit mask 0x0800c000 has bits set at bit positions 0xe, 0xf, 0x1b
113 * corresponding to SO, SI, and ESC.
115 #define IS_2022_CONTROL(c) (((c)<0x20) && (((uint32_t)1<<(c))&0x0800c000)!=0)
117 /* for ISO-2022-JP and -CN implementations */
134 HWKANA_7BIT
=8, /* Halfwidth Katakana 7 bit */
137 /* the first few enum constants must keep their values because they correspond to myConverterArray[] */
143 * these are used in StateEnum and ISO2022State variables,
144 * but CNS_11643 must be used to index into myConverterArray[]
156 /* is the StateEnum charset value for a DBCS charset? */
157 #if UCONFIG_ONLY_HTML_CONVERSION
158 #define IS_JP_DBCS(cs) (JISX208==(cs))
160 #define IS_JP_DBCS(cs) (JISX208<=(cs) && (cs)<=KSC5601)
163 #define CSM(cs) ((uint16_t)1<<(cs))
166 * Each of these charset masks (with index x) contains a bit for a charset in exact correspondence
167 * to whether that charset is used in the corresponding version x of ISO_2022,locale=ja,version=x
169 * Note: The converter uses some leniency:
170 * - The escape sequence ESC ( I for half-width 7-bit Katakana is recognized in
171 * all versions, not just JIS7 and JIS8.
172 * - ICU does not distinguish between different versions of JIS X 0208.
174 #if UCONFIG_ONLY_HTML_CONVERSION
175 enum { MAX_JA_VERSION
=0 };
177 enum { MAX_JA_VERSION
=4 };
179 static const uint16_t jpCharsetMasks
[MAX_JA_VERSION
+1]={
180 CSM(ASCII
)|CSM(JISX201
)|CSM(JISX208
)|CSM(HWKANA_7BIT
),
181 #if !UCONFIG_ONLY_HTML_CONVERSION
182 CSM(ASCII
)|CSM(JISX201
)|CSM(JISX208
)|CSM(HWKANA_7BIT
)|CSM(JISX212
),
183 CSM(ASCII
)|CSM(JISX201
)|CSM(JISX208
)|CSM(HWKANA_7BIT
)|CSM(JISX212
)|CSM(GB2312
)|CSM(KSC5601
)|CSM(ISO8859_1
)|CSM(ISO8859_7
),
184 CSM(ASCII
)|CSM(JISX201
)|CSM(JISX208
)|CSM(HWKANA_7BIT
)|CSM(JISX212
)|CSM(GB2312
)|CSM(KSC5601
)|CSM(ISO8859_1
)|CSM(ISO8859_7
),
185 CSM(ASCII
)|CSM(JISX201
)|CSM(JISX208
)|CSM(HWKANA_7BIT
)|CSM(JISX212
)|CSM(GB2312
)|CSM(KSC5601
)|CSM(ISO8859_1
)|CSM(ISO8859_7
)
198 typedef struct ISO2022State
{
199 int8_t cs
[4]; /* charset number for SI (G0)/SO (G1)/SS2 (G2)/SS3 (G3) */
200 int8_t g
; /* 0..3 for G0..G3 (SI/SO/SS2/SS3) */
201 int8_t prevG
; /* g before single shift (SS2 or SS3) */
204 #define UCNV_OPTIONS_VERSION_MASK 0xf
205 #define UCNV_2022_MAX_CONVERTERS 10
208 UConverterSharedData
*myConverterArray
[UCNV_2022_MAX_CONVERTERS
];
209 UConverter
*currentConverter
;
210 Cnv2022Type currentType
;
211 ISO2022State toU2022State
, fromU2022State
;
214 #ifdef U_ENABLE_GENERIC_ISO_2022
217 UBool isEmptySegment
;
220 }UConverterDataISO2022
;
223 /* ISO-2022 ----------------------------------------------------------------- */
225 /*Forward declaration */
227 ucnv_fromUnicode_UTF8(UConverterFromUnicodeArgs
* args
,
230 ucnv_fromUnicode_UTF8_OFFSETS_LOGIC(UConverterFromUnicodeArgs
* args
,
233 #define ESC_2022 0x1B /*ESC*/
237 INVALID_2022
= -1, /*Doesn't correspond to a valid iso 2022 escape sequence*/
238 VALID_NON_TERMINAL_2022
= 0, /*so far corresponds to a valid iso 2022 escape sequence*/
239 VALID_TERMINAL_2022
= 1, /*corresponds to a valid iso 2022 escape sequence*/
240 VALID_MAYBE_TERMINAL_2022
= 2 /*so far matches one iso 2022 escape sequence, but by adding more characters might match another escape sequence*/
241 } UCNV_TableStates_2022
;
244 * The way these state transition arrays work is:
245 * ex : ESC$B is the sequence for JISX208
246 * a) First Iteration: char is ESC
247 * i) Get the value of ESC from normalize_esq_chars_2022[] with int value of ESC as index
248 * int x = normalize_esq_chars_2022[27] which is equal to 1
249 * ii) Search for this value in escSeqStateTable_Key_2022[]
250 * value of x is stored at escSeqStateTable_Key_2022[0]
251 * iii) Save this index as offset
252 * iv) Get state of this sequence from escSeqStateTable_Value_2022[]
253 * escSeqStateTable_Value_2022[offset], which is VALID_NON_TERMINAL_2022
254 * b) Switch on this state and continue to next char
255 * i) Get the value of $ from normalize_esq_chars_2022[] with int value of $ as index
256 * which is normalize_esq_chars_2022[36] == 4
257 * ii) x is currently 1(from above)
258 * x<<=5 -- x is now 32
259 * x+=normalize_esq_chars_2022[36]
261 * iii) Search for this value in escSeqStateTable_Key_2022[]
262 * value of x is stored at escSeqStateTable_Key_2022[2], so offset is 2
263 * iv) Get state of this sequence from escSeqStateTable_Value_2022[]
264 * escSeqStateTable_Value_2022[offset], which is VALID_NON_TERMINAL_2022
265 * c) Switch on this state and continue to next char
266 * i) Get the value of B from normalize_esq_chars_2022[] with int value of B as index
267 * ii) x is currently 36 (from above)
268 * x<<=5 -- x is now 1152
269 * x+=normalize_esq_chars_2022[66]
271 * iii) Search for this value in escSeqStateTable_Key_2022[]
272 * value of x is stored at escSeqStateTable_Key_2022[21], so offset is 21
273 * iv) Get state of this sequence from escSeqStateTable_Value_2022[21]
274 * escSeqStateTable_Value_2022[offset], which is VALID_TERMINAL_2022
275 * v) Get the converter name form escSeqStateTable_Result_2022[21] which is JISX208
279 /*Below are the 3 arrays depicting a state transition table*/
280 static const int8_t normalize_esq_chars_2022
[256] = {
281 /* 0 1 2 3 4 5 6 7 8 9 */
283 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
284 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
285 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,0 ,0
286 ,0 ,0 ,0 ,0 ,0 ,0 ,4 ,7 ,29 ,0
287 ,2 ,24 ,26 ,27 ,0 ,3 ,23 ,6 ,0 ,0
288 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
289 ,0 ,0 ,0 ,0 ,5 ,8 ,9 ,10 ,11 ,12
290 ,13 ,14 ,15 ,16 ,17 ,18 ,19 ,20 ,25 ,28
291 ,0 ,0 ,21 ,0 ,0 ,0 ,0 ,0 ,0 ,0
292 ,22 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
293 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
294 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
295 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
296 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
297 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
298 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
299 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
300 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
301 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
302 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
303 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
304 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
305 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
306 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
307 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
311 #ifdef U_ENABLE_GENERIC_ISO_2022
313 * When the generic ISO-2022 converter is completely removed, not just disabled
314 * per #ifdef, then the following state table and the associated tables that are
315 * dimensioned with MAX_STATES_2022 should be trimmed.
317 * Especially, VALID_MAYBE_TERMINAL_2022 will not be used any more, and all of
318 * the associated escape sequences starting with ESC ( B should be removed.
319 * This includes the ones with key values 1097 and all of the ones above 1000000.
321 * For the latter, the tables can simply be truncated.
322 * For the former, since the tables must be kept parallel, it is probably best
323 * to simply duplicate an adjacent table cell, parallel in all tables.
325 * It may make sense to restructure the tables, especially by using small search
326 * tables for the variants instead of indexing them parallel to the table here.
330 #define MAX_STATES_2022 74
331 static const int32_t escSeqStateTable_Key_2022
[MAX_STATES_2022
] = {
332 /* 0 1 2 3 4 5 6 7 8 9 */
334 1 ,34 ,36 ,39 ,55 ,57 ,60 ,61 ,1093 ,1096
335 ,1097 ,1098 ,1099 ,1100 ,1101 ,1102 ,1103 ,1104 ,1105 ,1106
336 ,1109 ,1154 ,1157 ,1160 ,1161 ,1176 ,1178 ,1179 ,1254 ,1257
337 ,1768 ,1773 ,1957 ,35105 ,36933 ,36936 ,36937 ,36938 ,36939 ,36940
338 ,36942 ,36943 ,36944 ,36945 ,36946 ,36947 ,36948 ,37640 ,37642 ,37644
339 ,37646 ,37711 ,37744 ,37745 ,37746 ,37747 ,37748 ,40133 ,40136 ,40138
340 ,40139 ,40140 ,40141 ,1123363 ,35947624 ,35947625 ,35947626 ,35947627 ,35947629 ,35947630
341 ,35947631 ,35947635 ,35947636 ,35947638
344 #ifdef U_ENABLE_GENERIC_ISO_2022
346 static const char* const escSeqStateTable_Result_2022
[MAX_STATES_2022
] = {
347 /* 0 1 2 3 4 5 6 7 8 9 */
349 NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,"latin1" ,"latin1"
350 ,"latin1" ,"ibm-865" ,"ibm-865" ,"ibm-865" ,"ibm-865" ,"ibm-865" ,"ibm-865" ,"JISX0201" ,"JISX0201" ,"latin1"
351 ,"latin1" ,NULL
,"JISX-208" ,"ibm-5478" ,"JISX-208" ,NULL
,NULL
,NULL
,NULL
,"UTF8"
352 ,"ISO-8859-1" ,"ISO-8859-7" ,"JIS-X-208" ,NULL
,"ibm-955" ,"ibm-367" ,"ibm-952" ,"ibm-949" ,"JISX-212" ,"ibm-1383"
353 ,"ibm-952" ,"ibm-964" ,"ibm-964" ,"ibm-964" ,"ibm-964" ,"ibm-964" ,"ibm-964" ,"ibm-5478" ,"ibm-949" ,"ISO-IR-165"
354 ,"CNS-11643-1992,1" ,"CNS-11643-1992,2" ,"CNS-11643-1992,3" ,"CNS-11643-1992,4" ,"CNS-11643-1992,5" ,"CNS-11643-1992,6" ,"CNS-11643-1992,7" ,"UTF16_PlatformEndian" ,"UTF16_PlatformEndian" ,"UTF16_PlatformEndian"
355 ,"UTF16_PlatformEndian" ,"UTF16_PlatformEndian" ,"UTF16_PlatformEndian" ,NULL
,"latin1" ,"ibm-912" ,"ibm-913" ,"ibm-914" ,"ibm-813" ,"ibm-1089"
356 ,"ibm-920" ,"ibm-915" ,"ibm-915" ,"latin1"
361 static const int8_t escSeqStateTable_Value_2022
[MAX_STATES_2022
] = {
362 /* 0 1 2 3 4 5 6 7 8 9 */
363 VALID_NON_TERMINAL_2022
,VALID_NON_TERMINAL_2022
,VALID_NON_TERMINAL_2022
,VALID_NON_TERMINAL_2022
,VALID_NON_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_NON_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
364 ,VALID_MAYBE_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
365 ,VALID_TERMINAL_2022
,VALID_NON_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_NON_TERMINAL_2022
,VALID_NON_TERMINAL_2022
,VALID_NON_TERMINAL_2022
,VALID_NON_TERMINAL_2022
,VALID_TERMINAL_2022
366 ,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_NON_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
367 ,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
368 ,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
369 ,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_NON_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
370 ,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
,VALID_TERMINAL_2022
373 /* Type def for refactoring changeState_2022 code*/
375 #ifdef U_ENABLE_GENERIC_ISO_2022
379 #if !UCONFIG_ONLY_HTML_CONVERSION
385 /*********** ISO 2022 Converter Protos ***********/
387 _ISO2022Open(UConverter
*cnv
, UConverterLoadArgs
*pArgs
, UErrorCode
*errorCode
);
390 _ISO2022Close(UConverter
*converter
);
393 _ISO2022Reset(UConverter
*converter
, UConverterResetChoice choice
);
396 _ISO2022getName(const UConverter
* cnv
);
399 _ISO_2022_WriteSub(UConverterFromUnicodeArgs
*args
, int32_t offsetIndex
, UErrorCode
*err
);
402 _ISO_2022_SafeClone(const UConverter
*cnv
, void *stackBuffer
, int32_t *pBufferSize
, UErrorCode
*status
);
404 #ifdef U_ENABLE_GENERIC_ISO_2022
406 T_UConverter_toUnicode_ISO_2022_OFFSETS_LOGIC(UConverterToUnicodeArgs
* args
, UErrorCode
* err
);
411 /*const UConverterSharedData _ISO2022Data;*/
412 extern const UConverterSharedData _ISO2022JPData
;
414 #if !UCONFIG_ONLY_HTML_CONVERSION
415 extern const UConverterSharedData _ISO2022KRData
;
416 extern const UConverterSharedData _ISO2022CNData
;
421 /*************** Converter implementations ******************/
423 /* The purpose of this function is to get around gcc compiler warnings. */
425 fromUWriteUInt8(UConverter
*cnv
,
426 const char *bytes
, int32_t length
,
427 uint8_t **target
, const char *targetLimit
,
430 UErrorCode
*pErrorCode
)
432 char *targetChars
= (char *)*target
;
433 ucnv_fromUWriteBytes(cnv
, bytes
, length
, &targetChars
, targetLimit
,
434 offsets
, sourceIndex
, pErrorCode
);
435 *target
= (uint8_t*)targetChars
;
440 setInitialStateToUnicodeKR(UConverter
* /*converter*/, UConverterDataISO2022
*myConverterData
){
441 if(myConverterData
->version
== 1) {
442 UConverter
*cnv
= myConverterData
->currentConverter
;
444 cnv
->toUnicodeStatus
=0; /* offset */
445 cnv
->mode
=0; /* state */
446 cnv
->toULength
=0; /* byteIndex */
451 setInitialStateFromUnicodeKR(UConverter
* converter
,UConverterDataISO2022
*myConverterData
){
452 /* in ISO-2022-KR the designator sequence appears only once
453 * in a file so we append it only once
455 if( converter
->charErrorBufferLength
==0){
457 converter
->charErrorBufferLength
= 4;
458 converter
->charErrorBuffer
[0] = 0x1b;
459 converter
->charErrorBuffer
[1] = 0x24;
460 converter
->charErrorBuffer
[2] = 0x29;
461 converter
->charErrorBuffer
[3] = 0x43;
463 if(myConverterData
->version
== 1) {
464 UConverter
*cnv
= myConverterData
->currentConverter
;
467 cnv
->fromUnicodeStatus
=1; /* prevLength */
472 _ISO2022Open(UConverter
*cnv
, UConverterLoadArgs
*pArgs
, UErrorCode
*errorCode
){
474 char myLocale
[6]={' ',' ',' ',' ',' ',' '};
476 cnv
->extraInfo
= uprv_malloc (sizeof (UConverterDataISO2022
));
477 if(cnv
->extraInfo
!= NULL
) {
478 UConverterNamePieces stackPieces
;
479 UConverterLoadArgs stackArgs
=UCNV_LOAD_ARGS_INITIALIZER
;
480 UConverterDataISO2022
*myConverterData
=(UConverterDataISO2022
*) cnv
->extraInfo
;
483 stackArgs
.onlyTestIsLoadable
= pArgs
->onlyTestIsLoadable
;
485 uprv_memset(myConverterData
, 0, sizeof(UConverterDataISO2022
));
486 myConverterData
->currentType
= ASCII1
;
487 cnv
->fromUnicodeStatus
=FALSE
;
489 uprv_strncpy(myLocale
, pArgs
->locale
, sizeof(myLocale
));
491 version
= pArgs
->options
& UCNV_OPTIONS_VERSION_MASK
;
492 myConverterData
->version
= version
;
493 if(myLocale
[0]=='j' && (myLocale
[1]=='a'|| myLocale
[1]=='p') &&
494 (myLocale
[2]=='_' || myLocale
[2]=='\0'))
496 /* open the required converters and cache them */
497 if(version
>MAX_JA_VERSION
) {
498 // ICU 55 fails to open a converter for an unsupported version.
499 // Previously, it fell back to version 0, but that would yield
500 // unexpected behavior.
501 *errorCode
= U_MISSING_RESOURCE_ERROR
;
504 if(jpCharsetMasks
[version
]&CSM(ISO8859_7
)) {
505 myConverterData
->myConverterArray
[ISO8859_7
] =
506 ucnv_loadSharedData("ISO8859_7", &stackPieces
, &stackArgs
, errorCode
);
508 myConverterData
->myConverterArray
[JISX208
] =
509 ucnv_loadSharedData("Shift-JIS", &stackPieces
, &stackArgs
, errorCode
);
510 if(jpCharsetMasks
[version
]&CSM(JISX212
)) {
511 myConverterData
->myConverterArray
[JISX212
] =
512 ucnv_loadSharedData("jisx-212", &stackPieces
, &stackArgs
, errorCode
);
514 if(jpCharsetMasks
[version
]&CSM(GB2312
)) {
515 myConverterData
->myConverterArray
[GB2312
] =
516 ucnv_loadSharedData("ibm-5478", &stackPieces
, &stackArgs
, errorCode
); /* gb_2312_80-1 */
518 if(jpCharsetMasks
[version
]&CSM(KSC5601
)) {
519 myConverterData
->myConverterArray
[KSC5601
] =
520 ucnv_loadSharedData("ksc_5601", &stackPieces
, &stackArgs
, errorCode
);
523 /* set the function pointers to appropriate funtions */
524 cnv
->sharedData
=(UConverterSharedData
*)(&_ISO2022JPData
);
525 uprv_strcpy(myConverterData
->locale
,"ja");
527 (void)uprv_strcpy(myConverterData
->name
,"ISO_2022,locale=ja,version=");
528 size_t len
= uprv_strlen(myConverterData
->name
);
529 myConverterData
->name
[len
]=(char)(myConverterData
->version
+(int)'0');
530 myConverterData
->name
[len
+1]='\0';
532 #if !UCONFIG_ONLY_HTML_CONVERSION
533 else if(myLocale
[0]=='k' && (myLocale
[1]=='o'|| myLocale
[1]=='r') &&
534 (myLocale
[2]=='_' || myLocale
[2]=='\0'))
537 // ICU 55 fails to open a converter for an unsupported version.
538 // Previously, it fell back to version 0, but that would yield
539 // unexpected behavior.
540 *errorCode
= U_MISSING_RESOURCE_ERROR
;
545 cnvName
="icu-internal-25546";
548 myConverterData
->version
=version
=0;
550 if(pArgs
->onlyTestIsLoadable
) {
551 ucnv_canCreateConverter(cnvName
, errorCode
); /* errorCode carries result */
552 uprv_free(cnv
->extraInfo
);
556 myConverterData
->currentConverter
=ucnv_open(cnvName
, errorCode
);
557 if (U_FAILURE(*errorCode
)) {
563 (void)uprv_strcpy(myConverterData
->name
,"ISO_2022,locale=ko,version=1");
564 uprv_memcpy(cnv
->subChars
, myConverterData
->currentConverter
->subChars
, 4);
565 cnv
->subCharLen
= myConverterData
->currentConverter
->subCharLen
;
567 (void)uprv_strcpy(myConverterData
->name
,"ISO_2022,locale=ko,version=0");
570 /* initialize the state variables */
571 setInitialStateToUnicodeKR(cnv
, myConverterData
);
572 setInitialStateFromUnicodeKR(cnv
, myConverterData
);
574 /* set the function pointers to appropriate funtions */
575 cnv
->sharedData
=(UConverterSharedData
*)&_ISO2022KRData
;
576 uprv_strcpy(myConverterData
->locale
,"ko");
579 else if(((myLocale
[0]=='z' && myLocale
[1]=='h') || (myLocale
[0]=='c'&& myLocale
[1]=='n'))&&
580 (myLocale
[2]=='_' || myLocale
[2]=='\0'))
583 // ICU 55 fails to open a converter for an unsupported version.
584 // Previously, it fell back to version 0, but that would yield
585 // unexpected behavior.
586 *errorCode
= U_MISSING_RESOURCE_ERROR
;
590 /* open the required converters and cache them */
591 myConverterData
->myConverterArray
[GB2312_1
] =
592 ucnv_loadSharedData("ibm-5478", &stackPieces
, &stackArgs
, errorCode
);
594 myConverterData
->myConverterArray
[ISO_IR_165
] =
595 ucnv_loadSharedData("iso-ir-165", &stackPieces
, &stackArgs
, errorCode
);
597 myConverterData
->myConverterArray
[CNS_11643
] =
598 ucnv_loadSharedData("cns-11643-1992", &stackPieces
, &stackArgs
, errorCode
);
601 /* set the function pointers to appropriate funtions */
602 cnv
->sharedData
=(UConverterSharedData
*)&_ISO2022CNData
;
603 uprv_strcpy(myConverterData
->locale
,"cn");
606 myConverterData
->version
= 0;
607 (void)uprv_strcpy(myConverterData
->name
,"ISO_2022,locale=zh,version=0");
608 }else if (version
==1){
609 myConverterData
->version
= 1;
610 (void)uprv_strcpy(myConverterData
->name
,"ISO_2022,locale=zh,version=1");
612 myConverterData
->version
= 2;
613 (void)uprv_strcpy(myConverterData
->name
,"ISO_2022,locale=zh,version=2");
616 #endif // !UCONFIG_ONLY_HTML_CONVERSION
618 #ifdef U_ENABLE_GENERIC_ISO_2022
619 myConverterData
->isFirstBuffer
= TRUE
;
621 /* append the UTF-8 escape sequence */
622 cnv
->charErrorBufferLength
= 3;
623 cnv
->charErrorBuffer
[0] = 0x1b;
624 cnv
->charErrorBuffer
[1] = 0x25;
625 cnv
->charErrorBuffer
[2] = 0x42;
627 cnv
->sharedData
=(UConverterSharedData
*)&_ISO2022Data
;
628 /* initialize the state variables */
629 uprv_strcpy(myConverterData
->name
,"ISO_2022");
631 *errorCode
= U_MISSING_RESOURCE_ERROR
;
632 // Was U_UNSUPPORTED_ERROR but changed in ICU 55 to a more standard
633 // data loading error code.
638 cnv
->maxBytesPerUChar
=cnv
->sharedData
->staticData
->maxBytesPerChar
;
640 if(U_FAILURE(*errorCode
) || pArgs
->onlyTestIsLoadable
) {
644 *errorCode
= U_MEMORY_ALLOCATION_ERROR
;
650 _ISO2022Close(UConverter
*converter
) {
651 UConverterDataISO2022
* myData
=(UConverterDataISO2022
*) (converter
->extraInfo
);
652 UConverterSharedData
**array
= myData
->myConverterArray
;
655 if (converter
->extraInfo
!= NULL
) {
656 /*close the array of converter pointers and free the memory*/
657 for (i
=0; i
<UCNV_2022_MAX_CONVERTERS
; i
++) {
659 ucnv_unloadSharedDataIfReady(array
[i
]);
663 ucnv_close(myData
->currentConverter
);
665 if(!converter
->isExtraLocal
){
666 uprv_free (converter
->extraInfo
);
667 converter
->extraInfo
= NULL
;
673 _ISO2022Reset(UConverter
*converter
, UConverterResetChoice choice
) {
674 UConverterDataISO2022
*myConverterData
=(UConverterDataISO2022
*) (converter
->extraInfo
);
675 if(choice
<=UCNV_RESET_TO_UNICODE
) {
676 uprv_memset(&myConverterData
->toU2022State
, 0, sizeof(ISO2022State
));
677 myConverterData
->key
= 0;
678 myConverterData
->isEmptySegment
= FALSE
;
680 if(choice
!=UCNV_RESET_TO_UNICODE
) {
681 uprv_memset(&myConverterData
->fromU2022State
, 0, sizeof(ISO2022State
));
683 #ifdef U_ENABLE_GENERIC_ISO_2022
684 if(myConverterData
->locale
[0] == 0){
685 if(choice
<=UCNV_RESET_TO_UNICODE
) {
686 myConverterData
->isFirstBuffer
= TRUE
;
687 myConverterData
->key
= 0;
688 if (converter
->mode
== UCNV_SO
){
689 ucnv_close (myConverterData
->currentConverter
);
690 myConverterData
->currentConverter
=NULL
;
692 converter
->mode
= UCNV_SI
;
694 if(choice
!=UCNV_RESET_TO_UNICODE
) {
695 /* re-append UTF-8 escape sequence */
696 converter
->charErrorBufferLength
= 3;
697 converter
->charErrorBuffer
[0] = 0x1b;
698 converter
->charErrorBuffer
[1] = 0x28;
699 converter
->charErrorBuffer
[2] = 0x42;
705 /* reset the state variables */
706 if(myConverterData
->locale
[0] == 'k'){
707 if(choice
<=UCNV_RESET_TO_UNICODE
) {
708 setInitialStateToUnicodeKR(converter
, myConverterData
);
710 if(choice
!=UCNV_RESET_TO_UNICODE
) {
711 setInitialStateFromUnicodeKR(converter
, myConverterData
);
718 _ISO2022getName(const UConverter
* cnv
){
720 UConverterDataISO2022
* myData
= (UConverterDataISO2022
*)cnv
->extraInfo
;
727 /*************** to unicode *******************/
728 /****************************************************************************
729 * Recognized escape sequences are
741 static const int8_t nextStateToUnicodeJP
[MAX_STATES_2022
]= {
742 /* 0 1 2 3 4 5 6 7 8 9 */
743 INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,SS2_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
744 ,ASCII
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,JISX201
,HWKANA_7BIT
,JISX201
,INVALID_STATE
745 ,INVALID_STATE
,INVALID_STATE
,JISX208
,GB2312
,JISX208
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
746 ,ISO8859_1
,ISO8859_7
,JISX208
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,KSC5601
,JISX212
,INVALID_STATE
747 ,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
748 ,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
749 ,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
750 ,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
753 #if !UCONFIG_ONLY_HTML_CONVERSION
754 /*************** to unicode *******************/
755 static const int8_t nextStateToUnicodeCN
[MAX_STATES_2022
]= {
756 /* 0 1 2 3 4 5 6 7 8 9 */
757 INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,SS2_STATE
,SS3_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
758 ,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
759 ,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
760 ,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
761 ,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,GB2312_1
,INVALID_STATE
,ISO_IR_165
762 ,CNS_11643_1
,CNS_11643_2
,CNS_11643_3
,CNS_11643_4
,CNS_11643_5
,CNS_11643_6
,CNS_11643_7
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
763 ,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
764 ,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
,INVALID_STATE
769 static UCNV_TableStates_2022
770 getKey_2022(char c
,int32_t* key
,int32_t* offset
){
773 int32_t hi
= MAX_STATES_2022
;
776 togo
= normalize_esq_chars_2022
[(uint8_t)c
];
778 /* not a valid character anywhere in an escape sequence */
783 togo
= (*key
<< 5) + togo
;
785 while (hi
!= low
) /*binary search*/{
787 int32_t mid
= (hi
+low
) >> 1; /*Finds median*/
792 if (escSeqStateTable_Key_2022
[mid
] > togo
){
795 else if (escSeqStateTable_Key_2022
[mid
] < togo
){
798 else /*we found it*/{
801 return (UCNV_TableStates_2022
)escSeqStateTable_Value_2022
[mid
];
812 /*runs through a state machine to determine the escape sequence - codepage correspondance
815 changeState_2022(UConverter
* _this
,
817 const char* sourceLimit
,
820 UCNV_TableStates_2022 value
;
821 UConverterDataISO2022
* myData2022
= ((UConverterDataISO2022
*)_this
->extraInfo
);
822 uint32_t key
= myData2022
->key
;
824 int8_t initialToULength
= _this
->toULength
;
827 value
= VALID_NON_TERMINAL_2022
;
828 while (*source
< sourceLimit
) {
830 _this
->toUBytes
[_this
->toULength
++]=(uint8_t)c
;
831 value
= getKey_2022(c
,(int32_t *) &key
, &offset
);
835 case VALID_NON_TERMINAL_2022
:
836 /* continue with the loop */
839 case VALID_TERMINAL_2022
:
846 case VALID_MAYBE_TERMINAL_2022
:
847 #ifdef U_ENABLE_GENERIC_ISO_2022
848 /* ESC ( B is ambiguous only for ISO_2022 itself */
849 if(var
== ISO_2022
) {
850 /* discard toUBytes[] for ESC ( B because this sequence is correct and complete */
851 _this
->toULength
= 0;
853 /* TODO need to indicate that ESC ( B was seen; if failure, then need to replay from source or from MBCS-style replay */
855 /* continue with the loop */
856 value
= VALID_NON_TERMINAL_2022
;
861 /* not ISO_2022 itself, finish here */
862 value
= VALID_TERMINAL_2022
;
870 myData2022
->key
= key
;
872 if (value
== VALID_NON_TERMINAL_2022
) {
873 /* indicate that the escape sequence is incomplete: key!=0 */
875 } else if (value
== INVALID_2022
) {
876 *err
= U_ILLEGAL_ESCAPE_SEQUENCE
;
877 } else /* value == VALID_TERMINAL_2022 */ {
879 #ifdef U_ENABLE_GENERIC_ISO_2022
882 const char *chosenConverterName
= escSeqStateTable_Result_2022
[offset
];
883 if(chosenConverterName
== NULL
) {
885 *err
= U_UNSUPPORTED_ESCAPE_SEQUENCE
;
886 _this
->toUCallbackReason
= UCNV_UNASSIGNED
;
890 _this
->mode
= UCNV_SI
;
891 ucnv_close(myData2022
->currentConverter
);
892 myData2022
->currentConverter
= myUConverter
= ucnv_open(chosenConverterName
, err
);
893 if(U_SUCCESS(*err
)) {
894 myUConverter
->fromCharErrorBehaviour
= UCNV_TO_U_CALLBACK_STOP
;
895 _this
->mode
= UCNV_SO
;
902 StateEnum tempState
=(StateEnum
)nextStateToUnicodeJP
[offset
];
905 *err
= U_UNSUPPORTED_ESCAPE_SEQUENCE
;
908 if(myData2022
->toU2022State
.cs
[2]!=0) {
909 if(myData2022
->toU2022State
.g
<2) {
910 myData2022
->toU2022State
.prevG
=myData2022
->toU2022State
.g
;
912 myData2022
->toU2022State
.g
=2;
914 /* illegal to have SS2 before a matching designator */
915 *err
= U_ILLEGAL_ESCAPE_SEQUENCE
;
918 /* case SS3_STATE: not used in ISO-2022-JP-x */
921 if((jpCharsetMasks
[myData2022
->version
] & CSM(tempState
)) == 0) {
922 *err
= U_UNSUPPORTED_ESCAPE_SEQUENCE
;
924 /* G2 charset for SS2 */
925 myData2022
->toU2022State
.cs
[2]=(int8_t)tempState
;
929 if((jpCharsetMasks
[myData2022
->version
] & CSM(tempState
)) == 0) {
930 *err
= U_UNSUPPORTED_ESCAPE_SEQUENCE
;
933 myData2022
->toU2022State
.cs
[0]=(int8_t)tempState
;
939 #if !UCONFIG_ONLY_HTML_CONVERSION
942 StateEnum tempState
=(StateEnum
)nextStateToUnicodeCN
[offset
];
945 *err
= U_UNSUPPORTED_ESCAPE_SEQUENCE
;
948 if(myData2022
->toU2022State
.cs
[2]!=0) {
949 if(myData2022
->toU2022State
.g
<2) {
950 myData2022
->toU2022State
.prevG
=myData2022
->toU2022State
.g
;
952 myData2022
->toU2022State
.g
=2;
954 /* illegal to have SS2 before a matching designator */
955 *err
= U_ILLEGAL_ESCAPE_SEQUENCE
;
959 if(myData2022
->toU2022State
.cs
[3]!=0) {
960 if(myData2022
->toU2022State
.g
<2) {
961 myData2022
->toU2022State
.prevG
=myData2022
->toU2022State
.g
;
963 myData2022
->toU2022State
.g
=3;
965 /* illegal to have SS3 before a matching designator */
966 *err
= U_ILLEGAL_ESCAPE_SEQUENCE
;
970 if(myData2022
->version
==0) {
971 *err
= U_UNSUPPORTED_ESCAPE_SEQUENCE
;
978 myData2022
->toU2022State
.cs
[1]=(int8_t)tempState
;
981 myData2022
->toU2022State
.cs
[2]=(int8_t)tempState
;
984 /* other CNS 11643 planes */
985 if(myData2022
->version
==0) {
986 *err
= U_UNSUPPORTED_ESCAPE_SEQUENCE
;
988 myData2022
->toU2022State
.cs
[3]=(int8_t)tempState
;
996 /* nothing to be done, just accept this one escape sequence */
998 *err
= U_UNSUPPORTED_ESCAPE_SEQUENCE
;
1001 #endif // !UCONFIG_ONLY_HTML_CONVERSION
1004 *err
= U_ILLEGAL_ESCAPE_SEQUENCE
;
1008 if(U_SUCCESS(*err
)) {
1009 _this
->toULength
= 0;
1010 } else if(*err
==U_ILLEGAL_ESCAPE_SEQUENCE
) {
1011 if(_this
->toULength
>1) {
1013 * Ticket 5691: consistent illegal sequences:
1014 * - We include at least the first byte (ESC) in the illegal sequence.
1015 * - If any of the non-initial bytes could be the start of a character,
1016 * we stop the illegal sequence before the first one of those.
1017 * In escape sequences, all following bytes are "printable", that is,
1018 * unless they are completely illegal (>7f in SBCS, outside 21..7e in DBCS),
1019 * they are valid single/lead bytes.
1020 * For simplicity, we always only report the initial ESC byte as the
1021 * illegal sequence and back out all other bytes we looked at.
1023 /* Back out some bytes. */
1024 int8_t backOutDistance
=_this
->toULength
-1;
1025 int8_t bytesFromThisBuffer
=_this
->toULength
-initialToULength
;
1026 if(backOutDistance
<=bytesFromThisBuffer
) {
1027 /* same as initialToULength<=1 */
1028 *source
-=backOutDistance
;
1030 /* Back out bytes from the previous buffer: Need to replay them. */
1031 _this
->preToULength
=(int8_t)(bytesFromThisBuffer
-backOutDistance
);
1032 /* same as -(initialToULength-1) */
1033 /* preToULength is negative! */
1034 uprv_memcpy(_this
->preToU
, _this
->toUBytes
+1, -_this
->preToULength
);
1035 *source
-=bytesFromThisBuffer
;
1039 } else if(*err
==U_UNSUPPORTED_ESCAPE_SEQUENCE
) {
1040 _this
->toUCallbackReason
= UCNV_UNASSIGNED
;
1044 #if !UCONFIG_ONLY_HTML_CONVERSION
1045 /*Checks the characters of the buffer against valid 2022 escape sequences
1046 *if the match we return a pointer to the initial start of the sequence otherwise
1047 *we return sourceLimit
1049 /*for 2022 looks ahead in the stream
1050 *to determine the longest possible convertible
1053 static inline const char*
1054 getEndOfBuffer_2022(const char** source
,
1055 const char* sourceLimit
,
1058 const char* mySource
= *source
;
1060 #ifdef U_ENABLE_GENERIC_ISO_2022
1061 if (*source
>= sourceLimit
)
1066 if (*mySource
== ESC_2022
){
1070 UCNV_TableStates_2022 value
= VALID_NON_TERMINAL_2022
;
1072 /* Kludge: I could not
1073 * figure out the reason for validating an escape sequence
1074 * twice - once here and once in changeState_2022().
1075 * is it possible to have an ESC character in a ISO2022
1076 * byte stream which is valid in a code page? Is it legal?
1079 (mySource
+i
< sourceLimit
)&&(value
== VALID_NON_TERMINAL_2022
);
1081 value
= getKey_2022(*(mySource
+i
), &key
, &offset
);
1083 if (value
> 0 || *mySource
==ESC_2022
)
1086 if ((value
== VALID_NON_TERMINAL_2022
)&&(!flush
) )
1089 }while (++mySource
< sourceLimit
);
1093 while(mySource
< sourceLimit
&& *mySource
!= ESC_2022
) {
1101 /* This inline function replicates code in _MBCSFromUChar32() function in ucnvmbcs.c
1102 * any future change in _MBCSFromUChar32() function should be reflected here.
1103 * @return number of bytes in *value; negative number if fallback; 0 if no mapping
1105 static inline int32_t
1106 MBCS_FROM_UCHAR32_ISO2022(UConverterSharedData
* sharedData
,
1113 const uint16_t *table
;
1114 uint32_t stage2Entry
;
1119 * TODO(markus): Use and require new, faster MBCS conversion table structures.
1120 * Use internal version of ucnv_open() that verifies that the new structures are available,
1121 * else U_INTERNAL_PROGRAM_ERROR.
1123 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
1124 if(c
<0x10000 || (sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
1125 table
=sharedData
->mbcs
.fromUnicodeTable
;
1126 stage2Entry
=MBCS_STAGE_2_FROM_U(table
, c
);
1127 /* get the bytes and the length for the output */
1128 if(outputType
==MBCS_OUTPUT_2
){
1129 myValue
=MBCS_VALUE_2_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
1135 } else /* outputType==MBCS_OUTPUT_3 */ {
1136 p
=MBCS_POINTER_3_FROM_STAGE_2(sharedData
->mbcs
.fromUnicodeBytes
, stage2Entry
, c
);
1137 myValue
=((uint32_t)*p
<<16)|((uint32_t)p
[1]<<8)|p
[2];
1140 } else if(myValue
<=0xffff) {
1146 /* is this code point assigned, or do we use fallbacks? */
1147 if((stage2Entry
&(1<<(16+(c
&0xf))))!=0) {
1151 } else if(FROM_U_USE_FALLBACK(useFallback
, c
) && myValue
!=0) {
1153 * We allow a 0 byte output if the "assigned" bit is set for this entry.
1154 * There is no way with this data structure for fallback output
1155 * to be a zero byte.
1162 cx
=sharedData
->mbcs
.extIndexes
;
1164 return ucnv_extSimpleMatchFromU(cx
, c
, value
, useFallback
);
1171 /* This inline function replicates code in _MBCSSingleFromUChar32() function in ucnvmbcs.c
1172 * any future change in _MBCSSingleFromUChar32() function should be reflected here.
1173 * @param retval pointer to output byte
1174 * @return 1 roundtrip byte 0 no mapping -1 fallback byte
1176 static inline int32_t
1177 MBCS_SINGLE_FROM_UCHAR32(UConverterSharedData
* sharedData
,
1182 const uint16_t *table
;
1184 /* BMP-only codepages are stored without stage 1 entries for supplementary code points */
1185 if(c
>=0x10000 && !(sharedData
->mbcs
.unicodeMask
&UCNV_HAS_SUPPLEMENTARY
)) {
1188 /* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
1189 table
=sharedData
->mbcs
.fromUnicodeTable
;
1190 /* get the byte for the output */
1191 value
=MBCS_SINGLE_RESULT_FROM_U(table
, (uint16_t *)sharedData
->mbcs
.fromUnicodeBytes
, c
);
1192 /* is this code point assigned, or do we use fallbacks? */
1193 *retval
=(uint32_t)(value
&0xff);
1195 return 1; /* roundtrip */
1196 } else if(useFallback
? value
>=0x800 : value
>=0xc00) {
1197 return -1; /* fallback taken */
1199 return 0; /* no mapping */
1204 * Check that the result is a 2-byte value with each byte in the range A1..FE
1205 * (strict EUC DBCS) before accepting it and subtracting 0x80 from each byte
1206 * to move it to the ISO 2022 range 21..7E.
1207 * Return 0 if out of range.
1209 static inline uint32_t
1210 _2022FromGR94DBCS(uint32_t value
) {
1211 if( (uint16_t)(value
- 0xa1a1) <= (0xfefe - 0xa1a1) &&
1212 (uint8_t)(value
- 0xa1) <= (0xfe - 0xa1)
1214 return value
- 0x8080; /* shift down to 21..7e byte range */
1216 return 0; /* not valid for ISO 2022 */
1220 #if 0 /* 5691: Call sites now check for validity. They can just += 0x8080 after that. */
1222 * This method does the reverse of _2022FromGR94DBCS(). Given the 2022 code point, it returns the
1223 * 2 byte value that is in the range A1..FE for each byte. Otherwise it returns the 2022 code point
1226 static inline uint32_t
1227 _2022ToGR94DBCS(uint32_t value
) {
1228 uint32_t returnValue
= value
+ 0x8080;
1229 if( (uint16_t)(returnValue
- 0xa1a1) <= (0xfefe - 0xa1a1) &&
1230 (uint8_t)(returnValue
- 0xa1) <= (0xfe - 0xa1)) {
1238 #ifdef U_ENABLE_GENERIC_ISO_2022
1240 /**********************************************************************************
1241 * ISO-2022 Converter
1247 T_UConverter_toUnicode_ISO_2022_OFFSETS_LOGIC(UConverterToUnicodeArgs
* args
,
1249 const char* mySourceLimit
, *realSourceLimit
;
1250 const char* sourceStart
;
1251 const UChar
* myTargetStart
;
1252 UConverter
* saveThis
;
1253 UConverterDataISO2022
* myData
;
1256 saveThis
= args
->converter
;
1257 myData
=((UConverterDataISO2022
*)(saveThis
->extraInfo
));
1259 realSourceLimit
= args
->sourceLimit
;
1260 while (args
->source
< realSourceLimit
) {
1261 if(myData
->key
== 0) { /* are we in the middle of an escape sequence? */
1262 /*Find the end of the buffer e.g : Next Escape Seq | end of Buffer*/
1263 mySourceLimit
= getEndOfBuffer_2022(&(args
->source
), realSourceLimit
, args
->flush
);
1265 if(args
->source
< mySourceLimit
) {
1266 if(myData
->currentConverter
==NULL
) {
1267 myData
->currentConverter
= ucnv_open("ASCII",err
);
1268 if(U_FAILURE(*err
)){
1272 myData
->currentConverter
->fromCharErrorBehaviour
= UCNV_TO_U_CALLBACK_STOP
;
1273 saveThis
->mode
= UCNV_SO
;
1276 /* convert to before the ESC or until the end of the buffer */
1277 myData
->isFirstBuffer
=FALSE
;
1278 sourceStart
= args
->source
;
1279 myTargetStart
= args
->target
;
1280 args
->converter
= myData
->currentConverter
;
1281 ucnv_toUnicode(args
->converter
,
1287 (UBool
)(args
->flush
&& mySourceLimit
== realSourceLimit
),
1289 args
->converter
= saveThis
;
1291 if (*err
== U_BUFFER_OVERFLOW_ERROR
) {
1292 /* move the overflow buffer */
1293 length
= saveThis
->UCharErrorBufferLength
= myData
->currentConverter
->UCharErrorBufferLength
;
1294 myData
->currentConverter
->UCharErrorBufferLength
= 0;
1296 uprv_memcpy(saveThis
->UCharErrorBuffer
,
1297 myData
->currentConverter
->UCharErrorBuffer
,
1298 length
*U_SIZEOF_UCHAR
);
1305 * -Error while converting
1306 * -Done with entire buffer
1307 * -Need to write offsets or update the current offset
1308 * (leave that up to the code in ucnv.c)
1310 * or else we just stopped at an ESC byte and continue with changeState_2022()
1312 if (U_FAILURE(*err
) ||
1313 (args
->source
== realSourceLimit
) ||
1314 (args
->offsets
!= NULL
&& (args
->target
!= myTargetStart
|| args
->source
!= sourceStart
) ||
1315 (mySourceLimit
< realSourceLimit
&& myData
->currentConverter
->toULength
> 0))
1317 /* copy partial or error input for truncated detection and error handling */
1318 if(U_FAILURE(*err
)) {
1319 length
= saveThis
->invalidCharLength
= myData
->currentConverter
->invalidCharLength
;
1321 uprv_memcpy(saveThis
->invalidCharBuffer
, myData
->currentConverter
->invalidCharBuffer
, length
);
1324 length
= saveThis
->toULength
= myData
->currentConverter
->toULength
;
1326 uprv_memcpy(saveThis
->toUBytes
, myData
->currentConverter
->toUBytes
, length
);
1327 if(args
->source
< mySourceLimit
) {
1328 *err
= U_TRUNCATED_CHAR_FOUND
; /* truncated input before ESC */
1337 sourceStart
= args
->source
;
1338 changeState_2022(args
->converter
,
1343 if (U_FAILURE(*err
) || (args
->source
!= sourceStart
&& args
->offsets
!= NULL
)) {
1344 /* let the ucnv.c code update its current offset */
1353 * To Unicode Callback helper function
1356 toUnicodeCallback(UConverter
*cnv
,
1357 const uint32_t sourceChar
, const uint32_t targetUniChar
,
1359 if(sourceChar
>0xff){
1360 cnv
->toUBytes
[0] = (uint8_t)(sourceChar
>>8);
1361 cnv
->toUBytes
[1] = (uint8_t)sourceChar
;
1365 cnv
->toUBytes
[0] =(char) sourceChar
;
1369 if(targetUniChar
== (missingCharMarker
-1/*0xfffe*/)){
1370 *err
= U_INVALID_CHAR_FOUND
;
1373 *err
= U_ILLEGAL_CHAR_FOUND
;
1377 /**************************************ISO-2022-JP*************************************************/
1379 /************************************** IMPORTANT **************************************************
1380 * The UConverter_fromUnicode_ISO2022_JP converter does not use ucnv_fromUnicode() functions for SBCS,DBCS and
1381 * MBCS; instead, the values are obtained directly by calling _MBCSFromUChar32().
1382 * The converter iterates over each Unicode codepoint
1383 * to obtain the equivalent codepoints from the codepages supported. Since the source buffer is
1384 * processed one char at a time it would make sense to reduce the extra processing a canned converter
1385 * would do as far as possible.
1387 * If the implementation of these macros or structure of sharedData struct change in the future, make
1388 * sure that ISO-2022 is also changed.
1389 ***************************************************************************************************
1392 /***************************************************************************************************
1393 * Rules for ISO-2022-jp encoding
1394 * (i) Escape sequences must be fully contained within a line they should not
1395 * span new lines or CRs
1396 * (ii) If the last character on a line is represented by two bytes then an ASCII or
1397 * JIS-Roman character escape sequence should follow before the line terminates
1398 * (iii) If the first character on the line is represented by two bytes then a two
1399 * byte character escape sequence should precede it
1400 * (iv) If no escape sequence is encountered then the characters are ASCII
1401 * (v) Latin(ISO-8859-1) and Greek(ISO-8859-7) characters must be designated to G2,
1402 * and invoked with SS2 (ESC N).
1403 * (vi) If there is any G0 designation in text, there must be a switch to
1404 * ASCII or to JIS X 0201-Roman before a space character (but not
1405 * necessarily before "ESC 4/14 2/0" or "ESC N ' '") or control
1406 * characters such as tab or CRLF.
1407 * (vi) Supported encodings:
1408 * ASCII, JISX201, JISX208, JISX212, GB2312, KSC5601, ISO-8859-1,ISO-8859-7
1412 * JISX201, JISX208,JISX212 : new .cnv data files created
1413 * KSC5601 : alias to ibm-949 mapping table
1414 * GB2312 : alias to ibm-1386 mapping table
1415 * ISO-8859-1 : Algorithmic implemented as LATIN1 case
1416 * ISO-8859-7 : alisas to ibm-9409 mapping table
1419 /* preference order of JP charsets */
1420 static const StateEnum jpCharsetPref
[]={
1433 * The escape sequences must be in order of the enum constants like JISX201 = 3,
1434 * not in order of jpCharsetPref[]!
1436 static const char escSeqChars
[][6] ={
1437 "\x1B\x28\x42", /* <ESC>(B ASCII */
1438 "\x1B\x2E\x41", /* <ESC>.A ISO-8859-1 */
1439 "\x1B\x2E\x46", /* <ESC>.F ISO-8859-7 */
1440 "\x1B\x28\x4A", /* <ESC>(J JISX-201 */
1441 "\x1B\x24\x42", /* <ESC>$B JISX-208 */
1442 "\x1B\x24\x28\x44", /* <ESC>$(D JISX-212 */
1443 "\x1B\x24\x41", /* <ESC>$A GB2312 */
1444 "\x1B\x24\x28\x43", /* <ESC>$(C KSC5601 */
1445 "\x1B\x28\x49" /* <ESC>(I HWKANA_7BIT */
1448 static const int8_t escSeqCharsLen
[] ={
1449 3, /* length of <ESC>(B ASCII */
1450 3, /* length of <ESC>.A ISO-8859-1 */
1451 3, /* length of <ESC>.F ISO-8859-7 */
1452 3, /* length of <ESC>(J JISX-201 */
1453 3, /* length of <ESC>$B JISX-208 */
1454 4, /* length of <ESC>$(D JISX-212 */
1455 3, /* length of <ESC>$A GB2312 */
1456 4, /* length of <ESC>$(C KSC5601 */
1457 3 /* length of <ESC>(I HWKANA_7BIT */
1461 * The iteration over various code pages works this way:
1462 * i) Get the currentState from myConverterData->currentState
1463 * ii) Check if the character is mapped to a valid character in the currentState
1464 * Yes -> a) set the initIterState to currentState
1465 * b) remain in this state until an invalid character is found
1466 * No -> a) go to the next code page and find the character
1467 * iii) Before changing the state increment the current state check if the current state
1468 * is equal to the intitIteration state
1469 * Yes -> A character that cannot be represented in any of the supported encodings
1470 * break and return a U_INVALID_CHARACTER error
1471 * No -> Continue and find the character in next code page
1474 * TODO: Implement a priority technique where the users are allowed to set the priority of code pages
1477 /* Map 00..7F to Unicode according to JIS X 0201. */
1478 static inline uint32_t
1479 jisx201ToU(uint32_t value
) {
1482 } else if(value
== 0x5c) {
1484 } else if(value
== 0x7e) {
1486 } else /* value <= 0x7f */ {
1491 /* Map Unicode to 00..7F according to JIS X 0201. Return U+FFFE if unmappable. */
1492 static inline uint32_t
1493 jisx201FromU(uint32_t value
) {
1495 if(value
!=0x5c && value
!=0x7e) {
1498 } else if(value
==0xa5) {
1500 } else if(value
==0x203e) {
1507 * Take a valid Shift-JIS byte pair, check that it is in the range corresponding
1508 * to JIS X 0208, and convert it to a pair of 21..7E bytes.
1509 * Return 0 if the byte pair is out of range.
1511 static inline uint32_t
1512 _2022FromSJIS(uint32_t value
) {
1515 if(value
> 0xEFFC) {
1516 return 0; /* beyond JIS X 0208 */
1519 trail
= (uint8_t)value
;
1521 value
&= 0xff00; /* lead byte */
1522 if(value
<= 0x9f00) {
1524 } else /* 0xe000 <= value <= 0xef00 */ {
1532 value
|= trail
- 0x1f;
1534 value
|= trail
- 0x20;
1536 } else /* trail <= 0xfc */ {
1537 value
|= trail
- 0x7e;
1543 * Convert a pair of JIS X 0208 21..7E bytes to Shift-JIS.
1544 * If either byte is outside 21..7E make sure that the result is not valid
1545 * for Shift-JIS so that the converter catches it.
1546 * Some invalid byte values already turn into equally invalid Shift-JIS
1547 * byte values and need not be tested explicitly.
1550 _2022ToSJIS(uint8_t c1
, uint8_t c2
, char bytes
[2]) {
1555 } else if(c2
<= 0x7e) {
1558 c2
= 0; /* invalid */
1561 if((uint8_t)(c2
-0x21) <= ((0x7e)-0x21)) {
1564 c2
= 0; /* invalid */
1570 } else if(c1
<= 0x3f) {
1573 c1
= 0; /* invalid */
1575 bytes
[0] = (char)c1
;
1576 bytes
[1] = (char)c2
;
1580 * JIS X 0208 has fallbacks from Unicode half-width Katakana to full-width (DBCS)
1582 * Now that we use a Shift-JIS table for JIS X 0208 we need to hardcode these fallbacks
1583 * because Shift-JIS roundtrips half-width Katakana to single bytes.
1584 * These were the only fallbacks in ICU's jisx-208.ucm file.
1586 static const uint16_t hwkana_fb
[HWKANA_END
- HWKANA_START
+ 1] = {
1587 0x2123, /* U+FF61 */
1602 0x213C, /* U+FF70 */
1618 0x253F, /* U+FF80 */
1634 0x255F, /* U+FF90 */
1653 UConverter_fromUnicode_ISO_2022_JP_OFFSETS_LOGIC(UConverterFromUnicodeArgs
* args
, UErrorCode
* err
) {
1654 UConverter
*cnv
= args
->converter
;
1655 UConverterDataISO2022
*converterData
;
1656 ISO2022State
*pFromU2022State
;
1657 uint8_t *target
= (uint8_t *) args
->target
;
1658 const uint8_t *targetLimit
= (const uint8_t *) args
->targetLimit
;
1659 const UChar
* source
= args
->source
;
1660 const UChar
* sourceLimit
= args
->sourceLimit
;
1661 int32_t* offsets
= args
->offsets
;
1664 int32_t len
, outLen
;
1666 int32_t choiceCount
;
1667 uint32_t targetValue
= 0;
1673 /* set up the state */
1674 converterData
= (UConverterDataISO2022
*)cnv
->extraInfo
;
1675 pFromU2022State
= &converterData
->fromU2022State
;
1679 /* check if the last codepoint of previous buffer was a lead surrogate*/
1680 if((sourceChar
= cnv
->fromUChar32
)!=0 && target
< targetLimit
) {
1684 while(source
< sourceLimit
) {
1685 if(target
< targetLimit
) {
1687 sourceChar
= *(source
++);
1688 /*check if the char is a First surrogate*/
1689 if(U16_IS_SURROGATE(sourceChar
)) {
1690 if(U16_IS_SURROGATE_LEAD(sourceChar
)) {
1692 /*look ahead to find the trail surrogate*/
1693 if(source
< sourceLimit
) {
1694 /* test the following code unit */
1695 UChar trail
=(UChar
) *source
;
1696 if(U16_IS_TRAIL(trail
)) {
1698 sourceChar
=U16_GET_SUPPLEMENTARY(sourceChar
, trail
);
1699 cnv
->fromUChar32
=0x00;
1700 /* convert this supplementary code point */
1701 /* exit this condition tree */
1703 /* this is an unmatched lead code unit (1st surrogate) */
1704 /* callback(illegal) */
1705 *err
=U_ILLEGAL_CHAR_FOUND
;
1706 cnv
->fromUChar32
=sourceChar
;
1711 cnv
->fromUChar32
=sourceChar
;
1715 /* this is an unmatched trail code unit (2nd surrogate) */
1716 /* callback(illegal) */
1717 *err
=U_ILLEGAL_CHAR_FOUND
;
1718 cnv
->fromUChar32
=sourceChar
;
1723 /* do not convert SO/SI/ESC */
1724 if(IS_2022_CONTROL(sourceChar
)) {
1725 /* callback(illegal) */
1726 *err
=U_ILLEGAL_CHAR_FOUND
;
1727 cnv
->fromUChar32
=sourceChar
;
1731 /* do the conversion */
1733 if(choiceCount
== 0) {
1737 * The csm variable keeps track of which charsets are allowed
1738 * and not used yet while building the choices[].
1740 csm
= jpCharsetMasks
[converterData
->version
];
1743 /* JIS7/8: try single-byte half-width Katakana before JISX208 */
1744 if(converterData
->version
== 3 || converterData
->version
== 4) {
1745 choices
[choiceCount
++] = (int8_t)HWKANA_7BIT
;
1747 /* Do not try single-byte half-width Katakana for other versions. */
1748 csm
&= ~CSM(HWKANA_7BIT
);
1750 /* try the current G0 charset */
1751 choices
[choiceCount
++] = cs
= pFromU2022State
->cs
[0];
1754 /* try the current G2 charset */
1755 if((cs
= pFromU2022State
->cs
[2]) != 0) {
1756 choices
[choiceCount
++] = cs
;
1760 /* try all the other possible charsets */
1761 for(i
= 0; i
< UPRV_LENGTHOF(jpCharsetPref
); ++i
) {
1762 cs
= (int8_t)jpCharsetPref
[i
];
1764 choices
[choiceCount
++] = cs
;
1772 * len==0: no mapping found yet
1773 * len<0: found a fallback result: continue looking for a roundtrip but no further fallbacks
1774 * len>0: found a roundtrip result, done
1778 * We will turn off useFallback after finding a fallback,
1779 * but we still get fallbacks from PUA code points as usual.
1780 * Therefore, we will also need to check that we don't overwrite
1781 * an early fallback with a later one.
1783 useFallback
= cnv
->useFallback
;
1785 for(i
= 0; i
< choiceCount
&& len
<= 0; ++i
) {
1788 int8_t cs0
= choices
[i
];
1791 if(sourceChar
<= 0x7f) {
1792 targetValue
= (uint32_t)sourceChar
;
1799 if(GR96_START
<= sourceChar
&& sourceChar
<= GR96_END
) {
1800 targetValue
= (uint32_t)sourceChar
- 0x80;
1807 if((uint32_t)(sourceChar
- HWKANA_START
) <= (HWKANA_END
- HWKANA_START
)) {
1808 if(converterData
->version
==3) {
1809 /* JIS7: use G1 (SO) */
1810 /* Shift U+FF61..U+FF9F to bytes 21..5F. */
1811 targetValue
= (uint32_t)(sourceChar
- (HWKANA_START
- 0x21));
1813 pFromU2022State
->cs
[1] = cs
= cs0
; /* do not output an escape sequence */
1815 } else if(converterData
->version
==4) {
1816 /* JIS8: use 8-bit bytes with any single-byte charset, see escape sequence output below */
1817 /* Shift U+FF61..U+FF9F to bytes A1..DF. */
1818 targetValue
= (uint32_t)(sourceChar
- (HWKANA_START
- 0xa1));
1821 cs
= pFromU2022State
->cs
[0];
1822 if(IS_JP_DBCS(cs
)) {
1823 /* switch from a DBCS charset to JISX201 */
1824 cs
= (int8_t)JISX201
;
1826 /* else stay in the current G0 charset */
1829 /* else do not use HWKANA_7BIT with other versions */
1834 value
= jisx201FromU(sourceChar
);
1836 targetValue
= value
;
1840 useFallback
= FALSE
;
1844 /* G0 DBCS from Shift-JIS table */
1845 len2
= MBCS_FROM_UCHAR32_ISO2022(
1846 converterData
->myConverterArray
[cs0
],
1848 useFallback
, MBCS_OUTPUT_2
);
1849 if(len2
== 2 || (len2
== -2 && len
== 0)) { /* only accept DBCS: abs(len)==2 */
1850 value
= _2022FromSJIS(value
);
1852 targetValue
= value
;
1856 useFallback
= FALSE
;
1858 } else if(len
== 0 && useFallback
&&
1859 (uint32_t)(sourceChar
- HWKANA_START
) <= (HWKANA_END
- HWKANA_START
)) {
1860 targetValue
= hwkana_fb
[sourceChar
- HWKANA_START
];
1864 useFallback
= FALSE
;
1868 /* G0 SBCS forced to 7-bit output */
1869 len2
= MBCS_SINGLE_FROM_UCHAR32(
1870 converterData
->myConverterArray
[cs0
],
1873 if(len2
!= 0 && !(len2
< 0 && len
!= 0) && GR96_START
<= value
&& value
<= GR96_END
) {
1874 targetValue
= value
- 0x80;
1878 useFallback
= FALSE
;
1883 len2
= MBCS_FROM_UCHAR32_ISO2022(
1884 converterData
->myConverterArray
[cs0
],
1886 useFallback
, MBCS_OUTPUT_2
);
1887 if(len2
== 2 || (len2
== -2 && len
== 0)) { /* only accept DBCS: abs(len)==2 */
1888 if(cs0
== KSC5601
) {
1890 * Check for valid bytes for the encoding scheme.
1891 * This is necessary because the sub-converter (windows-949)
1892 * has a broader encoding scheme than is valid for 2022.
1894 value
= _2022FromGR94DBCS(value
);
1899 targetValue
= value
;
1903 useFallback
= FALSE
;
1911 len
= -len
; /* fallback */
1913 outLen
= 0; /* count output bytes */
1915 /* write SI if necessary (only for JIS7) */
1916 if(pFromU2022State
->g
== 1 && g
== 0) {
1917 buffer
[outLen
++] = UCNV_SI
;
1918 pFromU2022State
->g
= 0;
1921 /* write the designation sequence if necessary */
1922 if(cs
!= pFromU2022State
->cs
[g
]) {
1923 int32_t escLen
= escSeqCharsLen
[cs
];
1924 uprv_memcpy(buffer
+ outLen
, escSeqChars
[cs
], escLen
);
1926 pFromU2022State
->cs
[g
] = cs
;
1928 /* invalidate the choices[] */
1932 /* write the shift sequence if necessary */
1933 if(g
!= pFromU2022State
->g
) {
1935 /* case 0 handled before writing escapes */
1937 buffer
[outLen
++] = UCNV_SO
;
1938 pFromU2022State
->g
= 1;
1940 default: /* case 2 */
1941 buffer
[outLen
++] = 0x1b;
1942 buffer
[outLen
++] = 0x4e;
1944 /* no case 3: no SS3 in ISO-2022-JP-x */
1948 /* write the output bytes */
1950 buffer
[outLen
++] = (char)targetValue
;
1951 } else /* len == 2 */ {
1952 buffer
[outLen
++] = (char)(targetValue
>> 8);
1953 buffer
[outLen
++] = (char)targetValue
;
1957 * if we cannot find the character after checking all codepages
1958 * then this is an error
1960 *err
= U_INVALID_CHAR_FOUND
;
1961 cnv
->fromUChar32
=sourceChar
;
1965 if(sourceChar
== CR
|| sourceChar
== LF
) {
1966 /* reset the G2 state at the end of a line (conversion got us into ASCII or JISX201 already) */
1967 pFromU2022State
->cs
[2] = 0;
1971 /* output outLen>0 bytes in buffer[] */
1973 *target
++ = buffer
[0];
1975 *offsets
++ = (int32_t)(source
- args
->source
- 1); /* -1: known to be ASCII */
1977 } else if(outLen
== 2 && (target
+ 2) <= targetLimit
) {
1978 *target
++ = buffer
[0];
1979 *target
++ = buffer
[1];
1981 int32_t sourceIndex
= (int32_t)(source
- args
->source
- U16_LENGTH(sourceChar
));
1982 *offsets
++ = sourceIndex
;
1983 *offsets
++ = sourceIndex
;
1989 &target
, (const char *)targetLimit
,
1990 &offsets
, (int32_t)(source
- args
->source
- U16_LENGTH(sourceChar
)),
1992 if(U_FAILURE(*err
)) {
1996 } /* end if(myTargetIndex<myTargetLength) */
1998 *err
=U_BUFFER_OVERFLOW_ERROR
;
2002 }/* end while(mySourceIndex<mySourceLength) */
2005 * the end of the input stream and detection of truncated input
2006 * are handled by the framework, but for ISO-2022-JP conversion
2007 * we need to be in ASCII mode at the very end
2011 * in SO mode or not in ASCII mode
2012 * end of input and no truncated input
2014 if( U_SUCCESS(*err
) &&
2015 (pFromU2022State
->g
!=0 || pFromU2022State
->cs
[0]!=ASCII
) &&
2016 args
->flush
&& source
>=sourceLimit
&& cnv
->fromUChar32
==0
2018 int32_t sourceIndex
;
2022 if(pFromU2022State
->g
!= 0) {
2023 buffer
[outLen
++] = UCNV_SI
;
2024 pFromU2022State
->g
= 0;
2027 if(pFromU2022State
->cs
[0] != ASCII
) {
2028 int32_t escLen
= escSeqCharsLen
[ASCII
];
2029 uprv_memcpy(buffer
+ outLen
, escSeqChars
[ASCII
], escLen
);
2031 pFromU2022State
->cs
[0] = (int8_t)ASCII
;
2034 /* get the source index of the last input character */
2036 * TODO this would be simpler and more reliable if we used a pair
2037 * of sourceIndex/prevSourceIndex like in ucnvmbcs.c
2038 * so that we could simply use the prevSourceIndex here;
2039 * this code gives an incorrect result for the rare case of an unmatched
2040 * trail surrogate that is alone in the last buffer of the text stream
2042 sourceIndex
=(int32_t)(source
-args
->source
);
2045 if( U16_IS_TRAIL(args
->source
[sourceIndex
]) &&
2046 (sourceIndex
==0 || U16_IS_LEAD(args
->source
[sourceIndex
-1]))
2057 &target
, (const char *)targetLimit
,
2058 &offsets
, sourceIndex
,
2062 /*save the state and return */
2063 args
->source
= source
;
2064 args
->target
= (char*)target
;
2067 /*************** to unicode *******************/
2070 UConverter_toUnicode_ISO_2022_JP_OFFSETS_LOGIC(UConverterToUnicodeArgs
*args
,
2073 const char *mySource
= (char *) args
->source
;
2074 UChar
*myTarget
= args
->target
;
2075 const char *mySourceLimit
= args
->sourceLimit
;
2076 uint32_t targetUniChar
= 0x0000;
2077 uint32_t mySourceChar
= 0x0000;
2078 uint32_t tmpSourceChar
= 0x0000;
2079 UConverterDataISO2022
* myData
;
2080 ISO2022State
*pToU2022State
;
2083 myData
=(UConverterDataISO2022
*)(args
->converter
->extraInfo
);
2084 pToU2022State
= &myData
->toU2022State
;
2086 if(myData
->key
!= 0) {
2087 /* continue with a partial escape sequence */
2089 } else if(args
->converter
->toULength
== 1 && mySource
< mySourceLimit
&& myTarget
< args
->targetLimit
) {
2090 /* continue with a partial double-byte character */
2091 mySourceChar
= args
->converter
->toUBytes
[0];
2092 args
->converter
->toULength
= 0;
2093 cs
= (StateEnum
)pToU2022State
->cs
[pToU2022State
->g
];
2094 targetUniChar
= missingCharMarker
;
2098 while(mySource
< mySourceLimit
){
2100 targetUniChar
=missingCharMarker
;
2102 if(myTarget
< args
->targetLimit
){
2104 mySourceChar
= (unsigned char) *mySource
++;
2106 switch(mySourceChar
) {
2108 if(myData
->version
==3) {
2112 /* only JIS7 uses SI/SO, not ISO-2022-JP-x */
2113 myData
->isEmptySegment
= FALSE
; /* reset this, we have a different error */
2118 if(myData
->version
==3) {
2119 /* JIS7: switch to G1 half-width Katakana */
2120 pToU2022State
->cs
[1] = (int8_t)HWKANA_7BIT
;
2124 /* only JIS7 uses SI/SO, not ISO-2022-JP-x */
2125 myData
->isEmptySegment
= FALSE
; /* reset this, we have a different error */
2133 const char * mySourceBefore
= mySource
;
2134 int8_t toULengthBefore
= args
->converter
->toULength
;
2136 changeState_2022(args
->converter
,&(mySource
),
2137 mySourceLimit
, ISO_2022_JP
,err
);
2139 /* If in ISO-2022-JP only and we successully completed an escape sequence, but previous segment was empty, create an error */
2140 if(myData
->version
==0 && myData
->key
==0 && U_SUCCESS(*err
) && myData
->isEmptySegment
) {
2141 *err
= U_ILLEGAL_ESCAPE_SEQUENCE
;
2142 args
->converter
->toUCallbackReason
= UCNV_IRREGULAR
;
2143 args
->converter
->toULength
= (int8_t)(toULengthBefore
+ (mySource
- mySourceBefore
));
2147 /* invalid or illegal escape sequence */
2148 if(U_FAILURE(*err
)){
2149 args
->target
= myTarget
;
2150 args
->source
= mySource
;
2151 myData
->isEmptySegment
= FALSE
; /* Reset to avoid future spurious errors */
2154 /* If we successfully completed an escape sequence, we begin a new segment, empty so far */
2155 if(myData
->key
==0) {
2156 myData
->isEmptySegment
= TRUE
;
2160 /* ISO-2022-JP does not use single-byte (C1) SS2 and SS3 */
2165 /* automatically reset to single-byte mode */
2166 if((StateEnum
)pToU2022State
->cs
[0] != ASCII
&& (StateEnum
)pToU2022State
->cs
[0] != JISX201
) {
2167 pToU2022State
->cs
[0] = (int8_t)ASCII
;
2169 pToU2022State
->cs
[2] = 0;
2170 pToU2022State
->g
= 0;
2173 /* convert one or two bytes */
2174 myData
->isEmptySegment
= FALSE
;
2175 cs
= (StateEnum
)pToU2022State
->cs
[pToU2022State
->g
];
2176 if( (uint8_t)(mySourceChar
- 0xa1) <= (0xdf - 0xa1) && myData
->version
==4 &&
2179 /* 8-bit halfwidth katakana in any single-byte mode for JIS8 */
2180 targetUniChar
= mySourceChar
+ (HWKANA_START
- 0xa1);
2182 /* return from a single-shift state to the previous one */
2183 if(pToU2022State
->g
>= 2) {
2184 pToU2022State
->g
=pToU2022State
->prevG
;
2188 if(mySourceChar
<= 0x7f) {
2189 targetUniChar
= mySourceChar
;
2193 if(mySourceChar
<= 0x7f) {
2194 targetUniChar
= mySourceChar
+ 0x80;
2196 /* return from a single-shift state to the previous one */
2197 pToU2022State
->g
=pToU2022State
->prevG
;
2200 if(mySourceChar
<= 0x7f) {
2201 /* convert mySourceChar+0x80 to use a normal 8-bit table */
2203 _MBCS_SINGLE_SIMPLE_GET_NEXT_BMP(
2204 myData
->myConverterArray
[cs
],
2205 mySourceChar
+ 0x80);
2207 /* return from a single-shift state to the previous one */
2208 pToU2022State
->g
=pToU2022State
->prevG
;
2211 if(mySourceChar
<= 0x7f) {
2212 targetUniChar
= jisx201ToU(mySourceChar
);
2216 if((uint8_t)(mySourceChar
- 0x21) <= (0x5f - 0x21)) {
2217 /* 7-bit halfwidth Katakana */
2218 targetUniChar
= mySourceChar
+ (HWKANA_START
- 0x21);
2223 if(mySource
< mySourceLimit
) {
2224 int leadIsOk
, trailIsOk
;
2227 trailByte
= (uint8_t)*mySource
;
2229 * Ticket 5691: consistent illegal sequences:
2230 * - We include at least the first byte in the illegal sequence.
2231 * - If any of the non-initial bytes could be the start of a character,
2232 * we stop the illegal sequence before the first one of those.
2234 * In ISO-2022 DBCS, if the second byte is in the 21..7e range or is
2235 * an ESC/SO/SI, we report only the first byte as the illegal sequence.
2236 * Otherwise we convert or report the pair of bytes.
2238 leadIsOk
= (uint8_t)(mySourceChar
- 0x21) <= (0x7e - 0x21);
2239 trailIsOk
= (uint8_t)(trailByte
- 0x21) <= (0x7e - 0x21);
2240 if (leadIsOk
&& trailIsOk
) {
2242 tmpSourceChar
= (mySourceChar
<< 8) | trailByte
;
2244 _2022ToSJIS((uint8_t)mySourceChar
, trailByte
, tempBuf
);
2245 mySourceChar
= tmpSourceChar
;
2247 /* Copy before we modify tmpSourceChar so toUnicodeCallback() sees the correct bytes. */
2248 mySourceChar
= tmpSourceChar
;
2249 if (cs
== KSC5601
) {
2250 tmpSourceChar
+= 0x8080; /* = _2022ToGR94DBCS(tmpSourceChar) */
2252 tempBuf
[0] = (char)(tmpSourceChar
>> 8);
2253 tempBuf
[1] = (char)(tmpSourceChar
);
2255 targetUniChar
= ucnv_MBCSSimpleGetNextUChar(myData
->myConverterArray
[cs
], tempBuf
, 2, FALSE
);
2256 } else if (!(trailIsOk
|| IS_2022_CONTROL(trailByte
))) {
2257 /* report a pair of illegal bytes if the second byte is not a DBCS starter */
2259 /* add another bit so that the code below writes 2 bytes in case of error */
2260 mySourceChar
= 0x10000 | (mySourceChar
<< 8) | trailByte
;
2263 args
->converter
->toUBytes
[0] = (uint8_t)mySourceChar
;
2264 args
->converter
->toULength
= 1;
2267 } /* End of inner switch */
2269 } /* End of outer switch */
2270 if(targetUniChar
< (missingCharMarker
-1/*0xfffe*/)){
2272 args
->offsets
[myTarget
- args
->target
] = (int32_t)(mySource
- args
->source
- (mySourceChar
<= 0xff ? 1 : 2));
2274 *(myTarget
++)=(UChar
)targetUniChar
;
2276 else if(targetUniChar
> missingCharMarker
){
2277 /* disassemble the surrogate pair and write to output*/
2278 targetUniChar
-=0x0010000;
2279 *myTarget
= (UChar
)(0xd800+(UChar
)(targetUniChar
>>10));
2281 args
->offsets
[myTarget
- args
->target
] = (int32_t)(mySource
- args
->source
- (mySourceChar
<= 0xff ? 1 : 2));
2284 if(myTarget
< args
->targetLimit
){
2285 *myTarget
= (UChar
)(0xdc00+(UChar
)(targetUniChar
&0x3ff));
2287 args
->offsets
[myTarget
- args
->target
] = (int32_t)(mySource
- args
->source
- (mySourceChar
<= 0xff ? 1 : 2));
2291 args
->converter
->UCharErrorBuffer
[args
->converter
->UCharErrorBufferLength
++]=
2292 (UChar
)(0xdc00+(UChar
)(targetUniChar
&0x3ff));
2297 /* Call the callback function*/
2298 toUnicodeCallback(args
->converter
,mySourceChar
,targetUniChar
,err
);
2302 else{ /* goes with "if(myTarget < args->targetLimit)" way up near top of function */
2303 *err
=U_BUFFER_OVERFLOW_ERROR
;
2308 args
->target
= myTarget
;
2309 args
->source
= mySource
;
2313 #if !UCONFIG_ONLY_HTML_CONVERSION
2314 /***************************************************************
2315 * Rules for ISO-2022-KR encoding
2316 * i) The KSC5601 designator sequence should appear only once in a file,
2317 * at the begining of a line before any KSC5601 characters. This usually
2318 * means that it appears by itself on the first line of the file
2319 * ii) There are only 2 shifting sequences SO to shift into double byte mode
2320 * and SI to shift into single byte mode
2323 UConverter_fromUnicode_ISO_2022_KR_OFFSETS_LOGIC_IBM(UConverterFromUnicodeArgs
* args
, UErrorCode
* err
){
2325 UConverter
* saveConv
= args
->converter
;
2326 UConverterDataISO2022
*myConverterData
=(UConverterDataISO2022
*)saveConv
->extraInfo
;
2327 args
->converter
=myConverterData
->currentConverter
;
2329 myConverterData
->currentConverter
->fromUChar32
= saveConv
->fromUChar32
;
2330 ucnv_MBCSFromUnicodeWithOffsets(args
,err
);
2331 saveConv
->fromUChar32
= myConverterData
->currentConverter
->fromUChar32
;
2333 if(*err
== U_BUFFER_OVERFLOW_ERROR
) {
2334 if(myConverterData
->currentConverter
->charErrorBufferLength
> 0) {
2336 saveConv
->charErrorBuffer
,
2337 myConverterData
->currentConverter
->charErrorBuffer
,
2338 myConverterData
->currentConverter
->charErrorBufferLength
);
2340 saveConv
->charErrorBufferLength
= myConverterData
->currentConverter
->charErrorBufferLength
;
2341 myConverterData
->currentConverter
->charErrorBufferLength
= 0;
2343 args
->converter
=saveConv
;
2347 UConverter_fromUnicode_ISO_2022_KR_OFFSETS_LOGIC(UConverterFromUnicodeArgs
* args
, UErrorCode
* err
){
2349 const UChar
*source
= args
->source
;
2350 const UChar
*sourceLimit
= args
->sourceLimit
;
2351 unsigned char *target
= (unsigned char *) args
->target
;
2352 unsigned char *targetLimit
= (unsigned char *) args
->targetLimit
;
2353 int32_t* offsets
= args
->offsets
;
2354 uint32_t targetByteUnit
= 0x0000;
2355 UChar32 sourceChar
= 0x0000;
2356 UBool isTargetByteDBCS
;
2357 UBool oldIsTargetByteDBCS
;
2358 UConverterDataISO2022
*converterData
;
2359 UConverterSharedData
* sharedData
;
2363 converterData
=(UConverterDataISO2022
*)args
->converter
->extraInfo
;
2364 /* if the version is 1 then the user is requesting
2365 * conversion with ibm-25546 pass the arguments to
2366 * MBCS converter and return
2368 if(converterData
->version
==1){
2369 UConverter_fromUnicode_ISO_2022_KR_OFFSETS_LOGIC_IBM(args
,err
);
2373 /* initialize data */
2374 sharedData
= converterData
->currentConverter
->sharedData
;
2375 useFallback
= args
->converter
->useFallback
;
2376 isTargetByteDBCS
=(UBool
)args
->converter
->fromUnicodeStatus
;
2377 oldIsTargetByteDBCS
= isTargetByteDBCS
;
2379 isTargetByteDBCS
= (UBool
) args
->converter
->fromUnicodeStatus
;
2380 if((sourceChar
= args
->converter
->fromUChar32
)!=0 && target
<targetLimit
) {
2383 while(source
< sourceLimit
){
2385 targetByteUnit
= missingCharMarker
;
2387 if(target
< (unsigned char*) args
->targetLimit
){
2388 sourceChar
= *source
++;
2390 /* do not convert SO/SI/ESC */
2391 if(IS_2022_CONTROL(sourceChar
)) {
2392 /* callback(illegal) */
2393 *err
=U_ILLEGAL_CHAR_FOUND
;
2394 args
->converter
->fromUChar32
=sourceChar
;
2398 length
= MBCS_FROM_UCHAR32_ISO2022(sharedData
,sourceChar
,&targetByteUnit
,useFallback
,MBCS_OUTPUT_2
);
2400 length
= -length
; /* fallback */
2402 /* only DBCS or SBCS characters are expected*/
2403 /* DB characters with high bit set to 1 are expected */
2404 if( length
> 2 || length
==0 ||
2405 (length
== 1 && targetByteUnit
> 0x7f) ||
2407 ((uint16_t)(targetByteUnit
- 0xa1a1) > (0xfefe - 0xa1a1) ||
2408 (uint8_t)(targetByteUnit
- 0xa1) > (0xfe - 0xa1)))
2410 targetByteUnit
=missingCharMarker
;
2412 if (targetByteUnit
!= missingCharMarker
){
2414 oldIsTargetByteDBCS
= isTargetByteDBCS
;
2415 isTargetByteDBCS
= (UBool
)(targetByteUnit
>0x00FF);
2416 /* append the shift sequence */
2417 if (oldIsTargetByteDBCS
!= isTargetByteDBCS
){
2419 if (isTargetByteDBCS
)
2420 *target
++ = UCNV_SO
;
2422 *target
++ = UCNV_SI
;
2424 *(offsets
++) = (int32_t)(source
- args
->source
-1);
2426 /* write the targetUniChar to target */
2427 if(targetByteUnit
<= 0x00FF){
2428 if( target
< targetLimit
){
2429 *(target
++) = (unsigned char) targetByteUnit
;
2431 *(offsets
++) = (int32_t)(source
- args
->source
-1);
2435 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] = (unsigned char) (targetByteUnit
);
2436 *err
= U_BUFFER_OVERFLOW_ERROR
;
2439 if(target
< targetLimit
){
2440 *(target
++) =(unsigned char) ((targetByteUnit
>>8) -0x80);
2442 *(offsets
++) = (int32_t)(source
- args
->source
-1);
2444 if(target
< targetLimit
){
2445 *(target
++) =(unsigned char) (targetByteUnit
-0x80);
2447 *(offsets
++) = (int32_t)(source
- args
->source
-1);
2450 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] = (unsigned char) (targetByteUnit
-0x80);
2451 *err
= U_BUFFER_OVERFLOW_ERROR
;
2454 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] = (unsigned char) ((targetByteUnit
>>8) -0x80);
2455 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] = (unsigned char) (targetByteUnit
-0x80);
2456 *err
= U_BUFFER_OVERFLOW_ERROR
;
2462 /* oops.. the code point is unassingned
2463 * set the error and reason
2466 /*check if the char is a First surrogate*/
2467 if(U16_IS_SURROGATE(sourceChar
)) {
2468 if(U16_IS_SURROGATE_LEAD(sourceChar
)) {
2470 /*look ahead to find the trail surrogate*/
2471 if(source
< sourceLimit
) {
2472 /* test the following code unit */
2473 UChar trail
=(UChar
) *source
;
2474 if(U16_IS_TRAIL(trail
)) {
2476 sourceChar
=U16_GET_SUPPLEMENTARY(sourceChar
, trail
);
2477 *err
= U_INVALID_CHAR_FOUND
;
2478 /* convert this surrogate code point */
2479 /* exit this condition tree */
2481 /* this is an unmatched lead code unit (1st surrogate) */
2482 /* callback(illegal) */
2483 *err
=U_ILLEGAL_CHAR_FOUND
;
2487 *err
= U_ZERO_ERROR
;
2490 /* this is an unmatched trail code unit (2nd surrogate) */
2491 /* callback(illegal) */
2492 *err
=U_ILLEGAL_CHAR_FOUND
;
2495 /* callback(unassigned) for a BMP code point */
2496 *err
= U_INVALID_CHAR_FOUND
;
2499 args
->converter
->fromUChar32
=sourceChar
;
2502 } /* end if(myTargetIndex<myTargetLength) */
2504 *err
=U_BUFFER_OVERFLOW_ERROR
;
2508 }/* end while(mySourceIndex<mySourceLength) */
2511 * the end of the input stream and detection of truncated input
2512 * are handled by the framework, but for ISO-2022-KR conversion
2513 * we need to be in ASCII mode at the very end
2518 * end of input and no truncated input
2520 if( U_SUCCESS(*err
) &&
2522 args
->flush
&& source
>=sourceLimit
&& args
->converter
->fromUChar32
==0
2524 int32_t sourceIndex
;
2526 /* we are switching to ASCII */
2527 isTargetByteDBCS
=FALSE
;
2529 /* get the source index of the last input character */
2531 * TODO this would be simpler and more reliable if we used a pair
2532 * of sourceIndex/prevSourceIndex like in ucnvmbcs.c
2533 * so that we could simply use the prevSourceIndex here;
2534 * this code gives an incorrect result for the rare case of an unmatched
2535 * trail surrogate that is alone in the last buffer of the text stream
2537 sourceIndex
=(int32_t)(source
-args
->source
);
2540 if( U16_IS_TRAIL(args
->source
[sourceIndex
]) &&
2541 (sourceIndex
==0 || U16_IS_LEAD(args
->source
[sourceIndex
-1]))
2552 &target
, (const char *)targetLimit
,
2553 &offsets
, sourceIndex
,
2557 /*save the state and return */
2558 args
->source
= source
;
2559 args
->target
= (char*)target
;
2560 args
->converter
->fromUnicodeStatus
= (uint32_t)isTargetByteDBCS
;
2563 /************************ To Unicode ***************************************/
2566 UConverter_toUnicode_ISO_2022_KR_OFFSETS_LOGIC_IBM(UConverterToUnicodeArgs
*args
,
2568 char const* sourceStart
;
2569 UConverterDataISO2022
* myData
=(UConverterDataISO2022
*)(args
->converter
->extraInfo
);
2571 UConverterToUnicodeArgs subArgs
;
2572 int32_t minArgsSize
;
2574 /* set up the subconverter arguments */
2575 if(args
->size
<sizeof(UConverterToUnicodeArgs
)) {
2576 minArgsSize
= args
->size
;
2578 minArgsSize
= (int32_t)sizeof(UConverterToUnicodeArgs
);
2581 uprv_memcpy(&subArgs
, args
, minArgsSize
);
2582 subArgs
.size
= (uint16_t)minArgsSize
;
2583 subArgs
.converter
= myData
->currentConverter
;
2585 /* remember the original start of the input for offsets */
2586 sourceStart
= args
->source
;
2588 if(myData
->key
!= 0) {
2589 /* continue with a partial escape sequence */
2593 while(U_SUCCESS(*err
) && args
->source
< args
->sourceLimit
) {
2594 /*Find the end of the buffer e.g : Next Escape Seq | end of Buffer*/
2595 subArgs
.source
= args
->source
;
2596 subArgs
.sourceLimit
= getEndOfBuffer_2022(&(args
->source
), args
->sourceLimit
, args
->flush
);
2597 if(subArgs
.source
!= subArgs
.sourceLimit
) {
2599 * get the current partial byte sequence
2601 * it needs to be moved between the public and the subconverter
2602 * so that the conversion framework, which only sees the public
2603 * converter, can handle truncated and illegal input etc.
2605 if(args
->converter
->toULength
> 0) {
2606 uprv_memcpy(subArgs
.converter
->toUBytes
, args
->converter
->toUBytes
, args
->converter
->toULength
);
2608 subArgs
.converter
->toULength
= args
->converter
->toULength
;
2611 * Convert up to the end of the input, or to before the next escape character.
2612 * Does not handle conversion extensions because the preToU[] state etc.
2615 ucnv_MBCSToUnicodeWithOffsets(&subArgs
, err
);
2617 if(args
->offsets
!= NULL
&& sourceStart
!= args
->source
) {
2618 /* update offsets to base them on the actual start of the input */
2619 int32_t *offsets
= args
->offsets
;
2620 UChar
*target
= args
->target
;
2621 int32_t delta
= (int32_t)(args
->source
- sourceStart
);
2622 while(target
< subArgs
.target
) {
2630 args
->source
= subArgs
.source
;
2631 args
->target
= subArgs
.target
;
2632 args
->offsets
= subArgs
.offsets
;
2634 /* copy input/error/overflow buffers */
2635 if(subArgs
.converter
->toULength
> 0) {
2636 uprv_memcpy(args
->converter
->toUBytes
, subArgs
.converter
->toUBytes
, subArgs
.converter
->toULength
);
2638 args
->converter
->toULength
= subArgs
.converter
->toULength
;
2640 if(*err
== U_BUFFER_OVERFLOW_ERROR
) {
2641 if(subArgs
.converter
->UCharErrorBufferLength
> 0) {
2642 uprv_memcpy(args
->converter
->UCharErrorBuffer
, subArgs
.converter
->UCharErrorBuffer
,
2643 subArgs
.converter
->UCharErrorBufferLength
);
2645 args
->converter
->UCharErrorBufferLength
=subArgs
.converter
->UCharErrorBufferLength
;
2646 subArgs
.converter
->UCharErrorBufferLength
= 0;
2650 if (U_FAILURE(*err
) || (args
->source
== args
->sourceLimit
)) {
2655 changeState_2022(args
->converter
,
2664 UConverter_toUnicode_ISO_2022_KR_OFFSETS_LOGIC(UConverterToUnicodeArgs
*args
,
2667 const char *mySource
= ( char *) args
->source
;
2668 UChar
*myTarget
= args
->target
;
2669 const char *mySourceLimit
= args
->sourceLimit
;
2670 UChar32 targetUniChar
= 0x0000;
2671 UChar mySourceChar
= 0x0000;
2672 UConverterDataISO2022
* myData
;
2673 UConverterSharedData
* sharedData
;
2676 myData
=(UConverterDataISO2022
*)(args
->converter
->extraInfo
);
2677 if(myData
->version
==1){
2678 UConverter_toUnicode_ISO_2022_KR_OFFSETS_LOGIC_IBM(args
,err
);
2682 /* initialize state */
2683 sharedData
= myData
->currentConverter
->sharedData
;
2684 useFallback
= args
->converter
->useFallback
;
2686 if(myData
->key
!= 0) {
2687 /* continue with a partial escape sequence */
2689 } else if(args
->converter
->toULength
== 1 && mySource
< mySourceLimit
&& myTarget
< args
->targetLimit
) {
2690 /* continue with a partial double-byte character */
2691 mySourceChar
= args
->converter
->toUBytes
[0];
2692 args
->converter
->toULength
= 0;
2696 while(mySource
< mySourceLimit
){
2698 if(myTarget
< args
->targetLimit
){
2700 mySourceChar
= (unsigned char) *mySource
++;
2702 if(mySourceChar
==UCNV_SI
){
2703 myData
->toU2022State
.g
= 0;
2704 if (myData
->isEmptySegment
) {
2705 myData
->isEmptySegment
= FALSE
; /* we are handling it, reset to avoid future spurious errors */
2706 *err
= U_ILLEGAL_ESCAPE_SEQUENCE
;
2707 args
->converter
->toUCallbackReason
= UCNV_IRREGULAR
;
2708 args
->converter
->toUBytes
[0] = (uint8_t)mySourceChar
;
2709 args
->converter
->toULength
= 1;
2710 args
->target
= myTarget
;
2711 args
->source
= mySource
;
2714 /*consume the source */
2716 }else if(mySourceChar
==UCNV_SO
){
2717 myData
->toU2022State
.g
= 1;
2718 myData
->isEmptySegment
= TRUE
; /* Begin a new segment, empty so far */
2719 /*consume the source */
2721 }else if(mySourceChar
==ESC_2022
){
2724 myData
->isEmptySegment
= FALSE
; /* Any invalid ESC sequences will be detected separately, so just reset this */
2725 changeState_2022(args
->converter
,&(mySource
),
2726 mySourceLimit
, ISO_2022_KR
, err
);
2727 if(U_FAILURE(*err
)){
2728 args
->target
= myTarget
;
2729 args
->source
= mySource
;
2735 myData
->isEmptySegment
= FALSE
; /* Any invalid char errors will be detected separately, so just reset this */
2736 if(myData
->toU2022State
.g
== 1) {
2737 if(mySource
< mySourceLimit
) {
2738 int leadIsOk
, trailIsOk
;
2741 targetUniChar
= missingCharMarker
;
2742 trailByte
= (uint8_t)*mySource
;
2744 * Ticket 5691: consistent illegal sequences:
2745 * - We include at least the first byte in the illegal sequence.
2746 * - If any of the non-initial bytes could be the start of a character,
2747 * we stop the illegal sequence before the first one of those.
2749 * In ISO-2022 DBCS, if the second byte is in the 21..7e range or is
2750 * an ESC/SO/SI, we report only the first byte as the illegal sequence.
2751 * Otherwise we convert or report the pair of bytes.
2753 leadIsOk
= (uint8_t)(mySourceChar
- 0x21) <= (0x7e - 0x21);
2754 trailIsOk
= (uint8_t)(trailByte
- 0x21) <= (0x7e - 0x21);
2755 if (leadIsOk
&& trailIsOk
) {
2757 tempBuf
[0] = (char)(mySourceChar
+ 0x80);
2758 tempBuf
[1] = (char)(trailByte
+ 0x80);
2759 targetUniChar
= ucnv_MBCSSimpleGetNextUChar(sharedData
, tempBuf
, 2, useFallback
);
2760 mySourceChar
= (mySourceChar
<< 8) | trailByte
;
2761 } else if (!(trailIsOk
|| IS_2022_CONTROL(trailByte
))) {
2762 /* report a pair of illegal bytes if the second byte is not a DBCS starter */
2764 /* add another bit so that the code below writes 2 bytes in case of error */
2765 mySourceChar
= 0x10000 | (mySourceChar
<< 8) | trailByte
;
2768 args
->converter
->toUBytes
[0] = (uint8_t)mySourceChar
;
2769 args
->converter
->toULength
= 1;
2773 else if(mySourceChar
<= 0x7f) {
2774 targetUniChar
= ucnv_MBCSSimpleGetNextUChar(sharedData
, mySource
- 1, 1, useFallback
);
2776 targetUniChar
= 0xffff;
2778 if(targetUniChar
< 0xfffe){
2780 args
->offsets
[myTarget
- args
->target
] = (int32_t)(mySource
- args
->source
- (mySourceChar
<= 0xff ? 1 : 2));
2782 *(myTarget
++)=(UChar
)targetUniChar
;
2785 /* Call the callback function*/
2786 toUnicodeCallback(args
->converter
,mySourceChar
,targetUniChar
,err
);
2791 *err
=U_BUFFER_OVERFLOW_ERROR
;
2795 args
->target
= myTarget
;
2796 args
->source
= mySource
;
2799 /*************************** END ISO2022-KR *********************************/
2801 /*************************** ISO-2022-CN *********************************
2803 * Rules for ISO-2022-CN Encoding:
2804 * i) The designator sequence must appear once on a line before any instance
2805 * of character set it designates.
2806 * ii) If two lines contain characters from the same character set, both lines
2807 * must include the designator sequence.
2808 * iii) Once the designator sequence is known, a shifting sequence has to be found
2809 * to invoke the shifting
2810 * iv) All lines start in ASCII and end in ASCII.
2811 * v) Four shifting sequences are employed for this purpose:
2813 * Sequcence ASCII Eq Charsets
2814 * ---------- ------- ---------
2816 * SO <SO> CNS-11643-1992 Plane 1, GB2312, ISO-IR-165
2817 * SS2 <ESC>N CNS-11643-1992 Plane 2
2818 * SS3 <ESC>O CNS-11643-1992 Planes 3-7
2821 * SOdesignator : ESC "$" ")" finalchar_for_SO
2822 * SS2designator : ESC "$" "*" finalchar_for_SS2
2823 * SS3designator : ESC "$" "+" finalchar_for_SS3
2825 * ESC $ ) A Indicates the bytes following SO are Chinese
2826 * characters as defined in GB 2312-80, until
2827 * another SOdesignation appears
2830 * ESC $ ) E Indicates the bytes following SO are as defined
2831 * in ISO-IR-165 (for details, see section 2.1),
2832 * until another SOdesignation appears
2834 * ESC $ ) G Indicates the bytes following SO are as defined
2835 * in CNS 11643-plane-1, until another
2836 * SOdesignation appears
2838 * ESC $ * H Indicates the two bytes immediately following
2839 * SS2 is a Chinese character as defined in CNS
2840 * 11643-plane-2, until another SS2designation
2842 * (Meaning <ESC>N must preceed every 2 byte
2845 * ESC $ + I Indicates the immediate two bytes following SS3
2846 * is a Chinese character as defined in CNS
2847 * 11643-plane-3, until another SS3designation
2849 * (Meaning <ESC>O must preceed every 2 byte
2852 * ESC $ + J Indicates the immediate two bytes following SS3
2853 * is a Chinese character as defined in CNS
2854 * 11643-plane-4, until another SS3designation
2856 * (In English: <ESC>O must preceed every 2 byte
2859 * ESC $ + K Indicates the immediate two bytes following SS3
2860 * is a Chinese character as defined in CNS
2861 * 11643-plane-5, until another SS3designation
2864 * ESC $ + L Indicates the immediate two bytes following SS3
2865 * is a Chinese character as defined in CNS
2866 * 11643-plane-6, until another SS3designation
2869 * ESC $ + M Indicates the immediate two bytes following SS3
2870 * is a Chinese character as defined in CNS
2871 * 11643-plane-7, until another SS3designation
2874 * As in ISO-2022-CN, each line starts in ASCII, and ends in ASCII, and
2875 * has its own designation information before any Chinese characters
2880 /* The following are defined this way to make the strings truly readonly */
2881 static const char GB_2312_80_STR
[] = "\x1B\x24\x29\x41";
2882 static const char ISO_IR_165_STR
[] = "\x1B\x24\x29\x45";
2883 static const char CNS_11643_1992_Plane_1_STR
[] = "\x1B\x24\x29\x47";
2884 static const char CNS_11643_1992_Plane_2_STR
[] = "\x1B\x24\x2A\x48";
2885 static const char CNS_11643_1992_Plane_3_STR
[] = "\x1B\x24\x2B\x49";
2886 static const char CNS_11643_1992_Plane_4_STR
[] = "\x1B\x24\x2B\x4A";
2887 static const char CNS_11643_1992_Plane_5_STR
[] = "\x1B\x24\x2B\x4B";
2888 static const char CNS_11643_1992_Plane_6_STR
[] = "\x1B\x24\x2B\x4C";
2889 static const char CNS_11643_1992_Plane_7_STR
[] = "\x1B\x24\x2B\x4D";
2891 /********************** ISO2022-CN Data **************************/
2892 static const char* const escSeqCharsCN
[10] ={
2893 SHIFT_IN_STR
, /* 0 ASCII */
2894 GB_2312_80_STR
, /* 1 GB2312_1 */
2895 ISO_IR_165_STR
, /* 2 ISO_IR_165 */
2896 CNS_11643_1992_Plane_1_STR
,
2897 CNS_11643_1992_Plane_2_STR
,
2898 CNS_11643_1992_Plane_3_STR
,
2899 CNS_11643_1992_Plane_4_STR
,
2900 CNS_11643_1992_Plane_5_STR
,
2901 CNS_11643_1992_Plane_6_STR
,
2902 CNS_11643_1992_Plane_7_STR
2906 UConverter_fromUnicode_ISO_2022_CN_OFFSETS_LOGIC(UConverterFromUnicodeArgs
* args
, UErrorCode
* err
){
2907 UConverter
*cnv
= args
->converter
;
2908 UConverterDataISO2022
*converterData
;
2909 ISO2022State
*pFromU2022State
;
2910 uint8_t *target
= (uint8_t *) args
->target
;
2911 const uint8_t *targetLimit
= (const uint8_t *) args
->targetLimit
;
2912 const UChar
* source
= args
->source
;
2913 const UChar
* sourceLimit
= args
->sourceLimit
;
2914 int32_t* offsets
= args
->offsets
;
2919 int32_t choiceCount
;
2920 uint32_t targetValue
= 0;
2923 /* set up the state */
2924 converterData
= (UConverterDataISO2022
*)cnv
->extraInfo
;
2925 pFromU2022State
= &converterData
->fromU2022State
;
2929 /* check if the last codepoint of previous buffer was a lead surrogate*/
2930 if((sourceChar
= cnv
->fromUChar32
)!=0 && target
< targetLimit
) {
2934 while( source
< sourceLimit
){
2935 if(target
< targetLimit
){
2937 sourceChar
= *(source
++);
2938 /*check if the char is a First surrogate*/
2939 if(U16_IS_SURROGATE(sourceChar
)) {
2940 if(U16_IS_SURROGATE_LEAD(sourceChar
)) {
2942 /*look ahead to find the trail surrogate*/
2943 if(source
< sourceLimit
) {
2944 /* test the following code unit */
2945 UChar trail
=(UChar
) *source
;
2946 if(U16_IS_TRAIL(trail
)) {
2948 sourceChar
=U16_GET_SUPPLEMENTARY(sourceChar
, trail
);
2949 cnv
->fromUChar32
=0x00;
2950 /* convert this supplementary code point */
2951 /* exit this condition tree */
2953 /* this is an unmatched lead code unit (1st surrogate) */
2954 /* callback(illegal) */
2955 *err
=U_ILLEGAL_CHAR_FOUND
;
2956 cnv
->fromUChar32
=sourceChar
;
2961 cnv
->fromUChar32
=sourceChar
;
2965 /* this is an unmatched trail code unit (2nd surrogate) */
2966 /* callback(illegal) */
2967 *err
=U_ILLEGAL_CHAR_FOUND
;
2968 cnv
->fromUChar32
=sourceChar
;
2973 /* do the conversion */
2974 if(sourceChar
<= 0x007f ){
2975 /* do not convert SO/SI/ESC */
2976 if(IS_2022_CONTROL(sourceChar
)) {
2977 /* callback(illegal) */
2978 *err
=U_ILLEGAL_CHAR_FOUND
;
2979 cnv
->fromUChar32
=sourceChar
;
2984 if(pFromU2022State
->g
== 0) {
2985 buffer
[0] = (char)sourceChar
;
2988 buffer
[0] = UCNV_SI
;
2989 buffer
[1] = (char)sourceChar
;
2991 pFromU2022State
->g
= 0;
2994 if(sourceChar
== CR
|| sourceChar
== LF
) {
2995 /* reset the state at the end of a line */
2996 uprv_memset(pFromU2022State
, 0, sizeof(ISO2022State
));
3001 /* convert U+0080..U+10ffff */
3005 if(choiceCount
== 0) {
3006 /* try the current SO/G1 converter first */
3007 choices
[0] = pFromU2022State
->cs
[1];
3009 /* default to GB2312_1 if none is designated yet */
3010 if(choices
[0] == 0) {
3011 choices
[0] = GB2312_1
;
3014 if(converterData
->version
== 0) {
3017 /* try the other SO/G1 converter; a CNS_11643_1 lookup may result in any plane */
3018 if(choices
[0] == GB2312_1
) {
3019 choices
[1] = (int8_t)CNS_11643_1
;
3021 choices
[1] = (int8_t)GB2312_1
;
3025 } else if (converterData
->version
== 1) {
3026 /* ISO-2022-CN-EXT */
3028 /* try one of the other converters */
3029 switch(choices
[0]) {
3031 choices
[1] = (int8_t)CNS_11643_1
;
3032 choices
[2] = (int8_t)ISO_IR_165
;
3035 choices
[1] = (int8_t)GB2312_1
;
3036 choices
[2] = (int8_t)CNS_11643_1
;
3038 default: /* CNS_11643_x */
3039 choices
[1] = (int8_t)GB2312_1
;
3040 choices
[2] = (int8_t)ISO_IR_165
;
3046 choices
[0] = (int8_t)CNS_11643_1
;
3047 choices
[1] = (int8_t)GB2312_1
;
3053 * len==0: no mapping found yet
3054 * len<0: found a fallback result: continue looking for a roundtrip but no further fallbacks
3055 * len>0: found a roundtrip result, done
3059 * We will turn off useFallback after finding a fallback,
3060 * but we still get fallbacks from PUA code points as usual.
3061 * Therefore, we will also need to check that we don't overwrite
3062 * an early fallback with a later one.
3064 useFallback
= cnv
->useFallback
;
3066 for(i
= 0; i
< choiceCount
&& len
<= 0; ++i
) {
3067 int8_t cs0
= choices
[i
];
3071 if(cs0
>= CNS_11643_0
) {
3072 len2
= MBCS_FROM_UCHAR32_ISO2022(
3073 converterData
->myConverterArray
[CNS_11643
],
3078 if(len2
== 3 || (len2
== -3 && len
== 0)) {
3079 targetValue
= value
;
3080 cs
= (int8_t)(CNS_11643_0
+ (value
>> 16) - 0x80);
3085 useFallback
= FALSE
;
3087 if(cs
== CNS_11643_1
) {
3089 } else if(cs
== CNS_11643_2
) {
3091 } else /* plane 3..7 */ if(converterData
->version
== 1) {
3094 /* ISO-2022-CN (without -EXT) does not support plane 3..7 */
3099 /* GB2312_1 or ISO-IR-165 */
3100 U_ASSERT(cs0
<UCNV_2022_MAX_CONVERTERS
);
3101 len2
= MBCS_FROM_UCHAR32_ISO2022(
3102 converterData
->myConverterArray
[cs0
],
3107 if(len2
== 2 || (len2
== -2 && len
== 0)) {
3108 targetValue
= value
;
3112 useFallback
= FALSE
;
3119 len
= 0; /* count output bytes; it must have been abs(len) == 2 */
3121 /* write the designation sequence if necessary */
3122 if(cs
!= pFromU2022State
->cs
[g
]) {
3123 if(cs
< CNS_11643
) {
3124 uprv_memcpy(buffer
, escSeqCharsCN
[cs
], 4);
3126 U_ASSERT(cs
>= CNS_11643_1
);
3127 uprv_memcpy(buffer
, escSeqCharsCN
[CNS_11643
+ (cs
- CNS_11643_1
)], 4);
3130 pFromU2022State
->cs
[g
] = cs
;
3132 /* changing the SO/G1 charset invalidates the choices[] */
3137 /* write the shift sequence if necessary */
3138 if(g
!= pFromU2022State
->g
) {
3141 buffer
[len
++] = UCNV_SO
;
3143 /* set the new state only if it is the locking shift SO/G1, not for SS2 or SS3 */
3144 pFromU2022State
->g
= 1;
3147 buffer
[len
++] = 0x1b;
3148 buffer
[len
++] = 0x4e;
3150 default: /* case 3 */
3151 buffer
[len
++] = 0x1b;
3152 buffer
[len
++] = 0x4f;
3157 /* write the two output bytes */
3158 buffer
[len
++] = (char)(targetValue
>> 8);
3159 buffer
[len
++] = (char)targetValue
;
3161 /* if we cannot find the character after checking all codepages
3162 * then this is an error
3164 *err
= U_INVALID_CHAR_FOUND
;
3165 cnv
->fromUChar32
=sourceChar
;
3170 /* output len>0 bytes in buffer[] */
3172 *target
++ = buffer
[0];
3174 *offsets
++ = (int32_t)(source
- args
->source
- 1); /* -1: known to be ASCII */
3176 } else if(len
== 2 && (target
+ 2) <= targetLimit
) {
3177 *target
++ = buffer
[0];
3178 *target
++ = buffer
[1];
3180 int32_t sourceIndex
= (int32_t)(source
- args
->source
- U16_LENGTH(sourceChar
));
3181 *offsets
++ = sourceIndex
;
3182 *offsets
++ = sourceIndex
;
3188 &target
, (const char *)targetLimit
,
3189 &offsets
, (int32_t)(source
- args
->source
- U16_LENGTH(sourceChar
)),
3191 if(U_FAILURE(*err
)) {
3195 } /* end if(myTargetIndex<myTargetLength) */
3197 *err
=U_BUFFER_OVERFLOW_ERROR
;
3201 }/* end while(mySourceIndex<mySourceLength) */
3204 * the end of the input stream and detection of truncated input
3205 * are handled by the framework, but for ISO-2022-CN conversion
3206 * we need to be in ASCII mode at the very end
3211 * end of input and no truncated input
3213 if( U_SUCCESS(*err
) &&
3214 pFromU2022State
->g
!=0 &&
3215 args
->flush
&& source
>=sourceLimit
&& cnv
->fromUChar32
==0
3217 int32_t sourceIndex
;
3219 /* we are switching to ASCII */
3220 pFromU2022State
->g
=0;
3222 /* get the source index of the last input character */
3224 * TODO this would be simpler and more reliable if we used a pair
3225 * of sourceIndex/prevSourceIndex like in ucnvmbcs.c
3226 * so that we could simply use the prevSourceIndex here;
3227 * this code gives an incorrect result for the rare case of an unmatched
3228 * trail surrogate that is alone in the last buffer of the text stream
3230 sourceIndex
=(int32_t)(source
-args
->source
);
3233 if( U16_IS_TRAIL(args
->source
[sourceIndex
]) &&
3234 (sourceIndex
==0 || U16_IS_LEAD(args
->source
[sourceIndex
-1]))
3245 &target
, (const char *)targetLimit
,
3246 &offsets
, sourceIndex
,
3250 /*save the state and return */
3251 args
->source
= source
;
3252 args
->target
= (char*)target
;
3257 UConverter_toUnicode_ISO_2022_CN_OFFSETS_LOGIC(UConverterToUnicodeArgs
*args
,
3260 const char *mySource
= (char *) args
->source
;
3261 UChar
*myTarget
= args
->target
;
3262 const char *mySourceLimit
= args
->sourceLimit
;
3263 uint32_t targetUniChar
= 0x0000;
3264 uint32_t mySourceChar
= 0x0000;
3265 UConverterDataISO2022
* myData
;
3266 ISO2022State
*pToU2022State
;
3268 myData
=(UConverterDataISO2022
*)(args
->converter
->extraInfo
);
3269 pToU2022State
= &myData
->toU2022State
;
3271 if(myData
->key
!= 0) {
3272 /* continue with a partial escape sequence */
3274 } else if(args
->converter
->toULength
== 1 && mySource
< mySourceLimit
&& myTarget
< args
->targetLimit
) {
3275 /* continue with a partial double-byte character */
3276 mySourceChar
= args
->converter
->toUBytes
[0];
3277 args
->converter
->toULength
= 0;
3278 targetUniChar
= missingCharMarker
;
3282 while(mySource
< mySourceLimit
){
3284 targetUniChar
=missingCharMarker
;
3286 if(myTarget
< args
->targetLimit
){
3288 mySourceChar
= (unsigned char) *mySource
++;
3290 switch(mySourceChar
){
3293 if (myData
->isEmptySegment
) {
3294 myData
->isEmptySegment
= FALSE
; /* we are handling it, reset to avoid future spurious errors */
3295 *err
= U_ILLEGAL_ESCAPE_SEQUENCE
;
3296 args
->converter
->toUCallbackReason
= UCNV_IRREGULAR
;
3297 args
->converter
->toUBytes
[0] = mySourceChar
;
3298 args
->converter
->toULength
= 1;
3299 args
->target
= myTarget
;
3300 args
->source
= mySource
;
3306 if(pToU2022State
->cs
[1] != 0) {
3308 myData
->isEmptySegment
= TRUE
; /* Begin a new segment, empty so far */
3311 /* illegal to have SO before a matching designator */
3312 myData
->isEmptySegment
= FALSE
; /* Handling a different error, reset this to avoid future spurious errs */
3320 const char * mySourceBefore
= mySource
;
3321 int8_t toULengthBefore
= args
->converter
->toULength
;
3323 changeState_2022(args
->converter
,&(mySource
),
3324 mySourceLimit
, ISO_2022_CN
,err
);
3326 /* After SO there must be at least one character before a designator (designator error handled separately) */
3327 if(myData
->key
==0 && U_SUCCESS(*err
) && myData
->isEmptySegment
) {
3328 *err
= U_ILLEGAL_ESCAPE_SEQUENCE
;
3329 args
->converter
->toUCallbackReason
= UCNV_IRREGULAR
;
3330 args
->converter
->toULength
= (int8_t)(toULengthBefore
+ (mySource
- mySourceBefore
));
3334 /* invalid or illegal escape sequence */
3335 if(U_FAILURE(*err
)){
3336 args
->target
= myTarget
;
3337 args
->source
= mySource
;
3338 myData
->isEmptySegment
= FALSE
; /* Reset to avoid future spurious errors */
3343 /* ISO-2022-CN does not use single-byte (C1) SS2 and SS3 */
3348 uprv_memset(pToU2022State
, 0, sizeof(ISO2022State
));
3351 /* convert one or two bytes */
3352 myData
->isEmptySegment
= FALSE
;
3353 if(pToU2022State
->g
!= 0) {
3354 if(mySource
< mySourceLimit
) {
3355 UConverterSharedData
*cnv
;
3356 StateEnum tempState
;
3358 int leadIsOk
, trailIsOk
;
3361 trailByte
= (uint8_t)*mySource
;
3363 * Ticket 5691: consistent illegal sequences:
3364 * - We include at least the first byte in the illegal sequence.
3365 * - If any of the non-initial bytes could be the start of a character,
3366 * we stop the illegal sequence before the first one of those.
3368 * In ISO-2022 DBCS, if the second byte is in the 21..7e range or is
3369 * an ESC/SO/SI, we report only the first byte as the illegal sequence.
3370 * Otherwise we convert or report the pair of bytes.
3372 leadIsOk
= (uint8_t)(mySourceChar
- 0x21) <= (0x7e - 0x21);
3373 trailIsOk
= (uint8_t)(trailByte
- 0x21) <= (0x7e - 0x21);
3374 if (leadIsOk
&& trailIsOk
) {
3376 tempState
= (StateEnum
)pToU2022State
->cs
[pToU2022State
->g
];
3377 if(tempState
>= CNS_11643_0
) {
3378 cnv
= myData
->myConverterArray
[CNS_11643
];
3379 tempBuf
[0] = (char) (0x80+(tempState
-CNS_11643_0
));
3380 tempBuf
[1] = (char) (mySourceChar
);
3381 tempBuf
[2] = (char) trailByte
;
3385 U_ASSERT(tempState
<UCNV_2022_MAX_CONVERTERS
);
3386 cnv
= myData
->myConverterArray
[tempState
];
3387 tempBuf
[0] = (char) (mySourceChar
);
3388 tempBuf
[1] = (char) trailByte
;
3391 targetUniChar
= ucnv_MBCSSimpleGetNextUChar(cnv
, tempBuf
, tempBufLen
, FALSE
);
3392 mySourceChar
= (mySourceChar
<< 8) | trailByte
;
3393 } else if (!(trailIsOk
|| IS_2022_CONTROL(trailByte
))) {
3394 /* report a pair of illegal bytes if the second byte is not a DBCS starter */
3396 /* add another bit so that the code below writes 2 bytes in case of error */
3397 mySourceChar
= 0x10000 | (mySourceChar
<< 8) | trailByte
;
3399 if(pToU2022State
->g
>=2) {
3400 /* return from a single-shift state to the previous one */
3401 pToU2022State
->g
=pToU2022State
->prevG
;
3404 args
->converter
->toUBytes
[0] = (uint8_t)mySourceChar
;
3405 args
->converter
->toULength
= 1;
3410 if(mySourceChar
<= 0x7f) {
3411 targetUniChar
= (UChar
) mySourceChar
;
3416 if(targetUniChar
< (missingCharMarker
-1/*0xfffe*/)){
3418 args
->offsets
[myTarget
- args
->target
] = (int32_t)(mySource
- args
->source
- (mySourceChar
<= 0xff ? 1 : 2));
3420 *(myTarget
++)=(UChar
)targetUniChar
;
3422 else if(targetUniChar
> missingCharMarker
){
3423 /* disassemble the surrogate pair and write to output*/
3424 targetUniChar
-=0x0010000;
3425 *myTarget
= (UChar
)(0xd800+(UChar
)(targetUniChar
>>10));
3427 args
->offsets
[myTarget
- args
->target
] = (int32_t)(mySource
- args
->source
- (mySourceChar
<= 0xff ? 1 : 2));
3430 if(myTarget
< args
->targetLimit
){
3431 *myTarget
= (UChar
)(0xdc00+(UChar
)(targetUniChar
&0x3ff));
3433 args
->offsets
[myTarget
- args
->target
] = (int32_t)(mySource
- args
->source
- (mySourceChar
<= 0xff ? 1 : 2));
3437 args
->converter
->UCharErrorBuffer
[args
->converter
->UCharErrorBufferLength
++]=
3438 (UChar
)(0xdc00+(UChar
)(targetUniChar
&0x3ff));
3443 /* Call the callback function*/
3444 toUnicodeCallback(args
->converter
,mySourceChar
,targetUniChar
,err
);
3449 *err
=U_BUFFER_OVERFLOW_ERROR
;
3454 args
->target
= myTarget
;
3455 args
->source
= mySource
;
3457 #endif /* #if !UCONFIG_ONLY_HTML_CONVERSION */
3460 _ISO_2022_WriteSub(UConverterFromUnicodeArgs
*args
, int32_t offsetIndex
, UErrorCode
*err
) {
3461 UConverter
*cnv
= args
->converter
;
3462 UConverterDataISO2022
*myConverterData
=(UConverterDataISO2022
*) cnv
->extraInfo
;
3463 ISO2022State
*pFromU2022State
=&myConverterData
->fromU2022State
;
3468 subchar
=(char *)cnv
->subChars
;
3469 length
=cnv
->subCharLen
; /* assume length==1 for most variants */
3472 switch(myConverterData
->locale
[0]){
3477 if(pFromU2022State
->g
== 1) {
3478 /* JIS7: switch from G1 to G0 */
3479 pFromU2022State
->g
= 0;
3483 cs
= pFromU2022State
->cs
[0];
3484 if(cs
!= ASCII
&& cs
!= JISX201
) {
3485 /* not in ASCII or JIS X 0201: switch to ASCII */
3486 pFromU2022State
->cs
[0] = (int8_t)ASCII
;
3496 if(pFromU2022State
->g
!= 0) {
3497 /* not in ASCII mode: switch to ASCII */
3498 pFromU2022State
->g
= 0;
3504 if(myConverterData
->version
== 0) {
3506 if((UBool
)args
->converter
->fromUnicodeStatus
) {
3507 /* in DBCS mode: switch to SBCS */
3508 args
->converter
->fromUnicodeStatus
= 0;
3512 } else /* length == 2*/ {
3513 if(!(UBool
)args
->converter
->fromUnicodeStatus
) {
3514 /* in SBCS mode: switch to DBCS */
3515 args
->converter
->fromUnicodeStatus
= 1;
3523 /* save the subconverter's substitution string */
3524 uint8_t *currentSubChars
= myConverterData
->currentConverter
->subChars
;
3525 int8_t currentSubCharLen
= myConverterData
->currentConverter
->subCharLen
;
3527 /* set our substitution string into the subconverter */
3528 myConverterData
->currentConverter
->subChars
= (uint8_t *)subchar
;
3529 myConverterData
->currentConverter
->subCharLen
= (int8_t)length
;
3531 /* let the subconverter write the subchar, set/retrieve fromUChar32 state */
3532 args
->converter
= myConverterData
->currentConverter
;
3533 myConverterData
->currentConverter
->fromUChar32
= cnv
->fromUChar32
;
3534 ucnv_cbFromUWriteSub(args
, 0, err
);
3535 cnv
->fromUChar32
= myConverterData
->currentConverter
->fromUChar32
;
3536 args
->converter
= cnv
;
3538 /* restore the subconverter's substitution string */
3539 myConverterData
->currentConverter
->subChars
= currentSubChars
;
3540 myConverterData
->currentConverter
->subCharLen
= currentSubCharLen
;
3542 if(*err
== U_BUFFER_OVERFLOW_ERROR
) {
3543 if(myConverterData
->currentConverter
->charErrorBufferLength
> 0) {
3545 cnv
->charErrorBuffer
,
3546 myConverterData
->currentConverter
->charErrorBuffer
,
3547 myConverterData
->currentConverter
->charErrorBufferLength
);
3549 cnv
->charErrorBufferLength
= myConverterData
->currentConverter
->charErrorBufferLength
;
3550 myConverterData
->currentConverter
->charErrorBufferLength
= 0;
3558 ucnv_cbFromUWriteBytes(args
,
3559 buffer
, (int32_t)(p
- buffer
),
3564 * Structure for cloning an ISO 2022 converter into a single memory block.
3565 * ucnv_safeClone() of the converter will align the entire cloneStruct,
3566 * and then ucnv_safeClone() of the sub-converter may additionally align
3567 * currentConverter inside the cloneStruct, for which we need the deadSpace
3568 * after currentConverter.
3569 * This is because UAlignedMemory may be larger than the actually
3570 * necessary alignment size for the platform.
3571 * The other cloneStruct fields will not be moved around,
3572 * and are aligned properly with cloneStruct's alignment.
3577 UConverter currentConverter
;
3578 UAlignedMemory deadSpace
;
3579 UConverterDataISO2022 mydata
;
3584 _ISO_2022_SafeClone(
3585 const UConverter
*cnv
,
3587 int32_t *pBufferSize
,
3590 struct cloneStruct
* localClone
;
3591 UConverterDataISO2022
*cnvData
;
3594 if (*pBufferSize
== 0) { /* 'preflighting' request - set needed size into *pBufferSize */
3595 *pBufferSize
= (int32_t)sizeof(struct cloneStruct
);
3599 cnvData
= (UConverterDataISO2022
*)cnv
->extraInfo
;
3600 localClone
= (struct cloneStruct
*)stackBuffer
;
3602 /* ucnv.c/ucnv_safeClone() copied the main UConverter already */
3604 uprv_memcpy(&localClone
->mydata
, cnvData
, sizeof(UConverterDataISO2022
));
3605 localClone
->cnv
.extraInfo
= &localClone
->mydata
; /* set pointer to extra data */
3606 localClone
->cnv
.isExtraLocal
= TRUE
;
3608 /* share the subconverters */
3610 if(cnvData
->currentConverter
!= NULL
) {
3611 size
= (int32_t)(sizeof(UConverter
) + sizeof(UAlignedMemory
)); /* include size of padding */
3612 localClone
->mydata
.currentConverter
=
3613 ucnv_safeClone(cnvData
->currentConverter
,
3614 &localClone
->currentConverter
,
3616 if(U_FAILURE(*status
)) {
3621 for(i
=0; i
<UCNV_2022_MAX_CONVERTERS
; ++i
) {
3622 if(cnvData
->myConverterArray
[i
] != NULL
) {
3623 ucnv_incrementRefCount(cnvData
->myConverterArray
[i
]);
3627 return &localClone
->cnv
;
3631 _ISO_2022_GetUnicodeSet(const UConverter
*cnv
,
3632 const USetAdder
*sa
,
3633 UConverterUnicodeSet which
,
3634 UErrorCode
*pErrorCode
)
3637 UConverterDataISO2022
* cnvData
;
3639 if (U_FAILURE(*pErrorCode
)) {
3642 #ifdef U_ENABLE_GENERIC_ISO_2022
3643 if (cnv
->sharedData
== &_ISO2022Data
) {
3644 /* We use UTF-8 in this case */
3645 sa
->addRange(sa
->set
, 0, 0xd7FF);
3646 sa
->addRange(sa
->set
, 0xE000, 0x10FFFF);
3651 cnvData
= (UConverterDataISO2022
*)cnv
->extraInfo
;
3653 /* open a set and initialize it with code points that are algorithmically round-tripped */
3654 switch(cnvData
->locale
[0]){
3656 /* include JIS X 0201 which is hardcoded */
3657 sa
->add(sa
->set
, 0xa5);
3658 sa
->add(sa
->set
, 0x203e);
3659 if(jpCharsetMasks
[cnvData
->version
]&CSM(ISO8859_1
)) {
3660 /* include Latin-1 for some variants of JP */
3661 sa
->addRange(sa
->set
, 0, 0xff);
3663 /* include ASCII for JP */
3664 sa
->addRange(sa
->set
, 0, 0x7f);
3666 if(cnvData
->version
==3 || cnvData
->version
==4 || which
==UCNV_ROUNDTRIP_AND_FALLBACK_SET
) {
3668 * Do not test (jpCharsetMasks[cnvData->version]&CSM(HWKANA_7BIT))!=0
3669 * because the bit is on for all JP versions although only versions 3 & 4 (JIS7 & JIS8)
3670 * use half-width Katakana.
3671 * This is because all ISO-2022-JP variants are lenient in that they accept (in toUnicode)
3672 * half-width Katakana via the ESC ( I sequence.
3673 * However, we only emit (fromUnicode) half-width Katakana according to the
3674 * definition of each variant.
3676 * When including fallbacks,
3677 * we need to include half-width Katakana Unicode code points for all JP variants because
3678 * JIS X 0208 has hardcoded fallbacks for them (which map to full-width Katakana).
3680 /* include half-width Katakana for JP */
3681 sa
->addRange(sa
->set
, HWKANA_START
, HWKANA_END
);
3684 #if !UCONFIG_ONLY_HTML_CONVERSION
3687 /* include ASCII for CN */
3688 sa
->addRange(sa
->set
, 0, 0x7f);
3691 /* there is only one converter for KR, and it is not in the myConverterArray[] */
3692 cnvData
->currentConverter
->sharedData
->impl
->getUnicodeSet(
3693 cnvData
->currentConverter
, sa
, which
, pErrorCode
);
3694 /* the loop over myConverterArray[] will simply not find another converter */
3701 #if 0 /* Replaced by ucnv_MBCSGetFilteredUnicodeSetForUnicode() until we implement ucnv_getUnicodeSet() with reverse fallbacks. */
3702 if( (cnvData
->locale
[0]=='c' || cnvData
->locale
[0]=='z') &&
3703 cnvData
->version
==0 && i
==CNS_11643
3705 /* special handling for non-EXT ISO-2022-CN: add only code points for CNS planes 1 and 2 */
3706 ucnv_MBCSGetUnicodeSetForBytes(
3707 cnvData
->myConverterArray
[i
],
3708 sa
, UCNV_ROUNDTRIP_SET
,
3714 for (i
=0; i
<UCNV_2022_MAX_CONVERTERS
; i
++) {
3715 UConverterSetFilter filter
;
3716 if(cnvData
->myConverterArray
[i
]!=NULL
) {
3717 if(cnvData
->locale
[0]=='j' && i
==JISX208
) {
3719 * Only add code points that map to Shift-JIS codes
3720 * corresponding to JIS X 0208.
3722 filter
=UCNV_SET_FILTER_SJIS
;
3723 #if !UCONFIG_ONLY_HTML_CONVERSION
3724 } else if( (cnvData
->locale
[0]=='c' || cnvData
->locale
[0]=='z') &&
3725 cnvData
->version
==0 && i
==CNS_11643
) {
3727 * Version-specific for CN:
3728 * CN version 0 does not map CNS planes 3..7 although
3729 * they are all available in the CNS conversion table;
3730 * CN version 1 (-EXT) does map them all.
3731 * The two versions create different Unicode sets.
3733 filter
=UCNV_SET_FILTER_2022_CN
;
3734 } else if(i
==KSC5601
) {
3736 * Some of the KSC 5601 tables (convrtrs.txt has this aliases on multiple tables)
3737 * are broader than GR94.
3739 filter
=UCNV_SET_FILTER_GR94DBCS
;
3742 filter
=UCNV_SET_FILTER_NONE
;
3744 ucnv_MBCSGetFilteredUnicodeSetForUnicode(cnvData
->myConverterArray
[i
], sa
, which
, filter
, pErrorCode
);
3749 * ISO 2022 converters must not convert SO/SI/ESC despite what
3750 * sub-converters do by themselves.
3751 * Remove these characters from the set.
3753 sa
->remove(sa
->set
, 0x0e);
3754 sa
->remove(sa
->set
, 0x0f);
3755 sa
->remove(sa
->set
, 0x1b);
3757 /* ISO 2022 converters do not convert C1 controls either */
3758 sa
->removeRange(sa
->set
, 0x80, 0x9f);
3761 static const UConverterImpl _ISO2022Impl
={
3771 #ifdef U_ENABLE_GENERIC_ISO_2022
3772 T_UConverter_toUnicode_ISO_2022_OFFSETS_LOGIC
,
3773 T_UConverter_toUnicode_ISO_2022_OFFSETS_LOGIC
,
3774 ucnv_fromUnicode_UTF8
,
3775 ucnv_fromUnicode_UTF8_OFFSETS_LOGIC
,
3787 _ISO_2022_SafeClone
,
3788 _ISO_2022_GetUnicodeSet
,
3793 static const UConverterStaticData _ISO2022StaticData
={
3794 sizeof(UConverterStaticData
),
3800 3, /* max 3 bytes per UChar from UTF-8 (4 bytes from surrogate _pair_) */
3807 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */
3809 const UConverterSharedData _ISO2022Data
={
3810 sizeof(UConverterSharedData
),
3814 &_ISO2022StaticData
,
3817 0, UCNV_MBCS_TABLE_INITIALIZER
3820 /*************JP****************/
3821 static const UConverterImpl _ISO2022JPImpl
={
3831 UConverter_toUnicode_ISO_2022_JP_OFFSETS_LOGIC
,
3832 UConverter_toUnicode_ISO_2022_JP_OFFSETS_LOGIC
,
3833 UConverter_fromUnicode_ISO_2022_JP_OFFSETS_LOGIC
,
3834 UConverter_fromUnicode_ISO_2022_JP_OFFSETS_LOGIC
,
3840 _ISO_2022_SafeClone
,
3841 _ISO_2022_GetUnicodeSet
,
3846 static const UConverterStaticData _ISO2022JPStaticData
={
3847 sizeof(UConverterStaticData
),
3853 6, /* max 6 bytes per UChar: 4-byte escape sequence + DBCS */
3860 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */
3865 const UConverterSharedData _ISO2022JPData
={
3866 sizeof(UConverterSharedData
),
3870 &_ISO2022JPStaticData
,
3873 0, UCNV_MBCS_TABLE_INITIALIZER
3878 #if !UCONFIG_ONLY_HTML_CONVERSION
3879 /************* KR ***************/
3880 static const UConverterImpl _ISO2022KRImpl
={
3890 UConverter_toUnicode_ISO_2022_KR_OFFSETS_LOGIC
,
3891 UConverter_toUnicode_ISO_2022_KR_OFFSETS_LOGIC
,
3892 UConverter_fromUnicode_ISO_2022_KR_OFFSETS_LOGIC
,
3893 UConverter_fromUnicode_ISO_2022_KR_OFFSETS_LOGIC
,
3899 _ISO_2022_SafeClone
,
3900 _ISO_2022_GetUnicodeSet
,
3905 static const UConverterStaticData _ISO2022KRStaticData
={
3906 sizeof(UConverterStaticData
),
3912 3, /* max 3 bytes per UChar: SO+DBCS */
3919 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */
3924 const UConverterSharedData _ISO2022KRData
={
3925 sizeof(UConverterSharedData
),
3929 &_ISO2022KRStaticData
,
3932 0, UCNV_MBCS_TABLE_INITIALIZER
3937 /*************** CN ***************/
3938 static const UConverterImpl _ISO2022CNImpl
={
3949 UConverter_toUnicode_ISO_2022_CN_OFFSETS_LOGIC
,
3950 UConverter_toUnicode_ISO_2022_CN_OFFSETS_LOGIC
,
3951 UConverter_fromUnicode_ISO_2022_CN_OFFSETS_LOGIC
,
3952 UConverter_fromUnicode_ISO_2022_CN_OFFSETS_LOGIC
,
3958 _ISO_2022_SafeClone
,
3959 _ISO_2022_GetUnicodeSet
,
3964 static const UConverterStaticData _ISO2022CNStaticData
={
3965 sizeof(UConverterStaticData
),
3971 8, /* max 8 bytes per UChar: 4-byte CNS designator + 2 bytes for SS2/SS3 + DBCS */
3978 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */
3983 const UConverterSharedData _ISO2022CNData
={
3984 sizeof(UConverterSharedData
),
3988 &_ISO2022CNStaticData
,
3991 0, UCNV_MBCS_TABLE_INITIALIZER
3995 #endif /* #if !UCONFIG_ONLY_HTML_CONVERSION */
3997 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */