2 * base64.c -- base-64 conversion routines.
4 * For license terms, see the file COPYING in this directory.
6 * This base 64 encoding is defined in RFC2045 section 6.8,
7 * "Base64 Content-Transfer-Encoding", but lines must not be broken in the
10 * Modified by Hiroyuki Yamamoto <hiro-y@kcn.ne.jp>
21 static const char base64digits[] =
22 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
25 static const char base64val[] = {
26 BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
27 BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
28 BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63,
29 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,BAD,BAD, BAD,BAD,BAD,BAD,
30 BAD, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
31 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,BAD, BAD,BAD,BAD,BAD,
32 BAD, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
33 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,BAD, BAD,BAD,BAD,BAD
35 #define DECODE64(c) (isascii(c) ? base64val[c] : BAD)
37 void to64frombits(unsigned char *out, const unsigned char *in, int inlen)
38 /* raw bytes in quasi-big-endian order to base 64 string (NUL-terminated) */
40 for (; inlen >= 3; inlen -= 3)
42 *out++ = base64digits[in[0] >> 2];
43 *out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)];
44 *out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
45 *out++ = base64digits[in[2] & 0x3f];
50 unsigned char fragment;
52 *out++ = base64digits[in[0] >> 2];
53 fragment = (in[0] << 4) & 0x30;
55 fragment |= in[1] >> 4;
56 *out++ = base64digits[fragment];
57 *out++ = (inlen < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c];
63 int from64tobits(char *out, const char *in)
64 /* base 64 to raw bytes in quasi-big-endian order, returning count of bytes */
67 register unsigned char digit1, digit2, digit3, digit4;
69 if (in[0] == '+' && in[1] == ' ')
71 if (*in == '\r' || *in == '\n')
76 if (DECODE64(digit1) == BAD)
79 if (DECODE64(digit2) == BAD)
82 if (digit3 != '=' && DECODE64(digit3) == BAD)
85 if (digit4 != '=' && DECODE64(digit4) == BAD)
88 *out++ = (DECODE64(digit1) << 2) | ((DECODE64(digit2) >> 4) & 0x03);
92 *out++ = ((DECODE64(digit2) << 4) & 0xf0) | (DECODE64(digit3) >> 2);
96 *out++ = ((DECODE64(digit3) << 6) & 0xc0) | DECODE64(digit4);
100 } while (*in && *in != '\r' && *in != '\n' && digit4 != '=');
105 struct _Base64Decoder
108 unsigned char buf[4];
112 base64_decoder_new (void)
114 Base64Decoder *decoder;
116 decoder = g_new0 (Base64Decoder, 1);
121 base64_decoder_free (Base64Decoder *decoder)
127 base64_decoder_decode (Base64Decoder *decoder,
133 unsigned char buf[4];
135 g_return_val_if_fail (decoder != NULL, -1);
136 g_return_val_if_fail (in != NULL, -1);
137 g_return_val_if_fail (out != NULL, -1);
139 buf_len = decoder->buf_len;
140 memcpy (buf, decoder->buf, sizeof(buf));
142 while (buf_len < 4) {
143 int c = *(unsigned char *)in++;
144 if (c == '\0') break;
145 if (c == '\r' || c == '\n') continue;
146 if (c != '=' && DECODE64(c) == BAD)
150 if (buf_len < 4 || buf[0] == '=' || buf[1] == '=') {
151 decoder->buf_len = buf_len;
152 memcpy (decoder->buf, buf, sizeof(buf));
155 *out++ = ((DECODE64(buf[0]) << 2)
156 | ((DECODE64(buf[1]) >> 4) & 0x03));
159 *out++ = (((DECODE64(buf[1]) << 4) & 0xf0)
160 | (DECODE64(buf[2]) >> 2));
163 *out++ = (((DECODE64(buf[2]) << 6) & 0xc0)
169 if (buf[2] == '=' || buf[3] == '=') {
170 decoder->buf_len = 0;
176 /* base64.c ends here */