2000-06-28 19:43:17 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2004-08-26 22:51:40 +00:00
|
|
|
* Copyright (C) 2000-2004, International Business Machines
|
2000-06-28 19:43:17 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* ucnv_cb.c:
|
|
|
|
* External APIs for the ICU's codeset conversion library
|
|
|
|
* Helena Shih
|
2002-05-22 22:56:27 +00:00
|
|
|
*
|
2000-06-28 19:43:17 +00:00
|
|
|
* Modification History:
|
|
|
|
*
|
2002-05-22 22:56:27 +00:00
|
|
|
* Date Name Description
|
2000-07-31 14:38:10 +00:00
|
|
|
* 7/28/2000 srl Implementation
|
2000-06-28 19:43:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Character Conversion C API
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2000-07-31 14:38:10 +00:00
|
|
|
#include "unicode/utypes.h"
|
2004-08-26 22:51:40 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_CONVERSION
|
|
|
|
|
2000-06-28 19:43:17 +00:00
|
|
|
#include "unicode/ucnv_cb.h"
|
2000-07-31 14:38:10 +00:00
|
|
|
#include "ucnv_bld.h"
|
2000-12-20 22:54:51 +00:00
|
|
|
#include "ucnv_cnv.h"
|
2000-07-31 14:38:10 +00:00
|
|
|
#include "cmemory.h"
|
2000-06-28 19:43:17 +00:00
|
|
|
|
2000-06-30 17:38:47 +00:00
|
|
|
/* need to update the offsets when the target moves. */
|
|
|
|
/* Note: Recursion may occur in the cb functions, be sure to update the offsets correctly
|
|
|
|
if you don't use ucnv_cbXXX functions. Make sure you don't use the same callback within
|
|
|
|
the same call stack if the complexity arises. */
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
ucnv_cbFromUWriteBytes (UConverterFromUnicodeArgs *args,
|
2000-06-30 17:38:47 +00:00
|
|
|
const char* source,
|
2000-06-28 19:43:17 +00:00
|
|
|
int32_t length,
|
2000-06-30 17:38:47 +00:00
|
|
|
int32_t offsetIndex,
|
2000-06-28 19:43:17 +00:00
|
|
|
UErrorCode * err)
|
|
|
|
{
|
2003-10-25 00:29:13 +00:00
|
|
|
if(U_FAILURE(*err)) {
|
|
|
|
return;
|
2000-07-31 14:38:10 +00:00
|
|
|
}
|
2002-05-22 22:56:27 +00:00
|
|
|
|
2003-10-25 00:29:13 +00:00
|
|
|
ucnv_fromUWriteBytes(
|
|
|
|
args->converter,
|
|
|
|
source, length,
|
|
|
|
&args->target, args->targetLimit,
|
|
|
|
&args->offsets, offsetIndex,
|
|
|
|
err);
|
2000-06-30 17:38:47 +00:00
|
|
|
}
|
2000-06-28 19:43:17 +00:00
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
ucnv_cbFromUWriteUChars(UConverterFromUnicodeArgs *args,
|
2000-08-02 19:35:17 +00:00
|
|
|
const UChar** source,
|
|
|
|
const UChar* sourceLimit,
|
2000-06-30 17:38:47 +00:00
|
|
|
int32_t offsetIndex,
|
|
|
|
UErrorCode * err)
|
|
|
|
{
|
2002-05-22 22:56:27 +00:00
|
|
|
/*
|
|
|
|
This is a fun one. Recursion can occur - we're basically going to
|
|
|
|
just retry shoving data through the same converter. Note, if you got
|
|
|
|
here through some kind of invalid sequence, you maybe should emit a
|
2000-07-31 14:38:10 +00:00
|
|
|
reset sequence of some kind and/or call ucnv_reset(). Since this
|
|
|
|
IS an actual conversion, take care that you've changed the callback
|
|
|
|
or the data, or you'll get an infinite loop.
|
2002-05-22 22:56:27 +00:00
|
|
|
|
2000-07-31 14:38:10 +00:00
|
|
|
Please set the err value to something reasonable before calling
|
|
|
|
into this.
|
2002-05-22 22:56:27 +00:00
|
|
|
*/
|
2000-07-31 14:38:10 +00:00
|
|
|
|
2002-05-22 22:56:27 +00:00
|
|
|
char *oldTarget;
|
2000-07-31 14:38:10 +00:00
|
|
|
|
2002-05-22 22:56:27 +00:00
|
|
|
if(U_FAILURE(*err))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2000-08-08 21:36:52 +00:00
|
|
|
|
2002-05-22 22:56:27 +00:00
|
|
|
oldTarget = args->target;
|
2000-07-31 14:38:10 +00:00
|
|
|
|
2002-05-22 22:56:27 +00:00
|
|
|
ucnv_fromUnicode(args->converter,
|
|
|
|
&args->target,
|
|
|
|
args->targetLimit,
|
|
|
|
source,
|
|
|
|
sourceLimit,
|
|
|
|
NULL, /* no offsets */
|
|
|
|
FALSE, /* no flush */
|
|
|
|
err);
|
2000-08-08 21:36:52 +00:00
|
|
|
|
2002-05-22 22:56:27 +00:00
|
|
|
if(args->offsets)
|
2000-07-31 14:38:10 +00:00
|
|
|
{
|
2002-05-22 22:56:27 +00:00
|
|
|
while (args->target != oldTarget) /* if it moved at all.. */
|
|
|
|
{
|
|
|
|
*(args->offsets)++ = offsetIndex;
|
|
|
|
oldTarget++;
|
|
|
|
}
|
2000-07-31 14:38:10 +00:00
|
|
|
}
|
2002-05-22 22:56:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Note, if you did something like used a Stop subcallback, things would get interesting.
|
|
|
|
In fact, here's where we want to return the partially consumed in-source!
|
|
|
|
*/
|
|
|
|
if(*err == U_BUFFER_OVERFLOW_ERROR)
|
|
|
|
/* && (*source < sourceLimit && args->target >= args->targetLimit)
|
|
|
|
-- S. Hrcek */
|
|
|
|
{
|
2000-07-31 14:38:10 +00:00
|
|
|
/* Overflowed the target. Now, we'll write into the charErrorBuffer.
|
2002-05-22 22:56:27 +00:00
|
|
|
It's a fixed size. If we overflow it... Hmm */
|
|
|
|
char *newTarget;
|
|
|
|
const char *newTargetLimit;
|
|
|
|
UErrorCode err2 = U_ZERO_ERROR;
|
2000-07-31 14:38:10 +00:00
|
|
|
|
2002-05-22 22:56:27 +00:00
|
|
|
int8_t errBuffLen;
|
2000-07-31 14:38:10 +00:00
|
|
|
|
2002-05-22 22:56:27 +00:00
|
|
|
errBuffLen = args->converter->charErrorBufferLength;
|
2000-07-31 14:38:10 +00:00
|
|
|
|
2002-05-22 22:56:27 +00:00
|
|
|
/* start the new target at the first free slot in the errbuff.. */
|
|
|
|
newTarget = (char *)(args->converter->charErrorBuffer + errBuffLen);
|
2000-07-31 14:38:10 +00:00
|
|
|
|
2002-05-22 22:56:27 +00:00
|
|
|
newTargetLimit = (char *)(args->converter->charErrorBuffer +
|
|
|
|
sizeof(args->converter->charErrorBuffer));
|
|
|
|
|
|
|
|
if(newTarget >= newTargetLimit)
|
|
|
|
{
|
|
|
|
*err = U_INTERNAL_PROGRAM_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2000-07-31 14:38:10 +00:00
|
|
|
|
2002-05-22 22:56:27 +00:00
|
|
|
/* We're going to tell the converter that the errbuff len is empty.
|
2000-07-31 14:38:10 +00:00
|
|
|
This prevents the existing errbuff from being 'flushed' out onto
|
2002-05-22 22:56:27 +00:00
|
|
|
itself. If the errbuff is needed by the converter this time,
|
2000-07-31 14:38:10 +00:00
|
|
|
we're hosed - we're out of space! */
|
|
|
|
|
2002-05-22 22:56:27 +00:00
|
|
|
args->converter->charErrorBufferLength = 0;
|
2000-07-31 14:38:10 +00:00
|
|
|
|
2002-05-22 22:56:27 +00:00
|
|
|
ucnv_fromUnicode(args->converter,
|
|
|
|
&newTarget,
|
|
|
|
newTargetLimit,
|
|
|
|
source,
|
|
|
|
sourceLimit,
|
|
|
|
NULL,
|
|
|
|
FALSE,
|
|
|
|
&err2);
|
2000-07-31 14:38:10 +00:00
|
|
|
|
2002-05-22 22:56:27 +00:00
|
|
|
/* We can go ahead and overwrite the length here. We know just how
|
|
|
|
to recalculate it. */
|
|
|
|
|
|
|
|
args->converter->charErrorBufferLength = (int8_t)(
|
|
|
|
newTarget - (char*)args->converter->charErrorBuffer);
|
|
|
|
|
|
|
|
if((newTarget >= newTargetLimit) || (err2 == U_BUFFER_OVERFLOW_ERROR))
|
|
|
|
{
|
|
|
|
/* now we're REALLY in trouble.
|
|
|
|
Internal program error - callback shouldn't have written this much
|
|
|
|
data!
|
|
|
|
*/
|
|
|
|
*err = U_INTERNAL_PROGRAM_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* sub errs could be invalid/truncated/illegal chars or w/e.
|
|
|
|
These might want to be passed on up.. But the problem is, we already
|
|
|
|
need to pass U_BUFFER_OVERFLOW_ERROR. That has to override these
|
|
|
|
other errs.. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
if(U_FAILURE(err2))
|
|
|
|
??
|
|
|
|
*/
|
|
|
|
}
|
2000-07-31 14:38:10 +00:00
|
|
|
}
|
2000-06-28 19:43:17 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
ucnv_cbFromUWriteSub (UConverterFromUnicodeArgs *args,
|
2000-06-30 17:38:47 +00:00
|
|
|
int32_t offsetIndex,
|
2000-12-20 22:54:51 +00:00
|
|
|
UErrorCode * err)
|
2000-06-28 19:43:17 +00:00
|
|
|
{
|
2000-12-20 22:54:51 +00:00
|
|
|
if(U_FAILURE(*err)) {
|
|
|
|
return;
|
2000-07-31 14:38:10 +00:00
|
|
|
}
|
|
|
|
|
2000-12-20 22:54:51 +00:00
|
|
|
if(args->converter->sharedData->impl->writeSub!=NULL) {
|
|
|
|
args->converter->sharedData->impl->writeSub(args, offsetIndex, err);
|
2001-02-26 19:51:20 +00:00
|
|
|
} else if(args->converter->subChar1!=0 && args->converter->invalidUCharBuffer[0]<=0xff) {
|
|
|
|
ucnv_cbFromUWriteBytes(args,
|
|
|
|
(const char *)&args->converter->subChar1, 1,
|
|
|
|
offsetIndex, err);
|
2000-12-20 22:54:51 +00:00
|
|
|
} else {
|
|
|
|
ucnv_cbFromUWriteBytes(args,
|
|
|
|
(const char *)args->converter->subChar, args->converter->subCharLen,
|
|
|
|
offsetIndex, err);
|
2000-07-31 14:38:10 +00:00
|
|
|
}
|
2000-06-28 19:43:17 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
ucnv_cbToUWriteUChars (UConverterToUnicodeArgs *args,
|
2000-07-31 14:38:10 +00:00
|
|
|
const UChar* source,
|
|
|
|
int32_t length,
|
|
|
|
int32_t offsetIndex,
|
|
|
|
UErrorCode * err)
|
2000-06-28 19:43:17 +00:00
|
|
|
{
|
2003-10-25 00:29:13 +00:00
|
|
|
if(U_FAILURE(*err)) {
|
2002-05-22 22:56:27 +00:00
|
|
|
return;
|
2000-07-31 14:38:10 +00:00
|
|
|
}
|
2002-05-22 22:56:27 +00:00
|
|
|
|
2003-10-25 00:29:13 +00:00
|
|
|
ucnv_toUWriteUChars(
|
|
|
|
args->converter,
|
|
|
|
source, length,
|
|
|
|
&args->target, args->targetLimit,
|
|
|
|
&args->offsets, offsetIndex,
|
|
|
|
err);
|
2000-06-28 19:43:17 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
ucnv_cbToUWriteSub (UConverterToUnicodeArgs *args,
|
2000-06-30 17:38:47 +00:00
|
|
|
int32_t offsetIndex,
|
2000-06-28 19:43:17 +00:00
|
|
|
UErrorCode * err)
|
|
|
|
{
|
2002-05-22 22:56:27 +00:00
|
|
|
static const UChar kSubstituteChar1 = 0x1A, kSubstituteChar = 0xFFFD;
|
|
|
|
|
|
|
|
/* could optimize this case, just one uchar */
|
|
|
|
if(args->converter->invalidCharLength == 1 && args->converter->subChar1 != 0) {
|
|
|
|
ucnv_cbToUWriteUChars(args, &kSubstituteChar1, 1, offsetIndex, err);
|
|
|
|
} else {
|
|
|
|
ucnv_cbToUWriteUChars(args, &kSubstituteChar, 1, offsetIndex, err);
|
|
|
|
}
|
2000-06-28 19:43:17 +00:00
|
|
|
}
|
2004-08-26 22:51:40 +00:00
|
|
|
|
|
|
|
#endif
|