diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b808a96317a..d5c10cf275f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,6 +35,17 @@ jobs: - name: Build, test, and upload coverage run: other/docker/coverage/run + generate-events: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + - name: Run generate_event_c + run: | + other/event_tooling/run + git diff --exit-code + cimplefmt: runs-on: ubuntu-latest steps: diff --git a/CMakeLists.txt b/CMakeLists.txt index ba173ec3154..3bf7fe051ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -327,6 +327,8 @@ set(toxcore_SOURCES toxcore/tox.c toxcore/tox_dispatch.c toxcore/tox_dispatch.h + toxcore/tox_event.c + toxcore/tox_event.h toxcore/tox_events.c toxcore/tox_events.h toxcore/tox.h diff --git a/auto_tests/tox_dispatch_test.c b/auto_tests/tox_dispatch_test.c index b9e63a76b84..01294589e20 100644 --- a/auto_tests/tox_dispatch_test.c +++ b/auto_tests/tox_dispatch_test.c @@ -32,31 +32,36 @@ static void handle_events_friend_message(Tox *tox, const Tox_Event_Friend_Messag static void dump_events(const char *path, const Tox_Events *events) { - if (want_dump_events) { - FILE *fh = fopen(path, "w"); - ck_assert(fh != nullptr); - const uint32_t len = tox_events_bytes_size(events); - uint8_t *buf = (uint8_t *)malloc(len); - ck_assert(buf != nullptr); - tox_events_get_bytes(events, buf); - fwrite(buf, 1, len, fh); - free(buf); - fclose(fh); - } + FILE *fh = fopen(path, "w"); + ck_assert(fh != nullptr); + const uint32_t len = tox_events_bytes_size(events); + uint8_t *buf = (uint8_t *)malloc(len); + ck_assert(buf != nullptr); + ck_assert(tox_events_get_bytes(events, buf)); + fwrite(buf, 1, len, fh); + free(buf); + fclose(fh); } static void print_events(const Tox_System *sys, Tox_Events *events) { const uint32_t size = tox_events_bytes_size(events); - uint8_t *bytes = (uint8_t *)malloc(size); - ck_assert(bytes != nullptr); + uint8_t *bytes1 = (uint8_t *)malloc(size); + uint8_t *bytes2 = (uint8_t *)malloc(size); + ck_assert(bytes1 != nullptr); + ck_assert(bytes2 != nullptr); + + ck_assert(tox_events_get_bytes(events, bytes1)); + ck_assert(tox_events_get_bytes(events, bytes2)); - tox_events_get_bytes(events, bytes); + // Make sure get_bytes is deterministic. + ck_assert(memcmp(bytes1, bytes2, size) == 0); - Tox_Events *events_copy = tox_events_load(sys, bytes, size); + Tox_Events *events_copy = tox_events_load(sys, bytes1, size); ck_assert(events_copy != nullptr); - free(bytes); + free(bytes1); + free(bytes2); ck_assert(tox_events_equal(sys, events, events_copy)); @@ -74,7 +79,9 @@ static bool await_message(Tox **toxes, const Tox_Dispatch *dispatch) // Check if tox 2 got the message from tox 1. Tox_Events *events = tox_events_iterate(toxes[1], false, nullptr); - dump_events("/tmp/test.mp", events); + if (want_dump_events) { + dump_events("/tmp/test.mp", events); + } bool success = false; tox_dispatch_invoke(dispatch, events, toxes[1], &success); diff --git a/auto_tests/tox_events_test.c b/auto_tests/tox_events_test.c index 04549b2179c..918bc17ec0e 100644 --- a/auto_tests/tox_events_test.c +++ b/auto_tests/tox_events_test.c @@ -28,6 +28,15 @@ static bool await_message(Tox **toxes) const uint8_t *msg = tox_event_friend_message_get_message(msg_event); ck_assert_msg(memcmp(msg, "hello", sizeof("hello")) == 0, "message was not expected 'hello' but '%s'", (const char *)msg); + + const uint32_t event_count = tox_events_get_size(events); + for (uint32_t j = 0; j < event_count; ++j) { + const Tox_Event *event = tox_events_get(events, j); + if (tox_event_get_type(event) == TOX_EVENT_FRIEND_MESSAGE) { + ck_assert(tox_event_get_friend_message(event) == msg_event); + } + } + tox_events_free(events); return true; } diff --git a/other/event_tooling/Dockerfile b/other/event_tooling/Dockerfile new file mode 100644 index 00000000000..b94e6f0e7f8 --- /dev/null +++ b/other/event_tooling/Dockerfile @@ -0,0 +1,12 @@ +FROM ubuntu:22.04 + +RUN apt-get update && apt-get install --no-install-recommends -y \ + build-essential \ + ca-certificates \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /src +COPY generate_event_c.cpp /src/ +RUN ["g++", "generate_event_c.cpp", "-o", "generate_event_c"] +CMD ["./generate_event_c"] diff --git a/other/event_tooling/generate_event_c.cpp b/other/event_tooling/generate_event_c.cpp index d3ac0475db9..d440c3014e4 100644 --- a/other/event_tooling/generate_event_c.cpp +++ b/other/event_tooling/generate_event_c.cpp @@ -524,7 +524,7 @@ int main(int argc, char** argv) { EventTypeTrivial{"uint32_t", "file_number"}, EventTypeTrivial{"uint32_t", "kind"}, EventTypeTrivial{"uint64_t", "file_size"}, - EventTypeByteRange{"filename", "filename_length", "length"}, // the latter two are ideally the same + EventTypeByteRange{"filename", "filename_length", "filename_length"}, } }, { diff --git a/other/event_tooling/run b/other/event_tooling/run new file mode 100755 index 00000000000..95e2364649d --- /dev/null +++ b/other/event_tooling/run @@ -0,0 +1,8 @@ +#!/bin/sh + +set -eux +BUILD=events +docker build -t "toxchat/c-toxcore:$BUILD" "other/event_tooling" +docker run --rm -v "$PWD/toxcore/events:/src/out" -t "toxchat/c-toxcore:$BUILD" +sed -i -e 's/, uint16_t length,/, size_t length,/' toxcore/events/file_chunk_request.c +sed -i -e 's/^ 0/ TOX_CONNECTION_NONE/' toxcore/events/self_connection_status.c diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index bf6bfba78fb..450c17a31f1 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -892,11 +892,17 @@ cc_library( cc_library( name = "tox_events", - srcs = ["tox_events.c"] + glob([ + srcs = [ + "tox_event.c", + "tox_events.c", + ] + glob([ "events/*.c", - "events/*.h", ]), - hdrs = ["tox_events.h"], + hdrs = [ + "events/events_alloc.h", + "tox_event.h", + "tox_events.h", + ], visibility = ["//c-toxcore:__subpackages__"], deps = [ ":attributes", diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index eecd52b7920..f8e6a454623 100644 --- a/toxcore/Makefile.inc +++ b/toxcore/Makefile.inc @@ -21,6 +21,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \ ../toxcore/events/conference_peer_list_changed.c \ ../toxcore/events/conference_peer_name.c \ ../toxcore/events/conference_title.c \ + ../toxcore/events/events_alloc.c \ + ../toxcore/events/events_alloc.h \ ../toxcore/events/file_chunk_request.c \ ../toxcore/events/file_recv.c \ ../toxcore/events/file_recv_chunk.c \ @@ -35,8 +37,6 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \ ../toxcore/events/friend_status.c \ ../toxcore/events/friend_status_message.c \ ../toxcore/events/friend_typing.c \ - ../toxcore/events/events_alloc.c \ - ../toxcore/events/events_alloc.h \ ../toxcore/events/self_connection_status.c \ ../toxcore/DHT.h \ ../toxcore/DHT.c \ @@ -72,6 +72,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \ ../toxcore/tox.c \ ../toxcore/tox_dispatch.h \ ../toxcore/tox_dispatch.c \ + ../toxcore/tox_event.h \ + ../toxcore/tox_event.c \ ../toxcore/tox_events.h \ ../toxcore/tox_events.c \ ../toxcore/tox_unpack.h \ diff --git a/toxcore/events/conference_connected.c b/toxcore/events/conference_connected.c index fe51503f02e..a6c6ad22c3d 100644 --- a/toxcore/events/conference_connected.c +++ b/toxcore/events/conference_connected.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -26,34 +27,32 @@ struct Tox_Event_Conference_Connected { }; non_null() -static void tox_event_conference_connected_construct(Tox_Event_Conference_Connected *conference_connected) +static void tox_event_conference_connected_set_conference_number(Tox_Event_Conference_Connected *conference_connected, + uint32_t conference_number) { - *conference_connected = (Tox_Event_Conference_Connected) { - 0 - }; + assert(conference_connected != nullptr); + conference_connected->conference_number = conference_number; } -non_null() -static void tox_event_conference_connected_destruct(Tox_Event_Conference_Connected *conference_connected) +uint32_t tox_event_conference_connected_get_conference_number(const Tox_Event_Conference_Connected *conference_connected) { - return; + assert(conference_connected != nullptr); + return conference_connected->conference_number; } non_null() -static void tox_event_conference_connected_set_conference_number( - Tox_Event_Conference_Connected *conference_connected, uint32_t conference_number) +static void tox_event_conference_connected_construct(Tox_Event_Conference_Connected *conference_connected) { - assert(conference_connected != nullptr); - conference_connected->conference_number = conference_number; + *conference_connected = (Tox_Event_Conference_Connected) { + 0 + }; } -uint32_t tox_event_conference_connected_get_conference_number( - const Tox_Event_Conference_Connected *conference_connected) +non_null() +static void tox_event_conference_connected_destruct(Tox_Event_Conference_Connected *conference_connected, const Memory *mem) { - assert(conference_connected != nullptr); - return conference_connected->conference_number; + return; } -non_null() -static bool tox_event_conference_connected_pack( +bool tox_event_conference_connected_pack( const Tox_Event_Conference_Connected *event, Bin_Pack *bp) { assert(event != nullptr); @@ -63,7 +62,7 @@ static bool tox_event_conference_connected_pack( } non_null() -static bool tox_event_conference_connected_unpack( +static bool tox_event_conference_connected_unpack_into( Tox_Event_Conference_Connected *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -73,93 +72,121 @@ static bool tox_event_conference_connected_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Conference_Connected *tox_events_add_conference_connected(Tox_Events *events) +const Tox_Event_Conference_Connected *tox_event_get_conference_connected(const Tox_Event *event) { - if (events->conference_connected_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_connected_size == events->conference_connected_capacity) { - const uint32_t new_conference_connected_capacity = events->conference_connected_capacity * 2 + 1; - Tox_Event_Conference_Connected *new_conference_connected = (Tox_Event_Conference_Connected *)realloc( - events->conference_connected, new_conference_connected_capacity * sizeof(Tox_Event_Conference_Connected)); + return event->type == TOX_EVENT_CONFERENCE_CONNECTED ? event->data.conference_connected : nullptr; +} - if (new_conference_connected == nullptr) { - return nullptr; - } +Tox_Event_Conference_Connected *tox_event_conference_connected_new(const Memory *mem) +{ + Tox_Event_Conference_Connected *const conference_connected = + (Tox_Event_Conference_Connected *)mem_alloc(mem, sizeof(Tox_Event_Conference_Connected)); - events->conference_connected = new_conference_connected; - events->conference_connected_capacity = new_conference_connected_capacity; + if (conference_connected == nullptr) { + return nullptr; } - Tox_Event_Conference_Connected *const conference_connected = - &events->conference_connected[events->conference_connected_size]; tox_event_conference_connected_construct(conference_connected); - ++events->conference_connected_size; return conference_connected; } -void tox_events_clear_conference_connected(Tox_Events *events) +void tox_event_conference_connected_free(Tox_Event_Conference_Connected *conference_connected, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->conference_connected_size; ++i) { - tox_event_conference_connected_destruct(&events->conference_connected[i]); + if (conference_connected != nullptr) { + tox_event_conference_connected_destruct(conference_connected, mem); } - - free(events->conference_connected); - events->conference_connected = nullptr; - events->conference_connected_size = 0; - events->conference_connected_capacity = 0; + mem_delete(mem, conference_connected); } -uint32_t tox_events_get_conference_connected_size(const Tox_Events *events) +non_null() +static Tox_Event_Conference_Connected *tox_events_add_conference_connected(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Conference_Connected *const conference_connected = tox_event_conference_connected_new(mem); + + if (conference_connected == nullptr) { + return nullptr; } - return events->conference_connected_size; + Tox_Event event; + event.type = TOX_EVENT_CONFERENCE_CONNECTED; + event.data.conference_connected = conference_connected; + + tox_events_add(events, &event); + return conference_connected; } const Tox_Event_Conference_Connected *tox_events_get_conference_connected(const Tox_Events *events, uint32_t index) { - assert(index < events->conference_connected_size); - assert(events->conference_connected != nullptr); - return &events->conference_connected[index]; + uint32_t conference_connected_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (conference_connected_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_CONFERENCE_CONNECTED) { + const Tox_Event_Conference_Connected *conference_connected = events->events[i].data.conference_connected; + if (conference_connected_index == index) { + return conference_connected; + } + ++conference_connected_index; + } + } + + return nullptr; } -bool tox_events_pack_conference_connected(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_conference_connected_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_conference_connected_size(events); + uint32_t conference_connected_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_connected_pack(tox_events_get_conference_connected(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_CONFERENCE_CONNECTED) { + ++conference_connected_size; } } - return true; + + return conference_connected_size; } -bool tox_events_unpack_conference_connected(Tox_Events *events, Bin_Unpack *bu) +bool tox_event_conference_connected_unpack( + Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem) { - Tox_Event_Conference_Connected *event = tox_events_add_conference_connected(events); + assert(event != nullptr); + *event = tox_event_conference_connected_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_conference_connected_unpack(event, bu); + return tox_event_conference_connected_unpack_into(*event, bu); } +non_null() +static Tox_Event_Conference_Connected *tox_event_conference_connected_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Conference_Connected *conference_connected = tox_events_add_conference_connected(state->events, state->mem); + + if (conference_connected == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return conference_connected; +} /***************************************************** @@ -169,19 +196,12 @@ bool tox_events_unpack_conference_connected(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_conference_connected(Tox *tox, uint32_t conference_number, void *user_data) +void tox_events_handle_conference_connected(Tox *tox, uint32_t conference_number, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Connected *conference_connected = tox_events_add_conference_connected(state->events); + Tox_Event_Conference_Connected *conference_connected = tox_event_conference_connected_alloc(user_data); if (conference_connected == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/conference_invite.c b/toxcore/events/conference_invite.c index a3a4c2380e7..75d39182177 100644 --- a/toxcore/events/conference_invite.c +++ b/toxcore/events/conference_invite.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -30,19 +30,6 @@ struct Tox_Event_Conference_Invite { uint32_t cookie_length; }; -non_null() -static void tox_event_conference_invite_construct(Tox_Event_Conference_Invite *conference_invite) -{ - *conference_invite = (Tox_Event_Conference_Invite) { - 0 - }; -} -non_null() -static void tox_event_conference_invite_destruct(Tox_Event_Conference_Invite *conference_invite) -{ - free(conference_invite->cookie); -} - non_null() static void tox_event_conference_invite_set_friend_number(Tox_Event_Conference_Invite *conference_invite, uint32_t friend_number) @@ -104,7 +91,19 @@ const uint8_t *tox_event_conference_invite_get_cookie(const Tox_Event_Conference } non_null() -static bool tox_event_conference_invite_pack( +static void tox_event_conference_invite_construct(Tox_Event_Conference_Invite *conference_invite) +{ + *conference_invite = (Tox_Event_Conference_Invite) { + 0 + }; +} +non_null() +static void tox_event_conference_invite_destruct(Tox_Event_Conference_Invite *conference_invite, const Memory *mem) +{ + free(conference_invite->cookie); +} + +bool tox_event_conference_invite_pack( const Tox_Event_Conference_Invite *event, Bin_Pack *bp) { assert(event != nullptr); @@ -117,7 +116,7 @@ static bool tox_event_conference_invite_pack( } non_null() -static bool tox_event_conference_invite_unpack( +static bool tox_event_conference_invite_unpack_into( Tox_Event_Conference_Invite *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -133,90 +132,120 @@ static bool tox_event_conference_invite_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Conference_Invite *tox_events_add_conference_invite(Tox_Events *events) +const Tox_Event_Conference_Invite *tox_event_get_conference_invite(const Tox_Event *event) { - if (events->conference_invite_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_invite_size == events->conference_invite_capacity) { - const uint32_t new_conference_invite_capacity = events->conference_invite_capacity * 2 + 1; - Tox_Event_Conference_Invite *new_conference_invite = (Tox_Event_Conference_Invite *)realloc( - events->conference_invite, new_conference_invite_capacity * sizeof(Tox_Event_Conference_Invite)); + return event->type == TOX_EVENT_CONFERENCE_INVITE ? event->data.conference_invite : nullptr; +} - if (new_conference_invite == nullptr) { - return nullptr; - } +Tox_Event_Conference_Invite *tox_event_conference_invite_new(const Memory *mem) +{ + Tox_Event_Conference_Invite *const conference_invite = + (Tox_Event_Conference_Invite *)mem_alloc(mem, sizeof(Tox_Event_Conference_Invite)); - events->conference_invite = new_conference_invite; - events->conference_invite_capacity = new_conference_invite_capacity; + if (conference_invite == nullptr) { + return nullptr; } - Tox_Event_Conference_Invite *const conference_invite = &events->conference_invite[events->conference_invite_size]; tox_event_conference_invite_construct(conference_invite); - ++events->conference_invite_size; return conference_invite; } -void tox_events_clear_conference_invite(Tox_Events *events) +void tox_event_conference_invite_free(Tox_Event_Conference_Invite *conference_invite, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->conference_invite_size; ++i) { - tox_event_conference_invite_destruct(&events->conference_invite[i]); + if (conference_invite != nullptr) { + tox_event_conference_invite_destruct(conference_invite, mem); } - - free(events->conference_invite); - events->conference_invite = nullptr; - events->conference_invite_size = 0; - events->conference_invite_capacity = 0; + mem_delete(mem, conference_invite); } -uint32_t tox_events_get_conference_invite_size(const Tox_Events *events) +non_null() +static Tox_Event_Conference_Invite *tox_events_add_conference_invite(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Conference_Invite *const conference_invite = tox_event_conference_invite_new(mem); + + if (conference_invite == nullptr) { + return nullptr; } - return events->conference_invite_size; + Tox_Event event; + event.type = TOX_EVENT_CONFERENCE_INVITE; + event.data.conference_invite = conference_invite; + + tox_events_add(events, &event); + return conference_invite; } const Tox_Event_Conference_Invite *tox_events_get_conference_invite(const Tox_Events *events, uint32_t index) { - assert(index < events->conference_invite_size); - assert(events->conference_invite != nullptr); - return &events->conference_invite[index]; + uint32_t conference_invite_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (conference_invite_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_CONFERENCE_INVITE) { + const Tox_Event_Conference_Invite *conference_invite = events->events[i].data.conference_invite; + if (conference_invite_index == index) { + return conference_invite; + } + ++conference_invite_index; + } + } + + return nullptr; } -bool tox_events_pack_conference_invite(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_conference_invite_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_conference_invite_size(events); + uint32_t conference_invite_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_invite_pack(tox_events_get_conference_invite(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_CONFERENCE_INVITE) { + ++conference_invite_size; } } - return true; + + return conference_invite_size; } -bool tox_events_unpack_conference_invite(Tox_Events *events, Bin_Unpack *bu) +bool tox_event_conference_invite_unpack( + Tox_Event_Conference_Invite **event, Bin_Unpack *bu, const Memory *mem) { - Tox_Event_Conference_Invite *event = tox_events_add_conference_invite(events); + assert(event != nullptr); + *event = tox_event_conference_invite_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_conference_invite_unpack(event, bu); + return tox_event_conference_invite_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Conference_Invite *tox_event_conference_invite_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Conference_Invite *conference_invite = tox_events_add_conference_invite(state->events, state->mem); + + if (conference_invite == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return conference_invite; } @@ -227,20 +256,12 @@ bool tox_events_unpack_conference_invite(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_conference_invite(Tox *tox, uint32_t friend_number, Tox_Conference_Type type, - const uint8_t *cookie, size_t length, void *user_data) +void tox_events_handle_conference_invite(Tox *tox, uint32_t friend_number, Tox_Conference_Type type, const uint8_t *cookie, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Invite *conference_invite = tox_events_add_conference_invite(state->events); + Tox_Event_Conference_Invite *conference_invite = tox_event_conference_invite_alloc(user_data); if (conference_invite == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/conference_message.c b/toxcore/events/conference_message.c index 6910086abc3..1574b68d6e4 100644 --- a/toxcore/events/conference_message.c +++ b/toxcore/events/conference_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -31,19 +31,6 @@ struct Tox_Event_Conference_Message { uint32_t message_length; }; -non_null() -static void tox_event_conference_message_construct(Tox_Event_Conference_Message *conference_message) -{ - *conference_message = (Tox_Event_Conference_Message) { - 0 - }; -} -non_null() -static void tox_event_conference_message_destruct(Tox_Event_Conference_Message *conference_message) -{ - free(conference_message->message); -} - non_null() static void tox_event_conference_message_set_conference_number(Tox_Event_Conference_Message *conference_message, uint32_t conference_number) @@ -118,7 +105,19 @@ const uint8_t *tox_event_conference_message_get_message(const Tox_Event_Conferen } non_null() -static bool tox_event_conference_message_pack( +static void tox_event_conference_message_construct(Tox_Event_Conference_Message *conference_message) +{ + *conference_message = (Tox_Event_Conference_Message) { + 0 + }; +} +non_null() +static void tox_event_conference_message_destruct(Tox_Event_Conference_Message *conference_message, const Memory *mem) +{ + free(conference_message->message); +} + +bool tox_event_conference_message_pack( const Tox_Event_Conference_Message *event, Bin_Pack *bp) { assert(event != nullptr); @@ -132,7 +131,7 @@ static bool tox_event_conference_message_pack( } non_null() -static bool tox_event_conference_message_unpack( +static bool tox_event_conference_message_unpack_into( Tox_Event_Conference_Message *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -149,90 +148,120 @@ static bool tox_event_conference_message_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Conference_Message *tox_events_add_conference_message(Tox_Events *events) +const Tox_Event_Conference_Message *tox_event_get_conference_message(const Tox_Event *event) { - if (events->conference_message_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_message_size == events->conference_message_capacity) { - const uint32_t new_conference_message_capacity = events->conference_message_capacity * 2 + 1; - Tox_Event_Conference_Message *new_conference_message = (Tox_Event_Conference_Message *)realloc( - events->conference_message, new_conference_message_capacity * sizeof(Tox_Event_Conference_Message)); + return event->type == TOX_EVENT_CONFERENCE_MESSAGE ? event->data.conference_message : nullptr; +} - if (new_conference_message == nullptr) { - return nullptr; - } +Tox_Event_Conference_Message *tox_event_conference_message_new(const Memory *mem) +{ + Tox_Event_Conference_Message *const conference_message = + (Tox_Event_Conference_Message *)mem_alloc(mem, sizeof(Tox_Event_Conference_Message)); - events->conference_message = new_conference_message; - events->conference_message_capacity = new_conference_message_capacity; + if (conference_message == nullptr) { + return nullptr; } - Tox_Event_Conference_Message *const conference_message = &events->conference_message[events->conference_message_size]; tox_event_conference_message_construct(conference_message); - ++events->conference_message_size; return conference_message; } -void tox_events_clear_conference_message(Tox_Events *events) +void tox_event_conference_message_free(Tox_Event_Conference_Message *conference_message, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->conference_message_size; ++i) { - tox_event_conference_message_destruct(&events->conference_message[i]); + if (conference_message != nullptr) { + tox_event_conference_message_destruct(conference_message, mem); } - - free(events->conference_message); - events->conference_message = nullptr; - events->conference_message_size = 0; - events->conference_message_capacity = 0; + mem_delete(mem, conference_message); } -uint32_t tox_events_get_conference_message_size(const Tox_Events *events) +non_null() +static Tox_Event_Conference_Message *tox_events_add_conference_message(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Conference_Message *const conference_message = tox_event_conference_message_new(mem); + + if (conference_message == nullptr) { + return nullptr; } - return events->conference_message_size; + Tox_Event event; + event.type = TOX_EVENT_CONFERENCE_MESSAGE; + event.data.conference_message = conference_message; + + tox_events_add(events, &event); + return conference_message; } const Tox_Event_Conference_Message *tox_events_get_conference_message(const Tox_Events *events, uint32_t index) { - assert(index < events->conference_message_size); - assert(events->conference_message != nullptr); - return &events->conference_message[index]; + uint32_t conference_message_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (conference_message_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_CONFERENCE_MESSAGE) { + const Tox_Event_Conference_Message *conference_message = events->events[i].data.conference_message; + if (conference_message_index == index) { + return conference_message; + } + ++conference_message_index; + } + } + + return nullptr; } -bool tox_events_pack_conference_message(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_conference_message_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_conference_message_size(events); + uint32_t conference_message_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_message_pack(tox_events_get_conference_message(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_CONFERENCE_MESSAGE) { + ++conference_message_size; } } - return true; + + return conference_message_size; } -bool tox_events_unpack_conference_message(Tox_Events *events, Bin_Unpack *bu) +bool tox_event_conference_message_unpack( + Tox_Event_Conference_Message **event, Bin_Unpack *bu, const Memory *mem) { - Tox_Event_Conference_Message *event = tox_events_add_conference_message(events); + assert(event != nullptr); + *event = tox_event_conference_message_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_conference_message_unpack(event, bu); + return tox_event_conference_message_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Conference_Message *tox_event_conference_message_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Conference_Message *conference_message = tox_events_add_conference_message(state->events, state->mem); + + if (conference_message == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return conference_message; } @@ -243,20 +272,12 @@ bool tox_events_unpack_conference_message(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_conference_message(Tox *tox, uint32_t conference_number, uint32_t peer_number, - Tox_Message_Type type, const uint8_t *message, size_t length, void *user_data) +void tox_events_handle_conference_message(Tox *tox, uint32_t conference_number, uint32_t peer_number, Tox_Message_Type type, const uint8_t *message, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Message *conference_message = tox_events_add_conference_message(state->events); + Tox_Event_Conference_Message *conference_message = tox_event_conference_message_alloc(user_data); if (conference_message == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/conference_peer_list_changed.c b/toxcore/events/conference_peer_list_changed.c index 9360f89dbfb..3d7cc1c6808 100644 --- a/toxcore/events/conference_peer_list_changed.c +++ b/toxcore/events/conference_peer_list_changed.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -26,36 +27,32 @@ struct Tox_Event_Conference_Peer_List_Changed { }; non_null() -static void tox_event_conference_peer_list_changed_construct(Tox_Event_Conference_Peer_List_Changed - *conference_peer_list_changed) +static void tox_event_conference_peer_list_changed_set_conference_number(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed, + uint32_t conference_number) { - *conference_peer_list_changed = (Tox_Event_Conference_Peer_List_Changed) { - 0 - }; + assert(conference_peer_list_changed != nullptr); + conference_peer_list_changed->conference_number = conference_number; } -non_null() -static void tox_event_conference_peer_list_changed_destruct(Tox_Event_Conference_Peer_List_Changed - *conference_peer_list_changed) +uint32_t tox_event_conference_peer_list_changed_get_conference_number(const Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed) { - return; + assert(conference_peer_list_changed != nullptr); + return conference_peer_list_changed->conference_number; } non_null() -static void tox_event_conference_peer_list_changed_set_conference_number(Tox_Event_Conference_Peer_List_Changed - *conference_peer_list_changed, uint32_t conference_number) +static void tox_event_conference_peer_list_changed_construct(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed) { - assert(conference_peer_list_changed != nullptr); - conference_peer_list_changed->conference_number = conference_number; + *conference_peer_list_changed = (Tox_Event_Conference_Peer_List_Changed) { + 0 + }; } -uint32_t tox_event_conference_peer_list_changed_get_conference_number(const Tox_Event_Conference_Peer_List_Changed - *conference_peer_list_changed) +non_null() +static void tox_event_conference_peer_list_changed_destruct(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed, const Memory *mem) { - assert(conference_peer_list_changed != nullptr); - return conference_peer_list_changed->conference_number; + return; } -non_null() -static bool tox_event_conference_peer_list_changed_pack( +bool tox_event_conference_peer_list_changed_pack( const Tox_Event_Conference_Peer_List_Changed *event, Bin_Pack *bp) { assert(event != nullptr); @@ -65,7 +62,7 @@ static bool tox_event_conference_peer_list_changed_pack( } non_null() -static bool tox_event_conference_peer_list_changed_unpack( +static bool tox_event_conference_peer_list_changed_unpack_into( Tox_Event_Conference_Peer_List_Changed *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -75,94 +72,120 @@ static bool tox_event_conference_peer_list_changed_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ +const Tox_Event_Conference_Peer_List_Changed *tox_event_get_conference_peer_list_changed(const Tox_Event *event) +{ + return event->type == TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED ? event->data.conference_peer_list_changed : nullptr; +} -non_null() -static Tox_Event_Conference_Peer_List_Changed *tox_events_add_conference_peer_list_changed(Tox_Events *events) +Tox_Event_Conference_Peer_List_Changed *tox_event_conference_peer_list_changed_new(const Memory *mem) { - if (events->conference_peer_list_changed_size == UINT32_MAX) { + Tox_Event_Conference_Peer_List_Changed *const conference_peer_list_changed = + (Tox_Event_Conference_Peer_List_Changed *)mem_alloc(mem, sizeof(Tox_Event_Conference_Peer_List_Changed)); + + if (conference_peer_list_changed == nullptr) { return nullptr; } - if (events->conference_peer_list_changed_size == events->conference_peer_list_changed_capacity) { - const uint32_t new_conference_peer_list_changed_capacity = events->conference_peer_list_changed_capacity * 2 + 1; - Tox_Event_Conference_Peer_List_Changed *new_conference_peer_list_changed = (Tox_Event_Conference_Peer_List_Changed *) - realloc( - events->conference_peer_list_changed, - new_conference_peer_list_changed_capacity * sizeof(Tox_Event_Conference_Peer_List_Changed)); + tox_event_conference_peer_list_changed_construct(conference_peer_list_changed); + return conference_peer_list_changed; +} - if (new_conference_peer_list_changed == nullptr) { - return nullptr; - } +void tox_event_conference_peer_list_changed_free(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed, const Memory *mem) +{ + if (conference_peer_list_changed != nullptr) { + tox_event_conference_peer_list_changed_destruct(conference_peer_list_changed, mem); + } + mem_delete(mem, conference_peer_list_changed); +} - events->conference_peer_list_changed = new_conference_peer_list_changed; - events->conference_peer_list_changed_capacity = new_conference_peer_list_changed_capacity; +non_null() +static Tox_Event_Conference_Peer_List_Changed *tox_events_add_conference_peer_list_changed(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Conference_Peer_List_Changed *const conference_peer_list_changed = tox_event_conference_peer_list_changed_new(mem); + + if (conference_peer_list_changed == nullptr) { + return nullptr; } - Tox_Event_Conference_Peer_List_Changed *const conference_peer_list_changed = - &events->conference_peer_list_changed[events->conference_peer_list_changed_size]; - tox_event_conference_peer_list_changed_construct(conference_peer_list_changed); - ++events->conference_peer_list_changed_size; + Tox_Event event; + event.type = TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED; + event.data.conference_peer_list_changed = conference_peer_list_changed; + + tox_events_add(events, &event); return conference_peer_list_changed; } -void tox_events_clear_conference_peer_list_changed(Tox_Events *events) +const Tox_Event_Conference_Peer_List_Changed *tox_events_get_conference_peer_list_changed(const Tox_Events *events, uint32_t index) { - if (events == nullptr) { - return; - } + uint32_t conference_peer_list_changed_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (conference_peer_list_changed_index > index) { + return nullptr; + } - for (uint32_t i = 0; i < events->conference_peer_list_changed_size; ++i) { - tox_event_conference_peer_list_changed_destruct(&events->conference_peer_list_changed[i]); + if (events->events[i].type == TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED) { + const Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed = events->events[i].data.conference_peer_list_changed; + if (conference_peer_list_changed_index == index) { + return conference_peer_list_changed; + } + ++conference_peer_list_changed_index; + } } - free(events->conference_peer_list_changed); - events->conference_peer_list_changed = nullptr; - events->conference_peer_list_changed_size = 0; - events->conference_peer_list_changed_capacity = 0; + return nullptr; } uint32_t tox_events_get_conference_peer_list_changed_size(const Tox_Events *events) { - if (events == nullptr) { - return 0; + uint32_t conference_peer_list_changed_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED) { + ++conference_peer_list_changed_size; + } } - return events->conference_peer_list_changed_size; + return conference_peer_list_changed_size; } -const Tox_Event_Conference_Peer_List_Changed *tox_events_get_conference_peer_list_changed(const Tox_Events *events, - uint32_t index) +bool tox_event_conference_peer_list_changed_unpack( + Tox_Event_Conference_Peer_List_Changed **event, Bin_Unpack *bu, const Memory *mem) { - assert(index < events->conference_peer_list_changed_size); - assert(events->conference_peer_list_changed != nullptr); - return &events->conference_peer_list_changed[index]; + assert(event != nullptr); + *event = tox_event_conference_peer_list_changed_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_conference_peer_list_changed_unpack_into(*event, bu); } -bool tox_events_pack_conference_peer_list_changed(const Tox_Events *events, Bin_Pack *bp) +non_null() +static Tox_Event_Conference_Peer_List_Changed *tox_event_conference_peer_list_changed_alloc(void *user_data) { - const uint32_t size = tox_events_get_conference_peer_list_changed_size(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_peer_list_changed_pack(tox_events_get_conference_peer_list_changed(events, i), bp)) { - return false; - } + if (state->events == nullptr) { + return nullptr; } - return true; -} -bool tox_events_unpack_conference_peer_list_changed(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Conference_Peer_List_Changed *event = tox_events_add_conference_peer_list_changed(events); + Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed = tox_events_add_conference_peer_list_changed(state->events, state->mem); - if (event == nullptr) { - return false; + if (conference_peer_list_changed == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; } - return tox_event_conference_peer_list_changed_unpack(event, bu); + return conference_peer_list_changed; } @@ -173,20 +196,12 @@ bool tox_events_unpack_conference_peer_list_changed(Tox_Events *events, Bin_Unpa *****************************************************/ -void tox_events_handle_conference_peer_list_changed(Tox *tox, uint32_t conference_number, void *user_data) +void tox_events_handle_conference_peer_list_changed(Tox *tox, uint32_t conference_number, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed = tox_events_add_conference_peer_list_changed( - state->events); + Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed = tox_event_conference_peer_list_changed_alloc(user_data); if (conference_peer_list_changed == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/conference_peer_name.c b/toxcore/events/conference_peer_name.c index 0859b99c5d1..c47f258fd28 100644 --- a/toxcore/events/conference_peer_name.c +++ b/toxcore/events/conference_peer_name.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -29,19 +30,6 @@ struct Tox_Event_Conference_Peer_Name { uint32_t name_length; }; -non_null() -static void tox_event_conference_peer_name_construct(Tox_Event_Conference_Peer_Name *conference_peer_name) -{ - *conference_peer_name = (Tox_Event_Conference_Peer_Name) { - 0 - }; -} -non_null() -static void tox_event_conference_peer_name_destruct(Tox_Event_Conference_Peer_Name *conference_peer_name) -{ - free(conference_peer_name->name); -} - non_null() static void tox_event_conference_peer_name_set_conference_number(Tox_Event_Conference_Peer_Name *conference_peer_name, uint32_t conference_number) @@ -49,8 +37,7 @@ static void tox_event_conference_peer_name_set_conference_number(Tox_Event_Confe assert(conference_peer_name != nullptr); conference_peer_name->conference_number = conference_number; } -uint32_t tox_event_conference_peer_name_get_conference_number(const Tox_Event_Conference_Peer_Name - *conference_peer_name) +uint32_t tox_event_conference_peer_name_get_conference_number(const Tox_Event_Conference_Peer_Name *conference_peer_name) { assert(conference_peer_name != nullptr); return conference_peer_name->conference_number; @@ -104,7 +91,19 @@ const uint8_t *tox_event_conference_peer_name_get_name(const Tox_Event_Conferenc } non_null() -static bool tox_event_conference_peer_name_pack( +static void tox_event_conference_peer_name_construct(Tox_Event_Conference_Peer_Name *conference_peer_name) +{ + *conference_peer_name = (Tox_Event_Conference_Peer_Name) { + 0 + }; +} +non_null() +static void tox_event_conference_peer_name_destruct(Tox_Event_Conference_Peer_Name *conference_peer_name, const Memory *mem) +{ + free(conference_peer_name->name); +} + +bool tox_event_conference_peer_name_pack( const Tox_Event_Conference_Peer_Name *event, Bin_Pack *bp) { assert(event != nullptr); @@ -117,7 +116,7 @@ static bool tox_event_conference_peer_name_pack( } non_null() -static bool tox_event_conference_peer_name_unpack( +static bool tox_event_conference_peer_name_unpack_into( Tox_Event_Conference_Peer_Name *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -133,91 +132,120 @@ static bool tox_event_conference_peer_name_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Conference_Peer_Name *tox_events_add_conference_peer_name(Tox_Events *events) +const Tox_Event_Conference_Peer_Name *tox_event_get_conference_peer_name(const Tox_Event *event) { - if (events->conference_peer_name_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_peer_name_size == events->conference_peer_name_capacity) { - const uint32_t new_conference_peer_name_capacity = events->conference_peer_name_capacity * 2 + 1; - Tox_Event_Conference_Peer_Name *new_conference_peer_name = (Tox_Event_Conference_Peer_Name *)realloc( - events->conference_peer_name, new_conference_peer_name_capacity * sizeof(Tox_Event_Conference_Peer_Name)); + return event->type == TOX_EVENT_CONFERENCE_PEER_NAME ? event->data.conference_peer_name : nullptr; +} - if (new_conference_peer_name == nullptr) { - return nullptr; - } +Tox_Event_Conference_Peer_Name *tox_event_conference_peer_name_new(const Memory *mem) +{ + Tox_Event_Conference_Peer_Name *const conference_peer_name = + (Tox_Event_Conference_Peer_Name *)mem_alloc(mem, sizeof(Tox_Event_Conference_Peer_Name)); - events->conference_peer_name = new_conference_peer_name; - events->conference_peer_name_capacity = new_conference_peer_name_capacity; + if (conference_peer_name == nullptr) { + return nullptr; } - Tox_Event_Conference_Peer_Name *const conference_peer_name = - &events->conference_peer_name[events->conference_peer_name_size]; tox_event_conference_peer_name_construct(conference_peer_name); - ++events->conference_peer_name_size; return conference_peer_name; } -void tox_events_clear_conference_peer_name(Tox_Events *events) +void tox_event_conference_peer_name_free(Tox_Event_Conference_Peer_Name *conference_peer_name, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->conference_peer_name_size; ++i) { - tox_event_conference_peer_name_destruct(&events->conference_peer_name[i]); + if (conference_peer_name != nullptr) { + tox_event_conference_peer_name_destruct(conference_peer_name, mem); } - - free(events->conference_peer_name); - events->conference_peer_name = nullptr; - events->conference_peer_name_size = 0; - events->conference_peer_name_capacity = 0; + mem_delete(mem, conference_peer_name); } -uint32_t tox_events_get_conference_peer_name_size(const Tox_Events *events) +non_null() +static Tox_Event_Conference_Peer_Name *tox_events_add_conference_peer_name(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Conference_Peer_Name *const conference_peer_name = tox_event_conference_peer_name_new(mem); + + if (conference_peer_name == nullptr) { + return nullptr; } - return events->conference_peer_name_size; + Tox_Event event; + event.type = TOX_EVENT_CONFERENCE_PEER_NAME; + event.data.conference_peer_name = conference_peer_name; + + tox_events_add(events, &event); + return conference_peer_name; } const Tox_Event_Conference_Peer_Name *tox_events_get_conference_peer_name(const Tox_Events *events, uint32_t index) { - assert(index < events->conference_peer_name_size); - assert(events->conference_peer_name != nullptr); - return &events->conference_peer_name[index]; + uint32_t conference_peer_name_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (conference_peer_name_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_CONFERENCE_PEER_NAME) { + const Tox_Event_Conference_Peer_Name *conference_peer_name = events->events[i].data.conference_peer_name; + if (conference_peer_name_index == index) { + return conference_peer_name; + } + ++conference_peer_name_index; + } + } + + return nullptr; } -bool tox_events_pack_conference_peer_name(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_conference_peer_name_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_conference_peer_name_size(events); + uint32_t conference_peer_name_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_peer_name_pack(tox_events_get_conference_peer_name(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_CONFERENCE_PEER_NAME) { + ++conference_peer_name_size; } } - return true; + + return conference_peer_name_size; } -bool tox_events_unpack_conference_peer_name(Tox_Events *events, Bin_Unpack *bu) +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 = tox_events_add_conference_peer_name(events); + assert(event != nullptr); + *event = tox_event_conference_peer_name_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_conference_peer_name_unpack(event, bu); + return tox_event_conference_peer_name_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Conference_Peer_Name *tox_event_conference_peer_name_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Conference_Peer_Name *conference_peer_name = tox_events_add_conference_peer_name(state->events, state->mem); + + if (conference_peer_name == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return conference_peer_name; } @@ -228,20 +256,12 @@ bool tox_events_unpack_conference_peer_name(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_conference_peer_name(Tox *tox, uint32_t conference_number, uint32_t peer_number, - const uint8_t *name, size_t length, void *user_data) +void tox_events_handle_conference_peer_name(Tox *tox, uint32_t conference_number, uint32_t peer_number, const uint8_t *name, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Peer_Name *conference_peer_name = tox_events_add_conference_peer_name(state->events); + Tox_Event_Conference_Peer_Name *conference_peer_name = tox_event_conference_peer_name_alloc(user_data); if (conference_peer_name == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/conference_title.c b/toxcore/events/conference_title.c index 2043de132fe..76bc9057aaa 100644 --- a/toxcore/events/conference_title.c +++ b/toxcore/events/conference_title.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -29,19 +30,6 @@ struct Tox_Event_Conference_Title { uint32_t title_length; }; -non_null() -static void tox_event_conference_title_construct(Tox_Event_Conference_Title *conference_title) -{ - *conference_title = (Tox_Event_Conference_Title) { - 0 - }; -} -non_null() -static void tox_event_conference_title_destruct(Tox_Event_Conference_Title *conference_title) -{ - free(conference_title->title); -} - non_null() static void tox_event_conference_title_set_conference_number(Tox_Event_Conference_Title *conference_title, uint32_t conference_number) @@ -69,8 +57,8 @@ uint32_t tox_event_conference_title_get_peer_number(const Tox_Event_Conference_T } non_null() -static bool tox_event_conference_title_set_title(Tox_Event_Conference_Title *conference_title, const uint8_t *title, - uint32_t title_length) +static bool tox_event_conference_title_set_title(Tox_Event_Conference_Title *conference_title, + const uint8_t *title, uint32_t title_length) { assert(conference_title != nullptr); @@ -103,7 +91,19 @@ const uint8_t *tox_event_conference_title_get_title(const Tox_Event_Conference_T } non_null() -static bool tox_event_conference_title_pack( +static void tox_event_conference_title_construct(Tox_Event_Conference_Title *conference_title) +{ + *conference_title = (Tox_Event_Conference_Title) { + 0 + }; +} +non_null() +static void tox_event_conference_title_destruct(Tox_Event_Conference_Title *conference_title, const Memory *mem) +{ + free(conference_title->title); +} + +bool tox_event_conference_title_pack( const Tox_Event_Conference_Title *event, Bin_Pack *bp) { assert(event != nullptr); @@ -116,7 +116,7 @@ static bool tox_event_conference_title_pack( } non_null() -static bool tox_event_conference_title_unpack( +static bool tox_event_conference_title_unpack_into( Tox_Event_Conference_Title *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -132,90 +132,120 @@ static bool tox_event_conference_title_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Conference_Title *tox_events_add_conference_title(Tox_Events *events) +const Tox_Event_Conference_Title *tox_event_get_conference_title(const Tox_Event *event) { - if (events->conference_title_size == UINT32_MAX) { - return nullptr; - } - - if (events->conference_title_size == events->conference_title_capacity) { - const uint32_t new_conference_title_capacity = events->conference_title_capacity * 2 + 1; - Tox_Event_Conference_Title *new_conference_title = (Tox_Event_Conference_Title *)realloc( - events->conference_title, new_conference_title_capacity * sizeof(Tox_Event_Conference_Title)); + return event->type == TOX_EVENT_CONFERENCE_TITLE ? event->data.conference_title : nullptr; +} - if (new_conference_title == nullptr) { - return nullptr; - } +Tox_Event_Conference_Title *tox_event_conference_title_new(const Memory *mem) +{ + Tox_Event_Conference_Title *const conference_title = + (Tox_Event_Conference_Title *)mem_alloc(mem, sizeof(Tox_Event_Conference_Title)); - events->conference_title = new_conference_title; - events->conference_title_capacity = new_conference_title_capacity; + if (conference_title == nullptr) { + return nullptr; } - Tox_Event_Conference_Title *const conference_title = &events->conference_title[events->conference_title_size]; tox_event_conference_title_construct(conference_title); - ++events->conference_title_size; return conference_title; } -void tox_events_clear_conference_title(Tox_Events *events) +void tox_event_conference_title_free(Tox_Event_Conference_Title *conference_title, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->conference_title_size; ++i) { - tox_event_conference_title_destruct(&events->conference_title[i]); + if (conference_title != nullptr) { + tox_event_conference_title_destruct(conference_title, mem); } - - free(events->conference_title); - events->conference_title = nullptr; - events->conference_title_size = 0; - events->conference_title_capacity = 0; + mem_delete(mem, conference_title); } -uint32_t tox_events_get_conference_title_size(const Tox_Events *events) +non_null() +static Tox_Event_Conference_Title *tox_events_add_conference_title(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Conference_Title *const conference_title = tox_event_conference_title_new(mem); + + if (conference_title == nullptr) { + return nullptr; } - return events->conference_title_size; + Tox_Event event; + event.type = TOX_EVENT_CONFERENCE_TITLE; + event.data.conference_title = conference_title; + + tox_events_add(events, &event); + return conference_title; } const Tox_Event_Conference_Title *tox_events_get_conference_title(const Tox_Events *events, uint32_t index) { - assert(index < events->conference_title_size); - assert(events->conference_title != nullptr); - return &events->conference_title[index]; + uint32_t conference_title_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (conference_title_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_CONFERENCE_TITLE) { + const Tox_Event_Conference_Title *conference_title = events->events[i].data.conference_title; + if (conference_title_index == index) { + return conference_title; + } + ++conference_title_index; + } + } + + return nullptr; } -bool tox_events_pack_conference_title(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_conference_title_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_conference_title_size(events); + uint32_t conference_title_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_conference_title_pack(tox_events_get_conference_title(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_CONFERENCE_TITLE) { + ++conference_title_size; } } - return true; + + return conference_title_size; } -bool tox_events_unpack_conference_title(Tox_Events *events, Bin_Unpack *bu) +bool tox_event_conference_title_unpack( + Tox_Event_Conference_Title **event, Bin_Unpack *bu, const Memory *mem) { - Tox_Event_Conference_Title *event = tox_events_add_conference_title(events); + assert(event != nullptr); + *event = tox_event_conference_title_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_conference_title_unpack(event, bu); + return tox_event_conference_title_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Conference_Title *tox_event_conference_title_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Conference_Title *conference_title = tox_events_add_conference_title(state->events, state->mem); + + if (conference_title == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return conference_title; } @@ -226,20 +256,12 @@ bool tox_events_unpack_conference_title(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_conference_title(Tox *tox, uint32_t conference_number, uint32_t peer_number, - const uint8_t *title, size_t length, void *user_data) +void tox_events_handle_conference_title(Tox *tox, uint32_t conference_number, uint32_t peer_number, const uint8_t *title, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Conference_Title *conference_title = tox_events_add_conference_title(state->events); + Tox_Event_Conference_Title *conference_title = tox_event_conference_title_alloc(user_data); if (conference_title == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/events_alloc.c b/toxcore/events/events_alloc.c index 2fc5d363ab0..88d4b1a77ec 100644 --- a/toxcore/events/events_alloc.c +++ b/toxcore/events/events_alloc.c @@ -14,13 +14,14 @@ Tox_Events_State *tox_events_alloc(void *user_data) { Tox_Events_State *state = (Tox_Events_State *)user_data; assert(state != nullptr); + assert(state->mem != nullptr); if (state->events != nullptr) { // Already allocated. return state; } - Tox_Events *events = (Tox_Events *)calloc(1, sizeof(Tox_Events)); + Tox_Events *events = (Tox_Events *)mem_alloc(state->mem, sizeof(Tox_Events)); if (events == nullptr) { // It's still null => allocation failed. @@ -33,6 +34,7 @@ Tox_Events_State *tox_events_alloc(void *user_data) nullptr }; state->events = events; + state->events->mem = state->mem; return state; } @@ -43,26 +45,35 @@ void tox_events_free(Tox_Events *events) return; } - tox_events_clear_conference_connected(events); - tox_events_clear_conference_invite(events); - tox_events_clear_conference_message(events); - tox_events_clear_conference_peer_list_changed(events); - tox_events_clear_conference_peer_name(events); - tox_events_clear_conference_title(events); - tox_events_clear_file_chunk_request(events); - tox_events_clear_file_recv_chunk(events); - tox_events_clear_file_recv_control(events); - tox_events_clear_file_recv(events); - tox_events_clear_friend_connection_status(events); - tox_events_clear_friend_lossless_packet(events); - tox_events_clear_friend_lossy_packet(events); - tox_events_clear_friend_message(events); - tox_events_clear_friend_name(events); - tox_events_clear_friend_read_receipt(events); - tox_events_clear_friend_request(events); - tox_events_clear_friend_status(events); - tox_events_clear_friend_status_message(events); - tox_events_clear_friend_typing(events); - tox_events_clear_self_connection_status(events); - free(events); + for (uint32_t i = 0; i < events->events_size; ++i) { + tox_event_destruct(&events->events[i], events->mem); + } + + mem_delete(events->mem, events->events); + mem_delete(events->mem, events); +} + +bool tox_events_add(Tox_Events *events, const Tox_Event *event) +{ + if (events->events_size == UINT32_MAX) { + return false; + } + + if (events->events_size == events->events_capacity) { + const uint32_t new_events_capacity = events->events_capacity * 2 + 1; + Tox_Event *new_events = (Tox_Event *)mem_vrealloc( + events->mem, events->events, new_events_capacity, sizeof(Tox_Event)); + + if (new_events == nullptr) { + return false; + } + + events->events = new_events; + events->events_capacity = new_events_capacity; + } + + events->events[events->events_size] = *event; + ++events->events_size; + + return true; } diff --git a/toxcore/events/events_alloc.h b/toxcore/events/events_alloc.h index e59bfcea3a2..9bbddd4bb8e 100644 --- a/toxcore/events/events_alloc.h +++ b/toxcore/events/events_alloc.h @@ -8,100 +8,23 @@ #include "../attributes.h" #include "../bin_pack.h" #include "../bin_unpack.h" -#include "../tox_events.h" +#include "../tox_event.h" #ifdef __cplusplus extern "C" { #endif struct Tox_Events { - Tox_Event_Conference_Connected *conference_connected; - uint32_t conference_connected_size; - uint32_t conference_connected_capacity; + Tox_Event *events; + uint32_t events_size; + uint32_t events_capacity; - Tox_Event_Conference_Invite *conference_invite; - uint32_t conference_invite_size; - uint32_t conference_invite_capacity; - - Tox_Event_Conference_Message *conference_message; - uint32_t conference_message_size; - uint32_t conference_message_capacity; - - Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed; - uint32_t conference_peer_list_changed_size; - uint32_t conference_peer_list_changed_capacity; - - Tox_Event_Conference_Peer_Name *conference_peer_name; - uint32_t conference_peer_name_size; - uint32_t conference_peer_name_capacity; - - Tox_Event_Conference_Title *conference_title; - uint32_t conference_title_size; - uint32_t conference_title_capacity; - - Tox_Event_File_Chunk_Request *file_chunk_request; - uint32_t file_chunk_request_size; - uint32_t file_chunk_request_capacity; - - Tox_Event_File_Recv *file_recv; - uint32_t file_recv_size; - uint32_t file_recv_capacity; - - Tox_Event_File_Recv_Chunk *file_recv_chunk; - uint32_t file_recv_chunk_size; - uint32_t file_recv_chunk_capacity; - - Tox_Event_File_Recv_Control *file_recv_control; - uint32_t file_recv_control_size; - uint32_t file_recv_control_capacity; - - Tox_Event_Friend_Connection_Status *friend_connection_status; - uint32_t friend_connection_status_size; - uint32_t friend_connection_status_capacity; - - Tox_Event_Friend_Lossless_Packet *friend_lossless_packet; - uint32_t friend_lossless_packet_size; - uint32_t friend_lossless_packet_capacity; - - Tox_Event_Friend_Lossy_Packet *friend_lossy_packet; - uint32_t friend_lossy_packet_size; - uint32_t friend_lossy_packet_capacity; - - Tox_Event_Friend_Message *friend_message; - uint32_t friend_message_size; - uint32_t friend_message_capacity; - - Tox_Event_Friend_Name *friend_name; - uint32_t friend_name_size; - uint32_t friend_name_capacity; - - Tox_Event_Friend_Read_Receipt *friend_read_receipt; - uint32_t friend_read_receipt_size; - uint32_t friend_read_receipt_capacity; - - Tox_Event_Friend_Request *friend_request; - uint32_t friend_request_size; - uint32_t friend_request_capacity; - - Tox_Event_Friend_Status *friend_status; - uint32_t friend_status_size; - uint32_t friend_status_capacity; - - Tox_Event_Friend_Status_Message *friend_status_message; - uint32_t friend_status_message_size; - uint32_t friend_status_message_capacity; - - Tox_Event_Friend_Typing *friend_typing; - uint32_t friend_typing_size; - uint32_t friend_typing_capacity; - - Tox_Event_Self_Connection_Status *self_connection_status; - uint32_t self_connection_status_size; - uint32_t self_connection_status_capacity; + const Memory *mem; }; typedef struct Tox_Events_State { Tox_Err_Events_Iterate error; + const Memory *mem; Tox_Events *events; } Tox_Events_State; @@ -127,88 +50,18 @@ tox_friend_status_message_cb tox_events_handle_friend_status_message; tox_friend_typing_cb tox_events_handle_friend_typing; tox_self_connection_status_cb tox_events_handle_self_connection_status; -// non_null() -typedef void tox_events_clear_cb(Tox_Events *events); - -tox_events_clear_cb tox_events_clear_conference_connected; -tox_events_clear_cb tox_events_clear_conference_invite; -tox_events_clear_cb tox_events_clear_conference_message; -tox_events_clear_cb tox_events_clear_conference_peer_list_changed; -tox_events_clear_cb tox_events_clear_conference_peer_name; -tox_events_clear_cb tox_events_clear_conference_title; -tox_events_clear_cb tox_events_clear_file_chunk_request; -tox_events_clear_cb tox_events_clear_file_recv_chunk; -tox_events_clear_cb tox_events_clear_file_recv_control; -tox_events_clear_cb tox_events_clear_file_recv; -tox_events_clear_cb tox_events_clear_friend_connection_status; -tox_events_clear_cb tox_events_clear_friend_lossless_packet; -tox_events_clear_cb tox_events_clear_friend_lossy_packet; -tox_events_clear_cb tox_events_clear_friend_message; -tox_events_clear_cb tox_events_clear_friend_name; -tox_events_clear_cb tox_events_clear_friend_read_receipt; -tox_events_clear_cb tox_events_clear_friend_request; -tox_events_clear_cb tox_events_clear_friend_status_message; -tox_events_clear_cb tox_events_clear_friend_status; -tox_events_clear_cb tox_events_clear_friend_typing; -tox_events_clear_cb tox_events_clear_self_connection_status; - -// non_null() -typedef bool tox_events_pack_cb(const Tox_Events *events, Bin_Pack *bp); +non_null(2) nullable(1) +bool tox_events_pack(const Tox_Events *events, Bin_Pack *bp); -tox_events_pack_cb tox_events_pack_conference_connected; -tox_events_pack_cb tox_events_pack_conference_invite; -tox_events_pack_cb tox_events_pack_conference_message; -tox_events_pack_cb tox_events_pack_conference_peer_list_changed; -tox_events_pack_cb tox_events_pack_conference_peer_name; -tox_events_pack_cb tox_events_pack_conference_title; -tox_events_pack_cb tox_events_pack_file_chunk_request; -tox_events_pack_cb tox_events_pack_file_recv_chunk; -tox_events_pack_cb tox_events_pack_file_recv_control; -tox_events_pack_cb tox_events_pack_file_recv; -tox_events_pack_cb tox_events_pack_friend_connection_status; -tox_events_pack_cb tox_events_pack_friend_lossless_packet; -tox_events_pack_cb tox_events_pack_friend_lossy_packet; -tox_events_pack_cb tox_events_pack_friend_message; -tox_events_pack_cb tox_events_pack_friend_name; -tox_events_pack_cb tox_events_pack_friend_read_receipt; -tox_events_pack_cb tox_events_pack_friend_request; -tox_events_pack_cb tox_events_pack_friend_status_message; -tox_events_pack_cb tox_events_pack_friend_status; -tox_events_pack_cb tox_events_pack_friend_typing; -tox_events_pack_cb tox_events_pack_self_connection_status; - -tox_events_pack_cb tox_events_pack; - -// non_null() -typedef bool tox_events_unpack_cb(Tox_Events *events, Bin_Unpack *bu); - -tox_events_unpack_cb tox_events_unpack_conference_connected; -tox_events_unpack_cb tox_events_unpack_conference_invite; -tox_events_unpack_cb tox_events_unpack_conference_message; -tox_events_unpack_cb tox_events_unpack_conference_peer_list_changed; -tox_events_unpack_cb tox_events_unpack_conference_peer_name; -tox_events_unpack_cb tox_events_unpack_conference_title; -tox_events_unpack_cb tox_events_unpack_file_chunk_request; -tox_events_unpack_cb tox_events_unpack_file_recv_chunk; -tox_events_unpack_cb tox_events_unpack_file_recv_control; -tox_events_unpack_cb tox_events_unpack_file_recv; -tox_events_unpack_cb tox_events_unpack_friend_connection_status; -tox_events_unpack_cb tox_events_unpack_friend_lossless_packet; -tox_events_unpack_cb tox_events_unpack_friend_lossy_packet; -tox_events_unpack_cb tox_events_unpack_friend_message; -tox_events_unpack_cb tox_events_unpack_friend_name; -tox_events_unpack_cb tox_events_unpack_friend_read_receipt; -tox_events_unpack_cb tox_events_unpack_friend_request; -tox_events_unpack_cb tox_events_unpack_friend_status_message; -tox_events_unpack_cb tox_events_unpack_friend_status; -tox_events_unpack_cb tox_events_unpack_friend_typing; -tox_events_unpack_cb tox_events_unpack_self_connection_status; - -tox_events_unpack_cb tox_events_unpack; +non_null() +bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu, const Memory *mem); non_null() Tox_Events_State *tox_events_alloc(void *user_data); +non_null() +bool tox_events_add(Tox_Events *events, const Tox_Event *event); + #ifdef __cplusplus } #endif diff --git a/toxcore/events/file_chunk_request.c b/toxcore/events/file_chunk_request.c index 4f4deafc0d1..55bb6d0a869 100644 --- a/toxcore/events/file_chunk_request.c +++ b/toxcore/events/file_chunk_request.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -28,19 +29,6 @@ struct Tox_Event_File_Chunk_Request { uint16_t length; }; -non_null() -static void tox_event_file_chunk_request_construct(Tox_Event_File_Chunk_Request *file_chunk_request) -{ - *file_chunk_request = (Tox_Event_File_Chunk_Request) { - 0 - }; -} -non_null() -static void tox_event_file_chunk_request_destruct(Tox_Event_File_Chunk_Request *file_chunk_request) -{ - return; -} - non_null() static void tox_event_file_chunk_request_set_friend_number(Tox_Event_File_Chunk_Request *file_chunk_request, uint32_t friend_number) @@ -81,7 +69,8 @@ uint64_t tox_event_file_chunk_request_get_position(const Tox_Event_File_Chunk_Re } non_null() -static void tox_event_file_chunk_request_set_length(Tox_Event_File_Chunk_Request *file_chunk_request, uint16_t length) +static void tox_event_file_chunk_request_set_length(Tox_Event_File_Chunk_Request *file_chunk_request, + uint16_t length) { assert(file_chunk_request != nullptr); file_chunk_request->length = length; @@ -93,7 +82,19 @@ uint16_t tox_event_file_chunk_request_get_length(const Tox_Event_File_Chunk_Requ } non_null() -static bool tox_event_file_chunk_request_pack( +static void tox_event_file_chunk_request_construct(Tox_Event_File_Chunk_Request *file_chunk_request) +{ + *file_chunk_request = (Tox_Event_File_Chunk_Request) { + 0 + }; +} +non_null() +static void tox_event_file_chunk_request_destruct(Tox_Event_File_Chunk_Request *file_chunk_request, const Memory *mem) +{ + return; +} + +bool tox_event_file_chunk_request_pack( const Tox_Event_File_Chunk_Request *event, Bin_Pack *bp) { assert(event != nullptr); @@ -107,7 +108,7 @@ static bool tox_event_file_chunk_request_pack( } non_null() -static bool tox_event_file_chunk_request_unpack( +static bool tox_event_file_chunk_request_unpack_into( Tox_Event_File_Chunk_Request *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -124,90 +125,120 @@ static bool tox_event_file_chunk_request_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_File_Chunk_Request *tox_events_add_file_chunk_request(Tox_Events *events) +const Tox_Event_File_Chunk_Request *tox_event_get_file_chunk_request(const Tox_Event *event) { - if (events->file_chunk_request_size == UINT32_MAX) { - return nullptr; - } - - if (events->file_chunk_request_size == events->file_chunk_request_capacity) { - const uint32_t new_file_chunk_request_capacity = events->file_chunk_request_capacity * 2 + 1; - Tox_Event_File_Chunk_Request *new_file_chunk_request = (Tox_Event_File_Chunk_Request *)realloc( - events->file_chunk_request, new_file_chunk_request_capacity * sizeof(Tox_Event_File_Chunk_Request)); + return event->type == TOX_EVENT_FILE_CHUNK_REQUEST ? event->data.file_chunk_request : nullptr; +} - if (new_file_chunk_request == nullptr) { - return nullptr; - } +Tox_Event_File_Chunk_Request *tox_event_file_chunk_request_new(const Memory *mem) +{ + Tox_Event_File_Chunk_Request *const file_chunk_request = + (Tox_Event_File_Chunk_Request *)mem_alloc(mem, sizeof(Tox_Event_File_Chunk_Request)); - events->file_chunk_request = new_file_chunk_request; - events->file_chunk_request_capacity = new_file_chunk_request_capacity; + if (file_chunk_request == nullptr) { + return nullptr; } - Tox_Event_File_Chunk_Request *const file_chunk_request = &events->file_chunk_request[events->file_chunk_request_size]; tox_event_file_chunk_request_construct(file_chunk_request); - ++events->file_chunk_request_size; return file_chunk_request; } -void tox_events_clear_file_chunk_request(Tox_Events *events) +void tox_event_file_chunk_request_free(Tox_Event_File_Chunk_Request *file_chunk_request, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->file_chunk_request_size; ++i) { - tox_event_file_chunk_request_destruct(&events->file_chunk_request[i]); + if (file_chunk_request != nullptr) { + tox_event_file_chunk_request_destruct(file_chunk_request, mem); } - - free(events->file_chunk_request); - events->file_chunk_request = nullptr; - events->file_chunk_request_size = 0; - events->file_chunk_request_capacity = 0; + mem_delete(mem, file_chunk_request); } -uint32_t tox_events_get_file_chunk_request_size(const Tox_Events *events) +non_null() +static Tox_Event_File_Chunk_Request *tox_events_add_file_chunk_request(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_File_Chunk_Request *const file_chunk_request = tox_event_file_chunk_request_new(mem); + + if (file_chunk_request == nullptr) { + return nullptr; } - return events->file_chunk_request_size; + Tox_Event event; + event.type = TOX_EVENT_FILE_CHUNK_REQUEST; + event.data.file_chunk_request = file_chunk_request; + + tox_events_add(events, &event); + return file_chunk_request; } const Tox_Event_File_Chunk_Request *tox_events_get_file_chunk_request(const Tox_Events *events, uint32_t index) { - assert(index < events->file_chunk_request_size); - assert(events->file_chunk_request != nullptr); - return &events->file_chunk_request[index]; + uint32_t file_chunk_request_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (file_chunk_request_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_FILE_CHUNK_REQUEST) { + const Tox_Event_File_Chunk_Request *file_chunk_request = events->events[i].data.file_chunk_request; + if (file_chunk_request_index == index) { + return file_chunk_request; + } + ++file_chunk_request_index; + } + } + + return nullptr; } -bool tox_events_pack_file_chunk_request(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_file_chunk_request_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_file_chunk_request_size(events); + uint32_t file_chunk_request_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_file_chunk_request_pack(tox_events_get_file_chunk_request(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_FILE_CHUNK_REQUEST) { + ++file_chunk_request_size; } } - return true; + + return file_chunk_request_size; } -bool tox_events_unpack_file_chunk_request(Tox_Events *events, Bin_Unpack *bu) +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 = tox_events_add_file_chunk_request(events); + assert(event != nullptr); + *event = tox_event_file_chunk_request_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_file_chunk_request_unpack(event, bu); + return tox_event_file_chunk_request_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_File_Chunk_Request *tox_event_file_chunk_request_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_File_Chunk_Request *file_chunk_request = tox_events_add_file_chunk_request(state->events, state->mem); + + if (file_chunk_request == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return file_chunk_request; } @@ -218,20 +249,12 @@ bool tox_events_unpack_file_chunk_request(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, - size_t length, void *user_data) +void tox_events_handle_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_File_Chunk_Request *file_chunk_request = tox_events_add_file_chunk_request(state->events); + Tox_Event_File_Chunk_Request *file_chunk_request = tox_event_file_chunk_request_alloc(user_data); if (file_chunk_request == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/file_recv.c b/toxcore/events/file_recv.c index 0aefb23d145..21e076fda9d 100644 --- a/toxcore/events/file_recv.c +++ b/toxcore/events/file_recv.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -31,19 +32,6 @@ struct Tox_Event_File_Recv { uint32_t filename_length; }; -non_null() -static void tox_event_file_recv_construct(Tox_Event_File_Recv *file_recv) -{ - *file_recv = (Tox_Event_File_Recv) { - 0 - }; -} -non_null() -static void tox_event_file_recv_destruct(Tox_Event_File_Recv *file_recv) -{ - free(file_recv->filename); -} - non_null() static void tox_event_file_recv_set_friend_number(Tox_Event_File_Recv *file_recv, uint32_t friend_number) @@ -97,8 +85,8 @@ uint64_t tox_event_file_recv_get_file_size(const Tox_Event_File_Recv *file_recv) } non_null() -static bool tox_event_file_recv_set_filename(Tox_Event_File_Recv *file_recv, const uint8_t *filename, - uint32_t filename_length) +static bool tox_event_file_recv_set_filename(Tox_Event_File_Recv *file_recv, + const uint8_t *filename, uint32_t filename_length) { assert(file_recv != nullptr); @@ -131,7 +119,19 @@ const uint8_t *tox_event_file_recv_get_filename(const Tox_Event_File_Recv *file_ } non_null() -static bool tox_event_file_recv_pack( +static void tox_event_file_recv_construct(Tox_Event_File_Recv *file_recv) +{ + *file_recv = (Tox_Event_File_Recv) { + 0 + }; +} +non_null() +static void tox_event_file_recv_destruct(Tox_Event_File_Recv *file_recv, const Memory *mem) +{ + free(file_recv->filename); +} + +bool tox_event_file_recv_pack( const Tox_Event_File_Recv *event, Bin_Pack *bp) { assert(event != nullptr); @@ -146,7 +146,7 @@ static bool tox_event_file_recv_pack( } non_null() -static bool tox_event_file_recv_unpack( +static bool tox_event_file_recv_unpack_into( Tox_Event_File_Recv *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -164,90 +164,120 @@ static bool tox_event_file_recv_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_File_Recv *tox_events_add_file_recv(Tox_Events *events) +const Tox_Event_File_Recv *tox_event_get_file_recv(const Tox_Event *event) { - if (events->file_recv_size == UINT32_MAX) { - return nullptr; - } - - if (events->file_recv_size == events->file_recv_capacity) { - const uint32_t new_file_recv_capacity = events->file_recv_capacity * 2 + 1; - Tox_Event_File_Recv *new_file_recv = (Tox_Event_File_Recv *)realloc( - events->file_recv, new_file_recv_capacity * sizeof(Tox_Event_File_Recv)); + return event->type == TOX_EVENT_FILE_RECV ? event->data.file_recv : nullptr; +} - if (new_file_recv == nullptr) { - return nullptr; - } +Tox_Event_File_Recv *tox_event_file_recv_new(const Memory *mem) +{ + Tox_Event_File_Recv *const file_recv = + (Tox_Event_File_Recv *)mem_alloc(mem, sizeof(Tox_Event_File_Recv)); - events->file_recv = new_file_recv; - events->file_recv_capacity = new_file_recv_capacity; + if (file_recv == nullptr) { + return nullptr; } - Tox_Event_File_Recv *const file_recv = &events->file_recv[events->file_recv_size]; tox_event_file_recv_construct(file_recv); - ++events->file_recv_size; return file_recv; } -void tox_events_clear_file_recv(Tox_Events *events) +void tox_event_file_recv_free(Tox_Event_File_Recv *file_recv, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->file_recv_size; ++i) { - tox_event_file_recv_destruct(&events->file_recv[i]); + if (file_recv != nullptr) { + tox_event_file_recv_destruct(file_recv, mem); } - - free(events->file_recv); - events->file_recv = nullptr; - events->file_recv_size = 0; - events->file_recv_capacity = 0; + mem_delete(mem, file_recv); } -uint32_t tox_events_get_file_recv_size(const Tox_Events *events) +non_null() +static Tox_Event_File_Recv *tox_events_add_file_recv(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_File_Recv *const file_recv = tox_event_file_recv_new(mem); + + if (file_recv == nullptr) { + return nullptr; } - return events->file_recv_size; + Tox_Event event; + event.type = TOX_EVENT_FILE_RECV; + event.data.file_recv = file_recv; + + tox_events_add(events, &event); + return file_recv; } const Tox_Event_File_Recv *tox_events_get_file_recv(const Tox_Events *events, uint32_t index) { - assert(index < events->file_recv_size); - assert(events->file_recv != nullptr); - return &events->file_recv[index]; + uint32_t file_recv_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (file_recv_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_FILE_RECV) { + const Tox_Event_File_Recv *file_recv = events->events[i].data.file_recv; + if (file_recv_index == index) { + return file_recv; + } + ++file_recv_index; + } + } + + return nullptr; } -bool tox_events_pack_file_recv(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_file_recv_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_file_recv_size(events); + uint32_t file_recv_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_file_recv_pack(tox_events_get_file_recv(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_FILE_RECV) { + ++file_recv_size; } } - return true; + + return file_recv_size; } -bool tox_events_unpack_file_recv(Tox_Events *events, Bin_Unpack *bu) +bool tox_event_file_recv_unpack( + Tox_Event_File_Recv **event, Bin_Unpack *bu, const Memory *mem) { - Tox_Event_File_Recv *event = tox_events_add_file_recv(events); + assert(event != nullptr); + *event = tox_event_file_recv_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_file_recv_unpack(event, bu); + return tox_event_file_recv_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_File_Recv *tox_event_file_recv_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_File_Recv *file_recv = tox_events_add_file_recv(state->events, state->mem); + + if (file_recv == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return file_recv; } @@ -258,20 +288,12 @@ bool tox_events_unpack_file_recv(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_file_recv(Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, - uint64_t file_size, const uint8_t *filename, size_t filename_length, void *user_data) +void tox_events_handle_file_recv(Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, uint64_t file_size, const uint8_t *filename, size_t filename_length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_File_Recv *file_recv = tox_events_add_file_recv(state->events); + Tox_Event_File_Recv *file_recv = tox_event_file_recv_alloc(user_data); if (file_recv == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/file_recv_chunk.c b/toxcore/events/file_recv_chunk.c index 98c667a13a9..291f4bf757c 100644 --- a/toxcore/events/file_recv_chunk.c +++ b/toxcore/events/file_recv_chunk.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -30,19 +31,6 @@ struct Tox_Event_File_Recv_Chunk { uint32_t data_length; }; -non_null() -static void tox_event_file_recv_chunk_construct(Tox_Event_File_Recv_Chunk *file_recv_chunk) -{ - *file_recv_chunk = (Tox_Event_File_Recv_Chunk) { - 0 - }; -} -non_null() -static void tox_event_file_recv_chunk_destruct(Tox_Event_File_Recv_Chunk *file_recv_chunk) -{ - free(file_recv_chunk->data); -} - non_null() static void tox_event_file_recv_chunk_set_friend_number(Tox_Event_File_Recv_Chunk *file_recv_chunk, uint32_t friend_number) @@ -83,8 +71,8 @@ uint64_t tox_event_file_recv_chunk_get_position(const Tox_Event_File_Recv_Chunk } non_null() -static bool tox_event_file_recv_chunk_set_data(Tox_Event_File_Recv_Chunk *file_recv_chunk, const uint8_t *data, - uint32_t data_length) +static bool tox_event_file_recv_chunk_set_data(Tox_Event_File_Recv_Chunk *file_recv_chunk, + const uint8_t *data, uint32_t data_length) { assert(file_recv_chunk != nullptr); @@ -105,7 +93,7 @@ static bool tox_event_file_recv_chunk_set_data(Tox_Event_File_Recv_Chunk *file_r file_recv_chunk->data_length = data_length; return true; } -uint32_t tox_event_file_recv_chunk_get_length(const Tox_Event_File_Recv_Chunk *file_recv_chunk) +uint32_t tox_event_file_recv_chunk_get_data_length(const Tox_Event_File_Recv_Chunk *file_recv_chunk) { assert(file_recv_chunk != nullptr); return file_recv_chunk->data_length; @@ -117,7 +105,19 @@ const uint8_t *tox_event_file_recv_chunk_get_data(const Tox_Event_File_Recv_Chun } non_null() -static bool tox_event_file_recv_chunk_pack( +static void tox_event_file_recv_chunk_construct(Tox_Event_File_Recv_Chunk *file_recv_chunk) +{ + *file_recv_chunk = (Tox_Event_File_Recv_Chunk) { + 0 + }; +} +non_null() +static void tox_event_file_recv_chunk_destruct(Tox_Event_File_Recv_Chunk *file_recv_chunk, const Memory *mem) +{ + free(file_recv_chunk->data); +} + +bool tox_event_file_recv_chunk_pack( const Tox_Event_File_Recv_Chunk *event, Bin_Pack *bp) { assert(event != nullptr); @@ -131,7 +131,7 @@ static bool tox_event_file_recv_chunk_pack( } non_null() -static bool tox_event_file_recv_chunk_unpack( +static bool tox_event_file_recv_chunk_unpack_into( Tox_Event_File_Recv_Chunk *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -148,90 +148,120 @@ static bool tox_event_file_recv_chunk_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_File_Recv_Chunk *tox_events_add_file_recv_chunk(Tox_Events *events) +const Tox_Event_File_Recv_Chunk *tox_event_get_file_recv_chunk(const Tox_Event *event) { - if (events->file_recv_chunk_size == UINT32_MAX) { - return nullptr; - } - - if (events->file_recv_chunk_size == events->file_recv_chunk_capacity) { - const uint32_t new_file_recv_chunk_capacity = events->file_recv_chunk_capacity * 2 + 1; - Tox_Event_File_Recv_Chunk *new_file_recv_chunk = (Tox_Event_File_Recv_Chunk *)realloc( - events->file_recv_chunk, new_file_recv_chunk_capacity * sizeof(Tox_Event_File_Recv_Chunk)); + return event->type == TOX_EVENT_FILE_RECV_CHUNK ? event->data.file_recv_chunk : nullptr; +} - if (new_file_recv_chunk == nullptr) { - return nullptr; - } +Tox_Event_File_Recv_Chunk *tox_event_file_recv_chunk_new(const Memory *mem) +{ + Tox_Event_File_Recv_Chunk *const file_recv_chunk = + (Tox_Event_File_Recv_Chunk *)mem_alloc(mem, sizeof(Tox_Event_File_Recv_Chunk)); - events->file_recv_chunk = new_file_recv_chunk; - events->file_recv_chunk_capacity = new_file_recv_chunk_capacity; + if (file_recv_chunk == nullptr) { + return nullptr; } - Tox_Event_File_Recv_Chunk *const file_recv_chunk = &events->file_recv_chunk[events->file_recv_chunk_size]; tox_event_file_recv_chunk_construct(file_recv_chunk); - ++events->file_recv_chunk_size; return file_recv_chunk; } -void tox_events_clear_file_recv_chunk(Tox_Events *events) +void tox_event_file_recv_chunk_free(Tox_Event_File_Recv_Chunk *file_recv_chunk, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->file_recv_chunk_size; ++i) { - tox_event_file_recv_chunk_destruct(&events->file_recv_chunk[i]); + if (file_recv_chunk != nullptr) { + tox_event_file_recv_chunk_destruct(file_recv_chunk, mem); } - - free(events->file_recv_chunk); - events->file_recv_chunk = nullptr; - events->file_recv_chunk_size = 0; - events->file_recv_chunk_capacity = 0; + mem_delete(mem, file_recv_chunk); } -uint32_t tox_events_get_file_recv_chunk_size(const Tox_Events *events) +non_null() +static Tox_Event_File_Recv_Chunk *tox_events_add_file_recv_chunk(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_File_Recv_Chunk *const file_recv_chunk = tox_event_file_recv_chunk_new(mem); + + if (file_recv_chunk == nullptr) { + return nullptr; } - return events->file_recv_chunk_size; + Tox_Event event; + event.type = TOX_EVENT_FILE_RECV_CHUNK; + event.data.file_recv_chunk = file_recv_chunk; + + tox_events_add(events, &event); + return file_recv_chunk; } const Tox_Event_File_Recv_Chunk *tox_events_get_file_recv_chunk(const Tox_Events *events, uint32_t index) { - assert(index < events->file_recv_chunk_size); - assert(events->file_recv_chunk != nullptr); - return &events->file_recv_chunk[index]; + uint32_t file_recv_chunk_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (file_recv_chunk_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_FILE_RECV_CHUNK) { + const Tox_Event_File_Recv_Chunk *file_recv_chunk = events->events[i].data.file_recv_chunk; + if (file_recv_chunk_index == index) { + return file_recv_chunk; + } + ++file_recv_chunk_index; + } + } + + return nullptr; } -bool tox_events_pack_file_recv_chunk(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_file_recv_chunk_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_file_recv_chunk_size(events); + uint32_t file_recv_chunk_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_file_recv_chunk_pack(tox_events_get_file_recv_chunk(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_FILE_RECV_CHUNK) { + ++file_recv_chunk_size; } } - return true; + + return file_recv_chunk_size; } -bool tox_events_unpack_file_recv_chunk(Tox_Events *events, Bin_Unpack *bu) +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 = tox_events_add_file_recv_chunk(events); + assert(event != nullptr); + *event = tox_event_file_recv_chunk_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_file_recv_chunk_unpack(event, bu); + return tox_event_file_recv_chunk_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_File_Recv_Chunk *tox_event_file_recv_chunk_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_File_Recv_Chunk *file_recv_chunk = tox_events_add_file_recv_chunk(state->events, state->mem); + + if (file_recv_chunk == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return file_recv_chunk; } @@ -242,20 +272,12 @@ bool tox_events_unpack_file_recv_chunk(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_file_recv_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, - const uint8_t *data, size_t length, void *user_data) +void tox_events_handle_file_recv_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, const uint8_t *data, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_File_Recv_Chunk *file_recv_chunk = tox_events_add_file_recv_chunk(state->events); + Tox_Event_File_Recv_Chunk *file_recv_chunk = tox_event_file_recv_chunk_alloc(user_data); if (file_recv_chunk == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/file_recv_control.c b/toxcore/events/file_recv_control.c index 21ef33f5ea5..ee5bec1d275 100644 --- a/toxcore/events/file_recv_control.c +++ b/toxcore/events/file_recv_control.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -28,19 +28,6 @@ struct Tox_Event_File_Recv_Control { Tox_File_Control control; }; -non_null() -static void tox_event_file_recv_control_construct(Tox_Event_File_Recv_Control *file_recv_control) -{ - *file_recv_control = (Tox_Event_File_Recv_Control) { - 0 - }; -} -non_null() -static void tox_event_file_recv_control_destruct(Tox_Event_File_Recv_Control *file_recv_control) -{ - return; -} - non_null() static void tox_event_file_recv_control_set_friend_number(Tox_Event_File_Recv_Control *file_recv_control, uint32_t friend_number) @@ -81,7 +68,19 @@ Tox_File_Control tox_event_file_recv_control_get_control(const Tox_Event_File_Re } non_null() -static bool tox_event_file_recv_control_pack( +static void tox_event_file_recv_control_construct(Tox_Event_File_Recv_Control *file_recv_control) +{ + *file_recv_control = (Tox_Event_File_Recv_Control) { + 0 + }; +} +non_null() +static void tox_event_file_recv_control_destruct(Tox_Event_File_Recv_Control *file_recv_control, const Memory *mem) +{ + return; +} + +bool tox_event_file_recv_control_pack( const Tox_Event_File_Recv_Control *event, Bin_Pack *bp) { assert(event != nullptr); @@ -94,7 +93,7 @@ static bool tox_event_file_recv_control_pack( } non_null() -static bool tox_event_file_recv_control_unpack( +static bool tox_event_file_recv_control_unpack_into( Tox_Event_File_Recv_Control *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -110,90 +109,120 @@ static bool tox_event_file_recv_control_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_File_Recv_Control *tox_events_add_file_recv_control(Tox_Events *events) +const Tox_Event_File_Recv_Control *tox_event_get_file_recv_control(const Tox_Event *event) { - if (events->file_recv_control_size == UINT32_MAX) { - return nullptr; - } - - if (events->file_recv_control_size == events->file_recv_control_capacity) { - const uint32_t new_file_recv_control_capacity = events->file_recv_control_capacity * 2 + 1; - Tox_Event_File_Recv_Control *new_file_recv_control = (Tox_Event_File_Recv_Control *)realloc( - events->file_recv_control, new_file_recv_control_capacity * sizeof(Tox_Event_File_Recv_Control)); + return event->type == TOX_EVENT_FILE_RECV_CONTROL ? event->data.file_recv_control : nullptr; +} - if (new_file_recv_control == nullptr) { - return nullptr; - } +Tox_Event_File_Recv_Control *tox_event_file_recv_control_new(const Memory *mem) +{ + Tox_Event_File_Recv_Control *const file_recv_control = + (Tox_Event_File_Recv_Control *)mem_alloc(mem, sizeof(Tox_Event_File_Recv_Control)); - events->file_recv_control = new_file_recv_control; - events->file_recv_control_capacity = new_file_recv_control_capacity; + if (file_recv_control == nullptr) { + return nullptr; } - Tox_Event_File_Recv_Control *const file_recv_control = &events->file_recv_control[events->file_recv_control_size]; tox_event_file_recv_control_construct(file_recv_control); - ++events->file_recv_control_size; return file_recv_control; } -void tox_events_clear_file_recv_control(Tox_Events *events) +void tox_event_file_recv_control_free(Tox_Event_File_Recv_Control *file_recv_control, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->file_recv_control_size; ++i) { - tox_event_file_recv_control_destruct(&events->file_recv_control[i]); + if (file_recv_control != nullptr) { + tox_event_file_recv_control_destruct(file_recv_control, mem); } - - free(events->file_recv_control); - events->file_recv_control = nullptr; - events->file_recv_control_size = 0; - events->file_recv_control_capacity = 0; + mem_delete(mem, file_recv_control); } -uint32_t tox_events_get_file_recv_control_size(const Tox_Events *events) +non_null() +static Tox_Event_File_Recv_Control *tox_events_add_file_recv_control(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_File_Recv_Control *const file_recv_control = tox_event_file_recv_control_new(mem); + + if (file_recv_control == nullptr) { + return nullptr; } - return events->file_recv_control_size; + Tox_Event event; + event.type = TOX_EVENT_FILE_RECV_CONTROL; + event.data.file_recv_control = file_recv_control; + + tox_events_add(events, &event); + return file_recv_control; } const Tox_Event_File_Recv_Control *tox_events_get_file_recv_control(const Tox_Events *events, uint32_t index) { - assert(index < events->file_recv_control_size); - assert(events->file_recv_control != nullptr); - return &events->file_recv_control[index]; + uint32_t file_recv_control_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (file_recv_control_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_FILE_RECV_CONTROL) { + const Tox_Event_File_Recv_Control *file_recv_control = events->events[i].data.file_recv_control; + if (file_recv_control_index == index) { + return file_recv_control; + } + ++file_recv_control_index; + } + } + + return nullptr; } -bool tox_events_pack_file_recv_control(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_file_recv_control_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_file_recv_control_size(events); + uint32_t file_recv_control_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_file_recv_control_pack(tox_events_get_file_recv_control(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_FILE_RECV_CONTROL) { + ++file_recv_control_size; } } - return true; + + return file_recv_control_size; } -bool tox_events_unpack_file_recv_control(Tox_Events *events, Bin_Unpack *bu) +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 = tox_events_add_file_recv_control(events); + assert(event != nullptr); + *event = tox_event_file_recv_control_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_file_recv_control_unpack(event, bu); + return tox_event_file_recv_control_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_File_Recv_Control *tox_event_file_recv_control_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_File_Recv_Control *file_recv_control = tox_events_add_file_recv_control(state->events, state->mem); + + if (file_recv_control == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return file_recv_control; } @@ -204,20 +233,12 @@ bool tox_events_unpack_file_recv_control(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_file_recv_control(Tox *tox, uint32_t friend_number, uint32_t file_number, - Tox_File_Control control, void *user_data) +void tox_events_handle_file_recv_control(Tox *tox, uint32_t friend_number, uint32_t file_number, Tox_File_Control control, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_File_Recv_Control *file_recv_control = tox_events_add_file_recv_control(state->events); + Tox_Event_File_Recv_Control *file_recv_control = tox_event_file_recv_control_alloc(user_data); if (file_recv_control == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_connection_status.c b/toxcore/events/friend_connection_status.c index 4341f72d054..0701ccd091d 100644 --- a/toxcore/events/friend_connection_status.c +++ b/toxcore/events/friend_connection_status.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -28,48 +28,45 @@ struct Tox_Event_Friend_Connection_Status { }; non_null() -static void tox_event_friend_connection_status_construct(Tox_Event_Friend_Connection_Status *friend_connection_status) -{ - *friend_connection_status = (Tox_Event_Friend_Connection_Status) { - 0 - }; -} -non_null() -static void tox_event_friend_connection_status_destruct(Tox_Event_Friend_Connection_Status *friend_connection_status) -{ - return; -} - -non_null() -static void tox_event_friend_connection_status_set_friend_number(Tox_Event_Friend_Connection_Status - *friend_connection_status, uint32_t friend_number) +static void tox_event_friend_connection_status_set_friend_number(Tox_Event_Friend_Connection_Status *friend_connection_status, + uint32_t friend_number) { assert(friend_connection_status != nullptr); friend_connection_status->friend_number = friend_number; } -uint32_t tox_event_friend_connection_status_get_friend_number(const Tox_Event_Friend_Connection_Status - *friend_connection_status) +uint32_t tox_event_friend_connection_status_get_friend_number(const Tox_Event_Friend_Connection_Status *friend_connection_status) { assert(friend_connection_status != nullptr); return friend_connection_status->friend_number; } non_null() -static void tox_event_friend_connection_status_set_connection_status(Tox_Event_Friend_Connection_Status - *friend_connection_status, Tox_Connection connection_status) +static void tox_event_friend_connection_status_set_connection_status(Tox_Event_Friend_Connection_Status *friend_connection_status, + Tox_Connection connection_status) { assert(friend_connection_status != nullptr); friend_connection_status->connection_status = connection_status; } -Tox_Connection tox_event_friend_connection_status_get_connection_status(const Tox_Event_Friend_Connection_Status - *friend_connection_status) +Tox_Connection tox_event_friend_connection_status_get_connection_status(const Tox_Event_Friend_Connection_Status *friend_connection_status) { assert(friend_connection_status != nullptr); return friend_connection_status->connection_status; } non_null() -static bool tox_event_friend_connection_status_pack( +static void tox_event_friend_connection_status_construct(Tox_Event_Friend_Connection_Status *friend_connection_status) +{ + *friend_connection_status = (Tox_Event_Friend_Connection_Status) { + 0 + }; +} +non_null() +static void tox_event_friend_connection_status_destruct(Tox_Event_Friend_Connection_Status *friend_connection_status, const Memory *mem) +{ + return; +} + +bool tox_event_friend_connection_status_pack( const Tox_Event_Friend_Connection_Status *event, Bin_Pack *bp) { assert(event != nullptr); @@ -81,7 +78,7 @@ static bool tox_event_friend_connection_status_pack( } non_null() -static bool tox_event_friend_connection_status_unpack( +static bool tox_event_friend_connection_status_unpack_into( Tox_Event_Friend_Connection_Status *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -96,92 +93,120 @@ static bool tox_event_friend_connection_status_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ +const Tox_Event_Friend_Connection_Status *tox_event_get_friend_connection_status(const Tox_Event *event) +{ + return event->type == TOX_EVENT_FRIEND_CONNECTION_STATUS ? event->data.friend_connection_status : nullptr; +} -non_null() -static Tox_Event_Friend_Connection_Status *tox_events_add_friend_connection_status(Tox_Events *events) +Tox_Event_Friend_Connection_Status *tox_event_friend_connection_status_new(const Memory *mem) { - if (events->friend_connection_status_size == UINT32_MAX) { + Tox_Event_Friend_Connection_Status *const friend_connection_status = + (Tox_Event_Friend_Connection_Status *)mem_alloc(mem, sizeof(Tox_Event_Friend_Connection_Status)); + + if (friend_connection_status == nullptr) { return nullptr; } - if (events->friend_connection_status_size == events->friend_connection_status_capacity) { - const uint32_t new_friend_connection_status_capacity = events->friend_connection_status_capacity * 2 + 1; - Tox_Event_Friend_Connection_Status *new_friend_connection_status = (Tox_Event_Friend_Connection_Status *)realloc( - events->friend_connection_status, new_friend_connection_status_capacity * sizeof(Tox_Event_Friend_Connection_Status)); + tox_event_friend_connection_status_construct(friend_connection_status); + return friend_connection_status; +} + +void tox_event_friend_connection_status_free(Tox_Event_Friend_Connection_Status *friend_connection_status, const Memory *mem) +{ + if (friend_connection_status != nullptr) { + tox_event_friend_connection_status_destruct(friend_connection_status, mem); + } + mem_delete(mem, friend_connection_status); +} - if (new_friend_connection_status == nullptr) { - return nullptr; - } +non_null() +static Tox_Event_Friend_Connection_Status *tox_events_add_friend_connection_status(Tox_Events *events, const Memory *mem) +{ + Tox_Event_Friend_Connection_Status *const friend_connection_status = tox_event_friend_connection_status_new(mem); - events->friend_connection_status = new_friend_connection_status; - events->friend_connection_status_capacity = new_friend_connection_status_capacity; + if (friend_connection_status == nullptr) { + return nullptr; } - Tox_Event_Friend_Connection_Status *const friend_connection_status = - &events->friend_connection_status[events->friend_connection_status_size]; - tox_event_friend_connection_status_construct(friend_connection_status); - ++events->friend_connection_status_size; + Tox_Event event; + event.type = TOX_EVENT_FRIEND_CONNECTION_STATUS; + event.data.friend_connection_status = friend_connection_status; + + tox_events_add(events, &event); return friend_connection_status; } -void tox_events_clear_friend_connection_status(Tox_Events *events) +const Tox_Event_Friend_Connection_Status *tox_events_get_friend_connection_status(const Tox_Events *events, uint32_t index) { - if (events == nullptr) { - return; - } + uint32_t friend_connection_status_index = 0; + const uint32_t size = tox_events_get_size(events); - for (uint32_t i = 0; i < events->friend_connection_status_size; ++i) { - tox_event_friend_connection_status_destruct(&events->friend_connection_status[i]); + for (uint32_t i = 0; i < size; ++i) { + if (friend_connection_status_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_FRIEND_CONNECTION_STATUS) { + const Tox_Event_Friend_Connection_Status *friend_connection_status = events->events[i].data.friend_connection_status; + if (friend_connection_status_index == index) { + return friend_connection_status; + } + ++friend_connection_status_index; + } } - free(events->friend_connection_status); - events->friend_connection_status = nullptr; - events->friend_connection_status_size = 0; - events->friend_connection_status_capacity = 0; + return nullptr; } uint32_t tox_events_get_friend_connection_status_size(const Tox_Events *events) { - if (events == nullptr) { - return 0; + uint32_t friend_connection_status_size = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_FRIEND_CONNECTION_STATUS) { + ++friend_connection_status_size; + } } - return events->friend_connection_status_size; + return friend_connection_status_size; } -const Tox_Event_Friend_Connection_Status *tox_events_get_friend_connection_status(const Tox_Events *events, - uint32_t index) +bool tox_event_friend_connection_status_unpack( + Tox_Event_Friend_Connection_Status **event, Bin_Unpack *bu, const Memory *mem) { - assert(index < events->friend_connection_status_size); - assert(events->friend_connection_status != nullptr); - return &events->friend_connection_status[index]; + assert(event != nullptr); + *event = tox_event_friend_connection_status_new(mem); + + if (*event == nullptr) { + return false; + } + + return tox_event_friend_connection_status_unpack_into(*event, bu); } -bool tox_events_pack_friend_connection_status(const Tox_Events *events, Bin_Pack *bp) +non_null() +static Tox_Event_Friend_Connection_Status *tox_event_friend_connection_status_alloc(void *user_data) { - const uint32_t size = tox_events_get_friend_connection_status_size(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_connection_status_pack(tox_events_get_friend_connection_status(events, i), bp)) { - return false; - } + if (state->events == nullptr) { + return nullptr; } - return true; -} -bool tox_events_unpack_friend_connection_status(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Friend_Connection_Status *event = tox_events_add_friend_connection_status(events); + Tox_Event_Friend_Connection_Status *friend_connection_status = tox_events_add_friend_connection_status(state->events, state->mem); - if (event == nullptr) { - return false; + if (friend_connection_status == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; } - return tox_event_friend_connection_status_unpack(event, bu); + return friend_connection_status; } @@ -195,17 +220,9 @@ bool tox_events_unpack_friend_connection_status(Tox_Events *events, Bin_Unpack * void tox_events_handle_friend_connection_status(Tox *tox, uint32_t friend_number, Tox_Connection connection_status, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Connection_Status *friend_connection_status = tox_events_add_friend_connection_status(state->events); + Tox_Event_Friend_Connection_Status *friend_connection_status = tox_event_friend_connection_status_alloc(user_data); if (friend_connection_status == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_lossless_packet.c b/toxcore/events/friend_lossless_packet.c index c1d4ec03a19..ebecf5ee060 100644 --- a/toxcore/events/friend_lossless_packet.c +++ b/toxcore/events/friend_lossless_packet.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -28,19 +29,6 @@ struct Tox_Event_Friend_Lossless_Packet { uint32_t data_length; }; -non_null() -static void tox_event_friend_lossless_packet_construct(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) -{ - *friend_lossless_packet = (Tox_Event_Friend_Lossless_Packet) { - 0 - }; -} -non_null() -static void tox_event_friend_lossless_packet_destruct(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) -{ - free(friend_lossless_packet->data); -} - non_null() static void tox_event_friend_lossless_packet_set_friend_number(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet, uint32_t friend_number) @@ -48,8 +36,7 @@ static void tox_event_friend_lossless_packet_set_friend_number(Tox_Event_Friend_ assert(friend_lossless_packet != nullptr); friend_lossless_packet->friend_number = friend_number; } -uint32_t tox_event_friend_lossless_packet_get_friend_number(const Tox_Event_Friend_Lossless_Packet - *friend_lossless_packet) +uint32_t tox_event_friend_lossless_packet_get_friend_number(const Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) { assert(friend_lossless_packet != nullptr); return friend_lossless_packet->friend_number; @@ -90,7 +77,19 @@ const uint8_t *tox_event_friend_lossless_packet_get_data(const Tox_Event_Friend_ } non_null() -static bool tox_event_friend_lossless_packet_pack( +static void tox_event_friend_lossless_packet_construct(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet) +{ + *friend_lossless_packet = (Tox_Event_Friend_Lossless_Packet) { + 0 + }; +} +non_null() +static void tox_event_friend_lossless_packet_destruct(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet, const Memory *mem) +{ + free(friend_lossless_packet->data); +} + +bool tox_event_friend_lossless_packet_pack( const Tox_Event_Friend_Lossless_Packet *event, Bin_Pack *bp) { assert(event != nullptr); @@ -102,7 +101,7 @@ static bool tox_event_friend_lossless_packet_pack( } non_null() -static bool tox_event_friend_lossless_packet_unpack( +static bool tox_event_friend_lossless_packet_unpack_into( Tox_Event_Friend_Lossless_Packet *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -117,91 +116,120 @@ static bool tox_event_friend_lossless_packet_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Lossless_Packet *tox_events_add_friend_lossless_packet(Tox_Events *events) +const Tox_Event_Friend_Lossless_Packet *tox_event_get_friend_lossless_packet(const Tox_Event *event) { - if (events->friend_lossless_packet_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_lossless_packet_size == events->friend_lossless_packet_capacity) { - const uint32_t new_friend_lossless_packet_capacity = events->friend_lossless_packet_capacity * 2 + 1; - Tox_Event_Friend_Lossless_Packet *new_friend_lossless_packet = (Tox_Event_Friend_Lossless_Packet *)realloc( - events->friend_lossless_packet, new_friend_lossless_packet_capacity * sizeof(Tox_Event_Friend_Lossless_Packet)); + return event->type == TOX_EVENT_FRIEND_LOSSLESS_PACKET ? event->data.friend_lossless_packet : nullptr; +} - if (new_friend_lossless_packet == nullptr) { - return nullptr; - } +Tox_Event_Friend_Lossless_Packet *tox_event_friend_lossless_packet_new(const Memory *mem) +{ + Tox_Event_Friend_Lossless_Packet *const friend_lossless_packet = + (Tox_Event_Friend_Lossless_Packet *)mem_alloc(mem, sizeof(Tox_Event_Friend_Lossless_Packet)); - events->friend_lossless_packet = new_friend_lossless_packet; - events->friend_lossless_packet_capacity = new_friend_lossless_packet_capacity; + if (friend_lossless_packet == nullptr) { + return nullptr; } - Tox_Event_Friend_Lossless_Packet *const friend_lossless_packet = - &events->friend_lossless_packet[events->friend_lossless_packet_size]; tox_event_friend_lossless_packet_construct(friend_lossless_packet); - ++events->friend_lossless_packet_size; return friend_lossless_packet; } -void tox_events_clear_friend_lossless_packet(Tox_Events *events) +void tox_event_friend_lossless_packet_free(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_lossless_packet_size; ++i) { - tox_event_friend_lossless_packet_destruct(&events->friend_lossless_packet[i]); + if (friend_lossless_packet != nullptr) { + tox_event_friend_lossless_packet_destruct(friend_lossless_packet, mem); } - - free(events->friend_lossless_packet); - events->friend_lossless_packet = nullptr; - events->friend_lossless_packet_size = 0; - events->friend_lossless_packet_capacity = 0; + mem_delete(mem, friend_lossless_packet); } -uint32_t tox_events_get_friend_lossless_packet_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Lossless_Packet *tox_events_add_friend_lossless_packet(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Lossless_Packet *const friend_lossless_packet = tox_event_friend_lossless_packet_new(mem); + + if (friend_lossless_packet == nullptr) { + return nullptr; } - return events->friend_lossless_packet_size; + Tox_Event event; + event.type = TOX_EVENT_FRIEND_LOSSLESS_PACKET; + event.data.friend_lossless_packet = friend_lossless_packet; + + tox_events_add(events, &event); + return friend_lossless_packet; } const Tox_Event_Friend_Lossless_Packet *tox_events_get_friend_lossless_packet(const Tox_Events *events, uint32_t index) { - assert(index < events->friend_lossless_packet_size); - assert(events->friend_lossless_packet != nullptr); - return &events->friend_lossless_packet[index]; + uint32_t friend_lossless_packet_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (friend_lossless_packet_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_FRIEND_LOSSLESS_PACKET) { + const Tox_Event_Friend_Lossless_Packet *friend_lossless_packet = events->events[i].data.friend_lossless_packet; + if (friend_lossless_packet_index == index) { + return friend_lossless_packet; + } + ++friend_lossless_packet_index; + } + } + + return nullptr; } -bool tox_events_pack_friend_lossless_packet(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_friend_lossless_packet_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_friend_lossless_packet_size(events); + uint32_t friend_lossless_packet_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_lossless_packet_pack(tox_events_get_friend_lossless_packet(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_FRIEND_LOSSLESS_PACKET) { + ++friend_lossless_packet_size; } } - return true; + + return friend_lossless_packet_size; } -bool tox_events_unpack_friend_lossless_packet(Tox_Events *events, Bin_Unpack *bu) +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 = tox_events_add_friend_lossless_packet(events); + assert(event != nullptr); + *event = tox_event_friend_lossless_packet_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_friend_lossless_packet_unpack(event, bu); + return tox_event_friend_lossless_packet_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Friend_Lossless_Packet *tox_event_friend_lossless_packet_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Friend_Lossless_Packet *friend_lossless_packet = tox_events_add_friend_lossless_packet(state->events, state->mem); + + if (friend_lossless_packet == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_lossless_packet; } @@ -215,17 +243,9 @@ bool tox_events_unpack_friend_lossless_packet(Tox_Events *events, Bin_Unpack *bu void tox_events_handle_friend_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Lossless_Packet *friend_lossless_packet = tox_events_add_friend_lossless_packet(state->events); + Tox_Event_Friend_Lossless_Packet *friend_lossless_packet = tox_event_friend_lossless_packet_alloc(user_data); if (friend_lossless_packet == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_lossy_packet.c b/toxcore/events/friend_lossy_packet.c index e3ba13615b5..384ffb552fc 100644 --- a/toxcore/events/friend_lossy_packet.c +++ b/toxcore/events/friend_lossy_packet.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -28,19 +29,6 @@ struct Tox_Event_Friend_Lossy_Packet { uint32_t data_length; }; -non_null() -static void tox_event_friend_lossy_packet_construct(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet) -{ - *friend_lossy_packet = (Tox_Event_Friend_Lossy_Packet) { - 0 - }; -} -non_null() -static void tox_event_friend_lossy_packet_destruct(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet) -{ - free(friend_lossy_packet->data); -} - non_null() static void tox_event_friend_lossy_packet_set_friend_number(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet, uint32_t friend_number) @@ -89,7 +77,19 @@ const uint8_t *tox_event_friend_lossy_packet_get_data(const Tox_Event_Friend_Los } non_null() -static bool tox_event_friend_lossy_packet_pack( +static void tox_event_friend_lossy_packet_construct(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet) +{ + *friend_lossy_packet = (Tox_Event_Friend_Lossy_Packet) { + 0 + }; +} +non_null() +static void tox_event_friend_lossy_packet_destruct(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet, const Memory *mem) +{ + free(friend_lossy_packet->data); +} + +bool tox_event_friend_lossy_packet_pack( const Tox_Event_Friend_Lossy_Packet *event, Bin_Pack *bp) { assert(event != nullptr); @@ -101,7 +101,7 @@ static bool tox_event_friend_lossy_packet_pack( } non_null() -static bool tox_event_friend_lossy_packet_unpack( +static bool tox_event_friend_lossy_packet_unpack_into( Tox_Event_Friend_Lossy_Packet *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -116,91 +116,120 @@ static bool tox_event_friend_lossy_packet_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Lossy_Packet *tox_events_add_friend_lossy_packet(Tox_Events *events) +const Tox_Event_Friend_Lossy_Packet *tox_event_get_friend_lossy_packet(const Tox_Event *event) { - if (events->friend_lossy_packet_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_lossy_packet_size == events->friend_lossy_packet_capacity) { - const uint32_t new_friend_lossy_packet_capacity = events->friend_lossy_packet_capacity * 2 + 1; - Tox_Event_Friend_Lossy_Packet *new_friend_lossy_packet = (Tox_Event_Friend_Lossy_Packet *)realloc( - events->friend_lossy_packet, new_friend_lossy_packet_capacity * sizeof(Tox_Event_Friend_Lossy_Packet)); + return event->type == TOX_EVENT_FRIEND_LOSSY_PACKET ? event->data.friend_lossy_packet : nullptr; +} - if (new_friend_lossy_packet == nullptr) { - return nullptr; - } +Tox_Event_Friend_Lossy_Packet *tox_event_friend_lossy_packet_new(const Memory *mem) +{ + Tox_Event_Friend_Lossy_Packet *const friend_lossy_packet = + (Tox_Event_Friend_Lossy_Packet *)mem_alloc(mem, sizeof(Tox_Event_Friend_Lossy_Packet)); - events->friend_lossy_packet = new_friend_lossy_packet; - events->friend_lossy_packet_capacity = new_friend_lossy_packet_capacity; + if (friend_lossy_packet == nullptr) { + return nullptr; } - Tox_Event_Friend_Lossy_Packet *const friend_lossy_packet = - &events->friend_lossy_packet[events->friend_lossy_packet_size]; tox_event_friend_lossy_packet_construct(friend_lossy_packet); - ++events->friend_lossy_packet_size; return friend_lossy_packet; } -void tox_events_clear_friend_lossy_packet(Tox_Events *events) +void tox_event_friend_lossy_packet_free(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_lossy_packet_size; ++i) { - tox_event_friend_lossy_packet_destruct(&events->friend_lossy_packet[i]); + if (friend_lossy_packet != nullptr) { + tox_event_friend_lossy_packet_destruct(friend_lossy_packet, mem); } - - free(events->friend_lossy_packet); - events->friend_lossy_packet = nullptr; - events->friend_lossy_packet_size = 0; - events->friend_lossy_packet_capacity = 0; + mem_delete(mem, friend_lossy_packet); } -uint32_t tox_events_get_friend_lossy_packet_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Lossy_Packet *tox_events_add_friend_lossy_packet(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Lossy_Packet *const friend_lossy_packet = tox_event_friend_lossy_packet_new(mem); + + if (friend_lossy_packet == nullptr) { + return nullptr; } - return events->friend_lossy_packet_size; + Tox_Event event; + event.type = TOX_EVENT_FRIEND_LOSSY_PACKET; + event.data.friend_lossy_packet = friend_lossy_packet; + + tox_events_add(events, &event); + return friend_lossy_packet; } const Tox_Event_Friend_Lossy_Packet *tox_events_get_friend_lossy_packet(const Tox_Events *events, uint32_t index) { - assert(index < events->friend_lossy_packet_size); - assert(events->friend_lossy_packet != nullptr); - return &events->friend_lossy_packet[index]; + uint32_t friend_lossy_packet_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (friend_lossy_packet_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_FRIEND_LOSSY_PACKET) { + const Tox_Event_Friend_Lossy_Packet *friend_lossy_packet = events->events[i].data.friend_lossy_packet; + if (friend_lossy_packet_index == index) { + return friend_lossy_packet; + } + ++friend_lossy_packet_index; + } + } + + return nullptr; } -bool tox_events_pack_friend_lossy_packet(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_friend_lossy_packet_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_friend_lossy_packet_size(events); + uint32_t friend_lossy_packet_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_lossy_packet_pack(tox_events_get_friend_lossy_packet(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_FRIEND_LOSSY_PACKET) { + ++friend_lossy_packet_size; } } - return true; + + return friend_lossy_packet_size; } -bool tox_events_unpack_friend_lossy_packet(Tox_Events *events, Bin_Unpack *bu) +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 = tox_events_add_friend_lossy_packet(events); + assert(event != nullptr); + *event = tox_event_friend_lossy_packet_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_friend_lossy_packet_unpack(event, bu); + return tox_event_friend_lossy_packet_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Friend_Lossy_Packet *tox_event_friend_lossy_packet_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Friend_Lossy_Packet *friend_lossy_packet = tox_events_add_friend_lossy_packet(state->events, state->mem); + + if (friend_lossy_packet == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_lossy_packet; } @@ -214,17 +243,9 @@ bool tox_events_unpack_friend_lossy_packet(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Lossy_Packet *friend_lossy_packet = tox_events_add_friend_lossy_packet(state->events); + Tox_Event_Friend_Lossy_Packet *friend_lossy_packet = tox_event_friend_lossy_packet_alloc(user_data); if (friend_lossy_packet == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_message.c b/toxcore/events/friend_message.c index 0487b7bfdb9..092d26b3922 100644 --- a/toxcore/events/friend_message.c +++ b/toxcore/events/friend_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -30,19 +30,6 @@ struct Tox_Event_Friend_Message { uint32_t message_length; }; -non_null() -static void tox_event_friend_message_construct(Tox_Event_Friend_Message *friend_message) -{ - *friend_message = (Tox_Event_Friend_Message) { - 0 - }; -} -non_null() -static void tox_event_friend_message_destruct(Tox_Event_Friend_Message *friend_message) -{ - free(friend_message->message); -} - non_null() static void tox_event_friend_message_set_friend_number(Tox_Event_Friend_Message *friend_message, uint32_t friend_number) @@ -57,7 +44,8 @@ uint32_t tox_event_friend_message_get_friend_number(const Tox_Event_Friend_Messa } non_null() -static void tox_event_friend_message_set_type(Tox_Event_Friend_Message *friend_message, Tox_Message_Type type) +static void tox_event_friend_message_set_type(Tox_Event_Friend_Message *friend_message, + Tox_Message_Type type) { assert(friend_message != nullptr); friend_message->type = type; @@ -69,8 +57,8 @@ Tox_Message_Type tox_event_friend_message_get_type(const Tox_Event_Friend_Messag } non_null() -static bool tox_event_friend_message_set_message(Tox_Event_Friend_Message *friend_message, const uint8_t *message, - uint32_t message_length) +static bool tox_event_friend_message_set_message(Tox_Event_Friend_Message *friend_message, + const uint8_t *message, uint32_t message_length) { assert(friend_message != nullptr); @@ -103,7 +91,19 @@ const uint8_t *tox_event_friend_message_get_message(const Tox_Event_Friend_Messa } non_null() -static bool tox_event_friend_message_pack( +static void tox_event_friend_message_construct(Tox_Event_Friend_Message *friend_message) +{ + *friend_message = (Tox_Event_Friend_Message) { + 0 + }; +} +non_null() +static void tox_event_friend_message_destruct(Tox_Event_Friend_Message *friend_message, const Memory *mem) +{ + free(friend_message->message); +} + +bool tox_event_friend_message_pack( const Tox_Event_Friend_Message *event, Bin_Pack *bp) { assert(event != nullptr); @@ -116,7 +116,7 @@ static bool tox_event_friend_message_pack( } non_null() -static bool tox_event_friend_message_unpack( +static bool tox_event_friend_message_unpack_into( Tox_Event_Friend_Message *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -132,90 +132,120 @@ static bool tox_event_friend_message_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Message *tox_events_add_friend_message(Tox_Events *events) +const Tox_Event_Friend_Message *tox_event_get_friend_message(const Tox_Event *event) { - if (events->friend_message_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_message_size == events->friend_message_capacity) { - const uint32_t new_friend_message_capacity = events->friend_message_capacity * 2 + 1; - Tox_Event_Friend_Message *new_friend_message = (Tox_Event_Friend_Message *)realloc( - events->friend_message, new_friend_message_capacity * sizeof(Tox_Event_Friend_Message)); + return event->type == TOX_EVENT_FRIEND_MESSAGE ? event->data.friend_message : nullptr; +} - if (new_friend_message == nullptr) { - return nullptr; - } +Tox_Event_Friend_Message *tox_event_friend_message_new(const Memory *mem) +{ + Tox_Event_Friend_Message *const friend_message = + (Tox_Event_Friend_Message *)mem_alloc(mem, sizeof(Tox_Event_Friend_Message)); - events->friend_message = new_friend_message; - events->friend_message_capacity = new_friend_message_capacity; + if (friend_message == nullptr) { + return nullptr; } - Tox_Event_Friend_Message *const friend_message = &events->friend_message[events->friend_message_size]; tox_event_friend_message_construct(friend_message); - ++events->friend_message_size; return friend_message; } -void tox_events_clear_friend_message(Tox_Events *events) +void tox_event_friend_message_free(Tox_Event_Friend_Message *friend_message, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_message_size; ++i) { - tox_event_friend_message_destruct(&events->friend_message[i]); + if (friend_message != nullptr) { + tox_event_friend_message_destruct(friend_message, mem); } - - free(events->friend_message); - events->friend_message = nullptr; - events->friend_message_size = 0; - events->friend_message_capacity = 0; + mem_delete(mem, friend_message); } -uint32_t tox_events_get_friend_message_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Message *tox_events_add_friend_message(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Message *const friend_message = tox_event_friend_message_new(mem); + + if (friend_message == nullptr) { + return nullptr; } - return events->friend_message_size; + Tox_Event event; + event.type = TOX_EVENT_FRIEND_MESSAGE; + event.data.friend_message = friend_message; + + tox_events_add(events, &event); + return friend_message; } const Tox_Event_Friend_Message *tox_events_get_friend_message(const Tox_Events *events, uint32_t index) { - assert(index < events->friend_message_size); - assert(events->friend_message != nullptr); - return &events->friend_message[index]; + uint32_t friend_message_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (friend_message_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_FRIEND_MESSAGE) { + const Tox_Event_Friend_Message *friend_message = events->events[i].data.friend_message; + if (friend_message_index == index) { + return friend_message; + } + ++friend_message_index; + } + } + + return nullptr; } -bool tox_events_pack_friend_message(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_friend_message_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_friend_message_size(events); + uint32_t friend_message_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_message_pack(tox_events_get_friend_message(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_FRIEND_MESSAGE) { + ++friend_message_size; } } - return true; + + return friend_message_size; } -bool tox_events_unpack_friend_message(Tox_Events *events, Bin_Unpack *bu) +bool tox_event_friend_message_unpack( + Tox_Event_Friend_Message **event, Bin_Unpack *bu, const Memory *mem) { - Tox_Event_Friend_Message *event = tox_events_add_friend_message(events); + assert(event != nullptr); + *event = tox_event_friend_message_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_friend_message_unpack(event, bu); + return tox_event_friend_message_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Friend_Message *tox_event_friend_message_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Friend_Message *friend_message = tox_events_add_friend_message(state->events, state->mem); + + if (friend_message == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_message; } @@ -226,20 +256,12 @@ bool tox_events_unpack_friend_message(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_friend_message(Tox *tox, uint32_t friend_number, Tox_Message_Type type, const uint8_t *message, - size_t length, void *user_data) +void tox_events_handle_friend_message(Tox *tox, uint32_t friend_number, Tox_Message_Type type, const uint8_t *message, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Message *friend_message = tox_events_add_friend_message(state->events); + Tox_Event_Friend_Message *friend_message = tox_event_friend_message_alloc(user_data); if (friend_message == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_name.c b/toxcore/events/friend_name.c index 6dfc3ddf0e1..97791b7509c 100644 --- a/toxcore/events/friend_name.c +++ b/toxcore/events/friend_name.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -28,19 +29,6 @@ struct Tox_Event_Friend_Name { uint32_t name_length; }; -non_null() -static void tox_event_friend_name_construct(Tox_Event_Friend_Name *friend_name) -{ - *friend_name = (Tox_Event_Friend_Name) { - 0 - }; -} -non_null() -static void tox_event_friend_name_destruct(Tox_Event_Friend_Name *friend_name) -{ - free(friend_name->name); -} - non_null() static void tox_event_friend_name_set_friend_number(Tox_Event_Friend_Name *friend_name, uint32_t friend_number) @@ -55,8 +43,8 @@ uint32_t tox_event_friend_name_get_friend_number(const Tox_Event_Friend_Name *fr } non_null() -static bool tox_event_friend_name_set_name(Tox_Event_Friend_Name *friend_name, const uint8_t *name, - uint32_t name_length) +static bool tox_event_friend_name_set_name(Tox_Event_Friend_Name *friend_name, + const uint8_t *name, uint32_t name_length) { assert(friend_name != nullptr); @@ -89,7 +77,19 @@ const uint8_t *tox_event_friend_name_get_name(const Tox_Event_Friend_Name *frien } non_null() -static bool tox_event_friend_name_pack( +static void tox_event_friend_name_construct(Tox_Event_Friend_Name *friend_name) +{ + *friend_name = (Tox_Event_Friend_Name) { + 0 + }; +} +non_null() +static void tox_event_friend_name_destruct(Tox_Event_Friend_Name *friend_name, const Memory *mem) +{ + free(friend_name->name); +} + +bool tox_event_friend_name_pack( const Tox_Event_Friend_Name *event, Bin_Pack *bp) { assert(event != nullptr); @@ -101,7 +101,7 @@ static bool tox_event_friend_name_pack( } non_null() -static bool tox_event_friend_name_unpack( +static bool tox_event_friend_name_unpack_into( Tox_Event_Friend_Name *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -116,90 +116,120 @@ static bool tox_event_friend_name_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Name *tox_events_add_friend_name(Tox_Events *events) +const Tox_Event_Friend_Name *tox_event_get_friend_name(const Tox_Event *event) { - if (events->friend_name_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_name_size == events->friend_name_capacity) { - const uint32_t new_friend_name_capacity = events->friend_name_capacity * 2 + 1; - Tox_Event_Friend_Name *new_friend_name = (Tox_Event_Friend_Name *)realloc( - events->friend_name, new_friend_name_capacity * sizeof(Tox_Event_Friend_Name)); + return event->type == TOX_EVENT_FRIEND_NAME ? event->data.friend_name : nullptr; +} - if (new_friend_name == nullptr) { - return nullptr; - } +Tox_Event_Friend_Name *tox_event_friend_name_new(const Memory *mem) +{ + Tox_Event_Friend_Name *const friend_name = + (Tox_Event_Friend_Name *)mem_alloc(mem, sizeof(Tox_Event_Friend_Name)); - events->friend_name = new_friend_name; - events->friend_name_capacity = new_friend_name_capacity; + if (friend_name == nullptr) { + return nullptr; } - Tox_Event_Friend_Name *const friend_name = &events->friend_name[events->friend_name_size]; tox_event_friend_name_construct(friend_name); - ++events->friend_name_size; return friend_name; } -void tox_events_clear_friend_name(Tox_Events *events) +void tox_event_friend_name_free(Tox_Event_Friend_Name *friend_name, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_name_size; ++i) { - tox_event_friend_name_destruct(&events->friend_name[i]); + if (friend_name != nullptr) { + tox_event_friend_name_destruct(friend_name, mem); } - - free(events->friend_name); - events->friend_name = nullptr; - events->friend_name_size = 0; - events->friend_name_capacity = 0; + mem_delete(mem, friend_name); } -uint32_t tox_events_get_friend_name_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Name *tox_events_add_friend_name(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Name *const friend_name = tox_event_friend_name_new(mem); + + if (friend_name == nullptr) { + return nullptr; } - return events->friend_name_size; + Tox_Event event; + event.type = TOX_EVENT_FRIEND_NAME; + event.data.friend_name = friend_name; + + tox_events_add(events, &event); + return friend_name; } const Tox_Event_Friend_Name *tox_events_get_friend_name(const Tox_Events *events, uint32_t index) { - assert(index < events->friend_name_size); - assert(events->friend_name != nullptr); - return &events->friend_name[index]; + uint32_t friend_name_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (friend_name_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_FRIEND_NAME) { + const Tox_Event_Friend_Name *friend_name = events->events[i].data.friend_name; + if (friend_name_index == index) { + return friend_name; + } + ++friend_name_index; + } + } + + return nullptr; } -bool tox_events_pack_friend_name(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_friend_name_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_friend_name_size(events); + uint32_t friend_name_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_name_pack(tox_events_get_friend_name(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_FRIEND_NAME) { + ++friend_name_size; } } - return true; + + return friend_name_size; } -bool tox_events_unpack_friend_name(Tox_Events *events, Bin_Unpack *bu) +bool tox_event_friend_name_unpack( + Tox_Event_Friend_Name **event, Bin_Unpack *bu, const Memory *mem) { - Tox_Event_Friend_Name *event = tox_events_add_friend_name(events); + assert(event != nullptr); + *event = tox_event_friend_name_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_friend_name_unpack(event, bu); + return tox_event_friend_name_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Friend_Name *tox_event_friend_name_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Friend_Name *friend_name = tox_events_add_friend_name(state->events, state->mem); + + if (friend_name == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_name; } @@ -211,19 +241,11 @@ bool tox_events_unpack_friend_name(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_name(Tox *tox, uint32_t friend_number, const uint8_t *name, size_t length, - void *user_data) + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Name *friend_name = tox_events_add_friend_name(state->events); + Tox_Event_Friend_Name *friend_name = tox_event_friend_name_alloc(user_data); if (friend_name == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_read_receipt.c b/toxcore/events/friend_read_receipt.c index 0a8cddb48a0..7b65c885a1c 100644 --- a/toxcore/events/friend_read_receipt.c +++ b/toxcore/events/friend_read_receipt.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -26,19 +27,6 @@ struct Tox_Event_Friend_Read_Receipt { uint32_t message_id; }; -non_null() -static void tox_event_friend_read_receipt_construct(Tox_Event_Friend_Read_Receipt *friend_read_receipt) -{ - *friend_read_receipt = (Tox_Event_Friend_Read_Receipt) { - 0 - }; -} -non_null() -static void tox_event_friend_read_receipt_destruct(Tox_Event_Friend_Read_Receipt *friend_read_receipt) -{ - return; -} - non_null() static void tox_event_friend_read_receipt_set_friend_number(Tox_Event_Friend_Read_Receipt *friend_read_receipt, uint32_t friend_number) @@ -66,7 +54,19 @@ uint32_t tox_event_friend_read_receipt_get_message_id(const Tox_Event_Friend_Rea } non_null() -static bool tox_event_friend_read_receipt_pack( +static void tox_event_friend_read_receipt_construct(Tox_Event_Friend_Read_Receipt *friend_read_receipt) +{ + *friend_read_receipt = (Tox_Event_Friend_Read_Receipt) { + 0 + }; +} +non_null() +static void tox_event_friend_read_receipt_destruct(Tox_Event_Friend_Read_Receipt *friend_read_receipt, const Memory *mem) +{ + return; +} + +bool tox_event_friend_read_receipt_pack( const Tox_Event_Friend_Read_Receipt *event, Bin_Pack *bp) { assert(event != nullptr); @@ -78,7 +78,7 @@ static bool tox_event_friend_read_receipt_pack( } non_null() -static bool tox_event_friend_read_receipt_unpack( +static bool tox_event_friend_read_receipt_unpack_into( Tox_Event_Friend_Read_Receipt *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -93,91 +93,120 @@ static bool tox_event_friend_read_receipt_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Read_Receipt *tox_events_add_friend_read_receipt(Tox_Events *events) +const Tox_Event_Friend_Read_Receipt *tox_event_get_friend_read_receipt(const Tox_Event *event) { - if (events->friend_read_receipt_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_read_receipt_size == events->friend_read_receipt_capacity) { - const uint32_t new_friend_read_receipt_capacity = events->friend_read_receipt_capacity * 2 + 1; - Tox_Event_Friend_Read_Receipt *new_friend_read_receipt = (Tox_Event_Friend_Read_Receipt *)realloc( - events->friend_read_receipt, new_friend_read_receipt_capacity * sizeof(Tox_Event_Friend_Read_Receipt)); + return event->type == TOX_EVENT_FRIEND_READ_RECEIPT ? event->data.friend_read_receipt : nullptr; +} - if (new_friend_read_receipt == nullptr) { - return nullptr; - } +Tox_Event_Friend_Read_Receipt *tox_event_friend_read_receipt_new(const Memory *mem) +{ + Tox_Event_Friend_Read_Receipt *const friend_read_receipt = + (Tox_Event_Friend_Read_Receipt *)mem_alloc(mem, sizeof(Tox_Event_Friend_Read_Receipt)); - events->friend_read_receipt = new_friend_read_receipt; - events->friend_read_receipt_capacity = new_friend_read_receipt_capacity; + if (friend_read_receipt == nullptr) { + return nullptr; } - Tox_Event_Friend_Read_Receipt *const friend_read_receipt = - &events->friend_read_receipt[events->friend_read_receipt_size]; tox_event_friend_read_receipt_construct(friend_read_receipt); - ++events->friend_read_receipt_size; return friend_read_receipt; } -void tox_events_clear_friend_read_receipt(Tox_Events *events) +void tox_event_friend_read_receipt_free(Tox_Event_Friend_Read_Receipt *friend_read_receipt, const Memory *mem) { - if (events == nullptr) { - return; + if (friend_read_receipt != nullptr) { + tox_event_friend_read_receipt_destruct(friend_read_receipt, mem); } - - for (uint32_t i = 0; i < events->friend_read_receipt_size; ++i) { - tox_event_friend_read_receipt_destruct(&events->friend_read_receipt[i]); - } - - free(events->friend_read_receipt); - events->friend_read_receipt = nullptr; - events->friend_read_receipt_size = 0; - events->friend_read_receipt_capacity = 0; + mem_delete(mem, friend_read_receipt); } -uint32_t tox_events_get_friend_read_receipt_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Read_Receipt *tox_events_add_friend_read_receipt(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Read_Receipt *const friend_read_receipt = tox_event_friend_read_receipt_new(mem); + + if (friend_read_receipt == nullptr) { + return nullptr; } - return events->friend_read_receipt_size; + Tox_Event event; + event.type = TOX_EVENT_FRIEND_READ_RECEIPT; + event.data.friend_read_receipt = friend_read_receipt; + + tox_events_add(events, &event); + return friend_read_receipt; } const Tox_Event_Friend_Read_Receipt *tox_events_get_friend_read_receipt(const Tox_Events *events, uint32_t index) { - assert(index < events->friend_read_receipt_size); - assert(events->friend_read_receipt != nullptr); - return &events->friend_read_receipt[index]; + uint32_t friend_read_receipt_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (friend_read_receipt_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_FRIEND_READ_RECEIPT) { + const Tox_Event_Friend_Read_Receipt *friend_read_receipt = events->events[i].data.friend_read_receipt; + if (friend_read_receipt_index == index) { + return friend_read_receipt; + } + ++friend_read_receipt_index; + } + } + + return nullptr; } -bool tox_events_pack_friend_read_receipt(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_friend_read_receipt_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_friend_read_receipt_size(events); + uint32_t friend_read_receipt_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_read_receipt_pack(tox_events_get_friend_read_receipt(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_FRIEND_READ_RECEIPT) { + ++friend_read_receipt_size; } } - return true; + + return friend_read_receipt_size; } -bool tox_events_unpack_friend_read_receipt(Tox_Events *events, Bin_Unpack *bu) +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 = tox_events_add_friend_read_receipt(events); + assert(event != nullptr); + *event = tox_event_friend_read_receipt_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_friend_read_receipt_unpack(event, bu); + return tox_event_friend_read_receipt_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Friend_Read_Receipt *tox_event_friend_read_receipt_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Friend_Read_Receipt *friend_read_receipt = tox_events_add_friend_read_receipt(state->events, state->mem); + + if (friend_read_receipt == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_read_receipt; } @@ -188,19 +217,12 @@ bool tox_events_unpack_friend_read_receipt(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_friend_read_receipt(Tox *tox, uint32_t friend_number, uint32_t message_id, void *user_data) +void tox_events_handle_friend_read_receipt(Tox *tox, uint32_t friend_number, uint32_t message_id, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Read_Receipt *friend_read_receipt = tox_events_add_friend_read_receipt(state->events); + Tox_Event_Friend_Read_Receipt *friend_read_receipt = tox_event_friend_read_receipt_alloc(user_data); if (friend_read_receipt == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_request.c b/toxcore/events/friend_request.c index 401085803dd..5c7b6b964e3 100644 --- a/toxcore/events/friend_request.c +++ b/toxcore/events/friend_request.c @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_private.h" /***************************************************** @@ -28,19 +29,6 @@ struct Tox_Event_Friend_Request { uint32_t message_length; }; -non_null() -static void tox_event_friend_request_construct(Tox_Event_Friend_Request *friend_request) -{ - *friend_request = (Tox_Event_Friend_Request) { - 0 - }; -} -non_null() -static void tox_event_friend_request_destruct(Tox_Event_Friend_Request *friend_request) -{ - free(friend_request->message); -} - non_null() static bool tox_event_friend_request_set_public_key(Tox_Event_Friend_Request *friend_request, const uint8_t *public_key) { @@ -56,18 +44,18 @@ const uint8_t *tox_event_friend_request_get_public_key(const Tox_Event_Friend_Re } non_null() -static bool tox_event_friend_request_set_message(Tox_Event_Friend_Request *friend_request, const uint8_t *message, - uint32_t message_length) +static bool tox_event_friend_request_set_message(Tox_Event_Friend_Request *friend_request, + const uint8_t *message, uint32_t message_length, const Memory *mem) { assert(friend_request != nullptr); if (friend_request->message != nullptr) { - free(friend_request->message); + mem_delete(mem, friend_request->message); friend_request->message = nullptr; friend_request->message_length = 0; } - uint8_t *message_copy = (uint8_t *)malloc(message_length); + uint8_t *message_copy = (uint8_t *)mem_balloc(mem, message_length); if (message_copy == nullptr) { return false; @@ -90,7 +78,19 @@ const uint8_t *tox_event_friend_request_get_message(const Tox_Event_Friend_Reque } non_null() -static bool tox_event_friend_request_pack( +static void tox_event_friend_request_construct(Tox_Event_Friend_Request *friend_request) +{ + *friend_request = (Tox_Event_Friend_Request) { + 0 + }; +} +non_null() +static void tox_event_friend_request_destruct(Tox_Event_Friend_Request *friend_request, const Memory *mem) +{ + mem_delete(mem, friend_request->message); +} + +bool tox_event_friend_request_pack( const Tox_Event_Friend_Request *event, Bin_Pack *bp) { assert(event != nullptr); @@ -102,7 +102,7 @@ static bool tox_event_friend_request_pack( } non_null() -static bool tox_event_friend_request_unpack( +static bool tox_event_friend_request_unpack_into( Tox_Event_Friend_Request *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -114,93 +114,119 @@ static bool tox_event_friend_request_unpack( && bin_unpack_bin(bu, &event->message, &event->message_length); } +const Tox_Event_Friend_Request *tox_event_get_friend_request( + const Tox_Event *event) +{ + return event->type == TOX_EVENT_FRIEND_REQUEST ? event->data.friend_request : nullptr; +} -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ +Tox_Event_Friend_Request *tox_event_friend_request_new(const Memory *mem) +{ + Tox_Event_Friend_Request *const friend_request = + (Tox_Event_Friend_Request *)mem_alloc(mem, sizeof(Tox_Event_Friend_Request)); + if (friend_request == nullptr) { + return nullptr; + } + + tox_event_friend_request_construct(friend_request); + return friend_request; +} + +void tox_event_friend_request_free(Tox_Event_Friend_Request *friend_request, const Memory *mem) +{ + if (friend_request != nullptr) { + tox_event_friend_request_destruct(friend_request, mem); + } + mem_delete(mem, friend_request); +} non_null() -static Tox_Event_Friend_Request *tox_events_add_friend_request(Tox_Events *events) +static Tox_Event_Friend_Request *tox_events_add_friend_request(Tox_Events *events, const Memory *mem) { - if (events->friend_request_size == UINT32_MAX) { + Tox_Event_Friend_Request *const friend_request = tox_event_friend_request_new(mem); + + if (friend_request == nullptr) { return nullptr; } - if (events->friend_request_size == events->friend_request_capacity) { - const uint32_t new_friend_request_capacity = events->friend_request_capacity * 2 + 1; - Tox_Event_Friend_Request *new_friend_request = (Tox_Event_Friend_Request *)realloc( - events->friend_request, new_friend_request_capacity * sizeof(Tox_Event_Friend_Request)); + Tox_Event event; + event.type = TOX_EVENT_FRIEND_REQUEST; + event.data.friend_request = friend_request; + + tox_events_add(events, &event); + return friend_request; +} + +const Tox_Event_Friend_Request *tox_events_get_friend_request( + const Tox_Events *events, uint32_t index) +{ + uint32_t friend_request_index = 0; + const uint32_t size = tox_events_get_size(events); - if (new_friend_request == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (friend_request_index > index) { return nullptr; } - events->friend_request = new_friend_request; - events->friend_request_capacity = new_friend_request_capacity; + if (events->events[i].type == TOX_EVENT_FRIEND_REQUEST) { + const Tox_Event_Friend_Request *friend_request = events->events[i].data.friend_request; + if (friend_request_index == index) { + return friend_request; + } + ++friend_request_index; + } } - Tox_Event_Friend_Request *const friend_request = &events->friend_request[events->friend_request_size]; - tox_event_friend_request_construct(friend_request); - ++events->friend_request_size; - return friend_request; + return nullptr; } -void tox_events_clear_friend_request(Tox_Events *events) +uint32_t tox_events_get_friend_request_size( + const Tox_Events *events) { - if (events == nullptr) { - return; - } + uint32_t friend_request_size = 0; + const uint32_t size = tox_events_get_size(events); - for (uint32_t i = 0; i < events->friend_request_size; ++i) { - tox_event_friend_request_destruct(&events->friend_request[i]); + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_FRIEND_REQUEST) { + ++friend_request_size; + } } - free(events->friend_request); - events->friend_request = nullptr; - events->friend_request_size = 0; - events->friend_request_capacity = 0; + return friend_request_size; } -uint32_t tox_events_get_friend_request_size(const Tox_Events *events) +bool tox_event_friend_request_unpack( + Tox_Event_Friend_Request **event, Bin_Unpack *bu, const Memory *mem) { - if (events == nullptr) { - return 0; - } + assert(event != nullptr); + *event = tox_event_friend_request_new(mem); - return events->friend_request_size; -} + if (*event == nullptr) { + return false; + } -const Tox_Event_Friend_Request *tox_events_get_friend_request(const Tox_Events *events, uint32_t index) -{ - assert(index < events->friend_request_size); - assert(events->friend_request != nullptr); - return &events->friend_request[index]; + return tox_event_friend_request_unpack_into(*event, bu); } -bool tox_events_pack_friend_request(const Tox_Events *events, Bin_Pack *bp) +non_null() +static Tox_Event_Friend_Request *tox_event_friend_request_alloc(void *user_data) { - const uint32_t size = tox_events_get_friend_request_size(events); + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); - for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_request_pack(tox_events_get_friend_request(events, i), bp)) { - return false; - } + if (state->events == nullptr) { + return nullptr; } - return true; -} -bool tox_events_unpack_friend_request(Tox_Events *events, Bin_Unpack *bu) -{ - Tox_Event_Friend_Request *event = tox_events_add_friend_request(events); + Tox_Event_Friend_Request *friend_request = tox_events_add_friend_request(state->events, state->mem); - if (event == nullptr) { - return false; + if (friend_request == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; } - return tox_event_friend_request_unpack(event, bu); + return friend_request; } @@ -214,20 +240,14 @@ bool tox_events_unpack_friend_request(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Request *friend_request = tox_events_add_friend_request(state->events); + Tox_Event_Friend_Request *friend_request = tox_event_friend_request_alloc(user_data); if (friend_request == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } + const Tox_System *sys = tox_get_system(tox); + tox_event_friend_request_set_public_key(friend_request, public_key); - tox_event_friend_request_set_message(friend_request, message, length); + tox_event_friend_request_set_message(friend_request, message, length, sys->mem); } diff --git a/toxcore/events/friend_status.c b/toxcore/events/friend_status.c index 3c2a55732dd..a5c5b03909f 100644 --- a/toxcore/events/friend_status.c +++ b/toxcore/events/friend_status.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -27,19 +27,6 @@ struct Tox_Event_Friend_Status { Tox_User_Status status; }; -non_null() -static void tox_event_friend_status_construct(Tox_Event_Friend_Status *friend_status) -{ - *friend_status = (Tox_Event_Friend_Status) { - 0 - }; -} -non_null() -static void tox_event_friend_status_destruct(Tox_Event_Friend_Status *friend_status) -{ - return; -} - non_null() static void tox_event_friend_status_set_friend_number(Tox_Event_Friend_Status *friend_status, uint32_t friend_number) @@ -67,7 +54,19 @@ Tox_User_Status tox_event_friend_status_get_status(const Tox_Event_Friend_Status } non_null() -static bool tox_event_friend_status_pack( +static void tox_event_friend_status_construct(Tox_Event_Friend_Status *friend_status) +{ + *friend_status = (Tox_Event_Friend_Status) { + 0 + }; +} +non_null() +static void tox_event_friend_status_destruct(Tox_Event_Friend_Status *friend_status, const Memory *mem) +{ + return; +} + +bool tox_event_friend_status_pack( const Tox_Event_Friend_Status *event, Bin_Pack *bp) { assert(event != nullptr); @@ -79,7 +78,7 @@ static bool tox_event_friend_status_pack( } non_null() -static bool tox_event_friend_status_unpack( +static bool tox_event_friend_status_unpack_into( Tox_Event_Friend_Status *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -94,90 +93,120 @@ static bool tox_event_friend_status_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Status *tox_events_add_friend_status(Tox_Events *events) +const Tox_Event_Friend_Status *tox_event_get_friend_status(const Tox_Event *event) { - if (events->friend_status_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_status_size == events->friend_status_capacity) { - const uint32_t new_friend_status_capacity = events->friend_status_capacity * 2 + 1; - Tox_Event_Friend_Status *new_friend_status = (Tox_Event_Friend_Status *)realloc( - events->friend_status, new_friend_status_capacity * sizeof(Tox_Event_Friend_Status)); + return event->type == TOX_EVENT_FRIEND_STATUS ? event->data.friend_status : nullptr; +} - if (new_friend_status == nullptr) { - return nullptr; - } +Tox_Event_Friend_Status *tox_event_friend_status_new(const Memory *mem) +{ + Tox_Event_Friend_Status *const friend_status = + (Tox_Event_Friend_Status *)mem_alloc(mem, sizeof(Tox_Event_Friend_Status)); - events->friend_status = new_friend_status; - events->friend_status_capacity = new_friend_status_capacity; + if (friend_status == nullptr) { + return nullptr; } - Tox_Event_Friend_Status *const friend_status = &events->friend_status[events->friend_status_size]; tox_event_friend_status_construct(friend_status); - ++events->friend_status_size; return friend_status; } -void tox_events_clear_friend_status(Tox_Events *events) +void tox_event_friend_status_free(Tox_Event_Friend_Status *friend_status, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_status_size; ++i) { - tox_event_friend_status_destruct(&events->friend_status[i]); + if (friend_status != nullptr) { + tox_event_friend_status_destruct(friend_status, mem); } - - free(events->friend_status); - events->friend_status = nullptr; - events->friend_status_size = 0; - events->friend_status_capacity = 0; + mem_delete(mem, friend_status); } -uint32_t tox_events_get_friend_status_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Status *tox_events_add_friend_status(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Status *const friend_status = tox_event_friend_status_new(mem); + + if (friend_status == nullptr) { + return nullptr; } - return events->friend_status_size; + Tox_Event event; + event.type = TOX_EVENT_FRIEND_STATUS; + event.data.friend_status = friend_status; + + tox_events_add(events, &event); + return friend_status; } const Tox_Event_Friend_Status *tox_events_get_friend_status(const Tox_Events *events, uint32_t index) { - assert(index < events->friend_status_size); - assert(events->friend_status != nullptr); - return &events->friend_status[index]; + uint32_t friend_status_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (friend_status_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_FRIEND_STATUS) { + const Tox_Event_Friend_Status *friend_status = events->events[i].data.friend_status; + if (friend_status_index == index) { + return friend_status; + } + ++friend_status_index; + } + } + + return nullptr; } -bool tox_events_pack_friend_status(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_friend_status_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_friend_status_size(events); + uint32_t friend_status_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_status_pack(tox_events_get_friend_status(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_FRIEND_STATUS) { + ++friend_status_size; } } - return true; + + return friend_status_size; } -bool tox_events_unpack_friend_status(Tox_Events *events, Bin_Unpack *bu) +bool tox_event_friend_status_unpack( + Tox_Event_Friend_Status **event, Bin_Unpack *bu, const Memory *mem) { - Tox_Event_Friend_Status *event = tox_events_add_friend_status(events); + assert(event != nullptr); + *event = tox_event_friend_status_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_friend_status_unpack(event, bu); + return tox_event_friend_status_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Friend_Status *tox_event_friend_status_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Friend_Status *friend_status = tox_events_add_friend_status(state->events, state->mem); + + if (friend_status == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_status; } @@ -189,19 +218,11 @@ bool tox_events_unpack_friend_status(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_friend_status(Tox *tox, uint32_t friend_number, Tox_User_Status status, - void *user_data) + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Status *friend_status = tox_events_add_friend_status(state->events); + Tox_Event_Friend_Status *friend_status = tox_event_friend_status_alloc(user_data); if (friend_status == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_status_message.c b/toxcore/events/friend_status_message.c index 58bb0b97a7e..322f2adb433 100644 --- a/toxcore/events/friend_status_message.c +++ b/toxcore/events/friend_status_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -28,19 +29,6 @@ struct Tox_Event_Friend_Status_Message { uint32_t message_length; }; -non_null() -static void tox_event_friend_status_message_construct(Tox_Event_Friend_Status_Message *friend_status_message) -{ - *friend_status_message = (Tox_Event_Friend_Status_Message) { - 0 - }; -} -non_null() -static void tox_event_friend_status_message_destruct(Tox_Event_Friend_Status_Message *friend_status_message) -{ - free(friend_status_message->message); -} - non_null() static void tox_event_friend_status_message_set_friend_number(Tox_Event_Friend_Status_Message *friend_status_message, uint32_t friend_number) @@ -77,21 +65,31 @@ static bool tox_event_friend_status_message_set_message(Tox_Event_Friend_Status_ friend_status_message->message_length = message_length; return true; } -uint32_t tox_event_friend_status_message_get_message_length(const Tox_Event_Friend_Status_Message - *friend_status_message) +uint32_t tox_event_friend_status_message_get_message_length(const Tox_Event_Friend_Status_Message *friend_status_message) { assert(friend_status_message != nullptr); return friend_status_message->message_length; } -const uint8_t *tox_event_friend_status_message_get_message(const Tox_Event_Friend_Status_Message - *friend_status_message) +const uint8_t *tox_event_friend_status_message_get_message(const Tox_Event_Friend_Status_Message *friend_status_message) { assert(friend_status_message != nullptr); return friend_status_message->message; } non_null() -static bool tox_event_friend_status_message_pack( +static void tox_event_friend_status_message_construct(Tox_Event_Friend_Status_Message *friend_status_message) +{ + *friend_status_message = (Tox_Event_Friend_Status_Message) { + 0 + }; +} +non_null() +static void tox_event_friend_status_message_destruct(Tox_Event_Friend_Status_Message *friend_status_message, const Memory *mem) +{ + free(friend_status_message->message); +} + +bool tox_event_friend_status_message_pack( const Tox_Event_Friend_Status_Message *event, Bin_Pack *bp) { assert(event != nullptr); @@ -103,7 +101,7 @@ static bool tox_event_friend_status_message_pack( } non_null() -static bool tox_event_friend_status_message_unpack( +static bool tox_event_friend_status_message_unpack_into( Tox_Event_Friend_Status_Message *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -118,91 +116,120 @@ static bool tox_event_friend_status_message_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Status_Message *tox_events_add_friend_status_message(Tox_Events *events) +const Tox_Event_Friend_Status_Message *tox_event_get_friend_status_message(const Tox_Event *event) { - if (events->friend_status_message_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_status_message_size == events->friend_status_message_capacity) { - const uint32_t new_friend_status_message_capacity = events->friend_status_message_capacity * 2 + 1; - Tox_Event_Friend_Status_Message *new_friend_status_message = (Tox_Event_Friend_Status_Message *)realloc( - events->friend_status_message, new_friend_status_message_capacity * sizeof(Tox_Event_Friend_Status_Message)); + return event->type == TOX_EVENT_FRIEND_STATUS_MESSAGE ? event->data.friend_status_message : nullptr; +} - if (new_friend_status_message == nullptr) { - return nullptr; - } +Tox_Event_Friend_Status_Message *tox_event_friend_status_message_new(const Memory *mem) +{ + Tox_Event_Friend_Status_Message *const friend_status_message = + (Tox_Event_Friend_Status_Message *)mem_alloc(mem, sizeof(Tox_Event_Friend_Status_Message)); - events->friend_status_message = new_friend_status_message; - events->friend_status_message_capacity = new_friend_status_message_capacity; + if (friend_status_message == nullptr) { + return nullptr; } - Tox_Event_Friend_Status_Message *const friend_status_message = - &events->friend_status_message[events->friend_status_message_size]; tox_event_friend_status_message_construct(friend_status_message); - ++events->friend_status_message_size; return friend_status_message; } -void tox_events_clear_friend_status_message(Tox_Events *events) +void tox_event_friend_status_message_free(Tox_Event_Friend_Status_Message *friend_status_message, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_status_message_size; ++i) { - tox_event_friend_status_message_destruct(&events->friend_status_message[i]); + if (friend_status_message != nullptr) { + tox_event_friend_status_message_destruct(friend_status_message, mem); } - - free(events->friend_status_message); - events->friend_status_message = nullptr; - events->friend_status_message_size = 0; - events->friend_status_message_capacity = 0; + mem_delete(mem, friend_status_message); } -uint32_t tox_events_get_friend_status_message_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Status_Message *tox_events_add_friend_status_message(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Status_Message *const friend_status_message = tox_event_friend_status_message_new(mem); + + if (friend_status_message == nullptr) { + return nullptr; } - return events->friend_status_message_size; + Tox_Event event; + event.type = TOX_EVENT_FRIEND_STATUS_MESSAGE; + event.data.friend_status_message = friend_status_message; + + tox_events_add(events, &event); + return friend_status_message; } const Tox_Event_Friend_Status_Message *tox_events_get_friend_status_message(const Tox_Events *events, uint32_t index) { - assert(index < events->friend_status_message_size); - assert(events->friend_status_message != nullptr); - return &events->friend_status_message[index]; + uint32_t friend_status_message_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (friend_status_message_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_FRIEND_STATUS_MESSAGE) { + const Tox_Event_Friend_Status_Message *friend_status_message = events->events[i].data.friend_status_message; + if (friend_status_message_index == index) { + return friend_status_message; + } + ++friend_status_message_index; + } + } + + return nullptr; } -bool tox_events_pack_friend_status_message(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_friend_status_message_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_friend_status_message_size(events); + uint32_t friend_status_message_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_status_message_pack(tox_events_get_friend_status_message(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_FRIEND_STATUS_MESSAGE) { + ++friend_status_message_size; } } - return true; + + return friend_status_message_size; } -bool tox_events_unpack_friend_status_message(Tox_Events *events, Bin_Unpack *bu) +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 = tox_events_add_friend_status_message(events); + assert(event != nullptr); + *event = tox_event_friend_status_message_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_friend_status_message_unpack(event, bu); + return tox_event_friend_status_message_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Friend_Status_Message *tox_event_friend_status_message_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Friend_Status_Message *friend_status_message = tox_events_add_friend_status_message(state->events, state->mem); + + if (friend_status_message == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_status_message; } @@ -213,20 +240,12 @@ bool tox_events_unpack_friend_status_message(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_friend_status_message(Tox *tox, uint32_t friend_number, const uint8_t *message, - size_t length, void *user_data) +void tox_events_handle_friend_status_message(Tox *tox, uint32_t friend_number, const uint8_t *message, size_t length, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Status_Message *friend_status_message = tox_events_add_friend_status_message(state->events); + Tox_Event_Friend_Status_Message *friend_status_message = tox_event_friend_status_message_alloc(user_data); if (friend_status_message == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/friend_typing.c b/toxcore/events/friend_typing.c index 64e494d7413..6d48391b5f6 100644 --- a/toxcore/events/friend_typing.c +++ b/toxcore/events/friend_typing.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -26,19 +27,6 @@ struct Tox_Event_Friend_Typing { bool typing; }; -non_null() -static void tox_event_friend_typing_construct(Tox_Event_Friend_Typing *friend_typing) -{ - *friend_typing = (Tox_Event_Friend_Typing) { - 0 - }; -} -non_null() -static void tox_event_friend_typing_destruct(Tox_Event_Friend_Typing *friend_typing) -{ - return; -} - non_null() static void tox_event_friend_typing_set_friend_number(Tox_Event_Friend_Typing *friend_typing, uint32_t friend_number) @@ -53,7 +41,8 @@ uint32_t tox_event_friend_typing_get_friend_number(const Tox_Event_Friend_Typing } non_null() -static void tox_event_friend_typing_set_typing(Tox_Event_Friend_Typing *friend_typing, bool typing) +static void tox_event_friend_typing_set_typing(Tox_Event_Friend_Typing *friend_typing, + bool typing) { assert(friend_typing != nullptr); friend_typing->typing = typing; @@ -65,7 +54,19 @@ bool tox_event_friend_typing_get_typing(const Tox_Event_Friend_Typing *friend_ty } non_null() -static bool tox_event_friend_typing_pack( +static void tox_event_friend_typing_construct(Tox_Event_Friend_Typing *friend_typing) +{ + *friend_typing = (Tox_Event_Friend_Typing) { + 0 + }; +} +non_null() +static void tox_event_friend_typing_destruct(Tox_Event_Friend_Typing *friend_typing, const Memory *mem) +{ + return; +} + +bool tox_event_friend_typing_pack( const Tox_Event_Friend_Typing *event, Bin_Pack *bp) { assert(event != nullptr); @@ -77,7 +78,7 @@ static bool tox_event_friend_typing_pack( } non_null() -static bool tox_event_friend_typing_unpack( +static bool tox_event_friend_typing_unpack_into( Tox_Event_Friend_Typing *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -92,90 +93,120 @@ static bool tox_event_friend_typing_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Typing *tox_events_add_friend_typing(Tox_Events *events) +const Tox_Event_Friend_Typing *tox_event_get_friend_typing(const Tox_Event *event) { - if (events->friend_typing_size == UINT32_MAX) { - return nullptr; - } - - if (events->friend_typing_size == events->friend_typing_capacity) { - const uint32_t new_friend_typing_capacity = events->friend_typing_capacity * 2 + 1; - Tox_Event_Friend_Typing *new_friend_typing = (Tox_Event_Friend_Typing *)realloc( - events->friend_typing, new_friend_typing_capacity * sizeof(Tox_Event_Friend_Typing)); + return event->type == TOX_EVENT_FRIEND_TYPING ? event->data.friend_typing : nullptr; +} - if (new_friend_typing == nullptr) { - return nullptr; - } +Tox_Event_Friend_Typing *tox_event_friend_typing_new(const Memory *mem) +{ + Tox_Event_Friend_Typing *const friend_typing = + (Tox_Event_Friend_Typing *)mem_alloc(mem, sizeof(Tox_Event_Friend_Typing)); - events->friend_typing = new_friend_typing; - events->friend_typing_capacity = new_friend_typing_capacity; + if (friend_typing == nullptr) { + return nullptr; } - Tox_Event_Friend_Typing *const friend_typing = &events->friend_typing[events->friend_typing_size]; tox_event_friend_typing_construct(friend_typing); - ++events->friend_typing_size; return friend_typing; } -void tox_events_clear_friend_typing(Tox_Events *events) +void tox_event_friend_typing_free(Tox_Event_Friend_Typing *friend_typing, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->friend_typing_size; ++i) { - tox_event_friend_typing_destruct(&events->friend_typing[i]); + if (friend_typing != nullptr) { + tox_event_friend_typing_destruct(friend_typing, mem); } - - free(events->friend_typing); - events->friend_typing = nullptr; - events->friend_typing_size = 0; - events->friend_typing_capacity = 0; + mem_delete(mem, friend_typing); } -uint32_t tox_events_get_friend_typing_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Typing *tox_events_add_friend_typing(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Typing *const friend_typing = tox_event_friend_typing_new(mem); + + if (friend_typing == nullptr) { + return nullptr; } - return events->friend_typing_size; + Tox_Event event; + event.type = TOX_EVENT_FRIEND_TYPING; + event.data.friend_typing = friend_typing; + + tox_events_add(events, &event); + return friend_typing; } const Tox_Event_Friend_Typing *tox_events_get_friend_typing(const Tox_Events *events, uint32_t index) { - assert(index < events->friend_typing_size); - assert(events->friend_typing != nullptr); - return &events->friend_typing[index]; + uint32_t friend_typing_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (friend_typing_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_FRIEND_TYPING) { + const Tox_Event_Friend_Typing *friend_typing = events->events[i].data.friend_typing; + if (friend_typing_index == index) { + return friend_typing; + } + ++friend_typing_index; + } + } + + return nullptr; } -bool tox_events_pack_friend_typing(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_friend_typing_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_friend_typing_size(events); + uint32_t friend_typing_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_friend_typing_pack(tox_events_get_friend_typing(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_FRIEND_TYPING) { + ++friend_typing_size; } } - return true; + + return friend_typing_size; } -bool tox_events_unpack_friend_typing(Tox_Events *events, Bin_Unpack *bu) +bool tox_event_friend_typing_unpack( + Tox_Event_Friend_Typing **event, Bin_Unpack *bu, const Memory *mem) { - Tox_Event_Friend_Typing *event = tox_events_add_friend_typing(events); + assert(event != nullptr); + *event = tox_event_friend_typing_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_friend_typing_unpack(event, bu); + return tox_event_friend_typing_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Friend_Typing *tox_event_friend_typing_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Friend_Typing *friend_typing = tox_events_add_friend_typing(state->events, state->mem); + + if (friend_typing == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_typing; } @@ -186,19 +217,12 @@ bool tox_events_unpack_friend_typing(Tox_Events *events, Bin_Unpack *bu) *****************************************************/ -void tox_events_handle_friend_typing(Tox *tox, uint32_t friend_number, bool typing, void *user_data) +void tox_events_handle_friend_typing(Tox *tox, uint32_t friend_number, bool typing, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Friend_Typing *friend_typing = tox_events_add_friend_typing(state->events); + Tox_Event_Friend_Typing *friend_typing = tox_event_friend_typing_alloc(user_data); if (friend_typing == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/self_connection_status.c b/toxcore/events/self_connection_status.c index 097319e93ae..c8fdaa56fd2 100644 --- a/toxcore/events/self_connection_status.c +++ b/toxcore/events/self_connection_status.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2023 The TokTok team. */ #include "events_alloc.h" @@ -27,34 +27,32 @@ struct Tox_Event_Self_Connection_Status { }; non_null() -static void tox_event_self_connection_status_construct(Tox_Event_Self_Connection_Status *self_connection_status) +static void tox_event_self_connection_status_set_connection_status(Tox_Event_Self_Connection_Status *self_connection_status, + Tox_Connection connection_status) { - *self_connection_status = (Tox_Event_Self_Connection_Status) { - TOX_CONNECTION_NONE - }; + assert(self_connection_status != nullptr); + self_connection_status->connection_status = connection_status; } -non_null() -static void tox_event_self_connection_status_destruct(Tox_Event_Self_Connection_Status *self_connection_status) +Tox_Connection tox_event_self_connection_status_get_connection_status(const Tox_Event_Self_Connection_Status *self_connection_status) { - return; + assert(self_connection_status != nullptr); + return self_connection_status->connection_status; } non_null() -static void tox_event_self_connection_status_set_connection_status(Tox_Event_Self_Connection_Status - *self_connection_status, Tox_Connection connection_status) +static void tox_event_self_connection_status_construct(Tox_Event_Self_Connection_Status *self_connection_status) { - assert(self_connection_status != nullptr); - self_connection_status->connection_status = connection_status; + *self_connection_status = (Tox_Event_Self_Connection_Status) { + TOX_CONNECTION_NONE + }; } -Tox_Connection tox_event_self_connection_status_get_connection_status(const Tox_Event_Self_Connection_Status - *self_connection_status) +non_null() +static void tox_event_self_connection_status_destruct(Tox_Event_Self_Connection_Status *self_connection_status, const Memory *mem) { - assert(self_connection_status != nullptr); - return self_connection_status->connection_status; + return; } -non_null() -static bool tox_event_self_connection_status_pack( +bool tox_event_self_connection_status_pack( const Tox_Event_Self_Connection_Status *event, Bin_Pack *bp) { assert(event != nullptr); @@ -64,7 +62,7 @@ static bool tox_event_self_connection_status_pack( } non_null() -static bool tox_event_self_connection_status_unpack( +static bool tox_event_self_connection_status_unpack_into( Tox_Event_Self_Connection_Status *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -74,91 +72,120 @@ static bool tox_event_self_connection_status_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Self_Connection_Status *tox_events_add_self_connection_status(Tox_Events *events) +const Tox_Event_Self_Connection_Status *tox_event_get_self_connection_status(const Tox_Event *event) { - if (events->self_connection_status_size == UINT32_MAX) { - return nullptr; - } - - if (events->self_connection_status_size == events->self_connection_status_capacity) { - const uint32_t new_self_connection_status_capacity = events->self_connection_status_capacity * 2 + 1; - Tox_Event_Self_Connection_Status *new_self_connection_status = (Tox_Event_Self_Connection_Status *)realloc( - events->self_connection_status, new_self_connection_status_capacity * sizeof(Tox_Event_Self_Connection_Status)); + return event->type == TOX_EVENT_SELF_CONNECTION_STATUS ? event->data.self_connection_status : nullptr; +} - if (new_self_connection_status == nullptr) { - return nullptr; - } +Tox_Event_Self_Connection_Status *tox_event_self_connection_status_new(const Memory *mem) +{ + Tox_Event_Self_Connection_Status *const self_connection_status = + (Tox_Event_Self_Connection_Status *)mem_alloc(mem, sizeof(Tox_Event_Self_Connection_Status)); - events->self_connection_status = new_self_connection_status; - events->self_connection_status_capacity = new_self_connection_status_capacity; + if (self_connection_status == nullptr) { + return nullptr; } - Tox_Event_Self_Connection_Status *const self_connection_status = - &events->self_connection_status[events->self_connection_status_size]; tox_event_self_connection_status_construct(self_connection_status); - ++events->self_connection_status_size; return self_connection_status; } -void tox_events_clear_self_connection_status(Tox_Events *events) +void tox_event_self_connection_status_free(Tox_Event_Self_Connection_Status *self_connection_status, const Memory *mem) { - if (events == nullptr) { - return; - } - - for (uint32_t i = 0; i < events->self_connection_status_size; ++i) { - tox_event_self_connection_status_destruct(&events->self_connection_status[i]); + if (self_connection_status != nullptr) { + tox_event_self_connection_status_destruct(self_connection_status, mem); } - - free(events->self_connection_status); - events->self_connection_status = nullptr; - events->self_connection_status_size = 0; - events->self_connection_status_capacity = 0; + mem_delete(mem, self_connection_status); } -uint32_t tox_events_get_self_connection_status_size(const Tox_Events *events) +non_null() +static Tox_Event_Self_Connection_Status *tox_events_add_self_connection_status(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Self_Connection_Status *const self_connection_status = tox_event_self_connection_status_new(mem); + + if (self_connection_status == nullptr) { + return nullptr; } - return events->self_connection_status_size; + Tox_Event event; + event.type = TOX_EVENT_SELF_CONNECTION_STATUS; + event.data.self_connection_status = self_connection_status; + + tox_events_add(events, &event); + return self_connection_status; } const Tox_Event_Self_Connection_Status *tox_events_get_self_connection_status(const Tox_Events *events, uint32_t index) { - assert(index < events->self_connection_status_size); - assert(events->self_connection_status != nullptr); - return &events->self_connection_status[index]; + uint32_t self_connection_status_index = 0; + const uint32_t size = tox_events_get_size(events); + + for (uint32_t i = 0; i < size; ++i) { + if (self_connection_status_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_SELF_CONNECTION_STATUS) { + const Tox_Event_Self_Connection_Status *self_connection_status = events->events[i].data.self_connection_status; + if (self_connection_status_index == index) { + return self_connection_status; + } + ++self_connection_status_index; + } + } + + return nullptr; } -bool tox_events_pack_self_connection_status(const Tox_Events *events, Bin_Pack *bp) +uint32_t tox_events_get_self_connection_status_size(const Tox_Events *events) { - const uint32_t size = tox_events_get_self_connection_status_size(events); + uint32_t self_connection_status_size = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_self_connection_status_pack(tox_events_get_self_connection_status(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_SELF_CONNECTION_STATUS) { + ++self_connection_status_size; } } - return true; + + return self_connection_status_size; } -bool tox_events_unpack_self_connection_status(Tox_Events *events, Bin_Unpack *bu) +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 = tox_events_add_self_connection_status(events); + assert(event != nullptr); + *event = tox_event_self_connection_status_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_self_connection_status_unpack(event, bu); + return tox_event_self_connection_status_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Self_Connection_Status *tox_event_self_connection_status_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Self_Connection_Status *self_connection_status = tox_events_add_self_connection_status(state->events, state->mem); + + if (self_connection_status == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return self_connection_status; } @@ -169,19 +196,12 @@ bool tox_events_unpack_self_connection_status(Tox_Events *events, Bin_Unpack *bu *****************************************************/ -void tox_events_handle_self_connection_status(Tox *tox, Tox_Connection connection_status, void *user_data) +void tox_events_handle_self_connection_status(Tox *tox, Tox_Connection connection_status, + void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Self_Connection_Status *self_connection_status = tox_events_add_self_connection_status(state->events); + Tox_Event_Self_Connection_Status *self_connection_status = tox_event_self_connection_status_alloc(user_data); if (self_connection_status == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/tox_dispatch.c b/toxcore/tox_dispatch.c index 25dafdcb36b..218e506d536 100644 --- a/toxcore/tox_dispatch.c +++ b/toxcore/tox_dispatch.c @@ -7,8 +7,10 @@ #include #include "ccompat.h" -#include "tox.h" +#include "events/events_alloc.h" +#include "tox_event.h" #include "tox_events.h" +#include "tox.h" struct Tox_Dispatch { tox_events_conference_connected_cb *conference_connected_callback; @@ -168,321 +170,189 @@ void tox_events_callback_self_connection_status( dispatch->self_connection_status_callback = callback; } -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_connected( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) +non_null(1, 2, 3) nullable(4) +static void tox_dispatch_invoke_event(const Tox_Dispatch *dispatch, const Tox_Event *event, Tox *tox, void *user_data) { - const uint32_t size = tox_events_get_conference_connected_size(events); + switch (event->type) { + case TOX_EVENT_CONFERENCE_CONNECTED: { + if (dispatch->conference_connected_callback != nullptr) { + dispatch->conference_connected_callback(tox, event->data.conference_connected, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_connected_callback != nullptr) { - dispatch->conference_connected_callback( - tox, tox_events_get_conference_connected(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_invite( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_conference_invite_size(events); + case TOX_EVENT_CONFERENCE_INVITE: { + if (dispatch->conference_invite_callback != nullptr) { + dispatch->conference_invite_callback(tox, event->data.conference_invite, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_invite_callback != nullptr) { - dispatch->conference_invite_callback( - tox, tox_events_get_conference_invite(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_message( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_conference_message_size(events); + case TOX_EVENT_CONFERENCE_MESSAGE: { + if (dispatch->conference_message_callback != nullptr) { + dispatch->conference_message_callback(tox, event->data.conference_message, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_message_callback != nullptr) { - dispatch->conference_message_callback( - tox, tox_events_get_conference_message(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_peer_list_changed( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_conference_peer_list_changed_size(events); + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: { + if (dispatch->conference_peer_list_changed_callback != nullptr) { + dispatch->conference_peer_list_changed_callback(tox, event->data.conference_peer_list_changed, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_peer_list_changed_callback != nullptr) { - dispatch->conference_peer_list_changed_callback( - tox, tox_events_get_conference_peer_list_changed(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_peer_name( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_conference_peer_name_size(events); + case TOX_EVENT_CONFERENCE_PEER_NAME: { + if (dispatch->conference_peer_name_callback != nullptr) { + dispatch->conference_peer_name_callback(tox, event->data.conference_peer_name, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_peer_name_callback != nullptr) { - dispatch->conference_peer_name_callback( - tox, tox_events_get_conference_peer_name(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_conference_title( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_conference_title_size(events); + case TOX_EVENT_CONFERENCE_TITLE: { + if (dispatch->conference_title_callback != nullptr) { + dispatch->conference_title_callback(tox, event->data.conference_title, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->conference_title_callback != nullptr) { - dispatch->conference_title_callback( - tox, tox_events_get_conference_title(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_file_chunk_request( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_file_chunk_request_size(events); + case TOX_EVENT_FILE_CHUNK_REQUEST: { + if (dispatch->file_chunk_request_callback != nullptr) { + dispatch->file_chunk_request_callback(tox, event->data.file_chunk_request, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->file_chunk_request_callback != nullptr) { - dispatch->file_chunk_request_callback( - tox, tox_events_get_file_chunk_request(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_file_recv( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_file_recv_size(events); + case TOX_EVENT_FILE_RECV_CHUNK: { + if (dispatch->file_recv_chunk_callback != nullptr) { + dispatch->file_recv_chunk_callback(tox, event->data.file_recv_chunk, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->file_recv_callback != nullptr) { - dispatch->file_recv_callback( - tox, tox_events_get_file_recv(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_file_recv_chunk( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_file_recv_chunk_size(events); + case TOX_EVENT_FILE_RECV_CONTROL: { + if (dispatch->file_recv_control_callback != nullptr) { + dispatch->file_recv_control_callback(tox, event->data.file_recv_control, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->file_recv_chunk_callback != nullptr) { - dispatch->file_recv_chunk_callback( - tox, tox_events_get_file_recv_chunk(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_file_recv_control( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_file_recv_control_size(events); + case TOX_EVENT_FILE_RECV: { + if (dispatch->file_recv_callback != nullptr) { + dispatch->file_recv_callback(tox, event->data.file_recv, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->file_recv_control_callback != nullptr) { - dispatch->file_recv_control_callback( - tox, tox_events_get_file_recv_control(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_connection_status( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_connection_status_size(events); + case TOX_EVENT_FRIEND_CONNECTION_STATUS: { + if (dispatch->friend_connection_status_callback != nullptr) { + dispatch->friend_connection_status_callback(tox, event->data.friend_connection_status, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_connection_status_callback != nullptr) { - dispatch->friend_connection_status_callback( - tox, tox_events_get_friend_connection_status(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_lossless_packet( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_lossless_packet_size(events); + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: { + if (dispatch->friend_lossless_packet_callback != nullptr) { + dispatch->friend_lossless_packet_callback(tox, event->data.friend_lossless_packet, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_lossless_packet_callback != nullptr) { - dispatch->friend_lossless_packet_callback( - tox, tox_events_get_friend_lossless_packet(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_lossy_packet( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_lossy_packet_size(events); + case TOX_EVENT_FRIEND_LOSSY_PACKET: { + if (dispatch->friend_lossy_packet_callback != nullptr) { + dispatch->friend_lossy_packet_callback(tox, event->data.friend_lossy_packet, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_lossy_packet_callback != nullptr) { - dispatch->friend_lossy_packet_callback( - tox, tox_events_get_friend_lossy_packet(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_message( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_message_size(events); + case TOX_EVENT_FRIEND_MESSAGE: { + if (dispatch->friend_message_callback != nullptr) { + dispatch->friend_message_callback(tox, event->data.friend_message, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_message_callback != nullptr) { - dispatch->friend_message_callback( - tox, tox_events_get_friend_message(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_name( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_name_size(events); + case TOX_EVENT_FRIEND_NAME: { + if (dispatch->friend_name_callback != nullptr) { + dispatch->friend_name_callback(tox, event->data.friend_name, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_name_callback != nullptr) { - dispatch->friend_name_callback( - tox, tox_events_get_friend_name(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_read_receipt( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_read_receipt_size(events); + case TOX_EVENT_FRIEND_READ_RECEIPT: { + if (dispatch->friend_read_receipt_callback != nullptr) { + dispatch->friend_read_receipt_callback(tox, event->data.friend_read_receipt, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_read_receipt_callback != nullptr) { - dispatch->friend_read_receipt_callback( - tox, tox_events_get_friend_read_receipt(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_request( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_request_size(events); + case TOX_EVENT_FRIEND_REQUEST: { + if (dispatch->friend_request_callback != nullptr) { + dispatch->friend_request_callback(tox, event->data.friend_request, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_request_callback != nullptr) { - dispatch->friend_request_callback( - tox, tox_events_get_friend_request(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_status( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_status_size(events); + case TOX_EVENT_FRIEND_STATUS: { + if (dispatch->friend_status_callback != nullptr) { + dispatch->friend_status_callback(tox, event->data.friend_status, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_status_callback != nullptr) { - dispatch->friend_status_callback( - tox, tox_events_get_friend_status(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_status_message( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_status_message_size(events); + case TOX_EVENT_FRIEND_STATUS_MESSAGE: { + if (dispatch->friend_status_message_callback != nullptr) { + dispatch->friend_status_message_callback(tox, event->data.friend_status_message, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_status_message_callback != nullptr) { - dispatch->friend_status_message_callback( - tox, tox_events_get_friend_status_message(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_friend_typing( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_friend_typing_size(events); + case TOX_EVENT_FRIEND_TYPING: { + if (dispatch->friend_typing_callback != nullptr) { + dispatch->friend_typing_callback(tox, event->data.friend_typing, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->friend_typing_callback != nullptr) { - dispatch->friend_typing_callback( - tox, tox_events_get_friend_typing(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_self_connection_status( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_self_connection_status_size(events); + case TOX_EVENT_SELF_CONNECTION_STATUS: { + if (dispatch->self_connection_status_callback != nullptr) { + dispatch->self_connection_status_callback(tox, event->data.self_connection_status, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->self_connection_status_callback != nullptr) { - dispatch->self_connection_status_callback( - tox, tox_events_get_self_connection_status(events, i), user_data); + break; + } + + case TOX_EVENT_INVALID: { + break; } } } void tox_dispatch_invoke(const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) { - tox_dispatch_invoke_conference_connected(dispatch, events, tox, user_data); - tox_dispatch_invoke_conference_invite(dispatch, events, tox, user_data); - tox_dispatch_invoke_conference_message(dispatch, events, tox, user_data); - tox_dispatch_invoke_conference_peer_list_changed(dispatch, events, tox, user_data); - tox_dispatch_invoke_conference_peer_name(dispatch, events, tox, user_data); - tox_dispatch_invoke_conference_title(dispatch, events, tox, user_data); - tox_dispatch_invoke_file_chunk_request(dispatch, events, tox, user_data); - tox_dispatch_invoke_file_recv(dispatch, events, tox, user_data); - tox_dispatch_invoke_file_recv_chunk(dispatch, events, tox, user_data); - tox_dispatch_invoke_file_recv_control(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_connection_status(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_lossless_packet(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_lossy_packet(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_message(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_name(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_read_receipt(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_request(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_status(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_status_message(dispatch, events, tox, user_data); - tox_dispatch_invoke_friend_typing(dispatch, events, tox, user_data); - tox_dispatch_invoke_self_connection_status(dispatch, events, tox, user_data); + const uint32_t size = tox_events_get_size(events); + for (uint32_t i = 0; i < size; ++i) { + const Tox_Event *event = &events->events[i]; + tox_dispatch_invoke_event(dispatch, event, tox, user_data); + } } diff --git a/toxcore/tox_event.c b/toxcore/tox_event.c new file mode 100644 index 00000000000..feda8e028ad --- /dev/null +++ b/toxcore/tox_event.c @@ -0,0 +1,625 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022 The TokTok team. + */ + +#include "tox_event.h" + +#include +#include +#include +#include + +#include "bin_unpack.h" +#include "ccompat.h" +#include "events/events_alloc.h" +#include "tox.h" + +const char *tox_event_type_to_string(Tox_Event_Type type) +{ + switch (type) { + case TOX_EVENT_SELF_CONNECTION_STATUS: + return "TOX_EVENT_SELF_CONNECTION_STATUS"; + + case TOX_EVENT_FRIEND_REQUEST: + return "TOX_EVENT_FRIEND_REQUEST"; + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: + return "TOX_EVENT_FRIEND_CONNECTION_STATUS"; + + case TOX_EVENT_FRIEND_LOSSY_PACKET: + return "TOX_EVENT_FRIEND_LOSSY_PACKET"; + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: + return "TOX_EVENT_FRIEND_LOSSLESS_PACKET"; + + case TOX_EVENT_FRIEND_NAME: + return "TOX_EVENT_FRIEND_NAME"; + + case TOX_EVENT_FRIEND_STATUS: + return "TOX_EVENT_FRIEND_STATUS"; + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: + return "TOX_EVENT_FRIEND_STATUS_MESSAGE"; + + case TOX_EVENT_FRIEND_MESSAGE: + return "TOX_EVENT_FRIEND_MESSAGE"; + + case TOX_EVENT_FRIEND_READ_RECEIPT: + return "TOX_EVENT_FRIEND_READ_RECEIPT"; + + case TOX_EVENT_FRIEND_TYPING: + return "TOX_EVENT_FRIEND_TYPING"; + + case TOX_EVENT_FILE_CHUNK_REQUEST: + return "TOX_EVENT_FILE_CHUNK_REQUEST"; + + case TOX_EVENT_FILE_RECV: + return "TOX_EVENT_FILE_RECV"; + + case TOX_EVENT_FILE_RECV_CHUNK: + return "TOX_EVENT_FILE_RECV_CHUNK"; + + case TOX_EVENT_FILE_RECV_CONTROL: + return "TOX_EVENT_FILE_RECV_CONTROL"; + + case TOX_EVENT_CONFERENCE_INVITE: + return "TOX_EVENT_CONFERENCE_INVITE"; + + case TOX_EVENT_CONFERENCE_CONNECTED: + return "TOX_EVENT_CONFERENCE_CONNECTED"; + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: + return "TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED"; + + case TOX_EVENT_CONFERENCE_PEER_NAME: + return "TOX_EVENT_CONFERENCE_PEER_NAME"; + + case TOX_EVENT_CONFERENCE_TITLE: + return "TOX_EVENT_CONFERENCE_TITLE"; + + case TOX_EVENT_CONFERENCE_MESSAGE: + return "TOX_EVENT_CONFERENCE_MESSAGE"; + + case TOX_EVENT_INVALID: + return "TOX_EVENT_INVALID"; + } + + return ""; +} + +Tox_Event_Type tox_event_get_type(const Tox_Event *event) +{ + assert(event != nullptr); + return event->type; +} + +bool tox_event_construct(Tox_Event *event, Tox_Event_Type type, const Memory *mem) +{ + event->type = type; + event->data.value = nullptr; + + switch (type) { + case TOX_EVENT_CONFERENCE_CONNECTED: { + event->data.conference_connected = tox_event_conference_connected_new(mem); + break; + } + + case TOX_EVENT_CONFERENCE_INVITE: { + event->data.conference_invite = tox_event_conference_invite_new(mem); + break; + } + + case TOX_EVENT_CONFERENCE_MESSAGE: { + event->data.conference_message = tox_event_conference_message_new(mem); + break; + } + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: { + event->data.conference_peer_list_changed = tox_event_conference_peer_list_changed_new(mem); + break; + } + + case TOX_EVENT_CONFERENCE_PEER_NAME: { + event->data.conference_peer_name = tox_event_conference_peer_name_new(mem); + break; + } + + case TOX_EVENT_CONFERENCE_TITLE: { + event->data.conference_title = tox_event_conference_title_new(mem); + break; + } + + case TOX_EVENT_FILE_CHUNK_REQUEST: { + event->data.file_chunk_request = tox_event_file_chunk_request_new(mem); + break; + } + + case TOX_EVENT_FILE_RECV_CHUNK: { + event->data.file_recv_chunk = tox_event_file_recv_chunk_new(mem); + break; + } + + case TOX_EVENT_FILE_RECV_CONTROL: { + event->data.file_recv_control = tox_event_file_recv_control_new(mem); + break; + } + + case TOX_EVENT_FILE_RECV: { + event->data.file_recv = tox_event_file_recv_new(mem); + break; + } + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: { + event->data.friend_connection_status = tox_event_friend_connection_status_new(mem); + break; + } + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: { + event->data.friend_lossless_packet = tox_event_friend_lossless_packet_new(mem); + break; + } + + case TOX_EVENT_FRIEND_LOSSY_PACKET: { + event->data.friend_lossy_packet = tox_event_friend_lossy_packet_new(mem); + break; + } + + case TOX_EVENT_FRIEND_MESSAGE: { + event->data.friend_message = tox_event_friend_message_new(mem); + break; + } + + case TOX_EVENT_FRIEND_NAME: { + event->data.friend_name = tox_event_friend_name_new(mem); + break; + } + + case TOX_EVENT_FRIEND_READ_RECEIPT: { + event->data.friend_read_receipt = tox_event_friend_read_receipt_new(mem); + break; + } + + case TOX_EVENT_FRIEND_REQUEST: { + event->data.friend_request = tox_event_friend_request_new(mem); + break; + } + + case TOX_EVENT_FRIEND_STATUS: { + event->data.friend_status = tox_event_friend_status_new(mem); + break; + } + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: { + event->data.friend_status_message = tox_event_friend_status_message_new(mem); + break; + } + + case TOX_EVENT_FRIEND_TYPING: { + event->data.friend_typing = tox_event_friend_typing_new(mem); + break; + } + + case TOX_EVENT_SELF_CONNECTION_STATUS: { + event->data.self_connection_status = tox_event_self_connection_status_new(mem); + break; + } + + case TOX_EVENT_INVALID: + return false; + } + + return event->data.value != nullptr; +} + +void tox_event_destruct(Tox_Event *event, const Memory *mem) +{ + if (event == nullptr) { + return; + } + + switch (event->type) { + case TOX_EVENT_CONFERENCE_CONNECTED: { + tox_event_conference_connected_free(event->data.conference_connected, mem); + break; + } + + case TOX_EVENT_CONFERENCE_INVITE: { + tox_event_conference_invite_free(event->data.conference_invite, mem); + break; + } + + case TOX_EVENT_CONFERENCE_MESSAGE: { + tox_event_conference_message_free(event->data.conference_message, mem); + break; + } + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: { + tox_event_conference_peer_list_changed_free(event->data.conference_peer_list_changed, mem); + break; + } + + case TOX_EVENT_CONFERENCE_PEER_NAME: { + tox_event_conference_peer_name_free(event->data.conference_peer_name, mem); + break; + } + + case TOX_EVENT_CONFERENCE_TITLE: { + tox_event_conference_title_free(event->data.conference_title, mem); + break; + } + + case TOX_EVENT_FILE_CHUNK_REQUEST: { + tox_event_file_chunk_request_free(event->data.file_chunk_request, mem); + break; + } + + case TOX_EVENT_FILE_RECV_CHUNK: { + tox_event_file_recv_chunk_free(event->data.file_recv_chunk, mem); + break; + } + + case TOX_EVENT_FILE_RECV_CONTROL: { + tox_event_file_recv_control_free(event->data.file_recv_control, mem); + break; + } + + case TOX_EVENT_FILE_RECV: { + tox_event_file_recv_free(event->data.file_recv, mem); + break; + } + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: { + tox_event_friend_connection_status_free(event->data.friend_connection_status, mem); + break; + } + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: { + tox_event_friend_lossless_packet_free(event->data.friend_lossless_packet, mem); + break; + } + + case TOX_EVENT_FRIEND_LOSSY_PACKET: { + tox_event_friend_lossy_packet_free(event->data.friend_lossy_packet, mem); + break; + } + + case TOX_EVENT_FRIEND_MESSAGE: { + tox_event_friend_message_free(event->data.friend_message, mem); + break; + } + + case TOX_EVENT_FRIEND_NAME: { + tox_event_friend_name_free(event->data.friend_name, mem); + break; + } + + case TOX_EVENT_FRIEND_READ_RECEIPT: { + tox_event_friend_read_receipt_free(event->data.friend_read_receipt, mem); + break; + } + + case TOX_EVENT_FRIEND_REQUEST: { + tox_event_friend_request_free(event->data.friend_request, mem); + break; + } + + case TOX_EVENT_FRIEND_STATUS: { + tox_event_friend_status_free(event->data.friend_status, mem); + break; + } + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: { + tox_event_friend_status_message_free(event->data.friend_status_message, mem); + break; + } + + case TOX_EVENT_FRIEND_TYPING: { + tox_event_friend_typing_free(event->data.friend_typing, mem); + break; + } + + case TOX_EVENT_SELF_CONNECTION_STATUS: { + tox_event_self_connection_status_free(event->data.self_connection_status, mem); + break; + } + + case TOX_EVENT_INVALID: { + break; + } + } + + event->data.value = nullptr; +} + +bool tox_event_pack(const Tox_Event *event, Bin_Pack *bp) +{ + assert(event->type != TOX_EVENT_INVALID); + + switch (event->type) { + case TOX_EVENT_CONFERENCE_CONNECTED: + return tox_event_conference_connected_pack(event->data.conference_connected, bp); + + case TOX_EVENT_CONFERENCE_INVITE: + return tox_event_conference_invite_pack(event->data.conference_invite, bp); + + case TOX_EVENT_CONFERENCE_MESSAGE: + return tox_event_conference_message_pack(event->data.conference_message, bp); + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: + return tox_event_conference_peer_list_changed_pack(event->data.conference_peer_list_changed, bp); + + case TOX_EVENT_CONFERENCE_PEER_NAME: + return tox_event_conference_peer_name_pack(event->data.conference_peer_name, bp); + + case TOX_EVENT_CONFERENCE_TITLE: + return tox_event_conference_title_pack(event->data.conference_title, bp); + + case TOX_EVENT_FILE_CHUNK_REQUEST: + return tox_event_file_chunk_request_pack(event->data.file_chunk_request, bp); + + case TOX_EVENT_FILE_RECV_CHUNK: + return tox_event_file_recv_chunk_pack(event->data.file_recv_chunk, bp); + + case TOX_EVENT_FILE_RECV_CONTROL: + return tox_event_file_recv_control_pack(event->data.file_recv_control, bp); + + case TOX_EVENT_FILE_RECV: + return tox_event_file_recv_pack(event->data.file_recv, bp); + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: + return tox_event_friend_connection_status_pack(event->data.friend_connection_status, bp); + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: + return tox_event_friend_lossless_packet_pack(event->data.friend_lossless_packet, bp); + + case TOX_EVENT_FRIEND_LOSSY_PACKET: + return tox_event_friend_lossy_packet_pack(event->data.friend_lossy_packet, bp); + + case TOX_EVENT_FRIEND_MESSAGE: + return tox_event_friend_message_pack(event->data.friend_message, bp); + + case TOX_EVENT_FRIEND_NAME: + return tox_event_friend_name_pack(event->data.friend_name, bp); + + case TOX_EVENT_FRIEND_READ_RECEIPT: + return tox_event_friend_read_receipt_pack(event->data.friend_read_receipt, bp); + + case TOX_EVENT_FRIEND_REQUEST: + return tox_event_friend_request_pack(event->data.friend_request, bp); + + case TOX_EVENT_FRIEND_STATUS: + return tox_event_friend_status_pack(event->data.friend_status, bp); + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: + return tox_event_friend_status_message_pack(event->data.friend_status_message, bp); + + case TOX_EVENT_FRIEND_TYPING: + return tox_event_friend_typing_pack(event->data.friend_typing, bp); + + case TOX_EVENT_SELF_CONNECTION_STATUS: + return tox_event_self_connection_status_pack(event->data.self_connection_status, bp); + + case TOX_EVENT_INVALID: + return false; + } + + return false; +} + +non_null() +static bool tox_event_type_from_int(uint32_t value, Tox_Event_Type *out) +{ + switch (value) { + case TOX_EVENT_SELF_CONNECTION_STATUS: { + *out = TOX_EVENT_SELF_CONNECTION_STATUS; + return true; + } + + case TOX_EVENT_FRIEND_REQUEST: { + *out = TOX_EVENT_FRIEND_REQUEST; + return true; + } + + case TOX_EVENT_FRIEND_CONNECTION_STATUS: { + *out = TOX_EVENT_FRIEND_CONNECTION_STATUS; + return true; + } + + case TOX_EVENT_FRIEND_LOSSY_PACKET: { + *out = TOX_EVENT_FRIEND_LOSSY_PACKET; + return true; + } + + case TOX_EVENT_FRIEND_LOSSLESS_PACKET: { + *out = TOX_EVENT_FRIEND_LOSSLESS_PACKET; + return true; + } + + case TOX_EVENT_FRIEND_NAME: { + *out = TOX_EVENT_FRIEND_NAME; + return true; + } + + case TOX_EVENT_FRIEND_STATUS: { + *out = TOX_EVENT_FRIEND_STATUS; + return true; + } + + case TOX_EVENT_FRIEND_STATUS_MESSAGE: { + *out = TOX_EVENT_FRIEND_STATUS_MESSAGE; + return true; + } + + case TOX_EVENT_FRIEND_MESSAGE: { + *out = TOX_EVENT_FRIEND_MESSAGE; + return true; + } + + case TOX_EVENT_FRIEND_READ_RECEIPT: { + *out = TOX_EVENT_FRIEND_READ_RECEIPT; + return true; + } + + case TOX_EVENT_FRIEND_TYPING: { + *out = TOX_EVENT_FRIEND_TYPING; + return true; + } + + case TOX_EVENT_FILE_CHUNK_REQUEST: { + *out = TOX_EVENT_FILE_CHUNK_REQUEST; + return true; + } + + case TOX_EVENT_FILE_RECV: { + *out = TOX_EVENT_FILE_RECV; + return true; + } + + case TOX_EVENT_FILE_RECV_CHUNK: { + *out = TOX_EVENT_FILE_RECV_CHUNK; + return true; + } + + case TOX_EVENT_FILE_RECV_CONTROL: { + *out = TOX_EVENT_FILE_RECV_CONTROL; + return true; + } + + case TOX_EVENT_CONFERENCE_INVITE: { + *out = TOX_EVENT_CONFERENCE_INVITE; + return true; + } + + case TOX_EVENT_CONFERENCE_CONNECTED: { + *out = TOX_EVENT_CONFERENCE_CONNECTED; + return true; + } + + case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: { + *out = TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED; + return true; + } + + case TOX_EVENT_CONFERENCE_PEER_NAME: { + *out = TOX_EVENT_CONFERENCE_PEER_NAME; + return true; + } + + case TOX_EVENT_CONFERENCE_TITLE: { + *out = TOX_EVENT_CONFERENCE_TITLE; + return true; + } + + case TOX_EVENT_CONFERENCE_MESSAGE: { + *out = TOX_EVENT_CONFERENCE_MESSAGE; + return true; + } + + case TOX_EVENT_INVALID: { + *out = TOX_EVENT_INVALID; + return true; + } + + default: { + *out = TOX_EVENT_INVALID; + return false; + } + } +} + +non_null() +static bool tox_event_type_unpack(Bin_Unpack *bu, Tox_Event_Type *val) +{ + uint32_t u32; + return bin_unpack_u32(bu, &u32) + && tox_event_type_from_int(u32, val); +} + +bool tox_event_unpack_into(Tox_Event *event, Bin_Unpack *bu, const Memory *mem) +{ + uint32_t size; + if (!bin_unpack_array(bu, &size)) { + return false; + } + + if (size != 2) { + return false; + } + + Tox_Event_Type type; + if (!tox_event_type_unpack(bu, &type)) { + return false; + } + + event->type = type; + + switch (type) { + 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_INVALID: + return false; + } + + return false; +} diff --git a/toxcore/tox_event.h b/toxcore/tox_event.h new file mode 100644 index 00000000000..fcf0fa3a41c --- /dev/null +++ b/toxcore/tox_event.h @@ -0,0 +1,164 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022 The TokTok team. + */ + +#ifndef C_TOXCORE_TOXCORE_TOX_EVENT_H +#define C_TOXCORE_TOXCORE_TOX_EVENT_H + +#include "attributes.h" +#include "bin_pack.h" +#include "bin_unpack.h" +#include "mem.h" +#include "tox_events.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef union Tox_Event_Data { + /** + * Opaque pointer just to check whether any value is set. + */ + void *value; + + Tox_Event_Conference_Connected *conference_connected; + Tox_Event_Conference_Invite *conference_invite; + Tox_Event_Conference_Message *conference_message; + Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed; + Tox_Event_Conference_Peer_Name *conference_peer_name; + Tox_Event_Conference_Title *conference_title; + Tox_Event_File_Chunk_Request *file_chunk_request; + Tox_Event_File_Recv *file_recv; + Tox_Event_File_Recv_Chunk *file_recv_chunk; + Tox_Event_File_Recv_Control *file_recv_control; + Tox_Event_Friend_Connection_Status *friend_connection_status; + Tox_Event_Friend_Lossless_Packet *friend_lossless_packet; + Tox_Event_Friend_Lossy_Packet *friend_lossy_packet; + Tox_Event_Friend_Message *friend_message; + Tox_Event_Friend_Name *friend_name; + Tox_Event_Friend_Read_Receipt *friend_read_receipt; + Tox_Event_Friend_Request *friend_request; + Tox_Event_Friend_Status *friend_status; + Tox_Event_Friend_Status_Message *friend_status_message; + Tox_Event_Friend_Typing *friend_typing; + Tox_Event_Self_Connection_Status *self_connection_status; +} Tox_Event_Data; + +struct Tox_Event { + Tox_Event_Type type; + Tox_Event_Data data; +}; + +/** + * Constructor. + */ +non_null() bool tox_event_construct(Tox_Event *event, Tox_Event_Type type, const Memory *mem); + +non_null() Tox_Event_Conference_Connected *tox_event_conference_connected_new(const Memory *mem); +non_null() Tox_Event_Conference_Invite *tox_event_conference_invite_new(const Memory *mem); +non_null() Tox_Event_Conference_Message *tox_event_conference_message_new(const Memory *mem); +non_null() Tox_Event_Conference_Peer_List_Changed *tox_event_conference_peer_list_changed_new(const Memory *mem); +non_null() Tox_Event_Conference_Peer_Name *tox_event_conference_peer_name_new(const Memory *mem); +non_null() Tox_Event_Conference_Title *tox_event_conference_title_new(const Memory *mem); +non_null() Tox_Event_File_Chunk_Request *tox_event_file_chunk_request_new(const Memory *mem); +non_null() Tox_Event_File_Recv_Chunk *tox_event_file_recv_chunk_new(const Memory *mem); +non_null() Tox_Event_File_Recv_Control *tox_event_file_recv_control_new(const Memory *mem); +non_null() Tox_Event_File_Recv *tox_event_file_recv_new(const Memory *mem); +non_null() Tox_Event_Friend_Connection_Status *tox_event_friend_connection_status_new(const Memory *mem); +non_null() Tox_Event_Friend_Lossless_Packet *tox_event_friend_lossless_packet_new(const Memory *mem); +non_null() Tox_Event_Friend_Lossy_Packet *tox_event_friend_lossy_packet_new(const Memory *mem); +non_null() Tox_Event_Friend_Message *tox_event_friend_message_new(const Memory *mem); +non_null() Tox_Event_Friend_Name *tox_event_friend_name_new(const Memory *mem); +non_null() Tox_Event_Friend_Read_Receipt *tox_event_friend_read_receipt_new(const Memory *mem); +non_null() Tox_Event_Friend_Request *tox_event_friend_request_new(const Memory *mem); +non_null() Tox_Event_Friend_Status_Message *tox_event_friend_status_message_new(const Memory *mem); +non_null() Tox_Event_Friend_Status *tox_event_friend_status_new(const Memory *mem); +non_null() Tox_Event_Friend_Typing *tox_event_friend_typing_new(const Memory *mem); +non_null() Tox_Event_Self_Connection_Status *tox_event_self_connection_status_new(const Memory *mem); + +/** + * Destructor. + */ +non_null(2) nullable(1) void tox_event_destruct(Tox_Event *event, const Memory *mem); + +non_null(2) nullable(1) void tox_event_conference_connected_free(Tox_Event_Conference_Connected *conference_connected, const Memory *mem); +non_null(2) nullable(1) void tox_event_conference_invite_free(Tox_Event_Conference_Invite *conference_invite, const Memory *mem); +non_null(2) nullable(1) void tox_event_conference_message_free(Tox_Event_Conference_Message *conference_message, const Memory *mem); +non_null(2) nullable(1) void tox_event_conference_peer_list_changed_free(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed, const Memory *mem); +non_null(2) nullable(1) void tox_event_conference_peer_name_free(Tox_Event_Conference_Peer_Name *conference_peer_name, const Memory *mem); +non_null(2) nullable(1) void tox_event_conference_title_free(Tox_Event_Conference_Title *conference_title, const Memory *mem); +non_null(2) nullable(1) void tox_event_file_chunk_request_free(Tox_Event_File_Chunk_Request *file_chunk_request, const Memory *mem); +non_null(2) nullable(1) void tox_event_file_recv_chunk_free(Tox_Event_File_Recv_Chunk *file_recv_chunk, const Memory *mem); +non_null(2) nullable(1) void tox_event_file_recv_control_free(Tox_Event_File_Recv_Control *file_recv_control, const Memory *mem); +non_null(2) nullable(1) void tox_event_file_recv_free(Tox_Event_File_Recv *file_recv, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_connection_status_free(Tox_Event_Friend_Connection_Status *friend_connection_status, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_lossless_packet_free(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_lossy_packet_free(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_message_free(Tox_Event_Friend_Message *friend_message, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_name_free(Tox_Event_Friend_Name *friend_name, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_read_receipt_free(Tox_Event_Friend_Read_Receipt *friend_read_receipt, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_request_free(Tox_Event_Friend_Request *friend_request, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_status_message_free(Tox_Event_Friend_Status_Message *friend_status_message, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_status_free(Tox_Event_Friend_Status *friend_status, const Memory *mem); +non_null(2) nullable(1) void tox_event_friend_typing_free(Tox_Event_Friend_Typing *friend_typing, const Memory *mem); +non_null(2) nullable(1) void tox_event_self_connection_status_free(Tox_Event_Self_Connection_Status *self_connection_status, const Memory *mem); + +/** + * Pack into msgpack. + */ +non_null() bool tox_event_pack(const Tox_Event *event, Bin_Pack *bp); + +non_null() bool tox_event_conference_connected_pack(const Tox_Event_Conference_Connected *event, Bin_Pack *bp); +non_null() bool tox_event_conference_invite_pack(const Tox_Event_Conference_Invite *event, Bin_Pack *bp); +non_null() bool tox_event_conference_message_pack(const Tox_Event_Conference_Message *event, Bin_Pack *bp); +non_null() bool tox_event_conference_peer_list_changed_pack(const Tox_Event_Conference_Peer_List_Changed *event, Bin_Pack *bp); +non_null() bool tox_event_conference_peer_name_pack(const Tox_Event_Conference_Peer_Name *event, Bin_Pack *bp); +non_null() bool tox_event_conference_title_pack(const Tox_Event_Conference_Title *event, Bin_Pack *bp); +non_null() bool tox_event_file_chunk_request_pack(const Tox_Event_File_Chunk_Request *event, Bin_Pack *bp); +non_null() bool tox_event_file_recv_chunk_pack(const Tox_Event_File_Recv_Chunk *event, Bin_Pack *bp); +non_null() bool tox_event_file_recv_control_pack(const Tox_Event_File_Recv_Control *event, Bin_Pack *bp); +non_null() bool tox_event_file_recv_pack(const Tox_Event_File_Recv *event, Bin_Pack *bp); +non_null() bool tox_event_friend_connection_status_pack(const Tox_Event_Friend_Connection_Status *event, Bin_Pack *bp); +non_null() bool tox_event_friend_lossless_packet_pack(const Tox_Event_Friend_Lossless_Packet *event, Bin_Pack *bp); +non_null() bool tox_event_friend_lossy_packet_pack(const Tox_Event_Friend_Lossy_Packet *event, Bin_Pack *bp); +non_null() bool tox_event_friend_message_pack(const Tox_Event_Friend_Message *event, Bin_Pack *bp); +non_null() bool tox_event_friend_name_pack(const Tox_Event_Friend_Name *event, Bin_Pack *bp); +non_null() bool tox_event_friend_read_receipt_pack(const Tox_Event_Friend_Read_Receipt *event, Bin_Pack *bp); +non_null() bool tox_event_friend_request_pack(const Tox_Event_Friend_Request *event, Bin_Pack *bp); +non_null() bool tox_event_friend_status_message_pack(const Tox_Event_Friend_Status_Message *event, Bin_Pack *bp); +non_null() bool tox_event_friend_status_pack(const Tox_Event_Friend_Status *event, Bin_Pack *bp); +non_null() bool tox_event_friend_typing_pack(const Tox_Event_Friend_Typing *event, Bin_Pack *bp); +non_null() bool tox_event_self_connection_status_pack(const Tox_Event_Self_Connection_Status *event, Bin_Pack *bp); + +/** + * Unpack from msgpack. + */ +non_null() bool tox_event_unpack_into(Tox_Event *event, Bin_Unpack *bu, const Memory *mem); + +non_null() bool tox_event_conference_connected_unpack(Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_conference_invite_unpack(Tox_Event_Conference_Invite **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_conference_message_unpack(Tox_Event_Conference_Message **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_conference_peer_list_changed_unpack(Tox_Event_Conference_Peer_List_Changed **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_conference_peer_name_unpack(Tox_Event_Conference_Peer_Name **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_conference_title_unpack(Tox_Event_Conference_Title **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_file_chunk_request_unpack(Tox_Event_File_Chunk_Request **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_file_recv_chunk_unpack(Tox_Event_File_Recv_Chunk **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_file_recv_control_unpack(Tox_Event_File_Recv_Control **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_file_recv_unpack(Tox_Event_File_Recv **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_connection_status_unpack(Tox_Event_Friend_Connection_Status **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_lossless_packet_unpack(Tox_Event_Friend_Lossless_Packet **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_lossy_packet_unpack(Tox_Event_Friend_Lossy_Packet **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_message_unpack(Tox_Event_Friend_Message **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_name_unpack(Tox_Event_Friend_Name **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_read_receipt_unpack(Tox_Event_Friend_Read_Receipt **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_request_unpack(Tox_Event_Friend_Request **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_status_message_unpack(Tox_Event_Friend_Status_Message **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_status_unpack(Tox_Event_Friend_Status **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_friend_typing_unpack(Tox_Event_Friend_Typing **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_self_connection_status_unpack(Tox_Event_Self_Connection_Status **event, Bin_Unpack *bu, const Memory *mem); + +#ifdef __cplusplus +} +#endif + +#endif // C_TOXCORE_TOXCORE_TOX_EVENT_H diff --git a/toxcore/tox_events.c b/toxcore/tox_events.c index c5f2d6016f9..9991d2472d5 100644 --- a/toxcore/tox_events.c +++ b/toxcore/tox_events.c @@ -49,9 +49,24 @@ void tox_events_init(Tox *tox) tox_callback_self_connection_status(tox, tox_events_handle_self_connection_status); } +uint32_t tox_events_get_size(const Tox_Events *events) +{ + return events == nullptr ? 0 : events->events_size; +} + +const Tox_Event *tox_events_get(const Tox_Events *events, uint32_t index) +{ + if (index >= tox_events_get_size(events)) { + return nullptr; + } + + return &events->events[index]; +} + Tox_Events *tox_events_iterate(Tox *tox, bool fail_hard, Tox_Err_Events_Iterate *error) { - Tox_Events_State state = {TOX_ERR_EVENTS_ITERATE_OK}; + const Tox_System *sys = tox_get_system(tox); + Tox_Events_State state = {TOX_ERR_EVENTS_ITERATE_OK, sys->mem}; tox_iterate(tox, &state); if (error != nullptr) { @@ -68,141 +83,21 @@ Tox_Events *tox_events_iterate(Tox *tox, bool fail_hard, Tox_Err_Events_Iterate bool tox_events_pack(const Tox_Events *events, Bin_Pack *bp) { - const uint32_t count = tox_events_get_conference_connected_size(events) - + tox_events_get_conference_invite_size(events) - + tox_events_get_conference_message_size(events) - + tox_events_get_conference_peer_list_changed_size(events) - + tox_events_get_conference_peer_name_size(events) - + tox_events_get_conference_title_size(events) - + tox_events_get_file_chunk_request_size(events) - + tox_events_get_file_recv_chunk_size(events) - + tox_events_get_file_recv_control_size(events) - + tox_events_get_file_recv_size(events) - + tox_events_get_friend_connection_status_size(events) - + tox_events_get_friend_lossless_packet_size(events) - + tox_events_get_friend_lossy_packet_size(events) - + tox_events_get_friend_message_size(events) - + tox_events_get_friend_name_size(events) - + tox_events_get_friend_read_receipt_size(events) - + tox_events_get_friend_request_size(events) - + tox_events_get_friend_status_message_size(events) - + tox_events_get_friend_status_size(events) - + tox_events_get_friend_typing_size(events) - + tox_events_get_self_connection_status_size(events); - - return bin_pack_array(bp, count) - && tox_events_pack_conference_connected(events, bp) - && tox_events_pack_conference_invite(events, bp) - && tox_events_pack_conference_message(events, bp) - && tox_events_pack_conference_peer_list_changed(events, bp) - && tox_events_pack_conference_peer_name(events, bp) - && tox_events_pack_conference_title(events, bp) - && tox_events_pack_file_chunk_request(events, bp) - && tox_events_pack_file_recv_chunk(events, bp) - && tox_events_pack_file_recv_control(events, bp) - && tox_events_pack_file_recv(events, bp) - && tox_events_pack_friend_connection_status(events, bp) - && tox_events_pack_friend_lossless_packet(events, bp) - && tox_events_pack_friend_lossy_packet(events, bp) - && tox_events_pack_friend_message(events, bp) - && tox_events_pack_friend_name(events, bp) - && tox_events_pack_friend_read_receipt(events, bp) - && tox_events_pack_friend_request(events, bp) - && tox_events_pack_friend_status_message(events, bp) - && tox_events_pack_friend_status(events, bp) - && tox_events_pack_friend_typing(events, bp) - && tox_events_pack_self_connection_status(events, bp); -} - -non_null() -static bool tox_events_unpack_event(Tox_Events *events, Bin_Unpack *bu) -{ - uint32_t size; - if (!bin_unpack_array(bu, &size)) { - return false; - } - - if (size != 2) { + const uint32_t size = tox_events_get_size(events); + if (!bin_pack_array(bp, size)) { return false; } - uint8_t type; - if (!bin_unpack_u08(bu, &type)) { - return false; - } - - switch (type) { - case TOX_EVENT_CONFERENCE_CONNECTED: - return tox_events_unpack_conference_connected(events, bu); - - case TOX_EVENT_CONFERENCE_INVITE: - return tox_events_unpack_conference_invite(events, bu); - - case TOX_EVENT_CONFERENCE_MESSAGE: - return tox_events_unpack_conference_message(events, bu); - - case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: - return tox_events_unpack_conference_peer_list_changed(events, bu); - - case TOX_EVENT_CONFERENCE_PEER_NAME: - return tox_events_unpack_conference_peer_name(events, bu); - - case TOX_EVENT_CONFERENCE_TITLE: - return tox_events_unpack_conference_title(events, bu); - - case TOX_EVENT_FILE_CHUNK_REQUEST: - return tox_events_unpack_file_chunk_request(events, bu); - - case TOX_EVENT_FILE_RECV_CHUNK: - return tox_events_unpack_file_recv_chunk(events, bu); - - case TOX_EVENT_FILE_RECV_CONTROL: - return tox_events_unpack_file_recv_control(events, bu); - - case TOX_EVENT_FILE_RECV: - return tox_events_unpack_file_recv(events, bu); - - case TOX_EVENT_FRIEND_CONNECTION_STATUS: - return tox_events_unpack_friend_connection_status(events, bu); - - case TOX_EVENT_FRIEND_LOSSLESS_PACKET: - return tox_events_unpack_friend_lossless_packet(events, bu); - - case TOX_EVENT_FRIEND_LOSSY_PACKET: - return tox_events_unpack_friend_lossy_packet(events, bu); - - case TOX_EVENT_FRIEND_MESSAGE: - return tox_events_unpack_friend_message(events, bu); - - case TOX_EVENT_FRIEND_NAME: - return tox_events_unpack_friend_name(events, bu); - - case TOX_EVENT_FRIEND_READ_RECEIPT: - return tox_events_unpack_friend_read_receipt(events, bu); - - case TOX_EVENT_FRIEND_REQUEST: - return tox_events_unpack_friend_request(events, bu); - - case TOX_EVENT_FRIEND_STATUS_MESSAGE: - return tox_events_unpack_friend_status_message(events, bu); - - case TOX_EVENT_FRIEND_STATUS: - return tox_events_unpack_friend_status(events, bu); - - case TOX_EVENT_FRIEND_TYPING: - return tox_events_unpack_friend_typing(events, bu); - - case TOX_EVENT_SELF_CONNECTION_STATUS: - return tox_events_unpack_self_connection_status(events, bu); - - default: + for (uint32_t i = 0; i < size; ++i) { + if (!tox_event_pack(&events->events[i], bp)) { return false; + } } return true; } -bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu) +bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu, const Memory *mem) { uint32_t size; if (!bin_unpack_array(bu, &size)) { @@ -210,11 +105,20 @@ bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu) } for (uint32_t i = 0; i < size; ++i) { - if (!tox_events_unpack_event(events, bu)) { + Tox_Event event = {TOX_EVENT_INVALID}; + if (!tox_event_unpack_into(&event, bu, mem)) { + tox_event_destruct(&event, mem); + return false; + } + + if (!tox_events_add(events, &event)) { + tox_event_destruct(&event, mem); return false; } } + // Invariant: if all adds worked, the events size must be the input array size. + assert(tox_events_get_size(events) == size); return true; } @@ -230,9 +134,9 @@ uint32_t tox_events_bytes_size(const Tox_Events *events) return bin_pack_obj_size(tox_events_bin_pack_handler, nullptr, events); } -void tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes) +bool tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes) { - bin_pack_obj(tox_events_bin_pack_handler, nullptr, events, bytes, UINT32_MAX); + return bin_pack_obj(tox_events_bin_pack_handler, nullptr, events, bytes, UINT32_MAX); } Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_t bytes_size) @@ -253,8 +157,9 @@ Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_ *events = (Tox_Events) { nullptr }; + events->mem = sys->mem; - if (!tox_events_unpack(events, bu)) { + if (!tox_events_unpack(events, bu, sys->mem)) { tox_events_free(events); bin_unpack_free(bu); return nullptr; diff --git a/toxcore/tox_events.h b/toxcore/tox_events.h index e868d89098a..c8713674153 100644 --- a/toxcore/tox_events.h +++ b/toxcore/tox_events.h @@ -88,7 +88,7 @@ uint32_t tox_event_file_recv_get_kind( typedef struct Tox_Event_File_Recv_Chunk Tox_Event_File_Recv_Chunk; const uint8_t *tox_event_file_recv_chunk_get_data( const Tox_Event_File_Recv_Chunk *file_recv_chunk); -uint32_t tox_event_file_recv_chunk_get_length( +uint32_t tox_event_file_recv_chunk_get_data_length( const Tox_Event_File_Recv_Chunk *file_recv_chunk); uint32_t tox_event_file_recv_chunk_get_file_number( const Tox_Event_File_Recv_Chunk *file_recv_chunk); @@ -184,7 +184,7 @@ Tox_Connection tox_event_self_connection_status_get_connection_status( const Tox_Event_Self_Connection_Status *self_connection_status); -typedef enum Tox_Event { +typedef enum Tox_Event_Type { TOX_EVENT_SELF_CONNECTION_STATUS = 0, TOX_EVENT_FRIEND_REQUEST = 1, @@ -212,7 +212,66 @@ typedef enum Tox_Event { TOX_EVENT_CONFERENCE_TITLE = 19, TOX_EVENT_CONFERENCE_MESSAGE = 20, -} Tox_Event; + + TOX_EVENT_INVALID = 255, +} Tox_Event_Type; + +const char *tox_event_type_to_string(Tox_Event_Type type); + +/** + * A single Tox core event. + * + * It could contain any of the above event types. Use `tox_event_get_type` to + * find out which one it is, then use one of the `get` functions to get the + * actual event object out. The `get` functions will return NULL in case of type + * mismatch. + */ +typedef struct Tox_Event Tox_Event; + +Tox_Event_Type tox_event_get_type(const Tox_Event *event); + +const Tox_Event_Conference_Connected *tox_event_get_conference_connected( + const Tox_Event *event); +const Tox_Event_Conference_Invite *tox_event_get_conference_invite( + const Tox_Event *event); +const Tox_Event_Conference_Message *tox_event_get_conference_message( + const Tox_Event *event); +const Tox_Event_Conference_Peer_List_Changed *tox_event_get_conference_peer_list_changed( + const Tox_Event *event); +const Tox_Event_Conference_Peer_Name *tox_event_get_conference_peer_name( + const Tox_Event *event); +const Tox_Event_Conference_Title *tox_event_get_conference_title( + const Tox_Event *event); +const Tox_Event_File_Chunk_Request *tox_event_get_file_chunk_request( + const Tox_Event *event); +const Tox_Event_File_Recv_Chunk *tox_event_get_file_recv_chunk( + const Tox_Event *event); +const Tox_Event_File_Recv_Control *tox_event_get_file_recv_control( + const Tox_Event *event); +const Tox_Event_File_Recv *tox_event_get_file_recv( + const Tox_Event *event); +const Tox_Event_Friend_Connection_Status *tox_event_get_friend_connection_status( + const Tox_Event *event); +const Tox_Event_Friend_Lossless_Packet *tox_event_get_friend_lossless_packet( + const Tox_Event *event); +const Tox_Event_Friend_Lossy_Packet *tox_event_get_friend_lossy_packet( + const Tox_Event *event); +const Tox_Event_Friend_Message *tox_event_get_friend_message( + const Tox_Event *event); +const Tox_Event_Friend_Name *tox_event_get_friend_name( + const Tox_Event *event); +const Tox_Event_Friend_Read_Receipt *tox_event_get_friend_read_receipt( + const Tox_Event *event); +const Tox_Event_Friend_Request *tox_event_get_friend_request( + const Tox_Event *event); +const Tox_Event_Friend_Status_Message *tox_event_get_friend_status_message( + const Tox_Event *event); +const Tox_Event_Friend_Status *tox_event_get_friend_status( + const Tox_Event *event); +const Tox_Event_Friend_Typing *tox_event_get_friend_typing( + const Tox_Event *event); +const Tox_Event_Self_Connection_Status *tox_event_get_self_connection_status( + const Tox_Event *event); /** * Container object for all Tox core events. @@ -221,6 +280,9 @@ typedef enum Tox_Event { */ typedef struct Tox_Events Tox_Events; +uint32_t tox_events_get_size(const Tox_Events *events); +const Tox_Event *tox_events_get(const Tox_Events *events, uint32_t index); + uint32_t tox_events_get_conference_connected_size(const Tox_Events *events); uint32_t tox_events_get_conference_invite_size(const Tox_Events *events); uint32_t tox_events_get_conference_message_size(const Tox_Events *events); @@ -341,7 +403,7 @@ Tox_Events *tox_events_iterate(Tox *tox, bool fail_hard, Tox_Err_Events_Iterate void tox_events_free(Tox_Events *events); uint32_t tox_events_bytes_size(const Tox_Events *events); -void tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes); +bool tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes); Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_t bytes_size); diff --git a/toxcore/tox_events_test.cc b/toxcore/tox_events_test.cc index 5de29b5e483..fa76d3933cb 100644 --- a/toxcore/tox_events_test.cc +++ b/toxcore/tox_events_test.cc @@ -48,14 +48,14 @@ TEST(ToxEvents, NullEventsPacksToEmptyArray) TEST(ToxEvents, PackedEventsCanBeUnpacked) { const Tox_System sys = tox_default_system(); - // [[0, 1]] == Tox_Self_Connection_Status { .connection_status = TOX_CONNECTION_TCP } - std::array packed{0x91, 0x92, 0xcc, 0x00, 0xcc, 0x01}; + // [[1, 1]] == Tox_Self_Connection_Status { .connection_status = TOX_CONNECTION_TCP } + std::array packed{0x91, 0x92, 0xcc, 0x01, 0xcc, 0x01}; Tox_Events *events = tox_events_load(&sys, packed.data(), packed.size()); ASSERT_NE(events, nullptr); std::array bytes; ASSERT_EQ(tox_events_bytes_size(events), bytes.size()); tox_events_get_bytes(events, bytes.data()); - EXPECT_EQ(bytes, (std::array{0x91, 0x92, 0x00, 0x01})); + EXPECT_EQ(bytes, (std::array{0x91, 0x92, 0x01, 0x01})); tox_events_free(events); }