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
360
361
362
363
364
365
366
367
368
369
|
//
// MessagePack for C++ deserializing routine
//
// Copyright (C) 2008-2009 FURUHASHI Sadayuki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "msgpack/unpack.hpp"
#include "msgpack/unpack_define.h"
#include <stdlib.h>
namespace msgpack {
//namespace {
struct unpack_user {
zone* z;
bool referenced;
};
//} // noname namespace
#define msgpack_unpack_struct(name) \
struct msgpack_unpacker ## name
#define msgpack_unpack_func(ret, name) \
ret msgpack_unpacker ## name
#define msgpack_unpack_callback(name) \
msgpack_unpack ## name
#define msgpack_unpack_object object
#define msgpack_unpack_user unpack_user
struct msgpack_unpacker_context;
static void msgpack_unpacker_init(struct msgpack_unpacker_context* ctx);
static object msgpack_unpacker_data(struct msgpack_unpacker_context* ctx);
static int msgpack_unpacker_execute(struct msgpack_unpacker_context* ctx,
const char* data, size_t len, size_t* off);
static inline object msgpack_unpack_init(unpack_user* u)
{ return object(); }
static inline object msgpack_unpack_uint8(unpack_user* u, uint8_t d)
{ object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; }
static inline object msgpack_unpack_uint16(unpack_user* u, uint16_t d)
{ object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; }
static inline object msgpack_unpack_uint32(unpack_user* u, uint32_t d)
{ object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; }
static inline object msgpack_unpack_uint64(unpack_user* u, uint64_t d)
{ object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; }
static inline object msgpack_unpack_int8(unpack_user* u, int8_t d)
{ if(d >= 0) { object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; }
else { object o; o.type = type::NEGATIVE_INTEGER; o.via.i64 = d; return o; } }
static inline object msgpack_unpack_int16(unpack_user* u, int16_t d)
{ if(d >= 0) { object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; }
else { object o; o.type = type::NEGATIVE_INTEGER; o.via.i64 = d; return o; } }
static inline object msgpack_unpack_int32(unpack_user* u, int32_t d)
{ if(d >= 0) { object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; }
else { object o; o.type = type::NEGATIVE_INTEGER; o.via.i64 = d; return o; } }
static inline object msgpack_unpack_int64(unpack_user* u, int64_t d)
{ if(d >= 0) { object o; o.type = type::POSITIVE_INTEGER; o.via.u64 = d; return o; }
else { object o; o.type = type::NEGATIVE_INTEGER; o.via.i64 = d; return o; } }
static inline object msgpack_unpack_float(unpack_user* u, float d)
{ object o; o.type = type::DOUBLE; o.via.dec = d; return o; }
static inline object msgpack_unpack_double(unpack_user* u, double d)
{ object o; o.type = type::DOUBLE; o.via.dec = d; return o; }
static inline object msgpack_unpack_nil(unpack_user* u)
{ object o; o.type = type::NIL; return o; }
static inline object msgpack_unpack_true(unpack_user* u)
{ object o; o.type = type::BOOLEAN; o.via.boolean = true; return o; }
static inline object msgpack_unpack_false(unpack_user* u)
{ object o; o.type = type::BOOLEAN; o.via.boolean = false; return o; }
static inline object msgpack_unpack_array(unpack_user* u, unsigned int n)
{
object o;
o.type = type::ARRAY;
o.via.container.size = 0;
o.via.container.ptr = (object*)u->z->malloc(n*sizeof(object));
return o;
}
static inline void msgpack_unpack_array_item(unpack_user* u, object* c, object o)
{ c->via.container.ptr[ c->via.container.size++ ] = o; }
static inline object msgpack_unpack_map(unpack_user* u, unsigned int n)
{
object o;
o.type = type::MAP;
o.via.container.size = 0;
o.via.container.ptr = (object*)u->z->malloc(n*2*sizeof(object));
return o;
}
static inline void msgpack_unpack_map_item(unpack_user* u, object* c, object k, object v)
{
c->via.container.ptr[ c->via.container.size ] = k;
c->via.container.ptr[ c->via.container.size+1 ] = v;
++c->via.container.size;
}
static inline object msgpack_unpack_raw(unpack_user* u, const char* b, const char* p, unsigned int l)
{
object o;
o.type = type::RAW;
o.via.ref.ptr = p;
o.via.ref.size = l;
u->referenced = true;
return o;
}
#include "msgpack/unpack_template.h"
namespace {
struct context {
context()
{
msgpack_unpacker_init(&m_ctx);
unpack_user u = {NULL, false};
m_ctx.user = u;
}
~context() { }
int execute(const char* data, size_t len, size_t* off)
{
return msgpack_unpacker_execute(&m_ctx, data, len, off);
}
object data()
{
return msgpack_unpacker_data(&m_ctx);
}
void reset()
{
zone* z = m_ctx.user.z;
msgpack_unpacker_init(&m_ctx);
unpack_user u = {z, false};
m_ctx.user = u;
}
void set_zone(zone* z)
{
m_ctx.user.z = z;
}
bool is_referenced() const
{
return m_ctx.user.referenced;
}
private:
msgpack_unpacker_context m_ctx;
zone* m_zone;
private:
context(const context&);
};
static inline context* as_ctx(void* m)
{
return reinterpret_cast<context*>(m);
}
static const size_t COUNTER_SIZE = sizeof(unsigned int);
static inline void init_count(void* buffer)
{
*(volatile unsigned int*)buffer = 1;
}
static inline void decl_count(void* buffer)
{
//if(--*(unsigned int*)buffer == 0) {
if(__sync_sub_and_fetch((unsigned int*)buffer, 1) == 0) {
free(buffer);
}
}
static inline void incr_count(void* buffer)
{
//++*(unsigned int*)buffer;
__sync_add_and_fetch((unsigned int*)buffer, 1);
}
static inline unsigned int get_count(void* buffer)
{
return *(volatile unsigned int*)buffer;
}
} // noname namespace
unpacker::unpacker(size_t initial_buffer_size) :
m_buffer(NULL),
m_used(0),
m_free(0),
m_off(0),
m_zone(new zone()),
m_ctx(new context()),
m_initial_buffer_size(initial_buffer_size)
{
if(m_initial_buffer_size < COUNTER_SIZE) {
m_initial_buffer_size = COUNTER_SIZE;
}
as_ctx(m_ctx)->set_zone(m_zone.get());
m_buffer = (char*)::malloc(m_initial_buffer_size);
if(!m_buffer) { throw std::bad_alloc(); }
init_count(m_buffer);
m_used = COUNTER_SIZE;
m_free = m_initial_buffer_size - m_used;
m_off = COUNTER_SIZE;
}
unpacker::~unpacker()
{
delete as_ctx(m_ctx);
decl_count(m_buffer);
}
void unpacker::expand_buffer(size_t len)
{
if(m_used == m_off && get_count(m_buffer) == 1 &&
!as_ctx(m_ctx)->is_referenced()) {
// rewind buffer
m_free += m_used - COUNTER_SIZE;
m_used = COUNTER_SIZE;
m_off = COUNTER_SIZE;
if(m_free >= len) { return; }
}
if(m_off == COUNTER_SIZE) {
size_t next_size = (m_used + m_free) * 2;
while(next_size < len + m_used) { next_size *= 2; }
char* tmp = (char*)::realloc(m_buffer, next_size);
if(!tmp) { throw std::bad_alloc(); }
m_buffer = tmp;
m_free = next_size - m_used;
} else {
size_t next_size = m_initial_buffer_size; // include COUNTER_SIZE
size_t not_parsed = m_used - m_off;
while(next_size < len + not_parsed + COUNTER_SIZE) { next_size *= 2; }
char* tmp = (char*)::malloc(next_size);
if(!tmp) { throw std::bad_alloc(); }
init_count(tmp);
try {
m_zone->push_finalizer(decl_count, m_buffer);
} catch (...) { free(tmp); throw; }
memcpy(tmp+COUNTER_SIZE, m_buffer+m_off, not_parsed);
m_buffer = tmp;
m_used = not_parsed + COUNTER_SIZE;
m_free = next_size - m_used;
m_off = COUNTER_SIZE;
}
}
bool unpacker::execute()
{
int ret = as_ctx(m_ctx)->execute(m_buffer, m_used, &m_off);
if(ret < 0) {
throw unpack_error("parse error");
} else if(ret == 0) {
return false;
} else {
return true;
}
}
zone* unpacker::release_zone()
{
m_zone->push_finalizer(decl_count, m_buffer);
incr_count(m_buffer);
//std::auto_ptr<zone> old(new zone());
//m_zone.swap(old);
zone* n = new zone();
std::auto_ptr<zone> old(m_zone.release());
m_zone.reset(n);
as_ctx(m_ctx)->set_zone(m_zone.get());
return old.release();
}
object unpacker::data()
{
return as_ctx(m_ctx)->data();
}
void unpacker::reset()
{
if(!m_zone->empty()) { delete release_zone(); }
as_ctx(m_ctx)->reset();
}
object unpacker::unpack(const char* data, size_t len, zone& z, size_t* off)
{
context ctx;
ctx.set_zone(&z);
if(off) {
size_t noff = *off;
int ret = ctx.execute(data, len, &noff);
if(ret < 0) {
throw unpack_error("parse error");
} else if(ret == 0) {
throw unpack_error("insufficient bytes");
}
*off = noff;
} else {
size_t noff = 0;
int ret = ctx.execute(data, len, &noff);
if(ret < 0) {
throw unpack_error("parse error");
} else if(ret == 0) {
throw unpack_error("insufficient bytes");
} else if(noff < len) {
throw unpack_error("extra bytes");
}
}
return ctx.data();
}
} // namespace msgpack
|