Added tox-weechat-utils.*

This commit is contained in:
Håvard Pettersson 2014-09-02 13:58:34 +02:00
parent abd3505d40
commit 7c2d98c32b
2 changed files with 55 additions and 0 deletions

39
src/tox-weechat-utils.c Normal file
View File

@ -0,0 +1,39 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#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;
}

16
src/tox-weechat-utils.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef TOX_WEECHAT_UTILS_H
#define TOX_WEECHAT_UTILS_H
#include <stdint.h>
/**
* 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