robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
8275b7b491
master
1#ifndef MPACK_CORE_H 2#define MPACK_CORE_H 3 4#ifndef MPACK_API 5# define MPACK_API extern 6#endif 7 8#include <assert.h> 9#include <limits.h> 10#include <stdbool.h> 11#include <stddef.h> 12 13#ifdef __GNUC__ 14# define FPURE __attribute__((const)) 15# define FNONULL __attribute__((nonnull)) 16# define FNONULL_ARG (x ) __attribute__((nonnull x)) 17# define FUNUSED __attribute__((unused)) 18#else 19# define FPURE 20# define FNONULL 21# define FNONULL_ARG (x ) 22# define FUNUSED 23#endif 24 25#if UINT_MAX == 0xffffffff 26typedef int mpack_sint32_t ; 27typedef unsigned int mpack_uint32_t ; 28#elif ULONG_MAX == 0xffffffff 29typedef long mpack_sint32_t ; 30typedef unsigned long mpack_uint32_t ; 31#else 32# error "can't find unsigned 32-bit integer type" 33#endif 34 35typedef struct mpack_value_s { 36mpack_uint32_t lo ,hi ; 37}mpack_value_t ; 38 39 40enum { 41MPACK_OK = 0 , 42MPACK_EOF = 1 , 43MPACK_ERROR = 2 44}; 45 46#define MPACK_MAX_TOKEN_LEN 9/* 64-bit ints/floats plus type code */ 47 48typedef enum { 49MPACK_TOKEN_NIL = 1 , 50MPACK_TOKEN_BOOLEAN = 2 , 51MPACK_TOKEN_UINT = 3 , 52MPACK_TOKEN_SINT = 4 , 53MPACK_TOKEN_FLOAT = 5 , 54MPACK_TOKEN_CHUNK = 6 , 55MPACK_TOKEN_ARRAY = 7 , 56MPACK_TOKEN_MAP = 8 , 57MPACK_TOKEN_BIN = 9 , 58MPACK_TOKEN_STR = 10 , 59MPACK_TOKEN_EXT = 11 60}mpack_token_type_t ; 61 62typedef struct mpack_token_s { 63mpack_token_type_t type ;/* Type of token */ 64mpack_uint32_t length ;/* Byte length for str/bin/ext/chunk/float/int/uint. 65Item count for array/map. */ 66union { 67mpack_value_t value ;/* 32-bit parts of primitives (bool,int,float) */ 68const char * chunk_ptr ;/* Chunk of data from str/bin/ext */ 69int ext_type ;/* Type field for ext tokens */ 70 }data ; 71}mpack_token_t ; 72 73typedef struct mpack_tokbuf_s { 74char pending [MPACK_MAX_TOKEN_LEN ]; 75mpack_token_t pending_tok ; 76size_t ppos ,plen ; 77mpack_uint32_t passthrough ; 78}mpack_tokbuf_t ; 79 80#define MPACK_TOKBUF_INITIAL_VALUE { { 0 }, { 0, 0, { { 0, 0 } } }, 0, 0, 0 } 81 82MPACK_API void mpack_tokbuf_init (mpack_tokbuf_t * tb )FUNUSED FNONULL ; 83MPACK_API int mpack_read (mpack_tokbuf_t * tb ,const char ** b ,size_t * bl , 84mpack_token_t * tok )FUNUSED FNONULL ; 85MPACK_API int mpack_write (mpack_tokbuf_t * tb ,char ** b ,size_t * bl , 86const mpack_token_t * tok )FUNUSED FNONULL ; 87int mpack_rtoken (const char ** buf ,size_t * buflen , 88mpack_token_t * tok ); 89 90#endif /* MPACK_CORE_H */