michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (C) 2002-2011, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ********************************************************************** michael@0: * file name: ucnv_u7.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-7 converter implementation. Used to be in ucnv_utf.c. 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 "ucnv_bld.h" michael@0: #include "ucnv_cnv.h" michael@0: #include "uassert.h" michael@0: michael@0: /* UTF-7 -------------------------------------------------------------------- */ michael@0: michael@0: /* michael@0: * UTF-7 is a stateful encoding of Unicode. michael@0: * It is defined in RFC 2152. (http://www.ietf.org/rfc/rfc2152.txt) michael@0: * It was intended for use in Internet email systems, using in its bytewise michael@0: * encoding only a subset of 7-bit US-ASCII. michael@0: * UTF-7 is deprecated in favor of UTF-8/16/32 and SCSU, but still michael@0: * occasionally used. michael@0: * michael@0: * For converting Unicode to UTF-7, the RFC allows to encode some US-ASCII michael@0: * characters directly or in base64. Especially, the characters in set O michael@0: * as defined in the RFC (see below) may be encoded directly but are not michael@0: * allowed in, e.g., email headers. michael@0: * By default, the ICU UTF-7 converter encodes set O directly. michael@0: * By choosing the option "version=1", set O will be escaped instead. michael@0: * For example: michael@0: * utf7Converter=ucnv_open("UTF-7,version=1"); michael@0: * michael@0: * For details about email headers see RFC 2047. michael@0: */ michael@0: michael@0: /* michael@0: * Tests for US-ASCII characters belonging to character classes michael@0: * defined in UTF-7. michael@0: * michael@0: * Set D (directly encoded characters) consists of the following michael@0: * characters: the upper and lower case letters A through Z michael@0: * and a through z, the 10 digits 0-9, and the following nine special michael@0: * characters (note that "+" and "=" are omitted): michael@0: * '(),-./:? michael@0: * michael@0: * Set O (optional direct characters) consists of the following michael@0: * characters (note that "\" and "~" are omitted): michael@0: * !"#$%&*;<=>@[]^_`{|} michael@0: * michael@0: * According to the rules in RFC 2152, the byte values for the following michael@0: * US-ASCII characters are not used in UTF-7 and are therefore illegal: michael@0: * - all C0 control codes except for CR LF TAB michael@0: * - BACKSLASH michael@0: * - TILDE michael@0: * - DEL michael@0: * - all codes beyond US-ASCII, i.e. all >127 michael@0: */ michael@0: #define inSetD(c) \ michael@0: ((uint8_t)((c)-97)<26 || (uint8_t)((c)-65)<26 || /* letters */ \ michael@0: (uint8_t)((c)-48)<10 || /* digits */ \ michael@0: (uint8_t)((c)-39)<3 || /* '() */ \ michael@0: (uint8_t)((c)-44)<4 || /* ,-./ */ \ michael@0: (c)==58 || (c)==63 /* :? */ \ michael@0: ) michael@0: michael@0: #define inSetO(c) \ michael@0: ((uint8_t)((c)-33)<6 || /* !"#$%& */ \ michael@0: (uint8_t)((c)-59)<4 || /* ;<=> */ \ michael@0: (uint8_t)((c)-93)<4 || /* ]^_` */ \ michael@0: (uint8_t)((c)-123)<3 || /* {|} */ \ michael@0: (c)==42 || (c)==64 || (c)==91 /* *@[ */ \ michael@0: ) michael@0: michael@0: #define isCRLFTAB(c) ((c)==13 || (c)==10 || (c)==9) michael@0: #define isCRLFSPTAB(c) ((c)==32 || (c)==13 || (c)==10 || (c)==9) michael@0: michael@0: #define PLUS 43 michael@0: #define MINUS 45 michael@0: #define BACKSLASH 92 michael@0: #define TILDE 126 michael@0: michael@0: /* legal byte values: all US-ASCII graphic characters from space to before tilde, and CR LF TAB */ michael@0: #define isLegalUTF7(c) (((uint8_t)((c)-32)<94 && (c)!=BACKSLASH) || isCRLFTAB(c)) michael@0: michael@0: /* encode directly sets D and O and CR LF SP TAB */ michael@0: static const UBool encodeDirectlyMaximum[128]={ michael@0: /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: michael@0: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, michael@0: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, michael@0: michael@0: 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, 0, 1, 1, 1, michael@0: michael@0: 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, 0, 0 michael@0: }; michael@0: michael@0: /* encode directly set D and CR LF SP TAB but not set O */ michael@0: static const UBool encodeDirectlyRestricted[128]={ michael@0: /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, michael@0: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, michael@0: michael@0: 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, michael@0: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, michael@0: michael@0: 0, 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, 0, 0, 0, 0, 0, michael@0: michael@0: 0, 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, 0, 0, 0, 0, 0 michael@0: }; michael@0: michael@0: static const uint8_t michael@0: toBase64[64]={ michael@0: /* A-Z */ michael@0: 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, michael@0: 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, michael@0: /* a-z */ michael@0: 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, michael@0: 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, michael@0: /* 0-9 */ michael@0: 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, michael@0: /* +/ */ michael@0: 43, 47 michael@0: }; michael@0: michael@0: static const int8_t michael@0: fromBase64[128]={ michael@0: /* C0 controls, -1 for legal ones (CR LF TAB), -3 for illegal ones */ michael@0: -3, -3, -3, -3, -3, -3, -3, -3, -3, -1, -1, -3, -3, -1, -3, -3, michael@0: -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, michael@0: michael@0: /* general punctuation with + and / and a special value (-2) for - */ michael@0: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -2, -1, 63, michael@0: /* digits */ michael@0: 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, michael@0: michael@0: /* A-Z */ michael@0: -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, michael@0: 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -3, -1, -1, -1, michael@0: michael@0: /* a-z */ michael@0: -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, michael@0: 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -3, -3 michael@0: }; michael@0: michael@0: /* michael@0: * converter status values: michael@0: * michael@0: * toUnicodeStatus: michael@0: * 24 inDirectMode (boolean) michael@0: * 23..16 base64Counter (-1..7) michael@0: * 15..0 bits (up to 14 bits incoming base64) michael@0: * michael@0: * fromUnicodeStatus: michael@0: * 31..28 version (0: set O direct 1: set O escaped) michael@0: * 24 inDirectMode (boolean) michael@0: * 23..16 base64Counter (0..2) michael@0: * 7..0 bits (6 bits outgoing base64) michael@0: * michael@0: */ michael@0: michael@0: static void michael@0: _UTF7Reset(UConverter *cnv, UConverterResetChoice choice) { michael@0: if(choice<=UCNV_RESET_TO_UNICODE) { michael@0: /* reset toUnicode */ michael@0: cnv->toUnicodeStatus=0x1000000; /* inDirectMode=TRUE */ michael@0: cnv->toULength=0; michael@0: } michael@0: if(choice!=UCNV_RESET_TO_UNICODE) { michael@0: /* reset fromUnicode */ michael@0: cnv->fromUnicodeStatus=(cnv->fromUnicodeStatus&0xf0000000)|0x1000000; /* keep version, inDirectMode=TRUE */ michael@0: } michael@0: } michael@0: michael@0: static void michael@0: _UTF7Open(UConverter *cnv, michael@0: UConverterLoadArgs *pArgs, michael@0: UErrorCode *pErrorCode) { michael@0: if(UCNV_GET_VERSION(cnv)<=1) { michael@0: /* TODO(markus): Should just use cnv->options rather than copying the version number. */ michael@0: cnv->fromUnicodeStatus=UCNV_GET_VERSION(cnv)<<28; michael@0: _UTF7Reset(cnv, UCNV_RESET_BOTH); michael@0: } else { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: _UTF7ToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs, michael@0: UErrorCode *pErrorCode) { michael@0: UConverter *cnv; michael@0: const uint8_t *source, *sourceLimit; michael@0: UChar *target; michael@0: const UChar *targetLimit; michael@0: int32_t *offsets; michael@0: michael@0: uint8_t *bytes; michael@0: uint8_t byteIndex; michael@0: michael@0: int32_t length, targetCapacity; michael@0: michael@0: /* UTF-7 state */ michael@0: uint16_t bits; michael@0: int8_t base64Counter; michael@0: UBool inDirectMode; michael@0: michael@0: int8_t base64Value; michael@0: michael@0: int32_t sourceIndex, nextSourceIndex; michael@0: michael@0: uint8_t b; michael@0: /* set up the local pointers */ michael@0: cnv=pArgs->converter; michael@0: michael@0: source=(const uint8_t *)pArgs->source; michael@0: sourceLimit=(const uint8_t *)pArgs->sourceLimit; michael@0: target=pArgs->target; michael@0: targetLimit=pArgs->targetLimit; michael@0: offsets=pArgs->offsets; michael@0: /* get the state machine state */ michael@0: { michael@0: uint32_t status=cnv->toUnicodeStatus; michael@0: inDirectMode=(UBool)((status>>24)&1); michael@0: base64Counter=(int8_t)(status>>16); michael@0: bits=(uint16_t)status; michael@0: } michael@0: bytes=cnv->toUBytes; michael@0: byteIndex=cnv->toULength; michael@0: michael@0: /* sourceIndex=-1 if the current character began in the previous buffer */ michael@0: sourceIndex=byteIndex==0 ? 0 : -1; michael@0: nextSourceIndex=0; michael@0: michael@0: if(inDirectMode) { michael@0: directMode: michael@0: /* michael@0: * In Direct Mode, most US-ASCII characters are encoded directly, i.e., michael@0: * with their US-ASCII byte values. michael@0: * Backslash and Tilde and most control characters are not allowed in UTF-7. michael@0: * A plus sign starts Unicode (or "escape") Mode. michael@0: * michael@0: * In Direct Mode, only the sourceIndex is used. michael@0: */ michael@0: byteIndex=0; michael@0: length=(int32_t)(sourceLimit-source); michael@0: targetCapacity=(int32_t)(targetLimit-target); michael@0: if(length>targetCapacity) { michael@0: length=targetCapacity; michael@0: } michael@0: while(length>0) { michael@0: b=*source++; michael@0: if(!isLegalUTF7(b)) { michael@0: /* illegal */ michael@0: bytes[0]=b; michael@0: byteIndex=1; michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } else if(b!=PLUS) { michael@0: /* write directly encoded character */ michael@0: *target++=b; michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: } else /* PLUS */ { michael@0: /* switch to Unicode mode */ michael@0: nextSourceIndex=++sourceIndex; michael@0: inDirectMode=FALSE; michael@0: byteIndex=0; michael@0: bits=0; michael@0: base64Counter=-1; michael@0: goto unicodeMode; michael@0: } michael@0: --length; michael@0: } michael@0: if(source=targetLimit) { michael@0: /* target is full */ michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } else { michael@0: unicodeMode: michael@0: /* michael@0: * In Unicode (or "escape") Mode, UTF-16BE is base64-encoded. michael@0: * The base64 sequence ends with any character that is not in the base64 alphabet. michael@0: * A terminating minus sign is consumed. michael@0: * michael@0: * In Unicode Mode, the sourceIndex has the index to the start of the current michael@0: * base64 bytes, while nextSourceIndex is precisely parallel to source, michael@0: * keeping the index to the following byte. michael@0: * Note that in 2 out of 3 cases, UChars overlap within a base64 byte. michael@0: */ michael@0: while(source=126 || (base64Value=fromBase64[b])==-3 || base64Value==-1) { michael@0: /* either michael@0: * base64Value==-1 for any legal character except base64 and minus sign, or michael@0: * base64Value==-3 for illegal characters: michael@0: * 1. In either case, leave Unicode mode. michael@0: * 2.1. If we ended with an incomplete UChar or none after the +, then michael@0: * generate an error for the preceding erroneous sequence and deal with michael@0: * the current (possibly illegal) character next time through. michael@0: * 2.2. Else the current char comes after a complete UChar, which was already michael@0: * pushed to the output buf, so: michael@0: * 2.2.1. If the current char is legal, just save it for processing next time. michael@0: * It may be for example, a plus which we need to deal with in direct mode. michael@0: * 2.2.2. Else if the current char is illegal, we might as well deal with it here. michael@0: */ michael@0: inDirectMode=TRUE; michael@0: if(base64Counter==-1) { michael@0: /* illegal: + immediately followed by something other than base64 or minus sign */ michael@0: /* include the plus sign in the reported sequence, but not the subsequent char */ michael@0: --source; michael@0: bytes[0]=PLUS; michael@0: byteIndex=1; michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } else if(bits!=0) { michael@0: /* bits are illegally left over, a UChar is incomplete */ michael@0: /* don't include current char (legal or illegal) in error seq */ michael@0: --source; michael@0: --byteIndex; michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } else { michael@0: /* previous UChar was complete */ michael@0: if(base64Value==-3) { michael@0: /* current character is illegal, deal with it here */ michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } else { michael@0: /* un-read the current character in case it is a plus sign */ michael@0: --source; michael@0: sourceIndex=nextSourceIndex-1; michael@0: goto directMode; michael@0: } michael@0: } michael@0: } else if(base64Value>=0) { michael@0: /* collect base64 bytes into UChars */ michael@0: switch(base64Counter) { michael@0: case -1: /* -1 is immediately after the + */ michael@0: case 0: michael@0: bits=base64Value; michael@0: base64Counter=1; michael@0: break; michael@0: case 1: michael@0: case 3: michael@0: case 4: michael@0: case 6: michael@0: bits=(uint16_t)((bits<<6)|base64Value); michael@0: ++base64Counter; michael@0: break; michael@0: case 2: michael@0: *target++=(UChar)((bits<<4)|(base64Value>>2)); michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex; michael@0: sourceIndex=nextSourceIndex-1; michael@0: } michael@0: bytes[0]=b; /* keep this byte in case an error occurs */ michael@0: byteIndex=1; michael@0: bits=(uint16_t)(base64Value&3); michael@0: base64Counter=3; michael@0: break; michael@0: case 5: michael@0: *target++=(UChar)((bits<<2)|(base64Value>>4)); michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex; michael@0: sourceIndex=nextSourceIndex-1; michael@0: } michael@0: bytes[0]=b; /* keep this byte in case an error occurs */ michael@0: byteIndex=1; michael@0: bits=(uint16_t)(base64Value&15); michael@0: base64Counter=6; michael@0: break; michael@0: case 7: michael@0: *target++=(UChar)((bits<<6)|base64Value); michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex; michael@0: sourceIndex=nextSourceIndex; michael@0: } michael@0: byteIndex=0; michael@0: bits=0; michael@0: base64Counter=0; michael@0: break; michael@0: default: michael@0: /* will never occur */ michael@0: break; michael@0: } michael@0: } else /*base64Value==-2*/ { michael@0: /* minus sign terminates the base64 sequence */ michael@0: inDirectMode=TRUE; michael@0: if(base64Counter==-1) { michael@0: /* +- i.e. a minus immediately following a plus */ michael@0: *target++=PLUS; michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex-1; michael@0: } michael@0: } else { michael@0: /* absorb the minus and leave the Unicode Mode */ michael@0: if(bits!=0) { michael@0: /* bits are illegally left over, a UChar is incomplete */ michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } michael@0: } michael@0: sourceIndex=nextSourceIndex; michael@0: goto directMode; michael@0: } michael@0: } else { michael@0: /* target is full */ michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if(U_SUCCESS(*pErrorCode) && pArgs->flush && source==sourceLimit && bits==0) { michael@0: /* michael@0: * if we are in Unicode mode, then the byteIndex might not be 0, michael@0: * but that is ok if bits==0 michael@0: * -> we set byteIndex=0 at the end of the stream to avoid a truncated error michael@0: * (not true for IMAP-mailbox-name where we must end in direct mode) michael@0: */ michael@0: byteIndex=0; michael@0: } michael@0: michael@0: /* set the converter state back into UConverter */ michael@0: cnv->toUnicodeStatus=((uint32_t)inDirectMode<<24)|((uint32_t)((uint8_t)base64Counter)<<16)|(uint32_t)bits; michael@0: cnv->toULength=byteIndex; michael@0: michael@0: /* write back the updated pointers */ michael@0: pArgs->source=(const char *)source; michael@0: pArgs->target=target; michael@0: pArgs->offsets=offsets; michael@0: return; michael@0: } michael@0: michael@0: static void michael@0: _UTF7FromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs, michael@0: UErrorCode *pErrorCode) { michael@0: UConverter *cnv; michael@0: const UChar *source, *sourceLimit; michael@0: uint8_t *target, *targetLimit; michael@0: int32_t *offsets; michael@0: michael@0: int32_t length, targetCapacity, sourceIndex; michael@0: UChar c; michael@0: michael@0: /* UTF-7 state */ michael@0: const UBool *encodeDirectly; michael@0: uint8_t bits; michael@0: int8_t base64Counter; michael@0: UBool inDirectMode; michael@0: michael@0: /* set up the local pointers */ michael@0: cnv=pArgs->converter; michael@0: michael@0: /* set up the local pointers */ michael@0: source=pArgs->source; michael@0: sourceLimit=pArgs->sourceLimit; michael@0: target=(uint8_t *)pArgs->target; michael@0: targetLimit=(uint8_t *)pArgs->targetLimit; michael@0: offsets=pArgs->offsets; michael@0: michael@0: /* get the state machine state */ michael@0: { michael@0: uint32_t status=cnv->fromUnicodeStatus; michael@0: encodeDirectly= status<0x10000000 ? encodeDirectlyMaximum : encodeDirectlyRestricted; michael@0: inDirectMode=(UBool)((status>>24)&1); michael@0: base64Counter=(int8_t)(status>>16); michael@0: bits=(uint8_t)status; michael@0: U_ASSERT(bits<=sizeof(toBase64)/sizeof(toBase64[0])); michael@0: } michael@0: michael@0: /* UTF-7 always encodes UTF-16 code units, therefore we need only a simple sourceIndex */ michael@0: sourceIndex=0; michael@0: michael@0: if(inDirectMode) { michael@0: directMode: michael@0: length=(int32_t)(sourceLimit-source); michael@0: targetCapacity=(int32_t)(targetLimit-target); michael@0: if(length>targetCapacity) { michael@0: length=targetCapacity; michael@0: } michael@0: while(length>0) { michael@0: c=*source++; michael@0: /* currently always encode CR LF SP TAB directly */ michael@0: if(c<=127 && encodeDirectly[c]) { michael@0: /* encode directly */ michael@0: *target++=(uint8_t)c; michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: } else if(c==PLUS) { michael@0: /* output +- for + */ michael@0: *target++=PLUS; michael@0: if(targetcharErrorBuffer[0]=MINUS; michael@0: cnv->charErrorBufferLength=1; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: break; michael@0: } michael@0: } else { michael@0: /* un-read this character and switch to Unicode Mode */ michael@0: --source; michael@0: *target++=PLUS; michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex; michael@0: } michael@0: inDirectMode=FALSE; michael@0: base64Counter=0; michael@0: goto unicodeMode; michael@0: } michael@0: --length; michael@0: } michael@0: if(source=targetLimit) { michael@0: /* target is full */ michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } else { michael@0: unicodeMode: michael@0: while(sourcecharErrorBuffer[0]=MINUS; michael@0: cnv->charErrorBufferLength=1; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: break; michael@0: } michael@0: } michael@0: goto directMode; michael@0: } else { michael@0: /* michael@0: * base64 this character: michael@0: * Output 2 or 3 base64 bytes for the remaining bits of the previous character michael@0: * and the bits of this character, each implicitly in UTF-16BE. michael@0: * michael@0: * Here, bits is an 8-bit variable because only 6 bits need to be kept from one michael@0: * character to the next. The actual 2 or 4 bits are shifted to the left edge michael@0: * of the 6-bits field 5..0 to make the termination of the base64 sequence easier. michael@0: */ michael@0: switch(base64Counter) { michael@0: case 0: michael@0: *target++=toBase64[c>>10]; michael@0: if(target>4)&0x3f]; michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex; michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: } else { michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: cnv->charErrorBuffer[0]=toBase64[(c>>4)&0x3f]; michael@0: cnv->charErrorBufferLength=1; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: bits=(uint8_t)((c&15)<<2); michael@0: base64Counter=1; michael@0: break; michael@0: case 1: michael@0: *target++=toBase64[bits|(c>>14)]; michael@0: if(target>8)&0x3f]; michael@0: if(target>2)&0x3f]; michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex; michael@0: *offsets++=sourceIndex; michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: } else { michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex; michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: cnv->charErrorBuffer[0]=toBase64[(c>>2)&0x3f]; michael@0: cnv->charErrorBufferLength=1; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } else { michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: cnv->charErrorBuffer[0]=toBase64[(c>>8)&0x3f]; michael@0: cnv->charErrorBuffer[1]=toBase64[(c>>2)&0x3f]; michael@0: cnv->charErrorBufferLength=2; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: bits=(uint8_t)((c&3)<<4); michael@0: base64Counter=2; michael@0: break; michael@0: case 2: michael@0: *target++=toBase64[bits|(c>>12)]; michael@0: if(target>6)&0x3f]; michael@0: if(targetcharErrorBuffer[0]=toBase64[c&0x3f]; michael@0: cnv->charErrorBufferLength=1; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } else { michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: cnv->charErrorBuffer[0]=toBase64[(c>>6)&0x3f]; michael@0: cnv->charErrorBuffer[1]=toBase64[c&0x3f]; michael@0: cnv->charErrorBufferLength=2; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: bits=0; michael@0: base64Counter=0; michael@0: break; michael@0: default: michael@0: /* will never occur */ michael@0: break; michael@0: } michael@0: } michael@0: } else { michael@0: /* target is full */ michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if(pArgs->flush && source>=sourceLimit) { michael@0: /* flush remaining bits to the target */ michael@0: if(!inDirectMode) { michael@0: if (base64Counter!=0) { michael@0: if(targetcharErrorBuffer[cnv->charErrorBufferLength++]=toBase64[bits]; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } michael@0: /* Add final MINUS to terminate unicodeMode */ michael@0: if(targetcharErrorBuffer[cnv->charErrorBufferLength++]=MINUS; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } michael@0: /* reset the state for the next conversion */ michael@0: cnv->fromUnicodeStatus=(cnv->fromUnicodeStatus&0xf0000000)|0x1000000; /* keep version, inDirectMode=TRUE */ michael@0: } else { michael@0: /* set the converter state back into UConverter */ michael@0: cnv->fromUnicodeStatus= michael@0: (cnv->fromUnicodeStatus&0xf0000000)| /* keep version*/ michael@0: ((uint32_t)inDirectMode<<24)|((uint32_t)base64Counter<<16)|(uint32_t)bits; michael@0: } michael@0: michael@0: /* write back the updated pointers */ michael@0: pArgs->source=source; michael@0: pArgs->target=(char *)target; michael@0: pArgs->offsets=offsets; michael@0: return; michael@0: } michael@0: michael@0: static const char * michael@0: _UTF7GetName(const UConverter *cnv) { michael@0: switch(cnv->fromUnicodeStatus>>28) { michael@0: case 1: michael@0: return "UTF-7,version=1"; michael@0: default: michael@0: return "UTF-7"; michael@0: } michael@0: } michael@0: michael@0: static const UConverterImpl _UTF7Impl={ michael@0: UCNV_UTF7, michael@0: michael@0: NULL, michael@0: NULL, michael@0: michael@0: _UTF7Open, michael@0: NULL, michael@0: _UTF7Reset, michael@0: michael@0: _UTF7ToUnicodeWithOffsets, michael@0: _UTF7ToUnicodeWithOffsets, michael@0: _UTF7FromUnicodeWithOffsets, michael@0: _UTF7FromUnicodeWithOffsets, michael@0: NULL, michael@0: michael@0: NULL, michael@0: _UTF7GetName, michael@0: NULL, /* we don't need writeSub() because we never call a callback at fromUnicode() */ michael@0: NULL, michael@0: ucnv_getCompleteUnicodeSet michael@0: }; michael@0: michael@0: static const UConverterStaticData _UTF7StaticData={ michael@0: sizeof(UConverterStaticData), michael@0: "UTF-7", michael@0: 0, /* TODO CCSID for UTF-7 */ michael@0: UCNV_IBM, UCNV_UTF7, michael@0: 1, 4, michael@0: { 0x3f, 0, 0, 0 }, 1, /* the subchar is not used */ michael@0: 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: const UConverterSharedData _UTF7Data={ michael@0: sizeof(UConverterSharedData), ~((uint32_t)0), michael@0: NULL, NULL, &_UTF7StaticData, FALSE, &_UTF7Impl, michael@0: 0 michael@0: }; michael@0: michael@0: /* IMAP mailbox name encoding ----------------------------------------------- */ michael@0: michael@0: /* michael@0: * RFC 2060: INTERNET MESSAGE ACCESS PROTOCOL - VERSION 4rev1 michael@0: * http://www.ietf.org/rfc/rfc2060.txt michael@0: * michael@0: * 5.1.3. Mailbox International Naming Convention michael@0: * michael@0: * By convention, international mailbox names are specified using a michael@0: * modified version of the UTF-7 encoding described in [UTF-7]. The michael@0: * purpose of these modifications is to correct the following problems michael@0: * with UTF-7: michael@0: * michael@0: * 1) UTF-7 uses the "+" character for shifting; this conflicts with michael@0: * the common use of "+" in mailbox names, in particular USENET michael@0: * newsgroup names. michael@0: * michael@0: * 2) UTF-7's encoding is BASE64 which uses the "/" character; this michael@0: * conflicts with the use of "/" as a popular hierarchy delimiter. michael@0: * michael@0: * 3) UTF-7 prohibits the unencoded usage of "\"; this conflicts with michael@0: * the use of "\" as a popular hierarchy delimiter. michael@0: * michael@0: * 4) UTF-7 prohibits the unencoded usage of "~"; this conflicts with michael@0: * the use of "~" in some servers as a home directory indicator. michael@0: * michael@0: * 5) UTF-7 permits multiple alternate forms to represent the same michael@0: * string; in particular, printable US-ASCII chararacters can be michael@0: * represented in encoded form. michael@0: * michael@0: * In modified UTF-7, printable US-ASCII characters except for "&" michael@0: * represent themselves; that is, characters with octet values 0x20-0x25 michael@0: * and 0x27-0x7e. The character "&" (0x26) is represented by the two- michael@0: * octet sequence "&-". michael@0: * michael@0: * All other characters (octet values 0x00-0x1f, 0x7f-0xff, and all michael@0: * Unicode 16-bit octets) are represented in modified BASE64, with a michael@0: * further modification from [UTF-7] that "," is used instead of "/". michael@0: * Modified BASE64 MUST NOT be used to represent any printing US-ASCII michael@0: * character which can represent itself. michael@0: * michael@0: * "&" is used to shift to modified BASE64 and "-" to shift back to US- michael@0: * ASCII. All names start in US-ASCII, and MUST end in US-ASCII (that michael@0: * is, a name that ends with a Unicode 16-bit octet MUST end with a "- michael@0: * "). michael@0: * michael@0: * For example, here is a mailbox name which mixes English, Japanese, michael@0: * and Chinese text: ~peter/mail/&ZeVnLIqe-/&U,BTFw- michael@0: */ michael@0: michael@0: /* michael@0: * Tests for US-ASCII characters belonging to character classes michael@0: * defined in UTF-7. michael@0: * michael@0: * Set D (directly encoded characters) consists of the following michael@0: * characters: the upper and lower case letters A through Z michael@0: * and a through z, the 10 digits 0-9, and the following nine special michael@0: * characters (note that "+" and "=" are omitted): michael@0: * '(),-./:? michael@0: * michael@0: * Set O (optional direct characters) consists of the following michael@0: * characters (note that "\" and "~" are omitted): michael@0: * !"#$%&*;<=>@[]^_`{|} michael@0: * michael@0: * According to the rules in RFC 2152, the byte values for the following michael@0: * US-ASCII characters are not used in UTF-7 and are therefore illegal: michael@0: * - all C0 control codes except for CR LF TAB michael@0: * - BACKSLASH michael@0: * - TILDE michael@0: * - DEL michael@0: * - all codes beyond US-ASCII, i.e. all >127 michael@0: */ michael@0: michael@0: /* uses '&' not '+' to start a base64 sequence */ michael@0: #define AMPERSAND 0x26 michael@0: #define COMMA 0x2c michael@0: #define SLASH 0x2f michael@0: michael@0: /* legal byte values: all US-ASCII graphic characters 0x20..0x7e */ michael@0: #define isLegalIMAP(c) (0x20<=(c) && (c)<=0x7e) michael@0: michael@0: /* direct-encode all of printable ASCII 0x20..0x7e except '&' 0x26 */ michael@0: #define inSetDIMAP(c) (isLegalIMAP(c) && c!=AMPERSAND) michael@0: michael@0: #define TO_BASE64_IMAP(n) ((n)<63 ? toBase64[n] : COMMA) michael@0: #define FROM_BASE64_IMAP(c) ((c)==COMMA ? 63 : (c)==SLASH ? -1 : fromBase64[c]) michael@0: michael@0: /* michael@0: * converter status values: michael@0: * michael@0: * toUnicodeStatus: michael@0: * 24 inDirectMode (boolean) michael@0: * 23..16 base64Counter (-1..7) michael@0: * 15..0 bits (up to 14 bits incoming base64) michael@0: * michael@0: * fromUnicodeStatus: michael@0: * 24 inDirectMode (boolean) michael@0: * 23..16 base64Counter (0..2) michael@0: * 7..0 bits (6 bits outgoing base64) michael@0: * michael@0: * ignore bits 31..25 michael@0: */ michael@0: michael@0: static void michael@0: _IMAPToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs, michael@0: UErrorCode *pErrorCode) { michael@0: UConverter *cnv; michael@0: const uint8_t *source, *sourceLimit; michael@0: UChar *target; michael@0: const UChar *targetLimit; michael@0: int32_t *offsets; michael@0: michael@0: uint8_t *bytes; michael@0: uint8_t byteIndex; michael@0: michael@0: int32_t length, targetCapacity; michael@0: michael@0: /* UTF-7 state */ michael@0: uint16_t bits; michael@0: int8_t base64Counter; michael@0: UBool inDirectMode; michael@0: michael@0: int8_t base64Value; michael@0: michael@0: int32_t sourceIndex, nextSourceIndex; michael@0: michael@0: UChar c; michael@0: uint8_t b; michael@0: michael@0: /* set up the local pointers */ michael@0: cnv=pArgs->converter; michael@0: michael@0: source=(const uint8_t *)pArgs->source; michael@0: sourceLimit=(const uint8_t *)pArgs->sourceLimit; michael@0: target=pArgs->target; michael@0: targetLimit=pArgs->targetLimit; michael@0: offsets=pArgs->offsets; michael@0: /* get the state machine state */ michael@0: { michael@0: uint32_t status=cnv->toUnicodeStatus; michael@0: inDirectMode=(UBool)((status>>24)&1); michael@0: base64Counter=(int8_t)(status>>16); michael@0: bits=(uint16_t)status; michael@0: } michael@0: bytes=cnv->toUBytes; michael@0: byteIndex=cnv->toULength; michael@0: michael@0: /* sourceIndex=-1 if the current character began in the previous buffer */ michael@0: sourceIndex=byteIndex==0 ? 0 : -1; michael@0: nextSourceIndex=0; michael@0: michael@0: if(inDirectMode) { michael@0: directMode: michael@0: /* michael@0: * In Direct Mode, US-ASCII characters are encoded directly, i.e., michael@0: * with their US-ASCII byte values. michael@0: * An ampersand starts Unicode (or "escape") Mode. michael@0: * michael@0: * In Direct Mode, only the sourceIndex is used. michael@0: */ michael@0: byteIndex=0; michael@0: length=(int32_t)(sourceLimit-source); michael@0: targetCapacity=(int32_t)(targetLimit-target); michael@0: if(length>targetCapacity) { michael@0: length=targetCapacity; michael@0: } michael@0: while(length>0) { michael@0: b=*source++; michael@0: if(!isLegalIMAP(b)) { michael@0: /* illegal */ michael@0: bytes[0]=b; michael@0: byteIndex=1; michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } else if(b!=AMPERSAND) { michael@0: /* write directly encoded character */ michael@0: *target++=b; michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: } else /* AMPERSAND */ { michael@0: /* switch to Unicode mode */ michael@0: nextSourceIndex=++sourceIndex; michael@0: inDirectMode=FALSE; michael@0: byteIndex=0; michael@0: bits=0; michael@0: base64Counter=-1; michael@0: goto unicodeMode; michael@0: } michael@0: --length; michael@0: } michael@0: if(source=targetLimit) { michael@0: /* target is full */ michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } else { michael@0: unicodeMode: michael@0: /* michael@0: * In Unicode (or "escape") Mode, UTF-16BE is base64-encoded. michael@0: * The base64 sequence ends with any character that is not in the base64 alphabet. michael@0: * A terminating minus sign is consumed. michael@0: * US-ASCII must not be base64-ed. michael@0: * michael@0: * In Unicode Mode, the sourceIndex has the index to the start of the current michael@0: * base64 bytes, while nextSourceIndex is precisely parallel to source, michael@0: * keeping the index to the following byte. michael@0: * Note that in 2 out of 3 cases, UChars overlap within a base64 byte. michael@0: */ michael@0: while(source0x7e) { michael@0: /* illegal - test other illegal US-ASCII values by base64Value==-3 */ michael@0: inDirectMode=TRUE; michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } else if((base64Value=FROM_BASE64_IMAP(b))>=0) { michael@0: /* collect base64 bytes into UChars */ michael@0: switch(base64Counter) { michael@0: case -1: /* -1 is immediately after the & */ michael@0: case 0: michael@0: bits=base64Value; michael@0: base64Counter=1; michael@0: break; michael@0: case 1: michael@0: case 3: michael@0: case 4: michael@0: case 6: michael@0: bits=(uint16_t)((bits<<6)|base64Value); michael@0: ++base64Counter; michael@0: break; michael@0: case 2: michael@0: c=(UChar)((bits<<4)|(base64Value>>2)); michael@0: if(isLegalIMAP(c)) { michael@0: /* illegal */ michael@0: inDirectMode=TRUE; michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: goto endloop; michael@0: } michael@0: *target++=c; michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex; michael@0: sourceIndex=nextSourceIndex-1; michael@0: } michael@0: bytes[0]=b; /* keep this byte in case an error occurs */ michael@0: byteIndex=1; michael@0: bits=(uint16_t)(base64Value&3); michael@0: base64Counter=3; michael@0: break; michael@0: case 5: michael@0: c=(UChar)((bits<<2)|(base64Value>>4)); michael@0: if(isLegalIMAP(c)) { michael@0: /* illegal */ michael@0: inDirectMode=TRUE; michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: goto endloop; michael@0: } michael@0: *target++=c; michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex; michael@0: sourceIndex=nextSourceIndex-1; michael@0: } michael@0: bytes[0]=b; /* keep this byte in case an error occurs */ michael@0: byteIndex=1; michael@0: bits=(uint16_t)(base64Value&15); michael@0: base64Counter=6; michael@0: break; michael@0: case 7: michael@0: c=(UChar)((bits<<6)|base64Value); michael@0: if(isLegalIMAP(c)) { michael@0: /* illegal */ michael@0: inDirectMode=TRUE; michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: goto endloop; michael@0: } michael@0: *target++=c; michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex; michael@0: sourceIndex=nextSourceIndex; michael@0: } michael@0: byteIndex=0; michael@0: bits=0; michael@0: base64Counter=0; michael@0: break; michael@0: default: michael@0: /* will never occur */ michael@0: break; michael@0: } michael@0: } else if(base64Value==-2) { michael@0: /* minus sign terminates the base64 sequence */ michael@0: inDirectMode=TRUE; michael@0: if(base64Counter==-1) { michael@0: /* &- i.e. a minus immediately following an ampersand */ michael@0: *target++=AMPERSAND; michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex-1; michael@0: } michael@0: } else { michael@0: /* absorb the minus and leave the Unicode Mode */ michael@0: if(bits!=0 || (base64Counter!=0 && base64Counter!=3 && base64Counter!=6)) { michael@0: /* bits are illegally left over, a UChar is incomplete */ michael@0: /* base64Counter other than 0, 3, 6 means non-minimal zero-padding, also illegal */ michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } michael@0: } michael@0: sourceIndex=nextSourceIndex; michael@0: goto directMode; michael@0: } else { michael@0: if(base64Counter==-1) { michael@0: /* illegal: & immediately followed by something other than base64 or minus sign */ michael@0: /* include the ampersand in the reported sequence */ michael@0: --sourceIndex; michael@0: bytes[0]=AMPERSAND; michael@0: bytes[1]=b; michael@0: byteIndex=2; michael@0: } michael@0: /* base64Value==-1 for characters that are illegal only in Unicode mode */ michael@0: /* base64Value==-3 for illegal characters */ michael@0: /* illegal */ michael@0: inDirectMode=TRUE; michael@0: *pErrorCode=U_ILLEGAL_CHAR_FOUND; michael@0: break; michael@0: } michael@0: } else { michael@0: /* target is full */ michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: endloop: michael@0: michael@0: /* michael@0: * the end of the input stream and detection of truncated input michael@0: * are handled by the framework, but here we must check if we are in Unicode michael@0: * mode and byteIndex==0 because we must end in direct mode michael@0: * michael@0: * conditions: michael@0: * successful michael@0: * in Unicode mode and byteIndex==0 michael@0: * end of input and no truncated input michael@0: */ michael@0: if( U_SUCCESS(*pErrorCode) && michael@0: !inDirectMode && byteIndex==0 && michael@0: pArgs->flush && source>=sourceLimit michael@0: ) { michael@0: if(base64Counter==-1) { michael@0: /* & at the very end of the input */ michael@0: /* make the ampersand the reported sequence */ michael@0: bytes[0]=AMPERSAND; michael@0: byteIndex=1; michael@0: } michael@0: /* else if(base64Counter!=-1) byteIndex remains 0 because there is no particular byte sequence */ michael@0: michael@0: inDirectMode=TRUE; /* avoid looping */ michael@0: *pErrorCode=U_TRUNCATED_CHAR_FOUND; michael@0: } michael@0: michael@0: /* set the converter state back into UConverter */ michael@0: cnv->toUnicodeStatus=((uint32_t)inDirectMode<<24)|((uint32_t)((uint8_t)base64Counter)<<16)|(uint32_t)bits; michael@0: cnv->toULength=byteIndex; michael@0: michael@0: /* write back the updated pointers */ michael@0: pArgs->source=(const char *)source; michael@0: pArgs->target=target; michael@0: pArgs->offsets=offsets; michael@0: return; michael@0: } michael@0: michael@0: static void michael@0: _IMAPFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs, michael@0: UErrorCode *pErrorCode) { michael@0: UConverter *cnv; michael@0: const UChar *source, *sourceLimit; michael@0: uint8_t *target, *targetLimit; michael@0: int32_t *offsets; michael@0: michael@0: int32_t length, targetCapacity, sourceIndex; michael@0: UChar c; michael@0: uint8_t b; michael@0: michael@0: /* UTF-7 state */ michael@0: uint8_t bits; michael@0: int8_t base64Counter; michael@0: UBool inDirectMode; michael@0: michael@0: /* set up the local pointers */ michael@0: cnv=pArgs->converter; michael@0: michael@0: /* set up the local pointers */ michael@0: source=pArgs->source; michael@0: sourceLimit=pArgs->sourceLimit; michael@0: target=(uint8_t *)pArgs->target; michael@0: targetLimit=(uint8_t *)pArgs->targetLimit; michael@0: offsets=pArgs->offsets; michael@0: michael@0: /* get the state machine state */ michael@0: { michael@0: uint32_t status=cnv->fromUnicodeStatus; michael@0: inDirectMode=(UBool)((status>>24)&1); michael@0: base64Counter=(int8_t)(status>>16); michael@0: bits=(uint8_t)status; michael@0: } michael@0: michael@0: /* UTF-7 always encodes UTF-16 code units, therefore we need only a simple sourceIndex */ michael@0: sourceIndex=0; michael@0: michael@0: if(inDirectMode) { michael@0: directMode: michael@0: length=(int32_t)(sourceLimit-source); michael@0: targetCapacity=(int32_t)(targetLimit-target); michael@0: if(length>targetCapacity) { michael@0: length=targetCapacity; michael@0: } michael@0: while(length>0) { michael@0: c=*source++; michael@0: /* encode 0x20..0x7e except '&' directly */ michael@0: if(inSetDIMAP(c)) { michael@0: /* encode directly */ michael@0: *target++=(uint8_t)c; michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: } else if(c==AMPERSAND) { michael@0: /* output &- for & */ michael@0: *target++=AMPERSAND; michael@0: if(targetcharErrorBuffer[0]=MINUS; michael@0: cnv->charErrorBufferLength=1; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: break; michael@0: } michael@0: } else { michael@0: /* un-read this character and switch to Unicode Mode */ michael@0: --source; michael@0: *target++=AMPERSAND; michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex; michael@0: } michael@0: inDirectMode=FALSE; michael@0: base64Counter=0; michael@0: goto unicodeMode; michael@0: } michael@0: --length; michael@0: } michael@0: if(source=targetLimit) { michael@0: /* target is full */ michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } else { michael@0: unicodeMode: michael@0: while(sourcecharErrorBuffer[0]=MINUS; michael@0: cnv->charErrorBufferLength=1; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: break; michael@0: } michael@0: goto directMode; michael@0: } else { michael@0: /* michael@0: * base64 this character: michael@0: * Output 2 or 3 base64 bytes for the remaining bits of the previous character michael@0: * and the bits of this character, each implicitly in UTF-16BE. michael@0: * michael@0: * Here, bits is an 8-bit variable because only 6 bits need to be kept from one michael@0: * character to the next. The actual 2 or 4 bits are shifted to the left edge michael@0: * of the 6-bits field 5..0 to make the termination of the base64 sequence easier. michael@0: */ michael@0: switch(base64Counter) { michael@0: case 0: michael@0: b=(uint8_t)(c>>10); michael@0: *target++=TO_BASE64_IMAP(b); michael@0: if(target>4)&0x3f); michael@0: *target++=TO_BASE64_IMAP(b); michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex; michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: } else { michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: b=(uint8_t)((c>>4)&0x3f); michael@0: cnv->charErrorBuffer[0]=TO_BASE64_IMAP(b); michael@0: cnv->charErrorBufferLength=1; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: bits=(uint8_t)((c&15)<<2); michael@0: base64Counter=1; michael@0: break; michael@0: case 1: michael@0: b=(uint8_t)(bits|(c>>14)); michael@0: *target++=TO_BASE64_IMAP(b); michael@0: if(target>8)&0x3f); michael@0: *target++=TO_BASE64_IMAP(b); michael@0: if(target>2)&0x3f); michael@0: *target++=TO_BASE64_IMAP(b); michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex; michael@0: *offsets++=sourceIndex; michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: } else { michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex; michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: b=(uint8_t)((c>>2)&0x3f); michael@0: cnv->charErrorBuffer[0]=TO_BASE64_IMAP(b); michael@0: cnv->charErrorBufferLength=1; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } else { michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: b=(uint8_t)((c>>8)&0x3f); michael@0: cnv->charErrorBuffer[0]=TO_BASE64_IMAP(b); michael@0: b=(uint8_t)((c>>2)&0x3f); michael@0: cnv->charErrorBuffer[1]=TO_BASE64_IMAP(b); michael@0: cnv->charErrorBufferLength=2; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: bits=(uint8_t)((c&3)<<4); michael@0: base64Counter=2; michael@0: break; michael@0: case 2: michael@0: b=(uint8_t)(bits|(c>>12)); michael@0: *target++=TO_BASE64_IMAP(b); michael@0: if(target>6)&0x3f); michael@0: *target++=TO_BASE64_IMAP(b); michael@0: if(targetcharErrorBuffer[0]=TO_BASE64_IMAP(b); michael@0: cnv->charErrorBufferLength=1; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } else { michael@0: if(offsets!=NULL) { michael@0: *offsets++=sourceIndex++; michael@0: } michael@0: b=(uint8_t)((c>>6)&0x3f); michael@0: cnv->charErrorBuffer[0]=TO_BASE64_IMAP(b); michael@0: b=(uint8_t)(c&0x3f); michael@0: cnv->charErrorBuffer[1]=TO_BASE64_IMAP(b); michael@0: cnv->charErrorBufferLength=2; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: bits=0; michael@0: base64Counter=0; michael@0: break; michael@0: default: michael@0: /* will never occur */ michael@0: break; michael@0: } michael@0: } michael@0: } else { michael@0: /* target is full */ michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if(pArgs->flush && source>=sourceLimit) { michael@0: /* flush remaining bits to the target */ michael@0: if(!inDirectMode) { michael@0: if(base64Counter!=0) { michael@0: if(targetcharErrorBuffer[cnv->charErrorBufferLength++]=TO_BASE64_IMAP(bits); michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } michael@0: /* need to terminate with a minus */ michael@0: if(targetcharErrorBuffer[cnv->charErrorBufferLength++]=MINUS; michael@0: *pErrorCode=U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } michael@0: /* reset the state for the next conversion */ michael@0: cnv->fromUnicodeStatus=(cnv->fromUnicodeStatus&0xf0000000)|0x1000000; /* keep version, inDirectMode=TRUE */ michael@0: } else { michael@0: /* set the converter state back into UConverter */ michael@0: cnv->fromUnicodeStatus= michael@0: (cnv->fromUnicodeStatus&0xf0000000)| /* keep version*/ michael@0: ((uint32_t)inDirectMode<<24)|((uint32_t)base64Counter<<16)|(uint32_t)bits; michael@0: } michael@0: michael@0: /* write back the updated pointers */ michael@0: pArgs->source=source; michael@0: pArgs->target=(char *)target; michael@0: pArgs->offsets=offsets; michael@0: return; michael@0: } michael@0: michael@0: static const UConverterImpl _IMAPImpl={ michael@0: UCNV_IMAP_MAILBOX, michael@0: michael@0: NULL, michael@0: NULL, michael@0: michael@0: _UTF7Open, michael@0: NULL, michael@0: _UTF7Reset, michael@0: michael@0: _IMAPToUnicodeWithOffsets, michael@0: _IMAPToUnicodeWithOffsets, michael@0: _IMAPFromUnicodeWithOffsets, michael@0: _IMAPFromUnicodeWithOffsets, michael@0: NULL, michael@0: michael@0: NULL, michael@0: NULL, michael@0: NULL, /* we don't need writeSub() because we never call a callback at fromUnicode() */ michael@0: NULL, michael@0: ucnv_getCompleteUnicodeSet michael@0: }; michael@0: michael@0: static const UConverterStaticData _IMAPStaticData={ michael@0: sizeof(UConverterStaticData), michael@0: "IMAP-mailbox-name", michael@0: 0, /* TODO CCSID for IMAP-mailbox-name */ michael@0: UCNV_IBM, UCNV_IMAP_MAILBOX, michael@0: 1, 4, michael@0: { 0x3f, 0, 0, 0 }, 1, /* the subchar is not used */ michael@0: 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: const UConverterSharedData _IMAPData={ michael@0: sizeof(UConverterSharedData), ~((uint32_t)0), michael@0: NULL, NULL, &_IMAPStaticData, FALSE, &_IMAPImpl, michael@0: 0 michael@0: }; michael@0: michael@0: #endif