2 **********************************************************************
3 * Copyright (C) 2000-2008, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
8 * tab size: 8 (not used)
11 * created on: 2000oct16
12 * created by: Ram Viswanadha
13 * 10/31/2000 Ram Implemented offsets logic function
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION
22 #include "unicode/ucnv.h"
23 #include "unicode/ucnv_cb.h"
24 #include "unicode/uset.h"
28 #define UCNV_TILDE 0x7E /* ~ */
29 #define UCNV_OPEN_BRACE 0x7B /* { */
30 #define UCNV_CLOSE_BRACE 0x7D /* } */
31 #define SB_ESCAPE "\x7E\x7D"
32 #define DB_ESCAPE "\x7E\x7B"
33 #define TILDE_ESCAPE "\x7E\x7E"
37 #define CONCAT_ESCAPE_MACRO( args, targetIndex,targetLength,strToAppend, err, len,sourceIndex){ \
39 if(targetIndex < targetLength){ \
40 args->target[targetIndex] = (unsigned char) *strToAppend; \
41 if(args->offsets!=NULL){ \
42 *(offsets++) = sourceIndex-1; \
47 args->converter->charErrorBuffer[(int)args->converter->charErrorBufferLength++] = (unsigned char) *strToAppend; \
48 *err =U_BUFFER_OVERFLOW_ERROR; \
56 UConverter
* gbConverter
;
59 UBool isEscapeAppended
;
61 UBool isTargetUCharDBCS
;
68 _HZOpen(UConverter
*cnv
, const char *name
,const char *locale
,uint32_t options
, UErrorCode
*errorCode
){
69 cnv
->toUnicodeStatus
= 0;
70 cnv
->fromUnicodeStatus
= 0;
72 cnv
->fromUChar32
=0x0000;
73 cnv
->extraInfo
= uprv_malloc(sizeof(UConverterDataHZ
));
74 if(cnv
->extraInfo
!= NULL
){
75 uprv_memset(cnv
->extraInfo
, 0, sizeof(UConverterDataHZ
));
76 ((UConverterDataHZ
*)cnv
->extraInfo
)->gbConverter
= ucnv_open("GBK",errorCode
);
79 *errorCode
= U_MEMORY_ALLOCATION_ERROR
;
85 _HZClose(UConverter
*cnv
){
86 if(cnv
->extraInfo
!= NULL
) {
87 ucnv_close (((UConverterDataHZ
*) (cnv
->extraInfo
))->gbConverter
);
88 if(!cnv
->isExtraLocal
) {
89 uprv_free(cnv
->extraInfo
);
91 cnv
->extraInfo
= NULL
;
96 _HZReset(UConverter
*cnv
, UConverterResetChoice choice
){
97 if(choice
<=UCNV_RESET_TO_UNICODE
) {
98 cnv
->toUnicodeStatus
= 0;
100 if(cnv
->extraInfo
!= NULL
){
101 ((UConverterDataHZ
*)cnv
->extraInfo
)->isStateDBCS
= FALSE
;
102 ((UConverterDataHZ
*)cnv
->extraInfo
)->isEmptySegment
= FALSE
;
105 if(choice
!=UCNV_RESET_TO_UNICODE
) {
106 cnv
->fromUnicodeStatus
= 0;
107 cnv
->fromUChar32
=0x0000;
108 if(cnv
->extraInfo
!= NULL
){
109 ((UConverterDataHZ
*)cnv
->extraInfo
)->isEscapeAppended
= FALSE
;
110 ((UConverterDataHZ
*)cnv
->extraInfo
)->targetIndex
= 0;
111 ((UConverterDataHZ
*)cnv
->extraInfo
)->sourceIndex
= 0;
112 ((UConverterDataHZ
*)cnv
->extraInfo
)->isTargetUCharDBCS
= FALSE
;
117 /**************************************HZ Encoding*************************************************
118 * Rules for HZ encoding
120 * In ASCII mode, a byte is interpreted as an ASCII character, unless a
121 * '~' is encountered. The character '~' is an escape character. By
122 * convention, it must be immediately followed ONLY by '~', '{' or '\n'
123 * (<LF>), with the following special meaning.
125 * 1. The escape sequence '~~' is interpreted as a '~'.
126 * 2. The escape-to-GB sequence '~{' switches the mode from ASCII to GB.
127 * 3. The escape sequence '~\n' is a line-continuation marker to be
128 * consumed with no output produced.
129 * In GB mode, characters are interpreted two bytes at a time as (pure)
130 * GB codes until the escape-from-GB code '~}' is read. This code
131 * switches the mode from GB back to ASCII. (Note that the escape-
132 * from-GB code '~}' ($7E7D) is outside the defined GB range.)
136 * Note that the formal syntax in RFC 1842 is invalid. I assume that the
137 * intended definition of single-byte-segment is as follows (pedberg):
138 * single-byte-segment = single-byte-seq 1*single-byte-char
143 UConverter_toUnicode_HZ_OFFSETS_LOGIC(UConverterToUnicodeArgs
*args
,
146 const char *mySource
= ( char *) args
->source
;
147 UChar
*myTarget
= args
->target
;
148 const char *mySourceLimit
= args
->sourceLimit
;
149 UChar32 targetUniChar
= 0x0000;
150 int32_t mySourceChar
= 0x0000;
151 UConverterDataHZ
* myData
=(UConverterDataHZ
*)(args
->converter
->extraInfo
);
155 /* Calling code already handles this situation. */
156 /*if ((args->converter == NULL) || (args->targetLimit < args->target) || (mySourceLimit < args->source)){
157 *err = U_ILLEGAL_ARGUMENT_ERROR;
161 while(mySource
< mySourceLimit
){
163 if(myTarget
< args
->targetLimit
){
165 mySourceChar
= (unsigned char) *mySource
++;
167 if(args
->converter
->mode
== UCNV_TILDE
) {
168 /* second byte after ~ */
169 args
->converter
->mode
=0;
170 switch(mySourceChar
) {
172 /* no output for ~\n (line-continuation marker) */
176 args
->offsets
[myTarget
- args
->target
]=(int32_t)(mySource
- args
->source
- 2);
178 *(myTarget
++)=(UChar
)mySourceChar
;
179 myData
->isEmptySegment
= FALSE
;
181 case UCNV_OPEN_BRACE
:
182 case UCNV_CLOSE_BRACE
:
183 myData
->isStateDBCS
= (mySourceChar
== UCNV_OPEN_BRACE
);
184 if (myData
->isEmptySegment
) {
185 myData
->isEmptySegment
= FALSE
; /* we are handling it, reset to avoid future spurious errors */
186 *err
= U_ILLEGAL_ESCAPE_SEQUENCE
;
187 args
->converter
->toUCallbackReason
= UCNV_IRREGULAR
;
188 args
->converter
->toUBytes
[0] = UCNV_TILDE
;
189 args
->converter
->toUBytes
[1] = mySourceChar
;
190 args
->converter
->toULength
= 2;
191 args
->target
= myTarget
;
192 args
->source
= mySource
;
195 myData
->isEmptySegment
= TRUE
;
198 /* if the first byte is equal to TILDE and the trail byte
199 * is not a valid byte then it is an error condition
202 * Ticket 5691: consistent illegal sequences:
203 * - We include at least the first byte in the illegal sequence.
204 * - If any of the non-initial bytes could be the start of a character,
205 * we stop the illegal sequence before the first one of those.
207 myData
->isEmptySegment
= FALSE
; /* different error here, reset this to avoid spurious future error */
208 *err
= U_ILLEGAL_ESCAPE_SEQUENCE
;
209 args
->converter
->toUBytes
[0] = UCNV_TILDE
;
210 if( myData
->isStateDBCS
?
211 (0x21 <= mySourceChar
&& mySourceChar
<= 0x7e) :
214 /* The current byte could be the start of a character: Back it out. */
215 args
->converter
->toULength
= 1;
218 /* Include the current byte in the illegal sequence. */
219 args
->converter
->toUBytes
[1] = mySourceChar
;
220 args
->converter
->toULength
= 2;
222 args
->target
= myTarget
;
223 args
->source
= mySource
;
226 } else if(myData
->isStateDBCS
) {
227 if(args
->converter
->toUnicodeStatus
== 0x00){
229 if(mySourceChar
== UCNV_TILDE
) {
230 args
->converter
->mode
= UCNV_TILDE
;
232 /* add another bit to distinguish a 0 byte from not having seen a lead byte */
233 args
->converter
->toUnicodeStatus
= (uint32_t) (mySourceChar
| 0x100);
234 myData
->isEmptySegment
= FALSE
; /* the segment has something, either valid or will produce a different error, so reset this */
240 int leadIsOk
, trailIsOk
;
241 uint32_t leadByte
= args
->converter
->toUnicodeStatus
& 0xff;
242 targetUniChar
= 0xffff;
244 * Ticket 5691: consistent illegal sequences:
245 * - We include at least the first byte in the illegal sequence.
246 * - If any of the non-initial bytes could be the start of a character,
247 * we stop the illegal sequence before the first one of those.
249 * In HZ DBCS, if the second byte is in the 21..7e range,
250 * we report only the first byte as the illegal sequence.
251 * Otherwise we convert or report the pair of bytes.
253 leadIsOk
= (uint8_t)(leadByte
- 0x21) <= (0x7d - 0x21);
254 trailIsOk
= (uint8_t)(mySourceChar
- 0x21) <= (0x7e - 0x21);
255 if (leadIsOk
&& trailIsOk
) {
256 tempBuf
[0] = (char) (leadByte
+0x80) ;
257 tempBuf
[1] = (char) (mySourceChar
+0x80);
258 targetUniChar
= ucnv_MBCSSimpleGetNextUChar(myData
->gbConverter
->sharedData
,
259 tempBuf
, 2, args
->converter
->useFallback
);
260 mySourceChar
= (leadByte
<< 8) | mySourceChar
;
261 } else if (trailIsOk
) {
262 /* report a single illegal byte and continue with the following DBCS starter byte */
264 mySourceChar
= (int32_t)leadByte
;
266 /* report a pair of illegal bytes if the second byte is not a DBCS starter */
267 /* add another bit so that the code below writes 2 bytes in case of error */
268 mySourceChar
= 0x10000 | (leadByte
<< 8) | mySourceChar
;
270 args
->converter
->toUnicodeStatus
=0x00;
274 if(mySourceChar
== UCNV_TILDE
) {
275 args
->converter
->mode
= UCNV_TILDE
;
277 } else if(mySourceChar
<= 0x7f) {
278 targetUniChar
= (UChar
)mySourceChar
; /* ASCII */
279 myData
->isEmptySegment
= FALSE
; /* the segment has something valid */
281 targetUniChar
= 0xffff;
282 myData
->isEmptySegment
= FALSE
; /* different error here, reset this to avoid spurious future error */
285 if(targetUniChar
< 0xfffe){
287 args
->offsets
[myTarget
- args
->target
]=(int32_t)(mySource
- args
->source
- 1-(myData
->isStateDBCS
));
290 *(myTarget
++)=(UChar
)targetUniChar
;
292 else /* targetUniChar>=0xfffe */ {
293 if(targetUniChar
== 0xfffe){
294 *err
= U_INVALID_CHAR_FOUND
;
297 *err
= U_ILLEGAL_CHAR_FOUND
;
299 if(mySourceChar
> 0xff){
300 args
->converter
->toUBytes
[0] = (uint8_t)(mySourceChar
>> 8);
301 args
->converter
->toUBytes
[1] = (uint8_t)mySourceChar
;
302 args
->converter
->toULength
=2;
305 args
->converter
->toUBytes
[0] = (uint8_t)mySourceChar
;
306 args
->converter
->toULength
=1;
312 *err
=U_BUFFER_OVERFLOW_ERROR
;
317 args
->target
= myTarget
;
318 args
->source
= mySource
;
323 UConverter_fromUnicode_HZ_OFFSETS_LOGIC (UConverterFromUnicodeArgs
* args
,
325 const UChar
*mySource
= args
->source
;
326 char *myTarget
= args
->target
;
327 int32_t* offsets
= args
->offsets
;
328 int32_t mySourceIndex
= 0;
329 int32_t myTargetIndex
= 0;
330 int32_t targetLength
= (int32_t)(args
->targetLimit
- myTarget
);
331 int32_t mySourceLength
= (int32_t)(args
->sourceLimit
- args
->source
);
333 uint32_t targetUniChar
= 0x0000;
334 UChar32 mySourceChar
= 0x0000;
335 UConverterDataHZ
*myConverterData
=(UConverterDataHZ
*)args
->converter
->extraInfo
;
336 UBool isTargetUCharDBCS
= (UBool
) myConverterData
->isTargetUCharDBCS
;
337 UBool oldIsTargetUCharDBCS
= isTargetUCharDBCS
;
339 const char* escSeq
=NULL
;
341 /* Calling code already handles this situation. */
342 /*if ((args->converter == NULL) || (args->targetLimit < myTarget) || (args->sourceLimit < args->source)){
343 *err = U_ILLEGAL_ARGUMENT_ERROR;
346 if(args
->converter
->fromUChar32
!=0 && myTargetIndex
< targetLength
) {
349 /*writing the char to the output stream */
350 while (mySourceIndex
< mySourceLength
){
351 targetUniChar
= missingCharMarker
;
352 if (myTargetIndex
< targetLength
){
354 mySourceChar
= (UChar
) mySource
[mySourceIndex
++];
357 oldIsTargetUCharDBCS
= isTargetUCharDBCS
;
358 if(mySourceChar
==UCNV_TILDE
){
359 /*concatEscape(args, &myTargetIndex, &targetLength,"\x7E\x7E",err,2,&mySourceIndex);*/
361 escSeq
= TILDE_ESCAPE
;
362 CONCAT_ESCAPE_MACRO(args
, myTargetIndex
, targetLength
, escSeq
,err
,len
,mySourceIndex
);
364 } else if(mySourceChar
<= 0x7f) {
366 targetUniChar
= mySourceChar
;
368 length
= ucnv_MBCSFromUChar32(myConverterData
->gbConverter
->sharedData
,
369 mySourceChar
,&targetUniChar
,args
->converter
->useFallback
);
370 /* we can only use lead bytes 21..7D and trail bytes 21..7E */
372 (uint16_t)(targetUniChar
- 0xa1a1) <= (0xfdfe - 0xa1a1) &&
373 (uint8_t)(targetUniChar
- 0xa1) <= (0xfe - 0xa1)
375 targetUniChar
-= 0x8080;
377 targetUniChar
= missingCharMarker
;
380 if (targetUniChar
!= missingCharMarker
){
381 myConverterData
->isTargetUCharDBCS
= isTargetUCharDBCS
= (UBool
)(targetUniChar
>0x00FF);
382 if(oldIsTargetUCharDBCS
!= isTargetUCharDBCS
|| !myConverterData
->isEscapeAppended
){
383 /*Shifting from a double byte to single byte mode*/
384 if(!isTargetUCharDBCS
){
387 CONCAT_ESCAPE_MACRO(args
, myTargetIndex
, targetLength
, escSeq
,err
,len
,mySourceIndex
);
388 myConverterData
->isEscapeAppended
= TRUE
;
390 else{ /* Shifting from a single byte to double byte mode*/
393 CONCAT_ESCAPE_MACRO(args
, myTargetIndex
, targetLength
, escSeq
,err
,len
,mySourceIndex
);
394 myConverterData
->isEscapeAppended
= TRUE
;
399 if(isTargetUCharDBCS
){
400 if( myTargetIndex
<targetLength
){
401 myTarget
[myTargetIndex
++] =(char) (targetUniChar
>> 8);
403 *(offsets
++) = mySourceIndex
-1;
405 if(myTargetIndex
< targetLength
){
406 myTarget
[myTargetIndex
++] =(char) targetUniChar
;
408 *(offsets
++) = mySourceIndex
-1;
411 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] = (char) targetUniChar
;
412 *err
= U_BUFFER_OVERFLOW_ERROR
;
415 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] =(char) (targetUniChar
>> 8);
416 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] = (char) targetUniChar
;
417 *err
= U_BUFFER_OVERFLOW_ERROR
;
421 if( myTargetIndex
<targetLength
){
422 myTarget
[myTargetIndex
++] = (char) (targetUniChar
);
424 *(offsets
++) = mySourceIndex
-1;
428 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] = (char) targetUniChar
;
429 *err
= U_BUFFER_OVERFLOW_ERROR
;
435 /* oops.. the code point is unassigned */
436 /*Handle surrogates */
437 /*check if the char is a First surrogate*/
438 if(UTF_IS_SURROGATE(mySourceChar
)) {
439 if(UTF_IS_SURROGATE_FIRST(mySourceChar
)) {
440 args
->converter
->fromUChar32
=mySourceChar
;
442 /*look ahead to find the trail surrogate*/
443 if(mySourceIndex
< mySourceLength
) {
444 /* test the following code unit */
445 UChar trail
=(UChar
) args
->source
[mySourceIndex
];
446 if(UTF_IS_SECOND_SURROGATE(trail
)) {
448 mySourceChar
=UTF16_GET_PAIR_VALUE(args
->converter
->fromUChar32
, trail
);
449 args
->converter
->fromUChar32
=0x00;
450 /* there are no surrogates in GB2312*/
451 *err
= U_INVALID_CHAR_FOUND
;
452 /* exit this condition tree */
454 /* this is an unmatched lead code unit (1st surrogate) */
455 /* callback(illegal) */
456 *err
=U_ILLEGAL_CHAR_FOUND
;
463 /* this is an unmatched trail code unit (2nd surrogate) */
464 /* callback(illegal) */
465 *err
=U_ILLEGAL_CHAR_FOUND
;
468 /* callback(unassigned) for a BMP code point */
469 *err
= U_INVALID_CHAR_FOUND
;
472 args
->converter
->fromUChar32
=mySourceChar
;
477 *err
= U_BUFFER_OVERFLOW_ERROR
;
480 targetUniChar
=missingCharMarker
;
483 args
->target
+= myTargetIndex
;
484 args
->source
+= mySourceIndex
;
485 myConverterData
->isTargetUCharDBCS
= isTargetUCharDBCS
;
489 _HZ_WriteSub(UConverterFromUnicodeArgs
*args
, int32_t offsetIndex
, UErrorCode
*err
) {
490 UConverter
*cnv
= args
->converter
;
491 UConverterDataHZ
*convData
=(UConverterDataHZ
*) cnv
->extraInfo
;
496 if( convData
->isTargetUCharDBCS
){
498 *p
++= UCNV_CLOSE_BRACE
;
499 convData
->isTargetUCharDBCS
=FALSE
;
501 *p
++= (char)cnv
->subChars
[0];
503 ucnv_cbFromUWriteBytes(args
,
504 buffer
, (int32_t)(p
- buffer
),
509 * Structure for cloning an HZ converter into a single memory block.
510 * ucnv_safeClone() of the HZ converter will align the entire cloneHZStruct,
511 * and then ucnv_safeClone() of the sub-converter may additionally align
512 * subCnv inside the cloneHZStruct, for which we need the deadSpace after
513 * subCnv. This is because UAlignedMemory may be larger than the actually
514 * necessary alignment size for the platform.
515 * The other cloneHZStruct fields will not be moved around,
516 * and are aligned properly with cloneHZStruct's alignment.
522 UAlignedMemory deadSpace
;
523 UConverterDataHZ mydata
;
528 _HZ_SafeClone(const UConverter
*cnv
,
530 int32_t *pBufferSize
,
533 struct cloneHZStruct
* localClone
;
534 int32_t size
, bufferSizeNeeded
= sizeof(struct cloneHZStruct
);
536 if (U_FAILURE(*status
)){
540 if (*pBufferSize
== 0){ /* 'preflighting' request - set needed size into *pBufferSize */
541 *pBufferSize
= bufferSizeNeeded
;
545 localClone
= (struct cloneHZStruct
*)stackBuffer
;
546 /* ucnv.c/ucnv_safeClone() copied the main UConverter already */
548 uprv_memcpy(&localClone
->mydata
, cnv
->extraInfo
, sizeof(UConverterDataHZ
));
549 localClone
->cnv
.extraInfo
= &localClone
->mydata
;
550 localClone
->cnv
.isExtraLocal
= TRUE
;
552 /* deep-clone the sub-converter */
553 size
= (int32_t)(sizeof(UConverter
) + sizeof(UAlignedMemory
)); /* include size of padding */
554 ((UConverterDataHZ
*)localClone
->cnv
.extraInfo
)->gbConverter
=
555 ucnv_safeClone(((UConverterDataHZ
*)cnv
->extraInfo
)->gbConverter
, &localClone
->subCnv
, &size
, status
);
557 return &localClone
->cnv
;
561 _HZ_GetUnicodeSet(const UConverter
*cnv
,
563 UConverterUnicodeSet which
,
564 UErrorCode
*pErrorCode
) {
565 /* HZ converts all of ASCII */
566 sa
->addRange(sa
->set
, 0, 0x7f);
568 /* add all of the code points that the sub-converter handles */
569 ucnv_MBCSGetFilteredUnicodeSetForUnicode(
570 ((UConverterDataHZ
*)cnv
->extraInfo
)->gbConverter
->sharedData
,
571 sa
, which
, UCNV_SET_FILTER_HZ
,
575 static const UConverterImpl _HZImpl
={
586 UConverter_toUnicode_HZ_OFFSETS_LOGIC
,
587 UConverter_toUnicode_HZ_OFFSETS_LOGIC
,
588 UConverter_fromUnicode_HZ_OFFSETS_LOGIC
,
589 UConverter_fromUnicode_HZ_OFFSETS_LOGIC
,
599 static const UConverterStaticData _HZStaticData
={
600 sizeof(UConverterStaticData
),
613 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, /* reserved */
618 const UConverterSharedData _HZData
={
619 sizeof(UConverterSharedData
),
629 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */