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