json-gen-c  0.1.5
json-gen-c generate C code for json manipulation
msgpack_codec.h
Go to the documentation of this file.
1
9#ifndef MSGPACK_CODEC_H_
10#define MSGPACK_CODEC_H_
11
12#include <stdint.h>
13#include <stddef.h>
14#include <string.h>
15
16/* ── MessagePack format constants ───────────────────────────────────── */
17
18/* Positive fixint: 0x00 – 0x7f */
19#define MP_FIXINT_MAX 0x7f
20
21/* Negative fixint: 0xe0 – 0xff (values -32 .. -1) */
22#define MP_NEG_FIXINT 0xe0
23
24/* Nil, booleans */
25#define MP_NIL 0xc0
26#define MP_FALSE 0xc2
27#define MP_TRUE 0xc3
28
29/* Floats */
30#define MP_FLOAT32 0xca
31#define MP_FLOAT64 0xcb
32
33/* Unsigned integers */
34#define MP_UINT8 0xcc
35#define MP_UINT16 0xcd
36#define MP_UINT32 0xce
37#define MP_UINT64 0xcf
38
39/* Signed integers */
40#define MP_INT8 0xd0
41#define MP_INT16 0xd1
42#define MP_INT32 0xd2
43#define MP_INT64 0xd3
44
45/* Strings */
46#define MP_FIXSTR 0xa0 /* 101xxxxx, len in low 5 bits */
47#define MP_FIXSTR_MASK 0x1f
48#define MP_STR8 0xd9
49#define MP_STR16 0xda
50#define MP_STR32 0xdb
51
52/* Arrays */
53#define MP_FIXARRAY 0x90 /* 1001xxxx, len in low 4 bits */
54#define MP_FIXARRAY_MASK 0x0f
55#define MP_ARRAY16 0xdc
56#define MP_ARRAY32 0xdd
57
58/* Maps */
59#define MP_FIXMAP 0x80 /* 1000xxxx, len in low 4 bits */
60#define MP_FIXMAP_MASK 0x0f
61#define MP_MAP16 0xde
62#define MP_MAP32 0xdf
63
64/* ── Read cursor ────────────────────────────────────────────────────── */
65
66struct mp_reader {
67 const unsigned char *data;
68 size_t len;
69 size_t pos;
70};
71
72/* ── Field offset item (shared with JSON — same layout) ───────────── */
73/* Reused from the generated offset map; declared in gencode output. */
74
75/* ── Pack (encode) API ──────────────────────────────────────────────── */
76
77static void mp_pack_nil(sstr_t out);
78static void mp_pack_bool(sstr_t out, int v);
79static void mp_pack_int(sstr_t out, int64_t v);
80static void mp_pack_uint(sstr_t out, uint64_t v);
81static void mp_pack_float(sstr_t out, float v);
82static void mp_pack_double(sstr_t out, double v);
83static void mp_pack_str(sstr_t out, const char *s, uint32_t len);
84static void mp_pack_sstr(sstr_t out, sstr_t s);
85static void mp_pack_array_header(sstr_t out, uint32_t count);
86static void mp_pack_map_header(sstr_t out, uint32_t count);
87
88/* ── Unpack (decode) API ────────────────────────────────────────────── */
89
90static int mp_reader_init(struct mp_reader *r, const unsigned char *data, size_t len);
91static int mp_peek(struct mp_reader *r);
92
93static int mp_unpack_nil(struct mp_reader *r);
94static int mp_unpack_bool(struct mp_reader *r, int *out);
95static int mp_unpack_int64(struct mp_reader *r, int64_t *out);
96static int mp_unpack_uint64(struct mp_reader *r, uint64_t *out);
97static int mp_unpack_float(struct mp_reader *r, float *out);
98static int mp_unpack_double(struct mp_reader *r, double *out);
99static int mp_unpack_str(struct mp_reader *r, const char **out, uint32_t *out_len);
100static int mp_unpack_array_header(struct mp_reader *r, uint32_t *count);
101static int mp_unpack_map_header(struct mp_reader *r, uint32_t *count);
102
103/* Skip one value (any type, recursively for containers). */
104static int mp_unpack_skip(struct mp_reader *r);
105
106/* Generic numeric unpack — reads any int/uint/float into an int64_t. */
107static int mp_unpack_number_as_int64(struct mp_reader *r, int64_t *out);
108static int mp_unpack_number_as_uint64(struct mp_reader *r, uint64_t *out);
109static int mp_unpack_number_as_double(struct mp_reader *r, double *out);
110
111#endif /* MSGPACK_CODEC_H_ */
void * sstr_t
sstr_t are objects that represent sequences of characters.
Definition sstr.h:75
Definition msgpack_codec.h:66
position of string
Definition struct_parse.h:175