Add sodium_base64_encoded_len()

This commit is contained in:
Frank Denis 2017-09-21 11:25:09 +02:00
parent 4ce2856a5d
commit 3e0b4dec6e
6 changed files with 28 additions and 11 deletions

View File

@ -528,6 +528,7 @@ _randombytes_stir 1 1
_randombytes_uniform 1 1
_sodium_add 0 0
_sodium_allocarray 0 0
_sodium_base64_encoded_len 1 1
_sodium_base642bin 1 1
_sodium_bin2base64 1 1
_sodium_bin2hex 1 1

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -75,6 +75,9 @@ int sodium_hex2bin(unsigned char * const bin, const size_t bin_maxlen,
((((BIN_LEN) - ((BIN_LEN) / 3U) * 3U) | (((BIN_LEN) - ((BIN_LEN) / 3U) * 3U) >> 1)) & 1U) * \
(4U - (~((((VARIANT) & 2U) >> 1) - 1U) & (3U - ((BIN_LEN) - ((BIN_LEN) / 3U) * 3U)))) + 1U)
SODIUM_EXPORT
size_t sodium_base64_encoded_len(const size_t bin_len, const int variant);
SODIUM_EXPORT
char *sodium_bin2base64(char * const b64, const size_t b64_maxlen,
const unsigned char * const bin, const size_t bin_len,

View File

@ -159,6 +159,22 @@ b64_urlsafe_char_to_byte(int c)
#define VARIANT_NO_PADDING_MASK 0x2U
#define VARIANT_URLSAFE_MASK 0x4U
static void
sodium_base64_check_variant(const int variant)
{
if ((((unsigned int) variant) & ~ 0x6U) != 0x1U) {
sodium_misuse();
}
}
size_t
sodium_base64_encoded_len(const size_t bin_len, const int variant)
{
sodium_base64_check_variant(variant);
return sodium_base64_ENCODED_LEN(bin_len, variant);
}
char *
sodium_bin2base64(char * const b64, const size_t b64_maxlen,
const unsigned char * const bin, const size_t bin_len,
@ -172,9 +188,7 @@ sodium_bin2base64(char * const b64, const size_t b64_maxlen,
size_t remainder;
unsigned int acc = 0U;
if ((((unsigned int) variant) & ~ 0x6U) != 0x1U) {
sodium_misuse();
}
sodium_base64_check_variant(variant);
nibbles = bin_len / 3;
remainder = bin_len - 3 * nibbles;
b64_len = nibbles * 4;
@ -263,9 +277,7 @@ sodium_base642bin(unsigned char * const bin, const size_t bin_maxlen,
unsigned int d;
char c;
if ((((unsigned int) variant) & ~ 0x6U) != 0x1U) {
sodium_misuse();
}
sodium_base64_check_variant(variant);
is_urlsafe = ((unsigned int) variant) & VARIANT_URLSAFE_MASK;
while (b64_pos < b64_len) {
c = b64[b64_pos];

View File

@ -206,7 +206,8 @@ main(void)
assert(sizeof buf1 >= 100);
bin_len = (size_t) randombytes_uniform(100);
bin = (unsigned char *) sodium_malloc(bin_len);
b64_len = 1 + (bin_len + 2) / 3 * 4;
b64_len = (bin_len + 2U) / 3U * 4U + 1U;
assert(b64_len == sodium_base64_encoded_len(bin_len, sodium_base64_VARIANT_URLSAFE));
b64_ = (char *) sodium_malloc(b64_len);
randombytes_buf(bin, bin_len);
memcpy(buf1, bin, bin_len);