From f3819eb740ab6a2604c11be35aea4a411d4be494 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Thu, 10 Feb 2022 15:28:02 -0500 Subject: [PATCH] Split tox_unpack into two smaller libs tox_unpack is for unpacking Tox types, and bin_unpack is for unpacking ints and binary data. This prevents us from creating dependency cycles due to tox_unpack depending on tox.h --- CMakeLists.txt | 2 + toxcore/BUILD.bazel | 12 +++ toxcore/Makefile.inc | 2 + toxcore/bin_unpack.c | 80 ++++++++++++++++++ toxcore/bin_unpack.h | 20 +++++ toxcore/events/conference_connected.c | 4 +- toxcore/events/conference_invite.c | 5 +- toxcore/events/conference_message.c | 7 +- toxcore/events/conference_peer_list_changed.c | 4 +- toxcore/events/conference_peer_name.c | 8 +- toxcore/events/conference_title.c | 8 +- toxcore/events/file_chunk_request.c | 10 +-- toxcore/events/file_recv.c | 12 +-- toxcore/events/file_recv_chunk.c | 10 +-- toxcore/events/file_recv_control.c | 5 +- toxcore/events/friend_connection_status.c | 3 +- toxcore/events/friend_lossless_packet.c | 6 +- toxcore/events/friend_lossy_packet.c | 6 +- toxcore/events/friend_message.c | 5 +- toxcore/events/friend_name.c | 6 +- toxcore/events/friend_read_receipt.c | 6 +- toxcore/events/friend_request.c | 6 +- toxcore/events/friend_status.c | 3 +- toxcore/events/friend_status_message.c | 6 +- toxcore/events/friend_typing.c | 6 +- toxcore/events/self_connection_status.c | 1 + toxcore/tox_unpack.c | 83 ++----------------- toxcore/tox_unpack.h | 8 -- 28 files changed, 190 insertions(+), 144 deletions(-) create mode 100644 toxcore/bin_unpack.c create mode 100644 toxcore/bin_unpack.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7937fdfbdea..9ee20b205b8 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_unpack.c + toxcore/bin_unpack.h toxcore/tox_events.c toxcore/tox_events.h toxcore/tox_unpack.c diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index ecef1c65ad5..3d9a8ba6588 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -14,6 +14,17 @@ cc_library( visibility = ["//c-toxcore:__subpackages__"], ) +cc_library( + name = "bin_unpack", + srcs = ["bin_unpack.c"], + hdrs = ["bin_unpack.h"], + visibility = ["//c-toxcore:__subpackages__"], + deps = [ + ":ccompat", + "@msgpack-c", + ], +) + cc_library( name = "ccompat", srcs = ["ccompat.c"], @@ -490,6 +501,7 @@ cc_library( hdrs = ["tox_events.h"], visibility = ["//c-toxcore:__subpackages__"], deps = [ + ":bin_unpack", ":ccompat", ":tox_unpack", ":toxcore", diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index 3f51dbe129b..108466635fb 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_unpack.c \ + ../toxcore/bin_unpack.h \ ../toxcore/ccompat.c \ ../toxcore/ccompat.h \ ../toxcore/events/conference_connected.c \ diff --git a/toxcore/bin_unpack.c b/toxcore/bin_unpack.c new file mode 100644 index 00000000000..ee3cd35687b --- /dev/null +++ b/toxcore/bin_unpack.c @@ -0,0 +1,80 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022 The TokTok team. + */ + +#include "bin_unpack.h" + +#include + +#include "ccompat.h" + +bool bin_unpack_bool(bool *val, const msgpack_object *obj) +{ + if (obj->type != MSGPACK_OBJECT_BOOLEAN) { + return false; + } + + *val = obj->via.boolean; + return true; +} + +bool bin_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 bin_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 bin_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 bin_unpack_bytes(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 bin_unpack_bytes_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; +} diff --git a/toxcore/bin_unpack.h b/toxcore/bin_unpack.h new file mode 100644 index 00000000000..b2d0dc4d7ed --- /dev/null +++ b/toxcore/bin_unpack.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022 The TokTok team. + */ + +#ifndef C_TOXCORE_TOXCORE_BIN_UNPACK_H +#define C_TOXCORE_TOXCORE_BIN_UNPACK_H + +#include +#include + +#include "attributes.h" + +non_null() bool bin_unpack_bool(bool *val, const msgpack_object *obj); +non_null() bool bin_unpack_u16(uint16_t *val, const msgpack_object *obj); +non_null() bool bin_unpack_u32(uint32_t *val, const msgpack_object *obj); +non_null() bool bin_unpack_u64(uint64_t *val, const msgpack_object *obj); +non_null() bool bin_unpack_bytes(uint8_t **data, size_t *data_length, const msgpack_object *obj); +non_null() bool bin_unpack_bytes_fixed(uint8_t *data, uint32_t data_length, const msgpack_object *obj); + +#endif // C_TOXCORE_TOXCORE_BIN_UNPACK_H diff --git a/toxcore/events/conference_connected.c b/toxcore/events/conference_connected.c index e11d5c3737d..9e938f32765 100644 --- a/toxcore/events/conference_connected.c +++ b/toxcore/events/conference_connected.c @@ -8,10 +8,10 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** @@ -71,7 +71,7 @@ static bool tox_event_conference_connected_unpack( return false; } - return tox_unpack_u32(&event->conference_number, &obj->via.array.ptr[0]); + return bin_unpack_u32(&event->conference_number, &obj->via.array.ptr[0]); } diff --git a/toxcore/events/conference_invite.c b/toxcore/events/conference_invite.c index 133cbfbb2f9..9481fd0d526 100644 --- a/toxcore/events/conference_invite.c +++ b/toxcore/events/conference_invite.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" @@ -122,9 +123,9 @@ static bool tox_event_conference_invite_unpack( return false; } - return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + return bin_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]); + && bin_unpack_bytes(&event->cookie, &event->cookie_length, &obj->via.array.ptr[2]); } diff --git a/toxcore/events/conference_message.c b/toxcore/events/conference_message.c index 8f392930417..9bf99f4f5cd 100644 --- a/toxcore/events/conference_message.c +++ b/toxcore/events/conference_message.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" @@ -137,10 +138,10 @@ static bool tox_event_conference_message_unpack( 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]) + return bin_unpack_u32(&event->conference_number, &obj->via.array.ptr[0]) + && bin_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]); + && bin_unpack_bytes(&event->message, &event->message_length, &obj->via.array.ptr[3]); } diff --git a/toxcore/events/conference_peer_list_changed.c b/toxcore/events/conference_peer_list_changed.c index b0cc5750165..52781499d0a 100644 --- a/toxcore/events/conference_peer_list_changed.c +++ b/toxcore/events/conference_peer_list_changed.c @@ -8,10 +8,10 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** @@ -73,7 +73,7 @@ static bool tox_event_conference_peer_list_changed_unpack( return false; } - return tox_unpack_u32(&event->conference_number, &obj->via.array.ptr[0]); + return bin_unpack_u32(&event->conference_number, &obj->via.array.ptr[0]); } diff --git a/toxcore/events/conference_peer_name.c b/toxcore/events/conference_peer_name.c index f5d50aee189..ef2fb3ea688 100644 --- a/toxcore/events/conference_peer_name.c +++ b/toxcore/events/conference_peer_name.c @@ -8,10 +8,10 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** @@ -123,9 +123,9 @@ static bool tox_event_conference_peer_name_unpack( 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]); + return bin_unpack_u32(&event->conference_number, &obj->via.array.ptr[0]) + && bin_unpack_u32(&event->peer_number, &obj->via.array.ptr[1]) + && bin_unpack_bytes(&event->name, &event->name_length, &obj->via.array.ptr[2]); } diff --git a/toxcore/events/conference_title.c b/toxcore/events/conference_title.c index 70854af5e05..12a541f0662 100644 --- a/toxcore/events/conference_title.c +++ b/toxcore/events/conference_title.c @@ -8,10 +8,10 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** @@ -122,9 +122,9 @@ static bool tox_event_conference_title_unpack( 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]); + return bin_unpack_u32(&event->conference_number, &obj->via.array.ptr[0]) + && bin_unpack_u32(&event->peer_number, &obj->via.array.ptr[1]) + && bin_unpack_bytes(&event->title, &event->title_length, &obj->via.array.ptr[2]); } diff --git a/toxcore/events/file_chunk_request.c b/toxcore/events/file_chunk_request.c index 3f76e528f09..386f6865cb7 100644 --- a/toxcore/events/file_chunk_request.c +++ b/toxcore/events/file_chunk_request.c @@ -8,10 +8,10 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** @@ -114,10 +114,10 @@ static bool tox_event_file_chunk_request_unpack( 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]); + return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && bin_unpack_u32(&event->file_number, &obj->via.array.ptr[1]) + && bin_unpack_u64(&event->position, &obj->via.array.ptr[2]) + && bin_unpack_u16(&event->length, &obj->via.array.ptr[3]); } diff --git a/toxcore/events/file_recv.c b/toxcore/events/file_recv.c index 6334cbe4fdf..2f02a964bd5 100644 --- a/toxcore/events/file_recv.c +++ b/toxcore/events/file_recv.c @@ -8,10 +8,10 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** @@ -152,11 +152,11 @@ static bool tox_event_file_recv_unpack( 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]); + return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && bin_unpack_u32(&event->file_number, &obj->via.array.ptr[1]) + && bin_unpack_u32(&event->kind, &obj->via.array.ptr[2]) + && bin_unpack_u64(&event->file_size, &obj->via.array.ptr[3]) + && bin_unpack_bytes(&event->filename, &event->filename_length, &obj->via.array.ptr[4]); } diff --git a/toxcore/events/file_recv_chunk.c b/toxcore/events/file_recv_chunk.c index 4272337563f..fa787e20173 100644 --- a/toxcore/events/file_recv_chunk.c +++ b/toxcore/events/file_recv_chunk.c @@ -8,10 +8,10 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** @@ -137,10 +137,10 @@ static bool tox_event_file_recv_chunk_unpack( 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]); + return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && bin_unpack_u32(&event->file_number, &obj->via.array.ptr[1]) + && bin_unpack_u64(&event->position, &obj->via.array.ptr[2]) + && bin_unpack_bytes(&event->data, &event->data_length, &obj->via.array.ptr[3]); } diff --git a/toxcore/events/file_recv_control.c b/toxcore/events/file_recv_control.c index 4ebadc9f7fd..e822787cd40 100644 --- a/toxcore/events/file_recv_control.c +++ b/toxcore/events/file_recv_control.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" @@ -100,8 +101,8 @@ static bool tox_event_file_recv_control_unpack( 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]) + return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && bin_unpack_u32(&event->file_number, &obj->via.array.ptr[1]) && tox_unpack_file_control(&event->control, &obj->via.array.ptr[2]); } diff --git a/toxcore/events/friend_connection_status.c b/toxcore/events/friend_connection_status.c index 4578ca12260..bdee5f71d04 100644 --- a/toxcore/events/friend_connection_status.c +++ b/toxcore/events/friend_connection_status.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" @@ -87,7 +88,7 @@ static bool tox_event_friend_connection_status_unpack( return false; } - return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) && tox_unpack_connection(&event->connection_status, &obj->via.array.ptr[1]); } diff --git a/toxcore/events/friend_lossless_packet.c b/toxcore/events/friend_lossless_packet.c index dcf850883a6..b002ad76842 100644 --- a/toxcore/events/friend_lossless_packet.c +++ b/toxcore/events/friend_lossless_packet.c @@ -8,10 +8,10 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** @@ -108,8 +108,8 @@ static bool tox_event_friend_lossless_packet_unpack( 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]); + return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && bin_unpack_bytes(&event->data, &event->data_length, &obj->via.array.ptr[1]); } diff --git a/toxcore/events/friend_lossy_packet.c b/toxcore/events/friend_lossy_packet.c index 53b47b3fa15..e4d8d9f4d45 100644 --- a/toxcore/events/friend_lossy_packet.c +++ b/toxcore/events/friend_lossy_packet.c @@ -8,10 +8,10 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** @@ -107,8 +107,8 @@ static bool tox_event_friend_lossy_packet_unpack( 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]); + return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && bin_unpack_bytes(&event->data, &event->data_length, &obj->via.array.ptr[1]); } diff --git a/toxcore/events/friend_message.c b/toxcore/events/friend_message.c index 608ad97c220..8a685e876a9 100644 --- a/toxcore/events/friend_message.c +++ b/toxcore/events/friend_message.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" @@ -121,9 +122,9 @@ static bool tox_event_friend_message_unpack( return false; } - return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + return bin_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]); + && bin_unpack_bytes(&event->message, &event->message_length, &obj->via.array.ptr[2]); } diff --git a/toxcore/events/friend_name.c b/toxcore/events/friend_name.c index b56b10715ab..1fa79b1cad5 100644 --- a/toxcore/events/friend_name.c +++ b/toxcore/events/friend_name.c @@ -8,10 +8,10 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** @@ -107,8 +107,8 @@ static bool tox_event_friend_name_unpack( 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]); + return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && bin_unpack_bytes(&event->name, &event->name_length, &obj->via.array.ptr[1]); } diff --git a/toxcore/events/friend_read_receipt.c b/toxcore/events/friend_read_receipt.c index 68ebf086c0a..ea86984bbeb 100644 --- a/toxcore/events/friend_read_receipt.c +++ b/toxcore/events/friend_read_receipt.c @@ -8,10 +8,10 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** @@ -85,8 +85,8 @@ static bool tox_event_friend_read_receipt_unpack( 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]); + return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && bin_unpack_u32(&event->message_id, &obj->via.array.ptr[1]); } diff --git a/toxcore/events/friend_request.c b/toxcore/events/friend_request.c index d997f424ed7..aa3eff3cc1e 100644 --- a/toxcore/events/friend_request.c +++ b/toxcore/events/friend_request.c @@ -8,10 +8,10 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** @@ -109,8 +109,8 @@ static bool tox_event_friend_request_unpack( 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]); + return bin_unpack_bytes_fixed(event->public_key, TOX_PUBLIC_KEY_SIZE, &obj->via.array.ptr[0]) + && bin_unpack_bytes(&event->message, &event->message_length, &obj->via.array.ptr[1]); } diff --git a/toxcore/events/friend_status.c b/toxcore/events/friend_status.c index ccebdac70c9..ff2255d6a2b 100644 --- a/toxcore/events/friend_status.c +++ b/toxcore/events/friend_status.c @@ -7,6 +7,7 @@ #include #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" @@ -85,7 +86,7 @@ static bool tox_event_friend_status_unpack( return false; } - return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) && tox_unpack_user_status(&event->connection_status, &obj->via.array.ptr[1]); } diff --git a/toxcore/events/friend_status_message.c b/toxcore/events/friend_status_message.c index 1658697175b..1e4664399f1 100644 --- a/toxcore/events/friend_status_message.c +++ b/toxcore/events/friend_status_message.c @@ -8,10 +8,10 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** @@ -109,8 +109,8 @@ static bool tox_event_friend_status_message_unpack( 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]); + return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && bin_unpack_bytes(&event->status_message, &event->status_message_length, &obj->via.array.ptr[1]); } diff --git a/toxcore/events/friend_typing.c b/toxcore/events/friend_typing.c index ce0b4c9c8bd..cfeae639b3e 100644 --- a/toxcore/events/friend_typing.c +++ b/toxcore/events/friend_typing.c @@ -8,10 +8,10 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" -#include "../tox_unpack.h" /***************************************************** @@ -89,8 +89,8 @@ static bool tox_event_friend_typing_unpack( return false; } - return tox_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) - && tox_unpack_bool(&event->typing, &obj->via.array.ptr[1]); + return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0]) + && bin_unpack_bool(&event->typing, &obj->via.array.ptr[1]); } diff --git a/toxcore/events/self_connection_status.c b/toxcore/events/self_connection_status.c index 482d7d31f42..0beeaa17ef8 100644 --- a/toxcore/events/self_connection_status.c +++ b/toxcore/events/self_connection_status.c @@ -8,6 +8,7 @@ #include #include +#include "../bin_unpack.h" #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" diff --git a/toxcore/tox_unpack.c b/toxcore/tox_unpack.c index 1c311b03ea0..2c5a81cf138 100644 --- a/toxcore/tox_unpack.c +++ b/toxcore/tox_unpack.c @@ -5,85 +5,16 @@ #include "tox_unpack.h" #include +#include +#include "bin_unpack.h" #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)) { + if (!bin_unpack_u32(&u32, obj)) { return false; } @@ -95,7 +26,7 @@ bool tox_unpack_connection(Tox_Connection *val, const msgpack_object *obj) { uint32_t u32; - if (!tox_unpack_u32(&u32, obj)) { + if (!bin_unpack_u32(&u32, obj)) { return false; } @@ -107,7 +38,7 @@ bool tox_unpack_file_control(Tox_File_Control *val, const msgpack_object *obj) { uint32_t u32; - if (!tox_unpack_u32(&u32, obj)) { + if (!bin_unpack_u32(&u32, obj)) { return false; } @@ -119,7 +50,7 @@ bool tox_unpack_message_type(Tox_Message_Type *val, const msgpack_object *obj) { uint32_t u32; - if (!tox_unpack_u32(&u32, obj)) { + if (!bin_unpack_u32(&u32, obj)) { return false; } @@ -131,7 +62,7 @@ bool tox_unpack_user_status(Tox_User_Status *val, const msgpack_object *obj) { uint32_t u32; - if (!tox_unpack_u32(&u32, obj)) { + if (!bin_unpack_u32(&u32, obj)) { return false; } diff --git a/toxcore/tox_unpack.h b/toxcore/tox_unpack.h index 4ef3be09cfd..d3638b6bbc5 100644 --- a/toxcore/tox_unpack.h +++ b/toxcore/tox_unpack.h @@ -6,18 +6,10 @@ #define C_TOXCORE_TOXCORE_TOX_UNPACK_H #include -#include #include "attributes.h" #include "tox.h" -non_null() bool tox_unpack_bool(bool *val, const msgpack_object *obj); -non_null() bool tox_unpack_u16(uint16_t *val, const msgpack_object *obj); -non_null() bool tox_unpack_u32(uint32_t *val, const msgpack_object *obj); -non_null() bool tox_unpack_u64(uint64_t *val, const msgpack_object *obj); -non_null() bool tox_unpack_bin(uint8_t **data, size_t *data_length, const msgpack_object *obj); -non_null() bool tox_unpack_bin_fixed(uint8_t *data, uint32_t data_length, const msgpack_object *obj); - non_null() bool tox_unpack_conference_type(Tox_Conference_Type *val, const msgpack_object *obj); non_null() bool tox_unpack_connection(Tox_Connection *val, const msgpack_object *obj); non_null() bool tox_unpack_file_control(Tox_File_Control *val, const msgpack_object *obj);