From 7c2d98c32b3712a4619aa2914177efd81f6c3e32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Pettersson?= Date: Tue, 2 Sep 2014 13:58:34 +0200 Subject: [PATCH] Added tox-weechat-utils.* --- src/tox-weechat-utils.c | 39 +++++++++++++++++++++++++++++++++++++++ src/tox-weechat-utils.h | 16 ++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/tox-weechat-utils.c create mode 100644 src/tox-weechat-utils.h diff --git a/src/tox-weechat-utils.c b/src/tox-weechat-utils.c new file mode 100644 index 0000000..90a2ec0 --- /dev/null +++ b/src/tox-weechat-utils.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +#include "tox-weechat-utils.h" + +uint8_t * +tox_weechat_hex2bin(const char *hex) +{ + size_t length = strlen(hex) / 2; + uint8_t *bin = malloc(length); + const char *position = hex; + + for (size_t i = 0; i < length; ++i) + { + sscanf(position, "%2hhx", &bin[i]); + position += 2; + } + + return bin; +} + +char * +weechat_tox_bin2hex(const uint8_t *bin, size_t size) +{ + char *hex = malloc(size * 2 + 1); + char *position = hex; + + for (size_t i = 0; i < size; ++i) + { + sprintf(position, "%02X", bin[i]); + position += 2; + } + *position = 0; + + return hex; +} + diff --git a/src/tox-weechat-utils.h b/src/tox-weechat-utils.h new file mode 100644 index 0000000..94c4d59 --- /dev/null +++ b/src/tox-weechat-utils.h @@ -0,0 +1,16 @@ +#ifndef TOX_WEECHAT_UTILS_H +#define TOX_WEECHAT_UTILS_H + +#include + +/** + * Convert a hex string to a binary address. Return value must be freed. + */ +uint8_t *tox_weechat_hex2bin(const char *hex); + +/** + * Convert a binary address to a null-terminated hex string. Must be freed. + */ +char *tox_weechat_bin2hex(const char *hex); + +#endif // TOX_WEECHAT_UTILS_H