From e6a1f14c205782f9ee8580575185cf6e74428bdc Mon Sep 17 00:00:00 2001 From: phoebos Date: Sun, 11 Jul 2021 23:46:46 +0100 Subject: base64: wrap output --- base64.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/base64.c b/base64.c index d63c301..6bf3b4b 100644 --- a/base64.c +++ b/base64.c @@ -36,7 +36,11 @@ #include #include -#define BUF_SIZE 999 +/* 54 == 76 * 3/4 + * so reading the file 54 bytes at a time will cause + * the base64 string to be printed out wrapped at 76 chars. + */ +#define BUF_SIZE 54 const char tbl_base64[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', @@ -55,6 +59,7 @@ static void usage(const char *name) { } char *base64(char *i, ssize_t length, unsigned dflg){ + /* TODO: support Unicode */ if (dflg) return NULL; char *out = malloc(length/3 * 4 + 100); int o = 0; /* index of position in out */ @@ -85,6 +90,20 @@ char *base64(char *i, ssize_t length, unsigned dflg){ return out; } +void print_wrapped(char *s, int line_length){ + /* printf a string wrapped to line_length chars */ + int c = 0; + while (1){ + if (*s == '\0') break; + printf("%c", *s++); + c++; + if (c == line_length){ + c = 0; + printf("\n"); + } + } +} + int main(int argc, char **argv) { int c; unsigned dflg = 0; @@ -130,7 +149,9 @@ int main(int argc, char **argv) { char *output = base64(buf, bytes, dflg); printf("%s\n", output); + //print_wrapped(output, 76); } + printf("\n"); if (close(fd) != 0) { perror("close"); -- cgit 1.4.1-2-gfad0