1 /* Conversion module for UTF-7.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Bruno Haible <haible@clisp.cons.org>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 /* UTF-7 is a legacy encoding used for transmitting Unicode within the
21 ASCII character set, used primarily by mail agents. New programs
22 are encouraged to use UTF-8 instead.
24 UTF-7 is specified in RFC 2152 (and old RFC 1641, RFC 1642). The
25 original Base64 encoding is defined in RFC 2045. */
33 /* Define this to 1 if you want the so-called "optional direct" characters
34 ! " # $ % & * ; < = > @ [ ] ^ _ ` { | }
35 to be encoded. Define to 0 if you want them to be passed straight
36 through, like the so-called "direct" characters.
37 We set this to 1 because it's safer.
39 #define UTF7_ENCODE_OPTIONAL_CHARS 1
42 /* The set of "direct characters":
43 A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr
46 static const unsigned char direct_tab
[128 / 8] =
48 0x00, 0x26, 0x00, 0x00, 0x81, 0xf3, 0xff, 0x87,
49 0xfe, 0xff, 0xff, 0x07, 0xfe, 0xff, 0xff, 0x07
53 isdirect (uint32_t ch
)
55 return (ch
< 128 && ((direct_tab
[ch
>> 3] >> (ch
& 7)) & 1));
59 /* The set of "direct and optional direct characters":
60 A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr
61 ! " # $ % & * ; < = > @ [ ] ^ _ ` { | }
64 static const unsigned char xdirect_tab
[128 / 8] =
66 0x00, 0x26, 0x00, 0x00, 0xff, 0xf7, 0xff, 0xff,
67 0xff, 0xff, 0xff, 0xef, 0xff, 0xff, 0xff, 0x3f
71 isxdirect (uint32_t ch
)
73 return (ch
< 128 && ((xdirect_tab
[ch
>> 3] >> (ch
& 7)) & 1));
77 /* The set of "extended base64 characters":
81 static const unsigned char xbase64_tab
[128 / 8] =
83 0x00, 0x00, 0x00, 0x00, 0x00, 0xa8, 0xff, 0x03,
84 0xfe, 0xff, 0xff, 0x07, 0xfe, 0xff, 0xff, 0x07
88 isxbase64 (uint32_t ch
)
90 return (ch
< 128 && ((xbase64_tab
[ch
>> 3] >> (ch
& 7)) & 1));
94 /* Converts a value in the range 0..63 to a base64 encoded char. */
96 base64 (unsigned int i
)
113 /* Definitions used in the body of the `gconv' function. */
114 #define CHARSET_NAME "UTF-7//"
115 #define DEFINE_INIT 1
116 #define DEFINE_FINI 1
117 #define FROM_LOOP from_utf7_loop
118 #define TO_LOOP to_utf7_loop
119 #define MIN_NEEDED_FROM 1
120 #define MAX_NEEDED_FROM 6
121 #define MIN_NEEDED_TO 4
122 #define MAX_NEEDED_TO 4
123 #define ONE_DIRECTION 0
124 #define PREPARE_LOOP \
125 mbstate_t saved_state; \
126 mbstate_t *statep = data->__statep;
127 #define EXTRA_LOOP_ARGS , statep
130 /* Since we might have to reset input pointer we must be able to save
131 and restore the state. */
132 #define SAVE_RESET_STATE(Save) \
134 saved_state = *statep; \
136 *statep = saved_state
139 /* First define the conversion function from UTF-7 to UCS4.
140 The state is structured as follows:
141 __count bit 2..0: zero
142 __count bit 8..3: shift
146 0 -- not inside base64 encoding
147 1..32 XX..XX00..00 inside base64, (32 - shift) bits pending
148 This state layout is simpler than relying on STORE_REST/UNPACK_BYTES.
150 When shift = 0, __wch needs to store at most one lookahead byte (see
151 __GCONV_INCOMPLETE_INPUT below).
153 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
154 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
155 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
156 #define MAX_NEEDED_OUTPUT MAX_NEEDED_TO
157 #define LOOPFCT FROM_LOOP
160 uint_fast8_t ch = *inptr; \
162 if ((statep->__count >> 3) == 0) \
164 /* base64 encoding inactive. */ \
165 if (isxdirect (ch)) \
168 put32 (outptr, ch); \
171 else if (__glibc_likely (ch == '+')) \
173 if (__glibc_unlikely (inptr + 2 > inend)) \
175 /* Not enough input available. */ \
176 result = __GCONV_INCOMPLETE_INPUT; \
179 if (inptr[1] == '-') \
182 put32 (outptr, ch); \
187 /* Switch into base64 mode. */ \
189 statep->__count = (32 << 3); \
190 statep->__value.__wch = 0; \
195 /* The input is invalid. */ \
196 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
201 /* base64 encoding active. */ \
205 if (ch >= 'A' && ch <= 'Z') \
207 else if (ch >= 'a' && ch <= 'z') \
209 else if (ch >= '0' && ch <= '9') \
211 else if (ch == '+') \
213 else if (ch == '/') \
217 /* Terminate base64 encoding. */ \
219 /* If accumulated data is nonzero, the input is invalid. */ \
220 /* Also, partial UTF-16 characters are invalid. */ \
221 if (__builtin_expect (statep->__value.__wch != 0, 0) \
222 || __builtin_expect ((statep->__count >> 3) <= 26, 0)) \
224 STANDARD_FROM_LOOP_ERR_HANDLER ((statep->__count = 0, 1)); \
230 statep->__count = 0; \
234 /* Concatenate the base64 integer i to the accumulator. */ \
235 shift = (statep->__count >> 3); \
241 wch = statep->__value.__wch | (i << shift); \
243 if (shift <= 16 && shift > 10) \
245 /* An UTF-16 character has just been completed. */ \
246 uint32_t wc1 = wch >> 16; \
248 /* UTF-16: When we see a High Surrogate, we must also decode \
249 the following Low Surrogate. */ \
250 if (!(wc1 >= 0xd800 && wc1 < 0xdc00)) \
254 put32 (outptr, wc1); \
258 else if (shift <= 10 && shift > 4) \
260 /* After a High Surrogate, verify that the next 16 bit \
261 indeed form a Low Surrogate. */ \
262 uint32_t wc2 = wch & 0xffff; \
264 if (! __builtin_expect (wc2 >= 0xdc00 && wc2 < 0xe000, 1)) \
266 STANDARD_FROM_LOOP_ERR_HANDLER ((statep->__count = 0, 1));\
270 statep->__value.__wch = wch; \
274 /* An UTF-16 surrogate pair has just been completed. */ \
275 uint32_t wc1 = (uint32_t) statep->__value.__wch >> 16; \
276 uint32_t wc2 = ((uint32_t) statep->__value.__wch & 0xffff) \
277 | (i >> (6 - shift)); \
279 statep->__value.__wch = (i << shift) << 26; \
282 assert (wc1 >= 0xd800 && wc1 < 0xdc00); \
283 assert (wc2 >= 0xdc00 && wc2 < 0xe000); \
285 0x10000 + ((wc1 - 0xd800) << 10) + (wc2 - 0xdc00)); \
289 statep->__count = shift << 3; \
291 /* Now that we digested the input increment the input pointer. */ \
295 #define LOOP_NEED_FLAGS
296 #define EXTRA_LOOP_DECLS , mbstate_t *statep
297 #include <iconv/loop.c>
300 /* Next, define the conversion from UCS4 to UTF-7.
301 The state is structured as follows:
302 __count bit 2..0: zero
303 __count bit 4..3: shift
304 __count bit 8..5: data
307 0 0 not inside base64 encoding
308 1 0 inside base64, no pending bits
309 2 XX00 inside base64, 2 bits known for next byte
310 3 XXXX inside base64, 4 bits known for next byte
312 __count bit 2..0 and __wch are always zero, because this direction
313 never returns __GCONV_INCOMPLETE_INPUT.
315 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
316 #define MAX_NEEDED_INPUT MAX_NEEDED_TO
317 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
318 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
319 #define LOOPFCT TO_LOOP
322 uint32_t ch = get32 (inptr); \
324 if ((statep->__count & 0x18) == 0) \
326 /* base64 encoding inactive */ \
327 if (UTF7_ENCODE_OPTIONAL_CHARS ? isdirect (ch) : isxdirect (ch)) \
329 *outptr++ = (unsigned char) ch; \
337 else if (ch < 0x10000) \
339 else if (ch < 0x110000) \
342 STANDARD_TO_LOOP_ERR_HANDLER (4); \
344 if (__glibc_unlikely (outptr + count > outend)) \
346 result = __GCONV_FULL_OUTPUT; \
353 else if (ch < 0x10000) \
355 *outptr++ = base64 (ch >> 10); \
356 *outptr++ = base64 ((ch >> 4) & 0x3f); \
357 statep->__count = ((ch & 15) << 5) | (3 << 3); \
359 else if (ch < 0x110000) \
361 uint32_t ch1 = 0xd800 + ((ch - 0x10000) >> 10); \
362 uint32_t ch2 = 0xdc00 + ((ch - 0x10000) & 0x3ff); \
364 ch = (ch1 << 16) | ch2; \
365 *outptr++ = base64 (ch >> 26); \
366 *outptr++ = base64 ((ch >> 20) & 0x3f); \
367 *outptr++ = base64 ((ch >> 14) & 0x3f); \
368 *outptr++ = base64 ((ch >> 8) & 0x3f); \
369 *outptr++ = base64 ((ch >> 2) & 0x3f); \
370 statep->__count = ((ch & 3) << 7) | (2 << 3); \
378 /* base64 encoding active */ \
379 if (UTF7_ENCODE_OPTIONAL_CHARS ? isdirect (ch) : isxdirect (ch)) \
381 /* deactivate base64 encoding */ \
384 count = ((statep->__count & 0x18) >= 0x10) + isxbase64 (ch) + 1; \
385 if (__glibc_unlikely (outptr + count > outend)) \
387 result = __GCONV_FULL_OUTPUT; \
391 if ((statep->__count & 0x18) >= 0x10) \
392 *outptr++ = base64 ((statep->__count >> 3) & ~3); \
393 if (isxbase64 (ch)) \
395 *outptr++ = (unsigned char) ch; \
396 statep->__count = 0; \
403 count = ((statep->__count & 0x18) >= 0x10 ? 3 : 2); \
404 else if (ch < 0x110000) \
405 count = ((statep->__count & 0x18) >= 0x18 ? 6 : 5); \
407 STANDARD_TO_LOOP_ERR_HANDLER (4); \
409 if (__glibc_unlikely (outptr + count > outend)) \
411 result = __GCONV_FULL_OUTPUT; \
417 switch ((statep->__count >> 3) & 3) \
420 *outptr++ = base64 (ch >> 10); \
421 *outptr++ = base64 ((ch >> 4) & 0x3f); \
422 statep->__count = ((ch & 15) << 5) | (3 << 3); \
426 base64 (((statep->__count >> 3) & ~3) | (ch >> 12)); \
427 *outptr++ = base64 ((ch >> 6) & 0x3f); \
428 *outptr++ = base64 (ch & 0x3f); \
429 statep->__count = (1 << 3); \
433 base64 (((statep->__count >> 3) & ~3) | (ch >> 14)); \
434 *outptr++ = base64 ((ch >> 8) & 0x3f); \
435 *outptr++ = base64 ((ch >> 2) & 0x3f); \
436 statep->__count = ((ch & 3) << 7) | (2 << 3); \
442 else if (ch < 0x110000) \
444 uint32_t ch1 = 0xd800 + ((ch - 0x10000) >> 10); \
445 uint32_t ch2 = 0xdc00 + ((ch - 0x10000) & 0x3ff); \
447 ch = (ch1 << 16) | ch2; \
448 switch ((statep->__count >> 3) & 3) \
451 *outptr++ = base64 (ch >> 26); \
452 *outptr++ = base64 ((ch >> 20) & 0x3f); \
453 *outptr++ = base64 ((ch >> 14) & 0x3f); \
454 *outptr++ = base64 ((ch >> 8) & 0x3f); \
455 *outptr++ = base64 ((ch >> 2) & 0x3f); \
456 statep->__count = ((ch & 3) << 7) | (2 << 3); \
460 base64 (((statep->__count >> 3) & ~3) | (ch >> 28)); \
461 *outptr++ = base64 ((ch >> 22) & 0x3f); \
462 *outptr++ = base64 ((ch >> 16) & 0x3f); \
463 *outptr++ = base64 ((ch >> 10) & 0x3f); \
464 *outptr++ = base64 ((ch >> 4) & 0x3f); \
465 statep->__count = ((ch & 15) << 5) | (3 << 3); \
469 base64 (((statep->__count >> 3) & ~3) | (ch >> 30)); \
470 *outptr++ = base64 ((ch >> 24) & 0x3f); \
471 *outptr++ = base64 ((ch >> 18) & 0x3f); \
472 *outptr++ = base64 ((ch >> 12) & 0x3f); \
473 *outptr++ = base64 ((ch >> 6) & 0x3f); \
474 *outptr++ = base64 (ch & 0x3f); \
475 statep->__count = (1 << 3); \
486 /* Now that we wrote the output increment the input pointer. */ \
489 #define LOOP_NEED_FLAGS
490 #define EXTRA_LOOP_DECLS , mbstate_t *statep
491 #include <iconv/loop.c>
494 /* Since this is a stateful encoding we have to provide code which resets
495 the output state to the initial state. This has to be done during the
497 #define EMIT_SHIFT_TO_INIT \
498 if (FROM_DIRECTION) \
499 /* Nothing to emit. */ \
500 memset (data->__statep, '\0', sizeof (mbstate_t)); \
503 /* The "to UTF-7" direction. Flush the remaining bits and terminate \
504 with a '-' byte. This will guarantee correct decoding if more \
505 UTF-7 encoded text is added afterwards. */ \
506 int state = data->__statep->__count; \
510 /* Deactivate base64 encoding. */ \
511 size_t count = ((state & 0x18) >= 0x10) + 1; \
513 if (__glibc_unlikely (outbuf + count > outend)) \
514 /* We don't have enough room in the output buffer. */ \
515 status = __GCONV_FULL_OUTPUT; \
518 /* Write out the shift sequence. */ \
519 if ((state & 0x18) >= 0x10) \
520 *outbuf++ = base64 ((state >> 3) & ~3); \
523 data->__statep->__count = 0; \
527 data->__statep->__count = 0; \
531 /* Now define the toplevel functions. */
532 #include <iconv/skeleton.c>