4 * This is the MIME parser for Citadel.
6 * Copyright (c) 1998-2007 by the citadel.org development team.
7 * This code is distributed under the GNU General Public License v3.
15 #include <sys/types.h>
19 #include <sys/types.h>
23 #include "xdgmime/xdgmime.h"
24 #include "libcitadel.h"
25 #include "libcitadellocal.h"
27 void extract_key(char *target, char *source, char *key)
30 char looking_for[256];
31 int double_quotes = 0;
33 snprintf(looking_for, sizeof looking_for, "%s=", key);
35 ptr = bmstrcasestr(source, looking_for);
40 strcpy(target, (ptr + strlen(looking_for)));
42 for (ptr=target; (*ptr != 0); ++ptr) {
44 /* A semicolon means we've hit the end of the key, unless we're inside double quotes */
45 if ( (double_quotes != 1) && (*ptr == ';')) {
49 /* if we find double quotes, we've got a great set of string boundaries */
52 if (double_quotes == 1) {
64 * For non-multipart messages, we need to generate a quickie partnum of "1"
65 * to return to callback functions. Some callbacks demand it.
67 char *fixed_partnum(char *supplied_partnum) {
68 if (supplied_partnum == NULL) return "1";
69 if (strlen(supplied_partnum)==0) return "1";
70 return supplied_partnum;
74 static inline unsigned int _decode_hex(char *Source)
77 if ((*Source >= '0') && (*Source <= '9')) {
78 ret += (*Source - '0');
80 else if ((*Source >= 'A') && (*Source <= 'F')) {
81 ret += (*Source - 'A' + 10);
83 else if ((*Source >= 'a') && (*Source <= 'f')) {
84 ret += (*Source - 'a' + 10);
92 if ((*(Source + 1) >= '0') && (*(Source + 1) <= '9')) {
93 ret |= (*(Source + 1) - '0');
95 else if ((*(Source + 1) >= 'A') && (*(Source + 1) <= 'F')) {
96 ret |= (*(Source + 1) - 'A' + 10);
98 else if ((*(Source + 1) >= 'a') && (*(Source + 1) <= 'f')) {
99 ret |= (*(Source + 1) - 'a' + 10);
107 unsigned int decode_hex(char *Source) {return _decode_hex(Source);}
110 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
111 * according to RFC2045 section 6.7
113 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
115 int decoded_length = 0;
118 while (pos < sourcelen)
120 if (!strncmp(&encoded[pos], "=\r\n", 3))
124 else if (!strncmp(&encoded[pos], "=\n", 2))
128 else if (encoded[pos] == '=')
131 ch = _decode_hex(&encoded[pos+1]);
133 decoded[decoded_length++] = ch;
137 decoded[decoded_length++] = encoded[pos];
141 decoded[decoded_length] = 0;
142 return(decoded_length);
147 * Given a message or message-part body and a length, handle any necessary
148 * decoding and pass the request up the stack.
150 void mime_decode(char *partnum,
151 char *part_start, size_t length,
152 char *content_type, char *charset, char *encoding,
155 char *name, char *filename,
168 void (*PreMultiPartCallBack)
180 void (*PostMultiPartCallBack)
198 size_t bytes_decoded = 0;
200 /* Some encodings aren't really encodings */
201 if (!strcasecmp(encoding, "7bit"))
202 strcpy(encoding, "");
203 if (!strcasecmp(encoding, "8bit"))
204 strcpy(encoding, "");
205 if (!strcasecmp(encoding, "binary"))
206 strcpy(encoding, "");
208 /* If this part is not encoded, send as-is */
209 if ( (strlen(encoding) == 0) || (dont_decode)) {
210 if (CallBack != NULL) {
211 CallBack(name, filename, fixed_partnum(partnum),
212 disposition, part_start,
213 content_type, charset, length, encoding, id, userdata);
218 /* Fail silently if we hit an unknown encoding. */
219 if ((strcasecmp(encoding, "base64"))
220 && (strcasecmp(encoding, "quoted-printable"))) {
225 * Allocate a buffer for the decoded data. The output buffer is slightly
226 * larger than the input buffer; this assumes that the decoded data
227 * will never be significantly larger than the encoded data. This is a
228 * safe assumption with base64, uuencode, and quoted-printable.
230 decoded = malloc(length + 32768);
231 if (decoded == NULL) {
235 if (!strcasecmp(encoding, "base64")) {
236 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
238 else if (!strcasecmp(encoding, "quoted-printable")) {
239 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
242 if (bytes_decoded > 0) if (CallBack != NULL) {
243 CallBack(name, filename, fixed_partnum(partnum),
244 disposition, decoded,
245 content_type, charset, bytes_decoded, "binary", id, userdata);
252 * Break out the components of a multipart message
253 * (This function expects to be fed HEADERS + CONTENT)
254 * Note: NULL can be supplied as content_end; in this case, the message is
255 * considered to have ended when the parser encounters a 0x00 byte.
257 void the_mime_parser(char *partnum,
258 char *content_start, char *content_end,
271 void (*PreMultiPartCallBack)
283 void (*PostMultiPartCallBack)
302 char *part_start, *part_end = NULL;
307 size_t startary_len = 0;
312 size_t content_length;
317 char *content_type_name;
318 char *content_disposition_name;
324 char nested_partnum[256];
326 char *evaluate_crlf_ptr = NULL;
333 boundary = malloc(SIZ);
334 memset(boundary, 0, SIZ);
336 startary = malloc(SIZ);
337 memset(startary, 0, SIZ);
339 endary = malloc(SIZ);
340 memset(endary, 0, SIZ);
342 header = malloc(SIZ);
343 memset(header, 0, SIZ);
345 content_type = malloc(SIZ);
346 memset(content_type, 0, SIZ);
348 charset = malloc(SIZ);
349 memset(charset, 0, SIZ);
351 encoding = malloc(SIZ);
352 memset(encoding, 0, SIZ);
354 content_type_name = malloc(SIZ);
355 memset(content_type_name, 0, SIZ);
357 content_disposition_name = malloc(SIZ);
358 memset(content_disposition_name, 0, SIZ);
360 filename = malloc(SIZ);
361 memset(filename, 0, SIZ);
363 disposition = malloc(SIZ);
364 memset(disposition, 0, SIZ);
369 /* If the caller didn't supply an endpointer, generate one by measure */
370 if (content_end == NULL) {
371 content_end = &content_start[strlen(content_start)];
374 /* Learn interesting things from the headers */
378 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
379 if (ptr >= content_end) {
383 for (i = 0; i < buflen; ++i) {
384 if (isspace(buf[i])) {
389 if (!isspace(buf[0])) {
390 if (!strncasecmp(header, "Content-type:", 13)) {
391 strcpy(content_type, &header[13]);
392 striplt(content_type);
393 extract_key(content_type_name, content_type, "name");
394 extract_key(charset, content_type, "charset");
395 extract_key(boundary, header, "boundary");
396 /* Deal with weird headers */
397 if (strchr(content_type, ' '))
398 *(strchr(content_type, ' ')) = '\0';
399 if (strchr(content_type, ';'))
400 *(strchr(content_type, ';')) = '\0';
402 if (!strncasecmp(header, "Content-Disposition:", 20)) {
403 strcpy(disposition, &header[20]);
404 striplt(disposition);
405 extract_key(content_disposition_name, disposition, "name");
406 extract_key(filename, disposition, "filename");
408 if (!strncasecmp(header, "Content-ID:", 11)) {
409 strcpy(id, &header[11]);
411 stripallbut(id, '<', '>');
413 if (!strncasecmp(header, "Content-length: ", 15)) {
415 safestrncpy(clbuf, &header[15], sizeof clbuf);
417 content_length = (size_t) atol(clbuf);
419 if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
420 strcpy(encoding, &header[26]);
426 if ((headerlen + buflen + 2) < SIZ) {
427 memcpy(&header[headerlen], buf, buflen);
429 header[headerlen] = '\0';
431 } while ((!IsEmptyStr(buf)) && (*ptr != 0));
433 if (strchr(disposition, ';'))
434 *(strchr(disposition, ';')) = '\0';
435 striplt(disposition);
436 if (strchr(content_type, ';'))
437 *(strchr(content_type, ';')) = '\0';
438 striplt(content_type);
440 if (!IsEmptyStr(boundary)) {
446 /* If this is a multipart message, then recursively process it */
450 /* Tell the client about this message's multipartedness */
451 if (PreMultiPartCallBack != NULL) {
452 PreMultiPartCallBack("", "", partnum, "",
453 NULL, content_type, charset,
454 0, encoding, id, userdata);
457 /* Figure out where the boundaries are */
458 snprintf(startary, SIZ, "--%s", boundary);
459 snprintf(endary, SIZ, "--%s--", boundary);
460 startary_len = strlen(startary);
464 next_boundary = NULL;
465 for (srch=ptr; srch<content_end; ++srch) {
466 if (!memcmp(srch, startary, startary_len)) {
467 next_boundary = srch;
472 if ( (part_start != NULL) && (next_boundary != NULL) ) {
473 part_end = next_boundary;
474 --part_end; /* omit the trailing LF */
476 --part_end; /* omit the trailing CR */
479 if (!IsEmptyStr(partnum)) {
480 snprintf(nested_partnum,
481 sizeof nested_partnum,
486 snprintf(nested_partnum,
487 sizeof nested_partnum,
490 the_mime_parser(nested_partnum,
491 part_start, part_end,
493 PreMultiPartCallBack,
494 PostMultiPartCallBack,
499 if (next_boundary != NULL) {
500 /* If we pass out of scope, don't attempt to
501 * read past the end boundary. */
502 if (!strcmp(next_boundary, endary)) {
506 /* Set up for the next part. */
507 part_start = strstr(next_boundary, "\n");
509 /* Determine whether newlines are LF or CRLF */
510 evaluate_crlf_ptr = part_start;
512 if (!memcmp(evaluate_crlf_ptr, "\r\n", 2)) {
519 /* Advance past the LF ... now we're in the next part */
525 /* Invalid end of multipart. Bail out! */
528 } while ( (ptr < content_end) && (next_boundary != NULL) );
530 if (PostMultiPartCallBack != NULL) {
531 PostMultiPartCallBack("", "", partnum, "", NULL,
532 content_type, charset, 0, encoding, id, userdata);
537 /* If it's not a multipart message, then do something with it */
541 while (ptr < content_end) {
545 part_end = content_end;
548 /* The following code will truncate the MIME part to the size
549 * specified by the Content-length: header. We have commented it
550 * out because these headers have a tendency to be wrong.
552 * if ( (content_length > 0) && (length > content_length) ) {
553 * length = content_length;
557 /* Sometimes the "name" field is tacked on to Content-type,
558 * and sometimes it's tacked on to Content-disposition. Use
559 * whichever one we have.
561 if (strlen(content_disposition_name) > strlen(content_type_name)) {
562 name = content_disposition_name;
565 name = content_type_name;
568 /* Ok, we've got a non-multipart part here, so do something with it.
572 content_type, charset, encoding, disposition, id,
574 CallBack, NULL, NULL,
575 userdata, dont_decode
579 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
581 if (!strcasecmp(content_type, "message/rfc822")) {
583 if (PreMultiPartCallBack != NULL) {
584 PreMultiPartCallBack("", "", partnum, "",
585 NULL, content_type, charset,
586 0, encoding, id, userdata);
588 if (CallBack != NULL) {
589 if (strlen(partnum) > 0) {
590 snprintf(nested_partnum,
591 sizeof nested_partnum,
596 snprintf(nested_partnum,
597 sizeof nested_partnum,
600 the_mime_parser(nested_partnum,
601 part_start, part_end,
603 PreMultiPartCallBack,
604 PostMultiPartCallBack,
609 if (PostMultiPartCallBack != NULL) {
610 PostMultiPartCallBack("", "", partnum, "", NULL,
611 content_type, charset, 0, encoding, id, userdata);
619 end_parser: /* free the buffers! end the oppression!! */
627 free(content_type_name);
628 free(content_disposition_name);
637 * Entry point for the MIME parser.
638 * (This function expects to be fed HEADERS + CONTENT)
639 * Note: NULL can be supplied as content_end; in this case, the message is
640 * considered to have ended when the parser encounters a 0x00 byte.
642 void mime_parser(char *content_start,
658 void (*PreMultiPartCallBack)
671 void (*PostMultiPartCallBack)
689 the_mime_parser("", content_start, content_end,
691 PreMultiPartCallBack,
692 PostMultiPartCallBack,
693 userdata, dont_decode);
701 typedef struct _MimeGuess {
705 const char *MimeString;
708 MimeGuess MyMimes [] = {
736 const char *GuessMimeType(const char *data, size_t dlen)
740 while (MyMimes[MimeIndex].PatternLen != 0)
742 if ((MyMimes[MimeIndex].PatternLen +
743 MyMimes[MimeIndex].PatternOffset < dlen) &&
744 strncmp(MyMimes[MimeIndex].Pattern,
745 &data[MyMimes[MimeIndex].PatternOffset],
746 MyMimes[MimeIndex].PatternLen) == 0)
748 return MyMimes[MimeIndex].MimeString;
753 * ok, our simple minded algorythm didn't find anything,
754 * let the big chegger try it, he wil default to application/octet-stream
756 return (xdg_mime_get_mime_type_for_data(data, dlen));
760 const char* GuessMimeByFilename(const char *what, size_t len)
762 /* we know some hardcoded on our own, try them... */
763 if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
765 else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
766 return "text/javascript";
767 else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
769 else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
771 else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
773 else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
775 else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
776 return "image/x-icon";
777 else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
778 return "text/x-vcard";
779 else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
781 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
783 else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
784 return "text/vnd.wap.wml";
785 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
786 return "text/vnd.wap.wmlscript";
787 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
788 return "application/vnd.wap.wmlc";
789 else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
790 return "application/vnd.wap.wmlscriptc";
791 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
792 return "image/vnd.wap.wbmp";
794 /* and let xdgmime do the fallback. */
795 return xdg_mime_get_mime_type_from_file_name(what);
798 static HashList *IconHash = NULL;
800 typedef struct IconName IconName;
807 static void DeleteIcon(void *IconNamePtr)
809 IconName *Icon = (IconName*) IconNamePtr;
810 free(Icon->FlatName);
811 free(Icon->FileName);
816 static const char *PrintFlat(void *IconNamePtr)
818 IconName *Icon = (IconName*) IconNamePtr;
819 return Icon->FlatName;
821 static const char *PrintFile(void *IconNamePtr)
823 IconName *Icon = (IconName*) IconNamePtr;
824 return Icon->FileName;
828 #define GENSTR "x-generic"
829 #define IGNORE_PREFIX_1 "gnome-mime"
830 int LoadIconDir(const char *DirName)
833 struct dirent *filedir_entry;
838 filedir = opendir (DirName);
839 IconHash = NewHash(1, NULL);
840 if (filedir == NULL) {
844 while ((filedir_entry = readdir(filedir)))
848 #ifdef _DIRENT_HAVE_D_NAMELEN
849 d_namelen = filedir_entry->d_namelen;
851 d_namelen = strlen(filedir_entry->d_name);
853 d_without_ext = d_namelen;
854 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
856 if ((d_without_ext == 0) || (d_namelen < 3))
859 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
860 (strncmp(IGNORE_PREFIX_1,
861 filedir_entry->d_name,
862 sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
863 PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
864 d_without_ext -= sizeof(IGNORE_PREFIX_1);
867 PStart = filedir_entry->d_name;
869 Icon = malloc(sizeof(IconName));
871 Icon->FileName = malloc(d_namelen + 1);
872 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
874 Icon->FlatName = malloc(d_without_ext + 1);
875 memcpy(Icon->FlatName, PStart, d_without_ext);
876 Icon->FlatName[d_without_ext] = '\0';
877 /* Try to find Minor type in image-jpeg */
878 MinorPtr = strchr(Icon->FlatName, '-');
879 if (MinorPtr != NULL) {
881 MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
882 if ((MinorLen == sizeof(GENSTR)) &&
883 (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
884 /* ok, we found a generic filename. cut the generic. */
886 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
888 else { /* Map the major / minor separator to / */
893 // PrintHash(IconHash, PrintFlat, PrintFile);
894 // printf("%s - %s\n", Icon->FlatName, Icon->FileName);
895 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
896 // PrintHash(IconHash, PrintFlat, PrintFile);
902 const char *GetIconFilename(char *MimeType, size_t len)
910 GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
911 /* didn't find the exact mimetype? try major only. */
914 pMinor = strchr(MimeType, '/');
915 if (pMinor != NULL) {
917 GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
918 Icon = (IconName*) vIcon;
925 /*printf("Getting: [%s] == [%s] -> [%s]\n", MimeType, Icon->FlatName, Icon->FileName);*/
926 return Icon->FileName;
929 void ShutDownLibCitadelMime(void)
931 DeleteHash(&IconHash);