diff options
-rw-r--r-- | auto_tests/messenger_test.c | 13 | ||||
-rw-r--r-- | other/bootstrap_serverdaemon/tox_bootstrap_daemon.c | 5 | ||||
-rw-r--r-- | other/fun/sign.c | 15 | ||||
-rw-r--r-- | testing/DHT_test.c | 6 | ||||
-rw-r--r-- | testing/Messenger_test.c | 10 | ||||
-rw-r--r-- | testing/experiment/group_chats_test1.c | 21 | ||||
-rw-r--r-- | testing/misc_tools.c | 18 | ||||
-rw-r--r-- | testing/tox_sync.c | 5 |
8 files changed, 39 insertions, 54 deletions
diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c index 5c8e242e..0393e6d9 100644 --- a/auto_tests/messenger_test.c +++ b/auto_tests/messenger_test.c | |||
@@ -14,6 +14,7 @@ | |||
14 | #include "config.h" | 14 | #include "config.h" |
15 | #endif | 15 | #endif |
16 | 16 | ||
17 | #include "../testing/misc_tools.c" // hex_string_to_bin | ||
17 | #include "../toxcore/Messenger.h" | 18 | #include "../toxcore/Messenger.h" |
18 | #include "../toxcore/Lossless_UDP.h" | 19 | #include "../toxcore/Lossless_UDP.h" |
19 | #include <sys/types.h> | 20 | #include <sys/types.h> |
@@ -41,18 +42,6 @@ int friend_id_num = 0; | |||
41 | 42 | ||
42 | Messenger *m; | 43 | Messenger *m; |
43 | 44 | ||
44 | unsigned char *hex_string_to_bin(char hex_string[]) | ||
45 | { | ||
46 | size_t i, len = strlen(hex_string); | ||
47 | unsigned char *val = calloc(1, len); | ||
48 | char *pos = hex_string; | ||
49 | |||
50 | for (i = 0; i < len; ++i, pos += 2) | ||
51 | sscanf(pos, "%2hhx", &val[i]); | ||
52 | |||
53 | return val; | ||
54 | } | ||
55 | |||
56 | START_TEST(test_m_sendmesage) | 45 | START_TEST(test_m_sendmesage) |
57 | { | 46 | { |
58 | char *message = "h-hi :3"; | 47 | char *message = "h-hi :3"; |
diff --git a/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c index f2c54bb1..7f1f652c 100644 --- a/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c +++ b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c | |||
@@ -266,8 +266,9 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6) | |||
266 | goto next; | 266 | goto next; |
267 | } | 267 | } |
268 | 268 | ||
269 | const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port), | 269 | uint8_t *bs_public_key_bin = hex_string_to_bin((char *)bs_public_key); |
270 | hex_string_to_bin((char *)bs_public_key)); | 270 | const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port), bs_public_key_bin); |
271 | free(bs_public_key_bin); | ||
271 | 272 | ||
272 | if (!address_resolved) { | 273 | if (!address_resolved) { |
273 | syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %s. Skipping the server.\n", i, NAME_ADDRESS, bs_address); | 274 | syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %s. Skipping the server.\n", i, NAME_ADDRESS, bs_address); |
diff --git a/other/fun/sign.c b/other/fun/sign.c index 423d974a..eaea9d6a 100644 --- a/other/fun/sign.c +++ b/other/fun/sign.c | |||
@@ -17,19 +17,7 @@ | |||
17 | */ | 17 | */ |
18 | #include <sodium.h> | 18 | #include <sodium.h> |
19 | #include <string.h> | 19 | #include <string.h> |
20 | 20 | #include "../../testing/misc_tools.c" // hex_string_to_bin | |
21 | unsigned char *hex_string_to_bin(char hex_string[]) | ||
22 | { | ||
23 | size_t len = strlen(hex_string); | ||
24 | unsigned char *val = malloc(len); | ||
25 | char *pos = hex_string; | ||
26 | int i; | ||
27 | |||
28 | for (i = 0; i < len; ++i, pos += 2) | ||
29 | sscanf(pos, "%2hhx", &val[i]); | ||
30 | |||
31 | return val; | ||
32 | } | ||
33 | 21 | ||
34 | int load_file(char *filename, char **result) | 22 | int load_file(char *filename, char **result) |
35 | { | 23 | { |
@@ -90,6 +78,7 @@ int main(int argc, char *argv[]) | |||
90 | unsigned long long smlen; | 78 | unsigned long long smlen; |
91 | char *sm = malloc(size + crypto_sign_ed25519_BYTES * 2); | 79 | char *sm = malloc(size + crypto_sign_ed25519_BYTES * 2); |
92 | crypto_sign_ed25519(sm, &smlen, data, size, secret_key); | 80 | crypto_sign_ed25519(sm, &smlen, data, size, secret_key); |
81 | free(secret_key); | ||
93 | 82 | ||
94 | if (smlen - size != crypto_sign_ed25519_BYTES) | 83 | if (smlen - size != crypto_sign_ed25519_BYTES) |
95 | goto fail; | 84 | goto fail; |
diff --git a/testing/DHT_test.c b/testing/DHT_test.c index a8ad8426..5ddb5945 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c | |||
@@ -205,8 +205,10 @@ int main(int argc, char *argv[]) | |||
205 | 205 | ||
206 | if (scanf("%s", temp_id) != 1) | 206 | if (scanf("%s", temp_id) != 1) |
207 | exit(0); | 207 | exit(0); |
208 | 208 | ||
209 | DHT_addfriend(dht, hex_string_to_bin(temp_id)); | 209 | uint8_t *bin_id = hex_string_to_bin(temp_id); |
210 | DHT_addfriend(dht, bin_id); | ||
211 | free(bin_id); | ||
210 | 212 | ||
211 | perror("Initialization"); | 213 | perror("Initialization"); |
212 | 214 | ||
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 969d6b1f..707bb8db 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c | |||
@@ -161,14 +161,16 @@ int main(int argc, char *argv[]) | |||
161 | 161 | ||
162 | setname(m, (uint8_t *)"Anon", 5); | 162 | setname(m, (uint8_t *)"Anon", 5); |
163 | 163 | ||
164 | char temp_id[128]; | 164 | char temp_hex_id[128]; |
165 | printf("\nEnter the address of the friend you wish to add (38 bytes HEX format):\n"); | 165 | printf("\nEnter the address of the friend you wish to add (38 bytes HEX format):\n"); |
166 | 166 | ||
167 | if (scanf("%s", temp_id) != 1) { | 167 | if (scanf("%s", temp_hex_id) != 1) { |
168 | return 1; | 168 | return 1; |
169 | } | 169 | } |
170 | 170 | ||
171 | int num = m_addfriend(m, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); | 171 | uint8_t *bin_id = hex_string_to_bin(temp_hex_id); |
172 | int num = m_addfriend(m, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); | ||
173 | free(bin_id); | ||
172 | 174 | ||
173 | perror("Initialization"); | 175 | perror("Initialization"); |
174 | 176 | ||
diff --git a/testing/experiment/group_chats_test1.c b/testing/experiment/group_chats_test1.c index a74f808a..fc10ab64 100644 --- a/testing/experiment/group_chats_test1.c +++ b/testing/experiment/group_chats_test1.c | |||
@@ -1,4 +1,6 @@ | |||
1 | #include "../../toxcore/group_chats.h" | 1 | #include "../../toxcore/group_chats.h" |
2 | #include "../misc_tools.c" // hex_string_to_bin | ||
3 | |||
2 | #define NUM_CHATS 8 | 4 | #define NUM_CHATS 8 |
3 | 5 | ||
4 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | 6 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) |
@@ -54,19 +56,6 @@ void print_group(Group_Chat *chat) | |||
54 | } | 56 | } |
55 | } | 57 | } |
56 | 58 | ||
57 | unsigned char *hex_string_to_bin(char hex_string[]) | ||
58 | { | ||
59 | size_t len = strlen(hex_string); | ||
60 | unsigned char *val = malloc(len); | ||
61 | char *pos = hex_string; | ||
62 | int i; | ||
63 | |||
64 | for (i = 0; i < len; ++i, pos += 2) | ||
65 | sscanf(pos, "%2hhx", &val[i]); | ||
66 | |||
67 | return val; | ||
68 | } | ||
69 | |||
70 | void print_message(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata) | 59 | void print_message(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata) |
71 | { | 60 | { |
72 | printf("%u: %s | %u\n", peer_number, message, length); | 61 | printf("%u: %s | %u\n", peer_number, message, length); |
@@ -95,8 +84,12 @@ int main(int argc, char *argv[]) | |||
95 | * bootstrap_ip_port.ip.c[2] = 0; | 84 | * bootstrap_ip_port.ip.c[2] = 0; |
96 | * bootstrap_ip_port.ip.c[3] = 1; */ | 85 | * bootstrap_ip_port.ip.c[3] = 1; */ |
97 | bootstrap_ip_port.ip.uint32 = inet_addr(argv[1]); | 86 | bootstrap_ip_port.ip.uint32 = inet_addr(argv[1]); |
87 | |||
88 | uint8_t *bootstrap_id = hex_string_to_bin(argv[3]); | ||
98 | 89 | ||
99 | chat_bootstrap(chat, bootstrap_ip_port, hex_string_to_bin(argv[3])); | 90 | chat_bootstrap(chat, bootstrap_ip_port, bootstrap_id); |
91 | |||
92 | free(bootstrap_id); | ||
100 | 93 | ||
101 | while (1) { | 94 | while (1) { |
102 | 95 | ||
diff --git a/testing/misc_tools.c b/testing/misc_tools.c index efe1a555..5e322a90 100644 --- a/testing/misc_tools.c +++ b/testing/misc_tools.c | |||
@@ -28,22 +28,28 @@ | |||
28 | #include <string.h> | 28 | #include <string.h> |
29 | #include <stdlib.h> | 29 | #include <stdlib.h> |
30 | #include <stdio.h> | 30 | #include <stdio.h> |
31 | #include <stdint.h> | ||
31 | 32 | ||
32 | #ifdef DEBUG | 33 | #ifdef DEBUG |
33 | #include <assert.h> | 34 | #include <assert.h> |
34 | #endif // DEBUG | 35 | #endif // DEBUG |
35 | 36 | ||
36 | /* TODO: rewrite */ | 37 | // You are responsible for freeing the return value! |
37 | unsigned char *hex_string_to_bin(char hex_string[]) | 38 | uint8_t *hex_string_to_bin(char *hex_string) |
38 | { | 39 | { |
39 | size_t i, len = strlen(hex_string); | 40 | // byte is represented by exactly 2 hex digits, so lenth of binary string |
40 | unsigned char *val = malloc(len); | 41 | // is half of that of the hex one. only hex string with even length |
42 | // valid. the more proper implementation would be to check if strlen(hex_string) | ||
43 | // is odd and return error code if it is. we assume strlen is even. if it's not | ||
44 | // then the last byte just won't be written in 'ret'. | ||
45 | size_t i, len = strlen(hex_string)/2; | ||
46 | uint8_t *ret = malloc(len); | ||
41 | char *pos = hex_string; | 47 | char *pos = hex_string; |
42 | 48 | ||
43 | for (i = 0; i < len; ++i, pos += 2) | 49 | for (i = 0; i < len; ++i, pos += 2) |
44 | sscanf(pos, "%2hhx", &val[i]); | 50 | sscanf(pos, "%2hhx", &ret[i]); |
45 | 51 | ||
46 | return val; | 52 | return ret; |
47 | } | 53 | } |
48 | 54 | ||
49 | int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled) | 55 | int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled) |
diff --git a/testing/tox_sync.c b/testing/tox_sync.c index ccb5d20d..5d38d2e5 100644 --- a/testing/tox_sync.c +++ b/testing/tox_sync.c | |||
@@ -221,6 +221,7 @@ int main(int argc, char *argv[]) | |||
221 | uint16_t port = htons(atoi(argv[argvoffset + 2])); | 221 | uint16_t port = htons(atoi(argv[argvoffset + 2])); |
222 | unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]); | 222 | unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]); |
223 | int res = tox_bootstrap_from_address(tox, argv[argvoffset + 1], ipv6enabled, port, binary_string); | 223 | int res = tox_bootstrap_from_address(tox, argv[argvoffset + 1], ipv6enabled, port, binary_string); |
224 | free(binary_string); | ||
224 | 225 | ||
225 | if (!res) { | 226 | if (!res) { |
226 | printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); | 227 | printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); |
@@ -242,7 +243,9 @@ int main(int argc, char *argv[]) | |||
242 | return 1; | 243 | return 1; |
243 | } | 244 | } |
244 | 245 | ||
245 | int num = tox_add_friend(tox, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); | 246 | uint8_t *bin_id = hex_string_to_bin(temp_id); |
247 | int num = tox_add_friend(tox, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); | ||
248 | free(bin_id); | ||
246 | 249 | ||
247 | if (num < 0) { | 250 | if (num < 0) { |
248 | printf("\nSomething went wrong when adding friend.\n"); | 251 | printf("\nSomething went wrong when adding friend.\n"); |