Reimplementation of Pijul in C, for education, fun and absolutely no profit
#ifndef ANI_BINCODE_H
#define ANI_BINCODE_H

/** Requires: <stdint.h> */

/**
 * Definitions for the bincode encode/decode routines.
 *
 * Generally, the idea here is that you open a file, get a file descriptor and
 * use the encode/decode routines to get components read from the fd and and
 * decoded as the type you want.
 */

/**
 * This approach could simplify signatures a bit (in the sense of changing to a
 * single pointer again). It would also allow adding some bounds checking that
 * we aren't trying to read more data than available in the buffer. Maybe a case
 * for "if (unlikely(s->len - s->count < sizeof(type)))" ?
 */
struct bincode {
	size_t avail; /* bytes still available */
	uint8_t *buf;
};

/**
 * Some notes regarding the bincode format:
 *
 * Enum types: The order of the variants matter, as the encoding indicates the
 * variant with a u32.
 */
uint8_t bincode_getu8(struct bincode *);
uint16_t bincode_getu16(struct bincode *);
uint32_t bincode_getu32(struct bincode *);
uint64_t bincode_getu64(struct bincode *);

/**
 * Fill in a string of the given length, the destination should have
 * sufficient space.
 */
void bincode_getstr(struct bincode *, char *, size_t);
void bincode_getbytes(struct bincode *, uint8_t *, size_t);

/* void bincode_putu16(uint8_t *, uint16_t); */
/* void bincode_putu32(uint8_t *, uint32_t); */
/* void bincode_putu64(uint8_t *, uint64_t); */

#endif