rohrpost

A commandline mail client to change the world as we see it.
git clone git://r-36.net/rohrpost
Log | Files | Refs | LICENSE

commit 56d7dc2f40c709b78b997c755a1766eb75e0b450
parent be740e56ae9bba9d06943b8378edaff489f6e556
Author: Christoph Lohmann <20h@r-36.net>
Date:   Sat, 21 Apr 2012 17:52:22 +0200

Fixing base64 and adding the util for encoding.

Diffstat:
base64.c | 8+++++---
mime.c | 15+++++++++++++++
mime.h | 1+
util.c | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
util.h | 12++++++++++++
5 files changed, 118 insertions(+), 3 deletions(-)

diff --git a/base64.c b/base64.c @@ -50,9 +50,11 @@ b64enc(char *str, int l) for (; p < l; p += 3, po += 4) { ret = reallocz(ret, po + 5, 0); - ret[po] = be[str[p] >> 2]; - ret[po+1] = be[(str[p] & 0x03) << 4 | str[p+1] >> 4]; - ret[po+2] = be[(str[p+1] & 0x0F) << 2 | str[p+2] >> 6]; + ret[po] = be[(str[p] & 0xFC) >> 2]; + ret[po+1] = be[(str[p] & 0x03) << 4 \ + | (str[p+1] & 0xF0) >> 4]; + ret[po+2] = be[(str[p+1] & 0x0F) << 2 \ + | (str[p+2] & 0xC0) >> 6]; ret[po+3] = be[str[p+2] & 0x3F]; } left = l - p + 3; diff --git a/mime.c b/mime.c @@ -332,6 +332,21 @@ mime_decodeparam(char *value) } char * +mime_encodestring(char *value) +{ + char *b64, *ret; + + if (strisascii(value)) + return memdup(value, strlen(value)+1); + + b64 = b64enc(value, strlen(value)); + ret = smprintf("=?UTF-8?b?%s?=", b64); + free(b64); + + return ret; +} + +char * mime_encodeheader(char *header, char *value) { char *ret, *b64, *p, *mp, *str; diff --git a/mime.h b/mime.h @@ -37,6 +37,7 @@ char *mime_decodeheaderext(char *value); int mime_isextws(char *str, int len); char *mime_decodeheader(char *value); char *mime_decodeparam(char *value); +char *mime_encodestring(char *value); char *mime_encodeheader(char *header, char *value); int mime_paramsort(llistelem_t *elem1, llistelem_t *elem2); llist_t *mime_sanitizeparams(llist_t *phdr); diff --git a/util.c b/util.c @@ -0,0 +1,85 @@ +/* + * Copy me if you can. + * by 20h + */ + +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> + +#include "ind.h" +#include "arg.h" +#include "mime.h" +#include "llist.h" + +char *argv0; + +void +utilusage(void) +{ + die("usage: %s [-h] [-e|-d] string\n", argv0); +} + +int +utilmain(int argc, char *argv[]) +{ + int status, worklen; + llist_t *workl; + char *works, *ress; + + enum { + ENCODE = 0x04, + DECODE = 0x08, + ISBODY = 0x10, + + NOARGS = 0x40 + }; + + status = 0; + + ARGBEGIN { + case 'd': + status |= DECODE; + break; + case 'e': + status |= ENCODE; + break; + case 'o': + status |= ISBODY; + break; + case 'h': + default: + utilusage(); + } ARGEND; + + worklen = 0; + if (argc < 1) { + works = readtoeoffd(0, &worklen); + if (works == NULL) + edie("readtoeoffd"); + } else { + workl = llist_splitargv(argc, argv); + if (workl == NULL) + utilusage(); + works = llist_joinstr(workl, " "); + llist_free(workl); + } + + if (status & ENCODE) { + ress = mime_encodestring(works); + printf("%s", ress); + } else if (status & DECODE) { + ress = mime_decodeheader(works); + printf("%s", ress); + } else { + free(works); + utilusage(); + } + free(ress); + free(works); + + return 0; +} + diff --git a/util.h b/util.h @@ -0,0 +1,12 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef __UTIL_H__ +#define __UTIL_H__ + +int utilmain(int argc, char *argv[]); + +#endif +