From b2ae9530a405e02a50476c04fc7196c5e9863ad6 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 27 Dec 2023 12:37:22 +0100 Subject: [PATCH] Squashed 'external/toxcore/c-toxcore/' changes from e29e185c03..f1df709b87 f1df709b87 feat: add ngc events 1b6c907235 refactor: Make event dispatch ordered by receive time. b7f9367f6f test: Upgrade cppcheck, fix some warnings. 766e62bc89 chore: Use `pkg_search_module` directly in cmake. 00ff078f91 cleanup: Use target_link_libraries directly in cmake. c58928cc89 chore: Add `IMPORTED_TARGET` to pkg-config packages. 895a6af122 cleanup: Remove NaCl support. 41dfb1c1c0 fix: unpack enum function names in event impl generator 447666d1a1 chore: Disable targets for cross-compilation. 572924e924 chore: Build a docker image with coverage info in it. 415cb78f5e cleanup: Some portability/warning fixes for Windows builds. 425216d9ec fix: Correct a use-after-free and fix some memory leaks. 4b1cfa3e08 refactor: Change all enum-like `#define` sequences into enums. d3c2704fa9 chore: Fix make_single_file to support core-only. 0ce46b644e refactor: Change the `TCP_PACKET_*` defines into an enum. 22cd38ad50 adopt event impl generation tool to #2392 f31ea1088a add the event impl generation tool 4e603bb613 refactor: Use `enum-from-int` rule from tokstyle. 19d8f180d6 chore: Update github actions `uses`. 6a895be0c7 test: Make esp32 build actually try to instantiate tox. 65d09c9bfb cleanup: Remove test net support. REVERT: e29e185c03 feat: add ngc events git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: f1df709b8792da4c0e946d826b11df77d565064d --- .circleci/config.yml | 2 - .github/scripts/autotools-linux | 3 - .github/scripts/flags-clang.sh | 2 + .github/scripts/flags-coverage.sh | 2 +- .github/settings.yml | 2 +- .github/workflows/ci.yml | 63 +- .github/workflows/docker.yml | 50 +- .github/workflows/post-submit.yml | 31 +- .github/workflows/sonar-scan.yml | 5 +- CMakeLists.txt | 95 +- INSTALL.md | 18 +- README.md | 15 +- auto_tests/CMakeLists.txt | 43 +- auto_tests/Makefile.inc | 20 +- auto_tests/announce_test.c | 4 + auto_tests/auto_test_support.c | 19 +- auto_tests/encryptsave_test.c | 7 - auto_tests/group_general_test.c | 2 - auto_tests/group_invite_test.c | 3 - auto_tests/group_message_test.c | 2 - auto_tests/group_moderation_test.c | 2 - auto_tests/group_save_test.c | 3 - auto_tests/group_state_test.c | 3 - auto_tests/group_sync_test.c | 3 - auto_tests/group_tcp_test.c | 22 +- auto_tests/group_topic_test.c | 3 - auto_tests/invalid_tcp_proxy_test.c | 16 +- auto_tests/invalid_udp_proxy_test.c | 16 +- auto_tests/send_message_test.c | 1 + auto_tests/tox_dispatch_test.c | 55 +- auto_tests/tox_events_test.c | 9 + cmake/CompileGTest.cmake | 62 - cmake/Dependencies.cmake | 20 +- cmake/ModulePackage.cmake | 95 +- configure.ac | 130 +- docs/TCP_Network.txt | 106 +- docs/updates/Crypto.md | 34 +- libtoxcore.pc.in | 2 +- other/Makefile.inc | 6 +- other/analysis/run-cppcheck | 28 +- other/bootstrap_daemon/CMakeLists.txt | 10 +- .../docker/tox-bootstrapd.sha256 | 2 +- other/bootstrap_daemon/src/Makefile.inc | 6 +- other/bootstrap_daemon/src/tox-bootstrapd.c | 2 +- other/docker/autotools/Dockerfile | 23 +- other/docker/coverage/Dockerfile | 31 +- other/docker/coverage/Dockerfile.nginx | 5 + other/docker/coverage/run | 8 +- other/docker/coverage/run_mallocfail | 20 +- other/docker/coverage/serve | 8 + other/docker/cppcheck/Dockerfile | 30 + other/docker/cppcheck/run | 5 + other/docker/cppcheck/toxcore.cfg | 117 + other/docker/esp32/BUILD.bazel | 15 + other/docker/esp32/Dockerfile | 72 +- other/docker/esp32/bootstrap/CMakeLists.txt | 4 + other/docker/esp32/bootstrap/hello_main.cc | 4 + .../docker/esp32/bootstrap/idf_component.yml | 2 + other/docker/esp32/hello/CMakeLists.txt | 4 - other/docker/esp32/hello/hello_main.c | 6 - other/docker/esp32/host_main.cc | 3 + other/docker/esp32/main/CMakeLists.txt | 16 + other/docker/esp32/main/app_main.cc | 73 + other/docker/esp32/main/tox_main.cc | 98 + other/docker/esp32/main/tox_main.h | 6 + other/docker/esp32/qemu-test | 17 + other/docker/esp32/run | 1 - other/docker/esp32/run-host | 5 + other/docker/esp32/sdkconfig | 1937 +++++++++++++++++ other/docker/esp32/toxcore/CMakeLists.txt | 12 - other/docker/esp32/toxcore/toxcore_main.cc | 14 - other/docker/tcc/Dockerfile | 16 +- other/event_tooling/Dockerfile | 12 + other/event_tooling/generate_event_c.cpp | 256 +-- other/event_tooling/run | 8 + other/fun/CMakeLists.txt | 29 +- other/fun/create_savedata.c | 2 +- other/fun/save-generator.c | 2 +- other/fun/sign.c | 2 +- other/fun/strkey.c | 6 +- other/make_single_file | 9 +- testing/BUILD.bazel | 23 +- testing/CMakeLists.txt | 19 +- testing/Makefile.inc | 6 +- testing/Messenger_test.c | 2 +- testing/fuzzing/CMakeLists.txt | 8 +- testing/misc_tools.c | 11 - toxav/Makefile.inc | 3 - toxav/groupav.c | 4 +- toxav/groupav.h | 2 +- toxav/msi.c | 8 +- toxcore/BUILD.bazel | 12 +- toxcore/DHT.c | 13 +- toxcore/DHT_test.cc | 3 + toxcore/Makefile.inc | 10 +- toxcore/Messenger.c | 86 +- toxcore/Messenger.h | 2 +- toxcore/TCP_client.c | 29 +- toxcore/TCP_common.h | 41 +- toxcore/TCP_connection.c | 6 +- toxcore/TCP_server.c | 3 +- toxcore/announce.c | 9 +- toxcore/crypto_core.c | 50 +- toxcore/crypto_core.h | 3 +- toxcore/events/conference_connected.c | 182 +- toxcore/events/conference_invite.c | 179 +- toxcore/events/conference_message.c | 179 +- toxcore/events/conference_peer_list_changed.c | 195 +- toxcore/events/conference_peer_name.c | 182 +- toxcore/events/conference_title.c | 182 +- toxcore/events/events_alloc.c | 75 +- toxcore/events/events_alloc.h | 299 +-- toxcore/events/file_chunk_request.c | 181 +- toxcore/events/file_recv.c | 182 +- toxcore/events/file_recv_chunk.c | 184 +- toxcore/events/file_recv_control.c | 179 +- toxcore/events/friend_connection_status.c | 213 +- toxcore/events/friend_lossless_packet.c | 178 +- toxcore/events/friend_lossy_packet.c | 175 +- toxcore/events/friend_message.c | 186 +- toxcore/events/friend_name.c | 180 +- toxcore/events/friend_read_receipt.c | 178 +- toxcore/events/friend_request.c | 196 +- toxcore/events/friend_status.c | 177 +- toxcore/events/friend_status_message.c | 185 +- toxcore/events/friend_typing.c | 180 +- toxcore/events/group_custom_packet.c | 176 +- toxcore/events/group_custom_private_packet.c | 176 +- toxcore/events/group_invite.c | 180 +- toxcore/events/group_join_fail.c | 176 +- toxcore/events/group_message.c | 178 +- toxcore/events/group_moderation.c | 176 +- toxcore/events/group_password.c | 176 +- toxcore/events/group_peer_exit.c | 182 +- toxcore/events/group_peer_join.c | 174 +- toxcore/events/group_peer_limit.c | 174 +- toxcore/events/group_peer_name.c | 176 +- toxcore/events/group_peer_status.c | 176 +- toxcore/events/group_privacy_state.c | 176 +- toxcore/events/group_private_message.c | 178 +- toxcore/events/group_self_join.c | 175 +- toxcore/events/group_topic.c | 176 +- toxcore/events/group_topic_lock.c | 176 +- toxcore/events/group_voice_state.c | 176 +- toxcore/events/self_connection_status.c | 184 +- toxcore/forwarding.c | 6 +- toxcore/forwarding.h | 6 +- toxcore/friend_connection.c | 20 +- toxcore/group.c | 5 +- toxcore/group_chats.c | 19 +- toxcore/group_connection.c | 6 +- toxcore/group_onion_announce.c | 3 - toxcore/group_pack.c | 55 +- toxcore/group_pack.h | 6 +- toxcore/net_crypto.c | 3 +- toxcore/net_crypto.h | 46 +- toxcore/network.c | 27 +- toxcore/network.h | 53 +- toxcore/onion.c | 1 + toxcore/onion_announce.c | 1 + toxcore/onion_client.c | 7 +- toxcore/tox.c | 35 +- toxcore/tox_dispatch.c | 674 ++---- toxcore/tox_event.c | 1058 +++++++++ toxcore/tox_event.h | 254 +++ toxcore/tox_events.c | 257 +-- toxcore/tox_events.h | 128 +- toxcore/tox_events_test.cc | 6 +- toxcore/tox_private.c | 3 - toxcore/tox_test.cc | 7 + toxcore/tox_unpack.c | 447 ++-- toxcore/tox_unpack.h | 22 +- toxencryptsave/Makefile.inc | 6 - 173 files changed, 9191 insertions(+), 5633 deletions(-) delete mode 100644 cmake/CompileGTest.cmake create mode 100644 other/docker/coverage/Dockerfile.nginx create mode 100755 other/docker/coverage/serve create mode 100644 other/docker/cppcheck/Dockerfile create mode 100755 other/docker/cppcheck/run create mode 100644 other/docker/cppcheck/toxcore.cfg create mode 100644 other/docker/esp32/BUILD.bazel create mode 100644 other/docker/esp32/bootstrap/CMakeLists.txt create mode 100644 other/docker/esp32/bootstrap/hello_main.cc create mode 100644 other/docker/esp32/bootstrap/idf_component.yml delete mode 100644 other/docker/esp32/hello/CMakeLists.txt delete mode 100644 other/docker/esp32/hello/hello_main.c create mode 100644 other/docker/esp32/host_main.cc create mode 100644 other/docker/esp32/main/CMakeLists.txt create mode 100644 other/docker/esp32/main/app_main.cc create mode 100644 other/docker/esp32/main/tox_main.cc create mode 100644 other/docker/esp32/main/tox_main.h create mode 100755 other/docker/esp32/qemu-test create mode 100755 other/docker/esp32/run-host create mode 100644 other/docker/esp32/sdkconfig delete mode 100644 other/docker/esp32/toxcore/CMakeLists.txt delete mode 100644 other/docker/esp32/toxcore/toxcore_main.cc create mode 100644 other/event_tooling/Dockerfile create mode 100755 other/event_tooling/run create mode 100644 toxcore/tox_event.c create mode 100644 toxcore/tox_event.h diff --git a/.circleci/config.yml b/.circleci/config.yml index ee37ea56..9006f681 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -134,7 +134,6 @@ jobs: - run: apt-get install -y --no-install-recommends ca-certificates - cppcheck g++ llvm-dev - checkout @@ -142,7 +141,6 @@ jobs: - run: other/analysis/check_includes - run: other/analysis/check_logger_levels - run: other/analysis/run-clang - - run: other/analysis/run-cppcheck - run: other/analysis/run-gcc clang-analyze: diff --git a/.github/scripts/autotools-linux b/.github/scripts/autotools-linux index a6e36f53..7cb30299 100755 --- a/.github/scripts/autotools-linux +++ b/.github/scripts/autotools-linux @@ -11,10 +11,7 @@ add_ld_flag -Wl,-z,defs # Make compilation error on a warning add_flag -Werror -add_config_flag --with-nacl-libs="$CACHEDIR/lib/amd64" -add_config_flag --with-nacl-headers="$CACHEDIR/include/amd64" add_config_flag --disable-ipv6 -add_config_flag --enable-nacl add_config_flag --enable-daemon add_config_flag --with-log-level=TRACE diff --git a/.github/scripts/flags-clang.sh b/.github/scripts/flags-clang.sh index d083672e..f5ae253f 100644 --- a/.github/scripts/flags-clang.sh +++ b/.github/scripts/flags-clang.sh @@ -65,6 +65,8 @@ add_cxx_flag -Wno-c++98-compat-pedantic add_cxx_flag -Wno-c99-extensions # We're C-compatible, so use C style casts. add_cxx_flag -Wno-old-style-cast +# GTest does this. +add_cxx_flag -Wno-global-constructors # Downgrade to warning so we still see it. add_flag -Wno-error=unreachable-code diff --git a/.github/scripts/flags-coverage.sh b/.github/scripts/flags-coverage.sh index 3fa6b6bd..ebfa2b1c 100644 --- a/.github/scripts/flags-coverage.sh +++ b/.github/scripts/flags-coverage.sh @@ -14,7 +14,7 @@ add_flag --coverage add_c_flag -fno-inline -fno-omit-frame-pointer # Show useful stack traces on crash. -add_flag -fsanitize=undefined -fno-sanitize-recover=all +add_flag -fsanitize=undefined -fno-sanitize-recover=all -D_DEBUG # In test code (_test.cc and libgtest), throw away all debug information. # We only care about stack frames inside toxcore (which is C). Without this, diff --git a/.github/settings.yml b/.github/settings.yml index 18c45426..6e118f0d 100644 --- a/.github/settings.yml +++ b/.github/settings.yml @@ -16,9 +16,9 @@ branches: - "bazel-opt" - "build-alpine-s390x" - "build-android" + - "build-autotools" - "build-compcert" - "build-macos" - - "build-nacl" - "build-tcc" - "build-win32" - "build-win64" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1fe12fb7..d6f715bd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,10 +13,20 @@ jobs: common: uses: TokTok/ci-tools/.github/workflows/common-ci.yml@master + cppcheck: + runs-on: ubuntu-latest + steps: + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Docker Build + uses: docker/build-push-action@v4 + with: + file: other/docker/cppcheck/Dockerfile + mypy: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: submodules: recursive - name: Set up Python 3.9 @@ -34,9 +44,9 @@ jobs: runs-on: ubuntu-latest steps: - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Docker Build - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v4 with: file: other/docker/doxygen/Dockerfile @@ -44,9 +54,9 @@ jobs: runs-on: ubuntu-latest steps: - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Docker Build - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v4 with: file: other/docker/tokstyle/Dockerfile @@ -54,19 +64,19 @@ jobs: runs-on: ubuntu-latest steps: - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Docker Build - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v4 with: file: other/docker/misra/Dockerfile - build-nacl: + build-autotools: runs-on: ubuntu-latest steps: - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Docker Build - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v4 with: file: other/docker/autotools/Dockerfile @@ -74,9 +84,9 @@ jobs: runs-on: ubuntu-latest steps: - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Docker Build - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v4 with: file: other/docker/tcc/Dockerfile @@ -84,16 +94,27 @@ jobs: runs-on: ubuntu-latest steps: - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Docker Build - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v4 with: file: other/docker/compcert/Dockerfile + 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: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: submodules: recursive - name: Run cimplefmt @@ -102,7 +123,7 @@ jobs: build-win32: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: submodules: recursive - name: Cross compilation @@ -111,7 +132,7 @@ jobs: build-win64: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: submodules: recursive - name: Cross compilation @@ -120,7 +141,7 @@ jobs: build-macos: runs-on: macos-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: submodules: recursive - name: Build and test @@ -129,16 +150,16 @@ jobs: coverage-linux: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: submodules: recursive - name: Build, test, and upload coverage - run: .github/scripts/coverage-linux + run: other/docker/coverage/run build-android: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: submodules: recursive - run: .github/scripts/cmake-android armeabi-v7a diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index ad88b436..3779621d 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -17,11 +17,11 @@ jobs: steps: - name: Login to DockerHub if: ${{ github.event_name == 'push' }} - uses: docker/login-action@v1 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: submodules: recursive - name: Docker Build @@ -38,15 +38,15 @@ jobs: needs: [docker-bootstrap-node] steps: - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Login to DockerHub if: ${{ github.event_name == 'push' }} - uses: docker/login-action@v1 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Build and push - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v4 with: context: "{{defaultContext}}:other/bootstrap_daemon/websocket" push: ${{ github.event_name == 'push' }} @@ -57,17 +57,17 @@ jobs: docker-clusterfuzz: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Login to DockerHub if: ${{ github.event_name == 'push' }} - uses: docker/login-action@v1 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Build and push - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v4 with: context: "." file: .clusterfuzzlite/Dockerfile @@ -80,15 +80,15 @@ jobs: runs-on: ubuntu-latest steps: - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Login to DockerHub if: ${{ github.event_name == 'push' }} - uses: docker/login-action@v1 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Build and push - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v4 with: file: testing/Dockerfile push: ${{ github.event_name == 'push' }} @@ -100,15 +100,15 @@ jobs: runs-on: ubuntu-latest steps: - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Login to DockerHub if: ${{ github.event_name == 'push' }} - uses: docker/login-action@v1 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Build and push - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v4 with: file: other/emscripten/Dockerfile push: ${{ github.event_name == 'push' }} @@ -120,22 +120,22 @@ jobs: runs-on: ubuntu-latest steps: - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 with: driver: docker - name: Login to DockerHub if: ${{ github.event_name == 'push' }} - uses: docker/login-action@v1 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Build toxchat/c-toxcore:sources - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v4 with: file: other/docker/sources/Dockerfile tags: toxchat/c-toxcore:sources - name: Build and push - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v4 with: file: other/docker/esp32/Dockerfile push: ${{ github.event_name == 'push' }} @@ -147,15 +147,15 @@ jobs: runs-on: ubuntu-latest steps: - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Login to DockerHub if: ${{ github.event_name == 'push' }} - uses: docker/login-action@v1 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Build and push - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v4 with: context: "{{defaultContext}}:other/docker/windows" push: ${{ github.event_name == 'push' }} @@ -171,15 +171,15 @@ jobs: runs-on: ubuntu-latest steps: - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Login to DockerHub if: ${{ github.event_name == 'push' }} - uses: docker/login-action@v1 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Build and push - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v4 with: context: "{{defaultContext}}:other/docker/windows" push: ${{ github.event_name == 'push' }} diff --git a/.github/workflows/post-submit.yml b/.github/workflows/post-submit.yml index 56895a6a..346d2472 100644 --- a/.github/workflows/post-submit.yml +++ b/.github/workflows/post-submit.yml @@ -9,8 +9,35 @@ jobs: runs-on: ubuntu-latest steps: - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v3 - name: Docker Build - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v4 with: file: other/docker/alpine-s390x/Dockerfile + + docker-coverage: + runs-on: ubuntu-latest + steps: + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + driver: docker + - name: Login to DockerHub + if: ${{ github.event_name == 'push' }} + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Build toxchat/c-toxcore:sources + uses: docker/build-push-action@v4 + with: + file: other/docker/sources/Dockerfile + tags: toxchat/c-toxcore:sources + - name: Build and push + uses: docker/build-push-action@v4 + with: + file: other/docker/coverage/Dockerfile + push: ${{ github.event_name == 'push' }} + tags: toxchat/c-toxcore:coverage + cache-from: type=registry,ref=toxchat/c-toxcore:coverage + cache-to: type=inline diff --git a/.github/workflows/sonar-scan.yml b/.github/workflows/sonar-scan.yml index b10bfc8e..e25ac375 100644 --- a/.github/workflows/sonar-scan.yml +++ b/.github/workflows/sonar-scan.yml @@ -19,13 +19,14 @@ jobs: SONAR_SERVER_URL: "https://sonarcloud.io" BUILD_WRAPPER_OUT_DIR: build_wrapper_output_directory # Directory where build-wrapper output will be placed steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis submodules: recursive - name: Set up JDK 17 - uses: actions/setup-java@v1 + uses: actions/setup-java@v2 with: + distribution: "zulu" java-version: 17 - name: Download and set up sonar-scanner env: diff --git a/CMakeLists.txt b/CMakeLists.txt index fd4f071b..113eaa28 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,8 +14,8 @@ # ################################################################################ -cmake_minimum_required(VERSION 3.5) -cmake_policy(VERSION 3.5) +cmake_minimum_required(VERSION 3.16) +cmake_policy(VERSION 3.16) project(toxcore) list(APPEND CMAKE_MODULE_PATH ${toxcore_SOURCE_DIR}/cmake) @@ -76,6 +76,7 @@ if(APPLE) endif() enable_testing() +find_package(GTest) set(CMAKE_MACOSX_RPATH ON) @@ -111,16 +112,11 @@ if(NOT USE_IPV6) add_definitions(-DUSE_IPV6=0) endif() -option(USE_TEST_NETWORK "Use a separate test network with different packet IDs" OFF) -if(USE_TEST_NETWORK) - add_definitions(-DUSE_TEST_NETWORK=1) -endif() - option(BUILD_MISC_TESTS "Build additional tests and utilities" OFF) option(BUILD_FUN_UTILS "Build additional just for fun utilities" OFF) option(AUTOTEST "Enable autotests (mainly for CI)" OFF) -if (AUTOTEST) +if(AUTOTEST) option(NON_HERMETIC_TESTS "Whether to build and run tests that depend on an internet connection" OFF) option(PROXY_TEST "Enable proxy test (needs HTTP/SOCKS5 proxy on port 8080/8081)" OFF) endif() @@ -320,6 +316,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 @@ -329,7 +327,10 @@ set(toxcore_SOURCES toxcore/tox_unpack.h toxcore/util.c toxcore/util.h) -set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${LIBSODIUM_LIBRARIES}) +set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${LIBSODIUM_LIBRARIES}) +set(toxcore_LINK_DIRECTORIES ${toxcore_LINK_DIRECTORIES} ${LIBSODIUM_LIBRARY_DIRS}) +set(toxcore_INCLUDE_DIRECTORIES ${toxcore_INCLUDE_DIRECTORIES} ${LIBSODIUM_INCLUDE_DIRS}) +set(toxcore_COMPILE_OPTIONS ${toxcore_COMPILE_OPTIONS} ${LIBSODIUM_CFLAGS_OTHER}) set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} libsodium) set(toxcore_API_HEADERS ${toxcore_SOURCE_DIR}/toxcore/tox.h^tox @@ -365,7 +366,10 @@ if(BUILD_TOXAV) set(toxcore_API_HEADERS ${toxcore_API_HEADERS} ${toxcore_SOURCE_DIR}/toxav/toxav.h^toxav) - set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${OPUS_LIBRARIES} ${VPX_LIBRARIES}) + set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${OPUS_LIBRARIES} ${VPX_LIBRARIES}) + set(toxcore_LINK_DIRECTORIES ${toxcore_LINK_DIRECTORIES} ${OPUS_LIBRARY_DIRS} ${VPX_LIBRARY_DIRS}) + set(toxcore_INCLUDE_DIRECTORIES ${toxcore_INCLUDE_DIRECTORIES} ${OPUS_INCLUDE_DIRS} ${VPX_INCLUDE_DIRS}) + set(toxcore_COMPILE_OPTIONS ${toxcore_COMPILE_OPTIONS} ${OPUS_CFLAGS_OTHER} ${VPX_CFLAGS_OTHER}) set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} opus vpx) endif() @@ -392,28 +396,28 @@ set(toxcore_API_HEADERS ${toxcore_API_HEADERS} # any potential libvpx linking. message("CMAKE_THREAD_LIBS_INIT: ${CMAKE_THREAD_LIBS_INIT}") if(CMAKE_THREAD_LIBS_INIT) - set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${CMAKE_THREAD_LIBS_INIT}) + set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} ${CMAKE_THREAD_LIBS_INIT}) endif() if(NSL_LIBRARIES) - set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${NSL_LIBRARIES}) + set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${NSL_LIBRARIES}) set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} -lnsl) endif() if(RT_LIBRARIES) - set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${RT_LIBRARIES}) + set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${RT_LIBRARIES}) set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} -lrt) endif() if(SOCKET_LIBRARIES) - set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${SOCKET_LIBRARIES}) + set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ${SOCKET_LIBRARIES}) set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} -lsocket) endif() if(WIN32) - set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ws2_32 iphlpapi) + set(toxcore_LINK_LIBRARIES ${toxcore_LINK_LIBRARIES} ws2_32 iphlpapi) set(toxcore_PKGCONFIG_LIBS ${toxcore_PKGCONFIG_LIBS} -lws2_32 -liphlpapi) endif() @@ -427,7 +431,18 @@ endif() add_module(toxcore ${toxcore_SOURCES}) # Link it to all dependencies. -target_link_modules(toxcore ${toxcore_LINK_MODULES}) +if(TARGET toxcore_static) + target_link_libraries(toxcore_static PRIVATE ${toxcore_LINK_LIBRARIES}) + target_link_directories(toxcore_static PUBLIC ${toxcore_LINK_DIRECTORIES}) + target_include_directories(toxcore_static SYSTEM PRIVATE ${toxcore_INCLUDE_DIRECTORIES}) + target_compile_options(toxcore_static PRIVATE ${toxcore_COMPILE_OPTIONS}) +endif() +if(TARGET toxcore_shared) + target_link_libraries(toxcore_shared PRIVATE ${toxcore_LINK_LIBRARIES}) + target_link_directories(toxcore_shared PUBLIC ${toxcore_LINK_DIRECTORIES}) + target_include_directories(toxcore_shared SYSTEM PRIVATE ${toxcore_INCLUDE_DIRECTORIES}) + target_compile_options(toxcore_shared PRIVATE ${toxcore_COMPILE_OPTIONS}) +endif() # Make version script (on systems that support it) to limit symbol visibility. make_version_script(toxcore ${toxcore_API_HEADERS}) @@ -442,23 +457,36 @@ install_module(toxcore DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tox) # ################################################################################ -include(CompileGTest) +function(unit_test subdir target) + add_executable(unit_${target}_test ${subdir}/${target}_test.cc) + if(TARGET toxcore_static) + target_link_libraries(unit_${target}_test PRIVATE toxcore_static) + else() + target_link_libraries(unit_${target}_test PRIVATE toxcore_shared) + endif() + target_link_libraries(unit_${target}_test PRIVATE GTest::GTest GTest::Main) + set_target_properties(unit_${target}_test PROPERTIES COMPILE_FLAGS "${TEST_CXX_FLAGS}") + add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} unit_${target}_test) + set_property(TEST ${target} PROPERTY ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw") +endfunction() # The actual unit tests follow. # -unit_test(toxav ring_buffer) -unit_test(toxav rtp) -unit_test(toxcore DHT) -unit_test(toxcore bin_pack) -unit_test(toxcore crypto_core) -unit_test(toxcore group_announce) -unit_test(toxcore group_moderation) -unit_test(toxcore list) -unit_test(toxcore mem) -unit_test(toxcore mono_time) -unit_test(toxcore ping_array) -unit_test(toxcore tox) -unit_test(toxcore util) +if(GTEST_FOUND) + unit_test(toxav ring_buffer) + unit_test(toxav rtp) + unit_test(toxcore DHT) + unit_test(toxcore bin_pack) + unit_test(toxcore crypto_core) + unit_test(toxcore group_announce) + unit_test(toxcore group_moderation) + unit_test(toxcore list) + unit_test(toxcore mem) + unit_test(toxcore mono_time) + unit_test(toxcore ping_array) + unit_test(toxcore tox) + unit_test(toxcore util) +endif() add_subdirectory(testing) @@ -482,7 +510,12 @@ if(DHT_BOOTSTRAP) add_executable(DHT_bootstrap other/DHT_bootstrap.c other/bootstrap_node_packets.c) - target_link_modules(DHT_bootstrap toxcore misc_tools) + if(TARGET toxcore_static) + target_link_libraries(DHT_bootstrap PRIVATE toxcore_static) + else() + target_link_libraries(DHT_bootstrap PRIVATE toxcore_shared) + endif() + target_link_libraries(DHT_bootstrap PRIVATE misc_tools) install(TARGETS DHT_bootstrap RUNTIME DESTINATION bin) endif() diff --git a/INSTALL.md b/INSTALL.md index f54a161d..8bb901bd 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -32,14 +32,14 @@ These instructions will guide you through the process of building and installing This repository, although called `toxcore`, in fact contains several libraries besides `toxcore` which complement it, as well as several executables. However, note that although these are separate libraries, at the moment, when building the libraries, they are all merged into a single `toxcore` library. Here is the full list of the main components that can be built using the CMake, their dependencies and descriptions. -| Name | Type | Dependencies | Platform | Description | -|------------------|------------|-----------------------------------------------|----------------|----------------------------------------------------------------------------| -| `toxcore` | Library | libnacl or libsodium, libm, libpthread, librt | Cross-platform | The main Tox library that provides the messenger functionality. | -| `toxav` | Library | libtoxcore, libopus, libvpx | Cross-platform | Provides audio/video functionality. | -| `toxencryptsave` | Library | libtoxcore, libnacl or libsodium | Cross-platform | Provides encryption of Tox profiles (savedata), as well as arbitrary data. | -| `DHT_bootstrap` | Executable | libtoxcore | Cross-platform | A simple DHT bootstrap node. | -| `tox-bootstrapd` | Executable | libtoxcore, libconfig | Unix-like | Highly configurable DHT bootstrap node daemon (systemd, SysVinit, Docker). | -| `cmp` | Library | | Cross-platform | C implementation of the MessagePack serialization format. [https://github.com/camgunz/cmp](https://github.com/camgunz/cmp) | +| Name | Type | Dependencies | Platform | Description | +|------------------|------------|------------------------------------|----------------|----------------------------------------------------------------------------| +| `toxcore` | Library | libsodium, libm, libpthread, librt | Cross-platform | The main Tox library that provides the messenger functionality. | +| `toxav` | Library | libtoxcore, libopus, libvpx | Cross-platform | Provides audio/video functionality. | +| `toxencryptsave` | Library | libtoxcore, libsodium | Cross-platform | Provides encryption of Tox profiles (savedata), as well as arbitrary data. | +| `DHT_bootstrap` | Executable | libtoxcore | Cross-platform | A simple DHT bootstrap node. | +| `tox-bootstrapd` | Executable | libtoxcore, libconfig | Unix-like | Highly configurable DHT bootstrap node daemon (systemd, SysVinit, Docker). | +| `cmp` | Library | | Cross-platform | C implementation of the MessagePack serialization format. [https://github.com/camgunz/cmp](https://github.com/camgunz/cmp) | #### Secondary @@ -83,7 +83,7 @@ Useful for generating Tox profiles from the output of the vanity key generators, Library dependencies are listed in the [components](#components) table. The dependencies need to be satisfied for the components to be built. Note that if you don't have a dependency for some component, e.g. you don't have `libopus` installed required for building `toxav` component, building of that component is silently disabled. -Be advised that due to the addition of `cmp` as a submodule, you now also need to initialize the git submodules required by toxcore. This can be done by cloning the repo with the addition of `--recurse-submodules` or by running `git submodule update --init` in the root directory of the repo. +Be advised that due to the addition of `cmp` as a submodule, you now also need to initialize the git submodules required by toxcore. This can be done by cloning the repo with the addition of `--recurse-submodules` or by running `git submodule update --init` in the root directory of the repo. #### Compiler requirements diff --git a/README.md b/README.md index e2d2ecf6..cb62273f 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,8 @@ Tox is a peer to peer (serverless) instant messenger aimed at making security and privacy easy to obtain for regular users. It uses -[NaCl](https://nacl.cr.yp.to/) for its encryption and authentication. +[libsodium](https://doc.libsodium.org/) (based on +[NaCl](https://nacl.cr.yp.to/)) for its encryption and authentication. ## IMPORTANT! @@ -18,12 +19,12 @@ This is an **experimental** cryptographic network library. It has not been formally audited by an independent third party that specializes in cryptography or cryptanalysis. **Use this library at your own risk.** -The underlying crypto library [NaCl](https://nacl.cr.yp.to/install.html) -provides reliable encryption, but the security model has not yet been fully -specified. See [issue 210](https://github.com/TokTok/c-toxcore/issues/210) for -a discussion on developing a threat model. See other issues for known -weaknesses (e.g. [issue 426](https://github.com/TokTok/c-toxcore/issues/426) -describes what can happen if your secret key is stolen). +The underlying crypto library [libsodium](https://doc.libsodium.org/) provides +reliable encryption, but the security model has not yet been fully specified. +See [issue 210](https://github.com/TokTok/c-toxcore/issues/210) for a +discussion on developing a threat model. See other issues for known weaknesses +(e.g. [issue 426](https://github.com/TokTok/c-toxcore/issues/426) describes +what can happen if your secret key is stolen). ## Toxcore Development Roadmap diff --git a/auto_tests/CMakeLists.txt b/auto_tests/CMakeLists.txt index 774e3e33..ffdf8055 100644 --- a/auto_tests/CMakeLists.txt +++ b/auto_tests/CMakeLists.txt @@ -3,18 +3,26 @@ set(TEST_TIMEOUT_SECONDS "" CACHE STRING "Limit runtime of each test to the numb add_library(auto_test_support auto_test_support.c auto_test_support.h) -target_link_modules(auto_test_support toxcore misc_tools) +target_link_libraries(auto_test_support PRIVATE misc_tools) +if(TARGET toxcore_static) + target_link_libraries(auto_test_support PRIVATE toxcore_static) +else() + target_link_libraries(auto_test_support PRIVATE toxcore_shared) +endif() function(auto_test target) - if(AUTOTEST AND NOT (MSVC AND ARGV1 STREQUAL "MSVC_DONT_BUILD")) - add_executable(auto_${target}_test ${target}_test.c) - target_link_modules(auto_${target}_test toxcore misc_tools auto_test_support) - if(NOT ARGV1 STREQUAL "DONT_RUN") - add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} auto_${target}_test) - set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}") - # add the source dir as environment variable, so the testdata can be found - set_tests_properties(${target} PROPERTIES ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw;srcdir=${CMAKE_CURRENT_SOURCE_DIR}") - endif() + add_executable(auto_${target}_test ${target}_test.c) + target_link_libraries(auto_${target}_test PRIVATE misc_tools auto_test_support) + if(TARGET toxcore_static) + target_link_libraries(auto_${target}_test PRIVATE toxcore_static) + else() + target_link_libraries(auto_${target}_test PRIVATE toxcore_shared) + endif() + if(NOT ARGV1 STREQUAL "DONT_RUN") + add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} auto_${target}_test) + set_tests_properties(${target} PROPERTIES TIMEOUT "${TEST_TIMEOUT_SECONDS}") + # add the source dir as environment variable, so the testdata can be found + set_tests_properties(${target} PROPERTIES ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw;srcdir=${CMAKE_CURRENT_SOURCE_DIR}") endif() endfunction() @@ -69,17 +77,28 @@ auto_test(typing) auto_test(version) auto_test(save_compatibility) +target_include_directories(auto_encryptsave_test SYSTEM PRIVATE ${LIBSODIUM_INCLUDE_DIRS}) + if(NON_HERMETIC_TESTS) auto_test(bootstrap) auto_test(tcp_relay) endif() if(BUILD_TOXAV) - auto_test(conference_av MSVC_DONT_BUILD) + auto_test(conference_av) auto_test(toxav_basic) auto_test(toxav_many) -endif() + target_link_libraries(auto_toxav_basic_test PRIVATE ${VPX_LIBRARIES}) + target_link_directories(auto_toxav_basic_test PRIVATE ${VPX_LIBRARY_DIRS}) + target_include_directories(auto_toxav_basic_test SYSTEM PRIVATE ${VPX_INCLUDE_DIRS}) + target_compile_options(auto_toxav_basic_test PRIVATE ${VPX_CFLAGS_OTHER}) + + target_link_libraries(auto_toxav_many_test PRIVATE ${VPX_LIBRARIES}) + target_link_directories(auto_toxav_many_test PRIVATE ${VPX_LIBRARY_DIRS}) + target_include_directories(auto_toxav_many_test SYSTEM PRIVATE ${VPX_INCLUDE_DIRS}) + target_compile_options(auto_toxav_many_test PRIVATE ${VPX_CFLAGS_OTHER}) +endif() if(PROXY_TEST) auto_test(proxy) diff --git a/auto_tests/Makefile.inc b/auto_tests/Makefile.inc index 6b73c2c1..db22d7ff 100644 --- a/auto_tests/Makefile.inc +++ b/auto_tests/Makefile.inc @@ -5,7 +5,7 @@ libauto_test_support_la_SOURCES = ../auto_tests/auto_test_support.c ../auto_test libauto_test_support_la_LIBADD = libmisc_tools.la libtoxcore.la TESTS = \ - announce_test \ + announce_test \ conference_double_invite_test \ conference_invite_merge_test \ conference_peer_nick_test \ @@ -13,6 +13,8 @@ TESTS = \ conference_test \ conference_two_test \ crypto_test \ + encryptsave_test \ + file_saving_test \ file_transfer_test \ forwarding_test \ friend_connection_test \ @@ -34,34 +36,24 @@ TESTS = \ set_name_test \ set_status_message_test \ TCP_test \ - tox_events_test \ tox_dispatch_test \ + tox_events_test \ tox_many_tcp_test \ tox_many_test \ tox_strncasecmp_test \ typing_test \ version_test -if !WITH_NACL -TESTS += \ - encryptsave_test \ - file_saving_test -endif - AUTOTEST_CFLAGS = \ - $(LIBSODIUM_CFLAGS) \ - $(NACL_CFLAGS) + $(LIBSODIUM_CFLAGS) AUTOTEST_LDADD = \ $(LIBSODIUM_LDFLAGS) \ - $(NACL_LDFLAGS) \ libmisc_tools.la \ libauto_test_support.la \ libtoxcore.la \ libtoxencryptsave.la \ - $(LIBSODIUM_LIBS) \ - $(NACL_OBJECTS) \ - $(NACL_LIBS) + $(LIBSODIUM_LIBS) if BUILD_AV diff --git a/auto_tests/announce_test.c b/auto_tests/announce_test.c index f0929c75..f6d22a07 100644 --- a/auto_tests/announce_test.c +++ b/auto_tests/announce_test.c @@ -61,9 +61,13 @@ static void test_store_data(void) ck_assert(log != nullptr); logger_callback_log(log, print_debug_logger, nullptr, nullptr); Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); + ck_assert(mono_time != nullptr); Networking_Core *net = new_networking_no_udp(log, mem, ns); + ck_assert(net != nullptr); DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true); + ck_assert(dht != nullptr); Forwarding *forwarding = new_forwarding(log, rng, mono_time, dht); + ck_assert(forwarding != nullptr); Announcements *announce = new_announcements(log, mem, rng, mono_time, forwarding); ck_assert(announce != nullptr); diff --git a/auto_tests/auto_test_support.c b/auto_tests/auto_test_support.c index 320acc4b..afc6c2cc 100644 --- a/auto_tests/auto_test_support.c +++ b/auto_tests/auto_test_support.c @@ -28,7 +28,6 @@ static const struct BootstrapNodes { uint16_t port; const uint8_t key[32]; } bootstrap_nodes[] = { -#ifndef USE_TEST_NETWORK { "tox.abilinski.com", 33445, 0x10, 0xC0, 0x0E, 0xB2, 0x50, 0xC3, 0x23, 0x3E, @@ -57,22 +56,6 @@ static const struct BootstrapNodes { 0x6D, 0xC9, 0xD0, 0xA3, 0x00, 0xE6, 0xC3, 0x57, 0x63, 0x4E, 0xE2, 0xDA, 0x88, 0xC3, 0x54, 0x63, }, -#else - { - "172.93.52.70", 33445, - 0x79, 0xCA, 0xDA, 0x49, 0x74, 0xB0, 0x92, 0x6F, - 0x28, 0x6F, 0x02, 0x5C, 0xD5, 0xFF, 0xDF, 0x3E, - 0x65, 0x4A, 0x37, 0x58, 0xC5, 0x3E, 0x02, 0x73, - 0xEC, 0xFC, 0x4D, 0x12, 0xC2, 0x1D, 0xCA, 0x48, - }, - { - "tox.plastiras.org", 38445, - 0x5E, 0x47, 0xBA, 0x1D, 0xC3, 0x91, 0x3E, 0xB2, - 0xCB, 0xF2, 0xD6, 0x4C, 0xE4, 0xF2, 0x3D, 0x8B, - 0xFE, 0x53, 0x91, 0xBF, 0xAB, 0xE5, 0xC4, 0x3C, - 0x5B, 0xAD, 0x13, 0xF0, 0xA4, 0x14, 0xCD, 0x77, - }, -#endif // USE_TEST_NETWORK { nullptr, 0, 0 }, }; @@ -332,7 +315,7 @@ static void bootstrap_autotoxes(struct Tox_Options *options, uint32_t tox_count, for (uint32_t i = 1; i < tox_count; ++i) { Tox_Err_Bootstrap err; tox_bootstrap(autotoxes[i].tox, "localhost", dht_port, dht_key, &err); - ck_assert(err == TOX_ERR_BOOTSTRAP_OK); + ck_assert_msg(err == TOX_ERR_BOOTSTRAP_OK, "bootstrap error for port %d: %d", dht_port, err); } if (!udp_enabled) { diff --git a/auto_tests/encryptsave_test.c b/auto_tests/encryptsave_test.c index fb49c4e2..2a3b5fbe 100644 --- a/auto_tests/encryptsave_test.c +++ b/auto_tests/encryptsave_test.c @@ -3,7 +3,6 @@ #include #include -#ifndef VANILLA_NACL #include #include "../testing/misc_tools.h" @@ -232,9 +231,3 @@ int main(void) return 0; } -#else // VANILLA_NACL -int main(void) -{ - return 0; -} -#endif diff --git a/auto_tests/group_general_test.c b/auto_tests/group_general_test.c index 328e76f3..06663041 100644 --- a/auto_tests/group_general_test.c +++ b/auto_tests/group_general_test.c @@ -259,7 +259,6 @@ static void group_peer_status_handler(Tox *tox, uint32_t groupnumber, uint32_t p static void group_announce_test(AutoTox *autotoxes) { -#ifndef VANILLA_NACL ck_assert_msg(NUM_GROUP_TOXES == 2, "NUM_GROUP_TOXES needs to be 2"); Tox *tox0 = autotoxes[0].tox; @@ -436,7 +435,6 @@ static void group_announce_test(AutoTox *autotoxes) ck_assert(num_groups1 == num_groups2 && num_groups2 == 0); printf("All tests passed!\n"); -#endif // VANILLA_NACL } int main(void) diff --git a/auto_tests/group_invite_test.c b/auto_tests/group_invite_test.c index f8dde62a..b83bed5e 100644 --- a/auto_tests/group_invite_test.c +++ b/auto_tests/group_invite_test.c @@ -102,7 +102,6 @@ static void group_peer_join_handler(Tox *tox, uint32_t group_number, uint32_t pe static void group_invite_test(AutoTox *autotoxes) { -#ifndef VANILLA_NACL ck_assert_msg(NUM_GROUP_TOXES > 7, "NUM_GROUP_TOXES is too small: %d", NUM_GROUP_TOXES); for (size_t i = 0; i < NUM_GROUP_TOXES; ++i) { @@ -260,8 +259,6 @@ static void group_invite_test(AutoTox *autotoxes) } printf("All tests passed!\n"); - -#endif // VANILLA_NACL } int main(void) diff --git a/auto_tests/group_message_test.c b/auto_tests/group_message_test.c index 7c90a08d..d93e460a 100644 --- a/auto_tests/group_message_test.c +++ b/auto_tests/group_message_test.c @@ -350,7 +350,6 @@ static void group_message_handler_wraparound_test(Tox *tox, uint32_t groupnumber static void group_message_test(AutoTox *autotoxes) { -#ifndef VANILLA_NACL ck_assert_msg(NUM_GROUP_TOXES >= 2, "NUM_GROUP_TOXES is too small: %d", NUM_GROUP_TOXES); const Random *rng = system_random(); @@ -544,7 +543,6 @@ static void group_message_test(AutoTox *autotoxes) } fprintf(stderr, "All tests passed!\n"); -#endif // VANILLA_NACL } int main(void) diff --git a/auto_tests/group_moderation_test.c b/auto_tests/group_moderation_test.c index a53609fe..712c5907 100644 --- a/auto_tests/group_moderation_test.c +++ b/auto_tests/group_moderation_test.c @@ -432,7 +432,6 @@ static void check_voice_state(AutoTox *autotoxes, uint32_t num_toxes) static void group_moderation_test(AutoTox *autotoxes) { -#ifndef VANILLA_NACL ck_assert_msg(NUM_GROUP_TOXES >= 4, "NUM_GROUP_TOXES is too small: %d", NUM_GROUP_TOXES); ck_assert_msg(NUM_GROUP_TOXES < 10, "NUM_GROUP_TOXES is too big: %d", NUM_GROUP_TOXES); @@ -634,7 +633,6 @@ static void group_moderation_test(AutoTox *autotoxes) } fprintf(stderr, "All tests passed!\n"); -#endif // VANILLA_NACL } int main(void) diff --git a/auto_tests/group_save_test.c b/auto_tests/group_save_test.c index 0cca6cd9..aea898dc 100644 --- a/auto_tests/group_save_test.c +++ b/auto_tests/group_save_test.c @@ -148,7 +148,6 @@ static int has_correct_self_state(const Tox *tox, uint32_t group_number, const u static void group_save_test(AutoTox *autotoxes) { -#ifndef VANILLA_NACL ck_assert_msg(NUM_GROUP_TOXES > 1, "NUM_GROUP_TOXES is too small: %d", NUM_GROUP_TOXES); for (size_t i = 0; i < NUM_GROUP_TOXES; ++i) { @@ -270,8 +269,6 @@ static void group_save_test(AutoTox *autotoxes) tox_kill(new_tox); printf("All tests passed!\n"); - -#endif // VANILLA_NACL } int main(void) diff --git a/auto_tests/group_state_test.c b/auto_tests/group_state_test.c index adbe1725..c506f802 100644 --- a/auto_tests/group_state_test.c +++ b/auto_tests/group_state_test.c @@ -238,7 +238,6 @@ static void set_group_state(Tox *tox, uint32_t groupnumber, uint32_t peer_limit, static void group_state_test(AutoTox *autotoxes) { -#ifndef VANILLA_NACL ck_assert_msg(NUM_GROUP_TOXES >= 3, "NUM_GROUP_TOXES is too small: %d", NUM_GROUP_TOXES); for (size_t i = 0; i < NUM_GROUP_TOXES; ++i) { @@ -318,8 +317,6 @@ static void group_state_test(AutoTox *autotoxes) } fprintf(stderr, "All tests passed!\n"); - -#endif /* VANILLA_NACL */ } int main(void) diff --git a/auto_tests/group_sync_test.c b/auto_tests/group_sync_test.c index 4d2bc186..d635f76f 100644 --- a/auto_tests/group_sync_test.c +++ b/auto_tests/group_sync_test.c @@ -330,7 +330,6 @@ static void topic_spam(const Random *rng, AutoTox *autotoxes, uint32_t num_peers static void group_sync_test(AutoTox *autotoxes) { -#ifndef VANILLA_NACL ck_assert(NUM_GROUP_TOXES >= 5); const Random *rng = system_random(); ck_assert(rng != nullptr); @@ -443,8 +442,6 @@ static void group_sync_test(AutoTox *autotoxes) } fprintf(stderr, "All tests passed!\n"); - -#endif // VANILLA_NACL } int main(void) diff --git a/auto_tests/group_tcp_test.c b/auto_tests/group_tcp_test.c index c1f66de9..43cfa666 100644 --- a/auto_tests/group_tcp_test.c +++ b/auto_tests/group_tcp_test.c @@ -8,8 +8,6 @@ #include "auto_test_support.h" -#ifdef USE_TEST_NETWORK - #define NUM_GROUP_TOXES 2 #define CODEWORD "RONALD MCDONALD" #define CODEWORD_LEN (sizeof(CODEWORD) - 1) @@ -129,7 +127,6 @@ static bool all_peers_got_code(AutoTox *autotoxes) static void group_tcp_test(AutoTox *autotoxes) { -#ifndef VANILLA_NACL ck_assert(NUM_GROUP_TOXES >= 2); State *state0 = (State *)autotoxes[0].state; @@ -222,34 +219,29 @@ static void group_tcp_test(AutoTox *autotoxes) } printf("Test passed!\n"); - -#endif // VANILLA_NACL } -#endif // USE_TEST_NETWORK -int main(void) +int main(int argc, char **argv) { -#ifdef USE_TEST_NETWORK // TODO(Jfreegman): Enable this test when the mainnet works with DHT groupchats setvbuf(stdout, nullptr, _IONBF, 0); - struct Tox_Options *options = (struct Tox_Options *)calloc(1, sizeof(struct Tox_Options)); + struct Tox_Options *options = tox_options_new(nullptr); ck_assert(options != nullptr); - tox_options_default(options); tox_options_set_udp_enabled(options, false); Run_Auto_Options autotest_opts = default_run_auto_options(); autotest_opts.graph = GRAPH_COMPLETE; - run_auto_test(options, NUM_GROUP_TOXES, group_tcp_test, sizeof(State), &autotest_opts); + // TODO(JFreegman): Fix this test and remove the "if". + if (argc > 2) { + run_auto_test(options, NUM_GROUP_TOXES, group_tcp_test, sizeof(State), &autotest_opts); + } tox_options_free(options); -#endif return 0; } -#ifdef USE_TEST_NETWORK -#undef NUM_GROUP_TOXES #undef CODEWORD_LEN #undef CODEWORD -#endif // USE_TEST_NETWORK +#undef NUM_GROUP_TOXES diff --git a/auto_tests/group_topic_test.c b/auto_tests/group_topic_test.c index 26dafcb4..60f78acf 100644 --- a/auto_tests/group_topic_test.c +++ b/auto_tests/group_topic_test.c @@ -203,7 +203,6 @@ static uint32_t set_topic_all_peers(const Random *rng, AutoTox *autotoxes, size_ static void group_topic_test(AutoTox *autotoxes) { -#ifndef VANILLA_NACL ck_assert_msg(NUM_GROUP_TOXES >= 3, "NUM_GROUP_TOXES is too small: %d", NUM_GROUP_TOXES); const Random *rng = system_random(); @@ -318,8 +317,6 @@ static void group_topic_test(AutoTox *autotoxes) } fprintf(stderr, "All tests passed!\n"); - -#endif /* VANILLA_NACL */ } int main(void) diff --git a/auto_tests/invalid_tcp_proxy_test.c b/auto_tests/invalid_tcp_proxy_test.c index 08869c59..43d3c62c 100644 --- a/auto_tests/invalid_tcp_proxy_test.c +++ b/auto_tests/invalid_tcp_proxy_test.c @@ -7,15 +7,8 @@ #include "auto_test_support.h" #include "check_compat.h" -static uint8_t const key[] = { - 0x15, 0xE9, 0xC3, 0x09, 0xCF, 0xCB, 0x79, 0xFD, - 0xDF, 0x0E, 0xBA, 0x05, 0x7D, 0xAB, 0xB4, 0x9F, - 0xE1, 0x5F, 0x38, 0x03, 0xB1, 0xBF, 0xF0, 0x65, - 0x36, 0xAE, 0x2E, 0x5B, 0xA5, 0xE4, 0x69, 0x0E, -}; - -// Try to bootstrap for 30 seconds. -#define NUM_ITERATIONS (unsigned)(30.0 / (ITERATION_INTERVAL / 1000.0)) +// Try to bootstrap for 20 seconds. +#define NUM_ITERATIONS (unsigned)(20.0 / (ITERATION_INTERVAL / 1000.0)) int main(void) { @@ -24,13 +17,12 @@ int main(void) struct Tox_Options *opts = tox_options_new(nullptr); tox_options_set_udp_enabled(opts, false); tox_options_set_proxy_type(opts, TOX_PROXY_TYPE_SOCKS5); - tox_options_set_proxy_host(opts, "localhost"); + tox_options_set_proxy_host(opts, "127.0.0.1"); tox_options_set_proxy_port(opts, 51724); Tox *tox = tox_new_log(opts, nullptr, nullptr); tox_options_free(opts); - tox_add_tcp_relay(tox, "tox.ngc.zone", 33445, key, nullptr); - tox_bootstrap(tox, "tox.ngc.zone", 33445, key, nullptr); + bootstrap_tox_live_network(tox, true); printf("Waiting for connection...\n"); diff --git a/auto_tests/invalid_udp_proxy_test.c b/auto_tests/invalid_udp_proxy_test.c index cf24be46..431ec59f 100644 --- a/auto_tests/invalid_udp_proxy_test.c +++ b/auto_tests/invalid_udp_proxy_test.c @@ -7,15 +7,8 @@ #include "auto_test_support.h" #include "check_compat.h" -static uint8_t const key[] = { - 0x15, 0xE9, 0xC3, 0x09, 0xCF, 0xCB, 0x79, 0xFD, - 0xDF, 0x0E, 0xBA, 0x05, 0x7D, 0xAB, 0xB4, 0x9F, - 0xE1, 0x5F, 0x38, 0x03, 0xB1, 0xBF, 0xF0, 0x65, - 0x36, 0xAE, 0x2E, 0x5B, 0xA5, 0xE4, 0x69, 0x0E, -}; - -// Try to bootstrap for 30 seconds. -#define NUM_ITERATIONS (unsigned)(30.0 / (ITERATION_INTERVAL / 1000.0)) +// Try to bootstrap for 20 seconds. +#define NUM_ITERATIONS (unsigned)(20.0 / (ITERATION_INTERVAL / 1000.0)) int main(void) { @@ -24,13 +17,12 @@ int main(void) struct Tox_Options *opts = tox_options_new(nullptr); tox_options_set_udp_enabled(opts, true); tox_options_set_proxy_type(opts, TOX_PROXY_TYPE_SOCKS5); - tox_options_set_proxy_host(opts, "localhost"); + tox_options_set_proxy_host(opts, "127.0.0.1"); tox_options_set_proxy_port(opts, 51724); Tox *tox = tox_new_log(opts, nullptr, nullptr); tox_options_free(opts); - tox_add_tcp_relay(tox, "tox.ngc.zone", 33445, key, nullptr); - tox_bootstrap(tox, "tox.ngc.zone", 33445, key, nullptr); + bootstrap_tox_live_network(tox, true); printf("Waiting for connection..."); diff --git a/auto_tests/send_message_test.c b/auto_tests/send_message_test.c index 52470f33..92079cb8 100644 --- a/auto_tests/send_message_test.c +++ b/auto_tests/send_message_test.c @@ -42,6 +42,7 @@ static void send_message_test(AutoTox *autotoxes) const size_t msgs_len = tox_max_message_length() + 1; uint8_t *msgs = (uint8_t *)malloc(msgs_len); + ck_assert(msgs != nullptr); memset(msgs, MESSAGE_FILLER, msgs_len); Tox_Err_Friend_Send_Message errm; diff --git a/auto_tests/tox_dispatch_test.c b/auto_tests/tox_dispatch_test.c index 98634369..ca50bb61 100644 --- a/auto_tests/tox_dispatch_test.c +++ b/auto_tests/tox_dispatch_test.c @@ -31,31 +31,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); - tox_events_get_bytes(events, bytes); + ck_assert(tox_events_get_bytes(events, bytes1)); + ck_assert(tox_events_get_bytes(events, bytes2)); - Tox_Events *events_copy = tox_events_load(sys, bytes, size); + // Make sure get_bytes is deterministic. + ck_assert(memcmp(bytes1, bytes2, size) == 0); + + 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)); @@ -73,7 +78,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); @@ -164,13 +171,13 @@ static void test_tox_events(void) static void fake_test_unpack(void) { // TODO(Green-Sky): add proper unpack tests and/or implement ngc events - (void)tox_unpack_group_privacy_state; - (void)tox_unpack_group_privacy_state; - (void)tox_unpack_group_voice_state; - (void)tox_unpack_group_topic_lock; - (void)tox_unpack_group_join_fail; - (void)tox_unpack_group_mod_event; - (void)tox_unpack_group_exit_type; + (void)tox_group_privacy_state_unpack; + (void)tox_group_privacy_state_unpack; + (void)tox_group_voice_state_unpack; + (void)tox_group_topic_lock_unpack; + (void)tox_group_join_fail_unpack; + (void)tox_group_mod_event_unpack; + (void)tox_group_exit_type_unpack; } int main(void) diff --git a/auto_tests/tox_events_test.c b/auto_tests/tox_events_test.c index 04549b21..918bc17e 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/cmake/CompileGTest.cmake b/cmake/CompileGTest.cmake deleted file mode 100644 index 9aa4712f..00000000 --- a/cmake/CompileGTest.cmake +++ /dev/null @@ -1,62 +0,0 @@ -# Find and compile the GTest library. - -include(CheckCXXCompilerFlag) -include(CheckIncludeFileCXX) - -message(STATUS "Checking for gtest") - -# Look for the sources. -find_file(GTEST_ALL_CC gtest-all.cc PATHS - ${CMAKE_SOURCE_DIR}/third_party/googletest/googletest/src - /usr/src/gtest/src - NO_DEFAULT_PATH -) - -if(GTEST_ALL_CC) - # ../.. from the source file is the source root. - get_filename_component(GTEST_SRC_DIR ${GTEST_ALL_CC} DIRECTORY) - get_filename_component(GTEST_SRC_ROOT ${GTEST_SRC_DIR} DIRECTORY) - - # Look for the header file. - include(CheckIncludeFileCXX) - include_directories(SYSTEM ${GTEST_SRC_ROOT}/include) - check_include_file_cxx("gtest/gtest.h" HAVE_GTEST_GTEST_H) - - if(HAVE_GTEST_GTEST_H) - message(STATUS "Found gtest: ${GTEST_SRC_ROOT}") - - add_library(gtest - ${GTEST_SRC_DIR}/gtest-all.cc - ${GTEST_SRC_DIR}/gtest_main.cc) - target_include_directories(gtest PRIVATE ${GTEST_SRC_ROOT}) - - # Ignore all warnings for gtest. We don't care about their implementation. - check_cxx_compiler_flag("-w" HAVE_CXX_W QUIET) - if(HAVE_CXX_W) - set_target_properties(gtest PROPERTIES COMPILE_FLAGS "-w") - endif() - - set(HAVE_GTEST TRUE) - set(TEST_CXX_FLAGS "") - - check_cxx_compiler_flag("-Wno-global-constructors" HAVE_CXX_W_NO_GLOBAL_CONSTRUCTORS QUIET) - if(HAVE_CXX_W_NO_GLOBAL_CONSTRUCTORS) - set(TEST_CXX_FLAGS "${TEST_CXX_FLAGS} -Wno-global-constructors") - endif() - - check_cxx_compiler_flag("-Wno-zero-as-null-pointer-constant" HAVE_CXX_W_NO_ZERO_AS_NULL_POINTER_CONSTANT QUIET) - if(HAVE_CXX_W_NO_ZERO_AS_NULL_POINTER_CONSTANT) - set(TEST_CXX_FLAGS "${TEST_CXX_FLAGS} -Wno-zero-as-null-pointer-constant") - endif() - endif() -endif() - -function(unit_test subdir target) - if(HAVE_GTEST) - add_executable(unit_${target}_test ${subdir}/${target}_test.cc) - target_link_modules(unit_${target}_test toxcore gtest) - set_target_properties(unit_${target}_test PROPERTIES COMPILE_FLAGS "${TEST_CXX_FLAGS}") - add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} unit_${target}_test) - set_property(TEST ${target} PROPERTY ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw") - endif() -endfunction() diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake index fb49944e..6f2ec906 100644 --- a/cmake/Dependencies.cmake +++ b/cmake/Dependencies.cmake @@ -8,19 +8,25 @@ include(ModulePackage) find_package(Threads REQUIRED) -find_library(NSL_LIBRARIES nsl ) -find_library(RT_LIBRARIES rt ) -find_library(SOCKET_LIBRARIES socket ) +find_library(NSL_LIBRARIES nsl ) +find_library(RT_LIBRARIES rt ) +find_library(SOCKET_LIBRARIES socket) # For toxcore. -pkg_use_module(LIBSODIUM libsodium ) +pkg_search_module(LIBSODIUM libsodium IMPORTED_TARGET) # For toxav. -pkg_use_module(OPUS "opus;Opus" ) -pkg_use_module(VPX "vpx;libvpx" ) +pkg_search_module(OPUS opus IMPORTED_TARGET) +if(NOT OPUS_FOUND) + pkg_search_module(OPUS Opus IMPORTED_TARGET) +endif() +pkg_search_module(VPX vpx IMPORTED_TARGET) +if(NOT VPX_FOUND) + pkg_search_module(VPX libvpx IMPORTED_TARGET) +endif() # For tox-bootstrapd. -pkg_use_module(LIBCONFIG libconfig ) +pkg_search_module(LIBCONFIG libconfig IMPORTED_TARGET) ############################################################################### # diff --git a/cmake/ModulePackage.cmake b/cmake/ModulePackage.cmake index 04dbf16d..cbc62f41 100644 --- a/cmake/ModulePackage.cmake +++ b/cmake/ModulePackage.cmake @@ -21,41 +21,6 @@ endif() find_package(PkgConfig) -function(pkg_use_module mod pkgs) - foreach(pkg IN ITEMS ${pkgs}) - if(PKG_CONFIG_FOUND) - pkg_search_module(${mod} ${pkg}) - endif() - if(NOT ${mod}_FOUND) - find_package(${pkg} QUIET) - # This is very very ugly, but the variables are sometimes used in this scope - # and sometimes in the parent scope, so we have to set them to both places. - set(${mod}_FOUND ${${pkg}_FOUND}) - set(${mod}_FOUND ${${pkg}_FOUND} PARENT_SCOPE) - set(${mod}_LIBRARIES ${${pkg}_LIBS}) - set(${mod}_LIBRARIES ${${pkg}_LIBS} PARENT_SCOPE) - set(${mod}_LIBRARY_DIRS ${${pkg}_LIBRARY_DIRS}) - set(${mod}_LIBRARY_DIRS ${${pkg}_LIBRARY_DIRS} PARENT_SCOPE) - set(${mod}_INCLUDE_DIRS ${${pkg}_INCLUDE_DIRS}) - set(${mod}_INCLUDE_DIRS ${${pkg}_INCLUDE_DIRS} PARENT_SCOPE) - endif() - if(${mod}_FOUND) - link_directories(${${mod}_LIBRARY_DIRS}) - include_directories(${${mod}_INCLUDE_DIRS}) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${${mod}_CFLAGS_OTHER}" PARENT_SCOPE) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${${mod}_CFLAGS_OTHER}" PARENT_SCOPE) - - if(NOT MSVC) - foreach(dir ${${mod}_INCLUDE_DIRS}) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem ${dir}" PARENT_SCOPE) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem ${dir}" PARENT_SCOPE) - endforeach() - endif() - break() - endif() - endforeach() -endfunction() - function(add_module lib) set(${lib}_SOURCES ${ARGN} PARENT_SCOPE) @@ -70,7 +35,7 @@ function(add_module lib) endfunction() function(install_module lib) - if(ENABLE_SHARED) + if(TARGET ${lib}_shared) set_target_properties(${lib}_shared PROPERTIES VERSION ${SOVERSION} SOVERSION ${SOVERSION_MAJOR} @@ -80,7 +45,7 @@ function(install_module lib) LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) endif() - if(ENABLE_STATIC) + if(TARGET ${lib}_static) install(TARGETS ${lib}_static RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} @@ -113,59 +78,3 @@ function(install_module lib) install(FILES ${header} ${ARGN}) endforeach() endfunction() - -function(target_link_modules target) - # If the target we're adding dependencies to is a shared library, add it to - # the set of targets. - if(TARGET ${target}_shared) - set(_targets ${_targets} ${target}_shared) - # Shared libraries should first try to link against other shared libraries. - set(${target}_shared_primary shared) - # If that fails (because the shared target doesn't exist), try linking - # against the static library. This requires the static library's objects to - # be PIC. - set(${target}_shared_secondary static) - endif() - # It can also be a static library at the same time. - if(TARGET ${target}_static) - set(_targets ${_targets} ${target}_static) - # Static libraries aren't actually linked, but their dependencies are - # recorded by "linking" them. If we link an executable to a static library, - # we want to also link statically against its transitive dependencies. - set(${target}_static_primary static) - # If a dependency doesn't exist as static library, we link against the - # shared one. - set(${target}_static_secondary shared) - endif() - # If it's neither, then it's an executable. - if(NOT _targets) - set(_targets ${_targets} ${target}) - # Executables preferably link against static libraries, so they are - # standalone and can be shipped without any external dependencies. As a - # frame of reference: tests become roughly 600-800K binaries instead of - # 50-100K on x86_64 Linux. - set(${target}_primary static) - set(${target}_secondary shared) - endif() - - foreach(dep ${ARGN}) - foreach(_target ${_targets}) - if(TARGET ${dep}_${${_target}_primary}) - target_link_libraries(${_target} ${dep}_${${_target}_primary}) - elseif(TARGET ${dep}_${${_target}_secondary}) - target_link_libraries(${_target} ${dep}_${${_target}_secondary}) - else() - # We record the modules linked to this target, so that we can collect - # them later when linking a composed module. - list(FIND LINK_MODULES ${dep} _index) - if(_index EQUAL -1) - set(LINK_MODULES ${LINK_MODULES} ${dep}) - endif() - - target_link_libraries(${_target} ${dep}) - endif() - endforeach() - endforeach() - - set(${target}_LINK_MODULES ${${target}_LINK_MODULES} ${LINK_MODULES} PARENT_SCOPE) -endfunction() diff --git a/configure.ac b/configure.ac index 1afb0a75..8f9a50bb 100644 --- a/configure.ac +++ b/configure.ac @@ -22,8 +22,6 @@ BUILD_AV="yes" BUILD_TESTING="yes" LIBCONFIG_FOUND="no" -WANT_NACL="no" -ADD_NACL_OBJECTS_TO_PKGCONFIG="yes" SET_SO_VERSION="yes" AC_ARG_ENABLE([soname-versions], @@ -38,26 +36,11 @@ AC_ARG_ENABLE([soname-versions], ) AM_CONDITIONAL(SET_SO_VERSION, test "x$SET_SO_VERSION" = "xyes") -AC_ARG_ENABLE([nacl], - [AC_HELP_STRING([--enable-nacl], [use nacl instead of libsodium (default: disabled)]) ], - [ - if test "x$enableval" = "xno"; then - WANT_NACL="no" - elif test "x$enableval" = "xyes"; then - WANT_NACL="yes" - fi - ] -) - AC_ARG_ENABLE([randombytes-stir], [AC_HELP_STRING([--enable-randombytes-stir], [use randombytes_stir() instead of sodium_init() for faster startup on android (default: disabled)]) ], [ if test "x$enableval" = "xyes"; then - if test "x$WANT_NACL" = "xyes"; then - AC_MSG_WARN([randombytes_stir() is not available with NaCl library]) - else - AC_DEFINE([USE_RANDOMBYTES_STIR], [1], [randombytes_stir() instead of sodium_init()]) - fi + AC_DEFINE([USE_RANDOMBYTES_STIR], [1], [randombytes_stir() instead of sodium_init()]) fi ] ) @@ -171,16 +154,6 @@ if test "$use_ipv6" != "yes"; then AC_DEFINE([USE_IPV6],[0],[define to 0 to force ipv4]) fi -AC_ARG_ENABLE([[test_network]], - [AS_HELP_STRING([[--enable-test-network[=ARG]]], [build tox for a test network incompatible with the main DHT [no]])], - [use_test_network=${enableval}], - [use_test_network='no'] - ) - -if test "$use_test_network" == "yes"; then - AC_DEFINE([USE_TEST_NETWORK],[1],[define to 1 to enable the test network]) -fi - AX_HAVE_EPOLL if test "$enable_epoll" != "no"; then if test "${ax_cv_have_epoll}" = "yes"; then @@ -197,8 +170,6 @@ fi DEPSEARCH= LIBSODIUM_SEARCH_HEADERS= LIBSODIUM_SEARCH_LIBS= -NACL_SEARCH_HEADERS= -NACL_SEARCH_LIBS= AC_ARG_WITH(dependency-search, AC_HELP_STRING([--with-dependency-search=DIR], @@ -216,24 +187,6 @@ if test -n "$DEPSEARCH"; then export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$DEPSEARCH/lib/pkgconfig fi -AC_ARG_WITH(nacl-headers, - AC_HELP_STRING([--with-nacl-headers=DIR], - [search for nacl header files in DIR]), - [ - NACL_SEARCH_HEADERS="$withval" - AC_MSG_NOTICE([will search for nacl header files in $withval]) - ] -) - -AC_ARG_WITH(nacl-libs, - AC_HELP_STRING([--with-nacl-libs=DIR], - [search for nacl libraries in DIR]), - [ - NACL_SEARCH_LIBS="$withval" - AC_MSG_NOTICE([will search for nacl libraries in $withval]) - ] -) - AC_ARG_WITH(libsodium-headers, AC_HELP_STRING([--with-libsodium-headers=DIR], [search for libsodium header files in DIR]), @@ -252,11 +205,6 @@ AC_ARG_WITH(libsodium-libs, ] ) -if test "x$WANT_NACL" = "xyes"; then - enable_shared=no - enable_static=yes -fi - # Checks for programs. AC_PROG_CC_C99 @@ -287,7 +235,6 @@ case $host_os in LDFLAGS="$LDFLAGS -L/usr/local/lib" CFLAGS="$CFLAGS -I/usr/local/include" CPPFLAGS="$CPPFLAGS -I/usr/local/include" - ADD_NACL_OBJECTS_TO_PKGCONFIG="no" ;; darwin*) MACH=yes @@ -312,50 +259,7 @@ PKG_CHECK_MODULES([LIBSODIUM], [libsodium], LIBSODIUM_FOUND="no" ]) -if test "x$WANT_NACL" = "xyes"; then - NACL_LIBS= - NACL_LDFLAGS= - NACL_OBJECTS= - NACL_OBJECTS_PKGCONFIG= - LDFLAGS_SAVE="$LDFLAGS" - if test -n "$NACL_SEARCH_LIBS"; then - LDFLAGS="-L$NACL_SEARCH_LIBS $LDFLAGS" - AC_CHECK_LIB(nacl, random, - [ - NACL_LDFLAGS="-L$NACL_SEARCH_LIBS" - NACL_LIBS="-lnacl" - ], - [ - AC_MSG_ERROR([library nacl was not found in requested location $NACL_SEARCH_LIBS]) - ] - ) - else - AC_CHECK_LIB(nacl, random, - [], - [ - AC_MSG_ERROR([you enabled nacl support, but library nacl was not found on your system]) - ] - ) - fi - - if (test -f "$NACL_SEARCH_LIBS/cpucycles.o") && - (test -f "$NACL_SEARCH_LIBS/randombytes.o"); then - NACL_OBJECTS="$NACL_SEARCH_LIBS/cpucycles.o $NACL_SEARCH_LIBS/randombytes.o" - AC_MSG_NOTICE([found extra NaCl objects: $NACL_OBJECTS]) - if test "x$ADD_NACL_OBJECTS_TO_PKGCONFIG" = "xyes"; then - AC_MSG_NOTICE([adding extra NaCl objects to pkg-config file]) - NACL_OBJECTS_PKGCONFIG="$NACL_OBJECTS" - fi - else - AC_MSG_ERROR([required NaCl object files cpucycles.o randombytes.o not found, please specify their location using the --with-nacl-libs parameter]) - fi - - LDFLAGS="$LDFLAGS_SAVE" - AC_SUBST(NACL_LIBS) - AC_SUBST(NACL_LDFLAGS) - AC_SUBST(NACL_OBJECTS) - AC_SUBST(NACL_OBJECTS_PKGCONFIG) -elif test "x$LIBSODIUM_FOUND" = "xno"; then +if test "x$LIBSODIUM_FOUND" = "xno"; then LIBSODIUM_LIBS= LIBSODIUM_LDFLAGS= LDFLAGS_SAVE="$LDFLAGS" @@ -388,34 +292,7 @@ fi AC_CHECK_HEADERS([arpa/inet.h fcntl.h netdb.h netinet/in.h stdint.h stdlib.h string.h sys/socket.h sys/time.h unistd.h]) -if test "x$WANT_NACL" = "xyes"; then - NACL_CFLAGS= - CFLAGS_SAVE="$CFLAGS" - CPPFLAGS_SAVE="$CPPFLAGS" - if test -n "$NACL_SEARCH_HEADERS"; then - CFLAGS="-I$NACL_SEARCH_HEADERS $CFLAGS" - CPPFLAGS="-I$NACL_SEARCH_HEADERS $CPPFLAGS" - AC_CHECK_HEADER(crypto_box.h, - [ - NACL_CFLAGS="-I$NACL_SEARCH_HEADERS" - ], - [ - AC_MSG_ERROR([header files for library nacl were not found in requested location $NACL_SEARCH_HEADERS]) - ] - ) - else - AC_CHECK_HEADER(crypto_box.h, - [], - [ - AC_MSG_ERROR([you enabled nacl support, but nacl header files were not found on your system]) - ] - ) - fi - CFLAGS="$CFLAGS_SAVE" - CPPFLAGS="$CPPFLAGS_SAVE" - AC_SUBST(NACL_CFLAGS) - AC_DEFINE([VANILLA_NACL], [1], [use nacl instead of libsodium]) -elif test "x$LIBSODIUM_FOUND" = "xno"; then +if test "x$LIBSODIUM_FOUND" = "xno"; then LIBSODIUM_CFLAGS= CFLAGS_SAVE="$CFLAGS" CPPFLAGS_SAVE="$CPPFLAGS" @@ -555,7 +432,6 @@ AM_CONDITIONAL(BUILD_DHT_BOOTSTRAP, test "x$BUILD_DHT_BOOTSTRAP" = "xyes") AM_CONDITIONAL(BUILD_TESTS, test "x$BUILD_TESTS" = "xyes") AM_CONDITIONAL(BUILD_AV, test "x$BUILD_AV" = "xyes") AM_CONDITIONAL(BUILD_TESTING, test "x$BUILD_TESTING" = "xyes") -AM_CONDITIONAL(WITH_NACL, test "x$WANT_NACL" = "xyes") AM_CONDITIONAL(WIN32, test "x$WIN32" = "xyes") AC_CONFIG_FILES([Makefile diff --git a/docs/TCP_Network.txt b/docs/TCP_Network.txt index aa2fd46f..761947a5 100644 --- a/docs/TCP_Network.txt +++ b/docs/TCP_Network.txt @@ -1,100 +1,100 @@ -It has come to our attention that to achieve decent market penetration Tox -must work behind ALL internet connections, may they be behind enterprise NATs -or any other bad network conditions. +It has come to our attention that to achieve decent market penetration Tox +must work behind ALL internet connections, may they be behind enterprise NATs +or any other bad network conditions. -The people who have issues with the UDP direct connection approach seem to be a +The people who have issues with the UDP direct connection approach seem to be a small minority though it is hard to estimate how many. -This means that routing their packets using good nodes on the network will +This means that routing their packets using good nodes on the network will probably not take a huge toll on the network and will assure that people can use Tox regardless of the quality of their internet connection. How it's going to work: -1. Alice, a Tox client on a TCP only network generates a temporary public key +1. Alice, a Tox client on a TCP only network generates a temporary public key and connects to a bootstrap node. -2. Using the bootstrap node she finds and connects to a couple (exact number +2. Using the bootstrap node she finds and connects to a couple (exact number to be determined later) number of random nodes that have TCP relay support. -3. She uses the onion through the TCP relay connections to send friend requests -or tell online friends which TCP nodes she is connected to and her temporary +3. She uses the onion through the TCP relay connections to send friend requests +or tell online friends which TCP nodes she is connected to and her temporary public key. -4. Bob receives an onion packet from Alice telling him which nodes she is -connected to. Bob connects to these nodes and establishes a routed connection +4. Bob receives an onion packet from Alice telling him which nodes she is +connected to. Bob connects to these nodes and establishes a routed connection with Alice using that temporary public key. -5. That connection is used by both to transmit encrypted Messenger and A/V +5. That connection is used by both to transmit encrypted Messenger and A/V packets. -6. If one of the nodes shuts down while it is currently routing traffic, Alice +6. If one of the nodes shuts down while it is currently routing traffic, Alice and bob just switch to one of the other nodes they are both connected to. Detailed implementation details: -There are two distinct parts for TCP relays, the client part and the server +There are two distinct parts for TCP relays, the client part and the server part. -The server acts as the actual relay. Servers must have fully forwarded TCP -ports (NAT-PMP and uPNP can help here). The first port the server will try -binding to is 443 followed by port 3389 and possibly some others. Onion packets +The server acts as the actual relay. Servers must have fully forwarded TCP +ports (NAT-PMP and uPNP can help here). The first port the server will try +binding to is 443 followed by port 3389 and possibly some others. Onion packets can be sent/received through the TCP servers. Server: -The public/private key pair the TCP server uses is the same one he uses for the -DHT. +The public/private key pair the TCP server uses is the same one he uses for the +DHT. -all crypto for communication with the server uses the crypto_box() function of -NaCl. +all crypto for communication with the server uses the crypto_box() function of +libsodium. -TCP doesn't have packets so what we will refer to as packets are sent this way: +TCP doesn't have packets so what we will refer to as packets are sent this way: [[uint16_t (length of data)][data]] So if you would inspect the TCP stream you would see: -[[uint16_t (length of data)][data]][[uint16_t (length of +[[uint16_t (length of data)][data]][[uint16_t (length of data)][data]][[uint16_t (length of data)][data]] -Note that both handshake packets don't have this format (the length for them is +Note that both handshake packets don't have this format (the length for them is always the same so we don't need to specify it.) When the client connects to the server, he sends this packet: -[public key of client (32 bytes)][nonce for the encrypted data [24 -bytes]][encrypted with the private key of the client and public key of the -server and the nonce:[public key (32 bytes) and][base nonce we want the server +[public key of client (32 bytes)][nonce for the encrypted data [24 +bytes]][encrypted with the private key of the client and public key of the +server and the nonce:[public key (32 bytes) and][base nonce we want the server to use to encrypt the packets sent to us (24 bytes)]] The server responds with: -[nonce for the encrypted data [24 bytes]][encrypted with the public key of the -client and private key of the server and the nonce:[public key (32 bytes) -and][base nonce we want the client to use to encrypt the packets sent to us (24 +[nonce for the encrypted data [24 bytes]][encrypted with the public key of the +client and private key of the server and the nonce:[public key (32 bytes) +and][base nonce we want the client to use to encrypt the packets sent to us (24 bytes)]] -All packets to the server are end to end encrypted with the information -received +All packets to the server are end to end encrypted with the information +received (and sent) in the handshake. -(first packet is encrypted with the base nonce the private key for which the -client sent the server the public key and the public key we sent to the client, +(first packet is encrypted with the base nonce the private key for which the +client sent the server the public key and the public key we sent to the client, the next with base nonce + 1...) -The connection is set to an unconfirmed state until a packet is received and +The connection is set to an unconfirmed state until a packet is received and decrypted correctly using the information in the handshake. -each packet sent to/from the server has an id (the first byte of the plain text +each packet sent to/from the server has an id (the first byte of the plain text data of the packet.) -ids 0 to 15 are reserved for special packets, ids 16 to 255 are used to denote +ids 0 to 15 are reserved for special packets, ids 16 to 255 are used to denote who we want the data to be routed to/who the packet is from. special ids and packets: 0 - Routing request. [uint8_t id (0)][public key (32 bytes)] 1 - Routing request response. -[uint8_t id (1)][uint8_t (rpid) 0 if refused, packet id if accepted][public key +[uint8_t id (1)][uint8_t (rpid) 0 if refused, packet id if accepted][public key (32 bytes)] 2 - Connect notification: [uint8_t id (2)][uint8_t (packet id of connection that got connected)] @@ -108,44 +108,44 @@ special ids and packets: [uint8_t id (6)][destination public key (32 bytes)][data] 7 - OOB recv [uint8_t id (7)][senders public key (32 bytes)][data] -8 - onion packet (same format as initial onion packet (See: Prevent +8 - onion packet (same format as initial onion packet (See: Prevent tracking.txt) but packet id is 8 instead of 128) -9 - onion packet response (same format as onion packet with id 142 but id is 9 +9 - onion packet response (same format as onion packet with id 142 but id is 9 instead.) The rest of the special ids are reserved for possible future usage. -If the server receives a routing request he stores server side that the client -wants to connect to the person with that public key and sends back a Routing +If the server receives a routing request he stores server side that the client +wants to connect to the person with that public key and sends back a Routing request response with the rpid along with the public key sent in the request. -If for some reason the server must refuse the routing request (too many) he +If for some reason the server must refuse the routing request (too many) he sends the response with a rpid of 0. -If the person who the client wants to connect to is also online and wants to -connect to the client a connect notification is sent to both with the +If the person who the client wants to connect to is also online and wants to +connect to the client a connect notification is sent to both with the appropriate packet id. -If either one disconnects, a disconnect notification is sent to the other with +If either one disconnects, a disconnect notification is sent to the other with appropriate packet id. -If a client sends a disconnect notification, the entry on the server for that -routed connection is cleared and a disconnect notification is sent to the peer +If a client sends a disconnect notification, the entry on the server for that +routed connection is cleared and a disconnect notification is sent to the peer (if he was online) -If the server receives an onion packet he handles it the same as he would if it -was one received normally via UDP, he must also assure himself that any +If the server receives an onion packet he handles it the same as he would if it +was one received normally via UDP, he must also assure himself that any responses must be sent to the proper client. Ping responses must have the same ping_id as the request. If the server receives a ping packet he must respond with a ping response. -The server will send a ping packet to clients every 30 seconds, they have 30 +The server will send a ping packet to clients every 30 seconds, they have 30 seconds to respond, if they don't the connection is deleted. -OOB send packets will be sent to the peer connected to the TCP server with the -destination public key as a OOB recv packet. The client sending this packet has +OOB send packets will be sent to the peer connected to the TCP server with the +destination public key as a OOB recv packet. The client sending this packet has no way of knowing if the packet reached its destination. diff --git a/docs/updates/Crypto.md b/docs/updates/Crypto.md index fd169a2e..1a0eda5a 100644 --- a/docs/updates/Crypto.md +++ b/docs/updates/Crypto.md @@ -1,7 +1,7 @@ -Encryption library used: http://nacl.cr.yp.to/ +Encryption library used: https://doc.libsodium.org/ -When running the program for the first time the crypto_box_keypair() function is used to +When running the program for the first time the crypto_box_keypair() function is used to generate the users public-private key pair. (32 bytes each) The generated public key is set as the client_id of the peer. @@ -9,13 +9,13 @@ The generated public key is set as the client_id of the peer. Adding a friend --------------- -Alice adds Bob to her friend list by adding his 32 byte public key (client_id) to her friend list. -2 cases: -case 1: Alice adds the public key of Bob, then Bob waits for Alice to attempt to connect to him. -case 2: Bob and Alice add their respective public keys to their friend lists at the same time. - -case 1: -Alice sends an onion data (see: Prevent_tracking.txt) packet to Bob with the encrypted part containing the friend request like so: +Alice adds Bob to her friend list by adding his 32 byte public key (client_id) to her friend list. +2 cases: +case 1: Alice adds the public key of Bob, then Bob waits for Alice to attempt to connect to him. +case 2: Bob and Alice add their respective public keys to their friend lists at the same time. + +case 1: +Alice sends an onion data (see: Prevent_tracking.txt) packet to Bob with the encrypted part containing the friend request like so: ``` [char with a value of 32][nospam number (4 bytes)][Message] ``` @@ -23,15 +23,15 @@ Alice sends an onion data (see: Prevent_tracking.txt) packet to Bob with the enc Ex message: hello Bob it's me Alice -_- add me pl0x. For more info on the nospam see: Spam_Prevention.txt - -Bob receives the request and decrypts the message using the function crypto_box_open() - -If the message decrypts successfully: -If Alice is already in Bob's friend list: case 2 -If Alice is not in Bob's friend list and the nospam is good: Bob is prompt to add Alice and is shown the message from her. -If Bob accepts Alice friend request he adds her public key to his friend list. -case 2: +Bob receives the request and decrypts the message using the function crypto_box_open() + +If the message decrypts successfully: +If Alice is already in Bob's friend list: case 2 +If Alice is not in Bob's friend list and the nospam is good: Bob is prompt to add Alice and is shown the message from her. +If Bob accepts Alice friend request he adds her public key to his friend list. + +case 2: Bob and Alice both have the others public key in their friend list, they are ready for the next step: Connecting to an already added friend In the next step only crypto_box() is used for encryption and only crypto_box_open() for decryption (just like in the last step.) diff --git a/libtoxcore.pc.in b/libtoxcore.pc.in index d9d7fb79..f42590e6 100644 --- a/libtoxcore.pc.in +++ b/libtoxcore.pc.in @@ -7,5 +7,5 @@ Name: libtoxcore Description: Tox protocol library Requires: Version: @PACKAGE_VERSION@ -Libs: @NACL_OBJECTS_PKGCONFIG@ -L${libdir} -ltoxcore @NACL_LDFLAGS@ -ltoxencryptsave @NACL_LIBS@ @LIBS@ @MATH_LDFLAGS@ @PTHREAD_LDFLAGS@ +Libs: -L${libdir} -ltoxcore -ltoxencryptsave @LIBS@ @MATH_LDFLAGS@ @PTHREAD_LDFLAGS@ Cflags: -I${includedir} diff --git a/other/Makefile.inc b/other/Makefile.inc index 6d4ec593..7f2fe86b 100644 --- a/other/Makefile.inc +++ b/other/Makefile.inc @@ -9,16 +9,12 @@ DHT_bootstrap_SOURCES = ../other/DHT_bootstrap.c \ ../other/bootstrap_node_packets.c DHT_bootstrap_CFLAGS = -I$(top_srcdir)/other \ - $(LIBSODIUM_CFLAGS) \ - $(NACL_CFLAGS) + $(LIBSODIUM_CFLAGS) DHT_bootstrap_LDADD = $(LIBSODIUM_LDFLAGS) \ - $(NACL_LDFLAGS) \ libmisc_tools.la \ libtoxcore.la \ $(LIBSODIUM_LIBS) \ - $(NACL_OBJECTS) \ - $(NACL_LIBS) \ $(WINSOCK2_LIBS) endif diff --git a/other/analysis/run-cppcheck b/other/analysis/run-cppcheck index c269c4e1..9dc6f179 100755 --- a/other/analysis/run-cppcheck +++ b/other/analysis/run-cppcheck @@ -8,9 +8,12 @@ set -e CPPCHECK=("--enable=all") CPPCHECK+=("--inconclusive") +CPPCHECK+=("--check-level=exhaustive") +CPPCHECK+=("--inline-suppr") +CPPCHECK+=("--library=other/docker/cppcheck/toxcore.cfg") CPPCHECK+=("--error-exitcode=1") -# Used for VLA. -CPPCHECK+=("--suppress=allocaCalled") +# We don't cast function pointers, which cppcheck suggests here. +CPPCHECK+=("--suppress=constParameterCallback") # False positives in switch statements. CPPCHECK+=("--suppress=knownConditionTrueFalse") # Cppcheck does not need standard library headers to get proper results. @@ -19,27 +22,22 @@ CPPCHECK+=("--suppress=missingIncludeSystem") CPPCHECK+=("--suppress=signConversion") # TODO(iphydf): Fixed in the toxav refactor PR. CPPCHECK+=("--suppress=redundantAssignment") -# We have some redundant nullptr checks in assertions -CPPCHECK+=("--suppress=nullPointerRedundantCheck") -# Triggers a false warning in group.c -CPPCHECK+=("--suppress=AssignmentAddressToInteger") -# TODO(sudden6): This triggers a false positive, check again later to enable it -CPPCHECK+=("--suppress=arrayIndexOutOfBoundsCond") - -# We're a library. This only works on whole programs. -CPPCHECK_C=("--suppress=unusedFunction") +# We use this for VLAs. +CPPCHECK_CXX+=("--suppress=allocaCalled") # False positive in auto_tests. -CPPCHECK_CXX+=("--suppress=shadowArgument") CPPCHECK_CXX+=("--suppress=shadowFunction") -# False positive for callback functions -CPPCHECK_CXX+=("--suppress=constParameter") +# False positive in group.c. +# Using cppcheck-suppress claims the suppression is unused. +CPPCHECK_CXX+=("--suppress=AssignmentAddressToInteger") +# We use C style casts because we write C code. +CPPCHECK_CXX+=("--suppress=cstyleCast") # Used in Messenger.c for a static_assert(...) CPPCHECK_CXX+=("--suppress=sizeofFunctionCall") run() { echo "Running cppcheck in variant '$*'" - cppcheck "${CPPCHECK[@]}" "${CPPCHECK_C[@]}" tox*/*.[ch] tox*/*/*.[ch] "${CPPFLAGS[@]}" "$@" + cppcheck -j8 "${CPPCHECK[@]}" "${CPPCHECK_C[@]}" tox*/*.[ch] tox*/*/*.[ch] "${CPPFLAGS[@]}" "$@" cppcheck "${CPPCHECK[@]}" "${CPPCHECK_CXX[@]}" amalgamation.cc "${CPPFLAGS[@]}" "$@" } diff --git a/other/bootstrap_daemon/CMakeLists.txt b/other/bootstrap_daemon/CMakeLists.txt index b2de8ace..52d8c5d6 100644 --- a/other/bootstrap_daemon/CMakeLists.txt +++ b/other/bootstrap_daemon/CMakeLists.txt @@ -19,6 +19,14 @@ add_executable(tox-bootstrapd src/tox-bootstrapd.c ../bootstrap_node_packets.c ../bootstrap_node_packets.h) -target_link_modules(tox-bootstrapd toxcore ${LIBCONFIG_LIBRARIES}) +target_link_libraries(tox-bootstrapd PRIVATE ${LIBCONFIG_LIBRARIES}) +target_link_directories(tox-bootstrapd PRIVATE ${LIBCONFIG_LIBRARY_DIRS}) +target_include_directories(tox-bootstrapd SYSTEM PRIVATE ${LIBCONFIG_INCLUDE_DIRS}) +target_compile_options(tox-bootstrapd PRIVATE ${LIBCONFIG_CFLAGS_OTHER}) +if(TARGET toxcore_static) + target_link_libraries(tox-bootstrapd PRIVATE toxcore_static) +else() + target_link_libraries(tox-bootstrapd PRIVATE toxcore_shared) +endif() install(TARGETS tox-bootstrapd RUNTIME DESTINATION bin) install(FILES bash-completion/completions/tox-bootstrapd DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/bash-completion/completions") diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index 7b49f7c4..f8d00e17 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -21cf23b1a2e46712663dc4f8daa322991af51b9e82626a127cf1bc8dc583b598 /usr/local/bin/tox-bootstrapd +b0bd5099f2f77fbd540a5a929a23cece39ff95e3a66702a5381342d01775cbd3 /usr/local/bin/tox-bootstrapd diff --git a/other/bootstrap_daemon/src/Makefile.inc b/other/bootstrap_daemon/src/Makefile.inc index 4babf8c8..5a9b021b 100644 --- a/other/bootstrap_daemon/src/Makefile.inc +++ b/other/bootstrap_daemon/src/Makefile.inc @@ -24,17 +24,13 @@ tox_bootstrapd_SOURCES = \ tox_bootstrapd_CFLAGS = \ -I$(top_srcdir)/other/bootstrap_daemon \ $(LIBSODIUM_CFLAGS) \ - $(NACL_CFLAGS) \ $(LIBCONFIG_CFLAGS) tox_bootstrapd_LDADD = \ $(LIBSODIUM_LDFLAGS) \ - $(NACL_LDFLAGS) \ libtoxcore.la \ $(LIBCONFIG_LIBS) \ - $(LIBSODIUM_LIBS) \ - $(NACL_OBJECTS) \ - $(NACL_LIBS) + $(LIBSODIUM_LIBS) bashcompdir = $(datarootdir)/bash-completion/completions dist_bashcomp_DATA = $(top_builddir)/other/bootstrap_daemon/bash-completion/completions/tox-bootstrapd diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index 5227db78..9330e8ce 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -57,7 +57,7 @@ static void sleep_milliseconds(uint32_t ms) // returns 1 on success // 0 on failure - no keys were read or stored -static int manage_keys(DHT *dht, char *keys_file_path) +static int manage_keys(DHT *dht, const char *keys_file_path) { enum { KEYS_SIZE = CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE }; uint8_t keys[KEYS_SIZE]; diff --git a/other/docker/autotools/Dockerfile b/other/docker/autotools/Dockerfile index 499ecedc..e61c83f1 100644 --- a/other/docker/autotools/Dockerfile +++ b/other/docker/autotools/Dockerfile @@ -1,6 +1,27 @@ ################################################ # autotools-linux -FROM toxchat/nacl:latest +FROM ubuntu:22.04 + +RUN apt-get update && apt-get install --no-install-recommends -y \ + autoconf \ + automake \ + ca-certificates \ + curl \ + libconfig-dev \ + libopus-dev \ + libsodium-dev \ + libtool \ + libvpx-dev \ + make \ + pkg-config \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +RUN groupadd -r -g 1000 builder \ + && useradd -m --no-log-init -r -g builder -u 1000 builder +USER builder + +WORKDIR /home/builder # Copy the sources and run the build. COPY --chown=builder:builder . /home/builder/c-toxcore/ diff --git a/other/docker/coverage/Dockerfile b/other/docker/coverage/Dockerfile index 031afc7b..56f88064 100644 --- a/other/docker/coverage/Dockerfile +++ b/other/docker/coverage/Dockerfile @@ -1,13 +1,15 @@ FROM toxchat/c-toxcore:sources AS src -FROM ubuntu:22.04 AS build +FROM ubuntu:20.04 AS build RUN apt-get update && \ DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \ + ca-certificates \ clang \ cmake \ + curl \ gcc \ git \ - golang \ + golang-1.18 \ libconfig-dev \ libgtest-dev \ libopus-dev \ @@ -22,10 +24,16 @@ RUN apt-get update && \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* \ && pip3 install --no-cache-dir gcovr +RUN ["strip", "-g",\ + "/usr/lib/x86_64-linux-gnu/libgtest.a",\ + "/usr/lib/x86_64-linux-gnu/libgtest_main.a"] +RUN ["curl", "-s", "https://codecov.io/bash", "-o", "/usr/local/bin/codecov"] +RUN ["chmod", "+x", "/usr/local/bin/codecov"] ENV CC=clang \ CXX=clang++ \ - PYTHONUNBUFFERED=1 + PYTHONUNBUFFERED=1 \ + PATH=$PATH:/usr/lib/go-1.18/bin SHELL ["/bin/bash", "-c"] WORKDIR /work @@ -41,26 +49,27 @@ RUN source .github/scripts/flags-coverage.sh \ -DENABLE_SHARED=OFF \ -DMIN_LOGGER_LEVEL=TRACE \ -DMUST_BUILD_TOXAV=ON \ - -DNON_HERMETIC_TESTS=ON \ + -DNON_HERMETIC_TESTS=OFF \ -DSTRICT_ABI=ON \ -DAUTOTEST=ON \ -DPROXY_TEST=ON \ -DUSE_IPV6=OFF \ - -DTEST_TIMEOUT_SECONDS=30 \ + -DTEST_TIMEOUT_SECONDS=40 \ && cmake --build _build --parallel 8 --target install WORKDIR /work/_build -RUN /work/proxy_server \ - & ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6 +RUN /work/other/proxy/proxy_server \ + & (ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6 || \ + ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6) WORKDIR /work/mallocfail RUN ["git", "clone", "--depth=1", "https://github.com/ralight/mallocfail", "/work/mallocfail"] -COPY run_mallocfail /usr/local/bin/ -COPY syscall_funcs.c src/ +COPY other/docker/coverage/syscall_funcs.c src/ RUN gcc -fPIC -shared -O2 -g3 -Wall -Ideps/uthash -Ideps/sha3 deps/*/*.c src/*.c -o mallocfail.so -ldl -lbacktrace \ && install mallocfail.so /usr/local/lib/mallocfail.so WORKDIR /work/_build +COPY other/docker/coverage/run_mallocfail /usr/local/bin/ RUN ["run_mallocfail", "--ctest=2", "--jobs=8"] RUN ["gcovr", \ "--sort-percentage", \ @@ -74,6 +83,4 @@ RUN ["gcovr", \ "--exclude=(.+/)?other/", \ "--exclude=(.+/)?testing/"] -FROM nginx:alpine -COPY --from=build /work/_build/html/coverage_details.html /usr/share/nginx/html/index.html -COPY --from=build /work/_build/html/ /usr/share/nginx/html/ +WORKDIR /work diff --git a/other/docker/coverage/Dockerfile.nginx b/other/docker/coverage/Dockerfile.nginx new file mode 100644 index 00000000..5e085978 --- /dev/null +++ b/other/docker/coverage/Dockerfile.nginx @@ -0,0 +1,5 @@ +# vim:ft=dockerfile +FROM toxchat/c-toxcore:coverage AS build +FROM nginx:alpine +COPY --from=build /work/_build/html/coverage_details.html /usr/share/nginx/html/index.html +COPY --from=build /work/_build/html/ /usr/share/nginx/html/ diff --git a/other/docker/coverage/run b/other/docker/coverage/run index a170812b..368d8429 100755 --- a/other/docker/coverage/run +++ b/other/docker/coverage/run @@ -1,7 +1,9 @@ -#!/bin/sh +#!/usr/bin/env bash set -eux +read -a ci_env <<<"$(bash <(curl -s https://codecov.io/env))" + docker build -t toxchat/c-toxcore:sources -f other/docker/sources/Dockerfile . -docker build -t toxchat/c-toxcore:coverage other/docker/coverage -docker run --name toxcore-coverage --rm -it -p "28192:80" toxchat/c-toxcore:coverage +docker build -t toxchat/c-toxcore:coverage -f other/docker/coverage/Dockerfile . +docker run "${ci_env[@]}" -e CI=true --name toxcore-coverage --rm -t toxchat/c-toxcore:coverage /usr/local/bin/codecov -x "llvm-cov gcov" diff --git a/other/docker/coverage/run_mallocfail b/other/docker/coverage/run_mallocfail index ddfea65b..d75e5f73 100755 --- a/other/docker/coverage/run_mallocfail +++ b/other/docker/coverage/run_mallocfail @@ -39,8 +39,8 @@ _ENV = { } -def run_mallocfail(tmpdir: str, timeout: float, exe: str, - iteration: int) -> bool: +def run_mallocfail(tmpdir: str, timeout: float, exe: str, iteration: int, + keep_going: bool) -> bool: """Run a program with mallocfail.""" print(f"\x1b[1;33mmallocfail '{exe}' run #{iteration}\x1b[0m") hashes = os.path.join(tmpdir, _HASHES) @@ -73,7 +73,8 @@ def run_mallocfail(tmpdir: str, timeout: float, exe: str, print( f"\x1b[1;32mProgram '{exe}' failed to handle OOM situation cleanly\x1b[0m" ) - raise Exception("Aborting test") + if not keep_going: + raise Exception("Aborting test") return True @@ -95,7 +96,8 @@ def find_prog(name: str) -> Tuple[Optional[str], ...]: return path return None - return (attempt(f"./unit_{name}_test"), attempt(f"./auto_{name}_test")) + return (attempt(f"./unit_{name}_test"), + attempt(f"auto_tests/auto_{name}_test")) def parse_flags(args: List[str]) -> Tuple[Dict[str, str], List[str]]: @@ -110,15 +112,19 @@ def parse_flags(args: List[str]) -> Tuple[Dict[str, str], List[str]]: return flags, exes -def loop_mallocfail(tmpdir: str, timeout: float, exe: str) -> None: +def loop_mallocfail(tmpdir: str, + timeout: float, + exe: str, + keep_going: bool = False) -> None: i = 1 - while run_mallocfail(tmpdir, timeout, exe, i): + while run_mallocfail(tmpdir, timeout, exe, i, keep_going): i += 1 def isolated_mallocfail(timeout: int, exe: str) -> None: with tempfile.TemporaryDirectory(prefix="mallocfail") as tmpdir: print(f"\x1b[1;33mRunning for {exe} in isolated path {tmpdir}\x1b[0m") + os.mkdir(os.path.join(tmpdir, "auto_tests")) shutil.copy(exe, os.path.join(tmpdir, exe)) shutil.copy(_HASHES, os.path.join(tmpdir, _HASHES)) loop_mallocfail(tmpdir, timeout, exe) @@ -149,7 +155,7 @@ def main(args: List[str]) -> None: # such as llvm_gcov_init fail. if os.path.exists(_PRIMER): print(f"\x1b[1;33mPriming hashes with unit_util_test\x1b[0m") - loop_mallocfail(".", timeout, _PRIMER) + loop_mallocfail(".", timeout, _PRIMER, keep_going=True) print(f"\x1b[1;33m--------------------------------\x1b[0m") print(f"\x1b[1;33mStarting mallocfail for {len(exes)} programs:\x1b[0m") diff --git a/other/docker/coverage/serve b/other/docker/coverage/serve new file mode 100755 index 00000000..94a09d15 --- /dev/null +++ b/other/docker/coverage/serve @@ -0,0 +1,8 @@ +#!/bin/sh + +set -eux + +docker build -t toxchat/c-toxcore:sources -f other/docker/sources/Dockerfile . +docker build -t toxchat/c-toxcore:coverage -f other/docker/coverage/Dockerfile . +docker build -t toxchat/c-toxcore:coverage-nginx -f other/docker/coverage/Dockerfile.nginx other/docker/coverage +docker run --name toxcore-coverage --rm -it -p "28192:80" toxchat/c-toxcore:coverage-nginx diff --git a/other/docker/cppcheck/Dockerfile b/other/docker/cppcheck/Dockerfile new file mode 100644 index 00000000..34b516fb --- /dev/null +++ b/other/docker/cppcheck/Dockerfile @@ -0,0 +1,30 @@ +FROM alpine:3.19.0 + +RUN ["apk", "add", "--no-cache", \ + "bash", \ + "cppcheck", \ + "findutils", \ + "libconfig-dev", \ + "libsodium-dev", \ + "libvpx-dev", \ + "linux-headers", \ + "make", \ + "opus-dev"] + +COPY other/bootstrap_daemon/ /src/workspace/c-toxcore/other/bootstrap_daemon/ +COPY other/bootstrap_node_packets.* /src/workspace/c-toxcore/other/ +COPY other/fun/ /src/workspace/c-toxcore/other/fun/ +COPY auto_tests/check_compat.h /src/workspace/c-toxcore/auto_tests/ +COPY testing/ /src/workspace/c-toxcore/testing/ +COPY toxav/ /src/workspace/c-toxcore/toxav/ +COPY toxcore/ /src/workspace/c-toxcore/toxcore/ +COPY toxencryptsave/ /src/workspace/c-toxcore/toxencryptsave/ +COPY third_party/cmp/cmp.h /src/workspace/c-toxcore/third_party/cmp/ +COPY other/analysis/run-cppcheck \ + other/analysis/gen-file.sh \ + other/analysis/variants.sh \ + /src/workspace/c-toxcore/other/analysis/ +COPY other/docker/cppcheck/toxcore.cfg \ + /src/workspace/c-toxcore/other/docker/cppcheck/ +WORKDIR /src/workspace/c-toxcore +RUN ["other/analysis/run-cppcheck"] diff --git a/other/docker/cppcheck/run b/other/docker/cppcheck/run new file mode 100755 index 00000000..8e437f4c --- /dev/null +++ b/other/docker/cppcheck/run @@ -0,0 +1,5 @@ +#!/bin/sh + +set -eux +BUILD=cppcheck +docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/Dockerfile" . diff --git a/other/docker/cppcheck/toxcore.cfg b/other/docker/cppcheck/toxcore.cfg new file mode 100644 index 00000000..f120246a --- /dev/null +++ b/other/docker/cppcheck/toxcore.cfg @@ -0,0 +1,117 @@ + + + + mem_balloc + mem_alloc + mem_valloc + mem_vrealloc + mem_delete + + + bin_pack_new + bin_pack_free + + + bin_unpack_new + bin_unpack_free + + + friendreq_new + friendreq_kill + + + logger_new + logger_kill + + + mono_time_new + mono_time_free + + + ping_array_new + ping_array_kill + + + ping_new + ping_kill + + + shared_key_cache_new + shared_key_cache_free + + + tox_dispatch_new + tox_dispatch_free + + + tox_new + tox_kill + + + tox_options_new + tox_options_free + + + new_announcements + kill_announcements + + + new_dht + kill_dht + + + new_dht_groupchats + kill_dht_groupchats + + + new_forwarding + kill_forwarding + + + new_friend_connections + kill_friend_connections + + + new_gca_list + kill_gca_list + + + new_groupchats + kill_groupchats + + + new_messenger + kill_messenger + + + new_net_crypto + kill_net_crypto + + + new_networking_ex + new_networking_no_udp + kill_networking + + + new_onion + kill_onion + + + new_onion_announce + kill_onion_announce + + + new_onion_client + kill_onion_client + + + new_tcp_connections + kill_tcp_connections + + + new_tcp_server + kill_tcp_server + + + diff --git a/other/docker/esp32/BUILD.bazel b/other/docker/esp32/BUILD.bazel new file mode 100644 index 00000000..5a4009e9 --- /dev/null +++ b/other/docker/esp32/BUILD.bazel @@ -0,0 +1,15 @@ +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "host_main", + testonly = 1, + srcs = [ + "host_main.cc", + "main/tox_main.cc", + "main/tox_main.h", + ], + deps = [ + "//c-toxcore/toxcore:tox", + "//c-toxcore/toxcore:tox_events", + ], +) diff --git a/other/docker/esp32/Dockerfile b/other/docker/esp32/Dockerfile index 9b6ad76c..71e2afe2 100644 --- a/other/docker/esp32/Dockerfile +++ b/other/docker/esp32/Dockerfile @@ -1,48 +1,32 @@ -FROM toxchat/c-toxcore:sources AS src -FROM ubuntu:18.04 +FROM mluis/qemu-esp32:latest +SHELL ["/bin/bash", "-o", "pipefail", "-c"] +ENV IDF_TARGET=esp32 -RUN apt-get update && \ - DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \ - bison \ - ccache \ - cmake \ - flex \ - git \ - gperf \ - libncurses-dev \ - ninja-build \ - python \ - python-cryptography \ - python-future \ - python-pip \ - python-pyparsing \ - python-serial \ - python-setuptools \ - wget \ - && apt-get clean \ - && rm -rf /var/lib/apt/lists/* +WORKDIR /root/toxcore -ENV ESP32_TARBALL=xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0 \ - IDF_PATH="/root/esp/esp-idf" \ - PATH="/root/esp/esp-idf/tools:/root/esp/xtensa-esp32-elf/bin:$PATH" -WORKDIR /root/esp -RUN wget -q https://dl.espressif.com/dl/$ESP32_TARBALL.tar.gz \ - && tar zxf $ESP32_TARBALL.tar.gz \ - && rm -f $ESP32_TARBALL.tar.gz \ - && git clone -b v3.3 --recursive --depth=1 --shallow-submodules https://github.com/espressif/esp-idf +# Build an initial bootstrap hello world just to compile libsodium and other +# system level dependencies. +COPY other/docker/esp32/sdkconfig \ + other/docker/esp32/CMakeLists.txt \ + /root/toxcore/ +COPY other/docker/esp32/bootstrap/ \ + /root/toxcore/main/ +RUN . /root/esp/esp-idf/export.sh && idf.py build -# Build a hello world first, so the OS and libsodium etc. are compiled. -WORKDIR /root/esp/toxcore -COPY other/docker/esp32/CMakeLists.txt /root/esp/toxcore/ -COPY other/docker/esp32/hello/ /root/esp/toxcore/main/ -RUN idf.py build - -# Then copy over the actual toxcore sources and build those. -COPY --from=src /src/third_party/cmp/ /root/esp/toxcore/main/third_party/cmp/ -COPY --from=src /src/toxencryptsave/defines.h /root/esp/toxcore/main/toxencryptsave/ -COPY --from=src /src/toxcore/ /root/esp/toxcore/main/toxcore/ -COPY other/docker/esp32/toxcore/CMakeLists.txt /root/esp/toxcore/main/ -COPY other/docker/esp32/toxcore/toxcore_main.cc /root/esp/toxcore/main/other/docker/esp32/main/ -RUN idf.py build +# Copy over toxcore sources and build those. +COPY third_party/cmp/ /root/toxcore/main/third_party/cmp/ +COPY toxencryptsave/defines.h /root/toxcore/main/toxencryptsave/ +COPY toxcore/ /root/toxcore/main/toxcore/ +COPY other/docker/esp32/main/CMakeLists.txt \ + /root/toxcore/main/ +COPY other/docker/esp32/main/*.cc \ + other/docker/esp32/main/*.h \ + /root/toxcore/main/other/docker/esp32/main/ +RUN touch /root/toxcore/main/CMakeLists.txt \ + && . /root/esp/esp-idf/export.sh && idf.py build RUN ls -lh build/toxcore.bin \ - && shasum build/toxcore.bin + && sha512sum build/toxcore.bin +RUN /root/flash.sh build/toxcore.bin + +COPY other/docker/esp32/qemu-test /root/toxcore/ +RUN ["/root/toxcore/qemu-test"] diff --git a/other/docker/esp32/bootstrap/CMakeLists.txt b/other/docker/esp32/bootstrap/CMakeLists.txt new file mode 100644 index 00000000..3276b2a0 --- /dev/null +++ b/other/docker/esp32/bootstrap/CMakeLists.txt @@ -0,0 +1,4 @@ +idf_component_register( + SRCS hello_main.cc + INCLUDE_DIRS "." + REQUIRES esp_netif lwip) diff --git a/other/docker/esp32/bootstrap/hello_main.cc b/other/docker/esp32/bootstrap/hello_main.cc new file mode 100644 index 00000000..74236a3c --- /dev/null +++ b/other/docker/esp32/bootstrap/hello_main.cc @@ -0,0 +1,4 @@ +#include + +// Bootstrap main. Only writes hello world. See ../main/ for the real thing. +extern "C" void app_main(void) { printf("Hello world!\n"); } diff --git a/other/docker/esp32/bootstrap/idf_component.yml b/other/docker/esp32/bootstrap/idf_component.yml new file mode 100644 index 00000000..6d04f9bf --- /dev/null +++ b/other/docker/esp32/bootstrap/idf_component.yml @@ -0,0 +1,2 @@ +dependencies: + espressif/libsodium: "==1.0.20" diff --git a/other/docker/esp32/hello/CMakeLists.txt b/other/docker/esp32/hello/CMakeLists.txt deleted file mode 100644 index 0a8bbce1..00000000 --- a/other/docker/esp32/hello/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -set(COMPONENT_SRCS "hello_main.c") -set(COMPONENT_ADD_INCLUDEDIRS "") - -register_component() diff --git a/other/docker/esp32/hello/hello_main.c b/other/docker/esp32/hello/hello_main.c deleted file mode 100644 index 1c961d6c..00000000 --- a/other/docker/esp32/hello/hello_main.c +++ /dev/null @@ -1,6 +0,0 @@ -#include - -void app_main(void) -{ - printf("Hello world!\n"); -} diff --git a/other/docker/esp32/host_main.cc b/other/docker/esp32/host_main.cc new file mode 100644 index 00000000..a878d214 --- /dev/null +++ b/other/docker/esp32/host_main.cc @@ -0,0 +1,3 @@ +#include "main/tox_main.h" + +int main() { tox_main(); } diff --git a/other/docker/esp32/main/CMakeLists.txt b/other/docker/esp32/main/CMakeLists.txt new file mode 100644 index 00000000..c81c1244 --- /dev/null +++ b/other/docker/esp32/main/CMakeLists.txt @@ -0,0 +1,16 @@ +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-missing-field-initializers -Wno-format -DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE") + +file(GLOB toxcore_SRCS "toxcore/*.[ch]" "toxcore/*/*.[ch]") +set(COMPONENT_SRCS + ${toxcore_SRCS} + other/docker/esp32/main/app_main.cc + other/docker/esp32/main/tox_main.cc + other/docker/esp32/main/tox_main.h + third_party/cmp/cmp.c + third_party/cmp/cmp.h + toxencryptsave/defines.h) + +idf_component_register( + SRCS ${COMPONENT_SRCS} + INCLUDE_DIRS "." + REQUIRES esp_eth esp_netif lwip) diff --git a/other/docker/esp32/main/app_main.cc b/other/docker/esp32/main/app_main.cc new file mode 100644 index 00000000..d33286f1 --- /dev/null +++ b/other/docker/esp32/main/app_main.cc @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "tox_main.h" + +static const char *MAIN_TAG = "app_main"; +static constexpr int NTP_TIMEOUT = 60; // 1 minute + +static esp_eth_handle_t eth_handle = nullptr; +static esp_netif_t *eth_netif = nullptr; + +static void event_handler( + void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) +{ + if (event_base == ETH_EVENT) { + if (event_id == ETHERNET_EVENT_START) { + return; + } + if (event_id == ETHERNET_EVENT_STOP) { + return; + } + } + if (event_base == IP_EVENT) { + if (event_id == IP_EVENT_ETH_GOT_IP) { + return; + } + } +} + +static void register_ethernet(void) +{ + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); + eth_netif = esp_netif_new(&cfg); + + eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); + eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); + esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config); + + esp_eth_phy_t *phy = esp_eth_phy_new_dp83848(&phy_config); + + esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); + ESP_ERROR_CHECK(esp_eth_driver_install(&config, ð_handle)); + ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle))); + ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); + ESP_ERROR_CHECK( + esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &event_handler, NULL)); + ESP_ERROR_CHECK(esp_eth_start(eth_handle)); +} + +// Does all the esp32-specific init before running generic tox code. +extern "C" void app_main(void) +{ + register_ethernet(); + + esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org"); + ESP_ERROR_CHECK(esp_netif_sntp_init(&config)); + + if (esp_netif_sntp_sync_wait(pdMS_TO_TICKS(NTP_TIMEOUT * 1000)) != ESP_OK) { + ESP_LOGE(MAIN_TAG, "failed to update system time within %ds timeout", NTP_TIMEOUT); + return; + } + + ESP_LOGI(MAIN_TAG, "time is updated: %lld", time(nullptr)); + + tox_main(); +} diff --git a/other/docker/esp32/main/tox_main.cc b/other/docker/esp32/main/tox_main.cc new file mode 100644 index 00000000..a9f9c100 --- /dev/null +++ b/other/docker/esp32/main/tox_main.cc @@ -0,0 +1,98 @@ +#include "../main/tox_main.h" + +#include +#include +#include +#include + +#include + +#include "../../../../toxcore/ccompat.h" +#include "../../../../toxcore/tox.h" +#include "../../../../toxcore/tox_events.h" + +static const char *color(int index) +{ + switch (index) { + case 0: + return "\033" + "[35m"; + case 1: + return "\033" + "[36m"; + } + + return "\033" + "[0m"; +} + +static tox_log_cb log_handler; +static void log_handler(Tox *tox, Tox_Log_Level level, const char *file, uint32_t line, + const char *func, const char *msg, void *user_data) +{ + const int *index = static_cast(user_data); + const uint16_t udp_port = tox_self_get_udp_port(tox, nullptr); + printf("%s#%d (:%d) [%c] %s:%u(%s): %s\n", color(*index), *index, udp_port, + tox_log_level_to_string(level)[0], file, static_cast(line), func, msg); +} + +using Tox_Options_Ptr = std::unique_ptr; +using Tox_Ptr = std::unique_ptr; + +void tox_main() +{ + printf("Hello Tox!\n"); + + Tox_Options_Ptr opts(tox_options_new(nullptr), tox_options_free); + assert(opts != nullptr); + + tox_options_set_ipv6_enabled(opts.get(), false); + tox_options_set_local_discovery_enabled(opts.get(), false); + + tox_options_set_log_callback(opts.get(), log_handler); + + Tox_Err_New err; + + int index[] = {0, 1}; + + tox_options_set_log_user_data(opts.get(), &index[0]); + Tox_Ptr tox0(tox_new(opts.get(), &err), tox_kill); + printf("tox_new(#0): %p\n", static_cast(tox0.get())); + + if (err != TOX_ERR_NEW_OK) { + printf("tox_new(#0): %s\n", tox_err_new_to_string(err)); + return; + } + + tox_options_set_log_user_data(opts.get(), &index[1]); + Tox_Ptr tox1(tox_new(opts.get(), &err), tox_kill); + printf("tox_new(#1): %p\n", static_cast(tox0.get())); + + if (err != TOX_ERR_NEW_OK) { + printf("tox_new(#1): %s\n", tox_err_new_to_string(err)); + return; + } + + uint8_t pk[TOX_PUBLIC_KEY_SIZE]; + tox_self_get_dht_id(tox0.get(), pk); + tox_bootstrap(tox1.get(), "localhost", tox_self_get_udp_port(tox0.get(), nullptr), pk, nullptr); + +#if 0 + tox_self_get_public_key(tox0.get(), pk); + tox_friend_add_norequest(tox1.get(), pk, nullptr); + + tox_self_get_public_key(tox1.get(), pk); + tox_friend_add_norequest(tox0.get(), pk, nullptr); +#endif + + printf("bootstrapping and connecting 2 toxes\n"); + + while (tox_self_get_connection_status(tox1.get()) == TOX_CONNECTION_NONE + || tox_self_get_connection_status(tox0.get()) == TOX_CONNECTION_NONE) { + tox_events_free(tox_events_iterate(tox0.get(), true, nullptr)); + tox_events_free(tox_events_iterate(tox1.get(), true, nullptr)); + + usleep(tox_iteration_interval(tox0.get()) * 1000); + usleep(250); // a bit less noise in the log + } +} diff --git a/other/docker/esp32/main/tox_main.h b/other/docker/esp32/main/tox_main.h new file mode 100644 index 00000000..29d30a30 --- /dev/null +++ b/other/docker/esp32/main/tox_main.h @@ -0,0 +1,6 @@ +#ifndef TOX_MAIN_H +#define TOX_MAIN_H + +void tox_main(); + +#endif // TOX_MAIN_H diff --git a/other/docker/esp32/qemu-test b/other/docker/esp32/qemu-test new file mode 100755 index 00000000..27e4b2f8 --- /dev/null +++ b/other/docker/esp32/qemu-test @@ -0,0 +1,17 @@ +#!/bin/bash + +set -eu + +qemu-system-xtensa \ + -nographic \ + -M esp32 \ + -m 4 \ + -drive file=flash.bin,if=mtd,format=raw \ + -nic user,model=open_eth,hostfwd=tcp::80-:80 \ + -s | + tee qemu.log & + +echo "Waiting for program to complete" +while ! grep 'Returned from app_main' qemu.log >/dev/null; do + sleep 1 +done diff --git a/other/docker/esp32/run b/other/docker/esp32/run index c611413f..2f989960 100755 --- a/other/docker/esp32/run +++ b/other/docker/esp32/run @@ -1,4 +1,3 @@ #!/bin/sh -docker build -t toxchat/c-toxcore:sources -f other/docker/sources/Dockerfile . docker build -t toxchat/c-toxcore:esp32 -f other/docker/esp32/Dockerfile . diff --git a/other/docker/esp32/run-host b/other/docker/esp32/run-host new file mode 100755 index 00000000..66c52587 --- /dev/null +++ b/other/docker/esp32/run-host @@ -0,0 +1,5 @@ +#!/bin/sh + +set -eux + +bazel run //c-toxcore/other/docker/esp32:host_main diff --git a/other/docker/esp32/sdkconfig b/other/docker/esp32/sdkconfig new file mode 100644 index 00000000..a87ad906 --- /dev/null +++ b/other/docker/esp32/sdkconfig @@ -0,0 +1,1937 @@ +# +# Automatically generated file. DO NOT EDIT. +# Espressif IoT Development Framework (ESP-IDF) 5.2.0 Project Configuration +# +CONFIG_SOC_BROWNOUT_RESET_SUPPORTED="Not determined" +CONFIG_SOC_TWAI_BRP_DIV_SUPPORTED="Not determined" +CONFIG_SOC_DPORT_WORKAROUND="Not determined" +CONFIG_SOC_CAPS_ECO_VER_MAX=301 +CONFIG_SOC_ADC_SUPPORTED=y +CONFIG_SOC_DAC_SUPPORTED=y +CONFIG_SOC_UART_SUPPORTED=y +CONFIG_SOC_MCPWM_SUPPORTED=y +CONFIG_SOC_GPTIMER_SUPPORTED=y +CONFIG_SOC_SDMMC_HOST_SUPPORTED=y +CONFIG_SOC_BT_SUPPORTED=y +CONFIG_SOC_PCNT_SUPPORTED=y +CONFIG_SOC_WIFI_SUPPORTED=y +CONFIG_SOC_SDIO_SLAVE_SUPPORTED=y +CONFIG_SOC_TWAI_SUPPORTED=y +CONFIG_SOC_EFUSE_SUPPORTED=y +CONFIG_SOC_EMAC_SUPPORTED=y +CONFIG_SOC_ULP_SUPPORTED=y +CONFIG_SOC_CCOMP_TIMER_SUPPORTED=y +CONFIG_SOC_RTC_FAST_MEM_SUPPORTED=y +CONFIG_SOC_RTC_SLOW_MEM_SUPPORTED=y +CONFIG_SOC_RTC_MEM_SUPPORTED=y +CONFIG_SOC_I2S_SUPPORTED=y +CONFIG_SOC_RMT_SUPPORTED=y +CONFIG_SOC_SDM_SUPPORTED=y +CONFIG_SOC_GPSPI_SUPPORTED=y +CONFIG_SOC_LEDC_SUPPORTED=y +CONFIG_SOC_I2C_SUPPORTED=y +CONFIG_SOC_SUPPORT_COEXISTENCE=y +CONFIG_SOC_AES_SUPPORTED=y +CONFIG_SOC_MPI_SUPPORTED=y +CONFIG_SOC_SHA_SUPPORTED=y +CONFIG_SOC_FLASH_ENC_SUPPORTED=y +CONFIG_SOC_SECURE_BOOT_SUPPORTED=y +CONFIG_SOC_TOUCH_SENSOR_SUPPORTED=y +CONFIG_SOC_BOD_SUPPORTED=y +CONFIG_SOC_ULP_FSM_SUPPORTED=y +CONFIG_SOC_CLK_TREE_SUPPORTED=y +CONFIG_SOC_DPORT_WORKAROUND_DIS_INTERRUPT_LVL=5 +CONFIG_SOC_XTAL_SUPPORT_26M=y +CONFIG_SOC_XTAL_SUPPORT_40M=y +CONFIG_SOC_XTAL_SUPPORT_AUTO_DETECT=y +CONFIG_SOC_ADC_RTC_CTRL_SUPPORTED=y +CONFIG_SOC_ADC_DIG_CTRL_SUPPORTED=y +CONFIG_SOC_ADC_DMA_SUPPORTED=y +CONFIG_SOC_ADC_PERIPH_NUM=2 +CONFIG_SOC_ADC_MAX_CHANNEL_NUM=10 +CONFIG_SOC_ADC_ATTEN_NUM=4 +CONFIG_SOC_ADC_DIGI_CONTROLLER_NUM=2 +CONFIG_SOC_ADC_PATT_LEN_MAX=16 +CONFIG_SOC_ADC_DIGI_MIN_BITWIDTH=9 +CONFIG_SOC_ADC_DIGI_MAX_BITWIDTH=12 +CONFIG_SOC_ADC_DIGI_RESULT_BYTES=2 +CONFIG_SOC_ADC_DIGI_DATA_BYTES_PER_CONV=4 +CONFIG_SOC_ADC_DIGI_MONITOR_NUM=0 +CONFIG_SOC_ADC_SAMPLE_FREQ_THRES_HIGH=2 +CONFIG_SOC_ADC_SAMPLE_FREQ_THRES_LOW=20 +CONFIG_SOC_ADC_RTC_MIN_BITWIDTH=9 +CONFIG_SOC_ADC_RTC_MAX_BITWIDTH=12 +CONFIG_SOC_ADC_SHARED_POWER=y +CONFIG_SOC_SHARED_IDCACHE_SUPPORTED=y +CONFIG_SOC_IDCACHE_PER_CORE=y +CONFIG_SOC_CPU_CORES_NUM=2 +CONFIG_SOC_CPU_INTR_NUM=32 +CONFIG_SOC_CPU_HAS_FPU=y +CONFIG_SOC_CPU_BREAKPOINTS_NUM=2 +CONFIG_SOC_CPU_WATCHPOINTS_NUM=2 +CONFIG_SOC_CPU_WATCHPOINT_SIZE=64 +CONFIG_SOC_DAC_CHAN_NUM=2 +CONFIG_SOC_DAC_RESOLUTION=8 +CONFIG_SOC_DAC_DMA_16BIT_ALIGN=y +CONFIG_SOC_GPIO_PORT=1 +CONFIG_SOC_GPIO_PIN_COUNT=40 +CONFIG_SOC_GPIO_VALID_GPIO_MASK=0xFFFFFFFFFF +CONFIG_SOC_GPIO_VALID_DIGITAL_IO_PAD_MASK=0xEF0FEA +CONFIG_SOC_I2C_NUM=2 +CONFIG_SOC_I2C_FIFO_LEN=32 +CONFIG_SOC_I2C_CMD_REG_NUM=16 +CONFIG_SOC_I2C_SUPPORT_SLAVE=y +CONFIG_SOC_I2C_SUPPORT_APB=y +CONFIG_SOC_I2C_STOP_INDEPENDENT=y +CONFIG_SOC_I2S_NUM=2 +CONFIG_SOC_I2S_HW_VERSION_1=y +CONFIG_SOC_I2S_SUPPORTS_APLL=y +CONFIG_SOC_I2S_SUPPORTS_PLL_F160M=y +CONFIG_SOC_I2S_SUPPORTS_PDM=y +CONFIG_SOC_I2S_SUPPORTS_PDM_TX=y +CONFIG_SOC_I2S_PDM_MAX_TX_LINES=1 +CONFIG_SOC_I2S_SUPPORTS_PDM_RX=y +CONFIG_SOC_I2S_PDM_MAX_RX_LINES=1 +CONFIG_SOC_I2S_SUPPORTS_ADC_DAC=y +CONFIG_SOC_I2S_SUPPORTS_ADC=y +CONFIG_SOC_I2S_SUPPORTS_DAC=y +CONFIG_SOC_I2S_SUPPORTS_LCD_CAMERA=y +CONFIG_SOC_I2S_TRANS_SIZE_ALIGN_WORD=y +CONFIG_SOC_I2S_LCD_I80_VARIANT=y +CONFIG_SOC_LCD_I80_SUPPORTED=y +CONFIG_SOC_LCD_I80_BUSES=2 +CONFIG_SOC_LCD_I80_BUS_WIDTH=24 +CONFIG_SOC_LEDC_HAS_TIMER_SPECIFIC_MUX=y +CONFIG_SOC_LEDC_SUPPORT_APB_CLOCK=y +CONFIG_SOC_LEDC_SUPPORT_REF_TICK=y +CONFIG_SOC_LEDC_SUPPORT_HS_MODE=y +CONFIG_SOC_LEDC_CHANNEL_NUM=8 +CONFIG_SOC_LEDC_TIMER_BIT_WIDTH=20 +CONFIG_SOC_MCPWM_GROUPS=2 +CONFIG_SOC_MCPWM_TIMERS_PER_GROUP=3 +CONFIG_SOC_MCPWM_OPERATORS_PER_GROUP=3 +CONFIG_SOC_MCPWM_COMPARATORS_PER_OPERATOR=2 +CONFIG_SOC_MCPWM_GENERATORS_PER_OPERATOR=2 +CONFIG_SOC_MCPWM_TRIGGERS_PER_OPERATOR=2 +CONFIG_SOC_MCPWM_GPIO_FAULTS_PER_GROUP=3 +CONFIG_SOC_MCPWM_CAPTURE_TIMERS_PER_GROUP=y +CONFIG_SOC_MCPWM_CAPTURE_CHANNELS_PER_TIMER=3 +CONFIG_SOC_MCPWM_GPIO_SYNCHROS_PER_GROUP=3 +CONFIG_SOC_MMU_PERIPH_NUM=2 +CONFIG_SOC_MMU_LINEAR_ADDRESS_REGION_NUM=3 +CONFIG_SOC_MPU_MIN_REGION_SIZE=0x20000000 +CONFIG_SOC_MPU_REGIONS_MAX_NUM=8 +CONFIG_SOC_PCNT_GROUPS=1 +CONFIG_SOC_PCNT_UNITS_PER_GROUP=8 +CONFIG_SOC_PCNT_CHANNELS_PER_UNIT=2 +CONFIG_SOC_PCNT_THRES_POINT_PER_UNIT=2 +CONFIG_SOC_RMT_GROUPS=1 +CONFIG_SOC_RMT_TX_CANDIDATES_PER_GROUP=8 +CONFIG_SOC_RMT_RX_CANDIDATES_PER_GROUP=8 +CONFIG_SOC_RMT_CHANNELS_PER_GROUP=8 +CONFIG_SOC_RMT_MEM_WORDS_PER_CHANNEL=64 +CONFIG_SOC_RMT_SUPPORT_REF_TICK=y +CONFIG_SOC_RMT_SUPPORT_APB=y +CONFIG_SOC_RMT_CHANNEL_CLK_INDEPENDENT=y +CONFIG_SOC_RTCIO_PIN_COUNT=18 +CONFIG_SOC_RTCIO_INPUT_OUTPUT_SUPPORTED=y +CONFIG_SOC_RTCIO_HOLD_SUPPORTED=y +CONFIG_SOC_RTCIO_WAKE_SUPPORTED=y +CONFIG_SOC_SDM_GROUPS=1 +CONFIG_SOC_SDM_CHANNELS_PER_GROUP=8 +CONFIG_SOC_SDM_CLK_SUPPORT_APB=y +CONFIG_SOC_SPI_HD_BOTH_INOUT_SUPPORTED=y +CONFIG_SOC_SPI_AS_CS_SUPPORTED=y +CONFIG_SOC_SPI_PERIPH_NUM=3 +CONFIG_SOC_SPI_DMA_CHAN_NUM=2 +CONFIG_SOC_SPI_MAX_CS_NUM=3 +CONFIG_SOC_SPI_SUPPORT_CLK_APB=y +CONFIG_SOC_SPI_MAXIMUM_BUFFER_SIZE=64 +CONFIG_SOC_SPI_MAX_PRE_DIVIDER=8192 +CONFIG_SOC_MEMSPI_SRC_FREQ_80M_SUPPORTED=y +CONFIG_SOC_MEMSPI_SRC_FREQ_40M_SUPPORTED=y +CONFIG_SOC_MEMSPI_SRC_FREQ_26M_SUPPORTED=y +CONFIG_SOC_MEMSPI_SRC_FREQ_20M_SUPPORTED=y +CONFIG_SOC_TIMER_GROUPS=2 +CONFIG_SOC_TIMER_GROUP_TIMERS_PER_GROUP=2 +CONFIG_SOC_TIMER_GROUP_COUNTER_BIT_WIDTH=64 +CONFIG_SOC_TIMER_GROUP_TOTAL_TIMERS=4 +CONFIG_SOC_TIMER_GROUP_SUPPORT_APB=y +CONFIG_SOC_TOUCH_VERSION_1=y +CONFIG_SOC_TOUCH_SENSOR_NUM=10 +CONFIG_SOC_TOUCH_PAD_MEASURE_WAIT_MAX=0xFF +CONFIG_SOC_TWAI_CONTROLLER_NUM=1 +CONFIG_SOC_TWAI_BRP_MIN=2 +CONFIG_SOC_TWAI_CLK_SUPPORT_APB=y +CONFIG_SOC_TWAI_SUPPORT_MULTI_ADDRESS_LAYOUT=y +CONFIG_SOC_UART_NUM=3 +CONFIG_SOC_UART_HP_NUM=3 +CONFIG_SOC_UART_SUPPORT_APB_CLK=y +CONFIG_SOC_UART_SUPPORT_REF_TICK=y +CONFIG_SOC_UART_FIFO_LEN=128 +CONFIG_SOC_UART_BITRATE_MAX=5000000 +CONFIG_SOC_SPIRAM_SUPPORTED=y +CONFIG_SOC_SPI_MEM_SUPPORT_CONFIG_GPIO_BY_EFUSE=y +CONFIG_SOC_SHA_SUPPORT_PARALLEL_ENG=y +CONFIG_SOC_SHA_ENDIANNESS_BE=y +CONFIG_SOC_SHA_SUPPORT_SHA1=y +CONFIG_SOC_SHA_SUPPORT_SHA256=y +CONFIG_SOC_SHA_SUPPORT_SHA384=y +CONFIG_SOC_SHA_SUPPORT_SHA512=y +CONFIG_SOC_MPI_MEM_BLOCKS_NUM=4 +CONFIG_SOC_MPI_OPERATIONS_NUM=y +CONFIG_SOC_RSA_MAX_BIT_LEN=4096 +CONFIG_SOC_AES_SUPPORT_AES_128=y +CONFIG_SOC_AES_SUPPORT_AES_192=y +CONFIG_SOC_AES_SUPPORT_AES_256=y +CONFIG_SOC_SECURE_BOOT_V1=y +CONFIG_SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS=y +CONFIG_SOC_FLASH_ENCRYPTED_XTS_AES_BLOCK_MAX=32 +CONFIG_SOC_PHY_DIG_REGS_MEM_SIZE=21 +CONFIG_SOC_PM_SUPPORT_EXT0_WAKEUP=y +CONFIG_SOC_PM_SUPPORT_EXT1_WAKEUP=y +CONFIG_SOC_PM_SUPPORT_EXT_WAKEUP=y +CONFIG_SOC_PM_SUPPORT_TOUCH_SENSOR_WAKEUP=y +CONFIG_SOC_PM_SUPPORT_RTC_PERIPH_PD=y +CONFIG_SOC_PM_SUPPORT_RTC_FAST_MEM_PD=y +CONFIG_SOC_PM_SUPPORT_RTC_SLOW_MEM_PD=y +CONFIG_SOC_PM_SUPPORT_RC_FAST_PD=y +CONFIG_SOC_PM_SUPPORT_VDDSDIO_PD=y +CONFIG_SOC_PM_SUPPORT_MODEM_PD=y +CONFIG_SOC_CONFIGURABLE_VDDSDIO_SUPPORTED=y +CONFIG_SOC_CLK_APLL_SUPPORTED=y +CONFIG_SOC_APLL_MULTIPLIER_OUT_MIN_HZ=350000000 +CONFIG_SOC_APLL_MULTIPLIER_OUT_MAX_HZ=500000000 +CONFIG_SOC_APLL_MIN_HZ=5303031 +CONFIG_SOC_APLL_MAX_HZ=125000000 +CONFIG_SOC_CLK_RC_FAST_D256_SUPPORTED=y +CONFIG_SOC_RTC_SLOW_CLK_SUPPORT_RC_FAST_D256=y +CONFIG_SOC_CLK_RC_FAST_SUPPORT_CALIBRATION=y +CONFIG_SOC_CLK_XTAL32K_SUPPORTED=y +CONFIG_SOC_SDMMC_USE_IOMUX=y +CONFIG_SOC_SDMMC_NUM_SLOTS=2 +CONFIG_SOC_WIFI_WAPI_SUPPORT=y +CONFIG_SOC_WIFI_CSI_SUPPORT=y +CONFIG_SOC_WIFI_MESH_SUPPORT=y +CONFIG_SOC_WIFI_SUPPORT_VARIABLE_BEACON_WINDOW=y +CONFIG_SOC_WIFI_NAN_SUPPORT=y +CONFIG_SOC_BLE_SUPPORTED=y +CONFIG_SOC_BLE_MESH_SUPPORTED=y +CONFIG_SOC_BT_CLASSIC_SUPPORTED=y +CONFIG_SOC_BLUFI_SUPPORTED=y +CONFIG_SOC_ULP_HAS_ADC=y +CONFIG_IDF_CMAKE=y +CONFIG_IDF_TOOLCHAIN="gcc" +CONFIG_IDF_TARGET_ARCH_XTENSA=y +CONFIG_IDF_TARGET_ARCH="xtensa" +CONFIG_IDF_TARGET="esp32" +CONFIG_IDF_INIT_VERSION="5.2.0" +CONFIG_IDF_TARGET_ESP32=y +CONFIG_IDF_FIRMWARE_CHIP_ID=0x0000 + +# +# Build type +# +CONFIG_APP_BUILD_TYPE_APP_2NDBOOT=y +# CONFIG_APP_BUILD_TYPE_RAM is not set +CONFIG_APP_BUILD_GENERATE_BINARIES=y +CONFIG_APP_BUILD_BOOTLOADER=y +CONFIG_APP_BUILD_USE_FLASH_SECTIONS=y +CONFIG_APP_REPRODUCIBLE_BUILD=y +# CONFIG_APP_NO_BLOBS is not set +# CONFIG_APP_COMPATIBLE_PRE_V2_1_BOOTLOADERS is not set +# CONFIG_APP_COMPATIBLE_PRE_V3_1_BOOTLOADERS is not set +# end of Build type + +# +# Bootloader config +# + +# +# Bootloader manager +# +CONFIG_BOOTLOADER_PROJECT_VER=1 +# end of Bootloader manager + +CONFIG_BOOTLOADER_OFFSET_IN_FLASH=0x1000 +CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y +# CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_DEBUG is not set +# CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_PERF is not set +# CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_NONE is not set +# CONFIG_BOOTLOADER_LOG_LEVEL_NONE is not set +# CONFIG_BOOTLOADER_LOG_LEVEL_ERROR is not set +# CONFIG_BOOTLOADER_LOG_LEVEL_WARN is not set +CONFIG_BOOTLOADER_LOG_LEVEL_INFO=y +# CONFIG_BOOTLOADER_LOG_LEVEL_DEBUG is not set +# CONFIG_BOOTLOADER_LOG_LEVEL_VERBOSE is not set +CONFIG_BOOTLOADER_LOG_LEVEL=3 +# CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_8V is not set +CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V=y +# CONFIG_BOOTLOADER_FACTORY_RESET is not set +# CONFIG_BOOTLOADER_APP_TEST is not set +CONFIG_BOOTLOADER_REGION_PROTECTION_ENABLE=y +CONFIG_BOOTLOADER_WDT_ENABLE=y +# CONFIG_BOOTLOADER_WDT_DISABLE_IN_USER_CODE is not set +CONFIG_BOOTLOADER_WDT_TIME_MS=9000 +# CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE is not set +# CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP is not set +# CONFIG_BOOTLOADER_SKIP_VALIDATE_ON_POWER_ON is not set +# CONFIG_BOOTLOADER_SKIP_VALIDATE_ALWAYS is not set +CONFIG_BOOTLOADER_RESERVE_RTC_SIZE=0 +# CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC is not set +CONFIG_BOOTLOADER_FLASH_XMC_SUPPORT=y +# end of Bootloader config + +# +# Security features +# +CONFIG_SECURE_BOOT_V1_SUPPORTED=y +# CONFIG_SECURE_SIGNED_APPS_NO_SECURE_BOOT is not set +# CONFIG_SECURE_BOOT is not set +# CONFIG_SECURE_FLASH_ENC_ENABLED is not set +# end of Security features + +# +# Application manager +# +# CONFIG_APP_EXCLUDE_PROJECT_VER_VAR is not set +# CONFIG_APP_EXCLUDE_PROJECT_NAME_VAR is not set +# CONFIG_APP_PROJECT_VER_FROM_CONFIG is not set +CONFIG_APP_RETRIEVE_LEN_ELF_SHA=9 +# end of Application manager + +CONFIG_ESP_ROM_HAS_CRC_LE=y +CONFIG_ESP_ROM_HAS_CRC_BE=y +CONFIG_ESP_ROM_HAS_MZ_CRC32=y +CONFIG_ESP_ROM_HAS_JPEG_DECODE=y +CONFIG_ESP_ROM_HAS_UART_BUF_SWITCH=y +CONFIG_ESP_ROM_NEEDS_SWSETUP_WORKAROUND=y +CONFIG_ESP_ROM_HAS_NEWLIB_NANO_FORMAT=y + +# +# Serial flasher config +# +# CONFIG_ESPTOOLPY_NO_STUB is not set +# CONFIG_ESPTOOLPY_FLASHMODE_QIO is not set +# CONFIG_ESPTOOLPY_FLASHMODE_QOUT is not set +CONFIG_ESPTOOLPY_FLASHMODE_DIO=y +# CONFIG_ESPTOOLPY_FLASHMODE_DOUT is not set +CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_STR=y +CONFIG_ESPTOOLPY_FLASHMODE="dio" +# CONFIG_ESPTOOLPY_FLASHFREQ_80M is not set +CONFIG_ESPTOOLPY_FLASHFREQ_40M=y +# CONFIG_ESPTOOLPY_FLASHFREQ_26M is not set +# CONFIG_ESPTOOLPY_FLASHFREQ_20M is not set +CONFIG_ESPTOOLPY_FLASHFREQ="40m" +# CONFIG_ESPTOOLPY_FLASHSIZE_1MB is not set +# CONFIG_ESPTOOLPY_FLASHSIZE_2MB is not set +CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y +# CONFIG_ESPTOOLPY_FLASHSIZE_8MB is not set +# CONFIG_ESPTOOLPY_FLASHSIZE_16MB is not set +# CONFIG_ESPTOOLPY_FLASHSIZE_32MB is not set +# CONFIG_ESPTOOLPY_FLASHSIZE_64MB is not set +# CONFIG_ESPTOOLPY_FLASHSIZE_128MB is not set +CONFIG_ESPTOOLPY_FLASHSIZE="4MB" +# CONFIG_ESPTOOLPY_HEADER_FLASHSIZE_UPDATE is not set +CONFIG_ESPTOOLPY_BEFORE_RESET=y +# CONFIG_ESPTOOLPY_BEFORE_NORESET is not set +CONFIG_ESPTOOLPY_BEFORE="default_reset" +CONFIG_ESPTOOLPY_AFTER_RESET=y +# CONFIG_ESPTOOLPY_AFTER_NORESET is not set +CONFIG_ESPTOOLPY_AFTER="hard_reset" +CONFIG_ESPTOOLPY_MONITOR_BAUD=115200 +# end of Serial flasher config + +# +# Partition Table +# +CONFIG_PARTITION_TABLE_SINGLE_APP=y +# CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE is not set +# CONFIG_PARTITION_TABLE_TWO_OTA is not set +# CONFIG_PARTITION_TABLE_CUSTOM is not set +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" +CONFIG_PARTITION_TABLE_FILENAME="partitions_singleapp.csv" +CONFIG_PARTITION_TABLE_OFFSET=0x8000 +CONFIG_PARTITION_TABLE_MD5=y +# end of Partition Table + +# +# Compiler options +# +CONFIG_COMPILER_OPTIMIZATION_DEBUG=y +# CONFIG_COMPILER_OPTIMIZATION_SIZE is not set +# CONFIG_COMPILER_OPTIMIZATION_PERF is not set +# CONFIG_COMPILER_OPTIMIZATION_NONE is not set +CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE=y +# CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT is not set +# CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE is not set +CONFIG_COMPILER_FLOAT_LIB_FROM_GCCLIB=y +CONFIG_COMPILER_OPTIMIZATION_ASSERTION_LEVEL=2 +# CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT is not set +CONFIG_COMPILER_HIDE_PATHS_MACROS=y +# CONFIG_COMPILER_CXX_EXCEPTIONS is not set +# CONFIG_COMPILER_CXX_RTTI is not set +# CONFIG_COMPILER_STACK_CHECK_MODE_NONE is not set +# CONFIG_COMPILER_STACK_CHECK_MODE_NORM is not set +CONFIG_COMPILER_STACK_CHECK_MODE_STRONG=y +# CONFIG_COMPILER_STACK_CHECK_MODE_ALL is not set +CONFIG_COMPILER_STACK_CHECK=y +# CONFIG_COMPILER_WARN_WRITE_STRINGS is not set +CONFIG_COMPILER_DISABLE_GCC12_WARNINGS=y +# CONFIG_COMPILER_DUMP_RTL_FILES is not set +CONFIG_COMPILER_RT_LIB_GCCLIB=y +CONFIG_COMPILER_RT_LIB_NAME="gcc" +# end of Compiler options + +# +# Component config +# + +# +# Application Level Tracing +# +# CONFIG_APPTRACE_DEST_JTAG is not set +CONFIG_APPTRACE_DEST_NONE=y +# CONFIG_APPTRACE_DEST_UART1 is not set +# CONFIG_APPTRACE_DEST_UART2 is not set +CONFIG_APPTRACE_DEST_UART_NONE=y +CONFIG_APPTRACE_UART_TASK_PRIO=1 +CONFIG_APPTRACE_LOCK_ENABLE=y +# end of Application Level Tracing + +# +# Bluetooth +# +# CONFIG_BT_ENABLED is not set +# end of Bluetooth + +# +# Driver Configurations +# + +# +# Legacy ADC Configuration +# +CONFIG_ADC_DISABLE_DAC=y +# CONFIG_ADC_SUPPRESS_DEPRECATE_WARN is not set + +# +# Legacy ADC Calibration Configuration +# +CONFIG_ADC_CAL_EFUSE_TP_ENABLE=y +CONFIG_ADC_CAL_EFUSE_VREF_ENABLE=y +CONFIG_ADC_CAL_LUT_ENABLE=y +# CONFIG_ADC_CALI_SUPPRESS_DEPRECATE_WARN is not set +# end of Legacy ADC Calibration Configuration +# end of Legacy ADC Configuration + +# +# SPI Configuration +# +# CONFIG_SPI_MASTER_IN_IRAM is not set +CONFIG_SPI_MASTER_ISR_IN_IRAM=y +# CONFIG_SPI_SLAVE_IN_IRAM is not set +CONFIG_SPI_SLAVE_ISR_IN_IRAM=y +# end of SPI Configuration + +# +# TWAI Configuration +# +# CONFIG_TWAI_ISR_IN_IRAM is not set +CONFIG_TWAI_ERRATA_FIX_BUS_OFF_REC=y +CONFIG_TWAI_ERRATA_FIX_TX_INTR_LOST=y +CONFIG_TWAI_ERRATA_FIX_RX_FRAME_INVALID=y +CONFIG_TWAI_ERRATA_FIX_RX_FIFO_CORRUPT=y +CONFIG_TWAI_ERRATA_FIX_LISTEN_ONLY_DOM=y +# end of TWAI Configuration + +# +# UART Configuration +# +# CONFIG_UART_ISR_IN_IRAM is not set +# end of UART Configuration + +# +# GPIO Configuration +# +# CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL is not set +# CONFIG_GPIO_CTRL_FUNC_IN_IRAM is not set +# end of GPIO Configuration + +# +# Sigma Delta Modulator Configuration +# +# CONFIG_SDM_CTRL_FUNC_IN_IRAM is not set +# CONFIG_SDM_SUPPRESS_DEPRECATE_WARN is not set +# CONFIG_SDM_ENABLE_DEBUG_LOG is not set +# end of Sigma Delta Modulator Configuration + +# +# GPTimer Configuration +# +CONFIG_GPTIMER_ISR_HANDLER_IN_IRAM=y +# CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM is not set +# CONFIG_GPTIMER_ISR_IRAM_SAFE is not set +# CONFIG_GPTIMER_SUPPRESS_DEPRECATE_WARN is not set +# CONFIG_GPTIMER_ENABLE_DEBUG_LOG is not set +# end of GPTimer Configuration + +# +# PCNT Configuration +# +# CONFIG_PCNT_CTRL_FUNC_IN_IRAM is not set +# CONFIG_PCNT_ISR_IRAM_SAFE is not set +# CONFIG_PCNT_SUPPRESS_DEPRECATE_WARN is not set +# CONFIG_PCNT_ENABLE_DEBUG_LOG is not set +# end of PCNT Configuration + +# +# RMT Configuration +# +# CONFIG_RMT_ISR_IRAM_SAFE is not set +# CONFIG_RMT_SUPPRESS_DEPRECATE_WARN is not set +# CONFIG_RMT_ENABLE_DEBUG_LOG is not set +# end of RMT Configuration + +# +# MCPWM Configuration +# +# CONFIG_MCPWM_ISR_IRAM_SAFE is not set +# CONFIG_MCPWM_CTRL_FUNC_IN_IRAM is not set +# CONFIG_MCPWM_SUPPRESS_DEPRECATE_WARN is not set +# CONFIG_MCPWM_ENABLE_DEBUG_LOG is not set +# end of MCPWM Configuration + +# +# I2S Configuration +# +# CONFIG_I2S_ISR_IRAM_SAFE is not set +# CONFIG_I2S_SUPPRESS_DEPRECATE_WARN is not set +# CONFIG_I2S_ENABLE_DEBUG_LOG is not set +# end of I2S Configuration + +# +# DAC Configuration +# +# CONFIG_DAC_CTRL_FUNC_IN_IRAM is not set +# CONFIG_DAC_ISR_IRAM_SAFE is not set +# CONFIG_DAC_SUPPRESS_DEPRECATE_WARN is not set +# CONFIG_DAC_ENABLE_DEBUG_LOG is not set +CONFIG_DAC_DMA_AUTO_16BIT_ALIGN=y +# end of DAC Configuration + +# +# LEDC Configuration +# +# CONFIG_LEDC_CTRL_FUNC_IN_IRAM is not set +# end of LEDC Configuration + +# +# I2C Configuration +# +# CONFIG_I2C_ISR_IRAM_SAFE is not set +# CONFIG_I2C_ENABLE_DEBUG_LOG is not set +# end of I2C Configuration +# end of Driver Configurations + +# +# eFuse Bit Manager +# +# CONFIG_EFUSE_CUSTOM_TABLE is not set +# CONFIG_EFUSE_VIRTUAL is not set +# CONFIG_EFUSE_CODE_SCHEME_COMPAT_NONE is not set +CONFIG_EFUSE_CODE_SCHEME_COMPAT_3_4=y +# CONFIG_EFUSE_CODE_SCHEME_COMPAT_REPEAT is not set +CONFIG_EFUSE_MAX_BLK_LEN=192 +# end of eFuse Bit Manager + +# +# ESP-TLS +# +CONFIG_ESP_TLS_USING_MBEDTLS=y +# CONFIG_ESP_TLS_USE_SECURE_ELEMENT is not set +# CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS is not set +# CONFIG_ESP_TLS_SERVER is not set +# CONFIG_ESP_TLS_PSK_VERIFICATION is not set +# CONFIG_ESP_TLS_INSECURE is not set +# end of ESP-TLS + +# +# ADC and ADC Calibration +# +# CONFIG_ADC_ONESHOT_CTRL_FUNC_IN_IRAM is not set +# CONFIG_ADC_CONTINUOUS_ISR_IRAM_SAFE is not set + +# +# ADC Calibration Configurations +# +CONFIG_ADC_CALI_EFUSE_TP_ENABLE=y +CONFIG_ADC_CALI_EFUSE_VREF_ENABLE=y +CONFIG_ADC_CALI_LUT_ENABLE=y +# end of ADC Calibration Configurations + +CONFIG_ADC_DISABLE_DAC_OUTPUT=y +# end of ADC and ADC Calibration + +# +# Wireless Coexistence +# +# end of Wireless Coexistence + +# +# Common ESP-related +# +CONFIG_ESP_ERR_TO_NAME_LOOKUP=y +# end of Common ESP-related + +# +# Ethernet +# +CONFIG_ETH_ENABLED=y +CONFIG_ETH_USE_ESP32_EMAC=y +CONFIG_ETH_PHY_INTERFACE_RMII=y +CONFIG_ETH_RMII_CLK_INPUT=y +# CONFIG_ETH_RMII_CLK_OUTPUT is not set +CONFIG_ETH_RMII_CLK_IN_GPIO=0 +CONFIG_ETH_DMA_BUFFER_SIZE=512 +CONFIG_ETH_DMA_RX_BUFFER_NUM=10 +CONFIG_ETH_DMA_TX_BUFFER_NUM=10 +# CONFIG_ETH_IRAM_OPTIMIZATION is not set +CONFIG_ETH_USE_SPI_ETHERNET=y +# CONFIG_ETH_SPI_ETHERNET_DM9051 is not set +# CONFIG_ETH_SPI_ETHERNET_W5500 is not set +# CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL is not set +CONFIG_ETH_USE_OPENETH=y +# CONFIG_ETH_TRANSMIT_MUTEX is not set +# end of Ethernet + +# +# Event Loop Library +# +# CONFIG_ESP_EVENT_LOOP_PROFILING is not set +CONFIG_ESP_EVENT_POST_FROM_ISR=y +CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR=y +# end of Event Loop Library + +# +# GDB Stub +# +# CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME is not set +# end of GDB Stub + +# +# ESP HTTP client +# +# CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS is not set +# CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH is not set +# CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH is not set +# end of ESP HTTP client + +# +# HTTP Server +# +CONFIG_HTTPD_MAX_REQ_HDR_LEN=512 +CONFIG_HTTPD_MAX_URI_LEN=512 +CONFIG_HTTPD_ERR_RESP_NO_DELAY=y +CONFIG_HTTPD_PURGE_BUF_LEN=32 +# CONFIG_HTTPD_LOG_PURGE_DATA is not set +# CONFIG_HTTPD_WS_SUPPORT is not set +# CONFIG_HTTPD_QUEUE_WORK_BLOCKING is not set +# end of HTTP Server + +# +# ESP HTTPS OTA +# +# CONFIG_ESP_HTTPS_OTA_DECRYPT_CB is not set +# CONFIG_ESP_HTTPS_OTA_ALLOW_HTTP is not set +# end of ESP HTTPS OTA + +# +# ESP HTTPS server +# +# CONFIG_ESP_HTTPS_SERVER_ENABLE is not set +# end of ESP HTTPS server + +# +# Hardware Settings +# + +# +# Chip revision +# +CONFIG_ESP32_REV_MIN_0=y +# CONFIG_ESP32_REV_MIN_1 is not set +# CONFIG_ESP32_REV_MIN_1_1 is not set +# CONFIG_ESP32_REV_MIN_2 is not set +# CONFIG_ESP32_REV_MIN_3 is not set +# CONFIG_ESP32_REV_MIN_3_1 is not set +CONFIG_ESP32_REV_MIN=0 +CONFIG_ESP32_REV_MIN_FULL=0 +CONFIG_ESP_REV_MIN_FULL=0 + +# +# Maximum Supported ESP32 Revision (Rev v3.99) +# +CONFIG_ESP32_REV_MAX_FULL=399 +CONFIG_ESP_REV_MAX_FULL=399 +# end of Chip revision + +# +# MAC Config +# +CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_STA=y +CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP=y +CONFIG_ESP_MAC_ADDR_UNIVERSE_BT=y +CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH=y +CONFIG_ESP_MAC_UNIVERSAL_MAC_ADDRESSES_FOUR=y +# CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_TWO is not set +CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_FOUR=y +CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES=4 +# CONFIG_ESP_MAC_IGNORE_MAC_CRC_ERROR is not set +# end of MAC Config + +# +# Sleep Config +# +CONFIG_ESP_SLEEP_FLASH_LEAKAGE_WORKAROUND=y +CONFIG_ESP_SLEEP_PSRAM_LEAKAGE_WORKAROUND=y +# CONFIG_ESP_SLEEP_MSPI_NEED_ALL_IO_PU is not set +CONFIG_ESP_SLEEP_RTC_BUS_ISO_WORKAROUND=y +# CONFIG_ESP_SLEEP_GPIO_RESET_WORKAROUND is not set +CONFIG_ESP_SLEEP_DEEP_SLEEP_WAKEUP_DELAY=2000 +# CONFIG_ESP_SLEEP_CACHE_SAFE_ASSERTION is not set +# CONFIG_ESP_SLEEP_DEBUG is not set +# end of Sleep Config + +# +# RTC Clock Config +# +CONFIG_RTC_CLK_SRC_INT_RC=y +# CONFIG_RTC_CLK_SRC_EXT_CRYS is not set +# CONFIG_RTC_CLK_SRC_EXT_OSC is not set +# CONFIG_RTC_CLK_SRC_INT_8MD256 is not set +CONFIG_RTC_CLK_CAL_CYCLES=1024 +# end of RTC Clock Config + +# +# Peripheral Control +# +CONFIG_PERIPH_CTRL_FUNC_IN_IRAM=y +# end of Peripheral Control + +# +# Main XTAL Config +# +# CONFIG_XTAL_FREQ_26 is not set +CONFIG_XTAL_FREQ_40=y +# CONFIG_XTAL_FREQ_AUTO is not set +CONFIG_XTAL_FREQ=40 +# end of Main XTAL Config +# end of Hardware Settings + +# +# LCD and Touch Panel +# + +# +# LCD Touch Drivers are maintained in the IDF Component Registry +# + +# +# LCD Peripheral Configuration +# +CONFIG_LCD_PANEL_IO_FORMAT_BUF_SIZE=32 +# CONFIG_LCD_ENABLE_DEBUG_LOG is not set +# end of LCD Peripheral Configuration +# end of LCD and Touch Panel + +# +# ESP NETIF Adapter +# +CONFIG_ESP_NETIF_IP_LOST_TIMER_INTERVAL=120 +CONFIG_ESP_NETIF_TCPIP_LWIP=y +# CONFIG_ESP_NETIF_LOOPBACK is not set +CONFIG_ESP_NETIF_USES_TCPIP_WITH_BSD_API=y +# CONFIG_ESP_NETIF_RECEIVE_REPORT_ERRORS is not set +# CONFIG_ESP_NETIF_L2_TAP is not set +# CONFIG_ESP_NETIF_BRIDGE_EN is not set +# end of ESP NETIF Adapter + +# +# Partition API Configuration +# +# end of Partition API Configuration + +# +# PHY +# +CONFIG_ESP_PHY_CALIBRATION_AND_DATA_STORAGE=y +# CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION is not set +CONFIG_ESP_PHY_MAX_WIFI_TX_POWER=20 +CONFIG_ESP_PHY_MAX_TX_POWER=20 +# CONFIG_ESP_PHY_REDUCE_TX_POWER is not set +CONFIG_ESP_PHY_RF_CAL_PARTIAL=y +# CONFIG_ESP_PHY_RF_CAL_NONE is not set +# CONFIG_ESP_PHY_RF_CAL_FULL is not set +CONFIG_ESP_PHY_CALIBRATION_MODE=0 +# end of PHY + +# +# Power Management +# +# CONFIG_PM_ENABLE is not set +# end of Power Management + +# +# ESP PSRAM +# +CONFIG_SPIRAM=y + +# +# SPI RAM config +# +CONFIG_SPIRAM_MODE_QUAD=y +CONFIG_SPIRAM_TYPE_AUTO=y +# CONFIG_SPIRAM_TYPE_ESPPSRAM16 is not set +# CONFIG_SPIRAM_TYPE_ESPPSRAM32 is not set +# CONFIG_SPIRAM_TYPE_ESPPSRAM64 is not set +CONFIG_SPIRAM_SPEED_40M=y +CONFIG_SPIRAM_SPEED=40 +CONFIG_SPIRAM_BOOT_INIT=y +# CONFIG_SPIRAM_IGNORE_NOTFOUND is not set +# CONFIG_SPIRAM_USE_MEMMAP is not set +# CONFIG_SPIRAM_USE_CAPS_ALLOC is not set +CONFIG_SPIRAM_USE_MALLOC=y +CONFIG_SPIRAM_MEMTEST=y +CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=16384 +# CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is not set +CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=32768 +# CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY is not set +# CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY is not set +CONFIG_SPIRAM_CACHE_WORKAROUND=y + +# +# SPIRAM cache workaround debugging +# +CONFIG_SPIRAM_CACHE_WORKAROUND_STRATEGY_MEMW=y +# CONFIG_SPIRAM_CACHE_WORKAROUND_STRATEGY_DUPLDST is not set +# CONFIG_SPIRAM_CACHE_WORKAROUND_STRATEGY_NOPS is not set +# end of SPIRAM cache workaround debugging + +# +# SPIRAM workaround libraries placement +# +CONFIG_SPIRAM_CACHE_LIBJMP_IN_IRAM=y +CONFIG_SPIRAM_CACHE_LIBMATH_IN_IRAM=y +CONFIG_SPIRAM_CACHE_LIBNUMPARSER_IN_IRAM=y +CONFIG_SPIRAM_CACHE_LIBIO_IN_IRAM=y +CONFIG_SPIRAM_CACHE_LIBTIME_IN_IRAM=y +CONFIG_SPIRAM_CACHE_LIBCHAR_IN_IRAM=y +CONFIG_SPIRAM_CACHE_LIBMEM_IN_IRAM=y +CONFIG_SPIRAM_CACHE_LIBSTR_IN_IRAM=y +CONFIG_SPIRAM_CACHE_LIBRAND_IN_IRAM=y +CONFIG_SPIRAM_CACHE_LIBENV_IN_IRAM=y +CONFIG_SPIRAM_CACHE_LIBFILE_IN_IRAM=y +CONFIG_SPIRAM_CACHE_LIBMISC_IN_IRAM=y +# end of SPIRAM workaround libraries placement + +CONFIG_SPIRAM_BANKSWITCH_ENABLE=y +CONFIG_SPIRAM_BANKSWITCH_RESERVE=8 +# CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY is not set + +# +# PSRAM clock and cs IO for ESP32-DOWD +# +CONFIG_D0WD_PSRAM_CLK_IO=17 +CONFIG_D0WD_PSRAM_CS_IO=16 +# end of PSRAM clock and cs IO for ESP32-DOWD + +# +# PSRAM clock and cs IO for ESP32-D2WD +# +CONFIG_D2WD_PSRAM_CLK_IO=9 +CONFIG_D2WD_PSRAM_CS_IO=10 +# end of PSRAM clock and cs IO for ESP32-D2WD + +# +# PSRAM clock and cs IO for ESP32-PICO-D4 +# +CONFIG_PICO_PSRAM_CS_IO=10 +# end of PSRAM clock and cs IO for ESP32-PICO-D4 + +# CONFIG_SPIRAM_CUSTOM_SPIWP_SD3_PIN is not set +CONFIG_SPIRAM_SPIWP_SD3_PIN=7 +# CONFIG_SPIRAM_2T_MODE is not set +# end of SPI RAM config +# end of ESP PSRAM + +# +# ESP Ringbuf +# +# CONFIG_RINGBUF_PLACE_FUNCTIONS_INTO_FLASH is not set +# end of ESP Ringbuf + +# +# ESP System Settings +# +# CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_80 is not set +CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_160=y +# CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240 is not set +CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ=160 + +# +# Memory +# +# CONFIG_ESP32_USE_FIXED_STATIC_RAM_SIZE is not set + +# +# Non-backward compatible options +# +# CONFIG_ESP_SYSTEM_ESP32_SRAM1_REGION_AS_IRAM is not set +# end of Non-backward compatible options +# end of Memory + +# +# Trace memory +# +# CONFIG_ESP32_TRAX is not set +CONFIG_ESP32_TRACEMEM_RESERVE_DRAM=0x0 +# end of Trace memory + +# CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT is not set +CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT=y +# CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT is not set +# CONFIG_ESP_SYSTEM_PANIC_GDBSTUB is not set +CONFIG_ESP_SYSTEM_PANIC_REBOOT_DELAY_SECONDS=0 + +# +# Memory protection +# +# end of Memory protection + +CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE=32 +CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2304 +CONFIG_ESP_MAIN_TASK_STACK_SIZE=10000 +CONFIG_ESP_MAIN_TASK_AFFINITY_CPU0=y +# CONFIG_ESP_MAIN_TASK_AFFINITY_CPU1 is not set +# CONFIG_ESP_MAIN_TASK_AFFINITY_NO_AFFINITY is not set +CONFIG_ESP_MAIN_TASK_AFFINITY=0x0 +CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE=2048 +CONFIG_ESP_CONSOLE_UART_DEFAULT=y +# CONFIG_ESP_CONSOLE_UART_CUSTOM is not set +# CONFIG_ESP_CONSOLE_NONE is not set +CONFIG_ESP_CONSOLE_UART=y +CONFIG_ESP_CONSOLE_MULTIPLE_UART=y +CONFIG_ESP_CONSOLE_UART_NUM=0 +CONFIG_ESP_CONSOLE_UART_BAUDRATE=115200 +CONFIG_ESP_INT_WDT=y +CONFIG_ESP_INT_WDT_TIMEOUT_MS=300 +CONFIG_ESP_INT_WDT_CHECK_CPU1=y +CONFIG_ESP_TASK_WDT_EN=y +CONFIG_ESP_TASK_WDT_INIT=y +# CONFIG_ESP_TASK_WDT_PANIC is not set +CONFIG_ESP_TASK_WDT_TIMEOUT_S=5 +CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=y +CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1=y +# CONFIG_ESP_PANIC_HANDLER_IRAM is not set +# CONFIG_ESP_DEBUG_STUBS_ENABLE is not set +CONFIG_ESP_DEBUG_OCDAWARE=y +# CONFIG_ESP_SYSTEM_CHECK_INT_LEVEL_5 is not set +CONFIG_ESP_SYSTEM_CHECK_INT_LEVEL_4=y + +# +# Brownout Detector +# +CONFIG_ESP_BROWNOUT_DET=y +CONFIG_ESP_BROWNOUT_DET_LVL_SEL_0=y +# CONFIG_ESP_BROWNOUT_DET_LVL_SEL_1 is not set +# CONFIG_ESP_BROWNOUT_DET_LVL_SEL_2 is not set +# CONFIG_ESP_BROWNOUT_DET_LVL_SEL_3 is not set +# CONFIG_ESP_BROWNOUT_DET_LVL_SEL_4 is not set +# CONFIG_ESP_BROWNOUT_DET_LVL_SEL_5 is not set +# CONFIG_ESP_BROWNOUT_DET_LVL_SEL_6 is not set +# CONFIG_ESP_BROWNOUT_DET_LVL_SEL_7 is not set +CONFIG_ESP_BROWNOUT_DET_LVL=0 +# end of Brownout Detector + +# CONFIG_ESP32_DISABLE_BASIC_ROM_CONSOLE is not set +CONFIG_ESP32_ECO3_CACHE_LOCK_FIX=y +CONFIG_ESP_SYSTEM_BROWNOUT_INTR=y +# end of ESP System Settings + +# +# IPC (Inter-Processor Call) +# +CONFIG_ESP_IPC_TASK_STACK_SIZE=1024 +CONFIG_ESP_IPC_USES_CALLERS_PRIORITY=y +CONFIG_ESP_IPC_ISR_ENABLE=y +# end of IPC (Inter-Processor Call) + +# +# High resolution timer (esp_timer) +# +# CONFIG_ESP_TIMER_PROFILING is not set +CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER=y +CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER=y +CONFIG_ESP_TIMER_TASK_STACK_SIZE=3584 +CONFIG_ESP_TIMER_INTERRUPT_LEVEL=1 +# CONFIG_ESP_TIMER_SHOW_EXPERIMENTAL is not set +CONFIG_ESP_TIMER_TASK_AFFINITY=0x0 +CONFIG_ESP_TIMER_TASK_AFFINITY_CPU0=y +CONFIG_ESP_TIMER_ISR_AFFINITY=0x1 +CONFIG_ESP_TIMER_ISR_AFFINITY_CPU0=y +# CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD is not set +CONFIG_ESP_TIMER_IMPL_TG0_LAC=y +# end of High resolution timer (esp_timer) + +# +# Wi-Fi +# +CONFIG_ESP_WIFI_ENABLED=y +CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM=10 +CONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM=32 +CONFIG_ESP_WIFI_STATIC_TX_BUFFER=y +CONFIG_ESP_WIFI_TX_BUFFER_TYPE=0 +CONFIG_ESP_WIFI_STATIC_TX_BUFFER_NUM=16 +CONFIG_ESP_WIFI_CACHE_TX_BUFFER_NUM=32 +# CONFIG_ESP_WIFI_CSI_ENABLED is not set +CONFIG_ESP_WIFI_AMPDU_TX_ENABLED=y +CONFIG_ESP_WIFI_TX_BA_WIN=6 +CONFIG_ESP_WIFI_AMPDU_RX_ENABLED=y +CONFIG_ESP_WIFI_RX_BA_WIN=6 +# CONFIG_ESP_WIFI_AMSDU_TX_ENABLED is not set +CONFIG_ESP_WIFI_NVS_ENABLED=y +CONFIG_ESP_WIFI_TASK_PINNED_TO_CORE_0=y +# CONFIG_ESP_WIFI_TASK_PINNED_TO_CORE_1 is not set +CONFIG_ESP_WIFI_SOFTAP_BEACON_MAX_LEN=752 +CONFIG_ESP_WIFI_MGMT_SBUF_NUM=32 +CONFIG_ESP_WIFI_IRAM_OPT=y +# CONFIG_ESP_WIFI_EXTRA_IRAM_OPT is not set +CONFIG_ESP_WIFI_RX_IRAM_OPT=y +CONFIG_ESP_WIFI_ENABLE_WPA3_SAE=y +CONFIG_ESP_WIFI_ENABLE_SAE_PK=y +CONFIG_ESP_WIFI_SOFTAP_SAE_SUPPORT=y +CONFIG_ESP_WIFI_ENABLE_WPA3_OWE_STA=y +# CONFIG_ESP_WIFI_SLP_IRAM_OPT is not set +CONFIG_ESP_WIFI_STA_DISCONNECTED_PM_ENABLE=y +# CONFIG_ESP_WIFI_GMAC_SUPPORT is not set +CONFIG_ESP_WIFI_SOFTAP_SUPPORT=y +# CONFIG_ESP_WIFI_SLP_BEACON_LOST_OPT is not set +CONFIG_ESP_WIFI_ESPNOW_MAX_ENCRYPT_NUM=7 +# CONFIG_ESP_WIFI_NAN_ENABLE is not set +CONFIG_ESP_WIFI_MBEDTLS_CRYPTO=y +CONFIG_ESP_WIFI_MBEDTLS_TLS_CLIENT=y +# CONFIG_ESP_WIFI_WAPI_PSK is not set +# CONFIG_ESP_WIFI_SUITE_B_192 is not set +# CONFIG_ESP_WIFI_11KV_SUPPORT is not set +# CONFIG_ESP_WIFI_MBO_SUPPORT is not set +# CONFIG_ESP_WIFI_DPP_SUPPORT is not set +# CONFIG_ESP_WIFI_11R_SUPPORT is not set +# CONFIG_ESP_WIFI_WPS_SOFTAP_REGISTRAR is not set + +# +# WPS Configuration Options +# +# CONFIG_ESP_WIFI_WPS_STRICT is not set +# CONFIG_ESP_WIFI_WPS_PASSPHRASE is not set +# end of WPS Configuration Options + +# CONFIG_ESP_WIFI_DEBUG_PRINT is not set +# CONFIG_ESP_WIFI_TESTING_OPTIONS is not set +# end of Wi-Fi + +# +# Core dump +# +# CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH is not set +# CONFIG_ESP_COREDUMP_ENABLE_TO_UART is not set +CONFIG_ESP_COREDUMP_ENABLE_TO_NONE=y +# end of Core dump + +# +# FAT Filesystem support +# +CONFIG_FATFS_VOLUME_COUNT=2 +CONFIG_FATFS_LFN_NONE=y +# CONFIG_FATFS_LFN_HEAP is not set +# CONFIG_FATFS_LFN_STACK is not set +# CONFIG_FATFS_SECTOR_512 is not set +CONFIG_FATFS_SECTOR_4096=y +# CONFIG_FATFS_CODEPAGE_DYNAMIC is not set +CONFIG_FATFS_CODEPAGE_437=y +# CONFIG_FATFS_CODEPAGE_720 is not set +# CONFIG_FATFS_CODEPAGE_737 is not set +# CONFIG_FATFS_CODEPAGE_771 is not set +# CONFIG_FATFS_CODEPAGE_775 is not set +# CONFIG_FATFS_CODEPAGE_850 is not set +# CONFIG_FATFS_CODEPAGE_852 is not set +# CONFIG_FATFS_CODEPAGE_855 is not set +# CONFIG_FATFS_CODEPAGE_857 is not set +# CONFIG_FATFS_CODEPAGE_860 is not set +# CONFIG_FATFS_CODEPAGE_861 is not set +# CONFIG_FATFS_CODEPAGE_862 is not set +# CONFIG_FATFS_CODEPAGE_863 is not set +# CONFIG_FATFS_CODEPAGE_864 is not set +# CONFIG_FATFS_CODEPAGE_865 is not set +# CONFIG_FATFS_CODEPAGE_866 is not set +# CONFIG_FATFS_CODEPAGE_869 is not set +# CONFIG_FATFS_CODEPAGE_932 is not set +# CONFIG_FATFS_CODEPAGE_936 is not set +# CONFIG_FATFS_CODEPAGE_949 is not set +# CONFIG_FATFS_CODEPAGE_950 is not set +CONFIG_FATFS_CODEPAGE=437 +CONFIG_FATFS_FS_LOCK=0 +CONFIG_FATFS_TIMEOUT_MS=10000 +CONFIG_FATFS_PER_FILE_CACHE=y +CONFIG_FATFS_ALLOC_PREFER_EXTRAM=y +# CONFIG_FATFS_USE_FASTSEEK is not set +CONFIG_FATFS_VFS_FSTAT_BLKSIZE=0 +# end of FAT Filesystem support + +# +# FreeRTOS +# + +# +# Kernel +# +# CONFIG_FREERTOS_SMP is not set +# CONFIG_FREERTOS_UNICORE is not set +CONFIG_FREERTOS_HZ=100 +# CONFIG_FREERTOS_CHECK_STACKOVERFLOW_NONE is not set +# CONFIG_FREERTOS_CHECK_STACKOVERFLOW_PTRVAL is not set +CONFIG_FREERTOS_CHECK_STACKOVERFLOW_CANARY=y +CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS=1 +CONFIG_FREERTOS_IDLE_TASK_STACKSIZE=1536 +# CONFIG_FREERTOS_USE_IDLE_HOOK is not set +# CONFIG_FREERTOS_USE_TICK_HOOK is not set +CONFIG_FREERTOS_MAX_TASK_NAME_LEN=16 +# CONFIG_FREERTOS_ENABLE_BACKWARD_COMPATIBILITY is not set +CONFIG_FREERTOS_TIMER_TASK_PRIORITY=1 +CONFIG_FREERTOS_TIMER_TASK_STACK_DEPTH=2048 +CONFIG_FREERTOS_TIMER_QUEUE_LENGTH=10 +CONFIG_FREERTOS_QUEUE_REGISTRY_SIZE=0 +CONFIG_FREERTOS_TASK_NOTIFICATION_ARRAY_ENTRIES=1 +# CONFIG_FREERTOS_USE_TRACE_FACILITY is not set +# CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS is not set +# end of Kernel + +# +# Port +# +CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER=y +# CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK is not set +CONFIG_FREERTOS_TLSP_DELETION_CALLBACKS=y +# CONFIG_FREERTOS_TASK_PRE_DELETION_HOOK is not set +# CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP is not set +CONFIG_FREERTOS_CHECK_MUTEX_GIVEN_BY_OWNER=y +CONFIG_FREERTOS_ISR_STACKSIZE=1536 +CONFIG_FREERTOS_INTERRUPT_BACKTRACE=y +# CONFIG_FREERTOS_FPU_IN_ISR is not set +CONFIG_FREERTOS_TICK_SUPPORT_CORETIMER=y +CONFIG_FREERTOS_CORETIMER_0=y +# CONFIG_FREERTOS_CORETIMER_1 is not set +CONFIG_FREERTOS_SYSTICK_USES_CCOUNT=y +# CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH is not set +# CONFIG_FREERTOS_PLACE_SNAPSHOT_FUNS_INTO_FLASH is not set +# CONFIG_FREERTOS_CHECK_PORT_CRITICAL_COMPLIANCE is not set +# end of Port + +CONFIG_FREERTOS_PORT=y +CONFIG_FREERTOS_NO_AFFINITY=0x7FFFFFFF +CONFIG_FREERTOS_SUPPORT_STATIC_ALLOCATION=y +CONFIG_FREERTOS_DEBUG_OCDAWARE=y +CONFIG_FREERTOS_ENABLE_TASK_SNAPSHOT=y +# end of FreeRTOS + +# +# Hardware Abstraction Layer (HAL) and Low Level (LL) +# +CONFIG_HAL_ASSERTION_EQUALS_SYSTEM=y +# CONFIG_HAL_ASSERTION_DISABLE is not set +# CONFIG_HAL_ASSERTION_SILENT is not set +# CONFIG_HAL_ASSERTION_ENABLE is not set +CONFIG_HAL_DEFAULT_ASSERTION_LEVEL=2 +CONFIG_HAL_SPI_MASTER_FUNC_IN_IRAM=y +CONFIG_HAL_SPI_SLAVE_FUNC_IN_IRAM=y +# end of Hardware Abstraction Layer (HAL) and Low Level (LL) + +# +# Heap memory debugging +# +CONFIG_HEAP_POISONING_DISABLED=y +# CONFIG_HEAP_POISONING_LIGHT is not set +# CONFIG_HEAP_POISONING_COMPREHENSIVE is not set +CONFIG_HEAP_TRACING_OFF=y +# CONFIG_HEAP_TRACING_STANDALONE is not set +# CONFIG_HEAP_TRACING_TOHOST is not set +# CONFIG_HEAP_USE_HOOKS is not set +# CONFIG_HEAP_ABORT_WHEN_ALLOCATION_FAILS is not set +# CONFIG_HEAP_PLACE_FUNCTION_INTO_FLASH is not set +# end of Heap memory debugging + +CONFIG_IEEE802154_CCA_THRESHOLD=-60 +CONFIG_IEEE802154_PENDING_TABLE_SIZE=20 + +# +# Log output +# +# CONFIG_LOG_DEFAULT_LEVEL_NONE is not set +# CONFIG_LOG_DEFAULT_LEVEL_ERROR is not set +# CONFIG_LOG_DEFAULT_LEVEL_WARN is not set +CONFIG_LOG_DEFAULT_LEVEL_INFO=y +# CONFIG_LOG_DEFAULT_LEVEL_DEBUG is not set +# CONFIG_LOG_DEFAULT_LEVEL_VERBOSE is not set +CONFIG_LOG_DEFAULT_LEVEL=3 +CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT=y +# CONFIG_LOG_MAXIMUM_LEVEL_DEBUG is not set +# CONFIG_LOG_MAXIMUM_LEVEL_VERBOSE is not set +CONFIG_LOG_MAXIMUM_LEVEL=3 +# CONFIG_LOG_MASTER_LEVEL is not set +CONFIG_LOG_COLORS=y +CONFIG_LOG_TIMESTAMP_SOURCE_RTOS=y +# CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM is not set +# end of Log output + +# +# LWIP +# +CONFIG_LWIP_LOCAL_HOSTNAME="espressif" +# CONFIG_LWIP_NETIF_API is not set +CONFIG_LWIP_TCPIP_TASK_PRIO=18 +# CONFIG_LWIP_TCPIP_CORE_LOCKING is not set +# CONFIG_LWIP_CHECK_THREAD_SAFETY is not set +CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y +# CONFIG_LWIP_L2_TO_L3_COPY is not set +# CONFIG_LWIP_IRAM_OPTIMIZATION is not set +# CONFIG_LWIP_EXTRA_IRAM_OPTIMIZATION is not set +CONFIG_LWIP_TIMERS_ONDEMAND=y +CONFIG_LWIP_ND6=y +CONFIG_LWIP_MAX_SOCKETS=10 +# CONFIG_LWIP_USE_ONLY_LWIP_SELECT is not set +# CONFIG_LWIP_SO_LINGER is not set +CONFIG_LWIP_SO_REUSE=y +CONFIG_LWIP_SO_REUSE_RXTOALL=y +# CONFIG_LWIP_SO_RCVBUF is not set +# CONFIG_LWIP_NETBUF_RECVINFO is not set +CONFIG_LWIP_IP4_FRAG=y +CONFIG_LWIP_IP6_FRAG=y +# CONFIG_LWIP_IP4_REASSEMBLY is not set +# CONFIG_LWIP_IP6_REASSEMBLY is not set +CONFIG_LWIP_IP_REASS_MAX_PBUFS=10 +# CONFIG_LWIP_IP_FORWARD is not set +# CONFIG_LWIP_STATS is not set +CONFIG_LWIP_ESP_GRATUITOUS_ARP=y +CONFIG_LWIP_GARP_TMR_INTERVAL=60 +CONFIG_LWIP_ESP_MLDV6_REPORT=y +CONFIG_LWIP_MLDV6_TMR_INTERVAL=40 +CONFIG_LWIP_TCPIP_RECVMBOX_SIZE=32 +CONFIG_LWIP_DHCP_DOES_ARP_CHECK=y +# CONFIG_LWIP_DHCP_DISABLE_CLIENT_ID is not set +CONFIG_LWIP_DHCP_DISABLE_VENDOR_CLASS_ID=y +# CONFIG_LWIP_DHCP_RESTORE_LAST_IP is not set +CONFIG_LWIP_DHCP_OPTIONS_LEN=68 +CONFIG_LWIP_NUM_NETIF_CLIENT_DATA=0 +CONFIG_LWIP_DHCP_COARSE_TIMER_SECS=1 + +# +# DHCP server +# +CONFIG_LWIP_DHCPS=y +CONFIG_LWIP_DHCPS_LEASE_UNIT=60 +CONFIG_LWIP_DHCPS_MAX_STATION_NUM=8 +# end of DHCP server + +# CONFIG_LWIP_AUTOIP is not set +CONFIG_LWIP_IPV4=y +CONFIG_LWIP_IPV6=y +# CONFIG_LWIP_IPV6_AUTOCONFIG is not set +CONFIG_LWIP_IPV6_NUM_ADDRESSES=3 +# CONFIG_LWIP_IPV6_FORWARD is not set +# CONFIG_LWIP_NETIF_STATUS_CALLBACK is not set +CONFIG_LWIP_NETIF_LOOPBACK=y +CONFIG_LWIP_LOOPBACK_MAX_PBUFS=8 + +# +# TCP +# +CONFIG_LWIP_MAX_ACTIVE_TCP=16 +CONFIG_LWIP_MAX_LISTENING_TCP=16 +CONFIG_LWIP_TCP_HIGH_SPEED_RETRANSMISSION=y +CONFIG_LWIP_TCP_MAXRTX=12 +CONFIG_LWIP_TCP_SYNMAXRTX=12 +CONFIG_LWIP_TCP_MSS=1440 +CONFIG_LWIP_TCP_TMR_INTERVAL=250 +CONFIG_LWIP_TCP_MSL=60000 +CONFIG_LWIP_TCP_FIN_WAIT_TIMEOUT=20000 +CONFIG_LWIP_TCP_SND_BUF_DEFAULT=5744 +CONFIG_LWIP_TCP_WND_DEFAULT=5744 +CONFIG_LWIP_TCP_RECVMBOX_SIZE=6 +CONFIG_LWIP_TCP_QUEUE_OOSEQ=y +# CONFIG_LWIP_TCP_SACK_OUT is not set +CONFIG_LWIP_TCP_OVERSIZE_MSS=y +# CONFIG_LWIP_TCP_OVERSIZE_QUARTER_MSS is not set +# CONFIG_LWIP_TCP_OVERSIZE_DISABLE is not set +CONFIG_LWIP_TCP_RTO_TIME=1500 +# end of TCP + +# +# UDP +# +CONFIG_LWIP_MAX_UDP_PCBS=16 +CONFIG_LWIP_UDP_RECVMBOX_SIZE=6 +# end of UDP + +# +# Checksums +# +# CONFIG_LWIP_CHECKSUM_CHECK_IP is not set +# CONFIG_LWIP_CHECKSUM_CHECK_UDP is not set +CONFIG_LWIP_CHECKSUM_CHECK_ICMP=y +# end of Checksums + +CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=3072 +CONFIG_LWIP_TCPIP_TASK_AFFINITY_NO_AFFINITY=y +# CONFIG_LWIP_TCPIP_TASK_AFFINITY_CPU0 is not set +# CONFIG_LWIP_TCPIP_TASK_AFFINITY_CPU1 is not set +CONFIG_LWIP_TCPIP_TASK_AFFINITY=0x7FFFFFFF +# CONFIG_LWIP_PPP_SUPPORT is not set +CONFIG_LWIP_IPV6_MEMP_NUM_ND6_QUEUE=3 +CONFIG_LWIP_IPV6_ND6_NUM_NEIGHBORS=5 +# CONFIG_LWIP_SLIP_SUPPORT is not set + +# +# ICMP +# +CONFIG_LWIP_ICMP=y +# CONFIG_LWIP_MULTICAST_PING is not set +# CONFIG_LWIP_BROADCAST_PING is not set +# end of ICMP + +# +# LWIP RAW API +# +CONFIG_LWIP_MAX_RAW_PCBS=16 +# end of LWIP RAW API + +# +# SNTP +# +CONFIG_LWIP_SNTP_MAX_SERVERS=1 +# CONFIG_LWIP_DHCP_GET_NTP_SRV is not set +CONFIG_LWIP_SNTP_UPDATE_DELAY=3600000 +# end of SNTP + +CONFIG_LWIP_BRIDGEIF_MAX_PORTS=7 +CONFIG_LWIP_ESP_LWIP_ASSERT=y + +# +# Hooks +# +# CONFIG_LWIP_HOOK_TCP_ISN_NONE is not set +CONFIG_LWIP_HOOK_TCP_ISN_DEFAULT=y +# CONFIG_LWIP_HOOK_TCP_ISN_CUSTOM is not set +CONFIG_LWIP_HOOK_IP6_ROUTE_NONE=y +# CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT is not set +# CONFIG_LWIP_HOOK_IP6_ROUTE_CUSTOM is not set +CONFIG_LWIP_HOOK_ND6_GET_GW_NONE=y +# CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT is not set +# CONFIG_LWIP_HOOK_ND6_GET_GW_CUSTOM is not set +CONFIG_LWIP_HOOK_IP6_SELECT_SRC_ADDR_NONE=y +# CONFIG_LWIP_HOOK_IP6_SELECT_SRC_ADDR_DEFAULT is not set +# CONFIG_LWIP_HOOK_IP6_SELECT_SRC_ADDR_CUSTOM is not set +CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_NONE=y +# CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_DEFAULT is not set +# CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_CUSTOM is not set +CONFIG_LWIP_HOOK_IP6_INPUT_NONE=y +# CONFIG_LWIP_HOOK_IP6_INPUT_DEFAULT is not set +# CONFIG_LWIP_HOOK_IP6_INPUT_CUSTOM is not set +# end of Hooks + +# CONFIG_LWIP_DEBUG is not set +# end of LWIP + +# +# mbedTLS +# +CONFIG_MBEDTLS_INTERNAL_MEM_ALLOC=y +# CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC is not set +# CONFIG_MBEDTLS_DEFAULT_MEM_ALLOC is not set +# CONFIG_MBEDTLS_CUSTOM_MEM_ALLOC is not set +CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y +CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=16384 +CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=4096 +# CONFIG_MBEDTLS_DYNAMIC_BUFFER is not set +# CONFIG_MBEDTLS_DEBUG is not set + +# +# mbedTLS v3.x related +# +# CONFIG_MBEDTLS_SSL_PROTO_TLS1_3 is not set +# CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH is not set +# CONFIG_MBEDTLS_X509_TRUSTED_CERT_CALLBACK is not set +# CONFIG_MBEDTLS_SSL_CONTEXT_SERIALIZATION is not set +CONFIG_MBEDTLS_SSL_KEEP_PEER_CERTIFICATE=y +CONFIG_MBEDTLS_PKCS7_C=y +# end of mbedTLS v3.x related + +# +# Certificate Bundle +# +CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y +CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=y +# CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN is not set +# CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_NONE is not set +# CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE is not set +CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_MAX_CERTS=200 +# end of Certificate Bundle + +# CONFIG_MBEDTLS_ECP_RESTARTABLE is not set +# CONFIG_MBEDTLS_CMAC_C is not set +CONFIG_MBEDTLS_HARDWARE_AES=y +CONFIG_MBEDTLS_HARDWARE_MPI=y +CONFIG_MBEDTLS_HARDWARE_SHA=y +CONFIG_MBEDTLS_ROM_MD5=y +# CONFIG_MBEDTLS_ATCA_HW_ECDSA_SIGN is not set +# CONFIG_MBEDTLS_ATCA_HW_ECDSA_VERIFY is not set +CONFIG_MBEDTLS_HAVE_TIME=y +# CONFIG_MBEDTLS_PLATFORM_TIME_ALT is not set +# CONFIG_MBEDTLS_HAVE_TIME_DATE is not set +CONFIG_MBEDTLS_ECDSA_DETERMINISTIC=y +CONFIG_MBEDTLS_SHA512_C=y +CONFIG_MBEDTLS_TLS_SERVER_AND_CLIENT=y +# CONFIG_MBEDTLS_TLS_SERVER_ONLY is not set +# CONFIG_MBEDTLS_TLS_CLIENT_ONLY is not set +# CONFIG_MBEDTLS_TLS_DISABLED is not set +CONFIG_MBEDTLS_TLS_SERVER=y +CONFIG_MBEDTLS_TLS_CLIENT=y +CONFIG_MBEDTLS_TLS_ENABLED=y + +# +# TLS Key Exchange Methods +# +# CONFIG_MBEDTLS_PSK_MODES is not set +CONFIG_MBEDTLS_KEY_EXCHANGE_RSA=y +CONFIG_MBEDTLS_KEY_EXCHANGE_ELLIPTIC_CURVE=y +CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_RSA=y +CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA=y +CONFIG_MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA=y +CONFIG_MBEDTLS_KEY_EXCHANGE_ECDH_RSA=y +# end of TLS Key Exchange Methods + +CONFIG_MBEDTLS_SSL_RENEGOTIATION=y +CONFIG_MBEDTLS_SSL_PROTO_TLS1_2=y +# CONFIG_MBEDTLS_SSL_PROTO_GMTSSL1_1 is not set +# CONFIG_MBEDTLS_SSL_PROTO_DTLS is not set +CONFIG_MBEDTLS_SSL_ALPN=y +CONFIG_MBEDTLS_CLIENT_SSL_SESSION_TICKETS=y +CONFIG_MBEDTLS_SERVER_SSL_SESSION_TICKETS=y + +# +# Symmetric Ciphers +# +CONFIG_MBEDTLS_AES_C=y +# CONFIG_MBEDTLS_CAMELLIA_C is not set +# CONFIG_MBEDTLS_DES_C is not set +# CONFIG_MBEDTLS_BLOWFISH_C is not set +# CONFIG_MBEDTLS_XTEA_C is not set +CONFIG_MBEDTLS_CCM_C=y +CONFIG_MBEDTLS_GCM_C=y +# CONFIG_MBEDTLS_NIST_KW_C is not set +# end of Symmetric Ciphers + +# CONFIG_MBEDTLS_RIPEMD160_C is not set + +# +# Certificates +# +CONFIG_MBEDTLS_PEM_PARSE_C=y +CONFIG_MBEDTLS_PEM_WRITE_C=y +CONFIG_MBEDTLS_X509_CRL_PARSE_C=y +CONFIG_MBEDTLS_X509_CSR_PARSE_C=y +# end of Certificates + +CONFIG_MBEDTLS_ECP_C=y +# CONFIG_MBEDTLS_DHM_C is not set +CONFIG_MBEDTLS_ECDH_C=y +CONFIG_MBEDTLS_ECDSA_C=y +# CONFIG_MBEDTLS_ECJPAKE_C is not set +CONFIG_MBEDTLS_ECP_DP_SECP192R1_ENABLED=y +CONFIG_MBEDTLS_ECP_DP_SECP224R1_ENABLED=y +CONFIG_MBEDTLS_ECP_DP_SECP256R1_ENABLED=y +CONFIG_MBEDTLS_ECP_DP_SECP384R1_ENABLED=y +CONFIG_MBEDTLS_ECP_DP_SECP521R1_ENABLED=y +CONFIG_MBEDTLS_ECP_DP_SECP192K1_ENABLED=y +CONFIG_MBEDTLS_ECP_DP_SECP224K1_ENABLED=y +CONFIG_MBEDTLS_ECP_DP_SECP256K1_ENABLED=y +CONFIG_MBEDTLS_ECP_DP_BP256R1_ENABLED=y +CONFIG_MBEDTLS_ECP_DP_BP384R1_ENABLED=y +CONFIG_MBEDTLS_ECP_DP_BP512R1_ENABLED=y +CONFIG_MBEDTLS_ECP_DP_CURVE25519_ENABLED=y +CONFIG_MBEDTLS_ECP_NIST_OPTIM=y +CONFIG_MBEDTLS_ECP_FIXED_POINT_OPTIM=y +# CONFIG_MBEDTLS_POLY1305_C is not set +# CONFIG_MBEDTLS_CHACHA20_C is not set +# CONFIG_MBEDTLS_HKDF_C is not set +# CONFIG_MBEDTLS_THREADING_C is not set +# CONFIG_MBEDTLS_LARGE_KEY_SOFTWARE_MPI is not set +# CONFIG_MBEDTLS_SECURITY_RISKS is not set +# end of mbedTLS + +# +# ESP-MQTT Configurations +# +CONFIG_MQTT_PROTOCOL_311=y +# CONFIG_MQTT_PROTOCOL_5 is not set +CONFIG_MQTT_TRANSPORT_SSL=y +CONFIG_MQTT_TRANSPORT_WEBSOCKET=y +CONFIG_MQTT_TRANSPORT_WEBSOCKET_SECURE=y +# CONFIG_MQTT_MSG_ID_INCREMENTAL is not set +# CONFIG_MQTT_SKIP_PUBLISH_IF_DISCONNECTED is not set +# CONFIG_MQTT_REPORT_DELETED_MESSAGES is not set +# CONFIG_MQTT_USE_CUSTOM_CONFIG is not set +# CONFIG_MQTT_TASK_CORE_SELECTION_ENABLED is not set +# CONFIG_MQTT_CUSTOM_OUTBOX is not set +# end of ESP-MQTT Configurations + +# +# Newlib +# +CONFIG_NEWLIB_STDOUT_LINE_ENDING_CRLF=y +# CONFIG_NEWLIB_STDOUT_LINE_ENDING_LF is not set +# CONFIG_NEWLIB_STDOUT_LINE_ENDING_CR is not set +# CONFIG_NEWLIB_STDIN_LINE_ENDING_CRLF is not set +# CONFIG_NEWLIB_STDIN_LINE_ENDING_LF is not set +CONFIG_NEWLIB_STDIN_LINE_ENDING_CR=y +# CONFIG_NEWLIB_NANO_FORMAT is not set +CONFIG_NEWLIB_TIME_SYSCALL_USE_RTC_HRT=y +# CONFIG_NEWLIB_TIME_SYSCALL_USE_RTC is not set +# CONFIG_NEWLIB_TIME_SYSCALL_USE_HRT is not set +# CONFIG_NEWLIB_TIME_SYSCALL_USE_NONE is not set +# end of Newlib + +# +# NVS +# +# CONFIG_NVS_ASSERT_ERROR_CHECK is not set +# end of NVS + +# +# OpenThread +# +# CONFIG_OPENTHREAD_ENABLED is not set + +# +# Thread Operational Dataset +# +CONFIG_OPENTHREAD_NETWORK_NAME="OpenThread-ESP" +CONFIG_OPENTHREAD_MESH_LOCAL_PREFIX="fd00:db8:a0:0::/64" +CONFIG_OPENTHREAD_NETWORK_CHANNEL=15 +CONFIG_OPENTHREAD_NETWORK_PANID=0x1234 +CONFIG_OPENTHREAD_NETWORK_EXTPANID="dead00beef00cafe" +CONFIG_OPENTHREAD_NETWORK_MASTERKEY="00112233445566778899aabbccddeeff" +CONFIG_OPENTHREAD_NETWORK_PSKC="104810e2315100afd6bc9215a6bfac53" +# end of Thread Operational Dataset +# end of OpenThread + +# +# Protocomm +# +CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_0=y +CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_1=y +CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_2=y +# end of Protocomm + +# +# PThreads +# +CONFIG_PTHREAD_TASK_PRIO_DEFAULT=5 +CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT=3072 +CONFIG_PTHREAD_STACK_MIN=768 +CONFIG_PTHREAD_DEFAULT_CORE_NO_AFFINITY=y +# CONFIG_PTHREAD_DEFAULT_CORE_0 is not set +# CONFIG_PTHREAD_DEFAULT_CORE_1 is not set +CONFIG_PTHREAD_TASK_CORE_DEFAULT=-1 +CONFIG_PTHREAD_TASK_NAME_DEFAULT="pthread" +# end of PThreads + +# +# MMU Config +# +CONFIG_MMU_PAGE_SIZE_64KB=y +CONFIG_MMU_PAGE_MODE="64KB" +CONFIG_MMU_PAGE_SIZE=0x10000 +# end of MMU Config + +# +# SPI Flash driver +# +# CONFIG_SPI_FLASH_VERIFY_WRITE is not set +# CONFIG_SPI_FLASH_ENABLE_COUNTERS is not set +CONFIG_SPI_FLASH_ROM_DRIVER_PATCH=y +CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS=y +# CONFIG_SPI_FLASH_DANGEROUS_WRITE_FAILS is not set +# CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED is not set +# CONFIG_SPI_FLASH_SHARE_SPI1_BUS is not set +# CONFIG_SPI_FLASH_BYPASS_BLOCK_ERASE is not set +CONFIG_SPI_FLASH_YIELD_DURING_ERASE=y +CONFIG_SPI_FLASH_ERASE_YIELD_DURATION_MS=20 +CONFIG_SPI_FLASH_ERASE_YIELD_TICKS=1 +CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE=8192 +# CONFIG_SPI_FLASH_SIZE_OVERRIDE is not set +# CONFIG_SPI_FLASH_CHECK_ERASE_TIMEOUT_DISABLED is not set +# CONFIG_SPI_FLASH_OVERRIDE_CHIP_DRIVER_LIST is not set + +# +# SPI Flash behavior when brownout +# +CONFIG_SPI_FLASH_BROWNOUT_RESET_XMC=y +CONFIG_SPI_FLASH_BROWNOUT_RESET=y +# end of SPI Flash behavior when brownout + +# +# Auto-detect flash chips +# +CONFIG_SPI_FLASH_VENDOR_XMC_SUPPORTED=y +CONFIG_SPI_FLASH_VENDOR_GD_SUPPORTED=y +CONFIG_SPI_FLASH_VENDOR_ISSI_SUPPORTED=y +CONFIG_SPI_FLASH_VENDOR_MXIC_SUPPORTED=y +CONFIG_SPI_FLASH_VENDOR_WINBOND_SUPPORTED=y +CONFIG_SPI_FLASH_SUPPORT_ISSI_CHIP=y +CONFIG_SPI_FLASH_SUPPORT_MXIC_CHIP=y +CONFIG_SPI_FLASH_SUPPORT_GD_CHIP=y +CONFIG_SPI_FLASH_SUPPORT_WINBOND_CHIP=y +# CONFIG_SPI_FLASH_SUPPORT_BOYA_CHIP is not set +# CONFIG_SPI_FLASH_SUPPORT_TH_CHIP is not set +# end of Auto-detect flash chips + +CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE=y +# end of SPI Flash driver + +# +# SPIFFS Configuration +# +CONFIG_SPIFFS_MAX_PARTITIONS=3 + +# +# SPIFFS Cache Configuration +# +CONFIG_SPIFFS_CACHE=y +CONFIG_SPIFFS_CACHE_WR=y +# CONFIG_SPIFFS_CACHE_STATS is not set +# end of SPIFFS Cache Configuration + +CONFIG_SPIFFS_PAGE_CHECK=y +CONFIG_SPIFFS_GC_MAX_RUNS=10 +# CONFIG_SPIFFS_GC_STATS is not set +CONFIG_SPIFFS_PAGE_SIZE=256 +CONFIG_SPIFFS_OBJ_NAME_LEN=32 +# CONFIG_SPIFFS_FOLLOW_SYMLINKS is not set +CONFIG_SPIFFS_USE_MAGIC=y +CONFIG_SPIFFS_USE_MAGIC_LENGTH=y +CONFIG_SPIFFS_META_LENGTH=4 +CONFIG_SPIFFS_USE_MTIME=y + +# +# Debug Configuration +# +# CONFIG_SPIFFS_DBG is not set +# CONFIG_SPIFFS_API_DBG is not set +# CONFIG_SPIFFS_GC_DBG is not set +# CONFIG_SPIFFS_CACHE_DBG is not set +# CONFIG_SPIFFS_CHECK_DBG is not set +# CONFIG_SPIFFS_TEST_VISUALISATION is not set +# end of Debug Configuration +# end of SPIFFS Configuration + +# +# TCP Transport +# + +# +# Websocket +# +CONFIG_WS_TRANSPORT=y +CONFIG_WS_BUFFER_SIZE=1024 +# CONFIG_WS_DYNAMIC_BUFFER is not set +# end of Websocket +# end of TCP Transport + +# +# Ultra Low Power (ULP) Co-processor +# +# CONFIG_ULP_COPROC_ENABLED is not set +# end of Ultra Low Power (ULP) Co-processor + +# +# Unity unit testing library +# +CONFIG_UNITY_ENABLE_FLOAT=y +CONFIG_UNITY_ENABLE_DOUBLE=y +# CONFIG_UNITY_ENABLE_64BIT is not set +# CONFIG_UNITY_ENABLE_COLOR is not set +CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=y +# CONFIG_UNITY_ENABLE_FIXTURE is not set +# CONFIG_UNITY_ENABLE_BACKTRACE_ON_FAIL is not set +# end of Unity unit testing library + +# +# Root Hub configuration +# +# end of Root Hub configuration + +# +# Virtual file system +# +CONFIG_VFS_SUPPORT_IO=y +CONFIG_VFS_SUPPORT_DIR=y +CONFIG_VFS_SUPPORT_SELECT=y +CONFIG_VFS_SUPPRESS_SELECT_DEBUG_OUTPUT=y +CONFIG_VFS_SUPPORT_TERMIOS=y +CONFIG_VFS_MAX_COUNT=8 + +# +# Host File System I/O (Semihosting) +# +CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS=1 +# end of Host File System I/O (Semihosting) +# end of Virtual file system + +# +# Wear Levelling +# +# CONFIG_WL_SECTOR_SIZE_512 is not set +CONFIG_WL_SECTOR_SIZE_4096=y +CONFIG_WL_SECTOR_SIZE=4096 +# end of Wear Levelling + +# +# Wi-Fi Provisioning Manager +# +CONFIG_WIFI_PROV_SCAN_MAX_ENTRIES=16 +CONFIG_WIFI_PROV_AUTOSTOP_TIMEOUT=30 +# CONFIG_WIFI_PROV_BLE_FORCE_ENCRYPTION is not set +CONFIG_WIFI_PROV_STA_ALL_CHANNEL_SCAN=y +# CONFIG_WIFI_PROV_STA_FAST_SCAN is not set +# end of Wi-Fi Provisioning Manager + +# +# libsodium +# +# end of libsodium +# end of Component config + +# CONFIG_IDF_EXPERIMENTAL_FEATURES is not set + +# Deprecated options for backward compatibility +# CONFIG_APP_BUILD_TYPE_ELF_RAM is not set +# CONFIG_NO_BLOBS is not set +# CONFIG_ESP32_NO_BLOBS is not set +# CONFIG_ESP32_COMPATIBLE_PRE_V2_1_BOOTLOADERS is not set +# CONFIG_ESP32_COMPATIBLE_PRE_V3_1_BOOTLOADERS is not set +# CONFIG_LOG_BOOTLOADER_LEVEL_NONE is not set +# CONFIG_LOG_BOOTLOADER_LEVEL_ERROR is not set +# CONFIG_LOG_BOOTLOADER_LEVEL_WARN is not set +CONFIG_LOG_BOOTLOADER_LEVEL_INFO=y +# CONFIG_LOG_BOOTLOADER_LEVEL_DEBUG is not set +# CONFIG_LOG_BOOTLOADER_LEVEL_VERBOSE is not set +CONFIG_LOG_BOOTLOADER_LEVEL=3 +# CONFIG_APP_ROLLBACK_ENABLE is not set +# CONFIG_FLASH_ENCRYPTION_ENABLED is not set +# CONFIG_FLASHMODE_QIO is not set +# CONFIG_FLASHMODE_QOUT is not set +CONFIG_FLASHMODE_DIO=y +# CONFIG_FLASHMODE_DOUT is not set +CONFIG_MONITOR_BAUD=115200 +CONFIG_OPTIMIZATION_LEVEL_DEBUG=y +CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG=y +CONFIG_COMPILER_OPTIMIZATION_DEFAULT=y +# CONFIG_OPTIMIZATION_LEVEL_RELEASE is not set +# CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE is not set +CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED=y +# CONFIG_OPTIMIZATION_ASSERTIONS_SILENT is not set +# CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED is not set +CONFIG_OPTIMIZATION_ASSERTION_LEVEL=2 +# CONFIG_CXX_EXCEPTIONS is not set +# CONFIG_STACK_CHECK_NONE is not set +# CONFIG_STACK_CHECK_NORM is not set +CONFIG_STACK_CHECK_STRONG=y +# CONFIG_STACK_CHECK_ALL is not set +CONFIG_STACK_CHECK=y +# CONFIG_WARN_WRITE_STRINGS is not set +# CONFIG_ESP32_APPTRACE_DEST_TRAX is not set +CONFIG_ESP32_APPTRACE_DEST_NONE=y +CONFIG_ESP32_APPTRACE_LOCK_ENABLE=y +CONFIG_ADC2_DISABLE_DAC=y +# CONFIG_MCPWM_ISR_IN_IRAM is not set +# CONFIG_EVENT_LOOP_PROFILING is not set +CONFIG_POST_EVENTS_FROM_ISR=y +CONFIG_POST_EVENTS_FROM_IRAM_ISR=y +# CONFIG_OTA_ALLOW_HTTP is not set +# CONFIG_TWO_UNIVERSAL_MAC_ADDRESS is not set +CONFIG_FOUR_UNIVERSAL_MAC_ADDRESS=y +CONFIG_NUMBER_OF_UNIVERSAL_MAC_ADDRESS=4 +CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY=2000 +CONFIG_ESP32_RTC_CLK_SRC_INT_RC=y +CONFIG_ESP32_RTC_CLOCK_SOURCE_INTERNAL_RC=y +# CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS is not set +# CONFIG_ESP32_RTC_CLOCK_SOURCE_EXTERNAL_CRYSTAL is not set +# CONFIG_ESP32_RTC_CLK_SRC_EXT_OSC is not set +# CONFIG_ESP32_RTC_CLOCK_SOURCE_EXTERNAL_OSC is not set +# CONFIG_ESP32_RTC_CLK_SRC_INT_8MD256 is not set +# CONFIG_ESP32_RTC_CLOCK_SOURCE_INTERNAL_8MD256 is not set +CONFIG_ESP32_RTC_CLK_CAL_CYCLES=1024 +# CONFIG_ESP32_XTAL_FREQ_26 is not set +CONFIG_ESP32_XTAL_FREQ_40=y +# CONFIG_ESP32_XTAL_FREQ_AUTO is not set +CONFIG_ESP32_XTAL_FREQ=40 +CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE=y +# CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION is not set +CONFIG_ESP32_PHY_MAX_WIFI_TX_POWER=20 +CONFIG_ESP32_PHY_MAX_TX_POWER=20 +# CONFIG_REDUCE_PHY_TX_POWER is not set +# CONFIG_ESP32_REDUCE_PHY_TX_POWER is not set +CONFIG_SPIRAM_SUPPORT=y +CONFIG_ESP32_SPIRAM_SUPPORT=y +# CONFIG_WIFI_LWIP_ALLOCATION_FROM_SPIRAM_FIRST is not set +# CONFIG_ESP32_DEFAULT_CPU_FREQ_80 is not set +CONFIG_ESP32_DEFAULT_CPU_FREQ_160=y +# CONFIG_ESP32_DEFAULT_CPU_FREQ_240 is not set +CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ=160 +CONFIG_TRACEMEM_RESERVE_DRAM=0x0 +# CONFIG_ESP32_PANIC_PRINT_HALT is not set +CONFIG_ESP32_PANIC_PRINT_REBOOT=y +# CONFIG_ESP32_PANIC_SILENT_REBOOT is not set +# CONFIG_ESP32_PANIC_GDBSTUB is not set +CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32 +CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=2304 +CONFIG_MAIN_TASK_STACK_SIZE=3584 +CONFIG_CONSOLE_UART_DEFAULT=y +# CONFIG_CONSOLE_UART_CUSTOM is not set +# CONFIG_CONSOLE_UART_NONE is not set +# CONFIG_ESP_CONSOLE_UART_NONE is not set +CONFIG_CONSOLE_UART=y +CONFIG_CONSOLE_UART_NUM=0 +CONFIG_CONSOLE_UART_BAUDRATE=115200 +CONFIG_INT_WDT=y +CONFIG_INT_WDT_TIMEOUT_MS=300 +CONFIG_INT_WDT_CHECK_CPU1=y +CONFIG_TASK_WDT=y +CONFIG_ESP_TASK_WDT=y +# CONFIG_TASK_WDT_PANIC is not set +CONFIG_TASK_WDT_TIMEOUT_S=5 +CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0=y +CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU1=y +# CONFIG_ESP32_DEBUG_STUBS_ENABLE is not set +CONFIG_ESP32_DEBUG_OCDAWARE=y +CONFIG_BROWNOUT_DET=y +CONFIG_ESP32_BROWNOUT_DET=y +CONFIG_BROWNOUT_DET_LVL_SEL_0=y +CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_0=y +# CONFIG_BROWNOUT_DET_LVL_SEL_1 is not set +# CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_1 is not set +# CONFIG_BROWNOUT_DET_LVL_SEL_2 is not set +# CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_2 is not set +# CONFIG_BROWNOUT_DET_LVL_SEL_3 is not set +# CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_3 is not set +# CONFIG_BROWNOUT_DET_LVL_SEL_4 is not set +# CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_4 is not set +# CONFIG_BROWNOUT_DET_LVL_SEL_5 is not set +# CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_5 is not set +# CONFIG_BROWNOUT_DET_LVL_SEL_6 is not set +# CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_6 is not set +# CONFIG_BROWNOUT_DET_LVL_SEL_7 is not set +# CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_7 is not set +CONFIG_BROWNOUT_DET_LVL=0 +CONFIG_ESP32_BROWNOUT_DET_LVL=0 +# CONFIG_DISABLE_BASIC_ROM_CONSOLE is not set +CONFIG_IPC_TASK_STACK_SIZE=1024 +CONFIG_TIMER_TASK_STACK_SIZE=3584 +CONFIG_ESP32_WIFI_ENABLED=y +CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM=10 +CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM=32 +CONFIG_ESP32_WIFI_STATIC_TX_BUFFER=y +CONFIG_ESP32_WIFI_TX_BUFFER_TYPE=0 +CONFIG_ESP32_WIFI_STATIC_TX_BUFFER_NUM=16 +CONFIG_ESP32_WIFI_CACHE_TX_BUFFER_NUM=32 +# CONFIG_ESP32_WIFI_CSI_ENABLED is not set +CONFIG_ESP32_WIFI_AMPDU_TX_ENABLED=y +CONFIG_ESP32_WIFI_TX_BA_WIN=6 +CONFIG_ESP32_WIFI_AMPDU_RX_ENABLED=y +CONFIG_ESP32_WIFI_AMPDU_RX_ENABLED=y +CONFIG_ESP32_WIFI_RX_BA_WIN=6 +CONFIG_ESP32_WIFI_RX_BA_WIN=6 +# CONFIG_ESP32_WIFI_AMSDU_TX_ENABLED is not set +CONFIG_ESP32_WIFI_NVS_ENABLED=y +CONFIG_ESP32_WIFI_TASK_PINNED_TO_CORE_0=y +# CONFIG_ESP32_WIFI_TASK_PINNED_TO_CORE_1 is not set +CONFIG_ESP32_WIFI_SOFTAP_BEACON_MAX_LEN=752 +CONFIG_ESP32_WIFI_MGMT_SBUF_NUM=32 +CONFIG_ESP32_WIFI_IRAM_OPT=y +CONFIG_ESP32_WIFI_RX_IRAM_OPT=y +CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE=y +CONFIG_ESP32_WIFI_ENABLE_WPA3_OWE_STA=y +CONFIG_WPA_MBEDTLS_CRYPTO=y +CONFIG_WPA_MBEDTLS_TLS_CLIENT=y +# CONFIG_WPA_WAPI_PSK is not set +# CONFIG_WPA_SUITE_B_192 is not set +# CONFIG_WPA_11KV_SUPPORT is not set +# CONFIG_WPA_MBO_SUPPORT is not set +# CONFIG_WPA_DPP_SUPPORT is not set +# CONFIG_WPA_11R_SUPPORT is not set +# CONFIG_WPA_WPS_SOFTAP_REGISTRAR is not set +# CONFIG_WPA_WPS_STRICT is not set +# CONFIG_WPA_DEBUG_PRINT is not set +# CONFIG_WPA_TESTING_OPTIONS is not set +# CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH is not set +# CONFIG_ESP32_ENABLE_COREDUMP_TO_UART is not set +CONFIG_ESP32_ENABLE_COREDUMP_TO_NONE=y +CONFIG_TIMER_TASK_PRIORITY=1 +CONFIG_TIMER_TASK_STACK_DEPTH=2048 +CONFIG_TIMER_QUEUE_LENGTH=10 +# CONFIG_ENABLE_STATIC_TASK_CLEAN_UP_HOOK is not set +# CONFIG_HAL_ASSERTION_SILIENT is not set +# CONFIG_L2_TO_L3_COPY is not set +CONFIG_ESP_GRATUITOUS_ARP=y +CONFIG_GARP_TMR_INTERVAL=60 +CONFIG_TCPIP_RECVMBOX_SIZE=32 +CONFIG_TCP_MAXRTX=12 +CONFIG_TCP_SYNMAXRTX=12 +CONFIG_TCP_MSS=1440 +CONFIG_TCP_MSL=60000 +CONFIG_TCP_SND_BUF_DEFAULT=5744 +CONFIG_TCP_WND_DEFAULT=5744 +CONFIG_TCP_RECVMBOX_SIZE=6 +CONFIG_TCP_QUEUE_OOSEQ=y +CONFIG_TCP_OVERSIZE_MSS=y +# CONFIG_TCP_OVERSIZE_QUARTER_MSS is not set +# CONFIG_TCP_OVERSIZE_DISABLE is not set +CONFIG_UDP_RECVMBOX_SIZE=6 +CONFIG_TCPIP_TASK_STACK_SIZE=3072 +CONFIG_TCPIP_TASK_AFFINITY_NO_AFFINITY=y +# CONFIG_TCPIP_TASK_AFFINITY_CPU0 is not set +# CONFIG_TCPIP_TASK_AFFINITY_CPU1 is not set +CONFIG_TCPIP_TASK_AFFINITY=0x7FFFFFFF +# CONFIG_PPP_SUPPORT is not set +CONFIG_ESP32_TIME_SYSCALL_USE_RTC_HRT=y +CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1=y +# CONFIG_ESP32_TIME_SYSCALL_USE_RTC is not set +# CONFIG_ESP32_TIME_SYSCALL_USE_HRT is not set +# CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 is not set +# CONFIG_ESP32_TIME_SYSCALL_USE_NONE is not set +CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT=5 +CONFIG_ESP32_PTHREAD_TASK_STACK_SIZE_DEFAULT=3072 +CONFIG_ESP32_PTHREAD_STACK_MIN=768 +CONFIG_ESP32_DEFAULT_PTHREAD_CORE_NO_AFFINITY=y +# CONFIG_ESP32_DEFAULT_PTHREAD_CORE_0 is not set +# CONFIG_ESP32_DEFAULT_PTHREAD_CORE_1 is not set +CONFIG_ESP32_PTHREAD_TASK_CORE_DEFAULT=-1 +CONFIG_ESP32_PTHREAD_TASK_NAME_DEFAULT="pthread" +CONFIG_SPI_FLASH_WRITING_DANGEROUS_REGIONS_ABORTS=y +# CONFIG_SPI_FLASH_WRITING_DANGEROUS_REGIONS_FAILS is not set +# CONFIG_SPI_FLASH_WRITING_DANGEROUS_REGIONS_ALLOWED is not set +# CONFIG_ESP32_ULP_COPROC_ENABLED is not set +CONFIG_SUPPRESS_SELECT_DEBUG_OUTPUT=y +CONFIG_SUPPORT_TERMIOS=y +CONFIG_SEMIHOSTFS_MAX_MOUNT_POINTS=1 +# End of deprecated options diff --git a/other/docker/esp32/toxcore/CMakeLists.txt b/other/docker/esp32/toxcore/CMakeLists.txt deleted file mode 100644 index 0b844e5e..00000000 --- a/other/docker/esp32/toxcore/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-missing-field-initializers") - -file(GLOB toxcore_SRCS "toxcore/*.[ch]" "toxcore/*/*.[ch]") -set(COMPONENT_SRCS - ${toxcore_SRCS} - other/docker/esp32/main/toxcore_main.cc - third_party/cmp/cmp.c - third_party/cmp/cmp.h - toxencryptsave/defines.h) -set(COMPONENT_ADD_INCLUDEDIRS "") - -register_component() diff --git a/other/docker/esp32/toxcore/toxcore_main.cc b/other/docker/esp32/toxcore/toxcore_main.cc deleted file mode 100644 index 0ddec81a..00000000 --- a/other/docker/esp32/toxcore/toxcore_main.cc +++ /dev/null @@ -1,14 +0,0 @@ -#include - -#include "../../../../toxcore/ccompat.h" -#include "../../../../toxcore/tox.h" -#include "../../../../toxcore/tox_events.h" - -extern "C" void app_main(void) -{ - printf("Hello Tox!\n"); - - Tox *tox = tox_new(nullptr, nullptr); - tox_events_free(tox_events_iterate(tox, true, nullptr)); - tox_kill(tox); -} diff --git a/other/docker/tcc/Dockerfile b/other/docker/tcc/Dockerfile index 65ab3e76..d1f10181 100644 --- a/other/docker/tcc/Dockerfile +++ b/other/docker/tcc/Dockerfile @@ -39,7 +39,7 @@ RUN tcc \ COPY other/make_single_file /work/other/ RUN \ - other/make_single_file \ + other/make_single_file -core \ auto_tests/auto_test_support.c \ auto_tests/send_message_test.c \ testing/misc_tools.c | \ @@ -47,5 +47,17 @@ RUN \ -o send_message_test \ -Wall -Werror \ -bench -g \ - $(pkg-config --cflags --libs libsodium opus vpx) \ + $(pkg-config --cflags --libs libsodium) \ && ./send_message_test | grep 'tox clients connected' + +RUN \ + other/make_single_file \ + auto_tests/auto_test_support.c \ + auto_tests/toxav_basic_test.c \ + testing/misc_tools.c | \ + tcc - \ + -o toxav_basic_test \ + -Wall -Werror \ + -bench -g \ + $(pkg-config --cflags --libs libsodium opus vpx) \ + && ./toxav_basic_test | grep 'Test successful' diff --git a/other/event_tooling/Dockerfile b/other/event_tooling/Dockerfile new file mode 100644 index 00000000..b94e6f0e --- /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 92b01060..a3dd991e 100644 --- a/other/event_tooling/generate_event_c.cpp +++ b/other/event_tooling/generate_event_c.cpp @@ -80,27 +80,27 @@ std::string bin_unpack_name_from_type(const std::string& type) { } else if (type == "bool") { return "bin_unpack_bool"; } else if (type == "Tox_User_Status") { - return "tox_unpack_user_status"; + return "tox_user_status_unpack"; } else if (type == "Tox_Conference_Type") { - return "tox_unpack_conference_type"; + return "tox_conference_type_unpack"; } else if (type == "Tox_Message_Type") { - return "tox_unpack_message_type"; + return "tox_message_type_unpack"; } else if (type == "Tox_File_Control") { - return "tox_unpack_file_control"; + return "tox_file_control_unpack"; } else if (type == "Tox_Connection") { - return "tox_unpack_connection"; + return "tox_connection_unpack"; } else if (type == "Tox_Group_Privacy_State") { - return "tox_unpack_group_privacy_state"; + return "tox_group_privacy_state_unpack"; } else if (type == "Tox_Group_Voice_State") { - return "tox_unpack_group_voice_state"; + return "tox_group_voice_state_unpack"; } else if (type == "Tox_Group_Topic_Lock") { - return "tox_unpack_group_topic_lock"; + return "tox_group_topic_lock_unpack"; } else if (type == "Tox_Group_Join_Fail") { - return "tox_unpack_group_join_fail"; + return "tox_group_join_fail_unpack"; } else if (type == "Tox_Group_Mod_Event") { - return "tox_unpack_group_mod_event"; + return "tox_group_mod_event_unpack"; } else if (type == "Tox_Group_Exit_Type") { - return "tox_unpack_group_exit_type"; + return "tox_group_exit_type_unpack"; } else { std::cerr << "unknown type " << type << "\n"; exit(1); @@ -109,7 +109,7 @@ std::string bin_unpack_name_from_type(const std::string& type) { } } -void generate_event_impl(const std::string& event_name, std::vector event_types) { +void generate_event_impl(const std::string& event_name, const std::vector& event_types) { const std::string event_name_l = str_tolower(event_name); std::string file_name = output_folder + "/" + event_name_l + ".c"; @@ -164,35 +164,6 @@ void generate_event_impl(const std::string& event_name, std::vector e } f << "};\n\n"; - // gen contruct - f << "non_null()\n"; - f << "static void tox_event_" << event_name_l << "_construct(Tox_Event_" << event_name << " *" << event_name_l << ")\n{\n"; - // TODO: initialize all members properly - // TODO: check if _NONE is universal - // str_toupper( - f << " *" << event_name_l << " = (Tox_Event_" << event_name << ") {\n 0\n };\n}\n"; - - // gen destruct - f << "non_null()\n"; - f << "static void tox_event_" << event_name_l << "_destruct(Tox_Event_" << event_name << " *" << event_name_l << ")\n{\n"; - size_t data_count = 0; - for (const auto& t : event_types) { - std::visit( - overloaded{ - [&](const EventTypeTrivial&) {}, - [&](const EventTypeByteRange& t) { - f << " free(" << event_name_l << "->" << t.name_data << ");\n"; - data_count++; - } - }, - t - ); - } - if (data_count == 0) { - f << " return;\n"; - } - f << "}\n\n"; - // gen setters and getters for (const auto& t : event_types) { // setter @@ -258,7 +229,7 @@ void generate_event_impl(const std::string& event_name, std::vector e }, [&](const EventTypeByteRange& t) { //f << "non_null()\n"; // TODO: is this missing in the original? - f << "size_t tox_event_" << event_name_l << "_get_" << t.name_length; + f << "uint32_t tox_event_" << event_name_l << "_get_" << t.name_length; f << "(const Tox_Event_" << event_name << " *" << event_name_l << ")\n"; f << "{\n assert(" << event_name_l << " != nullptr);\n"; f << " return " << event_name_l << "->" << t.name_length << ";\n}\n"; @@ -272,15 +243,45 @@ void generate_event_impl(const std::string& event_name, std::vector e ); } - // pack + + // gen contruct f << "non_null()\n"; - f << "static bool tox_event_" << event_name_l << "_pack(\n"; + f << "static void tox_event_" << event_name_l << "_construct(Tox_Event_" << event_name << " *" << event_name_l << ")\n{\n"; + // TODO: initialize all members properly + // TODO: check if _NONE is universal + // str_toupper( + f << " *" << event_name_l << " = (Tox_Event_" << event_name << ") {\n 0\n };\n}\n"; + + // gen destruct + f << "non_null()\n"; + f << "static void tox_event_" << event_name_l << "_destruct(Tox_Event_" << event_name << " *" << event_name_l << ", const Memory *mem)\n{\n"; + size_t data_count = 0; + for (const auto& t : event_types) { + std::visit( + overloaded{ + [&](const EventTypeTrivial&) {}, + [&](const EventTypeByteRange& t) { + f << " free(" << event_name_l << "->" << t.name_data << ");\n"; + //f << " mem->funcs->free(mem->obj, " << event_name_l << "->" << t.name_data << ");\n"; + data_count++; + } + }, + t + ); + } + if (data_count == 0) { + f << " return;\n"; + } + f << "}\n\n"; + + // pack + f << "bool tox_event_" << event_name_l << "_pack(\n"; f << " const Tox_Event_" << event_name << " *event, Bin_Pack *bp)\n{\n"; f << " assert(event != nullptr);\n"; f << " return bin_pack_array(bp, 2)\n"; - f << " && bin_pack_u32(bp, TOX_EVENT_" << str_toupper(event_name) << ")\n"; + f << " && bin_pack_u32(bp, TOX_EVENT_" << str_toupper(event_name) << ")"; if (event_types.size() > 1) { - f << " && bin_pack_array(bp, " << event_types.size() << ")"; + f << "\n && bin_pack_array(bp, " << event_types.size() << ")"; } for (const auto& t : event_types) { @@ -302,7 +303,7 @@ void generate_event_impl(const std::string& event_name, std::vector e // unpack f << "non_null()\n"; - f << "static bool tox_event_" << event_name_l << "_unpack(\n"; + f << "static bool tox_event_" << event_name_l << "_unpack_into(\n"; f << " Tox_Event_" << event_name << " *event, Bin_Unpack *bu)\n{\n"; f << " assert(event != nullptr);\n"; if (event_types.size() > 1) { @@ -335,71 +336,82 @@ void generate_event_impl(const std::string& event_name, std::vector e f << R"( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - )"; + f << "const Tox_Event_" << event_name << " *tox_event_get_" << event_name_l << "(const Tox_Event *event)\n{\n"; + f << " return event->type == TOX_EVENT_" << str_toupper(event_name) << " ? event->data." << event_name_l << " : nullptr;\n}\n\n"; + + // new + f << "Tox_Event_" << event_name << " *tox_event_" << event_name_l << "_new(const Memory *mem)\n{\n"; + f << " Tox_Event_" << event_name << " *const " << event_name_l << " =\n"; + f << " (Tox_Event_" << event_name << " *)mem_alloc(mem, sizeof(Tox_Event_" << event_name << "));\n\n"; + f << " if (" << event_name_l << " == nullptr) {\n return nullptr;\n }\n\n"; + f << " tox_event_" << event_name_l << "_construct(" << event_name_l << ");\n"; + f << " return " << event_name_l << ";\n}\n\n"; + + // free + f << "void tox_event_" << event_name_l << "_free(Tox_Event_" << event_name << " *" << event_name_l << ", const Memory *mem)\n{\n"; + f << " if (" << event_name_l << " != nullptr) {\n"; + f << " tox_event_" << event_name_l << "_destruct(" << event_name_l << ", mem);\n }\n"; + f << " mem_delete(mem, " << event_name_l << ");\n}\n\n"; + // add f << "non_null()\n"; - f << "static Tox_Event_" << event_name << " *tox_events_add_" << event_name_l << "(Tox_Events *events)\n{\n"; - f << " if (events->" << event_name_l << "_size == UINT32_MAX) {\n"; + f << "static Tox_Event_" << event_name << " *tox_events_add_" << event_name_l << "(Tox_Events *events, const Memory *mem)\n{\n"; + f << " Tox_Event_" << event_name << " *const " << event_name_l << " = tox_event_" << event_name_l << "_new(mem);\n\n"; + f << " if (" << event_name_l << " == nullptr) {\n"; f << " return nullptr;\n }\n\n"; - f << " if (events->" << event_name_l << "_size == events->" << event_name_l << "_capacity) {\n"; - f << " const uint32_t new_" << event_name_l << "_capacity = events->" << event_name_l << "_capacity * 2 + 1;\n"; - - f << " Tox_Event_" << event_name << " *new_" << event_name_l << " = (Tox_Event_" << event_name << " *)\n"; - f << " realloc(\n"; - - f << " events->" << event_name_l << ",\n"; - f << " new_" << event_name_l << "_capacity * sizeof(Tox_Event_" << event_name << "));\n\n"; - f << " if (new_" << event_name_l << " == nullptr) {\n return nullptr;\n }\n\n"; - f << " events->" << event_name_l << " = new_" << event_name_l << ";\n"; - f << " events->" << event_name_l << "_capacity = new_" << event_name_l << "_capacity;\n"; - f << " }\n\n"; - f << " Tox_Event_" << event_name << " *const " << event_name_l << " =\n"; - f << " &events->" << event_name_l << "[events->" << event_name_l << "_size];\n"; - f << " tox_event_" << event_name_l << "_construct(" << event_name_l << ");\n"; - f << " ++events->" << event_name_l << "_size;\n"; + f << " Tox_Event event;\n"; + f << " event.type = TOX_EVENT_" << str_toupper(event_name) << ";\n"; + f << " event.data." << event_name_l << " = " << event_name_l << ";\n\n"; + f << " tox_events_add(events, &event);\n"; f << " return " << event_name_l << ";\n}\n\n"; - // clear - f << "void tox_events_clear_" << event_name_l << "(Tox_Events *events)\n{\n"; - f << " if (events == nullptr) {\n return;\n }\n\n"; - f << " for (uint32_t i = 0; i < events->" << event_name_l << "_size; ++i) {\n"; - f << " tox_event_" << event_name_l << "_destruct(&events->" << event_name_l << "[i]);\n }\n\n"; - f << " free(events->" << event_name_l << ");\n"; - f << " events->" << event_name_l << " = nullptr;\n"; - f << " events->" << event_name_l << "_size = 0;\n"; - f << " events->" << event_name_l << "_capacity = 0;\n"; - f << "}\n\n"; - - // get size - f << "uint32_t tox_events_get_" << event_name_l << "_size(const Tox_Events *events)\n{\n"; - f << " if (events == nullptr) {\n return 0;\n }\n\n"; - f << " return events->" << event_name_l << "_size;\n}\n\n"; - // get f << "const Tox_Event_" << event_name << " *tox_events_get_" << event_name_l << "(const Tox_Events *events, uint32_t index)\n{\n"; - f << " assert(index < events->" << event_name_l << "_size);\n"; - f << " assert(events->" << event_name_l << " != nullptr);\n"; - f << " return &events->" << event_name_l << "[index];\n}\n\n"; - - // aux pack - f << "bool tox_events_pack_" << event_name_l << "(const Tox_Events *events, Bin_Pack *bp)\n{\n"; - f << " const uint32_t size = tox_events_get_" << event_name_l << "_size(events);\n\n"; + f << " uint32_t " << event_name_l << "_index = 0;\n"; + f << " const uint32_t size = tox_events_get_size(events);\n\n"; f << " for (uint32_t i = 0; i < size; ++i) {\n"; - f << " if (!tox_event_" << event_name_l << "_pack(tox_events_get_" << event_name_l << "(events, i), bp)) {\n"; - f << " return false;\n }\n }\n"; - f << " return true;\n}\n\n"; + f << " if (" << event_name_l << "_index > index) {\n"; + f << " return nullptr;\n }\n\n"; + f << " if (events->events[i].type == TOX_EVENT_" << str_toupper(event_name) << ") {\n"; + f << " const Tox_Event_" << event_name << " *" << event_name_l << " = events->events[i].data." << event_name_l << ";\n"; + f << " if (" << event_name_l << "_index == index) {\n"; + f << " return " << event_name_l << ";\n }\n"; + f << " ++" << event_name_l << "_index;\n }\n }\n\n return nullptr;\n}\n\n"; + + // get size + f << "uint32_t tox_events_get_" << event_name_l << "_size(const Tox_Events *events)\n{\n"; + f << " uint32_t " << event_name_l << "_size = 0;\n"; + f << " const uint32_t size = tox_events_get_size(events);\n\n"; + f << " for (uint32_t i = 0; i < size; ++i) {\n"; + f << " if (events->events[i].type == TOX_EVENT_" << str_toupper(event_name) << ") {\n"; + f << " ++" << event_name_l << "_size;\n }\n }\n\n"; + f << " return " << event_name_l << "_size;\n}\n\n"; + + // unpack + f << "bool tox_event_" << event_name_l << "_unpack(\n"; + f << " Tox_Event_" << event_name << " **event, Bin_Unpack *bu, const Memory *mem)\n{\n"; + f << " assert(event != nullptr);\n"; + f << " *event = tox_event_" << event_name_l << "_new(mem);\n\n"; + f << " if (*event == nullptr) {\n return false;\n }\n\n"; + f << " return tox_event_" << event_name_l << "_unpack_into(*event, bu);\n}\n\n"; + + // alloc + f << "non_null()\n"; + f << "static Tox_Event_" << event_name << " *tox_event_" << event_name_l << "_alloc(void *user_data)\n{\n"; + f << " Tox_Events_State *state = tox_events_alloc(user_data);\n"; + f << " assert(state != nullptr);\n\n"; + f << " if (state->events == nullptr) {\n return nullptr;\n }\n\n"; + f << " Tox_Event_" << event_name << " *" << event_name_l << " = tox_events_add_" << event_name_l << "(state->events, state->mem);\n\n"; + f << " if (" << event_name_l << " == nullptr) {\n"; + f << " state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;\n return nullptr;\n }\n\n"; + f << " return " << event_name_l << ";\n}\n\n"; - // aux unpack - f << "bool tox_events_unpack_" << event_name_l << "(Tox_Events *events, Bin_Unpack *bu)\n{\n"; - f << " Tox_Event_" << event_name << " *event = tox_events_add_" << event_name_l << "(events);\n\n"; - f << " if (event == nullptr) {\n return false;\n }\n\n"; - f << " return tox_event_" << event_name_l << "_unpack(event, bu);\n}\n\n"; f << R"( /***************************************************** @@ -427,12 +439,8 @@ void generate_event_impl(const std::string& event_name, std::vector e } f << ",\n void *user_data)\n{\n"; - f << " Tox_Events_State *state = tox_events_alloc(user_data);\n"; - f << " assert(state != nullptr);\n\n"; - f << " if (state->events == nullptr) {\n return;\n }\n\n"; - f << " Tox_Event_" << event_name << " *" << event_name_l << " = tox_events_add_" << event_name_l << "(state->events);\n\n"; - f << " if (" << event_name_l << " == nullptr) {\n"; - f << " state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;\n return;\n }\n\n"; + f << " Tox_Event_" << event_name << " *" << event_name_l << " = tox_event_" << event_name_l << "_alloc(user_data);\n\n"; + f << " if (" << event_name_l << " == nullptr) {\n return;\n }\n\n"; for (const auto& t : event_types) { std::visit( @@ -465,7 +473,7 @@ int main(int argc, char** argv) { { EventTypeTrivial{"uint32_t", "friend_number"}, EventTypeTrivial{"Tox_Conference_Type", "type"}, - EventTypeByteRange{"cookie", "cookie_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"cookie", "cookie_length", "length"}, // the latter two are ideally the same } }, { @@ -474,7 +482,7 @@ int main(int argc, char** argv) { EventTypeTrivial{"uint32_t", "conference_number"}, EventTypeTrivial{"uint32_t", "peer_number"}, EventTypeTrivial{"Tox_Message_Type", "type"}, - EventTypeByteRange{"message", "message_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same } }, { @@ -488,7 +496,7 @@ int main(int argc, char** argv) { { EventTypeTrivial{"uint32_t", "conference_number"}, EventTypeTrivial{"uint32_t", "peer_number"}, - EventTypeByteRange{"name", "name_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"name", "name_length", "length"}, // the latter two are ideally the same } }, { @@ -496,7 +504,7 @@ int main(int argc, char** argv) { { EventTypeTrivial{"uint32_t", "conference_number"}, EventTypeTrivial{"uint32_t", "peer_number"}, - EventTypeByteRange{"title", "title_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"title", "title_length", "length"}, // the latter two are ideally the same } }, @@ -516,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 later two are idealy the same + EventTypeByteRange{"filename", "filename_length", "filename_length"}, } }, { @@ -525,7 +533,7 @@ int main(int argc, char** argv) { EventTypeTrivial{"uint32_t", "friend_number"}, EventTypeTrivial{"uint32_t", "file_number"}, EventTypeTrivial{"uint64_t", "position"}, - EventTypeByteRange{"data", "data_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"data", "data_length", "length"}, // the latter two are ideally the same } }, { @@ -548,14 +556,14 @@ int main(int argc, char** argv) { "Friend_Lossless_Packet", { EventTypeTrivial{"uint32_t", "friend_number"}, - EventTypeByteRange{"data", "data_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"data", "data_length", "length"}, // the latter two are ideally the same } }, { "Friend_Lossy_Packet", { EventTypeTrivial{"uint32_t", "friend_number"}, - EventTypeByteRange{"data", "data_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"data", "data_length", "length"}, // the latter two are ideally the same } }, { @@ -563,14 +571,14 @@ int main(int argc, char** argv) { { EventTypeTrivial{"uint32_t", "friend_number"}, EventTypeTrivial{"Tox_Message_Type", "type"}, - EventTypeByteRange{"message", "message_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same } }, { "Friend_Name", { EventTypeTrivial{"uint32_t", "friend_number"}, - EventTypeByteRange{"name", "name_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"name", "name_length", "length"}, // the latter two are ideally the same } }, { @@ -585,7 +593,7 @@ int main(int argc, char** argv) { "Friend_Request", { //EventTypeTrivial{"uint32_t", "friend_number"}, // public_key ByteArray - EventTypeByteRange{"message", "message_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same } }, #endif @@ -600,7 +608,7 @@ int main(int argc, char** argv) { "Friend_Status_Message", { EventTypeTrivial{"uint32_t", "friend_number"}, - EventTypeByteRange{"message", "message_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same } }, { @@ -623,7 +631,7 @@ int main(int argc, char** argv) { { EventTypeTrivial{"uint32_t", "group_number"}, EventTypeTrivial{"uint32_t", "peer_id"}, - EventTypeByteRange{"name", "name_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"name", "name_length", "length"}, // the latter two are ideally the same } }, { @@ -639,7 +647,7 @@ int main(int argc, char** argv) { { EventTypeTrivial{"uint32_t", "group_number"}, EventTypeTrivial{"uint32_t", "peer_id"}, - EventTypeByteRange{"topic", "topic_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"topic", "topic_length", "length"}, // the latter two are ideally the same } }, { @@ -674,7 +682,7 @@ int main(int argc, char** argv) { "Group_Password", { EventTypeTrivial{"uint32_t", "group_number"}, - EventTypeByteRange{"password", "password_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"password", "password_length", "length"}, // the latter two are ideally the same } }, { @@ -683,7 +691,7 @@ int main(int argc, char** argv) { EventTypeTrivial{"uint32_t", "group_number"}, EventTypeTrivial{"uint32_t", "peer_id"}, EventTypeTrivial{"Tox_Message_Type", "type"}, - EventTypeByteRange{"message", "message_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same EventTypeTrivial{"uint32_t", "message_id"}, } }, @@ -693,7 +701,7 @@ int main(int argc, char** argv) { EventTypeTrivial{"uint32_t", "group_number"}, EventTypeTrivial{"uint32_t", "peer_id"}, EventTypeTrivial{"Tox_Message_Type", "type"}, - EventTypeByteRange{"message", "message_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"message", "message_length", "length"}, // the latter two are ideally the same } }, { @@ -701,7 +709,7 @@ int main(int argc, char** argv) { { EventTypeTrivial{"uint32_t", "group_number"}, EventTypeTrivial{"uint32_t", "peer_id"}, - EventTypeByteRange{"data", "data_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"data", "data_length", "length"}, // the latter two are ideally the same } }, { @@ -709,14 +717,14 @@ int main(int argc, char** argv) { { EventTypeTrivial{"uint32_t", "group_number"}, EventTypeTrivial{"uint32_t", "peer_id"}, - EventTypeByteRange{"data", "data_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"data", "data_length", "length"}, // the latter two are ideally the same } }, { "Group_Invite", { EventTypeTrivial{"uint32_t", "friend_number"}, - EventTypeByteRange{"invite_data", "invite_data_length", "length"}, // the later two are idealy the same + EventTypeByteRange{"invite_data", "invite_data_length", "length"}, // the latter two are ideally the same EventTypeByteRange{"group_name", "group_name_length", "group_name_length"}, // they are :) } }, diff --git a/other/event_tooling/run b/other/event_tooling/run new file mode 100755 index 00000000..95e23646 --- /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/other/fun/CMakeLists.txt b/other/fun/CMakeLists.txt index 52129409..fb700378 100644 --- a/other/fun/CMakeLists.txt +++ b/other/fun/CMakeLists.txt @@ -1,30 +1,41 @@ +function(target_link_toxcore target) + if(TARGET toxcore_static) + target_link_libraries(${target} PRIVATE toxcore_static) + else() + target_link_libraries(${target} PRIVATE toxcore_shared) + endif() +endfunction() + add_executable(save-generator save-generator.c) -target_link_modules(save-generator toxcore misc_tools) +target_link_libraries(save-generator PRIVATE misc_tools) +target_link_toxcore(save-generator) add_executable(strkey strkey.c) -target_link_modules(strkey toxcore ${LIBSODIUM_LIBRARIES}) +target_link_libraries(strkey PRIVATE ${LIBSODIUM_LIBRARIES}) +target_link_toxcore(strkey) add_executable(create_bootstrap_keys create_bootstrap_keys.c) -target_link_modules(create_bootstrap_keys ${LIBSODIUM_LIBRARIES}) +target_link_libraries(create_bootstrap_keys PRIVATE ${LIBSODIUM_LIBRARIES}) +target_link_toxcore(create_bootstrap_keys) add_executable(create_minimal_savedata create_minimal_savedata.c) -target_link_modules(create_minimal_savedata ${LIBSODIUM_LIBRARIES}) +target_link_libraries(create_minimal_savedata PRIVATE ${LIBSODIUM_LIBRARIES}) add_executable(create_savedata create_savedata.c) -target_link_modules(create_savedata toxcore ${LIBSODIUM_LIBRARIES}) +target_link_libraries(create_savedata PRIVATE ${LIBSODIUM_LIBRARIES}) +target_link_toxcore(create_savedata) add_executable(sign sign.c) -target_link_modules(sign ${LIBSODIUM_LIBRARIES} misc_tools) +target_link_libraries(sign PRIVATE ${LIBSODIUM_LIBRARIES} misc_tools) add_executable(cracker_simple cracker_simple.c) -target_link_modules(cracker_simple ${LIBSODIUM_LIBRARIES} misc_tools) +target_link_libraries(cracker_simple ${LIBSODIUM_LIBRARIES} misc_tools) # MSVC doesn't support OpenMP if(NOT MSVC) find_package(OpenMP) if(OpenMP_C_FOUND) add_executable(cracker cracker.c) - target_link_modules(cracker ${LIBSODIUM_LIBRARIES}) - target_link_libraries(cracker OpenMP::OpenMP_C) + target_link_libraries(cracker PRIVATE OpenMP::OpenMP_C ${LIBSODIUM_LIBRARIES}) endif() endif() diff --git a/other/fun/create_savedata.c b/other/fun/create_savedata.c index 8a30525c..84569402 100644 --- a/other/fun/create_savedata.c +++ b/other/fun/create_savedata.c @@ -41,7 +41,7 @@ static bool create_tox(const unsigned char *const secret_key, Tox **const tox) return true; } -static bool print_savedata(Tox *const tox) +static bool print_savedata(const Tox *const tox) { const size_t savedata_size = tox_get_savedata_size(tox); uint8_t *const savedata = (uint8_t *)malloc(savedata_size); diff --git a/other/fun/save-generator.c b/other/fun/save-generator.c index 6f65a959..9eb55689 100644 --- a/other/fun/save-generator.c +++ b/other/fun/save-generator.c @@ -61,7 +61,7 @@ static void tox_connection_callback(Tox *tox, Tox_Connection connection, void *u } } -static void print_information(Tox *tox) +static void print_information(const Tox *tox) { uint8_t tox_id[TOX_ADDRESS_SIZE]; char tox_id_str[TOX_ADDRESS_SIZE * 2]; diff --git a/other/fun/sign.c b/other/fun/sign.c index f6dcc71e..a75fa0d4 100644 --- a/other/fun/sign.c +++ b/other/fun/sign.c @@ -21,7 +21,7 @@ #include "../../testing/misc_tools.h" // hex_string_to_bin #include "../../toxcore/ccompat.h" -static int load_file(char *filename, unsigned char **result) +static int load_file(const char *filename, unsigned char **result) { int size = 0; FILE *f = fopen(filename, "rb"); diff --git a/other/fun/strkey.c b/other/fun/strkey.c index 0b0aa5c2..538f59ad 100644 --- a/other/fun/strkey.c +++ b/other/fun/strkey.c @@ -2,7 +2,7 @@ * * Generates Tox's key pairs, checking if a certain string is in the public key. * - * Requires sodium or nacl library. + * Requires sodium library. * * There seem to be some problems with the code working on Windows -- it works * when built in debug mode with MinGW 4.8, but it doesn't work correctly when @@ -45,7 +45,7 @@ #define PRINT_TRIES_COUNT -static void print_key(unsigned char *key) +static void print_key(const unsigned char *key) { for (size_t i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) { if (key[i] < 16) { @@ -125,7 +125,7 @@ int main(int argc, char *argv[]) } } while (!found); } else { - unsigned char *p = public_key + offset; + const unsigned char *p = public_key + offset; do { #ifdef PRINT_TRIES_COUNT diff --git a/other/make_single_file b/other/make_single_file index 50ae499d..7487ca51 100755 --- a/other/make_single_file +++ b/other/make_single_file @@ -6,8 +6,11 @@ # # Example: # -# other/make_single_file testing/misc_tools.c auto_tests/send_message_test.c | \ -# tcc -o send_message_test - $(pkg-config --cflags --libs libsodium opus vpx) +# other/make_single_file auto_tests/toxav_basic_test.c auto_tests/auto_test_support.c testing/misc_tools.c | \ +# tcc -o toxav_basic_test - $(pkg-config --cflags --libs libsodium opus vpx) +# +# other/make_single_file -core auto_tests/send_message_test.c auto_tests/auto_test_support.c testing/misc_tools.c | \ +# tcc -o send_message_test - $(pkg-config --cflags --libs libsodium) use strict; use warnings; @@ -44,7 +47,7 @@ sub emit { if (@ARGV and $ARGV[0] eq "-core") { shift @ARGV; - for my $fn (, ) { + for my $fn (, , ) { emit(abs_path $fn); } } else { diff --git a/testing/BUILD.bazel b/testing/BUILD.bazel index 3206e5f5..bb7a4930 100644 --- a/testing/BUILD.bazel +++ b/testing/BUILD.bazel @@ -15,11 +15,14 @@ sh_test( "-Wno-callback-names", "-Wno-enum-names", "+RTS", - "-N3", + "-N4", "-RTS", ], data = CIMPLE_FILES, - tags = ["haskell"], + tags = [ + "haskell", + "no-cross", + ], ) sh_test( @@ -32,7 +35,11 @@ sh_test( "-Iexternal/libvpx", "-Iexternal/opus/include", "-Ihs-tokstyle/include", - ] + ["$(locations %s)" % f for f in CIMPLE_FILES], + ] + ["$(locations %s)" % f for f in CIMPLE_FILES] + [ + "+RTS", + "-N4", + "-RTS", + ], data = CIMPLE_FILES + [ "//c-toxcore/third_party:headers", "//hs-tokstyle:headers", @@ -40,7 +47,10 @@ sh_test( "@libvpx//:headers", "@opus//:headers", ], - tags = ["haskell"], + tags = [ + "haskell", + "no-cross", + ], toolchains = ["@rules_cc//cc:current_cc_toolchain"], ) @@ -50,7 +60,10 @@ sh_test( srcs = ["//hs-cimple/tools:cimplefmt"], args = ["--reparse"] + ["$(locations %s)" % f for f in CIMPLE_FILES], data = CIMPLE_FILES, - tags = ["haskell"], + tags = [ + "haskell", + "no-cross", + ], ) cc_library( diff --git a/testing/CMakeLists.txt b/testing/CMakeLists.txt index 8cd8962b..82f228bf 100644 --- a/testing/CMakeLists.txt +++ b/testing/CMakeLists.txt @@ -3,7 +3,15 @@ set(misc_tools_SOURCES misc_tools.c misc_tools.h) add_library(misc_tools STATIC ${misc_tools_SOURCES}) -target_link_modules(misc_tools toxcore) +if(TARGET toxcore_static) + target_link_libraries(misc_tools PRIVATE toxcore_static) +else() + target_link_libraries(misc_tools PRIVATE toxcore_shared) +endif() +target_link_libraries(misc_tools PRIVATE ${LIBSODIUM_LIBRARIES}) +target_link_directories(misc_tools PUBLIC ${LIBSODIUM_LIBRARY_DIRS}) +target_include_directories(misc_tools SYSTEM PRIVATE ${LIBSODIUM_INCLUDE_DIRS}) +target_compile_options(misc_tools PRIVATE ${LIBSODIUM_CFLAGS_OTHER}) ################################################################################ # @@ -11,7 +19,12 @@ target_link_modules(misc_tools toxcore) # ################################################################################ -if (BUILD_MISC_TESTS) +if(BUILD_MISC_TESTS) add_executable(Messenger_test Messenger_test.c) - target_link_modules(Messenger_test toxcore misc_tools) + target_link_libraries(Messenger_test PRIVATE misc_tools) + if(TARGET toxcore_static) + target_link_libraries(Messenger_test PRIVATE toxcore_static) + else() + target_link_libraries(Messenger_test PRIVATE toxcore_shared) + endif() endif() diff --git a/testing/Makefile.inc b/testing/Makefile.inc index fd9093e4..223ebdcf 100644 --- a/testing/Makefile.inc +++ b/testing/Makefile.inc @@ -19,16 +19,12 @@ noinst_PROGRAMS += Messenger_test Messenger_test_SOURCES = \ ../testing/Messenger_test.c -Messenger_test_CFLAGS = $(LIBSODIUM_CFLAGS) \ - $(NACL_CFLAGS) +Messenger_test_CFLAGS = $(LIBSODIUM_CFLAGS) Messenger_test_LDADD = $(LIBSODIUM_LDFLAGS) \ - $(NACL_LDFLAGS) \ libmisc_tools.la \ libtoxcore.la \ $(LIBSODIUM_LIBS) \ - $(NACL_OBJECTS) \ - $(NACL_LIBS) \ $(WINSOCK2_LIBS) endif diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 4c2c8f85..50537995 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -12,7 +12,7 @@ * If it receives a message from a friend it replies back. * * - * This is how I compile it: gcc -O2 -Wall -D VANILLA_NACL -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} Messenger_test.c + * This is how I compile it: gcc -O2 -Wall -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c Messenger_test.c -lsodium * * * Command line arguments are the ip, port and public_key of a node (for bootstrapping). diff --git a/testing/fuzzing/CMakeLists.txt b/testing/fuzzing/CMakeLists.txt index a000ccb0..c2b88c72 100644 --- a/testing/fuzzing/CMakeLists.txt +++ b/testing/fuzzing/CMakeLists.txt @@ -13,14 +13,14 @@ endif() # Fuzzes the toxsave API add_executable(toxsave_fuzzer toxsave_harness.cc) -target_link_libraries(toxsave_fuzzer toxcore_static fuzz_support ${LIBFUZZER_LINKER_FLAGS}) +target_link_libraries(toxsave_fuzzer PRIVATE toxcore_static fuzz_support ${LIBFUZZER_LINKER_FLAGS}) # Fuzzes the bootstrap process add_executable(bootstrap_fuzzer bootstrap_harness.cc) -target_link_libraries(bootstrap_fuzzer toxcore_static fuzz_support ${LIBFUZZER_LINKER_FLAGS}) +target_link_libraries(bootstrap_fuzzer PRIVATE toxcore_static fuzz_support ${LIBFUZZER_LINKER_FLAGS}) add_executable(DHT_fuzz_test ../../toxcore/DHT_fuzz_test.cc) -target_link_libraries(DHT_fuzz_test toxcore_static fuzz_support ${LIBFUZZER_LINKER_FLAGS}) +target_link_libraries(DHT_fuzz_test PRIVATE toxcore_static fuzz_support ${LIBFUZZER_LINKER_FLAGS}) add_executable(tox_events_fuzz_test ../../toxcore/tox_events_fuzz_test.cc) -target_link_libraries(tox_events_fuzz_test toxcore_static fuzz_support ${LIBFUZZER_LINKER_FLAGS}) +target_link_libraries(tox_events_fuzz_test PRIVATE toxcore_static fuzz_support ${LIBFUZZER_LINKER_FLAGS}) diff --git a/testing/misc_tools.c b/testing/misc_tools.c index 6188599b..e274e5a7 100644 --- a/testing/misc_tools.c +++ b/testing/misc_tools.c @@ -20,9 +20,7 @@ #include #include -#ifndef VANILLA_NACL #include -#endif #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) #include @@ -141,7 +139,6 @@ int cmdline_parsefor_ipv46(int argc, char **argv, bool *ipv6enabled) } -#ifndef VANILLA_NACL static const char *test_rng_name(void) { return "test_rng"; @@ -198,11 +195,3 @@ int use_test_rng(uint32_t seed) return randombytes_set_implementation(&test_rng); } - -#else - -int use_test_rng(uint32_t seed) -{ - assert(!"libsodium required for use_test_rng"); -} -#endif diff --git a/toxav/Makefile.inc b/toxav/Makefile.inc index 7b788198..a8465a2f 100644 --- a/toxav/Makefile.inc +++ b/toxav/Makefile.inc @@ -25,19 +25,16 @@ libtoxav_la_SOURCES = ../toxav/rtp.h \ libtoxav_la_CFLAGS = -I../toxcore \ -I../toxav \ $(LIBSODIUM_CFLAGS) \ - $(NACL_CFLAGS) \ $(AV_CFLAGS) \ $(PTHREAD_CFLAGS) libtoxav_la_LDFLAGS = $(LT_LDFLAGS) \ $(LIBSODIUM_LDFLAGS) \ - $(NACL_LDFLAGS) \ $(EXTRA_LT_LDFLAGS) \ $(WINSOCK2_LIBS) libtoxav_la_LIBADD = libtoxcore.la \ $(LIBSODIUM_LIBS) \ - $(NACL_LIBS) \ $(PTHREAD_LIBS) \ $(AV_LIBS) diff --git a/toxav/groupav.c b/toxav/groupav.c index 4e3ad2cc..6a48c684 100644 --- a/toxav/groupav.c +++ b/toxav/groupav.c @@ -434,7 +434,7 @@ static int handle_group_audio_packet(void *object, uint32_t groupnumber, uint32_ } while (decode_audio_packet((Group_AV *)object, peer_av, groupnumber, friendgroupnumber) == 0) { - continue; + /* Continue. */ } return 0; @@ -612,7 +612,7 @@ static int send_audio_packet(const Group_Chats *g_c, uint32_t groupnumber, const * @retval 0 on success. * @retval -1 on failure. */ -int group_send_audio(Group_Chats *g_c, uint32_t groupnumber, const int16_t *pcm, unsigned int samples, uint8_t channels, +int group_send_audio(const Group_Chats *g_c, uint32_t groupnumber, const int16_t *pcm, unsigned int samples, uint8_t channels, uint32_t sample_rate) { Group_AV *group_av = (Group_AV *)group_get_object(g_c, groupnumber); diff --git a/toxav/groupav.h b/toxav/groupav.h index a2801535..e85bc5f2 100644 --- a/toxav/groupav.h +++ b/toxav/groupav.h @@ -40,7 +40,7 @@ int join_av_groupchat(const Logger *log, Tox *tox, Group_Chats *g_c, uint32_t fr * @retval 0 on success. * @retval -1 on failure. */ -int group_send_audio(Group_Chats *g_c, uint32_t groupnumber, const int16_t *pcm, unsigned int samples, uint8_t channels, +int group_send_audio(const Group_Chats *g_c, uint32_t groupnumber, const int16_t *pcm, unsigned int samples, uint8_t channels, uint32_t sample_rate); /** @brief Enable A/V in a groupchat. diff --git a/toxav/msi.c b/toxav/msi.c index e243884a..2b45e7cb 100644 --- a/toxav/msi.c +++ b/toxav/msi.c @@ -62,8 +62,8 @@ static void msg_init(MSIMessage *dest, MSIRequest request); static int msg_parse_in(const Logger *log, MSIMessage *dest, const uint8_t *data, uint16_t length); static uint8_t *msg_parse_header_out(MSIHeaderID id, uint8_t *dest, const void *value, uint8_t value_len, uint16_t *length); -static int send_message(Messenger *m, uint32_t friend_number, const MSIMessage *msg); -static int send_error(Messenger *m, uint32_t friend_number, MSIError error); +static int send_message(const Messenger *m, uint32_t friend_number, const MSIMessage *msg); +static int send_error(const Messenger *m, uint32_t friend_number, MSIError error); static bool invoke_callback(MSICall *call, MSICallbackID cb); static MSICall *get_call(MSISession *session, uint32_t friend_number); static MSICall *new_call(MSISession *session, uint32_t friend_number); @@ -444,7 +444,7 @@ static uint8_t *msg_parse_header_out(MSIHeaderID id, uint8_t *dest, const void * return dest + value_len; /* Set to next position ready to be written */ } -static int send_message(Messenger *m, uint32_t friend_number, const MSIMessage *msg) +static int send_message(const Messenger *m, uint32_t friend_number, const MSIMessage *msg) { /* Parse and send message */ assert(m != nullptr); @@ -489,7 +489,7 @@ static int send_message(Messenger *m, uint32_t friend_number, const MSIMessage * return -1; } -static int send_error(Messenger *m, uint32_t friend_number, MSIError error) +static int send_error(const Messenger *m, uint32_t friend_number, MSIError error) { /* Send error message */ assert(m != nullptr); diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index 56693417..9424debc 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -838,11 +838,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/DHT.c b/toxcore/DHT.c index 9a418039..66a16600 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -1930,14 +1930,11 @@ static void do_close(DHT *dht) for (size_t i = 0; i < LCLIENT_LIST; ++i) { Client_data *const client = &dht->close_clientlist[i]; - IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4, nullptr }; - - for (IPPTsPng * const *it = assocs; *it != nullptr; ++it) { - IPPTsPng *const assoc = *it; - - if (assoc->timestamp != 0) { - assoc->timestamp = badonly; - } + if (client->assoc4.timestamp != 0) { + client->assoc4.timestamp = badonly; + } + if (client->assoc6.timestamp != 0) { + client->assoc6.timestamp = badonly; } } } diff --git a/toxcore/DHT_test.cc b/toxcore/DHT_test.cc index 84d3b982..f5021613 100644 --- a/toxcore/DHT_test.cc +++ b/toxcore/DHT_test.cc @@ -192,8 +192,11 @@ TEST(AnnounceNodes, SetAndTest) const Memory *mem = system_memory(); Logger *log = logger_new(); + ASSERT_NE(log, nullptr); Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); + ASSERT_NE(mono_time, nullptr); Networking_Core *net = new_networking_no_udp(log, mem, ns); + ASSERT_NE(net, nullptr); DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true); ASSERT_NE(dht, nullptr); diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index 88be145d..5783c91b 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/events/group_custom_packet.c \ ../toxcore/events/group_custom_private_packet.c \ @@ -90,6 +90,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 \ @@ -141,7 +143,6 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \ libtoxcore_la_CFLAGS = -I$(top_srcdir) \ -I$(top_srcdir)/toxcore \ $(LIBSODIUM_CFLAGS) \ - $(NACL_CFLAGS) \ $(MSGPACK_CFLAGS) \ $(PTHREAD_CFLAGS) \ -DCMP_NO_FLOAT=1 @@ -149,15 +150,12 @@ libtoxcore_la_CFLAGS = -I$(top_srcdir) \ libtoxcore_la_LDFLAGS = $(LT_LDFLAGS) \ $(EXTRA_LT_LDFLAGS) \ $(LIBSODIUM_LDFLAGS) \ - $(NACL_LDFLAGS) \ $(MSGPACK_LDFLAGS) \ $(MATH_LDFLAGS) \ $(RT_LIBS) \ $(WINSOCK2_LIBS) libtoxcore_la_LIBADD = $(LIBSODIUM_LIBS) \ - $(NACL_OBJECTS) \ - $(NACL_LIBS) \ $(MSGPACK_LIBS) \ $(PTHREAD_LIBS) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index cf6f613d..03872cc6 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -757,17 +757,34 @@ int m_set_statusmessage(Messenger *m, const uint8_t *status, uint16_t length) return 0; } -static Userstatus userstatus_from_int(uint8_t status) +non_null() +static bool userstatus_from_int(uint8_t status, Userstatus *out) { switch (status) { - case 0: - return USERSTATUS_NONE; - case 1: - return USERSTATUS_AWAY; - case 2: - return USERSTATUS_BUSY; - default: - return USERSTATUS_INVALID; + case USERSTATUS_NONE: { + *out = USERSTATUS_NONE; + return true; + } + + case USERSTATUS_AWAY: { + *out = USERSTATUS_AWAY; + return true; + } + + case USERSTATUS_BUSY: { + *out = USERSTATUS_BUSY; + return true; + } + + case USERSTATUS_INVALID: { + *out = USERSTATUS_INVALID; + return true; + } + + default: { + *out = USERSTATUS_INVALID; + return false; + } } } @@ -781,7 +798,7 @@ int m_set_userstatus(Messenger *m, uint8_t status) return 0; } - m->userstatus = userstatus_from_int(status); + userstatus_from_int(status, &m->userstatus); for (uint32_t i = 0; i < m->numfriends; ++i) { m->friendlist[i].userstatus_sent = false; @@ -937,7 +954,7 @@ static int set_friend_statusmessage(const Messenger *m, int32_t friendnumber, co non_null() static void set_friend_userstatus(const Messenger *m, int32_t friendnumber, uint8_t status) { - m->friendlist[friendnumber].userstatus = userstatus_from_int(status); + userstatus_from_int(status, &m->friendlist[friendnumber].userstatus); } non_null() @@ -2095,9 +2112,8 @@ static int m_handle_packet_userstatus(Messenger *m, const int i, const uint8_t * return 0; } - const Userstatus status = userstatus_from_int(data[0]); - - if (status == USERSTATUS_INVALID) { + Userstatus status; + if (!userstatus_from_int(data[0], &status)) { return 0; } @@ -2343,8 +2359,6 @@ static int m_handle_packet_msi(Messenger *m, const int i, const uint8_t *data, c non_null(1, 3) nullable(5) static int m_handle_packet_invite_groupchat(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata) { -#ifndef VANILLA_NACL - // first two bytes are messenger packet type and group invite type if (data_length < 2 + GC_JOIN_DATA_LENGTH) { return 0; @@ -2365,8 +2379,6 @@ static int m_handle_packet_invite_groupchat(Messenger *m, const int i, const uin handle_gc_invite_confirmed_packet(m->group_handler, i, join_data, join_data_len); } -#endif // VANILLA_NACL - return 0; } @@ -2547,7 +2559,6 @@ uint32_t messenger_run_interval(const Messenger *m) * * @retval true if success. */ -#ifndef VANILLA_NACL non_null() static bool self_announce_group(const Messenger *m, GC_Chat *chat, Onion_Friend *onion_friend) { @@ -2623,7 +2634,6 @@ static void do_gc_onion_friends(const Messenger *m) } } } -#endif // VANILLA_NACL /** @brief The main loop that needs to be run at least 20 times per second. */ void do_messenger(Messenger *m, void *userdata) @@ -2661,11 +2671,9 @@ void do_messenger(Messenger *m, void *userdata) do_onion_client(m->onion_c); do_friend_connections(m->fr_c, userdata); do_friends(m, userdata); -#ifndef VANILLA_NACL do_gc(m->group_handler, userdata); do_gca(m->mono_time, m->group_announce); do_gc_onion_friends(m); -#endif m_connection_status_callback(m, userdata); if (mono_time_get(m->mono_time) > m->lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) { @@ -3148,7 +3156,6 @@ static State_Load_Status friends_list_load(Messenger *m, const uint8_t *data, ui return STATE_LOAD_STATUS_CONTINUE; } -#ifndef VANILLA_NACL non_null() static void pack_groupchats(const GC_Session *c, Bin_Pack *bp) { @@ -3176,7 +3183,7 @@ static bool pack_groupchats_handler(Bin_Pack *bp, const Logger *log, const void non_null() static uint32_t saved_groups_size(const Messenger *m) { - GC_Session *c = m->group_handler; + const GC_Session *c = m->group_handler; return bin_pack_obj_size(pack_groupchats_handler, m->log, c); } @@ -3245,7 +3252,6 @@ static State_Load_Status groups_load(Messenger *m, const uint8_t *data, uint32_t return STATE_LOAD_STATUS_CONTINUE; } -#endif /* VANILLA_NACL */ // name state plugin non_null() @@ -3433,9 +3439,7 @@ static void m_register_default_plugins(Messenger *m) m_register_state_plugin(m, STATE_TYPE_STATUSMESSAGE, status_message_size, load_status_message, save_status_message); m_register_state_plugin(m, STATE_TYPE_STATUS, status_size, load_status, save_status); -#ifndef VANILLA_NACL m_register_state_plugin(m, STATE_TYPE_GROUPS, saved_groups_size, groups_load, groups_save); -#endif m_register_state_plugin(m, STATE_TYPE_TCP_RELAY, tcp_relay_size, load_tcp_relays, save_tcp_relays); m_register_state_plugin(m, STATE_TYPE_PATH_NODE, path_node_size, load_path_nodes, save_path_nodes); } @@ -3579,14 +3583,14 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * if (m->net == nullptr) { friendreq_kill(m->fr); - logger_kill(m->log); - mem_delete(mem, m); if (error != nullptr && net_err == 1) { LOGGER_WARNING(m->log, "network initialisation failed (no ports available)"); *error = MESSENGER_ERROR_PORT; } + logger_kill(m->log); + mem_delete(mem, m); return nullptr; } @@ -3613,7 +3617,6 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * return nullptr; } -#ifndef VANILLA_NACL m->group_announce = new_gca_list(); if (m->group_announce == nullptr) { @@ -3628,11 +3631,13 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * return nullptr; } -#endif /* VANILLA_NACL */ - if (options->dht_announcements_enabled) { m->forwarding = new_forwarding(m->log, m->rng, m->mono_time, m->dht); - m->announce = new_announcements(m->log, m->mem, m->rng, m->mono_time, m->forwarding); + if (m->forwarding != nullptr) { + m->announce = new_announcements(m->log, m->mem, m->rng, m->mono_time, m->forwarding); + } else { + m->announce = nullptr; + } } else { m->forwarding = nullptr; m->announce = nullptr; @@ -3652,9 +3657,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * kill_onion(m->onion); kill_onion_announce(m->onion_a); kill_onion_client(m->onion_c); -#ifndef VANILLA_NACL kill_gca(m->group_announce); -#endif /* VANILLA_NACL */ kill_friend_connections(m->fr_c); kill_announcements(m->announce); kill_forwarding(m->forwarding); @@ -3667,7 +3670,6 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * return nullptr; } -#ifndef VANILLA_NACL gca_onion_init(m->group_announce, m->onion_a); m->group_handler = new_dht_groupchats(m); @@ -3691,8 +3693,6 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * return nullptr; } -#endif /* VANILLA_NACL */ - if (options->tcp_server_port != 0) { m->tcp_server = new_tcp_server(m->log, m->mem, m->rng, m->ns, options->ipv6enabled, 1, &options->tcp_server_port, dht_get_self_secret_key(m->dht), @@ -3703,14 +3703,10 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * kill_onion(m->onion); kill_onion_announce(m->onion_a); -#ifndef VANILLA_NACL kill_dht_groupchats(m->group_handler); -#endif kill_friend_connections(m->fr_c); kill_onion_client(m->onion_c); -#ifndef VANILLA_NACL kill_gca(m->group_announce); -#endif kill_announcements(m->announce); kill_forwarding(m->forwarding); kill_net_crypto(m->net_crypto); @@ -3762,14 +3758,10 @@ void kill_messenger(Messenger *m) kill_onion(m->onion); kill_onion_announce(m->onion_a); -#ifndef VANILLA_NACL kill_dht_groupchats(m->group_handler); -#endif kill_friend_connections(m->fr_c); kill_onion_client(m->onion_c); -#ifndef VANILLA_NACL kill_gca(m->group_announce); -#endif kill_announcements(m->announce); kill_forwarding(m->forwarding); kill_net_crypto(m->net_crypto); @@ -3780,11 +3772,11 @@ void kill_messenger(Messenger *m) clear_receipts(m, i); } - logger_kill(m->log); mem_delete(m->mem, m->friendlist); friendreq_kill(m->fr); mem_delete(m->mem, m->options.state_plugins); + logger_kill(m->log); mem_delete(m->mem, m); } diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index cabb2af8..0ea88d70 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -297,7 +297,7 @@ struct Messenger { m_friend_connectionstatuschange_internal_cb *friend_connectionstatuschange_internal; void *friend_connectionstatuschange_internal_userdata; - struct Group_Chats *conferences_object; /* Set by new_groupchats()*/ + struct Group_Chats *conferences_object; m_conference_invite_cb *conference_invite; m_group_invite_cb *group_invite; diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index aeb722f6..a698eca8 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -118,9 +118,9 @@ static bool connect_sock_to(const Logger *logger, const Memory *mem, Socket sock non_null() static int proxy_http_generate_connection_request(TCP_Client_Connection *tcp_conn) { - char one[] = "CONNECT "; - char two[] = " HTTP/1.1\nHost: "; - char three[] = "\r\n\r\n"; + const char one[] = "CONNECT "; + const char two[] = " HTTP/1.1\nHost: "; + const char three[] = "\r\n\r\n"; char ip[TOX_INET6_ADDRSTRLEN]; @@ -149,7 +149,7 @@ static int proxy_http_generate_connection_request(TCP_Client_Connection *tcp_con non_null() static int proxy_http_read_connection_response(const Logger *logger, const TCP_Client_Connection *tcp_conn) { - char success[] = "200"; + const char success[] = "200"; uint8_t data[16]; // draining works the best if the length is a power of 2 const TCP_Connection *con0 = &tcp_conn->con; @@ -185,14 +185,16 @@ static int proxy_http_read_connection_response(const Logger *logger, const TCP_C return -1; } -#define TCP_SOCKS5_PROXY_HS_VERSION_SOCKS5 0x05 -#define TCP_SOCKS5_PROXY_HS_COMM_ESTABLISH_REQUEST 0x01 -#define TCP_SOCKS5_PROXY_HS_COMM_REQUEST_GRANTED 0x00 -#define TCP_SOCKS5_PROXY_HS_AUTH_METHODS_SUPPORTED 0x01 -#define TCP_SOCKS5_PROXY_HS_NO_AUTH 0x00 -#define TCP_SOCKS5_PROXY_HS_RESERVED 0x00 -#define TCP_SOCKS5_PROXY_HS_ADDR_TYPE_IPV4 0x01 -#define TCP_SOCKS5_PROXY_HS_ADDR_TYPE_IPV6 0x04 +enum Tcp_Socks5_Proxy_Hs { + TCP_SOCKS5_PROXY_HS_VERSION_SOCKS5 = 0x05, + TCP_SOCKS5_PROXY_HS_COMM_ESTABLISH_REQUEST = 0x01, + TCP_SOCKS5_PROXY_HS_COMM_REQUEST_GRANTED = 0x00, + TCP_SOCKS5_PROXY_HS_AUTH_METHODS_SUPPORTED = 0x01, + TCP_SOCKS5_PROXY_HS_NO_AUTH = 0x00, + TCP_SOCKS5_PROXY_HS_RESERVED = 0x00, + TCP_SOCKS5_PROXY_HS_ADDR_TYPE_IPV4 = 0x01, + TCP_SOCKS5_PROXY_HS_ADDR_TYPE_IPV6 = 0x04, +}; non_null() static void proxy_socks5_generate_greetings(TCP_Client_Connection *tcp_conn) @@ -910,8 +912,7 @@ static int do_confirmed_tcp(const Logger *logger, TCP_Client_Connection *conn, c } while (tcp_process_packet(logger, conn, userdata)) { - // Keep reading until error or out of data. - continue; + /* Keep reading until error or out of data. */ } return 0; diff --git a/toxcore/TCP_common.h b/toxcore/TCP_common.h index 7891168f..778962f8 100644 --- a/toxcore/TCP_common.h +++ b/toxcore/TCP_common.h @@ -24,33 +24,20 @@ void wipe_priority_list(const Memory *mem, TCP_Priority_List *p); #define NUM_RESERVED_PORTS 16 #define NUM_CLIENT_CONNECTIONS (256 - NUM_RESERVED_PORTS) -#ifdef USE_TEST_NETWORK -#define TCP_PACKET_FORWARD_REQUEST 11 -#define TCP_PACKET_FORWARDING 10 -#define TCP_PACKET_ROUTING_REQUEST 9 -#define TCP_PACKET_ROUTING_RESPONSE 8 -#define TCP_PACKET_CONNECTION_NOTIFICATION 7 -#define TCP_PACKET_DISCONNECT_NOTIFICATION 6 -#define TCP_PACKET_PING 5 -#define TCP_PACKET_PONG 4 -#define TCP_PACKET_OOB_SEND 3 -#define TCP_PACKET_OOB_RECV 2 -#define TCP_PACKET_ONION_REQUEST 1 -#define TCP_PACKET_ONION_RESPONSE 0 -#else -#define TCP_PACKET_ROUTING_REQUEST 0 -#define TCP_PACKET_ROUTING_RESPONSE 1 -#define TCP_PACKET_CONNECTION_NOTIFICATION 2 -#define TCP_PACKET_DISCONNECT_NOTIFICATION 3 -#define TCP_PACKET_PING 4 -#define TCP_PACKET_PONG 5 -#define TCP_PACKET_OOB_SEND 6 -#define TCP_PACKET_OOB_RECV 7 -#define TCP_PACKET_ONION_REQUEST 8 -#define TCP_PACKET_ONION_RESPONSE 9 -#define TCP_PACKET_FORWARD_REQUEST 10 -#define TCP_PACKET_FORWARDING 11 -#endif // test network +typedef enum Tcp_Packet { + TCP_PACKET_ROUTING_REQUEST = 0, + TCP_PACKET_ROUTING_RESPONSE = 1, + TCP_PACKET_CONNECTION_NOTIFICATION = 2, + TCP_PACKET_DISCONNECT_NOTIFICATION = 3, + TCP_PACKET_PING = 4, + TCP_PACKET_PONG = 5, + TCP_PACKET_OOB_SEND = 6, + TCP_PACKET_OOB_RECV = 7, + TCP_PACKET_ONION_REQUEST = 8, + TCP_PACKET_ONION_RESPONSE = 9, + TCP_PACKET_FORWARD_REQUEST = 10, + TCP_PACKET_FORWARDING = 11, +} Tcp_Packet; #define TCP_HANDSHAKE_PLAIN_SIZE (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE) #define TCP_SERVER_HANDSHAKE_SIZE (CRYPTO_NONCE_SIZE + TCP_HANDSHAKE_PLAIN_SIZE + CRYPTO_MAC_SIZE) diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index f274376c..c6118cdd 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -407,7 +407,7 @@ int get_random_tcp_onion_conn_number(const TCP_Connections *tcp_c) * return -1 on failure. */ non_null() -static int get_conn_number_by_ip_port(TCP_Connections *tcp_c, const IP_Port *ip_port) +static int get_conn_number_by_ip_port(const TCP_Connections *tcp_c, const IP_Port *ip_port) { for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) { const IP_Port conn_ip_port = tcp_con_ip_port(tcp_c->tcp_connections[i].connection); @@ -1059,7 +1059,7 @@ static int send_tcp_relay_routing_request(const TCP_Connections *tcp_c, int tcp_ non_null() static int tcp_response_callback(void *object, uint8_t connection_id, const uint8_t *public_key) { - TCP_Client_Connection *tcp_client_con = (TCP_Client_Connection *)object; + const TCP_Client_Connection *tcp_client_con = (const TCP_Client_Connection *)object; const TCP_Connections *tcp_c = (const TCP_Connections *)tcp_con_custom_object(tcp_client_con); const unsigned int tcp_connections_number = tcp_con_custom_uint(tcp_client_con); @@ -1256,7 +1256,7 @@ static int tcp_relay_on_online(TCP_Connections *tcp_c, int tcp_connections_numbe bool sent_any = false; for (uint32_t i = 0; i < tcp_c->connections_length; ++i) { - TCP_Connection_to *con_to = get_connection(tcp_c, i); + const TCP_Connection_to *con_to = get_connection(tcp_c, i); if (con_to != nullptr) { if (tcp_connection_in_conn(con_to, tcp_connections_number)) { diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 905d2bb4..c07b176a 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -1144,8 +1144,7 @@ non_null() static void do_confirmed_recv(TCP_Server *tcp_server, uint32_t i) { while (tcp_process_secure_packet(tcp_server, i)) { - // Keep reading until an error occurs or there is no more data to read. - continue; + /* Keep reading until an error occurs or there is no more data to read. */ } } diff --git a/toxcore/announce.c b/toxcore/announce.c index 402d1136..5ac49867 100644 --- a/toxcore/announce.c +++ b/toxcore/announce.c @@ -302,7 +302,7 @@ static int create_reply_plain_data_search_request(Announcements *announce, const IP_Port *source, const uint8_t *data, uint16_t length, uint8_t *reply, uint16_t reply_max_length, - uint8_t *to_auth, uint16_t to_auth_length) + const uint8_t *to_auth, uint16_t to_auth_length) { if (length != CRYPTO_PUBLIC_KEY_SIZE && length != CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SHA256_SIZE) { @@ -377,11 +377,12 @@ static int create_reply_plain_data_search_request(Announcements *announce, } non_null() -static int create_reply_plain_data_retrieve_request(Announcements *announce, +static int create_reply_plain_data_retrieve_request( + const Announcements *announce, const IP_Port *source, const uint8_t *data, uint16_t length, uint8_t *reply, uint16_t reply_max_length, - uint8_t *to_auth, uint16_t to_auth_length) + const uint8_t *to_auth, uint16_t to_auth_length) { if (length != CRYPTO_PUBLIC_KEY_SIZE + 1 + TIMED_AUTH_SIZE) { return -1; @@ -423,7 +424,7 @@ static int create_reply_plain_store_announce_request(Announcements *announce, const IP_Port *source, const uint8_t *data, uint16_t length, uint8_t *reply, uint16_t reply_max_length, - uint8_t *to_auth, uint16_t to_auth_length) + const uint8_t *to_auth, uint16_t to_auth_length) { const int plain_len = (int)length - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE); const int announcement_len = (int)plain_len - (TIMED_AUTH_SIZE + sizeof(uint32_t) + 1); diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index 14025252..44ce4a1f 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c @@ -14,19 +14,7 @@ #include #include -#ifndef VANILLA_NACL -// We use libsodium by default. #include -#else -#include -#include -#include -#include -#include -#include -#include -#include -#endif #include "ccompat.h" @@ -34,11 +22,9 @@ #define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) #endif -#ifndef VANILLA_NACL // Need dht because of ENC_SECRET_KEY_SIZE and ENC_PUBLIC_KEY_SIZE #define ENC_PUBLIC_KEY_SIZE CRYPTO_PUBLIC_KEY_SIZE #define ENC_SECRET_KEY_SIZE CRYPTO_SECRET_KEY_SIZE -#endif static_assert(CRYPTO_PUBLIC_KEY_SIZE == crypto_box_PUBLICKEYBYTES, "CRYPTO_PUBLIC_KEY_SIZE should be equal to crypto_box_PUBLICKEYBYTES"); @@ -63,20 +49,15 @@ static_assert(CRYPTO_SHA512_SIZE == crypto_hash_sha512_BYTES, static_assert(CRYPTO_PUBLIC_KEY_SIZE == 32, "CRYPTO_PUBLIC_KEY_SIZE is required to be 32 bytes for pk_equal to work"); -#ifndef VANILLA_NACL static_assert(CRYPTO_SIGNATURE_SIZE == crypto_sign_BYTES, "CRYPTO_SIGNATURE_SIZE should be equal to crypto_sign_BYTES"); static_assert(CRYPTO_SIGN_PUBLIC_KEY_SIZE == crypto_sign_PUBLICKEYBYTES, "CRYPTO_SIGN_PUBLIC_KEY_SIZE should be equal to crypto_sign_PUBLICKEYBYTES"); static_assert(CRYPTO_SIGN_SECRET_KEY_SIZE == crypto_sign_SECRETKEYBYTES, "CRYPTO_SIGN_SECRET_KEY_SIZE should be equal to crypto_sign_SECRETKEYBYTES"); -#endif /* VANILLA_NACL */ bool create_extended_keypair(uint8_t *pk, uint8_t *sk) { -#ifdef VANILLA_NACL - return false; -#else /* create signature key pair */ crypto_sign_keypair(pk + ENC_PUBLIC_KEY_SIZE, sk + ENC_SECRET_KEY_SIZE); @@ -87,7 +68,6 @@ bool create_extended_keypair(uint8_t *pk, uint8_t *sk) const int res2 = crypto_sign_ed25519_sk_to_curve25519(sk, sk + ENC_SECRET_KEY_SIZE); return res1 == 0 && res2 == 0; -#endif } const uint8_t *get_enc_key(const uint8_t *key) @@ -141,7 +121,7 @@ static void crypto_free(uint8_t *ptr, size_t bytes) void crypto_memzero(void *data, size_t length) { -#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) || defined(VANILLA_NACL) +#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) memset(data, 0, length); #else sodium_memzero(data, length); @@ -150,7 +130,7 @@ void crypto_memzero(void *data, size_t length) bool crypto_memlock(void *data, size_t length) { -#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) || defined(VANILLA_NACL) +#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) return false; #else @@ -164,7 +144,7 @@ bool crypto_memlock(void *data, size_t length) bool crypto_memunlock(void *data, size_t length) { -#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) || defined(VANILLA_NACL) +#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) return false; #else @@ -196,10 +176,6 @@ bool crypto_sha512_eq(const uint8_t *cksum1, const uint8_t *cksum2) #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) // Hope that this is better for the fuzzer return memcmp(cksum1, cksum2, CRYPTO_SHA512_SIZE) == 0; -#elif defined(VANILLA_NACL) - const int lo = crypto_verify_32(cksum1, cksum2) == 0 ? 1 : 0; - const int hi = crypto_verify_32(cksum1 + 8, cksum2 + 8) == 0 ? 1 : 0; - return (lo & hi) == 1; #else return crypto_verify_64(cksum1, cksum2) == 0; #endif @@ -251,21 +227,13 @@ uint32_t random_range_u32(const Random *rng, uint32_t upper_bound) bool crypto_signature_create(uint8_t *signature, const uint8_t *message, uint64_t message_length, const uint8_t *secret_key) { -#ifdef VANILLA_NACL - return false; -#else return crypto_sign_detached(signature, nullptr, message, message_length, secret_key) == 0; -#endif // VANILLA_NACL } bool crypto_signature_verify(const uint8_t *signature, const uint8_t *message, uint64_t message_length, const uint8_t *public_key) { -#ifdef VANILLA_NACL - return false; -#else return crypto_sign_verify_detached(signature, message, message_length, public_key) == 0; -#endif } bool public_key_valid(const uint8_t *public_key) @@ -539,17 +507,7 @@ static void sys_random_bytes(void *obj, uint8_t *bytes, size_t length) non_null() static uint32_t sys_random_uniform(void *obj, uint32_t upper_bound) { -#ifdef VANILLA_NACL - if (upper_bound == 0) { - return 0; - } - - uint32_t randnum; - sys_random_bytes(obj, (uint8_t *)&randnum, sizeof(randnum)); - return randnum % upper_bound; -#else return randombytes_uniform(upper_bound); -#endif } static const Random_Funcs system_random_funcs = { @@ -566,13 +524,11 @@ const Random *system_random(void) return nullptr; } #endif -#ifndef VANILLA_NACL // It is safe to call this function more than once and from different // threads -- subsequent calls won't have any effects. if (sodium_init() == -1) { return nullptr; } -#endif return &system_random_obj; } diff --git a/toxcore/crypto_core.h b/toxcore/crypto_core.h index 3b9f27d6..bfcddc31 100644 --- a/toxcore/crypto_core.h +++ b/toxcore/crypto_core.h @@ -235,8 +235,7 @@ uint64_t random_u64(const Random *rng); /** * @brief Return a random 32 bit integer between 0 and upper_bound (excluded). * - * On libsodium builds this function guarantees a uniform distribution of possible outputs. - * On vanilla NACL builds this function is equivalent to `random() % upper_bound`. + * This function guarantees a uniform distribution of possible outputs. */ non_null() uint32_t random_range_u32(const Random *rng, uint32_t upper_bound); diff --git a/toxcore/events/conference_connected.c b/toxcore/events/conference_connected.c index ce278755..74935e76 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" @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -26,6 +27,19 @@ struct Tox_Event_Conference_Connected { uint32_t conference_number; }; +non_null() +static void tox_event_conference_connected_set_conference_number(Tox_Event_Conference_Connected *conference_connected, + uint32_t conference_number) +{ + assert(conference_connected != nullptr); + conference_connected->conference_number = conference_number; +} +uint32_t tox_event_conference_connected_get_conference_number(const Tox_Event_Conference_Connected *conference_connected) +{ + assert(conference_connected != nullptr); + return conference_connected->conference_number; +} + non_null() static void tox_event_conference_connected_construct(Tox_Event_Conference_Connected *conference_connected) { @@ -34,27 +48,12 @@ static void tox_event_conference_connected_construct(Tox_Event_Conference_Connec }; } non_null() -static void tox_event_conference_connected_destruct(Tox_Event_Conference_Connected *conference_connected) +static void tox_event_conference_connected_destruct(Tox_Event_Conference_Connected *conference_connected, const Memory *mem) { return; } -non_null() -static void tox_event_conference_connected_set_conference_number( - Tox_Event_Conference_Connected *conference_connected, uint32_t conference_number) -{ - assert(conference_connected != nullptr); - conference_connected->conference_number = conference_number; -} -uint32_t tox_event_conference_connected_get_conference_number( - const Tox_Event_Conference_Connected *conference_connected) -{ - assert(conference_connected != nullptr); - return conference_connected->conference_number; -} - -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); @@ -64,7 +63,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); @@ -74,93 +73,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 event->type == TOX_EVENT_CONFERENCE_CONNECTED ? event->data.conference_connected : 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)); + + if (conference_connected == nullptr) { 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)); - - if (new_conference_connected == nullptr) { - return nullptr; - } - - events->conference_connected = new_conference_connected; - events->conference_connected_capacity = new_conference_connected_capacity; - } - - 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; + if (conference_connected != nullptr) { + tox_event_conference_connected_destruct(conference_connected, mem); } - - for (uint32_t i = 0; i < events->conference_connected_size; ++i) { - tox_event_conference_connected_destruct(&events->conference_connected[i]); - } - - 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]; -} - -bool tox_events_pack_conference_connected(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_conference_connected_size(events); + uint32_t conference_connected_index = 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 (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 true; + + return nullptr; } -bool tox_events_unpack_conference_connected(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_conference_connected_size(const Tox_Events *events) { - Tox_Event_Conference_Connected *event = tox_events_add_conference_connected(events); + uint32_t conference_connected_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_CONFERENCE_CONNECTED) { + ++conference_connected_size; + } + } + + return conference_connected_size; +} + +bool tox_event_conference_connected_unpack( + Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_conference_connected_new(mem); + + 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; +} /***************************************************** @@ -170,19 +197,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 64a4898b..f47b72ec 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) @@ -103,7 +90,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); @@ -116,7 +115,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); @@ -125,97 +124,127 @@ static bool tox_event_conference_invite_unpack( } return bin_unpack_u32(bu, &event->friend_number) - && tox_unpack_conference_type(bu, &event->type) + && tox_conference_type_unpack(bu, &event->type) && bin_unpack_bin(bu, &event->cookie, &event->cookie_length); } /***************************************************** * - * :: 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 event->type == TOX_EVENT_CONFERENCE_INVITE ? event->data.conference_invite : 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)); + + if (conference_invite == nullptr) { 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)); - - if (new_conference_invite == nullptr) { - return nullptr; - } - - events->conference_invite = new_conference_invite; - events->conference_invite_capacity = new_conference_invite_capacity; - } - - 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; + if (conference_invite != nullptr) { + tox_event_conference_invite_destruct(conference_invite, mem); } - - for (uint32_t i = 0; i < events->conference_invite_size; ++i) { - tox_event_conference_invite_destruct(&events->conference_invite[i]); - } - - 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]; -} - -bool tox_events_pack_conference_invite(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_conference_invite_size(events); + uint32_t conference_invite_index = 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 (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 true; + + return nullptr; } -bool tox_events_unpack_conference_invite(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_conference_invite_size(const Tox_Events *events) { - Tox_Event_Conference_Invite *event = tox_events_add_conference_invite(events); + uint32_t conference_invite_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_CONFERENCE_INVITE) { + ++conference_invite_size; + } + } + + return conference_invite_size; +} + +bool tox_event_conference_invite_unpack( + Tox_Event_Conference_Invite **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_conference_invite_new(mem); + + 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; } @@ -226,20 +255,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 e877fece..7f79e7ae 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) @@ -117,7 +104,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); @@ -131,7 +130,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); @@ -141,97 +140,127 @@ static bool tox_event_conference_message_unpack( return bin_unpack_u32(bu, &event->conference_number) && bin_unpack_u32(bu, &event->peer_number) - && tox_unpack_message_type(bu, &event->type) + && tox_message_type_unpack(bu, &event->type) && bin_unpack_bin(bu, &event->message, &event->message_length); } /***************************************************** * - * :: 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 event->type == TOX_EVENT_CONFERENCE_MESSAGE ? event->data.conference_message : 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)); + + if (conference_message == nullptr) { 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)); - - if (new_conference_message == nullptr) { - return nullptr; - } - - events->conference_message = new_conference_message; - events->conference_message_capacity = new_conference_message_capacity; - } - - 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; + if (conference_message != nullptr) { + tox_event_conference_message_destruct(conference_message, mem); } - - for (uint32_t i = 0; i < events->conference_message_size; ++i) { - tox_event_conference_message_destruct(&events->conference_message[i]); - } - - 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]; -} - -bool tox_events_pack_conference_message(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_conference_message_size(events); + uint32_t conference_message_index = 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 (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 true; + + return nullptr; } -bool tox_events_unpack_conference_message(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_conference_message_size(const Tox_Events *events) { - Tox_Event_Conference_Message *event = tox_events_add_conference_message(events); + uint32_t conference_message_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_CONFERENCE_MESSAGE) { + ++conference_message_size; + } + } + + return conference_message_size; +} + +bool tox_event_conference_message_unpack( + Tox_Event_Conference_Message **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_conference_message_new(mem); + + 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; } @@ -242,20 +271,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 b6aaa3bf..049f3e38 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" @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -27,36 +28,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) -{ - *conference_peer_list_changed = (Tox_Event_Conference_Peer_List_Changed) { - 0 - }; -} -non_null() -static void tox_event_conference_peer_list_changed_destruct(Tox_Event_Conference_Peer_List_Changed - *conference_peer_list_changed) -{ - return; -} - -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_set_conference_number(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed, + uint32_t conference_number) { assert(conference_peer_list_changed != nullptr); conference_peer_list_changed->conference_number = conference_number; } -uint32_t tox_event_conference_peer_list_changed_get_conference_number(const 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) { assert(conference_peer_list_changed != nullptr); return conference_peer_list_changed->conference_number; } non_null() -static bool tox_event_conference_peer_list_changed_pack( +static void tox_event_conference_peer_list_changed_construct(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed) +{ + *conference_peer_list_changed = (Tox_Event_Conference_Peer_List_Changed) { + 0 + }; +} +non_null() +static void tox_event_conference_peer_list_changed_destruct(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed, const Memory *mem) +{ + return; +} + +bool tox_event_conference_peer_list_changed_pack( const Tox_Event_Conference_Peer_List_Changed *event, Bin_Pack *bp) { assert(event != nullptr); @@ -66,7 +63,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); @@ -76,94 +73,120 @@ static bool tox_event_conference_peer_list_changed_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Conference_Peer_List_Changed *tox_events_add_conference_peer_list_changed(Tox_Events *events) +const Tox_Event_Conference_Peer_List_Changed *tox_event_get_conference_peer_list_changed(const Tox_Event *event) { - if (events->conference_peer_list_changed_size == UINT32_MAX) { + return event->type == TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED ? event->data.conference_peer_list_changed : nullptr; +} + +Tox_Event_Conference_Peer_List_Changed *tox_event_conference_peer_list_changed_new(const Memory *mem) +{ + 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)); - - if (new_conference_peer_list_changed == nullptr) { - return nullptr; - } - - events->conference_peer_list_changed = new_conference_peer_list_changed; - events->conference_peer_list_changed_capacity = new_conference_peer_list_changed_capacity; - } - - 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; return conference_peer_list_changed; } -void tox_events_clear_conference_peer_list_changed(Tox_Events *events) +void tox_event_conference_peer_list_changed_free(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed, const Memory *mem) { - if (events == nullptr) { - return; + 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); +} + +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; } - 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]); + 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; +} + +const Tox_Event_Conference_Peer_List_Changed *tox_events_get_conference_peer_list_changed(const Tox_Events *events, uint32_t index) +{ + 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; + } + + 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; - } - - return events->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) -{ - assert(index < events->conference_peer_list_changed_size); - assert(events->conference_peer_list_changed != nullptr); - return &events->conference_peer_list_changed[index]; -} - -bool tox_events_pack_conference_peer_list_changed(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_conference_peer_list_changed_size(events); + 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 (!tox_event_conference_peer_list_changed_pack(tox_events_get_conference_peer_list_changed(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED) { + ++conference_peer_list_changed_size; } } - return true; + + return conference_peer_list_changed_size; } -bool tox_events_unpack_conference_peer_list_changed(Tox_Events *events, Bin_Unpack *bu) +bool tox_event_conference_peer_list_changed_unpack( + Tox_Event_Conference_Peer_List_Changed **event, Bin_Unpack *bu, const Memory *mem) { - Tox_Event_Conference_Peer_List_Changed *event = tox_events_add_conference_peer_list_changed(events); + assert(event != nullptr); + *event = tox_event_conference_peer_list_changed_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_conference_peer_list_changed_unpack(event, bu); + return tox_event_conference_peer_list_changed_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Conference_Peer_List_Changed *tox_event_conference_peer_list_changed_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_List_Changed *conference_peer_list_changed = tox_events_add_conference_peer_list_changed(state->events, state->mem); + + if (conference_peer_list_changed == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return conference_peer_list_changed; } @@ -174,20 +197,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 07aaa5a5..2a056945 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; @@ -103,7 +90,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); @@ -116,7 +115,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); @@ -132,91 +131,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 event->type == TOX_EVENT_CONFERENCE_PEER_NAME ? event->data.conference_peer_name : 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)); + + if (conference_peer_name == nullptr) { 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)); - - if (new_conference_peer_name == nullptr) { - return nullptr; - } - - events->conference_peer_name = new_conference_peer_name; - events->conference_peer_name_capacity = new_conference_peer_name_capacity; - } - - 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; + if (conference_peer_name != nullptr) { + tox_event_conference_peer_name_destruct(conference_peer_name, mem); } - - for (uint32_t i = 0; i < events->conference_peer_name_size; ++i) { - tox_event_conference_peer_name_destruct(&events->conference_peer_name[i]); - } - - 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]; -} - -bool tox_events_pack_conference_peer_name(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_conference_peer_name_size(events); + 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 (!tox_event_conference_peer_name_pack(tox_events_get_conference_peer_name(events, i), bp)) { - return false; + 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 true; + + return nullptr; } -bool tox_events_unpack_conference_peer_name(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_conference_peer_name_size(const Tox_Events *events) { - Tox_Event_Conference_Peer_Name *event = tox_events_add_conference_peer_name(events); + uint32_t conference_peer_name_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_CONFERENCE_PEER_NAME) { + ++conference_peer_name_size; + } + } + + return conference_peer_name_size; +} + +bool tox_event_conference_peer_name_unpack( + Tox_Event_Conference_Peer_Name **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_conference_peer_name_new(mem); + + 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; } @@ -227,20 +255,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 13bd8cbb..5cb4e676 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); @@ -102,7 +90,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); @@ -115,7 +115,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); @@ -131,90 +131,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 event->type == TOX_EVENT_CONFERENCE_TITLE ? event->data.conference_title : 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)); + + if (conference_title == nullptr) { 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)); - - if (new_conference_title == nullptr) { - return nullptr; - } - - events->conference_title = new_conference_title; - events->conference_title_capacity = new_conference_title_capacity; - } - - 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; + if (conference_title != nullptr) { + tox_event_conference_title_destruct(conference_title, mem); } - - for (uint32_t i = 0; i < events->conference_title_size; ++i) { - tox_event_conference_title_destruct(&events->conference_title[i]); - } - - 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]; -} - -bool tox_events_pack_conference_title(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_conference_title_size(events); + uint32_t conference_title_index = 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 (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 true; + + return nullptr; } -bool tox_events_unpack_conference_title(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_conference_title_size(const Tox_Events *events) { - Tox_Event_Conference_Title *event = tox_events_add_conference_title(events); + uint32_t conference_title_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_CONFERENCE_TITLE) { + ++conference_title_size; + } + } + + return conference_title_size; +} + +bool tox_event_conference_title_unpack( + Tox_Event_Conference_Title **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_conference_title_new(mem); + + 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; } @@ -225,20 +255,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 ba9c5773..d470351d 100644 --- a/toxcore/events/events_alloc.c +++ b/toxcore/events/events_alloc.c @@ -13,13 +13,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; } - state->events = (Tox_Events *)calloc(1, sizeof(Tox_Events)); + state->events = (Tox_Events *)mem_alloc(state->mem, sizeof(Tox_Events)); if (state->events == nullptr) { // It's still null => allocation failed. @@ -28,6 +29,7 @@ Tox_Events_State *tox_events_alloc(void *user_data) *state->events = (Tox_Events) { nullptr }; + state->events->mem = state->mem; } return state; @@ -39,44 +41,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); - tox_events_clear_group_peer_name(events); - tox_events_clear_group_peer_status(events); - tox_events_clear_group_topic(events); - tox_events_clear_group_privacy_state(events); - tox_events_clear_group_voice_state(events); - tox_events_clear_group_topic_lock(events); - tox_events_clear_group_peer_limit(events); - tox_events_clear_group_password(events); - tox_events_clear_group_message(events); - tox_events_clear_group_private_message(events); - tox_events_clear_group_custom_packet(events); - tox_events_clear_group_custom_private_packet(events); - tox_events_clear_group_invite(events); - tox_events_clear_group_peer_join(events); - tox_events_clear_group_peer_exit(events); - tox_events_clear_group_self_join(events); - tox_events_clear_group_join_fail(events); - tox_events_clear_group_moderation(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 769c1194..90b6f9fc 100644 --- a/toxcore/events/events_alloc.h +++ b/toxcore/events/events_alloc.h @@ -8,172 +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; - - Tox_Event_Group_Peer_Name *group_peer_name; - uint32_t group_peer_name_size; - uint32_t group_peer_name_capacity; - - Tox_Event_Group_Peer_Status *group_peer_status; - uint32_t group_peer_status_size; - uint32_t group_peer_status_capacity; - - Tox_Event_Group_Topic *group_topic; - uint32_t group_topic_size; - uint32_t group_topic_capacity; - - Tox_Event_Group_Privacy_State *group_privacy_state; - uint32_t group_privacy_state_size; - uint32_t group_privacy_state_capacity; - - Tox_Event_Group_Voice_State *group_voice_state; - uint32_t group_voice_state_size; - uint32_t group_voice_state_capacity; - - Tox_Event_Group_Topic_Lock *group_topic_lock; - uint32_t group_topic_lock_size; - uint32_t group_topic_lock_capacity; - - Tox_Event_Group_Peer_Limit *group_peer_limit; - uint32_t group_peer_limit_size; - uint32_t group_peer_limit_capacity; - - Tox_Event_Group_Password *group_password; - uint32_t group_password_size; - uint32_t group_password_capacity; - - Tox_Event_Group_Message *group_message; - uint32_t group_message_size; - uint32_t group_message_capacity; - - Tox_Event_Group_Private_Message *group_private_message; - uint32_t group_private_message_size; - uint32_t group_private_message_capacity; - - Tox_Event_Group_Custom_Packet *group_custom_packet; - uint32_t group_custom_packet_size; - uint32_t group_custom_packet_capacity; - - Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet; - uint32_t group_custom_private_packet_size; - uint32_t group_custom_private_packet_capacity; - - Tox_Event_Group_Invite *group_invite; - uint32_t group_invite_size; - uint32_t group_invite_capacity; - - Tox_Event_Group_Peer_Join *group_peer_join; - uint32_t group_peer_join_size; - uint32_t group_peer_join_capacity; - - Tox_Event_Group_Peer_Exit *group_peer_exit; - uint32_t group_peer_exit_size; - uint32_t group_peer_exit_capacity; - - Tox_Event_Group_Self_Join *group_self_join; - uint32_t group_self_join_size; - uint32_t group_self_join_capacity; - - Tox_Event_Group_Join_Fail *group_join_fail; - uint32_t group_join_fail_size; - uint32_t group_join_fail_capacity; - - Tox_Event_Group_Moderation *group_moderation; - uint32_t group_moderation_size; - uint32_t group_moderation_capacity; + const Memory *mem; }; typedef struct Tox_Events_State { Tox_Err_Events_Iterate error; + const Memory *mem; Tox_Events *events; } Tox_Events_State; @@ -217,142 +68,18 @@ tox_group_self_join_cb tox_events_handle_group_self_join; tox_group_join_fail_cb tox_events_handle_group_join_fail; tox_group_moderation_cb tox_events_handle_group_moderation; -// non_null() -typedef void tox_events_clear_cb(Tox_Events *events); +non_null(2) nullable(1) +bool tox_events_pack(const Tox_Events *events, Bin_Pack *bp); -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; -tox_events_clear_cb tox_events_clear_group_peer_name; -tox_events_clear_cb tox_events_clear_group_peer_status; -tox_events_clear_cb tox_events_clear_group_topic; -tox_events_clear_cb tox_events_clear_group_privacy_state; -tox_events_clear_cb tox_events_clear_group_voice_state; -tox_events_clear_cb tox_events_clear_group_topic_lock; -tox_events_clear_cb tox_events_clear_group_peer_limit; -tox_events_clear_cb tox_events_clear_group_password; -tox_events_clear_cb tox_events_clear_group_message; -tox_events_clear_cb tox_events_clear_group_private_message; -tox_events_clear_cb tox_events_clear_group_custom_packet; -tox_events_clear_cb tox_events_clear_group_custom_private_packet; -tox_events_clear_cb tox_events_clear_group_invite; -tox_events_clear_cb tox_events_clear_group_peer_join; -tox_events_clear_cb tox_events_clear_group_peer_exit; -tox_events_clear_cb tox_events_clear_group_self_join; -tox_events_clear_cb tox_events_clear_group_join_fail; -tox_events_clear_cb tox_events_clear_group_moderation; - -// non_null() -typedef bool tox_events_pack_cb(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_group_peer_name; -tox_events_pack_cb tox_events_pack_group_peer_status; -tox_events_pack_cb tox_events_pack_group_topic; -tox_events_pack_cb tox_events_pack_group_privacy_state; -tox_events_pack_cb tox_events_pack_group_voice_state; -tox_events_pack_cb tox_events_pack_group_topic_lock; -tox_events_pack_cb tox_events_pack_group_peer_limit; -tox_events_pack_cb tox_events_pack_group_password; -tox_events_pack_cb tox_events_pack_group_message; -tox_events_pack_cb tox_events_pack_group_private_message; -tox_events_pack_cb tox_events_pack_group_custom_packet; -tox_events_pack_cb tox_events_pack_group_custom_private_packet; -tox_events_pack_cb tox_events_pack_group_invite; -tox_events_pack_cb tox_events_pack_group_peer_join; -tox_events_pack_cb tox_events_pack_group_peer_exit; -tox_events_pack_cb tox_events_pack_group_self_join; -tox_events_pack_cb tox_events_pack_group_join_fail; -tox_events_pack_cb tox_events_pack_group_moderation; - -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_group_peer_name; -tox_events_unpack_cb tox_events_unpack_group_peer_status; -tox_events_unpack_cb tox_events_unpack_group_topic; -tox_events_unpack_cb tox_events_unpack_group_privacy_state; -tox_events_unpack_cb tox_events_unpack_group_voice_state; -tox_events_unpack_cb tox_events_unpack_group_topic_lock; -tox_events_unpack_cb tox_events_unpack_group_peer_limit; -tox_events_unpack_cb tox_events_unpack_group_password; -tox_events_unpack_cb tox_events_unpack_group_message; -tox_events_unpack_cb tox_events_unpack_group_private_message; -tox_events_unpack_cb tox_events_unpack_group_custom_packet; -tox_events_unpack_cb tox_events_unpack_group_custom_private_packet; -tox_events_unpack_cb tox_events_unpack_group_invite; -tox_events_unpack_cb tox_events_unpack_group_peer_join; -tox_events_unpack_cb tox_events_unpack_group_peer_exit; -tox_events_unpack_cb tox_events_unpack_group_self_join; -tox_events_unpack_cb tox_events_unpack_group_join_fail; -tox_events_unpack_cb tox_events_unpack_group_moderation; - -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 b7c3042f..8734c6d9 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" @@ -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_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) @@ -82,7 +70,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; @@ -94,7 +83,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); @@ -108,7 +109,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); @@ -125,90 +126,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 event->type == TOX_EVENT_FILE_CHUNK_REQUEST ? event->data.file_chunk_request : 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)); + + if (file_chunk_request == nullptr) { 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)); - - if (new_file_chunk_request == nullptr) { - return nullptr; - } - - events->file_chunk_request = new_file_chunk_request; - events->file_chunk_request_capacity = new_file_chunk_request_capacity; - } - - 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; + if (file_chunk_request != nullptr) { + tox_event_file_chunk_request_destruct(file_chunk_request, mem); } - - for (uint32_t i = 0; i < events->file_chunk_request_size; ++i) { - tox_event_file_chunk_request_destruct(&events->file_chunk_request[i]); - } - - 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]; -} - -bool tox_events_pack_file_chunk_request(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_file_chunk_request_size(events); + 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 (!tox_event_file_chunk_request_pack(tox_events_get_file_chunk_request(events, i), bp)) { - return false; + 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 true; + + return nullptr; } -bool tox_events_unpack_file_chunk_request(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_file_chunk_request_size(const Tox_Events *events) { - Tox_Event_File_Chunk_Request *event = tox_events_add_file_chunk_request(events); + uint32_t file_chunk_request_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_FILE_CHUNK_REQUEST) { + ++file_chunk_request_size; + } + } + + return file_chunk_request_size; +} + +bool tox_event_file_chunk_request_unpack( + Tox_Event_File_Chunk_Request **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_file_chunk_request_new(mem); + + 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; } @@ -219,20 +250,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 a11132d4..5c211623 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); @@ -130,7 +118,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); @@ -145,7 +145,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); @@ -163,90 +163,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 event->type == TOX_EVENT_FILE_RECV ? event->data.file_recv : 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)); + + if (file_recv == nullptr) { 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)); - - if (new_file_recv == nullptr) { - return nullptr; - } - - events->file_recv = new_file_recv; - events->file_recv_capacity = new_file_recv_capacity; - } - - 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; + if (file_recv != nullptr) { + tox_event_file_recv_destruct(file_recv, mem); } - - for (uint32_t i = 0; i < events->file_recv_size; ++i) { - tox_event_file_recv_destruct(&events->file_recv[i]); - } - - 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]; -} - -bool tox_events_pack_file_recv(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_file_recv_size(events); + uint32_t file_recv_index = 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 (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 true; + + return nullptr; } -bool tox_events_unpack_file_recv(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_file_recv_size(const Tox_Events *events) { - Tox_Event_File_Recv *event = tox_events_add_file_recv(events); + uint32_t file_recv_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_FILE_RECV) { + ++file_recv_size; + } + } + + return file_recv_size; +} + +bool tox_event_file_recv_unpack( + Tox_Event_File_Recv **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_file_recv_new(mem); + + 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; } @@ -257,20 +287,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 aa9bcf1c..bec4b386 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); @@ -104,7 +92,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; @@ -116,7 +104,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); @@ -130,7 +130,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); @@ -147,90 +147,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 event->type == TOX_EVENT_FILE_RECV_CHUNK ? event->data.file_recv_chunk : 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)); + + if (file_recv_chunk == nullptr) { 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)); - - if (new_file_recv_chunk == nullptr) { - return nullptr; - } - - events->file_recv_chunk = new_file_recv_chunk; - events->file_recv_chunk_capacity = new_file_recv_chunk_capacity; - } - - 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; + if (file_recv_chunk != nullptr) { + tox_event_file_recv_chunk_destruct(file_recv_chunk, mem); } - - for (uint32_t i = 0; i < events->file_recv_chunk_size; ++i) { - tox_event_file_recv_chunk_destruct(&events->file_recv_chunk[i]); - } - - 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]; -} - -bool tox_events_pack_file_recv_chunk(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_file_recv_chunk_size(events); + 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 (!tox_event_file_recv_chunk_pack(tox_events_get_file_recv_chunk(events, i), bp)) { - return false; + 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 true; + + return nullptr; } -bool tox_events_unpack_file_recv_chunk(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_file_recv_chunk_size(const Tox_Events *events) { - Tox_Event_File_Recv_Chunk *event = tox_events_add_file_recv_chunk(events); + uint32_t file_recv_chunk_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_FILE_RECV_CHUNK) { + ++file_recv_chunk_size; + } + } + + return file_recv_chunk_size; +} + +bool tox_event_file_recv_chunk_unpack( + Tox_Event_File_Recv_Chunk **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_file_recv_chunk_new(mem); + + 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; } @@ -241,20 +271,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 c66a3109..b1a87f34 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" @@ -29,19 +29,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) @@ -82,7 +69,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); @@ -95,7 +94,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); @@ -105,96 +104,126 @@ static bool tox_event_file_recv_control_unpack( return bin_unpack_u32(bu, &event->friend_number) && bin_unpack_u32(bu, &event->file_number) - && tox_unpack_file_control(bu, &event->control); + && tox_file_control_unpack(bu, &event->control); } /***************************************************** * - * :: 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 event->type == TOX_EVENT_FILE_RECV_CONTROL ? event->data.file_recv_control : 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)); + + if (file_recv_control == nullptr) { 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)); - - if (new_file_recv_control == nullptr) { - return nullptr; - } - - events->file_recv_control = new_file_recv_control; - events->file_recv_control_capacity = new_file_recv_control_capacity; - } - - 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; + if (file_recv_control != nullptr) { + tox_event_file_recv_control_destruct(file_recv_control, mem); } - - for (uint32_t i = 0; i < events->file_recv_control_size; ++i) { - tox_event_file_recv_control_destruct(&events->file_recv_control[i]); - } - - 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]; -} - -bool tox_events_pack_file_recv_control(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_file_recv_control_size(events); + 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 (!tox_event_file_recv_control_pack(tox_events_get_file_recv_control(events, i), bp)) { - return false; + 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 true; + + return nullptr; } -bool tox_events_unpack_file_recv_control(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_file_recv_control_size(const Tox_Events *events) { - Tox_Event_File_Recv_Control *event = tox_events_add_file_recv_control(events); + uint32_t file_recv_control_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_FILE_RECV_CONTROL) { + ++file_recv_control_size; + } + } + + return file_recv_control_size; +} + +bool tox_event_file_recv_control_unpack( + Tox_Event_File_Recv_Control **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_file_recv_control_new(mem); + + 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; } @@ -205,20 +234,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 230428da..64553b42 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,6 +28,32 @@ struct Tox_Event_Friend_Connection_Status { Tox_Connection connection_status; }; +non_null() +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) +{ + 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) +{ + 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) +{ + assert(friend_connection_status != nullptr); + return friend_connection_status->connection_status; +} + non_null() static void tox_event_friend_connection_status_construct(Tox_Event_Friend_Connection_Status *friend_connection_status) { @@ -36,41 +62,12 @@ static void tox_event_friend_connection_status_construct(Tox_Event_Friend_Connec }; } non_null() -static void tox_event_friend_connection_status_destruct(Tox_Event_Friend_Connection_Status *friend_connection_status) +static void tox_event_friend_connection_status_destruct(Tox_Event_Friend_Connection_Status *friend_connection_status, const Memory *mem) { 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) -{ - 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) -{ - 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) -{ - 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) -{ - assert(friend_connection_status != nullptr); - return friend_connection_status->connection_status; -} - -non_null() -static bool tox_event_friend_connection_status_pack( +bool tox_event_friend_connection_status_pack( const Tox_Event_Friend_Connection_Status *event, Bin_Pack *bp) { assert(event != nullptr); @@ -82,7 +79,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); @@ -91,98 +88,126 @@ static bool tox_event_friend_connection_status_unpack( } return bin_unpack_u32(bu, &event->friend_number) - && tox_unpack_connection(bu, &event->connection_status); + && tox_connection_unpack(bu, &event->connection_status); } /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Friend_Connection_Status *tox_events_add_friend_connection_status(Tox_Events *events) +const Tox_Event_Friend_Connection_Status *tox_event_get_friend_connection_status(const Tox_Event *event) { - if (events->friend_connection_status_size == UINT32_MAX) { + return event->type == TOX_EVENT_FRIEND_CONNECTION_STATUS ? event->data.friend_connection_status : nullptr; +} + +Tox_Event_Friend_Connection_Status *tox_event_friend_connection_status_new(const Memory *mem) +{ + 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)); - - if (new_friend_connection_status == nullptr) { - return nullptr; - } - - events->friend_connection_status = new_friend_connection_status; - events->friend_connection_status_capacity = new_friend_connection_status_capacity; - } - - 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; return friend_connection_status; } -void tox_events_clear_friend_connection_status(Tox_Events *events) +void tox_event_friend_connection_status_free(Tox_Event_Friend_Connection_Status *friend_connection_status, const Memory *mem) { - if (events == nullptr) { - return; + if (friend_connection_status != nullptr) { + tox_event_friend_connection_status_destruct(friend_connection_status, mem); + } + mem_delete(mem, friend_connection_status); +} + +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); + + if (friend_connection_status == nullptr) { + return nullptr; } - for (uint32_t i = 0; i < events->friend_connection_status_size; ++i) { - tox_event_friend_connection_status_destruct(&events->friend_connection_status[i]); + 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; +} + +const Tox_Event_Friend_Connection_Status *tox_events_get_friend_connection_status(const Tox_Events *events, uint32_t index) +{ + uint32_t friend_connection_status_index = 0; + const uint32_t size = tox_events_get_size(events); + + 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; - } - - return events->friend_connection_status_size; -} - -const Tox_Event_Friend_Connection_Status *tox_events_get_friend_connection_status(const Tox_Events *events, - uint32_t index) -{ - assert(index < events->friend_connection_status_size); - assert(events->friend_connection_status != nullptr); - return &events->friend_connection_status[index]; -} - -bool tox_events_pack_friend_connection_status(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_connection_status_size(events); + 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 (!tox_event_friend_connection_status_pack(tox_events_get_friend_connection_status(events, i), bp)) { - return false; + if (events->events[i].type == TOX_EVENT_FRIEND_CONNECTION_STATUS) { + ++friend_connection_status_size; } } - return true; + + return friend_connection_status_size; } -bool tox_events_unpack_friend_connection_status(Tox_Events *events, Bin_Unpack *bu) +bool tox_event_friend_connection_status_unpack( + Tox_Event_Friend_Connection_Status **event, Bin_Unpack *bu, const Memory *mem) { - Tox_Event_Friend_Connection_Status *event = tox_events_add_friend_connection_status(events); + assert(event != nullptr); + *event = tox_event_friend_connection_status_new(mem); - if (event == nullptr) { + if (*event == nullptr) { return false; } - return tox_event_friend_connection_status_unpack(event, bu); + return tox_event_friend_connection_status_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Friend_Connection_Status *tox_event_friend_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_Friend_Connection_Status *friend_connection_status = tox_events_add_friend_connection_status(state->events, state->mem); + + if (friend_connection_status == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_connection_status; } @@ -196,17 +221,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 0fd26051..1999d4cb 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; @@ -89,7 +76,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); @@ -101,7 +100,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); @@ -116,91 +115,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 event->type == TOX_EVENT_FRIEND_LOSSLESS_PACKET ? event->data.friend_lossless_packet : 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)); + + if (friend_lossless_packet == nullptr) { 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)); - - if (new_friend_lossless_packet == nullptr) { - return nullptr; - } - - events->friend_lossless_packet = new_friend_lossless_packet; - events->friend_lossless_packet_capacity = new_friend_lossless_packet_capacity; - } - - 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; + if (friend_lossless_packet != nullptr) { + tox_event_friend_lossless_packet_destruct(friend_lossless_packet, mem); } - - for (uint32_t i = 0; i < events->friend_lossless_packet_size; ++i) { - tox_event_friend_lossless_packet_destruct(&events->friend_lossless_packet[i]); - } - - 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]; -} - -bool tox_events_pack_friend_lossless_packet(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_lossless_packet_size(events); + 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 (!tox_event_friend_lossless_packet_pack(tox_events_get_friend_lossless_packet(events, i), bp)) { - return false; + 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 true; + + return nullptr; } -bool tox_events_unpack_friend_lossless_packet(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_friend_lossless_packet_size(const Tox_Events *events) { - Tox_Event_Friend_Lossless_Packet *event = tox_events_add_friend_lossless_packet(events); + uint32_t friend_lossless_packet_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_FRIEND_LOSSLESS_PACKET) { + ++friend_lossless_packet_size; + } + } + + return friend_lossless_packet_size; +} + +bool tox_event_friend_lossless_packet_unpack( + Tox_Event_Friend_Lossless_Packet **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_friend_lossless_packet_new(mem); + + 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; } @@ -214,17 +242,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 a6639ee6..010bd938 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) @@ -88,7 +76,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); @@ -100,7 +100,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); @@ -115,91 +115,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 event->type == TOX_EVENT_FRIEND_LOSSY_PACKET ? event->data.friend_lossy_packet : 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)); + + if (friend_lossy_packet == nullptr) { 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)); - - if (new_friend_lossy_packet == nullptr) { - return nullptr; - } - - events->friend_lossy_packet = new_friend_lossy_packet; - events->friend_lossy_packet_capacity = new_friend_lossy_packet_capacity; - } - - 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; + if (friend_lossy_packet != nullptr) { + tox_event_friend_lossy_packet_destruct(friend_lossy_packet, mem); } - - for (uint32_t i = 0; i < events->friend_lossy_packet_size; ++i) { - tox_event_friend_lossy_packet_destruct(&events->friend_lossy_packet[i]); - } - - 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]; -} - -bool tox_events_pack_friend_lossy_packet(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_lossy_packet_size(events); + 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 (!tox_event_friend_lossy_packet_pack(tox_events_get_friend_lossy_packet(events, i), bp)) { - return false; + 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 true; + + return nullptr; } -bool tox_events_unpack_friend_lossy_packet(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_friend_lossy_packet_size(const Tox_Events *events) { - Tox_Event_Friend_Lossy_Packet *event = tox_events_add_friend_lossy_packet(events); + uint32_t friend_lossy_packet_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_FRIEND_LOSSY_PACKET) { + ++friend_lossy_packet_size; + } + } + + return friend_lossy_packet_size; +} + +bool tox_event_friend_lossy_packet_unpack( + Tox_Event_Friend_Lossy_Packet **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_friend_lossy_packet_new(mem); + + 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; } @@ -213,17 +242,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 a66483fe..8a0fc555 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); @@ -102,7 +90,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); @@ -115,7 +115,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); @@ -124,97 +124,127 @@ static bool tox_event_friend_message_unpack( } return bin_unpack_u32(bu, &event->friend_number) - && tox_unpack_message_type(bu, &event->type) + && tox_message_type_unpack(bu, &event->type) && bin_unpack_bin(bu, &event->message, &event->message_length); } /***************************************************** * - * :: 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 event->type == TOX_EVENT_FRIEND_MESSAGE ? event->data.friend_message : 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)); + + if (friend_message == nullptr) { 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)); - - if (new_friend_message == nullptr) { - return nullptr; - } - - events->friend_message = new_friend_message; - events->friend_message_capacity = new_friend_message_capacity; - } - - 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; + if (friend_message != nullptr) { + tox_event_friend_message_destruct(friend_message, mem); } - - for (uint32_t i = 0; i < events->friend_message_size; ++i) { - tox_event_friend_message_destruct(&events->friend_message[i]); - } - - 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]; -} - -bool tox_events_pack_friend_message(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_message_size(events); + uint32_t friend_message_index = 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 (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 true; + + return nullptr; } -bool tox_events_unpack_friend_message(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_friend_message_size(const Tox_Events *events) { - Tox_Event_Friend_Message *event = tox_events_add_friend_message(events); + uint32_t friend_message_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_FRIEND_MESSAGE) { + ++friend_message_size; + } + } + + return friend_message_size; +} + +bool tox_event_friend_message_unpack( + Tox_Event_Friend_Message **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_friend_message_new(mem); + + 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; } @@ -225,20 +255,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 364ddcf9..a526acb5 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); @@ -88,7 +76,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); @@ -100,7 +100,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); @@ -115,90 +115,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 event->type == TOX_EVENT_FRIEND_NAME ? event->data.friend_name : 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)); + + if (friend_name == nullptr) { 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)); - - if (new_friend_name == nullptr) { - return nullptr; - } - - events->friend_name = new_friend_name; - events->friend_name_capacity = new_friend_name_capacity; - } - - 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; + if (friend_name != nullptr) { + tox_event_friend_name_destruct(friend_name, mem); } - - for (uint32_t i = 0; i < events->friend_name_size; ++i) { - tox_event_friend_name_destruct(&events->friend_name[i]); - } - - 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]; -} - -bool tox_events_pack_friend_name(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_name_size(events); + uint32_t friend_name_index = 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 (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 true; + + return nullptr; } -bool tox_events_unpack_friend_name(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_friend_name_size(const Tox_Events *events) { - Tox_Event_Friend_Name *event = tox_events_add_friend_name(events); + uint32_t friend_name_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_FRIEND_NAME) { + ++friend_name_size; + } + } + + return friend_name_size; +} + +bool tox_event_friend_name_unpack( + Tox_Event_Friend_Name **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_friend_name_new(mem); + + 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; } @@ -210,19 +240,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 dee1aa47..23d2ff32 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" @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -27,19 +28,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) @@ -67,7 +55,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); @@ -79,7 +79,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); @@ -94,91 +94,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 event->type == TOX_EVENT_FRIEND_READ_RECEIPT ? event->data.friend_read_receipt : 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)); + + if (friend_read_receipt == nullptr) { 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)); - - if (new_friend_read_receipt == nullptr) { - return nullptr; - } - - events->friend_read_receipt = new_friend_read_receipt; - events->friend_read_receipt_capacity = new_friend_read_receipt_capacity; - } - - 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]; -} - -bool tox_events_pack_friend_read_receipt(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_read_receipt_size(events); + 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 (!tox_event_friend_read_receipt_pack(tox_events_get_friend_read_receipt(events, i), bp)) { - return false; + 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 true; + + return nullptr; } -bool tox_events_unpack_friend_read_receipt(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_friend_read_receipt_size(const Tox_Events *events) { - Tox_Event_Friend_Read_Receipt *event = tox_events_add_friend_read_receipt(events); + uint32_t friend_read_receipt_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_FRIEND_READ_RECEIPT) { + ++friend_read_receipt_size; + } + } + + return friend_read_receipt_size; +} + +bool tox_event_friend_read_receipt_unpack( + Tox_Event_Friend_Read_Receipt **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_friend_read_receipt_new(mem); + + 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; } @@ -189,19 +218,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 e7ecf678..2b9424e5 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,24 +44,24 @@ 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; } - friend_request->message = (uint8_t *)malloc(message_length); + friend_request->message = (uint8_t *)mem_balloc(mem, message_length * sizeof(uint8_t)); if (friend_request->message == nullptr) { return false; } - memcpy(friend_request->message, message, message_length); + memcpy(friend_request->message, message, message_length * sizeof(uint8_t)); friend_request->message_length = message_length; return true; } @@ -89,7 +77,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); @@ -101,7 +101,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); @@ -113,93 +113,119 @@ static bool tox_event_friend_request_unpack( && bin_unpack_bin(bu, &event->message, &event->message_length); } - -/***************************************************** - * - * :: add/clear/get - * - *****************************************************/ - - -non_null() -static Tox_Event_Friend_Request *tox_events_add_friend_request(Tox_Events *events) +const Tox_Event_Friend_Request *tox_event_get_friend_request( + const Tox_Event *event) { - if (events->friend_request_size == UINT32_MAX) { + return event->type == TOX_EVENT_FRIEND_REQUEST ? event->data.friend_request : nullptr; +} + +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; } - 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)); - - if (new_friend_request == nullptr) { - return nullptr; - } - - events->friend_request = new_friend_request; - events->friend_request_capacity = new_friend_request_capacity; - } - - 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; } -void tox_events_clear_friend_request(Tox_Events *events) +void tox_event_friend_request_free(Tox_Event_Friend_Request *friend_request, const Memory *mem) { - if (events == nullptr) { - return; + if (friend_request != nullptr) { + tox_event_friend_request_destruct(friend_request, mem); } - - for (uint32_t i = 0; i < events->friend_request_size; ++i) { - tox_event_friend_request_destruct(&events->friend_request[i]); - } - - free(events->friend_request); - events->friend_request = nullptr; - events->friend_request_size = 0; - events->friend_request_capacity = 0; + mem_delete(mem, friend_request); } -uint32_t tox_events_get_friend_request_size(const Tox_Events *events) +non_null() +static Tox_Event_Friend_Request *tox_events_add_friend_request(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Friend_Request *const friend_request = tox_event_friend_request_new(mem); + + if (friend_request == nullptr) { + return nullptr; } - return events->friend_request_size; + 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) +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]; -} - -bool tox_events_pack_friend_request(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_request_size(events); + uint32_t friend_request_index = 0; + const uint32_t size = tox_events_get_size(events); 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 (friend_request_index > index) { + return nullptr; + } + + 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; } } - return true; + + return nullptr; } -bool tox_events_unpack_friend_request(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_friend_request_size( + const Tox_Events *events) { - Tox_Event_Friend_Request *event = tox_events_add_friend_request(events); + uint32_t friend_request_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_FRIEND_REQUEST) { + ++friend_request_size; + } + } + + return friend_request_size; +} + +bool tox_event_friend_request_unpack( + Tox_Event_Friend_Request **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_friend_request_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_friend_request_unpack(event, bu); + return tox_event_friend_request_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Friend_Request *tox_event_friend_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_Friend_Request *friend_request = tox_events_add_friend_request(state->events, state->mem); + + if (friend_request == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return friend_request; } @@ -213,20 +239,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 75c7d35c..1d4b65b3 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" @@ -28,19 +28,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) @@ -68,7 +55,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); @@ -80,7 +79,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); @@ -89,96 +88,126 @@ static bool tox_event_friend_status_unpack( } return bin_unpack_u32(bu, &event->friend_number) - && tox_unpack_user_status(bu, &event->status); + && tox_user_status_unpack(bu, &event->status); } /***************************************************** * - * :: 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 event->type == TOX_EVENT_FRIEND_STATUS ? event->data.friend_status : 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)); + + if (friend_status == nullptr) { 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)); - - if (new_friend_status == nullptr) { - return nullptr; - } - - events->friend_status = new_friend_status; - events->friend_status_capacity = new_friend_status_capacity; - } - - 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; + if (friend_status != nullptr) { + tox_event_friend_status_destruct(friend_status, mem); } - - for (uint32_t i = 0; i < events->friend_status_size; ++i) { - tox_event_friend_status_destruct(&events->friend_status[i]); - } - - 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]; -} - -bool tox_events_pack_friend_status(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_status_size(events); + uint32_t friend_status_index = 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 (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 true; + + return nullptr; } -bool tox_events_unpack_friend_status(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_friend_status_size(const Tox_Events *events) { - Tox_Event_Friend_Status *event = tox_events_add_friend_status(events); + uint32_t friend_status_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_FRIEND_STATUS) { + ++friend_status_size; + } + } + + return friend_status_size; +} + +bool tox_event_friend_status_unpack( + Tox_Event_Friend_Status **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_friend_status_new(mem); + + 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; } @@ -190,19 +219,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 c54d154e..e2613f75 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) @@ -76,21 +64,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); @@ -102,7 +100,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); @@ -117,91 +115,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 event->type == TOX_EVENT_FRIEND_STATUS_MESSAGE ? event->data.friend_status_message : 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)); + + if (friend_status_message == nullptr) { 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)); - - if (new_friend_status_message == nullptr) { - return nullptr; - } - - events->friend_status_message = new_friend_status_message; - events->friend_status_message_capacity = new_friend_status_message_capacity; - } - - 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; + if (friend_status_message != nullptr) { + tox_event_friend_status_message_destruct(friend_status_message, mem); } - - for (uint32_t i = 0; i < events->friend_status_message_size; ++i) { - tox_event_friend_status_message_destruct(&events->friend_status_message[i]); - } - - 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]; -} - -bool tox_events_pack_friend_status_message(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_status_message_size(events); + 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 (!tox_event_friend_status_message_pack(tox_events_get_friend_status_message(events, i), bp)) { - return false; + 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 true; + + return nullptr; } -bool tox_events_unpack_friend_status_message(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_friend_status_message_size(const Tox_Events *events) { - Tox_Event_Friend_Status_Message *event = tox_events_add_friend_status_message(events); + uint32_t friend_status_message_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_FRIEND_STATUS_MESSAGE) { + ++friend_status_message_size; + } + } + + return friend_status_message_size; +} + +bool tox_event_friend_status_message_unpack( + Tox_Event_Friend_Status_Message **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_friend_status_message_new(mem); + + 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; } @@ -212,20 +239,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 4a05abc1..ffbe44b1 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" @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../tox.h" #include "../tox_events.h" +#include "../tox_unpack.h" /***************************************************** @@ -27,19 +28,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) @@ -54,7 +42,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; @@ -66,7 +55,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); @@ -78,7 +79,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); @@ -93,90 +94,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 event->type == TOX_EVENT_FRIEND_TYPING ? event->data.friend_typing : 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)); + + if (friend_typing == nullptr) { 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)); - - if (new_friend_typing == nullptr) { - return nullptr; - } - - events->friend_typing = new_friend_typing; - events->friend_typing_capacity = new_friend_typing_capacity; - } - - 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; + if (friend_typing != nullptr) { + tox_event_friend_typing_destruct(friend_typing, mem); } - - for (uint32_t i = 0; i < events->friend_typing_size; ++i) { - tox_event_friend_typing_destruct(&events->friend_typing[i]); - } - - 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]; -} - -bool tox_events_pack_friend_typing(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_friend_typing_size(events); + uint32_t friend_typing_index = 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 (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 true; + + return nullptr; } -bool tox_events_unpack_friend_typing(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_friend_typing_size(const Tox_Events *events) { - Tox_Event_Friend_Typing *event = tox_events_add_friend_typing(events); + uint32_t friend_typing_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_FRIEND_TYPING) { + ++friend_typing_size; + } + } + + return friend_typing_size; +} + +bool tox_event_friend_typing_unpack( + Tox_Event_Friend_Typing **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_friend_typing_new(mem); + + 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; } @@ -187,19 +218,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/group_custom_packet.c b/toxcore/events/group_custom_packet.c index 8bfc8192..fb29dfee 100644 --- a/toxcore/events/group_custom_packet.c +++ b/toxcore/events/group_custom_packet.c @@ -30,19 +30,6 @@ struct Tox_Event_Group_Custom_Packet { uint32_t data_length; }; -non_null() -static void tox_event_group_custom_packet_construct(Tox_Event_Group_Custom_Packet *group_custom_packet) -{ - *group_custom_packet = (Tox_Event_Group_Custom_Packet) { - 0 - }; -} -non_null() -static void tox_event_group_custom_packet_destruct(Tox_Event_Group_Custom_Packet *group_custom_packet) -{ - free(group_custom_packet->data); -} - non_null() static void tox_event_group_custom_packet_set_group_number(Tox_Event_Group_Custom_Packet *group_custom_packet, uint32_t group_number) @@ -91,7 +78,7 @@ static bool tox_event_group_custom_packet_set_data(Tox_Event_Group_Custom_Packet group_custom_packet->data_length = data_length; return true; } -size_t tox_event_group_custom_packet_get_data_length(const Tox_Event_Group_Custom_Packet *group_custom_packet) +uint32_t tox_event_group_custom_packet_get_data_length(const Tox_Event_Group_Custom_Packet *group_custom_packet) { assert(group_custom_packet != nullptr); return group_custom_packet->data_length; @@ -103,7 +90,19 @@ const uint8_t *tox_event_group_custom_packet_get_data(const Tox_Event_Group_Cust } non_null() -static bool tox_event_group_custom_packet_pack( +static void tox_event_group_custom_packet_construct(Tox_Event_Group_Custom_Packet *group_custom_packet) +{ + *group_custom_packet = (Tox_Event_Group_Custom_Packet) { + 0 + }; +} +non_null() +static void tox_event_group_custom_packet_destruct(Tox_Event_Group_Custom_Packet *group_custom_packet, const Memory *mem) +{ + free(group_custom_packet->data); +} + +bool tox_event_group_custom_packet_pack( const Tox_Event_Group_Custom_Packet *event, Bin_Pack *bp) { assert(event != nullptr); @@ -116,7 +115,7 @@ static bool tox_event_group_custom_packet_pack( } non_null() -static bool tox_event_group_custom_packet_unpack( +static bool tox_event_group_custom_packet_unpack_into( Tox_Event_Group_Custom_Packet *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -132,93 +131,120 @@ static bool tox_event_group_custom_packet_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Custom_Packet *tox_events_add_group_custom_packet(Tox_Events *events) +const Tox_Event_Group_Custom_Packet *tox_event_get_group_custom_packet(const Tox_Event *event) { - if (events->group_custom_packet_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_CUSTOM_PACKET ? event->data.group_custom_packet : nullptr; +} + +Tox_Event_Group_Custom_Packet *tox_event_group_custom_packet_new(const Memory *mem) +{ + Tox_Event_Group_Custom_Packet *const group_custom_packet = + (Tox_Event_Group_Custom_Packet *)mem_alloc(mem, sizeof(Tox_Event_Group_Custom_Packet)); + + if (group_custom_packet == nullptr) { return nullptr; } - if (events->group_custom_packet_size == events->group_custom_packet_capacity) { - const uint32_t new_group_custom_packet_capacity = events->group_custom_packet_capacity * 2 + 1; - Tox_Event_Group_Custom_Packet *new_group_custom_packet = (Tox_Event_Group_Custom_Packet *) - realloc( - events->group_custom_packet, - new_group_custom_packet_capacity * sizeof(Tox_Event_Group_Custom_Packet)); - - if (new_group_custom_packet == nullptr) { - return nullptr; - } - - events->group_custom_packet = new_group_custom_packet; - events->group_custom_packet_capacity = new_group_custom_packet_capacity; - } - - Tox_Event_Group_Custom_Packet *const group_custom_packet = - &events->group_custom_packet[events->group_custom_packet_size]; tox_event_group_custom_packet_construct(group_custom_packet); - ++events->group_custom_packet_size; return group_custom_packet; } -void tox_events_clear_group_custom_packet(Tox_Events *events) +void tox_event_group_custom_packet_free(Tox_Event_Group_Custom_Packet *group_custom_packet, const Memory *mem) { - if (events == nullptr) { - return; + if (group_custom_packet != nullptr) { + tox_event_group_custom_packet_destruct(group_custom_packet, mem); } - - for (uint32_t i = 0; i < events->group_custom_packet_size; ++i) { - tox_event_group_custom_packet_destruct(&events->group_custom_packet[i]); - } - - free(events->group_custom_packet); - events->group_custom_packet = nullptr; - events->group_custom_packet_size = 0; - events->group_custom_packet_capacity = 0; + mem_delete(mem, group_custom_packet); } -uint32_t tox_events_get_group_custom_packet_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Custom_Packet *tox_events_add_group_custom_packet(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Custom_Packet *const group_custom_packet = tox_event_group_custom_packet_new(mem); + + if (group_custom_packet == nullptr) { + return nullptr; } - return events->group_custom_packet_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_CUSTOM_PACKET; + event.data.group_custom_packet = group_custom_packet; + + tox_events_add(events, &event); + return group_custom_packet; } const Tox_Event_Group_Custom_Packet *tox_events_get_group_custom_packet(const Tox_Events *events, uint32_t index) { - assert(index < events->group_custom_packet_size); - assert(events->group_custom_packet != nullptr); - return &events->group_custom_packet[index]; -} - -bool tox_events_pack_group_custom_packet(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_custom_packet_size(events); + uint32_t group_custom_packet_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_custom_packet_pack(tox_events_get_group_custom_packet(events, i), bp)) { - return false; + if (group_custom_packet_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_CUSTOM_PACKET) { + const Tox_Event_Group_Custom_Packet *group_custom_packet = events->events[i].data.group_custom_packet; + if (group_custom_packet_index == index) { + return group_custom_packet; + } + ++group_custom_packet_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_custom_packet(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_custom_packet_size(const Tox_Events *events) { - Tox_Event_Group_Custom_Packet *event = tox_events_add_group_custom_packet(events); + uint32_t group_custom_packet_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_CUSTOM_PACKET) { + ++group_custom_packet_size; + } + } + + return group_custom_packet_size; +} + +bool tox_event_group_custom_packet_unpack( + Tox_Event_Group_Custom_Packet **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_custom_packet_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_custom_packet_unpack(event, bu); + return tox_event_group_custom_packet_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Custom_Packet *tox_event_group_custom_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_Group_Custom_Packet *group_custom_packet = tox_events_add_group_custom_packet(state->events, state->mem); + + if (group_custom_packet == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_custom_packet; } @@ -232,17 +258,9 @@ bool tox_events_unpack_group_custom_packet(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_custom_packet(Tox *tox, uint32_t group_number, uint32_t peer_id, 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_Group_Custom_Packet *group_custom_packet = tox_events_add_group_custom_packet(state->events); + Tox_Event_Group_Custom_Packet *group_custom_packet = tox_event_group_custom_packet_alloc(user_data); if (group_custom_packet == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_custom_private_packet.c b/toxcore/events/group_custom_private_packet.c index 2d752593..12c5725c 100644 --- a/toxcore/events/group_custom_private_packet.c +++ b/toxcore/events/group_custom_private_packet.c @@ -30,19 +30,6 @@ struct Tox_Event_Group_Custom_Private_Packet { uint32_t data_length; }; -non_null() -static void tox_event_group_custom_private_packet_construct(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet) -{ - *group_custom_private_packet = (Tox_Event_Group_Custom_Private_Packet) { - 0 - }; -} -non_null() -static void tox_event_group_custom_private_packet_destruct(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet) -{ - free(group_custom_private_packet->data); -} - non_null() static void tox_event_group_custom_private_packet_set_group_number(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet, uint32_t group_number) @@ -91,7 +78,7 @@ static bool tox_event_group_custom_private_packet_set_data(Tox_Event_Group_Custo group_custom_private_packet->data_length = data_length; return true; } -size_t tox_event_group_custom_private_packet_get_data_length(const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet) +uint32_t tox_event_group_custom_private_packet_get_data_length(const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet) { assert(group_custom_private_packet != nullptr); return group_custom_private_packet->data_length; @@ -103,7 +90,19 @@ const uint8_t *tox_event_group_custom_private_packet_get_data(const Tox_Event_Gr } non_null() -static bool tox_event_group_custom_private_packet_pack( +static void tox_event_group_custom_private_packet_construct(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet) +{ + *group_custom_private_packet = (Tox_Event_Group_Custom_Private_Packet) { + 0 + }; +} +non_null() +static void tox_event_group_custom_private_packet_destruct(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet, const Memory *mem) +{ + free(group_custom_private_packet->data); +} + +bool tox_event_group_custom_private_packet_pack( const Tox_Event_Group_Custom_Private_Packet *event, Bin_Pack *bp) { assert(event != nullptr); @@ -116,7 +115,7 @@ static bool tox_event_group_custom_private_packet_pack( } non_null() -static bool tox_event_group_custom_private_packet_unpack( +static bool tox_event_group_custom_private_packet_unpack_into( Tox_Event_Group_Custom_Private_Packet *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -132,93 +131,120 @@ static bool tox_event_group_custom_private_packet_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Custom_Private_Packet *tox_events_add_group_custom_private_packet(Tox_Events *events) +const Tox_Event_Group_Custom_Private_Packet *tox_event_get_group_custom_private_packet(const Tox_Event *event) { - if (events->group_custom_private_packet_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET ? event->data.group_custom_private_packet : nullptr; +} + +Tox_Event_Group_Custom_Private_Packet *tox_event_group_custom_private_packet_new(const Memory *mem) +{ + Tox_Event_Group_Custom_Private_Packet *const group_custom_private_packet = + (Tox_Event_Group_Custom_Private_Packet *)mem_alloc(mem, sizeof(Tox_Event_Group_Custom_Private_Packet)); + + if (group_custom_private_packet == nullptr) { return nullptr; } - if (events->group_custom_private_packet_size == events->group_custom_private_packet_capacity) { - const uint32_t new_group_custom_private_packet_capacity = events->group_custom_private_packet_capacity * 2 + 1; - Tox_Event_Group_Custom_Private_Packet *new_group_custom_private_packet = (Tox_Event_Group_Custom_Private_Packet *) - realloc( - events->group_custom_private_packet, - new_group_custom_private_packet_capacity * sizeof(Tox_Event_Group_Custom_Private_Packet)); - - if (new_group_custom_private_packet == nullptr) { - return nullptr; - } - - events->group_custom_private_packet = new_group_custom_private_packet; - events->group_custom_private_packet_capacity = new_group_custom_private_packet_capacity; - } - - Tox_Event_Group_Custom_Private_Packet *const group_custom_private_packet = - &events->group_custom_private_packet[events->group_custom_private_packet_size]; tox_event_group_custom_private_packet_construct(group_custom_private_packet); - ++events->group_custom_private_packet_size; return group_custom_private_packet; } -void tox_events_clear_group_custom_private_packet(Tox_Events *events) +void tox_event_group_custom_private_packet_free(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet, const Memory *mem) { - if (events == nullptr) { - return; + if (group_custom_private_packet != nullptr) { + tox_event_group_custom_private_packet_destruct(group_custom_private_packet, mem); } - - for (uint32_t i = 0; i < events->group_custom_private_packet_size; ++i) { - tox_event_group_custom_private_packet_destruct(&events->group_custom_private_packet[i]); - } - - free(events->group_custom_private_packet); - events->group_custom_private_packet = nullptr; - events->group_custom_private_packet_size = 0; - events->group_custom_private_packet_capacity = 0; + mem_delete(mem, group_custom_private_packet); } -uint32_t tox_events_get_group_custom_private_packet_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Custom_Private_Packet *tox_events_add_group_custom_private_packet(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Custom_Private_Packet *const group_custom_private_packet = tox_event_group_custom_private_packet_new(mem); + + if (group_custom_private_packet == nullptr) { + return nullptr; } - return events->group_custom_private_packet_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET; + event.data.group_custom_private_packet = group_custom_private_packet; + + tox_events_add(events, &event); + return group_custom_private_packet; } const Tox_Event_Group_Custom_Private_Packet *tox_events_get_group_custom_private_packet(const Tox_Events *events, uint32_t index) { - assert(index < events->group_custom_private_packet_size); - assert(events->group_custom_private_packet != nullptr); - return &events->group_custom_private_packet[index]; -} - -bool tox_events_pack_group_custom_private_packet(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_custom_private_packet_size(events); + uint32_t group_custom_private_packet_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_custom_private_packet_pack(tox_events_get_group_custom_private_packet(events, i), bp)) { - return false; + if (group_custom_private_packet_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET) { + const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet = events->events[i].data.group_custom_private_packet; + if (group_custom_private_packet_index == index) { + return group_custom_private_packet; + } + ++group_custom_private_packet_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_custom_private_packet(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_custom_private_packet_size(const Tox_Events *events) { - Tox_Event_Group_Custom_Private_Packet *event = tox_events_add_group_custom_private_packet(events); + uint32_t group_custom_private_packet_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET) { + ++group_custom_private_packet_size; + } + } + + return group_custom_private_packet_size; +} + +bool tox_event_group_custom_private_packet_unpack( + Tox_Event_Group_Custom_Private_Packet **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_custom_private_packet_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_custom_private_packet_unpack(event, bu); + return tox_event_group_custom_private_packet_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Custom_Private_Packet *tox_event_group_custom_private_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_Group_Custom_Private_Packet *group_custom_private_packet = tox_events_add_group_custom_private_packet(state->events, state->mem); + + if (group_custom_private_packet == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_custom_private_packet; } @@ -232,17 +258,9 @@ bool tox_events_unpack_group_custom_private_packet(Tox_Events *events, Bin_Unpac void tox_events_handle_group_custom_private_packet(Tox *tox, uint32_t group_number, uint32_t peer_id, 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_Group_Custom_Private_Packet *group_custom_private_packet = tox_events_add_group_custom_private_packet(state->events); + Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet = tox_event_group_custom_private_packet_alloc(user_data); if (group_custom_private_packet == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_invite.c b/toxcore/events/group_invite.c index 8522bb9c..6e6fdb25 100644 --- a/toxcore/events/group_invite.c +++ b/toxcore/events/group_invite.c @@ -31,20 +31,6 @@ struct Tox_Event_Group_Invite { uint32_t group_name_length; }; -non_null() -static void tox_event_group_invite_construct(Tox_Event_Group_Invite *group_invite) -{ - *group_invite = (Tox_Event_Group_Invite) { - 0 - }; -} -non_null() -static void tox_event_group_invite_destruct(Tox_Event_Group_Invite *group_invite) -{ - free(group_invite->invite_data); - free(group_invite->group_name); -} - non_null() static void tox_event_group_invite_set_friend_number(Tox_Event_Group_Invite *group_invite, uint32_t friend_number) @@ -80,7 +66,7 @@ static bool tox_event_group_invite_set_invite_data(Tox_Event_Group_Invite *group group_invite->invite_data_length = invite_data_length; return true; } -size_t tox_event_group_invite_get_invite_data_length(const Tox_Event_Group_Invite *group_invite) +uint32_t tox_event_group_invite_get_invite_data_length(const Tox_Event_Group_Invite *group_invite) { assert(group_invite != nullptr); return group_invite->invite_data_length; @@ -113,7 +99,7 @@ static bool tox_event_group_invite_set_group_name(Tox_Event_Group_Invite *group_ group_invite->group_name_length = group_name_length; return true; } -size_t tox_event_group_invite_get_group_name_length(const Tox_Event_Group_Invite *group_invite) +uint32_t tox_event_group_invite_get_group_name_length(const Tox_Event_Group_Invite *group_invite) { assert(group_invite != nullptr); return group_invite->group_name_length; @@ -125,7 +111,20 @@ const uint8_t *tox_event_group_invite_get_group_name(const Tox_Event_Group_Invit } non_null() -static bool tox_event_group_invite_pack( +static void tox_event_group_invite_construct(Tox_Event_Group_Invite *group_invite) +{ + *group_invite = (Tox_Event_Group_Invite) { + 0 + }; +} +non_null() +static void tox_event_group_invite_destruct(Tox_Event_Group_Invite *group_invite, const Memory *mem) +{ + free(group_invite->invite_data); + free(group_invite->group_name); +} + +bool tox_event_group_invite_pack( const Tox_Event_Group_Invite *event, Bin_Pack *bp) { assert(event != nullptr); @@ -138,7 +137,7 @@ static bool tox_event_group_invite_pack( } non_null() -static bool tox_event_group_invite_unpack( +static bool tox_event_group_invite_unpack_into( Tox_Event_Group_Invite *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -154,93 +153,120 @@ static bool tox_event_group_invite_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Invite *tox_events_add_group_invite(Tox_Events *events) +const Tox_Event_Group_Invite *tox_event_get_group_invite(const Tox_Event *event) { - if (events->group_invite_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_INVITE ? event->data.group_invite : nullptr; +} + +Tox_Event_Group_Invite *tox_event_group_invite_new(const Memory *mem) +{ + Tox_Event_Group_Invite *const group_invite = + (Tox_Event_Group_Invite *)mem_alloc(mem, sizeof(Tox_Event_Group_Invite)); + + if (group_invite == nullptr) { return nullptr; } - if (events->group_invite_size == events->group_invite_capacity) { - const uint32_t new_group_invite_capacity = events->group_invite_capacity * 2 + 1; - Tox_Event_Group_Invite *new_group_invite = (Tox_Event_Group_Invite *) - realloc( - events->group_invite, - new_group_invite_capacity * sizeof(Tox_Event_Group_Invite)); - - if (new_group_invite == nullptr) { - return nullptr; - } - - events->group_invite = new_group_invite; - events->group_invite_capacity = new_group_invite_capacity; - } - - Tox_Event_Group_Invite *const group_invite = - &events->group_invite[events->group_invite_size]; tox_event_group_invite_construct(group_invite); - ++events->group_invite_size; return group_invite; } -void tox_events_clear_group_invite(Tox_Events *events) +void tox_event_group_invite_free(Tox_Event_Group_Invite *group_invite, const Memory *mem) { - if (events == nullptr) { - return; + if (group_invite != nullptr) { + tox_event_group_invite_destruct(group_invite, mem); } - - for (uint32_t i = 0; i < events->group_invite_size; ++i) { - tox_event_group_invite_destruct(&events->group_invite[i]); - } - - free(events->group_invite); - events->group_invite = nullptr; - events->group_invite_size = 0; - events->group_invite_capacity = 0; + mem_delete(mem, group_invite); } -uint32_t tox_events_get_group_invite_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Invite *tox_events_add_group_invite(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Invite *const group_invite = tox_event_group_invite_new(mem); + + if (group_invite == nullptr) { + return nullptr; } - return events->group_invite_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_INVITE; + event.data.group_invite = group_invite; + + tox_events_add(events, &event); + return group_invite; } const Tox_Event_Group_Invite *tox_events_get_group_invite(const Tox_Events *events, uint32_t index) { - assert(index < events->group_invite_size); - assert(events->group_invite != nullptr); - return &events->group_invite[index]; -} - -bool tox_events_pack_group_invite(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_invite_size(events); + uint32_t group_invite_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_invite_pack(tox_events_get_group_invite(events, i), bp)) { - return false; + if (group_invite_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_INVITE) { + const Tox_Event_Group_Invite *group_invite = events->events[i].data.group_invite; + if (group_invite_index == index) { + return group_invite; + } + ++group_invite_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_invite(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_invite_size(const Tox_Events *events) { - Tox_Event_Group_Invite *event = tox_events_add_group_invite(events); + uint32_t group_invite_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_INVITE) { + ++group_invite_size; + } + } + + return group_invite_size; +} + +bool tox_event_group_invite_unpack( + Tox_Event_Group_Invite **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_invite_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_invite_unpack(event, bu); + return tox_event_group_invite_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Invite *tox_event_group_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_Group_Invite *group_invite = tox_events_add_group_invite(state->events, state->mem); + + if (group_invite == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_invite; } @@ -254,17 +280,9 @@ bool tox_events_unpack_group_invite(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_invite(Tox *tox, uint32_t friend_number, const uint8_t *invite_data, size_t length, const uint8_t *group_name, size_t group_name_length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Group_Invite *group_invite = tox_events_add_group_invite(state->events); + Tox_Event_Group_Invite *group_invite = tox_event_group_invite_alloc(user_data); if (group_invite == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_join_fail.c b/toxcore/events/group_join_fail.c index 347fa120..154523ea 100644 --- a/toxcore/events/group_join_fail.c +++ b/toxcore/events/group_join_fail.c @@ -28,19 +28,6 @@ struct Tox_Event_Group_Join_Fail { Tox_Group_Join_Fail fail_type; }; -non_null() -static void tox_event_group_join_fail_construct(Tox_Event_Group_Join_Fail *group_join_fail) -{ - *group_join_fail = (Tox_Event_Group_Join_Fail) { - 0 - }; -} -non_null() -static void tox_event_group_join_fail_destruct(Tox_Event_Group_Join_Fail *group_join_fail) -{ - return; -} - non_null() static void tox_event_group_join_fail_set_group_number(Tox_Event_Group_Join_Fail *group_join_fail, uint32_t group_number) @@ -68,7 +55,19 @@ Tox_Group_Join_Fail tox_event_group_join_fail_get_fail_type(const Tox_Event_Grou } non_null() -static bool tox_event_group_join_fail_pack( +static void tox_event_group_join_fail_construct(Tox_Event_Group_Join_Fail *group_join_fail) +{ + *group_join_fail = (Tox_Event_Group_Join_Fail) { + 0 + }; +} +non_null() +static void tox_event_group_join_fail_destruct(Tox_Event_Group_Join_Fail *group_join_fail, const Memory *mem) +{ + return; +} + +bool tox_event_group_join_fail_pack( const Tox_Event_Group_Join_Fail *event, Bin_Pack *bp) { assert(event != nullptr); @@ -80,7 +79,7 @@ static bool tox_event_group_join_fail_pack( } non_null() -static bool tox_event_group_join_fail_unpack( +static bool tox_event_group_join_fail_unpack_into( Tox_Event_Group_Join_Fail *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -89,99 +88,126 @@ static bool tox_event_group_join_fail_unpack( } return bin_unpack_u32(bu, &event->group_number) - && tox_unpack_group_join_fail(bu, &event->fail_type); + && tox_group_join_fail_unpack(bu, &event->fail_type); } /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Join_Fail *tox_events_add_group_join_fail(Tox_Events *events) +const Tox_Event_Group_Join_Fail *tox_event_get_group_join_fail(const Tox_Event *event) { - if (events->group_join_fail_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_JOIN_FAIL ? event->data.group_join_fail : nullptr; +} + +Tox_Event_Group_Join_Fail *tox_event_group_join_fail_new(const Memory *mem) +{ + Tox_Event_Group_Join_Fail *const group_join_fail = + (Tox_Event_Group_Join_Fail *)mem_alloc(mem, sizeof(Tox_Event_Group_Join_Fail)); + + if (group_join_fail == nullptr) { return nullptr; } - if (events->group_join_fail_size == events->group_join_fail_capacity) { - const uint32_t new_group_join_fail_capacity = events->group_join_fail_capacity * 2 + 1; - Tox_Event_Group_Join_Fail *new_group_join_fail = (Tox_Event_Group_Join_Fail *) - realloc( - events->group_join_fail, - new_group_join_fail_capacity * sizeof(Tox_Event_Group_Join_Fail)); - - if (new_group_join_fail == nullptr) { - return nullptr; - } - - events->group_join_fail = new_group_join_fail; - events->group_join_fail_capacity = new_group_join_fail_capacity; - } - - Tox_Event_Group_Join_Fail *const group_join_fail = - &events->group_join_fail[events->group_join_fail_size]; tox_event_group_join_fail_construct(group_join_fail); - ++events->group_join_fail_size; return group_join_fail; } -void tox_events_clear_group_join_fail(Tox_Events *events) +void tox_event_group_join_fail_free(Tox_Event_Group_Join_Fail *group_join_fail, const Memory *mem) { - if (events == nullptr) { - return; + if (group_join_fail != nullptr) { + tox_event_group_join_fail_destruct(group_join_fail, mem); } - - for (uint32_t i = 0; i < events->group_join_fail_size; ++i) { - tox_event_group_join_fail_destruct(&events->group_join_fail[i]); - } - - free(events->group_join_fail); - events->group_join_fail = nullptr; - events->group_join_fail_size = 0; - events->group_join_fail_capacity = 0; + mem_delete(mem, group_join_fail); } -uint32_t tox_events_get_group_join_fail_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Join_Fail *tox_events_add_group_join_fail(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Join_Fail *const group_join_fail = tox_event_group_join_fail_new(mem); + + if (group_join_fail == nullptr) { + return nullptr; } - return events->group_join_fail_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_JOIN_FAIL; + event.data.group_join_fail = group_join_fail; + + tox_events_add(events, &event); + return group_join_fail; } const Tox_Event_Group_Join_Fail *tox_events_get_group_join_fail(const Tox_Events *events, uint32_t index) { - assert(index < events->group_join_fail_size); - assert(events->group_join_fail != nullptr); - return &events->group_join_fail[index]; -} - -bool tox_events_pack_group_join_fail(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_join_fail_size(events); + uint32_t group_join_fail_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_join_fail_pack(tox_events_get_group_join_fail(events, i), bp)) { - return false; + if (group_join_fail_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_JOIN_FAIL) { + const Tox_Event_Group_Join_Fail *group_join_fail = events->events[i].data.group_join_fail; + if (group_join_fail_index == index) { + return group_join_fail; + } + ++group_join_fail_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_join_fail(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_join_fail_size(const Tox_Events *events) { - Tox_Event_Group_Join_Fail *event = tox_events_add_group_join_fail(events); + uint32_t group_join_fail_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_JOIN_FAIL) { + ++group_join_fail_size; + } + } + + return group_join_fail_size; +} + +bool tox_event_group_join_fail_unpack( + Tox_Event_Group_Join_Fail **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_join_fail_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_join_fail_unpack(event, bu); + return tox_event_group_join_fail_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Join_Fail *tox_event_group_join_fail_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Group_Join_Fail *group_join_fail = tox_events_add_group_join_fail(state->events, state->mem); + + if (group_join_fail == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_join_fail; } @@ -195,17 +221,9 @@ bool tox_events_unpack_group_join_fail(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_join_fail(Tox *tox, uint32_t group_number, Tox_Group_Join_Fail fail_type, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Group_Join_Fail *group_join_fail = tox_events_add_group_join_fail(state->events); + Tox_Event_Group_Join_Fail *group_join_fail = tox_event_group_join_fail_alloc(user_data); if (group_join_fail == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_message.c b/toxcore/events/group_message.c index 992ad4b7..f3413a9d 100644 --- a/toxcore/events/group_message.c +++ b/toxcore/events/group_message.c @@ -32,19 +32,6 @@ struct Tox_Event_Group_Message { uint32_t message_id; }; -non_null() -static void tox_event_group_message_construct(Tox_Event_Group_Message *group_message) -{ - *group_message = (Tox_Event_Group_Message) { - 0 - }; -} -non_null() -static void tox_event_group_message_destruct(Tox_Event_Group_Message *group_message) -{ - free(group_message->message); -} - non_null() static void tox_event_group_message_set_group_number(Tox_Event_Group_Message *group_message, uint32_t group_number) @@ -106,7 +93,7 @@ static bool tox_event_group_message_set_message(Tox_Event_Group_Message *group_m group_message->message_length = message_length; return true; } -size_t tox_event_group_message_get_message_length(const Tox_Event_Group_Message *group_message) +uint32_t tox_event_group_message_get_message_length(const Tox_Event_Group_Message *group_message) { assert(group_message != nullptr); return group_message->message_length; @@ -131,7 +118,19 @@ uint32_t tox_event_group_message_get_message_id(const Tox_Event_Group_Message *g } non_null() -static bool tox_event_group_message_pack( +static void tox_event_group_message_construct(Tox_Event_Group_Message *group_message) +{ + *group_message = (Tox_Event_Group_Message) { + 0 + }; +} +non_null() +static void tox_event_group_message_destruct(Tox_Event_Group_Message *group_message, const Memory *mem) +{ + free(group_message->message); +} + +bool tox_event_group_message_pack( const Tox_Event_Group_Message *event, Bin_Pack *bp) { assert(event != nullptr); @@ -146,7 +145,7 @@ static bool tox_event_group_message_pack( } non_null() -static bool tox_event_group_message_unpack( +static bool tox_event_group_message_unpack_into( Tox_Event_Group_Message *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -156,7 +155,7 @@ static bool tox_event_group_message_unpack( return bin_unpack_u32(bu, &event->group_number) && bin_unpack_u32(bu, &event->peer_id) - && tox_unpack_message_type(bu, &event->type) + && tox_message_type_unpack(bu, &event->type) && bin_unpack_bin(bu, &event->message, &event->message_length) && bin_unpack_u32(bu, &event->message_id); } @@ -164,93 +163,120 @@ static bool tox_event_group_message_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Message *tox_events_add_group_message(Tox_Events *events) +const Tox_Event_Group_Message *tox_event_get_group_message(const Tox_Event *event) { - if (events->group_message_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_MESSAGE ? event->data.group_message : nullptr; +} + +Tox_Event_Group_Message *tox_event_group_message_new(const Memory *mem) +{ + Tox_Event_Group_Message *const group_message = + (Tox_Event_Group_Message *)mem_alloc(mem, sizeof(Tox_Event_Group_Message)); + + if (group_message == nullptr) { return nullptr; } - if (events->group_message_size == events->group_message_capacity) { - const uint32_t new_group_message_capacity = events->group_message_capacity * 2 + 1; - Tox_Event_Group_Message *new_group_message = (Tox_Event_Group_Message *) - realloc( - events->group_message, - new_group_message_capacity * sizeof(Tox_Event_Group_Message)); - - if (new_group_message == nullptr) { - return nullptr; - } - - events->group_message = new_group_message; - events->group_message_capacity = new_group_message_capacity; - } - - Tox_Event_Group_Message *const group_message = - &events->group_message[events->group_message_size]; tox_event_group_message_construct(group_message); - ++events->group_message_size; return group_message; } -void tox_events_clear_group_message(Tox_Events *events) +void tox_event_group_message_free(Tox_Event_Group_Message *group_message, const Memory *mem) { - if (events == nullptr) { - return; + if (group_message != nullptr) { + tox_event_group_message_destruct(group_message, mem); } - - for (uint32_t i = 0; i < events->group_message_size; ++i) { - tox_event_group_message_destruct(&events->group_message[i]); - } - - free(events->group_message); - events->group_message = nullptr; - events->group_message_size = 0; - events->group_message_capacity = 0; + mem_delete(mem, group_message); } -uint32_t tox_events_get_group_message_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Message *tox_events_add_group_message(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Message *const group_message = tox_event_group_message_new(mem); + + if (group_message == nullptr) { + return nullptr; } - return events->group_message_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_MESSAGE; + event.data.group_message = group_message; + + tox_events_add(events, &event); + return group_message; } const Tox_Event_Group_Message *tox_events_get_group_message(const Tox_Events *events, uint32_t index) { - assert(index < events->group_message_size); - assert(events->group_message != nullptr); - return &events->group_message[index]; -} - -bool tox_events_pack_group_message(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_message_size(events); + uint32_t group_message_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_message_pack(tox_events_get_group_message(events, i), bp)) { - return false; + if (group_message_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_MESSAGE) { + const Tox_Event_Group_Message *group_message = events->events[i].data.group_message; + if (group_message_index == index) { + return group_message; + } + ++group_message_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_message(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_message_size(const Tox_Events *events) { - Tox_Event_Group_Message *event = tox_events_add_group_message(events); + uint32_t group_message_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_MESSAGE) { + ++group_message_size; + } + } + + return group_message_size; +} + +bool tox_event_group_message_unpack( + Tox_Event_Group_Message **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_message_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_message_unpack(event, bu); + return tox_event_group_message_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Message *tox_event_group_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_Group_Message *group_message = tox_events_add_group_message(state->events, state->mem); + + if (group_message == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_message; } @@ -264,17 +290,9 @@ bool tox_events_unpack_group_message(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_message(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Message_Type type, const uint8_t *message, size_t length, 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_Group_Message *group_message = tox_events_add_group_message(state->events); + Tox_Event_Group_Message *group_message = tox_event_group_message_alloc(user_data); if (group_message == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_moderation.c b/toxcore/events/group_moderation.c index f6b7d17f..7e573753 100644 --- a/toxcore/events/group_moderation.c +++ b/toxcore/events/group_moderation.c @@ -30,19 +30,6 @@ struct Tox_Event_Group_Moderation { Tox_Group_Mod_Event mod_type; }; -non_null() -static void tox_event_group_moderation_construct(Tox_Event_Group_Moderation *group_moderation) -{ - *group_moderation = (Tox_Event_Group_Moderation) { - 0 - }; -} -non_null() -static void tox_event_group_moderation_destruct(Tox_Event_Group_Moderation *group_moderation) -{ - return; -} - non_null() static void tox_event_group_moderation_set_group_number(Tox_Event_Group_Moderation *group_moderation, uint32_t group_number) @@ -96,7 +83,19 @@ Tox_Group_Mod_Event tox_event_group_moderation_get_mod_type(const Tox_Event_Grou } non_null() -static bool tox_event_group_moderation_pack( +static void tox_event_group_moderation_construct(Tox_Event_Group_Moderation *group_moderation) +{ + *group_moderation = (Tox_Event_Group_Moderation) { + 0 + }; +} +non_null() +static void tox_event_group_moderation_destruct(Tox_Event_Group_Moderation *group_moderation, const Memory *mem) +{ + return; +} + +bool tox_event_group_moderation_pack( const Tox_Event_Group_Moderation *event, Bin_Pack *bp) { assert(event != nullptr); @@ -110,7 +109,7 @@ static bool tox_event_group_moderation_pack( } non_null() -static bool tox_event_group_moderation_unpack( +static bool tox_event_group_moderation_unpack_into( Tox_Event_Group_Moderation *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -121,99 +120,126 @@ static bool tox_event_group_moderation_unpack( return bin_unpack_u32(bu, &event->group_number) && bin_unpack_u32(bu, &event->source_peer_id) && bin_unpack_u32(bu, &event->target_peer_id) - && tox_unpack_group_mod_event(bu, &event->mod_type); + && tox_group_mod_event_unpack(bu, &event->mod_type); } /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Moderation *tox_events_add_group_moderation(Tox_Events *events) +const Tox_Event_Group_Moderation *tox_event_get_group_moderation(const Tox_Event *event) { - if (events->group_moderation_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_MODERATION ? event->data.group_moderation : nullptr; +} + +Tox_Event_Group_Moderation *tox_event_group_moderation_new(const Memory *mem) +{ + Tox_Event_Group_Moderation *const group_moderation = + (Tox_Event_Group_Moderation *)mem_alloc(mem, sizeof(Tox_Event_Group_Moderation)); + + if (group_moderation == nullptr) { return nullptr; } - if (events->group_moderation_size == events->group_moderation_capacity) { - const uint32_t new_group_moderation_capacity = events->group_moderation_capacity * 2 + 1; - Tox_Event_Group_Moderation *new_group_moderation = (Tox_Event_Group_Moderation *) - realloc( - events->group_moderation, - new_group_moderation_capacity * sizeof(Tox_Event_Group_Moderation)); - - if (new_group_moderation == nullptr) { - return nullptr; - } - - events->group_moderation = new_group_moderation; - events->group_moderation_capacity = new_group_moderation_capacity; - } - - Tox_Event_Group_Moderation *const group_moderation = - &events->group_moderation[events->group_moderation_size]; tox_event_group_moderation_construct(group_moderation); - ++events->group_moderation_size; return group_moderation; } -void tox_events_clear_group_moderation(Tox_Events *events) +void tox_event_group_moderation_free(Tox_Event_Group_Moderation *group_moderation, const Memory *mem) { - if (events == nullptr) { - return; + if (group_moderation != nullptr) { + tox_event_group_moderation_destruct(group_moderation, mem); } - - for (uint32_t i = 0; i < events->group_moderation_size; ++i) { - tox_event_group_moderation_destruct(&events->group_moderation[i]); - } - - free(events->group_moderation); - events->group_moderation = nullptr; - events->group_moderation_size = 0; - events->group_moderation_capacity = 0; + mem_delete(mem, group_moderation); } -uint32_t tox_events_get_group_moderation_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Moderation *tox_events_add_group_moderation(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Moderation *const group_moderation = tox_event_group_moderation_new(mem); + + if (group_moderation == nullptr) { + return nullptr; } - return events->group_moderation_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_MODERATION; + event.data.group_moderation = group_moderation; + + tox_events_add(events, &event); + return group_moderation; } const Tox_Event_Group_Moderation *tox_events_get_group_moderation(const Tox_Events *events, uint32_t index) { - assert(index < events->group_moderation_size); - assert(events->group_moderation != nullptr); - return &events->group_moderation[index]; -} - -bool tox_events_pack_group_moderation(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_moderation_size(events); + uint32_t group_moderation_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_moderation_pack(tox_events_get_group_moderation(events, i), bp)) { - return false; + if (group_moderation_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_MODERATION) { + const Tox_Event_Group_Moderation *group_moderation = events->events[i].data.group_moderation; + if (group_moderation_index == index) { + return group_moderation; + } + ++group_moderation_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_moderation(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_moderation_size(const Tox_Events *events) { - Tox_Event_Group_Moderation *event = tox_events_add_group_moderation(events); + uint32_t group_moderation_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_MODERATION) { + ++group_moderation_size; + } + } + + return group_moderation_size; +} + +bool tox_event_group_moderation_unpack( + Tox_Event_Group_Moderation **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_moderation_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_moderation_unpack(event, bu); + return tox_event_group_moderation_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Moderation *tox_event_group_moderation_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Group_Moderation *group_moderation = tox_events_add_group_moderation(state->events, state->mem); + + if (group_moderation == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_moderation; } @@ -227,17 +253,9 @@ bool tox_events_unpack_group_moderation(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_moderation(Tox *tox, uint32_t group_number, uint32_t source_peer_id, uint32_t target_peer_id, Tox_Group_Mod_Event mod_type, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Group_Moderation *group_moderation = tox_events_add_group_moderation(state->events); + Tox_Event_Group_Moderation *group_moderation = tox_event_group_moderation_alloc(user_data); if (group_moderation == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_password.c b/toxcore/events/group_password.c index 2bf16d3a..82f9166e 100644 --- a/toxcore/events/group_password.c +++ b/toxcore/events/group_password.c @@ -29,19 +29,6 @@ struct Tox_Event_Group_Password { uint32_t password_length; }; -non_null() -static void tox_event_group_password_construct(Tox_Event_Group_Password *group_password) -{ - *group_password = (Tox_Event_Group_Password) { - 0 - }; -} -non_null() -static void tox_event_group_password_destruct(Tox_Event_Group_Password *group_password) -{ - free(group_password->password); -} - non_null() static void tox_event_group_password_set_group_number(Tox_Event_Group_Password *group_password, uint32_t group_number) @@ -77,7 +64,7 @@ static bool tox_event_group_password_set_password(Tox_Event_Group_Password *grou group_password->password_length = password_length; return true; } -size_t tox_event_group_password_get_password_length(const Tox_Event_Group_Password *group_password) +uint32_t tox_event_group_password_get_password_length(const Tox_Event_Group_Password *group_password) { assert(group_password != nullptr); return group_password->password_length; @@ -89,7 +76,19 @@ const uint8_t *tox_event_group_password_get_password(const Tox_Event_Group_Passw } non_null() -static bool tox_event_group_password_pack( +static void tox_event_group_password_construct(Tox_Event_Group_Password *group_password) +{ + *group_password = (Tox_Event_Group_Password) { + 0 + }; +} +non_null() +static void tox_event_group_password_destruct(Tox_Event_Group_Password *group_password, const Memory *mem) +{ + free(group_password->password); +} + +bool tox_event_group_password_pack( const Tox_Event_Group_Password *event, Bin_Pack *bp) { assert(event != nullptr); @@ -101,7 +100,7 @@ static bool tox_event_group_password_pack( } non_null() -static bool tox_event_group_password_unpack( +static bool tox_event_group_password_unpack_into( Tox_Event_Group_Password *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -116,93 +115,120 @@ static bool tox_event_group_password_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Password *tox_events_add_group_password(Tox_Events *events) +const Tox_Event_Group_Password *tox_event_get_group_password(const Tox_Event *event) { - if (events->group_password_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_PASSWORD ? event->data.group_password : nullptr; +} + +Tox_Event_Group_Password *tox_event_group_password_new(const Memory *mem) +{ + Tox_Event_Group_Password *const group_password = + (Tox_Event_Group_Password *)mem_alloc(mem, sizeof(Tox_Event_Group_Password)); + + if (group_password == nullptr) { return nullptr; } - if (events->group_password_size == events->group_password_capacity) { - const uint32_t new_group_password_capacity = events->group_password_capacity * 2 + 1; - Tox_Event_Group_Password *new_group_password = (Tox_Event_Group_Password *) - realloc( - events->group_password, - new_group_password_capacity * sizeof(Tox_Event_Group_Password)); - - if (new_group_password == nullptr) { - return nullptr; - } - - events->group_password = new_group_password; - events->group_password_capacity = new_group_password_capacity; - } - - Tox_Event_Group_Password *const group_password = - &events->group_password[events->group_password_size]; tox_event_group_password_construct(group_password); - ++events->group_password_size; return group_password; } -void tox_events_clear_group_password(Tox_Events *events) +void tox_event_group_password_free(Tox_Event_Group_Password *group_password, const Memory *mem) { - if (events == nullptr) { - return; + if (group_password != nullptr) { + tox_event_group_password_destruct(group_password, mem); } - - for (uint32_t i = 0; i < events->group_password_size; ++i) { - tox_event_group_password_destruct(&events->group_password[i]); - } - - free(events->group_password); - events->group_password = nullptr; - events->group_password_size = 0; - events->group_password_capacity = 0; + mem_delete(mem, group_password); } -uint32_t tox_events_get_group_password_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Password *tox_events_add_group_password(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Password *const group_password = tox_event_group_password_new(mem); + + if (group_password == nullptr) { + return nullptr; } - return events->group_password_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_PASSWORD; + event.data.group_password = group_password; + + tox_events_add(events, &event); + return group_password; } const Tox_Event_Group_Password *tox_events_get_group_password(const Tox_Events *events, uint32_t index) { - assert(index < events->group_password_size); - assert(events->group_password != nullptr); - return &events->group_password[index]; -} - -bool tox_events_pack_group_password(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_password_size(events); + uint32_t group_password_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_password_pack(tox_events_get_group_password(events, i), bp)) { - return false; + if (group_password_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PASSWORD) { + const Tox_Event_Group_Password *group_password = events->events[i].data.group_password; + if (group_password_index == index) { + return group_password; + } + ++group_password_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_password(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_password_size(const Tox_Events *events) { - Tox_Event_Group_Password *event = tox_events_add_group_password(events); + uint32_t group_password_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PASSWORD) { + ++group_password_size; + } + } + + return group_password_size; +} + +bool tox_event_group_password_unpack( + Tox_Event_Group_Password **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_password_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_password_unpack(event, bu); + return tox_event_group_password_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Password *tox_event_group_password_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Group_Password *group_password = tox_events_add_group_password(state->events, state->mem); + + if (group_password == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_password; } @@ -216,17 +242,9 @@ bool tox_events_unpack_group_password(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_password(Tox *tox, uint32_t group_number, const uint8_t *password, 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_Group_Password *group_password = tox_events_add_group_password(state->events); + Tox_Event_Group_Password *group_password = tox_event_group_password_alloc(user_data); if (group_password == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_peer_exit.c b/toxcore/events/group_peer_exit.c index 81085d38..3a800a99 100644 --- a/toxcore/events/group_peer_exit.c +++ b/toxcore/events/group_peer_exit.c @@ -33,20 +33,6 @@ struct Tox_Event_Group_Peer_Exit { uint32_t part_message_length; }; -non_null() -static void tox_event_group_peer_exit_construct(Tox_Event_Group_Peer_Exit *group_peer_exit) -{ - *group_peer_exit = (Tox_Event_Group_Peer_Exit) { - 0 - }; -} -non_null() -static void tox_event_group_peer_exit_destruct(Tox_Event_Group_Peer_Exit *group_peer_exit) -{ - free(group_peer_exit->name); - free(group_peer_exit->part_message); -} - non_null() static void tox_event_group_peer_exit_set_group_number(Tox_Event_Group_Peer_Exit *group_peer_exit, uint32_t group_number) @@ -108,7 +94,7 @@ static bool tox_event_group_peer_exit_set_name(Tox_Event_Group_Peer_Exit *group_ group_peer_exit->name_length = name_length; return true; } -size_t tox_event_group_peer_exit_get_name_length(const Tox_Event_Group_Peer_Exit *group_peer_exit) +uint32_t tox_event_group_peer_exit_get_name_length(const Tox_Event_Group_Peer_Exit *group_peer_exit) { assert(group_peer_exit != nullptr); return group_peer_exit->name_length; @@ -141,7 +127,7 @@ static bool tox_event_group_peer_exit_set_part_message(Tox_Event_Group_Peer_Exit group_peer_exit->part_message_length = part_message_length; return true; } -size_t tox_event_group_peer_exit_get_part_message_length(const Tox_Event_Group_Peer_Exit *group_peer_exit) +uint32_t tox_event_group_peer_exit_get_part_message_length(const Tox_Event_Group_Peer_Exit *group_peer_exit) { assert(group_peer_exit != nullptr); return group_peer_exit->part_message_length; @@ -153,7 +139,20 @@ const uint8_t *tox_event_group_peer_exit_get_part_message(const Tox_Event_Group_ } non_null() -static bool tox_event_group_peer_exit_pack( +static void tox_event_group_peer_exit_construct(Tox_Event_Group_Peer_Exit *group_peer_exit) +{ + *group_peer_exit = (Tox_Event_Group_Peer_Exit) { + 0 + }; +} +non_null() +static void tox_event_group_peer_exit_destruct(Tox_Event_Group_Peer_Exit *group_peer_exit, const Memory *mem) +{ + free(group_peer_exit->name); + free(group_peer_exit->part_message); +} + +bool tox_event_group_peer_exit_pack( const Tox_Event_Group_Peer_Exit *event, Bin_Pack *bp) { assert(event != nullptr); @@ -168,7 +167,7 @@ static bool tox_event_group_peer_exit_pack( } non_null() -static bool tox_event_group_peer_exit_unpack( +static bool tox_event_group_peer_exit_unpack_into( Tox_Event_Group_Peer_Exit *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -178,7 +177,7 @@ static bool tox_event_group_peer_exit_unpack( return bin_unpack_u32(bu, &event->group_number) && bin_unpack_u32(bu, &event->peer_id) - && tox_unpack_group_exit_type(bu, &event->exit_type) + && tox_group_exit_type_unpack(bu, &event->exit_type) && bin_unpack_bin(bu, &event->name, &event->name_length) && bin_unpack_bin(bu, &event->part_message, &event->part_message_length); } @@ -186,93 +185,120 @@ static bool tox_event_group_peer_exit_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Peer_Exit *tox_events_add_group_peer_exit(Tox_Events *events) +const Tox_Event_Group_Peer_Exit *tox_event_get_group_peer_exit(const Tox_Event *event) { - if (events->group_peer_exit_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_PEER_EXIT ? event->data.group_peer_exit : nullptr; +} + +Tox_Event_Group_Peer_Exit *tox_event_group_peer_exit_new(const Memory *mem) +{ + Tox_Event_Group_Peer_Exit *const group_peer_exit = + (Tox_Event_Group_Peer_Exit *)mem_alloc(mem, sizeof(Tox_Event_Group_Peer_Exit)); + + if (group_peer_exit == nullptr) { return nullptr; } - if (events->group_peer_exit_size == events->group_peer_exit_capacity) { - const uint32_t new_group_peer_exit_capacity = events->group_peer_exit_capacity * 2 + 1; - Tox_Event_Group_Peer_Exit *new_group_peer_exit = (Tox_Event_Group_Peer_Exit *) - realloc( - events->group_peer_exit, - new_group_peer_exit_capacity * sizeof(Tox_Event_Group_Peer_Exit)); - - if (new_group_peer_exit == nullptr) { - return nullptr; - } - - events->group_peer_exit = new_group_peer_exit; - events->group_peer_exit_capacity = new_group_peer_exit_capacity; - } - - Tox_Event_Group_Peer_Exit *const group_peer_exit = - &events->group_peer_exit[events->group_peer_exit_size]; tox_event_group_peer_exit_construct(group_peer_exit); - ++events->group_peer_exit_size; return group_peer_exit; } -void tox_events_clear_group_peer_exit(Tox_Events *events) +void tox_event_group_peer_exit_free(Tox_Event_Group_Peer_Exit *group_peer_exit, const Memory *mem) { - if (events == nullptr) { - return; + if (group_peer_exit != nullptr) { + tox_event_group_peer_exit_destruct(group_peer_exit, mem); } - - for (uint32_t i = 0; i < events->group_peer_exit_size; ++i) { - tox_event_group_peer_exit_destruct(&events->group_peer_exit[i]); - } - - free(events->group_peer_exit); - events->group_peer_exit = nullptr; - events->group_peer_exit_size = 0; - events->group_peer_exit_capacity = 0; + mem_delete(mem, group_peer_exit); } -uint32_t tox_events_get_group_peer_exit_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Peer_Exit *tox_events_add_group_peer_exit(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Peer_Exit *const group_peer_exit = tox_event_group_peer_exit_new(mem); + + if (group_peer_exit == nullptr) { + return nullptr; } - return events->group_peer_exit_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_PEER_EXIT; + event.data.group_peer_exit = group_peer_exit; + + tox_events_add(events, &event); + return group_peer_exit; } const Tox_Event_Group_Peer_Exit *tox_events_get_group_peer_exit(const Tox_Events *events, uint32_t index) { - assert(index < events->group_peer_exit_size); - assert(events->group_peer_exit != nullptr); - return &events->group_peer_exit[index]; -} - -bool tox_events_pack_group_peer_exit(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_peer_exit_size(events); + uint32_t group_peer_exit_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_peer_exit_pack(tox_events_get_group_peer_exit(events, i), bp)) { - return false; + if (group_peer_exit_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PEER_EXIT) { + const Tox_Event_Group_Peer_Exit *group_peer_exit = events->events[i].data.group_peer_exit; + if (group_peer_exit_index == index) { + return group_peer_exit; + } + ++group_peer_exit_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_peer_exit(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_peer_exit_size(const Tox_Events *events) { - Tox_Event_Group_Peer_Exit *event = tox_events_add_group_peer_exit(events); + uint32_t group_peer_exit_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PEER_EXIT) { + ++group_peer_exit_size; + } + } + + return group_peer_exit_size; +} + +bool tox_event_group_peer_exit_unpack( + Tox_Event_Group_Peer_Exit **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_peer_exit_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_peer_exit_unpack(event, bu); + return tox_event_group_peer_exit_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Peer_Exit *tox_event_group_peer_exit_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Group_Peer_Exit *group_peer_exit = tox_events_add_group_peer_exit(state->events, state->mem); + + if (group_peer_exit == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_peer_exit; } @@ -286,17 +312,9 @@ bool tox_events_unpack_group_peer_exit(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_peer_exit(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Group_Exit_Type exit_type, const uint8_t *name, size_t name_length, const uint8_t *part_message, size_t part_message_length, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Group_Peer_Exit *group_peer_exit = tox_events_add_group_peer_exit(state->events); + Tox_Event_Group_Peer_Exit *group_peer_exit = tox_event_group_peer_exit_alloc(user_data); if (group_peer_exit == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_peer_join.c b/toxcore/events/group_peer_join.c index d872611a..f9cc1758 100644 --- a/toxcore/events/group_peer_join.c +++ b/toxcore/events/group_peer_join.c @@ -28,19 +28,6 @@ struct Tox_Event_Group_Peer_Join { uint32_t peer_id; }; -non_null() -static void tox_event_group_peer_join_construct(Tox_Event_Group_Peer_Join *group_peer_join) -{ - *group_peer_join = (Tox_Event_Group_Peer_Join) { - 0 - }; -} -non_null() -static void tox_event_group_peer_join_destruct(Tox_Event_Group_Peer_Join *group_peer_join) -{ - return; -} - non_null() static void tox_event_group_peer_join_set_group_number(Tox_Event_Group_Peer_Join *group_peer_join, uint32_t group_number) @@ -68,7 +55,19 @@ uint32_t tox_event_group_peer_join_get_peer_id(const Tox_Event_Group_Peer_Join * } non_null() -static bool tox_event_group_peer_join_pack( +static void tox_event_group_peer_join_construct(Tox_Event_Group_Peer_Join *group_peer_join) +{ + *group_peer_join = (Tox_Event_Group_Peer_Join) { + 0 + }; +} +non_null() +static void tox_event_group_peer_join_destruct(Tox_Event_Group_Peer_Join *group_peer_join, const Memory *mem) +{ + return; +} + +bool tox_event_group_peer_join_pack( const Tox_Event_Group_Peer_Join *event, Bin_Pack *bp) { assert(event != nullptr); @@ -80,7 +79,7 @@ static bool tox_event_group_peer_join_pack( } non_null() -static bool tox_event_group_peer_join_unpack( +static bool tox_event_group_peer_join_unpack_into( Tox_Event_Group_Peer_Join *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -95,93 +94,120 @@ static bool tox_event_group_peer_join_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Peer_Join *tox_events_add_group_peer_join(Tox_Events *events) +const Tox_Event_Group_Peer_Join *tox_event_get_group_peer_join(const Tox_Event *event) { - if (events->group_peer_join_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_PEER_JOIN ? event->data.group_peer_join : nullptr; +} + +Tox_Event_Group_Peer_Join *tox_event_group_peer_join_new(const Memory *mem) +{ + Tox_Event_Group_Peer_Join *const group_peer_join = + (Tox_Event_Group_Peer_Join *)mem_alloc(mem, sizeof(Tox_Event_Group_Peer_Join)); + + if (group_peer_join == nullptr) { return nullptr; } - if (events->group_peer_join_size == events->group_peer_join_capacity) { - const uint32_t new_group_peer_join_capacity = events->group_peer_join_capacity * 2 + 1; - Tox_Event_Group_Peer_Join *new_group_peer_join = (Tox_Event_Group_Peer_Join *) - realloc( - events->group_peer_join, - new_group_peer_join_capacity * sizeof(Tox_Event_Group_Peer_Join)); - - if (new_group_peer_join == nullptr) { - return nullptr; - } - - events->group_peer_join = new_group_peer_join; - events->group_peer_join_capacity = new_group_peer_join_capacity; - } - - Tox_Event_Group_Peer_Join *const group_peer_join = - &events->group_peer_join[events->group_peer_join_size]; tox_event_group_peer_join_construct(group_peer_join); - ++events->group_peer_join_size; return group_peer_join; } -void tox_events_clear_group_peer_join(Tox_Events *events) +void tox_event_group_peer_join_free(Tox_Event_Group_Peer_Join *group_peer_join, const Memory *mem) { - if (events == nullptr) { - return; + if (group_peer_join != nullptr) { + tox_event_group_peer_join_destruct(group_peer_join, mem); } - - for (uint32_t i = 0; i < events->group_peer_join_size; ++i) { - tox_event_group_peer_join_destruct(&events->group_peer_join[i]); - } - - free(events->group_peer_join); - events->group_peer_join = nullptr; - events->group_peer_join_size = 0; - events->group_peer_join_capacity = 0; + mem_delete(mem, group_peer_join); } -uint32_t tox_events_get_group_peer_join_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Peer_Join *tox_events_add_group_peer_join(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Peer_Join *const group_peer_join = tox_event_group_peer_join_new(mem); + + if (group_peer_join == nullptr) { + return nullptr; } - return events->group_peer_join_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_PEER_JOIN; + event.data.group_peer_join = group_peer_join; + + tox_events_add(events, &event); + return group_peer_join; } const Tox_Event_Group_Peer_Join *tox_events_get_group_peer_join(const Tox_Events *events, uint32_t index) { - assert(index < events->group_peer_join_size); - assert(events->group_peer_join != nullptr); - return &events->group_peer_join[index]; -} - -bool tox_events_pack_group_peer_join(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_peer_join_size(events); + uint32_t group_peer_join_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_peer_join_pack(tox_events_get_group_peer_join(events, i), bp)) { - return false; + if (group_peer_join_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PEER_JOIN) { + const Tox_Event_Group_Peer_Join *group_peer_join = events->events[i].data.group_peer_join; + if (group_peer_join_index == index) { + return group_peer_join; + } + ++group_peer_join_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_peer_join(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_peer_join_size(const Tox_Events *events) { - Tox_Event_Group_Peer_Join *event = tox_events_add_group_peer_join(events); + uint32_t group_peer_join_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PEER_JOIN) { + ++group_peer_join_size; + } + } + + return group_peer_join_size; +} + +bool tox_event_group_peer_join_unpack( + Tox_Event_Group_Peer_Join **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_peer_join_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_peer_join_unpack(event, bu); + return tox_event_group_peer_join_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Peer_Join *tox_event_group_peer_join_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Group_Peer_Join *group_peer_join = tox_events_add_group_peer_join(state->events, state->mem); + + if (group_peer_join == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_peer_join; } @@ -195,17 +221,9 @@ bool tox_events_unpack_group_peer_join(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_peer_join(Tox *tox, uint32_t group_number, uint32_t peer_id, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Group_Peer_Join *group_peer_join = tox_events_add_group_peer_join(state->events); + Tox_Event_Group_Peer_Join *group_peer_join = tox_event_group_peer_join_alloc(user_data); if (group_peer_join == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_peer_limit.c b/toxcore/events/group_peer_limit.c index 2c1f21d8..6a2b72cb 100644 --- a/toxcore/events/group_peer_limit.c +++ b/toxcore/events/group_peer_limit.c @@ -28,19 +28,6 @@ struct Tox_Event_Group_Peer_Limit { uint32_t peer_limit; }; -non_null() -static void tox_event_group_peer_limit_construct(Tox_Event_Group_Peer_Limit *group_peer_limit) -{ - *group_peer_limit = (Tox_Event_Group_Peer_Limit) { - 0 - }; -} -non_null() -static void tox_event_group_peer_limit_destruct(Tox_Event_Group_Peer_Limit *group_peer_limit) -{ - return; -} - non_null() static void tox_event_group_peer_limit_set_group_number(Tox_Event_Group_Peer_Limit *group_peer_limit, uint32_t group_number) @@ -68,7 +55,19 @@ uint32_t tox_event_group_peer_limit_get_peer_limit(const Tox_Event_Group_Peer_Li } non_null() -static bool tox_event_group_peer_limit_pack( +static void tox_event_group_peer_limit_construct(Tox_Event_Group_Peer_Limit *group_peer_limit) +{ + *group_peer_limit = (Tox_Event_Group_Peer_Limit) { + 0 + }; +} +non_null() +static void tox_event_group_peer_limit_destruct(Tox_Event_Group_Peer_Limit *group_peer_limit, const Memory *mem) +{ + return; +} + +bool tox_event_group_peer_limit_pack( const Tox_Event_Group_Peer_Limit *event, Bin_Pack *bp) { assert(event != nullptr); @@ -80,7 +79,7 @@ static bool tox_event_group_peer_limit_pack( } non_null() -static bool tox_event_group_peer_limit_unpack( +static bool tox_event_group_peer_limit_unpack_into( Tox_Event_Group_Peer_Limit *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -95,93 +94,120 @@ static bool tox_event_group_peer_limit_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Peer_Limit *tox_events_add_group_peer_limit(Tox_Events *events) +const Tox_Event_Group_Peer_Limit *tox_event_get_group_peer_limit(const Tox_Event *event) { - if (events->group_peer_limit_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_PEER_LIMIT ? event->data.group_peer_limit : nullptr; +} + +Tox_Event_Group_Peer_Limit *tox_event_group_peer_limit_new(const Memory *mem) +{ + Tox_Event_Group_Peer_Limit *const group_peer_limit = + (Tox_Event_Group_Peer_Limit *)mem_alloc(mem, sizeof(Tox_Event_Group_Peer_Limit)); + + if (group_peer_limit == nullptr) { return nullptr; } - if (events->group_peer_limit_size == events->group_peer_limit_capacity) { - const uint32_t new_group_peer_limit_capacity = events->group_peer_limit_capacity * 2 + 1; - Tox_Event_Group_Peer_Limit *new_group_peer_limit = (Tox_Event_Group_Peer_Limit *) - realloc( - events->group_peer_limit, - new_group_peer_limit_capacity * sizeof(Tox_Event_Group_Peer_Limit)); - - if (new_group_peer_limit == nullptr) { - return nullptr; - } - - events->group_peer_limit = new_group_peer_limit; - events->group_peer_limit_capacity = new_group_peer_limit_capacity; - } - - Tox_Event_Group_Peer_Limit *const group_peer_limit = - &events->group_peer_limit[events->group_peer_limit_size]; tox_event_group_peer_limit_construct(group_peer_limit); - ++events->group_peer_limit_size; return group_peer_limit; } -void tox_events_clear_group_peer_limit(Tox_Events *events) +void tox_event_group_peer_limit_free(Tox_Event_Group_Peer_Limit *group_peer_limit, const Memory *mem) { - if (events == nullptr) { - return; + if (group_peer_limit != nullptr) { + tox_event_group_peer_limit_destruct(group_peer_limit, mem); } - - for (uint32_t i = 0; i < events->group_peer_limit_size; ++i) { - tox_event_group_peer_limit_destruct(&events->group_peer_limit[i]); - } - - free(events->group_peer_limit); - events->group_peer_limit = nullptr; - events->group_peer_limit_size = 0; - events->group_peer_limit_capacity = 0; + mem_delete(mem, group_peer_limit); } -uint32_t tox_events_get_group_peer_limit_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Peer_Limit *tox_events_add_group_peer_limit(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Peer_Limit *const group_peer_limit = tox_event_group_peer_limit_new(mem); + + if (group_peer_limit == nullptr) { + return nullptr; } - return events->group_peer_limit_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_PEER_LIMIT; + event.data.group_peer_limit = group_peer_limit; + + tox_events_add(events, &event); + return group_peer_limit; } const Tox_Event_Group_Peer_Limit *tox_events_get_group_peer_limit(const Tox_Events *events, uint32_t index) { - assert(index < events->group_peer_limit_size); - assert(events->group_peer_limit != nullptr); - return &events->group_peer_limit[index]; -} - -bool tox_events_pack_group_peer_limit(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_peer_limit_size(events); + uint32_t group_peer_limit_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_peer_limit_pack(tox_events_get_group_peer_limit(events, i), bp)) { - return false; + if (group_peer_limit_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PEER_LIMIT) { + const Tox_Event_Group_Peer_Limit *group_peer_limit = events->events[i].data.group_peer_limit; + if (group_peer_limit_index == index) { + return group_peer_limit; + } + ++group_peer_limit_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_peer_limit(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_peer_limit_size(const Tox_Events *events) { - Tox_Event_Group_Peer_Limit *event = tox_events_add_group_peer_limit(events); + uint32_t group_peer_limit_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PEER_LIMIT) { + ++group_peer_limit_size; + } + } + + return group_peer_limit_size; +} + +bool tox_event_group_peer_limit_unpack( + Tox_Event_Group_Peer_Limit **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_peer_limit_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_peer_limit_unpack(event, bu); + return tox_event_group_peer_limit_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Peer_Limit *tox_event_group_peer_limit_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Group_Peer_Limit *group_peer_limit = tox_events_add_group_peer_limit(state->events, state->mem); + + if (group_peer_limit == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_peer_limit; } @@ -195,17 +221,9 @@ bool tox_events_unpack_group_peer_limit(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_peer_limit(Tox *tox, uint32_t group_number, uint32_t peer_limit, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Group_Peer_Limit *group_peer_limit = tox_events_add_group_peer_limit(state->events); + Tox_Event_Group_Peer_Limit *group_peer_limit = tox_event_group_peer_limit_alloc(user_data); if (group_peer_limit == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_peer_name.c b/toxcore/events/group_peer_name.c index 6de30457..56b67dd9 100644 --- a/toxcore/events/group_peer_name.c +++ b/toxcore/events/group_peer_name.c @@ -30,19 +30,6 @@ struct Tox_Event_Group_Peer_Name { uint32_t name_length; }; -non_null() -static void tox_event_group_peer_name_construct(Tox_Event_Group_Peer_Name *group_peer_name) -{ - *group_peer_name = (Tox_Event_Group_Peer_Name) { - 0 - }; -} -non_null() -static void tox_event_group_peer_name_destruct(Tox_Event_Group_Peer_Name *group_peer_name) -{ - free(group_peer_name->name); -} - non_null() static void tox_event_group_peer_name_set_group_number(Tox_Event_Group_Peer_Name *group_peer_name, uint32_t group_number) @@ -91,7 +78,7 @@ static bool tox_event_group_peer_name_set_name(Tox_Event_Group_Peer_Name *group_ group_peer_name->name_length = name_length; return true; } -size_t tox_event_group_peer_name_get_name_length(const Tox_Event_Group_Peer_Name *group_peer_name) +uint32_t tox_event_group_peer_name_get_name_length(const Tox_Event_Group_Peer_Name *group_peer_name) { assert(group_peer_name != nullptr); return group_peer_name->name_length; @@ -103,7 +90,19 @@ const uint8_t *tox_event_group_peer_name_get_name(const Tox_Event_Group_Peer_Nam } non_null() -static bool tox_event_group_peer_name_pack( +static void tox_event_group_peer_name_construct(Tox_Event_Group_Peer_Name *group_peer_name) +{ + *group_peer_name = (Tox_Event_Group_Peer_Name) { + 0 + }; +} +non_null() +static void tox_event_group_peer_name_destruct(Tox_Event_Group_Peer_Name *group_peer_name, const Memory *mem) +{ + free(group_peer_name->name); +} + +bool tox_event_group_peer_name_pack( const Tox_Event_Group_Peer_Name *event, Bin_Pack *bp) { assert(event != nullptr); @@ -116,7 +115,7 @@ static bool tox_event_group_peer_name_pack( } non_null() -static bool tox_event_group_peer_name_unpack( +static bool tox_event_group_peer_name_unpack_into( Tox_Event_Group_Peer_Name *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -132,93 +131,120 @@ static bool tox_event_group_peer_name_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Peer_Name *tox_events_add_group_peer_name(Tox_Events *events) +const Tox_Event_Group_Peer_Name *tox_event_get_group_peer_name(const Tox_Event *event) { - if (events->group_peer_name_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_PEER_NAME ? event->data.group_peer_name : nullptr; +} + +Tox_Event_Group_Peer_Name *tox_event_group_peer_name_new(const Memory *mem) +{ + Tox_Event_Group_Peer_Name *const group_peer_name = + (Tox_Event_Group_Peer_Name *)mem_alloc(mem, sizeof(Tox_Event_Group_Peer_Name)); + + if (group_peer_name == nullptr) { return nullptr; } - if (events->group_peer_name_size == events->group_peer_name_capacity) { - const uint32_t new_group_peer_name_capacity = events->group_peer_name_capacity * 2 + 1; - Tox_Event_Group_Peer_Name *new_group_peer_name = (Tox_Event_Group_Peer_Name *) - realloc( - events->group_peer_name, - new_group_peer_name_capacity * sizeof(Tox_Event_Group_Peer_Name)); - - if (new_group_peer_name == nullptr) { - return nullptr; - } - - events->group_peer_name = new_group_peer_name; - events->group_peer_name_capacity = new_group_peer_name_capacity; - } - - Tox_Event_Group_Peer_Name *const group_peer_name = - &events->group_peer_name[events->group_peer_name_size]; tox_event_group_peer_name_construct(group_peer_name); - ++events->group_peer_name_size; return group_peer_name; } -void tox_events_clear_group_peer_name(Tox_Events *events) +void tox_event_group_peer_name_free(Tox_Event_Group_Peer_Name *group_peer_name, const Memory *mem) { - if (events == nullptr) { - return; + if (group_peer_name != nullptr) { + tox_event_group_peer_name_destruct(group_peer_name, mem); } - - for (uint32_t i = 0; i < events->group_peer_name_size; ++i) { - tox_event_group_peer_name_destruct(&events->group_peer_name[i]); - } - - free(events->group_peer_name); - events->group_peer_name = nullptr; - events->group_peer_name_size = 0; - events->group_peer_name_capacity = 0; + mem_delete(mem, group_peer_name); } -uint32_t tox_events_get_group_peer_name_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Peer_Name *tox_events_add_group_peer_name(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Peer_Name *const group_peer_name = tox_event_group_peer_name_new(mem); + + if (group_peer_name == nullptr) { + return nullptr; } - return events->group_peer_name_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_PEER_NAME; + event.data.group_peer_name = group_peer_name; + + tox_events_add(events, &event); + return group_peer_name; } const Tox_Event_Group_Peer_Name *tox_events_get_group_peer_name(const Tox_Events *events, uint32_t index) { - assert(index < events->group_peer_name_size); - assert(events->group_peer_name != nullptr); - return &events->group_peer_name[index]; -} - -bool tox_events_pack_group_peer_name(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_peer_name_size(events); + uint32_t group_peer_name_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_peer_name_pack(tox_events_get_group_peer_name(events, i), bp)) { - return false; + if (group_peer_name_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PEER_NAME) { + const Tox_Event_Group_Peer_Name *group_peer_name = events->events[i].data.group_peer_name; + if (group_peer_name_index == index) { + return group_peer_name; + } + ++group_peer_name_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_peer_name(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_peer_name_size(const Tox_Events *events) { - Tox_Event_Group_Peer_Name *event = tox_events_add_group_peer_name(events); + uint32_t group_peer_name_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PEER_NAME) { + ++group_peer_name_size; + } + } + + return group_peer_name_size; +} + +bool tox_event_group_peer_name_unpack( + Tox_Event_Group_Peer_Name **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_peer_name_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_peer_name_unpack(event, bu); + return tox_event_group_peer_name_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Peer_Name *tox_event_group_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_Group_Peer_Name *group_peer_name = tox_events_add_group_peer_name(state->events, state->mem); + + if (group_peer_name == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_peer_name; } @@ -232,17 +258,9 @@ bool tox_events_unpack_group_peer_name(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_peer_name(Tox *tox, uint32_t group_number, uint32_t peer_id, 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_Group_Peer_Name *group_peer_name = tox_events_add_group_peer_name(state->events); + Tox_Event_Group_Peer_Name *group_peer_name = tox_event_group_peer_name_alloc(user_data); if (group_peer_name == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_peer_status.c b/toxcore/events/group_peer_status.c index 0a290e98..4fdefa94 100644 --- a/toxcore/events/group_peer_status.c +++ b/toxcore/events/group_peer_status.c @@ -29,19 +29,6 @@ struct Tox_Event_Group_Peer_Status { Tox_User_Status status; }; -non_null() -static void tox_event_group_peer_status_construct(Tox_Event_Group_Peer_Status *group_peer_status) -{ - *group_peer_status = (Tox_Event_Group_Peer_Status) { - 0 - }; -} -non_null() -static void tox_event_group_peer_status_destruct(Tox_Event_Group_Peer_Status *group_peer_status) -{ - return; -} - non_null() static void tox_event_group_peer_status_set_group_number(Tox_Event_Group_Peer_Status *group_peer_status, uint32_t group_number) @@ -82,7 +69,19 @@ Tox_User_Status tox_event_group_peer_status_get_status(const Tox_Event_Group_Pee } non_null() -static bool tox_event_group_peer_status_pack( +static void tox_event_group_peer_status_construct(Tox_Event_Group_Peer_Status *group_peer_status) +{ + *group_peer_status = (Tox_Event_Group_Peer_Status) { + 0 + }; +} +non_null() +static void tox_event_group_peer_status_destruct(Tox_Event_Group_Peer_Status *group_peer_status, const Memory *mem) +{ + return; +} + +bool tox_event_group_peer_status_pack( const Tox_Event_Group_Peer_Status *event, Bin_Pack *bp) { assert(event != nullptr); @@ -95,7 +94,7 @@ static bool tox_event_group_peer_status_pack( } non_null() -static bool tox_event_group_peer_status_unpack( +static bool tox_event_group_peer_status_unpack_into( Tox_Event_Group_Peer_Status *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -105,99 +104,126 @@ static bool tox_event_group_peer_status_unpack( return bin_unpack_u32(bu, &event->group_number) && bin_unpack_u32(bu, &event->peer_id) - && tox_unpack_user_status(bu, &event->status); + && tox_user_status_unpack(bu, &event->status); } /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Peer_Status *tox_events_add_group_peer_status(Tox_Events *events) +const Tox_Event_Group_Peer_Status *tox_event_get_group_peer_status(const Tox_Event *event) { - if (events->group_peer_status_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_PEER_STATUS ? event->data.group_peer_status : nullptr; +} + +Tox_Event_Group_Peer_Status *tox_event_group_peer_status_new(const Memory *mem) +{ + Tox_Event_Group_Peer_Status *const group_peer_status = + (Tox_Event_Group_Peer_Status *)mem_alloc(mem, sizeof(Tox_Event_Group_Peer_Status)); + + if (group_peer_status == nullptr) { return nullptr; } - if (events->group_peer_status_size == events->group_peer_status_capacity) { - const uint32_t new_group_peer_status_capacity = events->group_peer_status_capacity * 2 + 1; - Tox_Event_Group_Peer_Status *new_group_peer_status = (Tox_Event_Group_Peer_Status *) - realloc( - events->group_peer_status, - new_group_peer_status_capacity * sizeof(Tox_Event_Group_Peer_Status)); - - if (new_group_peer_status == nullptr) { - return nullptr; - } - - events->group_peer_status = new_group_peer_status; - events->group_peer_status_capacity = new_group_peer_status_capacity; - } - - Tox_Event_Group_Peer_Status *const group_peer_status = - &events->group_peer_status[events->group_peer_status_size]; tox_event_group_peer_status_construct(group_peer_status); - ++events->group_peer_status_size; return group_peer_status; } -void tox_events_clear_group_peer_status(Tox_Events *events) +void tox_event_group_peer_status_free(Tox_Event_Group_Peer_Status *group_peer_status, const Memory *mem) { - if (events == nullptr) { - return; + if (group_peer_status != nullptr) { + tox_event_group_peer_status_destruct(group_peer_status, mem); } - - for (uint32_t i = 0; i < events->group_peer_status_size; ++i) { - tox_event_group_peer_status_destruct(&events->group_peer_status[i]); - } - - free(events->group_peer_status); - events->group_peer_status = nullptr; - events->group_peer_status_size = 0; - events->group_peer_status_capacity = 0; + mem_delete(mem, group_peer_status); } -uint32_t tox_events_get_group_peer_status_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Peer_Status *tox_events_add_group_peer_status(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Peer_Status *const group_peer_status = tox_event_group_peer_status_new(mem); + + if (group_peer_status == nullptr) { + return nullptr; } - return events->group_peer_status_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_PEER_STATUS; + event.data.group_peer_status = group_peer_status; + + tox_events_add(events, &event); + return group_peer_status; } const Tox_Event_Group_Peer_Status *tox_events_get_group_peer_status(const Tox_Events *events, uint32_t index) { - assert(index < events->group_peer_status_size); - assert(events->group_peer_status != nullptr); - return &events->group_peer_status[index]; -} - -bool tox_events_pack_group_peer_status(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_peer_status_size(events); + uint32_t group_peer_status_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_peer_status_pack(tox_events_get_group_peer_status(events, i), bp)) { - return false; + if (group_peer_status_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PEER_STATUS) { + const Tox_Event_Group_Peer_Status *group_peer_status = events->events[i].data.group_peer_status; + if (group_peer_status_index == index) { + return group_peer_status; + } + ++group_peer_status_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_peer_status(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_peer_status_size(const Tox_Events *events) { - Tox_Event_Group_Peer_Status *event = tox_events_add_group_peer_status(events); + uint32_t group_peer_status_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PEER_STATUS) { + ++group_peer_status_size; + } + } + + return group_peer_status_size; +} + +bool tox_event_group_peer_status_unpack( + Tox_Event_Group_Peer_Status **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_peer_status_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_peer_status_unpack(event, bu); + return tox_event_group_peer_status_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Peer_Status *tox_event_group_peer_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_Group_Peer_Status *group_peer_status = tox_events_add_group_peer_status(state->events, state->mem); + + if (group_peer_status == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_peer_status; } @@ -211,17 +237,9 @@ bool tox_events_unpack_group_peer_status(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_peer_status(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_User_Status status, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Group_Peer_Status *group_peer_status = tox_events_add_group_peer_status(state->events); + Tox_Event_Group_Peer_Status *group_peer_status = tox_event_group_peer_status_alloc(user_data); if (group_peer_status == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_privacy_state.c b/toxcore/events/group_privacy_state.c index dfad858a..b0b4967d 100644 --- a/toxcore/events/group_privacy_state.c +++ b/toxcore/events/group_privacy_state.c @@ -28,19 +28,6 @@ struct Tox_Event_Group_Privacy_State { Tox_Group_Privacy_State privacy_state; }; -non_null() -static void tox_event_group_privacy_state_construct(Tox_Event_Group_Privacy_State *group_privacy_state) -{ - *group_privacy_state = (Tox_Event_Group_Privacy_State) { - 0 - }; -} -non_null() -static void tox_event_group_privacy_state_destruct(Tox_Event_Group_Privacy_State *group_privacy_state) -{ - return; -} - non_null() static void tox_event_group_privacy_state_set_group_number(Tox_Event_Group_Privacy_State *group_privacy_state, uint32_t group_number) @@ -68,7 +55,19 @@ Tox_Group_Privacy_State tox_event_group_privacy_state_get_privacy_state(const To } non_null() -static bool tox_event_group_privacy_state_pack( +static void tox_event_group_privacy_state_construct(Tox_Event_Group_Privacy_State *group_privacy_state) +{ + *group_privacy_state = (Tox_Event_Group_Privacy_State) { + 0 + }; +} +non_null() +static void tox_event_group_privacy_state_destruct(Tox_Event_Group_Privacy_State *group_privacy_state, const Memory *mem) +{ + return; +} + +bool tox_event_group_privacy_state_pack( const Tox_Event_Group_Privacy_State *event, Bin_Pack *bp) { assert(event != nullptr); @@ -80,7 +79,7 @@ static bool tox_event_group_privacy_state_pack( } non_null() -static bool tox_event_group_privacy_state_unpack( +static bool tox_event_group_privacy_state_unpack_into( Tox_Event_Group_Privacy_State *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -89,99 +88,126 @@ static bool tox_event_group_privacy_state_unpack( } return bin_unpack_u32(bu, &event->group_number) - && tox_unpack_group_privacy_state(bu, &event->privacy_state); + && tox_group_privacy_state_unpack(bu, &event->privacy_state); } /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Privacy_State *tox_events_add_group_privacy_state(Tox_Events *events) +const Tox_Event_Group_Privacy_State *tox_event_get_group_privacy_state(const Tox_Event *event) { - if (events->group_privacy_state_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_PRIVACY_STATE ? event->data.group_privacy_state : nullptr; +} + +Tox_Event_Group_Privacy_State *tox_event_group_privacy_state_new(const Memory *mem) +{ + Tox_Event_Group_Privacy_State *const group_privacy_state = + (Tox_Event_Group_Privacy_State *)mem_alloc(mem, sizeof(Tox_Event_Group_Privacy_State)); + + if (group_privacy_state == nullptr) { return nullptr; } - if (events->group_privacy_state_size == events->group_privacy_state_capacity) { - const uint32_t new_group_privacy_state_capacity = events->group_privacy_state_capacity * 2 + 1; - Tox_Event_Group_Privacy_State *new_group_privacy_state = (Tox_Event_Group_Privacy_State *) - realloc( - events->group_privacy_state, - new_group_privacy_state_capacity * sizeof(Tox_Event_Group_Privacy_State)); - - if (new_group_privacy_state == nullptr) { - return nullptr; - } - - events->group_privacy_state = new_group_privacy_state; - events->group_privacy_state_capacity = new_group_privacy_state_capacity; - } - - Tox_Event_Group_Privacy_State *const group_privacy_state = - &events->group_privacy_state[events->group_privacy_state_size]; tox_event_group_privacy_state_construct(group_privacy_state); - ++events->group_privacy_state_size; return group_privacy_state; } -void tox_events_clear_group_privacy_state(Tox_Events *events) +void tox_event_group_privacy_state_free(Tox_Event_Group_Privacy_State *group_privacy_state, const Memory *mem) { - if (events == nullptr) { - return; + if (group_privacy_state != nullptr) { + tox_event_group_privacy_state_destruct(group_privacy_state, mem); } - - for (uint32_t i = 0; i < events->group_privacy_state_size; ++i) { - tox_event_group_privacy_state_destruct(&events->group_privacy_state[i]); - } - - free(events->group_privacy_state); - events->group_privacy_state = nullptr; - events->group_privacy_state_size = 0; - events->group_privacy_state_capacity = 0; + mem_delete(mem, group_privacy_state); } -uint32_t tox_events_get_group_privacy_state_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Privacy_State *tox_events_add_group_privacy_state(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Privacy_State *const group_privacy_state = tox_event_group_privacy_state_new(mem); + + if (group_privacy_state == nullptr) { + return nullptr; } - return events->group_privacy_state_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_PRIVACY_STATE; + event.data.group_privacy_state = group_privacy_state; + + tox_events_add(events, &event); + return group_privacy_state; } const Tox_Event_Group_Privacy_State *tox_events_get_group_privacy_state(const Tox_Events *events, uint32_t index) { - assert(index < events->group_privacy_state_size); - assert(events->group_privacy_state != nullptr); - return &events->group_privacy_state[index]; -} - -bool tox_events_pack_group_privacy_state(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_privacy_state_size(events); + uint32_t group_privacy_state_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_privacy_state_pack(tox_events_get_group_privacy_state(events, i), bp)) { - return false; + if (group_privacy_state_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PRIVACY_STATE) { + const Tox_Event_Group_Privacy_State *group_privacy_state = events->events[i].data.group_privacy_state; + if (group_privacy_state_index == index) { + return group_privacy_state; + } + ++group_privacy_state_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_privacy_state(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_privacy_state_size(const Tox_Events *events) { - Tox_Event_Group_Privacy_State *event = tox_events_add_group_privacy_state(events); + uint32_t group_privacy_state_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PRIVACY_STATE) { + ++group_privacy_state_size; + } + } + + return group_privacy_state_size; +} + +bool tox_event_group_privacy_state_unpack( + Tox_Event_Group_Privacy_State **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_privacy_state_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_privacy_state_unpack(event, bu); + return tox_event_group_privacy_state_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Privacy_State *tox_event_group_privacy_state_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Group_Privacy_State *group_privacy_state = tox_events_add_group_privacy_state(state->events, state->mem); + + if (group_privacy_state == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_privacy_state; } @@ -195,17 +221,9 @@ bool tox_events_unpack_group_privacy_state(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_privacy_state(Tox *tox, uint32_t group_number, Tox_Group_Privacy_State privacy_state, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Group_Privacy_State *group_privacy_state = tox_events_add_group_privacy_state(state->events); + Tox_Event_Group_Privacy_State *group_privacy_state = tox_event_group_privacy_state_alloc(user_data); if (group_privacy_state == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_private_message.c b/toxcore/events/group_private_message.c index 9fed263a..9b588add 100644 --- a/toxcore/events/group_private_message.c +++ b/toxcore/events/group_private_message.c @@ -31,19 +31,6 @@ struct Tox_Event_Group_Private_Message { uint32_t message_length; }; -non_null() -static void tox_event_group_private_message_construct(Tox_Event_Group_Private_Message *group_private_message) -{ - *group_private_message = (Tox_Event_Group_Private_Message) { - 0 - }; -} -non_null() -static void tox_event_group_private_message_destruct(Tox_Event_Group_Private_Message *group_private_message) -{ - free(group_private_message->message); -} - non_null() static void tox_event_group_private_message_set_group_number(Tox_Event_Group_Private_Message *group_private_message, uint32_t group_number) @@ -105,7 +92,7 @@ static bool tox_event_group_private_message_set_message(Tox_Event_Group_Private_ group_private_message->message_length = message_length; return true; } -size_t tox_event_group_private_message_get_message_length(const Tox_Event_Group_Private_Message *group_private_message) +uint32_t tox_event_group_private_message_get_message_length(const Tox_Event_Group_Private_Message *group_private_message) { assert(group_private_message != nullptr); return group_private_message->message_length; @@ -117,7 +104,19 @@ const uint8_t *tox_event_group_private_message_get_message(const Tox_Event_Group } non_null() -static bool tox_event_group_private_message_pack( +static void tox_event_group_private_message_construct(Tox_Event_Group_Private_Message *group_private_message) +{ + *group_private_message = (Tox_Event_Group_Private_Message) { + 0 + }; +} +non_null() +static void tox_event_group_private_message_destruct(Tox_Event_Group_Private_Message *group_private_message, const Memory *mem) +{ + free(group_private_message->message); +} + +bool tox_event_group_private_message_pack( const Tox_Event_Group_Private_Message *event, Bin_Pack *bp) { assert(event != nullptr); @@ -131,7 +130,7 @@ static bool tox_event_group_private_message_pack( } non_null() -static bool tox_event_group_private_message_unpack( +static bool tox_event_group_private_message_unpack_into( Tox_Event_Group_Private_Message *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -141,100 +140,127 @@ static bool tox_event_group_private_message_unpack( return bin_unpack_u32(bu, &event->group_number) && bin_unpack_u32(bu, &event->peer_id) - && tox_unpack_message_type(bu, &event->type) + && tox_message_type_unpack(bu, &event->type) && bin_unpack_bin(bu, &event->message, &event->message_length); } /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Private_Message *tox_events_add_group_private_message(Tox_Events *events) +const Tox_Event_Group_Private_Message *tox_event_get_group_private_message(const Tox_Event *event) { - if (events->group_private_message_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_PRIVATE_MESSAGE ? event->data.group_private_message : nullptr; +} + +Tox_Event_Group_Private_Message *tox_event_group_private_message_new(const Memory *mem) +{ + Tox_Event_Group_Private_Message *const group_private_message = + (Tox_Event_Group_Private_Message *)mem_alloc(mem, sizeof(Tox_Event_Group_Private_Message)); + + if (group_private_message == nullptr) { return nullptr; } - if (events->group_private_message_size == events->group_private_message_capacity) { - const uint32_t new_group_private_message_capacity = events->group_private_message_capacity * 2 + 1; - Tox_Event_Group_Private_Message *new_group_private_message = (Tox_Event_Group_Private_Message *) - realloc( - events->group_private_message, - new_group_private_message_capacity * sizeof(Tox_Event_Group_Private_Message)); - - if (new_group_private_message == nullptr) { - return nullptr; - } - - events->group_private_message = new_group_private_message; - events->group_private_message_capacity = new_group_private_message_capacity; - } - - Tox_Event_Group_Private_Message *const group_private_message = - &events->group_private_message[events->group_private_message_size]; tox_event_group_private_message_construct(group_private_message); - ++events->group_private_message_size; return group_private_message; } -void tox_events_clear_group_private_message(Tox_Events *events) +void tox_event_group_private_message_free(Tox_Event_Group_Private_Message *group_private_message, const Memory *mem) { - if (events == nullptr) { - return; + if (group_private_message != nullptr) { + tox_event_group_private_message_destruct(group_private_message, mem); } - - for (uint32_t i = 0; i < events->group_private_message_size; ++i) { - tox_event_group_private_message_destruct(&events->group_private_message[i]); - } - - free(events->group_private_message); - events->group_private_message = nullptr; - events->group_private_message_size = 0; - events->group_private_message_capacity = 0; + mem_delete(mem, group_private_message); } -uint32_t tox_events_get_group_private_message_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Private_Message *tox_events_add_group_private_message(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Private_Message *const group_private_message = tox_event_group_private_message_new(mem); + + if (group_private_message == nullptr) { + return nullptr; } - return events->group_private_message_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_PRIVATE_MESSAGE; + event.data.group_private_message = group_private_message; + + tox_events_add(events, &event); + return group_private_message; } const Tox_Event_Group_Private_Message *tox_events_get_group_private_message(const Tox_Events *events, uint32_t index) { - assert(index < events->group_private_message_size); - assert(events->group_private_message != nullptr); - return &events->group_private_message[index]; -} - -bool tox_events_pack_group_private_message(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_private_message_size(events); + uint32_t group_private_message_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_private_message_pack(tox_events_get_group_private_message(events, i), bp)) { - return false; + if (group_private_message_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_PRIVATE_MESSAGE) { + const Tox_Event_Group_Private_Message *group_private_message = events->events[i].data.group_private_message; + if (group_private_message_index == index) { + return group_private_message; + } + ++group_private_message_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_private_message(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_private_message_size(const Tox_Events *events) { - Tox_Event_Group_Private_Message *event = tox_events_add_group_private_message(events); + uint32_t group_private_message_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_PRIVATE_MESSAGE) { + ++group_private_message_size; + } + } + + return group_private_message_size; +} + +bool tox_event_group_private_message_unpack( + Tox_Event_Group_Private_Message **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_private_message_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_private_message_unpack(event, bu); + return tox_event_group_private_message_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Private_Message *tox_event_group_private_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_Group_Private_Message *group_private_message = tox_events_add_group_private_message(state->events, state->mem); + + if (group_private_message == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_private_message; } @@ -248,17 +274,9 @@ bool tox_events_unpack_group_private_message(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_private_message(Tox *tox, uint32_t group_number, uint32_t peer_id, 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_Group_Private_Message *group_private_message = tox_events_add_group_private_message(state->events); + Tox_Event_Group_Private_Message *group_private_message = tox_event_group_private_message_alloc(user_data); if (group_private_message == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_self_join.c b/toxcore/events/group_self_join.c index c4f78d0e..a8ad880f 100644 --- a/toxcore/events/group_self_join.c +++ b/toxcore/events/group_self_join.c @@ -27,19 +27,6 @@ struct Tox_Event_Group_Self_Join { uint32_t group_number; }; -non_null() -static void tox_event_group_self_join_construct(Tox_Event_Group_Self_Join *group_self_join) -{ - *group_self_join = (Tox_Event_Group_Self_Join) { - 0 - }; -} -non_null() -static void tox_event_group_self_join_destruct(Tox_Event_Group_Self_Join *group_self_join) -{ - return; -} - non_null() static void tox_event_group_self_join_set_group_number(Tox_Event_Group_Self_Join *group_self_join, uint32_t group_number) @@ -54,18 +41,29 @@ uint32_t tox_event_group_self_join_get_group_number(const Tox_Event_Group_Self_J } non_null() -static bool tox_event_group_self_join_pack( +static void tox_event_group_self_join_construct(Tox_Event_Group_Self_Join *group_self_join) +{ + *group_self_join = (Tox_Event_Group_Self_Join) { + 0 + }; +} +non_null() +static void tox_event_group_self_join_destruct(Tox_Event_Group_Self_Join *group_self_join, const Memory *mem) +{ + return; +} + +bool tox_event_group_self_join_pack( const Tox_Event_Group_Self_Join *event, Bin_Pack *bp) { assert(event != nullptr); return bin_pack_array(bp, 2) && bin_pack_u32(bp, TOX_EVENT_GROUP_SELF_JOIN) - && bin_pack_u32(bp, event->group_number); } non_null() -static bool tox_event_group_self_join_unpack( +static bool tox_event_group_self_join_unpack_into( Tox_Event_Group_Self_Join *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -75,93 +73,120 @@ static bool tox_event_group_self_join_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Self_Join *tox_events_add_group_self_join(Tox_Events *events) +const Tox_Event_Group_Self_Join *tox_event_get_group_self_join(const Tox_Event *event) { - if (events->group_self_join_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_SELF_JOIN ? event->data.group_self_join : nullptr; +} + +Tox_Event_Group_Self_Join *tox_event_group_self_join_new(const Memory *mem) +{ + Tox_Event_Group_Self_Join *const group_self_join = + (Tox_Event_Group_Self_Join *)mem_alloc(mem, sizeof(Tox_Event_Group_Self_Join)); + + if (group_self_join == nullptr) { return nullptr; } - if (events->group_self_join_size == events->group_self_join_capacity) { - const uint32_t new_group_self_join_capacity = events->group_self_join_capacity * 2 + 1; - Tox_Event_Group_Self_Join *new_group_self_join = (Tox_Event_Group_Self_Join *) - realloc( - events->group_self_join, - new_group_self_join_capacity * sizeof(Tox_Event_Group_Self_Join)); - - if (new_group_self_join == nullptr) { - return nullptr; - } - - events->group_self_join = new_group_self_join; - events->group_self_join_capacity = new_group_self_join_capacity; - } - - Tox_Event_Group_Self_Join *const group_self_join = - &events->group_self_join[events->group_self_join_size]; tox_event_group_self_join_construct(group_self_join); - ++events->group_self_join_size; return group_self_join; } -void tox_events_clear_group_self_join(Tox_Events *events) +void tox_event_group_self_join_free(Tox_Event_Group_Self_Join *group_self_join, const Memory *mem) { - if (events == nullptr) { - return; + if (group_self_join != nullptr) { + tox_event_group_self_join_destruct(group_self_join, mem); } - - for (uint32_t i = 0; i < events->group_self_join_size; ++i) { - tox_event_group_self_join_destruct(&events->group_self_join[i]); - } - - free(events->group_self_join); - events->group_self_join = nullptr; - events->group_self_join_size = 0; - events->group_self_join_capacity = 0; + mem_delete(mem, group_self_join); } -uint32_t tox_events_get_group_self_join_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Self_Join *tox_events_add_group_self_join(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Self_Join *const group_self_join = tox_event_group_self_join_new(mem); + + if (group_self_join == nullptr) { + return nullptr; } - return events->group_self_join_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_SELF_JOIN; + event.data.group_self_join = group_self_join; + + tox_events_add(events, &event); + return group_self_join; } const Tox_Event_Group_Self_Join *tox_events_get_group_self_join(const Tox_Events *events, uint32_t index) { - assert(index < events->group_self_join_size); - assert(events->group_self_join != nullptr); - return &events->group_self_join[index]; -} - -bool tox_events_pack_group_self_join(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_self_join_size(events); + uint32_t group_self_join_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_self_join_pack(tox_events_get_group_self_join(events, i), bp)) { - return false; + if (group_self_join_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_SELF_JOIN) { + const Tox_Event_Group_Self_Join *group_self_join = events->events[i].data.group_self_join; + if (group_self_join_index == index) { + return group_self_join; + } + ++group_self_join_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_self_join(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_self_join_size(const Tox_Events *events) { - Tox_Event_Group_Self_Join *event = tox_events_add_group_self_join(events); + uint32_t group_self_join_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_SELF_JOIN) { + ++group_self_join_size; + } + } + + return group_self_join_size; +} + +bool tox_event_group_self_join_unpack( + Tox_Event_Group_Self_Join **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_self_join_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_self_join_unpack(event, bu); + return tox_event_group_self_join_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Self_Join *tox_event_group_self_join_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Group_Self_Join *group_self_join = tox_events_add_group_self_join(state->events, state->mem); + + if (group_self_join == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_self_join; } @@ -175,17 +200,9 @@ bool tox_events_unpack_group_self_join(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_self_join(Tox *tox, uint32_t group_number, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Group_Self_Join *group_self_join = tox_events_add_group_self_join(state->events); + Tox_Event_Group_Self_Join *group_self_join = tox_event_group_self_join_alloc(user_data); if (group_self_join == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_topic.c b/toxcore/events/group_topic.c index ca213e9a..fab0a8a4 100644 --- a/toxcore/events/group_topic.c +++ b/toxcore/events/group_topic.c @@ -30,19 +30,6 @@ struct Tox_Event_Group_Topic { uint32_t topic_length; }; -non_null() -static void tox_event_group_topic_construct(Tox_Event_Group_Topic *group_topic) -{ - *group_topic = (Tox_Event_Group_Topic) { - 0 - }; -} -non_null() -static void tox_event_group_topic_destruct(Tox_Event_Group_Topic *group_topic) -{ - free(group_topic->topic); -} - non_null() static void tox_event_group_topic_set_group_number(Tox_Event_Group_Topic *group_topic, uint32_t group_number) @@ -91,7 +78,7 @@ static bool tox_event_group_topic_set_topic(Tox_Event_Group_Topic *group_topic, group_topic->topic_length = topic_length; return true; } -size_t tox_event_group_topic_get_topic_length(const Tox_Event_Group_Topic *group_topic) +uint32_t tox_event_group_topic_get_topic_length(const Tox_Event_Group_Topic *group_topic) { assert(group_topic != nullptr); return group_topic->topic_length; @@ -103,7 +90,19 @@ const uint8_t *tox_event_group_topic_get_topic(const Tox_Event_Group_Topic *grou } non_null() -static bool tox_event_group_topic_pack( +static void tox_event_group_topic_construct(Tox_Event_Group_Topic *group_topic) +{ + *group_topic = (Tox_Event_Group_Topic) { + 0 + }; +} +non_null() +static void tox_event_group_topic_destruct(Tox_Event_Group_Topic *group_topic, const Memory *mem) +{ + free(group_topic->topic); +} + +bool tox_event_group_topic_pack( const Tox_Event_Group_Topic *event, Bin_Pack *bp) { assert(event != nullptr); @@ -116,7 +115,7 @@ static bool tox_event_group_topic_pack( } non_null() -static bool tox_event_group_topic_unpack( +static bool tox_event_group_topic_unpack_into( Tox_Event_Group_Topic *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -132,93 +131,120 @@ static bool tox_event_group_topic_unpack( /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Topic *tox_events_add_group_topic(Tox_Events *events) +const Tox_Event_Group_Topic *tox_event_get_group_topic(const Tox_Event *event) { - if (events->group_topic_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_TOPIC ? event->data.group_topic : nullptr; +} + +Tox_Event_Group_Topic *tox_event_group_topic_new(const Memory *mem) +{ + Tox_Event_Group_Topic *const group_topic = + (Tox_Event_Group_Topic *)mem_alloc(mem, sizeof(Tox_Event_Group_Topic)); + + if (group_topic == nullptr) { return nullptr; } - if (events->group_topic_size == events->group_topic_capacity) { - const uint32_t new_group_topic_capacity = events->group_topic_capacity * 2 + 1; - Tox_Event_Group_Topic *new_group_topic = (Tox_Event_Group_Topic *) - realloc( - events->group_topic, - new_group_topic_capacity * sizeof(Tox_Event_Group_Topic)); - - if (new_group_topic == nullptr) { - return nullptr; - } - - events->group_topic = new_group_topic; - events->group_topic_capacity = new_group_topic_capacity; - } - - Tox_Event_Group_Topic *const group_topic = - &events->group_topic[events->group_topic_size]; tox_event_group_topic_construct(group_topic); - ++events->group_topic_size; return group_topic; } -void tox_events_clear_group_topic(Tox_Events *events) +void tox_event_group_topic_free(Tox_Event_Group_Topic *group_topic, const Memory *mem) { - if (events == nullptr) { - return; + if (group_topic != nullptr) { + tox_event_group_topic_destruct(group_topic, mem); } - - for (uint32_t i = 0; i < events->group_topic_size; ++i) { - tox_event_group_topic_destruct(&events->group_topic[i]); - } - - free(events->group_topic); - events->group_topic = nullptr; - events->group_topic_size = 0; - events->group_topic_capacity = 0; + mem_delete(mem, group_topic); } -uint32_t tox_events_get_group_topic_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Topic *tox_events_add_group_topic(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Topic *const group_topic = tox_event_group_topic_new(mem); + + if (group_topic == nullptr) { + return nullptr; } - return events->group_topic_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_TOPIC; + event.data.group_topic = group_topic; + + tox_events_add(events, &event); + return group_topic; } const Tox_Event_Group_Topic *tox_events_get_group_topic(const Tox_Events *events, uint32_t index) { - assert(index < events->group_topic_size); - assert(events->group_topic != nullptr); - return &events->group_topic[index]; -} - -bool tox_events_pack_group_topic(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_topic_size(events); + uint32_t group_topic_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_topic_pack(tox_events_get_group_topic(events, i), bp)) { - return false; + if (group_topic_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_TOPIC) { + const Tox_Event_Group_Topic *group_topic = events->events[i].data.group_topic; + if (group_topic_index == index) { + return group_topic; + } + ++group_topic_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_topic(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_topic_size(const Tox_Events *events) { - Tox_Event_Group_Topic *event = tox_events_add_group_topic(events); + uint32_t group_topic_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_TOPIC) { + ++group_topic_size; + } + } + + return group_topic_size; +} + +bool tox_event_group_topic_unpack( + Tox_Event_Group_Topic **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_topic_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_topic_unpack(event, bu); + return tox_event_group_topic_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Topic *tox_event_group_topic_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Group_Topic *group_topic = tox_events_add_group_topic(state->events, state->mem); + + if (group_topic == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_topic; } @@ -232,17 +258,9 @@ bool tox_events_unpack_group_topic(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_topic(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *topic, 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_Group_Topic *group_topic = tox_events_add_group_topic(state->events); + Tox_Event_Group_Topic *group_topic = tox_event_group_topic_alloc(user_data); if (group_topic == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_topic_lock.c b/toxcore/events/group_topic_lock.c index b9c2c27c..a628f8ae 100644 --- a/toxcore/events/group_topic_lock.c +++ b/toxcore/events/group_topic_lock.c @@ -28,19 +28,6 @@ struct Tox_Event_Group_Topic_Lock { Tox_Group_Topic_Lock topic_lock; }; -non_null() -static void tox_event_group_topic_lock_construct(Tox_Event_Group_Topic_Lock *group_topic_lock) -{ - *group_topic_lock = (Tox_Event_Group_Topic_Lock) { - 0 - }; -} -non_null() -static void tox_event_group_topic_lock_destruct(Tox_Event_Group_Topic_Lock *group_topic_lock) -{ - return; -} - non_null() static void tox_event_group_topic_lock_set_group_number(Tox_Event_Group_Topic_Lock *group_topic_lock, uint32_t group_number) @@ -68,7 +55,19 @@ Tox_Group_Topic_Lock tox_event_group_topic_lock_get_topic_lock(const Tox_Event_G } non_null() -static bool tox_event_group_topic_lock_pack( +static void tox_event_group_topic_lock_construct(Tox_Event_Group_Topic_Lock *group_topic_lock) +{ + *group_topic_lock = (Tox_Event_Group_Topic_Lock) { + 0 + }; +} +non_null() +static void tox_event_group_topic_lock_destruct(Tox_Event_Group_Topic_Lock *group_topic_lock, const Memory *mem) +{ + return; +} + +bool tox_event_group_topic_lock_pack( const Tox_Event_Group_Topic_Lock *event, Bin_Pack *bp) { assert(event != nullptr); @@ -80,7 +79,7 @@ static bool tox_event_group_topic_lock_pack( } non_null() -static bool tox_event_group_topic_lock_unpack( +static bool tox_event_group_topic_lock_unpack_into( Tox_Event_Group_Topic_Lock *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -89,99 +88,126 @@ static bool tox_event_group_topic_lock_unpack( } return bin_unpack_u32(bu, &event->group_number) - && tox_unpack_group_topic_lock(bu, &event->topic_lock); + && tox_group_topic_lock_unpack(bu, &event->topic_lock); } /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Topic_Lock *tox_events_add_group_topic_lock(Tox_Events *events) +const Tox_Event_Group_Topic_Lock *tox_event_get_group_topic_lock(const Tox_Event *event) { - if (events->group_topic_lock_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_TOPIC_LOCK ? event->data.group_topic_lock : nullptr; +} + +Tox_Event_Group_Topic_Lock *tox_event_group_topic_lock_new(const Memory *mem) +{ + Tox_Event_Group_Topic_Lock *const group_topic_lock = + (Tox_Event_Group_Topic_Lock *)mem_alloc(mem, sizeof(Tox_Event_Group_Topic_Lock)); + + if (group_topic_lock == nullptr) { return nullptr; } - if (events->group_topic_lock_size == events->group_topic_lock_capacity) { - const uint32_t new_group_topic_lock_capacity = events->group_topic_lock_capacity * 2 + 1; - Tox_Event_Group_Topic_Lock *new_group_topic_lock = (Tox_Event_Group_Topic_Lock *) - realloc( - events->group_topic_lock, - new_group_topic_lock_capacity * sizeof(Tox_Event_Group_Topic_Lock)); - - if (new_group_topic_lock == nullptr) { - return nullptr; - } - - events->group_topic_lock = new_group_topic_lock; - events->group_topic_lock_capacity = new_group_topic_lock_capacity; - } - - Tox_Event_Group_Topic_Lock *const group_topic_lock = - &events->group_topic_lock[events->group_topic_lock_size]; tox_event_group_topic_lock_construct(group_topic_lock); - ++events->group_topic_lock_size; return group_topic_lock; } -void tox_events_clear_group_topic_lock(Tox_Events *events) +void tox_event_group_topic_lock_free(Tox_Event_Group_Topic_Lock *group_topic_lock, const Memory *mem) { - if (events == nullptr) { - return; + if (group_topic_lock != nullptr) { + tox_event_group_topic_lock_destruct(group_topic_lock, mem); } - - for (uint32_t i = 0; i < events->group_topic_lock_size; ++i) { - tox_event_group_topic_lock_destruct(&events->group_topic_lock[i]); - } - - free(events->group_topic_lock); - events->group_topic_lock = nullptr; - events->group_topic_lock_size = 0; - events->group_topic_lock_capacity = 0; + mem_delete(mem, group_topic_lock); } -uint32_t tox_events_get_group_topic_lock_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Topic_Lock *tox_events_add_group_topic_lock(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Topic_Lock *const group_topic_lock = tox_event_group_topic_lock_new(mem); + + if (group_topic_lock == nullptr) { + return nullptr; } - return events->group_topic_lock_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_TOPIC_LOCK; + event.data.group_topic_lock = group_topic_lock; + + tox_events_add(events, &event); + return group_topic_lock; } const Tox_Event_Group_Topic_Lock *tox_events_get_group_topic_lock(const Tox_Events *events, uint32_t index) { - assert(index < events->group_topic_lock_size); - assert(events->group_topic_lock != nullptr); - return &events->group_topic_lock[index]; -} - -bool tox_events_pack_group_topic_lock(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_topic_lock_size(events); + uint32_t group_topic_lock_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_topic_lock_pack(tox_events_get_group_topic_lock(events, i), bp)) { - return false; + if (group_topic_lock_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_TOPIC_LOCK) { + const Tox_Event_Group_Topic_Lock *group_topic_lock = events->events[i].data.group_topic_lock; + if (group_topic_lock_index == index) { + return group_topic_lock; + } + ++group_topic_lock_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_topic_lock(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_topic_lock_size(const Tox_Events *events) { - Tox_Event_Group_Topic_Lock *event = tox_events_add_group_topic_lock(events); + uint32_t group_topic_lock_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_TOPIC_LOCK) { + ++group_topic_lock_size; + } + } + + return group_topic_lock_size; +} + +bool tox_event_group_topic_lock_unpack( + Tox_Event_Group_Topic_Lock **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_topic_lock_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_topic_lock_unpack(event, bu); + return tox_event_group_topic_lock_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Topic_Lock *tox_event_group_topic_lock_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Group_Topic_Lock *group_topic_lock = tox_events_add_group_topic_lock(state->events, state->mem); + + if (group_topic_lock == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_topic_lock; } @@ -195,17 +221,9 @@ bool tox_events_unpack_group_topic_lock(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_topic_lock(Tox *tox, uint32_t group_number, Tox_Group_Topic_Lock topic_lock, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Group_Topic_Lock *group_topic_lock = tox_events_add_group_topic_lock(state->events); + Tox_Event_Group_Topic_Lock *group_topic_lock = tox_event_group_topic_lock_alloc(user_data); if (group_topic_lock == nullptr) { - state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; return; } diff --git a/toxcore/events/group_voice_state.c b/toxcore/events/group_voice_state.c index efc9d135..c1c402d8 100644 --- a/toxcore/events/group_voice_state.c +++ b/toxcore/events/group_voice_state.c @@ -28,19 +28,6 @@ struct Tox_Event_Group_Voice_State { Tox_Group_Voice_State voice_state; }; -non_null() -static void tox_event_group_voice_state_construct(Tox_Event_Group_Voice_State *group_voice_state) -{ - *group_voice_state = (Tox_Event_Group_Voice_State) { - 0 - }; -} -non_null() -static void tox_event_group_voice_state_destruct(Tox_Event_Group_Voice_State *group_voice_state) -{ - return; -} - non_null() static void tox_event_group_voice_state_set_group_number(Tox_Event_Group_Voice_State *group_voice_state, uint32_t group_number) @@ -68,7 +55,19 @@ Tox_Group_Voice_State tox_event_group_voice_state_get_voice_state(const Tox_Even } non_null() -static bool tox_event_group_voice_state_pack( +static void tox_event_group_voice_state_construct(Tox_Event_Group_Voice_State *group_voice_state) +{ + *group_voice_state = (Tox_Event_Group_Voice_State) { + 0 + }; +} +non_null() +static void tox_event_group_voice_state_destruct(Tox_Event_Group_Voice_State *group_voice_state, const Memory *mem) +{ + return; +} + +bool tox_event_group_voice_state_pack( const Tox_Event_Group_Voice_State *event, Bin_Pack *bp) { assert(event != nullptr); @@ -80,7 +79,7 @@ static bool tox_event_group_voice_state_pack( } non_null() -static bool tox_event_group_voice_state_unpack( +static bool tox_event_group_voice_state_unpack_into( Tox_Event_Group_Voice_State *event, Bin_Unpack *bu) { assert(event != nullptr); @@ -89,99 +88,126 @@ static bool tox_event_group_voice_state_unpack( } return bin_unpack_u32(bu, &event->group_number) - && tox_unpack_group_voice_state(bu, &event->voice_state); + && tox_group_voice_state_unpack(bu, &event->voice_state); } /***************************************************** * - * :: add/clear/get + * :: new/free/add/get/size/unpack * *****************************************************/ - -non_null() -static Tox_Event_Group_Voice_State *tox_events_add_group_voice_state(Tox_Events *events) +const Tox_Event_Group_Voice_State *tox_event_get_group_voice_state(const Tox_Event *event) { - if (events->group_voice_state_size == UINT32_MAX) { + return event->type == TOX_EVENT_GROUP_VOICE_STATE ? event->data.group_voice_state : nullptr; +} + +Tox_Event_Group_Voice_State *tox_event_group_voice_state_new(const Memory *mem) +{ + Tox_Event_Group_Voice_State *const group_voice_state = + (Tox_Event_Group_Voice_State *)mem_alloc(mem, sizeof(Tox_Event_Group_Voice_State)); + + if (group_voice_state == nullptr) { return nullptr; } - if (events->group_voice_state_size == events->group_voice_state_capacity) { - const uint32_t new_group_voice_state_capacity = events->group_voice_state_capacity * 2 + 1; - Tox_Event_Group_Voice_State *new_group_voice_state = (Tox_Event_Group_Voice_State *) - realloc( - events->group_voice_state, - new_group_voice_state_capacity * sizeof(Tox_Event_Group_Voice_State)); - - if (new_group_voice_state == nullptr) { - return nullptr; - } - - events->group_voice_state = new_group_voice_state; - events->group_voice_state_capacity = new_group_voice_state_capacity; - } - - Tox_Event_Group_Voice_State *const group_voice_state = - &events->group_voice_state[events->group_voice_state_size]; tox_event_group_voice_state_construct(group_voice_state); - ++events->group_voice_state_size; return group_voice_state; } -void tox_events_clear_group_voice_state(Tox_Events *events) +void tox_event_group_voice_state_free(Tox_Event_Group_Voice_State *group_voice_state, const Memory *mem) { - if (events == nullptr) { - return; + if (group_voice_state != nullptr) { + tox_event_group_voice_state_destruct(group_voice_state, mem); } - - for (uint32_t i = 0; i < events->group_voice_state_size; ++i) { - tox_event_group_voice_state_destruct(&events->group_voice_state[i]); - } - - free(events->group_voice_state); - events->group_voice_state = nullptr; - events->group_voice_state_size = 0; - events->group_voice_state_capacity = 0; + mem_delete(mem, group_voice_state); } -uint32_t tox_events_get_group_voice_state_size(const Tox_Events *events) +non_null() +static Tox_Event_Group_Voice_State *tox_events_add_group_voice_state(Tox_Events *events, const Memory *mem) { - if (events == nullptr) { - return 0; + Tox_Event_Group_Voice_State *const group_voice_state = tox_event_group_voice_state_new(mem); + + if (group_voice_state == nullptr) { + return nullptr; } - return events->group_voice_state_size; + Tox_Event event; + event.type = TOX_EVENT_GROUP_VOICE_STATE; + event.data.group_voice_state = group_voice_state; + + tox_events_add(events, &event); + return group_voice_state; } const Tox_Event_Group_Voice_State *tox_events_get_group_voice_state(const Tox_Events *events, uint32_t index) { - assert(index < events->group_voice_state_size); - assert(events->group_voice_state != nullptr); - return &events->group_voice_state[index]; -} - -bool tox_events_pack_group_voice_state(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_group_voice_state_size(events); + uint32_t group_voice_state_index = 0; + const uint32_t size = tox_events_get_size(events); for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_group_voice_state_pack(tox_events_get_group_voice_state(events, i), bp)) { - return false; + if (group_voice_state_index > index) { + return nullptr; + } + + if (events->events[i].type == TOX_EVENT_GROUP_VOICE_STATE) { + const Tox_Event_Group_Voice_State *group_voice_state = events->events[i].data.group_voice_state; + if (group_voice_state_index == index) { + return group_voice_state; + } + ++group_voice_state_index; } } - return true; + + return nullptr; } -bool tox_events_unpack_group_voice_state(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_group_voice_state_size(const Tox_Events *events) { - Tox_Event_Group_Voice_State *event = tox_events_add_group_voice_state(events); + uint32_t group_voice_state_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_GROUP_VOICE_STATE) { + ++group_voice_state_size; + } + } + + return group_voice_state_size; +} + +bool tox_event_group_voice_state_unpack( + Tox_Event_Group_Voice_State **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_group_voice_state_new(mem); + + if (*event == nullptr) { return false; } - return tox_event_group_voice_state_unpack(event, bu); + return tox_event_group_voice_state_unpack_into(*event, bu); +} + +non_null() +static Tox_Event_Group_Voice_State *tox_event_group_voice_state_alloc(void *user_data) +{ + Tox_Events_State *state = tox_events_alloc(user_data); + assert(state != nullptr); + + if (state->events == nullptr) { + return nullptr; + } + + Tox_Event_Group_Voice_State *group_voice_state = tox_events_add_group_voice_state(state->events, state->mem); + + if (group_voice_state == nullptr) { + state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; + return nullptr; + } + + return group_voice_state; } @@ -195,17 +221,9 @@ bool tox_events_unpack_group_voice_state(Tox_Events *events, Bin_Unpack *bu) void tox_events_handle_group_voice_state(Tox *tox, uint32_t group_number, Tox_Group_Voice_State voice_state, void *user_data) { - Tox_Events_State *state = tox_events_alloc(user_data); - assert(state != nullptr); - - if (state->events == nullptr) { - return; - } - - Tox_Event_Group_Voice_State *group_voice_state = tox_events_add_group_voice_state(state->events); + Tox_Event_Group_Voice_State *group_voice_state = tox_event_group_voice_state_alloc(user_data); if (group_voice_state == 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 0e381de7..95e06a7c 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,6 +27,19 @@ struct Tox_Event_Self_Connection_Status { Tox_Connection 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) +{ + assert(self_connection_status != nullptr); + self_connection_status->connection_status = connection_status; +} +Tox_Connection tox_event_self_connection_status_get_connection_status(const Tox_Event_Self_Connection_Status *self_connection_status) +{ + assert(self_connection_status != nullptr); + return self_connection_status->connection_status; +} + non_null() static void tox_event_self_connection_status_construct(Tox_Event_Self_Connection_Status *self_connection_status) { @@ -35,27 +48,12 @@ static void tox_event_self_connection_status_construct(Tox_Event_Self_Connection }; } non_null() -static void tox_event_self_connection_status_destruct(Tox_Event_Self_Connection_Status *self_connection_status) +static void tox_event_self_connection_status_destruct(Tox_Event_Self_Connection_Status *self_connection_status, const Memory *mem) { return; } -non_null() -static void tox_event_self_connection_status_set_connection_status(Tox_Event_Self_Connection_Status - *self_connection_status, Tox_Connection connection_status) -{ - assert(self_connection_status != nullptr); - self_connection_status->connection_status = connection_status; -} -Tox_Connection tox_event_self_connection_status_get_connection_status(const Tox_Event_Self_Connection_Status - *self_connection_status) -{ - assert(self_connection_status != nullptr); - return self_connection_status->connection_status; -} - -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); @@ -65,101 +63,130 @@ 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); - return tox_unpack_connection(bu, &event->connection_status); + return tox_connection_unpack(bu, &event->connection_status); } /***************************************************** * - * :: 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 event->type == TOX_EVENT_SELF_CONNECTION_STATUS ? event->data.self_connection_status : 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)); + + if (self_connection_status == nullptr) { 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)); - - if (new_self_connection_status == nullptr) { - return nullptr; - } - - events->self_connection_status = new_self_connection_status; - events->self_connection_status_capacity = new_self_connection_status_capacity; - } - - 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; + if (self_connection_status != nullptr) { + tox_event_self_connection_status_destruct(self_connection_status, mem); } - - for (uint32_t i = 0; i < events->self_connection_status_size; ++i) { - tox_event_self_connection_status_destruct(&events->self_connection_status[i]); - } - - 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]; -} - -bool tox_events_pack_self_connection_status(const Tox_Events *events, Bin_Pack *bp) -{ - const uint32_t size = tox_events_get_self_connection_status_size(events); + 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 (!tox_event_self_connection_status_pack(tox_events_get_self_connection_status(events, i), bp)) { - return false; + 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 true; + + return nullptr; } -bool tox_events_unpack_self_connection_status(Tox_Events *events, Bin_Unpack *bu) +uint32_t tox_events_get_self_connection_status_size(const Tox_Events *events) { - Tox_Event_Self_Connection_Status *event = tox_events_add_self_connection_status(events); + uint32_t self_connection_status_size = 0; + const uint32_t size = tox_events_get_size(events); - if (event == nullptr) { + for (uint32_t i = 0; i < size; ++i) { + if (events->events[i].type == TOX_EVENT_SELF_CONNECTION_STATUS) { + ++self_connection_status_size; + } + } + + return self_connection_status_size; +} + +bool tox_event_self_connection_status_unpack( + Tox_Event_Self_Connection_Status **event, Bin_Unpack *bu, const Memory *mem) +{ + assert(event != nullptr); + *event = tox_event_self_connection_status_new(mem); + + 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; } @@ -170,19 +197,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/forwarding.c b/toxcore/forwarding.c index 5e885abd..be96a21d 100644 --- a/toxcore/forwarding.c +++ b/toxcore/forwarding.c @@ -31,14 +31,14 @@ struct Forwarding { void *forwarded_response_callback_object; }; -DHT *forwarding_get_dht(Forwarding *forwarding) +DHT *forwarding_get_dht(const Forwarding *forwarding) { return forwarding->dht; } #define SENDBACK_TIMEOUT 3600 -bool send_forward_request(Networking_Core *net, const IP_Port *forwarder, +bool send_forward_request(const Networking_Core *net, const IP_Port *forwarder, const uint8_t *chain_keys, uint16_t chain_length, const uint8_t *data, uint16_t data_length) { @@ -316,7 +316,7 @@ static int handle_forwarding(void *object, const IP_Port *source, const uint8_t } } -bool forward_reply(Networking_Core *net, const IP_Port *forwarder, +bool forward_reply(const Networking_Core *net, const IP_Port *forwarder, const uint8_t *sendback, uint16_t sendback_length, const uint8_t *data, uint16_t length) { diff --git a/toxcore/forwarding.h b/toxcore/forwarding.h index 36ce8ad8..a97971e6 100644 --- a/toxcore/forwarding.h +++ b/toxcore/forwarding.h @@ -26,7 +26,7 @@ extern "C" { typedef struct Forwarding Forwarding; non_null() -DHT *forwarding_get_dht(Forwarding *forwarding); +DHT *forwarding_get_dht(const Forwarding *forwarding); /** * @brief Send data to forwarder for forwarding via chain of dht nodes. @@ -41,7 +41,7 @@ DHT *forwarding_get_dht(Forwarding *forwarding); * @return true on success, false otherwise. */ non_null() -bool send_forward_request(Networking_Core *net, const IP_Port *forwarder, +bool send_forward_request(const Networking_Core *net, const IP_Port *forwarder, const uint8_t *chain_keys, uint16_t chain_length, const uint8_t *data, uint16_t data_length); @@ -75,7 +75,7 @@ bool create_forward_chain_packet(const uint8_t *chain_keys, uint16_t chain_lengt * @return true on success, false otherwise. */ non_null() -bool forward_reply(Networking_Core *net, const IP_Port *forwarder, +bool forward_reply(const Networking_Core *net, const IP_Port *forwarder, const uint8_t *sendback, uint16_t sendback_length, const uint8_t *data, uint16_t length); diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 49fed514..2326560c 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -911,25 +911,27 @@ Friend_Connections *new_friend_connections( return nullptr; } - temp->mono_time = mono_time; - temp->logger = logger; - temp->dht = onion_get_dht(onion_c); - temp->net_crypto = onion_get_net_crypto(onion_c); - temp->onion_c = onion_c; temp->local_discovery_enabled = local_discovery_enabled; - // Don't include default port in port range - temp->next_lan_port = TOX_PORTRANGE_FROM + 1; - - new_connection_handler(temp->net_crypto, &handle_new_connections, temp); if (temp->local_discovery_enabled) { temp->broadcast = lan_discovery_init(ns); if (temp->broadcast == nullptr) { LOGGER_ERROR(logger, "could not initialise LAN discovery"); + temp->local_discovery_enabled = false; } } + temp->mono_time = mono_time; + temp->logger = logger; + temp->dht = onion_get_dht(onion_c); + temp->net_crypto = onion_get_net_crypto(onion_c); + temp->onion_c = onion_c; + // Don't include default port in port range + temp->next_lan_port = TOX_PORTRANGE_FROM + 1; + + new_connection_handler(temp->net_crypto, &handle_new_connections, temp); + return temp; } diff --git a/toxcore/group.c b/toxcore/group.c index 40e81a11..57a81ef2 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -212,13 +212,13 @@ static bool group_id_eq(const uint8_t *a, const uint8_t *b) } non_null() -static bool g_title_eq(Group_c *g, const uint8_t *title, uint8_t title_len) +static bool g_title_eq(const Group_c *g, const uint8_t *title, uint8_t title_len) { return memeq(g->title, g->title_len, title, title_len); } non_null() -static bool g_peer_nick_eq(Group_Peer *peer, const uint8_t *nick, uint8_t nick_len) +static bool g_peer_nick_eq(const Group_Peer *peer, const uint8_t *nick, uint8_t nick_len) { return memeq(peer->nick, peer->nick_len, nick, nick_len); } @@ -3767,7 +3767,6 @@ Group_Chats *new_groupchats(const Mono_Time *mono_time, Messenger *m) temp->mono_time = mono_time; temp->m = m; temp->fr_c = m->fr_c; - m->conferences_object = temp; m_callback_conference_invite(m, &handle_friend_invite_packet); set_global_status_callback(m->fr_c, &g_handle_any_status, temp); diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index f3731108..901c73d5 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -11,9 +11,7 @@ #include -#ifndef VANILLA_NACL #include -#endif #include @@ -30,8 +28,6 @@ #include "network.h" #include "util.h" -#ifndef VANILLA_NACL - /* The minimum size of a plaintext group handshake packet */ #define GC_MIN_HS_PACKET_PAYLOAD_SIZE (1 + ENC_PUBLIC_KEY_SIZE + SIG_PUBLIC_KEY_SIZE + 1 + 1) @@ -160,7 +156,7 @@ non_null() static bool self_gc_is_founder(const GC_Chat *chat); non_null() static bool group_number_valid(const GC_Session *c, int group_number); non_null() static int peer_update(const GC_Chat *chat, const GC_Peer *peer, uint32_t peer_number); non_null() static void group_delete(GC_Session *c, GC_Chat *chat); -non_null() static void group_cleanup(GC_Session *c, GC_Chat *chat); +non_null() static void group_cleanup(const GC_Session *c, GC_Chat *chat); non_null() static bool group_exists(const GC_Session *c, const uint8_t *chat_id); non_null() static void add_tcp_relays_to_chat(const GC_Session *c, GC_Chat *chat); non_null(1, 2) nullable(4) @@ -1280,8 +1276,8 @@ static uint16_t unpack_gc_shared_state(GC_SharedState *shared_state, const uint8 memcpy(&voice_state, data + len_processed, sizeof(uint8_t)); len_processed += sizeof(uint8_t); - shared_state->voice_state = group_voice_state_from_int(voice_state); - shared_state->privacy_state = group_privacy_state_from_int(privacy_state); + group_voice_state_from_int(voice_state, &shared_state->voice_state); + group_privacy_state_from_int(privacy_state, &shared_state->privacy_state); return len_processed; } @@ -7434,6 +7430,9 @@ static int create_new_group(GC_Session *c, const uint8_t *nick, size_t nick_leng return -1; } + init_gc_shared_state(chat, privacy_state); + init_gc_moderation(chat); + if (!init_gc_tcp_connection(c, chat)) { group_delete(c, chat); return -1; @@ -7454,9 +7453,6 @@ static int create_new_group(GC_Session *c, const uint8_t *nick, size_t nick_leng self_gc_set_confirmed(chat, true); self_gc_set_ext_public_key(chat, chat->self_public_key); - init_gc_shared_state(chat, privacy_state); - init_gc_moderation(chat); - return group_number; } @@ -8182,7 +8178,7 @@ GC_Session *new_dht_groupchats(Messenger *m) return c; } -static void group_cleanup(GC_Session *c, GC_Chat *chat) +static void group_cleanup(const GC_Session *c, GC_Chat *chat) { kill_group_friend_connection(c, chat); @@ -8496,4 +8492,3 @@ int gc_add_peers_from_announces(GC_Chat *chat, const GC_Announce *announces, uin return added_peers; } -#endif // VANILLA_NACL diff --git a/toxcore/group_connection.c b/toxcore/group_connection.c index 5d036960..9d919361 100644 --- a/toxcore/group_connection.c +++ b/toxcore/group_connection.c @@ -22,8 +22,6 @@ #include "mono_time.h" #include "util.h" -#ifndef VANILLA_NACL - /** Seconds since last direct UDP packet was received before the connection is considered dead */ #define GCC_UDP_DIRECT_TIMEOUT (GC_PING_TIMEOUT + 4) @@ -211,7 +209,7 @@ bool gcc_send_lossless_packet_fragments(const GC_Chat *chat, GC_Connection *gcon const uint16_t end_idx = gcc_get_array_index(gconn->send_message_id); for (uint16_t i = start_idx; i != end_idx; i = (i + 1) % GCC_BUFFER_SIZE) { - GC_Message_Array_Entry *entry = &gconn->send_array[i]; + const GC_Message_Array_Entry *entry = &gconn->send_array[i]; if (array_entry_is_empty(entry)) { LOGGER_FATAL(chat->log, "array entry for packet chunk is empty"); @@ -704,5 +702,3 @@ void gcc_cleanup(const GC_Chat *chat) gcc_peer_cleanup(gconn); } } - -#endif // VANILLA_NACL diff --git a/toxcore/group_onion_announce.c b/toxcore/group_onion_announce.c index b797770e..7efb4af6 100644 --- a/toxcore/group_onion_announce.c +++ b/toxcore/group_onion_announce.c @@ -66,8 +66,6 @@ void gca_onion_init(GC_Announces_List *group_announce, Onion_Announce *onion_a) group_announce); } -#ifndef VANILLA_NACL - int create_gca_announce_request( const Random *rng, uint8_t *packet, uint16_t max_packet_length, const uint8_t *dest_client_id, const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *ping_id, @@ -112,4 +110,3 @@ int create_gca_announce_request( return full_length; } -#endif // VANILLA_NACL diff --git a/toxcore/group_pack.c b/toxcore/group_pack.c index c63a46af..c681d821 100644 --- a/toxcore/group_pack.c +++ b/toxcore/group_pack.c @@ -18,29 +18,48 @@ #include "ccompat.h" #include "util.h" -Group_Privacy_State group_privacy_state_from_int(uint8_t value) +bool group_privacy_state_from_int(uint8_t value, Group_Privacy_State *out) { switch (value) { - case 0: - return GI_PUBLIC; - case 1: - return GI_PRIVATE; - default: - return GI_PUBLIC; + case GI_PUBLIC: { + *out = GI_PUBLIC; + return true; + } + + case GI_PRIVATE: { + *out = GI_PRIVATE; + return true; + } + + default: { + *out = GI_PUBLIC; + return false; + } } } -Group_Voice_State group_voice_state_from_int(uint8_t value) +bool group_voice_state_from_int(uint8_t value, Group_Voice_State *out) { switch (value) { - case 0: - return GV_ALL; - case 1: - return GV_MODS; - case 2: - return GV_FOUNDER; - default: - return GV_ALL; + case GV_ALL: { + *out = GV_ALL; + return true; + } + + case GV_MODS: { + *out = GV_MODS; + return true; + } + + case GV_FOUNDER: { + *out = GV_FOUNDER; + return true; + } + + default: { + *out = GV_ALL; + return false; + } } } @@ -69,8 +88,8 @@ static bool load_unpack_state_values(GC_Chat *chat, Bin_Unpack *bu) } chat->connection_state = manually_disconnected ? CS_DISCONNECTED : CS_CONNECTING; - chat->shared_state.privacy_state = group_privacy_state_from_int(privacy_state); - chat->shared_state.voice_state = group_voice_state_from_int(voice_state); + group_privacy_state_from_int(privacy_state, &chat->shared_state.privacy_state); + group_voice_state_from_int(voice_state, &chat->shared_state.voice_state); // we always load saved groups as private in case the group became private while we were offline. // this will have no detrimental effect if the group is public, as the correct privacy diff --git a/toxcore/group_pack.h b/toxcore/group_pack.h index 9d188407..deea3fab 100644 --- a/toxcore/group_pack.h +++ b/toxcore/group_pack.h @@ -32,7 +32,9 @@ void gc_save_pack_group(const GC_Chat *chat, Bin_Pack *bp); non_null() bool gc_load_unpack_group(GC_Chat *chat, Bin_Unpack *bu); -Group_Privacy_State group_privacy_state_from_int(uint8_t value); -Group_Voice_State group_voice_state_from_int(uint8_t value); +non_null() +bool group_privacy_state_from_int(uint8_t value, Group_Privacy_State *out); +non_null() +bool group_voice_state_from_int(uint8_t value, Group_Voice_State *out); #endif // GROUP_PACK_H diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 4d2aaac1..cf11ef15 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -219,10 +219,9 @@ static int create_cookie_request(const Net_Crypto *c, uint8_t *packet, const uin uint64_t number, uint8_t *shared_key) { uint8_t plain[COOKIE_REQUEST_PLAIN_LENGTH]; - uint8_t padding[CRYPTO_PUBLIC_KEY_SIZE] = {0}; memcpy(plain, c->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); - memcpy(plain + CRYPTO_PUBLIC_KEY_SIZE, padding, CRYPTO_PUBLIC_KEY_SIZE); + memset(plain + CRYPTO_PUBLIC_KEY_SIZE, 0, CRYPTO_PUBLIC_KEY_SIZE); memcpy(plain + (CRYPTO_PUBLIC_KEY_SIZE * 2), &number, sizeof(uint64_t)); const uint8_t *tmp_shared_key = dht_get_shared_key_sent(c->dht, dht_public_key); memcpy(shared_key, tmp_shared_key, CRYPTO_SHARED_KEY_SIZE); diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index ac6a0b59..939b672e 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -43,29 +43,31 @@ /*** Messages. */ -#define PACKET_ID_PADDING 0 // Denotes padding -#define PACKET_ID_REQUEST 1 // Used to request unreceived packets -#define PACKET_ID_KILL 2 // Used to kill connection +typedef enum Packet_Id { + PACKET_ID_PADDING = 0, // Denotes padding + PACKET_ID_REQUEST = 1, // Used to request unreceived packets + PACKET_ID_KILL = 2, // Used to kill connection -#define PACKET_ID_ONLINE 24 -#define PACKET_ID_OFFLINE 25 -#define PACKET_ID_NICKNAME 48 -#define PACKET_ID_STATUSMESSAGE 49 -#define PACKET_ID_USERSTATUS 50 -#define PACKET_ID_TYPING 51 -#define PACKET_ID_MESSAGE 64 -#define PACKET_ID_ACTION 65 // PACKET_ID_MESSAGE + MESSAGE_ACTION -#define PACKET_ID_MSI 69 // Used by AV to setup calls and etc -#define PACKET_ID_FILE_SENDREQUEST 80 -#define PACKET_ID_FILE_CONTROL 81 -#define PACKET_ID_FILE_DATA 82 -#define PACKET_ID_INVITE_GROUPCHAT 95 -#define PACKET_ID_INVITE_CONFERENCE 96 -#define PACKET_ID_ONLINE_PACKET 97 -#define PACKET_ID_DIRECT_CONFERENCE 98 -#define PACKET_ID_MESSAGE_CONFERENCE 99 -#define PACKET_ID_REJOIN_CONFERENCE 100 -#define PACKET_ID_LOSSY_CONFERENCE 199 + PACKET_ID_ONLINE = 24, + PACKET_ID_OFFLINE = 25, + PACKET_ID_NICKNAME = 48, + PACKET_ID_STATUSMESSAGE = 49, + PACKET_ID_USERSTATUS = 50, + PACKET_ID_TYPING = 51, + PACKET_ID_MESSAGE = 64, + PACKET_ID_ACTION = 65, // PACKET_ID_MESSAGE + MESSAGE_ACTION + PACKET_ID_MSI = 69, // Used by AV to setup calls and etc + PACKET_ID_FILE_SENDREQUEST = 80, + PACKET_ID_FILE_CONTROL = 81, + PACKET_ID_FILE_DATA = 82, + PACKET_ID_INVITE_GROUPCHAT = 95, + PACKET_ID_INVITE_CONFERENCE = 96, + PACKET_ID_ONLINE_PACKET = 97, + PACKET_ID_DIRECT_CONFERENCE = 98, + PACKET_ID_MESSAGE_CONFERENCE = 99, + PACKET_ID_REJOIN_CONFERENCE = 100, + PACKET_ID_LOSSY_CONFERENCE = 199, +} Packet_Id; /** Maximum size of receiving and sending packet buffers. */ #define CRYPTO_PACKET_BUFFER_SIZE 32768 // Must be a power of 2 diff --git a/toxcore/network.c b/toxcore/network.c index d8cabc71..726a3410 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -21,7 +21,7 @@ #define _XOPEN_SOURCE 700 #endif -#if defined(_WIN32) && _WIN32_WINNT >= _WIN32_WINNT_WINXP +#if defined(_WIN32) && defined(_WIN32_WINNT) && _WIN32_WINNT >= _WIN32_WINNT_WINXP #undef _WIN32_WINNT #define _WIN32_WINNT 0x501 #endif @@ -87,11 +87,6 @@ #include #include -#ifndef VANILLA_NACL -// Used for sodium_init() -#include -#endif - #include "ccompat.h" #include "logger.h" #include "mono_time.h" @@ -361,13 +356,9 @@ IP4 get_ip4_loopback(void) IP6 get_ip6_loopback(void) { - IP6 loopback; -#ifdef ESP_PLATFORM - loopback = empty_ip_port.ip.ip.v6; + /* in6addr_loopback isn't available everywhere, so we do it ourselves. */ + IP6 loopback = empty_ip_port.ip.ip.v6; loopback.uint8[15] = 1; -#else - get_ip6(&loopback, &in6addr_loopback); -#endif return loopback; } @@ -565,7 +556,7 @@ non_null() static int sys_getsockopt(void *obj, int sock, int level, int optname, void *optval, size_t *optlen) { socklen_t len = *optlen; - const int ret = getsockopt(sock, level, optname, optval, &len); + const int ret = getsockopt(sock, level, optname, (char *)optval, &len); *optlen = len; return ret; } @@ -573,7 +564,7 @@ static int sys_getsockopt(void *obj, int sock, int level, int optname, void *opt non_null() static int sys_setsockopt(void *obj, int sock, int level, int optname, const void *optval, size_t optlen) { - return setsockopt(sock, level, optname, optval, optlen); + return setsockopt(sock, level, optname, (const char *)optval, optlen); } static const Network_Funcs system_network_funcs = { @@ -1340,8 +1331,8 @@ Networking_Core *new_networking_ex( Ip_Ntoa ip_str; int neterror = net_error(); char *strerror = net_new_strerror(neterror); - LOGGER_ERROR(log, "failed to bind socket: %d, %s IP: %s port_from: %u port_to: %u", neterror, strerror, - net_ip_ntoa(ip, &ip_str), port_from, port_to); + LOGGER_ERROR(log, "failed to bind socket: %d, %s IP: %s port_from: %u port_to: %u", + neterror, strerror, net_ip_ntoa(ip, &ip_str), port_from, port_to); net_kill_strerror(strerror); kill_networking(temp); @@ -1769,6 +1760,8 @@ bool net_connect(const Memory *mem, const Logger *log, Socket sock, const IP_Por int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int tox_type) { + assert(node != nullptr); + // Try parsing as IP address first. IP_Port parsed = {{{0}}}; // Initialise to nullptr. In error paths, at least we initialise the out @@ -2031,7 +2024,7 @@ char *net_new_strerror(int error) return str; } #else -#ifdef _GNU_SOURCE +#if defined(_GNU_SOURCE) && defined(__GLIBC__) non_null() static const char *net_strerror_r(int error, char *tmp, size_t tmp_size) { diff --git a/toxcore/network.h b/toxcore/network.h index 2a6a0833..d6056764 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -97,56 +97,6 @@ Family net_family_tox_tcp_ipv6(void); #define MAX_UDP_PACKET_SIZE 2048 -#ifdef USE_TEST_NETWORK -typedef enum Net_Packet_Type { - NET_PACKET_PING_REQUEST = 0x05, /* Ping request packet ID. */ - NET_PACKET_PING_RESPONSE = 0x06, /* Ping response packet ID. */ - NET_PACKET_GET_NODES = 0x07, /* Get nodes request packet ID. */ - NET_PACKET_SEND_NODES_IPV6 = 0x08, /* Send nodes response packet ID for other addresses. */ - NET_PACKET_COOKIE_REQUEST = 0x1c, /* Cookie request packet */ - NET_PACKET_COOKIE_RESPONSE = 0x1d, /* Cookie response packet */ - NET_PACKET_CRYPTO_HS = 0x1e, /* Crypto handshake packet */ - NET_PACKET_CRYPTO_DATA = 0x1f, /* Crypto data packet */ - NET_PACKET_CRYPTO = 0x24, /* Encrypted data packet ID. */ - NET_PACKET_LAN_DISCOVERY = 0x25, /* LAN discovery packet ID. */ - - NET_PACKET_GC_HANDSHAKE = 0x62, /* Group chat handshake packet ID */ - NET_PACKET_GC_LOSSLESS = 0x63, /* Group chat lossless packet ID */ - NET_PACKET_GC_LOSSY = 0x64, /* Group chat lossy packet ID */ - - /* See: `docs/Prevent_Tracking.txt` and `onion.{c,h}` */ - NET_PACKET_ONION_SEND_INITIAL = 0x8f, - NET_PACKET_ONION_SEND_1 = 0x90, - NET_PACKET_ONION_SEND_2 = 0x91, - - NET_PACKET_ANNOUNCE_REQUEST = 0x92, - NET_PACKET_ANNOUNCE_RESPONSE = 0x93, - NET_PACKET_ONION_DATA_REQUEST = 0x94, - NET_PACKET_ONION_DATA_RESPONSE = 0x95, - - NET_PACKET_ANNOUNCE_REQUEST_OLD = 0x96, /* TODO: DEPRECATE */ - NET_PACKET_ANNOUNCE_RESPONSE_OLD = 0x97, /* TODO: DEPRECATE */ - - NET_PACKET_ONION_RECV_3 = 0x9b, - NET_PACKET_ONION_RECV_2 = 0x9c, - NET_PACKET_ONION_RECV_1 = 0x9d, - - NET_PACKET_FORWARD_REQUEST = 0x9e, - NET_PACKET_FORWARDING = 0x9f, - NET_PACKET_FORWARD_REPLY = 0xa0, - - NET_PACKET_DATA_SEARCH_REQUEST = 0xa1, - NET_PACKET_DATA_SEARCH_RESPONSE = 0xa2, - NET_PACKET_DATA_RETRIEVE_REQUEST = 0xa3, - NET_PACKET_DATA_RETRIEVE_RESPONSE = 0xa4, - NET_PACKET_STORE_ANNOUNCE_REQUEST = 0xa5, - NET_PACKET_STORE_ANNOUNCE_RESPONSE = 0xa6, - - BOOTSTRAP_INFO_PACKET_ID = 0xf1, /* Only used for bootstrap nodes */ - - NET_PACKET_MAX = 0xff, /* This type must remain within a single uint8. */ -} Net_Packet_Type; -#else typedef enum Net_Packet_Type { NET_PACKET_PING_REQUEST = 0x00, /* Ping request packet ID. */ NET_PACKET_PING_RESPONSE = 0x01, /* Ping response packet ID. */ @@ -195,7 +145,6 @@ typedef enum Net_Packet_Type { NET_PACKET_MAX = 0xff, /* This type must remain within a single uint8. */ } Net_Packet_Type; -#endif // test network #define TOX_PORTRANGE_FROM 33445 @@ -591,7 +540,7 @@ char *net_new_strerror(int error); * It's valid to pass NULL as the argument, the function does nothing in this * case. */ -non_null() +nullable(1) void net_kill_strerror(char *strerror); /** @brief Initialize networking. diff --git a/toxcore/onion.c b/toxcore/onion.c index 80bb4920..3e515752 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c @@ -735,6 +735,7 @@ Onion *new_onion(const Logger *log, const Memory *mem, const Mono_Time *mono_tim if (onion->shared_keys_1 == nullptr || onion->shared_keys_2 == nullptr || onion->shared_keys_3 == nullptr) { + // cppcheck-suppress mismatchAllocDealloc kill_onion(onion); return nullptr; } diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index d4a37ad9..ffe368e3 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -669,6 +669,7 @@ Onion_Announce *new_onion_announce(const Logger *log, const Memory *mem, const R onion_a->shared_keys_recv = shared_key_cache_new(log, mono_time, mem, dht_get_self_secret_key(dht), KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); if (onion_a->shared_keys_recv == nullptr) { + // cppcheck-suppress mismatchAllocDealloc kill_onion_announce(onion_a); return nullptr; } diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index cdf0b85d..53718ffc 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -671,7 +671,6 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, con onion_friend->temp_secret_key, ping_id, onion_friend->real_public_key, zero_ping_id, sendback); } else { // contact is a gc -#ifndef VANILLA_NACL onion_friend->is_groupchat = true; len = create_gca_announce_request( @@ -679,9 +678,6 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, con onion_friend->temp_secret_key, ping_id, onion_friend->real_public_key, zero_ping_id, sendback, onion_friend->gc_data, onion_friend->gc_data_length); -#else - return -1; -#endif // VANILLA_NACL } } @@ -1996,6 +1992,9 @@ static void do_announce(Onion_Client *onion_c) // Don't send announces to the same node twice. If we don't have many nodes, // the random selection below may have overlaps. This ensures that we deduplicate // nodes before sending packets to save some bandwidth. + // + // TODO(iphydf): Figure out why on esp32, this is necessary for the onion + // connection to succeed. This is an optimisation and shouldn't be necessary. const uint8_t *targets[MAX_ONION_CLIENTS_ANNOUNCE / 2]; unsigned int targets_count = 0; diff --git a/toxcore/tox.c b/toxcore/tox.c index f802ce16..290f6bb8 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -362,7 +362,6 @@ static void tox_friend_lossless_packet_handler(Messenger *m, uint32_t friend_num } } -#ifndef VANILLA_NACL non_null(1, 4) nullable(6) static void tox_group_peer_name_handler(const Messenger *m, uint32_t group_number, uint32_t peer_id, const uint8_t *name, size_t length, void *user_data) @@ -573,7 +572,6 @@ static void tox_group_moderation_handler(const Messenger *m, uint32_t group_numb tox_data->user_data); } } -#endif bool tox_version_is_compatible(uint32_t major, uint32_t minor, uint32_t patch) { @@ -637,6 +635,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) switch (err) { case TOX_ERR_OPTIONS_NEW_OK: { + assert(default_options != nullptr); break; } @@ -660,6 +659,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) if (sys->rng == nullptr || sys->ns == nullptr || sys->mem == nullptr) { // TODO(iphydf): Not quite right, but similar. SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); + tox_options_free(default_options); return nullptr; } @@ -717,6 +717,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) if (tox == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); + tox_options_free(default_options); return nullptr; } @@ -743,8 +744,8 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) default: { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_TYPE); - tox_options_free(default_options); mem_delete(sys->mem, tox); + tox_options_free(default_options); return nullptr; } } @@ -754,8 +755,8 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) if (m_options.proxy_info.proxy_type != TCP_PROXY_NONE) { if (tox_options_get_proxy_port(opts) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_PORT); - tox_options_free(default_options); mem_delete(sys->mem, tox); + tox_options_free(default_options); return nullptr; } @@ -771,8 +772,8 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) || !addr_resolve_or_parse_ip(tox->sys.ns, proxy_host, &m_options.proxy_info.ip_port.ip, nullptr)) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_HOST); // TODO(irungentoo): TOX_ERR_NEW_PROXY_NOT_FOUND if domain. - tox_options_free(default_options); mem_delete(sys->mem, tox); + tox_options_free(default_options); return nullptr; } @@ -783,8 +784,8 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) if (tox->mono_time == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); - tox_options_free(default_options); mem_delete(sys->mem, tox); + tox_options_free(default_options); return nullptr; } @@ -793,8 +794,8 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) if (tox->mutex == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); - tox_options_free(default_options); mem_delete(sys->mem, tox); + tox_options_free(default_options); return nullptr; } @@ -828,7 +829,6 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) } mono_time_free(tox->sys.mem, tox->mono_time); - tox_options_free(default_options); tox_unlock(tox); if (tox->mutex != nullptr) { @@ -837,14 +837,16 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) mem_delete(sys->mem, tox->mutex); mem_delete(sys->mem, tox); + tox_options_free(default_options); return nullptr; } - if (new_groupchats(tox->mono_time, tox->m) == nullptr) { + tox->m->conferences_object = new_groupchats(tox->mono_time, tox->m); + + if (tox->m->conferences_object == nullptr) { kill_messenger(tox->m); mono_time_free(tox->sys.mem, tox->mono_time); - tox_options_free(default_options); tox_unlock(tox); if (tox->mutex != nullptr) { @@ -855,6 +857,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) mem_delete(sys->mem, tox); SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); + tox_options_free(default_options); return nullptr; } @@ -864,7 +867,6 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) kill_messenger(tox->m); mono_time_free(tox->sys.mem, tox->mono_time); - tox_options_free(default_options); tox_unlock(tox); if (tox->mutex != nullptr) { @@ -875,6 +877,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) mem_delete(sys->mem, tox); SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); + tox_options_free(default_options); return nullptr; } @@ -905,7 +908,6 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) custom_lossy_packet_registerhandler(tox->m, tox_friend_lossy_packet_handler); custom_lossless_packet_registerhandler(tox->m, tox_friend_lossless_packet_handler); -#ifndef VANILLA_NACL m_callback_group_invite(tox->m, tox_group_invite_handler); gc_callback_message(tox->m, tox_group_message_handler); gc_callback_private_message(tox->m, tox_group_private_message_handler); @@ -924,14 +926,12 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) gc_callback_self_join(tox->m, tox_group_self_join_handler); gc_callback_rejected(tox->m, tox_group_join_fail_handler); gc_callback_voice_state(tox->m, tox_group_voice_state_handler); -#endif - - tox_options_free(default_options); tox_unlock(tox); SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); + tox_options_free(default_options); return tox; } @@ -2725,6 +2725,7 @@ static void set_custom_packet_error(int ret, Tox_Err_Friend_Custom_Packet *error } } +// cppcheck-suppress constParameterPointer bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, Tox_Err_Friend_Custom_Packet *error) { @@ -2764,6 +2765,7 @@ void tox_callback_friend_lossy_packet(Tox *tox, tox_friend_lossy_packet_cb *call } } +// cppcheck-suppress constParameterPointer bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, Tox_Err_Friend_Custom_Packet *error) { @@ -2843,7 +2845,6 @@ uint16_t tox_self_get_tcp_port(const Tox *tox, Tox_Err_Get_Port *error) /* GROUPCHAT FUNCTIONS */ -#ifndef VANILLA_NACL void tox_callback_group_invite(Tox *tox, tox_group_invite_cb *callback) { assert(tox != nullptr); @@ -4595,8 +4596,6 @@ bool tox_group_mod_kick_peer(const Tox *tox, uint32_t group_number, uint32_t pee return false; } -#endif /* VANILLA_NACL */ - const Tox_System *tox_get_system(Tox *tox) { assert(tox != nullptr); diff --git a/toxcore/tox_dispatch.c b/toxcore/tox_dispatch.c index ee294373..9f304b0c 100644 --- a/toxcore/tox_dispatch.c +++ b/toxcore/tox_dispatch.c @@ -7,6 +7,8 @@ #include #include "ccompat.h" +#include "events/events_alloc.h" +#include "tox_event.h" struct Tox_Dispatch { tox_events_conference_connected_cb *conference_connected_callback; @@ -274,591 +276,333 @@ void tox_events_callback_group_moderation( dispatch->group_moderation_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; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_peer_name( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_peer_name_size(events); + case TOX_EVENT_GROUP_PEER_NAME: { + if (dispatch->group_peer_name_callback != nullptr) { + dispatch->group_peer_name_callback(tox, event->data.group_peer_name, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_peer_name_callback != nullptr) { - dispatch->group_peer_name_callback( - tox, tox_events_get_group_peer_name(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_peer_status( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_peer_status_size(events); + case TOX_EVENT_GROUP_PEER_STATUS: { + if (dispatch->group_peer_status_callback != nullptr) { + dispatch->group_peer_status_callback(tox, event->data.group_peer_status, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_peer_status_callback != nullptr) { - dispatch->group_peer_status_callback( - tox, tox_events_get_group_peer_status(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_topic( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_topic_size(events); + case TOX_EVENT_GROUP_TOPIC: { + if (dispatch->group_topic_callback != nullptr) { + dispatch->group_topic_callback(tox, event->data.group_topic, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_topic_callback != nullptr) { - dispatch->group_topic_callback( - tox, tox_events_get_group_topic(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_privacy_state( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_privacy_state_size(events); + case TOX_EVENT_GROUP_PRIVACY_STATE: { + if (dispatch->group_privacy_state_callback != nullptr) { + dispatch->group_privacy_state_callback(tox, event->data.group_privacy_state, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_privacy_state_callback != nullptr) { - dispatch->group_privacy_state_callback( - tox, tox_events_get_group_privacy_state(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_voice_state( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_voice_state_size(events); + case TOX_EVENT_GROUP_VOICE_STATE: { + if (dispatch->group_voice_state_callback != nullptr) { + dispatch->group_voice_state_callback(tox, event->data.group_voice_state, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_voice_state_callback != nullptr) { - dispatch->group_voice_state_callback( - tox, tox_events_get_group_voice_state(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_topic_lock( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_topic_lock_size(events); + case TOX_EVENT_GROUP_TOPIC_LOCK: { + if (dispatch->group_topic_lock_callback != nullptr) { + dispatch->group_topic_lock_callback(tox, event->data.group_topic_lock, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_topic_lock_callback != nullptr) { - dispatch->group_topic_lock_callback( - tox, tox_events_get_group_topic_lock(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_peer_limit( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_peer_limit_size(events); + case TOX_EVENT_GROUP_PEER_LIMIT: { + if (dispatch->group_peer_limit_callback != nullptr) { + dispatch->group_peer_limit_callback(tox, event->data.group_peer_limit, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_peer_limit_callback != nullptr) { - dispatch->group_peer_limit_callback( - tox, tox_events_get_group_peer_limit(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_password( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_password_size(events); + case TOX_EVENT_GROUP_PASSWORD: { + if (dispatch->group_password_callback != nullptr) { + dispatch->group_password_callback(tox, event->data.group_password, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_password_callback != nullptr) { - dispatch->group_password_callback( - tox, tox_events_get_group_password(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_message( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_message_size(events); + case TOX_EVENT_GROUP_MESSAGE: { + if (dispatch->group_message_callback != nullptr) { + dispatch->group_message_callback(tox, event->data.group_message, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_message_callback != nullptr) { - dispatch->group_message_callback( - tox, tox_events_get_group_message(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_private_message( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_private_message_size(events); + case TOX_EVENT_GROUP_PRIVATE_MESSAGE: { + if (dispatch->group_private_message_callback != nullptr) { + dispatch->group_private_message_callback(tox, event->data.group_private_message, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_private_message_callback != nullptr) { - dispatch->group_private_message_callback( - tox, tox_events_get_group_private_message(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_custom_packet( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_custom_packet_size(events); + case TOX_EVENT_GROUP_CUSTOM_PACKET: { + if (dispatch->group_custom_packet_callback != nullptr) { + dispatch->group_custom_packet_callback(tox, event->data.group_custom_packet, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_custom_packet_callback != nullptr) { - dispatch->group_custom_packet_callback( - tox, tox_events_get_group_custom_packet(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_custom_private_packet( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_custom_private_packet_size(events); + case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: { + if (dispatch->group_custom_private_packet_callback != nullptr) { + dispatch->group_custom_private_packet_callback(tox, event->data.group_custom_private_packet, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_custom_private_packet_callback != nullptr) { - dispatch->group_custom_private_packet_callback( - tox, tox_events_get_group_custom_private_packet(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_invite( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_invite_size(events); + case TOX_EVENT_GROUP_INVITE: { + if (dispatch->group_invite_callback != nullptr) { + dispatch->group_invite_callback(tox, event->data.group_invite, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_invite_callback != nullptr) { - dispatch->group_invite_callback( - tox, tox_events_get_group_invite(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_peer_join( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_peer_join_size(events); + case TOX_EVENT_GROUP_PEER_JOIN: { + if (dispatch->group_peer_join_callback != nullptr) { + dispatch->group_peer_join_callback(tox, event->data.group_peer_join, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_peer_join_callback != nullptr) { - dispatch->group_peer_join_callback( - tox, tox_events_get_group_peer_join(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_peer_exit( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_peer_exit_size(events); + case TOX_EVENT_GROUP_PEER_EXIT: { + if (dispatch->group_peer_exit_callback != nullptr) { + dispatch->group_peer_exit_callback(tox, event->data.group_peer_exit, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_peer_exit_callback != nullptr) { - dispatch->group_peer_exit_callback( - tox, tox_events_get_group_peer_exit(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_self_join( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_self_join_size(events); + case TOX_EVENT_GROUP_SELF_JOIN: { + if (dispatch->group_self_join_callback != nullptr) { + dispatch->group_self_join_callback(tox, event->data.group_self_join, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_self_join_callback != nullptr) { - dispatch->group_self_join_callback( - tox, tox_events_get_group_self_join(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_join_fail( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_join_fail_size(events); + case TOX_EVENT_GROUP_JOIN_FAIL: { + if (dispatch->group_join_fail_callback != nullptr) { + dispatch->group_join_fail_callback(tox, event->data.group_join_fail, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_join_fail_callback != nullptr) { - dispatch->group_join_fail_callback( - tox, tox_events_get_group_join_fail(events, i), user_data); + break; } - } -} -non_null(1, 3) nullable(2, 4) -static void tox_dispatch_invoke_group_moderation( - const Tox_Dispatch *dispatch, const Tox_Events *events, Tox *tox, void *user_data) -{ - const uint32_t size = tox_events_get_group_moderation_size(events); + case TOX_EVENT_GROUP_MODERATION: { + if (dispatch->group_moderation_callback != nullptr) { + dispatch->group_moderation_callback(tox, event->data.group_moderation, user_data); + } - for (uint32_t i = 0; i < size; ++i) { - if (dispatch->group_moderation_callback != nullptr) { - dispatch->group_moderation_callback( - tox, tox_events_get_group_moderation(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); - tox_dispatch_invoke_group_peer_name(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_peer_status(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_topic(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_privacy_state(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_voice_state(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_topic_lock(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_peer_limit(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_password(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_message(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_private_message(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_custom_packet(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_custom_private_packet(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_invite(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_peer_join(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_peer_exit(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_self_join(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_join_fail(dispatch, events, tox, user_data); - tox_dispatch_invoke_group_moderation(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 00000000..17a6dbfb --- /dev/null +++ b/toxcore/tox_event.c @@ -0,0 +1,1058 @@ +/* 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_GROUP_PEER_NAME: + return "TOX_EVENT_GROUP_PEER_NAME"; + + case TOX_EVENT_GROUP_PEER_STATUS: + return "TOX_EVENT_GROUP_PEER_STATUS"; + + case TOX_EVENT_GROUP_TOPIC: + return "TOX_EVENT_GROUP_TOPIC"; + + case TOX_EVENT_GROUP_PRIVACY_STATE: + return "TOX_EVENT_GROUP_PRIVACY_STATE"; + + case TOX_EVENT_GROUP_VOICE_STATE: + return "TOX_EVENT_GROUP_VOICE_STATE"; + + case TOX_EVENT_GROUP_TOPIC_LOCK: + return "TOX_EVENT_GROUP_TOPIC_LOCK"; + + case TOX_EVENT_GROUP_PEER_LIMIT: + return "TOX_EVENT_GROUP_PEER_LIMIT"; + + case TOX_EVENT_GROUP_PASSWORD: + return "TOX_EVENT_GROUP_PASSWORD"; + + case TOX_EVENT_GROUP_MESSAGE: + return "TOX_EVENT_GROUP_MESSAGE"; + + case TOX_EVENT_GROUP_PRIVATE_MESSAGE: + return "TOX_EVENT_GROUP_PRIVATE_MESSAGE"; + + case TOX_EVENT_GROUP_CUSTOM_PACKET: + return "TOX_EVENT_GROUP_CUSTOM_PACKET"; + + case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: + return "TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET"; + + case TOX_EVENT_GROUP_INVITE: + return "TOX_EVENT_GROUP_INVITE"; + + case TOX_EVENT_GROUP_PEER_JOIN: + return "TOX_EVENT_GROUP_PEER_JOIN"; + + case TOX_EVENT_GROUP_PEER_EXIT: + return "TOX_EVENT_GROUP_PEER_EXIT"; + + case TOX_EVENT_GROUP_SELF_JOIN: + return "TOX_EVENT_GROUP_SELF_JOIN"; + + case TOX_EVENT_GROUP_JOIN_FAIL: + return "TOX_EVENT_GROUP_JOIN_FAIL"; + + case TOX_EVENT_GROUP_MODERATION: + return "TOX_EVENT_GROUP_MODERATION"; + + 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_GROUP_PEER_NAME: { + event->data.group_peer_name = tox_event_group_peer_name_new(mem); + break; + } + + case TOX_EVENT_GROUP_PEER_STATUS: { + event->data.group_peer_status = tox_event_group_peer_status_new(mem); + break; + } + + case TOX_EVENT_GROUP_TOPIC: { + event->data.group_topic = tox_event_group_topic_new(mem); + break; + } + + case TOX_EVENT_GROUP_PRIVACY_STATE: { + event->data.group_privacy_state = tox_event_group_privacy_state_new(mem); + break; + } + + case TOX_EVENT_GROUP_VOICE_STATE: { + event->data.group_voice_state = tox_event_group_voice_state_new(mem); + break; + } + + case TOX_EVENT_GROUP_TOPIC_LOCK: { + event->data.group_topic_lock = tox_event_group_topic_lock_new(mem); + break; + } + + case TOX_EVENT_GROUP_PEER_LIMIT: { + event->data.group_peer_limit = tox_event_group_peer_limit_new(mem); + break; + } + + case TOX_EVENT_GROUP_PASSWORD: { + event->data.group_password = tox_event_group_password_new(mem); + break; + } + + case TOX_EVENT_GROUP_MESSAGE: { + event->data.group_message = tox_event_group_message_new(mem); + break; + } + + case TOX_EVENT_GROUP_PRIVATE_MESSAGE: { + event->data.group_private_message = tox_event_group_private_message_new(mem); + break; + } + + case TOX_EVENT_GROUP_CUSTOM_PACKET: { + event->data.group_custom_packet = tox_event_group_custom_packet_new(mem); + break; + } + + case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: { + event->data.group_custom_private_packet = tox_event_group_custom_private_packet_new(mem); + break; + } + + case TOX_EVENT_GROUP_INVITE: { + event->data.group_invite = tox_event_group_invite_new(mem); + break; + } + + case TOX_EVENT_GROUP_PEER_JOIN: { + event->data.group_peer_join = tox_event_group_peer_join_new(mem); + break; + } + + case TOX_EVENT_GROUP_PEER_EXIT: { + event->data.group_peer_exit = tox_event_group_peer_exit_new(mem); + break; + } + + case TOX_EVENT_GROUP_SELF_JOIN: { + event->data.group_self_join = tox_event_group_self_join_new(mem); + break; + } + + case TOX_EVENT_GROUP_JOIN_FAIL: { + event->data.group_join_fail = tox_event_group_join_fail_new(mem); + break; + } + + case TOX_EVENT_GROUP_MODERATION: { + event->data.group_moderation = tox_event_group_moderation_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_GROUP_PEER_NAME: { + tox_event_group_peer_name_free(event->data.group_peer_name, mem); + break; + } + + case TOX_EVENT_GROUP_PEER_STATUS: { + tox_event_group_peer_status_free(event->data.group_peer_status, mem); + break; + } + + case TOX_EVENT_GROUP_TOPIC: { + tox_event_group_topic_free(event->data.group_topic, mem); + break; + } + + case TOX_EVENT_GROUP_PRIVACY_STATE: { + tox_event_group_privacy_state_free(event->data.group_privacy_state, mem); + break; + } + + case TOX_EVENT_GROUP_VOICE_STATE: { + tox_event_group_voice_state_free(event->data.group_voice_state, mem); + break; + } + + case TOX_EVENT_GROUP_TOPIC_LOCK: { + tox_event_group_topic_lock_free(event->data.group_topic_lock, mem); + break; + } + + case TOX_EVENT_GROUP_PEER_LIMIT: { + tox_event_group_peer_limit_free(event->data.group_peer_limit, mem); + break; + } + + case TOX_EVENT_GROUP_PASSWORD: { + tox_event_group_password_free(event->data.group_password, mem); + break; + } + + case TOX_EVENT_GROUP_MESSAGE: { + tox_event_group_message_free(event->data.group_message, mem); + break; + } + + case TOX_EVENT_GROUP_PRIVATE_MESSAGE: { + tox_event_group_private_message_free(event->data.group_private_message, mem); + break; + } + + case TOX_EVENT_GROUP_CUSTOM_PACKET: { + tox_event_group_custom_packet_free(event->data.group_custom_packet, mem); + break; + } + + case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: { + tox_event_group_custom_private_packet_free(event->data.group_custom_private_packet, mem); + break; + } + + case TOX_EVENT_GROUP_INVITE: { + tox_event_group_invite_free(event->data.group_invite, mem); + break; + } + + case TOX_EVENT_GROUP_PEER_JOIN: { + tox_event_group_peer_join_free(event->data.group_peer_join, mem); + break; + } + + case TOX_EVENT_GROUP_PEER_EXIT: { + tox_event_group_peer_exit_free(event->data.group_peer_exit, mem); + break; + } + + case TOX_EVENT_GROUP_SELF_JOIN: { + tox_event_group_self_join_free(event->data.group_self_join, mem); + break; + } + + case TOX_EVENT_GROUP_JOIN_FAIL: { + tox_event_group_join_fail_free(event->data.group_join_fail, mem); + break; + } + + case TOX_EVENT_GROUP_MODERATION: { + tox_event_group_moderation_free(event->data.group_moderation, 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_GROUP_PEER_NAME: + return tox_event_group_peer_name_pack(event->data.group_peer_name, bp); + + case TOX_EVENT_GROUP_PEER_STATUS: + return tox_event_group_peer_status_pack(event->data.group_peer_status, bp); + + case TOX_EVENT_GROUP_TOPIC: + return tox_event_group_topic_pack(event->data.group_topic, bp); + + case TOX_EVENT_GROUP_PRIVACY_STATE: + return tox_event_group_privacy_state_pack(event->data.group_privacy_state, bp); + + case TOX_EVENT_GROUP_VOICE_STATE: + return tox_event_group_voice_state_pack(event->data.group_voice_state, bp); + + case TOX_EVENT_GROUP_TOPIC_LOCK: + return tox_event_group_topic_lock_pack(event->data.group_topic_lock, bp); + + case TOX_EVENT_GROUP_PEER_LIMIT: + return tox_event_group_peer_limit_pack(event->data.group_peer_limit, bp); + + case TOX_EVENT_GROUP_PASSWORD: + return tox_event_group_password_pack(event->data.group_password, bp); + + case TOX_EVENT_GROUP_MESSAGE: + return tox_event_group_message_pack(event->data.group_message, bp); + + case TOX_EVENT_GROUP_PRIVATE_MESSAGE: + return tox_event_group_private_message_pack(event->data.group_private_message, bp); + + case TOX_EVENT_GROUP_CUSTOM_PACKET: + return tox_event_group_custom_packet_pack(event->data.group_custom_packet, bp); + + case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: + return tox_event_group_custom_private_packet_pack(event->data.group_custom_private_packet, bp); + + case TOX_EVENT_GROUP_INVITE: + return tox_event_group_invite_pack(event->data.group_invite, bp); + + case TOX_EVENT_GROUP_PEER_JOIN: + return tox_event_group_peer_join_pack(event->data.group_peer_join, bp); + + case TOX_EVENT_GROUP_PEER_EXIT: + return tox_event_group_peer_exit_pack(event->data.group_peer_exit, bp); + + case TOX_EVENT_GROUP_SELF_JOIN: + return tox_event_group_self_join_pack(event->data.group_self_join, bp); + + case TOX_EVENT_GROUP_JOIN_FAIL: + return tox_event_group_join_fail_pack(event->data.group_join_fail, bp); + + case TOX_EVENT_GROUP_MODERATION: + return tox_event_group_moderation_pack(event->data.group_moderation, 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_GROUP_PEER_NAME: { + *out = TOX_EVENT_GROUP_PEER_NAME; + return true; + } + + case TOX_EVENT_GROUP_PEER_STATUS: { + *out = TOX_EVENT_GROUP_PEER_STATUS; + return true; + } + + case TOX_EVENT_GROUP_TOPIC: { + *out = TOX_EVENT_GROUP_TOPIC; + return true; + } + + case TOX_EVENT_GROUP_PRIVACY_STATE: { + *out = TOX_EVENT_GROUP_PRIVACY_STATE; + return true; + } + + case TOX_EVENT_GROUP_VOICE_STATE: { + *out = TOX_EVENT_GROUP_VOICE_STATE; + return true; + } + + case TOX_EVENT_GROUP_TOPIC_LOCK: { + *out = TOX_EVENT_GROUP_TOPIC_LOCK; + return true; + } + + case TOX_EVENT_GROUP_PEER_LIMIT: { + *out = TOX_EVENT_GROUP_PEER_LIMIT; + return true; + } + + case TOX_EVENT_GROUP_PASSWORD: { + *out = TOX_EVENT_GROUP_PASSWORD; + return true; + } + + case TOX_EVENT_GROUP_MESSAGE: { + *out = TOX_EVENT_GROUP_MESSAGE; + return true; + } + + case TOX_EVENT_GROUP_PRIVATE_MESSAGE: { + *out = TOX_EVENT_GROUP_PRIVATE_MESSAGE; + return true; + } + + case TOX_EVENT_GROUP_CUSTOM_PACKET: { + *out = TOX_EVENT_GROUP_CUSTOM_PACKET; + return true; + } + + case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: { + *out = TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET; + return true; + } + + case TOX_EVENT_GROUP_INVITE: { + *out = TOX_EVENT_GROUP_INVITE; + return true; + } + + case TOX_EVENT_GROUP_PEER_JOIN: { + *out = TOX_EVENT_GROUP_PEER_JOIN; + return true; + } + + case TOX_EVENT_GROUP_PEER_EXIT: { + *out = TOX_EVENT_GROUP_PEER_EXIT; + return true; + } + + case TOX_EVENT_GROUP_SELF_JOIN: { + *out = TOX_EVENT_GROUP_SELF_JOIN; + return true; + } + + case TOX_EVENT_GROUP_JOIN_FAIL: { + *out = TOX_EVENT_GROUP_JOIN_FAIL; + return true; + } + + case TOX_EVENT_GROUP_MODERATION: { + *out = TOX_EVENT_GROUP_MODERATION; + 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_GROUP_PEER_NAME: + return tox_event_group_peer_name_unpack(&event->data.group_peer_name, bu, mem); + + case TOX_EVENT_GROUP_PEER_STATUS: + return tox_event_group_peer_status_unpack(&event->data.group_peer_status, bu, mem); + + case TOX_EVENT_GROUP_TOPIC: + return tox_event_group_topic_unpack(&event->data.group_topic, bu, mem); + + case TOX_EVENT_GROUP_PRIVACY_STATE: + return tox_event_group_privacy_state_unpack(&event->data.group_privacy_state, bu, mem); + + case TOX_EVENT_GROUP_VOICE_STATE: + return tox_event_group_voice_state_unpack(&event->data.group_voice_state, bu, mem); + + case TOX_EVENT_GROUP_TOPIC_LOCK: + return tox_event_group_topic_lock_unpack(&event->data.group_topic_lock, bu, mem); + + case TOX_EVENT_GROUP_PEER_LIMIT: + return tox_event_group_peer_limit_unpack(&event->data.group_peer_limit, bu, mem); + + case TOX_EVENT_GROUP_PASSWORD: + return tox_event_group_password_unpack(&event->data.group_password, bu, mem); + + case TOX_EVENT_GROUP_MESSAGE: + return tox_event_group_message_unpack(&event->data.group_message, bu, mem); + + case TOX_EVENT_GROUP_PRIVATE_MESSAGE: + return tox_event_group_private_message_unpack(&event->data.group_private_message, bu, mem); + + case TOX_EVENT_GROUP_CUSTOM_PACKET: + return tox_event_group_custom_packet_unpack(&event->data.group_custom_packet, bu, mem); + + case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: + return tox_event_group_custom_private_packet_unpack(&event->data.group_custom_private_packet, bu, mem); + + case TOX_EVENT_GROUP_INVITE: + return tox_event_group_invite_unpack(&event->data.group_invite, bu, mem); + + case TOX_EVENT_GROUP_PEER_JOIN: + return tox_event_group_peer_join_unpack(&event->data.group_peer_join, bu, mem); + + case TOX_EVENT_GROUP_PEER_EXIT: + return tox_event_group_peer_exit_unpack(&event->data.group_peer_exit, bu, mem); + + case TOX_EVENT_GROUP_SELF_JOIN: + return tox_event_group_self_join_unpack(&event->data.group_self_join, bu, mem); + + case TOX_EVENT_GROUP_JOIN_FAIL: + return tox_event_group_join_fail_unpack(&event->data.group_join_fail, bu, mem); + + case TOX_EVENT_GROUP_MODERATION: + return tox_event_group_moderation_unpack(&event->data.group_moderation, bu, mem); + + case TOX_EVENT_INVALID: + return false; + } + + return false; +} diff --git a/toxcore/tox_event.h b/toxcore/tox_event.h new file mode 100644 index 00000000..c28ba79b --- /dev/null +++ b/toxcore/tox_event.h @@ -0,0 +1,254 @@ +/* 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_Group_Peer_Name *group_peer_name; + Tox_Event_Group_Peer_Status *group_peer_status; + Tox_Event_Group_Topic *group_topic; + Tox_Event_Group_Privacy_State *group_privacy_state; + Tox_Event_Group_Voice_State *group_voice_state; + Tox_Event_Group_Topic_Lock *group_topic_lock; + Tox_Event_Group_Peer_Limit *group_peer_limit; + Tox_Event_Group_Password *group_password; + Tox_Event_Group_Message *group_message; + Tox_Event_Group_Private_Message *group_private_message; + Tox_Event_Group_Custom_Packet *group_custom_packet; + Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet; + Tox_Event_Group_Invite *group_invite; + Tox_Event_Group_Peer_Join *group_peer_join; + Tox_Event_Group_Peer_Exit *group_peer_exit; + Tox_Event_Group_Self_Join *group_self_join; + Tox_Event_Group_Join_Fail *group_join_fail; + Tox_Event_Group_Moderation *group_moderation; +} 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); +non_null() Tox_Event_Group_Peer_Name *tox_event_group_peer_name_new(const Memory *mem); +non_null() Tox_Event_Group_Peer_Status *tox_event_group_peer_status_new(const Memory *mem); +non_null() Tox_Event_Group_Topic *tox_event_group_topic_new(const Memory *mem); +non_null() Tox_Event_Group_Privacy_State *tox_event_group_privacy_state_new(const Memory *mem); +non_null() Tox_Event_Group_Voice_State *tox_event_group_voice_state_new(const Memory *mem); +non_null() Tox_Event_Group_Topic_Lock *tox_event_group_topic_lock_new(const Memory *mem); +non_null() Tox_Event_Group_Peer_Limit *tox_event_group_peer_limit_new(const Memory *mem); +non_null() Tox_Event_Group_Password *tox_event_group_password_new(const Memory *mem); +non_null() Tox_Event_Group_Message *tox_event_group_message_new(const Memory *mem); +non_null() Tox_Event_Group_Private_Message *tox_event_group_private_message_new(const Memory *mem); +non_null() Tox_Event_Group_Custom_Packet *tox_event_group_custom_packet_new(const Memory *mem); +non_null() Tox_Event_Group_Custom_Private_Packet *tox_event_group_custom_private_packet_new(const Memory *mem); +non_null() Tox_Event_Group_Invite *tox_event_group_invite_new(const Memory *mem); +non_null() Tox_Event_Group_Peer_Join *tox_event_group_peer_join_new(const Memory *mem); +non_null() Tox_Event_Group_Peer_Exit *tox_event_group_peer_exit_new(const Memory *mem); +non_null() Tox_Event_Group_Self_Join *tox_event_group_self_join_new(const Memory *mem); +non_null() Tox_Event_Group_Join_Fail *tox_event_group_join_fail_new(const Memory *mem); +non_null() Tox_Event_Group_Moderation *tox_event_group_moderation_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); +non_null(2) nullable(1) void tox_event_group_peer_name_free(Tox_Event_Group_Peer_Name *group_peer_name, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_peer_status_free(Tox_Event_Group_Peer_Status *group_peer_status, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_topic_free(Tox_Event_Group_Topic *group_topic, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_privacy_state_free(Tox_Event_Group_Privacy_State *group_privacy_state, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_voice_state_free(Tox_Event_Group_Voice_State *group_voice_state, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_topic_lock_free(Tox_Event_Group_Topic_Lock *group_topic_lock, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_peer_limit_free(Tox_Event_Group_Peer_Limit *group_peer_limit, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_password_free(Tox_Event_Group_Password *group_password, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_message_free(Tox_Event_Group_Message *group_message, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_private_message_free(Tox_Event_Group_Private_Message *group_private_message, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_custom_packet_free(Tox_Event_Group_Custom_Packet *group_custom_packet, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_custom_private_packet_free(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_invite_free(Tox_Event_Group_Invite *group_invite, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_peer_join_free(Tox_Event_Group_Peer_Join *group_peer_join, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_peer_exit_free(Tox_Event_Group_Peer_Exit *group_peer_exit, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_self_join_free(Tox_Event_Group_Self_Join *group_self_join, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_join_fail_free(Tox_Event_Group_Join_Fail *group_join_fail, const Memory *mem); +non_null(2) nullable(1) void tox_event_group_moderation_free(Tox_Event_Group_Moderation *group_moderation, 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); +non_null() bool tox_event_group_peer_name_pack(const Tox_Event_Group_Peer_Name *event, Bin_Pack *bp); +non_null() bool tox_event_group_peer_status_pack(const Tox_Event_Group_Peer_Status *event, Bin_Pack *bp); +non_null() bool tox_event_group_topic_pack(const Tox_Event_Group_Topic *event, Bin_Pack *bp); +non_null() bool tox_event_group_privacy_state_pack(const Tox_Event_Group_Privacy_State *event, Bin_Pack *bp); +non_null() bool tox_event_group_voice_state_pack(const Tox_Event_Group_Voice_State *event, Bin_Pack *bp); +non_null() bool tox_event_group_topic_lock_pack(const Tox_Event_Group_Topic_Lock *event, Bin_Pack *bp); +non_null() bool tox_event_group_peer_limit_pack(const Tox_Event_Group_Peer_Limit *event, Bin_Pack *bp); +non_null() bool tox_event_group_password_pack(const Tox_Event_Group_Password *event, Bin_Pack *bp); +non_null() bool tox_event_group_message_pack(const Tox_Event_Group_Message *event, Bin_Pack *bp); +non_null() bool tox_event_group_private_message_pack(const Tox_Event_Group_Private_Message *event, Bin_Pack *bp); +non_null() bool tox_event_group_custom_packet_pack(const Tox_Event_Group_Custom_Packet *event, Bin_Pack *bp); +non_null() bool tox_event_group_custom_private_packet_pack(const Tox_Event_Group_Custom_Private_Packet *event, Bin_Pack *bp); +non_null() bool tox_event_group_invite_pack(const Tox_Event_Group_Invite *event, Bin_Pack *bp); +non_null() bool tox_event_group_peer_join_pack(const Tox_Event_Group_Peer_Join *event, Bin_Pack *bp); +non_null() bool tox_event_group_peer_exit_pack(const Tox_Event_Group_Peer_Exit *event, Bin_Pack *bp); +non_null() bool tox_event_group_self_join_pack(const Tox_Event_Group_Self_Join *event, Bin_Pack *bp); +non_null() bool tox_event_group_join_fail_pack(const Tox_Event_Group_Join_Fail *event, Bin_Pack *bp); +non_null() bool tox_event_group_moderation_pack(const Tox_Event_Group_Moderation *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); +non_null() bool tox_event_group_peer_name_unpack(Tox_Event_Group_Peer_Name **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_peer_status_unpack(Tox_Event_Group_Peer_Status **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_topic_unpack(Tox_Event_Group_Topic **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_privacy_state_unpack(Tox_Event_Group_Privacy_State **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_voice_state_unpack(Tox_Event_Group_Voice_State **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_topic_lock_unpack(Tox_Event_Group_Topic_Lock **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_peer_limit_unpack(Tox_Event_Group_Peer_Limit **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_password_unpack(Tox_Event_Group_Password **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_message_unpack(Tox_Event_Group_Message **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_private_message_unpack(Tox_Event_Group_Private_Message **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_custom_packet_unpack(Tox_Event_Group_Custom_Packet **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_custom_private_packet_unpack(Tox_Event_Group_Custom_Private_Packet **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_invite_unpack(Tox_Event_Group_Invite **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_peer_join_unpack(Tox_Event_Group_Peer_Join **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_peer_exit_unpack(Tox_Event_Group_Peer_Exit **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_self_join_unpack(Tox_Event_Group_Self_Join **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_join_fail_unpack(Tox_Event_Group_Join_Fail **event, Bin_Unpack *bu, const Memory *mem); +non_null() bool tox_event_group_moderation_unpack(Tox_Event_Group_Moderation **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 c61d31f7..431f0766 100644 --- a/toxcore/tox_events.c +++ b/toxcore/tox_events.c @@ -67,9 +67,24 @@ void tox_events_init(Tox *tox) tox_callback_group_moderation(tox, tox_events_handle_group_moderation); } +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) { @@ -86,231 +101,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) - + tox_events_get_group_peer_name_size(events) - + tox_events_get_group_peer_status_size(events) - + tox_events_get_group_topic_size(events) - + tox_events_get_group_privacy_state_size(events) - + tox_events_get_group_voice_state_size(events) - + tox_events_get_group_topic_lock_size(events) - + tox_events_get_group_peer_limit_size(events) - + tox_events_get_group_password_size(events) - + tox_events_get_group_message_size(events) - + tox_events_get_group_private_message_size(events) - + tox_events_get_group_custom_packet_size(events) - + tox_events_get_group_custom_private_packet_size(events) - + tox_events_get_group_invite_size(events) - + tox_events_get_group_peer_join_size(events) - + tox_events_get_group_peer_exit_size(events) - + tox_events_get_group_self_join_size(events) - + tox_events_get_group_join_fail_size(events) - + tox_events_get_group_moderation_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) - && tox_events_pack_group_peer_name(events, bp) - && tox_events_pack_group_peer_status(events, bp) - && tox_events_pack_group_topic(events, bp) - && tox_events_pack_group_privacy_state(events, bp) - && tox_events_pack_group_voice_state(events, bp) - && tox_events_pack_group_topic_lock(events, bp) - && tox_events_pack_group_peer_limit(events, bp) - && tox_events_pack_group_password(events, bp) - && tox_events_pack_group_message(events, bp) - && tox_events_pack_group_private_message(events, bp) - && tox_events_pack_group_custom_packet(events, bp) - && tox_events_pack_group_custom_private_packet(events, bp) - && tox_events_pack_group_invite(events, bp) - && tox_events_pack_group_peer_join(events, bp) - && tox_events_pack_group_peer_exit(events, bp) - && tox_events_pack_group_self_join(events, bp) - && tox_events_pack_group_join_fail(events, bp) - && tox_events_pack_group_moderation(events, bp); -} - -non_null() -static bool tox_event_unpack(Tox_Events *events, Bin_Unpack *bu) -{ - uint32_t size; - if (!bin_unpack_array(bu, &size)) { + const uint32_t size = tox_events_get_size(events); + if (!bin_pack_array(bp, size)) { return false; } - if (size != 2) { - 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); - - case TOX_EVENT_GROUP_PEER_NAME: - return tox_events_unpack_group_peer_name(events, bu); - - case TOX_EVENT_GROUP_PEER_STATUS: - return tox_events_unpack_group_peer_status(events, bu); - - case TOX_EVENT_GROUP_TOPIC: - return tox_events_unpack_group_topic(events, bu); - - case TOX_EVENT_GROUP_PRIVACY_STATE: - return tox_events_unpack_group_privacy_state(events, bu); - - case TOX_EVENT_GROUP_VOICE_STATE: - return tox_events_unpack_group_voice_state(events, bu); - - case TOX_EVENT_GROUP_TOPIC_LOCK: - return tox_events_unpack_group_topic_lock(events, bu); - - case TOX_EVENT_GROUP_PEER_LIMIT: - return tox_events_unpack_group_peer_limit(events, bu); - - case TOX_EVENT_GROUP_PASSWORD: - return tox_events_unpack_group_password(events, bu); - - case TOX_EVENT_GROUP_MESSAGE: - return tox_events_unpack_group_message(events, bu); - - case TOX_EVENT_GROUP_PRIVATE_MESSAGE: - return tox_events_unpack_group_private_message(events, bu); - - case TOX_EVENT_GROUP_CUSTOM_PACKET: - return tox_events_unpack_group_custom_packet(events, bu); - - case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: - return tox_events_unpack_group_custom_private_packet(events, bu); - - case TOX_EVENT_GROUP_INVITE: - return tox_events_unpack_group_invite(events, bu); - - case TOX_EVENT_GROUP_PEER_JOIN: - return tox_events_unpack_group_peer_join(events, bu); - - case TOX_EVENT_GROUP_PEER_EXIT: - return tox_events_unpack_group_peer_exit(events, bu); - - case TOX_EVENT_GROUP_SELF_JOIN: - return tox_events_unpack_group_self_join(events, bu); - - case TOX_EVENT_GROUP_JOIN_FAIL: - return tox_events_unpack_group_join_fail(events, bu); - - case TOX_EVENT_GROUP_MODERATION: - return tox_events_unpack_group_moderation(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)) { @@ -318,11 +123,20 @@ bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu) } for (uint32_t i = 0; i < size; ++i) { - if (!tox_event_unpack(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; } @@ -337,9 +151,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) @@ -360,8 +174,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 39196273..40108489 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); @@ -190,7 +190,7 @@ uint32_t tox_event_group_peer_name_get_peer_id( const Tox_Event_Group_Peer_Name *group_peer_name); const uint8_t *tox_event_group_peer_name_get_name( const Tox_Event_Group_Peer_Name *group_peer_name); -size_t tox_event_group_peer_name_get_name_length( +uint32_t tox_event_group_peer_name_get_name_length( const Tox_Event_Group_Peer_Name *group_peer_name); typedef struct Tox_Event_Group_Peer_Status Tox_Event_Group_Peer_Status; @@ -208,7 +208,7 @@ uint32_t tox_event_group_topic_get_peer_id( const Tox_Event_Group_Topic *group_topic); const uint8_t *tox_event_group_topic_get_topic( const Tox_Event_Group_Topic *group_topic); -size_t tox_event_group_topic_get_topic_length( +uint32_t tox_event_group_topic_get_topic_length( const Tox_Event_Group_Topic *group_topic); typedef struct Tox_Event_Group_Privacy_State Tox_Event_Group_Privacy_State; @@ -240,7 +240,7 @@ uint32_t tox_event_group_password_get_group_number( const Tox_Event_Group_Password *group_password); const uint8_t *tox_event_group_password_get_password( const Tox_Event_Group_Password *group_password); -size_t tox_event_group_password_get_password_length( +uint32_t tox_event_group_password_get_password_length( const Tox_Event_Group_Password *group_password); typedef struct Tox_Event_Group_Message Tox_Event_Group_Message; @@ -252,7 +252,7 @@ Tox_Message_Type tox_event_group_message_get_type( const Tox_Event_Group_Message *group_message); const uint8_t *tox_event_group_message_get_message( const Tox_Event_Group_Message *group_message); -size_t tox_event_group_message_get_message_length( +uint32_t tox_event_group_message_get_message_length( const Tox_Event_Group_Message *group_message); uint32_t tox_event_group_message_get_message_id( const Tox_Event_Group_Message *group_message); @@ -266,7 +266,7 @@ Tox_Message_Type tox_event_group_private_message_get_type( const Tox_Event_Group_Private_Message *group_private_message); const uint8_t *tox_event_group_private_message_get_message( const Tox_Event_Group_Private_Message *group_private_message); -size_t tox_event_group_private_message_get_message_length( +uint32_t tox_event_group_private_message_get_message_length( const Tox_Event_Group_Private_Message *group_private_message); typedef struct Tox_Event_Group_Custom_Packet Tox_Event_Group_Custom_Packet; @@ -276,7 +276,7 @@ uint32_t tox_event_group_custom_packet_get_peer_id( const Tox_Event_Group_Custom_Packet *group_custom_packet); const uint8_t *tox_event_group_custom_packet_get_data( const Tox_Event_Group_Custom_Packet *group_custom_packet); -size_t tox_event_group_custom_packet_get_data_length( +uint32_t tox_event_group_custom_packet_get_data_length( const Tox_Event_Group_Custom_Packet *group_custom_packet); typedef struct Tox_Event_Group_Custom_Private_Packet Tox_Event_Group_Custom_Private_Packet; @@ -286,7 +286,7 @@ uint32_t tox_event_group_custom_private_packet_get_peer_id( const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet); const uint8_t *tox_event_group_custom_private_packet_get_data( const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet); -size_t tox_event_group_custom_private_packet_get_data_length( +uint32_t tox_event_group_custom_private_packet_get_data_length( const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet); typedef struct Tox_Event_Group_Invite Tox_Event_Group_Invite; @@ -294,11 +294,11 @@ uint32_t tox_event_group_invite_get_friend_number( const Tox_Event_Group_Invite *group_invite); const uint8_t *tox_event_group_invite_get_invite_data( const Tox_Event_Group_Invite *group_invite); -size_t tox_event_group_invite_get_invite_data_length( +uint32_t tox_event_group_invite_get_invite_data_length( const Tox_Event_Group_Invite *group_invite); const uint8_t *tox_event_group_invite_get_group_name( const Tox_Event_Group_Invite *group_invite); -size_t tox_event_group_invite_get_group_name_length( +uint32_t tox_event_group_invite_get_group_name_length( const Tox_Event_Group_Invite *group_invite); typedef struct Tox_Event_Group_Peer_Join Tox_Event_Group_Peer_Join; @@ -316,11 +316,11 @@ Tox_Group_Exit_Type tox_event_group_peer_exit_get_exit_type( const Tox_Event_Group_Peer_Exit *group_peer_exit); const uint8_t *tox_event_group_peer_exit_get_name( const Tox_Event_Group_Peer_Exit *group_peer_exit); -size_t tox_event_group_peer_exit_get_name_length( +uint32_t tox_event_group_peer_exit_get_name_length( const Tox_Event_Group_Peer_Exit *group_peer_exit); const uint8_t *tox_event_group_peer_exit_get_part_message( const Tox_Event_Group_Peer_Exit *group_peer_exit); -size_t tox_event_group_peer_exit_get_part_message_length( +uint32_t tox_event_group_peer_exit_get_part_message_length( const Tox_Event_Group_Peer_Exit *group_peer_exit); typedef struct Tox_Event_Group_Self_Join Tox_Event_Group_Self_Join; @@ -343,7 +343,7 @@ uint32_t tox_event_group_moderation_get_target_peer_id( Tox_Group_Mod_Event tox_event_group_moderation_get_mod_type( const Tox_Event_Group_Moderation *group_moderation); -typedef enum Tox_Event { +typedef enum Tox_Event_Type { TOX_EVENT_SELF_CONNECTION_STATUS = 0, TOX_EVENT_FRIEND_REQUEST = 1, @@ -390,7 +390,102 @@ typedef enum Tox_Event { TOX_EVENT_GROUP_SELF_JOIN = 36, TOX_EVENT_GROUP_JOIN_FAIL = 37, TOX_EVENT_GROUP_MODERATION = 38, -} 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); +const Tox_Event_Group_Peer_Name *tox_event_get_group_peer_name( + const Tox_Event *event); +const Tox_Event_Group_Peer_Status *tox_event_get_group_peer_status( + const Tox_Event *event); +const Tox_Event_Group_Topic *tox_event_get_group_topic( + const Tox_Event *event); +const Tox_Event_Group_Privacy_State *tox_event_get_group_privacy_state( + const Tox_Event *event); +const Tox_Event_Group_Voice_State *tox_event_get_group_voice_state( + const Tox_Event *event); +const Tox_Event_Group_Topic_Lock *tox_event_get_group_topic_lock( + const Tox_Event *event); +const Tox_Event_Group_Peer_Limit *tox_event_get_group_peer_limit( + const Tox_Event *event); +const Tox_Event_Group_Password *tox_event_get_group_password( + const Tox_Event *event); +const Tox_Event_Group_Message *tox_event_get_group_message( + const Tox_Event *event); +const Tox_Event_Group_Private_Message *tox_event_get_group_private_message( + const Tox_Event *event); +const Tox_Event_Group_Custom_Packet *tox_event_get_group_custom_packet( + const Tox_Event *event); +const Tox_Event_Group_Custom_Private_Packet *tox_event_get_group_custom_private_packet( + const Tox_Event *event); +const Tox_Event_Group_Invite *tox_event_get_group_invite( + const Tox_Event *event); +const Tox_Event_Group_Peer_Join *tox_event_get_group_peer_join( + const Tox_Event *event); +const Tox_Event_Group_Peer_Exit *tox_event_get_group_peer_exit( + const Tox_Event *event); +const Tox_Event_Group_Self_Join *tox_event_get_group_self_join( + const Tox_Event *event); +const Tox_Event_Group_Join_Fail *tox_event_get_group_join_fail( + const Tox_Event *event); +const Tox_Event_Group_Moderation *tox_event_get_group_moderation( + const Tox_Event *event); /** * Container object for all Tox core events. @@ -399,6 +494,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); @@ -573,7 +671,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 5de29b5e..fa76d393 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); } diff --git a/toxcore/tox_private.c b/toxcore/tox_private.c index cab991f2..abe09d6b 100644 --- a/toxcore/tox_private.c +++ b/toxcore/tox_private.c @@ -167,7 +167,6 @@ uint16_t tox_dht_get_num_closelist_announce_capable(const Tox *tox){ return num_cap; } -#ifndef VANILLA_NACL size_t tox_group_peer_get_ip_address_size(const Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Err_Group_Peer_Query *error) { @@ -219,5 +218,3 @@ bool tox_group_peer_get_ip_address(const Tox *tox, uint32_t group_number, uint32 SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_PEER_QUERY_OK); return true; } - -#endif /* VANILLA_NACL */ diff --git a/toxcore/tox_test.cc b/toxcore/tox_test.cc index 530c1390..2deb961c 100644 --- a/toxcore/tox_test.cc +++ b/toxcore/tox_test.cc @@ -82,6 +82,13 @@ TEST(Tox, OneTest) struct Tox_Options *options = tox_options_new(nullptr); ASSERT_NE(options, nullptr); + tox_options_set_log_callback(options, + [](Tox *tox, Tox_Log_Level level, const char *file, uint32_t line, const char *func, + const char *message, void *user_data) { + fprintf(stderr, "[%c] %s:%d(%s): %s\n", tox_log_level_to_string(level)[0], file, line, + func, message); + }); + // Higher start/end point here to avoid conflict with the LAN discovery test. tox_options_set_start_port(options, 33545); tox_options_set_end_port(options, 33545 + 2000); diff --git a/toxcore/tox_unpack.c b/toxcore/tox_unpack.c index 09f5de2a..9d58879a 100644 --- a/toxcore/tox_unpack.c +++ b/toxcore/tox_unpack.c @@ -9,276 +9,337 @@ #include "bin_unpack.h" #include "ccompat.h" -static Tox_Conference_Type tox_conference_type_from_int(uint32_t value) +non_null() +static bool tox_conference_type_from_int(uint32_t value, Tox_Conference_Type *out) { switch (value) { - case 0: - return TOX_CONFERENCE_TYPE_TEXT; - case 1: - return TOX_CONFERENCE_TYPE_AV; - default: - return TOX_CONFERENCE_TYPE_TEXT; + case TOX_CONFERENCE_TYPE_TEXT: { + *out = TOX_CONFERENCE_TYPE_TEXT; + return true; + } + + case TOX_CONFERENCE_TYPE_AV: { + *out = TOX_CONFERENCE_TYPE_AV; + return true; + } + + default: { + *out = TOX_CONFERENCE_TYPE_TEXT; + return false; + } } } -bool tox_unpack_conference_type(Bin_Unpack *bu, Tox_Conference_Type *val) +bool tox_conference_type_unpack(Bin_Unpack *bu, Tox_Conference_Type *val) { uint32_t u32; - - if (!bin_unpack_u32(bu, &u32)) { - return false; - } - - *val = tox_conference_type_from_int(u32); - return true; + return bin_unpack_u32(bu, &u32) + && tox_conference_type_from_int(u32, val); } -static Tox_Connection tox_connection_from_int(uint32_t value) +non_null() +static bool tox_connection_from_int(uint32_t value, Tox_Connection *out) { switch (value) { - case 0: - return TOX_CONNECTION_NONE; - case 1: - return TOX_CONNECTION_TCP; - case 2: - return TOX_CONNECTION_UDP; - default: - return TOX_CONNECTION_NONE; + case TOX_CONNECTION_NONE: { + *out = TOX_CONNECTION_NONE; + return true; + } + + case TOX_CONNECTION_TCP: { + *out = TOX_CONNECTION_TCP; + return true; + } + + case TOX_CONNECTION_UDP: { + *out = TOX_CONNECTION_UDP; + return true; + } + + default: { + *out = TOX_CONNECTION_NONE; + return false; + } } } -bool tox_unpack_connection(Bin_Unpack *bu, Tox_Connection *val) + +bool tox_connection_unpack(Bin_Unpack *bu, Tox_Connection *val) { uint32_t u32; - - if (!bin_unpack_u32(bu, &u32)) { - return false; - } - - *val = tox_connection_from_int(u32); - return true; + return bin_unpack_u32(bu, &u32) + && tox_connection_from_int(u32, val); } -static Tox_File_Control tox_file_control_from_int(uint32_t value) +non_null() +static bool tox_file_control_from_int(uint32_t value, Tox_File_Control *out) { switch (value) { - case 0: - return TOX_FILE_CONTROL_RESUME; - case 1: - return TOX_FILE_CONTROL_PAUSE; - case 2: - return TOX_FILE_CONTROL_CANCEL; - default: - return TOX_FILE_CONTROL_RESUME; + case TOX_FILE_CONTROL_RESUME: { + *out = TOX_FILE_CONTROL_RESUME; + return true; + } + + case TOX_FILE_CONTROL_PAUSE: { + *out = TOX_FILE_CONTROL_PAUSE; + return true; + } + + case TOX_FILE_CONTROL_CANCEL: { + *out = TOX_FILE_CONTROL_CANCEL; + return true; + } + + default: { + *out = TOX_FILE_CONTROL_RESUME; + return false; + } } } -bool tox_unpack_file_control(Bin_Unpack *bu, Tox_File_Control *val) + +bool tox_file_control_unpack(Bin_Unpack *bu, Tox_File_Control *val) { uint32_t u32; - - if (!bin_unpack_u32(bu, &u32)) { - return false; - } - - *val = tox_file_control_from_int(u32); - return true; + return bin_unpack_u32(bu, &u32) + && tox_file_control_from_int(u32, val); } -static Tox_Message_Type tox_message_type_from_int(uint32_t value) +non_null() +static bool tox_message_type_from_int(uint32_t value, Tox_Message_Type *out) { switch (value) { - case 0: - return TOX_MESSAGE_TYPE_NORMAL; - case 1: - return TOX_MESSAGE_TYPE_ACTION; - default: - return TOX_MESSAGE_TYPE_NORMAL; + case TOX_MESSAGE_TYPE_NORMAL: { + *out = TOX_MESSAGE_TYPE_NORMAL; + return true; + } + + case TOX_MESSAGE_TYPE_ACTION: { + *out = TOX_MESSAGE_TYPE_ACTION; + return true; + } + + default: { + *out = TOX_MESSAGE_TYPE_NORMAL; + return false; + } } } -bool tox_unpack_message_type(Bin_Unpack *bu, Tox_Message_Type *val) + +bool tox_message_type_unpack(Bin_Unpack *bu, Tox_Message_Type *val) { uint32_t u32; - - if (!bin_unpack_u32(bu, &u32)) { - return false; - } - - *val = tox_message_type_from_int(u32); - return true; + return bin_unpack_u32(bu, &u32) + && tox_message_type_from_int(u32, val); } -static Tox_User_Status tox_user_status_from_int(uint32_t value) +non_null() +static bool tox_user_status_from_int(uint32_t value, Tox_User_Status *out) { switch (value) { - case 0: - return TOX_USER_STATUS_NONE; - case 1: - return TOX_USER_STATUS_AWAY; - case 2: - return TOX_USER_STATUS_BUSY; - default: - return TOX_USER_STATUS_NONE; + case TOX_USER_STATUS_NONE: { + *out = TOX_USER_STATUS_NONE; + return true; + } + + case TOX_USER_STATUS_AWAY: { + *out = TOX_USER_STATUS_AWAY; + return true; + } + + case TOX_USER_STATUS_BUSY: { + *out = TOX_USER_STATUS_BUSY; + return true; + } + + default: { + *out = TOX_USER_STATUS_NONE; + return false; + } } } -bool tox_unpack_user_status(Bin_Unpack *bu, Tox_User_Status *val) + +bool tox_user_status_unpack(Bin_Unpack *bu, Tox_User_Status *val) { uint32_t u32; - - if (!bin_unpack_u32(bu, &u32)) { - return false; - } - - *val = tox_user_status_from_int(u32); - return true; + return bin_unpack_u32(bu, &u32) + && tox_user_status_from_int(u32, val); } -static Tox_Group_Privacy_State tox_group_privacy_state_from_int(uint32_t value) +non_null() +static bool tox_group_privacy_state_from_int(uint32_t value, Tox_Group_Privacy_State *out) { - switch (value) { - case 0: - return TOX_GROUP_PRIVACY_STATE_PUBLIC; - case 1: - return TOX_GROUP_PRIVACY_STATE_PRIVATE; - default: - return TOX_GROUP_PRIVACY_STATE_PRIVATE; + switch (value) { + case TOX_GROUP_PRIVACY_STATE_PUBLIC: { + *out = TOX_GROUP_PRIVACY_STATE_PUBLIC; + return true; } + case TOX_GROUP_PRIVACY_STATE_PRIVATE: { + *out = TOX_GROUP_PRIVACY_STATE_PRIVATE; + return true; + } + default: { + *out = TOX_GROUP_PRIVACY_STATE_PUBLIC; + return false; + } + } } -bool tox_unpack_group_privacy_state(Bin_Unpack *bu, Tox_Group_Privacy_State *val) +bool tox_group_privacy_state_unpack(Bin_Unpack *bu, Tox_Group_Privacy_State *val) { uint32_t u32; - - if (!bin_unpack_u32(bu, &u32)) { - return false; - } - - *val = tox_group_privacy_state_from_int(u32); - return true; + return bin_unpack_u32(bu, &u32) + && tox_group_privacy_state_from_int(u32, val); } -static Tox_Group_Voice_State tox_group_voice_state_from_int(uint32_t value) +non_null() +static bool tox_group_voice_state_from_int(uint32_t value, Tox_Group_Voice_State *out) { - switch (value) { - case 0: - return TOX_GROUP_VOICE_STATE_ALL; - case 1: - return TOX_GROUP_VOICE_STATE_MODERATOR; - case 2: - return TOX_GROUP_VOICE_STATE_FOUNDER; - default: - return TOX_GROUP_VOICE_STATE_FOUNDER; + switch (value) { + case TOX_GROUP_VOICE_STATE_ALL: { + *out = TOX_GROUP_VOICE_STATE_ALL; + return true; } + case TOX_GROUP_VOICE_STATE_MODERATOR: { + *out = TOX_GROUP_VOICE_STATE_MODERATOR; + return true; + } + case TOX_GROUP_VOICE_STATE_FOUNDER: { + *out = TOX_GROUP_VOICE_STATE_FOUNDER; + return true; + } + default: { + *out = TOX_GROUP_VOICE_STATE_ALL; + return false; + } + } } -bool tox_unpack_group_voice_state(Bin_Unpack *bu, Tox_Group_Voice_State *val) +bool tox_group_voice_state_unpack(Bin_Unpack *bu, Tox_Group_Voice_State *val) { uint32_t u32; - - if (!bin_unpack_u32(bu, &u32)) { - return false; - } - - *val = tox_group_voice_state_from_int(u32); - return true; + return bin_unpack_u32(bu, &u32) + && tox_group_voice_state_from_int(u32, val); } -static Tox_Group_Topic_Lock tox_group_topic_lock_from_int(uint32_t value) +non_null() +static bool tox_group_topic_lock_from_int(uint32_t value, Tox_Group_Topic_Lock *out) { - switch (value) { - case 0: - return TOX_GROUP_TOPIC_LOCK_ENABLED; - case 1: - return TOX_GROUP_TOPIC_LOCK_DISABLED; - default: - return TOX_GROUP_TOPIC_LOCK_ENABLED; + switch (value) { + case TOX_GROUP_TOPIC_LOCK_ENABLED: { + *out = TOX_GROUP_TOPIC_LOCK_ENABLED; + return true; } + case TOX_GROUP_TOPIC_LOCK_DISABLED: { + *out = TOX_GROUP_TOPIC_LOCK_DISABLED; + return true; + } + default: { + *out = TOX_GROUP_TOPIC_LOCK_ENABLED; + return false; + } + } } -bool tox_unpack_group_topic_lock(Bin_Unpack *bu, Tox_Group_Topic_Lock *val) +bool tox_group_topic_lock_unpack(Bin_Unpack *bu, Tox_Group_Topic_Lock *val) { uint32_t u32; - - if (!bin_unpack_u32(bu, &u32)) { - return false; - } - - *val = tox_group_topic_lock_from_int(u32); - return true; + return bin_unpack_u32(bu, &u32) + && tox_group_topic_lock_from_int(u32, val); } -static Tox_Group_Join_Fail tox_group_join_fail_from_int(uint32_t value) +non_null() +static bool tox_group_join_fail_from_int(uint32_t value, Tox_Group_Join_Fail *out) { - switch (value) { - case 0: - return TOX_GROUP_JOIN_FAIL_PEER_LIMIT; - case 1: - return TOX_GROUP_JOIN_FAIL_INVALID_PASSWORD; - case 2: - return TOX_GROUP_JOIN_FAIL_UNKNOWN; - default: - return TOX_GROUP_JOIN_FAIL_UNKNOWN; + switch (value) { + case TOX_GROUP_JOIN_FAIL_PEER_LIMIT: { + *out = TOX_GROUP_JOIN_FAIL_PEER_LIMIT; + return true; } + case TOX_GROUP_JOIN_FAIL_INVALID_PASSWORD: { + *out = TOX_GROUP_JOIN_FAIL_INVALID_PASSWORD; + return true; + } + case TOX_GROUP_JOIN_FAIL_UNKNOWN: { + *out = TOX_GROUP_JOIN_FAIL_UNKNOWN; + return true; + } + default: { + *out = TOX_GROUP_JOIN_FAIL_PEER_LIMIT; + return false; + } + } } -bool tox_unpack_group_join_fail(Bin_Unpack *bu, Tox_Group_Join_Fail *val) +bool tox_group_join_fail_unpack(Bin_Unpack *bu, Tox_Group_Join_Fail *val) { uint32_t u32; - - if (!bin_unpack_u32(bu, &u32)) { - return false; - } - - *val = tox_group_join_fail_from_int(u32); - return true; + return bin_unpack_u32(bu, &u32) + && tox_group_join_fail_from_int(u32, val); } -static Tox_Group_Mod_Event tox_group_mod_event_from_int(uint32_t value) +non_null() +static bool tox_group_mod_event_from_int(uint32_t value, Tox_Group_Mod_Event *out) { - switch (value) { - case 0: - return TOX_GROUP_MOD_EVENT_KICK; - case 1: - return TOX_GROUP_MOD_EVENT_OBSERVER; - case 2: - return TOX_GROUP_MOD_EVENT_USER; - case 3: - return TOX_GROUP_MOD_EVENT_MODERATOR; - default: - return TOX_GROUP_MOD_EVENT_MODERATOR; + switch (value) { + case TOX_GROUP_MOD_EVENT_KICK: { + *out = TOX_GROUP_MOD_EVENT_KICK; + return true; } + case TOX_GROUP_MOD_EVENT_OBSERVER: { + *out = TOX_GROUP_MOD_EVENT_OBSERVER; + return true; + } + case TOX_GROUP_MOD_EVENT_USER: { + *out = TOX_GROUP_MOD_EVENT_USER; + return true; + } + case TOX_GROUP_MOD_EVENT_MODERATOR: { + *out = TOX_GROUP_MOD_EVENT_MODERATOR; + return true; + } + default: { + *out = TOX_GROUP_MOD_EVENT_KICK; + return false; + } + } } -bool tox_unpack_group_mod_event(Bin_Unpack *bu, Tox_Group_Mod_Event *val) +bool tox_group_mod_event_unpack(Bin_Unpack *bu, Tox_Group_Mod_Event *val) { uint32_t u32; - - if (!bin_unpack_u32(bu, &u32)) { - return false; - } - - *val = tox_group_mod_event_from_int(u32); - return true; + return bin_unpack_u32(bu, &u32) + && tox_group_mod_event_from_int(u32, val); } -static Tox_Group_Exit_Type tox_group_exit_type_from_int(uint32_t value) +non_null() +static bool tox_group_exit_type_from_int(uint32_t value, Tox_Group_Exit_Type *out) { - switch (value) { - case 0: - return TOX_GROUP_EXIT_TYPE_QUIT; - case 1: - return TOX_GROUP_EXIT_TYPE_TIMEOUT; - case 2: - return TOX_GROUP_EXIT_TYPE_DISCONNECTED; - case 3: - return TOX_GROUP_EXIT_TYPE_SELF_DISCONNECTED; - case 4: - return TOX_GROUP_EXIT_TYPE_KICK; - case 5: - return TOX_GROUP_EXIT_TYPE_SYNC_ERROR; - default: - return TOX_GROUP_EXIT_TYPE_QUIT; + switch (value) { + case TOX_GROUP_EXIT_TYPE_QUIT: { + *out = TOX_GROUP_EXIT_TYPE_QUIT; + return true; } + case TOX_GROUP_EXIT_TYPE_TIMEOUT: { + *out = TOX_GROUP_EXIT_TYPE_TIMEOUT; + return true; + } + case TOX_GROUP_EXIT_TYPE_DISCONNECTED: { + *out = TOX_GROUP_EXIT_TYPE_DISCONNECTED; + return true; + } + case TOX_GROUP_EXIT_TYPE_SELF_DISCONNECTED: { + *out = TOX_GROUP_EXIT_TYPE_SELF_DISCONNECTED; + return true; + } + case TOX_GROUP_EXIT_TYPE_KICK: { + *out = TOX_GROUP_EXIT_TYPE_KICK; + return true; + } + case TOX_GROUP_EXIT_TYPE_SYNC_ERROR: { + *out = TOX_GROUP_EXIT_TYPE_SYNC_ERROR; + return true; + } + default: { + *out = TOX_GROUP_EXIT_TYPE_QUIT; + return false; + } + } } -bool tox_unpack_group_exit_type(Bin_Unpack *bu, Tox_Group_Exit_Type *val) +bool tox_group_exit_type_unpack(Bin_Unpack *bu, Tox_Group_Exit_Type *val) { uint32_t u32; - - if (!bin_unpack_u32(bu, &u32)) { - return false; - } - - *val = tox_group_exit_type_from_int(u32); - return true; + return bin_unpack_u32(bu, &u32) + && tox_group_exit_type_from_int(u32, val); } diff --git a/toxcore/tox_unpack.h b/toxcore/tox_unpack.h index 69bb207b..af134ca5 100644 --- a/toxcore/tox_unpack.h +++ b/toxcore/tox_unpack.h @@ -9,16 +9,16 @@ #include "bin_unpack.h" #include "tox.h" -non_null() bool tox_unpack_conference_type(Bin_Unpack *bu, Tox_Conference_Type *val); -non_null() bool tox_unpack_connection(Bin_Unpack *bu, Tox_Connection *val); -non_null() bool tox_unpack_file_control(Bin_Unpack *bu, Tox_File_Control *val); -non_null() bool tox_unpack_message_type(Bin_Unpack *bu, Tox_Message_Type *val); -non_null() bool tox_unpack_user_status(Bin_Unpack *bu, Tox_User_Status *val); -non_null() bool tox_unpack_group_privacy_state(Bin_Unpack *bu, Tox_Group_Privacy_State *val); -non_null() bool tox_unpack_group_voice_state(Bin_Unpack *bu, Tox_Group_Voice_State *val); -non_null() bool tox_unpack_group_topic_lock(Bin_Unpack *bu, Tox_Group_Topic_Lock *val); -non_null() bool tox_unpack_group_join_fail(Bin_Unpack *bu, Tox_Group_Join_Fail *val); -non_null() bool tox_unpack_group_mod_event(Bin_Unpack *bu, Tox_Group_Mod_Event *val); -non_null() bool tox_unpack_group_exit_type(Bin_Unpack *bu, Tox_Group_Exit_Type *val); +non_null() bool tox_conference_type_unpack(Bin_Unpack *bu, Tox_Conference_Type *val); +non_null() bool tox_connection_unpack(Bin_Unpack *bu, Tox_Connection *val); +non_null() bool tox_file_control_unpack(Bin_Unpack *bu, Tox_File_Control *val); +non_null() bool tox_message_type_unpack(Bin_Unpack *bu, Tox_Message_Type *val); +non_null() bool tox_user_status_unpack(Bin_Unpack *bu, Tox_User_Status *val); +non_null() bool tox_group_privacy_state_unpack(Bin_Unpack *bu, Tox_Group_Privacy_State *val); +non_null() bool tox_group_voice_state_unpack(Bin_Unpack *bu, Tox_Group_Voice_State *val); +non_null() bool tox_group_topic_lock_unpack(Bin_Unpack *bu, Tox_Group_Topic_Lock *val); +non_null() bool tox_group_join_fail_unpack(Bin_Unpack *bu, Tox_Group_Join_Fail *val); +non_null() bool tox_group_mod_event_unpack(Bin_Unpack *bu, Tox_Group_Mod_Event *val); +non_null() bool tox_group_exit_type_unpack(Bin_Unpack *bu, Tox_Group_Exit_Type *val); #endif // C_TOXCORE_TOXCORE_TOX_UNPACK_H diff --git a/toxencryptsave/Makefile.inc b/toxencryptsave/Makefile.inc index 4b517a19..154c6744 100644 --- a/toxencryptsave/Makefile.inc +++ b/toxencryptsave/Makefile.inc @@ -5,7 +5,6 @@ libtoxencryptsave_la_include_HEADERS = \ libtoxencryptsave_la_includedir = $(includedir)/tox -if !WITH_NACL libtoxencryptsave_la_SOURCES = ../toxencryptsave/toxencryptsave.h \ ../toxencryptsave/toxencryptsave.c \ ../toxencryptsave/defines.h @@ -13,20 +12,16 @@ libtoxencryptsave_la_SOURCES = ../toxencryptsave/toxencryptsave.h \ libtoxencryptsave_la_CFLAGS = -I$(top_srcdir) \ -I$(top_srcdir)/toxcore \ $(LIBSODIUM_CFLAGS) \ - $(NACL_CFLAGS) \ $(PTHREAD_CFLAGS) libtoxencryptsave_la_LDFLAGS = $(LT_LDFLAGS) \ $(EXTRA_LT_LDFLAGS) \ $(LIBSODIUM_LDFLAGS) \ - $(NACL_LDFLAGS) \ $(MATH_LDFLAGS) \ $(RT_LIBS) \ $(WINSOCK2_LIBS) libtoxencryptsave_la_LIBADD = $(LIBSODIUM_LIBS) \ - $(NACL_OBJECTS) \ - $(NACL_LIBS) \ $(PTHREAD_LIBS) \ libtoxcore.la @@ -35,4 +30,3 @@ if SET_SO_VERSION EXTRA_libtoxencryptsave_la_DEPENDENCIES = ../so.version endif -endif