refactor: Add common msgpack array packer with callback.

There will be more object arrays that need to be packed. This function
takes care of NULL (creating an empty array), and putting the correct
array size and calling the per-element callback the right amount of
times.
This commit is contained in:
iphydf 2024-01-16 23:09:29 +00:00
parent 3c659f5288
commit bdd42b5452
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
45 changed files with 253 additions and 160 deletions

View File

@ -1 +1 @@
3b4f5c3224e919d4474c4af4b5058916b181a2689f59a3b04e72305b454b2ae3 /usr/local/bin/tox-bootstrapd 3acc3a1f08e67dac66d91657a36d98be397fa3f14bc4798d3349605e37a90fc3 /usr/local/bin/tox-bootstrapd

View File

@ -452,6 +452,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
f << "bool tox_event_" << event_name_l << "_unpack(\n"; f << "bool tox_event_" << event_name_l << "_unpack(\n";
f << " Tox_Event_" << event_name << " **event, Bin_Unpack *bu, const Memory *mem)\n{\n"; f << " Tox_Event_" << event_name << " **event, Bin_Unpack *bu, const Memory *mem)\n{\n";
f << " assert(event != nullptr);\n"; f << " assert(event != nullptr);\n";
f << " assert(*event == nullptr);\n";
f << " *event = tox_event_" << event_name_l << "_new(mem);\n\n"; f << " *event = tox_event_" << event_name_l << "_new(mem);\n\n";
f << " if (*event == nullptr) {\n return false;\n }\n\n"; f << " if (*event == nullptr) {\n return false;\n }\n\n";
f << " return tox_event_" << event_name_l << "_unpack_into(*event, bu);\n}\n\n"; f << " return tox_event_" << event_name_l << "_unpack_into(*event, bu);\n}\n\n";

View File

@ -109,6 +109,26 @@ bool bin_pack_obj_array_b(bin_pack_array_cb *callback, const void *arr, uint32_t
return true; return true;
} }
bool bin_pack_obj_array(Bin_Pack *bp, bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger)
{
if (arr == nullptr) {
assert(arr_size == 0);
return bin_pack_array(bp, 0);
}
if (!bin_pack_array(bp, arr_size)) {
return false;
}
for (uint32_t i = 0; i < arr_size; ++i) {
if (!callback(arr, i, logger, bp)) {
return false;
}
}
return true;
}
bool bin_pack_array(Bin_Pack *bp, uint32_t size) bool bin_pack_array(Bin_Pack *bp, uint32_t size)
{ {
return cmp_write_array(&bp->ctx, size); return cmp_write_array(&bp->ctx, size);

View File

@ -17,6 +17,11 @@ extern "C" {
/** /**
* @brief Binary serialisation object. * @brief Binary serialisation object.
* *
* Naming convention:
* - Functions ending in `_b` (or `_b_size`) are NOT MessagePack, i.e. write
* data in plain big endian binary format.
* - All other functions encode their input in MessagePack format.
*
* Some notes on parameter order: * Some notes on parameter order:
* *
* - We pass the `obj` pointer as `this`-like pointer first to the callbacks. * - We pass the `obj` pointer as `this`-like pointer first to the callbacks.
@ -66,7 +71,9 @@ uint32_t bin_pack_obj_size(bin_pack_cb *callback, const void *obj, const Logger
/** @brief Pack an object into a buffer of a given size. /** @brief Pack an object into a buffer of a given size.
* *
* This function creates and initialises a `Bin_Pack` packer object, calls the callback with the * This function creates and initialises a `Bin_Pack` packer object, calls the callback with the
* packer object and the to-be-packed object, and then cleans up the packer object. * packer object and the to-be-packed object, and then cleans up the packer object. Note that
* there is nothing MessagePack-specific about this function, so it can be used for both custom
* binary and MessagePack formats.
* *
* You can use `bin_pack_obj_size` to determine the minimum required size of `buf`. If packing * You can use `bin_pack_obj_size` to determine the minimum required size of `buf`. If packing
* overflows `uint32_t`, this function returns `false`. * overflows `uint32_t`, this function returns `false`.
@ -102,13 +109,8 @@ uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const void *arr,
/** @brief Pack an object array into a buffer of a given size. /** @brief Pack an object array into a buffer of a given size.
* *
* Calls the callback `arr_size` times with increasing `index` argument from 0 to * Similar to `bin_pack_obj_array` but does not write the array length, so
* `arr_size`. This function is here just so we don't need to write the same * if you need that, encoding it is on you.
* trivial loop many times and so we don't need an extra struct just to contain
* an array with size so it can be passed to `bin_pack_obj`.
*
* Similar to `bin_pack_obj` but for arrays. Does not write the array length, so
* if you need that, write it manually using `bin_pack_array`.
* *
* Passing NULL for `arr` has no effect, but requires that `arr_size` is 0. * Passing NULL for `arr` has no effect, but requires that `arr_size` is 0.
* *
@ -125,6 +127,32 @@ uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const void *arr,
non_null(1, 5) nullable(2, 4) non_null(1, 5) nullable(2, 4)
bool bin_pack_obj_array_b(bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger, uint8_t *buf, uint32_t buf_size); bool bin_pack_obj_array_b(bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger, uint8_t *buf, uint32_t buf_size);
/** @brief Encode an object array as MessagePack array into a bin packer.
*
* Calls the callback `arr_size` times with increasing `index` argument from 0 to
* `arr_size`. This function is here just so we don't need to write the same
* trivial loop many times and so we don't need an extra struct just to contain
* an array with size so it can be passed to `bin_pack_obj`.
*
* Similar to `bin_pack_obj` but for arrays. Note that a `Bin_Pack` object is
* required here, so it must be called from within a callback to one of the
* functions above.
*
* Passing NULL for `arr` requires that `arr_size` is 0. This will write a 0-size
* MessagePack array to the packer.
*
* @param bp Bin packer object.
* @param callback The function called on the created packer and packed object
* array.
* @param arr The object array to be packed, passed as `arr` to the callback.
* @param arr_size The number of elements in the object array.
* @param logger Optional logger object to pass to the callback.
*
* @retval false if an error occurred (e.g. buffer overflow).
*/
non_null(1, 2) nullable(3, 5)
bool bin_pack_obj_array(Bin_Pack *bp, bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger);
/** @brief Start packing a MessagePack array. /** @brief Start packing a MessagePack array.
* *
* A call to this function must be followed by exactly `size` calls to other functions below. * A call to this function must be followed by exactly `size` calls to other functions below.

View File

@ -119,6 +119,7 @@ bool tox_event_conference_connected_unpack(
Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_conference_connected_new(mem); *event = tox_event_conference_connected_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -187,6 +187,7 @@ bool tox_event_conference_invite_unpack(
Tox_Event_Conference_Invite **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Conference_Invite **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_conference_invite_new(mem); *event = tox_event_conference_invite_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -203,6 +203,7 @@ bool tox_event_conference_message_unpack(
Tox_Event_Conference_Message **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Conference_Message **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_conference_message_new(mem); *event = tox_event_conference_message_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -119,6 +119,7 @@ bool tox_event_conference_peer_list_changed_unpack(
Tox_Event_Conference_Peer_List_Changed **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Conference_Peer_List_Changed **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_conference_peer_list_changed_new(mem); *event = tox_event_conference_peer_list_changed_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -185,6 +185,7 @@ bool tox_event_conference_peer_name_unpack(
Tox_Event_Conference_Peer_Name **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Conference_Peer_Name **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_conference_peer_name_new(mem); *event = tox_event_conference_peer_name_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -185,6 +185,7 @@ bool tox_event_conference_title_unpack(
Tox_Event_Conference_Title **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Conference_Title **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_conference_title_new(mem); *event = tox_event_conference_title_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -172,6 +172,7 @@ bool tox_event_file_chunk_request_unpack(
Tox_Event_File_Chunk_Request **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_File_Chunk_Request **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_file_chunk_request_new(mem); *event = tox_event_file_chunk_request_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -217,6 +217,7 @@ bool tox_event_file_recv_unpack(
Tox_Event_File_Recv **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_File_Recv **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_file_recv_new(mem); *event = tox_event_file_recv_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -201,6 +201,7 @@ bool tox_event_file_recv_chunk_unpack(
Tox_Event_File_Recv_Chunk **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_File_Recv_Chunk **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_file_recv_chunk_new(mem); *event = tox_event_file_recv_chunk_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -158,6 +158,7 @@ bool tox_event_file_recv_control_unpack(
Tox_Event_File_Recv_Control **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_File_Recv_Control **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_file_recv_control_new(mem); *event = tox_event_file_recv_control_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -142,6 +142,7 @@ bool tox_event_friend_connection_status_unpack(
Tox_Event_Friend_Connection_Status **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Friend_Connection_Status **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_connection_status_new(mem); *event = tox_event_friend_connection_status_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -169,6 +169,7 @@ bool tox_event_friend_lossless_packet_unpack(
Tox_Event_Friend_Lossless_Packet **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Friend_Lossless_Packet **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_lossless_packet_new(mem); *event = tox_event_friend_lossless_packet_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -169,6 +169,7 @@ bool tox_event_friend_lossy_packet_unpack(
Tox_Event_Friend_Lossy_Packet **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Friend_Lossy_Packet **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_lossy_packet_new(mem); *event = tox_event_friend_lossy_packet_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -187,6 +187,7 @@ bool tox_event_friend_message_unpack(
Tox_Event_Friend_Message **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Friend_Message **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_message_new(mem); *event = tox_event_friend_message_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -169,6 +169,7 @@ bool tox_event_friend_name_unpack(
Tox_Event_Friend_Name **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Friend_Name **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_name_new(mem); *event = tox_event_friend_name_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -140,6 +140,7 @@ bool tox_event_friend_read_receipt_unpack(
Tox_Event_Friend_Read_Receipt **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Friend_Read_Receipt **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_read_receipt_new(mem); *event = tox_event_friend_read_receipt_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -159,6 +159,7 @@ bool tox_event_friend_request_unpack(
Tox_Event_Friend_Request **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Friend_Request **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_request_new(mem); *event = tox_event_friend_request_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -142,6 +142,7 @@ bool tox_event_friend_status_unpack(
Tox_Event_Friend_Status **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Friend_Status **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_status_new(mem); *event = tox_event_friend_status_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -169,6 +169,7 @@ bool tox_event_friend_status_message_unpack(
Tox_Event_Friend_Status_Message **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Friend_Status_Message **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_status_message_new(mem); *event = tox_event_friend_status_message_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -140,6 +140,7 @@ bool tox_event_friend_typing_unpack(
Tox_Event_Friend_Typing **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Friend_Typing **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_friend_typing_new(mem); *event = tox_event_friend_typing_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -185,6 +185,7 @@ bool tox_event_group_custom_packet_unpack(
Tox_Event_Group_Custom_Packet **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Custom_Packet **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_custom_packet_new(mem); *event = tox_event_group_custom_packet_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -185,6 +185,7 @@ bool tox_event_group_custom_private_packet_unpack(
Tox_Event_Group_Custom_Private_Packet **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Custom_Private_Packet **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_custom_private_packet_new(mem); *event = tox_event_group_custom_private_packet_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -213,6 +213,7 @@ bool tox_event_group_invite_unpack(
Tox_Event_Group_Invite **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Invite **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_invite_new(mem); *event = tox_event_group_invite_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -142,6 +142,7 @@ bool tox_event_group_join_fail_unpack(
Tox_Event_Group_Join_Fail **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Join_Fail **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_join_fail_new(mem); *event = tox_event_group_join_fail_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -219,6 +219,7 @@ bool tox_event_group_message_unpack(
Tox_Event_Group_Message **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Message **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_message_new(mem); *event = tox_event_group_message_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -174,6 +174,7 @@ bool tox_event_group_moderation_unpack(
Tox_Event_Group_Moderation **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Moderation **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_moderation_new(mem); *event = tox_event_group_moderation_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -169,6 +169,7 @@ bool tox_event_group_password_unpack(
Tox_Event_Group_Password **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Password **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_password_new(mem); *event = tox_event_group_password_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -247,6 +247,7 @@ bool tox_event_group_peer_exit_unpack(
Tox_Event_Group_Peer_Exit **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Peer_Exit **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_peer_exit_new(mem); *event = tox_event_group_peer_exit_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -140,6 +140,7 @@ bool tox_event_group_peer_join_unpack(
Tox_Event_Group_Peer_Join **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Peer_Join **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_peer_join_new(mem); *event = tox_event_group_peer_join_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -140,6 +140,7 @@ bool tox_event_group_peer_limit_unpack(
Tox_Event_Group_Peer_Limit **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Peer_Limit **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_peer_limit_new(mem); *event = tox_event_group_peer_limit_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -185,6 +185,7 @@ bool tox_event_group_peer_name_unpack(
Tox_Event_Group_Peer_Name **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Peer_Name **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_peer_name_new(mem); *event = tox_event_group_peer_name_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -158,6 +158,7 @@ bool tox_event_group_peer_status_unpack(
Tox_Event_Group_Peer_Status **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Peer_Status **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_peer_status_new(mem); *event = tox_event_group_peer_status_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -142,6 +142,7 @@ bool tox_event_group_privacy_state_unpack(
Tox_Event_Group_Privacy_State **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Privacy_State **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_privacy_state_new(mem); *event = tox_event_group_privacy_state_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -203,6 +203,7 @@ bool tox_event_group_private_message_unpack(
Tox_Event_Group_Private_Message **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Private_Message **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_private_message_new(mem); *event = tox_event_group_private_message_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -119,6 +119,7 @@ bool tox_event_group_self_join_unpack(
Tox_Event_Group_Self_Join **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Self_Join **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_self_join_new(mem); *event = tox_event_group_self_join_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -185,6 +185,7 @@ bool tox_event_group_topic_unpack(
Tox_Event_Group_Topic **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Topic **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_topic_new(mem); *event = tox_event_group_topic_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -142,6 +142,7 @@ bool tox_event_group_topic_lock_unpack(
Tox_Event_Group_Topic_Lock **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Topic_Lock **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_topic_lock_new(mem); *event = tox_event_group_topic_lock_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -142,6 +142,7 @@ bool tox_event_group_voice_state_unpack(
Tox_Event_Group_Voice_State **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Group_Voice_State **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_group_voice_state_new(mem); *event = tox_event_group_voice_state_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -121,6 +121,7 @@ bool tox_event_self_connection_status_unpack(
Tox_Event_Self_Connection_Status **event, Bin_Unpack *bu, const Memory *mem) Tox_Event_Self_Connection_Status **event, Bin_Unpack *bu, const Memory *mem)
{ {
assert(event != nullptr); assert(event != nullptr);
assert(*event == nullptr);
*event = tox_event_self_connection_status_new(mem); *event = tox_event_self_connection_status_new(mem);
if (*event == nullptr) { if (*event == nullptr) {

View File

@ -947,6 +947,137 @@ static bool tox_event_type_unpack(Bin_Unpack *bu, Tox_Event_Type *val)
&& tox_event_type_from_int(u32, val); && tox_event_type_from_int(u32, val);
} }
non_null()
static bool tox_event_data_unpack(Tox_Event_Type type, Tox_Event_Data *data, Bin_Unpack *bu, const Memory *mem)
{
switch (type) {
case TOX_EVENT_CONFERENCE_CONNECTED:
return tox_event_conference_connected_unpack(&data->conference_connected, bu, mem);
case TOX_EVENT_CONFERENCE_INVITE:
return tox_event_conference_invite_unpack(&data->conference_invite, bu, mem);
case TOX_EVENT_CONFERENCE_MESSAGE:
return tox_event_conference_message_unpack(&data->conference_message, bu, mem);
case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED:
return tox_event_conference_peer_list_changed_unpack(&data->conference_peer_list_changed, bu, mem);
case TOX_EVENT_CONFERENCE_PEER_NAME:
return tox_event_conference_peer_name_unpack(&data->conference_peer_name, bu, mem);
case TOX_EVENT_CONFERENCE_TITLE:
return tox_event_conference_title_unpack(&data->conference_title, bu, mem);
case TOX_EVENT_FILE_CHUNK_REQUEST:
return tox_event_file_chunk_request_unpack(&data->file_chunk_request, bu, mem);
case TOX_EVENT_FILE_RECV_CHUNK:
return tox_event_file_recv_chunk_unpack(&data->file_recv_chunk, bu, mem);
case TOX_EVENT_FILE_RECV_CONTROL:
return tox_event_file_recv_control_unpack(&data->file_recv_control, bu, mem);
case TOX_EVENT_FILE_RECV:
return tox_event_file_recv_unpack(&data->file_recv, bu, mem);
case TOX_EVENT_FRIEND_CONNECTION_STATUS:
return tox_event_friend_connection_status_unpack(&data->friend_connection_status, bu, mem);
case TOX_EVENT_FRIEND_LOSSLESS_PACKET:
return tox_event_friend_lossless_packet_unpack(&data->friend_lossless_packet, bu, mem);
case TOX_EVENT_FRIEND_LOSSY_PACKET:
return tox_event_friend_lossy_packet_unpack(&data->friend_lossy_packet, bu, mem);
case TOX_EVENT_FRIEND_MESSAGE:
return tox_event_friend_message_unpack(&data->friend_message, bu, mem);
case TOX_EVENT_FRIEND_NAME:
return tox_event_friend_name_unpack(&data->friend_name, bu, mem);
case TOX_EVENT_FRIEND_READ_RECEIPT:
return tox_event_friend_read_receipt_unpack(&data->friend_read_receipt, bu, mem);
case TOX_EVENT_FRIEND_REQUEST:
return tox_event_friend_request_unpack(&data->friend_request, bu, mem);
case TOX_EVENT_FRIEND_STATUS_MESSAGE:
return tox_event_friend_status_message_unpack(&data->friend_status_message, bu, mem);
case TOX_EVENT_FRIEND_STATUS:
return tox_event_friend_status_unpack(&data->friend_status, bu, mem);
case TOX_EVENT_FRIEND_TYPING:
return tox_event_friend_typing_unpack(&data->friend_typing, bu, mem);
case TOX_EVENT_SELF_CONNECTION_STATUS:
return tox_event_self_connection_status_unpack(&data->self_connection_status, bu, mem);
case TOX_EVENT_GROUP_PEER_NAME:
return tox_event_group_peer_name_unpack(&data->group_peer_name, bu, mem);
case TOX_EVENT_GROUP_PEER_STATUS:
return tox_event_group_peer_status_unpack(&data->group_peer_status, bu, mem);
case TOX_EVENT_GROUP_TOPIC:
return tox_event_group_topic_unpack(&data->group_topic, bu, mem);
case TOX_EVENT_GROUP_PRIVACY_STATE:
return tox_event_group_privacy_state_unpack(&data->group_privacy_state, bu, mem);
case TOX_EVENT_GROUP_VOICE_STATE:
return tox_event_group_voice_state_unpack(&data->group_voice_state, bu, mem);
case TOX_EVENT_GROUP_TOPIC_LOCK:
return tox_event_group_topic_lock_unpack(&data->group_topic_lock, bu, mem);
case TOX_EVENT_GROUP_PEER_LIMIT:
return tox_event_group_peer_limit_unpack(&data->group_peer_limit, bu, mem);
case TOX_EVENT_GROUP_PASSWORD:
return tox_event_group_password_unpack(&data->group_password, bu, mem);
case TOX_EVENT_GROUP_MESSAGE:
return tox_event_group_message_unpack(&data->group_message, bu, mem);
case TOX_EVENT_GROUP_PRIVATE_MESSAGE:
return tox_event_group_private_message_unpack(&data->group_private_message, bu, mem);
case TOX_EVENT_GROUP_CUSTOM_PACKET:
return tox_event_group_custom_packet_unpack(&data->group_custom_packet, bu, mem);
case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET:
return tox_event_group_custom_private_packet_unpack(&data->group_custom_private_packet, bu, mem);
case TOX_EVENT_GROUP_INVITE:
return tox_event_group_invite_unpack(&data->group_invite, bu, mem);
case TOX_EVENT_GROUP_PEER_JOIN:
return tox_event_group_peer_join_unpack(&data->group_peer_join, bu, mem);
case TOX_EVENT_GROUP_PEER_EXIT:
return tox_event_group_peer_exit_unpack(&data->group_peer_exit, bu, mem);
case TOX_EVENT_GROUP_SELF_JOIN:
return tox_event_group_self_join_unpack(&data->group_self_join, bu, mem);
case TOX_EVENT_GROUP_JOIN_FAIL:
return tox_event_group_join_fail_unpack(&data->group_join_fail, bu, mem);
case TOX_EVENT_GROUP_MODERATION:
return tox_event_group_moderation_unpack(&data->group_moderation, bu, mem);
case TOX_EVENT_DHT_GET_NODES_RESPONSE:
return tox_event_dht_get_nodes_response_unpack(&data->dht_get_nodes_response, bu, mem);
case TOX_EVENT_INVALID:
return false;
}
return false;
}
bool tox_event_unpack_into(Tox_Event *event, Bin_Unpack *bu, const Memory *mem) bool tox_event_unpack_into(Tox_Event *event, Bin_Unpack *bu, const Memory *mem)
{ {
uint32_t size; uint32_t size;
@ -965,130 +1096,5 @@ bool tox_event_unpack_into(Tox_Event *event, Bin_Unpack *bu, const Memory *mem)
event->type = type; event->type = type;
switch (type) { return tox_event_data_unpack(event->type, &event->data, bu, mem);
case TOX_EVENT_CONFERENCE_CONNECTED:
return tox_event_conference_connected_unpack(&event->data.conference_connected, bu, mem);
case TOX_EVENT_CONFERENCE_INVITE:
return tox_event_conference_invite_unpack(&event->data.conference_invite, bu, mem);
case TOX_EVENT_CONFERENCE_MESSAGE:
return tox_event_conference_message_unpack(&event->data.conference_message, bu, mem);
case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED:
return tox_event_conference_peer_list_changed_unpack(&event->data.conference_peer_list_changed, bu, mem);
case TOX_EVENT_CONFERENCE_PEER_NAME:
return tox_event_conference_peer_name_unpack(&event->data.conference_peer_name, bu, mem);
case TOX_EVENT_CONFERENCE_TITLE:
return tox_event_conference_title_unpack(&event->data.conference_title, bu, mem);
case TOX_EVENT_FILE_CHUNK_REQUEST:
return tox_event_file_chunk_request_unpack(&event->data.file_chunk_request, bu, mem);
case TOX_EVENT_FILE_RECV_CHUNK:
return tox_event_file_recv_chunk_unpack(&event->data.file_recv_chunk, bu, mem);
case TOX_EVENT_FILE_RECV_CONTROL:
return tox_event_file_recv_control_unpack(&event->data.file_recv_control, bu, mem);
case TOX_EVENT_FILE_RECV:
return tox_event_file_recv_unpack(&event->data.file_recv, bu, mem);
case TOX_EVENT_FRIEND_CONNECTION_STATUS:
return tox_event_friend_connection_status_unpack(&event->data.friend_connection_status, bu, mem);
case TOX_EVENT_FRIEND_LOSSLESS_PACKET:
return tox_event_friend_lossless_packet_unpack(&event->data.friend_lossless_packet, bu, mem);
case TOX_EVENT_FRIEND_LOSSY_PACKET:
return tox_event_friend_lossy_packet_unpack(&event->data.friend_lossy_packet, bu, mem);
case TOX_EVENT_FRIEND_MESSAGE:
return tox_event_friend_message_unpack(&event->data.friend_message, bu, mem);
case TOX_EVENT_FRIEND_NAME:
return tox_event_friend_name_unpack(&event->data.friend_name, bu, mem);
case TOX_EVENT_FRIEND_READ_RECEIPT:
return tox_event_friend_read_receipt_unpack(&event->data.friend_read_receipt, bu, mem);
case TOX_EVENT_FRIEND_REQUEST:
return tox_event_friend_request_unpack(&event->data.friend_request, bu, mem);
case TOX_EVENT_FRIEND_STATUS_MESSAGE:
return tox_event_friend_status_message_unpack(&event->data.friend_status_message, bu, mem);
case TOX_EVENT_FRIEND_STATUS:
return tox_event_friend_status_unpack(&event->data.friend_status, bu, mem);
case TOX_EVENT_FRIEND_TYPING:
return tox_event_friend_typing_unpack(&event->data.friend_typing, bu, mem);
case TOX_EVENT_SELF_CONNECTION_STATUS:
return tox_event_self_connection_status_unpack(&event->data.self_connection_status, bu, mem);
case TOX_EVENT_GROUP_PEER_NAME:
return tox_event_group_peer_name_unpack(&event->data.group_peer_name, bu, mem);
case TOX_EVENT_GROUP_PEER_STATUS:
return tox_event_group_peer_status_unpack(&event->data.group_peer_status, bu, mem);
case TOX_EVENT_GROUP_TOPIC:
return tox_event_group_topic_unpack(&event->data.group_topic, bu, mem);
case TOX_EVENT_GROUP_PRIVACY_STATE:
return tox_event_group_privacy_state_unpack(&event->data.group_privacy_state, bu, mem);
case TOX_EVENT_GROUP_VOICE_STATE:
return tox_event_group_voice_state_unpack(&event->data.group_voice_state, bu, mem);
case TOX_EVENT_GROUP_TOPIC_LOCK:
return tox_event_group_topic_lock_unpack(&event->data.group_topic_lock, bu, mem);
case TOX_EVENT_GROUP_PEER_LIMIT:
return tox_event_group_peer_limit_unpack(&event->data.group_peer_limit, bu, mem);
case TOX_EVENT_GROUP_PASSWORD:
return tox_event_group_password_unpack(&event->data.group_password, bu, mem);
case TOX_EVENT_GROUP_MESSAGE:
return tox_event_group_message_unpack(&event->data.group_message, bu, mem);
case TOX_EVENT_GROUP_PRIVATE_MESSAGE:
return tox_event_group_private_message_unpack(&event->data.group_private_message, bu, mem);
case TOX_EVENT_GROUP_CUSTOM_PACKET:
return tox_event_group_custom_packet_unpack(&event->data.group_custom_packet, bu, mem);
case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET:
return tox_event_group_custom_private_packet_unpack(&event->data.group_custom_private_packet, bu, mem);
case TOX_EVENT_GROUP_INVITE:
return tox_event_group_invite_unpack(&event->data.group_invite, bu, mem);
case TOX_EVENT_GROUP_PEER_JOIN:
return tox_event_group_peer_join_unpack(&event->data.group_peer_join, bu, mem);
case TOX_EVENT_GROUP_PEER_EXIT:
return tox_event_group_peer_exit_unpack(&event->data.group_peer_exit, bu, mem);
case TOX_EVENT_GROUP_SELF_JOIN:
return tox_event_group_self_join_unpack(&event->data.group_self_join, bu, mem);
case TOX_EVENT_GROUP_JOIN_FAIL:
return tox_event_group_join_fail_unpack(&event->data.group_join_fail, bu, mem);
case TOX_EVENT_GROUP_MODERATION:
return tox_event_group_moderation_unpack(&event->data.group_moderation, bu, mem);
case TOX_EVENT_DHT_GET_NODES_RESPONSE:
return tox_event_dht_get_nodes_response_unpack(&event->data.dht_get_nodes_response, bu, mem);
case TOX_EVENT_INVALID:
return false;
}
return false;
} }

View File

@ -75,6 +75,12 @@ uint32_t tox_events_get_size(const Tox_Events *events)
return events == nullptr ? 0 : events->events_size; return events == nullptr ? 0 : events->events_size;
} }
nullable(1)
static const Tox_Event *tox_events_get_events(const Tox_Events *events)
{
return events == nullptr ? nullptr : events->events;
}
const Tox_Event *tox_events_get(const Tox_Events *events, uint32_t index) const Tox_Event *tox_events_get(const Tox_Events *events, uint32_t index)
{ {
if (index >= tox_events_get_size(events)) { if (index >= tox_events_get_size(events)) {
@ -102,26 +108,29 @@ Tox_Events *tox_events_iterate(Tox *tox, bool fail_hard, Tox_Err_Events_Iterate
return state.events; return state.events;
} }
non_null()
static bool tox_event_pack_handler(const void *obj, uint32_t index, const Logger *logger, Bin_Pack *bp)
{
const Tox_Event *events = (const Tox_Event *)obj;
assert(events != nullptr);
return tox_event_pack(&events[index], bp);
}
non_null(3) nullable(1, 2) non_null(3) nullable(1, 2)
static bool tox_events_pack(const void *obj, const Logger *logger, Bin_Pack *bp) static bool tox_events_pack_handler(const void *obj, const Logger *logger, Bin_Pack *bp)
{ {
const Tox_Events *events = (const Tox_Events *)obj; const Tox_Events *events = (const Tox_Events *)obj;
return bin_pack_obj_array(bp, tox_event_pack_handler, tox_events_get_events(events), tox_events_get_size(events), logger);
}
if (events == nullptr) { uint32_t tox_events_bytes_size(const Tox_Events *events)
return bin_pack_array(bp, 0); {
} return bin_pack_obj_size(tox_events_pack_handler, events, nullptr);
}
if (!bin_pack_array(bp, events->events_size)) { bool tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes)
return false; {
} return bin_pack_obj(tox_events_pack_handler, events, nullptr, bytes, UINT32_MAX);
for (uint32_t i = 0; i < events->events_size; ++i) {
if (!tox_event_pack(&events->events[i], bp)) {
return false;
}
}
return true;
} }
non_null() non_null()
@ -152,16 +161,6 @@ static bool tox_events_unpack(void *obj, Bin_Unpack *bu)
return true; return true;
} }
uint32_t tox_events_bytes_size(const Tox_Events *events)
{
return bin_pack_obj_size(tox_events_pack, events, nullptr);
}
bool tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes)
{
return bin_pack_obj(tox_events_pack, events, nullptr, bytes, UINT32_MAX);
}
Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_t bytes_size) Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_t bytes_size)
{ {
Tox_Events *events = (Tox_Events *)mem_alloc(sys->mem, sizeof(Tox_Events)); Tox_Events *events = (Tox_Events *)mem_alloc(sys->mem, sizeof(Tox_Events));