2016-06-15 18:58:17 +00:00
|
|
|
// Copyright (C) 2016 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2000-10-17 08:03:36 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 2000-2015, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2000-10-17 08:03:36 +00:00
|
|
|
**********************************************************************
|
|
|
|
* file name: ucnvhz.c
|
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 2000oct16
|
|
|
|
* created by: Ram Viswanadha
|
2000-11-02 04:08:12 +00:00
|
|
|
* 10/31/2000 Ram Implemented offsets logic function
|
2000-10-17 08:03:36 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2002-09-20 01:54:48 +00:00
|
|
|
|
2015-02-20 19:31:33 +00:00
|
|
|
#if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION
|
2002-09-20 01:54:48 +00:00
|
|
|
|
2000-10-17 08:03:36 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
#include "unicode/ucnv.h"
|
2001-02-02 03:51:57 +00:00
|
|
|
#include "unicode/ucnv_cb.h"
|
2003-04-05 01:33:02 +00:00
|
|
|
#include "unicode/uset.h"
|
2011-07-27 05:53:56 +00:00
|
|
|
#include "unicode/utf16.h"
|
2003-04-05 01:33:02 +00:00
|
|
|
#include "ucnv_bld.h"
|
|
|
|
#include "ucnv_cnv.h"
|
2009-03-13 22:05:01 +00:00
|
|
|
#include "ucnv_imp.h"
|
2000-10-17 08:03:36 +00:00
|
|
|
|
|
|
|
#define UCNV_TILDE 0x7E /* ~ */
|
|
|
|
#define UCNV_OPEN_BRACE 0x7B /* { */
|
|
|
|
#define UCNV_CLOSE_BRACE 0x7D /* } */
|
2001-02-02 03:51:57 +00:00
|
|
|
#define SB_ESCAPE "\x7E\x7D"
|
|
|
|
#define DB_ESCAPE "\x7E\x7B"
|
|
|
|
#define TILDE_ESCAPE "\x7E\x7E"
|
|
|
|
#define ESC_LEN 2
|
|
|
|
|
|
|
|
|
2001-07-18 23:45:04 +00:00
|
|
|
#define CONCAT_ESCAPE_MACRO( args, targetIndex,targetLength,strToAppend, err, len,sourceIndex){ \
|
|
|
|
while(len-->0){ \
|
|
|
|
if(targetIndex < targetLength){ \
|
|
|
|
args->target[targetIndex] = (unsigned char) *strToAppend; \
|
|
|
|
if(args->offsets!=NULL){ \
|
|
|
|
*(offsets++) = sourceIndex-1; \
|
|
|
|
} \
|
|
|
|
targetIndex++; \
|
|
|
|
} \
|
|
|
|
else{ \
|
|
|
|
args->converter->charErrorBuffer[(int)args->converter->charErrorBufferLength++] = (unsigned char) *strToAppend; \
|
|
|
|
*err =U_BUFFER_OVERFLOW_ERROR; \
|
|
|
|
} \
|
|
|
|
strToAppend++; \
|
|
|
|
} \
|
2000-10-17 08:03:36 +00:00
|
|
|
}
|
|
|
|
|
2001-02-02 03:51:57 +00:00
|
|
|
|
2000-10-17 08:03:36 +00:00
|
|
|
typedef struct{
|
2006-06-20 22:14:05 +00:00
|
|
|
UConverter* gbConverter;
|
2000-10-17 08:03:36 +00:00
|
|
|
int32_t targetIndex;
|
|
|
|
int32_t sourceIndex;
|
|
|
|
UBool isEscapeAppended;
|
|
|
|
UBool isStateDBCS;
|
|
|
|
UBool isTargetUCharDBCS;
|
2008-03-12 23:20:11 +00:00
|
|
|
UBool isEmptySegment;
|
2000-10-17 08:03:36 +00:00
|
|
|
}UConverterDataHZ;
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-03-22 19:57:05 +00:00
|
|
|
static void
|
2009-03-13 22:05:01 +00:00
|
|
|
_HZOpen(UConverter *cnv, UConverterLoadArgs *pArgs, UErrorCode *errorCode){
|
|
|
|
UConverter *gbConverter;
|
|
|
|
if(pArgs->onlyTestIsLoadable) {
|
2009-04-07 18:31:32 +00:00
|
|
|
ucnv_canCreateConverter("GBK", errorCode); /* errorCode carries result */
|
2009-03-13 22:05:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
gbConverter = ucnv_open("GBK", errorCode);
|
|
|
|
if(U_FAILURE(*errorCode)) {
|
|
|
|
return;
|
|
|
|
}
|
2000-10-17 08:03:36 +00:00
|
|
|
cnv->toUnicodeStatus = 0;
|
|
|
|
cnv->fromUnicodeStatus= 0;
|
|
|
|
cnv->mode=0;
|
2003-07-24 00:28:47 +00:00
|
|
|
cnv->fromUChar32=0x0000;
|
2011-11-30 17:52:09 +00:00
|
|
|
cnv->extraInfo = uprv_calloc(1, sizeof(UConverterDataHZ));
|
2000-10-17 08:03:36 +00:00
|
|
|
if(cnv->extraInfo != NULL){
|
2009-03-13 22:05:01 +00:00
|
|
|
((UConverterDataHZ*)cnv->extraInfo)->gbConverter = gbConverter;
|
2002-07-02 15:10:30 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-03-13 22:05:01 +00:00
|
|
|
ucnv_close(gbConverter);
|
2002-06-29 09:31:05 +00:00
|
|
|
*errorCode = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2000-10-17 08:03:36 +00:00
|
|
|
}
|
2002-06-29 09:31:05 +00:00
|
|
|
|
2001-03-22 19:57:05 +00:00
|
|
|
static void
|
|
|
|
_HZClose(UConverter *cnv){
|
2003-05-13 21:05:05 +00:00
|
|
|
if(cnv->extraInfo != NULL) {
|
|
|
|
ucnv_close (((UConverterDataHZ *) (cnv->extraInfo))->gbConverter);
|
|
|
|
if(!cnv->isExtraLocal) {
|
|
|
|
uprv_free(cnv->extraInfo);
|
|
|
|
}
|
|
|
|
cnv->extraInfo = NULL;
|
2001-09-05 22:51:53 +00:00
|
|
|
}
|
2000-10-17 08:03:36 +00:00
|
|
|
}
|
2001-02-20 01:08:37 +00:00
|
|
|
|
2001-09-06 18:34:57 +00:00
|
|
|
static void
|
2001-03-22 19:57:05 +00:00
|
|
|
_HZReset(UConverter *cnv, UConverterResetChoice choice){
|
2000-12-20 21:18:46 +00:00
|
|
|
if(choice<=UCNV_RESET_TO_UNICODE) {
|
|
|
|
cnv->toUnicodeStatus = 0;
|
|
|
|
cnv->mode=0;
|
|
|
|
if(cnv->extraInfo != NULL){
|
|
|
|
((UConverterDataHZ*)cnv->extraInfo)->isStateDBCS = FALSE;
|
2008-03-12 23:20:11 +00:00
|
|
|
((UConverterDataHZ*)cnv->extraInfo)->isEmptySegment = FALSE;
|
2000-12-20 21:18:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(choice!=UCNV_RESET_TO_UNICODE) {
|
|
|
|
cnv->fromUnicodeStatus= 0;
|
2003-07-24 00:28:47 +00:00
|
|
|
cnv->fromUChar32=0x0000;
|
2000-12-20 21:18:46 +00:00
|
|
|
if(cnv->extraInfo != NULL){
|
|
|
|
((UConverterDataHZ*)cnv->extraInfo)->isEscapeAppended = FALSE;
|
|
|
|
((UConverterDataHZ*)cnv->extraInfo)->targetIndex = 0;
|
|
|
|
((UConverterDataHZ*)cnv->extraInfo)->sourceIndex = 0;
|
|
|
|
((UConverterDataHZ*)cnv->extraInfo)->isTargetUCharDBCS = FALSE;
|
|
|
|
}
|
2000-10-17 08:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************HZ Encoding*************************************************
|
|
|
|
* Rules for HZ encoding
|
|
|
|
*
|
|
|
|
* In ASCII mode, a byte is interpreted as an ASCII character, unless a
|
|
|
|
* '~' is encountered. The character '~' is an escape character. By
|
|
|
|
* convention, it must be immediately followed ONLY by '~', '{' or '\n'
|
|
|
|
* (<LF>), with the following special meaning.
|
|
|
|
|
|
|
|
* 1. The escape sequence '~~' is interpreted as a '~'.
|
|
|
|
* 2. The escape-to-GB sequence '~{' switches the mode from ASCII to GB.
|
|
|
|
* 3. The escape sequence '~\n' is a line-continuation marker to be
|
|
|
|
* consumed with no output produced.
|
|
|
|
* In GB mode, characters are interpreted two bytes at a time as (pure)
|
|
|
|
* GB codes until the escape-from-GB code '~}' is read. This code
|
|
|
|
* switches the mode from GB back to ASCII. (Note that the escape-
|
|
|
|
* from-GB code '~}' ($7E7D) is outside the defined GB range.)
|
|
|
|
*
|
|
|
|
* Source: RFC 1842
|
2008-03-12 23:20:11 +00:00
|
|
|
*
|
|
|
|
* Note that the formal syntax in RFC 1842 is invalid. I assume that the
|
|
|
|
* intended definition of single-byte-segment is as follows (pedberg):
|
|
|
|
* single-byte-segment = single-byte-seq 1*single-byte-char
|
2000-10-17 08:03:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2001-09-06 18:34:57 +00:00
|
|
|
static void
|
2001-03-22 19:57:05 +00:00
|
|
|
UConverter_toUnicode_HZ_OFFSETS_LOGIC(UConverterToUnicodeArgs *args,
|
2000-10-17 08:03:36 +00:00
|
|
|
UErrorCode* err){
|
2003-11-11 18:42:09 +00:00
|
|
|
char tempBuf[2];
|
2000-10-17 08:03:36 +00:00
|
|
|
const char *mySource = ( char *) args->source;
|
|
|
|
UChar *myTarget = args->target;
|
|
|
|
const char *mySourceLimit = args->sourceLimit;
|
|
|
|
UChar32 targetUniChar = 0x0000;
|
2007-10-25 05:03:34 +00:00
|
|
|
int32_t mySourceChar = 0x0000;
|
2000-10-17 08:03:36 +00:00
|
|
|
UConverterDataHZ* myData=(UConverterDataHZ*)(args->converter->extraInfo);
|
2005-08-03 02:54:26 +00:00
|
|
|
tempBuf[0]=0;
|
|
|
|
tempBuf[1]=0;
|
2008-02-13 09:35:50 +00:00
|
|
|
|
|
|
|
/* Calling code already handles this situation. */
|
|
|
|
/*if ((args->converter == NULL) || (args->targetLimit < args->target) || (mySourceLimit < args->source)){
|
2000-10-17 08:03:36 +00:00
|
|
|
*err = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
2008-02-13 09:35:50 +00:00
|
|
|
}*/
|
2000-10-17 08:03:36 +00:00
|
|
|
|
2003-12-04 00:34:51 +00:00
|
|
|
while(mySource< mySourceLimit){
|
2000-10-17 08:03:36 +00:00
|
|
|
|
|
|
|
if(myTarget < args->targetLimit){
|
|
|
|
|
|
|
|
mySourceChar= (unsigned char) *mySource++;
|
|
|
|
|
2007-10-25 05:03:34 +00:00
|
|
|
if(args->converter->mode == UCNV_TILDE) {
|
|
|
|
/* second byte after ~ */
|
|
|
|
args->converter->mode=0;
|
|
|
|
switch(mySourceChar) {
|
2000-10-17 08:03:36 +00:00
|
|
|
case 0x0A:
|
2007-10-25 05:03:34 +00:00
|
|
|
/* no output for ~\n (line-continuation marker) */
|
2000-10-17 08:03:36 +00:00
|
|
|
continue;
|
|
|
|
case UCNV_TILDE:
|
2007-10-25 05:03:34 +00:00
|
|
|
if(args->offsets) {
|
|
|
|
args->offsets[myTarget - args->target]=(int32_t)(mySource - args->source - 2);
|
2000-10-17 08:03:36 +00:00
|
|
|
}
|
2007-10-25 05:03:34 +00:00
|
|
|
*(myTarget++)=(UChar)mySourceChar;
|
2008-03-12 23:20:11 +00:00
|
|
|
myData->isEmptySegment = FALSE;
|
2007-10-25 05:03:34 +00:00
|
|
|
continue;
|
2000-10-17 08:03:36 +00:00
|
|
|
case UCNV_OPEN_BRACE:
|
|
|
|
case UCNV_CLOSE_BRACE:
|
2008-03-12 23:20:11 +00:00
|
|
|
myData->isStateDBCS = (mySourceChar == UCNV_OPEN_BRACE);
|
|
|
|
if (myData->isEmptySegment) {
|
|
|
|
myData->isEmptySegment = FALSE; /* we are handling it, reset to avoid future spurious errors */
|
|
|
|
*err = U_ILLEGAL_ESCAPE_SEQUENCE;
|
|
|
|
args->converter->toUCallbackReason = UCNV_IRREGULAR;
|
|
|
|
args->converter->toUBytes[0] = UCNV_TILDE;
|
|
|
|
args->converter->toUBytes[1] = mySourceChar;
|
|
|
|
args->converter->toULength = 2;
|
|
|
|
args->target = myTarget;
|
|
|
|
args->source = mySource;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
myData->isEmptySegment = TRUE;
|
2007-10-25 05:03:34 +00:00
|
|
|
continue;
|
2000-10-20 02:46:16 +00:00
|
|
|
default:
|
|
|
|
/* if the first byte is equal to TILDE and the trail byte
|
|
|
|
* is not a valid byte then it is an error condition
|
|
|
|
*/
|
2008-10-10 16:30:28 +00:00
|
|
|
/*
|
|
|
|
* Ticket 5691: consistent illegal sequences:
|
|
|
|
* - We include at least the first byte in the illegal sequence.
|
|
|
|
* - If any of the non-initial bytes could be the start of a character,
|
|
|
|
* we stop the illegal sequence before the first one of those.
|
|
|
|
*/
|
2008-03-12 23:20:11 +00:00
|
|
|
myData->isEmptySegment = FALSE; /* different error here, reset this to avoid spurious future error */
|
2008-10-10 16:30:28 +00:00
|
|
|
*err = U_ILLEGAL_ESCAPE_SEQUENCE;
|
|
|
|
args->converter->toUBytes[0] = UCNV_TILDE;
|
|
|
|
if( myData->isStateDBCS ?
|
|
|
|
(0x21 <= mySourceChar && mySourceChar <= 0x7e) :
|
|
|
|
mySourceChar <= 0x7f
|
|
|
|
) {
|
|
|
|
/* The current byte could be the start of a character: Back it out. */
|
|
|
|
args->converter->toULength = 1;
|
|
|
|
--mySource;
|
|
|
|
} else {
|
|
|
|
/* Include the current byte in the illegal sequence. */
|
|
|
|
args->converter->toUBytes[1] = mySourceChar;
|
|
|
|
args->converter->toULength = 2;
|
|
|
|
}
|
|
|
|
args->target = myTarget;
|
|
|
|
args->source = mySource;
|
|
|
|
return;
|
2007-10-25 05:03:34 +00:00
|
|
|
}
|
|
|
|
} else if(myData->isStateDBCS) {
|
2000-10-17 08:03:36 +00:00
|
|
|
if(args->converter->toUnicodeStatus == 0x00){
|
2007-10-25 05:03:34 +00:00
|
|
|
/* lead byte */
|
|
|
|
if(mySourceChar == UCNV_TILDE) {
|
|
|
|
args->converter->mode = UCNV_TILDE;
|
|
|
|
} else {
|
|
|
|
/* add another bit to distinguish a 0 byte from not having seen a lead byte */
|
|
|
|
args->converter->toUnicodeStatus = (uint32_t) (mySourceChar | 0x100);
|
2008-03-12 23:20:11 +00:00
|
|
|
myData->isEmptySegment = FALSE; /* the segment has something, either valid or will produce a different error, so reset this */
|
2007-10-25 05:03:34 +00:00
|
|
|
}
|
2000-10-17 08:03:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else{
|
2007-10-25 05:03:34 +00:00
|
|
|
/* trail byte */
|
2008-10-07 04:33:51 +00:00
|
|
|
int leadIsOk, trailIsOk;
|
2007-10-25 05:03:34 +00:00
|
|
|
uint32_t leadByte = args->converter->toUnicodeStatus & 0xff;
|
2008-10-07 04:33:51 +00:00
|
|
|
targetUniChar = 0xffff;
|
|
|
|
/*
|
|
|
|
* Ticket 5691: consistent illegal sequences:
|
|
|
|
* - We include at least the first byte in the illegal sequence.
|
|
|
|
* - If any of the non-initial bytes could be the start of a character,
|
|
|
|
* we stop the illegal sequence before the first one of those.
|
|
|
|
*
|
2008-10-16 00:24:42 +00:00
|
|
|
* In HZ DBCS, if the second byte is in the 21..7e range,
|
2008-10-07 04:33:51 +00:00
|
|
|
* we report only the first byte as the illegal sequence.
|
2008-10-16 00:24:42 +00:00
|
|
|
* Otherwise we convert or report the pair of bytes.
|
2008-10-07 04:33:51 +00:00
|
|
|
*/
|
|
|
|
leadIsOk = (uint8_t)(leadByte - 0x21) <= (0x7d - 0x21);
|
|
|
|
trailIsOk = (uint8_t)(mySourceChar - 0x21) <= (0x7e - 0x21);
|
2008-10-16 00:24:42 +00:00
|
|
|
if (leadIsOk && trailIsOk) {
|
|
|
|
tempBuf[0] = (char) (leadByte+0x80) ;
|
|
|
|
tempBuf[1] = (char) (mySourceChar+0x80);
|
|
|
|
targetUniChar = ucnv_MBCSSimpleGetNextUChar(myData->gbConverter->sharedData,
|
|
|
|
tempBuf, 2, args->converter->useFallback);
|
|
|
|
mySourceChar= (leadByte << 8) | mySourceChar;
|
|
|
|
} else if (trailIsOk) {
|
|
|
|
/* report a single illegal byte and continue with the following DBCS starter byte */
|
2008-10-07 04:33:51 +00:00
|
|
|
--mySource;
|
|
|
|
mySourceChar = (int32_t)leadByte;
|
2008-10-16 00:24:42 +00:00
|
|
|
} else {
|
|
|
|
/* report a pair of illegal bytes if the second byte is not a DBCS starter */
|
|
|
|
/* add another bit so that the code below writes 2 bytes in case of error */
|
|
|
|
mySourceChar= 0x10000 | (leadByte << 8) | mySourceChar;
|
2007-10-25 05:03:34 +00:00
|
|
|
}
|
2000-10-17 08:03:36 +00:00
|
|
|
args->converter->toUnicodeStatus =0x00;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2007-10-25 05:03:34 +00:00
|
|
|
if(mySourceChar == UCNV_TILDE) {
|
|
|
|
args->converter->mode = UCNV_TILDE;
|
|
|
|
continue;
|
|
|
|
} else if(mySourceChar <= 0x7f) {
|
|
|
|
targetUniChar = (UChar)mySourceChar; /* ASCII */
|
2008-03-12 23:20:11 +00:00
|
|
|
myData->isEmptySegment = FALSE; /* the segment has something valid */
|
2007-10-25 05:03:34 +00:00
|
|
|
} else {
|
|
|
|
targetUniChar = 0xffff;
|
2008-03-12 23:20:11 +00:00
|
|
|
myData->isEmptySegment = FALSE; /* different error here, reset this to avoid spurious future error */
|
2000-10-17 08:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(targetUniChar < 0xfffe){
|
2002-05-22 22:56:27 +00:00
|
|
|
if(args->offsets) {
|
|
|
|
args->offsets[myTarget - args->target]=(int32_t)(mySource - args->source - 1-(myData->isStateDBCS));
|
|
|
|
}
|
2001-02-02 03:51:57 +00:00
|
|
|
|
2000-10-17 08:03:36 +00:00
|
|
|
*(myTarget++)=(UChar)targetUniChar;
|
|
|
|
}
|
2007-10-25 05:03:34 +00:00
|
|
|
else /* targetUniChar>=0xfffe */ {
|
2003-08-01 14:58:43 +00:00
|
|
|
if(targetUniChar == 0xfffe){
|
|
|
|
*err = U_INVALID_CHAR_FOUND;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
*err = U_ILLEGAL_CHAR_FOUND;
|
|
|
|
}
|
2007-10-25 05:03:34 +00:00
|
|
|
if(mySourceChar > 0xff){
|
|
|
|
args->converter->toUBytes[0] = (uint8_t)(mySourceChar >> 8);
|
|
|
|
args->converter->toUBytes[1] = (uint8_t)mySourceChar;
|
|
|
|
args->converter->toULength=2;
|
2003-08-01 14:58:43 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
args->converter->toUBytes[0] = (uint8_t)mySourceChar;
|
|
|
|
args->converter->toULength=1;
|
2000-10-17 08:03:36 +00:00
|
|
|
}
|
2003-08-01 14:58:43 +00:00
|
|
|
break;
|
2000-10-17 08:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
*err =U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-11-17 03:01:23 +00:00
|
|
|
|
2000-10-17 08:03:36 +00:00
|
|
|
args->target = myTarget;
|
|
|
|
args->source = mySource;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-06 18:34:57 +00:00
|
|
|
static void
|
2001-03-22 19:57:05 +00:00
|
|
|
UConverter_fromUnicode_HZ_OFFSETS_LOGIC (UConverterFromUnicodeArgs * args,
|
2001-02-02 03:51:57 +00:00
|
|
|
UErrorCode * err){
|
2000-10-17 08:03:36 +00:00
|
|
|
const UChar *mySource = args->source;
|
2003-12-04 00:34:51 +00:00
|
|
|
char *myTarget = args->target;
|
2001-02-02 03:51:57 +00:00
|
|
|
int32_t* offsets = args->offsets;
|
2000-10-17 08:03:36 +00:00
|
|
|
int32_t mySourceIndex = 0;
|
|
|
|
int32_t myTargetIndex = 0;
|
2003-12-04 00:34:51 +00:00
|
|
|
int32_t targetLength = (int32_t)(args->targetLimit - myTarget);
|
2002-05-22 22:56:27 +00:00
|
|
|
int32_t mySourceLength = (int32_t)(args->sourceLimit - args->source);
|
2000-11-29 01:04:03 +00:00
|
|
|
uint32_t targetUniChar = 0x0000;
|
2005-08-03 19:27:44 +00:00
|
|
|
UChar32 mySourceChar = 0x0000;
|
2000-10-17 08:03:36 +00:00
|
|
|
UConverterDataHZ *myConverterData=(UConverterDataHZ*)args->converter->extraInfo;
|
|
|
|
UBool isTargetUCharDBCS = (UBool) myConverterData->isTargetUCharDBCS;
|
2014-09-11 05:25:13 +00:00
|
|
|
UBool oldIsTargetUCharDBCS;
|
2001-02-02 03:51:57 +00:00
|
|
|
int len =0;
|
2001-02-03 03:15:29 +00:00
|
|
|
const char* escSeq=NULL;
|
2002-07-09 12:47:45 +00:00
|
|
|
|
2008-02-13 09:35:50 +00:00
|
|
|
/* Calling code already handles this situation. */
|
|
|
|
/*if ((args->converter == NULL) || (args->targetLimit < myTarget) || (args->sourceLimit < args->source)){
|
2000-10-17 08:03:36 +00:00
|
|
|
*err = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
2008-02-13 09:35:50 +00:00
|
|
|
}*/
|
2003-07-24 00:28:47 +00:00
|
|
|
if(args->converter->fromUChar32!=0 && myTargetIndex < targetLength) {
|
2000-11-08 23:16:20 +00:00
|
|
|
goto getTrail;
|
|
|
|
}
|
2000-10-17 08:03:36 +00:00
|
|
|
/*writing the char to the output stream */
|
2001-02-02 03:51:57 +00:00
|
|
|
while (mySourceIndex < mySourceLength){
|
2001-02-16 20:16:09 +00:00
|
|
|
targetUniChar = missingCharMarker;
|
2000-10-17 08:03:36 +00:00
|
|
|
if (myTargetIndex < targetLength){
|
|
|
|
|
2005-08-03 19:27:44 +00:00
|
|
|
mySourceChar = (UChar) mySource[mySourceIndex++];
|
2000-10-17 08:03:36 +00:00
|
|
|
|
2001-02-02 03:51:57 +00:00
|
|
|
|
2000-10-17 08:03:36 +00:00
|
|
|
oldIsTargetUCharDBCS = isTargetUCharDBCS;
|
2001-02-02 03:51:57 +00:00
|
|
|
if(mySourceChar ==UCNV_TILDE){
|
|
|
|
/*concatEscape(args, &myTargetIndex, &targetLength,"\x7E\x7E",err,2,&mySourceIndex);*/
|
|
|
|
len = ESC_LEN;
|
|
|
|
escSeq = TILDE_ESCAPE;
|
|
|
|
CONCAT_ESCAPE_MACRO(args, myTargetIndex, targetLength, escSeq,err,len,mySourceIndex);
|
2000-10-17 08:03:36 +00:00
|
|
|
continue;
|
2007-10-25 05:03:34 +00:00
|
|
|
} else if(mySourceChar <= 0x7f) {
|
|
|
|
targetUniChar = mySourceChar;
|
|
|
|
} else {
|
2014-09-11 05:25:13 +00:00
|
|
|
int32_t length= ucnv_MBCSFromUChar32(myConverterData->gbConverter->sharedData,
|
2001-02-02 03:51:57 +00:00
|
|
|
mySourceChar,&targetUniChar,args->converter->useFallback);
|
2007-10-25 05:03:34 +00:00
|
|
|
/* we can only use lead bytes 21..7D and trail bytes 21..7E */
|
|
|
|
if( length == 2 &&
|
|
|
|
(uint16_t)(targetUniChar - 0xa1a1) <= (0xfdfe - 0xa1a1) &&
|
|
|
|
(uint8_t)(targetUniChar - 0xa1) <= (0xfe - 0xa1)
|
|
|
|
) {
|
|
|
|
targetUniChar -= 0x8080;
|
|
|
|
} else {
|
|
|
|
targetUniChar = missingCharMarker;
|
|
|
|
}
|
2000-10-17 22:56:32 +00:00
|
|
|
}
|
2000-10-17 08:03:36 +00:00
|
|
|
if (targetUniChar != missingCharMarker){
|
2001-02-02 03:51:57 +00:00
|
|
|
myConverterData->isTargetUCharDBCS = isTargetUCharDBCS = (UBool)(targetUniChar>0x00FF);
|
2000-11-08 23:16:20 +00:00
|
|
|
if(oldIsTargetUCharDBCS != isTargetUCharDBCS || !myConverterData->isEscapeAppended ){
|
2000-10-17 08:03:36 +00:00
|
|
|
/*Shifting from a double byte to single byte mode*/
|
|
|
|
if(!isTargetUCharDBCS){
|
2001-02-02 03:51:57 +00:00
|
|
|
len =ESC_LEN;
|
|
|
|
escSeq = SB_ESCAPE;
|
|
|
|
CONCAT_ESCAPE_MACRO(args, myTargetIndex, targetLength, escSeq,err,len,mySourceIndex);
|
2005-08-03 19:27:44 +00:00
|
|
|
myConverterData->isEscapeAppended = TRUE;
|
2000-10-17 08:03:36 +00:00
|
|
|
}
|
|
|
|
else{ /* Shifting from a single byte to double byte mode*/
|
2001-02-02 03:51:57 +00:00
|
|
|
len =ESC_LEN;
|
|
|
|
escSeq = DB_ESCAPE;
|
|
|
|
CONCAT_ESCAPE_MACRO(args, myTargetIndex, targetLength, escSeq,err,len,mySourceIndex);
|
2005-08-03 19:27:44 +00:00
|
|
|
myConverterData->isEscapeAppended = TRUE;
|
2000-10-20 02:46:16 +00:00
|
|
|
|
2000-10-17 08:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-02 03:51:57 +00:00
|
|
|
if(isTargetUCharDBCS){
|
|
|
|
if( myTargetIndex <targetLength){
|
2007-10-25 05:03:34 +00:00
|
|
|
myTarget[myTargetIndex++] =(char) (targetUniChar >> 8);
|
2001-04-07 01:24:30 +00:00
|
|
|
if(offsets){
|
|
|
|
*(offsets++) = mySourceIndex-1;
|
|
|
|
}
|
2001-02-02 03:51:57 +00:00
|
|
|
if(myTargetIndex < targetLength){
|
2007-10-25 05:03:34 +00:00
|
|
|
myTarget[myTargetIndex++] =(char) targetUniChar;
|
2001-04-07 01:24:30 +00:00
|
|
|
if(offsets){
|
|
|
|
*(offsets++) = mySourceIndex-1;
|
|
|
|
}
|
2001-02-02 03:51:57 +00:00
|
|
|
}else{
|
2007-10-25 05:03:34 +00:00
|
|
|
args->converter->charErrorBuffer[args->converter->charErrorBufferLength++] = (char) targetUniChar;
|
2001-02-02 03:51:57 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
}else{
|
2007-10-25 05:03:34 +00:00
|
|
|
args->converter->charErrorBuffer[args->converter->charErrorBufferLength++] =(char) (targetUniChar >> 8);
|
|
|
|
args->converter->charErrorBuffer[args->converter->charErrorBufferLength++] = (char) targetUniChar;
|
2001-02-02 03:51:57 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
if( myTargetIndex <targetLength){
|
2003-12-04 00:34:51 +00:00
|
|
|
myTarget[myTargetIndex++] = (char) (targetUniChar );
|
2001-04-07 01:24:30 +00:00
|
|
|
if(offsets){
|
|
|
|
*(offsets++) = mySourceIndex-1;
|
|
|
|
}
|
2001-02-02 03:51:57 +00:00
|
|
|
|
|
|
|
}else{
|
|
|
|
args->converter->charErrorBuffer[args->converter->charErrorBufferLength++] = (char) targetUniChar;
|
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2001-04-07 01:24:30 +00:00
|
|
|
|
2000-10-17 08:03:36 +00:00
|
|
|
}
|
|
|
|
else{
|
2003-07-24 00:28:47 +00:00
|
|
|
/* oops.. the code point is unassigned */
|
2001-02-02 03:51:57 +00:00
|
|
|
/*Handle surrogates */
|
|
|
|
/*check if the char is a First surrogate*/
|
2011-07-27 05:53:56 +00:00
|
|
|
if(U16_IS_SURROGATE(mySourceChar)) {
|
|
|
|
if(U16_IS_SURROGATE_LEAD(mySourceChar)) {
|
2003-07-24 00:28:47 +00:00
|
|
|
args->converter->fromUChar32=mySourceChar;
|
2000-11-08 23:16:20 +00:00
|
|
|
getTrail:
|
2001-02-02 03:51:57 +00:00
|
|
|
/*look ahead to find the trail surrogate*/
|
|
|
|
if(mySourceIndex < mySourceLength) {
|
|
|
|
/* test the following code unit */
|
|
|
|
UChar trail=(UChar) args->source[mySourceIndex];
|
2011-07-27 05:53:56 +00:00
|
|
|
if(U16_IS_TRAIL(trail)) {
|
2001-02-02 03:51:57 +00:00
|
|
|
++mySourceIndex;
|
2011-07-27 05:53:56 +00:00
|
|
|
mySourceChar=U16_GET_SUPPLEMENTARY(args->converter->fromUChar32, trail);
|
2003-07-24 00:28:47 +00:00
|
|
|
args->converter->fromUChar32=0x00;
|
2001-02-23 04:39:32 +00:00
|
|
|
/* there are no surrogates in GB2312*/
|
2001-07-18 23:45:04 +00:00
|
|
|
*err = U_INVALID_CHAR_FOUND;
|
2001-02-02 03:51:57 +00:00
|
|
|
/* exit this condition tree */
|
|
|
|
} else {
|
|
|
|
/* this is an unmatched lead code unit (1st surrogate) */
|
|
|
|
/* callback(illegal) */
|
|
|
|
*err=U_ILLEGAL_CHAR_FOUND;
|
|
|
|
}
|
2000-11-08 23:16:20 +00:00
|
|
|
} else {
|
2001-02-02 03:51:57 +00:00
|
|
|
/* no more input */
|
|
|
|
*err = U_ZERO_ERROR;
|
2000-11-08 23:16:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2001-02-02 03:51:57 +00:00
|
|
|
/* this is an unmatched trail code unit (2nd surrogate) */
|
|
|
|
/* callback(illegal) */
|
|
|
|
*err=U_ILLEGAL_CHAR_FOUND;
|
2000-11-02 04:08:12 +00:00
|
|
|
}
|
2003-07-24 00:28:47 +00:00
|
|
|
} else {
|
|
|
|
/* callback(unassigned) for a BMP code point */
|
|
|
|
*err = U_INVALID_CHAR_FOUND;
|
2000-11-02 04:08:12 +00:00
|
|
|
}
|
|
|
|
|
2003-07-24 00:28:47 +00:00
|
|
|
args->converter->fromUChar32=mySourceChar;
|
|
|
|
break;
|
2000-11-02 04:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2000-11-08 23:16:20 +00:00
|
|
|
targetUniChar=missingCharMarker;
|
|
|
|
}
|
2000-11-17 03:01:23 +00:00
|
|
|
|
2000-11-02 04:08:12 +00:00
|
|
|
args->target += myTargetIndex;
|
|
|
|
args->source += mySourceIndex;
|
|
|
|
myConverterData->isTargetUCharDBCS = isTargetUCharDBCS;
|
2001-02-02 03:51:57 +00:00
|
|
|
}
|
|
|
|
|
2001-09-06 18:34:57 +00:00
|
|
|
static void
|
2001-02-02 03:51:57 +00:00
|
|
|
_HZ_WriteSub(UConverterFromUnicodeArgs *args, int32_t offsetIndex, UErrorCode *err) {
|
|
|
|
UConverter *cnv = args->converter;
|
|
|
|
UConverterDataHZ *convData=(UConverterDataHZ *) cnv->extraInfo;
|
|
|
|
char *p;
|
|
|
|
char buffer[4];
|
|
|
|
p = buffer;
|
2002-07-09 12:47:45 +00:00
|
|
|
|
2001-02-02 03:51:57 +00:00
|
|
|
if( convData->isTargetUCharDBCS){
|
|
|
|
*p++= UCNV_TILDE;
|
|
|
|
*p++= UCNV_CLOSE_BRACE;
|
|
|
|
convData->isTargetUCharDBCS=FALSE;
|
|
|
|
}
|
2006-07-05 23:08:50 +00:00
|
|
|
*p++= (char)cnv->subChars[0];
|
2001-02-02 03:51:57 +00:00
|
|
|
|
|
|
|
ucnv_cbFromUWriteBytes(args,
|
|
|
|
buffer, (int32_t)(p - buffer),
|
|
|
|
offsetIndex, err);
|
2000-10-17 08:03:36 +00:00
|
|
|
}
|
2001-02-16 22:22:40 +00:00
|
|
|
|
2006-06-27 16:17:06 +00:00
|
|
|
/*
|
|
|
|
* Structure for cloning an HZ converter into a single memory block.
|
|
|
|
* ucnv_safeClone() of the HZ converter will align the entire cloneHZStruct,
|
|
|
|
* and then ucnv_safeClone() of the sub-converter may additionally align
|
|
|
|
* subCnv inside the cloneHZStruct, for which we need the deadSpace after
|
|
|
|
* subCnv. This is because UAlignedMemory may be larger than the actually
|
|
|
|
* necessary alignment size for the platform.
|
|
|
|
* The other cloneHZStruct fields will not be moved around,
|
|
|
|
* and are aligned properly with cloneHZStruct's alignment.
|
|
|
|
*/
|
2004-04-08 22:49:40 +00:00
|
|
|
struct cloneHZStruct
|
2001-02-16 22:22:40 +00:00
|
|
|
{
|
2003-04-18 23:05:20 +00:00
|
|
|
UConverter cnv;
|
|
|
|
UConverter subCnv;
|
2006-06-27 16:17:06 +00:00
|
|
|
UAlignedMemory deadSpace;
|
2001-03-21 20:44:20 +00:00
|
|
|
UConverterDataHZ mydata;
|
2001-02-16 22:22:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-09-06 18:34:57 +00:00
|
|
|
static UConverter *
|
2001-03-21 20:44:20 +00:00
|
|
|
_HZ_SafeClone(const UConverter *cnv,
|
|
|
|
void *stackBuffer,
|
|
|
|
int32_t *pBufferSize,
|
|
|
|
UErrorCode *status)
|
2001-02-16 22:22:40 +00:00
|
|
|
{
|
2004-04-08 22:49:40 +00:00
|
|
|
struct cloneHZStruct * localClone;
|
|
|
|
int32_t size, bufferSizeNeeded = sizeof(struct cloneHZStruct);
|
2001-02-16 22:22:40 +00:00
|
|
|
|
|
|
|
if (U_FAILURE(*status)){
|
|
|
|
return 0;
|
|
|
|
}
|
2001-03-21 20:44:20 +00:00
|
|
|
|
|
|
|
if (*pBufferSize == 0){ /* 'preflighting' request - set needed size into *pBufferSize */
|
|
|
|
*pBufferSize = bufferSizeNeeded;
|
|
|
|
return 0;
|
2001-02-16 22:22:40 +00:00
|
|
|
}
|
|
|
|
|
2004-04-08 22:49:40 +00:00
|
|
|
localClone = (struct cloneHZStruct *)stackBuffer;
|
2006-06-27 16:17:06 +00:00
|
|
|
/* ucnv.c/ucnv_safeClone() copied the main UConverter already */
|
2001-03-21 20:44:20 +00:00
|
|
|
|
2001-07-18 23:45:04 +00:00
|
|
|
uprv_memcpy(&localClone->mydata, cnv->extraInfo, sizeof(UConverterDataHZ));
|
2001-03-21 20:44:20 +00:00
|
|
|
localClone->cnv.extraInfo = &localClone->mydata;
|
2003-05-13 21:05:05 +00:00
|
|
|
localClone->cnv.isExtraLocal = TRUE;
|
2003-04-05 00:01:54 +00:00
|
|
|
|
|
|
|
/* deep-clone the sub-converter */
|
2006-06-20 22:27:15 +00:00
|
|
|
size = (int32_t)(sizeof(UConverter) + sizeof(UAlignedMemory)); /* include size of padding */
|
2003-04-05 00:01:54 +00:00
|
|
|
((UConverterDataHZ*)localClone->cnv.extraInfo)->gbConverter =
|
|
|
|
ucnv_safeClone(((UConverterDataHZ*)cnv->extraInfo)->gbConverter, &localClone->subCnv, &size, status);
|
2001-02-16 22:22:40 +00:00
|
|
|
|
2001-03-21 20:44:20 +00:00
|
|
|
return &localClone->cnv;
|
2001-02-16 22:22:40 +00:00
|
|
|
}
|
|
|
|
|
2003-04-05 01:33:02 +00:00
|
|
|
static void
|
|
|
|
_HZ_GetUnicodeSet(const UConverter *cnv,
|
2004-12-02 04:18:35 +00:00
|
|
|
const USetAdder *sa,
|
2003-04-05 01:33:02 +00:00
|
|
|
UConverterUnicodeSet which,
|
|
|
|
UErrorCode *pErrorCode) {
|
2007-10-25 05:03:34 +00:00
|
|
|
/* HZ converts all of ASCII */
|
|
|
|
sa->addRange(sa->set, 0, 0x7f);
|
2003-04-05 01:33:02 +00:00
|
|
|
|
|
|
|
/* add all of the code points that the sub-converter handles */
|
2007-10-25 05:03:34 +00:00
|
|
|
ucnv_MBCSGetFilteredUnicodeSetForUnicode(
|
|
|
|
((UConverterDataHZ*)cnv->extraInfo)->gbConverter->sharedData,
|
|
|
|
sa, which, UCNV_SET_FILTER_HZ,
|
|
|
|
pErrorCode);
|
2003-04-05 01:33:02 +00:00
|
|
|
}
|
2001-09-06 18:34:57 +00:00
|
|
|
|
|
|
|
static const UConverterImpl _HZImpl={
|
|
|
|
|
|
|
|
UCNV_HZ,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
|
|
|
_HZOpen,
|
|
|
|
_HZClose,
|
|
|
|
_HZReset,
|
|
|
|
|
|
|
|
UConverter_toUnicode_HZ_OFFSETS_LOGIC,
|
|
|
|
UConverter_toUnicode_HZ_OFFSETS_LOGIC,
|
|
|
|
UConverter_fromUnicode_HZ_OFFSETS_LOGIC,
|
|
|
|
UConverter_fromUnicode_HZ_OFFSETS_LOGIC,
|
|
|
|
NULL,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
_HZ_WriteSub,
|
2003-04-05 01:33:02 +00:00
|
|
|
_HZ_SafeClone,
|
|
|
|
_HZ_GetUnicodeSet
|
2001-09-06 18:34:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const UConverterStaticData _HZStaticData={
|
|
|
|
sizeof(UConverterStaticData),
|
|
|
|
"HZ",
|
|
|
|
0,
|
|
|
|
UCNV_IBM,
|
|
|
|
UCNV_HZ,
|
|
|
|
1,
|
|
|
|
4,
|
|
|
|
{ 0x1a, 0, 0, 0 },
|
|
|
|
1,
|
|
|
|
FALSE,
|
|
|
|
FALSE,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, /* reserved */
|
|
|
|
|
|
|
|
};
|
2015-05-29 19:33:24 +00:00
|
|
|
|
|
|
|
const UConverterSharedData _HZData=
|
|
|
|
UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(&_HZStaticData, &_HZImpl);
|
2001-09-06 18:34:57 +00:00
|
|
|
|
2015-02-20 19:31:33 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION */
|