X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=rfc822.c;h=4efba8349eb6a79c494cae0d95aa54e9b72fb7dc;hb=33cddbff323efcbae1503e91e6e65b2733da80c7;hp=5e3356160618a2e3e9a707e8c3d1bb227e040f21;hpb=a92c8c212e2f1e014166cfb84751ea052035599e;p=~andy%2Ffetchmail diff --git a/rfc822.c b/rfc822.c index 5e335616..4efba834 100644 --- a/rfc822.c +++ b/rfc822.c @@ -19,19 +19,27 @@ is part of fetchmail and the Unix Cookbook, and are released under the MIT license. Compile with -DMAIN to build the demonstrator. ******************************************************************************/ + +#define _XOPEN_SOURCE 600 +#define __BSD_VISIBLE 1 + +#include "config.h" +#include "fetchmail.h" + #include #include #include +#include #include -#include "fetchmail.h" +#include "sdump.h" #ifndef MAIN -#include "i18n.h" +#include "gettext.h" #else #include static int verbose; -char *program_name = "rfc822"; +const char *program_name = "rfc822"; #endif /* MAIN */ #ifndef TRUE @@ -41,13 +49,15 @@ char *program_name = "rfc822"; #define HEADER_END(p) ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t')) -unsigned char *reply_hack( - unsigned char *buf /* header to be hacked */, - const unsigned char *host /* server hostname */, +#define BEFORE_EOL(s) (strcspn((s), "\r\n")) + +char *reply_hack( + char *buf /* header to be hacked */, + const char *host /* server hostname */, size_t *length) /* hack message headers so replies will work properly */ { - unsigned char *from, *cp, last_nws = '\0', *parens_from = NULL; + char *from, *cp, last_nws = '\0', *parens_from = NULL; int parendepth, state, has_bare_name_part, has_host_part; #ifndef MAIN int addresscount = 1; @@ -72,14 +82,16 @@ unsigned char *reply_hack( } #ifndef MAIN - if (outlevel >= O_DEBUG) - report_build(stdout, GT_("About to rewrite %s"), buf); + if (outlevel >= O_DEBUG) { + report_build(stdout, GT_("About to rewrite %s...\n"), (cp = sdump(buf, BEFORE_EOL(buf)))); + xfree(cp); + } /* make room to hack the address; buf must be malloced */ for (cp = buf; *cp; cp++) - if (*cp == ',' || isspace(*cp)) + if (*cp == ',' || isspace((unsigned char)*cp)) addresscount++; - buf = (unsigned char *)xrealloc(buf, strlen(buf) + addresscount * (strlen(host) + 1) + 1); + buf = (char *)xrealloc(buf, strlen(buf) + addresscount * (strlen(host) + 1) + 1); #endif /* MAIN */ /* @@ -95,8 +107,8 @@ unsigned char *reply_hack( #ifdef MAIN if (verbose) { - printf("state %d: %s", state, (char *)buf); - printf("%*s^\n", from - buf + 10, " "); + printf("state %d: %s", state, buf); + printf("%*s^\n", (int)(from - buf + 10), " "); } #endif /* MAIN */ if (state != 2) @@ -116,7 +128,7 @@ unsigned char *reply_hack( break; case 1: /* we've seen the colon, we're looking for addresses */ - if (!isspace(*from)) + if (!isspace((unsigned char)*from)) last_nws = *from; if (*from == '<') state = 3; @@ -135,12 +147,12 @@ unsigned char *reply_hack( && last_nws != ';') { int hostlen; - unsigned char *p; + char *p; p = from; if (parens_from) from = parens_from; - while (isspace(*from) || (*from == ',')) + while (isspace((unsigned char)*from) || (*from == ',')) --from; from++; hostlen = strlen(host); @@ -158,7 +170,7 @@ unsigned char *reply_hack( { parens_from = from; } - else if (!isspace(*from)) + else if (!isspace((unsigned char)*from)) has_bare_name_part = TRUE; break; @@ -208,22 +220,26 @@ unsigned char *reply_hack( } #ifndef MAIN - if (outlevel >= O_DEBUG) - report_complete(stdout, GT_("Rewritten version is %s\n"), buf); + if (outlevel >= O_DEBUG) { + report_complete(stdout, GT_("...rewritten version is %s.\n"), + (cp = sdump(buf, BEFORE_EOL(buf)))); + xfree(cp) + } + #endif /* MAIN */ *length = strlen(buf); return(buf); } -unsigned char *nxtaddr(const unsigned char *hdr /* header to be parsed, NUL to continue previous hdr */) +char *nxtaddr(const char *hdr /* header to be parsed, NUL to continue previous hdr */) /* parse addresses in succession out of a specified RFC822 header */ { - static unsigned char address[BUFSIZ]; - static int tp; - static const unsigned char *hp; + static char address[BUFSIZ]; + static size_t tp; + static const char *hp; static int state, oldstate; #ifdef MAIN - static const unsigned char *orighdr; + static const char *orighdr; #endif /* MAIN */ int parendepth = 0; @@ -247,13 +263,15 @@ unsigned char *nxtaddr(const unsigned char *hdr /* header to be parsed, NUL to c tp = 0; } + if (!hp) return NULL; + for (; *hp; hp++) { #ifdef MAIN if (verbose) { - printf("state %d: %s", state, (char *)orighdr); - printf("%*s^\n", hp - orighdr + 10, " "); + printf("state %d: %s", state, orighdr); + printf("%*s^\n", (int)(hp - orighdr + 10), " "); } #endif /* MAIN */ @@ -264,13 +282,13 @@ unsigned char *nxtaddr(const unsigned char *hdr /* header to be parsed, NUL to c state = ENDIT_ALL; if (tp) { - while (isspace(address[--tp])) - continue; - address[++tp] = '\0'; + while (tp > 0 && isspace((unsigned char)address[tp - 1])) + tp--; + address[tp] = '\0'; tp = 0; return (address); } - return((unsigned char *)NULL); + return(NULL); } else if (*hp == '\\') /* handle RFC822 escaping */ { @@ -305,7 +323,7 @@ unsigned char *nxtaddr(const unsigned char *hdr /* header to be parsed, NUL to c state = INSIDE_BRACKETS; tp = 0; } - else if (*hp != ',' && !isspace(*hp)) + else if (*hp != ',' && !isspace((unsigned char)*hp)) { --hp; state = BARE_ADDRESS; @@ -340,18 +358,14 @@ unsigned char *nxtaddr(const unsigned char *hdr /* header to be parsed, NUL to c state = INSIDE_DQUOTE; address[NEXTTP()] = *hp; } - else if (!isspace(*hp)) /* just take it, ignoring whitespace */ + else if (!isspace((unsigned char)*hp)) /* just take it, ignoring whitespace */ address[NEXTTP()] = *hp; break; case INSIDE_DQUOTE: /* we're in a quoted string, copy verbatim */ - if (*hp != '"') - address[NEXTTP()] = *hp; - else - { - address[NEXTTP()] = *hp; + address[NEXTTP()] = *hp; + if (*hp == '"') state = oldstate; - } break; case INSIDE_PARENS: /* we're in a parenthesized comment, ignore */ @@ -390,10 +404,10 @@ unsigned char *nxtaddr(const unsigned char *hdr /* header to be parsed, NUL to c } #ifdef MAIN -static void parsebuf(unsigned char *longbuf, int reply) +static void parsebuf(char *longbuf, int reply) { - unsigned char *cp; - size_t dummy; + char *cp; + size_t dummy; if (reply) { @@ -401,19 +415,19 @@ static void parsebuf(unsigned char *longbuf, int reply) printf("Rewritten buffer: %s", (char *)longbuf); } else - if ((cp = nxtaddr(longbuf)) != (unsigned char *)NULL) + if ((cp = nxtaddr(longbuf)) != (char *)NULL) do { printf("\t-> \"%s\"\n", (char *)cp); } while - ((cp = nxtaddr((unsigned char *)NULL)) != (unsigned char *)NULL); + ((cp = nxtaddr((char *)NULL)) != (char *)NULL); } int main(int argc, char *argv[]) { - unsigned char buf[BUFSIZ], longbuf[BUFSIZ]; - int ch, reply; + char buf[BUFSIZ], longbuf[BUFSIZ]; + int ch, reply; verbose = reply = FALSE; while ((ch = getopt(argc, argv, "rv")) != EOF) @@ -428,16 +442,18 @@ int main(int argc, char *argv[]) break; } + longbuf[0] = '\0'; + while (fgets(buf, sizeof(buf)-1, stdin)) { if (buf[0] == ' ' || buf[0] == '\t') - strcat(longbuf, buf); + strlcat(longbuf, buf, sizeof(longbuf)); else if (!strncasecmp("From: ", buf, 6) || !strncasecmp("To: ", buf, 4) || !strncasecmp("Reply-", buf, 6) || !strncasecmp("Cc: ", buf, 4) || !strncasecmp("Bcc: ", buf, 5)) - strcpy(longbuf, buf); + strlcpy(longbuf, buf, sizeof(longbuf)); else if (longbuf[0]) { if (verbose)