From 2e80e1093ab6de23c33fbc1f258ce281f1e4dae6 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sat, 9 Aug 2014 08:07:07 +0000 Subject: Tox: - added pooling thread - added test code to check connection git-svn-id: http://svn.miranda-ng.org/main/trunk@10133 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/tox_utils.cpp | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 protocols/Tox/src/tox_utils.cpp (limited to 'protocols/Tox/src/tox_utils.cpp') diff --git a/protocols/Tox/src/tox_utils.cpp b/protocols/Tox/src/tox_utils.cpp new file mode 100644 index 0000000000..093becbce4 --- /dev/null +++ b/protocols/Tox/src/tox_utils.cpp @@ -0,0 +1,49 @@ +#include "common.h" + +uint8_t *CToxProto::HexToBinString(char *hex_string) +{ + // byte is represented by exactly 2 hex digits, so lenth of binary string + // is half of that of the hex one. only hex string with even length + // valid. the more proper implementation would be to check if strlen(hex_string) + // is odd and return error code if it is. we assume strlen is even. if it's not + // then the last byte just won't be written in 'ret'. + size_t i, len = (strlen(hex_string) / 2); + uint8_t *ret = (uint8_t*)mir_alloc(len); + char *pos = hex_string; + + for (i = 0; i < len; i++, pos += 2) + { + sscanf(pos, "%2hhx", &ret[i]); + } + + return ret; +} + +char *CToxProto::BinToHexString(uint8_t *bin_string) +{ + uint32_t i, delta = 0, pos_extra, sum_extra = 0; + char *ret = (char*)mir_alloc(TOX_FRIEND_ADDRESS_SIZE); + + for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) + { + sprintf(&ret[2 * i + delta], "%02hhX", bin_string[i]); + + if ((i + 1) == TOX_CLIENT_ID_SIZE) + pos_extra = 2 * (i + 1) + delta; + + if (i >= TOX_CLIENT_ID_SIZE) + sum_extra |= bin_string[i]; + + /*if (!((i + 1) % FRADDR_TOSTR_CHUNK_LEN)) { + id_str[2 * (i + 1) + delta] = ' '; + delta++; + }*/ + } + + ret[2 * i + delta] = 0; + + if (!sum_extra) + ret[pos_extra] = 0; + + return ret; +} \ No newline at end of file -- cgit v1.2.3