#include "unicode/utypes.h"
#if !UCONFIG_NO_LEGACY_CONVERSION
#include "unicode/ucnv.h"
#include "unicode/ucnv_cb.h"
#include "unicode/udata.h"
#include "unicode/uset.h"
#include "ucnv_bld.h"
#include "ucnvmbcs.h"
#include "ucnv_cnv.h"
#include "umutex.h"
#include "cmemory.h"
#include "cstring.h"
#define MBCS_UNROLL_SINGLE_TO_BMP 1
#define MBCS_UNROLL_SINGLE_FROM_BMP 0
static void
_MBCSSingleToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs,
UErrorCode *pErrorCode);
static void
_MBCSSingleToBMPWithOffsets(UConverterToUnicodeArgs *pArgs,
UErrorCode *pErrorCode);
static UChar32
_MBCSGetNextUChar(UConverterToUnicodeArgs *pArgs,
UErrorCode *pErrorCode);
static UChar32
_MBCSSingleGetNextUChar(UConverterToUnicodeArgs *pArgs,
UErrorCode *pErrorCode);
static void
_MBCSDoubleFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
UErrorCode *pErrorCode);
static void
_MBCSSingleFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
UErrorCode *pErrorCode);
static void
_MBCSSingleFromBMPWithOffsets(UConverterFromUnicodeArgs *pArgs,
UErrorCode *pErrorCode);
static void
fromUCallback(UConverter *cnv,
const void *context, UConverterFromUnicodeArgs *pArgs,
UChar32 codePoint,
UConverterCallbackReason reason, UErrorCode *pErrorCode);
static void
toUCallback(UConverter *cnv,
const void *context, UConverterToUnicodeArgs *pArgs,
const char *codeUnits, int32_t length,
UConverterCallbackReason reason, UErrorCode *pErrorCode);
#define LINEAR_18030(a, b, c, d) ((((a)*10+(b))*126L+(c))*10L+(d))
#define LINEAR_18030_BASE LINEAR_18030(0x81, 0x30, 0x81, 0x30)
#define LINEAR(x) LINEAR_18030(x>>24, (x>>16)&0xff, (x>>8)&0xff, x&0xff)
static const uint32_t
gb18030Ranges[13][4]={
{0x10000, 0x10FFFF, LINEAR(0x90308130), LINEAR(0xE3329A35)},
{0x9FA6, 0xD7FF, LINEAR(0x82358F33), LINEAR(0x8336C738)},
{0x0452, 0x200F, LINEAR(0x8130D330), LINEAR(0x8136A531)},
{0xE865, 0xF92B, LINEAR(0x8336D030), LINEAR(0x84308534)},
{0x2643, 0x2E80, LINEAR(0x8137A839), LINEAR(0x8138FD38)},
{0xFA2A, 0xFE2F, LINEAR(0x84309C38), LINEAR(0x84318537)},
{0x3CE1, 0x4055, LINEAR(0x8231D438), LINEAR(0x8232AF32)},
{0x361B, 0x3917, LINEAR(0x8230A633), LINEAR(0x8230F237)},
{0x49B8, 0x4C76, LINEAR(0x8234A131), LINEAR(0x8234E733)},
{0x4160, 0x4336, LINEAR(0x8232C937), LINEAR(0x8232F837)},
{0x478E, 0x4946, LINEAR(0x8233E838), LINEAR(0x82349638)},
{0x44D7, 0x464B, LINEAR(0x8233A339), LINEAR(0x8233C931)},
{0xFFE6, 0xFFFF, LINEAR(0x8431A234), LINEAR(0x8431A439)}
};
#define _MBCS_OPTION_GB18030 0x8000
static uint32_t
_MBCSSizeofFromUBytes(UConverterMBCSTable *mbcsTable) {
const uint16_t *table;
uint32_t st3, maxStage3;
uint16_t st1, maxStage1, st2;
if(mbcsTable->fromUBytesLength>0) {
return mbcsTable->fromUBytesLength;
}
table=mbcsTable->fromUnicodeTable;
maxStage3=0;
if(mbcsTable->unicodeMask&UCNV_HAS_SUPPLEMENTARY) {
maxStage1=0x440;
} else {
maxStage1=0x40;
}
if(mbcsTable->outputType==MBCS_OUTPUT_1) {
const uint16_t *stage2;
for(st1=0; st1<maxStage1; ++st1) {
st2=table[st1];
if(st2>maxStage1) {
stage2=table+st2;
for(st2=0; st2<64; ++st2) {
st3=stage2[st2];
if(st3>maxStage3) {
maxStage3=st3;
}
}
}
}
return (maxStage3+16)*2;
} else {
const uint32_t *stage2;
for(st1=0; st1<maxStage1; ++st1) {
st2=table[st1];
if(st2>(maxStage1>>1)) {
stage2=(const uint32_t *)table+st2;
for(st2=0; st2<64; ++st2) {
st3=stage2[st2]&0xffff;
if(st3>maxStage3) {
maxStage3=st3;
}
}
}
}
maxStage3=16*maxStage3+16;
switch(mbcsTable->outputType) {
case MBCS_OUTPUT_3:
case MBCS_OUTPUT_4_EUC:
maxStage3*=3;
break;
case MBCS_OUTPUT_4:
maxStage3*=4;
break;
default:
maxStage3*=2;
break;
}
return maxStage3;
}
}
static void
_MBCSGetUnicodeSet(const UConverter *cnv,
USet *set,
UConverterUnicodeSet which,
UErrorCode *pErrorCode) {
UConverterMBCSTable *mbcsTable;
const uint16_t *table;
uint32_t st3;
uint16_t st1, maxStage1, st2;
UChar32 c;
if(cnv->options&_MBCS_OPTION_GB18030) {
uset_addRange(set, 0, 0xd7ff);
uset_addRange(set, 0xe000, 0x10ffff);
return;
}
mbcsTable=&cnv->sharedData->table->mbcs;
table=mbcsTable->fromUnicodeTable;
if(mbcsTable->unicodeMask&UCNV_HAS_SUPPLEMENTARY) {
maxStage1=0x440;
} else {
maxStage1=0x40;
}
c=0;
if(mbcsTable->outputType==MBCS_OUTPUT_1) {
const uint16_t *stage2, *stage3, *results;
results=(const uint16_t *)mbcsTable->fromUnicodeBytes;
for(st1=0; st1<maxStage1; ++st1) {
st2=table[st1];
if(st2>maxStage1) {
stage2=table+st2;
for(st2=0; st2<64; ++st2) {
if((st3=stage2[st2])!=0) {
stage3=results+st3;
do {
if(*stage3++>=0xf00) {
uset_add(set, c);
}
} while((++c&0xf)!=0);
} else {
c+=16;
}
}
} else {
c+=1024;
}
}
} else {
const uint32_t *stage2;
for(st1=0; st1<maxStage1; ++st1) {
st2=table[st1];
if(st2>(maxStage1>>1)) {
stage2=(const uint32_t *)table+st2;
for(st2=0; st2<64; ++st2) {
if((st3=stage2[st2])!=0) {
st3>>=16;
do {
if(st3&1) {
uset_add(set, c);
}
st3>>=1;
} while((++c&0xf)!=0);
} else {
c+=16;
}
}
} else {
c+=1024;
}
}
}
}
#define EBCDIC_LF 0x25
#define EBCDIC_NL 0x15
#define EBCDIC_RT_LF 0xf25
#define EBCDIC_RT_NL 0xf15
#define U_LF 0x0a
#define U_NL 0x85
static UBool
_EBCDICSwapLFNL(UConverterSharedData *sharedData, UErrorCode *pErrorCode) {
UConverterMBCSTable *mbcsTable;
const uint16_t *table, *results;
const uint8_t *bytes;
int32_t (*newStateTable)[256];
uint16_t *newResults;
uint8_t *p;
char *name;
uint32_t stage2Entry;
uint32_t size, sizeofFromUBytes;
mbcsTable=&sharedData->table->mbcs;
table=mbcsTable->fromUnicodeTable;
bytes=mbcsTable->fromUnicodeBytes;
results=(const uint16_t *)bytes;
if(!(
(mbcsTable->outputType==MBCS_OUTPUT_1 || mbcsTable->outputType==MBCS_OUTPUT_2_SISO) &&
mbcsTable->stateTable[0][EBCDIC_LF]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_LF) &&
mbcsTable->stateTable[0][EBCDIC_NL]==MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_NL)
)) {
return FALSE;
}
if(mbcsTable->outputType==MBCS_OUTPUT_1) {
if(!(
EBCDIC_RT_LF==MBCS_SINGLE_RESULT_FROM_U(table, results, U_LF) &&
EBCDIC_RT_NL==MBCS_SINGLE_RESULT_FROM_U(table, results, U_NL)
)) {
return FALSE;
}
} else {
stage2Entry=MBCS_STAGE_2_FROM_U(table, U_LF);
if(!(
MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, U_LF)!=0 &&
EBCDIC_LF==MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, U_LF)
)) {
return FALSE;
}
stage2Entry=MBCS_STAGE_2_FROM_U(table, U_NL);
if(!(
MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, U_NL)!=0 &&
EBCDIC_NL==MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, U_NL)
)) {
return FALSE;
}
}
sizeofFromUBytes=_MBCSSizeofFromUBytes(mbcsTable);
size=
mbcsTable->countStates*1024+
sizeofFromUBytes+
UCNV_MAX_CONVERTER_NAME_LENGTH+20;
p=(uint8_t *)uprv_malloc(size);
if(p==NULL) {
*pErrorCode=U_MEMORY_ALLOCATION_ERROR;
return FALSE;
}
newStateTable=(int32_t (*)[256])p;
uprv_memcpy(newStateTable, mbcsTable->stateTable, mbcsTable->countStates*1024);
newStateTable[0][EBCDIC_LF]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_NL);
newStateTable[0][EBCDIC_NL]=MBCS_ENTRY_FINAL(0, MBCS_STATE_VALID_DIRECT_16, U_LF);
newResults=(uint16_t *)newStateTable[mbcsTable->countStates];
uprv_memcpy(newResults, bytes, sizeofFromUBytes);
if(mbcsTable->outputType==MBCS_OUTPUT_1) {
MBCS_SINGLE_RESULT_FROM_U(table, newResults, U_LF)=EBCDIC_RT_NL;
MBCS_SINGLE_RESULT_FROM_U(table, newResults, U_NL)=EBCDIC_RT_LF;
} else {
stage2Entry=MBCS_STAGE_2_FROM_U(table, U_LF);
MBCS_VALUE_2_FROM_STAGE_2(newResults, stage2Entry, U_LF)=EBCDIC_NL;
stage2Entry=MBCS_STAGE_2_FROM_U(table, U_NL);
MBCS_VALUE_2_FROM_STAGE_2(newResults, stage2Entry, U_NL)=EBCDIC_LF;
}
name=(char *)newResults+sizeofFromUBytes;
uprv_strcpy(name, sharedData->staticData->name);
uprv_strcat(name, UCNV_SWAP_LFNL_OPTION_STRING);
umtx_lock(NULL);
if(mbcsTable->swapLFNLStateTable==NULL) {
mbcsTable->swapLFNLStateTable=newStateTable;
mbcsTable->swapLFNLFromUnicodeBytes=(uint8_t *)newResults;
mbcsTable->swapLFNLName=name;
newStateTable=NULL;
}
umtx_unlock(NULL);
if(newStateTable!=NULL) {
uprv_free(newStateTable);
}
return TRUE;
}
static void
_MBCSLoad(UConverterSharedData *sharedData,
const uint8_t *raw,
UErrorCode *pErrorCode) {
UDataInfo info;
UConverterMBCSTable *mbcsTable=&sharedData->table->mbcs;
_MBCSHeader *header=(_MBCSHeader *)raw;
if(header->version[0]!=4) {
*pErrorCode=U_INVALID_TABLE_FORMAT;
return;
}
mbcsTable->countStates=(uint8_t)header->countStates;
mbcsTable->countToUFallbacks=header->countToUFallbacks;
mbcsTable->stateTable=(const int32_t (*)[256])(raw+sizeof(_MBCSHeader));
mbcsTable->toUFallbacks=(const _MBCSToUFallback *)(mbcsTable->stateTable+header->countStates);
mbcsTable->unicodeCodeUnits=(const uint16_t *)(raw+header->offsetToUCodeUnits);
mbcsTable->fromUnicodeTable=(const uint16_t *)(raw+header->offsetFromUTable);
mbcsTable->fromUnicodeBytes=(const uint8_t *)(raw+header->offsetFromUBytes);
mbcsTable->fromUBytesLength=header->fromUBytesLength;
mbcsTable->outputType=(uint8_t)header->flags;
switch(mbcsTable->outputType) {
case MBCS_OUTPUT_1:
case MBCS_OUTPUT_2:
case MBCS_OUTPUT_3:
case MBCS_OUTPUT_4:
case MBCS_OUTPUT_3_EUC:
case MBCS_OUTPUT_4_EUC:
case MBCS_OUTPUT_2_SISO:
break;
default:
*pErrorCode=U_INVALID_TABLE_FORMAT;
return;
}
info.size=sizeof(UDataInfo);
udata_getInfo((UDataMemory *)sharedData->dataMemory, &info);
if(info.formatVersion[0]>6 || (info.formatVersion[0]==6 && info.formatVersion[1]>=1)) {
mbcsTable->unicodeMask=(uint8_t)(sharedData->staticData->unicodeMask&3);
} else {
mbcsTable->unicodeMask=UCNV_HAS_SUPPLEMENTARY|UCNV_HAS_SURROGATES;
}
}
static void
_MBCSUnload(UConverterSharedData *sharedData) {
UConverterMBCSTable *mbcsTable=&sharedData->table->mbcs;
if(mbcsTable->swapLFNLStateTable!=NULL) {
uprv_free(mbcsTable->swapLFNLStateTable);
}
}
static void
_MBCSReset(UConverter *cnv, UConverterResetChoice choice) {
if(choice<=UCNV_RESET_TO_UNICODE) {
cnv->toUnicodeStatus=0;
cnv->mode=0;
cnv->toULength=0;
}
if(choice!=UCNV_RESET_TO_UNICODE) {
cnv->fromUSurrogateLead=0;
cnv->fromUnicodeStatus=1;
}
}
static void
_MBCSOpen(UConverter *cnv,
const char *name,
const char *locale,
uint32_t options,
UErrorCode *pErrorCode) {
if((options&UCNV_OPTION_SWAP_LFNL)!=0) {
UBool isCached;
umtx_lock(NULL);
isCached=cnv->sharedData->table->mbcs.swapLFNLStateTable!=NULL;
umtx_unlock(NULL);
if(!isCached) {
if(!_EBCDICSwapLFNL(cnv->sharedData, pErrorCode)) {
cnv->options&=~UCNV_OPTION_SWAP_LFNL;
}
}
}
if(uprv_strstr(name, "18030")!=NULL) {
if(uprv_strstr(name, "gb18030")!=NULL || uprv_strstr(name, "GB18030")!=NULL) {
cnv->options|=_MBCS_OPTION_GB18030;
}
}
_MBCSReset(cnv, UCNV_RESET_BOTH);
}
static const char *
_MBCSGetName(const UConverter *cnv) {
if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0 && cnv->sharedData->table->mbcs.swapLFNLName!=NULL) {
return cnv->sharedData->table->mbcs.swapLFNLName;
} else {
return cnv->sharedData->staticData->name;
}
}
static UChar32
_MBCSGetFallback(UConverterMBCSTable *mbcsTable, uint32_t offset) {
const _MBCSToUFallback *toUFallbacks;
uint32_t i, start, limit;
limit=mbcsTable->countToUFallbacks;
if(limit>0) {
toUFallbacks=mbcsTable->toUFallbacks;
start=0;
while(start<limit-1) {
i=(start+limit)/2;
if(offset<toUFallbacks[i].offset) {
limit=i;
} else {
start=i;
}
}
if(offset==toUFallbacks[start].offset) {
return toUFallbacks[start].codePoint;
}
}
return 0xfffe;
}
U_CFUNC void
_MBCSToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UConverter *cnv;
const uint8_t *source, *sourceLimit;
UChar *target;
const UChar *targetLimit;
int32_t *offsets;
const int32_t (*stateTable)[256];
const uint16_t *unicodeCodeUnits;
uint32_t offset;
uint8_t state;
int8_t byteIndex;
uint8_t *bytes;
int32_t sourceIndex, nextSourceIndex;
int32_t entry;
UChar c;
uint8_t action;
UConverterCallbackReason reason;
cnv=pArgs->converter;
if(cnv->sharedData->table->mbcs.countStates==1) {
if(!(cnv->sharedData->table->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
_MBCSSingleToBMPWithOffsets(pArgs, pErrorCode);
} else {
_MBCSSingleToUnicodeWithOffsets(pArgs, pErrorCode);
}
return;
}
source=(const uint8_t *)pArgs->source;
sourceLimit=(const uint8_t *)pArgs->sourceLimit;
target=pArgs->target;
targetLimit=pArgs->targetLimit;
offsets=pArgs->offsets;
if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
stateTable=(const int32_t (*)[256])cnv->sharedData->table->mbcs.swapLFNLStateTable;
} else {
stateTable=cnv->sharedData->table->mbcs.stateTable;
}
unicodeCodeUnits=cnv->sharedData->table->mbcs.unicodeCodeUnits;
offset=cnv->toUnicodeStatus;
state=(uint8_t)(cnv->mode);
byteIndex=cnv->toULength;
bytes=cnv->toUBytes;
sourceIndex=byteIndex==0 ? 0 : -1;
nextSourceIndex=0;
while(source<sourceLimit) {
if(target<targetLimit) {
++nextSourceIndex;
entry=stateTable[state][bytes[byteIndex++]=*source++];
if(MBCS_ENTRY_IS_TRANSITION(entry)) {
state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
offset+=MBCS_ENTRY_TRANSITION_OFFSET(entry);
} else {
state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry);
action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
if(action==MBCS_STATE_VALID_16) {
offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
c=unicodeCodeUnits[offset];
if(c<0xfffe) {
*target++=c;
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
} else if(c==0xfffe) {
if(UCNV_TO_U_USE_FALLBACK(cnv) && (entry=(int32_t)_MBCSGetFallback(&cnv->sharedData->table->mbcs, offset))!=0xfffe) {
*target++=(UChar)entry;
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
} else {
goto unassigned;
}
} else {
goto illegal;
}
} else if(action==MBCS_STATE_VALID_DIRECT_16) {
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
} else if(action==MBCS_STATE_VALID_16_PAIR) {
offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
c=unicodeCodeUnits[offset++];
if(c<0xd800) {
*target++=c;
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
} else if(UCNV_TO_U_USE_FALLBACK(cnv) ? c<=0xdfff : c<=0xdbff) {
*target++=(UChar)(c&0xdbff);
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
if(target<targetLimit) {
*target++=unicodeCodeUnits[offset];
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
} else {
cnv->UCharErrorBuffer[0]=unicodeCodeUnits[offset];
cnv->UCharErrorBufferLength=1;
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
offset=0;
byteIndex=0;
break;
}
} else if(UCNV_TO_U_USE_FALLBACK(cnv) ? (c&0xfffe)==0xe000 : c==0xe000) {
*target++=unicodeCodeUnits[offset];
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
} else if(c==0xffff) {
goto illegal;
} else {
goto unassigned;
}
} else if(action==MBCS_STATE_VALID_DIRECT_20) {
valid20:
entry=MBCS_ENTRY_FINAL_VALUE(entry);
*target++=(UChar)(0xd800|(UChar)(entry>>10));
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
c=(UChar)(0xdc00|(UChar)(entry&0x3ff));
if(target<targetLimit) {
*target++=c;
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
} else {
cnv->UCharErrorBuffer[0]=c;
cnv->UCharErrorBufferLength=1;
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
offset=0;
byteIndex=0;
break;
}
} else if(action==MBCS_STATE_CHANGE_ONLY) {
} else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
if(!UCNV_TO_U_USE_FALLBACK(cnv)) {
goto unassigned;
}
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
} else if(action==MBCS_STATE_FALLBACK_DIRECT_20) {
if(!UCNV_TO_U_USE_FALLBACK(cnv)) {
goto unassigned;
}
goto valid20;
} else if(action==MBCS_STATE_UNASSIGNED) {
goto unassigned;
} else if(action==MBCS_STATE_ILLEGAL) {
goto illegal;
} else {
}
offset=0;
byteIndex=0;
sourceIndex=nextSourceIndex;
continue;
illegal:
reason=UCNV_ILLEGAL;
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
goto callback;
unassigned:
reason=UCNV_UNASSIGNED;
*pErrorCode=U_INVALID_CHAR_FOUND;
callback:
pArgs->source=(const char *)source;
pArgs->target=target;
pArgs->offsets=offsets;
cnv->toUnicodeStatus=0;
cnv->mode=state;
cnv->toULength=0;
toUCallback(cnv, cnv->toUContext, pArgs, (const char *)bytes, byteIndex, reason, pErrorCode);
offset=cnv->toUnicodeStatus;
state=(uint8_t)cnv->mode;
byteIndex=cnv->toULength;
offsets=ucnv_updateCallbackOffsets(offsets, pArgs->target-target, sourceIndex);
target=pArgs->target;
sourceIndex=nextSourceIndex+((const uint8_t *)pArgs->source-source);
source=(const uint8_t *)pArgs->source;
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
break;
} else if(U_FAILURE(*pErrorCode)) {
offset=0;
state=0;
byteIndex=0;
break;
} else if(cnv->UCharErrorBufferLength>0) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
break;
}
}
} else {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
break;
}
}
if(pArgs->flush && source>=sourceLimit) {
if(byteIndex>0 && U_SUCCESS(*pErrorCode)) {
*pErrorCode=U_TRUNCATED_CHAR_FOUND;
}
cnv->toUnicodeStatus=0;
cnv->mode=0;
cnv->toULength=0;
} else {
cnv->toUnicodeStatus=offset;
cnv->mode=state;
cnv->toULength=byteIndex;
}
pArgs->source=(const char *)source;
pArgs->target=target;
pArgs->offsets=offsets;
}
static void
_MBCSSingleToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UConverter *cnv;
const uint8_t *source, *sourceLimit;
UChar *target;
const UChar *targetLimit;
int32_t *offsets;
const int32_t (*stateTable)[256];
int32_t sourceIndex, nextSourceIndex;
int32_t entry;
UChar c;
uint8_t action;
UConverterCallbackReason reason;
cnv=pArgs->converter;
source=(const uint8_t *)pArgs->source;
sourceLimit=(const uint8_t *)pArgs->sourceLimit;
target=pArgs->target;
targetLimit=pArgs->targetLimit;
offsets=pArgs->offsets;
if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
stateTable=(const int32_t (*)[256])cnv->sharedData->table->mbcs.swapLFNLStateTable;
} else {
stateTable=cnv->sharedData->table->mbcs.stateTable;
}
sourceIndex=0;
nextSourceIndex=0;
while(source<sourceLimit) {
if(target<targetLimit) {
++nextSourceIndex;
entry=stateTable[0][*source++];
if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
sourceIndex=nextSourceIndex;
continue;
}
action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
if(action==MBCS_STATE_VALID_DIRECT_20) {
valid20:
entry=MBCS_ENTRY_FINAL_VALUE(entry);
*target++=(UChar)(0xd800|(UChar)(entry>>10));
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
c=(UChar)(0xdc00|(UChar)(entry&0x3ff));
if(target<targetLimit) {
*target++=c;
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
} else {
cnv->UCharErrorBuffer[0]=c;
cnv->UCharErrorBufferLength=1;
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
break;
}
} else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
if(!UCNV_TO_U_USE_FALLBACK(cnv)) {
goto unassigned;
}
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
} else if(action==MBCS_STATE_FALLBACK_DIRECT_20) {
if(!UCNV_TO_U_USE_FALLBACK(cnv)) {
goto unassigned;
}
goto valid20;
} else if(action==MBCS_STATE_UNASSIGNED) {
goto unassigned;
} else if(action==MBCS_STATE_ILLEGAL) {
reason=UCNV_ILLEGAL;
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
goto callback;
} else {
}
sourceIndex=nextSourceIndex;
continue;
unassigned:
reason=UCNV_UNASSIGNED;
*pErrorCode=U_INVALID_CHAR_FOUND;
callback:
pArgs->source=(const char *)source;
pArgs->target=target;
pArgs->offsets=offsets;
toUCallback(cnv, cnv->toUContext, pArgs, (const char *)(source-1), 1, reason, pErrorCode);
offsets=ucnv_updateCallbackOffsets(offsets, pArgs->target-target, sourceIndex);
target=pArgs->target;
sourceIndex=nextSourceIndex+((const uint8_t *)pArgs->source-source);
source=(const uint8_t *)pArgs->source;
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
break;
} else if(U_FAILURE(*pErrorCode)) {
break;
} else if(cnv->UCharErrorBufferLength>0) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
break;
}
} else {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
break;
}
}
pArgs->source=(const char *)source;
pArgs->target=target;
pArgs->offsets=offsets;
}
static void
_MBCSSingleToBMPWithOffsets(UConverterToUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UConverter *cnv;
const uint8_t *source, *sourceLimit, *lastSource;
UChar *target;
int32_t targetCapacity, length;
int32_t *offsets;
const int32_t (*stateTable)[256];
int32_t sourceIndex;
int32_t entry;
uint8_t action;
UConverterCallbackReason reason;
cnv=pArgs->converter;
source=(const uint8_t *)pArgs->source;
sourceLimit=(const uint8_t *)pArgs->sourceLimit;
target=pArgs->target;
targetCapacity=pArgs->targetLimit-pArgs->target;
offsets=pArgs->offsets;
if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
stateTable=(const int32_t (*)[256])cnv->sharedData->table->mbcs.swapLFNLStateTable;
} else {
stateTable=cnv->sharedData->table->mbcs.stateTable;
}
sourceIndex=0;
lastSource=source;
length=sourceLimit-source;
if(length<targetCapacity) {
targetCapacity=length;
}
#if MBCS_UNROLL_SINGLE_TO_BMP
unrolled:
if(targetCapacity>=16) {
int32_t count, loops, oredEntries;
loops=count=targetCapacity>>4;
do {
oredEntries=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
oredEntries|=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
oredEntries|=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
oredEntries|=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
oredEntries|=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
oredEntries|=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
oredEntries|=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
oredEntries|=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
oredEntries|=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
oredEntries|=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
oredEntries|=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
oredEntries|=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
oredEntries|=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
oredEntries|=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
oredEntries|=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
oredEntries|=entry=stateTable[0][*source++];
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
if(!MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(oredEntries)) {
source-=16;
target-=16;
break;
}
} while(--count>0);
count=loops-count;
targetCapacity-=16*count;
if(offsets!=NULL) {
lastSource+=16*count;
while(count>0) {
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
--count;
}
}
}
#endif
while(targetCapacity>0) {
entry=stateTable[0][*source++];
if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
--targetCapacity;
continue;
}
action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
if(!UCNV_TO_U_USE_FALLBACK(cnv)) {
reason=UCNV_UNASSIGNED;
*pErrorCode=U_INVALID_CHAR_FOUND;
}
*target++=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
--targetCapacity;
continue;
} else if(action==MBCS_STATE_UNASSIGNED) {
reason=UCNV_UNASSIGNED;
*pErrorCode=U_INVALID_CHAR_FOUND;
} else if(action==MBCS_STATE_ILLEGAL) {
reason=UCNV_ILLEGAL;
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
} else {
continue;
}
if(offsets!=NULL) {
int32_t count=(int32_t)(source-lastSource);
while(--count>0) {
*offsets++=sourceIndex++;
}
}
pArgs->source=(const char *)source;
pArgs->target=target;
pArgs->offsets=offsets;
toUCallback(cnv, cnv->toUContext, pArgs, (const char *)(source-1), 1, reason, pErrorCode);
offsets=ucnv_updateCallbackOffsets(offsets, pArgs->target-target, sourceIndex);
target=pArgs->target;
sourceIndex+=1+((const uint8_t *)pArgs->source-source);
source=lastSource=(const uint8_t *)pArgs->source;
targetCapacity=pArgs->targetLimit-target;
length=sourceLimit-source;
if(length<targetCapacity) {
targetCapacity=length;
}
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
break;
} else if(U_FAILURE(*pErrorCode)) {
break;
} else if(cnv->UCharErrorBufferLength>0) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
break;
}
#if MBCS_UNROLL_SINGLE_TO_BMP
goto unrolled;
#endif
}
if(U_SUCCESS(*pErrorCode) && source<sourceLimit && target>=pArgs->targetLimit) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
}
if(offsets!=NULL) {
size_t count=source-lastSource;
while(count>0) {
*offsets++=sourceIndex++;
--count;
}
}
pArgs->source=(const char *)source;
pArgs->target=target;
pArgs->offsets=offsets;
}
static UChar32
_MBCSGetNextUChar(UConverterToUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UChar buffer[UTF_MAX_CHAR_LENGTH];
UConverter *cnv;
const uint8_t *source, *sourceLimit;
const int32_t (*stateTable)[256];
const uint16_t *unicodeCodeUnits;
uint32_t offset;
uint8_t state;
int8_t byteIndex;
uint8_t *bytes;
int32_t entry;
UChar32 c;
uint8_t action;
UConverterCallbackReason reason;
cnv=pArgs->converter;
if(cnv->sharedData->table->mbcs.unicodeMask&UCNV_HAS_SURROGATES) {
return ucnv_getNextUCharFromToUImpl(pArgs, _MBCSToUnicodeWithOffsets, TRUE, pErrorCode);
} else if(cnv->sharedData->table->mbcs.countStates==1) {
return _MBCSSingleGetNextUChar(pArgs, pErrorCode);
}
source=(const uint8_t *)pArgs->source;
sourceLimit=(const uint8_t *)pArgs->sourceLimit;
if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
stateTable=(const int32_t (*)[256])cnv->sharedData->table->mbcs.swapLFNLStateTable;
} else {
stateTable=cnv->sharedData->table->mbcs.stateTable;
}
unicodeCodeUnits=cnv->sharedData->table->mbcs.unicodeCodeUnits;
offset=cnv->toUnicodeStatus;
state=(uint8_t)(cnv->mode);
byteIndex=cnv->toULength;
bytes=cnv->toUBytes;
while(source<sourceLimit) {
entry=stateTable[state][bytes[byteIndex++]=*source++];
if(MBCS_ENTRY_IS_TRANSITION(entry)) {
state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
offset+=MBCS_ENTRY_TRANSITION_OFFSET(entry);
} else {
state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry);
action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
if(action==MBCS_STATE_VALID_16) {
offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
c=unicodeCodeUnits[offset];
if(c<0xfffe) {
goto finish;
} else if(c==0xfffe) {
if(UCNV_TO_U_USE_FALLBACK(cnv) && (c=_MBCSGetFallback(&cnv->sharedData->table->mbcs, offset))!=0xfffe) {
goto finish;
}
goto unassigned;
} else {
goto illegal;
}
} else if(action==MBCS_STATE_VALID_DIRECT_16) {
c=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
goto finish;
} else if(action==MBCS_STATE_VALID_16_PAIR) {
offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
c=unicodeCodeUnits[offset++];
if(c<0xd800) {
goto finish;
} else if(UCNV_TO_U_USE_FALLBACK(cnv) ? c<=0xdfff : c<=0xdbff) {
c=((c&0x3ff)<<10)+unicodeCodeUnits[offset]+(0x10000-0xdc00);
goto finish;
} else if(UCNV_TO_U_USE_FALLBACK(cnv) ? (c&0xfffe)==0xe000 : c==0xe000) {
c=unicodeCodeUnits[offset];
goto finish;
} else if(c==0xffff) {
goto illegal;
} else {
goto unassigned;
}
} else if(action==MBCS_STATE_VALID_DIRECT_20) {
c=(UChar32)(MBCS_ENTRY_FINAL_VALUE(entry)+0x10000);
goto finish;
} else if(action==MBCS_STATE_CHANGE_ONLY) {
} else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
if(!UCNV_TO_U_USE_FALLBACK(cnv)) {
goto unassigned;
}
c=(UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
goto finish;
} else if(action==MBCS_STATE_FALLBACK_DIRECT_20) {
if(!UCNV_TO_U_USE_FALLBACK(cnv)) {
goto unassigned;
}
c=(UChar32)(MBCS_ENTRY_FINAL_VALUE(entry)+0x10000);
goto finish;
} else if(action==MBCS_STATE_UNASSIGNED) {
goto unassigned;
} else if(action==MBCS_STATE_ILLEGAL) {
goto illegal;
} else {
}
offset=0;
byteIndex=0;
continue;
illegal:
reason=UCNV_ILLEGAL;
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
goto callback;
unassigned:
reason=UCNV_UNASSIGNED;
*pErrorCode=U_INVALID_CHAR_FOUND;
callback:
pArgs->source=(const char *)source;
pArgs->target=buffer;
pArgs->targetLimit=buffer+UTF_MAX_CHAR_LENGTH;
cnv->toUnicodeStatus=0;
cnv->mode=state;
cnv->toULength=0;
toUCallback(cnv, cnv->toUContext, pArgs, (const char *)bytes, byteIndex, reason, pErrorCode);
offset=cnv->toUnicodeStatus;
state=(uint8_t)cnv->mode;
byteIndex=cnv->toULength;
source=(const uint8_t *)pArgs->source;
if(U_SUCCESS(*pErrorCode)) {
entry=pArgs->target-buffer;
if(entry>0) {
return ucnv_getUChar32KeepOverflow(cnv, buffer, entry);
}
} else if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
*pErrorCode=U_ZERO_ERROR;
return ucnv_getUChar32KeepOverflow(cnv, buffer, UTF_MAX_CHAR_LENGTH);
} else {
state=0;
c=0xffff;
goto finish;
}
}
}
if(byteIndex>0) {
*pErrorCode=U_TRUNCATED_CHAR_FOUND;
state=0;
} else {
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
}
c=0xffff;
finish:
cnv->toUnicodeStatus=0;
cnv->mode=state;
cnv->toULength=0;
pArgs->source=(const char *)source;
return c;
}
static UChar32
_MBCSSingleGetNextUChar(UConverterToUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UChar buffer[UTF_MAX_CHAR_LENGTH];
UConverter *cnv;
const int32_t (*stateTable)[256];
const uint8_t *source, *sourceLimit;
int32_t entry;
uint8_t action;
UConverterCallbackReason reason;
cnv=pArgs->converter;
source=(const uint8_t *)pArgs->source;
sourceLimit=(const uint8_t *)pArgs->sourceLimit;
if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
stateTable=(const int32_t (*)[256])cnv->sharedData->table->mbcs.swapLFNLStateTable;
} else {
stateTable=cnv->sharedData->table->mbcs.stateTable;
}
while(source<sourceLimit) {
entry=stateTable[0][*source++];
pArgs->source=(const char *)source;
if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
return (UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
}
action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
if(action==MBCS_STATE_VALID_DIRECT_20) {
return (UChar32)(MBCS_ENTRY_FINAL_VALUE(entry)+0x10000);
} else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
if(!UCNV_TO_U_USE_FALLBACK(cnv)) {
reason=UCNV_UNASSIGNED;
*pErrorCode=U_INVALID_CHAR_FOUND;
} else {
return (UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
}
} else if(action==MBCS_STATE_FALLBACK_DIRECT_20) {
if(!UCNV_TO_U_USE_FALLBACK(cnv)) {
reason=UCNV_UNASSIGNED;
*pErrorCode=U_INVALID_CHAR_FOUND;
} else {
return (UChar32)(MBCS_ENTRY_FINAL_VALUE(entry)+0x10000);
}
} else if(action==MBCS_STATE_UNASSIGNED) {
reason=UCNV_UNASSIGNED;
*pErrorCode=U_INVALID_CHAR_FOUND;
} else if(action==MBCS_STATE_ILLEGAL) {
reason=UCNV_ILLEGAL;
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
} else {
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0xffff;
}
pArgs->target=buffer;
pArgs->targetLimit=buffer+UTF_MAX_CHAR_LENGTH;
toUCallback(cnv, cnv->toUContext, pArgs, (const char *)(source-1), 1, reason, pErrorCode);
source=(const uint8_t *)pArgs->source;
if(U_SUCCESS(*pErrorCode)) {
entry=pArgs->target-buffer;
if(entry>0) {
return ucnv_getUChar32KeepOverflow(cnv, buffer, entry);
}
} else if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
*pErrorCode=U_ZERO_ERROR;
return ucnv_getUChar32KeepOverflow(cnv, buffer, UTF_MAX_CHAR_LENGTH);
} else {
return 0xffff;
}
}
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return 0xffff;
}
U_CFUNC UChar32
_MBCSSimpleGetNextUChar(UConverterSharedData *sharedData,
const char **pSource, const char *sourceLimit,
UBool useFallback) {
const uint8_t *source;
const int32_t (*stateTable)[256];
const uint16_t *unicodeCodeUnits;
uint32_t offset;
uint8_t state, action;
int32_t entry;
source=(const uint8_t *)*pSource;
if(source>=(const uint8_t *)sourceLimit) {
return 0xffff;
}
#if 0
if(sharedData->table->mbcs.countStates==1) {
return _MBCSSingleSimpleGetNextUChar(sharedData, (uint8_t)(*(*pSource)++), useFallback);
}
#endif
stateTable=sharedData->table->mbcs.stateTable;
unicodeCodeUnits=sharedData->table->mbcs.unicodeCodeUnits;
offset=0;
state=0;
do {
entry=stateTable[state][*source++];
if(MBCS_ENTRY_IS_TRANSITION(entry)) {
state=(uint8_t)MBCS_ENTRY_TRANSITION_STATE(entry);
offset+=MBCS_ENTRY_TRANSITION_OFFSET(entry);
} else {
*pSource=(const char *)source;
action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
if(action==MBCS_STATE_VALID_16) {
offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
entry=unicodeCodeUnits[offset];
if(entry!=0xfffe) {
return (UChar32)entry;
} else if(UCNV_TO_U_USE_FALLBACK(cnv)) {
return _MBCSGetFallback(&sharedData->table->mbcs, offset);
} else {
return 0xfffe;
}
} else if(action==MBCS_STATE_VALID_DIRECT_16) {
return (UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
} else if(action==MBCS_STATE_VALID_16_PAIR) {
offset+=MBCS_ENTRY_FINAL_VALUE_16(entry);
entry=unicodeCodeUnits[offset++];
if(entry<0xd800) {
return (UChar32)entry;
} else if(UCNV_TO_U_USE_FALLBACK(cnv) ? entry<=0xdfff : entry<=0xdbff) {
return (UChar32)(((entry&0x3ff)<<10)+unicodeCodeUnits[offset]+(0x10000-0xdc00));
} else if(UCNV_TO_U_USE_FALLBACK(cnv) ? (entry&0xfffe)==0xe000 : entry==0xe000) {
return unicodeCodeUnits[offset];
} else if(entry==0xffff) {
return 0xffff;
} else {
return 0xfffe;
}
} else if(action==MBCS_STATE_VALID_DIRECT_20) {
return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
} else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
if(!TO_U_USE_FALLBACK(useFallback)) {
return 0xfffe;
}
return (UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
} else if(action==MBCS_STATE_FALLBACK_DIRECT_20) {
if(!TO_U_USE_FALLBACK(useFallback)) {
return 0xfffe;
}
return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
} else if(action==MBCS_STATE_CHANGE_ONLY) {
if(source==(const uint8_t *)sourceLimit) {
return 0xfffe;
}
} else if(action==MBCS_STATE_UNASSIGNED) {
return 0xfffe;
} else if(action==MBCS_STATE_ILLEGAL) {
return 0xffff;
} else {
}
state=(uint8_t)MBCS_ENTRY_FINAL_STATE(entry);
offset=0;
}
} while(source<(const uint8_t *)sourceLimit);
*pSource=(const char *)source;
return 0xffff;
}
#if 0
U_CFUNC UChar32
_MBCSSingleSimpleGetNextUChar(UConverterSharedData *sharedData,
uint8_t b, UBool useFallback) {
int32_t entry;
uint8_t action;
entry=sharedData->table->mbcs.stateTable[0][b];
if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) {
return (UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
}
action=(uint8_t)(MBCS_ENTRY_FINAL_ACTION(entry));
if(action==MBCS_STATE_VALID_DIRECT_20) {
return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
} else if(action==MBCS_STATE_FALLBACK_DIRECT_16) {
if(!TO_U_USE_FALLBACK(useFallback)) {
return 0xfffe;
}
return (UChar)MBCS_ENTRY_FINAL_VALUE_16(entry);
} else if(action==MBCS_STATE_FALLBACK_DIRECT_20) {
if(!TO_U_USE_FALLBACK(useFallback)) {
return 0xfffe;
}
return 0x10000+MBCS_ENTRY_FINAL_VALUE(entry);
} else if(action==MBCS_STATE_UNASSIGNED) {
return 0xfffe;
} else if(action==MBCS_STATE_ILLEGAL) {
return 0xffff;
} else {
return 0xffff;
}
}
#endif
U_CFUNC void
_MBCSFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UConverter *cnv;
const UChar *source, *sourceLimit;
uint8_t *target;
int32_t targetCapacity;
int32_t *offsets;
const uint16_t *table;
const uint8_t *p, *bytes;
uint8_t outputType;
UChar32 c;
int32_t prevSourceIndex, sourceIndex, nextSourceIndex;
UConverterCallbackReason reason;
uint32_t stage2Entry;
uint32_t value;
int32_t length, prevLength;
uint8_t unicodeMask;
cnv=pArgs->converter;
outputType=cnv->sharedData->table->mbcs.outputType;
unicodeMask=cnv->sharedData->table->mbcs.unicodeMask;
if(outputType==MBCS_OUTPUT_1 && !(unicodeMask&UCNV_HAS_SURROGATES)) {
if(!(unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
_MBCSSingleFromBMPWithOffsets(pArgs, pErrorCode);
} else {
_MBCSSingleFromUnicodeWithOffsets(pArgs, pErrorCode);
}
return;
} else if(outputType==MBCS_OUTPUT_2) {
_MBCSDoubleFromUnicodeWithOffsets(pArgs, pErrorCode);
return;
}
source=pArgs->source;
sourceLimit=pArgs->sourceLimit;
target=(uint8_t *)pArgs->target;
targetCapacity=pArgs->targetLimit-pArgs->target;
offsets=pArgs->offsets;
table=cnv->sharedData->table->mbcs.fromUnicodeTable;
if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
bytes=cnv->sharedData->table->mbcs.swapLFNLFromUnicodeBytes;
} else {
bytes=cnv->sharedData->table->mbcs.fromUnicodeBytes;
}
c=cnv->fromUSurrogateLead;
prevLength=cnv->fromUnicodeStatus;
prevSourceIndex=-1;
sourceIndex= c==0 ? 0 : -1;
nextSourceIndex=0;
if(c!=0 && targetCapacity>0) {
goto getTrail;
}
while(source<sourceLimit) {
if(targetCapacity>0) {
c=*source++;
++nextSourceIndex;
if(UTF_IS_SURROGATE(c) && !(unicodeMask&UCNV_HAS_SURROGATES)) {
if(UTF_IS_SURROGATE_FIRST(c)) {
getTrail:
if(source<sourceLimit) {
UChar trail=*source;
if(UTF_IS_SECOND_SURROGATE(trail)) {
++source;
++nextSourceIndex;
c=UTF16_GET_PAIR_VALUE(c, trail);
if(!(unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
goto unassigned;
}
} else {
reason=UCNV_ILLEGAL;
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
goto callback;
}
} else {
break;
}
} else {
reason=UCNV_ILLEGAL;
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
goto callback;
}
}
stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
switch(outputType) {
case MBCS_OUTPUT_2:
value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
if(value<=0xff) {
length=1;
} else {
length=2;
}
break;
case MBCS_OUTPUT_2_SISO:
cnv->fromUnicodeStatus=prevLength;
value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
if(value<=0xff) {
if(prevLength==1) {
length=1;
} else {
value|=(uint32_t)UCNV_SI<<8;
length=2;
prevLength=1;
}
} else {
if(prevLength==2) {
length=2;
} else {
value|=(uint32_t)UCNV_SO<<16;
length=3;
prevLength=2;
}
}
break;
case MBCS_OUTPUT_3:
p=MBCS_POINTER_3_FROM_STAGE_2(bytes, stage2Entry, c);
value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
if(value<=0xff) {
length=1;
} else if(value<=0xffff) {
length=2;
} else {
length=3;
}
break;
case MBCS_OUTPUT_4:
value=MBCS_VALUE_4_FROM_STAGE_2(bytes, stage2Entry, c);
if(value<=0xff) {
length=1;
} else if(value<=0xffff) {
length=2;
} else if(value<=0xffffff) {
length=3;
} else {
length=4;
}
break;
case MBCS_OUTPUT_3_EUC:
value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
if(value<=0xff) {
length=1;
} else if((value&0x8000)==0) {
value|=0x8e8000;
length=3;
} else if((value&0x80)==0) {
value|=0x8f0080;
length=3;
} else {
length=2;
}
break;
case MBCS_OUTPUT_4_EUC:
p=MBCS_POINTER_3_FROM_STAGE_2(bytes, stage2Entry, c);
value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
if(value<=0xff) {
length=1;
} else if(value<=0xffff) {
length=2;
} else if((value&0x800000)==0) {
value|=0x8e800000;
length=4;
} else if((value&0x8000)==0) {
value|=0x8f008000;
length=4;
} else {
length=3;
}
break;
default:
value=0;
length=0;
break;
}
if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c)!=0 ||
(UCNV_FROM_U_USE_FALLBACK(cnv, c) && (value!=0 || c==0)))
) {
goto unassigned;
}
if(length<=targetCapacity) {
if(offsets==NULL) {
switch(length) {
case 4:
*target++=(uint8_t)(value>>24);
case 3:
*target++=(uint8_t)(value>>16);
case 2:
*target++=(uint8_t)(value>>8);
case 1:
*target++=(uint8_t)value;
default:
break;
}
} else {
switch(length) {
case 4:
*target++=(uint8_t)(value>>24);
*offsets++=sourceIndex;
case 3:
*target++=(uint8_t)(value>>16);
*offsets++=sourceIndex;
case 2:
*target++=(uint8_t)(value>>8);
*offsets++=sourceIndex;
case 1:
*target++=(uint8_t)value;
*offsets++=sourceIndex;
default:
break;
}
}
targetCapacity-=length;
} else {
uint8_t *charErrorBuffer;
length-=targetCapacity;
charErrorBuffer=(uint8_t *)cnv->charErrorBuffer;
switch(length) {
case 3:
*charErrorBuffer++=(uint8_t)(value>>16);
case 2:
*charErrorBuffer++=(uint8_t)(value>>8);
case 1:
*charErrorBuffer=(uint8_t)value;
default:
break;
}
cnv->charErrorBufferLength=(int8_t)length;
value>>=8*length;
switch(targetCapacity) {
case 3:
*target++=(uint8_t)(value>>16);
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
case 2:
*target++=(uint8_t)(value>>8);
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
case 1:
*target++=(uint8_t)value;
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
default:
break;
}
targetCapacity=0;
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
c=0;
break;
}
c=0;
if(offsets!=NULL) {
prevSourceIndex=sourceIndex;
sourceIndex=nextSourceIndex;
}
continue;
unassigned:
reason=UCNV_UNASSIGNED;
*pErrorCode=U_INVALID_CHAR_FOUND;
callback:
pArgs->source=source;
pArgs->target=(char *)target;
pArgs->offsets=offsets;
cnv->fromUSurrogateLead=0;
prevSourceIndex=sourceIndex;
fromUCallback(cnv, cnv->fromUContext, pArgs, c, reason, pErrorCode);
c=cnv->fromUSurrogateLead;
prevLength=cnv->fromUnicodeStatus;
offsets=ucnv_updateCallbackOffsets(offsets, ((uint8_t *)pArgs->target)-target, sourceIndex);
target=(uint8_t *)pArgs->target;
sourceIndex=nextSourceIndex+(pArgs->source-source);
source=pArgs->source;
targetCapacity=(uint8_t *)pArgs->targetLimit-target;
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
break;
} else if(U_FAILURE(*pErrorCode)) {
c=0;
break;
} else if(cnv->charErrorBufferLength>0) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
break;
}
} else {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
break;
}
}
if(pArgs->flush && source>=sourceLimit && U_SUCCESS(*pErrorCode)) {
if(c!=0) {
*pErrorCode=U_TRUNCATED_CHAR_FOUND;
c=0;
prevLength=1;
} else if(outputType==MBCS_OUTPUT_2_SISO && prevLength==2) {
if(targetCapacity>0) {
*target++=(uint8_t)UCNV_SI;
if(offsets!=NULL) {
*offsets++=prevSourceIndex;
}
} else {
cnv->charErrorBuffer[0]=(char)UCNV_SI;
cnv->charErrorBufferLength=1;
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
}
prevLength=1;
}
if(U_SUCCESS(*pErrorCode)) {
c=0;
prevLength=1;
}
}
cnv->fromUSurrogateLead=(UChar)c;
cnv->fromUnicodeStatus=prevLength;
pArgs->source=source;
pArgs->target=(char *)target;
pArgs->offsets=offsets;
}
static void
_MBCSDoubleFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UConverter *cnv;
const UChar *source, *sourceLimit;
uint8_t *target;
int32_t targetCapacity;
int32_t *offsets;
const uint16_t *table;
const uint8_t *bytes;
UChar32 c;
int32_t sourceIndex, nextSourceIndex;
UConverterCallbackReason reason;
uint32_t stage2Entry;
uint32_t value;
int32_t length, prevLength;
uint8_t unicodeMask;
cnv=pArgs->converter;
unicodeMask=cnv->sharedData->table->mbcs.unicodeMask;
source=pArgs->source;
sourceLimit=pArgs->sourceLimit;
target=(uint8_t *)pArgs->target;
targetCapacity=pArgs->targetLimit-pArgs->target;
offsets=pArgs->offsets;
table=cnv->sharedData->table->mbcs.fromUnicodeTable;
if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
bytes=cnv->sharedData->table->mbcs.swapLFNLFromUnicodeBytes;
} else {
bytes=cnv->sharedData->table->mbcs.fromUnicodeBytes;
}
c=cnv->fromUSurrogateLead;
prevLength=cnv->fromUnicodeStatus;
sourceIndex= c==0 ? 0 : -1;
nextSourceIndex=0;
if(c!=0 && targetCapacity>0) {
goto getTrail;
}
while(source<sourceLimit) {
if(targetCapacity>0) {
c=*source++;
++nextSourceIndex;
if(UTF_IS_SURROGATE(c) && !(unicodeMask&UCNV_HAS_SURROGATES)) {
if(UTF_IS_SURROGATE_FIRST(c)) {
getTrail:
if(source<sourceLimit) {
UChar trail=*source;
if(UTF_IS_SECOND_SURROGATE(trail)) {
++source;
++nextSourceIndex;
c=UTF16_GET_PAIR_VALUE(c, trail);
if(!(unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
goto unassigned;
}
} else {
reason=UCNV_ILLEGAL;
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
goto callback;
}
} else {
break;
}
} else {
reason=UCNV_ILLEGAL;
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
goto callback;
}
}
stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
if(value<=0xff) {
length=1;
} else {
length=2;
}
if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c) ||
(UCNV_FROM_U_USE_FALLBACK(cnv, c) && (value!=0 || c==0)))
) {
goto unassigned;
}
if(length==1) {
*target++=(uint8_t)value;
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
--targetCapacity;
} else {
*target++=(uint8_t)(value>>8);
if(2<=targetCapacity) {
*target++=(uint8_t)value;
if(offsets!=NULL) {
*offsets++=sourceIndex;
*offsets++=sourceIndex;
}
targetCapacity-=2;
} else {
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
cnv->charErrorBuffer[0]=(char)value;
cnv->charErrorBufferLength=1;
targetCapacity=0;
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
c=0;
break;
}
}
c=0;
sourceIndex=nextSourceIndex;
continue;
unassigned:
reason=UCNV_UNASSIGNED;
*pErrorCode=U_INVALID_CHAR_FOUND;
callback:
pArgs->source=source;
pArgs->target=(char *)target;
pArgs->offsets=offsets;
cnv->fromUSurrogateLead=0;
cnv->fromUnicodeStatus=prevLength;
fromUCallback(cnv, cnv->fromUContext, pArgs, c, reason, pErrorCode);
c=cnv->fromUSurrogateLead;
prevLength=cnv->fromUnicodeStatus;
offsets=ucnv_updateCallbackOffsets(offsets, ((uint8_t *)pArgs->target)-target, sourceIndex);
target=(uint8_t *)pArgs->target;
sourceIndex=nextSourceIndex+(pArgs->source-source);
source=pArgs->source;
targetCapacity=(uint8_t *)pArgs->targetLimit-target;
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
break;
} else if(U_FAILURE(*pErrorCode)) {
c=0;
break;
} else if(cnv->charErrorBufferLength>0) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
break;
}
} else {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
break;
}
}
if(pArgs->flush && source>=sourceLimit) {
if(c!=0 && U_SUCCESS(*pErrorCode)) {
*pErrorCode=U_TRUNCATED_CHAR_FOUND;
}
cnv->fromUSurrogateLead=0;
cnv->fromUnicodeStatus=1;
} else {
cnv->fromUSurrogateLead=(UChar)c;
cnv->fromUnicodeStatus=prevLength;
}
pArgs->source=source;
pArgs->target=(char *)target;
pArgs->offsets=offsets;
}
static void
_MBCSSingleFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UConverter *cnv;
const UChar *source, *sourceLimit;
uint8_t *target;
int32_t targetCapacity;
int32_t *offsets;
const uint16_t *table;
const uint16_t *results;
UChar32 c;
int32_t sourceIndex, nextSourceIndex;
UConverterCallbackReason reason;
uint16_t value, minValue;
UBool hasSupplementary;
cnv=pArgs->converter;
source=pArgs->source;
sourceLimit=pArgs->sourceLimit;
target=(uint8_t *)pArgs->target;
targetCapacity=pArgs->targetLimit-pArgs->target;
offsets=pArgs->offsets;
table=cnv->sharedData->table->mbcs.fromUnicodeTable;
if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
results=(uint16_t *)cnv->sharedData->table->mbcs.swapLFNLFromUnicodeBytes;
} else {
results=(uint16_t *)cnv->sharedData->table->mbcs.fromUnicodeBytes;
}
if(cnv->useFallback) {
minValue=0x800;
} else {
minValue=0xc00;
}
hasSupplementary=(UBool)(cnv->sharedData->table->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY);
c=cnv->fromUSurrogateLead;
sourceIndex= c==0 ? 0 : -1;
nextSourceIndex=0;
if(c!=0 && targetCapacity>0) {
goto getTrail;
}
while(source<sourceLimit) {
if(targetCapacity>0) {
c=*source++;
++nextSourceIndex;
if(UTF_IS_SURROGATE(c)) {
if(UTF_IS_SURROGATE_FIRST(c)) {
getTrail:
if(source<sourceLimit) {
UChar trail=*source;
if(UTF_IS_SECOND_SURROGATE(trail)) {
++source;
++nextSourceIndex;
c=UTF16_GET_PAIR_VALUE(c, trail);
if(!hasSupplementary) {
goto unassigned;
}
} else {
reason=UCNV_ILLEGAL;
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
goto callback;
}
} else {
break;
}
} else {
reason=UCNV_ILLEGAL;
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
goto callback;
}
}
value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
if(value>=minValue) {
*target++=(uint8_t)value;
if(offsets!=NULL) {
*offsets++=sourceIndex;
}
--targetCapacity;
c=0;
sourceIndex=nextSourceIndex;
continue;
} else {
}
unassigned:
reason=UCNV_UNASSIGNED;
*pErrorCode=U_INVALID_CHAR_FOUND;
callback:
pArgs->source=source;
pArgs->target=(char *)target;
pArgs->offsets=offsets;
cnv->fromUSurrogateLead=0;
fromUCallback(cnv, cnv->fromUContext, pArgs, c, reason, pErrorCode);
c=cnv->fromUSurrogateLead;
offsets=ucnv_updateCallbackOffsets(offsets, ((uint8_t *)pArgs->target)-target, sourceIndex);
target=(uint8_t *)pArgs->target;
sourceIndex=nextSourceIndex+(pArgs->source-source);
source=pArgs->source;
targetCapacity=(uint8_t *)pArgs->targetLimit-target;
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
break;
} else if(U_FAILURE(*pErrorCode)) {
c=0;
break;
} else if(cnv->charErrorBufferLength>0) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
break;
}
} else {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
break;
}
}
if(pArgs->flush && source>=sourceLimit) {
if(c!=0 && U_SUCCESS(*pErrorCode)) {
*pErrorCode=U_TRUNCATED_CHAR_FOUND;
}
cnv->fromUSurrogateLead=0;
} else {
cnv->fromUSurrogateLead=(UChar)c;
}
pArgs->source=source;
pArgs->target=(char *)target;
pArgs->offsets=offsets;
}
static void
_MBCSSingleFromBMPWithOffsets(UConverterFromUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UConverter *cnv;
const UChar *source, *sourceLimit, *lastSource;
uint8_t *target;
int32_t targetCapacity, length;
int32_t *offsets;
const uint16_t *table;
const uint16_t *results;
UChar32 c;
int32_t sourceIndex;
UConverterCallbackReason reason;
uint16_t value, minValue;
cnv=pArgs->converter;
source=pArgs->source;
sourceLimit=pArgs->sourceLimit;
target=(uint8_t *)pArgs->target;
targetCapacity=pArgs->targetLimit-pArgs->target;
offsets=pArgs->offsets;
table=cnv->sharedData->table->mbcs.fromUnicodeTable;
if((cnv->options&UCNV_OPTION_SWAP_LFNL)!=0) {
results=(uint16_t *)cnv->sharedData->table->mbcs.swapLFNLFromUnicodeBytes;
} else {
results=(uint16_t *)cnv->sharedData->table->mbcs.fromUnicodeBytes;
}
if(cnv->useFallback) {
minValue=0x800;
} else {
minValue=0xc00;
}
c=cnv->fromUSurrogateLead;
sourceIndex= c==0 ? 0 : -1;
lastSource=source;
length=sourceLimit-source;
if(length<targetCapacity) {
targetCapacity=length;
}
if(c!=0 && targetCapacity>0) {
goto getTrail;
}
#if MBCS_UNROLL_SINGLE_FROM_BMP
unrolled:
if(targetCapacity>=4) {
int32_t count, loops;
uint16_t andedValues;
loops=count=targetCapacity>>2;
do {
c=*source++;
andedValues=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
*target++=(uint8_t)value;
c=*source++;
andedValues&=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
*target++=(uint8_t)value;
c=*source++;
andedValues&=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
*target++=(uint8_t)value;
c=*source++;
andedValues&=value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
*target++=(uint8_t)value;
if(andedValues<minValue) {
source-=4;
target-=4;
break;
}
} while(--count>0);
count=loops-count;
targetCapacity-=4*count;
if(offsets!=NULL) {
lastSource+=4*count;
while(count>0) {
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
*offsets++=sourceIndex++;
--count;
}
}
c=0;
}
#endif
while(targetCapacity>0) {
c=*source++;
value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
if(value>=minValue) {
*target++=(uint8_t)value;
--targetCapacity;
c=0;
continue;
} else if(!UTF_IS_SURROGATE(c)) {
reason=UCNV_UNASSIGNED;
*pErrorCode=U_INVALID_CHAR_FOUND;
} else if(UTF_IS_SURROGATE_FIRST(c)) {
getTrail:
if(source<sourceLimit) {
UChar trail=*source;
if(UTF_IS_SECOND_SURROGATE(trail)) {
++source;
c=UTF16_GET_PAIR_VALUE(c, trail);
reason=UCNV_UNASSIGNED;
*pErrorCode=U_INVALID_CHAR_FOUND;
} else {
reason=UCNV_ILLEGAL;
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
}
} else {
break;
}
} else {
reason=UCNV_ILLEGAL;
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
}
length=UTF_CHAR_LENGTH(c);
if(offsets!=NULL) {
int32_t count=(int32_t)(source-lastSource);
count-=length;
while(count>0) {
*offsets++=sourceIndex++;
--count;
}
}
pArgs->source=source;
pArgs->target=(char *)target;
pArgs->offsets=offsets;
cnv->fromUSurrogateLead=0;
fromUCallback(cnv, cnv->fromUContext, pArgs, c, reason, pErrorCode);
c=cnv->fromUSurrogateLead;
offsets=ucnv_updateCallbackOffsets(offsets, ((uint8_t *)pArgs->target)-target, sourceIndex);
target=(uint8_t *)pArgs->target;
sourceIndex+=length+(pArgs->source-source);
source=lastSource=pArgs->source;
targetCapacity=(uint8_t *)pArgs->targetLimit-target;
length=sourceLimit-source;
if(length<targetCapacity) {
targetCapacity=length;
}
if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
break;
} else if(U_FAILURE(*pErrorCode)) {
c=0;
break;
} else if(cnv->charErrorBufferLength>0) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
break;
}
#if MBCS_UNROLL_SINGLE_FROM_BMP
goto unrolled;
#endif
}
if(U_SUCCESS(*pErrorCode) && source<sourceLimit && target>=(uint8_t *)pArgs->targetLimit) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
}
if(offsets!=NULL) {
size_t count=source-lastSource;
while(count>0) {
*offsets++=sourceIndex++;
--count;
}
}
if(pArgs->flush && source>=sourceLimit) {
if(c!=0 && U_SUCCESS(*pErrorCode)) {
*pErrorCode=U_TRUNCATED_CHAR_FOUND;
}
cnv->fromUSurrogateLead=0;
} else {
cnv->fromUSurrogateLead=(UChar)c;
}
pArgs->source=source;
pArgs->target=(char *)target;
pArgs->offsets=offsets;
}
U_CFUNC int32_t
_MBCSFromUChar32(UConverterSharedData *sharedData,
UChar32 c, uint32_t *pValue,
UBool useFallback) {
const uint16_t *table=sharedData->table->mbcs.fromUnicodeTable;
const uint8_t *p;
uint32_t stage2Entry;
uint32_t value;
int32_t length;
if(c>=0x10000 && !(sharedData->table->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
return 0;
}
if(sharedData->table->mbcs.outputType==MBCS_OUTPUT_1) {
value=MBCS_SINGLE_RESULT_FROM_U(table, (uint16_t *)sharedData->table->mbcs.fromUnicodeBytes, c);
if(useFallback ? value>=0x800 : value>=0xc00) {
*pValue=value&0xff;
return 1;
} else {
return 0;
}
}
stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
switch(sharedData->table->mbcs.outputType) {
case MBCS_OUTPUT_2:
value=MBCS_VALUE_2_FROM_STAGE_2(sharedData->table->mbcs.fromUnicodeBytes, stage2Entry, c);
if(value<=0xff) {
length=1;
} else {
length=2;
}
break;
case MBCS_OUTPUT_3:
p=MBCS_POINTER_3_FROM_STAGE_2(sharedData->table->mbcs.fromUnicodeBytes, stage2Entry, c);
value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
if(value<=0xff) {
length=1;
} else if(value<=0xffff) {
length=2;
} else {
length=3;
}
break;
case MBCS_OUTPUT_4:
value=MBCS_VALUE_4_FROM_STAGE_2(sharedData->table->mbcs.fromUnicodeBytes, stage2Entry, c);
if(value<=0xff) {
length=1;
} else if(value<=0xffff) {
length=2;
} else if(value<=0xffffff) {
length=3;
} else {
length=4;
}
break;
case MBCS_OUTPUT_3_EUC:
value=MBCS_VALUE_2_FROM_STAGE_2(sharedData->table->mbcs.fromUnicodeBytes, stage2Entry, c);
if(value<=0xff) {
length=1;
} else if((value&0x8000)==0) {
value|=0x8e8000;
length=3;
} else if((value&0x80)==0) {
value|=0x8f0080;
length=3;
} else {
length=2;
}
break;
case MBCS_OUTPUT_4_EUC:
p=MBCS_POINTER_3_FROM_STAGE_2(sharedData->table->mbcs.fromUnicodeBytes, stage2Entry, c);
value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2];
if(value<=0xff) {
length=1;
} else if(value<=0xffff) {
length=2;
} else if((value&0x800000)==0) {
value|=0x8e800000;
length=4;
} else if((value&0x8000)==0) {
value|=0x8f008000;
length=4;
} else {
length=3;
}
break;
default:
return -1;
}
if( MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c) ||
(FROM_U_USE_FALLBACK(useFallback, c) && (value!=0 || c==0))
) {
*pValue=value;
return length;
} else {
return 0;
}
}
#if 0
U_CFUNC int32_t
_MBCSSingleFromUChar32(UConverterSharedData *sharedData,
UChar32 c,
UBool useFallback) {
const uint16_t *table;
int32_t value;
if(c>=0x10000 && !(sharedData->table->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
return -1;
}
table=sharedData->table->mbcs.fromUnicodeTable;
value=MBCS_SINGLE_RESULT_FROM_U(table, (uint16_t *)sharedData->table->mbcs.fromUnicodeBytes, c);
if(useFallback ? value>=0x800 : value>=0xc00) {
return value&0xff;
} else {
return -1;
}
}
#endif
static void
_MBCSGetStarters(const UConverter* cnv,
UBool starters[256],
UErrorCode *pErrorCode) {
const int32_t *state0=cnv->sharedData->table->mbcs.stateTable[0];
int i;
for(i=0; i<256; ++i) {
starters[i]= (UBool)MBCS_ENTRY_IS_TRANSITION(state0[i]);
}
}
U_CFUNC UBool
_MBCSIsLeadByte(UConverterSharedData *sharedData, char byte) {
return (UBool)MBCS_ENTRY_IS_TRANSITION(sharedData->table->mbcs.stateTable[0][(uint8_t)byte]);
}
static void
_MBCSWriteSub(UConverterFromUnicodeArgs *pArgs,
int32_t offsetIndex,
UErrorCode *pErrorCode) {
UConverter *cnv=pArgs->converter;
char *p, *subchar;
char buffer[4];
int32_t length;
if(cnv->subChar1!=0 && cnv->invalidUCharBuffer[0]<=0xff) {
subchar=(char *)&cnv->subChar1;
length=1;
} else {
subchar=(char *)cnv->subChar;
length=cnv->subCharLen;
}
switch(cnv->sharedData->table->mbcs.outputType) {
case MBCS_OUTPUT_2_SISO:
p=buffer;
switch(length) {
case 1:
if(cnv->fromUnicodeStatus==2) {
cnv->fromUnicodeStatus=1;
*p++=UCNV_SI;
}
*p++=subchar[0];
break;
case 2:
if(cnv->fromUnicodeStatus==1) {
cnv->fromUnicodeStatus=2;
*p++=UCNV_SO;
}
*p++=subchar[0];
*p++=subchar[1];
break;
default:
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return;
}
ucnv_cbFromUWriteBytes(pArgs,
buffer, (int32_t)(p-buffer),
offsetIndex, pErrorCode);
break;
default:
ucnv_cbFromUWriteBytes(pArgs,
subchar, length,
offsetIndex, pErrorCode);
break;
}
}
U_CFUNC UConverterType
_MBCSGetType(const UConverter* converter) {
if(converter->sharedData->table->mbcs.countStates==1) {
return (UConverterType)UCNV_SBCS;
} else if((converter->sharedData->table->mbcs.outputType&0xff)==MBCS_OUTPUT_2_SISO) {
return (UConverterType)UCNV_EBCDIC_STATEFUL;
} else if(converter->sharedData->staticData->minBytesPerChar==2 && converter->sharedData->staticData->maxBytesPerChar==2) {
return (UConverterType)UCNV_DBCS;
}
return (UConverterType)UCNV_MBCS;
}
static const UConverterImpl _MBCSImpl={
UCNV_MBCS,
_MBCSLoad,
_MBCSUnload,
_MBCSOpen,
NULL,
_MBCSReset,
_MBCSToUnicodeWithOffsets,
_MBCSToUnicodeWithOffsets,
_MBCSFromUnicodeWithOffsets,
_MBCSFromUnicodeWithOffsets,
_MBCSGetNextUChar,
_MBCSGetStarters,
_MBCSGetName,
_MBCSWriteSub,
NULL,
_MBCSGetUnicodeSet
};
const UConverterSharedData _MBCSData={
sizeof(UConverterSharedData), 1,
NULL, NULL, NULL, FALSE, &_MBCSImpl,
0
};
static void
fromUCallback(UConverter *cnv,
const void *context, UConverterFromUnicodeArgs *pArgs,
UChar32 codePoint,
UConverterCallbackReason reason, UErrorCode *pErrorCode) {
int32_t i;
if((cnv->options&_MBCS_OPTION_GB18030)!=0 && reason==UCNV_UNASSIGNED) {
const uint32_t *range;
range=gb18030Ranges[0];
for(i=0; i<sizeof(gb18030Ranges)/sizeof(gb18030Ranges[0]); range+=4, ++i) {
if(range[0]<=(uint32_t)codePoint && (uint32_t)codePoint<=range[1]) {
uint32_t linear;
char bytes[4];
*pErrorCode=U_ZERO_ERROR;
linear=range[2]-LINEAR_18030_BASE;
linear+=((uint32_t)codePoint-range[0]);
bytes[3]=(char)(0x30+linear%10); linear/=10;
bytes[2]=(char)(0x81+linear%126); linear/=126;
bytes[1]=(char)(0x30+linear%10); linear/=10;
bytes[0]=(char)(0x81+linear);
ucnv_cbFromUWriteBytes(pArgs, bytes, 4, 0, pErrorCode);
return;
}
}
}
i=0;
UTF_APPEND_CHAR_UNSAFE(cnv->invalidUCharBuffer, i, codePoint);
cnv->invalidUCharLength=(int8_t)i;
cnv->fromUCharErrorBehaviour(context, pArgs, cnv->invalidUCharBuffer, i, codePoint, reason, pErrorCode);
}
static void
toUCallback(UConverter *cnv,
const void *context, UConverterToUnicodeArgs *pArgs,
const char *codeUnits, int32_t length,
UConverterCallbackReason reason, UErrorCode *pErrorCode) {
int32_t i;
if((cnv->options&_MBCS_OPTION_GB18030)!=0 && reason==UCNV_UNASSIGNED && length==4) {
const uint32_t *range;
uint32_t linear;
linear=LINEAR_18030((uint8_t)codeUnits[0], (uint8_t)codeUnits[1], (uint8_t)codeUnits[2], (uint8_t)codeUnits[3]);
range=gb18030Ranges[0];
for(i=0; i<sizeof(gb18030Ranges)/sizeof(gb18030Ranges[0]); range+=4, ++i) {
if(range[2]<=linear && linear<=range[3]) {
UChar u[UTF_MAX_CHAR_LENGTH];
*pErrorCode=U_ZERO_ERROR;
linear=range[0]+(linear-range[2]);
i=0;
UTF_APPEND_CHAR_UNSAFE(u, i, linear);
ucnv_cbToUWriteUChars(pArgs, u, i, 0, pErrorCode);
return;
}
}
}
for(i=0; i<length; ++i) {
cnv->invalidCharBuffer[i]=codeUnits[i];
}
cnv->invalidCharLength=(int8_t)length;
cnv->fromCharErrorBehaviour(context, pArgs, codeUnits, length, reason, pErrorCode);
}
#endif