From 0b280bec2b1b907fb146f4ff50777b3772c926b2 Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Tue, 17 Feb 2009 21:03:13 +0000 Subject: [PATCH] clean up codec reverse inits --- src/base32.c | 37 ++++++++++++++++++------------------- src/base64.c | 9 --------- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/base32.c b/src/base32.c index 06a59fe..5ee0664 100644 --- a/src/base32.c +++ b/src/base32.c @@ -27,7 +27,6 @@ static const char cb32[] = "abcdefghijklmnopqrstuvwxyz012345"; static unsigned char rev32[128]; -static int reverse_init = 0; static int base32_decode(void *, size_t *, const char *, size_t); static int base32_encode(char *, size_t *, const void *, size_t); @@ -70,6 +69,22 @@ base32_blksize_enc() return BLKSIZE_ENC; } +static void +base32_reverse_init() +{ + int i; + unsigned char c; + static int reverse_init = 0; + + if (!reverse_init) { + for (i = 0; i < 32; i++) { + c = cb32[i]; + rev32[(int) c] = i; + } + reverse_init = 1; + } +} + int b32_5to8(int in) { @@ -79,15 +94,7 @@ b32_5to8(int in) int b32_8to5(int in) { - int i; - int c; - if (!reverse_init) { - for (i = 0; i < 32; i++) { - c = cb32[i]; - rev32[(int) c] = i; - } - reverse_init = 1; - } + base32_reverse_init(); return rev32[in]; } @@ -186,17 +193,9 @@ base32_decode(void *buf, size_t *buflen, const char *str, size_t slen) size_t newsize; size_t maxsize; const char *p; - unsigned char c; int len; - int i; - if (!reverse_init) { - for (i = 0; i < 32; i++) { - c = cb32[i]; - rev32[(int) c] = i; - } - reverse_init = 1; - } + base32_reverse_init(); /* chars needed to decode slen */ newsize = BLKSIZE_RAW * (slen / BLKSIZE_ENC + 1) + 1; diff --git a/src/base64.c b/src/base64.c index 9889fb9..7da2d2a 100644 --- a/src/base64.c +++ b/src/base64.c @@ -78,7 +78,6 @@ base64_encode(char *buf, size_t *buflen, const void *data, size_t size) { size_t newsize; size_t maxsize; - unsigned char c; unsigned char *s; unsigned char *p; unsigned char *q; @@ -86,14 +85,6 @@ base64_encode(char *buf, size_t *buflen, const void *data, size_t size) memset(buf, 0, *buflen); - if (!reverse_init) { - for (i = 0; i < 64; i++) { - c = cb64[i]; - rev64[(int) c] = i; - } - reverse_init = 1; - } - /* how many chars can we encode within the buf */ maxsize = BLKSIZE_RAW * (*buflen / BLKSIZE_ENC); /* how big will the encoded data be */