From da69892f56a081cb4cd65eb0a8065783d9473bd3 Mon Sep 17 00:00:00 2001 From: "Alex Xu (Hello71)" Date: Sun, 26 Aug 2018 19:41:23 -0400 Subject: Refactor. --- src/util.c | 62 +++++--------------------------------------------------------- 1 file changed, 5 insertions(+), 57 deletions(-) (limited to 'src/util.c') diff --git a/src/util.c b/src/util.c index b552afc..bd2c9a7 100644 --- a/src/util.c +++ b/src/util.c @@ -1,63 +1,11 @@ -// SPDX-License-Identifier: BSD-3-Clause +#include "util.h" -#include -#include -#include +#include #include #include - -#include "util.h" - -static inline signed char hexchr2int(char c) { - /* No, nobody uses EBCDIC anymore. */ - return (c >= '0' && c <= '9') ? (c - '0') : - (c >= 'a' && c <= 'f') ? (c - 'a' + 10) : - (c >= 'A' && c <= 'F') ? (c - 'A' + 10) : - -1; -} - -/** - * Decode hex. - * - * \param dest where to store the decoded data - * \param size the maximum number of bytes to decode - * \param src the hex-encoded data - * - * \return 0 if an error occurred, otherwise the number of bytes written to - * dest - */ -size_t hex2mem(unsigned char *dest, size_t size, const char *src) { -#ifdef DEBUG - fprintf(stderr, "hex decoding %zu bytes\n", size); -#endif - size_t i; - for (i = 0; i < size; i++) { - int n1 = hexchr2int(src[2*i]); - if (n1 < 0) return 0; - int n2 = hexchr2int(src[2*i+1]); - if (n2 < 0) return 0; - dest[i] = (unsigned char)(n1 << 4 | n2); - } - return i; -} - -static const char *HEX_CHARS = "0123456789abcdef"; - -/** Encode hex. - * - * \param dest where to store the encoded data (must have at least size*2+1 bytes) - * \param src the data to encode - * \param size the number of bytes to encode - */ -void mem2hex(char *dest, const void *src, size_t size) { - size_t i; - for (i = 0; i < size; i++) { - unsigned char c = *((const unsigned char *)src + i); - dest[2*i] = HEX_CHARS[c >> 4]; - dest[2*i+1] = HEX_CHARS[c & 0xf]; - } - dest[2*i] = '\0'; -} +#include +#include +#include ssize_t random_get(void *buf, size_t buflen, unsigned int flags) { memset(buf, 0, buflen); -- cgit v1.2.3-54-g00ecf