2 **********************************************************************
3 * Copyright (C) 2000-2016, 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 */
2164 /* automatically reset to single-byte mode */
2165 if((StateEnum
)pToU2022State
->cs
[0] != ASCII
&& (StateEnum
)pToU2022State
->cs
[0] != JISX201
) {
2166 pToU2022State
->cs
[0] = (int8_t)ASCII
;
2168 pToU2022State
->cs
[2] = 0;
2169 pToU2022State
->g
= 0;
2172 /* convert one or two bytes */
2173 myData
->isEmptySegment
= FALSE
;
2174 cs
= (StateEnum
)pToU2022State
->cs
[pToU2022State
->g
];
2175 if( (uint8_t)(mySourceChar
- 0xa1) <= (0xdf - 0xa1) && myData
->version
==4 &&
2178 /* 8-bit halfwidth katakana in any single-byte mode for JIS8 */
2179 targetUniChar
= mySourceChar
+ (HWKANA_START
- 0xa1);
2181 /* return from a single-shift state to the previous one */
2182 if(pToU2022State
->g
>= 2) {
2183 pToU2022State
->g
=pToU2022State
->prevG
;
2187 if(mySourceChar
<= 0x7f) {
2188 targetUniChar
= mySourceChar
;
2192 if(mySourceChar
<= 0x7f) {
2193 targetUniChar
= mySourceChar
+ 0x80;
2195 /* return from a single-shift state to the previous one */
2196 pToU2022State
->g
=pToU2022State
->prevG
;
2199 if(mySourceChar
<= 0x7f) {
2200 /* convert mySourceChar+0x80 to use a normal 8-bit table */
2202 _MBCS_SINGLE_SIMPLE_GET_NEXT_BMP(
2203 myData
->myConverterArray
[cs
],
2204 mySourceChar
+ 0x80);
2206 /* return from a single-shift state to the previous one */
2207 pToU2022State
->g
=pToU2022State
->prevG
;
2210 if(mySourceChar
<= 0x7f) {
2211 targetUniChar
= jisx201ToU(mySourceChar
);
2215 if((uint8_t)(mySourceChar
- 0x21) <= (0x5f - 0x21)) {
2216 /* 7-bit halfwidth Katakana */
2217 targetUniChar
= mySourceChar
+ (HWKANA_START
- 0x21);
2222 if(mySource
< mySourceLimit
) {
2223 int leadIsOk
, trailIsOk
;
2226 trailByte
= (uint8_t)*mySource
;
2228 * Ticket 5691: consistent illegal sequences:
2229 * - We include at least the first byte in the illegal sequence.
2230 * - If any of the non-initial bytes could be the start of a character,
2231 * we stop the illegal sequence before the first one of those.
2233 * In ISO-2022 DBCS, if the second byte is in the 21..7e range or is
2234 * an ESC/SO/SI, we report only the first byte as the illegal sequence.
2235 * Otherwise we convert or report the pair of bytes.
2237 leadIsOk
= (uint8_t)(mySourceChar
- 0x21) <= (0x7e - 0x21);
2238 trailIsOk
= (uint8_t)(trailByte
- 0x21) <= (0x7e - 0x21);
2239 if (leadIsOk
&& trailIsOk
) {
2241 tmpSourceChar
= (mySourceChar
<< 8) | trailByte
;
2243 _2022ToSJIS((uint8_t)mySourceChar
, trailByte
, tempBuf
);
2244 mySourceChar
= tmpSourceChar
;
2246 /* Copy before we modify tmpSourceChar so toUnicodeCallback() sees the correct bytes. */
2247 mySourceChar
= tmpSourceChar
;
2248 if (cs
== KSC5601
) {
2249 tmpSourceChar
+= 0x8080; /* = _2022ToGR94DBCS(tmpSourceChar) */
2251 tempBuf
[0] = (char)(tmpSourceChar
>> 8);
2252 tempBuf
[1] = (char)(tmpSourceChar
);
2254 targetUniChar
= ucnv_MBCSSimpleGetNextUChar(myData
->myConverterArray
[cs
], tempBuf
, 2, FALSE
);
2255 } else if (!(trailIsOk
|| IS_2022_CONTROL(trailByte
))) {
2256 /* report a pair of illegal bytes if the second byte is not a DBCS starter */
2258 /* add another bit so that the code below writes 2 bytes in case of error */
2259 mySourceChar
= 0x10000 | (mySourceChar
<< 8) | trailByte
;
2262 args
->converter
->toUBytes
[0] = (uint8_t)mySourceChar
;
2263 args
->converter
->toULength
= 1;
2266 } /* End of inner switch */
2268 } /* End of outer switch */
2269 if(targetUniChar
< (missingCharMarker
-1/*0xfffe*/)){
2271 args
->offsets
[myTarget
- args
->target
] = (int32_t)(mySource
- args
->source
- (mySourceChar
<= 0xff ? 1 : 2));
2273 *(myTarget
++)=(UChar
)targetUniChar
;
2275 else if(targetUniChar
> missingCharMarker
){
2276 /* disassemble the surrogate pair and write to output*/
2277 targetUniChar
-=0x0010000;
2278 *myTarget
= (UChar
)(0xd800+(UChar
)(targetUniChar
>>10));
2280 args
->offsets
[myTarget
- args
->target
] = (int32_t)(mySource
- args
->source
- (mySourceChar
<= 0xff ? 1 : 2));
2283 if(myTarget
< args
->targetLimit
){
2284 *myTarget
= (UChar
)(0xdc00+(UChar
)(targetUniChar
&0x3ff));
2286 args
->offsets
[myTarget
- args
->target
] = (int32_t)(mySource
- args
->source
- (mySourceChar
<= 0xff ? 1 : 2));
2290 args
->converter
->UCharErrorBuffer
[args
->converter
->UCharErrorBufferLength
++]=
2291 (UChar
)(0xdc00+(UChar
)(targetUniChar
&0x3ff));
2296 /* Call the callback function*/
2297 toUnicodeCallback(args
->converter
,mySourceChar
,targetUniChar
,err
);
2301 else{ /* goes with "if(myTarget < args->targetLimit)" way up near top of function */
2302 *err
=U_BUFFER_OVERFLOW_ERROR
;
2307 args
->target
= myTarget
;
2308 args
->source
= mySource
;
2312 #if !UCONFIG_ONLY_HTML_CONVERSION
2313 /***************************************************************
2314 * Rules for ISO-2022-KR encoding
2315 * i) The KSC5601 designator sequence should appear only once in a file,
2316 * at the begining of a line before any KSC5601 characters. This usually
2317 * means that it appears by itself on the first line of the file
2318 * ii) There are only 2 shifting sequences SO to shift into double byte mode
2319 * and SI to shift into single byte mode
2322 UConverter_fromUnicode_ISO_2022_KR_OFFSETS_LOGIC_IBM(UConverterFromUnicodeArgs
* args
, UErrorCode
* err
){
2324 UConverter
* saveConv
= args
->converter
;
2325 UConverterDataISO2022
*myConverterData
=(UConverterDataISO2022
*)saveConv
->extraInfo
;
2326 args
->converter
=myConverterData
->currentConverter
;
2328 myConverterData
->currentConverter
->fromUChar32
= saveConv
->fromUChar32
;
2329 ucnv_MBCSFromUnicodeWithOffsets(args
,err
);
2330 saveConv
->fromUChar32
= myConverterData
->currentConverter
->fromUChar32
;
2332 if(*err
== U_BUFFER_OVERFLOW_ERROR
) {
2333 if(myConverterData
->currentConverter
->charErrorBufferLength
> 0) {
2335 saveConv
->charErrorBuffer
,
2336 myConverterData
->currentConverter
->charErrorBuffer
,
2337 myConverterData
->currentConverter
->charErrorBufferLength
);
2339 saveConv
->charErrorBufferLength
= myConverterData
->currentConverter
->charErrorBufferLength
;
2340 myConverterData
->currentConverter
->charErrorBufferLength
= 0;
2342 args
->converter
=saveConv
;
2346 UConverter_fromUnicode_ISO_2022_KR_OFFSETS_LOGIC(UConverterFromUnicodeArgs
* args
, UErrorCode
* err
){
2348 const UChar
*source
= args
->source
;
2349 const UChar
*sourceLimit
= args
->sourceLimit
;
2350 unsigned char *target
= (unsigned char *) args
->target
;
2351 unsigned char *targetLimit
= (unsigned char *) args
->targetLimit
;
2352 int32_t* offsets
= args
->offsets
;
2353 uint32_t targetByteUnit
= 0x0000;
2354 UChar32 sourceChar
= 0x0000;
2355 UBool isTargetByteDBCS
;
2356 UBool oldIsTargetByteDBCS
;
2357 UConverterDataISO2022
*converterData
;
2358 UConverterSharedData
* sharedData
;
2362 converterData
=(UConverterDataISO2022
*)args
->converter
->extraInfo
;
2363 /* if the version is 1 then the user is requesting
2364 * conversion with ibm-25546 pass the arguments to
2365 * MBCS converter and return
2367 if(converterData
->version
==1){
2368 UConverter_fromUnicode_ISO_2022_KR_OFFSETS_LOGIC_IBM(args
,err
);
2372 /* initialize data */
2373 sharedData
= converterData
->currentConverter
->sharedData
;
2374 useFallback
= args
->converter
->useFallback
;
2375 isTargetByteDBCS
=(UBool
)args
->converter
->fromUnicodeStatus
;
2376 oldIsTargetByteDBCS
= isTargetByteDBCS
;
2378 isTargetByteDBCS
= (UBool
) args
->converter
->fromUnicodeStatus
;
2379 if((sourceChar
= args
->converter
->fromUChar32
)!=0 && target
<targetLimit
) {
2382 while(source
< sourceLimit
){
2384 targetByteUnit
= missingCharMarker
;
2386 if(target
< (unsigned char*) args
->targetLimit
){
2387 sourceChar
= *source
++;
2389 /* do not convert SO/SI/ESC */
2390 if(IS_2022_CONTROL(sourceChar
)) {
2391 /* callback(illegal) */
2392 *err
=U_ILLEGAL_CHAR_FOUND
;
2393 args
->converter
->fromUChar32
=sourceChar
;
2397 length
= MBCS_FROM_UCHAR32_ISO2022(sharedData
,sourceChar
,&targetByteUnit
,useFallback
,MBCS_OUTPUT_2
);
2399 length
= -length
; /* fallback */
2401 /* only DBCS or SBCS characters are expected*/
2402 /* DB characters with high bit set to 1 are expected */
2403 if( length
> 2 || length
==0 ||
2404 (length
== 1 && targetByteUnit
> 0x7f) ||
2406 ((uint16_t)(targetByteUnit
- 0xa1a1) > (0xfefe - 0xa1a1) ||
2407 (uint8_t)(targetByteUnit
- 0xa1) > (0xfe - 0xa1)))
2409 targetByteUnit
=missingCharMarker
;
2411 if (targetByteUnit
!= missingCharMarker
){
2413 oldIsTargetByteDBCS
= isTargetByteDBCS
;
2414 isTargetByteDBCS
= (UBool
)(targetByteUnit
>0x00FF);
2415 /* append the shift sequence */
2416 if (oldIsTargetByteDBCS
!= isTargetByteDBCS
){
2418 if (isTargetByteDBCS
)
2419 *target
++ = UCNV_SO
;
2421 *target
++ = UCNV_SI
;
2423 *(offsets
++) = (int32_t)(source
- args
->source
-1);
2425 /* write the targetUniChar to target */
2426 if(targetByteUnit
<= 0x00FF){
2427 if( target
< targetLimit
){
2428 *(target
++) = (unsigned char) targetByteUnit
;
2430 *(offsets
++) = (int32_t)(source
- args
->source
-1);
2434 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] = (unsigned char) (targetByteUnit
);
2435 *err
= U_BUFFER_OVERFLOW_ERROR
;
2438 if(target
< targetLimit
){
2439 *(target
++) =(unsigned char) ((targetByteUnit
>>8) -0x80);
2441 *(offsets
++) = (int32_t)(source
- args
->source
-1);
2443 if(target
< targetLimit
){
2444 *(target
++) =(unsigned char) (targetByteUnit
-0x80);
2446 *(offsets
++) = (int32_t)(source
- args
->source
-1);
2449 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] = (unsigned char) (targetByteUnit
-0x80);
2450 *err
= U_BUFFER_OVERFLOW_ERROR
;
2453 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] = (unsigned char) ((targetByteUnit
>>8) -0x80);
2454 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] = (unsigned char) (targetByteUnit
-0x80);
2455 *err
= U_BUFFER_OVERFLOW_ERROR
;
2461 /* oops.. the code point is unassingned
2462 * set the error and reason
2465 /*check if the char is a First surrogate*/
2466 if(U16_IS_SURROGATE(sourceChar
)) {
2467 if(U16_IS_SURROGATE_LEAD(sourceChar
)) {
2469 /*look ahead to find the trail surrogate*/
2470 if(source
< sourceLimit
) {
2471 /* test the following code unit */
2472 UChar trail
=(UChar
) *source
;
2473 if(U16_IS_TRAIL(trail
)) {
2475 sourceChar
=U16_GET_SUPPLEMENTARY(sourceChar
, trail
);
2476 *err
= U_INVALID_CHAR_FOUND
;
2477 /* convert this surrogate code point */
2478 /* exit this condition tree */
2480 /* this is an unmatched lead code unit (1st surrogate) */
2481 /* callback(illegal) */
2482 *err
=U_ILLEGAL_CHAR_FOUND
;
2486 *err
= U_ZERO_ERROR
;
2489 /* this is an unmatched trail code unit (2nd surrogate) */
2490 /* callback(illegal) */
2491 *err
=U_ILLEGAL_CHAR_FOUND
;
2494 /* callback(unassigned) for a BMP code point */
2495 *err
= U_INVALID_CHAR_FOUND
;
2498 args
->converter
->fromUChar32
=sourceChar
;
2501 } /* end if(myTargetIndex<myTargetLength) */
2503 *err
=U_BUFFER_OVERFLOW_ERROR
;
2507 }/* end while(mySourceIndex<mySourceLength) */
2510 * the end of the input stream and detection of truncated input
2511 * are handled by the framework, but for ISO-2022-KR conversion
2512 * we need to be in ASCII mode at the very end
2517 * end of input and no truncated input
2519 if( U_SUCCESS(*err
) &&
2521 args
->flush
&& source
>=sourceLimit
&& args
->converter
->fromUChar32
==0
2523 int32_t sourceIndex
;
2525 /* we are switching to ASCII */
2526 isTargetByteDBCS
=FALSE
;
2528 /* get the source index of the last input character */
2530 * TODO this would be simpler and more reliable if we used a pair
2531 * of sourceIndex/prevSourceIndex like in ucnvmbcs.c
2532 * so that we could simply use the prevSourceIndex here;
2533 * this code gives an incorrect result for the rare case of an unmatched
2534 * trail surrogate that is alone in the last buffer of the text stream
2536 sourceIndex
=(int32_t)(source
-args
->source
);
2539 if( U16_IS_TRAIL(args
->source
[sourceIndex
]) &&
2540 (sourceIndex
==0 || U16_IS_LEAD(args
->source
[sourceIndex
-1]))
2551 &target
, (const char *)targetLimit
,
2552 &offsets
, sourceIndex
,
2556 /*save the state and return */
2557 args
->source
= source
;
2558 args
->target
= (char*)target
;
2559 args
->converter
->fromUnicodeStatus
= (uint32_t)isTargetByteDBCS
;
2562 /************************ To Unicode ***************************************/
2565 UConverter_toUnicode_ISO_2022_KR_OFFSETS_LOGIC_IBM(UConverterToUnicodeArgs
*args
,
2567 char const* sourceStart
;
2568 UConverterDataISO2022
* myData
=(UConverterDataISO2022
*)(args
->converter
->extraInfo
);
2570 UConverterToUnicodeArgs subArgs
;
2571 int32_t minArgsSize
;
2573 /* set up the subconverter arguments */
2574 if(args
->size
<sizeof(UConverterToUnicodeArgs
)) {
2575 minArgsSize
= args
->size
;
2577 minArgsSize
= (int32_t)sizeof(UConverterToUnicodeArgs
);
2580 uprv_memcpy(&subArgs
, args
, minArgsSize
);
2581 subArgs
.size
= (uint16_t)minArgsSize
;
2582 subArgs
.converter
= myData
->currentConverter
;
2584 /* remember the original start of the input for offsets */
2585 sourceStart
= args
->source
;
2587 if(myData
->key
!= 0) {
2588 /* continue with a partial escape sequence */
2592 while(U_SUCCESS(*err
) && args
->source
< args
->sourceLimit
) {
2593 /*Find the end of the buffer e.g : Next Escape Seq | end of Buffer*/
2594 subArgs
.source
= args
->source
;
2595 subArgs
.sourceLimit
= getEndOfBuffer_2022(&(args
->source
), args
->sourceLimit
, args
->flush
);
2596 if(subArgs
.source
!= subArgs
.sourceLimit
) {
2598 * get the current partial byte sequence
2600 * it needs to be moved between the public and the subconverter
2601 * so that the conversion framework, which only sees the public
2602 * converter, can handle truncated and illegal input etc.
2604 if(args
->converter
->toULength
> 0) {
2605 uprv_memcpy(subArgs
.converter
->toUBytes
, args
->converter
->toUBytes
, args
->converter
->toULength
);
2607 subArgs
.converter
->toULength
= args
->converter
->toULength
;
2610 * Convert up to the end of the input, or to before the next escape character.
2611 * Does not handle conversion extensions because the preToU[] state etc.
2614 ucnv_MBCSToUnicodeWithOffsets(&subArgs
, err
);
2616 if(args
->offsets
!= NULL
&& sourceStart
!= args
->source
) {
2617 /* update offsets to base them on the actual start of the input */
2618 int32_t *offsets
= args
->offsets
;
2619 UChar
*target
= args
->target
;
2620 int32_t delta
= (int32_t)(args
->source
- sourceStart
);
2621 while(target
< subArgs
.target
) {
2629 args
->source
= subArgs
.source
;
2630 args
->target
= subArgs
.target
;
2631 args
->offsets
= subArgs
.offsets
;
2633 /* copy input/error/overflow buffers */
2634 if(subArgs
.converter
->toULength
> 0) {
2635 uprv_memcpy(args
->converter
->toUBytes
, subArgs
.converter
->toUBytes
, subArgs
.converter
->toULength
);
2637 args
->converter
->toULength
= subArgs
.converter
->toULength
;
2639 if(*err
== U_BUFFER_OVERFLOW_ERROR
) {
2640 if(subArgs
.converter
->UCharErrorBufferLength
> 0) {
2641 uprv_memcpy(args
->converter
->UCharErrorBuffer
, subArgs
.converter
->UCharErrorBuffer
,
2642 subArgs
.converter
->UCharErrorBufferLength
);
2644 args
->converter
->UCharErrorBufferLength
=subArgs
.converter
->UCharErrorBufferLength
;
2645 subArgs
.converter
->UCharErrorBufferLength
= 0;
2649 if (U_FAILURE(*err
) || (args
->source
== args
->sourceLimit
)) {
2654 changeState_2022(args
->converter
,
2663 UConverter_toUnicode_ISO_2022_KR_OFFSETS_LOGIC(UConverterToUnicodeArgs
*args
,
2666 const char *mySource
= ( char *) args
->source
;
2667 UChar
*myTarget
= args
->target
;
2668 const char *mySourceLimit
= args
->sourceLimit
;
2669 UChar32 targetUniChar
= 0x0000;
2670 UChar mySourceChar
= 0x0000;
2671 UConverterDataISO2022
* myData
;
2672 UConverterSharedData
* sharedData
;
2675 myData
=(UConverterDataISO2022
*)(args
->converter
->extraInfo
);
2676 if(myData
->version
==1){
2677 UConverter_toUnicode_ISO_2022_KR_OFFSETS_LOGIC_IBM(args
,err
);
2681 /* initialize state */
2682 sharedData
= myData
->currentConverter
->sharedData
;
2683 useFallback
= args
->converter
->useFallback
;
2685 if(myData
->key
!= 0) {
2686 /* continue with a partial escape sequence */
2688 } else if(args
->converter
->toULength
== 1 && mySource
< mySourceLimit
&& myTarget
< args
->targetLimit
) {
2689 /* continue with a partial double-byte character */
2690 mySourceChar
= args
->converter
->toUBytes
[0];
2691 args
->converter
->toULength
= 0;
2695 while(mySource
< mySourceLimit
){
2697 if(myTarget
< args
->targetLimit
){
2699 mySourceChar
= (unsigned char) *mySource
++;
2701 if(mySourceChar
==UCNV_SI
){
2702 myData
->toU2022State
.g
= 0;
2703 if (myData
->isEmptySegment
) {
2704 myData
->isEmptySegment
= FALSE
; /* we are handling it, reset to avoid future spurious errors */
2705 *err
= U_ILLEGAL_ESCAPE_SEQUENCE
;
2706 args
->converter
->toUCallbackReason
= UCNV_IRREGULAR
;
2707 args
->converter
->toUBytes
[0] = (uint8_t)mySourceChar
;
2708 args
->converter
->toULength
= 1;
2709 args
->target
= myTarget
;
2710 args
->source
= mySource
;
2713 /*consume the source */
2715 }else if(mySourceChar
==UCNV_SO
){
2716 myData
->toU2022State
.g
= 1;
2717 myData
->isEmptySegment
= TRUE
; /* Begin a new segment, empty so far */
2718 /*consume the source */
2720 }else if(mySourceChar
==ESC_2022
){
2723 myData
->isEmptySegment
= FALSE
; /* Any invalid ESC sequences will be detected separately, so just reset this */
2724 changeState_2022(args
->converter
,&(mySource
),
2725 mySourceLimit
, ISO_2022_KR
, err
);
2726 if(U_FAILURE(*err
)){
2727 args
->target
= myTarget
;
2728 args
->source
= mySource
;
2734 myData
->isEmptySegment
= FALSE
; /* Any invalid char errors will be detected separately, so just reset this */
2735 if(myData
->toU2022State
.g
== 1) {
2736 if(mySource
< mySourceLimit
) {
2737 int leadIsOk
, trailIsOk
;
2740 targetUniChar
= missingCharMarker
;
2741 trailByte
= (uint8_t)*mySource
;
2743 * Ticket 5691: consistent illegal sequences:
2744 * - We include at least the first byte in the illegal sequence.
2745 * - If any of the non-initial bytes could be the start of a character,
2746 * we stop the illegal sequence before the first one of those.
2748 * In ISO-2022 DBCS, if the second byte is in the 21..7e range or is
2749 * an ESC/SO/SI, we report only the first byte as the illegal sequence.
2750 * Otherwise we convert or report the pair of bytes.
2752 leadIsOk
= (uint8_t)(mySourceChar
- 0x21) <= (0x7e - 0x21);
2753 trailIsOk
= (uint8_t)(trailByte
- 0x21) <= (0x7e - 0x21);
2754 if (leadIsOk
&& trailIsOk
) {
2756 tempBuf
[0] = (char)(mySourceChar
+ 0x80);
2757 tempBuf
[1] = (char)(trailByte
+ 0x80);
2758 targetUniChar
= ucnv_MBCSSimpleGetNextUChar(sharedData
, tempBuf
, 2, useFallback
);
2759 mySourceChar
= (mySourceChar
<< 8) | trailByte
;
2760 } else if (!(trailIsOk
|| IS_2022_CONTROL(trailByte
))) {
2761 /* report a pair of illegal bytes if the second byte is not a DBCS starter */
2763 /* add another bit so that the code below writes 2 bytes in case of error */
2764 mySourceChar
= 0x10000 | (mySourceChar
<< 8) | trailByte
;
2767 args
->converter
->toUBytes
[0] = (uint8_t)mySourceChar
;
2768 args
->converter
->toULength
= 1;
2772 else if(mySourceChar
<= 0x7f) {
2773 targetUniChar
= ucnv_MBCSSimpleGetNextUChar(sharedData
, mySource
- 1, 1, useFallback
);
2775 targetUniChar
= 0xffff;
2777 if(targetUniChar
< 0xfffe){
2779 args
->offsets
[myTarget
- args
->target
] = (int32_t)(mySource
- args
->source
- (mySourceChar
<= 0xff ? 1 : 2));
2781 *(myTarget
++)=(UChar
)targetUniChar
;
2784 /* Call the callback function*/
2785 toUnicodeCallback(args
->converter
,mySourceChar
,targetUniChar
,err
);
2790 *err
=U_BUFFER_OVERFLOW_ERROR
;
2794 args
->target
= myTarget
;
2795 args
->source
= mySource
;
2798 /*************************** END ISO2022-KR *********************************/
2800 /*************************** ISO-2022-CN *********************************
2802 * Rules for ISO-2022-CN Encoding:
2803 * i) The designator sequence must appear once on a line before any instance
2804 * of character set it designates.
2805 * ii) If two lines contain characters from the same character set, both lines
2806 * must include the designator sequence.
2807 * iii) Once the designator sequence is known, a shifting sequence has to be found
2808 * to invoke the shifting
2809 * iv) All lines start in ASCII and end in ASCII.
2810 * v) Four shifting sequences are employed for this purpose:
2812 * Sequcence ASCII Eq Charsets
2813 * ---------- ------- ---------
2815 * SO <SO> CNS-11643-1992 Plane 1, GB2312, ISO-IR-165
2816 * SS2 <ESC>N CNS-11643-1992 Plane 2
2817 * SS3 <ESC>O CNS-11643-1992 Planes 3-7
2820 * SOdesignator : ESC "$" ")" finalchar_for_SO
2821 * SS2designator : ESC "$" "*" finalchar_for_SS2
2822 * SS3designator : ESC "$" "+" finalchar_for_SS3
2824 * ESC $ ) A Indicates the bytes following SO are Chinese
2825 * characters as defined in GB 2312-80, until
2826 * another SOdesignation appears
2829 * ESC $ ) E Indicates the bytes following SO are as defined
2830 * in ISO-IR-165 (for details, see section 2.1),
2831 * until another SOdesignation appears
2833 * ESC $ ) G Indicates the bytes following SO are as defined
2834 * in CNS 11643-plane-1, until another
2835 * SOdesignation appears
2837 * ESC $ * H Indicates the two bytes immediately following
2838 * SS2 is a Chinese character as defined in CNS
2839 * 11643-plane-2, until another SS2designation
2841 * (Meaning <ESC>N must preceed every 2 byte
2844 * ESC $ + I Indicates the immediate two bytes following SS3
2845 * is a Chinese character as defined in CNS
2846 * 11643-plane-3, until another SS3designation
2848 * (Meaning <ESC>O must preceed every 2 byte
2851 * ESC $ + J Indicates the immediate two bytes following SS3
2852 * is a Chinese character as defined in CNS
2853 * 11643-plane-4, until another SS3designation
2855 * (In English: <ESC>O must preceed every 2 byte
2858 * ESC $ + K Indicates the immediate two bytes following SS3
2859 * is a Chinese character as defined in CNS
2860 * 11643-plane-5, until another SS3designation
2863 * ESC $ + L Indicates the immediate two bytes following SS3
2864 * is a Chinese character as defined in CNS
2865 * 11643-plane-6, until another SS3designation
2868 * ESC $ + M Indicates the immediate two bytes following SS3
2869 * is a Chinese character as defined in CNS
2870 * 11643-plane-7, until another SS3designation
2873 * As in ISO-2022-CN, each line starts in ASCII, and ends in ASCII, and
2874 * has its own designation information before any Chinese characters
2879 /* The following are defined this way to make the strings truly readonly */
2880 static const char GB_2312_80_STR
[] = "\x1B\x24\x29\x41";
2881 static const char ISO_IR_165_STR
[] = "\x1B\x24\x29\x45";
2882 static const char CNS_11643_1992_Plane_1_STR
[] = "\x1B\x24\x29\x47";
2883 static const char CNS_11643_1992_Plane_2_STR
[] = "\x1B\x24\x2A\x48";
2884 static const char CNS_11643_1992_Plane_3_STR
[] = "\x1B\x24\x2B\x49";
2885 static const char CNS_11643_1992_Plane_4_STR
[] = "\x1B\x24\x2B\x4A";
2886 static const char CNS_11643_1992_Plane_5_STR
[] = "\x1B\x24\x2B\x4B";
2887 static const char CNS_11643_1992_Plane_6_STR
[] = "\x1B\x24\x2B\x4C";
2888 static const char CNS_11643_1992_Plane_7_STR
[] = "\x1B\x24\x2B\x4D";
2890 /********************** ISO2022-CN Data **************************/
2891 static const char* const escSeqCharsCN
[10] ={
2892 SHIFT_IN_STR
, /* 0 ASCII */
2893 GB_2312_80_STR
, /* 1 GB2312_1 */
2894 ISO_IR_165_STR
, /* 2 ISO_IR_165 */
2895 CNS_11643_1992_Plane_1_STR
,
2896 CNS_11643_1992_Plane_2_STR
,
2897 CNS_11643_1992_Plane_3_STR
,
2898 CNS_11643_1992_Plane_4_STR
,
2899 CNS_11643_1992_Plane_5_STR
,
2900 CNS_11643_1992_Plane_6_STR
,
2901 CNS_11643_1992_Plane_7_STR
2905 UConverter_fromUnicode_ISO_2022_CN_OFFSETS_LOGIC(UConverterFromUnicodeArgs
* args
, UErrorCode
* err
){
2906 UConverter
*cnv
= args
->converter
;
2907 UConverterDataISO2022
*converterData
;
2908 ISO2022State
*pFromU2022State
;
2909 uint8_t *target
= (uint8_t *) args
->target
;
2910 const uint8_t *targetLimit
= (const uint8_t *) args
->targetLimit
;
2911 const UChar
* source
= args
->source
;
2912 const UChar
* sourceLimit
= args
->sourceLimit
;
2913 int32_t* offsets
= args
->offsets
;
2918 int32_t choiceCount
;
2919 uint32_t targetValue
= 0;
2922 /* set up the state */
2923 converterData
= (UConverterDataISO2022
*)cnv
->extraInfo
;
2924 pFromU2022State
= &converterData
->fromU2022State
;
2928 /* check if the last codepoint of previous buffer was a lead surrogate*/
2929 if((sourceChar
= cnv
->fromUChar32
)!=0 && target
< targetLimit
) {
2933 while( source
< sourceLimit
){
2934 if(target
< targetLimit
){
2936 sourceChar
= *(source
++);
2937 /*check if the char is a First surrogate*/
2938 if(U16_IS_SURROGATE(sourceChar
)) {
2939 if(U16_IS_SURROGATE_LEAD(sourceChar
)) {
2941 /*look ahead to find the trail surrogate*/
2942 if(source
< sourceLimit
) {
2943 /* test the following code unit */
2944 UChar trail
=(UChar
) *source
;
2945 if(U16_IS_TRAIL(trail
)) {
2947 sourceChar
=U16_GET_SUPPLEMENTARY(sourceChar
, trail
);
2948 cnv
->fromUChar32
=0x00;
2949 /* convert this supplementary code point */
2950 /* exit this condition tree */
2952 /* this is an unmatched lead code unit (1st surrogate) */
2953 /* callback(illegal) */
2954 *err
=U_ILLEGAL_CHAR_FOUND
;
2955 cnv
->fromUChar32
=sourceChar
;
2960 cnv
->fromUChar32
=sourceChar
;
2964 /* this is an unmatched trail code unit (2nd surrogate) */
2965 /* callback(illegal) */
2966 *err
=U_ILLEGAL_CHAR_FOUND
;
2967 cnv
->fromUChar32
=sourceChar
;
2972 /* do the conversion */
2973 if(sourceChar
<= 0x007f ){
2974 /* do not convert SO/SI/ESC */
2975 if(IS_2022_CONTROL(sourceChar
)) {
2976 /* callback(illegal) */
2977 *err
=U_ILLEGAL_CHAR_FOUND
;
2978 cnv
->fromUChar32
=sourceChar
;
2983 if(pFromU2022State
->g
== 0) {
2984 buffer
[0] = (char)sourceChar
;
2987 buffer
[0] = UCNV_SI
;
2988 buffer
[1] = (char)sourceChar
;
2990 pFromU2022State
->g
= 0;
2993 if(sourceChar
== CR
|| sourceChar
== LF
) {
2994 /* reset the state at the end of a line */
2995 uprv_memset(pFromU2022State
, 0, sizeof(ISO2022State
));
3000 /* convert U+0080..U+10ffff */
3004 if(choiceCount
== 0) {
3005 /* try the current SO/G1 converter first */
3006 choices
[0] = pFromU2022State
->cs
[1];
3008 /* default to GB2312_1 if none is designated yet */
3009 if(choices
[0] == 0) {
3010 choices
[0] = GB2312_1
;
3013 if(converterData
->version
== 0) {
3016 /* try the other SO/G1 converter; a CNS_11643_1 lookup may result in any plane */
3017 if(choices
[0] == GB2312_1
) {
3018 choices
[1] = (int8_t)CNS_11643_1
;
3020 choices
[1] = (int8_t)GB2312_1
;
3024 } else if (converterData
->version
== 1) {
3025 /* ISO-2022-CN-EXT */
3027 /* try one of the other converters */
3028 switch(choices
[0]) {
3030 choices
[1] = (int8_t)CNS_11643_1
;
3031 choices
[2] = (int8_t)ISO_IR_165
;
3034 choices
[1] = (int8_t)GB2312_1
;
3035 choices
[2] = (int8_t)CNS_11643_1
;
3037 default: /* CNS_11643_x */
3038 choices
[1] = (int8_t)GB2312_1
;
3039 choices
[2] = (int8_t)ISO_IR_165
;
3045 choices
[0] = (int8_t)CNS_11643_1
;
3046 choices
[1] = (int8_t)GB2312_1
;
3052 * len==0: no mapping found yet
3053 * len<0: found a fallback result: continue looking for a roundtrip but no further fallbacks
3054 * len>0: found a roundtrip result, done
3058 * We will turn off useFallback after finding a fallback,
3059 * but we still get fallbacks from PUA code points as usual.
3060 * Therefore, we will also need to check that we don't overwrite
3061 * an early fallback with a later one.
3063 useFallback
= cnv
->useFallback
;
3065 for(i
= 0; i
< choiceCount
&& len
<= 0; ++i
) {
3066 int8_t cs0
= choices
[i
];
3070 if(cs0
>= CNS_11643_0
) {
3071 len2
= MBCS_FROM_UCHAR32_ISO2022(
3072 converterData
->myConverterArray
[CNS_11643
],
3077 if(len2
== 3 || (len2
== -3 && len
== 0)) {
3078 targetValue
= value
;
3079 cs
= (int8_t)(CNS_11643_0
+ (value
>> 16) - 0x80);
3084 useFallback
= FALSE
;
3086 if(cs
== CNS_11643_1
) {
3088 } else if(cs
== CNS_11643_2
) {
3090 } else /* plane 3..7 */ if(converterData
->version
== 1) {
3093 /* ISO-2022-CN (without -EXT) does not support plane 3..7 */
3098 /* GB2312_1 or ISO-IR-165 */
3099 U_ASSERT(cs0
<UCNV_2022_MAX_CONVERTERS
);
3100 len2
= MBCS_FROM_UCHAR32_ISO2022(
3101 converterData
->myConverterArray
[cs0
],
3106 if(len2
== 2 || (len2
== -2 && len
== 0)) {
3107 targetValue
= value
;
3111 useFallback
= FALSE
;
3118 len
= 0; /* count output bytes; it must have been abs(len) == 2 */
3120 /* write the designation sequence if necessary */
3121 if(cs
!= pFromU2022State
->cs
[g
]) {
3122 if(cs
< CNS_11643
) {
3123 uprv_memcpy(buffer
, escSeqCharsCN
[cs
], 4);
3125 U_ASSERT(cs
>= CNS_11643_1
);
3126 uprv_memcpy(buffer
, escSeqCharsCN
[CNS_11643
+ (cs
- CNS_11643_1
)], 4);
3129 pFromU2022State
->cs
[g
] = cs
;
3131 /* changing the SO/G1 charset invalidates the choices[] */
3136 /* write the shift sequence if necessary */
3137 if(g
!= pFromU2022State
->g
) {
3140 buffer
[len
++] = UCNV_SO
;
3142 /* set the new state only if it is the locking shift SO/G1, not for SS2 or SS3 */
3143 pFromU2022State
->g
= 1;
3146 buffer
[len
++] = 0x1b;
3147 buffer
[len
++] = 0x4e;
3149 default: /* case 3 */
3150 buffer
[len
++] = 0x1b;
3151 buffer
[len
++] = 0x4f;
3156 /* write the two output bytes */
3157 buffer
[len
++] = (char)(targetValue
>> 8);
3158 buffer
[len
++] = (char)targetValue
;
3160 /* if we cannot find the character after checking all codepages
3161 * then this is an error
3163 *err
= U_INVALID_CHAR_FOUND
;
3164 cnv
->fromUChar32
=sourceChar
;
3169 /* output len>0 bytes in buffer[] */
3171 *target
++ = buffer
[0];
3173 *offsets
++ = (int32_t)(source
- args
->source
- 1); /* -1: known to be ASCII */
3175 } else if(len
== 2 && (target
+ 2) <= targetLimit
) {
3176 *target
++ = buffer
[0];
3177 *target
++ = buffer
[1];
3179 int32_t sourceIndex
= (int32_t)(source
- args
->source
- U16_LENGTH(sourceChar
));
3180 *offsets
++ = sourceIndex
;
3181 *offsets
++ = sourceIndex
;
3187 &target
, (const char *)targetLimit
,
3188 &offsets
, (int32_t)(source
- args
->source
- U16_LENGTH(sourceChar
)),
3190 if(U_FAILURE(*err
)) {
3194 } /* end if(myTargetIndex<myTargetLength) */
3196 *err
=U_BUFFER_OVERFLOW_ERROR
;
3200 }/* end while(mySourceIndex<mySourceLength) */
3203 * the end of the input stream and detection of truncated input
3204 * are handled by the framework, but for ISO-2022-CN conversion
3205 * we need to be in ASCII mode at the very end
3210 * end of input and no truncated input
3212 if( U_SUCCESS(*err
) &&
3213 pFromU2022State
->g
!=0 &&
3214 args
->flush
&& source
>=sourceLimit
&& cnv
->fromUChar32
==0
3216 int32_t sourceIndex
;
3218 /* we are switching to ASCII */
3219 pFromU2022State
->g
=0;
3221 /* get the source index of the last input character */
3223 * TODO this would be simpler and more reliable if we used a pair
3224 * of sourceIndex/prevSourceIndex like in ucnvmbcs.c
3225 * so that we could simply use the prevSourceIndex here;
3226 * this code gives an incorrect result for the rare case of an unmatched
3227 * trail surrogate that is alone in the last buffer of the text stream
3229 sourceIndex
=(int32_t)(source
-args
->source
);
3232 if( U16_IS_TRAIL(args
->source
[sourceIndex
]) &&
3233 (sourceIndex
==0 || U16_IS_LEAD(args
->source
[sourceIndex
-1]))
3244 &target
, (const char *)targetLimit
,
3245 &offsets
, sourceIndex
,
3249 /*save the state and return */
3250 args
->source
= source
;
3251 args
->target
= (char*)target
;
3256 UConverter_toUnicode_ISO_2022_CN_OFFSETS_LOGIC(UConverterToUnicodeArgs
*args
,
3259 const char *mySource
= (char *) args
->source
;
3260 UChar
*myTarget
= args
->target
;
3261 const char *mySourceLimit
= args
->sourceLimit
;
3262 uint32_t targetUniChar
= 0x0000;
3263 uint32_t mySourceChar
= 0x0000;
3264 UConverterDataISO2022
* myData
;
3265 ISO2022State
*pToU2022State
;
3267 myData
=(UConverterDataISO2022
*)(args
->converter
->extraInfo
);
3268 pToU2022State
= &myData
->toU2022State
;
3270 if(myData
->key
!= 0) {
3271 /* continue with a partial escape sequence */
3273 } else if(args
->converter
->toULength
== 1 && mySource
< mySourceLimit
&& myTarget
< args
->targetLimit
) {
3274 /* continue with a partial double-byte character */
3275 mySourceChar
= args
->converter
->toUBytes
[0];
3276 args
->converter
->toULength
= 0;
3277 targetUniChar
= missingCharMarker
;
3281 while(mySource
< mySourceLimit
){
3283 targetUniChar
=missingCharMarker
;
3285 if(myTarget
< args
->targetLimit
){
3287 mySourceChar
= (unsigned char) *mySource
++;
3289 switch(mySourceChar
){
3292 if (myData
->isEmptySegment
) {
3293 myData
->isEmptySegment
= FALSE
; /* we are handling it, reset to avoid future spurious errors */
3294 *err
= U_ILLEGAL_ESCAPE_SEQUENCE
;
3295 args
->converter
->toUCallbackReason
= UCNV_IRREGULAR
;
3296 args
->converter
->toUBytes
[0] = mySourceChar
;
3297 args
->converter
->toULength
= 1;
3298 args
->target
= myTarget
;
3299 args
->source
= mySource
;
3305 if(pToU2022State
->cs
[1] != 0) {
3307 myData
->isEmptySegment
= TRUE
; /* Begin a new segment, empty so far */
3310 /* illegal to have SO before a matching designator */
3311 myData
->isEmptySegment
= FALSE
; /* Handling a different error, reset this to avoid future spurious errs */
3319 const char * mySourceBefore
= mySource
;
3320 int8_t toULengthBefore
= args
->converter
->toULength
;
3322 changeState_2022(args
->converter
,&(mySource
),
3323 mySourceLimit
, ISO_2022_CN
,err
);
3325 /* After SO there must be at least one character before a designator (designator error handled separately) */
3326 if(myData
->key
==0 && U_SUCCESS(*err
) && myData
->isEmptySegment
) {
3327 *err
= U_ILLEGAL_ESCAPE_SEQUENCE
;
3328 args
->converter
->toUCallbackReason
= UCNV_IRREGULAR
;
3329 args
->converter
->toULength
= (int8_t)(toULengthBefore
+ (mySource
- mySourceBefore
));
3333 /* invalid or illegal escape sequence */
3334 if(U_FAILURE(*err
)){
3335 args
->target
= myTarget
;
3336 args
->source
= mySource
;
3337 myData
->isEmptySegment
= FALSE
; /* Reset to avoid future spurious errors */
3342 /* ISO-2022-CN does not use single-byte (C1) SS2 and SS3 */
3346 uprv_memset(pToU2022State
, 0, sizeof(ISO2022State
));
3349 /* convert one or two bytes */
3350 myData
->isEmptySegment
= FALSE
;
3351 if(pToU2022State
->g
!= 0) {
3352 if(mySource
< mySourceLimit
) {
3353 UConverterSharedData
*cnv
;
3354 StateEnum tempState
;
3356 int leadIsOk
, trailIsOk
;
3359 trailByte
= (uint8_t)*mySource
;
3361 * Ticket 5691: consistent illegal sequences:
3362 * - We include at least the first byte in the illegal sequence.
3363 * - If any of the non-initial bytes could be the start of a character,
3364 * we stop the illegal sequence before the first one of those.
3366 * In ISO-2022 DBCS, if the second byte is in the 21..7e range or is
3367 * an ESC/SO/SI, we report only the first byte as the illegal sequence.
3368 * Otherwise we convert or report the pair of bytes.
3370 leadIsOk
= (uint8_t)(mySourceChar
- 0x21) <= (0x7e - 0x21);
3371 trailIsOk
= (uint8_t)(trailByte
- 0x21) <= (0x7e - 0x21);
3372 if (leadIsOk
&& trailIsOk
) {
3374 tempState
= (StateEnum
)pToU2022State
->cs
[pToU2022State
->g
];
3375 if(tempState
>= CNS_11643_0
) {
3376 cnv
= myData
->myConverterArray
[CNS_11643
];
3377 tempBuf
[0] = (char) (0x80+(tempState
-CNS_11643_0
));
3378 tempBuf
[1] = (char) (mySourceChar
);
3379 tempBuf
[2] = (char) trailByte
;
3383 U_ASSERT(tempState
<UCNV_2022_MAX_CONVERTERS
);
3384 cnv
= myData
->myConverterArray
[tempState
];
3385 tempBuf
[0] = (char) (mySourceChar
);
3386 tempBuf
[1] = (char) trailByte
;
3389 targetUniChar
= ucnv_MBCSSimpleGetNextUChar(cnv
, tempBuf
, tempBufLen
, FALSE
);
3390 mySourceChar
= (mySourceChar
<< 8) | trailByte
;
3391 } else if (!(trailIsOk
|| IS_2022_CONTROL(trailByte
))) {
3392 /* report a pair of illegal bytes if the second byte is not a DBCS starter */
3394 /* add another bit so that the code below writes 2 bytes in case of error */
3395 mySourceChar
= 0x10000 | (mySourceChar
<< 8) | trailByte
;
3397 if(pToU2022State
->g
>=2) {
3398 /* return from a single-shift state to the previous one */
3399 pToU2022State
->g
=pToU2022State
->prevG
;
3402 args
->converter
->toUBytes
[0] = (uint8_t)mySourceChar
;
3403 args
->converter
->toULength
= 1;
3408 if(mySourceChar
<= 0x7f) {
3409 targetUniChar
= (UChar
) mySourceChar
;
3414 if(targetUniChar
< (missingCharMarker
-1/*0xfffe*/)){
3416 args
->offsets
[myTarget
- args
->target
] = (int32_t)(mySource
- args
->source
- (mySourceChar
<= 0xff ? 1 : 2));
3418 *(myTarget
++)=(UChar
)targetUniChar
;
3420 else if(targetUniChar
> missingCharMarker
){
3421 /* disassemble the surrogate pair and write to output*/
3422 targetUniChar
-=0x0010000;
3423 *myTarget
= (UChar
)(0xd800+(UChar
)(targetUniChar
>>10));
3425 args
->offsets
[myTarget
- args
->target
] = (int32_t)(mySource
- args
->source
- (mySourceChar
<= 0xff ? 1 : 2));
3428 if(myTarget
< args
->targetLimit
){
3429 *myTarget
= (UChar
)(0xdc00+(UChar
)(targetUniChar
&0x3ff));
3431 args
->offsets
[myTarget
- args
->target
] = (int32_t)(mySource
- args
->source
- (mySourceChar
<= 0xff ? 1 : 2));
3435 args
->converter
->UCharErrorBuffer
[args
->converter
->UCharErrorBufferLength
++]=
3436 (UChar
)(0xdc00+(UChar
)(targetUniChar
&0x3ff));
3441 /* Call the callback function*/
3442 toUnicodeCallback(args
->converter
,mySourceChar
,targetUniChar
,err
);
3447 *err
=U_BUFFER_OVERFLOW_ERROR
;
3452 args
->target
= myTarget
;
3453 args
->source
= mySource
;
3455 #endif /* #if !UCONFIG_ONLY_HTML_CONVERSION */
3458 _ISO_2022_WriteSub(UConverterFromUnicodeArgs
*args
, int32_t offsetIndex
, UErrorCode
*err
) {
3459 UConverter
*cnv
= args
->converter
;
3460 UConverterDataISO2022
*myConverterData
=(UConverterDataISO2022
*) cnv
->extraInfo
;
3461 ISO2022State
*pFromU2022State
=&myConverterData
->fromU2022State
;
3466 subchar
=(char *)cnv
->subChars
;
3467 length
=cnv
->subCharLen
; /* assume length==1 for most variants */
3470 switch(myConverterData
->locale
[0]){
3475 if(pFromU2022State
->g
== 1) {
3476 /* JIS7: switch from G1 to G0 */
3477 pFromU2022State
->g
= 0;
3481 cs
= pFromU2022State
->cs
[0];
3482 if(cs
!= ASCII
&& cs
!= JISX201
) {
3483 /* not in ASCII or JIS X 0201: switch to ASCII */
3484 pFromU2022State
->cs
[0] = (int8_t)ASCII
;
3494 if(pFromU2022State
->g
!= 0) {
3495 /* not in ASCII mode: switch to ASCII */
3496 pFromU2022State
->g
= 0;
3502 if(myConverterData
->version
== 0) {
3504 if((UBool
)args
->converter
->fromUnicodeStatus
) {
3505 /* in DBCS mode: switch to SBCS */
3506 args
->converter
->fromUnicodeStatus
= 0;
3510 } else /* length == 2*/ {
3511 if(!(UBool
)args
->converter
->fromUnicodeStatus
) {
3512 /* in SBCS mode: switch to DBCS */
3513 args
->converter
->fromUnicodeStatus
= 1;
3521 /* save the subconverter's substitution string */
3522 uint8_t *currentSubChars
= myConverterData
->currentConverter
->subChars
;
3523 int8_t currentSubCharLen
= myConverterData
->currentConverter
->subCharLen
;
3525 /* set our substitution string into the subconverter */
3526 myConverterData
->currentConverter
->subChars
= (uint8_t *)subchar
;
3527 myConverterData
->currentConverter
->subCharLen
= (int8_t)length
;
3529 /* let the subconverter write the subchar, set/retrieve fromUChar32 state */
3530 args
->converter
= myConverterData
->currentConverter
;
3531 myConverterData
->currentConverter
->fromUChar32
= cnv
->fromUChar32
;
3532 ucnv_cbFromUWriteSub(args
, 0, err
);
3533 cnv
->fromUChar32
= myConverterData
->currentConverter
->fromUChar32
;
3534 args
->converter
= cnv
;
3536 /* restore the subconverter's substitution string */
3537 myConverterData
->currentConverter
->subChars
= currentSubChars
;
3538 myConverterData
->currentConverter
->subCharLen
= currentSubCharLen
;
3540 if(*err
== U_BUFFER_OVERFLOW_ERROR
) {
3541 if(myConverterData
->currentConverter
->charErrorBufferLength
> 0) {
3543 cnv
->charErrorBuffer
,
3544 myConverterData
->currentConverter
->charErrorBuffer
,
3545 myConverterData
->currentConverter
->charErrorBufferLength
);
3547 cnv
->charErrorBufferLength
= myConverterData
->currentConverter
->charErrorBufferLength
;
3548 myConverterData
->currentConverter
->charErrorBufferLength
= 0;
3556 ucnv_cbFromUWriteBytes(args
,
3557 buffer
, (int32_t)(p
- buffer
),
3562 * Structure for cloning an ISO 2022 converter into a single memory block.
3563 * ucnv_safeClone() of the converter will align the entire cloneStruct,
3564 * and then ucnv_safeClone() of the sub-converter may additionally align
3565 * currentConverter inside the cloneStruct, for which we need the deadSpace
3566 * after currentConverter.
3567 * This is because UAlignedMemory may be larger than the actually
3568 * necessary alignment size for the platform.
3569 * The other cloneStruct fields will not be moved around,
3570 * and are aligned properly with cloneStruct's alignment.
3575 UConverter currentConverter
;
3576 UAlignedMemory deadSpace
;
3577 UConverterDataISO2022 mydata
;
3582 _ISO_2022_SafeClone(
3583 const UConverter
*cnv
,
3585 int32_t *pBufferSize
,
3588 struct cloneStruct
* localClone
;
3589 UConverterDataISO2022
*cnvData
;
3592 if (*pBufferSize
== 0) { /* 'preflighting' request - set needed size into *pBufferSize */
3593 *pBufferSize
= (int32_t)sizeof(struct cloneStruct
);
3597 cnvData
= (UConverterDataISO2022
*)cnv
->extraInfo
;
3598 localClone
= (struct cloneStruct
*)stackBuffer
;
3600 /* ucnv.c/ucnv_safeClone() copied the main UConverter already */
3602 uprv_memcpy(&localClone
->mydata
, cnvData
, sizeof(UConverterDataISO2022
));
3603 localClone
->cnv
.extraInfo
= &localClone
->mydata
; /* set pointer to extra data */
3604 localClone
->cnv
.isExtraLocal
= TRUE
;
3606 /* share the subconverters */
3608 if(cnvData
->currentConverter
!= NULL
) {
3609 size
= (int32_t)(sizeof(UConverter
) + sizeof(UAlignedMemory
)); /* include size of padding */
3610 localClone
->mydata
.currentConverter
=
3611 ucnv_safeClone(cnvData
->currentConverter
,
3612 &localClone
->currentConverter
,
3614 if(U_FAILURE(*status
)) {
3619 for(i
=0; i
<UCNV_2022_MAX_CONVERTERS
; ++i
) {
3620 if(cnvData
->myConverterArray
[i
] != NULL
) {
3621 ucnv_incrementRefCount(cnvData
->myConverterArray
[i
]);
3625 return &localClone
->cnv
;
3629 _ISO_2022_GetUnicodeSet(const UConverter
*cnv
,
3630 const USetAdder
*sa
,
3631 UConverterUnicodeSet which
,
3632 UErrorCode
*pErrorCode
)
3635 UConverterDataISO2022
* cnvData
;
3637 if (U_FAILURE(*pErrorCode
)) {
3640 #ifdef U_ENABLE_GENERIC_ISO_2022
3641 if (cnv
->sharedData
== &_ISO2022Data
) {
3642 /* We use UTF-8 in this case */
3643 sa
->addRange(sa
->set
, 0, 0xd7FF);
3644 sa
->addRange(sa
->set
, 0xE000, 0x10FFFF);
3649 cnvData
= (UConverterDataISO2022
*)cnv
->extraInfo
;
3651 /* open a set and initialize it with code points that are algorithmically round-tripped */
3652 switch(cnvData
->locale
[0]){
3654 /* include JIS X 0201 which is hardcoded */
3655 sa
->add(sa
->set
, 0xa5);
3656 sa
->add(sa
->set
, 0x203e);
3657 if(jpCharsetMasks
[cnvData
->version
]&CSM(ISO8859_1
)) {
3658 /* include Latin-1 for some variants of JP */
3659 sa
->addRange(sa
->set
, 0, 0xff);
3661 /* include ASCII for JP */
3662 sa
->addRange(sa
->set
, 0, 0x7f);
3664 if(cnvData
->version
==3 || cnvData
->version
==4 || which
==UCNV_ROUNDTRIP_AND_FALLBACK_SET
) {
3666 * Do not test (jpCharsetMasks[cnvData->version]&CSM(HWKANA_7BIT))!=0
3667 * because the bit is on for all JP versions although only versions 3 & 4 (JIS7 & JIS8)
3668 * use half-width Katakana.
3669 * This is because all ISO-2022-JP variants are lenient in that they accept (in toUnicode)
3670 * half-width Katakana via the ESC ( I sequence.
3671 * However, we only emit (fromUnicode) half-width Katakana according to the
3672 * definition of each variant.
3674 * When including fallbacks,
3675 * we need to include half-width Katakana Unicode code points for all JP variants because
3676 * JIS X 0208 has hardcoded fallbacks for them (which map to full-width Katakana).
3678 /* include half-width Katakana for JP */
3679 sa
->addRange(sa
->set
, HWKANA_START
, HWKANA_END
);
3682 #if !UCONFIG_ONLY_HTML_CONVERSION
3685 /* include ASCII for CN */
3686 sa
->addRange(sa
->set
, 0, 0x7f);
3689 /* there is only one converter for KR, and it is not in the myConverterArray[] */
3690 cnvData
->currentConverter
->sharedData
->impl
->getUnicodeSet(
3691 cnvData
->currentConverter
, sa
, which
, pErrorCode
);
3692 /* the loop over myConverterArray[] will simply not find another converter */
3699 #if 0 /* Replaced by ucnv_MBCSGetFilteredUnicodeSetForUnicode() until we implement ucnv_getUnicodeSet() with reverse fallbacks. */
3700 if( (cnvData
->locale
[0]=='c' || cnvData
->locale
[0]=='z') &&
3701 cnvData
->version
==0 && i
==CNS_11643
3703 /* special handling for non-EXT ISO-2022-CN: add only code points for CNS planes 1 and 2 */
3704 ucnv_MBCSGetUnicodeSetForBytes(
3705 cnvData
->myConverterArray
[i
],
3706 sa
, UCNV_ROUNDTRIP_SET
,
3712 for (i
=0; i
<UCNV_2022_MAX_CONVERTERS
; i
++) {
3713 UConverterSetFilter filter
;
3714 if(cnvData
->myConverterArray
[i
]!=NULL
) {
3715 if(cnvData
->locale
[0]=='j' && i
==JISX208
) {
3717 * Only add code points that map to Shift-JIS codes
3718 * corresponding to JIS X 0208.
3720 filter
=UCNV_SET_FILTER_SJIS
;
3721 #if !UCONFIG_ONLY_HTML_CONVERSION
3722 } else if( (cnvData
->locale
[0]=='c' || cnvData
->locale
[0]=='z') &&
3723 cnvData
->version
==0 && i
==CNS_11643
) {
3725 * Version-specific for CN:
3726 * CN version 0 does not map CNS planes 3..7 although
3727 * they are all available in the CNS conversion table;
3728 * CN version 1 (-EXT) does map them all.
3729 * The two versions create different Unicode sets.
3731 filter
=UCNV_SET_FILTER_2022_CN
;
3732 } else if(i
==KSC5601
) {
3734 * Some of the KSC 5601 tables (convrtrs.txt has this aliases on multiple tables)
3735 * are broader than GR94.
3737 filter
=UCNV_SET_FILTER_GR94DBCS
;
3740 filter
=UCNV_SET_FILTER_NONE
;
3742 ucnv_MBCSGetFilteredUnicodeSetForUnicode(cnvData
->myConverterArray
[i
], sa
, which
, filter
, pErrorCode
);
3747 * ISO 2022 converters must not convert SO/SI/ESC despite what
3748 * sub-converters do by themselves.
3749 * Remove these characters from the set.
3751 sa
->remove(sa
->set
, 0x0e);
3752 sa
->remove(sa
->set
, 0x0f);
3753 sa
->remove(sa
->set
, 0x1b);
3755 /* ISO 2022 converters do not convert C1 controls either */
3756 sa
->removeRange(sa
->set
, 0x80, 0x9f);
3759 static const UConverterImpl _ISO2022Impl
={
3769 #ifdef U_ENABLE_GENERIC_ISO_2022
3770 T_UConverter_toUnicode_ISO_2022_OFFSETS_LOGIC
,
3771 T_UConverter_toUnicode_ISO_2022_OFFSETS_LOGIC
,
3772 ucnv_fromUnicode_UTF8
,
3773 ucnv_fromUnicode_UTF8_OFFSETS_LOGIC
,
3785 _ISO_2022_SafeClone
,
3786 _ISO_2022_GetUnicodeSet
,
3791 static const UConverterStaticData _ISO2022StaticData
={
3792 sizeof(UConverterStaticData
),
3798 3, /* max 3 bytes per UChar from UTF-8 (4 bytes from surrogate _pair_) */
3805 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */
3807 const UConverterSharedData _ISO2022Data
=
3808 UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(&_ISO2022StaticData
, &_ISO2022Impl
);
3810 /*************JP****************/
3811 static const UConverterImpl _ISO2022JPImpl
={
3821 UConverter_toUnicode_ISO_2022_JP_OFFSETS_LOGIC
,
3822 UConverter_toUnicode_ISO_2022_JP_OFFSETS_LOGIC
,
3823 UConverter_fromUnicode_ISO_2022_JP_OFFSETS_LOGIC
,
3824 UConverter_fromUnicode_ISO_2022_JP_OFFSETS_LOGIC
,
3830 _ISO_2022_SafeClone
,
3831 _ISO_2022_GetUnicodeSet
,
3836 static const UConverterStaticData _ISO2022JPStaticData
={
3837 sizeof(UConverterStaticData
),
3843 6, /* max 6 bytes per UChar: 4-byte escape sequence + DBCS */
3850 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */
3855 const UConverterSharedData _ISO2022JPData
=
3856 UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(&_ISO2022JPStaticData
, &_ISO2022JPImpl
);
3860 #if !UCONFIG_ONLY_HTML_CONVERSION
3861 /************* KR ***************/
3862 static const UConverterImpl _ISO2022KRImpl
={
3872 UConverter_toUnicode_ISO_2022_KR_OFFSETS_LOGIC
,
3873 UConverter_toUnicode_ISO_2022_KR_OFFSETS_LOGIC
,
3874 UConverter_fromUnicode_ISO_2022_KR_OFFSETS_LOGIC
,
3875 UConverter_fromUnicode_ISO_2022_KR_OFFSETS_LOGIC
,
3881 _ISO_2022_SafeClone
,
3882 _ISO_2022_GetUnicodeSet
,
3887 static const UConverterStaticData _ISO2022KRStaticData
={
3888 sizeof(UConverterStaticData
),
3894 8, /* max 8 bytes per UChar */
3901 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */
3906 const UConverterSharedData _ISO2022KRData
=
3907 UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(&_ISO2022KRStaticData
, &_ISO2022KRImpl
);
3911 /*************** CN ***************/
3912 static const UConverterImpl _ISO2022CNImpl
={
3923 UConverter_toUnicode_ISO_2022_CN_OFFSETS_LOGIC
,
3924 UConverter_toUnicode_ISO_2022_CN_OFFSETS_LOGIC
,
3925 UConverter_fromUnicode_ISO_2022_CN_OFFSETS_LOGIC
,
3926 UConverter_fromUnicode_ISO_2022_CN_OFFSETS_LOGIC
,
3932 _ISO_2022_SafeClone
,
3933 _ISO_2022_GetUnicodeSet
,
3938 static const UConverterStaticData _ISO2022CNStaticData
={
3939 sizeof(UConverterStaticData
),
3945 8, /* max 8 bytes per UChar: 4-byte CNS designator + 2 bytes for SS2/SS3 + DBCS */
3952 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */
3957 const UConverterSharedData _ISO2022CNData
=
3958 UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(&_ISO2022CNStaticData
, &_ISO2022CNImpl
);
3961 #endif /* #if !UCONFIG_ONLY_HTML_CONVERSION */
3963 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */