From: chris mikkelson Date: Wed, 10 Mar 2010 04:46:41 +0000 (-0600) Subject: Converted quoted-printable decoder to msgproc API. X-Git-Url: https://git.mikk.net/?a=commitdiff_plain;h=4ddfb082a6f088be4c6dd68e4d0d1bd2c1aa45c2;p=liburl Converted quoted-printable decoder to msgproc API. --- diff --git a/quoted-printable.c b/quoted-printable.c index 2696140..8111ab3 100644 --- a/quoted-printable.c +++ b/quoted-printable.c @@ -3,20 +3,31 @@ * All Rights Reserved, for now. */ +#include #include -#include "decoders.h" +#include "msgproc.h" -struct qp_state * -qp_start(struct qp_state *qps) +struct qp_state { + unsigned char c; + int state; +}; + +static void +quoted_start(msgproc *m) { - bzero(qps, sizeof(*qps)); - return qps; + struct qp_state *qps = malloc(sizeof(struct qp_state)); + if (qps) { + bzero(qps, sizeof(*qps)); + } + msgproc_setpriv(m, qps); } -void -qp_stop(struct qp_state *qps) +static void +quoted_finish(msgproc *m) { - qp_start(qps); + struct qp_state *qps = msgproc_getpriv(m); + if (qps) free(qps); + msgproc_free(m); } static inline char @@ -27,7 +38,7 @@ hexval(char c) { return -1; } -int +static int qp_decode(struct qp_state *qps, char *s, int len, char *out, int size, int *olen) { @@ -89,26 +100,31 @@ qp_decode(struct qp_state *qps, char *s, int len, return ret; } -#ifdef _UNIT_TEST -#include -char *chunks[] = { - "the quick=20brown=\r", - "\nfox=3d jumped over\r\n", - 0 -}; -int main(void) { - struct qp_state qps; - char out[80]; - int ret, off = 0, outinc; - char **c; +static void +quoted_process(msgproc *m, char *buf, size_t len) +{ + char tmp[2048]; + int tmplen, ret; + struct qp_state *qps = (struct qp_state *)msgproc_getpriv(m); + msgproc *next = msgproc_next(m); - qp_start(&qps); - for (c=chunks; *c; c++) { - ret = qp_decode(&qps, *c, strlen(*c), out + off, sizeof(out) - off, &outinc); - off += outinc; + while (len > 0) { + ret = qp_decode(qps, buf, len, tmp, sizeof(tmp), &tmplen); + if (ret < 1) { + /* TODO: handle errors */ + } + msgproc_process(next, tmp, tmplen); + buf += ret; len -= ret; } - - write(1,out,off); - return 0; } -#endif + +msgproc_module msgproc_quoted = { + MSGPROC_QUOTED_PRINTABLE, /* type */ + NULL, /* module init */ + NULL, /* set module parameter */ + quoted_start, /* start module instance */ + NULL, /* set module instance parameter */ + quoted_process, /* process data */ + quoted_finish, /* shut down, free module instance */ + NULL /* shut down, free module */ +};