Skip to content
Snippets Groups Projects
Commit e3d745cb authored by Jan Včelák's avatar Jan Včelák :rocket:
Browse files

common lib: add hex string to bytes conversion

parent 1fc3d923
No related branches found
No related tags found
No related merge requests found
......@@ -54,6 +54,8 @@ src/common/hattrie/murmurhash3.c
src/common/hattrie/murmurhash3.h
src/common/heap.c
src/common/heap.h
src/common/hex.c
src/common/hex.h
src/common/latency.c
src/common/latency.h
src/common/libtap/tap.c
......
......@@ -232,7 +232,9 @@ libknots_la_SOURCES = \
common/hattrie/hat-trie.c \
common/hattrie/hat-trie.h \
common/hattrie/murmurhash3.c \
common/hattrie/murmurhash3.h
common/hattrie/murmurhash3.h \
common/hex.c \
common/hex.h
libknotd_la_SOURCES = \
knot/stat/gatherer.c \
......
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "common/errcode.h"
/*!
* \brief Convert HEX char to byte.
* \note Expects valid lowercase letters.
*/
static uint8_t hex_to_num(int c)
{
if (c >= '0' && c <= '9')
return c - '0';
else
return c - 'a' + 10;
}
/*!
* \brief Convert string encoded in hex to bytes.
*/
int hex_decode(const char *input, uint8_t **output, size_t *output_size)
{
// input validation (length and content)
size_t input_size = strlen(input);
if (input_size % 2 != 0)
return KNOT_EINVAL;
for (size_t i = 0; i < input_size; i++) {
if (!isxdigit(input[i]))
return KNOT_EINVAL;
}
// output allocation
size_t result_size = input_size / 2;
uint8_t *result = malloc(result_size * sizeof(uint8_t));
if (!output)
return KNOT_ENOMEM;
// conversion
for (size_t i = 0; i < result_size; i++) {
int high_nib = tolower(input[2 * i]);
int low_nib = tolower(input[2 * i + 1]);
result[i] = hex_to_num(high_nib) << 4 | hex_to_num(low_nib);
}
*output = result;
*output_size = result_size;
return KNOT_EOK;
}
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file hex.h
*
* \brief Coversion between HEX strings and bytes.
*
* \author Jan Vcelak <jan.vcelak@nic.cz>
*
* \addtogroup common_lib
* @{
*/
#ifndef _KNOT_COMMON_HEX_H_
#define _KNOT_COMMON_HEX_H_
#include <stdint.h>
#include <stdlib.h>
/*!
* \brief Convert string encoded in hex to bytes.
*
* \param input Hex encoded input string.
* \param output Decoded bytes.
* \param output_size Size of the output.
*
* \return Error code, KNOT_EOK if successful.
*/
int hex_decode(const char *input, uint8_t **output, size_t *output_size);
#endif // _KNOT_COMMON_HEX_H_
/*! @} */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment