about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorphoebos <ben@bvnf.space>2021-07-10 20:48:46 +0100
committerphoebos <ben@bvnf.space>2021-07-10 20:48:46 +0100
commitac721ae15c08de48286829c6864991532dba6ac2 (patch)
treea61cf1b529707b9098cc04f990b5d9278e0a76e6
parenta34ba1fe4e9cb27ca7df689e477576181e0009d0 (diff)
downloadkandr-ac721ae15c08de48286829c6864991532dba6ac2.tar.gz
base64: initial with pointers
doesn't work
-rw-r--r--base64.c29
1 files changed, 28 insertions, 1 deletions
diff --git a/base64.c b/base64.c
index e649728..acc9999 100644
--- a/base64.c
+++ b/base64.c
@@ -32,6 +32,7 @@
 
 #include <unistd.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 
@@ -54,7 +55,33 @@ static void usage(const char *name) {
 }
 
 char *base64(char *i, ssize_t length, unsigned dflg){
-    return i;
+    if (dflg) return NULL;
+    char *out = malloc(length/3 * 4 + 100);
+    char *start_p = out;
+    while (length > 0) {
+        char i1, i2;
+        i1 = i2 = 0;
+        length -= 3;
+        if (length >= -2) {
+            i2 = i[2];
+            if (length >= -1) i1 = i[1];
+        }
+        *out = tbl_base64[i[0] >> 2];
+        ++out;
+        *out = tbl_base64[((i[0] & 3) << 4) + (i1 >> 2)];
+        ++out;
+        *out = tbl_base64[((i1 & 3) << 4) + (i2 >> 2)];
+        ++out;
+        *out = tbl_base64[i2 & 0x3f];
+        ++out;
+        i += 3;
+    }
+    *out = '\0';
+    while (length < 0){
+        length++;
+        *--out = tbl_base64[64];
+    }
+    return start_p;
 }
 
 int main(int argc, char **argv) {