allow gpgme in non-standard locations
[claws.git] / src / base64.c
blobf0ef2061cd290aa664c3ad264f0654751090b61a
1 /*
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
8 * scheme used here.
10 * Modified by Hiroyuki Yamamoto <hiro-y@kcn.ne.jp>
13 #include "defs.h"
15 #include <ctype.h>
16 #include <string.h>
17 #include <glib.h>
19 #include "base64.h"
21 static const char base64digits[] =
22 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
24 #define BAD -1
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];
46 in += 3;
48 if (inlen > 0)
50 unsigned char fragment;
52 *out++ = base64digits[in[0] >> 2];
53 fragment = (in[0] << 4) & 0x30;
54 if (inlen > 1)
55 fragment |= in[1] >> 4;
56 *out++ = base64digits[fragment];
57 *out++ = (inlen < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c];
58 *out++ = '=';
60 *out = '\0';
63 int from64tobits(char *out, const char *in)
64 /* base 64 to raw bytes in quasi-big-endian order, returning count of bytes */
66 int len = 0;
67 register unsigned char digit1, digit2, digit3, digit4;
69 if (in[0] == '+' && in[1] == ' ')
70 in += 2;
71 if (*in == '\r' || *in == '\n')
72 return 0;
74 do {
75 digit1 = in[0];
76 if (DECODE64(digit1) == BAD)
77 return -1;
78 digit2 = in[1];
79 if (DECODE64(digit2) == BAD)
80 return -1;
81 digit3 = in[2];
82 if (digit3 != '=' && DECODE64(digit3) == BAD)
83 return -1;
84 digit4 = in[3];
85 if (digit4 != '=' && DECODE64(digit4) == BAD)
86 return -1;
87 in += 4;
88 *out++ = (DECODE64(digit1) << 2) | ((DECODE64(digit2) >> 4) & 0x03);
89 ++len;
90 if (digit3 != '=')
92 *out++ = ((DECODE64(digit2) << 4) & 0xf0) | (DECODE64(digit3) >> 2);
93 ++len;
94 if (digit4 != '=')
96 *out++ = ((DECODE64(digit3) << 6) & 0xc0) | DECODE64(digit4);
97 ++len;
100 } while (*in && *in != '\r' && *in != '\n' && digit4 != '=');
102 return len;
105 struct _Base64Decoder
107 int buf_len;
108 unsigned char buf[4];
111 Base64Decoder *
112 base64_decoder_new (void)
114 Base64Decoder *decoder;
116 decoder = g_new0 (Base64Decoder, 1);
117 return decoder;
120 void
121 base64_decoder_free (Base64Decoder *decoder)
123 g_free (decoder);
127 base64_decoder_decode (Base64Decoder *decoder,
128 const char *in,
129 char *out)
131 int len = 0;
132 int buf_len;
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));
141 while (1) {
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)
147 return -1;
148 buf[buf_len++] = c;
150 if (buf_len < 4 || buf[0] == '=' || buf[1] == '=') {
151 decoder->buf_len = buf_len;
152 memcpy (decoder->buf, buf, sizeof(buf));
153 return len;
155 *out++ = ((DECODE64(buf[0]) << 2)
156 | ((DECODE64(buf[1]) >> 4) & 0x03));
157 ++len;
158 if (buf[2] != '=') {
159 *out++ = (((DECODE64(buf[1]) << 4) & 0xf0)
160 | (DECODE64(buf[2]) >> 2));
161 ++len;
162 if (buf[3] != '=') {
163 *out++ = (((DECODE64(buf[2]) << 6) & 0xc0)
164 | DECODE64(buf[3]));
165 ++len;
168 buf_len = 0;
169 if (buf[2] == '=' || buf[3] == '=') {
170 decoder->buf_len = 0;
171 return len;
176 /* base64.c ends here */