commit ac721ae15c08de48286829c6864991532dba6ac2
parent a34ba1fe4e9cb27ca7df689e477576181e0009d0
Author: phoebos <ben@bvnf.space>
Date: Sat, 10 Jul 2021 20:48:46 +0100
base64: initial with pointers
doesn't work
Diffstat:
M | base64.c | | | 29 | ++++++++++++++++++++++++++++- |
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git 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) {