/* bit flag for UConverter.options indicating GB 18030 special handling */ #define _MBCS_OPTION_GB18030 0x8000
/* bit flag for UConverter.options indicating KEIS,JEF,JIF special handling */ #define _MBCS_OPTION_KEIS 0x01000 #define _MBCS_OPTION_JEF 0x02000 #define _MBCS_OPTION_JIPS 0x04000
range=gb18030Ranges[0]; for(i=0; i<UPRV_LENGTHOF(gb18030Ranges); range+=4, ++i) { if (range[0] <= static_cast<uint32_t>(cp) && static_cast<uint32_t>(cp) <= range[1]) { /* found the Unicode code point, output the four-byte sequence for it */
uint32_t linear; char bytes[4];
/* get the linear value of the first GB 18030 code in this range */
linear=range[2]-LINEAR_18030_BASE;
/* add the offset from the beginning of the range */
linear += (static_cast<uint32_t>(cp) - range[0]);
/* turn this into a four-byte sequence */
bytes[3] = static_cast<char>(0x30 + linear % 10); linear /= 10;
bytes[2] = static_cast<char>(0x81 + linear % 126); linear /= 126;
bytes[1] = static_cast<char>(0x30 + linear % 10); linear /= 10;
bytes[0] = static_cast<char>(0x81 + linear);
linear=LINEAR_18030(cnv->toUBytes[0], cnv->toUBytes[1], cnv->toUBytes[2], cnv->toUBytes[3]);
range=gb18030Ranges[0]; for(i=0; i<UPRV_LENGTHOF(gb18030Ranges); range+=4, ++i) { if(range[2]<=linear && linear<=range[3]) { /* found the sequence, output the Unicode code point for it */
*pErrorCode=U_ZERO_ERROR;
/* add the linear difference between the input and start sequences to the start code point */
linear=range[0]+(linear-range[2]);
/* output this code point */
ucnv_toUWriteCodePoint(cnv, linear, target, targetLimit, offsets, sourceIndex, pErrorCode);
return0;
}
}
}
/* no mapping */
*pErrorCode=U_INVALID_CHAR_FOUND; return length;
}
/* copy and modify the to-Unicode state table */
newStateTable = reinterpret_cast<int32_t(*)[256]>(p);
uprv_memcpy(newStateTable, mbcsTable->stateTable, mbcsTable->countStates*1024);
/* copy and modify the from-Unicode result table */
newResults = reinterpret_cast<uint16_t*>(newStateTable[mbcsTable->countStates]);
uprv_memcpy(newResults, bytes, sizeofFromUBytes);
/* conveniently, the table access macros work on the left side of expressions */ 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/* MBCS_OUTPUT_2_SISO */ {
stage2Entry=MBCS_STAGE_2_FROM_U(table, U_LF);
MBCS_VALUE_2_FROM_STAGE_2(newResults, stage2Entry, U_LF)=EBCDIC_NL;
/* set the canonical converter name */
name = reinterpret_cast<char*>(newResults) + sizeofFromUBytes;
uprv_strcpy(name, sharedData->staticData->name);
uprv_strcat(name, UCNV_SWAP_LFNL_OPTION_STRING);
/* set the pointers */
icu::umtx_lock(nullptr); if(mbcsTable->swapLFNLStateTable==nullptr) {
mbcsTable->swapLFNLStateTable=newStateTable;
mbcsTable->swapLFNLFromUnicodeBytes = reinterpret_cast<uint8_t*>(newResults);
mbcsTable->swapLFNLName=name;
/* for EUC outputTypes, modify the value like genmbcs.c's transformEUC() */ switch(mbcsTable->outputType) { case MBCS_OUTPUT_3_EUC: if(value<=0xffff) { /* short sequences are stored directly */ /* code set 0 or 1 */
} elseif(value<=0x8effff) { /* code set 2 */
value&=0x7fff;
} else/* first byte is 0x8f */ { /* code set 3 */
value&=0xff7f;
} break; case MBCS_OUTPUT_4_EUC: if(value<=0xffffff) { /* short sequences are stored directly */ /* code set 0 or 1 */
} elseif(value<=0x8effffff) { /* code set 2 */
value&=0x7fffff;
} else/* first byte is 0x8f */ { /* code set 3 */
value&=0xff7fff;
} break; default: break;
}
/* copy existing data and reroute the pointers */
stage1 = reinterpret_cast<uint16_t*>(mbcsTable->reconstitutedData);
uprv_memcpy(stage1, mbcsTable->fromUnicodeTable, stage1Length*2);
/* indexes into stage 2 count from the bottom of the fromUnicodeTable */
stage2 = reinterpret_cast<uint32_t*>(stage1);
/* reconstitute the initial part of stage 2 from the mbcsIndex */
{
int32_t stageUTF8Length = (static_cast<int32_t>(mbcsTable->maxFastUChar) + 1) >> 6;
int32_t stageUTF8Index=0;
int32_t st1, st2, st3, i;
for(st1=0; stageUTF8Index<stageUTF8Length; ++st1) {
st2=stage1[st1]; if (st2 != static_cast<int32_t>(stage1Length) / 2) { /* each stage 2 block has 64 entries corresponding to 16 entries in the mbcsIndex */ for(i=0; i<16; ++i) {
st3=mbcsTable->mbcsIndex[stageUTF8Index++]; if(st3!=0) { /* an stage 2 entry's index is per stage 3 16-block, not per stage 3 entry */
st3>>=4; /* *4stage2entriespointto4consecutivestage316-blockswhichare *allocatedtogetherasasingle64-blockforaccessfromthembcsIndex
*/
stage2[st2++]=st3++;
stage2[st2++]=st3++;
stage2[st2++]=st3++;
stage2[st2++]=st3;
} else { /* no stage 3 block, skip */
st2+=4;
}
}
} else { /* no stage 2 block, skip */
stageUTF8Index+=16;
}
}
}
/* reconstitute fromUnicodeBytes with roundtrips from toUnicode data */
ucnv_MBCSEnumToUnicode(mbcsTable, writeStage3Roundtrip, mbcsTable, pErrorCode);
}
/* extension-only file, load the base table and set values appropriately */ if((extIndexes=mbcsTable->extIndexes)==nullptr) { /* extension-only file without extension */
*pErrorCode=U_INVALID_TABLE_FORMAT; return;
}
if(pArgs->nestedLoads!=1) { /* an extension table must not be loaded as a base table */
*pErrorCode=U_INVALID_TABLE_FILE; return;
}
/* load the base table */
baseName = reinterpret_cast<constchar*>(header) + headerLength * 4; if(0==uprv_strcmp(baseName, sharedData->staticData->name)) { /* forbid loading this same extension-only file */
*pErrorCode=U_INVALID_TABLE_FORMAT; return;
}
/* TODO parse package name out of the prefix of the base name in the extension .cnv file? */
args.size=sizeof(UConverterLoadArgs);
args.nestedLoads=2;
args.onlyTestIsLoadable=pArgs->onlyTestIsLoadable;
args.reserved=pArgs->reserved;
args.options=pArgs->options;
args.pkg=pArgs->pkg;
args.name=baseName;
baseSharedData=ucnv_load(&args, pErrorCode); if(U_FAILURE(*pErrorCode)) { return;
} if( baseSharedData->staticData->conversionType!=UCNV_MBCS ||
baseSharedData->mbcs.baseSharedData!=nullptr
) {
ucnv_unload(baseSharedData);
*pErrorCode=U_INVALID_TABLE_FORMAT; return;
} if(pArgs->onlyTestIsLoadable) { /* *Exitassoonasweknowthatwecanloadtheconverter *andtheformatisvalidandsupported. *Theworstthatcanhappeninthefollowingcodeisamemory *allocationerror.
*/
ucnv_unload(baseSharedData); return;
}
/* copy the base table data */
uprv_memcpy(mbcsTable, &baseSharedData->mbcs, sizeof(UConverterMBCSTable));
/* overwrite values with relevant ones for the extension converter */
mbcsTable->baseSharedData=baseSharedData;
mbcsTable->extIndexes=extIndexes;
/* *Setaspecial,runtime-onlyoutputTypeiftheextensionconverter *isaDBCSversionofabaseconverterthatalsomapssinglebytes.
*/ if( sharedData->staticData->conversionType==UCNV_DBCS ||
(sharedData->staticData->conversionType==UCNV_MBCS &&
sharedData->staticData->minBytesPerChar>=2)
) { if(baseSharedData->mbcs.outputType==MBCS_OUTPUT_2_SISO) { /* the base converter is SI/SO-stateful */
int32_t entry;
/* get the dbcs state from the state table entry for SO=0x0e */
entry=mbcsTable->stateTable[0][0xe]; if( MBCS_ENTRY_IS_FINAL(entry) &&
MBCS_ENTRY_FINAL_ACTION(entry)==MBCS_STATE_CHANGE_ONLY &&
MBCS_ENTRY_FINAL_STATE(entry)!=0
) {
mbcsTable->dbcsOnlyState = static_cast<uint8_t>(MBCS_ENTRY_FINAL_STATE(entry));
mbcsTable->outputType=MBCS_OUTPUT_DBCS_ONLY;
}
} elseif(
baseSharedData->staticData->conversionType==UCNV_MBCS &&
baseSharedData->staticData->minBytesPerChar==1 &&
baseSharedData->staticData->maxBytesPerChar==2 &&
mbcsTable->countStates<=127
) { /* non-stateful base converter, need to modify the state table */
int32_t (*newStateTable)[256];
int32_t *state;
int32_t i, count;
/* allocate a new state table and copy the base state table contents */
count=mbcsTable->countStates;
newStateTable = static_cast<int32_t(*)[256]>(uprv_malloc((count + 1) * 1024)); if(newStateTable==nullptr) {
ucnv_unload(baseSharedData);
*pErrorCode=U_MEMORY_ALLOCATION_ERROR; return;
}
/* change all final single-byte entries to go to a new all-illegal state */
state=newStateTable[0]; for(i=0; i<256; ++i) { if(MBCS_ENTRY_IS_FINAL(state[i])) {
state[i]=MBCS_ENTRY_TRANSITION(count, 0);
}
}
/* build the new all-illegal state */
state=newStateTable[count]; for(i=0; i<256; ++i) {
state[i]=MBCS_ENTRY_FINAL(0, MBCS_STATE_ILLEGAL, 0);
}
mbcsTable->stateTable=(const int32_t (*)[256])newStateTable;
mbcsTable->countStates = static_cast<uint8_t>(count + 1);
mbcsTable->stateTableOwned=true;
mbcsTable->outputType=MBCS_OUTPUT_DBCS_ONLY;
}
}
/* *unlikebelowforfileswithbasetables,donotgettheunicodeMask *fromthesharedData;instead,usethebasetable'sunicodeMask, *whichwecopiedinthememcpyabove; *thisisnecessarybecausethestaticdataunicodeMask,especially *theUCNV_HAS_SUPPLEMENTARYflag,ispartofthebasetabledata
*/
} else { /* conversion file with a base table; an additional extension table is optional */ /* make sure that the output type is known */ 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: /* OK */ break; default:
*pErrorCode=U_INVALID_TABLE_FORMAT; return;
} if(pArgs->onlyTestIsLoadable) { /* *Exitassoonasweknowthatwecanloadtheconverter *andtheformatisvalidandsupported. *Theworstthatcanhappeninthefollowingcodeisamemory *allocationerror.
*/ return;
}
/* Set the impl pointer here so that it is set for both extension-only and base tables. */ if(mbcsTable->utf8Friendly) { if(mbcsTable->countStates==1) {
sharedData->impl=&_SBCSUTF8Impl;
} else { if(mbcsTable->outputType==MBCS_OUTPUT_2) {
sharedData->impl=&_DBCSUTF8Impl;
}
}
}
/* the option does not apply, remove it */
cnv->options=pArgs->options&=~UCNV_OPTION_SWAP_LFNL;
}
}
}
if(uprv_strstr(pArgs->name, "18030")!=nullptr) { if(uprv_strstr(pArgs->name, "gb18030")!=nullptr || uprv_strstr(pArgs->name, "GB18030")!=nullptr) { /* set a flag for GB 18030 mode, which changes the callback behavior */
cnv->options|=_MBCS_OPTION_GB18030;
}
} elseif((uprv_strstr(pArgs->name, "KEIS")!=nullptr) || (uprv_strstr(pArgs->name, "keis")!=nullptr)) { /* set a flag for KEIS converter, which changes the SI/SO character sequence */
cnv->options|=_MBCS_OPTION_KEIS;
} elseif((uprv_strstr(pArgs->name, "JEF")!=nullptr) || (uprv_strstr(pArgs->name, "jef")!=nullptr)) { /* set a flag for JEF converter, which changes the SI/SO character sequence */
cnv->options|=_MBCS_OPTION_JEF;
} elseif((uprv_strstr(pArgs->name, "JIPS")!=nullptr) || (uprv_strstr(pArgs->name, "jips")!=nullptr)) { /* set a flag for JIPS converter, which changes the SI/SO character sequence */
cnv->options|=_MBCS_OPTION_JIPS;
}
/* fix maxBytesPerUChar depending on outputType and options etc. */ if(outputType==MBCS_OUTPUT_2_SISO) {
cnv->maxBytesPerUChar=3; /* SO+DBCS */
}
limit=mbcsTable->countToUFallbacks; if(limit>0) { /* do a binary search for the fallback mapping */
toUFallbacks=mbcsTable->toUFallbacks;
start=0; while(start<limit-1) {
i=(start+limit)/2; if(offset<toUFallbacks[i].offset) {
limit=i;
} else {
start=i;
}
}
/* did we really find it? */ if(offset==toUFallbacks[start].offset) { return toUFallbacks[start].codePoint;
}
}
return0xfffe;
}
/* This version of ucnv_MBCSToUnicodeWithOffsets() is optimized for single-byte, single-state codepages. */ staticvoid
ucnv_MBCSSingleToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UConverter *cnv; const uint8_t *source, *sourceLimit;
char16_t *target; const char16_t *targetLimit;
int32_t *offsets;
const int32_t (*stateTable)[256];
int32_t sourceIndex;
int32_t entry;
char16_t c;
uint8_t action;
/* set up the local pointers */
cnv=pArgs->converter;
source = reinterpret_cast<const uint8_t*>(pArgs->source);
sourceLimit = reinterpret_cast<const uint8_t*>(pArgs->sourceLimit);
target=pArgs->target;
targetLimit=pArgs->targetLimit;
offsets=pArgs->offsets;
/* test the most common case first */ if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) { /* output BMP code point */
*target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry)); if(offsets!=nullptr) {
*offsets++=sourceIndex;
}
/* normal end of action codes: prepare for a new character */
++sourceIndex; continue;
}
#if MBCS_UNROLL_SINGLE_TO_BMP /* unrolling makes it faster on Pentium III/Windows 2000 */ /* unroll the loop with the most common case */
unrolled: if(targetCapacity>=16) {
int32_t count, loops, oredEntries;
/* were all 16 entries really valid? */ if(!MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(oredEntries)) { /* no, return to the first of these 16 */
source-=16;
target-=16; break;
}
} while(--count>0);
count=loops-count;
targetCapacity-=16*count;
/* test the most common case first */ if(MBCS_ENTRY_FINAL_IS_VALID_DIRECT_16(entry)) { /* output BMP code point */
*target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
--targetCapacity; continue;
}
/* *Anif-else-ifchainprovidesmorereliableperformancefor *themostcommoncasescomparedtoaswitch.
*/
action = static_cast<uint8_t>(MBCS_ENTRY_FINAL_ACTION(entry)); if(action==MBCS_STATE_FALLBACK_DIRECT_16) { if(UCNV_TO_U_USE_FALLBACK(cnv)) { /* output BMP code point */
*target++ = static_cast<char16_t>(MBCS_ENTRY_FINAL_VALUE_16(entry));
--targetCapacity; continue;
}
} elseif(action==MBCS_STATE_UNASSIGNED) { /* just fall through */
} elseif(action==MBCS_STATE_ILLEGAL) { /* callback(illegal) */
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
} else { /* reserved, must never occur */ continue;
}
/* set offsets since the start or the last extension */ if(offsets!=nullptr) {
int32_t count = static_cast<int32_t>(source - lastSource);
/* predecrement: do not set the offset for the callback-causing character */ while(--count>0) {
*offsets++=sourceIndex++;
} /* offset and sourceIndex are now set for the current character */
}
if(U_FAILURE(*pErrorCode)) { /* not mappable or buffer overflow */ break;
}
/* recalculate the targetCapacity after an extension mapping */
targetCapacity = static_cast<int32_t>(pArgs->targetLimit - target);
length = static_cast<int32_t>(sourceLimit - source); if(length<targetCapacity) {
targetCapacity=length;
}
}
#if MBCS_UNROLL_SINGLE_TO_BMP /* unrolling makes it faster on Pentium III/Windows 2000 */ goto unrolled; #endif
}
if(U_SUCCESS(*pErrorCode) && source<sourceLimit && target>=pArgs->targetLimit) { /* target is full */
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
}
/* set offsets since the start or the last callback */ if(offsets!=nullptr) {
size_t count=source-lastSource; while(count>0) {
*offsets++=sourceIndex++;
--count;
}
}
/* write back the updated pointers */
pArgs->source = reinterpret_cast<constchar*>(source);
pArgs->target=target;
pArgs->offsets=offsets;
}
static UBool
hasValidTrailBytes(const int32_t (*stateTable)[256], uint8_t state) { const int32_t *row=stateTable[state];
int32_t b, entry; /* First test for final entries in this state for some commonly valid byte values. */
entry=row[0xa1]; if( !MBCS_ENTRY_IS_TRANSITION(entry) &&
MBCS_ENTRY_FINAL_ACTION(entry)!=MBCS_STATE_ILLEGAL
) { return true;
}
entry=row[0x41]; if( !MBCS_ENTRY_IS_TRANSITION(entry) &&
MBCS_ENTRY_FINAL_ACTION(entry)!=MBCS_STATE_ILLEGAL
) { return true;
} /* Then test for final entries in this state. */ for(b=0; b<=0xff; ++b) {
entry=row[b]; if( !MBCS_ENTRY_IS_TRANSITION(entry) &&
MBCS_ENTRY_FINAL_ACTION(entry)!=MBCS_STATE_ILLEGAL
) { return true;
}
} /* Then recurse for transition entries. */ for(b=0; b<=0xff; ++b) {
entry=row[b]; if( MBCS_ENTRY_IS_TRANSITION(entry) &&
hasValidTrailBytes(stateTable, static_cast<uint8_t>(MBCS_ENTRY_TRANSITION_STATE(entry)))
) { return true;
}
} returnfalse;
}
/* set up the local pointers */
source=(const uint8_t *)pArgs->source;
sourceLimit=(const uint8_t *)pArgs->sourceLimit;
target=pArgs->target;
targetLimit=pArgs->targetLimit;
offsets=pArgs->offsets;
/* set up the local pointers */
source = lastSource = reinterpret_cast<const uint8_t*>(pArgs->source);
sourceLimit = reinterpret_cast<const uint8_t*>(pArgs->sourceLimit);
if(length<=0) { /* no input at all: "illegal" */ return0xffff;
}
#if0 /* *Codedisabled2002dec09(ICU2.4)becauseitisnotcurrentlyusedinICU.markus *TODOInfuturereleases,verifythatthisfunctionisnevercalledforSBCS *conversions,i.e.,thatsharedData->mbcs.countStates==1isstilltrue. *Removalimprovescodecoverage.
*/ /* use optimized function if possible */ if(sharedData->mbcs.countStates==1) { if(length==1) { return ucnv_MBCSSingleSimpleGetNextUChar(sharedData, (uint8_t)*source, useFallback);
} else { return0xffff; /* illegal: more than a single byte for an SBCS converter */
}
} #endif
/* set up the local pointers */
stateTable=sharedData->mbcs.stateTable;
unicodeCodeUnits=sharedData->mbcs.unicodeCodeUnits;
/* converter state */
offset=0;
state=sharedData->mbcs.dbcsOnlyState;
/* use optimized function if possible */
cnv=pArgs->converter;
unicodeMask=cnv->sharedData->mbcs.unicodeMask;
/* set up the local pointers */
source=pArgs->source;
sourceLimit=pArgs->sourceLimit;
target = reinterpret_cast<uint8_t*>(pArgs->target);
targetCapacity = static_cast<int32_t>(pArgs->targetLimit - pArgs->target);
offsets=pArgs->offsets;
while(source<sourceLimit) { /* *Thisfollowingtestistoseeifavailableinputwouldoverflowtheoutput. *Itdoesnotcatchoutputofmorethanonebytethat *overflowsasaresultofamulti-bytecharacterorcallbackoutput *fromthelastsourcecharacter. *Therefore,thosesituationsalsotestforoverflowsandwill *thenbreaktheloop,too.
*/ if(targetCapacity>0) { /* *GetacorrectUnicodecodepoint: *asinglechar16_tforaBMPcodepointor *amatchedsurrogatepairfora"supplementarycodepoint".
*/
c=*source++;
++nextSourceIndex; if(c<=0x7f && IS_ASCII_ROUNDTRIP(c, asciiRoundtrips)) {
*target++ = static_cast<uint8_t>(c); if(offsets!=nullptr) {
*offsets++=sourceIndex;
sourceIndex=nextSourceIndex;
}
--targetCapacity;
c=0; continue;
} /* *utf8Friendlytable:Testfor<=0xd7ffratherthan<=MBCS_FAST_MAX *toavoiddealingwithsurrogates. *MBCS_FAST_MAXmustbe>=0xd7ff.
*/ if(c<=0xd7ff) {
value=DBCS_RESULT_FROM_MOST_BMP(mbcsIndex, (const uint16_t *)bytes, c); /* There are only roundtrips (!=0) and no-mapping (==0) entries. */ if(value==0) { goto unassigned;
} /* output the value */
} else { /* *Thisalsotestsifthecodepagemapssinglesurrogates. *Ifitdoes,thensurrogatesarenotpairedbutmappedseparately. *Notethatinthiscaseunmatchedsurrogatesarenotdetected.
*/ if(U16_IS_SURROGATE(c) && !(unicodeMask&UCNV_HAS_SURROGATES)) { if(U16_IS_SURROGATE_LEAD(c)) {
getTrail: if(source<sourceLimit) { /* test the following code unit */
char16_t trail=*source; if(U16_IS_TRAIL(trail)) {
++source;
++nextSourceIndex;
c=U16_GET_SUPPLEMENTARY(c, trail); if(!(unicodeMask&UCNV_HAS_SUPPLEMENTARY)) { /* BMP-only codepages are stored without stage 1 entries for supplementary code points */ /* callback(unassigned) */ goto unassigned;
} /* convert this supplementary code point */ /* exit this condition tree */
} else { /* this is an unmatched lead code unit (1st surrogate) */ /* callback(illegal) */
*pErrorCode=U_ILLEGAL_CHAR_FOUND; break;
}
} else { /* no more input */ break;
}
} else { /* this is an unmatched trail code unit (2nd surrogate) */ /* callback(illegal) */
*pErrorCode=U_ILLEGAL_CHAR_FOUND; break;
}
}
/* convert the Unicode code point in c into codepage bytes */
stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
/* get the bytes and the length for the output */ /* MBCS_OUTPUT_2 */
value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c);
/* is this code point assigned, or do we use fallbacks? */ if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c) ||
(UCNV_FROM_U_USE_FALLBACK(cnv, c) && value!=0))
) { /* *Weallowa0byteoutputifthe"assigned"bitissetforthisentry. *Thereisnowaywiththisdatastructureforfallbackoutput *tobeazerobyte.
*/
if(U_FAILURE(*pErrorCode)) { /* not mappable or buffer overflow */ break;
} else { /* a mapping was written to the target, continue */
/* recalculate the targetCapacity after an extension mapping */
targetCapacity = static_cast<int32_t>(pArgs->targetLimit - reinterpret_cast<char*>(target));
/* normal end of conversion: prepare for a new character */
sourceIndex=nextSourceIndex; continue;
}
}
}
/* write the output character bytes from value and length */ /* from the first if in the loop we know that targetCapacity>0 */ if(value<=0xff) { /* this is easy because we know that there is enough space */
*target++ = static_cast<uint8_t>(value); if(offsets!=nullptr) {
*offsets++=sourceIndex;
}
--targetCapacity;
} else/* length==2 */ {
*target++ = static_cast<uint8_t>(value >> 8); if(2<=targetCapacity) {
*target++ = static_cast<uint8_t>(value); if(offsets!=nullptr) {
*offsets++=sourceIndex;
*offsets++=sourceIndex;
}
targetCapacity-=2;
} else { if(offsets!=nullptr) {
*offsets++=sourceIndex;
}
cnv->charErrorBuffer[0] = static_cast<char>(value);
cnv->charErrorBufferLength=1;
/* normal end of conversion: prepare for a new character */
c=0;
sourceIndex=nextSourceIndex; continue;
} else { /* target is full */
*pErrorCode=U_BUFFER_OVERFLOW_ERROR; break;
}
}
/* set the converter state back into UConverter */
cnv->fromUChar32=c;
/* write back the updated pointers */
pArgs->source=source;
pArgs->target = reinterpret_cast<char*>(target);
pArgs->offsets=offsets;
}
/* This version of ucnv_MBCSFromUnicodeWithOffsets() is optimized for single-byte codepages. */ staticvoid
ucnv_MBCSSingleFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UConverter *cnv; const char16_t *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;
uint16_t value, minValue;
UBool hasSupplementary;
/* set up the local pointers */
cnv=pArgs->converter;
source=pArgs->source;
sourceLimit=pArgs->sourceLimit;
target = reinterpret_cast<uint8_t*>(pArgs->target);
targetCapacity = static_cast<int32_t>(pArgs->targetLimit - pArgs->target);
offsets=pArgs->offsets;
if(cnv->useFallback) { /* use all roundtrip and fallback results */
minValue=0x800;
} else { /* use only roundtrips and fallbacks from private-use characters */
minValue=0xc00;
}
hasSupplementary = static_cast<UBool>(cnv->sharedData->mbcs.unicodeMask & UCNV_HAS_SUPPLEMENTARY);
/* get the converter state from UConverter */
c=cnv->fromUChar32;
/* sourceIndex=-1 if the current character began in the previous buffer */
sourceIndex= c==0 ? 0 : -1;
nextSourceIndex=0;
while(source<sourceLimit) { /* *Thisfollowingtestistoseeifavailableinputwouldoverflowtheoutput. *Itdoesnotcatchoutputofmorethanonebytethat *overflowsasaresultofamulti-bytecharacterorcallbackoutput *fromthelastsourcecharacter. *Therefore,thosesituationsalsotestforoverflowsandwill *thenbreaktheloop,too.
*/ if(targetCapacity>0) { /* *GetacorrectUnicodecodepoint: *asinglechar16_tforaBMPcodepointor *amatchedsurrogatepairfora"supplementarycodepoint".
*/
c=*source++;
++nextSourceIndex; if(U16_IS_SURROGATE(c)) { if(U16_IS_SURROGATE_LEAD(c)) {
getTrail: if(source<sourceLimit) { /* test the following code unit */
char16_t trail=*source; if(U16_IS_TRAIL(trail)) {
++source;
++nextSourceIndex;
c=U16_GET_SUPPLEMENTARY(c, trail); if(!hasSupplementary) { /* BMP-only codepages are stored without stage 1 entries for supplementary code points */ /* callback(unassigned) */ goto unassigned;
} /* convert this supplementary code point */ /* exit this condition tree */
} else { /* this is an unmatched lead code unit (1st surrogate) */ /* callback(illegal) */
*pErrorCode=U_ILLEGAL_CHAR_FOUND; break;
}
} else { /* no more input */ break;
}
} else { /* this is an unmatched trail code unit (2nd surrogate) */ /* callback(illegal) */
*pErrorCode=U_ILLEGAL_CHAR_FOUND; break;
}
}
/* convert the Unicode code point in c into codepage bytes */
value=MBCS_SINGLE_RESULT_FROM_U(table, results, c);
/* is this code point assigned, or do we use fallbacks? */ if(value>=minValue) { /* assigned, write the output character bytes from value and length */ /* length==1 */ /* this is easy because we know that there is enough space */
*target++ = static_cast<uint8_t>(value); if(offsets!=nullptr) {
*offsets++=sourceIndex;
}
--targetCapacity;
/* normal end of conversion: prepare for a new character */
c=0;
sourceIndex=nextSourceIndex;
} else { /* unassigned */
unassigned: /* try an extension mapping */
pArgs->source=source;
c=_extFromU(cnv, cnv->sharedData,
c, &source, sourceLimit,
&target, target+targetCapacity,
&offsets, sourceIndex,
pArgs->flush,
pErrorCode);
nextSourceIndex += static_cast<int32_t>(source - pArgs->source);
if(U_FAILURE(*pErrorCode)) { /* not mappable or buffer overflow */ break;
} else { /* a mapping was written to the target, continue */
/* recalculate the targetCapacity after an extension mapping */
targetCapacity = static_cast<int32_t>(pArgs->targetLimit - reinterpret_cast<char*>(target));
/* normal end of conversion: prepare for a new character */
sourceIndex=nextSourceIndex;
}
}
} else { /* target is full */
*pErrorCode=U_BUFFER_OVERFLOW_ERROR; break;
}
}
/* set the converter state back into UConverter */
cnv->fromUChar32=c;
/* write back the updated pointers */
pArgs->source=source;
pArgs->target = reinterpret_cast<char*>(target);
pArgs->offsets=offsets;
}
/* set up the local pointers */
cnv=pArgs->converter;
source=pArgs->source;
sourceLimit=pArgs->sourceLimit;
target = reinterpret_cast<uint8_t*>(pArgs->target);
targetCapacity = static_cast<int32_t>(pArgs->targetLimit - pArgs->target);
offsets=pArgs->offsets;
if(cnv->useFallback) { /* use all roundtrip and fallback results */
minValue=0x800;
} else { /* use only roundtrips and fallbacks from private-use characters */
minValue=0xc00;
}
/* get the converter state from UConverter */
c=cnv->fromUChar32;
/* sourceIndex=-1 if the current character began in the previous buffer */
sourceIndex= c==0 ? 0 : -1;
lastSource=source;
#if MBCS_UNROLL_SINGLE_FROM_BMP /* unrolling makes it slower on Pentium III/Windows 2000?! */ /* unroll the loop with the most common case */
unrolled: if(targetCapacity>=4) {
int32_t count, loops;
uint16_t andedValues;
/* were all 4 entries really valid? */ if(andedValues<minValue) { /* no, return to the first of these 4 */
source-=4;
target-=4; break;
}
} while(--count>0);
count=loops-count;
targetCapacity-=4*count;
while(targetCapacity>0) { /* *GetacorrectUnicodecodepoint: *asinglechar16_tforaBMPcodepointor *amatchedsurrogatepairfora"supplementarycodepoint".
*/
c=*source++; /* *Donotimmediatelycheckforsinglesurrogates: *Assumethattheyareunassignedandcheckfortheminthatcase. *Thisspeedsuptheconversionofassignedcharacters.
*/ /* convert the Unicode code point in c into codepage bytes */ if(c<=0x7f && IS_ASCII_ROUNDTRIP(c, asciiRoundtrips)) {
*target++ = static_cast<uint8_t>(c);
--targetCapacity;
c=0; continue;
}
value=MBCS_SINGLE_RESULT_FROM_U(table, results, c); /* is this code point assigned, or do we use fallbacks? */ if(value>=minValue) { /* assigned, write the output character bytes from value and length */ /* length==1 */ /* this is easy because we know that there is enough space */
*target++ = static_cast<uint8_t>(value);
--targetCapacity;
/* normal end of conversion: prepare for a new character */
c=0; continue;
} elseif(!U16_IS_SURROGATE(c)) { /* normal, unassigned BMP character */
} elseif(U16_IS_SURROGATE_LEAD(c)) {
getTrail: if(source<sourceLimit) { /* test the following code unit */
char16_t trail=*source; if(U16_IS_TRAIL(trail)) {
++source;
c=U16_GET_SUPPLEMENTARY(c, trail); /* this codepage does not map supplementary code points */ /* callback(unassigned) */
} else { /* this is an unmatched lead code unit (1st surrogate) */ /* callback(illegal) */
*pErrorCode=U_ILLEGAL_CHAR_FOUND; break;
}
} else { /* no more input */ if (pArgs->flush) {
*pErrorCode=U_TRUNCATED_CHAR_FOUND;
} break;
}
} else { /* this is an unmatched trail code unit (2nd surrogate) */ /* callback(illegal) */
*pErrorCode=U_ILLEGAL_CHAR_FOUND; break;
}
/* c does not have a mapping */
/* get the number of code units for c to correctly advance sourceIndex */
length=U16_LENGTH(c);
/* set offsets since the start or the last extension */ if(offsets!=nullptr) {
int32_t count = static_cast<int32_t>(source - lastSource);
/* do not set the offset for this character */
count-=length;
while(count>0) {
*offsets++=sourceIndex++;
--count;
} /* offsets and sourceIndex are now set for the current character */
}
/* use optimized function if possible */
outputType=cnv->sharedData->mbcs.outputType;
unicodeMask=cnv->sharedData->mbcs.unicodeMask; if(outputType==MBCS_OUTPUT_1 && !(unicodeMask&UCNV_HAS_SURROGATES)) { if(!(unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
ucnv_MBCSSingleFromBMPWithOffsets(pArgs, pErrorCode);
} else {
ucnv_MBCSSingleFromUnicodeWithOffsets(pArgs, pErrorCode);
} return;
} elseif(outputType==MBCS_OUTPUT_2 && cnv->sharedData->mbcs.utf8Friendly) {
ucnv_MBCSDoubleFromUnicodeWithOffsets(pArgs, pErrorCode); return;
}
/* set up the local pointers */
source=pArgs->source;
sourceLimit=pArgs->sourceLimit;
target=(uint8_t *)pArgs->target;
targetCapacity=(int32_t)(pArgs->targetLimit-pArgs->target);
offsets=pArgs->offsets;
/* get the converter state from UConverter */
c=cnv->fromUChar32;
if(outputType==MBCS_OUTPUT_2_SISO) {
prevLength=cnv->fromUnicodeStatus; if(prevLength==0) { /* set the real value */
prevLength=1;
}
} else { /* prevent fromUnicodeStatus from being set to something non-0 */
prevLength=0;
}
/* sourceIndex=-1 if the current character began in the previous buffer */
prevSourceIndex=-1;
sourceIndex= c==0 ? 0 : -1;
nextSourceIndex=0;
/* Get the SI/SO character for the converter */
siLength = static_cast<uint8_t>(getSISOBytes(SI, cnv->options, siBytes));
soLength = static_cast<uint8_t>(getSISOBytes(SO, cnv->options, soBytes));
/* get the bytes and the length for the output */ 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: /* 1/2-byte stateful with Shift-In/Shift-Out */ /* *Savetheoldstateintheconverterobject *righthere,thenchangethelocalprevLengthstatevariableifnecessary. *Then,ifthischaracterturnsouttobeunassignedorafallbackthat *isnottaken,thecallbackcodemustnotsavethenewstateintheconverter *becausethenewstateisforacharacterthatisnotoutput. *However,thecallbackmuststillrestorethestatefromtheconverter *incasethecallbackfunctionchangeditforitsoutput.
*/
cnv->fromUnicodeStatus=prevLength; /* save the old state */
value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c); if(value<=0xff) { if(value==0 && MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c)==0) { /* no mapping, leave value==0 */
length=0;
} elseif(prevLength<=1) {
length=1;
} else { /* change from double-byte mode to single-byte */ if (siLength == 1) {
value|=(uint32_t)siBytes[0]<<8;
length = 2;
} elseif (siLength == 2) {
value|=(uint32_t)siBytes[1]<<8;
value|=(uint32_t)siBytes[0]<<16;
length = 3;
}
prevLength=1;
}
} else { if(prevLength==2) {
length=2;
} else { /* change from single-byte mode to double-byte */ if (soLength == 1) {
value|=(uint32_t)soBytes[0]<<16;
length = 3;
} elseif (soLength == 2) {
value|=(uint32_t)soBytes[1]<<16;
value|=(uint32_t)soBytes[0]<<24;
length = 4;
}
prevLength=2;
}
} break; case MBCS_OUTPUT_DBCS_ONLY: /* table with single-byte results, but only DBCS mappings used */
value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c); if(value<=0xff) { /* no mapping or SBCS result, not taken for DBCS-only */
value=stage2Entry=0; /* stage2Entry=0 to reset roundtrip flags */
length=0;
} else {
length=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;
} elseif(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;
} elseif(value<=0xffff) {
length=2;
} elseif(value<=0xffffff) {
length=3;
} else {
length=4;
} break; case MBCS_OUTPUT_3_EUC:
value=MBCS_VALUE_2_FROM_STAGE_2(bytes, stage2Entry, c); /* EUC 16-bit fixed-length representation */ if(value<=0xff) {
length=1;
} elseif((value&0x8000)==0) {
value|=0x8e8000;
length=3;
} elseif((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]; /* EUC 16-bit fixed-length representation applied to the first two bytes */ if(value<=0xff) {
length=1;
} elseif(value<=0xffff) {
length=2;
} elseif((value&0x800000)==0) {
value|=0x8e800000;
length=4;
} elseif((value&0x8000)==0) {
value|=0x8f008000;
length=4;
} else {
length=3;
} break; default: /* must not occur */ /* *Toavoidcompilerwarningsthatvalue&lengthmaybe *usedwithouthavingbeeninitialized,wesetthemhere. *Inreality,thisisunreachablecode. *Nothavingadefaultbranchalsocauseswarningswith *somecompilers.
*/
value=stage2Entry=0; /* stage2Entry=0 to reset roundtrip flags */
length=0; break;
}
/* is this code point assigned, or do we use fallbacks? */ if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c)!=0 ||
(UCNV_FROM_U_USE_FALLBACK(cnv, c) && value!=0))
) { /* *Weallowa0byteoutputifthe"assigned"bitissetforthisentry. *Thereisnowaywiththisdatastructureforfallbackoutput *tobeazerobyte.
*/
if(U_FAILURE(*pErrorCode)) { /* not mappable or buffer overflow */ break;
} else { /* a mapping was written to the target, continue */
/* recalculate the targetCapacity after an extension mapping */
targetCapacity=(int32_t)(pArgs->targetLimit-(char *)target);
/* normal end of conversion: prepare for a new character */ if(offsets!=nullptr) {
prevSourceIndex=sourceIndex;
sourceIndex=nextSourceIndex;
} continue;
}
}
}
/* write the output character bytes from value and length */ /* from the first if in the loop we know that targetCapacity>0 */ if(length<=targetCapacity) { if(offsets==nullptr) { switch(length) { /* each branch falls through to the next one */ case4:
*target++=(uint8_t)(value>>24);
U_FALLTHROUGH; case3:
*target++=(uint8_t)(value>>16);
U_FALLTHROUGH; case2:
*target++=(uint8_t)(value>>8);
U_FALLTHROUGH; case1:
*target++=(uint8_t)value;
U_FALLTHROUGH; default: /* will never occur */ break;
}
} else { switch(length) { /* each branch falls through to the next one */ case4:
*target++=(uint8_t)(value>>24);
*offsets++=sourceIndex;
U_FALLTHROUGH; case3:
*target++=(uint8_t)(value>>16);
*offsets++=sourceIndex;
U_FALLTHROUGH; case2:
*target++=(uint8_t)(value>>8);
*offsets++=sourceIndex;
U_FALLTHROUGH; case1:
*target++=(uint8_t)value;
*offsets++=sourceIndex;
U_FALLTHROUGH; default: /* will never occur */ break;
}
}
targetCapacity-=length;
} else {
uint8_t *charErrorBuffer;
/* *Weactuallydothisbackwardshere: *Inordertosaveanintermediatevariable,weoutput *firsttotheoverflowbufferwhatdoesnotfitintothe *regulartarget.
*/ /* we know that 1<=targetCapacity<length<=4 */
length-=targetCapacity;
charErrorBuffer=(uint8_t *)cnv->charErrorBuffer; switch(length) { /* each branch falls through to the next one */ case3:
*charErrorBuffer++=(uint8_t)(value>>16);
U_FALLTHROUGH; case2:
*charErrorBuffer++=(uint8_t)(value>>8);
U_FALLTHROUGH; case1:
*charErrorBuffer=(uint8_t)value;
U_FALLTHROUGH; default: /* will never occur */ break;
}
cnv->charErrorBufferLength=(int8_t)length;
/* now output what fits into the regular target */
value>>=8*length; /* length was reduced by targetCapacity */ switch(targetCapacity) { /* each branch falls through to the next one */ case3:
*target++=(uint8_t)(value>>16); if(offsets!=nullptr) {
*offsets++=sourceIndex;
}
U_FALLTHROUGH; case2:
*target++=(uint8_t)(value>>8); if(offsets!=nullptr) {
*offsets++=sourceIndex;
}
U_FALLTHROUGH; case1:
*target++=(uint8_t)value; if(offsets!=nullptr) {
*offsets++=sourceIndex;
}
U_FALLTHROUGH; default: /* will never occur */ break;
}
/* normal end of conversion: prepare for a new character */
c=0; if(offsets!=nullptr) {
prevSourceIndex=sourceIndex;
sourceIndex=nextSourceIndex;
} continue;
} else { /* target is full */
*pErrorCode=U_BUFFER_OVERFLOW_ERROR; break;
}
}
/* *theendoftheinputstreamanddetectionoftruncatedinput *arehandledbytheframework,butforEBCDIC_STATEFULconversion *weneedtoemitanSIattheveryend * *conditions: *successful *EBCDIC_STATEFULinDBCSmode *endofinputandnotruncatedinput
*/ if( U_SUCCESS(*pErrorCode) &&
outputType==MBCS_OUTPUT_2_SISO && prevLength==2 &&
pArgs->flush && source>=sourceLimit && c==0
) { /* EBCDIC_STATEFUL ending with DBCS: emit an SI to return the output stream to SBCS */ if(targetCapacity>0) {
*target++ = siBytes[0]; if (siLength == 2) { if (targetCapacity<2) {
cnv->charErrorBuffer[0] = siBytes[1];
cnv->charErrorBufferLength=1;
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
} else {
*target++ = siBytes[1];
}
} if(offsets!=nullptr) { /* set the last source character's index (sourceIndex points at sourceLimit now) */
*offsets++=prevSourceIndex;
}
} else { /* target is full */
cnv->charErrorBuffer[0] = siBytes[0]; if (siLength == 2) {
cnv->charErrorBuffer[1] = siBytes[1];
}
cnv->charErrorBufferLength=siLength;
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
}
prevLength=1; /* we switched into SBCS */
}
/* set the converter state back into UConverter */
cnv->fromUChar32=c;
cnv->fromUnicodeStatus=prevLength;
/* write back the updated pointers */
pArgs->source=source;
pArgs->target=(char *)target;
pArgs->offsets=offsets;
}
/* *Thisisanothersimpleconversionfunctionforinternalusebyother *conversionimplementations. *Itdoesnotusetheconverterstatenorcallcallbacks. *ItdoesnothandletheEBCDICswaplfnloption(setinUConverter). *IthandlesconversionextensionsbutnotGB18030. * *ItconvertsonesingleUnicodecodepointintocodepagebytes,encoded *asone32-bitvalue.Thefunctionreturnsthenumberofbytesin*pValue: *1..4thenumberofbytesin*pValue *0unassigned(*pValueundefined) *-1illegal(currentlynotused,*pValueundefined) * **pValuewillcontaintheresultingbyteswiththelastbyteinbits7..0, *thesecondtolastbyteinbits15..8,etc. *Currently,thefunctionassumesbutdoesnotcheckthat0<=c<=0x10ffff.
*/
U_CFUNC int32_t
ucnv_MBCSFromUChar32(UConverterSharedData *sharedData,
UChar32 c, uint32_t *pValue,
UBool useFallback) { const int32_t *cx; const uint16_t *table; #if0 /* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */ const uint8_t *p; #endif
uint32_t stage2Entry;
uint32_t value;
int32_t length;
/* BMP-only codepages are stored without stage 1 entries for supplementary code points */ if(c<=0xffff || (sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY)) {
table=sharedData->mbcs.fromUnicodeTable;
/* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */ if(sharedData->mbcs.outputType==MBCS_OUTPUT_1) {
value=MBCS_SINGLE_RESULT_FROM_U(table, (uint16_t *)sharedData->mbcs.fromUnicodeBytes, c); /* is this code point assigned, or do we use fallbacks? */ if(useFallback ? value>=0x800 : value>=0xc00) {
*pValue=value&0xff; return1;
}
} else/* outputType!=MBCS_OUTPUT_1 */ {
stage2Entry=MBCS_STAGE_2_FROM_U(table, c);
/* get the bytes and the length for the output */ switch(sharedData->mbcs.outputType) { case MBCS_OUTPUT_2:
value=MBCS_VALUE_2_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c); if(value<=0xff) {
length=1;
} else {
length=2;
} break; #if0 /* #if 0 because this is not currently used in ICU - reduce code, increase code coverage */ case MBCS_OUTPUT_DBCS_ONLY: /* table with single-byte results, but only DBCS mappings used */
value=MBCS_VALUE_2_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c); if(value<=0xff) { /* no mapping or SBCS result, not taken for DBCS-only */
value=stage2Entry=0; /* stage2Entry=0 to reset roundtrip flags */
length=0;
} else {
length=2;
} break; case MBCS_OUTPUT_3:
p=MBCS_POINTER_3_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2]; if(value<=0xff) {
length=1;
} elseif(value<=0xffff) {
length=2;
} else {
length=3;
} break; case MBCS_OUTPUT_4:
value=MBCS_VALUE_4_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c); if(value<=0xff) {
length=1;
} elseif(value<=0xffff) {
length=2;
} elseif(value<=0xffffff) {
length=3;
} else {
length=4;
} break; case MBCS_OUTPUT_3_EUC:
value=MBCS_VALUE_2_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c); /* EUC 16-bit fixed-length representation */ if(value<=0xff) {
length=1;
} elseif((value&0x8000)==0) {
value|=0x8e8000;
length=3;
} elseif((value&0x80)==0) {
value|=0x8f0080;
length=3;
} else {
length=2;
} break; case MBCS_OUTPUT_4_EUC:
p=MBCS_POINTER_3_FROM_STAGE_2(sharedData->mbcs.fromUnicodeBytes, stage2Entry, c);
value=((uint32_t)*p<<16)|((uint32_t)p[1]<<8)|p[2]; /* EUC 16-bit fixed-length representation applied to the first two bytes */ if(value<=0xff) {
length=1;
} elseif(value<=0xffff) {
length=2;
} elseif((value&0x800000)==0) {
value|=0x8e800000;
length=4;
} elseif((value&0x8000)==0) {
value|=0x8f008000;
length=4;
} else {
length=3;
} break; #endif default: /* must not occur */ return -1;
}
/* is this code point assigned, or do we use fallbacks? */ if( MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c) ||
(FROM_U_USE_FALLBACK(useFallback, c) && value!=0)
) { /* *Weallowa0byteoutputifthe"assigned"bitissetforthisentry. *Thereisnowaywiththisdatastructureforfallbackoutput *tobeazerobyte.
*/ /* assigned */
*pValue=value; return length;
}
}
}
/* BMP-only codepages are stored without stage 1 entries for supplementary code points */ if(c>=0x10000 && !(sharedData->mbcs.unicodeMask&UCNV_HAS_SUPPLEMENTARY)) { return -1;
}
/* convert the Unicode code point in c into codepage bytes (same as in _MBCSFromUnicodeWithOffsets) */
table=sharedData->mbcs.fromUnicodeTable;
/* get the byte for the output */
value=MBCS_SINGLE_RESULT_FROM_U(table, (uint16_t *)sharedData->mbcs.fromUnicodeBytes, c); /* is this code point assigned, or do we use fallbacks? */ if(useFallback ? value>=0x800 : value>=0xc00) { return value&0xff;
} else { return -1;
}
} #endif
if(cnv->useFallback) { /* use all roundtrip and fallback results */
minValue=0x800;
} else { /* use only roundtrips and fallbacks from private-use characters */
minValue=0xc00;
}
hasSupplementary = static_cast<UBool>(cnv->sharedData->mbcs.unicodeMask & UCNV_HAS_SUPPLEMENTARY);
/* get the converter state from the UTF-8 UConverter */ if(utf8->toULength > 0) {
toULength=oldToULength=utf8->toULength;
toULimit = static_cast<int8_t>(utf8->mode);
c = static_cast<UChar32>(utf8->toUnicodeStatus);
} else {
toULength=oldToULength=toULimit=0;
c = 0;
}
// The conversion loop checks source<sourceLimit only once per 1/2/3-byte character. // If the buffer ends with a truncated 2- or 3-byte sequence, // then we reduce the sourceLimit to before that, // and collect the remaining bytes after the conversion loop.
{ // Do not go back into the bytes that will be read for finishing a partial // sequence from the previous buffer.
int32_t length = static_cast<int32_t>(sourceLimit - source) - (toULimit - oldToULength); if(length>0) {
uint8_t b1=*(sourceLimit-1); if(U8_IS_SINGLE(b1)) { // common ASCII character
} elseif(U8_IS_TRAIL(b1) && length>=2) {
uint8_t b2=*(sourceLimit-2); if(0xe0<=b2 && b2<0xf0 && U8_IS_VALID_LEAD3_AND_T1(b2, b1)) { // truncated 3-byte sequence
sourceLimit-=2;
}
} elseif(0xc2<=b1 && b1<0xf0) { // truncated 2- or 3-byte sequence
--sourceLimit;
}
}
}
if(value>=minValue) { /* output the mapping for c */
*target++ = static_cast<uint8_t>(value);
--targetCapacity;
} else { /* value<minValue means c is unassigned (unmappable) */ /* *Tryanextensionmapping. *Passinnosourcebecausewedon'thaveUTF-16input. *Ifwehaveapartialmatchonc,wewillreturnandrevert *toUTF-8->UTF-16->charsetconversion.
*/ staticconst char16_t nul=0; const char16_t *noSource=&nul;
c=_extFromU(cnv, cnv->sharedData,
c, &noSource, noSource,
&target, target+targetCapacity,
nullptr, -1,
pFromUArgs->flush,
pErrorCode);
if(U_FAILURE(*pErrorCode)) { /* not mappable or buffer overflow */
cnv->fromUChar32=c; break;
} elseif(cnv->preFromUFirstCP>=0) { /* *Partialmatch,returnandreverttopivoting. *Innormalfrom-UTF-16conversion,wewouldjustcontinue *butthenexittheloopbecausetheextensionmatchwould *haveconsumedthesource.
*/
*pErrorCode=U_USING_DEFAULT_WARNING; break;
} else { /* a mapping was written to the target, continue */
/* recalculate the targetCapacity after an extension mapping */
targetCapacity = static_cast<int32_t>(pFromUArgs->targetLimit - reinterpret_cast<char*>(target));
}
}
} else { /* target is full */
*pErrorCode=U_BUFFER_OVERFLOW_ERROR; break;
}
}
/* get the converter state from the UTF-8 UConverter */ if(utf8->toULength > 0) {
toULength=oldToULength=utf8->toULength;
toULimit = static_cast<int8_t>(utf8->mode);
c = static_cast<UChar32>(utf8->toUnicodeStatus);
} else {
toULength=oldToULength=toULimit=0;
c = 0;
}
// The conversion loop checks source<sourceLimit only once per 1/2/3-byte character. // If the buffer ends with a truncated 2- or 3-byte sequence, // then we reduce the sourceLimit to before that, // and collect the remaining bytes after the conversion loop.
{ // Do not go back into the bytes that will be read for finishing a partial // sequence from the previous buffer.
int32_t length = static_cast<int32_t>(sourceLimit - source) - (toULimit - oldToULength); if(length>0) {
uint8_t b1=*(sourceLimit-1); if(U8_IS_SINGLE(b1)) { // common ASCII character
} elseif(U8_IS_TRAIL(b1) && length>=2) {
uint8_t b2=*(sourceLimit-2); if(0xe0<=b2 && b2<0xf0 && U8_IS_VALID_LEAD3_AND_T1(b2, b1)) { // truncated 3-byte sequence
sourceLimit-=2;
}
} elseif(0xc2<=b1 && b1<0xf0) { // truncated 2- or 3-byte sequence
--sourceLimit;
}
}
}
if(c!=0 && targetCapacity>0) {
utf8->toUnicodeStatus=0;
utf8->toULength=0; goto moreBytes; /* See note in ucnv_SBCSFromUTF8() about this goto. */
}
/* get the bytes and the length for the output */ /* MBCS_OUTPUT_2 */
value=MBCS_VALUE_2_FROM_STAGE_2(results, stage2Entry, c);
/* is this code point assigned, or do we use fallbacks? */ if(!(MBCS_FROM_U_IS_ROUNDTRIP(stage2Entry, c) ||
(UCNV_FROM_U_USE_FALLBACK(cnv, c) && value!=0))
) { goto unassigned;
}
}
}
/* write the output character bytes from value and length */ /* from the first if in the loop we know that targetCapacity>0 */ if(value<=0xff) { /* this is easy because we know that there is enough space */
*target++ = static_cast<uint8_t>(value);
--targetCapacity;
} else/* length==2 */ {
*target++ = static_cast<uint8_t>(value >> 8); if(2<=targetCapacity) {
*target++ = static_cast<uint8_t>(value);
targetCapacity-=2;
} else {
cnv->charErrorBuffer[0] = static_cast<char>(value);
cnv->charErrorBufferLength=1;
if(U_FAILURE(*pErrorCode)) { /* not mappable or buffer overflow */
cnv->fromUChar32=c; break;
} elseif(cnv->preFromUFirstCP>=0) { /* *Partialmatch,returnandreverttopivoting. *Innormalfrom-UTF-16conversion,wewouldjustcontinue *butthenexittheloopbecausetheextensionmatchwould *haveconsumedthesource.
*/
*pErrorCode=U_USING_DEFAULT_WARNING; break;
} else { /* a mapping was written to the target, continue */
/* recalculate the targetCapacity after an extension mapping */
targetCapacity = static_cast<int32_t>(pFromUArgs->targetLimit - reinterpret_cast<char*>(target)); continue;
}
}
} else { /* target is full */
*pErrorCode=U_BUFFER_OVERFLOW_ERROR; break;
}
}
state0=cnv->sharedData->mbcs.stateTable[cnv->sharedData->mbcs.dbcsOnlyState]; for(i=0; i<256; ++i) { /* all bytes that cause a state transition from state 0 are lead bytes */
starters[i] = static_cast<UBool>(MBCS_ENTRY_IS_TRANSITION(state0[i]));
}
}
/* first, select between subChar and subChar1 */ if( cnv->subChar1!=0 &&
(cnv->sharedData->mbcs.extIndexes!=nullptr ?
cnv->useSubChar1 :
(cnv->invalidUCharBuffer[0]<=0xff))
) { /* select subChar1 if it is set (not 0) and the unmappable Unicode code point is up to U+00ff (IBM MBCS behavior) */
subchar = reinterpret_cast<char*>(&cnv->subChar1);
length=1;
} else { /* select subChar in all other cases */
subchar = reinterpret_cast<char*>(cnv->subChars);
length=cnv->subCharLen;
}
/* reset the selector for the next code point */
cnv->useSubChar1=false;
if (cnv->sharedData->mbcs.outputType == MBCS_OUTPUT_2_SISO) {
p=buffer;
/* fromUnicodeStatus contains prevLength */ switch(length) { case1: if(cnv->fromUnicodeStatus==2) { /* DBCS mode and SBCS sub char: change to SBCS */
cnv->fromUnicodeStatus=1;
*p++=UCNV_SI;
}
*p++=subchar[0]; break; case2: if(cnv->fromUnicodeStatus<=1) { /* SBCS mode and DBCS sub char: change to DBCS */
cnv->fromUnicodeStatus=2;
*p++=UCNV_SO;
}
*p++=subchar[0];
*p++=subchar[1]; break; default:
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; return;
}
subchar=buffer;
length = static_cast<int32_t>(p - buffer);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.