1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +00:00

base32: Add base32_decoded_length

This commit is contained in:
Saleem Rashid 2017-05-26 18:48:32 +01:00 committed by Pavol Rusnak
parent b26a44a205
commit c4db240e70
3 changed files with 16 additions and 7 deletions

View File

@ -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);

View File

@ -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

View File

@ -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