diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ee20b205b..50f87a97c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -279,6 +279,8 @@ set(toxcore_SOURCES ${toxcore_SOURCES} toxcore/events/events_alloc.c toxcore/events/events_alloc.h toxcore/events/self_connection_status.c + toxcore/bin_pack.c + toxcore/bin_pack.h toxcore/bin_unpack.c toxcore/bin_unpack.h toxcore/tox_events.c diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index 6ecfae9ed6..e98160a450 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -14,6 +14,17 @@ cc_library( visibility = ["//c-toxcore:__subpackages__"], ) +cc_library( + name = "bin_pack", + srcs = ["bin_pack.c"], + hdrs = ["bin_pack.h"], + visibility = ["//c-toxcore:__subpackages__"], + deps = [ + ":ccompat", + "@msgpack-c", + ], +) + cc_library( name = "bin_unpack", srcs = ["bin_unpack.c"], @@ -502,6 +513,7 @@ cc_library( hdrs = ["tox_events.h"], visibility = ["//c-toxcore:__subpackages__"], deps = [ + ":bin_pack", ":bin_unpack", ":ccompat", ":tox_unpack", diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index 108466635f..a035ddd7ae 100644 --- a/toxcore/Makefile.inc +++ b/toxcore/Makefile.inc @@ -6,6 +6,8 @@ libtoxcore_la_include_HEADERS = \ libtoxcore_la_includedir = $(includedir)/tox libtoxcore_la_SOURCES = ../toxcore/attributes.h \ + ../toxcore/bin_pack.c \ + ../toxcore/bin_pack.h \ ../toxcore/bin_unpack.c \ ../toxcore/bin_unpack.h \ ../toxcore/ccompat.c \ diff --git a/toxcore/bin_pack.c b/toxcore/bin_pack.c new file mode 100644 index 0000000000..e9377104ea --- /dev/null +++ b/toxcore/bin_pack.c @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022 The TokTok team. + */ + +#include "bin_pack.h" + +#include + +void bin_pack_array(msgpack_packer *mp, size_t size) +{ + msgpack_pack_array(mp, size); +} + +void bin_pack_bool(msgpack_packer *mp, bool val) +{ + if (val) { + msgpack_pack_true(mp); + } else { + msgpack_pack_false(mp); + } +} + +void bin_pack_u16(msgpack_packer *mp, uint16_t val) +{ + msgpack_pack_uint16(mp, val); +} + +void bin_pack_u32(msgpack_packer *mp, uint32_t val) +{ + msgpack_pack_uint32(mp, val); +} + +void bin_pack_u64(msgpack_packer *mp, uint64_t val) +{ + msgpack_pack_uint64(mp, val); +} + +void bin_pack_bytes(msgpack_packer *mp, const uint8_t *data, size_t length) +{ + msgpack_pack_bin(mp, length); + msgpack_pack_bin_body(mp, data, length); +} diff --git a/toxcore/bin_pack.h b/toxcore/bin_pack.h new file mode 100644 index 0000000000..0ef05b44c1 --- /dev/null +++ b/toxcore/bin_pack.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022 The TokTok team. + */ + +#ifndef C_TOXCORE_TOXCORE_BIN_PACK_H +#define C_TOXCORE_TOXCORE_BIN_PACK_H + +#include +#include +#include + +#include "attributes.h" + +non_null() void bin_pack_array(msgpack_packer *mp, size_t size); +non_null() void bin_pack_bool(msgpack_packer *mp, bool val); +non_null() void bin_pack_u16(msgpack_packer *mp, uint16_t val); +non_null() void bin_pack_u32(msgpack_packer *mp, uint32_t val); +non_null() void bin_pack_u64(msgpack_packer *mp, uint64_t val); +non_null() void bin_pack_bytes(msgpack_packer *mp, const uint8_t *data, size_t length); + +#endif // C_TOXCORE_TOXCORE_BIN_PACK_H diff --git a/toxcore/events/conference_connected.c b/toxcore/events/conference_connected.c index 9e938f3276..ab3494befc 100644 --- a/toxcore/events/conference_connected.c +++ b/toxcore/events/conference_connected.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -57,8 +58,8 @@ static void tox_event_conference_connected_pack( const Tox_Event_Conference_Connected *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 1); - msgpack_pack_uint32(mp, event->conference_number); + bin_pack_array(mp, 1); + bin_pack_u32(mp, event->conference_number); } non_null() @@ -145,7 +146,7 @@ void tox_events_pack_conference_connected(const Tox_Events *events, msgpack_pack { const uint32_t size = tox_events_get_conference_connected_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_conference_connected_pack(tox_events_get_conference_connected(events, i), mp); diff --git a/toxcore/events/conference_invite.c b/toxcore/events/conference_invite.c index 9481fd0d52..6970062475 100644 --- a/toxcore/events/conference_invite.c +++ b/toxcore/events/conference_invite.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -106,11 +107,10 @@ static void tox_event_conference_invite_pack( const Tox_Event_Conference_Invite *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 3); - msgpack_pack_uint32(mp, event->friend_number); - msgpack_pack_uint32(mp, event->type); - msgpack_pack_bin(mp, event->cookie_length); - msgpack_pack_bin_body(mp, event->cookie, event->cookie_length); + bin_pack_array(mp, 3); + bin_pack_u32(mp, event->friend_number); + bin_pack_u32(mp, event->type); + bin_pack_bytes(mp, event->cookie, event->cookie_length); } non_null() @@ -198,7 +198,7 @@ void tox_events_pack_conference_invite(const Tox_Events *events, msgpack_packer { const uint32_t size = tox_events_get_conference_invite_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_conference_invite_pack(tox_events_get_conference_invite(events, i), mp); diff --git a/toxcore/events/conference_message.c b/toxcore/events/conference_message.c index 9bf99f4f5c..66ba5c7e08 100644 --- a/toxcore/events/conference_message.c +++ b/toxcore/events/conference_message.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -120,12 +121,11 @@ static void tox_event_conference_message_pack( const Tox_Event_Conference_Message *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 4); - msgpack_pack_uint32(mp, event->conference_number); - msgpack_pack_uint32(mp, event->peer_number); - msgpack_pack_uint32(mp, event->type); - msgpack_pack_bin(mp, event->message_length); - msgpack_pack_bin_body(mp, event->message, event->message_length); + bin_pack_array(mp, 4); + bin_pack_u32(mp, event->conference_number); + bin_pack_u32(mp, event->peer_number); + bin_pack_u32(mp, event->type); + bin_pack_bytes(mp, event->message, event->message_length); } non_null() @@ -214,7 +214,7 @@ void tox_events_pack_conference_message(const Tox_Events *events, msgpack_packer { const uint32_t size = tox_events_get_conference_message_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_conference_message_pack(tox_events_get_conference_message(events, i), mp); diff --git a/toxcore/events/conference_peer_list_changed.c b/toxcore/events/conference_peer_list_changed.c index 52781499d0..676d4163c1 100644 --- a/toxcore/events/conference_peer_list_changed.c +++ b/toxcore/events/conference_peer_list_changed.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -59,8 +60,8 @@ static void tox_event_conference_peer_list_changed_pack( const Tox_Event_Conference_Peer_List_Changed *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 1); - msgpack_pack_uint32(mp, event->conference_number); + bin_pack_array(mp, 1); + bin_pack_u32(mp, event->conference_number); } non_null() @@ -150,7 +151,7 @@ void tox_events_pack_conference_peer_list_changed(const Tox_Events *events, msgp { const uint32_t size = tox_events_get_conference_peer_list_changed_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_conference_peer_list_changed_pack(tox_events_get_conference_peer_list_changed(events, i), mp); diff --git a/toxcore/events/conference_peer_name.c b/toxcore/events/conference_peer_name.c index ef2fb3ea68..bc69e7134b 100644 --- a/toxcore/events/conference_peer_name.c +++ b/toxcore/events/conference_peer_name.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -106,11 +107,10 @@ static void tox_event_conference_peer_name_pack( const Tox_Event_Conference_Peer_Name *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 3); - msgpack_pack_uint32(mp, event->conference_number); - msgpack_pack_uint32(mp, event->peer_number); - msgpack_pack_bin(mp, event->name_length); - msgpack_pack_bin_body(mp, event->name, event->name_length); + bin_pack_array(mp, 3); + bin_pack_u32(mp, event->conference_number); + bin_pack_u32(mp, event->peer_number); + bin_pack_bytes(mp, event->name, event->name_length); } non_null() @@ -199,7 +199,7 @@ void tox_events_pack_conference_peer_name(const Tox_Events *events, msgpack_pack { const uint32_t size = tox_events_get_conference_peer_name_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_conference_peer_name_pack(tox_events_get_conference_peer_name(events, i), mp); diff --git a/toxcore/events/conference_title.c b/toxcore/events/conference_title.c index 12a541f066..9c8fdba62a 100644 --- a/toxcore/events/conference_title.c +++ b/toxcore/events/conference_title.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -105,11 +106,10 @@ static void tox_event_conference_title_pack( const Tox_Event_Conference_Title *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 3); - msgpack_pack_uint32(mp, event->conference_number); - msgpack_pack_uint32(mp, event->peer_number); - msgpack_pack_bin(mp, event->title_length); - msgpack_pack_bin_body(mp, event->title, event->title_length); + bin_pack_array(mp, 3); + bin_pack_u32(mp, event->conference_number); + bin_pack_u32(mp, event->peer_number); + bin_pack_bytes(mp, event->title, event->title_length); } non_null() @@ -197,7 +197,7 @@ void tox_events_pack_conference_title(const Tox_Events *events, msgpack_packer * { const uint32_t size = tox_events_get_conference_title_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_conference_title_pack(tox_events_get_conference_title(events, i), mp); diff --git a/toxcore/events/file_chunk_request.c b/toxcore/events/file_chunk_request.c index 386f6865cb..251b4fdb99 100644 --- a/toxcore/events/file_chunk_request.c +++ b/toxcore/events/file_chunk_request.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -97,11 +98,11 @@ static void tox_event_file_chunk_request_pack( const Tox_Event_File_Chunk_Request *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 4); - msgpack_pack_uint32(mp, event->friend_number); - msgpack_pack_uint32(mp, event->file_number); - msgpack_pack_uint64(mp, event->position); - msgpack_pack_uint16(mp, event->length); + bin_pack_array(mp, 4); + bin_pack_u32(mp, event->friend_number); + bin_pack_u32(mp, event->file_number); + bin_pack_u64(mp, event->position); + bin_pack_u16(mp, event->length); } non_null() @@ -190,7 +191,7 @@ void tox_events_pack_file_chunk_request(const Tox_Events *events, msgpack_packer { const uint32_t size = tox_events_get_file_chunk_request_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_file_chunk_request_pack(tox_events_get_file_chunk_request(events, i), mp); diff --git a/toxcore/events/file_recv.c b/toxcore/events/file_recv.c index 2f02a964bd..a2ec30ff71 100644 --- a/toxcore/events/file_recv.c +++ b/toxcore/events/file_recv.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -133,13 +134,12 @@ static void tox_event_file_recv_pack( const Tox_Event_File_Recv *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 5); - msgpack_pack_uint32(mp, event->friend_number); - msgpack_pack_uint32(mp, event->file_number); - msgpack_pack_uint32(mp, event->kind); - msgpack_pack_uint64(mp, event->file_size); - msgpack_pack_bin(mp, event->filename_length); - msgpack_pack_bin_body(mp, event->filename, event->filename_length); + bin_pack_array(mp, 5); + bin_pack_u32(mp, event->friend_number); + bin_pack_u32(mp, event->file_number); + bin_pack_u32(mp, event->kind); + bin_pack_u64(mp, event->file_size); + bin_pack_bytes(mp, event->filename, event->filename_length); } non_null() @@ -229,7 +229,7 @@ void tox_events_pack_file_recv(const Tox_Events *events, msgpack_packer *mp) { const uint32_t size = tox_events_get_file_recv_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_file_recv_pack(tox_events_get_file_recv(events, i), mp); diff --git a/toxcore/events/file_recv_chunk.c b/toxcore/events/file_recv_chunk.c index fa787e2017..b35bb57402 100644 --- a/toxcore/events/file_recv_chunk.c +++ b/toxcore/events/file_recv_chunk.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -119,12 +120,11 @@ static void tox_event_file_recv_chunk_pack( const Tox_Event_File_Recv_Chunk *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 4); - msgpack_pack_uint32(mp, event->friend_number); - msgpack_pack_uint32(mp, event->file_number); - msgpack_pack_uint64(mp, event->position); - msgpack_pack_bin(mp, event->data_length); - msgpack_pack_bin_body(mp, event->data, event->data_length); + bin_pack_array(mp, 4); + bin_pack_u32(mp, event->friend_number); + bin_pack_u32(mp, event->file_number); + bin_pack_u64(mp, event->position); + bin_pack_bytes(mp, event->data, event->data_length); } non_null() @@ -213,7 +213,7 @@ void tox_events_pack_file_recv_chunk(const Tox_Events *events, msgpack_packer *m { const uint32_t size = tox_events_get_file_recv_chunk_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_file_recv_chunk_pack(tox_events_get_file_recv_chunk(events, i), mp); diff --git a/toxcore/events/file_recv_control.c b/toxcore/events/file_recv_control.c index e822787cd4..ee9e6e7671 100644 --- a/toxcore/events/file_recv_control.c +++ b/toxcore/events/file_recv_control.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -85,10 +86,10 @@ static void tox_event_file_recv_control_pack( const Tox_Event_File_Recv_Control *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 3); - msgpack_pack_uint32(mp, event->friend_number); - msgpack_pack_uint32(mp, event->file_number); - msgpack_pack_uint32(mp, event->control); + bin_pack_array(mp, 3); + bin_pack_u32(mp, event->friend_number); + bin_pack_u32(mp, event->file_number); + bin_pack_u32(mp, event->control); } non_null() @@ -176,7 +177,7 @@ void tox_events_pack_file_recv_control(const Tox_Events *events, msgpack_packer { const uint32_t size = tox_events_get_file_recv_control_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_file_recv_control_pack(tox_events_get_file_recv_control(events, i), mp); diff --git a/toxcore/events/friend_connection_status.c b/toxcore/events/friend_connection_status.c index bdee5f71d0..02bb39c1ee 100644 --- a/toxcore/events/friend_connection_status.c +++ b/toxcore/events/friend_connection_status.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -73,9 +74,9 @@ static void tox_event_friend_connection_status_pack( const Tox_Event_Friend_Connection_Status *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 2); - msgpack_pack_uint32(mp, event->friend_number); - msgpack_pack_uint32(mp, event->connection_status); + bin_pack_array(mp, 2); + bin_pack_u32(mp, event->friend_number); + bin_pack_u32(mp, event->connection_status); } non_null() @@ -164,7 +165,7 @@ void tox_events_pack_friend_connection_status(const Tox_Events *events, msgpack_ { const uint32_t size = tox_events_get_friend_connection_status_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_friend_connection_status_pack(tox_events_get_friend_connection_status(events, i), mp); diff --git a/toxcore/events/friend_lossless_packet.c b/toxcore/events/friend_lossless_packet.c index b002ad7684..331d579a79 100644 --- a/toxcore/events/friend_lossless_packet.c +++ b/toxcore/events/friend_lossless_packet.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -92,10 +93,9 @@ static void tox_event_friend_lossless_packet_pack( const Tox_Event_Friend_Lossless_Packet *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 2); - msgpack_pack_uint32(mp, event->friend_number); - msgpack_pack_bin(mp, event->data_length); - msgpack_pack_bin_body(mp, event->data, event->data_length); + bin_pack_array(mp, 2); + bin_pack_u32(mp, event->friend_number); + bin_pack_bytes(mp, event->data, event->data_length); } non_null() @@ -183,7 +183,7 @@ void tox_events_pack_friend_lossless_packet(const Tox_Events *events, msgpack_pa { const uint32_t size = tox_events_get_friend_lossless_packet_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_friend_lossless_packet_pack(tox_events_get_friend_lossless_packet(events, i), mp); diff --git a/toxcore/events/friend_lossy_packet.c b/toxcore/events/friend_lossy_packet.c index e4d8d9f4d4..a7049e04d1 100644 --- a/toxcore/events/friend_lossy_packet.c +++ b/toxcore/events/friend_lossy_packet.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -91,10 +92,9 @@ static void tox_event_friend_lossy_packet_pack( const Tox_Event_Friend_Lossy_Packet *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 2); - msgpack_pack_uint32(mp, event->friend_number); - msgpack_pack_bin(mp, event->data_length); - msgpack_pack_bin_body(mp, event->data, event->data_length); + bin_pack_array(mp, 2); + bin_pack_u32(mp, event->friend_number); + bin_pack_bytes(mp, event->data, event->data_length); } non_null() @@ -182,7 +182,7 @@ void tox_events_pack_friend_lossy_packet(const Tox_Events *events, msgpack_packe { const uint32_t size = tox_events_get_friend_lossy_packet_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_friend_lossy_packet_pack(tox_events_get_friend_lossy_packet(events, i), mp); diff --git a/toxcore/events/friend_message.c b/toxcore/events/friend_message.c index 8a685e876a..956ed22e33 100644 --- a/toxcore/events/friend_message.c +++ b/toxcore/events/friend_message.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -105,11 +106,10 @@ static void tox_event_friend_message_pack( const Tox_Event_Friend_Message *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 3); - msgpack_pack_uint32(mp, event->friend_number); - msgpack_pack_uint32(mp, event->type); - msgpack_pack_bin(mp, event->message_length); - msgpack_pack_bin_body(mp, event->message, event->message_length); + bin_pack_array(mp, 3); + bin_pack_u32(mp, event->friend_number); + bin_pack_u32(mp, event->type); + bin_pack_bytes(mp, event->message, event->message_length); } non_null() @@ -197,7 +197,7 @@ void tox_events_pack_friend_message(const Tox_Events *events, msgpack_packer *mp { const uint32_t size = tox_events_get_friend_message_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_friend_message_pack(tox_events_get_friend_message(events, i), mp); diff --git a/toxcore/events/friend_name.c b/toxcore/events/friend_name.c index 1fa79b1cad..0d54514f83 100644 --- a/toxcore/events/friend_name.c +++ b/toxcore/events/friend_name.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -91,10 +92,9 @@ static void tox_event_friend_name_pack( const Tox_Event_Friend_Name *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 2); - msgpack_pack_uint32(mp, event->friend_number); - msgpack_pack_bin(mp, event->name_length); - msgpack_pack_bin_body(mp, event->name, event->name_length); + bin_pack_array(mp, 2); + bin_pack_u32(mp, event->friend_number); + bin_pack_bytes(mp, event->name, event->name_length); } non_null() @@ -181,7 +181,7 @@ void tox_events_pack_friend_name(const Tox_Events *events, msgpack_packer *mp) { const uint32_t size = tox_events_get_friend_name_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_friend_name_pack(tox_events_get_friend_name(events, i), mp); diff --git a/toxcore/events/friend_read_receipt.c b/toxcore/events/friend_read_receipt.c index ea86984bbe..49829032e2 100644 --- a/toxcore/events/friend_read_receipt.c +++ b/toxcore/events/friend_read_receipt.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -70,9 +71,9 @@ static void tox_event_friend_read_receipt_pack( const Tox_Event_Friend_Read_Receipt *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 2); - msgpack_pack_uint32(mp, event->friend_number); - msgpack_pack_uint32(mp, event->message_id); + bin_pack_array(mp, 2); + bin_pack_u32(mp, event->friend_number); + bin_pack_u32(mp, event->message_id); } non_null() @@ -160,7 +161,7 @@ void tox_events_pack_friend_read_receipt(const Tox_Events *events, msgpack_packe { const uint32_t size = tox_events_get_friend_read_receipt_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_friend_read_receipt_pack(tox_events_get_friend_read_receipt(events, i), mp); diff --git a/toxcore/events/friend_request.c b/toxcore/events/friend_request.c index aa3eff3cc1..b1582c44b8 100644 --- a/toxcore/events/friend_request.c +++ b/toxcore/events/friend_request.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -92,11 +93,9 @@ static void tox_event_friend_request_pack( const Tox_Event_Friend_Request *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 2); - msgpack_pack_bin(mp, TOX_PUBLIC_KEY_SIZE); - msgpack_pack_bin_body(mp, event->public_key, TOX_PUBLIC_KEY_SIZE); - msgpack_pack_bin(mp, event->message_length); - msgpack_pack_bin_body(mp, event->message, event->message_length); + bin_pack_array(mp, 2); + bin_pack_bytes(mp, event->public_key, TOX_PUBLIC_KEY_SIZE); + bin_pack_bytes(mp, event->message, event->message_length); } non_null() @@ -183,7 +182,7 @@ void tox_events_pack_friend_request(const Tox_Events *events, msgpack_packer *mp { const uint32_t size = tox_events_get_friend_request_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_friend_request_pack(tox_events_get_friend_request(events, i), mp); diff --git a/toxcore/events/friend_status.c b/toxcore/events/friend_status.c index ff2255d6a2..333bb30624 100644 --- a/toxcore/events/friend_status.c +++ b/toxcore/events/friend_status.c @@ -9,6 +9,7 @@ #include #include "../bin_unpack.h" +#include "../bin_pack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" @@ -71,9 +72,9 @@ static void tox_event_friend_status_pack( const Tox_Event_Friend_Status *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 2); - msgpack_pack_uint32(mp, event->friend_number); - msgpack_pack_uint32(mp, event->connection_status); + bin_pack_array(mp, 2); + bin_pack_u32(mp, event->friend_number); + bin_pack_u32(mp, event->connection_status); } non_null() @@ -160,7 +161,7 @@ void tox_events_pack_friend_status(const Tox_Events *events, msgpack_packer *mp) { const uint32_t size = tox_events_get_friend_status_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_friend_status_pack(tox_events_get_friend_status(events, i), mp); diff --git a/toxcore/events/friend_status_message.c b/toxcore/events/friend_status_message.c index 1e4664399f..4d135f7234 100644 --- a/toxcore/events/friend_status_message.c +++ b/toxcore/events/friend_status_message.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -93,10 +94,9 @@ static void tox_event_friend_status_message_pack( const Tox_Event_Friend_Status_Message *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 2); - msgpack_pack_uint32(mp, event->friend_number); - msgpack_pack_bin(mp, event->status_message_length); - msgpack_pack_bin_body(mp, event->status_message, event->status_message_length); + bin_pack_array(mp, 2); + bin_pack_u32(mp, event->friend_number); + bin_pack_bytes(mp, event->status_message, event->status_message_length); } non_null() @@ -184,7 +184,7 @@ void tox_events_pack_friend_status_message(const Tox_Events *events, msgpack_pac { const uint32_t size = tox_events_get_friend_status_message_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_friend_status_message_pack(tox_events_get_friend_status_message(events, i), mp); diff --git a/toxcore/events/friend_typing.c b/toxcore/events/friend_typing.c index cfeae639b3..69cebf6842 100644 --- a/toxcore/events/friend_typing.c +++ b/toxcore/events/friend_typing.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -69,14 +70,9 @@ static void tox_event_friend_typing_pack( const Tox_Event_Friend_Typing *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 2); - msgpack_pack_uint32(mp, event->friend_number); - - if (event->typing) { - msgpack_pack_true(mp); - } else { - msgpack_pack_false(mp); - } + bin_pack_array(mp, 2); + bin_pack_u32(mp, event->friend_number); + bin_pack_bool(mp, event->typing); } non_null() @@ -163,7 +159,7 @@ void tox_events_pack_friend_typing(const Tox_Events *events, msgpack_packer *mp) { const uint32_t size = tox_events_get_friend_typing_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_friend_typing_pack(tox_events_get_friend_typing(events, i), mp); diff --git a/toxcore/events/self_connection_status.c b/toxcore/events/self_connection_status.c index 0beeaa17ef..1b16aab3b4 100644 --- a/toxcore/events/self_connection_status.c +++ b/toxcore/events/self_connection_status.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_pack.h" #include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -58,8 +59,8 @@ static void tox_event_self_connection_status_pack( const Tox_Event_Self_Connection_Status *event, msgpack_packer *mp) { assert(event != nullptr); - msgpack_pack_array(mp, 1); - msgpack_pack_uint32(mp, event->connection_status); + bin_pack_array(mp, 1); + bin_pack_u32(mp, event->connection_status); } non_null() @@ -146,7 +147,7 @@ void tox_events_pack_self_connection_status(const Tox_Events *events, msgpack_pa { const uint32_t size = tox_events_get_self_connection_status_size(events); - msgpack_pack_array(mp, size); + bin_pack_array(mp, size); for (uint32_t i = 0; i < size; ++i) { tox_event_self_connection_status_pack(tox_events_get_self_connection_status(events, i), mp);