diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 945d655676..93ed6495ca 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -28,16 +28,14 @@ static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_ uint32_t length, uint8_t congestion_control); static void m_register_default_plugins(Messenger *m); -// friend_not_valid determines if the friendnumber passed is valid in the Messenger object -static uint8_t friend_not_valid(const Messenger *m, int32_t friendnumber) +/** + * Determines if the friendnumber passed is valid in the Messenger object. + * + * @param friendnumber The index in the friend list. + */ +static bool friend_is_valid(const Messenger *m, int32_t friendnumber) { - if ((unsigned int)friendnumber < m->numfriends) { - if (m->friendlist[friendnumber].status != 0) { - return 0; - } - } - - return 1; + return (unsigned int)friendnumber < m->numfriends && m->friendlist[friendnumber].status != 0; } /* Set the size of the friend list to numfriends. @@ -88,7 +86,7 @@ int32_t getfriend_id(const Messenger *m, const uint8_t *real_pk) */ int get_real_pk(const Messenger *m, int32_t friendnumber, uint8_t *real_pk) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -101,7 +99,7 @@ int get_real_pk(const Messenger *m, int32_t friendnumber, uint8_t *real_pk) */ int getfriendcon_id(const Messenger *m, int32_t friendnumber) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -140,7 +138,7 @@ void getaddress(const Messenger *m, uint8_t *address) static int send_online_packet(Messenger *m, int32_t friendnumber) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return 0; } @@ -301,7 +299,7 @@ int32_t m_addfriend_norequest(Messenger *m, const uint8_t *real_pk) static int clear_receipts(Messenger *m, int32_t friendnumber) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -320,7 +318,7 @@ static int clear_receipts(Messenger *m, int32_t friendnumber) static int add_receipt(Messenger *m, int32_t friendnumber, uint32_t packet_num, uint32_t msg_id) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -349,7 +347,7 @@ static int add_receipt(Messenger *m, int32_t friendnumber, uint32_t packet_num, */ static int friend_received_packet(const Messenger *m, int32_t friendnumber, uint32_t number) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -359,7 +357,7 @@ static int friend_received_packet(const Messenger *m, int32_t friendnumber, uint static int do_receipts(Messenger *m, int32_t friendnumber, void *userdata) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -397,7 +395,7 @@ static int do_receipts(Messenger *m, int32_t friendnumber, void *userdata) */ int m_delfriend(Messenger *m, int32_t friendnumber) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -435,7 +433,7 @@ int m_delfriend(Messenger *m, int32_t friendnumber) int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -463,7 +461,7 @@ int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber) int m_friend_exists(const Messenger *m, int32_t friendnumber) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return 0; } @@ -487,7 +485,7 @@ int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, con return -5; } - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { LOGGER_ERROR(m->log, "Friend number %d is invalid", friendnumber); return -1; } @@ -548,7 +546,7 @@ static int m_sendname(const Messenger *m, int32_t friendnumber, const uint8_t *n */ int setfriendname(Messenger *m, int32_t friendnumber, const uint8_t *name, uint16_t length) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -617,7 +615,7 @@ uint16_t getself_name(const Messenger *m, uint8_t *name) */ int getname(const Messenger *m, int32_t friendnumber, uint8_t *name) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -627,7 +625,7 @@ int getname(const Messenger *m, int32_t friendnumber, uint8_t *name) int m_get_name_size(const Messenger *m, int32_t friendnumber) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -689,7 +687,7 @@ int m_set_userstatus(Messenger *m, uint8_t status) */ int m_get_statusmessage_size(const Messenger *m, int32_t friendnumber) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -701,7 +699,7 @@ int m_get_statusmessage_size(const Messenger *m, int32_t friendnumber) */ int m_copy_statusmessage(const Messenger *m, int32_t friendnumber, uint8_t *buf, uint32_t maxlen) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -730,7 +728,7 @@ int m_copy_self_statusmessage(const Messenger *m, uint8_t *buf) uint8_t m_get_userstatus(const Messenger *m, int32_t friendnumber) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return USERSTATUS_INVALID; } @@ -750,7 +748,7 @@ uint8_t m_get_self_userstatus(const Messenger *m) uint64_t m_get_last_online(const Messenger *m, int32_t friendnumber) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return UINT64_MAX; } @@ -763,7 +761,7 @@ int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing) return -1; } - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -779,7 +777,7 @@ int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing) int m_get_istyping(const Messenger *m, int32_t friendnumber) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -804,7 +802,7 @@ static int send_user_istyping(const Messenger *m, int32_t friendnumber, uint8_t static int set_friend_statusmessage(const Messenger *m, int32_t friendnumber, const uint8_t *status, uint16_t length) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -952,7 +950,7 @@ static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_t packet_id, const uint8_t *data, uint32_t length, uint8_t congestion_control) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return 0; } @@ -1033,7 +1031,7 @@ void callback_file_reqchunk(Messenger *m, m_file_chunk_request_cb *function) */ int file_get_id(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint8_t *file_id) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -1082,7 +1080,7 @@ int file_get_id(const Messenger *m, int32_t friendnumber, uint32_t filenumber, u static int file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t filenumber, uint32_t file_type, uint64_t filesize, const uint8_t *file_id, const uint8_t *filename, uint16_t filename_length) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return 0; } @@ -1116,7 +1114,7 @@ static int file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t fi long int new_filesender(const Messenger *m, int32_t friendnumber, uint32_t file_type, uint64_t filesize, const uint8_t *file_id, const uint8_t *filename, uint16_t filename_length) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -1195,7 +1193,7 @@ static int send_file_control_packet(const Messenger *m, int32_t friendnumber, ui */ int file_control(const Messenger *m, int32_t friendnumber, uint32_t filenumber, unsigned int control) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -1296,7 +1294,7 @@ int file_control(const Messenger *m, int32_t friendnumber, uint32_t filenumber, */ int file_seek(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint64_t position) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -1352,7 +1350,7 @@ int file_seek(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uin static int64_t send_file_data_packet(const Messenger *m, int32_t friendnumber, uint8_t filenumber, const uint8_t *data, uint16_t length) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -1384,7 +1382,7 @@ static int64_t send_file_data_packet(const Messenger *m, int32_t friendnumber, u int file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint64_t position, const uint8_t *data, uint16_t length) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -1455,7 +1453,7 @@ int file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uin */ uint64_t file_dataremaining(const Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return 0; } @@ -1776,7 +1774,7 @@ static int m_handle_lossy_packet(void *object, int friend_num, const uint8_t *pa { Messenger *m = (Messenger *)object; - if (friend_not_valid(m, friend_num)) { + if (!friend_is_valid(m, friend_num)) { return 1; } @@ -1806,7 +1804,7 @@ void custom_lossy_packet_registerhandler(Messenger *m, m_friend_lossy_packet_cb int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, m_lossy_rtp_packet_cb *function, void *object) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -1829,7 +1827,7 @@ int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, m_lo */ int m_send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } @@ -1859,7 +1857,7 @@ static int handle_custom_lossless_packet(void *object, int friend_num, const uin { Messenger *m = (Messenger *)object; - if (friend_not_valid(m, friend_num)) { + if (!friend_is_valid(m, friend_num)) { return -1; } @@ -1881,7 +1879,7 @@ void custom_lossless_packet_registerhandler(Messenger *m, m_friend_lossless_pack int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length) { - if (friend_not_valid(m, friendnumber)) { + if (!friend_is_valid(m, friendnumber)) { return -1; } diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index 3dd8652ead..adef31751f 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -100,44 +100,44 @@ static int realloc_TCP_con(TCP_con **array, size_t num) } -/* return 1 if the connections_number is not valid. - * return 0 if the connections_number is valid. +/** + * Return true if the connections_number is valid. */ -static bool connections_number_not_valid(const TCP_Connections *tcp_c, int connections_number) +static bool connections_number_is_valid(const TCP_Connections *tcp_c, int connections_number) { if ((unsigned int)connections_number >= tcp_c->connections_length) { - return 1; + return false; } if (tcp_c->connections == nullptr) { - return 1; + return false; } if (tcp_c->connections[connections_number].status == TCP_CONN_NONE) { - return 1; + return false; } - return 0; + return true; } -/* return 1 if the tcp_connections_number is not valid. - * return 0 if the tcp_connections_number is valid. +/** + * Return true if the tcp_connections_number is valid. */ -static bool tcp_connections_number_not_valid(const TCP_Connections *tcp_c, int tcp_connections_number) +static bool tcp_connections_number_is_valid(const TCP_Connections *tcp_c, int tcp_connections_number) { if ((unsigned int)tcp_connections_number >= tcp_c->tcp_connections_length) { - return 1; + return false; } if (tcp_c->tcp_connections == nullptr) { - return 1; + return false; } if (tcp_c->tcp_connections[tcp_connections_number].status == TCP_CONN_NONE) { - return 1; + return false; } - return 0; + return true; } /* Create a new empty connection. @@ -199,7 +199,7 @@ static int create_tcp_connection(TCP_Connections *tcp_c) */ static int wipe_connection(TCP_Connections *tcp_c, int connections_number) { - if (connections_number_not_valid(tcp_c, connections_number)) { + if (!connections_number_is_valid(tcp_c, connections_number)) { return -1; } @@ -227,7 +227,7 @@ static int wipe_connection(TCP_Connections *tcp_c, int connections_number) */ static int wipe_tcp_connection(TCP_Connections *tcp_c, int tcp_connections_number) { - if (tcp_connections_number_not_valid(tcp_c, tcp_connections_number)) { + if (!tcp_connections_number_is_valid(tcp_c, tcp_connections_number)) { return -1; } @@ -250,7 +250,7 @@ static int wipe_tcp_connection(TCP_Connections *tcp_c, int tcp_connections_numbe static TCP_Connection_to *get_connection(const TCP_Connections *tcp_c, int connections_number) { - if (connections_number_not_valid(tcp_c, connections_number)) { + if (!connections_number_is_valid(tcp_c, connections_number)) { return nullptr; } @@ -259,7 +259,7 @@ static TCP_Connection_to *get_connection(const TCP_Connections *tcp_c, int conne static TCP_con *get_tcp_connection(const TCP_Connections *tcp_c, int tcp_connections_number) { - if (tcp_connections_number_not_valid(tcp_c, tcp_connections_number)) { + if (!tcp_connections_number_is_valid(tcp_c, tcp_connections_number)) { return nullptr; } diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index fb69b7685f..2851053bbf 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -174,23 +174,23 @@ DHT *nc_get_dht(const Net_Crypto *c) return c->dht; } -static uint8_t crypt_connection_id_not_valid(const Net_Crypto *c, int crypt_connection_id) +static bool crypt_connection_id_is_valid(const Net_Crypto *c, int crypt_connection_id) { if ((uint32_t)crypt_connection_id >= c->crypto_connections_length) { - return 1; + return false; } if (c->crypto_connections == nullptr) { - return 1; + return false; } const Crypto_Conn_State status = c->crypto_connections[crypt_connection_id].status; if (status == CRYPTO_CONN_NO_CONNECTION || status == CRYPTO_CONN_FREE) { - return 1; + return false; } - return 0; + return true; } /* cookie timeout in seconds */ @@ -555,7 +555,7 @@ static int handle_crypto_handshake(const Net_Crypto *c, uint8_t *nonce, uint8_t static Crypto_Connection *get_crypto_connection(const Net_Crypto *c, int crypt_connection_id) { - if (crypt_connection_id_not_valid(c, crypt_connection_id)) { + if (!crypt_connection_id_is_valid(c, crypt_connection_id)) { return nullptr; } @@ -1858,7 +1858,7 @@ static int wipe_crypto_connection(Net_Crypto *c, int crypt_connection_id) static int getcryptconnection_id(const Net_Crypto *c, const uint8_t *public_key) { for (uint32_t i = 0; i < c->crypto_connections_length; ++i) { - if (crypt_connection_id_not_valid(c, i)) { + if (!crypt_connection_id_is_valid(c, i)) { continue; }