2000-02-05 00:19:15 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
|
|
|
* Copyright (C) 2000, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* file name: ucnv_utf.cpp
|
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 2000feb03
|
|
|
|
* created by: Markus W. Scherer
|
2000-06-27 20:47:56 +00:00
|
|
|
*
|
|
|
|
* Change history:
|
|
|
|
*
|
|
|
|
* 06/29/2000 helena Major rewrite of the callback APIs.
|
2000-07-21 01:25:30 +00:00
|
|
|
* 07/20/2000 george Change the coding style to conform to the coding guidelines,
|
|
|
|
* and a few miscellaneous bug fixes.
|
2000-11-16 17:20:03 +00:00
|
|
|
* 11/15/2000 george Added UTF-32
|
2000-02-05 00:19:15 +00:00
|
|
|
*/
|
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
#include "cmemory.h"
|
2000-02-05 00:19:15 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "ucmp16.h"
|
|
|
|
#include "ucmp8.h"
|
2000-06-27 20:47:56 +00:00
|
|
|
#include "unicode/ucnv_err.h"
|
|
|
|
#include "ucnv_bld.h"
|
2000-02-05 00:19:15 +00:00
|
|
|
#include "unicode/ucnv.h"
|
|
|
|
#include "ucnv_cnv.h"
|
|
|
|
|
|
|
|
/* UTF-8 -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* UTF-8 Conversion DATA
|
|
|
|
* for more information see Unicode Strandard 2.0 , Transformation Formats Appendix A-9
|
|
|
|
*/
|
2000-07-21 01:25:30 +00:00
|
|
|
/*static const uint32_t REPLACEMENT_CHARACTER = 0x0000FFFD;*/
|
|
|
|
static const uint32_t MAXIMUM_UCS2 = 0x0000FFFF;
|
2000-11-16 17:20:03 +00:00
|
|
|
static const uint32_t MAXIMUM_UTF = 0x0010FFFF;
|
2000-07-21 01:25:30 +00:00
|
|
|
static const uint32_t MAXIMUM_UCS4 = 0x7FFFFFFF;
|
|
|
|
static const int8_t HALF_SHIFT = 10;
|
|
|
|
static const uint32_t HALF_BASE = 0x0010000;
|
|
|
|
static const uint32_t HALF_MASK = 0x3FF;
|
|
|
|
static const uint32_t SURROGATE_HIGH_START = 0xD800;
|
|
|
|
static const uint32_t SURROGATE_HIGH_END = 0xDBFF;
|
|
|
|
static const uint32_t SURROGATE_LOW_START = 0xDC00;
|
|
|
|
static const uint32_t SURROGATE_LOW_END = 0xDFFF;
|
2000-07-28 03:11:38 +00:00
|
|
|
static const uint32_t SURROGATE_LOW_BASE = 9216; /* -SURROGATE_LOW_START + HALF_BASE */
|
2000-02-05 00:19:15 +00:00
|
|
|
|
2000-07-20 05:17:49 +00:00
|
|
|
static const uint32_t offsetsFromUTF8[7] = {0,
|
2000-02-05 00:19:15 +00:00
|
|
|
(uint32_t) 0x00000000, (uint32_t) 0x00003080, (uint32_t) 0x000E2080,
|
|
|
|
(uint32_t) 0x03C82080, (uint32_t) 0xFA082080, (uint32_t) 0x82082080
|
|
|
|
};
|
|
|
|
|
|
|
|
/* END OF UTF-8 Conversion DATA */
|
|
|
|
|
2000-07-20 05:17:49 +00:00
|
|
|
static const int8_t bytesFromUTF8[256] = {
|
2000-02-05 00:19:15 +00:00
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0
|
|
|
|
};
|
|
|
|
|
2000-07-26 00:55:30 +00:00
|
|
|
/* static const unsigned char firstByteMark[7] = {0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC};*/
|
2000-02-05 00:19:15 +00:00
|
|
|
|
2000-07-22 03:21:16 +00:00
|
|
|
#define INVALID_UTF8_TAIL(utf8) (((utf8) & 0xC0) != 0x80)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls invalid char callback when an invalid character sequence is encountered.
|
|
|
|
* It presumes that the converter has a callback to call.
|
|
|
|
*
|
2000-07-25 19:33:02 +00:00
|
|
|
* @returns true when callback fails
|
2000-07-22 03:21:16 +00:00
|
|
|
*/
|
2000-11-16 17:20:03 +00:00
|
|
|
static UBool
|
|
|
|
T_UConverter_toUnicode_InvalidChar_Callback(UConverterToUnicodeArgs * args,
|
2000-07-22 03:21:16 +00:00
|
|
|
UErrorCode *err)
|
|
|
|
{
|
|
|
|
UConverter *converter = args->converter;
|
|
|
|
|
2000-07-25 19:33:02 +00:00
|
|
|
if (U_SUCCESS(*err))
|
|
|
|
{
|
|
|
|
*err = U_ILLEGAL_CHAR_FOUND;
|
|
|
|
}
|
|
|
|
|
2000-07-26 00:55:30 +00:00
|
|
|
/* Make the toUBytes invalid */
|
2000-07-22 03:21:16 +00:00
|
|
|
uprv_memcpy(converter->invalidCharBuffer,
|
|
|
|
converter->toUBytes,
|
|
|
|
converter->invalidCharLength);
|
|
|
|
|
|
|
|
/* Call the ErrorFunction */
|
|
|
|
args->converter->fromCharErrorBehaviour(converter->toUContext,
|
|
|
|
args,
|
|
|
|
converter->invalidCharBuffer,
|
|
|
|
converter->invalidCharLength,
|
|
|
|
UCNV_ILLEGAL,
|
|
|
|
err);
|
|
|
|
|
2000-07-26 00:55:30 +00:00
|
|
|
return (UBool)U_FAILURE(*err);
|
2000-07-22 03:21:16 +00:00
|
|
|
}
|
|
|
|
|
2000-11-16 17:20:03 +00:00
|
|
|
static UBool
|
|
|
|
T_UConverter_toUnicode_InvalidChar_OffsetCallback(UConverterToUnicodeArgs * args,
|
2000-07-25 19:33:02 +00:00
|
|
|
int32_t currentOffset,
|
|
|
|
UErrorCode *err)
|
|
|
|
{
|
|
|
|
int32_t *saveOffsets = args->offsets;
|
|
|
|
UBool result;
|
|
|
|
|
|
|
|
result = T_UConverter_toUnicode_InvalidChar_Callback(args, err);
|
|
|
|
|
|
|
|
while (saveOffsets < args->offsets)
|
|
|
|
{
|
|
|
|
*(saveOffsets++) = currentOffset;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-07-17 23:36:52 +00:00
|
|
|
U_CFUNC void T_UConverter_toUnicode_UTF8 (UConverterToUnicodeArgs * args,
|
2000-02-05 00:19:15 +00:00
|
|
|
UErrorCode * err)
|
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
const unsigned char *mySource = (unsigned char *) args->source;
|
|
|
|
UChar *myTarget = args->target;
|
2000-07-25 19:33:02 +00:00
|
|
|
const unsigned char *sourceLimit = (unsigned char *) args->sourceLimit;
|
|
|
|
const UChar *targetLimit = args->targetLimit;
|
2000-07-28 03:11:38 +00:00
|
|
|
unsigned char *toUBytes = args->converter->toUBytes;
|
2000-07-25 19:33:02 +00:00
|
|
|
UBool invalidTailChar = FALSE;
|
2000-07-28 03:11:38 +00:00
|
|
|
uint32_t ch, ch2 = 0, i;
|
|
|
|
uint32_t inBytes; /* Total number of bytes in the current UTF8 sequence */
|
2000-02-05 00:19:15 +00:00
|
|
|
|
2000-07-25 19:33:02 +00:00
|
|
|
if (U_FAILURE(*err))
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-25 19:33:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2000-02-05 00:19:15 +00:00
|
|
|
|
2000-07-25 19:33:02 +00:00
|
|
|
/* Restore size of current sequence */
|
2000-07-28 03:11:38 +00:00
|
|
|
if (args->converter->toUnicodeStatus && myTarget < targetLimit)
|
2000-07-25 19:33:02 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
inBytes = args->converter->toULength; /* restore # of bytes to consume */
|
|
|
|
i = args->converter->invalidCharLength; /* restore # of bytes consumed */
|
2000-07-25 19:33:02 +00:00
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
ch = args->converter->toUnicodeStatus;/*Stores the previously calculated ch from a previous call*/
|
|
|
|
args->converter->toUnicodeStatus = 0;
|
2000-07-21 01:25:30 +00:00
|
|
|
goto morebytes;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
while (mySource < sourceLimit && myTarget < targetLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
ch = *(mySource++);
|
|
|
|
if (ch < 0x80) /* Simple case */
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
*(myTarget++) = (UChar) ch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* store the first char */
|
|
|
|
toUBytes[0] = (char)ch;
|
|
|
|
inBytes = bytesFromUTF8[ch]; /* lookup current sequence length */
|
|
|
|
i = 1;
|
2000-02-05 00:19:15 +00:00
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
morebytes:
|
2000-07-28 03:11:38 +00:00
|
|
|
while (i < inBytes)
|
|
|
|
{
|
|
|
|
if (mySource < sourceLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
toUBytes[i] = (char) (ch2 = *(mySource++));
|
|
|
|
if (INVALID_UTF8_TAIL(ch2))
|
2000-07-21 01:25:30 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
|
|
|
invalidTailChar = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ch = (ch << 6) + ch2;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (args->flush)
|
|
|
|
{
|
|
|
|
if (U_SUCCESS(*err))
|
2000-07-25 19:33:02 +00:00
|
|
|
{
|
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
}
|
|
|
|
else
|
2000-07-28 03:11:38 +00:00
|
|
|
{ /* stores a partially calculated target*/
|
|
|
|
args->converter->toUnicodeStatus = ch;
|
|
|
|
args->converter->toULength = (int8_t) inBytes;
|
|
|
|
args->converter->invalidCharLength = (int8_t) i;
|
2000-07-21 01:25:30 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
goto donefornow;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
}
|
2000-02-05 00:19:15 +00:00
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
/* Remove the acummulated high bits */
|
|
|
|
ch -= offsetsFromUTF8[inBytes];
|
2000-02-05 00:19:15 +00:00
|
|
|
|
2000-11-16 17:20:03 +00:00
|
|
|
if (i == inBytes && ch <= MAXIMUM_UTF)
|
2000-07-28 03:11:38 +00:00
|
|
|
{
|
|
|
|
/* Normal valid byte when the loop has not prematurely terminated (i < inBytes) */
|
|
|
|
if (ch <= MAXIMUM_UCS2)
|
2000-07-26 00:55:30 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
/* fits in 16 bits */
|
|
|
|
*(myTarget++) = (UChar) ch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* write out the surrogates */
|
|
|
|
ch -= HALF_BASE;
|
|
|
|
*(myTarget++) = (UChar) ((ch >> HALF_SHIFT) + SURROGATE_HIGH_START);
|
|
|
|
ch = (ch & HALF_MASK) + SURROGATE_LOW_START;
|
|
|
|
if (myTarget < targetLimit)
|
2000-07-26 00:55:30 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
*(myTarget++) = (UChar)ch;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
/* Put in overflow buffer (not handled here) */
|
|
|
|
args->converter->UCharErrorBuffer[0] = (UChar) ch;
|
|
|
|
args->converter->UCharErrorBufferLength = 1;
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-07-28 03:11:38 +00:00
|
|
|
break;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
args->source = (const char *) mySource;
|
|
|
|
args->target = myTarget;
|
|
|
|
args->converter->invalidCharLength = (int8_t)i;
|
|
|
|
if (T_UConverter_toUnicode_InvalidChar_Callback(args, err))
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
/* Stop if the error wasn't handled */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
args->converter->invalidCharLength = 0;
|
|
|
|
mySource = (unsigned char *) args->source;
|
|
|
|
myTarget = args->target;
|
|
|
|
if (invalidTailChar)
|
|
|
|
{
|
|
|
|
/* Treat the tail as ASCII*/
|
|
|
|
if (myTarget < targetLimit)
|
2000-07-21 01:25:30 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
*(myTarget++) = (UChar) ch2;
|
|
|
|
invalidTailChar = FALSE;
|
2000-07-25 19:33:02 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
else
|
2000-07-25 19:33:02 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
/* Put in overflow buffer (not handled here) */
|
|
|
|
args->converter->UCharErrorBuffer[0] = (UChar) ch2;
|
|
|
|
args->converter->UCharErrorBufferLength = 1;
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-07-28 03:11:38 +00:00
|
|
|
break;
|
2000-07-21 01:25:30 +00:00
|
|
|
}
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
donefornow:
|
2000-07-28 03:11:38 +00:00
|
|
|
if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err))
|
|
|
|
{
|
|
|
|
/* End of target buffer */
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-07-28 03:11:38 +00:00
|
|
|
}
|
|
|
|
|
2000-07-25 19:33:02 +00:00
|
|
|
args->target = myTarget;
|
|
|
|
args->source = (const char *) mySource;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2000-07-17 23:36:52 +00:00
|
|
|
U_CFUNC void T_UConverter_toUnicode_UTF8_OFFSETS_LOGIC (UConverterToUnicodeArgs * args,
|
2000-02-05 00:19:15 +00:00
|
|
|
UErrorCode * err)
|
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
const unsigned char *mySource = (unsigned char *) args->source;
|
|
|
|
UChar *myTarget = args->target;
|
2000-07-21 20:42:04 +00:00
|
|
|
int32_t *myOffsets = args->offsets;
|
2000-07-25 19:33:02 +00:00
|
|
|
int32_t offsetNum = 0;
|
|
|
|
const unsigned char *sourceLimit = (unsigned char *) args->sourceLimit;
|
|
|
|
const UChar *targetLimit = args->targetLimit;
|
2000-07-28 03:11:38 +00:00
|
|
|
unsigned char *toUBytes = args->converter->toUBytes;
|
2000-07-25 19:33:02 +00:00
|
|
|
UBool invalidTailChar = FALSE;
|
|
|
|
uint32_t ch, ch2 = 0, i;
|
2000-07-28 03:11:38 +00:00
|
|
|
uint32_t inBytes;
|
2000-07-25 19:33:02 +00:00
|
|
|
|
|
|
|
/* Restore size of current sequence */
|
2000-07-28 03:11:38 +00:00
|
|
|
if (args->converter->toUnicodeStatus && myTarget < targetLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
inBytes = args->converter->toULength; /* restore # of bytes to consume */
|
|
|
|
i = args->converter->invalidCharLength; /* restore # of bytes consumed */
|
2000-07-22 03:21:16 +00:00
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
ch = args->converter->toUnicodeStatus;/*Stores the previously calculated ch from a previous call*/
|
|
|
|
args->converter->toUnicodeStatus = 0;
|
2000-07-21 01:25:30 +00:00
|
|
|
goto morebytes;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
while (mySource < sourceLimit && myTarget < targetLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
ch = *(mySource++);
|
|
|
|
if (ch < 0x80) /* Simple case */
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
*(myTarget++) = (UChar) ch;
|
|
|
|
*(myOffsets++) = offsetNum++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
toUBytes[0] = (char)ch;
|
|
|
|
inBytes = bytesFromUTF8[ch];
|
|
|
|
i = 1;
|
2000-02-05 00:19:15 +00:00
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
morebytes:
|
2000-07-28 03:11:38 +00:00
|
|
|
while (i < inBytes)
|
|
|
|
{
|
|
|
|
if (mySource < sourceLimit)
|
|
|
|
{
|
|
|
|
toUBytes[i] = (char) (ch2 = *(mySource++));
|
|
|
|
if (INVALID_UTF8_TAIL(ch2))
|
|
|
|
{
|
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
|
|
|
invalidTailChar = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ch = (ch << 6) + ch2;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
if (args->flush)
|
2000-07-21 01:25:30 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
if (U_SUCCESS(*err))
|
2000-07-25 19:33:02 +00:00
|
|
|
{
|
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
2000-07-28 03:11:38 +00:00
|
|
|
args->converter->toUnicodeStatus = 0;
|
2000-07-25 19:33:02 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
}
|
2000-07-22 03:21:16 +00:00
|
|
|
else
|
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
args->converter->toUnicodeStatus = ch;
|
|
|
|
args->converter->toULength = (int8_t)inBytes;
|
|
|
|
args->converter->invalidCharLength = (int8_t)i;
|
2000-07-21 01:25:30 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
goto donefornow;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
}
|
2000-02-05 00:19:15 +00:00
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
/* Remove the acummulated high bits */
|
|
|
|
ch -= offsetsFromUTF8[inBytes];
|
2000-02-05 00:19:15 +00:00
|
|
|
|
2000-11-16 17:20:03 +00:00
|
|
|
if (i == inBytes && ch <= MAXIMUM_UTF)
|
2000-07-28 03:11:38 +00:00
|
|
|
{
|
|
|
|
/* Normal valid byte when the loop has not prematurely terminated (i < inBytes) */
|
|
|
|
if (ch <= MAXIMUM_UCS2)
|
2000-07-21 01:25:30 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
/* fits in 16 bits */
|
|
|
|
*(myTarget++) = (UChar) ch;
|
|
|
|
*(myOffsets++) = offsetNum;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* write out the surrogates */
|
|
|
|
*(myOffsets++) = offsetNum;
|
|
|
|
ch -= HALF_BASE;
|
|
|
|
*(myTarget++) = (UChar) ((ch >> HALF_SHIFT) + SURROGATE_HIGH_START);
|
|
|
|
ch = (ch & HALF_MASK) + SURROGATE_LOW_START;
|
|
|
|
if (myTarget < targetLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
*(myTarget++) = (UChar)ch;
|
2000-07-26 00:55:30 +00:00
|
|
|
*(myOffsets++) = offsetNum;
|
2000-07-21 01:25:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
args->converter->UCharErrorBuffer[0] = (UChar) ch;
|
|
|
|
args->converter->UCharErrorBufferLength = 1;
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
offsetNum += i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
UBool useOffset;
|
|
|
|
|
|
|
|
args->source = (const char *) mySource;
|
|
|
|
args->target = myTarget;
|
|
|
|
args->offsets = myOffsets;
|
|
|
|
args->converter->invalidCharLength = (int8_t)i;
|
|
|
|
if (T_UConverter_toUnicode_InvalidChar_OffsetCallback(args,
|
|
|
|
offsetNum, err))
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
/* Stop if the error wasn't handled */
|
|
|
|
break;
|
|
|
|
}
|
2000-07-26 00:55:30 +00:00
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
args->converter->invalidCharLength = 0;
|
|
|
|
mySource = (unsigned char *) args->source;
|
|
|
|
myTarget = args->target;
|
2000-07-26 00:55:30 +00:00
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
useOffset = (UBool)(myOffsets != args->offsets);
|
|
|
|
myOffsets = args->offsets;
|
|
|
|
offsetNum += i;
|
2000-07-26 00:55:30 +00:00
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
if (invalidTailChar)
|
|
|
|
{
|
|
|
|
/* Treat the tail as ASCII*/
|
|
|
|
if (myTarget < targetLimit)
|
2000-07-25 19:33:02 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
*(myTarget++) = (UChar) ch2;
|
|
|
|
*myOffsets = offsetNum++;
|
|
|
|
if (useOffset)
|
2000-07-25 19:33:02 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
/* Increment when the target was consumed */
|
|
|
|
myOffsets++;
|
2000-07-25 19:33:02 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
invalidTailChar = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Put in overflow buffer (not handled here) */
|
|
|
|
args->converter->UCharErrorBuffer[0] = (UChar) ch2;
|
|
|
|
args->converter->UCharErrorBufferLength = 1;
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-07-28 03:11:38 +00:00
|
|
|
break;
|
2000-07-25 19:33:02 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
}
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
donefornow:
|
2000-07-28 03:11:38 +00:00
|
|
|
if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err))
|
|
|
|
{ /* End of target buffer */
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-07-28 03:11:38 +00:00
|
|
|
}
|
|
|
|
|
2000-07-25 19:33:02 +00:00
|
|
|
args->target = myTarget;
|
|
|
|
args->source = (const char *) mySource;
|
|
|
|
args->offsets = myOffsets;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2000-07-19 00:21:29 +00:00
|
|
|
U_CFUNC void T_UConverter_fromUnicode_UTF8 (UConverterFromUnicodeArgs * args,
|
2000-02-05 00:19:15 +00:00
|
|
|
UErrorCode * err)
|
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
const UChar *mySource = args->source;
|
|
|
|
unsigned char *myTarget = (unsigned char *) args->target;
|
2000-07-28 03:11:38 +00:00
|
|
|
const UChar *sourceLimit = args->sourceLimit;
|
|
|
|
const unsigned char *targetLimit = (unsigned char *) args->targetLimit;
|
|
|
|
uint32_t ch, ch2;
|
|
|
|
int16_t indexToWrite;
|
2000-07-21 01:25:30 +00:00
|
|
|
char temp[4];
|
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
if (args->converter->fromUnicodeStatus && myTarget < targetLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
ch = args->converter->fromUnicodeStatus;
|
|
|
|
args->converter->fromUnicodeStatus = 0;
|
|
|
|
goto lowsurogate;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
while (mySource < sourceLimit && myTarget < targetLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
ch = *(mySource++);
|
2000-02-05 00:19:15 +00:00
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
if (ch < 0x80) /* Single byte */
|
|
|
|
{
|
|
|
|
*(myTarget++) = (char) ch;
|
|
|
|
}
|
|
|
|
else if (ch < 0x800) /* Double byte */
|
|
|
|
{
|
|
|
|
*(myTarget++) = (char) ((ch >> 6) | 0xc0);
|
|
|
|
if (myTarget < targetLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
*(myTarget++) = (char) ((ch & 0x3f) | 0x80);
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
args->converter->charErrorBuffer[0] = (char) ((ch & 0x3f) | 0x80);
|
|
|
|
args->converter->charErrorBufferLength = 1;
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Check for surogates */
|
|
|
|
{
|
|
|
|
if ((ch >= SURROGATE_HIGH_START) && (ch <= SURROGATE_HIGH_END))
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
lowsurogate:
|
2000-07-28 03:11:38 +00:00
|
|
|
if (mySource < sourceLimit)
|
|
|
|
{
|
|
|
|
ch2 = *mySource;
|
|
|
|
if ((ch2 >= SURROGATE_LOW_START) && (ch2 <= SURROGATE_LOW_END))
|
2000-07-20 00:10:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
/* If there were two surrogates, combine them otherwise treat them normally */
|
|
|
|
ch = ((ch - SURROGATE_HIGH_START) << HALF_SHIFT) + ch2 + SURROGATE_LOW_BASE;
|
|
|
|
mySource++;
|
2000-07-20 00:10:15 +00:00
|
|
|
}
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
else if (!args->flush)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
args->converter->fromUnicodeStatus = ch;
|
|
|
|
break;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ch < 0x10000)
|
|
|
|
{
|
|
|
|
indexToWrite = 2;
|
|
|
|
temp[2] = (char) ((ch >> 12) | 0xe0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
indexToWrite = 3;
|
|
|
|
temp[3] = (char) ((ch >> 18) | 0xf0);
|
2000-11-28 19:30:04 +00:00
|
|
|
temp[2] = (char) (((ch >> 12) & 0x3f) | 0x80);
|
2000-07-28 03:11:38 +00:00
|
|
|
}
|
2000-11-28 19:30:04 +00:00
|
|
|
temp[1] = (char) (((ch >> 6) & 0x3f) | 0x80);
|
|
|
|
temp[0] = (char) ((ch & 0x3f) | 0x80);
|
2000-07-28 03:11:38 +00:00
|
|
|
|
|
|
|
for (; indexToWrite >= 0; indexToWrite--)
|
|
|
|
{
|
|
|
|
if (myTarget < targetLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
*(myTarget++) = temp[indexToWrite];
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
args->converter->charErrorBuffer[args->converter->charErrorBufferLength++] = temp[indexToWrite];
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err))
|
|
|
|
{
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-07-28 03:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args->target = (char *) myTarget;
|
|
|
|
args->source = mySource;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2000-07-19 00:21:29 +00:00
|
|
|
U_CFUNC void T_UConverter_fromUnicode_UTF8_OFFSETS_LOGIC (UConverterFromUnicodeArgs * args,
|
2000-02-05 00:19:15 +00:00
|
|
|
UErrorCode * err)
|
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
const UChar *mySource = args->source;
|
|
|
|
unsigned char *myTarget = (unsigned char *) args->target;
|
2000-07-21 20:42:04 +00:00
|
|
|
int32_t *myOffsets = args->offsets;
|
2000-07-28 03:11:38 +00:00
|
|
|
const UChar *sourceLimit = args->sourceLimit;
|
|
|
|
const unsigned char *targetLimit = (unsigned char *) args->targetLimit;
|
|
|
|
uint32_t ch, ch2;
|
|
|
|
int32_t offsetNum = 0;
|
|
|
|
int16_t indexToWrite;
|
2000-07-21 01:25:30 +00:00
|
|
|
char temp[4];
|
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
if (args->converter->fromUnicodeStatus && myTarget < targetLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
ch = args->converter->fromUnicodeStatus;
|
|
|
|
args->converter->fromUnicodeStatus = 0;
|
|
|
|
goto lowsurogate;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
while (mySource < sourceLimit && myTarget < targetLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
ch = *(mySource++);
|
2000-02-05 00:19:15 +00:00
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
if (ch < 0x80) /* Single byte */
|
|
|
|
{
|
|
|
|
*(myOffsets++) = offsetNum++;
|
|
|
|
*(myTarget++) = (char) ch;
|
|
|
|
}
|
|
|
|
else if (ch < 0x800) /* Double byte */
|
|
|
|
{
|
|
|
|
*(myOffsets++) = offsetNum;
|
|
|
|
*(myTarget++) = (char) ((ch >> 6) | 0xc0);
|
|
|
|
if (myTarget < targetLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
*(myOffsets++) = offsetNum++;
|
|
|
|
*(myTarget++) = (char) ((ch & 0x3f) | 0x80);
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
args->converter->charErrorBuffer[0] = (char) ((ch & 0x3f) | 0x80);
|
|
|
|
args->converter->charErrorBufferLength = 1;
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Check for surogates */
|
|
|
|
{
|
|
|
|
if ((ch >= SURROGATE_HIGH_START) && (ch <= SURROGATE_HIGH_END))
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
lowsurogate:
|
2000-07-28 03:11:38 +00:00
|
|
|
if (mySource < sourceLimit)
|
|
|
|
{
|
|
|
|
ch2 = *mySource;
|
|
|
|
if ((ch2 >= SURROGATE_LOW_START) && (ch2 <= SURROGATE_LOW_END))
|
2000-07-20 00:10:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
/* If there were two surrogates, combine them otherwise treat them normally */
|
|
|
|
ch = ((ch - SURROGATE_HIGH_START) << HALF_SHIFT) + ch2 + SURROGATE_LOW_BASE;
|
|
|
|
mySource++;
|
2000-07-20 00:10:15 +00:00
|
|
|
}
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
else if (!args->flush)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
args->converter->fromUnicodeStatus = ch;
|
|
|
|
break;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ch < 0x10000)
|
|
|
|
{
|
|
|
|
indexToWrite = 2;
|
|
|
|
temp[2] = (char) ((ch >> 12) | 0xe0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
indexToWrite = 3;
|
|
|
|
temp[3] = (char) ((ch >> 18) | 0xf0);
|
2000-11-28 19:30:04 +00:00
|
|
|
temp[2] = (char) (((ch >> 12) & 0x3f) | 0x80);
|
2000-07-28 03:11:38 +00:00
|
|
|
}
|
2000-11-28 19:30:04 +00:00
|
|
|
temp[1] = (char) (((ch >> 6) & 0x3f) | 0x80);
|
|
|
|
temp[0] = (char) ((ch & 0x3f) | 0x80);
|
2000-07-28 03:11:38 +00:00
|
|
|
|
|
|
|
for (; indexToWrite >= 0; indexToWrite--)
|
|
|
|
{
|
|
|
|
if (myTarget < targetLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
*(myOffsets++) = offsetNum;
|
|
|
|
*(myTarget++) = temp[indexToWrite];
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-28 03:11:38 +00:00
|
|
|
args->converter->charErrorBuffer[args->converter->charErrorBufferLength++] = temp[indexToWrite];
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
2000-07-28 03:11:38 +00:00
|
|
|
offsetNum += (ch >= 0x10000) + 1;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-28 03:11:38 +00:00
|
|
|
if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err))
|
|
|
|
{
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-07-28 03:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args->target = (char *) myTarget;
|
|
|
|
args->source = mySource;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2000-07-19 00:21:29 +00:00
|
|
|
U_CFUNC UChar32 T_UConverter_getNextUChar_UTF8(UConverterToUnicodeArgs *args,
|
2000-02-05 00:19:15 +00:00
|
|
|
UErrorCode* err)
|
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
/*safe keeps a ptr to the beginning in case we need to step back*/
|
|
|
|
char const *sourceInitial = args->source;
|
|
|
|
uint16_t extraBytesToWrite;
|
|
|
|
uint8_t myByte;
|
|
|
|
UChar32 ch;
|
|
|
|
int8_t isLegalSequence = 1;
|
|
|
|
|
|
|
|
/*Input boundary check*/
|
|
|
|
if (args->source >= args->sourceLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
return 0xffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
myByte = (uint8_t)*(args->source++);
|
|
|
|
if (myByte < 0x80)
|
|
|
|
{
|
|
|
|
return (UChar32)myByte;
|
|
|
|
}
|
|
|
|
extraBytesToWrite = (uint16_t)bytesFromUTF8[myByte];
|
|
|
|
if (extraBytesToWrite == 0) {
|
|
|
|
goto CALL_ERROR_FUNCTION;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
/*The byte sequence is longer than the buffer area passed*/
|
2000-02-05 00:19:15 +00:00
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
if ((args->source + extraBytesToWrite - 1) > args->sourceLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
|
|
|
return 0xffff;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
ch = myByte << 6;
|
|
|
|
switch(extraBytesToWrite)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
|
|
|
/* note: code falls through cases! (sic)*/
|
2000-07-21 01:25:30 +00:00
|
|
|
case 6:
|
|
|
|
ch += (myByte = (uint8_t)*(args->source++));
|
|
|
|
ch <<= 6;
|
|
|
|
if ((myByte & 0xC0) != 0x80)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
isLegalSequence = 0;
|
|
|
|
break;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
case 5:
|
|
|
|
ch += (myByte = *(args->source++));
|
|
|
|
ch <<= 6;
|
|
|
|
if ((myByte & 0xC0) != 0x80)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
isLegalSequence = 0;
|
|
|
|
break;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
case 4:
|
|
|
|
ch += (myByte = *(args->source++));
|
|
|
|
ch <<= 6;
|
|
|
|
if ((myByte & 0xC0) != 0x80)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
isLegalSequence = 0;
|
|
|
|
break;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
case 3:
|
|
|
|
ch += (myByte = *(args->source++));
|
|
|
|
ch <<= 6;
|
|
|
|
if ((myByte & 0xC0) != 0x80)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
isLegalSequence = 0;
|
|
|
|
break;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
case 2:
|
|
|
|
ch += (myByte = *(args->source++));
|
|
|
|
if ((myByte & 0xC0) != 0x80)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
isLegalSequence = 0;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
ch -= offsetsFromUTF8[extraBytesToWrite];
|
2000-04-13 17:27:35 +00:00
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
if (isLegalSequence)
|
|
|
|
return ch; /* return the code point */
|
2000-04-13 17:27:35 +00:00
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
CALL_ERROR_FUNCTION:
|
|
|
|
{
|
2000-07-20 00:10:15 +00:00
|
|
|
UChar myUChar = (UChar)0xffff; /* ### TODO: this is a hack until we prepare the callbacks for code points */
|
2000-02-05 00:19:15 +00:00
|
|
|
UChar* myUCharPtr = &myUChar;
|
|
|
|
|
|
|
|
*err = U_ILLEGAL_CHAR_FOUND;
|
|
|
|
|
2000-04-13 17:27:35 +00:00
|
|
|
/*It is very likely that the ErrorFunctor will write to the
|
2000-02-05 00:19:15 +00:00
|
|
|
*internal buffers */
|
2000-07-10 20:51:54 +00:00
|
|
|
args->target = myUCharPtr;
|
|
|
|
args->targetLimit = myUCharPtr + 1;
|
|
|
|
args->converter->fromCharErrorBehaviour(args->converter->toUContext,
|
|
|
|
args,
|
2000-07-13 23:55:33 +00:00
|
|
|
sourceInitial,
|
|
|
|
args->source-sourceInitial,
|
|
|
|
UCNV_ILLEGAL,
|
2000-06-27 20:47:56 +00:00
|
|
|
err);
|
|
|
|
|
2000-02-05 00:19:15 +00:00
|
|
|
/*makes the internal caching transparent to the user*/
|
2000-08-11 19:23:50 +00:00
|
|
|
if (*err == U_BUFFER_OVERFLOW_ERROR)
|
2000-07-21 01:25:30 +00:00
|
|
|
*err = U_ZERO_ERROR;
|
|
|
|
|
2000-04-13 17:27:35 +00:00
|
|
|
return (UChar32)myUChar;
|
2000-07-21 01:25:30 +00:00
|
|
|
}
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2000-02-09 19:15:17 +00:00
|
|
|
static const UConverterImpl _UTF8Impl={
|
2000-02-05 00:19:15 +00:00
|
|
|
UCNV_UTF8,
|
|
|
|
|
2000-02-08 23:41:16 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
2000-02-05 00:19:15 +00:00
|
|
|
T_UConverter_toUnicode_UTF8,
|
|
|
|
T_UConverter_toUnicode_UTF8_OFFSETS_LOGIC,
|
|
|
|
T_UConverter_fromUnicode_UTF8,
|
|
|
|
T_UConverter_fromUnicode_UTF8_OFFSETS_LOGIC,
|
2000-02-09 19:15:17 +00:00
|
|
|
T_UConverter_getNextUChar_UTF8,
|
|
|
|
|
2000-11-27 17:40:41 +00:00
|
|
|
NULL,
|
2000-02-09 19:15:17 +00:00
|
|
|
NULL
|
2000-02-05 00:19:15 +00:00
|
|
|
};
|
|
|
|
|
2000-08-01 17:58:43 +00:00
|
|
|
/* Todo: verify that UTF-8 == (ccsid (ibm-codepage) 1208) for unicode version 2.0 and 3.0 */
|
2000-04-19 23:05:27 +00:00
|
|
|
const UConverterStaticData _UTF8StaticData={
|
|
|
|
sizeof(UConverterStaticData),
|
|
|
|
"UTF8",
|
|
|
|
1208, UCNV_IBM, UCNV_UTF8, 1, 4,
|
2000-10-03 20:18:22 +00:00
|
|
|
{ 0xef, 0xbf, 0xbd, 0 },3,FALSE,FALSE,{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
|
2000-04-19 23:05:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-04-13 23:00:43 +00:00
|
|
|
const UConverterSharedData _UTF8Data={
|
2000-03-29 21:41:11 +00:00
|
|
|
sizeof(UConverterSharedData), ~((uint32_t) 0),
|
2000-04-19 23:05:27 +00:00
|
|
|
NULL, NULL, &_UTF8StaticData, FALSE, &_UTF8Impl,
|
|
|
|
0
|
2000-02-05 00:19:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* UTF-16BE ----------------------------------------------------------------- */
|
|
|
|
|
2000-07-17 23:36:52 +00:00
|
|
|
U_CFUNC void T_UConverter_toUnicode_UTF16_BE (UConverterToUnicodeArgs * args,
|
2000-02-05 00:19:15 +00:00
|
|
|
UErrorCode * err)
|
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
const unsigned char *mySource = (unsigned char *) args->source;
|
|
|
|
UChar *myTarget = args->target;
|
|
|
|
int32_t mySourceIndex = 0;
|
|
|
|
int32_t myTargetIndex = 0;
|
|
|
|
int32_t targetLength = args->targetLimit - myTarget;
|
|
|
|
int32_t sourceLength = args->sourceLimit - (char *) mySource;
|
|
|
|
UChar mySourceChar = 0x0000;
|
|
|
|
UChar oldmySourceChar = 0x0000;
|
|
|
|
|
|
|
|
while (mySourceIndex < sourceLength)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
if (myTargetIndex < targetLength)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
/*gets the corresponding UChar */
|
|
|
|
mySourceChar = (unsigned char) mySource[mySourceIndex++];
|
|
|
|
oldmySourceChar = mySourceChar;
|
|
|
|
if (args->converter->toUnicodeStatus == 0)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
args->converter->toUnicodeStatus =
|
|
|
|
(unsigned char) mySourceChar == 0 ? 0xFFFF : mySourceChar;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
if (args->converter->toUnicodeStatus != 0xFFFF)
|
|
|
|
mySourceChar = (UChar) ((args->converter->toUnicodeStatus << 8) | mySourceChar);
|
|
|
|
args->converter->toUnicodeStatus = 0;
|
2000-02-05 00:19:15 +00:00
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
myTarget[myTargetIndex++] = mySourceChar;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-07-21 01:25:30 +00:00
|
|
|
break;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
if (U_SUCCESS(*err) && args->flush
|
2000-02-05 00:19:15 +00:00
|
|
|
&& (mySourceIndex == sourceLength)
|
2000-07-10 20:51:54 +00:00
|
|
|
&& (args->converter->toUnicodeStatus != 0x00))
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
if (U_SUCCESS(*err))
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
|
|
|
args->converter->toUnicodeStatus = 0x00;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
|
|
|
|
args->target += myTargetIndex;
|
|
|
|
args->source += mySourceIndex;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2000-07-19 00:21:29 +00:00
|
|
|
U_CFUNC void T_UConverter_fromUnicode_UTF16_BE (UConverterFromUnicodeArgs * args,
|
2000-02-05 00:19:15 +00:00
|
|
|
UErrorCode * err)
|
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
const UChar *mySource = args->source;
|
|
|
|
unsigned char *myTarget = (unsigned char *) args->target;
|
|
|
|
int32_t mySourceIndex = 0;
|
|
|
|
int32_t myTargetIndex = 0;
|
|
|
|
int32_t targetLength = args->targetLimit - (char *) myTarget;
|
|
|
|
int32_t sourceLength = args->sourceLimit - mySource;
|
|
|
|
UChar mySourceChar;
|
|
|
|
|
|
|
|
/*writing the char to the output stream */
|
|
|
|
while (mySourceIndex < sourceLength)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
if (myTargetIndex < targetLength)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
mySourceChar = (UChar) mySource[mySourceIndex++];
|
|
|
|
myTarget[myTargetIndex++] = (char) (mySourceChar >> 8);
|
|
|
|
if (myTargetIndex < targetLength)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
myTarget[myTargetIndex++] = (char) mySourceChar;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
args->converter->charErrorBuffer[0] = (char) mySourceChar;
|
|
|
|
args->converter->charErrorBufferLength = 1;
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-07-21 01:25:30 +00:00
|
|
|
break;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
args->target += myTargetIndex;
|
|
|
|
args->source += mySourceIndex;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2000-07-19 00:21:29 +00:00
|
|
|
U_CFUNC UChar32 T_UConverter_getNextUChar_UTF16_BE(UConverterToUnicodeArgs* args,
|
2000-02-05 00:19:15 +00:00
|
|
|
UErrorCode* err)
|
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
UChar32 myUChar;
|
|
|
|
uint16_t first;
|
|
|
|
/*Checks boundaries and set appropriate error codes*/
|
|
|
|
if (args->source+2 > args->sourceLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
if (args->source >= args->sourceLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
/*Either caller has reached the end of the byte stream*/
|
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
/* a character was cut in half*/
|
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
return 0xffff;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
/*Gets the corresponding codepoint*/
|
2000-08-11 02:08:59 +00:00
|
|
|
first = (uint16_t)(((uint16_t)(*(args->source)) << 8) |((uint8_t)*((args->source)+1)));
|
2000-07-21 01:25:30 +00:00
|
|
|
myUChar = first;
|
|
|
|
args->source += 2;
|
2000-04-13 17:27:35 +00:00
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
if(UTF_IS_FIRST_SURROGATE(first)) {
|
|
|
|
uint16_t second;
|
2000-04-13 17:27:35 +00:00
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
if (args->source+2 > args->sourceLimit) {
|
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
|
|
|
return 0xffff;
|
|
|
|
}
|
2000-04-13 17:27:35 +00:00
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
/* get the second surrogate and assemble the code point */
|
2000-08-11 02:08:59 +00:00
|
|
|
second = (uint16_t)(((uint16_t)(*(args->source)) << 8) |((uint8_t)*(args->source+1)));
|
2000-04-13 17:27:35 +00:00
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
/* ignore unmatched surrogates and just deliver the first one in such a case */
|
|
|
|
if(UTF_IS_SECOND_SURROGATE(second)) {
|
|
|
|
/* matched pair, get pair value */
|
|
|
|
myUChar = UTF16_GET_PAIR_VALUE(first, second);
|
|
|
|
args->source += 2;
|
|
|
|
}
|
2000-04-13 17:27:35 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
|
|
|
|
return myUChar;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2000-02-09 19:15:17 +00:00
|
|
|
static const UConverterImpl _UTF16BEImpl={
|
2000-02-05 00:19:15 +00:00
|
|
|
UCNV_UTF16_BigEndian,
|
|
|
|
|
2000-02-08 23:41:16 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
2000-02-05 00:19:15 +00:00
|
|
|
T_UConverter_toUnicode_UTF16_BE,
|
|
|
|
NULL,
|
|
|
|
T_UConverter_fromUnicode_UTF16_BE,
|
|
|
|
NULL,
|
2000-02-09 19:15:17 +00:00
|
|
|
T_UConverter_getNextUChar_UTF16_BE,
|
|
|
|
|
2000-11-27 17:40:41 +00:00
|
|
|
NULL,
|
2000-02-09 19:15:17 +00:00
|
|
|
NULL
|
2000-02-05 00:19:15 +00:00
|
|
|
};
|
|
|
|
|
2000-08-01 17:58:43 +00:00
|
|
|
/* Todo: verify that UTF-16BE == (ccsid (ibm-codepage) 1200) for unicode version 2.0 and 3.0 */
|
2000-04-19 23:05:27 +00:00
|
|
|
const UConverterStaticData _UTF16BEStaticData={
|
|
|
|
sizeof(UConverterStaticData),
|
|
|
|
"UTF16_BigEndian",
|
|
|
|
1200, UCNV_IBM, UCNV_UTF16_BigEndian, 2, 2,
|
2000-10-03 20:18:22 +00:00
|
|
|
{ 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}
|
2000-04-19 23:05:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-04-13 23:00:43 +00:00
|
|
|
const UConverterSharedData _UTF16BEData={
|
2000-03-29 21:41:11 +00:00
|
|
|
sizeof(UConverterSharedData), ~((uint32_t) 0),
|
2000-04-19 23:05:27 +00:00
|
|
|
NULL, NULL, &_UTF16BEStaticData, FALSE, &_UTF16BEImpl,
|
|
|
|
0
|
2000-02-05 00:19:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* UTF-16LE ----------------------------------------------------------------- */
|
|
|
|
|
2000-07-17 23:36:52 +00:00
|
|
|
U_CFUNC void T_UConverter_toUnicode_UTF16_LE (UConverterToUnicodeArgs * args,
|
2000-02-05 00:19:15 +00:00
|
|
|
UErrorCode * err)
|
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
const unsigned char *mySource = (unsigned char *) args->source;
|
|
|
|
UChar *myTarget = args->target;
|
|
|
|
int32_t mySourceIndex = 0;
|
|
|
|
int32_t myTargetIndex = 0;
|
|
|
|
int32_t targetLength = args->targetLimit - myTarget;
|
|
|
|
int32_t sourceLength = args->sourceLimit - (char *) mySource;
|
|
|
|
UChar mySourceChar = 0x0000;
|
|
|
|
|
|
|
|
while (mySourceIndex < sourceLength)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
if (myTargetIndex < targetLength)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
/*gets the corresponding UniChar */
|
|
|
|
mySourceChar = (unsigned char) mySource[mySourceIndex++];
|
2000-02-05 00:19:15 +00:00
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
if (args->converter->toUnicodeStatus == 0x00)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
args->converter->toUnicodeStatus = (unsigned char) mySourceChar == 0x00 ? 0xFFFF : mySourceChar;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
if (args->converter->toUnicodeStatus == 0xFFFF) {
|
|
|
|
mySourceChar = (UChar) (mySourceChar << 8);
|
|
|
|
}
|
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
mySourceChar <<= 8;
|
|
|
|
mySourceChar |= (UChar) (args->converter->toUnicodeStatus);
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
args->converter->toUnicodeStatus = 0x00;
|
|
|
|
myTarget[myTargetIndex++] = mySourceChar;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-07-21 01:25:30 +00:00
|
|
|
break;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
if (U_SUCCESS(*err) && args->flush
|
2000-02-05 00:19:15 +00:00
|
|
|
&& (mySourceIndex == sourceLength)
|
2000-07-10 20:51:54 +00:00
|
|
|
&& (args->converter->toUnicodeStatus != 0x00))
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
if (U_SUCCESS(*err))
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
2000-07-10 20:51:54 +00:00
|
|
|
args->converter->toUnicodeStatus = 0x00;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
|
|
|
|
args->target += myTargetIndex;
|
|
|
|
args->source += mySourceIndex;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2000-11-16 17:20:03 +00:00
|
|
|
U_CFUNC void T_UConverter_fromUnicode_UTF16_LE (UConverterFromUnicodeArgs * args,
|
2000-02-05 00:19:15 +00:00
|
|
|
UErrorCode * err)
|
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
const UChar *mySource = args->source;
|
|
|
|
unsigned char *myTarget = (unsigned char *) args->target;
|
|
|
|
int32_t mySourceIndex = 0;
|
|
|
|
int32_t myTargetIndex = 0;
|
|
|
|
int32_t targetLength = args->targetLimit - (char *) myTarget;
|
|
|
|
int32_t sourceLength = args->sourceLimit - mySource;
|
|
|
|
UChar mySourceChar;
|
|
|
|
|
|
|
|
/*writing the char to the output stream */
|
|
|
|
while (mySourceIndex < sourceLength)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
if (myTargetIndex < targetLength)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
mySourceChar = (UChar) mySource[mySourceIndex++];
|
|
|
|
myTarget[myTargetIndex++] = (char) mySourceChar;
|
|
|
|
if (myTargetIndex < targetLength)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
myTarget[myTargetIndex++] = (char) (mySourceChar >> 8);
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
args->converter->charErrorBuffer[0] = (char) (mySourceChar >> 8);
|
|
|
|
args->converter->charErrorBufferLength = 1;
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-08-11 19:23:50 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
2000-07-21 01:25:30 +00:00
|
|
|
break;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
args->target += myTargetIndex;
|
|
|
|
args->source += mySourceIndex;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2000-07-19 00:21:29 +00:00
|
|
|
U_CFUNC UChar32 T_UConverter_getNextUChar_UTF16_LE(UConverterToUnicodeArgs* args,
|
2000-02-05 00:19:15 +00:00
|
|
|
UErrorCode* err)
|
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
UChar32 myUChar;
|
|
|
|
uint16_t first;
|
|
|
|
/*Checks boundaries and set appropriate error codes*/
|
|
|
|
if (args->source+2 > args->sourceLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
if (args->source >= args->sourceLimit)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
/*Either caller has reached the end of the byte stream*/
|
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
else
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
2000-07-21 01:25:30 +00:00
|
|
|
/* a character was cut in half*/
|
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
|
|
|
|
return 0xffff;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
/*Gets the corresponding codepoint*/
|
2000-08-11 02:08:59 +00:00
|
|
|
first = (uint16_t)(((uint16_t)*((args->source)+1) << 8) | ((uint8_t)(*(args->source))));
|
2000-07-21 01:25:30 +00:00
|
|
|
myUChar=first;
|
|
|
|
/*updates the source*/
|
|
|
|
args->source += 2;
|
2000-04-13 17:27:35 +00:00
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
if (UTF_IS_FIRST_SURROGATE(first))
|
|
|
|
{
|
|
|
|
uint16_t second;
|
2000-04-13 17:27:35 +00:00
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
if (args->source+2 > args->sourceLimit)
|
|
|
|
{
|
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
|
|
|
return 0xffff;
|
|
|
|
}
|
2000-04-13 17:27:35 +00:00
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
/* get the second surrogate and assemble the code point */
|
2000-08-11 02:08:59 +00:00
|
|
|
second = (uint16_t)(((uint16_t)*(args->source+1) << 8) |((uint8_t)(*(args->source))));
|
2000-04-13 17:27:35 +00:00
|
|
|
|
2000-07-21 01:25:30 +00:00
|
|
|
/* ignore unmatched surrogates and just deliver the first one in such a case */
|
|
|
|
if(UTF_IS_SECOND_SURROGATE(second))
|
|
|
|
{
|
|
|
|
/* matched pair, get pair value */
|
|
|
|
myUChar = UTF16_GET_PAIR_VALUE(first, second);
|
|
|
|
args->source += 2;
|
|
|
|
}
|
2000-04-13 17:27:35 +00:00
|
|
|
}
|
2000-07-21 01:25:30 +00:00
|
|
|
|
|
|
|
return myUChar;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2000-02-09 19:15:17 +00:00
|
|
|
static const UConverterImpl _UTF16LEImpl={
|
2000-02-05 00:19:15 +00:00
|
|
|
UCNV_UTF16_LittleEndian,
|
|
|
|
|
2000-02-08 23:41:16 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
2000-02-05 00:19:15 +00:00
|
|
|
T_UConverter_toUnicode_UTF16_LE,
|
|
|
|
NULL,
|
|
|
|
T_UConverter_fromUnicode_UTF16_LE,
|
|
|
|
NULL,
|
2000-02-09 19:15:17 +00:00
|
|
|
T_UConverter_getNextUChar_UTF16_LE,
|
|
|
|
|
2000-11-27 17:40:41 +00:00
|
|
|
NULL,
|
2000-02-09 19:15:17 +00:00
|
|
|
NULL
|
2000-02-05 00:19:15 +00:00
|
|
|
};
|
|
|
|
|
2000-04-19 23:05:27 +00:00
|
|
|
|
2000-08-01 17:58:43 +00:00
|
|
|
/* Todo: verify that UTF-16LE == (ccsid (ibm-codepage) 1200) for unicode version 2.0 and 3.0 */
|
2000-04-19 23:05:27 +00:00
|
|
|
const UConverterStaticData _UTF16LEStaticData={
|
2000-07-21 01:25:30 +00:00
|
|
|
sizeof(UConverterStaticData),
|
|
|
|
"UTF16_LittleEndian",
|
|
|
|
1200, UCNV_IBM, UCNV_UTF16_LittleEndian, 2, 2,
|
2000-10-03 20:18:22 +00:00
|
|
|
{ 0xfd, 0xff, 0, 0 },2,0,0,{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
|
2000-04-19 23:05:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-04-13 23:00:43 +00:00
|
|
|
const UConverterSharedData _UTF16LEData={
|
2000-03-29 21:41:11 +00:00
|
|
|
sizeof(UConverterSharedData), ~((uint32_t) 0),
|
2000-04-19 23:05:27 +00:00
|
|
|
NULL, NULL, &_UTF16LEStaticData, FALSE, &_UTF16LEImpl,
|
|
|
|
0
|
2000-02-05 00:19:15 +00:00
|
|
|
};
|
2000-11-16 17:20:03 +00:00
|
|
|
|
|
|
|
/* UTF-32BE ----------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void T_UConverter_toUnicode_UTF32_BE(UConverterToUnicodeArgs * args,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
|
|
|
const unsigned char *mySource = (unsigned char *) args->source;
|
|
|
|
UChar *myTarget = args->target;
|
|
|
|
const unsigned char *sourceLimit = (unsigned char *) args->sourceLimit;
|
|
|
|
const UChar *targetLimit = args->targetLimit;
|
|
|
|
unsigned char *toUBytes = args->converter->toUBytes;
|
|
|
|
uint32_t ch, i;
|
|
|
|
|
|
|
|
/* UTF-8 returns here for only non-offset, this needs to change.*/
|
|
|
|
if (args->converter->toUnicodeStatus && myTarget < targetLimit)
|
|
|
|
{
|
|
|
|
i = args->converter->toULength; /* restore # of bytes consumed */
|
|
|
|
|
|
|
|
ch = args->converter->toUnicodeStatus - 1;/*Stores the previously calculated ch from a previous call*/
|
|
|
|
args->converter->toUnicodeStatus = 0;
|
|
|
|
goto morebytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mySource < sourceLimit && myTarget < targetLimit)
|
|
|
|
{
|
|
|
|
i = 0;
|
|
|
|
ch = 0;
|
|
|
|
morebytes:
|
|
|
|
while (i < sizeof(uint32_t))
|
|
|
|
{
|
|
|
|
if (mySource < sourceLimit)
|
|
|
|
{
|
|
|
|
ch = (ch << 8) | (uint8_t)(*mySource);
|
|
|
|
toUBytes[i++] = (char) *(mySource++);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (args->flush)
|
|
|
|
{
|
|
|
|
if (U_SUCCESS(*err))
|
|
|
|
{
|
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
|
|
|
args->converter->toUnicodeStatus = MAXIMUM_UCS4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* stores a partially calculated target*/
|
|
|
|
/* + 1 to make 0 a valid character */
|
|
|
|
args->converter->toUnicodeStatus = ch + 1;
|
|
|
|
args->converter->toULength = (int8_t) i;
|
|
|
|
}
|
|
|
|
goto donefornow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch <= MAXIMUM_UTF)
|
|
|
|
{
|
|
|
|
/* Normal valid byte when the loop has not prematurely terminated (i < inBytes) */
|
|
|
|
if (ch <= MAXIMUM_UCS2)
|
|
|
|
{
|
|
|
|
/* fits in 16 bits */
|
|
|
|
*(myTarget++) = (UChar) ch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* write out the surrogates */
|
|
|
|
ch -= HALF_BASE;
|
|
|
|
*(myTarget++) = (UChar) ((ch >> HALF_SHIFT) + SURROGATE_HIGH_START);
|
|
|
|
ch = (ch & HALF_MASK) + SURROGATE_LOW_START;
|
|
|
|
if (myTarget < targetLimit)
|
|
|
|
{
|
|
|
|
*(myTarget++) = (UChar)ch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Put in overflow buffer (not handled here) */
|
|
|
|
args->converter->UCharErrorBuffer[0] = (UChar) ch;
|
|
|
|
args->converter->UCharErrorBufferLength = 1;
|
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
args->source = (const char *) mySource;
|
|
|
|
args->target = myTarget;
|
|
|
|
args->converter->invalidCharLength = (int8_t)i;
|
|
|
|
if (T_UConverter_toUnicode_InvalidChar_Callback(args, err))
|
|
|
|
{
|
|
|
|
/* Stop if the error wasn't handled */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
args->converter->invalidCharLength = 0;
|
|
|
|
mySource = (unsigned char *) args->source;
|
|
|
|
myTarget = args->target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
donefornow:
|
|
|
|
if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err))
|
|
|
|
{
|
|
|
|
/* End of target buffer */
|
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
args->target = myTarget;
|
|
|
|
args->source = (const char *) mySource;
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_UConverter_fromUnicode_UTF32_BE(UConverterFromUnicodeArgs * args,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
|
|
|
const UChar *mySource = args->source;
|
|
|
|
unsigned char *myTarget = (unsigned char *) args->target;
|
|
|
|
const UChar *sourceLimit = args->sourceLimit;
|
|
|
|
const unsigned char *targetLimit = (unsigned char *) args->targetLimit;
|
|
|
|
UChar32 ch, ch2;
|
|
|
|
unsigned int indexToWrite;
|
|
|
|
unsigned char temp[sizeof(uint32_t)];
|
|
|
|
|
|
|
|
temp[0] = 0;
|
|
|
|
|
|
|
|
if (args->converter->fromUnicodeStatus)
|
|
|
|
{
|
|
|
|
ch = args->converter->fromUnicodeStatus;
|
|
|
|
args->converter->fromUnicodeStatus = 0;
|
|
|
|
goto lowsurogate;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mySource < sourceLimit && myTarget < targetLimit)
|
|
|
|
{
|
|
|
|
ch = *(mySource++);
|
|
|
|
|
|
|
|
if (SURROGATE_HIGH_START <= ch && ch < SURROGATE_LOW_START)
|
|
|
|
{
|
|
|
|
lowsurogate:
|
|
|
|
if (mySource < sourceLimit)
|
|
|
|
{
|
|
|
|
ch2 = *mySource;
|
|
|
|
if (SURROGATE_LOW_START <= ch2 && ch2 <= SURROGATE_LOW_END)
|
|
|
|
{
|
|
|
|
ch = ((ch - SURROGATE_HIGH_START) << HALF_SHIFT) + ch2 + SURROGATE_LOW_BASE;
|
|
|
|
mySource++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!args->flush)
|
|
|
|
{
|
2000-11-20 18:09:58 +00:00
|
|
|
/* ran out of source */
|
2000-11-16 17:20:03 +00:00
|
|
|
args->converter->fromUnicodeStatus = ch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We cannot get any larger than 10FFFF because we are coming from UTF-16 */
|
|
|
|
/* Todo: Can the & part be left off implicitly? Does it really save time? */
|
|
|
|
temp[1] = (uint8_t) (ch >> 16 & 0x1F);
|
|
|
|
temp[2] = (uint8_t) (ch >> 8 & 0xFF);
|
|
|
|
temp[3] = (uint8_t) (ch & 0xFF);
|
|
|
|
|
|
|
|
for (indexToWrite = 0; indexToWrite <= sizeof(uint32_t) - 1; indexToWrite++)
|
|
|
|
{
|
|
|
|
if (myTarget < targetLimit)
|
|
|
|
{
|
|
|
|
*(myTarget++) = temp[indexToWrite];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
args->converter->charErrorBuffer[args->converter->charErrorBufferLength++] = temp[indexToWrite];
|
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR; /* Todo: is this needed because of ending if */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err))
|
|
|
|
{
|
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
args->target = (char *) myTarget;
|
|
|
|
args->source = mySource;
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar32 T_UConverter_getNextUChar_UTF32_BE(UConverterToUnicodeArgs* args,
|
|
|
|
UErrorCode* err)
|
|
|
|
{
|
|
|
|
*err = U_UNSUPPORTED_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2000-11-16 18:21:01 +00:00
|
|
|
|
2000-11-16 17:20:03 +00:00
|
|
|
static const UConverterImpl _UTF32BEImpl = {
|
|
|
|
UCNV_UTF32_BigEndian,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
|
|
|
T_UConverter_toUnicode_UTF32_BE,
|
|
|
|
NULL,
|
2000-11-20 18:09:58 +00:00
|
|
|
/* T_UConverter_toUnicode_UTF32_BE_OFFSETS_LOGIC, */
|
2000-11-16 17:20:03 +00:00
|
|
|
T_UConverter_fromUnicode_UTF32_BE,
|
|
|
|
NULL,
|
2000-11-20 18:09:58 +00:00
|
|
|
/* T_UConverter_fromUnicode_UTF32_BE_OFFSETS_LOGIC, */
|
2000-11-16 18:21:01 +00:00
|
|
|
T_UConverter_getNextUChar_UTF32_BE,
|
2000-11-16 17:20:03 +00:00
|
|
|
|
2000-11-27 17:40:41 +00:00
|
|
|
NULL,
|
2000-11-16 17:20:03 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Todo: These numbers are probably in correct. */
|
|
|
|
const UConverterStaticData _UTF32BEStaticData = {
|
|
|
|
sizeof(UConverterStaticData),
|
|
|
|
"UTF32_BigEndian",
|
|
|
|
1200, UCNV_IBM, UCNV_UTF32_BigEndian, 4, 4,
|
|
|
|
{ 0, 0, 0xff, 0xfd }, 4, 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 _UTF32BEData = {
|
|
|
|
sizeof(UConverterSharedData), ~((uint32_t) 0),
|
|
|
|
NULL, NULL, &_UTF32BEStaticData, FALSE, &_UTF32BEImpl,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
/* UTF-32LE ---------------------------------------------------------- */
|
|
|
|
|
|
|
|
void T_UConverter_toUnicode_UTF32_LE(UConverterToUnicodeArgs * args,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
|
|
|
const unsigned char *mySource = (unsigned char *) args->source;
|
|
|
|
UChar *myTarget = args->target;
|
|
|
|
const unsigned char *sourceLimit = (unsigned char *) args->sourceLimit;
|
|
|
|
const UChar *targetLimit = args->targetLimit;
|
|
|
|
unsigned char *toUBytes = args->converter->toUBytes;
|
|
|
|
uint32_t ch, i;
|
|
|
|
|
|
|
|
/* UTF-8 returns here for only non-offset, this needs to change.*/
|
|
|
|
if (args->converter->toUnicodeStatus && myTarget < targetLimit)
|
|
|
|
{
|
|
|
|
i = args->converter->toULength; /* restore # of bytes consumed */
|
|
|
|
|
2000-11-20 18:09:58 +00:00
|
|
|
/* Stores the previously calculated ch from a previous call*/
|
|
|
|
ch = args->converter->toUnicodeStatus - 1;
|
2000-11-16 17:20:03 +00:00
|
|
|
args->converter->toUnicodeStatus = 0;
|
|
|
|
goto morebytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mySource < sourceLimit && myTarget < targetLimit)
|
|
|
|
{
|
|
|
|
i = 0;
|
|
|
|
ch = 0;
|
|
|
|
morebytes:
|
|
|
|
while (i < sizeof(uint32_t))
|
|
|
|
{
|
|
|
|
if (mySource < sourceLimit)
|
|
|
|
{
|
|
|
|
ch |= ((uint8_t)(*mySource)) << (i * 8);
|
|
|
|
toUBytes[i++] = (char) *(mySource++);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (args->flush)
|
|
|
|
{
|
|
|
|
if (U_SUCCESS(*err))
|
|
|
|
{
|
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
|
|
|
args->converter->toUnicodeStatus = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2000-11-20 18:09:58 +00:00
|
|
|
{ /* stores a partially calculated target*/
|
|
|
|
/* + 1 to make 0 a valid character */
|
|
|
|
args->converter->toUnicodeStatus = ch + 1;
|
2000-11-16 17:20:03 +00:00
|
|
|
args->converter->toULength = (int8_t) i;
|
|
|
|
}
|
|
|
|
goto donefornow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch <= MAXIMUM_UTF)
|
|
|
|
{
|
|
|
|
/* Normal valid byte when the loop has not prematurely terminated (i < inBytes) */
|
|
|
|
if (ch <= MAXIMUM_UCS2)
|
|
|
|
{
|
|
|
|
/* fits in 16 bits */
|
|
|
|
*(myTarget++) = (UChar) ch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* write out the surrogates */
|
|
|
|
ch -= HALF_BASE;
|
|
|
|
*(myTarget++) = (UChar) ((ch >> HALF_SHIFT) + SURROGATE_HIGH_START);
|
|
|
|
ch = (ch & HALF_MASK) + SURROGATE_LOW_START;
|
|
|
|
if (myTarget < targetLimit)
|
|
|
|
{
|
|
|
|
*(myTarget++) = (UChar)ch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Put in overflow buffer (not handled here) */
|
|
|
|
args->converter->UCharErrorBuffer[0] = (UChar) ch;
|
|
|
|
args->converter->UCharErrorBufferLength = 1;
|
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
args->source = (const char *) mySource;
|
|
|
|
args->target = myTarget;
|
|
|
|
args->converter->invalidCharLength = (int8_t)i;
|
|
|
|
if (T_UConverter_toUnicode_InvalidChar_Callback(args, err))
|
|
|
|
{
|
|
|
|
/* Stop if the error wasn't handled */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
args->converter->invalidCharLength = 0;
|
|
|
|
mySource = (unsigned char *) args->source;
|
|
|
|
myTarget = args->target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
donefornow:
|
|
|
|
if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err))
|
|
|
|
{
|
|
|
|
/* End of target buffer */
|
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
args->target = myTarget;
|
|
|
|
args->source = (const char *) mySource;
|
|
|
|
}
|
|
|
|
|
|
|
|
void T_UConverter_fromUnicode_UTF32_LE(UConverterFromUnicodeArgs * args,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
|
|
|
const UChar *mySource = args->source;
|
|
|
|
unsigned char *myTarget = (unsigned char *) args->target;
|
|
|
|
const UChar *sourceLimit = args->sourceLimit;
|
|
|
|
const unsigned char *targetLimit = (unsigned char *) args->targetLimit;
|
|
|
|
UChar32 ch, ch2;
|
|
|
|
unsigned int indexToWrite;
|
|
|
|
unsigned char temp[sizeof(uint32_t)];
|
|
|
|
|
|
|
|
temp[3] = 0;
|
|
|
|
|
|
|
|
if (args->converter->fromUnicodeStatus)
|
|
|
|
{
|
|
|
|
ch = args->converter->fromUnicodeStatus;
|
|
|
|
args->converter->fromUnicodeStatus = 0;
|
|
|
|
goto lowsurogate;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mySource < sourceLimit && myTarget < targetLimit)
|
|
|
|
{
|
|
|
|
ch = *(mySource++);
|
|
|
|
|
|
|
|
if (SURROGATE_HIGH_START <= ch && ch < SURROGATE_LOW_START)
|
|
|
|
{
|
|
|
|
lowsurogate:
|
|
|
|
if (mySource < sourceLimit)
|
|
|
|
{
|
|
|
|
ch2 = *mySource;
|
|
|
|
if (SURROGATE_LOW_START <= ch2 && ch2 <= SURROGATE_LOW_END)
|
|
|
|
{
|
|
|
|
ch = ((ch - SURROGATE_HIGH_START) << HALF_SHIFT) + ch2 + SURROGATE_LOW_BASE;
|
|
|
|
mySource++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!args->flush)
|
|
|
|
{
|
2000-11-20 18:09:58 +00:00
|
|
|
/* ran out of source */
|
2000-11-16 17:20:03 +00:00
|
|
|
args->converter->fromUnicodeStatus = ch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We cannot get any larger than 10FFFF because we are coming from UTF-16 */
|
|
|
|
/* Todo: Can the & part be left off implicitly? Does it really save time? */
|
|
|
|
temp[2] = (uint8_t) (ch >> 16 & 0x1F);
|
|
|
|
temp[1] = (uint8_t) (ch >> 8 & 0xFF);
|
|
|
|
temp[0] = (uint8_t) (ch & 0xFF);
|
|
|
|
|
|
|
|
for (indexToWrite = 0; indexToWrite <= sizeof(uint32_t) - 1; indexToWrite++)
|
|
|
|
{
|
|
|
|
if (myTarget < targetLimit)
|
|
|
|
{
|
|
|
|
*(myTarget++) = temp[indexToWrite];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
args->converter->charErrorBuffer[args->converter->charErrorBufferLength++] = temp[indexToWrite];
|
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR; /* Todo: is this needed because of ending if */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err))
|
|
|
|
{
|
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
args->target = (char *) myTarget;
|
|
|
|
args->source = mySource;
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar32 T_UConverter_getNextUChar_UTF32_LE(UConverterToUnicodeArgs* args,
|
|
|
|
UErrorCode* err)
|
|
|
|
{
|
|
|
|
*err = U_UNSUPPORTED_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const UConverterImpl _UTF32LEImpl = {
|
|
|
|
UCNV_UTF32_LittleEndian,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
|
|
|
T_UConverter_toUnicode_UTF32_LE,
|
|
|
|
NULL,
|
2000-11-20 18:09:58 +00:00
|
|
|
/* T_UConverter_toUnicode_UTF32_LE_OFFSETS_LOGIC, */
|
2000-11-16 17:20:03 +00:00
|
|
|
T_UConverter_fromUnicode_UTF32_LE,
|
|
|
|
NULL,
|
2000-11-20 18:09:58 +00:00
|
|
|
/* T_UConverter_fromUnicode_UTF32_LE_OFFSETS_LOGIC, */
|
2000-11-16 18:21:01 +00:00
|
|
|
T_UConverter_getNextUChar_UTF32_LE,
|
2000-11-16 17:20:03 +00:00
|
|
|
|
2000-11-27 17:40:41 +00:00
|
|
|
NULL,
|
2000-11-16 17:20:03 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Todo: These numbers are probably in correct. */
|
|
|
|
const UConverterStaticData _UTF32LEStaticData = {
|
|
|
|
sizeof(UConverterStaticData),
|
|
|
|
"UTF32_LittleEndian",
|
|
|
|
1200, UCNV_IBM, UCNV_UTF32_BigEndian, 4, 4,
|
|
|
|
{ 0xfd, 0xff, 0, 0 }, 4, 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 _UTF32LEData = {
|
|
|
|
sizeof(UConverterSharedData), ~((uint32_t) 0),
|
|
|
|
NULL, NULL, &_UTF32LEStaticData, FALSE, &_UTF32LEImpl,
|
|
|
|
0
|
|
|
|
};
|