2 **********************************************************************
3 * Copyright (C) 2000-2004, 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; \
58 UBool isEscapeAppended
;
59 UConverter
* gbConverter
;
61 UBool isTargetUCharDBCS
;
67 _HZOpen(UConverter
*cnv
, const char *name
,const char *locale
,uint32_t options
, UErrorCode
*errorCode
){
68 cnv
->toUnicodeStatus
= 0;
69 cnv
->fromUnicodeStatus
= 0;
71 cnv
->fromUChar32
=0x0000;
72 cnv
->extraInfo
= uprv_malloc (sizeof (UConverterDataHZ
));
73 if(cnv
->extraInfo
!= NULL
){
74 ((UConverterDataHZ
*)cnv
->extraInfo
)->gbConverter
= ucnv_open("ibm-1386",errorCode
);
75 ((UConverterDataHZ
*)cnv
->extraInfo
)->isStateDBCS
= FALSE
;
76 ((UConverterDataHZ
*)cnv
->extraInfo
)->isEscapeAppended
= FALSE
;
77 ((UConverterDataHZ
*)cnv
->extraInfo
)->targetIndex
= 0;
78 ((UConverterDataHZ
*)cnv
->extraInfo
)->sourceIndex
= 0;
79 ((UConverterDataHZ
*)cnv
->extraInfo
)->isTargetUCharDBCS
= FALSE
;
83 *errorCode
= U_MEMORY_ALLOCATION_ERROR
;
89 _HZClose(UConverter
*cnv
){
90 if(cnv
->extraInfo
!= NULL
) {
91 ucnv_close (((UConverterDataHZ
*) (cnv
->extraInfo
))->gbConverter
);
92 if(!cnv
->isExtraLocal
) {
93 uprv_free(cnv
->extraInfo
);
95 cnv
->extraInfo
= NULL
;
100 _HZReset(UConverter
*cnv
, UConverterResetChoice choice
){
101 if(choice
<=UCNV_RESET_TO_UNICODE
) {
102 cnv
->toUnicodeStatus
= 0;
104 if(cnv
->extraInfo
!= NULL
){
105 ((UConverterDataHZ
*)cnv
->extraInfo
)->isStateDBCS
= FALSE
;
108 if(choice
!=UCNV_RESET_TO_UNICODE
) {
109 cnv
->fromUnicodeStatus
= 0;
110 cnv
->fromUChar32
=0x0000;
111 if(cnv
->extraInfo
!= NULL
){
112 ((UConverterDataHZ
*)cnv
->extraInfo
)->isEscapeAppended
= FALSE
;
113 ((UConverterDataHZ
*)cnv
->extraInfo
)->targetIndex
= 0;
114 ((UConverterDataHZ
*)cnv
->extraInfo
)->sourceIndex
= 0;
115 ((UConverterDataHZ
*)cnv
->extraInfo
)->isTargetUCharDBCS
= FALSE
;
120 /**************************************HZ Encoding*************************************************
121 * Rules for HZ encoding
123 * In ASCII mode, a byte is interpreted as an ASCII character, unless a
124 * '~' is encountered. The character '~' is an escape character. By
125 * convention, it must be immediately followed ONLY by '~', '{' or '\n'
126 * (<LF>), with the following special meaning.
128 * 1. The escape sequence '~~' is interpreted as a '~'.
129 * 2. The escape-to-GB sequence '~{' switches the mode from ASCII to GB.
130 * 3. The escape sequence '~\n' is a line-continuation marker to be
131 * consumed with no output produced.
132 * In GB mode, characters are interpreted two bytes at a time as (pure)
133 * GB codes until the escape-from-GB code '~}' is read. This code
134 * switches the mode from GB back to ASCII. (Note that the escape-
135 * from-GB code '~}' ($7E7D) is outside the defined GB range.)
142 UConverter_toUnicode_HZ_OFFSETS_LOGIC(UConverterToUnicodeArgs
*args
,
145 const char *mySource
= ( char *) args
->source
;
146 UChar
*myTarget
= args
->target
;
147 const char *mySourceLimit
= args
->sourceLimit
;
148 UChar32 targetUniChar
= 0x0000;
149 UChar mySourceChar
= 0x0000;
150 UConverterDataHZ
* myData
=(UConverterDataHZ
*)(args
->converter
->extraInfo
);
152 if ((args
->converter
== NULL
) || (args
->targetLimit
< args
->target
) || (mySourceLimit
< args
->source
)){
153 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
157 while(mySource
< mySourceLimit
){
159 if(myTarget
< args
->targetLimit
){
161 mySourceChar
= (unsigned char) *mySource
++;
163 switch(mySourceChar
){
165 if(args
->converter
->mode
==UCNV_TILDE
){
166 args
->converter
->mode
=0;
169 *(myTarget
++)=(UChar
)mySourceChar
;
173 if(args
->converter
->mode
==UCNV_TILDE
){
174 *(myTarget
++)=(UChar
)mySourceChar
;
175 args
->converter
->mode
=0;
179 else if(args
->converter
->toUnicodeStatus
!=0){
180 args
->converter
->mode
=0;
184 args
->converter
->mode
= UCNV_TILDE
;
189 case UCNV_OPEN_BRACE
:
190 if(args
->converter
->mode
== UCNV_TILDE
){
191 args
->converter
->mode
=0;
192 myData
->isStateDBCS
= TRUE
;
200 case UCNV_CLOSE_BRACE
:
201 if(args
->converter
->mode
== UCNV_TILDE
){
202 args
->converter
->mode
=0;
203 myData
->isStateDBCS
= FALSE
;
211 /* if the first byte is equal to TILDE and the trail byte
212 * is not a valid byte then it is an error condition
214 if(args
->converter
->mode
== UCNV_TILDE
){
215 args
->converter
->mode
=0;
216 mySourceChar
= (UChar
)(((UCNV_TILDE
+0x80) << 8) | ((mySourceChar
& 0x00ff)+0x80));
224 if(myData
->isStateDBCS
){
225 if(args
->converter
->toUnicodeStatus
== 0x00){
226 args
->converter
->toUnicodeStatus
= (UChar
) mySourceChar
;
230 tempBuf
[0] = (char) (args
->converter
->toUnicodeStatus
+0x80) ;
231 tempBuf
[1] = (char) (mySourceChar
+0x80);
232 mySourceChar
= (UChar
)(((args
->converter
->toUnicodeStatus
+0x80) << 8) | ((mySourceChar
& 0x00ff)+0x80));
233 args
->converter
->toUnicodeStatus
=0x00;
234 targetUniChar
= ucnv_MBCSSimpleGetNextUChar(myData
->gbConverter
->sharedData
,
235 tempBuf
, 2, args
->converter
->useFallback
);
239 if(args
->converter
->fromUnicodeStatus
== 0x00){
240 targetUniChar
= ucnv_MBCSSimpleGetNextUChar(myData
->gbConverter
->sharedData
,
241 mySource
- 1, 1, args
->converter
->useFallback
);
248 if(targetUniChar
< 0xfffe){
250 args
->offsets
[myTarget
- args
->target
]=(int32_t)(mySource
- args
->source
- 1-(myData
->isStateDBCS
));
253 *(myTarget
++)=(UChar
)targetUniChar
;
255 else if(targetUniChar
>=0xfffe){
257 if(targetUniChar
== 0xfffe){
258 *err
= U_INVALID_CHAR_FOUND
;
261 *err
= U_ILLEGAL_CHAR_FOUND
;
263 if(myData
->isStateDBCS
){
264 args
->converter
->toUBytes
[0] = (uint8_t)(tempBuf
[0]-0x80);
265 args
->converter
->toUBytes
[1] = (uint8_t)(tempBuf
[1]-0x80);
266 args
->converter
->toULength
=2;
269 args
->converter
->toUBytes
[0] = (uint8_t)mySourceChar
;
270 args
->converter
->toULength
=1;
276 *err
=U_BUFFER_OVERFLOW_ERROR
;
281 args
->target
= myTarget
;
282 args
->source
= mySource
;
287 UConverter_fromUnicode_HZ_OFFSETS_LOGIC (UConverterFromUnicodeArgs
* args
,
289 const UChar
*mySource
= args
->source
;
290 char *myTarget
= args
->target
;
291 int32_t* offsets
= args
->offsets
;
292 int32_t mySourceIndex
= 0;
293 int32_t myTargetIndex
= 0;
294 int32_t targetLength
= (int32_t)(args
->targetLimit
- myTarget
);
295 int32_t mySourceLength
= (int32_t)(args
->sourceLimit
- args
->source
);
297 uint32_t targetUniChar
= 0x0000;
298 UChar32 mySourceChar
= 0x0000,c
=0x0000;
299 UConverterDataHZ
*myConverterData
=(UConverterDataHZ
*)args
->converter
->extraInfo
;
300 UBool isTargetUCharDBCS
= (UBool
) myConverterData
->isTargetUCharDBCS
;
301 UBool oldIsTargetUCharDBCS
= isTargetUCharDBCS
;
302 UBool isEscapeAppended
=FALSE
;
304 const char* escSeq
=NULL
;
306 if ((args
->converter
== NULL
) || (args
->targetLimit
< myTarget
) || (args
->sourceLimit
< args
->source
)){
307 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
310 if(args
->converter
->fromUChar32
!=0 && myTargetIndex
< targetLength
) {
313 /*writing the char to the output stream */
314 while (mySourceIndex
< mySourceLength
){
315 targetUniChar
= missingCharMarker
;
316 if (myTargetIndex
< targetLength
){
318 c
=mySourceChar
= (UChar
) mySource
[mySourceIndex
++];
321 oldIsTargetUCharDBCS
= isTargetUCharDBCS
;
322 if(mySourceChar
==UCNV_TILDE
){
323 /*concatEscape(args, &myTargetIndex, &targetLength,"\x7E\x7E",err,2,&mySourceIndex);*/
325 escSeq
= TILDE_ESCAPE
;
326 CONCAT_ESCAPE_MACRO(args
, myTargetIndex
, targetLength
, escSeq
,err
,len
,mySourceIndex
);
330 length
= ucnv_MBCSFromUChar32(myConverterData
->gbConverter
->sharedData
,
331 mySourceChar
,&targetUniChar
,args
->converter
->useFallback
);
334 /* only DBCS or SBCS characters are expected*/
335 /* DB haracters with high bit set to 1 are expected */
336 if(length
> 2 || length
==0 ||(((targetUniChar
& 0x8080) != 0x8080)&& length
==2)){
337 targetUniChar
= missingCharMarker
;
339 if (targetUniChar
!= missingCharMarker
){
340 myConverterData
->isTargetUCharDBCS
= isTargetUCharDBCS
= (UBool
)(targetUniChar
>0x00FF);
341 if(oldIsTargetUCharDBCS
!= isTargetUCharDBCS
|| !myConverterData
->isEscapeAppended
){
342 /*Shifting from a double byte to single byte mode*/
343 if(!isTargetUCharDBCS
){
346 CONCAT_ESCAPE_MACRO(args
, myTargetIndex
, targetLength
, escSeq
,err
,len
,mySourceIndex
);
347 myConverterData
->isEscapeAppended
=isEscapeAppended
=TRUE
;
349 else{ /* Shifting from a single byte to double byte mode*/
352 CONCAT_ESCAPE_MACRO(args
, myTargetIndex
, targetLength
, escSeq
,err
,len
,mySourceIndex
);
353 myConverterData
->isEscapeAppended
=isEscapeAppended
=TRUE
;
358 if(isTargetUCharDBCS
){
359 if( myTargetIndex
<targetLength
){
360 myTarget
[myTargetIndex
++] =(char) ((targetUniChar
>> 8) -0x80);
362 *(offsets
++) = mySourceIndex
-1;
364 if(myTargetIndex
< targetLength
){
365 myTarget
[myTargetIndex
++] =(char) ((targetUniChar
& 0x00FF) -0x80);
367 *(offsets
++) = mySourceIndex
-1;
370 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] = (char) ((targetUniChar
& 0x00FF) -0x80);
371 *err
= U_BUFFER_OVERFLOW_ERROR
;
374 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] =(char) ((targetUniChar
>> 8) -0x80);
375 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] = (char) ((targetUniChar
& 0x00FF) -0x80);
376 *err
= U_BUFFER_OVERFLOW_ERROR
;
380 if( myTargetIndex
<targetLength
){
381 myTarget
[myTargetIndex
++] = (char) (targetUniChar
);
383 *(offsets
++) = mySourceIndex
-1;
387 args
->converter
->charErrorBuffer
[args
->converter
->charErrorBufferLength
++] = (char) targetUniChar
;
388 *err
= U_BUFFER_OVERFLOW_ERROR
;
394 /* oops.. the code point is unassigned */
395 /*Handle surrogates */
396 /*check if the char is a First surrogate*/
397 if(UTF_IS_SURROGATE(mySourceChar
)) {
398 if(UTF_IS_SURROGATE_FIRST(mySourceChar
)) {
399 args
->converter
->fromUChar32
=mySourceChar
;
401 /*look ahead to find the trail surrogate*/
402 if(mySourceIndex
< mySourceLength
) {
403 /* test the following code unit */
404 UChar trail
=(UChar
) args
->source
[mySourceIndex
];
405 if(UTF_IS_SECOND_SURROGATE(trail
)) {
407 mySourceChar
=UTF16_GET_PAIR_VALUE(args
->converter
->fromUChar32
, trail
);
408 args
->converter
->fromUChar32
=0x00;
409 /* there are no surrogates in GB2312*/
410 *err
= U_INVALID_CHAR_FOUND
;
411 /* exit this condition tree */
413 /* this is an unmatched lead code unit (1st surrogate) */
414 /* callback(illegal) */
415 *err
=U_ILLEGAL_CHAR_FOUND
;
422 /* this is an unmatched trail code unit (2nd surrogate) */
423 /* callback(illegal) */
424 *err
=U_ILLEGAL_CHAR_FOUND
;
427 /* callback(unassigned) for a BMP code point */
428 *err
= U_INVALID_CHAR_FOUND
;
431 args
->converter
->fromUChar32
=mySourceChar
;
436 *err
= U_BUFFER_OVERFLOW_ERROR
;
439 targetUniChar
=missingCharMarker
;
442 args
->target
+= myTargetIndex
;
443 args
->source
+= mySourceIndex
;
444 myConverterData
->isTargetUCharDBCS
= isTargetUCharDBCS
;
448 _HZ_WriteSub(UConverterFromUnicodeArgs
*args
, int32_t offsetIndex
, UErrorCode
*err
) {
449 UConverter
*cnv
= args
->converter
;
450 UConverterDataHZ
*convData
=(UConverterDataHZ
*) cnv
->extraInfo
;
455 if( convData
->isTargetUCharDBCS
){
457 *p
++= UCNV_CLOSE_BRACE
;
458 convData
->isTargetUCharDBCS
=FALSE
;
460 *p
++= cnv
->subChar
[0];
462 ucnv_cbFromUWriteBytes(args
,
463 buffer
, (int32_t)(p
- buffer
),
467 /* structure for SafeClone calculations */
471 UAlignedMemory deadSpace1
;
473 UAlignedMemory deadSpace2
;
474 UConverterDataHZ mydata
;
479 _HZ_SafeClone(const UConverter
*cnv
,
481 int32_t *pBufferSize
,
484 struct cloneHZStruct
* localClone
;
485 int32_t size
, bufferSizeNeeded
= sizeof(struct cloneHZStruct
);
487 if (U_FAILURE(*status
)){
491 if (*pBufferSize
== 0){ /* 'preflighting' request - set needed size into *pBufferSize */
492 *pBufferSize
= bufferSizeNeeded
;
496 localClone
= (struct cloneHZStruct
*)stackBuffer
;
497 uprv_memcpy(&localClone
->cnv
, cnv
, sizeof(UConverter
));
499 uprv_memcpy(&localClone
->mydata
, cnv
->extraInfo
, sizeof(UConverterDataHZ
));
500 localClone
->cnv
.extraInfo
= &localClone
->mydata
;
501 localClone
->cnv
.isExtraLocal
= TRUE
;
503 /* deep-clone the sub-converter */
504 size
= (int32_t)sizeof(UConverter
);
505 ((UConverterDataHZ
*)localClone
->cnv
.extraInfo
)->gbConverter
=
506 ucnv_safeClone(((UConverterDataHZ
*)cnv
->extraInfo
)->gbConverter
, &localClone
->subCnv
, &size
, status
);
508 return &localClone
->cnv
;
512 _HZ_GetUnicodeSet(const UConverter
*cnv
,
514 UConverterUnicodeSet which
,
515 UErrorCode
*pErrorCode
) {
516 /* the tilde '~' is hardcoded in the converter */
517 sa
->add(sa
->set
, 0x7e);
519 /* add all of the code points that the sub-converter handles */
520 ((UConverterDataHZ
*)cnv
->extraInfo
)->
521 gbConverter
->sharedData
->impl
->
522 getUnicodeSet(((UConverterDataHZ
*)cnv
->extraInfo
)->gbConverter
,
523 sa
, which
, pErrorCode
);
526 static const UConverterImpl _HZImpl
={
537 UConverter_toUnicode_HZ_OFFSETS_LOGIC
,
538 UConverter_toUnicode_HZ_OFFSETS_LOGIC
,
539 UConverter_fromUnicode_HZ_OFFSETS_LOGIC
,
540 UConverter_fromUnicode_HZ_OFFSETS_LOGIC
,
550 static const UConverterStaticData _HZStaticData
={
551 sizeof(UConverterStaticData
),
564 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, /* reserved */
569 const UConverterSharedData _HZData
={
570 sizeof(UConverterSharedData
),
580 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */