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 const char *data, *end;
62 const char *remainder;
63 struct list_head headers;
67 struct rfc822_header {
68 struct bytestring all, rawname, rawvalue;
69 struct bytestring unfolded;
70 struct list_node list;
73 struct rfc822_msg *rfc822_check(const struct rfc822_msg *msg,
77 if (!list_check(&msg->headers, abortstr))
79 return (struct rfc822_msg *)msg;
82 #ifdef CCAN_RFC822_DEBUG
83 #define CHECK(msg, str) do { rfc822_check((msg), (str)); } while (0)
85 #define CHECK(msg, str) do { } while (0)
88 struct rfc822_msg *rfc822_start(const void *ctx, const char *p, size_t len)
90 struct rfc822_msg *msg;
92 msg = talloc(ctx, struct rfc822_msg);
93 ALLOC_CHECK(msg, NULL);
98 msg->remainder = msg->data;
101 list_head_init(&msg->headers);
103 CHECK(msg, "<rfc22_start");
108 void rfc822_free(struct rfc822_msg *msg)
110 CHECK(msg, ">rfc822_free");
114 static struct rfc822_header *next_header_cached(struct rfc822_msg *msg,
115 struct rfc822_header *hdr)
117 struct list_node *h = &msg->headers.n;
118 const struct list_node *n = h;
120 CHECK(msg, ">next_header_cached");
128 CHECK(msg, "<next_header_cached");
130 return list_entry(n->next, struct rfc822_header, list);
133 static const char *next_line(const char *start, const char *end)
135 const char *p = memchr(start, '\n', end - start);
137 return p ? (p + 1) : end;
140 static struct rfc822_header *next_header_parse(struct rfc822_msg *msg)
142 const char *h, *eh, *ev, *colon;
143 struct rfc822_header *hi;
145 CHECK(msg, ">next_header_parse");
150 if (msg->body && (msg->remainder >= msg->body))
153 eh = h = msg->remainder;
155 eh = next_line(eh, msg->end);
156 } while ((eh < msg->end) && rfc822_iswsp(*eh));
159 msg->remainder = NULL;
164 if ((ev > h) && (ev[-1] == '\n'))
166 if ((ev > h) && (ev[-1] == '\r'))
170 /* Found the end of the headers */
176 hi = talloc_zero(msg, struct rfc822_header);
177 ALLOC_CHECK(hi, NULL);
179 hi->all = bytestring(h, eh - h);
180 list_add_tail(&msg->headers, &hi->list);
182 colon = memchr(h, ':', hi->all.len);
184 hi->rawname = bytestring(h, colon - h);
185 hi->rawvalue = bytestring(colon + 1, eh - colon - 1);
187 hi->rawname = bytestring_NULL;
188 hi->rawvalue = bytestring_NULL;
191 CHECK(msg, "<next_header_parse");
196 struct rfc822_header *rfc822_next_header(struct rfc822_msg *msg,
197 struct rfc822_header *hdr)
199 struct rfc822_header *h;
201 CHECK(msg, ">rfc822_next_header");
203 h = next_header_cached(msg, hdr);
207 return next_header_parse(msg);
210 struct bytestring rfc822_body(struct rfc822_msg *msg)
212 CHECK(msg, ">rfc822_body");
214 if (!msg->body && msg->remainder) {
217 p = memmem(msg->remainder, msg->end - msg->remainder,
219 q = memmem(msg->remainder, msg->end - msg->remainder,
222 if (p && (!q || (p < q)))
224 else if (q && (!p || (q < p)))
227 if (msg->body >= msg->end) {
228 assert(msg->body == msg->end);
233 CHECK(msg, "<rfc822_body");
236 return bytestring(msg->body, msg->end - msg->body);
238 return bytestring_NULL;
241 enum rfc822_header_errors rfc822_header_errors(struct rfc822_msg *msg,
242 struct rfc822_header *hdr)
244 enum rfc822_header_errors err = 0;
247 if (!hdr->rawname.ptr) {
248 err |= RFC822_HDR_NO_COLON;
250 for (i = 0; i < hdr->rawname.len; i++) {
251 char c = hdr->rawname.ptr[i];
255 if ((c < 33) || (c > 126)) {
256 err |= RFC822_HDR_BAD_NAME;
264 struct bytestring rfc822_header_raw_content(struct rfc822_msg *msg,
265 struct rfc822_header *hdr)
270 struct bytestring rfc822_header_raw_name(struct rfc822_msg *msg,
271 struct rfc822_header *hdr)
276 struct bytestring rfc822_header_raw_value(struct rfc822_msg *msg,
277 struct rfc822_header *hdr)
279 return hdr->rawvalue;
282 static void get_line(struct bytestring in, struct bytestring *first,
283 struct bytestring *rest)
285 size_t rawlen, trimlen;
286 const char *inp = in.ptr;
289 nl = memchr(inp, '\n', in.len);
293 rawlen = nl - inp + 1;
296 if ((trimlen > 0) && (inp[trimlen-1] == '\n')) {
298 if ((trimlen > 0) && (inp[trimlen-1] == '\r'))
302 *first = bytestring(in.ptr, trimlen);
305 *rest = bytestring(in.ptr + rawlen, in.len - rawlen);
307 *rest = bytestring_NULL;
311 struct bytestring rfc822_header_unfolded_value(struct rfc822_msg *msg,
312 struct rfc822_header *hdr)
314 struct bytestring raw = rfc822_header_raw_value(msg, hdr);
315 struct bytestring next, rest;
319 if (!hdr->unfolded.ptr) {
322 get_line(rest, &next, &rest);
328 hdr->unfolded = bytestring(raw.ptr, len);
330 char *unfold = talloc_array(msg, char, len);
333 ALLOC_CHECK(unfold, bytestring_NULL);
337 get_line(rest, &next, &rest);
338 memcpy(p, next.ptr, next.len);
342 assert(p == (unfold + len));
343 hdr->unfolded = bytestring(unfold, len);
347 return hdr->unfolded;