send.c: forgot to commit one more longjmp(2) globber fix
[s-mailx.git] / base64.c
blob43131f6f11ae87631ca2686570b0affd2b696422
1 /*
2 * S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
6 */
7 /*
8 * These base64 routines are derived from the metamail-2.7 sources which
9 * state the following copyright notice:
11 * Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
13 * Permission to use, copy, modify, and distribute this material
14 * for any purpose and without fee is hereby granted, provided
15 * that the above copyright notice and this permission notice
16 * appear in all copies, and that the name of Bellcore not be
17 * used in advertising or publicity pertaining to this
18 * material without the specific, prior written permission
19 * of an authorized representative of Bellcore. BELLCORE
20 * MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
21 * OF THIS MATERIAL FOR ANY PURPOSE. IT IS PROVIDED "AS IS",
22 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
26 * Mail -- a mail program
28 * base64 functions
31 #include "rcv.h"
32 #include "extern.h"
34 static const char b64table[] =
35 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
36 static const signed char b64index[] = {
37 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
38 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
39 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
40 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
41 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
42 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
43 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
44 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
47 #define char64(c) ((c) < 0 ? -1 : b64index[(int)(c)])
49 static signed char *ctob64(unsigned char *p, int pad);
52 * Convert three characters to base64.
54 static signed char *
55 ctob64(unsigned char *p, int pad)
57 static signed char b64[4];
59 b64[0] = b64table[p[0] >> 2];
60 b64[1] = b64table[((p[0] & 0x3) << 4) | ((p[1] & 0xF0) >> 4)];
61 if (pad == 2) {
62 b64[1] = b64table[(p[0] & 0x3) << 4];
63 b64[2] = b64[3] = '=';
64 } else if (pad == 1) {
65 b64[2] = b64table[((p[1] & 0xF) << 2)];
66 b64[3] = '=';
67 } else {
68 b64[2] = b64table[((p[1] & 0xF) << 2) | ((p[2] & 0xC0) >> 6)];
69 b64[3] = b64table[p[2] & 0x3F];
71 return b64;
74 char *
75 strtob64(const char *p)
77 return memtob64(p, strlen(p));
80 char *
81 memtob64(const void *vp, size_t isz)
83 char q[3];
84 const char *p = vp;
85 signed char *h;
86 size_t c = 0;
87 int i, l = 0, sz = 0, pads;
88 char *rs = NULL;
90 if (isz == 0) {
91 rs = smalloc(1);
92 *rs = '\0';
93 return rs;
95 do {
96 for (pads = 2, i = 0; i <= 2; i++, pads--) {
97 q[i] = p[c++];
98 if (c == isz)
99 break;
101 h = ctob64((unsigned char *)q, pads);
102 if (l + 5 >= sz)
103 rs = srealloc(rs, sz = l + 100);
104 for (i = 0; i < 4; i++)
105 rs[l++] = h[i];
106 } while (c < isz);
107 rs[l] = '\0';
108 return rs;
112 * Write to a file converting to base64. The input must be aligned
113 * e.g. at 972 character bounds.
115 size_t
116 mime_write_tob64(struct str *in, FILE *fo, int is_header)
118 char *p, *upper, q[3];
119 signed char *h;
120 int i, l, pads;
121 size_t sz;
123 sz = 0;
124 upper = in->s + in->l;
125 for (p = in->s, l = 0; p < upper; ) {
126 for (pads = 2, i = 0; i <= 2; i++, pads--) {
127 q[i] = *p++;
128 if (p == upper)
129 break;
131 h = ctob64((unsigned char *)q, pads);
132 fwrite(h, sizeof(char), 4, fo);
133 sz += 4, l += 4;
134 if (l >= 71) {
135 putc('\n', fo), sz++;
136 l = 0;
139 if (l != 0 && !is_header) {
140 putc('\n', fo), sz++;
142 return sz;
146 * Decode from base64.
148 void
149 mime_fromb64(struct str *in, struct str *out, int is_text)
151 char *p, *q, *upper;
152 signed char c, d, e, f, g;
153 int done = 0, newline = 0;
155 out->s = smalloc(in->l * 3 / 4 + 2);
156 out->l = 0;
157 upper = in->s + in->l;
158 for (p = in->s, q = out->s; p < upper; ) {
159 while (c = *p++, whitechar(c));
160 if (p >= upper) break;
161 if (done) continue;
162 while (d = *p++, whitechar(d));
163 if (p >= upper) break;
164 while (e = *p++, whitechar(e));
165 if (p >= upper) break;
166 while (f = *p++, whitechar(f));
167 if (c == '=' || d == '=') {
168 done = 1;
169 continue;
171 c = char64(c);
172 d = char64(d);
173 g = ((c << 2) | ((d & 0x30) >> 4));
174 if (is_text) {
175 if (g == '\r') {
176 newline = 1;
177 } else if (g == '\n' && newline) {
178 q--;
179 out->l--;
180 newline = 0;
181 } else {
182 newline = 0;
185 *q++ = g;
186 out->l++;
187 if (e == '=') {
188 done = 1;
189 } else {
190 e = char64(e);
191 g = (((d & 0xF) << 4) | ((e & 0x3C) >> 2));
192 if (is_text) {
193 if (g == '\r') {
194 newline = 1;
195 } else if (g == '\n' && newline) {
196 q--;
197 out->l--;
198 newline = 0;
199 } else {
200 newline = 0;
203 *q++ = g;
204 out->l++;
205 if (f == '=') {
206 done = 1;
207 } else {
208 f = char64(f);
209 g = (((e & 0x03) << 6) | f);
210 if (is_text) {
211 if (g == '\r') {
212 newline = 1;
213 } else if (g == '\n' && newline) {
214 q--;
215 out->l--;
216 newline = 0;
217 } else {
218 newline = 0;
221 *q++ = g;
222 out->l++;
226 return;
230 * Buffer the base64 input so mime_fromb64 gets always multiples of
231 * 4 characters.
232 * As we have only one buffer, this function is not reentrant.
234 void
235 mime_fromb64_b(struct str *in, struct str *out, int is_text, FILE *f)
237 static signed char b[4];
238 static size_t n;
239 static FILE *f_b = (FILE *)-1;
240 signed char c;
241 size_t i;
242 struct str nin;
244 nin.s = smalloc(in->l + n);
245 if (n != 0 && f_b == f) {
246 for (nin.l = 0; nin.l < n; nin.l++)
247 nin.s[nin.l] = b[nin.l];
248 } else {
249 nin.l = 0;
250 n = 0;
253 for (i = 0; i <= in->l; i++) {
254 c = in->s[i];
255 if (char64(c) == -1 && c != '=')
256 continue;
257 b[n] = nin.s[nin.l++] = c;
258 if (n >= 3)
259 n = 0;
260 else
261 n++;
263 nin.l -= n;
264 mime_fromb64(&nin, out, is_text);
265 free(nin.s);
266 f_b = f;