2026-01-14 22:55:33 +00:00

67 lines
2.5 KiB
C

#ifndef PARCER_H
#define PARCER_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include "server_structs.h"
// Ensures that there are at least 'need' bytes available in the buffer
// starting from 'offset'. Returns 1 if enough bytes are available, 0 otherwise.
int ensure(size_t recv_len, size_t offset, size_t need);
// Reads an 8-bit unsigned integer from the buffer.
// Advances the offset by 1 byte.
u_int8_t read_uint8(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
// Reads a 16-bit unsigned integer from the buffer in network byte order.
// Advances the offset by 2 bytes.
u_int16_t read_uint16(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
// Reads a 16-bit unsigned integer from the buffer in little-endian byte order.
// Advances the offset by 2 bytes.
u_int16_t read_uint16_le(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
// Reads a 32-bit unsigned integer from the buffer in network byte order.
// Advances the offset by 4 bytes.
u_int32_t read_uint32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
// Reads a 32-bit signed integer from the buffer in network byte order.
// Advances the offset by 4 bytes.
int32_t read_int32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
// Reads a 32-bit float from the buffer in network byte order.
// Advances the offset by 4 bytes.
float read_float(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
// Reads a fixed number of bytes into the output buffer.
// The output buffer must be at least 'len' bytes long.
void read_bytes(const u_int8_t *buf, size_t recv_len, size_t *offset, u_int8_t *out, size_t len, int *ok);
// Reads a length-prefixed string. The length is a single byte.
// The string is not null-terminated in the buffer, but the function
void read_utf32le_string(const uint8_t *buffer, size_t buf_size, size_t *offset, char *dest, size_t max_len, int *ok);
// reads up to max_len - 1 characters and null-terminates the destination buffer.
// The string is encoded in UTF-32LE.
void read_utf16le_string(const uint8_t *buffer, size_t buf_size, size_t *offset, char *dest, size_t max_len, int *ok);
// Reads a null-terminated string from the buffer.
// The string is read into the output buffer, which must be at least max_len bytes long
void read_string(const u_int8_t *buf, size_t recv_len, size_t *offset, char *out, size_t max_len, int *ok);
#ifdef __cplusplus
}
#endif
#endif // PARCER_H