michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (C) 2002-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ********************************************************************** michael@0: * file name: ucnv_u8.c michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2002jul01 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * UTF-8 converter implementation. Used to be in ucnv_utf.c. michael@0: * michael@0: * Also, CESU-8 implementation, see UTR 26. michael@0: * The CESU-8 converter uses all the same functions as the michael@0: * UTF-8 converter, with a branch for converting supplementary code points. michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_CONVERSION michael@0: michael@0: #include "unicode/ucnv.h" michael@0: #include "unicode/utf.h" michael@0: #include "unicode/utf8.h" michael@0: #include "unicode/utf16.h" michael@0: #include "ucnv_bld.h" michael@0: #include "ucnv_cnv.h" michael@0: #include "cmemory.h" michael@0: michael@0: /* Prototypes --------------------------------------------------------------- */ michael@0: michael@0: /* Keep these here to make finicky compilers happy */ michael@0: michael@0: U_CFUNC void ucnv_fromUnicode_UTF8(UConverterFromUnicodeArgs *args, michael@0: UErrorCode *err); michael@0: U_CFUNC void ucnv_fromUnicode_UTF8_OFFSETS_LOGIC(UConverterFromUnicodeArgs *args, michael@0: UErrorCode *err); michael@0: michael@0: michael@0: /* UTF-8 -------------------------------------------------------------------- */ michael@0: michael@0: /* UTF-8 Conversion DATA michael@0: * for more information see Unicode Standard 2.0, Transformation Formats Appendix A-9 michael@0: */ michael@0: /*static const uint32_t REPLACEMENT_CHARACTER = 0x0000FFFD;*/ michael@0: #define MAXIMUM_UCS2 0x0000FFFF michael@0: #define MAXIMUM_UTF 0x0010FFFF michael@0: #define MAXIMUM_UCS4 0x7FFFFFFF michael@0: #define HALF_SHIFT 10 michael@0: #define HALF_BASE 0x0010000 michael@0: #define HALF_MASK 0x3FF michael@0: #define SURROGATE_HIGH_START 0xD800 michael@0: #define SURROGATE_HIGH_END 0xDBFF michael@0: #define SURROGATE_LOW_START 0xDC00 michael@0: #define SURROGATE_LOW_END 0xDFFF michael@0: michael@0: /* -SURROGATE_LOW_START + HALF_BASE */ michael@0: #define SURROGATE_LOW_BASE 9216 michael@0: michael@0: static const uint32_t offsetsFromUTF8[7] = {0, michael@0: (uint32_t) 0x00000000, (uint32_t) 0x00003080, (uint32_t) 0x000E2080, michael@0: (uint32_t) 0x03C82080, (uint32_t) 0xFA082080, (uint32_t) 0x82082080 michael@0: }; michael@0: michael@0: /* END OF UTF-8 Conversion DATA */ michael@0: michael@0: static const int8_t bytesFromUTF8[256] = { michael@0: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, michael@0: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, michael@0: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, michael@0: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, michael@0: 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 michael@0: }; michael@0: michael@0: /* michael@0: * Starting with Unicode 3.0.1: michael@0: * UTF-8 byte sequences of length N _must_ encode code points of or above utf8_minChar32[N]; michael@0: * byte sequences with more than 4 bytes are illegal in UTF-8, michael@0: * which is tested with impossible values for them michael@0: */ michael@0: static const uint32_t michael@0: utf8_minChar32[7]={ 0, 0, 0x80, 0x800, 0x10000, 0xffffffff, 0xffffffff }; michael@0: michael@0: static void ucnv_toUnicode_UTF8 (UConverterToUnicodeArgs * args, michael@0: UErrorCode * err) michael@0: { michael@0: UConverter *cnv = args->converter; michael@0: const unsigned char *mySource = (unsigned char *) args->source; michael@0: UChar *myTarget = args->target; michael@0: const unsigned char *sourceLimit = (unsigned char *) args->sourceLimit; michael@0: const UChar *targetLimit = args->targetLimit; michael@0: unsigned char *toUBytes = cnv->toUBytes; michael@0: UBool isCESU8 = (UBool)(cnv->sharedData == &_CESU8Data); michael@0: uint32_t ch, ch2 = 0; michael@0: int32_t i, inBytes; michael@0: michael@0: /* Restore size of current sequence */ michael@0: if (cnv->toUnicodeStatus && myTarget < targetLimit) michael@0: { michael@0: inBytes = cnv->mode; /* restore # of bytes to consume */ michael@0: i = cnv->toULength; /* restore # of bytes consumed */ michael@0: cnv->toULength = 0; michael@0: michael@0: ch = cnv->toUnicodeStatus;/*Stores the previously calculated ch from a previous call*/ michael@0: cnv->toUnicodeStatus = 0; michael@0: goto morebytes; michael@0: } michael@0: michael@0: michael@0: while (mySource < sourceLimit && myTarget < targetLimit) michael@0: { michael@0: ch = *(mySource++); michael@0: if (ch < 0x80) /* Simple case */ michael@0: { michael@0: *(myTarget++) = (UChar) ch; michael@0: } michael@0: else michael@0: { michael@0: /* store the first char */ michael@0: toUBytes[0] = (char)ch; michael@0: inBytes = bytesFromUTF8[ch]; /* lookup current sequence length */ michael@0: i = 1; michael@0: michael@0: morebytes: michael@0: while (i < inBytes) michael@0: { michael@0: if (mySource < sourceLimit) michael@0: { michael@0: toUBytes[i] = (char) (ch2 = *mySource); michael@0: if (!U8_IS_TRAIL(ch2)) michael@0: { michael@0: break; /* i < inBytes */ michael@0: } michael@0: ch = (ch << 6) + ch2; michael@0: ++mySource; michael@0: i++; michael@0: } michael@0: else michael@0: { michael@0: /* stores a partially calculated target*/ michael@0: cnv->toUnicodeStatus = ch; michael@0: cnv->mode = inBytes; michael@0: cnv->toULength = (int8_t) i; michael@0: goto donefornow; michael@0: } michael@0: } michael@0: michael@0: /* Remove the accumulated high bits */ michael@0: ch -= offsetsFromUTF8[inBytes]; michael@0: michael@0: /* michael@0: * Legal UTF-8 byte sequences in Unicode 3.0.1 and up: michael@0: * - use only trail bytes after a lead byte (checked above) michael@0: * - use the right number of trail bytes for a given lead byte michael@0: * - encode a code point <= U+10ffff michael@0: * - use the fewest possible number of bytes for their code points michael@0: * - use at most 4 bytes (for i>=5 it is 0x10ffff= utf8_minChar32[i] && michael@0: (isCESU8 ? i <= 3 : !U_IS_SURROGATE(ch))) michael@0: { michael@0: /* Normal valid byte when the loop has not prematurely terminated (i < inBytes) */ michael@0: if (ch <= MAXIMUM_UCS2) michael@0: { michael@0: /* fits in 16 bits */ michael@0: *(myTarget++) = (UChar) ch; michael@0: } michael@0: else michael@0: { michael@0: /* write out the surrogates */ michael@0: ch -= HALF_BASE; michael@0: *(myTarget++) = (UChar) ((ch >> HALF_SHIFT) + SURROGATE_HIGH_START); michael@0: ch = (ch & HALF_MASK) + SURROGATE_LOW_START; michael@0: if (myTarget < targetLimit) michael@0: { michael@0: *(myTarget++) = (UChar)ch; michael@0: } michael@0: else michael@0: { michael@0: /* Put in overflow buffer (not handled here) */ michael@0: cnv->UCharErrorBuffer[0] = (UChar) ch; michael@0: cnv->UCharErrorBufferLength = 1; michael@0: *err = U_BUFFER_OVERFLOW_ERROR; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: else michael@0: { michael@0: cnv->toULength = (int8_t)i; michael@0: *err = U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: donefornow: michael@0: if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err)) michael@0: { michael@0: /* End of target buffer */ michael@0: *err = U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: michael@0: args->target = myTarget; michael@0: args->source = (const char *) mySource; michael@0: } michael@0: michael@0: static void ucnv_toUnicode_UTF8_OFFSETS_LOGIC (UConverterToUnicodeArgs * args, michael@0: UErrorCode * err) michael@0: { michael@0: UConverter *cnv = args->converter; michael@0: const unsigned char *mySource = (unsigned char *) args->source; michael@0: UChar *myTarget = args->target; michael@0: int32_t *myOffsets = args->offsets; michael@0: int32_t offsetNum = 0; michael@0: const unsigned char *sourceLimit = (unsigned char *) args->sourceLimit; michael@0: const UChar *targetLimit = args->targetLimit; michael@0: unsigned char *toUBytes = cnv->toUBytes; michael@0: UBool isCESU8 = (UBool)(cnv->sharedData == &_CESU8Data); michael@0: uint32_t ch, ch2 = 0; michael@0: int32_t i, inBytes; michael@0: michael@0: /* Restore size of current sequence */ michael@0: if (cnv->toUnicodeStatus && myTarget < targetLimit) michael@0: { michael@0: inBytes = cnv->mode; /* restore # of bytes to consume */ michael@0: i = cnv->toULength; /* restore # of bytes consumed */ michael@0: cnv->toULength = 0; michael@0: michael@0: ch = cnv->toUnicodeStatus;/*Stores the previously calculated ch from a previous call*/ michael@0: cnv->toUnicodeStatus = 0; michael@0: goto morebytes; michael@0: } michael@0: michael@0: while (mySource < sourceLimit && myTarget < targetLimit) michael@0: { michael@0: ch = *(mySource++); michael@0: if (ch < 0x80) /* Simple case */ michael@0: { michael@0: *(myTarget++) = (UChar) ch; michael@0: *(myOffsets++) = offsetNum++; michael@0: } michael@0: else michael@0: { michael@0: toUBytes[0] = (char)ch; michael@0: inBytes = bytesFromUTF8[ch]; michael@0: i = 1; michael@0: michael@0: morebytes: michael@0: while (i < inBytes) michael@0: { michael@0: if (mySource < sourceLimit) michael@0: { michael@0: toUBytes[i] = (char) (ch2 = *mySource); michael@0: if (!U8_IS_TRAIL(ch2)) michael@0: { michael@0: break; /* i < inBytes */ michael@0: } michael@0: ch = (ch << 6) + ch2; michael@0: ++mySource; michael@0: i++; michael@0: } michael@0: else michael@0: { michael@0: cnv->toUnicodeStatus = ch; michael@0: cnv->mode = inBytes; michael@0: cnv->toULength = (int8_t)i; michael@0: goto donefornow; michael@0: } michael@0: } michael@0: michael@0: /* Remove the accumulated high bits */ michael@0: ch -= offsetsFromUTF8[inBytes]; michael@0: michael@0: /* michael@0: * Legal UTF-8 byte sequences in Unicode 3.0.1 and up: michael@0: * - use only trail bytes after a lead byte (checked above) michael@0: * - use the right number of trail bytes for a given lead byte michael@0: * - encode a code point <= U+10ffff michael@0: * - use the fewest possible number of bytes for their code points michael@0: * - use at most 4 bytes (for i>=5 it is 0x10ffff= utf8_minChar32[i] && michael@0: (isCESU8 ? i <= 3 : !U_IS_SURROGATE(ch))) michael@0: { michael@0: /* Normal valid byte when the loop has not prematurely terminated (i < inBytes) */ michael@0: if (ch <= MAXIMUM_UCS2) michael@0: { michael@0: /* fits in 16 bits */ michael@0: *(myTarget++) = (UChar) ch; michael@0: *(myOffsets++) = offsetNum; michael@0: } michael@0: else michael@0: { michael@0: /* write out the surrogates */ michael@0: ch -= HALF_BASE; michael@0: *(myTarget++) = (UChar) ((ch >> HALF_SHIFT) + SURROGATE_HIGH_START); michael@0: *(myOffsets++) = offsetNum; michael@0: ch = (ch & HALF_MASK) + SURROGATE_LOW_START; michael@0: if (myTarget < targetLimit) michael@0: { michael@0: *(myTarget++) = (UChar)ch; michael@0: *(myOffsets++) = offsetNum; michael@0: } michael@0: else michael@0: { michael@0: cnv->UCharErrorBuffer[0] = (UChar) ch; michael@0: cnv->UCharErrorBufferLength = 1; michael@0: *err = U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } michael@0: offsetNum += i; michael@0: } michael@0: else michael@0: { michael@0: cnv->toULength = (int8_t)i; michael@0: *err = U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: donefornow: michael@0: if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err)) michael@0: { /* End of target buffer */ michael@0: *err = U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: michael@0: args->target = myTarget; michael@0: args->source = (const char *) mySource; michael@0: args->offsets = myOffsets; michael@0: } michael@0: michael@0: U_CFUNC void ucnv_fromUnicode_UTF8 (UConverterFromUnicodeArgs * args, michael@0: UErrorCode * err) michael@0: { michael@0: UConverter *cnv = args->converter; michael@0: const UChar *mySource = args->source; michael@0: const UChar *sourceLimit = args->sourceLimit; michael@0: uint8_t *myTarget = (uint8_t *) args->target; michael@0: const uint8_t *targetLimit = (uint8_t *) args->targetLimit; michael@0: uint8_t *tempPtr; michael@0: UChar32 ch; michael@0: uint8_t tempBuf[4]; michael@0: int32_t indexToWrite; michael@0: UBool isNotCESU8 = (UBool)(cnv->sharedData != &_CESU8Data); michael@0: michael@0: if (cnv->fromUChar32 && myTarget < targetLimit) michael@0: { michael@0: ch = cnv->fromUChar32; michael@0: cnv->fromUChar32 = 0; michael@0: goto lowsurrogate; michael@0: } michael@0: michael@0: while (mySource < sourceLimit && myTarget < targetLimit) michael@0: { michael@0: ch = *(mySource++); michael@0: michael@0: if (ch < 0x80) /* Single byte */ michael@0: { michael@0: *(myTarget++) = (uint8_t) ch; michael@0: } michael@0: else if (ch < 0x800) /* Double byte */ michael@0: { michael@0: *(myTarget++) = (uint8_t) ((ch >> 6) | 0xc0); michael@0: if (myTarget < targetLimit) michael@0: { michael@0: *(myTarget++) = (uint8_t) ((ch & 0x3f) | 0x80); michael@0: } michael@0: else michael@0: { michael@0: cnv->charErrorBuffer[0] = (uint8_t) ((ch & 0x3f) | 0x80); michael@0: cnv->charErrorBufferLength = 1; michael@0: *err = U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } michael@0: else { michael@0: /* Check for surrogates */ michael@0: if(U16_IS_SURROGATE(ch) && isNotCESU8) { michael@0: lowsurrogate: michael@0: if (mySource < sourceLimit) { michael@0: /* test both code units */ michael@0: if(U16_IS_SURROGATE_LEAD(ch) && U16_IS_TRAIL(*mySource)) { michael@0: /* convert and consume this supplementary code point */ michael@0: ch=U16_GET_SUPPLEMENTARY(ch, *mySource); michael@0: ++mySource; michael@0: /* exit this condition tree */ michael@0: } michael@0: else { michael@0: /* this is an unpaired trail or lead code unit */ michael@0: /* callback(illegal) */ michael@0: cnv->fromUChar32 = ch; michael@0: *err = U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } michael@0: } michael@0: else { michael@0: /* no more input */ michael@0: cnv->fromUChar32 = ch; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: /* Do we write the buffer directly for speed, michael@0: or do we have to be careful about target buffer space? */ michael@0: tempPtr = (((targetLimit - myTarget) >= 4) ? myTarget : tempBuf); michael@0: michael@0: if (ch <= MAXIMUM_UCS2) { michael@0: indexToWrite = 2; michael@0: tempPtr[0] = (uint8_t) ((ch >> 12) | 0xe0); michael@0: } michael@0: else { michael@0: indexToWrite = 3; michael@0: tempPtr[0] = (uint8_t) ((ch >> 18) | 0xf0); michael@0: tempPtr[1] = (uint8_t) (((ch >> 12) & 0x3f) | 0x80); michael@0: } michael@0: tempPtr[indexToWrite-1] = (uint8_t) (((ch >> 6) & 0x3f) | 0x80); michael@0: tempPtr[indexToWrite] = (uint8_t) ((ch & 0x3f) | 0x80); michael@0: michael@0: if (tempPtr == myTarget) { michael@0: /* There was enough space to write the codepoint directly. */ michael@0: myTarget += (indexToWrite + 1); michael@0: } michael@0: else { michael@0: /* We might run out of room soon. Write it slowly. */ michael@0: for (; tempPtr <= (tempBuf + indexToWrite); tempPtr++) { michael@0: if (myTarget < targetLimit) { michael@0: *(myTarget++) = *tempPtr; michael@0: } michael@0: else { michael@0: cnv->charErrorBuffer[cnv->charErrorBufferLength++] = *tempPtr; michael@0: *err = U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err)) michael@0: { michael@0: *err = U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: michael@0: args->target = (char *) myTarget; michael@0: args->source = mySource; michael@0: } michael@0: michael@0: U_CFUNC void ucnv_fromUnicode_UTF8_OFFSETS_LOGIC (UConverterFromUnicodeArgs * args, michael@0: UErrorCode * err) michael@0: { michael@0: UConverter *cnv = args->converter; michael@0: const UChar *mySource = args->source; michael@0: int32_t *myOffsets = args->offsets; michael@0: const UChar *sourceLimit = args->sourceLimit; michael@0: uint8_t *myTarget = (uint8_t *) args->target; michael@0: const uint8_t *targetLimit = (uint8_t *) args->targetLimit; michael@0: uint8_t *tempPtr; michael@0: UChar32 ch; michael@0: int32_t offsetNum, nextSourceIndex; michael@0: int32_t indexToWrite; michael@0: uint8_t tempBuf[4]; michael@0: UBool isNotCESU8 = (UBool)(cnv->sharedData != &_CESU8Data); michael@0: michael@0: if (cnv->fromUChar32 && myTarget < targetLimit) michael@0: { michael@0: ch = cnv->fromUChar32; michael@0: cnv->fromUChar32 = 0; michael@0: offsetNum = -1; michael@0: nextSourceIndex = 0; michael@0: goto lowsurrogate; michael@0: } else { michael@0: offsetNum = 0; michael@0: } michael@0: michael@0: while (mySource < sourceLimit && myTarget < targetLimit) michael@0: { michael@0: ch = *(mySource++); michael@0: michael@0: if (ch < 0x80) /* Single byte */ michael@0: { michael@0: *(myOffsets++) = offsetNum++; michael@0: *(myTarget++) = (char) ch; michael@0: } michael@0: else if (ch < 0x800) /* Double byte */ michael@0: { michael@0: *(myOffsets++) = offsetNum; michael@0: *(myTarget++) = (uint8_t) ((ch >> 6) | 0xc0); michael@0: if (myTarget < targetLimit) michael@0: { michael@0: *(myOffsets++) = offsetNum++; michael@0: *(myTarget++) = (uint8_t) ((ch & 0x3f) | 0x80); michael@0: } michael@0: else michael@0: { michael@0: cnv->charErrorBuffer[0] = (uint8_t) ((ch & 0x3f) | 0x80); michael@0: cnv->charErrorBufferLength = 1; michael@0: *err = U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } michael@0: else michael@0: /* Check for surrogates */ michael@0: { michael@0: nextSourceIndex = offsetNum + 1; michael@0: michael@0: if(U16_IS_SURROGATE(ch) && isNotCESU8) { michael@0: lowsurrogate: michael@0: if (mySource < sourceLimit) { michael@0: /* test both code units */ michael@0: if(U16_IS_SURROGATE_LEAD(ch) && U16_IS_TRAIL(*mySource)) { michael@0: /* convert and consume this supplementary code point */ michael@0: ch=U16_GET_SUPPLEMENTARY(ch, *mySource); michael@0: ++mySource; michael@0: ++nextSourceIndex; michael@0: /* exit this condition tree */ michael@0: } michael@0: else { michael@0: /* this is an unpaired trail or lead code unit */ michael@0: /* callback(illegal) */ michael@0: cnv->fromUChar32 = ch; michael@0: *err = U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } michael@0: } michael@0: else { michael@0: /* no more input */ michael@0: cnv->fromUChar32 = ch; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: /* Do we write the buffer directly for speed, michael@0: or do we have to be careful about target buffer space? */ michael@0: tempPtr = (((targetLimit - myTarget) >= 4) ? myTarget : tempBuf); michael@0: michael@0: if (ch <= MAXIMUM_UCS2) { michael@0: indexToWrite = 2; michael@0: tempPtr[0] = (uint8_t) ((ch >> 12) | 0xe0); michael@0: } michael@0: else { michael@0: indexToWrite = 3; michael@0: tempPtr[0] = (uint8_t) ((ch >> 18) | 0xf0); michael@0: tempPtr[1] = (uint8_t) (((ch >> 12) & 0x3f) | 0x80); michael@0: } michael@0: tempPtr[indexToWrite-1] = (uint8_t) (((ch >> 6) & 0x3f) | 0x80); michael@0: tempPtr[indexToWrite] = (uint8_t) ((ch & 0x3f) | 0x80); michael@0: michael@0: if (tempPtr == myTarget) { michael@0: /* There was enough space to write the codepoint directly. */ michael@0: myTarget += (indexToWrite + 1); michael@0: myOffsets[0] = offsetNum; michael@0: myOffsets[1] = offsetNum; michael@0: myOffsets[2] = offsetNum; michael@0: if (indexToWrite >= 3) { michael@0: myOffsets[3] = offsetNum; michael@0: } michael@0: myOffsets += (indexToWrite + 1); michael@0: } michael@0: else { michael@0: /* We might run out of room soon. Write it slowly. */ michael@0: for (; tempPtr <= (tempBuf + indexToWrite); tempPtr++) { michael@0: if (myTarget < targetLimit) michael@0: { michael@0: *(myOffsets++) = offsetNum; michael@0: *(myTarget++) = *tempPtr; michael@0: } michael@0: else michael@0: { michael@0: cnv->charErrorBuffer[cnv->charErrorBufferLength++] = *tempPtr; michael@0: *err = U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } michael@0: } michael@0: offsetNum = nextSourceIndex; michael@0: } michael@0: } michael@0: michael@0: if (mySource < sourceLimit && myTarget >= targetLimit && U_SUCCESS(*err)) michael@0: { michael@0: *err = U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: michael@0: args->target = (char *) myTarget; michael@0: args->source = mySource; michael@0: args->offsets = myOffsets; michael@0: } michael@0: michael@0: static UChar32 ucnv_getNextUChar_UTF8(UConverterToUnicodeArgs *args, michael@0: UErrorCode *err) { michael@0: UConverter *cnv; michael@0: const uint8_t *sourceInitial; michael@0: const uint8_t *source; michael@0: uint16_t extraBytesToWrite; michael@0: uint8_t myByte; michael@0: UChar32 ch; michael@0: int8_t i, isLegalSequence; michael@0: michael@0: /* UTF-8 only here, the framework handles CESU-8 to combine surrogate pairs */ michael@0: michael@0: cnv = args->converter; michael@0: sourceInitial = source = (const uint8_t *)args->source; michael@0: if (source >= (const uint8_t *)args->sourceLimit) michael@0: { michael@0: /* no input */ michael@0: *err = U_INDEX_OUTOFBOUNDS_ERROR; michael@0: return 0xffff; michael@0: } michael@0: michael@0: myByte = (uint8_t)*(source++); michael@0: if (myByte < 0x80) michael@0: { michael@0: args->source = (const char *)source; michael@0: return (UChar32)myByte; michael@0: } michael@0: michael@0: extraBytesToWrite = (uint16_t)bytesFromUTF8[myByte]; michael@0: if (extraBytesToWrite == 0) { michael@0: cnv->toUBytes[0] = myByte; michael@0: cnv->toULength = 1; michael@0: *err = U_ILLEGAL_CHAR_FOUND; michael@0: args->source = (const char *)source; michael@0: return 0xffff; michael@0: } michael@0: michael@0: /*The byte sequence is longer than the buffer area passed*/ michael@0: if (((const char *)source + extraBytesToWrite - 1) > args->sourceLimit) michael@0: { michael@0: /* check if all of the remaining bytes are trail bytes */ michael@0: cnv->toUBytes[0] = myByte; michael@0: i = 1; michael@0: *err = U_TRUNCATED_CHAR_FOUND; michael@0: while(source < (const uint8_t *)args->sourceLimit) { michael@0: if(U8_IS_TRAIL(myByte = *source)) { michael@0: cnv->toUBytes[i++] = myByte; michael@0: ++source; michael@0: } else { michael@0: /* error even before we run out of input */ michael@0: *err = U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } michael@0: } michael@0: cnv->toULength = i; michael@0: args->source = (const char *)source; michael@0: return 0xffff; michael@0: } michael@0: michael@0: isLegalSequence = 1; michael@0: ch = myByte << 6; michael@0: switch(extraBytesToWrite) michael@0: { michael@0: /* note: code falls through cases! (sic)*/ michael@0: case 6: michael@0: ch += (myByte = *source); michael@0: ch <<= 6; michael@0: if (!U8_IS_TRAIL(myByte)) michael@0: { michael@0: isLegalSequence = 0; michael@0: break; michael@0: } michael@0: ++source; michael@0: case 5: /*fall through*/ michael@0: ch += (myByte = *source); michael@0: ch <<= 6; michael@0: if (!U8_IS_TRAIL(myByte)) michael@0: { michael@0: isLegalSequence = 0; michael@0: break; michael@0: } michael@0: ++source; michael@0: case 4: /*fall through*/ michael@0: ch += (myByte = *source); michael@0: ch <<= 6; michael@0: if (!U8_IS_TRAIL(myByte)) michael@0: { michael@0: isLegalSequence = 0; michael@0: break; michael@0: } michael@0: ++source; michael@0: case 3: /*fall through*/ michael@0: ch += (myByte = *source); michael@0: ch <<= 6; michael@0: if (!U8_IS_TRAIL(myByte)) michael@0: { michael@0: isLegalSequence = 0; michael@0: break; michael@0: } michael@0: ++source; michael@0: case 2: /*fall through*/ michael@0: ch += (myByte = *source); michael@0: if (!U8_IS_TRAIL(myByte)) michael@0: { michael@0: isLegalSequence = 0; michael@0: break; michael@0: } michael@0: ++source; michael@0: }; michael@0: ch -= offsetsFromUTF8[extraBytesToWrite]; michael@0: args->source = (const char *)source; michael@0: michael@0: /* michael@0: * Legal UTF-8 byte sequences in Unicode 3.0.1 and up: michael@0: * - use only trail bytes after a lead byte (checked above) michael@0: * - use the right number of trail bytes for a given lead byte michael@0: * - encode a code point <= U+10ffff michael@0: * - use the fewest possible number of bytes for their code points michael@0: * - use at most 4 bytes (for i>=5 it is 0x10ffff= utf8_minChar32[extraBytesToWrite] && michael@0: !U_IS_SURROGATE(ch) michael@0: ) { michael@0: return ch; /* return the code point */ michael@0: } michael@0: michael@0: for(i = 0; sourceInitial < source; ++i) { michael@0: cnv->toUBytes[i] = *sourceInitial++; michael@0: } michael@0: cnv->toULength = i; michael@0: *err = U_ILLEGAL_CHAR_FOUND; michael@0: return 0xffff; michael@0: } michael@0: michael@0: /* UTF-8-from-UTF-8 conversion functions ------------------------------------ */ michael@0: michael@0: /* minimum code point values for n-byte UTF-8 sequences, n=0..4 */ michael@0: static const UChar32 michael@0: utf8_minLegal[5]={ 0, 0, 0x80, 0x800, 0x10000 }; michael@0: michael@0: /* offsets for n-byte UTF-8 sequences that were calculated with ((lead<<6)+trail)<<6+trail... */ michael@0: static const UChar32 michael@0: utf8_offsets[7]={ 0, 0, 0x3080, 0xE2080, 0x3C82080 }; michael@0: michael@0: /* "Convert" UTF-8 to UTF-8: Validate and copy. Modified from ucnv_DBCSFromUTF8(). */ michael@0: static void michael@0: ucnv_UTF8FromUTF8(UConverterFromUnicodeArgs *pFromUArgs, michael@0: UConverterToUnicodeArgs *pToUArgs, michael@0: UErrorCode *pErrorCode) { michael@0: UConverter *utf8; michael@0: const uint8_t *source, *sourceLimit; michael@0: uint8_t *target; michael@0: int32_t targetCapacity; michael@0: int32_t count; michael@0: michael@0: int8_t oldToULength, toULength, toULimit; michael@0: michael@0: UChar32 c; michael@0: uint8_t b, t1, t2; michael@0: michael@0: /* set up the local pointers */ michael@0: utf8=pToUArgs->converter; michael@0: source=(uint8_t *)pToUArgs->source; michael@0: sourceLimit=(uint8_t *)pToUArgs->sourceLimit; michael@0: target=(uint8_t *)pFromUArgs->target; michael@0: targetCapacity=(int32_t)(pFromUArgs->targetLimit-pFromUArgs->target); michael@0: michael@0: /* get the converter state from the UTF-8 UConverter */ michael@0: c=(UChar32)utf8->toUnicodeStatus; michael@0: if(c!=0) { michael@0: toULength=oldToULength=utf8->toULength; michael@0: toULimit=(int8_t)utf8->mode; michael@0: } else { michael@0: toULength=oldToULength=toULimit=0; michael@0: } michael@0: michael@0: count=(int32_t)(sourceLimit-source)+oldToULength; michael@0: if(counttargetCapacity) { michael@0: count=targetCapacity; michael@0: } michael@0: michael@0: i=0; michael@0: while(i<3 && i<(count-toULimit)) { michael@0: b=source[count-oldToULength-i-1]; michael@0: if(U8_IS_TRAIL(b)) { michael@0: ++i; michael@0: } else { michael@0: if(itoUnicodeStatus=0; michael@0: utf8->toULength=0; michael@0: goto moreBytes; michael@0: /* See note in ucnv_SBCSFromUTF8() about this goto. */ michael@0: } michael@0: michael@0: /* conversion loop */ michael@0: while(count>0) { michael@0: b=*source++; michael@0: if((int8_t)b>=0) { michael@0: /* convert ASCII */ michael@0: *target++=b; michael@0: --count; michael@0: continue; michael@0: } else { michael@0: if(b>0xe0) { michael@0: if( /* handle U+1000..U+D7FF inline */ michael@0: (t1=source[0]) >= 0x80 && ((b<0xed && (t1 <= 0xbf)) || michael@0: (b==0xed && (t1 <= 0x9f))) && michael@0: (t2=source[1]) >= 0x80 && t2 <= 0xbf michael@0: ) { michael@0: source+=2; michael@0: *target++=b; michael@0: *target++=t1; michael@0: *target++=t2; michael@0: count-=3; michael@0: continue; michael@0: } michael@0: } else if(b<0xe0) { michael@0: if( /* handle U+0080..U+07FF inline */ michael@0: b>=0xc2 && michael@0: (t1=*source) >= 0x80 && t1 <= 0xbf michael@0: ) { michael@0: ++source; michael@0: *target++=b; michael@0: *target++=t1; michael@0: count-=2; michael@0: continue; michael@0: } michael@0: } else if(b==0xe0) { michael@0: if( /* handle U+0800..U+0FFF inline */ michael@0: (t1=source[0]) >= 0xa0 && t1 <= 0xbf && michael@0: (t2=source[1]) >= 0x80 && t2 <= 0xbf michael@0: ) { michael@0: source+=2; michael@0: *target++=b; michael@0: *target++=t1; michael@0: *target++=t2; michael@0: count-=3; michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: /* handle "complicated" and error cases, and continuing partial characters */ michael@0: oldToULength=0; michael@0: toULength=1; michael@0: toULimit=U8_COUNT_TRAIL_BYTES(b)+1; michael@0: c=b; michael@0: moreBytes: michael@0: while(toULengthtoUBytes[oldToULength++]=*source++; michael@0: } michael@0: utf8->toUnicodeStatus=c; michael@0: utf8->toULength=toULength; michael@0: utf8->mode=toULimit; michael@0: pToUArgs->source=(char *)source; michael@0: pFromUArgs->target=(char *)target; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: if( toULength==toULimit && /* consumed all trail bytes */ michael@0: (toULength==3 || toULength==2) && /* BMP */ michael@0: (c-=utf8_offsets[toULength])>=utf8_minLegal[toULength] && michael@0: (c<=0xd7ff || 0xe000<=c) /* not a surrogate */ michael@0: ) { michael@0: /* legal byte sequence for BMP code point */ michael@0: } else if( michael@0: toULength==toULimit && toULength==4 && michael@0: (0x10000<=(c-=utf8_offsets[4]) && c<=0x10ffff) michael@0: ) { michael@0: /* legal byte sequence for supplementary code point */ michael@0: } else { michael@0: /* error handling: illegal UTF-8 byte sequence */ michael@0: source-=(toULength-oldToULength); michael@0: while(oldToULengthtoUBytes[oldToULength++]=*source++; michael@0: } michael@0: utf8->toULength=toULength; michael@0: pToUArgs->source=(char *)source; michael@0: pFromUArgs->target=(char *)target; michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: return; michael@0: } michael@0: michael@0: /* copy the legal byte sequence to the target */ michael@0: { michael@0: int8_t i; michael@0: michael@0: for(i=0; itoUBytes[i]; michael@0: } michael@0: source-=(toULength-oldToULength); michael@0: for(; itargetLimit) { michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } else { michael@0: b=*source; michael@0: toULimit=U8_COUNT_TRAIL_BYTES(b)+1; michael@0: if(toULimit>(sourceLimit-source)) { michael@0: /* collect a truncated byte sequence */ michael@0: toULength=0; michael@0: c=b; michael@0: for(;;) { michael@0: utf8->toUBytes[toULength++]=b; michael@0: if(++source==sourceLimit) { michael@0: /* partial byte sequence at end of source */ michael@0: utf8->toUnicodeStatus=c; michael@0: utf8->toULength=toULength; michael@0: utf8->mode=toULimit; michael@0: break; michael@0: } else if(!U8_IS_TRAIL(b=*source)) { michael@0: /* lead byte in trail byte position */ michael@0: utf8->toULength=toULength; michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } michael@0: c=(c<<6)+b; michael@0: } michael@0: } else { michael@0: /* partial-sequence target overflow: fall back to the pivoting implementation */ michael@0: *pErrorCode=U_USING_DEFAULT_WARNING; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* write back the updated pointers */ michael@0: pToUArgs->source=(char *)source; michael@0: pFromUArgs->target=(char *)target; michael@0: } michael@0: michael@0: /* UTF-8 converter data ----------------------------------------------------- */ michael@0: michael@0: static const UConverterImpl _UTF8Impl={ michael@0: UCNV_UTF8, michael@0: michael@0: NULL, michael@0: NULL, michael@0: michael@0: NULL, michael@0: NULL, michael@0: NULL, michael@0: michael@0: ucnv_toUnicode_UTF8, michael@0: ucnv_toUnicode_UTF8_OFFSETS_LOGIC, michael@0: ucnv_fromUnicode_UTF8, michael@0: ucnv_fromUnicode_UTF8_OFFSETS_LOGIC, michael@0: ucnv_getNextUChar_UTF8, michael@0: michael@0: NULL, michael@0: NULL, michael@0: NULL, michael@0: NULL, michael@0: ucnv_getNonSurrogateUnicodeSet, michael@0: michael@0: ucnv_UTF8FromUTF8, michael@0: ucnv_UTF8FromUTF8 michael@0: }; michael@0: michael@0: /* The 1208 CCSID refers to any version of Unicode of UTF-8 */ michael@0: static const UConverterStaticData _UTF8StaticData={ michael@0: sizeof(UConverterStaticData), michael@0: "UTF-8", michael@0: 1208, UCNV_IBM, UCNV_UTF8, michael@0: 1, 3, /* max 3 bytes per UChar from UTF-8 (4 bytes from surrogate _pair_) */ michael@0: { 0xef, 0xbf, 0xbd, 0 },3,FALSE,FALSE, michael@0: 0, michael@0: 0, michael@0: { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */ michael@0: }; michael@0: michael@0: michael@0: const UConverterSharedData _UTF8Data={ michael@0: sizeof(UConverterSharedData), ~((uint32_t) 0), michael@0: NULL, NULL, &_UTF8StaticData, FALSE, &_UTF8Impl, michael@0: 0 michael@0: }; michael@0: michael@0: /* CESU-8 converter data ---------------------------------------------------- */ michael@0: michael@0: static const UConverterImpl _CESU8Impl={ michael@0: UCNV_CESU8, michael@0: michael@0: NULL, michael@0: NULL, michael@0: michael@0: NULL, michael@0: NULL, michael@0: NULL, michael@0: michael@0: ucnv_toUnicode_UTF8, michael@0: ucnv_toUnicode_UTF8_OFFSETS_LOGIC, michael@0: ucnv_fromUnicode_UTF8, michael@0: ucnv_fromUnicode_UTF8_OFFSETS_LOGIC, michael@0: NULL, michael@0: michael@0: NULL, michael@0: NULL, michael@0: NULL, michael@0: NULL, michael@0: ucnv_getCompleteUnicodeSet michael@0: }; michael@0: michael@0: static const UConverterStaticData _CESU8StaticData={ michael@0: sizeof(UConverterStaticData), michael@0: "CESU-8", michael@0: 9400, /* CCSID for CESU-8 */ michael@0: UCNV_UNKNOWN, UCNV_CESU8, 1, 3, michael@0: { 0xef, 0xbf, 0xbd, 0 },3,FALSE,FALSE, michael@0: 0, michael@0: 0, michael@0: { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */ michael@0: }; michael@0: michael@0: michael@0: const UConverterSharedData _CESU8Data={ michael@0: sizeof(UConverterSharedData), ~((uint32_t) 0), michael@0: NULL, NULL, &_CESU8StaticData, FALSE, &_CESU8Impl, michael@0: 0 michael@0: }; michael@0: michael@0: #endif