-/*
- * This is the MIME parser for Citadel.
- *
- * Copyright (c) 1998-2010 by the citadel.org development team.
- * This code is distributed under the GNU General Public License v3.
- *
- */
+// This is the MIME parser for Citadel.
+//
+// Copyright (c) 1998-2023 by the citadel.org development team.
+//
+// This program is open source software. Use, duplication, or disclosure
+// is subject to the terms of the GNU General Public License, version 3.
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 10
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 20
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 10
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 20
-long extract_key(char *target, char *source, long sourcelen, char *key, long keylen, char KeyEnd)
-{
+long extract_key(char *target, char *source, long sourcelen, char *key, long keylen, char KeyEnd) {
- while (sptr != NULL)
- {
- ptr = bmstrcasestr_len(sptr, sourcelen - (sptr - source),
- key, keylen);
- if(ptr != NULL)
- {
+ while (sptr != NULL) {
+ ptr = bmstrcasestr_len(sptr, sourcelen - (sptr - source), key, keylen);
+ if (ptr != NULL) {
-/*
- * For non-multipart messages, we need to generate a quickie partnum of "1"
- * to return to callback functions. Some callbacks demand it.
- */
+// For non-multipart messages, we need to generate a quickie partnum of "1"
+// to return to callback functions. Some callbacks demand it.
char *fixed_partnum(char *supplied_partnum) {
if (supplied_partnum == NULL) return "1";
if (strlen(supplied_partnum)==0) return "1";
char *fixed_partnum(char *supplied_partnum) {
if (supplied_partnum == NULL) return "1";
if (strlen(supplied_partnum)==0) return "1";
-/*
- * Convert "quoted-printable" to binary. Returns number of bytes decoded.
- * according to RFC2045 section 6.7
- */
+
+// Convert "quoted-printable" to binary. Returns number of bytes decoded.
+// according to RFC2045 section 6.7
-/*
- * Given a message or message-part body and a length, handle any necessary
- * decoding and pass the request up the stack.
- */
+// Given a message or message-part body and a length, handle any necessary
+// decoding and pass the request up the stack.
void mime_decode(char *partnum,
char *part_start, size_t length,
char *content_type, char *charset, char *encoding,
void mime_decode(char *partnum,
char *part_start, size_t length,
char *content_type, char *charset, char *encoding,
MimeParserCallBackType PreMultiPartCallBack,
MimeParserCallBackType PostMultiPartCallBack,
void *userdata,
MimeParserCallBackType PreMultiPartCallBack,
MimeParserCallBackType PostMultiPartCallBack,
void *userdata,
- /* Fail silently if we hit an unknown encoding. */
- if ((strcasecmp(encoding, "base64"))
- && (strcasecmp(encoding, "quoted-printable"))) {
+ // Fail silently if we hit an unknown encoding.
+ if ((strcasecmp(encoding, "base64")) && (strcasecmp(encoding, "quoted-printable"))) {
- /*
- * Allocate a buffer for the decoded data. The output buffer is slightly
- * larger than the input buffer; this assumes that the decoded data
- * will never be significantly larger than the encoded data. This is a
- * safe assumption with base64, uuencode, and quoted-printable.
- */
+ // Allocate a buffer for the decoded data. The output buffer is slightly
+ // larger than the input buffer; this assumes that the decoded data
+ // will never be significantly larger than the encoded data. This is a
+ // safe assumption with base64, uuencode, and quoted-printable.
-/*
- * this is the extract of mime_decode which can be called if 'dont_decode' was set;
- * to save the cpu intense process of decoding to the time when it realy wants the content.
- * returns:
- * - > 0 we decoded something, its on *decoded, you need to free it.
- * - = 0 no need to decode stuff. *decoded will be NULL.
- * - < 0 an error occured, either an unknown encoding, or alloc failed. no need to free.
- */
+
+// this is a bastardization of mime_decode() which can be called if 'dont_decode' was set;
+// to postpone the cpu intense process of decoding until the time when it realy wants the content.
+// returns:
+// - > 0 we decoded something, its on *decoded, you need to free it.
+// - = 0 no need to decode stuff. *decoded will be NULL.
+// - < 0 an error occured, either an unknown encoding, or alloc failed. no need to free.
- /* Fail if we hit an unknown encoding. */
- if ((strcasecmp(encoding, "base64"))
- && (strcasecmp(encoding, "quoted-printable"))) {
+ // Fail if we hit an unknown encoding.
+ if ((strcasecmp(encoding, "base64")) && (strcasecmp(encoding, "quoted-printable"))) {
- /*
- * Allocate a buffer for the decoded data. The output buffer is slightly
- * larger than the input buffer; this assumes that the decoded data
- * will never be significantly larger than the encoded data. This is a
- * safe assumption with base64, uuencode, and quoted-printable.
- */
+ // Allocate a buffer for the decoded data. The output buffer is slightly
+ // larger than the input buffer; this assumes that the decoded data
+ // will never be significantly larger than the encoded data. This is a
+ // safe assumption with base64, uuencode, and quoted-printable.
interesting_mime_headers *m;
m = (interesting_mime_headers*) malloc( sizeof(interesting_mime_headers));
interesting_mime_headers *m;
m = (interesting_mime_headers*) malloc( sizeof(interesting_mime_headers));
if (!isspace(buf[0]) && (headerlen > 0)) {
if (!strncasecmp(header, "Content-type:", 13)) {
memcpy (m->b[content_type].Key, &header[13], headerlen - 12);
if (!isspace(buf[0]) && (headerlen > 0)) {
if (!strncasecmp(header, "Content-type:", 13)) {
memcpy (m->b[content_type].Key, &header[13], headerlen - 12);
m->b[content_type_name].len = extract_key(m->b[content_type_name].Key, CKEY(m->b[content_type]), HKEY("name"), '=');
m->b[charset].len = extract_key(m->b[charset].Key, CKEY(m->b[content_type]), HKEY("charset"), '=');
m->b[boundary].len = extract_key(m->b[boundary].Key, header, headerlen, HKEY("boundary"), '=');
m->b[content_type_name].len = extract_key(m->b[content_type_name].Key, CKEY(m->b[content_type]), HKEY("name"), '=');
m->b[charset].len = extract_key(m->b[charset].Key, CKEY(m->b[content_type]), HKEY("charset"), '=');
m->b[boundary].len = extract_key(m->b[boundary].Key, header, headerlen, HKEY("boundary"), '=');
}
else if (!strncasecmp(header, "Content-Disposition:", 20)) {
memcpy (m->b[disposition].Key, &header[20], headerlen - 19);
}
else if (!strncasecmp(header, "Content-Disposition:", 20)) {
memcpy (m->b[disposition].Key, &header[20], headerlen - 19);
m->b[content_disposition_name].len = extract_key(m->b[content_disposition_name].Key, CKEY(m->b[disposition]), HKEY("name"), '=');
m->b[filename].len = extract_key(m->b[filename].Key, CKEY(m->b[disposition]), HKEY("filename"), '=');
pch = strchr(m->b[disposition].Key, ';');
if (pch != NULL) *pch = '\0';
m->b[content_disposition_name].len = extract_key(m->b[content_disposition_name].Key, CKEY(m->b[disposition]), HKEY("name"), '=');
m->b[filename].len = extract_key(m->b[filename].Key, CKEY(m->b[disposition]), HKEY("filename"), '=');
pch = strchr(m->b[disposition].Key, ';');
if (pch != NULL) *pch = '\0';
- memcpy(m->b[id].Key, &header[11], headerlen);
- striplt(m->b[id].Key);
+ memcpy(m->b[id].Key, &header[11], headerlen - 11);
+ m->b[id].Key[headerlen - 11] = '\0';
+ string_trim(m->b[id].Key);
m->b[id].len = stripallbut(m->b[id].Key, '<', '>');
}
else if (!strncasecmp(header, "Content-length: ", 15)) {
m->b[id].len = stripallbut(m->b[id].Key, '<', '>');
}
else if (!strncasecmp(header, "Content-length: ", 15)) {
}
else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
memcpy(m->b[encoding].Key, &header[26], headerlen - 26);
}
else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
memcpy(m->b[encoding].Key, &header[26], headerlen - 26);
-static char *FindNextContent(char *ptr,
- char *content_end,
- interesting_mime_headers *SubMimeHeaders,
- interesting_mime_headers *m)
-{
+
+static char *FindNextContent(
+ char *ptr,
+ char *content_end,
+ interesting_mime_headers *SubMimeHeaders,
+ interesting_mime_headers *m
+) {
- /**
- * ok, if we have a content length of the mime part,
- * try skipping the content on the search for the next
- * boundary. since we don't trust the content_length
- * to be all accurate, and suspect it to lose one digit
- * per line with a line length of 80 chars, we need
- * to start searching a little before..
- */
-
- if ((SubMimeHeaders->content_length != -1) &&
- (SubMimeHeaders->content_length > 10))
- {
+ // ok, if we have a content length of the mime part,
+ // try skipping the content on the search for the next
+ // boundary. since we don't trust the content_length
+ // to be all accurate, and suspect it to lose one digit
+ // per line with a line length of 80 chars, we need
+ // to start searching a little before..
+
+ if ((SubMimeHeaders->content_length != -1) && (SubMimeHeaders->content_length > 10)) {
char *pptr;
long lines;
lines = SubMimeHeaders->content_length / 80;
pptr = ptr + SubMimeHeaders->content_length - lines - 10;
char *pptr;
long lines;
lines = SubMimeHeaders->content_length / 80;
pptr = ptr + SubMimeHeaders->content_length - lines - 10;
- /**
- * ok, if we have a content length of the mime part,
- * try skipping the content on the search for the next
- * boundary. since we don't trust the content_length
- * to be all accurate, start searching a little before..
- */
-
- if ((SubMimeHeaders->content_length != -1) &&
- (SubMimeHeaders->content_length > 10))
- {
+ // ok, if we have a content length of the mime part,
+ // try skipping the content on the search for the next
+ // boundary. since we don't trust the content_length
+ // to be all accurate, start searching a little before..
+
+ if ((SubMimeHeaders->content_length != -1) && (SubMimeHeaders->content_length > 10)) {
-
- next_boundary = NULL;
- for (srch=ptr;
- (srch != NULL) && (srch < content_end);
- srch = memchr(srch, '-', content_end - srch))
+ srch = next_boundary = NULL;
+ for (srch = memchr(ptr, '-', content_end - ptr);
+ (srch != NULL) && (srch < content_end);
+ srch = memchr(srch, '-', content_end - srch))
-/*
- * Break out the components of a multipart message
- * (This function expects to be fed HEADERS + CONTENT)
- * Note: NULL can be supplied as content_end; in this case, the message is
- * considered to have ended when the parser encounters a 0x00 byte.
- */
+
+// Break out the components of a multipart message
+// (This function expects to be fed HEADERS + CONTENT)
+// Note: NULL can be supplied as content_end; in this case, the message is
+// considered to have ended when the parser encounters a 0x00 byte.
- char *content_start, char *content_end,
- MimeParserCallBackType CallBack,
- MimeParserCallBackType PreMultiPartCallBack,
- MimeParserCallBackType PostMultiPartCallBack,
- void *userdata,
- int dont_decode,
- interesting_mime_headers *m)
-{
+ char *content_start, char *content_end,
+ MimeParserCallBackType CallBack,
+ MimeParserCallBackType PreMultiPartCallBack,
+ MimeParserCallBackType PostMultiPartCallBack,
+ void *userdata,
+ int dont_decode,
+ interesting_mime_headers *m
+) {
- char *ptr;
- char *part_start;
- char *part_end = NULL;
- char *evaluate_crlf_ptr = NULL;
- char *next_boundary;
- char nested_partnum[256];
- int crlf_in_use = 0;
- int part_seq = 0;
- CBufStr *chosen_name;
-
-
- /* If this is a multipart message, then recursively process it */
+ char *ptr;
+ char *part_start;
+ char *part_end = NULL;
+ char *evaluate_crlf_ptr = NULL;
+ char *next_boundary;
+ char nested_partnum[256];
+ int crlf_in_use = 0;
+ int part_seq = 0;
+ CBufStr *chosen_name;
+
+ // If this is a multipart message, then recursively process it
m->b[startary].len = snprintf(m->b[startary].Key, SIZ, "--%s", m->b[boundary].Key);
m->b[startary].len = snprintf(m->b[startary].Key, SIZ, "--%s", m->b[boundary].Key);
- SubMimeHeaders = InitInterestingMimes ();
- if (*ptr == '\r')
- ptr ++;
- if (*ptr == '\n')
- ptr ++;
- if (strncmp(ptr, m->b[startary].Key, m->b[startary].len) == 0)
+ SubMimeHeaders = InitInterestingMimes();
+
+ while ((*ptr == '\r') || (*ptr == '\n')) {
+ ptr++;
+ }
+
+ if (strncmp(ptr, m->b[startary].Key, m->b[startary].len) == 0) {
- next_boundary = FindNextContent(ptr,
- content_end,
- SubMimeHeaders,
- m);
- if ((next_boundary != NULL) &&
- (next_boundary - part_start < 3))
+ next_boundary = FindNextContent(ptr, content_end, SubMimeHeaders, m);
+ if ((next_boundary != NULL) && (next_boundary - part_start < 3)) {
+ FlushInterestingMimes(SubMimeHeaders);
- /* If we pass out of scope, don't attempt to
- * read past the end boundary. */
- if ((*(next_boundary + m->b[startary].len + 1) == '-') &&
- (*(next_boundary + m->b[startary].len + 2) == '-') ){
+ // If we pass out of scope, don't attempt to read past the end boundary.
+ if ((*(next_boundary + m->b[startary].len) == '-') &&
+ (*(next_boundary + m->b[startary].len + 1) == '-') ){
if (PostMultiPartCallBack != NULL) {
PostMultiPartCallBack("",
if (PostMultiPartCallBack != NULL) {
PostMultiPartCallBack("",
-
- /* The following code will truncate the MIME part to the size
- * specified by the Content-length: header. We have commented it
- * out because these headers have a tendency to be wrong.
- *
- * if ( (content_length > 0) && (length > content_length) ) {
- * length = content_length;
- * }
- */
-
- /* Sometimes the "name" field is tacked on to Content-type,
- * and sometimes it's tacked on to Content-disposition. Use
- * whichever one we have.
- */
+ // The following code will truncate the MIME part to the size
+ // specified by the Content-length: header. We have commented it
+ // out because these headers have a tendency to be wrong.
+ //
+ // if ( (content_length > 0) && (length > content_length) ) {
+ // length = content_length;
+ // }
+
+ // Sometimes the "name" field is tacked on to Content-type,
+ // and sometimes it's tacked on to Content-disposition. Use
+ // whichever one we have.
if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
chosen_name = &m->b[content_disposition_name];
}
if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
chosen_name = &m->b[content_disposition_name];
}
if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
if (PreMultiPartCallBack != NULL) {
PreMultiPartCallBack("",
if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
if (PreMultiPartCallBack != NULL) {
PreMultiPartCallBack("",
}
if (PostMultiPartCallBack != NULL) {
PostMultiPartCallBack("",
}
if (PostMultiPartCallBack != NULL) {
PostMultiPartCallBack("",
-/*
- * Break out the components of a multipart message
- * (This function expects to be fed HEADERS + CONTENT)
- * Note: NULL can be supplied as content_end; in this case, the message is
- * considered to have ended when the parser encounters a 0x00 byte.
- */
+
+// Break out the components of a multipart message
+// (This function expects to be fed HEADERS + CONTENT)
+// Note: NULL can be supplied as content_end; in this case, the message is
+// considered to have ended when the parser encounters a 0x00 byte.
void the_mime_parser(char *partnum,
char *content_start, char *content_end,
MimeParserCallBackType CallBack,
void the_mime_parser(char *partnum,
char *content_start, char *content_end,
MimeParserCallBackType CallBack,
if (content_end == NULL) {
content_end = &content_start[strlen(content_start)];
}
m = InitInterestingMimes();
if (content_end == NULL) {
content_end = &content_start[strlen(content_start)];
}
m = InitInterestingMimes();
recurseable_mime_parser(partnum,
content_start, content_end,
CallBack,
recurseable_mime_parser(partnum,
content_start, content_end,
CallBack,
-/*
- * Entry point for the MIME parser.
- * (This function expects to be fed HEADERS + CONTENT)
- * Note: NULL can be supplied as content_end; in this case, the message is
- * considered to have ended when the parser encounters a 0x00 byte.
- */
+
+// Entry point for the MIME parser.
+// (This function expects to be fed HEADERS + CONTENT)
+// Note: NULL can be supplied as content_end; in this case, the message is
+// considered to have ended when the parser encounters a 0x00 byte.
void mime_parser(char *content_start,
char *content_end,
MimeParserCallBackType CallBack,
void mime_parser(char *content_start,
char *content_end,
MimeParserCallBackType CallBack,
the_mime_parser("", content_start, content_end,
CallBack,
PreMultiPartCallBack,
the_mime_parser("", content_start, content_end,
CallBack,
PreMultiPartCallBack,
if ((MyMimes[MimeIndex].PatternLen +
MyMimes[MimeIndex].PatternOffset < dlen) &&
strncmp(MyMimes[MimeIndex].Pattern,
if ((MyMimes[MimeIndex].PatternLen +
MyMimes[MimeIndex].PatternOffset < dlen) &&
strncmp(MyMimes[MimeIndex].Pattern,
- /*
- * ok, our simple minded algorythm didn't find anything,
- * let the big chegger try it, he wil default to application/octet-stream
- */
+ // ok, our simple minded algorythm didn't find anything,
+ // let the big chegger try it, he wil default to application/octet-stream
-const char* GuessMimeByFilename(const char *what, size_t len)
-{
- /* we know some hardcoded on our own, try them... */
+const char* GuessMimeByFilename(const char *what, size_t len) {
+ // we know some hardcoded on our own, try them...
if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
return "image/gif";
else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
return "image/gif";
else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
return "text/x-component";
else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
return "image/jpeg";
return "text/x-component";
else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
return "image/jpeg";
else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
return "image/png";
else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
return "image/png";
else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
return "image/vnd.wap.wbmp";
else
else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
return "image/vnd.wap.wbmp";
else
Icon->FlatName = malloc(d_without_ext + 1);
memcpy(Icon->FlatName, PStart, d_without_ext);
Icon->FlatName[d_without_ext] = '\0';
Icon->FlatName = malloc(d_without_ext + 1);
memcpy(Icon->FlatName, PStart, d_without_ext);
Icon->FlatName[d_without_ext] = '\0';
MinorPtr = strchr(Icon->FlatName, '-');
if (MinorPtr != NULL) {
size_t MinorLen;
MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
if ((MinorLen == sizeof(GENSTR)) &&
(strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
MinorPtr = strchr(Icon->FlatName, '-');
if (MinorPtr != NULL) {
size_t MinorLen;
MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
if ((MinorLen == sizeof(GENSTR)) &&
(strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;