Make sodium_bin2hex() slightly faster

This commit is contained in:
Frank Denis 2014-10-07 21:15:46 -07:00
parent 814df1e60d
commit 9e64361e66

View File

@ -97,16 +97,21 @@ sodium_bin2hex(char * const hex, const size_t hex_maxlen,
const unsigned char * const bin, const size_t bin_len)
{
size_t i = (size_t) 0U;
unsigned int x;
int b;
int c;
if (bin_len >= SIZE_MAX / 2 || hex_maxlen < bin_len * 2U) {
abort(); /* LCOV_EXCL_LINE */
}
while (i < bin_len) {
c = bin[i] & 0xf;
b = bin[i] >> 4;
hex[i * 2U] = (char) (87 + b + (((b - 10) >> 31) & -39));
b = bin[i] & 0xf;
hex[i * 2U + 1U] = (char) (87 + b + (((b - 10) >> 31) & -39));
x = (unsigned char) (87 + c + (((c - 10) >> 31) & -39)) << 8 |
(unsigned char) (87 + b + (((b - 10) >> 31) & -39));
hex[i * 2U] = (char) x;
x >>= 8;
hex[i * 2U + 1U] = (char) x;
i++;
}
hex[i * 2U] = 0;