2 ******************************************************************************
4 * Copyright (C) 1998-2004, 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"
41 /* size of intermediate and preflighting buffers in ucnv_convert() */
42 #define CHUNK_SIZE 1024
44 typedef struct UAmbiguousConverter
{
46 const UChar variant5c
;
47 } UAmbiguousConverter
;
49 static const UAmbiguousConverter ambiguousConverters
[]={
50 { "ibm-942_P120-1999", 0xa5 },
51 { "ibm-943_P130-1999", 0xa5 },
52 { "ibm-897_P100-1995", 0xa5 },
53 { "ibm-33722_P120-1999", 0xa5 },
54 { "ibm-949_P110-1999", 0x20a9 },
55 { "ibm-1363_P110-1997", 0x20a9 },
56 { "ISO_2022,locale=ko,version=0", 0x20a9 }
59 U_CAPI
const char* U_EXPORT2
60 ucnv_getDefaultName ()
62 return ucnv_io_getDefaultConverterName();
66 ucnv_setDefaultName (const char *converterName
)
68 ucnv_io_setDefaultConverterName(converterName
);
70 /*Calls through createConverter */
71 U_CAPI UConverter
* U_EXPORT2
72 ucnv_open (const char *name
,
77 if (err
== NULL
|| U_FAILURE (*err
)) {
81 r
= ucnv_createConverter(NULL
, name
, err
);
85 U_CAPI UConverter
* U_EXPORT2
86 ucnv_openPackage (const char *packageName
, const char *converterName
, UErrorCode
* err
)
88 return ucnv_createConverterFromPackage(packageName
, converterName
, err
);
91 /*Extracts the UChar* to a char* and calls through createConverter */
92 U_CAPI UConverter
* U_EXPORT2
93 ucnv_openU (const UChar
* name
,
96 char asciiName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
98 if (err
== NULL
|| U_FAILURE(*err
))
101 return ucnv_open (NULL
, err
);
102 if (u_strlen(name
) >= UCNV_MAX_CONVERTER_NAME_LENGTH
)
104 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
107 return ucnv_open(u_austrcpy(asciiName
, name
), err
);
110 /*Assumes a $platform-#codepage.$CONVERTER_FILE_EXTENSION scheme and calls
111 *through createConverter*/
112 U_CAPI UConverter
* U_EXPORT2
113 ucnv_openCCSID (int32_t codepage
,
114 UConverterPlatform platform
,
117 char myName
[UCNV_MAX_CONVERTER_NAME_LENGTH
];
120 if (err
== NULL
|| U_FAILURE (*err
))
123 /* ucnv_copyPlatformString could return "ibm-" or "cp" */
124 myNameLen
= ucnv_copyPlatformString(myName
, platform
);
125 T_CString_integerToString(myName
+ myNameLen
, codepage
, 10);
127 return ucnv_createConverter(NULL
, myName
, err
);
130 /* Creating a temporary stack-based object that can be used in one thread,
131 and created from a converter that is shared across threads.
134 U_CAPI UConverter
* U_EXPORT2
135 ucnv_safeClone(const UConverter
* cnv
, void *stackBuffer
, int32_t *pBufferSize
, UErrorCode
*status
)
137 UConverter
*localConverter
, *allocatedConverter
;
138 int32_t bufferSizeNeeded
;
139 char *stackBufferChars
= (char *)stackBuffer
;
141 UConverterToUnicodeArgs toUArgs
= {
142 sizeof(UConverterToUnicodeArgs
),
151 UConverterFromUnicodeArgs fromUArgs
= {
152 sizeof(UConverterFromUnicodeArgs
),
162 UTRACE_ENTRY_OC(UTRACE_UCNV_CLONE
);
164 if (status
== NULL
|| U_FAILURE(*status
)){
165 UTRACE_EXIT_STATUS(status
? *status
: U_ILLEGAL_ARGUMENT_ERROR
);
169 if (!pBufferSize
|| !cnv
){
170 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
171 UTRACE_EXIT_STATUS(*status
);
175 UTRACE_DATA3(UTRACE_OPEN_CLOSE
, "clone converter %s at %p into stackBuffer %p",
176 ucnv_getName(cnv
, status
), cnv
, stackBuffer
);
178 if (cnv
->sharedData
->impl
->safeClone
!= NULL
) {
179 /* call the custom safeClone function for sizing */
180 bufferSizeNeeded
= 0;
181 cnv
->sharedData
->impl
->safeClone(cnv
, NULL
, &bufferSizeNeeded
, status
);
185 /* inherent sizing */
186 bufferSizeNeeded
= sizeof(UConverter
);
189 if (*pBufferSize
<= 0){ /* 'preflighting' request - set needed size into *pBufferSize */
190 *pBufferSize
= bufferSizeNeeded
;
191 UTRACE_EXIT_VALUE(bufferSizeNeeded
);
196 /* Pointers on 64-bit platforms need to be aligned
197 * on a 64-bit boundary in memory.
199 if (U_ALIGNMENT_OFFSET(stackBuffer
) != 0) {
200 int32_t offsetUp
= (int32_t)U_ALIGNMENT_OFFSET_UP(stackBufferChars
);
201 if(*pBufferSize
> offsetUp
) {
202 *pBufferSize
-= offsetUp
;
203 stackBufferChars
+= offsetUp
;
205 /* prevent using the stack buffer but keep the size > 0 so that we do not just preflight */
210 stackBuffer
= (void *)stackBufferChars
;
212 /* Now, see if we must allocate any memory */
213 if (*pBufferSize
< bufferSizeNeeded
|| stackBuffer
== NULL
)
215 /* allocate one here...*/
216 localConverter
= allocatedConverter
= (UConverter
*) uprv_malloc (bufferSizeNeeded
);
218 if(localConverter
== NULL
) {
219 *status
= U_MEMORY_ALLOCATION_ERROR
;
220 UTRACE_EXIT_STATUS(*status
);
224 if (U_SUCCESS(*status
)) {
225 *status
= U_SAFECLONE_ALLOCATED_WARNING
;
228 /* record the fact that memory was allocated */
229 *pBufferSize
= bufferSizeNeeded
;
231 /* just use the stack buffer */
232 localConverter
= (UConverter
*) stackBuffer
;
233 allocatedConverter
= NULL
;
236 uprv_memset(localConverter
, 0, bufferSizeNeeded
);
238 /* Copy initial state */
239 uprv_memcpy(localConverter
, cnv
, sizeof(UConverter
));
240 localConverter
->isCopyLocal
= localConverter
->isExtraLocal
= FALSE
;
242 /* now either call the safeclone fcn or not */
243 if (cnv
->sharedData
->impl
->safeClone
!= NULL
) {
244 /* call the custom safeClone function */
245 localConverter
= cnv
->sharedData
->impl
->safeClone(cnv
, localConverter
, pBufferSize
, status
);
248 if(localConverter
==NULL
|| U_FAILURE(*status
)) {
249 uprv_free(allocatedConverter
);
250 UTRACE_EXIT_STATUS(*status
);
254 /* increment refcount of shared data if needed */
256 Checking whether it's an algorithic converter is okay
257 in multithreaded applications because the value never changes.
258 Don't check referenceCounter for any other value.
260 if (cnv
->sharedData
->referenceCounter
!= ~0) {
261 ucnv_incrementRefCount(cnv
->sharedData
);
264 if(localConverter
== (UConverter
*)stackBuffer
) {
265 /* we're using user provided data - set to not destroy */
266 localConverter
->isCopyLocal
= TRUE
;
269 /* allow callback functions to handle any memory allocation */
270 toUArgs
.converter
= fromUArgs
.converter
= localConverter
;
271 cbErr
= U_ZERO_ERROR
;
272 cnv
->fromCharErrorBehaviour(cnv
->toUContext
, &toUArgs
, NULL
, 0, UCNV_CLONE
, &cbErr
);
273 cbErr
= U_ZERO_ERROR
;
274 cnv
->fromUCharErrorBehaviour(cnv
->fromUContext
, &fromUArgs
, NULL
, 0, 0, UCNV_CLONE
, &cbErr
);
276 UTRACE_EXIT_PTR_STATUS(localConverter
, *status
);
277 return localConverter
;
282 /*Decreases the reference counter in the shared immutable section of the object
283 *and frees the mutable part*/
285 U_CAPI
void U_EXPORT2
286 ucnv_close (UConverter
* converter
)
288 /* first, notify the callback functions that the converter is closed */
289 UConverterToUnicodeArgs toUArgs
= {
290 sizeof(UConverterToUnicodeArgs
),
299 UConverterFromUnicodeArgs fromUArgs
= {
300 sizeof(UConverterFromUnicodeArgs
),
309 UErrorCode errorCode
= U_ZERO_ERROR
;
311 UTRACE_ENTRY_OC(UTRACE_UCNV_CLOSE
);
313 if (converter
== NULL
)
319 UTRACE_DATA3(UTRACE_OPEN_CLOSE
, "close converter %s at %p, isCopyLocal=%b",
320 ucnv_getName(converter
, &errorCode
), converter
, converter
->isCopyLocal
);
322 toUArgs
.converter
= fromUArgs
.converter
= converter
;
324 converter
->fromCharErrorBehaviour(converter
->toUContext
, &toUArgs
, NULL
, 0, UCNV_CLOSE
, &errorCode
);
325 errorCode
= U_ZERO_ERROR
;
326 converter
->fromUCharErrorBehaviour(converter
->fromUContext
, &fromUArgs
, NULL
, 0, 0, UCNV_CLOSE
, &errorCode
);
328 if (converter
->sharedData
->impl
->close
!= NULL
) {
329 converter
->sharedData
->impl
->close(converter
);
333 Checking whether it's an algorithic converter is okay
334 in multithreaded applications because the value never changes.
335 Don't check referenceCounter for any other value.
337 if (converter
->sharedData
->referenceCounter
!= ~0) {
338 ucnv_unloadSharedDataIfReady(converter
->sharedData
);
341 if(!converter
->isCopyLocal
){
342 uprv_free (converter
);
348 /*returns a single Name from the list, will return NULL if out of bounds
350 U_CAPI
const char* U_EXPORT2
351 ucnv_getAvailableName (int32_t n
)
353 if (0 <= n
&& n
<= 0xffff) {
354 UErrorCode err
= U_ZERO_ERROR
;
355 const char *name
= ucnv_io_getAvailableConverter((uint16_t)n
, &err
);
356 if (U_SUCCESS(err
)) {
363 U_CAPI
int32_t U_EXPORT2
364 ucnv_countAvailable ()
366 UErrorCode err
= U_ZERO_ERROR
;
367 return ucnv_io_countAvailableConverters(&err
);
370 U_CAPI
uint16_t U_EXPORT2
371 ucnv_countAliases(const char *alias
, UErrorCode
*pErrorCode
)
373 return ucnv_io_countAliases(alias
, pErrorCode
);
377 U_CAPI
const char* U_EXPORT2
378 ucnv_getAlias(const char *alias
, uint16_t n
, UErrorCode
*pErrorCode
)
380 return ucnv_io_getAlias(alias
, n
, pErrorCode
);
383 U_CAPI
void U_EXPORT2
384 ucnv_getAliases(const char *alias
, const char **aliases
, UErrorCode
*pErrorCode
)
386 ucnv_io_getAliases(alias
, 0, aliases
, pErrorCode
);
389 U_CAPI
uint16_t U_EXPORT2
390 ucnv_countStandards(void)
392 UErrorCode err
= U_ZERO_ERROR
;
393 return ucnv_io_countStandards(&err
);
396 U_CAPI
void U_EXPORT2
397 ucnv_getSubstChars (const UConverter
* converter
,
402 if (U_FAILURE (*err
))
405 if (*len
< converter
->subCharLen
) /*not enough space in subChars */
407 *err
= U_INDEX_OUTOFBOUNDS_ERROR
;
411 uprv_memcpy (mySubChar
, converter
->subChar
, converter
->subCharLen
); /*fills in the subchars */
412 *len
= converter
->subCharLen
; /*store # of bytes copied to buffer */
413 uprv_memcpy (mySubChar
, converter
->subChar
, converter
->subCharLen
); /*fills in the subchars */
414 *len
= converter
->subCharLen
; /*store # of bytes copied to buffer */
417 U_CAPI
void U_EXPORT2
418 ucnv_setSubstChars (UConverter
* converter
,
419 const char *mySubChar
,
423 if (U_FAILURE (*err
))
426 /*Makes sure that the subChar is within the codepages char length boundaries */
427 if ((len
> converter
->sharedData
->staticData
->maxBytesPerChar
)
428 || (len
< converter
->sharedData
->staticData
->minBytesPerChar
))
430 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
434 uprv_memcpy (converter
->subChar
, mySubChar
, len
); /*copies the subchars */
435 converter
->subCharLen
= len
; /*sets the new len */
438 * There is currently (2001Feb) no separate API to set/get subChar1.
439 * In order to always have subChar written after it is explicitly set,
440 * we set subChar1 to 0.
442 converter
->subChar1
= 0;
447 /*resets the internal states of a converter
448 *goal : have the same behaviour than a freshly created converter
450 static void _reset(UConverter
*converter
, UConverterResetChoice choice
,
451 UBool callCallback
) {
452 if(converter
== NULL
) {
457 /* first, notify the callback functions that the converter is reset */
458 UConverterToUnicodeArgs toUArgs
= {
459 sizeof(UConverterToUnicodeArgs
),
468 UConverterFromUnicodeArgs fromUArgs
= {
469 sizeof(UConverterFromUnicodeArgs
),
478 UErrorCode errorCode
;
480 toUArgs
.converter
= fromUArgs
.converter
= converter
;
481 if(choice
<=UCNV_RESET_TO_UNICODE
) {
482 errorCode
= U_ZERO_ERROR
;
483 converter
->fromCharErrorBehaviour(converter
->toUContext
, &toUArgs
, NULL
, 0, UCNV_RESET
, &errorCode
);
485 if(choice
!=UCNV_RESET_TO_UNICODE
) {
486 errorCode
= U_ZERO_ERROR
;
487 converter
->fromUCharErrorBehaviour(converter
->fromUContext
, &fromUArgs
, NULL
, 0, 0, UCNV_RESET
, &errorCode
);
491 /* now reset the converter itself */
492 if(choice
<=UCNV_RESET_TO_UNICODE
) {
493 converter
->toUnicodeStatus
= converter
->sharedData
->toUnicodeStatus
;
495 converter
->toULength
= 0;
496 converter
->invalidCharLength
= converter
->UCharErrorBufferLength
= 0;
497 converter
->preToULength
= 0;
499 if(choice
!=UCNV_RESET_TO_UNICODE
) {
500 converter
->fromUnicodeStatus
= 0;
501 converter
->fromUChar32
= 0;
502 converter
->invalidUCharLength
= converter
->charErrorBufferLength
= 0;
503 converter
->preFromUFirstCP
= U_SENTINEL
;
504 converter
->preFromULength
= 0;
507 if (converter
->sharedData
->impl
->reset
!= NULL
) {
508 /* call the custom reset function */
509 converter
->sharedData
->impl
->reset(converter
, choice
);
513 U_CAPI
void U_EXPORT2
514 ucnv_reset(UConverter
*converter
)
516 _reset(converter
, UCNV_RESET_BOTH
, TRUE
);
519 U_CAPI
void U_EXPORT2
520 ucnv_resetToUnicode(UConverter
*converter
)
522 _reset(converter
, UCNV_RESET_TO_UNICODE
, TRUE
);
525 U_CAPI
void U_EXPORT2
526 ucnv_resetFromUnicode(UConverter
*converter
)
528 _reset(converter
, UCNV_RESET_FROM_UNICODE
, TRUE
);
531 U_CAPI
int8_t U_EXPORT2
532 ucnv_getMaxCharSize (const UConverter
* converter
)
534 return converter
->maxBytesPerUChar
;
538 U_CAPI
int8_t U_EXPORT2
539 ucnv_getMinCharSize (const UConverter
* converter
)
541 return converter
->sharedData
->staticData
->minBytesPerChar
;
544 U_CAPI
const char* U_EXPORT2
545 ucnv_getName (const UConverter
* converter
, UErrorCode
* err
)
548 if (U_FAILURE (*err
))
550 if(converter
->sharedData
->impl
->getName
){
551 const char* temp
= converter
->sharedData
->impl
->getName(converter
);
555 return converter
->sharedData
->staticData
->name
;
558 U_CAPI
int32_t U_EXPORT2
559 ucnv_getCCSID(const UConverter
* converter
,
563 if (U_FAILURE (*err
))
566 ccsid
= converter
->sharedData
->staticData
->codepage
;
568 /* Rare case. This is for cases like gb18030,
569 which doesn't have an IBM cannonical name, but does have an IBM alias. */
570 const char *standardName
= ucnv_getStandardName(ucnv_getName(converter
, err
), "IBM", err
);
571 if (U_SUCCESS(*err
) && standardName
) {
572 const char *ccsidStr
= uprv_strchr(standardName
, '-');
574 ccsid
= (int32_t)atol(ccsidStr
+1); /* +1 to skip '-' */
582 U_CAPI UConverterPlatform U_EXPORT2
583 ucnv_getPlatform (const UConverter
* converter
,
586 if (U_FAILURE (*err
))
589 return (UConverterPlatform
)converter
->sharedData
->staticData
->platform
;
592 U_CAPI
void U_EXPORT2
593 ucnv_getToUCallBack (const UConverter
* converter
,
594 UConverterToUCallback
*action
,
595 const void **context
)
597 *action
= converter
->fromCharErrorBehaviour
;
598 *context
= converter
->toUContext
;
601 U_CAPI
void U_EXPORT2
602 ucnv_getFromUCallBack (const UConverter
* converter
,
603 UConverterFromUCallback
*action
,
604 const void **context
)
606 *action
= converter
->fromUCharErrorBehaviour
;
607 *context
= converter
->fromUContext
;
610 U_CAPI
void U_EXPORT2
611 ucnv_setToUCallBack (UConverter
* converter
,
612 UConverterToUCallback newAction
,
613 const void* newContext
,
614 UConverterToUCallback
*oldAction
,
615 const void** oldContext
,
618 if (U_FAILURE (*err
))
620 if (oldAction
) *oldAction
= converter
->fromCharErrorBehaviour
;
621 converter
->fromCharErrorBehaviour
= newAction
;
622 if (oldContext
) *oldContext
= converter
->toUContext
;
623 converter
->toUContext
= newContext
;
626 U_CAPI
void U_EXPORT2
627 ucnv_setFromUCallBack (UConverter
* converter
,
628 UConverterFromUCallback newAction
,
629 const void* newContext
,
630 UConverterFromUCallback
*oldAction
,
631 const void** oldContext
,
634 if (U_FAILURE (*err
))
636 if (oldAction
) *oldAction
= converter
->fromUCharErrorBehaviour
;
637 converter
->fromUCharErrorBehaviour
= newAction
;
638 if (oldContext
) *oldContext
= converter
->fromUContext
;
639 converter
->fromUContext
= newContext
;
643 _updateOffsets(int32_t *offsets
, int32_t length
,
644 int32_t sourceIndex
, int32_t errorInputLength
) {
646 int32_t delta
, offset
;
650 * adjust each offset by adding the previous sourceIndex
651 * minus the length of the input sequence that caused an
654 delta
=sourceIndex
-errorInputLength
;
657 * set each offset to -1 because this conversion function
658 * does not handle offsets
663 limit
=offsets
+length
;
665 /* most common case, nothing to do */
667 /* add the delta to each offset (but not if the offset is <0) */
668 while(offsets
<limit
) {
671 *offsets
=offset
+delta
;
675 } else /* delta<0 */ {
677 * set each offset to -1 because this conversion function
678 * does not handle offsets
679 * or the error input sequence started in a previous buffer
681 while(offsets
<limit
) {
687 /* ucnv_fromUnicode --------------------------------------------------------- */
690 * Implementation note for m:n conversions
692 * While collecting source units to find the longest match for m:n conversion,
693 * some source units may need to be stored for a partial match.
694 * When a second buffer does not yield a match on all of the previously stored
695 * source units, then they must be "replayed", i.e., fed back into the converter.
697 * The code relies on the fact that replaying will not nest -
698 * converting a replay buffer will not result in a replay.
699 * This is because a replay is necessary only after the _continuation_ of a
700 * partial match failed, but a replay buffer is converted as a whole.
701 * It may result in some of its units being stored again for a partial match,
702 * but there will not be a continuation _during_ the replay which could fail.
704 * It is conceivable that a callback function could call the converter
705 * recursively in a way that causes another replay to be stored, but that
706 * would be an error in the callback function.
707 * Such violations will cause assertion failures in a debug build,
708 * and wrong output, but they will not cause a crash.
712 _fromUnicodeWithCallback(UConverterFromUnicodeArgs
*pArgs
, UErrorCode
*err
) {
713 UConverterFromUnicode fromUnicode
;
719 int32_t errorInputLength
;
720 UBool converterSawEndOfInput
, calledCallback
;
722 /* variables for m:n conversion */
723 UChar replay
[UCNV_EXT_MAX_UCHARS
];
724 const UChar
*realSource
, *realSourceLimit
;
725 int32_t realSourceIndex
;
728 cnv
=pArgs
->converter
;
731 offsets
=pArgs
->offsets
;
733 /* get the converter implementation function */
736 fromUnicode
=cnv
->sharedData
->impl
->fromUnicode
;
738 fromUnicode
=cnv
->sharedData
->impl
->fromUnicodeWithOffsets
;
739 if(fromUnicode
==NULL
) {
740 /* there is no WithOffsets implementation */
741 fromUnicode
=cnv
->sharedData
->impl
->fromUnicode
;
742 /* we will write -1 for each offset */
747 if(cnv
->preFromULength
>=0) {
751 /* avoid compiler warnings - not otherwise necessary, and the values do not matter */
752 realSourceLimit
=NULL
;
757 * Previous m:n conversion stored source units from a partial match
758 * and failed to consume all of them.
759 * We need to "replay" them from a temporary buffer and convert them first.
761 realSource
=pArgs
->source
;
762 realSourceLimit
=pArgs
->sourceLimit
;
763 realFlush
=pArgs
->flush
;
764 realSourceIndex
=sourceIndex
;
766 uprv_memcpy(replay
, cnv
->preFromU
, -cnv
->preFromULength
*U_SIZEOF_UCHAR
);
767 pArgs
->source
=replay
;
768 pArgs
->sourceLimit
=replay
-cnv
->preFromULength
;
772 cnv
->preFromULength
=0;
776 * loop for conversion and error handling
782 * handle end of input
783 * handle errors/call callback
789 fromUnicode(pArgs
, err
);
792 * set a flag for whether the converter
793 * successfully processed the end of the input
795 * need not check cnv->preFromULength==0 because a replay (<0) will cause
796 * s<sourceLimit before converterSawEndOfInput is checked
798 converterSawEndOfInput
=
799 (UBool
)(U_SUCCESS(*err
) &&
800 pArgs
->flush
&& pArgs
->source
==pArgs
->sourceLimit
&&
801 cnv
->fromUChar32
==0);
803 /* no callback called yet for this iteration */
804 calledCallback
=FALSE
;
806 /* no sourceIndex adjustment for conversion, only for callback output */
810 * loop for offsets and error handling
812 * iterates at most 3 times:
813 * 1. to clean up after the conversion function
814 * 2. after the callback
815 * 3. after the callback again if there was truncated input
818 /* update offsets if we write any */
820 int32_t length
=(int32_t)(pArgs
->target
-t
);
822 _updateOffsets(offsets
, length
, sourceIndex
, errorInputLength
);
825 * if a converter handles offsets and updates the offsets
826 * pointer at the end, then pArgs->offset should not change
828 * however, some converters do not handle offsets at all
829 * (sourceIndex<0) or may not update the offsets pointer
831 pArgs
->offsets
=offsets
+=length
;
835 sourceIndex
+=(int32_t)(pArgs
->source
-s
);
839 if(cnv
->preFromULength
<0) {
841 * switch the source to new replay units (cannot occur while replaying)
842 * after offset handling and before end-of-input and callback handling
844 if(realSource
==NULL
) {
845 realSource
=pArgs
->source
;
846 realSourceLimit
=pArgs
->sourceLimit
;
847 realFlush
=pArgs
->flush
;
848 realSourceIndex
=sourceIndex
;
850 uprv_memcpy(replay
, cnv
->preFromU
, -cnv
->preFromULength
*U_SIZEOF_UCHAR
);
851 pArgs
->source
=replay
;
852 pArgs
->sourceLimit
=replay
-cnv
->preFromULength
;
854 if((sourceIndex
+=cnv
->preFromULength
)<0) {
858 cnv
->preFromULength
=0;
860 /* see implementation note before _fromUnicodeWithCallback() */
861 U_ASSERT(realSource
==NULL
);
862 *err
=U_INTERNAL_PROGRAM_ERROR
;
866 /* update pointers */
870 if(U_SUCCESS(*err
)) {
871 if(s
<pArgs
->sourceLimit
) {
873 * continue with the conversion loop while there is still input left
874 * (continue converting by breaking out of only the inner loop)
877 } else if(realSource
!=NULL
) {
878 /* switch back from replaying to the real source and continue */
879 pArgs
->source
=realSource
;
880 pArgs
->sourceLimit
=realSourceLimit
;
881 pArgs
->flush
=realFlush
;
882 sourceIndex
=realSourceIndex
;
886 } else if(pArgs
->flush
&& cnv
->fromUChar32
!=0) {
888 * the entire input stream is consumed
889 * and there is a partial, truncated input sequence left
892 /* inject an error and continue with callback handling */
893 *err
=U_TRUNCATED_CHAR_FOUND
;
894 calledCallback
=FALSE
; /* new error condition */
899 * return to the conversion loop once more if the flush
900 * flag is set and the conversion function has not
901 * successfully processed the end of the input yet
903 * (continue converting by breaking out of only the inner loop)
905 if(!converterSawEndOfInput
) {
909 /* reset the converter without calling the callback function */
910 _reset(cnv
, UCNV_RESET_FROM_UNICODE
, FALSE
);
913 /* done successfully */
918 /* U_FAILURE(*err) */
922 if( calledCallback
||
923 (e
=*err
)==U_BUFFER_OVERFLOW_ERROR
||
924 (e
!=U_INVALID_CHAR_FOUND
&&
925 e
!=U_ILLEGAL_CHAR_FOUND
&&
926 e
!=U_TRUNCATED_CHAR_FOUND
)
929 * the callback did not or cannot resolve the error:
930 * set output pointers and return
932 * the check for buffer overflow is redundant but it is
933 * a high-runner case and hopefully documents the intent
936 * if we were replaying, then the replay buffer must be
937 * copied back into the UConverter
938 * and the real arguments must be restored
940 if(realSource
!=NULL
) {
943 U_ASSERT(cnv
->preFromULength
==0);
945 length
=(int32_t)(pArgs
->sourceLimit
-pArgs
->source
);
947 uprv_memcpy(cnv
->preFromU
, pArgs
->source
, length
*U_SIZEOF_UCHAR
);
948 cnv
->preFromULength
=(int8_t)-length
;
951 pArgs
->source
=realSource
;
952 pArgs
->sourceLimit
=realSourceLimit
;
953 pArgs
->flush
=realFlush
;
960 /* callback handling */
964 /* get and write the code point */
965 codePoint
=cnv
->fromUChar32
;
967 U16_APPEND_UNSAFE(cnv
->invalidUCharBuffer
, errorInputLength
, codePoint
);
968 cnv
->invalidUCharLength
=(int8_t)errorInputLength
;
970 /* set the converter state to deal with the next character */
973 /* call the callback function */
974 cnv
->fromUCharErrorBehaviour(cnv
->fromUContext
, pArgs
,
975 cnv
->invalidUCharBuffer
, errorInputLength
, codePoint
,
976 *err
==U_INVALID_CHAR_FOUND
? UCNV_UNASSIGNED
: UCNV_ILLEGAL
,
981 * loop back to the offset handling
983 * this flag will indicate after offset handling
984 * that a callback was called;
985 * if the callback did not resolve the error, then we return
992 U_CAPI
void U_EXPORT2
993 ucnv_fromUnicode(UConverter
*cnv
,
994 char **target
, const char *targetLimit
,
995 const UChar
**source
, const UChar
*sourceLimit
,
999 UConverterFromUnicodeArgs args
;
1003 /* check parameters */
1004 if(err
==NULL
|| U_FAILURE(*err
)) {
1008 if(cnv
==NULL
|| target
==NULL
|| source
==NULL
) {
1009 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1015 if(sourceLimit
<s
|| targetLimit
<t
) {
1016 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1021 * Make sure that the buffer sizes do not exceed the number range for
1022 * int32_t because some functions use the size (in units or bytes)
1023 * rather than comparing pointers, and because offsets are int32_t values.
1025 * size_t is guaranteed to be unsigned and large enough for the job.
1027 * Return with an error instead of adjusting the limits because we would
1028 * not be able to maintain the semantics that either the source must be
1029 * consumed or the target filled (unless an error occurs).
1030 * An adjustment would be targetLimit=t+0x7fffffff; for example.
1033 ((size_t)(sourceLimit
-s
)>(size_t)0x3fffffff && sourceLimit
>s
) ||
1034 ((size_t)(targetLimit
-t
)>(size_t)0x7fffffff && targetLimit
>t
)
1036 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1040 /* flush the target overflow buffer */
1041 if(cnv
->charErrorBufferLength
>0) {
1045 overflow
=(char *)cnv
->charErrorBuffer
;
1046 length
=cnv
->charErrorBufferLength
;
1049 if(t
==targetLimit
) {
1050 /* the overflow buffer contains too much, keep the rest */
1054 overflow
[j
++]=overflow
[i
++];
1057 cnv
->charErrorBufferLength
=(int8_t)j
;
1059 *err
=U_BUFFER_OVERFLOW_ERROR
;
1063 /* copy the overflow contents to the target */
1066 *offsets
++=-1; /* no source index available for old output */
1070 /* the overflow buffer is completely copied to the target */
1071 cnv
->charErrorBufferLength
=0;
1074 if(!flush
&& s
==sourceLimit
&& cnv
->preFromULength
>=0) {
1075 /* the overflow buffer is emptied and there is no new input: we are done */
1081 * Do not simply return with a buffer overflow error if
1082 * !flush && t==targetLimit
1083 * because it is possible that the source will not generate any output.
1084 * For example, the skip callback may be called;
1085 * it does not output anything.
1088 /* prepare the converter arguments */
1091 args
.offsets
=offsets
;
1093 args
.sourceLimit
=sourceLimit
;
1095 args
.targetLimit
=targetLimit
;
1096 args
.size
=sizeof(args
);
1098 _fromUnicodeWithCallback(&args
, err
);
1100 *source
=args
.source
;
1101 *target
=args
.target
;
1104 /* ucnv_toUnicode() --------------------------------------------------------- */
1107 _toUnicodeWithCallback(UConverterToUnicodeArgs
*pArgs
, UErrorCode
*err
) {
1108 UConverterToUnicode toUnicode
;
1113 int32_t sourceIndex
;
1114 int32_t errorInputLength
;
1115 UBool converterSawEndOfInput
, calledCallback
;
1117 /* variables for m:n conversion */
1118 char replay
[UCNV_EXT_MAX_BYTES
];
1119 const char *realSource
, *realSourceLimit
;
1120 int32_t realSourceIndex
;
1123 cnv
=pArgs
->converter
;
1126 offsets
=pArgs
->offsets
;
1128 /* get the converter implementation function */
1131 toUnicode
=cnv
->sharedData
->impl
->toUnicode
;
1133 toUnicode
=cnv
->sharedData
->impl
->toUnicodeWithOffsets
;
1134 if(toUnicode
==NULL
) {
1135 /* there is no WithOffsets implementation */
1136 toUnicode
=cnv
->sharedData
->impl
->toUnicode
;
1137 /* we will write -1 for each offset */
1142 if(cnv
->preToULength
>=0) {
1146 /* avoid compiler warnings - not otherwise necessary, and the values do not matter */
1147 realSourceLimit
=NULL
;
1152 * Previous m:n conversion stored source units from a partial match
1153 * and failed to consume all of them.
1154 * We need to "replay" them from a temporary buffer and convert them first.
1156 realSource
=pArgs
->source
;
1157 realSourceLimit
=pArgs
->sourceLimit
;
1158 realFlush
=pArgs
->flush
;
1159 realSourceIndex
=sourceIndex
;
1161 uprv_memcpy(replay
, cnv
->preToU
, -cnv
->preToULength
);
1162 pArgs
->source
=replay
;
1163 pArgs
->sourceLimit
=replay
-cnv
->preToULength
;
1167 cnv
->preToULength
=0;
1171 * loop for conversion and error handling
1177 * handle end of input
1178 * handle errors/call callback
1183 if(U_SUCCESS(*err
)) {
1185 toUnicode(pArgs
, err
);
1188 * set a flag for whether the converter
1189 * successfully processed the end of the input
1191 * need not check cnv->preToULength==0 because a replay (<0) will cause
1192 * s<sourceLimit before converterSawEndOfInput is checked
1194 converterSawEndOfInput
=
1195 (UBool
)(U_SUCCESS(*err
) &&
1196 pArgs
->flush
&& pArgs
->source
==pArgs
->sourceLimit
&&
1199 /* handle error from getNextUChar() */
1200 converterSawEndOfInput
=FALSE
;
1203 /* no callback called yet for this iteration */
1204 calledCallback
=FALSE
;
1206 /* no sourceIndex adjustment for conversion, only for callback output */
1210 * loop for offsets and error handling
1212 * iterates at most 3 times:
1213 * 1. to clean up after the conversion function
1214 * 2. after the callback
1215 * 3. after the callback again if there was truncated input
1218 /* update offsets if we write any */
1220 int32_t length
=(int32_t)(pArgs
->target
-t
);
1222 _updateOffsets(offsets
, length
, sourceIndex
, errorInputLength
);
1225 * if a converter handles offsets and updates the offsets
1226 * pointer at the end, then pArgs->offset should not change
1228 * however, some converters do not handle offsets at all
1229 * (sourceIndex<0) or may not update the offsets pointer
1231 pArgs
->offsets
=offsets
+=length
;
1234 if(sourceIndex
>=0) {
1235 sourceIndex
+=(int32_t)(pArgs
->source
-s
);
1239 if(cnv
->preToULength
<0) {
1241 * switch the source to new replay units (cannot occur while replaying)
1242 * after offset handling and before end-of-input and callback handling
1244 if(realSource
==NULL
) {
1245 realSource
=pArgs
->source
;
1246 realSourceLimit
=pArgs
->sourceLimit
;
1247 realFlush
=pArgs
->flush
;
1248 realSourceIndex
=sourceIndex
;
1250 uprv_memcpy(replay
, cnv
->preToU
, -cnv
->preToULength
);
1251 pArgs
->source
=replay
;
1252 pArgs
->sourceLimit
=replay
-cnv
->preToULength
;
1254 if((sourceIndex
+=cnv
->preToULength
)<0) {
1258 cnv
->preToULength
=0;
1260 /* see implementation note before _fromUnicodeWithCallback() */
1261 U_ASSERT(realSource
==NULL
);
1262 *err
=U_INTERNAL_PROGRAM_ERROR
;
1266 /* update pointers */
1270 if(U_SUCCESS(*err
)) {
1271 if(s
<pArgs
->sourceLimit
) {
1273 * continue with the conversion loop while there is still input left
1274 * (continue converting by breaking out of only the inner loop)
1277 } else if(realSource
!=NULL
) {
1278 /* switch back from replaying to the real source and continue */
1279 pArgs
->source
=realSource
;
1280 pArgs
->sourceLimit
=realSourceLimit
;
1281 pArgs
->flush
=realFlush
;
1282 sourceIndex
=realSourceIndex
;
1286 } else if(pArgs
->flush
&& cnv
->toULength
>0) {
1288 * the entire input stream is consumed
1289 * and there is a partial, truncated input sequence left
1292 /* inject an error and continue with callback handling */
1293 *err
=U_TRUNCATED_CHAR_FOUND
;
1294 calledCallback
=FALSE
; /* new error condition */
1296 /* input consumed */
1299 * return to the conversion loop once more if the flush
1300 * flag is set and the conversion function has not
1301 * successfully processed the end of the input yet
1303 * (continue converting by breaking out of only the inner loop)
1305 if(!converterSawEndOfInput
) {
1309 /* reset the converter without calling the callback function */
1310 _reset(cnv
, UCNV_RESET_TO_UNICODE
, FALSE
);
1313 /* done successfully */
1318 /* U_FAILURE(*err) */
1322 if( calledCallback
||
1323 (e
=*err
)==U_BUFFER_OVERFLOW_ERROR
||
1324 (e
!=U_INVALID_CHAR_FOUND
&&
1325 e
!=U_ILLEGAL_CHAR_FOUND
&&
1326 e
!=U_TRUNCATED_CHAR_FOUND
&&
1327 e
!=U_ILLEGAL_ESCAPE_SEQUENCE
&&
1328 e
!=U_UNSUPPORTED_ESCAPE_SEQUENCE
)
1331 * the callback did not or cannot resolve the error:
1332 * set output pointers and return
1334 * the check for buffer overflow is redundant but it is
1335 * a high-runner case and hopefully documents the intent
1338 * if we were replaying, then the replay buffer must be
1339 * copied back into the UConverter
1340 * and the real arguments must be restored
1342 if(realSource
!=NULL
) {
1345 U_ASSERT(cnv
->preToULength
==0);
1347 length
=(int32_t)(pArgs
->sourceLimit
-pArgs
->source
);
1349 uprv_memcpy(cnv
->preToU
, pArgs
->source
, length
);
1350 cnv
->preToULength
=(int8_t)-length
;
1353 pArgs
->source
=realSource
;
1354 pArgs
->sourceLimit
=realSourceLimit
;
1355 pArgs
->flush
=realFlush
;
1362 /* copy toUBytes[] to invalidCharBuffer[] */
1363 errorInputLength
=cnv
->invalidCharLength
=cnv
->toULength
;
1364 if(errorInputLength
>0) {
1365 uprv_memcpy(cnv
->invalidCharBuffer
, cnv
->toUBytes
, errorInputLength
);
1368 /* set the converter state to deal with the next character */
1371 /* call the callback function */
1372 cnv
->fromCharErrorBehaviour(cnv
->toUContext
, pArgs
,
1373 cnv
->invalidCharBuffer
, errorInputLength
,
1374 (*err
==U_INVALID_CHAR_FOUND
|| *err
==U_UNSUPPORTED_ESCAPE_SEQUENCE
) ?
1375 UCNV_UNASSIGNED
: UCNV_ILLEGAL
,
1379 * loop back to the offset handling
1381 * this flag will indicate after offset handling
1382 * that a callback was called;
1383 * if the callback did not resolve the error, then we return
1385 calledCallback
=TRUE
;
1390 U_CAPI
void U_EXPORT2
1391 ucnv_toUnicode(UConverter
*cnv
,
1392 UChar
**target
, const UChar
*targetLimit
,
1393 const char **source
, const char *sourceLimit
,
1397 UConverterToUnicodeArgs args
;
1401 /* check parameters */
1402 if(err
==NULL
|| U_FAILURE(*err
)) {
1406 if(cnv
==NULL
|| target
==NULL
|| source
==NULL
) {
1407 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1413 if(sourceLimit
<s
|| targetLimit
<t
) {
1414 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1419 * Make sure that the buffer sizes do not exceed the number range for
1420 * int32_t because some functions use the size (in units or bytes)
1421 * rather than comparing pointers, and because offsets are int32_t values.
1423 * size_t is guaranteed to be unsigned and large enough for the job.
1425 * Return with an error instead of adjusting the limits because we would
1426 * not be able to maintain the semantics that either the source must be
1427 * consumed or the target filled (unless an error occurs).
1428 * An adjustment would be sourceLimit=t+0x7fffffff; for example.
1431 ((size_t)(sourceLimit
-s
)>(size_t)0x7fffffff && sourceLimit
>s
) ||
1432 ((size_t)(targetLimit
-t
)>(size_t)0x3fffffff && targetLimit
>t
)
1434 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1438 /* flush the target overflow buffer */
1439 if(cnv
->UCharErrorBufferLength
>0) {
1443 overflow
=cnv
->UCharErrorBuffer
;
1444 length
=cnv
->UCharErrorBufferLength
;
1447 if(t
==targetLimit
) {
1448 /* the overflow buffer contains too much, keep the rest */
1452 overflow
[j
++]=overflow
[i
++];
1455 cnv
->UCharErrorBufferLength
=(int8_t)j
;
1457 *err
=U_BUFFER_OVERFLOW_ERROR
;
1461 /* copy the overflow contents to the target */
1464 *offsets
++=-1; /* no source index available for old output */
1468 /* the overflow buffer is completely copied to the target */
1469 cnv
->UCharErrorBufferLength
=0;
1472 if(!flush
&& s
==sourceLimit
&& cnv
->preToULength
>=0) {
1473 /* the overflow buffer is emptied and there is no new input: we are done */
1479 * Do not simply return with a buffer overflow error if
1480 * !flush && t==targetLimit
1481 * because it is possible that the source will not generate any output.
1482 * For example, the skip callback may be called;
1483 * it does not output anything.
1486 /* prepare the converter arguments */
1489 args
.offsets
=offsets
;
1491 args
.sourceLimit
=sourceLimit
;
1493 args
.targetLimit
=targetLimit
;
1494 args
.size
=sizeof(args
);
1496 _toUnicodeWithCallback(&args
, err
);
1498 *source
=args
.source
;
1499 *target
=args
.target
;
1502 /* ucnv_to/fromUChars() ----------------------------------------------------- */
1504 U_CAPI
int32_t U_EXPORT2
1505 ucnv_fromUChars(UConverter
*cnv
,
1506 char *dest
, int32_t destCapacity
,
1507 const UChar
*src
, int32_t srcLength
,
1508 UErrorCode
*pErrorCode
) {
1509 const UChar
*srcLimit
;
1510 char *originalDest
, *destLimit
;
1513 /* check arguments */
1514 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1519 destCapacity
<0 || (destCapacity
>0 && dest
==NULL
) ||
1520 srcLength
<-1 || (srcLength
!=0 && src
==NULL
)
1522 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1527 ucnv_resetFromUnicode(cnv
);
1530 srcLength
=u_strlen(src
);
1533 srcLimit
=src
+srcLength
;
1534 destLimit
=dest
+destCapacity
;
1536 /* pin the destination limit to U_MAX_PTR; NULL check is for OS/400 */
1537 if(destLimit
<dest
|| (destLimit
==NULL
&& dest
!=NULL
)) {
1538 destLimit
=(char *)U_MAX_PTR(dest
);
1541 /* perform the conversion */
1542 ucnv_fromUnicode(cnv
, &dest
, destLimit
, &src
, srcLimit
, 0, TRUE
, pErrorCode
);
1543 destLength
=(int32_t)(dest
-originalDest
);
1545 /* if an overflow occurs, then get the preflighting length */
1546 if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
1549 destLimit
=buffer
+sizeof(buffer
);
1552 *pErrorCode
=U_ZERO_ERROR
;
1553 ucnv_fromUnicode(cnv
, &dest
, destLimit
, &src
, srcLimit
, 0, TRUE
, pErrorCode
);
1554 destLength
+=(int32_t)(dest
-buffer
);
1555 } while(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
);
1561 return u_terminateChars(originalDest
, destCapacity
, destLength
, pErrorCode
);
1564 U_CAPI
int32_t U_EXPORT2
1565 ucnv_toUChars(UConverter
*cnv
,
1566 UChar
*dest
, int32_t destCapacity
,
1567 const char *src
, int32_t srcLength
,
1568 UErrorCode
*pErrorCode
) {
1569 const char *srcLimit
;
1570 UChar
*originalDest
, *destLimit
;
1573 /* check arguments */
1574 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1579 destCapacity
<0 || (destCapacity
>0 && dest
==NULL
) ||
1580 srcLength
<-1 || (srcLength
!=0 && src
==NULL
))
1582 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1587 ucnv_resetToUnicode(cnv
);
1590 srcLength
=uprv_strlen(src
);
1593 srcLimit
=src
+srcLength
;
1594 destLimit
=dest
+destCapacity
;
1596 /* pin the destination limit to U_MAX_PTR; NULL check is for OS/400 */
1597 if(destLimit
<dest
|| (destLimit
==NULL
&& dest
!=NULL
)) {
1598 destLimit
=(UChar
*)U_MAX_PTR(dest
);
1601 /* perform the conversion */
1602 ucnv_toUnicode(cnv
, &dest
, destLimit
, &src
, srcLimit
, 0, TRUE
, pErrorCode
);
1603 destLength
=(int32_t)(dest
-originalDest
);
1605 /* if an overflow occurs, then get the preflighting length */
1606 if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
)
1610 destLimit
=buffer
+sizeof(buffer
)/U_SIZEOF_UCHAR
;
1613 *pErrorCode
=U_ZERO_ERROR
;
1614 ucnv_toUnicode(cnv
, &dest
, destLimit
, &src
, srcLimit
, 0, TRUE
, pErrorCode
);
1615 destLength
+=(int32_t)(dest
-buffer
);
1617 while(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
);
1623 return u_terminateUChars(originalDest
, destCapacity
, destLength
, pErrorCode
);
1626 /* ucnv_getNextUChar() ------------------------------------------------------ */
1628 U_CAPI UChar32 U_EXPORT2
1629 ucnv_getNextUChar(UConverter
*cnv
,
1630 const char **source
, const char *sourceLimit
,
1632 UConverterToUnicodeArgs args
;
1633 UChar buffer
[U16_MAX_LENGTH
];
1638 /* check parameters */
1639 if(err
==NULL
|| U_FAILURE(*err
)) {
1643 if(cnv
==NULL
|| source
==NULL
) {
1644 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1650 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1655 * Make sure that the buffer sizes do not exceed the number range for
1656 * int32_t because some functions use the size (in units or bytes)
1657 * rather than comparing pointers, and because offsets are int32_t values.
1659 * size_t is guaranteed to be unsigned and large enough for the job.
1661 * Return with an error instead of adjusting the limits because we would
1662 * not be able to maintain the semantics that either the source must be
1663 * consumed or the target filled (unless an error occurs).
1664 * An adjustment would be sourceLimit=t+0x7fffffff; for example.
1666 if(((size_t)(sourceLimit
-s
)>(size_t)0x7fffffff && sourceLimit
>s
)) {
1667 *err
=U_ILLEGAL_ARGUMENT_ERROR
;
1673 /* flush the target overflow buffer */
1674 if(cnv
->UCharErrorBufferLength
>0) {
1677 overflow
=cnv
->UCharErrorBuffer
;
1679 length
=cnv
->UCharErrorBufferLength
;
1680 U16_NEXT(overflow
, i
, length
, c
);
1682 /* move the remaining overflow contents up to the beginning */
1683 if((cnv
->UCharErrorBufferLength
=(int8_t)(length
-i
))>0) {
1684 uprv_memmove(cnv
->UCharErrorBuffer
, cnv
->UCharErrorBuffer
+i
,
1685 cnv
->UCharErrorBufferLength
*U_SIZEOF_UCHAR
);
1688 if(!U16_IS_LEAD(c
) || i
<length
) {
1692 * Continue if the overflow buffer contained only a lead surrogate,
1693 * in case the converter outputs single surrogates from complete
1699 * flush==TRUE is implied for ucnv_getNextUChar()
1701 * do not simply return even if s==sourceLimit because the converter may
1702 * not have seen flush==TRUE before
1705 /* prepare the converter arguments */
1710 args
.sourceLimit
=sourceLimit
;
1712 args
.targetLimit
=buffer
+1;
1713 args
.size
=sizeof(args
);
1717 * call the native getNextUChar() implementation if we are
1718 * at a character boundary (toULength==0)
1720 * unlike with _toUnicode(), getNextUChar() implementations must set
1721 * U_TRUNCATED_CHAR_FOUND for truncated input,
1722 * in addition to setting toULength/toUBytes[]
1724 if(cnv
->toULength
==0 && cnv
->sharedData
->impl
->getNextUChar
!=NULL
) {
1725 c
=cnv
->sharedData
->impl
->getNextUChar(&args
, err
);
1726 *source
=s
=args
.source
;
1727 if(*err
==U_INDEX_OUTOFBOUNDS_ERROR
) {
1728 /* reset the converter without calling the callback function */
1729 _reset(cnv
, UCNV_RESET_TO_UNICODE
, FALSE
);
1730 return 0xffff; /* no output */
1731 } else if(U_SUCCESS(*err
) && c
>=0) {
1734 * else fall through to use _toUnicode() because
1735 * UCNV_GET_NEXT_UCHAR_USE_TO_U: the native function did not want to handle it after all
1736 * U_FAILURE: call _toUnicode() for callback handling (do not output c)
1741 /* convert to one UChar in buffer[0], or handle getNextUChar() errors */
1742 _toUnicodeWithCallback(&args
, err
);
1744 if(*err
==U_BUFFER_OVERFLOW_ERROR
) {
1749 length
=(int32_t)(args
.target
-buffer
);
1751 /* write the lead surrogate from the overflow buffer */
1753 args
.target
=buffer
+1;
1758 /* buffer contents starts at i and ends before length */
1760 if(U_FAILURE(*err
)) {
1761 c
=0xffff; /* no output */
1762 } else if(length
==0) {
1763 /* no input or only state changes */
1764 *err
=U_INDEX_OUTOFBOUNDS_ERROR
;
1765 /* no need to reset explicitly because _toUnicodeWithCallback() did it */
1766 c
=0xffff; /* no output */
1770 if(!U16_IS_LEAD(c
)) {
1771 /* consume c=buffer[0], done */
1773 /* got a lead surrogate, see if a trail surrogate follows */
1776 if(cnv
->UCharErrorBufferLength
>0) {
1777 /* got overflow output from the conversion */
1778 if(U16_IS_TRAIL(c2
=cnv
->UCharErrorBuffer
[0])) {
1779 /* got a trail surrogate, too */
1780 c
=U16_GET_SUPPLEMENTARY(c
, c2
);
1782 /* move the remaining overflow contents up to the beginning */
1783 if((--cnv
->UCharErrorBufferLength
)>0) {
1784 uprv_memmove(cnv
->UCharErrorBuffer
, cnv
->UCharErrorBuffer
+1,
1785 cnv
->UCharErrorBufferLength
*U_SIZEOF_UCHAR
);
1788 /* c is an unpaired lead surrogate, just return it */
1790 } else if(args
.source
<sourceLimit
) {
1791 /* convert once more, to buffer[1] */
1792 args
.targetLimit
=buffer
+2;
1793 _toUnicodeWithCallback(&args
, err
);
1794 if(*err
==U_BUFFER_OVERFLOW_ERROR
) {
1798 length
=(int32_t)(args
.target
-buffer
);
1799 if(U_SUCCESS(*err
) && length
==2 && U16_IS_TRAIL(c2
=buffer
[1])) {
1800 /* got a trail surrogate, too */
1801 c
=U16_GET_SUPPLEMENTARY(c
, c2
);
1809 * move leftover output from buffer[i..length[
1810 * into the beginning of the overflow buffer
1813 /* move further overflow back */
1814 int32_t delta
=length
-i
;
1815 if((length
=cnv
->UCharErrorBufferLength
)>0) {
1816 uprv_memmove(cnv
->UCharErrorBuffer
+delta
, cnv
->UCharErrorBuffer
,
1817 length
*U_SIZEOF_UCHAR
);
1819 cnv
->UCharErrorBufferLength
=(int8_t)(length
+delta
);
1821 cnv
->UCharErrorBuffer
[0]=buffer
[i
++];
1823 cnv
->UCharErrorBuffer
[1]=buffer
[i
];
1827 *source
=args
.source
;
1831 /* ucnv_convert() and siblings ---------------------------------------------- */
1833 U_CAPI
void U_EXPORT2
1834 ucnv_convertEx(UConverter
*targetCnv
, UConverter
*sourceCnv
,
1835 char **target
, const char *targetLimit
,
1836 const char **source
, const char *sourceLimit
,
1837 UChar
*pivotStart
, UChar
**pivotSource
,
1838 UChar
**pivotTarget
, const UChar
*pivotLimit
,
1839 UBool reset
, UBool flush
,
1840 UErrorCode
*pErrorCode
) {
1841 UChar pivotBuffer
[CHUNK_SIZE
];
1842 UChar
*myPivotSource
, *myPivotTarget
;
1844 /* error checking */
1845 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
1849 if( targetCnv
==NULL
|| sourceCnv
==NULL
||
1850 source
==NULL
|| *source
==NULL
||
1851 target
==NULL
|| *target
==NULL
|| targetLimit
==NULL
1853 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1857 if(pivotStart
==NULL
) {
1858 /* use the stack pivot buffer */
1859 pivotStart
=myPivotSource
=myPivotTarget
=pivotBuffer
;
1860 pivotSource
=&myPivotSource
;
1861 pivotTarget
=&myPivotTarget
;
1862 pivotLimit
=pivotBuffer
+CHUNK_SIZE
;
1863 } else if( pivotStart
>=pivotLimit
||
1864 pivotSource
==NULL
|| *pivotSource
==NULL
||
1865 pivotTarget
==NULL
|| *pivotTarget
==NULL
||
1868 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1872 if(sourceLimit
==NULL
) {
1873 /* get limit of single-byte-NUL-terminated source string */
1874 sourceLimit
=uprv_strchr(*source
, 0);
1878 ucnv_resetToUnicode(sourceCnv
);
1879 ucnv_resetFromUnicode(targetCnv
);
1880 *pivotTarget
=*pivotSource
=pivotStart
;
1883 /* conversion loop */
1887 * if we did a reset in this function, we know that there is nothing
1888 * to convert to the target yet, so we save a function call
1893 * convert to the target first in case the pivot is filled at entry
1894 * or the targetCnv has some output bytes in its state
1896 ucnv_fromUnicode(targetCnv
,
1897 target
, targetLimit
,
1898 (const UChar
**)pivotSource
, *pivotTarget
,
1900 (UBool
)(flush
&& *source
==sourceLimit
),
1902 if(U_FAILURE(*pErrorCode
)) {
1906 /* ucnv_fromUnicode() must have consumed the pivot contents since it returned with U_SUCCESS() */
1907 *pivotSource
=*pivotTarget
=pivotStart
;
1910 /* convert from the source to the pivot */
1911 ucnv_toUnicode(sourceCnv
,
1912 pivotTarget
, pivotLimit
,
1913 source
, sourceLimit
,
1917 if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
1918 /* pivot overflow: continue with the conversion loop */
1919 *pErrorCode
=U_ZERO_ERROR
;
1920 } else if(U_FAILURE(*pErrorCode
) || *pivotTarget
==pivotStart
) {
1921 /* conversion error, or there was nothing left to convert */
1924 /* else ucnv_toUnicode() wrote into the pivot buffer: continue */
1928 * The conversion loop is exited when one of the following is true:
1929 * - the entire source text has been converted successfully to the target buffer
1930 * - a target buffer overflow occurred
1931 * - a conversion error occurred
1934 /* terminate the target buffer if possible */
1935 if(flush
&& U_SUCCESS(*pErrorCode
)) {
1936 if(*target
!=targetLimit
) {
1938 if(*pErrorCode
==U_STRING_NOT_TERMINATED_WARNING
) {
1939 *pErrorCode
=U_ZERO_ERROR
;
1942 *pErrorCode
=U_STRING_NOT_TERMINATED_WARNING
;
1947 /* internal implementation of ucnv_convert() etc. with preflighting */
1949 ucnv_internalConvert(UConverter
*outConverter
, UConverter
*inConverter
,
1950 char *target
, int32_t targetCapacity
,
1951 const char *source
, int32_t sourceLength
,
1952 UErrorCode
*pErrorCode
) {
1953 UChar pivotBuffer
[CHUNK_SIZE
];
1954 UChar
*pivot
, *pivot2
;
1957 const char *sourceLimit
;
1958 const char *targetLimit
;
1959 int32_t targetLength
=0;
1962 if(sourceLength
<0) {
1963 sourceLimit
=uprv_strchr(source
, 0);
1965 sourceLimit
=source
+sourceLength
;
1968 /* if there is no input data, we're done */
1969 if(source
==sourceLimit
) {
1970 return u_terminateChars(target
, targetCapacity
, 0, pErrorCode
);
1973 pivot
=pivot2
=pivotBuffer
;
1977 if(targetCapacity
>0) {
1978 /* perform real conversion */
1979 targetLimit
=target
+targetCapacity
;
1980 ucnv_convertEx(outConverter
, inConverter
,
1981 &myTarget
, targetLimit
,
1982 &source
, sourceLimit
,
1983 pivotBuffer
, &pivot
, &pivot2
, pivotBuffer
+CHUNK_SIZE
,
1987 targetLength
=myTarget
-target
;
1991 * If the output buffer is exhausted (or we are only "preflighting"), we need to stop writing
1992 * to it but continue the conversion in order to store in targetCapacity
1993 * the number of bytes that was required.
1995 if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
|| targetCapacity
==0)
1997 char targetBuffer
[CHUNK_SIZE
];
1999 targetLimit
=targetBuffer
+CHUNK_SIZE
;
2001 *pErrorCode
=U_ZERO_ERROR
;
2002 myTarget
=targetBuffer
;
2003 ucnv_convertEx(outConverter
, inConverter
,
2004 &myTarget
, targetLimit
,
2005 &source
, sourceLimit
,
2006 pivotBuffer
, &pivot
, &pivot2
, pivotBuffer
+CHUNK_SIZE
,
2010 targetLength
+=(myTarget
-targetBuffer
);
2011 } while(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
);
2013 /* done with preflighting, set warnings and errors as appropriate */
2014 return u_terminateChars(target
, targetCapacity
, targetLength
, pErrorCode
);
2017 /* no need to call u_terminateChars() because ucnv_convertEx() took care of that */
2018 return targetLength
;
2021 U_CAPI
int32_t U_EXPORT2
2022 ucnv_convert(const char *toConverterName
, const char *fromConverterName
,
2023 char *target
, int32_t targetCapacity
,
2024 const char *source
, int32_t sourceLength
,
2025 UErrorCode
*pErrorCode
) {
2026 UConverter in
, out
; /* stack-allocated */
2027 UConverter
*inConverter
, *outConverter
;
2028 int32_t targetLength
;
2030 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
2034 if( source
==NULL
|| sourceLength
<-1 ||
2035 targetCapacity
<0 || (targetCapacity
>0 && target
==NULL
)
2037 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
2041 /* if there is no input data, we're done */
2042 if(sourceLength
==0 || (sourceLength
<0 && *source
==0)) {
2043 return u_terminateChars(target
, targetCapacity
, 0, pErrorCode
);
2046 /* create the converters */
2047 inConverter
=ucnv_createConverter(&in
, fromConverterName
, pErrorCode
);
2048 if(U_FAILURE(*pErrorCode
)) {
2052 outConverter
=ucnv_createConverter(&out
, toConverterName
, pErrorCode
);
2053 if(U_FAILURE(*pErrorCode
)) {
2054 ucnv_close(inConverter
);
2058 targetLength
=ucnv_internalConvert(outConverter
, inConverter
,
2059 target
, targetCapacity
,
2060 source
, sourceLength
,
2063 ucnv_close(inConverter
);
2064 ucnv_close(outConverter
);
2066 return targetLength
;
2071 ucnv_convertAlgorithmic(UBool convertToAlgorithmic
,
2072 UConverterType algorithmicType
,
2074 char *target
, int32_t targetCapacity
,
2075 const char *source
, int32_t sourceLength
,
2076 UErrorCode
*pErrorCode
) {
2077 UConverter algoConverterStatic
; /* stack-allocated */
2078 UConverter
*algoConverter
, *to
, *from
;
2079 int32_t targetLength
;
2081 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
2085 if( cnv
==NULL
|| source
==NULL
|| sourceLength
<-1 ||
2086 targetCapacity
<0 || (targetCapacity
>0 && target
==NULL
)
2088 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
2092 /* if there is no input data, we're done */
2093 if(sourceLength
==0 || (sourceLength
<0 && *source
==0)) {
2094 return u_terminateChars(target
, targetCapacity
, 0, pErrorCode
);
2097 /* create the algorithmic converter */
2098 algoConverter
=ucnv_createAlgorithmicConverter(&algoConverterStatic
, algorithmicType
,
2100 if(U_FAILURE(*pErrorCode
)) {
2104 /* reset the other converter */
2105 if(convertToAlgorithmic
) {
2106 /* cnv->Unicode->algo */
2107 ucnv_resetToUnicode(cnv
);
2111 /* algo->Unicode->cnv */
2112 ucnv_resetFromUnicode(cnv
);
2117 targetLength
=ucnv_internalConvert(to
, from
,
2118 target
, targetCapacity
,
2119 source
, sourceLength
,
2122 ucnv_close(algoConverter
);
2124 return targetLength
;
2127 U_CAPI
int32_t U_EXPORT2
2128 ucnv_toAlgorithmic(UConverterType algorithmicType
,
2130 char *target
, int32_t targetCapacity
,
2131 const char *source
, int32_t sourceLength
,
2132 UErrorCode
*pErrorCode
) {
2133 return ucnv_convertAlgorithmic(TRUE
, algorithmicType
, cnv
,
2134 target
, targetCapacity
,
2135 source
, sourceLength
,
2139 U_CAPI
int32_t U_EXPORT2
2140 ucnv_fromAlgorithmic(UConverter
*cnv
,
2141 UConverterType algorithmicType
,
2142 char *target
, int32_t targetCapacity
,
2143 const char *source
, int32_t sourceLength
,
2144 UErrorCode
*pErrorCode
) {
2145 return ucnv_convertAlgorithmic(FALSE
, algorithmicType
, cnv
,
2146 target
, targetCapacity
,
2147 source
, sourceLength
,
2151 U_CAPI UConverterType U_EXPORT2
2152 ucnv_getType(const UConverter
* converter
)
2154 int8_t type
= converter
->sharedData
->staticData
->conversionType
;
2155 #if !UCONFIG_NO_LEGACY_CONVERSION
2156 if(type
== UCNV_MBCS
) {
2157 return ucnv_MBCSGetType(converter
);
2160 return (UConverterType
)type
;
2163 U_CAPI
void U_EXPORT2
2164 ucnv_getStarters(const UConverter
* converter
,
2165 UBool starters
[256],
2168 if (err
== NULL
|| U_FAILURE(*err
)) {
2172 if(converter
->sharedData
->impl
->getStarters
!= NULL
) {
2173 converter
->sharedData
->impl
->getStarters(converter
, starters
, err
);
2175 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
2179 static const UAmbiguousConverter
*ucnv_getAmbiguous(const UConverter
*cnv
)
2181 UErrorCode errorCode
;
2189 errorCode
=U_ZERO_ERROR
;
2190 name
=ucnv_getName(cnv
, &errorCode
);
2191 if(U_FAILURE(errorCode
)) {
2195 for(i
=0; i
<(int32_t)(sizeof(ambiguousConverters
)/sizeof(UAmbiguousConverter
)); ++i
)
2197 if(0==uprv_strcmp(name
, ambiguousConverters
[i
].name
))
2199 return ambiguousConverters
+i
;
2206 U_CAPI
void U_EXPORT2
2207 ucnv_fixFileSeparator(const UConverter
*cnv
,
2209 int32_t sourceLength
) {
2210 const UAmbiguousConverter
*a
;
2214 if(cnv
==NULL
|| source
==NULL
|| sourceLength
<=0 || (a
=ucnv_getAmbiguous(cnv
))==NULL
)
2219 variant5c
=a
->variant5c
;
2220 for(i
=0; i
<sourceLength
; ++i
) {
2221 if(source
[i
]==variant5c
) {
2227 U_CAPI UBool U_EXPORT2
2228 ucnv_isAmbiguous(const UConverter
*cnv
) {
2229 return (UBool
)(ucnv_getAmbiguous(cnv
)!=NULL
);
2232 U_CAPI
void U_EXPORT2
2233 ucnv_setFallback(UConverter
*cnv
, UBool usesFallback
)
2235 cnv
->useFallback
= usesFallback
;
2238 U_CAPI UBool U_EXPORT2
2239 ucnv_usesFallback(const UConverter
*cnv
)
2241 return cnv
->useFallback
;
2244 U_CAPI
void U_EXPORT2
2245 ucnv_getInvalidChars (const UConverter
* converter
,
2250 if (err
== NULL
|| U_FAILURE(*err
))
2254 if (len
== NULL
|| errBytes
== NULL
|| converter
== NULL
)
2256 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
2259 if (*len
< converter
->invalidCharLength
)
2261 *err
= U_INDEX_OUTOFBOUNDS_ERROR
;
2264 if ((*len
= converter
->invalidCharLength
) > 0)
2266 uprv_memcpy (errBytes
, converter
->invalidCharBuffer
, *len
);
2270 U_CAPI
void U_EXPORT2
2271 ucnv_getInvalidUChars (const UConverter
* converter
,
2276 if (err
== NULL
|| U_FAILURE(*err
))
2280 if (len
== NULL
|| errChars
== NULL
|| converter
== NULL
)
2282 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
2285 if (*len
< converter
->invalidUCharLength
)
2287 *err
= U_INDEX_OUTOFBOUNDS_ERROR
;
2290 if ((*len
= converter
->invalidUCharLength
) > 0)
2292 uprv_memcpy (errChars
, converter
->invalidUCharBuffer
, sizeof(UChar
) * (*len
));
2296 #define SIG_MAX_LEN 5
2298 U_CAPI
const char* U_EXPORT2
2299 ucnv_detectUnicodeSignature( const char* source
,
2300 int32_t sourceLength
,
2301 int32_t* signatureLength
,
2302 UErrorCode
* pErrorCode
) {
2305 /* initial 0xa5 bytes: make sure that if we read <SIG_MAX_LEN
2306 * bytes we don't misdetect something
2308 char start
[SIG_MAX_LEN
]={ '\xa5', '\xa5', '\xa5', '\xa5', '\xa5' };
2311 if((pErrorCode
==NULL
) || U_FAILURE(*pErrorCode
)){
2315 if(source
== NULL
|| sourceLength
< -1){
2316 *pErrorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
2320 if(signatureLength
== NULL
) {
2321 signatureLength
= &dummy
;
2324 if(sourceLength
==-1){
2325 sourceLength
=uprv_strlen(source
);
2329 while(i
<sourceLength
&& i
<SIG_MAX_LEN
){
2334 if(start
[0] == '\xFE' && start
[1] == '\xFF') {
2337 } else if(start
[0] == '\xFF' && start
[1] == '\xFE') {
2338 if(start
[2] == '\x00' && start
[3] =='\x00') {
2345 } else if(start
[0] == '\xEF' && start
[1] == '\xBB' && start
[2] == '\xBF') {
2348 } else if(start
[0] == '\x00' && start
[1] == '\x00' &&
2349 start
[2] == '\xFE' && start
[3]=='\xFF') {
2352 } else if(start
[0] == '\x0E' && start
[1] == '\xFE' && start
[2] == '\xFF') {
2355 } else if(start
[0] == '\xFB' && start
[1] == '\xEE' && start
[2] == '\x28') {
2358 } else if(start
[0] == '\x2B' && start
[1] == '\x2F' && start
[2] == '\x76') {
2360 * UTF-7: Initial U+FEFF is encoded as +/v8 or +/v9 or +/v+ or +/v/
2361 * depending on the second UTF-16 code unit.
2362 * Detect the entire, closed Unicode mode sequence +/v8- for only U+FEFF
2365 * So far we have +/v
2367 if(start
[3] == '\x38' && start
[4] == '\x2D') {
2371 } else if(start
[3] == '\x38' || start
[3] == '\x39' || start
[3] == '\x2B' || start
[3] == '\x2F') {
2372 /* 4 bytes +/v8 or +/v9 or +/v+ or +/v/ */
2376 }else if(start
[0]=='\xDD' && start
[1]== '\x73'&& start
[2]=='\x66' && start
[3]=='\x73'){
2378 return "UTF-EBCDIC";
2382 /* no known Unicode signature byte sequence recognized */
2390 * Hey, Emacs, please set the following:
2393 * indent-tabs-mode: nil