about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorphoebos <ben@bvnf.space>2021-07-10 20:55:05 +0100
committerphoebos <ben@bvnf.space>2021-07-10 20:55:05 +0100
commit8890c639a0b3f5e5b3ec6fe74cdaac841f9350c6 (patch)
tree9059b33dbda6b6cbadfe26d7991f41c686d6d634
parentac721ae15c08de48286829c6864991532dba6ac2 (diff)
downloadkandr-8890c639a0b3f5e5b3ec6fe74cdaac841f9350c6.tar.gz
base64: array instead of pointers
-rw-r--r--base64.c25
1 files changed, 13 insertions, 12 deletions
diff --git a/base64.c b/base64.c
index acc9999..760f5ad 100644
--- a/base64.c
+++ b/base64.c
@@ -57,7 +57,7 @@ static void usage(const char *name) {
 char *base64(char *i, ssize_t length, unsigned dflg){
     if (dflg) return NULL;
     char *out = malloc(length/3 * 4 + 100);
-    char *start_p = out;
+    int o = 0; /* index of position in out */
     while (length > 0) {
         char i1, i2;
         i1 = i2 = 0;
@@ -66,22 +66,23 @@ char *base64(char *i, ssize_t length, unsigned dflg){
             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;
+        out[o] = tbl_base64[i[0] >> 2];
+        ++o;
+        out[o] = tbl_base64[((i[0] & 3) << 4) + (i1 >> 2)];
+        ++o;
+        out[o] = tbl_base64[((i1 & 3) << 4) + (i2 >> 2)];
+        ++o;
+        out[o] = tbl_base64[i2 & 0x3f];
+        ++o;
         i += 3;
     }
-    *out = '\0';
+    out[o] = '\0';
     while (length < 0){
         length++;
-        *--out = tbl_base64[64];
+        o--;
+        out[o] = tbl_base64[64];
     }
-    return start_p;
+    return out;
 }
 
 int main(int argc, char **argv) {