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, long sourcelen, char *key, long keylen, char KeyEnd)
29 char *sptr, *ptr = NULL;
30 int double_quotes = 0;
31 long RealKeyLen = keylen;
37 ptr = bmstrcasestr_len(sptr, sourcelen - (sptr - source),
41 while (isspace(*(ptr + RealKeyLen)))
43 if (*(ptr + RealKeyLen) == KeyEnd)
50 sptr = ptr + RealKeyLen + 1;
60 strcpy(target, (ptr + RealKeyLen));
62 for (ptr=target; (*ptr != 0); ptr++) {
64 /* A semicolon means we've hit the end of the key, unless we're inside double quotes */
65 if ( (double_quotes != 1) && (*ptr == ';')) {
69 /* if we find double quotes, we've got a great set of string boundaries */
72 if (double_quotes == 1) {
85 * For non-multipart messages, we need to generate a quickie partnum of "1"
86 * to return to callback functions. Some callbacks demand it.
88 char *fixed_partnum(char *supplied_partnum) {
89 if (supplied_partnum == NULL) return "1";
90 if (strlen(supplied_partnum)==0) return "1";
91 return supplied_partnum;
95 static inline unsigned int _decode_hex(char *Source)
98 if ((*Source >= '0') && (*Source <= '9')) {
99 ret += (*Source - '0');
101 else if ((*Source >= 'A') && (*Source <= 'F')) {
102 ret += (*Source - 'A' + 10);
104 else if ((*Source >= 'a') && (*Source <= 'f')) {
105 ret += (*Source - 'a' + 10);
113 if ((*(Source + 1) >= '0') && (*(Source + 1) <= '9')) {
114 ret |= (*(Source + 1) - '0');
116 else if ((*(Source + 1) >= 'A') && (*(Source + 1) <= 'F')) {
117 ret |= (*(Source + 1) - 'A' + 10);
119 else if ((*(Source + 1) >= 'a') && (*(Source + 1) <= 'f')) {
120 ret |= (*(Source + 1) - 'a' + 10);
128 unsigned int decode_hex(char *Source) {return _decode_hex(Source);}
131 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
132 * according to RFC2045 section 6.7
134 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
136 int decoded_length = 0;
139 while (pos < sourcelen)
141 if (!strncmp(&encoded[pos], "=\r\n", 3))
145 else if (!strncmp(&encoded[pos], "=\n", 2))
149 else if (encoded[pos] == '=')
152 ch = _decode_hex(&encoded[pos+1]);
154 decoded[decoded_length++] = ch;
158 decoded[decoded_length++] = encoded[pos];
162 decoded[decoded_length] = 0;
163 return(decoded_length);
168 * Given a message or message-part body and a length, handle any necessary
169 * decoding and pass the request up the stack.
171 void mime_decode(char *partnum,
172 char *part_start, size_t length,
173 char *content_type, char *charset, char *encoding,
176 char *name, char *filename,
177 MimeParserCallBackType CallBack,
178 MimeParserCallBackType PreMultiPartCallBack,
179 MimeParserCallBackType PostMultiPartCallBack,
185 size_t bytes_decoded = 0;
187 /* Some encodings aren't really encodings */
188 if (!strcasecmp(encoding, "7bit"))
189 strcpy(encoding, "");
190 if (!strcasecmp(encoding, "8bit"))
191 strcpy(encoding, "");
192 if (!strcasecmp(encoding, "binary"))
193 strcpy(encoding, "");
195 /* If this part is not encoded, send as-is */
196 if ( (strlen(encoding) == 0) || (dont_decode)) {
197 if (CallBack != NULL) {
198 CallBack(name, filename, fixed_partnum(partnum),
199 disposition, part_start,
200 content_type, charset, length, encoding, id, userdata);
205 /* Fail silently if we hit an unknown encoding. */
206 if ((strcasecmp(encoding, "base64"))
207 && (strcasecmp(encoding, "quoted-printable"))) {
212 * Allocate a buffer for the decoded data. The output buffer is slightly
213 * larger than the input buffer; this assumes that the decoded data
214 * will never be significantly larger than the encoded data. This is a
215 * safe assumption with base64, uuencode, and quoted-printable.
217 decoded = malloc(length + 32768);
218 if (decoded == NULL) {
222 if (!strcasecmp(encoding, "base64")) {
223 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
225 else if (!strcasecmp(encoding, "quoted-printable")) {
226 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
229 if (bytes_decoded > 0) if (CallBack != NULL) {
230 CallBack(name, filename, fixed_partnum(partnum),
231 disposition, decoded,
232 content_type, charset, bytes_decoded, "binary", id, userdata);
239 * Break out the components of a multipart message
240 * (This function expects to be fed HEADERS + CONTENT)
241 * Note: NULL can be supplied as content_end; in this case, the message is
242 * considered to have ended when the parser encounters a 0x00 byte.
244 void the_mime_parser(char *partnum,
245 char *content_start, char *content_end,
246 MimeParserCallBackType CallBack,
247 MimeParserCallBackType PreMultiPartCallBack,
248 MimeParserCallBackType PostMultiPartCallBack,
255 char *part_start, *part_end = NULL;
260 size_t startary_len = 0;
265 size_t content_type_len;
266 size_t content_length;
269 size_t disposition_len;
272 char *content_type_name;
273 char *content_disposition_name;
279 char nested_partnum[256];
281 char *evaluate_crlf_ptr = NULL;
288 boundary = malloc(SIZ * 12);
291 startary = boundary + SIZ * 1;
294 endary = boundary + SIZ * 2;
297 header = boundary + SIZ * 3;
300 content_type = boundary + SIZ * 4;
301 *content_type = '\0';
303 charset = boundary + SIZ * 5;
306 encoding = boundary + SIZ * 6;
309 content_type_name = boundary + SIZ * 7;
310 *content_type_name = '\0';
312 content_disposition_name = boundary + SIZ * 8;
313 *content_disposition_name = '\0';
315 filename = boundary + SIZ * 9;
318 disposition = boundary + SIZ * 10;
321 id = boundary + SIZ * 11;
324 /* If the caller didn't supply an endpointer, generate one by measure */
325 if (content_end == NULL) {
326 content_end = &content_start[strlen(content_start)];
329 /* Learn interesting things from the headers */
333 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
334 if (ptr >= content_end) {
338 for (i = 0; i < buflen; ++i) {
339 if (isspace(buf[i])) {
344 if (!isspace(buf[0])) {
345 if (!strncasecmp(header, "Content-type:", 13)) {
346 memcpy (content_type, &header[13], headerlen - 12);
347 content_type_len = striplt (content_type);
349 extract_key(content_type_name, content_type, content_type_len, HKEY("name"), '=');
350 extract_key(charset, content_type, content_type_len, HKEY("charset"), '=');
351 extract_key(boundary, header, headerlen, HKEY("boundary"), '=');
353 /* Deal with weird headers */
354 if (strchr(content_type, ' '))
355 *(strchr(content_type, ' ')) = '\0';
356 if (strchr(content_type, ';'))
357 *(strchr(content_type, ';')) = '\0';
359 else if (!strncasecmp(header, "Content-Disposition:", 20)) {
360 memcpy (disposition, &header[20], headerlen - 19);
361 disposition_len = striplt(disposition);
362 extract_key(content_disposition_name, disposition, disposition_len, HKEY("name"), '=');
363 extract_key(filename, disposition, disposition_len, HKEY("filename"), '=');
365 else if (!strncasecmp(header, "Content-ID:", 11)) {
366 strcpy(id, &header[11]);
368 stripallbut(id, '<', '>');
370 else if (!strncasecmp(header, "Content-length: ", 15)) {
372 safestrncpy(clbuf, &header[15], sizeof clbuf);
374 content_length = (size_t) atol(clbuf);
376 else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
377 strcpy(encoding, &header[26]);
383 if ((headerlen + buflen + 2) < SIZ) {
384 memcpy(&header[headerlen], buf, buflen);
386 header[headerlen] = '\0';
388 } while ((!IsEmptyStr(buf)) && (*ptr != 0));
390 if (strchr(disposition, ';'))
391 *(strchr(disposition, ';')) = '\0';
392 striplt(disposition);
393 if (strchr(content_type, ';'))
394 *(strchr(content_type, ';')) = '\0';
395 striplt(content_type);
397 if (!IsEmptyStr(boundary)) {
403 /* If this is a multipart message, then recursively process it */
407 /* Tell the client about this message's multipartedness */
408 if (PreMultiPartCallBack != NULL) {
409 PreMultiPartCallBack("", "", partnum, "",
410 NULL, content_type, charset,
411 0, encoding, id, userdata);
414 /* Figure out where the boundaries are */
415 startary_len = snprintf(startary, SIZ, "--%s", boundary);
416 snprintf(endary, SIZ, "--%s--", boundary);
420 next_boundary = NULL;
421 for (srch=ptr; srch<content_end; ++srch) {
422 if (!memcmp(srch, startary, startary_len)) {
423 next_boundary = srch;
428 if ( (part_start != NULL) && (next_boundary != NULL) ) {
429 part_end = next_boundary;
430 --part_end; /* omit the trailing LF */
432 --part_end; /* omit the trailing CR */
435 if (!IsEmptyStr(partnum)) {
436 snprintf(nested_partnum,
437 sizeof nested_partnum,
442 snprintf(nested_partnum,
443 sizeof nested_partnum,
446 the_mime_parser(nested_partnum,
447 part_start, part_end,
449 PreMultiPartCallBack,
450 PostMultiPartCallBack,
455 if (next_boundary != NULL) {
456 /* If we pass out of scope, don't attempt to
457 * read past the end boundary. */
458 if (!strcmp(next_boundary, endary)) {
462 /* Set up for the next part. */
463 part_start = strstr(next_boundary, "\n");
465 /* Determine whether newlines are LF or CRLF */
466 evaluate_crlf_ptr = part_start;
468 if (!memcmp(evaluate_crlf_ptr, "\r\n", 2)) {
475 /* Advance past the LF ... now we're in the next part */
481 /* Invalid end of multipart. Bail out! */
484 } while ( (ptr < content_end) && (next_boundary != NULL) );
486 if (PostMultiPartCallBack != NULL) {
487 PostMultiPartCallBack("", "", partnum, "", NULL,
488 content_type, charset, 0, encoding, id, userdata);
493 /* If it's not a multipart message, then do something with it */
497 while (ptr < content_end) {
501 part_end = content_end;
504 /* The following code will truncate the MIME part to the size
505 * specified by the Content-length: header. We have commented it
506 * out because these headers have a tendency to be wrong.
508 * if ( (content_length > 0) && (length > content_length) ) {
509 * length = content_length;
513 /* Sometimes the "name" field is tacked on to Content-type,
514 * and sometimes it's tacked on to Content-disposition. Use
515 * whichever one we have.
517 if (strlen(content_disposition_name) > strlen(content_type_name)) {
518 name = content_disposition_name;
521 name = content_type_name;
524 /* Ok, we've got a non-multipart part here, so do something with it.
528 content_type, charset, encoding, disposition, id,
530 CallBack, NULL, NULL,
531 userdata, dont_decode
535 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
537 if (!strcasecmp(content_type, "message/rfc822")) {
539 if (PreMultiPartCallBack != NULL) {
540 PreMultiPartCallBack("", "", partnum, "",
541 NULL, content_type, charset,
542 0, encoding, id, userdata);
544 if (CallBack != NULL) {
545 if (strlen(partnum) > 0) {
546 snprintf(nested_partnum,
547 sizeof nested_partnum,
552 snprintf(nested_partnum,
553 sizeof nested_partnum,
556 the_mime_parser(nested_partnum,
557 part_start, part_end,
559 PreMultiPartCallBack,
560 PostMultiPartCallBack,
565 if (PostMultiPartCallBack != NULL) {
566 PostMultiPartCallBack("", "", partnum, "", NULL,
567 content_type, charset, 0, encoding, id, userdata);
575 end_parser: /* free the buffers! end the oppression!! */
582 * Entry point for the MIME parser.
583 * (This function expects to be fed HEADERS + CONTENT)
584 * Note: NULL can be supplied as content_end; in this case, the message is
585 * considered to have ended when the parser encounters a 0x00 byte.
587 void mime_parser(char *content_start,
589 MimeParserCallBackType CallBack,
590 MimeParserCallBackType PreMultiPartCallBack,
591 MimeParserCallBackType PostMultiPartCallBack,
596 the_mime_parser("", content_start, content_end,
598 PreMultiPartCallBack,
599 PostMultiPartCallBack,
600 userdata, dont_decode);
608 typedef struct _MimeGuess {
612 const char *MimeString;
615 MimeGuess MyMimes [] = {
643 const char *GuessMimeType(const char *data, size_t dlen)
647 while (MyMimes[MimeIndex].PatternLen != 0)
649 if ((MyMimes[MimeIndex].PatternLen +
650 MyMimes[MimeIndex].PatternOffset < dlen) &&
651 strncmp(MyMimes[MimeIndex].Pattern,
652 &data[MyMimes[MimeIndex].PatternOffset],
653 MyMimes[MimeIndex].PatternLen) == 0)
655 return MyMimes[MimeIndex].MimeString;
660 * ok, our simple minded algorythm didn't find anything,
661 * let the big chegger try it, he wil default to application/octet-stream
663 return (xdg_mime_get_mime_type_for_data(data, dlen));
667 const char* GuessMimeByFilename(const char *what, size_t len)
669 /* we know some hardcoded on our own, try them... */
670 if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
672 else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
673 return "text/javascript";
674 else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
676 else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
678 else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
680 else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
682 else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
683 return "image/x-icon";
684 else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
685 return "text/x-vcard";
686 else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
688 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
690 else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
691 return "text/vnd.wap.wml";
692 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
693 return "text/vnd.wap.wmlscript";
694 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
695 return "application/vnd.wap.wmlc";
696 else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
697 return "application/vnd.wap.wmlscriptc";
698 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
699 return "image/vnd.wap.wbmp";
701 /* and let xdgmime do the fallback. */
702 return xdg_mime_get_mime_type_from_file_name(what);
705 static HashList *IconHash = NULL;
707 typedef struct IconName IconName;
714 static void DeleteIcon(void *IconNamePtr)
716 IconName *Icon = (IconName*) IconNamePtr;
717 free(Icon->FlatName);
718 free(Icon->FileName);
723 static const char *PrintFlat(void *IconNamePtr)
725 IconName *Icon = (IconName*) IconNamePtr;
726 return Icon->FlatName;
728 static const char *PrintFile(void *IconNamePtr)
730 IconName *Icon = (IconName*) IconNamePtr;
731 return Icon->FileName;
735 #define GENSTR "x-generic"
736 #define IGNORE_PREFIX_1 "gnome-mime"
737 int LoadIconDir(const char *DirName)
740 struct dirent *filedir_entry;
745 filedir = opendir (DirName);
746 IconHash = NewHash(1, NULL);
747 if (filedir == NULL) {
751 while ((filedir_entry = readdir(filedir)))
755 #ifdef _DIRENT_HAVE_D_NAMELEN
756 d_namelen = filedir_entry->d_namelen;
758 d_namelen = strlen(filedir_entry->d_name);
760 d_without_ext = d_namelen;
761 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
763 if ((d_without_ext == 0) || (d_namelen < 3))
766 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
767 (strncmp(IGNORE_PREFIX_1,
768 filedir_entry->d_name,
769 sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
770 PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
771 d_without_ext -= sizeof(IGNORE_PREFIX_1);
774 PStart = filedir_entry->d_name;
776 Icon = malloc(sizeof(IconName));
778 Icon->FileName = malloc(d_namelen + 1);
779 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
781 Icon->FlatName = malloc(d_without_ext + 1);
782 memcpy(Icon->FlatName, PStart, d_without_ext);
783 Icon->FlatName[d_without_ext] = '\0';
784 /* Try to find Minor type in image-jpeg */
785 MinorPtr = strchr(Icon->FlatName, '-');
786 if (MinorPtr != NULL) {
788 MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
789 if ((MinorLen == sizeof(GENSTR)) &&
790 (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
791 /* ok, we found a generic filename. cut the generic. */
793 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
795 else { /* Map the major / minor separator to / */
800 // PrintHash(IconHash, PrintFlat, PrintFile);
801 // printf("%s - %s\n", Icon->FlatName, Icon->FileName);
802 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
803 // PrintHash(IconHash, PrintFlat, PrintFile);
809 const char *GetIconFilename(char *MimeType, size_t len)
817 GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
818 /* didn't find the exact mimetype? try major only. */
821 pMinor = strchr(MimeType, '/');
822 if (pMinor != NULL) {
824 GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
825 Icon = (IconName*) vIcon;
832 /*printf("Getting: [%s] == [%s] -> [%s]\n", MimeType, Icon->FlatName, Icon->FileName);*/
833 return Icon->FileName;
836 void ShutDownLibCitadelMime(void)
838 DeleteHash(&IconHash);