robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

bfredlMerge pull request #39736 from bfredl/odod8275b7b491

master
2.3 KiB90 linesraw
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 {
36  mpack_uint32_t lo, hi;
37} mpack_value_t;
38
39
40enum {
41  MPACK_OK = 0,
42  MPACK_EOF = 1,
43  MPACK_ERROR = 2
44};
45
46#define MPACK_MAX_TOKEN_LEN 9  /* 64-bit ints/floats plus type code */
47
48typedef enum {
49  MPACK_TOKEN_NIL       = 1,
50  MPACK_TOKEN_BOOLEAN   = 2,
51  MPACK_TOKEN_UINT      = 3,
52  MPACK_TOKEN_SINT      = 4,
53  MPACK_TOKEN_FLOAT     = 5,
54  MPACK_TOKEN_CHUNK     = 6,
55  MPACK_TOKEN_ARRAY     = 7,
56  MPACK_TOKEN_MAP       = 8,
57  MPACK_TOKEN_BIN       = 9,
58  MPACK_TOKEN_STR       = 10,
59  MPACK_TOKEN_EXT       = 11
60} mpack_token_type_t;
61
62typedef struct mpack_token_s {
63  mpack_token_type_t type;  /* Type of token */
64  mpack_uint32_t length;    /* Byte length for str/bin/ext/chunk/float/int/uint.
65                               Item count for array/map. */
66  union {
67    mpack_value_t value;    /* 32-bit parts of primitives (bool,int,float) */
68    const char *chunk_ptr;  /* Chunk of data from str/bin/ext */
69    int ext_type;           /* Type field for ext tokens */
70  } data;
71} mpack_token_t;
72
73typedef struct mpack_tokbuf_s {
74  char pending[MPACK_MAX_TOKEN_LEN];
75  mpack_token_t pending_tok;
76  size_t ppos, plen;
77  mpack_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,
84    mpack_token_t *tok) FUNUSED FNONULL;
85MPACK_API int mpack_write(mpack_tokbuf_t *tb, char **b, size_t *bl,
86    const mpack_token_t *tok) FUNUSED FNONULL;
87int mpack_rtoken(const char **buf, size_t *buflen,
88    mpack_token_t *tok);
89
90#endif  /* MPACK_CORE_H */