Debian Codegraph @

msgpack/unpack_template.h

raw
1 /*
2 * MessagePack unpacking routine template
3 *
4 * Copyright (C) 2008-2010 FURUHASHI Sadayuki
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #ifndef USE_CASE_RANGE
20 #if !defined(_MSC_VER)
21 #define USE_CASE_RANGE
22 #endif
23 #endif
24
25 typedef struct unpack_stack {
26 PyObject* obj;
27 Py_ssize_t size;
28 Py_ssize_t count;
29 unsigned int ct;
30 PyObject* map_key;
31 } unpack_stack;
32
33 struct unpack_context {
34 unpack_user user;
35 unsigned int cs;
36 unsigned int trail;
37 unsigned int top;
38 /*
39 unpack_stack* stack;
40 unsigned int stack_size;
41 unpack_stack embed_stack[MSGPACK_EMBED_STACK_SIZE];
42 */
43 unpack_stack stack[MSGPACK_EMBED_STACK_SIZE];
44 };
45
46
47 static inline void unpack_init(unpack_context* ctx)
48 {
49 ctx->cs = CS_HEADER;
50 ctx->trail = 0;
51 ctx->top = 0;
52 /*
53 ctx->stack = ctx->embed_stack;
54 ctx->stack_size = MSGPACK_EMBED_STACK_SIZE;
55 */
56 ctx->stack[0].obj = unpack_callback_root(&ctx->user);
57 }
58
59 /*
60 static inline void unpack_destroy(unpack_context* ctx)
61 {
62 if(ctx->stack_size != MSGPACK_EMBED_STACK_SIZE) {
63 free(ctx->stack);
64 }
65 }
66 */
67
68 static inline PyObject* unpack_data(unpack_context* ctx)
69 {
70 return (ctx)->stack[0].obj;
71 }
72
73 static inline void unpack_clear(unpack_context *ctx)
74 {
75 for (unsigned int i = 0; i < ctx->top; i++) {
76 /* map_key holds a live reference only while waiting for the value */
77 if (ctx->stack[i].ct == CT_MAP_VALUE) {
78 Py_CLEAR(ctx->stack[i].map_key);
79 }
80 Py_CLEAR(ctx->stack[i].obj);
81 }
82 unpack_init(ctx);
83 }
84
85 static inline int unpack_execute(bool construct, unpack_context* ctx, const char* data, Py_ssize_t len, Py_ssize_t* off)
86 {
87 assert(len >= *off);
88
89 const unsigned char* p = (unsigned char*)data + *off;
90 const unsigned char* const pe = (unsigned char*)data + len;
91 const void* n = p;
92
93 unsigned int trail = ctx->trail;
94 unsigned int cs = ctx->cs;
95 unsigned int top = ctx->top;
96 unpack_stack* stack = ctx->stack;
97 /*
98 unsigned int stack_size = ctx->stack_size;
99 */
100 unpack_user* user = &ctx->user;
101
102 PyObject* obj = NULL;
103 unpack_stack* c = NULL;
104
105 int ret;
106
107 #define construct_cb(name) \
108 construct && unpack_callback ## name
109
110 #define push_simple_value(func) \
111 if(construct_cb(func)(user, &obj) < 0) { goto _failed; } \
112 goto _push
113 #define push_fixed_value(func, arg) \
114 if(construct_cb(func)(user, arg, &obj) < 0) { goto _failed; } \
115 goto _push
116 #define push_variable_value(func, base, pos, len) \
117 if(construct_cb(func)(user, \
118 (const char*)base, (const char*)pos, len, &obj) < 0) { goto _failed; } \
119 goto _push
120
121 #define again_fixed_trail(_cs, trail_len) \
122 trail = trail_len; \
123 cs = _cs; \
124 goto _fixed_trail_again
125 #define again_fixed_trail_if_zero(_cs, trail_len, ifzero) \
126 trail = trail_len; \
127 if(trail == 0) { goto ifzero; } \
128 cs = _cs; \
129 goto _fixed_trail_again
130
131 #define start_container(func, count_, ct_) \
132 if(top >= MSGPACK_EMBED_STACK_SIZE) { ret = -3; goto _end; } \
133 if(construct_cb(func)(user, count_, &stack[top].obj) < 0) { goto _failed; } \
134 if((count_) == 0) { obj = stack[top].obj; \
135 if (construct_cb(func##_end)(user, &obj) < 0) { goto _failed; } \
136 goto _push; } \
137 stack[top].ct = ct_; \
138 stack[top].size = count_; \
139 stack[top].count = 0; \
140 ++top; \
141 goto _header_again
142
143 #define NEXT_CS(p) ((unsigned int)*p & 0x1f)
144
145 #ifdef USE_CASE_RANGE
146 #define SWITCH_RANGE_BEGIN switch(*p) {
147 #define SWITCH_RANGE(FROM, TO) case FROM ... TO:
148 #define SWITCH_RANGE_DEFAULT default:
149 #define SWITCH_RANGE_END }
150 #else
151 #define SWITCH_RANGE_BEGIN { if(0) {
152 #define SWITCH_RANGE(FROM, TO) } else if(FROM <= *p && *p <= TO) {
153 #define SWITCH_RANGE_DEFAULT } else {
154 #define SWITCH_RANGE_END } }
155 #endif
156
157 if(p == pe) { goto _out; }
158 do {
159 switch(cs) {
160 case CS_HEADER:
161 SWITCH_RANGE_BEGIN
162 SWITCH_RANGE(0x00, 0x7f) // Positive Fixnum
163 push_fixed_value(_uint8, *(uint8_t*)p);
164 SWITCH_RANGE(0xe0, 0xff) // Negative Fixnum
165 push_fixed_value(_int8, *(int8_t*)p);
166 SWITCH_RANGE(0xc0, 0xdf) // Variable
167 switch(*p) {
168 case 0xc0: // nil
169 push_simple_value(_nil);
170 //case 0xc1: // never used
171 case 0xc2: // false
172 push_simple_value(_false);
173 case 0xc3: // true
174 push_simple_value(_true);
175 case 0xc4: // bin 8
176 again_fixed_trail(NEXT_CS(p), 1);
177 case 0xc5: // bin 16
178 again_fixed_trail(NEXT_CS(p), 2);
179 case 0xc6: // bin 32
180 again_fixed_trail(NEXT_CS(p), 4);
181 case 0xc7: // ext 8
182 again_fixed_trail(NEXT_CS(p), 1);
183 case 0xc8: // ext 16
184 again_fixed_trail(NEXT_CS(p), 2);
185 case 0xc9: // ext 32
186 again_fixed_trail(NEXT_CS(p), 4);
187 case 0xca: // float
188 case 0xcb: // double
189 case 0xcc: // unsigned int 8
190 case 0xcd: // unsigned int 16
191 case 0xce: // unsigned int 32
192 case 0xcf: // unsigned int 64
193 case 0xd0: // signed int 8
194 case 0xd1: // signed int 16
195 case 0xd2: // signed int 32
196 case 0xd3: // signed int 64
197 again_fixed_trail(NEXT_CS(p), 1 << (((unsigned int)*p) & 0x03));
198 case 0xd4: // fixext 1
199 case 0xd5: // fixext 2
200 case 0xd6: // fixext 4
201 case 0xd7: // fixext 8
202 again_fixed_trail_if_zero(ACS_EXT_VALUE,
203 (1 << (((unsigned int)*p) & 0x03))+1,
204 _ext_zero);
205 case 0xd8: // fixext 16
206 again_fixed_trail_if_zero(ACS_EXT_VALUE, 16+1, _ext_zero);
207 case 0xd9: // str 8
208 again_fixed_trail(NEXT_CS(p), 1);
209 case 0xda: // raw 16
210 case 0xdb: // raw 32
211 case 0xdc: // array 16
212 case 0xdd: // array 32
213 case 0xde: // map 16
214 case 0xdf: // map 32
215 again_fixed_trail(NEXT_CS(p), 2 << (((unsigned int)*p) & 0x01));
216 default:
217 ret = -2;
218 goto _end;
219 }
220 SWITCH_RANGE(0xa0, 0xbf) // FixRaw
221 again_fixed_trail_if_zero(ACS_RAW_VALUE, ((unsigned int)*p & 0x1f), _raw_zero);
222 SWITCH_RANGE(0x90, 0x9f) // FixArray
223 start_container(_array, ((unsigned int)*p) & 0x0f, CT_ARRAY_ITEM);
224 SWITCH_RANGE(0x80, 0x8f) // FixMap
225 start_container(_map, ((unsigned int)*p) & 0x0f, CT_MAP_KEY);
226
227 SWITCH_RANGE_DEFAULT
228 ret = -2;
229 goto _end;
230 SWITCH_RANGE_END
231 // end CS_HEADER
232
233
234 _fixed_trail_again:
235 ++p;
236
237 default:
238 if((size_t)(pe - p) < trail) { goto _out; }
239 n = p; p += trail - 1;
240 switch(cs) {
241 case CS_EXT_8:
242 again_fixed_trail_if_zero(ACS_EXT_VALUE, *(uint8_t*)n+1, _ext_zero);
243 case CS_EXT_16:
244 again_fixed_trail_if_zero(ACS_EXT_VALUE,
245 _msgpack_load16(uint16_t,n)+1,
246 _ext_zero);
247 case CS_EXT_32:
248 again_fixed_trail_if_zero(ACS_EXT_VALUE,
249 _msgpack_load32(uint32_t,n)+1,
250 _ext_zero);
251 case CS_FLOAT: {
252 double f;
253 #if PY_VERSION_HEX >= 0x030B00A7
254 f = PyFloat_Unpack4((const char*)n, 0);
255 #else
256 f = _PyFloat_Unpack4((unsigned char*)n, 0);
257 #endif
258 push_fixed_value(_float, f); }
259 case CS_DOUBLE: {
260 double f;
261 #if PY_VERSION_HEX >= 0x030B00A7
262 f = PyFloat_Unpack8((const char*)n, 0);
263 #else
264 f = _PyFloat_Unpack8((unsigned char*)n, 0);
265 #endif
266 push_fixed_value(_double, f); }
267 case CS_UINT_8:
268 push_fixed_value(_uint8, *(uint8_t*)n);
269 case CS_UINT_16:
270 push_fixed_value(_uint16, _msgpack_load16(uint16_t,n));
271 case CS_UINT_32:
272 push_fixed_value(_uint32, _msgpack_load32(uint32_t,n));
273 case CS_UINT_64:
274 push_fixed_value(_uint64, _msgpack_load64(uint64_t,n));
275
276 case CS_INT_8:
277 push_fixed_value(_int8, *(int8_t*)n);
278 case CS_INT_16:
279 push_fixed_value(_int16, _msgpack_load16(int16_t,n));
280 case CS_INT_32:
281 push_fixed_value(_int32, _msgpack_load32(int32_t,n));
282 case CS_INT_64:
283 push_fixed_value(_int64, _msgpack_load64(int64_t,n));
284
285 case CS_BIN_8:
286 again_fixed_trail_if_zero(ACS_BIN_VALUE, *(uint8_t*)n, _bin_zero);
287 case CS_BIN_16:
288 again_fixed_trail_if_zero(ACS_BIN_VALUE, _msgpack_load16(uint16_t,n), _bin_zero);
289 case CS_BIN_32:
290 again_fixed_trail_if_zero(ACS_BIN_VALUE, _msgpack_load32(uint32_t,n), _bin_zero);
291 case ACS_BIN_VALUE:
292 _bin_zero:
293 push_variable_value(_bin, data, n, trail);
294
295 case CS_RAW_8:
296 again_fixed_trail_if_zero(ACS_RAW_VALUE, *(uint8_t*)n, _raw_zero);
297 case CS_RAW_16:
298 again_fixed_trail_if_zero(ACS_RAW_VALUE, _msgpack_load16(uint16_t,n), _raw_zero);
299 case CS_RAW_32:
300 again_fixed_trail_if_zero(ACS_RAW_VALUE, _msgpack_load32(uint32_t,n), _raw_zero);
301 case ACS_RAW_VALUE:
302 _raw_zero:
303 push_variable_value(_raw, data, n, trail);
304
305 case ACS_EXT_VALUE:
306 _ext_zero:
307 push_variable_value(_ext, data, n, trail);
308
309 case CS_ARRAY_16:
310 start_container(_array, _msgpack_load16(uint16_t,n), CT_ARRAY_ITEM);
311 case CS_ARRAY_32:
312 /* FIXME security guard */
313 start_container(_array, _msgpack_load32(uint32_t,n), CT_ARRAY_ITEM);
314
315 case CS_MAP_16:
316 start_container(_map, _msgpack_load16(uint16_t,n), CT_MAP_KEY);
317 case CS_MAP_32:
318 /* FIXME security guard */
319 start_container(_map, _msgpack_load32(uint32_t,n), CT_MAP_KEY);
320
321 default:
322 goto _failed;
323 }
324 }
325
326 _push:
327 if(top == 0) { goto _finish; }
328 c = &stack[top-1];
329 switch(c->ct) {
330 case CT_ARRAY_ITEM:
331 if(construct_cb(_array_item)(user, c->count, &c->obj, obj) < 0) { goto _failed; }
332 if(++c->count == c->size) {
333 obj = c->obj;
334 if (construct_cb(_array_end)(user, &obj) < 0) { goto _failed; }
335 --top;
336 /*printf("stack pop %d\n", top);*/
337 goto _push;
338 }
339 goto _header_again;
340 case CT_MAP_KEY:
341 c->map_key = obj;
342 c->ct = CT_MAP_VALUE;
343 goto _header_again;
344 case CT_MAP_VALUE:
345 if(construct_cb(_map_item)(user, c->count, &c->obj, c->map_key, obj) < 0) { goto _failed; }
346 c->map_key = NULL;
347 if(++c->count == c->size) {
348 obj = c->obj;
349 if (construct_cb(_map_end)(user, &obj) < 0) { goto _failed; }
350 --top;
351 /*printf("stack pop %d\n", top);*/
352 goto _push;
353 }
354 c->ct = CT_MAP_KEY;
355 goto _header_again;
356
357 default:
358 goto _failed;
359 }
360
361 _header_again:
362 cs = CS_HEADER;
363 ++p;
364 } while(p != pe);
365 goto _out;
366
367
368 _finish:
369 if (!construct)
370 unpack_callback_nil(user, &obj);
371 stack[0].obj = obj;
372 ++p;
373 ret = 1;
374 /*printf("-- finish --\n"); */
375 goto _end;
376
377 _failed:
378 /*printf("** FAILED **\n"); */
379 ret = -1;
380 goto _end;
381
382 _out:
383 ret = 0;
384 goto _end;
385
386 _end:
387 ctx->cs = cs;
388 ctx->trail = trail;
389 ctx->top = top;
390 *off = p - (const unsigned char*)data;
391
392 return ret;
393 #undef construct_cb
394 }
395
396 #undef NEXT_CS
397 #undef SWITCH_RANGE_BEGIN
398 #undef SWITCH_RANGE
399 #undef SWITCH_RANGE_DEFAULT
400 #undef SWITCH_RANGE_END
401 #undef push_simple_value
402 #undef push_fixed_value
403 #undef push_variable_value
404 #undef again_fixed_trail
405 #undef again_fixed_trail_if_zero
406 #undef start_container
407
408 static int unpack_construct(unpack_context *ctx, const char *data, Py_ssize_t len, Py_ssize_t *off) {
409 int ret = unpack_execute(1, ctx, data, len, off);
410 if (ret == -1) {
411 unpack_clear(ctx);
412 }
413 return ret;
414 }
415 static int unpack_skip(unpack_context *ctx, const char *data, Py_ssize_t len, Py_ssize_t *off) {
416 int ret = unpack_execute(0, ctx, data, len, off);
417 if (ret == -1) {
418 unpack_clear(ctx);
419 }
420 return ret;
421 }
422
423 #define unpack_container_header read_array_header
424 #define fixed_offset 0x90
425 #define var_offset 0xdc
426 #include "unpack_container_header.h"
427 #undef unpack_container_header
428 #undef fixed_offset
429 #undef var_offset
430
431 #define unpack_container_header read_map_header
432 #define fixed_offset 0x80
433 #define var_offset 0xde
434 #include "unpack_container_header.h"
435 #undef unpack_container_header
436 #undef fixed_offset
437 #undef var_offset
438
439 /* vim: set ts=4 sw=4 sts=4 expandtab */