cbor 0.5.10.3 → 0.6.2.2
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.
- checksums.yaml +4 -4
- data/ChangeLog +21 -0
- data/README.rdoc +26 -0
- data/ext/cbor/buffer_class.c +54 -8
- data/ext/cbor/core_ext.c +1 -3
- data/ext/cbor/packer_class.c +48 -7
- data/ext/cbor/packer_class.h +2 -1
- data/ext/cbor/rbinit.c +6 -0
- data/ext/cbor/unpacker.c +13 -2
- data/ext/cbor/unpacker.h +3 -1
- data/ext/cbor/unpacker_class.c +63 -8
- data/lib/cbor/version.rb +1 -1
- data/spec/format_spec.rb +19 -11
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7f5496d45110e3e4bacb9c4cd725ab7331e3bbbf873a8264099cb101612e13be
|
|
4
|
+
data.tar.gz: ed19073bb8575be84f9ba68f3d21e5aeb5633cd914302d2e386178c2a9d0312d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9e4c6576cd258c83d89acaefdd92017c2d0f572c5e8fd2e3c41fdf69df0e665801c7c7083cbeee4eaf88a5ac5cf767792f8c3b8445887ee0a3144f673eb5aaa9
|
|
7
|
+
data.tar.gz: c1367b8a9f37bbb5ddda5ccc4a73be44c448b240f49ea323a0cbc003b7bf2a33f089f7190ebb40870d955f1a4c1f0aee9de0182dde243df70c198da2944936f9
|
data/ChangeLog
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
2026-11-08 Carsten Bormann <cabo@tzi.org>
|
|
2
|
+
|
|
3
|
+
* Safer decode defaults (BREAKING). Object-constructing
|
|
4
|
+
tags are no longer reconstructed into live Ruby objects by default,
|
|
5
|
+
because CBOR.decode is commonly run on input from untrusted sources.
|
|
6
|
+
Tag 35 (Regexp) and tag 32 (URI) now decode to a generic CBOR::Tagged
|
|
7
|
+
value instead of building a Regexp or URI from the decoded value. A
|
|
8
|
+
caller that trusts the source opts in per tag:
|
|
9
|
+
|
|
10
|
+
CBOR.decode(data) # CBOR::Tagged
|
|
11
|
+
CBOR.decode(data, reconstruct_from_tags: [CBOR::TAG_REGEXP])
|
|
12
|
+
CBOR.decode(data, reconstruct_from_tags: [CBOR::TAG_URI])
|
|
13
|
+
|
|
14
|
+
Module constants CBOR::TAG_REGEXP (35) and CBOR::TAG_URI (32) are
|
|
15
|
+
provided; bare integers are also accepted. Tag 0/1 (time) stays on
|
|
16
|
+
by default, as reconstructing a Time from an epoch is a plain value
|
|
17
|
+
conversion. The option works for both CBOR.decode and Unpacker.new,
|
|
18
|
+
like the existing symbolize_keys option. This mirrors the principle
|
|
19
|
+
behind YAML.safe_load and JSON.parse(create_additions: false):
|
|
20
|
+
turning wire data into live objects should be opt-in.
|
|
21
|
+
|
|
1
22
|
2016-04-11 Carsten Bormann <cabo@tzi.org>
|
|
2
23
|
|
|
3
24
|
* 0.5.9.1: Fix WIN32 problem inherited from upstream
|
data/README.rdoc
CHANGED
|
@@ -15,6 +15,32 @@ Use RubyGems to install:
|
|
|
15
15
|
|
|
16
16
|
gem install cbor
|
|
17
17
|
|
|
18
|
+
== Decoding untrusted input
|
|
19
|
+
|
|
20
|
+
+CBOR.decode+ is commonly run on data from the network. By default it
|
|
21
|
+
does not turn object-constructing tags into live Ruby objects, since
|
|
22
|
+
decoding data from an untrusted source should not build objects the
|
|
23
|
+
caller never asked for. Tag 35 (Regexp) and tag 32 (URI) decode to a
|
|
24
|
+
generic +CBOR::Tagged+ value instead of building a +Regexp+ or +URI+
|
|
25
|
+
from the decoded value:
|
|
26
|
+
|
|
27
|
+
CBOR.decode(data) #=> #<struct CBOR::Tagged tag=35, value="foo">
|
|
28
|
+
|
|
29
|
+
A caller that trusts the source opts in, one tag at a time:
|
|
30
|
+
|
|
31
|
+
CBOR.decode(data, reconstruct_from_tags: [CBOR::TAG_REGEXP]) #=> /foo/
|
|
32
|
+
CBOR.decode(data, reconstruct_from_tags: [CBOR::TAG_URI]) #=> #<URI::HTTP ...>
|
|
33
|
+
CBOR.decode(data, reconstruct_from_tags: [CBOR::TAG_REGEXP, CBOR::TAG_URI])
|
|
34
|
+
|
|
35
|
+
+CBOR::TAG_REGEXP+ (35) and +CBOR::TAG_URI+ (32) are module constants;
|
|
36
|
+
bare integers work too. Tag 0/1 (time) stays on by default, since
|
|
37
|
+
reconstructing a +Time+ from an epoch is a plain value conversion. The
|
|
38
|
+
option works for both +CBOR.decode+ and <tt>CBOR::Unpacker.new</tt>.
|
|
39
|
+
|
|
40
|
+
This follows the same principle as <tt>YAML.safe_load</tt> and
|
|
41
|
+
<tt>JSON.parse(create_additions: false)</tt>: deserializing wire data
|
|
42
|
+
into live objects should be opt-in.
|
|
43
|
+
|
|
18
44
|
CBOR is an object representation format defined by the IETF[http://ietf.org].
|
|
19
45
|
The specification[http://tools.ietf.org/html/rfc8949]
|
|
20
46
|
is an IETF Standards-Track specification
|
data/ext/cbor/buffer_class.c
CHANGED
|
@@ -38,9 +38,10 @@ static ID s_write;
|
|
|
38
38
|
static ID s_append;
|
|
39
39
|
static ID s_close;
|
|
40
40
|
|
|
41
|
+
static msgpack_buffer_t* Buffer_get(VALUE self);
|
|
42
|
+
|
|
41
43
|
#define BUFFER(from, name) \
|
|
42
|
-
msgpack_buffer_t *name =
|
|
43
|
-
Data_Get_Struct(from, msgpack_buffer_t, name); \
|
|
44
|
+
msgpack_buffer_t *name = Buffer_get(from); \
|
|
44
45
|
if(name == NULL) { \
|
|
45
46
|
rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
|
|
46
47
|
}
|
|
@@ -51,22 +52,64 @@ static ID s_close;
|
|
|
51
52
|
rb_raise(rb_eTypeError, "instance of String needed"); \
|
|
52
53
|
}
|
|
53
54
|
|
|
55
|
+
static void Buffer_mark(void* data)
|
|
56
|
+
{
|
|
57
|
+
msgpack_buffer_t* b = data;
|
|
58
|
+
msgpack_buffer_mark(b);
|
|
59
|
+
}
|
|
60
|
+
|
|
54
61
|
static void Buffer_free(void* data)
|
|
55
62
|
{
|
|
56
63
|
if(data == NULL) {
|
|
57
64
|
return;
|
|
58
65
|
}
|
|
59
|
-
msgpack_buffer_t* b =
|
|
66
|
+
msgpack_buffer_t* b = data;
|
|
60
67
|
msgpack_buffer_destroy(b);
|
|
61
|
-
xfree(
|
|
68
|
+
xfree(data);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
#ifdef TypedData_Make_Struct
|
|
72
|
+
static size_t Buffer_memsize(const void* data)
|
|
73
|
+
{
|
|
74
|
+
return data == NULL ? 0 : sizeof(msgpack_buffer_t);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static const rb_data_type_t buffer_data_type = {
|
|
78
|
+
"CBOR::Buffer",
|
|
79
|
+
{Buffer_mark, Buffer_free, Buffer_memsize,},
|
|
80
|
+
NULL, NULL, 0
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
static const rb_data_type_t wrapped_buffer_data_type = {
|
|
84
|
+
"CBOR::WrappedBuffer",
|
|
85
|
+
{Buffer_mark, NULL, Buffer_memsize,},
|
|
86
|
+
&buffer_data_type, NULL, 0
|
|
87
|
+
};
|
|
88
|
+
#endif
|
|
89
|
+
|
|
90
|
+
static msgpack_buffer_t* Buffer_get(VALUE self)
|
|
91
|
+
{
|
|
92
|
+
msgpack_buffer_t* b = NULL;
|
|
93
|
+
#ifdef TypedData_Make_Struct
|
|
94
|
+
TypedData_Get_Struct(self, msgpack_buffer_t, &buffer_data_type, b);
|
|
95
|
+
#else
|
|
96
|
+
Data_Get_Struct(self, msgpack_buffer_t, b);
|
|
97
|
+
#endif
|
|
98
|
+
return b;
|
|
62
99
|
}
|
|
63
100
|
|
|
64
101
|
static VALUE Buffer_alloc(VALUE klass)
|
|
65
102
|
{
|
|
66
|
-
msgpack_buffer_t* b
|
|
103
|
+
msgpack_buffer_t* b;
|
|
104
|
+
VALUE self;
|
|
105
|
+
#ifdef TypedData_Make_Struct
|
|
106
|
+
self = TypedData_Make_Struct(klass, msgpack_buffer_t, &buffer_data_type, b);
|
|
107
|
+
#else
|
|
108
|
+
self = Data_Make_Struct(klass, msgpack_buffer_t, Buffer_mark, Buffer_free, b);
|
|
109
|
+
#endif
|
|
67
110
|
msgpack_buffer_init(b);
|
|
68
111
|
|
|
69
|
-
return
|
|
112
|
+
return self;
|
|
70
113
|
}
|
|
71
114
|
|
|
72
115
|
static ID get_partial_read_method(VALUE io)
|
|
@@ -118,7 +161,11 @@ void MessagePack_Buffer_initialize(msgpack_buffer_t* b, VALUE io, VALUE options)
|
|
|
118
161
|
VALUE MessagePack_Buffer_wrap(msgpack_buffer_t* b, VALUE owner)
|
|
119
162
|
{
|
|
120
163
|
b->owner = owner;
|
|
121
|
-
|
|
164
|
+
#ifdef TypedData_Make_Struct
|
|
165
|
+
return TypedData_Wrap_Struct(cMessagePack_Buffer, &wrapped_buffer_data_type, b);
|
|
166
|
+
#else
|
|
167
|
+
return Data_Wrap_Struct(cMessagePack_Buffer, Buffer_mark, NULL, b);
|
|
168
|
+
#endif
|
|
122
169
|
}
|
|
123
170
|
|
|
124
171
|
static VALUE Buffer_initialize(int argc, VALUE* argv, VALUE self)
|
|
@@ -512,4 +559,3 @@ void MessagePack_Buffer_module_init(VALUE mMessagePack)
|
|
|
512
559
|
rb_define_alias(cMessagePack_Buffer, "to_s", "to_str");
|
|
513
560
|
rb_define_method(cMessagePack_Buffer, "to_a", Buffer_to_a, 0);
|
|
514
561
|
}
|
|
515
|
-
|
data/ext/cbor/core_ext.c
CHANGED
|
@@ -49,8 +49,7 @@ static inline VALUE delegete_to_pack(int argc, VALUE* argv, VALUE self)
|
|
|
49
49
|
return delegete_to_pack(argc, argv, self); \
|
|
50
50
|
} \
|
|
51
51
|
VALUE packer = argv[0]; \
|
|
52
|
-
msgpack_packer_t *pk;
|
|
53
|
-
Data_Get_Struct(packer, msgpack_packer_t, pk);
|
|
52
|
+
msgpack_packer_t *pk = MessagePack_Packer_get(packer);
|
|
54
53
|
|
|
55
54
|
static VALUE NilClass_to_msgpack(int argc, VALUE* argv, VALUE self)
|
|
56
55
|
{
|
|
@@ -198,4 +197,3 @@ void MessagePack_core_ext_module_init()
|
|
|
198
197
|
rb_define_method(rb_cCBOR_Simple, "to_cbor", Simple_to_msgpack, -1);
|
|
199
198
|
rb_define_method(rb_cCBOR_Tagged, "to_cbor", Tagged_to_msgpack, -1);
|
|
200
199
|
}
|
|
201
|
-
|
data/ext/cbor/packer_class.c
CHANGED
|
@@ -39,15 +39,23 @@ static ID s_write;
|
|
|
39
39
|
//static VALUE s_packer_value;
|
|
40
40
|
//static msgpack_packer_t* s_packer;
|
|
41
41
|
|
|
42
|
+
static msgpack_packer_t* Packer_get(VALUE self);
|
|
43
|
+
|
|
42
44
|
#define PACKER(from, name) \
|
|
43
|
-
msgpack_packer_t* name; \
|
|
44
|
-
Data_Get_Struct(from, msgpack_packer_t, name); \
|
|
45
|
+
msgpack_packer_t* name = Packer_get(from); \
|
|
45
46
|
if(name == NULL) { \
|
|
46
47
|
rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
static void
|
|
50
|
+
static void Packer_mark(void* data)
|
|
50
51
|
{
|
|
52
|
+
msgpack_packer_t* pk = data;
|
|
53
|
+
msgpack_packer_mark(pk);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static void Packer_free(void* data)
|
|
57
|
+
{
|
|
58
|
+
msgpack_packer_t* pk = data;
|
|
51
59
|
if(pk == NULL) {
|
|
52
60
|
return;
|
|
53
61
|
}
|
|
@@ -55,13 +63,47 @@ static void Packer_free(msgpack_packer_t* pk)
|
|
|
55
63
|
xfree(pk);
|
|
56
64
|
}
|
|
57
65
|
|
|
66
|
+
#ifdef TypedData_Make_Struct
|
|
67
|
+
static size_t Packer_memsize(const void* data)
|
|
68
|
+
{
|
|
69
|
+
return data == NULL ? 0 : sizeof(msgpack_packer_t);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static const rb_data_type_t packer_data_type = {
|
|
73
|
+
"CBOR::Packer",
|
|
74
|
+
{Packer_mark, Packer_free, Packer_memsize,},
|
|
75
|
+
NULL, NULL, 0
|
|
76
|
+
};
|
|
77
|
+
#endif
|
|
78
|
+
|
|
79
|
+
static msgpack_packer_t* Packer_get(VALUE self)
|
|
80
|
+
{
|
|
81
|
+
msgpack_packer_t* pk = NULL;
|
|
82
|
+
#ifdef TypedData_Make_Struct
|
|
83
|
+
TypedData_Get_Struct(self, msgpack_packer_t, &packer_data_type, pk);
|
|
84
|
+
#else
|
|
85
|
+
Data_Get_Struct(self, msgpack_packer_t, pk);
|
|
86
|
+
#endif
|
|
87
|
+
return pk;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
msgpack_packer_t* MessagePack_Packer_get(VALUE self)
|
|
91
|
+
{
|
|
92
|
+
PACKER(self, pk);
|
|
93
|
+
return pk;
|
|
94
|
+
}
|
|
95
|
+
|
|
58
96
|
static VALUE Packer_alloc(VALUE klass)
|
|
59
97
|
{
|
|
60
|
-
msgpack_packer_t* pk
|
|
98
|
+
msgpack_packer_t* pk;
|
|
99
|
+
VALUE self;
|
|
100
|
+
#ifdef TypedData_Make_Struct
|
|
101
|
+
self = TypedData_Make_Struct(klass, msgpack_packer_t, &packer_data_type, pk);
|
|
102
|
+
#else
|
|
103
|
+
self = Data_Make_Struct(klass, msgpack_packer_t, Packer_mark, Packer_free, pk);
|
|
104
|
+
#endif
|
|
61
105
|
msgpack_packer_init(pk);
|
|
62
106
|
|
|
63
|
-
VALUE self = Data_Wrap_Struct(klass, msgpack_packer_mark, Packer_free, pk);
|
|
64
|
-
|
|
65
107
|
msgpack_packer_set_to_msgpack_method(pk, s_to_msgpack, self);
|
|
66
108
|
pk->buffer_ref = MessagePack_Buffer_wrap(PACKER_BUFFER_(pk), self);
|
|
67
109
|
|
|
@@ -301,4 +343,3 @@ void MessagePack_Packer_module_init(VALUE mMessagePack)
|
|
|
301
343
|
rb_define_module_function(mMessagePack, "encode", MessagePack_pack_module_method, -1);
|
|
302
344
|
rb_define_module_function(mMessagePack, "dump", MessagePack_dump_module_method, -1);
|
|
303
345
|
}
|
|
304
|
-
|
data/ext/cbor/packer_class.h
CHANGED
data/ext/cbor/rbinit.c
CHANGED
|
@@ -43,6 +43,12 @@ void Init_cbor(void)
|
|
|
43
43
|
rb_cCBOR_Simple = rb_struct_define(NULL, "value", NULL);
|
|
44
44
|
rb_define_const(mMessagePack, "Simple", rb_cCBOR_Simple);
|
|
45
45
|
|
|
46
|
+
/* CBOR tag numbers accepted by the decode option reconstruct_from_tags:,
|
|
47
|
+
so callers can write reconstruct_from_tags: [CBOR::TAG_REGEXP] instead of
|
|
48
|
+
a bare integer. */
|
|
49
|
+
rb_define_const(mMessagePack, "TAG_URI", INT2FIX(TAG_URI));
|
|
50
|
+
rb_define_const(mMessagePack, "TAG_REGEXP", INT2FIX(TAG_RE));
|
|
51
|
+
|
|
46
52
|
MessagePack_Buffer_module_init(mMessagePack);
|
|
47
53
|
MessagePack_Packer_module_init(mMessagePack);
|
|
48
54
|
MessagePack_Unpacker_module_init(mMessagePack);
|
data/ext/cbor/unpacker.c
CHANGED
|
@@ -515,16 +515,27 @@ int msgpack_unpacker_read_map_header(msgpack_unpacker_t* uk, uint64_t* result_si
|
|
|
515
515
|
return msgpack_unpacker_read_container_header(uk, result_size, IB_MAP);
|
|
516
516
|
}
|
|
517
517
|
|
|
518
|
-
static VALUE msgpack_unpacker_process_tag(uint64_t tag, VALUE v) {
|
|
518
|
+
static VALUE msgpack_unpacker_process_tag(msgpack_unpacker_t* uk, uint64_t tag, VALUE v) {
|
|
519
519
|
VALUE res = v;
|
|
520
520
|
switch (tag) {
|
|
521
521
|
case TAG_TIME_EPOCH: {
|
|
522
522
|
return rb_funcall(rb_cTime, rb_intern("at"), 1, v);
|
|
523
523
|
}
|
|
524
524
|
case TAG_RE: {
|
|
525
|
+
/* Reconstructing a Regexp runs the decoded data through Regexp.new,
|
|
526
|
+
which the caller may not want when decoding input from an untrusted
|
|
527
|
+
source. Only do it when the caller opts in with
|
|
528
|
+
reconstruct_from_tags: [35]; otherwise return the value as a generic
|
|
529
|
+
tagged object. */
|
|
530
|
+
if (!uk->reconstruct_regexp)
|
|
531
|
+
goto unknown_tag;
|
|
525
532
|
return rb_funcall(rb_cRegexp, rb_intern("new"), 1, v);
|
|
526
533
|
}
|
|
527
534
|
case TAG_URI: {
|
|
535
|
+
/* Likewise, building a URI from the decoded value is opt-in via
|
|
536
|
+
reconstruct_from_tags: [32]. */
|
|
537
|
+
if (!uk->reconstruct_uri)
|
|
538
|
+
goto unknown_tag;
|
|
528
539
|
if (!rb_const_defined(rb_cObject, rb_intern("URI")))
|
|
529
540
|
goto unknown_tag;
|
|
530
541
|
return rb_funcall(rb_const_get(rb_cObject, rb_intern("URI")),
|
|
@@ -642,7 +653,7 @@ int msgpack_unpacker_read(msgpack_unpacker_t* uk, size_t target_stack_depth)
|
|
|
642
653
|
top->type = STACK_TYPE_MAP_KEY;
|
|
643
654
|
break;
|
|
644
655
|
case STACK_TYPE_TAG:
|
|
645
|
-
object_complete(uk, msgpack_unpacker_process_tag(top->tag, uk->last_object));
|
|
656
|
+
object_complete(uk, msgpack_unpacker_process_tag(uk, top->tag, uk->last_object));
|
|
646
657
|
goto done;
|
|
647
658
|
|
|
648
659
|
case STACK_TYPE_ARRAY_INDEF:
|
data/ext/cbor/unpacker.h
CHANGED
|
@@ -73,7 +73,9 @@ struct msgpack_unpacker_t {
|
|
|
73
73
|
int textflag;
|
|
74
74
|
|
|
75
75
|
bool keys_as_symbols; /* Experimental */
|
|
76
|
-
|
|
76
|
+
bool reconstruct_regexp; /* reconstruct tag 35 (Regexp); opt-in */
|
|
77
|
+
bool reconstruct_uri; /* reconstruct tag 32 (URI); opt-in */
|
|
78
|
+
|
|
77
79
|
VALUE buffer_ref;
|
|
78
80
|
};
|
|
79
81
|
|
data/ext/cbor/unpacker_class.c
CHANGED
|
@@ -39,15 +39,23 @@ static VALUE eMalformedFormatError;
|
|
|
39
39
|
static VALUE eStackError;
|
|
40
40
|
static VALUE eTypeError;
|
|
41
41
|
|
|
42
|
+
static msgpack_unpacker_t* Unpacker_get(VALUE self);
|
|
43
|
+
|
|
42
44
|
#define UNPACKER(from, name) \
|
|
43
|
-
msgpack_unpacker_t *name =
|
|
44
|
-
Data_Get_Struct(from, msgpack_unpacker_t, name); \
|
|
45
|
+
msgpack_unpacker_t *name = Unpacker_get(from); \
|
|
45
46
|
if(name == NULL) { \
|
|
46
47
|
rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
static void
|
|
50
|
+
static void Unpacker_mark(void* data)
|
|
51
|
+
{
|
|
52
|
+
msgpack_unpacker_t* uk = data;
|
|
53
|
+
msgpack_unpacker_mark(uk);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static void Unpacker_free(void* data)
|
|
50
57
|
{
|
|
58
|
+
msgpack_unpacker_t* uk = data;
|
|
51
59
|
if(uk == NULL) {
|
|
52
60
|
return;
|
|
53
61
|
}
|
|
@@ -55,18 +63,64 @@ static void Unpacker_free(msgpack_unpacker_t* uk)
|
|
|
55
63
|
xfree(uk);
|
|
56
64
|
}
|
|
57
65
|
|
|
66
|
+
#ifdef TypedData_Make_Struct
|
|
67
|
+
static size_t Unpacker_memsize(const void* data)
|
|
68
|
+
{
|
|
69
|
+
return data == NULL ? 0 : sizeof(msgpack_unpacker_t);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static const rb_data_type_t unpacker_data_type = {
|
|
73
|
+
"CBOR::Unpacker",
|
|
74
|
+
{Unpacker_mark, Unpacker_free, Unpacker_memsize,},
|
|
75
|
+
NULL, NULL, 0
|
|
76
|
+
};
|
|
77
|
+
#endif
|
|
78
|
+
|
|
79
|
+
static msgpack_unpacker_t* Unpacker_get(VALUE self)
|
|
80
|
+
{
|
|
81
|
+
msgpack_unpacker_t* uk = NULL;
|
|
82
|
+
#ifdef TypedData_Make_Struct
|
|
83
|
+
TypedData_Get_Struct(self, msgpack_unpacker_t, &unpacker_data_type, uk);
|
|
84
|
+
#else
|
|
85
|
+
Data_Get_Struct(self, msgpack_unpacker_t, uk);
|
|
86
|
+
#endif
|
|
87
|
+
return uk;
|
|
88
|
+
}
|
|
89
|
+
|
|
58
90
|
static VALUE Unpacker_alloc(VALUE klass)
|
|
59
91
|
{
|
|
60
|
-
msgpack_unpacker_t* uk
|
|
92
|
+
msgpack_unpacker_t* uk;
|
|
93
|
+
VALUE self;
|
|
94
|
+
#ifdef TypedData_Make_Struct
|
|
95
|
+
self = TypedData_Make_Struct(klass, msgpack_unpacker_t, &unpacker_data_type, uk);
|
|
96
|
+
#else
|
|
97
|
+
self = Data_Make_Struct(klass, msgpack_unpacker_t, Unpacker_mark, Unpacker_free, uk);
|
|
98
|
+
#endif
|
|
61
99
|
msgpack_unpacker_init(uk);
|
|
62
100
|
|
|
63
|
-
VALUE self = Data_Wrap_Struct(klass, msgpack_unpacker_mark, Unpacker_free, uk);
|
|
64
|
-
|
|
65
101
|
uk->buffer_ref = MessagePack_Buffer_wrap(UNPACKER_BUFFER_(uk), self);
|
|
66
102
|
|
|
67
103
|
return self;
|
|
68
104
|
}
|
|
69
105
|
|
|
106
|
+
/* reconstruct_from_tags: a list of CBOR tag numbers whose decoded value should be
|
|
107
|
+
reconstructed into a live Ruby object. Examined with #include? .
|
|
108
|
+
Only the object-constructing tags
|
|
109
|
+
are gated (35 -> Regexp, 32 -> URI); anything else decodes to CBOR::Tagged.
|
|
110
|
+
Absent or not responding to include? means "none", the safe default. */
|
|
111
|
+
static void set_reconstruct_from_tags(msgpack_unpacker_t* uk, VALUE options)
|
|
112
|
+
{
|
|
113
|
+
VALUE tags;
|
|
114
|
+
if (!RB_TYPE_P(options, T_HASH))
|
|
115
|
+
return;
|
|
116
|
+
tags = rb_hash_aref(options, ID2SYM(rb_intern("reconstruct_from_tags")));
|
|
117
|
+
ID include_p = rb_intern("include?");
|
|
118
|
+
if (rb_respond_to(tags, include_p)) {
|
|
119
|
+
uk->reconstruct_regexp = RTEST(rb_funcall(tags, include_p, 1, INT2FIX(TAG_RE)));
|
|
120
|
+
uk->reconstruct_uri = RTEST(rb_funcall(tags, include_p, 1, INT2FIX(TAG_URI)));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
70
124
|
static VALUE Unpacker_initialize(int argc, VALUE* argv, VALUE self)
|
|
71
125
|
{
|
|
72
126
|
VALUE io = Qnil;
|
|
@@ -101,6 +155,7 @@ static VALUE Unpacker_initialize(int argc, VALUE* argv, VALUE self)
|
|
|
101
155
|
VALUE v;
|
|
102
156
|
v = rb_hash_aref(options, ID2SYM(rb_intern("symbolize_keys")));
|
|
103
157
|
uk->keys_as_symbols = RTEST(v);
|
|
158
|
+
set_reconstruct_from_tags(uk, options);
|
|
104
159
|
}
|
|
105
160
|
}
|
|
106
161
|
|
|
@@ -344,7 +399,8 @@ VALUE MessagePack_unpack(int argc, VALUE* argv)
|
|
|
344
399
|
msgpack_buffer_set_write_reference_threshold(UNPACKER_BUFFER_(uk), 0);
|
|
345
400
|
|
|
346
401
|
uk->keys_as_symbols = keys_as_symbols;
|
|
347
|
-
|
|
402
|
+
set_reconstruct_from_tags(uk, options);
|
|
403
|
+
|
|
348
404
|
if(io != Qnil) {
|
|
349
405
|
MessagePack_Buffer_initialize(UNPACKER_BUFFER_(uk), io, Qnil);
|
|
350
406
|
}
|
|
@@ -426,4 +482,3 @@ void MessagePack_Unpacker_module_init(VALUE mMessagePack)
|
|
|
426
482
|
rb_define_module_function(mMessagePack, "unpack", MessagePack_unpack_module_method, -1);
|
|
427
483
|
rb_define_module_function(mMessagePack, "decode", MessagePack_unpack_module_method, -1);
|
|
428
484
|
}
|
|
429
|
-
|
data/lib/cbor/version.rb
CHANGED
data/spec/format_spec.rb
CHANGED
|
@@ -450,17 +450,25 @@ describe MessagePack do
|
|
|
450
450
|
end
|
|
451
451
|
|
|
452
452
|
it "URI" do
|
|
453
|
-
# check_decode "\xd8\x20\x78\x13http://www.ietf.org", CBOR::Tagged.new(32, "http://www.ietf.org")
|
|
454
453
|
require 'uri'
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
#
|
|
454
|
+
# Safe default: object-constructing tags are not reconstructed.
|
|
455
|
+
check_decode "\xd8\x20\x78\x13http://www.ietf.org", CBOR::Tagged.new(32, "http://www.ietf.org")
|
|
456
|
+
# Opt in to reconstruct the URI by listing its tag.
|
|
457
|
+
check_decode "\xd8\x20\x78\x13http://www.ietf.org", URI.parse("http://www.ietf.org"), reconstruct_from_tags: [CBOR::TAG_URI]
|
|
458
|
+
expect { check_decode "\xd8\x20\x78\x13h``p://www.ietf.org", URI.parse("http://www.ietf.org"), reconstruct_from_tags: [CBOR::TAG_URI] }.to raise_error(URI::InvalidURIError)
|
|
459
|
+
check_decode "\xd8\x20\x78\x13http://www.ietf.org", URI.parse("http://www.ietf.org"), reconstruct_from_tags: {CBOR::TAG_URI => true}
|
|
460
|
+
check_decode "\xd8\x20\x78\x13http://www.ietf.org", CBOR::Tagged.new(32, "http://www.ietf.org"), reconstruct_from_tags: {4711 => true}
|
|
461
|
+
check_decode "\xd8\x20\x78\x13http://www.ietf.org", CBOR::Tagged.new(32, "http://www.ietf.org"), reconstruct_from_tags: 4711
|
|
458
462
|
end
|
|
459
463
|
|
|
460
464
|
it "regexp" do
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
465
|
+
# Safe default: tag 35 is not compiled into a Regexp.
|
|
466
|
+
check_decode "\xd8\x23\x63foo", CBOR::Tagged.new(35, "foo")
|
|
467
|
+
# Opt in to reconstruct the Regexp by listing its tag.
|
|
468
|
+
check_decode "\xd8\x23\x63foo", /foo/, reconstruct_from_tags: [CBOR::TAG_REGEXP]
|
|
469
|
+
expect { check_decode "\xd8\x23\x63*oo", /foo/, reconstruct_from_tags: [CBOR::TAG_REGEXP] }.to raise_error(RegexpError)
|
|
470
|
+
check 14, /(?-mix:foo)/, reconstruct_from_tags: [CBOR::TAG_REGEXP]
|
|
471
|
+
check 6, /foo/, reconstruct_from_tags: [CBOR::TAG_REGEXP]
|
|
464
472
|
end
|
|
465
473
|
|
|
466
474
|
it "Unknown simple 0" do
|
|
@@ -559,10 +567,10 @@ describe MessagePack do
|
|
|
559
567
|
# #check_map 5, (1<<32)-1 # memory error
|
|
560
568
|
# end
|
|
561
569
|
|
|
562
|
-
def check(len, obj)
|
|
570
|
+
def check(len, obj, opts = nil)
|
|
563
571
|
raw = obj.to_cbor.to_s
|
|
564
572
|
raw.length.should == len
|
|
565
|
-
obj2 = MessagePack.unpack(raw)
|
|
573
|
+
obj2 = opts ? MessagePack.unpack(raw, opts) : MessagePack.unpack(raw)
|
|
566
574
|
obj2.should == obj
|
|
567
575
|
if obj.respond_to? :encoding
|
|
568
576
|
obj2.encoding.should == obj.encoding
|
|
@@ -588,8 +596,8 @@ describe MessagePack do
|
|
|
588
596
|
raw.should == buf
|
|
589
597
|
end
|
|
590
598
|
|
|
591
|
-
def check_decode(c, obj)
|
|
592
|
-
obj2 = MessagePack.unpack(c)
|
|
599
|
+
def check_decode(c, obj, opts = nil)
|
|
600
|
+
obj2 = opts ? MessagePack.unpack(c, opts) : MessagePack.unpack(c)
|
|
593
601
|
obj2.should == obj
|
|
594
602
|
if obj.respond_to? :encoding
|
|
595
603
|
obj2.encoding.should == obj.encoding
|