about summary refs log tree commit diff
path: root/blaze822.c
blob: 188182d54cb89ecc7700f5a5b149385bb2db040c (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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

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

#define bufsiz 4096

static long
parse_posint(char **s, size_t minn, size_t maxn)
{
	long n;
	char *end;

	errno = 0;
	n = strtol(*s, &end, 10);
	if (errno)
		return -1;
	if (n < (long)minn || n > (long)maxn) {
		errno = ERANGE;
		return -1;
	}
	*s = end;
	return n;
}

time_t
blaze822_date(char *s) {
	struct tm tm;
	int c;

#define i4(m) (((uint32_t) m[0]<<24 | m[1]<<16 | m[2]<<8 | m[3]) == \
	       ((uint32_t) s[0]<<24 | s[1]<<16 | s[2]<<8 | s[3] | 0x20202020) \
	       && (s += 4))

#define i3(m) (((uint32_t) m[0]<<24 | m[1]<<16 | m[2]<<8) == \
	       ((uint32_t) s[0]<<24 | s[1]<<16 | s[2]<<8 | 0x20202000) \
	       && (s += 3))

	while (iswsp(*s))
		s++;
	if (i4("mon,") || i4("tue,") || i4("wed,") || i4("thu,") ||
	    i4("fri,") || i4("sat,") || i4("sun,"))
		while (iswsp(*s))
			s++;

	if ((c = parse_posint(&s, 1, 31)) < 0) goto fail;
	tm.tm_mday = c;

	while (iswsp(*s))
		s++;

	if      (i3("jan")) tm.tm_mon = 0;
	else if (i3("feb")) tm.tm_mon = 1;
	else if (i3("mar")) tm.tm_mon = 2;
	else if (i3("apr")) tm.tm_mon = 3;
	else if (i3("may")) tm.tm_mon = 4;
	else if (i3("jun")) tm.tm_mon = 5;
	else if (i3("jul")) tm.tm_mon = 6;
	else if (i3("aug")) tm.tm_mon = 7;
	else if (i3("sep")) tm.tm_mon = 8;
	else if (i3("oct")) tm.tm_mon = 9;
	else if (i3("nov")) tm.tm_mon = 10;
	else if (i3("dec")) tm.tm_mon = 11;
	else goto fail;

#undef i3
#undef i4

	while (iswsp(*s))
		s++;

	if ((c = parse_posint(&s, 1000, 9999)) > 0) {
		tm.tm_year = c - 1900;
	} else if ((c = parse_posint(&s, 0, 49)) > 0) {
		tm.tm_year = c + 100;
	} else if ((c = parse_posint(&s, 50, 99)) > 0) {
		tm.tm_year = c;
	} else goto fail;

	while (iswsp(*s))
		s++;

	if ((c = parse_posint(&s, 0, 24)) < 0) goto fail;
	tm.tm_hour = c;
	if (*s++ != ':') goto fail;
	if ((c = parse_posint(&s, 0, 59)) < 0) goto fail;
	tm.tm_min = c;
	if (*s++ == ':') {
		if ((c = parse_posint(&s, 0, 61)) < 0) goto fail;
		tm.tm_sec = c;
	} else {
		tm.tm_sec = 0;
	}

	while (iswsp(*s))
		s++;

	if (*s == '+' || *s == '-') {
		int neg = (*s == '-');
		s++;
		if ((c = parse_posint(&s, 0, 10000)) < 0) goto fail;
		if (neg) {
			tm.tm_hour += c / 100;
			tm.tm_min  += c % 100;
		} else {
			tm.tm_hour -= c / 100;
			tm.tm_min  -= c % 100;
		}
	}

	tm.tm_isdst = -1;

	time_t r = tm_to_secs(&tm);
	return r;

fail:
	return -1;
}

static char *
skip_comment(char *s)
{
	if (*s != '(')
		return s;

	long d = 0;
	do {
		if (!*s)
			break;
		else if (*s == '(')
			d++;
		else if (*s == ')')
			d--;
		s++;
	} while (d > 0);

	return s;
}

// always 0 terminates
// never writes more than dstmax to dst
// returns how many bytes were appended
static size_t
safe_append(char *dst, size_t dstmax, char *strbeg, char *strend)
{
	size_t dstlen = strlen(dst);
	if (dstmax - dstlen - 1 < strend - strbeg)
		strend = strbeg + (dstmax - dstlen - 1);
	memcpy(dst + dstlen, strbeg, strend - strbeg);
	dst[dstlen + (strend - strbeg)] = 0;
	return strend - strbeg;
}

static size_t
safe_append_space(char *dst, size_t dstmax)
{
	char sp[] = " ";
	char *se = sp + 1;

	return safe_append(dst, dstmax, sp, se);
}

char *
blaze822_addr(char *s, char **dispo, char **addro)
{
	static char disp[1024];
	static char addr[1024];

	memset(addr, 0, sizeof addr);
	memset(disp, 0, sizeof disp);

	char ttok[1024] = { 0 };
	char *tc = ttok;
	char *te = ttok + sizeof ttok;

	int not_addr = 0;

	while (1) {
		if (!*s || iswsp(*s) || *s == ',' || *s == ';') {
			if (tc != ttok) {
				if (!*addr && !not_addr && memchr(ttok, '@', tc - ttok)) {
					safe_append(addr, sizeof addr, ttok, tc);
				} else {
					if (*disp)
						safe_append_space(disp, sizeof disp);
					safe_append(disp, sizeof disp,
					    ttok, tc);
				}
				tc = ttok;
				*ttok = 0;
				not_addr = 0;
			}
			if (!*s) {
				if (!*addr && !*disp) {
					if (dispo) *dispo = 0;
					if (addro) *addro = 0;
					return 0;
				}
				break;
			}
			if (*s == ',' || *s == ';') {
				s++;
				if (*addr || *disp)
					break;
			}
			s++;
		} else if (*s == '<') {
			char tok[1024] = { 0 };
			char *c = tok;
			char *e = tok + sizeof tok;
			s++;
			while (*s && c < e && *s != '>') {
				s = skip_comment(s);
				if (*s == '"') {
					// local part may be quoted, allow all
					s++;
					while (*s && c < e && *s != '"') {
						if (*s == '\\')
							s++;
						*c++ = *s++;
					}
					if (*s == '"')
						s++;
				} else if (*s == '<') {
					c = tok;
					s++;
				} else {
					if (iswsp(*s))
						s++;
					else
						*c++ = *s++;
				}
			}
			if (*s == '>')
				s++;
			if (*addr) {
				if (*disp)
					safe_append_space(disp, sizeof disp);
				safe_append(disp, sizeof disp,
				    addr, addr + strlen(addr));
			}
			*addr = 0;
			safe_append(addr, sizeof addr, tok, c);
		} else if (*s == '"') {
			char tok[1024] = { 0 };
			char *c = tok;
			char *e = tok + sizeof tok;
			s++;
			while (*s && c < e && *s != '"') {
				if (*s == '\\')
					s++;
				*c++ = *s++;
			}
			if (*s == '"')
				s++;

			if (memchr(tok, '@', c - tok))
				not_addr = 1;  // @ inside "" is never an addr

			if (tc != ttok)
				tc += safe_append_space(ttok, sizeof ttok);
			tc += safe_append(ttok, sizeof ttok, tok, c);
		} else if (*s == '(') {
			char *z = skip_comment(s);
			if (!*disp && *addr)  // user@host (name)
				safe_append(disp, sizeof disp, s + 1, z - 1);
			else if (*disp) {  // copy comment
				safe_append_space(disp, sizeof disp);
				safe_append(disp, sizeof disp, s, z);
			}
			s = z;
		} else if (*s == ':') {
			if (memchr(ttok, '[', tc - ttok)) {
				// in ipv6 address
				if (tc < te)
					*tc++ = *s++;
			} else {  // ignore group name and start over
				s++;
				tc = ttok;
				memset(addr, 0, sizeof addr);
				memset(disp, 0, sizeof disp);
				*ttok = 0;
				not_addr = 0;
			}
		} else {
			if (*s == '\\' && *(s+1))
				s++;
			if (tc < te)
				*tc++ = *s++;
		}
	}

	char *host = strrchr(addr, '@');
	char *u;
	if (host && (u = strpbrk(addr, "()<>[]:;@\\,\" \t")) && u < host) {
		// need to "-quote local-part

		ssize_t hlen = strlen(host);
		char addr2[sizeof addr];
		char *e = addr2 + sizeof addr2 - 1;
		char *t;

		u = addr;
		t = addr2;
		*t++ = '"';
		while (u < host && e - t > 2) {
			if (*u == '"' || *u == '\\')
				*t++ = '\\';
			*t++ = *u++;
		}
		*t++ = '"';
		if (e - t > hlen + 1) {
			memcpy(t, host, hlen);
			*(t + hlen) = 0;
			memcpy(addr, addr2, sizeof addr);
		}
	}

	if (dispo) *dispo = *disp ? disp : 0;
	if (addro) *addro = *addr ? addr : 0;

	return s;
}

static void
compress_hdr(char *s, char *end)
{
	char *t, *h;

	if ((t = h = strchr(s, '\n'))) {
		while (h < end && *h) {
			if (*h == '\n') {
				*t++ = ' ';
				while (*h && isfws(*h) && *(h+1))
					h++;
			}
			*t++ = *h++;
		}
		// remove trailing whitespace
		while (s < t && isfws(t[-1]))
			*--t = 0;
		// zero fill gap
		while (t < h)
			*t++ = 0;
	}
}


static void
unfold_hdr(char *buf, char *end)
{
	char *s, *l;
	*end = 0;

	// sanitize all nul in message headers, srsly
	if (memchr(buf, 0, end-buf))
		for (s = buf; s < end; s++)
			if (*s == 0)
				*s = ' ';

	// normalize crlf
	if (memchr(buf, '\r', end-buf))
		for (s = buf; s < end; s++)
			if (*s == '\r') {
				if (*(s+1) == '\n')
					*s = '\n';
				else
					*s = ' ';
			}

	l = buf;
	s = buf;

	while (s < end && *s != ':' && *s != '\n') {
		*s = lc(*s);
		s++;
	}

	while (s < end) {
		s = memchr(s+1, '\n', end-s);
		if (!s)
			break;

		while (s < end && *s == '\n')
			s++;
		if (!iswsp(*s)) {
			*(s-1) = 0;
			compress_hdr(l, s-1);
			l = s;
			while (s < end && *s != ':' && *s != '\n') {
				*s = lc(*s);
				s++;
			}
		}
	}
	compress_hdr(l, end);
}

struct message *
blaze822(char *file)
{
	int fd;
	ssize_t rd;
	char *buf;
	ssize_t bufalloc;
	ssize_t used;
	char *end;

	struct message *mesg = malloc(sizeof (struct message));
	if (!mesg)
		return 0;

	fd = open(file, O_RDONLY);
	if (fd < 0) {
		free(mesg);
		return 0;
	}

	buf = 0;
	bufalloc = 0;
	used = 0;

	while (1) {
		int overlap = used > 3 ? 3 : 0;

		bufalloc += bufsiz;
		buf = realloc(buf, bufalloc);
		if (!buf) {
			free(mesg);
			close(fd);
			return 0;
		}

		rd = read(fd, buf+used, bufalloc-used);
		if (rd == 0) {
			end = buf+used;
			break;
		}
		if (rd < 0) {
			free(mesg);
			free(buf);
			close(fd);
			return 0;
		}

		if ((end = mymemmem(buf-overlap+used, rd+overlap, "\n\n", 2))) {
			end++;
			break;
		}
		if ((end = mymemmem(buf-overlap+used, rd+overlap, "\r\n\r\n", 4))) {
			end++;
			end++;
			break;
		}

		used += rd;
	}
	close(fd);

	*end = 0;   // dereferencing *end is safe

	unfold_hdr(buf, end);

	mesg->msg = buf;
	mesg->end = end;
	mesg->body = mesg->bodyend = mesg->bodychunk = mesg->orig_header = 0;

	return mesg;
}

struct message *
blaze822_mem(char *src, size_t len)
{
	char *buf;
	char *end;

	struct message *mesg = malloc(sizeof (struct message));
	if (!mesg)
		return 0;

	if ((end = mymemmem(src, len, "\n\n", 2))) {
		mesg->body = end+2;
	} else if ((end = mymemmem(src, len, "\r\n\r\n", 4))) {
		mesg->body = end+4;
	} else {
		end = src + len;
		mesg->body = end;
		mesg->bodyend = end;
	}
	if (mesg->body)
		mesg->bodyend = src + len;

	size_t hlen = end - src;

	buf = malloc(hlen+1);
	if (!buf)
		return 0;
	memcpy(buf, src, hlen);

	end = buf+hlen;
	*end = 0;   // dereferencing *end is safe

	unfold_hdr(buf, end);

	mesg->msg = buf;
	mesg->end = end;
	mesg->bodychunk = 0;   // src is not ours
	mesg->orig_header = src;

	return mesg;
}

void
blaze822_free(struct message *mesg)
{
	if (!mesg)
		return;
	if (mesg->bodychunk == mesg->msg) {
		munmap(mesg->bodychunk, mesg->bodyend - mesg->msg);
	} else {
		free(mesg->msg);
		free(mesg->bodychunk);
	}
	free(mesg);
}

char *
blaze822_hdr_(struct message *mesg, const char *hdr, size_t hdrlen)
{
	char *v;

	if (hdrlen == 0 || hdrlen-1 >= (size_t)(mesg->end - mesg->msg))
		return 0;  // header too small for the key, probably empty

	// special case: first header, no leading nul
	if (memcmp(mesg->msg, hdr+1, hdrlen-1) == 0) {
		v = mesg->msg;
		hdrlen--;
	} else {
		v = mymemmem(mesg->msg, mesg->end - mesg->msg, hdr, hdrlen);
	}
	if (!v)
		return 0;
	v += hdrlen;
	while (*v && iswsp(*v))
		v++;
	return v;
}

char *
blaze822_chdr(struct message *mesg, const char *chdr)
{
	char hdr[256];
	char *c;

	size_t l = snprintf(hdr, sizeof hdr, "%c%s:", 0, chdr);
	for (c = hdr+1; *c; c++)
		*c = lc(*c);

	return blaze822_hdr_(mesg, hdr, l);
}

struct message *
blaze822_file(char *file)
{
	char *buf = 0;
	ssize_t rd = 0, n;

	int fd = open(file, O_RDONLY);
	if (fd < 0)
		return 0;

	struct stat st;
	if (fstat(fd, &st) < 0)
		goto error;

	if (S_ISFIFO(st.st_mode)) {  // unbounded read, grow buffer
		const ssize_t bufblk = 16384;
		ssize_t bufalloc = bufblk;
		buf = malloc(bufalloc);
		if (!buf)
			goto error;

		do {
			if (bufalloc < rd + bufblk) {
				bufalloc *= 2;
				buf = realloc(buf, bufalloc);
				if (!buf)
					goto error;
			}
			if ((n = read(fd, buf + rd, bufblk)) < 0) {
				if (errno == EINTR) {
					continue;
				} else {
					perror("read");
					goto error;
				}
			}
			rd += n;
		} while (n > 0);
	} else {  // file size known
		ssize_t s = st.st_size;

		buf = malloc(s+1);
		if (!buf)
			goto error;

		do {
			if ((n = read(fd, buf + rd, s - rd)) < 0) {
				if (errno == EINTR) {
					continue;
				} else {
					perror("read");
					goto error;
				}
			}
			rd += n;
		} while (rd < s && n > 0);
	}

	close(fd);

	buf[rd] = 0;

	// XXX duplicate header in ram...
	struct message *mesg = blaze822_mem(buf, rd);
	if (mesg)
		mesg->bodychunk = buf;
	return mesg;

error:
	close(fd);
	free(buf);
	return 0;
}

struct message *
blaze822_mmap(char *file)
{
	int fd = open(file, O_RDONLY);
	if (fd < 0)
		return 0;

	struct stat st;
	if (fstat(fd, &st) < 0)
		goto error;

	size_t len = st.st_size;

	struct message *mesg = malloc(sizeof (struct message));
	if (!mesg)
		goto error;

	char *buf = mmap(0, len+1, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
	if (buf == MAP_FAILED) {
		perror("mmap");
		goto error;
	}
	close(fd);

	char *end;
	if ((end = mymemmem(buf, len, "\n\n", 2))) {
		mesg->body = end+2;
	} else if ((end = mymemmem(buf, len, "\r\n\r\n", 4))) {
		mesg->body = end+4;
	} else {
		end = buf + len;
		mesg->body = end;
	}

	unfold_hdr(buf, end);

	mesg->msg = mesg->bodychunk = buf;
	mesg->end = end;
	mesg->bodyend = buf + len;
	mesg->orig_header = 0;

	return mesg;

error:
	close(fd);
	return 0;
}

size_t
blaze822_headerlen(struct message *mesg)
{
	return mesg->end - mesg->msg;
}

char *
blaze822_body(struct message *mesg)
{
	return mesg->body;
}

char *
blaze822_orig_header(struct message *mesg)
{
	return mesg->orig_header;
}

size_t
blaze822_bodylen(struct message *mesg)
{
	if (!mesg->body || !mesg->bodyend)
		return 0;
	return mesg->bodyend - mesg->body;
}

char *
blaze822_next_header(struct message *mesg, char *prev)
{
	if (!prev) {
		prev = mesg->msg;
	} else {
		if (prev >= mesg->end)
			return 0;
		prev = prev + strlen(prev);
	}
	while (prev < mesg->end && *prev == 0)
		prev++;
	if (prev >= mesg->end)
		return 0;
	return prev;
}