forked from camgunz/cmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmp.h
360 lines (286 loc) · 12.4 KB
/
cmp.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#ifndef CMP_H__
#define CMP_H__
#if defined (__cplusplus)
extern "C" {
#endif
struct cmp_ctx_s;
typedef bool (*cmp_reader)(struct cmp_ctx_s *ctx, void *data, size_t limit);
typedef size_t (*cmp_writer)(struct cmp_ctx_s *ctx, const void *data,
size_t count);
enum {
CMP_TYPE_POSITIVE_FIXNUM, /* 0 */
CMP_TYPE_FIXMAP, /* 1 */
CMP_TYPE_FIXARRAY, /* 2 */
CMP_TYPE_FIXSTR, /* 3 */
CMP_TYPE_NIL, /* 4 */
CMP_TYPE_BOOLEAN, /* 5 */
CMP_TYPE_BIN8, /* 6 */
CMP_TYPE_BIN16, /* 7 */
CMP_TYPE_BIN32, /* 8 */
CMP_TYPE_EXT8, /* 9 */
CMP_TYPE_EXT16, /* 10 */
CMP_TYPE_EXT32, /* 11 */
CMP_TYPE_FLOAT, /* 12 */
CMP_TYPE_DOUBLE, /* 13 */
CMP_TYPE_UINT8, /* 14 */
CMP_TYPE_UINT16, /* 15 */
CMP_TYPE_UINT32, /* 16 */
CMP_TYPE_UINT64, /* 17 */
CMP_TYPE_SINT8, /* 18 */
CMP_TYPE_SINT16, /* 19 */
CMP_TYPE_SINT32, /* 20 */
CMP_TYPE_SINT64, /* 21 */
CMP_TYPE_FIXEXT1, /* 22 */
CMP_TYPE_FIXEXT2, /* 23 */
CMP_TYPE_FIXEXT4, /* 24 */
CMP_TYPE_FIXEXT8, /* 25 */
CMP_TYPE_FIXEXT16, /* 26 */
CMP_TYPE_STR8, /* 27 */
CMP_TYPE_STR16, /* 28 */
CMP_TYPE_STR32, /* 29 */
CMP_TYPE_ARRAY16, /* 30 */
CMP_TYPE_ARRAY32, /* 31 */
CMP_TYPE_MAP16, /* 32 */
CMP_TYPE_MAP32, /* 33 */
CMP_TYPE_NEGATIVE_FIXNUM /* 34 */
};
typedef struct cmp_ext_s {
int8_t type;
uint32_t size;
} cmp_ext_t;
union cmp_object_data_u {
bool boolean;
uint8_t u8;
uint16_t u16;
uint32_t u32;
uint64_t u64;
int8_t s8;
int16_t s16;
int32_t s32;
int64_t s64;
float flt;
double dbl;
uint32_t array_size;
uint32_t map_size;
uint32_t str_size;
uint32_t bin_size;
cmp_ext_t ext;
};
typedef struct cmp_ctx_s {
uint8_t error;
void *buf;
cmp_reader read;
cmp_writer write;
} cmp_ctx_t;
typedef struct cmp_object_s {
uint8_t type;
union cmp_object_data_u as;
} cmp_object_t;
/*
* ============================================================================
* === Main API
* ============================================================================
*/
/* Initializes a CMP context */
void cmp_init(cmp_ctx_t *ctx, void *buf, cmp_reader read, cmp_writer write);
/* Returns CMP's version */
uint32_t cmp_version(void);
/* Returns the MessagePack version employed by CMP */
uint32_t cmp_mp_version(void);
/* Returns a string description of a CMP context's error */
const char* cmp_strerror(cmp_ctx_t *ctx);
/* Writes a signed integer to the backend */
bool cmp_write_sint(cmp_ctx_t *ctx, int64_t d);
/* Writes an unsigned integer to the backend */
bool cmp_write_uint(cmp_ctx_t *ctx, uint64_t u);
/* Writes a single-precision float to the backend */
bool cmp_write_float(cmp_ctx_t *ctx, float f);
/* Writes a double-precision float to the backend */
bool cmp_write_double(cmp_ctx_t *ctx, double d);
/* Writes NULL to the backend */
bool cmp_write_nil(cmp_ctx_t *ctx);
/* Writes true to the backend */
bool cmp_write_true(cmp_ctx_t *ctx);
/* Writes false to the backend */
bool cmp_write_false(cmp_ctx_t *ctx);
/* Writes a boolean value to the backend */
bool cmp_write_bool(cmp_ctx_t *ctx, bool b);
/*
* Writes an unsigned char's value to the backend as a boolean. This is useful
* if you are using a different boolean type in your application.
*/
bool cmp_write_u8_as_bool(cmp_ctx_t *ctx, uint8_t b);
/*
* Writes a string to the backend; according to the MessagePack spec, this must
* be encoded using UTF-8, but CMP leaves that job up to the programmer.
*/
bool cmp_write_str(cmp_ctx_t *ctx, const char *data, uint32_t size);
/*
* Writes the string marker to the backend. This is useful if you are writing
* data in chunks instead of a single shot.
*/
bool cmp_write_str_marker(cmp_ctx_t *ctx, uint32_t size);
/* Writes binary data to the backend */
bool cmp_write_bin(cmp_ctx_t *ctx, const void *data, uint32_t size);
/*
* Writes the binary data marker to the backend. This is useful if you are
* writing data in chunks instead of a single shot.
*/
bool cmp_write_bin_marker(cmp_ctx_t *ctx, uint32_t size);
/* Writes an array to the backend. */
bool cmp_write_array(cmp_ctx_t *ctx, uint32_t size);
/* Writes a map to the backend. */
bool cmp_write_map(cmp_ctx_t *ctx, uint32_t size);
/* Writes an extended type to the backend */
bool cmp_write_ext(cmp_ctx_t *ctx, int8_t type, uint32_t size,
const void *data);
/*
* Writes the extended type marker to the backend. This is useful if you want
* to write the type's data in chunks instead of a single shot.
*/
bool cmp_write_ext_marker(cmp_ctx_t *ctx, int8_t type, uint32_t size);
/* Writes an object to the backend */
bool cmp_write_object(cmp_ctx_t *ctx, cmp_object_t *obj);
/* Reads a signed integer that fits inside a signed char */
bool cmp_read_char(cmp_ctx_t *ctx, int8_t *c);
/* Reads a signed integer that fits inside a signed short */
bool cmp_read_short(cmp_ctx_t *ctx, int16_t *s);
/* Reads a signed integer that fits inside a signed int */
bool cmp_read_int(cmp_ctx_t *ctx, int32_t *i);
/* Reads a signed integer that fits inside a signed long */
bool cmp_read_long(cmp_ctx_t *ctx, int64_t *d);
/* Reads a signed integer */
bool cmp_read_sinteger(cmp_ctx_t *ctx, int64_t *d);
/* Reads an unsigned integer that fits inside an unsigned char */
bool cmp_read_uchar(cmp_ctx_t *ctx, uint8_t *c);
/* Reads an unsigned integer that fits inside an unsigned short */
bool cmp_read_ushort(cmp_ctx_t *ctx, uint16_t *s);
/* Reads an unsigned integer that fits inside an unsigned int */
bool cmp_read_uint(cmp_ctx_t *ctx, uint32_t *i);
/* Reads an unsigned integer that fits inside an unsigned long */
bool cmp_read_ulong(cmp_ctx_t *ctx, uint64_t *u);
/* Reads an unsigned integer */
bool cmp_read_uinteger(cmp_ctx_t *ctx, uint64_t *u);
/* Reads a single-precision float from the backend */
bool cmp_read_float(cmp_ctx_t *ctx, float *f);
/* Reads a double-precision float from the backend */
bool cmp_read_double(cmp_ctx_t *ctx, double *d);
/* "Reads" (more like "skips") a NULL value from the backend */
bool cmp_read_nil(cmp_ctx_t *ctx);
/* Reads a boolean from the backend */
bool cmp_read_bool(cmp_ctx_t *ctx, bool *b);
/*
* Reads a boolean as an unsigned char from the backend; this is useful if your
* application uses a different boolean type.
*/
bool cmp_read_bool_as_u8(cmp_ctx_t *ctx, uint8_t *b);
/* Reads a string's size from the backend */
bool cmp_read_str_size(cmp_ctx_t *ctx, uint32_t *size);
/*
* Reads a string from the backend; according to the spec, the string's data
* ought to be encoded using UTF-8,
*/
bool cmp_read_str(cmp_ctx_t *ctx, char *data, uint32_t *size);
/* Reads the size of packed binary data from the backend */
bool cmp_read_bin_size(cmp_ctx_t *ctx, uint32_t *size);
/* Reads packed binary data from the backend */
bool cmp_read_bin(cmp_ctx_t *ctx, void *data, uint32_t *size);
/* Reads an array from the backend */
bool cmp_read_array(cmp_ctx_t *ctx, uint32_t *size);
/* Reads a map from the backend */
bool cmp_read_map(cmp_ctx_t *ctx, uint32_t *size);
/* Reads the extended type's marker from the backend */
bool cmp_read_ext_marker(cmp_ctx_t *ctx, int8_t *type, uint32_t *size);
/* Reads an extended type from the backend */
bool cmp_read_ext(cmp_ctx_t *ctx, int8_t *type, uint32_t *size, void *data);
/* Reads an object from the backend */
bool cmp_read_object(cmp_ctx_t *ctx, cmp_object_t *obj);
/*
* ============================================================================
* === Specific API
* ============================================================================
*/
bool cmp_write_pfix(cmp_ctx_t *ctx, uint8_t c);
bool cmp_write_nfix(cmp_ctx_t *ctx, int8_t c);
bool cmp_write_sfix(cmp_ctx_t *ctx, int8_t c);
bool cmp_write_s8(cmp_ctx_t *ctx, int8_t c);
bool cmp_write_s16(cmp_ctx_t *ctx, int16_t s);
bool cmp_write_s32(cmp_ctx_t *ctx, int32_t i);
bool cmp_write_s64(cmp_ctx_t *ctx, int64_t l);
bool cmp_write_ufix(cmp_ctx_t *ctx, uint8_t c);
bool cmp_write_u8(cmp_ctx_t *ctx, uint8_t c);
bool cmp_write_u16(cmp_ctx_t *ctx, uint16_t s);
bool cmp_write_u32(cmp_ctx_t *ctx, uint32_t i);
bool cmp_write_u64(cmp_ctx_t *ctx, uint64_t l);
bool cmp_write_fixstr_marker(cmp_ctx_t *ctx, uint8_t size);
bool cmp_write_fixstr(cmp_ctx_t *ctx, const char *data, uint8_t size);
bool cmp_write_str8_marker(cmp_ctx_t *ctx, uint8_t size);
bool cmp_write_str8(cmp_ctx_t *ctx, const char *data, uint8_t size);
bool cmp_write_str16_marker(cmp_ctx_t *ctx, uint16_t size);
bool cmp_write_str16(cmp_ctx_t *ctx, const char *data, uint16_t size);
bool cmp_write_str32_marker(cmp_ctx_t *ctx, uint32_t size);
bool cmp_write_str32(cmp_ctx_t *ctx, const char *data, uint32_t size);
bool cmp_write_bin8_marker(cmp_ctx_t *ctx, uint8_t size);
bool cmp_write_bin8(cmp_ctx_t *ctx, const void *data, uint8_t size);
bool cmp_write_bin16_marker(cmp_ctx_t *ctx, uint16_t size);
bool cmp_write_bin16(cmp_ctx_t *ctx, const void *data, uint16_t size);
bool cmp_write_bin32_marker(cmp_ctx_t *ctx, uint32_t size);
bool cmp_write_bin32(cmp_ctx_t *ctx, const void *data, uint32_t size);
bool cmp_write_fixarray(cmp_ctx_t *ctx, uint8_t size);
bool cmp_write_array16(cmp_ctx_t *ctx, uint16_t size);
bool cmp_write_array32(cmp_ctx_t *ctx, uint32_t size);
bool cmp_write_fixmap(cmp_ctx_t *ctx, uint8_t size);
bool cmp_write_map16(cmp_ctx_t *ctx, uint16_t size);
bool cmp_write_map32(cmp_ctx_t *ctx, uint32_t size);
bool cmp_write_fixext1_marker(cmp_ctx_t *ctx, int8_t type);
bool cmp_write_fixext1(cmp_ctx_t *ctx, int8_t type, const void *data);
bool cmp_write_fixext2_marker(cmp_ctx_t *ctx, int8_t type);
bool cmp_write_fixext2(cmp_ctx_t *ctx, int8_t type, const void *data);
bool cmp_write_fixext4_marker(cmp_ctx_t *ctx, int8_t type);
bool cmp_write_fixext4(cmp_ctx_t *ctx, int8_t type, const void *data);
bool cmp_write_fixext8_marker(cmp_ctx_t *ctx, int8_t type);
bool cmp_write_fixext8(cmp_ctx_t *ctx, int8_t type, const void *data);
bool cmp_write_fixext16_marker(cmp_ctx_t *ctx, int8_t type);
bool cmp_write_fixext16(cmp_ctx_t *ctx, int8_t type, const void *data);
bool cmp_write_ext8_marker(cmp_ctx_t *ctx, int8_t type, uint8_t size);
bool cmp_write_ext8(cmp_ctx_t *ctx, int8_t type, uint8_t size,
const void *data);
bool cmp_write_ext16_marker(cmp_ctx_t *ctx, int8_t type, uint16_t size);
bool cmp_write_ext16(cmp_ctx_t *ctx, int8_t type, uint16_t size,
const void *data);
bool cmp_write_ext32_marker(cmp_ctx_t *ctx, int8_t type, uint32_t size);
bool cmp_write_ext32(cmp_ctx_t *ctx, int8_t type, uint32_t size,
const void *data);
bool cmp_read_pfix(cmp_ctx_t *ctx, uint8_t *c);
bool cmp_read_nfix(cmp_ctx_t *ctx, int8_t *c);
bool cmp_read_sfix(cmp_ctx_t *ctx, int8_t *c);
bool cmp_read_s8(cmp_ctx_t *ctx, int8_t *c);
bool cmp_read_s16(cmp_ctx_t *ctx, int16_t *s);
bool cmp_read_s32(cmp_ctx_t *ctx, int32_t *i);
bool cmp_read_s64(cmp_ctx_t *ctx, int64_t *l);
bool cmp_read_ufix(cmp_ctx_t *ctx, uint8_t *c);
bool cmp_read_u8(cmp_ctx_t *ctx, uint8_t *c);
bool cmp_read_u16(cmp_ctx_t *ctx, uint16_t *s);
bool cmp_read_u32(cmp_ctx_t *ctx, uint32_t *i);
bool cmp_read_u64(cmp_ctx_t *ctx, uint64_t *l);
bool cmp_read_fixext1_marker(cmp_ctx_t *ctx, int8_t *type);
bool cmp_read_fixext1(cmp_ctx_t *ctx, int8_t *type, void *data);
bool cmp_read_fixext2_marker(cmp_ctx_t *ctx, int8_t *type);
bool cmp_read_fixext2(cmp_ctx_t *ctx, int8_t *type, void *data);
bool cmp_read_fixext4_marker(cmp_ctx_t *ctx, int8_t *type);
bool cmp_read_fixext4(cmp_ctx_t *ctx, int8_t *type, void *data);
bool cmp_read_fixext8_marker(cmp_ctx_t *ctx, int8_t *type);
bool cmp_read_fixext8(cmp_ctx_t *ctx, int8_t *type, void *data);
bool cmp_read_fixext16_marker(cmp_ctx_t *ctx, int8_t *type);
bool cmp_read_fixext16(cmp_ctx_t *ctx, int8_t *type, void *data);
bool cmp_read_ext8_marker(cmp_ctx_t *ctx, int8_t *type, uint8_t *size);
bool cmp_read_ext8(cmp_ctx_t *ctx, int8_t *type, uint8_t *size, void *data);
bool cmp_read_ext16_marker(cmp_ctx_t *ctx, int8_t *type, uint16_t *size);
bool cmp_read_ext16(cmp_ctx_t *ctx, int8_t *type, uint16_t *size, void *data);
bool cmp_read_ext32_marker(cmp_ctx_t *ctx, int8_t *type, uint32_t *size);
bool cmp_read_ext32(cmp_ctx_t *ctx, int8_t *type, uint32_t *size, void *data);
#if defined (__cplusplus)
}
#endif
#endif
/* vi: set et ts=2 sw=2: */