Added simple 5bits to 8 and reverse encoder, with test

This commit is contained in:
Erik Ekman 2008-12-11 19:26:11 +00:00 committed by Erik Ekman
parent 9ff6683119
commit 2c4c5ec1ba
3 changed files with 36 additions and 0 deletions

View file

@ -70,6 +70,27 @@ base32_blksize_enc()
return 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 static int
base32_encode(char *buf, size_t *buflen, const void *data, size_t size) base32_encode(char *buf, size_t *buflen, const void *data, size_t size)
{ {

View file

@ -19,4 +19,6 @@
struct encoder *get_base32_encoder(void); struct encoder *get_base32_encoder(void);
int b32_5to8(int);
int b32_8to5(int);
#endif #endif

View file

@ -77,6 +77,18 @@ START_TEST(test_base32_decode)
} }
END_TEST 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 * TCase *
test_base32_create_tests() test_base32_create_tests()
{ {
@ -85,6 +97,7 @@ test_base32_create_tests()
tc = tcase_create("Base32"); tc = tcase_create("Base32");
tcase_add_test(tc, test_base32_encode); tcase_add_test(tc, test_base32_encode);
tcase_add_test(tc, test_base32_decode); tcase_add_test(tc, test_base32_decode);
tcase_add_test(tc, test_base32_5to8_8to5);
return tc; return tc;
} }