From: Pierre Habouzit Date: Fri, 3 Nov 2006 21:30:43 +0000 (+0100) Subject: rfc822 final touch X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=commitdiff_plain;h=7a368b3670a90656b2e0e724ed3efd79221f3d31 rfc822 final touch Signed-off-by: Pierre Habouzit --- diff --git a/commands.c b/commands.c index 5ceb9ce..a14b471 100644 --- a/commands.c +++ b/commands.c @@ -859,7 +859,7 @@ void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp) for (p = b->parameter; p; p = p->next) { l = m_strlen(buf); - rfc822_cat (tmp, sizeof (tmp), p->value, MimeSpecials); + rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials); snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp); } } diff --git a/copy.c b/copy.c index 66d30b8..05a7371 100644 --- a/copy.c +++ b/copy.c @@ -356,7 +356,7 @@ mutt_copy_header (FILE * in, HEADER * h, FILE * out, int flags, fputs ("Content-Type: text/plain; charset=", out); mutt_canonical_charset (chsbuf, sizeof (chsbuf), Charset ? Charset : "us-ascii"); - rfc822_cat (buffer, sizeof (buffer), chsbuf, MimeSpecials); + rfc822_strcpy(buffer, sizeof(buffer), chsbuf, MimeSpecials); fputs (buffer, out); fputc ('\n', out); diff --git a/lib-mime/mime.h b/lib-mime/mime.h index 90f8c68..6f20910 100644 --- a/lib-mime/mime.h +++ b/lib-mime/mime.h @@ -111,9 +111,9 @@ void rfc822_qualify(address_t *, const char *); address_t *rfc822_parse_adrlist(address_t *, const char *s); -void rfc822_write_address(char *, size_t, address_t *, int); -void rfc822_write_address_single(char *, size_t, address_t *, int); -void rfc822_cat(char *, size_t, const char *, const char *); +ssize_t rfc822_write_address(char *, ssize_t, address_t *, int); +ssize_t rfc822_write_address_single(char *, ssize_t, address_t *, int); +ssize_t rfc822_strcpy(char *, ssize_t, const char *, const char *); extern int RFC822Error; extern const char RFC822Specials[]; diff --git a/lib-mime/rfc822.c b/lib-mime/rfc822.c index d672891..22e54a4 100644 --- a/lib-mime/rfc822.c +++ b/lib-mime/rfc822.c @@ -359,185 +359,119 @@ address_t *rfc822_parse_adrlist(address_t *top, const char *s) /* Output functions */ /****************************************************************************/ -void -rfc822_cat(char *buf, size_t buflen, const char *value, const char *specials) +ssize_t +rfc822_strcpy(char *buf, ssize_t buflen, const char *p, const char *specials) { - if (strpbrk(value, specials)) { - char tmp[256], *pc = tmp; - size_t tmplen = sizeof (tmp) - 3; - - *pc++ = '"'; - for (; *value && tmplen > 1; value++) { - if (*value == '\\' || *value == '"') { - *pc++ = '\\'; - tmplen--; + if (strpbrk(p, specials)) { + ssize_t pos = 0; + + buf[pos++] = '"'; + + while (*p && pos < buflen - 2) { + if (*p == '\\' || *p == '"') { + if (pos >= buflen - 4) + break; + buf[pos++] = '\\'; } - *pc++ = *value; - tmplen--; + + buf[pos++] = *p++; } - *pc++ = '"'; - *pc = 0; - m_strcpy(buf, buflen, tmp); + + buf[pos++] = '"'; + buf[pos] = '\0'; + return pos; } else { - m_strcpy(buf, buflen, value); + return m_strcpy(buf, buflen, p); } } -void rfc822_write_address_single(char *buf, size_t buflen, address_t * addr, - int display) +ssize_t rfc822_write_address_single(char *buf, ssize_t buflen, + address_t *addr, int display) { - size_t len; - char *pbuf = buf; - char *pc; + ssize_t pos = 0; if (!addr) - return; + return 0; buflen--; /* save room for the terminal nul */ if (addr->personal) { - if (strpbrk (addr->personal, RFC822Specials)) { - if (!buflen) - goto done; - *pbuf++ = '"'; - buflen--; - for (pc = addr->personal; *pc && buflen > 0; pc++) { - if (*pc == '"' || *pc == '\\') { - if (!buflen) - goto done; - *pbuf++ = '\\'; - buflen--; - } - if (!buflen) - goto done; - *pbuf++ = *pc; - buflen--; - } - if (!buflen) - goto done; - *pbuf++ = '"'; - buflen--; - } - else { - if (!buflen) - goto done; - m_strcpy(pbuf, buflen, addr->personal); - len = m_strlen(pbuf); - pbuf += len; - buflen -= len; - } - - if (!buflen) + pos = rfc822_strcpy(buf, buflen, addr->personal, RFC822Specials); + if (pos + 2 >= buflen) goto done; - *pbuf++ = ' '; - buflen--; - } - if (addr->personal || (addr->mailbox && *addr->mailbox == '@')) { - if (!buflen) - goto done; - *pbuf++ = '<'; - buflen--; + buf[pos++] = ' '; + buf[pos++] = '<'; } if (addr->mailbox) { - if (!buflen) - goto done; - if (ascii_strcmp (addr->mailbox, "@") && !display) { - m_strcpy(pbuf, buflen, addr->mailbox); - len = m_strlen(pbuf); - } - else if (ascii_strcmp (addr->mailbox, "@") && display) { - m_strcpy(pbuf, buflen, mutt_addr_for_display(addr)); - len = m_strlen(pbuf); - } - else { - *pbuf = '\0'; - len = 0; + if (!display) { + pos += m_strcpy(buf + pos, buflen - pos, addr->mailbox); + } else { + pos += m_strcpy(buf + pos, buflen - pos, mutt_addr_for_display(addr)); } - pbuf += len; - buflen -= len; - if (addr->personal || (addr->mailbox && *addr->mailbox == '@')) { - if (!buflen) + if (addr->personal) { + if (pos + 1 >= buflen) goto done; - *pbuf++ = '>'; - buflen--; + buf[pos++] = '>'; } if (addr->group) { - if (!buflen) + if (pos + 1 >= buflen) goto done; - *pbuf++ = ':'; - buflen--; - if (!buflen) - goto done; - *pbuf++ = ' '; - buflen--; + buf[pos++] = ':'; } - } - else { - if (!buflen) + } else { + if (pos + 1 >= buflen) goto done; - *pbuf++ = ';'; - buflen--; + buf[pos++] = ';'; } -done: + + done: /* no need to check for length here since we already save space at the beginning of this routine */ - *pbuf = 0; + buf[pos] = 0; + return pos; } /* note: it is assumed that `buf' is nul terminated! */ -void rfc822_write_address (char *buf, size_t buflen, address_t * addr, - int display) +ssize_t +rfc822_write_address(char *buf, ssize_t buflen, address_t *addr, int display) { - char *pbuf = buf; - size_t len = m_strlen(buf); + ssize_t pos; buflen--; /* save room for the terminal nul */ + pos = m_strnlen(buf, buflen); - if (len > 0) { - if (len > buflen) - return; /* safety check for bogus arguments */ - - pbuf += len; - buflen -= len; - if (!buflen) - goto done; - *pbuf++ = ','; - buflen--; - if (!buflen) + if (pos) { + if (pos + 2 >= buflen) goto done; - *pbuf++ = ' '; - buflen--; - } - for (; addr && buflen > 0; addr = addr->next) { - /* use buflen+1 here because we already saved space for the trailing - nul char, and the subroutine can make use of it */ - rfc822_write_address_single (pbuf, buflen + 1, addr, display); - - /* this should be safe since we always have at least 1 char passed into - the above call, which means `pbuf' should always be nul terminated */ - len = m_strlen(pbuf); - pbuf += len; - buflen -= len; + buf[pos++] = ','; + buf[pos++] = ' '; + } + while (addr) { + pos += rfc822_write_address_single(buf + pos, buflen + 1 - pos, + addr, display); /* if there is another address, and its not a group mailbox name or group terminator, add a comma to separate the addresses */ - if (addr->next && addr->next->mailbox && !addr->group) { - if (!buflen) + if (!addr->group && addr->next && addr->next->mailbox) { + if (pos + 2 >= buflen) goto done; - *pbuf++ = ','; - buflen--; if (!buflen) goto done; - *pbuf++ = ' '; - buflen--; + + buf[pos++] = ','; + buf[pos++] = ' '; } + + addr = addr->next; } -done: - *pbuf = 0; + + done: + buf[pos] = '\0'; + return pos; } diff --git a/sendlib.c b/sendlib.c index 6a25038..b2d283d 100644 --- a/sendlib.c +++ b/sendlib.c @@ -297,7 +297,7 @@ int mutt_write_mime_header (BODY * a, FILE * f) buffer[0] = 0; tmp = m_strdup(p->value); encode = rfc2231_encode_string (&tmp); - rfc822_cat (buffer, sizeof (buffer), tmp, MimeSpecials); + rfc822_strcpy(buffer, sizeof(buffer), tmp, MimeSpecials); /* Dirty hack to make messages readable by Outlook Express * for the Mac: force quotes around the boundary parameter @@ -349,7 +349,7 @@ int mutt_write_mime_header (BODY * a, FILE * f) buffer[0] = 0; tmp = m_strdup(t); encode = rfc2231_encode_string (&tmp); - rfc822_cat (buffer, sizeof (buffer), tmp, MimeSpecials); + rfc822_strcpy(buffer, sizeof(buffer), tmp, MimeSpecials); p_delete(&tmp); fprintf (f, "; filename%s=%s", encode ? "*" : "", buffer); } @@ -2153,8 +2153,8 @@ void mutt_prepare_envelope (ENVELOPE * env, int final) env->to->next = address_new (); buffer[0] = 0; - rfc822_cat (buffer, sizeof (buffer), "undisclosed-recipients", - RFC822Specials); + rfc822_strcpy(buffer, sizeof(buffer), "undisclosed-recipients", + RFC822Specials); env->to->mailbox = m_strdup(buffer); }