forked from rc0/mairix
-
Notifications
You must be signed in to change notification settings - Fork 8
/
nvp.c
470 lines (448 loc) · 11 KB
/
nvp.c
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
mairix - message index builder and finder for maildir folders.
**********************************************************************
* Copyright (C) Richard P. Curnow 2006,2007
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
**********************************************************************
*/
#ifdef VERBOSE_TEST
#define TEST 1
#endif
/* Parse name/value pairs from mail headers into a lookup table. */
#include <stdio.h>
#include <ctype.h>
#include "mairix.h"
#include "nvptypes.h"
#include "nvpscan.h"
#include "nvp.h"
enum nvp_type {/*{{{*/
NVP_NAME,
NVP_MAJORMINOR,
NVP_NAMEVALUE
};
/*}}}*/
struct nvp_entry {/*{{{*/
struct nvp_entry *next;
struct nvp_entry *prev;
enum nvp_type type;
char *lhs;
char *rhs;
};
/*}}}*/
struct nvp {/*{{{*/
struct nvp_entry *first, *last;
};
/*}}}*/
static void append(struct nvp *nvp, struct nvp_entry *ne)/*{{{*/
{
if (!ne->rhs) {
ne->rhs = Malloc(1);
ne->rhs[0] = 0;
}
if (!ne->lhs) {
ne->lhs = Malloc(1);
ne->lhs[0] = 0;
}
ne->next = NULL;
ne->prev = nvp->last;
if (nvp->last) nvp->last->next = ne;
else nvp->first = ne;
nvp->last = ne;
}
/*}}}*/
static void append_name(struct nvp *nvp, char **name)/*{{{*/
{
struct nvp_entry *ne;
ne = new(struct nvp_entry);
ne->type = NVP_NAME;
ne->lhs = *name;
ne->rhs = NULL;
*name = NULL;
append(nvp, ne);
}
/*}}}*/
static void append_majorminor(struct nvp *nvp, char **major, char **minor)/*{{{*/
{
struct nvp_entry *ne;
ne = new(struct nvp_entry);
ne->type = NVP_MAJORMINOR;
ne->lhs = *major;
ne->rhs = *minor;
*major = *minor = NULL;
append(nvp, ne);
}
/*}}}*/
static void append_namevalue(struct nvp *nvp, char **name, char **value)/*{{{*/
{
struct nvp_entry *ne;
ne = new(struct nvp_entry);
ne->type = NVP_NAMEVALUE;
ne->lhs = *name;
ne->rhs = *value;
*name = *value = NULL;
append(nvp, ne);
}
/*}}}*/
static void combine_namevalue(struct nvp *nvp, char **name, char **value)/*{{{*/
{
struct nvp_entry *n;
for (n=nvp->first; n; n=n->next) {
if (n->type == NVP_NAMEVALUE) {
if (!strcmp(n->lhs, *name)) {
char *new_rhs;
new_rhs = new_array(char, strlen(n->rhs) strlen(*value) 1);
strcpy(new_rhs, n->rhs);
strcat(new_rhs, *value);
free(n->rhs);
n->rhs = new_rhs;
return;
}
}
}
/* No match : it's the first one */
append_namevalue(nvp, name, value);
}
/*}}}*/
static int hex_to_val(int ch)/*{{{*/
{
if (isdigit(ch))
return (ch - '0');
if (ch >= 'a' && ch <= 'f')
return (10 ch - 'a');
if (ch >= 'A' && ch <= 'F')
return (10 ch - 'A');
return (-1);
}
/*}}}*/
static void release_nvp(struct nvp *nvp)/*{{{*/
{
struct nvp_entry *e, *ne;
for (e=nvp->first; e; e=ne) {
ne = e->next;
free(e->lhs);
free(e->rhs);
free(e);
}
free(nvp);
}
/*}}}*/
struct nvp *make_nvp(struct msg_src *src, char *s, const char *pfx)/*{{{*/
{
int current_state;
unsigned int tok;
char *q, *tempsrc, *tempdst;
unsigned char qq;
char *name = NULL;
char *minor = NULL;
char *value = NULL;
char *copy_start;
enum nvp_action last_action, current_action;
enum nvp_copier last_copier;
struct nvp *result;
size_t pfxlen;
pfxlen = strlen(pfx);
if (strncasecmp(pfx, s, pfxlen))
return NULL;
s = pfxlen;
result = new(struct nvp);
result->first = result->last = NULL;
current_state = nvp_in;
q = s;
last_action = GOT_NOTHING;
last_copier = COPY_NOWHERE;
do {
qq = *(unsigned char *) q;
if (qq) {
tok = nvp_char2tok[qq];
} else {
tok = nvp_EOS;
}
current_state = nvp_next_state(current_state, tok);
#ifdef VERBOSE_TEST
fprintf(stderr, "Char x (%c) tok=%d new_current_state=%d\n",
qq, ((qq>=32) && (qq<=126)) ? qq : '.',
tok, current_state);
#endif
if (current_state < 0) {
#ifdef TEST
fprintf(stderr, "'%s' could not be parsed\n", s);
#else
fprintf(stderr, "Header '%s%s' in %s could not be parsed\n",
pfx, s, format_msg_src(src));
#endif
release_nvp(result);
result = NULL;
goto out;
}
if (nvp_copier[current_state] != last_copier) {
if (last_copier != COPY_NOWHERE) {
char *newstring = Malloc(q - copy_start 1);
memcpy(newstring, copy_start, q - copy_start);
newstring[q - copy_start] = 0;
switch (last_copier) {
case COPY_TO_NAME:
free(name);
name = newstring;
#ifdef VERBOSE_TEST
fprintf(stderr, " COPY_TO_NAME \"%s\"\n", name);
#endif
break;
case COPY_TO_MINOR:
free(minor);
minor = newstring;
#ifdef VERBOSE_TEST
fprintf(stderr, " COPY_TO_MINOR \"%s\"\n", minor);
#endif
break;
case COPY_TO_VALUE:
free(value);
value = newstring;
#ifdef VERBOSE_TEST
fprintf(stderr, " COPY_TO_VALUE \"%s\"\n", value);
#endif
break;
case COPY_NOWHERE:
/* NOTREACHED */
break;
}
}
last_copier = nvp_copier[current_state];
copy_start = q;
}
current_action = nvp_action[current_state];
switch (current_action) {
case GOT_NAME:
case GOT_NAME_TRAILING_SPACE:
case GOT_MAJORMINOR:
case GOT_NAMEVALUE:
case GOT_NAMEVALUE_CONT:
case GOT_NAMEVALUE_CSET:
case GOT_NAMEVALUE_CCONT:
#ifdef VERBOSE_TEST
fprintf(stderr, " Setting last action to %d\n", current_action);
#endif
last_action = current_action;
break;
case GOT_TERMINATOR:
#ifdef VERBOSE_TEST
fprintf(stderr, " Hit terminator; last_action=%d\n", last_action);
#endif
switch (last_action) {
case GOT_NAME:
append_name(result, &name);
break;
case GOT_NAME_TRAILING_SPACE:
tempdst = name strlen(name);
while (isspace(*--tempdst)) {}
* tempdst = 0;
append_name(result, &name);
break;
case GOT_MAJORMINOR:
append_majorminor(result, &name, &minor);
break;
case GOT_NAMEVALUE:
append_namevalue(result, &name, &value);
break;
case GOT_NAMEVALUE_CSET:
case GOT_NAMEVALUE_CCONT:
for(tempsrc = tempdst = value; *tempsrc; tempsrc ) {
if (*tempsrc == '%') {
int val = -1;
if (tempsrc[1] && tempsrc[2]) {
val = hex_to_val(* tempsrc) << 4;
val |= hex_to_val(* tempsrc);
}
if (val < 0) {
#ifdef TEST
fprintf(stderr, "'%s' could not be parsed (%%)\n", s);
#else
fprintf(stderr, "Header '%s%s' in %s could not be parsed\n",
pfx, s, format_msg_src(src));
#endif
release_nvp(result);
result = NULL;
goto out;
}
*tempdst = val;
} else
*tempdst = *tempsrc;
}
*tempdst = 0;
if (current_action == GOT_NAMEVALUE_CSET)
append_namevalue(result, &name, &value);
else
combine_namevalue(result, &name, &value);
break;
case GOT_NAMEVALUE_CONT:
combine_namevalue(result, &name, &value);
break;
default:
break;
}
/* Clear last_action so we don't repeat the action if we
get another terminator immediately. */
last_action = GOT_NOTHING;
break;
case GOT_NOTHING:
break;
}
q ;
} while (tok != nvp_EOS);
out:
/* Not all productions consume these values */
free(name);
free(value);
free(minor);
return result;
}
/*}}}*/
void free_nvp(struct nvp *nvp)/*{{{*/
{
struct nvp_entry *ne, *nne;
for (ne = nvp->first; ne; ne=nne) {
nne = ne->next;
free(ne->lhs);
free(ne->rhs);
free(ne);
}
free(nvp);
}
/*}}}*/
const char *nvp_lookup(struct nvp *nvp, const char *name)/*{{{*/
{
struct nvp_entry *ne;
for (ne = nvp->first; ne; ne=ne->next) {
if (ne->type == NVP_NAMEVALUE) {
if (!strcmp(ne->lhs, name)) {
return ne->rhs;
}
}
}
return NULL;
}
/*}}}*/
const char *nvp_lookupcase(struct nvp *nvp, const char *name)/*{{{*/
{
struct nvp_entry *ne;
for (ne = nvp->first; ne; ne=ne->next) {
if (ne->type == NVP_NAMEVALUE) {
if (!strcasecmp(ne->lhs, name)) {
return ne->rhs;
}
}
}
return NULL;
}
/*}}}*/
void nvp_dump(struct nvp *nvp, FILE *out)/*{{{*/
{
struct nvp_entry *ne;
fprintf(out, "----\n");
for (ne = nvp->first; ne; ne=ne->next) {
switch (ne->type) {
case NVP_NAME:
fprintf(out, "NAME: %s\n", ne->lhs);
break;
case NVP_MAJORMINOR:
fprintf(out, "MAJORMINOR: %s/%s\n", ne->lhs, ne->rhs);
break;
case NVP_NAMEVALUE:
fprintf(out, "NAMEVALUE: %s=%s\n", ne->lhs, ne->rhs);
break;
}
}
}
/*}}}*/
/* In these cases, we only look at the first entry */
const char *nvp_major(struct nvp *nvp)/*{{{*/
{
struct nvp_entry *ne;
ne = nvp->first;
if (ne) {
if (ne->type == NVP_MAJORMINOR) {
return ne->lhs;
} else {
return NULL;
}
} else {
return NULL;
}
}
/*}}}*/
const char *nvp_minor(struct nvp *nvp)/*{{{*/
{
struct nvp_entry *ne;
ne = nvp->first;
if (ne) {
if (ne->type == NVP_MAJORMINOR) {
return ne->rhs;
} else {
return NULL;
}
} else {
return NULL;
}
}
/*}}}*/
const char *nvp_first(struct nvp *nvp)/*{{{*/
{
struct nvp_entry *ne;
ne = nvp->first;
if (ne) {
if (ne->type == NVP_NAME) {
return ne->lhs;
} else {
return NULL;
}
} else {
return NULL;
}
}
/*}}}*/
#ifdef TEST
static void do_test(char *s)
{
struct nvp *n;
n = make_nvp(NULL, s, "");
if (n) {
nvp_dump(n, stderr);
free_nvp(n);
}
}
int main (int argc, char **argv) {
struct nvp *n;
if (argc > 1) {
while (* argv)
do_test(*argv);
return 0;
}
#if 0
do_test("attachment; filename=\"foo.c\"; prot=ro");
do_test("attachment; filename= \"foo bar.c\" ;prot=ro");
do_test("attachment ; filename= \"foo bar.c\" ;prot= ro");
do_test("attachment ; filename= \"foo bar.c\" ;prot= ro");
do_test("attachment ; filename= \"foo ; bar.c\" ;prot= ro");
do_test("attachment ; x*0=\"hi \"; x*1=\"there\"");
#endif
do_test("attachment; filename*=utf-8''Section 4-1 – Response Matrix PartIIA.doc");
#if 0
do_test("application/vnd.ms-excel; name=\"thequiz.xls\"");
do_test("inline; filename*0=\"aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj\t kkkkllll\"");
do_test(" text/plain ; name= \"foo bar.c\" ;prot= ro/rw; read/write; read= foo bar");
#endif
return 0;
}
#endif