cleanup: Return boolean constants, not ints from bool functions.

This commit is contained in:
iphydf 2022-02-22 16:44:39 +00:00
parent b7f95bbe70
commit 6be655c56c
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
11 changed files with 109 additions and 165 deletions

View File

@ -1 +1 @@
65261078cb22504cf84f16d65cbdb0bd3e297bd11f4e801d4127499aab283e96 /usr/local/bin/tox-bootstrapd 30dec481315934168e3b8abf54fe4ed5814161230bd499e0f935a4df1922dd33 /usr/local/bin/tox-bootstrapd

View File

@ -16,6 +16,7 @@ sh_test(
"-Wno-callback-names", "-Wno-callback-names",
"-Wno-enum-names", "-Wno-enum-names",
"-Wno-memcpy-structs", "-Wno-memcpy-structs",
"-Wno-type-check",
"+RTS", "+RTS",
"-N3", "-N3",
], ],

View File

@ -1113,7 +1113,7 @@ static bool is_pk_in_client_list(const Client_data *list, unsigned int client_li
const uint32_t index = index_of_client_pk(list, client_list_length, public_key); const uint32_t index = index_of_client_pk(list, client_list_length, public_key);
if (index == UINT32_MAX) { if (index == UINT32_MAX) {
return 0; return false;
} }
const IPPTsPng *assoc = net_family_is_ipv4(ip_port->ip.family) const IPPTsPng *assoc = net_family_is_ipv4(ip_port->ip.family)
@ -1355,11 +1355,7 @@ bool dht_getnodes(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, c
return false; return false;
} }
if (sendpacket(dht->net, ip_port, data, len) > 0) { return sendpacket(dht->net, ip_port, data, len) > 0;
return true;
}
return false;
} }
/** Send a send nodes response: message for IPv6 nodes */ /** Send a send nodes response: message for IPv6 nodes */
@ -1467,11 +1463,7 @@ static bool sent_getnode_to_node(DHT *dht, const uint8_t *public_key, const IP_P
return false; return false;
} }
if (!ipport_equal(&test.ip_port, node_ip_port) || !id_equal(test.public_key, public_key)) { return ipport_equal(&test.ip_port, node_ip_port) && id_equal(test.public_key, public_key);
return false;
}
return true;
} }
non_null() non_null()

View File

@ -271,11 +271,7 @@ bool ip_is_local(const IP *ip)
} }
/* localhost in IPv6 (::1) */ /* localhost in IPv6 (::1) */
if (ip->ip.v6.uint64[0] == 0 && ip->ip.v6.uint32[2] == 0 && ip->ip.v6.uint32[3] == net_htonl(1)) { return ip->ip.v6.uint64[0] == 0 && ip->ip.v6.uint32[2] == 0 && ip->ip.v6.uint32[3] == net_htonl(1);
return true;
}
return false;
} }
non_null() non_null()
@ -304,11 +300,7 @@ static bool ip4_is_lan(const IP4 *ip4)
/* RFC 6598: 100.64.0.0 to 100.127.255.255 (100.64.0.0/10) /* RFC 6598: 100.64.0.0 to 100.127.255.255 (100.64.0.0/10)
* (shared address space to stack another layer of NAT) */ * (shared address space to stack another layer of NAT) */
if ((ip4->uint8[0] == 100) && ((ip4->uint8[1] & 0xC0) == 0x40)) { return (ip4->uint8[0] == 100) && ((ip4->uint8[1] & 0xC0) == 0x40);
return true;
}
return false;
} }
bool ip_is_lan(const IP *ip) bool ip_is_lan(const IP *ip)

View File

@ -118,11 +118,7 @@ static bool connections_number_is_valid(const TCP_Connections *tcp_c, int connec
return false; return false;
} }
if (tcp_c->connections[connections_number].status == TCP_CONN_NONE) { return tcp_c->connections[connections_number].status != TCP_CONN_NONE;
return false;
}
return true;
} }
/** /**
@ -139,11 +135,7 @@ static bool tcp_connections_number_is_valid(const TCP_Connections *tcp_c, int tc
return false; return false;
} }
if (tcp_c->tcp_connections[tcp_connections_number].status == TCP_CONN_NONE) { return tcp_c->tcp_connections[tcp_connections_number].status != TCP_CONN_NONE;
return false;
}
return true;
} }
/** Create a new empty connection. /** Create a new empty connection.
@ -680,11 +672,11 @@ static bool tcp_connection_in_conn(const TCP_Connection_to *con_to, unsigned int
{ {
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) { for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
if (con_to->connections[i].tcp_connection == (tcp_connections_number + 1)) { if (con_to->connections[i].tcp_connection == (tcp_connections_number + 1)) {
return 1; return true;
} }
} }
return 0; return false;
} }
/** return index on success. /** return index on success.

View File

@ -10,6 +10,7 @@
*/ */
#include "crypto_core.h" #include "crypto_core.h"
#include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -34,7 +35,6 @@
//!TOKSTYLE- //!TOKSTYLE-
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
#include <assert.h>
#include "../testing/fuzzing/fuzz_adapter.h" #include "../testing/fuzzing/fuzz_adapter.h"
#endif #endif
//!TOKSTYLE+ //!TOKSTYLE+
@ -178,10 +178,10 @@ uint32_t random_range_u32(uint32_t upper_bound)
bool public_key_valid(const uint8_t *public_key) bool public_key_valid(const uint8_t *public_key)
{ {
if (public_key[31] >= 128) { /* Last bit of key is always zero. */ if (public_key[31] >= 128) { /* Last bit of key is always zero. */
return 0; return false;
} }
return 1; return true;
} }
int32_t encrypt_precompute(const uint8_t *public_key, const uint8_t *secret_key, int32_t encrypt_precompute(const uint8_t *public_key, const uint8_t *secret_key,
@ -238,7 +238,8 @@ int32_t encrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
crypto_free(temp_plain, size_temp_plain); crypto_free(temp_plain, size_temp_plain);
crypto_free(temp_encrypted, size_temp_encrypted); crypto_free(temp_encrypted, size_temp_encrypted);
#endif #endif
return length + crypto_box_MACBYTES; assert(length < INT32_MAX - crypto_box_MACBYTES);
return (int32_t)(length + crypto_box_MACBYTES);
} }
int32_t decrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce, int32_t decrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
@ -287,7 +288,9 @@ int32_t decrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
crypto_free(temp_plain, size_temp_plain); crypto_free(temp_plain, size_temp_plain);
crypto_free(temp_encrypted, size_temp_encrypted); crypto_free(temp_encrypted, size_temp_encrypted);
#endif #endif
return length - crypto_box_MACBYTES; assert(length > crypto_box_MACBYTES);
assert(length < INT32_MAX);
return (int32_t)(length - crypto_box_MACBYTES);
} }
int32_t encrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *nonce, int32_t encrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *nonce,

View File

@ -1441,7 +1441,7 @@ static bool send_packet_group_peer(const Friend_Connections *fr_c, int friendcon
uint16_t group_num, const uint8_t *data, uint16_t length) uint16_t group_num, const uint8_t *data, uint16_t length)
{ {
if (1 + sizeof(uint16_t) + length > MAX_CRYPTO_DATA_SIZE) { if (1 + sizeof(uint16_t) + length > MAX_CRYPTO_DATA_SIZE) {
return 0; return false;
} }
group_num = net_htons(group_num); group_num = net_htons(group_num);
@ -1463,7 +1463,7 @@ static bool send_lossy_group_peer(const Friend_Connections *fr_c, int friendcon_
uint16_t group_num, const uint8_t *data, uint16_t length) uint16_t group_num, const uint8_t *data, uint16_t length)
{ {
if (1 + sizeof(uint16_t) + length > MAX_CRYPTO_DATA_SIZE) { if (1 + sizeof(uint16_t) + length > MAX_CRYPTO_DATA_SIZE) {
return 0; return false;
} }
group_num = net_htons(group_num); group_num = net_htons(group_num);
@ -1757,11 +1757,7 @@ static int send_message_group(const Group_Chats *g_c, uint32_t groupnumber, uint
non_null() non_null()
static bool group_ping_send(const Group_Chats *g_c, uint32_t groupnumber) static bool group_ping_send(const Group_Chats *g_c, uint32_t groupnumber)
{ {
if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_PING_ID, nullptr, 0) > 0) { return send_message_group(g_c, groupnumber, GROUP_MESSAGE_PING_ID, nullptr, 0) > 0;
return true;
}
return false;
} }
/** send a new_peer message /** send a new_peer message
@ -1778,11 +1774,7 @@ static bool group_new_peer_send(const Group_Chats *g_c, uint32_t groupnumber, ui
memcpy(packet + sizeof(uint16_t), real_pk, CRYPTO_PUBLIC_KEY_SIZE); memcpy(packet + sizeof(uint16_t), real_pk, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(packet + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE, temp_pk, CRYPTO_PUBLIC_KEY_SIZE); memcpy(packet + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE, temp_pk, CRYPTO_PUBLIC_KEY_SIZE);
if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_NEW_PEER_ID, packet, sizeof(packet)) > 0) { return send_message_group(g_c, groupnumber, GROUP_MESSAGE_NEW_PEER_ID, packet, sizeof(packet)) > 0;
return true;
}
return false;
} }
/** send a kill_peer message /** send a kill_peer message
@ -1796,11 +1788,7 @@ static bool group_kill_peer_send(const Group_Chats *g_c, uint32_t groupnumber, u
peer_num = net_htons(peer_num); peer_num = net_htons(peer_num);
memcpy(packet, &peer_num, sizeof(uint16_t)); memcpy(packet, &peer_num, sizeof(uint16_t));
if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_KILL_PEER_ID, packet, sizeof(packet)) > 0) { return send_message_group(g_c, groupnumber, GROUP_MESSAGE_KILL_PEER_ID, packet, sizeof(packet)) > 0;
return true;
}
return false;
} }
/** send a freeze_peer message /** send a freeze_peer message
@ -1814,11 +1802,7 @@ static bool group_freeze_peer_send(const Group_Chats *g_c, uint32_t groupnumber,
peer_num = net_htons(peer_num); peer_num = net_htons(peer_num);
memcpy(packet, &peer_num, sizeof(uint16_t)); memcpy(packet, &peer_num, sizeof(uint16_t));
if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_FREEZE_PEER_ID, packet, sizeof(packet)) > 0) { return send_message_group(g_c, groupnumber, GROUP_MESSAGE_FREEZE_PEER_ID, packet, sizeof(packet)) > 0;
return true;
}
return false;
} }
/** send a name message /** send a name message
@ -1831,11 +1815,7 @@ static bool group_name_send(const Group_Chats *g_c, uint32_t groupnumber, const
return false; return false;
} }
if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_NAME_ID, nick, nick_len) > 0) { return send_message_group(g_c, groupnumber, GROUP_MESSAGE_NAME_ID, nick, nick_len) > 0;
return true;
}
return false;
} }
/** send message to announce leaving group /** send message to announce leaving group

View File

@ -188,11 +188,7 @@ static bool crypt_connection_id_is_valid(const Net_Crypto *c, int crypt_connecti
const Crypto_Conn_State status = c->crypto_connections[crypt_connection_id].status; const Crypto_Conn_State status = c->crypto_connections[crypt_connection_id].status;
if (status == CRYPTO_CONN_NO_CONNECTION || status == CRYPTO_CONN_FREE) { return status != CRYPTO_CONN_NO_CONNECTION && status != CRYPTO_CONN_FREE;
return false;
}
return true;
} }
/** cookie timeout in seconds */ /** cookie timeout in seconds */

View File

@ -416,7 +416,7 @@ non_null()
static bool path_exists(const Mono_Time *mono_time, const Onion_Client_Paths *onion_paths, uint32_t path_num) static bool path_exists(const Mono_Time *mono_time, const Onion_Client_Paths *onion_paths, uint32_t path_num)
{ {
if (path_timed_out(mono_time, onion_paths, path_num)) { if (path_timed_out(mono_time, onion_paths, path_num)) {
return 0; return false;
} }
return onion_paths->paths[path_num % NUMBER_ONION_PATHS].path_num == path_num; return onion_paths->paths[path_num % NUMBER_ONION_PATHS].path_num == path_num;

View File

@ -765,12 +765,12 @@ bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *pub
if (host == nullptr || public_key == nullptr) { if (host == nullptr || public_key == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_NULL); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_NULL);
return 0; return false;
} }
if (port == 0) { if (port == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT);
return 0; return false;
} }
IP_Port *root; IP_Port *root;
@ -781,7 +781,7 @@ bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *pub
LOGGER_DEBUG(tox->m->log, "could not resolve bootstrap node '%s'", host); LOGGER_DEBUG(tox->m->log, "could not resolve bootstrap node '%s'", host);
net_freeipport(root); net_freeipport(root);
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST);
return 0; return false;
} }
lock(tox); lock(tox);
@ -803,12 +803,12 @@ bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *pub
if (count > 0) { if (count > 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK);
return 1; return true;
} }
LOGGER_DEBUG(tox->m->log, "bootstrap node '%s' resolved to 0 IP_Ports", host); LOGGER_DEBUG(tox->m->log, "bootstrap node '%s' resolved to 0 IP_Ports", host);
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST);
return 0; return false;
} }
bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t *public_key, bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t *public_key,
@ -818,12 +818,12 @@ bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t
if (host == nullptr || public_key == nullptr) { if (host == nullptr || public_key == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_NULL); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_NULL);
return 0; return false;
} }
if (port == 0) { if (port == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT);
return 0; return false;
} }
IP_Port *root; IP_Port *root;
@ -833,7 +833,7 @@ bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t
if (count == -1) { if (count == -1) {
net_freeipport(root); net_freeipport(root);
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST);
return 0; return false;
} }
lock(tox); lock(tox);
@ -851,11 +851,11 @@ bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t
if (count > 0) { if (count > 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK);
return 1; return true;
} }
SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST);
return 0; return false;
} }
Tox_Connection tox_self_get_connection_status(const Tox *tox) Tox_Connection tox_self_get_connection_status(const Tox *tox)
@ -970,7 +970,7 @@ bool tox_self_set_name(Tox *tox, const uint8_t *name, size_t length, Tox_Err_Set
if (name == nullptr && length != 0) { if (name == nullptr && length != 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_NULL); SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_NULL);
return 0; return false;
} }
lock(tox); lock(tox);
@ -980,12 +980,12 @@ bool tox_self_set_name(Tox *tox, const uint8_t *name, size_t length, Tox_Err_Set
send_name_all_groups(tox->m->conferences_object); send_name_all_groups(tox->m->conferences_object);
SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_OK); SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_OK);
unlock(tox); unlock(tox);
return 1; return true;
} }
SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_TOO_LONG); SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_TOO_LONG);
unlock(tox); unlock(tox);
return 0; return false;
} }
size_t tox_self_get_name_size(const Tox *tox) size_t tox_self_get_name_size(const Tox *tox)
@ -1014,7 +1014,7 @@ bool tox_self_set_status_message(Tox *tox, const uint8_t *status_message, size_t
if (status_message == nullptr && length != 0) { if (status_message == nullptr && length != 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_NULL); SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_NULL);
return 0; return false;
} }
lock(tox); lock(tox);
@ -1022,12 +1022,12 @@ bool tox_self_set_status_message(Tox *tox, const uint8_t *status_message, size_t
if (m_set_statusmessage(tox->m, status_message, length) == 0) { if (m_set_statusmessage(tox->m, status_message, length) == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_OK); SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_OK);
unlock(tox); unlock(tox);
return 1; return true;
} }
SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_TOO_LONG); SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_TOO_LONG);
unlock(tox); unlock(tox);
return 0; return false;
} }
size_t tox_self_get_status_message_size(const Tox *tox) size_t tox_self_get_status_message_size(const Tox *tox)
@ -1171,11 +1171,11 @@ bool tox_friend_delete(Tox *tox, uint32_t friend_number, Tox_Err_Friend_Delete *
// TODO(irungentoo): handle if realloc fails? // TODO(irungentoo): handle if realloc fails?
if (ret == -1) { if (ret == -1) {
SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_DELETE_FRIEND_NOT_FOUND); SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_DELETE_FRIEND_NOT_FOUND);
return 0; return false;
} }
SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_DELETE_OK); SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_DELETE_OK);
return 1; return true;
} }
uint32_t tox_friend_by_public_key(const Tox *tox, const uint8_t *public_key, Tox_Err_Friend_By_Public_Key *error) uint32_t tox_friend_by_public_key(const Tox *tox, const uint8_t *public_key, Tox_Err_Friend_By_Public_Key *error)
@ -1206,7 +1206,7 @@ bool tox_friend_get_public_key(const Tox *tox, uint32_t friend_number, uint8_t *
assert(tox != nullptr); assert(tox != nullptr);
if (public_key == nullptr) { if (public_key == nullptr) {
return 0; return false;
} }
lock(tox); lock(tox);
@ -1214,12 +1214,12 @@ bool tox_friend_get_public_key(const Tox *tox, uint32_t friend_number, uint8_t *
if (get_real_pk(tox->m, friend_number, public_key) == -1) { if (get_real_pk(tox->m, friend_number, public_key) == -1) {
SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_GET_PUBLIC_KEY_FRIEND_NOT_FOUND); SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_GET_PUBLIC_KEY_FRIEND_NOT_FOUND);
unlock(tox); unlock(tox);
return 0; return false;
} }
SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK); SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK);
unlock(tox); unlock(tox);
return 1; return true;
} }
bool tox_friend_exists(const Tox *tox, uint32_t friend_number) bool tox_friend_exists(const Tox *tox, uint32_t friend_number)
@ -1290,7 +1290,7 @@ bool tox_friend_get_name(const Tox *tox, uint32_t friend_number, uint8_t *name,
if (name == nullptr) { if (name == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_NULL); SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_NULL);
return 0; return false;
} }
lock(tox); lock(tox);
@ -1299,11 +1299,11 @@ bool tox_friend_get_name(const Tox *tox, uint32_t friend_number, uint8_t *name,
if (ret == -1) { if (ret == -1) {
SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_FRIEND_NOT_FOUND); SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_FRIEND_NOT_FOUND);
return 0; return false;
} }
SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_OK); SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_OK);
return 1; return true;
} }
void tox_callback_friend_name(Tox *tox, tox_friend_name_cb *callback) void tox_callback_friend_name(Tox *tox, tox_friend_name_cb *callback)
@ -1414,7 +1414,7 @@ bool tox_friend_get_typing(const Tox *tox, uint32_t friend_number, Tox_Err_Frien
if (ret == -1) { if (ret == -1) {
SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_FRIEND_NOT_FOUND); SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_FRIEND_NOT_FOUND);
return 0; return false;
} }
SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_OK); SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_OK);
@ -1435,12 +1435,12 @@ bool tox_self_set_typing(Tox *tox, uint32_t friend_number, bool typing, Tox_Err_
if (m_set_usertyping(tox->m, friend_number, typing) == -1) { if (m_set_usertyping(tox->m, friend_number, typing) == -1) {
SET_ERROR_PARAMETER(error, TOX_ERR_SET_TYPING_FRIEND_NOT_FOUND); SET_ERROR_PARAMETER(error, TOX_ERR_SET_TYPING_FRIEND_NOT_FOUND);
unlock(tox); unlock(tox);
return 0; return false;
} }
SET_ERROR_PARAMETER(error, TOX_ERR_SET_TYPING_OK); SET_ERROR_PARAMETER(error, TOX_ERR_SET_TYPING_OK);
unlock(tox); unlock(tox);
return 1; return true;
} }
non_null(1) nullable(3) non_null(1) nullable(3)
@ -1529,11 +1529,11 @@ void tox_callback_friend_message(Tox *tox, tox_friend_message_cb *callback)
bool tox_hash(uint8_t *hash, const uint8_t *data, size_t length) bool tox_hash(uint8_t *hash, const uint8_t *data, size_t length)
{ {
if (hash == nullptr || (data == nullptr && length != 0)) { if (hash == nullptr || (data == nullptr && length != 0)) {
return 0; return false;
} }
crypto_sha256(hash, data, length); crypto_sha256(hash, data, length);
return 1; return true;
} }
bool tox_file_control(Tox *tox, uint32_t friend_number, uint32_t file_number, Tox_File_Control control, bool tox_file_control(Tox *tox, uint32_t friend_number, uint32_t file_number, Tox_File_Control control,
@ -1546,56 +1546,56 @@ bool tox_file_control(Tox *tox, uint32_t friend_number, uint32_t file_number, To
if (ret == 0) { if (ret == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_OK); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_OK);
return 1; return true;
} }
switch (ret) { switch (ret) {
case -1: { case -1: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_FRIEND_NOT_FOUND); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_FRIEND_NOT_FOUND);
return 0; return false;
} }
case -2: { case -2: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_FRIEND_NOT_CONNECTED); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_FRIEND_NOT_CONNECTED);
return 0; return false;
} }
case -3: { case -3: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_NOT_FOUND); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_NOT_FOUND);
return 0; return false;
} }
case -4: { case -4: {
/* can't happen (this code is returned if `control` is invalid type) */ /* can't happen (this code is returned if `control` is invalid type) */
LOGGER_FATAL(tox->m->log, "impossible return value: %d", ret); LOGGER_FATAL(tox->m->log, "impossible return value: %d", ret);
return 0; return false;
} }
case -5: { case -5: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_ALREADY_PAUSED); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_ALREADY_PAUSED);
return 0; return false;
} }
case -6: { case -6: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_DENIED); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_DENIED);
return 0; return false;
} }
case -7: { case -7: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_NOT_PAUSED); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_NOT_PAUSED);
return 0; return false;
} }
case -8: { case -8: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_SENDQ); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_SENDQ);
return 0; return false;
} }
} }
/* can't happen */ /* can't happen */
LOGGER_FATAL(tox->m->log, "impossible return value: %d", ret); LOGGER_FATAL(tox->m->log, "impossible return value: %d", ret);
return 0; return false;
} }
bool tox_file_seek(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, bool tox_file_seek(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position,
@ -1608,46 +1608,46 @@ bool tox_file_seek(Tox *tox, uint32_t friend_number, uint32_t file_number, uint6
if (ret == 0) { if (ret == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_OK); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_OK);
return 1; return true;
} }
switch (ret) { switch (ret) {
case -1: { case -1: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_FRIEND_NOT_FOUND); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_FRIEND_NOT_FOUND);
return 0; return false;
} }
case -2: { case -2: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_FRIEND_NOT_CONNECTED); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_FRIEND_NOT_CONNECTED);
return 0; return false;
} }
case -3: { case -3: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_NOT_FOUND); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_NOT_FOUND);
return 0; return false;
} }
case -4: // fall-through case -4: // fall-through
case -5: { case -5: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_DENIED); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_DENIED);
return 0; return false;
} }
case -6: { case -6: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_INVALID_POSITION); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_INVALID_POSITION);
return 0; return false;
} }
case -8: { case -8: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_SENDQ); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_SENDQ);
return 0; return false;
} }
} }
/* can't happen */ /* can't happen */
LOGGER_FATAL(tox->m->log, "impossible return value: %d", ret); LOGGER_FATAL(tox->m->log, "impossible return value: %d", ret);
return 0; return false;
} }
void tox_callback_file_recv_control(Tox *tox, tox_file_recv_control_cb *callback) void tox_callback_file_recv_control(Tox *tox, tox_file_recv_control_cb *callback)
@ -1663,7 +1663,7 @@ bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_
if (file_id == nullptr) { if (file_id == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_GET_NULL); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_GET_NULL);
return 0; return false;
} }
lock(tox); lock(tox);
@ -1672,7 +1672,7 @@ bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_
if (ret == 0) { if (ret == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_GET_OK); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_GET_OK);
return 1; return true;
} }
if (ret == -1) { if (ret == -1) {
@ -1681,7 +1681,7 @@ bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_GET_NOT_FOUND); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_GET_NOT_FOUND);
} }
return 0; return false;
} }
uint32_t tox_file_send(Tox *tox, uint32_t friend_number, uint32_t kind, uint64_t file_size, const uint8_t *file_id, uint32_t tox_file_send(Tox *tox, uint32_t friend_number, uint32_t kind, uint64_t file_size, const uint8_t *file_id,
@ -1749,50 +1749,50 @@ bool tox_file_send_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number,
if (ret == 0) { if (ret == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_OK); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_OK);
return 1; return true;
} }
switch (ret) { switch (ret) {
case -1: { case -1: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_FRIEND_NOT_FOUND); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_FRIEND_NOT_FOUND);
return 0; return false;
} }
case -2: { case -2: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_FRIEND_NOT_CONNECTED); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_FRIEND_NOT_CONNECTED);
return 0; return false;
} }
case -3: { case -3: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_NOT_FOUND); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_NOT_FOUND);
return 0; return false;
} }
case -4: { case -4: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_NOT_TRANSFERRING); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_NOT_TRANSFERRING);
return 0; return false;
} }
case -5: { case -5: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_INVALID_LENGTH); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_INVALID_LENGTH);
return 0; return false;
} }
case -6: { case -6: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_SENDQ); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_SENDQ);
return 0; return false;
} }
case -7: { case -7: {
SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_WRONG_POSITION); SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_WRONG_POSITION);
return 0; return false;
} }
} }
/* can't happen */ /* can't happen */
LOGGER_FATAL(tox->m->log, "impossible return value: %d", ret); LOGGER_FATAL(tox->m->log, "impossible return value: %d", ret);
return 0; return false;
} }
void tox_callback_file_chunk_request(Tox *tox, tox_file_chunk_request_cb *callback) void tox_callback_file_chunk_request(Tox *tox, tox_file_chunk_request_cb *callback)
@ -1995,7 +1995,7 @@ bool tox_conference_peer_number_is_ours(const Tox *tox, uint32_t conference_numb
} }
SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_PEER_QUERY_OK); SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_PEER_QUERY_OK);
return ret; return ret != 0;
} }
uint32_t tox_conference_offline_peer_count(const Tox *tox, uint32_t conference_number, uint32_t tox_conference_offline_peer_count(const Tox *tox, uint32_t conference_number,
@ -2468,17 +2468,17 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_
if (data == nullptr) { if (data == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_NULL); SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_NULL);
return 0; return false;
} }
if (length == 0) { if (length == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_EMPTY); SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_EMPTY);
return 0; return false;
} }
if (data[0] < PACKET_ID_RANGE_LOSSY_START || data[0] > PACKET_ID_RANGE_LOSSY_END) { if (data[0] < PACKET_ID_RANGE_LOSSY_START || data[0] > PACKET_ID_RANGE_LOSSY_END) {
SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_INVALID); SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_INVALID);
return 0; return false;
} }
lock(tox); lock(tox);
@ -2487,11 +2487,7 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_
set_custom_packet_error(ret, error); set_custom_packet_error(ret, error);
if (ret == 0) { return ret == 0;
return 1;
}
return 0;
} }
void tox_callback_friend_lossy_packet(Tox *tox, tox_friend_lossy_packet_cb *callback) void tox_callback_friend_lossy_packet(Tox *tox, tox_friend_lossy_packet_cb *callback)
@ -2520,12 +2516,12 @@ bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uin
if (data == nullptr) { if (data == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_NULL); SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_NULL);
return 0; return false;
} }
if (length == 0) { if (length == 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_EMPTY); SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_EMPTY);
return 0; return false;
} }
lock(tox); lock(tox);
@ -2534,11 +2530,7 @@ bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uin
set_custom_packet_error(ret, error); set_custom_packet_error(ret, error);
if (ret == 0) { return ret == 0;
return 1;
}
return 0;
} }
void tox_callback_friend_lossless_packet(Tox *tox, tox_friend_lossless_packet_cb *callback) void tox_callback_friend_lossless_packet(Tox *tox, tox_friend_lossless_packet_cb *callback)

View File

@ -166,7 +166,7 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *plaintext, siz
{ {
if (plaintext_len == 0 || plaintext == nullptr || key == nullptr || ciphertext == nullptr) { if (plaintext_len == 0 || plaintext == nullptr || key == nullptr || ciphertext == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_NULL); SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_NULL);
return 0; return false;
} }
// the output data consists of, in order: // the output data consists of, in order:
@ -193,11 +193,11 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *plaintext, siz
if (encrypt_data_symmetric(key->key, nonce, plaintext, plaintext_len, ciphertext) if (encrypt_data_symmetric(key->key, nonce, plaintext, plaintext_len, ciphertext)
!= plaintext_len + crypto_box_MACBYTES) { != plaintext_len + crypto_box_MACBYTES) {
SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_FAILED); SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_FAILED);
return 0; return false;
} }
SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_OK); SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_OK);
return 1; return true;
} }
/* Encrypts the given data with the given passphrase. The output array must be /* Encrypts the given data with the given passphrase. The output array must be
@ -219,7 +219,7 @@ bool tox_pass_encrypt(const uint8_t *plaintext, size_t plaintext_len, const uint
SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_KEY_DERIVATION_FAILED); SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_KEY_DERIVATION_FAILED);
} }
return 0; return false;
} }
bool result = tox_pass_key_encrypt(key, plaintext, plaintext_len, ciphertext, error); bool result = tox_pass_key_encrypt(key, plaintext, plaintext_len, ciphertext, error);
@ -239,17 +239,17 @@ bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t *ciphertext, si
{ {
if (length <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) { if (length <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) {
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_INVALID_LENGTH); SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_INVALID_LENGTH);
return 0; return false;
} }
if (ciphertext == nullptr || key == nullptr || plaintext == nullptr) { if (ciphertext == nullptr || key == nullptr || plaintext == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_NULL); SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_NULL);
return 0; return false;
} }
if (memcmp(ciphertext, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0) { if (memcmp(ciphertext, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_BAD_FORMAT); SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_BAD_FORMAT);
return 0; return false;
} }
ciphertext += TOX_ENC_SAVE_MAGIC_LENGTH; ciphertext += TOX_ENC_SAVE_MAGIC_LENGTH;
@ -265,11 +265,11 @@ bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t *ciphertext, si
if (decrypt_data_symmetric(key->key, nonce, ciphertext, decrypt_length + crypto_box_MACBYTES, plaintext) if (decrypt_data_symmetric(key->key, nonce, ciphertext, decrypt_length + crypto_box_MACBYTES, plaintext)
!= decrypt_length) { != decrypt_length) {
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_FAILED); SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_FAILED);
return 0; return false;
} }
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_OK); SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_OK);
return 1; return true;
} }
/* Decrypts the given data with the given passphrase. The output array must be /* Decrypts the given data with the given passphrase. The output array must be
@ -285,17 +285,17 @@ bool tox_pass_decrypt(const uint8_t *ciphertext, size_t ciphertext_len, const ui
{ {
if (ciphertext_len <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) { if (ciphertext_len <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) {
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_INVALID_LENGTH); SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_INVALID_LENGTH);
return 0; return false;
} }
if (ciphertext == nullptr || passphrase == nullptr || plaintext == nullptr) { if (ciphertext == nullptr || passphrase == nullptr || plaintext == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_NULL); SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_NULL);
return 0; return false;
} }
if (memcmp(ciphertext, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0) { if (memcmp(ciphertext, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_BAD_FORMAT); SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_BAD_FORMAT);
return 0; return false;
} }
uint8_t salt[crypto_pwhash_scryptsalsa208sha256_SALTBYTES]; uint8_t salt[crypto_pwhash_scryptsalsa208sha256_SALTBYTES];
@ -307,7 +307,7 @@ bool tox_pass_decrypt(const uint8_t *ciphertext, size_t ciphertext_len, const ui
if (key == nullptr) { if (key == nullptr) {
/* out of memory most likely */ /* out of memory most likely */
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_KEY_DERIVATION_FAILED); SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_KEY_DERIVATION_FAILED);
return 0; return false;
} }
bool result = tox_pass_key_decrypt(key, ciphertext, ciphertext_len, plaintext, error); bool result = tox_pass_key_decrypt(key, ciphertext, ciphertext_len, plaintext, error);
@ -319,9 +319,5 @@ bool tox_pass_decrypt(const uint8_t *ciphertext, size_t ciphertext_len, const ui
*/ */
bool tox_is_data_encrypted(const uint8_t *data) bool tox_is_data_encrypted(const uint8_t *data)
{ {
if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) { return memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0;
return 1;
}
return 0;
} }