9ed2fa80d fix(toxav): remove extra copy of video frame on encode de30cf3ad docs: Add new file kinds, that should be useful to all clients. d5b5e879d fix(DHT): Correct node skipping logic timed out nodes. 30e71fe97 refactor: Generate event dispatch functions and add tox_events_dispatch. 8fdbb0b50 style: Format parameter lists in event handlers. d00dee12b refactor: Add warning logs when losing chat invites. b144e8db1 feat: Add a way to look up a file number by ID. 849281ea0 feat: Add a way to fetch groups by chat ID. a2c177396 refactor: Harden event system and improve type safety. 8f5caa656 refactor: Add MessagePack string support to bin_pack. 34e8d5ad5 chore: Add GitHub CodeQL workflow and local Docker runner. f7b068010 refactor: Add nullability annotations to event headers. 788abe651 refactor(toxav): Use system allocator for mutexes. 2e4b423eb refactor: Use specific typedefs for public API arrays. 2baf34775 docs(toxav): update idle iteration interval see 679444751876fa3882a717772918ebdc8f083354 2f87ac67b feat: Add Event Loop abstraction (Ev). f8dfc38d8 test: Fix data race in ToxScenario virtual_clock. 38313921e test(TCP): Add regression test for TCP priority queue integrity. f94a50d9a refactor(toxav): Replace mutable_mutex with dynamically allocated mutex. ad054511e refactor: Internalize DHT structs and add debug helpers. 8b467cc96 fix: Prevent potential integer overflow in group chat handshake. 4962bdbb8 test: Improve TCP simulation and add tests 5f0227093 refactor: Allow nullable data in group chat handlers. e97b18ea9 chore: Improve Windows Docker support. b14943bbd refactor: Move Logger out of Messenger into Tox. dd3136250 cleanup: Apply nullability qualifiers to C++ codebase. 1849f70fc refactor: Extract low-level networking code to net and os_network. 8fec75421 refactor: Delete tox_random, align on rng and os_random. a03ae8051 refactor: Delete tox_memory, align on mem and os_memory. 4c88fed2c refactor: Use `std::` prefixes more consistently in C++ code. 72452f2ae test: Add some more tests for onion and shared key cache. d5a51b09a cleanup: Use tox_attributes.h in tox_private.h and install it. b6f5b9fc5 test: Add some benchmarks for various high level things. 8a8d02785 test(support): Introduce threaded Tox runner and simulation barrier d68d1d095 perf(toxav): optimize audio and video intermediate buffers by keeping them around REVERT: c9cdae001 fix(toxav): remove extra copy of video frame on encode git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: 9ed2fa80d582c714d6bdde6a7648220a92cddff8
195 lines
8.8 KiB
C
195 lines
8.8 KiB
C
/* SPDX-License-Identifier: GPL-3.0-or-later
|
|
* Copyright © 2022-2025 The TokTok team.
|
|
*/
|
|
#ifndef C_TOXCORE_TOXCORE_BIN_PACK_H
|
|
#define C_TOXCORE_TOXCORE_BIN_PACK_H
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
|
|
#include "attributes.h"
|
|
#include "logger.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/**
|
|
* @brief Binary serialisation object.
|
|
*
|
|
* Naming convention:
|
|
* - Functions ending in `_b` (or `_b_size`) are NOT MessagePack, i.e. write
|
|
* data in plain big endian binary format.
|
|
* - All other functions encode their input in MessagePack format.
|
|
*
|
|
* Some notes on parameter order:
|
|
*
|
|
* - We pass the `obj` pointer as `this`-like pointer first to the callbacks.
|
|
* - Any extra arguments passed to the callback follow the `obj` (and in case of
|
|
* array packing, the `arr` and `arr_size` parameters).
|
|
* - The packer is passed last.
|
|
*
|
|
* This roughly matches a curried lambda function:
|
|
*
|
|
* @code
|
|
* bin_pack_obj([](const void *obj, const Logger *logger, Bin_Pack *bp) { ... }, obj, logger, buf, buf_size);
|
|
* // Translates roughly to:
|
|
* bin_pack_obj([obj, logger](Bin_Pack *bp) { ... }, buf, buf_size);
|
|
* @endcode
|
|
*/
|
|
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(const void *_Nullable obj, const Logger *_Nullable logger, Bin_Pack *_Nonnull bp);
|
|
|
|
/** @brief Function used to pack an array of objects.
|
|
*
|
|
* This function would typically cast the `void *` to the actual object pointer type and then call
|
|
* more appropriately typed packing functions.
|
|
*
|
|
* @param arr is the object array as void pointer.
|
|
* @param index is the index in the object array that is currently being packed.
|
|
*/
|
|
typedef bool bin_pack_array_cb(const void *_Nullable arr, uint32_t index, const Logger *_Nullable logger, Bin_Pack *_Nonnull bp);
|
|
|
|
/** @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.
|
|
* @param logger Optional logger object to pass to the callback.
|
|
*
|
|
* @return The packed size of the passed object according to the callback.
|
|
* @retval UINT32_MAX in case of errors such as buffer overflow.
|
|
*/
|
|
uint32_t bin_pack_obj_size(bin_pack_cb *_Nonnull callback, const void *_Nullable obj, const Logger *_Nullable logger);
|
|
/** @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. Note that
|
|
* there is nothing MessagePack-specific about this function, so it can be used for both custom
|
|
* binary and MessagePack formats.
|
|
*
|
|
* You can use `bin_pack_obj_size` to determine the minimum required size of `buf`. If packing
|
|
* overflows `uint32_t`, this function returns `false`.
|
|
*
|
|
* Passing NULL for `obj` is supported, but requires that the callback supports nullable inputs.
|
|
*
|
|
* @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 logger Optional logger object to pass 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).
|
|
*/
|
|
bool bin_pack_obj(bin_pack_cb *_Nonnull callback, const void *_Nullable obj, const Logger *_Nullable logger, uint8_t *_Nonnull buf, uint32_t buf_size);
|
|
/** @brief Determine the serialised size of an object array.
|
|
*
|
|
* Behaves exactly like `bin_pack_obj_b_array` but doesn't write.
|
|
*
|
|
* @param callback The function called on the created packer and each object to
|
|
* be packed.
|
|
* @param arr The object array to be packed, passed as `arr` to the callback.
|
|
* @param arr_size The number of elements in the object array.
|
|
* @param logger Optional logger object to pass to the callback.
|
|
*
|
|
* @return The packed size of the passed object array according to the callback.
|
|
* @retval UINT32_MAX in case of errors such as buffer overflow.
|
|
*/
|
|
uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *_Nonnull callback, const void *_Nullable arr, uint32_t arr_size, const Logger *_Nullable logger);
|
|
/** @brief Pack an object array into a buffer of a given size.
|
|
*
|
|
* Similar to `bin_pack_obj_array` but does not write the array length, so
|
|
* if you need that, encoding it is on you.
|
|
*
|
|
* Passing NULL for `arr` has no effect, but requires that `arr_size` is 0.
|
|
*
|
|
* @param callback The function called on the created packer and packed object
|
|
* array.
|
|
* @param arr The object array to be packed, passed as `arr` to the callback.
|
|
* @param arr_size The number of elements in the object array.
|
|
* @param logger Optional logger object to pass to the callback.
|
|
* @param buf A byte array large enough to hold the serialised representation of `arr`.
|
|
* @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).
|
|
*/
|
|
bool bin_pack_obj_array_b(bin_pack_array_cb *_Nonnull callback, const void *_Nullable arr, uint32_t arr_size, const Logger *_Nullable logger, uint8_t *_Nonnull buf, uint32_t buf_size);
|
|
/** @brief Encode an object array as MessagePack array into a bin packer.
|
|
*
|
|
* Calls the callback `arr_size` times with increasing `index` argument from 0 to
|
|
* `arr_size`. This function is here just so we don't need to write the same
|
|
* trivial loop many times and so we don't need an extra struct just to contain
|
|
* an array with size so it can be passed to `bin_pack_obj`.
|
|
*
|
|
* Similar to `bin_pack_obj` but for arrays. Note that a `Bin_Pack` object is
|
|
* required here, so it must be called from within a callback to one of the
|
|
* functions above.
|
|
*
|
|
* Passing NULL for `arr` requires that `arr_size` is 0. This will write a 0-size
|
|
* MessagePack array to the packer.
|
|
*
|
|
* @param bp Bin packer object.
|
|
* @param callback The function called on the created packer and packed object
|
|
* array.
|
|
* @param arr The object array to be packed, passed as `arr` to the callback.
|
|
* @param arr_size The number of elements in the object array.
|
|
* @param logger Optional logger object to pass to the callback.
|
|
*
|
|
* @retval false if an error occurred (e.g. buffer overflow).
|
|
*/
|
|
bool bin_pack_obj_array(Bin_Pack *_Nonnull bp, bin_pack_array_cb *_Nonnull callback, const void *_Nullable arr, uint32_t arr_size, const Logger *_Nullable logger);
|
|
/** @brief Start packing a MessagePack array.
|
|
*
|
|
* A call to this function must be followed by exactly `size` calls to other functions below.
|
|
*/
|
|
bool bin_pack_array(Bin_Pack *_Nonnull bp, uint32_t size);
|
|
|
|
/** @brief Pack a MessagePack bool. */
|
|
bool bin_pack_bool(Bin_Pack *_Nonnull bp, bool val);
|
|
/** @brief Pack a `uint8_t` as MessagePack positive integer. */
|
|
bool bin_pack_u08(Bin_Pack *_Nonnull bp, uint8_t val);
|
|
/** @brief Pack a `uint16_t` as MessagePack positive integer. */
|
|
bool bin_pack_u16(Bin_Pack *_Nonnull bp, uint16_t val);
|
|
/** @brief Pack a `uint32_t` as MessagePack positive integer. */
|
|
bool bin_pack_u32(Bin_Pack *_Nonnull bp, uint32_t val);
|
|
/** @brief Pack a `uint64_t` as MessagePack positive integer. */
|
|
bool bin_pack_u64(Bin_Pack *_Nonnull bp, uint64_t val);
|
|
/** @brief Pack an empty array member as a MessagePack nil value. */
|
|
bool bin_pack_nil(Bin_Pack *_Nonnull bp);
|
|
/** @brief Pack a byte array as MessagePack bin. */
|
|
bool bin_pack_bin(Bin_Pack *_Nonnull bp, const uint8_t *_Nullable data, uint32_t length);
|
|
/** @brief Pack a string as MessagePack str. */
|
|
bool bin_pack_str(Bin_Pack *_Nonnull bp, const char *_Nullable 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.
|
|
*/
|
|
bool bin_pack_bin_marker(Bin_Pack *_Nonnull bp, uint32_t size);
|
|
|
|
/** @brief Write a `uint8_t` directly to the packer in 1 byte. */
|
|
bool bin_pack_u08_b(Bin_Pack *_Nonnull bp, uint8_t val);
|
|
/** @brief Write a `uint16_t` as big endian 16 bit int in 2 bytes. */
|
|
bool bin_pack_u16_b(Bin_Pack *_Nonnull bp, uint16_t val);
|
|
/** @brief Write a `uint32_t` as big endian 32 bit int in 4 bytes. */
|
|
bool bin_pack_u32_b(Bin_Pack *_Nonnull bp, uint32_t val);
|
|
/** @brief Write a `uint64_t` as big endian 64 bit int in 8 bytes. */
|
|
bool bin_pack_u64_b(Bin_Pack *_Nonnull 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.
|
|
*/
|
|
bool bin_pack_bin_b(Bin_Pack *_Nonnull bp, const uint8_t *_Nullable data, uint32_t length);
|
|
|
|
#ifdef __cplusplus
|
|
} /* extern "C" */
|
|
#endif
|
|
|
|
#endif /* C_TOXCORE_TOXCORE_BIN_PACK_H */
|