From: Richard Russon Date: Sat, 14 Jul 2018 16:45:16 +0000 (+0100) Subject: tidy parse X-Git-Tag: 2019-10-25~755^2~6 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=187ae0d73d3b15121e18de562130effe6b77353b;p=neomutt tidy parse --- diff --git a/email/parse.c b/email/parse.c index c03e74fb0..a4dcf138e 100644 --- a/email/parse.c +++ b/email/parse.c @@ -43,152 +43,6 @@ #include "rfc2231.h" #include "url.h" -/** - * mutt_matches_ignore - Does the string match the ignore list - * @param s String to check - * @retval true If string matches - * - * Checks Ignore and UnIgnore using mutt_list_match - */ -bool mutt_matches_ignore(const char *s) -{ - return mutt_list_match(s, &Ignore) && !mutt_list_match(s, &UnIgnore); -} - -/** - * mutt_check_mime_type - Check a MIME type string - * @param s String to check - * @retval num MIME type, e.g. #TYPE_TEXT - */ -int mutt_check_mime_type(const char *s) -{ - if (mutt_str_strcasecmp("text", s) == 0) - return TYPE_TEXT; - else if (mutt_str_strcasecmp("multipart", s) == 0) - return TYPE_MULTIPART; -#ifdef SUN_ATTACHMENT - else if (mutt_str_strcasecmp("x-sun-attachment", s) == 0) - return TYPE_MULTIPART; -#endif - else if (mutt_str_strcasecmp("application", s) == 0) - return TYPE_APPLICATION; - else if (mutt_str_strcasecmp("message", s) == 0) - return TYPE_MESSAGE; - else if (mutt_str_strcasecmp("image", s) == 0) - return TYPE_IMAGE; - else if (mutt_str_strcasecmp("audio", s) == 0) - return TYPE_AUDIO; - else if (mutt_str_strcasecmp("video", s) == 0) - return TYPE_VIDEO; - else if (mutt_str_strcasecmp("model", s) == 0) - return TYPE_MODEL; - else if (mutt_str_strcasecmp("*", s) == 0) - return TYPE_ANY; - else if (mutt_str_strcasecmp(".*", s) == 0) - return TYPE_ANY; - else - return TYPE_OTHER; -} - -/** - * mutt_extract_message_id - Find a message-id - * @param s String to parse - * @param saveptr Save result here - * @retval ptr First character after message-id - * @retval NULL No more message ids - * - * Extract the first substring that looks like a message-id. - * Call back with NULL for more (like strtok). - */ -char *mutt_extract_message_id(const char *s, const char **saveptr) -{ - const char *o = NULL, *onull = NULL, *p = NULL; - char *ret = NULL; - - if (s) - p = s; - else if (saveptr) - p = *saveptr; - else - return NULL; - - for (s = NULL, o = NULL, onull = NULL; (p = strpbrk(p, "<> \t;")) != NULL; ++p) - { - if (*p == '<') - { - s = p; - o = onull = NULL; - continue; - } - - if (!s) - continue; - - if (*p == '>') - { - size_t olen = onull - o, slen = p - s + 1; - ret = mutt_mem_malloc(olen + slen + 1); - if (o) - memcpy(ret, o, olen); - memcpy(ret + olen, s, slen); - ret[olen + slen] = '\0'; - if (saveptr) - *saveptr = p + 1; /* next call starts after '>' */ - return ret; - } - - /* some idiotic clients break their message-ids between lines */ - if (s == p) - { - /* step past another whitespace */ - s = p + 1; - } - else if (o) - { - /* more than two lines, give up */ - s = o = onull = NULL; - } - else - { - /* remember the first line, start looking for the second */ - o = s; - onull = p; - s = p + 1; - } - } - - return NULL; -} - -/** - * mutt_check_encoding - Check the encoding type - * @param c String to check - * @retval num Encoding type, e.g. #ENC_QUOTED_PRINTABLE - */ -int mutt_check_encoding(const char *c) -{ - if (mutt_str_strncasecmp("7bit", c, sizeof("7bit") - 1) == 0) - return ENC_7BIT; - else if (mutt_str_strncasecmp("8bit", c, sizeof("8bit") - 1) == 0) - return ENC_8BIT; - else if (mutt_str_strncasecmp("binary", c, sizeof("binary") - 1) == 0) - return ENC_BINARY; - else if (mutt_str_strncasecmp("quoted-printable", c, sizeof("quoted-printable") - 1) == 0) - { - return ENC_QUOTED_PRINTABLE; - } - else if (mutt_str_strncasecmp("base64", c, sizeof("base64") - 1) == 0) - return ENC_BASE64; - else if (mutt_str_strncasecmp("x-uuencode", c, sizeof("x-uuencode") - 1) == 0) - return ENC_UUENCODED; -#ifdef SUN_ATTACHMENT - else if (mutt_str_strncasecmp("uuencode", c, sizeof("uuencode") - 1) == 0) - return ENC_UUENCODED; -#endif - else - return ENC_OTHER; -} - /** * parse_parameters - Parse a list of Parameters * @param param Parameter list for the results @@ -240,21 +94,21 @@ static void parse_parameters(struct ParameterList *param, const char *s) { bool state_ascii = true; s++; - for (i = 0; *s && i < sizeof(buffer) - 1; i++, s++) + for (i = 0; *s && (i < (sizeof(buffer) - 1)); i++, s++) { if (AssumedCharset && *AssumedCharset) { /* As iso-2022-* has a character of '"' with non-ascii state, * ignore it. */ - if (*s == 0x1b && i < sizeof(buffer) - 2) + if ((*s == 0x1b) && (i < (sizeof(buffer) - 2))) { - if (s[1] == '(' && (s[2] == 'B' || s[2] == 'J')) + if ((s[1] == '(') && ((s[2] == 'B') || (s[2] == 'J'))) state_ascii = true; else state_ascii = false; } } - if (state_ascii && *s == '"') + if (state_ascii && (*s == '"')) break; if (*s == '\\') { @@ -272,7 +126,7 @@ static void parse_parameters(struct ParameterList *param, const char *s) } else { - for (i = 0; *s && *s != ' ' && *s != ';' && i < sizeof(buffer) - 1; i++, s++) + for (i = 0; *s && (*s != ' ') && (*s != ';') && (i < (sizeof(buffer) - 1)); i++, s++) buffer[i] = *s; buffer[i] = 0; } @@ -296,7 +150,7 @@ static void parse_parameters(struct ParameterList *param, const char *s) } /* Find the next parameter */ - if (*s != ';' && (s = strchr(s, ';')) == NULL) + if ((*s != ';') && (s = strchr(s, ';')) == NULL) break; /* no more parameters */ do @@ -364,11 +218,11 @@ static void parse_references(struct ListHead *head, char *s) } /** - * mutt_parse_content_language - Read the content's language + * parse_content_language - Read the content's language * @param s Language string * @param ct Body of the email */ -static void mutt_parse_content_language(char *s, struct Body *ct) +static void parse_content_language(char *s, struct Body *ct) { if (!s || !ct) return; @@ -377,6 +231,151 @@ static void mutt_parse_content_language(char *s, struct Body *ct) ct->language = mutt_str_strdup(s); } +/** + * mutt_matches_ignore - Does the string match the ignore list + * @param s String to check + * @retval true If string matches + * + * Checks Ignore and UnIgnore using mutt_list_match + */ +bool mutt_matches_ignore(const char *s) +{ + return mutt_list_match(s, &Ignore) && !mutt_list_match(s, &UnIgnore); +} + +/** + * mutt_check_mime_type - Check a MIME type string + * @param s String to check + * @retval num MIME type, e.g. #TYPE_TEXT + */ +int mutt_check_mime_type(const char *s) +{ + if (mutt_str_strcasecmp("text", s) == 0) + return TYPE_TEXT; + else if (mutt_str_strcasecmp("multipart", s) == 0) + return TYPE_MULTIPART; +#ifdef SUN_ATTACHMENT + else if (mutt_str_strcasecmp("x-sun-attachment", s) == 0) + return TYPE_MULTIPART; +#endif + else if (mutt_str_strcasecmp("application", s) == 0) + return TYPE_APPLICATION; + else if (mutt_str_strcasecmp("message", s) == 0) + return TYPE_MESSAGE; + else if (mutt_str_strcasecmp("image", s) == 0) + return TYPE_IMAGE; + else if (mutt_str_strcasecmp("audio", s) == 0) + return TYPE_AUDIO; + else if (mutt_str_strcasecmp("video", s) == 0) + return TYPE_VIDEO; + else if (mutt_str_strcasecmp("model", s) == 0) + return TYPE_MODEL; + else if (mutt_str_strcasecmp("*", s) == 0) + return TYPE_ANY; + else if (mutt_str_strcasecmp(".*", s) == 0) + return TYPE_ANY; + else + return TYPE_OTHER; +} + +/** + * mutt_extract_message_id - Find a message-id + * @param s String to parse + * @param saveptr Save result here + * @retval ptr First character after message-id + * @retval NULL No more message ids + * + * Extract the first substring that looks like a message-id. + * Call back with NULL for more (like strtok). + */ +char *mutt_extract_message_id(const char *s, const char **saveptr) +{ + const char *o = NULL, *onull = NULL, *p = NULL; + char *ret = NULL; + + if (s) + p = s; + else if (saveptr) + p = *saveptr; + else + return NULL; + + for (s = NULL, o = NULL, onull = NULL; (p = strpbrk(p, "<> \t;")) != NULL; ++p) + { + if (*p == '<') + { + s = p; + o = onull = NULL; + continue; + } + + if (!s) + continue; + + if (*p == '>') + { + size_t olen = onull - o; + size_t slen = p - s + 1; + ret = mutt_mem_malloc(olen + slen + 1); + if (o) + memcpy(ret, o, olen); + memcpy(ret + olen, s, slen); + ret[olen + slen] = '\0'; + if (saveptr) + *saveptr = p + 1; /* next call starts after '>' */ + return ret; + } + + /* some idiotic clients break their message-ids between lines */ + if (s == p) + { + /* step past another whitespace */ + s = p + 1; + } + else if (o) + { + /* more than two lines, give up */ + s = o = onull = NULL; + } + else + { + /* remember the first line, start looking for the second */ + o = s; + onull = p; + s = p + 1; + } + } + + return NULL; +} + +/** + * mutt_check_encoding - Check the encoding type + * @param c String to check + * @retval num Encoding type, e.g. #ENC_QUOTED_PRINTABLE + */ +int mutt_check_encoding(const char *c) +{ + if (mutt_str_strncasecmp("7bit", c, sizeof("7bit") - 1) == 0) + return ENC_7BIT; + else if (mutt_str_strncasecmp("8bit", c, sizeof("8bit") - 1) == 0) + return ENC_8BIT; + else if (mutt_str_strncasecmp("binary", c, sizeof("binary") - 1) == 0) + return ENC_BINARY; + else if (mutt_str_strncasecmp("quoted-printable", c, sizeof("quoted-printable") - 1) == 0) + return ENC_QUOTED_PRINTABLE; + else if (mutt_str_strncasecmp("base64", c, sizeof("base64") - 1) == 0) + return ENC_BASE64; + else if (mutt_str_strncasecmp("x-uuencode", c, sizeof("x-uuencode") - 1) == 0) + return ENC_UUENCODED; +#ifdef SUN_ATTACHMENT + else if (mutt_str_strncasecmp("uuencode", c, sizeof("uuencode") - 1) == 0) + return ENC_UUENCODED; +#endif + else + return ENC_OTHER; +} + /** * mutt_parse_content_type - Parse a content type * @param s String to parse @@ -418,7 +417,7 @@ void mutt_parse_content_type(char *s, struct Body *ct) if (subtype) { *subtype++ = '\0'; - for (pc = subtype; *pc && !ISSPACE(*pc) && *pc != ';'; pc++) + for (pc = subtype; *pc && !ISSPACE(*pc) && (*pc != ';'); pc++) ; *pc = '\0'; ct->subtype = mutt_str_strdup(subtype); @@ -534,7 +533,7 @@ int mutt_rfc822_parse_line(struct Envelope *e, struct Header *hdr, char *line, else if (mutt_str_strcasecmp(line + 8, "language") == 0) { if (hdr) - mutt_parse_content_language(p, hdr->content); + parse_content_language(p, hdr->content); matched = 1; } else if (mutt_str_strcasecmp(line + 8, "transfer-encoding") == 0) @@ -634,7 +633,7 @@ int mutt_rfc822_parse_line(struct Envelope *e, struct Header *hdr, char *line, /* HACK - neomutt has, for a very short time, produced negative * Lines header values. Ignore them. */ - if (mutt_str_atoi(p, &hdr->lines) < 0 || hdr->lines < 0) + if (mutt_str_atoi(p, &hdr->lines) < 0 || (hdr->lines < 0)) hdr->lines = 0; } @@ -891,8 +890,8 @@ char *mutt_rfc822_read_line(FILE *f, char *line, size_t *linelen) while (true) { - if (fgets(buf, *linelen - offset, f) == NULL || /* end of file or */ - (ISSPACE(*line) && !offset)) /* end of headers */ + if (!fgets(buf, *linelen - offset, f) || /* end of file or */ + (ISSPACE(*line) && !offset)) /* end of headers */ { *line = 0; return line; @@ -921,7 +920,7 @@ char *mutt_rfc822_read_line(FILE *f, char *line, size_t *linelen) } /* eat tabs and spaces from the beginning of the continuation line */ - while ((ch = fgetc(f)) == ' ' || ch == '\t') + while (((ch = fgetc(f)) == ' ') || (ch == '\t')) ; ungetc(ch, f); *++buf = ' '; /* string is still terminated because we removed @@ -930,7 +929,7 @@ char *mutt_rfc822_read_line(FILE *f, char *line, size_t *linelen) buf++; offset = buf - line; - if (*linelen < offset + STRING) + if (*linelen < (offset + STRING)) { /* grow the buffer */ *linelen += STRING; @@ -1151,7 +1150,7 @@ struct Body *mutt_read_mime_header(FILE *fp, bool digest) if (mutt_str_strcasecmp("type", line + 8) == 0) mutt_parse_content_type(c, p); else if (mutt_str_strcasecmp("language", line + 8) == 0) - mutt_parse_content_language(c, p); + parse_content_language(c, p); else if (mutt_str_strcasecmp("transfer-encoding", line + 8) == 0) p->encoding = mutt_check_encoding(c); else if (mutt_str_strcasecmp("disposition", line + 8) == 0) @@ -1278,13 +1277,13 @@ struct Body *mutt_parse_multipart(FILE *fp, const char *boundary, LOFF_T end_off } const size_t blen = mutt_str_strlen(boundary); - while (ftello(fp) < end_off && fgets(buffer, LONG_STRING, fp) != NULL) + while ((ftello(fp) < end_off) && fgets(buffer, LONG_STRING, fp)) { const size_t len = mutt_str_strlen(buffer); - const size_t crlf = (len > 1 && buffer[len - 2] == '\r') ? 1 : 0; + const size_t crlf = ((len > 1) && (buffer[len - 2] == '\r')) ? 1 : 0; - if (buffer[0] == '-' && buffer[1] == '-' && + if ((buffer[0] == '-') && (buffer[1] == '-') && (mutt_str_strncmp(buffer + 2, boundary, blen) == 0)) { if (last) @@ -1300,7 +1299,7 @@ struct Body *mutt_parse_multipart(FILE *fp, const char *boundary, LOFF_T end_off if (len > 0) { /* Remove any trailing whitespace, up to the length of the boundary */ - for (size_t i = len - 1; ISSPACE(buffer[i]) && i >= blen + 2; i--) + for (size_t i = len - 1; ISSPACE(buffer[i]) && (i >= (blen + 2)); i--) buffer[i] = 0; } @@ -1321,7 +1320,7 @@ struct Body *mutt_parse_multipart(FILE *fp, const char *boundary, LOFF_T end_off if (mutt_str_atoi(mutt_param_get(&new->parameter, "content-lines"), &lines) < 0) lines = 0; for (; lines; lines--) - if (ftello(fp) >= end_off || fgets(buffer, LONG_STRING, fp) == NULL) + if ((ftello(fp) >= end_off) || !fgets(buffer, LONG_STRING, fp)) break; } #endif @@ -1346,7 +1345,7 @@ struct Body *mutt_parse_multipart(FILE *fp, const char *boundary, LOFF_T end_off } /* in case of missing end boundary, set the length to something reasonable */ - if (last && last->length == 0 && !final) + if (last && (last->length == 0) && !final) last->length = end_off - last->offset; /* parse recursive MIME parts */ diff --git a/email/parse.h b/email/parse.h index 955c0eab4..df36d1092 100644 --- a/email/parse.h +++ b/email/parse.h @@ -29,18 +29,18 @@ struct Body; struct Envelope; struct Header; -struct Envelope *mutt_rfc822_read_header(FILE *f, struct Header *hdr, short user_hdrs, short weed); -char * mutt_rfc822_read_line(FILE *f, char *line, size_t *linelen); -int mutt_rfc822_parse_line(struct Envelope *e, struct Header *hdr, char *line, char *p, short user_hdrs, short weed, short do_2047); int mutt_check_encoding(const char *c); int mutt_check_mime_type(const char *s); char * mutt_extract_message_id(const char *s, const char **saveptr); +bool mutt_is_message_type(int type, const char *subtype); +bool mutt_matches_ignore(const char *s); void mutt_parse_content_type(char *s, struct Body *ct); -struct Body * mutt_read_mime_header(FILE *fp, bool digest); - -bool mutt_is_message_type(int type, const char *subtype); -void mutt_parse_part(FILE *fp, struct Body *b); struct Body * mutt_parse_multipart(FILE *fp, const char *boundary, LOFF_T end_off, bool digest); +void mutt_parse_part(FILE *fp, struct Body *b); +struct Body * mutt_read_mime_header(FILE *fp, bool digest); +int mutt_rfc822_parse_line(struct Envelope *e, struct Header *hdr, char *line, char *p, short user_hdrs, short weed, short do_2047); struct Body * mutt_rfc822_parse_message(FILE *fp, struct Body *parent); +struct Envelope *mutt_rfc822_read_header(FILE *f, struct Header *hdr, short user_hdrs, short weed); +char * mutt_rfc822_read_line(FILE *f, char *line, size_t *linelen); #endif /* _EMAIL_PARSE_H */