2 **********************************************************************
3 * Copyright (C) 2000-2006, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
7 * External APIs for the ICU's codeset conversion library
10 * Modification History:
12 * Date Name Description
13 * 7/28/2000 srl Implementation
17 * @name Character Conversion C API
21 #include "unicode/utypes.h"
23 #if !UCONFIG_NO_CONVERSION
25 #include "unicode/ucnv_cb.h"
30 /* need to update the offsets when the target moves. */
31 /* Note: Recursion may occur in the cb functions, be sure to update the offsets correctly
32 if you don't use ucnv_cbXXX functions. Make sure you don't use the same callback within
33 the same call stack if the complexity arises. */
35 ucnv_cbFromUWriteBytes (UConverterFromUnicodeArgs
*args
,
48 &args
->target
, args
->targetLimit
,
49 &args
->offsets
, offsetIndex
,
54 ucnv_cbFromUWriteUChars(UConverterFromUnicodeArgs
*args
,
56 const UChar
* sourceLimit
,
61 This is a fun one. Recursion can occur - we're basically going to
62 just retry shoving data through the same converter. Note, if you got
63 here through some kind of invalid sequence, you maybe should emit a
64 reset sequence of some kind and/or call ucnv_reset(). Since this
65 IS an actual conversion, take care that you've changed the callback
66 or the data, or you'll get an infinite loop.
68 Please set the err value to something reasonable before calling
79 oldTarget
= args
->target
;
81 ucnv_fromUnicode(args
->converter
,
86 NULL
, /* no offsets */
92 while (args
->target
!= oldTarget
) /* if it moved at all.. */
94 *(args
->offsets
)++ = offsetIndex
;
100 Note, if you did something like used a Stop subcallback, things would get interesting.
101 In fact, here's where we want to return the partially consumed in-source!
103 if(*err
== U_BUFFER_OVERFLOW_ERROR
)
104 /* && (*source < sourceLimit && args->target >= args->targetLimit)
107 /* Overflowed the target. Now, we'll write into the charErrorBuffer.
108 It's a fixed size. If we overflow it... Hmm */
110 const char *newTargetLimit
;
111 UErrorCode err2
= U_ZERO_ERROR
;
115 errBuffLen
= args
->converter
->charErrorBufferLength
;
117 /* start the new target at the first free slot in the errbuff.. */
118 newTarget
= (char *)(args
->converter
->charErrorBuffer
+ errBuffLen
);
120 newTargetLimit
= (char *)(args
->converter
->charErrorBuffer
+
121 sizeof(args
->converter
->charErrorBuffer
));
123 if(newTarget
>= newTargetLimit
)
125 *err
= U_INTERNAL_PROGRAM_ERROR
;
129 /* We're going to tell the converter that the errbuff len is empty.
130 This prevents the existing errbuff from being 'flushed' out onto
131 itself. If the errbuff is needed by the converter this time,
132 we're hosed - we're out of space! */
134 args
->converter
->charErrorBufferLength
= 0;
136 ucnv_fromUnicode(args
->converter
,
145 /* We can go ahead and overwrite the length here. We know just how
146 to recalculate it. */
148 args
->converter
->charErrorBufferLength
= (int8_t)(
149 newTarget
- (char*)args
->converter
->charErrorBuffer
);
151 if((newTarget
>= newTargetLimit
) || (err2
== U_BUFFER_OVERFLOW_ERROR
))
153 /* now we're REALLY in trouble.
154 Internal program error - callback shouldn't have written this much
157 *err
= U_INTERNAL_PROGRAM_ERROR
;
161 /* sub errs could be invalid/truncated/illegal chars or w/e.
162 These might want to be passed on up.. But the problem is, we already
163 need to pass U_BUFFER_OVERFLOW_ERROR. That has to override these
174 U_CAPI
void U_EXPORT2
175 ucnv_cbFromUWriteSub (UConverterFromUnicodeArgs
*args
,
179 UConverter
*converter
;
182 if(U_FAILURE(*err
)) {
185 converter
= args
->converter
;
186 length
= converter
->subCharLen
;
194 * Write/convert the substitution string. Its real length is -length.
195 * Unlike the escape callback, we need not change the converter's
196 * callback function because ucnv_setSubstString() verified that
197 * the string can be converted, so we will not get a conversion error
198 * and will not recurse.
199 * At worst we should get a U_BUFFER_OVERFLOW_ERROR.
201 const UChar
*source
= (const UChar
*)converter
->subChars
;
202 ucnv_cbFromUWriteUChars(args
, &source
, source
- length
, offsetIndex
, err
);
206 if(converter
->sharedData
->impl
->writeSub
!=NULL
) {
207 converter
->sharedData
->impl
->writeSub(args
, offsetIndex
, err
);
209 else if(converter
->subChar1
!=0 && (uint16_t)converter
->invalidUCharBuffer
[0]<=(uint16_t)0xffu
) {
211 TODO: Is this untestable because the MBCS converter has a writeSub function to call
212 and the other converters don't use subChar1?
214 ucnv_cbFromUWriteBytes(args
,
215 (const char *)&converter
->subChar1
, 1,
219 ucnv_cbFromUWriteBytes(args
,
220 (const char *)converter
->subChars
, length
,
225 U_CAPI
void U_EXPORT2
226 ucnv_cbToUWriteUChars (UConverterToUnicodeArgs
*args
,
232 if(U_FAILURE(*err
)) {
239 &args
->target
, args
->targetLimit
,
240 &args
->offsets
, offsetIndex
,
244 U_CAPI
void U_EXPORT2
245 ucnv_cbToUWriteSub (UConverterToUnicodeArgs
*args
,
249 static const UChar kSubstituteChar1
= 0x1A, kSubstituteChar
= 0xFFFD;
251 /* could optimize this case, just one uchar */
252 if(args
->converter
->invalidCharLength
== 1 && args
->converter
->subChar1
!= 0) {
253 ucnv_cbToUWriteUChars(args
, &kSubstituteChar1
, 1, offsetIndex
, err
);
255 ucnv_cbToUWriteUChars(args
, &kSubstituteChar
, 1, offsetIndex
, err
);