hyper-pro-mod 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.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/hyper-pro-mod.gemspec +11 -0
  3. data/msgpack-1.8.3/ChangeLog +383 -0
  4. data/msgpack-1.8.3/LICENSE +177 -0
  5. data/msgpack-1.8.3/README.md +302 -0
  6. data/msgpack-1.8.3/ext/java/org/msgpack/jruby/Buffer.java +233 -0
  7. data/msgpack-1.8.3/ext/java/org/msgpack/jruby/Decoder.java +315 -0
  8. data/msgpack-1.8.3/ext/java/org/msgpack/jruby/Encoder.java +456 -0
  9. data/msgpack-1.8.3/ext/java/org/msgpack/jruby/ExtensionRegistry.java +167 -0
  10. data/msgpack-1.8.3/ext/java/org/msgpack/jruby/ExtensionValue.java +128 -0
  11. data/msgpack-1.8.3/ext/java/org/msgpack/jruby/Factory.java +130 -0
  12. data/msgpack-1.8.3/ext/java/org/msgpack/jruby/MessagePackLibrary.java +45 -0
  13. data/msgpack-1.8.3/ext/java/org/msgpack/jruby/Packer.java +266 -0
  14. data/msgpack-1.8.3/ext/java/org/msgpack/jruby/Types.java +37 -0
  15. data/msgpack-1.8.3/ext/java/org/msgpack/jruby/Unpacker.java +336 -0
  16. data/msgpack-1.8.3/ext/msgpack/buffer.c +672 -0
  17. data/msgpack-1.8.3/ext/msgpack/buffer.h +604 -0
  18. data/msgpack-1.8.3/ext/msgpack/buffer_class.c +616 -0
  19. data/msgpack-1.8.3/ext/msgpack/buffer_class.h +33 -0
  20. data/msgpack-1.8.3/ext/msgpack/compat.h +26 -0
  21. data/msgpack-1.8.3/ext/msgpack/extconf.rb +53 -0
  22. data/msgpack-1.8.3/ext/msgpack/extension_value_class.c +34 -0
  23. data/msgpack-1.8.3/ext/msgpack/extension_value_class.h +31 -0
  24. data/msgpack-1.8.3/ext/msgpack/factory_class.c +276 -0
  25. data/msgpack-1.8.3/ext/msgpack/factory_class.h +33 -0
  26. data/msgpack-1.8.3/ext/msgpack/packer.c +199 -0
  27. data/msgpack-1.8.3/ext/msgpack/packer.h +513 -0
  28. data/msgpack-1.8.3/ext/msgpack/packer_class.c +442 -0
  29. data/msgpack-1.8.3/ext/msgpack/packer_class.h +43 -0
  30. data/msgpack-1.8.3/ext/msgpack/packer_ext_registry.c +74 -0
  31. data/msgpack-1.8.3/ext/msgpack/packer_ext_registry.h +140 -0
  32. data/msgpack-1.8.3/ext/msgpack/rbinit.c +35 -0
  33. data/msgpack-1.8.3/ext/msgpack/rmem.c +93 -0
  34. data/msgpack-1.8.3/ext/msgpack/rmem.h +109 -0
  35. data/msgpack-1.8.3/ext/msgpack/sysdep.h +118 -0
  36. data/msgpack-1.8.3/ext/msgpack/sysdep_endian.h +50 -0
  37. data/msgpack-1.8.3/ext/msgpack/sysdep_types.h +46 -0
  38. data/msgpack-1.8.3/ext/msgpack/unpacker.c +987 -0
  39. data/msgpack-1.8.3/ext/msgpack/unpacker.h +152 -0
  40. data/msgpack-1.8.3/ext/msgpack/unpacker_class.c +447 -0
  41. data/msgpack-1.8.3/ext/msgpack/unpacker_class.h +43 -0
  42. data/msgpack-1.8.3/ext/msgpack/unpacker_ext_registry.c +74 -0
  43. data/msgpack-1.8.3/ext/msgpack/unpacker_ext_registry.h +62 -0
  44. data/msgpack-1.8.3/lib/msgpack/bigint.rb +69 -0
  45. data/msgpack-1.8.3/lib/msgpack/buffer.rb +9 -0
  46. data/msgpack-1.8.3/lib/msgpack/core_ext.rb +139 -0
  47. data/msgpack-1.8.3/lib/msgpack/factory.rb +211 -0
  48. data/msgpack-1.8.3/lib/msgpack/packer.rb +37 -0
  49. data/msgpack-1.8.3/lib/msgpack/symbol.rb +26 -0
  50. data/msgpack-1.8.3/lib/msgpack/time.rb +29 -0
  51. data/msgpack-1.8.3/lib/msgpack/timestamp.rb +76 -0
  52. data/msgpack-1.8.3/lib/msgpack/unpacker.rb +41 -0
  53. data/msgpack-1.8.3/lib/msgpack/version.rb +6 -0
  54. data/msgpack-1.8.3/lib/msgpack.rb +48 -0
  55. data/msgpack-1.8.3/msgpack.gemspec +41 -0
  56. metadata +94 -0
@@ -0,0 +1,987 @@
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
+ size_t buffered_size = msgpack_buffer_all_readable_size(UNPACKER_BUFFER_(uk));
340
+ uk->reading_raw = rb_str_buf_new(length > buffered_size ? buffered_size : length);
341
+ }
342
+
343
+ do {
344
+ size_t n = msgpack_buffer_read_to_string(UNPACKER_BUFFER_(uk), uk->reading_raw, length);
345
+ if(n == 0) {
346
+ return PRIMITIVE_EOF;
347
+ }
348
+ /* update reading_raw_remaining everytime because
349
+ * msgpack_buffer_read_to_string raises IOError */
350
+ uk->reading_raw_remaining = length = length - n;
351
+ } while(length > 0);
352
+
353
+ int ret;
354
+ if(uk->reading_raw_type == RAW_TYPE_STRING) {
355
+ ENCODING_SET(uk->reading_raw, msgpack_rb_encindex_utf8);
356
+ ret = object_complete(uk, uk->reading_raw);
357
+ } else if (uk->reading_raw_type == RAW_TYPE_BINARY) {
358
+ ret = object_complete(uk, uk->reading_raw);
359
+ } else {
360
+ ret = object_complete_ext(uk, uk->reading_raw_type, uk->reading_raw);
361
+ }
362
+ uk->reading_raw = Qnil;
363
+ return ret;
364
+ }
365
+
366
+ static inline int read_raw_body_begin(msgpack_unpacker_t* uk, int raw_type)
367
+ {
368
+ /* assuming uk->reading_raw == Qnil */
369
+
370
+ int ext_flags;
371
+ VALUE proc;
372
+
373
+ if(!(raw_type == RAW_TYPE_STRING || raw_type == RAW_TYPE_BINARY)) {
374
+ proc = msgpack_unpacker_ext_registry_lookup(uk->ext_registry, raw_type, &ext_flags);
375
+ if(proc != Qnil && ext_flags & MSGPACK_EXT_RECURSIVE) {
376
+ VALUE obj;
377
+ uk->last_object = Qnil;
378
+ reset_head_byte(uk);
379
+ uk->reading_raw_remaining = 0;
380
+
381
+ _msgpack_unpacker_stack_push(uk, STACK_TYPE_RECURSIVE, 1, Qnil);
382
+ int raised;
383
+ obj = protected_proc_call(proc, 1, &uk->self, &raised);
384
+ msgpack_unpacker_stack_pop(uk);
385
+
386
+ if (raised) {
387
+ uk->last_object = rb_errinfo();
388
+ return PRIMITIVE_RECURSIVE_RAISED;
389
+ }
390
+
391
+ return object_complete(uk, obj);
392
+ }
393
+ }
394
+
395
+ /* try optimized read */
396
+ size_t length = uk->reading_raw_remaining;
397
+ if(length <= msgpack_buffer_top_readable_size(UNPACKER_BUFFER_(uk))) {
398
+ int ret;
399
+ if ((uk->optimized_symbol_ext_type && uk->symbol_ext_type == raw_type)) {
400
+ VALUE symbol = msgpack_buffer_read_top_as_symbol(UNPACKER_BUFFER_(uk), length, raw_type != RAW_TYPE_BINARY);
401
+ ret = object_complete_symbol(uk, symbol);
402
+ } else if (is_reading_map_key(uk) && raw_type == RAW_TYPE_STRING) {
403
+ /* don't use zerocopy for hash keys but get a frozen string directly
404
+ * because rb_hash_aset freezes keys and it causes copying */
405
+ VALUE key;
406
+ if (uk->symbolize_keys) {
407
+ if (uk->use_key_cache) {
408
+ key = msgpack_buffer_read_top_as_interned_symbol(UNPACKER_BUFFER_(uk), &uk->key_cache, length);
409
+ } else {
410
+ key = msgpack_buffer_read_top_as_symbol(UNPACKER_BUFFER_(uk), length, true);
411
+ }
412
+ ret = object_complete_symbol(uk, key);
413
+ } else {
414
+ if (uk->use_key_cache) {
415
+ key = msgpack_buffer_read_top_as_interned_string(UNPACKER_BUFFER_(uk), &uk->key_cache, length);
416
+ } else {
417
+ key = msgpack_buffer_read_top_as_string(UNPACKER_BUFFER_(uk), length, true, true);
418
+ }
419
+
420
+ ret = object_complete(uk, key);
421
+ }
422
+ } else {
423
+ bool will_freeze = uk->freeze;
424
+ if(raw_type == RAW_TYPE_STRING || raw_type == RAW_TYPE_BINARY) {
425
+ VALUE string = msgpack_buffer_read_top_as_string(UNPACKER_BUFFER_(uk), length, will_freeze, raw_type == RAW_TYPE_STRING);
426
+ ret = object_complete(uk, string);
427
+ } else {
428
+ VALUE string = msgpack_buffer_read_top_as_string(UNPACKER_BUFFER_(uk), length, false, false);
429
+ ret = object_complete_ext(uk, raw_type, string);
430
+ }
431
+ }
432
+ uk->reading_raw_remaining = 0;
433
+ return ret;
434
+ }
435
+
436
+ uk->reading_raw_type = raw_type;
437
+ return read_raw_body_cont(uk);
438
+ }
439
+
440
+ static int read_primitive(msgpack_unpacker_t* uk)
441
+ {
442
+ if(uk->reading_raw_remaining > 0) {
443
+ return read_raw_body_cont(uk);
444
+ }
445
+
446
+ int b = get_head_byte(uk);
447
+ if(b < 0) {
448
+ return b;
449
+ }
450
+
451
+ SWITCH_RANGE_BEGIN(b)
452
+ SWITCH_RANGE(b, 0x00, 0x7f) // Positive Fixnum
453
+ return object_complete(uk, INT2NUM(b));
454
+
455
+ SWITCH_RANGE(b, 0xe0, 0xff) // Negative Fixnum
456
+ return object_complete(uk, INT2NUM((int8_t)b));
457
+
458
+ SWITCH_RANGE(b, 0xa0, 0xbf) // FixRaw / fixstr
459
+ size_t count = b & 0x1f;
460
+ /* read_raw_body_begin sets uk->reading_raw */
461
+ uk->reading_raw_remaining = count;
462
+ return read_raw_body_begin(uk, RAW_TYPE_STRING);
463
+
464
+ SWITCH_RANGE(b, 0x90, 0x9f) // FixArray
465
+ size_t count = b & 0x0f;
466
+ if(count == 0) {
467
+ return object_complete(uk, rb_ary_new());
468
+ }
469
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_ARRAY, count, rb_ary_new2(initial_buffer_size(count)));
470
+
471
+ SWITCH_RANGE(b, 0x80, 0x8f) // FixMap
472
+ int count = b & 0x0f;
473
+ if(count == 0) {
474
+ return object_complete(uk, rb_hash_new());
475
+ }
476
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_MAP_KEY, count*2, rb_hash_new_capa(initial_buffer_size(count)));
477
+
478
+ SWITCH_RANGE(b, 0xc0, 0xdf) // Variable
479
+ switch(b) {
480
+ case 0xc0: // nil
481
+ return object_complete(uk, Qnil);
482
+
483
+ //case 0xc1: // string
484
+
485
+ case 0xc2: // false
486
+ return object_complete(uk, Qfalse);
487
+
488
+ case 0xc3: // true
489
+ return object_complete(uk, Qtrue);
490
+
491
+ case 0xc7: // ext 8
492
+ {
493
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
494
+ uint8_t length = cb.u8;
495
+ int ext_type = (signed char) cb.buffer[1];
496
+ if(length == 0) {
497
+ return object_complete_ext(uk, ext_type, Qnil);
498
+ }
499
+ uk->reading_raw_remaining = length;
500
+ return read_raw_body_begin(uk, ext_type);
501
+ }
502
+
503
+ case 0xc8: // ext 16
504
+ {
505
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 3);
506
+ uint16_t length = _msgpack_be16(cb.u16);
507
+ int ext_type = (signed char) cb.buffer[2];
508
+ if(length == 0) {
509
+ return object_complete_ext(uk, ext_type, Qnil);
510
+ }
511
+ uk->reading_raw_remaining = length;
512
+ return read_raw_body_begin(uk, ext_type);
513
+ }
514
+
515
+ case 0xc9: // ext 32
516
+ {
517
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 5);
518
+ uint32_t length = _msgpack_be32(cb.u32);
519
+ int ext_type = (signed char) cb.buffer[4];
520
+ if(length == 0) {
521
+ return object_complete_ext(uk, ext_type, Qnil);
522
+ }
523
+ uk->reading_raw_remaining = length;
524
+ return read_raw_body_begin(uk, ext_type);
525
+ }
526
+
527
+ case 0xca: // float
528
+ {
529
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
530
+ cb.u32 = _msgpack_be_float(cb.u32);
531
+ return object_complete(uk, rb_float_new(cb.f));
532
+ }
533
+
534
+ case 0xcb: // double
535
+ {
536
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 8);
537
+ cb.u64 = _msgpack_be_double(cb.u64);
538
+ return object_complete(uk, rb_float_new(cb.d));
539
+ }
540
+
541
+ case 0xcc: // unsigned int 8
542
+ {
543
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
544
+ uint8_t u8 = cb.u8;
545
+ return object_complete(uk, INT2NUM((int)u8));
546
+ }
547
+
548
+ case 0xcd: // unsigned int 16
549
+ {
550
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
551
+ uint16_t u16 = _msgpack_be16(cb.u16);
552
+ return object_complete(uk, INT2NUM((int)u16));
553
+ }
554
+
555
+ case 0xce: // unsigned int 32
556
+ {
557
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
558
+ uint32_t u32 = _msgpack_be32(cb.u32);
559
+ return object_complete(uk, ULONG2NUM(u32)); // long at least 32 bits
560
+ }
561
+
562
+ case 0xcf: // unsigned int 64
563
+ {
564
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 8);
565
+ uint64_t u64 = _msgpack_be64(cb.u64);
566
+ return object_complete(uk, rb_ull2inum(u64));
567
+ }
568
+
569
+ case 0xd0: // signed int 8
570
+ {
571
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
572
+ int8_t i8 = cb.i8;
573
+ return object_complete(uk, INT2NUM((int)i8));
574
+ }
575
+
576
+ case 0xd1: // signed int 16
577
+ {
578
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
579
+ int16_t i16 = _msgpack_be16(cb.i16);
580
+ return object_complete(uk, INT2NUM((int)i16));
581
+ }
582
+
583
+ case 0xd2: // signed int 32
584
+ {
585
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
586
+ int32_t i32 = _msgpack_be32(cb.i32);
587
+ return object_complete(uk, LONG2NUM(i32)); // long at least 32 bits
588
+ }
589
+
590
+ case 0xd3: // signed int 64
591
+ {
592
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 8);
593
+ int64_t i64 = _msgpack_be64(cb.i64);
594
+ return object_complete(uk, rb_ll2inum(i64));
595
+ }
596
+
597
+ case 0xd4: // fixext 1
598
+ {
599
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
600
+ int ext_type = cb.i8;
601
+ uk->reading_raw_remaining = 1;
602
+ return read_raw_body_begin(uk, ext_type);
603
+ }
604
+
605
+ case 0xd5: // fixext 2
606
+ {
607
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
608
+ int ext_type = cb.i8;
609
+ uk->reading_raw_remaining = 2;
610
+ return read_raw_body_begin(uk, ext_type);
611
+ }
612
+
613
+ case 0xd6: // fixext 4
614
+ {
615
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
616
+ int ext_type = cb.i8;
617
+ uk->reading_raw_remaining = 4;
618
+ return read_raw_body_begin(uk, ext_type);
619
+ }
620
+
621
+ case 0xd7: // fixext 8
622
+ {
623
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
624
+ int ext_type = cb.i8;
625
+ uk->reading_raw_remaining = 8;
626
+ return read_raw_body_begin(uk, ext_type);
627
+ }
628
+
629
+ case 0xd8: // fixext 16
630
+ {
631
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
632
+ int ext_type = cb.i8;
633
+ uk->reading_raw_remaining = 16;
634
+ return read_raw_body_begin(uk, ext_type);
635
+ }
636
+
637
+
638
+ case 0xd9: // raw 8 / str 8
639
+ {
640
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
641
+ size_t count = cb.u8;
642
+ /* read_raw_body_begin sets uk->reading_raw */
643
+ uk->reading_raw_remaining = count;
644
+ return read_raw_body_begin(uk, RAW_TYPE_STRING);
645
+ }
646
+
647
+ case 0xda: // raw 16 / str 16
648
+ {
649
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
650
+ size_t count = _msgpack_be16(cb.u16);
651
+ /* read_raw_body_begin sets uk->reading_raw */
652
+ uk->reading_raw_remaining = count;
653
+ return read_raw_body_begin(uk, RAW_TYPE_STRING);
654
+ }
655
+
656
+ case 0xdb: // raw 32 / str 32
657
+ {
658
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
659
+ size_t count = _msgpack_be32(cb.u32);
660
+ /* read_raw_body_begin sets uk->reading_raw */
661
+ uk->reading_raw_remaining = count;
662
+ return read_raw_body_begin(uk, RAW_TYPE_STRING);
663
+ }
664
+
665
+ case 0xc4: // bin 8
666
+ {
667
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
668
+ size_t count = cb.u8;
669
+ /* read_raw_body_begin sets uk->reading_raw */
670
+ uk->reading_raw_remaining = count;
671
+ return read_raw_body_begin(uk, RAW_TYPE_BINARY);
672
+ }
673
+
674
+ case 0xc5: // bin 16
675
+ {
676
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
677
+ size_t count = _msgpack_be16(cb.u16);
678
+ /* read_raw_body_begin sets uk->reading_raw */
679
+ uk->reading_raw_remaining = count;
680
+ return read_raw_body_begin(uk, RAW_TYPE_BINARY);
681
+ }
682
+
683
+ case 0xc6: // bin 32
684
+ {
685
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
686
+ size_t count = _msgpack_be32(cb.u32);
687
+ /* read_raw_body_begin sets uk->reading_raw */
688
+ uk->reading_raw_remaining = count;
689
+ return read_raw_body_begin(uk, RAW_TYPE_BINARY);
690
+ }
691
+
692
+ case 0xdc: // array 16
693
+ {
694
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
695
+ size_t count = _msgpack_be16(cb.u16);
696
+ if(count == 0) {
697
+ return object_complete(uk, rb_ary_new());
698
+ }
699
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_ARRAY, count, rb_ary_new2(initial_buffer_size(count)));
700
+ }
701
+
702
+ case 0xdd: // array 32
703
+ {
704
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
705
+ size_t count = _msgpack_be32(cb.u32);
706
+ if(count == 0) {
707
+ return object_complete(uk, rb_ary_new());
708
+ }
709
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_ARRAY, count, rb_ary_new2(initial_buffer_size(count)));
710
+ }
711
+
712
+ case 0xde: // map 16
713
+ {
714
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
715
+ size_t count = _msgpack_be16(cb.u16);
716
+ if(count == 0) {
717
+ return object_complete(uk, rb_hash_new());
718
+ }
719
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_MAP_KEY, count*2, rb_hash_new_capa(initial_buffer_size(count)));
720
+ }
721
+
722
+ case 0xdf: // map 32
723
+ {
724
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
725
+ size_t count = _msgpack_be32(cb.u32);
726
+ if(count == 0) {
727
+ return object_complete(uk, rb_hash_new());
728
+ }
729
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_MAP_KEY, count*2, rb_hash_new_capa(initial_buffer_size(count)));
730
+ }
731
+
732
+ default:
733
+ return PRIMITIVE_INVALID_BYTE;
734
+ }
735
+
736
+ SWITCH_RANGE_DEFAULT
737
+ return PRIMITIVE_INVALID_BYTE;
738
+
739
+ SWITCH_RANGE_END
740
+ }
741
+
742
+ int msgpack_unpacker_read_array_header(msgpack_unpacker_t* uk, uint32_t* result_size)
743
+ {
744
+ int b = get_head_byte(uk);
745
+ if(b < 0) {
746
+ return b;
747
+ }
748
+
749
+ if(0x90 <= b && b <= 0x9f) {
750
+ *result_size = b & 0x0f;
751
+
752
+ } else if(b == 0xdc) {
753
+ /* array 16 */
754
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
755
+ *result_size = _msgpack_be16(cb.u16);
756
+
757
+ } else if(b == 0xdd) {
758
+ /* array 32 */
759
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
760
+ *result_size = _msgpack_be32(cb.u32);
761
+
762
+ } else {
763
+ return PRIMITIVE_UNEXPECTED_TYPE;
764
+ }
765
+
766
+ reset_head_byte(uk);
767
+ return 0;
768
+ }
769
+
770
+ int msgpack_unpacker_read_map_header(msgpack_unpacker_t* uk, uint32_t* result_size)
771
+ {
772
+ int b = get_head_byte(uk);
773
+ if(b < 0) {
774
+ return b;
775
+ }
776
+
777
+ if(0x80 <= b && b <= 0x8f) {
778
+ *result_size = b & 0x0f;
779
+
780
+ } else if(b == 0xde) {
781
+ /* map 16 */
782
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
783
+ *result_size = _msgpack_be16(cb.u16);
784
+
785
+ } else if(b == 0xdf) {
786
+ /* map 32 */
787
+ READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
788
+ *result_size = _msgpack_be32(cb.u32);
789
+
790
+ } else {
791
+ return PRIMITIVE_UNEXPECTED_TYPE;
792
+ }
793
+
794
+ reset_head_byte(uk);
795
+ return 0;
796
+ }
797
+
798
+ int msgpack_unpacker_read(msgpack_unpacker_t* uk, size_t target_stack_depth)
799
+ {
800
+ STACK_INIT(uk);
801
+
802
+ while(true) {
803
+ int r = read_primitive(uk);
804
+ if(r < 0) {
805
+ if (r != PRIMITIVE_EOF) {
806
+ // We keep the stack on EOF as the parsing may be resumed.
807
+ STACK_FREE(uk);
808
+ }
809
+ return r;
810
+ }
811
+ if(r == PRIMITIVE_CONTAINER_START) {
812
+ continue;
813
+ }
814
+ /* PRIMITIVE_OBJECT_COMPLETE */
815
+
816
+ if(msgpack_unpacker_stack_is_empty(uk)) {
817
+ STACK_FREE(uk);
818
+ return PRIMITIVE_OBJECT_COMPLETE;
819
+ }
820
+
821
+ container_completed:
822
+ {
823
+ msgpack_unpacker_stack_entry_t* top = _msgpack_unpacker_stack_entry_top(uk);
824
+ switch(top->type) {
825
+ case STACK_TYPE_ARRAY:
826
+ rb_ary_push(top->object, uk->last_object);
827
+ break;
828
+ case STACK_TYPE_MAP_KEY:
829
+ top->key = uk->last_object;
830
+ top->type = STACK_TYPE_MAP_VALUE;
831
+ break;
832
+ case STACK_TYPE_MAP_VALUE:
833
+ if(uk->symbolize_keys && rb_type(top->key) == T_STRING) {
834
+ /* here uses rb_str_intern instead of rb_intern so that Ruby VM can GC unused symbols */
835
+ rb_hash_aset(top->object, rb_str_intern(top->key), uk->last_object);
836
+ } else {
837
+ rb_hash_aset(top->object, top->key, uk->last_object);
838
+ }
839
+ top->type = STACK_TYPE_MAP_KEY;
840
+ break;
841
+ case STACK_TYPE_RECURSIVE:
842
+ STACK_FREE(uk);
843
+ return PRIMITIVE_OBJECT_COMPLETE;
844
+ }
845
+ size_t count = --top->count;
846
+
847
+ if(count == 0) {
848
+ object_complete(uk, top->object);
849
+ if(msgpack_unpacker_stack_pop(uk) <= target_stack_depth) {
850
+ STACK_FREE(uk);
851
+ return PRIMITIVE_OBJECT_COMPLETE;
852
+ }
853
+ goto container_completed;
854
+ }
855
+ }
856
+ }
857
+ }
858
+
859
+ int msgpack_unpacker_skip(msgpack_unpacker_t* uk, size_t target_stack_depth)
860
+ {
861
+ STACK_INIT(uk);
862
+
863
+ while(true) {
864
+ int r = read_primitive(uk);
865
+ if(r < 0) {
866
+ STACK_FREE(uk);
867
+ return r;
868
+ }
869
+ if(r == PRIMITIVE_CONTAINER_START) {
870
+ continue;
871
+ }
872
+ /* PRIMITIVE_OBJECT_COMPLETE */
873
+
874
+ if(msgpack_unpacker_stack_is_empty(uk)) {
875
+ STACK_FREE(uk);
876
+ return PRIMITIVE_OBJECT_COMPLETE;
877
+ }
878
+
879
+ container_completed:
880
+ {
881
+ msgpack_unpacker_stack_entry_t* top = _msgpack_unpacker_stack_entry_top(uk);
882
+
883
+ /* this section optimized out */
884
+ // TODO object_complete still creates objects which should be optimized out
885
+
886
+ size_t count = --top->count;
887
+
888
+ if(count == 0) {
889
+ object_complete(uk, Qnil);
890
+ if(msgpack_unpacker_stack_pop(uk) <= target_stack_depth) {
891
+ STACK_FREE(uk);
892
+ return PRIMITIVE_OBJECT_COMPLETE;
893
+ }
894
+ goto container_completed;
895
+ }
896
+ }
897
+ }
898
+ }
899
+
900
+ int msgpack_unpacker_peek_next_object_type(msgpack_unpacker_t* uk)
901
+ {
902
+ int b = get_head_byte(uk);
903
+ if(b < 0) {
904
+ return b;
905
+ }
906
+
907
+ SWITCH_RANGE_BEGIN(b)
908
+ SWITCH_RANGE(b, 0x00, 0x7f) // Positive Fixnum
909
+ return TYPE_INTEGER;
910
+
911
+ SWITCH_RANGE(b, 0xe0, 0xff) // Negative Fixnum
912
+ return TYPE_INTEGER;
913
+
914
+ SWITCH_RANGE(b, 0xa0, 0xbf) // FixRaw
915
+ return TYPE_RAW;
916
+
917
+ SWITCH_RANGE(b, 0x90, 0x9f) // FixArray
918
+ return TYPE_ARRAY;
919
+
920
+ SWITCH_RANGE(b, 0x80, 0x8f) // FixMap
921
+ return TYPE_MAP;
922
+
923
+ SWITCH_RANGE(b, 0xc0, 0xdf) // Variable
924
+ switch(b) {
925
+ case 0xc0: // nil
926
+ return TYPE_NIL;
927
+
928
+ case 0xc2: // false
929
+ case 0xc3: // true
930
+ return TYPE_BOOLEAN;
931
+
932
+ case 0xca: // float
933
+ case 0xcb: // double
934
+ return TYPE_FLOAT;
935
+
936
+ case 0xcc: // unsigned int 8
937
+ case 0xcd: // unsigned int 16
938
+ case 0xce: // unsigned int 32
939
+ case 0xcf: // unsigned int 64
940
+ return TYPE_INTEGER;
941
+
942
+ case 0xd0: // signed int 8
943
+ case 0xd1: // signed int 16
944
+ case 0xd2: // signed int 32
945
+ case 0xd3: // signed int 64
946
+ return TYPE_INTEGER;
947
+
948
+ case 0xd9: // raw 8 / str 8
949
+ case 0xda: // raw 16 / str 16
950
+ case 0xdb: // raw 32 / str 32
951
+ return TYPE_RAW;
952
+
953
+ case 0xc4: // bin 8
954
+ case 0xc5: // bin 16
955
+ case 0xc6: // bin 32
956
+ return TYPE_RAW;
957
+
958
+ case 0xdc: // array 16
959
+ case 0xdd: // array 32
960
+ return TYPE_ARRAY;
961
+
962
+ case 0xde: // map 16
963
+ case 0xdf: // map 32
964
+ return TYPE_MAP;
965
+
966
+ default:
967
+ return PRIMITIVE_INVALID_BYTE;
968
+ }
969
+
970
+ SWITCH_RANGE_DEFAULT
971
+ return PRIMITIVE_INVALID_BYTE;
972
+
973
+ SWITCH_RANGE_END
974
+ }
975
+
976
+ int msgpack_unpacker_skip_nil(msgpack_unpacker_t* uk)
977
+ {
978
+ int b = get_head_byte(uk);
979
+ if(b < 0) {
980
+ return b;
981
+ }
982
+ if(b == 0xc0) {
983
+ return 1;
984
+ }
985
+ return 0;
986
+ }
987
+