about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorphoebos <ben@bvnf.space>2021-07-11 23:46:46 +0100
committerphoebos <ben@bvnf.space>2021-07-11 23:46:46 +0100
commite6a1f14c205782f9ee8580575185cf6e74428bdc (patch)
tree714b427f04b17306a7290da7df279f051c8631a4
parent0278dc6c7c605e7ef87e0d1127f7bc67cee4f66a (diff)
downloadkandr-e6a1f14c205782f9ee8580575185cf6e74428bdc.tar.gz
base64: wrap output
-rw-r--r--base64.c23
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");