mirror of
https://github.com/jedisct1/libsodium.git
synced 2026-08-25 08:37:13 +09:00
Move the codecs from sodium/utils.c to a dedicated file
This commit is contained in:
@@ -87,6 +87,7 @@ libsodium_la_SOURCES = \
|
||||
include/sodium/private/mutex.h \
|
||||
include/sodium/private/sse2_64_32.h \
|
||||
randombytes/randombytes.c \
|
||||
sodium/codecs.c \
|
||||
sodium/core.c \
|
||||
sodium/runtime.c \
|
||||
sodium/utils.c \
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "core.h"
|
||||
#include "utils.h"
|
||||
|
||||
/* Derived from original code by CodesInChaos */
|
||||
char *
|
||||
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) {
|
||||
sodium_misuse(); /* LCOV_EXCL_LINE */
|
||||
}
|
||||
while (i < bin_len) {
|
||||
c = bin[i] & 0xf;
|
||||
b = bin[i] >> 4;
|
||||
x = (unsigned char) (87U + c + (((c - 10U) >> 8) & ~38U)) << 8 |
|
||||
(unsigned char) (87U + b + (((b - 10U) >> 8) & ~38U));
|
||||
hex[i * 2U] = (char) x;
|
||||
x >>= 8;
|
||||
hex[i * 2U + 1U] = (char) x;
|
||||
i++;
|
||||
}
|
||||
hex[i * 2U] = 0U;
|
||||
|
||||
return hex;
|
||||
}
|
||||
|
||||
int
|
||||
sodium_hex2bin(unsigned char *const bin, const size_t bin_maxlen,
|
||||
const char *const hex, const size_t hex_len,
|
||||
const char *const ignore, size_t *const bin_len,
|
||||
const char **const hex_end)
|
||||
{
|
||||
size_t bin_pos = (size_t) 0U;
|
||||
size_t hex_pos = (size_t) 0U;
|
||||
int ret = 0;
|
||||
unsigned char c;
|
||||
unsigned char c_acc = 0U;
|
||||
unsigned char c_alpha0, c_alpha;
|
||||
unsigned char c_num0, c_num;
|
||||
unsigned char c_val;
|
||||
unsigned char state = 0U;
|
||||
|
||||
while (hex_pos < hex_len) {
|
||||
c = (unsigned char) hex[hex_pos];
|
||||
c_num = c ^ 48U;
|
||||
c_num0 = (c_num - 10U) >> 8;
|
||||
c_alpha = (c & ~32U) - 55U;
|
||||
c_alpha0 = ((c_alpha - 10U) ^ (c_alpha - 16U)) >> 8;
|
||||
if ((c_num0 | c_alpha0) == 0U) {
|
||||
if (ignore != NULL && state == 0U && strchr(ignore, c) != NULL) {
|
||||
hex_pos++;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
c_val = (c_num0 & c_num) | (c_alpha0 & c_alpha);
|
||||
if (bin_pos >= bin_maxlen) {
|
||||
ret = -1;
|
||||
errno = ERANGE;
|
||||
break;
|
||||
}
|
||||
if (state == 0U) {
|
||||
c_acc = c_val * 16U;
|
||||
} else {
|
||||
bin[bin_pos++] = c_acc | c_val;
|
||||
}
|
||||
state = ~state;
|
||||
hex_pos++;
|
||||
}
|
||||
if (state != 0U) {
|
||||
hex_pos--;
|
||||
}
|
||||
if (hex_end != NULL) {
|
||||
*hex_end = &hex[hex_pos];
|
||||
}
|
||||
if (bin_len != NULL) {
|
||||
*bin_len = bin_pos;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -287,89 +287,6 @@ sodium_add(unsigned char *a, const unsigned char *b, const size_t len)
|
||||
}
|
||||
}
|
||||
|
||||
/* Derived from original code by CodesInChaos */
|
||||
char *
|
||||
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) {
|
||||
sodium_misuse(); /* LCOV_EXCL_LINE */
|
||||
}
|
||||
while (i < bin_len) {
|
||||
c = bin[i] & 0xf;
|
||||
b = bin[i] >> 4;
|
||||
x = (unsigned char) (87U + c + (((c - 10U) >> 8) & ~38U)) << 8 |
|
||||
(unsigned char) (87U + b + (((b - 10U) >> 8) & ~38U));
|
||||
hex[i * 2U] = (char) x;
|
||||
x >>= 8;
|
||||
hex[i * 2U + 1U] = (char) x;
|
||||
i++;
|
||||
}
|
||||
hex[i * 2U] = 0U;
|
||||
|
||||
return hex;
|
||||
}
|
||||
|
||||
int
|
||||
sodium_hex2bin(unsigned char *const bin, const size_t bin_maxlen,
|
||||
const char *const hex, const size_t hex_len,
|
||||
const char *const ignore, size_t *const bin_len,
|
||||
const char **const hex_end)
|
||||
{
|
||||
size_t bin_pos = (size_t) 0U;
|
||||
size_t hex_pos = (size_t) 0U;
|
||||
int ret = 0;
|
||||
unsigned char c;
|
||||
unsigned char c_acc = 0U;
|
||||
unsigned char c_alpha0, c_alpha;
|
||||
unsigned char c_num0, c_num;
|
||||
unsigned char c_val;
|
||||
unsigned char state = 0U;
|
||||
|
||||
while (hex_pos < hex_len) {
|
||||
c = (unsigned char) hex[hex_pos];
|
||||
c_num = c ^ 48U;
|
||||
c_num0 = (c_num - 10U) >> 8;
|
||||
c_alpha = (c & ~32U) - 55U;
|
||||
c_alpha0 = ((c_alpha - 10U) ^ (c_alpha - 16U)) >> 8;
|
||||
if ((c_num0 | c_alpha0) == 0U) {
|
||||
if (ignore != NULL && state == 0U && strchr(ignore, c) != NULL) {
|
||||
hex_pos++;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
c_val = (c_num0 & c_num) | (c_alpha0 & c_alpha);
|
||||
if (bin_pos >= bin_maxlen) {
|
||||
ret = -1;
|
||||
errno = ERANGE;
|
||||
break;
|
||||
}
|
||||
if (state == 0U) {
|
||||
c_acc = c_val * 16U;
|
||||
} else {
|
||||
bin[bin_pos++] = c_acc | c_val;
|
||||
}
|
||||
state = ~state;
|
||||
hex_pos++;
|
||||
}
|
||||
if (state != 0U) {
|
||||
hex_pos--;
|
||||
}
|
||||
if (hex_end != NULL) {
|
||||
*hex_end = &hex[hex_pos];
|
||||
}
|
||||
if (bin_len != NULL) {
|
||||
*bin_len = bin_pos;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
_sodium_alloc_init(void)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user