diff options
-rw-r--r-- | base64.c | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/base64.c b/base64.c index d63c301..6bf3b4b 100644 --- a/base64.c +++ b/base64.c @@ -36,7 +36,11 @@ #include <sys/stat.h> #include <fcntl.h> -#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"); |