From cb8b9bbabfc8b669226cddcbe985b82909fdad22 Mon Sep 17 00:00:00 2001 From: iphydf Date: Mon, 7 Feb 2022 18:58:34 +0000 Subject: [PATCH] feat: Add unpacker functions for events structures. --- CMakeLists.txt | 4 +- auto_tests/tox_dispatch_test.c | 23 ++- toxcore/BUILD.bazel | 13 ++ toxcore/Makefile.inc | 2 + toxcore/events/conference_connected.c | 58 ++++++-- toxcore/events/conference_invite.c | 60 ++++++-- toxcore/events/conference_message.c | 60 ++++++-- toxcore/events/conference_peer_list_changed.c | 50 ++++++- toxcore/events/conference_peer_name.c | 57 +++++-- toxcore/events/conference_title.c | 57 +++++-- toxcore/events/events_alloc.h | 24 +++ toxcore/events/file_chunk_request.c | 64 ++++++-- toxcore/events/file_recv.c | 63 ++++++-- toxcore/events/file_recv_chunk.c | 60 ++++++-- toxcore/events/file_recv_control.c | 55 +++++-- toxcore/events/friend_connection_status.c | 52 ++++++- toxcore/events/friend_lossless_packet.c | 54 +++++-- toxcore/events/friend_lossy_packet.c | 54 +++++-- toxcore/events/friend_message.c | 57 +++++-- toxcore/events/friend_name.c | 54 +++++-- toxcore/events/friend_read_receipt.c | 52 ++++++- toxcore/events/friend_request.c | 56 +++++-- toxcore/events/friend_status.c | 52 ++++++- toxcore/events/friend_status_message.c | 54 +++++-- toxcore/events/friend_typing.c | 62 ++++++-- toxcore/events/self_connection_status.c | 49 +++++- toxcore/tox_events.c | 109 ++++++++++++-- toxcore/tox_events.h | 6 +- toxcore/tox_unpack.c | 140 ++++++++++++++++++ toxcore/tox_unpack.h | 26 ++++ 30 files changed, 1306 insertions(+), 221 deletions(-) create mode 100644 toxcore/tox_unpack.c create mode 100644 toxcore/tox_unpack.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 76ff7176..d2dfb4da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -272,7 +272,9 @@ set(toxcore_SOURCES ${toxcore_SOURCES} toxcore/events/events_alloc.h toxcore/events/self_connection_status.c toxcore/tox_events.c - toxcore/tox_events.h) + toxcore/tox_events.h + toxcore/tox_unpack.c + toxcore/tox_unpack.h) set(toxcore_API_HEADERS ${toxcore_API_HEADERS} ${toxcore_SOURCE_DIR}/toxcore/tox_events.h^tox) set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${MSGPACK_LIBRARIES}) set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} msgpack) diff --git a/auto_tests/tox_dispatch_test.c b/auto_tests/tox_dispatch_test.c index 0dc348bf..77bc6e4c 100644 --- a/auto_tests/tox_dispatch_test.c +++ b/auto_tests/tox_dispatch_test.c @@ -44,10 +44,31 @@ static void dump_events(const char *path, const Tox_Events *events) static void print_events(Tox_Events *events) { - if (tox_events_bytes_size(events) > 24) { + const uint32_t size = tox_events_bytes_size(events); + + if (size > 24) { tox_events_print(events); } + uint8_t *bytes = (uint8_t *)malloc(size); + ck_assert(bytes != nullptr); + + tox_events_get_bytes(events, bytes); + + Tox_Events *events_copy = tox_events_load(bytes, size); + ck_assert(events_copy != nullptr); + free(bytes); + + if (!tox_events_equal(events, events_copy)) { + printf("serialised and deserialised events are not equal:\n"); + printf("a = "); + tox_events_print(events); + printf("b = "); + tox_events_print(events_copy); + abort(); + } + + tox_events_free(events_copy); tox_events_free(events); } diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index 0f9e7917..e0cef0db 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -460,6 +460,18 @@ cc_library( ], ) +cc_library( + name = "tox_unpack", + srcs = ["tox_unpack.c"], + hdrs = ["tox_unpack.h"], + visibility = ["//c-toxcore:__subpackages__"], + deps = [ + ":ccompat", + ":toxcore", + "@msgpack-c", + ], +) + cc_library( name = "tox_events", srcs = ["tox_events.c"] + glob([ @@ -470,6 +482,7 @@ cc_library( visibility = ["//c-toxcore:__subpackages__"], deps = [ ":ccompat", + ":tox_unpack", ":toxcore", "@msgpack-c", ], diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index fb1812df..32142c40 100644 --- a/toxcore/Makefile.inc +++ b/toxcore/Makefile.inc @@ -60,6 +60,8 @@ libtoxcore_la_SOURCES = ../toxcore/ccompat.h \ ../toxcore/tox_dispatch.c \ ../toxcore/tox_events.h \ ../toxcore/tox_events.c \ + ../toxcore/tox_unpack.h \ + ../toxcore/tox_unpack.c \ ../toxcore/tox_private.h \ ../toxcore/tox_api.c \ ../toxcore/util.h \ diff --git a/toxcore/events/conference_connected.c b/toxcore/events/conference_connected.c index 33262288..86c24d6c 100644 --- a/toxcore/events/conference_connected.c +++ b/toxcore/events/conference_connected.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -24,13 +25,6 @@ struct Tox_Event_Conference_Connected { uint32_t conference_number; }; -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); -} - static void tox_event_conference_connected_construct(Tox_Event_Conference_Connected *conference_connected) { *conference_connected = (Tox_Event_Conference_Connected) { @@ -42,19 +36,39 @@ static void tox_event_conference_connected_destruct(Tox_Event_Conference_Connect return; } -static void tox_event_conference_connected_set_conference_number(Tox_Event_Conference_Connected *conference_connected, - uint32_t conference_number) +static void tox_event_conference_connected_set_conference_number( + Tox_Event_Conference_Connected *conference_connected, uint32_t conference_number) { assert(conference_connected != nullptr); conference_connected->conference_number = conference_number; } -uint32_t tox_event_conference_connected_get_conference_number(const Tox_Event_Conference_Connected - *conference_connected) +uint32_t tox_event_conference_connected_get_conference_number( + const Tox_Event_Conference_Connected *conference_connected) { assert(conference_connected != nullptr); return conference_connected->conference_number; } +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); +} + +static bool tox_event_conference_connected_unpack( + Tox_Event_Conference_Connected *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 1) { + return false; + } + + return tox_unpack_u32(&event->conference_number, &obj->via.array.ptr[0]); +} + /***************************************************** * @@ -132,6 +146,28 @@ void tox_events_pack_conference_connected(const Tox_Events *events, msgpack_pack } } +bool tox_events_unpack_conference_connected(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Conference_Connected *event = tox_events_add_conference_connected(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_conference_connected_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + + /***************************************************** * diff --git a/toxcore/events/conference_invite.c b/toxcore/events/conference_invite.c index 130968ea..4d6005c3 100644 --- a/toxcore/events/conference_invite.c +++ b/toxcore/events/conference_invite.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -27,16 +28,6 @@ struct Tox_Event_Conference_Invite { size_t cookie_length; }; -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); -} - static void tox_event_conference_invite_construct(Tox_Event_Conference_Invite *conference_invite) { *conference_invite = (Tox_Event_Conference_Invite) { @@ -73,8 +64,7 @@ Tox_Conference_Type tox_event_conference_invite_get_type(const Tox_Event_Confere } static bool tox_event_conference_invite_set_cookie(Tox_Event_Conference_Invite *conference_invite, - const uint8_t *cookie, - size_t cookie_length) + const uint8_t *cookie, size_t cookie_length) { assert(conference_invite != nullptr); @@ -105,6 +95,31 @@ const uint8_t *tox_event_conference_invite_get_cookie(const Tox_Event_Conference return conference_invite->cookie; } +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); +} + +static bool tox_event_conference_invite_unpack( + Tox_Event_Conference_Invite *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 3) { + return false; + } + + return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && tox_unpack_conference_type(&event->type, &obj->via.array.ptr[1]) + && tox_unpack_bin(&event->cookie, &event->cookie_length, &obj->via.array.ptr[2]); +} + /***************************************************** * @@ -181,6 +196,27 @@ void tox_events_pack_conference_invite(const Tox_Events *events, msgpack_packer } } +bool tox_events_unpack_conference_invite(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Conference_Invite *event = tox_events_add_conference_invite(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_conference_invite_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/conference_message.c b/toxcore/events/conference_message.c index 3a2199a0..9f47fcb4 100644 --- a/toxcore/events/conference_message.c +++ b/toxcore/events/conference_message.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -28,17 +29,6 @@ struct Tox_Event_Conference_Message { size_t message_length; }; -static void tox_event_conference_message_pack(const Tox_Event_Conference_Message *event, msgpack_packer *mp) -{ - assert(event != nullptr); - msgpack_pack_array(mp, 5); - 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); -} - static void tox_event_conference_message_construct(Tox_Event_Conference_Message *conference_message) { *conference_message = (Tox_Event_Conference_Message) { @@ -118,6 +108,33 @@ const uint8_t *tox_event_conference_message_get_message(const Tox_Event_Conferen return conference_message->message; } +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); +} + +static bool tox_event_conference_message_unpack( + Tox_Event_Conference_Message *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 4) { + return false; + } + + return tox_unpack_u32(&event->conference_number, &obj->via.array.ptr[0]) + && tox_unpack_u32(&event->peer_number, &obj->via.array.ptr[1]) + && tox_unpack_message_type(&event->type, &obj->via.array.ptr[2]) + && tox_unpack_bin(&event->message, &event->message_length, &obj->via.array.ptr[3]); +} + /***************************************************** * @@ -194,6 +211,27 @@ void tox_events_pack_conference_message(const Tox_Events *events, msgpack_packer } } +bool tox_events_unpack_conference_message(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Conference_Message *event = tox_events_add_conference_message(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_conference_message_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/conference_peer_list_changed.c b/toxcore/events/conference_peer_list_changed.c index c7b5b7c6..3d66b835 100644 --- a/toxcore/events/conference_peer_list_changed.c +++ b/toxcore/events/conference_peer_list_changed.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -24,14 +25,6 @@ struct Tox_Event_Conference_Peer_List_Changed { uint32_t conference_number; }; -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); -} - static void tox_event_conference_peer_list_changed_construct(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed) { @@ -58,6 +51,26 @@ uint32_t tox_event_conference_peer_list_changed_get_conference_number(const Tox_ return conference_peer_list_changed->conference_number; } +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); +} + +static bool tox_event_conference_peer_list_changed_unpack( + Tox_Event_Conference_Peer_List_Changed *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 1) { + return false; + } + + return tox_unpack_u32(&event->conference_number, &obj->via.array.ptr[0]); +} + /***************************************************** * @@ -138,6 +151,27 @@ void tox_events_pack_conference_peer_list_changed(const Tox_Events *events, msgp } } +bool tox_events_unpack_conference_peer_list_changed(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Conference_Peer_List_Changed *event = tox_events_add_conference_peer_list_changed(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_conference_peer_list_changed_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/conference_peer_name.c b/toxcore/events/conference_peer_name.c index e93c5e37..ac4cfcd8 100644 --- a/toxcore/events/conference_peer_name.c +++ b/toxcore/events/conference_peer_name.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -27,16 +28,6 @@ struct Tox_Event_Conference_Peer_Name { size_t name_length; }; -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); -} - static void tox_event_conference_peer_name_construct(Tox_Event_Conference_Peer_Name *conference_peer_name) { *conference_peer_name = (Tox_Event_Conference_Peer_Name) { @@ -105,6 +96,31 @@ const uint8_t *tox_event_conference_peer_name_get_name(const Tox_Event_Conferenc return conference_peer_name->name; } +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); +} + +static bool tox_event_conference_peer_name_unpack( + Tox_Event_Conference_Peer_Name *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 3) { + return false; + } + + return tox_unpack_u32(&event->conference_number, &obj->via.array.ptr[0]) + && tox_unpack_u32(&event->peer_number, &obj->via.array.ptr[1]) + && tox_unpack_bin(&event->name, &event->name_length, &obj->via.array.ptr[2]); +} + /***************************************************** * @@ -182,6 +198,27 @@ void tox_events_pack_conference_peer_name(const Tox_Events *events, msgpack_pack } } +bool tox_events_unpack_conference_peer_name(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Conference_Peer_Name *event = tox_events_add_conference_peer_name(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_conference_peer_name_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/conference_title.c b/toxcore/events/conference_title.c index a3a49220..cd331254 100644 --- a/toxcore/events/conference_title.c +++ b/toxcore/events/conference_title.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -27,16 +28,6 @@ struct Tox_Event_Conference_Title { size_t title_length; }; -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); -} - static void tox_event_conference_title_construct(Tox_Event_Conference_Title *conference_title) { *conference_title = (Tox_Event_Conference_Title) { @@ -104,6 +95,31 @@ const uint8_t *tox_event_conference_title_get_title(const Tox_Event_Conference_T return conference_title->title; } +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); +} + +static bool tox_event_conference_title_unpack( + Tox_Event_Conference_Title *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 3) { + return false; + } + + return tox_unpack_u32(&event->conference_number, &obj->via.array.ptr[0]) + && tox_unpack_u32(&event->peer_number, &obj->via.array.ptr[1]) + && tox_unpack_bin(&event->title, &event->title_length, &obj->via.array.ptr[2]); +} + /***************************************************** * @@ -180,6 +196,27 @@ void tox_events_pack_conference_title(const Tox_Events *events, msgpack_packer * } } +bool tox_events_unpack_conference_title(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Conference_Title *event = tox_events_add_conference_title(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_conference_title_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/events_alloc.h b/toxcore/events/events_alloc.h index ecffbeec..a839d436 100644 --- a/toxcore/events/events_alloc.h +++ b/toxcore/events/events_alloc.h @@ -172,6 +172,30 @@ void tox_events_pack_self_connection_status(const Tox_Events *events, msgpack_pa void tox_events_pack(const Tox_Events *events, msgpack_packer *mp); +bool tox_events_unpack_conference_connected(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_conference_invite(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_conference_message(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_conference_peer_list_changed(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_conference_peer_name(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_conference_title(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_file_chunk_request(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_file_recv_chunk(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_file_recv_control(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_file_recv(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_friend_connection_status(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_friend_lossless_packet(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_friend_lossy_packet(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_friend_message(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_friend_name(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_friend_read_receipt(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_friend_request(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_friend_status_message(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_friend_status(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_friend_typing(Tox_Events *events, const msgpack_object *obj); +bool tox_events_unpack_self_connection_status(Tox_Events *events, const msgpack_object *obj); + +bool tox_events_unpack(Tox_Events *events, const msgpack_object *obj); + Tox_Events_State *tox_events_alloc(void *user_data); #ifdef __cplusplus diff --git a/toxcore/events/file_chunk_request.c b/toxcore/events/file_chunk_request.c index de671fa9..2aeef255 100644 --- a/toxcore/events/file_chunk_request.c +++ b/toxcore/events/file_chunk_request.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -24,19 +25,9 @@ struct Tox_Event_File_Chunk_Request { uint32_t friend_number; uint32_t file_number; uint64_t position; - size_t length; + uint16_t length; }; -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); -} - static void tox_event_file_chunk_request_construct(Tox_Event_File_Chunk_Request *file_chunk_request) { *file_chunk_request = (Tox_Event_File_Chunk_Request) { @@ -84,17 +75,43 @@ uint64_t tox_event_file_chunk_request_get_position(const Tox_Event_File_Chunk_Re return file_chunk_request->position; } -static void tox_event_file_chunk_request_set_length(Tox_Event_File_Chunk_Request *file_chunk_request, size_t length) +static void tox_event_file_chunk_request_set_length(Tox_Event_File_Chunk_Request *file_chunk_request, uint16_t length) { assert(file_chunk_request != nullptr); file_chunk_request->length = length; } -size_t tox_event_file_chunk_request_get_length(const Tox_Event_File_Chunk_Request *file_chunk_request) +uint16_t tox_event_file_chunk_request_get_length(const Tox_Event_File_Chunk_Request *file_chunk_request) { assert(file_chunk_request != nullptr); return file_chunk_request->length; } +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); +} + +static bool tox_event_file_chunk_request_unpack( + Tox_Event_File_Chunk_Request *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 4) { + return false; + } + + return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && tox_unpack_u32(&event->file_number, &obj->via.array.ptr[1]) + && tox_unpack_u64(&event->position, &obj->via.array.ptr[2]) + && tox_unpack_u16(&event->length, &obj->via.array.ptr[3]); +} + /***************************************************** * @@ -171,6 +188,27 @@ void tox_events_pack_file_chunk_request(const Tox_Events *events, msgpack_packer } } +bool tox_events_unpack_file_chunk_request(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_File_Chunk_Request *event = tox_events_add_file_chunk_request(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_file_chunk_request_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/file_recv.c b/toxcore/events/file_recv.c index eb9064d3..be0d2cfb 100644 --- a/toxcore/events/file_recv.c +++ b/toxcore/events/file_recv.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -29,18 +30,6 @@ struct Tox_Event_File_Recv { size_t filename_length; }; -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); -} - static void tox_event_file_recv_construct(Tox_Event_File_Recv *file_recv) { *file_recv = (Tox_Event_File_Recv) { @@ -132,6 +121,35 @@ const uint8_t *tox_event_file_recv_get_filename(const Tox_Event_File_Recv *file_ return file_recv->filename; } +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); +} + +static bool tox_event_file_recv_unpack( + Tox_Event_File_Recv *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 5) { + return false; + } + + return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && tox_unpack_u32(&event->file_number, &obj->via.array.ptr[1]) + && tox_unpack_u32(&event->kind, &obj->via.array.ptr[2]) + && tox_unpack_u64(&event->file_size, &obj->via.array.ptr[3]) + && tox_unpack_bin(&event->filename, &event->filename_length, &obj->via.array.ptr[4]); +} + /***************************************************** * @@ -208,6 +226,27 @@ void tox_events_pack_file_recv(const Tox_Events *events, msgpack_packer *mp) } } +bool tox_events_unpack_file_recv(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_File_Recv *event = tox_events_add_file_recv(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_file_recv_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/file_recv_chunk.c b/toxcore/events/file_recv_chunk.c index 2a8cbd75..0449e57a 100644 --- a/toxcore/events/file_recv_chunk.c +++ b/toxcore/events/file_recv_chunk.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -28,17 +29,6 @@ struct Tox_Event_File_Recv_Chunk { size_t data_length; }; -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); -} - static void tox_event_file_recv_chunk_construct(Tox_Event_File_Recv_Chunk *file_recv_chunk) { *file_recv_chunk = (Tox_Event_File_Recv_Chunk) { @@ -118,6 +108,33 @@ const uint8_t *tox_event_file_recv_chunk_get_data(const Tox_Event_File_Recv_Chun return file_recv_chunk->data; } +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); +} + +static bool tox_event_file_recv_chunk_unpack( + Tox_Event_File_Recv_Chunk *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 4) { + return false; + } + + return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && tox_unpack_u32(&event->file_number, &obj->via.array.ptr[1]) + && tox_unpack_u64(&event->position, &obj->via.array.ptr[2]) + && tox_unpack_bin(&event->data, &event->data_length, &obj->via.array.ptr[3]); +} + /***************************************************** * @@ -194,6 +211,27 @@ void tox_events_pack_file_recv_chunk(const Tox_Events *events, msgpack_packer *m } } +bool tox_events_unpack_file_recv_chunk(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_File_Recv_Chunk *event = tox_events_add_file_recv_chunk(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_file_recv_chunk_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/file_recv_control.c b/toxcore/events/file_recv_control.c index 8200406a..10d716fe 100644 --- a/toxcore/events/file_recv_control.c +++ b/toxcore/events/file_recv_control.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -26,15 +27,6 @@ struct Tox_Event_File_Recv_Control { Tox_File_Control control; }; -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); -} - static void tox_event_file_recv_control_construct(Tox_Event_File_Recv_Control *file_recv_control) { *file_recv_control = (Tox_Event_File_Recv_Control) { @@ -82,6 +74,30 @@ Tox_File_Control tox_event_file_recv_control_get_control(const Tox_Event_File_Re return file_recv_control->control; } +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); +} + +static bool tox_event_file_recv_control_unpack( + Tox_Event_File_Recv_Control *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 3) { + return false; + } + + return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && tox_unpack_u32(&event->file_number, &obj->via.array.ptr[1]) + && tox_unpack_file_control(&event->control, &obj->via.array.ptr[2]); +} + /***************************************************** * @@ -158,6 +174,27 @@ void tox_events_pack_file_recv_control(const Tox_Events *events, msgpack_packer } } +bool tox_events_unpack_file_recv_control(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_File_Recv_Control *event = tox_events_add_file_recv_control(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_file_recv_control_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/friend_connection_status.c b/toxcore/events/friend_connection_status.c index e5c6e2bf..0408e995 100644 --- a/toxcore/events/friend_connection_status.c +++ b/toxcore/events/friend_connection_status.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -25,14 +26,6 @@ struct Tox_Event_Friend_Connection_Status { Tox_Connection connection_status; }; -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); -} - static void tox_event_friend_connection_status_construct(Tox_Event_Friend_Connection_Status *friend_connection_status) { *friend_connection_status = (Tox_Event_Friend_Connection_Status) { @@ -70,6 +63,28 @@ Tox_Connection tox_event_friend_connection_status_get_connection_status(const To return friend_connection_status->connection_status; } +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); +} + +static bool tox_event_friend_connection_status_unpack( + Tox_Event_Friend_Connection_Status *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) { + return false; + } + + return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && tox_unpack_connection(&event->connection_status, &obj->via.array.ptr[1]); +} + /***************************************************** * @@ -148,6 +163,27 @@ void tox_events_pack_friend_connection_status(const Tox_Events *events, msgpack_ } } +bool tox_events_unpack_friend_connection_status(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Friend_Connection_Status *event = tox_events_add_friend_connection_status(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_friend_connection_status_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/friend_lossless_packet.c b/toxcore/events/friend_lossless_packet.c index 6be919e8..09bf9ad9 100644 --- a/toxcore/events/friend_lossless_packet.c +++ b/toxcore/events/friend_lossless_packet.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -26,15 +27,6 @@ struct Tox_Event_Friend_Lossless_Packet { size_t data_length; }; -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); -} - static void tox_event_friend_lossless_packet_construct(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) { *friend_lossless_packet = (Tox_Event_Friend_Lossless_Packet) { @@ -91,6 +83,29 @@ const uint8_t *tox_event_friend_lossless_packet_get_data(const Tox_Event_Friend_ return friend_lossless_packet->data; } +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); +} + +static bool tox_event_friend_lossless_packet_unpack( + Tox_Event_Friend_Lossless_Packet *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) { + return false; + } + + return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && tox_unpack_bin(&event->data, &event->data_length, &obj->via.array.ptr[1]); +} + /***************************************************** * @@ -168,6 +183,27 @@ void tox_events_pack_friend_lossless_packet(const Tox_Events *events, msgpack_pa } } +bool tox_events_unpack_friend_lossless_packet(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Friend_Lossless_Packet *event = tox_events_add_friend_lossless_packet(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_friend_lossless_packet_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/friend_lossy_packet.c b/toxcore/events/friend_lossy_packet.c index 0bde8ac9..544db1ab 100644 --- a/toxcore/events/friend_lossy_packet.c +++ b/toxcore/events/friend_lossy_packet.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -26,15 +27,6 @@ struct Tox_Event_Friend_Lossy_Packet { size_t data_length; }; -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); -} - static void tox_event_friend_lossy_packet_construct(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet) { *friend_lossy_packet = (Tox_Event_Friend_Lossy_Packet) { @@ -90,6 +82,29 @@ const uint8_t *tox_event_friend_lossy_packet_get_data(const Tox_Event_Friend_Los return friend_lossy_packet->data; } +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); +} + +static bool tox_event_friend_lossy_packet_unpack( + Tox_Event_Friend_Lossy_Packet *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) { + return false; + } + + return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && tox_unpack_bin(&event->data, &event->data_length, &obj->via.array.ptr[1]); +} + /***************************************************** * @@ -167,6 +182,27 @@ void tox_events_pack_friend_lossy_packet(const Tox_Events *events, msgpack_packe } } +bool tox_events_unpack_friend_lossy_packet(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Friend_Lossy_Packet *event = tox_events_add_friend_lossy_packet(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_friend_lossy_packet_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/friend_message.c b/toxcore/events/friend_message.c index 9c1357a4..4960640f 100644 --- a/toxcore/events/friend_message.c +++ b/toxcore/events/friend_message.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -27,16 +28,6 @@ struct Tox_Event_Friend_Message { size_t message_length; }; -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); -} - static void tox_event_friend_message_construct(Tox_Event_Friend_Message *friend_message) { *friend_message = (Tox_Event_Friend_Message) { @@ -103,6 +94,31 @@ const uint8_t *tox_event_friend_message_get_message(const Tox_Event_Friend_Messa return friend_message->message; } +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); +} + +static bool tox_event_friend_message_unpack( + Tox_Event_Friend_Message *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 3) { + return false; + } + + return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && tox_unpack_message_type(&event->type, &obj->via.array.ptr[1]) + && tox_unpack_bin(&event->message, &event->message_length, &obj->via.array.ptr[2]); +} + /***************************************************** * @@ -179,6 +195,27 @@ void tox_events_pack_friend_message(const Tox_Events *events, msgpack_packer *mp } } +bool tox_events_unpack_friend_message(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Friend_Message *event = tox_events_add_friend_message(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_friend_message_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/friend_name.c b/toxcore/events/friend_name.c index d6bec033..486d28aa 100644 --- a/toxcore/events/friend_name.c +++ b/toxcore/events/friend_name.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -26,15 +27,6 @@ struct Tox_Event_Friend_Name { size_t name_length; }; -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); -} - static void tox_event_friend_name_construct(Tox_Event_Friend_Name *friend_name) { *friend_name = (Tox_Event_Friend_Name) { @@ -90,6 +82,29 @@ const uint8_t *tox_event_friend_name_get_name(const Tox_Event_Friend_Name *frien return friend_name->name; } +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); +} + +static bool tox_event_friend_name_unpack( + Tox_Event_Friend_Name *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) { + return false; + } + + return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && tox_unpack_bin(&event->name, &event->name_length, &obj->via.array.ptr[1]); +} + /***************************************************** * @@ -166,6 +181,27 @@ void tox_events_pack_friend_name(const Tox_Events *events, msgpack_packer *mp) } } +bool tox_events_unpack_friend_name(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Friend_Name *event = tox_events_add_friend_name(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_friend_name_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/friend_read_receipt.c b/toxcore/events/friend_read_receipt.c index 3ae80995..6f0ff19c 100644 --- a/toxcore/events/friend_read_receipt.c +++ b/toxcore/events/friend_read_receipt.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -25,14 +26,6 @@ struct Tox_Event_Friend_Read_Receipt { uint32_t message_id; }; -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); -} - static void tox_event_friend_read_receipt_construct(Tox_Event_Friend_Read_Receipt *friend_read_receipt) { *friend_read_receipt = (Tox_Event_Friend_Read_Receipt) { @@ -68,6 +61,28 @@ uint32_t tox_event_friend_read_receipt_get_message_id(const Tox_Event_Friend_Rea return friend_read_receipt->message_id; } +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); +} + +static bool tox_event_friend_read_receipt_unpack( + Tox_Event_Friend_Read_Receipt *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) { + return false; + } + + return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && tox_unpack_u32(&event->message_id, &obj->via.array.ptr[1]); +} + /***************************************************** * @@ -145,6 +160,27 @@ void tox_events_pack_friend_read_receipt(const Tox_Events *events, msgpack_packe } } +bool tox_events_unpack_friend_read_receipt(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Friend_Read_Receipt *event = tox_events_add_friend_read_receipt(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_friend_read_receipt_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/friend_request.c b/toxcore/events/friend_request.c index 95044bca..938f7fdf 100644 --- a/toxcore/events/friend_request.c +++ b/toxcore/events/friend_request.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -26,16 +27,6 @@ struct Tox_Event_Friend_Request { size_t message_length; }; -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); -} - static void tox_event_friend_request_construct(Tox_Event_Friend_Request *friend_request) { *friend_request = (Tox_Event_Friend_Request) { @@ -92,6 +83,30 @@ const uint8_t *tox_event_friend_request_get_message(const Tox_Event_Friend_Reque return friend_request->message; } +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); +} + +static bool tox_event_friend_request_unpack( + Tox_Event_Friend_Request *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) { + return false; + } + + return tox_unpack_bin_fixed(event->public_key, TOX_PUBLIC_KEY_SIZE, &obj->via.array.ptr[0]) + && tox_unpack_bin(&event->message, &event->message_length, &obj->via.array.ptr[1]); +} + /***************************************************** * @@ -168,6 +183,27 @@ void tox_events_pack_friend_request(const Tox_Events *events, msgpack_packer *mp } } +bool tox_events_unpack_friend_request(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Friend_Request *event = tox_events_add_friend_request(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_friend_request_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/friend_status.c b/toxcore/events/friend_status.c index 7bb5e3fe..d996de6d 100644 --- a/toxcore/events/friend_status.c +++ b/toxcore/events/friend_status.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -25,14 +26,6 @@ struct Tox_Event_Friend_Status { Tox_User_Status connection_status; }; -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); -} - static void tox_event_friend_status_construct(Tox_Event_Friend_Status *friend_status) { *friend_status = (Tox_Event_Friend_Status) { @@ -68,6 +61,28 @@ Tox_User_Status tox_event_friend_status_get_connection_status(const Tox_Event_Fr return friend_status->connection_status; } +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); +} + +static bool tox_event_friend_status_unpack( + Tox_Event_Friend_Status *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) { + return false; + } + + return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && tox_unpack_user_status(&event->connection_status, &obj->via.array.ptr[1]); +} + /***************************************************** * @@ -144,6 +159,27 @@ void tox_events_pack_friend_status(const Tox_Events *events, msgpack_packer *mp) } } +bool tox_events_unpack_friend_status(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Friend_Status *event = tox_events_add_friend_status(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_friend_status_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/friend_status_message.c b/toxcore/events/friend_status_message.c index d071ef16..eebcd9f0 100644 --- a/toxcore/events/friend_status_message.c +++ b/toxcore/events/friend_status_message.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -26,15 +27,6 @@ struct Tox_Event_Friend_Status_Message { size_t status_message_length; }; -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); -} - static void tox_event_friend_status_message_construct(Tox_Event_Friend_Status_Message *friend_status_message) { *friend_status_message = (Tox_Event_Friend_Status_Message) { @@ -92,6 +84,29 @@ const uint8_t *tox_event_friend_status_message_get_status_message(const Tox_Even return friend_status_message->status_message; } +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); +} + +static bool tox_event_friend_status_message_unpack( + Tox_Event_Friend_Status_Message *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) { + return false; + } + + return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && tox_unpack_bin(&event->status_message, &event->status_message_length, &obj->via.array.ptr[1]); +} + /***************************************************** * @@ -169,6 +184,27 @@ void tox_events_pack_friend_status_message(const Tox_Events *events, msgpack_pac } } +bool tox_events_unpack_friend_status_message(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Friend_Status_Message *event = tox_events_add_friend_status_message(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_friend_status_message_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/friend_typing.c b/toxcore/events/friend_typing.c index fdea3318..1f6f766d 100644 --- a/toxcore/events/friend_typing.c +++ b/toxcore/events/friend_typing.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -25,19 +26,6 @@ struct Tox_Event_Friend_Typing { bool typing; }; -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); - } -} - static void tox_event_friend_typing_construct(Tox_Event_Friend_Typing *friend_typing) { *friend_typing = (Tox_Event_Friend_Typing) { @@ -72,6 +60,33 @@ bool tox_event_friend_typing_get_typing(const Tox_Event_Friend_Typing *friend_ty return friend_typing->typing; } +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); + } +} + +static bool tox_event_friend_typing_unpack( + Tox_Event_Friend_Typing *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) { + return false; + } + + return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && tox_unpack_bool(&event->typing, &obj->via.array.ptr[1]); +} + /***************************************************** * @@ -148,6 +163,27 @@ void tox_events_pack_friend_typing(const Tox_Events *events, msgpack_packer *mp) } } +bool tox_events_unpack_friend_typing(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Friend_Typing *event = tox_events_add_friend_typing(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_friend_typing_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/events/self_connection_status.c b/toxcore/events/self_connection_status.c index 9b85eb58..0cab1a0d 100644 --- a/toxcore/events/self_connection_status.c +++ b/toxcore/events/self_connection_status.c @@ -11,6 +11,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -24,13 +25,6 @@ struct Tox_Event_Self_Connection_Status { Tox_Connection connection_status; }; -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); -} - static void tox_event_self_connection_status_construct(Tox_Event_Self_Connection_Status *self_connection_status) { *self_connection_status = (Tox_Event_Self_Connection_Status) { @@ -55,6 +49,26 @@ Tox_Connection tox_event_self_connection_status_get_connection_status(const Tox_ return self_connection_status->connection_status; } +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); +} + +static bool tox_event_self_connection_status_unpack( + Tox_Event_Self_Connection_Status *event, const msgpack_object *obj) +{ + assert(event != nullptr); + + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 1) { + return false; + } + + return tox_unpack_connection(&event->connection_status, &obj->via.array.ptr[0]); +} + /***************************************************** * @@ -132,6 +146,27 @@ void tox_events_pack_self_connection_status(const Tox_Events *events, msgpack_pa } } +bool tox_events_unpack_self_connection_status(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY) { + return false; + } + + for (uint32_t i = 0; i < obj->via.array.size; ++i) { + Tox_Event_Self_Connection_Status *event = tox_events_add_self_connection_status(events); + + if (event == nullptr) { + return false; + } + + if (!tox_event_self_connection_status_unpack(event, &obj->via.array.ptr[i])) { + return false; + } + } + + return true; +} + /***************************************************** * diff --git a/toxcore/tox_events.c b/toxcore/tox_events.c index 7b9c4d28..bbc876bd 100644 --- a/toxcore/tox_events.c +++ b/toxcore/tox_events.c @@ -84,6 +84,35 @@ void tox_events_pack(const Tox_Events *events, msgpack_packer *mp) tox_events_pack_self_connection_status(events, mp); } +bool tox_events_unpack(Tox_Events *events, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 21) { + return false; + } + + return tox_events_unpack_conference_connected(events, &obj->via.array.ptr[0]) + && tox_events_unpack_conference_invite(events, &obj->via.array.ptr[1]) + && tox_events_unpack_conference_message(events, &obj->via.array.ptr[2]) + && tox_events_unpack_conference_peer_list_changed(events, &obj->via.array.ptr[3]) + && tox_events_unpack_conference_peer_name(events, &obj->via.array.ptr[4]) + && tox_events_unpack_conference_title(events, &obj->via.array.ptr[5]) + && tox_events_unpack_file_chunk_request(events, &obj->via.array.ptr[6]) + && tox_events_unpack_file_recv_chunk(events, &obj->via.array.ptr[7]) + && tox_events_unpack_file_recv_control(events, &obj->via.array.ptr[8]) + && tox_events_unpack_file_recv(events, &obj->via.array.ptr[9]) + && tox_events_unpack_friend_connection_status(events, &obj->via.array.ptr[10]) + && tox_events_unpack_friend_lossless_packet(events, &obj->via.array.ptr[11]) + && tox_events_unpack_friend_lossy_packet(events, &obj->via.array.ptr[12]) + && tox_events_unpack_friend_message(events, &obj->via.array.ptr[13]) + && tox_events_unpack_friend_name(events, &obj->via.array.ptr[14]) + && tox_events_unpack_friend_read_receipt(events, &obj->via.array.ptr[15]) + && tox_events_unpack_friend_request(events, &obj->via.array.ptr[16]) + && tox_events_unpack_friend_status_message(events, &obj->via.array.ptr[17]) + && tox_events_unpack_friend_status(events, &obj->via.array.ptr[18]) + && tox_events_unpack_friend_typing(events, &obj->via.array.ptr[19]) + && tox_events_unpack_self_connection_status(events, &obj->via.array.ptr[20]); +} + static int count_bytes(void *data, const char *buf, size_t len) { uint32_t *count = (uint32_t *)data; @@ -117,24 +146,82 @@ void tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes) tox_events_pack(events, &mp); } -void tox_events_print(const Tox_Events *events) +Tox_Events *tox_events_load(const uint8_t *bytes, uint32_t bytes_size) { - msgpack_sbuffer sbuf; - msgpack_sbuffer_init(&sbuf); + msgpack_unpacked msg; + size_t offset = 0; + msgpack_unpacked_init(&msg); + const msgpack_unpack_return result = msgpack_unpack_next(&msg, (const char *)bytes, bytes_size, &offset); + + if (result != MSGPACK_UNPACK_SUCCESS) { + msgpack_unpacked_destroy(&msg); + return nullptr; + } + + Tox_Events *events = (Tox_Events *)calloc(1, sizeof(Tox_Events)); + + if (events == nullptr) { + msgpack_unpacked_destroy(&msg); + return nullptr; + } + + *events = (Tox_Events) { + nullptr + }; + + if (!tox_events_unpack(events, &msg.data)) { + tox_events_free(events); + msgpack_unpacked_destroy(&msg); + return nullptr; + } + + msgpack_unpacked_destroy(&msg); + return events; +} + +static bool tox_events_to_object(const Tox_Events *events, msgpack_unpacked *msg, msgpack_sbuffer *sbuf) +{ + msgpack_sbuffer_init(sbuf); msgpack_packer mp; - msgpack_packer_init(&mp, &sbuf, msgpack_sbuffer_write); + msgpack_packer_init(&mp, sbuf, msgpack_sbuffer_write); tox_events_pack(events, &mp); - msgpack_zone mempool; - msgpack_zone_init(&mempool, 2048); + size_t offset = 0; + msgpack_unpacked_init(msg); + const msgpack_unpack_return result = msgpack_unpack_next(msg, sbuf->data, sbuf->size, &offset); - msgpack_object deserialized; - msgpack_unpack(sbuf.data, sbuf.size, nullptr, &mempool, &deserialized); - msgpack_object_print(stdout, deserialized); + return result == MSGPACK_UNPACK_SUCCESS; +} + +void tox_events_print(const Tox_Events *events) +{ + msgpack_sbuffer sbuf; + msgpack_unpacked msg; + tox_events_to_object(events, &msg, &sbuf); + msgpack_object_print(stdout, msg.data); fputc('\n', stdout); - - msgpack_zone_destroy(&mempool); + msgpack_unpacked_destroy(&msg); msgpack_sbuffer_destroy(&sbuf); } + +bool tox_events_equal(const Tox_Events *a, const Tox_Events *b) +{ + msgpack_sbuffer sbuf_a = {0}; + msgpack_unpacked msg_a = {nullptr}; + msgpack_sbuffer sbuf_b = {0}; + msgpack_unpacked msg_b = {nullptr}; + + const bool success = tox_events_to_object(a, &msg_a, &sbuf_a) + && tox_events_to_object(b, &msg_b, &sbuf_b); + + const bool ret = success && msgpack_object_equal(msg_a.data, msg_b.data); + + msgpack_unpacked_destroy(&msg_b); + msgpack_sbuffer_destroy(&sbuf_a); + msgpack_unpacked_destroy(&msg_a); + msgpack_sbuffer_destroy(&sbuf_b); + + return ret; +} diff --git a/toxcore/tox_events.h b/toxcore/tox_events.h index 7a44c49c..9e58bb64 100644 --- a/toxcore/tox_events.h +++ b/toxcore/tox_events.h @@ -62,7 +62,7 @@ uint32_t tox_event_conference_title_get_peer_number( const Tox_Event_Conference_Title *conference_title); typedef struct Tox_Event_File_Chunk_Request Tox_Event_File_Chunk_Request; -size_t tox_event_file_chunk_request_get_length( +uint16_t tox_event_file_chunk_request_get_length( const Tox_Event_File_Chunk_Request *file_chunk_request); uint32_t tox_event_file_chunk_request_get_file_number( const Tox_Event_File_Chunk_Request *file_chunk_request); @@ -309,6 +309,10 @@ void tox_events_free(Tox_Events *events); uint32_t tox_events_bytes_size(const Tox_Events *events); void tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes); +Tox_Events *tox_events_load(const uint8_t *bytes, uint32_t bytes_size); + +bool tox_events_equal(const Tox_Events *a, const Tox_Events *b); + void tox_events_print(const Tox_Events *events); #ifdef __cplusplus diff --git a/toxcore/tox_unpack.c b/toxcore/tox_unpack.c new file mode 100644 index 00000000..1c311b03 --- /dev/null +++ b/toxcore/tox_unpack.c @@ -0,0 +1,140 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022 The TokTok team. + */ + +#include "tox_unpack.h" + +#include + +#include "ccompat.h" + +bool tox_unpack_bool(bool *val, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_BOOLEAN) { + return false; + } + + *val = obj->via.boolean; + return true; +} + +bool tox_unpack_u16(uint16_t *val, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_POSITIVE_INTEGER || obj->via.u64 > UINT16_MAX) { + return false; + } + + *val = (uint16_t)obj->via.u64; + return true; +} + +bool tox_unpack_u32(uint32_t *val, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_POSITIVE_INTEGER || obj->via.u64 > UINT32_MAX) { + return false; + } + + *val = (uint32_t)obj->via.u64; + return true; +} + +bool tox_unpack_u64(uint64_t *val, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_POSITIVE_INTEGER) { + return false; + } + + *val = obj->via.u64; + return true; +} + +bool tox_unpack_bin(uint8_t **data_ptr, size_t *data_length_ptr, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_BIN) { + return false; + } + + const uint32_t data_length = obj->via.bin.size; + uint8_t *const data = (uint8_t *)malloc(data_length); + + if (data == nullptr) { + return false; + } + + memcpy(data, obj->via.bin.ptr, data_length); + + *data_ptr = data; + *data_length_ptr = data_length; + return true; +} + +bool tox_unpack_bin_fixed(uint8_t *data, uint32_t data_length, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_BIN || obj->via.bin.size != data_length) { + return false; + } + + memcpy(data, obj->via.bin.ptr, data_length); + + return true; +} + +bool tox_unpack_conference_type(Tox_Conference_Type *val, const msgpack_object *obj) +{ + uint32_t u32; + + if (!tox_unpack_u32(&u32, obj)) { + return false; + } + + *val = (Tox_Conference_Type)u32; + return true; +} + +bool tox_unpack_connection(Tox_Connection *val, const msgpack_object *obj) +{ + uint32_t u32; + + if (!tox_unpack_u32(&u32, obj)) { + return false; + } + + *val = (Tox_Connection)u32; + return true; +} + +bool tox_unpack_file_control(Tox_File_Control *val, const msgpack_object *obj) +{ + uint32_t u32; + + if (!tox_unpack_u32(&u32, obj)) { + return false; + } + + *val = (Tox_File_Control)u32; + return true; +} + +bool tox_unpack_message_type(Tox_Message_Type *val, const msgpack_object *obj) +{ + uint32_t u32; + + if (!tox_unpack_u32(&u32, obj)) { + return false; + } + + *val = (Tox_Message_Type)u32; + return true; +} + +bool tox_unpack_user_status(Tox_User_Status *val, const msgpack_object *obj) +{ + uint32_t u32; + + if (!tox_unpack_u32(&u32, obj)) { + return false; + } + + *val = (Tox_User_Status)u32; + return true; +} diff --git a/toxcore/tox_unpack.h b/toxcore/tox_unpack.h new file mode 100644 index 00000000..40e9b0df --- /dev/null +++ b/toxcore/tox_unpack.h @@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022 The TokTok team. + */ + +#ifndef C_TOXCORE_TOXCORE_TOX_UNPACK_H +#define C_TOXCORE_TOXCORE_TOX_UNPACK_H + +#include +#include + +#include "tox.h" + +bool tox_unpack_bool(bool *val, const msgpack_object *obj); +bool tox_unpack_u16(uint16_t *val, const msgpack_object *obj); +bool tox_unpack_u32(uint32_t *val, const msgpack_object *obj); +bool tox_unpack_u64(uint64_t *val, const msgpack_object *obj); +bool tox_unpack_bin(uint8_t **data, size_t *data_length, const msgpack_object *obj); +bool tox_unpack_bin_fixed(uint8_t *data, uint32_t data_length, const msgpack_object *obj); + +bool tox_unpack_conference_type(Tox_Conference_Type *val, const msgpack_object *obj); +bool tox_unpack_connection(Tox_Connection *val, const msgpack_object *obj); +bool tox_unpack_file_control(Tox_File_Control *val, const msgpack_object *obj); +bool tox_unpack_message_type(Tox_Message_Type *val, const msgpack_object *obj); +bool tox_unpack_user_status(Tox_User_Status *val, const msgpack_object *obj); + +#endif // C_TOXCORE_TOXCORE_TOX_UNPACK_H