1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
7 #include <ccan/str/str.h>
8 #include <ccan/list/list.h>
11 #include <ccan/mem/mem.h>
12 #include <ccan/rfc822/rfc822.h>
15 #include <ccan/tal/talloc/talloc.h>
17 #include <ccan/tal/tal.h>
20 static void (*allocation_failure_hook)(const char *);
22 static void NORETURN default_allocation_failure(const char *s)
24 fprintf(stderr, "ccan/rfc822: Allocation failure: %s", s);
28 static void allocation_failure(const char *s)
30 if (allocation_failure_hook)
31 (*allocation_failure_hook)(s);
33 default_allocation_failure(s);
36 void rfc822_set_allocation_failure_handler(void (*h)(const char *))
38 allocation_failure_hook = h;
41 #define ALLOC_CHECK(p, r) \
44 allocation_failure(__FILE__ ":" stringify(__LINE__)); \
50 * No real point doing fancy resizing hashes, when any given mail
51 * message is unlikely to have more than a fairly small number of
52 * distinct header types. This should be ample.
54 #define INDEX_HASH_SIZE 63
57 const char *data, *end;
58 const char *remainder;
59 struct list_head headers;
60 struct list_head header_index[INDEX_HASH_SIZE];
64 struct rfc822_header {
65 struct bytestring all, rawname, rawvalue;
66 struct bytestring unfolded;
67 struct list_node list;
68 struct rfc822_header *name_next;
71 struct rfc822_headers_of_name {
72 struct bytestring name;
73 struct rfc822_header *first;
74 struct rfc822_header **lastptr;
75 struct list_node bucket;
78 struct rfc822_msg *rfc822_check(const struct rfc822_msg *msg,
82 if (!list_check(&msg->headers, abortstr))
84 if (!tal_check(msg, abortstr))
86 return (struct rfc822_msg *)msg;
89 #ifdef CCAN_RFC822_DEBUG
90 #define CHECK(msg, str) do { rfc822_check((msg), (str)); } while (0)
92 #define CHECK(msg, str) do { } while (0)
95 struct rfc822_msg *rfc822_start(const void *ctx, const char *p, size_t len)
97 struct rfc822_msg *msg;
100 msg = tal(ctx, struct rfc822_msg);
101 ALLOC_CHECK(msg, NULL);
106 msg->remainder = msg->data;
109 list_head_init(&msg->headers);
111 for (i = 0; i < INDEX_HASH_SIZE; i++)
112 list_head_init(&msg->header_index[i]);
114 CHECK(msg, "<rfc22_start");
119 void rfc822_free(struct rfc822_msg *msg)
121 CHECK(msg, ">rfc822_free");
125 static struct rfc822_header *next_header_cached(struct rfc822_msg *msg,
126 struct rfc822_header *hdr)
128 struct list_node *h = &msg->headers.n;
129 const struct list_node *n = h;
131 CHECK(msg, ">next_header_cached");
139 CHECK(msg, "<next_header_cached");
141 return list_entry(n->next, struct rfc822_header, list);
144 static const char *next_line(const char *start, const char *end)
146 const char *p = memchr(start, '\n', end - start);
148 return p ? (p + 1) : end;
151 static struct rfc822_header *index_header(struct rfc822_msg *msg,
152 struct rfc822_header *hdr);
154 static struct rfc822_header *next_header_parse(struct rfc822_msg *msg)
156 const char *h, *eh, *ev, *colon;
157 struct rfc822_header *hi;
159 CHECK(msg, ">next_header_parse");
164 if (msg->body && (msg->remainder >= msg->body))
168 eh = next_line(h, msg->end);
171 if ((ev > h) && (ev[-1] == '\n'))
173 if ((ev > h) && (ev[-1] == '\r'))
176 /* Found the end of the headers */
178 assert(!msg->body || (msg->body == eh));
185 while ((eh < msg->end) && rfc822_iswsp(*eh))
186 eh = next_line(eh, msg->end);
189 msg->remainder = NULL;
194 hi = talz(msg, struct rfc822_header);
195 ALLOC_CHECK(hi, NULL);
197 hi->all = bytestring(h, eh - h);
198 list_add_tail(&msg->headers, &hi->list);
200 colon = memchr(h, ':', hi->all.len);
202 hi->rawname = bytestring(h, colon - h);
203 hi->rawvalue = bytestring(colon + 1, eh - colon - 1);
205 hi->rawname = bytestring_NULL;
206 hi->rawvalue = bytestring_NULL;
209 CHECK(msg, "<next_header_parse");
211 return index_header(msg, hi);
214 struct rfc822_header *rfc822_next_header(struct rfc822_msg *msg,
215 struct rfc822_header *hdr)
217 struct rfc822_header *h;
219 CHECK(msg, ">rfc822_next_header");
221 h = next_header_cached(msg, hdr);
225 return next_header_parse(msg);
228 struct bytestring rfc822_body(struct rfc822_msg *msg)
230 CHECK(msg, ">rfc822_body");
232 if (!msg->body && msg->remainder) {
235 p = memmem(msg->remainder, msg->end - msg->remainder,
237 q = memmem(msg->remainder, msg->end - msg->remainder,
240 if (p && (!q || (p < q)))
242 else if (q && (!p || (q < p)))
245 if (msg->body >= msg->end) {
246 assert(msg->body == msg->end);
251 CHECK(msg, "<rfc822_body");
254 return bytestring(msg->body, msg->end - msg->body);
256 return bytestring_NULL;
259 enum rfc822_header_errors rfc822_header_errors(struct rfc822_msg *msg,
260 struct rfc822_header *hdr)
262 enum rfc822_header_errors err = 0;
265 if (!hdr->rawname.ptr) {
266 err |= RFC822_HDR_NO_COLON;
268 for (i = 0; i < hdr->rawname.len; i++) {
269 char c = hdr->rawname.ptr[i];
273 if ((c < 33) || (c > 126)) {
274 err |= RFC822_HDR_BAD_NAME_CHARS;
282 struct bytestring rfc822_header_raw_content(struct rfc822_msg *msg,
283 struct rfc822_header *hdr)
288 struct bytestring rfc822_header_raw_name(struct rfc822_msg *msg,
289 struct rfc822_header *hdr)
294 struct bytestring rfc822_header_raw_value(struct rfc822_msg *msg,
295 struct rfc822_header *hdr)
297 return hdr->rawvalue;
300 static void get_line(struct bytestring in, struct bytestring *first,
301 struct bytestring *rest)
303 size_t rawlen, trimlen;
304 const char *inp = in.ptr;
307 nl = memchr(inp, '\n', in.len);
311 rawlen = nl - inp + 1;
314 if ((trimlen > 0) && (inp[trimlen-1] == '\n')) {
316 if ((trimlen > 0) && (inp[trimlen-1] == '\r'))
320 *first = bytestring(in.ptr, trimlen);
323 *rest = bytestring(in.ptr + rawlen, in.len - rawlen);
325 *rest = bytestring_NULL;
329 struct bytestring rfc822_header_unfolded_value(struct rfc822_msg *msg,
330 struct rfc822_header *hdr)
332 struct bytestring raw = rfc822_header_raw_value(msg, hdr);
333 struct bytestring next, rest;
337 if (!hdr->unfolded.ptr) {
340 get_line(rest, &next, &rest);
346 hdr->unfolded = bytestring(raw.ptr, len);
348 char *unfold = tal_arr(msg, char, len);
351 ALLOC_CHECK(unfold, bytestring_NULL);
355 get_line(rest, &next, &rest);
356 memcpy(p, next.ptr, next.len);
360 assert(p == (unfold + len));
361 hdr->unfolded = bytestring(unfold, len);
365 return hdr->unfolded;
368 /* Specifically locale *un*aware tolower() - headers should be ascii
369 * only, and if they're not best to leave them as is */
370 static char xtolower(char c)
372 if ((c >= 'A') && (c <= 'Z'))
373 return 'a' + (c - 'A');
378 static bool hdr_name_eq(struct bytestring a, struct bytestring b)
385 for (i = 0; i < a.len; i++)
386 if (xtolower(a.ptr[i]) != xtolower(b.ptr[i]))
392 bool rfc822_header_is(struct rfc822_msg *msg, struct rfc822_header *hdr,
395 struct bytestring hname = rfc822_header_raw_name(msg, hdr);
397 if (!hname.ptr || !name)
400 return hdr_name_eq(hname, bytestring_from_string(name));
403 static unsigned headerhash(struct bytestring name)
406 * This is stolen from hash_string() in ccan/hash, but adapted
407 * to add the xtolower() call and use a bytestring
412 for (i = 0; i < name.len; i++)
413 ret = (ret << 5) - ret + xtolower(name.ptr[i]);
415 return ret % INDEX_HASH_SIZE;
418 static struct rfc822_headers_of_name *headers_of_name(struct rfc822_msg *msg,
419 struct bytestring name)
421 unsigned hash = headerhash(name);
422 struct rfc822_headers_of_name *hn;
424 list_for_each(&msg->header_index[hash], hn, bucket) {
425 if (hdr_name_eq(hn->name, name))
432 static struct rfc822_header *index_header(struct rfc822_msg *msg,
433 struct rfc822_header *hdr)
435 struct bytestring hname = rfc822_header_raw_name(msg, hdr);
436 struct rfc822_headers_of_name *hn = headers_of_name(msg, hname);
439 unsigned hash = headerhash(hname);
441 hn = talz(msg, struct rfc822_headers_of_name);
442 ALLOC_CHECK(hn, NULL);
446 hn->lastptr = &hn->first;
447 list_add_tail(&msg->header_index[hash], &hn->bucket);
450 hdr->name_next = NULL;
451 *(hn->lastptr) = hdr;
452 hn->lastptr = &hdr->name_next;
456 struct rfc822_header *rfc822_first_header_of_name(struct rfc822_msg *msg,
459 struct bytestring namebs = bytestring_from_string(name);
460 struct rfc822_headers_of_name *hn = headers_of_name(msg, namebs);
461 struct rfc822_header *hdr;
467 hdr = next_header_parse(msg);
468 if (hdr && rfc822_header_is(msg, hdr, name))
475 struct rfc822_header *rfc822_next_header_of_name(struct rfc822_msg *msg,
476 struct rfc822_header *hdr,
480 return rfc822_first_header_of_name(msg, name);
482 if (hdr->name_next) {
483 assert(rfc822_header_is(msg, hdr->name_next, name));
484 return hdr->name_next;
488 hdr = next_header_parse(msg);
489 if (hdr && rfc822_header_is(msg, hdr, name))