msgpack 0.0.1

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,438 @@
1
+ /*
2
+ * MessagePack unpacking routine
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_INLINE_IMPL_H__
19
+ #define MSGPACK_UNPACK_INLINE_IMPL_H__
20
+
21
+ #include <string.h>
22
+ #include <assert.h>
23
+ #include <arpa/inet.h>
24
+ /*#include <stdio.h>*/
25
+
26
+ // Positive FixNum 0xxxxxxx 0x00 - 0x7f
27
+ // Negative FixNum 111xxxxx 0xe0 - 0xff
28
+ // Variable 110xxxxx 0xc0 - 0xdf
29
+ // nil 00000 0xc0
30
+ // string 00001 0xc1
31
+ // false 00010 0xc2
32
+ // true 00011 0xc3
33
+ // (?) 00100 0xc4
34
+ // (?) 00101 0xc5
35
+ // (?) 00110 0xc6
36
+ // (?) 00111 0xc7
37
+ // (?) 01000 0xc8
38
+ // (?) 01001 0xc9
39
+ // float 01010 0xca
40
+ // double 01011 0xcb
41
+ // uint 8 01100 0xcc
42
+ // uint 16 01101 0xcd
43
+ // uint 32 01110 0xce
44
+ // uint 64 01111 0xcf
45
+ // int 8 10000 0xd0
46
+ // int 16 10001 0xd1
47
+ // int 32 10010 0xd2
48
+ // int 64 10011 0xd3
49
+ // (?) 10100 0xd4
50
+ // (?) 10101 0xd5
51
+ // (big float 16) 10110 0xd6
52
+ // (big float 32) 10111 0xd7
53
+ // (big integer 16) 11000 0xd8
54
+ // (big integer 32) 11001 0xd9
55
+ // raw 16 11010 0xda
56
+ // raw 32 11011 0xdb
57
+ // array 16 11100 0xdc
58
+ // array 32 11101 0xdd
59
+ // map 16 11110 0xde
60
+ // map 32 11111 0xdf
61
+ // FixRaw 101xxxxx 0xa0 - 0xbf
62
+ // FixArray 1001xxxx 0x90 - 0x9f
63
+ // FixMap 1000xxxx 0x80 - 0x8f
64
+
65
+
66
+ #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
67
+ #if __BYTE_ORDER == __LITTLE_ENDIAN
68
+ #define __LITTLE_ENDIAN__
69
+ #elif __BYTE_ORDER == __BIG_ENDIAN
70
+ #define __BIG_ENDIAN__
71
+ #endif
72
+ #endif
73
+
74
+ static inline uint64_t ntohll(uint64_t x) {
75
+ #ifdef __LITTLE_ENDIAN__ // FIXME
76
+ #if defined(__bswap_64)
77
+ return __bswap_64(x);
78
+ #elif defined(__DARWIN_OSSwapInt64)
79
+ return __DARWIN_OSSwapInt64(x);
80
+ #else
81
+ return ((x << 56) & 0xff00000000000000ULL ) |
82
+ ((x << 40) & 0x00ff000000000000ULL ) |
83
+ ((x << 24) & 0x0000ff0000000000ULL ) |
84
+ ((x << 8) & 0x000000ff00000000ULL ) |
85
+ ((x >> 8) & 0x00000000ff000000ULL ) |
86
+ ((x >> 24) & 0x0000000000ff0000ULL ) |
87
+ ((x >> 40) & 0x000000000000ff00ULL ) |
88
+ ((x >> 56) & 0x00000000000000ffULL ) ;
89
+ #endif
90
+ #else
91
+ return x;
92
+ #endif
93
+ }
94
+
95
+ typedef enum {
96
+ CS_HEADER = 0x00, // nil
97
+
98
+ CS_STRING = 0x01,
99
+ //CS_ = 0x02, // false
100
+ //CS_ = 0x03, // true
101
+
102
+ //CS_ = 0x04,
103
+ //CS_ = 0x05,
104
+ //CS_ = 0x06,
105
+ //CS_ = 0x07,
106
+
107
+ //CS_ = 0x08,
108
+ //CS_ = 0x09,
109
+ CS_FLOAT = 0x0a,
110
+ CS_DOUBLE = 0x0b,
111
+ CS_UNSIGNED_INT_8 = 0x0c,
112
+ CS_UNSIGNED_INT_16 = 0x0d,
113
+ CS_UNSIGNED_INT_32 = 0x0e,
114
+ CS_UNSIGNED_INT_64 = 0x0f,
115
+ CS_SIGNED_INT_8 = 0x10,
116
+ CS_SIGNED_INT_16 = 0x11,
117
+ CS_SIGNED_INT_32 = 0x12,
118
+ CS_SIGNED_INT_64 = 0x13,
119
+
120
+ //CS_ = 0x14,
121
+ //CS_ = 0x15,
122
+ //CS_BIG_INT_16 = 0x16,
123
+ //CS_BIG_INT_32 = 0x17,
124
+ //CS_BIG_FLOAT_16 = 0x18,
125
+ //CS_BIG_FLOAT_32 = 0x19,
126
+ CS_RAW_16 = 0x1a,
127
+ CS_RAW_32 = 0x1b,
128
+ CS_ARRAY_16 = 0x1c,
129
+ CS_ARRAY_32 = 0x1d,
130
+ CS_MAP_16 = 0x1e,
131
+ CS_MAP_32 = 0x1f,
132
+
133
+ //ACS_BIG_INT_VALUE,
134
+ //ACS_BIG_FLOAT_VALUE,
135
+ ACS_RAW_VALUE,
136
+ } current_state_t;
137
+
138
+
139
+ typedef enum {
140
+ CT_ARRAY_ITEM,
141
+ CT_MAP_KEY,
142
+ CT_MAP_VALUE,
143
+ } container_type_t;
144
+
145
+
146
+ void msgpack_unpacker_init(msgpack_unpacker* ctx)
147
+ {
148
+ memset(ctx, 0, sizeof(msgpack_unpacker)); // FIXME init ctx->user?
149
+ ctx->cs = CS_HEADER;
150
+ ctx->trail = 0;
151
+ ctx->top = 0;
152
+ ctx->stack[0].obj = msgpack_unpack_init(&ctx->user);
153
+ }
154
+
155
+ int msgpack_unpacker_execute(msgpack_unpacker* ctx, const char* data, size_t len, size_t* off)
156
+ {
157
+ assert(len >= *off);
158
+
159
+ const unsigned char* p = (unsigned char*)data + *off;
160
+ const unsigned char* const pe = (unsigned char*)data + len;
161
+ const void* n = NULL;
162
+
163
+ size_t trail = ctx->trail;
164
+ unsigned int cs = ctx->cs;
165
+ unsigned int top = ctx->top;
166
+ msgpack_unpacker_stack* stack = ctx->stack;
167
+ msgpack_unpack_context* user = &ctx->user;
168
+
169
+ msgpack_object obj;
170
+
171
+ int ret;
172
+
173
+ #define push_simple_value(func) \
174
+ obj = func(user); \
175
+ /*printf("obj %d\n",obj);*/ \
176
+ goto _push
177
+ #define push_fixed_value(func, arg) \
178
+ obj = func(user, arg); \
179
+ /*printf("obj %d\n",obj);*/ \
180
+ goto _push
181
+ #define push_variable_value(func, arg, arglen) \
182
+ obj = func(user, arg, arglen); \
183
+ /*printf("obj %d\n",obj);*/ \
184
+ goto _push
185
+
186
+ #define again_terminal_trail(_cs, from) \
187
+ cs = _cs; \
188
+ stack[top].tmp.terminal_trail_start = from; \
189
+ goto _terminal_trail_again
190
+ #define again_fixed_trail(_cs, trail_len) \
191
+ trail = trail_len; \
192
+ cs = _cs; \
193
+ goto _fixed_trail_again
194
+ #define again_fixed_trail_if_zero(_cs, trail_len, ifzero) \
195
+ trail = trail_len; \
196
+ if(trail == 0) { goto ifzero; } \
197
+ cs = _cs; \
198
+ goto _fixed_trail_again
199
+
200
+ #define start_container(func, count_, ct_) \
201
+ stack[top].obj = func(user, count_); \
202
+ if((count_) == 0) { obj = stack[top].obj; goto _push; } \
203
+ if(top >= MSG_STACK_SIZE) { goto _failed; } \
204
+ stack[top].ct = ct_; \
205
+ stack[top].count = count_; \
206
+ /*printf("container %d count %d stack %d\n",stack[top].obj,count_,top);*/ \
207
+ /*printf("stack push %d\n", top);*/ \
208
+ ++top; \
209
+ goto _header_again
210
+
211
+ #define NEXT_CS(p) \
212
+ ((unsigned int)*p & 0x1f)
213
+
214
+ #define PTR_CAST_8(ptr) (*(uint8_t*)ptr)
215
+ #define PTR_CAST_16(ptr) ntohs(*(uint16_t*)ptr)
216
+ #define PTR_CAST_32(ptr) ntohl(*(uint32_t*)ptr)
217
+ #define PTR_CAST_64(ptr) ntohll(*(uint64_t*)ptr)
218
+
219
+ if(p == pe) { goto _out; }
220
+ do {
221
+ switch(cs) {
222
+ case CS_HEADER:
223
+ switch(*p) {
224
+ case 0x00 ... 0x7f: // Positive Fixnum
225
+ push_fixed_value(msgpack_unpack_unsigned_int_8, *(uint8_t*)p);
226
+ case 0xe0 ... 0xff: // Negative Fixnum
227
+ push_fixed_value(msgpack_unpack_signed_int_8, *(int8_t*)p);
228
+ case 0xc0 ... 0xdf: // Variable
229
+ switch(*p) {
230
+ case 0xc0: // nil
231
+ push_simple_value(msgpack_unpack_nil);
232
+ case 0xc1: // string
233
+ again_terminal_trail(NEXT_CS(p), p+1);
234
+ case 0xc2: // false
235
+ push_simple_value(msgpack_unpack_false);
236
+ case 0xc3: // true
237
+ push_simple_value(msgpack_unpack_true);
238
+ //case 0xc4:
239
+ //case 0xc5:
240
+ //case 0xc6:
241
+ //case 0xc7:
242
+ //case 0xc8:
243
+ //case 0xc9:
244
+ case 0xca: // float
245
+ case 0xcb: // double
246
+ case 0xcc: // unsigned int 8
247
+ case 0xcd: // unsigned int 16
248
+ case 0xce: // unsigned int 32
249
+ case 0xcf: // unsigned int 64
250
+ case 0xd0: // signed int 8
251
+ case 0xd1: // signed int 16
252
+ case 0xd2: // signed int 32
253
+ case 0xd3: // signed int 64
254
+ again_fixed_trail(NEXT_CS(p), 1 << (((unsigned int)*p) & 0x03));
255
+ //case 0xd4:
256
+ //case 0xd5:
257
+ //case 0xd6: // big integer 16
258
+ //case 0xd7: // big integer 32
259
+ //case 0xd8: // big float 16
260
+ //case 0xd9: // big float 32
261
+ case 0xda: // raw 16
262
+ case 0xdb: // raw 32
263
+ case 0xdc: // array 16
264
+ case 0xdd: // array 32
265
+ case 0xde: // map 16
266
+ case 0xdf: // map 32
267
+ again_fixed_trail(NEXT_CS(p), 2 << (((unsigned int)*p) & 0x01));
268
+ default:
269
+ goto _failed;
270
+ }
271
+ case 0xa0 ... 0xbf: // FixRaw
272
+ again_fixed_trail_if_zero(ACS_RAW_VALUE, ((unsigned int)*p & 0x1f), _raw_zero);
273
+ case 0x90 ... 0x9f: // FixArray
274
+ start_container(msgpack_unpack_array_start, ((unsigned int)*p) & 0x0f, CT_ARRAY_ITEM);
275
+ case 0x80 ... 0x8f: // FixMap
276
+ start_container(msgpack_unpack_map_start, ((unsigned int)*p) & 0x0f, CT_MAP_KEY);
277
+
278
+ default:
279
+ goto _failed;
280
+ }
281
+ // end CS_HEADER
282
+
283
+
284
+ _terminal_trail_again:
285
+ ++p;
286
+
287
+ case CS_STRING:
288
+ if(*p == 0) {
289
+ const unsigned char* start = stack[top].tmp.terminal_trail_start;
290
+ obj = msgpack_unpack_string(user, start, p-start);
291
+ goto _push;
292
+ }
293
+ goto _terminal_trail_again;
294
+
295
+
296
+ _fixed_trail_again:
297
+ ++p;
298
+
299
+ default:
300
+ if((size_t)(pe - p) < trail) { goto _out; }
301
+ n = p; p += trail - 1;
302
+ switch(cs) {
303
+ //case CS_
304
+ //case CS_
305
+ case CS_FLOAT: {
306
+ uint32_t x = PTR_CAST_32(n); // FIXME
307
+ push_fixed_value(msgpack_unpack_float, *((float*)&x)); }
308
+ case CS_DOUBLE: {
309
+ uint64_t x = PTR_CAST_64(n); // FIXME
310
+ push_fixed_value(msgpack_unpack_double, *((double*)&x)); }
311
+ case CS_UNSIGNED_INT_8:
312
+ push_fixed_value(msgpack_unpack_unsigned_int_8, (uint8_t)PTR_CAST_8(n));
313
+ case CS_UNSIGNED_INT_16:
314
+ push_fixed_value(msgpack_unpack_unsigned_int_16, (uint16_t)PTR_CAST_16(n));
315
+ case CS_UNSIGNED_INT_32:
316
+ push_fixed_value(msgpack_unpack_unsigned_int_32, (uint32_t)PTR_CAST_32(n));
317
+ case CS_UNSIGNED_INT_64:
318
+ push_fixed_value(msgpack_unpack_unsigned_int_64, (uint64_t)PTR_CAST_64(n));
319
+
320
+ case CS_SIGNED_INT_8:
321
+ push_fixed_value(msgpack_unpack_signed_int_8, (int8_t)PTR_CAST_8(n));
322
+ case CS_SIGNED_INT_16:
323
+ push_fixed_value(msgpack_unpack_signed_int_16, (int16_t)PTR_CAST_16(n));
324
+ case CS_SIGNED_INT_32:
325
+ push_fixed_value(msgpack_unpack_signed_int_32, (int32_t)PTR_CAST_32(n));
326
+ case CS_SIGNED_INT_64:
327
+ push_fixed_value(msgpack_unpack_signed_int_64, (int64_t)PTR_CAST_64(n));
328
+
329
+ //case CS_
330
+ //case CS_
331
+ //case CS_BIG_INT_16:
332
+ // again_fixed_trail_if_zero(ACS_BIG_INT_VALUE, (uint16_t)PTR_CAST_16(n), _big_int_zero);
333
+ //case CS_BIG_INT_32:
334
+ // again_fixed_trail_if_zero(ACS_BIG_INT_VALUE, (uint32_t)PTR_CAST_32(n), _big_int_zero);
335
+ //case ACS_BIG_INT_VALUE:
336
+ //_big_int_zero:
337
+ // // FIXME
338
+ // push_variable_value(msgpack_unpack_big_int, n, trail);
339
+
340
+ //case CS_BIG_FLOAT_16:
341
+ // again_fixed_trail_if_zero(ACS_BIG_FLOAT_VALUE, (uint16_t)PTR_CAST_16(n), _big_float_zero);
342
+ //case CS_BIG_FLOAT_32:
343
+ // again_fixed_trail_if_zero(ACS_BIG_FLOAT_VALUE, (uint32_t)PTR_CAST_32(n), _big_float_zero);
344
+ //case ACS_BIG_FLOAT_VALUE:
345
+ //_big_float_zero:
346
+ // // FIXME
347
+ // push_variable_value(msgpack_unpack_big_float, n, trail);
348
+
349
+ case CS_RAW_16:
350
+ again_fixed_trail_if_zero(ACS_RAW_VALUE, (uint16_t)PTR_CAST_16(n), _raw_zero);
351
+ case CS_RAW_32:
352
+ again_fixed_trail_if_zero(ACS_RAW_VALUE, (uint32_t)PTR_CAST_32(n), _raw_zero);
353
+ case ACS_RAW_VALUE:
354
+ _raw_zero:
355
+ push_variable_value(msgpack_unpack_raw, n, trail);
356
+
357
+ case CS_ARRAY_16:
358
+ start_container(msgpack_unpack_array_start, (uint16_t)PTR_CAST_16(n), CT_ARRAY_ITEM);
359
+ case CS_ARRAY_32:
360
+ start_container(msgpack_unpack_array_start, (uint32_t)PTR_CAST_32(n), CT_ARRAY_ITEM);
361
+
362
+ case CS_MAP_16:
363
+ start_container(msgpack_unpack_map_start, (uint16_t)PTR_CAST_16(n), CT_MAP_KEY);
364
+ case CS_MAP_32:
365
+ start_container(msgpack_unpack_map_start, (uint32_t)PTR_CAST_32(n), CT_MAP_KEY);
366
+
367
+ default:
368
+ goto _failed;
369
+ }
370
+ }
371
+
372
+ _push:
373
+ if(top == 0) { goto _finish; }
374
+ msgpack_unpacker_stack* c = &stack[top-1];
375
+ switch(c->ct) {
376
+ case CT_ARRAY_ITEM:
377
+ msgpack_unpack_array_item(user, c->obj, obj);
378
+ if(--c->count == 0) {
379
+ obj = c->obj;
380
+ --top;
381
+ /*printf("stack pop %d\n", top);*/
382
+ goto _push;
383
+ }
384
+ goto _header_again;
385
+ case CT_MAP_KEY:
386
+ c->tmp.map_key = obj;
387
+ c->ct = CT_MAP_VALUE;
388
+ goto _header_again;
389
+ case CT_MAP_VALUE:
390
+ msgpack_unpack_map_item(user, c->obj, c->tmp.map_key, obj);
391
+ if(--c->count == 0) {
392
+ obj = c->obj;
393
+ --top;
394
+ /*printf("stack pop %d\n", top);*/
395
+ goto _push;
396
+ }
397
+ c->ct = CT_MAP_KEY;
398
+ goto _header_again;
399
+
400
+ default:
401
+ goto _failed;
402
+ }
403
+
404
+ _header_again:
405
+ cs = CS_HEADER;
406
+ ++p;
407
+ } while(p != pe);
408
+ goto _out;
409
+
410
+
411
+ _finish:
412
+ stack[0].obj = obj;
413
+ ++p;
414
+ ret = 1;
415
+ /*printf("-- finish --\n"); */
416
+ goto _end;
417
+
418
+ _failed:
419
+ /*printf("** FAILED **\n"); */
420
+ ret = -1;
421
+ goto _end;
422
+
423
+ _out:
424
+ ret = 0;
425
+ goto _end;
426
+
427
+ _end:
428
+ ctx->cs = cs;
429
+ ctx->trail = trail;
430
+ ctx->top = top;
431
+ *off = p - (const unsigned char*)data;
432
+
433
+ return ret;
434
+ }
435
+
436
+
437
+ #endif /* msgpack/unpack/inline_impl.h */
438
+
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/msgpack.rb'}"
9
+ puts "Loading msgpack gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)