YAPB
Loading...
Searching...
No Matches
yapb.h File Reference

YAPB - Yet Another Protocol Buffer format. More...

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdalign.h>

Go to the source code of this file.

Data Structures

struct  YAPB_Packet
 Opaque packet handle, stack-allocatable. More...
struct  YAPB_Element
 Tagged union returned by YAPB_pop_next(). More...

Macros

#define YAPB_MODE_WRITE   0
 Packet is in write mode.
#define YAPB_MODE_READ   1
 Packet is in read mode.
#define YAPB_HEADER_SIZE   4
 Size of the packet header in bytes.
#define YAPB_PACKET_SIZE   48
 Size of the opaque YAPB_Packet_t storage in bytes.

Typedefs

typedef struct YAPB_Packet YAPB_Packet_t
 Opaque packet handle, stack-allocatable.
typedef struct YAPB_Element YAPB_Element_t
 Tagged union returned by YAPB_pop_next().

Enumerations

enum  YAPB_Type_t {
  YAPB_INT8 = 0x00 , YAPB_INT16 = 0x01 , YAPB_INT32 = 0x02 , YAPB_INT64 = 0x03 ,
  YAPB_FLOAT = 0x04 , YAPB_DOUBLE = 0x05 , YAPB_BLOB = 0x0E , YAPB_NESTED_PKT = 0x0F
}
 Element type tags stored in the wire format. More...
enum  YAPB_Result_t {
  YAPB_ERR_NO_MORE_ELEMENTS = -7 , YAPB_ERR_INVALID_PACKET = -6 , YAPB_ERR_TYPE_MISMATCH = -5 , YAPB_ERR_INVALID_MODE = -4 ,
  YAPB_ERR_BUFFER_TOO_SMALL = -3 , YAPB_ERR_NULL_PTR = -2 , YAPB_ERR_UNKNOWN = -1 , YAPB_OK = 0 ,
  YAPB_STS_COMPLETE = 1
}
 Result codes returned by all YAPB functions. More...

Functions

YAPB_Result_t YAPB_initialize (YAPB_Packet_t *pkt, uint8_t *buffer, size_t size)
 Initialize a packet for writing.
YAPB_Result_t YAPB_finalize (YAPB_Packet_t *pkt, size_t *out_len)
 Finalize the packet, writing the total length into the header.
YAPB_Result_t YAPB_load (YAPB_Packet_t *pkt, const uint8_t *data, size_t size)
 Load a packet for reading from raw data.
YAPB_Result_t YAPB_push_i8 (YAPB_Packet_t *pkt, const int8_t *val)
 Push a signed 8-bit integer.
YAPB_Result_t YAPB_push_i16 (YAPB_Packet_t *pkt, const int16_t *val)
 Push a signed 16-bit integer.
YAPB_Result_t YAPB_push_i32 (YAPB_Packet_t *pkt, const int32_t *val)
 Push a signed 32-bit integer.
YAPB_Result_t YAPB_push_i64 (YAPB_Packet_t *pkt, const int64_t *val)
 Push a signed 64-bit integer.
YAPB_Result_t YAPB_push_float (YAPB_Packet_t *pkt, const float *val)
 Push a single-precision float.
YAPB_Result_t YAPB_push_double (YAPB_Packet_t *pkt, const double *val)
 Push a double-precision float.
YAPB_Result_t YAPB_push_blob (YAPB_Packet_t *pkt, const uint8_t *data, uint16_t len)
 Push a raw byte blob.
YAPB_Result_t YAPB_push_nested (YAPB_Packet_t *pkt, const YAPB_Packet_t *nested)
 Push a finalized nested packet.
YAPB_Result_t YAPB_pop_next (YAPB_Packet_t *pkt, YAPB_Element_t *out)
 Pop the next element regardless of its type.
YAPB_Result_t YAPB_pop_i8 (YAPB_Packet_t *pkt, int8_t *out)
 Pop a signed 8-bit integer.
YAPB_Result_t YAPB_pop_i16 (YAPB_Packet_t *pkt, int16_t *out)
 Pop a signed 16-bit integer.
YAPB_Result_t YAPB_pop_i32 (YAPB_Packet_t *pkt, int32_t *out)
 Pop a signed 32-bit integer.
YAPB_Result_t YAPB_pop_i64 (YAPB_Packet_t *pkt, int64_t *out)
 Pop a signed 64-bit integer.
YAPB_Result_t YAPB_pop_float (YAPB_Packet_t *pkt, float *out)
 Pop a single-precision float.
YAPB_Result_t YAPB_pop_double (YAPB_Packet_t *pkt, double *out)
 Pop a double-precision float.
YAPB_Result_t YAPB_pop_blob (YAPB_Packet_t *pkt, const uint8_t **data, uint16_t *len)
 Pop a raw byte blob.
YAPB_Result_t YAPB_pop_nested (YAPB_Packet_t *pkt, YAPB_Packet_t *out)
 Pop a nested packet.
YAPB_Result_t YAPB_get_error (const YAPB_Packet_t *pkt)
 Get the sticky error state of a packet.
YAPB_Result_t YAPB_get_elem_count (const YAPB_Packet_t *pkt, uint16_t *out_count)
 Count the number of elements in a packet.
const char * YAPB_Result_str (YAPB_Result_t result)
 Get a human-readable string for a result code.
const uint8_t * YAPB_get_buffer (const YAPB_Packet_t *pkt, size_t *out_len)
 Get a const pointer to the packet's backing buffer and its length.
bool YAPB_check_complete (const uint8_t *data, size_t len)
 Check if a receive buffer contains a complete YAPB packet.

Detailed Description

YAPB - Yet Another Protocol Buffer format.

Packet structure:

  • Header: 4 bytes pkt_len (network byte order, total packet size)
  • Data: Each element = 1 byte type + value (network byte order)
  • For BLOB: type + 2 byte length + raw bytes
  • For NESTED_PKT: type + nested packet (with its own 4 byte header)

Error handling: YAPB uses sticky errors (like errno/OpenGL). Once an error occurs, all subsequent pop/push calls return immediately with that error. Check the final state with YAPB_get_error() after a sequence of operations.

Pop functions do NOT modify the output value on error. This enables forward compatibility - initialize fields to defaults before popping, and they'll keep their default if the packet lacks that field:

uint16_t new_field = 42; // default for older packets
YAPB_pop_u16(pkt, &new_field); // stays 42 if packet ended