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