1*aef5731fSOliver Tappe /*
2*aef5731fSOliver Tappe * Copyright (C) 1999-2001 Free Software Foundation, Inc.
3*aef5731fSOliver Tappe * This file is part of the GNU LIBICONV Library.
4*aef5731fSOliver Tappe *
5*aef5731fSOliver Tappe * The GNU LIBICONV Library is free software; you can redistribute it
6*aef5731fSOliver Tappe * and/or modify it under the terms of the GNU Library General Public
7*aef5731fSOliver Tappe * License as published by the Free Software Foundation; either version 2
8*aef5731fSOliver Tappe * of the License, or (at your option) any later version.
9*aef5731fSOliver Tappe *
10*aef5731fSOliver Tappe * The GNU LIBICONV Library is distributed in the hope that it will be
11*aef5731fSOliver Tappe * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12*aef5731fSOliver Tappe * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13*aef5731fSOliver Tappe * Library General Public License for more details.
14*aef5731fSOliver Tappe *
15*aef5731fSOliver Tappe * You should have received a copy of the GNU Library General Public
16*aef5731fSOliver Tappe * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
17*aef5731fSOliver Tappe * If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
18*aef5731fSOliver Tappe * Fifth Floor, Boston, MA 02110-1301, USA.
19*aef5731fSOliver Tappe */
20*aef5731fSOliver Tappe
21*aef5731fSOliver Tappe /*
22*aef5731fSOliver Tappe * UTF-7
23*aef5731fSOliver Tappe */
24*aef5731fSOliver Tappe
25*aef5731fSOliver Tappe /* Specification: RFC 2152 (and old RFC 1641, RFC 1642) */
26*aef5731fSOliver Tappe /* The original Base64 encoding is defined in RFC 2045. */
27*aef5731fSOliver Tappe
28*aef5731fSOliver Tappe /* Set of direct characters:
29*aef5731fSOliver Tappe * A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr
30*aef5731fSOliver Tappe */
31*aef5731fSOliver Tappe static const unsigned char direct_tab[128/8] = {
32*aef5731fSOliver Tappe 0x00, 0x26, 0x00, 0x00, 0x81, 0xf3, 0xff, 0x87,
33*aef5731fSOliver Tappe 0xfe, 0xff, 0xff, 0x07, 0xfe, 0xff, 0xff, 0x07,
34*aef5731fSOliver Tappe };
35*aef5731fSOliver Tappe #define isdirect(ch) ((ch) < 128 && ((direct_tab[(ch)>>3] >> (ch & 7)) & 1))
36*aef5731fSOliver Tappe
37*aef5731fSOliver Tappe /* Set of direct and optional direct characters:
38*aef5731fSOliver Tappe * A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr
39*aef5731fSOliver Tappe * ! " # $ % & * ; < = > @ [ ] ^ _ ` { | }
40*aef5731fSOliver Tappe */
41*aef5731fSOliver Tappe static const unsigned char xdirect_tab[128/8] = {
42*aef5731fSOliver Tappe 0x00, 0x26, 0x00, 0x00, 0xff, 0xf7, 0xff, 0xff,
43*aef5731fSOliver Tappe 0xff, 0xff, 0xff, 0xef, 0xff, 0xff, 0xff, 0x3f,
44*aef5731fSOliver Tappe };
45*aef5731fSOliver Tappe #define isxdirect(ch) ((ch) < 128 && ((xdirect_tab[(ch)>>3] >> (ch & 7)) & 1))
46*aef5731fSOliver Tappe
47*aef5731fSOliver Tappe /* Set of base64 characters, extended:
48*aef5731fSOliver Tappe * A-Z a-z 0-9 + / -
49*aef5731fSOliver Tappe */
50*aef5731fSOliver Tappe static const unsigned char xbase64_tab[128/8] = {
51*aef5731fSOliver Tappe 0x00, 0x00, 0x00, 0x00, 0x00, 0xa8, 0xff, 0x03,
52*aef5731fSOliver Tappe 0xfe, 0xff, 0xff, 0x07, 0xfe, 0xff, 0xff, 0x07,
53*aef5731fSOliver Tappe };
54*aef5731fSOliver Tappe #define isxbase64(ch) ((ch) < 128 && ((xbase64_tab[(ch)>>3] >> (ch & 7)) & 1))
55*aef5731fSOliver Tappe
56*aef5731fSOliver Tappe /*
57*aef5731fSOliver Tappe * The state is structured as follows:
58*aef5731fSOliver Tappe * bit 1..0: shift
59*aef5731fSOliver Tappe * bit 7..2: data
60*aef5731fSOliver Tappe * Precise meaning:
61*aef5731fSOliver Tappe * shift data
62*aef5731fSOliver Tappe * 0 0 not inside base64 encoding
63*aef5731fSOliver Tappe * 1 0 inside base64, no pending bits
64*aef5731fSOliver Tappe * 2 XXXX00 inside base64, 4 bits remain from 2nd byte
65*aef5731fSOliver Tappe * 3 XX0000 inside base64, 2 bits remain from 3rd byte
66*aef5731fSOliver Tappe */
67*aef5731fSOliver Tappe
68*aef5731fSOliver Tappe static int
utf7_mbtowc(conv_t conv,ucs4_t * pwc,const unsigned char * s,int n)69*aef5731fSOliver Tappe utf7_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
70*aef5731fSOliver Tappe {
71*aef5731fSOliver Tappe state_t state = conv->istate;
72*aef5731fSOliver Tappe int count = 0; /* number of input bytes already read */
73*aef5731fSOliver Tappe if (state & 3)
74*aef5731fSOliver Tappe goto active;
75*aef5731fSOliver Tappe else
76*aef5731fSOliver Tappe goto inactive;
77*aef5731fSOliver Tappe
78*aef5731fSOliver Tappe inactive:
79*aef5731fSOliver Tappe {
80*aef5731fSOliver Tappe /* Here (state & 3) == 0 */
81*aef5731fSOliver Tappe if (n < count+1)
82*aef5731fSOliver Tappe goto none;
83*aef5731fSOliver Tappe {
84*aef5731fSOliver Tappe unsigned char c = *s;
85*aef5731fSOliver Tappe if (isxdirect(c)) {
86*aef5731fSOliver Tappe *pwc = (ucs4_t) c;
87*aef5731fSOliver Tappe conv->istate = state;
88*aef5731fSOliver Tappe return count+1;
89*aef5731fSOliver Tappe }
90*aef5731fSOliver Tappe if (c == '+') {
91*aef5731fSOliver Tappe if (n < count+2)
92*aef5731fSOliver Tappe goto none;
93*aef5731fSOliver Tappe if (s[1] == '-') {
94*aef5731fSOliver Tappe *pwc = (ucs4_t) '+';
95*aef5731fSOliver Tappe conv->istate = state;
96*aef5731fSOliver Tappe return count+2;
97*aef5731fSOliver Tappe }
98*aef5731fSOliver Tappe s++; count++;
99*aef5731fSOliver Tappe state = 1;
100*aef5731fSOliver Tappe goto active;
101*aef5731fSOliver Tappe }
102*aef5731fSOliver Tappe return RET_ILSEQ;
103*aef5731fSOliver Tappe }
104*aef5731fSOliver Tappe }
105*aef5731fSOliver Tappe
106*aef5731fSOliver Tappe active:
107*aef5731fSOliver Tappe {
108*aef5731fSOliver Tappe /* base64 encoding active */
109*aef5731fSOliver Tappe unsigned int wc = 0;
110*aef5731fSOliver Tappe state_t base64state = state;
111*aef5731fSOliver Tappe unsigned int kmax = 2; /* number of payload bytes to read */
112*aef5731fSOliver Tappe unsigned int k = 0; /* number of payload bytes already read */
113*aef5731fSOliver Tappe unsigned int base64count = 0; /* number of base64 bytes already read */
114*aef5731fSOliver Tappe for (;;) {
115*aef5731fSOliver Tappe unsigned char c = *s;
116*aef5731fSOliver Tappe unsigned int i;
117*aef5731fSOliver Tappe if (c >= 'A' && c <= 'Z')
118*aef5731fSOliver Tappe i = c-'A';
119*aef5731fSOliver Tappe else if (c >= 'a' && c <= 'z')
120*aef5731fSOliver Tappe i = c-'a'+26;
121*aef5731fSOliver Tappe else if (c >= '0' && c <= '9')
122*aef5731fSOliver Tappe i = c-'0'+52;
123*aef5731fSOliver Tappe else if (c == '+')
124*aef5731fSOliver Tappe i = 62;
125*aef5731fSOliver Tappe else if (c == '/')
126*aef5731fSOliver Tappe i = 63;
127*aef5731fSOliver Tappe else {
128*aef5731fSOliver Tappe /* c terminates base64 encoding */
129*aef5731fSOliver Tappe if (base64state & -4)
130*aef5731fSOliver Tappe return RET_ILSEQ; /* data must be 0, otherwise illegal */
131*aef5731fSOliver Tappe if (base64count)
132*aef5731fSOliver Tappe return RET_ILSEQ; /* partial UTF-16 characters are invalid */
133*aef5731fSOliver Tappe if (c == '-') {
134*aef5731fSOliver Tappe s++; count++;
135*aef5731fSOliver Tappe }
136*aef5731fSOliver Tappe state = 0;
137*aef5731fSOliver Tappe goto inactive;
138*aef5731fSOliver Tappe }
139*aef5731fSOliver Tappe s++; base64count++;
140*aef5731fSOliver Tappe /* read 6 bits: 0 <= i < 64 */
141*aef5731fSOliver Tappe switch (base64state & 3) {
142*aef5731fSOliver Tappe case 1: /* inside base64, no pending bits */
143*aef5731fSOliver Tappe base64state = (i << 2) | 0; break;
144*aef5731fSOliver Tappe case 0: /* inside base64, 6 bits remain from 1st byte */
145*aef5731fSOliver Tappe wc = (wc << 8) | (base64state & -4) | (i >> 4); k++;
146*aef5731fSOliver Tappe base64state = ((i & 15) << 4) | 2; break;
147*aef5731fSOliver Tappe case 2: /* inside base64, 4 bits remain from 2nd byte */
148*aef5731fSOliver Tappe wc = (wc << 8) | (base64state & -4) | (i >> 2); k++;
149*aef5731fSOliver Tappe base64state = ((i & 3) << 6) | 3; break;
150*aef5731fSOliver Tappe case 3: /* inside base64, 2 bits remain from 3rd byte */
151*aef5731fSOliver Tappe wc = (wc << 8) | (base64state & -4) | i; k++;
152*aef5731fSOliver Tappe base64state = 1; break;
153*aef5731fSOliver Tappe }
154*aef5731fSOliver Tappe if (k == kmax) {
155*aef5731fSOliver Tappe /* UTF-16: When we see a High Surrogate, we must also decode
156*aef5731fSOliver Tappe the following Low Surrogate. */
157*aef5731fSOliver Tappe if (kmax == 2 && (wc >= 0xd800 && wc < 0xdc00))
158*aef5731fSOliver Tappe kmax = 4;
159*aef5731fSOliver Tappe else
160*aef5731fSOliver Tappe break;
161*aef5731fSOliver Tappe }
162*aef5731fSOliver Tappe if (n < count+base64count+1)
163*aef5731fSOliver Tappe goto none;
164*aef5731fSOliver Tappe }
165*aef5731fSOliver Tappe /* Here k = kmax > 0, hence base64count > 0. */
166*aef5731fSOliver Tappe if ((base64state & 3) == 0) abort();
167*aef5731fSOliver Tappe if (kmax == 4) {
168*aef5731fSOliver Tappe ucs4_t wc1 = wc >> 16;
169*aef5731fSOliver Tappe ucs4_t wc2 = wc & 0xffff;
170*aef5731fSOliver Tappe if (!(wc1 >= 0xd800 && wc1 < 0xdc00)) abort();
171*aef5731fSOliver Tappe if (!(wc2 >= 0xdc00 && wc2 < 0xe000)) return RET_ILSEQ;
172*aef5731fSOliver Tappe *pwc = 0x10000 + ((wc1 - 0xd800) << 10) + (wc2 - 0xdc00);
173*aef5731fSOliver Tappe } else {
174*aef5731fSOliver Tappe *pwc = wc;
175*aef5731fSOliver Tappe }
176*aef5731fSOliver Tappe conv->istate = base64state;
177*aef5731fSOliver Tappe return count+base64count;
178*aef5731fSOliver Tappe }
179*aef5731fSOliver Tappe
180*aef5731fSOliver Tappe none:
181*aef5731fSOliver Tappe conv->istate = state;
182*aef5731fSOliver Tappe return RET_TOOFEW(count);
183*aef5731fSOliver Tappe }
184*aef5731fSOliver Tappe
185*aef5731fSOliver Tappe /*
186*aef5731fSOliver Tappe * The state is structured as follows:
187*aef5731fSOliver Tappe * bit 1..0: shift
188*aef5731fSOliver Tappe * bit 7..2: data
189*aef5731fSOliver Tappe * Precise meaning:
190*aef5731fSOliver Tappe * shift data
191*aef5731fSOliver Tappe * 0 0 not inside base64 encoding
192*aef5731fSOliver Tappe * 1 0 inside base64, no pending bits
193*aef5731fSOliver Tappe * 2 XX00 inside base64, 2 bits known for 2nd byte
194*aef5731fSOliver Tappe * 3 XXXX inside base64, 4 bits known for 3rd byte
195*aef5731fSOliver Tappe */
196*aef5731fSOliver Tappe
197*aef5731fSOliver Tappe /* Define this to 1 if you want the so-called "optional direct" characters
198*aef5731fSOliver Tappe ! " # $ % & * ; < = > @ [ ] ^ _ ` { | }
199*aef5731fSOliver Tappe to be encoded. Define to 0 if you want them to be passed straight through,
200*aef5731fSOliver Tappe like the so-called "direct" characters.
201*aef5731fSOliver Tappe We set this to 1 because it's safer.
202*aef5731fSOliver Tappe */
203*aef5731fSOliver Tappe #define UTF7_ENCODE_OPTIONAL_CHARS 1
204*aef5731fSOliver Tappe
205*aef5731fSOliver Tappe static int
utf7_wctomb(conv_t conv,unsigned char * r,ucs4_t iwc,int n)206*aef5731fSOliver Tappe utf7_wctomb (conv_t conv, unsigned char *r, ucs4_t iwc, int n)
207*aef5731fSOliver Tappe {
208*aef5731fSOliver Tappe state_t state = conv->ostate;
209*aef5731fSOliver Tappe unsigned int wc = iwc;
210*aef5731fSOliver Tappe int count = 0;
211*aef5731fSOliver Tappe if (state & 3)
212*aef5731fSOliver Tappe goto active;
213*aef5731fSOliver Tappe
214*aef5731fSOliver Tappe /*inactive:*/
215*aef5731fSOliver Tappe {
216*aef5731fSOliver Tappe if (UTF7_ENCODE_OPTIONAL_CHARS ? isdirect(wc) : isxdirect(wc)) {
217*aef5731fSOliver Tappe r[0] = (unsigned char) wc;
218*aef5731fSOliver Tappe /*conv->ostate = state;*/
219*aef5731fSOliver Tappe return 1;
220*aef5731fSOliver Tappe } else {
221*aef5731fSOliver Tappe *r++ = '+';
222*aef5731fSOliver Tappe if (wc == '+') {
223*aef5731fSOliver Tappe if (n < 2)
224*aef5731fSOliver Tappe return RET_TOOSMALL;
225*aef5731fSOliver Tappe *r = '-';
226*aef5731fSOliver Tappe /*conv->ostate = state;*/
227*aef5731fSOliver Tappe return 2;
228*aef5731fSOliver Tappe }
229*aef5731fSOliver Tappe count = 1;
230*aef5731fSOliver Tappe state = 1;
231*aef5731fSOliver Tappe goto active;
232*aef5731fSOliver Tappe }
233*aef5731fSOliver Tappe }
234*aef5731fSOliver Tappe
235*aef5731fSOliver Tappe active:
236*aef5731fSOliver Tappe {
237*aef5731fSOliver Tappe /* base64 encoding active */
238*aef5731fSOliver Tappe if (UTF7_ENCODE_OPTIONAL_CHARS ? isdirect(wc) : isxdirect(wc)) {
239*aef5731fSOliver Tappe /* deactivate base64 encoding */
240*aef5731fSOliver Tappe count += ((state & 3) >= 2 ? 1 : 0) + (isxbase64(wc) ? 1 : 0) + 1;
241*aef5731fSOliver Tappe if (n < count)
242*aef5731fSOliver Tappe return RET_TOOSMALL;
243*aef5731fSOliver Tappe if ((state & 3) >= 2) {
244*aef5731fSOliver Tappe unsigned int i = state & -4;
245*aef5731fSOliver Tappe unsigned char c;
246*aef5731fSOliver Tappe if (i < 26)
247*aef5731fSOliver Tappe c = i+'A';
248*aef5731fSOliver Tappe else if (i < 52)
249*aef5731fSOliver Tappe c = i-26+'a';
250*aef5731fSOliver Tappe else if (i < 62)
251*aef5731fSOliver Tappe c = i-52+'0';
252*aef5731fSOliver Tappe else if (i == 62)
253*aef5731fSOliver Tappe c = '+';
254*aef5731fSOliver Tappe else if (i == 63)
255*aef5731fSOliver Tappe c = '/';
256*aef5731fSOliver Tappe else
257*aef5731fSOliver Tappe abort();
258*aef5731fSOliver Tappe *r++ = c;
259*aef5731fSOliver Tappe }
260*aef5731fSOliver Tappe if (isxbase64(wc))
261*aef5731fSOliver Tappe *r++ = '-';
262*aef5731fSOliver Tappe state = 0;
263*aef5731fSOliver Tappe *r++ = (unsigned char) wc;
264*aef5731fSOliver Tappe conv->ostate = state;
265*aef5731fSOliver Tappe return count;
266*aef5731fSOliver Tappe } else {
267*aef5731fSOliver Tappe unsigned int k; /* number of payload bytes to write */
268*aef5731fSOliver Tappe if (wc < 0x10000) {
269*aef5731fSOliver Tappe k = 2;
270*aef5731fSOliver Tappe count += ((state & 3) >= 2 ? 3 : 2);
271*aef5731fSOliver Tappe } else if (wc < 0x110000) {
272*aef5731fSOliver Tappe unsigned int wc1 = 0xd800 + ((wc - 0x10000) >> 10);
273*aef5731fSOliver Tappe unsigned int wc2 = 0xdc00 + ((wc - 0x10000) & 0x3ff);
274*aef5731fSOliver Tappe wc = (wc1 << 16) | wc2;
275*aef5731fSOliver Tappe k = 4;
276*aef5731fSOliver Tappe count += ((state & 3) >= 3 ? 6 : 5);
277*aef5731fSOliver Tappe } else
278*aef5731fSOliver Tappe return RET_ILUNI;
279*aef5731fSOliver Tappe if (n < count)
280*aef5731fSOliver Tappe return RET_TOOSMALL;
281*aef5731fSOliver Tappe for (;;) {
282*aef5731fSOliver Tappe unsigned int i;
283*aef5731fSOliver Tappe unsigned char c;
284*aef5731fSOliver Tappe switch (state & 3) {
285*aef5731fSOliver Tappe case 0: /* inside base64, 6 bits known for 4th byte */
286*aef5731fSOliver Tappe c = (state & -4) >> 2; state = 1; break;
287*aef5731fSOliver Tappe case 1: /* inside base64, no pending bits */
288*aef5731fSOliver Tappe i = (wc >> (8 * --k)) & 0xff;
289*aef5731fSOliver Tappe c = i >> 2; state = ((i & 3) << 4) | 2; break;
290*aef5731fSOliver Tappe case 2: /* inside base64, 2 bits known for 2nd byte */
291*aef5731fSOliver Tappe i = (wc >> (8 * --k)) & 0xff;
292*aef5731fSOliver Tappe c = (state & -4) | (i >> 4); state = ((i & 15) << 2) | 3; break;
293*aef5731fSOliver Tappe case 3: /* inside base64, 4 bits known for 3rd byte */
294*aef5731fSOliver Tappe i = (wc >> (8 * --k)) & 0xff;
295*aef5731fSOliver Tappe c = (state & -4) | (i >> 6); state = ((i & 63) << 2) | 0; break;
296*aef5731fSOliver Tappe default: abort(); /* stupid gcc */
297*aef5731fSOliver Tappe }
298*aef5731fSOliver Tappe if (c < 26)
299*aef5731fSOliver Tappe c = c+'A';
300*aef5731fSOliver Tappe else if (c < 52)
301*aef5731fSOliver Tappe c = c-26+'a';
302*aef5731fSOliver Tappe else if (c < 62)
303*aef5731fSOliver Tappe c = c-52+'0';
304*aef5731fSOliver Tappe else if (c == 62)
305*aef5731fSOliver Tappe c = '+';
306*aef5731fSOliver Tappe else if (c == 63)
307*aef5731fSOliver Tappe c = '/';
308*aef5731fSOliver Tappe else
309*aef5731fSOliver Tappe abort();
310*aef5731fSOliver Tappe *r++ = c;
311*aef5731fSOliver Tappe if ((state & 3) && (k == 0))
312*aef5731fSOliver Tappe break;
313*aef5731fSOliver Tappe }
314*aef5731fSOliver Tappe conv->ostate = state;
315*aef5731fSOliver Tappe return count;
316*aef5731fSOliver Tappe }
317*aef5731fSOliver Tappe }
318*aef5731fSOliver Tappe }
319*aef5731fSOliver Tappe
320*aef5731fSOliver Tappe static int
utf7_reset(conv_t conv,unsigned char * r,int n)321*aef5731fSOliver Tappe utf7_reset (conv_t conv, unsigned char *r, int n)
322*aef5731fSOliver Tappe {
323*aef5731fSOliver Tappe state_t state = conv->ostate;
324*aef5731fSOliver Tappe if (state & 3) {
325*aef5731fSOliver Tappe /* deactivate base64 encoding */
326*aef5731fSOliver Tappe unsigned int count = ((state & 3) >= 2 ? 1 : 0) + 1;
327*aef5731fSOliver Tappe if (n < count)
328*aef5731fSOliver Tappe return RET_TOOSMALL;
329*aef5731fSOliver Tappe if ((state & 3) >= 2) {
330*aef5731fSOliver Tappe unsigned int i = state & -4;
331*aef5731fSOliver Tappe unsigned char c;
332*aef5731fSOliver Tappe if (i < 26)
333*aef5731fSOliver Tappe c = i+'A';
334*aef5731fSOliver Tappe else if (i < 52)
335*aef5731fSOliver Tappe c = i-26+'a';
336*aef5731fSOliver Tappe else if (i < 62)
337*aef5731fSOliver Tappe c = i-52+'0';
338*aef5731fSOliver Tappe else if (i == 62)
339*aef5731fSOliver Tappe c = '+';
340*aef5731fSOliver Tappe else if (i == 63)
341*aef5731fSOliver Tappe c = '/';
342*aef5731fSOliver Tappe else
343*aef5731fSOliver Tappe abort();
344*aef5731fSOliver Tappe *r++ = c;
345*aef5731fSOliver Tappe }
346*aef5731fSOliver Tappe *r++ = '-';
347*aef5731fSOliver Tappe /* conv->ostate = 0; will be done by the caller */
348*aef5731fSOliver Tappe return count;
349*aef5731fSOliver Tappe } else
350*aef5731fSOliver Tappe return 0;
351*aef5731fSOliver Tappe }
352