From 2c4c5ec1ba4cb46d37735cea144ccdb2fd225cde Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Thu, 11 Dec 2008 19:26:11 +0000 Subject: [PATCH] Added simple 5bits to 8 and reverse encoder, with test --- src/base32.c | 21 +++++++++++++++++++++ src/base32.h | 2 ++ tests/base32.c | 13 +++++++++++++ 3 files changed, 36 insertions(+) diff --git a/src/base32.c b/src/base32.c index 9040c88..12885a7 100644 --- a/src/base32.c +++ b/src/base32.c @@ -70,6 +70,27 @@ base32_blksize_enc() return BLKSIZE_ENC; } +int +b32_5to8(int in) +{ + return cb32[in & 31]; +} + +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; + } + return rev32[in]; +} + static int base32_encode(char *buf, size_t *buflen, const void *data, size_t size) { diff --git a/src/base32.h b/src/base32.h index 3eb764d..114b9ae 100644 --- a/src/base32.h +++ b/src/base32.h @@ -19,4 +19,6 @@ struct encoder *get_base32_encoder(void); +int b32_5to8(int); +int b32_8to5(int); #endif diff --git a/tests/base32.c b/tests/base32.c index 723490b..fffaea3 100644 --- a/tests/base32.c +++ b/tests/base32.c @@ -77,6 +77,18 @@ START_TEST(test_base32_decode) } END_TEST +START_TEST(test_base32_5to8_8to5) +{ + int i; + int c; + + for (i = 0; i < 32; i++) { + c = b32_5to8(i); + fail_unless(b32_8to5(c) == i); + } +} +END_TEST + TCase * test_base32_create_tests() { @@ -85,6 +97,7 @@ test_base32_create_tests() tc = tcase_create("Base32"); tcase_add_test(tc, test_base32_encode); tcase_add_test(tc, test_base32_decode); + tcase_add_test(tc, test_base32_5to8_8to5); return tc; }