-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbser_private.h
94 lines (81 loc) · 2.23 KB
/
bser_private.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifndef LIBWATCHMAN_BSER_PRIVATE_H_
#define LIBWATCHMAN_BSER_PRIVATE_H_
#include <stdint.h>
#include <stdlib.h>
enum {
BSER_TAG_ARRAY = 0x00,
BSER_TAG_OBJECT = 0x01,
BSER_TAG_STRING = 0x02,
BSER_TAG_INT8 = 0x03,
BSER_TAG_INT16 = 0x04,
BSER_TAG_INT32 = 0x05,
BSER_TAG_INT64 = 0x06,
BSER_TAG_REAL = 0x07,
BSER_TAG_TRUE = 0x08,
BSER_TAG_FALSE = 0x09,
BSER_TAG_NULL = 0x0a,
BSER_TAG_COMPACT_ARRAY = 0x0b,
BSER_TAG_NO_FIELD = 0x0c,
BSER_TAG_UNPARSED = 0x0d,
BSER_TAG_ERROR = 0x0e,
BSER_NUM_TAGS = 0x0f
};
typedef struct bser_buffer {
void* data;
size_t datalen;
size_t cursor;
} bser_buffer_t;
struct bser_key_value_pair;
struct bser_buffer;
typedef struct bser {
uint8_t type;
union {
int64_t integer;
double real;
struct {
const char* chars;
size_t length;
} string;
struct {
struct bser* elements;
size_t length;
} array;
struct {
struct bser_key_value_pair* fields;
size_t length;
} object;
struct bser_buffer* unparsed;
const char* error_message;
} value;
} bser_t;
typedef struct bser_key_value_pair {
struct bser key;
struct bser value;
} bser_key_value_pair_t;
static inline bser_t* bser_alloc(void)
{
return (bser_t*)malloc(sizeof(bser_t));
}
void bser_parse_generic(bser_t* fill, struct bser_buffer* buffer);
static inline void bser_parse_if_necessary(bser_t* bser)
{
if (bser->type == BSER_TAG_UNPARSED) {
bser_parse_generic(bser, bser->value.unparsed);
}
}
static inline int bser_is_unparsed(bser_t* bser)
{
return bser->type == BSER_TAG_UNPARSED;
}
static inline int bser_is_no_field(bser_t* bser)
{
return bser->type == BSER_TAG_NO_FIELD;
}
void bser_parse_array_elements_to(struct bser* array, size_t limit);
void bser_parse_object_fields_to(struct bser* array, size_t limit);
static inline bser_key_value_pair_t* bser_object_pair_at(
struct bser* bser, size_t index)
{
return &bser->value.object.fields[index];
}
#endif /* ndef LIBWATCHMAN_BSER_PRIVATE_H_ */