From 17355dbe63c675517f88d501d36f7e0fb2d52885 Mon Sep 17 00:00:00 2001 From: Christian Neukirchen Date: Wed, 13 Jul 2016 15:52:39 +0200 Subject: [PATCH] blaze822: api refactoring --- blaze822.c | 89 ++++++++++++++++++++++--------------------------- blaze822.h | 21 ++++++++---- blaze822_priv.h | 3 ++ rfc2047.c | 9 ++--- 4 files changed, 62 insertions(+), 60 deletions(-) diff --git a/blaze822.c b/blaze822.c index 33caba1..7bba970 100644 --- a/blaze822.c +++ b/blaze822.c @@ -337,8 +337,7 @@ blaze822(char *file) mesg->msg = buf; mesg->end = end; - mesg->body = 0; - mesg->bodyend = 0; + mesg->body = mesg->bodyend = mesg->bodychunk = 0; return mesg; } @@ -355,41 +354,37 @@ blaze822_mem(char *src, size_t len) return 0; end = memmem(src, len, "\n\n", 2); - if (!end) end = memmem(src, len, "\r\n\r\n", 4); - if (!end) end = src + len; + if (!end) + end = memmem(src, len, "\r\n\r\n", 4); + if (!end) + end = src + len; - len = end - src; + size_t hlen = end - src; - buf = malloc(len+1); + buf = malloc(hlen+1); if (!buf) return 0; - memcpy(buf, src, len); + memcpy(buf, src, hlen); - end = buf+len+1; + end = buf+hlen+1; *end = 0; // dereferencing *end is safe unfold_hdr(buf, end); mesg->msg = buf; mesg->end = end; - mesg->body = 0; - mesg->bodyend = 0; + mesg->body = src + hlen; + mesg->bodyend = src + len; + mesg->bodychunk = 0; // src is not ours return mesg; } -void -blaze822_mem_body(struct message *mesg, char *buf, size_t len) -{ - mesg->body = buf + (mesg->end - mesg->msg - 2); - mesg->bodyend = buf + len - (mesg->end - mesg->msg - 2); -} - void blaze822_free(struct message *mesg) { free(mesg->msg); - // XXX body? keep track who malloced it? + free(mesg->bodychunk); free(mesg); } @@ -434,24 +429,8 @@ blaze822_loop(int argc, char *argv[], void (*cb)(char *)) return i; } -int -blaze822_body(struct message *mesg, char *file) -{ - int fd = open(file, O_RDONLY); - if (fd < 0) - return fd; - - if (lseek(fd, mesg->end - mesg->msg - 2, SEEK_SET) < 0) { - perror("lseek"); - close(fd); - return -1; - } - - return fd; -} - -int -blaze822_file_body(struct message *mesg, char *file) +struct message * +blaze822_file(char *file) { int fd = open(file, O_RDONLY); if (fd < 0) @@ -461,29 +440,39 @@ blaze822_file_body(struct message *mesg, char *file) if (fstat(fd, &st) < 0) goto error; - if (lseek(fd, mesg->end - mesg->msg - 2, SEEK_SET) < 0) { - perror("lseek"); - goto error; - } - - size_t s = st.st_size - (mesg->end - mesg->msg - 2); + size_t s = st.st_size; - char *body = malloc(s+1); - if (read(fd, body, s) < 0) { + char *buf = malloc(s+1); + if (read(fd, buf, s) < 0) { // XXX handle short reads? perror("read"); goto error; } - body[s] = 0; + close(fd); - mesg->body = body; - mesg->bodyend = body+s; + buf[s] = 0; - close(fd); - return 1; + // XXX duplicate header in ram... + struct message *mesg = blaze822_mem(buf, s); + if (mesg) + mesg->bodychunk = buf; + return mesg; error: close(fd); - return -1; + return 0; } +char * +blaze822_body(struct message *mesg) +{ + return mesg->body; +} + +size_t +blaze822_bodylen(struct message *mesg) +{ + if (!mesg->body || !mesg->bodyend) + return 0; + return mesg->bodyend - mesg->body; +} diff --git a/blaze822.h b/blaze822.h index 2705084..d3d7d17 100644 --- a/blaze822.h +++ b/blaze822.h @@ -1,20 +1,29 @@ #include +#include struct message; -struct message *blaze822(char *file); -struct message *blaze822_mem(char *buf, size_t len); -void blaze822_free(struct message *mesg); +// blaze822.c + +struct message *blaze822(char *file); // just header +struct message *blaze822_file(char *file); // header + body +struct message *blaze822_mem(char *buf, size_t len); // header + body + char *blaze822_hdr_(struct message *mesg, const char *hdr, size_t len); #define blaze822_hdr(mesg, hdr) blaze822_hdr_(mesg, "\0" hdr ":", 2+strlen((hdr))) -int blaze822_body(struct message *mesg, char *file); -void blaze822_mem_body(struct message *mesg, char *buf, size_t len); -int blaze822_loop(int, char **, void (*)(char *)); +void blaze822_free(struct message *mesg); time_t blaze822_date(char *); char *blaze822_addr(char *, char **, char **); +char *blaze822_body(struct message *mesg); +size_t blaze822_bodylen(struct message *mesg); + +int blaze822_loop(int, char **, void (*)(char *)); +// rfc2047.c int blaze822_decode_rfc2047(char *, char *, size_t, char *); +int blaze822_decode_qp(char *start, char *stop, char **deco, size_t *decleno); +int blaze822_decode_b64(char *start, char *stop, char **deco, size_t *decleno); diff --git a/blaze822_priv.h b/blaze822_priv.h index 946b9b0..dfd8895 100644 --- a/blaze822_priv.h +++ b/blaze822_priv.h @@ -3,4 +3,7 @@ struct message { char *end; char *body; char *bodyend; + char *bodychunk; }; + +#define iswsp(c) (((c) == ' ' || (c) == '\t')) diff --git a/rfc2047.c b/rfc2047.c index 36d1ede..15c9cf7 100644 --- a/rfc2047.c +++ b/rfc2047.c @@ -14,7 +14,7 @@ // XXX keep trying bytewise on invalid iconv int -decode_qp(char *start, char *stop, char **deco, size_t *decleno) +blaze822_decode_qp(char *start, char *stop, char **deco, size_t *decleno) { static signed char hex[] = { -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, @@ -55,8 +55,9 @@ decode_qp(char *start, char *stop, char **deco, size_t *decleno) *decleno = buf - *deco; return 1; } + int -decode_b64(char *s, char *e, char **deco, size_t *decleno) +blaze822_decode_b64(char *s, char *e, char **deco, size_t *decleno) { static signed char b64[128] = { -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, @@ -153,9 +154,9 @@ blaze822_decode_rfc2047(char *dst, char *src, size_t dlen, char *tgtenc) char *dec; size_t declen; if (enc == 'q') - decode_qp(start, stop, &dec, &declen); + blaze822_decode_qp(start, stop, &dec, &declen); else if (enc == 'b') - decode_b64(start, stop, &dec, &declen); + blaze822_decode_b64(start, stop, &dec, &declen); else goto nocode;