]> git.ozlabs.org Git - ccan/blob - ccan/rfc822/rfc822.c
rfc822: new module.
[ccan] / ccan / rfc822 / rfc822.c
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2
3 #include "config.h"
4
5 #include <string.h>
6
7 #include <ccan/str/str.h>
8 #include <ccan/talloc/talloc.h>
9 #include <ccan/list/list.h>
10
11 #include <ccan/rfc822/rfc822.h>
12
13 #if !HAVE_MEMMEM
14 void *memmem(const void *haystack, size_t haystacklen,
15              const void *needle, size_t needlelen)
16 {
17         const char *p, *last;
18
19         p = haystack;
20         last = p + haystacklen - needlelen;
21
22         do {
23                 if (memcmp(p, needle, needlelen) == 0)
24                         return (void *)p;
25         } while (p++ <= last);
26
27         return NULL;
28 }
29 #endif
30
31 static void (*allocation_failure_hook)(const char *);
32
33 static void NORETURN default_allocation_failure(const char *s)
34 {
35         fprintf(stderr, "ccan/rfc822: Allocation failure: %s", s);
36         abort();
37 }
38
39 static void allocation_failure(const char *s)
40 {
41         if (allocation_failure_hook)
42                 (*allocation_failure_hook)(s);
43         else
44                 default_allocation_failure(s);
45 }
46
47 void rfc822_set_allocation_failure_handler(void (*h)(const char *))
48 {
49         allocation_failure_hook = h;
50 }
51
52 #define ALLOC_CHECK(p, r) \
53         do { \
54                 if (!(p)) { \
55                         allocation_failure(__FILE__ ":" stringify(__LINE__)); \
56                         return (r); \
57                 } \
58         } while (0)
59
60 struct rfc822_msg {
61         const char *data, *end;
62         const char *remainder;
63         struct list_head headers;
64         const char *body;
65 };
66
67 struct rfc822_header {
68         struct bytestring all, rawname, rawvalue;
69         struct bytestring unfolded;
70         struct list_node list;
71 };
72
73 struct rfc822_msg *rfc822_check(const struct rfc822_msg *msg,
74                                 const char *abortstr)
75 {
76         assert(msg);
77         if (!list_check(&msg->headers, abortstr))
78                 return NULL;
79         return (struct rfc822_msg *)msg;
80 }
81
82 #ifdef CCAN_RFC822_DEBUG
83 #define CHECK(msg, str) do { rfc822_check((msg), (str)); } while (0)
84 #else
85 #define CHECK(msg, str) do { } while (0)
86 #endif
87
88 struct rfc822_msg *rfc822_start(const void *ctx, const char *p, size_t len)
89 {
90         struct rfc822_msg *msg;
91
92         msg = talloc(ctx, struct rfc822_msg);
93         ALLOC_CHECK(msg, NULL);
94
95         msg->data = p;
96         msg->end = p + len;
97
98         msg->remainder = msg->data;
99         msg->body = NULL;
100
101         list_head_init(&msg->headers);
102
103         CHECK(msg, "<rfc22_start");
104
105         return msg;
106 }
107
108 void rfc822_free(struct rfc822_msg *msg)
109 {
110         CHECK(msg, ">rfc822_free");
111         talloc_free(msg);
112 }
113
114 static struct rfc822_header *next_header_cached(struct rfc822_msg *msg,
115                                                 struct rfc822_header *hdr)
116 {
117         struct list_node *h = &msg->headers.n;
118         const struct list_node *n = h;
119
120         CHECK(msg, ">next_header_cached");
121
122         if (hdr)
123                 n = &hdr->list;
124
125         if (n->next == h)
126                 return NULL;
127
128         CHECK(msg, "<next_header_cached");
129
130         return list_entry(n->next, struct rfc822_header, list);
131 }
132
133 static const char *next_line(const char *start, const char *end)
134 {
135         const char *p = memchr(start, '\n', end - start);
136
137         return p ? (p + 1) : end;
138 }
139
140 static struct rfc822_header *next_header_parse(struct rfc822_msg *msg)
141 {
142         const char *h, *eh, *ev, *colon;
143         struct rfc822_header *hi;
144
145         CHECK(msg, ">next_header_parse");
146
147         if (!msg->remainder)
148                 return NULL;
149
150         if (msg->body && (msg->remainder >= msg->body))
151                 return NULL;
152
153         eh = h = msg->remainder;
154         do {
155                 eh = next_line(eh, msg->end);
156         } while ((eh < msg->end) && rfc822_iswsp(*eh));
157
158         if (eh >= msg->end)
159                 msg->remainder = NULL;
160         else
161                 msg->remainder = eh;
162
163         ev = eh;
164         if ((ev > h) && (ev[-1] == '\n'))
165                 ev--;
166         if ((ev > h) && (ev[-1] == '\r'))
167                 ev--;
168
169         if (ev == h) {
170                 /* Found the end of the headers */
171                 if (eh < msg->end)
172                         msg->body = eh;
173                 return NULL;
174         }
175
176         hi = talloc_zero(msg, struct rfc822_header);
177         ALLOC_CHECK(hi, NULL);
178
179         hi->all = bytestring(h, eh - h);
180         list_add_tail(&msg->headers, &hi->list);
181
182         colon = memchr(h, ':', hi->all.len);
183         if (colon) {
184                 hi->rawname = bytestring(h, colon - h);
185                 hi->rawvalue = bytestring(colon + 1, eh - colon - 1);
186         } else {
187                 hi->rawname = bytestring_NULL;
188                 hi->rawvalue = bytestring_NULL;
189         }
190
191         CHECK(msg, "<next_header_parse");
192
193         return hi;
194 }
195
196 struct rfc822_header *rfc822_next_header(struct rfc822_msg *msg,
197                                          struct rfc822_header *hdr)
198 {
199         struct rfc822_header *h;
200
201         CHECK(msg, ">rfc822_next_header");
202
203         h = next_header_cached(msg, hdr);
204         if (h)
205                 return h;
206
207         return next_header_parse(msg);
208 }
209
210 struct bytestring rfc822_body(struct rfc822_msg *msg)
211 {
212         CHECK(msg, ">rfc822_body");
213
214         if (!msg->body && msg->remainder) {
215                 const char *p, *q;
216
217                 p = memmem(msg->remainder, msg->end - msg->remainder,
218                            "\n\r\n", 3);
219                 q = memmem(msg->remainder, msg->end - msg->remainder,
220                            "\n\n", 2);
221
222                 if (p && (!q || (p < q)))
223                         msg->body = p + 3;
224                 else if (q && (!p || (q < p)))
225                         msg->body = q + 2;
226
227                 if (msg->body >= msg->end) {
228                         assert(msg->body == msg->end);
229                         msg->body = NULL;
230                 }
231         }
232
233         CHECK(msg, "<rfc822_body");
234
235         if (msg->body)
236                 return bytestring(msg->body, msg->end - msg->body);
237         else
238                 return bytestring_NULL;
239 }
240
241 enum rfc822_header_errors rfc822_header_errors(struct rfc822_msg *msg,
242                                                struct rfc822_header *hdr)
243 {
244         enum rfc822_header_errors err = 0;
245         int i;
246
247         if (!hdr->rawname.ptr) {
248                 err |= RFC822_HDR_NO_COLON;
249         } else {
250                 for (i = 0; i < hdr->rawname.len; i++) {
251                         char c = hdr->rawname.ptr[i];
252
253                         assert(c != ':');
254
255                         if ((c < 33) || (c > 126)) {
256                                 err |= RFC822_HDR_BAD_NAME;
257                                 break;
258                         }
259                 }
260         }
261         return err;
262 }
263
264 struct bytestring rfc822_header_raw_content(struct rfc822_msg *msg,
265                                             struct rfc822_header *hdr)
266 {
267         return hdr->all;
268 }
269
270 struct bytestring rfc822_header_raw_name(struct rfc822_msg *msg,
271                                          struct rfc822_header *hdr)
272 {
273         return hdr->rawname;
274 }
275
276 struct bytestring rfc822_header_raw_value(struct rfc822_msg *msg,
277                                           struct rfc822_header *hdr)
278 {
279         return hdr->rawvalue;
280 }
281
282 static void get_line(struct bytestring in, struct bytestring *first,
283                      struct bytestring *rest)
284 {
285         size_t rawlen, trimlen;
286         const char *inp = in.ptr;
287         const char *nl;
288
289         nl = memchr(inp, '\n', in.len);
290         if (!nl)
291                 rawlen = in.len;
292         else
293                 rawlen = nl - inp + 1;
294
295         trimlen = rawlen;
296         if ((trimlen > 0) && (inp[trimlen-1] == '\n')) {
297                 trimlen--;
298                 if ((trimlen > 0) && (inp[trimlen-1] == '\r'))
299                         trimlen--;
300         }
301
302         *first = bytestring(in.ptr, trimlen);
303
304         if (rawlen < in.len)
305                 *rest = bytestring(in.ptr + rawlen, in.len - rawlen);
306         else
307                 *rest = bytestring_NULL;
308 }
309
310
311 struct bytestring rfc822_header_unfolded_value(struct rfc822_msg *msg,
312                                                struct rfc822_header *hdr)
313 {
314         struct bytestring raw = rfc822_header_raw_value(msg, hdr);
315         struct bytestring next, rest;
316         int lines = 0;
317         size_t len = 0;
318
319         if (!hdr->unfolded.ptr) {
320                 rest = raw;
321                 while (rest.ptr) {
322                         get_line(rest, &next, &rest);
323                         lines++;
324                         len += next.len;
325                 }
326
327                 if (lines <= 1) {
328                         hdr->unfolded = bytestring(raw.ptr, len);
329                 } else {
330                         char *unfold = talloc_array(msg, char, len);
331                         char *p = unfold;
332
333                         ALLOC_CHECK(unfold, bytestring_NULL);
334
335                         rest = raw;
336                         while (rest.ptr) {
337                                 get_line(rest, &next, &rest);
338                                 memcpy(p, next.ptr, next.len);
339                                 p += next.len;
340                         }
341
342                         assert(p == (unfold + len));
343                         hdr->unfolded = bytestring(unfold, len);
344                 }
345         }
346
347         return hdr->unfolded;
348 }