sq_detailed_metrics 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/extconf.rb +26 -0
- data/include/half.hpp +4575 -0
- data/include/msgpack.h +24 -0
- data/include/msgpack/fbuffer.h +42 -0
- data/include/msgpack/gcc_atomic.h +25 -0
- data/include/msgpack/object.h +118 -0
- data/include/msgpack/pack.h +174 -0
- data/include/msgpack/pack_define.h +18 -0
- data/include/msgpack/pack_template.h +952 -0
- data/include/msgpack/sbuffer.h +115 -0
- data/include/msgpack/sysdep.h +221 -0
- data/include/msgpack/timestamp.h +58 -0
- data/include/msgpack/unpack.h +281 -0
- data/include/msgpack/unpack_define.h +89 -0
- data/include/msgpack/unpack_template.h +471 -0
- data/include/msgpack/util.h +15 -0
- data/include/msgpack/version.h +38 -0
- data/include/msgpack/version_master.h +3 -0
- data/include/msgpack/vrefbuffer.h +144 -0
- data/include/msgpack/zbuffer.h +205 -0
- data/include/msgpack/zone.h +163 -0
- data/include/rapidjson/allocators.h +271 -0
- data/include/rapidjson/document.h +2575 -0
- data/include/rapidjson/encodedstream.h +299 -0
- data/include/rapidjson/encodings.h +716 -0
- data/include/rapidjson/error/en.h +74 -0
- data/include/rapidjson/error/error.h +155 -0
- data/include/rapidjson/filereadstream.h +99 -0
- data/include/rapidjson/filewritestream.h +104 -0
- data/include/rapidjson/fwd.h +151 -0
- data/include/rapidjson/internal/biginteger.h +290 -0
- data/include/rapidjson/internal/diyfp.h +258 -0
- data/include/rapidjson/internal/dtoa.h +245 -0
- data/include/rapidjson/internal/ieee754.h +78 -0
- data/include/rapidjson/internal/itoa.h +304 -0
- data/include/rapidjson/internal/meta.h +181 -0
- data/include/rapidjson/internal/pow10.h +55 -0
- data/include/rapidjson/internal/regex.h +701 -0
- data/include/rapidjson/internal/stack.h +230 -0
- data/include/rapidjson/internal/strfunc.h +55 -0
- data/include/rapidjson/internal/strtod.h +269 -0
- data/include/rapidjson/internal/swap.h +46 -0
- data/include/rapidjson/istreamwrapper.h +115 -0
- data/include/rapidjson/memorybuffer.h +70 -0
- data/include/rapidjson/memorystream.h +71 -0
- data/include/rapidjson/msinttypes/inttypes.h +316 -0
- data/include/rapidjson/msinttypes/stdint.h +300 -0
- data/include/rapidjson/ostreamwrapper.h +81 -0
- data/include/rapidjson/pointer.h +1358 -0
- data/include/rapidjson/prettywriter.h +255 -0
- data/include/rapidjson/rapidjson.h +615 -0
- data/include/rapidjson/reader.h +1879 -0
- data/include/rapidjson/schema.h +2006 -0
- data/include/rapidjson/stream.h +179 -0
- data/include/rapidjson/stringbuffer.h +117 -0
- data/include/rapidjson/writer.h +610 -0
- data/include/xxhash.h +328 -0
- data/json_conv.cpp +284 -0
- data/json_conv.hpp +17 -0
- data/metrics.cpp +239 -0
- data/metrics.hpp +84 -0
- data/msgpack/objectc.c +482 -0
- data/msgpack/unpack.c +703 -0
- data/msgpack/version.c +22 -0
- data/msgpack/vrefbuffer.c +250 -0
- data/msgpack/zone.c +222 -0
- data/sq_detailed_metrics.cpp +248 -0
- metadata +199 -0
data/msgpack/unpack.c
ADDED
@@ -0,0 +1,703 @@
|
|
1
|
+
/*
|
2
|
+
* MessagePack for C unpacking routine
|
3
|
+
*
|
4
|
+
* Copyright (C) 2008-2009 FURUHASHI Sadayuki
|
5
|
+
*
|
6
|
+
* Distributed under the Boost Software License, Version 1.0.
|
7
|
+
* (See accompanying file LICENSE_1_0.txt or copy at
|
8
|
+
* http://www.boost.org/LICENSE_1_0.txt)
|
9
|
+
*/
|
10
|
+
#include "msgpack/unpack.h"
|
11
|
+
#include "msgpack/unpack_define.h"
|
12
|
+
#include "msgpack/util.h"
|
13
|
+
#include <stdlib.h>
|
14
|
+
#include <stdint.h>
|
15
|
+
|
16
|
+
#ifdef _msgpack_atomic_counter_header
|
17
|
+
#include _msgpack_atomic_counter_header
|
18
|
+
#endif
|
19
|
+
|
20
|
+
|
21
|
+
typedef struct {
|
22
|
+
msgpack_zone** z;
|
23
|
+
bool referenced;
|
24
|
+
} unpack_user;
|
25
|
+
|
26
|
+
|
27
|
+
#define msgpack_unpack_struct(name) \
|
28
|
+
struct template ## name
|
29
|
+
|
30
|
+
#define msgpack_unpack_func(ret, name) \
|
31
|
+
ret template ## name
|
32
|
+
|
33
|
+
#define msgpack_unpack_callback(name) \
|
34
|
+
template_callback ## name
|
35
|
+
|
36
|
+
#define msgpack_unpack_object msgpack_object
|
37
|
+
|
38
|
+
#define msgpack_unpack_user unpack_user
|
39
|
+
|
40
|
+
|
41
|
+
struct template_context;
|
42
|
+
typedef struct template_context template_context;
|
43
|
+
|
44
|
+
static void template_init(template_context* ctx);
|
45
|
+
|
46
|
+
static msgpack_object template_data(template_context* ctx);
|
47
|
+
|
48
|
+
static int template_execute(
|
49
|
+
template_context* ctx, const char* data, size_t len, size_t* off);
|
50
|
+
|
51
|
+
|
52
|
+
static inline msgpack_object template_callback_root(unpack_user* u)
|
53
|
+
{
|
54
|
+
msgpack_object o;
|
55
|
+
MSGPACK_UNUSED(u);
|
56
|
+
o.type = MSGPACK_OBJECT_NIL;
|
57
|
+
return o;
|
58
|
+
}
|
59
|
+
|
60
|
+
static inline int template_callback_uint8(unpack_user* u, uint8_t d, msgpack_object* o)
|
61
|
+
{
|
62
|
+
MSGPACK_UNUSED(u);
|
63
|
+
o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
|
64
|
+
o->via.u64 = d;
|
65
|
+
return 0;
|
66
|
+
}
|
67
|
+
|
68
|
+
static inline int template_callback_uint16(unpack_user* u, uint16_t d, msgpack_object* o)
|
69
|
+
{
|
70
|
+
MSGPACK_UNUSED(u);
|
71
|
+
o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
|
72
|
+
o->via.u64 = d;
|
73
|
+
return 0;
|
74
|
+
}
|
75
|
+
|
76
|
+
static inline int template_callback_uint32(unpack_user* u, uint32_t d, msgpack_object* o)
|
77
|
+
{
|
78
|
+
MSGPACK_UNUSED(u);
|
79
|
+
o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
|
80
|
+
o->via.u64 = d;
|
81
|
+
return 0;
|
82
|
+
}
|
83
|
+
|
84
|
+
static inline int template_callback_uint64(unpack_user* u, uint64_t d, msgpack_object* o)
|
85
|
+
{
|
86
|
+
MSGPACK_UNUSED(u);
|
87
|
+
o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
|
88
|
+
o->via.u64 = d;
|
89
|
+
return 0;
|
90
|
+
}
|
91
|
+
|
92
|
+
static inline int template_callback_int8(unpack_user* u, int8_t d, msgpack_object* o)
|
93
|
+
{
|
94
|
+
MSGPACK_UNUSED(u);
|
95
|
+
if(d >= 0) {
|
96
|
+
o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
|
97
|
+
o->via.u64 = (uint64_t)d;
|
98
|
+
return 0;
|
99
|
+
}
|
100
|
+
else {
|
101
|
+
o->type = MSGPACK_OBJECT_NEGATIVE_INTEGER;
|
102
|
+
o->via.i64 = d;
|
103
|
+
return 0;
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
static inline int template_callback_int16(unpack_user* u, int16_t d, msgpack_object* o)
|
108
|
+
{
|
109
|
+
MSGPACK_UNUSED(u);
|
110
|
+
if(d >= 0) {
|
111
|
+
o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
|
112
|
+
o->via.u64 = (uint64_t)d;
|
113
|
+
return 0;
|
114
|
+
}
|
115
|
+
else {
|
116
|
+
o->type = MSGPACK_OBJECT_NEGATIVE_INTEGER;
|
117
|
+
o->via.i64 = d;
|
118
|
+
return 0;
|
119
|
+
}
|
120
|
+
}
|
121
|
+
|
122
|
+
static inline int template_callback_int32(unpack_user* u, int32_t d, msgpack_object* o)
|
123
|
+
{
|
124
|
+
MSGPACK_UNUSED(u);
|
125
|
+
if(d >= 0) {
|
126
|
+
o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
|
127
|
+
o->via.u64 = (uint64_t)d;
|
128
|
+
return 0;
|
129
|
+
}
|
130
|
+
else {
|
131
|
+
o->type = MSGPACK_OBJECT_NEGATIVE_INTEGER;
|
132
|
+
o->via.i64 = d;
|
133
|
+
return 0;
|
134
|
+
}
|
135
|
+
}
|
136
|
+
|
137
|
+
static inline int template_callback_int64(unpack_user* u, int64_t d, msgpack_object* o)
|
138
|
+
{
|
139
|
+
MSGPACK_UNUSED(u);
|
140
|
+
if(d >= 0) {
|
141
|
+
o->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
|
142
|
+
o->via.u64 = (uint64_t)d;
|
143
|
+
return 0;
|
144
|
+
}
|
145
|
+
else {
|
146
|
+
o->type = MSGPACK_OBJECT_NEGATIVE_INTEGER;
|
147
|
+
o->via.i64 = d;
|
148
|
+
return 0;
|
149
|
+
}
|
150
|
+
}
|
151
|
+
|
152
|
+
static inline int template_callback_float(unpack_user* u, float d, msgpack_object* o)
|
153
|
+
{
|
154
|
+
MSGPACK_UNUSED(u);
|
155
|
+
o->type = MSGPACK_OBJECT_FLOAT32;
|
156
|
+
o->via.f64 = d;
|
157
|
+
return 0;
|
158
|
+
}
|
159
|
+
|
160
|
+
static inline int template_callback_double(unpack_user* u, double d, msgpack_object* o)
|
161
|
+
{
|
162
|
+
MSGPACK_UNUSED(u);
|
163
|
+
o->type = MSGPACK_OBJECT_FLOAT64;
|
164
|
+
o->via.f64 = d;
|
165
|
+
return 0;
|
166
|
+
}
|
167
|
+
|
168
|
+
static inline int template_callback_nil(unpack_user* u, msgpack_object* o)
|
169
|
+
{
|
170
|
+
MSGPACK_UNUSED(u);
|
171
|
+
o->type = MSGPACK_OBJECT_NIL;
|
172
|
+
return 0;
|
173
|
+
}
|
174
|
+
|
175
|
+
static inline int template_callback_true(unpack_user* u, msgpack_object* o)
|
176
|
+
{
|
177
|
+
MSGPACK_UNUSED(u);
|
178
|
+
o->type = MSGPACK_OBJECT_BOOLEAN;
|
179
|
+
o->via.boolean = true;
|
180
|
+
return 0;
|
181
|
+
}
|
182
|
+
|
183
|
+
static inline int template_callback_false(unpack_user* u, msgpack_object* o)
|
184
|
+
{
|
185
|
+
MSGPACK_UNUSED(u);
|
186
|
+
o->type = MSGPACK_OBJECT_BOOLEAN;
|
187
|
+
o->via.boolean = false;
|
188
|
+
return 0;
|
189
|
+
}
|
190
|
+
|
191
|
+
static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_object* o)
|
192
|
+
{
|
193
|
+
size_t size;
|
194
|
+
// Let's leverage the fact that sizeof(msgpack_object) is a compile time constant
|
195
|
+
// to check for int overflows.
|
196
|
+
// Note - while n is constrained to 32-bit, the product of n * sizeof(msgpack_object)
|
197
|
+
// might not be constrained to 4GB on 64-bit systems
|
198
|
+
#if SIZE_MAX == UINT_MAX
|
199
|
+
if (n > SIZE_MAX/sizeof(msgpack_object))
|
200
|
+
return MSGPACK_UNPACK_NOMEM_ERROR;
|
201
|
+
#endif
|
202
|
+
|
203
|
+
o->type = MSGPACK_OBJECT_ARRAY;
|
204
|
+
o->via.array.size = 0;
|
205
|
+
|
206
|
+
size = n * sizeof(msgpack_object);
|
207
|
+
|
208
|
+
if (*u->z == NULL) {
|
209
|
+
*u->z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
|
210
|
+
if(*u->z == NULL) {
|
211
|
+
return MSGPACK_UNPACK_NOMEM_ERROR;
|
212
|
+
}
|
213
|
+
}
|
214
|
+
|
215
|
+
// Unsure whether size = 0 should be an error, and if so, what to return
|
216
|
+
o->via.array.ptr = (msgpack_object*)msgpack_zone_malloc(*u->z, size);
|
217
|
+
if(o->via.array.ptr == NULL) { return MSGPACK_UNPACK_NOMEM_ERROR; }
|
218
|
+
return 0;
|
219
|
+
}
|
220
|
+
|
221
|
+
static inline int template_callback_array_item(unpack_user* u, msgpack_object* c, msgpack_object o)
|
222
|
+
{
|
223
|
+
MSGPACK_UNUSED(u);
|
224
|
+
#if defined(__GNUC__) && !defined(__clang__)
|
225
|
+
memcpy(&c->via.array.ptr[c->via.array.size], &o, sizeof(msgpack_object));
|
226
|
+
#else /* __GNUC__ && !__clang__ */
|
227
|
+
c->via.array.ptr[c->via.array.size] = o;
|
228
|
+
#endif /* __GNUC__ && !__clang__ */
|
229
|
+
++c->via.array.size;
|
230
|
+
return 0;
|
231
|
+
}
|
232
|
+
|
233
|
+
static inline int template_callback_map(unpack_user* u, unsigned int n, msgpack_object* o)
|
234
|
+
{
|
235
|
+
size_t size;
|
236
|
+
// Let's leverage the fact that sizeof(msgpack_object_kv) is a compile time constant
|
237
|
+
// to check for int overflows
|
238
|
+
// Note - while n is constrained to 32-bit, the product of n * sizeof(msgpack_object)
|
239
|
+
// might not be constrained to 4GB on 64-bit systems
|
240
|
+
|
241
|
+
// Note - this will always be false on 64-bit systems
|
242
|
+
#if SIZE_MAX == UINT_MAX
|
243
|
+
if (n > SIZE_MAX/sizeof(msgpack_object_kv))
|
244
|
+
return MSGPACK_UNPACK_NOMEM_ERROR;
|
245
|
+
#endif
|
246
|
+
|
247
|
+
o->type = MSGPACK_OBJECT_MAP;
|
248
|
+
o->via.map.size = 0;
|
249
|
+
|
250
|
+
size = n * sizeof(msgpack_object_kv);
|
251
|
+
|
252
|
+
if (*u->z == NULL) {
|
253
|
+
*u->z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
|
254
|
+
if(*u->z == NULL) {
|
255
|
+
return MSGPACK_UNPACK_NOMEM_ERROR;
|
256
|
+
}
|
257
|
+
}
|
258
|
+
|
259
|
+
// Should size = 0 be an error? If so, what error to return?
|
260
|
+
o->via.map.ptr = (msgpack_object_kv*)msgpack_zone_malloc(*u->z, size);
|
261
|
+
if(o->via.map.ptr == NULL) { return MSGPACK_UNPACK_NOMEM_ERROR; }
|
262
|
+
return 0;
|
263
|
+
}
|
264
|
+
|
265
|
+
static inline int template_callback_map_item(unpack_user* u, msgpack_object* c, msgpack_object k, msgpack_object v)
|
266
|
+
{
|
267
|
+
MSGPACK_UNUSED(u);
|
268
|
+
#if defined(__GNUC__) && !defined(__clang__)
|
269
|
+
memcpy(&c->via.map.ptr[c->via.map.size].key, &k, sizeof(msgpack_object));
|
270
|
+
memcpy(&c->via.map.ptr[c->via.map.size].val, &v, sizeof(msgpack_object));
|
271
|
+
#else /* __GNUC__ && !__clang__ */
|
272
|
+
c->via.map.ptr[c->via.map.size].key = k;
|
273
|
+
c->via.map.ptr[c->via.map.size].val = v;
|
274
|
+
#endif /* __GNUC__ && !__clang__ */
|
275
|
+
++c->via.map.size;
|
276
|
+
return 0;
|
277
|
+
}
|
278
|
+
|
279
|
+
static inline int template_callback_str(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_object* o)
|
280
|
+
{
|
281
|
+
MSGPACK_UNUSED(b);
|
282
|
+
if (*u->z == NULL) {
|
283
|
+
*u->z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
|
284
|
+
if(*u->z == NULL) {
|
285
|
+
return MSGPACK_UNPACK_NOMEM_ERROR;
|
286
|
+
}
|
287
|
+
}
|
288
|
+
o->type = MSGPACK_OBJECT_STR;
|
289
|
+
o->via.str.ptr = p;
|
290
|
+
o->via.str.size = l;
|
291
|
+
u->referenced = true;
|
292
|
+
return 0;
|
293
|
+
}
|
294
|
+
|
295
|
+
static inline int template_callback_bin(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_object* o)
|
296
|
+
{
|
297
|
+
MSGPACK_UNUSED(b);
|
298
|
+
if (*u->z == NULL) {
|
299
|
+
*u->z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
|
300
|
+
if(*u->z == NULL) {
|
301
|
+
return MSGPACK_UNPACK_NOMEM_ERROR;
|
302
|
+
}
|
303
|
+
}
|
304
|
+
o->type = MSGPACK_OBJECT_BIN;
|
305
|
+
o->via.bin.ptr = p;
|
306
|
+
o->via.bin.size = l;
|
307
|
+
u->referenced = true;
|
308
|
+
return 0;
|
309
|
+
}
|
310
|
+
|
311
|
+
static inline int template_callback_ext(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_object* o)
|
312
|
+
{
|
313
|
+
MSGPACK_UNUSED(b);
|
314
|
+
if (l == 0) {
|
315
|
+
return MSGPACK_UNPACK_PARSE_ERROR;
|
316
|
+
}
|
317
|
+
if (*u->z == NULL) {
|
318
|
+
*u->z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
|
319
|
+
if(*u->z == NULL) {
|
320
|
+
return MSGPACK_UNPACK_NOMEM_ERROR;
|
321
|
+
}
|
322
|
+
}
|
323
|
+
o->type = MSGPACK_OBJECT_EXT;
|
324
|
+
o->via.ext.type = *p;
|
325
|
+
o->via.ext.ptr = p + 1;
|
326
|
+
o->via.ext.size = l - 1;
|
327
|
+
u->referenced = true;
|
328
|
+
return 0;
|
329
|
+
}
|
330
|
+
|
331
|
+
#include "msgpack/unpack_template.h"
|
332
|
+
|
333
|
+
|
334
|
+
#define CTX_CAST(m) ((template_context*)(m))
|
335
|
+
#define CTX_REFERENCED(mpac) CTX_CAST((mpac)->ctx)->user.referenced
|
336
|
+
|
337
|
+
#define COUNTER_SIZE (sizeof(_msgpack_atomic_counter_t))
|
338
|
+
|
339
|
+
|
340
|
+
static inline void init_count(void* buffer)
|
341
|
+
{
|
342
|
+
*(volatile _msgpack_atomic_counter_t*)buffer = 1;
|
343
|
+
}
|
344
|
+
|
345
|
+
static inline void decr_count(void* buffer)
|
346
|
+
{
|
347
|
+
// atomic if(--*(_msgpack_atomic_counter_t*)buffer == 0) { free(buffer); }
|
348
|
+
if(_msgpack_sync_decr_and_fetch((volatile _msgpack_atomic_counter_t*)buffer) == 0) {
|
349
|
+
free(buffer);
|
350
|
+
}
|
351
|
+
}
|
352
|
+
|
353
|
+
static inline void incr_count(void* buffer)
|
354
|
+
{
|
355
|
+
// atomic ++*(_msgpack_atomic_counter_t*)buffer;
|
356
|
+
_msgpack_sync_incr_and_fetch((volatile _msgpack_atomic_counter_t*)buffer);
|
357
|
+
}
|
358
|
+
|
359
|
+
static inline _msgpack_atomic_counter_t get_count(void* buffer)
|
360
|
+
{
|
361
|
+
return *(volatile _msgpack_atomic_counter_t*)buffer;
|
362
|
+
}
|
363
|
+
|
364
|
+
bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size)
|
365
|
+
{
|
366
|
+
char* buffer;
|
367
|
+
void* ctx;
|
368
|
+
|
369
|
+
if(initial_buffer_size < COUNTER_SIZE) {
|
370
|
+
initial_buffer_size = COUNTER_SIZE;
|
371
|
+
}
|
372
|
+
|
373
|
+
buffer = (char*)malloc(initial_buffer_size);
|
374
|
+
if(buffer == NULL) {
|
375
|
+
return false;
|
376
|
+
}
|
377
|
+
|
378
|
+
ctx = malloc(sizeof(template_context));
|
379
|
+
if(ctx == NULL) {
|
380
|
+
free(buffer);
|
381
|
+
return false;
|
382
|
+
}
|
383
|
+
|
384
|
+
mpac->buffer = buffer;
|
385
|
+
mpac->used = COUNTER_SIZE;
|
386
|
+
mpac->free = initial_buffer_size - mpac->used;
|
387
|
+
mpac->off = COUNTER_SIZE;
|
388
|
+
mpac->parsed = 0;
|
389
|
+
mpac->initial_buffer_size = initial_buffer_size;
|
390
|
+
mpac->z = NULL;
|
391
|
+
mpac->ctx = ctx;
|
392
|
+
|
393
|
+
init_count(mpac->buffer);
|
394
|
+
|
395
|
+
template_init(CTX_CAST(mpac->ctx));
|
396
|
+
CTX_CAST(mpac->ctx)->user.z = &mpac->z;
|
397
|
+
CTX_CAST(mpac->ctx)->user.referenced = false;
|
398
|
+
|
399
|
+
return true;
|
400
|
+
}
|
401
|
+
|
402
|
+
void msgpack_unpacker_destroy(msgpack_unpacker* mpac)
|
403
|
+
{
|
404
|
+
msgpack_zone_free(mpac->z);
|
405
|
+
free(mpac->ctx);
|
406
|
+
decr_count(mpac->buffer);
|
407
|
+
}
|
408
|
+
|
409
|
+
msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size)
|
410
|
+
{
|
411
|
+
msgpack_unpacker* mpac = (msgpack_unpacker*)malloc(sizeof(msgpack_unpacker));
|
412
|
+
if(mpac == NULL) {
|
413
|
+
return NULL;
|
414
|
+
}
|
415
|
+
|
416
|
+
if(!msgpack_unpacker_init(mpac, initial_buffer_size)) {
|
417
|
+
free(mpac);
|
418
|
+
return NULL;
|
419
|
+
}
|
420
|
+
|
421
|
+
return mpac;
|
422
|
+
}
|
423
|
+
|
424
|
+
void msgpack_unpacker_free(msgpack_unpacker* mpac)
|
425
|
+
{
|
426
|
+
msgpack_unpacker_destroy(mpac);
|
427
|
+
free(mpac);
|
428
|
+
}
|
429
|
+
|
430
|
+
bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size)
|
431
|
+
{
|
432
|
+
if(mpac->used == mpac->off && get_count(mpac->buffer) == 1
|
433
|
+
&& !CTX_REFERENCED(mpac)) {
|
434
|
+
// rewind buffer
|
435
|
+
mpac->free += mpac->used - COUNTER_SIZE;
|
436
|
+
mpac->used = COUNTER_SIZE;
|
437
|
+
mpac->off = COUNTER_SIZE;
|
438
|
+
|
439
|
+
if(mpac->free >= size) {
|
440
|
+
return true;
|
441
|
+
}
|
442
|
+
}
|
443
|
+
|
444
|
+
if(mpac->off == COUNTER_SIZE) {
|
445
|
+
char* tmp;
|
446
|
+
size_t next_size = (mpac->used + mpac->free) * 2; // include COUNTER_SIZE
|
447
|
+
while(next_size < size + mpac->used) {
|
448
|
+
size_t tmp_next_size = next_size * 2;
|
449
|
+
if (tmp_next_size <= next_size) {
|
450
|
+
next_size = size + mpac->used;
|
451
|
+
break;
|
452
|
+
}
|
453
|
+
next_size = tmp_next_size;
|
454
|
+
}
|
455
|
+
|
456
|
+
tmp = (char*)realloc(mpac->buffer, next_size);
|
457
|
+
if(tmp == NULL) {
|
458
|
+
return false;
|
459
|
+
}
|
460
|
+
|
461
|
+
mpac->buffer = tmp;
|
462
|
+
mpac->free = next_size - mpac->used;
|
463
|
+
|
464
|
+
} else {
|
465
|
+
char* tmp;
|
466
|
+
size_t next_size = mpac->initial_buffer_size; // include COUNTER_SIZE
|
467
|
+
size_t not_parsed = mpac->used - mpac->off;
|
468
|
+
while(next_size < size + not_parsed + COUNTER_SIZE) {
|
469
|
+
size_t tmp_next_size = next_size * 2;
|
470
|
+
if (tmp_next_size <= next_size) {
|
471
|
+
next_size = size + not_parsed + COUNTER_SIZE;
|
472
|
+
break;
|
473
|
+
}
|
474
|
+
next_size = tmp_next_size;
|
475
|
+
}
|
476
|
+
|
477
|
+
tmp = (char*)malloc(next_size);
|
478
|
+
if(tmp == NULL) {
|
479
|
+
return false;
|
480
|
+
}
|
481
|
+
|
482
|
+
init_count(tmp);
|
483
|
+
|
484
|
+
memcpy(tmp+COUNTER_SIZE, mpac->buffer+mpac->off, not_parsed);
|
485
|
+
|
486
|
+
if(CTX_REFERENCED(mpac)) {
|
487
|
+
if(!msgpack_zone_push_finalizer(mpac->z, decr_count, mpac->buffer)) {
|
488
|
+
free(tmp);
|
489
|
+
return false;
|
490
|
+
}
|
491
|
+
CTX_REFERENCED(mpac) = false;
|
492
|
+
} else {
|
493
|
+
decr_count(mpac->buffer);
|
494
|
+
}
|
495
|
+
|
496
|
+
mpac->buffer = tmp;
|
497
|
+
mpac->used = not_parsed + COUNTER_SIZE;
|
498
|
+
mpac->free = next_size - mpac->used;
|
499
|
+
mpac->off = COUNTER_SIZE;
|
500
|
+
}
|
501
|
+
|
502
|
+
return true;
|
503
|
+
}
|
504
|
+
|
505
|
+
int msgpack_unpacker_execute(msgpack_unpacker* mpac)
|
506
|
+
{
|
507
|
+
size_t off = mpac->off;
|
508
|
+
int ret = template_execute(CTX_CAST(mpac->ctx),
|
509
|
+
mpac->buffer, mpac->used, &mpac->off);
|
510
|
+
if(mpac->off > off) {
|
511
|
+
mpac->parsed += mpac->off - off;
|
512
|
+
}
|
513
|
+
return ret;
|
514
|
+
}
|
515
|
+
|
516
|
+
msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac)
|
517
|
+
{
|
518
|
+
return template_data(CTX_CAST(mpac->ctx));
|
519
|
+
}
|
520
|
+
|
521
|
+
msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac)
|
522
|
+
{
|
523
|
+
msgpack_zone* old = mpac->z;
|
524
|
+
|
525
|
+
if (old == NULL) return NULL;
|
526
|
+
if(!msgpack_unpacker_flush_zone(mpac)) {
|
527
|
+
return NULL;
|
528
|
+
}
|
529
|
+
|
530
|
+
mpac->z = NULL;
|
531
|
+
CTX_CAST(mpac->ctx)->user.z = &mpac->z;
|
532
|
+
|
533
|
+
return old;
|
534
|
+
}
|
535
|
+
|
536
|
+
void msgpack_unpacker_reset_zone(msgpack_unpacker* mpac)
|
537
|
+
{
|
538
|
+
msgpack_zone_clear(mpac->z);
|
539
|
+
}
|
540
|
+
|
541
|
+
bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac)
|
542
|
+
{
|
543
|
+
if(CTX_REFERENCED(mpac)) {
|
544
|
+
if(!msgpack_zone_push_finalizer(mpac->z, decr_count, mpac->buffer)) {
|
545
|
+
return false;
|
546
|
+
}
|
547
|
+
CTX_REFERENCED(mpac) = false;
|
548
|
+
|
549
|
+
incr_count(mpac->buffer);
|
550
|
+
}
|
551
|
+
|
552
|
+
return true;
|
553
|
+
}
|
554
|
+
|
555
|
+
void msgpack_unpacker_reset(msgpack_unpacker* mpac)
|
556
|
+
{
|
557
|
+
template_init(CTX_CAST(mpac->ctx));
|
558
|
+
// don't reset referenced flag
|
559
|
+
mpac->parsed = 0;
|
560
|
+
}
|
561
|
+
|
562
|
+
static inline msgpack_unpack_return unpacker_next(msgpack_unpacker* mpac,
|
563
|
+
msgpack_unpacked* result)
|
564
|
+
{
|
565
|
+
int ret;
|
566
|
+
|
567
|
+
msgpack_unpacked_destroy(result);
|
568
|
+
|
569
|
+
ret = msgpack_unpacker_execute(mpac);
|
570
|
+
|
571
|
+
if(ret < 0) {
|
572
|
+
result->zone = NULL;
|
573
|
+
memset(&result->data, 0, sizeof(msgpack_object));
|
574
|
+
return (msgpack_unpack_return)ret;
|
575
|
+
}
|
576
|
+
|
577
|
+
if(ret == 0) {
|
578
|
+
return MSGPACK_UNPACK_CONTINUE;
|
579
|
+
}
|
580
|
+
result->zone = msgpack_unpacker_release_zone(mpac);
|
581
|
+
result->data = msgpack_unpacker_data(mpac);
|
582
|
+
|
583
|
+
return MSGPACK_UNPACK_SUCCESS;
|
584
|
+
}
|
585
|
+
|
586
|
+
msgpack_unpack_return msgpack_unpacker_next(msgpack_unpacker* mpac,
|
587
|
+
msgpack_unpacked* result)
|
588
|
+
{
|
589
|
+
msgpack_unpack_return ret;
|
590
|
+
|
591
|
+
ret = unpacker_next(mpac, result);
|
592
|
+
if (ret == MSGPACK_UNPACK_SUCCESS) {
|
593
|
+
msgpack_unpacker_reset(mpac);
|
594
|
+
}
|
595
|
+
|
596
|
+
return ret;
|
597
|
+
}
|
598
|
+
|
599
|
+
msgpack_unpack_return
|
600
|
+
msgpack_unpacker_next_with_size(msgpack_unpacker* mpac,
|
601
|
+
msgpack_unpacked* result, size_t *p_bytes)
|
602
|
+
{
|
603
|
+
msgpack_unpack_return ret;
|
604
|
+
|
605
|
+
ret = unpacker_next(mpac, result);
|
606
|
+
if (ret == MSGPACK_UNPACK_SUCCESS || ret == MSGPACK_UNPACK_CONTINUE) {
|
607
|
+
*p_bytes = mpac->parsed;
|
608
|
+
}
|
609
|
+
|
610
|
+
if (ret == MSGPACK_UNPACK_SUCCESS) {
|
611
|
+
msgpack_unpacker_reset(mpac);
|
612
|
+
}
|
613
|
+
|
614
|
+
return ret;
|
615
|
+
}
|
616
|
+
|
617
|
+
msgpack_unpack_return
|
618
|
+
msgpack_unpack(const char* data, size_t len, size_t* off,
|
619
|
+
msgpack_zone* result_zone, msgpack_object* result)
|
620
|
+
{
|
621
|
+
size_t noff = 0;
|
622
|
+
if(off != NULL) { noff = *off; }
|
623
|
+
|
624
|
+
if(len <= noff) {
|
625
|
+
// FIXME
|
626
|
+
return MSGPACK_UNPACK_CONTINUE;
|
627
|
+
}
|
628
|
+
else {
|
629
|
+
int e;
|
630
|
+
template_context ctx;
|
631
|
+
template_init(&ctx);
|
632
|
+
|
633
|
+
ctx.user.z = &result_zone;
|
634
|
+
ctx.user.referenced = false;
|
635
|
+
|
636
|
+
e = template_execute(&ctx, data, len, &noff);
|
637
|
+
if(e < 0) {
|
638
|
+
return (msgpack_unpack_return)e;
|
639
|
+
}
|
640
|
+
|
641
|
+
if(off != NULL) { *off = noff; }
|
642
|
+
|
643
|
+
if(e == 0) {
|
644
|
+
return MSGPACK_UNPACK_CONTINUE;
|
645
|
+
}
|
646
|
+
|
647
|
+
*result = template_data(&ctx);
|
648
|
+
|
649
|
+
if(noff < len) {
|
650
|
+
return MSGPACK_UNPACK_EXTRA_BYTES;
|
651
|
+
}
|
652
|
+
|
653
|
+
return MSGPACK_UNPACK_SUCCESS;
|
654
|
+
}
|
655
|
+
}
|
656
|
+
|
657
|
+
msgpack_unpack_return
|
658
|
+
msgpack_unpack_next(msgpack_unpacked* result,
|
659
|
+
const char* data, size_t len, size_t* off)
|
660
|
+
{
|
661
|
+
size_t noff = 0;
|
662
|
+
msgpack_unpacked_destroy(result);
|
663
|
+
|
664
|
+
if(off != NULL) { noff = *off; }
|
665
|
+
|
666
|
+
if(len <= noff) {
|
667
|
+
return MSGPACK_UNPACK_CONTINUE;
|
668
|
+
}
|
669
|
+
|
670
|
+
{
|
671
|
+
int e;
|
672
|
+
template_context ctx;
|
673
|
+
template_init(&ctx);
|
674
|
+
|
675
|
+
ctx.user.z = &result->zone;
|
676
|
+
ctx.user.referenced = false;
|
677
|
+
|
678
|
+
e = template_execute(&ctx, data, len, &noff);
|
679
|
+
|
680
|
+
if(off != NULL) { *off = noff; }
|
681
|
+
|
682
|
+
if(e < 0) {
|
683
|
+
msgpack_zone_free(result->zone);
|
684
|
+
result->zone = NULL;
|
685
|
+
return (msgpack_unpack_return)e;
|
686
|
+
}
|
687
|
+
|
688
|
+
if(e == 0) {
|
689
|
+
return MSGPACK_UNPACK_CONTINUE;
|
690
|
+
}
|
691
|
+
|
692
|
+
result->data = template_data(&ctx);
|
693
|
+
|
694
|
+
return MSGPACK_UNPACK_SUCCESS;
|
695
|
+
}
|
696
|
+
}
|
697
|
+
|
698
|
+
#if defined(MSGPACK_OLD_COMPILER_BUS_ERROR_WORKAROUND)
|
699
|
+
// FIXME: Dirty hack to avoid a bus error caused by OS X's old gcc.
|
700
|
+
static void dummy_function_to_avoid_bus_error()
|
701
|
+
{
|
702
|
+
}
|
703
|
+
#endif
|