From bdd42b5452c47a63b6b87fd8e637e5793691121a Mon Sep 17 00:00:00 2001 From: iphydf Date: Tue, 16 Jan 2024 23:09:29 +0000 Subject: [PATCH] refactor: Add common msgpack array packer with callback. There will be more object arrays that need to be packed. This function takes care of NULL (creating an empty array), and putting the correct array size and calling the per-element callback the right amount of times. --- .../docker/tox-bootstrapd.sha256 | 2 +- other/event_tooling/generate_event_c.cpp | 1 + toxcore/bin_pack.c | 20 ++ toxcore/bin_pack.h | 44 ++- toxcore/events/conference_connected.c | 1 + toxcore/events/conference_invite.c | 1 + toxcore/events/conference_message.c | 1 + toxcore/events/conference_peer_list_changed.c | 1 + toxcore/events/conference_peer_name.c | 1 + toxcore/events/conference_title.c | 1 + toxcore/events/file_chunk_request.c | 1 + toxcore/events/file_recv.c | 1 + toxcore/events/file_recv_chunk.c | 1 + toxcore/events/file_recv_control.c | 1 + toxcore/events/friend_connection_status.c | 1 + toxcore/events/friend_lossless_packet.c | 1 + toxcore/events/friend_lossy_packet.c | 1 + toxcore/events/friend_message.c | 1 + toxcore/events/friend_name.c | 1 + toxcore/events/friend_read_receipt.c | 1 + toxcore/events/friend_request.c | 1 + toxcore/events/friend_status.c | 1 + toxcore/events/friend_status_message.c | 1 + toxcore/events/friend_typing.c | 1 + toxcore/events/group_custom_packet.c | 1 + toxcore/events/group_custom_private_packet.c | 1 + toxcore/events/group_invite.c | 1 + toxcore/events/group_join_fail.c | 1 + toxcore/events/group_message.c | 1 + toxcore/events/group_moderation.c | 1 + toxcore/events/group_password.c | 1 + toxcore/events/group_peer_exit.c | 1 + toxcore/events/group_peer_join.c | 1 + toxcore/events/group_peer_limit.c | 1 + toxcore/events/group_peer_name.c | 1 + toxcore/events/group_peer_status.c | 1 + toxcore/events/group_privacy_state.c | 1 + toxcore/events/group_private_message.c | 1 + toxcore/events/group_self_join.c | 1 + toxcore/events/group_topic.c | 1 + toxcore/events/group_topic_lock.c | 1 + toxcore/events/group_voice_state.c | 1 + toxcore/events/self_connection_status.c | 1 + toxcore/tox_event.c | 258 +++++++++--------- toxcore/tox_events.c | 49 ++-- 45 files changed, 253 insertions(+), 160 deletions(-) diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index dc6c8ff2..a316ea13 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -3b4f5c3224e919d4474c4af4b5058916b181a2689f59a3b04e72305b454b2ae3 /usr/local/bin/tox-bootstrapd +3acc3a1f08e67dac66d91657a36d98be397fa3f14bc4798d3349605e37a90fc3 /usr/local/bin/tox-bootstrapd diff --git a/other/event_tooling/generate_event_c.cpp b/other/event_tooling/generate_event_c.cpp index d2998de3..bc7b2eb5 100644 --- a/other/event_tooling/generate_event_c.cpp +++ b/other/event_tooling/generate_event_c.cpp @@ -452,6 +452,7 @@ void generate_event_impl(const std::string& event_name, const std::vectorctx, size); diff --git a/toxcore/bin_pack.h b/toxcore/bin_pack.h index 0de0b116..d6ada196 100644 --- a/toxcore/bin_pack.h +++ b/toxcore/bin_pack.h @@ -17,6 +17,11 @@ extern "C" { /** * @brief Binary serialisation object. * + * Naming convention: + * - Functions ending in `_b` (or `_b_size`) are NOT MessagePack, i.e. write + * data in plain big endian binary format. + * - All other functions encode their input in MessagePack format. + * * Some notes on parameter order: * * - We pass the `obj` pointer as `this`-like pointer first to the callbacks. @@ -66,7 +71,9 @@ uint32_t bin_pack_obj_size(bin_pack_cb *callback, const void *obj, const Logger /** @brief Pack an object into a buffer of a given size. * * This function creates and initialises a `Bin_Pack` packer object, calls the callback with the - * packer object and the to-be-packed object, and then cleans up the packer object. + * packer object and the to-be-packed object, and then cleans up the packer object. Note that + * there is nothing MessagePack-specific about this function, so it can be used for both custom + * binary and MessagePack formats. * * You can use `bin_pack_obj_size` to determine the minimum required size of `buf`. If packing * overflows `uint32_t`, this function returns `false`. @@ -102,13 +109,8 @@ uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const void *arr, /** @brief Pack an object array into a buffer of a given size. * - * Calls the callback `arr_size` times with increasing `index` argument from 0 to - * `arr_size`. This function is here just so we don't need to write the same - * trivial loop many times and so we don't need an extra struct just to contain - * an array with size so it can be passed to `bin_pack_obj`. - * - * Similar to `bin_pack_obj` but for arrays. Does not write the array length, so - * if you need that, write it manually using `bin_pack_array`. + * Similar to `bin_pack_obj_array` but does not write the array length, so + * if you need that, encoding it is on you. * * Passing NULL for `arr` has no effect, but requires that `arr_size` is 0. * @@ -125,6 +127,32 @@ uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const void *arr, non_null(1, 5) nullable(2, 4) bool bin_pack_obj_array_b(bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger, uint8_t *buf, uint32_t buf_size); +/** @brief Encode an object array as MessagePack array into a bin packer. + * + * Calls the callback `arr_size` times with increasing `index` argument from 0 to + * `arr_size`. This function is here just so we don't need to write the same + * trivial loop many times and so we don't need an extra struct just to contain + * an array with size so it can be passed to `bin_pack_obj`. + * + * Similar to `bin_pack_obj` but for arrays. Note that a `Bin_Pack` object is + * required here, so it must be called from within a callback to one of the + * functions above. + * + * Passing NULL for `arr` requires that `arr_size` is 0. This will write a 0-size + * MessagePack array to the packer. + * + * @param bp Bin packer object. + * @param callback The function called on the created packer and packed object + * array. + * @param arr The object array to be packed, passed as `arr` to the callback. + * @param arr_size The number of elements in the object array. + * @param logger Optional logger object to pass to the callback. + * + * @retval false if an error occurred (e.g. buffer overflow). + */ +non_null(1, 2) nullable(3, 5) +bool bin_pack_obj_array(Bin_Pack *bp, bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger); + /** @brief Start packing a MessagePack array. * * A call to this function must be followed by exactly `size` calls to other functions below. diff --git a/toxcore/events/conference_connected.c b/toxcore/events/conference_connected.c index 80834bf4..4fd5708a 100644 --- a/toxcore/events/conference_connected.c +++ b/toxcore/events/conference_connected.c @@ -119,6 +119,7 @@ bool tox_event_conference_connected_unpack( Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_conference_connected_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/conference_invite.c b/toxcore/events/conference_invite.c index f4a71ddd..f4ef1db4 100644 --- a/toxcore/events/conference_invite.c +++ b/toxcore/events/conference_invite.c @@ -187,6 +187,7 @@ bool tox_event_conference_invite_unpack( Tox_Event_Conference_Invite **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_conference_invite_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/conference_message.c b/toxcore/events/conference_message.c index 57d151a9..eb706490 100644 --- a/toxcore/events/conference_message.c +++ b/toxcore/events/conference_message.c @@ -203,6 +203,7 @@ bool tox_event_conference_message_unpack( Tox_Event_Conference_Message **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_conference_message_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/conference_peer_list_changed.c b/toxcore/events/conference_peer_list_changed.c index 37b97568..cff268ce 100644 --- a/toxcore/events/conference_peer_list_changed.c +++ b/toxcore/events/conference_peer_list_changed.c @@ -119,6 +119,7 @@ bool tox_event_conference_peer_list_changed_unpack( Tox_Event_Conference_Peer_List_Changed **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_conference_peer_list_changed_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/conference_peer_name.c b/toxcore/events/conference_peer_name.c index 1119bd6f..ce1b5bf8 100644 --- a/toxcore/events/conference_peer_name.c +++ b/toxcore/events/conference_peer_name.c @@ -185,6 +185,7 @@ bool tox_event_conference_peer_name_unpack( Tox_Event_Conference_Peer_Name **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_conference_peer_name_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/conference_title.c b/toxcore/events/conference_title.c index 8d3be90a..7d52fda6 100644 --- a/toxcore/events/conference_title.c +++ b/toxcore/events/conference_title.c @@ -185,6 +185,7 @@ bool tox_event_conference_title_unpack( Tox_Event_Conference_Title **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_conference_title_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/file_chunk_request.c b/toxcore/events/file_chunk_request.c index a83bbd49..ec1da49b 100644 --- a/toxcore/events/file_chunk_request.c +++ b/toxcore/events/file_chunk_request.c @@ -172,6 +172,7 @@ bool tox_event_file_chunk_request_unpack( Tox_Event_File_Chunk_Request **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_file_chunk_request_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/file_recv.c b/toxcore/events/file_recv.c index a23a9c0b..b962980d 100644 --- a/toxcore/events/file_recv.c +++ b/toxcore/events/file_recv.c @@ -217,6 +217,7 @@ bool tox_event_file_recv_unpack( Tox_Event_File_Recv **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_file_recv_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/file_recv_chunk.c b/toxcore/events/file_recv_chunk.c index b4f1c908..535f38bf 100644 --- a/toxcore/events/file_recv_chunk.c +++ b/toxcore/events/file_recv_chunk.c @@ -201,6 +201,7 @@ bool tox_event_file_recv_chunk_unpack( Tox_Event_File_Recv_Chunk **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_file_recv_chunk_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/file_recv_control.c b/toxcore/events/file_recv_control.c index 0fc26ae3..a5a23733 100644 --- a/toxcore/events/file_recv_control.c +++ b/toxcore/events/file_recv_control.c @@ -158,6 +158,7 @@ bool tox_event_file_recv_control_unpack( Tox_Event_File_Recv_Control **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_file_recv_control_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/friend_connection_status.c b/toxcore/events/friend_connection_status.c index 4e158e0b..81639b87 100644 --- a/toxcore/events/friend_connection_status.c +++ b/toxcore/events/friend_connection_status.c @@ -142,6 +142,7 @@ bool tox_event_friend_connection_status_unpack( Tox_Event_Friend_Connection_Status **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_friend_connection_status_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/friend_lossless_packet.c b/toxcore/events/friend_lossless_packet.c index 9e26e68f..0d752a00 100644 --- a/toxcore/events/friend_lossless_packet.c +++ b/toxcore/events/friend_lossless_packet.c @@ -169,6 +169,7 @@ bool tox_event_friend_lossless_packet_unpack( Tox_Event_Friend_Lossless_Packet **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_friend_lossless_packet_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/friend_lossy_packet.c b/toxcore/events/friend_lossy_packet.c index fce146a6..e667bf15 100644 --- a/toxcore/events/friend_lossy_packet.c +++ b/toxcore/events/friend_lossy_packet.c @@ -169,6 +169,7 @@ bool tox_event_friend_lossy_packet_unpack( Tox_Event_Friend_Lossy_Packet **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_friend_lossy_packet_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/friend_message.c b/toxcore/events/friend_message.c index f9b2bde8..81173b3d 100644 --- a/toxcore/events/friend_message.c +++ b/toxcore/events/friend_message.c @@ -187,6 +187,7 @@ bool tox_event_friend_message_unpack( Tox_Event_Friend_Message **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_friend_message_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/friend_name.c b/toxcore/events/friend_name.c index 75f9bd4d..4cee44c2 100644 --- a/toxcore/events/friend_name.c +++ b/toxcore/events/friend_name.c @@ -169,6 +169,7 @@ bool tox_event_friend_name_unpack( Tox_Event_Friend_Name **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_friend_name_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/friend_read_receipt.c b/toxcore/events/friend_read_receipt.c index f52fc37a..66f364ef 100644 --- a/toxcore/events/friend_read_receipt.c +++ b/toxcore/events/friend_read_receipt.c @@ -140,6 +140,7 @@ bool tox_event_friend_read_receipt_unpack( Tox_Event_Friend_Read_Receipt **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_friend_read_receipt_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/friend_request.c b/toxcore/events/friend_request.c index 70f468cb..3d700712 100644 --- a/toxcore/events/friend_request.c +++ b/toxcore/events/friend_request.c @@ -159,6 +159,7 @@ bool tox_event_friend_request_unpack( Tox_Event_Friend_Request **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_friend_request_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/friend_status.c b/toxcore/events/friend_status.c index 55e60657..e802d0b8 100644 --- a/toxcore/events/friend_status.c +++ b/toxcore/events/friend_status.c @@ -142,6 +142,7 @@ bool tox_event_friend_status_unpack( Tox_Event_Friend_Status **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_friend_status_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/friend_status_message.c b/toxcore/events/friend_status_message.c index e4284b5a..39f42cfb 100644 --- a/toxcore/events/friend_status_message.c +++ b/toxcore/events/friend_status_message.c @@ -169,6 +169,7 @@ bool tox_event_friend_status_message_unpack( Tox_Event_Friend_Status_Message **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_friend_status_message_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/friend_typing.c b/toxcore/events/friend_typing.c index 9b099974..5ae2ded6 100644 --- a/toxcore/events/friend_typing.c +++ b/toxcore/events/friend_typing.c @@ -140,6 +140,7 @@ bool tox_event_friend_typing_unpack( Tox_Event_Friend_Typing **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_friend_typing_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_custom_packet.c b/toxcore/events/group_custom_packet.c index 8f8478a3..39436dba 100644 --- a/toxcore/events/group_custom_packet.c +++ b/toxcore/events/group_custom_packet.c @@ -185,6 +185,7 @@ bool tox_event_group_custom_packet_unpack( Tox_Event_Group_Custom_Packet **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_custom_packet_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_custom_private_packet.c b/toxcore/events/group_custom_private_packet.c index 8efa1075..0297c66b 100644 --- a/toxcore/events/group_custom_private_packet.c +++ b/toxcore/events/group_custom_private_packet.c @@ -185,6 +185,7 @@ bool tox_event_group_custom_private_packet_unpack( Tox_Event_Group_Custom_Private_Packet **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_custom_private_packet_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_invite.c b/toxcore/events/group_invite.c index 6cb38f95..103dda18 100644 --- a/toxcore/events/group_invite.c +++ b/toxcore/events/group_invite.c @@ -213,6 +213,7 @@ bool tox_event_group_invite_unpack( Tox_Event_Group_Invite **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_invite_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_join_fail.c b/toxcore/events/group_join_fail.c index 88565680..07ec596d 100644 --- a/toxcore/events/group_join_fail.c +++ b/toxcore/events/group_join_fail.c @@ -142,6 +142,7 @@ bool tox_event_group_join_fail_unpack( Tox_Event_Group_Join_Fail **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_join_fail_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_message.c b/toxcore/events/group_message.c index b938a8ba..e24c039b 100644 --- a/toxcore/events/group_message.c +++ b/toxcore/events/group_message.c @@ -219,6 +219,7 @@ bool tox_event_group_message_unpack( Tox_Event_Group_Message **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_message_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_moderation.c b/toxcore/events/group_moderation.c index 7ac46ffc..339a84c3 100644 --- a/toxcore/events/group_moderation.c +++ b/toxcore/events/group_moderation.c @@ -174,6 +174,7 @@ bool tox_event_group_moderation_unpack( Tox_Event_Group_Moderation **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_moderation_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_password.c b/toxcore/events/group_password.c index 863cc6a6..9776fffb 100644 --- a/toxcore/events/group_password.c +++ b/toxcore/events/group_password.c @@ -169,6 +169,7 @@ bool tox_event_group_password_unpack( Tox_Event_Group_Password **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_password_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_peer_exit.c b/toxcore/events/group_peer_exit.c index c6ac4263..4653e5fa 100644 --- a/toxcore/events/group_peer_exit.c +++ b/toxcore/events/group_peer_exit.c @@ -247,6 +247,7 @@ bool tox_event_group_peer_exit_unpack( Tox_Event_Group_Peer_Exit **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_peer_exit_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_peer_join.c b/toxcore/events/group_peer_join.c index c587c179..48a745c9 100644 --- a/toxcore/events/group_peer_join.c +++ b/toxcore/events/group_peer_join.c @@ -140,6 +140,7 @@ bool tox_event_group_peer_join_unpack( Tox_Event_Group_Peer_Join **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_peer_join_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_peer_limit.c b/toxcore/events/group_peer_limit.c index 9a2daa7e..3a0326c1 100644 --- a/toxcore/events/group_peer_limit.c +++ b/toxcore/events/group_peer_limit.c @@ -140,6 +140,7 @@ bool tox_event_group_peer_limit_unpack( Tox_Event_Group_Peer_Limit **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_peer_limit_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_peer_name.c b/toxcore/events/group_peer_name.c index a4a478de..d41d594d 100644 --- a/toxcore/events/group_peer_name.c +++ b/toxcore/events/group_peer_name.c @@ -185,6 +185,7 @@ bool tox_event_group_peer_name_unpack( Tox_Event_Group_Peer_Name **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_peer_name_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_peer_status.c b/toxcore/events/group_peer_status.c index 2cfe32e0..6e437b0d 100644 --- a/toxcore/events/group_peer_status.c +++ b/toxcore/events/group_peer_status.c @@ -158,6 +158,7 @@ bool tox_event_group_peer_status_unpack( Tox_Event_Group_Peer_Status **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_peer_status_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_privacy_state.c b/toxcore/events/group_privacy_state.c index 8623668b..939a7129 100644 --- a/toxcore/events/group_privacy_state.c +++ b/toxcore/events/group_privacy_state.c @@ -142,6 +142,7 @@ bool tox_event_group_privacy_state_unpack( Tox_Event_Group_Privacy_State **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_privacy_state_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_private_message.c b/toxcore/events/group_private_message.c index 27efc1e2..f3e21375 100644 --- a/toxcore/events/group_private_message.c +++ b/toxcore/events/group_private_message.c @@ -203,6 +203,7 @@ bool tox_event_group_private_message_unpack( Tox_Event_Group_Private_Message **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_private_message_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_self_join.c b/toxcore/events/group_self_join.c index 904a3e53..b80667a6 100644 --- a/toxcore/events/group_self_join.c +++ b/toxcore/events/group_self_join.c @@ -119,6 +119,7 @@ bool tox_event_group_self_join_unpack( Tox_Event_Group_Self_Join **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_self_join_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_topic.c b/toxcore/events/group_topic.c index 1a03c72f..fcca0248 100644 --- a/toxcore/events/group_topic.c +++ b/toxcore/events/group_topic.c @@ -185,6 +185,7 @@ bool tox_event_group_topic_unpack( Tox_Event_Group_Topic **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_topic_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_topic_lock.c b/toxcore/events/group_topic_lock.c index 82d37a15..27e94f61 100644 --- a/toxcore/events/group_topic_lock.c +++ b/toxcore/events/group_topic_lock.c @@ -142,6 +142,7 @@ bool tox_event_group_topic_lock_unpack( Tox_Event_Group_Topic_Lock **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_topic_lock_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/group_voice_state.c b/toxcore/events/group_voice_state.c index 7bcfa556..8f41f697 100644 --- a/toxcore/events/group_voice_state.c +++ b/toxcore/events/group_voice_state.c @@ -142,6 +142,7 @@ bool tox_event_group_voice_state_unpack( Tox_Event_Group_Voice_State **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_group_voice_state_new(mem); if (*event == nullptr) { diff --git a/toxcore/events/self_connection_status.c b/toxcore/events/self_connection_status.c index 417a3fe3..1b1175ed 100644 --- a/toxcore/events/self_connection_status.c +++ b/toxcore/events/self_connection_status.c @@ -121,6 +121,7 @@ bool tox_event_self_connection_status_unpack( Tox_Event_Self_Connection_Status **event, Bin_Unpack *bu, const Memory *mem) { assert(event != nullptr); + assert(*event == nullptr); *event = tox_event_self_connection_status_new(mem); if (*event == nullptr) { diff --git a/toxcore/tox_event.c b/toxcore/tox_event.c index cba99eed..dd54fa97 100644 --- a/toxcore/tox_event.c +++ b/toxcore/tox_event.c @@ -947,6 +947,137 @@ static bool tox_event_type_unpack(Bin_Unpack *bu, Tox_Event_Type *val) && tox_event_type_from_int(u32, val); } +non_null() +static bool tox_event_data_unpack(Tox_Event_Type type, Tox_Event_Data *data, Bin_Unpack *bu, const Memory *mem) +{ + switch (type) { + case TOX_EVENT_CONFERENCE_CONNECTED: + return tox_event_conference_connected_unpack(&data->conference_connected, bu, mem); + + case TOX_EVENT_CONFERENCE_INVITE: + return tox_event_conference_invite_unpack(&data->conference_invite, bu, mem); + + case TOX_EVENT_CONFERENCE_MESSAGE: + return tox_event_conference_message_unpack(&data->conference_message, bu, mem); + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: + return tox_event_conference_peer_list_changed_unpack(&data->conference_peer_list_changed, bu, mem); + + case TOX_EVENT_CONFERENCE_PEER_NAME: + return tox_event_conference_peer_name_unpack(&data->conference_peer_name, bu, mem); + + case TOX_EVENT_CONFERENCE_TITLE: + return tox_event_conference_title_unpack(&data->conference_title, bu, mem); + + case TOX_EVENT_FILE_CHUNK_REQUEST: + return tox_event_file_chunk_request_unpack(&data->file_chunk_request, bu, mem); + + case TOX_EVENT_FILE_RECV_CHUNK: + return tox_event_file_recv_chunk_unpack(&data->file_recv_chunk, bu, mem); + + case TOX_EVENT_FILE_RECV_CONTROL: + return tox_event_file_recv_control_unpack(&data->file_recv_control, bu, mem); + + case TOX_EVENT_FILE_RECV: + return tox_event_file_recv_unpack(&data->file_recv, bu, mem); + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: + return tox_event_friend_connection_status_unpack(&data->friend_connection_status, bu, mem); + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: + return tox_event_friend_lossless_packet_unpack(&data->friend_lossless_packet, bu, mem); + + case TOX_EVENT_FRIEND_LOSSY_PACKET: + return tox_event_friend_lossy_packet_unpack(&data->friend_lossy_packet, bu, mem); + + case TOX_EVENT_FRIEND_MESSAGE: + return tox_event_friend_message_unpack(&data->friend_message, bu, mem); + + case TOX_EVENT_FRIEND_NAME: + return tox_event_friend_name_unpack(&data->friend_name, bu, mem); + + case TOX_EVENT_FRIEND_READ_RECEIPT: + return tox_event_friend_read_receipt_unpack(&data->friend_read_receipt, bu, mem); + + case TOX_EVENT_FRIEND_REQUEST: + return tox_event_friend_request_unpack(&data->friend_request, bu, mem); + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: + return tox_event_friend_status_message_unpack(&data->friend_status_message, bu, mem); + + case TOX_EVENT_FRIEND_STATUS: + return tox_event_friend_status_unpack(&data->friend_status, bu, mem); + + case TOX_EVENT_FRIEND_TYPING: + return tox_event_friend_typing_unpack(&data->friend_typing, bu, mem); + + case TOX_EVENT_SELF_CONNECTION_STATUS: + return tox_event_self_connection_status_unpack(&data->self_connection_status, bu, mem); + + case TOX_EVENT_GROUP_PEER_NAME: + return tox_event_group_peer_name_unpack(&data->group_peer_name, bu, mem); + + case TOX_EVENT_GROUP_PEER_STATUS: + return tox_event_group_peer_status_unpack(&data->group_peer_status, bu, mem); + + case TOX_EVENT_GROUP_TOPIC: + return tox_event_group_topic_unpack(&data->group_topic, bu, mem); + + case TOX_EVENT_GROUP_PRIVACY_STATE: + return tox_event_group_privacy_state_unpack(&data->group_privacy_state, bu, mem); + + case TOX_EVENT_GROUP_VOICE_STATE: + return tox_event_group_voice_state_unpack(&data->group_voice_state, bu, mem); + + case TOX_EVENT_GROUP_TOPIC_LOCK: + return tox_event_group_topic_lock_unpack(&data->group_topic_lock, bu, mem); + + case TOX_EVENT_GROUP_PEER_LIMIT: + return tox_event_group_peer_limit_unpack(&data->group_peer_limit, bu, mem); + + case TOX_EVENT_GROUP_PASSWORD: + return tox_event_group_password_unpack(&data->group_password, bu, mem); + + case TOX_EVENT_GROUP_MESSAGE: + return tox_event_group_message_unpack(&data->group_message, bu, mem); + + case TOX_EVENT_GROUP_PRIVATE_MESSAGE: + return tox_event_group_private_message_unpack(&data->group_private_message, bu, mem); + + case TOX_EVENT_GROUP_CUSTOM_PACKET: + return tox_event_group_custom_packet_unpack(&data->group_custom_packet, bu, mem); + + case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: + return tox_event_group_custom_private_packet_unpack(&data->group_custom_private_packet, bu, mem); + + case TOX_EVENT_GROUP_INVITE: + return tox_event_group_invite_unpack(&data->group_invite, bu, mem); + + case TOX_EVENT_GROUP_PEER_JOIN: + return tox_event_group_peer_join_unpack(&data->group_peer_join, bu, mem); + + case TOX_EVENT_GROUP_PEER_EXIT: + return tox_event_group_peer_exit_unpack(&data->group_peer_exit, bu, mem); + + case TOX_EVENT_GROUP_SELF_JOIN: + return tox_event_group_self_join_unpack(&data->group_self_join, bu, mem); + + case TOX_EVENT_GROUP_JOIN_FAIL: + return tox_event_group_join_fail_unpack(&data->group_join_fail, bu, mem); + + case TOX_EVENT_GROUP_MODERATION: + return tox_event_group_moderation_unpack(&data->group_moderation, bu, mem); + + case TOX_EVENT_DHT_GET_NODES_RESPONSE: + return tox_event_dht_get_nodes_response_unpack(&data->dht_get_nodes_response, bu, mem); + + case TOX_EVENT_INVALID: + return false; + } + + return false; +} + bool tox_event_unpack_into(Tox_Event *event, Bin_Unpack *bu, const Memory *mem) { uint32_t size; @@ -965,130 +1096,5 @@ bool tox_event_unpack_into(Tox_Event *event, Bin_Unpack *bu, const Memory *mem) event->type = type; - switch (type) { - case TOX_EVENT_CONFERENCE_CONNECTED: - return tox_event_conference_connected_unpack(&event->data.conference_connected, bu, mem); - - case TOX_EVENT_CONFERENCE_INVITE: - return tox_event_conference_invite_unpack(&event->data.conference_invite, bu, mem); - - case TOX_EVENT_CONFERENCE_MESSAGE: - return tox_event_conference_message_unpack(&event->data.conference_message, bu, mem); - - case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: - return tox_event_conference_peer_list_changed_unpack(&event->data.conference_peer_list_changed, bu, mem); - - case TOX_EVENT_CONFERENCE_PEER_NAME: - return tox_event_conference_peer_name_unpack(&event->data.conference_peer_name, bu, mem); - - case TOX_EVENT_CONFERENCE_TITLE: - return tox_event_conference_title_unpack(&event->data.conference_title, bu, mem); - - case TOX_EVENT_FILE_CHUNK_REQUEST: - return tox_event_file_chunk_request_unpack(&event->data.file_chunk_request, bu, mem); - - case TOX_EVENT_FILE_RECV_CHUNK: - return tox_event_file_recv_chunk_unpack(&event->data.file_recv_chunk, bu, mem); - - case TOX_EVENT_FILE_RECV_CONTROL: - return tox_event_file_recv_control_unpack(&event->data.file_recv_control, bu, mem); - - case TOX_EVENT_FILE_RECV: - return tox_event_file_recv_unpack(&event->data.file_recv, bu, mem); - - case TOX_EVENT_FRIEND_CONNECTION_STATUS: - return tox_event_friend_connection_status_unpack(&event->data.friend_connection_status, bu, mem); - - case TOX_EVENT_FRIEND_LOSSLESS_PACKET: - return tox_event_friend_lossless_packet_unpack(&event->data.friend_lossless_packet, bu, mem); - - case TOX_EVENT_FRIEND_LOSSY_PACKET: - return tox_event_friend_lossy_packet_unpack(&event->data.friend_lossy_packet, bu, mem); - - case TOX_EVENT_FRIEND_MESSAGE: - return tox_event_friend_message_unpack(&event->data.friend_message, bu, mem); - - case TOX_EVENT_FRIEND_NAME: - return tox_event_friend_name_unpack(&event->data.friend_name, bu, mem); - - case TOX_EVENT_FRIEND_READ_RECEIPT: - return tox_event_friend_read_receipt_unpack(&event->data.friend_read_receipt, bu, mem); - - case TOX_EVENT_FRIEND_REQUEST: - return tox_event_friend_request_unpack(&event->data.friend_request, bu, mem); - - case TOX_EVENT_FRIEND_STATUS_MESSAGE: - return tox_event_friend_status_message_unpack(&event->data.friend_status_message, bu, mem); - - case TOX_EVENT_FRIEND_STATUS: - return tox_event_friend_status_unpack(&event->data.friend_status, bu, mem); - - case TOX_EVENT_FRIEND_TYPING: - return tox_event_friend_typing_unpack(&event->data.friend_typing, bu, mem); - - case TOX_EVENT_SELF_CONNECTION_STATUS: - return tox_event_self_connection_status_unpack(&event->data.self_connection_status, bu, mem); - - case TOX_EVENT_GROUP_PEER_NAME: - return tox_event_group_peer_name_unpack(&event->data.group_peer_name, bu, mem); - - case TOX_EVENT_GROUP_PEER_STATUS: - return tox_event_group_peer_status_unpack(&event->data.group_peer_status, bu, mem); - - case TOX_EVENT_GROUP_TOPIC: - return tox_event_group_topic_unpack(&event->data.group_topic, bu, mem); - - case TOX_EVENT_GROUP_PRIVACY_STATE: - return tox_event_group_privacy_state_unpack(&event->data.group_privacy_state, bu, mem); - - case TOX_EVENT_GROUP_VOICE_STATE: - return tox_event_group_voice_state_unpack(&event->data.group_voice_state, bu, mem); - - case TOX_EVENT_GROUP_TOPIC_LOCK: - return tox_event_group_topic_lock_unpack(&event->data.group_topic_lock, bu, mem); - - case TOX_EVENT_GROUP_PEER_LIMIT: - return tox_event_group_peer_limit_unpack(&event->data.group_peer_limit, bu, mem); - - case TOX_EVENT_GROUP_PASSWORD: - return tox_event_group_password_unpack(&event->data.group_password, bu, mem); - - case TOX_EVENT_GROUP_MESSAGE: - return tox_event_group_message_unpack(&event->data.group_message, bu, mem); - - case TOX_EVENT_GROUP_PRIVATE_MESSAGE: - return tox_event_group_private_message_unpack(&event->data.group_private_message, bu, mem); - - case TOX_EVENT_GROUP_CUSTOM_PACKET: - return tox_event_group_custom_packet_unpack(&event->data.group_custom_packet, bu, mem); - - case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: - return tox_event_group_custom_private_packet_unpack(&event->data.group_custom_private_packet, bu, mem); - - case TOX_EVENT_GROUP_INVITE: - return tox_event_group_invite_unpack(&event->data.group_invite, bu, mem); - - case TOX_EVENT_GROUP_PEER_JOIN: - return tox_event_group_peer_join_unpack(&event->data.group_peer_join, bu, mem); - - case TOX_EVENT_GROUP_PEER_EXIT: - return tox_event_group_peer_exit_unpack(&event->data.group_peer_exit, bu, mem); - - case TOX_EVENT_GROUP_SELF_JOIN: - return tox_event_group_self_join_unpack(&event->data.group_self_join, bu, mem); - - case TOX_EVENT_GROUP_JOIN_FAIL: - return tox_event_group_join_fail_unpack(&event->data.group_join_fail, bu, mem); - - case TOX_EVENT_GROUP_MODERATION: - return tox_event_group_moderation_unpack(&event->data.group_moderation, bu, mem); - - case TOX_EVENT_DHT_GET_NODES_RESPONSE: - return tox_event_dht_get_nodes_response_unpack(&event->data.dht_get_nodes_response, bu, mem); - - case TOX_EVENT_INVALID: - return false; - } - - return false; + return tox_event_data_unpack(event->type, &event->data, bu, mem); } diff --git a/toxcore/tox_events.c b/toxcore/tox_events.c index 27efeaa8..a0b53bbe 100644 --- a/toxcore/tox_events.c +++ b/toxcore/tox_events.c @@ -75,6 +75,12 @@ uint32_t tox_events_get_size(const Tox_Events *events) return events == nullptr ? 0 : events->events_size; } +nullable(1) +static const Tox_Event *tox_events_get_events(const Tox_Events *events) +{ + return events == nullptr ? nullptr : events->events; +} + const Tox_Event *tox_events_get(const Tox_Events *events, uint32_t index) { if (index >= tox_events_get_size(events)) { @@ -102,26 +108,29 @@ Tox_Events *tox_events_iterate(Tox *tox, bool fail_hard, Tox_Err_Events_Iterate return state.events; } +non_null() +static bool tox_event_pack_handler(const void *obj, uint32_t index, const Logger *logger, Bin_Pack *bp) +{ + const Tox_Event *events = (const Tox_Event *)obj; + assert(events != nullptr); + return tox_event_pack(&events[index], bp); +} + non_null(3) nullable(1, 2) -static bool tox_events_pack(const void *obj, const Logger *logger, Bin_Pack *bp) +static bool tox_events_pack_handler(const void *obj, const Logger *logger, Bin_Pack *bp) { const Tox_Events *events = (const Tox_Events *)obj; + return bin_pack_obj_array(bp, tox_event_pack_handler, tox_events_get_events(events), tox_events_get_size(events), logger); +} - if (events == nullptr) { - return bin_pack_array(bp, 0); - } +uint32_t tox_events_bytes_size(const Tox_Events *events) +{ + return bin_pack_obj_size(tox_events_pack_handler, events, nullptr); +} - if (!bin_pack_array(bp, events->events_size)) { - return false; - } - - for (uint32_t i = 0; i < events->events_size; ++i) { - if (!tox_event_pack(&events->events[i], bp)) { - return false; - } - } - - return true; +bool tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes) +{ + return bin_pack_obj(tox_events_pack_handler, events, nullptr, bytes, UINT32_MAX); } non_null() @@ -152,16 +161,6 @@ static bool tox_events_unpack(void *obj, Bin_Unpack *bu) return true; } -uint32_t tox_events_bytes_size(const Tox_Events *events) -{ - return bin_pack_obj_size(tox_events_pack, events, nullptr); -} - -bool tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes) -{ - return bin_pack_obj(tox_events_pack, events, nullptr, bytes, UINT32_MAX); -} - Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_t bytes_size) { Tox_Events *events = (Tox_Events *)mem_alloc(sys->mem, sizeof(Tox_Events));