-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.c
More file actions
316 lines (278 loc) · 6.34 KB
/
Copy pathdecode.c
File metadata and controls
316 lines (278 loc) · 6.34 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "decode.h"
#include "config.h"
#include <sys/types.h> /* ssize_t */
#include <ctype.h>
#include <stdint.h>
#include <string.h>
#include "libks/arena-buffer.h"
#include "libks/arena.h"
#include "libks/buffer.h"
#include "libks/compiler.h"
static ssize_t b64_pton(const char *, unsigned char *, size_t);
static void quoted_printable_decode_buffer(struct buffer *, const char *,
size_t, int);
static int
htoa(char c, char *res)
{
char up = toupper((unsigned char)c);
if (up >= 'A' && up <= 'F') {
*res = 10 + (up - 'A');
return 0;
}
if (up >= '0' && up <= '9') {
*res = up - '0';
return 0;
}
return 1;
}
const char *
base64_decode(const char *str, struct arena_scope *s)
{
size_t len = strlen(str);
uint8_t *dec = arena_malloc(s, len + 1);
ssize_t n = b64_pton(str, dec, len + 1);
if (n == -1)
return NULL;
dec[n] = '\0';
return (char *)dec;
}
const char *
quoted_printable_decode(const char *str, struct arena_scope *s)
{
struct buffer *bf;
size_t len;
len = strlen(str);
bf = arena_buffer_alloc(s, len);
quoted_printable_decode_buffer(bf, str, len, 0);
return buffer_str(bf);
}
/*
* Decode header which can either be encoded using quoted printable or base64,
* see RFC 2047.
*/
const char *
rfc2047_decode(const char *str, struct arena_scope *s)
{
struct buffer *bf;
const char *es = str;
bf = arena_buffer_alloc(s, strlen(str));
while (*es != '\0') {
if (strncmp(es, "=?", 2) == 0) {
const char *ee;
size_t len;
char enc;
es = &es[2]; /* consume "=?" */
es = strchr(es, '?');
if (es == NULL)
goto err;
es += 1;
if (*es == '\0')
goto err;
enc = *es;
es += 1;
if (*es != '?')
goto err;
es += 1;
ee = strstr(es, "?=");
if (ee == NULL)
goto err;
len = (size_t)(ee - es);
switch (toupper((unsigned char)enc)) {
case 'B': {
const char *dst, *src;
/* b64decode() requires a NUL-terminator. */
src = arena_strndup(s, es, len);
dst = base64_decode(src, s);
if (dst == NULL)
goto err;
buffer_printf(bf, "%s", dst);
break;
}
case 'Q':
quoted_printable_decode_buffer(bf, es, len, 1);
break;
default:
goto err;
}
es = &ee[2]; /* consume "?=" */
/* Spaces between encoded words must be ignored. */
ee = strstr(es, "=?");
if (ee != NULL) {
const char *p = es;
while (isspace((unsigned char)p[0]))
p++;
if (p == ee)
es = p;
}
} else {
buffer_putc(bf, *es++);
}
}
return buffer_str(bf);
err:
return arena_strdup(s, str);
}
static void
quoted_printable_decode_buffer(struct buffer *bf, const char *str, size_t len,
int dospace)
{
size_t i = 0;
while (i < len) {
char hi, lo;
if (str[i] == '_' && dospace) {
buffer_putc(bf, ' ');
i++;
continue;
}
if (str[i] != '=') {
buffer_putc(bf, str[i++]);
continue;
}
if (i + 1 == len) {
/* '=' followed by nothing, copy as is. */
buffer_putc(bf, str[i++]);
break;
}
i++; /* consume '=' */
if (str[i] == '\n') {
/* Soft line break. */
i++;
continue;
}
if (i + 1 == len ||
htoa(str[i], &hi) || htoa(str[i + 1], &lo)) {
/*
* Either too few characters to make up a hexadecimal
* representation or decoding failed. Copy the '=' and
* let subsequent iterations copy the following
* characters as is.
*/
buffer_putc(bf, '=');
continue;
}
buffer_putc(bf, (char)((hi << 4) | lo));
i += 2;
}
}
static ssize_t
b64_pton(const char *src, unsigned char *target, size_t targsize)
{
static const char Base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char Pad64 = '=';
size_t tarindex;
int ch, state;
unsigned char nextbyte;
const char *pos;
state = 0;
tarindex = 0;
while ((ch = (unsigned char)*src++) != '\0') {
if (isspace(ch)) /* Skip whitespace anywhere. */
continue;
if (ch == Pad64)
break;
pos = strchr(Base64, ch);
if (pos == 0) /* A non-base64 character. */
return -1;
switch (state) {
case 0:
if (target) {
if (tarindex >= targsize)
return -1;
target[tarindex] =
(uint8_t)((pos - Base64) << 2);
}
state = 1;
break;
case 1:
if (target) {
if (tarindex >= targsize)
return -1;
target[tarindex] |= (pos - Base64) >> 4;
nextbyte =
(uint8_t)(((pos - Base64) & 0x0f) << 4);
if (tarindex + 1 < targsize)
target[tarindex + 1] = nextbyte;
else if (nextbyte)
return -1;
}
tarindex++;
state = 2;
break;
case 2:
if (target) {
if (tarindex >= targsize)
return -1;
target[tarindex] |= (pos - Base64) >> 2;
nextbyte =
(uint8_t)(((pos - Base64) & 0x03) << 6);
if (tarindex + 1 < targsize)
target[tarindex + 1] = nextbyte;
else if (nextbyte)
return -1;
}
tarindex++;
state = 3;
break;
case 3:
if (target) {
if (tarindex >= targsize)
return -1;
target[tarindex] |= (pos - Base64);
}
tarindex++;
state = 0;
break;
}
}
/*
* We are done decoding Base-64 chars. Let's see if we ended
* on a byte boundary, and/or with erroneous trailing characters.
*/
if (ch == Pad64) { /* We got a pad char. */
ch = (unsigned char)*src++; /* Skip it, get next. */
switch (state) {
case 0: /* Invalid = in first position */
case 1: /* Invalid = in second position */
return -1;
case 2: /* Valid, means one byte of info */
/* Skip any number of spaces. */
for (; ch != '\0'; ch = (unsigned char)*src++) {
if (!isspace(ch))
break;
}
/* Make sure there is another trailing = sign. */
if (ch != Pad64)
return -1;
ch = (unsigned char)*src++; /* Skip the = */
/* Fall through to "single trailing =" case. */
FALLTHROUGH;
case 3: /* Valid, means two bytes of info */
/*
* We know this char is an =. Is there anything but
* whitespace after it?
*/
for (; ch != '\0'; ch = (unsigned char)*src++) {
if (!isspace(ch))
return -1;
}
/*
* Now make sure for cases 2 and 3 that the "extra"
* bits that slopped past the last full byte were
* zeros. If we don't check them, they become a
* subliminal channel.
*/
if (target && tarindex < targsize &&
target[tarindex] != 0)
return -1;
}
} else {
/*
* We ended by seeing the end of the string. Make sure we
* have no partial bytes lying around.
*/
if (state != 0)
return -1;
}
return (ssize_t)tarindex;
}