1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2000-01-13 23:54:23 +00:00
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998-1999, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* ucnv.c:
|
|
|
|
* Implements APIs for the ICU's codeset conversion library
|
|
|
|
* mostly calls through internal functions created and maintained
|
|
|
|
* by Bertrand A. Damiba
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 04/04/99 helena Fixed internal header inclusion.
|
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "umutex.h"
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/ures.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "uhash.h"
|
|
|
|
#include "ucmp16.h"
|
|
|
|
#include "ucmp8.h"
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/ucnv_bld.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "ucnv_io.h"
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/ucnv_err.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "ucnv_cnv.h"
|
1999-10-18 22:48:32 +00:00
|
|
|
#include "ucnv_imp.h"
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/ucnv.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "unicode/uloc.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
#define CHUNK_SIZE 5*1024
|
|
|
|
|
2000-01-08 00:51:44 +00:00
|
|
|
/* Internal function : begin */
|
|
|
|
static int32_t ucnv_getAmbiguousCCSID (const UConverter* cnv);
|
|
|
|
/* Internal function : end */
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
static void T_UConverter_fromCodepageToCodepage (UConverter * outConverter,
|
|
|
|
UConverter * inConverter,
|
|
|
|
char **target,
|
|
|
|
const char *targetLimit,
|
|
|
|
const char **source,
|
|
|
|
const char *sourceLimit,
|
|
|
|
int32_t* offsets,
|
|
|
|
bool_t flush,
|
|
|
|
UErrorCode * err);
|
|
|
|
|
|
|
|
|
|
|
|
const char* ucnv_getDefaultName ()
|
|
|
|
{
|
1999-12-04 00:55:54 +00:00
|
|
|
return ucnv_io_getDefaultConverterName();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ucnv_setDefaultName (const char *converterName)
|
|
|
|
{
|
1999-12-04 00:55:54 +00:00
|
|
|
ucnv_io_setDefaultConverterName(converterName);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
/*Calls through createConverter */
|
|
|
|
UConverter* ucnv_open (const char *name,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
1999-12-04 00:55:54 +00:00
|
|
|
if (err == NULL || U_FAILURE (*err)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
return NULL;
|
1999-12-04 00:55:54 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-04 00:55:54 +00:00
|
|
|
return createConverter (name, err);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*Extracts the UChar* to a char* and calls through createConverter */
|
|
|
|
UConverter* ucnv_openU (const UChar * name,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
char asciiName[UCNV_MAX_CONVERTER_NAME_LENGTH];
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
return NULL;
|
|
|
|
if (name == NULL)
|
|
|
|
return ucnv_open (NULL, err);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (u_strlen (name) > UCNV_MAX_CONVERTER_NAME_LENGTH)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_ILLEGAL_ARGUMENT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ucnv_open (u_austrcpy (asciiName, name), err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Assumes a $platform-#codepage.$CONVERTER_FILE_EXTENSION scheme and calls
|
|
|
|
*through createConverter*/
|
|
|
|
UConverter* ucnv_openCCSID (int32_t codepage,
|
1999-10-18 22:48:32 +00:00
|
|
|
UConverterPlatform platform,
|
1999-08-16 21:50:52 +00:00
|
|
|
UErrorCode * err)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
char myName[UCNV_MAX_CONVERTER_NAME_LENGTH];
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
copyPlatformString (myName, platform);
|
1999-12-28 23:39:02 +00:00
|
|
|
uprv_strcat (myName, "-");
|
|
|
|
T_CString_integerToString (myName + uprv_strlen (myName), codepage, 10);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
return createConverter (myName, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Decreases the reference counter in the shared immutable section of the object
|
|
|
|
*and frees the mutable part*/
|
|
|
|
|
|
|
|
void ucnv_close (UConverter * converter)
|
|
|
|
{
|
|
|
|
if (converter == NULL)
|
|
|
|
return;
|
2000-02-08 23:41:16 +00:00
|
|
|
|
|
|
|
if (converter->sharedData->impl->close != NULL) {
|
|
|
|
converter->sharedData->impl->close(converter);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-02-05 00:19:15 +00:00
|
|
|
if (converter->sharedData->referenceCounter != ~0) {
|
|
|
|
umtx_lock (NULL);
|
|
|
|
if (converter->sharedData->referenceCounter != 0) {
|
|
|
|
converter->sharedData->referenceCounter--;
|
|
|
|
}
|
|
|
|
umtx_unlock (NULL);
|
|
|
|
}
|
1999-12-28 23:39:02 +00:00
|
|
|
uprv_free (converter);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Frees all shared immutable objects that aren't referred to (reference count = 0)
|
|
|
|
*/
|
|
|
|
int32_t ucnv_flushCache ()
|
|
|
|
{
|
|
|
|
UConverterSharedData *mySharedData = NULL;
|
|
|
|
int32_t pos = -1;
|
|
|
|
int32_t tableDeletedNum = 0;
|
2000-03-30 04:17:27 +00:00
|
|
|
const UHashElement *e;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/*if shared data hasn't even been lazy evaluated yet
|
|
|
|
* return 0
|
|
|
|
*/
|
|
|
|
if (SHARED_DATA_HASHTABLE == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*creates an enumeration to iterate through every element in the
|
|
|
|
*table
|
|
|
|
*/
|
1999-09-30 23:47:52 +00:00
|
|
|
umtx_lock (NULL);
|
2000-03-28 22:04:39 +00:00
|
|
|
while ((e = uhash_nextElement (SHARED_DATA_HASHTABLE, &pos)) != NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-03-28 22:04:39 +00:00
|
|
|
mySharedData = (UConverterSharedData *) e->value;
|
1999-08-16 21:50:52 +00:00
|
|
|
/*deletes only if reference counter == 0 */
|
|
|
|
if (mySharedData->referenceCounter == 0)
|
2000-03-28 22:04:39 +00:00
|
|
|
{
|
|
|
|
tableDeletedNum++;
|
1999-09-30 23:47:52 +00:00
|
|
|
|
2000-03-28 22:04:39 +00:00
|
|
|
uhash_removeElement(SHARED_DATA_HASHTABLE, e);
|
|
|
|
deleteSharedConverterData (mySharedData);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
1999-09-30 23:47:52 +00:00
|
|
|
umtx_unlock (NULL);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
return tableDeletedNum;
|
|
|
|
}
|
|
|
|
|
1999-11-23 23:02:55 +00:00
|
|
|
/*returns a single Name from the list, will return NULL if out of bounds
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2000-04-14 05:22:29 +00:00
|
|
|
const char* ucnv_getAvailableName (int32_t n)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-04-14 05:22:29 +00:00
|
|
|
if (0 <= n && n <= 0xffff) {
|
1999-11-23 23:02:55 +00:00
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
2000-04-14 05:22:29 +00:00
|
|
|
const char *name = ucnv_io_getAvailableConverter((uint16_t)n, &err);
|
1999-11-23 23:02:55 +00:00
|
|
|
if (U_SUCCESS(err)) {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t ucnv_countAvailable ()
|
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
1999-12-14 00:37:27 +00:00
|
|
|
return ucnv_io_countAvailableConverters(&err);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-01-14 23:29:01 +00:00
|
|
|
U_CAPI uint16_t
|
|
|
|
ucnv_countAliases(const char *alias, UErrorCode *pErrorCode) {
|
|
|
|
const char *p;
|
|
|
|
return ucnv_io_getAliases(alias, &p, pErrorCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
U_CAPI const char *
|
2000-04-14 05:22:29 +00:00
|
|
|
ucnv_getAlias(const char *alias, uint16_t n, UErrorCode *pErrorCode) {
|
|
|
|
return ucnv_io_getAlias(alias, n, pErrorCode);
|
2000-01-14 23:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void
|
|
|
|
ucnv_getAliases(const char *alias, const char **aliases, UErrorCode *pErrorCode) {
|
2000-01-20 00:33:18 +00:00
|
|
|
const char *p;
|
2000-01-14 23:29:01 +00:00
|
|
|
uint16_t count=ucnv_io_getAliases(alias, &p, pErrorCode);
|
|
|
|
while(count>0) {
|
2000-01-20 00:33:18 +00:00
|
|
|
*aliases++=p;
|
2000-01-14 23:29:01 +00:00
|
|
|
/* skip a name, first the canonical converter name */
|
2000-01-20 00:33:18 +00:00
|
|
|
p+=uprv_strlen(p)+1;
|
2000-01-14 23:29:01 +00:00
|
|
|
--count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void ucnv_getSubstChars (const UConverter * converter,
|
|
|
|
char *mySubChar,
|
|
|
|
int8_t * len,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (*len < converter->subCharLen) /*not enough space in subChars */
|
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-12-28 23:39:02 +00:00
|
|
|
uprv_memcpy (mySubChar, converter->subChar, converter->subCharLen); /*fills in the subchars */
|
1999-08-16 21:50:52 +00:00
|
|
|
*len = converter->subCharLen; /*store # of bytes copied to buffer */
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ucnv_setSubstChars (UConverter * converter,
|
|
|
|
const char *mySubChar,
|
|
|
|
int8_t len,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/*Makes sure that the subChar is within the codepages char length boundaries */
|
|
|
|
if ((len > converter->sharedData->maxBytesPerChar)
|
|
|
|
|| (len < converter->sharedData->minBytesPerChar))
|
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_ILLEGAL_ARGUMENT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-12-28 23:39:02 +00:00
|
|
|
uprv_memcpy (converter->subChar, mySubChar, len); /*copies the subchars */
|
1999-08-16 21:50:52 +00:00
|
|
|
converter->subCharLen = len; /*sets the new len */
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int32_t ucnv_getDisplayName (const UConverter * converter,
|
|
|
|
const char *displayLocale,
|
|
|
|
UChar * displayName,
|
|
|
|
int32_t displayNameCapacity,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
UChar stringToWriteBuffer[UCNV_MAX_CONVERTER_NAME_LENGTH];
|
1999-08-16 21:50:52 +00:00
|
|
|
UChar const *stringToWrite;
|
|
|
|
int32_t stringToWriteLength;
|
|
|
|
UResourceBundle *rb = NULL;
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*create an RB, init the fill-in string, gets it from the RB */
|
|
|
|
rb = ures_open (NULL, displayLocale, err);
|
|
|
|
|
|
|
|
stringToWrite = ures_get (rb,
|
|
|
|
converter->sharedData->name,
|
|
|
|
err);
|
|
|
|
|
|
|
|
if (rb)
|
|
|
|
ures_close (rb);
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_SUCCESS (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
stringToWriteLength = u_strlen (stringToWrite);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*Error While creating or getting resource from the resource bundle
|
|
|
|
*use the internal name instead
|
|
|
|
*
|
|
|
|
*sets stringToWriteLength (which accounts for a NULL terminator)
|
|
|
|
*and stringToWrite
|
|
|
|
*/
|
1999-12-28 23:39:02 +00:00
|
|
|
stringToWriteLength = uprv_strlen (converter->sharedData->name) + 1;
|
1999-08-16 21:50:52 +00:00
|
|
|
stringToWrite = u_uastrcpy (stringToWriteBuffer, converter->sharedData->name);
|
|
|
|
|
|
|
|
/*Hides the fallback to the internal name from the user */
|
1999-10-07 00:07:53 +00:00
|
|
|
if (*err == U_MISSING_RESOURCE_ERROR)
|
|
|
|
*err = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*At this point we have a displayName and its length
|
|
|
|
*we want to see if it fits in the user provided params
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (stringToWriteLength <= displayNameCapacity)
|
|
|
|
{
|
|
|
|
/*it fits */
|
|
|
|
u_strcpy (displayName, stringToWrite);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*it doesn't fit */
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
u_strncpy (displayName,
|
|
|
|
stringToWrite,
|
|
|
|
displayNameCapacity);
|
|
|
|
/*Zero terminates the string */
|
|
|
|
if (displayNameCapacity > 0)
|
|
|
|
displayName[displayNameCapacity - 1] = 0x0000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*if the user provided us with a with an outputLength
|
|
|
|
*buffer we'll store in it the theoretical size of the
|
|
|
|
*displayString
|
|
|
|
*/
|
|
|
|
return stringToWriteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*resets the internal states of a converter
|
|
|
|
*goal : have the same behaviour than a freshly created converter
|
|
|
|
*/
|
|
|
|
void ucnv_reset (UConverter * converter)
|
|
|
|
{
|
|
|
|
converter->toUnicodeStatus = converter->sharedData->defaultConverterValues.toUnicodeStatus;
|
|
|
|
converter->fromUnicodeStatus = 0;
|
|
|
|
converter->UCharErrorBufferLength = 0;
|
|
|
|
converter->charErrorBufferLength = 0;
|
2000-02-08 23:41:16 +00:00
|
|
|
if (converter->sharedData->impl->reset != NULL) {
|
|
|
|
converter->sharedData->impl->reset(converter);
|
|
|
|
} else {
|
|
|
|
converter->mode = UCNV_SI;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t ucnv_getMaxCharSize (const UConverter * converter)
|
|
|
|
{
|
|
|
|
return converter->sharedData->maxBytesPerChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int8_t ucnv_getMinCharSize (const UConverter * converter)
|
|
|
|
{
|
|
|
|
return converter->sharedData->minBytesPerChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* ucnv_getName (const UConverter * converter, UErrorCode * err)
|
|
|
|
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return converter->sharedData->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t ucnv_getCCSID (const UConverter * converter,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return converter->sharedData->codepage;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
UConverterPlatform ucnv_getPlatform (const UConverter * converter,
|
1999-08-16 21:50:52 +00:00
|
|
|
UErrorCode * err)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err))
|
|
|
|
return UCNV_UNKNOWN;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
return converter->sharedData->platform;
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
UConverterToUCallback ucnv_getToUCallBack (const UConverter * converter)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
return converter->fromCharErrorBehaviour;
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
UConverterFromUCallback ucnv_getFromUCallBack (const UConverter * converter)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
return converter->fromUCharErrorBehaviour;
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
UConverterToUCallback ucnv_setToUCallBack (UConverter * converter,
|
|
|
|
UConverterToUCallback action,
|
1999-08-16 21:50:52 +00:00
|
|
|
UErrorCode * err)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
UConverterToUCallback myReturn = NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
return NULL;
|
|
|
|
myReturn = converter->fromCharErrorBehaviour;
|
|
|
|
converter->fromCharErrorBehaviour = action;
|
|
|
|
|
|
|
|
return myReturn;
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
UConverterFromUCallback ucnv_setFromUCallBack (UConverter * converter,
|
|
|
|
UConverterFromUCallback action,
|
1999-08-16 21:50:52 +00:00
|
|
|
UErrorCode * err)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
UConverterFromUCallback myReturn = NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
return NULL;
|
|
|
|
myReturn = converter->fromUCharErrorBehaviour;
|
|
|
|
converter->fromUCharErrorBehaviour = action;
|
|
|
|
|
|
|
|
return myReturn;
|
|
|
|
}
|
|
|
|
void ucnv_fromUnicode (UConverter * _this,
|
|
|
|
char **target,
|
|
|
|
const char *targetLimit,
|
|
|
|
const UChar ** source,
|
|
|
|
const UChar * sourceLimit,
|
|
|
|
int32_t* offsets,
|
|
|
|
bool_t flush,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Check parameters in for all conversions
|
|
|
|
*/
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err)) return;
|
1999-08-16 21:50:52 +00:00
|
|
|
if ((_this == NULL) || ((char *) targetLimit < *target) || (sourceLimit < *source))
|
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_ILLEGAL_ARGUMENT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deal with stored carry over data. This is done in the common location
|
|
|
|
* to avoid doing it for each conversion.
|
|
|
|
*/
|
|
|
|
if (_this->charErrorBufferLength > 0)
|
|
|
|
{
|
|
|
|
int32_t myTargetIndex = 0;
|
|
|
|
|
|
|
|
flushInternalCharBuffer (_this,
|
|
|
|
(char *) *target,
|
|
|
|
&myTargetIndex,
|
|
|
|
targetLimit - *target,
|
|
|
|
offsets?&offsets:NULL,
|
|
|
|
err);
|
|
|
|
*target += myTargetIndex;
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err)) return;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-02-08 23:41:16 +00:00
|
|
|
|
|
|
|
if (offsets) {
|
|
|
|
if (_this->sharedData->impl->fromUnicodeWithOffsets != NULL) {
|
|
|
|
_this->sharedData->impl->fromUnicodeWithOffsets(_this,
|
|
|
|
target,
|
|
|
|
targetLimit,
|
|
|
|
source,
|
|
|
|
sourceLimit,
|
|
|
|
offsets,
|
|
|
|
flush,
|
|
|
|
err);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
/* all code points are of the same length */
|
|
|
|
int32_t targetSize = targetLimit - *target;
|
|
|
|
int32_t i, bytesPerChar = _this->sharedData->maxBytesPerChar;
|
|
|
|
|
|
|
|
if(bytesPerChar == 1) {
|
|
|
|
for (i=0; i<targetSize; i++) {
|
|
|
|
offsets[i] = i;
|
|
|
|
}
|
|
|
|
} else if(bytesPerChar == 2) {
|
|
|
|
for (i=0; i<targetSize; i++) {
|
|
|
|
offsets[i] = i>>1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int32_t j = 0, k = bytesPerChar;
|
|
|
|
|
|
|
|
for (i=0; i<targetSize; i++) {
|
|
|
|
/* offsets[i] = i/bytesPerChar; -- without division */
|
|
|
|
offsets[i] = j;
|
|
|
|
if(--k == 0) {
|
|
|
|
k = bytesPerChar;
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-02-08 23:41:16 +00:00
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/*calls the specific conversion routines */
|
2000-02-05 00:19:15 +00:00
|
|
|
_this->sharedData->impl->fromUnicode(_this,
|
1999-08-16 21:50:52 +00:00
|
|
|
target,
|
|
|
|
targetLimit,
|
|
|
|
source,
|
|
|
|
sourceLimit,
|
|
|
|
offsets,
|
|
|
|
flush,
|
|
|
|
err);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ucnv_toUnicode (UConverter * _this,
|
|
|
|
UChar ** target,
|
|
|
|
const UChar * targetLimit,
|
|
|
|
const char **source,
|
|
|
|
const char *sourceLimit,
|
|
|
|
int32_t* offsets,
|
|
|
|
bool_t flush,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Check parameters in for all conversions
|
|
|
|
*/
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err)) return;
|
1999-08-16 21:50:52 +00:00
|
|
|
if ((_this == NULL) || ((UChar *) targetLimit < *target) || (sourceLimit < *source))
|
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_ILLEGAL_ARGUMENT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deal with stored carry over data. This is done in the common location
|
|
|
|
* to avoid doing it for each conversion.
|
|
|
|
*/
|
|
|
|
if (_this->UCharErrorBufferLength > 0)
|
|
|
|
{
|
|
|
|
int32_t myTargetIndex = 0;
|
|
|
|
|
|
|
|
flushInternalUnicodeBuffer (_this,
|
|
|
|
*target,
|
|
|
|
&myTargetIndex,
|
|
|
|
targetLimit - *target,
|
|
|
|
offsets?&offsets:NULL,
|
|
|
|
err);
|
|
|
|
*target += myTargetIndex;
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-02-08 23:41:16 +00:00
|
|
|
if (offsets) {
|
|
|
|
if (_this->sharedData->impl->toUnicodeWithOffsets != NULL) {
|
|
|
|
_this->sharedData->impl->toUnicodeWithOffsets(_this,
|
|
|
|
target,
|
|
|
|
targetLimit,
|
|
|
|
source,
|
|
|
|
sourceLimit,
|
|
|
|
offsets,
|
|
|
|
flush,
|
|
|
|
err);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
/* all code points are of the same length */
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t targetSize = targetLimit - *target;
|
2000-02-08 23:41:16 +00:00
|
|
|
int32_t i, bytesPerChar = _this->sharedData->maxBytesPerChar;
|
|
|
|
|
|
|
|
if(bytesPerChar == 1) {
|
|
|
|
for (i=0; i<targetSize; i++) {
|
|
|
|
offsets[i] = i;
|
|
|
|
}
|
|
|
|
} else if(bytesPerChar == 2) {
|
|
|
|
for (i=0; i<targetSize; i++) {
|
|
|
|
offsets[i] = i<<1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i=0; i<targetSize; i++) {
|
|
|
|
offsets[i] = i*bytesPerChar;
|
|
|
|
}
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-02-08 23:41:16 +00:00
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/*calls the specific conversion routines */
|
2000-02-05 00:19:15 +00:00
|
|
|
_this->sharedData->impl->toUnicode(_this,
|
1999-08-16 21:50:52 +00:00
|
|
|
target,
|
|
|
|
targetLimit,
|
|
|
|
source,
|
|
|
|
sourceLimit,
|
|
|
|
offsets,
|
|
|
|
flush,
|
|
|
|
err);
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t ucnv_fromUChars (const UConverter * converter,
|
|
|
|
char *target,
|
|
|
|
int32_t targetSize,
|
|
|
|
const UChar * source,
|
2000-03-07 01:42:40 +00:00
|
|
|
int32_t sourceSize,
|
1999-08-16 21:50:52 +00:00
|
|
|
UErrorCode * err)
|
|
|
|
{
|
|
|
|
const UChar *mySource = source;
|
|
|
|
const UChar *mySource_limit;
|
2000-03-07 01:42:40 +00:00
|
|
|
int32_t mySourceLength = sourceSize;
|
1999-08-16 21:50:52 +00:00
|
|
|
UConverter myConverter;
|
|
|
|
char *myTarget = target;
|
1999-10-25 22:20:12 +00:00
|
|
|
char *myTarget_limit;
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t targetCapacity = 0;
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((converter == NULL) || (targetSize < 0))
|
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_ILLEGAL_ARGUMENT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*makes a local copy of the UConverter */
|
|
|
|
myConverter = *converter;
|
|
|
|
|
|
|
|
|
|
|
|
/*Removes all state info on the UConverter */
|
|
|
|
ucnv_reset (&myConverter);
|
|
|
|
|
|
|
|
/*if the source is empty we return immediately */
|
2000-03-07 01:42:40 +00:00
|
|
|
if (sourceSize == -1) {
|
|
|
|
mySourceLength = u_strlen (source);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
if (mySourceLength == 0)
|
|
|
|
{
|
|
|
|
/*for consistency we still need to
|
|
|
|
*store 0 in the targetCapacity
|
|
|
|
*if the user requires it
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mySource_limit = mySource + mySourceLength;
|
1999-10-25 22:20:12 +00:00
|
|
|
myTarget_limit = target + targetSize;
|
|
|
|
|
2000-03-07 01:26:15 +00:00
|
|
|
/* Pin the limit to U_MAX_PTR. NULL check is for AS/400. */
|
|
|
|
if((myTarget_limit < target) || (myTarget_limit == NULL)) {
|
|
|
|
myTarget_limit = (char *)U_MAX_PTR;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if (targetSize > 0)
|
|
|
|
{
|
|
|
|
ucnv_fromUnicode (&myConverter,
|
|
|
|
&myTarget,
|
1999-10-26 21:15:33 +00:00
|
|
|
myTarget_limit,
|
1999-08-16 21:50:52 +00:00
|
|
|
&mySource,
|
|
|
|
mySource_limit,
|
|
|
|
NULL,
|
|
|
|
TRUE,
|
|
|
|
err);
|
|
|
|
targetCapacity = myTarget - target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Updates targetCapacity to contain the number of bytes written to target */
|
|
|
|
|
|
|
|
if (targetSize == 0)
|
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the output buffer is exhausted, we need to stop writing
|
|
|
|
* to it but continue the conversion in order to store in targetSize
|
|
|
|
* the number of bytes that was required*/
|
1999-10-07 00:07:53 +00:00
|
|
|
if (*err == U_INDEX_OUTOFBOUNDS_ERROR)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
char target2[CHUNK_SIZE];
|
|
|
|
char *target2_alias = target2;
|
|
|
|
const char *target2_limit = target2 + CHUNK_SIZE;
|
|
|
|
|
|
|
|
/*We use a stack allocated buffer around which we loop
|
|
|
|
*(in case the output is greater than CHUNK_SIZE)
|
|
|
|
*/
|
|
|
|
|
1999-10-07 00:07:53 +00:00
|
|
|
while (*err == U_INDEX_OUTOFBOUNDS_ERROR)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
target2_alias = target2;
|
|
|
|
ucnv_fromUnicode (&myConverter,
|
|
|
|
&target2_alias,
|
|
|
|
target2_limit,
|
|
|
|
&mySource,
|
|
|
|
mySource_limit,
|
|
|
|
NULL,
|
|
|
|
TRUE,
|
|
|
|
err);
|
|
|
|
|
|
|
|
/*updates the output parameter to contain the number of char required */
|
|
|
|
targetCapacity += (target2_alias - target2) + 1;
|
|
|
|
}
|
1999-10-07 00:07:53 +00:00
|
|
|
/*We will set the erro code to U_BUFFER_OVERFLOW_ERROR only if
|
1999-08-16 21:50:52 +00:00
|
|
|
*nothing graver happened in the previous loop*/
|
|
|
|
(targetCapacity)--;
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_SUCCESS (*err))
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return targetCapacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t ucnv_toUChars (const UConverter * converter,
|
|
|
|
UChar * target,
|
|
|
|
int32_t targetSize,
|
|
|
|
const char *source,
|
|
|
|
int32_t sourceSize,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
|
|
|
const char *mySource = source;
|
|
|
|
const char *mySource_limit = source + sourceSize;
|
|
|
|
UConverter myConverter;
|
|
|
|
UChar *myTarget = target;
|
1999-10-25 22:20:12 +00:00
|
|
|
UChar *myTarget_limit;
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t targetCapacity;
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((converter == NULL) || (targetSize < 0) || (sourceSize < 0))
|
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_ILLEGAL_ARGUMENT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*Means there is no work to be done */
|
|
|
|
if (sourceSize == 0)
|
|
|
|
{
|
|
|
|
/*for consistency we still need to
|
|
|
|
*store 0 in the targetCapacity
|
|
|
|
*if the user requires it
|
|
|
|
*/
|
|
|
|
if (targetSize >= 1)
|
|
|
|
{
|
|
|
|
target[0] = 0x0000;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*makes a local copy of the UConverter */
|
|
|
|
myConverter = *converter;
|
|
|
|
|
|
|
|
/*Removes all state info on the UConverter */
|
|
|
|
ucnv_reset (&myConverter);
|
|
|
|
|
|
|
|
if (targetSize > 0)
|
1999-12-23 18:40:27 +00:00
|
|
|
{
|
|
|
|
myTarget_limit = target + targetSize - 1;
|
2000-03-07 01:26:15 +00:00
|
|
|
|
|
|
|
/* Pin the limit to U_MAX_PTR. NULL check is for AS/400. */
|
|
|
|
if ((myTarget_limit == NULL) || (myTarget_limit < target)) {
|
1999-12-23 18:40:27 +00:00
|
|
|
myTarget_limit = ((UChar*)U_MAX_PTR) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Not in pure pre-flight mode */
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
ucnv_toUnicode (&myConverter,
|
|
|
|
&myTarget,
|
1999-10-25 22:20:12 +00:00
|
|
|
myTarget_limit, /*Save a spot for the Null terminator */
|
1999-08-16 21:50:52 +00:00
|
|
|
&mySource,
|
|
|
|
mySource_limit,
|
|
|
|
NULL,
|
|
|
|
TRUE,
|
|
|
|
err);
|
|
|
|
|
|
|
|
/*Null terminates the string */
|
|
|
|
*(myTarget) = 0x0000;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*Rigs targetCapacity to have at least one cell for zero termination */
|
|
|
|
/*Updates targetCapacity to contain the number of bytes written to target */
|
|
|
|
targetCapacity = 1;
|
|
|
|
targetCapacity += myTarget - target;
|
|
|
|
if (targetSize == 0)
|
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
/* If the output buffer is exhausted, we need to stop writing
|
|
|
|
* to it but if the input buffer is not exhausted,
|
|
|
|
* we need to continue the conversion in order to store in targetSize
|
|
|
|
* the number of bytes that was required
|
|
|
|
*/
|
1999-10-07 00:07:53 +00:00
|
|
|
if (*err == U_INDEX_OUTOFBOUNDS_ERROR)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
UChar target2[CHUNK_SIZE];
|
|
|
|
UChar *target2_alias = target2;
|
|
|
|
const UChar *target2_limit = target2 + CHUNK_SIZE;
|
|
|
|
|
|
|
|
/*We use a stack allocated buffer around which we loop
|
|
|
|
(in case the output is greater than CHUNK_SIZE) */
|
|
|
|
|
1999-10-07 00:07:53 +00:00
|
|
|
while (*err == U_INDEX_OUTOFBOUNDS_ERROR)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
target2_alias = target2;
|
|
|
|
ucnv_toUnicode (&myConverter,
|
|
|
|
&target2_alias,
|
|
|
|
target2_limit,
|
|
|
|
&mySource,
|
|
|
|
mySource_limit,
|
|
|
|
NULL,
|
|
|
|
TRUE,
|
|
|
|
err);
|
|
|
|
|
|
|
|
/*updates the output parameter to contain the number of char required */
|
|
|
|
targetCapacity += target2_alias - target2 + 1;
|
|
|
|
}
|
|
|
|
(targetCapacity)--; /*adjust for last one */
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_SUCCESS (*err))
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return targetCapacity;
|
|
|
|
}
|
|
|
|
|
2000-04-13 17:27:35 +00:00
|
|
|
UChar32 ucnv_getNextUChar (UConverter * converter,
|
1999-08-16 21:50:52 +00:00
|
|
|
const char **source,
|
|
|
|
const char *sourceLimit,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
|
|
|
/* In case internal data had been stored
|
2000-04-13 17:27:35 +00:00
|
|
|
* we return the first UChar32 in the internal buffer,
|
1999-08-16 21:50:52 +00:00
|
|
|
* and update the internal state accordingly
|
|
|
|
*/
|
|
|
|
if (converter->UCharErrorBufferLength > 0)
|
|
|
|
{
|
2000-04-13 17:27:35 +00:00
|
|
|
UTextOffset i = 0;
|
|
|
|
UChar32 myUChar;
|
|
|
|
UTF_NEXT_CHAR(converter->UCharErrorBuffer, i, sizeof(converter->UCharErrorBuffer), myUChar);
|
1999-08-16 21:50:52 +00:00
|
|
|
/*In this memmove we update the internal buffer by
|
|
|
|
*popping the first character.
|
|
|
|
*Note that in the call itself we decrement
|
|
|
|
*UCharErrorBufferLength
|
|
|
|
*/
|
1999-12-28 23:39:02 +00:00
|
|
|
uprv_memmove (converter->UCharErrorBuffer,
|
2000-04-13 17:27:35 +00:00
|
|
|
converter->UCharErrorBuffer + i,
|
|
|
|
(converter->UCharErrorBufferLength - i) * sizeof (UChar));
|
|
|
|
converter->UCharErrorBufferLength -= (int8_t)i;
|
1999-08-16 21:50:52 +00:00
|
|
|
return myUChar;
|
|
|
|
}
|
|
|
|
/*calls the specific conversion routines */
|
|
|
|
/*as dictated in a code review, avoids a switch statement */
|
2000-02-05 00:19:15 +00:00
|
|
|
return converter->sharedData->impl->getNextUChar(converter,
|
1999-08-16 21:50:52 +00:00
|
|
|
source,
|
|
|
|
sourceLimit,
|
|
|
|
err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**************************
|
|
|
|
* Will convert a sequence of bytes from one codepage to another.
|
|
|
|
* @param toConverterName: The name of the converter that will be used to encode the output buffer
|
|
|
|
* @param fromConverterName: The name of the converter that will be used to decode the input buffer
|
|
|
|
* @param target: Pointer to the output buffer* written
|
|
|
|
* @param targetLength: on input contains the capacity of target, on output the number of bytes copied to target
|
|
|
|
* @param source: Pointer to the input buffer
|
|
|
|
* @param sourceLength: on input contains the capacity of source, on output the number of bytes processed in "source"
|
|
|
|
* @param internal: used internally to store store state data across calls
|
|
|
|
* @param err: fills in an error status
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
T_UConverter_fromCodepageToCodepage (UConverter * outConverter,
|
|
|
|
UConverter * inConverter,
|
|
|
|
char **target,
|
|
|
|
const char *targetLimit,
|
|
|
|
const char **source,
|
|
|
|
const char *sourceLimit,
|
|
|
|
int32_t* offsets,
|
|
|
|
bool_t flush,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
|
|
|
|
|
|
|
UChar out_chunk[CHUNK_SIZE];
|
|
|
|
const UChar *out_chunk_limit = out_chunk + CHUNK_SIZE;
|
|
|
|
UChar *out_chunk_alias;
|
|
|
|
UChar const *out_chunk_alias2;
|
|
|
|
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err)) return;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*loops until the input buffer is completely consumed
|
|
|
|
*or if an error has be encountered
|
|
|
|
*first we convert from inConverter codepage to Unicode
|
|
|
|
*then from Unicode to outConverter codepage
|
|
|
|
*/
|
1999-10-18 22:48:32 +00:00
|
|
|
while ((*source != sourceLimit) && U_SUCCESS (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
out_chunk_alias = out_chunk;
|
|
|
|
ucnv_toUnicode (inConverter,
|
|
|
|
&out_chunk_alias,
|
|
|
|
out_chunk_limit,
|
|
|
|
source,
|
|
|
|
sourceLimit,
|
|
|
|
NULL,
|
|
|
|
flush,
|
|
|
|
err);
|
|
|
|
|
1999-10-07 00:07:53 +00:00
|
|
|
/*U_INDEX_OUTOFBOUNDS_ERROR means that the output "CHUNK" is full
|
1999-08-16 21:50:52 +00:00
|
|
|
*we will require at least another loop (it's a recoverable error)
|
|
|
|
*/
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_SUCCESS (*err) || (*err == U_INDEX_OUTOFBOUNDS_ERROR))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
out_chunk_alias2 = out_chunk;
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
while ((out_chunk_alias2 != out_chunk_alias) && U_SUCCESS (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
ucnv_fromUnicode (outConverter,
|
|
|
|
target,
|
|
|
|
targetLimit,
|
|
|
|
&out_chunk_alias2,
|
|
|
|
out_chunk_alias,
|
|
|
|
NULL,
|
|
|
|
TRUE,
|
|
|
|
err);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t ucnv_convert(const char *toConverterName,
|
|
|
|
const char *fromConverterName,
|
|
|
|
char *target,
|
|
|
|
int32_t targetSize,
|
|
|
|
const char *source,
|
|
|
|
int32_t sourceSize,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
|
|
|
const char *mySource = source;
|
|
|
|
const char *mySource_limit = source + sourceSize;
|
|
|
|
UConverter *inConverter;
|
|
|
|
UConverter *outConverter;
|
|
|
|
char *myTarget = target;
|
|
|
|
int32_t targetCapacity = 0;
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((targetSize < 0) || (sourceSize < 0))
|
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_ILLEGAL_ARGUMENT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*if there is no input data, we're done */
|
|
|
|
if (sourceSize == 0)
|
|
|
|
{
|
|
|
|
/*in case the caller passed an output ptr
|
|
|
|
*we update it
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*create the converters */
|
|
|
|
inConverter = ucnv_open (fromConverterName, err);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err)) return 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
outConverter = ucnv_open (toConverterName, err);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE (*err))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
ucnv_close (inConverter);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (targetSize > 0)
|
|
|
|
{
|
|
|
|
T_UConverter_fromCodepageToCodepage (outConverter,
|
|
|
|
inConverter,
|
|
|
|
&myTarget,
|
|
|
|
target + targetSize,
|
|
|
|
&mySource,
|
|
|
|
mySource_limit,
|
|
|
|
NULL,
|
|
|
|
TRUE,
|
|
|
|
err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*Updates targetCapacity to contain the number of bytes written to target */
|
|
|
|
targetCapacity = myTarget - target;
|
|
|
|
if (targetSize == 0)
|
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_INDEX_OUTOFBOUNDS_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the output buffer is exhausted, we need to stop writing
|
|
|
|
* to it but continue the conversion in order to store in targetSize
|
|
|
|
* the number of bytes that was required*/
|
1999-10-07 00:07:53 +00:00
|
|
|
if (*err == U_INDEX_OUTOFBOUNDS_ERROR)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
char target2[CHUNK_SIZE];
|
|
|
|
char *target2_alias = target2;
|
|
|
|
const char *target2_limit = target2 + CHUNK_SIZE;
|
|
|
|
|
|
|
|
/*We use a stack allocated buffer around which we loop
|
|
|
|
*(in case the output is greater than CHUNK_SIZE)
|
|
|
|
*/
|
|
|
|
|
1999-10-07 00:07:53 +00:00
|
|
|
while (*err == U_INDEX_OUTOFBOUNDS_ERROR)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
target2_alias = target2;
|
|
|
|
T_UConverter_fromCodepageToCodepage (outConverter,
|
|
|
|
inConverter,
|
|
|
|
&target2_alias,
|
|
|
|
target2_limit,
|
|
|
|
&mySource,
|
|
|
|
mySource_limit,
|
|
|
|
NULL,
|
|
|
|
TRUE,
|
|
|
|
err);
|
|
|
|
|
|
|
|
/*updates the output parameter to contain the number of char required */
|
|
|
|
targetCapacity += (target2_alias - target2) + 1;
|
|
|
|
}
|
1999-10-07 00:07:53 +00:00
|
|
|
/*We will set the erro code to U_BUFFER_OVERFLOW_ERROR only if
|
1999-08-16 21:50:52 +00:00
|
|
|
*nothing graver happened in the previous loop*/
|
|
|
|
(targetCapacity)--;
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_SUCCESS (*err))
|
1999-10-07 00:07:53 +00:00
|
|
|
*err = U_BUFFER_OVERFLOW_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ucnv_close (inConverter);
|
|
|
|
ucnv_close (outConverter);
|
|
|
|
|
|
|
|
return targetCapacity;
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
UConverterType ucnv_getType(const UConverter* converter)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
return converter->sharedData->conversionType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ucnv_getStarters(const UConverter* converter,
|
|
|
|
bool_t starters[256],
|
|
|
|
UErrorCode* err)
|
|
|
|
{
|
2000-02-09 19:15:17 +00:00
|
|
|
if (err == NULL || U_FAILURE(*err)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(converter->sharedData->impl->getStarters != NULL) {
|
|
|
|
converter->sharedData->impl->getStarters(converter, starters, err);
|
|
|
|
} else {
|
|
|
|
*err = U_ILLEGAL_ARGUMENT_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
2000-01-08 00:51:44 +00:00
|
|
|
|
|
|
|
int32_t ucnv_getAmbiguousCCSID(const UConverter *cnv)
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
int32_t i = 0;
|
|
|
|
int32_t ccsid = 0;
|
|
|
|
if (cnv == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ccsid = ucnv_getCCSID(cnv, &status);
|
|
|
|
if (U_FAILURE(status))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < UCNV_MAX_AMBIGUOUSCCSIDS; i++) {
|
|
|
|
if (ccsid == UCNV_AMBIGUOUSCONVERTERS[i].ccsid)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ucnv_fixFileSeparator(const UConverter *cnv,
|
|
|
|
UChar* source,
|
|
|
|
int32_t sourceLength)
|
|
|
|
{
|
|
|
|
int32_t i = 0;
|
2000-04-14 05:22:29 +00:00
|
|
|
int32_t offset = 0;
|
2000-01-08 00:51:44 +00:00
|
|
|
if ((source == NULL) || (cnv == NULL))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2000-04-14 05:22:29 +00:00
|
|
|
if ((offset = ucnv_getAmbiguousCCSID(cnv)) != -1)
|
2000-01-08 00:51:44 +00:00
|
|
|
{
|
|
|
|
for (i = 0; i < sourceLength; i++)
|
|
|
|
{
|
2000-04-14 05:22:29 +00:00
|
|
|
if (source[i] == UCNV_AMBIGUOUSCONVERTERS[offset].mismapped)
|
2000-01-08 00:51:44 +00:00
|
|
|
{
|
2000-04-14 05:22:29 +00:00
|
|
|
source[i] = UCNV_AMBIGUOUSCONVERTERS[offset].replacement;
|
2000-01-08 00:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool_t ucnv_isAmbiguous(const UConverter *cnv)
|
|
|
|
{
|
|
|
|
return (ucnv_getAmbiguousCCSID(cnv) == -1 ? FALSE : TRUE);
|
|
|
|
}
|