Init mime struct
[rmail.git] / src / utils / mime.c
blob809064aab1e01edd98f94dbdc81a127cc41cf4ed
1 /* MIME types
3 * http://www.iana.org/assignments/media-types/index.html
5 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <ctype.h>
11 #include "mime.h"
13 static void mime_content_subtype_text(const char *buf, struct mime_header *hdr)
15 if (*buf != '/') {
16 hdr->subtype = MIME_SUBTYPE_UNKNOWN;
17 return;
20 buf++;
22 if (!strncasecmp(buf, "plain", 5)) {
23 hdr->subtype = MIME_SUBTYPE_PLAIN;
24 } else if (!strncasecmp(buf, "html", 4)) {
25 hdr->subtype = MIME_SUBTYPE_HTML;
26 } else {
27 hdr->subtype = MIME_SUBTYPE_UNKNOWN;
32 * multipart/mixed: MIME E-mail; Defined in RFC 2045 and RFC 2046
33 * multipart/alternative: MIME E-mail; Defined in RFC 2045 and RFC 2046
34 * multipart/related: MIME E-mail; Defined in RFC 2387 and used by MHTML (HTML mail)
35 * multipart/form-data: MIME Webform; Defined in RFC 2388
36 * multipart/signed: Defined in RFC 1847
37 * multipart/encrypted: Defined in RFC 1847
40 static void mime_content_subtype_multipart(const char *buf, struct mime_header *hdr)
42 if (*buf != '/') {
43 hdr->subtype = MIME_SUBTYPE_UNKNOWN;
44 return;
47 buf++;
49 if (!strncasecmp(buf, "alternative", 11))
50 hdr->subtype = MIME_SUBTYPE_ALTERNATIVE;
51 else if (!strncasecmp(buf, "mixed", 5))
52 hdr->subtype = MIME_SUBTYPE_MIXED;
53 else if (!strncasecmp(buf, "related", 7))
54 hdr->subtype = MIME_SUBTYPE_RELATED;
55 else if (!strncasecmp(buf, "form-data", 9))
56 hdr->subtype = MIME_SUBTYPE_FORM_DATA;
57 else if (!strncasecmp(buf, "signed", 6))
58 hdr->subtype = MIME_SUBTYPE_SIGNED;
59 else if (!strncasecmp(buf, "encrypted", 9))
60 hdr->subtype = MIME_SUBTYPE_ENCRYPTED;
62 else if (!strncasecmp(buf, "byteranges", 10))
63 hdr->subtype = MIME_SUBTYPE_BYTERANGES;
64 else if (!strncasecmp(buf, "digest", 6))
65 hdr->subtype = MIME_SUBTYPE_DIGEST;
66 else if (!strncasecmp(buf, "header-set", 10))
67 hdr->subtype = MIME_SUBTYPE_HEADER_SET;
68 else if (!strncasecmp(buf, "parallel", 8))
69 hdr->subtype = MIME_SUBTYPE_PARALLEL;
70 else if (!strncasecmp(buf, "report", 6))
71 hdr->subtype = MIME_SUBTYPE_REPORT;
72 else if (!strncasecmp(buf, "appledouble", 11))
73 hdr->subtype = MIME_SUBTYPE_APPLEDOUBLE;
74 else if (!strncasecmp(buf, "voice-message", 13))
75 hdr->subtype = MIME_SUBTYPE_VOICE_MESSAGE;
76 else
77 hdr->subtype = MIME_SUBTYPE_UNKNOWN;
80 void mime_content_type(const char *buf, struct mime_header *hdr)
82 const char *p;
84 p = strchr(buf, ':');
85 p++;
87 while (*p != '\0' && isspace(*p)) {
88 if (*p == '\n' || *p == '\r') {
89 hdr->type = MIME_TYPE_UNKNOWN;
90 return;
92 p++;
95 /* Check MIME type */
96 /* text/plain is the default for e-mail */
97 if (!strncasecmp(p, "text", 4)) {
98 hdr->type = MIME_TYPE_TEXT;
99 p += 4;
100 mime_content_subtype_text(p, hdr);
101 } else if (!strncasecmp(p, "multipart", 9)) {
102 hdr->type = MIME_TYPE_MULTIPART;
103 p += 9;
104 mime_content_subtype_multipart(p, hdr);
105 } else if (!strncasecmp(p, "application", 11)) {
106 hdr->type = MIME_TYPE_APPLICATION;
107 p += 11;
108 } else if (!strncasecmp(p, "audio", 5)) {
109 hdr->type = MIME_TYPE_AUDIO;
110 p += 5;
111 } else if (!strncasecmp(p, "example", 7)) {
112 hdr->type = MIME_TYPE_EXAMPLE;
113 p += 7;
114 } else if (!strncasecmp(p, "image", 5)) {
115 hdr->type = MIME_TYPE_IMAGE;
116 p += 5;
117 } else if (!strncasecmp(p, "message", 7)) {
118 hdr->type = MIME_TYPE_MESSAGE;
119 p += 7;
120 } else if (!strncasecmp(p, "model", 5)) {
121 hdr->type = MIME_TYPE_MODEL;
122 p += 5;
123 } else if (!strncasecmp(p, "video", 5)) {
124 hdr->type = MIME_TYPE_VIDEO;
125 p += 5;
126 } else {
127 hdr->type = MIME_TYPE_UNKNOWN;
131 void mime_content_disposition(const char *buf, struct mime_header *hdr)
133 const char *p;
135 p = strchr(buf, ':');
136 p++;
138 while (*p != '\0' && isspace(*p)) {
139 if (*p == '\n' || *p == '\r') {
140 hdr->disposition = MIME_DISPOSITION_UNKNOWN;
141 return;
143 p++;
146 /* text/plain is the default */
147 if (!strncasecmp(p, "inline", 6)) {
148 hdr->disposition = MIME_DISPOSITION_INLINE;
149 } else if (!strncasecmp(p, "attachment", 10)) {
150 hdr->disposition = MIME_DISPOSITION_ATTACHMENT;
151 } else {
152 hdr->disposition = MIME_DISPOSITION_UNKNOWN;
156 void mime_content_transfer_encoding(const char *buf, struct mime_header *hdr)
158 const char *p;
160 p = strchr(buf, ':');
161 p++;
163 while (*p != '\0' && isspace(*p)) {
164 if (*p == '\n' || *p == '\r') {
165 hdr->transfer_encoding = MIME_ENCODING_7BIT;
166 return;
168 p++;
171 if (!strncasecmp(p, "7bit", 4)) {
172 hdr->transfer_encoding = MIME_ENCODING_7BIT;
173 } else if (!strncasecmp(p, "quoted-printable", 16)) {
174 hdr->transfer_encoding = MIME_ENCODING_QUOTED_PRINTABLE;
175 } else if (!strncasecmp(p, "base64", 6)) {
176 hdr->transfer_encoding = MIME_ENCODING_BASE64;
177 } else if (!strncasecmp(p, "8bit", 4)) {
178 hdr->transfer_encoding = MIME_ENCODING_8BIT;
179 } else if (!strncasecmp(p, "binary", 6)) {
180 hdr->transfer_encoding = MIME_ENCODING_BINARY;
181 } else {
182 hdr->transfer_encoding = MIME_ENCODING_UNKNOWN;
186 /* DEBUG */
188 static void debug_mime_text(const struct mime_header *hdr)
190 fprintf(stderr, "text/");
192 switch (hdr->subtype) {
193 case MIME_SUBTYPE_PLAIN:
194 fprintf(stderr, "plain");
195 break;
196 case MIME_SUBTYPE_HTML:
197 fprintf(stderr, "html");
198 break;
199 case MIME_SUBTYPE_UNKNOWN:
200 fprintf(stderr, "UNKNOWN");
201 break;
202 default:
203 fprintf(stderr, "%d", hdr->subtype);
204 break;
206 fprintf(stderr, "\n");
209 static void debug_mime_multipart(const struct mime_header *hdr)
211 fprintf(stderr, "multipart/");
213 switch (hdr->subtype) {
214 case MIME_SUBTYPE_ALTERNATIVE:
215 fprintf(stderr, "alternative");
216 break;
217 case MIME_SUBTYPE_MIXED:
218 fprintf(stderr, "mixed");
219 break;
220 case MIME_SUBTYPE_RELATED:
221 fprintf(stderr, "related");
222 break;
223 case MIME_SUBTYPE_FORM_DATA:
224 fprintf(stderr, "form-data");
225 break;
226 case MIME_SUBTYPE_SIGNED:
227 fprintf(stderr, "signed");
228 break;
229 case MIME_SUBTYPE_ENCRYPTED:
230 fprintf(stderr, "encrypted");
231 break;
232 case MIME_SUBTYPE_UNKNOWN:
233 fprintf(stderr, "UNKNOWN");
234 break;
235 default:
236 fprintf(stderr, "%d", hdr->subtype);
237 break;
239 fprintf(stderr, "\n");
242 static void debug_mime_application(const struct mime_header *hdr)
244 fprintf(stderr, "application/");
246 switch (hdr->subtype) {
247 case MIME_SUBTYPE_UNKNOWN:
248 fprintf(stderr, "UNKNOWN");
249 break;
250 default:
251 fprintf(stderr, "%d", hdr->subtype);
252 break;
254 fprintf(stderr, "\n");
257 static void debug_mime_audio(const struct mime_header *hdr)
259 fprintf(stderr, "audio/");
260 switch (hdr->subtype) {
261 case MIME_SUBTYPE_UNKNOWN:
262 fprintf(stderr, "UNKNOWN");
263 break;
264 default:
265 fprintf(stderr, "%d", hdr->subtype);
266 break;
268 fprintf(stderr, "\n");
272 static void debug_mime_image(const struct mime_header *hdr)
274 fprintf(stderr, "image/");
275 switch (hdr->subtype) {
276 case MIME_SUBTYPE_UNKNOWN:
277 fprintf(stderr, "UNKNOWN");
278 break;
279 default:
280 fprintf(stderr, "%d", hdr->subtype);
281 break;
283 fprintf(stderr, "\n");
287 static void debug_mime_message(const struct mime_header *hdr)
289 fprintf(stderr, "message/");
290 switch (hdr->subtype) {
291 case MIME_SUBTYPE_UNKNOWN:
292 fprintf(stderr, "UNKNOWN");
293 break;
294 default:
295 fprintf(stderr, "%d", hdr->subtype);
296 break;
298 fprintf(stderr, "\n");
301 static void debug_mime_model(const struct mime_header *hdr)
303 fprintf(stderr, "model/");
304 switch (hdr->subtype) {
305 case MIME_SUBTYPE_UNKNOWN:
306 fprintf(stderr, "UNKNOWN");
307 break;
308 default:
309 fprintf(stderr, "%d", hdr->subtype);
310 break;
312 fprintf(stderr, "\n");
315 static void debug_mime_video(const struct mime_header *hdr)
317 fprintf(stderr, "video/");
318 switch (hdr->subtype) {
319 case MIME_SUBTYPE_UNKNOWN:
320 fprintf(stderr, "UNKNOWN");
321 break;
322 default:
323 fprintf(stderr, "%d", hdr->subtype);
324 break;
326 fprintf(stderr, "\n");
329 void debug_mime_header(const struct mime_header *hdr)
331 fprintf(stderr, "MIME Header:\n");
333 /* MIME Content-Type */
334 fprintf(stderr, " Content-Type.............: ");
335 switch (hdr->type) {
336 case MIME_TYPE_TEXT:
337 debug_mime_text(hdr);
338 break;
339 case MIME_TYPE_MULTIPART:
340 debug_mime_multipart(hdr);
341 break;
342 case MIME_TYPE_APPLICATION:
343 debug_mime_application(hdr);
344 break;
345 case MIME_TYPE_AUDIO:
346 debug_mime_audio(hdr);
347 break;
348 case MIME_TYPE_EXAMPLE:
349 fprintf(stderr, "example\n");
350 break;
351 case MIME_TYPE_IMAGE:
352 debug_mime_image(hdr);
353 break;
354 case MIME_TYPE_MESSAGE:
355 debug_mime_message(hdr);
356 break;
357 case MIME_TYPE_MODEL:
358 debug_mime_model(hdr);
359 break;
360 case MIME_TYPE_VIDEO:
361 debug_mime_video(hdr);
362 break;
363 case MIME_TYPE_UNKNOWN:
364 default:
365 fprintf(stderr, "Unknown\n");
366 break;
369 /* MIME Content-Disposition */
370 fprintf(stderr, " Content-Disposition......: ");
371 switch (hdr->disposition) {
372 case MIME_DISPOSITION_INLINE:
373 fprintf(stderr, "inline\n");
374 break;
375 case MIME_DISPOSITION_ATTACHMENT:
376 fprintf(stderr, "attachment\n");
377 break;
378 case MIME_DISPOSITION_UNKNOWN:
379 default:
380 fprintf(stderr, "Unknown\n");
381 break;
384 /* MIME Content-Transfer-Encoding */
385 fprintf(stderr, " Content-Transfer-Encoding: ");
386 switch (hdr->transfer_encoding) {
387 case MIME_ENCODING_7BIT:
388 fprintf(stderr, "7bit\n");
389 break;
390 case MIME_ENCODING_QUOTED_PRINTABLE:
391 fprintf(stderr, "quoted-printable\n");
392 break;
393 case MIME_ENCODING_BASE64:
394 fprintf(stderr, "base64\n");
395 break;
396 case MIME_ENCODING_8BIT:
397 fprintf(stderr, "8bit\n");
398 break;
399 case MIME_ENCODING_BINARY:
400 fprintf(stderr, "binary\n");
401 break;
402 case MIME_ENCODING_UNKNOWN:
403 default:
404 fprintf(stderr, "Unknown\n");
405 break;