2 * Copyright (C) 1999-2001, 2008 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, write to the Free Software Foundation, Inc., 51 Franklin Street,
18 * Fifth Floor, Boston, MA 02110-1301, USA.
25 /* Specification: RFC 2152 (and old RFC 1641, RFC 1642) */
26 /* The original Base64 encoding is defined in RFC 2045. */
28 /* Set of direct characters:
29 * A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr
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,
35 #define isdirect(ch) ((ch) < 128 && ((direct_tab[(ch)>>3] >> (ch & 7)) & 1))
37 /* Set of direct and optional direct characters:
38 * A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr
39 * ! " # $ % & * ; < = > @ [ ] ^ _ ` { | }
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,
45 #define isxdirect(ch) ((ch) < 128 && ((xdirect_tab[(ch)>>3] >> (ch & 7)) & 1))
47 /* Set of base64 characters, extended:
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,
54 #define isxbase64(ch) ((ch) < 128 && ((xbase64_tab[(ch)>>3] >> (ch & 7)) & 1))
57 * The state is structured as follows:
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
69 utf7_mbtowc (conv_t conv
, ucs4_t
*pwc
, const unsigned char *s
, int n
)
71 state_t state
= conv
->istate
;
72 int count
= 0; /* number of input bytes already read */
80 /* Here (state & 3) == 0 */
108 /* base64 encoding active */
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 */
115 unsigned char c
= *s
;
117 if (c
>= 'A' && c
<= 'Z')
119 else if (c
>= 'a' && c
<= 'z')
121 else if (c
>= '0' && c
<= '9')
128 /* c terminates base64 encoding */
129 if (base64state
& -4)
130 goto ilseq
; /* data must be 0, otherwise illegal */
132 goto ilseq
; /* partial UTF-16 characters are invalid */
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;
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))
162 if (n
< count
+base64count
+1)
165 /* Here k = kmax > 0, hence base64count > 0. */
166 if ((base64state
& 3) == 0) abort();
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);
176 conv
->istate
= base64state
;
177 return count
+base64count
;
181 conv
->istate
= state
;
182 return RET_TOOFEW(count
);
185 conv
->istate
= state
;
186 return RET_SHIFT_ILSEQ(count
);
190 * The state is structured as follows:
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
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.
207 #define UTF7_ENCODE_OPTIONAL_CHARS 1
210 utf7_wctomb (conv_t conv
, unsigned char *r
, ucs4_t iwc
, int n
)
212 state_t state
= conv
->ostate
;
213 unsigned int wc
= iwc
;
220 if (UTF7_ENCODE_OPTIONAL_CHARS
? isdirect(wc
) : isxdirect(wc
)) {
221 r
[0] = (unsigned char) wc
;
222 /*conv->ostate = state;*/
230 /*conv->ostate = state;*/
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;
247 if ((state
& 3) >= 2) {
248 unsigned int i
= state
& -4;
267 *r
++ = (unsigned char) wc
;
268 conv
->ostate
= state
;
271 unsigned int k
; /* number of payload bytes to write */
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
;
280 count
+= ((state
& 3) >= 3 ? 6 : 5);
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 */
315 if ((state
& 3) && (k
== 0))
318 conv
->ostate
= state
;
325 utf7_reset (conv_t conv
, unsigned char *r
, int n
)
327 state_t state
= conv
->ostate
;
329 /* deactivate base64 encoding */
330 unsigned int count
= ((state
& 3) >= 2 ? 1 : 0) + 1;
333 if ((state
& 3) >= 2) {
334 unsigned int i
= state
& -4;
351 /* conv->ostate = 0; will be done by the caller */