mirror of
https://github.com/lux-tel/libsession-util.git
synced 2026-10-30 20:25:11 +00:00
`string8` (`{char* data; size_t size}`) was a structural duplicate of the
existing `span_u8` (`{uint8_t* data; size_t size}`) -- same ptr+len byte span,
right down to a duplicate pair of alloc/copy_or_throw helpers. Once its uses are
mapped, none actually need a string-with-length:
- SESSION_PROTOCOL_STRINGS (24 fields) and session_protocol_pro_features_for_msg
`error` are static, null-terminated strings -> `const char*`. The `error`
string_view only ever views static literals ("..." + simdutf::error_to_string,
which returns string_view over literals), so there is nothing to free and no
dangling. SESSION_PROTOCOL_STRINGS drops the `string8_literal` macro for plain
string literals.
- session_pro_backend_request `data` is an opaque (possibly binary) payload that
genuinely needs ptr+len -> `span_u8`, the type already used for sig/msg.
Then delete the dead string8_alloc_or_throw/string8_copy_or_throw helpers (no
callers; duplicates of span_u8_*), and the `string8` struct + `string8_literal`
macro. Test helper string8_equals -> span_u8_equals, now a std::string_view
compare instead of a hand-rolled memcmp.
C-API change (clients): session_protocol_strings fields and
session_protocol_pro_features_for_msg.error are now `const char*`;
session_pro_backend_request.data is now `span_u8`.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
60 lines
1.7 KiB
C
60 lines
1.7 KiB
C
#pragma once
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#define OPTIONAL
|
|
#if defined(_MSC_VER)
|
|
#define NON_NULL_ARG(...)
|
|
#else
|
|
#define NON_NULL_ARG(...) __attribute__((nonnull(__VA_ARGS__)))
|
|
#endif
|
|
|
|
/// C friendly buffer structure that is a pointer and length to a span of bytes.
|
|
typedef struct span_u8 span_u8;
|
|
struct span_u8 {
|
|
uint8_t* data;
|
|
size_t size;
|
|
};
|
|
|
|
typedef struct bytes32 bytes32;
|
|
struct bytes32 {
|
|
uint8_t data[32];
|
|
};
|
|
|
|
typedef struct bytes33 bytes33;
|
|
struct bytes33 {
|
|
uint8_t data[33];
|
|
};
|
|
|
|
typedef struct bytes64 bytes64;
|
|
struct bytes64 {
|
|
uint8_t data[64];
|
|
};
|
|
|
|
/// A wrapper around snprintf that fixes a common bug in the value the printing function returns
|
|
/// when a buffer is passed in. Irrespective of whether a buffer is passed in, snprintf is defined
|
|
/// to return:
|
|
///
|
|
/// number of characters (not including the terminating null character) which would have been
|
|
/// written to buffer if bufsz was ignored
|
|
///
|
|
/// This means if the user passes in a buffer to small, the return value is always the amount of
|
|
/// bytes required. This means the user always has to calculate the number of bytes written as:
|
|
///
|
|
/// size_t bytes_written = min(snprintf(buffer, size, ...), size);
|
|
///
|
|
/// This is error prone. This function does the `min(...)` for you so that this function
|
|
/// _always_ calculates the actual number of bytes written (not including the null-terminator). If a
|
|
/// NULL is passed in then this function returns the number of bytes actually needed to write the
|
|
/// entire string (as per normal snprintf behaviour).
|
|
int snprintf_clamped(char* buffer, size_t size, char const* fmt, ...);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|