ed-precompiled_msgpack 1.8.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.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/ChangeLog +368 -0
  3. data/LICENSE +177 -0
  4. data/README.md +302 -0
  5. data/ext/java/org/msgpack/jruby/Buffer.java +233 -0
  6. data/ext/java/org/msgpack/jruby/Decoder.java +307 -0
  7. data/ext/java/org/msgpack/jruby/Encoder.java +456 -0
  8. data/ext/java/org/msgpack/jruby/ExtensionRegistry.java +167 -0
  9. data/ext/java/org/msgpack/jruby/ExtensionValue.java +128 -0
  10. data/ext/java/org/msgpack/jruby/Factory.java +130 -0
  11. data/ext/java/org/msgpack/jruby/MessagePackLibrary.java +45 -0
  12. data/ext/java/org/msgpack/jruby/Packer.java +266 -0
  13. data/ext/java/org/msgpack/jruby/Types.java +37 -0
  14. data/ext/java/org/msgpack/jruby/Unpacker.java +336 -0
  15. data/ext/msgpack/buffer.c +669 -0
  16. data/ext/msgpack/buffer.h +604 -0
  17. data/ext/msgpack/buffer_class.c +616 -0
  18. data/ext/msgpack/buffer_class.h +33 -0
  19. data/ext/msgpack/compat.h +26 -0
  20. data/ext/msgpack/extconf.rb +53 -0
  21. data/ext/msgpack/extension_value_class.c +34 -0
  22. data/ext/msgpack/extension_value_class.h +31 -0
  23. data/ext/msgpack/factory_class.c +276 -0
  24. data/ext/msgpack/factory_class.h +33 -0
  25. data/ext/msgpack/packer.c +199 -0
  26. data/ext/msgpack/packer.h +513 -0
  27. data/ext/msgpack/packer_class.c +442 -0
  28. data/ext/msgpack/packer_class.h +43 -0
  29. data/ext/msgpack/packer_ext_registry.c +74 -0
  30. data/ext/msgpack/packer_ext_registry.h +140 -0
  31. data/ext/msgpack/rbinit.c +35 -0
  32. data/ext/msgpack/rmem.c +93 -0
  33. data/ext/msgpack/rmem.h +109 -0
  34. data/ext/msgpack/sysdep.h +118 -0
  35. data/ext/msgpack/sysdep_endian.h +50 -0
  36. data/ext/msgpack/sysdep_types.h +46 -0
  37. data/ext/msgpack/unpacker.c +986 -0
  38. data/ext/msgpack/unpacker.h +152 -0
  39. data/ext/msgpack/unpacker_class.c +447 -0
  40. data/ext/msgpack/unpacker_class.h +43 -0
  41. data/ext/msgpack/unpacker_ext_registry.c +74 -0
  42. data/ext/msgpack/unpacker_ext_registry.h +62 -0
  43. data/lib/msgpack/bigint.rb +69 -0
  44. data/lib/msgpack/buffer.rb +9 -0
  45. data/lib/msgpack/core_ext.rb +139 -0
  46. data/lib/msgpack/factory.rb +211 -0
  47. data/lib/msgpack/packer.rb +37 -0
  48. data/lib/msgpack/symbol.rb +26 -0
  49. data/lib/msgpack/time.rb +29 -0
  50. data/lib/msgpack/timestamp.rb +76 -0
  51. data/lib/msgpack/unpacker.rb +41 -0
  52. data/lib/msgpack/version.rb +6 -0
  53. data/lib/msgpack.rb +53 -0
  54. data/msgpack.gemspec +41 -0
  55. metadata +216 -0
@@ -0,0 +1,986 @@
1
+ /*
2
+ * MessagePack for Ruby
3
+ *
4
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
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
+ #include "unpacker.h"
20
+ #include "rmem.h"
21
+ #include "extension_value_class.h"
22
+ #include <assert.h>
23
+ #include <limits.h>
24
+
25
+ #if !defined(HAVE_RB_PROC_CALL_WITH_BLOCK)
26
+ #define rb_proc_call_with_block(recv, argc, argv, block) rb_funcallv(recv, rb_intern("call"), argc, argv)
27
+ #endif
28
+
29
+ #ifndef HAVE_RB_GC_MARK_LOCATIONS
30
+ // For TruffleRuby
31
+ void rb_gc_mark_locations(const VALUE *start, const VALUE *end)
32
+ {
33
+ VALUE *value = start;
34
+
35
+ while (value < end) {
36
+ rb_gc_mark(*value);
37
+ value++;
38
+ }
39
+ }
40
+ #endif
41
+
42
+ struct protected_proc_call_args {
43
+ VALUE proc;
44
+ int argc;
45
+ VALUE *argv;
46
+ };
47
+
48
+ static VALUE protected_proc_call_safe(VALUE _args) {
49
+ struct protected_proc_call_args *args = (struct protected_proc_call_args *)_args;
50
+
51
+ return rb_proc_call_with_block(args->proc, args->argc, args->argv, Qnil);
52
+ }
53
+
54
+ static VALUE protected_proc_call(VALUE proc, int argc, VALUE *argv, int *raised) {
55
+ struct protected_proc_call_args args = {
56
+ .proc = proc,
57
+ .argc = argc,
58
+ .argv = argv,
59
+ };
60
+ return rb_protect(protected_proc_call_safe, (VALUE)&args, raised);
61
+ }
62
+
63
+ static int RAW_TYPE_STRING = 256;
64
+ static int RAW_TYPE_BINARY = 257;
65
+ static int16_t INITIAL_BUFFER_CAPACITY_MAX = SHRT_MAX;
66
+
67
+ static msgpack_rmem_t s_stack_rmem;
68
+
69
+ #if !defined(HAVE_RB_HASH_NEW_CAPA)
70
+ static inline VALUE rb_hash_new_capa_inline(long capa)
71
+ {
72
+ return rb_hash_new();
73
+ }
74
+ #define rb_hash_new_capa rb_hash_new_capa_inline
75
+ #endif
76
+
77
+ static inline int16_t initial_buffer_size(long size)
78
+ {
79
+ return (size > INITIAL_BUFFER_CAPACITY_MAX) ? INITIAL_BUFFER_CAPACITY_MAX : size;
80
+ }
81
+
82
+ void msgpack_unpacker_static_init(void)
83
+ {
84
+ assert(sizeof(msgpack_unpacker_stack_entry_t) * MSGPACK_UNPACKER_STACK_CAPACITY <= MSGPACK_RMEM_PAGE_SIZE);
85
+
86
+ msgpack_rmem_init(&s_stack_rmem);
87
+ }
88
+
89
+ void msgpack_unpacker_static_destroy(void)
90
+ {
91
+ msgpack_rmem_destroy(&s_stack_rmem);
92
+ }
93
+
94
+ #define HEAD_BYTE_REQUIRED 0xc1
95
+
96
+ static inline bool _msgpack_unpacker_stack_init(msgpack_unpacker_stack_t *stack) {
97
+ if (!stack->data) {
98
+ stack->capacity = MSGPACK_UNPACKER_STACK_CAPACITY;
99
+ stack->data = msgpack_rmem_alloc(&s_stack_rmem);
100
+ stack->depth = 0;
101
+ return true;
102
+ }
103
+ return false;
104
+ }
105
+
106
+ static inline void _msgpack_unpacker_free_stack(msgpack_unpacker_stack_t* stack) {
107
+ if (stack->data) {
108
+ if (!msgpack_rmem_free(&s_stack_rmem, stack->data)) {
109
+ rb_bug("Failed to free an rmem pointer, memory leak?");
110
+ }
111
+ stack->data = NULL;
112
+ stack->depth = 0;
113
+ }
114
+ }
115
+
116
+ #define STACK_INIT(uk) bool stack_allocated = _msgpack_unpacker_stack_init(&uk->stack);
117
+ #define STACK_FREE(uk) if (stack_allocated) { _msgpack_unpacker_free_stack(&uk->stack); }
118
+
119
+ void _msgpack_unpacker_init(msgpack_unpacker_t* uk)
120
+ {
121
+ msgpack_buffer_init(UNPACKER_BUFFER_(uk));
122
+
123
+ uk->head_byte = HEAD_BYTE_REQUIRED;
124
+
125
+ uk->last_object = Qnil;
126
+ uk->reading_raw = Qnil;
127
+ }
128
+
129
+ void _msgpack_unpacker_destroy(msgpack_unpacker_t* uk)
130
+ {
131
+ _msgpack_unpacker_free_stack(&uk->stack);
132
+ msgpack_buffer_destroy(UNPACKER_BUFFER_(uk));
133
+ }
134
+
135
+ void msgpack_unpacker_mark_stack(msgpack_unpacker_stack_t* stack)
136
+ {
137
+ if (stack->data) {
138
+ msgpack_unpacker_stack_entry_t* s = stack->data;
139
+ msgpack_unpacker_stack_entry_t* send = stack->data + stack->depth;
140
+ for(; s < send; s++) {
141
+ rb_gc_mark(s->object);
142
+ rb_gc_mark(s->key);
143
+ }
144
+ }
145
+ }
146
+
147
+ void msgpack_unpacker_mark_key_cache(msgpack_key_cache_t *cache)
148
+ {
149
+ const VALUE *entries = &cache->entries[0];
150
+ rb_gc_mark_locations(entries, entries + cache->length);
151
+ }
152
+
153
+ void msgpack_unpacker_mark(msgpack_unpacker_t* uk)
154
+ {
155
+ rb_gc_mark(uk->last_object);
156
+ rb_gc_mark(uk->reading_raw);
157
+ msgpack_unpacker_mark_stack(&uk->stack);
158
+ msgpack_unpacker_mark_key_cache(&uk->key_cache);
159
+ /* See MessagePack_Buffer_wrap */
160
+ /* msgpack_buffer_mark(UNPACKER_BUFFER_(uk)); */
161
+ rb_gc_mark(uk->buffer_ref);
162
+ rb_gc_mark(uk->self);
163
+ }
164
+
165
+ void _msgpack_unpacker_reset(msgpack_unpacker_t* uk)
166
+ {
167
+ msgpack_buffer_clear(UNPACKER_BUFFER_(uk));
168
+
169
+ uk->head_byte = HEAD_BYTE_REQUIRED;
170
+
171
+ /*memset(uk->stack, 0, sizeof(msgpack_unpacker_t) * uk->stack.depth);*/
172
+ uk->stack.depth = 0;
173
+ uk->last_object = Qnil;
174
+ uk->reading_raw = Qnil;
175
+ uk->reading_raw_remaining = 0;
176
+ }
177
+
178
+
179
+ /* head byte functions */
180
+ static int read_head_byte(msgpack_unpacker_t* uk)
181
+ {
182
+ int r = msgpack_buffer_read_1(UNPACKER_BUFFER_(uk));
183
+ if(r == -1) {
184
+ return PRIMITIVE_EOF;
185
+ }
186
+ return uk->head_byte = r;
187
+ }
188
+
189
+ static inline int get_head_byte(msgpack_unpacker_t* uk)
190
+ {
191
+ int b = uk->head_byte;
192
+ if(b == HEAD_BYTE_REQUIRED) {
193
+ b = read_head_byte(uk);
194
+ }
195
+ return b;
196
+ }
197
+
198
+ static inline void reset_head_byte(msgpack_unpacker_t* uk)
199
+ {
200
+ uk->head_byte = HEAD_BYTE_REQUIRED;
201
+ }
202
+
203
+ static inline int object_complete(msgpack_unpacker_t* uk, VALUE object)
204
+ {
205
+ if(uk->freeze) {
206
+ rb_obj_freeze(object);
207
+ }
208
+
209
+ uk->last_object = object;
210
+ reset_head_byte(uk);
211
+ return PRIMITIVE_OBJECT_COMPLETE;
212
+ }
213
+
214
+ static inline int object_complete_symbol(msgpack_unpacker_t* uk, VALUE object)
215
+ {
216
+ uk->last_object = object;
217
+ reset_head_byte(uk);
218
+ return PRIMITIVE_OBJECT_COMPLETE;
219
+ }
220
+
221
+ static inline int object_complete_ext(msgpack_unpacker_t* uk, int ext_type, VALUE str)
222
+ {
223
+ if (uk->optimized_symbol_ext_type && ext_type == uk->symbol_ext_type) {
224
+ if (RB_UNLIKELY(NIL_P(str))) { // empty extension is returned as Qnil
225
+ return object_complete_symbol(uk, ID2SYM(rb_intern3("", 0, rb_utf8_encoding())));
226
+ }
227
+ return object_complete_symbol(uk, rb_str_intern(str));
228
+ }
229
+
230
+ int ext_flags;
231
+ VALUE proc = msgpack_unpacker_ext_registry_lookup(uk->ext_registry, ext_type, &ext_flags);
232
+
233
+ if(proc != Qnil) {
234
+ VALUE obj;
235
+ VALUE arg = (str == Qnil ? rb_str_buf_new(0) : str);
236
+ int raised;
237
+ obj = protected_proc_call(proc, 1, &arg, &raised);
238
+ if (raised) {
239
+ uk->last_object = rb_errinfo();
240
+ return PRIMITIVE_RECURSIVE_RAISED;
241
+ }
242
+ return object_complete(uk, obj);
243
+ }
244
+
245
+ if(uk->allow_unknown_ext) {
246
+ VALUE obj = MessagePack_ExtensionValue_new(ext_type, str == Qnil ? rb_str_buf_new(0) : str);
247
+ return object_complete(uk, obj);
248
+ }
249
+
250
+ return PRIMITIVE_UNEXPECTED_EXT_TYPE;
251
+ }
252
+
253
+ /* stack funcs */
254
+ static inline msgpack_unpacker_stack_entry_t* _msgpack_unpacker_stack_entry_top(msgpack_unpacker_t* uk)
255
+ {
256
+ return &uk->stack.data[uk->stack.depth-1];
257
+ }
258
+
259
+ static inline int _msgpack_unpacker_stack_push(msgpack_unpacker_t* uk, enum stack_type_t type, size_t count, VALUE object)
260
+ {
261
+ reset_head_byte(uk);
262
+
263
+ if(uk->stack.capacity - uk->stack.depth <= 0) {
264
+ return PRIMITIVE_STACK_TOO_DEEP;
265
+ }
266
+
267
+ msgpack_unpacker_stack_entry_t* next = &uk->stack.data[uk->stack.depth];
268
+ next->count = count;
269
+ next->type = type;
270
+ next->object = object;
271
+ next->key = Qnil;
272
+
273
+ uk->stack.depth++;
274
+ return PRIMITIVE_CONTAINER_START;
275
+ }
276
+
277
+ static inline size_t msgpack_unpacker_stack_pop(msgpack_unpacker_t* uk)
278
+ {
279
+ return --uk->stack.depth;
280
+ }
281
+
282
+ static inline bool msgpack_unpacker_stack_is_empty(msgpack_unpacker_t* uk)
283
+ {
284
+ return uk->stack.depth == 0;
285
+ }
286
+
287
+ #ifdef USE_CASE_RANGE
288
+
289
+ #define SWITCH_RANGE_BEGIN(BYTE) { switch(BYTE) {
290
+ #define SWITCH_RANGE(BYTE, FROM, TO) } case FROM ... TO: {
291
+ #define SWITCH_RANGE_DEFAULT } default: {
292
+ #define SWITCH_RANGE_END } }
293
+
294
+ #else
295
+
296
+ #define SWITCH_RANGE_BEGIN(BYTE) { if(0) {
297
+ #define SWITCH_RANGE(BYTE, FROM, TO) } else if(FROM <= (BYTE) && (BYTE) <= TO) {
298
+ #define SWITCH_RANGE_DEFAULT } else {
299
+ #define SWITCH_RANGE_END } }
300
+
301
+ #endif
302
+
303
+ union msgpack_buffer_cast_block_t {
304
+ char buffer[8];
305
+ uint8_t u8;
306
+ uint16_t u16;
307
+ uint32_t u32;
308
+ uint64_t u64;
309
+ int8_t i8;
310
+ int16_t i16;
311
+ int32_t i32;
312
+ int64_t i64;
313
+ float f;
314
+ double d;
315
+ };
316
+
317
+ #define READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, n) \
318
+ union msgpack_buffer_cast_block_t cb; \
319
+ if (!msgpack_buffer_read_all(UNPACKER_BUFFER_(uk), (char *)&cb.buffer, n)) { \
320
+ return PRIMITIVE_EOF; \
321
+ }
322
+
323
+ static inline bool is_reading_map_key(msgpack_unpacker_t* uk)
324
+ {
325
+ if(uk->stack.depth > 0) {
326
+ msgpack_unpacker_stack_entry_t* top = _msgpack_unpacker_stack_entry_top(uk);
327
+ if(top->type == STACK_TYPE_MAP_KEY) {
328
+ return true;
329
+ }
330
+ }
331
+ return false;
332
+ }
333
+
334
+ static int read_raw_body_cont(msgpack_unpacker_t* uk)
335
+ {
336
+ size_t length = uk->reading_raw_remaining;
337
+
338
+ if(uk->reading_raw == Qnil) {
339
+ uk->reading_raw = rb_str_buf_new(length);
340
+ }
341
+
342
+ do {
343
+ size_t n = msgpack_buffer_read_to_string(UNPACKER_BUFFER_(uk), uk->reading_raw, length);
344
+ if(n == 0) {
345
+ return PRIMITIVE_EOF;
346
+ }
347
+ /* update reading_raw_remaining everytime because
348
+ * msgpack_buffer_read_to_string raises IOError */
349
+ uk->reading_raw_remaining = length = length - n;
350
+ } while(length > 0);
351
+
352
+ int ret;
353
+ if(uk->reading_raw_type == RAW_TYPE_STRING) {
354
+ ENCODING_SET(uk->reading_raw, msgpack_rb_encindex_utf8);
355
+ ret = object_complete(uk, uk->reading_raw);
356
+ } else if (uk->reading_raw_type == RAW_TYPE_BINARY) {
357
+ ret = object_complete(uk, uk->reading_raw);
358
+ } else {
359
+ ret = object_complete_ext(uk, uk->reading_raw_type, uk->reading_raw);
360
+ }
361
+ uk->reading_raw = Qnil;
362
+ return ret;
363
+ }
364
+
365
+ static inline int read_raw_body_begin(msgpack_unpacker_t* uk, int raw_type)
366
+ {
367
+ /* assuming uk->reading_raw == Qnil */
368
+
369
+ int ext_flags;
370
+ VALUE proc;
371
+
372
+ if(!(raw_type == RAW_TYPE_STRING || raw_type == RAW_TYPE_BINARY)) {
373
+ proc = msgpack_unpacker_ext_registry_lookup(uk->ext_registry, raw_type, &ext_flags);
374
+ if(proc != Qnil && ext_flags & MSGPACK_EXT_RECURSIVE) {
375
+ VALUE obj;
376
+ uk->last_object = Qnil;
377
+ reset_head_byte(uk);
378
+ uk->reading_raw_remaining = 0;
379
+
380
+ _msgpack_unpacker_stack_push(uk, STACK_TYPE_RECURSIVE, 1, Qnil);
381
+ int raised;
382
+ obj = protected_proc_call(proc, 1, &uk->self, &raised);
383
+ msgpack_unpacker_stack_pop(uk);
384
+
385
+ if (raised) {
386
+ uk->last_object = rb_errinfo();
387
+ return PRIMITIVE_RECURSIVE_RAISED;
388
+ }
389
+
390
+ return object_complete(uk, obj);
391
+ }
392
+ }
393
+
394
+ /* try optimized read */
395
+ size_t length = uk->reading_raw_remaining;
396
+ if(length <= msgpack_buffer_top_readable_size(UNPACKER_BUFFER_(uk))) {
397
+ int ret;
398
+ if ((uk->optimized_symbol_ext_type && uk->symbol_ext_type == raw_type)) {
399
+ VALUE symbol = msgpack_buffer_read_top_as_symbol(UNPACKER_BUFFER_(uk), length, raw_type != RAW_TYPE_BINARY);
400
+ ret = object_complete_symbol(uk, symbol);
401
+ } else if (is_reading_map_key(uk) && raw_type == RAW_TYPE_STRING) {
402
+ /* don't use zerocopy for hash keys but get a frozen string directly
403
+ * because rb_hash_aset freezes keys and it causes copying */
404
+ VALUE key;
405
+ if (uk->symbolize_keys) {
406
+ if (uk->use_key_cache) {
407
+ key = msgpack_buffer_read_top_as_interned_symbol(UNPACKER_BUFFER_(uk), &uk->key_cache, length);
408
+ } else {
409
+ key = msgpack_buffer_read_top_as_symbol(UNPACKER_BUFFER_(uk), length, true);
410
+ }
411
+ ret = object_complete_symbol(uk, key);
412
+ } else {
413
+ if (uk->use_key_cache) {
414
+ key = msgpack_buffer_read_top_as_interned_string(UNPACKER_BUFFER_(uk), &uk->key_cache, length);
415
+ } else {
416
+ key = msgpack_buffer_read_top_as_string(UNPACKER_BUFFER_(uk), length, true, true);
417
+ }
418
+
419
+ ret = object_complete(uk, key);
420
+ }
421
+ } else {
422
+ bool will_freeze = uk->freeze;
423
+ if(raw_type == RAW_TYPE_STRING || raw_type == RAW_TYPE_BINARY) {
424
+ VALUE string = msgpack_buffer_read_top_as_string(UNPACKER_BUFFER_(uk), length, will_freeze, raw_type == RAW_TYPE_STRING);
425
+ ret = object_complete(uk, string);
426
+ } else {
427
+ VALUE string = msgpack_buffer_read_top_as_string(UNPACKER_BUFFER_(uk), length, false, false);
428
+ ret = object_complete_ext(uk, raw_type, string);
429
+ }
430
+ }
431
+ uk->reading_raw_remaining = 0;
432
+ return ret;
433
+ }
434
+
435
+ uk->reading_raw_type = raw_type;
436
+ return read_raw_body_cont(uk);
437
+ }
438
+
439
+ static int read_primitive(msgpack_unpacker_t* uk)
440
+ {
441
+ if(uk->reading_raw_remaining > 0) {
442
+ return read_raw_body_cont(uk);
443
+ }
444
+
445
+ int b = get_head_byte(uk);
446
+ if(b < 0) {
447
+ return b;
448
+ }
449
+
450
+ SWITCH_RANGE_BEGIN(b)
451
+ SWITCH_RANGE(b, 0x00, 0x7f) // Positive Fixnum
452
+ return object_complete(uk, INT2NUM(b));
453
+
454
+ SWITCH_RANGE(b, 0xe0, 0xff) // Negative Fixnum
455
+ return object_complete(uk, INT2NUM((int8_t)b));
456
+
457
+ SWITCH_RANGE(b, 0xa0, 0xbf) // FixRaw / fixstr
458
+ int count = b & 0x1f;
459
+ /* read_raw_body_begin sets uk->reading_raw */
460
+ uk->reading_raw_remaining = count;
461
+ return read_raw_body_begin(uk, RAW_TYPE_STRING);
462
+
463
+ SWITCH_RANGE(b, 0x90, 0x9f) // FixArray
464
+ int count = b & 0x0f;
465
+ if(count == 0) {
466
+ return object_complete(uk, rb_ary_new());
467
+ }
468
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_ARRAY, count, rb_ary_new2(initial_buffer_size(count)));
469
+
470
+ SWITCH_RANGE(b, 0x80, 0x8f) // FixMap
471
+ int count = b & 0x0f;
472
+ if(count == 0) {
473
+ return object_complete(uk, rb_hash_new());
474
+ }
475
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_MAP_KEY, count*2, rb_hash_new_capa(initial_buffer_size(count)));
476
+
477
+ SWITCH_RANGE(b, 0xc0, 0xdf) // Variable
478
+ switch(b) {
479
+ case 0xc0: // nil
480
+ return object_complete(uk, Qnil);
481
+
482
+ //case 0xc1: // string
483
+
484
+ case 0xc2: // false
485
+ return object_complete(uk, Qfalse);
486
+
487
+ case 0xc3: // true
488
+ return object_complete(uk, Qtrue);
489
+
490
+ case 0xc7: // ext 8
491
+ {
492
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
493
+ uint8_t length = cb.u8;
494
+ int ext_type = (signed char) cb.buffer[1];
495
+ if(length == 0) {
496
+ return object_complete_ext(uk, ext_type, Qnil);
497
+ }
498
+ uk->reading_raw_remaining = length;
499
+ return read_raw_body_begin(uk, ext_type);
500
+ }
501
+
502
+ case 0xc8: // ext 16
503
+ {
504
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 3);
505
+ uint16_t length = _msgpack_be16(cb.u16);
506
+ int ext_type = (signed char) cb.buffer[2];
507
+ if(length == 0) {
508
+ return object_complete_ext(uk, ext_type, Qnil);
509
+ }
510
+ uk->reading_raw_remaining = length;
511
+ return read_raw_body_begin(uk, ext_type);
512
+ }
513
+
514
+ case 0xc9: // ext 32
515
+ {
516
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 5);
517
+ uint32_t length = _msgpack_be32(cb.u32);
518
+ int ext_type = (signed char) cb.buffer[4];
519
+ if(length == 0) {
520
+ return object_complete_ext(uk, ext_type, Qnil);
521
+ }
522
+ uk->reading_raw_remaining = length;
523
+ return read_raw_body_begin(uk, ext_type);
524
+ }
525
+
526
+ case 0xca: // float
527
+ {
528
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
529
+ cb.u32 = _msgpack_be_float(cb.u32);
530
+ return object_complete(uk, rb_float_new(cb.f));
531
+ }
532
+
533
+ case 0xcb: // double
534
+ {
535
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 8);
536
+ cb.u64 = _msgpack_be_double(cb.u64);
537
+ return object_complete(uk, rb_float_new(cb.d));
538
+ }
539
+
540
+ case 0xcc: // unsigned int 8
541
+ {
542
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
543
+ uint8_t u8 = cb.u8;
544
+ return object_complete(uk, INT2NUM((int)u8));
545
+ }
546
+
547
+ case 0xcd: // unsigned int 16
548
+ {
549
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
550
+ uint16_t u16 = _msgpack_be16(cb.u16);
551
+ return object_complete(uk, INT2NUM((int)u16));
552
+ }
553
+
554
+ case 0xce: // unsigned int 32
555
+ {
556
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
557
+ uint32_t u32 = _msgpack_be32(cb.u32);
558
+ return object_complete(uk, ULONG2NUM(u32)); // long at least 32 bits
559
+ }
560
+
561
+ case 0xcf: // unsigned int 64
562
+ {
563
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 8);
564
+ uint64_t u64 = _msgpack_be64(cb.u64);
565
+ return object_complete(uk, rb_ull2inum(u64));
566
+ }
567
+
568
+ case 0xd0: // signed int 8
569
+ {
570
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
571
+ int8_t i8 = cb.i8;
572
+ return object_complete(uk, INT2NUM((int)i8));
573
+ }
574
+
575
+ case 0xd1: // signed int 16
576
+ {
577
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
578
+ int16_t i16 = _msgpack_be16(cb.i16);
579
+ return object_complete(uk, INT2NUM((int)i16));
580
+ }
581
+
582
+ case 0xd2: // signed int 32
583
+ {
584
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
585
+ int32_t i32 = _msgpack_be32(cb.i32);
586
+ return object_complete(uk, LONG2NUM(i32)); // long at least 32 bits
587
+ }
588
+
589
+ case 0xd3: // signed int 64
590
+ {
591
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 8);
592
+ int64_t i64 = _msgpack_be64(cb.i64);
593
+ return object_complete(uk, rb_ll2inum(i64));
594
+ }
595
+
596
+ case 0xd4: // fixext 1
597
+ {
598
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
599
+ int ext_type = cb.i8;
600
+ uk->reading_raw_remaining = 1;
601
+ return read_raw_body_begin(uk, ext_type);
602
+ }
603
+
604
+ case 0xd5: // fixext 2
605
+ {
606
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
607
+ int ext_type = cb.i8;
608
+ uk->reading_raw_remaining = 2;
609
+ return read_raw_body_begin(uk, ext_type);
610
+ }
611
+
612
+ case 0xd6: // fixext 4
613
+ {
614
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
615
+ int ext_type = cb.i8;
616
+ uk->reading_raw_remaining = 4;
617
+ return read_raw_body_begin(uk, ext_type);
618
+ }
619
+
620
+ case 0xd7: // fixext 8
621
+ {
622
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
623
+ int ext_type = cb.i8;
624
+ uk->reading_raw_remaining = 8;
625
+ return read_raw_body_begin(uk, ext_type);
626
+ }
627
+
628
+ case 0xd8: // fixext 16
629
+ {
630
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
631
+ int ext_type = cb.i8;
632
+ uk->reading_raw_remaining = 16;
633
+ return read_raw_body_begin(uk, ext_type);
634
+ }
635
+
636
+
637
+ case 0xd9: // raw 8 / str 8
638
+ {
639
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
640
+ uint8_t count = cb.u8;
641
+ /* read_raw_body_begin sets uk->reading_raw */
642
+ uk->reading_raw_remaining = count;
643
+ return read_raw_body_begin(uk, RAW_TYPE_STRING);
644
+ }
645
+
646
+ case 0xda: // raw 16 / str 16
647
+ {
648
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
649
+ uint16_t count = _msgpack_be16(cb.u16);
650
+ /* read_raw_body_begin sets uk->reading_raw */
651
+ uk->reading_raw_remaining = count;
652
+ return read_raw_body_begin(uk, RAW_TYPE_STRING);
653
+ }
654
+
655
+ case 0xdb: // raw 32 / str 32
656
+ {
657
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
658
+ uint32_t count = _msgpack_be32(cb.u32);
659
+ /* read_raw_body_begin sets uk->reading_raw */
660
+ uk->reading_raw_remaining = count;
661
+ return read_raw_body_begin(uk, RAW_TYPE_STRING);
662
+ }
663
+
664
+ case 0xc4: // bin 8
665
+ {
666
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
667
+ uint8_t count = cb.u8;
668
+ /* read_raw_body_begin sets uk->reading_raw */
669
+ uk->reading_raw_remaining = count;
670
+ return read_raw_body_begin(uk, RAW_TYPE_BINARY);
671
+ }
672
+
673
+ case 0xc5: // bin 16
674
+ {
675
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
676
+ uint16_t count = _msgpack_be16(cb.u16);
677
+ /* read_raw_body_begin sets uk->reading_raw */
678
+ uk->reading_raw_remaining = count;
679
+ return read_raw_body_begin(uk, RAW_TYPE_BINARY);
680
+ }
681
+
682
+ case 0xc6: // bin 32
683
+ {
684
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
685
+ uint32_t count = _msgpack_be32(cb.u32);
686
+ /* read_raw_body_begin sets uk->reading_raw */
687
+ uk->reading_raw_remaining = count;
688
+ return read_raw_body_begin(uk, RAW_TYPE_BINARY);
689
+ }
690
+
691
+ case 0xdc: // array 16
692
+ {
693
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
694
+ uint16_t count = _msgpack_be16(cb.u16);
695
+ if(count == 0) {
696
+ return object_complete(uk, rb_ary_new());
697
+ }
698
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_ARRAY, count, rb_ary_new2(initial_buffer_size(count)));
699
+ }
700
+
701
+ case 0xdd: // array 32
702
+ {
703
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
704
+ uint32_t count = _msgpack_be32(cb.u32);
705
+ if(count == 0) {
706
+ return object_complete(uk, rb_ary_new());
707
+ }
708
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_ARRAY, count, rb_ary_new2(initial_buffer_size(count)));
709
+ }
710
+
711
+ case 0xde: // map 16
712
+ {
713
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
714
+ uint16_t count = _msgpack_be16(cb.u16);
715
+ if(count == 0) {
716
+ return object_complete(uk, rb_hash_new());
717
+ }
718
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_MAP_KEY, count*2, rb_hash_new_capa(initial_buffer_size(count)));
719
+ }
720
+
721
+ case 0xdf: // map 32
722
+ {
723
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
724
+ uint32_t count = _msgpack_be32(cb.u32);
725
+ if(count == 0) {
726
+ return object_complete(uk, rb_hash_new());
727
+ }
728
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_MAP_KEY, count*2, rb_hash_new_capa(initial_buffer_size(count)));
729
+ }
730
+
731
+ default:
732
+ return PRIMITIVE_INVALID_BYTE;
733
+ }
734
+
735
+ SWITCH_RANGE_DEFAULT
736
+ return PRIMITIVE_INVALID_BYTE;
737
+
738
+ SWITCH_RANGE_END
739
+ }
740
+
741
+ int msgpack_unpacker_read_array_header(msgpack_unpacker_t* uk, uint32_t* result_size)
742
+ {
743
+ int b = get_head_byte(uk);
744
+ if(b < 0) {
745
+ return b;
746
+ }
747
+
748
+ if(0x90 <= b && b <= 0x9f) {
749
+ *result_size = b & 0x0f;
750
+
751
+ } else if(b == 0xdc) {
752
+ /* array 16 */
753
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
754
+ *result_size = _msgpack_be16(cb.u16);
755
+
756
+ } else if(b == 0xdd) {
757
+ /* array 32 */
758
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
759
+ *result_size = _msgpack_be32(cb.u32);
760
+
761
+ } else {
762
+ return PRIMITIVE_UNEXPECTED_TYPE;
763
+ }
764
+
765
+ reset_head_byte(uk);
766
+ return 0;
767
+ }
768
+
769
+ int msgpack_unpacker_read_map_header(msgpack_unpacker_t* uk, uint32_t* result_size)
770
+ {
771
+ int b = get_head_byte(uk);
772
+ if(b < 0) {
773
+ return b;
774
+ }
775
+
776
+ if(0x80 <= b && b <= 0x8f) {
777
+ *result_size = b & 0x0f;
778
+
779
+ } else if(b == 0xde) {
780
+ /* map 16 */
781
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
782
+ *result_size = _msgpack_be16(cb.u16);
783
+
784
+ } else if(b == 0xdf) {
785
+ /* map 32 */
786
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
787
+ *result_size = _msgpack_be32(cb.u32);
788
+
789
+ } else {
790
+ return PRIMITIVE_UNEXPECTED_TYPE;
791
+ }
792
+
793
+ reset_head_byte(uk);
794
+ return 0;
795
+ }
796
+
797
+ int msgpack_unpacker_read(msgpack_unpacker_t* uk, size_t target_stack_depth)
798
+ {
799
+ STACK_INIT(uk);
800
+
801
+ while(true) {
802
+ int r = read_primitive(uk);
803
+ if(r < 0) {
804
+ if (r != PRIMITIVE_EOF) {
805
+ // We keep the stack on EOF as the parsing may be resumed.
806
+ STACK_FREE(uk);
807
+ }
808
+ return r;
809
+ }
810
+ if(r == PRIMITIVE_CONTAINER_START) {
811
+ continue;
812
+ }
813
+ /* PRIMITIVE_OBJECT_COMPLETE */
814
+
815
+ if(msgpack_unpacker_stack_is_empty(uk)) {
816
+ STACK_FREE(uk);
817
+ return PRIMITIVE_OBJECT_COMPLETE;
818
+ }
819
+
820
+ container_completed:
821
+ {
822
+ msgpack_unpacker_stack_entry_t* top = _msgpack_unpacker_stack_entry_top(uk);
823
+ switch(top->type) {
824
+ case STACK_TYPE_ARRAY:
825
+ rb_ary_push(top->object, uk->last_object);
826
+ break;
827
+ case STACK_TYPE_MAP_KEY:
828
+ top->key = uk->last_object;
829
+ top->type = STACK_TYPE_MAP_VALUE;
830
+ break;
831
+ case STACK_TYPE_MAP_VALUE:
832
+ if(uk->symbolize_keys && rb_type(top->key) == T_STRING) {
833
+ /* here uses rb_str_intern instead of rb_intern so that Ruby VM can GC unused symbols */
834
+ rb_hash_aset(top->object, rb_str_intern(top->key), uk->last_object);
835
+ } else {
836
+ rb_hash_aset(top->object, top->key, uk->last_object);
837
+ }
838
+ top->type = STACK_TYPE_MAP_KEY;
839
+ break;
840
+ case STACK_TYPE_RECURSIVE:
841
+ STACK_FREE(uk);
842
+ return PRIMITIVE_OBJECT_COMPLETE;
843
+ }
844
+ size_t count = --top->count;
845
+
846
+ if(count == 0) {
847
+ object_complete(uk, top->object);
848
+ if(msgpack_unpacker_stack_pop(uk) <= target_stack_depth) {
849
+ STACK_FREE(uk);
850
+ return PRIMITIVE_OBJECT_COMPLETE;
851
+ }
852
+ goto container_completed;
853
+ }
854
+ }
855
+ }
856
+ }
857
+
858
+ int msgpack_unpacker_skip(msgpack_unpacker_t* uk, size_t target_stack_depth)
859
+ {
860
+ STACK_INIT(uk);
861
+
862
+ while(true) {
863
+ int r = read_primitive(uk);
864
+ if(r < 0) {
865
+ STACK_FREE(uk);
866
+ return r;
867
+ }
868
+ if(r == PRIMITIVE_CONTAINER_START) {
869
+ continue;
870
+ }
871
+ /* PRIMITIVE_OBJECT_COMPLETE */
872
+
873
+ if(msgpack_unpacker_stack_is_empty(uk)) {
874
+ STACK_FREE(uk);
875
+ return PRIMITIVE_OBJECT_COMPLETE;
876
+ }
877
+
878
+ container_completed:
879
+ {
880
+ msgpack_unpacker_stack_entry_t* top = _msgpack_unpacker_stack_entry_top(uk);
881
+
882
+ /* this section optimized out */
883
+ // TODO object_complete still creates objects which should be optimized out
884
+
885
+ size_t count = --top->count;
886
+
887
+ if(count == 0) {
888
+ object_complete(uk, Qnil);
889
+ if(msgpack_unpacker_stack_pop(uk) <= target_stack_depth) {
890
+ STACK_FREE(uk);
891
+ return PRIMITIVE_OBJECT_COMPLETE;
892
+ }
893
+ goto container_completed;
894
+ }
895
+ }
896
+ }
897
+ }
898
+
899
+ int msgpack_unpacker_peek_next_object_type(msgpack_unpacker_t* uk)
900
+ {
901
+ int b = get_head_byte(uk);
902
+ if(b < 0) {
903
+ return b;
904
+ }
905
+
906
+ SWITCH_RANGE_BEGIN(b)
907
+ SWITCH_RANGE(b, 0x00, 0x7f) // Positive Fixnum
908
+ return TYPE_INTEGER;
909
+
910
+ SWITCH_RANGE(b, 0xe0, 0xff) // Negative Fixnum
911
+ return TYPE_INTEGER;
912
+
913
+ SWITCH_RANGE(b, 0xa0, 0xbf) // FixRaw
914
+ return TYPE_RAW;
915
+
916
+ SWITCH_RANGE(b, 0x90, 0x9f) // FixArray
917
+ return TYPE_ARRAY;
918
+
919
+ SWITCH_RANGE(b, 0x80, 0x8f) // FixMap
920
+ return TYPE_MAP;
921
+
922
+ SWITCH_RANGE(b, 0xc0, 0xdf) // Variable
923
+ switch(b) {
924
+ case 0xc0: // nil
925
+ return TYPE_NIL;
926
+
927
+ case 0xc2: // false
928
+ case 0xc3: // true
929
+ return TYPE_BOOLEAN;
930
+
931
+ case 0xca: // float
932
+ case 0xcb: // double
933
+ return TYPE_FLOAT;
934
+
935
+ case 0xcc: // unsigned int 8
936
+ case 0xcd: // unsigned int 16
937
+ case 0xce: // unsigned int 32
938
+ case 0xcf: // unsigned int 64
939
+ return TYPE_INTEGER;
940
+
941
+ case 0xd0: // signed int 8
942
+ case 0xd1: // signed int 16
943
+ case 0xd2: // signed int 32
944
+ case 0xd3: // signed int 64
945
+ return TYPE_INTEGER;
946
+
947
+ case 0xd9: // raw 8 / str 8
948
+ case 0xda: // raw 16 / str 16
949
+ case 0xdb: // raw 32 / str 32
950
+ return TYPE_RAW;
951
+
952
+ case 0xc4: // bin 8
953
+ case 0xc5: // bin 16
954
+ case 0xc6: // bin 32
955
+ return TYPE_RAW;
956
+
957
+ case 0xdc: // array 16
958
+ case 0xdd: // array 32
959
+ return TYPE_ARRAY;
960
+
961
+ case 0xde: // map 16
962
+ case 0xdf: // map 32
963
+ return TYPE_MAP;
964
+
965
+ default:
966
+ return PRIMITIVE_INVALID_BYTE;
967
+ }
968
+
969
+ SWITCH_RANGE_DEFAULT
970
+ return PRIMITIVE_INVALID_BYTE;
971
+
972
+ SWITCH_RANGE_END
973
+ }
974
+
975
+ int msgpack_unpacker_skip_nil(msgpack_unpacker_t* uk)
976
+ {
977
+ int b = get_head_byte(uk);
978
+ if(b < 0) {
979
+ return b;
980
+ }
981
+ if(b == 0xc0) {
982
+ return 1;
983
+ }
984
+ return 0;
985
+ }
986
+