From c4db240e702ce722357b810916b9db71b402ad24 Mon Sep 17 00:00:00 2001 From: Saleem Rashid Date: Fri, 26 May 2017 18:48:32 +0100 Subject: [PATCH] base32: Add base32_decoded_length --- base32.c | 6 ++++++ base32.h | 1 + test_check.c | 16 +++++++++------- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/base32.c b/base32.c index 8604b0585..4ae018ce5 100644 --- a/base32.c +++ b/base32.c @@ -63,6 +63,12 @@ size_t base32_encoded_length(size_t inlen) { return (inlen / 5) * 8 + (remainder * 8 + 4) / 5; } +size_t base32_decoded_length(size_t inlen) { + uint8_t remainder = inlen % 8; + + return (inlen / 8) * 5 + (remainder * 5) / 8; +} + void base32_5to8(const uint8_t *in, uint8_t length, uint8_t *out) { if (length >= 1) { out[0] = (in[0] >> 3); diff --git a/base32.h b/base32.h index d2d7df41d..edef9806b 100644 --- a/base32.h +++ b/base32.h @@ -33,5 +33,6 @@ bool base32_encode(const uint8_t *in, size_t inlen, char *out, size_t outlen, co void base32_encode_unsafe(const uint8_t *in, size_t inlen, uint8_t *out); size_t base32_encoded_length(size_t inlen); +size_t base32_decoded_length(size_t inlen); #endif diff --git a/test_check.c b/test_check.c index e814da204..54037b615 100644 --- a/test_check.c +++ b/test_check.c @@ -518,15 +518,17 @@ START_TEST(test_base32_rfc4648) char buffer[64]; for (size_t i = 0; i < (sizeof(tests) / sizeof(*tests)); i++) { - const char *input = tests[i].input; - const char *output = tests[i].output; + const char *in = tests[i].input; + const char *out = tests[i].output; - size_t inlen = strlen(input); - size_t outlen = base32_encoded_length(inlen); - ck_assert_int_eq(outlen, strlen(output)); + size_t inlen = strlen(in); + size_t outlen = strlen(out); - base32_encode((uint8_t *) input, inlen, buffer, sizeof(buffer), BASE32_ALPHABET_RFC4648); - ck_assert_str_eq(buffer, output); + ck_assert_int_eq(outlen, base32_encoded_length(inlen)); + ck_assert_int_eq(inlen, base32_decoded_length(outlen)); + + base32_encode((uint8_t *) in, inlen, buffer, sizeof(buffer), BASE32_ALPHABET_RFC4648); + ck_assert_str_eq(buffer, out); } } END_TEST