cbor 0.5.6.2

Sign up to get free protection for your applications and to get access to all the features.
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,304 @@
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 "compat.h"
29
+ #include "ruby.h"
30
+ #include "packer.h"
31
+ #include "packer_class.h"
32
+ #include "buffer_class.h"
33
+
34
+ VALUE cMessagePack_Packer;
35
+
36
+ static ID s_to_msgpack;
37
+ static ID s_write;
38
+
39
+ //static VALUE s_packer_value;
40
+ //static msgpack_packer_t* s_packer;
41
+
42
+ #define PACKER(from, name) \
43
+ msgpack_packer_t* name; \
44
+ Data_Get_Struct(from, msgpack_packer_t, name); \
45
+ if(name == NULL) { \
46
+ rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
47
+ }
48
+
49
+ static void Packer_free(msgpack_packer_t* pk)
50
+ {
51
+ if(pk == NULL) {
52
+ return;
53
+ }
54
+ msgpack_packer_destroy(pk);
55
+ free(pk);
56
+ }
57
+
58
+ static VALUE Packer_alloc(VALUE klass)
59
+ {
60
+ msgpack_packer_t* pk = ALLOC_N(msgpack_packer_t, 1);
61
+ msgpack_packer_init(pk);
62
+
63
+ VALUE self = Data_Wrap_Struct(klass, msgpack_packer_mark, Packer_free, pk);
64
+
65
+ msgpack_packer_set_to_msgpack_method(pk, s_to_msgpack, self);
66
+ pk->buffer_ref = MessagePack_Buffer_wrap(PACKER_BUFFER_(pk), self);
67
+
68
+ return self;
69
+ }
70
+
71
+ static VALUE Packer_initialize(int argc, VALUE* argv, VALUE self)
72
+ {
73
+ VALUE io = Qnil;
74
+ VALUE options = Qnil;
75
+
76
+ if(argc == 0 || (argc == 1 && argv[0] == Qnil)) {
77
+ /* Qnil */
78
+
79
+ } else if(argc == 1) {
80
+ VALUE v = argv[0];
81
+ if(rb_type(v) == T_HASH) {
82
+ options = v;
83
+ } else {
84
+ io = v;
85
+ }
86
+
87
+ } else if(argc == 2) {
88
+ io = argv[0];
89
+ options = argv[1];
90
+ if(rb_type(options) != T_HASH) {
91
+ rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(io));
92
+ }
93
+
94
+ } else {
95
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
96
+ }
97
+
98
+ PACKER(self, pk);
99
+ if(io != Qnil || options != Qnil) {
100
+ MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, options);
101
+ }
102
+
103
+ // TODO options
104
+
105
+ return self;
106
+ }
107
+
108
+ static VALUE Packer_buffer(VALUE self)
109
+ {
110
+ PACKER(self, pk);
111
+ return pk->buffer_ref;
112
+ }
113
+
114
+ static VALUE Packer_write(VALUE self, VALUE v)
115
+ {
116
+ PACKER(self, pk);
117
+ msgpack_packer_write_value(pk, v);
118
+ return self;
119
+ }
120
+
121
+ static VALUE Packer_write_nil(VALUE self)
122
+ {
123
+ PACKER(self, pk);
124
+ msgpack_packer_write_nil(pk);
125
+ return self;
126
+ }
127
+
128
+ static VALUE Packer_write_array_header(VALUE self, VALUE n)
129
+ {
130
+ PACKER(self, pk);
131
+ msgpack_packer_write_array_header(pk, NUM2UINT(n));
132
+ return self;
133
+ }
134
+
135
+ static VALUE Packer_write_map_header(VALUE self, VALUE n)
136
+ {
137
+ PACKER(self, pk);
138
+ msgpack_packer_write_map_header(pk, NUM2UINT(n));
139
+ return self;
140
+ }
141
+
142
+ static VALUE Packer_flush(VALUE self)
143
+ {
144
+ PACKER(self, pk);
145
+ msgpack_buffer_flush(PACKER_BUFFER_(pk));
146
+ return self;
147
+ }
148
+
149
+ static VALUE Packer_clear(VALUE self)
150
+ {
151
+ PACKER(self, pk);
152
+ msgpack_buffer_clear(PACKER_BUFFER_(pk));
153
+ return Qnil;
154
+ }
155
+
156
+ static VALUE Packer_size(VALUE self)
157
+ {
158
+ PACKER(self, pk);
159
+ size_t size = msgpack_buffer_all_readable_size(PACKER_BUFFER_(pk));
160
+ return SIZET2NUM(size);
161
+ }
162
+
163
+ static VALUE Packer_empty_p(VALUE self)
164
+ {
165
+ PACKER(self, pk);
166
+ if(msgpack_buffer_top_readable_size(PACKER_BUFFER_(pk)) == 0) {
167
+ return Qtrue;
168
+ } else {
169
+ return Qfalse;
170
+ }
171
+ }
172
+
173
+ static VALUE Packer_to_str(VALUE self)
174
+ {
175
+ PACKER(self, pk);
176
+ return msgpack_buffer_all_as_string(PACKER_BUFFER_(pk));
177
+ }
178
+
179
+ static VALUE Packer_to_a(VALUE self)
180
+ {
181
+ PACKER(self, pk);
182
+ return msgpack_buffer_all_as_string_array(PACKER_BUFFER_(pk));
183
+ }
184
+
185
+ static VALUE Packer_write_to(VALUE self, VALUE io)
186
+ {
187
+ PACKER(self, pk);
188
+ size_t sz = msgpack_buffer_flush_to_io(PACKER_BUFFER_(pk), io, s_write, true);
189
+ return ULONG2NUM(sz);
190
+ }
191
+
192
+ //static VALUE Packer_append(VALUE self, VALUE string_or_buffer)
193
+ //{
194
+ // PACKER(self, pk);
195
+ //
196
+ // // TODO if string_or_buffer is a Buffer
197
+ // VALUE string = string_or_buffer;
198
+ //
199
+ // msgpack_buffer_append_string(PACKER_BUFFER_(pk), string);
200
+ //
201
+ // return self;
202
+ //}
203
+
204
+ VALUE MessagePack_pack(int argc, VALUE* argv)
205
+ {
206
+ // TODO options
207
+
208
+ VALUE v;
209
+ VALUE io = Qnil;
210
+
211
+ switch(argc) {
212
+ case 2:
213
+ io = argv[1];
214
+ /* pass-through */
215
+ case 1:
216
+ v = argv[0];
217
+ break;
218
+ default:
219
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
220
+ }
221
+
222
+ VALUE self = Packer_alloc(cMessagePack_Packer);
223
+ PACKER(self, pk);
224
+ //msgpack_packer_reset(s_packer);
225
+ //msgpack_buffer_reset_io(PACKER_BUFFER_(s_packer));
226
+
227
+ if(io != Qnil) {
228
+ MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, Qnil);
229
+ }
230
+
231
+ msgpack_packer_write_value(pk, v);
232
+
233
+ VALUE retval;
234
+ if(io != Qnil) {
235
+ msgpack_buffer_flush(PACKER_BUFFER_(pk));
236
+ retval = Qnil;
237
+ } else {
238
+ retval = msgpack_buffer_all_as_string(PACKER_BUFFER_(pk));
239
+ }
240
+
241
+ msgpack_buffer_clear(PACKER_BUFFER_(pk)); /* to free rmem before GC */
242
+
243
+ #ifdef RB_GC_GUARD
244
+ /* This prevents compilers from optimizing out the `self` variable
245
+ * from stack. Otherwise GC free()s it. */
246
+ RB_GC_GUARD(self);
247
+ #endif
248
+
249
+ return retval;
250
+ }
251
+
252
+ static VALUE MessagePack_dump_module_method(int argc, VALUE* argv, VALUE mod)
253
+ {
254
+ UNUSED(mod);
255
+ return MessagePack_pack(argc, argv);
256
+ }
257
+
258
+ static VALUE MessagePack_pack_module_method(int argc, VALUE* argv, VALUE mod)
259
+ {
260
+ UNUSED(mod);
261
+ return MessagePack_pack(argc, argv);
262
+ }
263
+
264
+ void MessagePack_Packer_module_init(VALUE mMessagePack)
265
+ {
266
+ s_to_msgpack = rb_intern("to_cbor");
267
+ s_write = rb_intern("write");
268
+
269
+ msgpack_packer_static_init();
270
+
271
+ cMessagePack_Packer = rb_define_class_under(mMessagePack, "Packer", rb_cObject);
272
+
273
+ rb_define_alloc_func(cMessagePack_Packer, Packer_alloc);
274
+
275
+ rb_define_method(cMessagePack_Packer, "initialize", Packer_initialize, -1);
276
+ rb_define_method(cMessagePack_Packer, "buffer", Packer_buffer, 0);
277
+ rb_define_method(cMessagePack_Packer, "write", Packer_write, 1);
278
+ rb_define_alias(cMessagePack_Packer, "pack", "write");
279
+ rb_define_method(cMessagePack_Packer, "write_nil", Packer_write_nil, 0);
280
+ rb_define_method(cMessagePack_Packer, "write_array_header", Packer_write_array_header, 1);
281
+ rb_define_method(cMessagePack_Packer, "write_map_header", Packer_write_map_header, 1);
282
+ rb_define_method(cMessagePack_Packer, "flush", Packer_flush, 0);
283
+
284
+ /* delegation methods */
285
+ rb_define_method(cMessagePack_Packer, "clear", Packer_clear, 0);
286
+ rb_define_method(cMessagePack_Packer, "size", Packer_size, 0);
287
+ rb_define_method(cMessagePack_Packer, "empty?", Packer_empty_p, 0);
288
+ rb_define_method(cMessagePack_Packer, "write_to", Packer_write_to, 1);
289
+ rb_define_method(cMessagePack_Packer, "to_str", Packer_to_str, 0);
290
+ rb_define_alias(cMessagePack_Packer, "to_s", "to_str");
291
+ rb_define_method(cMessagePack_Packer, "to_a", Packer_to_a, 0);
292
+ //rb_define_method(cMessagePack_Packer, "append", Packer_append, 1);
293
+ //rb_define_alias(cMessagePack_Packer, "<<", "append");
294
+
295
+ //s_packer_value = Packer_alloc(cMessagePack_Packer);
296
+ //rb_gc_register_address(&s_packer_value);
297
+ //Data_Get_Struct(s_packer_value, msgpack_packer_t, s_packer);
298
+
299
+ /* MessagePack.pack(x) */
300
+ rb_define_module_function(mMessagePack, "pack", MessagePack_pack_module_method, -1);
301
+ rb_define_module_function(mMessagePack, "encode", MessagePack_pack_module_method, -1);
302
+ rb_define_module_function(mMessagePack, "dump", MessagePack_dump_module_method, -1);
303
+ }
304
+
@@ -0,0 +1,39 @@
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_PACKER_CLASS_H__
28
+ #define MSGPACK_RUBY_PACKER_CLASS_H__
29
+
30
+ #include "packer.h"
31
+
32
+ extern VALUE cMessagePack_Packer;
33
+
34
+ void MessagePack_Packer_module_init(VALUE mMessagePack);
35
+
36
+ VALUE MessagePack_pack(int argc, VALUE* argv);
37
+
38
+ #endif
39
+
data/ext/cbor/rbinit.c ADDED
@@ -0,0 +1,51 @@
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 "buffer_class.h"
29
+ #include "packer_class.h"
30
+ #include "unpacker_class.h"
31
+ #include "core_ext.h"
32
+
33
+
34
+ VALUE rb_cCBOR_Tagged;
35
+ VALUE rb_cCBOR_Simple;
36
+
37
+ void Init_cbor(void)
38
+ {
39
+ VALUE mMessagePack = rb_define_module("CBOR");
40
+
41
+ rb_cCBOR_Tagged = rb_struct_define(NULL, "tag", "value", NULL);
42
+ rb_define_const(mMessagePack, "Tagged", rb_cCBOR_Tagged);
43
+ rb_cCBOR_Simple = rb_struct_define(NULL, "value", NULL);
44
+ rb_define_const(mMessagePack, "Simple", rb_cCBOR_Simple);
45
+
46
+ MessagePack_Buffer_module_init(mMessagePack);
47
+ MessagePack_Packer_module_init(mMessagePack);
48
+ MessagePack_Unpacker_module_init(mMessagePack);
49
+ MessagePack_core_ext_module_init();
50
+ }
51
+
@@ -0,0 +1,56 @@
1
+ #define MessagePack_Buffer_initialize CBOR_Buffer_initialize
2
+ #define MessagePack_Buffer_module_init CBOR_Buffer_module_init
3
+ #define MessagePack_Buffer_wrap CBOR_Buffer_wrap
4
+ #define MessagePack_Packer_module_init CBOR_Packer_module_init
5
+ #define MessagePack_Unpacker_module_init CBOR_Unpacker_module_init
6
+ #define MessagePack_core_ext_module_init CBOR_core_ext_module_init
7
+ #define MessagePack_pack CBOR_pack
8
+ #define MessagePack_unpack CBOR_unpack
9
+ #define _msgpack_buffer_append_long_string _CBOR_buffer_append_long_string
10
+ #define _msgpack_buffer_expand _CBOR_buffer_expand
11
+ #define _msgpack_buffer_feed_from_io _CBOR_buffer_feed_from_io
12
+ #define _msgpack_buffer_read_all2 _CBOR_buffer_read_all2
13
+ #define _msgpack_buffer_read_from_io_to_string _CBOR_buffer_read_from_io_to_string
14
+ #define _msgpack_buffer_shift_chunk _CBOR_buffer_shift_chunk
15
+ #define _msgpack_buffer_skip_from_io _CBOR_buffer_skip_from_io
16
+ #define _msgpack_rmem_alloc2 _CBOR_rmem_alloc2
17
+ #define _msgpack_rmem_chunk_free _CBOR_rmem_chunk_free
18
+ #define cMessagePack_Buffer cCBOR_Buffer
19
+ #define cMessagePack_Packer cCBOR_Packer
20
+ #define cMessagePack_Unpacker cCBOR_Unpacker
21
+ #define msgpack_buffer_all_as_string CBOR_buffer_all_as_string
22
+ #define msgpack_buffer_all_as_string_array CBOR_buffer_all_as_string_array
23
+ #define msgpack_buffer_all_readable_size CBOR_buffer_all_readable_size
24
+ #define msgpack_buffer_clear CBOR_buffer_clear
25
+ #define msgpack_buffer_destroy CBOR_buffer_destroy
26
+ #define msgpack_buffer_flush_to_io CBOR_buffer_flush_to_io
27
+ #define msgpack_buffer_init CBOR_buffer_init
28
+ #define msgpack_buffer_mark CBOR_buffer_mark
29
+ #define msgpack_buffer_read_nonblock CBOR_buffer_read_nonblock
30
+ #define msgpack_buffer_read_to_string_nonblock CBOR_buffer_read_to_string_nonblock
31
+ #define msgpack_buffer_static_destroy CBOR_buffer_static_destroy
32
+ #define msgpack_buffer_static_init CBOR_buffer_static_init
33
+ #define msgpack_packer_destroy CBOR_packer_destroy
34
+ #define msgpack_packer_init CBOR_packer_init
35
+ #define msgpack_packer_mark CBOR_packer_mark
36
+ #define msgpack_packer_reset CBOR_packer_reset
37
+ #define msgpack_packer_static_destroy CBOR_packer_static_destroy
38
+ #define msgpack_packer_static_init CBOR_packer_static_init
39
+ #define msgpack_packer_write_array_value CBOR_packer_write_array_value
40
+ #define msgpack_packer_write_hash_value CBOR_packer_write_hash_value
41
+ #define msgpack_packer_write_value CBOR_packer_write_value
42
+ #define msgpack_rmem_destroy CBOR_rmem_destroy
43
+ #define msgpack_rmem_init CBOR_rmem_init
44
+ #define msgpack_unpacker_destroy CBOR_unpacker_destroy
45
+ #define msgpack_unpacker_init CBOR_unpacker_init
46
+ #define msgpack_unpacker_mark CBOR_unpacker_mark
47
+ #define msgpack_unpacker_peek_next_object_type CBOR_unpacker_peek_next_object_type
48
+ #define msgpack_unpacker_read CBOR_unpacker_read
49
+ #define msgpack_unpacker_read_array_header CBOR_unpacker_read_array_header
50
+ #define msgpack_unpacker_read_container_header CBOR_unpacker_read_container_header
51
+ #define msgpack_unpacker_read_map_header CBOR_unpacker_read_map_header
52
+ #define msgpack_unpacker_reset CBOR_unpacker_reset
53
+ #define msgpack_unpacker_skip CBOR_unpacker_skip
54
+ #define msgpack_unpacker_skip_nil CBOR_unpacker_skip_nil
55
+ #define msgpack_unpacker_static_destroy CBOR_unpacker_static_destroy
56
+ #define msgpack_unpacker_static_init CBOR_unpacker_static_init