chiark / gitweb /
Split base64 code into a separate .c so that the server doesn't pull
[disorder] / lib / mime.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2005, 2007 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 /** @file lib/mime.c
21  * @brief Support for MIME and allied protocols
22  */
23
24 #include <config.h>
25 #include "types.h"
26
27 #include <string.h>
28 #include <ctype.h>
29
30 #include <stdio.h>
31
32 #include "mem.h"
33 #include "mime.h"
34 #include "vector.h"
35 #include "hex.h"
36 #include "log.h"
37 #include "base64.h"
38
39 /** @brief Match whitespace characters */
40 static int whitespace(int c) {
41   switch(c) {
42   case ' ':
43   case '\t':
44   case '\r':
45   case '\n':
46     return 1;
47   default:
48     return 0;
49   }
50 }
51
52 /** @brief Match RFC2045 tspecial characters */
53 static int tspecial(int c) {
54   switch(c) {
55   case '(':
56   case ')':
57   case '<':
58   case '>':
59   case '@':
60   case ',':
61   case ';':
62   case ':':
63   case '\\':
64   case '"':
65   case '/':
66   case '[':
67   case ']':
68   case '?':
69   case '=':
70     return 1;
71   default:
72     return 0;
73   }
74 }
75
76 /** @brief Match RFC2616 seprator characters */
77 static int http_separator(int c) {
78   switch(c) {
79   case '(':
80   case ')':
81   case '<':
82   case '>':
83   case '@':
84   case ',':
85   case ';':
86   case ':':
87   case '\\':
88   case '"':
89   case '/':
90   case '[':
91   case ']':
92   case '?':
93   case '=':
94   case '{':
95   case '}':
96   case ' ':
97   case '\t':
98     return 1;
99   default:
100     return 0;
101   }
102 }
103
104 /** @brief Match CRLF */
105 static int iscrlf(const char *ptr) {
106   return ptr[0] == '\r' && ptr[1] == '\n';
107 }
108
109 /** @brief Skip whitespace
110  * @param rfc822_comments If true, skip RFC822 nested comments
111  */
112 static const char *skipwhite(const char *s, int rfc822_comments) {
113   int c, depth;
114   
115   for(;;) {
116     switch(c = *s) {
117     case ' ':
118     case '\t':
119     case '\r':
120     case '\n':
121       ++s;
122       break;
123     case '(':
124       if(!rfc822_comments)
125         return s;
126       ++s;
127       depth = 1;
128       while(*s && depth) {
129         c = *s++;
130         switch(c) {
131         case '(': ++depth; break;
132         case ')': --depth; break;
133         case '\\':
134           if(!*s) return 0;
135           ++s;
136           break;
137         }
138       }
139       if(depth) return 0;
140       break;
141     default:
142       return s;
143     }
144   }
145 }
146
147 /** @brief Test for a word character
148  * @param c Character to test
149  * @param special tspecial() (MIME/RFC2405) or http_separator() (HTTP/RFC2616)
150  * @return 1 if @p c is a word character, else 0
151  */
152 static int iswordchar(int c, int (*special)(int)) {
153   return !(c <= ' ' || c > '~' || special(c));
154 }
155
156 /** @brief Parse an RFC1521/RFC2616 word
157  * @param s Pointer to start of word
158  * @param valuep Where to store value
159  * @param special tspecial() (MIME/RFC2405) or http_separator() (HTTP/RFC2616)
160  * @return Pointer just after end of word or NULL if there's no word
161  *
162  * A word is a token or a quoted-string.
163  */
164 static const char *parseword(const char *s, char **valuep,
165                              int (*special)(int)) {
166   struct dynstr value[1];
167   int c;
168
169   dynstr_init(value);
170   if(*s == '"') {
171     ++s;
172     while((c = *s++) != '"') {
173       switch(c) {
174       case '\\':
175         if(!(c = *s++)) return 0;
176       default:
177         dynstr_append(value, c);
178         break;
179       }
180     }
181     if(!c) return 0;
182   } else {
183     if(!iswordchar((unsigned char)*s, special))
184       return NULL;
185     dynstr_init(value);
186     while(iswordchar((unsigned char)*s, special))
187       dynstr_append(value, *s++);
188   }
189   dynstr_terminate(value);
190   *valuep = value->vec;
191   return s;
192 }
193
194 /** @brief Parse an RFC1521/RFC2616 token
195  * @param s Pointer to start of token
196  * @param valuep Where to store value
197  * @param special tspecial() (MIME/RFC2405) or http_separator() (HTTP/RFC2616)
198  * @return Pointer just after end of token or NULL if there's no token
199  */
200 static const char *parsetoken(const char *s, char **valuep,
201                               int (*special)(int)) {
202   if(*s == '"') return 0;
203   return parseword(s, valuep, special);
204 }
205
206 /** @brief Parse a MIME content-type field
207  * @param s Start of field
208  * @param typep Where to store type
209  * @param parameternamep Where to store parameter name
210  * @param parameternvaluep Wher to store parameter value
211  * @return 0 on success, non-0 on error
212  */
213 int mime_content_type(const char *s,
214                       char **typep,
215                       char **parameternamep,
216                       char **parametervaluep) {
217   struct dynstr type, parametername;
218
219   dynstr_init(&type);
220   if(!(s = skipwhite(s, 1))) return -1;
221   if(!*s) return -1;
222   while(*s && !tspecial(*s) && !whitespace(*s))
223     dynstr_append(&type, tolower((unsigned char)*s++));
224   if(!(s = skipwhite(s, 1))) return -1;
225   if(*s++ != '/') return -1;
226   dynstr_append(&type, '/');
227   if(!(s = skipwhite(s, 1))) return -1;
228   while(*s && !tspecial(*s) && !whitespace(*s))
229     dynstr_append(&type, tolower((unsigned char)*s++));
230   if(!(s = skipwhite(s, 1))) return -1;
231
232   if(*s == ';') {
233     dynstr_init(&parametername);
234     ++s;
235     if(!(s = skipwhite(s, 1))) return -1;
236     if(!*s) return -1;
237     while(*s && !tspecial(*s) && !whitespace(*s))
238       dynstr_append(&parametername, tolower((unsigned char)*s++));
239     if(!(s = skipwhite(s, 1))) return -1;
240     if(*s++ != '=') return -1;
241     if(!(s = skipwhite(s, 1))) return -1;
242     if(!(s = parseword(s, parametervaluep, tspecial))) return -1;
243     if(!(s = skipwhite(s, 1))) return -1;
244     dynstr_terminate(&parametername);
245     *parameternamep = parametername.vec;
246   } else
247     *parametervaluep = *parameternamep = 0;
248   dynstr_terminate(&type);
249   *typep = type.vec;
250   return 0;
251 }
252
253 /** @brief Parse a MIME message
254  * @param s Start of message
255  * @param callback Called for each header field
256  * @param u Passed to callback
257  * @return Pointer to decoded body (might be in original string)
258  */
259 const char *mime_parse(const char *s,
260                        int (*callback)(const char *name, const char *value,
261                                        void *u),
262                        void *u) {
263   struct dynstr name, value;
264   char *cte = 0, *p;
265   
266   while(*s && !iscrlf(s)) {
267     dynstr_init(&name);
268     dynstr_init(&value);
269     while(*s && !tspecial(*s) && !whitespace(*s))
270       dynstr_append(&name, tolower((unsigned char)*s++));
271     if(!(s = skipwhite(s, 1))) return 0;
272     if(*s != ':') return 0;
273     ++s;
274     while(*s && !(*s == '\n' && !(s[1] == ' ' || s[1] == '\t')))
275       dynstr_append(&value, *s++);
276     if(*s) ++s;
277     dynstr_terminate(&name);
278     dynstr_terminate(&value);
279     if(!strcmp(name.vec, "content-transfer-encoding")) {
280       cte = xstrdup(value.vec);
281       for(p = cte; *p; p++)
282         *p = tolower((unsigned char)*p);
283     }
284     if(callback(name.vec, value.vec, u)) return 0;
285   }
286   if(*s) s += 2;
287   if(cte) {
288     if(!strcmp(cte, "base64")) return mime_base64(s, 0);
289     if(!strcmp(cte, "quoted-printable")) return mime_qp(s);
290   }
291   return s;
292 }
293
294 static int isboundary(const char *ptr, const char *boundary, size_t bl) {
295   return (ptr[0] == '-'
296           && ptr[1] == '-'
297           && !strncmp(ptr + 2, boundary, bl)
298           && (iscrlf(ptr + bl + 2)
299               || (ptr[bl + 2] == '-'
300                   && ptr[bl + 3] == '-'
301                   && (iscrlf(ptr + bl + 4) || *(ptr + bl + 4) == 0))));
302 }
303
304 static int isfinal(const char *ptr, const char *boundary, size_t bl) {
305   return (ptr[0] == '-'
306           && ptr[1] == '-'
307           && !strncmp(ptr + 2, boundary, bl)
308           && ptr[bl + 2] == '-'
309           && ptr[bl + 3] == '-'
310           && (iscrlf(ptr + bl + 4) || *(ptr + bl + 4) == 0));
311 }
312
313 /** @brief Parse a multipart MIME body
314  * @param s Start of message
315  * @param callback CAllback for each part
316  * @param boundary Boundary string
317  * @param u Passed to callback
318  * @return 0 on success, non-0 on error
319  */
320 int mime_multipart(const char *s,
321                    int (*callback)(const char *s, void *u),
322                    const char *boundary,
323                    void *u) {
324   size_t bl = strlen(boundary);
325   const char *start, *e;
326   int ret;
327
328   /* We must start with a boundary string */
329   if(!isboundary(s, boundary, bl))
330     return -1;
331   /* Keep going until we hit a final boundary */
332   while(!isfinal(s, boundary, bl)) {
333     s = strstr(s, "\r\n") + 2;
334     start = s;
335     while(!isboundary(s, boundary, bl)) {
336       if(!(e = strstr(s, "\r\n")))
337         return -1;
338       s = e + 2;
339     }
340     if((ret = callback(xstrndup(start,
341                                 s == start ? 0 : s - start - 2),
342                        u)))
343       return ret;
344   }
345   return 0;
346 }
347
348 /** @brief Parse an RFC2388-style content-disposition field
349  * @param s Start of field
350  * @param typep Where to store type
351  * @param parameternamep Where to store parameter name
352  * @param parameternvaluep Wher to store parameter value
353  * @return 0 on success, non-0 on error
354  */
355 int mime_rfc2388_content_disposition(const char *s,
356                                      char **dispositionp,
357                                      char **parameternamep,
358                                      char **parametervaluep) {
359   struct dynstr disposition, parametername;
360
361   dynstr_init(&disposition);
362   if(!(s = skipwhite(s, 1))) return -1;
363   if(!*s) return -1;
364   while(*s && !tspecial(*s) && !whitespace(*s))
365     dynstr_append(&disposition, tolower((unsigned char)*s++));
366   if(!(s = skipwhite(s, 1))) return -1;
367
368   if(*s == ';') {
369     dynstr_init(&parametername);
370     ++s;
371     if(!(s = skipwhite(s, 1))) return -1;
372     if(!*s) return -1;
373     while(*s && !tspecial(*s) && !whitespace(*s))
374       dynstr_append(&parametername, tolower((unsigned char)*s++));
375     if(!(s = skipwhite(s, 1))) return -1;
376     if(*s++ != '=') return -1;
377     if(!(s = skipwhite(s, 1))) return -1;
378     if(!(s = parseword(s, parametervaluep, tspecial))) return -1;
379     if(!(s = skipwhite(s, 1))) return -1;
380     dynstr_terminate(&parametername);
381     *parameternamep = parametername.vec;
382   } else
383     *parametervaluep = *parameternamep = 0;
384   dynstr_terminate(&disposition);
385   *dispositionp = disposition.vec;
386   return 0;
387 }
388
389 /** @brief Convert MIME quoted-printable
390  * @param s Quoted-printable data
391  * @return Decoded data
392  */
393 char *mime_qp(const char *s) {
394   struct dynstr d;
395   int c, a, b;
396   const char *t;
397
398   dynstr_init(&d);
399   while((c = *s++)) {
400     switch(c) {
401     case '=':
402       if((a = unhexdigitq(s[0])) != -1
403          && (b = unhexdigitq(s[1])) != -1) {
404         dynstr_append(&d, a * 16 + b);
405         s += 2;
406       } else {
407         t = s;
408         while(*t == ' ' || *t == '\t') ++t;
409         if(iscrlf(t)) {
410           /* soft line break */
411           s = t + 2;
412         } else
413           return 0;
414       }
415       break;
416     case ' ':
417     case '\t':
418       t = s;
419       while(*t == ' ' || *t == '\t') ++t;
420       if(iscrlf(t))
421         /* trailing space is always eliminated */
422         s = t;
423       else
424         dynstr_append(&d, c);
425       break;
426     default:
427       dynstr_append(&d, c);
428       break;
429     }
430   }
431   dynstr_terminate(&d);
432   return d.vec;
433 }
434
435 /** @brief Parse a RFC2109 Cookie: header
436  * @param s Header field value
437  * @param cd Where to store result
438  * @return 0 on success, non-0 on error
439  */
440 int parse_cookie(const char *s,
441                  struct cookiedata *cd) {
442   char *n = 0, *v = 0;
443
444   memset(cd, 0, sizeof *cd);
445   s = skipwhite(s, 0);
446   while(*s) {
447     /* Skip separators */
448     if(*s == ';' || *s == ',') {
449       ++s;
450       s = skipwhite(s, 0);
451       continue;
452     }
453     if(!(s = parsetoken(s, &n, http_separator))) return -1;
454     s = skipwhite(s, 0);
455     if(*s++ != '=') return -1;
456     s = skipwhite(s, 0);
457     if(!(s = parseword(s, &v, http_separator))) return -1;
458     if(n[0] == '$') {
459       /* Some bit of meta-information */
460       if(!strcmp(n, "$Version"))
461         cd->version = v;
462       else if(!strcmp(n, "$Path")) {
463         if(cd->ncookies > 0 && cd->cookies[cd->ncookies-1].path == 0)
464           cd->cookies[cd->ncookies-1].path = v;
465         else {
466           error(0, "redundant $Path in Cookie: header");
467           return -1;
468         }
469       } else if(!strcmp(n, "$Domain")) {
470         if(cd->ncookies > 0 && cd->cookies[cd->ncookies-1].domain == 0)
471           cd->cookies[cd->ncookies-1].domain = v;
472         else {
473           error(0, "redundant $Domain in Cookie: header");
474           return -1;
475         }
476       }
477     } else {
478       /* It's a new cookie */
479       cd->cookies = xrealloc(cd->cookies,
480                              (cd->ncookies + 1) * sizeof (struct cookie));
481       cd->cookies[cd->ncookies].name = n;
482       cd->cookies[cd->ncookies].value = v;
483       cd->cookies[cd->ncookies].path = 0;
484       cd->cookies[cd->ncookies].domain = 0;
485       ++cd->ncookies;
486     }
487     s = skipwhite(s, 0);
488     if(*s && (*s != ',' && *s != ';')) {
489       error(0, "missing separator in Cookie: header");
490       return -1;
491     }
492   }
493   return 0;
494 }
495
496 /** @brief Find a named cookie
497  * @param cd Parse cookie data
498  * @param name Name of cookie
499  * @return Cookie structure or NULL if not found
500  */
501 const struct cookie *find_cookie(const struct cookiedata *cd,
502                                  const char *name) {
503   int n;
504
505   for(n = 0; n < cd->ncookies; ++n)
506     if(!strcmp(cd->cookies[n].name, name))
507       return &cd->cookies[n];
508   return 0;
509 }
510
511 /*
512 Local Variables:
513 c-basic-offset:2
514 comment-column:40
515 fill-column:79
516 End:
517 */