Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: Add wrapper library for msgpack pack functions #2023

Merged
merged 1 commit into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/events/events_alloc.c
toxcore/events/events_alloc.h
toxcore/events/self_connection_status.c
toxcore/bin_pack.c
toxcore/bin_pack.h
toxcore/bin_unpack.c
toxcore/bin_unpack.h
toxcore/tox_events.c
Expand Down
12 changes: 12 additions & 0 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ cc_library(
visibility = ["//c-toxcore:__subpackages__"],
)

cc_library(
name = "bin_pack",
srcs = ["bin_pack.c"],
hdrs = ["bin_pack.h"],
visibility = ["//c-toxcore:__subpackages__"],
deps = [
":ccompat",
"@msgpack-c",
],
)

cc_library(
name = "bin_unpack",
srcs = ["bin_unpack.c"],
Expand Down Expand Up @@ -502,6 +513,7 @@ cc_library(
hdrs = ["tox_events.h"],
visibility = ["//c-toxcore:__subpackages__"],
deps = [
":bin_pack",
":bin_unpack",
":ccompat",
":tox_unpack",
Expand Down
2 changes: 2 additions & 0 deletions toxcore/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ libtoxcore_la_include_HEADERS = \
libtoxcore_la_includedir = $(includedir)/tox

libtoxcore_la_SOURCES = ../toxcore/attributes.h \
../toxcore/bin_pack.c \
../toxcore/bin_pack.h \
../toxcore/bin_unpack.c \
../toxcore/bin_unpack.h \
../toxcore/ccompat.c \
Expand Down
42 changes: 42 additions & 0 deletions toxcore/bin_pack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2022 The TokTok team.
*/

#include "bin_pack.h"

#include <msgpack.h>

void bin_pack_array(msgpack_packer *mp, size_t size)
{
msgpack_pack_array(mp, size);
}

void bin_pack_bool(msgpack_packer *mp, bool val)
{
if (val) {
msgpack_pack_true(mp);
} else {
msgpack_pack_false(mp);
}
}

void bin_pack_u16(msgpack_packer *mp, uint16_t val)
{
msgpack_pack_uint16(mp, val);
}

void bin_pack_u32(msgpack_packer *mp, uint32_t val)
{
msgpack_pack_uint32(mp, val);
}

void bin_pack_u64(msgpack_packer *mp, uint64_t val)
{
msgpack_pack_uint64(mp, val);
}

void bin_pack_bytes(msgpack_packer *mp, const uint8_t *data, size_t length)
{
msgpack_pack_bin(mp, length);
msgpack_pack_bin_body(mp, data, length);
}
21 changes: 21 additions & 0 deletions toxcore/bin_pack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2022 The TokTok team.
*/

#ifndef C_TOXCORE_TOXCORE_BIN_PACK_H
#define C_TOXCORE_TOXCORE_BIN_PACK_H

#include <msgpack.h>
#include <stdbool.h>
#include <stdint.h>

#include "attributes.h"

non_null() void bin_pack_array(msgpack_packer *mp, size_t size);
non_null() void bin_pack_bool(msgpack_packer *mp, bool val);
non_null() void bin_pack_u16(msgpack_packer *mp, uint16_t val);
non_null() void bin_pack_u32(msgpack_packer *mp, uint32_t val);
non_null() void bin_pack_u64(msgpack_packer *mp, uint64_t val);
non_null() void bin_pack_bytes(msgpack_packer *mp, const uint8_t *data, size_t length);

#endif // C_TOXCORE_TOXCORE_BIN_PACK_H
7 changes: 4 additions & 3 deletions toxcore/events/conference_connected.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string.h>

#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
Expand Down Expand Up @@ -57,8 +58,8 @@ static void tox_event_conference_connected_pack(
const Tox_Event_Conference_Connected *event, msgpack_packer *mp)
{
assert(event != nullptr);
msgpack_pack_array(mp, 1);
msgpack_pack_uint32(mp, event->conference_number);
bin_pack_array(mp, 1);
bin_pack_u32(mp, event->conference_number);
}

non_null()
Expand Down Expand Up @@ -145,7 +146,7 @@ void tox_events_pack_conference_connected(const Tox_Events *events, msgpack_pack
{
const uint32_t size = tox_events_get_conference_connected_size(events);

msgpack_pack_array(mp, size);
bin_pack_array(mp, size);

for (uint32_t i = 0; i < size; ++i) {
tox_event_conference_connected_pack(tox_events_get_conference_connected(events, i), mp);
Expand Down
12 changes: 6 additions & 6 deletions toxcore/events/conference_invite.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string.h>

#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
Expand Down Expand Up @@ -106,11 +107,10 @@ static void tox_event_conference_invite_pack(
const Tox_Event_Conference_Invite *event, msgpack_packer *mp)
{
assert(event != nullptr);
msgpack_pack_array(mp, 3);
msgpack_pack_uint32(mp, event->friend_number);
msgpack_pack_uint32(mp, event->type);
msgpack_pack_bin(mp, event->cookie_length);
msgpack_pack_bin_body(mp, event->cookie, event->cookie_length);
bin_pack_array(mp, 3);
bin_pack_u32(mp, event->friend_number);
bin_pack_u32(mp, event->type);
bin_pack_bytes(mp, event->cookie, event->cookie_length);
}

non_null()
Expand Down Expand Up @@ -198,7 +198,7 @@ void tox_events_pack_conference_invite(const Tox_Events *events, msgpack_packer
{
const uint32_t size = tox_events_get_conference_invite_size(events);

msgpack_pack_array(mp, size);
bin_pack_array(mp, size);

for (uint32_t i = 0; i < size; ++i) {
tox_event_conference_invite_pack(tox_events_get_conference_invite(events, i), mp);
Expand Down
14 changes: 7 additions & 7 deletions toxcore/events/conference_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string.h>

#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
Expand Down Expand Up @@ -120,12 +121,11 @@ static void tox_event_conference_message_pack(
const Tox_Event_Conference_Message *event, msgpack_packer *mp)
{
assert(event != nullptr);
msgpack_pack_array(mp, 4);
msgpack_pack_uint32(mp, event->conference_number);
msgpack_pack_uint32(mp, event->peer_number);
msgpack_pack_uint32(mp, event->type);
msgpack_pack_bin(mp, event->message_length);
msgpack_pack_bin_body(mp, event->message, event->message_length);
bin_pack_array(mp, 4);
bin_pack_u32(mp, event->conference_number);
bin_pack_u32(mp, event->peer_number);
bin_pack_u32(mp, event->type);
bin_pack_bytes(mp, event->message, event->message_length);
}

non_null()
Expand Down Expand Up @@ -214,7 +214,7 @@ void tox_events_pack_conference_message(const Tox_Events *events, msgpack_packer
{
const uint32_t size = tox_events_get_conference_message_size(events);

msgpack_pack_array(mp, size);
bin_pack_array(mp, size);

for (uint32_t i = 0; i < size; ++i) {
tox_event_conference_message_pack(tox_events_get_conference_message(events, i), mp);
Expand Down
7 changes: 4 additions & 3 deletions toxcore/events/conference_peer_list_changed.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string.h>

#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
Expand Down Expand Up @@ -59,8 +60,8 @@ static void tox_event_conference_peer_list_changed_pack(
const Tox_Event_Conference_Peer_List_Changed *event, msgpack_packer *mp)
{
assert(event != nullptr);
msgpack_pack_array(mp, 1);
msgpack_pack_uint32(mp, event->conference_number);
bin_pack_array(mp, 1);
bin_pack_u32(mp, event->conference_number);
}

non_null()
Expand Down Expand Up @@ -150,7 +151,7 @@ void tox_events_pack_conference_peer_list_changed(const Tox_Events *events, msgp
{
const uint32_t size = tox_events_get_conference_peer_list_changed_size(events);

msgpack_pack_array(mp, size);
bin_pack_array(mp, size);

for (uint32_t i = 0; i < size; ++i) {
tox_event_conference_peer_list_changed_pack(tox_events_get_conference_peer_list_changed(events, i), mp);
Expand Down
12 changes: 6 additions & 6 deletions toxcore/events/conference_peer_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string.h>

#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
Expand Down Expand Up @@ -106,11 +107,10 @@ static void tox_event_conference_peer_name_pack(
const Tox_Event_Conference_Peer_Name *event, msgpack_packer *mp)
{
assert(event != nullptr);
msgpack_pack_array(mp, 3);
msgpack_pack_uint32(mp, event->conference_number);
msgpack_pack_uint32(mp, event->peer_number);
msgpack_pack_bin(mp, event->name_length);
msgpack_pack_bin_body(mp, event->name, event->name_length);
bin_pack_array(mp, 3);
bin_pack_u32(mp, event->conference_number);
bin_pack_u32(mp, event->peer_number);
bin_pack_bytes(mp, event->name, event->name_length);
}

non_null()
Expand Down Expand Up @@ -199,7 +199,7 @@ void tox_events_pack_conference_peer_name(const Tox_Events *events, msgpack_pack
{
const uint32_t size = tox_events_get_conference_peer_name_size(events);

msgpack_pack_array(mp, size);
bin_pack_array(mp, size);

for (uint32_t i = 0; i < size; ++i) {
tox_event_conference_peer_name_pack(tox_events_get_conference_peer_name(events, i), mp);
Expand Down
12 changes: 6 additions & 6 deletions toxcore/events/conference_title.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string.h>

#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
Expand Down Expand Up @@ -105,11 +106,10 @@ static void tox_event_conference_title_pack(
const Tox_Event_Conference_Title *event, msgpack_packer *mp)
{
assert(event != nullptr);
msgpack_pack_array(mp, 3);
msgpack_pack_uint32(mp, event->conference_number);
msgpack_pack_uint32(mp, event->peer_number);
msgpack_pack_bin(mp, event->title_length);
msgpack_pack_bin_body(mp, event->title, event->title_length);
bin_pack_array(mp, 3);
bin_pack_u32(mp, event->conference_number);
bin_pack_u32(mp, event->peer_number);
bin_pack_bytes(mp, event->title, event->title_length);
}

non_null()
Expand Down Expand Up @@ -197,7 +197,7 @@ void tox_events_pack_conference_title(const Tox_Events *events, msgpack_packer *
{
const uint32_t size = tox_events_get_conference_title_size(events);

msgpack_pack_array(mp, size);
bin_pack_array(mp, size);

for (uint32_t i = 0; i < size; ++i) {
tox_event_conference_title_pack(tox_events_get_conference_title(events, i), mp);
Expand Down
13 changes: 7 additions & 6 deletions toxcore/events/file_chunk_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string.h>

#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
Expand Down Expand Up @@ -97,11 +98,11 @@ static void tox_event_file_chunk_request_pack(
const Tox_Event_File_Chunk_Request *event, msgpack_packer *mp)
{
assert(event != nullptr);
msgpack_pack_array(mp, 4);
msgpack_pack_uint32(mp, event->friend_number);
msgpack_pack_uint32(mp, event->file_number);
msgpack_pack_uint64(mp, event->position);
msgpack_pack_uint16(mp, event->length);
bin_pack_array(mp, 4);
bin_pack_u32(mp, event->friend_number);
bin_pack_u32(mp, event->file_number);
bin_pack_u64(mp, event->position);
bin_pack_u16(mp, event->length);
}

non_null()
Expand Down Expand Up @@ -190,7 +191,7 @@ void tox_events_pack_file_chunk_request(const Tox_Events *events, msgpack_packer
{
const uint32_t size = tox_events_get_file_chunk_request_size(events);

msgpack_pack_array(mp, size);
bin_pack_array(mp, size);

for (uint32_t i = 0; i < size; ++i) {
tox_event_file_chunk_request_pack(tox_events_get_file_chunk_request(events, i), mp);
Expand Down
16 changes: 8 additions & 8 deletions toxcore/events/file_recv.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string.h>

#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
Expand Down Expand Up @@ -133,13 +134,12 @@ static void tox_event_file_recv_pack(
const Tox_Event_File_Recv *event, msgpack_packer *mp)
{
assert(event != nullptr);
msgpack_pack_array(mp, 5);
msgpack_pack_uint32(mp, event->friend_number);
msgpack_pack_uint32(mp, event->file_number);
msgpack_pack_uint32(mp, event->kind);
msgpack_pack_uint64(mp, event->file_size);
msgpack_pack_bin(mp, event->filename_length);
msgpack_pack_bin_body(mp, event->filename, event->filename_length);
bin_pack_array(mp, 5);
bin_pack_u32(mp, event->friend_number);
bin_pack_u32(mp, event->file_number);
bin_pack_u32(mp, event->kind);
bin_pack_u64(mp, event->file_size);
bin_pack_bytes(mp, event->filename, event->filename_length);
}

non_null()
Expand Down Expand Up @@ -229,7 +229,7 @@ void tox_events_pack_file_recv(const Tox_Events *events, msgpack_packer *mp)
{
const uint32_t size = tox_events_get_file_recv_size(events);

msgpack_pack_array(mp, size);
bin_pack_array(mp, size);

for (uint32_t i = 0; i < size; ++i) {
tox_event_file_recv_pack(tox_events_get_file_recv(events, i), mp);
Expand Down
Loading