10596faeddefbf198de137d5e893708495ab1584cFredrik Roubert// © 2016 and later: Unicode, Inc. and others. 264339d36f8bd4db5025fe2988eda22b491a9219cFredrik Roubert// License & terms of use: http://www.unicode.org/copyright.html 3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru********************************************************************** 58de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert* Copyright (C) 2002-2016, International Business Machines 6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* Corporation and others. All Rights Reserved. 7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru********************************************************************** 8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* file name: ucnv_u7.c 90596faeddefbf198de137d5e893708495ab1584cFredrik Roubert* encoding: UTF-8 10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* tab size: 8 (not used) 11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* indentation:4 12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* 13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* created on: 2002jul01 14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* created by: Markus W. Scherer 15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* 16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru* UTF-7 converter implementation. Used to be in ucnv_utf.c. 17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/ 18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/utypes.h" 20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 211b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert#if !UCONFIG_NO_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION 22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 238de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert#include "cmemory.h" 24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/ucnv.h" 25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "ucnv_bld.h" 26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "ucnv_cnv.h" 2783a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius#include "uassert.h" 28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* UTF-7 -------------------------------------------------------------------- */ 30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * UTF-7 is a stateful encoding of Unicode. 33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * It is defined in RFC 2152. (http://www.ietf.org/rfc/rfc2152.txt) 34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * It was intended for use in Internet email systems, using in its bytewise 35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * encoding only a subset of 7-bit US-ASCII. 36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * UTF-7 is deprecated in favor of UTF-8/16/32 and SCSU, but still 37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * occasionally used. 38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * For converting Unicode to UTF-7, the RFC allows to encode some US-ASCII 40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * characters directly or in base64. Especially, the characters in set O 41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * as defined in the RFC (see below) may be encoded directly but are not 42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * allowed in, e.g., email headers. 43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * By default, the ICU UTF-7 converter encodes set O directly. 44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * By choosing the option "version=1", set O will be escaped instead. 45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * For example: 46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * utf7Converter=ucnv_open("UTF-7,version=1"); 47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * For details about email headers see RFC 2047. 49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Tests for US-ASCII characters belonging to character classes 53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * defined in UTF-7. 54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Set D (directly encoded characters) consists of the following 56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * characters: the upper and lower case letters A through Z 57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * and a through z, the 10 digits 0-9, and the following nine special 58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * characters (note that "+" and "=" are omitted): 59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * '(),-./:? 60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Set O (optional direct characters) consists of the following 62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * characters (note that "\" and "~" are omitted): 63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * !"#$%&*;<=>@[]^_`{|} 64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * According to the rules in RFC 2152, the byte values for the following 66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * US-ASCII characters are not used in UTF-7 and are therefore illegal: 67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * - all C0 control codes except for CR LF TAB 68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * - BACKSLASH 69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * - TILDE 70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * - DEL 71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * - all codes beyond US-ASCII, i.e. all >127 72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define inSetD(c) \ 74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ((uint8_t)((c)-97)<26 || (uint8_t)((c)-65)<26 || /* letters */ \ 75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (uint8_t)((c)-48)<10 || /* digits */ \ 76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (uint8_t)((c)-39)<3 || /* '() */ \ 77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (uint8_t)((c)-44)<4 || /* ,-./ */ \ 78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (c)==58 || (c)==63 /* :? */ \ 79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ) 80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define inSetO(c) \ 82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ((uint8_t)((c)-33)<6 || /* !"#$%& */ \ 83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (uint8_t)((c)-59)<4 || /* ;<=> */ \ 84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (uint8_t)((c)-93)<4 || /* ]^_` */ \ 85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (uint8_t)((c)-123)<3 || /* {|} */ \ 86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (c)==42 || (c)==64 || (c)==91 /* *@[ */ \ 87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ) 88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define isCRLFTAB(c) ((c)==13 || (c)==10 || (c)==9) 90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define isCRLFSPTAB(c) ((c)==32 || (c)==13 || (c)==10 || (c)==9) 91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define PLUS 43 93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define MINUS 45 94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define BACKSLASH 92 95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define TILDE 126 96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* legal byte values: all US-ASCII graphic characters from space to before tilde, and CR LF TAB */ 98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define isLegalUTF7(c) (((uint8_t)((c)-32)<94 && (c)!=BACKSLASH) || isCRLFTAB(c)) 99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* encode directly sets D and O and CR LF SP TAB */ 101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UBool encodeDirectlyMaximum[128]={ 102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ 103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}; 115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* encode directly set D and CR LF SP TAB but not set O */ 117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UBool encodeDirectlyRestricted[128]={ 118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ 119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}; 131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const uint8_t 133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QuerutoBase64[64]={ 134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* A-Z */ 135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 136ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 137ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* a-z */ 138ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* 0-9 */ 141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* +/ */ 143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 43, 47 144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}; 145ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 146ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const int8_t 147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QuerufromBase64[128]={ 148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* C0 controls, -1 for legal ones (CR LF TAB), -3 for illegal ones */ 149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru -3, -3, -3, -3, -3, -3, -3, -3, -3, -1, -1, -3, -3, -1, -3, -3, 150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, 151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* general punctuation with + and / and a special value (-2) for - */ 153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -2, -1, 63, 154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* digits */ 155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, 156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* A-Z */ 158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -3, -1, -1, -1, 160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* a-z */ 162ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 163ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -3, -3 164ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}; 165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 166ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 167ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * converter status values: 168ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 169ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * toUnicodeStatus: 170ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 24 inDirectMode (boolean) 171ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 23..16 base64Counter (-1..7) 172ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 15..0 bits (up to 14 bits incoming base64) 173ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 174ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * fromUnicodeStatus: 175ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 31..28 version (0: set O direct 1: set O escaped) 176ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 24 inDirectMode (boolean) 177ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 23..16 base64Counter (0..2) 178ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 7..0 bits (6 bits outgoing base64) 179ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 180ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 181ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1820596faeddefbf198de137d5e893708495ab1584cFredrik RoubertU_CDECL_BEGIN 1830596faeddefbf198de137d5e893708495ab1584cFredrik Roubertstatic void U_CALLCONV 184ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru_UTF7Reset(UConverter *cnv, UConverterResetChoice choice) { 185ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(choice<=UCNV_RESET_TO_UNICODE) { 186ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* reset toUnicode */ 187ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->toUnicodeStatus=0x1000000; /* inDirectMode=TRUE */ 188ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->toULength=0; 189ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 190ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(choice!=UCNV_RESET_TO_UNICODE) { 191ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* reset fromUnicode */ 192ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->fromUnicodeStatus=(cnv->fromUnicodeStatus&0xf0000000)|0x1000000; /* keep version, inDirectMode=TRUE */ 193ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 194ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 195ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1960596faeddefbf198de137d5e893708495ab1584cFredrik Roubertstatic void U_CALLCONV 197ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru_UTF7Open(UConverter *cnv, 19885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho UConverterLoadArgs *pArgs, 199ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode *pErrorCode) { 2000596faeddefbf198de137d5e893708495ab1584cFredrik Roubert (void)pArgs; 20185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if(UCNV_GET_VERSION(cnv)<=1) { 20285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho /* TODO(markus): Should just use cnv->options rather than copying the version number. */ 20385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho cnv->fromUnicodeStatus=UCNV_GET_VERSION(cnv)<<28; 204ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _UTF7Reset(cnv, UCNV_RESET_BOTH); 205ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 206ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; 207ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 208ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 209ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 2100596faeddefbf198de137d5e893708495ab1584cFredrik Roubertstatic void U_CALLCONV 211ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru_UTF7ToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs, 212ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode *pErrorCode) { 213ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UConverter *cnv; 214ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const uint8_t *source, *sourceLimit; 215ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar *target; 216ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UChar *targetLimit; 217ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t *offsets; 218ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 219ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint8_t *bytes; 220ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint8_t byteIndex; 221ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 222ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t length, targetCapacity; 223ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 224ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* UTF-7 state */ 225ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint16_t bits; 226ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int8_t base64Counter; 227ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool inDirectMode; 228ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 229ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int8_t base64Value; 230ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 231ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t sourceIndex, nextSourceIndex; 232ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 233ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint8_t b; 234ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* set up the local pointers */ 235ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv=pArgs->converter; 236ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 237ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru source=(const uint8_t *)pArgs->source; 238ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceLimit=(const uint8_t *)pArgs->sourceLimit; 239ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru target=pArgs->target; 240ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru targetLimit=pArgs->targetLimit; 241ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru offsets=pArgs->offsets; 242ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* get the state machine state */ 243ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 244ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint32_t status=cnv->toUnicodeStatus; 245ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=(UBool)((status>>24)&1); 246ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=(int8_t)(status>>16); 247ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=(uint16_t)status; 248ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 249ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bytes=cnv->toUBytes; 250ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=cnv->toULength; 251ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 252ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* sourceIndex=-1 if the current character began in the previous buffer */ 253ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceIndex=byteIndex==0 ? 0 : -1; 254ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru nextSourceIndex=0; 255ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 256ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(inDirectMode) { 257ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QuerudirectMode: 258ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* 259ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * In Direct Mode, most US-ASCII characters are encoded directly, i.e., 260ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * with their US-ASCII byte values. 261ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Backslash and Tilde and most control characters are not allowed in UTF-7. 262ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * A plus sign starts Unicode (or "escape") Mode. 263ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 264ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * In Direct Mode, only the sourceIndex is used. 265ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 266ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=0; 267ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru length=(int32_t)(sourceLimit-source); 268ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru targetCapacity=(int32_t)(targetLimit-target); 269ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(length>targetCapacity) { 270ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru length=targetCapacity; 271ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 272ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while(length>0) { 273ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=*source++; 274ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(!isLegalUTF7(b)) { 275ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* illegal */ 276ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bytes[0]=b; 277ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=1; 278ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_ILLEGAL_CHAR_FOUND; 279ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 280ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else if(b!=PLUS) { 281ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* write directly encoded character */ 282ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=b; 283ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 284ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 285ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 286ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else /* PLUS */ { 287ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* switch to Unicode mode */ 288ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru nextSourceIndex=++sourceIndex; 289ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=FALSE; 290ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=0; 291ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=0; 292ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=-1; 293ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru goto unicodeMode; 294ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 295ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru --length; 296ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 297ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(source<sourceLimit && target>=targetLimit) { 298ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* target is full */ 299ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 300ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 301ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 302ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruunicodeMode: 303ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* 304ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * In Unicode (or "escape") Mode, UTF-16BE is base64-encoded. 305ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * The base64 sequence ends with any character that is not in the base64 alphabet. 306ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * A terminating minus sign is consumed. 307ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 308ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * In Unicode Mode, the sourceIndex has the index to the start of the current 309ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * base64 bytes, while nextSourceIndex is precisely parallel to source, 310ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * keeping the index to the following byte. 311ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Note that in 2 out of 3 cases, UChars overlap within a base64 byte. 312ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 313ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while(source<sourceLimit) { 314ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 315ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bytes[byteIndex++]=b=*source++; 316ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ++nextSourceIndex; 317b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho base64Value = -3; /* initialize as illegal */ 318b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho if(b>=126 || (base64Value=fromBase64[b])==-3 || base64Value==-1) { 319b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho /* either 320b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * base64Value==-1 for any legal character except base64 and minus sign, or 321b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * base64Value==-3 for illegal characters: 322b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * 1. In either case, leave Unicode mode. 323b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * 2.1. If we ended with an incomplete UChar or none after the +, then 324b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * generate an error for the preceding erroneous sequence and deal with 325b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * the current (possibly illegal) character next time through. 326b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * 2.2. Else the current char comes after a complete UChar, which was already 327b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * pushed to the output buf, so: 328b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * 2.2.1. If the current char is legal, just save it for processing next time. 329b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * It may be for example, a plus which we need to deal with in direct mode. 330b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * 2.2.2. Else if the current char is illegal, we might as well deal with it here. 331b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho */ 332ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=TRUE; 333b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho if(base64Counter==-1) { 334b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho /* illegal: + immediately followed by something other than base64 or minus sign */ 335b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho /* include the plus sign in the reported sequence, but not the subsequent char */ 336b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho --source; 337b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho bytes[0]=PLUS; 338b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho byteIndex=1; 339b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho *pErrorCode=U_ILLEGAL_CHAR_FOUND; 340b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho break; 341b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho } else if(bits!=0) { 342b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho /* bits are illegally left over, a UChar is incomplete */ 343b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho /* don't include current char (legal or illegal) in error seq */ 344b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho --source; 345b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho --byteIndex; 346b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho *pErrorCode=U_ILLEGAL_CHAR_FOUND; 347b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho break; 348b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho } else { 349b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho /* previous UChar was complete */ 350b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho if(base64Value==-3) { 351b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho /* current character is illegal, deal with it here */ 352b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho *pErrorCode=U_ILLEGAL_CHAR_FOUND; 353b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho break; 354b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho } else { 355b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho /* un-read the current character in case it is a plus sign */ 356b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho --source; 357b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho sourceIndex=nextSourceIndex-1; 358b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho goto directMode; 359b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho } 360b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho } 361b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho } else if(base64Value>=0) { 362ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* collect base64 bytes into UChars */ 363ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru switch(base64Counter) { 364ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case -1: /* -1 is immediately after the + */ 365ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 0: 366ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=base64Value; 367ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=1; 368ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 369ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 1: 370ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 3: 371ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 4: 372ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 6: 373ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=(uint16_t)((bits<<6)|base64Value); 374ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ++base64Counter; 375ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 376ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 2: 377ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=(UChar)((bits<<4)|(base64Value>>2)); 378ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 379ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 380ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceIndex=nextSourceIndex-1; 381ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 382ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bytes[0]=b; /* keep this byte in case an error occurs */ 383ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=1; 384ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=(uint16_t)(base64Value&3); 385ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=3; 386ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 387ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 5: 388ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=(UChar)((bits<<2)|(base64Value>>4)); 389ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 390ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 391ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceIndex=nextSourceIndex-1; 392ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 393ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bytes[0]=b; /* keep this byte in case an error occurs */ 394ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=1; 395ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=(uint16_t)(base64Value&15); 396ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=6; 397ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 398ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 7: 399ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=(UChar)((bits<<6)|base64Value); 400ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 401ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 402ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceIndex=nextSourceIndex; 403ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 404ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=0; 405ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=0; 406ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=0; 407ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 408ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru default: 409ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* will never occur */ 410ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 411ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 412b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho } else /*base64Value==-2*/ { 413ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* minus sign terminates the base64 sequence */ 414ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=TRUE; 415ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(base64Counter==-1) { 416ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* +- i.e. a minus immediately following a plus */ 417ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=PLUS; 418ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 419ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex-1; 420ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 421ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 422ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* absorb the minus and leave the Unicode Mode */ 423ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(bits!=0) { 424ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* bits are illegally left over, a UChar is incomplete */ 425ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_ILLEGAL_CHAR_FOUND; 426ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 427ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 428ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 429ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceIndex=nextSourceIndex; 430ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru goto directMode; 431ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 432ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 433ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* target is full */ 434ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 435ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 436ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 437ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 438ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 439ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 440ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(U_SUCCESS(*pErrorCode) && pArgs->flush && source==sourceLimit && bits==0) { 441ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* 442ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * if we are in Unicode mode, then the byteIndex might not be 0, 443ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * but that is ok if bits==0 444ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * -> we set byteIndex=0 at the end of the stream to avoid a truncated error 445ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * (not true for IMAP-mailbox-name where we must end in direct mode) 446ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 447ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=0; 448ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 449ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 450ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* set the converter state back into UConverter */ 451ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->toUnicodeStatus=((uint32_t)inDirectMode<<24)|((uint32_t)((uint8_t)base64Counter)<<16)|(uint32_t)bits; 452ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->toULength=byteIndex; 453ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 454ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* write back the updated pointers */ 455ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pArgs->source=(const char *)source; 456ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pArgs->target=target; 457ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pArgs->offsets=offsets; 458ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 459ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 460ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 4610596faeddefbf198de137d5e893708495ab1584cFredrik Roubertstatic void U_CALLCONV 462ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru_UTF7FromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs, 463ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode *pErrorCode) { 464ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UConverter *cnv; 465ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UChar *source, *sourceLimit; 466ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint8_t *target, *targetLimit; 467ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t *offsets; 468ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 469ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t length, targetCapacity, sourceIndex; 470ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar c; 471ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 472ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* UTF-7 state */ 473ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UBool *encodeDirectly; 474ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint8_t bits; 475ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int8_t base64Counter; 476ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool inDirectMode; 477ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 478ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* set up the local pointers */ 479ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv=pArgs->converter; 480ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 481ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* set up the local pointers */ 482ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru source=pArgs->source; 483ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceLimit=pArgs->sourceLimit; 484ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru target=(uint8_t *)pArgs->target; 485ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru targetLimit=(uint8_t *)pArgs->targetLimit; 486ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru offsets=pArgs->offsets; 487ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 488ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* get the state machine state */ 489ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 490ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint32_t status=cnv->fromUnicodeStatus; 491ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru encodeDirectly= status<0x10000000 ? encodeDirectlyMaximum : encodeDirectlyRestricted; 492ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=(UBool)((status>>24)&1); 493ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=(int8_t)(status>>16); 494ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=(uint8_t)status; 4958de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert U_ASSERT(bits<=UPRV_LENGTHOF(toBase64)); 496ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 497ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 498ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* UTF-7 always encodes UTF-16 code units, therefore we need only a simple sourceIndex */ 499ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceIndex=0; 500ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 501ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(inDirectMode) { 502ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QuerudirectMode: 503ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru length=(int32_t)(sourceLimit-source); 504ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru targetCapacity=(int32_t)(targetLimit-target); 505ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(length>targetCapacity) { 506ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru length=targetCapacity; 507ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 508ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while(length>0) { 509ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru c=*source++; 510ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* currently always encode CR LF SP TAB directly */ 511ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(c<=127 && encodeDirectly[c]) { 512ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* encode directly */ 513ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=(uint8_t)c; 514ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 515ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 516ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 517ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else if(c==PLUS) { 518ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* output +- for + */ 519ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=PLUS; 520ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 521ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=MINUS; 522ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 523ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 524ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 525ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 526ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* realign length and targetCapacity */ 527ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru goto directMode; 528ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 529ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 530ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 531ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 532ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[0]=MINUS; 533ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBufferLength=1; 534ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 535ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 536ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 537ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 538ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* un-read this character and switch to Unicode Mode */ 539ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru --source; 540ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=PLUS; 541ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 542ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 543ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 544ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=FALSE; 545ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=0; 546ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru goto unicodeMode; 547ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 548ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru --length; 549ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 550ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(source<sourceLimit && target>=targetLimit) { 551ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* target is full */ 552ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 553ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 554ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 555ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruunicodeMode: 556ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while(source<sourceLimit) { 557ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 558ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru c=*source++; 559ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(c<=127 && encodeDirectly[c]) { 560ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* encode directly */ 561ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=TRUE; 562ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 563ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* trick: back out this character to make this easier */ 564ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru --source; 565ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 566ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* terminate the base64 sequence */ 567ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(base64Counter!=0) { 568ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* write remaining bits for the previous character */ 569ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=toBase64[bits]; 570ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 571ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex-1; 572ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 573ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 574ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(fromBase64[c]!=-1) { 575ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* need to terminate with a minus */ 576ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 577ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=MINUS; 578ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 579ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex-1; 580ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 581ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 582ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[0]=MINUS; 583ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBufferLength=1; 584ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 585ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 586ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 587ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 588ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru goto directMode; 589ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 590ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* 591ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * base64 this character: 592ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Output 2 or 3 base64 bytes for the remaining bits of the previous character 593ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * and the bits of this character, each implicitly in UTF-16BE. 594ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 595ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Here, bits is an 8-bit variable because only 6 bits need to be kept from one 596ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * character to the next. The actual 2 or 4 bits are shifted to the left edge 597ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * of the 6-bits field 5..0 to make the termination of the base64 sequence easier. 598ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 599ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru switch(base64Counter) { 600ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 0: 601ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=toBase64[c>>10]; 602ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 603ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=toBase64[(c>>4)&0x3f]; 604ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 605ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 606ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 607ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 608ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 609ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 610ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 611ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 612ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[0]=toBase64[(c>>4)&0x3f]; 613ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBufferLength=1; 614ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 615ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 616ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=(uint8_t)((c&15)<<2); 617ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=1; 618ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 619ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 1: 620ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=toBase64[bits|(c>>14)]; 621ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 622ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=toBase64[(c>>8)&0x3f]; 623ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 624ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=toBase64[(c>>2)&0x3f]; 625ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 626ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 627ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 628ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 629ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 630ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 631ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 632ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 633ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 634ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 635ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[0]=toBase64[(c>>2)&0x3f]; 636ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBufferLength=1; 637ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 638ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 639ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 640ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 641ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 642ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 643ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[0]=toBase64[(c>>8)&0x3f]; 644ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[1]=toBase64[(c>>2)&0x3f]; 645ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBufferLength=2; 646ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 647ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 648ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=(uint8_t)((c&3)<<4); 649ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=2; 650ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 651ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 2: 652ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=toBase64[bits|(c>>12)]; 653ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 654ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=toBase64[(c>>6)&0x3f]; 655ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 656ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=toBase64[c&0x3f]; 657ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 658ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 659ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 660ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 661ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 662ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 663ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 664ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 665ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 666ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 667ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[0]=toBase64[c&0x3f]; 668ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBufferLength=1; 669ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 670ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 671ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 672ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 673ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 674ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 675ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[0]=toBase64[(c>>6)&0x3f]; 676ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[1]=toBase64[c&0x3f]; 677ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBufferLength=2; 678ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 679ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 680ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=0; 681ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=0; 682ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 683ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru default: 684ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* will never occur */ 685ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 686ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 687ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 688ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 689ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* target is full */ 690ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 691ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 692ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 693ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 694ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 695ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 696ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(pArgs->flush && source>=sourceLimit) { 697ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* flush remaining bits to the target */ 698b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho if(!inDirectMode) { 699b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho if (base64Counter!=0) { 700b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho if(target<targetLimit) { 701b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho *target++=toBase64[bits]; 702b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho if(offsets!=NULL) { 703b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho *offsets++=sourceIndex-1; 704b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho } 705b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho } else { 706b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho cnv->charErrorBuffer[cnv->charErrorBufferLength++]=toBase64[bits]; 707b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 708b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho } 709b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho } 710b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho /* Add final MINUS to terminate unicodeMode */ 711ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 712b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho *target++=MINUS; 713ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 714ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex-1; 715ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 716ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 717b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho cnv->charErrorBuffer[cnv->charErrorBufferLength++]=MINUS; 718ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 719ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 720ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 721ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* reset the state for the next conversion */ 722ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->fromUnicodeStatus=(cnv->fromUnicodeStatus&0xf0000000)|0x1000000; /* keep version, inDirectMode=TRUE */ 723ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 724ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* set the converter state back into UConverter */ 725ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->fromUnicodeStatus= 726ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (cnv->fromUnicodeStatus&0xf0000000)| /* keep version*/ 727ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ((uint32_t)inDirectMode<<24)|((uint32_t)base64Counter<<16)|(uint32_t)bits; 728ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 729ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 730ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* write back the updated pointers */ 731ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pArgs->source=source; 732ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pArgs->target=(char *)target; 733ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pArgs->offsets=offsets; 734ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 735ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 736ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 7370596faeddefbf198de137d5e893708495ab1584cFredrik Roubertstatic const char * U_CALLCONV 738ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru_UTF7GetName(const UConverter *cnv) { 739ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru switch(cnv->fromUnicodeStatus>>28) { 740ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 1: 741ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return "UTF-7,version=1"; 742ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru default: 743ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return "UTF-7"; 744ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 745ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 7460596faeddefbf198de137d5e893708495ab1584cFredrik RoubertU_CDECL_END 747ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 748ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UConverterImpl _UTF7Impl={ 749ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UCNV_UTF7, 750ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 751ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru NULL, 752ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru NULL, 753ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 754ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _UTF7Open, 755ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru NULL, 756ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _UTF7Reset, 757ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 758ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _UTF7ToUnicodeWithOffsets, 759ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _UTF7ToUnicodeWithOffsets, 760ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _UTF7FromUnicodeWithOffsets, 761ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _UTF7FromUnicodeWithOffsets, 762ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru NULL, 763ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 764ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru NULL, 765ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _UTF7GetName, 766ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru NULL, /* we don't need writeSub() because we never call a callback at fromUnicode() */ 767ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru NULL, 76864339d36f8bd4db5025fe2988eda22b491a9219cFredrik Roubert ucnv_getCompleteUnicodeSet, 76964339d36f8bd4db5025fe2988eda22b491a9219cFredrik Roubert 77064339d36f8bd4db5025fe2988eda22b491a9219cFredrik Roubert NULL, 77164339d36f8bd4db5025fe2988eda22b491a9219cFredrik Roubert NULL 772ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}; 773ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 774ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UConverterStaticData _UTF7StaticData={ 775ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sizeof(UConverterStaticData), 776ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru "UTF-7", 777ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0, /* TODO CCSID for UTF-7 */ 778ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UCNV_IBM, UCNV_UTF7, 779ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1, 4, 780ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 0x3f, 0, 0, 0 }, 1, /* the subchar is not used */ 781ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru FALSE, FALSE, 782ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0, 783ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0, 784ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */ 785ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}; 786ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 787c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubertconst UConverterSharedData _UTF7Data= 788c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(&_UTF7StaticData, &_UTF7Impl); 789ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 790ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* IMAP mailbox name encoding ----------------------------------------------- */ 791ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 792ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 793ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * RFC 2060: INTERNET MESSAGE ACCESS PROTOCOL - VERSION 4rev1 794ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * http://www.ietf.org/rfc/rfc2060.txt 795ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 796ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 5.1.3. Mailbox International Naming Convention 797ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 798ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * By convention, international mailbox names are specified using a 799ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * modified version of the UTF-7 encoding described in [UTF-7]. The 800ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * purpose of these modifications is to correct the following problems 801ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * with UTF-7: 802ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 803ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 1) UTF-7 uses the "+" character for shifting; this conflicts with 804ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * the common use of "+" in mailbox names, in particular USENET 805ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * newsgroup names. 806ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 807ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 2) UTF-7's encoding is BASE64 which uses the "/" character; this 808ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * conflicts with the use of "/" as a popular hierarchy delimiter. 809ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 810ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 3) UTF-7 prohibits the unencoded usage of "\"; this conflicts with 811ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * the use of "\" as a popular hierarchy delimiter. 812ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 813ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 4) UTF-7 prohibits the unencoded usage of "~"; this conflicts with 814ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * the use of "~" in some servers as a home directory indicator. 815ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 816ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 5) UTF-7 permits multiple alternate forms to represent the same 817ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * string; in particular, printable US-ASCII chararacters can be 818ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * represented in encoded form. 819ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 820ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * In modified UTF-7, printable US-ASCII characters except for "&" 821ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * represent themselves; that is, characters with octet values 0x20-0x25 822ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * and 0x27-0x7e. The character "&" (0x26) is represented by the two- 823ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * octet sequence "&-". 824ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 825ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * All other characters (octet values 0x00-0x1f, 0x7f-0xff, and all 826ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Unicode 16-bit octets) are represented in modified BASE64, with a 827ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * further modification from [UTF-7] that "," is used instead of "/". 828ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Modified BASE64 MUST NOT be used to represent any printing US-ASCII 829ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * character which can represent itself. 830ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 831ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * "&" is used to shift to modified BASE64 and "-" to shift back to US- 832ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * ASCII. All names start in US-ASCII, and MUST end in US-ASCII (that 833ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * is, a name that ends with a Unicode 16-bit octet MUST end with a "- 834ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * "). 835ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 836ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * For example, here is a mailbox name which mixes English, Japanese, 837ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * and Chinese text: ~peter/mail/&ZeVnLIqe-/&U,BTFw- 838ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 839ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 840ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 841ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Tests for US-ASCII characters belonging to character classes 842ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * defined in UTF-7. 843ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 844ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Set D (directly encoded characters) consists of the following 845ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * characters: the upper and lower case letters A through Z 846ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * and a through z, the 10 digits 0-9, and the following nine special 847ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * characters (note that "+" and "=" are omitted): 848ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * '(),-./:? 849ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 850ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Set O (optional direct characters) consists of the following 851ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * characters (note that "\" and "~" are omitted): 852ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * !"#$%&*;<=>@[]^_`{|} 853ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 854ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * According to the rules in RFC 2152, the byte values for the following 855ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * US-ASCII characters are not used in UTF-7 and are therefore illegal: 856ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * - all C0 control codes except for CR LF TAB 857ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * - BACKSLASH 858ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * - TILDE 859ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * - DEL 860ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * - all codes beyond US-ASCII, i.e. all >127 861ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 862ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 863ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* uses '&' not '+' to start a base64 sequence */ 864ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define AMPERSAND 0x26 865ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define COMMA 0x2c 866ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define SLASH 0x2f 867ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 868ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* legal byte values: all US-ASCII graphic characters 0x20..0x7e */ 869ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define isLegalIMAP(c) (0x20<=(c) && (c)<=0x7e) 870ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 871ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* direct-encode all of printable ASCII 0x20..0x7e except '&' 0x26 */ 872ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define inSetDIMAP(c) (isLegalIMAP(c) && c!=AMPERSAND) 873ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 874ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define TO_BASE64_IMAP(n) ((n)<63 ? toBase64[n] : COMMA) 875ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define FROM_BASE64_IMAP(c) ((c)==COMMA ? 63 : (c)==SLASH ? -1 : fromBase64[c]) 876ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 877ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 878ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * converter status values: 879ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 880ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * toUnicodeStatus: 881ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 24 inDirectMode (boolean) 882ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 23..16 base64Counter (-1..7) 883ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 15..0 bits (up to 14 bits incoming base64) 884ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 885ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * fromUnicodeStatus: 886ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 24 inDirectMode (boolean) 887ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 23..16 base64Counter (0..2) 888ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 7..0 bits (6 bits outgoing base64) 889ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 890ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * ignore bits 31..25 891ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 892ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 8930596faeddefbf198de137d5e893708495ab1584cFredrik RoubertU_CDECL_BEGIN 8940596faeddefbf198de137d5e893708495ab1584cFredrik Roubertstatic void U_CALLCONV 895ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru_IMAPToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs, 896ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode *pErrorCode) { 897ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UConverter *cnv; 898ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const uint8_t *source, *sourceLimit; 899ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar *target; 900ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UChar *targetLimit; 901ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t *offsets; 902ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 903ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint8_t *bytes; 904ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint8_t byteIndex; 905ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 906ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t length, targetCapacity; 907ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 908ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* UTF-7 state */ 909ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint16_t bits; 910ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int8_t base64Counter; 911ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool inDirectMode; 912ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 913ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int8_t base64Value; 914ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 915ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t sourceIndex, nextSourceIndex; 916ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 917ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar c; 918ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint8_t b; 919ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 920ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* set up the local pointers */ 921ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv=pArgs->converter; 922ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 923ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru source=(const uint8_t *)pArgs->source; 924ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceLimit=(const uint8_t *)pArgs->sourceLimit; 925ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru target=pArgs->target; 926ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru targetLimit=pArgs->targetLimit; 927ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru offsets=pArgs->offsets; 928ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* get the state machine state */ 929ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 930ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint32_t status=cnv->toUnicodeStatus; 931ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=(UBool)((status>>24)&1); 932ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=(int8_t)(status>>16); 933ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=(uint16_t)status; 934ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 935ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bytes=cnv->toUBytes; 936ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=cnv->toULength; 937ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 938ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* sourceIndex=-1 if the current character began in the previous buffer */ 939ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceIndex=byteIndex==0 ? 0 : -1; 940ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru nextSourceIndex=0; 941ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 942ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(inDirectMode) { 943ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QuerudirectMode: 944ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* 945ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * In Direct Mode, US-ASCII characters are encoded directly, i.e., 946ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * with their US-ASCII byte values. 947ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * An ampersand starts Unicode (or "escape") Mode. 948ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 949ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * In Direct Mode, only the sourceIndex is used. 950ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 951ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=0; 952ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru length=(int32_t)(sourceLimit-source); 953ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru targetCapacity=(int32_t)(targetLimit-target); 954ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(length>targetCapacity) { 955ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru length=targetCapacity; 956ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 957ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while(length>0) { 958ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=*source++; 959ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(!isLegalIMAP(b)) { 960ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* illegal */ 961ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bytes[0]=b; 962ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=1; 963ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_ILLEGAL_CHAR_FOUND; 964ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 965ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else if(b!=AMPERSAND) { 966ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* write directly encoded character */ 967ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=b; 968ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 969ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 970ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 971ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else /* AMPERSAND */ { 972ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* switch to Unicode mode */ 973ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru nextSourceIndex=++sourceIndex; 974ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=FALSE; 975ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=0; 976ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=0; 977ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=-1; 978ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru goto unicodeMode; 979ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 980ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru --length; 981ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 982ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(source<sourceLimit && target>=targetLimit) { 983ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* target is full */ 984ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 985ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 986ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 987ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruunicodeMode: 988ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* 989ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * In Unicode (or "escape") Mode, UTF-16BE is base64-encoded. 990ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * The base64 sequence ends with any character that is not in the base64 alphabet. 991ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * A terminating minus sign is consumed. 992ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * US-ASCII must not be base64-ed. 993ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 994ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * In Unicode Mode, the sourceIndex has the index to the start of the current 995ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * base64 bytes, while nextSourceIndex is precisely parallel to source, 996ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * keeping the index to the following byte. 997ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Note that in 2 out of 3 cases, UChars overlap within a base64 byte. 998ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 999ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while(source<sourceLimit) { 1000ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 1001ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bytes[byteIndex++]=b=*source++; 1002ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ++nextSourceIndex; 1003ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(b>0x7e) { 1004ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* illegal - test other illegal US-ASCII values by base64Value==-3 */ 1005ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=TRUE; 1006ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_ILLEGAL_CHAR_FOUND; 1007ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1008ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else if((base64Value=FROM_BASE64_IMAP(b))>=0) { 1009ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* collect base64 bytes into UChars */ 1010ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru switch(base64Counter) { 1011ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case -1: /* -1 is immediately after the & */ 1012ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 0: 1013ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=base64Value; 1014ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=1; 1015ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1016ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 1: 1017ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 3: 1018ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 4: 1019ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 6: 1020ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=(uint16_t)((bits<<6)|base64Value); 1021ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ++base64Counter; 1022ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1023ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 2: 1024ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru c=(UChar)((bits<<4)|(base64Value>>2)); 1025ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(isLegalIMAP(c)) { 1026ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* illegal */ 1027ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=TRUE; 1028ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_ILLEGAL_CHAR_FOUND; 1029ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru goto endloop; 1030ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1031ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=c; 1032ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1033ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 1034ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceIndex=nextSourceIndex-1; 1035ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1036ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bytes[0]=b; /* keep this byte in case an error occurs */ 1037ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=1; 1038ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=(uint16_t)(base64Value&3); 1039ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=3; 1040ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1041ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 5: 1042ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru c=(UChar)((bits<<2)|(base64Value>>4)); 1043ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(isLegalIMAP(c)) { 1044ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* illegal */ 1045ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=TRUE; 1046ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_ILLEGAL_CHAR_FOUND; 1047ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru goto endloop; 1048ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1049ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=c; 1050ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1051ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 1052ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceIndex=nextSourceIndex-1; 1053ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1054ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bytes[0]=b; /* keep this byte in case an error occurs */ 1055ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=1; 1056ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=(uint16_t)(base64Value&15); 1057ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=6; 1058ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1059ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 7: 1060ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru c=(UChar)((bits<<6)|base64Value); 1061ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(isLegalIMAP(c)) { 1062ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* illegal */ 1063ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=TRUE; 1064ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_ILLEGAL_CHAR_FOUND; 1065ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru goto endloop; 1066ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1067ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=c; 1068ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1069ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 1070ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceIndex=nextSourceIndex; 1071ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1072ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=0; 1073ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=0; 1074ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=0; 1075ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1076ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru default: 1077ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* will never occur */ 1078ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1079ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1080ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else if(base64Value==-2) { 1081ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* minus sign terminates the base64 sequence */ 1082ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=TRUE; 1083ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(base64Counter==-1) { 1084ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* &- i.e. a minus immediately following an ampersand */ 1085ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=AMPERSAND; 1086ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1087ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex-1; 1088ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1089ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1090ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* absorb the minus and leave the Unicode Mode */ 1091ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(bits!=0 || (base64Counter!=0 && base64Counter!=3 && base64Counter!=6)) { 1092ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* bits are illegally left over, a UChar is incomplete */ 1093ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* base64Counter other than 0, 3, 6 means non-minimal zero-padding, also illegal */ 1094ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_ILLEGAL_CHAR_FOUND; 1095ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1096ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1097ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1098ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceIndex=nextSourceIndex; 1099ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru goto directMode; 1100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(base64Counter==-1) { 1102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* illegal: & immediately followed by something other than base64 or minus sign */ 1103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* include the ampersand in the reported sequence */ 1104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru --sourceIndex; 1105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bytes[0]=AMPERSAND; 1106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bytes[1]=b; 1107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=2; 1108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* base64Value==-1 for characters that are illegal only in Unicode mode */ 1110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* base64Value==-3 for illegal characters */ 1111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* illegal */ 1112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=TRUE; 1113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_ILLEGAL_CHAR_FOUND; 1114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* target is full */ 1118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 1119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruendloop: 1124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* 1126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * the end of the input stream and detection of truncated input 1127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * are handled by the framework, but here we must check if we are in Unicode 1128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * mode and byteIndex==0 because we must end in direct mode 1129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 1130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * conditions: 1131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * successful 1132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * in Unicode mode and byteIndex==0 1133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * end of input and no truncated input 1134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if( U_SUCCESS(*pErrorCode) && 1136ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru !inDirectMode && byteIndex==0 && 1137ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pArgs->flush && source>=sourceLimit 1138ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ) { 1139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(base64Counter==-1) { 1140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* & at the very end of the input */ 1141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* make the ampersand the reported sequence */ 1142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bytes[0]=AMPERSAND; 1143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru byteIndex=1; 1144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1145ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* else if(base64Counter!=-1) byteIndex remains 0 because there is no particular byte sequence */ 1146ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=TRUE; /* avoid looping */ 1148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_TRUNCATED_CHAR_FOUND; 1149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* set the converter state back into UConverter */ 1152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->toUnicodeStatus=((uint32_t)inDirectMode<<24)|((uint32_t)((uint8_t)base64Counter)<<16)|(uint32_t)bits; 1153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->toULength=byteIndex; 1154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* write back the updated pointers */ 1156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pArgs->source=(const char *)source; 1157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pArgs->target=target; 1158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pArgs->offsets=offsets; 1159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 1160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 11620596faeddefbf198de137d5e893708495ab1584cFredrik Roubertstatic void U_CALLCONV 1163ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru_IMAPFromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs, 1164ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode *pErrorCode) { 1165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UConverter *cnv; 1166ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UChar *source, *sourceLimit; 1167ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint8_t *target, *targetLimit; 1168ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t *offsets; 1169ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1170ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t length, targetCapacity, sourceIndex; 1171ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar c; 1172ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint8_t b; 1173ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1174ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* UTF-7 state */ 1175ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint8_t bits; 1176ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int8_t base64Counter; 1177ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool inDirectMode; 1178ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1179ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* set up the local pointers */ 1180ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv=pArgs->converter; 1181ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1182ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* set up the local pointers */ 1183ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru source=pArgs->source; 1184ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceLimit=pArgs->sourceLimit; 1185ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru target=(uint8_t *)pArgs->target; 1186ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru targetLimit=(uint8_t *)pArgs->targetLimit; 1187ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru offsets=pArgs->offsets; 1188ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1189ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* get the state machine state */ 1190ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 1191ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint32_t status=cnv->fromUnicodeStatus; 1192ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=(UBool)((status>>24)&1); 1193ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=(int8_t)(status>>16); 1194ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=(uint8_t)status; 1195ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1196ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1197ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* UTF-7 always encodes UTF-16 code units, therefore we need only a simple sourceIndex */ 1198ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sourceIndex=0; 1199ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1200ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(inDirectMode) { 1201ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QuerudirectMode: 1202ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru length=(int32_t)(sourceLimit-source); 1203ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru targetCapacity=(int32_t)(targetLimit-target); 1204ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(length>targetCapacity) { 1205ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru length=targetCapacity; 1206ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1207ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while(length>0) { 1208ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru c=*source++; 1209ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* encode 0x20..0x7e except '&' directly */ 1210ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(inSetDIMAP(c)) { 1211ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* encode directly */ 1212ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=(uint8_t)c; 1213ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1214ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 1215ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1216ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else if(c==AMPERSAND) { 1217ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* output &- for & */ 1218ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=AMPERSAND; 1219ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 1220ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=MINUS; 1221ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1222ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 1223ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 1224ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1225ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* realign length and targetCapacity */ 1226ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru goto directMode; 1227ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1228ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1229ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 1230ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1231ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[0]=MINUS; 1232ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBufferLength=1; 1233ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 1234ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1235ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1236ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1237ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* un-read this character and switch to Unicode Mode */ 1238ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru --source; 1239ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=AMPERSAND; 1240ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1241ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 1242ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1243ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=FALSE; 1244ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=0; 1245ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru goto unicodeMode; 1246ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1247ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru --length; 1248ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1249ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(source<sourceLimit && target>=targetLimit) { 1250ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* target is full */ 1251ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 1252ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1253ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1254ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruunicodeMode: 1255ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while(source<sourceLimit) { 1256ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 1257ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru c=*source++; 1258ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(isLegalIMAP(c)) { 1259ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* encode directly */ 1260ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru inDirectMode=TRUE; 1261ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1262ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* trick: back out this character to make this easier */ 1263ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru --source; 1264ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1265ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* terminate the base64 sequence */ 1266ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(base64Counter!=0) { 1267ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* write remaining bits for the previous character */ 1268ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=TO_BASE64_IMAP(bits); 1269ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1270ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex-1; 1271ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1272ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1273ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* need to terminate with a minus */ 1274ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 1275ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=MINUS; 1276ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1277ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex-1; 1278ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1279ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1280ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[0]=MINUS; 1281ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBufferLength=1; 1282ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 1283ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1284ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1285ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru goto directMode; 1286ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1287ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* 1288ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * base64 this character: 1289ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Output 2 or 3 base64 bytes for the remaining bits of the previous character 1290ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * and the bits of this character, each implicitly in UTF-16BE. 1291ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 1292ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Here, bits is an 8-bit variable because only 6 bits need to be kept from one 1293ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * character to the next. The actual 2 or 4 bits are shifted to the left edge 1294ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * of the 6-bits field 5..0 to make the termination of the base64 sequence easier. 1295ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1296ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru switch(base64Counter) { 1297ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 0: 1298ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=(uint8_t)(c>>10); 1299ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=TO_BASE64_IMAP(b); 1300ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 1301ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=(uint8_t)((c>>4)&0x3f); 1302ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=TO_BASE64_IMAP(b); 1303ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1304ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 1305ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 1306ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1307ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1308ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1309ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 1310ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1311ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=(uint8_t)((c>>4)&0x3f); 1312ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[0]=TO_BASE64_IMAP(b); 1313ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBufferLength=1; 1314ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 1315ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1316ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=(uint8_t)((c&15)<<2); 1317ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=1; 1318ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1319ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 1: 1320ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=(uint8_t)(bits|(c>>14)); 1321ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=TO_BASE64_IMAP(b); 1322ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 1323ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=(uint8_t)((c>>8)&0x3f); 1324ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=TO_BASE64_IMAP(b); 1325ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 1326ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=(uint8_t)((c>>2)&0x3f); 1327ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=TO_BASE64_IMAP(b); 1328ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1329ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 1330ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 1331ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 1332ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1333ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1334ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1335ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 1336ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 1337ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1338ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=(uint8_t)((c>>2)&0x3f); 1339ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[0]=TO_BASE64_IMAP(b); 1340ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBufferLength=1; 1341ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 1342ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1343ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1344ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1345ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 1346ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1347ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=(uint8_t)((c>>8)&0x3f); 1348ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[0]=TO_BASE64_IMAP(b); 1349ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=(uint8_t)((c>>2)&0x3f); 1350ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[1]=TO_BASE64_IMAP(b); 1351ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBufferLength=2; 1352ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 1353ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1354ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=(uint8_t)((c&3)<<4); 1355ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=2; 1356ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1357ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case 2: 1358ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=(uint8_t)(bits|(c>>12)); 1359ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=TO_BASE64_IMAP(b); 1360ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 1361ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=(uint8_t)((c>>6)&0x3f); 1362ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=TO_BASE64_IMAP(b); 1363ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 1364ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=(uint8_t)(c&0x3f); 1365ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=TO_BASE64_IMAP(b); 1366ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1367ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 1368ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 1369ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 1370ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1371ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1372ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1373ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex; 1374ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 1375ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1376ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=(uint8_t)(c&0x3f); 1377ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[0]=TO_BASE64_IMAP(b); 1378ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBufferLength=1; 1379ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 1380ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1381ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1382ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1383ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex++; 1384ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1385ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=(uint8_t)((c>>6)&0x3f); 1386ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[0]=TO_BASE64_IMAP(b); 1387ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru b=(uint8_t)(c&0x3f); 1388ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[1]=TO_BASE64_IMAP(b); 1389ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBufferLength=2; 1390ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 1391ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1392ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru bits=0; 1393ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru base64Counter=0; 1394ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1395ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru default: 1396ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* will never occur */ 1397ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1398ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1399ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1400ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1401ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* target is full */ 1402ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 1403ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1404ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1405ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1406ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1407ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1408ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(pArgs->flush && source>=sourceLimit) { 1409ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* flush remaining bits to the target */ 1410ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(!inDirectMode) { 1411ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(base64Counter!=0) { 1412ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 1413ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=TO_BASE64_IMAP(bits); 1414ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1415ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex-1; 1416ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1417ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1418ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[cnv->charErrorBufferLength++]=TO_BASE64_IMAP(bits); 1419ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 1420ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1421ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1422ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* need to terminate with a minus */ 1423ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(target<targetLimit) { 1424ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target++=MINUS; 1425ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(offsets!=NULL) { 1426ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *offsets++=sourceIndex-1; 1427ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1428ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1429ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->charErrorBuffer[cnv->charErrorBufferLength++]=MINUS; 1430ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *pErrorCode=U_BUFFER_OVERFLOW_ERROR; 1431ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1432ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1433ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* reset the state for the next conversion */ 1434ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->fromUnicodeStatus=(cnv->fromUnicodeStatus&0xf0000000)|0x1000000; /* keep version, inDirectMode=TRUE */ 1435ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1436ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* set the converter state back into UConverter */ 1437ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cnv->fromUnicodeStatus= 1438ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (cnv->fromUnicodeStatus&0xf0000000)| /* keep version*/ 1439ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ((uint32_t)inDirectMode<<24)|((uint32_t)base64Counter<<16)|(uint32_t)bits; 1440ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1441ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1442ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* write back the updated pointers */ 1443ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pArgs->source=source; 1444ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pArgs->target=(char *)target; 1445ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pArgs->offsets=offsets; 1446ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 1447ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 14480596faeddefbf198de137d5e893708495ab1584cFredrik RoubertU_CDECL_END 1449ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1450ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UConverterImpl _IMAPImpl={ 1451ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UCNV_IMAP_MAILBOX, 1452ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1453ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru NULL, 1454ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru NULL, 1455ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1456ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _UTF7Open, 1457ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru NULL, 1458ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _UTF7Reset, 1459ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1460ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _IMAPToUnicodeWithOffsets, 1461ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _IMAPToUnicodeWithOffsets, 1462ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _IMAPFromUnicodeWithOffsets, 1463ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru _IMAPFromUnicodeWithOffsets, 1464ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru NULL, 1465ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1466ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru NULL, 1467ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru NULL, 1468ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru NULL, /* we don't need writeSub() because we never call a callback at fromUnicode() */ 1469ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru NULL, 14700596faeddefbf198de137d5e893708495ab1584cFredrik Roubert ucnv_getCompleteUnicodeSet, 14710596faeddefbf198de137d5e893708495ab1584cFredrik Roubert NULL, 14720596faeddefbf198de137d5e893708495ab1584cFredrik Roubert NULL 1473ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}; 1474ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1475ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UConverterStaticData _IMAPStaticData={ 1476ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru sizeof(UConverterStaticData), 1477ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru "IMAP-mailbox-name", 1478ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0, /* TODO CCSID for IMAP-mailbox-name */ 1479ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UCNV_IBM, UCNV_IMAP_MAILBOX, 1480ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1, 4, 1481ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 0x3f, 0, 0, 0 }, 1, /* the subchar is not used */ 1482ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru FALSE, FALSE, 1483ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0, 1484ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0, 1485ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */ 1486ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}; 1487ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1488c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubertconst UConverterSharedData _IMAPData= 1489c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(&_IMAPStaticData, &_IMAPImpl); 1490ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1491ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif 1492