refactor: Use enum-specific pack functions for enum values.

It's more obvious this way.
This commit is contained in:
iphydf 2024-01-15 17:15:17 +00:00
parent afc472402b
commit e2c01e457b
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
49 changed files with 207 additions and 109 deletions

View File

@ -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

View File

@ -1 +1 @@
50123098ff16637a19253e4ea24c0b7bc7d1ef35f6e7c9f2783bddd303a6c6f3 /usr/local/bin/tox-bootstrapd
0b904988d79b9576bb88c6c7316d107b5a61bd6119a0992ebd7c1fa43db70abf /usr/local/bin/tox-bootstrapd

View File

@ -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";
}
}
@ -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"(
@ -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;
@ -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 << ")";
@ -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 << ")";

View File

@ -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"],
@ -926,6 +939,7 @@ cc_library(
":ccompat",
":mem",
":tox",
":tox_pack",
":tox_unpack",
"//c-toxcore/third_party:cmp",
],

View File

@ -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 \

View File

@ -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);
}

View File

@ -14,6 +14,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -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);
}
@ -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);
}

View File

@ -14,6 +14,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -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);
}
@ -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);
}

View File

@ -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);
}

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -12,6 +12,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -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()
@ -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);
}

View File

@ -12,6 +12,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -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()
@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -14,6 +14,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -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);
}
@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -12,6 +12,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -69,10 +70,9 @@ static void tox_event_friend_status_destruct(Tox_Event_Friend_Status *friend_sta
bool tox_event_friend_status_pack(
const Tox_Event_Friend_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->status);
&& tox_user_status_pack(event->status, bp);
}
non_null()
@ -85,7 +85,7 @@ static bool tox_event_friend_status_unpack_into(
}
return bin_unpack_u32(bu, &event->friend_number)
&& tox_user_status_unpack(bu, &event->status);
&& tox_user_status_unpack(&event->status, bu);
}

View File

@ -92,7 +92,6 @@ static void tox_event_friend_status_message_destruct(Tox_Event_Friend_Status_Mes
bool tox_event_friend_status_message_pack(
const Tox_Event_Friend_Status_Message *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->message, event->message_length);

View File

@ -68,7 +68,6 @@ static void tox_event_friend_typing_destruct(Tox_Event_Friend_Typing *friend_typ
bool tox_event_friend_typing_pack(
const Tox_Event_Friend_Typing *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bool(bp, event->typing);

View File

@ -106,7 +106,6 @@ static void tox_event_group_custom_packet_destruct(Tox_Event_Group_Custom_Packet
bool tox_event_group_custom_packet_pack(
const Tox_Event_Group_Custom_Packet *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->peer_id)

View File

@ -106,7 +106,6 @@ static void tox_event_group_custom_private_packet_destruct(Tox_Event_Group_Custo
bool tox_event_group_custom_private_packet_pack(
const Tox_Event_Group_Custom_Private_Packet *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->peer_id)

View File

@ -129,7 +129,6 @@ static void tox_event_group_invite_destruct(Tox_Event_Group_Invite *group_invite
bool tox_event_group_invite_pack(
const Tox_Event_Group_Invite *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bin(bp, event->invite_data, event->invite_data_length)

View File

@ -12,6 +12,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -69,10 +70,9 @@ static void tox_event_group_join_fail_destruct(Tox_Event_Group_Join_Fail *group_
bool tox_event_group_join_fail_pack(
const Tox_Event_Group_Join_Fail *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->fail_type);
&& tox_group_join_fail_pack(event->fail_type, bp);
}
non_null()
@ -85,7 +85,7 @@ static bool tox_event_group_join_fail_unpack_into(
}
return bin_unpack_u32(bu, &event->group_number)
&& tox_group_join_fail_unpack(bu, &event->fail_type);
&& tox_group_join_fail_unpack(&event->fail_type, bu);
}

View File

@ -14,6 +14,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -135,11 +136,10 @@ static void tox_event_group_message_destruct(Tox_Event_Group_Message *group_mess
bool tox_event_group_message_pack(
const Tox_Event_Group_Message *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 5)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->peer_id)
&& bin_pack_u32(bp, event->type)
&& tox_message_type_pack(event->type, bp)
&& bin_pack_bin(bp, event->message, event->message_length)
&& bin_pack_u32(bp, event->message_id);
}
@ -155,7 +155,7 @@ static bool tox_event_group_message_unpack_into(
return bin_unpack_u32(bu, &event->group_number)
&& bin_unpack_u32(bu, &event->peer_id)
&& tox_message_type_unpack(bu, &event->type)
&& tox_message_type_unpack(&event->type, bu)
&& bin_unpack_bin(bu, &event->message, &event->message_length)
&& bin_unpack_u32(bu, &event->message_id);
}

View File

@ -12,6 +12,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -97,12 +98,11 @@ static void tox_event_group_moderation_destruct(Tox_Event_Group_Moderation *grou
bool tox_event_group_moderation_pack(
const Tox_Event_Group_Moderation *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 4)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->source_peer_id)
&& bin_pack_u32(bp, event->target_peer_id)
&& bin_pack_u32(bp, event->mod_type);
&& tox_group_mod_event_pack(event->mod_type, bp);
}
non_null()
@ -117,7 +117,7 @@ static bool tox_event_group_moderation_unpack_into(
return bin_unpack_u32(bu, &event->group_number)
&& bin_unpack_u32(bu, &event->source_peer_id)
&& bin_unpack_u32(bu, &event->target_peer_id)
&& tox_group_mod_event_unpack(bu, &event->mod_type);
&& tox_group_mod_event_unpack(&event->mod_type, bu);
}

View File

@ -92,7 +92,6 @@ static void tox_event_group_password_destruct(Tox_Event_Group_Password *group_pa
bool tox_event_group_password_pack(
const Tox_Event_Group_Password *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_bin(bp, event->password, event->password_length);

View File

@ -14,6 +14,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -158,11 +159,10 @@ static void tox_event_group_peer_exit_destruct(Tox_Event_Group_Peer_Exit *group_
bool tox_event_group_peer_exit_pack(
const Tox_Event_Group_Peer_Exit *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 5)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->peer_id)
&& bin_pack_u32(bp, event->exit_type)
&& tox_group_exit_type_pack(event->exit_type, bp)
&& bin_pack_bin(bp, event->name, event->name_length)
&& bin_pack_bin(bp, event->part_message, event->part_message_length);
}
@ -178,7 +178,7 @@ static bool tox_event_group_peer_exit_unpack_into(
return bin_unpack_u32(bu, &event->group_number)
&& bin_unpack_u32(bu, &event->peer_id)
&& tox_group_exit_type_unpack(bu, &event->exit_type)
&& tox_group_exit_type_unpack(&event->exit_type, bu)
&& bin_unpack_bin(bu, &event->name, &event->name_length)
&& bin_unpack_bin(bu, &event->part_message, &event->part_message_length);
}

View File

@ -68,7 +68,6 @@ static void tox_event_group_peer_join_destruct(Tox_Event_Group_Peer_Join *group_
bool tox_event_group_peer_join_pack(
const Tox_Event_Group_Peer_Join *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->peer_id);

View File

@ -68,7 +68,6 @@ static void tox_event_group_peer_limit_destruct(Tox_Event_Group_Peer_Limit *grou
bool tox_event_group_peer_limit_pack(
const Tox_Event_Group_Peer_Limit *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->peer_limit);

View File

@ -106,7 +106,6 @@ static void tox_event_group_peer_name_destruct(Tox_Event_Group_Peer_Name *group_
bool tox_event_group_peer_name_pack(
const Tox_Event_Group_Peer_Name *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->peer_id)

View File

@ -12,6 +12,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -83,11 +84,10 @@ static void tox_event_group_peer_status_destruct(Tox_Event_Group_Peer_Status *gr
bool tox_event_group_peer_status_pack(
const Tox_Event_Group_Peer_Status *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->peer_id)
&& bin_pack_u32(bp, event->status);
&& tox_user_status_pack(event->status, bp);
}
non_null()
@ -101,7 +101,7 @@ static bool tox_event_group_peer_status_unpack_into(
return bin_unpack_u32(bu, &event->group_number)
&& bin_unpack_u32(bu, &event->peer_id)
&& tox_user_status_unpack(bu, &event->status);
&& tox_user_status_unpack(&event->status, bu);
}

View File

@ -12,6 +12,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -69,10 +70,9 @@ static void tox_event_group_privacy_state_destruct(Tox_Event_Group_Privacy_State
bool tox_event_group_privacy_state_pack(
const Tox_Event_Group_Privacy_State *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->privacy_state);
&& tox_group_privacy_state_pack(event->privacy_state, bp);
}
non_null()
@ -85,7 +85,7 @@ static bool tox_event_group_privacy_state_unpack_into(
}
return bin_unpack_u32(bu, &event->group_number)
&& tox_group_privacy_state_unpack(bu, &event->privacy_state);
&& tox_group_privacy_state_unpack(&event->privacy_state, bu);
}

View File

@ -14,6 +14,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -121,11 +122,10 @@ static void tox_event_group_private_message_destruct(Tox_Event_Group_Private_Mes
bool tox_event_group_private_message_pack(
const Tox_Event_Group_Private_Message *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 4)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->peer_id)
&& bin_pack_u32(bp, event->type)
&& tox_message_type_pack(event->type, bp)
&& bin_pack_bin(bp, event->message, event->message_length);
}
@ -140,7 +140,7 @@ static bool tox_event_group_private_message_unpack_into(
return bin_unpack_u32(bu, &event->group_number)
&& bin_unpack_u32(bu, &event->peer_id)
&& tox_message_type_unpack(bu, &event->type)
&& tox_message_type_unpack(&event->type, bu)
&& bin_unpack_bin(bu, &event->message, &event->message_length);
}

View File

@ -54,7 +54,6 @@ static void tox_event_group_self_join_destruct(Tox_Event_Group_Self_Join *group_
bool tox_event_group_self_join_pack(
const Tox_Event_Group_Self_Join *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_u32(bp, event->group_number);
}

View File

@ -106,7 +106,6 @@ static void tox_event_group_topic_destruct(Tox_Event_Group_Topic *group_topic, c
bool tox_event_group_topic_pack(
const Tox_Event_Group_Topic *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->peer_id)

View File

@ -12,6 +12,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -69,10 +70,9 @@ static void tox_event_group_topic_lock_destruct(Tox_Event_Group_Topic_Lock *grou
bool tox_event_group_topic_lock_pack(
const Tox_Event_Group_Topic_Lock *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->topic_lock);
&& tox_group_topic_lock_pack(event->topic_lock, bp);
}
non_null()
@ -85,7 +85,7 @@ static bool tox_event_group_topic_lock_unpack_into(
}
return bin_unpack_u32(bu, &event->group_number)
&& tox_group_topic_lock_unpack(bu, &event->topic_lock);
&& tox_group_topic_lock_unpack(&event->topic_lock, bu);
}

View File

@ -12,6 +12,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -69,10 +70,9 @@ static void tox_event_group_voice_state_destruct(Tox_Event_Group_Voice_State *gr
bool tox_event_group_voice_state_pack(
const Tox_Event_Group_Voice_State *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->group_number)
&& bin_pack_u32(bp, event->voice_state);
&& tox_group_voice_state_pack(event->voice_state, bp);
}
non_null()
@ -85,7 +85,7 @@ static bool tox_event_group_voice_state_unpack_into(
}
return bin_unpack_u32(bu, &event->group_number)
&& tox_group_voice_state_unpack(bu, &event->voice_state);
&& tox_group_voice_state_unpack(&event->voice_state, bu);
}

View File

@ -12,6 +12,7 @@
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_pack.h"
#include "../tox_unpack.h"
@ -55,8 +56,7 @@ static void tox_event_self_connection_status_destruct(Tox_Event_Self_Connection_
bool tox_event_self_connection_status_pack(
const Tox_Event_Self_Connection_Status *event, Bin_Pack *bp)
{
assert(event != nullptr);
return bin_pack_u32(bp, event->connection_status);
return tox_connection_pack(event->connection_status, bp);
}
non_null()
@ -64,7 +64,7 @@ static bool tox_event_self_connection_status_unpack_into(
Tox_Event_Self_Connection_Status *event, Bin_Unpack *bu)
{
assert(event != nullptr);
return tox_connection_unpack(bu, &event->connection_status);
return tox_connection_unpack(&event->connection_status, bu);
}

View File

@ -48,7 +48,7 @@ static void change_symmetric_key(Onion *onion)
/** packing and unpacking functions */
non_null()
static void ip_pack(uint8_t *data, const IP *source)
static void ip_pack_to_bytes(uint8_t *data, const IP *source)
{
data[0] = source->family.value;
@ -62,7 +62,7 @@ static void ip_pack(uint8_t *data, const IP *source)
/** return 0 on success, -1 on failure. */
non_null()
static int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size, bool disable_family_check)
static int ip_unpack_from_bytes(IP *target, const uint8_t *data, unsigned int data_size, bool disable_family_check)
{
if (data_size < (1 + SIZE_IP6)) {
return -1;
@ -87,7 +87,7 @@ static int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size, bo
non_null()
static void ipport_pack(uint8_t *data, const IP_Port *source)
{
ip_pack(data, &source->ip);
ip_pack_to_bytes(data, &source->ip);
memcpy(data + SIZE_IP, &source->port, SIZE_PORT);
}
@ -99,7 +99,7 @@ static int ipport_unpack(IP_Port *target, const uint8_t *data, unsigned int data
return -1;
}
if (ip_unpack(&target->ip, data, data_size, disable_family_check) == -1) {
if (ip_unpack_from_bytes(&target->ip, data, data_size, disable_family_check) == -1) {
return -1;
}

55
toxcore/tox_pack.c Normal file
View File

@ -0,0 +1,55 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2022 The TokTok team.
*/
#include "tox_pack.h"
#include <stdint.h>
#include "bin_pack.h"
#include "tox.h"
bool tox_conference_type_pack(Tox_Conference_Type val, Bin_Pack *bp)
{
return bin_pack_u32(bp, (uint32_t)val);
}
bool tox_connection_pack(Tox_Connection val, Bin_Pack *bp)
{
return bin_pack_u32(bp, (uint32_t)val);
}
bool tox_file_control_pack(Tox_File_Control val, Bin_Pack *bp)
{
return bin_pack_u32(bp, (uint32_t)val);
}
bool tox_message_type_pack(Tox_Message_Type val, Bin_Pack *bp)
{
return bin_pack_u32(bp, (uint32_t)val);
}
bool tox_user_status_pack(Tox_User_Status val, Bin_Pack *bp)
{
return bin_pack_u32(bp, (uint32_t)val);
}
bool tox_group_privacy_state_pack(Tox_Group_Privacy_State val, Bin_Pack *bp)
{
return bin_pack_u32(bp, (uint32_t)val);
}
bool tox_group_voice_state_pack(Tox_Group_Voice_State val, Bin_Pack *bp)
{
return bin_pack_u32(bp, (uint32_t)val);
}
bool tox_group_topic_lock_pack(Tox_Group_Topic_Lock val, Bin_Pack *bp)
{
return bin_pack_u32(bp, (uint32_t)val);
}
bool tox_group_join_fail_pack(Tox_Group_Join_Fail val, Bin_Pack *bp)
{
return bin_pack_u32(bp, (uint32_t)val);
}
bool tox_group_mod_event_pack(Tox_Group_Mod_Event val, Bin_Pack *bp)
{
return bin_pack_u32(bp, (uint32_t)val);
}
bool tox_group_exit_type_pack(Tox_Group_Exit_Type val, Bin_Pack *bp)
{
return bin_pack_u32(bp, (uint32_t)val);
}

24
toxcore/tox_pack.h Normal file
View File

@ -0,0 +1,24 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2022 The TokTok team.
*/
#ifndef C_TOXCORE_TOXCORE_TOX_PACK_H
#define C_TOXCORE_TOXCORE_TOX_PACK_H
#include "attributes.h"
#include "bin_pack.h"
#include "tox.h"
non_null() bool tox_conference_type_pack(Tox_Conference_Type val, Bin_Pack *bp);
non_null() bool tox_connection_pack(Tox_Connection val, Bin_Pack *bp);
non_null() bool tox_file_control_pack(Tox_File_Control val, Bin_Pack *bp);
non_null() bool tox_message_type_pack(Tox_Message_Type val, Bin_Pack *bp);
non_null() bool tox_user_status_pack(Tox_User_Status val, Bin_Pack *bp);
non_null() bool tox_group_privacy_state_pack(Tox_Group_Privacy_State val, Bin_Pack *bp);
non_null() bool tox_group_voice_state_pack(Tox_Group_Voice_State val, Bin_Pack *bp);
non_null() bool tox_group_topic_lock_pack(Tox_Group_Topic_Lock val, Bin_Pack *bp);
non_null() bool tox_group_join_fail_pack(Tox_Group_Join_Fail val, Bin_Pack *bp);
non_null() bool tox_group_mod_event_pack(Tox_Group_Mod_Event val, Bin_Pack *bp);
non_null() bool tox_group_exit_type_pack(Tox_Group_Exit_Type val, Bin_Pack *bp);
#endif // C_TOXCORE_TOXCORE_TOX_PACK_H

View File

@ -29,7 +29,7 @@ static bool tox_conference_type_from_int(uint32_t value, Tox_Conference_Type *ou
}
}
}
bool tox_conference_type_unpack(Bin_Unpack *bu, Tox_Conference_Type *val)
bool tox_conference_type_unpack(Tox_Conference_Type *val, Bin_Unpack *bu)
{
uint32_t u32;
return bin_unpack_u32(bu, &u32)
@ -62,7 +62,7 @@ static bool tox_connection_from_int(uint32_t value, Tox_Connection *out)
}
}
bool tox_connection_unpack(Bin_Unpack *bu, Tox_Connection *val)
bool tox_connection_unpack(Tox_Connection *val, Bin_Unpack *bu)
{
uint32_t u32;
return bin_unpack_u32(bu, &u32)
@ -95,7 +95,7 @@ static bool tox_file_control_from_int(uint32_t value, Tox_File_Control *out)
}
}
bool tox_file_control_unpack(Bin_Unpack *bu, Tox_File_Control *val)
bool tox_file_control_unpack(Tox_File_Control *val, Bin_Unpack *bu)
{
uint32_t u32;
return bin_unpack_u32(bu, &u32)
@ -123,7 +123,7 @@ static bool tox_message_type_from_int(uint32_t value, Tox_Message_Type *out)
}
}
bool tox_message_type_unpack(Bin_Unpack *bu, Tox_Message_Type *val)
bool tox_message_type_unpack(Tox_Message_Type *val, Bin_Unpack *bu)
{
uint32_t u32;
return bin_unpack_u32(bu, &u32)
@ -156,7 +156,7 @@ static bool tox_user_status_from_int(uint32_t value, Tox_User_Status *out)
}
}
bool tox_user_status_unpack(Bin_Unpack *bu, Tox_User_Status *val)
bool tox_user_status_unpack(Tox_User_Status *val, Bin_Unpack *bu)
{
uint32_t u32;
return bin_unpack_u32(bu, &u32)
@ -181,7 +181,7 @@ static bool tox_group_privacy_state_from_int(uint32_t value, Tox_Group_Privacy_S
}
}
}
bool tox_group_privacy_state_unpack(Bin_Unpack *bu, Tox_Group_Privacy_State *val)
bool tox_group_privacy_state_unpack(Tox_Group_Privacy_State *val, Bin_Unpack *bu)
{
uint32_t u32;
return bin_unpack_u32(bu, &u32)
@ -209,7 +209,7 @@ static bool tox_group_voice_state_from_int(uint32_t value, Tox_Group_Voice_State
}
}
}
bool tox_group_voice_state_unpack(Bin_Unpack *bu, Tox_Group_Voice_State *val)
bool tox_group_voice_state_unpack(Tox_Group_Voice_State *val, Bin_Unpack *bu)
{
uint32_t u32;
return bin_unpack_u32(bu, &u32)
@ -234,7 +234,7 @@ static bool tox_group_topic_lock_from_int(uint32_t value, Tox_Group_Topic_Lock *
}
}
}
bool tox_group_topic_lock_unpack(Bin_Unpack *bu, Tox_Group_Topic_Lock *val)
bool tox_group_topic_lock_unpack(Tox_Group_Topic_Lock *val, Bin_Unpack *bu)
{
uint32_t u32;
return bin_unpack_u32(bu, &u32)
@ -263,7 +263,7 @@ static bool tox_group_join_fail_from_int(uint32_t value, Tox_Group_Join_Fail *ou
}
}
}
bool tox_group_join_fail_unpack(Bin_Unpack *bu, Tox_Group_Join_Fail *val)
bool tox_group_join_fail_unpack(Tox_Group_Join_Fail *val, Bin_Unpack *bu)
{
uint32_t u32;
return bin_unpack_u32(bu, &u32)
@ -296,7 +296,7 @@ static bool tox_group_mod_event_from_int(uint32_t value, Tox_Group_Mod_Event *ou
}
}
}
bool tox_group_mod_event_unpack(Bin_Unpack *bu, Tox_Group_Mod_Event *val)
bool tox_group_mod_event_unpack(Tox_Group_Mod_Event *val, Bin_Unpack *bu)
{
uint32_t u32;
return bin_unpack_u32(bu, &u32)
@ -337,7 +337,7 @@ static bool tox_group_exit_type_from_int(uint32_t value, Tox_Group_Exit_Type *ou
}
}
}
bool tox_group_exit_type_unpack(Bin_Unpack *bu, Tox_Group_Exit_Type *val)
bool tox_group_exit_type_unpack(Tox_Group_Exit_Type *val, Bin_Unpack *bu)
{
uint32_t u32;
return bin_unpack_u32(bu, &u32)

View File

@ -9,16 +9,16 @@
#include "bin_unpack.h"
#include "tox.h"
non_null() bool tox_conference_type_unpack(Bin_Unpack *bu, Tox_Conference_Type *val);
non_null() bool tox_connection_unpack(Bin_Unpack *bu, Tox_Connection *val);
non_null() bool tox_file_control_unpack(Bin_Unpack *bu, Tox_File_Control *val);
non_null() bool tox_message_type_unpack(Bin_Unpack *bu, Tox_Message_Type *val);
non_null() bool tox_user_status_unpack(Bin_Unpack *bu, Tox_User_Status *val);
non_null() bool tox_group_privacy_state_unpack(Bin_Unpack *bu, Tox_Group_Privacy_State *val);
non_null() bool tox_group_voice_state_unpack(Bin_Unpack *bu, Tox_Group_Voice_State *val);
non_null() bool tox_group_topic_lock_unpack(Bin_Unpack *bu, Tox_Group_Topic_Lock *val);
non_null() bool tox_group_join_fail_unpack(Bin_Unpack *bu, Tox_Group_Join_Fail *val);
non_null() bool tox_group_mod_event_unpack(Bin_Unpack *bu, Tox_Group_Mod_Event *val);
non_null() bool tox_group_exit_type_unpack(Bin_Unpack *bu, Tox_Group_Exit_Type *val);
non_null() bool tox_conference_type_unpack(Tox_Conference_Type *val, Bin_Unpack *bu);
non_null() bool tox_connection_unpack(Tox_Connection *val, Bin_Unpack *bu);
non_null() bool tox_file_control_unpack(Tox_File_Control *val, Bin_Unpack *bu);
non_null() bool tox_message_type_unpack(Tox_Message_Type *val, Bin_Unpack *bu);
non_null() bool tox_user_status_unpack(Tox_User_Status *val, Bin_Unpack *bu);
non_null() bool tox_group_privacy_state_unpack(Tox_Group_Privacy_State *val, Bin_Unpack *bu);
non_null() bool tox_group_voice_state_unpack(Tox_Group_Voice_State *val, Bin_Unpack *bu);
non_null() bool tox_group_topic_lock_unpack(Tox_Group_Topic_Lock *val, Bin_Unpack *bu);
non_null() bool tox_group_join_fail_unpack(Tox_Group_Join_Fail *val, Bin_Unpack *bu);
non_null() bool tox_group_mod_event_unpack(Tox_Group_Mod_Event *val, Bin_Unpack *bu);
non_null() bool tox_group_exit_type_unpack(Tox_Group_Exit_Type *val, Bin_Unpack *bu);
#endif // C_TOXCORE_TOXCORE_TOX_UNPACK_H