2 ******************************************************************************
4 * Copyright (C) 1998-2006,2008 International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
10 * Implements APIs for the ICU's codeset conversion library;
11 * mostly calls through internal functions;
12 * created by Bertrand A. Damiba
14 * Modification History:
16 * Date Name Description
17 * 04/04/99 helena Fixed internal header inclusion.
18 * 05/09/00 helena Added implementation to handle fallback mappings.
19 * 06/20/2000 helena OS/400 port changes; mostly typecast.
22 #include "unicode/utypes.h"
24 #if !UCONFIG_NO_CONVERSION
26 #include "unicode/ustring.h"
27 #include "unicode/ucnv.h"
28 #include "unicode/ucnv_err.h"
29 #include "unicode/uset.h"
40 /* size of intermediate and preflighting buffers in ucnv_convert() */
41 #define CHUNK_SIZE 1024
43 typedef struct UAmbiguousConverter
{
45 const UChar variant5c
;
46 } UAmbiguousConverter
;
48 static const UAmbiguousConverter ambiguousConverters
[]={
49 { "ibm-942_P120-1999", 0xa5 },
50 { "ibm-943_P130-1999", 0xa5 },
51 { "ibm-897_P100-1995", 0xa5 },
52 { "ibm-33722_P120-1999", 0xa5 },
53 { "ibm-949_P110-1999", 0x20a9 },
54 { "ibm-1363_P110-1997", 0x20a9 },
55 { "ISO_2022,locale=ko,version=0", 0x20a9 }
58 /*Calls through createConverter */
59 U_CAPI UConverter
* U_EXPORT2
60 ucnv_open (const char *name
,
65 if (err
== NULL
|| U_FAILURE (*err
)) {
69 r
= ucnv_createConverter(NULL
, name
, err
);
73 U_CAPI UConverter
* U_EXPORT2
74 ucnv_openPackage (const char *packageName
, const char *converterName
, UErrorCode
* err
)
76 return ucnv_createConverterFromPackage(packageName
, converterName
, err
);
79 /*Extracts the UChar* to a char* and calls through createConverter */
80 U_CAPI UConverter
* U_EXPORT2
81 ucnv_openU (const UChar
* name
,
84 char asciiName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
86 if (err
== NULL
|| U_FAILURE(*err
))
89 return ucnv_open (NULL
, err
);
90 if (u_strlen(name
) >= UCNV_MAX_CONVERTER_NAME_LENGTH
)
92 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
95 return ucnv_open(u_austrcpy(asciiName
, name
), err
);
98 /* Copy the string that is represented by the UConverterPlatform enum
99 * @param platformString An output buffer
100 * @param platform An enum representing a platform
101 * @return the length of the copied string.
104 ucnv_copyPlatformString(char *platformString
, UConverterPlatform pltfrm
)
109 uprv_strcpy(platformString
, "ibm-");
115 /* default to empty string */
120 /*Assumes a $platform-#codepage.$CONVERTER_FILE_EXTENSION scheme and calls
121 *through createConverter*/
122 U_CAPI UConverter
* U_EXPORT2
123 ucnv_openCCSID (int32_t codepage
,
124 UConverterPlatform platform
,
127 char myName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
130 if (err
== NULL
|| U_FAILURE (*err
))
133 /* ucnv_copyPlatformString could return "ibm-" or "cp" */
134 myNameLen
= ucnv_copyPlatformString(myName
, platform
);
135 T_CString_integerToString(myName
+ myNameLen
, codepage
, 10);
137 return ucnv_createConverter(NULL
, myName
, err
);
140 /* Creating a temporary stack-based object that can be used in one thread,
141 and created from a converter that is shared across threads.
144 U_CAPI UConverter
* U_EXPORT2
145 ucnv_safeClone(const UConverter
* cnv
, void *stackBuffer
, int32_t *pBufferSize
, UErrorCode
*status
)
147 UConverter
*localConverter
, *allocatedConverter
;
148 int32_t bufferSizeNeeded
;
149 char *stackBufferChars
= (char *)stackBuffer
;
151 UConverterToUnicodeArgs toUArgs
= {
152 sizeof(UConverterToUnicodeArgs
),
161 UConverterFromUnicodeArgs fromUArgs
= {
162 sizeof(UConverterFromUnicodeArgs
),
172 UTRACE_ENTRY_OC(UTRACE_UCNV_CLONE
);
174 if (status
== NULL
|| U_FAILURE(*status
)){
175 UTRACE_EXIT_STATUS(status
? *status
: U_ILLEGAL_ARGUMENT_ERROR
);
179 if (!pBufferSize
|| !cnv
){
180 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
181 UTRACE_EXIT_STATUS(*status
);
185 UTRACE_DATA3(UTRACE_OPEN_CLOSE
, "clone converter %s at %p into stackBuffer %p",
186 ucnv_getName(cnv
, status
), cnv
, stackBuffer
);
188 if (cnv
->sharedData
->impl
->safeClone
!= NULL
) {
189 /* call the custom safeClone function for sizing */
190 bufferSizeNeeded
= 0;
191 cnv
->sharedData
->impl
->safeClone(cnv
, NULL
, &bufferSizeNeeded
, status
);
195 /* inherent sizing */
196 bufferSizeNeeded
= sizeof(UConverter
);
199 if (*pBufferSize
<= 0){ /* 'preflighting' request - set needed size into *pBufferSize */
200 *pBufferSize
= bufferSizeNeeded
;
201 UTRACE_EXIT_VALUE(bufferSizeNeeded
);
206 /* Pointers on 64-bit platforms need to be aligned
207 * on a 64-bit boundary in memory.
209 if (U_ALIGNMENT_OFFSET(stackBuffer
) != 0) {
210 int32_t offsetUp
= (int32_t)U_ALIGNMENT_OFFSET_UP(stackBufferChars
);
211 if(*pBufferSize
> offsetUp
) {
212 *pBufferSize
-= offsetUp
;
213 stackBufferChars
+= offsetUp
;
215 /* prevent using the stack buffer but keep the size > 0 so that we do not just preflight */
220 stackBuffer
= (void *)stackBufferChars
;
222 /* Now, see if we must allocate any memory */
223 if (*pBufferSize
< bufferSizeNeeded
|| stackBuffer
== NULL
)
225 /* allocate one here...*/
226 localConverter
= allocatedConverter
= (UConverter
*) uprv_malloc (bufferSizeNeeded
);
228 if(localConverter
== NULL
) {
229 *status
= U_MEMORY_ALLOCATION_ERROR
;
230 UTRACE_EXIT_STATUS(*status
);
234 if (U_SUCCESS(*status
)) {
235 *status
= U_SAFECLONE_ALLOCATED_WARNING
;
238 /* record the fact that memory was allocated */
239 *pBufferSize
= bufferSizeNeeded
;
241 /* just use the stack buffer */
242 localConverter
= (UConverter
*) stackBuffer
;
243 allocatedConverter
= NULL
;
246 uprv_memset(localConverter
, 0, bufferSizeNeeded
);
248 /* Copy initial state */
249 uprv_memcpy(localConverter
, cnv
, sizeof(UConverter
));
250 localConverter
->isCopyLocal
= localConverter
->isExtraLocal
= FALSE
;
252 /* copy the substitution string */
253 if (cnv
->subChars
== (uint8_t *)cnv
->subUChars
) {
254 localConverter
->subChars
= (uint8_t *)localConverter
->subUChars
;
256 localConverter
->subChars
= (uint8_t *)uprv_malloc(UCNV_ERROR_BUFFER_LENGTH
* U_SIZEOF_UCHAR
);
257 if (localConverter
->subChars
== NULL
) {
258 uprv_free(allocatedConverter
);
259 UTRACE_EXIT_STATUS(*status
);
262 uprv_memcpy(localConverter
->subChars
, cnv
->subChars
, UCNV_ERROR_BUFFER_LENGTH
* U_SIZEOF_UCHAR
);
265 /* now either call the safeclone fcn or not */
266 if (cnv
->sharedData
->impl
->safeClone
!= NULL
) {
267 /* call the custom safeClone function */
268 localConverter
= cnv
->sharedData
->impl
->safeClone(cnv
, localConverter
, pBufferSize
, status
);
271 if(localConverter
==NULL
|| U_FAILURE(*status
)) {
272 if (allocatedConverter
!= NULL
&& allocatedConverter
->subChars
!= (uint8_t *)allocatedConverter
->subUChars
) {
273 uprv_free(allocatedConverter
->subChars
);
275 uprv_free(allocatedConverter
);
276 UTRACE_EXIT_STATUS(*status
);
280 /* increment refcount of shared data if needed */
282 Checking whether it's an algorithic converter is okay
283 in multithreaded applications because the value never changes.
284 Don't check referenceCounter for any other value.
286 if (cnv
->sharedData
->referenceCounter
!= ~0) {
287 ucnv_incrementRefCount(cnv
->sharedData
);
290 if(localConverter
== (UConverter
*)stackBuffer
) {
291 /* we're using user provided data - set to not destroy */
292 localConverter
->isCopyLocal
= TRUE
;
295 /* allow callback functions to handle any memory allocation */
296 toUArgs
.converter
= fromUArgs
.converter
= localConverter
;
297 cbErr
= U_ZERO_ERROR
;
298 cnv
->fromCharErrorBehaviour(cnv
->toUContext
, &toUArgs
, NULL
, 0, UCNV_CLONE
, &cbErr
);
299 cbErr
= U_ZERO_ERROR
;
300 cnv
->fromUCharErrorBehaviour(cnv
->fromUContext
, &fromUArgs
, NULL
, 0, 0, UCNV_CLONE
, &cbErr
);
302 UTRACE_EXIT_PTR_STATUS(localConverter
, *status
);
303 return localConverter
;
308 /*Decreases the reference counter in the shared immutable section of the object
309 *and frees the mutable part*/
311 U_CAPI
void U_EXPORT2
312 ucnv_close (UConverter
* converter
)
314 UErrorCode errorCode
= U_ZERO_ERROR
;
316 UTRACE_ENTRY_OC(UTRACE_UCNV_CLOSE
);
318 if (converter
== NULL
)
324 UTRACE_DATA3(UTRACE_OPEN_CLOSE
, "close converter %s at %p, isCopyLocal=%b",
325 ucnv_getName(converter
, &errorCode
), converter
, converter
->isCopyLocal
);
327 /* In order to speed up the close, only call the callbacks when they have been changed.
328 This performance check will only work when the callbacks are set within a shared library
329 or from user code that statically links this code. */
330 /* first, notify the callback functions that the converter is closed */
331 if (converter
->fromCharErrorBehaviour
!= UCNV_TO_U_DEFAULT_CALLBACK
) {
332 UConverterToUnicodeArgs toUArgs
= {
333 sizeof(UConverterToUnicodeArgs
),
343 toUArgs
.converter
= converter
;
344 errorCode
= U_ZERO_ERROR
;
345 converter
->fromCharErrorBehaviour(converter
->toUContext
, &toUArgs
, NULL
, 0, UCNV_CLOSE
, &errorCode
);
347 if (converter
->fromUCharErrorBehaviour
!= UCNV_FROM_U_DEFAULT_CALLBACK
) {
348 UConverterFromUnicodeArgs fromUArgs
= {
349 sizeof(UConverterFromUnicodeArgs
),
358 fromUArgs
.converter
= converter
;
359 errorCode
= U_ZERO_ERROR
;
360 converter
->fromUCharErrorBehaviour(converter
->fromUContext
, &fromUArgs
, NULL
, 0, 0, UCNV_CLOSE
, &errorCode
);
363 if (converter
->sharedData
->impl
->close
!= NULL
) {
364 converter
->sharedData
->impl
->close(converter
);
367 if (converter
->subChars
!= (uint8_t *)converter
->subUChars
) {
368 uprv_free(converter
->subChars
);
372 Checking whether it's an algorithic converter is okay
373 in multithreaded applications because the value never changes.
374 Don't check referenceCounter for any other value.
376 if (converter
->sharedData
->referenceCounter
!= ~0) {
377 ucnv_unloadSharedDataIfReady(converter
->sharedData
);
380 if(!converter
->isCopyLocal
){
381 uprv_free(converter
);
387 /*returns a single Name from the list, will return NULL if out of bounds
389 U_CAPI
const char* U_EXPORT2
390 ucnv_getAvailableName (int32_t n
)
392 if (0 <= n
&& n
<= 0xffff) {
393 UErrorCode err
= U_ZERO_ERROR
;
394 const char *name
= ucnv_bld_getAvailableConverter((uint16_t)n
, &err
);
395 if (U_SUCCESS(err
)) {
402 U_CAPI
int32_t U_EXPORT2
403 ucnv_countAvailable ()
405 UErrorCode err
= U_ZERO_ERROR
;
406 return ucnv_bld_countAvailableConverters(&err
);
409 U_CAPI
void U_EXPORT2
410 ucnv_getSubstChars (const UConverter
* converter
,
415 if (U_FAILURE (*err
))
418 if (converter
->subCharLen
<= 0) {
419 /* Unicode string or empty string from ucnv_setSubstString(). */
424 if (*len
< converter
->subCharLen
) /*not enough space in subChars */
426 *err
= U_INDEX_OUTOFBOUNDS_ERROR
;
430 uprv_memcpy (mySubChar
, converter
->subChars
, converter
->subCharLen
); /*fills in the subchars */
431 *len
= converter
->subCharLen
; /*store # of bytes copied to buffer */
434 U_CAPI
void U_EXPORT2
435 ucnv_setSubstChars (UConverter
* converter
,
436 const char *mySubChar
,
440 if (U_FAILURE (*err
))
443 /*Makes sure that the subChar is within the codepages char length boundaries */
444 if ((len
> converter
->sharedData
->staticData
->maxBytesPerChar
)
445 || (len
< converter
->sharedData
->staticData
->minBytesPerChar
))
447 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
451 uprv_memcpy (converter
->subChars
, mySubChar
, len
); /*copies the subchars */
452 converter
->subCharLen
= len
; /*sets the new len */
455 * There is currently (2001Feb) no separate API to set/get subChar1.
456 * In order to always have subChar written after it is explicitly set,
457 * we set subChar1 to 0.
459 converter
->subChar1
= 0;
464 U_DRAFT
void U_EXPORT2
465 ucnv_setSubstString(UConverter
*cnv
,
469 UAlignedMemory cloneBuffer
[U_CNV_SAFECLONE_BUFFERSIZE
/ sizeof(UAlignedMemory
) + 1];
470 char chars
[UCNV_ERROR_BUFFER_LENGTH
];
474 int32_t cloneSize
, length8
;
476 /* Let the following functions check all arguments. */
477 cloneSize
= sizeof(cloneBuffer
);
478 clone
= ucnv_safeClone(cnv
, cloneBuffer
, &cloneSize
, err
);
479 ucnv_setFromUCallBack(clone
, UCNV_FROM_U_CALLBACK_STOP
, NULL
, NULL
, NULL
, err
);
480 length8
= ucnv_fromUChars(clone
, chars
, (int32_t)sizeof(chars
), s
, length
, err
);
482 if (U_FAILURE(*err
)) {
486 if (cnv
->sharedData
->impl
->writeSub
== NULL
487 #if !UCONFIG_NO_LEGACY_CONVERSION
488 || (cnv
->sharedData
->staticData
->conversionType
== UCNV_MBCS
&&
489 ucnv_MBCSGetType(cnv
) != UCNV_EBCDIC_STATEFUL
)
492 /* The converter is not stateful. Store the charset bytes as a fixed string. */
493 subChars
= (uint8_t *)chars
;
496 * The converter has a non-default writeSub() function, indicating
497 * that it is stateful.
498 * Store the Unicode string for on-the-fly conversion for correct
501 if (length
> UCNV_ERROR_BUFFER_LENGTH
) {
503 * Should not occur. The converter should output at least one byte
504 * per UChar, which means that ucnv_fromUChars() should catch all
507 *err
= U_BUFFER_OVERFLOW_ERROR
;
510 subChars
= (uint8_t *)s
;
512 length
= u_strlen(s
);
514 length8
= length
* U_SIZEOF_UCHAR
;
518 * For storing the substitution string, select either the small buffer inside
519 * UConverter or allocate a subChars buffer.
521 if (length8
> UCNV_MAX_SUBCHAR_LEN
) {
522 /* Use a separate buffer for the string. Outside UConverter to not make it too large. */
523 if (cnv
->subChars
== (uint8_t *)cnv
->subUChars
) {
524 /* Allocate a new buffer for the string. */
525 cnv
->subChars
= (uint8_t *)uprv_malloc(UCNV_ERROR_BUFFER_LENGTH
* U_SIZEOF_UCHAR
);
526 if (cnv
->subChars
== NULL
) {
527 cnv
->subChars
= (uint8_t *)cnv
->subUChars
;
528 *err
= U_MEMORY_ALLOCATION_ERROR
;
531 uprv_memset(cnv
->subChars
, 0, UCNV_ERROR_BUFFER_LENGTH
* U_SIZEOF_UCHAR
);
535 /* Copy the substitution string into the UConverter or its subChars buffer. */
539 uprv_memcpy(cnv
->subChars
, subChars
, length8
);
540 if (subChars
== (uint8_t *)chars
) {
541 cnv
->subCharLen
= (int8_t)length8
;
542 } else /* subChars == s */ {
543 cnv
->subCharLen
= (int8_t)-length
;
547 /* See comment in ucnv_setSubstChars(). */
551 /*resets the internal states of a converter
552 *goal : have the same behaviour than a freshly created converter
554 static void _reset(UConverter
*converter
, UConverterResetChoice choice
,
555 UBool callCallback
) {
556 if(converter
== NULL
) {
561 /* first, notify the callback functions that the converter is reset */
562 UConverterToUnicodeArgs toUArgs
= {
563 sizeof(UConverterToUnicodeArgs
),
572 UConverterFromUnicodeArgs fromUArgs
= {
573 sizeof(UConverterFromUnicodeArgs
),
582 UErrorCode errorCode
;
584 toUArgs
.converter
= fromUArgs
.converter
= converter
;
585 if(choice
<=UCNV_RESET_TO_UNICODE
) {
586 errorCode
= U_ZERO_ERROR
;
587 converter
->fromCharErrorBehaviour(converter
->toUContext
, &toUArgs
, NULL
, 0, UCNV_RESET
, &errorCode
);
589 if(choice
!=UCNV_RESET_TO_UNICODE
) {
590 errorCode
= U_ZERO_ERROR
;
591 converter
->fromUCharErrorBehaviour(converter
->fromUContext
, &fromUArgs
, NULL
, 0, 0, UCNV_RESET
, &errorCode
);
595 /* now reset the converter itself */
596 if(choice
<=UCNV_RESET_TO_UNICODE
) {
597 converter
->toUnicodeStatus
= converter
->sharedData
->toUnicodeStatus
;
599 converter
->toULength
= 0;
600 converter
->invalidCharLength
= converter
->UCharErrorBufferLength
= 0;
601 converter
->preToULength
= 0;
603 if(choice
!=UCNV_RESET_TO_UNICODE
) {
604 converter
->fromUnicodeStatus
= 0;
605 converter
->fromUChar32
= 0;
606 converter
->invalidUCharLength
= converter
->charErrorBufferLength
= 0;
607 converter
->preFromUFirstCP
= U_SENTINEL
;
608 converter
->preFromULength
= 0;
611 if (converter
->sharedData
->impl
->reset
!= NULL
) {
612 /* call the custom reset function */
613 converter
->sharedData
->impl
->reset(converter
, choice
);
617 U_CAPI
void U_EXPORT2
618 ucnv_reset(UConverter
*converter
)
620 _reset(converter
, UCNV_RESET_BOTH
, TRUE
);
623 U_CAPI
void U_EXPORT2
624 ucnv_resetToUnicode(UConverter
*converter
)
626 _reset(converter
, UCNV_RESET_TO_UNICODE
, TRUE
);
629 U_CAPI
void U_EXPORT2
630 ucnv_resetFromUnicode(UConverter
*converter
)
632 _reset(converter
, UCNV_RESET_FROM_UNICODE
, TRUE
);
635 U_CAPI
int8_t U_EXPORT2
636 ucnv_getMaxCharSize (const UConverter
* converter
)
638 return converter
->maxBytesPerUChar
;
642 U_CAPI
int8_t U_EXPORT2
643 ucnv_getMinCharSize (const UConverter
* converter
)
645 return converter
->sharedData
->staticData
->minBytesPerChar
;
648 U_CAPI
const char* U_EXPORT2
649 ucnv_getName (const UConverter
* converter
, UErrorCode
* err
)
652 if (U_FAILURE (*err
))
654 if(converter
->sharedData
->impl
->getName
){
655 const char* temp
= converter
->sharedData
->impl
->getName(converter
);
659 return converter
->sharedData
->staticData
->name
;
662 U_CAPI
int32_t U_EXPORT2
663 ucnv_getCCSID(const UConverter
* converter
,
667 if (U_FAILURE (*err
))
670 ccsid
= converter
->sharedData
->staticData
->codepage
;
672 /* Rare case. This is for cases like gb18030,
673 which doesn't have an IBM cannonical name, but does have an IBM alias. */
674 const char *standardName
= ucnv_getStandardName(ucnv_getName(converter
, err
), "IBM", err
);
675 if (U_SUCCESS(*err
) && standardName
) {
676 const char *ccsidStr
= uprv_strchr(standardName
, '-');
678 ccsid
= (int32_t)atol(ccsidStr
+1); /* +1 to skip '-' */
686 U_CAPI UConverterPlatform U_EXPORT2
687 ucnv_getPlatform (const UConverter
* converter
,
690 if (U_FAILURE (*err
))
693 return (UConverterPlatform
)converter
->sharedData
->staticData
->platform
;
696 U_CAPI
void U_EXPORT2
697 ucnv_getToUCallBack (const UConverter
* converter
,
698 UConverterToUCallback
*action
,
699 const void **context
)
701 *action
= converter
->fromCharErrorBehaviour
;
702 *context
= converter
->toUContext
;
705 U_CAPI
void U_EXPORT2
706 ucnv_getFromUCallBack (const UConverter
* converter
,
707 UConverterFromUCallback
*action
,
708 const void **context
)
710 *action
= converter
->fromUCharErrorBehaviour
;
711 *context
= converter
->fromUContext
;
714 U_CAPI
void U_EXPORT2
715 ucnv_setToUCallBack (UConverter
* converter
,
716 UConverterToUCallback newAction
,
717 const void* newContext
,
718 UConverterToUCallback
*oldAction
,
719 const void** oldContext
,
722 if (U_FAILURE (*err
))
724 if (oldAction
) *oldAction
= converter
->fromCharErrorBehaviour
;
725 converter
->fromCharErrorBehaviour
= newAction
;
726 if (oldContext
) *oldContext
= converter
->toUContext
;
727 converter
->toUContext
= newContext
;
730 U_CAPI
void U_EXPORT2
731 ucnv_setFromUCallBack (UConverter
* converter
,
732 UConverterFromUCallback newAction
,
733 const void* newContext
,
734 UConverterFromUCallback
*oldAction
,
735 const void** oldContext
,
738 if (U_FAILURE (*err
))
740 if (oldAction
) *oldAction
= converter
->fromUCharErrorBehaviour
;
741 converter
->fromUCharErrorBehaviour
= newAction
;
742 if (oldContext
) *oldContext
= converter
->fromUContext
;
743 converter
->fromUContext
= newContext
;
747 _updateOffsets(int32_t *offsets
, int32_t length
,
748 int32_t sourceIndex
, int32_t errorInputLength
) {
750 int32_t delta
, offset
;
754 * adjust each offset by adding the previous sourceIndex
755 * minus the length of the input sequence that caused an
758 delta
=sourceIndex
-errorInputLength
;
761 * set each offset to -1 because this conversion function
762 * does not handle offsets
767 limit
=offsets
+length
;
769 /* most common case, nothing to do */
771 /* add the delta to each offset (but not if the offset is <0) */
772 while(offsets
<limit
) {
775 *offsets
=offset
+delta
;
779 } else /* delta<0 */ {
781 * set each offset to -1 because this conversion function
782 * does not handle offsets
783 * or the error input sequence started in a previous buffer
785 while(offsets
<limit
) {
791 /* ucnv_fromUnicode --------------------------------------------------------- */
794 * Implementation note for m:n conversions
796 * While collecting source units to find the longest match for m:n conversion,
797 * some source units may need to be stored for a partial match.
798 * When a second buffer does not yield a match on all of the previously stored
799 * source units, then they must be "replayed", i.e., fed back into the converter.
801 * The code relies on the fact that replaying will not nest -
802 * converting a replay buffer will not result in a replay.
803 * This is because a replay is necessary only after the _continuation_ of a
804 * partial match failed, but a replay buffer is converted as a whole.
805 * It may result in some of its units being stored again for a partial match,
806 * but there will not be a continuation _during_ the replay which could fail.
808 * It is conceivable that a callback function could call the converter
809 * recursively in a way that causes another replay to be stored, but that
810 * would be an error in the callback function.
811 * Such violations will cause assertion failures in a debug build,
812 * and wrong output, but they will not cause a crash.
816 _fromUnicodeWithCallback(UConverterFromUnicodeArgs
*pArgs
, UErrorCode
*err
) {
817 UConverterFromUnicode fromUnicode
;
823 int32_t errorInputLength
;
824 UBool converterSawEndOfInput
, calledCallback
;
826 /* variables for m:n conversion */
827 UChar replay
[UCNV_EXT_MAX_UCHARS
];
828 const UChar
*realSource
, *realSourceLimit
;
829 int32_t realSourceIndex
;
832 cnv
=pArgs
->converter
;
835 offsets
=pArgs
->offsets
;
837 /* get the converter implementation function */
840 fromUnicode
=cnv
->sharedData
->impl
->fromUnicode
;
842 fromUnicode
=cnv
->sharedData
->impl
->fromUnicodeWithOffsets
;
843 if(fromUnicode
==NULL
) {
844 /* there is no WithOffsets implementation */
845 fromUnicode
=cnv
->sharedData
->impl
->fromUnicode
;
846 /* we will write -1 for each offset */
851 if(cnv
->preFromULength
>=0) {
855 /* avoid compiler warnings - not otherwise necessary, and the values do not matter */
856 realSourceLimit
=NULL
;
861 * Previous m:n conversion stored source units from a partial match
862 * and failed to consume all of them.
863 * We need to "replay" them from a temporary buffer and convert them first.
865 realSource
=pArgs
->source
;
866 realSourceLimit
=pArgs
->sourceLimit
;
867 realFlush
=pArgs
->flush
;
868 realSourceIndex
=sourceIndex
;
870 uprv_memcpy(replay
, cnv
->preFromU
, -cnv
->preFromULength
*U_SIZEOF_UCHAR
);
871 pArgs
->source
=replay
;
872 pArgs
->sourceLimit
=replay
-cnv
->preFromULength
;
876 cnv
->preFromULength
=0;
880 * loop for conversion and error handling
886 * handle end of input
887 * handle errors/call callback
893 fromUnicode(pArgs
, err
);
896 * set a flag for whether the converter
897 * successfully processed the end of the input
899 * need not check cnv->preFromULength==0 because a replay (<0) will cause
900 * s<sourceLimit before converterSawEndOfInput is checked
902 converterSawEndOfInput
=
903 (UBool
)(U_SUCCESS(*err
) &&
904 pArgs
->flush
&& pArgs
->source
==pArgs
->sourceLimit
&&
905 cnv
->fromUChar32
==0);
907 /* no callback called yet for this iteration */
908 calledCallback
=FALSE
;
910 /* no sourceIndex adjustment for conversion, only for callback output */
914 * loop for offsets and error handling
916 * iterates at most 3 times:
917 * 1. to clean up after the conversion function
918 * 2. after the callback
919 * 3. after the callback again if there was truncated input
922 /* update offsets if we write any */
924 int32_t length
=(int32_t)(pArgs
->target
-t
);
926 _updateOffsets(offsets
, length
, sourceIndex
, errorInputLength
);
929 * if a converter handles offsets and updates the offsets
930 * pointer at the end, then pArgs->offset should not change
932 * however, some converters do not handle offsets at all
933 * (sourceIndex<0) or may not update the offsets pointer
935 pArgs
->offsets
=offsets
+=length
;
939 sourceIndex
+=(int32_t)(pArgs
->source
-s
);
943 if(cnv
->preFromULength
<0) {
945 * switch the source to new replay units (cannot occur while replaying)
946 * after offset handling and before end-of-input and callback handling
948 if(realSource
==NULL
) {
949 realSource
=pArgs
->source
;
950 realSourceLimit
=pArgs
->sourceLimit
;
951 realFlush
=pArgs
->flush
;
952 realSourceIndex
=sourceIndex
;
954 uprv_memcpy(replay
, cnv
->preFromU
, -cnv
->preFromULength
*U_SIZEOF_UCHAR
);
955 pArgs
->source
=replay
;
956 pArgs
->sourceLimit
=replay
-cnv
->preFromULength
;
958 if((sourceIndex
+=cnv
->preFromULength
)<0) {
962 cnv
->preFromULength
=0;
964 /* see implementation note before _fromUnicodeWithCallback() */
965 U_ASSERT(realSource
==NULL
);
966 *err
=U_INTERNAL_PROGRAM_ERROR
;
970 /* update pointers */
974 if(U_SUCCESS(*err
)) {
975 if(s
<pArgs
->sourceLimit
) {
977 * continue with the conversion loop while there is still input left
978 * (continue converting by breaking out of only the inner loop)
981 } else if(realSource
!=NULL
) {
982 /* switch back from replaying to the real source and continue */
983 pArgs
->source
=realSource
;
984 pArgs
->sourceLimit
=realSourceLimit
;
985 pArgs
->flush
=realFlush
;
986 sourceIndex
=realSourceIndex
;
990 } else if(pArgs
->flush
&& cnv
->fromUChar32
!=0) {
992 * the entire input stream is consumed
993 * and there is a partial, truncated input sequence left
996 /* inject an error and continue with callback handling */
997 *err
=U_TRUNCATED_CHAR_FOUND
;
998 calledCallback
=FALSE
; /* new error condition */
1000 /* input consumed */
1003 * return to the conversion loop once more if the flush
1004 * flag is set and the conversion function has not
1005 * successfully processed the end of the input yet
1007 * (continue converting by breaking out of only the inner loop)
1009 if(!converterSawEndOfInput
) {
1013 /* reset the converter without calling the callback function */
1014 _reset(cnv
, UCNV_RESET_FROM_UNICODE
, FALSE
);
1017 /* done successfully */
1022 /* U_FAILURE(*err) */
1026 if( calledCallback
||
1027 (e
=*err
)==U_BUFFER_OVERFLOW_ERROR
||
1028 (e
!=U_INVALID_CHAR_FOUND
&&
1029 e
!=U_ILLEGAL_CHAR_FOUND
&&
1030 e
!=U_TRUNCATED_CHAR_FOUND
)
1033 * the callback did not or cannot resolve the error:
1034 * set output pointers and return
1036 * the check for buffer overflow is redundant but it is
1037 * a high-runner case and hopefully documents the intent
1040 * if we were replaying, then the replay buffer must be
1041 * copied back into the UConverter
1042 * and the real arguments must be restored
1044 if(realSource
!=NULL
) {
1047 U_ASSERT(cnv
->preFromULength
==0);
1049 length
=(int32_t)(pArgs
->sourceLimit
-pArgs
->source
);
1051 uprv_memcpy(cnv
->preFromU
, pArgs
->source
, length
*U_SIZEOF_UCHAR
);
1052 cnv
->preFromULength
=(int8_t)-length
;
1055 pArgs
->source
=realSource
;
1056 pArgs
->sourceLimit
=realSourceLimit
;
1057 pArgs
->flush
=realFlush
;
1064 /* callback handling */
1068 /* get and write the code point */
1069 codePoint
=cnv
->fromUChar32
;
1071 U16_APPEND_UNSAFE(cnv
->invalidUCharBuffer
, errorInputLength
, codePoint
);
1072 cnv
->invalidUCharLength
=(int8_t)errorInputLength
;
1074 /* set the converter state to deal with the next character */
1077 /* call the callback function */
1078 cnv
->fromUCharErrorBehaviour(cnv
->fromUContext
, pArgs
,
1079 cnv
->invalidUCharBuffer
, errorInputLength
, codePoint
,
1080 *err
==U_INVALID_CHAR_FOUND
? UCNV_UNASSIGNED
: UCNV_ILLEGAL
,
1085 * loop back to the offset handling
1087 * this flag will indicate after offset handling
1088 * that a callback was called;
1089 * if the callback did not resolve the error, then we return
1091 calledCallback
=TRUE
;
1096 U_CAPI
void U_EXPORT2
1097 ucnv_fromUnicode(UConverter
*cnv
,
1098 char **target
, const char *targetLimit
,
1099 const UChar
**source
, const UChar
*sourceLimit
,
1103 UConverterFromUnicodeArgs args
;
1107 /* check parameters */
1108 if(err
==NULL
|| U_FAILURE(*err
)) {
1112 if(cnv
==NULL
|| target
==NULL
|| source
==NULL
) {
1113 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1119 if(sourceLimit
<s
|| targetLimit
<t
) {
1120 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1125 * Make sure that the buffer sizes do not exceed the number range for
1126 * int32_t because some functions use the size (in units or bytes)
1127 * rather than comparing pointers, and because offsets are int32_t values.
1129 * size_t is guaranteed to be unsigned and large enough for the job.
1131 * Return with an error instead of adjusting the limits because we would
1132 * not be able to maintain the semantics that either the source must be
1133 * consumed or the target filled (unless an error occurs).
1134 * An adjustment would be targetLimit=t+0x7fffffff; for example.
1137 ((size_t)(sourceLimit
-s
)>(size_t)0x3fffffff && sourceLimit
>s
) ||
1138 ((size_t)(targetLimit
-t
)>(size_t)0x7fffffff && targetLimit
>t
)
1140 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1144 /* flush the target overflow buffer */
1145 if(cnv
->charErrorBufferLength
>0) {
1149 overflow
=(char *)cnv
->charErrorBuffer
;
1150 length
=cnv
->charErrorBufferLength
;
1153 if(t
==targetLimit
) {
1154 /* the overflow buffer contains too much, keep the rest */
1158 overflow
[j
++]=overflow
[i
++];
1161 cnv
->charErrorBufferLength
=(int8_t)j
;
1163 *err
=U_BUFFER_OVERFLOW_ERROR
;
1167 /* copy the overflow contents to the target */
1170 *offsets
++=-1; /* no source index available for old output */
1174 /* the overflow buffer is completely copied to the target */
1175 cnv
->charErrorBufferLength
=0;
1178 if(!flush
&& s
==sourceLimit
&& cnv
->preFromULength
>=0) {
1179 /* the overflow buffer is emptied and there is no new input: we are done */
1185 * Do not simply return with a buffer overflow error if
1186 * !flush && t==targetLimit
1187 * because it is possible that the source will not generate any output.
1188 * For example, the skip callback may be called;
1189 * it does not output anything.
1192 /* prepare the converter arguments */
1195 args
.offsets
=offsets
;
1197 args
.sourceLimit
=sourceLimit
;
1199 args
.targetLimit
=targetLimit
;
1200 args
.size
=sizeof(args
);
1202 _fromUnicodeWithCallback(&args
, err
);
1204 *source
=args
.source
;
1205 *target
=args
.target
;
1208 /* ucnv_toUnicode() --------------------------------------------------------- */
1211 _toUnicodeWithCallback(UConverterToUnicodeArgs
*pArgs
, UErrorCode
*err
) {
1212 UConverterToUnicode toUnicode
;
1217 int32_t sourceIndex
;
1218 int32_t errorInputLength
;
1219 UBool converterSawEndOfInput
, calledCallback
;
1221 /* variables for m:n conversion */
1222 char replay
[UCNV_EXT_MAX_BYTES
];
1223 const char *realSource
, *realSourceLimit
;
1224 int32_t realSourceIndex
;
1227 cnv
=pArgs
->converter
;
1230 offsets
=pArgs
->offsets
;
1232 /* get the converter implementation function */
1235 toUnicode
=cnv
->sharedData
->impl
->toUnicode
;
1237 toUnicode
=cnv
->sharedData
->impl
->toUnicodeWithOffsets
;
1238 if(toUnicode
==NULL
) {
1239 /* there is no WithOffsets implementation */
1240 toUnicode
=cnv
->sharedData
->impl
->toUnicode
;
1241 /* we will write -1 for each offset */
1246 if(cnv
->preToULength
>=0) {
1250 /* avoid compiler warnings - not otherwise necessary, and the values do not matter */
1251 realSourceLimit
=NULL
;
1256 * Previous m:n conversion stored source units from a partial match
1257 * and failed to consume all of them.
1258 * We need to "replay" them from a temporary buffer and convert them first.
1260 realSource
=pArgs
->source
;
1261 realSourceLimit
=pArgs
->sourceLimit
;
1262 realFlush
=pArgs
->flush
;
1263 realSourceIndex
=sourceIndex
;
1265 uprv_memcpy(replay
, cnv
->preToU
, -cnv
->preToULength
);
1266 pArgs
->source
=replay
;
1267 pArgs
->sourceLimit
=replay
-cnv
->preToULength
;
1271 cnv
->preToULength
=0;
1275 * loop for conversion and error handling
1281 * handle end of input
1282 * handle errors/call callback
1287 if(U_SUCCESS(*err
)) {
1289 toUnicode(pArgs
, err
);
1292 * set a flag for whether the converter
1293 * successfully processed the end of the input
1295 * need not check cnv->preToULength==0 because a replay (<0) will cause
1296 * s<sourceLimit before converterSawEndOfInput is checked
1298 converterSawEndOfInput
=
1299 (UBool
)(U_SUCCESS(*err
) &&
1300 pArgs
->flush
&& pArgs
->source
==pArgs
->sourceLimit
&&
1303 /* handle error from getNextUChar() */
1304 converterSawEndOfInput
=FALSE
;
1307 /* no callback called yet for this iteration */
1308 calledCallback
=FALSE
;
1310 /* no sourceIndex adjustment for conversion, only for callback output */
1314 * loop for offsets and error handling
1316 * iterates at most 3 times:
1317 * 1. to clean up after the conversion function
1318 * 2. after the callback
1319 * 3. after the callback again if there was truncated input
1322 /* update offsets if we write any */
1324 int32_t length
=(int32_t)(pArgs
->target
-t
);
1326 _updateOffsets(offsets
, length
, sourceIndex
, errorInputLength
);
1329 * if a converter handles offsets and updates the offsets
1330 * pointer at the end, then pArgs->offset should not change
1332 * however, some converters do not handle offsets at all
1333 * (sourceIndex<0) or may not update the offsets pointer
1335 pArgs
->offsets
=offsets
+=length
;
1338 if(sourceIndex
>=0) {
1339 sourceIndex
+=(int32_t)(pArgs
->source
-s
);
1343 if(cnv
->preToULength
<0) {
1345 * switch the source to new replay units (cannot occur while replaying)
1346 * after offset handling and before end-of-input and callback handling
1348 if(realSource
==NULL
) {
1349 realSource
=pArgs
->source
;
1350 realSourceLimit
=pArgs
->sourceLimit
;
1351 realFlush
=pArgs
->flush
;
1352 realSourceIndex
=sourceIndex
;
1354 uprv_memcpy(replay
, cnv
->preToU
, -cnv
->preToULength
);
1355 pArgs
->source
=replay
;
1356 pArgs
->sourceLimit
=replay
-cnv
->preToULength
;
1358 if((sourceIndex
+=cnv
->preToULength
)<0) {
1362 cnv
->preToULength
=0;
1364 /* see implementation note before _fromUnicodeWithCallback() */
1365 U_ASSERT(realSource
==NULL
);
1366 *err
=U_INTERNAL_PROGRAM_ERROR
;
1370 /* update pointers */
1374 if(U_SUCCESS(*err
)) {
1375 if(s
<pArgs
->sourceLimit
) {
1377 * continue with the conversion loop while there is still input left
1378 * (continue converting by breaking out of only the inner loop)
1381 } else if(realSource
!=NULL
) {
1382 /* switch back from replaying to the real source and continue */
1383 pArgs
->source
=realSource
;
1384 pArgs
->sourceLimit
=realSourceLimit
;
1385 pArgs
->flush
=realFlush
;
1386 sourceIndex
=realSourceIndex
;
1390 } else if(pArgs
->flush
&& cnv
->toULength
>0) {
1392 * the entire input stream is consumed
1393 * and there is a partial, truncated input sequence left
1396 /* inject an error and continue with callback handling */
1397 *err
=U_TRUNCATED_CHAR_FOUND
;
1398 calledCallback
=FALSE
; /* new error condition */
1400 /* input consumed */
1403 * return to the conversion loop once more if the flush
1404 * flag is set and the conversion function has not
1405 * successfully processed the end of the input yet
1407 * (continue converting by breaking out of only the inner loop)
1409 if(!converterSawEndOfInput
) {
1413 /* reset the converter without calling the callback function */
1414 _reset(cnv
, UCNV_RESET_TO_UNICODE
, FALSE
);
1417 /* done successfully */
1422 /* U_FAILURE(*err) */
1426 if( calledCallback
||
1427 (e
=*err
)==U_BUFFER_OVERFLOW_ERROR
||
1428 (e
!=U_INVALID_CHAR_FOUND
&&
1429 e
!=U_ILLEGAL_CHAR_FOUND
&&
1430 e
!=U_TRUNCATED_CHAR_FOUND
&&
1431 e
!=U_ILLEGAL_ESCAPE_SEQUENCE
&&
1432 e
!=U_UNSUPPORTED_ESCAPE_SEQUENCE
&&
1433 e
!=U_PARSE_ERROR
) /* temporary err to flag empty segment, will be reset to U_ILLEGAL_ESCAPE_SEQUENCE below */
1436 * the callback did not or cannot resolve the error:
1437 * set output pointers and return
1439 * the check for buffer overflow is redundant but it is
1440 * a high-runner case and hopefully documents the intent
1443 * if we were replaying, then the replay buffer must be
1444 * copied back into the UConverter
1445 * and the real arguments must be restored
1447 if(realSource
!=NULL
) {
1450 U_ASSERT(cnv
->preToULength
==0);
1452 length
=(int32_t)(pArgs
->sourceLimit
-pArgs
->source
);
1454 uprv_memcpy(cnv
->preToU
, pArgs
->source
, length
);
1455 cnv
->preToULength
=(int8_t)-length
;
1458 pArgs
->source
=realSource
;
1459 pArgs
->sourceLimit
=realSourceLimit
;
1460 pArgs
->flush
=realFlush
;
1467 /* copy toUBytes[] to invalidCharBuffer[] */
1468 errorInputLength
=cnv
->invalidCharLength
=cnv
->toULength
;
1469 if(errorInputLength
>0) {
1470 uprv_memcpy(cnv
->invalidCharBuffer
, cnv
->toUBytes
, errorInputLength
);
1473 /* set the converter state to deal with the next character */
1476 /* call the callback function */
1478 UConverterCallbackReason reason
;
1479 if (*err
== U_PARSE_ERROR
) { /* Here U_PARSE_ERROR indicates empty segment */
1480 *err
= U_ILLEGAL_ESCAPE_SEQUENCE
;
1481 reason
= UCNV_IRREGULAR
;
1483 reason
= (*err
==U_INVALID_CHAR_FOUND
|| *err
==U_UNSUPPORTED_ESCAPE_SEQUENCE
) ?
1484 UCNV_UNASSIGNED
: UCNV_ILLEGAL
;
1486 cnv
->fromCharErrorBehaviour(cnv
->toUContext
, pArgs
,
1487 cnv
->invalidCharBuffer
, errorInputLength
, reason
, err
);
1491 * loop back to the offset handling
1493 * this flag will indicate after offset handling
1494 * that a callback was called;
1495 * if the callback did not resolve the error, then we return
1497 calledCallback
=TRUE
;
1502 U_CAPI
void U_EXPORT2
1503 ucnv_toUnicode(UConverter
*cnv
,
1504 UChar
**target
, const UChar
*targetLimit
,
1505 const char **source
, const char *sourceLimit
,
1509 UConverterToUnicodeArgs args
;
1513 /* check parameters */
1514 if(err
==NULL
|| U_FAILURE(*err
)) {
1518 if(cnv
==NULL
|| target
==NULL
|| source
==NULL
) {
1519 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1525 if(sourceLimit
<s
|| targetLimit
<t
) {
1526 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1531 * Make sure that the buffer sizes do not exceed the number range for
1532 * int32_t because some functions use the size (in units or bytes)
1533 * rather than comparing pointers, and because offsets are int32_t values.
1535 * size_t is guaranteed to be unsigned and large enough for the job.
1537 * Return with an error instead of adjusting the limits because we would
1538 * not be able to maintain the semantics that either the source must be
1539 * consumed or the target filled (unless an error occurs).
1540 * An adjustment would be sourceLimit=t+0x7fffffff; for example.
1543 ((size_t)(sourceLimit
-s
)>(size_t)0x7fffffff && sourceLimit
>s
) ||
1544 ((size_t)(targetLimit
-t
)>(size_t)0x3fffffff && targetLimit
>t
)
1546 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1550 /* flush the target overflow buffer */
1551 if(cnv
->UCharErrorBufferLength
>0) {
1555 overflow
=cnv
->UCharErrorBuffer
;
1556 length
=cnv
->UCharErrorBufferLength
;
1559 if(t
==targetLimit
) {
1560 /* the overflow buffer contains too much, keep the rest */
1564 overflow
[j
++]=overflow
[i
++];
1567 cnv
->UCharErrorBufferLength
=(int8_t)j
;
1569 *err
=U_BUFFER_OVERFLOW_ERROR
;
1573 /* copy the overflow contents to the target */
1576 *offsets
++=-1; /* no source index available for old output */
1580 /* the overflow buffer is completely copied to the target */
1581 cnv
->UCharErrorBufferLength
=0;
1584 if(!flush
&& s
==sourceLimit
&& cnv
->preToULength
>=0) {
1585 /* the overflow buffer is emptied and there is no new input: we are done */
1591 * Do not simply return with a buffer overflow error if
1592 * !flush && t==targetLimit
1593 * because it is possible that the source will not generate any output.
1594 * For example, the skip callback may be called;
1595 * it does not output anything.
1598 /* prepare the converter arguments */
1601 args
.offsets
=offsets
;
1603 args
.sourceLimit
=sourceLimit
;
1605 args
.targetLimit
=targetLimit
;
1606 args
.size
=sizeof(args
);
1608 _toUnicodeWithCallback(&args
, err
);
1610 *source
=args
.source
;
1611 *target
=args
.target
;
1614 /* ucnv_to/fromUChars() ----------------------------------------------------- */
1616 U_CAPI
int32_t U_EXPORT2
1617 ucnv_fromUChars(UConverter
*cnv
,
1618 char *dest
, int32_t destCapacity
,
1619 const UChar
*src
, int32_t srcLength
,
1620 UErrorCode
*pErrorCode
) {
1621 const UChar
*srcLimit
;
1622 char *originalDest
, *destLimit
;
1625 /* check arguments */
1626 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1631 destCapacity
<0 || (destCapacity
>0 && dest
==NULL
) ||
1632 srcLength
<-1 || (srcLength
!=0 && src
==NULL
)
1634 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1639 ucnv_resetFromUnicode(cnv
);
1642 srcLength
=u_strlen(src
);
1645 srcLimit
=src
+srcLength
;
1646 destLimit
=dest
+destCapacity
;
1648 /* pin the destination limit to U_MAX_PTR; NULL check is for OS/400 */
1649 if(destLimit
<dest
|| (destLimit
==NULL
&& dest
!=NULL
)) {
1650 destLimit
=(char *)U_MAX_PTR(dest
);
1653 /* perform the conversion */
1654 ucnv_fromUnicode(cnv
, &dest
, destLimit
, &src
, srcLimit
, 0, TRUE
, pErrorCode
);
1655 destLength
=(int32_t)(dest
-originalDest
);
1657 /* if an overflow occurs, then get the preflighting length */
1658 if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
1661 destLimit
=buffer
+sizeof(buffer
);
1664 *pErrorCode
=U_ZERO_ERROR
;
1665 ucnv_fromUnicode(cnv
, &dest
, destLimit
, &src
, srcLimit
, 0, TRUE
, pErrorCode
);
1666 destLength
+=(int32_t)(dest
-buffer
);
1667 } while(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
);
1673 return u_terminateChars(originalDest
, destCapacity
, destLength
, pErrorCode
);
1676 U_CAPI
int32_t U_EXPORT2
1677 ucnv_toUChars(UConverter
*cnv
,
1678 UChar
*dest
, int32_t destCapacity
,
1679 const char *src
, int32_t srcLength
,
1680 UErrorCode
*pErrorCode
) {
1681 const char *srcLimit
;
1682 UChar
*originalDest
, *destLimit
;
1685 /* check arguments */
1686 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1691 destCapacity
<0 || (destCapacity
>0 && dest
==NULL
) ||
1692 srcLength
<-1 || (srcLength
!=0 && src
==NULL
))
1694 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1699 ucnv_resetToUnicode(cnv
);
1702 srcLength
=(int32_t)uprv_strlen(src
);
1705 srcLimit
=src
+srcLength
;
1706 destLimit
=dest
+destCapacity
;
1708 /* pin the destination limit to U_MAX_PTR; NULL check is for OS/400 */
1709 if(destLimit
<dest
|| (destLimit
==NULL
&& dest
!=NULL
)) {
1710 destLimit
=(UChar
*)U_MAX_PTR(dest
);
1713 /* perform the conversion */
1714 ucnv_toUnicode(cnv
, &dest
, destLimit
, &src
, srcLimit
, 0, TRUE
, pErrorCode
);
1715 destLength
=(int32_t)(dest
-originalDest
);
1717 /* if an overflow occurs, then get the preflighting length */
1718 if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
)
1722 destLimit
=buffer
+sizeof(buffer
)/U_SIZEOF_UCHAR
;
1725 *pErrorCode
=U_ZERO_ERROR
;
1726 ucnv_toUnicode(cnv
, &dest
, destLimit
, &src
, srcLimit
, 0, TRUE
, pErrorCode
);
1727 destLength
+=(int32_t)(dest
-buffer
);
1729 while(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
);
1735 return u_terminateUChars(originalDest
, destCapacity
, destLength
, pErrorCode
);
1738 /* ucnv_getNextUChar() ------------------------------------------------------ */
1740 U_CAPI UChar32 U_EXPORT2
1741 ucnv_getNextUChar(UConverter
*cnv
,
1742 const char **source
, const char *sourceLimit
,
1744 UConverterToUnicodeArgs args
;
1745 UChar buffer
[U16_MAX_LENGTH
];
1750 /* check parameters */
1751 if(err
==NULL
|| U_FAILURE(*err
)) {
1755 if(cnv
==NULL
|| source
==NULL
) {
1756 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1762 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1767 * Make sure that the buffer sizes do not exceed the number range for
1768 * int32_t because some functions use the size (in units or bytes)
1769 * rather than comparing pointers, and because offsets are int32_t values.
1771 * size_t is guaranteed to be unsigned and large enough for the job.
1773 * Return with an error instead of adjusting the limits because we would
1774 * not be able to maintain the semantics that either the source must be
1775 * consumed or the target filled (unless an error occurs).
1776 * An adjustment would be sourceLimit=t+0x7fffffff; for example.
1778 if(((size_t)(sourceLimit
-s
)>(size_t)0x7fffffff && sourceLimit
>s
)) {
1779 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1785 /* flush the target overflow buffer */
1786 if(cnv
->UCharErrorBufferLength
>0) {
1789 overflow
=cnv
->UCharErrorBuffer
;
1791 length
=cnv
->UCharErrorBufferLength
;
1792 U16_NEXT(overflow
, i
, length
, c
);
1794 /* move the remaining overflow contents up to the beginning */
1795 if((cnv
->UCharErrorBufferLength
=(int8_t)(length
-i
))>0) {
1796 uprv_memmove(cnv
->UCharErrorBuffer
, cnv
->UCharErrorBuffer
+i
,
1797 cnv
->UCharErrorBufferLength
*U_SIZEOF_UCHAR
);
1800 if(!U16_IS_LEAD(c
) || i
<length
) {
1804 * Continue if the overflow buffer contained only a lead surrogate,
1805 * in case the converter outputs single surrogates from complete
1811 * flush==TRUE is implied for ucnv_getNextUChar()
1813 * do not simply return even if s==sourceLimit because the converter may
1814 * not have seen flush==TRUE before
1817 /* prepare the converter arguments */
1822 args
.sourceLimit
=sourceLimit
;
1824 args
.targetLimit
=buffer
+1;
1825 args
.size
=sizeof(args
);
1829 * call the native getNextUChar() implementation if we are
1830 * at a character boundary (toULength==0)
1832 * unlike with _toUnicode(), getNextUChar() implementations must set
1833 * U_TRUNCATED_CHAR_FOUND for truncated input,
1834 * in addition to setting toULength/toUBytes[]
1836 if(cnv
->toULength
==0 && cnv
->sharedData
->impl
->getNextUChar
!=NULL
) {
1837 c
=cnv
->sharedData
->impl
->getNextUChar(&args
, err
);
1838 *source
=s
=args
.source
;
1839 if(*err
==U_INDEX_OUTOFBOUNDS_ERROR
) {
1840 /* reset the converter without calling the callback function */
1841 _reset(cnv
, UCNV_RESET_TO_UNICODE
, FALSE
);
1842 return 0xffff; /* no output */
1843 } else if(U_SUCCESS(*err
) && c
>=0) {
1846 * else fall through to use _toUnicode() because
1847 * UCNV_GET_NEXT_UCHAR_USE_TO_U: the native function did not want to handle it after all
1848 * U_FAILURE: call _toUnicode() for callback handling (do not output c)
1853 /* convert to one UChar in buffer[0], or handle getNextUChar() errors */
1854 _toUnicodeWithCallback(&args
, err
);
1856 if(*err
==U_BUFFER_OVERFLOW_ERROR
) {
1861 length
=(int32_t)(args
.target
-buffer
);
1863 /* write the lead surrogate from the overflow buffer */
1865 args
.target
=buffer
+1;
1870 /* buffer contents starts at i and ends before length */
1872 if(U_FAILURE(*err
)) {
1873 c
=0xffff; /* no output */
1874 } else if(length
==0) {
1875 /* no input or only state changes */
1876 *err
=U_INDEX_OUTOFBOUNDS_ERROR
;
1877 /* no need to reset explicitly because _toUnicodeWithCallback() did it */
1878 c
=0xffff; /* no output */
1882 if(!U16_IS_LEAD(c
)) {
1883 /* consume c=buffer[0], done */
1885 /* got a lead surrogate, see if a trail surrogate follows */
1888 if(cnv
->UCharErrorBufferLength
>0) {
1889 /* got overflow output from the conversion */
1890 if(U16_IS_TRAIL(c2
=cnv
->UCharErrorBuffer
[0])) {
1891 /* got a trail surrogate, too */
1892 c
=U16_GET_SUPPLEMENTARY(c
, c2
);
1894 /* move the remaining overflow contents up to the beginning */
1895 if((--cnv
->UCharErrorBufferLength
)>0) {
1896 uprv_memmove(cnv
->UCharErrorBuffer
, cnv
->UCharErrorBuffer
+1,
1897 cnv
->UCharErrorBufferLength
*U_SIZEOF_UCHAR
);
1900 /* c is an unpaired lead surrogate, just return it */
1902 } else if(args
.source
<sourceLimit
) {
1903 /* convert once more, to buffer[1] */
1904 args
.targetLimit
=buffer
+2;
1905 _toUnicodeWithCallback(&args
, err
);
1906 if(*err
==U_BUFFER_OVERFLOW_ERROR
) {
1910 length
=(int32_t)(args
.target
-buffer
);
1911 if(U_SUCCESS(*err
) && length
==2 && U16_IS_TRAIL(c2
=buffer
[1])) {
1912 /* got a trail surrogate, too */
1913 c
=U16_GET_SUPPLEMENTARY(c
, c2
);
1921 * move leftover output from buffer[i..length[
1922 * into the beginning of the overflow buffer
1925 /* move further overflow back */
1926 int32_t delta
=length
-i
;
1927 if((length
=cnv
->UCharErrorBufferLength
)>0) {
1928 uprv_memmove(cnv
->UCharErrorBuffer
+delta
, cnv
->UCharErrorBuffer
,
1929 length
*U_SIZEOF_UCHAR
);
1931 cnv
->UCharErrorBufferLength
=(int8_t)(length
+delta
);
1933 cnv
->UCharErrorBuffer
[0]=buffer
[i
++];
1935 cnv
->UCharErrorBuffer
[1]=buffer
[i
];
1939 *source
=args
.source
;
1943 /* ucnv_convert() and siblings ---------------------------------------------- */
1945 U_CAPI
void U_EXPORT2
1946 ucnv_convertEx(UConverter
*targetCnv
, UConverter
*sourceCnv
,
1947 char **target
, const char *targetLimit
,
1948 const char **source
, const char *sourceLimit
,
1949 UChar
*pivotStart
, UChar
**pivotSource
,
1950 UChar
**pivotTarget
, const UChar
*pivotLimit
,
1951 UBool reset
, UBool flush
,
1952 UErrorCode
*pErrorCode
) {
1953 UChar pivotBuffer
[CHUNK_SIZE
];
1954 UChar
*myPivotSource
, *myPivotTarget
;
1956 /* error checking */
1957 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1961 if( targetCnv
==NULL
|| sourceCnv
==NULL
||
1962 source
==NULL
|| *source
==NULL
||
1963 target
==NULL
|| *target
==NULL
|| targetLimit
==NULL
1965 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1969 if(pivotStart
==NULL
) {
1971 /* streaming conversion requires an explicit pivot buffer */
1972 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1976 /* use the stack pivot buffer */
1977 pivotStart
=myPivotSource
=myPivotTarget
=pivotBuffer
;
1978 pivotSource
=&myPivotSource
;
1979 pivotTarget
=&myPivotTarget
;
1980 pivotLimit
=pivotBuffer
+CHUNK_SIZE
;
1981 } else if( pivotStart
>=pivotLimit
||
1982 pivotSource
==NULL
|| *pivotSource
==NULL
||
1983 pivotTarget
==NULL
|| *pivotTarget
==NULL
||
1986 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1990 if(sourceLimit
==NULL
) {
1991 /* get limit of single-byte-NUL-terminated source string */
1992 sourceLimit
=uprv_strchr(*source
, 0);
1996 ucnv_resetToUnicode(sourceCnv
);
1997 ucnv_resetFromUnicode(targetCnv
);
1998 *pivotTarget
=*pivotSource
=pivotStart
;
2001 /* conversion loop */
2005 * if we did a reset in this function, we know that there is nothing
2006 * to convert to the target yet, so we save a function call
2011 * convert to the target first in case the pivot is filled at entry
2012 * or the targetCnv has some output bytes in its state
2014 ucnv_fromUnicode(targetCnv
,
2015 target
, targetLimit
,
2016 (const UChar
**)pivotSource
, *pivotTarget
,
2018 (UBool
)(flush
&& *source
==sourceLimit
),
2020 if(U_FAILURE(*pErrorCode
)) {
2024 /* ucnv_fromUnicode() must have consumed the pivot contents since it returned with U_SUCCESS() */
2025 *pivotSource
=*pivotTarget
=pivotStart
;
2028 /* convert from the source to the pivot */
2029 ucnv_toUnicode(sourceCnv
,
2030 pivotTarget
, pivotLimit
,
2031 source
, sourceLimit
,
2035 if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
2036 /* pivot overflow: continue with the conversion loop */
2037 *pErrorCode
=U_ZERO_ERROR
;
2038 } else if(U_FAILURE(*pErrorCode
) || *pivotTarget
==pivotStart
) {
2039 /* conversion error, or there was nothing left to convert */
2042 /* else ucnv_toUnicode() wrote into the pivot buffer: continue */
2046 * The conversion loop is exited when one of the following is true:
2047 * - the entire source text has been converted successfully to the target buffer
2048 * - a target buffer overflow occurred
2049 * - a conversion error occurred
2052 /* terminate the target buffer if possible */
2053 if(flush
&& U_SUCCESS(*pErrorCode
)) {
2054 if(*target
!=targetLimit
) {
2056 if(*pErrorCode
==U_STRING_NOT_TERMINATED_WARNING
) {
2057 *pErrorCode
=U_ZERO_ERROR
;
2060 *pErrorCode
=U_STRING_NOT_TERMINATED_WARNING
;
2065 /* internal implementation of ucnv_convert() etc. with preflighting */
2067 ucnv_internalConvert(UConverter
*outConverter
, UConverter
*inConverter
,
2068 char *target
, int32_t targetCapacity
,
2069 const char *source
, int32_t sourceLength
,
2070 UErrorCode
*pErrorCode
) {
2071 UChar pivotBuffer
[CHUNK_SIZE
];
2072 UChar
*pivot
, *pivot2
;
2075 const char *sourceLimit
;
2076 const char *targetLimit
;
2077 int32_t targetLength
=0;
2080 if(sourceLength
<0) {
2081 sourceLimit
=uprv_strchr(source
, 0);
2083 sourceLimit
=source
+sourceLength
;
2086 /* if there is no input data, we're done */
2087 if(source
==sourceLimit
) {
2088 return u_terminateChars(target
, targetCapacity
, 0, pErrorCode
);
2091 pivot
=pivot2
=pivotBuffer
;
2095 if(targetCapacity
>0) {
2096 /* perform real conversion */
2097 targetLimit
=target
+targetCapacity
;
2098 ucnv_convertEx(outConverter
, inConverter
,
2099 &myTarget
, targetLimit
,
2100 &source
, sourceLimit
,
2101 pivotBuffer
, &pivot
, &pivot2
, pivotBuffer
+CHUNK_SIZE
,
2105 targetLength
=(int32_t)(myTarget
-target
);
2109 * If the output buffer is exhausted (or we are only "preflighting"), we need to stop writing
2110 * to it but continue the conversion in order to store in targetCapacity
2111 * the number of bytes that was required.
2113 if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
|| targetCapacity
==0)
2115 char targetBuffer
[CHUNK_SIZE
];
2117 targetLimit
=targetBuffer
+CHUNK_SIZE
;
2119 *pErrorCode
=U_ZERO_ERROR
;
2120 myTarget
=targetBuffer
;
2121 ucnv_convertEx(outConverter
, inConverter
,
2122 &myTarget
, targetLimit
,
2123 &source
, sourceLimit
,
2124 pivotBuffer
, &pivot
, &pivot2
, pivotBuffer
+CHUNK_SIZE
,
2128 targetLength
+=(int32_t)(myTarget
-targetBuffer
);
2129 } while(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
);
2131 /* done with preflighting, set warnings and errors as appropriate */
2132 return u_terminateChars(target
, targetCapacity
, targetLength
, pErrorCode
);
2135 /* no need to call u_terminateChars() because ucnv_convertEx() took care of that */
2136 return targetLength
;
2139 U_CAPI
int32_t U_EXPORT2
2140 ucnv_convert(const char *toConverterName
, const char *fromConverterName
,
2141 char *target
, int32_t targetCapacity
,
2142 const char *source
, int32_t sourceLength
,
2143 UErrorCode
*pErrorCode
) {
2144 UConverter in
, out
; /* stack-allocated */
2145 UConverter
*inConverter
, *outConverter
;
2146 int32_t targetLength
;
2148 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
2152 if( source
==NULL
|| sourceLength
<-1 ||
2153 targetCapacity
<0 || (targetCapacity
>0 && target
==NULL
)
2155 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
2159 /* if there is no input data, we're done */
2160 if(sourceLength
==0 || (sourceLength
<0 && *source
==0)) {
2161 return u_terminateChars(target
, targetCapacity
, 0, pErrorCode
);
2164 /* create the converters */
2165 inConverter
=ucnv_createConverter(&in
, fromConverterName
, pErrorCode
);
2166 if(U_FAILURE(*pErrorCode
)) {
2170 outConverter
=ucnv_createConverter(&out
, toConverterName
, pErrorCode
);
2171 if(U_FAILURE(*pErrorCode
)) {
2172 ucnv_close(inConverter
);
2176 targetLength
=ucnv_internalConvert(outConverter
, inConverter
,
2177 target
, targetCapacity
,
2178 source
, sourceLength
,
2181 ucnv_close(inConverter
);
2182 ucnv_close(outConverter
);
2184 return targetLength
;
2189 ucnv_convertAlgorithmic(UBool convertToAlgorithmic
,
2190 UConverterType algorithmicType
,
2192 char *target
, int32_t targetCapacity
,
2193 const char *source
, int32_t sourceLength
,
2194 UErrorCode
*pErrorCode
) {
2195 UConverter algoConverterStatic
; /* stack-allocated */
2196 UConverter
*algoConverter
, *to
, *from
;
2197 int32_t targetLength
;
2199 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
2203 if( cnv
==NULL
|| source
==NULL
|| sourceLength
<-1 ||
2204 targetCapacity
<0 || (targetCapacity
>0 && target
==NULL
)
2206 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
2210 /* if there is no input data, we're done */
2211 if(sourceLength
==0 || (sourceLength
<0 && *source
==0)) {
2212 return u_terminateChars(target
, targetCapacity
, 0, pErrorCode
);
2215 /* create the algorithmic converter */
2216 algoConverter
=ucnv_createAlgorithmicConverter(&algoConverterStatic
, algorithmicType
,
2218 if(U_FAILURE(*pErrorCode
)) {
2222 /* reset the other converter */
2223 if(convertToAlgorithmic
) {
2224 /* cnv->Unicode->algo */
2225 ucnv_resetToUnicode(cnv
);
2229 /* algo->Unicode->cnv */
2230 ucnv_resetFromUnicode(cnv
);
2235 targetLength
=ucnv_internalConvert(to
, from
,
2236 target
, targetCapacity
,
2237 source
, sourceLength
,
2240 ucnv_close(algoConverter
);
2242 return targetLength
;
2245 U_CAPI
int32_t U_EXPORT2
2246 ucnv_toAlgorithmic(UConverterType algorithmicType
,
2248 char *target
, int32_t targetCapacity
,
2249 const char *source
, int32_t sourceLength
,
2250 UErrorCode
*pErrorCode
) {
2251 return ucnv_convertAlgorithmic(TRUE
, algorithmicType
, cnv
,
2252 target
, targetCapacity
,
2253 source
, sourceLength
,
2257 U_CAPI
int32_t U_EXPORT2
2258 ucnv_fromAlgorithmic(UConverter
*cnv
,
2259 UConverterType algorithmicType
,
2260 char *target
, int32_t targetCapacity
,
2261 const char *source
, int32_t sourceLength
,
2262 UErrorCode
*pErrorCode
) {
2263 return ucnv_convertAlgorithmic(FALSE
, algorithmicType
, cnv
,
2264 target
, targetCapacity
,
2265 source
, sourceLength
,
2269 U_CAPI UConverterType U_EXPORT2
2270 ucnv_getType(const UConverter
* converter
)
2272 int8_t type
= converter
->sharedData
->staticData
->conversionType
;
2273 #if !UCONFIG_NO_LEGACY_CONVERSION
2274 if(type
== UCNV_MBCS
) {
2275 return ucnv_MBCSGetType(converter
);
2278 return (UConverterType
)type
;
2281 U_CAPI
void U_EXPORT2
2282 ucnv_getStarters(const UConverter
* converter
,
2283 UBool starters
[256],
2286 if (err
== NULL
|| U_FAILURE(*err
)) {
2290 if(converter
->sharedData
->impl
->getStarters
!= NULL
) {
2291 converter
->sharedData
->impl
->getStarters(converter
, starters
, err
);
2293 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
2297 static const UAmbiguousConverter
*ucnv_getAmbiguous(const UConverter
*cnv
)
2299 UErrorCode errorCode
;
2307 errorCode
=U_ZERO_ERROR
;
2308 name
=ucnv_getName(cnv
, &errorCode
);
2309 if(U_FAILURE(errorCode
)) {
2313 for(i
=0; i
<(int32_t)(sizeof(ambiguousConverters
)/sizeof(UAmbiguousConverter
)); ++i
)
2315 if(0==uprv_strcmp(name
, ambiguousConverters
[i
].name
))
2317 return ambiguousConverters
+i
;
2324 U_CAPI
void U_EXPORT2
2325 ucnv_fixFileSeparator(const UConverter
*cnv
,
2327 int32_t sourceLength
) {
2328 const UAmbiguousConverter
*a
;
2332 if(cnv
==NULL
|| source
==NULL
|| sourceLength
<=0 || (a
=ucnv_getAmbiguous(cnv
))==NULL
)
2337 variant5c
=a
->variant5c
;
2338 for(i
=0; i
<sourceLength
; ++i
) {
2339 if(source
[i
]==variant5c
) {
2345 U_CAPI UBool U_EXPORT2
2346 ucnv_isAmbiguous(const UConverter
*cnv
) {
2347 return (UBool
)(ucnv_getAmbiguous(cnv
)!=NULL
);
2350 U_CAPI
void U_EXPORT2
2351 ucnv_setFallback(UConverter
*cnv
, UBool usesFallback
)
2353 cnv
->useFallback
= usesFallback
;
2356 U_CAPI UBool U_EXPORT2
2357 ucnv_usesFallback(const UConverter
*cnv
)
2359 return cnv
->useFallback
;
2362 U_CAPI
void U_EXPORT2
2363 ucnv_getInvalidChars (const UConverter
* converter
,
2368 if (err
== NULL
|| U_FAILURE(*err
))
2372 if (len
== NULL
|| errBytes
== NULL
|| converter
== NULL
)
2374 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
2377 if (*len
< converter
->invalidCharLength
)
2379 *err
= U_INDEX_OUTOFBOUNDS_ERROR
;
2382 if ((*len
= converter
->invalidCharLength
) > 0)
2384 uprv_memcpy (errBytes
, converter
->invalidCharBuffer
, *len
);
2388 U_CAPI
void U_EXPORT2
2389 ucnv_getInvalidUChars (const UConverter
* converter
,
2394 if (err
== NULL
|| U_FAILURE(*err
))
2398 if (len
== NULL
|| errChars
== NULL
|| converter
== NULL
)
2400 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
2403 if (*len
< converter
->invalidUCharLength
)
2405 *err
= U_INDEX_OUTOFBOUNDS_ERROR
;
2408 if ((*len
= converter
->invalidUCharLength
) > 0)
2410 uprv_memcpy (errChars
, converter
->invalidUCharBuffer
, sizeof(UChar
) * (*len
));
2414 #define SIG_MAX_LEN 5
2416 U_CAPI
const char* U_EXPORT2
2417 ucnv_detectUnicodeSignature( const char* source
,
2418 int32_t sourceLength
,
2419 int32_t* signatureLength
,
2420 UErrorCode
* pErrorCode
) {
2423 /* initial 0xa5 bytes: make sure that if we read <SIG_MAX_LEN
2424 * bytes we don't misdetect something
2426 char start
[SIG_MAX_LEN
]={ '\xa5', '\xa5', '\xa5', '\xa5', '\xa5' };
2429 if((pErrorCode
==NULL
) || U_FAILURE(*pErrorCode
)){
2433 if(source
== NULL
|| sourceLength
< -1){
2434 *pErrorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
2438 if(signatureLength
== NULL
) {
2439 signatureLength
= &dummy
;
2442 if(sourceLength
==-1){
2443 sourceLength
=(int32_t)uprv_strlen(source
);
2447 while(i
<sourceLength
&& i
<SIG_MAX_LEN
){
2452 if(start
[0] == '\xFE' && start
[1] == '\xFF') {
2455 } else if(start
[0] == '\xFF' && start
[1] == '\xFE') {
2456 if(start
[2] == '\x00' && start
[3] =='\x00') {
2463 } else if(start
[0] == '\xEF' && start
[1] == '\xBB' && start
[2] == '\xBF') {
2466 } else if(start
[0] == '\x00' && start
[1] == '\x00' &&
2467 start
[2] == '\xFE' && start
[3]=='\xFF') {
2470 } else if(start
[0] == '\x0E' && start
[1] == '\xFE' && start
[2] == '\xFF') {
2473 } else if(start
[0] == '\xFB' && start
[1] == '\xEE' && start
[2] == '\x28') {
2476 } else if(start
[0] == '\x2B' && start
[1] == '\x2F' && start
[2] == '\x76') {
2478 * UTF-7: Initial U+FEFF is encoded as +/v8 or +/v9 or +/v+ or +/v/
2479 * depending on the second UTF-16 code unit.
2480 * Detect the entire, closed Unicode mode sequence +/v8- for only U+FEFF
2483 * So far we have +/v
2485 if(start
[3] == '\x38' && start
[4] == '\x2D') {
2489 } else if(start
[3] == '\x38' || start
[3] == '\x39' || start
[3] == '\x2B' || start
[3] == '\x2F') {
2490 /* 4 bytes +/v8 or +/v9 or +/v+ or +/v/ */
2494 }else if(start
[0]=='\xDD' && start
[1]== '\x73'&& start
[2]=='\x66' && start
[3]=='\x73'){
2496 return "UTF-EBCDIC";
2500 /* no known Unicode signature byte sequence recognized */
2505 U_DRAFT
int32_t U_EXPORT2
2506 ucnv_fromUCountPending(const UConverter
* cnv
, UErrorCode
* status
){
2508 if(status
== NULL
|| U_FAILURE(*status
)){
2512 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
2516 if(cnv
->preFromULength
> 0){
2517 return U16_LENGTH(cnv
->preFromUFirstCP
)+cnv
->preFromULength
;
2518 }else if(cnv
->preFromULength
< 0){
2519 return -cnv
->preFromULength
;
2520 }else if(cnv
->fromUChar32
> 0){
2522 }else if(cnv
->preFromUFirstCP
>0){
2523 return U16_LENGTH(cnv
->preFromUFirstCP
);
2529 U_DRAFT
int32_t U_EXPORT2
2530 ucnv_toUCountPending(const UConverter
* cnv
, UErrorCode
* status
){
2532 if(status
== NULL
|| U_FAILURE(*status
)){
2536 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
2540 if(cnv
->preToULength
> 0){
2541 return cnv
->preToULength
;
2542 }else if(cnv
->preToULength
< 0){
2543 return -cnv
->preToULength
;
2544 }else if(cnv
->toULength
> 0){
2545 return cnv
->toULength
;
2552 * Hey, Emacs, please set the following:
2555 * indent-tabs-mode: nil