/* Generic interface for binary data parsing. */ #ifndef COM_BIN_H #define COM_BIN_H #include #include "buf.h" #define COM_BIN_LIT_ENDIAN (1 << 0) #define COM_BIN_BIG_ENDIAN (1 << 1) #define COM_BIN_NAT_ENDIAN (1 << 2) typedef struct com_bin_interface { /* Attempts to read exactly N bytes, returns false if it cannot. */ bool (*readn)(void *ctx, char *into, uint32_t bytes); bool (*skipn)(void *ctx, uint32_t bytes); } com_bin_interface; typedef struct com_bin { struct com_bin_interface interface; void *ctx; uint32_t flags; } com_bin; bool com_bin_read_i16(com_bin *b, int16_t *value); bool com_bin_read_u16(com_bin *b, uint16_t *value); /* Read untyped bytes appending them to buffer. NOTE: Buffer must have typesz of 1. */ bool com_bin_read_bufb(com_bin *b, struct com_buf *buf, uint32_t bytes); /* Inline byte buffer read, making a new buffer inside. Resulted index can be safely used in case of future reallocations. NOTE: Buffer must have typesz of 1. */ bool com_bin_read_bufbi(com_bin *b, struct com_buf *buf, uint32_t *index, uint32_t bytes); /* TODO: Typed variant, use buf for typesz? */ /* Skip N bytes. */ bool com_bin_skip(com_bin *b, uint32_t n); #endif