Skip to content

Commit

Permalink
refactor: Use enum-specific pack functions for enum values.
Browse files Browse the repository at this point in the history
It's more obvious this way.
  • Loading branch information
iphydf committed Jan 15, 2024
1 parent afc4724 commit e2c01e4
Show file tree
Hide file tree
Showing 49 changed files with 207 additions and 109 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ set(toxcore_SOURCES
toxcore/tox.h
toxcore/tox_private.c
toxcore/tox_private.h
toxcore/tox_pack.c
toxcore/tox_pack.h
toxcore/tox_unpack.c
toxcore/tox_unpack.h
toxcore/util.c
Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
50123098ff16637a19253e4ea24c0b7bc7d1ef35f6e7c9f2783bddd303a6c6f3 /usr/local/bin/tox-bootstrapd
0b904988d79b9576bb88c6c7316d107b5a61bd6119a0992ebd7c1fa43db70abf /usr/local/bin/tox-bootstrapd
46 changes: 35 additions & 11 deletions other/event_tooling/generate_event_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,31 @@ std::string bin_pack_name_from_type(const std::string& type) {
return "bin_pack_u8";
} else if (type == "bool") {
return "bin_pack_bool";
// only unpack is special TODO(Green-Sky): should we change that?
//} else if (type == "Tox_User_Status") {
//return "tox_pack_user_status";
//} else if (type == "Tox_Conference_Type") {
//return "tox_pack_conference_type";
} else if (type == "Tox_User_Status") {
return "tox_user_status_pack";
} else if (type == "Tox_Conference_Type") {
return "tox_conference_type_pack";
} else if (type == "Tox_Message_Type") {
return "tox_message_type_pack";
} else if (type == "Tox_File_Control") {
return "tox_file_control_pack";
} else if (type == "Tox_Connection") {
return "tox_connection_pack";
} else if (type == "Tox_Group_Privacy_State") {
return "tox_group_privacy_state_pack";
} else if (type == "Tox_Group_Voice_State") {
return "tox_group_voice_state_pack";
} else if (type == "Tox_Group_Topic_Lock") {
return "tox_group_topic_lock_pack";
} else if (type == "Tox_Group_Join_Fail") {
return "tox_group_join_fail_pack";
} else if (type == "Tox_Group_Mod_Event") {
return "tox_group_mod_event_pack";
} else if (type == "Tox_Group_Exit_Type") {
return "tox_group_exit_type_pack";
} else {
//std::cerr << "unknown type " << type << "\n";
//exit(1);
// assume enum -> u32
std::cerr << "unknown type " << type << "\n";
exit(1);
return "bin_pack_u32";
}
}
Expand Down Expand Up @@ -164,6 +180,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
#include "../tox_events.h")";
if (need_tox_unpack_h) {
f << R"(
#include "../tox_pack.h"
#include "../tox_unpack.h")";
}
f << R"(
Expand Down Expand Up @@ -310,7 +327,6 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
// pack
f << "bool tox_event_" << event_name_l << "_pack(\n";
f << " const Tox_Event_" << event_name << " *event, Bin_Pack *bp)\n{\n";
f << " assert(event != nullptr);\n";

bool return_started = false;

Expand All @@ -330,7 +346,11 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
overloaded{
[&](const EventTypeTrivial& t) {
f << bin_pack_name_from_type(t.type);
f << "(bp, event->" << t.name << ")";
if (t.type.rfind("Tox_", 0) == 0) {
f << "(event->" << t.name << ", bp)";
} else {
f << "(bp, event->" << t.name << ")";
}
},
[&](const EventTypeByteRange& t) {
f << "bin_pack_bin(bp, event->" << t.name_data << ", event->" << t.name_length << ")";
Expand Down Expand Up @@ -361,7 +381,11 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
overloaded{
[&](const EventTypeTrivial& t) {
f << bin_unpack_name_from_type(t.type);
f << "(bu, &event->" << t.name << ")";
if (t.type.rfind("Tox_", 0) == 0) {
f << "(&event->" << t.name << ", bu)";
} else {
f << "(bu, &event->" << t.name << ")";
}
},
[&](const EventTypeByteRange& t) {
f << "bin_unpack_bin(bu, &event->" << t.name_data << ", &event->" << t.name_length << ")";
Expand Down
14 changes: 14 additions & 0 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,19 @@ cc_test(
],
)

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

cc_library(
name = "tox_unpack",
srcs = ["tox_unpack.c"],
Expand Down Expand Up @@ -926,6 +939,7 @@ cc_library(
":ccompat",
":mem",
":tox",
":tox_pack",
":tox_unpack",
"//c-toxcore/third_party:cmp",
],
Expand Down
2 changes: 2 additions & 0 deletions toxcore/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
../toxcore/tox_event.c \
../toxcore/tox_events.h \
../toxcore/tox_events.c \
../toxcore/tox_pack.h \
../toxcore/tox_pack.c \
../toxcore/tox_unpack.h \
../toxcore/tox_unpack.c \
../toxcore/tox_private.c \
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/conference_connected.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ static void tox_event_conference_connected_destruct(Tox_Event_Conference_Connect
bool tox_event_conference_connected_pack(
const Tox_Event_Conference_Connected *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_u32(bp, event->conference_number);
}

Expand Down
6 changes: 3 additions & 3 deletions toxcore/events/conference_invite.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"


Expand Down Expand Up @@ -107,10 +108,9 @@ static void tox_event_conference_invite_destruct(Tox_Event_Conference_Invite *co
bool tox_event_conference_invite_pack(
const Tox_Event_Conference_Invite *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->type)
&& tox_conference_type_pack(event->type, bp)
&& bin_pack_bin(bp, event->cookie, event->cookie_length);
}

Expand All @@ -124,7 +124,7 @@ static bool tox_event_conference_invite_unpack_into(
}

return bin_unpack_u32(bu, &event->friend_number)
&& tox_conference_type_unpack(bu, &event->type)
&& tox_conference_type_unpack(&event->type, bu)
&& bin_unpack_bin(bu, &event->cookie, &event->cookie_length);
}

Expand Down
6 changes: 3 additions & 3 deletions toxcore/events/conference_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"


Expand Down Expand Up @@ -121,11 +122,10 @@ static void tox_event_conference_message_destruct(Tox_Event_Conference_Message *
bool tox_event_conference_message_pack(
const Tox_Event_Conference_Message *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 4)
&& bin_pack_u32(bp, event->conference_number)
&& bin_pack_u32(bp, event->peer_number)
&& bin_pack_u32(bp, event->type)
&& tox_message_type_pack(event->type, bp)
&& bin_pack_bin(bp, event->message, event->message_length);
}

Expand All @@ -140,7 +140,7 @@ static bool tox_event_conference_message_unpack_into(

return bin_unpack_u32(bu, &event->conference_number)
&& bin_unpack_u32(bu, &event->peer_number)
&& tox_message_type_unpack(bu, &event->type)
&& tox_message_type_unpack(&event->type, bu)
&& bin_unpack_bin(bu, &event->message, &event->message_length);
}

Expand Down
1 change: 0 additions & 1 deletion toxcore/events/conference_peer_list_changed.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ static void tox_event_conference_peer_list_changed_destruct(Tox_Event_Conference
bool tox_event_conference_peer_list_changed_pack(
const Tox_Event_Conference_Peer_List_Changed *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_u32(bp, event->conference_number);
}

Expand Down
1 change: 0 additions & 1 deletion toxcore/events/conference_peer_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ static void tox_event_conference_peer_name_destruct(Tox_Event_Conference_Peer_Na
bool tox_event_conference_peer_name_pack(
const Tox_Event_Conference_Peer_Name *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->conference_number)
&& bin_pack_u32(bp, event->peer_number)
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/conference_title.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ static void tox_event_conference_title_destruct(Tox_Event_Conference_Title *conf
bool tox_event_conference_title_pack(
const Tox_Event_Conference_Title *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->conference_number)
&& bin_pack_u32(bp, event->peer_number)
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/file_chunk_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ static void tox_event_file_chunk_request_destruct(Tox_Event_File_Chunk_Request *
bool tox_event_file_chunk_request_pack(
const Tox_Event_File_Chunk_Request *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 4)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->file_number)
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/file_recv.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ static void tox_event_file_recv_destruct(Tox_Event_File_Recv *file_recv, const M
bool tox_event_file_recv_pack(
const Tox_Event_File_Recv *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 5)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->file_number)
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/file_recv_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ static void tox_event_file_recv_chunk_destruct(Tox_Event_File_Recv_Chunk *file_r
bool tox_event_file_recv_chunk_pack(
const Tox_Event_File_Recv_Chunk *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 4)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->file_number)
Expand Down
6 changes: 3 additions & 3 deletions toxcore/events/file_recv_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"


Expand Down Expand Up @@ -83,11 +84,10 @@ static void tox_event_file_recv_control_destruct(Tox_Event_File_Recv_Control *fi
bool tox_event_file_recv_control_pack(
const Tox_Event_File_Recv_Control *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->file_number)
&& bin_pack_u32(bp, event->control);
&& tox_file_control_pack(event->control, bp);
}

non_null()
Expand All @@ -101,7 +101,7 @@ static bool tox_event_file_recv_control_unpack_into(

return bin_unpack_u32(bu, &event->friend_number)
&& bin_unpack_u32(bu, &event->file_number)
&& tox_file_control_unpack(bu, &event->control);
&& tox_file_control_unpack(&event->control, bu);
}


Expand Down
6 changes: 3 additions & 3 deletions toxcore/events/friend_connection_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"


Expand Down Expand Up @@ -69,10 +70,9 @@ static void tox_event_friend_connection_status_destruct(Tox_Event_Friend_Connect
bool tox_event_friend_connection_status_pack(
const Tox_Event_Friend_Connection_Status *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->connection_status);
&& tox_connection_pack(event->connection_status, bp);
}

non_null()
Expand All @@ -85,7 +85,7 @@ static bool tox_event_friend_connection_status_unpack_into(
}

return bin_unpack_u32(bu, &event->friend_number)
&& tox_connection_unpack(bu, &event->connection_status);
&& tox_connection_unpack(&event->connection_status, bu);
}


Expand Down
1 change: 0 additions & 1 deletion toxcore/events/friend_lossless_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ static void tox_event_friend_lossless_packet_destruct(Tox_Event_Friend_Lossless_
bool tox_event_friend_lossless_packet_pack(
const Tox_Event_Friend_Lossless_Packet *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bin(bp, event->data, event->data_length);
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/friend_lossy_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ static void tox_event_friend_lossy_packet_destruct(Tox_Event_Friend_Lossy_Packet
bool tox_event_friend_lossy_packet_pack(
const Tox_Event_Friend_Lossy_Packet *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bin(bp, event->data, event->data_length);
Expand Down
6 changes: 3 additions & 3 deletions toxcore/events/friend_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"


Expand Down Expand Up @@ -107,10 +108,9 @@ static void tox_event_friend_message_destruct(Tox_Event_Friend_Message *friend_m
bool tox_event_friend_message_pack(
const Tox_Event_Friend_Message *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->type)
&& tox_message_type_pack(event->type, bp)
&& bin_pack_bin(bp, event->message, event->message_length);
}

Expand All @@ -124,7 +124,7 @@ static bool tox_event_friend_message_unpack_into(
}

return bin_unpack_u32(bu, &event->friend_number)
&& tox_message_type_unpack(bu, &event->type)
&& tox_message_type_unpack(&event->type, bu)
&& bin_unpack_bin(bu, &event->message, &event->message_length);
}

Expand Down
1 change: 0 additions & 1 deletion toxcore/events/friend_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ static void tox_event_friend_name_destruct(Tox_Event_Friend_Name *friend_name, c
bool tox_event_friend_name_pack(
const Tox_Event_Friend_Name *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bin(bp, event->name, event->name_length);
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/friend_read_receipt.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ static void tox_event_friend_read_receipt_destruct(Tox_Event_Friend_Read_Receipt
bool tox_event_friend_read_receipt_pack(
const Tox_Event_Friend_Read_Receipt *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->message_id);
Expand Down
1 change: 0 additions & 1 deletion toxcore/events/friend_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ static void tox_event_friend_request_destruct(Tox_Event_Friend_Request *friend_r
bool tox_event_friend_request_pack(
const Tox_Event_Friend_Request *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_bin(bp, event->public_key, TOX_PUBLIC_KEY_SIZE)
&& bin_pack_bin(bp, event->message, event->message_length);
Expand Down
Loading

0 comments on commit e2c01e4

Please sign in to comment.