michael@0: /* michael@0: ****************************************************************************** michael@0: * michael@0: * Copyright (C) 2003-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ****************************************************************************** michael@0: * file name: ucnv_ext.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2003jun13 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * Conversion extensions michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_CONVERSION && !UCONFIG_NO_LEGACY_CONVERSION michael@0: michael@0: #include "unicode/uset.h" michael@0: #include "ucnv_bld.h" michael@0: #include "ucnv_cnv.h" michael@0: #include "ucnv_ext.h" michael@0: #include "cmemory.h" michael@0: #include "uassert.h" michael@0: michael@0: /* to Unicode --------------------------------------------------------------- */ michael@0: michael@0: /* michael@0: * @return lookup value for the byte, if found; else 0 michael@0: */ michael@0: static inline uint32_t michael@0: ucnv_extFindToU(const uint32_t *toUSection, int32_t length, uint8_t byte) { michael@0: uint32_t word0, word; michael@0: int32_t i, start, limit; michael@0: michael@0: /* check the input byte against the lowest and highest section bytes */ michael@0: start=(int32_t)UCNV_EXT_TO_U_GET_BYTE(toUSection[0]); michael@0: limit=(int32_t)UCNV_EXT_TO_U_GET_BYTE(toUSection[length-1]); michael@0: if(byte1) { michael@0: return 0; /* no match of a DBCS sequence in SBCS mode */ michael@0: } else if(preLength==1) { michael@0: srcLength=0; michael@0: } else /* preLength==0 */ { michael@0: if(srcLength>1) { michael@0: srcLength=1; michael@0: } michael@0: } michael@0: flush=TRUE; michael@0: } michael@0: michael@0: /* we must not remember fallback matches when not using fallbacks */ michael@0: michael@0: /* match input units until there is a full match or the input is consumed */ michael@0: for(;;) { michael@0: /* go to the next section */ michael@0: toUSection=toUTable+idx; michael@0: michael@0: /* read first pair of the section */ michael@0: value=*toUSection++; michael@0: length=UCNV_EXT_TO_U_GET_BYTE(value); michael@0: value=UCNV_EXT_TO_U_GET_VALUE(value); michael@0: if( value!=0 && michael@0: (UCNV_EXT_TO_U_IS_ROUNDTRIP(value) || michael@0: TO_U_USE_FALLBACK(useFallback)) && michael@0: UCNV_EXT_TO_U_VERIFY_SISO_MATCH(sisoState, i+j) michael@0: ) { michael@0: /* remember longest match so far */ michael@0: matchValue=value; michael@0: matchLength=i+j; michael@0: } michael@0: michael@0: /* match pre[] then src[] */ michael@0: if(iUCNV_EXT_MAX_BYTES) { michael@0: /* michael@0: * end of the entire input stream, stop with the longest match so far michael@0: * or: partial match must not be longer than UCNV_EXT_MAX_BYTES michael@0: * because it must fit into state buffers michael@0: */ michael@0: break; michael@0: } else { michael@0: /* continue with more input next time */ michael@0: return -length; michael@0: } michael@0: } michael@0: michael@0: /* search for the current UChar */ michael@0: value=ucnv_extFindToU(toUSection, length, b); michael@0: if(value==0) { michael@0: /* no match here, stop with the longest match so far */ michael@0: break; michael@0: } else { michael@0: if(UCNV_EXT_TO_U_IS_PARTIAL(value)) { michael@0: /* partial match, continue */ michael@0: idx=(int32_t)UCNV_EXT_TO_U_GET_PARTIAL_INDEX(value); michael@0: } else { michael@0: if( (UCNV_EXT_TO_U_IS_ROUNDTRIP(value) || michael@0: TO_U_USE_FALLBACK(useFallback)) && michael@0: UCNV_EXT_TO_U_VERIFY_SISO_MATCH(sisoState, i+j) michael@0: ) { michael@0: /* full match, stop with result */ michael@0: matchValue=value; michael@0: matchLength=i+j; michael@0: } else { michael@0: /* full match on fallback not taken, stop with the longest match so far */ michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if(matchLength==0) { michael@0: /* no match at all */ michael@0: return 0; michael@0: } michael@0: michael@0: /* return result */ michael@0: *pMatchValue=UCNV_EXT_TO_U_MASK_ROUNDTRIP(matchValue); michael@0: return matchLength; michael@0: } michael@0: michael@0: static inline void michael@0: ucnv_extWriteToU(UConverter *cnv, const int32_t *cx, michael@0: uint32_t value, michael@0: UChar **target, const UChar *targetLimit, michael@0: int32_t **offsets, int32_t srcIndex, michael@0: UErrorCode *pErrorCode) { michael@0: /* output the result */ michael@0: if(UCNV_EXT_TO_U_IS_CODE_POINT(value)) { michael@0: /* output a single code point */ michael@0: ucnv_toUWriteCodePoint( michael@0: cnv, UCNV_EXT_TO_U_GET_CODE_POINT(value), michael@0: target, targetLimit, michael@0: offsets, srcIndex, michael@0: pErrorCode); michael@0: } else { michael@0: /* output a string - with correct data we have resultLength>0 */ michael@0: ucnv_toUWriteUChars( michael@0: cnv, michael@0: UCNV_EXT_ARRAY(cx, UCNV_EXT_TO_U_UCHARS_INDEX, UChar)+ michael@0: UCNV_EXT_TO_U_GET_INDEX(value), michael@0: UCNV_EXT_TO_U_GET_LENGTH(value), michael@0: target, targetLimit, michael@0: offsets, srcIndex, michael@0: pErrorCode); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * get the SI/SO toU state (state 0 is for SBCS, 1 for DBCS), michael@0: * or 1 for DBCS-only, michael@0: * or -1 if the converter is not SI/SO stateful michael@0: * michael@0: * Note: For SI/SO stateful converters getting here, michael@0: * cnv->mode==0 is equivalent to firstLength==1. michael@0: */ michael@0: #define UCNV_SISO_STATE(cnv) \ michael@0: ((cnv)->sharedData->mbcs.outputType==MBCS_OUTPUT_2_SISO ? (int8_t)(cnv)->mode : \ michael@0: (cnv)->sharedData->mbcs.outputType==MBCS_OUTPUT_DBCS_ONLY ? 1 : -1) michael@0: michael@0: /* michael@0: * targettoUBytes, firstLength, michael@0: *src, (int32_t)(srcLimit-*src), michael@0: &value, michael@0: cnv->useFallback, flush); michael@0: if(match>0) { michael@0: /* advance src pointer for the consumed input */ michael@0: *src+=match-firstLength; michael@0: michael@0: /* write result to target */ michael@0: ucnv_extWriteToU(cnv, cx, michael@0: value, michael@0: target, targetLimit, michael@0: offsets, srcIndex, michael@0: pErrorCode); michael@0: return TRUE; michael@0: } else if(match<0) { michael@0: /* save state for partial match */ michael@0: const char *s; michael@0: int32_t j; michael@0: michael@0: /* copy the first code point */ michael@0: s=(const char *)cnv->toUBytes; michael@0: cnv->preToUFirstLength=(int8_t)firstLength; michael@0: for(j=0; jpreToU[j]=*s++; michael@0: } michael@0: michael@0: /* now copy the newly consumed input */ michael@0: s=*src; michael@0: match=-match; michael@0: for(; jpreToU[j]=*s++; michael@0: } michael@0: *src=s; /* same as *src=srcLimit; because we reached the end of input */ michael@0: cnv->preToULength=(int8_t)match; michael@0: return TRUE; michael@0: } else /* match==0 no match */ { michael@0: return FALSE; michael@0: } michael@0: } michael@0: michael@0: U_CFUNC UChar32 michael@0: ucnv_extSimpleMatchToU(const int32_t *cx, michael@0: const char *source, int32_t length, michael@0: UBool useFallback) { michael@0: uint32_t value = 0; /* initialize output-only param to 0 to silence gcc */ michael@0: int32_t match; michael@0: michael@0: if(length<=0) { michael@0: return 0xffff; michael@0: } michael@0: michael@0: /* try to match */ michael@0: match=ucnv_extMatchToU(cx, -1, michael@0: source, length, michael@0: NULL, 0, michael@0: &value, michael@0: useFallback, TRUE); michael@0: if(match==length) { michael@0: /* write result for simple, single-character conversion */ michael@0: if(UCNV_EXT_TO_U_IS_CODE_POINT(value)) { michael@0: return UCNV_EXT_TO_U_GET_CODE_POINT(value); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * return no match because michael@0: * - match>0 && value points to string: simple conversion cannot handle multiple code points michael@0: * - match>0 && match!=length: not all input consumed, forbidden for this function michael@0: * - match==0: no match found in the first place michael@0: * - match<0: partial match, not supported for simple conversion (and flush==TRUE) michael@0: */ michael@0: return 0xfffe; michael@0: } michael@0: michael@0: /* michael@0: * continue partial match with new input michael@0: * never called for simple, single-character conversion michael@0: */ michael@0: U_CFUNC void michael@0: ucnv_extContinueMatchToU(UConverter *cnv, michael@0: UConverterToUnicodeArgs *pArgs, int32_t srcIndex, michael@0: UErrorCode *pErrorCode) { michael@0: uint32_t value = 0; /* initialize output-only param to 0 to silence gcc */ michael@0: int32_t match, length; michael@0: michael@0: match=ucnv_extMatchToU(cnv->sharedData->mbcs.extIndexes, (int8_t)UCNV_SISO_STATE(cnv), michael@0: cnv->preToU, cnv->preToULength, michael@0: pArgs->source, (int32_t)(pArgs->sourceLimit-pArgs->source), michael@0: &value, michael@0: cnv->useFallback, pArgs->flush); michael@0: if(match>0) { michael@0: if(match>=cnv->preToULength) { michael@0: /* advance src pointer for the consumed input */ michael@0: pArgs->source+=match-cnv->preToULength; michael@0: cnv->preToULength=0; michael@0: } else { michael@0: /* the match did not use all of preToU[] - keep the rest for replay */ michael@0: length=cnv->preToULength-match; michael@0: uprv_memmove(cnv->preToU, cnv->preToU+match, length); michael@0: cnv->preToULength=(int8_t)-length; michael@0: } michael@0: michael@0: /* write result */ michael@0: ucnv_extWriteToU(cnv, cnv->sharedData->mbcs.extIndexes, michael@0: value, michael@0: &pArgs->target, pArgs->targetLimit, michael@0: &pArgs->offsets, srcIndex, michael@0: pErrorCode); michael@0: } else if(match<0) { michael@0: /* save state for partial match */ michael@0: const char *s; michael@0: int32_t j; michael@0: michael@0: /* just _append_ the newly consumed input to preToU[] */ michael@0: s=pArgs->source; michael@0: match=-match; michael@0: for(j=cnv->preToULength; jpreToU[j]=*s++; michael@0: } michael@0: pArgs->source=s; /* same as *src=srcLimit; because we reached the end of input */ michael@0: cnv->preToULength=(int8_t)match; michael@0: } else /* match==0 */ { michael@0: /* michael@0: * no match michael@0: * michael@0: * We need to split the previous input into two parts: michael@0: * michael@0: * 1. The first codepage character is unmappable - that's how we got into michael@0: * trying the extension data in the first place. michael@0: * We need to move it from the preToU buffer michael@0: * to the error buffer, set an error code, michael@0: * and prepare the rest of the previous input for 2. michael@0: * michael@0: * 2. The rest of the previous input must be converted once we michael@0: * come back from the callback for the first character. michael@0: * At that time, we have to try again from scratch to convert michael@0: * these input characters. michael@0: * The replay will be handled by the ucnv.c conversion code. michael@0: */ michael@0: michael@0: /* move the first codepage character to the error field */ michael@0: uprv_memcpy(cnv->toUBytes, cnv->preToU, cnv->preToUFirstLength); michael@0: cnv->toULength=cnv->preToUFirstLength; michael@0: michael@0: /* move the rest up inside the buffer */ michael@0: length=cnv->preToULength-cnv->preToUFirstLength; michael@0: if(length>0) { michael@0: uprv_memmove(cnv->preToU, cnv->preToU+cnv->preToUFirstLength, length); michael@0: } michael@0: michael@0: /* mark preToU for replay */ michael@0: cnv->preToULength=(int8_t)-length; michael@0: michael@0: /* set the error code for unassigned */ michael@0: *pErrorCode=U_INVALID_CHAR_FOUND; michael@0: } michael@0: } michael@0: michael@0: /* from Unicode ------------------------------------------------------------- */ michael@0: michael@0: // Use roundtrips, "good one-way" mappings, and some normal fallbacks. michael@0: static inline UBool michael@0: extFromUUseMapping(UBool useFallback, uint32_t value, UChar32 firstCP) { michael@0: return michael@0: ((value&UCNV_EXT_FROM_U_STATUS_MASK)!=0 || michael@0: FROM_U_USE_FALLBACK(useFallback, firstCP)) && michael@0: (value&UCNV_EXT_FROM_U_RESERVED_MASK)==0; michael@0: } michael@0: michael@0: /* michael@0: * @return index of the UChar, if found; else <0 michael@0: */ michael@0: static inline int32_t michael@0: ucnv_extFindFromU(const UChar *fromUSection, int32_t length, UChar u) { michael@0: int32_t i, start, limit; michael@0: michael@0: /* binary search */ michael@0: start=0; michael@0: limit=length; michael@0: for(;;) { michael@0: i=limit-start; michael@0: if(i<=1) { michael@0: break; /* done */ michael@0: } michael@0: /* start=0 michael@0: * @param src UChars that can be used to complete a match michael@0: * @param srcLength length of src, >=0 michael@0: * @param pMatchValue [out] output result value for the match from the data structure michael@0: * @param useFallback "use fallback" flag, usually from cnv->useFallback michael@0: * @param flush TRUE if the end of the input stream is reached michael@0: * @return >1: matched, return value=total match length (number of input units matched) michael@0: * 1: matched, no mapping but request for michael@0: * (only for the first code point) michael@0: * 0: no match michael@0: * <0: partial match, return value=negative total match length michael@0: * (partial matches are never returned for flush==TRUE) michael@0: * (partial matches are never returned as being longer than UCNV_EXT_MAX_UCHARS) michael@0: * the matchLength is 2 if only firstCP matched, and >2 if firstCP and michael@0: * further code units matched michael@0: */ michael@0: static int32_t michael@0: ucnv_extMatchFromU(const int32_t *cx, michael@0: UChar32 firstCP, michael@0: const UChar *pre, int32_t preLength, michael@0: const UChar *src, int32_t srcLength, michael@0: uint32_t *pMatchValue, michael@0: UBool useFallback, UBool flush) { michael@0: const uint16_t *stage12, *stage3; michael@0: const uint32_t *stage3b; michael@0: michael@0: const UChar *fromUTableUChars, *fromUSectionUChars; michael@0: const uint32_t *fromUTableValues, *fromUSectionValues; michael@0: michael@0: uint32_t value, matchValue; michael@0: int32_t i, j, idx, length, matchLength; michael@0: UChar c; michael@0: michael@0: if(cx==NULL) { michael@0: return 0; /* no extension data, no match */ michael@0: } michael@0: michael@0: /* trie lookup of firstCP */ michael@0: idx=firstCP>>10; /* stage 1 index */ michael@0: if(idx>=cx[UCNV_EXT_FROM_U_STAGE_1_LENGTH]) { michael@0: return 0; /* the first code point is outside the trie */ michael@0: } michael@0: michael@0: stage12=UCNV_EXT_ARRAY(cx, UCNV_EXT_FROM_U_STAGE_12_INDEX, uint16_t); michael@0: stage3=UCNV_EXT_ARRAY(cx, UCNV_EXT_FROM_U_STAGE_3_INDEX, uint16_t); michael@0: idx=UCNV_EXT_FROM_U(stage12, stage3, idx, firstCP); michael@0: michael@0: stage3b=UCNV_EXT_ARRAY(cx, UCNV_EXT_FROM_U_STAGE_3B_INDEX, uint32_t); michael@0: value=stage3b[idx]; michael@0: if(value==0) { michael@0: return 0; michael@0: } michael@0: michael@0: /* michael@0: * Tests for (value&UCNV_EXT_FROM_U_RESERVED_MASK)==0: michael@0: * Do not interpret values with reserved bits used, for forward compatibility, michael@0: * and do not even remember intermediate results with reserved bits used. michael@0: */ michael@0: michael@0: if(UCNV_EXT_TO_U_IS_PARTIAL(value)) { michael@0: /* partial match, enter the loop below */ michael@0: idx=(int32_t)UCNV_EXT_FROM_U_GET_PARTIAL_INDEX(value); michael@0: michael@0: /* initialize */ michael@0: fromUTableUChars=UCNV_EXT_ARRAY(cx, UCNV_EXT_FROM_U_UCHARS_INDEX, UChar); michael@0: fromUTableValues=UCNV_EXT_ARRAY(cx, UCNV_EXT_FROM_U_VALUES_INDEX, uint32_t); michael@0: michael@0: matchValue=0; michael@0: i=j=matchLength=0; michael@0: michael@0: /* we must not remember fallback matches when not using fallbacks */ michael@0: michael@0: /* match input units until there is a full match or the input is consumed */ michael@0: for(;;) { michael@0: /* go to the next section */ michael@0: fromUSectionUChars=fromUTableUChars+idx; michael@0: fromUSectionValues=fromUTableValues+idx; michael@0: michael@0: /* read first pair of the section */ michael@0: length=*fromUSectionUChars++; michael@0: value=*fromUSectionValues++; michael@0: if(value!=0 && extFromUUseMapping(useFallback, value, firstCP)) { michael@0: /* remember longest match so far */ michael@0: matchValue=value; michael@0: matchLength=2+i+j; michael@0: } michael@0: michael@0: /* match pre[] then src[] */ michael@0: if(iUCNV_EXT_MAX_UCHARS) { michael@0: /* michael@0: * end of the entire input stream, stop with the longest match so far michael@0: * or: partial match must not be longer than UCNV_EXT_MAX_UCHARS michael@0: * because it must fit into state buffers michael@0: */ michael@0: break; michael@0: } else { michael@0: /* continue with more input next time */ michael@0: return -(2+length); michael@0: } michael@0: } michael@0: michael@0: /* search for the current UChar */ michael@0: idx=ucnv_extFindFromU(fromUSectionUChars, length, c); michael@0: if(idx<0) { michael@0: /* no match here, stop with the longest match so far */ michael@0: break; michael@0: } else { michael@0: value=fromUSectionValues[idx]; michael@0: if(UCNV_EXT_FROM_U_IS_PARTIAL(value)) { michael@0: /* partial match, continue */ michael@0: idx=(int32_t)UCNV_EXT_FROM_U_GET_PARTIAL_INDEX(value); michael@0: } else { michael@0: if(extFromUUseMapping(useFallback, value, firstCP)) { michael@0: /* full match, stop with result */ michael@0: matchValue=value; michael@0: matchLength=2+i+j; michael@0: } else { michael@0: /* full match on fallback not taken, stop with the longest match so far */ michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if(matchLength==0) { michael@0: /* no match at all */ michael@0: return 0; michael@0: } michael@0: } else /* result from firstCP trie lookup */ { michael@0: if(extFromUUseMapping(useFallback, value, firstCP)) { michael@0: /* full match, stop with result */ michael@0: matchValue=value; michael@0: matchLength=2; michael@0: } else { michael@0: /* fallback not taken */ michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: /* return result */ michael@0: if(matchValue==UCNV_EXT_FROM_U_SUBCHAR1) { michael@0: return 1; /* assert matchLength==2 */ michael@0: } michael@0: michael@0: *pMatchValue=matchValue; michael@0: return matchLength; michael@0: } michael@0: michael@0: /* michael@0: * @param value fromUnicode mapping table value; ignores roundtrip and reserved bits michael@0: */ michael@0: static inline void michael@0: ucnv_extWriteFromU(UConverter *cnv, const int32_t *cx, michael@0: uint32_t value, michael@0: char **target, const char *targetLimit, michael@0: int32_t **offsets, int32_t srcIndex, michael@0: UErrorCode *pErrorCode) { michael@0: uint8_t buffer[1+UCNV_EXT_MAX_BYTES]; michael@0: const uint8_t *result; michael@0: int32_t length, prevLength; michael@0: michael@0: length=UCNV_EXT_FROM_U_GET_LENGTH(value); michael@0: value=(uint32_t)UCNV_EXT_FROM_U_GET_DATA(value); michael@0: michael@0: /* output the result */ michael@0: if(length<=UCNV_EXT_FROM_U_MAX_DIRECT_LENGTH) { michael@0: /* michael@0: * Generate a byte array and then write it below. michael@0: * This is not the fastest possible way, but it should be ok for michael@0: * extension mappings, and it is much simpler. michael@0: * Offset and overflow handling are only done once this way. michael@0: */ michael@0: uint8_t *p=buffer+1; /* reserve buffer[0] for shiftByte below */ michael@0: switch(length) { michael@0: case 3: michael@0: *p++=(uint8_t)(value>>16); michael@0: case 2: /*fall through*/ michael@0: *p++=(uint8_t)(value>>8); michael@0: case 1: /*fall through*/ michael@0: *p++=(uint8_t)value; michael@0: default: michael@0: break; /* will never occur */ michael@0: } michael@0: result=buffer+1; michael@0: } else { michael@0: result=UCNV_EXT_ARRAY(cx, UCNV_EXT_FROM_U_BYTES_INDEX, uint8_t)+value; michael@0: } michael@0: michael@0: /* with correct data we have length>0 */ michael@0: michael@0: if((prevLength=cnv->fromUnicodeStatus)!=0) { michael@0: /* handle SI/SO stateful output */ michael@0: uint8_t shiftByte; michael@0: michael@0: if(prevLength>1 && length==1) { michael@0: /* change from double-byte mode to single-byte */ michael@0: shiftByte=(uint8_t)UCNV_SI; michael@0: cnv->fromUnicodeStatus=1; michael@0: } else if(prevLength==1 && length>1) { michael@0: /* change from single-byte mode to double-byte */ michael@0: shiftByte=(uint8_t)UCNV_SO; michael@0: cnv->fromUnicodeStatus=2; michael@0: } else { michael@0: shiftByte=0; michael@0: } michael@0: michael@0: if(shiftByte!=0) { michael@0: /* prepend the shift byte to the result bytes */ michael@0: buffer[0]=shiftByte; michael@0: if(result!=buffer+1) { michael@0: uprv_memcpy(buffer+1, result, length); michael@0: } michael@0: result=buffer; michael@0: ++length; michael@0: } michael@0: } michael@0: michael@0: ucnv_fromUWriteBytes(cnv, (const char *)result, length, michael@0: target, targetLimit, michael@0: offsets, srcIndex, michael@0: pErrorCode); michael@0: } michael@0: michael@0: /* michael@0: * targetuseFallback, flush); michael@0: michael@0: /* reject a match if the result is a single byte for DBCS-only */ michael@0: if( match>=2 && michael@0: !(UCNV_EXT_FROM_U_GET_LENGTH(value)==1 && michael@0: cnv->sharedData->mbcs.outputType==MBCS_OUTPUT_DBCS_ONLY) michael@0: ) { michael@0: /* advance src pointer for the consumed input */ michael@0: *src+=match-2; /* remove 2 for the initial code point */ michael@0: michael@0: /* write result to target */ michael@0: ucnv_extWriteFromU(cnv, cx, michael@0: value, michael@0: target, targetLimit, michael@0: offsets, srcIndex, michael@0: pErrorCode); michael@0: return TRUE; michael@0: } else if(match<0) { michael@0: /* save state for partial match */ michael@0: const UChar *s; michael@0: int32_t j; michael@0: michael@0: /* copy the first code point */ michael@0: cnv->preFromUFirstCP=cp; michael@0: michael@0: /* now copy the newly consumed input */ michael@0: s=*src; michael@0: match=-match-2; /* remove 2 for the initial code point */ michael@0: for(j=0; jpreFromU[j]=*s++; michael@0: } michael@0: *src=s; /* same as *src=srcLimit; because we reached the end of input */ michael@0: cnv->preFromULength=(int8_t)match; michael@0: return TRUE; michael@0: } else if(match==1) { michael@0: /* matched, no mapping but request for */ michael@0: cnv->useSubChar1=TRUE; michael@0: return FALSE; michael@0: } else /* match==0 no match */ { michael@0: return FALSE; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * Used by ISO 2022 implementation. michael@0: * @return number of bytes in *pValue; negative number if fallback; 0 for no mapping michael@0: */ michael@0: U_CFUNC int32_t michael@0: ucnv_extSimpleMatchFromU(const int32_t *cx, michael@0: UChar32 cp, uint32_t *pValue, michael@0: UBool useFallback) { michael@0: uint32_t value; michael@0: int32_t match; michael@0: michael@0: /* try to match */ michael@0: match=ucnv_extMatchFromU(cx, michael@0: cp, michael@0: NULL, 0, michael@0: NULL, 0, michael@0: &value, michael@0: useFallback, TRUE); michael@0: if(match>=2) { michael@0: /* write result for simple, single-character conversion */ michael@0: int32_t length; michael@0: int isRoundtrip; michael@0: michael@0: isRoundtrip=UCNV_EXT_FROM_U_IS_ROUNDTRIP(value); michael@0: length=UCNV_EXT_FROM_U_GET_LENGTH(value); michael@0: value=(uint32_t)UCNV_EXT_FROM_U_GET_DATA(value); michael@0: michael@0: if(length<=UCNV_EXT_FROM_U_MAX_DIRECT_LENGTH) { michael@0: *pValue=value; michael@0: return isRoundtrip ? length : -length; michael@0: #if 0 /* not currently used */ michael@0: } else if(length==4) { michael@0: /* de-serialize a 4-byte result */ michael@0: const uint8_t *result=UCNV_EXT_ARRAY(cx, UCNV_EXT_FROM_U_BYTES_INDEX, uint8_t)+value; michael@0: *pValue= michael@0: ((uint32_t)result[0]<<24)| michael@0: ((uint32_t)result[1]<<16)| michael@0: ((uint32_t)result[2]<<8)| michael@0: result[3]; michael@0: return isRoundtrip ? 4 : -4; michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * return no match because michael@0: * - match>1 && resultLength>4: result too long for simple conversion michael@0: * - match==1: no match found, preferred michael@0: * - match==0: no match found in the first place michael@0: * - match<0: partial match, not supported for simple conversion (and flush==TRUE) michael@0: */ michael@0: return 0; michael@0: } michael@0: michael@0: /* michael@0: * continue partial match with new input, requires cnv->preFromUFirstCP>=0 michael@0: * never called for simple, single-character conversion michael@0: */ michael@0: U_CFUNC void michael@0: ucnv_extContinueMatchFromU(UConverter *cnv, michael@0: UConverterFromUnicodeArgs *pArgs, int32_t srcIndex, michael@0: UErrorCode *pErrorCode) { michael@0: uint32_t value = 0; /* initialize output-only param to 0 to silence gcc */ michael@0: int32_t match; michael@0: michael@0: match=ucnv_extMatchFromU(cnv->sharedData->mbcs.extIndexes, michael@0: cnv->preFromUFirstCP, michael@0: cnv->preFromU, cnv->preFromULength, michael@0: pArgs->source, (int32_t)(pArgs->sourceLimit-pArgs->source), michael@0: &value, michael@0: cnv->useFallback, pArgs->flush); michael@0: if(match>=2) { michael@0: match-=2; /* remove 2 for the initial code point */ michael@0: michael@0: if(match>=cnv->preFromULength) { michael@0: /* advance src pointer for the consumed input */ michael@0: pArgs->source+=match-cnv->preFromULength; michael@0: cnv->preFromULength=0; michael@0: } else { michael@0: /* the match did not use all of preFromU[] - keep the rest for replay */ michael@0: int32_t length=cnv->preFromULength-match; michael@0: uprv_memmove(cnv->preFromU, cnv->preFromU+match, length*U_SIZEOF_UCHAR); michael@0: cnv->preFromULength=(int8_t)-length; michael@0: } michael@0: michael@0: /* finish the partial match */ michael@0: cnv->preFromUFirstCP=U_SENTINEL; michael@0: michael@0: /* write result */ michael@0: ucnv_extWriteFromU(cnv, cnv->sharedData->mbcs.extIndexes, michael@0: value, michael@0: &pArgs->target, pArgs->targetLimit, michael@0: &pArgs->offsets, srcIndex, michael@0: pErrorCode); michael@0: } else if(match<0) { michael@0: /* save state for partial match */ michael@0: const UChar *s; michael@0: int32_t j; michael@0: michael@0: /* just _append_ the newly consumed input to preFromU[] */ michael@0: s=pArgs->source; michael@0: match=-match-2; /* remove 2 for the initial code point */ michael@0: for(j=cnv->preFromULength; j=0); michael@0: cnv->preFromU[j]=*s++; michael@0: } michael@0: pArgs->source=s; /* same as *src=srcLimit; because we reached the end of input */ michael@0: cnv->preFromULength=(int8_t)match; michael@0: } else /* match==0 or 1 */ { michael@0: /* michael@0: * no match michael@0: * michael@0: * We need to split the previous input into two parts: michael@0: * michael@0: * 1. The first code point is unmappable - that's how we got into michael@0: * trying the extension data in the first place. michael@0: * We need to move it from the preFromU buffer michael@0: * to the error buffer, set an error code, michael@0: * and prepare the rest of the previous input for 2. michael@0: * michael@0: * 2. The rest of the previous input must be converted once we michael@0: * come back from the callback for the first code point. michael@0: * At that time, we have to try again from scratch to convert michael@0: * these input characters. michael@0: * The replay will be handled by the ucnv.c conversion code. michael@0: */ michael@0: michael@0: if(match==1) { michael@0: /* matched, no mapping but request for */ michael@0: cnv->useSubChar1=TRUE; michael@0: } michael@0: michael@0: /* move the first code point to the error field */ michael@0: cnv->fromUChar32=cnv->preFromUFirstCP; michael@0: cnv->preFromUFirstCP=U_SENTINEL; michael@0: michael@0: /* mark preFromU for replay */ michael@0: cnv->preFromULength=-cnv->preFromULength; michael@0: michael@0: /* set the error code for unassigned */ michael@0: *pErrorCode=U_INVALID_CHAR_FOUND; michael@0: } michael@0: } michael@0: michael@0: static UBool michael@0: extSetUseMapping(UConverterUnicodeSet which, int32_t minLength, uint32_t value) { michael@0: if(which==UCNV_ROUNDTRIP_SET) { michael@0: // Add only code points for which the roundtrip flag is set. michael@0: // Do not add any fallbacks, even if ucnv_fromUnicode() would use them michael@0: // (fallbacks from PUA). See the API docs for ucnv_getUnicodeSet(). michael@0: // michael@0: // By analogy, also do not add "good one-way" mappings. michael@0: // michael@0: // Do not add entries with reserved bits set. michael@0: if(((value&(UCNV_EXT_FROM_U_ROUNDTRIP_FLAG|UCNV_EXT_FROM_U_RESERVED_MASK))!= michael@0: UCNV_EXT_FROM_U_ROUNDTRIP_FLAG)) { michael@0: return FALSE; michael@0: } michael@0: } else /* UCNV_ROUNDTRIP_AND_FALLBACK_SET */ { michael@0: // Do not add entries with reserved bits set. michael@0: if((value&UCNV_EXT_FROM_U_RESERVED_MASK)!=0) { michael@0: return FALSE; michael@0: } michael@0: } michael@0: // Do not add entries or other (future?) pseudo-entries michael@0: // with an output length of 0. michael@0: return UCNV_EXT_FROM_U_GET_LENGTH(value)>=minLength; michael@0: } michael@0: michael@0: static void michael@0: ucnv_extGetUnicodeSetString(const UConverterSharedData *sharedData, michael@0: const int32_t *cx, michael@0: const USetAdder *sa, michael@0: UConverterUnicodeSet which, michael@0: int32_t minLength, michael@0: UChar32 firstCP, michael@0: UChar s[UCNV_EXT_MAX_UCHARS], int32_t length, michael@0: int32_t sectionIndex, michael@0: UErrorCode *pErrorCode) { michael@0: const UChar *fromUSectionUChars; michael@0: const uint32_t *fromUSectionValues; michael@0: michael@0: uint32_t value; michael@0: int32_t i, count; michael@0: michael@0: fromUSectionUChars=UCNV_EXT_ARRAY(cx, UCNV_EXT_FROM_U_UCHARS_INDEX, UChar)+sectionIndex; michael@0: fromUSectionValues=UCNV_EXT_ARRAY(cx, UCNV_EXT_FROM_U_VALUES_INDEX, uint32_t)+sectionIndex; michael@0: michael@0: /* read first pair of the section */ michael@0: count=*fromUSectionUChars++; michael@0: value=*fromUSectionValues++; michael@0: michael@0: if(extSetUseMapping(which, minLength, value)) { michael@0: if(length==U16_LENGTH(firstCP)) { michael@0: /* add the initial code point */ michael@0: sa->add(sa->set, firstCP); michael@0: } else { michael@0: /* add the string so far */ michael@0: sa->addString(sa->set, s, length); michael@0: } michael@0: } michael@0: michael@0: for(i=0; iaddString(sa->set, s, length+1); michael@0: } michael@0: } michael@0: } michael@0: michael@0: U_CFUNC void michael@0: ucnv_extGetUnicodeSet(const UConverterSharedData *sharedData, michael@0: const USetAdder *sa, michael@0: UConverterUnicodeSet which, michael@0: UConverterSetFilter filter, michael@0: UErrorCode *pErrorCode) { michael@0: const int32_t *cx; michael@0: const uint16_t *stage12, *stage3, *ps2, *ps3; michael@0: const uint32_t *stage3b; michael@0: michael@0: uint32_t value; michael@0: int32_t st1, stage1Length, st2, st3, minLength; michael@0: michael@0: UChar s[UCNV_EXT_MAX_UCHARS]; michael@0: UChar32 c; michael@0: int32_t length; michael@0: michael@0: cx=sharedData->mbcs.extIndexes; michael@0: if(cx==NULL) { michael@0: return; michael@0: } michael@0: michael@0: stage12=UCNV_EXT_ARRAY(cx, UCNV_EXT_FROM_U_STAGE_12_INDEX, uint16_t); michael@0: stage3=UCNV_EXT_ARRAY(cx, UCNV_EXT_FROM_U_STAGE_3_INDEX, uint16_t); michael@0: stage3b=UCNV_EXT_ARRAY(cx, UCNV_EXT_FROM_U_STAGE_3B_INDEX, uint32_t); michael@0: michael@0: stage1Length=cx[UCNV_EXT_FROM_U_STAGE_1_LENGTH]; michael@0: michael@0: /* enumerate the from-Unicode trie table */ michael@0: c=0; /* keep track of the current code point while enumerating */ michael@0: michael@0: if(filter==UCNV_SET_FILTER_2022_CN) { michael@0: minLength=3; michael@0: } else if( sharedData->mbcs.outputType==MBCS_OUTPUT_DBCS_ONLY || michael@0: filter!=UCNV_SET_FILTER_NONE michael@0: ) { michael@0: /* DBCS-only, ignore single-byte results */ michael@0: minLength=2; michael@0: } else { michael@0: minLength=1; michael@0: } michael@0: michael@0: /* michael@0: * the trie enumeration is almost the same as michael@0: * in MBCSGetUnicodeSet() for MBCS_OUTPUT_1 michael@0: */ michael@0: for(st1=0; st1stage1Length) { michael@0: ps2=stage12+st2; michael@0: for(st2=0; st2<64; ++st2) { michael@0: if((st3=(int32_t)ps2[st2]<=0x8140 && value<=0xeffc)) { michael@0: continue; michael@0: } michael@0: break; michael@0: case UCNV_SET_FILTER_GR94DBCS: michael@0: if(!(UCNV_EXT_FROM_U_GET_LENGTH(value)==2 && michael@0: (uint16_t)((value=UCNV_EXT_FROM_U_GET_DATA(value))-0xa1a1)<=(0xfefe - 0xa1a1) && michael@0: (uint8_t)(value-0xa1)<=(0xfe - 0xa1))) { michael@0: continue; michael@0: } michael@0: break; michael@0: case UCNV_SET_FILTER_HZ: michael@0: if(!(UCNV_EXT_FROM_U_GET_LENGTH(value)==2 && michael@0: (uint16_t)((value=UCNV_EXT_FROM_U_GET_DATA(value))-0xa1a1)<=(0xfdfe - 0xa1a1) && michael@0: (uint8_t)(value-0xa1)<=(0xfe - 0xa1))) { michael@0: continue; michael@0: } michael@0: break; michael@0: default: michael@0: /* michael@0: * UCNV_SET_FILTER_NONE, michael@0: * or UCNV_SET_FILTER_DBCS_ONLY which is handled via minLength michael@0: */ michael@0: break; michael@0: } michael@0: sa->add(sa->set, c); michael@0: } michael@0: } while((++c&0xf)!=0); michael@0: } else { michael@0: c+=16; /* empty stage 3 block */ michael@0: } michael@0: } michael@0: } else { michael@0: c+=1024; /* empty stage 2 block */ michael@0: } michael@0: } michael@0: } michael@0: michael@0: #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */