2000-02-05 00:19:15 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
|
|
|
* Copyright (C) 2000, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
2000-02-07 17:35:21 +00:00
|
|
|
* file name: ucnvmbcs.cpp
|
2000-02-05 00:19:15 +00:00
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 2000feb03
|
|
|
|
* created by: Markus W. Scherer
|
2000-05-12 19:59:03 +00:00
|
|
|
*
|
|
|
|
* Change history:
|
|
|
|
*
|
|
|
|
* 05/09/00 helena Added implementation to handle fallback mappings.
|
2000-02-05 00:19:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2000-02-08 23:41:16 +00:00
|
|
|
#include "cmemory.h"
|
2000-02-05 00:19:15 +00:00
|
|
|
#include "ucmp16.h"
|
|
|
|
#include "ucmp8.h"
|
|
|
|
#include "unicode/ucnv_bld.h"
|
|
|
|
#include "unicode/ucnv.h"
|
|
|
|
#include "ucnv_cnv.h"
|
|
|
|
|
|
|
|
/* MBCS --------------------------------------------------------------------- */
|
|
|
|
|
2000-02-08 23:41:16 +00:00
|
|
|
static void
|
|
|
|
_MBCSLoad(UConverterSharedData *sharedData, const uint8_t *raw, UErrorCode *pErrorCode) {
|
|
|
|
const uint8_t *oldraw;
|
|
|
|
|
|
|
|
sharedData->table->mbcs.starters = (bool_t*)raw;
|
|
|
|
oldraw = raw += sizeof(bool_t)*256;
|
|
|
|
|
2000-04-19 23:05:27 +00:00
|
|
|
ucmp16_initFromData(&sharedData->table->mbcs.toUnicode, &raw, pErrorCode);
|
2000-02-08 23:41:16 +00:00
|
|
|
if(((raw-oldraw)&3)!=0) {
|
|
|
|
raw+=4-((raw-oldraw)&3); /* pad to 4 */
|
|
|
|
}
|
2000-05-12 19:59:03 +00:00
|
|
|
oldraw = raw;
|
2000-04-19 23:05:27 +00:00
|
|
|
ucmp16_initFromData(&sharedData->table->mbcs.fromUnicode, &raw, pErrorCode);
|
2000-05-12 19:59:03 +00:00
|
|
|
if (sharedData->staticData->hasFromUnicodeFallback == TRUE)
|
|
|
|
{
|
|
|
|
if(((raw-oldraw)&3)!=0) {
|
|
|
|
raw+=4-((raw-oldraw)&3); /* pad to 4 */
|
|
|
|
}
|
|
|
|
oldraw = raw;
|
|
|
|
ucmp16_initFromData(&sharedData->table->mbcs.fromUnicodeFallback, &raw, pErrorCode);
|
|
|
|
}
|
|
|
|
if (sharedData->staticData->hasToUnicodeFallback == TRUE)
|
|
|
|
{
|
|
|
|
if(((raw-oldraw)&3)!=0) {
|
|
|
|
raw+=4-((raw-oldraw)&3); /* pad to 4 */
|
|
|
|
}
|
|
|
|
ucmp16_initFromData(&sharedData->table->mbcs.toUnicodeFallback, &raw, pErrorCode);
|
|
|
|
}
|
2000-02-08 23:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_MBCSUnload(UConverterSharedData *sharedData) {
|
2000-04-19 23:05:27 +00:00
|
|
|
ucmp16_close (&sharedData->table->mbcs.fromUnicode);
|
|
|
|
ucmp16_close (&sharedData->table->mbcs.toUnicode);
|
2000-05-12 19:59:03 +00:00
|
|
|
if (sharedData->staticData->hasFromUnicodeFallback == TRUE)
|
|
|
|
ucmp16_close (&sharedData->table->mbcs.fromUnicodeFallback);
|
|
|
|
if (sharedData->staticData->hasToUnicodeFallback == TRUE)
|
|
|
|
ucmp16_close (&sharedData->table->mbcs.toUnicodeFallback);
|
2000-02-08 23:41:16 +00:00
|
|
|
uprv_free (sharedData->table);
|
|
|
|
}
|
|
|
|
|
2000-04-13 23:00:43 +00:00
|
|
|
static void T_UConverter_toUnicode_MBCS (UConverter * _this,
|
2000-02-05 00:19:15 +00:00
|
|
|
UChar ** target,
|
|
|
|
const UChar * targetLimit,
|
|
|
|
const char **source,
|
|
|
|
const char *sourceLimit,
|
|
|
|
int32_t *offsets,
|
|
|
|
bool_t flush,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
|
|
|
const char *mySource = *source;
|
|
|
|
UChar *myTarget = *target;
|
|
|
|
int32_t mySourceIndex = 0;
|
|
|
|
int32_t myTargetIndex = 0;
|
|
|
|
int32_t targetLength = targetLimit - myTarget;
|
|
|
|
int32_t sourceLength = sourceLimit - mySource;
|
2000-05-12 19:59:03 +00:00
|
|
|
CompactShortArray *myToUnicode = NULL, *myToUnicodeFallback = NULL;
|
2000-02-05 00:19:15 +00:00
|
|
|
UChar targetUniChar = 0x0000;
|
|
|
|
UChar mySourceChar = 0x0000;
|
|
|
|
bool_t *myStarters = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-04-19 23:05:27 +00:00
|
|
|
myToUnicode = &_this->sharedData->table->mbcs.toUnicode;
|
2000-05-12 19:59:03 +00:00
|
|
|
myToUnicodeFallback = &_this->sharedData->table->mbcs.toUnicodeFallback;
|
2000-02-05 00:19:15 +00:00
|
|
|
myStarters = _this->sharedData->table->mbcs.starters;
|
|
|
|
|
|
|
|
while (mySourceIndex < sourceLength)
|
|
|
|
{
|
|
|
|
if (myTargetIndex < targetLength)
|
|
|
|
{
|
|
|
|
/*gets the corresponding UniChar */
|
|
|
|
mySourceChar = (unsigned char) (mySource[mySourceIndex++]);
|
|
|
|
|
|
|
|
|
|
|
|
if (myStarters[(uint8_t) mySourceChar] &&
|
|
|
|
(_this->toUnicodeStatus == 0x00))
|
|
|
|
{
|
|
|
|
_this->toUnicodeStatus = (unsigned char) mySourceChar;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*In case there is a state, we update the source char
|
|
|
|
*by concatenating the previous char with the current
|
|
|
|
*one
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (_this->toUnicodeStatus != 0x00)
|
|
|
|
{
|
|
|
|
mySourceChar |= (UChar) (_this->toUnicodeStatus << 8);
|
|
|
|
|
|
|
|
_this->toUnicodeStatus = 0x00;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*gets the corresponding Unicode codepoint */
|
|
|
|
targetUniChar = (UChar) ucmp16_getu (myToUnicode, mySourceChar);
|
|
|
|
|
|
|
|
/*writing the UniChar to the output stream */
|
|
|
|
if (targetUniChar != missingUCharMarker)
|
|
|
|
{
|
2000-05-12 19:59:03 +00:00
|
|
|
myTarget[myTargetIndex++] = targetUniChar;
|
2000-02-05 00:19:15 +00:00
|
|
|
}
|
2000-05-12 19:59:03 +00:00
|
|
|
else if ((_this->useFallback == TRUE) &&
|
|
|
|
(_this->sharedData->staticData->hasToUnicodeFallback == TRUE))
|
|
|
|
{
|
|
|
|
targetUniChar = (UChar) ucmp16_getu(myToUnicodeFallback, mySourceChar);
|
|
|
|
if (targetUniChar != missingUCharMarker)
|
|
|
|
{
|
|
|
|
myTarget[myTargetIndex++] = targetUniChar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (targetUniChar == missingUCharMarker)
|
|
|
|
{
|
2000-02-05 00:19:15 +00:00
|
|
|
*err = U_INVALID_CHAR_FOUND;
|
|
|
|
if (mySourceChar > 0xff)
|
|
|
|
{
|
|
|
|
_this->invalidCharLength = 2;
|
|
|
|
_this->invalidCharBuffer[0] = (char) (mySourceChar >> 8);
|
|
|
|
_this->invalidCharBuffer[1] = (char) mySourceChar;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_this->invalidCharLength = 1;
|
|
|
|
_this->invalidCharBuffer[0] = (char) mySourceChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
ToU_CALLBACK_MACRO(_this,
|
|
|
|
myTarget,
|
|
|
|
myTargetIndex,
|
|
|
|
targetLimit,
|
|
|
|
mySource,
|
|
|
|
mySourceIndex,
|
|
|
|
sourceLimit,
|
|
|
|
offsets,
|
|
|
|
flush,
|
|
|
|
err);
|
|
|
|
|
|
|
|
if (U_FAILURE (*err)) break;
|
|
|
|
_this->invalidCharLength = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*If at the end of conversion we are still carrying state information
|
|
|
|
*flush is TRUE, we can deduce that the input stream is truncated
|
|
|
|
*/
|
|
|
|
if (_this->toUnicodeStatus
|
|
|
|
&& (mySourceIndex == sourceLength)
|
|
|
|
&& (flush == TRUE))
|
|
|
|
{
|
|
|
|
if (U_SUCCESS(*err))
|
|
|
|
{
|
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
|
|
|
_this->toUnicodeStatus = 0x00;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*target += myTargetIndex;
|
|
|
|
*source += mySourceIndex;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-04-13 23:00:43 +00:00
|
|
|
static void T_UConverter_toUnicode_MBCS_OFFSETS_LOGIC (UConverter * _this,
|
2000-02-05 00:19:15 +00:00
|
|
|
UChar ** target,
|
|
|
|
const UChar * targetLimit,
|
|
|
|
const char **source,
|
|
|
|
const char *sourceLimit,
|
|
|
|
int32_t *offsets,
|
|
|
|
bool_t flush,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
|
|
|
const char *mySource = *source;
|
|
|
|
UChar *myTarget = *target;
|
|
|
|
int32_t mySourceIndex = 0;
|
|
|
|
int32_t myTargetIndex = 0;
|
|
|
|
int32_t targetLength = targetLimit - myTarget;
|
|
|
|
int32_t sourceLength = sourceLimit - mySource;
|
2000-05-12 19:59:03 +00:00
|
|
|
CompactShortArray *myToUnicode = NULL, *myToUnicodeFallback = NULL;
|
2000-02-05 00:19:15 +00:00
|
|
|
UChar targetUniChar = 0x0000;
|
|
|
|
UChar mySourceChar = 0x0000;
|
2000-04-13 23:00:43 +00:00
|
|
|
UChar oldMySourceChar = 0x0000;
|
2000-02-05 00:19:15 +00:00
|
|
|
bool_t *myStarters = NULL;
|
|
|
|
|
2000-04-19 23:05:27 +00:00
|
|
|
myToUnicode = &_this->sharedData->table->mbcs.toUnicode;
|
2000-05-12 19:59:03 +00:00
|
|
|
myToUnicodeFallback = &_this->sharedData->table->mbcs.toUnicodeFallback;
|
2000-02-05 00:19:15 +00:00
|
|
|
myStarters = _this->sharedData->table->mbcs.starters;
|
|
|
|
|
|
|
|
while (mySourceIndex < sourceLength)
|
|
|
|
{
|
|
|
|
if (myTargetIndex < targetLength)
|
|
|
|
{
|
|
|
|
/*gets the corresponding UniChar */
|
|
|
|
mySourceChar = (unsigned char) (mySource[mySourceIndex++]);
|
|
|
|
|
|
|
|
|
|
|
|
if (myStarters[(uint8_t) mySourceChar] &&
|
|
|
|
(_this->toUnicodeStatus == 0x00))
|
|
|
|
{
|
|
|
|
_this->toUnicodeStatus = (unsigned char) mySourceChar;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*In case there is a state, we update the source char
|
|
|
|
*by concatenating the previous char with the current
|
|
|
|
*one
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (_this->toUnicodeStatus != 0x00)
|
|
|
|
{
|
|
|
|
mySourceChar |= (UChar) (_this->toUnicodeStatus << 8);
|
|
|
|
|
|
|
|
_this->toUnicodeStatus = 0x00;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*gets the corresponding Unicode codepoint */
|
|
|
|
targetUniChar = (UChar) ucmp16_getu (myToUnicode, mySourceChar);
|
|
|
|
|
|
|
|
|
|
|
|
/*writing the UniChar to the output stream */
|
|
|
|
if (targetUniChar != missingUCharMarker)
|
|
|
|
{
|
|
|
|
/*writes the UniChar to the output stream */
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
if (targetUniChar > 0x00FF)
|
|
|
|
offsets[myTargetIndex] = mySourceIndex -2; /* double byte character - make the offset point to the first char */
|
|
|
|
else
|
|
|
|
offsets[myTargetIndex] = mySourceIndex -1 ; /* single byte char. Offset is OK */
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
myTarget[myTargetIndex++] = targetUniChar;
|
|
|
|
oldMySourceChar = mySourceChar;
|
|
|
|
|
|
|
|
}
|
2000-05-12 19:59:03 +00:00
|
|
|
else if ((_this->useFallback == TRUE) &&
|
|
|
|
(_this->sharedData->staticData->hasToUnicodeFallback == TRUE))
|
|
|
|
{
|
|
|
|
|
|
|
|
targetUniChar = (UChar) ucmp16_getu (myToUnicodeFallback, mySourceChar);
|
|
|
|
/*writes the UniChar to the output stream */
|
|
|
|
{
|
|
|
|
if (targetUniChar > 0x00FF)
|
|
|
|
offsets[myTargetIndex] = mySourceIndex -2; /* double byte character - make the offset point to the first char */
|
|
|
|
else
|
|
|
|
offsets[myTargetIndex] = mySourceIndex -1 ; /* single byte char. Offset is OK */
|
|
|
|
|
|
|
|
}
|
|
|
|
myTarget[myTargetIndex++] = targetUniChar;
|
|
|
|
oldMySourceChar = mySourceChar;
|
|
|
|
}
|
|
|
|
if (targetUniChar == missingUCharMarker)
|
|
|
|
{
|
|
|
|
int32_t currentOffset = offsets[myTargetIndex-1] + ((oldMySourceChar>0x00FF)?2:1);
|
2000-02-05 00:19:15 +00:00
|
|
|
|
|
|
|
*err = U_INVALID_CHAR_FOUND;
|
|
|
|
if (mySourceChar > 0xff)
|
|
|
|
{
|
|
|
|
_this->invalidCharLength = 2;
|
|
|
|
_this->invalidCharBuffer[0] = (char) (mySourceChar >> 8);
|
|
|
|
_this->invalidCharBuffer[1] = (char) mySourceChar;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_this->invalidCharLength = 1;
|
|
|
|
_this->invalidCharBuffer[0] = (char) mySourceChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
ToU_CALLBACK_OFFSETS_LOGIC_MACRO(_this,
|
|
|
|
myTarget,
|
|
|
|
myTargetIndex,
|
|
|
|
targetLimit,
|
|
|
|
mySource,
|
|
|
|
mySourceIndex,
|
|
|
|
sourceLimit,
|
|
|
|
offsets,
|
|
|
|
flush,
|
|
|
|
err);
|
|
|
|
|
|
|
|
if (U_FAILURE (*err)) break;
|
|
|
|
_this->invalidCharLength = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*If at the end of conversion we are still carrying state information
|
|
|
|
*flush is TRUE, we can deduce that the input stream is truncated
|
|
|
|
*/
|
|
|
|
if (_this->toUnicodeStatus
|
|
|
|
&& (mySourceIndex == sourceLength)
|
|
|
|
&& (flush == TRUE))
|
|
|
|
{
|
|
|
|
if (U_SUCCESS(*err))
|
|
|
|
{
|
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
|
|
|
_this->toUnicodeStatus = 0x00;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*target += myTargetIndex;
|
|
|
|
*source += mySourceIndex;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-04-13 23:00:43 +00:00
|
|
|
static void T_UConverter_fromUnicode_MBCS (UConverter * _this,
|
2000-02-05 00:19:15 +00:00
|
|
|
char **target,
|
|
|
|
const char *targetLimit,
|
|
|
|
const UChar ** source,
|
|
|
|
const UChar * sourceLimit,
|
|
|
|
int32_t *offsets,
|
|
|
|
bool_t flush,
|
|
|
|
UErrorCode * err)
|
|
|
|
|
|
|
|
{
|
|
|
|
const UChar *mySource = *source;
|
|
|
|
char *myTarget = *target;
|
|
|
|
int32_t mySourceIndex = 0;
|
|
|
|
int32_t myTargetIndex = 0;
|
|
|
|
int32_t targetLength = targetLimit - myTarget;
|
|
|
|
int32_t sourceLength = sourceLimit - mySource;
|
2000-05-12 19:59:03 +00:00
|
|
|
CompactShortArray *myFromUnicode = NULL, *myFromUnicodeFallback = NULL;
|
2000-02-05 00:19:15 +00:00
|
|
|
UChar targetUniChar = 0x0000;
|
|
|
|
UChar mySourceChar = 0x0000;
|
|
|
|
|
2000-04-19 23:05:27 +00:00
|
|
|
myFromUnicode = &_this->sharedData->table->mbcs.fromUnicode;
|
2000-05-12 19:59:03 +00:00
|
|
|
myFromUnicodeFallback = &_this->sharedData->table->mbcs.fromUnicodeFallback;
|
2000-02-05 00:19:15 +00:00
|
|
|
|
|
|
|
/*writing the char to the output stream */
|
|
|
|
while (mySourceIndex < sourceLength)
|
|
|
|
{
|
|
|
|
if (myTargetIndex < targetLength)
|
|
|
|
{
|
|
|
|
mySourceChar = (UChar) mySource[mySourceIndex++];
|
|
|
|
targetUniChar = (UChar) ucmp16_getu (myFromUnicode, mySourceChar);
|
|
|
|
|
|
|
|
if (targetUniChar != missingCharMarker)
|
|
|
|
{
|
|
|
|
if (targetUniChar <= 0x00FF)
|
|
|
|
{
|
|
|
|
myTarget[myTargetIndex++] = (char) targetUniChar;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
myTarget[myTargetIndex++] = (char) (targetUniChar >> 8);
|
|
|
|
if (myTargetIndex < targetLength)
|
|
|
|
{
|
|
|
|
myTarget[myTargetIndex++] = (char) targetUniChar;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_this->charErrorBuffer[0] = (char) targetUniChar;
|
|
|
|
_this->charErrorBufferLength = 1;
|
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2000-05-12 19:59:03 +00:00
|
|
|
}
|
|
|
|
else if ((_this->useFallback == TRUE) &&
|
|
|
|
(_this->sharedData->staticData->hasFromUnicodeFallback == TRUE))
|
|
|
|
{
|
|
|
|
targetUniChar = (UChar) ucmp16_getu (myFromUnicodeFallback, mySourceChar);
|
|
|
|
|
|
|
|
if (targetUniChar != missingCharMarker)
|
|
|
|
{
|
|
|
|
if (targetUniChar <= 0x00FF)
|
|
|
|
{
|
|
|
|
myTarget[myTargetIndex++] = (char) targetUniChar;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
myTarget[myTargetIndex++] = (char) (targetUniChar >> 8);
|
|
|
|
if (myTargetIndex < targetLength)
|
|
|
|
{
|
|
|
|
myTarget[myTargetIndex++] = (char) targetUniChar;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_this->charErrorBuffer[0] = (char) targetUniChar;
|
|
|
|
_this->charErrorBufferLength = 1;
|
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (targetUniChar == missingCharMarker)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
|
|
|
*err = U_INVALID_CHAR_FOUND;
|
|
|
|
_this->invalidUCharBuffer[0] = (UChar) mySourceChar;
|
|
|
|
_this->invalidUCharLength = 1;
|
|
|
|
|
|
|
|
FromU_CALLBACK_MACRO(_this,
|
|
|
|
myTarget,
|
|
|
|
myTargetIndex,
|
|
|
|
targetLimit,
|
|
|
|
mySource,
|
|
|
|
mySourceIndex,
|
|
|
|
sourceLimit,
|
|
|
|
offsets,
|
|
|
|
flush,
|
|
|
|
err);
|
|
|
|
|
|
|
|
if (U_FAILURE (*err)) break;
|
|
|
|
_this->invalidUCharLength = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
*target += myTargetIndex;
|
|
|
|
*source += mySourceIndex;;
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-04-13 23:00:43 +00:00
|
|
|
static void T_UConverter_fromUnicode_MBCS_OFFSETS_LOGIC (UConverter * _this,
|
2000-02-05 00:19:15 +00:00
|
|
|
char **target,
|
|
|
|
const char *targetLimit,
|
|
|
|
const UChar ** source,
|
|
|
|
const UChar * sourceLimit,
|
|
|
|
int32_t *offsets,
|
|
|
|
bool_t flush,
|
|
|
|
UErrorCode * err)
|
|
|
|
|
|
|
|
{
|
|
|
|
const UChar *mySource = *source;
|
|
|
|
char *myTarget = *target;
|
|
|
|
int32_t mySourceIndex = 0;
|
|
|
|
int32_t myTargetIndex = 0;
|
|
|
|
int32_t targetLength = targetLimit - myTarget;
|
|
|
|
int32_t sourceLength = sourceLimit - mySource;
|
2000-05-12 19:59:03 +00:00
|
|
|
CompactShortArray *myFromUnicode = NULL, *myFromUnicodeFallback = NULL;
|
2000-02-05 00:19:15 +00:00
|
|
|
UChar targetUniChar = 0x0000;
|
|
|
|
UChar mySourceChar = 0x0000;
|
|
|
|
|
2000-04-19 23:05:27 +00:00
|
|
|
myFromUnicode = &_this->sharedData->table->mbcs.fromUnicode;
|
2000-05-12 19:59:03 +00:00
|
|
|
myFromUnicodeFallback = &_this->sharedData->table->mbcs.fromUnicodeFallback;
|
2000-02-05 00:19:15 +00:00
|
|
|
|
|
|
|
/*writing the char to the output stream */
|
|
|
|
while (mySourceIndex < sourceLength)
|
|
|
|
{
|
|
|
|
if (myTargetIndex < targetLength)
|
|
|
|
{
|
|
|
|
mySourceChar = (UChar) mySource[mySourceIndex++];
|
|
|
|
targetUniChar = (UChar) ucmp16_getu (myFromUnicode, mySourceChar);
|
|
|
|
|
|
|
|
if (targetUniChar != missingCharMarker)
|
|
|
|
{
|
|
|
|
if (targetUniChar <= 0x00FF)
|
|
|
|
{
|
|
|
|
offsets[myTargetIndex] = mySourceIndex-1;
|
|
|
|
myTarget[myTargetIndex++] = (char) targetUniChar;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
offsets[myTargetIndex] = mySourceIndex-1;
|
|
|
|
myTarget[myTargetIndex++] = (char) (targetUniChar >> 8);
|
|
|
|
if (myTargetIndex < targetLength)
|
|
|
|
{
|
|
|
|
offsets[myTargetIndex] = mySourceIndex-1;
|
|
|
|
myTarget[myTargetIndex++] = (char) targetUniChar;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_this->charErrorBuffer[0] = (char) targetUniChar;
|
|
|
|
_this->charErrorBufferLength = 1;
|
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-05-12 19:59:03 +00:00
|
|
|
else if ((_this->useFallback == TRUE) &&
|
|
|
|
(_this->sharedData->staticData->hasFromUnicodeFallback == TRUE))
|
|
|
|
{
|
|
|
|
targetUniChar = (UChar) ucmp16_getu (myFromUnicodeFallback, mySourceChar);
|
|
|
|
if (targetUniChar != missingCharMarker)
|
|
|
|
{
|
|
|
|
if (targetUniChar <= 0x00FF)
|
|
|
|
{
|
|
|
|
offsets[myTargetIndex] = mySourceIndex-1;
|
|
|
|
myTarget[myTargetIndex++] = (char) targetUniChar;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
offsets[myTargetIndex] = mySourceIndex-1;
|
|
|
|
myTarget[myTargetIndex++] = (char) (targetUniChar >> 8);
|
|
|
|
if (myTargetIndex < targetLength)
|
|
|
|
{
|
|
|
|
offsets[myTargetIndex] = mySourceIndex-1;
|
|
|
|
myTarget[myTargetIndex++] = (char) targetUniChar;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_this->charErrorBuffer[0] = (char) targetUniChar;
|
|
|
|
_this->charErrorBufferLength = 1;
|
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetUniChar == missingCharMarker)
|
2000-02-05 00:19:15 +00:00
|
|
|
{
|
|
|
|
int32_t currentOffset = mySourceIndex -1;
|
|
|
|
|
|
|
|
*err = U_INVALID_CHAR_FOUND;
|
|
|
|
_this->invalidUCharBuffer[0] = (UChar) mySourceChar;
|
|
|
|
_this->invalidUCharLength = 1;
|
|
|
|
|
|
|
|
FromU_CALLBACK_OFFSETS_LOGIC_MACRO(_this,
|
|
|
|
myTarget,
|
|
|
|
myTargetIndex,
|
|
|
|
targetLimit,
|
|
|
|
mySource,
|
|
|
|
mySourceIndex,
|
|
|
|
sourceLimit,
|
|
|
|
offsets,
|
|
|
|
flush,
|
|
|
|
err);
|
|
|
|
|
|
|
|
if (U_FAILURE (*err)) break;
|
|
|
|
_this->invalidUCharLength = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
*target += myTargetIndex;
|
|
|
|
*source += mySourceIndex;;
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-04-13 23:00:43 +00:00
|
|
|
static UChar32 T_UConverter_getNextUChar_MBCS(UConverter* converter,
|
2000-02-05 00:19:15 +00:00
|
|
|
const char** source,
|
|
|
|
const char* sourceLimit,
|
|
|
|
UErrorCode* err)
|
|
|
|
{
|
|
|
|
UChar myUChar;
|
|
|
|
char const *sourceInitial = *source;
|
|
|
|
/*safe keeps a ptr to the beginning in case we need to step back*/
|
|
|
|
|
|
|
|
/*Input boundary check*/
|
|
|
|
if ((*source)+1 > sourceLimit)
|
|
|
|
{
|
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
return 0xFFFD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Checks to see if the byte is a lead*/
|
|
|
|
if (converter->sharedData->table->mbcs.starters[(uint8_t)**source] == FALSE)
|
|
|
|
{
|
|
|
|
/*Not lead byte: we update the source ptr and get the codepoint*/
|
2000-04-19 23:05:27 +00:00
|
|
|
myUChar = ucmp16_getu((&converter->sharedData->table->mbcs.toUnicode),
|
2000-02-05 00:19:15 +00:00
|
|
|
(UChar)(**source));
|
2000-05-12 19:59:03 +00:00
|
|
|
if ((converter->useFallback == TRUE) &&
|
|
|
|
(converter->sharedData->staticData->hasToUnicodeFallback == TRUE) &&
|
|
|
|
(myUChar == 0xFFFD))
|
|
|
|
{
|
|
|
|
myUChar = ucmp16_getu((&converter->sharedData->table->mbcs.toUnicodeFallback),
|
|
|
|
(UChar)(**source));
|
|
|
|
}
|
2000-02-05 00:19:15 +00:00
|
|
|
(*source)++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*Lead byte: we Build the codepoint and get the corresponding character
|
|
|
|
* and update the source ptr*/
|
|
|
|
if ((*source + 2) > sourceLimit)
|
|
|
|
{
|
|
|
|
*err = U_TRUNCATED_CHAR_FOUND;
|
|
|
|
return 0xFFFD;
|
|
|
|
}
|
|
|
|
|
2000-04-19 23:05:27 +00:00
|
|
|
myUChar = ucmp16_getu((&converter->sharedData->table->mbcs.toUnicode),
|
2000-02-05 00:19:15 +00:00
|
|
|
(uint16_t)(((UChar)((**source)) << 8) |((uint8_t)*((*source)+1))));
|
|
|
|
|
2000-05-12 19:59:03 +00:00
|
|
|
if ((converter->useFallback == TRUE) &&
|
|
|
|
(converter->sharedData->staticData->hasToUnicodeFallback == TRUE) &&
|
|
|
|
(myUChar == 0xFFFD))
|
|
|
|
{
|
|
|
|
myUChar = ucmp16_getu((&converter->sharedData->table->mbcs.toUnicodeFallback),
|
|
|
|
(uint16_t)(((UChar)((**source)) << 8) |((uint8_t)*((*source)+1))));
|
|
|
|
}
|
2000-02-05 00:19:15 +00:00
|
|
|
(*source) += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (myUChar != 0xFFFD) return myUChar;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*rewinds source*/
|
|
|
|
const char* sourceFinal = *source;
|
|
|
|
UChar* myUCharPtr = &myUChar;
|
|
|
|
|
|
|
|
*err = U_INVALID_CHAR_FOUND;
|
|
|
|
*source = sourceInitial;
|
|
|
|
|
|
|
|
/*It's is very likely that the ErrorFunctor will write to the
|
|
|
|
*internal buffers */
|
|
|
|
converter->fromCharErrorBehaviour(converter,
|
|
|
|
&myUCharPtr,
|
|
|
|
myUCharPtr + 1,
|
|
|
|
&sourceFinal,
|
|
|
|
sourceLimit,
|
|
|
|
NULL,
|
|
|
|
TRUE,
|
|
|
|
err);
|
|
|
|
|
|
|
|
/*makes the internal caching transparent to the user*/
|
|
|
|
if (*err == U_INDEX_OUTOFBOUNDS_ERROR) *err = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
return myUChar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-13 23:00:43 +00:00
|
|
|
static void
|
2000-02-09 19:15:17 +00:00
|
|
|
_MBCSGetStarters(const UConverter* converter, bool_t starters[256], UErrorCode *pErrorCode) {
|
|
|
|
/* fills in the starters boolean array */
|
|
|
|
uprv_memcpy(starters, converter->sharedData->table->mbcs.starters, 256*sizeof(bool_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const UConverterImpl _MBCSImpl={
|
2000-02-05 00:19:15 +00:00
|
|
|
UCNV_MBCS,
|
|
|
|
|
2000-02-08 23:41:16 +00:00
|
|
|
_MBCSLoad,
|
|
|
|
_MBCSUnload,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
2000-02-05 00:19:15 +00:00
|
|
|
T_UConverter_toUnicode_MBCS,
|
|
|
|
T_UConverter_toUnicode_MBCS_OFFSETS_LOGIC,
|
|
|
|
T_UConverter_fromUnicode_MBCS,
|
|
|
|
T_UConverter_fromUnicode_MBCS_OFFSETS_LOGIC,
|
2000-02-09 19:15:17 +00:00
|
|
|
T_UConverter_getNextUChar_MBCS,
|
|
|
|
|
|
|
|
_MBCSGetStarters
|
2000-02-05 00:19:15 +00:00
|
|
|
};
|
|
|
|
|
2000-04-19 23:05:27 +00:00
|
|
|
|
2000-04-20 19:42:36 +00:00
|
|
|
/* Static data is in tools/makeconv/ucnvstat.c for data-based
|
|
|
|
* converters. Be sure to update it as well.
|
|
|
|
*/
|
2000-04-19 23:05:27 +00:00
|
|
|
|
2000-04-13 23:00:43 +00:00
|
|
|
const UConverterSharedData _MBCSData={
|
2000-02-05 00:19:15 +00:00
|
|
|
sizeof(UConverterSharedData), 1,
|
2000-04-20 19:42:36 +00:00
|
|
|
NULL, NULL, NULL, FALSE, &_MBCSImpl,
|
2000-04-19 23:05:27 +00:00
|
|
|
0
|
2000-02-05 00:19:15 +00:00
|
|
|
};
|