cbor 0.5.6.2 → 0.5.10.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 95c5af13f98f6a6442e2a2b9c27ed4bdef1c5813
4
- data.tar.gz: 220ff8d73a25a32486e70a3ae863516193771661
2
+ SHA256:
3
+ metadata.gz: 350f51028ed72c54fb5adf9ab595f08e18384cecb8c82d874d195aab6eb16a65
4
+ data.tar.gz: 1eb340e507d590530f0c2d9a3cfaf723fb9210596a312443e7b2dc95b912614f
5
5
  SHA512:
6
- metadata.gz: e9ff567a84e861e9dea28e28465b14f5ce0bea61ebd5a964c57abb53dea6e7841d26e2396a98a8e97c0250336465b221f8b0f44709254b97d776859e904cb471
7
- data.tar.gz: 43bbd8691a9120b91385b1f0089fb3ee92d282b8ec2e63c617be1063e588849e2f84c57ad9425352c2263bf7d7cbe4fee4461b38957078784b2f0995b874cbd7
6
+ metadata.gz: 30fb71f7d3fce1bbf5f40c84d7ca389a1e30051c186c01553d0be07e784a4029dc27cdf6c01a70dd1a155c6bddd050d21705c642e5df6727633d15b39808d153
7
+ data.tar.gz: 75783b66fccc0dd03ce323411f81848964e0904805e5eeb9218cc59214d1bfd1a3011df255de2b6a95a92b1d42475d5fb770a0029d6486c6c4060605a3e24f94
data/.gitignore CHANGED
@@ -3,7 +3,7 @@
3
3
  *.gem
4
4
  *.class
5
5
  .bundle
6
- Gemfile*
6
+ Gemfile.lock
7
7
  pkg
8
8
  test/debug.log
9
9
  *~
data/.travis.yml CHANGED
@@ -1,5 +1,24 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
3
  - 2.0.0
4
+ - 2.1.10
5
+ - 2.2.10
6
+ - 2.3.8
7
+ - 2.4.6
8
+ - 2.5.5
9
+ - 2.6.3
10
+ - ruby-head
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: 2.0.0
14
+ - rvm: 2.1.10
15
+ - rvm: 2.2.10
16
+ - rvm: ruby-head
17
+
18
+ gemfile:
19
+ - Gemfile
20
+
5
21
  script: "bundle exec rake spec"
22
+
23
+ notifications:
24
+ irc: "irc.freenode.org#coap"
data/ChangeLog CHANGED
@@ -1,3 +1,22 @@
1
+ 2016-04-11 Carsten Bormann <cabo@tzi.org>
2
+
3
+ * 0.5.9.1: Fix WIN32 problem inherited from upstream
4
+
5
+ 2014-12-13 Carsten Bormann <cabo@tzi.org>
6
+
7
+ * 0.5.9.0: Upstream fixes; prepare for Ruby 2.2 (use GC-able
8
+ symbols for :keys_as_symbols, use rb_integer_pack if available)
9
+
10
+ 2014-12-12 Carsten Bormann <cabo@tzi.org>
11
+
12
+ * 0.5.8.0: Upstream fixes (including {symbolize_keys: true} as an
13
+ alternative for :keys_as_symbols); README updates.
14
+
15
+ 2014-02-09 Carsten Bormann <cabo@tzi.org>
16
+
17
+ * 0.5.6.4: Add more checking for bare indefinite breaks and
18
+ non-strings inside indefinite strings.
19
+
1
20
  2013-08-17 Carsten Bormann <cabo@tzi.org>
2
21
 
3
22
  * First version of CBOR variant of this code.
data/Gemfile CHANGED
@@ -3,9 +3,9 @@ source 'https://rubygems.org/'
3
3
  gemspec
4
4
 
5
5
  group :test do
6
- gem "rake", "~> 10.0.0"
7
- gem 'rake-compiler', '~> 0.8.3'
8
- gem 'rspec', '~> 2.11'
9
- gem 'json', '~> 1.7'
10
- gem 'yard', '~> 0.8.2'
6
+ gem "rake"
7
+ gem 'rake-compiler'
8
+ gem 'rspec'
9
+ gem 'json'
10
+ gem 'yard'
11
11
  end
data/README.rdoc CHANGED
@@ -1,16 +1,24 @@
1
1
  = CBOR for Ruby
2
2
 
3
- This is an initial Ruby implementation of the CBOR encoding, based on
3
+ This is a Ruby implementation of the CBOR[http://cbor.io] encoding, based on
4
4
  the (polished) high-performance msgpack-ruby code.
5
5
 
6
6
  Documentation will follow, but generally, if you replace MessagePack
7
- and msgpack with CBOR and cbor in the below, you will be on the right
8
- track.
7
+ and msgpack with CBOR and cbor in the text cited from MessagePack
8
+ below, you will be on the right track. For a starter:
9
+
10
+ require 'cbor'
11
+ s = [1, 2, 33.5, 4].to_cbor #=> "\x84\x01\x02\xF9P0\x04"
12
+ CBOR.decode(s) #=> [1, 2, 33.5, 4]
13
+
14
+ Use RubyGems to install:
15
+
16
+ gem install cbor
9
17
 
10
18
  CBOR is an object representation format defined by the IETF[http://ietf.org].
11
- The specification[http://tools.ietf.org/html/draft-bormann-cbor]
12
- has recently been approved as an IETF Standards-Track specification
13
- and will be an RFC in a couple of months, after the RFC editor has done their work.
19
+ The specification[http://tools.ietf.org/html/rfc8949]
20
+ is an IETF Standards-Track specification
21
+ and has been published as RFC 8949 (superseding the older RFC 7049).
14
22
 
15
23
  This is all based on wonderful work by frsyuki, and I have no idea how
16
24
  to acknowledge him appropriately. This gem is not intended to fork or
@@ -25,15 +33,18 @@ Todos:
25
33
  percent slower than msgpack-ruby.
26
34
 
27
35
  * Properly document things, in particular the classes CBOR::Simple and
28
- CBOR::Tagged. If you check out the source, you can +rake doc+ to
36
+ CBOR::Tagged. If you check out the source, you can +rake+ +doc+ to
29
37
  get some documentation in the directory +doc+ (see +index.html+ there).
30
38
 
31
- * So far, this has been tested on MRI (1.9.3 and 2.0.0). To do:
32
- * Check this code out with Rubinius.
33
- * Publish the pure-ruby version and make it work the same way on JRuby.
39
+ * Cover more rubies.
40
+ * \[✔✔✔✔] tested on MRI (1.9.3, 2.0.0, 2.1.10, 2.2.10, 2.3.7, 2.4.4 and 2.5.1).
41
+ * (\[✔] There now also is some basic MRI 1.8.7 compatibility, however 1.8.7 does not support differentiation between byte and text strings.)
42
+ * \[✔] tested on Rubinius 2.4.1.
43
+ * \[_] Publish the pure-ruby version and make it work the same way on JRuby.
34
44
 
35
45
  * Find and implement good ways to offer CBOR's indefinite length
36
- ("streaming") capability at the Ruby API level.
46
+ ("streaming") capability at the Ruby API level. (Decoding is fully
47
+ supported, just no streaming or indefinite length encoding.)
37
48
 
38
49
  * Rename some of the internals from msgpack to cbor. Right now, much
39
50
  of the code still uses the name msgpack in its identifiers, to
@@ -44,10 +55,10 @@ Same Apache 2.0 License applies to the changes as to the original.
44
55
  For the changes:
45
56
 
46
57
  Author:: Carsten Bormann <cabo@tzi.org>
47
- Copyright:: Copyright (c) 2013 Carsten Bormann
58
+ Copyright:: Copyright (c) 2013, 2014 Carsten Bormann
48
59
  License:: Apache License, Version 2.0
49
60
 
50
- {<img src="https://travis-ci.org/cabo/cbor-ruby.png?branch=master" />}[https://travis-ci.org/cabo/cbor-ruby]
61
+ {<img src="https://travis-ci.org/cabo/cbor-ruby.svg?branch=master" />}[https://travis-ci.org/cabo/cbor-ruby] {<img src="https://badge.fury.io/rb/cbor.svg" alt="Gem Version" />}[http://badge.fury.io/rb/cbor]
51
62
 
52
63
  For the original, see below.
53
64
 
data/Rakefile CHANGED
@@ -52,6 +52,9 @@ else
52
52
  end
53
53
  end
54
54
 
55
+ CLEAN.include('lib/cbor/*.jar')
56
+ CLEAN.include('lib/cbor/cbor.*')
57
+
55
58
  task :default => :build
56
59
 
57
60
 
data/cbor.gemspec CHANGED
@@ -8,19 +8,18 @@ Gem::Specification.new do |s|
8
8
  s.description = %q{CBOR is a library for the CBOR binary object representation format, based on Sadayuki Furuhashi's MessagePack library.}
9
9
  s.author = "Carsten Bormann, standing on the tall shoulders of Sadayuki Furuhashi"
10
10
  s.email = "cabo@tzi.org"
11
- s.license = "Apache 2.0"
12
- # s.homepage = "http://msgpack.org/"
13
- # s.rubyforge_project = "msgpack"
14
- s.has_rdoc = false
11
+ s.license = "Apache-2.0"
12
+ s.homepage = "http://cbor.io/"
13
+ # s.has_rdoc = false
15
14
  s.files = `git ls-files`.split("\n")
16
15
  s.test_files = `git ls-files -- {test,spec}/*`.split("\n")
17
16
  s.require_paths = ["lib"]
18
17
  s.extensions = ["ext/cbor/extconf.rb"]
19
18
 
20
- s.add_development_dependency 'bundler', ['~> 1.0']
19
+ s.add_development_dependency 'bundler'
21
20
  s.add_development_dependency 'rake', ['~> 0.9.2']
22
21
  s.add_development_dependency 'rake-compiler', ['~> 0.8.3']
23
22
  s.add_development_dependency 'rspec', ['~> 2.11']
24
23
  s.add_development_dependency 'json', ['~> 1.7']
25
- s.add_development_dependency 'yard', ['~> 0.8.2']
24
+ s.add_development_dependency 'yard', ['~> 0.9.11']
26
25
  end
data/ext/cbor/buffer.h CHANGED
@@ -454,7 +454,22 @@ static inline VALUE msgpack_buffer_read_top_as_string(msgpack_buffer_t* b, size_
454
454
 
455
455
  VALUE result;
456
456
  if (as_symbol) {
457
+ #ifndef HAVE_RB_STR_INTERN
458
+ #ifndef HAVE_RB_INTERN_STR
459
+ /* MRI 1.8 doesn't have rb_intern_str or rb_intern2, hack it... */
460
+ char *tmp = xmalloc(length+1);
461
+ memcpy(tmp, b->read_buffer, length);
462
+ tmp[length] = 0;
463
+ result = ID2SYM(rb_intern(tmp));
464
+ xfree(tmp);
465
+ #else
457
466
  result = ID2SYM(rb_intern2(b->read_buffer, length));
467
+ /* FIXME: This is stuck at ASCII encoding */
468
+ #endif
469
+ #else
470
+ /* enable GC-able symbols here: */
471
+ result = rb_str_intern(rb_str_new(b->read_buffer, length));
472
+ #endif
458
473
  /* todo: rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc) */
459
474
  } else {
460
475
  result = rb_str_new(b->read_buffer, length);
@@ -58,7 +58,7 @@ static void Buffer_free(void* data)
58
58
  }
59
59
  msgpack_buffer_t* b = (msgpack_buffer_t*) data;
60
60
  msgpack_buffer_destroy(b);
61
- free(b);
61
+ xfree(b);
62
62
  }
63
63
 
64
64
  static VALUE Buffer_alloc(VALUE klass)
@@ -254,10 +254,11 @@ static VALUE read_until_eof_rescue(VALUE args)
254
254
  return Qnil;
255
255
  }
256
256
 
257
- static VALUE read_until_eof_error(VALUE args)
257
+ static VALUE read_until_eof_error(VALUE args, VALUE error)
258
258
  {
259
259
  /* ignore EOFError */
260
260
  UNUSED(args);
261
+ UNUSED(error);
261
262
  return Qnil;
262
263
  }
263
264
 
data/ext/cbor/compat.h CHANGED
@@ -105,6 +105,9 @@
105
105
  # endif
106
106
  #endif
107
107
 
108
+
109
+ #ifndef HAVE_RB_INTEGER_UNPACK
110
+
108
111
  /* More MRI 1.8 */
109
112
  #ifndef RBIGNUM_LEN
110
113
  #define RBIGNUM_LEN(b) (RBIGNUM(b)->len)
@@ -116,10 +119,18 @@
116
119
  #define RBIGNUM_DIGITS(b) (RBIGNUM(b)->digits)
117
120
  #endif
118
121
  #ifndef HAVE_RB_BIG_NEW
122
+ /* not really worth fixing any more... */
123
+ #define CANT_DO_BIGNUMS_FAST_ON_THIS_PLATFORM
119
124
  /* gross 1.8.7 hack thanks to Mathieu Bouchard <matju@artengine.ca> */
120
125
  #define rb_big_new(len, sign) rb_funcall(INT2FIX(1),rb_intern("<<"),1,INT2FIX(len > 0 ? ((len) * SIZEOF_BDIGITS * 8) - 1 : 0));
121
126
  #endif
122
127
 
128
+ #endif
129
+
130
+ #ifndef RB_TYPE_P
131
+ #define RB_TYPE_P(obj, type) (TYPE(obj) == (type))
132
+ #endif
133
+
123
134
  /*
124
135
  * RSTRING_PTR, RSTRING_LEN
125
136
  */
data/ext/cbor/core_ext.c CHANGED
@@ -73,6 +73,20 @@ static VALUE FalseClass_to_msgpack(int argc, VALUE* argv, VALUE self)
73
73
  return packer;
74
74
  }
75
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
+
76
90
  static VALUE Fixnum_to_msgpack(int argc, VALUE* argv, VALUE self)
77
91
  {
78
92
  ENSURE_PACKER(argc, argv, packer, pk);
@@ -87,6 +101,8 @@ static VALUE Bignum_to_msgpack(int argc, VALUE* argv, VALUE self)
87
101
  return packer;
88
102
  }
89
103
 
104
+ #endif
105
+
90
106
  static VALUE Float_to_msgpack(int argc, VALUE* argv, VALUE self)
91
107
  {
92
108
  ENSURE_PACKER(argc, argv, packer, pk);
@@ -162,8 +178,12 @@ void MessagePack_core_ext_module_init()
162
178
  rb_define_method(rb_cNilClass, "to_cbor", NilClass_to_msgpack, -1);
163
179
  rb_define_method(rb_cTrueClass, "to_cbor", TrueClass_to_msgpack, -1);
164
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
165
184
  rb_define_method(rb_cFixnum, "to_cbor", Fixnum_to_msgpack, -1);
166
185
  rb_define_method(rb_cBignum, "to_cbor", Bignum_to_msgpack, -1);
186
+ #endif
167
187
  rb_define_method(rb_cFloat, "to_cbor", Float_to_msgpack, -1);
168
188
  rb_define_method(rb_cString, "to_cbor", String_to_msgpack, -1);
169
189
  rb_define_method(rb_cArray, "to_cbor", Array_to_msgpack, -1);
data/ext/cbor/extconf.rb CHANGED
@@ -4,8 +4,12 @@ have_header("ruby/st.h")
4
4
  have_header("st.h")
5
5
  have_func("rb_str_replace", ["ruby.h"])
6
6
  have_func("rb_big_new", ["ruby.h"])
7
+ have_func("rb_intern_str", ["ruby.h"])
8
+ have_func("rb_sym2str", ["ruby.h"])
9
+ have_func("rb_str_intern", ["ruby.h"])
10
+ have_func("rb_integer_unpack", ["ruby.h"])
7
11
 
8
- $CFLAGS << %[ -I.. -Wall -O3 -g -std=c99]
12
+ append_cflags(%w[-I.. -Wall -O3 -g -std=c99])
9
13
  #$CFLAGS << %[ -DDISABLE_RMEM]
10
14
  #$CFLAGS << %[ -DDISABLE_RMEM_REUSE_INTERNAL_FRAGMENT]
11
15
  #$CFLAGS << %[ -DDISABLE_BUFFER_READ_REFERENCE_OPTIMIZE]
@@ -13,9 +17,9 @@ $CFLAGS << %[ -I.. -Wall -O3 -g -std=c99]
13
17
 
14
18
  if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
15
19
  # msgpack-ruby doesn't modify data came from RSTRING_PTR(str)
16
- $CFLAGS << %[ -DRSTRING_NOT_MODIFIED]
20
+ append_cflags('-DRSTRING_NOT_MODIFIED')
17
21
  # Rubinius C extensions don't grab GVL while rmem is not thread safe
18
- $CFLAGS << %[ -DDISABLE_RMEM]
22
+ append_cflags('-DDISABLE_RMEM')
19
23
  end
20
24
 
21
25
  if warnflags = CONFIG['warnflags']
data/ext/cbor/packer.h CHANGED
@@ -80,7 +80,7 @@ static inline void cbor_encoder_write_head(msgpack_packer_t* pk, unsigned int ib
80
80
  {
81
81
  if (n < 24) {
82
82
  msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
83
- msgpack_buffer_write_1(PACKER_BUFFER_(pk), ib + n);
83
+ msgpack_buffer_write_1(PACKER_BUFFER_(pk), ib + (int)n);
84
84
  } else if (n < 256) {
85
85
  msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 3);
86
86
  msgpack_buffer_write_2(PACKER_BUFFER_(pk), ib + 24, n);
@@ -170,17 +170,25 @@ static inline void msgpack_packer_write_double(msgpack_packer_t* pk, double v)
170
170
  msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 5);
171
171
  castbuf.u32 = _msgpack_be_float(castbuf.u32);
172
172
  msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), IB_FLOAT4, castbuf.mem, 4);
173
- } else if (v != v) { /* NaN */
174
- cbor_encoder_write_head(pk, 0xe0, 0xfe00);
175
173
  } else {
176
- msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 9);
177
174
  union {
178
175
  double d;
179
176
  uint64_t u64;
180
177
  char mem[8];
181
178
  } castbuf = { v };
182
- castbuf.u64 = _msgpack_be_double(castbuf.u64);
183
- msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), IB_FLOAT8, castbuf.mem, 8);
179
+ if (v != v && (castbuf.u64 & 0x1fffffffUL) == 0) { /* NaN && can narrow */
180
+ uint64_t sign = castbuf.u64 >> 63;
181
+ uint64_t mant = castbuf.u64 & 0xfffffffffffff; /* 52 bits */
182
+ if ((mant & 0x3ffffffffffUL) == 0) { /* 42 zero bits: narrow to f16 */
183
+ cbor_encoder_write_head(pk, 0xe0, sign << 15 | 0x7c00 | mant >> 42);
184
+ } else { /* 29 zero bits (checked above): narrow to f32 */
185
+ cbor_encoder_write_head(pk, 0xe0, sign << 31 | 0x7f800000 | mant >> 29);
186
+ }
187
+ } else { /* can't narrow */
188
+ msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 9);
189
+ castbuf.u64 = _msgpack_be_double(castbuf.u64);
190
+ msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), IB_FLOAT8, castbuf.mem, 8);
191
+ }
184
192
  }
185
193
  }
186
194
 
@@ -216,6 +224,10 @@ static inline void msgpack_packer_write_string_value(msgpack_packer_t* pk, VALUE
216
224
 
217
225
  static inline void msgpack_packer_write_symbol_value(msgpack_packer_t* pk, VALUE v)
218
226
  {
227
+ #ifdef HAVE_RB_SYM2STR
228
+ /* rb_sym2str is added since MRI 2.2.0 */
229
+ msgpack_packer_write_string_value(pk, rb_sym2str(v));
230
+ #else
219
231
  const char* name = rb_id2name(SYM2ID(v));
220
232
  /* actual return type of strlen is size_t */
221
233
  unsigned long len = strlen(name);
@@ -225,6 +237,7 @@ static inline void msgpack_packer_write_symbol_value(msgpack_packer_t* pk, VALUE
225
237
  }
226
238
  cbor_encoder_write_head(pk, IB_TEXT, len);
227
239
  msgpack_buffer_append(PACKER_BUFFER_(pk), name, len);
240
+ #endif
228
241
  }
229
242
 
230
243
  static inline void msgpack_packer_write_fixnum_value(msgpack_packer_t* pk, VALUE v)
@@ -244,7 +257,26 @@ static inline void msgpack_packer_write_bignum_value(msgpack_packer_t* pk, VALUE
244
257
  v = rb_funcall(v, rb_intern("~"), 0); /* should be rb_big_neg(), but that is static. */
245
258
  ib = IB_NEGATIVE;
246
259
  }
247
- len = RBIGNUM_LEN(v); /* This API is broken in Rubinius 2.1.1, #2742 */
260
+
261
+
262
+ #ifdef HAVE_RB_INTEGER_UNPACK
263
+ len = rb_absint_size(v, NULL);
264
+
265
+ if (len > SIZEOF_LONG_LONG) { /* i.e., need real bignum */
266
+ msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
267
+ msgpack_buffer_write_1(PACKER_BUFFER_(pk), IB_BIGNUM + IB_NEGFLAG_AS_BIT(ib));
268
+ cbor_encoder_write_head(pk, IB_BYTES, len);
269
+ msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), len);
270
+
271
+ char buf[len]; /* XXX */
272
+ if (rb_integer_pack(v, buf, len, 1, 0, INTEGER_PACK_BIG_ENDIAN) != 1)
273
+ rb_raise(rb_eRangeError, "cbor rb_integer_pack() error");
274
+
275
+ msgpack_buffer_append(PACKER_BUFFER_(pk), buf, len);
276
+
277
+ #else
278
+
279
+ len = RBIGNUM_LEN(v);
248
280
  if (len > SIZEOF_LONG_LONG/SIZEOF_BDIGITS) {
249
281
  msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
250
282
  msgpack_buffer_write_1(PACKER_BUFFER_(pk), IB_BIGNUM + IB_NEGFLAG_AS_BIT(ib));
@@ -296,6 +328,7 @@ static inline void msgpack_packer_write_bignum_value(msgpack_packer_t* pk, VALUE
296
328
  }
297
329
  #endif
298
330
  }
331
+ #endif
299
332
  } else {
300
333
  cbor_encoder_write_head(pk, ib, rb_big2ull(v));
301
334
  }
@@ -323,7 +356,7 @@ void msgpack_packer_write_value(msgpack_packer_t* pk, VALUE v);
323
356
 
324
357
  static inline void msgpack_packer_write_tagged_value(msgpack_packer_t* pk, VALUE v)
325
358
  {
326
- cbor_encoder_write_head(pk, IB_TAG, FIX2LONG(rb_struct_aref(v, INT2FIX(0))));
359
+ cbor_encoder_write_head(pk, IB_TAG, rb_num2ulong(rb_struct_aref(v, INT2FIX(0))));
327
360
  msgpack_packer_write_value(pk, rb_struct_aref(v, INT2FIX(1)));
328
361
  }
329
362
 
@@ -52,7 +52,7 @@ static void Packer_free(msgpack_packer_t* pk)
52
52
  return;
53
53
  }
54
54
  msgpack_packer_destroy(pk);
55
- free(pk);
55
+ xfree(pk);
56
56
  }
57
57
 
58
58
  static VALUE Packer_alloc(VALUE klass)
data/ext/cbor/sysdep.h CHANGED
@@ -46,8 +46,8 @@
46
46
  # define _msgpack_be16(x) ((uint16_t)_byteswap_ushort((unsigned short)x))
47
47
  # else
48
48
  # define _msgpack_be16(x) ( \
49
- ((((uint16_t)x) << 8) ) | \
50
- ((((uint16_t)x) >> 8) ) )
49
+ ((((uint16_t)x) << 8) & 0x0000ff00U ) | \
50
+ ((((uint16_t)x) >> 8) & 0x000000ffU ) )
51
51
  # endif
52
52
  #else
53
53
  # define _msgpack_be16(x) ntohs(x)
@@ -121,6 +121,8 @@
121
121
  /* TODO default impl for _msgpack_bsp32 */
122
122
  #endif
123
123
 
124
+ #ifndef HAVE_RB_INTEGER_UNPACK
125
+
124
126
  #if SIZEOF_BDIGITS == 2
125
127
  #define NTOHBDIGIT _msgpack_be16
126
128
  #elif SIZEOF_BDIGITS == 4
@@ -133,3 +135,5 @@
133
135
 
134
136
  #endif
135
137
 
138
+ #endif
139
+
data/ext/cbor/unpacker.c CHANGED
@@ -29,6 +29,13 @@
29
29
  #include "rmem.h"
30
30
  #include <math.h> /* for ldexp */
31
31
 
32
+ /* work around https://bugs.ruby-lang.org/issues/15779 for now
33
+ * by limiting preallocation to about a Tebibyte
34
+ * limit is 2**n-1 (n==28) so we can avoid a conditional
35
+ */
36
+ #define SANE_PREALLOCATION_MAX 0xFFFFFFFUL
37
+ #define SANE_PREALLOCATE(n) (n & SANE_PREALLOCATION_MAX)
38
+
32
39
  #if !defined(DISABLE_RMEM) && !defined(DISABLE_UNPACKER_STACK_RMEM) && \
33
40
  MSGPACK_UNPACKER_STACK_CAPACITY * MSGPACK_UNPACKER_STACK_SIZE <= MSGPACK_RMEM_PAGE_SIZE
34
41
  #define UNPACKER_STACK_RMEM
@@ -245,7 +252,7 @@ static int read_raw_body_cont(msgpack_unpacker_t* uk, int textflag)
245
252
  size_t length = uk->reading_raw_remaining;
246
253
 
247
254
  if(uk->reading_raw == Qnil) {
248
- uk->reading_raw = rb_str_buf_new(length);
255
+ uk->reading_raw = rb_str_buf_new(SANE_PREALLOCATE(length));
249
256
  }
250
257
 
251
258
  do {
@@ -381,7 +388,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
381
388
  if (val == 0) {
382
389
  return object_complete(uk, rb_ary_new());
383
390
  }
384
- return _msgpack_unpacker_stack_push(uk, STACK_TYPE_ARRAY, val, rb_ary_new2(val));
391
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_ARRAY, val, rb_ary_new2(SANE_PREALLOCATE(val)));
385
392
  CASE_AI(MT_MAP):
386
393
  READ_VAL(uk, ai, val);
387
394
  CASE_IMM(MT_MAP): // map
@@ -410,11 +417,21 @@ static int read_primitive(msgpack_unpacker_t* uk)
410
417
  switch (ai) {
411
418
  case AI_2: { // half
412
419
  int exp = (val >> 10) & 0x1f;
413
- int mant = val & 0x3ff;
420
+ int mant = val & 0x3ff; /* 10 bits */
414
421
  double res;
415
422
  if (exp == 0) res = ldexp(mant, -24);
416
423
  else if (exp != 31) res = ldexp11(mant + 1024, exp - 25);
417
- else res = mant == 0 ? INFINITY : NAN;
424
+ else {
425
+ if (mant == 0)
426
+ res = INFINITY;
427
+ else { /* NAN */
428
+ union {
429
+ uint64_t u64;
430
+ double d;
431
+ } castbuf = { (val & 0x8000) << 48 | 0x7ff0000000000000UL | (uint64_t)mant << 42 };
432
+ return object_complete(uk, rb_float_new(castbuf.d));
433
+ }
434
+ }
418
435
  return object_complete(uk, rb_float_new(val & 0x8000 ? -res : res));
419
436
  }
420
437
  case AI_4: // float
@@ -422,8 +439,17 @@ static int read_primitive(msgpack_unpacker_t* uk)
422
439
  union {
423
440
  uint32_t u32;
424
441
  float f;
425
- } castbuf = { val };
426
- return object_complete(uk, rb_float_new(castbuf.f));
442
+ } castbuf = { (uint32_t)val }; /* sets Q to 1 for NAN */
443
+ if (castbuf.f == castbuf.f) {
444
+ return object_complete(uk, rb_float_new(castbuf.f));
445
+ } else { /* NAN */
446
+ uint64_t mant = val & 0x7fffff; /* 23 bits */
447
+ union {
448
+ uint64_t u64;
449
+ double d;
450
+ } castbuf1 = { (val & 0x80000000UL) << 32 | 0x7ff0000000000000UL | mant << 29 };
451
+ return object_complete(uk, rb_float_new(castbuf1.d));
452
+ }
427
453
  }
428
454
  case AI_8: // double
429
455
  {
@@ -476,6 +502,7 @@ int msgpack_unpacker_read_container_header(msgpack_unpacker_t* uk, uint64_t* res
476
502
  return PRIMITIVE_UNEXPECTED_TYPE; /* including INDEF! */
477
503
  }
478
504
 
505
+ reset_head_byte(uk);
479
506
  return 0;
480
507
  }
481
508
 
@@ -514,11 +541,19 @@ static VALUE msgpack_unpacker_process_tag(uint64_t tag, VALUE v) {
514
541
  #endif
515
542
  {
516
543
  char *sp = RSTRING_PTR(v);
517
- int slen = RSTRING_LEN(v);
544
+ size_t slen = RSTRING_LEN(v);
518
545
  while (slen && *sp == 0) {
519
546
  slen--;
520
547
  sp++;
521
548
  }
549
+
550
+ #ifdef HAVE_RB_INTEGER_UNPACK
551
+
552
+ res = rb_integer_unpack(sp, slen, 1, 0, INTEGER_PACK_BIG_ENDIAN);
553
+ /* (const void *words, size_t numwords, size_t wordsize, size_t nails, int flags); */
554
+
555
+ #else
556
+
522
557
  #ifndef CANT_DO_BIGNUMS_FAST_ON_THIS_PLATFORM
523
558
  int ndig = (slen + SIZEOF_BDIGITS - 1)/SIZEOF_BDIGITS;
524
559
  res = rb_big_new(ndig, 1);
@@ -554,6 +589,7 @@ static VALUE msgpack_unpacker_process_tag(uint64_t tag, VALUE v) {
554
589
  res = rb_cstr2inum(hex, 16);
555
590
  xfree(hex);
556
591
  }
592
+ #endif
557
593
  #endif
558
594
  if (tag == TAG_BIGNUM) /* non-negative */
559
595
  #ifndef CANT_DO_BIGNUMS_FAST_ON_THIS_PLATFORM
@@ -583,6 +619,8 @@ int msgpack_unpacker_read(msgpack_unpacker_t* uk, size_t target_stack_depth)
583
619
  /* PRIMITIVE_OBJECT_COMPLETE */
584
620
 
585
621
  if(msgpack_unpacker_stack_is_empty(uk)) {
622
+ if (r == PRIMITIVE_BREAK)
623
+ return PRIMITIVE_INVALID_BYTE;
586
624
  return PRIMITIVE_OBJECT_COMPLETE;
587
625
  }
588
626
 
@@ -624,9 +662,11 @@ int msgpack_unpacker_read(msgpack_unpacker_t* uk, size_t target_stack_depth)
624
662
  continue;
625
663
  case STACK_TYPE_STRING_INDEF:
626
664
  if (r == PRIMITIVE_BREAK) {
627
- object_complete_string(uk, top->object, top->count); /* use count as textflag */
665
+ object_complete_string(uk, top->object, (int)top->count); /* use count as textflag */
628
666
  goto done;
629
667
  }
668
+ if (!RB_TYPE_P(uk->last_object, T_STRING))
669
+ return PRIMITIVE_INVALID_BYTE;
630
670
  #ifdef COMPAT_HAVE_ENCODING /* XXX */
631
671
  if (ENCODING_GET(top->object) != ENCODING_GET(uk->last_object))
632
672
  return PRIMITIVE_INVALID_BYTE;
@@ -52,7 +52,7 @@ static void Unpacker_free(msgpack_unpacker_t* uk)
52
52
  return;
53
53
  }
54
54
  msgpack_unpacker_destroy(uk);
55
- free(uk);
55
+ xfree(uk);
56
56
  }
57
57
 
58
58
  static VALUE Unpacker_alloc(VALUE klass)
@@ -87,7 +87,7 @@ static VALUE Unpacker_initialize(int argc, VALUE* argv, VALUE self)
87
87
  io = argv[0];
88
88
  options = argv[1];
89
89
  if(rb_type(options) != T_HASH) {
90
- rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(io));
90
+ rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(options));
91
91
  }
92
92
 
93
93
  } else {
@@ -97,6 +97,11 @@ static VALUE Unpacker_initialize(int argc, VALUE* argv, VALUE self)
97
97
  UNPACKER(self, uk);
98
98
  if(io != Qnil || options != Qnil) {
99
99
  MessagePack_Buffer_initialize(UNPACKER_BUFFER_(uk), io, options);
100
+ if (options != Qnil) {
101
+ VALUE v;
102
+ v = rb_hash_aref(options, ID2SYM(rb_intern("symbolize_keys")));
103
+ uk->keys_as_symbols = RTEST(v);
104
+ }
100
105
  }
101
106
 
102
107
  // TODO options
@@ -256,9 +261,10 @@ static VALUE Unpacker_each_impl(VALUE self)
256
261
  }
257
262
  }
258
263
 
259
- static VALUE Unpacker_rescue_EOFError(VALUE self)
264
+ static VALUE Unpacker_rescue_EOFError(VALUE self, VALUE error)
260
265
  {
261
266
  UNUSED(self);
267
+ UNUSED(error);
262
268
  return Qnil;
263
269
  }
264
270
 
@@ -305,10 +311,16 @@ VALUE MessagePack_unpack(int argc, VALUE* argv)
305
311
  switch(argc) {
306
312
  case 2:
307
313
  options = argv[1]; /* Experimental! */
308
- if (options == ID2SYM(rb_intern("keys_as_symbols")))
314
+ if (options == ID2SYM(rb_intern("keys_as_symbols"))) /* backward compat */
309
315
  keys_as_symbols = true;
310
- else
311
- rb_raise(rb_eArgError, "unknown option");
316
+ else if (options != Qnil) {
317
+ VALUE v;
318
+ if (!RB_TYPE_P(options, T_HASH)) {
319
+ rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(options));
320
+ }
321
+ v = rb_hash_aref(options, ID2SYM(rb_intern("symbolize_keys")));
322
+ keys_as_symbols = RTEST(v);
323
+ }
312
324
  /* fall through */
313
325
  case 1:
314
326
  src = argv[0];
data/lib/cbor/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module CBOR
2
- VERSION = "0.5.6.2"
2
+ VERSION = "0.5.10.1"
3
3
  end
data/lib/cbor.rb CHANGED
@@ -1,9 +1,6 @@
1
- here = File.expand_path(File.dirname(__FILE__))
2
- require File.join(here, 'cbor', 'version')
1
+ require "cbor/version"
3
2
  begin
4
- m = /(\d+.\d+)/.match(RUBY_VERSION)
5
- ver = m[1]
6
- require File.join(here, 'cbor', ver, 'cbor')
3
+ require "cbor/#{RUBY_VERSION[/\d+.\d+/]}/cbor"
7
4
  rescue LoadError
8
- require File.join(here, 'cbor', 'cbor')
5
+ require "cbor/cbor"
9
6
  end
data/spec/format_spec.rb CHANGED
@@ -1,4 +1,4 @@
1
- # encoding: ascii-8bit
1
+ # encoding: utf-8
2
2
  require 'spec_helper'
3
3
 
4
4
  def bignum_to_bytes(bn)
@@ -150,85 +150,100 @@ describe MessagePack do
150
150
  end
151
151
 
152
152
  it "nil" do
153
- match nil, "\xf6"
153
+ match nil, "\xf6".b
154
154
  end
155
155
 
156
156
  it "false" do
157
- match false, "\xf4"
157
+ match false, "\xf4".b
158
158
  end
159
159
 
160
160
  it "true" do
161
- match true, "\xf5"
161
+ match true, "\xf5".b
162
162
  end
163
163
 
164
164
  it "0" do
165
- match 0, "\x00"
165
+ match 0, "\x00".b
166
166
  end
167
167
 
168
168
  it "127" do
169
- match 127, "\x18\x7f"
169
+ match 127, "\x18\x7f".b
170
170
  end
171
171
 
172
172
  it "128" do
173
- match 128, "\x18\x80"
173
+ match 128, "\x18\x80".b
174
174
  end
175
175
 
176
176
  it "256" do
177
- match 256, "\x19\x01\x00"
177
+ match 256, "\x19\x01\x00".b
178
178
  end
179
179
 
180
180
  it "-1" do
181
- match -1, "\x20"
181
+ match -1, "\x20".b
182
182
  end
183
183
 
184
184
  it "-33" do
185
- match -33, "\x38\x20"
185
+ match -33, "\x38\x20".b
186
186
  end
187
187
 
188
188
  it "-129" do
189
- match -129, "\x38\x80"
189
+ match -129, "\x38\x80".b
190
190
  end
191
191
 
192
192
  it "-257" do
193
- match -257, "\x39\x01\x00"
193
+ match -257, "\x39\x01\x00".b
194
194
  end
195
195
 
196
196
  it "{1=>1}" do
197
197
  obj = {1=>1}
198
- match obj, "\xA1\x01\x01"
198
+ match obj, "\xA1\x01\x01".b
199
199
  end
200
200
 
201
201
  it "1.0" do
202
- match 1.0, "\xF9\x3c\x00"
202
+ match 1.0, "\xF9\x3c\x00".b
203
+ end
204
+
205
+ it "NaN" do
206
+ match Float::NAN, "\xF9\x7e\x00".b
203
207
  end
204
208
 
205
209
  it "[]" do
206
- match [], "\x80"
210
+ match [], "\x80".b
207
211
  end
208
212
 
209
213
  it "[0, 1, ..., 14]" do
210
214
  obj = (0..14).to_a
211
- match obj, "\x8f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"
215
+ match obj, "\x8f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e".b
212
216
  end
213
217
 
214
218
  it "[0, 1, ..., 15]" do
215
219
  obj = (0..15).to_a
216
- match obj, "\x90\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
220
+ match obj, "\x90\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f".b
217
221
  end
218
222
 
219
223
  it "[0, 1, ..., 22]" do
220
224
  obj = (0..22).to_a
221
- match obj, "\x97\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16"
225
+ match obj, "\x97\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16".b
222
226
  end
223
227
 
224
228
  it "[0, 1, ..., 23]" do
225
229
  obj = (0..23).to_a
226
- match obj, "\x98\x18\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17"
230
+ match obj, "\x98\x18\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17".b
227
231
  end
228
232
 
229
233
  it "{}" do
230
234
  obj = {}
231
- match obj, "\xA0"
235
+ match obj, "\xA0".b
236
+ end
237
+
238
+ it "very simple bignums" do
239
+ CBOR.decode("\xc2\x40").should == 0
240
+ CBOR.decode("\xc2\x41\x00").should == 0
241
+ CBOR.decode("\xc2\x41a").should == 97
242
+ CBOR.decode("\xc2\x42aa").should == 24929
243
+ CBOR.decode("\xc3\x40").should == ~0
244
+ CBOR.decode("\xc3\x41\x00").should == ~0
245
+ CBOR.decode("\xc3\x41a").should == ~97
246
+ CBOR.decode("\xc3\x42aa").should == ~24929
232
247
  end
233
248
 
234
249
  it "0x982173487123985791827359872948752345" do
@@ -254,26 +269,26 @@ describe MessagePack do
254
269
  end
255
270
 
256
271
  it "fixnum/bignum switch" do
257
- CBOR.encode(CBOR.decode("\xc2\x40")).should == "\x00"
258
- CBOR.encode(CBOR.decode("\xc2\x41\x00")).should == "\x00"
259
- CBOR.encode(CBOR.decode("\xc2\x41a")).should == "\x18a"
260
- CBOR.encode(CBOR.decode("\xc2\x42aa")).should == "\x19aa"
261
- CBOR.encode(CBOR.decode("\xc2\x43aaa")).should == "\x1A\x00aaa"
262
- CBOR.encode(CBOR.decode("\xc2\x44aaaa")).should == "\x1Aaaaa"
263
- CBOR.encode(CBOR.decode("\xc2\x45aaaaa")).should == "\e\x00\x00\x00aaaaa"
264
- CBOR.encode(CBOR.decode("\xc2\x46aaaaaa")).should == "\e\x00\x00aaaaaa"
265
- CBOR.encode(CBOR.decode("\xc2\x47aaaaaaa")).should == "\e\x00aaaaaaa"
266
- CBOR.encode(CBOR.decode("\xc2\x48aaaaaaaa")).should == "\eaaaaaaaa"
267
- CBOR.encode(CBOR.decode("\xc2\x49\x00aaaaaaaa")).should == "\eaaaaaaaa"
268
- CBOR.encode(CBOR.decode("\xc2\x49aaaaaaaaa")).should == "\xC2Iaaaaaaaaa"
269
- CBOR.encode(CBOR.decode("\xc2\x4a\x00aaaaaaaaa")).should == "\xC2Iaaaaaaaaa"
270
- CBOR.encode(CBOR.decode("\xc2\x4aaaaaaaaaaa")).should == "\xC2Jaaaaaaaaaa"
271
- CBOR.encode(CBOR.decode("\xc2\x4b\x00aaaaaaaaaa")).should == "\xC2Jaaaaaaaaaa"
272
- CBOR.encode(CBOR.decode("\xc2\x4baaaaaaaaaaa")).should == "\xC2Kaaaaaaaaaaa"
273
- CBOR.encode(CBOR.decode("\xc2\x4c\x00aaaaaaaaaaa")).should == "\xC2Kaaaaaaaaaaa"
274
- CBOR.encode(CBOR.decode("\xc2\x4caaaaaaaaaaaa")).should == "\xC2Laaaaaaaaaaaa"
275
- CBOR.encode(CBOR.decode("\xc2\x4d\x00aaaaaaaaaaaa")).should == "\xC2Laaaaaaaaaaaa"
276
- CBOR.encode(CBOR.decode("\xc2\x4daaaaaaaaaaaaa")).should == "\xC2Maaaaaaaaaaaaa"
272
+ CBOR.encode(CBOR.decode("\xc2\x40")).should == "\x00".b
273
+ CBOR.encode(CBOR.decode("\xc2\x41\x00")).should == "\x00".b
274
+ CBOR.encode(CBOR.decode("\xc2\x41a")).should == "\x18a".b
275
+ CBOR.encode(CBOR.decode("\xc2\x42aa")).should == "\x19aa".b
276
+ CBOR.encode(CBOR.decode("\xc2\x43aaa")).should == "\x1A\x00aaa".b
277
+ CBOR.encode(CBOR.decode("\xc2\x44aaaa")).should == "\x1Aaaaa".b
278
+ CBOR.encode(CBOR.decode("\xc2\x45aaaaa")).should == "\e\x00\x00\x00aaaaa".b
279
+ CBOR.encode(CBOR.decode("\xc2\x46aaaaaa")).should == "\e\x00\x00aaaaaa".b
280
+ CBOR.encode(CBOR.decode("\xc2\x47aaaaaaa")).should == "\e\x00aaaaaaa".b
281
+ CBOR.encode(CBOR.decode("\xc2\x48aaaaaaaa")).should == "\eaaaaaaaa".b
282
+ CBOR.encode(CBOR.decode("\xc2\x49\x00aaaaaaaa")).should == "\eaaaaaaaa".b
283
+ CBOR.encode(CBOR.decode("\xc2\x49aaaaaaaaa")).should == "\xC2Iaaaaaaaaa".b
284
+ CBOR.encode(CBOR.decode("\xc2\x4a\x00aaaaaaaaa")).should == "\xC2Iaaaaaaaaa".b
285
+ CBOR.encode(CBOR.decode("\xc2\x4aaaaaaaaaaa")).should == "\xC2Jaaaaaaaaaa".b
286
+ CBOR.encode(CBOR.decode("\xc2\x4b\x00aaaaaaaaaa")).should == "\xC2Jaaaaaaaaaa".b
287
+ CBOR.encode(CBOR.decode("\xc2\x4baaaaaaaaaaa")).should == "\xC2Kaaaaaaaaaaa".b
288
+ CBOR.encode(CBOR.decode("\xc2\x4c\x00aaaaaaaaaaa")).should == "\xC2Kaaaaaaaaaaa".b
289
+ CBOR.encode(CBOR.decode("\xc2\x4caaaaaaaaaaaa")).should == "\xC2Laaaaaaaaaaaa".b
290
+ CBOR.encode(CBOR.decode("\xc2\x4d\x00aaaaaaaaaaaa")).should == "\xC2Laaaaaaaaaaaa".b
291
+ CBOR.encode(CBOR.decode("\xc2\x4daaaaaaaaaaaaa")).should == "\xC2Maaaaaaaaaaaaa".b
277
292
  end
278
293
 
279
294
  it "a-non-ascii" do
@@ -342,13 +357,40 @@ describe MessagePack do
342
357
  end
343
358
  end
344
359
 
360
+ it "(_ not-a-string)" do
361
+ lambda {
362
+ check_decode "\x5f\x00\xff", "".b
363
+ }.should raise_error(MessagePack::MalformedFormatError)
364
+ end
365
+
366
+ it "bare break" do
367
+ lambda {
368
+ check_decode "\xff", "".b
369
+ }.should raise_error(MessagePack::MalformedFormatError)
370
+ lambda {
371
+ check_decode "\x82\xff\x00\x00", "".b
372
+ }.should raise_error(MessagePack::MalformedFormatError)
373
+ lambda {
374
+ check_decode "\x82\x9f\xff\xff", "".b
375
+ }.should raise_error(MessagePack::MalformedFormatError)
376
+ end
377
+
378
+ it "Tagged" do
379
+ expect { check 10, CBOR::Tagged.new("foo", 2) }.to raise_error(TypeError)
380
+ check 2, CBOR::Tagged.new(10, 2)
381
+ check 4, CBOR::Tagged.new(0xBEEF, 2)
382
+ check 6, CBOR::Tagged.new(0xDEADBEEF, 2)
383
+ check 10, CBOR::Tagged.new(0xDEADBEEFDEADBEEF, 2)
384
+ expect { check 10, CBOR::Tagged.new(0x1DEADBEEFDEADBEEF, 2) }.to raise_error(RangeError)
385
+ end
386
+
345
387
  it "Time" do
346
388
  check_decode "\xc1\x19\x12\x67", Time.at(4711)
347
389
  check 6, Time.at(Time.now.to_i)
348
390
  end
349
391
 
350
392
  it "URI" do
351
- check_decode "\xd8\x20\x78\x13http://www.ietf.org", CBOR::Tagged.new(32, "http://www.ietf.org")
393
+ # check_decode "\xd8\x20\x78\x13http://www.ietf.org", CBOR::Tagged.new(32, "http://www.ietf.org")
352
394
  require 'uri'
353
395
  check_decode "\xd8\x20\x78\x13http://www.ietf.org", URI.parse("http://www.ietf.org")
354
396
  # This doesn't work yet if 'uri' is not required before 'cbor':
@@ -372,11 +414,64 @@ describe MessagePack do
372
414
  end
373
415
 
374
416
  it "Keys as Symbols" do # Experimental!
375
- CBOR.decode(CBOR.encode({a: 1}), :keys_as_symbols).should == {a: 1}
376
- CBOR.decode(CBOR.encode({a: 1})).should == {"a" => 1}
417
+ CBOR.decode(CBOR.encode({:a => 1}), :keys_as_symbols).should == {:a => 1}
418
+ CBOR.decode(CBOR.encode({:a => 1})).should == {"a" => 1}
377
419
  expect { CBOR.decode("\x00", :foobar) }.to raise_error(ArgumentError)
378
420
  end
379
421
 
422
+ it 'CBOR.decode symbolize_keys' do
423
+ symbolized_hash = {:a => 'b', :c => 'd'}
424
+ CBOR.decode(CBOR.encode(symbolized_hash), :symbolize_keys => true).should == symbolized_hash
425
+ end
426
+
427
+ it 'Unpacker#read symbolize_keys' do
428
+ unpacker = Unpacker.new(:symbolize_keys => true)
429
+ symbolized_hash = {:a => 'b', :c => 'd'}
430
+ unpacker.feed(CBOR.encode(symbolized_hash)).read.should == symbolized_hash
431
+ end
432
+
433
+ it 'handle outrageous sizes 1' do
434
+ expect { CBOR.decode("\xa1") }.to raise_error(EOFError)
435
+ expect { CBOR.decode("\xba\xff\xff\xff\xff") }.to raise_error(EOFError)
436
+ expect { CBOR.decode("\xbb\xff\xff\xff\xff\xff\xff\xff\xff") }.to raise_error(EOFError)
437
+ expect { CBOR.decode("\xbb\x01\x01\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
438
+ expect { CBOR.decode("\xbb\x00\x00\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
439
+ expect { CBOR.decode("\x81") }.to raise_error(EOFError)
440
+ expect { CBOR.decode("\x9a\xff\xff\xff\xff") }.to raise_error(EOFError)
441
+ end
442
+ it 'handle outrageous sizes 2' do
443
+ expect { CBOR.decode("\x9b\xff\xff\xff\xff\xff\xff\xff\xff") }.to raise_error(EOFError)
444
+ end
445
+ it 'handle outrageous sizes 3' do
446
+ expect { CBOR.decode("\x9b\x01\x01\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
447
+ end
448
+ it 'handle outrageous sizes 4' do
449
+ expect { CBOR.decode("\x9b\x00\x00\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
450
+ expect { CBOR.decode("\x61") }.to raise_error(EOFError)
451
+ expect { CBOR.decode("\x7a\xff\xff\xff\xff") }.to raise_error(EOFError)
452
+ end
453
+ it 'handle outrageous sizes 5' do
454
+ expect { CBOR.decode("\x7b\xff\xff\xff\xff\xff\xff\xff\xff") }.to raise_error(EOFError)
455
+ end
456
+ it 'handle outrageous sizes 6' do
457
+ expect { CBOR.decode("\x7b\x01\x01\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
458
+ end
459
+ it 'handle outrageous sizes 7' do
460
+ expect { CBOR.decode("\x7b\x00\x00\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
461
+ expect { CBOR.decode("\x41") }.to raise_error(EOFError)
462
+ expect { CBOR.decode("\x5a\xff\xff\xff\xff") }.to raise_error(EOFError)
463
+ end
464
+ it 'handle outrageous sizes 8' do
465
+ expect { CBOR.decode("\x5b\xff\xff\xff\xff\xff\xff\xff\xff") }.to raise_error(EOFError)
466
+ end
467
+ it 'handle outrageous sizes 9' do
468
+ expect { CBOR.decode("\x5b\x01\x01\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
469
+ end
470
+ it 'handle outrageous sizes 10' do
471
+ expect { CBOR.decode("\x5b\x00\x00\x01\x01\x01\x01\x01\x01") }.to raise_error(EOFError)
472
+ end
473
+
474
+
380
475
  ## FIXME
381
476
  # it "{0=>0, 1=>1, ..., 14=>14}" do
382
477
  # a = (0..14).to_a;
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,23 @@
1
+ require 'rubygems' # for 1.8
2
+
3
+ RSpec.configure do |config|
4
+ config.expect_with :rspec do |c|
5
+ c.syntax = [:should, :expect]
6
+ end
7
+ config.mock_with :rspec do |c|
8
+ c.syntax = [:should, :expect]
9
+ end
10
+ end
11
+
12
+
1
13
  class String
2
14
  if ''.respond_to? :encode
3
15
  def encode_as_utf8
4
16
  encode(Encoding::UTF_8)
5
17
  end
18
+ def force_as_utf8
19
+ force_encoding(Encoding::UTF_8)
20
+ end
6
21
  unless String.instance_methods.include?(:b)
7
22
  def b
8
23
  dup.force_encoding(Encoding::BINARY)
@@ -12,10 +27,18 @@ class String
12
27
  def encode_as_utf8
13
28
  self # MRI 1.8
14
29
  end
30
+ def force_as_utf8
31
+ self # MRI 1.8
32
+ end
15
33
  def b
16
34
  self
17
35
  end
18
36
  end
37
+ unless ''.respond_to? :clear
38
+ def clear
39
+ replace('')
40
+ end
41
+ end
19
42
  def hexbytes(sep = '')
20
43
  bytes.map{|x| "%02x" % x}.join(sep)
21
44
  end
@@ -24,11 +24,33 @@ describe Unpacker do
24
24
  }.should raise_error(MessagePack::TypeError)
25
25
  end
26
26
 
27
+ it 'read_array_header converts an array to value sequence' do
28
+ packer.write_array_header(2)
29
+ packer.write("e")
30
+ packer.write(1)
31
+ unpacker = Unpacker.new
32
+ unpacker.feed(packer.to_s)
33
+ unpacker.read_array_header.should == 2
34
+ unpacker.read.should == "e"
35
+ unpacker.read.should == 1
36
+ end
37
+
27
38
  it 'read_map_header succeeds' do
28
39
  unpacker.feed("\xa1")
29
40
  unpacker.read_map_header.should == 1
30
41
  end
31
42
 
43
+ it 'read_map_header converts an map to key-value sequence' do
44
+ packer.write_map_header(1)
45
+ packer.write("k")
46
+ packer.write("v")
47
+ unpacker = Unpacker.new
48
+ unpacker.feed(packer.to_s)
49
+ unpacker.read_map_header.should == 1
50
+ unpacker.read.should == "k"
51
+ unpacker.read.should == "v"
52
+ end
53
+
32
54
  it 'read_map_header fails' do
33
55
  unpacker.feed("\x81")
34
56
  lambda {
@@ -144,14 +166,14 @@ describe Unpacker do
144
166
  end
145
167
 
146
168
  it 'frozen short strings' do
147
- raw = sample_object.to_cbor.to_s.force_encoding('UTF-8')
169
+ raw = sample_object.to_cbor.to_s.force_as_utf8
148
170
  lambda {
149
171
  unpacker.feed_each(raw.freeze) { }
150
172
  }.should_not raise_error
151
173
  end
152
174
 
153
175
  it 'frozen long strings' do
154
- raw = (sample_object.to_cbor.to_s * 10240).force_encoding('UTF-8')
176
+ raw = (sample_object.to_cbor.to_s * 10240).force_as_utf8
155
177
  lambda {
156
178
  unpacker.feed_each(raw.freeze) { }
157
179
  }.should_not raise_error
metadata CHANGED
@@ -1,99 +1,98 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cbor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.6.2
4
+ version: 0.5.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carsten Bormann, standing on the tall shoulders of Sadayuki Furuhashi
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2013-10-27 00:00:00.000000000 Z
10
+ date: 2025-08-07 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: bundler
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
- - - ~>
16
+ - - ">="
18
17
  - !ruby/object:Gem::Version
19
- version: '1.0'
18
+ version: '0'
20
19
  type: :development
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
- - - ~>
23
+ - - ">="
25
24
  - !ruby/object:Gem::Version
26
- version: '1.0'
25
+ version: '0'
27
26
  - !ruby/object:Gem::Dependency
28
27
  name: rake
29
28
  requirement: !ruby/object:Gem::Requirement
30
29
  requirements:
31
- - - ~>
30
+ - - "~>"
32
31
  - !ruby/object:Gem::Version
33
32
  version: 0.9.2
34
33
  type: :development
35
34
  prerelease: false
36
35
  version_requirements: !ruby/object:Gem::Requirement
37
36
  requirements:
38
- - - ~>
37
+ - - "~>"
39
38
  - !ruby/object:Gem::Version
40
39
  version: 0.9.2
41
40
  - !ruby/object:Gem::Dependency
42
41
  name: rake-compiler
43
42
  requirement: !ruby/object:Gem::Requirement
44
43
  requirements:
45
- - - ~>
44
+ - - "~>"
46
45
  - !ruby/object:Gem::Version
47
46
  version: 0.8.3
48
47
  type: :development
49
48
  prerelease: false
50
49
  version_requirements: !ruby/object:Gem::Requirement
51
50
  requirements:
52
- - - ~>
51
+ - - "~>"
53
52
  - !ruby/object:Gem::Version
54
53
  version: 0.8.3
55
54
  - !ruby/object:Gem::Dependency
56
55
  name: rspec
57
56
  requirement: !ruby/object:Gem::Requirement
58
57
  requirements:
59
- - - ~>
58
+ - - "~>"
60
59
  - !ruby/object:Gem::Version
61
60
  version: '2.11'
62
61
  type: :development
63
62
  prerelease: false
64
63
  version_requirements: !ruby/object:Gem::Requirement
65
64
  requirements:
66
- - - ~>
65
+ - - "~>"
67
66
  - !ruby/object:Gem::Version
68
67
  version: '2.11'
69
68
  - !ruby/object:Gem::Dependency
70
69
  name: json
71
70
  requirement: !ruby/object:Gem::Requirement
72
71
  requirements:
73
- - - ~>
72
+ - - "~>"
74
73
  - !ruby/object:Gem::Version
75
74
  version: '1.7'
76
75
  type: :development
77
76
  prerelease: false
78
77
  version_requirements: !ruby/object:Gem::Requirement
79
78
  requirements:
80
- - - ~>
79
+ - - "~>"
81
80
  - !ruby/object:Gem::Version
82
81
  version: '1.7'
83
82
  - !ruby/object:Gem::Dependency
84
83
  name: yard
85
84
  requirement: !ruby/object:Gem::Requirement
86
85
  requirements:
87
- - - ~>
86
+ - - "~>"
88
87
  - !ruby/object:Gem::Version
89
- version: 0.8.2
88
+ version: 0.9.11
90
89
  type: :development
91
90
  prerelease: false
92
91
  version_requirements: !ruby/object:Gem::Requirement
93
92
  requirements:
94
- - - ~>
93
+ - - "~>"
95
94
  - !ruby/object:Gem::Version
96
- version: 0.8.2
95
+ version: 0.9.11
97
96
  description: CBOR is a library for the CBOR binary object representation format, based
98
97
  on Sadayuki Furuhashi's MessagePack library.
99
98
  email: cabo@tzi.org
@@ -102,8 +101,8 @@ extensions:
102
101
  - ext/cbor/extconf.rb
103
102
  extra_rdoc_files: []
104
103
  files:
105
- - .gitignore
106
- - .travis.yml
104
+ - ".gitignore"
105
+ - ".travis.yml"
107
106
  - ChangeLog
108
107
  - Gemfile
109
108
  - README.rdoc
@@ -156,28 +155,25 @@ files:
156
155
  - spec/random_compat.rb
157
156
  - spec/spec_helper.rb
158
157
  - spec/unpacker_spec.rb
159
- homepage:
158
+ homepage: http://cbor.io/
160
159
  licenses:
161
- - Apache 2.0
160
+ - Apache-2.0
162
161
  metadata: {}
163
- post_install_message:
164
162
  rdoc_options: []
165
163
  require_paths:
166
164
  - lib
167
165
  required_ruby_version: !ruby/object:Gem::Requirement
168
166
  requirements:
169
- - - '>='
167
+ - - ">="
170
168
  - !ruby/object:Gem::Version
171
169
  version: '0'
172
170
  required_rubygems_version: !ruby/object:Gem::Requirement
173
171
  requirements:
174
- - - '>='
172
+ - - ">="
175
173
  - !ruby/object:Gem::Version
176
174
  version: '0'
177
175
  requirements: []
178
- rubyforge_project:
179
- rubygems_version: 2.0.3
180
- signing_key:
176
+ rubygems_version: 3.6.2
181
177
  specification_version: 4
182
178
  summary: CBOR, Concise Binary Object Representation.
183
179
  test_files: