2 ******************************************************************************
4 * Copyright (C) 2003-2013, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
8 * file name: ucnv_ext.cpp
10 * tab size: 8 (not used)
13 * created on: 2003jun13
14 * created by: Markus W. Scherer
16 * Conversion extensions
19 #include "unicode/utypes.h"
21 #if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION
23 #include "unicode/uset.h"
30 /* to Unicode --------------------------------------------------------------- */
33 * @return lookup value for the byte, if found; else 0
35 static inline uint32_t
36 ucnv_extFindToU(const uint32_t *toUSection
, int32_t length
, uint8_t byte
) {
38 int32_t i
, start
, limit
;
40 /* check the input byte against the lowest and highest section bytes */
41 start
=(int32_t)UCNV_EXT_TO_U_GET_BYTE(toUSection
[0]);
42 limit
=(int32_t)UCNV_EXT_TO_U_GET_BYTE(toUSection
[length
-1]);
43 if(byte
<start
|| limit
<byte
) {
44 return 0; /* the byte is out of range */
47 if(length
==((limit
-start
)+1)) {
48 /* direct access on a linear array */
49 return UCNV_EXT_TO_U_GET_VALUE(toUSection
[byte
-start
]); /* could be 0 */
52 /* word0 is suitable for <=toUSection[] comparison, word for <toUSection[] */
53 word0
=UCNV_EXT_TO_U_MAKE_WORD(byte
, 0);
56 * Shift byte once instead of each section word and add 0xffffff.
57 * We will compare the shifted/added byte (bbffffff) against
58 * section words which have byte values in the same bit position.
59 * If and only if byte bb < section byte ss then bbffffff<ssvvvvvv
61 * so we need not mask off the lower 24 bits of each section word.
63 word
=word0
|UCNV_EXT_TO_U_VALUE_MASK
;
76 /* linear search for the last part */
77 if(word0
<=toUSection
[start
]) {
80 if(++start
<limit
&& word0
<=toUSection
[start
]) {
83 if(++start
<limit
&& word0
<=toUSection
[start
]) {
86 /* always break at start==limit-1 */
92 if(word
<toUSection
[i
]) {
99 /* did we really find it? */
100 if(start
<limit
&& byte
==UCNV_EXT_TO_U_GET_BYTE(word
=toUSection
[start
])) {
101 return UCNV_EXT_TO_U_GET_VALUE(word
); /* never 0 */
103 return 0; /* not found */
108 * TRUE if not an SI/SO stateful converter,
109 * or if the match length fits with the current converter state
111 #define UCNV_EXT_TO_U_VERIFY_SISO_MATCH(sisoState, match) \
112 ((sisoState)<0 || ((sisoState)==0) == (match==1))
115 * this works like ucnv_extMatchFromU() except
116 * - the first character is in pre
118 * - the returned matchLength is not offset by 2
121 ucnv_extMatchToU(const int32_t *cx
, int8_t sisoState
,
122 const char *pre
, int32_t preLength
,
123 const char *src
, int32_t srcLength
,
124 uint32_t *pMatchValue
,
125 UBool
/*useFallback*/, UBool flush
) {
126 const uint32_t *toUTable
, *toUSection
;
128 uint32_t value
, matchValue
;
129 int32_t i
, j
, idx
, length
, matchLength
;
132 if(cx
==NULL
|| cx
[UCNV_EXT_TO_U_LENGTH
]<=0) {
133 return 0; /* no extension data, no match */
137 toUTable
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_TO_U_INDEX
, uint32_t);
144 /* SBCS state of an SI/SO stateful converter, look at only exactly 1 byte */
146 return 0; /* no match of a DBCS sequence in SBCS mode */
147 } else if(preLength
==1) {
149 } else /* preLength==0 */ {
157 /* we must not remember fallback matches when not using fallbacks */
159 /* match input units until there is a full match or the input is consumed */
161 /* go to the next section */
162 toUSection
=toUTable
+idx
;
164 /* read first pair of the section */
166 length
=UCNV_EXT_TO_U_GET_BYTE(value
);
167 value
=UCNV_EXT_TO_U_GET_VALUE(value
);
169 (UCNV_EXT_TO_U_IS_ROUNDTRIP(value
) ||
170 TO_U_USE_FALLBACK(useFallback
)) &&
171 UCNV_EXT_TO_U_VERIFY_SISO_MATCH(sisoState
, i
+j
)
173 /* remember longest match so far */
178 /* match pre[] then src[] */
181 } else if(j
<srcLength
) {
184 /* all input consumed, partial match */
185 if(flush
|| (length
=(i
+j
))>UCNV_EXT_MAX_BYTES
) {
187 * end of the entire input stream, stop with the longest match so far
188 * or: partial match must not be longer than UCNV_EXT_MAX_BYTES
189 * because it must fit into state buffers
193 /* continue with more input next time */
198 /* search for the current UChar */
199 value
=ucnv_extFindToU(toUSection
, length
, b
);
201 /* no match here, stop with the longest match so far */
204 if(UCNV_EXT_TO_U_IS_PARTIAL(value
)) {
205 /* partial match, continue */
206 idx
=(int32_t)UCNV_EXT_TO_U_GET_PARTIAL_INDEX(value
);
208 if( (UCNV_EXT_TO_U_IS_ROUNDTRIP(value
) ||
209 TO_U_USE_FALLBACK(useFallback
)) &&
210 UCNV_EXT_TO_U_VERIFY_SISO_MATCH(sisoState
, i
+j
)
212 /* full match, stop with result */
216 /* full match on fallback not taken, stop with the longest match so far */
224 /* no match at all */
229 *pMatchValue
=UCNV_EXT_TO_U_MASK_ROUNDTRIP(matchValue
);
234 ucnv_extWriteToU(UConverter
*cnv
, const int32_t *cx
,
236 UChar
**target
, const UChar
*targetLimit
,
237 int32_t **offsets
, int32_t srcIndex
,
238 UErrorCode
*pErrorCode
) {
239 /* output the result */
240 if(UCNV_EXT_TO_U_IS_CODE_POINT(value
)) {
241 /* output a single code point */
242 ucnv_toUWriteCodePoint(
243 cnv
, UCNV_EXT_TO_U_GET_CODE_POINT(value
),
248 /* output a string - with correct data we have resultLength>0 */
251 UCNV_EXT_ARRAY(cx
, UCNV_EXT_TO_U_UCHARS_INDEX
, UChar
)+
252 UCNV_EXT_TO_U_GET_INDEX(value
),
253 UCNV_EXT_TO_U_GET_LENGTH(value
),
261 * get the SI/SO toU state (state 0 is for SBCS, 1 for DBCS),
262 * or 1 for DBCS-only,
263 * or -1 if the converter is not SI/SO stateful
265 * Note: For SI/SO stateful converters getting here,
266 * cnv->mode==0 is equivalent to firstLength==1.
268 #define UCNV_SISO_STATE(cnv) \
269 ((cnv)->sharedData->mbcs.outputType==MBCS_OUTPUT_2_SISO ? (int8_t)(cnv)->mode : \
270 (cnv)->sharedData->mbcs.outputType==MBCS_OUTPUT_DBCS_ONLY ? 1 : -1)
273 * target<targetLimit; set error code for overflow
276 ucnv_extInitialMatchToU(UConverter
*cnv
, const int32_t *cx
,
278 const char **src
, const char *srcLimit
,
279 UChar
**target
, const UChar
*targetLimit
,
280 int32_t **offsets
, int32_t srcIndex
,
282 UErrorCode
*pErrorCode
) {
283 uint32_t value
= 0; /* initialize output-only param to 0 to silence gcc */
287 match
=ucnv_extMatchToU(cx
, (int8_t)UCNV_SISO_STATE(cnv
),
288 (const char *)cnv
->toUBytes
, firstLength
,
289 *src
, (int32_t)(srcLimit
-*src
),
291 cnv
->useFallback
, flush
);
293 /* advance src pointer for the consumed input */
294 *src
+=match
-firstLength
;
296 /* write result to target */
297 ucnv_extWriteToU(cnv
, cx
,
304 /* save state for partial match */
308 /* copy the first code point */
309 s
=(const char *)cnv
->toUBytes
;
310 cnv
->preToUFirstLength
=(int8_t)firstLength
;
311 for(j
=0; j
<firstLength
; ++j
) {
315 /* now copy the newly consumed input */
318 for(; j
<match
; ++j
) {
321 *src
=s
; /* same as *src=srcLimit; because we reached the end of input */
322 cnv
->preToULength
=(int8_t)match
;
324 } else /* match==0 no match */ {
330 ucnv_extSimpleMatchToU(const int32_t *cx
,
331 const char *source
, int32_t length
,
333 uint32_t value
= 0; /* initialize output-only param to 0 to silence gcc */
341 match
=ucnv_extMatchToU(cx
, -1,
347 /* write result for simple, single-character conversion */
348 if(UCNV_EXT_TO_U_IS_CODE_POINT(value
)) {
349 return UCNV_EXT_TO_U_GET_CODE_POINT(value
);
354 * return no match because
355 * - match>0 && value points to string: simple conversion cannot handle multiple code points
356 * - match>0 && match!=length: not all input consumed, forbidden for this function
357 * - match==0: no match found in the first place
358 * - match<0: partial match, not supported for simple conversion (and flush==TRUE)
364 * continue partial match with new input
365 * never called for simple, single-character conversion
368 ucnv_extContinueMatchToU(UConverter
*cnv
,
369 UConverterToUnicodeArgs
*pArgs
, int32_t srcIndex
,
370 UErrorCode
*pErrorCode
) {
371 uint32_t value
= 0; /* initialize output-only param to 0 to silence gcc */
372 int32_t match
, length
;
374 match
=ucnv_extMatchToU(cnv
->sharedData
->mbcs
.extIndexes
, (int8_t)UCNV_SISO_STATE(cnv
),
375 cnv
->preToU
, cnv
->preToULength
,
376 pArgs
->source
, (int32_t)(pArgs
->sourceLimit
-pArgs
->source
),
378 cnv
->useFallback
, pArgs
->flush
);
380 if(match
>=cnv
->preToULength
) {
381 /* advance src pointer for the consumed input */
382 pArgs
->source
+=match
-cnv
->preToULength
;
385 /* the match did not use all of preToU[] - keep the rest for replay */
386 length
=cnv
->preToULength
-match
;
387 uprv_memmove(cnv
->preToU
, cnv
->preToU
+match
, length
);
388 cnv
->preToULength
=(int8_t)-length
;
392 ucnv_extWriteToU(cnv
, cnv
->sharedData
->mbcs
.extIndexes
,
394 &pArgs
->target
, pArgs
->targetLimit
,
395 &pArgs
->offsets
, srcIndex
,
398 /* save state for partial match */
402 /* just _append_ the newly consumed input to preToU[] */
405 for(j
=cnv
->preToULength
; j
<match
; ++j
) {
408 pArgs
->source
=s
; /* same as *src=srcLimit; because we reached the end of input */
409 cnv
->preToULength
=(int8_t)match
;
410 } else /* match==0 */ {
414 * We need to split the previous input into two parts:
416 * 1. The first codepage character is unmappable - that's how we got into
417 * trying the extension data in the first place.
418 * We need to move it from the preToU buffer
419 * to the error buffer, set an error code,
420 * and prepare the rest of the previous input for 2.
422 * 2. The rest of the previous input must be converted once we
423 * come back from the callback for the first character.
424 * At that time, we have to try again from scratch to convert
425 * these input characters.
426 * The replay will be handled by the ucnv.c conversion code.
429 /* move the first codepage character to the error field */
430 uprv_memcpy(cnv
->toUBytes
, cnv
->preToU
, cnv
->preToUFirstLength
);
431 cnv
->toULength
=cnv
->preToUFirstLength
;
433 /* move the rest up inside the buffer */
434 length
=cnv
->preToULength
-cnv
->preToUFirstLength
;
436 uprv_memmove(cnv
->preToU
, cnv
->preToU
+cnv
->preToUFirstLength
, length
);
439 /* mark preToU for replay */
440 cnv
->preToULength
=(int8_t)-length
;
442 /* set the error code for unassigned */
443 *pErrorCode
=U_INVALID_CHAR_FOUND
;
447 /* from Unicode ------------------------------------------------------------- */
449 // Use roundtrips, "good one-way" mappings, and some normal fallbacks.
451 extFromUUseMapping(UBool useFallback
, uint32_t value
, UChar32 firstCP
) {
453 ((value
&UCNV_EXT_FROM_U_STATUS_MASK
)!=0 ||
454 FROM_U_USE_FALLBACK(useFallback
, firstCP
)) &&
455 (value
&UCNV_EXT_FROM_U_RESERVED_MASK
)==0;
459 * @return index of the UChar, if found; else <0
461 static inline int32_t
462 ucnv_extFindFromU(const UChar
*fromUSection
, int32_t length
, UChar u
) {
463 int32_t i
, start
, limit
;
476 /* linear search for the last part */
477 if(u
<=fromUSection
[start
]) {
480 if(++start
<limit
&& u
<=fromUSection
[start
]) {
483 if(++start
<limit
&& u
<=fromUSection
[start
]) {
486 /* always break at start==limit-1 */
492 if(u
<fromUSection
[i
]) {
499 /* did we really find it? */
500 if(start
<limit
&& u
==fromUSection
[start
]) {
503 return -1; /* not found */
508 * @param cx pointer to extension data; if NULL, returns 0
509 * @param firstCP the first code point before all the other UChars
510 * @param pre UChars that must match; !initialMatch: partial match with them
511 * @param preLength length of pre, >=0
512 * @param src UChars that can be used to complete a match
513 * @param srcLength length of src, >=0
514 * @param pMatchValue [out] output result value for the match from the data structure
515 * @param useFallback "use fallback" flag, usually from cnv->useFallback
516 * @param flush TRUE if the end of the input stream is reached
517 * @return >1: matched, return value=total match length (number of input units matched)
518 * 1: matched, no mapping but request for <subchar1>
519 * (only for the first code point)
521 * <0: partial match, return value=negative total match length
522 * (partial matches are never returned for flush==TRUE)
523 * (partial matches are never returned as being longer than UCNV_EXT_MAX_UCHARS)
524 * the matchLength is 2 if only firstCP matched, and >2 if firstCP and
525 * further code units matched
528 ucnv_extMatchFromU(const int32_t *cx
,
530 const UChar
*pre
, int32_t preLength
,
531 const UChar
*src
, int32_t srcLength
,
532 uint32_t *pMatchValue
,
533 UBool useFallback
, UBool flush
) {
534 const uint16_t *stage12
, *stage3
;
535 const uint32_t *stage3b
;
537 const UChar
*fromUTableUChars
, *fromUSectionUChars
;
538 const uint32_t *fromUTableValues
, *fromUSectionValues
;
540 uint32_t value
, matchValue
;
541 int32_t i
, j
, idx
, length
, matchLength
;
545 return 0; /* no extension data, no match */
548 /* trie lookup of firstCP */
549 idx
=firstCP
>>10; /* stage 1 index */
550 if(idx
>=cx
[UCNV_EXT_FROM_U_STAGE_1_LENGTH
]) {
551 return 0; /* the first code point is outside the trie */
554 stage12
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_STAGE_12_INDEX
, uint16_t);
555 stage3
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_STAGE_3_INDEX
, uint16_t);
556 idx
=UCNV_EXT_FROM_U(stage12
, stage3
, idx
, firstCP
);
558 stage3b
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_STAGE_3B_INDEX
, uint32_t);
565 * Tests for (value&UCNV_EXT_FROM_U_RESERVED_MASK)==0:
566 * Do not interpret values with reserved bits used, for forward compatibility,
567 * and do not even remember intermediate results with reserved bits used.
570 if(UCNV_EXT_TO_U_IS_PARTIAL(value
)) {
571 /* partial match, enter the loop below */
572 idx
=(int32_t)UCNV_EXT_FROM_U_GET_PARTIAL_INDEX(value
);
575 fromUTableUChars
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_UCHARS_INDEX
, UChar
);
576 fromUTableValues
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_VALUES_INDEX
, uint32_t);
581 /* we must not remember fallback matches when not using fallbacks */
583 /* match input units until there is a full match or the input is consumed */
585 /* go to the next section */
586 fromUSectionUChars
=fromUTableUChars
+idx
;
587 fromUSectionValues
=fromUTableValues
+idx
;
589 /* read first pair of the section */
590 length
=*fromUSectionUChars
++;
591 value
=*fromUSectionValues
++;
592 if(value
!=0 && extFromUUseMapping(useFallback
, value
, firstCP
)) {
593 /* remember longest match so far */
598 /* match pre[] then src[] */
601 } else if(j
<srcLength
) {
604 /* all input consumed, partial match */
605 if(flush
|| (length
=(i
+j
))>UCNV_EXT_MAX_UCHARS
) {
607 * end of the entire input stream, stop with the longest match so far
608 * or: partial match must not be longer than UCNV_EXT_MAX_UCHARS
609 * because it must fit into state buffers
613 /* continue with more input next time */
618 /* search for the current UChar */
619 idx
=ucnv_extFindFromU(fromUSectionUChars
, length
, c
);
621 /* no match here, stop with the longest match so far */
624 value
=fromUSectionValues
[idx
];
625 if(UCNV_EXT_FROM_U_IS_PARTIAL(value
)) {
626 /* partial match, continue */
627 idx
=(int32_t)UCNV_EXT_FROM_U_GET_PARTIAL_INDEX(value
);
629 if(extFromUUseMapping(useFallback
, value
, firstCP
)) {
630 /* full match, stop with result */
634 /* full match on fallback not taken, stop with the longest match so far */
642 /* no match at all */
645 } else /* result from firstCP trie lookup */ {
646 if(extFromUUseMapping(useFallback
, value
, firstCP
)) {
647 /* full match, stop with result */
651 /* fallback not taken */
657 if(matchValue
==UCNV_EXT_FROM_U_SUBCHAR1
) {
658 return 1; /* assert matchLength==2 */
661 *pMatchValue
=matchValue
;
666 * @param value fromUnicode mapping table value; ignores roundtrip and reserved bits
669 ucnv_extWriteFromU(UConverter
*cnv
, const int32_t *cx
,
671 char **target
, const char *targetLimit
,
672 int32_t **offsets
, int32_t srcIndex
,
673 UErrorCode
*pErrorCode
) {
674 uint8_t buffer
[1+UCNV_EXT_MAX_BYTES
];
675 const uint8_t *result
;
676 int32_t length
, prevLength
;
678 length
=UCNV_EXT_FROM_U_GET_LENGTH(value
);
679 value
=(uint32_t)UCNV_EXT_FROM_U_GET_DATA(value
);
681 /* output the result */
682 if(length
<=UCNV_EXT_FROM_U_MAX_DIRECT_LENGTH
) {
684 * Generate a byte array and then write it below.
685 * This is not the fastest possible way, but it should be ok for
686 * extension mappings, and it is much simpler.
687 * Offset and overflow handling are only done once this way.
689 uint8_t *p
=buffer
+1; /* reserve buffer[0] for shiftByte below */
692 *p
++=(uint8_t)(value
>>16);
693 case 2: /*fall through*/
694 *p
++=(uint8_t)(value
>>8);
695 case 1: /*fall through*/
698 break; /* will never occur */
702 result
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_BYTES_INDEX
, uint8_t)+value
;
705 /* with correct data we have length>0 */
707 if((prevLength
=cnv
->fromUnicodeStatus
)!=0) {
708 /* handle SI/SO stateful output */
711 if(prevLength
>1 && length
==1) {
712 /* change from double-byte mode to single-byte */
713 shiftByte
=(uint8_t)UCNV_SI
;
714 cnv
->fromUnicodeStatus
=1;
715 } else if(prevLength
==1 && length
>1) {
716 /* change from single-byte mode to double-byte */
717 shiftByte
=(uint8_t)UCNV_SO
;
718 cnv
->fromUnicodeStatus
=2;
724 /* prepend the shift byte to the result bytes */
726 if(result
!=buffer
+1) {
727 uprv_memcpy(buffer
+1, result
, length
);
734 ucnv_fromUWriteBytes(cnv
, (const char *)result
, length
,
741 * target<targetLimit; set error code for overflow
744 ucnv_extInitialMatchFromU(UConverter
*cnv
, const int32_t *cx
,
746 const UChar
**src
, const UChar
*srcLimit
,
747 char **target
, const char *targetLimit
,
748 int32_t **offsets
, int32_t srcIndex
,
750 UErrorCode
*pErrorCode
) {
751 uint32_t value
= 0; /* initialize output-only param to 0 to silence gcc */
755 match
=ucnv_extMatchFromU(cx
, cp
,
757 *src
, (int32_t)(srcLimit
-*src
),
759 cnv
->useFallback
, flush
);
761 /* reject a match if the result is a single byte for DBCS-only */
763 !(UCNV_EXT_FROM_U_GET_LENGTH(value
)==1 &&
764 cnv
->sharedData
->mbcs
.outputType
==MBCS_OUTPUT_DBCS_ONLY
)
766 /* advance src pointer for the consumed input */
767 *src
+=match
-2; /* remove 2 for the initial code point */
769 /* write result to target */
770 ucnv_extWriteFromU(cnv
, cx
,
777 /* save state for partial match */
781 /* copy the first code point */
782 cnv
->preFromUFirstCP
=cp
;
784 /* now copy the newly consumed input */
786 match
=-match
-2; /* remove 2 for the initial code point */
787 for(j
=0; j
<match
; ++j
) {
788 cnv
->preFromU
[j
]=*s
++;
790 *src
=s
; /* same as *src=srcLimit; because we reached the end of input */
791 cnv
->preFromULength
=(int8_t)match
;
793 } else if(match
==1) {
794 /* matched, no mapping but request for <subchar1> */
795 cnv
->useSubChar1
=TRUE
;
797 } else /* match==0 no match */ {
803 * Used by ISO 2022 implementation.
804 * @return number of bytes in *pValue; negative number if fallback; 0 for no mapping
807 ucnv_extSimpleMatchFromU(const int32_t *cx
,
808 UChar32 cp
, uint32_t *pValue
,
814 match
=ucnv_extMatchFromU(cx
,
821 /* write result for simple, single-character conversion */
825 isRoundtrip
=UCNV_EXT_FROM_U_IS_ROUNDTRIP(value
);
826 length
=UCNV_EXT_FROM_U_GET_LENGTH(value
);
827 value
=(uint32_t)UCNV_EXT_FROM_U_GET_DATA(value
);
829 if(length
<=UCNV_EXT_FROM_U_MAX_DIRECT_LENGTH
) {
831 return isRoundtrip
? length
: -length
;
832 #if 0 /* not currently used */
833 } else if(length
==4) {
834 /* de-serialize a 4-byte result */
835 const uint8_t *result
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_BYTES_INDEX
, uint8_t)+value
;
837 ((uint32_t)result
[0]<<24)|
838 ((uint32_t)result
[1]<<16)|
839 ((uint32_t)result
[2]<<8)|
841 return isRoundtrip
? 4 : -4;
847 * return no match because
848 * - match>1 && resultLength>4: result too long for simple conversion
849 * - match==1: no match found, <subchar1> preferred
850 * - match==0: no match found in the first place
851 * - match<0: partial match, not supported for simple conversion (and flush==TRUE)
857 * continue partial match with new input, requires cnv->preFromUFirstCP>=0
858 * never called for simple, single-character conversion
861 ucnv_extContinueMatchFromU(UConverter
*cnv
,
862 UConverterFromUnicodeArgs
*pArgs
, int32_t srcIndex
,
863 UErrorCode
*pErrorCode
) {
864 uint32_t value
= 0; /* initialize output-only param to 0 to silence gcc */
867 match
=ucnv_extMatchFromU(cnv
->sharedData
->mbcs
.extIndexes
,
868 cnv
->preFromUFirstCP
,
869 cnv
->preFromU
, cnv
->preFromULength
,
870 pArgs
->source
, (int32_t)(pArgs
->sourceLimit
-pArgs
->source
),
872 cnv
->useFallback
, pArgs
->flush
);
874 match
-=2; /* remove 2 for the initial code point */
876 if(match
>=cnv
->preFromULength
) {
877 /* advance src pointer for the consumed input */
878 pArgs
->source
+=match
-cnv
->preFromULength
;
879 cnv
->preFromULength
=0;
881 /* the match did not use all of preFromU[] - keep the rest for replay */
882 int32_t length
=cnv
->preFromULength
-match
;
883 uprv_memmove(cnv
->preFromU
, cnv
->preFromU
+match
, length
*U_SIZEOF_UCHAR
);
884 cnv
->preFromULength
=(int8_t)-length
;
887 /* finish the partial match */
888 cnv
->preFromUFirstCP
=U_SENTINEL
;
891 ucnv_extWriteFromU(cnv
, cnv
->sharedData
->mbcs
.extIndexes
,
893 &pArgs
->target
, pArgs
->targetLimit
,
894 &pArgs
->offsets
, srcIndex
,
897 /* save state for partial match */
901 /* just _append_ the newly consumed input to preFromU[] */
903 match
=-match
-2; /* remove 2 for the initial code point */
904 for(j
=cnv
->preFromULength
; j
<match
; ++j
) {
906 cnv
->preFromU
[j
]=*s
++;
908 pArgs
->source
=s
; /* same as *src=srcLimit; because we reached the end of input */
909 cnv
->preFromULength
=(int8_t)match
;
910 } else /* match==0 or 1 */ {
914 * We need to split the previous input into two parts:
916 * 1. The first code point is unmappable - that's how we got into
917 * trying the extension data in the first place.
918 * We need to move it from the preFromU buffer
919 * to the error buffer, set an error code,
920 * and prepare the rest of the previous input for 2.
922 * 2. The rest of the previous input must be converted once we
923 * come back from the callback for the first code point.
924 * At that time, we have to try again from scratch to convert
925 * these input characters.
926 * The replay will be handled by the ucnv.c conversion code.
930 /* matched, no mapping but request for <subchar1> */
931 cnv
->useSubChar1
=TRUE
;
934 /* move the first code point to the error field */
935 cnv
->fromUChar32
=cnv
->preFromUFirstCP
;
936 cnv
->preFromUFirstCP
=U_SENTINEL
;
938 /* mark preFromU for replay */
939 cnv
->preFromULength
=-cnv
->preFromULength
;
941 /* set the error code for unassigned */
942 *pErrorCode
=U_INVALID_CHAR_FOUND
;
947 extSetUseMapping(UConverterUnicodeSet which
, int32_t minLength
, uint32_t value
) {
948 if(which
==UCNV_ROUNDTRIP_SET
) {
949 // Add only code points for which the roundtrip flag is set.
950 // Do not add any fallbacks, even if ucnv_fromUnicode() would use them
951 // (fallbacks from PUA). See the API docs for ucnv_getUnicodeSet().
953 // By analogy, also do not add "good one-way" mappings.
955 // Do not add entries with reserved bits set.
956 if(((value
&(UCNV_EXT_FROM_U_ROUNDTRIP_FLAG
|UCNV_EXT_FROM_U_RESERVED_MASK
))!=
957 UCNV_EXT_FROM_U_ROUNDTRIP_FLAG
)) {
960 } else /* UCNV_ROUNDTRIP_AND_FALLBACK_SET */ {
961 // Do not add entries with reserved bits set.
962 if((value
&UCNV_EXT_FROM_U_RESERVED_MASK
)!=0) {
966 // Do not add <subchar1> entries or other (future?) pseudo-entries
967 // with an output length of 0.
968 return UCNV_EXT_FROM_U_GET_LENGTH(value
)>=minLength
;
972 ucnv_extGetUnicodeSetString(const UConverterSharedData
*sharedData
,
975 UConverterUnicodeSet which
,
978 UChar s
[UCNV_EXT_MAX_UCHARS
], int32_t length
,
979 int32_t sectionIndex
,
980 UErrorCode
*pErrorCode
) {
981 const UChar
*fromUSectionUChars
;
982 const uint32_t *fromUSectionValues
;
987 fromUSectionUChars
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_UCHARS_INDEX
, UChar
)+sectionIndex
;
988 fromUSectionValues
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_VALUES_INDEX
, uint32_t)+sectionIndex
;
990 /* read first pair of the section */
991 count
=*fromUSectionUChars
++;
992 value
=*fromUSectionValues
++;
994 if(extSetUseMapping(which
, minLength
, value
)) {
995 if(length
==U16_LENGTH(firstCP
)) {
996 /* add the initial code point */
997 sa
->add(sa
->set
, firstCP
);
999 /* add the string so far */
1000 sa
->addString(sa
->set
, s
, length
);
1004 for(i
=0; i
<count
; ++i
) {
1005 /* append this code unit and recurse or add the string */
1006 s
[length
]=fromUSectionUChars
[i
];
1007 value
=fromUSectionValues
[i
];
1010 /* no mapping, do nothing */
1011 } else if(UCNV_EXT_FROM_U_IS_PARTIAL(value
)) {
1012 ucnv_extGetUnicodeSetString(
1013 sharedData
, cx
, sa
, which
, minLength
,
1014 firstCP
, s
, length
+1,
1015 (int32_t)UCNV_EXT_FROM_U_GET_PARTIAL_INDEX(value
),
1017 } else if(extSetUseMapping(which
, minLength
, value
)) {
1018 sa
->addString(sa
->set
, s
, length
+1);
1024 ucnv_extGetUnicodeSet(const UConverterSharedData
*sharedData
,
1025 const USetAdder
*sa
,
1026 UConverterUnicodeSet which
,
1027 UConverterSetFilter filter
,
1028 UErrorCode
*pErrorCode
) {
1030 const uint16_t *stage12
, *stage3
, *ps2
, *ps3
;
1031 const uint32_t *stage3b
;
1034 int32_t st1
, stage1Length
, st2
, st3
, minLength
;
1036 UChar s
[UCNV_EXT_MAX_UCHARS
];
1040 cx
=sharedData
->mbcs
.extIndexes
;
1045 stage12
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_STAGE_12_INDEX
, uint16_t);
1046 stage3
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_STAGE_3_INDEX
, uint16_t);
1047 stage3b
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_STAGE_3B_INDEX
, uint32_t);
1049 stage1Length
=cx
[UCNV_EXT_FROM_U_STAGE_1_LENGTH
];
1051 /* enumerate the from-Unicode trie table */
1052 c
=0; /* keep track of the current code point while enumerating */
1054 if(filter
==UCNV_SET_FILTER_2022_CN
) {
1056 } else if( sharedData
->mbcs
.outputType
==MBCS_OUTPUT_DBCS_ONLY
||
1057 filter
!=UCNV_SET_FILTER_NONE
1059 /* DBCS-only, ignore single-byte results */
1066 * the trie enumeration is almost the same as
1067 * in MBCSGetUnicodeSet() for MBCS_OUTPUT_1
1069 for(st1
=0; st1
<stage1Length
; ++st1
) {
1071 if(st2
>stage1Length
) {
1073 for(st2
=0; st2
<64; ++st2
) {
1074 if((st3
=(int32_t)ps2
[st2
]<<UCNV_EXT_STAGE_2_LEFT_SHIFT
)!=0) {
1075 /* read the stage 3 block */
1079 value
=stage3b
[*ps3
++];
1081 /* no mapping, do nothing */
1082 } else if(UCNV_EXT_FROM_U_IS_PARTIAL(value
)) {
1083 // Recurse for partial results.
1085 U16_APPEND_UNSAFE(s
, length
, c
);
1086 ucnv_extGetUnicodeSetString(
1087 sharedData
, cx
, sa
, which
, minLength
,
1089 (int32_t)UCNV_EXT_FROM_U_GET_PARTIAL_INDEX(value
),
1091 } else if(extSetUseMapping(which
, minLength
, value
)) {
1093 case UCNV_SET_FILTER_2022_CN
:
1094 if(!(UCNV_EXT_FROM_U_GET_LENGTH(value
)==3 && UCNV_EXT_FROM_U_GET_DATA(value
)<=0x82ffff)) {
1098 case UCNV_SET_FILTER_SJIS
:
1099 if(!(UCNV_EXT_FROM_U_GET_LENGTH(value
)==2 && (value
=UCNV_EXT_FROM_U_GET_DATA(value
))>=0x8140 && value
<=0xeffc)) {
1103 case UCNV_SET_FILTER_GR94DBCS
:
1104 if(!(UCNV_EXT_FROM_U_GET_LENGTH(value
)==2 &&
1105 (uint16_t)((value
=UCNV_EXT_FROM_U_GET_DATA(value
))-0xa1a1)<=(0xfefe - 0xa1a1) &&
1106 (uint8_t)(value
-0xa1)<=(0xfe - 0xa1))) {
1110 case UCNV_SET_FILTER_HZ
:
1111 if(!(UCNV_EXT_FROM_U_GET_LENGTH(value
)==2 &&
1112 (uint16_t)((value
=UCNV_EXT_FROM_U_GET_DATA(value
))-0xa1a1)<=(0xfdfe - 0xa1a1) &&
1113 (uint8_t)(value
-0xa1)<=(0xfe - 0xa1))) {
1119 * UCNV_SET_FILTER_NONE,
1120 * or UCNV_SET_FILTER_DBCS_ONLY which is handled via minLength
1124 sa
->add(sa
->set
, c
);
1126 } while((++c
&0xf)!=0);
1128 c
+=16; /* empty stage 3 block */
1132 c
+=1024; /* empty stage 2 block */
1137 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */