msgpack 0.4.7-x86-mingw32 → 0.5.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) 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 -1
  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 +157 -39
  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/lib/1.8/msgpack.so +0 -0
  54. data/lib/1.9/msgpack.so +0 -0
  55. data/msgpack/pack_template.h +0 -771
  56. data/msgpack/sysdep.h +0 -195
  57. data/msgpack/unpack_define.h +0 -93
  58. data/msgpack/unpack_template.h +0 -413
  59. data/test/test_cases.rb +0 -46
  60. data/test/test_encoding.rb +0 -68
  61. data/test/test_helper.rb +0 -10
  62. data/test/test_pack_unpack.rb +0 -308
@@ -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_UNPACKER_H__
19
+ #define MSGPACK_RUBY_UNPACKER_H__
20
+
21
+ #include "buffer.h"
22
+
23
+ #ifndef MSGPACK_UNPACKER_STACK_CAPACITY
24
+ #define MSGPACK_UNPACKER_STACK_CAPACITY 128
25
+ #endif
26
+
27
+ struct msgpack_unpacker_t;
28
+ typedef struct msgpack_unpacker_t msgpack_unpacker_t;
29
+
30
+ enum stack_type_t {
31
+ STACK_TYPE_ARRAY,
32
+ STACK_TYPE_MAP_KEY,
33
+ STACK_TYPE_MAP_VALUE,
34
+ };
35
+
36
+ typedef struct {
37
+ size_t count;
38
+ enum stack_type_t type;
39
+ VALUE object;
40
+ VALUE key;
41
+ } msgpack_unpacker_stack_t;
42
+
43
+ struct msgpack_unpacker_t {
44
+ msgpack_buffer_t buffer;
45
+
46
+ unsigned int head_byte;
47
+
48
+ msgpack_unpacker_stack_t* stack;
49
+ size_t stack_depth;
50
+ size_t stack_capacity;
51
+
52
+ VALUE last_object;
53
+
54
+ VALUE reading_raw;
55
+ size_t reading_raw_remaining;
56
+
57
+ VALUE buffer_ref;
58
+ };
59
+
60
+ #define UNPACKER_BUFFER_(uk) (&(uk)->buffer)
61
+
62
+ enum msgpack_unpacker_object_type {
63
+ TYPE_NIL = 0,
64
+ TYPE_BOOLEAN,
65
+ TYPE_INTEGER,
66
+ TYPE_FLOAT,
67
+ TYPE_RAW,
68
+ TYPE_ARRAY,
69
+ TYPE_MAP,
70
+ };
71
+
72
+ void msgpack_unpacker_static_init();
73
+
74
+ void msgpack_unpacker_static_destroy();
75
+
76
+ void msgpack_unpacker_init(msgpack_unpacker_t* uk);
77
+
78
+ void msgpack_unpacker_destroy(msgpack_unpacker_t* uk);
79
+
80
+ void msgpack_unpacker_mark(msgpack_unpacker_t* uk);
81
+
82
+ void msgpack_unpacker_reset(msgpack_unpacker_t* uk);
83
+
84
+
85
+ /* error codes */
86
+ #define PRIMITIVE_CONTAINER_START 1
87
+ #define PRIMITIVE_OBJECT_COMPLETE 0
88
+ #define PRIMITIVE_EOF -1
89
+ #define PRIMITIVE_INVALID_BYTE -2
90
+ #define PRIMITIVE_STACK_TOO_DEEP -3
91
+ #define PRIMITIVE_UNEXPECTED_TYPE -4
92
+
93
+ int msgpack_unpacker_read(msgpack_unpacker_t* uk, size_t target_stack_depth);
94
+
95
+ int msgpack_unpacker_skip(msgpack_unpacker_t* uk, size_t target_stack_depth);
96
+
97
+ static inline VALUE msgpack_unpacker_get_last_object(msgpack_unpacker_t* uk)
98
+ {
99
+ return uk->last_object;
100
+ }
101
+
102
+
103
+ int msgpack_unpacker_peek_next_object_type(msgpack_unpacker_t* uk);
104
+
105
+ int msgpack_unpacker_skip_nil(msgpack_unpacker_t* uk);
106
+
107
+ int msgpack_unpacker_read_array_header(msgpack_unpacker_t* uk, uint32_t* result_size);
108
+
109
+ int msgpack_unpacker_read_map_header(msgpack_unpacker_t* uk, uint32_t* result_size);
110
+
111
+ #endif
112
+
@@ -0,0 +1,376 @@
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 "unpacker.h"
20
+ #include "unpacker_class.h"
21
+ #include "buffer_class.h"
22
+
23
+ VALUE cMessagePack_Unpacker;
24
+
25
+ static VALUE s_unpacker_value;
26
+ static msgpack_unpacker_t* s_unpacker;
27
+
28
+ static VALUE eUnpackError;
29
+ static VALUE eMalformedFormatError;
30
+ static VALUE eStackError;
31
+ static VALUE eTypeError;
32
+
33
+ #define UNPACKER(from, name) \
34
+ msgpack_unpacker_t *name = NULL; \
35
+ Data_Get_Struct(from, msgpack_unpacker_t, name); \
36
+ if(name == NULL) { \
37
+ rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
38
+ }
39
+
40
+ static void Unpacker_free(msgpack_unpacker_t* uk)
41
+ {
42
+ if(uk == NULL) {
43
+ return;
44
+ }
45
+ msgpack_unpacker_destroy(uk);
46
+ free(uk);
47
+ }
48
+
49
+ static VALUE Unpacker_alloc(VALUE klass)
50
+ {
51
+ msgpack_unpacker_t* uk = ALLOC_N(msgpack_unpacker_t, 1);
52
+ msgpack_unpacker_init(uk);
53
+
54
+ VALUE self = Data_Wrap_Struct(klass, msgpack_unpacker_mark, Unpacker_free, uk);
55
+
56
+ uk->buffer_ref = MessagePack_Buffer_wrap(UNPACKER_BUFFER_(uk), self);
57
+
58
+ return self;
59
+ }
60
+
61
+ static VALUE Unpacker_initialize(int argc, VALUE* argv, VALUE self)
62
+ {
63
+ VALUE io = Qnil;
64
+ VALUE options = Qnil;
65
+
66
+ if(argc == 0 || (argc == 1 && argv[0] == Qnil)) {
67
+ /* Qnil */
68
+
69
+ } else if(argc == 1) {
70
+ VALUE v = argv[0];
71
+ if(rb_type(v) == T_HASH) {
72
+ options = v;
73
+ } else {
74
+ io = v;
75
+ }
76
+
77
+ } else if(argc == 2) {
78
+ io = argv[0];
79
+ options = argv[1];
80
+ if(rb_type(options) != T_HASH) {
81
+ rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(io));
82
+ }
83
+
84
+ } else {
85
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
86
+ }
87
+
88
+ UNPACKER(self, uk);
89
+ if(io != Qnil || options != Qnil) {
90
+ MessagePack_Buffer_initialize(UNPACKER_BUFFER_(uk), io, options);
91
+ }
92
+
93
+ // TODO options
94
+
95
+ return self;
96
+ }
97
+
98
+ static void raise_unpacker_error(int r)
99
+ {
100
+ switch(r) {
101
+ case PRIMITIVE_EOF:
102
+ rb_raise(rb_eEOFError, "end of buffer reached");
103
+ case PRIMITIVE_INVALID_BYTE:
104
+ rb_raise(eMalformedFormatError, "invalid byte");
105
+ case PRIMITIVE_STACK_TOO_DEEP:
106
+ rb_raise(eStackError, "stack level too deep");
107
+ case PRIMITIVE_UNEXPECTED_TYPE:
108
+ rb_raise(eTypeError, "unexpected type");
109
+ default:
110
+ rb_raise(eUnpackError, "logically unknown error %d", r);
111
+ }
112
+ }
113
+
114
+ static VALUE Unpacker_buffer(VALUE self)
115
+ {
116
+ UNPACKER(self, uk);
117
+ return uk->buffer_ref;
118
+ }
119
+
120
+ static VALUE Unpacker_read(VALUE self)
121
+ {
122
+ UNPACKER(self, uk);
123
+
124
+ int r = msgpack_unpacker_read(uk, 0);
125
+ if(r < 0) {
126
+ raise_unpacker_error(r);
127
+ }
128
+
129
+ return msgpack_unpacker_get_last_object(uk);
130
+ }
131
+
132
+ static VALUE Unpacker_skip(VALUE self)
133
+ {
134
+ UNPACKER(self, uk);
135
+
136
+ int r = msgpack_unpacker_skip(uk, 0);
137
+ if(r < 0) {
138
+ raise_unpacker_error(r);
139
+ }
140
+
141
+ return Qnil;
142
+ }
143
+
144
+ static VALUE Unpacker_skip_nil(VALUE self)
145
+ {
146
+ UNPACKER(self, uk);
147
+
148
+ int r = msgpack_unpacker_skip_nil(uk);
149
+ if(r < 0) {
150
+ raise_unpacker_error(r);
151
+ }
152
+
153
+ if(r) {
154
+ return Qtrue;
155
+ }
156
+ return Qfalse;
157
+ }
158
+
159
+ static VALUE Unpacker_read_array_header(VALUE self)
160
+ {
161
+ UNPACKER(self, uk);
162
+
163
+ uint32_t size;
164
+ int r = msgpack_unpacker_read_array_header(uk, &size);
165
+ if(r < 0) {
166
+ raise_unpacker_error(r);
167
+ }
168
+
169
+ return ULONG2NUM(size);
170
+ }
171
+
172
+ static VALUE Unpacker_read_map_header(VALUE self)
173
+ {
174
+ UNPACKER(self, uk);
175
+
176
+ uint32_t size;
177
+ int r = msgpack_unpacker_read_map_header(uk, &size);
178
+ if(r < 0) {
179
+ raise_unpacker_error((int)r);
180
+ }
181
+
182
+ return ULONG2NUM(size);
183
+ }
184
+
185
+ static VALUE Unpacker_peek_next_type(VALUE self)
186
+ {
187
+ UNPACKER(self, uk);
188
+
189
+ int r = msgpack_unpacker_peek_next_object_type(uk);
190
+ if(r < 0) {
191
+ raise_unpacker_error(r);
192
+ }
193
+
194
+ switch((enum msgpack_unpacker_object_type) r) {
195
+ case TYPE_NIL:
196
+ return rb_intern("nil");
197
+ case TYPE_BOOLEAN:
198
+ return rb_intern("boolean");
199
+ case TYPE_INTEGER:
200
+ return rb_intern("integer");
201
+ case TYPE_FLOAT:
202
+ return rb_intern("float");
203
+ case TYPE_RAW:
204
+ return rb_intern("raw");
205
+ case TYPE_ARRAY:
206
+ return rb_intern("array");
207
+ case TYPE_MAP:
208
+ return rb_intern("map");
209
+ default:
210
+ rb_raise(eUnpackError, "logically unknown type %d", r);
211
+ }
212
+ }
213
+
214
+ static VALUE Unpacker_feed(VALUE self, VALUE data)
215
+ {
216
+ UNPACKER(self, uk);
217
+
218
+ StringValue(data);
219
+
220
+ msgpack_buffer_append_string(UNPACKER_BUFFER_(uk), data);
221
+
222
+ return self;
223
+ }
224
+
225
+ static VALUE Unpacker_each_impl(VALUE self)
226
+ {
227
+ UNPACKER(self, uk);
228
+
229
+ while(true) {
230
+ int r = msgpack_unpacker_read(uk, 0);
231
+ if(r < 0) {
232
+ if(r == PRIMITIVE_EOF) {
233
+ return Qnil;
234
+ }
235
+ raise_unpacker_error(r);
236
+ }
237
+ VALUE v = msgpack_unpacker_get_last_object(uk);
238
+ #ifdef JRUBY
239
+ /* TODO JRuby's rb_yield behaves differently from Ruby 1.9.3 or Rubinius. */
240
+ if(rb_type(v) == T_ARRAY) {
241
+ v = rb_ary_new3(1, v);
242
+ }
243
+ #endif
244
+ rb_yield(v);
245
+ }
246
+ }
247
+
248
+ static VALUE Unpacker_rescue_EOFError(VALUE self)
249
+ {
250
+ UNUSED(self);
251
+ return Qnil;
252
+ }
253
+
254
+ static VALUE Unpacker_each(VALUE self)
255
+ {
256
+ UNPACKER(self, uk);
257
+
258
+ #ifdef RETURN_ENUMERATOR
259
+ RETURN_ENUMERATOR(self, 0, 0);
260
+ #endif
261
+
262
+ if(msgpack_buffer_has_io(UNPACKER_BUFFER_(uk))) {
263
+ return Unpacker_each_impl(self);
264
+ } else {
265
+ /* rescue EOFError only if io is set */
266
+ return rb_rescue2(Unpacker_each_impl, self,
267
+ Unpacker_rescue_EOFError, self,
268
+ rb_eEOFError, NULL);
269
+ }
270
+ }
271
+
272
+ static VALUE Unpacker_feed_each(VALUE self, VALUE data)
273
+ {
274
+ // TODO optimize
275
+ Unpacker_feed(self, data);
276
+ return Unpacker_each(self);
277
+ }
278
+
279
+ VALUE MessagePack_unpack(int argc, VALUE* argv)
280
+ {
281
+ VALUE src;
282
+
283
+ switch(argc) {
284
+ case 1:
285
+ src = argv[0];
286
+ break;
287
+ default:
288
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
289
+ }
290
+
291
+ VALUE io = Qnil;
292
+ if(rb_type(src) != T_STRING) {
293
+ io = src;
294
+ src = Qnil;
295
+ }
296
+
297
+ // TODO create an instance if io is set for thread safety?
298
+ //VALUE self = Unpacker_alloc(cMessagePack_Unpacker);
299
+ //UNPACKER(self, uk);
300
+ msgpack_unpacker_reset(s_unpacker);
301
+ msgpack_buffer_reset_io(UNPACKER_BUFFER_(s_unpacker));
302
+
303
+ if(io != Qnil) {
304
+ MessagePack_Buffer_initialize(UNPACKER_BUFFER_(s_unpacker), io, Qnil);
305
+ }
306
+
307
+ if(src != Qnil) {
308
+ /* prefer reference than copying; see MessagePack_Unpacker_module_init */
309
+ msgpack_buffer_append_string(UNPACKER_BUFFER_(s_unpacker), src);
310
+ }
311
+
312
+ int r = msgpack_unpacker_read(s_unpacker, 0);
313
+ if(r < 0) {
314
+ raise_unpacker_error(r);
315
+ }
316
+
317
+ /* raise if extra bytes follow */
318
+ if(msgpack_buffer_top_readable_size(UNPACKER_BUFFER_(s_unpacker)) > 0) {
319
+ rb_raise(eMalformedFormatError, "extra bytes follow after a deserialized object");
320
+ }
321
+
322
+ return msgpack_unpacker_get_last_object(s_unpacker);
323
+ }
324
+
325
+ static VALUE MessagePack_load_module_method(int argc, VALUE* argv, VALUE mod)
326
+ {
327
+ UNUSED(mod);
328
+ return MessagePack_unpack(argc, argv);
329
+ }
330
+
331
+ static VALUE MessagePack_unpack_module_method(int argc, VALUE* argv, VALUE mod)
332
+ {
333
+ UNUSED(mod);
334
+ return MessagePack_unpack(argc, argv);
335
+ }
336
+
337
+ void MessagePack_Unpacker_module_init(VALUE mMessagePack)
338
+ {
339
+ msgpack_unpacker_static_init();
340
+
341
+ cMessagePack_Unpacker = rb_define_class_under(mMessagePack, "Unpacker", rb_cObject);
342
+
343
+ eUnpackError = rb_define_class_under(mMessagePack, "UnpackError", rb_eStandardError);
344
+
345
+ eMalformedFormatError = rb_define_class_under(mMessagePack, "MalformedFormatError", eUnpackError);
346
+
347
+ eStackError = rb_define_class_under(mMessagePack, "StackError", eUnpackError);
348
+
349
+ eTypeError = rb_define_class_under(mMessagePack, "TypeError", rb_eStandardError);
350
+
351
+ rb_define_alloc_func(cMessagePack_Unpacker, Unpacker_alloc);
352
+
353
+ rb_define_method(cMessagePack_Unpacker, "initialize", Unpacker_initialize, -1);
354
+ rb_define_method(cMessagePack_Unpacker, "buffer", Unpacker_buffer, 0);
355
+ rb_define_method(cMessagePack_Unpacker, "read", Unpacker_read, 0);
356
+ rb_define_alias(cMessagePack_Unpacker, "unpack", "read");
357
+ rb_define_method(cMessagePack_Unpacker, "skip", Unpacker_skip, 0);
358
+ rb_define_method(cMessagePack_Unpacker, "skip_nil", Unpacker_skip_nil, 0);
359
+ rb_define_method(cMessagePack_Unpacker, "read_array_header", Unpacker_read_array_header, 0);
360
+ rb_define_method(cMessagePack_Unpacker, "read_map_header", Unpacker_read_map_header, 0);
361
+ //rb_define_method(cMessagePack_Unpacker, "peek_next_type", Unpacker_peek_next_type, 0); // TODO
362
+ rb_define_method(cMessagePack_Unpacker, "feed", Unpacker_feed, 1);
363
+ rb_define_method(cMessagePack_Unpacker, "each", Unpacker_each, 0);
364
+ rb_define_method(cMessagePack_Unpacker, "feed_each", Unpacker_feed_each, 1);
365
+
366
+ s_unpacker_value = Unpacker_alloc(cMessagePack_Unpacker);
367
+ rb_gc_register_address(&s_unpacker_value);
368
+ Data_Get_Struct(s_unpacker_value, msgpack_unpacker_t, s_unpacker);
369
+ /* prefer reference than copying */
370
+ msgpack_buffer_set_write_reference_threshold(UNPACKER_BUFFER_(s_unpacker), 0);
371
+
372
+ /* MessagePack.unpack(x) */
373
+ rb_define_module_function(mMessagePack, "load", MessagePack_load_module_method, -1);
374
+ rb_define_module_function(mMessagePack, "unpack", MessagePack_unpack_module_method, -1);
375
+ }
376
+