summary refs log tree commit diff
path: root/decode.c
blob: 8578c411b7912c82a743cdf9d718646349bf760b (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
/* Copyright (C) 2020  C. McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <err.h>
#include <errno.h>
#include <iconv.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sysexits.h>

#include "archive.h"

struct Buffer {
	size_t cap;
	size_t len;
	char *ptr;
};

static struct Buffer bufferAlloc(size_t cap) {
	struct Buffer buf = {
		.cap = cap,
		.len = 0,
		.ptr = malloc(cap),
	};
	if (!buf.ptr) err(EX_OSERR, "malloc");
	return buf;
}

static char *bufferDest(struct Buffer *buf, size_t len) {
	if (buf->len + len > buf->cap) {
		buf->cap *= 2;
		buf->ptr = realloc(buf->ptr, buf->cap);
		if (!buf->ptr) err(EX_OSERR, "realloc");
	}
	char *dest = &buf->ptr[buf->len];
	buf->len += len;
	return dest;
}

static void bufferCopy(struct Buffer *buf, const char *src, size_t len) {
	char *dst = bufferDest(buf, len);
	memcpy(dst, src, len);
}

static char *bufferString(struct Buffer *buf) {
	*bufferDest(buf, 1) = '\0';
	return buf->ptr;
}

static void convertCharset(
	struct Buffer *dst, const char *charset, const char *src, size_t len
) {
	iconv_t conv = iconv_open("utf-8", charset);
	if (conv == (iconv_t)-1) {
		warn("cannot convert from %s to utf-8", charset);
		return;
	}

	for (size_t pad = 0; len; ++pad) {
		char *ptr = bufferDest(dst, len + pad);
		size_t cap = dst->cap - (ptr - dst->ptr);
		size_t n = iconv(conv, (char **)&src, &len, &ptr, &cap);
		if (n == (size_t)-1 && errno != E2BIG) {
			warn("iconv");
			break;
		}
		dst->len = dst->cap - cap;
	}

	iconv_close(conv);
}

static const char Base64[64] = {
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
};

static void decodeBase64(struct Buffer *dst, const char *src) {
	while (src[0] && src[1] && src[2] && src[3]) {
		if (!strchr(Base64, src[0])) {
			src++;
			continue;
		}
		uint32_t bits = 0;
		for (int i = 0; i < 4; ++i) {
			bits <<= 6;
			bits |= strchr(Base64, src[i]) - Base64;
		}
		*bufferDest(dst, 1) = bits >> 16;
		if (src[2] != '=') *bufferDest(dst, 1) = bits >> 8;
		if (src[3] != '=') *bufferDest(dst, 1) = bits;
		src += 4;
	}
}

static char unhex(char ch) {
	if (ch <= '9') return ch - '0';
	if (ch <= 'F') return 0xA + ch - 'A';
	return 0xA + ch - 'a';
}

static void decodeQ(struct Buffer *dst, const char *src) {
	while (*src) {
		if (src[0] == '=' && src[1] && src[2]) {
			*bufferDest(dst, 1) = 0x10 * unhex(src[1]) + unhex(src[2]);
			src += 3;
		} else if (*src == '=') {
			src++;
		} else if (*src == '_') {
			*bufferDest(dst, 1) = ' ';
			src++;
		} else {
			size_t len = strcspn(src, "=_");
			bufferCopy(dst, src, len);
			src += len;
		}
	}
}

static void decodeQuotedPrintable(struct Buffer *dst, const char *src) {
	while (*src) {
		if (src[0] == '=' && src[1] == '\r' && src[2] == '\n') {
			src += 3;
		} else if (src[0] == '=' && src[1] && src[2]) {
			*bufferDest(dst, 1) = 0x10 * unhex(src[1]) + unhex(src[2]);
			src += 3;
		} else if (src[0] == '=') {
			src++;
		} else if (src[0] == '\r' && src[1] == '\n') {
			*bufferDest(dst, 1) = '\n';
			src += 2;
		} else if (src[0] == '\r') {
			src++;
		} else {
			size_t len = strcspn(src, "=\r");
			bufferCopy(dst, src, len);
			src += len;
		}
	}
}

static void decode8Bit(struct Buffer *dst, const char *src) {
	while (*src) {
		if (src[0] == '\r' && src[1] == '\n') {
			*bufferDest(dst, 1) = '\n';
			src += 2;
		} else if (src[0] == '\r') {
			src++;
		} else {
			size_t len = strcspn(src, "\r");
			bufferCopy(dst, src, len);
			src += len;
		}
	}
}

static void
decodeEncoding(struct Buffer *dst, const char *encoding, const char *src) {
	if (!strcasecmp(encoding, "base64") || !strcasecmp(encoding, "B")) {
		decodeBase64(dst, src);
	} else if (!strcasecmp(encoding, "Q")) {
		decodeQ(dst, src);
	} else if (!strcasecmp(encoding, "quoted-printable")) {
		decodeQuotedPrintable(dst, src);
	} else if (!strcasecmp(encoding, "7bit") || !strcasecmp(encoding, "8bit")) {
		decode8Bit(dst, src);
	} else if (!strcasecmp(encoding, "binary")) {
		bufferCopy(dst, src, strlen(src));
	} else {
		warnx("unknown encoding %s", encoding);
	}
}

static void decode(
	struct Buffer *dst,
	const char *encoding, const char *charset, const char *src
) {
	if (
		!charset ||
		!strcasecmp(charset, "us-ascii") ||
		!strcasecmp(charset, "utf-8")
	) {
		decodeEncoding(dst, encoding, src);
	} else {
		struct Buffer decoded = bufferAlloc(strlen(src));
		decodeEncoding(&decoded, encoding, src);
		convertCharset(dst, charset, decoded.ptr, decoded.len);
		free(decoded.ptr);
	}
}

static void decodeWord(struct Buffer *dst, const char *src, size_t len) {
	struct Buffer word = bufferAlloc(len + 1);
	bufferCopy(&word, src, len);

	char *ptr = bufferString(&word);
	strsep(&ptr, "?");
	char *charset = strsep(&ptr, "?");
	char *encoding = strsep(&ptr, "?");
	char *encoded = strsep(&ptr, "?");

	if (charset && encoding && encoded && ptr && *ptr == '=') {
		decode(dst, encoding, charset, encoded);
	} else {
		bufferCopy(dst, src, len);
	}

	free(word.ptr);
}

char *decodeHeader(const char *header) {
	struct Buffer buf = bufferAlloc(strlen(header) + 1);
	while (*header) {
		size_t len = strcspn(header, " ");
		if (!strncmp(header, "=?", 2)) {
			decodeWord(&buf, header, len);
		} else {
			if (header[len]) len++;
			bufferCopy(&buf, header, len);
		}
		header += len;
	}
	return bufferString(&buf);
}

char *decodeToString(const struct BodyPart *part, const char *src) {
	struct Buffer dst = bufferAlloc(strlen(src) + 1);
	decode(&dst, part->encoding, paramGet(part->params, "charset"), src);
	return bufferString(&dst);
}

int decodeToFile(FILE *file, const struct BodyPart *part, const char *src) {
	struct Buffer dst = bufferAlloc(strlen(src));
	decode(&dst, part->encoding, paramGet(part->params, "charset"), src);
	size_t n = fwrite(dst.ptr, dst.len, 1, file);
	free(dst.ptr);
	return (n ? 0 : -1);
}