From: Michael Elkins Date: Tue, 18 Dec 2012 21:50:20 +0000 (-0800) Subject: rename iswsp() to is_email_wsp() X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=90eb6a39d4d977d85b3d9ab04f0792e22fa655a1;p=mutt rename iswsp() to is_email_wsp() add new inline funtion skip_email_wsp() to be used in lieu of SKIPWS() when parsing ASCII protocols rather than user input. change use of SKIPWS() to skip_email_wsp() in places where it is likely to be a problem. --- diff --git a/headers.c b/headers.c index 72ee3eb2..902eb9b8 100644 --- a/headers.c +++ b/headers.c @@ -36,7 +36,7 @@ void mutt_edit_headers (const char *editor, { char path[_POSIX_PATH_MAX]; /* tempfile used to edit headers + body */ char buffer[LONG_STRING]; - char *p; + const char *p; FILE *ifp, *ofp; int i, keep; ENVELOPE *n; @@ -141,8 +141,7 @@ void mutt_edit_headers (const char *editor, if (fcc && ascii_strncasecmp ("fcc:", cur->data, 4) == 0) { - p = cur->data + 4; - SKIPWS (p); + p = skip_email_wsp(cur->data + 4); if (*p) { strfcpy (fcc, p, fcclen); @@ -154,10 +153,9 @@ void mutt_edit_headers (const char *editor, { BODY *body; BODY *parts; - int l = 0; + size_t l = 0; - p = cur->data + 7; - SKIPWS (p); + p = skip_email_wsp(cur->data + 7); if (*p) { for ( ; *p && *p != ' ' && *p != '\t'; p++) @@ -171,9 +169,7 @@ void mutt_edit_headers (const char *editor, if (l < sizeof (path) - 1) path[l++] = *p; } - if (*p) - *p++ = 0; - SKIPWS (p); + p = skip_email_wsp(p); path[l] = 0; mutt_expand_path (path, sizeof (path)); diff --git a/lib.h b/lib.h index 49fc8682..3c385873 100644 --- a/lib.h +++ b/lib.h @@ -98,6 +98,24 @@ on some systems */ # define SKIPWS(c) while (*(c) && isspace ((unsigned char) *(c))) c++; +#define EMAIL_WSP " \t\r\n" + +/* skip over WSP as defined by RFC5322. This is used primarily for parsing + * header fields. */ + +static inline char *skip_email_wsp(const char *s) +{ + if (s) + return (char *)(s + strspn(s, EMAIL_WSP)); + return (char *)s; +} + +static inline int is_email_wsp(char c) +{ + return strchr(EMAIL_WSP, c) != NULL; +} + + /* * These functions aren't defined in lib.c, but * they are used there. diff --git a/mutt_crypt.h b/mutt_crypt.h index db152b02..c9fcd893 100644 --- a/mutt_crypt.h +++ b/mutt_crypt.h @@ -119,7 +119,7 @@ int mutt_is_application_smime (BODY *); int mutt_signed_handler (BODY *, STATE *); -int mutt_parse_crypt_hdr (char *, int, int); +int mutt_parse_crypt_hdr (const char *, int, int); void convert_to_7bit (BODY *); diff --git a/parse.c b/parse.c index ebfd057c..acf0621f 100644 --- a/parse.c +++ b/parse.c @@ -152,19 +152,25 @@ static PARAMETER *parse_parameters (const char *s) if (*p != ';') { i = p - s; - - new = mutt_new_parameter (); - - new->attribute = safe_malloc (i + 1); - memcpy (new->attribute, s, i); - new->attribute[i] = 0; - /* remove whitespace from the end of the attribute name */ - while (ISSPACE (new->attribute[--i])) - new->attribute[i] = 0; + while (i > 0 && is_email_wsp(s[i-1])) + --i; + + /* the check for the missing parameter token is here so that we can skip + * over any quoted value that may be present. + */ + if (i == 0) + { + dprint(1, (debugfile, "parse_parameters: missing attribute: %s\n", s)); + new = NULL; + } + else + { + new = mutt_new_parameter (); + new->attribute = mutt_substrdup(s, s + i); + } - s = p + 1; /* skip over the = */ - SKIPWS (s); + s = skip_email_wsp(p + 1); /* skip over the = */ if (*s == '"') { @@ -206,20 +212,24 @@ static PARAMETER *parse_parameters (const char *s) buffer[i] = 0; } - new->value = safe_strdup (buffer); - - dprint (2, (debugfile, "parse_parameter: `%s' = `%s'\n", - new->attribute ? new->attribute : "", - new->value ? new->value : "")); - - /* Add this parameter to the list */ - if (head) + /* if the attribute token was missing, 'new' will be NULL */ + if (new) { - cur->next = new; - cur = cur->next; + new->value = safe_strdup (buffer); + + dprint (2, (debugfile, "parse_parameter: `%s' = `%s'\n", + new->attribute ? new->attribute : "", + new->value ? new->value : "")); + + /* Add this parameter to the list */ + if (head) + { + cur->next = new; + cur = cur->next; + } + else + head = cur = new; } - else - head = cur = new; } else { @@ -229,19 +239,17 @@ static PARAMETER *parse_parameters (const char *s) /* Find the next parameter */ if (*s != ';' && (s = strchr (s, ';')) == NULL) - break; /* no more parameters */ + break; /* no more parameters */ do { - s++; - - /* Move past any leading whitespace */ - SKIPWS (s); + /* Move past any leading whitespace. the +1 skips over the semicolon */ + s = skip_email_wsp(s + 1); } while (*s == ';'); /* skip empty parameters */ } - bail: +bail: rfc2231_decode_parameters (&head); return (head); @@ -364,7 +372,7 @@ void mutt_parse_content_type (char *s, BODY *ct) } -static void parse_content_disposition (char *s, BODY *ct) +static void parse_content_disposition (const char *s, BODY *ct) { PARAMETER *parms; @@ -378,8 +386,7 @@ static void parse_content_disposition (char *s, BODY *ct) /* Check to see if a default filename was given */ if ((s = strchr (s, ';')) != NULL) { - s++; - SKIPWS (s); + s = skip_email_wsp(s + 1); if ((s = mutt_get_parameter ("filename", (parms = parse_parameters (s))))) mutt_str_replace (&ct->filename, s); if ((s = mutt_get_parameter ("name", parms))) @@ -414,8 +421,7 @@ BODY *mutt_read_mime_header (FILE *fp, int digest) if ((c = strchr (line, ':'))) { *c = 0; - c++; - SKIPWS (c); + c = skip_email_wsp(c + 1); if (!*c) { dprint (1, (debugfile, "mutt_read_mime_header(): skipping empty header field: %s\n", line)); @@ -660,8 +666,7 @@ static const char *uncomment_timezone (char *buf, size_t buflen, const char *tz) if (*tz != '(') return tz; /* no need to do anything */ - tz++; - SKIPWS (tz); + tz = skip_email_wsp(tz + 1); if ((p = strpbrk (tz, " )")) == NULL) return tz; len = p - tz; @@ -764,7 +769,7 @@ time_t mutt_parse_date (const char *s, HEADER *h) t++; else t = scratch; - SKIPWS (t); + t = skip_email_wsp(t); memset (&tm, 0, sizeof (tm)); @@ -1407,8 +1412,7 @@ ENVELOPE *mutt_read_rfc822_header (FILE *f, HEADER *hdr, short user_hdrs, } *p = 0; - p++; - SKIPWS (p); + p = skip_email_wsp(p + 1); if (!*p) continue; /* skip empty header fields */ diff --git a/pop_lib.c b/pop_lib.c index d8cc9eaa..62fea35a 100644 --- a/pop_lib.c +++ b/pop_lib.c @@ -82,8 +82,7 @@ void pop_error (POP_DATA *pop_data, char *msg) if (!mutt_strncmp (msg, "-ERR ", 5)) { - c2 = msg + 5; - SKIPWS (c2); + c2 = skip_email_wsp(msg + 5); if (*c2) c = c2; @@ -102,8 +101,7 @@ static int fetch_capa (char *line, void *data) if (!ascii_strncasecmp (line, "SASL", 4)) { FREE (&pop_data->auth_list); - c = line + 4; - SKIPWS (c); + c = skip_email_wsp(line + 4); pop_data->auth_list = safe_strdup (c); } diff --git a/postpone.c b/postpone.c index 4c518200..7ff98145 100644 --- a/postpone.c +++ b/postpone.c @@ -233,7 +233,7 @@ int mutt_get_postponed (CONTEXT *ctx, HEADER *hdr, HEADER **cur, char *fcc, size LIST *tmp; LIST *last = NULL; LIST *next; - char *p; + const char *p; int opt_delete; if (!Postponed) @@ -296,8 +296,7 @@ int mutt_get_postponed (CONTEXT *ctx, HEADER *hdr, HEADER **cur, char *fcc, size { /* if a mailbox is currently open, look to see if the orignal message the user attempted to reply to is in this mailbox */ - p = tmp->data + 18; - SKIPWS (p); + p = skip_email_wsp(tmp->data + 18); if (!ctx->id_hash) ctx->id_hash = mutt_make_id_hash (ctx); *cur = hash_find (ctx->id_hash, p); @@ -317,8 +316,7 @@ int mutt_get_postponed (CONTEXT *ctx, HEADER *hdr, HEADER **cur, char *fcc, size } else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0) { - p = tmp->data + 11; - SKIPWS (p); + p = skip_email_wsp(tmp->data + 11); strfcpy (fcc, p, fcclen); mutt_pretty_mailbox (fcc, fcclen); @@ -405,7 +403,7 @@ int mutt_get_postponed (CONTEXT *ctx, HEADER *hdr, HEADER **cur, char *fcc, size -int mutt_parse_crypt_hdr (char *p, int set_signas, int crypt_app) +int mutt_parse_crypt_hdr (const char *p, int set_signas, int crypt_app) { char smime_cryptalg[LONG_STRING] = "\0"; char sign_as[LONG_STRING] = "\0", *q; @@ -414,7 +412,7 @@ int mutt_parse_crypt_hdr (char *p, int set_signas, int crypt_app) if (!WithCrypto) return 0; - SKIPWS (p); + p = skip_email_wsp(p); for (; *p; p++) { diff --git a/rfc1524.c b/rfc1524.c index 64b6de01..1baca748 100644 --- a/rfc1524.c +++ b/rfc1524.c @@ -136,8 +136,8 @@ static char *get_field (char *s) } else { - *ch++ = 0; - SKIPWS (ch); + *ch = 0; + ch = skip_email_wsp(ch + 1); break; } } diff --git a/rfc822.c b/rfc822.c index 43f3aca7..f437d72d 100644 --- a/rfc822.c +++ b/rfc822.c @@ -29,7 +29,6 @@ #else #define safe_strdup strdup #define safe_malloc malloc -#define SKIPWS(x) while(iswsp(*x))x++ #define FREE(x) safe_free(x) #define strfcpy(a,b,c) {if (c) {strncpy(a,b,c);a[c-1]=0;}} #define LONG_STRING 1024 @@ -43,12 +42,6 @@ #define terminate_buffer(a, b) terminate_string(a, b, sizeof (a) - 1) -/* undefine the version defined in lib.h */ -#undef SKIPWS - -/* uses the iswsp() function defined in this module so we get RFC5322 semantics */ -#define SKIPWS(s) while(iswsp(*s)) s++ - const char RFC822Specials[] = "@.,:;<>[]\\\"()"; #define is_special(x) strchr(RFC822Specials,x) @@ -64,17 +57,6 @@ const char * const RFC822Errors[] = { "bad address spec" }; -/* iswsp - * - * Returns non-zero if 'c' is a whitespace character as defined by RFC5322. - * - * WSP is defined as ASCII space (32) or hard tab (9). - */ -static inline int iswsp(char c) -{ - return (c == ' ' || c == '\t'); -} - void rfc822_dequote_comment (char *s) { char *w = s; @@ -228,7 +210,7 @@ next_token (const char *s, char *token, size_t *tokenlen, size_t tokenmax) } while (*s) { - if (iswsp(*s) || is_special (*s)) + if (is_email_wsp(*s) || is_special (*s)) break; if (*tokenlen < tokenmax) token[(*tokenlen)++] = *s; @@ -246,7 +228,7 @@ parse_mailboxdomain (const char *s, const char *nonspecial, while (*s) { - SKIPWS (s); + s = skip_email_wsp(s); if (strchr (nonspecial, *s) == NULL && is_special (*s)) return s; @@ -309,7 +291,7 @@ parse_route_addr (const char *s, char token[LONG_STRING]; size_t tokenlen = 0; - SKIPWS (s); + s = skip_email_wsp(s); /* find the end of the route */ if (*s == '@') @@ -388,7 +370,10 @@ add_addrspec (ADDRESS **top, ADDRESS **last, const char *phrase, ADDRESS *rfc822_parse_adrlist (ADDRESS *top, const char *s) { int ws_pending, nl; +#ifdef EXACT_ADDRESS const char *begin, *ps; +#endif + const char *ps; char comment[LONG_STRING], phrase[LONG_STRING]; size_t phraselen = 0, commentlen = 0; ADDRESS *cur, *last = NULL; @@ -399,12 +384,14 @@ ADDRESS *rfc822_parse_adrlist (ADDRESS *top, const char *s) while (last && last->next) last = last->next; - ws_pending = iswsp (*s); + ws_pending = is_email_wsp (*s); if ((nl = mutt_strlen (s))) nl = s[nl - 1] == '\n'; - SKIPWS (s); + s = skip_email_wsp(s); +#ifdef EXACT_ADDRESS begin = s; +#endif while (*s) { if (*s == ',') @@ -427,8 +414,9 @@ ADDRESS *rfc822_parse_adrlist (ADDRESS *top, const char *s) commentlen = 0; phraselen = 0; s++; - begin = s; - SKIPWS (begin); +#ifdef EXACT_ADDRESS + begin = skip_email_wsp(s); +#endif } else if (*s == '(') { @@ -472,8 +460,9 @@ ADDRESS *rfc822_parse_adrlist (ADDRESS *top, const char *s) phraselen = 0; commentlen = 0; s++; - begin = s; - SKIPWS (begin); +#ifdef EXACT_ADDRESS + begin = skip_email_wsp(s); +#endif } else if (*s == ';') { @@ -503,8 +492,9 @@ ADDRESS *rfc822_parse_adrlist (ADDRESS *top, const char *s) phraselen = 0; commentlen = 0; s++; - begin = s; - SKIPWS (begin); +#ifdef EXACT_ADDRESS + begin = skip_email_wsp(s); +#endif } else if (*s == '<') { @@ -540,8 +530,8 @@ ADDRESS *rfc822_parse_adrlist (ADDRESS *top, const char *s) } s = ps; } - ws_pending = iswsp (*s); - SKIPWS (s); + ws_pending = is_email_wsp(*s); + s = skip_email_wsp(s); } if (phraselen) diff --git a/send.c b/send.c index 6326610a..09f51b0b 100644 --- a/send.c +++ b/send.c @@ -234,15 +234,14 @@ static int edit_envelope (ENVELOPE *en) } else { - char *p; + const char *p; buf[0] = 0; for (; uh; uh = uh->next) { if (ascii_strncasecmp ("subject:", uh->data, 8) == 0) { - p = uh->data + 8; - SKIPWS (p); + p = skip_email_wsp(uh->data + 8); strncpy (buf, p, sizeof (buf)); } } diff --git a/sendlib.c b/sendlib.c index 44382602..0328dd2a 100644 --- a/sendlib.c +++ b/sendlib.c @@ -1813,12 +1813,8 @@ static int write_one_header (FILE *fp, int pfxw, int max, int wraplen, else { tagbuf = mutt_substrdup (start, t); - ++t; /* skip over the colon separating the header field name and value */ - - /* skip over any leading whitespace (WSP, as defined in RFC5322) */ - while (*t == ' ' || *t == '\t') - t++; - + /* skip over the colon separating the header field name and value */ + t = skip_email_wsp(t + 1); valbuf = mutt_substrdup (t, end); } dprint(4,(debugfile,"mwoh: buf[%s%s] too long, " @@ -2040,7 +2036,7 @@ int mutt_write_rfc822_header (FILE *fp, ENVELOPE *env, BODY *attach, *p = '\0'; - p++; SKIPWS (p); + p = skip_email_wsp(p + 1); if (!*p) { *q = ':'; @@ -2084,7 +2080,7 @@ static void encode_headers (LIST *h) continue; i = p - h->data; - ++p; SKIPWS (p); + p = skip_email_wsp(p + 1); tmp = safe_strdup (p); if (!tmp) diff --git a/url.c b/url.c index 1b583b50..988cf59e 100644 --- a/url.c +++ b/url.c @@ -295,8 +295,7 @@ int url_parse_mailto (ENVELOPE *e, char **body, const char *src) safe_asprintf (&scratch, "%s: %s", tag, value); scratch[taglen] = 0; /* overwrite the colon as mutt_parse_rfc822_line expects */ - value = &scratch[taglen + 1]; - SKIPWS (value); + value = skip_email_wsp(&scratch[taglen + 1]); mutt_parse_rfc822_line (e, NULL, scratch, value, 1, 0, 0, &last); FREE (&scratch); }