Skip to content
Snippets Groups Projects
Commit 7c0dbbb4 authored by rswindell's avatar rswindell
Browse files

MIME header fields are case-insensitive. <sigh>

So replace some strstr() calls with either (new local function) strStartWith_i()
or strcasestr(), depending.
strStartWith_i() return length of the matched word, so no need to sprinkle
about magic numeric constants everywhere. The extra calls to strlen() are worth
the code clarity / reliability, methinks.
TODO: find out if there's a way to calculate the length of string-constants at
compile-time (?).
parent a0067415
Branches
Tags
No related merge requests found
...@@ -251,17 +251,26 @@ char* qp_decode(char* buf) ...@@ -251,17 +251,26 @@ char* qp_decode(char* buf)
return buf; return buf;
} }
static size_t strStartsWith_i(const char* buf, const char* match)
{
size_t len = strlen(match);
if (strnicmp(buf, match, len) == 0)
return len;
return 0;
}
static enum content_transfer_encoding mime_getxferencoding(const char* beg, const char* end) static enum content_transfer_encoding mime_getxferencoding(const char* beg, const char* end)
{ {
const char* p = beg; const char* p = beg;
while(p < end) { while(p < end) {
SKIP_WHITESPACE(p); SKIP_WHITESPACE(p);
if(strnicmp(p, "content-transfer-encoding:", 26) != 0) { size_t len = strStartsWith_i(p, "content-transfer-encoding:");
if(len < 1) {
FIND_CHAR(p, '\n'); FIND_CHAR(p, '\n');
continue; continue;
} }
p += 26; p += len;
SKIP_WHITESPACE(p); SKIP_WHITESPACE(p);
if(strnicmp(p, "base64", 6) == 0) if(strnicmp(p, "base64", 6) == 0)
return CONTENT_TRANFER_ENCODING_BASE64; return CONTENT_TRANFER_ENCODING_BASE64;
...@@ -283,11 +292,12 @@ static BOOL mime_getattachment(const char* beg, const char* end, char* attachmen ...@@ -283,11 +292,12 @@ static BOOL mime_getattachment(const char* beg, const char* end, char* attachmen
while(p < end) { while(p < end) {
SKIP_WHITESPACE(p); SKIP_WHITESPACE(p);
if(strnicmp(p, "content-disposition:", 20) != 0) { size_t len = strStartsWith_i(p, "content-disposition:");
if(len < 1) {
FIND_CHAR(p, '\n'); FIND_CHAR(p, '\n');
continue; continue;
} }
p += 20; p += len;
SKIP_WHITESPACE(p); SKIP_WHITESPACE(p);
if(strnicmp(p, "inline", 6) == 0) { if(strnicmp(p, "inline", 6) == 0) {
FIND_CHAR(p, '\n'); FIND_CHAR(p, '\n');
...@@ -338,8 +348,9 @@ void SMBCALL smb_parse_content_type(const char* content_type, char** subtype, ch ...@@ -338,8 +348,9 @@ void SMBCALL smb_parse_content_type(const char* content_type, char** subtype, ch
char* p; char* p;
if((p = strstr(buf, "\r\n\r\n")) != NULL) /* Don't parse past the end of header */ if((p = strstr(buf, "\r\n\r\n")) != NULL) /* Don't parse past the end of header */
*p = 0; *p = 0;
if((p = strstr(buf, "text/")) == buf) { size_t len = strStartsWith_i(buf, "text/");
p += 5; if(len > 0) {
p = buf + len;
if(subtype != NULL) { if(subtype != NULL) {
if((*subtype = strdup(p)) != NULL) { if((*subtype = strdup(p)) != NULL) {
char* tp = *subtype; char* tp = *subtype;
...@@ -350,7 +361,7 @@ void SMBCALL smb_parse_content_type(const char* content_type, char** subtype, ch ...@@ -350,7 +361,7 @@ void SMBCALL smb_parse_content_type(const char* content_type, char** subtype, ch
*tp = 0; *tp = 0;
} }
} }
if(charset != NULL && (p = strstr(p, "charset=")) != NULL) { if(charset != NULL && (p = strcasestr(p, "charset=")) != NULL) {
p += 8; p += 8;
if(*p == '"') if(*p == '"')
p++; p++;
...@@ -365,7 +376,7 @@ void SMBCALL smb_parse_content_type(const char* content_type, char** subtype, ch ...@@ -365,7 +376,7 @@ void SMBCALL smb_parse_content_type(const char* content_type, char** subtype, ch
} }
} }
/* Find the specified content-type in a MIME-encoded message body, recursively */ /* Find the specified content-type in a multi-pat MIME-encoded message body, recursively */
static const char* mime_getcontent(const char* buf, const char* content_type, const char* content_match static const char* mime_getcontent(const char* buf, const char* content_type, const char* content_match
,int depth, enum content_transfer_encoding* encoding, char** charset, char* attachment, size_t attachment_len, int index) ,int depth, enum content_transfer_encoding* encoding, char** charset, char* attachment, size_t attachment_len, int index)
{ {
...@@ -386,15 +397,14 @@ static const char* mime_getcontent(const char* buf, const char* content_type, co ...@@ -386,15 +397,14 @@ static const char* mime_getcontent(const char* buf, const char* content_type, co
return NULL; return NULL;
if(content_type == NULL) /* Not MIME-encoded */ if(content_type == NULL) /* Not MIME-encoded */
return NULL; return NULL;
if(strstr(content_type, "multipart/alternative;") == content_type) size_t len;
content_type += 22; if(((len = strStartsWith_i(content_type, "multipart/alternative;")) < 1)
else if(strstr(content_type, "multipart/mixed;") == content_type) && ((len = strStartsWith_i(content_type, "multipart/mixed;")) < 1)
content_type +=16; && ((len = strStartsWith_i(content_type, "multipart/report;")) < 1)
else if(strstr(content_type, "multipart/report;") == content_type) && ((len = strStartsWith_i(content_type, "multipart/")) < 1))
content_type +=17;
else
return NULL; return NULL;
p = strstr(content_type, "boundary="); content_type += len;
p = strcasestr(content_type, "boundary=");
if(p == NULL) if(p == NULL)
return NULL; return NULL;
p += 9; p += 9;
...@@ -414,8 +424,8 @@ static const char* mime_getcontent(const char* buf, const char* content_type, co ...@@ -414,8 +424,8 @@ static const char* mime_getcontent(const char* buf, const char* content_type, co
continue; continue;
for(content_type = txt; content_type < p; content_type++) { for(content_type = txt; content_type < p; content_type++) {
SKIP_WHITESPACE(content_type); SKIP_WHITESPACE(content_type);
if(strnicmp(content_type, "Content-Type:", 13) == 0) { if((len = strStartsWith_i(content_type, "Content-Type:")) > 0) {
content_type += 13; content_type += len;
SKIP_WHITESPACE(content_type); SKIP_WHITESPACE(content_type);
break; break;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment