1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
7 #include <ccan/str/str.h>
8 #include <ccan/talloc/talloc.h>
9 #include <ccan/list/list.h>
11 #include <ccan/rfc822/rfc822.h>
14 void *memmem(const void *haystack, size_t haystacklen,
15 const void *needle, size_t needlelen)
20 last = p + haystacklen - needlelen;
23 if (memcmp(p, needle, needlelen) == 0)
25 } while (p++ <= last);
31 static void (*allocation_failure_hook)(const char *);
33 static void NORETURN default_allocation_failure(const char *s)
35 fprintf(stderr, "ccan/rfc822: Allocation failure: %s", s);
39 static void allocation_failure(const char *s)
41 if (allocation_failure_hook)
42 (*allocation_failure_hook)(s);
44 default_allocation_failure(s);
47 void rfc822_set_allocation_failure_handler(void (*h)(const char *))
49 allocation_failure_hook = h;
52 #define ALLOC_CHECK(p, r) \
55 allocation_failure(__FILE__ ":" stringify(__LINE__)); \
61 * No real point doing fancy resizing hashes, when any given mail
62 * message is unlikely to have more than a fairly small number of
63 * distinct header types. This should be ample.
65 #define INDEX_HASH_SIZE 63
68 const char *data, *end;
69 const char *remainder;
70 struct list_head headers;
71 struct list_head header_index[INDEX_HASH_SIZE];
75 struct rfc822_header {
76 struct bytestring all, rawname, rawvalue;
77 struct bytestring unfolded;
78 struct list_node list;
79 struct rfc822_header *name_next;
82 struct rfc822_headers_of_name {
83 struct bytestring name;
84 struct rfc822_header *first;
85 struct rfc822_header **lastptr;
86 struct list_node bucket;
89 struct rfc822_msg *rfc822_check(const struct rfc822_msg *msg,
93 if (!list_check(&msg->headers, abortstr))
95 return (struct rfc822_msg *)msg;
98 #ifdef CCAN_RFC822_DEBUG
99 #define CHECK(msg, str) do { rfc822_check((msg), (str)); } while (0)
101 #define CHECK(msg, str) do { } while (0)
104 struct rfc822_msg *rfc822_start(const void *ctx, const char *p, size_t len)
106 struct rfc822_msg *msg;
109 msg = talloc(ctx, struct rfc822_msg);
110 ALLOC_CHECK(msg, NULL);
115 msg->remainder = msg->data;
118 list_head_init(&msg->headers);
120 for (i = 0; i < INDEX_HASH_SIZE; i++)
121 list_head_init(&msg->header_index[i]);
123 CHECK(msg, "<rfc22_start");
128 void rfc822_free(struct rfc822_msg *msg)
130 CHECK(msg, ">rfc822_free");
134 static struct rfc822_header *next_header_cached(struct rfc822_msg *msg,
135 struct rfc822_header *hdr)
137 struct list_node *h = &msg->headers.n;
138 const struct list_node *n = h;
140 CHECK(msg, ">next_header_cached");
148 CHECK(msg, "<next_header_cached");
150 return list_entry(n->next, struct rfc822_header, list);
153 static const char *next_line(const char *start, const char *end)
155 const char *p = memchr(start, '\n', end - start);
157 return p ? (p + 1) : end;
160 static struct rfc822_header *index_header(struct rfc822_msg *msg,
161 struct rfc822_header *hdr);
163 static struct rfc822_header *next_header_parse(struct rfc822_msg *msg)
165 const char *h, *eh, *ev, *colon;
166 struct rfc822_header *hi;
168 CHECK(msg, ">next_header_parse");
173 if (msg->body && (msg->remainder >= msg->body))
177 eh = next_line(h, msg->end);
180 if ((ev > h) && (ev[-1] == '\n'))
182 if ((ev > h) && (ev[-1] == '\r'))
185 /* Found the end of the headers */
187 assert(!msg->body || (msg->body == eh));
194 while ((eh < msg->end) && rfc822_iswsp(*eh))
195 eh = next_line(eh, msg->end);
198 msg->remainder = NULL;
203 hi = talloc_zero(msg, struct rfc822_header);
204 ALLOC_CHECK(hi, NULL);
206 hi->all = bytestring(h, eh - h);
207 list_add_tail(&msg->headers, &hi->list);
209 colon = memchr(h, ':', hi->all.len);
211 hi->rawname = bytestring(h, colon - h);
212 hi->rawvalue = bytestring(colon + 1, eh - colon - 1);
214 hi->rawname = bytestring_NULL;
215 hi->rawvalue = bytestring_NULL;
218 CHECK(msg, "<next_header_parse");
220 return index_header(msg, hi);
223 struct rfc822_header *rfc822_next_header(struct rfc822_msg *msg,
224 struct rfc822_header *hdr)
226 struct rfc822_header *h;
228 CHECK(msg, ">rfc822_next_header");
230 h = next_header_cached(msg, hdr);
234 return next_header_parse(msg);
237 struct bytestring rfc822_body(struct rfc822_msg *msg)
239 CHECK(msg, ">rfc822_body");
241 if (!msg->body && msg->remainder) {
244 p = memmem(msg->remainder, msg->end - msg->remainder,
246 q = memmem(msg->remainder, msg->end - msg->remainder,
249 if (p && (!q || (p < q)))
251 else if (q && (!p || (q < p)))
254 if (msg->body >= msg->end) {
255 assert(msg->body == msg->end);
260 CHECK(msg, "<rfc822_body");
263 return bytestring(msg->body, msg->end - msg->body);
265 return bytestring_NULL;
268 enum rfc822_header_errors rfc822_header_errors(struct rfc822_msg *msg,
269 struct rfc822_header *hdr)
271 enum rfc822_header_errors err = 0;
274 if (!hdr->rawname.ptr) {
275 err |= RFC822_HDR_NO_COLON;
277 for (i = 0; i < hdr->rawname.len; i++) {
278 char c = hdr->rawname.ptr[i];
282 if ((c < 33) || (c > 126)) {
283 err |= RFC822_HDR_BAD_NAME_CHARS;
291 struct bytestring rfc822_header_raw_content(struct rfc822_msg *msg,
292 struct rfc822_header *hdr)
297 struct bytestring rfc822_header_raw_name(struct rfc822_msg *msg,
298 struct rfc822_header *hdr)
303 struct bytestring rfc822_header_raw_value(struct rfc822_msg *msg,
304 struct rfc822_header *hdr)
306 return hdr->rawvalue;
309 static void get_line(struct bytestring in, struct bytestring *first,
310 struct bytestring *rest)
312 size_t rawlen, trimlen;
313 const char *inp = in.ptr;
316 nl = memchr(inp, '\n', in.len);
320 rawlen = nl - inp + 1;
323 if ((trimlen > 0) && (inp[trimlen-1] == '\n')) {
325 if ((trimlen > 0) && (inp[trimlen-1] == '\r'))
329 *first = bytestring(in.ptr, trimlen);
332 *rest = bytestring(in.ptr + rawlen, in.len - rawlen);
334 *rest = bytestring_NULL;
338 struct bytestring rfc822_header_unfolded_value(struct rfc822_msg *msg,
339 struct rfc822_header *hdr)
341 struct bytestring raw = rfc822_header_raw_value(msg, hdr);
342 struct bytestring next, rest;
346 if (!hdr->unfolded.ptr) {
349 get_line(rest, &next, &rest);
355 hdr->unfolded = bytestring(raw.ptr, len);
357 char *unfold = talloc_array(msg, char, len);
360 ALLOC_CHECK(unfold, bytestring_NULL);
364 get_line(rest, &next, &rest);
365 memcpy(p, next.ptr, next.len);
369 assert(p == (unfold + len));
370 hdr->unfolded = bytestring(unfold, len);
374 return hdr->unfolded;
377 /* Specifically locale *un*aware tolower() - headers should be ascii
378 * only, and if they're not best to leave them as is */
379 static char xtolower(char c)
381 if ((c >= 'A') && (c <= 'Z'))
382 return 'a' + (c - 'A');
387 static bool hdr_name_eq(struct bytestring a, struct bytestring b)
394 for (i = 0; i < a.len; i++)
395 if (xtolower(a.ptr[i]) != xtolower(b.ptr[i]))
401 bool rfc822_header_is(struct rfc822_msg *msg, struct rfc822_header *hdr,
404 struct bytestring hname = rfc822_header_raw_name(msg, hdr);
406 if (!hname.ptr || !name)
409 return hdr_name_eq(hname, bytestring_from_string(name));
412 static unsigned headerhash(struct bytestring name)
415 * This is stolen from hash_string() in ccan/hash, but adapted
416 * to add the xtolower() call and use a bytestring
421 for (i = 0; i < name.len; i++)
422 ret = (ret << 5) - ret + xtolower(name.ptr[i]);
424 return ret % INDEX_HASH_SIZE;
427 static struct rfc822_headers_of_name *headers_of_name(struct rfc822_msg *msg,
428 struct bytestring name)
430 unsigned hash = headerhash(name);
431 struct rfc822_headers_of_name *hn;
433 list_for_each(&msg->header_index[hash], hn, bucket) {
434 if (hdr_name_eq(hn->name, name))
441 static struct rfc822_header *index_header(struct rfc822_msg *msg,
442 struct rfc822_header *hdr)
444 struct bytestring hname = rfc822_header_raw_name(msg, hdr);
445 struct rfc822_headers_of_name *hn = headers_of_name(msg, hname);
448 unsigned hash = headerhash(hname);
450 hn = talloc_zero(msg, struct rfc822_headers_of_name);
451 ALLOC_CHECK(hn, NULL);
455 hn->lastptr = &hn->first;
456 list_add_tail(&msg->header_index[hash], &hn->bucket);
459 hdr->name_next = NULL;
460 *(hn->lastptr) = hdr;
461 hn->lastptr = &hdr->name_next;
465 struct rfc822_header *rfc822_first_header_of_name(struct rfc822_msg *msg,
468 struct bytestring namebs = bytestring_from_string(name);
469 struct rfc822_headers_of_name *hn = headers_of_name(msg, namebs);
470 struct rfc822_header *hdr;
476 hdr = next_header_parse(msg);
477 if (hdr && rfc822_header_is(msg, hdr, name))
484 struct rfc822_header *rfc822_next_header_of_name(struct rfc822_msg *msg,
485 struct rfc822_header *hdr,
489 return rfc822_first_header_of_name(msg, name);
491 if (hdr->name_next) {
492 assert(rfc822_header_is(msg, hdr->name_next, name));
493 return hdr->name_next;
497 hdr = next_header_parse(msg);
498 if (hdr && rfc822_header_is(msg, hdr, name))