hx_cbor 2021.8.20

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +30 -0
  3. data/.travis.yml +24 -0
  4. data/ChangeLog +106 -0
  5. data/Gemfile +11 -0
  6. data/README.rdoc +191 -0
  7. data/Rakefile +97 -0
  8. data/doclib/cbor.rb +80 -0
  9. data/doclib/cbor/buffer.rb +193 -0
  10. data/doclib/cbor/core_ext.rb +133 -0
  11. data/doclib/cbor/error.rb +14 -0
  12. data/doclib/cbor/packer.rb +133 -0
  13. data/doclib/cbor/simple.rb +15 -0
  14. data/doclib/cbor/tagged.rb +16 -0
  15. data/doclib/cbor/unpacker.rb +138 -0
  16. data/ext/cbor/3424.i.rb +29 -0
  17. data/ext/cbor/buffer.c +693 -0
  18. data/ext/cbor/buffer.h +484 -0
  19. data/ext/cbor/buffer_class.c +516 -0
  20. data/ext/cbor/buffer_class.h +41 -0
  21. data/ext/cbor/cbor.h +69 -0
  22. data/ext/cbor/compat.h +147 -0
  23. data/ext/cbor/core_ext.c +201 -0
  24. data/ext/cbor/core_ext.h +35 -0
  25. data/ext/cbor/example.rb +10 -0
  26. data/ext/cbor/extconf.rb +29 -0
  27. data/ext/cbor/install.sh +1 -0
  28. data/ext/cbor/packer.c +169 -0
  29. data/ext/cbor/packer.h +362 -0
  30. data/ext/cbor/packer_class.c +304 -0
  31. data/ext/cbor/packer_class.h +39 -0
  32. data/ext/cbor/rbinit.c +51 -0
  33. data/ext/cbor/renamer.h +56 -0
  34. data/ext/cbor/rmem.c +103 -0
  35. data/ext/cbor/rmem.h +118 -0
  36. data/ext/cbor/sysdep.h +139 -0
  37. data/ext/cbor/sysdep_endian.h +59 -0
  38. data/ext/cbor/sysdep_types.h +55 -0
  39. data/ext/cbor/unpacker.c +784 -0
  40. data/ext/cbor/unpacker.h +135 -0
  41. data/ext/cbor/unpacker_class.c +439 -0
  42. data/ext/cbor/unpacker_class.h +39 -0
  43. data/hx_cbor.gemspec +25 -0
  44. data/lib/cbor.rb +6 -0
  45. data/lib/cbor/version.rb +3 -0
  46. data/spec/buffer_io_spec.rb +260 -0
  47. data/spec/buffer_spec.rb +576 -0
  48. data/spec/cases.cbor +0 -0
  49. data/spec/cases.cbor_stream +0 -0
  50. data/spec/cases.json +1 -0
  51. data/spec/cases.msg +0 -0
  52. data/spec/cases_compact.msg +0 -0
  53. data/spec/cases_spec.rb +39 -0
  54. data/spec/format_spec.rb +540 -0
  55. data/spec/packer_spec.rb +127 -0
  56. data/spec/random_compat.rb +24 -0
  57. data/spec/spec_helper.rb +68 -0
  58. data/spec/unpacker_spec.rb +260 -0
  59. metadata +198 -0
@@ -0,0 +1,41 @@
1
+ /*
2
+ * CBOR for Ruby
3
+ *
4
+ * Copyright (C) 2013 Carsten Bormann
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License").
7
+ *
8
+ * Based on:
9
+ ***********/
10
+ /*
11
+ * MessagePack for Ruby
12
+ *
13
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
14
+ *
15
+ * Licensed under the Apache License, Version 2.0 (the "License");
16
+ * you may not use this file except in compliance with the License.
17
+ * You may obtain a copy of the License at
18
+ *
19
+ * http://www.apache.org/licenses/LICENSE-2.0
20
+ *
21
+ * Unless required by applicable law or agreed to in writing, software
22
+ * distributed under the License is distributed on an "AS IS" BASIS,
23
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ * See the License for the specific language governing permissions and
25
+ * limitations under the License.
26
+ */
27
+ #ifndef MSGPACK_RUBY_BUFFER_CLASS_H__
28
+ #define MSGPACK_RUBY_BUFFER_CLASS_H__
29
+
30
+ #include "buffer.h"
31
+
32
+ extern VALUE cMessagePack_Buffer;
33
+
34
+ void MessagePack_Buffer_module_init(VALUE mMessagePack);
35
+
36
+ VALUE MessagePack_Buffer_wrap(msgpack_buffer_t* b, VALUE owner);
37
+
38
+ void MessagePack_Buffer_initialize(msgpack_buffer_t* b, VALUE io, VALUE options);
39
+
40
+ #endif
41
+
data/ext/cbor/cbor.h ADDED
@@ -0,0 +1,69 @@
1
+ #ifndef CBOR_RUBY_BUFFER_H__
2
+ #define CBOR_RUBY_BUFFER_H__
3
+
4
+ /* The 8 major types */
5
+ #define MT_UNSIGNED 0
6
+ #define MT_NEGATIVE 1
7
+ #define MT_BYTES 2
8
+ #define MT_TEXT 3
9
+ #define MT_ARRAY 4
10
+ #define MT_MAP 5
11
+ #define MT_TAG 6
12
+ #define MT_PRIM 7
13
+
14
+ /* The initial bytes resulting from those */
15
+ #define IB_UNSIGNED (MT_UNSIGNED << 5)
16
+ #define IB_NEGATIVE (MT_NEGATIVE << 5)
17
+ #define IB_BYTES (MT_BYTES << 5)
18
+ #define IB_TEXT (MT_TEXT << 5)
19
+ #define IB_ARRAY (MT_ARRAY << 5)
20
+ #define IB_MAP (MT_MAP << 5)
21
+ #define IB_TAG (MT_TAG << 5)
22
+ #define IB_PRIM (MT_PRIM << 5)
23
+
24
+ #define IB_NEGFLAG (IB_NEGATIVE - IB_UNSIGNED)
25
+ #define IB_NEGFLAG_AS_BIT(ib) ((ib) >> 5)
26
+ #define IB_TEXTFLAG (IB_TEXT - IB_BYTES)
27
+
28
+ #define IB_AI(ib) ((ib) & 0x1F)
29
+ #define IB_MT(ib) ((ib) >> 5)
30
+
31
+ /* Tag numbers handled by this implementation */
32
+ #define TAG_TIME_EPOCH 1
33
+ #define TAG_BIGNUM 2
34
+ #define TAG_BIGNUM_NEG 3
35
+ #define TAG_URI 32
36
+ #define TAG_RE 35
37
+
38
+ /* Initial bytes of those tag numbers */
39
+ #define IB_TIME_EPOCH (IB_TAG + TAG_TIME_EPOCH)
40
+ #define IB_BIGNUM (IB_TAG + TAG_BIGNUM)
41
+ #define IB_BIGNUM_NEG (IB_TAG + TAG_BIGNUM_NEG)
42
+ /* TAG_URI and TAG_RE are non-immediate tags */
43
+
44
+ /* Simple values handled by this implementation */
45
+ #define VAL_NIL 22
46
+ #define VAL_FALSE 20
47
+ #define VAL_TRUE 21
48
+
49
+ /* Initial bytes of those simple values */
50
+ #define IB_NIL (IB_PRIM + VAL_NIL)
51
+ #define IB_FALSE (IB_PRIM + VAL_FALSE)
52
+ #define IB_TRUE (IB_PRIM + VAL_TRUE)
53
+
54
+ /* AI values with more data in head */
55
+ #define AI_1 24
56
+ #define AI_2 25
57
+ #define AI_4 26
58
+ #define AI_8 27
59
+ #define AI_INDEF 31
60
+ #define IB_BREAK (IB_PRIM + AI_INDEF)
61
+ /* For */
62
+ #define IB_UNUSED (IB_TAG + AI_INDEF)
63
+
64
+ /* Floating point initial bytes */
65
+ #define IB_FLOAT2 (IB_PRIM + AI_2)
66
+ #define IB_FLOAT4 (IB_PRIM + AI_4)
67
+ #define IB_FLOAT8 (IB_PRIM + AI_8)
68
+
69
+ #endif
data/ext/cbor/compat.h ADDED
@@ -0,0 +1,147 @@
1
+ /*
2
+ * CBOR for Ruby
3
+ *
4
+ * Copyright (C) 2013 Carsten Bormann
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License").
7
+ *
8
+ * Based on:
9
+ ***********/
10
+ /*
11
+ * MessagePack for Ruby
12
+ *
13
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
14
+ *
15
+ * Licensed under the Apache License, Version 2.0 (the "License");
16
+ * you may not use this file except in compliance with the License.
17
+ * You may obtain a copy of the License at
18
+ *
19
+ * http://www.apache.org/licenses/LICENSE-2.0
20
+ *
21
+ * Unless required by applicable law or agreed to in writing, software
22
+ * distributed under the License is distributed on an "AS IS" BASIS,
23
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ * See the License for the specific language governing permissions and
25
+ * limitations under the License.
26
+ */
27
+ #ifndef MSGPACK_RUBY_COMPAT_H__
28
+ #define MSGPACK_RUBY_COMPAT_H__
29
+
30
+ #include "ruby.h"
31
+
32
+ #if defined(HAVE_RUBY_ST_H)
33
+ # include "ruby/st.h" /* ruby hash on Ruby 1.9 */
34
+ #elif defined(HAVE_ST_H)
35
+ # include "st.h" /* ruby hash on Ruby 1.8 */
36
+ #endif
37
+
38
+
39
+ /*
40
+ * COMPAT_HAVE_ENCODING
41
+ */
42
+ #ifdef HAVE_RUBY_ENCODING_H
43
+ # include "ruby/encoding.h"
44
+ # define COMPAT_HAVE_ENCODING
45
+ #endif
46
+
47
+ #if defined(__MACRUBY__) /* MacRuby */
48
+ # undef COMPAT_HAVE_ENCODING
49
+ #endif
50
+
51
+
52
+ /*
53
+ * define STR_DUP_LIKELY_DOES_COPY
54
+ * check rb_str_dup actually copies the string or not
55
+ */
56
+ #if defined(RUBY_VM) && defined(FL_ALL) && defined(FL_USER1) && defined(FL_USER3) /* MRI 1.9 */
57
+ # define STR_DUP_LIKELY_DOES_COPY(str) FL_ALL(str, FL_USER1|FL_USER3) /* same as STR_ASSOC_P(str) */
58
+
59
+ #elif defined(FL_TEST) && defined(ELTS_SHARED) /* MRI 1.8 */
60
+ # define STR_DUP_LIKELY_DOES_COPY(str) (!FL_TEST(str, ELTS_SHARED))
61
+
62
+ //#elif defined(RUBINIUS) || defined(JRUBY) /* Rubinius and JRuby */
63
+ #else
64
+ # define STR_DUP_LIKELY_DOES_COPY(str) (1)
65
+
66
+ #endif
67
+
68
+
69
+ /*
70
+ * SIZET2NUM
71
+ */
72
+ #ifndef SIZET2NUM /* MRI 1.8 */
73
+ # define SIZET2NUM(v) ULL2NUM(v)
74
+ #endif
75
+
76
+
77
+ /*
78
+ * rb_errinfo()
79
+ */
80
+ #if defined(RUBY_VM) /* MRI 1.9 */
81
+ # define COMPAT_RERAISE rb_exc_raise(rb_errinfo())
82
+
83
+ #elif defined(JRUBY) /* JRuby */
84
+ # define COMPAT_RERAISE rb_exc_raise(rb_gv_get("$!"))
85
+
86
+ #else /* MRI 1.8 and Rubinius */
87
+ # define COMPAT_RERAISE rb_exc_raise(ruby_errinfo)
88
+ #endif
89
+
90
+
91
+ /*
92
+ * RBIGNUM_POSITIVE_P
93
+ */
94
+ #ifndef RBIGNUM_POSITIVE_P
95
+ # if defined(RUBINIUS) /* Rubinius <= v1.2.3 */
96
+ # define RBIGNUM_POSITIVE_P(b) (rb_funcall(b, rb_intern(">="), 1, INT2FIX(0)) == Qtrue)
97
+
98
+ # elif defined(JRUBY) /* JRuby */
99
+ # define RBIGNUM_POSITIVE_P(b) (rb_funcall(b, rb_intern(">="), 1, INT2FIX(0)) == Qtrue)
100
+ # define rb_big2ull(b) rb_num2ull(b)
101
+ /*#define rb_big2ll(b) rb_num2ll(b)*/
102
+
103
+ # else /* MRI 1.8 */
104
+ # define RBIGNUM_POSITIVE_P(b) (RBIGNUM(b)->sign)
105
+ # endif
106
+ #endif
107
+
108
+
109
+ #ifndef HAVE_RB_INTEGER_UNPACK
110
+
111
+ /* More MRI 1.8 */
112
+ #ifndef RBIGNUM_LEN
113
+ #define RBIGNUM_LEN(b) (RBIGNUM(b)->len)
114
+ #endif
115
+ #ifndef RBIGNUM_DIGITS
116
+ #ifndef RBIGNUM
117
+ #define CANT_DO_BIGNUMS_FAST_ON_THIS_PLATFORM
118
+ #endif
119
+ #define RBIGNUM_DIGITS(b) (RBIGNUM(b)->digits)
120
+ #endif
121
+ #ifndef HAVE_RB_BIG_NEW
122
+ /* not really worth fixing any more... */
123
+ #define CANT_DO_BIGNUMS_FAST_ON_THIS_PLATFORM
124
+ /* gross 1.8.7 hack thanks to Mathieu Bouchard <matju@artengine.ca> */
125
+ #define rb_big_new(len, sign) rb_funcall(INT2FIX(1),rb_intern("<<"),1,INT2FIX(len > 0 ? ((len) * SIZEOF_BDIGITS * 8) - 1 : 0));
126
+ #endif
127
+
128
+ #endif
129
+
130
+ #ifndef RB_TYPE_P
131
+ #define RB_TYPE_P(obj, type) (TYPE(obj) == (type))
132
+ #endif
133
+
134
+ /*
135
+ * RSTRING_PTR, RSTRING_LEN
136
+ */
137
+ #ifndef RSTRING_PTR /* MRI 1.8.5 */
138
+ # define RSTRING_PTR(s) (RSTRING(s)->ptr)
139
+ #endif
140
+
141
+ #ifndef RSTRING_LEN /* MRI 1.8.5 */
142
+ # define RSTRING_LEN(s) (RSTRING(s)->len)
143
+ #endif
144
+
145
+
146
+ #endif
147
+
@@ -0,0 +1,201 @@
1
+ /*
2
+ * CBOR for Ruby
3
+ *
4
+ * Copyright (C) 2013 Carsten Bormann
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License").
7
+ *
8
+ * Based on:
9
+ ***********/
10
+ /*
11
+ * MessagePack for Ruby
12
+ *
13
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
14
+ *
15
+ * Licensed under the Apache License, Version 2.0 (the "License");
16
+ * you may not use this file except in compliance with the License.
17
+ * You may obtain a copy of the License at
18
+ *
19
+ * http://www.apache.org/licenses/LICENSE-2.0
20
+ *
21
+ * Unless required by applicable law or agreed to in writing, software
22
+ * distributed under the License is distributed on an "AS IS" BASIS,
23
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ * See the License for the specific language governing permissions and
25
+ * limitations under the License.
26
+ */
27
+
28
+ #include "core_ext.h"
29
+ #include "packer.h"
30
+ #include "packer_class.h"
31
+
32
+ static inline VALUE delegete_to_pack(int argc, VALUE* argv, VALUE self)
33
+ {
34
+ if(argc == 0) {
35
+ return MessagePack_pack(1, &self);
36
+ } else if(argc == 1) {
37
+ /* write to io */
38
+ VALUE argv2[2];
39
+ argv2[0] = self;
40
+ argv2[1] = argv[0];
41
+ return MessagePack_pack(2, argv2);
42
+ } else {
43
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
44
+ }
45
+ }
46
+
47
+ #define ENSURE_PACKER(argc, argv, packer, pk) \
48
+ if(argc != 1 || rb_class_of(argv[0]) != cMessagePack_Packer) { \
49
+ return delegete_to_pack(argc, argv, self); \
50
+ } \
51
+ VALUE packer = argv[0]; \
52
+ msgpack_packer_t *pk; \
53
+ Data_Get_Struct(packer, msgpack_packer_t, pk);
54
+
55
+ static VALUE NilClass_to_msgpack(int argc, VALUE* argv, VALUE self)
56
+ {
57
+ ENSURE_PACKER(argc, argv, packer, pk);
58
+ msgpack_packer_write_nil(pk);
59
+ return packer;
60
+ }
61
+
62
+ static VALUE TrueClass_to_msgpack(int argc, VALUE* argv, VALUE self)
63
+ {
64
+ ENSURE_PACKER(argc, argv, packer, pk);
65
+ msgpack_packer_write_true(pk);
66
+ return packer;
67
+ }
68
+
69
+ static VALUE FalseClass_to_msgpack(int argc, VALUE* argv, VALUE self)
70
+ {
71
+ ENSURE_PACKER(argc, argv, packer, pk);
72
+ msgpack_packer_write_false(pk);
73
+ return packer;
74
+ }
75
+
76
+ #ifdef RUBY_INTEGER_UNIFICATION
77
+
78
+ static VALUE Integer_to_msgpack(int argc, VALUE* argv, VALUE self)
79
+ {
80
+ ENSURE_PACKER(argc, argv, packer, pk);
81
+ if (FIXNUM_P(self))
82
+ msgpack_packer_write_fixnum_value(pk, self);
83
+ else
84
+ msgpack_packer_write_bignum_value(pk, self);
85
+ return packer;
86
+ }
87
+
88
+ #else
89
+
90
+ static VALUE Fixnum_to_msgpack(int argc, VALUE* argv, VALUE self)
91
+ {
92
+ ENSURE_PACKER(argc, argv, packer, pk);
93
+ msgpack_packer_write_fixnum_value(pk, self);
94
+ return packer;
95
+ }
96
+
97
+ static VALUE Bignum_to_msgpack(int argc, VALUE* argv, VALUE self)
98
+ {
99
+ ENSURE_PACKER(argc, argv, packer, pk);
100
+ msgpack_packer_write_bignum_value(pk, self);
101
+ return packer;
102
+ }
103
+
104
+ #endif
105
+
106
+ static VALUE Float_to_msgpack(int argc, VALUE* argv, VALUE self)
107
+ {
108
+ ENSURE_PACKER(argc, argv, packer, pk);
109
+ msgpack_packer_write_float_value(pk, self);
110
+ return packer;
111
+ }
112
+
113
+ static VALUE String_to_msgpack(int argc, VALUE* argv, VALUE self)
114
+ {
115
+ ENSURE_PACKER(argc, argv, packer, pk);
116
+ msgpack_packer_write_string_value(pk, self);
117
+ return packer;
118
+ }
119
+
120
+ static VALUE Array_to_msgpack(int argc, VALUE* argv, VALUE self)
121
+ {
122
+ ENSURE_PACKER(argc, argv, packer, pk);
123
+ msgpack_packer_write_array_value(pk, self);
124
+ return packer;
125
+ }
126
+
127
+ static VALUE Hash_to_msgpack(int argc, VALUE* argv, VALUE self)
128
+ {
129
+ ENSURE_PACKER(argc, argv, packer, pk);
130
+ msgpack_packer_write_hash_value(pk, self);
131
+ return packer;
132
+ }
133
+
134
+ static VALUE Symbol_to_msgpack(int argc, VALUE* argv, VALUE self)
135
+ {
136
+ ENSURE_PACKER(argc, argv, packer, pk);
137
+ msgpack_packer_write_symbol_value(pk, self);
138
+ return packer;
139
+ }
140
+
141
+ static VALUE Simple_to_msgpack(int argc, VALUE* argv, VALUE self)
142
+ {
143
+ ENSURE_PACKER(argc, argv, packer, pk);
144
+ msgpack_packer_write_simple_value(pk, self);
145
+ return packer;
146
+ }
147
+
148
+ static VALUE Tagged_to_msgpack(int argc, VALUE* argv, VALUE self)
149
+ {
150
+ ENSURE_PACKER(argc, argv, packer, pk);
151
+ msgpack_packer_write_tagged_value(pk, self);
152
+ return packer;
153
+ }
154
+
155
+ static VALUE Regexp_to_msgpack(int argc, VALUE* argv, VALUE self)
156
+ {
157
+ ENSURE_PACKER(argc, argv, packer, pk);
158
+ msgpack_packer_write_processed_value(pk, self, rb_intern("source"), TAG_RE);
159
+ return packer;
160
+ }
161
+
162
+ static VALUE URI_to_msgpack(int argc, VALUE* argv, VALUE self)
163
+ {
164
+ ENSURE_PACKER(argc, argv, packer, pk);
165
+ msgpack_packer_write_processed_value(pk, self, rb_intern("to_s"), TAG_URI);
166
+ return packer;
167
+ }
168
+
169
+ static VALUE Time_to_msgpack(int argc, VALUE* argv, VALUE self)
170
+ {
171
+ ENSURE_PACKER(argc, argv, packer, pk);
172
+ msgpack_packer_write_processed_value(pk, self, rb_intern("to_i"), TAG_TIME_EPOCH);
173
+ return packer;
174
+ }
175
+
176
+ void MessagePack_core_ext_module_init()
177
+ {
178
+ rb_define_method(rb_cNilClass, "to_cbor", NilClass_to_msgpack, -1);
179
+ rb_define_method(rb_cTrueClass, "to_cbor", TrueClass_to_msgpack, -1);
180
+ rb_define_method(rb_cFalseClass, "to_cbor", FalseClass_to_msgpack, -1);
181
+ #ifdef RUBY_INTEGER_UNIFICATION
182
+ rb_define_method(rb_cInteger, "to_cbor", Integer_to_msgpack, -1);
183
+ #else
184
+ rb_define_method(rb_cFixnum, "to_cbor", Fixnum_to_msgpack, -1);
185
+ rb_define_method(rb_cBignum, "to_cbor", Bignum_to_msgpack, -1);
186
+ #endif
187
+ rb_define_method(rb_cFloat, "to_cbor", Float_to_msgpack, -1);
188
+ rb_define_method(rb_cString, "to_cbor", String_to_msgpack, -1);
189
+ rb_define_method(rb_cArray, "to_cbor", Array_to_msgpack, -1);
190
+ rb_define_method(rb_cHash, "to_cbor", Hash_to_msgpack, -1);
191
+ rb_define_method(rb_cSymbol, "to_cbor", Symbol_to_msgpack, -1);
192
+ rb_define_method(rb_cTime, "to_cbor", Time_to_msgpack, -1);
193
+ rb_define_method(rb_cRegexp, "to_cbor", Regexp_to_msgpack, -1);
194
+ if (rb_const_defined(rb_cObject, rb_intern("URI"))) {
195
+ rb_define_method(rb_const_get(rb_cObject, rb_intern("URI")),
196
+ "to_cbor", URI_to_msgpack, -1);
197
+ }
198
+ rb_define_method(rb_cCBOR_Simple, "to_cbor", Simple_to_msgpack, -1);
199
+ rb_define_method(rb_cCBOR_Tagged, "to_cbor", Tagged_to_msgpack, -1);
200
+ }
201
+