msgpack 0.6.0-x86-mswin32-60
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.travis.yml +26 -0
- data/ChangeLog +129 -0
- data/Dockerfile +62 -0
- data/LICENSE +177 -0
- data/README.rdoc +141 -0
- data/Rakefile +68 -0
- data/bench/pack.rb +23 -0
- data/bench/pack_log.rb +33 -0
- data/bench/pack_log_long.rb +65 -0
- data/bench/run.sh +14 -0
- data/bench/run_long.sh +35 -0
- data/bench/unpack.rb +21 -0
- data/bench/unpack_log.rb +34 -0
- data/bench/unpack_log_long.rb +67 -0
- data/doclib/msgpack.rb +77 -0
- data/doclib/msgpack/buffer.rb +193 -0
- data/doclib/msgpack/core_ext.rb +101 -0
- data/doclib/msgpack/error.rb +14 -0
- data/doclib/msgpack/packer.rb +134 -0
- data/doclib/msgpack/unpacker.rb +146 -0
- data/ext/java/org/msgpack/jruby/Buffer.java +221 -0
- data/ext/java/org/msgpack/jruby/Decoder.java +201 -0
- data/ext/java/org/msgpack/jruby/Encoder.java +308 -0
- data/ext/java/org/msgpack/jruby/ExtensionValue.java +136 -0
- data/ext/java/org/msgpack/jruby/MessagePackLibrary.java +107 -0
- data/ext/java/org/msgpack/jruby/Packer.java +78 -0
- data/ext/java/org/msgpack/jruby/Types.java +37 -0
- data/ext/java/org/msgpack/jruby/Unpacker.java +170 -0
- data/ext/msgpack/buffer.c +695 -0
- data/ext/msgpack/buffer.h +447 -0
- data/ext/msgpack/buffer_class.c +507 -0
- data/ext/msgpack/buffer_class.h +32 -0
- data/ext/msgpack/compat.h +114 -0
- data/ext/msgpack/core_ext.c +129 -0
- data/ext/msgpack/core_ext.h +26 -0
- data/ext/msgpack/extconf.rb +30 -0
- data/ext/msgpack/packer.c +168 -0
- data/ext/msgpack/packer.h +441 -0
- data/ext/msgpack/packer_class.c +302 -0
- data/ext/msgpack/packer_class.h +30 -0
- data/ext/msgpack/rbinit.c +33 -0
- data/ext/msgpack/rmem.c +94 -0
- data/ext/msgpack/rmem.h +109 -0
- data/ext/msgpack/sysdep.h +115 -0
- data/ext/msgpack/sysdep_endian.h +50 -0
- data/ext/msgpack/sysdep_types.h +46 -0
- data/ext/msgpack/unpacker.c +771 -0
- data/ext/msgpack/unpacker.h +122 -0
- data/ext/msgpack/unpacker_class.c +405 -0
- data/ext/msgpack/unpacker_class.h +32 -0
- data/lib/msgpack.rb +13 -0
- data/lib/msgpack/version.rb +3 -0
- data/msgpack.gemspec +31 -0
- data/msgpack.org.md +46 -0
- data/spec/cases.json +1 -0
- data/spec/cases.msg +0 -0
- data/spec/cases_compact.msg +0 -0
- data/spec/cases_spec.rb +39 -0
- data/spec/cruby/buffer_io_spec.rb +256 -0
- data/spec/cruby/buffer_packer.rb +29 -0
- data/spec/cruby/buffer_spec.rb +572 -0
- data/spec/cruby/buffer_unpacker.rb +19 -0
- data/spec/cruby/packer_spec.rb +120 -0
- data/spec/cruby/unpacker_spec.rb +305 -0
- data/spec/format_spec.rb +282 -0
- data/spec/jruby/benchmarks/shootout_bm.rb +73 -0
- data/spec/jruby/benchmarks/symbolize_keys_bm.rb +25 -0
- data/spec/jruby/msgpack/unpacker_spec.rb +290 -0
- data/spec/jruby/msgpack_spec.rb +142 -0
- data/spec/pack_spec.rb +67 -0
- data/spec/random_compat.rb +24 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/unpack_spec.rb +60 -0
- metadata +208 -0
@@ -0,0 +1,441 @@
|
|
1
|
+
/*
|
2
|
+
* MessagePack for Ruby
|
3
|
+
*
|
4
|
+
* Copyright (C) 2008-2013 Sadayuki Furuhashi
|
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_PACKER_H__
|
19
|
+
#define MSGPACK_RUBY_PACKER_H__
|
20
|
+
|
21
|
+
#include "buffer.h"
|
22
|
+
|
23
|
+
#ifndef MSGPACK_PACKER_IO_FLUSH_THRESHOLD_TO_WRITE_STRING_BODY
|
24
|
+
#define MSGPACK_PACKER_IO_FLUSH_THRESHOLD_TO_WRITE_STRING_BODY (1024)
|
25
|
+
#endif
|
26
|
+
|
27
|
+
struct msgpack_packer_t;
|
28
|
+
typedef struct msgpack_packer_t msgpack_packer_t;
|
29
|
+
|
30
|
+
struct msgpack_packer_t {
|
31
|
+
msgpack_buffer_t buffer;
|
32
|
+
|
33
|
+
VALUE io;
|
34
|
+
ID io_write_all_method;
|
35
|
+
|
36
|
+
ID to_msgpack_method;
|
37
|
+
VALUE to_msgpack_arg;
|
38
|
+
|
39
|
+
VALUE buffer_ref;
|
40
|
+
};
|
41
|
+
|
42
|
+
#define PACKER_BUFFER_(pk) (&(pk)->buffer)
|
43
|
+
|
44
|
+
void msgpack_packer_static_init();
|
45
|
+
|
46
|
+
void msgpack_packer_static_destroy();
|
47
|
+
|
48
|
+
void msgpack_packer_init(msgpack_packer_t* pk);
|
49
|
+
|
50
|
+
void msgpack_packer_destroy(msgpack_packer_t* pk);
|
51
|
+
|
52
|
+
void msgpack_packer_mark(msgpack_packer_t* pk);
|
53
|
+
|
54
|
+
static inline void msgpack_packer_set_to_msgpack_method(msgpack_packer_t* pk,
|
55
|
+
ID to_msgpack_method, VALUE to_msgpack_arg)
|
56
|
+
{
|
57
|
+
pk->to_msgpack_method = to_msgpack_method;
|
58
|
+
pk->to_msgpack_arg = to_msgpack_arg;
|
59
|
+
}
|
60
|
+
|
61
|
+
static inline void msgpack_packer_set_io(msgpack_packer_t* pk, VALUE io, ID io_write_all_method)
|
62
|
+
{
|
63
|
+
pk->io = io;
|
64
|
+
pk->io_write_all_method = io_write_all_method;
|
65
|
+
}
|
66
|
+
|
67
|
+
void msgpack_packer_reset(msgpack_packer_t* pk);
|
68
|
+
|
69
|
+
static inline void msgpack_packer_write_nil(msgpack_packer_t* pk)
|
70
|
+
{
|
71
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
|
72
|
+
msgpack_buffer_write_1(PACKER_BUFFER_(pk), 0xc0);
|
73
|
+
}
|
74
|
+
|
75
|
+
static inline void msgpack_packer_write_true(msgpack_packer_t* pk)
|
76
|
+
{
|
77
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
|
78
|
+
msgpack_buffer_write_1(PACKER_BUFFER_(pk), 0xc3);
|
79
|
+
}
|
80
|
+
|
81
|
+
static inline void msgpack_packer_write_false(msgpack_packer_t* pk)
|
82
|
+
{
|
83
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
|
84
|
+
msgpack_buffer_write_1(PACKER_BUFFER_(pk), 0xc2);
|
85
|
+
}
|
86
|
+
|
87
|
+
static inline void _msgpack_packer_write_fixint(msgpack_packer_t* pk, int8_t v)
|
88
|
+
{
|
89
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
|
90
|
+
msgpack_buffer_write_1(PACKER_BUFFER_(pk), v);
|
91
|
+
}
|
92
|
+
|
93
|
+
static inline void _msgpack_packer_write_uint8(msgpack_packer_t* pk, uint8_t v)
|
94
|
+
{
|
95
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 2);
|
96
|
+
msgpack_buffer_write_2(PACKER_BUFFER_(pk), 0xcc, v);
|
97
|
+
}
|
98
|
+
|
99
|
+
static inline void _msgpack_packer_write_uint16(msgpack_packer_t* pk, uint16_t v)
|
100
|
+
{
|
101
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 3);
|
102
|
+
uint16_t be = _msgpack_be16(v);
|
103
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xcd, (const void*)&be, 2);
|
104
|
+
}
|
105
|
+
|
106
|
+
static inline void _msgpack_packer_write_uint32(msgpack_packer_t* pk, uint32_t v)
|
107
|
+
{
|
108
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 5);
|
109
|
+
uint32_t be = _msgpack_be32(v);
|
110
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xce, (const void*)&be, 4);
|
111
|
+
}
|
112
|
+
|
113
|
+
static inline void _msgpack_packer_write_uint64(msgpack_packer_t* pk, uint64_t v)
|
114
|
+
{
|
115
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 9);
|
116
|
+
uint64_t be = _msgpack_be64(v);
|
117
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xcf, (const void*)&be, 8);
|
118
|
+
}
|
119
|
+
|
120
|
+
static inline void _msgpack_packer_write_int8(msgpack_packer_t* pk, int8_t v)
|
121
|
+
{
|
122
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 2);
|
123
|
+
msgpack_buffer_write_2(PACKER_BUFFER_(pk), 0xd0, v);
|
124
|
+
}
|
125
|
+
|
126
|
+
static inline void _msgpack_packer_write_int16(msgpack_packer_t* pk, int16_t v)
|
127
|
+
{
|
128
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 3);
|
129
|
+
uint16_t be = _msgpack_be16(v);
|
130
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xd1, (const void*)&be, 2);
|
131
|
+
}
|
132
|
+
|
133
|
+
static inline void _msgpack_packer_write_int32(msgpack_packer_t* pk, int32_t v)
|
134
|
+
{
|
135
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 5);
|
136
|
+
uint32_t be = _msgpack_be32(v);
|
137
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xd2, (const void*)&be, 4);
|
138
|
+
}
|
139
|
+
|
140
|
+
static inline void _msgpack_packer_write_int64(msgpack_packer_t* pk, int64_t v)
|
141
|
+
{
|
142
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 9);
|
143
|
+
uint64_t be = _msgpack_be64(v);
|
144
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xd3, (const void*)&be, 8);
|
145
|
+
}
|
146
|
+
|
147
|
+
static inline void _msgpack_packer_write_long32(msgpack_packer_t* pk, long v)
|
148
|
+
{
|
149
|
+
if(v < -0x20L) {
|
150
|
+
if(v < -0x8000L) {
|
151
|
+
_msgpack_packer_write_int32(pk, (int32_t) v);
|
152
|
+
} else if(v < -0x80L) {
|
153
|
+
_msgpack_packer_write_int16(pk, (int16_t) v);
|
154
|
+
} else {
|
155
|
+
_msgpack_packer_write_int8(pk, (int8_t) v);
|
156
|
+
}
|
157
|
+
} else if(v <= 0x7fL) {
|
158
|
+
_msgpack_packer_write_fixint(pk, (int8_t) v);
|
159
|
+
} else {
|
160
|
+
if(v <= 0xffL) {
|
161
|
+
_msgpack_packer_write_uint8(pk, (uint8_t) v);
|
162
|
+
} else if(v <= 0xffffL) {
|
163
|
+
_msgpack_packer_write_uint16(pk, (uint16_t) v);
|
164
|
+
} else {
|
165
|
+
_msgpack_packer_write_uint32(pk, (uint32_t) v);
|
166
|
+
}
|
167
|
+
}
|
168
|
+
}
|
169
|
+
|
170
|
+
static inline void _msgpack_packer_write_long_long64(msgpack_packer_t* pk, long long v)
|
171
|
+
{
|
172
|
+
if(v < -0x20LL) {
|
173
|
+
if(v < -0x8000LL) {
|
174
|
+
if(v < -0x80000000LL) {
|
175
|
+
_msgpack_packer_write_int64(pk, (int64_t) v);
|
176
|
+
} else {
|
177
|
+
_msgpack_packer_write_int32(pk, (int32_t) v);
|
178
|
+
}
|
179
|
+
} else {
|
180
|
+
if(v < -0x80LL) {
|
181
|
+
_msgpack_packer_write_int16(pk, (int16_t) v);
|
182
|
+
} else {
|
183
|
+
_msgpack_packer_write_int8(pk, (int8_t) v);
|
184
|
+
}
|
185
|
+
}
|
186
|
+
} else if(v <= 0x7fLL) {
|
187
|
+
_msgpack_packer_write_fixint(pk, (int8_t) v);
|
188
|
+
} else {
|
189
|
+
if(v <= 0xffffLL) {
|
190
|
+
if(v <= 0xffLL) {
|
191
|
+
_msgpack_packer_write_uint8(pk, (uint8_t) v);
|
192
|
+
} else {
|
193
|
+
_msgpack_packer_write_uint16(pk, (uint16_t) v);
|
194
|
+
}
|
195
|
+
} else {
|
196
|
+
if(v <= 0xffffffffLL) {
|
197
|
+
_msgpack_packer_write_uint32(pk, (uint32_t) v);
|
198
|
+
} else {
|
199
|
+
_msgpack_packer_write_uint64(pk, (uint64_t) v);
|
200
|
+
}
|
201
|
+
}
|
202
|
+
}
|
203
|
+
}
|
204
|
+
|
205
|
+
static inline void msgpack_packer_write_long(msgpack_packer_t* pk, long v)
|
206
|
+
{
|
207
|
+
#if defined(SIZEOF_LONG)
|
208
|
+
# if SIZEOF_LONG <= 4
|
209
|
+
_msgpack_packer_write_long32(pk, v);
|
210
|
+
# else
|
211
|
+
_msgpack_packer_write_long_long64(pk, v);
|
212
|
+
# endif
|
213
|
+
|
214
|
+
#elif defined(LONG_MAX)
|
215
|
+
# if LONG_MAX <= 0x7fffffffL
|
216
|
+
_msgpack_packer_write_long32(pk, v);
|
217
|
+
# else
|
218
|
+
_msgpack_packer_write_long_long64(pk, v);
|
219
|
+
# endif
|
220
|
+
|
221
|
+
#else
|
222
|
+
if(sizeof(long) <= 4) {
|
223
|
+
_msgpack_packer_write_long32(pk, v);
|
224
|
+
} else {
|
225
|
+
_msgpack_packer_write_long_long64(pk, v);
|
226
|
+
}
|
227
|
+
#endif
|
228
|
+
}
|
229
|
+
|
230
|
+
static inline void msgpack_packer_write_long_long(msgpack_packer_t* pk, long long v)
|
231
|
+
{
|
232
|
+
/* assuming sizeof(long long) == 8 */
|
233
|
+
_msgpack_packer_write_long_long64(pk, v);
|
234
|
+
}
|
235
|
+
|
236
|
+
static inline void msgpack_packer_write_u64(msgpack_packer_t* pk, uint64_t v)
|
237
|
+
{
|
238
|
+
if(v <= 0xffULL) {
|
239
|
+
if(v <= 0x7fULL) {
|
240
|
+
_msgpack_packer_write_fixint(pk, (int8_t) v);
|
241
|
+
} else {
|
242
|
+
_msgpack_packer_write_uint8(pk, (uint8_t) v);
|
243
|
+
}
|
244
|
+
} else {
|
245
|
+
if(v <= 0xffffULL) {
|
246
|
+
_msgpack_packer_write_uint16(pk, (uint16_t) v);
|
247
|
+
} else if(v <= 0xffffffffULL) {
|
248
|
+
_msgpack_packer_write_uint32(pk, (uint32_t) v);
|
249
|
+
} else {
|
250
|
+
_msgpack_packer_write_uint64(pk, (uint64_t) v);
|
251
|
+
}
|
252
|
+
}
|
253
|
+
}
|
254
|
+
|
255
|
+
static inline void msgpack_packer_write_double(msgpack_packer_t* pk, double v)
|
256
|
+
{
|
257
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 9);
|
258
|
+
union {
|
259
|
+
double d;
|
260
|
+
uint64_t u64;
|
261
|
+
char mem[8];
|
262
|
+
} castbuf = { v };
|
263
|
+
castbuf.u64 = _msgpack_be_double(castbuf.u64);
|
264
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xcb, castbuf.mem, 8);
|
265
|
+
}
|
266
|
+
|
267
|
+
static inline void msgpack_packer_write_raw_header(msgpack_packer_t* pk, unsigned int n)
|
268
|
+
{
|
269
|
+
if(n < 32) {
|
270
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
|
271
|
+
unsigned char h = 0xa0 | (uint8_t) n;
|
272
|
+
msgpack_buffer_write_1(PACKER_BUFFER_(pk), h);
|
273
|
+
} else if(n < 256) {
|
274
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 2);
|
275
|
+
unsigned char be = (uint8_t) n;
|
276
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xd9, (const void*)&be, 1);
|
277
|
+
} else if(n < 65536) {
|
278
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 3);
|
279
|
+
uint16_t be = _msgpack_be16(n);
|
280
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xda, (const void*)&be, 2);
|
281
|
+
} else {
|
282
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 5);
|
283
|
+
uint32_t be = _msgpack_be32(n);
|
284
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xdb, (const void*)&be, 4);
|
285
|
+
}
|
286
|
+
}
|
287
|
+
|
288
|
+
static inline void msgpack_packer_write_bin_header(msgpack_packer_t* pk, unsigned int n)
|
289
|
+
{
|
290
|
+
if(n < 256) {
|
291
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
|
292
|
+
unsigned char be = (uint8_t) n;
|
293
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xc4, (const void*)&be, 1);
|
294
|
+
} else if(n < 65536) {
|
295
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 3);
|
296
|
+
uint16_t be = _msgpack_be16(n);
|
297
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xc5, (const void*)&be, 2);
|
298
|
+
} else {
|
299
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 5);
|
300
|
+
uint32_t be = _msgpack_be32(n);
|
301
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xc6, (const void*)&be, 4);
|
302
|
+
}
|
303
|
+
}
|
304
|
+
|
305
|
+
static inline void msgpack_packer_write_array_header(msgpack_packer_t* pk, unsigned int n)
|
306
|
+
{
|
307
|
+
if(n < 16) {
|
308
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
|
309
|
+
unsigned char h = 0x90 | (uint8_t) n;
|
310
|
+
msgpack_buffer_write_1(PACKER_BUFFER_(pk), h);
|
311
|
+
} else if(n < 65536) {
|
312
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 3);
|
313
|
+
uint16_t be = _msgpack_be16(n);
|
314
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xdc, (const void*)&be, 2);
|
315
|
+
} else {
|
316
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 5);
|
317
|
+
uint32_t be = _msgpack_be32(n);
|
318
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xdd, (const void*)&be, 4);
|
319
|
+
}
|
320
|
+
}
|
321
|
+
|
322
|
+
static inline void msgpack_packer_write_map_header(msgpack_packer_t* pk, unsigned int n)
|
323
|
+
{
|
324
|
+
if(n < 16) {
|
325
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
|
326
|
+
unsigned char h = 0x80 | (uint8_t) n;
|
327
|
+
msgpack_buffer_write_1(PACKER_BUFFER_(pk), h);
|
328
|
+
} else if(n < 65536) {
|
329
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 3);
|
330
|
+
uint16_t be = _msgpack_be16(n);
|
331
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xde, (const void*)&be, 2);
|
332
|
+
} else {
|
333
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 5);
|
334
|
+
uint32_t be = _msgpack_be32(n);
|
335
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), 0xdf, (const void*)&be, 4);
|
336
|
+
}
|
337
|
+
}
|
338
|
+
|
339
|
+
#ifdef COMPAT_HAVE_ENCODING
|
340
|
+
static inline bool msgpack_packer_is_binary(VALUE v, int encindex)
|
341
|
+
{
|
342
|
+
return encindex == msgpack_rb_encindex_ascii8bit;
|
343
|
+
}
|
344
|
+
|
345
|
+
static inline bool msgpack_packer_is_utf8_compat_string(VALUE v, int encindex)
|
346
|
+
{
|
347
|
+
return encindex == msgpack_rb_encindex_utf8
|
348
|
+
|| encindex == msgpack_rb_encindex_usascii
|
349
|
+
#ifdef ENC_CODERANGE_ASCIIONLY
|
350
|
+
/* Because ENC_CODERANGE_ASCIIONLY does not scan string, it may return ENC_CODERANGE_UNKNOWN unlike */
|
351
|
+
/* rb_enc_str_asciionly_p. It is always faster than rb_str_encode if it is available. */
|
352
|
+
/* Very old Rubinius (< v1.3.1) doesn't have ENC_CODERANGE_ASCIIONLY. */
|
353
|
+
|| (rb_enc_asciicompat(rb_enc_from_index(encindex)) && ENC_CODERANGE_ASCIIONLY(v))
|
354
|
+
#endif
|
355
|
+
;
|
356
|
+
}
|
357
|
+
#endif
|
358
|
+
|
359
|
+
static inline void msgpack_packer_write_string_value(msgpack_packer_t* pk, VALUE v)
|
360
|
+
{
|
361
|
+
/* TODO encoding conversion? */
|
362
|
+
/* actual return type of RSTRING_LEN is long */
|
363
|
+
unsigned long len = RSTRING_LEN(v);
|
364
|
+
if(len > 0xffffffffUL) {
|
365
|
+
// TODO rb_eArgError?
|
366
|
+
rb_raise(rb_eArgError, "size of string is too long to pack: %lu bytes should be <= %lu", len, 0xffffffffUL);
|
367
|
+
}
|
368
|
+
|
369
|
+
#ifdef COMPAT_HAVE_ENCODING
|
370
|
+
int encindex = ENCODING_GET(v);
|
371
|
+
if(msgpack_packer_is_binary(v, encindex)) {
|
372
|
+
/* write ASCII-8BIT string using Binary type */
|
373
|
+
msgpack_packer_write_bin_header(pk, (unsigned int)len);
|
374
|
+
msgpack_buffer_append_string(PACKER_BUFFER_(pk), v);
|
375
|
+
} else {
|
376
|
+
/* write UTF-8, US-ASCII, or 7bit-safe ascii-compatible string using String type directly */
|
377
|
+
if(!msgpack_packer_is_utf8_compat_string(v, encindex)) {
|
378
|
+
/* transcode other strings to UTF-8 and write using String type */
|
379
|
+
VALUE enc = rb_enc_from_encoding(rb_utf8_encoding()); /* rb_enc_from_encoding_index is not extern */
|
380
|
+
v = rb_str_encode(v, enc, 0, Qnil);
|
381
|
+
len = RSTRING_LEN(v);
|
382
|
+
}
|
383
|
+
msgpack_packer_write_raw_header(pk, (unsigned int)len);
|
384
|
+
msgpack_buffer_append_string(PACKER_BUFFER_(pk), v);
|
385
|
+
}
|
386
|
+
#else
|
387
|
+
msgpack_packer_write_raw_header(pk, (unsigned int)len);
|
388
|
+
msgpack_buffer_append_string(PACKER_BUFFER_(pk), v);
|
389
|
+
#endif
|
390
|
+
}
|
391
|
+
|
392
|
+
static inline void msgpack_packer_write_symbol_value(msgpack_packer_t* pk, VALUE v)
|
393
|
+
{
|
394
|
+
#ifdef HAVE_RB_SYM2STR
|
395
|
+
/* rb_sym2str is added since MRI 2.2.0 */
|
396
|
+
msgpack_packer_write_string_value(pk, rb_sym2str(v));
|
397
|
+
#else
|
398
|
+
const char* name = rb_id2name(SYM2ID(v));
|
399
|
+
unsigned long len = strlen(name);
|
400
|
+
/* actual return type of strlen is size_t */
|
401
|
+
if(len > 0xffffffffUL) {
|
402
|
+
// TODO rb_eArgError?
|
403
|
+
rb_raise(rb_eArgError, "size of symbol is too long to pack: %lu bytes should be <= %lu", len, 0xffffffffUL);
|
404
|
+
}
|
405
|
+
msgpack_packer_write_raw_header(pk, (unsigned int)len);
|
406
|
+
msgpack_buffer_append(PACKER_BUFFER_(pk), name, len);
|
407
|
+
#endif
|
408
|
+
}
|
409
|
+
|
410
|
+
static inline void msgpack_packer_write_fixnum_value(msgpack_packer_t* pk, VALUE v)
|
411
|
+
{
|
412
|
+
#ifdef JRUBY
|
413
|
+
msgpack_packer_write_long(pk, FIXNUM_P(v) ? FIX2LONG(v) : rb_num2ll(v));
|
414
|
+
#else
|
415
|
+
msgpack_packer_write_long(pk, FIX2LONG(v));
|
416
|
+
#endif
|
417
|
+
}
|
418
|
+
|
419
|
+
static inline void msgpack_packer_write_bignum_value(msgpack_packer_t* pk, VALUE v)
|
420
|
+
{
|
421
|
+
if(RBIGNUM_POSITIVE_P(v)) {
|
422
|
+
msgpack_packer_write_u64(pk, rb_big2ull(v));
|
423
|
+
} else {
|
424
|
+
msgpack_packer_write_long_long(pk, rb_big2ll(v));
|
425
|
+
}
|
426
|
+
}
|
427
|
+
|
428
|
+
static inline void msgpack_packer_write_float_value(msgpack_packer_t* pk, VALUE v)
|
429
|
+
{
|
430
|
+
msgpack_packer_write_double(pk, rb_num2dbl(v));
|
431
|
+
}
|
432
|
+
|
433
|
+
void msgpack_packer_write_array_value(msgpack_packer_t* pk, VALUE v);
|
434
|
+
|
435
|
+
void msgpack_packer_write_hash_value(msgpack_packer_t* pk, VALUE v);
|
436
|
+
|
437
|
+
void msgpack_packer_write_value(msgpack_packer_t* pk, VALUE v);
|
438
|
+
|
439
|
+
|
440
|
+
#endif
|
441
|
+
|
@@ -0,0 +1,302 @@
|
|
1
|
+
/*
|
2
|
+
* MessagePack for Ruby
|
3
|
+
*
|
4
|
+
* Copyright (C) 2008-2013 Sadayuki Furuhashi
|
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 "compat.h"
|
20
|
+
#include "ruby.h"
|
21
|
+
#include "packer.h"
|
22
|
+
#include "packer_class.h"
|
23
|
+
#include "buffer_class.h"
|
24
|
+
|
25
|
+
VALUE cMessagePack_Packer;
|
26
|
+
|
27
|
+
static ID s_to_msgpack;
|
28
|
+
static ID s_write;
|
29
|
+
|
30
|
+
//static VALUE s_packer_value;
|
31
|
+
//static msgpack_packer_t* s_packer;
|
32
|
+
|
33
|
+
#define PACKER(from, name) \
|
34
|
+
msgpack_packer_t* name; \
|
35
|
+
Data_Get_Struct(from, msgpack_packer_t, name); \
|
36
|
+
if(name == NULL) { \
|
37
|
+
rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
|
38
|
+
}
|
39
|
+
|
40
|
+
static void Packer_free(msgpack_packer_t* pk)
|
41
|
+
{
|
42
|
+
if(pk == NULL) {
|
43
|
+
return;
|
44
|
+
}
|
45
|
+
msgpack_packer_destroy(pk);
|
46
|
+
free(pk);
|
47
|
+
}
|
48
|
+
|
49
|
+
static VALUE Packer_alloc(VALUE klass)
|
50
|
+
{
|
51
|
+
msgpack_packer_t* pk = ALLOC_N(msgpack_packer_t, 1);
|
52
|
+
msgpack_packer_init(pk);
|
53
|
+
|
54
|
+
VALUE self = Data_Wrap_Struct(klass, msgpack_packer_mark, Packer_free, pk);
|
55
|
+
|
56
|
+
msgpack_packer_set_to_msgpack_method(pk, s_to_msgpack, self);
|
57
|
+
pk->buffer_ref = MessagePack_Buffer_wrap(PACKER_BUFFER_(pk), self);
|
58
|
+
|
59
|
+
return self;
|
60
|
+
}
|
61
|
+
|
62
|
+
static VALUE Packer_initialize(int argc, VALUE* argv, VALUE self)
|
63
|
+
{
|
64
|
+
VALUE io = Qnil;
|
65
|
+
VALUE options = Qnil;
|
66
|
+
|
67
|
+
if(argc == 0 || (argc == 1 && argv[0] == Qnil)) {
|
68
|
+
/* Qnil */
|
69
|
+
|
70
|
+
} else if(argc == 1) {
|
71
|
+
VALUE v = argv[0];
|
72
|
+
if(rb_type(v) == T_HASH) {
|
73
|
+
options = v;
|
74
|
+
} else {
|
75
|
+
io = v;
|
76
|
+
}
|
77
|
+
|
78
|
+
} else if(argc == 2) {
|
79
|
+
io = argv[0];
|
80
|
+
options = argv[1];
|
81
|
+
if(rb_type(options) != T_HASH) {
|
82
|
+
rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(options));
|
83
|
+
}
|
84
|
+
|
85
|
+
} else {
|
86
|
+
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
|
87
|
+
}
|
88
|
+
|
89
|
+
PACKER(self, pk);
|
90
|
+
MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, options);
|
91
|
+
|
92
|
+
// TODO MessagePack_Unpacker_initialize and options
|
93
|
+
|
94
|
+
return self;
|
95
|
+
}
|
96
|
+
|
97
|
+
static VALUE Packer_buffer(VALUE self)
|
98
|
+
{
|
99
|
+
PACKER(self, pk);
|
100
|
+
return pk->buffer_ref;
|
101
|
+
}
|
102
|
+
|
103
|
+
static VALUE Packer_write(VALUE self, VALUE v)
|
104
|
+
{
|
105
|
+
PACKER(self, pk);
|
106
|
+
msgpack_packer_write_value(pk, v);
|
107
|
+
return self;
|
108
|
+
}
|
109
|
+
|
110
|
+
static VALUE Packer_write_nil(VALUE self)
|
111
|
+
{
|
112
|
+
PACKER(self, pk);
|
113
|
+
msgpack_packer_write_nil(pk);
|
114
|
+
return self;
|
115
|
+
}
|
116
|
+
|
117
|
+
static VALUE Packer_write_array_header(VALUE self, VALUE n)
|
118
|
+
{
|
119
|
+
PACKER(self, pk);
|
120
|
+
msgpack_packer_write_array_header(pk, NUM2UINT(n));
|
121
|
+
return self;
|
122
|
+
}
|
123
|
+
|
124
|
+
static VALUE Packer_write_map_header(VALUE self, VALUE n)
|
125
|
+
{
|
126
|
+
PACKER(self, pk);
|
127
|
+
msgpack_packer_write_map_header(pk, NUM2UINT(n));
|
128
|
+
return self;
|
129
|
+
}
|
130
|
+
|
131
|
+
static VALUE Packer_flush(VALUE self)
|
132
|
+
{
|
133
|
+
PACKER(self, pk);
|
134
|
+
msgpack_buffer_flush(PACKER_BUFFER_(pk));
|
135
|
+
return self;
|
136
|
+
}
|
137
|
+
|
138
|
+
static VALUE Packer_clear(VALUE self)
|
139
|
+
{
|
140
|
+
PACKER(self, pk);
|
141
|
+
msgpack_buffer_clear(PACKER_BUFFER_(pk));
|
142
|
+
return Qnil;
|
143
|
+
}
|
144
|
+
|
145
|
+
static VALUE Packer_size(VALUE self)
|
146
|
+
{
|
147
|
+
PACKER(self, pk);
|
148
|
+
size_t size = msgpack_buffer_all_readable_size(PACKER_BUFFER_(pk));
|
149
|
+
return SIZET2NUM(size);
|
150
|
+
}
|
151
|
+
|
152
|
+
static VALUE Packer_empty_p(VALUE self)
|
153
|
+
{
|
154
|
+
PACKER(self, pk);
|
155
|
+
if(msgpack_buffer_top_readable_size(PACKER_BUFFER_(pk)) == 0) {
|
156
|
+
return Qtrue;
|
157
|
+
} else {
|
158
|
+
return Qfalse;
|
159
|
+
}
|
160
|
+
}
|
161
|
+
|
162
|
+
static VALUE Packer_to_str(VALUE self)
|
163
|
+
{
|
164
|
+
PACKER(self, pk);
|
165
|
+
return msgpack_buffer_all_as_string(PACKER_BUFFER_(pk));
|
166
|
+
}
|
167
|
+
|
168
|
+
static VALUE Packer_to_a(VALUE self)
|
169
|
+
{
|
170
|
+
PACKER(self, pk);
|
171
|
+
return msgpack_buffer_all_as_string_array(PACKER_BUFFER_(pk));
|
172
|
+
}
|
173
|
+
|
174
|
+
static VALUE Packer_write_to(VALUE self, VALUE io)
|
175
|
+
{
|
176
|
+
PACKER(self, pk);
|
177
|
+
size_t sz = msgpack_buffer_flush_to_io(PACKER_BUFFER_(pk), io, s_write, true);
|
178
|
+
return ULONG2NUM(sz);
|
179
|
+
}
|
180
|
+
|
181
|
+
//static VALUE Packer_append(VALUE self, VALUE string_or_buffer)
|
182
|
+
//{
|
183
|
+
// PACKER(self, pk);
|
184
|
+
//
|
185
|
+
// // TODO if string_or_buffer is a Buffer
|
186
|
+
// VALUE string = string_or_buffer;
|
187
|
+
//
|
188
|
+
// msgpack_buffer_append_string(PACKER_BUFFER_(pk), string);
|
189
|
+
//
|
190
|
+
// return self;
|
191
|
+
//}
|
192
|
+
|
193
|
+
VALUE MessagePack_pack(int argc, VALUE* argv)
|
194
|
+
{
|
195
|
+
VALUE v;
|
196
|
+
VALUE io = Qnil;
|
197
|
+
VALUE options = Qnil;
|
198
|
+
|
199
|
+
if(argc == 1) {
|
200
|
+
v = argv[0];
|
201
|
+
|
202
|
+
} else if(argc == 2) {
|
203
|
+
v = argv[0];
|
204
|
+
if(rb_type(argv[1]) == T_HASH) {
|
205
|
+
options = argv[1];
|
206
|
+
} else {
|
207
|
+
io = argv[1];
|
208
|
+
}
|
209
|
+
|
210
|
+
} else if(argc == 3) {
|
211
|
+
v = argv[0];
|
212
|
+
io = argv[1];
|
213
|
+
options = argv[2];
|
214
|
+
if(rb_type(options) != T_HASH) {
|
215
|
+
rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(options));
|
216
|
+
}
|
217
|
+
|
218
|
+
} else {
|
219
|
+
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..3)", 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
|
+
MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, options);
|
228
|
+
// TODO MessagePack_Unpacker_initialize and options
|
229
|
+
|
230
|
+
msgpack_packer_write_value(pk, v);
|
231
|
+
|
232
|
+
VALUE retval;
|
233
|
+
if(io != Qnil) {
|
234
|
+
msgpack_buffer_flush(PACKER_BUFFER_(pk));
|
235
|
+
retval = Qnil;
|
236
|
+
} else {
|
237
|
+
retval = msgpack_buffer_all_as_string(PACKER_BUFFER_(pk));
|
238
|
+
}
|
239
|
+
|
240
|
+
msgpack_buffer_clear(PACKER_BUFFER_(pk)); /* to free rmem before GC */
|
241
|
+
|
242
|
+
#ifdef RB_GC_GUARD
|
243
|
+
/* This prevents compilers from optimizing out the `self` variable
|
244
|
+
* from stack. Otherwise GC free()s it. */
|
245
|
+
RB_GC_GUARD(self);
|
246
|
+
#endif
|
247
|
+
|
248
|
+
return retval;
|
249
|
+
}
|
250
|
+
|
251
|
+
static VALUE MessagePack_dump_module_method(int argc, VALUE* argv, VALUE mod)
|
252
|
+
{
|
253
|
+
UNUSED(mod);
|
254
|
+
return MessagePack_pack(argc, argv);
|
255
|
+
}
|
256
|
+
|
257
|
+
static VALUE MessagePack_pack_module_method(int argc, VALUE* argv, VALUE mod)
|
258
|
+
{
|
259
|
+
UNUSED(mod);
|
260
|
+
return MessagePack_pack(argc, argv);
|
261
|
+
}
|
262
|
+
|
263
|
+
void MessagePack_Packer_module_init(VALUE mMessagePack)
|
264
|
+
{
|
265
|
+
s_to_msgpack = rb_intern("to_msgpack");
|
266
|
+
s_write = rb_intern("write");
|
267
|
+
|
268
|
+
msgpack_packer_static_init();
|
269
|
+
|
270
|
+
cMessagePack_Packer = rb_define_class_under(mMessagePack, "Packer", rb_cObject);
|
271
|
+
|
272
|
+
rb_define_alloc_func(cMessagePack_Packer, Packer_alloc);
|
273
|
+
|
274
|
+
rb_define_method(cMessagePack_Packer, "initialize", Packer_initialize, -1);
|
275
|
+
rb_define_method(cMessagePack_Packer, "buffer", Packer_buffer, 0);
|
276
|
+
rb_define_method(cMessagePack_Packer, "write", Packer_write, 1);
|
277
|
+
rb_define_alias(cMessagePack_Packer, "pack", "write");
|
278
|
+
rb_define_method(cMessagePack_Packer, "write_nil", Packer_write_nil, 0);
|
279
|
+
rb_define_method(cMessagePack_Packer, "write_array_header", Packer_write_array_header, 1);
|
280
|
+
rb_define_method(cMessagePack_Packer, "write_map_header", Packer_write_map_header, 1);
|
281
|
+
rb_define_method(cMessagePack_Packer, "flush", Packer_flush, 0);
|
282
|
+
|
283
|
+
/* delegation methods */
|
284
|
+
rb_define_method(cMessagePack_Packer, "clear", Packer_clear, 0);
|
285
|
+
rb_define_method(cMessagePack_Packer, "size", Packer_size, 0);
|
286
|
+
rb_define_method(cMessagePack_Packer, "empty?", Packer_empty_p, 0);
|
287
|
+
rb_define_method(cMessagePack_Packer, "write_to", Packer_write_to, 1);
|
288
|
+
rb_define_method(cMessagePack_Packer, "to_str", Packer_to_str, 0);
|
289
|
+
rb_define_alias(cMessagePack_Packer, "to_s", "to_str");
|
290
|
+
rb_define_method(cMessagePack_Packer, "to_a", Packer_to_a, 0);
|
291
|
+
//rb_define_method(cMessagePack_Packer, "append", Packer_append, 1);
|
292
|
+
//rb_define_alias(cMessagePack_Packer, "<<", "append");
|
293
|
+
|
294
|
+
//s_packer_value = Packer_alloc(cMessagePack_Packer);
|
295
|
+
//rb_gc_register_address(&s_packer_value);
|
296
|
+
//Data_Get_Struct(s_packer_value, msgpack_packer_t, s_packer);
|
297
|
+
|
298
|
+
/* MessagePack.pack(x) */
|
299
|
+
rb_define_module_function(mMessagePack, "pack", MessagePack_pack_module_method, -1);
|
300
|
+
rb_define_module_function(mMessagePack, "dump", MessagePack_dump_module_method, -1);
|
301
|
+
}
|
302
|
+
|