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
35 static int whitespace(int c
) {
47 static int tspecial(int c
) {
70 static const char *skipwhite(const char *s
) {
87 case '(': ++depth
; break;
88 case ')': --depth
; break;
103 static const char *parsestring(const char *s
, char **valuep
) {
109 while((c
= *s
++) != '"') {
112 if(!(c
= *s
++)) return 0;
114 dynstr_append(&value
, c
);
119 dynstr_terminate(&value
);
124 int mime_content_type(const char *s
,
126 char **parameternamep
,
127 char **parametervaluep
) {
128 struct dynstr type
, parametername
, parametervalue
;
131 if(!(s
= skipwhite(s
))) return -1;
133 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
134 dynstr_append(&type
, tolower((unsigned char)*s
++));
135 if(!(s
= skipwhite(s
))) return -1;
136 if(*s
++ != '/') return -1;
137 dynstr_append(&type
, '/');
138 if(!(s
= skipwhite(s
))) return -1;
139 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
140 dynstr_append(&type
, tolower((unsigned char)*s
++));
141 if(!(s
= skipwhite(s
))) return -1;
144 dynstr_init(¶metername
);
146 if(!(s
= skipwhite(s
))) return -1;
148 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
149 dynstr_append(¶metername
, tolower((unsigned char)*s
++));
150 if(!(s
= skipwhite(s
))) return -1;
151 if(*s
++ != '=') return -1;
152 if(!(s
= skipwhite(s
))) return -1;
154 if(!(s
= parsestring(s
, parametervaluep
))) return -1;
156 dynstr_init(¶metervalue
);
157 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
158 dynstr_append(¶metervalue
, *s
++);
159 dynstr_terminate(¶metervalue
);
160 *parametervaluep
= parametervalue
.vec
;
162 if(!(s
= skipwhite(s
))) return -1;
163 dynstr_terminate(¶metername
);
164 *parameternamep
= parametername
.vec
;
166 *parametervaluep
= *parameternamep
= 0;
167 dynstr_terminate(&type
);
172 static int iscrlf(const char *ptr
) {
173 return ptr
[0] == '\r' && ptr
[1] == '\n';
176 const char *mime_parse(const char *s
,
177 int (*callback
)(const char *name
, const char *value
,
180 struct dynstr name
, value
;
183 while(*s
&& !iscrlf(s
)) {
186 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
187 dynstr_append(&name
, tolower((unsigned char)*s
++));
188 if(!(s
= skipwhite(s
))) return 0;
189 if(*s
!= ':') return 0;
191 while(*s
&& !(*s
== '\n' && !(s
[1] == ' ' || s
[1] == '\t')))
192 dynstr_append(&value
, *s
++);
194 dynstr_terminate(&name
);
195 dynstr_terminate(&value
);
196 if(!strcmp(name
.vec
, "content-transfer-encoding")) {
197 cte
= xstrdup(value
.vec
);
198 for(p
= cte
; *p
; p
++)
199 *p
= tolower((unsigned char)*p
);
201 if(callback(name
.vec
, value
.vec
, u
)) return 0;
205 if(!strcmp(cte
, "base64")) return mime_base64(s
);
206 if(!strcmp(cte
, "quoted-printable")) return mime_qp(s
);
211 static int isboundary(const char *ptr
, const char *boundary
, size_t bl
) {
212 return (ptr
[0] == '-'
214 && !strncmp(ptr
+ 2, boundary
, bl
)
215 && (iscrlf(ptr
+ bl
+ 2)
216 || (ptr
[bl
+ 2] == '-'
217 && ptr
[bl
+ 3] == '-'
218 && (iscrlf(ptr
+ bl
+ 4) || *(ptr
+ bl
+ 4) == 0))));
221 static int isfinal(const char *ptr
, const char *boundary
, size_t bl
) {
222 return (ptr
[0] == '-'
224 && !strncmp(ptr
+ 2, boundary
, bl
)
225 && ptr
[bl
+ 2] == '-'
226 && ptr
[bl
+ 3] == '-'
227 && (iscrlf(ptr
+ bl
+ 4) || *(ptr
+ bl
+ 4) == 0));
230 int mime_multipart(const char *s
,
231 int (*callback
)(const char *s
, void *u
),
232 const char *boundary
,
234 size_t bl
= strlen(boundary
);
235 const char *start
, *e
;
238 /* We must start with a boundary string */
239 if(!isboundary(s
, boundary
, bl
))
241 /* Keep going until we hit a final boundary */
242 while(!isfinal(s
, boundary
, bl
)) {
243 s
= strstr(s
, "\r\n") + 2;
245 while(!isboundary(s
, boundary
, bl
)) {
246 if(!(e
= strstr(s
, "\r\n")))
250 if((ret
= callback(xstrndup(start
,
251 s
== start ?
0 : s
- start
- 2),
258 int mime_rfc2388_content_disposition(const char *s
,
260 char **parameternamep
,
261 char **parametervaluep
) {
262 struct dynstr disposition
, parametername
, parametervalue
;
264 dynstr_init(&disposition
);
265 if(!(s
= skipwhite(s
))) return -1;
267 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
268 dynstr_append(&disposition
, tolower((unsigned char)*s
++));
269 if(!(s
= skipwhite(s
))) return -1;
272 dynstr_init(¶metername
);
274 if(!(s
= skipwhite(s
))) return -1;
276 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
277 dynstr_append(¶metername
, tolower((unsigned char)*s
++));
278 if(!(s
= skipwhite(s
))) return -1;
279 if(*s
++ != '=') return -1;
280 if(!(s
= skipwhite(s
))) return -1;
282 if(!(s
= parsestring(s
, parametervaluep
))) return -1;
284 dynstr_init(¶metervalue
);
285 while(*s
&& !tspecial(*s
) && !whitespace(*s
))
286 dynstr_append(¶metervalue
, *s
++);
287 dynstr_terminate(¶metervalue
);
288 *parametervaluep
= parametervalue
.vec
;
290 if(!(s
= skipwhite(s
))) return -1;
291 dynstr_terminate(¶metername
);
292 *parameternamep
= parametername
.vec
;
294 *parametervaluep
= *parameternamep
= 0;
295 dynstr_terminate(&disposition
);
296 *dispositionp
= disposition
.vec
;
300 char *mime_qp(const char *s
) {
309 if((a
= unhexdigitq(s
[0])) != -1
310 && (b
= unhexdigitq(s
[1])) != -1) {
311 dynstr_append(&d
, a
* 16 + b
);
315 while(*t
== ' ' || *t
== '\t') ++t
;
317 /* soft line break */
326 while(*t
== ' ' || *t
== '\t') ++t
;
328 /* trailing space is always eliminated */
331 dynstr_append(&d
, c
);
334 dynstr_append(&d
, c
);
338 dynstr_terminate(&d
);
342 char *mime_base64(const char *s
) {
346 static const char table
[] =
347 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
351 while((c
= (unsigned char)*s
++)) {
352 if((t
= strchr(table
, c
))) {
355 dynstr_append(&d
, (b
[0] << 2) + (b
[1] >> 4));
356 dynstr_append(&d
, (b
[1] << 4) + (b
[2] >> 2));
357 dynstr_append(&d
, (b
[2] << 6) + b
[3]);
360 } else if(c
== '=') {
362 dynstr_append(&d
, (b
[0] << 2) + (b
[1] >> 4));
364 dynstr_append(&d
, (b
[1] << 4) + (b
[2] >> 2));
369 dynstr_terminate(&d
);