Copyright updates:
[exim.git] / src / src / base64.c
CommitLineData
f4d091fb
JH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
5/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004, 2015 */
6/* License: GPL */
7
f9ba5e22 8/* Copyright (c) University of Cambridge 1995 - 2018 */
1e1ddfac 9/* Copyright (c) The Exim Maintainers 2020 */
f4d091fb
JH
10/* See the file NOTICE for conditions of use and distribution. */
11
12
13#include "exim.h"
14#ifdef WITH_CONTENT_SCAN /* file-IO specific decode function */
15# include "mime.h"
16
17/* BASE64 decoder matrix */
18static unsigned char mime_b64[256]={
19/* 0 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
20/* 16 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
21/* 32 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 62, 128, 128, 128, 63,
22/* 48 */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 255, 128, 128,
23/* 64 */ 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
24/* 80 */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 128, 128, 128, 128, 128,
25/* 96 */ 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
26/* 112 */ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128,
27/* 128 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
28/* 144 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
29/* 160 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
30/* 176 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
31/* 192 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
32/* 208 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
33/* 224 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
34/* 240 */ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128
35};
36
37/* decode base64 MIME part */
38ssize_t
39mime_decode_base64(FILE * in, FILE * out, uschar * boundary)
40{
41uschar ibuf[MIME_MAX_LINE_LENGTH], obuf[MIME_MAX_LINE_LENGTH];
d7978c0f 42uschar *opos;
f4d091fb
JH
43ssize_t len, size = 0;
44int bytestate = 0;
45
46opos = obuf;
47
48while (Ufgets(ibuf, MIME_MAX_LINE_LENGTH, in) != NULL)
49 {
50 if (boundary != NULL
51 && Ustrncmp(ibuf, "--", 2) == 0
52 && Ustrncmp((ibuf+2), boundary, Ustrlen(boundary)) == 0
53 )
54 break;
55
d7978c0f 56 for (uschar * ipos = ibuf ; *ipos != '\r' && *ipos != '\n' && *ipos; ++ipos)
f4d091fb
JH
57 if (*ipos == '=') /* skip padding */
58 ++bytestate;
59
60 else if (mime_b64[*ipos] == 128) /* skip bad characters */
61 mime_set_anomaly(MIME_ANOMALY_BROKEN_BASE64);
62
63 /* simple state-machine */
64 else switch((bytestate++) & 3)
65 {
66 case 0:
67 *opos = mime_b64[*ipos] << 2; break;
68 case 1:
69 *opos++ |= mime_b64[*ipos] >> 4;
70 *opos = mime_b64[*ipos] << 4; break;
71 case 2:
72 *opos++ |= mime_b64[*ipos] >> 2;
73 *opos = mime_b64[*ipos] << 6; break;
74 case 3:
75 *opos++ |= mime_b64[*ipos]; break;
76 }
77
78 /* something to write? */
79 len = opos - obuf;
80 if (len > 0)
81 {
82 if (fwrite(obuf, 1, len, out) != len) return -1; /* error */
83 size += len;
84 /* copy incomplete last byte to start of obuf, where we continue */
85 if ((bytestate & 3) != 0)
86 *obuf = *opos;
87 opos = obuf;
88 }
89 } /* while */
90
91/* write out last byte if it was incomplete */
92if (bytestate & 3)
93 {
94 if (fwrite(obuf, 1, 1, out) != 1) return -1;
95 ++size;
96 }
97
98return size;
99}
100
101#endif /*WITH_CONTENT_SCAN*/
102
103/*************************************************
3c51463e
JH
104 *************************************************
105 *************************************************
106 *************************************************
107 *************************************************
108 *************************************************
109 *************************************************
110 *************************************************
111 *************************************************
112 *************************************************
113 *************************************************
114 *************************************************
115 *************************************************
116 *************************************************
117 *************************************************
118 *************************************************/
f4d091fb
JH
119
120
121/*************************************************
122* Decode byte-string in base 64 *
123*************************************************/
124
125/* This function decodes a string in base 64 format as defined in RFC 2045
126(MIME) and required by the SMTP AUTH extension (RFC 2554). The decoding
127algorithm is written out in a straightforward way. Turning it into some kind of
128compact loop is messy and would probably run more slowly.
129
130Arguments:
131 code points to the coded string, zero-terminated
132 ptr where to put the pointer to the result, which is in
df3def24 133 allocated store, and zero-terminated
f4d091fb
JH
134
135Returns: the number of bytes in the result,
136 or -1 if the input was malformed
137
69a70afa 138Whitespace in the input is ignored.
f4d091fb
JH
139A zero is added on to the end to make it easy in cases where the result is to
140be interpreted as text. This is not included in the count. */
141
142static uschar dec64table[] = {
143 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0-15 */
144 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 16-31 */
145 255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63, /* 32-47 */
146 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,255,255,255, /* 48-63 */
147 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 64-79 */
148 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255, /* 80-95 */
149 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 96-111 */
150 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255 /* 112-127*/
151};
152
153int
35cf75e9 154b64decode(const uschar *code, uschar **ptr)
f4d091fb 155{
cf3cd306 156
f4d091fb 157int x, y;
cf3cd306 158uschar *result;
f4d091fb 159
cf3cd306
HSHR
160{
161 int l = Ustrlen(code);
f3ebb786 162 *ptr = result = store_get(1 + l/4 * 3 + l%4, is_tainted(code));
cf3cd306 163}
f4d091fb
JH
164
165/* Each cycle of the loop handles a quantum of 4 input bytes. For the last
166quantum this may decode to 1, 2, or 3 output bytes. */
167
168while ((x = *code++) != 0)
169 {
69a70afa 170 if (isspace(x)) continue;
df3def24 171 /* debug_printf("b64d: '%c'\n", x); */
69a70afa 172
f4d091fb 173 if (x > 127 || (x = dec64table[x]) == 255) return -1;
69a70afa
JH
174
175 while (isspace(y = *code++)) ;
df3def24 176 /* debug_printf("b64d: '%c'\n", y); */
889d293b 177 if (y > 127 || (y = dec64table[y]) == 255)
f4d091fb
JH
178 return -1;
179
180 *result++ = (x << 2) | (y >> 4);
df3def24 181 /* debug_printf("b64d: -> %02x\n", result[-1]); */
f4d091fb 182
69a70afa 183 while (isspace(x = *code++)) ;
df3def24
JH
184 /* debug_printf("b64d: '%c'\n", x); */
185 if (x == '=') /* endmarker, but there should be another */
f4d091fb 186 {
69a70afa 187 while (isspace(x = *code++)) ;
df3def24
JH
188 /* debug_printf("b64d: '%c'\n", x); */
189 if (x != '=') return -1;
190 while (isspace(y = *code++)) ;
191 if (y != 0) return -1;
192 /* debug_printf("b64d: DONE\n"); */
193 break;
f4d091fb
JH
194 }
195 else
196 {
197 if (x > 127 || (x = dec64table[x]) == 255) return -1;
198 *result++ = (y << 4) | (x >> 2);
df3def24 199 /* debug_printf("b64d: -> %02x\n", result[-1]); */
69a70afa
JH
200
201 while (isspace(y = *code++)) ;
df3def24 202 /* debug_printf("b64d: '%c'\n", y); */
69a70afa 203 if (y == '=')
f4d091fb 204 {
df3def24
JH
205 while (isspace(y = *code++)) ;
206 if (y != 0) return -1;
207 /* debug_printf("b64d: DONE\n"); */
208 break;
f4d091fb
JH
209 }
210 else
211 {
212 if (y > 127 || (y = dec64table[y]) == 255) return -1;
213 *result++ = (x << 6) | y;
df3def24 214 /* debug_printf("b64d: -> %02x\n", result[-1]); */
f4d091fb
JH
215 }
216 }
217 }
218
219*result = 0;
220return result - *ptr;
221}
222
223
224/*************************************************
f4d091fb
JH
225* Encode byte-string in base 64 *
226*************************************************/
227
228/* This function encodes a string of bytes, containing any values whatsoever,
229in base 64 as defined in RFC 2045 (MIME) and required by the SMTP AUTH
230extension (RFC 2554). The encoding algorithm is written out in a
231straightforward way. Turning it into some kind of compact loop is messy and
232would probably run more slowly.
233
234Arguments:
235 clear points to the clear text bytes
236 len the number of bytes to encode
237
238Returns: a pointer to the zero-terminated base 64 string, which
239 is in working store
240*/
241
242static uschar *enc64table =
243 US"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
244
245uschar *
b1a32a3c 246b64encode_taint(const uschar * clear, int len, BOOL tainted)
f4d091fb 247{
b1a32a3c 248uschar *code = store_get(4*((len+2)/3) + 1, tainted);
f4d091fb
JH
249uschar *p = code;
250
251while (len-- >0)
252 {
286b9d5f 253 int x, y;
f4d091fb
JH
254
255 x = *clear++;
256 *p++ = enc64table[(x >> 2) & 63];
257
258 if (len-- <= 0)
259 {
260 *p++ = enc64table[(x << 4) & 63];
261 *p++ = '=';
262 *p++ = '=';
263 break;
264 }
265
266 y = *clear++;
267 *p++ = enc64table[((x << 4) | ((y >> 4) & 15)) & 63];
268
269 if (len-- <= 0)
270 {
271 *p++ = enc64table[(y << 2) & 63];
272 *p++ = '=';
273 break;
274 }
275
276 x = *clear++;
277 *p++ = enc64table[((y << 2) | ((x >> 6) & 3)) & 63];
278
279 *p++ = enc64table[x & 63];
280 }
281
282*p = 0;
283
284return code;
285}
286
b1a32a3c
JH
287uschar *
288b64encode(const uschar * clear, int len)
289{
290return b64encode_taint(clear, len, is_tainted(clear));
291}
292
f4d091fb
JH
293
294/* End of base64.c */
295/* vi: sw ai sw=2
296*/