25#define PACKET_MAX_FRAME_SIZE (256 * 1024 * 1024)
31#define PACKET_MAX_DIMENSION 32768
45 char *frame_data =
SAFE_MALLOC(original_size + 1,
char *);
53 if (frame_data_len != compressed_size) {
65 asciichat_error_string(decompress_result));
70 log_debug(
"Decompressed frame: %zu -> %u bytes", frame_data_len, original_size);
73 if (frame_data_len != original_size) {
74 log_error(
"Uncompressed frame size mismatch: expected %u, got %zu", original_size, frame_data_len);
80 size_t copy_size = (frame_data_len > original_size) ? original_size : frame_data_len;
81 memcpy(frame_data, frame_data_ptr, copy_size);
85 frame_data[original_size] =
'\0';
90 void *output_buffer,
size_t output_size,
uint32_t original_size,
92 if (!frame_data_ptr || !output_buffer) {
96 if (output_size < original_size) {
102 if (frame_data_len != compressed_size) {
112 asciichat_error_string(decompress_result));
115 log_debug(
"Decompressed frame to buffer: %zu -> %u bytes", frame_data_len, original_size);
118 if (frame_data_len != original_size) {
124 memcpy(output_buffer, frame_data_ptr, original_size);
136 if (width == 0 || height == 0) {
147 size_t pixel_count = 0;
165 *out_rgb_size = rgb_size;
174 size_t *out_opus_size,
const uint16_t **out_frame_sizes,
int *out_sample_rate,
175 int *out_frame_duration,
int *out_frame_count) {
176 if (!packet_data || !out_opus_data || !out_opus_size || !out_frame_sizes || !out_sample_rate || !out_frame_duration ||
182 const size_t header_size = 16;
183 if (packet_len < header_size) {
191 memcpy(&fd, buf + 4, 4);
192 memcpy(&fc, buf + 8, 4);
196 *out_frame_count = frame_count;
199 if (frame_count < 0 || frame_count > 1000) {
204 size_t frame_sizes_bytes = (size_t)frame_count *
sizeof(
uint16_t);
205 if (packet_len < header_size + frame_sizes_bytes) {
207 header_size + frame_sizes_bytes);
210 *out_frame_sizes = (
const uint16_t *)(buf + header_size);
213 *out_opus_data = buf + header_size + frame_sizes_bytes;
214 *out_opus_size = packet_len - header_size - frame_sizes_bytes;
🔢 Byte-Level Access and Arithmetic Utilities
📦 Network Packet Compression Utilities
🔄 Network byte order conversion helpers
#define NET_TO_HOST_U32(val)
#define SAFE_MALLOC(size, cast)
asciichat_error_t decompress_data(const void *input, size_t input_size, void *output, size_t output_size)
Decompress data using zstd.
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
asciichat_error_t
Error and exit codes - unified status values (0-255)
#define log_error(...)
Log an ERROR message.
#define log_debug(...)
Log a DEBUG message.
asciichat_error_t packet_parse_opus_batch(const void *packet_data, size_t packet_len, const uint8_t **out_opus_data, size_t *out_opus_size, const uint16_t **out_frame_sizes, int *out_sample_rate, int *out_frame_duration, int *out_frame_count)
Parse Opus audio batch packet header and extract frame data.
#define PACKET_MAX_DIMENSION
Maximum frame dimension (32768x32768) - prevents overflow.
asciichat_error_t packet_decode_frame_data_buffer(const char *frame_data_ptr, size_t frame_data_len, bool is_compressed, void *output_buffer, size_t output_size, uint32_t original_size, uint32_t compressed_size)
Decode frame data (fixed buffer version for server handlers)
asciichat_error_t packet_validate_frame_dimensions(uint32_t width, uint32_t height, size_t *out_rgb_size)
Validate frame dimensions and calculate RGB buffer size.
char * packet_decode_frame_data_malloc(const char *frame_data_ptr, size_t frame_data_len, bool is_compressed, uint32_t original_size, uint32_t compressed_size)
Decode frame data (malloc version for client handlers)
#define PACKET_MAX_FRAME_SIZE
Maximum frame size (256MB) - prevents memory exhaustion attacks.
void format_bytes_pretty(size_t bytes, char *out, size_t out_capacity)
Format byte count into human-readable string.
Shared packet parsing utilities to eliminate duplication between server and client handlers.
Test logging control utilities.