From 8890c639a0b3f5e5b3ec6fe74cdaac841f9350c6 Mon Sep 17 00:00:00 2001 From: phoebos Date: Sat, 10 Jul 2021 20:55:05 +0100 Subject: base64: array instead of pointers --- base64.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'base64.c') 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) { -- cgit 1.4.1-2-gfad0