summary refs log tree commit diff
path: root/rfc2047.c
blob: e53a5ce62b37a723eb4fbe0b523df84f402260d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <stdlib.h>
#include <errno.h>
#include <iconv.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>

#include "blaze822.h"
#include "blaze822_priv.h"

// XXX keep trying bytewise on invalid iconv

int
blaze822_decode_qp(char *start, char *stop, char **deco, size_t *decleno)
{
	static signed char hex[] = {
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		 0, 1, 2, 3,  4, 5, 6, 7,  8, 9,-1,-1, -1,-1,-1,-1,
		-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
	};

	char *buf = malloc(4 * (stop - start) + 1);
	if (!buf)
		return 0;

	*deco = buf;

	char *s = start;
	while (s < stop) {
		if (*s == '=' && s[1] == '\n') {
			s += 2;
		} else if (*s == '=' && s+2 < stop) {
			unsigned char c1 = s[1];
			unsigned char c2 = s[2];
			s += 3;
			if (c1 > 127 || c2 > 127 || hex[c1] < 0 || hex[c2] < 0) {
				*buf++ = '?';
				*buf++ = '?';
				*buf++ = '?';
				continue;
			}
			*buf++ = (hex[c1] << 4) | hex[c2];
		} else if (*s == '_') {
			*buf++ = ' ';
			s++;
		} else {
			*buf++ = *s++;
		}
	}

	*buf = 0;

	*decleno = buf - *deco;
	return 1;
}

int
blaze822_decode_b64(char *s, char *e, char **deco, size_t *decleno)
{
	static signed char b64[128] = {
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
		-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
		52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1,
		-1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
		15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
		-1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
		41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
	};

	char *buf = malloc((e - s) / 4 * 3);
	if (!buf)
		return 0;

	*deco = buf;

	while (s + 4 <= e) {
		while (s < e && isfws((unsigned char) *s))
			s++;
		if (s >= e)
			break;

		uint32_t v = 0;
		unsigned char t = 0;

		unsigned char c0=s[0], c1=s[1], c2=s[2], c3=s[3];
		s += 4;

		if ((c0 | c1 | c2 | c3) > 127)
			goto error;

		v |= b64[c0]; t |= b64[c0]; v <<= 6;
		v |= b64[c1]; t |= b64[c1]; v <<= 6;
		v |= b64[c2]; t |= b64[c2]; v <<= 6;
		v |= b64[c3]; t |= b64[c3];

		if (t >= 64) {
error:
			*buf++ = '?';
			*buf++ = '?';
			*buf++ = '?';
			continue;
		}

		char d2 = v & 0xff; v >>= 8;
		char d1 = v & 0xff; v >>= 8;
		char d0 = v & 0xff;

		if (c1 != '=') *buf++ = d0;
		if (c2 != '=') *buf++ = d1;
		if (c3 != '=') *buf++ = d2;
	}

	*decleno = buf - *deco;
	return 1;
}

int
blaze822_decode_rfc2047(char *dst, char *src, size_t dlen, char *tgtenc)
{
	iconv_t ic = (iconv_t)-1;

	char *b = src;

	// use memmem
	char *s = strstr(src, "=?");
	if (!s)
		goto nocodeok;

	do {
		char *t;
		t = b;
		while (t < s)  // strip space-only inbetween encoded words
			if (!isfws(*t++)) {
				while (b < s && dlen) {
					*dst++ = *b++;
					dlen--;
				}
				break;
			}

		if (!dlen)
			break;

		s += 2;

		char *e = strchr(s, '?');
		if (!e)
			goto nocode;

		*e = 0;
		ic = iconv_open(tgtenc, s);
		*e = '?';
		e++;

		if (ic == (iconv_t)-1)
			goto nocode;

		char enc = lc(*e++);
		if (*e++ != '?')
			goto nocode;
		char *start = e++;
		char *stop = strstr(e, "?=");
		if (!stop)
			goto nocode;

		char *dec, *decchunk;
		size_t declen;
		if (enc == 'q')
			blaze822_decode_qp(start, stop, &dec, &declen);
		else if (enc == 'b')
			blaze822_decode_b64(start, stop, &dec, &declen);
		else
			goto nocode;

		decchunk = dec;
		int r = iconv(ic, &dec, &declen, &dst, &dlen);
		if (r < 0) {
			if (errno == E2BIG) {
				iconv_close(ic);
				break;
			} else if (errno == EILSEQ || errno == EINVAL) {
				goto nocode;
			} else {
				perror("iconv");
				goto nocode;
			}
		}

		iconv_close(ic);

		while (declen && dlen) {
			*dst++ = *dec++;
			declen--;
			dlen--;
		}

		free(decchunk);

		b = stop + 2;
	} while (dlen && (s = strstr(b, "=?")));

	while (*b && dlen > 0) {
		*dst++ = *b++;
		dlen--;
	}

	*dst = 0;

	return 1;

nocode:
	if (ic != (iconv_t)-1)
		iconv_close(ic);

	fprintf(stderr, "error decoding rfc2047\n");
nocodeok:
	while (*src && dlen) {
		*dst++ = *src++;
		dlen--;
	}
	*dst = 0;

	return 1;
}

#ifdef TEST
int 
main() {
	char *r;
	size_t l;
	char test[] = "Keld_J=F8rn_Simonsen";
	blaze822_decode_qp(test, test + sizeof test, &r, &l);
	printf("%s %d\n", r, l);

	char *r2;
	size_t l2;
	char test2[] = "SWYgeW91IGNhbiByZWFkIHRoaXMgeW8="; // dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==";
	blaze822_decode_b64(test2, test2+sizeof test2, &r2, &l2);
	printf("%s %d\n", r2, l2);

	char test3[] = "=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>";
	char test3dec[255];
	blaze822_decode_rfc2047(test3dec, test3, sizeof test3dec, "UTF-8");
	printf("%s\n", test3dec);

	char test4[] = "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= "
	    "=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?= z "
	    "=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?=";
	char test4dec[255];
	blaze822_decode_rfc2047(test4dec, test4, sizeof test4dec, "UTF-8");
	printf("%s\n", test4dec);
	
}
#endif