#include "unicode/utypes.h"
#include "unicode/ucnv.h"
#include "unicode/ucnv_err.h"
#include "ucnv_bld.h"
#include "ucnv_cnv.h"
#include "cmemory.h"
static void
_UTF16PEToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UConverter *cnv = pArgs->converter;
const uint8_t *source = (const uint8_t *)pArgs->source;
UChar *target = pArgs->target;
int32_t *offsets = pArgs->offsets;
int32_t targetCapacity = pArgs->targetLimit - pArgs->target;
int32_t length = (const uint8_t *)pArgs->sourceLimit - source;
int32_t count;
int32_t sourceIndex = 0;
if(length <= 0 && cnv->toUnicodeStatus == 0) {
return;
}
if(targetCapacity <= 0) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
return;
}
if(length != 0 && cnv->toUnicodeStatus != 0) {
uint8_t *p = (uint8_t *)target++;
*p++ = (uint8_t)cnv->toUnicodeStatus;
cnv->toUnicodeStatus = 0;
*p = *source++;
--length;
--targetCapacity;
if(offsets != NULL) {
*offsets++ = -1;
}
}
count = 2 * targetCapacity;
if(count > length) {
count = length & ~1;
}
if(count > 0) {
uprv_memcpy(target, source, count);
source += count;
length -= count;
count >>= 1;
target += count;
targetCapacity -= count;
if(offsets != NULL) {
while(count > 0) {
*offsets++ = sourceIndex;
sourceIndex += 2;
--count;
}
}
}
if(length >= 2) {
*pErrorCode = U_BUFFER_OVERFLOW_ERROR;
} else if(length == 1) {
if(pArgs->flush) {
*pErrorCode = U_TRUNCATED_CHAR_FOUND;
} else {
cnv->toUnicodeStatus = *source++ | 0x100;
}
} else if(cnv->toUnicodeStatus!=0 && pArgs->flush) {
*pErrorCode = U_TRUNCATED_CHAR_FOUND;
}
pArgs->source = (const char *)source;
pArgs->target = target;
pArgs->offsets = offsets;
}
static void
_UTF16PEFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UConverter *cnv = pArgs->converter;
const UChar *source = pArgs->source;
uint8_t *target = (uint8_t *)pArgs->target;
int32_t *offsets = pArgs->offsets;
int32_t targetCapacity = pArgs->targetLimit - pArgs->target;
int32_t length = pArgs->sourceLimit - source;
int32_t count;
int32_t sourceIndex = 0;
if(length <= 0 && cnv->fromUnicodeStatus == 0) {
return;
}
if(targetCapacity <= 0) {
*pErrorCode = U_BUFFER_OVERFLOW_ERROR;
return;
}
if(cnv->fromUnicodeStatus != 0) {
*target++ = (uint8_t)cnv->fromUnicodeStatus;
cnv->fromUnicodeStatus = 0;
--targetCapacity;
if(offsets != NULL) {
*offsets++ = -1;
}
}
count = 2 * length;
if(count > targetCapacity) {
count = targetCapacity & ~1;
}
if(count>0) {
uprv_memcpy(target, source, count);
target += count;
targetCapacity -= count;
count >>= 1;
source += count;
length -= count;
if(offsets != NULL) {
while(count > 0) {
*offsets++ = sourceIndex;
*offsets++ = sourceIndex++;
--count;
}
}
}
if(length > 0) {
*pErrorCode = U_BUFFER_OVERFLOW_ERROR;
if(targetCapacity > 0) {
const uint8_t *p = (const uint8_t *)source++;
*target++ = *p++;
cnv->fromUnicodeStatus = *p | 0x100;
if(offsets != NULL) {
*offsets++ = sourceIndex;
}
}
}
pArgs->source = source;
pArgs->target = (char *)target;
pArgs->offsets = offsets;
}
static void
_UTF16OEToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UConverter *cnv = pArgs->converter;
const uint8_t *source = (const uint8_t *)pArgs->source;
UChar *target = pArgs->target;
uint8_t *target8 = (uint8_t *)target;
int32_t *offsets = pArgs->offsets;
int32_t targetCapacity = pArgs->targetLimit - pArgs->target;
int32_t length = (const uint8_t *)pArgs->sourceLimit - source;
int32_t count;
int32_t sourceIndex = 0;
if(length <= 0 && cnv->toUnicodeStatus == 0) {
return;
}
if(targetCapacity <= 0) {
*pErrorCode = U_BUFFER_OVERFLOW_ERROR;
return;
}
if(length != 0 && cnv->toUnicodeStatus != 0) {
*target8++ = *source++;
*target8++ = (uint8_t)cnv->toUnicodeStatus;
cnv->toUnicodeStatus = 0;
++target;
--length;
--targetCapacity;
if(offsets != NULL) {
*offsets++ = -1;
}
}
count = 2 * targetCapacity;
if(count > length) {
count = length & ~1;
}
if(count>0) {
length -= count;
count >>= 1;
targetCapacity -= count;
if(offsets == NULL) {
while(count > 0) {
target8[1] = *source++;
target8[0] = *source++;
target8 += 2;
--count;
}
} else {
while(count>0) {
target8[1] = *source++;
target8[0] = *source++;
target8 += 2;
*offsets++ = sourceIndex;
sourceIndex += 2;
--count;
}
}
target=(UChar *)target8;
}
if(length >= 2) {
*pErrorCode = U_BUFFER_OVERFLOW_ERROR;
} else if(length == 1) {
if(pArgs->flush) {
*pErrorCode = U_TRUNCATED_CHAR_FOUND;
} else {
cnv->toUnicodeStatus = *source++ | 0x100;
}
} else if(cnv->toUnicodeStatus!=0 && pArgs->flush) {
*pErrorCode = U_TRUNCATED_CHAR_FOUND;
}
pArgs->source = (const char *)source;
pArgs->target = target;
pArgs->offsets = offsets;
}
static void
_UTF16OEFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UConverter *cnv = pArgs->converter;
const UChar *source = pArgs->source;
const uint8_t *source8 = (const uint8_t *)source;
uint8_t *target = (uint8_t *)pArgs->target;
int32_t *offsets = pArgs->offsets;
int32_t targetCapacity = pArgs->targetLimit - pArgs->target;
int32_t length = pArgs->sourceLimit - source;
int32_t count;
int32_t sourceIndex = 0;
if(length <= 0 && cnv->fromUnicodeStatus == 0) {
return;
}
if(targetCapacity <= 0) {
*pErrorCode = U_BUFFER_OVERFLOW_ERROR;
return;
}
if(cnv->fromUnicodeStatus != 0) {
*target++ = (uint8_t)cnv->fromUnicodeStatus;
cnv->fromUnicodeStatus = 0;
--targetCapacity;
if(offsets != NULL) {
*offsets++ = -1;
}
}
count = 2 * length;
if(count > targetCapacity) {
count = targetCapacity & ~1;
}
if(count > 0) {
targetCapacity -= count;
count >>= 1;
length -= count;
if(offsets == NULL) {
while(count > 0) {
target[1] = *source8++;
target[0] = *source8++;
target += 2;
--count;
}
} else {
while(count>0) {
target[1] = *source8++;
target[0] = *source8++;
target += 2;
*offsets++ = sourceIndex;
*offsets++ = sourceIndex++;
--count;
}
}
source=(const UChar *)source8;
}
if(length > 0) {
*pErrorCode = U_BUFFER_OVERFLOW_ERROR;
if(targetCapacity > 0) {
cnv->fromUnicodeStatus = *source8++ | 0x100;
*target++ = *source8;
++source;
if(offsets != NULL) {
*offsets++ = sourceIndex;
}
}
}
pArgs->source = source;
pArgs->target = (char *)target;
pArgs->offsets = offsets;
}
#if U_IS_BIG_ENDIAN
# define _UTF16BEToUnicodeWithOffsets _UTF16PEToUnicodeWithOffsets
# define _UTF16LEToUnicodeWithOffsets _UTF16OEToUnicodeWithOffsets
# define _UTF16BEFromUnicodeWithOffsets _UTF16PEFromUnicodeWithOffsets
# define _UTF16LEFromUnicodeWithOffsets _UTF16OEFromUnicodeWithOffsets
#else
# define _UTF16BEToUnicodeWithOffsets _UTF16OEToUnicodeWithOffsets
# define _UTF16LEToUnicodeWithOffsets _UTF16PEToUnicodeWithOffsets
# define _UTF16BEFromUnicodeWithOffsets _UTF16OEFromUnicodeWithOffsets
# define _UTF16LEFromUnicodeWithOffsets _UTF16PEFromUnicodeWithOffsets
#endif
static UChar32 T_UConverter_getNextUChar_UTF16_BE(UConverterToUnicodeArgs* args,
UErrorCode* err)
{
UChar32 myUChar;
uint16_t first;
if (args->source+2 > args->sourceLimit)
{
if (args->source >= args->sourceLimit)
{
*err = U_INDEX_OUTOFBOUNDS_ERROR;
}
else
{
*err = U_TRUNCATED_CHAR_FOUND;
}
return 0xffff;
}
first = (uint16_t)(((uint16_t)(*(args->source)) << 8) |((uint8_t)*((args->source)+1)));
myUChar = first;
args->source += 2;
if(UTF_IS_FIRST_SURROGATE(first)) {
uint16_t second;
if (args->source+2 > args->sourceLimit) {
*err = U_TRUNCATED_CHAR_FOUND;
return 0xffff;
}
second = (uint16_t)(((uint16_t)(*(args->source)) << 8) |((uint8_t)*(args->source+1)));
if(UTF_IS_SECOND_SURROGATE(second)) {
myUChar = UTF16_GET_PAIR_VALUE(first, second);
args->source += 2;
}
}
return myUChar;
}
static const UConverterImpl _UTF16BEImpl={
UCNV_UTF16_BigEndian,
NULL,
NULL,
NULL,
NULL,
NULL,
_UTF16BEToUnicodeWithOffsets,
_UTF16BEToUnicodeWithOffsets,
_UTF16BEFromUnicodeWithOffsets,
_UTF16BEFromUnicodeWithOffsets,
T_UConverter_getNextUChar_UTF16_BE,
NULL,
NULL,
NULL,
NULL,
ucnv_getCompleteUnicodeSet
};
static const UConverterStaticData _UTF16BEStaticData={
sizeof(UConverterStaticData),
"UTF-16BE",
1200, UCNV_IBM, UCNV_UTF16_BigEndian, 2, 2,
{ 0xff, 0xfd, 0, 0 },2,FALSE,FALSE,
0,
0,
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }
};
const UConverterSharedData _UTF16BEData={
sizeof(UConverterSharedData), ~((uint32_t) 0),
NULL, NULL, &_UTF16BEStaticData, FALSE, &_UTF16BEImpl,
0
};
static UChar32 T_UConverter_getNextUChar_UTF16_LE(UConverterToUnicodeArgs* args,
UErrorCode* err)
{
UChar32 myUChar;
uint16_t first;
if (args->source+2 > args->sourceLimit)
{
if (args->source >= args->sourceLimit)
{
*err = U_INDEX_OUTOFBOUNDS_ERROR;
}
else
{
*err = U_TRUNCATED_CHAR_FOUND;
}
return 0xffff;
}
first = (uint16_t)(((uint16_t)*((args->source)+1) << 8) | ((uint8_t)(*(args->source))));
myUChar=first;
args->source += 2;
if (UTF_IS_FIRST_SURROGATE(first))
{
uint16_t second;
if (args->source+2 > args->sourceLimit)
{
*err = U_TRUNCATED_CHAR_FOUND;
return 0xffff;
}
second = (uint16_t)(((uint16_t)*(args->source+1) << 8) |((uint8_t)(*(args->source))));
if(UTF_IS_SECOND_SURROGATE(second))
{
myUChar = UTF16_GET_PAIR_VALUE(first, second);
args->source += 2;
}
}
return myUChar;
}
static const UConverterImpl _UTF16LEImpl={
UCNV_UTF16_LittleEndian,
NULL,
NULL,
NULL,
NULL,
NULL,
_UTF16LEToUnicodeWithOffsets,
_UTF16LEToUnicodeWithOffsets,
_UTF16LEFromUnicodeWithOffsets,
_UTF16LEFromUnicodeWithOffsets,
T_UConverter_getNextUChar_UTF16_LE,
NULL,
NULL,
NULL,
NULL,
ucnv_getCompleteUnicodeSet
};
static const UConverterStaticData _UTF16LEStaticData={
sizeof(UConverterStaticData),
"UTF-16LE",
1202, UCNV_IBM, UCNV_UTF16_LittleEndian, 2, 2,
{ 0xfd, 0xff, 0, 0 },2,FALSE,FALSE,
0,
0,
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }
};
const UConverterSharedData _UTF16LEData={
sizeof(UConverterSharedData), ~((uint32_t) 0),
NULL, NULL, &_UTF16LEStaticData, FALSE, &_UTF16LEImpl,
0
};
static void
_UTF16Reset(UConverter *cnv, UConverterResetChoice choice) {
if(choice<=UCNV_RESET_TO_UNICODE) {
cnv->mode=0;
}
if(choice!=UCNV_RESET_TO_UNICODE) {
cnv->charErrorBufferLength=2;
#if U_IS_BIG_ENDIAN
cnv->charErrorBuffer[0]=0xfe;
cnv->charErrorBuffer[1]=0xff;
#else
cnv->charErrorBuffer[0]=0xff;
cnv->charErrorBuffer[1]=0xfe;
#endif
}
}
static void
_UTF16Open(UConverter *cnv,
const char *name,
const char *locale,
uint32_t options,
UErrorCode *pErrorCode) {
_UTF16Reset(cnv, UCNV_RESET_BOTH);
}
static const char utf16BOM[8]={ (char)0xfe, (char)0xff, 0, 0, (char)0xff, (char)0xfe, 0, 0 };
static void
_UTF16ToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
UConverter *cnv=pArgs->converter;
const char *source=pArgs->source;
const char *sourceLimit=pArgs->sourceLimit;
int32_t *offsets=pArgs->offsets;
int32_t state, offsetDelta;
char b;
state=cnv->mode;
offsetDelta=0;
while(source<sourceLimit && U_SUCCESS(*pErrorCode)) {
switch(state) {
case 0:
b=*source;
if(b==(char)0xfe) {
state=1;
} else if(b==(char)0xff) {
state=5;
} else {
state=8;
continue;
}
++source;
break;
case 1:
case 5:
if(*source==utf16BOM[state]) {
++source;
if(state==1) {
state=8;
offsetDelta=source-pArgs->source;
} else if(state==5) {
state=9;
offsetDelta=source-pArgs->source;
}
} else {
if(source!=pArgs->source) {
source=pArgs->source;
} else {
UBool oldFlush=pArgs->flush;
pArgs->source=utf16BOM+(state&4);
pArgs->sourceLimit=pArgs->source+1;
pArgs->flush=FALSE;
_UTF16BEToUnicodeWithOffsets(pArgs, pErrorCode);
pArgs->sourceLimit=sourceLimit;
pArgs->flush=oldFlush;
}
state=8;
continue;
}
break;
case 8:
pArgs->source=source;
_UTF16BEToUnicodeWithOffsets(pArgs, pErrorCode);
source=pArgs->source;
break;
case 9:
pArgs->source=source;
_UTF16LEToUnicodeWithOffsets(pArgs, pErrorCode);
source=pArgs->source;
break;
default:
break;
}
}
if(offsets!=NULL && offsetDelta!=0) {
int32_t *offsetsLimit=pArgs->offsets;
while(offsets<offsetsLimit) {
*offsets++ += offsetDelta;
}
}
pArgs->source=source;
if(source==sourceLimit && pArgs->flush) {
switch(state) {
case 0:
break;
case 8:
_UTF16BEToUnicodeWithOffsets(pArgs, pErrorCode);
break;
case 9:
_UTF16LEToUnicodeWithOffsets(pArgs, pErrorCode);
break;
default:
pArgs->source=utf16BOM+(state&4);
pArgs->sourceLimit=pArgs->source+(state&3);
_UTF16BEToUnicodeWithOffsets(pArgs, pErrorCode);
pArgs->source=source;
pArgs->sourceLimit=sourceLimit;
break;
}
cnv->mode=0;
} else {
cnv->mode=state;
}
}
static UChar32
_UTF16GetNextUChar(UConverterToUnicodeArgs *pArgs,
UErrorCode *pErrorCode) {
switch(pArgs->converter->mode) {
case 8:
return T_UConverter_getNextUChar_UTF16_BE(pArgs, pErrorCode);
case 9:
return T_UConverter_getNextUChar_UTF16_LE(pArgs, pErrorCode);
default:
return ucnv_getNextUCharFromToUImpl(pArgs, _UTF16ToUnicodeWithOffsets, TRUE, pErrorCode);
}
}
static const UConverterImpl _UTF16Impl = {
UCNV_UTF16,
NULL,
NULL,
_UTF16Open,
NULL,
_UTF16Reset,
_UTF16ToUnicodeWithOffsets,
_UTF16ToUnicodeWithOffsets,
_UTF16PEFromUnicodeWithOffsets,
_UTF16PEFromUnicodeWithOffsets,
_UTF16GetNextUChar,
NULL,
NULL,
NULL,
NULL,
ucnv_getCompleteUnicodeSet
};
static const UConverterStaticData _UTF16StaticData = {
sizeof(UConverterStaticData),
"UTF-16",
0,
UCNV_IBM, UCNV_UTF16, 2, 2,
#if U_IS_BIG_ENDIAN
{ 0xff, 0xfd, 0, 0 }, 2,
#else
{ 0xfd, 0xff, 0, 0 }, 2,
#endif
FALSE, FALSE,
0,
0,
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }
};
const UConverterSharedData _UTF16Data = {
sizeof(UConverterSharedData), ~((uint32_t) 0),
NULL, NULL, &_UTF16StaticData, FALSE, &_UTF16Impl,
0
};