refactor: Use cmp instead of msgpack-c for events packing.

This library is better in every way.
This commit is contained in:
iphydf 2022-03-24 18:16:45 +00:00
parent 6749974470
commit ee154da4a9
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
73 changed files with 1335 additions and 811 deletions

View File

@ -28,12 +28,12 @@ jobs:
apt-get update &&
DEBIAN_FRONTEND=noninteractive
apt-get install -y --no-install-recommends
ca-certificates
clang
cmake
git
libconfig-dev
libgtest-dev
libmsgpack-dev
libopus-dev
libsodium-dev
libvpx-dev
@ -41,6 +41,7 @@ jobs:
ninja-build
pkg-config
- checkout
- run: git submodule update --init --recursive
- run: CC=clang .circleci/cmake-asan
tsan:
@ -51,6 +52,7 @@ jobs:
steps:
- run: *apt_install
- checkout
- run: git submodule update --init --recursive
- run: CC=clang .circleci/cmake-tsan
ubsan:
@ -61,6 +63,7 @@ jobs:
steps:
- run: *apt_install
- checkout
- run: git submodule update --init --recursive
- run: CC=clang .circleci/cmake-ubsan
msan:
@ -70,6 +73,7 @@ jobs:
steps:
- checkout
- run: git submodule update --init --recursive
- run: rm -rf /src/workspace/c-toxcore/* && mv * /src/workspace/c-toxcore/
- run: cd /src/workspace && bazel test //c-toxcore/auto_tests:lossless_packet_test
@ -81,6 +85,7 @@ jobs:
steps:
- run: *apt_install
- checkout
- run: git submodule update --init --recursive
- run: infer --no-progress-bar -- cc
auto_tests/auto_test_support.c
auto_tests/lossless_packet_test.c
@ -89,8 +94,9 @@ jobs:
toxcore/*.c
toxcore/*/*.c
toxencryptsave/*.c
third_party/cmp/*.c
-lpthread
$(pkg-config --cflags --libs libsodium msgpack opus vpx)
$(pkg-config --cflags --libs libsodium opus vpx)
static-analysis:
working_directory: ~/work
@ -99,8 +105,14 @@ jobs:
steps:
- run: *apt_install
- run: apt-get install -y --no-install-recommends cppcheck g++ llvm-dev
- run:
apt-get install -y --no-install-recommends
ca-certificates
cppcheck
g++
llvm-dev
- checkout
- run: git submodule update --init --recursive
- run: other/analysis/check_includes
- run: other/analysis/check_logger_levels
- run: other/analysis/run-check-recursion
@ -116,6 +128,7 @@ jobs:
steps:
- run: *apt_install
- checkout
- run: git submodule update --init --recursive
- run: other/analysis/run-clang-analyze
clang-tidy:
@ -125,8 +138,12 @@ jobs:
steps:
- run: *apt_install
- run: apt-get install -y --no-install-recommends clang-tidy-12
- run:
apt-get install -y --no-install-recommends
ca-certificates
clang-tidy-12
- checkout
- run: git submodule update --init --recursive
- run: cmake . -B_build -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
- run:
other/analysis/run-clang-tidy ||
@ -140,7 +157,11 @@ jobs:
steps:
- run: *apt_install
- run: apt-get install -y --no-install-recommends python3-pip
- run:
apt-get install -y --no-install-recommends
ca-certificates
python3-pip
- checkout
- run: git submodule update --init --recursive
- run: pip install cpplint
- run: other/analysis/run-cpplint

View File

@ -5,6 +5,7 @@ bazel-opt_task:
cpu: 2
memory: 2G
configure_script:
- git submodule update --init --recursive
- /src/workspace/tools/inject-repo c-toxcore
test_all_script:
- cd /src/workspace && bazel test -k
@ -22,6 +23,7 @@ bazel-dbg_task:
cpu: 2
memory: 2G
configure_script:
- git submodule update --init --recursive
- /src/workspace/tools/inject-repo c-toxcore
test_all_script:
- cd /src/workspace && bazel test -k
@ -39,6 +41,7 @@ bazel-asan_task:
cpu: 2
memory: 4G
configure_script:
- git submodule update --init --recursive
- /src/workspace/tools/inject-repo c-toxcore
test_all_script:
- cd /src/workspace && bazel test -k
@ -57,6 +60,7 @@ bazel-asan_task:
# cpu: 2
# memory: 4G
# configure_script:
# - git submodule update --init --recursive
# - /src/workspace/tools/inject-repo c-toxcore
# test_all_script:
# - cd /src/workspace && bazel test -k
@ -75,6 +79,7 @@ bazel-tsan_task:
cpu: 2
memory: 4G
configure_script:
- git submodule update --init --recursive
- /src/workspace/tools/inject-repo c-toxcore
test_all_script:
- cd /src/workspace && bazel test -k
@ -98,6 +103,7 @@ bazel-valgrind_task:
cpu: 2
memory: 4G
configure_script:
- git submodule update --init --recursive
- /src/workspace/tools/inject-repo c-toxcore
test_all_script:
- cd /src/workspace && bazel test -k
@ -123,6 +129,7 @@ cimple_task:
cpu: 2
memory: 4G
configure_script:
- git submodule update --init --recursive
- /src/workspace/tools/inject-repo c-toxcore
test_all_script:
- cd /src/workspace && bazel test -k

View File

@ -17,11 +17,6 @@ WORKDIR $SRC/libsodium
RUN ./autogen.sh && ./configure --enable-shared=no && make install
WORKDIR $SRC
# msgpack-c
RUN git clone --depth 1 --branch c_master https://github.com/msgpack/msgpack-c.git msgpack
WORKDIR $SRC/msgpack
RUN cmake -DBUILD_SHARED_LIBS=OFF . && make && make install
# Copy your project's source code.
COPY . $SRC/c-toxcore
# Working directory for build.sh.

View File

@ -51,22 +51,6 @@ autoreconf -fi
make -j"$(nproc)" install
cd ..
# Build msgpack
if [ ! -d msgpack-c ]; then
git clone --branch=c-4.0.0 https://github.com/msgpack/msgpack-c.git
fi
rm -rf msgpack-c/build
mkdir -p msgpack-c/build
cd msgpack-c/build
cmake .. \
-DMSGPACK_BUILD_EXAMPLES=OFF \
-DCMAKE_TOOLCHAIN_FILE="$NDK/build/cmake/android.toolchain.cmake" \
-DANDROID_ABI="$ABI" \
-DCMAKE_INSTALL_PREFIX="$PREFIX" \
-DCMAKE_PREFIX_PATH="$PREFIX"
cmake --build . --target install
cd ../..
# Build c-toxcore
rm -rf _build
mkdir -p _build

View File

@ -11,7 +11,6 @@ brew install \
libconfig \
libsodium \
libvpx \
msgpack \
opus
. ".github/scripts/flags-clang.sh"

View File

@ -6,7 +6,6 @@ NPROC=$(nproc)
sudo apt-get install -y --no-install-recommends \
libgtest-dev \
libmsgpack-dev \
libopus-dev \
libsodium-dev \
libvpx-dev \

View File

@ -4,7 +4,6 @@ set -eu
sudo apt-get install -y --no-install-recommends \
libconfig-dev \
libmsgpack-dev \
libopus-dev \
libsodium-dev \
libvpx-dev \

View File

@ -17,6 +17,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Set up Python 3.9
uses: actions/setup-python@v1
with:
@ -52,6 +54,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Run cimplefmt
run: other/docker/cimplefmt/run -u $(find tox* -name "*.[ch]")
@ -69,6 +73,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Cross compilation
run: .github/scripts/cmake-win32 script
@ -76,6 +82,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Cross compilation
run: .github/scripts/cmake-win64 script
@ -84,6 +92,8 @@ jobs:
container: toxchat/freebsd
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Build on FreeBSD
run: .github/scripts/cmake-freebsd-stage2
@ -91,6 +101,8 @@ jobs:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Build and test
run: .github/scripts/cmake-osx
@ -98,6 +110,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Build, test, and upload coverage
run: .github/scripts/coverage-linux
@ -105,12 +119,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Install dependencies
run:
sudo apt-get install -y --no-install-recommends
tcc
libconfig-dev
libmsgpack-dev
libopus-dev
libsodium-dev
libvpx-dev
@ -128,7 +143,8 @@ jobs:
toxcore/*.c
toxcore/*/*.c
toxencryptsave/*.c
$(pkg-config --cflags --libs libsodium msgpack opus vpx)
third_party/cmp/*.c
$(pkg-config --cflags --libs libsodium opus vpx)
- name: Run the test
run: "./send_message_test | grep 'tox clients connected'"
- name: Build amalgamation file with TCC
@ -141,7 +157,7 @@ jobs:
-o send_message_test
-Wall -Werror
-bench -g
$(pkg-config --cflags --libs libsodium msgpack opus vpx)
$(pkg-config --cflags --libs libsodium opus vpx)
- name: Run the test again
run: "./send_message_test | grep 'tox clients connected'"
@ -150,6 +166,8 @@ jobs:
container: toxchat/compcert
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Build with CompCert
run:
ccomp
@ -166,8 +184,9 @@ jobs:
toxcore/*.c
toxcore/*/*.c
toxencryptsave/*.c
third_party/cmp/*.c
-D__COMPCERT__ -DDISABLE_VLA -Dinline=
-lpthread $(pkg-config --cflags --libs libsodium msgpack opus vpx)
-lpthread $(pkg-config --cflags --libs libsodium opus vpx)
- name: Run the test
run: "./send_message_test | grep 'tox clients connected'"
@ -175,6 +194,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- run: .github/scripts/cmake-android armeabi-v7a
- run: .github/scripts/cmake-android arm64-v8a
- run: .github/scripts/cmake-android x86

View File

@ -9,11 +9,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Install libraries
run:
sudo apt-get update &&
sudo apt-get install -y --no-install-recommends
libmsgpack-dev
libopus-dev
libsodium-dev
libvpx-dev

View File

@ -17,6 +17,8 @@ jobs:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Docker Build
run: .github/scripts/tox-bootstrapd-docker local
- name: Push latest image to DockerHub

View File

@ -17,6 +17,7 @@ jobs:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
submodules: recursive
- name: Set up JDK 11
uses: actions/setup-java@v1
with:

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "third_party/cmp"]
path = third_party/cmp
url = https://github.com/TokTok/cmp

View File

@ -145,6 +145,10 @@ if(BUILD_TOXAV)
endif()
endif()
# Disable float/double packing in CMP (C MessagePack library).
# We don't transfer floats over the network, so we disable this functionality.
add_definitions(-DCMP_NO_FLOAT=1)
################################################################################
#
# :: Tox Core Library
@ -253,6 +257,8 @@ set(toxcore_API_HEADERS ${toxcore_API_HEADERS} ${toxcore_SOURCE_DIR}/toxcore/tox
# LAYER 9: New async events API
# -------------------
set(toxcore_SOURCES ${toxcore_SOURCES}
third_party/cmp/cmp.c
third_party/cmp/cmp.h
toxcore/events/conference_connected.c
toxcore/events/conference_invite.c
toxcore/events/conference_message.c
@ -285,8 +291,6 @@ set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/tox_unpack.c
toxcore/tox_unpack.h)
set(toxcore_API_HEADERS ${toxcore_API_HEADERS} ${toxcore_SOURCE_DIR}/toxcore/tox_events.h^tox)
set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${MSGPACK_LIBRARIES})
set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} msgpack)
# LAYER 10: Dispatch recorded events to callbacks.
# -------------------
@ -405,6 +409,7 @@ include(CompileGTest)
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 mono_time)
unit_test(toxcore ping_array)

View File

@ -32,13 +32,13 @@ 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, libmsgpack | 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). |
| 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). |
#### Secondary
@ -247,7 +247,6 @@ Build the container image based on the Dockerfile. The following options are ava
| `SUPPORT_ARCH_x86_64` | Support building 64-bit toxcore. | "true" or "false" (case sensitive). | true |
| `SUPPORT_TEST` | Support running toxcore automated tests. | "true" or "false" (case sensitive). | false |
| `CROSS_COMPILE` | Cross-compiling. True for Docker, false for Cygwin. | "true" or "false" (case sensitive). | true |
| `VERSION_MSGPACK` | Version of libmsgpackc to build toxcore with. | Numeric version number. | 4.0.0 |
| `VERSION_OPUS` | Version of libopus to build toxcore with. | Numeric version number. | 1.3.1 |
| `VERSION_SODIUM` | Version of libsodium to build toxcore with. | Numeric version number. | 1.0.18 |
| `VERSION_VPX` | Version of libvpx to build toxcore with. | Numeric version number. | 1.11.0 |

View File

@ -9,6 +9,7 @@ install:
- py -3 -m pip install conan
before_build:
- git submodule update --init --recursive
- conan install -if _build .
build_script:

View File

@ -47,10 +47,6 @@ static void print_events(Tox_Events *events)
{
const uint32_t size = tox_events_bytes_size(events);
if (size > 1) {
tox_events_print(events);
}
uint8_t *bytes = (uint8_t *)malloc(size);
ck_assert(bytes != nullptr);
@ -60,14 +56,7 @@ static void print_events(Tox_Events *events)
ck_assert(events_copy != nullptr);
free(bytes);
if (!tox_events_equal(events, events_copy)) {
printf("serialised and deserialised events are not equal:\n");
printf("a = ");
tox_events_print(events);
printf("b = ");
tox_events_print(events_copy);
abort();
}
ck_assert(tox_events_equal(events, events_copy));
tox_events_free(events_copy);
tox_events_free(events);

View File

@ -37,6 +37,12 @@ static bool await_message(Tox **toxes)
return false;
}
static uint64_t get_state_clock_callback(Mono_Time *mono_time, void *user_data)
{
const uint64_t *clock = (const uint64_t *)user_data;
return *clock;
}
static void test_tox_events(void)
{
uint8_t message[sizeof("hello")];
@ -52,6 +58,16 @@ static void test_tox_events(void)
ck_assert_msg(toxes[i] != nullptr, "failed to create tox instances %u", i);
}
// TODO(iphydf): Don't rely on toxcore internals.
uint64_t clock = current_time_monotonic(((Messenger *)toxes[0])->mono_time);
Mono_Time *mono_time;
// TODO(iphydf): Don't rely on toxcore internals.
mono_time = ((Messenger *)toxes[0])->mono_time;
mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &clock);
mono_time = ((Messenger *)toxes[1])->mono_time;
mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &clock);
uint8_t pk[TOX_PUBLIC_KEY_SIZE];
tox_self_get_dht_id(toxes[0], pk);
tox_bootstrap(toxes[1], "localhost", tox_self_get_udp_port(toxes[0], nullptr), pk, nullptr);
@ -70,7 +86,8 @@ static void test_tox_events(void)
tox_events_free(tox_events_iterate(toxes[0], false, nullptr));
tox_events_free(tox_events_iterate(toxes[1], false, nullptr));
c_sleep(tox_iteration_interval(toxes[0]));
clock += 100;
c_sleep(5);
}
printf("toxes online, waiting for friend connection\n");
@ -81,7 +98,8 @@ static void test_tox_events(void)
tox_events_free(tox_events_iterate(toxes[0], false, nullptr));
tox_events_free(tox_events_iterate(toxes[1], false, nullptr));
c_sleep(tox_iteration_interval(toxes[0]));
clock += 100;
c_sleep(5);
}
printf("friends are connected via %s, now sending message\n",

View File

@ -14,8 +14,6 @@ find_library(SOCKET_LIBRARIES socket )
# For toxcore.
pkg_use_module(LIBSODIUM libsodium )
pkg_use_module(MSGPACK msgpack )
#find_package(msgpack REQUIRED)
# For toxav.
pkg_use_module(OPUS "opus;Opus" )

View File

@ -14,7 +14,7 @@ class ToxConan(ConanFile):
description = "The future of online communications."
license = "GPL-3.0-only"
settings = "os", "compiler", "build_type", "arch"
requires = "libsodium/1.0.18", "opus/1.3.1", "msgpack/3.3.0", "libvpx/1.9.0"
requires = "libsodium/1.0.18", "opus/1.3.1", "libvpx/1.9.0"
generators = "cmake_find_package"
scm = {"type": "git", "url": "auto", "revision": "auto"}

View File

@ -189,8 +189,6 @@ LIBSODIUM_SEARCH_HEADERS=
LIBSODIUM_SEARCH_LIBS=
NACL_SEARCH_HEADERS=
NACL_SEARCH_LIBS=
MSGPACK_SEARCH_HEADERS=
MSGPACK_SEARCH_LIBS=
AC_ARG_WITH(dependency-search,
AC_HELP_STRING([--with-dependency-search=DIR],
@ -244,24 +242,6 @@ AC_ARG_WITH(libsodium-libs,
]
)
AC_ARG_WITH(msgpack-headers,
AC_HELP_STRING([--with-msgpack-headers=DIR],
[search for libmsgpackc header files in DIR]),
[
MSGPACK_SEARCH_HEADERS="$withval"
AC_MSG_NOTICE([will search for libmsgpackc header files in $withval])
]
)
AC_ARG_WITH(msgpack-libs,
AC_HELP_STRING([--with-msgpack-libs=DIR],
[search for libmsgpackc libraries in DIR]),
[
MSGPACK_SEARCH_LIBS="$withval"
AC_MSG_NOTICE([will search for libmsgpackc libraries in $withval])
]
)
if test "x$WANT_NACL" = "xyes"; then
enable_shared=no
enable_static=yes
@ -453,71 +433,6 @@ elif test "x$LIBSODIUM_FOUND" = "xno"; then
AC_SUBST(LIBSODIUM_CFLAGS)
fi
PKG_CHECK_MODULES([MSGPACK], [msgpack],
[
MSGPACK_FOUND="yes"
],
[
MSGPACK_FOUND="no"
])
if test "x$MSGPACK_FOUND" = "xno"; then
MSGPACK_LIBS=
MSGPACK_LDFLAGS=
LDFLAGS_SAVE="$LDFLAGS"
if test -n "$MSGPACK_SEARCH_LIBS"; then
LDFLAGS="-L$MSGPACK_SEARCH_LIBS $LDFLAGS"
AC_CHECK_LIB(msgpackc, msgpack_sbuffer_init,
[
MSGPACK_LDFLAGS="-L$MSGPACK_SEARCH_LIBS"
MSGPACK_LIBS="-lmsgpackc"
],
[
AC_MSG_ERROR([required library libmsgpackc was not found in requested location $MSGPACK_SEARCH_LIBS or library version is too old])
]
)
else
AC_CHECK_LIB(msgpackc, msgpack_sbuffer_init,
[],
[
AC_MSG_ERROR([required library libmsgpackc was not found on your system, please check https://github.com/msgpack/msgpack-c or library version is too old])
]
)
fi
LDFLAGS="$LDFLAGS_SAVE"
AC_SUBST(MSGPACK_LIBS)
AC_SUBST(MSGPACK_LDFLAGS)
fi
if test "x$MSGPACK_FOUND" = "xno"; then
MSGPACK_CFLAGS=
CFLAGS_SAVE="$CFLAGS"
CPPFLAGS_SAVE="$CPPFLAGS"
if test -n "$MSGPACK_SEARCH_HEADERS"; then
CFLAGS="-I$MSGPACK_SEARCH_HEADERS $CFLAGS"
CPPFLAGS="-I$MSGPACK_SEARCH_HEADERS $CPPFLAGS"
AC_CHECK_HEADER(msgpack.h,
[
MSGPACK_CFLAGS="-I$MSGPACK_SEARCH_HEADERS"
],
[
AC_MSG_ERROR([header files for required library libmsgpackc were not found in requested location $MSGPACK_SEARCH_HEADERS])
]
)
else
AC_CHECK_HEADER(msgpack.h,
[],
[
AC_MSG_ERROR([header files for required library libmsgpackc was not found on your system, please check https://github.com/msgpack/msgpack-c])
]
)
fi
CFLAGS="$CFLAGS_SAVE"
CPPFLAGS="$CPPFLAGS_SAVE"
AC_SUBST(MSGPACK_CFLAGS)
fi
# Checks for library functions.
if (test "x$WIN32" != "xyes") && (test "x$MACH" != "xyes") && (test "x${host_os#*openbsd}" = "x$host_os") && (test "x$DISABLE_RT" != "xyes"); then
AC_CHECK_LIB(rt, clock_gettime,

View File

@ -11,8 +11,6 @@ ALLOWLIST: Tuple[str, ...] = (
"stddef.h",
"stdint.h",
"time.h", # time_t used in Messenger.h TODO(iphydf): maybe don't?
# msgpack, currently not abstracted away
"msgpack.h",
# toxav stuff, maybe not worth abstracting away
"opus.h",
"vpx/vp8cx.h",

View File

@ -1,6 +1,7 @@
#!/bin/bash
CPPFLAGS="-DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE"
CPPFLAGS+=("-DCMP_NO_FLOAT=1")
CPPFLAGS+=("-isystem" "/usr/include/opus")
CPPFLAGS+=("-Iauto_tests")
CPPFLAGS+=("-Iother")
@ -12,8 +13,9 @@ CPPFLAGS+=("-Itoxcore")
CPPFLAGS+=("-Itoxcore/events")
CPPFLAGS+=("-Itoxav")
CPPFLAGS+=("-Itoxencryptsave")
CPPFLAGS+=("-Ithird_party/cmp")
LDFLAGS=("-lopus" "-lmsgpackc" "-lsodium" "-lvpx" "-lpthread" "-lconfig" "-lgtest")
LDFLAGS=("-lopus" "-lsodium" "-lvpx" "-lpthread" "-lconfig" "-lgtest")
LDFLAGS+=("-fuse-ld=gold")
LDFLAGS+=("-Wl,--detect-odr-violations")
LDFLAGS+=("-Wl,--warn-common")
@ -57,6 +59,8 @@ FIND_QUERY="$FIND_QUERY -and -not -name cracker.c"
FIND_QUERY="$FIND_QUERY -and -not -name version_test.c"
FIND_QUERY="$FIND_QUERY -and -not -name '*_fuzz_test.cc'"
FIND_QUERY="$FIND_QUERY -and -not -wholename './testing/fuzzing/*'"
FIND_QUERY="$FIND_QUERY -and -not -wholename './third_party/cmp/examples/*'"
FIND_QUERY="$FIND_QUERY -and -not -wholename './third_party/cmp/test/*'"
if [ "$SKIP_GTEST" == 1 ]; then
FIND_QUERY="$FIND_QUERY -and -not -name '*_test.cc'"

View File

@ -24,6 +24,8 @@ CPPCHECK_C=("--suppress=unusedFunction")
# We actually write C code.
CPPCHECK_CXX=("--suppress=cstyleCast")
# False positive in cmp.c.
CPPCHECK_CXX+=("--suppress=objectIndex")
# False positive in auto_tests.
CPPCHECK_CXX+=("--suppress=shadowArgument")
CPPCHECK_CXX+=("--suppress=shadowFunction")

View File

@ -10,7 +10,6 @@ RUN ["apk", "--no-cache", "add",\
"libconfig-static",\
"libsodium-dev",\
"libsodium-static",\
"msgpack-c-dev",\
"ninja",\
"python3"\
]
@ -30,6 +29,7 @@ COPY other/rpm other/rpm
COPY testing/misc_tools.[ch] testing/
COPY toxcore toxcore
COPY toxencryptsave toxencryptsave
COPY third_party third_party
COPY CMakeLists.txt so.version ./
RUN cmake -B_build -H. \

View File

@ -3,8 +3,7 @@
set -eux
docker_build() {
readarray -t FILES <<<"$(git ls-files)"
tar c "${FILES[@]}" | docker build -f other/bootstrap_daemon/docker/Dockerfile -t toxchat/bootstrap-node -
docker build -f other/bootstrap_daemon/docker/Dockerfile -t toxchat/bootstrap-node .
}
# Run Docker build once. If it succeeds, we're good.

View File

@ -8,7 +8,6 @@ RUN apt-get update && \
cmake \
libconfig-dev \
libgtest-dev \
libmsgpack-dev \
libopus-dev \
libsodium-dev \
libvpx-dev \

View File

@ -10,7 +10,6 @@ RUN apt-get update && \
golang \
libconfig-dev \
libgtest-dev \
libmsgpack-dev \
libopus-dev \
libsodium-dev \
libvpx-dev \

View File

@ -4,7 +4,6 @@ RUN ["apk", "add", "--no-cache", \
"bash", \
"gcc", \
"linux-headers", \
"msgpack-c-dev", \
"musl-dev", \
"libsodium-dev", \
"libvpx-dev", \

View File

@ -5,7 +5,7 @@ set -eux
TEST=${1:-conference_test}
OUTPUT="/work/c-toxcore/test.perf"
readarray -t FLAGS <<<"$(pkg-config --cflags --libs libsodium msgpack opus vpx | sed -e 's/ /\n/g')"
readarray -t FLAGS <<<"$(pkg-config --cflags --libs libsodium opus vpx | sed -e 's/ /\n/g')"
readarray -t SRCS <<<"$(find /work/c-toxcore/tox* -name "*.c")"
gcc -pthread -g \

View File

@ -3,7 +3,6 @@ FROM ubuntu:20.04
RUN apt-get update && \
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
libc-dev \
libmsgpack-dev \
libopus-dev \
libsodium-dev \
libvpx-dev \
@ -31,7 +30,8 @@ RUN tcc \
toxcore/*.c \
toxcore/*/*.c \
toxencryptsave/*.c \
$(pkg-config --cflags --libs libsodium msgpack opus vpx)
third_party/cmp/*.c \
$(pkg-config --cflags --libs libsodium opus vpx)
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
COPY other/make_single_file /work/other/
@ -44,4 +44,4 @@ RUN \
-o send_message_test \
-Wall -Werror \
-bench -g \
$(pkg-config --cflags --libs libsodium msgpack opus vpx)
$(pkg-config --cflags --libs libsodium opus vpx)

View File

@ -5,7 +5,6 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
ca-certificates \
gcc \
git \
libmsgpack-dev \
libopus-dev \
libsodium-dev \
libvpx-dev \
@ -18,4 +17,5 @@ RUN ["git", "clone", "--depth=1", "https://github.com/TokTok/hs-tokstyle", "/src
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/cmp.h
RUN /bin/check-c $(find /src/workspace/c-toxcore -name "*.c")

View File

@ -38,18 +38,6 @@ build() {
SET(CMAKE_FIND_ROOT_PATH /usr/$WINDOWS_TOOLCHAIN $DEP_PREFIX_DIR)
" >windows_toolchain.cmake
echo
echo "=== Building Msgpack $VERSION_MSGPACK $ARCH ==="
curl "${CURL_OPTIONS[@]}" -O "https://github.com/msgpack/msgpack-c/releases/download/c-$VERSION_MSGPACK/msgpack-c-$VERSION_MSGPACK.tar.gz"
tar -xf "msgpack-c-$VERSION_MSGPACK.tar.gz"
cd "msgpack-c-$VERSION_MSGPACK"
cmake -DCMAKE_TOOLCHAIN_FILE=../windows_toolchain.cmake \
-DCMAKE_INSTALL_PREFIX="$PREFIX_DIR" \
-DBUILD_SHARED_LIBS=OFF \
.
cmake --build . --target install -- -j"$(nproc)"
cd ..
echo
echo "=== Building Sodium $VERSION_SODIUM $ARCH ==="
curl "${CURL_OPTIONS[@]}" -O "https://download.libsodium.org/libsodium/releases/libsodium-$VERSION_SODIUM.tar.gz"

View File

@ -36,16 +36,6 @@ RUN . "/work/emsdk/emsdk_env.sh" \
--disable-ssp --disable-asm --disable-pie \
&& emmake make install -j8
# Build msgpack.
RUN . "/work/emsdk/emsdk_env.sh" \
&& git clone --depth=1 --branch=c-4.0.0 https://github.com/msgpack/msgpack-c /work/msgpack-c \
&& cd /work/msgpack-c \
&& emcmake cmake -B_build -H. -GNinja \
-DCMAKE_INSTALL_PREFIX:PATH="/usr/local" \
-DCMAKE_C_FLAGS="-O3 -flto -fPIC" \
-DBUILD_SHARED_LIBS=OFF \
&& emmake cmake --build _build --parallel 8 --target install
# Build an unused libsodium binding first so emcc caches all the system
# libraries. This makes rebuilds of toxcore below much faster.
RUN . "/work/emsdk/emsdk_env.sh" \
@ -89,7 +79,6 @@ RUN . "/work/emsdk/emsdk_env.sh" \
-s MODULARIZE=1 \
-s STRICT=1 \
-s WEBSOCKET_URL=wss:// \
/usr/local/lib/libmsgpackc.a \
/usr/local/lib/libsodium.a \
/usr/local/lib/libtoxcore.a \
-o /work/wasm/libtoxcore.js

View File

@ -44,11 +44,11 @@ sub emit {
if (@ARGV and $ARGV[0] eq "-core") {
shift @ARGV;
for my $fn (<toxcore/*.c>) {
for my $fn (<toxcore/*.c>, <third_party/cmp/*.c>) {
emit(abs_path $fn);
}
} else {
for my $fn (<toxav/*.c>, <toxcore/*.c>, <toxcore/*/*.c>, <toxencryptsave/*.c>) {
for my $fn (<toxav/*.c>, <toxcore/*.c>, <toxcore/*/*.c>, <toxencryptsave/*.c>, <third_party/cmp/*.c>) {
emit(abs_path $fn);
}
}

View File

@ -1 +0,0 @@
*/

11
third_party/BUILD.bazel vendored Normal file
View File

@ -0,0 +1,11 @@
load("//tools:no_undefined.bzl", "cc_library")
package(features = ["layering_check"])
cc_library(
name = "cmp",
srcs = ["cmp/cmp.c"],
hdrs = ["cmp/cmp.h"],
copts = ["-DCMP_NO_FLOAT"],
visibility = ["//c-toxcore:__subpackages__"],
)

1
third_party/cmp vendored Submodule

@ -0,0 +1 @@
Subproject commit a3426f7125c033b337f4c5cfda5680aa055f1f69

View File

@ -22,7 +22,7 @@ cc_library(
visibility = ["//c-toxcore:__subpackages__"],
deps = [
":ccompat",
"@msgpack-c",
"//c-toxcore/third_party:cmp",
],
)
@ -33,7 +33,19 @@ cc_library(
visibility = ["//c-toxcore:__subpackages__"],
deps = [
":ccompat",
"@msgpack-c",
"//c-toxcore/third_party:cmp",
],
)
cc_test(
name = "bin_pack_test",
size = "small",
srcs = ["bin_pack_test.cc"],
deps = [
":bin_pack",
":bin_unpack",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)
@ -551,7 +563,6 @@ cc_library(
":bin_unpack",
":ccompat",
":tox",
"@msgpack-c",
],
)
@ -569,7 +580,7 @@ cc_library(
":ccompat",
":tox",
":tox_unpack",
"@msgpack-c",
"//c-toxcore/third_party:cmp",
],
)

View File

@ -5,7 +5,9 @@ libtoxcore_la_include_HEADERS = \
libtoxcore_la_includedir = $(includedir)/tox
libtoxcore_la_SOURCES = ../toxcore/attributes.h \
libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
../third_party/cmp/cmp.h \
../toxcore/attributes.h \
../toxcore/bin_pack.c \
../toxcore/bin_pack.h \
../toxcore/bin_unpack.c \
@ -98,7 +100,8 @@ libtoxcore_la_CFLAGS = -I$(top_srcdir) \
$(LIBSODIUM_CFLAGS) \
$(NACL_CFLAGS) \
$(MSGPACK_CFLAGS) \
$(PTHREAD_CFLAGS)
$(PTHREAD_CFLAGS) \
-DCMP_NO_FLOAT=1
libtoxcore_la_LDFLAGS = $(LT_LDFLAGS) \
$(EXTRA_LT_LDFLAGS) \

View File

@ -4,39 +4,153 @@
#include "bin_pack.h"
#include <msgpack.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
void bin_pack_array(msgpack_packer *mp, size_t size)
#include "../third_party/cmp/cmp.h"
#include "ccompat.h"
struct Bin_Pack {
uint8_t *bytes;
uint32_t bytes_size;
uint32_t bytes_pos;
cmp_ctx_t ctx;
};
non_null()
static bool null_reader(cmp_ctx_t *ctx, void *data, size_t limit)
{
msgpack_pack_array(mp, size);
assert(limit == 0);
return false;
}
void bin_pack_bool(msgpack_packer *mp, bool val)
non_null()
static bool null_skipper(cmp_ctx_t *ctx, size_t limit)
{
if (val) {
msgpack_pack_true(mp);
} else {
msgpack_pack_false(mp);
assert(limit == 0);
return false;
}
non_null()
static size_t buf_writer(cmp_ctx_t *ctx, const void *data, size_t count)
{
Bin_Pack *bp = (Bin_Pack *)ctx->buf;
assert(bp != nullptr);
const uint32_t new_pos = bp->bytes_pos + count;
if (new_pos < bp->bytes_pos) {
// 32 bit overflow.
return 0;
}
if (bp->bytes != nullptr) {
if (new_pos > bp->bytes_size) {
// Buffer too small.
return 0;
}
memcpy(bp->bytes + bp->bytes_pos, data, count);
}
bp->bytes_pos += count;
return count;
}
void bin_pack_u16(msgpack_packer *mp, uint16_t val)
non_null(1) nullable(2)
static void bin_pack_init(Bin_Pack *bp, uint8_t *buf, uint32_t buf_size)
{
msgpack_pack_uint16(mp, val);
bp->bytes = buf;
bp->bytes_size = buf_size;
bp->bytes_pos = 0;
cmp_init(&bp->ctx, bp, null_reader, null_skipper, buf_writer);
}
void bin_pack_u32(msgpack_packer *mp, uint32_t val)
bool bin_pack_obj(bin_pack_cb *callback, const void *obj, uint8_t *buf, uint32_t buf_size)
{
msgpack_pack_uint32(mp, val);
Bin_Pack bp;
bin_pack_init(&bp, buf, buf_size);
return callback(&bp, obj);
}
void bin_pack_u64(msgpack_packer *mp, uint64_t val)
uint32_t bin_pack_obj_size(bin_pack_cb *callback, const void *obj)
{
msgpack_pack_uint64(mp, val);
Bin_Pack bp;
bin_pack_init(&bp, nullptr, 0);
callback(&bp, obj);
return bp.bytes_pos;
}
void bin_pack_bytes(msgpack_packer *mp, const uint8_t *data, size_t length)
Bin_Pack *bin_pack_new(uint8_t *buf, uint32_t buf_size)
{
msgpack_pack_bin(mp, length);
msgpack_pack_bin_body(mp, data, length);
Bin_Pack *bp = (Bin_Pack *)calloc(1, sizeof(Bin_Pack));
if (bp == nullptr) {
return nullptr;
}
bin_pack_init(bp, buf, buf_size);
return bp;
}
void bin_pack_free(Bin_Pack *bp)
{
free(bp);
}
bool bin_pack_array(Bin_Pack *bp, uint32_t size)
{
return cmp_write_array(&bp->ctx, size);
}
bool bin_pack_bool(Bin_Pack *bp, bool val)
{
return cmp_write_bool(&bp->ctx, val);
}
bool bin_pack_u16(Bin_Pack *bp, uint16_t val)
{
return cmp_write_uinteger(&bp->ctx, val);
}
bool bin_pack_u32(Bin_Pack *bp, uint32_t val)
{
return cmp_write_uinteger(&bp->ctx, val);
}
bool bin_pack_u64(Bin_Pack *bp, uint64_t val)
{
return cmp_write_uinteger(&bp->ctx, val);
}
bool bin_pack_bytes(Bin_Pack *bp, const uint8_t *data, uint32_t length)
{
return cmp_write_bin(&bp->ctx, data, length);
}
bool bin_pack_bin(Bin_Pack *bp, uint32_t size)
{
return cmp_write_bin_marker(&bp->ctx, size);
}
bool bin_pack_u08_b(Bin_Pack *bp, uint8_t val)
{
return bp->ctx.write(&bp->ctx, &val, 1) == 1;
}
bool bin_pack_u16_b(Bin_Pack *bp, uint16_t val)
{
return bin_pack_u08_b(bp, (val >> 8) & 0xff)
&& bin_pack_u08_b(bp, val & 0xff);
}
bool bin_pack_u32_b(Bin_Pack *bp, uint32_t val)
{
return bin_pack_u16_b(bp, (val >> 16) & 0xffff)
&& bin_pack_u16_b(bp, val & 0xffff);
}
bool bin_pack_u64_b(Bin_Pack *bp, uint64_t val)
{
return bin_pack_u32_b(bp, (val >> 32) & 0xffffffff)
&& bin_pack_u32_b(bp, val & 0xffffffff);
}
bool bin_pack_bytes_b(Bin_Pack *bp, const uint8_t *data, uint32_t length)
{
return bp->ctx.write(&bp->ctx, data, length) == length;
}

View File

@ -1,21 +1,120 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2022 The TokTok team.
*/
#ifndef C_TOXCORE_TOXCORE_BIN_PACK_H
#define C_TOXCORE_TOXCORE_BIN_PACK_H
#include <msgpack.h>
#include <stdbool.h>
#include <stdint.h>
#include "attributes.h"
non_null() void bin_pack_array(msgpack_packer *mp, size_t size);
non_null() void bin_pack_bool(msgpack_packer *mp, bool val);
non_null() void bin_pack_u16(msgpack_packer *mp, uint16_t val);
non_null() void bin_pack_u32(msgpack_packer *mp, uint32_t val);
non_null() void bin_pack_u64(msgpack_packer *mp, uint64_t val);
non_null() void bin_pack_bytes(msgpack_packer *mp, const uint8_t *data, size_t length);
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Binary serialisation object.
*/
typedef struct Bin_Pack Bin_Pack;
/** @brief Function used to pack an object.
*
* This function would typically cast the `void *` to the actual object pointer type and then call
* more appropriately typed packing functions.
*/
typedef bool bin_pack_cb(Bin_Pack *bp, const void *obj);
/** @brief Determine the serialised size of an object.
*
* @param callback The function called on the created packer and packed object.
* @param obj The object to be packed, passed as `obj` to the callback.
*
* @return The packed size of the passed object according to the callback. UINT32_MAX in case of
* errors such as buffer overflow.
*/
non_null(1) nullable(2)
uint32_t bin_pack_obj_size(bin_pack_cb *callback, const void *obj);
/** @brief Pack an object into a buffer of a given size.
*
* This function creates and initialises a `Bin_Pack` packer object, calls the callback with the
* packer object and the to-be-packed object, and then cleans up the packer object.
*
* You can use `bin_pack_obj_size` to determine the minimum required size of `buf`. If packing
* overflows `uint32_t`, this function returns `false`.
*
* @param callback The function called on the created packer and packed object.
* @param obj The object to be packed, passed as `obj` to the callback.
* @param buf A byte array large enough to hold the serialised representation of `obj`.
* @param buf_size The size of the byte array. Can be `UINT32_MAX` to disable bounds checking.
*
* @retval false if an error occurred (e.g. buffer overflow).
*/
non_null(1, 3) nullable(2)
bool bin_pack_obj(bin_pack_cb *callback, const void *obj, uint8_t *buf, uint32_t buf_size);
/** @brief Allocate a new packer object.
*
* This is the only function that allocates memory in this module.
*
* @param buf A byte array large enough to hold the serialised representation of `obj`.
* @param buf_size The size of the byte array. Can be `UINT32_MAX` to disable bounds checking.
*
* @retval nullptr on allocation failure.
*/
non_null()
Bin_Pack *bin_pack_new(uint8_t *buf, uint32_t buf_size);
/** @brief Deallocates a packer object.
*
* Does not deallocate the buffer inside.
*/
non_null()
void bin_pack_free(Bin_Pack *bp);
/** @brief Start packing a MessagePack array.
*
* A call to this function must be followed by exactly `size` calls to other functions below.
*/
non_null()
bool bin_pack_array(Bin_Pack *bp, uint32_t size);
/** @brief Pack a MessagePack bool. */
non_null() bool bin_pack_bool(Bin_Pack *bp, bool val);
/** @brief Pack a `uint16_t` as MessagePack positive integer. */
non_null() bool bin_pack_u16(Bin_Pack *bp, uint16_t val);
/** @brief Pack a `uint32_t` as MessagePack positive integer. */
non_null() bool bin_pack_u32(Bin_Pack *bp, uint32_t val);
/** @brief Pack a `uint64_t` as MessagePack positive integer. */
non_null() bool bin_pack_u64(Bin_Pack *bp, uint64_t val);
/** @brief Pack a byte array as MessagePack bin. */
non_null() bool bin_pack_bytes(Bin_Pack *bp, const uint8_t *data, uint32_t length);
/** @brief Start packing a custom binary representation.
*
* A call to this function must be followed by exactly `size` bytes packed by functions below.
*/
non_null() bool bin_pack_bin(Bin_Pack *bp, uint32_t size);
/** @brief Write a `uint8_t` directly to the packer in 1 byte. */
non_null() bool bin_pack_u08_b(Bin_Pack *bp, uint8_t val);
/** @brief Write a `uint16_t` as big endian 16 bit int in 2 bytes. */
non_null() bool bin_pack_u16_b(Bin_Pack *bp, uint16_t val);
/** @brief Write a `uint32_t` as big endian 32 bit int in 4 bytes. */
non_null() bool bin_pack_u32_b(Bin_Pack *bp, uint32_t val);
/** @brief Write a `uint64_t` as big endian 64 bit int in 8 bytes. */
non_null() bool bin_pack_u64_b(Bin_Pack *bp, uint64_t val);
/** @brief Write a byte array directly to the packer in `length` bytes.
*
* Note that unless you prepend the array length manually, there is no record of it in the resulting
* serialised representation.
*/
non_null() bool bin_pack_bytes_b(Bin_Pack *bp, const uint8_t *data, uint32_t length);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // C_TOXCORE_TOXCORE_BIN_PACK_H

47
toxcore/bin_pack_test.cc Normal file
View File

@ -0,0 +1,47 @@
#include "bin_pack.h"
#include <gtest/gtest.h>
#include <array>
#include <memory>
#include <vector>
#include "bin_unpack.h"
namespace {
struct Bin_Pack_Deleter {
void operator()(Bin_Pack *bp) const { bin_pack_free(bp); }
};
using Bin_Pack_Ptr = std::unique_ptr<Bin_Pack, Bin_Pack_Deleter>;
struct Bin_Unpack_Deleter {
void operator()(Bin_Unpack *bu) const { bin_unpack_free(bu); }
};
using Bin_Unpack_Ptr = std::unique_ptr<Bin_Unpack, Bin_Unpack_Deleter>;
TEST(BinPack, TooSmallBufferIsNotExceeded)
{
std::array<uint8_t, 7> buf;
Bin_Pack_Ptr bp(bin_pack_new(buf.data(), buf.size()));
ASSERT_NE(bp, nullptr);
EXPECT_FALSE(bin_pack_u64_b(bp.get(), 1234567812345678LL));
}
TEST(BinPack, PackedUint64CanBeUnpacked)
{
std::array<uint8_t, 8> buf;
Bin_Pack_Ptr bp(bin_pack_new(buf.data(), buf.size()));
ASSERT_NE(bp, nullptr);
ASSERT_TRUE(bin_pack_u64_b(bp.get(), 1234567812345678LL));
Bin_Unpack_Ptr bu(bin_unpack_new(buf.data(), buf.size()));
ASSERT_NE(bu, nullptr);
uint64_t val;
ASSERT_TRUE(bin_unpack_u64_b(bu.get(), &val));
EXPECT_EQ(val, 1234567812345678LL);
}
} // namespace

View File

@ -4,77 +4,181 @@
#include "bin_unpack.h"
#include <msgpack.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../third_party/cmp/cmp.h"
#include "ccompat.h"
bool bin_unpack_bool(bool *val, const msgpack_object *obj)
struct Bin_Unpack {
const uint8_t *bytes;
uint32_t bytes_size;
cmp_ctx_t ctx;
};
non_null()
static bool buf_reader(cmp_ctx_t *ctx, void *data, size_t limit)
{
if (obj->type != MSGPACK_OBJECT_BOOLEAN) {
Bin_Unpack *reader = (Bin_Unpack *)ctx->buf;
assert(reader != nullptr && reader->bytes != nullptr);
if (limit > reader->bytes_size) {
return false;
}
*val = obj->via.boolean;
memcpy(data, reader->bytes, limit);
reader->bytes += limit;
reader->bytes_size -= limit;
return true;
}
bool bin_unpack_u16(uint16_t *val, const msgpack_object *obj)
non_null()
static bool buf_skipper(cmp_ctx_t *ctx, size_t limit)
{
if (obj->type != MSGPACK_OBJECT_POSITIVE_INTEGER || obj->via.u64 > UINT16_MAX) {
Bin_Unpack *reader = (Bin_Unpack *)ctx->buf;
assert(reader != nullptr && reader->bytes != nullptr);
if (limit > reader->bytes_size) {
return false;
}
*val = (uint16_t)obj->via.u64;
reader->bytes += limit;
reader->bytes_size -= limit;
return true;
}
bool bin_unpack_u32(uint32_t *val, const msgpack_object *obj)
non_null()
static size_t null_writer(cmp_ctx_t *ctx, const void *data, size_t count)
{
if (obj->type != MSGPACK_OBJECT_POSITIVE_INTEGER || obj->via.u64 > UINT32_MAX) {
return false;
}
*val = (uint32_t)obj->via.u64;
return true;
assert(count == 0);
return 0;
}
bool bin_unpack_u64(uint64_t *val, const msgpack_object *obj)
Bin_Unpack *bin_unpack_new(const uint8_t *buf, uint32_t buf_size)
{
if (obj->type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
return false;
Bin_Unpack *bu = (Bin_Unpack *)calloc(1, sizeof(Bin_Unpack));
if (bu == nullptr) {
return nullptr;
}
*val = obj->via.u64;
return true;
bu->bytes = buf;
bu->bytes_size = buf_size;
cmp_init(&bu->ctx, bu, buf_reader, buf_skipper, null_writer);
return bu;
}
bool bin_unpack_bytes(uint8_t **data_ptr, size_t *data_length_ptr, const msgpack_object *obj)
void bin_unpack_free(Bin_Unpack *bu)
{
if (obj->type != MSGPACK_OBJECT_BIN) {
free(bu);
}
bool bin_unpack_array(Bin_Unpack *bu, uint32_t *size)
{
return cmp_read_array(&bu->ctx, size);
}
bool bin_unpack_array_fixed(Bin_Unpack *bu, uint32_t required_size)
{
uint32_t size;
return cmp_read_array(&bu->ctx, &size) && size == required_size;
}
bool bin_unpack_bool(Bin_Unpack *bu, bool *val)
{
return cmp_read_bool(&bu->ctx, val);
}
bool bin_unpack_u08(Bin_Unpack *bu, uint8_t *val)
{
return cmp_read_uchar(&bu->ctx, val);
}
bool bin_unpack_u16(Bin_Unpack *bu, uint16_t *val)
{
return cmp_read_ushort(&bu->ctx, val);
}
bool bin_unpack_u32(Bin_Unpack *bu, uint32_t *val)
{
return cmp_read_uint(&bu->ctx, val);
}
bool bin_unpack_u64(Bin_Unpack *bu, uint64_t *val)
{
return cmp_read_ulong(&bu->ctx, val);
}
bool bin_unpack_bytes(Bin_Unpack *bu, uint8_t **data_ptr, uint32_t *data_length_ptr)
{
uint32_t bin_size;
if (!cmp_read_bin_size(&bu->ctx, &bin_size) || bin_size > bu->bytes_size) {
return false;
}
uint8_t *const data = (uint8_t *)malloc(bin_size);
const uint32_t data_length = obj->via.bin.size;
uint8_t *const data = (uint8_t *)malloc(data_length);
if (data == nullptr) {
if (!bu->ctx.read(&bu->ctx, data, bin_size)) {
free(data);
return false;
}
memcpy(data, obj->via.bin.ptr, data_length);
*data_ptr = data;
*data_length_ptr = data_length;
*data_length_ptr = bin_size;
return true;
}
bool bin_unpack_bytes_fixed(uint8_t *data, uint32_t data_length, const msgpack_object *obj)
bool bin_unpack_bytes_fixed(Bin_Unpack *bu, uint8_t *data, uint32_t data_length)
{
if (obj->type != MSGPACK_OBJECT_BIN || obj->via.bin.size != data_length) {
uint32_t bin_size;
if (!cmp_read_bin_size(&bu->ctx, &bin_size) || bin_size != data_length) {
return false;
}
memcpy(data, obj->via.bin.ptr, data_length);
return bu->ctx.read(&bu->ctx, data, bin_size);
}
bool bin_unpack_bin(Bin_Unpack *bu, uint32_t *size)
{
return cmp_read_bin_size(&bu->ctx, size);
}
bool bin_unpack_u08_b(Bin_Unpack *bu, uint8_t *val)
{
return bu->ctx.read(&bu->ctx, val, 1);
}
bool bin_unpack_u16_b(Bin_Unpack *bu, uint16_t *val)
{
uint8_t hi = 0;
uint8_t lo = 0;
if (!(bin_unpack_u08_b(bu, &hi)
&& bin_unpack_u08_b(bu, &lo))) {
return false;
}
*val = ((uint16_t)hi << 8) | lo;
return true;
}
bool bin_unpack_u32_b(Bin_Unpack *bu, uint32_t *val)
{
uint16_t hi = 0;
uint16_t lo = 0;
if (!(bin_unpack_u16_b(bu, &hi)
&& bin_unpack_u16_b(bu, &lo))) {
return false;
}
*val = ((uint32_t)hi << 16) | lo;
return true;
}
bool bin_unpack_u64_b(Bin_Unpack *bu, uint64_t *val)
{
uint32_t hi = 0;
uint32_t lo = 0;
if (!(bin_unpack_u32_b(bu, &hi)
&& bin_unpack_u32_b(bu, &lo))) {
return false;
}
*val = ((uint64_t)hi << 32) | lo;
return true;
}
bool bin_unpack_bytes_b(Bin_Unpack *bu, uint8_t *data, uint32_t length)
{
return bu->ctx.read(&bu->ctx, data, length);
}

View File

@ -5,16 +5,96 @@
#ifndef C_TOXCORE_TOXCORE_BIN_UNPACK_H
#define C_TOXCORE_TOXCORE_BIN_UNPACK_H
#include <msgpack.h>
#include <stdbool.h>
#include <stdint.h>
#include "attributes.h"
non_null() bool bin_unpack_bool(bool *val, const msgpack_object *obj);
non_null() bool bin_unpack_u16(uint16_t *val, const msgpack_object *obj);
non_null() bool bin_unpack_u32(uint32_t *val, const msgpack_object *obj);
non_null() bool bin_unpack_u64(uint64_t *val, const msgpack_object *obj);
non_null() bool bin_unpack_bytes(uint8_t **data_ptr, size_t *data_length_ptr, const msgpack_object *obj);
non_null() bool bin_unpack_bytes_fixed(uint8_t *data, uint32_t data_length, const msgpack_object *obj);
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Binary deserialisation object.
*/
typedef struct Bin_Unpack Bin_Unpack;
/** @brief Allocate a new unpacker object.
*
* @param buf The byte array to unpack values from.
* @param buf_size The size of the byte array.
*
* @retval nullptr on allocation failure.
*/
non_null()
Bin_Unpack *bin_unpack_new(const uint8_t *buf, uint32_t buf_size);
/** @brief Deallocates an unpacker object.
*
* Does not deallocate the buffer inside.
*/
nullable(1)
void bin_unpack_free(Bin_Unpack *bu);
/** @brief Start unpacking a MessagePack array.
*
* A call to this function must be followed by exactly `size` calls to other functions below.
*
* @param size Will contain the number of array elements following the array marker.
*/
non_null() bool bin_unpack_array(Bin_Unpack *bu, uint32_t *size);
/** @brief Start unpacking a fixed size MessagePack array.
*
* @retval false if the packed array size is not exactly the required size.
*/
non_null() bool bin_unpack_array_fixed(Bin_Unpack *bu, uint32_t required_size);
/** @brief Unpack a MessagePack bool. */
non_null() bool bin_unpack_bool(Bin_Unpack *bu, bool *val);
/** @brief Unpack a MessagePack positive int into a `uint8_t`. */
non_null() bool bin_unpack_u08(Bin_Unpack *bu, uint8_t *val);
/** @brief Unpack a MessagePack positive int into a `uint16_t`. */
non_null() bool bin_unpack_u16(Bin_Unpack *bu, uint16_t *val);
/** @brief Unpack a MessagePack positive int into a `uint32_t`. */
non_null() bool bin_unpack_u32(Bin_Unpack *bu, uint32_t *val);
/** @brief Unpack a MessagePack positive int into a `uint64_t`. */
non_null() bool bin_unpack_u64(Bin_Unpack *bu, uint64_t *val);
/** @brief Unpack a MessagePack bin into a newly allocated byte array.
*
* Allocates a new byte array and stores it into `data_ptr` with its length stored in
* `data_length_ptr`. This function requires that the unpacking buffer has at least as many bytes
* remaining to be unpacked as the bin claims to need, so it's not possible to cause an arbitrarily
* large allocation unless the input array was already that large.
*/
non_null() bool bin_unpack_bytes(Bin_Unpack *bu, uint8_t **data_ptr, uint32_t *data_length_ptr);
/** @brief Unpack a MessagePack bin of a fixed length into a pre-allocated byte array.
*
* Unlike the function above, this function does not allocate any memory, but requires the size to
* be known up front.
*/
non_null() bool bin_unpack_bytes_fixed(Bin_Unpack *bu, uint8_t *data, uint32_t data_length);
/** @brief Start unpacking a custom binary representation.
*
* A call to this function must be followed by exactly `size` bytes packed by functions below.
*/
non_null() bool bin_unpack_bin(Bin_Unpack *bu, uint32_t *size);
/** @brief Read a `uint8_t` directly from the unpacker, consuming 1 byte. */
non_null() bool bin_unpack_u08_b(Bin_Unpack *bu, uint8_t *val);
/** @brief Read a `uint16_t` as big endian 16 bit int, consuming 2 bytes. */
non_null() bool bin_unpack_u16_b(Bin_Unpack *bu, uint16_t *val);
/** @brief Read a `uint32_t` as big endian 32 bit int, consuming 4 bytes. */
non_null() bool bin_unpack_u32_b(Bin_Unpack *bu, uint32_t *val);
/** @brief Read a `uint64_t` as big endian 64 bit int, consuming 8 bytes. */
non_null() bool bin_unpack_u64_b(Bin_Unpack *bu, uint64_t *val);
/** @brief Read a byte array directly from the packer, consuming `length` bytes. */
non_null() bool bin_unpack_bytes_b(Bin_Unpack *bu, uint8_t *data, uint32_t length);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // C_TOXCORE_TOXCORE_BIN_UNPACK_H

View File

@ -54,21 +54,21 @@ uint32_t tox_event_conference_connected_get_conference_number(
}
non_null()
static void tox_event_conference_connected_pack(
const Tox_Event_Conference_Connected *event, msgpack_packer *mp)
static bool tox_event_conference_connected_pack(
const Tox_Event_Conference_Connected *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_CONFERENCE_CONNECTED);
bin_pack_u32(mp, event->conference_number);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_CONFERENCE_CONNECTED)
&& bin_pack_u32(bp, event->conference_number);
}
non_null()
static bool tox_event_conference_connected_unpack(
Tox_Event_Conference_Connected *event, const msgpack_object *obj)
Tox_Event_Conference_Connected *event, Bin_Unpack *bu)
{
assert(event != nullptr);
return bin_unpack_u32(&event->conference_number, obj);
return bin_unpack_u32(bu, &event->conference_number);
}
@ -138,16 +138,19 @@ const Tox_Event_Conference_Connected *tox_events_get_conference_connected(const
return &events->conference_connected[index];
}
void tox_events_pack_conference_connected(const Tox_Events *events, msgpack_packer *mp)
bool tox_events_pack_conference_connected(const Tox_Events *events, Bin_Pack *bp)
{
const uint32_t size = tox_events_get_conference_connected_size(events);
for (uint32_t i = 0; i < size; ++i) {
tox_event_conference_connected_pack(tox_events_get_conference_connected(events, i), mp);
if (!tox_event_conference_connected_pack(tox_events_get_conference_connected(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_conference_connected(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_conference_connected(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Conference_Connected *event = tox_events_add_conference_connected(events);
@ -155,7 +158,7 @@ bool tox_events_unpack_conference_connected(Tox_Events *events, const msgpack_ob
return false;
}
return tox_event_conference_connected_unpack(event, obj);
return tox_event_conference_connected_unpack(event, bu);
}
@ -172,6 +175,10 @@ void tox_events_handle_conference_connected(Tox *tox, uint32_t conference_number
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);
if (conference_connected == nullptr) {

View File

@ -27,7 +27,7 @@ struct Tox_Event_Conference_Invite {
uint32_t friend_number;
Tox_Conference_Type type;
uint8_t *cookie;
size_t cookie_length;
uint32_t cookie_length;
};
non_null()
@ -71,7 +71,7 @@ Tox_Conference_Type tox_event_conference_invite_get_type(const Tox_Event_Confere
non_null()
static bool tox_event_conference_invite_set_cookie(Tox_Event_Conference_Invite *conference_invite,
const uint8_t *cookie, size_t cookie_length)
const uint8_t *cookie, uint32_t cookie_length)
{
assert(conference_invite != nullptr);
@ -91,7 +91,7 @@ static bool tox_event_conference_invite_set_cookie(Tox_Event_Conference_Invite *
conference_invite->cookie_length = cookie_length;
return true;
}
size_t tox_event_conference_invite_get_cookie_length(const Tox_Event_Conference_Invite *conference_invite)
uint32_t tox_event_conference_invite_get_cookie_length(const Tox_Event_Conference_Invite *conference_invite)
{
assert(conference_invite != nullptr);
return conference_invite->cookie_length;
@ -103,31 +103,30 @@ const uint8_t *tox_event_conference_invite_get_cookie(const Tox_Event_Conference
}
non_null()
static void tox_event_conference_invite_pack(
const Tox_Event_Conference_Invite *event, msgpack_packer *mp)
static bool tox_event_conference_invite_pack(
const Tox_Event_Conference_Invite *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_CONFERENCE_INVITE);
bin_pack_array(mp, 3);
bin_pack_u32(mp, event->friend_number);
bin_pack_u32(mp, event->type);
bin_pack_bytes(mp, event->cookie, event->cookie_length);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_CONFERENCE_INVITE)
&& bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->type)
&& bin_pack_bytes(bp, event->cookie, event->cookie_length);
}
non_null()
static bool tox_event_conference_invite_unpack(
Tox_Event_Conference_Invite *event, const msgpack_object *obj)
Tox_Event_Conference_Invite *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 3) {
if (!bin_unpack_array_fixed(bu, 3)) {
return false;
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& tox_unpack_conference_type(&event->type, &obj->via.array.ptr[1])
&& bin_unpack_bytes(&event->cookie, &event->cookie_length, &obj->via.array.ptr[2]);
return bin_unpack_u32(bu, &event->friend_number)
&& tox_unpack_conference_type(bu, &event->type)
&& bin_unpack_bytes(bu, &event->cookie, &event->cookie_length);
}
@ -196,16 +195,19 @@ const Tox_Event_Conference_Invite *tox_events_get_conference_invite(const Tox_Ev
return &events->conference_invite[index];
}
void tox_events_pack_conference_invite(const Tox_Events *events, msgpack_packer *mp)
bool tox_events_pack_conference_invite(const Tox_Events *events, Bin_Pack *bp)
{
const uint32_t size = tox_events_get_conference_invite_size(events);
for (uint32_t i = 0; i < size; ++i) {
tox_event_conference_invite_pack(tox_events_get_conference_invite(events, i), mp);
if (!tox_event_conference_invite_pack(tox_events_get_conference_invite(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_conference_invite(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_conference_invite(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Conference_Invite *event = tox_events_add_conference_invite(events);
@ -213,7 +215,7 @@ bool tox_events_unpack_conference_invite(Tox_Events *events, const msgpack_objec
return false;
}
return tox_event_conference_invite_unpack(event, obj);
return tox_event_conference_invite_unpack(event, bu);
}
@ -230,6 +232,10 @@ void tox_events_handle_conference_invite(Tox *tox, uint32_t friend_number, Tox_C
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);
if (conference_invite == nullptr) {

View File

@ -28,7 +28,7 @@ struct Tox_Event_Conference_Message {
uint32_t peer_number;
Tox_Message_Type type;
uint8_t *message;
size_t message_length;
uint32_t message_length;
};
non_null()
@ -85,7 +85,7 @@ Tox_Message_Type tox_event_conference_message_get_type(const Tox_Event_Conferenc
non_null()
static bool tox_event_conference_message_set_message(Tox_Event_Conference_Message *conference_message,
const uint8_t *message, size_t message_length)
const uint8_t *message, uint32_t message_length)
{
assert(conference_message != nullptr);
@ -105,7 +105,7 @@ static bool tox_event_conference_message_set_message(Tox_Event_Conference_Messag
conference_message->message_length = message_length;
return true;
}
size_t tox_event_conference_message_get_message_length(const Tox_Event_Conference_Message *conference_message)
uint32_t tox_event_conference_message_get_message_length(const Tox_Event_Conference_Message *conference_message)
{
assert(conference_message != nullptr);
return conference_message->message_length;
@ -117,33 +117,32 @@ const uint8_t *tox_event_conference_message_get_message(const Tox_Event_Conferen
}
non_null()
static void tox_event_conference_message_pack(
const Tox_Event_Conference_Message *event, msgpack_packer *mp)
static bool tox_event_conference_message_pack(
const Tox_Event_Conference_Message *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_CONFERENCE_MESSAGE);
bin_pack_array(mp, 4);
bin_pack_u32(mp, event->conference_number);
bin_pack_u32(mp, event->peer_number);
bin_pack_u32(mp, event->type);
bin_pack_bytes(mp, event->message, event->message_length);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_CONFERENCE_MESSAGE)
&& bin_pack_array(bp, 4)
&& bin_pack_u32(bp, event->conference_number)
&& bin_pack_u32(bp, event->peer_number)
&& bin_pack_u32(bp, event->type)
&& bin_pack_bytes(bp, event->message, event->message_length);
}
non_null()
static bool tox_event_conference_message_unpack(
Tox_Event_Conference_Message *event, const msgpack_object *obj)
Tox_Event_Conference_Message *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 4) {
if (!bin_unpack_array_fixed(bu, 4)) {
return false;
}
return bin_unpack_u32(&event->conference_number, &obj->via.array.ptr[0])
&& bin_unpack_u32(&event->peer_number, &obj->via.array.ptr[1])
&& tox_unpack_message_type(&event->type, &obj->via.array.ptr[2])
&& bin_unpack_bytes(&event->message, &event->message_length, &obj->via.array.ptr[3]);
return bin_unpack_u32(bu, &event->conference_number)
&& bin_unpack_u32(bu, &event->peer_number)
&& tox_unpack_message_type(bu, &event->type)
&& bin_unpack_bytes(bu, &event->message, &event->message_length);
}
@ -212,16 +211,19 @@ const Tox_Event_Conference_Message *tox_events_get_conference_message(const Tox_
return &events->conference_message[index];
}
void tox_events_pack_conference_message(const Tox_Events *events, msgpack_packer *mp)
bool tox_events_pack_conference_message(const Tox_Events *events, Bin_Pack *bp)
{
const uint32_t size = tox_events_get_conference_message_size(events);
for (uint32_t i = 0; i < size; ++i) {
tox_event_conference_message_pack(tox_events_get_conference_message(events, i), mp);
if (!tox_event_conference_message_pack(tox_events_get_conference_message(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_conference_message(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_conference_message(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Conference_Message *event = tox_events_add_conference_message(events);
@ -229,7 +231,7 @@ bool tox_events_unpack_conference_message(Tox_Events *events, const msgpack_obje
return false;
}
return tox_event_conference_message_unpack(event, obj);
return tox_event_conference_message_unpack(event, bu);
}
@ -246,6 +248,10 @@ void tox_events_handle_conference_message(Tox *tox, uint32_t conference_number,
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);
if (conference_message == nullptr) {

View File

@ -56,21 +56,21 @@ uint32_t tox_event_conference_peer_list_changed_get_conference_number(const Tox_
}
non_null()
static void tox_event_conference_peer_list_changed_pack(
const Tox_Event_Conference_Peer_List_Changed *event, msgpack_packer *mp)
static bool tox_event_conference_peer_list_changed_pack(
const Tox_Event_Conference_Peer_List_Changed *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED);
bin_pack_u32(mp, event->conference_number);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED)
&& bin_pack_u32(bp, event->conference_number);
}
non_null()
static bool tox_event_conference_peer_list_changed_unpack(
Tox_Event_Conference_Peer_List_Changed *event, const msgpack_object *obj)
Tox_Event_Conference_Peer_List_Changed *event, Bin_Unpack *bu)
{
assert(event != nullptr);
return bin_unpack_u32(&event->conference_number, obj);
return bin_unpack_u32(bu, &event->conference_number);
}
@ -143,16 +143,19 @@ const Tox_Event_Conference_Peer_List_Changed *tox_events_get_conference_peer_lis
return &events->conference_peer_list_changed[index];
}
void tox_events_pack_conference_peer_list_changed(const Tox_Events *events, msgpack_packer *mp)
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);
for (uint32_t i = 0; i < size; ++i) {
tox_event_conference_peer_list_changed_pack(tox_events_get_conference_peer_list_changed(events, i), mp);
if (!tox_event_conference_peer_list_changed_pack(tox_events_get_conference_peer_list_changed(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_conference_peer_list_changed(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_conference_peer_list_changed(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Conference_Peer_List_Changed *event = tox_events_add_conference_peer_list_changed(events);
@ -160,7 +163,7 @@ bool tox_events_unpack_conference_peer_list_changed(Tox_Events *events, const ms
return false;
}
return tox_event_conference_peer_list_changed_unpack(event, obj);
return tox_event_conference_peer_list_changed_unpack(event, bu);
}
@ -176,6 +179,10 @@ void tox_events_handle_conference_peer_list_changed(Tox *tox, uint32_t conferenc
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);

View File

@ -26,7 +26,7 @@ struct Tox_Event_Conference_Peer_Name {
uint32_t conference_number;
uint32_t peer_number;
uint8_t *name;
size_t name_length;
uint32_t name_length;
};
non_null()
@ -71,7 +71,7 @@ uint32_t tox_event_conference_peer_name_get_peer_number(const Tox_Event_Conferen
non_null()
static bool tox_event_conference_peer_name_set_name(Tox_Event_Conference_Peer_Name *conference_peer_name,
const uint8_t *name, size_t name_length)
const uint8_t *name, uint32_t name_length)
{
assert(conference_peer_name != nullptr);
@ -91,7 +91,7 @@ static bool tox_event_conference_peer_name_set_name(Tox_Event_Conference_Peer_Na
conference_peer_name->name_length = name_length;
return true;
}
size_t tox_event_conference_peer_name_get_name_length(const Tox_Event_Conference_Peer_Name *conference_peer_name)
uint32_t tox_event_conference_peer_name_get_name_length(const Tox_Event_Conference_Peer_Name *conference_peer_name)
{
assert(conference_peer_name != nullptr);
return conference_peer_name->name_length;
@ -103,31 +103,30 @@ const uint8_t *tox_event_conference_peer_name_get_name(const Tox_Event_Conferenc
}
non_null()
static void tox_event_conference_peer_name_pack(
const Tox_Event_Conference_Peer_Name *event, msgpack_packer *mp)
static bool tox_event_conference_peer_name_pack(
const Tox_Event_Conference_Peer_Name *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_CONFERENCE_PEER_NAME);
bin_pack_array(mp, 3);
bin_pack_u32(mp, event->conference_number);
bin_pack_u32(mp, event->peer_number);
bin_pack_bytes(mp, event->name, event->name_length);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_CONFERENCE_PEER_NAME)
&& bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->conference_number)
&& bin_pack_u32(bp, event->peer_number)
&& bin_pack_bytes(bp, event->name, event->name_length);
}
non_null()
static bool tox_event_conference_peer_name_unpack(
Tox_Event_Conference_Peer_Name *event, const msgpack_object *obj)
Tox_Event_Conference_Peer_Name *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 3) {
if (!bin_unpack_array_fixed(bu, 3)) {
return false;
}
return bin_unpack_u32(&event->conference_number, &obj->via.array.ptr[0])
&& bin_unpack_u32(&event->peer_number, &obj->via.array.ptr[1])
&& bin_unpack_bytes(&event->name, &event->name_length, &obj->via.array.ptr[2]);
return bin_unpack_u32(bu, &event->conference_number)
&& bin_unpack_u32(bu, &event->peer_number)
&& bin_unpack_bytes(bu, &event->name, &event->name_length);
}
@ -197,16 +196,19 @@ const Tox_Event_Conference_Peer_Name *tox_events_get_conference_peer_name(const
return &events->conference_peer_name[index];
}
void tox_events_pack_conference_peer_name(const Tox_Events *events, msgpack_packer *mp)
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);
for (uint32_t i = 0; i < size; ++i) {
tox_event_conference_peer_name_pack(tox_events_get_conference_peer_name(events, i), mp);
if (!tox_event_conference_peer_name_pack(tox_events_get_conference_peer_name(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_conference_peer_name(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_conference_peer_name(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Conference_Peer_Name *event = tox_events_add_conference_peer_name(events);
@ -214,7 +216,7 @@ bool tox_events_unpack_conference_peer_name(Tox_Events *events, const msgpack_ob
return false;
}
return tox_event_conference_peer_name_unpack(event, obj);
return tox_event_conference_peer_name_unpack(event, bu);
}
@ -231,6 +233,10 @@ void tox_events_handle_conference_peer_name(Tox *tox, uint32_t conference_number
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);
if (conference_peer_name == nullptr) {

View File

@ -26,7 +26,7 @@ struct Tox_Event_Conference_Title {
uint32_t conference_number;
uint32_t peer_number;
uint8_t *title;
size_t title_length;
uint32_t title_length;
};
non_null()
@ -70,7 +70,7 @@ 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,
size_t title_length)
uint32_t title_length)
{
assert(conference_title != nullptr);
@ -90,7 +90,7 @@ static bool tox_event_conference_title_set_title(Tox_Event_Conference_Title *con
conference_title->title_length = title_length;
return true;
}
size_t tox_event_conference_title_get_title_length(const Tox_Event_Conference_Title *conference_title)
uint32_t tox_event_conference_title_get_title_length(const Tox_Event_Conference_Title *conference_title)
{
assert(conference_title != nullptr);
return conference_title->title_length;
@ -102,31 +102,30 @@ const uint8_t *tox_event_conference_title_get_title(const Tox_Event_Conference_T
}
non_null()
static void tox_event_conference_title_pack(
const Tox_Event_Conference_Title *event, msgpack_packer *mp)
static bool tox_event_conference_title_pack(
const Tox_Event_Conference_Title *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_CONFERENCE_TITLE);
bin_pack_array(mp, 3);
bin_pack_u32(mp, event->conference_number);
bin_pack_u32(mp, event->peer_number);
bin_pack_bytes(mp, event->title, event->title_length);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_CONFERENCE_TITLE)
&& bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->conference_number)
&& bin_pack_u32(bp, event->peer_number)
&& bin_pack_bytes(bp, event->title, event->title_length);
}
non_null()
static bool tox_event_conference_title_unpack(
Tox_Event_Conference_Title *event, const msgpack_object *obj)
Tox_Event_Conference_Title *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 3) {
if (!bin_unpack_array_fixed(bu, 3)) {
return false;
}
return bin_unpack_u32(&event->conference_number, &obj->via.array.ptr[0])
&& bin_unpack_u32(&event->peer_number, &obj->via.array.ptr[1])
&& bin_unpack_bytes(&event->title, &event->title_length, &obj->via.array.ptr[2]);
return bin_unpack_u32(bu, &event->conference_number)
&& bin_unpack_u32(bu, &event->peer_number)
&& bin_unpack_bytes(bu, &event->title, &event->title_length);
}
@ -195,16 +194,19 @@ const Tox_Event_Conference_Title *tox_events_get_conference_title(const Tox_Even
return &events->conference_title[index];
}
void tox_events_pack_conference_title(const Tox_Events *events, msgpack_packer *mp)
bool tox_events_pack_conference_title(const Tox_Events *events, Bin_Pack *bp)
{
const uint32_t size = tox_events_get_conference_title_size(events);
for (uint32_t i = 0; i < size; ++i) {
tox_event_conference_title_pack(tox_events_get_conference_title(events, i), mp);
if (!tox_event_conference_title_pack(tox_events_get_conference_title(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_conference_title(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_conference_title(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Conference_Title *event = tox_events_add_conference_title(events);
@ -212,7 +214,7 @@ bool tox_events_unpack_conference_title(Tox_Events *events, const msgpack_object
return false;
}
return tox_event_conference_title_unpack(event, obj);
return tox_event_conference_title_unpack(event, bu);
}
@ -229,6 +231,10 @@ void tox_events_handle_conference_title(Tox *tox, uint32_t conference_number, ui
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);
if (conference_title == nullptr) {

View File

@ -5,9 +5,9 @@
#ifndef C_TOXCORE_TOXCORE_TOX_EVENTS_INTERNAL_H
#define C_TOXCORE_TOXCORE_TOX_EVENTS_INTERNAL_H
#include <msgpack.h>
#include "../attributes.h"
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../tox_events.h"
#ifdef __cplusplus
@ -153,7 +153,7 @@ tox_events_clear_cb tox_events_clear_friend_typing;
tox_events_clear_cb tox_events_clear_self_connection_status;
// non_null()
typedef void tox_events_pack_cb(const Tox_Events *events, msgpack_packer *mp);
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;
@ -180,7 +180,7 @@ tox_events_pack_cb tox_events_pack_self_connection_status;
tox_events_pack_cb tox_events_pack;
// non_null()
typedef bool tox_events_unpack_cb(Tox_Events *events, const msgpack_object *obj);
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;

View File

@ -94,33 +94,32 @@ uint16_t tox_event_file_chunk_request_get_length(const Tox_Event_File_Chunk_Requ
}
non_null()
static void tox_event_file_chunk_request_pack(
const Tox_Event_File_Chunk_Request *event, msgpack_packer *mp)
static bool tox_event_file_chunk_request_pack(
const Tox_Event_File_Chunk_Request *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_FILE_CHUNK_REQUEST);
bin_pack_array(mp, 4);
bin_pack_u32(mp, event->friend_number);
bin_pack_u32(mp, event->file_number);
bin_pack_u64(mp, event->position);
bin_pack_u16(mp, event->length);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_FILE_CHUNK_REQUEST)
&& bin_pack_array(bp, 4)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->file_number)
&& bin_pack_u64(bp, event->position)
&& bin_pack_u16(bp, event->length);
}
non_null()
static bool tox_event_file_chunk_request_unpack(
Tox_Event_File_Chunk_Request *event, const msgpack_object *obj)
Tox_Event_File_Chunk_Request *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 4) {
if (!bin_unpack_array_fixed(bu, 4)) {
return false;
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& bin_unpack_u32(&event->file_number, &obj->via.array.ptr[1])
&& bin_unpack_u64(&event->position, &obj->via.array.ptr[2])
&& bin_unpack_u16(&event->length, &obj->via.array.ptr[3]);
return bin_unpack_u32(bu, &event->friend_number)
&& bin_unpack_u32(bu, &event->file_number)
&& bin_unpack_u64(bu, &event->position)
&& bin_unpack_u16(bu, &event->length);
}
@ -189,16 +188,19 @@ const Tox_Event_File_Chunk_Request *tox_events_get_file_chunk_request(const Tox_
return &events->file_chunk_request[index];
}
void tox_events_pack_file_chunk_request(const Tox_Events *events, msgpack_packer *mp)
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);
for (uint32_t i = 0; i < size; ++i) {
tox_event_file_chunk_request_pack(tox_events_get_file_chunk_request(events, i), mp);
if (!tox_event_file_chunk_request_pack(tox_events_get_file_chunk_request(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_file_chunk_request(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_file_chunk_request(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_File_Chunk_Request *event = tox_events_add_file_chunk_request(events);
@ -206,7 +208,7 @@ bool tox_events_unpack_file_chunk_request(Tox_Events *events, const msgpack_obje
return false;
}
return tox_event_file_chunk_request_unpack(event, obj);
return tox_event_file_chunk_request_unpack(event, bu);
}
@ -223,6 +225,10 @@ void tox_events_handle_file_chunk_request(Tox *tox, uint32_t friend_number, uint
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);
if (file_chunk_request == nullptr) {

View File

@ -28,7 +28,7 @@ struct Tox_Event_File_Recv {
uint32_t kind;
uint64_t file_size;
uint8_t *filename;
size_t filename_length;
uint32_t filename_length;
};
non_null()
@ -98,7 +98,7 @@ 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,
size_t filename_length)
uint32_t filename_length)
{
assert(file_recv != nullptr);
@ -118,7 +118,7 @@ static bool tox_event_file_recv_set_filename(Tox_Event_File_Recv *file_recv, con
file_recv->filename_length = filename_length;
return true;
}
size_t tox_event_file_recv_get_filename_length(const Tox_Event_File_Recv *file_recv)
uint32_t tox_event_file_recv_get_filename_length(const Tox_Event_File_Recv *file_recv)
{
assert(file_recv != nullptr);
return file_recv->filename_length;
@ -130,35 +130,34 @@ const uint8_t *tox_event_file_recv_get_filename(const Tox_Event_File_Recv *file_
}
non_null()
static void tox_event_file_recv_pack(
const Tox_Event_File_Recv *event, msgpack_packer *mp)
static bool tox_event_file_recv_pack(
const Tox_Event_File_Recv *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_FILE_RECV);
bin_pack_array(mp, 5);
bin_pack_u32(mp, event->friend_number);
bin_pack_u32(mp, event->file_number);
bin_pack_u32(mp, event->kind);
bin_pack_u64(mp, event->file_size);
bin_pack_bytes(mp, event->filename, event->filename_length);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_FILE_RECV)
&& bin_pack_array(bp, 5)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->file_number)
&& bin_pack_u32(bp, event->kind)
&& bin_pack_u64(bp, event->file_size)
&& bin_pack_bytes(bp, event->filename, event->filename_length);
}
non_null()
static bool tox_event_file_recv_unpack(
Tox_Event_File_Recv *event, const msgpack_object *obj)
Tox_Event_File_Recv *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 5) {
if (!bin_unpack_array_fixed(bu, 5)) {
return false;
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& bin_unpack_u32(&event->file_number, &obj->via.array.ptr[1])
&& bin_unpack_u32(&event->kind, &obj->via.array.ptr[2])
&& bin_unpack_u64(&event->file_size, &obj->via.array.ptr[3])
&& bin_unpack_bytes(&event->filename, &event->filename_length, &obj->via.array.ptr[4]);
return bin_unpack_u32(bu, &event->friend_number)
&& bin_unpack_u32(bu, &event->file_number)
&& bin_unpack_u32(bu, &event->kind)
&& bin_unpack_u64(bu, &event->file_size)
&& bin_unpack_bytes(bu, &event->filename, &event->filename_length);
}
@ -227,16 +226,19 @@ const Tox_Event_File_Recv *tox_events_get_file_recv(const Tox_Events *events, ui
return &events->file_recv[index];
}
void tox_events_pack_file_recv(const Tox_Events *events, msgpack_packer *mp)
bool tox_events_pack_file_recv(const Tox_Events *events, Bin_Pack *bp)
{
const uint32_t size = tox_events_get_file_recv_size(events);
for (uint32_t i = 0; i < size; ++i) {
tox_event_file_recv_pack(tox_events_get_file_recv(events, i), mp);
if (!tox_event_file_recv_pack(tox_events_get_file_recv(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_file_recv(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_file_recv(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_File_Recv *event = tox_events_add_file_recv(events);
@ -244,7 +246,7 @@ bool tox_events_unpack_file_recv(Tox_Events *events, const msgpack_object *obj)
return false;
}
return tox_event_file_recv_unpack(event, obj);
return tox_event_file_recv_unpack(event, bu);
}
@ -261,6 +263,10 @@ void tox_events_handle_file_recv(Tox *tox, uint32_t friend_number, uint32_t file
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);
if (file_recv == nullptr) {

View File

@ -27,7 +27,7 @@ struct Tox_Event_File_Recv_Chunk {
uint32_t file_number;
uint64_t position;
uint8_t *data;
size_t data_length;
uint32_t data_length;
};
non_null()
@ -84,7 +84,7 @@ 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,
size_t data_length)
uint32_t data_length)
{
assert(file_recv_chunk != nullptr);
@ -104,7 +104,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;
}
size_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_length(const Tox_Event_File_Recv_Chunk *file_recv_chunk)
{
assert(file_recv_chunk != nullptr);
return file_recv_chunk->data_length;
@ -116,33 +116,32 @@ const uint8_t *tox_event_file_recv_chunk_get_data(const Tox_Event_File_Recv_Chun
}
non_null()
static void tox_event_file_recv_chunk_pack(
const Tox_Event_File_Recv_Chunk *event, msgpack_packer *mp)
static bool tox_event_file_recv_chunk_pack(
const Tox_Event_File_Recv_Chunk *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_FILE_RECV_CHUNK);
bin_pack_array(mp, 4);
bin_pack_u32(mp, event->friend_number);
bin_pack_u32(mp, event->file_number);
bin_pack_u64(mp, event->position);
bin_pack_bytes(mp, event->data, event->data_length);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_FILE_RECV_CHUNK)
&& bin_pack_array(bp, 4)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->file_number)
&& bin_pack_u64(bp, event->position)
&& bin_pack_bytes(bp, event->data, event->data_length);
}
non_null()
static bool tox_event_file_recv_chunk_unpack(
Tox_Event_File_Recv_Chunk *event, const msgpack_object *obj)
Tox_Event_File_Recv_Chunk *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 4) {
if (!bin_unpack_array_fixed(bu, 4)) {
return false;
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& bin_unpack_u32(&event->file_number, &obj->via.array.ptr[1])
&& bin_unpack_u64(&event->position, &obj->via.array.ptr[2])
&& bin_unpack_bytes(&event->data, &event->data_length, &obj->via.array.ptr[3]);
return bin_unpack_u32(bu, &event->friend_number)
&& bin_unpack_u32(bu, &event->file_number)
&& bin_unpack_u64(bu, &event->position)
&& bin_unpack_bytes(bu, &event->data, &event->data_length);
}
@ -211,16 +210,19 @@ const Tox_Event_File_Recv_Chunk *tox_events_get_file_recv_chunk(const Tox_Events
return &events->file_recv_chunk[index];
}
void tox_events_pack_file_recv_chunk(const Tox_Events *events, msgpack_packer *mp)
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);
for (uint32_t i = 0; i < size; ++i) {
tox_event_file_recv_chunk_pack(tox_events_get_file_recv_chunk(events, i), mp);
if (!tox_event_file_recv_chunk_pack(tox_events_get_file_recv_chunk(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_file_recv_chunk(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_file_recv_chunk(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_File_Recv_Chunk *event = tox_events_add_file_recv_chunk(events);
@ -228,7 +230,7 @@ bool tox_events_unpack_file_recv_chunk(Tox_Events *events, const msgpack_object
return false;
}
return tox_event_file_recv_chunk_unpack(event, obj);
return tox_event_file_recv_chunk_unpack(event, bu);
}
@ -245,6 +247,10 @@ void tox_events_handle_file_recv_chunk(Tox *tox, uint32_t friend_number, uint32_
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);
if (file_recv_chunk == nullptr) {

View File

@ -82,31 +82,30 @@ Tox_File_Control tox_event_file_recv_control_get_control(const Tox_Event_File_Re
}
non_null()
static void tox_event_file_recv_control_pack(
const Tox_Event_File_Recv_Control *event, msgpack_packer *mp)
static bool tox_event_file_recv_control_pack(
const Tox_Event_File_Recv_Control *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_FILE_RECV_CONTROL);
bin_pack_array(mp, 3);
bin_pack_u32(mp, event->friend_number);
bin_pack_u32(mp, event->file_number);
bin_pack_u32(mp, event->control);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_FILE_RECV_CONTROL)
&& bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->file_number)
&& bin_pack_u32(bp, event->control);
}
non_null()
static bool tox_event_file_recv_control_unpack(
Tox_Event_File_Recv_Control *event, const msgpack_object *obj)
Tox_Event_File_Recv_Control *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 3) {
if (!bin_unpack_array_fixed(bu, 3)) {
return false;
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& bin_unpack_u32(&event->file_number, &obj->via.array.ptr[1])
&& tox_unpack_file_control(&event->control, &obj->via.array.ptr[2]);
return bin_unpack_u32(bu, &event->friend_number)
&& bin_unpack_u32(bu, &event->file_number)
&& tox_unpack_file_control(bu, &event->control);
}
@ -175,16 +174,19 @@ const Tox_Event_File_Recv_Control *tox_events_get_file_recv_control(const Tox_Ev
return &events->file_recv_control[index];
}
void tox_events_pack_file_recv_control(const Tox_Events *events, msgpack_packer *mp)
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);
for (uint32_t i = 0; i < size; ++i) {
tox_event_file_recv_control_pack(tox_events_get_file_recv_control(events, i), mp);
if (!tox_event_file_recv_control_pack(tox_events_get_file_recv_control(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_file_recv_control(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_file_recv_control(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_File_Recv_Control *event = tox_events_add_file_recv_control(events);
@ -192,7 +194,7 @@ bool tox_events_unpack_file_recv_control(Tox_Events *events, const msgpack_objec
return false;
}
return tox_event_file_recv_control_unpack(event, obj);
return tox_event_file_recv_control_unpack(event, bu);
}
@ -209,6 +211,10 @@ void tox_events_handle_file_recv_control(Tox *tox, uint32_t friend_number, uint3
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);
if (file_recv_control == nullptr) {

View File

@ -70,29 +70,28 @@ Tox_Connection tox_event_friend_connection_status_get_connection_status(const To
}
non_null()
static void tox_event_friend_connection_status_pack(
const Tox_Event_Friend_Connection_Status *event, msgpack_packer *mp)
static bool tox_event_friend_connection_status_pack(
const Tox_Event_Friend_Connection_Status *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_FRIEND_CONNECTION_STATUS);
bin_pack_array(mp, 2);
bin_pack_u32(mp, event->friend_number);
bin_pack_u32(mp, event->connection_status);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_FRIEND_CONNECTION_STATUS)
&& bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->connection_status);
}
non_null()
static bool tox_event_friend_connection_status_unpack(
Tox_Event_Friend_Connection_Status *event, const msgpack_object *obj)
Tox_Event_Friend_Connection_Status *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) {
if (!bin_unpack_array_fixed(bu, 2)) {
return false;
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& tox_unpack_connection(&event->connection_status, &obj->via.array.ptr[1]);
return bin_unpack_u32(bu, &event->friend_number)
&& tox_unpack_connection(bu, &event->connection_status);
}
@ -163,16 +162,19 @@ const Tox_Event_Friend_Connection_Status *tox_events_get_friend_connection_statu
return &events->friend_connection_status[index];
}
void tox_events_pack_friend_connection_status(const Tox_Events *events, msgpack_packer *mp)
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);
for (uint32_t i = 0; i < size; ++i) {
tox_event_friend_connection_status_pack(tox_events_get_friend_connection_status(events, i), mp);
if (!tox_event_friend_connection_status_pack(tox_events_get_friend_connection_status(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_friend_connection_status(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_friend_connection_status(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Friend_Connection_Status *event = tox_events_add_friend_connection_status(events);
@ -180,7 +182,7 @@ bool tox_events_unpack_friend_connection_status(Tox_Events *events, const msgpac
return false;
}
return tox_event_friend_connection_status_unpack(event, obj);
return tox_event_friend_connection_status_unpack(event, bu);
}
@ -197,6 +199,10 @@ void tox_events_handle_friend_connection_status(Tox *tox, uint32_t friend_number
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);
if (friend_connection_status == nullptr) {

View File

@ -25,7 +25,7 @@
struct Tox_Event_Friend_Lossless_Packet {
uint32_t friend_number;
uint8_t *data;
size_t data_length;
uint32_t data_length;
};
non_null()
@ -57,7 +57,7 @@ uint32_t tox_event_friend_lossless_packet_get_friend_number(const Tox_Event_Frie
non_null()
static bool tox_event_friend_lossless_packet_set_data(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet,
const uint8_t *data, size_t data_length)
const uint8_t *data, uint32_t data_length)
{
assert(friend_lossless_packet != nullptr);
@ -77,7 +77,7 @@ static bool tox_event_friend_lossless_packet_set_data(Tox_Event_Friend_Lossless_
friend_lossless_packet->data_length = data_length;
return true;
}
size_t tox_event_friend_lossless_packet_get_data_length(const Tox_Event_Friend_Lossless_Packet *friend_lossless_packet)
uint32_t tox_event_friend_lossless_packet_get_data_length(const Tox_Event_Friend_Lossless_Packet *friend_lossless_packet)
{
assert(friend_lossless_packet != nullptr);
return friend_lossless_packet->data_length;
@ -89,29 +89,28 @@ const uint8_t *tox_event_friend_lossless_packet_get_data(const Tox_Event_Friend_
}
non_null()
static void tox_event_friend_lossless_packet_pack(
const Tox_Event_Friend_Lossless_Packet *event, msgpack_packer *mp)
static bool tox_event_friend_lossless_packet_pack(
const Tox_Event_Friend_Lossless_Packet *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_FRIEND_LOSSLESS_PACKET);
bin_pack_array(mp, 2);
bin_pack_u32(mp, event->friend_number);
bin_pack_bytes(mp, event->data, event->data_length);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_FRIEND_LOSSLESS_PACKET)
&& bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bytes(bp, event->data, event->data_length);
}
non_null()
static bool tox_event_friend_lossless_packet_unpack(
Tox_Event_Friend_Lossless_Packet *event, const msgpack_object *obj)
Tox_Event_Friend_Lossless_Packet *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) {
if (!bin_unpack_array_fixed(bu, 2)) {
return false;
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& bin_unpack_bytes(&event->data, &event->data_length, &obj->via.array.ptr[1]);
return bin_unpack_u32(bu, &event->friend_number)
&& bin_unpack_bytes(bu, &event->data, &event->data_length);
}
@ -181,16 +180,19 @@ const Tox_Event_Friend_Lossless_Packet *tox_events_get_friend_lossless_packet(co
return &events->friend_lossless_packet[index];
}
void tox_events_pack_friend_lossless_packet(const Tox_Events *events, msgpack_packer *mp)
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);
for (uint32_t i = 0; i < size; ++i) {
tox_event_friend_lossless_packet_pack(tox_events_get_friend_lossless_packet(events, i), mp);
if (!tox_event_friend_lossless_packet_pack(tox_events_get_friend_lossless_packet(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_friend_lossless_packet(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_friend_lossless_packet(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Friend_Lossless_Packet *event = tox_events_add_friend_lossless_packet(events);
@ -198,7 +200,7 @@ bool tox_events_unpack_friend_lossless_packet(Tox_Events *events, const msgpack_
return false;
}
return tox_event_friend_lossless_packet_unpack(event, obj);
return tox_event_friend_lossless_packet_unpack(event, bu);
}
@ -215,6 +217,10 @@ void tox_events_handle_friend_lossless_packet(Tox *tox, uint32_t friend_number,
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);
if (friend_lossless_packet == nullptr) {

View File

@ -25,7 +25,7 @@
struct Tox_Event_Friend_Lossy_Packet {
uint32_t friend_number;
uint8_t *data;
size_t data_length;
uint32_t data_length;
};
non_null()
@ -56,7 +56,7 @@ uint32_t tox_event_friend_lossy_packet_get_friend_number(const Tox_Event_Friend_
non_null()
static bool tox_event_friend_lossy_packet_set_data(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet,
const uint8_t *data, size_t data_length)
const uint8_t *data, uint32_t data_length)
{
assert(friend_lossy_packet != nullptr);
@ -76,7 +76,7 @@ static bool tox_event_friend_lossy_packet_set_data(Tox_Event_Friend_Lossy_Packet
friend_lossy_packet->data_length = data_length;
return true;
}
size_t tox_event_friend_lossy_packet_get_data_length(const Tox_Event_Friend_Lossy_Packet *friend_lossy_packet)
uint32_t tox_event_friend_lossy_packet_get_data_length(const Tox_Event_Friend_Lossy_Packet *friend_lossy_packet)
{
assert(friend_lossy_packet != nullptr);
return friend_lossy_packet->data_length;
@ -88,29 +88,28 @@ const uint8_t *tox_event_friend_lossy_packet_get_data(const Tox_Event_Friend_Los
}
non_null()
static void tox_event_friend_lossy_packet_pack(
const Tox_Event_Friend_Lossy_Packet *event, msgpack_packer *mp)
static bool tox_event_friend_lossy_packet_pack(
const Tox_Event_Friend_Lossy_Packet *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_FRIEND_LOSSY_PACKET);
bin_pack_array(mp, 2);
bin_pack_u32(mp, event->friend_number);
bin_pack_bytes(mp, event->data, event->data_length);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_FRIEND_LOSSY_PACKET)
&& bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bytes(bp, event->data, event->data_length);
}
non_null()
static bool tox_event_friend_lossy_packet_unpack(
Tox_Event_Friend_Lossy_Packet *event, const msgpack_object *obj)
Tox_Event_Friend_Lossy_Packet *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) {
if (!bin_unpack_array_fixed(bu, 2)) {
return false;
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& bin_unpack_bytes(&event->data, &event->data_length, &obj->via.array.ptr[1]);
return bin_unpack_u32(bu, &event->friend_number)
&& bin_unpack_bytes(bu, &event->data, &event->data_length);
}
@ -180,16 +179,19 @@ const Tox_Event_Friend_Lossy_Packet *tox_events_get_friend_lossy_packet(const To
return &events->friend_lossy_packet[index];
}
void tox_events_pack_friend_lossy_packet(const Tox_Events *events, msgpack_packer *mp)
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);
for (uint32_t i = 0; i < size; ++i) {
tox_event_friend_lossy_packet_pack(tox_events_get_friend_lossy_packet(events, i), mp);
if (!tox_event_friend_lossy_packet_pack(tox_events_get_friend_lossy_packet(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_friend_lossy_packet(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_friend_lossy_packet(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Friend_Lossy_Packet *event = tox_events_add_friend_lossy_packet(events);
@ -197,7 +199,7 @@ bool tox_events_unpack_friend_lossy_packet(Tox_Events *events, const msgpack_obj
return false;
}
return tox_event_friend_lossy_packet_unpack(event, obj);
return tox_event_friend_lossy_packet_unpack(event, bu);
}
@ -214,6 +216,10 @@ void tox_events_handle_friend_lossy_packet(Tox *tox, uint32_t friend_number, con
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);
if (friend_lossy_packet == nullptr) {

View File

@ -27,7 +27,7 @@ struct Tox_Event_Friend_Message {
uint32_t friend_number;
Tox_Message_Type type;
uint8_t *message;
size_t message_length;
uint32_t message_length;
};
non_null()
@ -70,7 +70,7 @@ 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,
size_t message_length)
uint32_t message_length)
{
assert(friend_message != nullptr);
@ -90,7 +90,7 @@ static bool tox_event_friend_message_set_message(Tox_Event_Friend_Message *frien
friend_message->message_length = message_length;
return true;
}
size_t tox_event_friend_message_get_message_length(const Tox_Event_Friend_Message *friend_message)
uint32_t tox_event_friend_message_get_message_length(const Tox_Event_Friend_Message *friend_message)
{
assert(friend_message != nullptr);
return friend_message->message_length;
@ -102,31 +102,30 @@ const uint8_t *tox_event_friend_message_get_message(const Tox_Event_Friend_Messa
}
non_null()
static void tox_event_friend_message_pack(
const Tox_Event_Friend_Message *event, msgpack_packer *mp)
static bool tox_event_friend_message_pack(
const Tox_Event_Friend_Message *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_FRIEND_MESSAGE);
bin_pack_array(mp, 3);
bin_pack_u32(mp, event->friend_number);
bin_pack_u32(mp, event->type);
bin_pack_bytes(mp, event->message, event->message_length);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_FRIEND_MESSAGE)
&& bin_pack_array(bp, 3)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->type)
&& bin_pack_bytes(bp, event->message, event->message_length);
}
non_null()
static bool tox_event_friend_message_unpack(
Tox_Event_Friend_Message *event, const msgpack_object *obj)
Tox_Event_Friend_Message *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 3) {
if (!bin_unpack_array_fixed(bu, 3)) {
return false;
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& tox_unpack_message_type(&event->type, &obj->via.array.ptr[1])
&& bin_unpack_bytes(&event->message, &event->message_length, &obj->via.array.ptr[2]);
return bin_unpack_u32(bu, &event->friend_number)
&& tox_unpack_message_type(bu, &event->type)
&& bin_unpack_bytes(bu, &event->message, &event->message_length);
}
@ -195,16 +194,19 @@ const Tox_Event_Friend_Message *tox_events_get_friend_message(const Tox_Events *
return &events->friend_message[index];
}
void tox_events_pack_friend_message(const Tox_Events *events, msgpack_packer *mp)
bool tox_events_pack_friend_message(const Tox_Events *events, Bin_Pack *bp)
{
const uint32_t size = tox_events_get_friend_message_size(events);
for (uint32_t i = 0; i < size; ++i) {
tox_event_friend_message_pack(tox_events_get_friend_message(events, i), mp);
if (!tox_event_friend_message_pack(tox_events_get_friend_message(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_friend_message(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_friend_message(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Friend_Message *event = tox_events_add_friend_message(events);
@ -212,7 +214,7 @@ bool tox_events_unpack_friend_message(Tox_Events *events, const msgpack_object *
return false;
}
return tox_event_friend_message_unpack(event, obj);
return tox_event_friend_message_unpack(event, bu);
}
@ -229,6 +231,10 @@ void tox_events_handle_friend_message(Tox *tox, uint32_t friend_number, Tox_Mess
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);
if (friend_message == nullptr) {

View File

@ -25,7 +25,7 @@
struct Tox_Event_Friend_Name {
uint32_t friend_number;
uint8_t *name;
size_t name_length;
uint32_t name_length;
};
non_null()
@ -56,7 +56,7 @@ 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,
size_t name_length)
uint32_t name_length)
{
assert(friend_name != nullptr);
@ -76,7 +76,7 @@ static bool tox_event_friend_name_set_name(Tox_Event_Friend_Name *friend_name, c
friend_name->name_length = name_length;
return true;
}
size_t tox_event_friend_name_get_name_length(const Tox_Event_Friend_Name *friend_name)
uint32_t tox_event_friend_name_get_name_length(const Tox_Event_Friend_Name *friend_name)
{
assert(friend_name != nullptr);
return friend_name->name_length;
@ -88,29 +88,28 @@ const uint8_t *tox_event_friend_name_get_name(const Tox_Event_Friend_Name *frien
}
non_null()
static void tox_event_friend_name_pack(
const Tox_Event_Friend_Name *event, msgpack_packer *mp)
static bool tox_event_friend_name_pack(
const Tox_Event_Friend_Name *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_FRIEND_NAME);
bin_pack_array(mp, 2);
bin_pack_u32(mp, event->friend_number);
bin_pack_bytes(mp, event->name, event->name_length);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_FRIEND_NAME)
&& bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bytes(bp, event->name, event->name_length);
}
non_null()
static bool tox_event_friend_name_unpack(
Tox_Event_Friend_Name *event, const msgpack_object *obj)
Tox_Event_Friend_Name *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) {
if (!bin_unpack_array_fixed(bu, 2)) {
return false;
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& bin_unpack_bytes(&event->name, &event->name_length, &obj->via.array.ptr[1]);
return bin_unpack_u32(bu, &event->friend_number)
&& bin_unpack_bytes(bu, &event->name, &event->name_length);
}
@ -179,16 +178,19 @@ const Tox_Event_Friend_Name *tox_events_get_friend_name(const Tox_Events *events
return &events->friend_name[index];
}
void tox_events_pack_friend_name(const Tox_Events *events, msgpack_packer *mp)
bool tox_events_pack_friend_name(const Tox_Events *events, Bin_Pack *bp)
{
const uint32_t size = tox_events_get_friend_name_size(events);
for (uint32_t i = 0; i < size; ++i) {
tox_event_friend_name_pack(tox_events_get_friend_name(events, i), mp);
if (!tox_event_friend_name_pack(tox_events_get_friend_name(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_friend_name(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_friend_name(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Friend_Name *event = tox_events_add_friend_name(events);
@ -196,7 +198,7 @@ bool tox_events_unpack_friend_name(Tox_Events *events, const msgpack_object *obj
return false;
}
return tox_event_friend_name_unpack(event, obj);
return tox_event_friend_name_unpack(event, bu);
}
@ -213,6 +215,10 @@ void tox_events_handle_friend_name(Tox *tox, uint32_t friend_number, const uint8
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);
if (friend_name == nullptr) {

View File

@ -67,29 +67,28 @@ uint32_t tox_event_friend_read_receipt_get_message_id(const Tox_Event_Friend_Rea
}
non_null()
static void tox_event_friend_read_receipt_pack(
const Tox_Event_Friend_Read_Receipt *event, msgpack_packer *mp)
static bool tox_event_friend_read_receipt_pack(
const Tox_Event_Friend_Read_Receipt *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_FRIEND_READ_RECEIPT);
bin_pack_array(mp, 2);
bin_pack_u32(mp, event->friend_number);
bin_pack_u32(mp, event->message_id);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_FRIEND_READ_RECEIPT)
&& bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->message_id);
}
non_null()
static bool tox_event_friend_read_receipt_unpack(
Tox_Event_Friend_Read_Receipt *event, const msgpack_object *obj)
Tox_Event_Friend_Read_Receipt *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) {
if (!bin_unpack_array_fixed(bu, 2)) {
return false;
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& bin_unpack_u32(&event->message_id, &obj->via.array.ptr[1]);
return bin_unpack_u32(bu, &event->friend_number)
&& bin_unpack_u32(bu, &event->message_id);
}
@ -159,16 +158,19 @@ const Tox_Event_Friend_Read_Receipt *tox_events_get_friend_read_receipt(const To
return &events->friend_read_receipt[index];
}
void tox_events_pack_friend_read_receipt(const Tox_Events *events, msgpack_packer *mp)
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);
for (uint32_t i = 0; i < size; ++i) {
tox_event_friend_read_receipt_pack(tox_events_get_friend_read_receipt(events, i), mp);
if (!tox_event_friend_read_receipt_pack(tox_events_get_friend_read_receipt(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_friend_read_receipt(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_friend_read_receipt(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Friend_Read_Receipt *event = tox_events_add_friend_read_receipt(events);
@ -176,7 +178,7 @@ bool tox_events_unpack_friend_read_receipt(Tox_Events *events, const msgpack_obj
return false;
}
return tox_event_friend_read_receipt_unpack(event, obj);
return tox_event_friend_read_receipt_unpack(event, bu);
}
@ -192,6 +194,10 @@ void tox_events_handle_friend_read_receipt(Tox *tox, uint32_t friend_number, uin
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);
if (friend_read_receipt == nullptr) {

View File

@ -25,7 +25,7 @@
struct Tox_Event_Friend_Request {
uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
uint8_t *message;
size_t message_length;
uint32_t message_length;
};
non_null()
@ -57,7 +57,7 @@ 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,
size_t message_length)
uint32_t message_length)
{
assert(friend_request != nullptr);
@ -77,7 +77,7 @@ static bool tox_event_friend_request_set_message(Tox_Event_Friend_Request *frien
friend_request->message_length = message_length;
return true;
}
size_t tox_event_friend_request_get_message_length(const Tox_Event_Friend_Request *friend_request)
uint32_t tox_event_friend_request_get_message_length(const Tox_Event_Friend_Request *friend_request)
{
assert(friend_request != nullptr);
return friend_request->message_length;
@ -89,29 +89,28 @@ const uint8_t *tox_event_friend_request_get_message(const Tox_Event_Friend_Reque
}
non_null()
static void tox_event_friend_request_pack(
const Tox_Event_Friend_Request *event, msgpack_packer *mp)
static bool tox_event_friend_request_pack(
const Tox_Event_Friend_Request *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_FRIEND_REQUEST);
bin_pack_array(mp, 2);
bin_pack_bytes(mp, event->public_key, TOX_PUBLIC_KEY_SIZE);
bin_pack_bytes(mp, event->message, event->message_length);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_FRIEND_REQUEST)
&& bin_pack_array(bp, 2)
&& bin_pack_bytes(bp, event->public_key, TOX_PUBLIC_KEY_SIZE)
&& bin_pack_bytes(bp, event->message, event->message_length);
}
non_null()
static bool tox_event_friend_request_unpack(
Tox_Event_Friend_Request *event, const msgpack_object *obj)
Tox_Event_Friend_Request *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) {
if (!bin_unpack_array_fixed(bu, 2)) {
return false;
}
return bin_unpack_bytes_fixed(event->public_key, TOX_PUBLIC_KEY_SIZE, &obj->via.array.ptr[0])
&& bin_unpack_bytes(&event->message, &event->message_length, &obj->via.array.ptr[1]);
return bin_unpack_bytes_fixed(bu, event->public_key, TOX_PUBLIC_KEY_SIZE)
&& bin_unpack_bytes(bu, &event->message, &event->message_length);
}
@ -180,16 +179,19 @@ const Tox_Event_Friend_Request *tox_events_get_friend_request(const Tox_Events *
return &events->friend_request[index];
}
void tox_events_pack_friend_request(const Tox_Events *events, msgpack_packer *mp)
bool tox_events_pack_friend_request(const Tox_Events *events, Bin_Pack *bp)
{
const uint32_t size = tox_events_get_friend_request_size(events);
for (uint32_t i = 0; i < size; ++i) {
tox_event_friend_request_pack(tox_events_get_friend_request(events, i), mp);
if (!tox_event_friend_request_pack(tox_events_get_friend_request(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_friend_request(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_friend_request(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Friend_Request *event = tox_events_add_friend_request(events);
@ -197,7 +199,7 @@ bool tox_events_unpack_friend_request(Tox_Events *events, const msgpack_object *
return false;
}
return tox_event_friend_request_unpack(event, obj);
return tox_event_friend_request_unpack(event, bu);
}
@ -214,6 +216,10 @@ void tox_events_handle_friend_request(Tox *tox, const uint8_t *public_key, const
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);
if (friend_request == nullptr) {

View File

@ -68,29 +68,28 @@ Tox_User_Status tox_event_friend_status_get_status(const Tox_Event_Friend_Status
}
non_null()
static void tox_event_friend_status_pack(
const Tox_Event_Friend_Status *event, msgpack_packer *mp)
static bool tox_event_friend_status_pack(
const Tox_Event_Friend_Status *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_FRIEND_STATUS);
bin_pack_array(mp, 2);
bin_pack_u32(mp, event->friend_number);
bin_pack_u32(mp, event->status);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_FRIEND_STATUS)
&& bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_u32(bp, event->status);
}
non_null()
static bool tox_event_friend_status_unpack(
Tox_Event_Friend_Status *event, const msgpack_object *obj)
Tox_Event_Friend_Status *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) {
if (!bin_unpack_array_fixed(bu, 2)) {
return false;
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& tox_unpack_user_status(&event->status, &obj->via.array.ptr[1]);
return bin_unpack_u32(bu, &event->friend_number)
&& tox_unpack_user_status(bu, &event->status);
}
@ -159,16 +158,19 @@ const Tox_Event_Friend_Status *tox_events_get_friend_status(const Tox_Events *ev
return &events->friend_status[index];
}
void tox_events_pack_friend_status(const Tox_Events *events, msgpack_packer *mp)
bool tox_events_pack_friend_status(const Tox_Events *events, Bin_Pack *bp)
{
const uint32_t size = tox_events_get_friend_status_size(events);
for (uint32_t i = 0; i < size; ++i) {
tox_event_friend_status_pack(tox_events_get_friend_status(events, i), mp);
if (!tox_event_friend_status_pack(tox_events_get_friend_status(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_friend_status(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_friend_status(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Friend_Status *event = tox_events_add_friend_status(events);
@ -176,7 +178,7 @@ bool tox_events_unpack_friend_status(Tox_Events *events, const msgpack_object *o
return false;
}
return tox_event_friend_status_unpack(event, obj);
return tox_event_friend_status_unpack(event, bu);
}
@ -193,6 +195,10 @@ void tox_events_handle_friend_status(Tox *tox, uint32_t friend_number, Tox_User_
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);
if (friend_status == nullptr) {

View File

@ -25,7 +25,7 @@
struct Tox_Event_Friend_Status_Message {
uint32_t friend_number;
uint8_t *message;
size_t message_length;
uint32_t message_length;
};
non_null()
@ -56,7 +56,7 @@ uint32_t tox_event_friend_status_message_get_friend_number(const Tox_Event_Frien
non_null()
static bool tox_event_friend_status_message_set_message(Tox_Event_Friend_Status_Message *friend_status_message,
const uint8_t *message, size_t message_length)
const uint8_t *message, uint32_t message_length)
{
assert(friend_status_message != nullptr);
@ -76,7 +76,7 @@ static bool tox_event_friend_status_message_set_message(Tox_Event_Friend_Status_
friend_status_message->message_length = message_length;
return true;
}
size_t tox_event_friend_status_message_get_message_length(const Tox_Event_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);
@ -90,29 +90,28 @@ const uint8_t *tox_event_friend_status_message_get_message(const Tox_Event_Frien
}
non_null()
static void tox_event_friend_status_message_pack(
const Tox_Event_Friend_Status_Message *event, msgpack_packer *mp)
static bool tox_event_friend_status_message_pack(
const Tox_Event_Friend_Status_Message *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_FRIEND_STATUS_MESSAGE);
bin_pack_array(mp, 2);
bin_pack_u32(mp, event->friend_number);
bin_pack_bytes(mp, event->message, event->message_length);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_FRIEND_STATUS_MESSAGE)
&& bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bytes(bp, event->message, event->message_length);
}
non_null()
static bool tox_event_friend_status_message_unpack(
Tox_Event_Friend_Status_Message *event, const msgpack_object *obj)
Tox_Event_Friend_Status_Message *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) {
if (!bin_unpack_array_fixed(bu, 2)) {
return false;
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& bin_unpack_bytes(&event->message, &event->message_length, &obj->via.array.ptr[1]);
return bin_unpack_u32(bu, &event->friend_number)
&& bin_unpack_bytes(bu, &event->message, &event->message_length);
}
@ -182,16 +181,19 @@ const Tox_Event_Friend_Status_Message *tox_events_get_friend_status_message(cons
return &events->friend_status_message[index];
}
void tox_events_pack_friend_status_message(const Tox_Events *events, msgpack_packer *mp)
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);
for (uint32_t i = 0; i < size; ++i) {
tox_event_friend_status_message_pack(tox_events_get_friend_status_message(events, i), mp);
if (!tox_event_friend_status_message_pack(tox_events_get_friend_status_message(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_friend_status_message(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_friend_status_message(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Friend_Status_Message *event = tox_events_add_friend_status_message(events);
@ -199,7 +201,7 @@ bool tox_events_unpack_friend_status_message(Tox_Events *events, const msgpack_o
return false;
}
return tox_event_friend_status_message_unpack(event, obj);
return tox_event_friend_status_message_unpack(event, bu);
}
@ -216,6 +218,10 @@ void tox_events_handle_friend_status_message(Tox *tox, uint32_t friend_number, c
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);
if (friend_status_message == nullptr) {

View File

@ -66,29 +66,28 @@ bool tox_event_friend_typing_get_typing(const Tox_Event_Friend_Typing *friend_ty
}
non_null()
static void tox_event_friend_typing_pack(
const Tox_Event_Friend_Typing *event, msgpack_packer *mp)
static bool tox_event_friend_typing_pack(
const Tox_Event_Friend_Typing *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_FRIEND_TYPING);
bin_pack_array(mp, 2);
bin_pack_u32(mp, event->friend_number);
bin_pack_bool(mp, event->typing);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_FRIEND_TYPING)
&& bin_pack_array(bp, 2)
&& bin_pack_u32(bp, event->friend_number)
&& bin_pack_bool(bp, event->typing);
}
non_null()
static bool tox_event_friend_typing_unpack(
Tox_Event_Friend_Typing *event, const msgpack_object *obj)
Tox_Event_Friend_Typing *event, Bin_Unpack *bu)
{
assert(event != nullptr);
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size < 2) {
if (!bin_unpack_array_fixed(bu, 2)) {
return false;
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& bin_unpack_bool(&event->typing, &obj->via.array.ptr[1]);
return bin_unpack_u32(bu, &event->friend_number)
&& bin_unpack_bool(bu, &event->typing);
}
@ -157,16 +156,19 @@ const Tox_Event_Friend_Typing *tox_events_get_friend_typing(const Tox_Events *ev
return &events->friend_typing[index];
}
void tox_events_pack_friend_typing(const Tox_Events *events, msgpack_packer *mp)
bool tox_events_pack_friend_typing(const Tox_Events *events, Bin_Pack *bp)
{
const uint32_t size = tox_events_get_friend_typing_size(events);
for (uint32_t i = 0; i < size; ++i) {
tox_event_friend_typing_pack(tox_events_get_friend_typing(events, i), mp);
if (!tox_event_friend_typing_pack(tox_events_get_friend_typing(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_friend_typing(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_friend_typing(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Friend_Typing *event = tox_events_add_friend_typing(events);
@ -174,7 +176,7 @@ bool tox_events_unpack_friend_typing(Tox_Events *events, const msgpack_object *o
return false;
}
return tox_event_friend_typing_unpack(event, obj);
return tox_event_friend_typing_unpack(event, bu);
}
@ -190,6 +192,10 @@ void tox_events_handle_friend_typing(Tox *tox, uint32_t friend_number, bool typi
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);
if (friend_typing == nullptr) {

View File

@ -55,21 +55,21 @@ Tox_Connection tox_event_self_connection_status_get_connection_status(const Tox_
}
non_null()
static void tox_event_self_connection_status_pack(
const Tox_Event_Self_Connection_Status *event, msgpack_packer *mp)
static bool tox_event_self_connection_status_pack(
const Tox_Event_Self_Connection_Status *event, Bin_Pack *bp)
{
assert(event != nullptr);
bin_pack_array(mp, 2);
bin_pack_u32(mp, TOX_EVENT_SELF_CONNECTION_STATUS);
bin_pack_u32(mp, event->connection_status);
return bin_pack_array(bp, 2)
&& bin_pack_u32(bp, TOX_EVENT_SELF_CONNECTION_STATUS)
&& bin_pack_u32(bp, event->connection_status);
}
non_null()
static bool tox_event_self_connection_status_unpack(
Tox_Event_Self_Connection_Status *event, const msgpack_object *obj)
Tox_Event_Self_Connection_Status *event, Bin_Unpack *bu)
{
assert(event != nullptr);
return tox_unpack_connection(&event->connection_status, obj);
return tox_unpack_connection(bu, &event->connection_status);
}
@ -139,16 +139,19 @@ const Tox_Event_Self_Connection_Status *tox_events_get_self_connection_status(co
return &events->self_connection_status[index];
}
void tox_events_pack_self_connection_status(const Tox_Events *events, msgpack_packer *mp)
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);
for (uint32_t i = 0; i < size; ++i) {
tox_event_self_connection_status_pack(tox_events_get_self_connection_status(events, i), mp);
if (!tox_event_self_connection_status_pack(tox_events_get_self_connection_status(events, i), bp)) {
return false;
}
}
return true;
}
bool tox_events_unpack_self_connection_status(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack_self_connection_status(Tox_Events *events, Bin_Unpack *bu)
{
Tox_Event_Self_Connection_Status *event = tox_events_add_self_connection_status(events);
@ -156,7 +159,7 @@ bool tox_events_unpack_self_connection_status(Tox_Events *events, const msgpack_
return false;
}
return tox_event_self_connection_status_unpack(event, obj);
return tox_event_self_connection_status_unpack(event, bu);
}
@ -172,6 +175,10 @@ void tox_events_handle_self_connection_status(Tox *tox, Tox_Connection connectio
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);
if (self_connection_status == nullptr) {

View File

@ -4,11 +4,11 @@
#include "tox_events.h"
#include <msgpack.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bin_unpack.h"
#include "ccompat.h"
#include "events/events_alloc.h"
#include "tox.h"
@ -63,7 +63,7 @@ Tox_Events *tox_events_iterate(Tox *tox, bool fail_hard, Tox_Err_Events_Iterate
return state.events;
}
void tox_events_pack(const Tox_Events *events, msgpack_packer *mp)
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)
@ -87,104 +87,110 @@ void tox_events_pack(const Tox_Events *events, msgpack_packer *mp)
+ tox_events_get_friend_typing_size(events)
+ tox_events_get_self_connection_status_size(events);
msgpack_pack_array(mp, count);
tox_events_pack_conference_connected(events, mp);
tox_events_pack_conference_invite(events, mp);
tox_events_pack_conference_message(events, mp);
tox_events_pack_conference_peer_list_changed(events, mp);
tox_events_pack_conference_peer_name(events, mp);
tox_events_pack_conference_title(events, mp);
tox_events_pack_file_chunk_request(events, mp);
tox_events_pack_file_recv_chunk(events, mp);
tox_events_pack_file_recv_control(events, mp);
tox_events_pack_file_recv(events, mp);
tox_events_pack_friend_connection_status(events, mp);
tox_events_pack_friend_lossless_packet(events, mp);
tox_events_pack_friend_lossy_packet(events, mp);
tox_events_pack_friend_message(events, mp);
tox_events_pack_friend_name(events, mp);
tox_events_pack_friend_read_receipt(events, mp);
tox_events_pack_friend_request(events, mp);
tox_events_pack_friend_status_message(events, mp);
tox_events_pack_friend_status(events, mp);
tox_events_pack_friend_typing(events, mp);
tox_events_pack_self_connection_status(events, mp);
return bin_pack_array(bp, count)
&& tox_events_pack_conference_connected(events, bp)
&& tox_events_pack_conference_invite(events, bp)
&& tox_events_pack_conference_message(events, bp)
&& tox_events_pack_conference_peer_list_changed(events, bp)
&& tox_events_pack_conference_peer_name(events, bp)
&& tox_events_pack_conference_title(events, bp)
&& tox_events_pack_file_chunk_request(events, bp)
&& tox_events_pack_file_recv_chunk(events, bp)
&& tox_events_pack_file_recv_control(events, bp)
&& tox_events_pack_file_recv(events, bp)
&& tox_events_pack_friend_connection_status(events, bp)
&& tox_events_pack_friend_lossless_packet(events, bp)
&& tox_events_pack_friend_lossy_packet(events, bp)
&& tox_events_pack_friend_message(events, bp)
&& tox_events_pack_friend_name(events, bp)
&& tox_events_pack_friend_read_receipt(events, bp)
&& tox_events_pack_friend_request(events, bp)
&& tox_events_pack_friend_status_message(events, bp)
&& tox_events_pack_friend_status(events, bp)
&& tox_events_pack_friend_typing(events, bp)
&& tox_events_pack_self_connection_status(events, bp);
}
non_null()
static bool tox_event_unpack(Tox_Events *events, const msgpack_object *obj)
static bool tox_event_unpack(Tox_Events *events, Bin_Unpack *bu)
{
if (obj->type != MSGPACK_OBJECT_ARRAY || obj->via.array.size != 2 ||
obj->via.array.ptr[0].type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
uint32_t size;
if (!bin_unpack_array(bu, &size)) {
return false;
}
const msgpack_object *inner = &obj->via.array.ptr[1];
if (size != 2) {
return false;
}
switch (obj->via.array.ptr[0].via.u64) {
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, inner);
return tox_events_unpack_conference_connected(events, bu);
case TOX_EVENT_CONFERENCE_INVITE:
return tox_events_unpack_conference_invite(events, inner);
return tox_events_unpack_conference_invite(events, bu);
case TOX_EVENT_CONFERENCE_MESSAGE:
return tox_events_unpack_conference_message(events, inner);
return tox_events_unpack_conference_message(events, bu);
case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED:
return tox_events_unpack_conference_peer_list_changed(events, inner);
return tox_events_unpack_conference_peer_list_changed(events, bu);
case TOX_EVENT_CONFERENCE_PEER_NAME:
return tox_events_unpack_conference_peer_name(events, inner);
return tox_events_unpack_conference_peer_name(events, bu);
case TOX_EVENT_CONFERENCE_TITLE:
return tox_events_unpack_conference_title(events, inner);
return tox_events_unpack_conference_title(events, bu);
case TOX_EVENT_FILE_CHUNK_REQUEST:
return tox_events_unpack_file_chunk_request(events, inner);
return tox_events_unpack_file_chunk_request(events, bu);
case TOX_EVENT_FILE_RECV_CHUNK:
return tox_events_unpack_file_recv_chunk(events, inner);
return tox_events_unpack_file_recv_chunk(events, bu);
case TOX_EVENT_FILE_RECV_CONTROL:
return tox_events_unpack_file_recv_control(events, inner);
return tox_events_unpack_file_recv_control(events, bu);
case TOX_EVENT_FILE_RECV:
return tox_events_unpack_file_recv(events, inner);
return tox_events_unpack_file_recv(events, bu);
case TOX_EVENT_FRIEND_CONNECTION_STATUS:
return tox_events_unpack_friend_connection_status(events, inner);
return tox_events_unpack_friend_connection_status(events, bu);
case TOX_EVENT_FRIEND_LOSSLESS_PACKET:
return tox_events_unpack_friend_lossless_packet(events, inner);
return tox_events_unpack_friend_lossless_packet(events, bu);
case TOX_EVENT_FRIEND_LOSSY_PACKET:
return tox_events_unpack_friend_lossy_packet(events, inner);
return tox_events_unpack_friend_lossy_packet(events, bu);
case TOX_EVENT_FRIEND_MESSAGE:
return tox_events_unpack_friend_message(events, inner);
return tox_events_unpack_friend_message(events, bu);
case TOX_EVENT_FRIEND_NAME:
return tox_events_unpack_friend_name(events, inner);
return tox_events_unpack_friend_name(events, bu);
case TOX_EVENT_FRIEND_READ_RECEIPT:
return tox_events_unpack_friend_read_receipt(events, inner);
return tox_events_unpack_friend_read_receipt(events, bu);
case TOX_EVENT_FRIEND_REQUEST:
return tox_events_unpack_friend_request(events, inner);
return tox_events_unpack_friend_request(events, bu);
case TOX_EVENT_FRIEND_STATUS_MESSAGE:
return tox_events_unpack_friend_status_message(events, inner);
return tox_events_unpack_friend_status_message(events, bu);
case TOX_EVENT_FRIEND_STATUS:
return tox_events_unpack_friend_status(events, inner);
return tox_events_unpack_friend_status(events, bu);
case TOX_EVENT_FRIEND_TYPING:
return tox_events_unpack_friend_typing(events, inner);
return tox_events_unpack_friend_typing(events, bu);
case TOX_EVENT_SELF_CONNECTION_STATUS:
return tox_events_unpack_self_connection_status(events, inner);
return tox_events_unpack_self_connection_status(events, bu);
default:
return false;
@ -193,14 +199,15 @@ static bool tox_event_unpack(Tox_Events *events, const msgpack_object *obj)
return true;
}
bool tox_events_unpack(Tox_Events *events, const msgpack_object *obj)
bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu)
{
if (obj->type != MSGPACK_OBJECT_ARRAY) {
uint32_t size;
if (!bin_unpack_array(bu, &size)) {
return false;
}
for (uint32_t i = 0; i < obj->via.array.size; ++i) {
if (!tox_event_unpack(events, &obj->via.array.ptr[i])) {
for (uint32_t i = 0; i < size; ++i) {
if (!tox_event_unpack(events, bu)) {
return false;
}
}
@ -208,58 +215,34 @@ bool tox_events_unpack(Tox_Events *events, const msgpack_object *obj)
return true;
}
non_null()
static int count_bytes(void *data, const char *buf, size_t len)
non_null(1) nullable(2)
static bool tox_events_bin_pack_handler(Bin_Pack *bp, const void *obj)
{
uint32_t *count = (uint32_t *)data;
assert(count != nullptr);
*count += len;
return 0;
return tox_events_pack((const Tox_Events *)obj, bp);
}
uint32_t tox_events_bytes_size(const Tox_Events *events)
{
uint32_t count = 0;
msgpack_packer mp;
msgpack_packer_init(&mp, &count, count_bytes);
tox_events_pack(events, &mp);
return count;
}
non_null()
static int write_bytes(void *data, const char *buf, size_t len)
{
uint8_t **bytes = (uint8_t **)data;
assert(bytes != nullptr && *bytes != nullptr);
memcpy(*bytes, buf, len);
*bytes += len;
return 0;
return bin_pack_obj_size(tox_events_bin_pack_handler, events);
}
void tox_events_get_bytes(const Tox_Events *events, uint8_t *bytes)
{
msgpack_packer mp;
msgpack_packer_init(&mp, &bytes, write_bytes);
tox_events_pack(events, &mp);
bin_pack_obj(tox_events_bin_pack_handler, events, bytes, UINT32_MAX);
}
Tox_Events *tox_events_load(const uint8_t *bytes, uint32_t bytes_size)
{
msgpack_unpacked msg;
msgpack_unpacked_init(&msg);
Bin_Unpack *bu = bin_unpack_new(bytes, bytes_size);
size_t offset = 0;
const msgpack_unpack_return result = msgpack_unpack_next(&msg, (const char *)bytes, bytes_size, &offset);
if (result != MSGPACK_UNPACK_SUCCESS) {
msgpack_unpacked_destroy(&msg);
if (bu == nullptr) {
return nullptr;
}
Tox_Events *events = (Tox_Events *)calloc(1, sizeof(Tox_Events));
if (events == nullptr) {
msgpack_unpacked_destroy(&msg);
bin_unpack_free(bu);
return nullptr;
}
@ -267,59 +250,41 @@ Tox_Events *tox_events_load(const uint8_t *bytes, uint32_t bytes_size)
nullptr
};
if (!tox_events_unpack(events, &msg.data)) {
if (!tox_events_unpack(events, bu)) {
tox_events_free(events);
msgpack_unpacked_destroy(&msg);
bin_unpack_free(bu);
return nullptr;
}
msgpack_unpacked_destroy(&msg);
bin_unpack_free(bu);
return events;
}
non_null(2, 3) nullable(1)
static bool tox_events_to_object(const Tox_Events *events, msgpack_unpacked *msg, msgpack_sbuffer *sbuf)
{
msgpack_sbuffer_init(sbuf);
msgpack_packer mp;
msgpack_packer_init(&mp, sbuf, msgpack_sbuffer_write);
tox_events_pack(events, &mp);
size_t offset = 0;
msgpack_unpacked_init(msg);
const msgpack_unpack_return result = msgpack_unpack_next(msg, sbuf->data, sbuf->size, &offset);
return result == MSGPACK_UNPACK_SUCCESS;
}
void tox_events_print(const Tox_Events *events)
{
msgpack_sbuffer sbuf;
msgpack_unpacked msg;
tox_events_to_object(events, &msg, &sbuf);
msgpack_object_print(stdout, msg.data);
fputc('\n', stdout);
msgpack_unpacked_destroy(&msg);
msgpack_sbuffer_destroy(&sbuf);
}
bool tox_events_equal(const Tox_Events *a, const Tox_Events *b)
{
msgpack_sbuffer sbuf_a = {0};
msgpack_unpacked msg_a = {nullptr};
msgpack_sbuffer sbuf_b = {0};
msgpack_unpacked msg_b = {nullptr};
const uint32_t a_size = tox_events_bytes_size(a);
const uint32_t b_size = tox_events_bytes_size(b);
const bool success = tox_events_to_object(a, &msg_a, &sbuf_a)
&& tox_events_to_object(b, &msg_b, &sbuf_b);
if (a_size != b_size) {
return false;
}
const bool ret = success && msgpack_object_equal(msg_a.data, msg_b.data);
uint8_t *a_bytes = (uint8_t *)malloc(a_size);
uint8_t *b_bytes = (uint8_t *)malloc(b_size);
msgpack_unpacked_destroy(&msg_b);
msgpack_sbuffer_destroy(&sbuf_a);
msgpack_unpacked_destroy(&msg_a);
msgpack_sbuffer_destroy(&sbuf_b);
if (a_bytes == nullptr || b_bytes == nullptr) {
free(b_bytes);
free(a_bytes);
return false;
}
tox_events_get_bytes(a, a_bytes);
tox_events_get_bytes(b, b_bytes);
const bool ret = memcmp(a_bytes, b_bytes, a_size) == 0;
free(b_bytes);
free(a_bytes);
return ret;
}

View File

@ -18,7 +18,7 @@ uint32_t tox_event_conference_connected_get_conference_number(
typedef struct Tox_Event_Conference_Invite Tox_Event_Conference_Invite;
const uint8_t *tox_event_conference_invite_get_cookie(
const Tox_Event_Conference_Invite *conference_invite);
size_t tox_event_conference_invite_get_cookie_length(
uint32_t tox_event_conference_invite_get_cookie_length(
const Tox_Event_Conference_Invite *conference_invite);
Tox_Conference_Type tox_event_conference_invite_get_type(
const Tox_Event_Conference_Invite *conference_invite);
@ -28,7 +28,7 @@ uint32_t tox_event_conference_invite_get_friend_number(
typedef struct Tox_Event_Conference_Message Tox_Event_Conference_Message;
const uint8_t *tox_event_conference_message_get_message(
const Tox_Event_Conference_Message *conference_message);
size_t tox_event_conference_message_get_message_length(
uint32_t tox_event_conference_message_get_message_length(
const Tox_Event_Conference_Message *conference_message);
Tox_Message_Type tox_event_conference_message_get_type(
const Tox_Event_Conference_Message *conference_message);
@ -44,7 +44,7 @@ uint32_t tox_event_conference_peer_list_changed_get_conference_number(
typedef struct Tox_Event_Conference_Peer_Name Tox_Event_Conference_Peer_Name;
const uint8_t *tox_event_conference_peer_name_get_name(
const Tox_Event_Conference_Peer_Name *conference_peer_name);
size_t tox_event_conference_peer_name_get_name_length(
uint32_t tox_event_conference_peer_name_get_name_length(
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);
@ -54,7 +54,7 @@ uint32_t tox_event_conference_peer_name_get_peer_number(
typedef struct Tox_Event_Conference_Title Tox_Event_Conference_Title;
const uint8_t *tox_event_conference_title_get_title(
const Tox_Event_Conference_Title *conference_title);
size_t tox_event_conference_title_get_title_length(
uint32_t tox_event_conference_title_get_title_length(
const Tox_Event_Conference_Title *conference_title);
uint32_t tox_event_conference_title_get_conference_number(
const Tox_Event_Conference_Title *conference_title);
@ -74,7 +74,7 @@ uint64_t tox_event_file_chunk_request_get_position(
typedef struct Tox_Event_File_Recv Tox_Event_File_Recv;
const uint8_t *tox_event_file_recv_get_filename(
const Tox_Event_File_Recv *file_recv);
size_t tox_event_file_recv_get_filename_length(
uint32_t tox_event_file_recv_get_filename_length(
const Tox_Event_File_Recv *file_recv);
uint32_t tox_event_file_recv_get_file_number(
const Tox_Event_File_Recv *file_recv);
@ -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);
size_t tox_event_file_recv_chunk_get_length(
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_file_number(
const Tox_Event_File_Recv_Chunk *file_recv_chunk);
@ -114,7 +114,7 @@ uint32_t tox_event_friend_connection_status_get_friend_number(
typedef struct Tox_Event_Friend_Lossless_Packet Tox_Event_Friend_Lossless_Packet;
const uint8_t *tox_event_friend_lossless_packet_get_data(
const Tox_Event_Friend_Lossless_Packet *friend_lossless_packet);
size_t tox_event_friend_lossless_packet_get_data_length(
uint32_t tox_event_friend_lossless_packet_get_data_length(
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);
@ -122,7 +122,7 @@ uint32_t tox_event_friend_lossless_packet_get_friend_number(
typedef struct Tox_Event_Friend_Lossy_Packet Tox_Event_Friend_Lossy_Packet;
const uint8_t *tox_event_friend_lossy_packet_get_data(
const Tox_Event_Friend_Lossy_Packet *friend_lossy_packet);
size_t tox_event_friend_lossy_packet_get_data_length(
uint32_t tox_event_friend_lossy_packet_get_data_length(
const Tox_Event_Friend_Lossy_Packet *friend_lossy_packet);
uint32_t tox_event_friend_lossy_packet_get_friend_number(
const Tox_Event_Friend_Lossy_Packet *friend_lossy_packet);
@ -132,7 +132,7 @@ uint32_t tox_event_friend_message_get_friend_number(
const Tox_Event_Friend_Message *friend_message);
Tox_Message_Type tox_event_friend_message_get_type(
const Tox_Event_Friend_Message *friend_message);
size_t tox_event_friend_message_get_message_length(
uint32_t tox_event_friend_message_get_message_length(
const Tox_Event_Friend_Message *friend_message);
const uint8_t *tox_event_friend_message_get_message(
const Tox_Event_Friend_Message *friend_message);
@ -140,7 +140,7 @@ const uint8_t *tox_event_friend_message_get_message(
typedef struct Tox_Event_Friend_Name Tox_Event_Friend_Name;
const uint8_t *tox_event_friend_name_get_name(
const Tox_Event_Friend_Name *friend_name);
size_t tox_event_friend_name_get_name_length(
uint32_t tox_event_friend_name_get_name_length(
const Tox_Event_Friend_Name *friend_name);
uint32_t tox_event_friend_name_get_friend_number(
const Tox_Event_Friend_Name *friend_name);
@ -156,7 +156,7 @@ const uint8_t *tox_event_friend_request_get_message(
const Tox_Event_Friend_Request *friend_request);
const uint8_t *tox_event_friend_request_get_public_key(
const Tox_Event_Friend_Request *friend_request);
size_t tox_event_friend_request_get_message_length(
uint32_t tox_event_friend_request_get_message_length(
const Tox_Event_Friend_Request *friend_request);
typedef struct Tox_Event_Friend_Status Tox_Event_Friend_Status;
@ -168,7 +168,7 @@ uint32_t tox_event_friend_status_get_friend_number(
typedef struct Tox_Event_Friend_Status_Message Tox_Event_Friend_Status_Message;
const uint8_t *tox_event_friend_status_message_get_message(
const Tox_Event_Friend_Status_Message *friend_status_message);
size_t tox_event_friend_status_message_get_message_length(
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_friend_number(
const Tox_Event_Friend_Status_Message *friend_status_message);
@ -347,8 +347,6 @@ Tox_Events *tox_events_load(const uint8_t *bytes, uint32_t bytes_size);
bool tox_events_equal(const Tox_Events *a, const Tox_Events *b);
void tox_events_print(const Tox_Events *events);
#ifdef __cplusplus
}
#endif

View File

@ -1,8 +1,19 @@
#include "tox_events.h"
#include <cstdint>
#include <vector>
namespace {
void TestUnpack(const uint8_t *data, size_t size) { tox_events_free(tox_events_load(data, size)); }
void TestUnpack(const uint8_t *data, size_t size)
{
Tox_Events *events = tox_events_load(data, size);
if (events) {
std::vector<uint8_t> packed(tox_events_bytes_size(events));
tox_events_get_bytes(events, packed.data());
}
tox_events_free(events);
}
} // namespace

View File

@ -32,13 +32,31 @@ TEST(ToxEvents, UnpackEmptyArrayCreatesEmptyEvents)
tox_events_free(events);
}
// TODO(iphydf): Enable this test once we've fully moved away from msgpack-c.
#if 0
TEST(ToxEvents, NullEventsPacksToEmptyArray)
{
std::array<uint8_t, 1> bytes;
ASSERT_EQ(tox_events_bytes_size(nullptr), bytes.size());
tox_events_get_bytes(nullptr, bytes.data());
EXPECT_EQ(bytes, (std::array<uint8_t, 1>{0x90}));
}
TEST(ToxEvents, PackedEventsCanBeUnpacked)
{
// [[0, 1]] == Tox_Self_Connection_Status { .connection_status = TOX_CONNECTION_TCP }
std::array<uint8_t, 6> packed{0x91, 0x92, 0xcc, 0x00, 0xcc, 0x01};
Tox_Events *events = tox_events_load(packed.data(), packed.size());
ASSERT_NE(events, nullptr);
std::array<uint8_t, 4> bytes;
ASSERT_EQ(tox_events_bytes_size(events), bytes.size());
tox_events_get_bytes(events, bytes.data());
EXPECT_EQ(bytes, (std::array<uint8_t, 4>{0x91, 0x92, 0x00, 0x01}));
tox_events_free(events);
}
TEST(ToxEvents, DealsWithHugeMsgpackArrays)
{
std::vector<uint8_t> data{0xdd, 0xff, 0xff, 0xff, 0xff};
EXPECT_EQ(tox_events_load(data.data(), data.size()), nullptr);
}
#endif
} // namespace

View File

@ -2,6 +2,8 @@
#include <gtest/gtest.h>
#include <array>
#include "crypto_core.h"
namespace {

View File

@ -4,17 +4,16 @@
#include "tox_unpack.h"
#include <msgpack.h>
#include <stdint.h>
#include "bin_unpack.h"
#include "ccompat.h"
bool tox_unpack_conference_type(Tox_Conference_Type *val, const msgpack_object *obj)
bool tox_unpack_conference_type(Bin_Unpack *bu, Tox_Conference_Type *val)
{
uint32_t u32;
if (!bin_unpack_u32(&u32, obj)) {
if (!bin_unpack_u32(bu, &u32)) {
return false;
}
@ -22,11 +21,11 @@ bool tox_unpack_conference_type(Tox_Conference_Type *val, const msgpack_object *
return true;
}
bool tox_unpack_connection(Tox_Connection *val, const msgpack_object *obj)
bool tox_unpack_connection(Bin_Unpack *bu, Tox_Connection *val)
{
uint32_t u32;
if (!bin_unpack_u32(&u32, obj)) {
if (!bin_unpack_u32(bu, &u32)) {
return false;
}
@ -34,11 +33,11 @@ bool tox_unpack_connection(Tox_Connection *val, const msgpack_object *obj)
return true;
}
bool tox_unpack_file_control(Tox_File_Control *val, const msgpack_object *obj)
bool tox_unpack_file_control(Bin_Unpack *bu, Tox_File_Control *val)
{
uint32_t u32;
if (!bin_unpack_u32(&u32, obj)) {
if (!bin_unpack_u32(bu, &u32)) {
return false;
}
@ -46,11 +45,11 @@ bool tox_unpack_file_control(Tox_File_Control *val, const msgpack_object *obj)
return true;
}
bool tox_unpack_message_type(Tox_Message_Type *val, const msgpack_object *obj)
bool tox_unpack_message_type(Bin_Unpack *bu, Tox_Message_Type *val)
{
uint32_t u32;
if (!bin_unpack_u32(&u32, obj)) {
if (!bin_unpack_u32(bu, &u32)) {
return false;
}
@ -58,11 +57,11 @@ bool tox_unpack_message_type(Tox_Message_Type *val, const msgpack_object *obj)
return true;
}
bool tox_unpack_user_status(Tox_User_Status *val, const msgpack_object *obj)
bool tox_unpack_user_status(Bin_Unpack *bu, Tox_User_Status *val)
{
uint32_t u32;
if (!bin_unpack_u32(&u32, obj)) {
if (!bin_unpack_u32(bu, &u32)) {
return false;
}

View File

@ -5,15 +5,14 @@
#ifndef C_TOXCORE_TOXCORE_TOX_UNPACK_H
#define C_TOXCORE_TOXCORE_TOX_UNPACK_H
#include <msgpack.h>
#include "attributes.h"
#include "bin_unpack.h"
#include "tox.h"
non_null() bool tox_unpack_conference_type(Tox_Conference_Type *val, const msgpack_object *obj);
non_null() bool tox_unpack_connection(Tox_Connection *val, const msgpack_object *obj);
non_null() bool tox_unpack_file_control(Tox_File_Control *val, const msgpack_object *obj);
non_null() bool tox_unpack_message_type(Tox_Message_Type *val, const msgpack_object *obj);
non_null() bool tox_unpack_user_status(Tox_User_Status *val, const msgpack_object *obj);
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);
#endif // C_TOXCORE_TOXCORE_TOX_UNPACK_H