diff options
author | iphydf <iphydf@users.noreply.github.com> | 2019-01-12 13:12:39 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2019-01-12 13:12:39 +0000 |
commit | 91ff39599d97d610321970161ba6410f3bdd9b51 (patch) | |
tree | 30ada73995b857c9bde65a577427db4199426692 /toxcore | |
parent | 3dd31b1fd06a39a460e259ad525449b7f64e4a70 (diff) |
Implement all min/max functions for (un)signed int types.
Also, use them in the `onion_client` module.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/network.c | 4 | ||||
-rw-r--r-- | toxcore/onion_client.c | 18 | ||||
-rw-r--r-- | toxcore/util.c | 34 | ||||
-rw-r--r-- | toxcore/util.h | 15 |
4 files changed, 58 insertions, 13 deletions
diff --git a/toxcore/network.c b/toxcore/network.c index ca363efa..5e5160d8 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -433,13 +433,13 @@ static void loglogdata(const Logger *log, const char *message, const uint8_t *bu | |||
433 | int error = net_error(); | 433 | int error = net_error(); |
434 | const char *strerror = net_new_strerror(error); | 434 | const char *strerror = net_new_strerror(error); |
435 | LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x", | 435 | LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x", |
436 | buffer[0], message, (buflen < 999 ? buflen : 999), 'E', | 436 | buffer[0], message, min_u16(buflen, 999), 'E', |
437 | ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), error, | 437 | ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), error, |
438 | strerror, data_0(buflen, buffer), data_1(buflen, buffer)); | 438 | strerror, data_0(buflen, buffer), data_1(buflen, buffer)); |
439 | net_kill_strerror(strerror); | 439 | net_kill_strerror(strerror); |
440 | } else if ((res > 0) && ((size_t)res <= buflen)) { | 440 | } else if ((res > 0) && ((size_t)res <= buflen)) { |
441 | LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x", | 441 | LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x", |
442 | buffer[0], message, (res < 999 ? res : 999), ((size_t)res < buflen ? '<' : '='), | 442 | buffer[0], message, min_u16(res, 999), ((size_t)res < buflen ? '<' : '='), |
443 | ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), 0, "OK", | 443 | ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), 0, "OK", |
444 | data_0(buflen, buffer), data_1(buflen, buffer)); | 444 | data_0(buflen, buffer), data_1(buflen, buffer)); |
445 | } else { /* empty or overwrite */ | 445 | } else { /* empty or overwrite */ |
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 9528a041..d11a91e8 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c | |||
@@ -236,7 +236,7 @@ uint16_t onion_backup_nodes(const Onion_Client *onion_c, Node_format *nodes, uin | |||
236 | return 0; | 236 | return 0; |
237 | } | 237 | } |
238 | 238 | ||
239 | const uint16_t num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; | 239 | const uint16_t num_nodes = min_u16(onion_c->path_nodes_index, MAX_PATH_NODES); |
240 | uint16_t i = 0; | 240 | uint16_t i = 0; |
241 | 241 | ||
242 | while (i < max_num && i < num_nodes) { | 242 | while (i < max_num && i < num_nodes) { |
@@ -275,7 +275,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format | |||
275 | return 0; | 275 | return 0; |
276 | } | 276 | } |
277 | 277 | ||
278 | const uint32_t num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; | 278 | const uint16_t num_nodes = min_u16(onion_c->path_nodes_index, MAX_PATH_NODES); |
279 | 279 | ||
280 | // if (dht_non_lan_connected(onion_c->dht)) { | 280 | // if (dht_non_lan_connected(onion_c->dht)) { |
281 | if (dht_isconnected(onion_c->dht)) { | 281 | if (dht_isconnected(onion_c->dht)) { |
@@ -301,8 +301,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format | |||
301 | nodes[i] = onion_c->path_nodes[random_u32() % num_nodes]; | 301 | nodes[i] = onion_c->path_nodes[random_u32() % num_nodes]; |
302 | } | 302 | } |
303 | } else { | 303 | } else { |
304 | unsigned int num_nodes_bs = (onion_c->path_nodes_index_bs < MAX_PATH_NODES) ? onion_c->path_nodes_index_bs : | 304 | const uint16_t num_nodes_bs = min_u16(onion_c->path_nodes_index_bs, MAX_PATH_NODES); |
305 | MAX_PATH_NODES; | ||
306 | 305 | ||
307 | if (num_nodes_bs == 0) { | 306 | if (num_nodes_bs == 0) { |
308 | return 0; | 307 | return 0; |
@@ -1619,9 +1618,8 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum) | |||
1619 | } | 1618 | } |
1620 | 1619 | ||
1621 | if (count != MAX_ONION_CLIENTS) { | 1620 | if (count != MAX_ONION_CLIENTS) { |
1622 | unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; | 1621 | const uint16_t num_nodes = min_u16(onion_c->path_nodes_index, MAX_PATH_NODES); |
1623 | 1622 | uint16_t n = num_nodes; | |
1624 | unsigned int n = num_nodes; | ||
1625 | 1623 | ||
1626 | if (num_nodes > (MAX_ONION_CLIENTS / 2)) { | 1624 | if (num_nodes > (MAX_ONION_CLIENTS / 2)) { |
1627 | n = (MAX_ONION_CLIENTS / 2); | 1625 | n = (MAX_ONION_CLIENTS / 2); |
@@ -1740,14 +1738,14 @@ static void do_announce(Onion_Client *onion_c) | |||
1740 | } | 1738 | } |
1741 | 1739 | ||
1742 | if (count != MAX_ONION_CLIENTS_ANNOUNCE) { | 1740 | if (count != MAX_ONION_CLIENTS_ANNOUNCE) { |
1743 | unsigned int num_nodes; | 1741 | uint16_t num_nodes; |
1744 | Node_format *path_nodes; | 1742 | Node_format *path_nodes; |
1745 | 1743 | ||
1746 | if (random_u08() % 2 == 0 || onion_c->path_nodes_index == 0) { | 1744 | if (random_u08() % 2 == 0 || onion_c->path_nodes_index == 0) { |
1747 | num_nodes = (onion_c->path_nodes_index_bs < MAX_PATH_NODES) ? onion_c->path_nodes_index_bs : MAX_PATH_NODES; | 1745 | num_nodes = min_u16(onion_c->path_nodes_index_bs, MAX_PATH_NODES); |
1748 | path_nodes = onion_c->path_nodes_bs; | 1746 | path_nodes = onion_c->path_nodes_bs; |
1749 | } else { | 1747 | } else { |
1750 | num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; | 1748 | num_nodes = min_u16(onion_c->path_nodes_index, MAX_PATH_NODES); |
1751 | path_nodes = onion_c->path_nodes; | 1749 | path_nodes = onion_c->path_nodes; |
1752 | } | 1750 | } |
1753 | 1751 | ||
diff --git a/toxcore/util.c b/toxcore/util.c index adc0dc09..73e16c45 100644 --- a/toxcore/util.c +++ b/toxcore/util.c | |||
@@ -95,21 +95,53 @@ int create_recursive_mutex(pthread_mutex_t *mutex) | |||
95 | return 0; | 95 | return 0; |
96 | } | 96 | } |
97 | 97 | ||
98 | int16_t max_s16(int16_t a, int16_t b) | ||
99 | { | ||
100 | return a > b ? a : b; | ||
101 | } | ||
98 | int32_t max_s32(int32_t a, int32_t b) | 102 | int32_t max_s32(int32_t a, int32_t b) |
99 | { | 103 | { |
100 | return a > b ? a : b; | 104 | return a > b ? a : b; |
101 | } | 105 | } |
106 | int64_t max_s64(int64_t a, int64_t b) | ||
107 | { | ||
108 | return a > b ? a : b; | ||
109 | } | ||
102 | 110 | ||
111 | int16_t min_s16(int16_t a, int16_t b) | ||
112 | { | ||
113 | return a < b ? a : b; | ||
114 | } | ||
103 | int32_t min_s32(int32_t a, int32_t b) | 115 | int32_t min_s32(int32_t a, int32_t b) |
104 | { | 116 | { |
105 | return a < b ? a : b; | 117 | return a < b ? a : b; |
106 | } | 118 | } |
119 | int64_t min_s64(int64_t a, int64_t b) | ||
120 | { | ||
121 | return a < b ? a : b; | ||
122 | } | ||
123 | |||
124 | uint16_t max_u16(uint16_t a, uint16_t b) | ||
125 | { | ||
126 | return a > b ? a : b; | ||
127 | } | ||
128 | uint32_t max_u32(uint32_t a, uint32_t b) | ||
129 | { | ||
130 | return a > b ? a : b; | ||
131 | } | ||
132 | uint64_t max_u64(uint64_t a, uint64_t b) | ||
133 | { | ||
134 | return a > b ? a : b; | ||
135 | } | ||
107 | 136 | ||
137 | uint16_t min_u16(uint16_t a, uint16_t b) | ||
138 | { | ||
139 | return a < b ? a : b; | ||
140 | } | ||
108 | uint32_t min_u32(uint32_t a, uint32_t b) | 141 | uint32_t min_u32(uint32_t a, uint32_t b) |
109 | { | 142 | { |
110 | return a < b ? a : b; | 143 | return a < b ? a : b; |
111 | } | 144 | } |
112 | |||
113 | uint64_t min_u64(uint64_t a, uint64_t b) | 145 | uint64_t min_u64(uint64_t a, uint64_t b) |
114 | { | 146 | { |
115 | return a < b ? a : b; | 147 | return a < b ? a : b; |
diff --git a/toxcore/util.h b/toxcore/util.h index 85586724..79f5deb5 100644 --- a/toxcore/util.h +++ b/toxcore/util.h | |||
@@ -46,8 +46,23 @@ void net_to_host(uint8_t *num, uint16_t numbytes); | |||
46 | /* Returns -1 if failed or 0 if success */ | 46 | /* Returns -1 if failed or 0 if success */ |
47 | int create_recursive_mutex(pthread_mutex_t *mutex); | 47 | int create_recursive_mutex(pthread_mutex_t *mutex); |
48 | 48 | ||
49 | // Safe min/max functions with specific types. This forces the conversion to the | ||
50 | // desired type before the comparison expression, giving the choice of | ||
51 | // conversion to the caller. Use these instead of inline comparisons or MIN/MAX | ||
52 | // macros (effectively inline comparisons). | ||
53 | int16_t max_s16(int16_t a, int16_t b); | ||
49 | int32_t max_s32(int32_t a, int32_t b); | 54 | int32_t max_s32(int32_t a, int32_t b); |
55 | int64_t max_s64(int64_t a, int64_t b); | ||
56 | |||
57 | int16_t min_s16(int16_t a, int16_t b); | ||
50 | int32_t min_s32(int32_t a, int32_t b); | 58 | int32_t min_s32(int32_t a, int32_t b); |
59 | int64_t min_s64(int64_t a, int64_t b); | ||
60 | |||
61 | uint16_t max_u16(uint16_t a, uint16_t b); | ||
62 | uint32_t max_u32(uint32_t a, uint32_t b); | ||
63 | uint64_t max_u64(uint64_t a, uint64_t b); | ||
64 | |||
65 | uint16_t min_u16(uint16_t a, uint16_t b); | ||
51 | uint32_t min_u32(uint32_t a, uint32_t b); | 66 | uint32_t min_u32(uint32_t a, uint32_t b); |
52 | uint64_t min_u64(uint64_t a, uint64_t b); | 67 | uint64_t min_u64(uint64_t a, uint64_t b); |
53 | 68 | ||