cbor 0.5.6.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.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.travis.yml +5 -0
  4. data/ChangeLog +87 -0
  5. data/README.rdoc +180 -0
  6. data/Rakefile +94 -0
  7. data/cbor.gemspec +26 -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/buffer.c +693 -0
  17. data/ext/cbor/buffer.h +469 -0
  18. data/ext/cbor/buffer_class.c +516 -0
  19. data/ext/cbor/buffer_class.h +41 -0
  20. data/ext/cbor/cbor.h +69 -0
  21. data/ext/cbor/compat.h +136 -0
  22. data/ext/cbor/core_ext.c +181 -0
  23. data/ext/cbor/core_ext.h +35 -0
  24. data/ext/cbor/extconf.rb +25 -0
  25. data/ext/cbor/packer.c +169 -0
  26. data/ext/cbor/packer.h +337 -0
  27. data/ext/cbor/packer_class.c +304 -0
  28. data/ext/cbor/packer_class.h +39 -0
  29. data/ext/cbor/rbinit.c +51 -0
  30. data/ext/cbor/renamer.h +56 -0
  31. data/ext/cbor/rmem.c +103 -0
  32. data/ext/cbor/rmem.h +118 -0
  33. data/ext/cbor/sysdep.h +135 -0
  34. data/ext/cbor/sysdep_endian.h +59 -0
  35. data/ext/cbor/sysdep_types.h +55 -0
  36. data/ext/cbor/unpacker.c +735 -0
  37. data/ext/cbor/unpacker.h +133 -0
  38. data/ext/cbor/unpacker_class.c +417 -0
  39. data/ext/cbor/unpacker_class.h +39 -0
  40. data/lib/cbor.rb +9 -0
  41. data/lib/cbor/version.rb +3 -0
  42. data/spec/buffer_io_spec.rb +260 -0
  43. data/spec/buffer_spec.rb +576 -0
  44. data/spec/cases.cbor +0 -0
  45. data/spec/cases.cbor_stream +0 -0
  46. data/spec/cases.json +1 -0
  47. data/spec/cases.msg +0 -0
  48. data/spec/cases_compact.msg +0 -0
  49. data/spec/cases_spec.rb +39 -0
  50. data/spec/format_spec.rb +445 -0
  51. data/spec/packer_spec.rb +127 -0
  52. data/spec/random_compat.rb +24 -0
  53. data/spec/spec_helper.rb +45 -0
  54. data/spec/unpacker_spec.rb +238 -0
  55. metadata +196 -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,136 @@
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
+ /* More MRI 1.8 */
109
+ #ifndef RBIGNUM_LEN
110
+ #define RBIGNUM_LEN(b) (RBIGNUM(b)->len)
111
+ #endif
112
+ #ifndef RBIGNUM_DIGITS
113
+ #ifndef RBIGNUM
114
+ #define CANT_DO_BIGNUMS_FAST_ON_THIS_PLATFORM
115
+ #endif
116
+ #define RBIGNUM_DIGITS(b) (RBIGNUM(b)->digits)
117
+ #endif
118
+ #ifndef HAVE_RB_BIG_NEW
119
+ /* gross 1.8.7 hack thanks to Mathieu Bouchard <matju@artengine.ca> */
120
+ #define rb_big_new(len, sign) rb_funcall(INT2FIX(1),rb_intern("<<"),1,INT2FIX(len > 0 ? ((len) * SIZEOF_BDIGITS * 8) - 1 : 0));
121
+ #endif
122
+
123
+ /*
124
+ * RSTRING_PTR, RSTRING_LEN
125
+ */
126
+ #ifndef RSTRING_PTR /* MRI 1.8.5 */
127
+ # define RSTRING_PTR(s) (RSTRING(s)->ptr)
128
+ #endif
129
+
130
+ #ifndef RSTRING_LEN /* MRI 1.8.5 */
131
+ # define RSTRING_LEN(s) (RSTRING(s)->len)
132
+ #endif
133
+
134
+
135
+ #endif
136
+
@@ -0,0 +1,181 @@
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
+ static VALUE Fixnum_to_msgpack(int argc, VALUE* argv, VALUE self)
77
+ {
78
+ ENSURE_PACKER(argc, argv, packer, pk);
79
+ msgpack_packer_write_fixnum_value(pk, self);
80
+ return packer;
81
+ }
82
+
83
+ static VALUE Bignum_to_msgpack(int argc, VALUE* argv, VALUE self)
84
+ {
85
+ ENSURE_PACKER(argc, argv, packer, pk);
86
+ msgpack_packer_write_bignum_value(pk, self);
87
+ return packer;
88
+ }
89
+
90
+ static VALUE Float_to_msgpack(int argc, VALUE* argv, VALUE self)
91
+ {
92
+ ENSURE_PACKER(argc, argv, packer, pk);
93
+ msgpack_packer_write_float_value(pk, self);
94
+ return packer;
95
+ }
96
+
97
+ static VALUE String_to_msgpack(int argc, VALUE* argv, VALUE self)
98
+ {
99
+ ENSURE_PACKER(argc, argv, packer, pk);
100
+ msgpack_packer_write_string_value(pk, self);
101
+ return packer;
102
+ }
103
+
104
+ static VALUE Array_to_msgpack(int argc, VALUE* argv, VALUE self)
105
+ {
106
+ ENSURE_PACKER(argc, argv, packer, pk);
107
+ msgpack_packer_write_array_value(pk, self);
108
+ return packer;
109
+ }
110
+
111
+ static VALUE Hash_to_msgpack(int argc, VALUE* argv, VALUE self)
112
+ {
113
+ ENSURE_PACKER(argc, argv, packer, pk);
114
+ msgpack_packer_write_hash_value(pk, self);
115
+ return packer;
116
+ }
117
+
118
+ static VALUE Symbol_to_msgpack(int argc, VALUE* argv, VALUE self)
119
+ {
120
+ ENSURE_PACKER(argc, argv, packer, pk);
121
+ msgpack_packer_write_symbol_value(pk, self);
122
+ return packer;
123
+ }
124
+
125
+ static VALUE Simple_to_msgpack(int argc, VALUE* argv, VALUE self)
126
+ {
127
+ ENSURE_PACKER(argc, argv, packer, pk);
128
+ msgpack_packer_write_simple_value(pk, self);
129
+ return packer;
130
+ }
131
+
132
+ static VALUE Tagged_to_msgpack(int argc, VALUE* argv, VALUE self)
133
+ {
134
+ ENSURE_PACKER(argc, argv, packer, pk);
135
+ msgpack_packer_write_tagged_value(pk, self);
136
+ return packer;
137
+ }
138
+
139
+ static VALUE Regexp_to_msgpack(int argc, VALUE* argv, VALUE self)
140
+ {
141
+ ENSURE_PACKER(argc, argv, packer, pk);
142
+ msgpack_packer_write_processed_value(pk, self, rb_intern("source"), TAG_RE);
143
+ return packer;
144
+ }
145
+
146
+ static VALUE URI_to_msgpack(int argc, VALUE* argv, VALUE self)
147
+ {
148
+ ENSURE_PACKER(argc, argv, packer, pk);
149
+ msgpack_packer_write_processed_value(pk, self, rb_intern("to_s"), TAG_URI);
150
+ return packer;
151
+ }
152
+
153
+ static VALUE Time_to_msgpack(int argc, VALUE* argv, VALUE self)
154
+ {
155
+ ENSURE_PACKER(argc, argv, packer, pk);
156
+ msgpack_packer_write_processed_value(pk, self, rb_intern("to_i"), TAG_TIME_EPOCH);
157
+ return packer;
158
+ }
159
+
160
+ void MessagePack_core_ext_module_init()
161
+ {
162
+ rb_define_method(rb_cNilClass, "to_cbor", NilClass_to_msgpack, -1);
163
+ rb_define_method(rb_cTrueClass, "to_cbor", TrueClass_to_msgpack, -1);
164
+ rb_define_method(rb_cFalseClass, "to_cbor", FalseClass_to_msgpack, -1);
165
+ rb_define_method(rb_cFixnum, "to_cbor", Fixnum_to_msgpack, -1);
166
+ rb_define_method(rb_cBignum, "to_cbor", Bignum_to_msgpack, -1);
167
+ rb_define_method(rb_cFloat, "to_cbor", Float_to_msgpack, -1);
168
+ rb_define_method(rb_cString, "to_cbor", String_to_msgpack, -1);
169
+ rb_define_method(rb_cArray, "to_cbor", Array_to_msgpack, -1);
170
+ rb_define_method(rb_cHash, "to_cbor", Hash_to_msgpack, -1);
171
+ rb_define_method(rb_cSymbol, "to_cbor", Symbol_to_msgpack, -1);
172
+ rb_define_method(rb_cTime, "to_cbor", Time_to_msgpack, -1);
173
+ rb_define_method(rb_cRegexp, "to_cbor", Regexp_to_msgpack, -1);
174
+ if (rb_const_defined(rb_cObject, rb_intern("URI"))) {
175
+ rb_define_method(rb_const_get(rb_cObject, rb_intern("URI")),
176
+ "to_cbor", URI_to_msgpack, -1);
177
+ }
178
+ rb_define_method(rb_cCBOR_Simple, "to_cbor", Simple_to_msgpack, -1);
179
+ rb_define_method(rb_cCBOR_Tagged, "to_cbor", Tagged_to_msgpack, -1);
180
+ }
181
+
@@ -0,0 +1,35 @@
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_CORE_EXT_H__
28
+ #define MSGPACK_RUBY_CORE_EXT_H__
29
+
30
+ #include "compat.h"
31
+
32
+ void MessagePack_core_ext_module_init();
33
+
34
+ #endif
35
+