1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-03 12:00:59 +00:00

base32: Return char* from base32_encode

This commit is contained in:
Saleem Rashid 2017-05-27 10:15:12 +01:00 committed by Pavol Rusnak
parent 0912c4d215
commit 0afb53fba2
3 changed files with 7 additions and 7 deletions

View File

@ -33,22 +33,22 @@ static inline void base32_8to5_raw(const uint8_t *in, uint8_t length, uint8_t *o
static inline int base32_encode_character(uint8_t decoded, const char *alphabet); static inline int base32_encode_character(uint8_t decoded, const char *alphabet);
static inline int base32_decode_character(char encoded, const char *alphabet); static inline int base32_decode_character(char encoded, const char *alphabet);
bool base32_encode(const uint8_t *in, size_t inlen, char *out, size_t outlen, const char *alphabet) { char *base32_encode(const uint8_t *in, size_t inlen, char *out, size_t outlen, const char *alphabet) {
size_t length = base32_encoded_length(inlen); size_t length = base32_encoded_length(inlen);
if (outlen <= length) { if (outlen <= length) {
return false; return NULL;
} }
base32_encode_unsafe(in, inlen, (uint8_t *) out); base32_encode_unsafe(in, inlen, (uint8_t *) out);
for (size_t i = 0; i < length; i++) { for (size_t i = 0; i < length; i++) {
if ((out[i] = base32_encode_character(out[i], alphabet)) == -1) { if ((out[i] = base32_encode_character(out[i], alphabet)) == -1) {
return false; return NULL;
} }
} }
out[length] = '\0'; out[length] = '\0';
return true; return &out[length];
} }
uint8_t *base32_decode(const char *in, size_t inlen, uint8_t *out, size_t outlen, const char *alphabet) { uint8_t *base32_decode(const char *in, size_t inlen, uint8_t *out, size_t outlen, const char *alphabet) {

View File

@ -29,7 +29,7 @@
extern const char *BASE32_ALPHABET_RFC4648; extern const char *BASE32_ALPHABET_RFC4648;
bool base32_encode(const uint8_t *in, size_t inlen, char *out, size_t outlen, const char *alphabet); char *base32_encode(const uint8_t *in, size_t inlen, char *out, size_t outlen, const char *alphabet);
void base32_encode_unsafe(const uint8_t *in, size_t inlen, uint8_t *out); void base32_encode_unsafe(const uint8_t *in, size_t inlen, uint8_t *out);
uint8_t *base32_decode(const char *in, size_t inlen, uint8_t *out, size_t outlen, const char *alphabet); uint8_t *base32_decode(const char *in, size_t inlen, uint8_t *out, size_t outlen, const char *alphabet);

View File

@ -527,11 +527,11 @@ START_TEST(test_base32_rfc4648)
ck_assert_int_eq(outlen, base32_encoded_length(inlen)); ck_assert_int_eq(outlen, base32_encoded_length(inlen));
ck_assert_int_eq(inlen, base32_decoded_length(outlen)); ck_assert_int_eq(inlen, base32_decoded_length(outlen));
ck_assert(base32_encode((uint8_t *) in, inlen, buffer, sizeof(buffer), BASE32_ALPHABET_RFC4648)); ck_assert(base32_encode((uint8_t *) in, inlen, buffer, sizeof(buffer), BASE32_ALPHABET_RFC4648) != NULL);
ck_assert_str_eq(buffer, out); ck_assert_str_eq(buffer, out);
char *ret = (char *) base32_decode(out, outlen, (uint8_t *) buffer, sizeof(buffer), BASE32_ALPHABET_RFC4648); char *ret = (char *) base32_decode(out, outlen, (uint8_t *) buffer, sizeof(buffer), BASE32_ALPHABET_RFC4648);
ck_assert(ret); ck_assert(ret != NULL);
*ret = '\0'; *ret = '\0';
ck_assert_str_eq(buffer, in); ck_assert_str_eq(buffer, in);