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