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