Skip to content

Commit

Permalink
Split tox_unpack into two smaller libs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JFreegman committed Feb 10, 2022
1 parent 6dc1656 commit a0a317d
Show file tree
Hide file tree
Showing 28 changed files with 191 additions and 144 deletions.
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_unpack.c
toxcore/bin_unpack.h
toxcore/tox_events.c
toxcore/tox_events.h
toxcore/tox_unpack.c
Expand Down
13 changes: 13 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_unpack",
srcs = ["bin_unpack.c"],
hdrs = ["bin_unpack.h"],
visibility = ["//c-toxcore:__subpackages__"],
deps = [
":ccompat",
"@msgpack-c",
],
)

cc_library(
name = "ccompat",
srcs = ["ccompat.c"],
Expand Down Expand Up @@ -475,6 +486,7 @@ cc_library(
hdrs = ["tox_unpack.h"],
visibility = ["//c-toxcore:__subpackages__"],
deps = [
":bin_unpack",
":ccompat",
":toxcore",
"@msgpack-c",
Expand All @@ -490,6 +502,7 @@ cc_library(
hdrs = ["tox_events.h"],
visibility = ["//c-toxcore:__subpackages__"],
deps = [
":bin_unpack",
":ccompat",
":tox_unpack",
":toxcore",
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_unpack.c \
../toxcore/bin_unpack.h \
../toxcore/ccompat.c \
../toxcore/ccompat.h \
../toxcore/events/conference_connected.c \
Expand Down
80 changes: 80 additions & 0 deletions toxcore/bin_unpack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2022 The TokTok team.
*/

#include "bin_unpack.h"

#include <msgpack.h>

#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;
}
20 changes: 20 additions & 0 deletions toxcore/bin_unpack.h
Original file line number Diff line number Diff line change
@@ -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 <msgpack.h>
#include <stdint.h>

#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
4 changes: 2 additions & 2 deletions toxcore/events/conference_connected.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include <stdlib.h>
#include <string.h>

#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"


/*****************************************************
Expand Down Expand Up @@ -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]);
}


Expand Down
5 changes: 3 additions & 2 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_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
#include "../tox_events.h"
Expand Down Expand Up @@ -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]);
}


Expand Down
7 changes: 4 additions & 3 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_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
#include "../tox_events.h"
Expand Down Expand Up @@ -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]);
}


Expand Down
4 changes: 2 additions & 2 deletions toxcore/events/conference_peer_list_changed.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include <stdlib.h>
#include <string.h>

#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"


/*****************************************************
Expand Down Expand Up @@ -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]);
}


Expand Down
8 changes: 4 additions & 4 deletions toxcore/events/conference_peer_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include <stdlib.h>
#include <string.h>

#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"


/*****************************************************
Expand Down Expand Up @@ -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]);
}


Expand Down
8 changes: 4 additions & 4 deletions toxcore/events/conference_title.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include <stdlib.h>
#include <string.h>

#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"


/*****************************************************
Expand Down Expand Up @@ -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]);
}


Expand Down
10 changes: 5 additions & 5 deletions toxcore/events/file_chunk_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include <stdlib.h>
#include <string.h>

#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"


/*****************************************************
Expand Down Expand Up @@ -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]);
}


Expand Down
12 changes: 6 additions & 6 deletions toxcore/events/file_recv.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include <stdlib.h>
#include <string.h>

#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"


/*****************************************************
Expand Down Expand Up @@ -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]);
}


Expand Down
10 changes: 5 additions & 5 deletions toxcore/events/file_recv_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include <stdlib.h>
#include <string.h>

#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"


/*****************************************************
Expand Down Expand Up @@ -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]);
}


Expand Down
5 changes: 3 additions & 2 deletions toxcore/events/file_recv_control.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_unpack.h"
#include "../ccompat.h"
#include "../tox.h"
#include "../tox_events.h"
Expand Down Expand Up @@ -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]);
}

Expand Down
Loading

0 comments on commit a0a317d

Please sign in to comment.