]> git.mikk.net Git - liburl/commitdiff
Converted quoted-printable decoder to msgproc API.
authorchris mikkelson <chris@mikk.net>
Wed, 10 Mar 2010 04:46:41 +0000 (22:46 -0600)
committerchris mikkelson <chris@mikk.net>
Wed, 10 Mar 2010 04:46:41 +0000 (22:46 -0600)
quoted-printable.c

index 2696140d57ad739c039043bc7fa31ecc98aea502..8111ab3639f99fba5ab6340388dfa021e8ff5b47 100644 (file)
@@ -3,20 +3,31 @@
  * All Rights Reserved, for now.
  */
 
+#include <stdlib.h>
 #include <string.h>
-#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 <unistd.h>
-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 */
+};