msgpack 0.4.7 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/.gitignore +17 -0
  2. data/ChangeLog +47 -0
  3. data/README.rdoc +102 -0
  4. data/Rakefile +88 -0
  5. data/doclib/msgpack.rb +55 -0
  6. data/doclib/msgpack/buffer.rb +193 -0
  7. data/doclib/msgpack/core_ext.rb +101 -0
  8. data/doclib/msgpack/error.rb +14 -0
  9. data/doclib/msgpack/packer.rb +131 -0
  10. data/doclib/msgpack/unpacker.rb +130 -0
  11. data/ext/msgpack/buffer.c +679 -0
  12. data/ext/msgpack/buffer.h +442 -0
  13. data/ext/msgpack/buffer_class.c +507 -0
  14. data/ext/msgpack/buffer_class.h +32 -0
  15. data/ext/msgpack/compat.h +112 -0
  16. data/ext/msgpack/core_ext.c +129 -0
  17. data/ext/{pack.h → msgpack/core_ext.h} +7 -7
  18. data/ext/msgpack/extconf.rb +17 -0
  19. data/ext/msgpack/packer.c +137 -0
  20. data/ext/msgpack/packer.h +319 -0
  21. data/ext/msgpack/packer_class.c +285 -0
  22. data/ext/{unpack.h → msgpack/packer_class.h} +11 -7
  23. data/ext/msgpack/rbinit.c +33 -0
  24. data/ext/msgpack/rmem.c +110 -0
  25. data/ext/msgpack/rmem.h +100 -0
  26. data/ext/msgpack/sysdep.h +115 -0
  27. data/ext/msgpack/sysdep_endian.h +50 -0
  28. data/ext/msgpack/sysdep_types.h +46 -0
  29. data/ext/msgpack/unpacker.c +669 -0
  30. data/ext/msgpack/unpacker.h +112 -0
  31. data/ext/msgpack/unpacker_class.c +376 -0
  32. data/{msgpack/pack_define.h → ext/msgpack/unpacker_class.h} +12 -8
  33. data/lib/msgpack.rb +10 -0
  34. data/{ext → lib/msgpack}/version.rb +1 -1
  35. data/msgpack.gemspec +25 -0
  36. data/spec/buffer_io_spec.rb +237 -0
  37. data/spec/buffer_spec.rb +572 -0
  38. data/{test → spec}/cases.json +0 -0
  39. data/{test/cases.mpac → spec/cases.msg} +0 -0
  40. data/{test/cases_compact.mpac → spec/cases_compact.msg} +0 -0
  41. data/spec/cases_spec.rb +39 -0
  42. data/spec/format_spec.rb +225 -0
  43. data/spec/packer_spec.rb +127 -0
  44. data/spec/random_compat.rb +24 -0
  45. data/spec/spec_helper.rb +21 -0
  46. data/spec/unpacker_spec.rb +128 -0
  47. metadata +171 -34
  48. data/ext/compat.h +0 -99
  49. data/ext/extconf.rb +0 -7
  50. data/ext/pack.c +0 -314
  51. data/ext/rbinit.c +0 -66
  52. data/ext/unpack.c +0 -1001
  53. data/msgpack/pack_template.h +0 -771
  54. data/msgpack/sysdep.h +0 -195
  55. data/msgpack/unpack_define.h +0 -93
  56. data/msgpack/unpack_template.h +0 -413
  57. data/test/test_cases.rb +0 -46
  58. data/test/test_encoding.rb +0 -68
  59. data/test/test_helper.rb +0 -10
  60. data/test/test_pack_unpack.rb +0 -308
@@ -0,0 +1,32 @@
1
+ /*
2
+ * MessagePack for Ruby
3
+ *
4
+ * Copyright (C) 2008-2012 FURUHASHI Sadayuki
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ #ifndef MSGPACK_RUBY_BUFFER_CLASS_H__
19
+ #define MSGPACK_RUBY_BUFFER_CLASS_H__
20
+
21
+ #include "buffer.h"
22
+
23
+ extern VALUE cMessagePack_Buffer;
24
+
25
+ void MessagePack_Buffer_module_init(VALUE mMessagePack);
26
+
27
+ VALUE MessagePack_Buffer_wrap(msgpack_buffer_t* b, VALUE owner);
28
+
29
+ void MessagePack_Buffer_initialize(msgpack_buffer_t* b, VALUE io, VALUE options);
30
+
31
+ #endif
32
+
@@ -0,0 +1,112 @@
1
+ /*
2
+ * MessagePack for Ruby
3
+ *
4
+ * Copyright (C) 2008-2012 FURUHASHI Sadayuki
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ #ifndef MSGPACK_RUBY_COMPAT_H__
19
+ #define MSGPACK_RUBY_COMPAT_H__
20
+
21
+ #include "ruby.h"
22
+
23
+ #if defined(HAVE_RUBY_ST_H)
24
+ # include "ruby/st.h" /* ruby hash on Ruby 1.9 */
25
+ #elif defined(HAVE_ST_H)
26
+ # include "st.h" /* ruby hash on Ruby 1.8 */
27
+ #endif
28
+
29
+
30
+ /*
31
+ * COMPAT_HAVE_ENCODING
32
+ */
33
+ #ifdef HAVE_RUBY_ENCODING_H
34
+ # include "ruby/encoding.h"
35
+ # define COMPAT_HAVE_ENCODING
36
+ #endif
37
+
38
+ #if defined(__MACRUBY__) /* MacRuby */
39
+ # undef COMPAT_HAVE_ENCODING
40
+ #endif
41
+
42
+
43
+ /*
44
+ * define STR_DUP_LIKELY_DOES_COPY
45
+ */
46
+ #if defined(RUBY_VM) /* MRI 1.9 */
47
+ /* if FL_ALL(str, FL_USER1|FL_USER3) == STR_ASSOC_P(str) returns true,
48
+ * rb_str_dup will copy the string */
49
+ # define STR_DUP_LIKELY_DOES_COPY(str) FL_ALL(str, FL_USER1|FL_USER3)
50
+
51
+ #elif defined(RUBINIUS) || defined(JRUBY) /* Rubinius and JRuby */
52
+ # define STR_DUP_LIKELY_DOES_COPY(str) (1)
53
+
54
+ #else /* MRI 1.8 */
55
+ # define STR_DUP_LIKELY_DOES_COPY(str) (!FL_TEST(str, ELTS_SHARED))
56
+ #endif
57
+
58
+
59
+ /*
60
+ * SIZET2NUM
61
+ */
62
+ #ifndef SIZET2NUM /* MRI 1.8 */
63
+ # define SIZET2NUM(v) ULL2NUM(v)
64
+ #endif
65
+
66
+
67
+ /*
68
+ * rb_errinfo()
69
+ */
70
+ #if defined(RUBY_VM) /* MRI 1.9 */
71
+ # define COMPAT_RERAISE rb_exc_raise(rb_errinfo())
72
+
73
+ #elif defined(JRUBY) /* JRuby */
74
+ # define COMPAT_RERAISE rb_exc_raise(rb_gv_get("$!"))
75
+
76
+ #else /* MRI 1.8 and Rubinius */
77
+ # define COMPAT_RERAISE rb_exc_raise(ruby_errinfo)
78
+ #endif
79
+
80
+
81
+ /*
82
+ * RBIGNUM_POSITIVE_P
83
+ */
84
+ #ifndef RBIGNUM_POSITIVE_P
85
+ # if defined(RUBINIUS) /* Rubinius <= v1.2.3 */
86
+ # define RBIGNUM_POSITIVE_P(b) (rb_funcall(b, rb_intern(">="), 1, INT2FIX(0)) == Qtrue)
87
+
88
+ # elif defined(JRUBY) /* JRuby */
89
+ # define RBIGNUM_POSITIVE_P(b) (rb_funcall(b, rb_intern(">="), 1, INT2FIX(0)) == Qtrue)
90
+ # define rb_big2ull(b) rb_num2ull(b)
91
+ /*#define rb_big2ll(b) rb_num2ll(b)*/
92
+
93
+ # else /* MRI 1.8 */
94
+ # define RBIGNUM_POSITIVE_P(b) (RBIGNUM(b)->sign)
95
+ # endif
96
+ #endif
97
+
98
+
99
+ /*
100
+ * RSTRING_PTR, RSTRING_LEN
101
+ */
102
+ #ifndef RSTRING_PTR /* MRI 1.8.5 */
103
+ # define RSTRING_PTR(s) (RSTRING(s)->ptr)
104
+ #endif
105
+
106
+ #ifndef RSTRING_LEN /* MRI 1.8.5 */
107
+ # define RSTRING_LEN(s) (RSTRING(s)->len)
108
+ #endif
109
+
110
+
111
+ #endif
112
+
@@ -0,0 +1,129 @@
1
+ /*
2
+ * MessagePack for Ruby
3
+ *
4
+ * Copyright (C) 2008-2012 FURUHASHI Sadayuki
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ #include "core_ext.h"
20
+ #include "packer.h"
21
+ #include "packer_class.h"
22
+
23
+ static inline VALUE delegete_to_pack(int argc, VALUE* argv, VALUE self)
24
+ {
25
+ if(argc == 0) {
26
+ return MessagePack_pack(1, &self);
27
+ } else if(argc == 1) {
28
+ /* write to io */
29
+ VALUE argv2[2];
30
+ argv2[0] = self;
31
+ argv2[1] = argv[0];
32
+ return MessagePack_pack(2, argv2);
33
+ } else {
34
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
35
+ }
36
+ }
37
+
38
+ #define ENSURE_PACKER(argc, argv, packer, pk) \
39
+ if(argc != 1 || rb_class_of(argv[0]) != cMessagePack_Packer) { \
40
+ return delegete_to_pack(argc, argv, self); \
41
+ } \
42
+ VALUE packer = argv[0]; \
43
+ msgpack_packer_t *pk; \
44
+ Data_Get_Struct(packer, msgpack_packer_t, pk);
45
+
46
+ static VALUE NilClass_to_msgpack(int argc, VALUE* argv, VALUE self)
47
+ {
48
+ ENSURE_PACKER(argc, argv, packer, pk);
49
+ msgpack_packer_write_nil(pk);
50
+ return packer;
51
+ }
52
+
53
+ static VALUE TrueClass_to_msgpack(int argc, VALUE* argv, VALUE self)
54
+ {
55
+ ENSURE_PACKER(argc, argv, packer, pk);
56
+ msgpack_packer_write_true(pk);
57
+ return packer;
58
+ }
59
+
60
+ static VALUE FalseClass_to_msgpack(int argc, VALUE* argv, VALUE self)
61
+ {
62
+ ENSURE_PACKER(argc, argv, packer, pk);
63
+ msgpack_packer_write_false(pk);
64
+ return packer;
65
+ }
66
+
67
+ static VALUE Fixnum_to_msgpack(int argc, VALUE* argv, VALUE self)
68
+ {
69
+ ENSURE_PACKER(argc, argv, packer, pk);
70
+ msgpack_packer_write_fixnum_value(pk, self);
71
+ return packer;
72
+ }
73
+
74
+ static VALUE Bignum_to_msgpack(int argc, VALUE* argv, VALUE self)
75
+ {
76
+ ENSURE_PACKER(argc, argv, packer, pk);
77
+ msgpack_packer_write_bignum_value(pk, self);
78
+ return packer;
79
+ }
80
+
81
+ static VALUE Float_to_msgpack(int argc, VALUE* argv, VALUE self)
82
+ {
83
+ ENSURE_PACKER(argc, argv, packer, pk);
84
+ msgpack_packer_write_float_value(pk, self);
85
+ return packer;
86
+ }
87
+
88
+ static VALUE String_to_msgpack(int argc, VALUE* argv, VALUE self)
89
+ {
90
+ ENSURE_PACKER(argc, argv, packer, pk);
91
+ msgpack_packer_write_string_value(pk, self);
92
+ return packer;
93
+ }
94
+
95
+ static VALUE Array_to_msgpack(int argc, VALUE* argv, VALUE self)
96
+ {
97
+ ENSURE_PACKER(argc, argv, packer, pk);
98
+ msgpack_packer_write_array_value(pk, self);
99
+ return packer;
100
+ }
101
+
102
+ static VALUE Hash_to_msgpack(int argc, VALUE* argv, VALUE self)
103
+ {
104
+ ENSURE_PACKER(argc, argv, packer, pk);
105
+ msgpack_packer_write_hash_value(pk, self);
106
+ return packer;
107
+ }
108
+
109
+ static VALUE Symbol_to_msgpack(int argc, VALUE* argv, VALUE self)
110
+ {
111
+ ENSURE_PACKER(argc, argv, packer, pk);
112
+ msgpack_packer_write_symbol_value(pk, self);
113
+ return packer;
114
+ }
115
+
116
+ void MessagePack_core_ext_module_init()
117
+ {
118
+ rb_define_method(rb_cNilClass, "to_msgpack", NilClass_to_msgpack, -1);
119
+ rb_define_method(rb_cTrueClass, "to_msgpack", TrueClass_to_msgpack, -1);
120
+ rb_define_method(rb_cFalseClass, "to_msgpack", FalseClass_to_msgpack, -1);
121
+ rb_define_method(rb_cFixnum, "to_msgpack", Fixnum_to_msgpack, -1);
122
+ rb_define_method(rb_cBignum, "to_msgpack", Bignum_to_msgpack, -1);
123
+ rb_define_method(rb_cFloat, "to_msgpack", Float_to_msgpack, -1);
124
+ rb_define_method(rb_cString, "to_msgpack", String_to_msgpack, -1);
125
+ rb_define_method(rb_cArray, "to_msgpack", Array_to_msgpack, -1);
126
+ rb_define_method(rb_cHash, "to_msgpack", Hash_to_msgpack, -1);
127
+ rb_define_method(rb_cSymbol, "to_msgpack", Symbol_to_msgpack, -1);
128
+ }
129
+
@@ -1,7 +1,7 @@
1
1
  /*
2
- * MessagePack for Ruby packing routine
2
+ * MessagePack for Ruby
3
3
  *
4
- * Copyright (C) 2008-2010 FURUHASHI Sadayuki
4
+ * Copyright (C) 2008-2012 FURUHASHI Sadayuki
5
5
  *
6
6
  * Licensed under the Apache License, Version 2.0 (the "License");
7
7
  * you may not use this file except in compliance with the License.
@@ -15,12 +15,12 @@
15
15
  * See the License for the specific language governing permissions and
16
16
  * limitations under the License.
17
17
  */
18
- #ifndef PACK_H__
19
- #define PACK_H__
18
+ #ifndef MSGPACK_RUBY_CORE_EXT_H__
19
+ #define MSGPACK_RUBY_CORE_EXT_H__
20
20
 
21
- #include "ruby.h"
21
+ #include "compat.h"
22
22
 
23
- void Init_msgpack_pack(VALUE mMessagePack);
23
+ void MessagePack_core_ext_module_init();
24
24
 
25
- #endif /* pack.h */
25
+ #endif
26
26
 
@@ -0,0 +1,17 @@
1
+ require 'mkmf'
2
+
3
+ have_header("ruby/st.h")
4
+ have_header("st.h")
5
+
6
+ $CFLAGS << %[ -I.. -Wall -O3 -g -std=c99]
7
+ #$CFLAGS << %[ -DDISABLE_RMEM]
8
+ #$CFLAGS << %[ -DDISABLE_RMEM_REUSE_INTERNAL_FRAGMENT]
9
+ #$CFLAGS << %[ -DDISABLE_BUFFER_READ_REFERENCE_OPTIMIZE]
10
+ #$CFLAGS << %[ -DDISABLE_BUFFER_READ_TO_S_OPTIMIZE]
11
+
12
+ if warnflags = CONFIG['warnflags']
13
+ warnflags.slice!(/ -Wdeclaration-after-statement/)
14
+ end
15
+
16
+ create_makefile('msgpack/msgpack')
17
+
@@ -0,0 +1,137 @@
1
+ /*
2
+ * MessagePack for Ruby
3
+ *
4
+ * Copyright (C) 2008-2012 FURUHASHI Sadayuki
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ #include "packer.h"
20
+
21
+ void msgpack_packer_init(msgpack_packer_t* pk)
22
+ {
23
+ memset(pk, 0, sizeof(msgpack_packer_t));
24
+
25
+ msgpack_buffer_init(PACKER_BUFFER_(pk));
26
+
27
+ pk->io = Qnil;
28
+ }
29
+
30
+ void msgpack_packer_destroy(msgpack_packer_t* pk)
31
+ {
32
+ msgpack_buffer_destroy(PACKER_BUFFER_(pk));
33
+ }
34
+
35
+ void msgpack_packer_mark(msgpack_packer_t* pk)
36
+ {
37
+ rb_gc_mark(pk->io);
38
+
39
+ /* See MessagePack_Buffer_wrap */
40
+ /* msgpack_buffer_mark(PACKER_BUFFER_(pk)); */
41
+ rb_gc_mark(pk->buffer_ref);
42
+ }
43
+
44
+ void msgpack_packer_reset(msgpack_packer_t* pk)
45
+ {
46
+ msgpack_buffer_clear(PACKER_BUFFER_(pk));
47
+
48
+ pk->io = Qnil;
49
+ pk->io_write_all_method = 0;
50
+ pk->buffer_ref = Qnil;
51
+ }
52
+
53
+
54
+ void msgpack_packer_write_array_value(msgpack_packer_t* pk, VALUE v)
55
+ {
56
+ size_t len = RARRAY_LEN(v);
57
+ if(len > 0xffffffffUL) {
58
+ // TODO rb_eArgError?
59
+ rb_raise(rb_eArgError, "size of array is too long to pack: %lu bytes should be <= %lu", len, 0xffffffffUL);
60
+ }
61
+ unsigned int len32 = (unsigned int)len;
62
+ msgpack_packer_write_array_header(pk, len32);
63
+
64
+ unsigned int i;
65
+ for(i=0; i < len32; ++i) {
66
+ VALUE e = rb_ary_entry(v, i);
67
+ msgpack_packer_write_value(pk, e);
68
+ }
69
+ }
70
+
71
+ static int write_hash_foreach(VALUE key, VALUE value, VALUE pk_value)
72
+ {
73
+ if (key == Qundef) {
74
+ return ST_CONTINUE;
75
+ }
76
+ msgpack_packer_t* pk = (msgpack_packer_t*) pk_value;
77
+ msgpack_packer_write_value(pk, key);
78
+ msgpack_packer_write_value(pk, value);
79
+ return ST_CONTINUE;
80
+ }
81
+
82
+ void msgpack_packer_write_hash_value(msgpack_packer_t* pk, VALUE v)
83
+ {
84
+ size_t len = RHASH_SIZE(v);
85
+ if(len > 0xffffffffUL) {
86
+ // TODO rb_eArgError?
87
+ rb_raise(rb_eArgError, "size of array is too long to pack: %lu bytes should be <= %lu", len, 0xffffffffUL);
88
+ }
89
+ unsigned int len32 = (unsigned int)len;
90
+ msgpack_packer_write_map_header(pk, len32);
91
+
92
+ rb_hash_foreach(v, write_hash_foreach, (VALUE) pk);
93
+ }
94
+
95
+ static void _msgpack_packer_write_other_value(msgpack_packer_t* pk, VALUE v)
96
+ {
97
+ rb_funcall(v, pk->to_msgpack_method, 1, pk->to_msgpack_arg);
98
+ }
99
+
100
+ void msgpack_packer_write_value(msgpack_packer_t* pk, VALUE v)
101
+ {
102
+ switch(rb_type(v)) {
103
+ case T_NIL:
104
+ msgpack_packer_write_nil(pk);
105
+ break;
106
+ case T_TRUE:
107
+ msgpack_packer_write_true(pk);
108
+ break;
109
+ case T_FALSE:
110
+ msgpack_packer_write_false(pk);
111
+ break;
112
+ case T_FIXNUM:
113
+ msgpack_packer_write_fixnum_value(pk, v);
114
+ break;
115
+ case T_SYMBOL:
116
+ msgpack_packer_write_symbol_value(pk, v);
117
+ break;
118
+ case T_STRING:
119
+ msgpack_packer_write_string_value(pk, v);
120
+ break;
121
+ case T_ARRAY:
122
+ msgpack_packer_write_array_value(pk, v);
123
+ break;
124
+ case T_HASH:
125
+ msgpack_packer_write_hash_value(pk, v);
126
+ break;
127
+ case T_BIGNUM:
128
+ msgpack_packer_write_bignum_value(pk, v);
129
+ break;
130
+ case T_FLOAT:
131
+ msgpack_packer_write_float_value(pk, v);
132
+ break;
133
+ default:
134
+ _msgpack_packer_write_other_value(pk, v);
135
+ }
136
+ }
137
+