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