| 1 |
// © 2016 and later: Unicode, Inc. and others. |
| 2 |
// License & terms of use: http://www.unicode.org/copyright.html |
| 3 |
/* |
| 4 |
********************************************************************** |
| 5 |
* Copyright (C) 2000-2015, International Business Machines |
| 6 |
* Corporation and others. All Rights Reserved. |
| 7 |
********************************************************************** |
| 8 |
* file name: ucnvhz.c |
| 9 |
* encoding: UTF-8 |
| 10 |
* tab size: 8 (not used) |
| 11 |
* indentation:4 |
| 12 |
* |
| 13 |
* created on: 2000oct16 |
| 14 |
* created by: Ram Viswanadha |
| 15 |
* 10/31/2000 Ram Implemented offsets logic function |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "unicode/utypes.h" |
| 20 |
|
| 21 |
#if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION |
| 22 |
|
| 23 |
#include "cmemory.h" |
| 24 |
#include "unicode/ucnv.h" |
| 25 |
#include "unicode/ucnv_cb.h" |
| 26 |
#include "unicode/uset.h" |
| 27 |
#include "unicode/utf16.h" |
| 28 |
#include "ucnv_bld.h" |
| 29 |
#include "ucnv_cnv.h" |
| 30 |
#include "ucnv_imp.h" |
| 31 |
|
| 32 |
#define UCNV_TILDE 0x7E /* ~ */ |
| 33 |
#define UCNV_OPEN_BRACE 0x7B /* { */ |
| 34 |
#define UCNV_CLOSE_BRACE 0x7D /* } */ |
| 35 |
#define SB_ESCAPE "\x7E\x7D" |
| 36 |
#define DB_ESCAPE "\x7E\x7B" |
| 37 |
#define TILDE_ESCAPE "\x7E\x7E" |
| 38 |
#define ESC_LEN 2 |
| 39 |
|
| 40 |
|
| 41 |
#define CONCAT_ESCAPE_MACRO(args, targetIndex,targetLength,strToAppend, err, len,sourceIndex) UPRV_BLOCK_MACRO_BEGIN { \ |
| 42 |
while(len-->0){ \ |
| 43 |
if(targetIndex < targetLength){ \ |
| 44 |
args->target[targetIndex] = (unsigned char) *strToAppend; \ |
| 45 |
if(args->offsets!=nullptr){ \ |
| 46 |
*(offsets++) = sourceIndex-1; \ |
| 47 |
} \ |
| 48 |
targetIndex++; \ |
| 49 |
} \ |
| 50 |
else{ \ |
| 51 |
args->converter->charErrorBuffer[(int)args->converter->charErrorBufferLength++] = (unsigned char) *strToAppend; \ |
| 52 |
*err =U_BUFFER_OVERFLOW_ERROR; \ |
| 53 |
} \ |
| 54 |
strToAppend++; \ |
| 55 |
} \ |
| 56 |
} UPRV_BLOCK_MACRO_END |
| 57 |
|
| 58 |
|
| 59 |
typedef struct{ |
| 60 |
UConverter* gbConverter; |
| 61 |
int32_t targetIndex; |
| 62 |
int32_t sourceIndex; |
| 63 |
UBool isEscapeAppended; |
| 64 |
UBool isStateDBCS; |
| 65 |
UBool isTargetUCharDBCS; |
| 66 |
UBool isEmptySegment; |
| 67 |
}UConverterDataHZ; |
| 68 |
|
| 69 |
|
| 70 |
U_CDECL_BEGIN |
| 71 |
static void U_CALLCONV |
| 72 |
_HZOpen(UConverter *cnv, UConverterLoadArgs *pArgs, UErrorCode *errorCode){ |
| 73 |
UConverter *gbConverter; |
| 74 |
if(pArgs->onlyTestIsLoadable) { |
| 75 |
ucnv_canCreateConverter("GBK", errorCode); /* errorCode carries result */ |
| 76 |
return; |
| 77 |
} |
| 78 |
gbConverter = ucnv_open("GBK", errorCode); |
| 79 |
if(U_FAILURE(*errorCode)) { |
| 80 |
return; |
| 81 |
} |
| 82 |
cnv->toUnicodeStatus = 0; |
| 83 |
cnv->fromUnicodeStatus= 0; |
| 84 |
cnv->mode=0; |
| 85 |
cnv->fromUChar32=0x0000; |
| 86 |
cnv->extraInfo = uprv_calloc(1, sizeof(UConverterDataHZ)); |
| 87 |
if(cnv->extraInfo != nullptr){ |
| 88 |
((UConverterDataHZ*)cnv->extraInfo)->gbConverter = gbConverter; |
| 89 |
} |
| 90 |
else { |
| 91 |
ucnv_close(gbConverter); |
| 92 |
*errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 93 |
return; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
static void U_CALLCONV |
| 98 |
_HZClose(UConverter *cnv){ |
| 99 |
if(cnv->extraInfo != nullptr) { |
| 100 |
ucnv_close (((UConverterDataHZ *) (cnv->extraInfo))->gbConverter); |
| 101 |
if(!cnv->isExtraLocal) { |
| 102 |
uprv_free(cnv->extraInfo); |
| 103 |
} |
| 104 |
cnv->extraInfo = nullptr; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
static void U_CALLCONV |
| 109 |
_HZReset(UConverter *cnv, UConverterResetChoice choice){ |
| 110 |
if(choice<=UCNV_RESET_TO_UNICODE) { |
| 111 |
cnv->toUnicodeStatus = 0; |
| 112 |
cnv->mode=0; |
| 113 |
if(cnv->extraInfo != nullptr){ |
| 114 |
((UConverterDataHZ*)cnv->extraInfo)->isStateDBCS = false; |
| 115 |
((UConverterDataHZ*)cnv->extraInfo)->isEmptySegment = false; |
| 116 |
} |
| 117 |
} |
| 118 |
if(choice!=UCNV_RESET_TO_UNICODE) { |
| 119 |
cnv->fromUnicodeStatus= 0; |
| 120 |
cnv->fromUChar32=0x0000; |
| 121 |
if(cnv->extraInfo != nullptr){ |
| 122 |
((UConverterDataHZ*)cnv->extraInfo)->isEscapeAppended = false; |
| 123 |
((UConverterDataHZ*)cnv->extraInfo)->targetIndex = 0; |
| 124 |
((UConverterDataHZ*)cnv->extraInfo)->sourceIndex = 0; |
| 125 |
((UConverterDataHZ*)cnv->extraInfo)->isTargetUCharDBCS = false; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/**************************************HZ Encoding************************************************* |
| 131 |
* Rules for HZ encoding |
| 132 |
* |
| 133 |
* In ASCII mode, a byte is interpreted as an ASCII character, unless a |
| 134 |
* '~' is encountered. The character '~' is an escape character. By |
| 135 |
* convention, it must be immediately followed ONLY by '~', '{' or '\n' |
| 136 |
* (<LF>), with the following special meaning. |
| 137 |
|
| 138 |
* 1. The escape sequence '~~' is interpreted as a '~'. |
| 139 |
* 2. The escape-to-GB sequence '~{' switches the mode from ASCII to GB. |
| 140 |
* 3. The escape sequence '~\n' is a line-continuation marker to be |
| 141 |
* consumed with no output produced. |
| 142 |
* In GB mode, characters are interpreted two bytes at a time as (pure) |
| 143 |
* GB codes until the escape-from-GB code '~}' is read. This code |
| 144 |
* switches the mode from GB back to ASCII. (Note that the escape- |
| 145 |
* from-GB code '~}' ($7E7D) is outside the defined GB range.) |
| 146 |
* |
| 147 |
* Source: RFC 1842 |
| 148 |
* |
| 149 |
* Note that the formal syntax in RFC 1842 is invalid. I assume that the |
| 150 |
* intended definition of single-byte-segment is as follows (pedberg): |
| 151 |
* single-byte-segment = single-byte-seq 1*single-byte-char |
| 152 |
*/ |
| 153 |
|
| 154 |
|
| 155 |
static void U_CALLCONV |
| 156 |
UConverter_toUnicode_HZ_OFFSETS_LOGIC(UConverterToUnicodeArgs *args, |
| 157 |
UErrorCode* err){ |
| 158 |
char tempBuf[2]; |
| 159 |
const char *mySource = ( char *) args->source; |
| 160 |
char16_t *myTarget = args->target; |
| 161 |
const char *mySourceLimit = args->sourceLimit; |
| 162 |
UChar32 targetUniChar = 0x0000; |
| 163 |
int32_t mySourceChar = 0x0000; |
| 164 |
UConverterDataHZ* myData=(UConverterDataHZ*)(args->converter->extraInfo); |
| 165 |
tempBuf[0]=0; |
| 166 |
tempBuf[1]=0; |
| 167 |
|
| 168 |
/* Calling code already handles this situation. */ |
| 169 |
/*if ((args->converter == nullptr) || (args->targetLimit < args->target) || (mySourceLimit < args->source)){ |
| 170 |
*err = U_ILLEGAL_ARGUMENT_ERROR; |
| 171 |
return; |
| 172 |
}*/ |
| 173 |
|
| 174 |
while(mySource< mySourceLimit){ |
| 175 |
|
| 176 |
if(myTarget < args->targetLimit){ |
| 177 |
|
| 178 |
mySourceChar= (unsigned char) *mySource++; |
| 179 |
|
| 180 |
if(args->converter->mode == UCNV_TILDE) { |
| 181 |
/* second byte after ~ */ |
| 182 |
args->converter->mode=0; |
| 183 |
switch(mySourceChar) { |
| 184 |
case 0x0A: |
| 185 |
/* no output for ~\n (line-continuation marker) */ |
| 186 |
continue; |
| 187 |
case UCNV_TILDE: |
| 188 |
if(args->offsets) { |
| 189 |
args->offsets[myTarget - args->target]=(int32_t)(mySource - args->source - 2); |
| 190 |
} |
| 191 |
*(myTarget++)=(char16_t)mySourceChar; |
| 192 |
myData->isEmptySegment = false; |
| 193 |
continue; |
| 194 |
case UCNV_OPEN_BRACE: |
| 195 |
case UCNV_CLOSE_BRACE: |
| 196 |
myData->isStateDBCS = (mySourceChar == UCNV_OPEN_BRACE); |
| 197 |
if (myData->isEmptySegment) { |
| 198 |
myData->isEmptySegment = false; /* we are handling it, reset to avoid future spurious errors */ |
| 199 |
*err = U_ILLEGAL_ESCAPE_SEQUENCE; |
| 200 |
args->converter->toUCallbackReason = UCNV_IRREGULAR; |
| 201 |
args->converter->toUBytes[0] = UCNV_TILDE; |
| 202 |
args->converter->toUBytes[1] = static_cast<uint8_t>(mySourceChar); |
| 203 |
args->converter->toULength = 2; |
| 204 |
args->target = myTarget; |
| 205 |
args->source = mySource; |
| 206 |
return; |
| 207 |
} |
| 208 |
myData->isEmptySegment = true; |
| 209 |
continue; |
| 210 |
default: |
| 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 |
| 213 |
*/ |
| 214 |
/* |
| 215 |
* Ticket 5691: consistent illegal sequences: |
| 216 |
* - We include at least the first byte in the illegal sequence. |
| 217 |
* - If any of the non-initial bytes could be the start of a character, |
| 218 |
* we stop the illegal sequence before the first one of those. |
| 219 |
*/ |
| 220 |
myData->isEmptySegment = false; /* different error here, reset this to avoid spurious future error */ |
| 221 |
*err = U_ILLEGAL_ESCAPE_SEQUENCE; |
| 222 |
args->converter->toUBytes[0] = UCNV_TILDE; |
| 223 |
if( myData->isStateDBCS ? |
| 224 |
(0x21 <= mySourceChar && mySourceChar <= 0x7e) : |
| 225 |
mySourceChar <= 0x7f |
| 226 |
) { |
| 227 |
/* The current byte could be the start of a character: Back it out. */ |
| 228 |
args->converter->toULength = 1; |
| 229 |
--mySource; |
| 230 |
} else { |
| 231 |
/* Include the current byte in the illegal sequence. */ |
| 232 |
args->converter->toUBytes[1] = static_cast<uint8_t>(mySourceChar); |
| 233 |
args->converter->toULength = 2; |
| 234 |
} |
| 235 |
args->target = myTarget; |
| 236 |
args->source = mySource; |
| 237 |
return; |
| 238 |
} |
| 239 |
} else if(myData->isStateDBCS) { |
| 240 |
if(args->converter->toUnicodeStatus == 0x00){ |
| 241 |
/* lead byte */ |
| 242 |
if(mySourceChar == UCNV_TILDE) { |
| 243 |
args->converter->mode = UCNV_TILDE; |
| 244 |
} else { |
| 245 |
/* add another bit to distinguish a 0 byte from not having seen a lead byte */ |
| 246 |
args->converter->toUnicodeStatus = (uint32_t) (mySourceChar | 0x100); |
| 247 |
myData->isEmptySegment = false; /* the segment has something, either valid or will produce a different error, so reset this */ |
| 248 |
} |
| 249 |
continue; |
| 250 |
} |
| 251 |
else{ |
| 252 |
/* trail byte */ |
| 253 |
int leadIsOk, trailIsOk; |
| 254 |
uint32_t leadByte = args->converter->toUnicodeStatus & 0xff; |
| 255 |
targetUniChar = 0xffff; |
| 256 |
/* |
| 257 |
* Ticket 5691: consistent illegal sequences: |
| 258 |
* - We include at least the first byte in the illegal sequence. |
| 259 |
* - If any of the non-initial bytes could be the start of a character, |
| 260 |
* we stop the illegal sequence before the first one of those. |
| 261 |
* |
| 262 |
* In HZ DBCS, if the second byte is in the 21..7e range, |
| 263 |
* we report only the first byte as the illegal sequence. |
| 264 |
* Otherwise we convert or report the pair of bytes. |
| 265 |
*/ |
| 266 |
leadIsOk = (uint8_t)(leadByte - 0x21) <= (0x7d - 0x21); |
| 267 |
trailIsOk = (uint8_t)(mySourceChar - 0x21) <= (0x7e - 0x21); |
| 268 |
if (leadIsOk && trailIsOk) { |
| 269 |
tempBuf[0] = (char) (leadByte+0x80) ; |
| 270 |
tempBuf[1] = (char) (mySourceChar+0x80); |
| 271 |
targetUniChar = ucnv_MBCSSimpleGetNextUChar(myData->gbConverter->sharedData, |
| 272 |
tempBuf, 2, args->converter->useFallback); |
| 273 |
mySourceChar= (leadByte << 8) | mySourceChar; |
| 274 |
} else if (trailIsOk) { |
| 275 |
/* report a single illegal byte and continue with the following DBCS starter byte */ |
| 276 |
--mySource; |
| 277 |
mySourceChar = (int32_t)leadByte; |
| 278 |
} else { |
| 279 |
/* report a pair of illegal bytes if the second byte is not a DBCS starter */ |
| 280 |
/* add another bit so that the code below writes 2 bytes in case of error */ |
| 281 |
mySourceChar= 0x10000 | (leadByte << 8) | mySourceChar; |
| 282 |
} |
| 283 |
args->converter->toUnicodeStatus =0x00; |
| 284 |
} |
| 285 |
} |
| 286 |
else{ |
| 287 |
if(mySourceChar == UCNV_TILDE) { |
| 288 |
args->converter->mode = UCNV_TILDE; |
| 289 |
continue; |
| 290 |
} else if(mySourceChar <= 0x7f) { |
| 291 |
targetUniChar = (char16_t)mySourceChar; /* ASCII */ |
| 292 |
myData->isEmptySegment = false; /* the segment has something valid */ |
| 293 |
} else { |
| 294 |
targetUniChar = 0xffff; |
| 295 |
myData->isEmptySegment = false; /* different error here, reset this to avoid spurious future error */ |
| 296 |
} |
| 297 |
} |
| 298 |
if(targetUniChar < 0xfffe){ |
| 299 |
if(args->offsets) { |
| 300 |
args->offsets[myTarget - args->target]=(int32_t)(mySource - args->source - 1-(myData->isStateDBCS)); |
| 301 |
} |
| 302 |
|
| 303 |
*(myTarget++)=(char16_t)targetUniChar; |
| 304 |
} |
| 305 |
else /* targetUniChar>=0xfffe */ { |
| 306 |
if(targetUniChar == 0xfffe){ |
| 307 |
*err = U_INVALID_CHAR_FOUND; |
| 308 |
} |
| 309 |
else{ |
| 310 |
*err = U_ILLEGAL_CHAR_FOUND; |
| 311 |
} |
| 312 |
if(mySourceChar > 0xff){ |
| 313 |
args->converter->toUBytes[0] = (uint8_t)(mySourceChar >> 8); |
| 314 |
args->converter->toUBytes[1] = (uint8_t)mySourceChar; |
| 315 |
args->converter->toULength=2; |
| 316 |
} |
| 317 |
else{ |
| 318 |
args->converter->toUBytes[0] = (uint8_t)mySourceChar; |
| 319 |
args->converter->toULength=1; |
| 320 |
} |
| 321 |
break; |
| 322 |
} |
| 323 |
} |
| 324 |
else{ |
| 325 |
*err =U_BUFFER_OVERFLOW_ERROR; |
| 326 |
break; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
args->target = myTarget; |
| 331 |
args->source = mySource; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
static void U_CALLCONV |
| 336 |
UConverter_fromUnicode_HZ_OFFSETS_LOGIC (UConverterFromUnicodeArgs * args, |
| 337 |
UErrorCode * err){ |
| 338 |
const char16_t *mySource = args->source; |
| 339 |
char *myTarget = args->target; |
| 340 |
int32_t* offsets = args->offsets; |
| 341 |
int32_t mySourceIndex = 0; |
| 342 |
int32_t myTargetIndex = 0; |
| 343 |
int32_t targetLength = (int32_t)(args->targetLimit - myTarget); |
| 344 |
int32_t mySourceLength = (int32_t)(args->sourceLimit - args->source); |
| 345 |
uint32_t targetUniChar = 0x0000; |
| 346 |
UChar32 mySourceChar = 0x0000; |
| 347 |
UConverterDataHZ *myConverterData=(UConverterDataHZ*)args->converter->extraInfo; |
| 348 |
UBool isTargetUCharDBCS = myConverterData->isTargetUCharDBCS; |
| 349 |
UBool oldIsTargetUCharDBCS; |
| 350 |
int len =0; |
| 351 |
const char* escSeq=nullptr; |
| 352 |
|
| 353 |
/* Calling code already handles this situation. */ |
| 354 |
/*if ((args->converter == nullptr) || (args->targetLimit < myTarget) || (args->sourceLimit < args->source)){ |
| 355 |
*err = U_ILLEGAL_ARGUMENT_ERROR; |
| 356 |
return; |
| 357 |
}*/ |
| 358 |
if(args->converter->fromUChar32!=0 && myTargetIndex < targetLength) { |
| 359 |
goto getTrail; |
| 360 |
} |
| 361 |
/*writing the char to the output stream */ |
| 362 |
while (mySourceIndex < mySourceLength){ |
| 363 |
targetUniChar = missingCharMarker; |
| 364 |
if (myTargetIndex < targetLength){ |
| 365 |
|
| 366 |
mySourceChar = mySource[mySourceIndex++]; |
| 367 |
|
| 368 |
|
| 369 |
oldIsTargetUCharDBCS = isTargetUCharDBCS; |
| 370 |
if(mySourceChar ==UCNV_TILDE){ |
| 371 |
/*concatEscape(args, &myTargetIndex, &targetLength,"\x7E\x7E",err,2,&mySourceIndex);*/ |
| 372 |
len = ESC_LEN; |
| 373 |
escSeq = TILDE_ESCAPE; |
| 374 |
CONCAT_ESCAPE_MACRO(args, myTargetIndex, targetLength, escSeq,err,len,mySourceIndex); |
| 375 |
continue; |
| 376 |
} else if(mySourceChar <= 0x7f) { |
| 377 |
targetUniChar = mySourceChar; |
| 378 |
} else { |
| 379 |
int32_t length= ucnv_MBCSFromUChar32(myConverterData->gbConverter->sharedData, |
| 380 |
mySourceChar,&targetUniChar,args->converter->useFallback); |
| 381 |
/* we can only use lead bytes 21..7D and trail bytes 21..7E */ |
| 382 |
if( length == 2 && |
| 383 |
(uint16_t)(targetUniChar - 0xa1a1) <= (0xfdfe - 0xa1a1) && |
| 384 |
(uint8_t)(targetUniChar - 0xa1) <= (0xfe - 0xa1) |
| 385 |
) { |
| 386 |
targetUniChar -= 0x8080; |
| 387 |
} else { |
| 388 |
targetUniChar = missingCharMarker; |
| 389 |
} |
| 390 |
} |
| 391 |
if (targetUniChar != missingCharMarker){ |
| 392 |
myConverterData->isTargetUCharDBCS = isTargetUCharDBCS = targetUniChar > 0x00FF; |
| 393 |
if(oldIsTargetUCharDBCS != isTargetUCharDBCS || !myConverterData->isEscapeAppended ){ |
| 394 |
/*Shifting from a double byte to single byte mode*/ |
| 395 |
if(!isTargetUCharDBCS){ |
| 396 |
len =ESC_LEN; |
| 397 |
escSeq = SB_ESCAPE; |
| 398 |
CONCAT_ESCAPE_MACRO(args, myTargetIndex, targetLength, escSeq,err,len,mySourceIndex); |
| 399 |
myConverterData->isEscapeAppended = true; |
| 400 |
} |
| 401 |
else{ /* Shifting from a single byte to double byte mode*/ |
| 402 |
len =ESC_LEN; |
| 403 |
escSeq = DB_ESCAPE; |
| 404 |
CONCAT_ESCAPE_MACRO(args, myTargetIndex, targetLength, escSeq,err,len,mySourceIndex); |
| 405 |
myConverterData->isEscapeAppended = true; |
| 406 |
|
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
if(isTargetUCharDBCS){ |
| 411 |
if( myTargetIndex <targetLength){ |
| 412 |
myTarget[myTargetIndex++] =(char) (targetUniChar >> 8); |
| 413 |
if(offsets){ |
| 414 |
*(offsets++) = mySourceIndex-1; |
| 415 |
} |
| 416 |
if(myTargetIndex < targetLength){ |
| 417 |
myTarget[myTargetIndex++] =(char) targetUniChar; |
| 418 |
if(offsets){ |
| 419 |
*(offsets++) = mySourceIndex-1; |
| 420 |
} |
| 421 |
}else{ |
| 422 |
args->converter->charErrorBuffer[args->converter->charErrorBufferLength++] = (char) targetUniChar; |
| 423 |
*err = U_BUFFER_OVERFLOW_ERROR; |
| 424 |
} |
| 425 |
}else{ |
| 426 |
args->converter->charErrorBuffer[args->converter->charErrorBufferLength++] =(char) (targetUniChar >> 8); |
| 427 |
args->converter->charErrorBuffer[args->converter->charErrorBufferLength++] = (char) targetUniChar; |
| 428 |
*err = U_BUFFER_OVERFLOW_ERROR; |
| 429 |
} |
| 430 |
|
| 431 |
}else{ |
| 432 |
if( myTargetIndex <targetLength){ |
| 433 |
myTarget[myTargetIndex++] = (char) (targetUniChar ); |
| 434 |
if(offsets){ |
| 435 |
*(offsets++) = mySourceIndex-1; |
| 436 |
} |
| 437 |
|
| 438 |
}else{ |
| 439 |
args->converter->charErrorBuffer[args->converter->charErrorBufferLength++] = (char) targetUniChar; |
| 440 |
*err = U_BUFFER_OVERFLOW_ERROR; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
} |
| 445 |
else{ |
| 446 |
/* oops.. the code point is unassigned */ |
| 447 |
/*Handle surrogates */ |
| 448 |
/*check if the char is a First surrogate*/ |
| 449 |
if(U16_IS_SURROGATE(mySourceChar)) { |
| 450 |
if(U16_IS_SURROGATE_LEAD(mySourceChar)) { |
| 451 |
args->converter->fromUChar32=mySourceChar; |
| 452 |
getTrail: |
| 453 |
/*look ahead to find the trail surrogate*/ |
| 454 |
if(mySourceIndex < mySourceLength) { |
| 455 |
/* test the following code unit */ |
| 456 |
char16_t trail=(char16_t) args->source[mySourceIndex]; |
| 457 |
if(U16_IS_TRAIL(trail)) { |
| 458 |
++mySourceIndex; |
| 459 |
mySourceChar=U16_GET_SUPPLEMENTARY(args->converter->fromUChar32, trail); |
| 460 |
args->converter->fromUChar32=0x00; |
| 461 |
/* there are no surrogates in GB2312*/ |
| 462 |
*err = U_INVALID_CHAR_FOUND; |
| 463 |
/* exit this condition tree */ |
| 464 |
} else { |
| 465 |
/* this is an unmatched lead code unit (1st surrogate) */ |
| 466 |
/* callback(illegal) */ |
| 467 |
*err=U_ILLEGAL_CHAR_FOUND; |
| 468 |
} |
| 469 |
} else { |
| 470 |
/* no more input */ |
| 471 |
*err = U_ZERO_ERROR; |
| 472 |
} |
| 473 |
} else { |
| 474 |
/* this is an unmatched trail code unit (2nd surrogate) */ |
| 475 |
/* callback(illegal) */ |
| 476 |
*err=U_ILLEGAL_CHAR_FOUND; |
| 477 |
} |
| 478 |
} else { |
| 479 |
/* callback(unassigned) for a BMP code point */ |
| 480 |
*err = U_INVALID_CHAR_FOUND; |
| 481 |
} |
| 482 |
|
| 483 |
args->converter->fromUChar32=mySourceChar; |
| 484 |
break; |
| 485 |
} |
| 486 |
} |
| 487 |
else{ |
| 488 |
*err = U_BUFFER_OVERFLOW_ERROR; |
| 489 |
break; |
| 490 |
} |
| 491 |
targetUniChar=missingCharMarker; |
| 492 |
} |
| 493 |
|
| 494 |
args->target += myTargetIndex; |
| 495 |
args->source += mySourceIndex; |
| 496 |
myConverterData->isTargetUCharDBCS = isTargetUCharDBCS; |
| 497 |
} |
| 498 |
|
| 499 |
static void U_CALLCONV |
| 500 |
_HZ_WriteSub(UConverterFromUnicodeArgs *args, int32_t offsetIndex, UErrorCode *err) { |
| 501 |
UConverter *cnv = args->converter; |
| 502 |
UConverterDataHZ *convData=(UConverterDataHZ *) cnv->extraInfo; |
| 503 |
char *p; |
| 504 |
char buffer[4]; |
| 505 |
p = buffer; |
| 506 |
|
| 507 |
if( convData->isTargetUCharDBCS){ |
| 508 |
*p++= UCNV_TILDE; |
| 509 |
*p++= UCNV_CLOSE_BRACE; |
| 510 |
convData->isTargetUCharDBCS=false; |
| 511 |
} |
| 512 |
*p++= (char)cnv->subChars[0]; |
| 513 |
|
| 514 |
ucnv_cbFromUWriteBytes(args, |
| 515 |
buffer, (int32_t)(p - buffer), |
| 516 |
offsetIndex, err); |
| 517 |
} |
| 518 |
|
| 519 |
/* |
| 520 |
* Structure for cloning an HZ converter into a single memory block. |
| 521 |
*/ |
| 522 |
struct cloneHZStruct |
| 523 |
{ |
| 524 |
UConverter cnv; |
| 525 |
UConverter subCnv; |
| 526 |
UConverterDataHZ mydata; |
| 527 |
}; |
| 528 |
|
| 529 |
|
| 530 |
static UConverter * U_CALLCONV |
| 531 |
_HZ_SafeClone(const UConverter *cnv, |
| 532 |
void *stackBuffer, |
| 533 |
int32_t *pBufferSize, |
| 534 |
UErrorCode *status) |
| 535 |
{ |
| 536 |
struct cloneHZStruct * localClone; |
| 537 |
int32_t size, bufferSizeNeeded = sizeof(struct cloneHZStruct); |
| 538 |
|
| 539 |
if (U_FAILURE(*status)){ |
| 540 |
return nullptr; |
| 541 |
} |
| 542 |
|
| 543 |
if (*pBufferSize == 0){ /* 'preflighting' request - set needed size into *pBufferSize */ |
| 544 |
*pBufferSize = bufferSizeNeeded; |
| 545 |
return nullptr; |
| 546 |
} |
| 547 |
|
| 548 |
localClone = (struct cloneHZStruct *)stackBuffer; |
| 549 |
/* ucnv.c/ucnv_safeClone() copied the main UConverter already */ |
| 550 |
|
| 551 |
uprv_memcpy(&localClone->mydata, cnv->extraInfo, sizeof(UConverterDataHZ)); |
| 552 |
localClone->cnv.extraInfo = &localClone->mydata; |
| 553 |
localClone->cnv.isExtraLocal = true; |
| 554 |
|
| 555 |
/* deep-clone the sub-converter */ |
| 556 |
size = (int32_t)sizeof(UConverter); |
| 557 |
((UConverterDataHZ*)localClone->cnv.extraInfo)->gbConverter = |
| 558 |
ucnv_safeClone(((UConverterDataHZ*)cnv->extraInfo)->gbConverter, &localClone->subCnv, &size, status); |
| 559 |
|
| 560 |
return &localClone->cnv; |
| 561 |
} |
| 562 |
|
| 563 |
static void U_CALLCONV |
| 564 |
_HZ_GetUnicodeSet(const UConverter *cnv, |
| 565 |
const USetAdder *sa, |
| 566 |
UConverterUnicodeSet which, |
| 567 |
UErrorCode *pErrorCode) { |
| 568 |
/* HZ converts all of ASCII */ |
| 569 |
sa->addRange(sa->set, 0, 0x7f); |
| 570 |
|
| 571 |
/* add all of the code points that the sub-converter handles */ |
| 572 |
ucnv_MBCSGetFilteredUnicodeSetForUnicode( |
| 573 |
((UConverterDataHZ*)cnv->extraInfo)->gbConverter->sharedData, |
| 574 |
sa, which, UCNV_SET_FILTER_HZ, |
| 575 |
pErrorCode); |
| 576 |
} |
| 577 |
U_CDECL_END |
| 578 |
static const UConverterImpl _HZImpl={ |
| 579 |
|
| 580 |
UCNV_HZ, |
| 581 |
|
| 582 |
nullptr, |
| 583 |
nullptr, |
| 584 |
|
| 585 |
_HZOpen, |
| 586 |
_HZClose, |
| 587 |
_HZReset, |
| 588 |
|
| 589 |
UConverter_toUnicode_HZ_OFFSETS_LOGIC, |
| 590 |
UConverter_toUnicode_HZ_OFFSETS_LOGIC, |
| 591 |
UConverter_fromUnicode_HZ_OFFSETS_LOGIC, |
| 592 |
UConverter_fromUnicode_HZ_OFFSETS_LOGIC, |
| 593 |
nullptr, |
| 594 |
|
| 595 |
nullptr, |
| 596 |
nullptr, |
| 597 |
_HZ_WriteSub, |
| 598 |
_HZ_SafeClone, |
| 599 |
_HZ_GetUnicodeSet, |
| 600 |
nullptr, |
| 601 |
nullptr |
| 602 |
}; |
| 603 |
|
| 604 |
static const UConverterStaticData _HZStaticData={ |
| 605 |
sizeof(UConverterStaticData), |
| 606 |
"HZ", |
| 607 |
0, |
| 608 |
UCNV_IBM, |
| 609 |
UCNV_HZ, |
| 610 |
1, |
| 611 |
4, |
| 612 |
{ 0x1a, 0, 0, 0 }, |
| 613 |
1, |
| 614 |
false, |
| 615 |
false, |
| 616 |
0, |
| 617 |
0, |
| 618 |
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, /* reserved */ |
| 619 |
|
| 620 |
}; |
| 621 |
|
| 622 |
const UConverterSharedData _HZData= |
| 623 |
UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(&_HZStaticData, &_HZImpl); |
| 624 |
|
| 625 |
#endif /* #if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION */ |