msgpack 0.0.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,127 @@
1
+ /*
2
+ * MessagePack unpacking routine template
3
+ *
4
+ * Copyright (C) 2008 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
+ #ifndef MSGPACK_UNPACK_DEFINE_H__
19
+ #define MSGPACK_UNPACK_DEFINE_H__
20
+
21
+ #include <stddef.h>
22
+ #include <stdint.h>
23
+ #include <string.h>
24
+ #include <assert.h>
25
+ #include <arpa/inet.h>
26
+ #include <stdio.h>
27
+
28
+ #ifdef __cplusplus
29
+ extern "C" {
30
+ #endif
31
+
32
+
33
+ #ifndef MSGPACK_MAX_STACK_SIZE
34
+ #define MSGPACK_MAX_STACK_SIZE 16
35
+ #endif
36
+
37
+
38
+ #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
39
+ #if __BYTE_ORDER == __LITTLE_ENDIAN
40
+ #define __LITTLE_ENDIAN__
41
+ #elif __BYTE_ORDER == __BIG_ENDIAN
42
+ #define __BIG_ENDIAN__
43
+ #endif
44
+ #endif
45
+
46
+ #define msgpack_betoh16(x) ntohs(x)
47
+ #define msgpack_betoh32(x) ntohl(x)
48
+
49
+ #ifdef __LITTLE_ENDIAN__
50
+ #if defined(__bswap_64)
51
+ # define msgpack_betoh64(x) __bswap_64(x)
52
+ #elif defined(__DARWIN_OSSwapInt64)
53
+ # define msgpack_betoh64(x) __DARWIN_OSSwapInt64(x)
54
+ #else
55
+ static inline uint64_t msgpack_betoh64(uint64_t x) {
56
+ return ((x << 56) & 0xff00000000000000ULL ) |
57
+ ((x << 40) & 0x00ff000000000000ULL ) |
58
+ ((x << 24) & 0x0000ff0000000000ULL ) |
59
+ ((x << 8) & 0x000000ff00000000ULL ) |
60
+ ((x >> 8) & 0x00000000ff000000ULL ) |
61
+ ((x >> 24) & 0x0000000000ff0000ULL ) |
62
+ ((x >> 40) & 0x000000000000ff00ULL ) |
63
+ ((x >> 56) & 0x00000000000000ffULL ) ;
64
+ }
65
+ #endif
66
+ #else
67
+ #define msgpack_betoh64(x) (x)
68
+ #endif
69
+
70
+
71
+ typedef enum {
72
+ CS_HEADER = 0x00, // nil
73
+
74
+ //CS_ = 0x01,
75
+ //CS_ = 0x02, // false
76
+ //CS_ = 0x03, // true
77
+
78
+ //CS_ = 0x04,
79
+ //CS_ = 0x05,
80
+ //CS_ = 0x06,
81
+ //CS_ = 0x07,
82
+
83
+ //CS_ = 0x08,
84
+ //CS_ = 0x09,
85
+ CS_FLOAT = 0x0a,
86
+ CS_DOUBLE = 0x0b,
87
+ CS_UINT_8 = 0x0c,
88
+ CS_UINT_16 = 0x0d,
89
+ CS_UINT_32 = 0x0e,
90
+ CS_UINT_64 = 0x0f,
91
+ CS_INT_8 = 0x10,
92
+ CS_INT_16 = 0x11,
93
+ CS_INT_32 = 0x12,
94
+ CS_INT_64 = 0x13,
95
+
96
+ //CS_ = 0x14,
97
+ //CS_ = 0x15,
98
+ //CS_BIG_INT_16 = 0x16,
99
+ //CS_BIG_INT_32 = 0x17,
100
+ //CS_BIG_FLOAT_16 = 0x18,
101
+ //CS_BIG_FLOAT_32 = 0x19,
102
+ CS_RAW_16 = 0x1a,
103
+ CS_RAW_32 = 0x1b,
104
+ CS_ARRAY_16 = 0x1c,
105
+ CS_ARRAY_32 = 0x1d,
106
+ CS_MAP_16 = 0x1e,
107
+ CS_MAP_32 = 0x1f,
108
+
109
+ //ACS_BIG_INT_VALUE,
110
+ //ACS_BIG_FLOAT_VALUE,
111
+ ACS_RAW_VALUE,
112
+ } msgpack_unpack_state;
113
+
114
+
115
+ typedef enum {
116
+ CT_ARRAY_ITEM,
117
+ CT_MAP_KEY,
118
+ CT_MAP_VALUE,
119
+ } msgpack_container_type;
120
+
121
+
122
+ #ifdef __cplusplus
123
+ }
124
+ #endif
125
+
126
+ #endif /* msgpack/unpack_define.h */
127
+
@@ -0,0 +1,360 @@
1
+ /*
2
+ * MessagePack unpacking routine template
3
+ *
4
+ * Copyright (C) 2008 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 msgpack_unpack_func
20
+ #error msgpack_unpack_func template is not defined
21
+ #endif
22
+
23
+ #ifndef msgpack_unpack_callback
24
+ #error msgpack_unpack_callback template is not defined
25
+ #endif
26
+
27
+ #ifndef msgpack_unpack_struct
28
+ #error msgpack_unpack_struct template is not defined
29
+ #endif
30
+
31
+ #ifndef msgpack_unpack_struct_decl
32
+ #define msgpack_unpack_struct_decl(name) msgpack_unpack_struct(name)
33
+ #endif
34
+
35
+ #ifndef msgpack_unpack_object
36
+ #error msgpack_unpack_object type is not defined
37
+ #endif
38
+
39
+ #ifndef msgpack_unpack_user
40
+ #error msgpack_unpack_user type is not defined
41
+ #endif
42
+
43
+
44
+ msgpack_unpack_struct_decl(stack) {
45
+ msgpack_unpack_object obj;
46
+ size_t count;
47
+ unsigned int ct;
48
+ msgpack_unpack_object map_key;
49
+ };
50
+
51
+ msgpack_unpack_struct_decl(context) {
52
+ msgpack_unpack_user user; // must be first
53
+ unsigned int cs;
54
+ unsigned int trail;
55
+ unsigned int top;
56
+ msgpack_unpack_struct(stack) stack[MSGPACK_MAX_STACK_SIZE];
57
+ };
58
+
59
+
60
+ msgpack_unpack_func(void, init)(msgpack_unpack_struct(context)* ctx)
61
+ {
62
+ /*memset(ctx, 0, sizeof( msgpack_unpack_struct(context) )); FIXME needed? */
63
+ ctx->cs = CS_HEADER;
64
+ ctx->trail = 0;
65
+ ctx->top = 0;
66
+ ctx->stack[0].obj = msgpack_unpack_callback(init)(&ctx->user);
67
+ }
68
+
69
+ msgpack_unpack_func(msgpack_unpack_object, data)(msgpack_unpack_struct(context)* unpacker)
70
+ {
71
+ return (unpacker)->stack[0].obj;
72
+ }
73
+
74
+
75
+ msgpack_unpack_func(int, execute)(msgpack_unpack_struct(context)* ctx, const char* data, size_t len, size_t* off)
76
+ {
77
+ assert(len >= *off);
78
+
79
+ const unsigned char* p = (unsigned char*)data + *off;
80
+ const unsigned char* const pe = (unsigned char*)data + len;
81
+ const void* n = NULL;
82
+
83
+ unsigned int trail = ctx->trail;
84
+ unsigned int cs = ctx->cs;
85
+ unsigned int top = ctx->top;
86
+ msgpack_unpack_struct(stack)* stack = ctx->stack;
87
+ msgpack_unpack_user* user = &ctx->user;
88
+
89
+ msgpack_unpack_object obj;
90
+ msgpack_unpack_struct(stack)* c = NULL;
91
+
92
+ int ret;
93
+
94
+ #define push_simple_value(func) \
95
+ obj = msgpack_unpack_callback(func)(user); \
96
+ /*printf("obj %d\n",obj);*/ \
97
+ goto _push
98
+ #define push_fixed_value(func, arg) \
99
+ obj = msgpack_unpack_callback(func)(user, arg); \
100
+ /*printf("obj %d\n",obj);*/ \
101
+ goto _push
102
+ #define push_variable_value(func, base, pos, len) \
103
+ obj = msgpack_unpack_callback(func)(user, (const char*)base, (const char*)pos, len); \
104
+ /*printf("obj %d\n",obj);*/ \
105
+ goto _push
106
+
107
+ #define again_fixed_trail(_cs, trail_len) \
108
+ trail = trail_len; \
109
+ cs = _cs; \
110
+ goto _fixed_trail_again
111
+ #define again_fixed_trail_if_zero(_cs, trail_len, ifzero) \
112
+ trail = trail_len; \
113
+ if(trail == 0) { goto ifzero; } \
114
+ cs = _cs; \
115
+ goto _fixed_trail_again
116
+
117
+ #define start_container(func, count_, ct_) \
118
+ stack[top].obj = msgpack_unpack_callback(func)(user, count_); \
119
+ if((count_) == 0) { obj = stack[top].obj; goto _push; } \
120
+ if(top >= MSGPACK_MAX_STACK_SIZE) { goto _failed; } \
121
+ stack[top].ct = ct_; \
122
+ stack[top].count = count_; \
123
+ /*printf("container %d count %d stack %d\n",stack[top].obj,count_,top);*/ \
124
+ /*printf("stack push %d\n", top);*/ \
125
+ ++top; \
126
+ goto _header_again
127
+
128
+ #define NEXT_CS(p) \
129
+ ((unsigned int)*p & 0x1f)
130
+
131
+ #define PTR_CAST_8(ptr) (*(uint8_t*)ptr)
132
+ #define PTR_CAST_16(ptr) msgpack_betoh16(*(uint16_t*)ptr)
133
+ #define PTR_CAST_32(ptr) msgpack_betoh32(*(uint32_t*)ptr)
134
+ #define PTR_CAST_64(ptr) msgpack_betoh64(*(uint64_t*)ptr)
135
+
136
+ if(p == pe) { goto _out; }
137
+ do {
138
+ switch(cs) {
139
+ case CS_HEADER:
140
+ switch(*p) {
141
+ case 0x00 ... 0x7f: // Positive Fixnum
142
+ push_fixed_value(uint8, *(uint8_t*)p);
143
+ case 0xe0 ... 0xff: // Negative Fixnum
144
+ push_fixed_value(int8, *(int8_t*)p);
145
+ case 0xc0 ... 0xdf: // Variable
146
+ switch(*p) {
147
+ case 0xc0: // nil
148
+ push_simple_value(nil);
149
+ //case 0xc1: // string
150
+ // again_terminal_trail(NEXT_CS(p), p+1);
151
+ case 0xc2: // false
152
+ push_simple_value(false);
153
+ case 0xc3: // true
154
+ push_simple_value(true);
155
+ //case 0xc4:
156
+ //case 0xc5:
157
+ //case 0xc6:
158
+ //case 0xc7:
159
+ //case 0xc8:
160
+ //case 0xc9:
161
+ case 0xca: // float
162
+ case 0xcb: // double
163
+ case 0xcc: // unsigned int 8
164
+ case 0xcd: // unsigned int 16
165
+ case 0xce: // unsigned int 32
166
+ case 0xcf: // unsigned int 64
167
+ case 0xd0: // signed int 8
168
+ case 0xd1: // signed int 16
169
+ case 0xd2: // signed int 32
170
+ case 0xd3: // signed int 64
171
+ again_fixed_trail(NEXT_CS(p), 1 << (((unsigned int)*p) & 0x03));
172
+ //case 0xd4:
173
+ //case 0xd5:
174
+ //case 0xd6: // big integer 16
175
+ //case 0xd7: // big integer 32
176
+ //case 0xd8: // big float 16
177
+ //case 0xd9: // big float 32
178
+ case 0xda: // raw 16
179
+ case 0xdb: // raw 32
180
+ case 0xdc: // array 16
181
+ case 0xdd: // array 32
182
+ case 0xde: // map 16
183
+ case 0xdf: // map 32
184
+ again_fixed_trail(NEXT_CS(p), 2 << (((unsigned int)*p) & 0x01));
185
+ default:
186
+ goto _failed;
187
+ }
188
+ case 0xa0 ... 0xbf: // FixRaw
189
+ again_fixed_trail_if_zero(ACS_RAW_VALUE, ((unsigned int)*p & 0x1f), _raw_zero);
190
+ case 0x90 ... 0x9f: // FixArray
191
+ start_container(array, ((unsigned int)*p) & 0x0f, CT_ARRAY_ITEM);
192
+ case 0x80 ... 0x8f: // FixMap
193
+ start_container(map, ((unsigned int)*p) & 0x0f, CT_MAP_KEY);
194
+
195
+ default:
196
+ goto _failed;
197
+ }
198
+ // end CS_HEADER
199
+
200
+
201
+ _fixed_trail_again:
202
+ ++p;
203
+
204
+ default:
205
+ if((size_t)(pe - p) < trail) { goto _out; }
206
+ n = p; p += trail - 1;
207
+ switch(cs) {
208
+ //case CS_
209
+ //case CS_
210
+ case CS_FLOAT: {
211
+ uint32_t x = PTR_CAST_32(n); // FIXME
212
+ push_fixed_value(float, *((float*)&x)); }
213
+ case CS_DOUBLE: {
214
+ uint64_t x = PTR_CAST_64(n); // FIXME
215
+ push_fixed_value(double, *((double*)&x)); }
216
+ case CS_UINT_8:
217
+ push_fixed_value(uint8, (uint8_t)PTR_CAST_8(n));
218
+ case CS_UINT_16:
219
+ push_fixed_value(uint16, (uint16_t)PTR_CAST_16(n));
220
+ case CS_UINT_32:
221
+ push_fixed_value(uint32, (uint32_t)PTR_CAST_32(n));
222
+ case CS_UINT_64:
223
+ push_fixed_value(uint64, (uint64_t)PTR_CAST_64(n));
224
+
225
+ case CS_INT_8:
226
+ push_fixed_value(int8, (int8_t)PTR_CAST_8(n));
227
+ case CS_INT_16:
228
+ push_fixed_value(int16, (int16_t)PTR_CAST_16(n));
229
+ case CS_INT_32:
230
+ push_fixed_value(int32, (int32_t)PTR_CAST_32(n));
231
+ case CS_INT_64:
232
+ push_fixed_value(int64, (int64_t)PTR_CAST_64(n));
233
+
234
+ //case CS_
235
+ //case CS_
236
+ //case CS_BIG_INT_16:
237
+ // again_fixed_trail_if_zero(ACS_BIG_INT_VALUE, (uint16_t)PTR_CAST_16(n), _big_int_zero);
238
+ //case CS_BIG_INT_32:
239
+ // again_fixed_trail_if_zero(ACS_BIG_INT_VALUE, (uint32_t)PTR_CAST_32(n), _big_int_zero);
240
+ //case ACS_BIG_INT_VALUE:
241
+ //_big_int_zero:
242
+ // // FIXME
243
+ // push_variable_value(big_int, data, n, trail);
244
+
245
+ //case CS_BIG_FLOAT_16:
246
+ // again_fixed_trail_if_zero(ACS_BIG_FLOAT_VALUE, (uint16_t)PTR_CAST_16(n), _big_float_zero);
247
+ //case CS_BIG_FLOAT_32:
248
+ // again_fixed_trail_if_zero(ACS_BIG_FLOAT_VALUE, (uint32_t)PTR_CAST_32(n), _big_float_zero);
249
+ //case ACS_BIG_FLOAT_VALUE:
250
+ //_big_float_zero:
251
+ // // FIXME
252
+ // push_variable_value(big_float, data, n, trail);
253
+
254
+ case CS_RAW_16:
255
+ again_fixed_trail_if_zero(ACS_RAW_VALUE, (uint16_t)PTR_CAST_16(n), _raw_zero);
256
+ case CS_RAW_32:
257
+ again_fixed_trail_if_zero(ACS_RAW_VALUE, (uint32_t)PTR_CAST_32(n), _raw_zero);
258
+ case ACS_RAW_VALUE:
259
+ _raw_zero:
260
+ push_variable_value(raw, data, n, trail);
261
+
262
+ case CS_ARRAY_16:
263
+ start_container(array, (uint16_t)PTR_CAST_16(n), CT_ARRAY_ITEM);
264
+ case CS_ARRAY_32:
265
+ start_container(array, (uint32_t)PTR_CAST_32(n), CT_ARRAY_ITEM);
266
+
267
+ case CS_MAP_16:
268
+ start_container(map, (uint16_t)PTR_CAST_16(n), CT_MAP_KEY);
269
+ case CS_MAP_32:
270
+ start_container(map, (uint32_t)PTR_CAST_32(n), CT_MAP_KEY);
271
+
272
+ default:
273
+ goto _failed;
274
+ }
275
+ }
276
+
277
+ _push:
278
+ if(top == 0) { goto _finish; }
279
+ c = &stack[top-1];
280
+ switch(c->ct) {
281
+ case CT_ARRAY_ITEM:
282
+ msgpack_unpack_callback(array_item)(user, &c->obj, obj);
283
+ if(--c->count == 0) {
284
+ obj = c->obj;
285
+ --top;
286
+ /*printf("stack pop %d\n", top);*/
287
+ goto _push;
288
+ }
289
+ goto _header_again;
290
+ case CT_MAP_KEY:
291
+ c->map_key = obj;
292
+ c->ct = CT_MAP_VALUE;
293
+ goto _header_again;
294
+ case CT_MAP_VALUE:
295
+ msgpack_unpack_callback(map_item)(user, &c->obj, c->map_key, obj);
296
+ if(--c->count == 0) {
297
+ obj = c->obj;
298
+ --top;
299
+ /*printf("stack pop %d\n", top);*/
300
+ goto _push;
301
+ }
302
+ c->ct = CT_MAP_KEY;
303
+ goto _header_again;
304
+
305
+ default:
306
+ goto _failed;
307
+ }
308
+
309
+ _header_again:
310
+ cs = CS_HEADER;
311
+ ++p;
312
+ } while(p != pe);
313
+ goto _out;
314
+
315
+
316
+ _finish:
317
+ stack[0].obj = obj;
318
+ ++p;
319
+ ret = 1;
320
+ /*printf("-- finish --\n"); */
321
+ goto _end;
322
+
323
+ _failed:
324
+ /*printf("** FAILED **\n"); */
325
+ ret = -1;
326
+ goto _end;
327
+
328
+ _out:
329
+ ret = 0;
330
+ goto _end;
331
+
332
+ _end:
333
+ ctx->cs = cs;
334
+ ctx->trail = trail;
335
+ ctx->top = top;
336
+ *off = p - (const unsigned char*)data;
337
+
338
+ return ret;
339
+ }
340
+
341
+
342
+ #undef msgpack_unpack_func
343
+ #undef msgpack_unpack_callback
344
+ #undef msgpack_unpack_struct
345
+ #undef msgpack_unpack_object
346
+ #undef msgpack_unpack_user
347
+
348
+ #undef push_simple_value
349
+ #undef push_fixed_value
350
+ #undef push_variable_value
351
+ #undef again_fixed_trail
352
+ #undef again_fixed_trail_if_zero
353
+ #undef start_container
354
+
355
+ #undef NEXT_CS
356
+ #undef PTR_CAST_8
357
+ #undef PTR_CAST_16
358
+ #undef PTR_CAST_32
359
+ #undef PTR_CAST_64
360
+