2 * This file is part of DisOrder
3 * Copyright (C) 2005 Richard Kettlewell
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.
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.
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
33 static int whitespace(int c
) {
45 static int tspecial(int c
) {
68 static const char *skipwhite(const char *s
) {
85 case '(': ++depth
; break;
86 case ')': --depth
; break;
101 static const char *parsestring(const char *s
, char **valuep
) {
107 while((c
= *s
++) != '"') {
110 if(!(c
= *s
++)) return 0;
112 dynstr_append(&value
, c
);
117 dynstr_terminate(&value
);
122 int mime_content_type(const char *s
,
124 char **parameternamep
,
125 char **parametervaluep
) {
126 struct dynstr type
, parametername
, parametervalue
;
129 if(!(s
= skipwhite(s
))) return -1;
131 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
132 dynstr_append(&type
, tolower((unsigned char)*s
++));
133 if(!(s
= skipwhite(s
))) return -1;
134 if(*s
++ != '/') return -1;
135 dynstr_append(&type
, '/');
136 if(!(s
= skipwhite(s
))) return -1;
137 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
138 dynstr_append(&type
, tolower((unsigned char)*s
++));
139 if(!(s
= skipwhite(s
))) return -1;
142 dynstr_init(¶metername
);
144 if(!(s
= skipwhite(s
))) return -1;
146 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
147 dynstr_append(¶metername
, tolower((unsigned char)*s
++));
148 if(!(s
= skipwhite(s
))) return -1;
149 if(*s
++ != '=') return -1;
150 if(!(s
= skipwhite(s
))) return -1;
152 if(!(s
= parsestring(s
, parametervaluep
))) return -1;
154 dynstr_init(¶metervalue
);
155 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
156 dynstr_append(¶metervalue
, *s
++);
157 dynstr_terminate(¶metervalue
);
158 *parametervaluep
= parametervalue
.vec
;
160 if(!(s
= skipwhite(s
))) return -1;
161 dynstr_terminate(¶metername
);
162 *parameternamep
= parametername
.vec
;
164 *parametervaluep
= *parameternamep
= 0;
165 dynstr_terminate(&type
);
170 static int iscrlf(const char *ptr
) {
171 return ptr
[0] == '\r' && ptr
[1] == '\n';
174 const char *mime_parse(const char *s
,
175 int (*callback
)(const char *name
, const char *value
,
178 struct dynstr name
, value
;
181 while(*s
&& !iscrlf(s
)) {
184 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
185 dynstr_append(&name
, tolower((unsigned char)*s
++));
186 if(!(s
= skipwhite(s
))) return 0;
187 if(*s
!= ':') return 0;
189 while(*s
&& !(*s
== '\n' && !(s
[1] == ' ' || s
[1] == '\t')))
190 dynstr_append(&value
, *s
++);
192 dynstr_terminate(&name
);
193 dynstr_terminate(&value
);
194 if(!strcmp(name
.vec
, "content-transfer-encoding")) {
195 cte
= xstrdup(value
.vec
);
196 for(p
= cte
; *p
; p
++)
197 *p
= tolower((unsigned char)*p
);
199 if(callback(name
.vec
, value
.vec
, u
)) return 0;
203 if(!strcmp(cte
, "base64")) return mime_base64(s
);
204 if(!strcmp(cte
, "quoted-printable")) return mime_qp(s
);
209 static int isboundary(const char *ptr
, const char *boundary
, size_t bl
) {
210 return (ptr
[0] == '-'
212 && !strncmp(ptr
+ 2, boundary
, bl
)
213 && (iscrlf(ptr
+ bl
+ 2)
214 || (ptr
[bl
+ 2] == '-'
215 && ptr
[bl
+ 3] == '-'
216 && iscrlf(ptr
+ bl
+ 4))));
219 static int isfinal(const char *ptr
, const char *boundary
, size_t bl
) {
220 return (ptr
[0] == '-'
222 && !strncmp(ptr
+ 2, boundary
, bl
)
223 && ptr
[bl
+ 2] == '-'
224 && ptr
[bl
+ 3] == '-'
225 && iscrlf(ptr
+ bl
+ 4));
228 int mime_multipart(const char *s
,
229 int (*callback
)(const char *s
, void *u
),
230 const char *boundary
,
232 size_t bl
= strlen(boundary
);
233 const char *start
, *e
;
236 if(!isboundary(s
, boundary
, bl
)) return -1;
237 while(!isfinal(s
, boundary
, bl
)) {
238 s
= strstr(s
, "\r\n") + 2;
240 while(!isboundary(s
, boundary
, bl
)) {
241 if(!(e
= strstr(s
, "\r\n"))) return -1;
244 if((ret
= callback(xstrndup(start
,
245 s
== start ?
0 : s
- start
- 2),
252 int mime_rfc2388_content_disposition(const char *s
,
254 char **parameternamep
,
255 char **parametervaluep
) {
256 struct dynstr disposition
, parametername
, parametervalue
;
258 dynstr_init(&disposition
);
259 if(!(s
= skipwhite(s
))) return -1;
261 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
262 dynstr_append(&disposition
, tolower((unsigned char)*s
++));
263 if(!(s
= skipwhite(s
))) return -1;
266 dynstr_init(¶metername
);
268 if(!(s
= skipwhite(s
))) return -1;
270 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
271 dynstr_append(¶metername
, tolower((unsigned char)*s
++));
272 if(!(s
= skipwhite(s
))) return -1;
273 if(*s
++ != '=') return -1;
274 if(!(s
= skipwhite(s
))) return -1;
276 if(!(s
= parsestring(s
, parametervaluep
))) return -1;
278 dynstr_init(¶metervalue
);
279 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
280 dynstr_append(¶metervalue
, *s
++);
281 dynstr_terminate(¶metervalue
);
282 *parametervaluep
= parametervalue
.vec
;
284 if(!(s
= skipwhite(s
))) return -1;
285 dynstr_terminate(¶metername
);
286 *parameternamep
= parametername
.vec
;
288 *parametervaluep
= *parameternamep
= 0;
289 dynstr_terminate(&disposition
);
290 *dispositionp
= disposition
.vec
;
294 char *mime_qp(const char *s
) {
303 if((a
= unhexdigitq(s
[0])) != -1
304 && (b
= unhexdigitq(s
[1])) != -1) {
305 dynstr_append(&d
, a
* 16 + b
);
309 while(*t
== ' ' || *t
== '\t') ++t
;
311 /* soft line break */
320 while(*t
== ' ' || *t
== '\t') ++t
;
322 /* trailing space is always eliminated */
325 dynstr_append(&d
, c
);
328 dynstr_append(&d
, c
);
332 dynstr_terminate(&d
);
336 char *mime_base64(const char *s
) {
340 static const char table
[] =
341 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
345 while((c
= (unsigned char)*s
++)) {
346 if((t
= strchr(table
, c
))) {
349 dynstr_append(&d
, (b
[0] << 2) + (b
[1] >> 4));
350 dynstr_append(&d
, (b
[1] << 4) + (b
[2] >> 2));
351 dynstr_append(&d
, (b
[2] << 6) + b
[3]);
354 } else if(c
== '=') {
356 dynstr_append(&d
, (b
[0] << 2) + (b
[1] >> 4));
358 dynstr_append(&d
, (b
[1] << 4) + (b
[2] >> 2));
363 dynstr_terminate(&d
);