X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=rfc822.c;h=15b88f05ef476312af280de1d2ec7183ed4768db;hb=cb8d898683ffccdf8de42a5b1236a6cf3cdbb6ce;hp=28dac71d96b2399abbf21790dafac2b79bdbbc30;hpb=82d5e0b48f322034a7c13e27ed55a19ce934c7b8;p=~andy%2Ffetchmail diff --git a/rfc822.c b/rfc822.c index 28dac71d..15b88f05 100644 --- a/rfc822.c +++ b/rfc822.c @@ -19,17 +19,24 @@ is part of fetchmail and the Unix Cookbook, and are released under the MIT license. Compile with -DMAIN to build the demonstrator. ******************************************************************************/ + +#include "config.h" + #include #include #include +#include #include -#ifndef MAIN #include "fetchmail.h" +#include "sdump.h" + +#ifndef MAIN #include "i18n.h" #else +#include static int verbose; -char *program_name = "rfc822"; +const char *program_name = "rfc822"; #endif /* MAIN */ #ifndef TRUE @@ -39,13 +46,15 @@ char *program_name = "rfc822"; #define HEADER_END(p) ((p)[0] == '\n' && ((p)[1] != ' ' && (p)[1] != '\t')) -unsigned char *reply_hack(buf, host, length) +#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 *buf; /* header to be hacked */ -const unsigned char *host; /* server hostname */ -int *length; { - 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; @@ -70,14 +79,16 @@ int *length; } #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 */ /* @@ -94,7 +105,7 @@ int *length; if (verbose) { printf("state %d: %s", state, buf); - printf("%*s^\n", from - buf + 10, " "); + printf("%*s^\n", (int)(from - buf + 10), " "); } #endif /* MAIN */ if (state != 2) @@ -114,7 +125,7 @@ int *length; 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; @@ -133,12 +144,12 @@ int *length; && 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); @@ -156,7 +167,7 @@ int *length; { parens_from = from; } - else if (!isspace(*from)) + else if (!isspace((unsigned char)*from)) has_bare_name_part = TRUE; break; @@ -206,23 +217,26 @@ int *length; } #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(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 */ -const unsigned char *hdr; /* header to be parsed, NUL to continue previous hdr */ { - 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; @@ -246,13 +260,15 @@ const unsigned char *hdr; /* header to be parsed, NUL to continue previous hdr * tp = 0; } + if (!hp) return NULL; + for (; *hp; hp++) { #ifdef MAIN if (verbose) { printf("state %d: %s", state, orighdr); - printf("%*s^\n", hp - orighdr + 10, " "); + printf("%*s^\n", (int)(hp - orighdr + 10), " "); } #endif /* MAIN */ @@ -263,13 +279,13 @@ const unsigned char *hdr; /* header to be parsed, NUL to continue previous hdr * 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 */ { @@ -304,7 +320,7 @@ const unsigned char *hdr; /* header to be parsed, NUL to continue previous hdr * state = INSIDE_BRACKETS; tp = 0; } - else if (*hp != ',' && !isspace(*hp)) + else if (*hp != ',' && !isspace((unsigned char)*hp)) { --hp; state = BARE_ADDRESS; @@ -339,18 +355,14 @@ const unsigned char *hdr; /* header to be parsed, NUL to continue previous hdr * 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 */ @@ -389,29 +401,30 @@ const unsigned char *hdr; /* header to be parsed, NUL to continue previous hdr * } #ifdef MAIN -static void parsebuf(unsigned char *longbuf, int reply) +static void parsebuf(char *longbuf, int reply) { - unsigned char *cp; + char *cp; + size_t dummy; if (reply) { - reply_hack(longbuf, "HOSTNAME.NET"); - printf("Rewritten buffer: %s", longbuf); + reply_hack(longbuf, "HOSTNAME.NET", &dummy); + 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", cp); + printf("\t-> \"%s\"\n", (char *)cp); } while - ((cp = nxtaddr((unsigned char *)NULL)) != (unsigned char *)NULL); + ((cp = nxtaddr((char *)NULL)) != (char *)NULL); } -main(int argc, char *argv[]) +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) @@ -426,16 +439,18 @@ 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) @@ -450,6 +465,7 @@ main(int argc, char *argv[]) fputs(longbuf, stdout); parsebuf(longbuf, reply); } + exit(0); } #endif /* MAIN */