2 * Copyright (C) 1999-2001, 2008, 2016 Free Software Foundation, Inc.
3 * This file is part of the GNU LIBICONV Library.
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.
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.
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 <http://www.gnu.org/licenses/>.
24 /* Specification: RFC 2152 (and old RFC 1641, RFC 1642) */
25 /* The original Base64 encoding is defined in RFC 2045. */
27 /* Set of direct characters:
28 * A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr
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,
34 #define isdirect(ch) ((ch) < 128 && ((direct_tab[(ch)>>3] >> (ch & 7)) & 1))
36 /* Set of direct and optional direct characters:
37 * A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr
38 * ! " # $ % & * ; < = > @ [ ] ^ _ ` { | }
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,
44 #define isxdirect(ch) ((ch) < 128 && ((xdirect_tab[(ch)>>3] >> (ch & 7)) & 1))
46 /* Set of base64 characters, extended:
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,
53 #define isxbase64(ch) ((ch) < 128 && ((xbase64_tab[(ch)>>3] >> (ch & 7)) & 1))
56 * The state is structured as follows:
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
68 utf7_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
70 state_t state = conv->istate;
71 int count = 0; /* number of input bytes already read */
79 /* Here (state & 3) == 0 */
107 /* base64 encoding active */
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 */
114 unsigned char c = *s;
116 if (c >= 'A' && c <= 'Z')
118 else if (c >= 'a' && c <= 'z')
120 else if (c >= '0' && c <= '9')
127 /* c terminates base64 encoding */
128 if (base64state & -4)
129 goto ilseq; /* data must be 0, otherwise illegal */
131 goto ilseq; /* partial UTF-16 characters are invalid */
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;
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))
161 if (n < count+base64count+1)
164 /* Here k = kmax > 0, hence base64count > 0. */
165 if ((base64state & 3) == 0) abort();
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);
175 conv->istate = base64state;
176 return count+base64count;
180 conv->istate = state;
181 return RET_TOOFEW(count);
184 conv->istate = state;
185 return RET_SHIFT_ILSEQ(count);
189 * The state is structured as follows:
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
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.
206 #define UTF7_ENCODE_OPTIONAL_CHARS 1
209 utf7_wctomb (conv_t conv, unsigned char *r, ucs4_t iwc, size_t n)
211 state_t state = conv->ostate;
212 unsigned int wc = iwc;
219 if (UTF7_ENCODE_OPTIONAL_CHARS ? isdirect(wc) : isxdirect(wc)) {
220 r[0] = (unsigned char) wc;
221 /*conv->ostate = state;*/
229 /*conv->ostate = state;*/
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;
246 if ((state & 3) >= 2) {
247 unsigned int i = state & -4;
266 *r++ = (unsigned char) wc;
267 conv->ostate = state;
270 unsigned int k; /* number of payload bytes to write */
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;
279 count += ((state & 3) >= 3 ? 6 : 5);
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 */
314 if ((state & 3) && (k == 0))
317 conv->ostate = state;
324 utf7_reset (conv_t conv, unsigned char *r, size_t n)
326 state_t state = conv->ostate;
328 /* deactivate base64 encoding */
329 unsigned int count = ((state & 3) >= 2 ? 1 : 0) + 1;
332 if ((state & 3) >= 2) {
333 unsigned int i = state & -4;
350 /* conv->ostate = 0; will be done by the caller */