commit 8890c639a0b3f5e5b3ec6fe74cdaac841f9350c6
parent ac721ae15c08de48286829c6864991532dba6ac2
Author: phoebos <ben@bvnf.space>
Date: Sat, 10 Jul 2021 20:55:05 +0100
base64: array instead of pointers
Diffstat:
1 file changed, 13 insertions(+), 12 deletions(-)
diff --git 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) {