2 ******************************************************************************
4 * Copyright (C) 2003-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
8 * file name: ucnv_ext.c
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"
29 /* to Unicode --------------------------------------------------------------- */
32 * @return lookup value for the byte, if found; else 0
34 static U_INLINE
uint32_t
35 ucnv_extFindToU(const uint32_t *toUSection
, int32_t length
, uint8_t byte
) {
37 int32_t i
, start
, limit
;
39 /* check the input byte against the lowest and highest section bytes */
40 start
=(int32_t)UCNV_EXT_TO_U_GET_BYTE(toUSection
[0]);
41 limit
=(int32_t)UCNV_EXT_TO_U_GET_BYTE(toUSection
[length
-1]);
42 if(byte
<start
|| limit
<byte
) {
43 return 0; /* the byte is out of range */
46 if(length
==((limit
-start
)+1)) {
47 /* direct access on a linear array */
48 return UCNV_EXT_TO_U_GET_VALUE(toUSection
[byte
-start
]); /* could be 0 */
51 /* word0 is suitable for <=toUSection[] comparison, word for <toUSection[] */
52 word0
=UCNV_EXT_TO_U_MAKE_WORD(byte
, 0);
55 * Shift byte once instead of each section word and add 0xffffff.
56 * We will compare the shifted/added byte (bbffffff) against
57 * section words which have byte values in the same bit position.
58 * If and only if byte bb < section byte ss then bbffffff<ssvvvvvv
60 * so we need not mask off the lower 24 bits of each section word.
62 word
=word0
|UCNV_EXT_TO_U_VALUE_MASK
;
75 /* linear search for the last part */
76 if(word0
<=toUSection
[start
]) {
79 if(++start
<limit
&& word0
<=toUSection
[start
]) {
82 if(++start
<limit
&& word0
<=toUSection
[start
]) {
85 /* always break at start==limit-1 */
91 if(word
<toUSection
[i
]) {
98 /* did we really find it? */
99 if(start
<limit
&& byte
==UCNV_EXT_TO_U_GET_BYTE(word
=toUSection
[start
])) {
100 return UCNV_EXT_TO_U_GET_VALUE(word
); /* never 0 */
102 return 0; /* not found */
107 * TRUE if not an SI/SO stateful converter,
108 * or if the match length fits with the current converter state
110 #define UCNV_EXT_TO_U_VERIFY_SISO_MATCH(sisoState, match) \
111 ((sisoState)<0 || ((sisoState)==0) == (match==1))
114 * this works like ucnv_extMatchFromU() except
115 * - the first character is in pre
117 * - the returned matchLength is not offset by 2
120 ucnv_extMatchToU(const int32_t *cx
, int8_t sisoState
,
121 const char *pre
, int32_t preLength
,
122 const char *src
, int32_t srcLength
,
123 uint32_t *pMatchValue
,
124 UBool useFallback
, UBool flush
) {
125 const uint32_t *toUTable
, *toUSection
;
127 uint32_t value
, matchValue
;
128 int32_t i
, j
, index
, length
, matchLength
;
131 if(cx
==NULL
|| cx
[UCNV_EXT_TO_U_LENGTH
]<=0) {
132 return 0; /* no extension data, no match */
136 toUTable
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_TO_U_INDEX
, uint32_t);
143 /* SBCS state of an SI/SO stateful converter, look at only exactly 1 byte */
145 return 0; /* no match of a DBCS sequence in SBCS mode */
146 } else if(preLength
==1) {
148 } else /* preLength==0 */ {
156 /* we must not remember fallback matches when not using fallbacks */
158 /* match input units until there is a full match or the input is consumed */
160 /* go to the next section */
161 toUSection
=toUTable
+index
;
163 /* read first pair of the section */
165 length
=UCNV_EXT_TO_U_GET_BYTE(value
);
166 value
=UCNV_EXT_TO_U_GET_VALUE(value
);
168 (UCNV_EXT_TO_U_IS_ROUNDTRIP(value
) ||
169 TO_U_USE_FALLBACK(useFallback
)) &&
170 UCNV_EXT_TO_U_VERIFY_SISO_MATCH(sisoState
, i
+j
)
172 /* remember longest match so far */
177 /* match pre[] then src[] */
180 } else if(j
<srcLength
) {
183 /* all input consumed, partial match */
184 if(flush
|| (length
=(i
+j
))>UCNV_EXT_MAX_BYTES
) {
186 * end of the entire input stream, stop with the longest match so far
187 * or: partial match must not be longer than UCNV_EXT_MAX_BYTES
188 * because it must fit into state buffers
192 /* continue with more input next time */
197 /* search for the current UChar */
198 value
=ucnv_extFindToU(toUSection
, length
, b
);
200 /* no match here, stop with the longest match so far */
203 if(UCNV_EXT_TO_U_IS_PARTIAL(value
)) {
204 /* partial match, continue */
205 index
=(int32_t)UCNV_EXT_TO_U_GET_PARTIAL_INDEX(value
);
207 if( (UCNV_EXT_TO_U_IS_ROUNDTRIP(value
) ||
208 TO_U_USE_FALLBACK(useFallback
)) &&
209 UCNV_EXT_TO_U_VERIFY_SISO_MATCH(sisoState
, i
+j
)
211 /* full match, stop with result */
215 /* full match on fallback not taken, stop with the longest match so far */
223 /* no match at all */
228 *pMatchValue
=UCNV_EXT_TO_U_MASK_ROUNDTRIP(matchValue
);
233 ucnv_extWriteToU(UConverter
*cnv
, const int32_t *cx
,
235 UChar
**target
, const UChar
*targetLimit
,
236 int32_t **offsets
, int32_t srcIndex
,
237 UErrorCode
*pErrorCode
) {
238 /* output the result */
239 if(UCNV_EXT_TO_U_IS_CODE_POINT(value
)) {
240 /* output a single code point */
241 ucnv_toUWriteCodePoint(
242 cnv
, UCNV_EXT_TO_U_GET_CODE_POINT(value
),
247 /* output a string - with correct data we have resultLength>0 */
250 UCNV_EXT_ARRAY(cx
, UCNV_EXT_TO_U_UCHARS_INDEX
, UChar
)+
251 UCNV_EXT_TO_U_GET_INDEX(value
),
252 UCNV_EXT_TO_U_GET_LENGTH(value
),
260 * get the SI/SO toU state (state 0 is for SBCS, 1 for DBCS),
261 * or 1 for DBCS-only,
262 * or -1 if the converter is not SI/SO stateful
264 * Note: For SI/SO stateful converters getting here,
265 * cnv->mode==0 is equivalent to firstLength==1.
267 #define UCNV_SISO_STATE(cnv) \
268 ((cnv)->sharedData->mbcs.outputType==MBCS_OUTPUT_2_SISO ? (int8_t)(cnv)->mode : \
269 (cnv)->sharedData->mbcs.outputType==MBCS_OUTPUT_DBCS_ONLY ? 1 : -1)
272 * target<targetLimit; set error code for overflow
275 ucnv_extInitialMatchToU(UConverter
*cnv
, const int32_t *cx
,
277 const char **src
, const char *srcLimit
,
278 UChar
**target
, const UChar
*targetLimit
,
279 int32_t **offsets
, int32_t srcIndex
,
281 UErrorCode
*pErrorCode
) {
286 match
=ucnv_extMatchToU(cx
, (int8_t)UCNV_SISO_STATE(cnv
),
287 (const char *)cnv
->toUBytes
, firstLength
,
288 *src
, (int32_t)(srcLimit
-*src
),
290 cnv
->useFallback
, flush
);
292 /* advance src pointer for the consumed input */
293 *src
+=match
-firstLength
;
295 /* write result to target */
296 ucnv_extWriteToU(cnv
, cx
,
303 /* save state for partial match */
307 /* copy the first code point */
308 s
=(const char *)cnv
->toUBytes
;
309 cnv
->preToUFirstLength
=(int8_t)firstLength
;
310 for(j
=0; j
<firstLength
; ++j
) {
314 /* now copy the newly consumed input */
317 for(; j
<match
; ++j
) {
320 *src
=s
; /* same as *src=srcLimit; because we reached the end of input */
321 cnv
->preToULength
=(int8_t)match
;
323 } else /* match==0 no match */ {
329 ucnv_extSimpleMatchToU(const int32_t *cx
,
330 const char *source
, int32_t length
,
340 match
=ucnv_extMatchToU(cx
, -1,
346 /* write result for simple, single-character conversion */
347 if(UCNV_EXT_TO_U_IS_CODE_POINT(value
)) {
348 return UCNV_EXT_TO_U_GET_CODE_POINT(value
);
353 * return no match because
354 * - match>0 && value points to string: simple conversion cannot handle multiple code points
355 * - match>0 && match!=length: not all input consumed, forbidden for this function
356 * - match==0: no match found in the first place
357 * - match<0: partial match, not supported for simple conversion (and flush==TRUE)
363 * continue partial match with new input
364 * never called for simple, single-character conversion
367 ucnv_extContinueMatchToU(UConverter
*cnv
,
368 UConverterToUnicodeArgs
*pArgs
, int32_t srcIndex
,
369 UErrorCode
*pErrorCode
) {
371 int32_t match
, length
;
373 match
=ucnv_extMatchToU(cnv
->sharedData
->mbcs
.extIndexes
, (int8_t)UCNV_SISO_STATE(cnv
),
374 cnv
->preToU
, cnv
->preToULength
,
375 pArgs
->source
, (int32_t)(pArgs
->sourceLimit
-pArgs
->source
),
377 cnv
->useFallback
, pArgs
->flush
);
379 if(match
>=cnv
->preToULength
) {
380 /* advance src pointer for the consumed input */
381 pArgs
->source
+=match
-cnv
->preToULength
;
384 /* the match did not use all of preToU[] - keep the rest for replay */
385 length
=cnv
->preToULength
-match
;
386 uprv_memmove(cnv
->preToU
, cnv
->preToU
+match
, length
);
387 cnv
->preToULength
=(int8_t)-length
;
391 ucnv_extWriteToU(cnv
, cnv
->sharedData
->mbcs
.extIndexes
,
393 &pArgs
->target
, pArgs
->targetLimit
,
394 &pArgs
->offsets
, srcIndex
,
397 /* save state for partial match */
401 /* just _append_ the newly consumed input to preToU[] */
404 for(j
=cnv
->preToULength
; j
<match
; ++j
) {
407 pArgs
->source
=s
; /* same as *src=srcLimit; because we reached the end of input */
408 cnv
->preToULength
=(int8_t)match
;
409 } else /* match==0 */ {
413 * We need to split the previous input into two parts:
415 * 1. The first codepage character is unmappable - that's how we got into
416 * trying the extension data in the first place.
417 * We need to move it from the preToU buffer
418 * to the error buffer, set an error code,
419 * and prepare the rest of the previous input for 2.
421 * 2. The rest of the previous input must be converted once we
422 * come back from the callback for the first character.
423 * At that time, we have to try again from scratch to convert
424 * these input characters.
425 * The replay will be handled by the ucnv.c conversion code.
428 /* move the first codepage character to the error field */
429 uprv_memcpy(cnv
->toUBytes
, cnv
->preToU
, cnv
->preToUFirstLength
);
430 cnv
->toULength
=cnv
->preToUFirstLength
;
432 /* move the rest up inside the buffer */
433 length
=cnv
->preToULength
-cnv
->preToUFirstLength
;
435 uprv_memmove(cnv
->preToU
, cnv
->preToU
+cnv
->preToUFirstLength
, length
);
438 /* mark preToU for replay */
439 cnv
->preToULength
=(int8_t)-length
;
441 /* set the error code for unassigned */
442 *pErrorCode
=U_INVALID_CHAR_FOUND
;
446 /* from Unicode ------------------------------------------------------------- */
449 * @return index of the UChar, if found; else <0
451 static U_INLINE
int32_t
452 ucnv_extFindFromU(const UChar
*fromUSection
, int32_t length
, UChar u
) {
453 int32_t i
, start
, limit
;
466 /* linear search for the last part */
467 if(u
<=fromUSection
[start
]) {
470 if(++start
<limit
&& u
<=fromUSection
[start
]) {
473 if(++start
<limit
&& u
<=fromUSection
[start
]) {
476 /* always break at start==limit-1 */
482 if(u
<fromUSection
[i
]) {
489 /* did we really find it? */
490 if(start
<limit
&& u
==fromUSection
[start
]) {
493 return -1; /* not found */
498 * @param cx pointer to extension data; if NULL, returns 0
499 * @param firstCP the first code point before all the other UChars
500 * @param pre UChars that must match; !initialMatch: partial match with them
501 * @param preLength length of pre, >=0
502 * @param src UChars that can be used to complete a match
503 * @param srcLength length of src, >=0
504 * @param pMatchValue [out] output result value for the match from the data structure
505 * @param useFallback "use fallback" flag, usually from cnv->useFallback
506 * @param flush TRUE if the end of the input stream is reached
507 * @return >1: matched, return value=total match length (number of input units matched)
508 * 1: matched, no mapping but request for <subchar1>
509 * (only for the first code point)
511 * <0: partial match, return value=negative total match length
512 * (partial matches are never returned for flush==TRUE)
513 * (partial matches are never returned as being longer than UCNV_EXT_MAX_UCHARS)
514 * the matchLength is 2 if only firstCP matched, and >2 if firstCP and
515 * further code units matched
518 ucnv_extMatchFromU(const int32_t *cx
,
520 const UChar
*pre
, int32_t preLength
,
521 const UChar
*src
, int32_t srcLength
,
522 uint32_t *pMatchValue
,
523 UBool useFallback
, UBool flush
) {
524 const uint16_t *stage12
, *stage3
;
525 const uint32_t *stage3b
;
527 const UChar
*fromUTableUChars
, *fromUSectionUChars
;
528 const uint32_t *fromUTableValues
, *fromUSectionValues
;
530 uint32_t value
, matchValue
;
531 int32_t i
, j
, index
, length
, matchLength
;
535 return 0; /* no extension data, no match */
538 /* trie lookup of firstCP */
539 index
=firstCP
>>10; /* stage 1 index */
540 if(index
>=cx
[UCNV_EXT_FROM_U_STAGE_1_LENGTH
]) {
541 return 0; /* the first code point is outside the trie */
544 stage12
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_STAGE_12_INDEX
, uint16_t);
545 stage3
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_STAGE_3_INDEX
, uint16_t);
546 index
=UCNV_EXT_FROM_U(stage12
, stage3
, index
, firstCP
);
548 stage3b
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_STAGE_3B_INDEX
, uint32_t);
549 value
=stage3b
[index
];
554 if(UCNV_EXT_TO_U_IS_PARTIAL(value
)) {
555 /* partial match, enter the loop below */
556 index
=(int32_t)UCNV_EXT_FROM_U_GET_PARTIAL_INDEX(value
);
559 fromUTableUChars
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_UCHARS_INDEX
, UChar
);
560 fromUTableValues
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_VALUES_INDEX
, uint32_t);
565 /* we must not remember fallback matches when not using fallbacks */
567 /* match input units until there is a full match or the input is consumed */
569 /* go to the next section */
570 fromUSectionUChars
=fromUTableUChars
+index
;
571 fromUSectionValues
=fromUTableValues
+index
;
573 /* read first pair of the section */
574 length
=*fromUSectionUChars
++;
575 value
=*fromUSectionValues
++;
577 (UCNV_EXT_FROM_U_IS_ROUNDTRIP(value
) ||
578 FROM_U_USE_FALLBACK(useFallback
, firstCP
))
580 /* remember longest match so far */
585 /* match pre[] then src[] */
588 } else if(j
<srcLength
) {
591 /* all input consumed, partial match */
592 if(flush
|| (length
=(i
+j
))>UCNV_EXT_MAX_UCHARS
) {
594 * end of the entire input stream, stop with the longest match so far
595 * or: partial match must not be longer than UCNV_EXT_MAX_UCHARS
596 * because it must fit into state buffers
600 /* continue with more input next time */
605 /* search for the current UChar */
606 index
=ucnv_extFindFromU(fromUSectionUChars
, length
, c
);
608 /* no match here, stop with the longest match so far */
611 value
=fromUSectionValues
[index
];
612 if(UCNV_EXT_FROM_U_IS_PARTIAL(value
)) {
613 /* partial match, continue */
614 index
=(int32_t)UCNV_EXT_FROM_U_GET_PARTIAL_INDEX(value
);
616 if( UCNV_EXT_FROM_U_IS_ROUNDTRIP(value
) ||
617 FROM_U_USE_FALLBACK(useFallback
, firstCP
)
619 /* full match, stop with result */
623 /* full match on fallback not taken, stop with the longest match so far */
631 /* no match at all */
634 } else /* result from firstCP trie lookup */ {
635 if( UCNV_EXT_FROM_U_IS_ROUNDTRIP(value
) ||
636 FROM_U_USE_FALLBACK(useFallback
, firstCP
)
638 /* full match, stop with result */
642 /* fallback not taken */
647 if(matchValue
&UCNV_EXT_FROM_U_RESERVED_MASK
) {
648 /* do not interpret values with reserved bits used, for forward compatibility */
653 if(matchValue
==UCNV_EXT_FROM_U_SUBCHAR1
) {
654 return 1; /* assert matchLength==2 */
657 *pMatchValue
=UCNV_EXT_FROM_U_MASK_ROUNDTRIP(matchValue
);
662 ucnv_extWriteFromU(UConverter
*cnv
, const int32_t *cx
,
664 char **target
, const char *targetLimit
,
665 int32_t **offsets
, int32_t srcIndex
,
666 UErrorCode
*pErrorCode
) {
667 uint8_t buffer
[1+UCNV_EXT_MAX_BYTES
];
668 const uint8_t *result
;
669 int32_t length
, prevLength
;
671 length
=UCNV_EXT_FROM_U_GET_LENGTH(value
);
672 value
=(uint32_t)UCNV_EXT_FROM_U_GET_DATA(value
);
674 /* output the result */
675 if(length
<=UCNV_EXT_FROM_U_MAX_DIRECT_LENGTH
) {
677 * Generate a byte array and then write it below.
678 * This is not the fastest possible way, but it should be ok for
679 * extension mappings, and it is much simpler.
680 * Offset and overflow handling are only done once this way.
682 uint8_t *p
=buffer
+1; /* reserve buffer[0] for shiftByte below */
685 *p
++=(uint8_t)(value
>>16);
687 *p
++=(uint8_t)(value
>>8);
691 break; /* will never occur */
695 result
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_BYTES_INDEX
, uint8_t)+value
;
698 /* with correct data we have length>0 */
700 if((prevLength
=cnv
->fromUnicodeStatus
)!=0) {
701 /* handle SI/SO stateful output */
704 if(prevLength
>1 && length
==1) {
705 /* change from double-byte mode to single-byte */
706 shiftByte
=(uint8_t)UCNV_SI
;
707 cnv
->fromUnicodeStatus
=1;
708 } else if(prevLength
==1 && length
>1) {
709 /* change from single-byte mode to double-byte */
710 shiftByte
=(uint8_t)UCNV_SO
;
711 cnv
->fromUnicodeStatus
=2;
717 /* prepend the shift byte to the result bytes */
719 if(result
!=buffer
+1) {
720 uprv_memcpy(buffer
+1, result
, length
);
727 ucnv_fromUWriteBytes(cnv
, (const char *)result
, length
,
734 * target<targetLimit; set error code for overflow
737 ucnv_extInitialMatchFromU(UConverter
*cnv
, const int32_t *cx
,
739 const UChar
**src
, const UChar
*srcLimit
,
740 char **target
, const char *targetLimit
,
741 int32_t **offsets
, int32_t srcIndex
,
743 UErrorCode
*pErrorCode
) {
748 match
=ucnv_extMatchFromU(cx
, cp
,
750 *src
, (int32_t)(srcLimit
-*src
),
752 cnv
->useFallback
, flush
);
754 /* reject a match if the result is a single byte for DBCS-only */
756 !(UCNV_EXT_FROM_U_GET_LENGTH(value
)==1 &&
757 cnv
->sharedData
->mbcs
.outputType
==MBCS_OUTPUT_DBCS_ONLY
)
759 /* advance src pointer for the consumed input */
760 *src
+=match
-2; /* remove 2 for the initial code point */
762 /* write result to target */
763 ucnv_extWriteFromU(cnv
, cx
,
770 /* save state for partial match */
774 /* copy the first code point */
775 cnv
->preFromUFirstCP
=cp
;
777 /* now copy the newly consumed input */
779 match
=-match
-2; /* remove 2 for the initial code point */
780 for(j
=0; j
<match
; ++j
) {
781 cnv
->preFromU
[j
]=*s
++;
783 *src
=s
; /* same as *src=srcLimit; because we reached the end of input */
784 cnv
->preFromULength
=(int8_t)match
;
786 } else if(match
==1) {
787 /* matched, no mapping but request for <subchar1> */
788 cnv
->useSubChar1
=TRUE
;
790 } else /* match==0 no match */ {
796 ucnv_extSimpleMatchFromU(const int32_t *cx
,
797 UChar32 cp
, uint32_t *pValue
,
803 match
=ucnv_extMatchFromU(cx
,
810 /* write result for simple, single-character conversion */
813 length
=UCNV_EXT_FROM_U_GET_LENGTH(value
);
814 value
=(uint32_t)UCNV_EXT_FROM_U_GET_DATA(value
);
816 if(length
<=UCNV_EXT_FROM_U_MAX_DIRECT_LENGTH
) {
819 #if 0 /* not currently used */
820 } else if(length
==4) {
821 /* de-serialize a 4-byte result */
822 const uint8_t *result
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_BYTES_INDEX
, uint8_t)+value
;
824 ((uint32_t)result
[0]<<24)|
825 ((uint32_t)result
[1]<<16)|
826 ((uint32_t)result
[2]<<8)|
834 * return no match because
835 * - match>1 && resultLength>4: result too long for simple conversion
836 * - match==1: no match found, <subchar1> preferred
837 * - match==0: no match found in the first place
838 * - match<0: partial match, not supported for simple conversion (and flush==TRUE)
844 * continue partial match with new input, requires cnv->preFromUFirstCP>=0
845 * never called for simple, single-character conversion
848 ucnv_extContinueMatchFromU(UConverter
*cnv
,
849 UConverterFromUnicodeArgs
*pArgs
, int32_t srcIndex
,
850 UErrorCode
*pErrorCode
) {
854 match
=ucnv_extMatchFromU(cnv
->sharedData
->mbcs
.extIndexes
,
855 cnv
->preFromUFirstCP
,
856 cnv
->preFromU
, cnv
->preFromULength
,
857 pArgs
->source
, (int32_t)(pArgs
->sourceLimit
-pArgs
->source
),
859 cnv
->useFallback
, pArgs
->flush
);
861 match
-=2; /* remove 2 for the initial code point */
863 if(match
>=cnv
->preFromULength
) {
864 /* advance src pointer for the consumed input */
865 pArgs
->source
+=match
-cnv
->preFromULength
;
866 cnv
->preFromULength
=0;
868 /* the match did not use all of preFromU[] - keep the rest for replay */
869 int32_t length
=cnv
->preFromULength
-match
;
870 uprv_memmove(cnv
->preFromU
, cnv
->preFromU
+match
, length
*U_SIZEOF_UCHAR
);
871 cnv
->preFromULength
=(int8_t)-length
;
874 /* finish the partial match */
875 cnv
->preFromUFirstCP
=U_SENTINEL
;
878 ucnv_extWriteFromU(cnv
, cnv
->sharedData
->mbcs
.extIndexes
,
880 &pArgs
->target
, pArgs
->targetLimit
,
881 &pArgs
->offsets
, srcIndex
,
884 /* save state for partial match */
888 /* just _append_ the newly consumed input to preFromU[] */
890 match
=-match
-2; /* remove 2 for the initial code point */
891 for(j
=cnv
->preFromULength
; j
<match
; ++j
) {
892 cnv
->preFromU
[j
]=*s
++;
894 pArgs
->source
=s
; /* same as *src=srcLimit; because we reached the end of input */
895 cnv
->preFromULength
=(int8_t)match
;
896 } else /* match==0 or 1 */ {
900 * We need to split the previous input into two parts:
902 * 1. The first code point is unmappable - that's how we got into
903 * trying the extension data in the first place.
904 * We need to move it from the preFromU buffer
905 * to the error buffer, set an error code,
906 * and prepare the rest of the previous input for 2.
908 * 2. The rest of the previous input must be converted once we
909 * come back from the callback for the first code point.
910 * At that time, we have to try again from scratch to convert
911 * these input characters.
912 * The replay will be handled by the ucnv.c conversion code.
916 /* matched, no mapping but request for <subchar1> */
917 cnv
->useSubChar1
=TRUE
;
920 /* move the first code point to the error field */
921 cnv
->fromUChar32
=cnv
->preFromUFirstCP
;
922 cnv
->preFromUFirstCP
=U_SENTINEL
;
924 /* mark preFromU for replay */
925 cnv
->preFromULength
=-cnv
->preFromULength
;
927 /* set the error code for unassigned */
928 *pErrorCode
=U_INVALID_CHAR_FOUND
;
933 ucnv_extGetUnicodeSetString(const UConverterSharedData
*sharedData
,
936 UConverterUnicodeSet which
,
939 UChar s
[UCNV_EXT_MAX_UCHARS
], int32_t length
,
940 int32_t sectionIndex
,
941 UErrorCode
*pErrorCode
) {
942 const UChar
*fromUSectionUChars
;
943 const uint32_t *fromUSectionValues
;
948 fromUSectionUChars
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_UCHARS_INDEX
, UChar
)+sectionIndex
;
949 fromUSectionValues
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_VALUES_INDEX
, uint32_t)+sectionIndex
;
951 /* read first pair of the section */
952 count
=*fromUSectionUChars
++;
953 value
=*fromUSectionValues
++;
956 UCNV_EXT_FROM_U_IS_ROUNDTRIP(value
) &&
957 UCNV_EXT_FROM_U_GET_LENGTH(value
)>=minLength
960 /* add the initial code point */
963 /* add the string so far */
964 sa
->addString(sa
->set
, s
, length
);
968 for(i
=0; i
<count
; ++i
) {
969 /* append this code unit and recurse or add the string */
970 s
[length
]=fromUSectionUChars
[i
];
971 value
=fromUSectionValues
[i
];
974 /* no mapping, do nothing */
975 } else if(UCNV_EXT_FROM_U_IS_PARTIAL(value
)) {
976 ucnv_extGetUnicodeSetString(
977 sharedData
, cx
, sa
, which
, minLength
,
978 U_SENTINEL
, s
, length
+1,
979 (int32_t)UCNV_EXT_FROM_U_GET_PARTIAL_INDEX(value
),
981 } else if(((value
&(UCNV_EXT_FROM_U_ROUNDTRIP_FLAG
|UCNV_EXT_FROM_U_RESERVED_MASK
))==
982 UCNV_EXT_FROM_U_ROUNDTRIP_FLAG
) &&
983 UCNV_EXT_FROM_U_GET_LENGTH(value
)>=minLength
985 sa
->addString(sa
->set
, s
, length
+1);
991 ucnv_extGetUnicodeSet(const UConverterSharedData
*sharedData
,
993 UConverterUnicodeSet which
,
994 UErrorCode
*pErrorCode
) {
996 const uint16_t *stage12
, *stage3
, *ps2
, *ps3
;
997 const uint32_t *stage3b
;
1000 int32_t st1
, stage1Length
, st2
, st3
, minLength
;
1002 UChar s
[UCNV_EXT_MAX_UCHARS
];
1006 cx
=sharedData
->mbcs
.extIndexes
;
1011 stage12
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_STAGE_12_INDEX
, uint16_t);
1012 stage3
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_STAGE_3_INDEX
, uint16_t);
1013 stage3b
=UCNV_EXT_ARRAY(cx
, UCNV_EXT_FROM_U_STAGE_3B_INDEX
, uint32_t);
1015 stage1Length
=cx
[UCNV_EXT_FROM_U_STAGE_1_LENGTH
];
1017 /* enumerate the from-Unicode trie table */
1018 c
=0; /* keep track of the current code point while enumerating */
1020 if(sharedData
->mbcs
.outputType
==MBCS_OUTPUT_DBCS_ONLY
) {
1021 /* DBCS-only, ignore single-byte results */
1028 * the trie enumeration is almost the same as
1029 * in MBCSGetUnicodeSet() for MBCS_OUTPUT_1
1031 for(st1
=0; st1
<stage1Length
; ++st1
) {
1033 if(st2
>stage1Length
) {
1035 for(st2
=0; st2
<64; ++st2
) {
1036 if((st3
=(int32_t)ps2
[st2
]<<UCNV_EXT_STAGE_2_LEFT_SHIFT
)!=0) {
1037 /* read the stage 3 block */
1041 * Add code points for which the roundtrip flag is set.
1042 * Do not add <subchar1> entries or other (future?) pseudo-entries
1043 * with an output length of 0, or entries with reserved bits set.
1044 * Recurse for partial results.
1047 value
=stage3b
[*ps3
++];
1049 /* no mapping, do nothing */
1050 } else if(UCNV_EXT_FROM_U_IS_PARTIAL(value
)) {
1052 U16_APPEND_UNSAFE(s
, length
, c
);
1053 ucnv_extGetUnicodeSetString(
1054 sharedData
, cx
, sa
, which
, minLength
,
1056 (int32_t)UCNV_EXT_FROM_U_GET_PARTIAL_INDEX(value
),
1058 } else if(((value
&(UCNV_EXT_FROM_U_ROUNDTRIP_FLAG
|UCNV_EXT_FROM_U_RESERVED_MASK
))==
1059 UCNV_EXT_FROM_U_ROUNDTRIP_FLAG
) &&
1060 UCNV_EXT_FROM_U_GET_LENGTH(value
)>=minLength
1062 sa
->add(sa
->set
, c
);
1064 } while((++c
&0xf)!=0);
1066 c
+=16; /* empty stage 3 block */
1070 c
+=1024; /* empty stage 2 block */
1075 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */