mirror of
https://github.com/yarrick/iodine.git
synced 2024-12-22 13:23:32 +02:00
Added simple 5bits to 8 and reverse encoder, with test
This commit is contained in:
parent
9ff6683119
commit
2c4c5ec1ba
3 changed files with 36 additions and 0 deletions
21
src/base32.c
21
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)
|
||||
{
|
||||
|
|
|
@ -19,4 +19,6 @@
|
|||
|
||||
struct encoder *get_base32_encoder(void);
|
||||
|
||||
int b32_5to8(int);
|
||||
int b32_8to5(int);
|
||||
#endif
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue