packsnap 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/ChangeLog +29 -0
- data/README.rdoc +26 -0
- data/Rakefile +150 -0
- data/doclib/msgpack.rb +54 -0
- data/doclib/packsnap/buffer.rb +177 -0
- data/doclib/packsnap/error.rb +14 -0
- data/doclib/packsnap/packer.rb +131 -0
- data/doclib/packsnap/unpacker.rb +130 -0
- data/ext/packsnap/buffer.cc +684 -0
- data/ext/packsnap/buffer.hh +439 -0
- data/ext/packsnap/buffer_class.cc +490 -0
- data/ext/packsnap/buffer_class.hh +32 -0
- data/ext/packsnap/compat.h +128 -0
- data/ext/packsnap/extconf.rb +94 -0
- data/ext/packsnap/packer.cc +137 -0
- data/ext/packsnap/packer.h +334 -0
- data/ext/packsnap/packer_class.cc +288 -0
- data/ext/packsnap/packer_class.hh +32 -0
- data/ext/packsnap/packsnap.h +4 -0
- data/ext/packsnap/rbinit.cc +52 -0
- data/ext/packsnap/rmem.cc +110 -0
- data/ext/packsnap/rmem.h +100 -0
- data/ext/packsnap/sysdep.h +112 -0
- data/ext/packsnap/sysdep_endian.h +50 -0
- data/ext/packsnap/sysdep_types.h +46 -0
- data/ext/packsnap/unpacker.cc +654 -0
- data/ext/packsnap/unpacker.hh +108 -0
- data/ext/packsnap/unpacker_class.cc +392 -0
- data/ext/packsnap/unpacker_class.hh +32 -0
- data/lib/packsnap.rb +12 -0
- data/lib/packsnap/version.rb +3 -0
- data/packsnap.gemspec +23 -0
- data/spec/buffer_io_spec.rb +228 -0
- data/spec/buffer_spec.rb +572 -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/format_spec.rb +225 -0
- data/spec/packer_spec.rb +127 -0
- data/spec/random_compat.rb +24 -0
- data/spec/spec_helper.rb +21 -0
- data/spec/unpacker_spec.rb +128 -0
- metadata +183 -0
@@ -0,0 +1,439 @@
|
|
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_H__
|
19
|
+
#define MSGPACK_RUBY_BUFFER_H__
|
20
|
+
|
21
|
+
#include "compat.h"
|
22
|
+
#include "sysdep.h"
|
23
|
+
|
24
|
+
#ifndef MSGPACK_BUFFER_STRING_WRITE_REFERENCE_DEFAULT
|
25
|
+
#define MSGPACK_BUFFER_STRING_WRITE_REFERENCE_DEFAULT (512*1024)
|
26
|
+
#endif
|
27
|
+
|
28
|
+
/* at least 23 (RSTRING_EMBED_LEN_MAX) bytes */
|
29
|
+
#ifndef MSGPACK_BUFFER_STRING_WRITE_REFERENCE_MINIMUM
|
30
|
+
#define MSGPACK_BUFFER_STRING_WRITE_REFERENCE_MINIMUM 256
|
31
|
+
#endif
|
32
|
+
|
33
|
+
#ifndef MSGPACK_BUFFER_STRING_READ_REFERENCE_DEFAULT
|
34
|
+
#define MSGPACK_BUFFER_STRING_READ_REFERENCE_DEFAULT 256
|
35
|
+
#endif
|
36
|
+
|
37
|
+
/* at least 23 (RSTRING_EMBED_LEN_MAX) bytes */
|
38
|
+
#ifndef MSGPACK_BUFFER_STRING_READ_REFERENCE_MINIMUM
|
39
|
+
#define MSGPACK_BUFFER_STRING_READ_REFERENCE_MINIMUM 256
|
40
|
+
#endif
|
41
|
+
|
42
|
+
#ifndef MSGPACK_BUFFER_IO_BUFFER_SIZE_DEFAULT
|
43
|
+
#define MSGPACK_BUFFER_IO_BUFFER_SIZE_DEFAULT (32*1024)
|
44
|
+
#endif
|
45
|
+
|
46
|
+
#ifndef MSGPACK_BUFFER_IO_BUFFER_SIZE_MINIMUM
|
47
|
+
#define MSGPACK_BUFFER_IO_BUFFER_SIZE_MINIMUM (1024)
|
48
|
+
#endif
|
49
|
+
|
50
|
+
#define NO_MAPPED_STRING ((VALUE)0)
|
51
|
+
|
52
|
+
struct msgpack_buffer_chunk_t;
|
53
|
+
typedef struct msgpack_buffer_chunk_t msgpack_buffer_chunk_t;
|
54
|
+
|
55
|
+
struct msgpack_buffer_t;
|
56
|
+
typedef struct msgpack_buffer_t msgpack_buffer_t;
|
57
|
+
|
58
|
+
/*
|
59
|
+
* msgpack_buffer_chunk_t
|
60
|
+
* +----------------+
|
61
|
+
* | filled | free |
|
62
|
+
* +---------+------+
|
63
|
+
* ^ first ^ last
|
64
|
+
*/
|
65
|
+
struct msgpack_buffer_chunk_t {
|
66
|
+
char* first;
|
67
|
+
char* last;
|
68
|
+
void* mem;
|
69
|
+
msgpack_buffer_chunk_t* next;
|
70
|
+
VALUE mapped_string; /* RBString or NO_MAPPED_STRING */
|
71
|
+
};
|
72
|
+
|
73
|
+
union msgpack_buffer_cast_block_t {
|
74
|
+
char buffer[8];
|
75
|
+
uint8_t u8;
|
76
|
+
uint16_t u16;
|
77
|
+
uint32_t u32;
|
78
|
+
uint64_t u64;
|
79
|
+
int8_t i8;
|
80
|
+
int16_t i16;
|
81
|
+
int32_t i32;
|
82
|
+
int64_t i64;
|
83
|
+
float f;
|
84
|
+
double d;
|
85
|
+
};
|
86
|
+
|
87
|
+
struct msgpack_buffer_t {
|
88
|
+
char* read_buffer;
|
89
|
+
char* tail_buffer_end;
|
90
|
+
|
91
|
+
msgpack_buffer_chunk_t tail;
|
92
|
+
msgpack_buffer_chunk_t* head;
|
93
|
+
msgpack_buffer_chunk_t* free_list;
|
94
|
+
|
95
|
+
char* rmem_last;
|
96
|
+
char* rmem_end;
|
97
|
+
void** rmem_owner;
|
98
|
+
|
99
|
+
union msgpack_buffer_cast_block_t cast_block;
|
100
|
+
|
101
|
+
VALUE io;
|
102
|
+
VALUE io_buffer;
|
103
|
+
ID io_write_all_method;
|
104
|
+
ID io_partial_read_method;
|
105
|
+
|
106
|
+
size_t write_reference_threshold;
|
107
|
+
size_t read_reference_threshold;
|
108
|
+
size_t io_buffer_size;
|
109
|
+
|
110
|
+
VALUE owner;
|
111
|
+
};
|
112
|
+
|
113
|
+
/*
|
114
|
+
* initialization functions
|
115
|
+
*/
|
116
|
+
void msgpack_buffer_static_init();
|
117
|
+
|
118
|
+
void msgpack_buffer_static_destroy();
|
119
|
+
|
120
|
+
void msgpack_buffer_init(msgpack_buffer_t* b);
|
121
|
+
|
122
|
+
void msgpack_buffer_destroy(msgpack_buffer_t* b);
|
123
|
+
|
124
|
+
void msgpack_buffer_mark(msgpack_buffer_t* b);
|
125
|
+
|
126
|
+
void msgpack_buffer_clear(msgpack_buffer_t* b);
|
127
|
+
|
128
|
+
static inline void msgpack_buffer_set_write_reference_threshold(msgpack_buffer_t* b, size_t length)
|
129
|
+
{
|
130
|
+
if(length < MSGPACK_BUFFER_STRING_WRITE_REFERENCE_MINIMUM) {
|
131
|
+
length = MSGPACK_BUFFER_STRING_WRITE_REFERENCE_MINIMUM;
|
132
|
+
}
|
133
|
+
b->write_reference_threshold = length;
|
134
|
+
}
|
135
|
+
|
136
|
+
static inline void msgpack_buffer_set_read_reference_threshold(msgpack_buffer_t* b, size_t length)
|
137
|
+
{
|
138
|
+
if(length < MSGPACK_BUFFER_STRING_READ_REFERENCE_MINIMUM) {
|
139
|
+
length = MSGPACK_BUFFER_STRING_READ_REFERENCE_MINIMUM;
|
140
|
+
}
|
141
|
+
b->read_reference_threshold = length;
|
142
|
+
}
|
143
|
+
|
144
|
+
static inline void msgpack_buffer_set_io_buffer_size(msgpack_buffer_t* b, size_t length)
|
145
|
+
{
|
146
|
+
if(length < MSGPACK_BUFFER_IO_BUFFER_SIZE_MINIMUM) {
|
147
|
+
length = MSGPACK_BUFFER_IO_BUFFER_SIZE_MINIMUM;
|
148
|
+
}
|
149
|
+
b->io_buffer_size = length;
|
150
|
+
}
|
151
|
+
|
152
|
+
static inline void msgpack_buffer_reset_io(msgpack_buffer_t* b)
|
153
|
+
{
|
154
|
+
b->io = Qnil;
|
155
|
+
}
|
156
|
+
|
157
|
+
static inline bool msgpack_buffer_has_io(msgpack_buffer_t* b)
|
158
|
+
{
|
159
|
+
return b->io != Qnil;
|
160
|
+
}
|
161
|
+
|
162
|
+
static inline void msgpack_buffer_reset(msgpack_buffer_t* b)
|
163
|
+
{
|
164
|
+
msgpack_buffer_clear(b);
|
165
|
+
msgpack_buffer_reset_io(b);
|
166
|
+
}
|
167
|
+
|
168
|
+
|
169
|
+
/*
|
170
|
+
* writer functions
|
171
|
+
*/
|
172
|
+
|
173
|
+
static inline size_t msgpack_buffer_writable_size(const msgpack_buffer_t* b)
|
174
|
+
{
|
175
|
+
return b->tail_buffer_end - b->tail.last;
|
176
|
+
}
|
177
|
+
|
178
|
+
static inline void msgpack_buffer_write_1(msgpack_buffer_t* b, int byte)
|
179
|
+
{
|
180
|
+
(*b->tail.last++) = (char) byte;
|
181
|
+
}
|
182
|
+
|
183
|
+
static inline void msgpack_buffer_write_2(msgpack_buffer_t* b, int byte1, unsigned char byte2)
|
184
|
+
{
|
185
|
+
*(b->tail.last++) = (char) byte1;
|
186
|
+
*(b->tail.last++) = (char) byte2;
|
187
|
+
}
|
188
|
+
|
189
|
+
static inline void msgpack_buffer_write_byte_and_data(msgpack_buffer_t* b, int byte, const void* data, size_t length)
|
190
|
+
{
|
191
|
+
(*b->tail.last++) = (char) byte;
|
192
|
+
|
193
|
+
memcpy(b->tail.last, data, length);
|
194
|
+
b->tail.last += length;
|
195
|
+
}
|
196
|
+
|
197
|
+
void _msgpack_buffer_expand(msgpack_buffer_t* b, const char* data, size_t length, bool use_flush);
|
198
|
+
|
199
|
+
size_t msgpack_buffer_flush_to_io(msgpack_buffer_t* b, VALUE io, ID write_method, bool consume);
|
200
|
+
|
201
|
+
static inline size_t msgpack_buffer_flush(msgpack_buffer_t* b)
|
202
|
+
{
|
203
|
+
if(b->io == Qnil) {
|
204
|
+
return 0;
|
205
|
+
}
|
206
|
+
return msgpack_buffer_flush_to_io(b, b->io, b->io_write_all_method, true);
|
207
|
+
}
|
208
|
+
|
209
|
+
static inline void msgpack_buffer_ensure_writable(msgpack_buffer_t* b, size_t require)
|
210
|
+
{
|
211
|
+
if(msgpack_buffer_writable_size(b) < require) {
|
212
|
+
_msgpack_buffer_expand(b, NULL, require, true);
|
213
|
+
}
|
214
|
+
}
|
215
|
+
|
216
|
+
static inline void _msgpack_buffer_append_impl(msgpack_buffer_t* b, const char* data, size_t length, bool flush_to_io)
|
217
|
+
{
|
218
|
+
if(length == 0) {
|
219
|
+
return;
|
220
|
+
}
|
221
|
+
|
222
|
+
if(length <= msgpack_buffer_writable_size(b)) {
|
223
|
+
memcpy(b->tail.last, data, length);
|
224
|
+
b->tail.last += length;
|
225
|
+
return;
|
226
|
+
}
|
227
|
+
|
228
|
+
_msgpack_buffer_expand(b, data, length, flush_to_io);
|
229
|
+
}
|
230
|
+
|
231
|
+
static inline void msgpack_buffer_append(msgpack_buffer_t* b, const char* data, size_t length)
|
232
|
+
{
|
233
|
+
_msgpack_buffer_append_impl(b, data, length, true);
|
234
|
+
}
|
235
|
+
|
236
|
+
static inline void msgpack_buffer_append_nonblock(msgpack_buffer_t* b, const char* data, size_t length)
|
237
|
+
{
|
238
|
+
_msgpack_buffer_append_impl(b, data, length, false);
|
239
|
+
}
|
240
|
+
|
241
|
+
void _msgpack_buffer_append_long_string(msgpack_buffer_t* b, VALUE string);
|
242
|
+
|
243
|
+
static inline size_t msgpack_buffer_append_string(msgpack_buffer_t* b, VALUE string)
|
244
|
+
{
|
245
|
+
size_t length = RSTRING_LEN(string);
|
246
|
+
|
247
|
+
if(length > b->write_reference_threshold) {
|
248
|
+
_msgpack_buffer_append_long_string(b, string);
|
249
|
+
|
250
|
+
} else {
|
251
|
+
msgpack_buffer_append(b, RSTRING_PTR(string), length);
|
252
|
+
}
|
253
|
+
|
254
|
+
return length;
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
/*
|
259
|
+
* IO functions
|
260
|
+
*/
|
261
|
+
size_t _msgpack_buffer_feed_from_io(msgpack_buffer_t* b);
|
262
|
+
|
263
|
+
size_t _msgpack_buffer_read_from_io_to_string(msgpack_buffer_t* b, VALUE string, size_t length);
|
264
|
+
|
265
|
+
size_t _msgpack_buffer_skip_from_io(msgpack_buffer_t* b, size_t length);
|
266
|
+
|
267
|
+
|
268
|
+
/*
|
269
|
+
* reader functions
|
270
|
+
*/
|
271
|
+
|
272
|
+
static inline size_t msgpack_buffer_top_readable_size(const msgpack_buffer_t* b)
|
273
|
+
{
|
274
|
+
return b->head->last - b->read_buffer;
|
275
|
+
}
|
276
|
+
|
277
|
+
size_t msgpack_buffer_all_readable_size(const msgpack_buffer_t* b);
|
278
|
+
|
279
|
+
bool _msgpack_buffer_shift_chunk(msgpack_buffer_t* b);
|
280
|
+
|
281
|
+
static inline void _msgpack_buffer_consumed(msgpack_buffer_t* b, size_t length)
|
282
|
+
{
|
283
|
+
b->read_buffer += length;
|
284
|
+
if(b->read_buffer >= b->head->last) {
|
285
|
+
_msgpack_buffer_shift_chunk(b);
|
286
|
+
//printf("shift %d tail_filled=%lu\n", i, b->tail.last - b->tail.first);
|
287
|
+
}
|
288
|
+
}
|
289
|
+
|
290
|
+
static inline int msgpack_buffer_peek_top_1(msgpack_buffer_t* b)
|
291
|
+
{
|
292
|
+
return (int) (unsigned char) b->read_buffer[0];
|
293
|
+
}
|
294
|
+
|
295
|
+
static inline int msgpack_buffer_read_top_1(msgpack_buffer_t* b)
|
296
|
+
{
|
297
|
+
int r = (int) (unsigned char) b->read_buffer[0];
|
298
|
+
|
299
|
+
_msgpack_buffer_consumed(b, 1);
|
300
|
+
|
301
|
+
return r;
|
302
|
+
}
|
303
|
+
|
304
|
+
static inline int msgpack_buffer_read_1(msgpack_buffer_t* b)
|
305
|
+
{
|
306
|
+
if(msgpack_buffer_top_readable_size(b) <= 0) {
|
307
|
+
if(b->io == Qnil) {
|
308
|
+
return -1;
|
309
|
+
}
|
310
|
+
_msgpack_buffer_feed_from_io(b);
|
311
|
+
}
|
312
|
+
|
313
|
+
int r = (int) (unsigned char) b->read_buffer[0];
|
314
|
+
_msgpack_buffer_consumed(b, 1);
|
315
|
+
|
316
|
+
return r;
|
317
|
+
}
|
318
|
+
|
319
|
+
|
320
|
+
/*
|
321
|
+
* bulk read / skip functions
|
322
|
+
*/
|
323
|
+
|
324
|
+
size_t msgpack_buffer_read_nonblock(msgpack_buffer_t* b, char* buffer, size_t length);
|
325
|
+
|
326
|
+
static inline bool msgpack_buffer_ensure_readable(msgpack_buffer_t* b, size_t require)
|
327
|
+
{
|
328
|
+
if(msgpack_buffer_top_readable_size(b) < require) {
|
329
|
+
size_t sz = msgpack_buffer_all_readable_size(b);
|
330
|
+
if(sz < require) {
|
331
|
+
if(b->io == Qnil) {
|
332
|
+
return false;
|
333
|
+
}
|
334
|
+
do {
|
335
|
+
size_t rl = _msgpack_buffer_feed_from_io(b);
|
336
|
+
sz += rl;
|
337
|
+
} while(sz < require);
|
338
|
+
}
|
339
|
+
}
|
340
|
+
return true;
|
341
|
+
}
|
342
|
+
|
343
|
+
bool _msgpack_buffer_read_all2(msgpack_buffer_t* b, char* buffer, size_t length);
|
344
|
+
|
345
|
+
static inline bool msgpack_buffer_read_all(msgpack_buffer_t* b, char* buffer, size_t length)
|
346
|
+
{
|
347
|
+
size_t avail = msgpack_buffer_top_readable_size(b);
|
348
|
+
if(avail < length) {
|
349
|
+
return _msgpack_buffer_read_all2(b, buffer, length);
|
350
|
+
}
|
351
|
+
|
352
|
+
memcpy(buffer, b->read_buffer, length);
|
353
|
+
_msgpack_buffer_consumed(b, length);
|
354
|
+
return true;
|
355
|
+
}
|
356
|
+
|
357
|
+
static inline size_t msgpack_buffer_skip_nonblock(msgpack_buffer_t* b, size_t length)
|
358
|
+
{
|
359
|
+
size_t avail = msgpack_buffer_top_readable_size(b);
|
360
|
+
if(avail < length) {
|
361
|
+
return msgpack_buffer_read_nonblock(b, NULL, length);
|
362
|
+
}
|
363
|
+
_msgpack_buffer_consumed(b, length);
|
364
|
+
return length;
|
365
|
+
}
|
366
|
+
|
367
|
+
static inline union msgpack_buffer_cast_block_t* msgpack_buffer_read_cast_block(msgpack_buffer_t* b, size_t n)
|
368
|
+
{
|
369
|
+
if(!msgpack_buffer_read_all(b, b->cast_block.buffer, n)) {
|
370
|
+
return NULL;
|
371
|
+
}
|
372
|
+
return &b->cast_block;
|
373
|
+
}
|
374
|
+
|
375
|
+
size_t msgpack_buffer_read_to_string_nonblock(msgpack_buffer_t* b, VALUE string, size_t length);
|
376
|
+
|
377
|
+
static inline size_t msgpack_buffer_read_to_string(msgpack_buffer_t* b, VALUE string, size_t length)
|
378
|
+
{
|
379
|
+
if(length == 0) {
|
380
|
+
return 0;
|
381
|
+
}
|
382
|
+
|
383
|
+
size_t avail = msgpack_buffer_top_readable_size(b);
|
384
|
+
if(avail > 0) {
|
385
|
+
return msgpack_buffer_read_to_string_nonblock(b, string, length);
|
386
|
+
} else if(b->io != Qnil) {
|
387
|
+
return _msgpack_buffer_read_from_io_to_string(b, string, length);
|
388
|
+
} else {
|
389
|
+
return 0;
|
390
|
+
}
|
391
|
+
}
|
392
|
+
|
393
|
+
static inline size_t msgpack_buffer_skip(msgpack_buffer_t* b, size_t length)
|
394
|
+
{
|
395
|
+
if(length == 0) {
|
396
|
+
return 0;
|
397
|
+
}
|
398
|
+
|
399
|
+
size_t avail = msgpack_buffer_top_readable_size(b);
|
400
|
+
if(avail > 0) {
|
401
|
+
return msgpack_buffer_skip_nonblock(b, length);
|
402
|
+
} else if(b->io != Qnil) {
|
403
|
+
return _msgpack_buffer_skip_from_io(b, length);
|
404
|
+
} else {
|
405
|
+
return 0;
|
406
|
+
}
|
407
|
+
}
|
408
|
+
|
409
|
+
|
410
|
+
VALUE msgpack_buffer_all_as_string(msgpack_buffer_t* b);
|
411
|
+
|
412
|
+
VALUE msgpack_buffer_all_as_string_array(msgpack_buffer_t* b);
|
413
|
+
|
414
|
+
static inline VALUE _msgpack_buffer_refer_head_mapped_string(msgpack_buffer_t* b, size_t length)
|
415
|
+
{
|
416
|
+
size_t offset = b->read_buffer - b->head->first;
|
417
|
+
return rb_str_substr(b->head->mapped_string, offset, length);
|
418
|
+
}
|
419
|
+
|
420
|
+
static inline VALUE msgpack_buffer_read_top_as_string(msgpack_buffer_t* b, size_t length, bool suppress_reference)
|
421
|
+
{
|
422
|
+
#ifndef DISABLE_BUFFER_READ_REFERENCE_OPTIMIZE
|
423
|
+
if(!suppress_reference &&
|
424
|
+
b->head->mapped_string != NO_MAPPED_STRING &&
|
425
|
+
length >= b->read_reference_threshold) {
|
426
|
+
VALUE result = _msgpack_buffer_refer_head_mapped_string(b, length);
|
427
|
+
_msgpack_buffer_consumed(b, length);
|
428
|
+
return result;
|
429
|
+
}
|
430
|
+
#endif
|
431
|
+
|
432
|
+
VALUE result = rb_str_new(b->read_buffer, length);
|
433
|
+
_msgpack_buffer_consumed(b, length);
|
434
|
+
return result;
|
435
|
+
}
|
436
|
+
|
437
|
+
|
438
|
+
#endif
|
439
|
+
|
@@ -0,0 +1,490 @@
|
|
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 "compat.h"
|
20
|
+
#include "ruby.h"
|
21
|
+
#include "buffer.hh"
|
22
|
+
#include "buffer_class.hh"
|
23
|
+
|
24
|
+
VALUE cMessagePack_Buffer;
|
25
|
+
|
26
|
+
static ID s_read;
|
27
|
+
static ID s_readpartial;
|
28
|
+
static ID s_write;
|
29
|
+
static ID s_append;
|
30
|
+
|
31
|
+
#define BUFFER(from, name) \
|
32
|
+
msgpack_buffer_t *name = NULL; \
|
33
|
+
Data_Get_Struct(from, msgpack_buffer_t, name); \
|
34
|
+
if(name == NULL) { \
|
35
|
+
rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
|
36
|
+
}
|
37
|
+
|
38
|
+
#define CHECK_STRING_TYPE(value) \
|
39
|
+
value = rb_check_string_type(value); \
|
40
|
+
if( NIL_P(value) ) { \
|
41
|
+
rb_raise(rb_eTypeError, "instance of String needed"); \
|
42
|
+
}
|
43
|
+
|
44
|
+
static void Buffer_free(void* data)
|
45
|
+
{
|
46
|
+
if(data == NULL) {
|
47
|
+
return;
|
48
|
+
}
|
49
|
+
msgpack_buffer_t* b = (msgpack_buffer_t*) data;
|
50
|
+
msgpack_buffer_destroy(b);
|
51
|
+
free(b);
|
52
|
+
}
|
53
|
+
|
54
|
+
static VALUE Buffer_alloc(VALUE klass)
|
55
|
+
{
|
56
|
+
msgpack_buffer_t* b = ALLOC_N(msgpack_buffer_t, 1);
|
57
|
+
msgpack_buffer_init(b);
|
58
|
+
|
59
|
+
return Data_Wrap_Struct(klass, msgpack_buffer_mark, Buffer_free, b);
|
60
|
+
}
|
61
|
+
|
62
|
+
static ID get_partial_read_method(VALUE io)
|
63
|
+
{
|
64
|
+
if(rb_respond_to(io, s_readpartial)) {
|
65
|
+
return s_readpartial;
|
66
|
+
} else if(rb_respond_to(io, s_read)) {
|
67
|
+
return s_read;
|
68
|
+
} else {
|
69
|
+
return s_read;
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
static ID get_write_all_method(VALUE io)
|
74
|
+
{
|
75
|
+
if(rb_respond_to(io, s_write)) {
|
76
|
+
return s_write;
|
77
|
+
} else if(rb_respond_to(io, s_append)) {
|
78
|
+
return s_append;
|
79
|
+
} else {
|
80
|
+
return s_write;
|
81
|
+
}
|
82
|
+
}
|
83
|
+
|
84
|
+
void MessagePack_Buffer_initialize(msgpack_buffer_t* b, VALUE io, VALUE options)
|
85
|
+
{
|
86
|
+
b->io = io;
|
87
|
+
b->io_partial_read_method = get_partial_read_method(io);
|
88
|
+
b->io_write_all_method = get_write_all_method(io);
|
89
|
+
|
90
|
+
if(options != Qnil) {
|
91
|
+
VALUE v;
|
92
|
+
|
93
|
+
v = rb_hash_aref(options, ID2SYM(rb_intern("read_reference_threshold")));
|
94
|
+
if(v != Qnil) {
|
95
|
+
msgpack_buffer_set_read_reference_threshold(b, NUM2ULONG(v));
|
96
|
+
}
|
97
|
+
|
98
|
+
v = rb_hash_aref(options, ID2SYM(rb_intern("write_reference_threshold")));
|
99
|
+
if(v != Qnil) {
|
100
|
+
msgpack_buffer_set_write_reference_threshold(b, NUM2ULONG(v));
|
101
|
+
}
|
102
|
+
|
103
|
+
v = rb_hash_aref(options, ID2SYM(rb_intern("io_buffer_size")));
|
104
|
+
if(v != Qnil) {
|
105
|
+
msgpack_buffer_set_io_buffer_size(b, NUM2ULONG(v));
|
106
|
+
}
|
107
|
+
}
|
108
|
+
}
|
109
|
+
|
110
|
+
extern "C"
|
111
|
+
VALUE MessagePack_Buffer_wrap(msgpack_buffer_t* b, VALUE owner)
|
112
|
+
{
|
113
|
+
b->owner = owner;
|
114
|
+
return Data_Wrap_Struct(cMessagePack_Buffer, msgpack_buffer_mark, NULL, b);
|
115
|
+
}
|
116
|
+
|
117
|
+
static VALUE Buffer_initialize(int argc, VALUE* argv, VALUE self)
|
118
|
+
{
|
119
|
+
VALUE io = Qnil;
|
120
|
+
VALUE options = Qnil;
|
121
|
+
|
122
|
+
if(argc == 0 || (argc == 1 && argv[0] == Qnil)) {
|
123
|
+
/* Qnil */
|
124
|
+
|
125
|
+
} else if(argc == 1) {
|
126
|
+
VALUE v = argv[0];
|
127
|
+
if(rb_type(v) == T_HASH) {
|
128
|
+
options = v;
|
129
|
+
} else {
|
130
|
+
io = v;
|
131
|
+
}
|
132
|
+
|
133
|
+
} else if(argc == 2) {
|
134
|
+
io = argv[0];
|
135
|
+
options = argv[1];
|
136
|
+
if(rb_type(options) != T_HASH) {
|
137
|
+
rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(io));
|
138
|
+
}
|
139
|
+
|
140
|
+
} else {
|
141
|
+
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
|
142
|
+
}
|
143
|
+
|
144
|
+
BUFFER(self, b);
|
145
|
+
|
146
|
+
MessagePack_Buffer_initialize(b, io, options);
|
147
|
+
|
148
|
+
return self;
|
149
|
+
}
|
150
|
+
|
151
|
+
static VALUE Buffer_clear(VALUE self)
|
152
|
+
{
|
153
|
+
BUFFER(self, b);
|
154
|
+
msgpack_buffer_clear(b);
|
155
|
+
return Qnil;
|
156
|
+
}
|
157
|
+
|
158
|
+
static VALUE Buffer_size(VALUE self)
|
159
|
+
{
|
160
|
+
BUFFER(self, b);
|
161
|
+
size_t size = msgpack_buffer_all_readable_size(b);
|
162
|
+
return SIZET2NUM(size);
|
163
|
+
}
|
164
|
+
|
165
|
+
static VALUE Buffer_empty_p(VALUE self)
|
166
|
+
{
|
167
|
+
BUFFER(self, b);
|
168
|
+
if(msgpack_buffer_top_readable_size(b) == 0) {
|
169
|
+
return Qtrue;
|
170
|
+
} else {
|
171
|
+
return Qfalse;
|
172
|
+
}
|
173
|
+
}
|
174
|
+
|
175
|
+
static VALUE Buffer_write(VALUE self, VALUE string_or_buffer)
|
176
|
+
{
|
177
|
+
BUFFER(self, b);
|
178
|
+
|
179
|
+
VALUE string = string_or_buffer; // TODO optimize if string_or_buffer is a Buffer
|
180
|
+
StringValue(string);
|
181
|
+
|
182
|
+
size_t length = msgpack_buffer_append_string(b, string);
|
183
|
+
|
184
|
+
return SIZET2NUM(length);
|
185
|
+
}
|
186
|
+
|
187
|
+
static VALUE Buffer_append(VALUE self, VALUE string_or_buffer)
|
188
|
+
{
|
189
|
+
BUFFER(self, b);
|
190
|
+
|
191
|
+
VALUE string = string_or_buffer; // TODO optimize if string_or_buffer is a Buffer
|
192
|
+
StringValue(string);
|
193
|
+
|
194
|
+
msgpack_buffer_append_string(b, string);
|
195
|
+
|
196
|
+
return self;
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
#define MAKE_EMPTY_STRING(orig) \
|
201
|
+
if(orig == Qnil) { \
|
202
|
+
orig = rb_str_buf_new(0); \
|
203
|
+
} else { \
|
204
|
+
rb_str_resize(orig, 0); \
|
205
|
+
}
|
206
|
+
|
207
|
+
static VALUE read_until_eof_rescue(VALUE args)
|
208
|
+
{
|
209
|
+
msgpack_buffer_t* b = (msgpack_buffer_t*) ((VALUE*) args)[0];
|
210
|
+
VALUE out = ((VALUE*) args)[1];
|
211
|
+
unsigned long max = ((VALUE*) args)[2];
|
212
|
+
size_t* sz = (size_t*) ((VALUE*) args)[3];
|
213
|
+
|
214
|
+
while(true) {
|
215
|
+
size_t rl;
|
216
|
+
if(max == 0) {
|
217
|
+
if(out == Qnil) {
|
218
|
+
rl = msgpack_buffer_skip(b, b->io_buffer_size);
|
219
|
+
} else {
|
220
|
+
rl = msgpack_buffer_read_to_string(b, out, b->io_buffer_size);
|
221
|
+
}
|
222
|
+
if(rl == 0) {
|
223
|
+
break;
|
224
|
+
}
|
225
|
+
*sz += rl;
|
226
|
+
|
227
|
+
} else {
|
228
|
+
if(out == Qnil) {
|
229
|
+
rl = msgpack_buffer_skip(b, max);
|
230
|
+
} else {
|
231
|
+
rl = msgpack_buffer_read_to_string(b, out, max);
|
232
|
+
}
|
233
|
+
if(rl == 0) {
|
234
|
+
break;
|
235
|
+
}
|
236
|
+
*sz += rl;
|
237
|
+
if(max <= rl) {
|
238
|
+
break;
|
239
|
+
} else {
|
240
|
+
max -= rl;
|
241
|
+
}
|
242
|
+
}
|
243
|
+
}
|
244
|
+
|
245
|
+
return Qnil;
|
246
|
+
}
|
247
|
+
|
248
|
+
static VALUE read_until_eof_error(VALUE args)
|
249
|
+
{
|
250
|
+
/* ignore EOFError */
|
251
|
+
UNUSED(args);
|
252
|
+
return Qnil;
|
253
|
+
}
|
254
|
+
|
255
|
+
static inline size_t read_until_eof(msgpack_buffer_t* b, VALUE out, unsigned long max)
|
256
|
+
{
|
257
|
+
if(msgpack_buffer_has_io(b)) {
|
258
|
+
size_t sz = 0;
|
259
|
+
VALUE args[4] = { (VALUE)(void*) b, out, (VALUE) max, (VALUE)(void*) &sz };
|
260
|
+
rb_rescue2((VALUE (*)(...))read_until_eof_rescue, (VALUE)(void*) args,
|
261
|
+
(VALUE (*)(...))read_until_eof_error, (VALUE)(void*) args,
|
262
|
+
rb_eEOFError, NULL);
|
263
|
+
return sz;
|
264
|
+
|
265
|
+
} else {
|
266
|
+
if(max == 0) {
|
267
|
+
max = ULONG_MAX;
|
268
|
+
}
|
269
|
+
if(out == Qnil) {
|
270
|
+
return msgpack_buffer_skip_nonblock(b, max);
|
271
|
+
} else {
|
272
|
+
return msgpack_buffer_read_to_string_nonblock(b, out, max);
|
273
|
+
}
|
274
|
+
}
|
275
|
+
}
|
276
|
+
|
277
|
+
static inline VALUE read_all(msgpack_buffer_t* b, VALUE out)
|
278
|
+
{
|
279
|
+
#ifndef DISABLE_BUFFER_READ_TO_S_OPTIMIZE
|
280
|
+
if(out == Qnil && !msgpack_buffer_has_io(b)) {
|
281
|
+
/* same as to_s && clear; optimize */
|
282
|
+
VALUE str = msgpack_buffer_all_as_string(b);
|
283
|
+
msgpack_buffer_clear(b);
|
284
|
+
return str;
|
285
|
+
}
|
286
|
+
#endif
|
287
|
+
MAKE_EMPTY_STRING(out);
|
288
|
+
read_until_eof(b, out, 0);
|
289
|
+
return out;
|
290
|
+
}
|
291
|
+
|
292
|
+
static VALUE Buffer_skip(VALUE self, VALUE sn)
|
293
|
+
{
|
294
|
+
BUFFER(self, b);
|
295
|
+
|
296
|
+
unsigned long n = FIX2ULONG(sn);
|
297
|
+
|
298
|
+
/* do nothing */
|
299
|
+
if(n == 0) {
|
300
|
+
return ULONG2NUM(0);
|
301
|
+
}
|
302
|
+
|
303
|
+
size_t sz = read_until_eof(b, Qnil, n);
|
304
|
+
return ULONG2NUM(sz);
|
305
|
+
}
|
306
|
+
|
307
|
+
static VALUE Buffer_skip_all(VALUE self, VALUE sn)
|
308
|
+
{
|
309
|
+
BUFFER(self, b);
|
310
|
+
|
311
|
+
unsigned long n = FIX2ULONG(sn);
|
312
|
+
|
313
|
+
/* do nothing */
|
314
|
+
if(n == 0) {
|
315
|
+
return self;
|
316
|
+
}
|
317
|
+
|
318
|
+
if(!msgpack_buffer_ensure_readable(b, n)) {
|
319
|
+
rb_raise(rb_eEOFError, "end of buffer reached");
|
320
|
+
}
|
321
|
+
|
322
|
+
msgpack_buffer_skip_nonblock(b, n);
|
323
|
+
|
324
|
+
return self;
|
325
|
+
}
|
326
|
+
|
327
|
+
static VALUE Buffer_read_all(int argc, VALUE* argv, VALUE self)
|
328
|
+
{
|
329
|
+
VALUE out = Qnil;
|
330
|
+
unsigned long n = 0;
|
331
|
+
bool all = false;
|
332
|
+
|
333
|
+
switch(argc) {
|
334
|
+
case 2:
|
335
|
+
out = argv[1];
|
336
|
+
/* pass through */
|
337
|
+
case 1:
|
338
|
+
n = FIX2ULONG(argv[0]);
|
339
|
+
break;
|
340
|
+
case 0:
|
341
|
+
all = true;
|
342
|
+
break;
|
343
|
+
default:
|
344
|
+
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
|
345
|
+
}
|
346
|
+
|
347
|
+
BUFFER(self, b);
|
348
|
+
|
349
|
+
if(out != Qnil) {
|
350
|
+
CHECK_STRING_TYPE(out);
|
351
|
+
}
|
352
|
+
|
353
|
+
if(all) {
|
354
|
+
return read_all(b, out);
|
355
|
+
}
|
356
|
+
|
357
|
+
if(n == 0) {
|
358
|
+
/* do nothing */
|
359
|
+
MAKE_EMPTY_STRING(out);
|
360
|
+
return out;
|
361
|
+
}
|
362
|
+
|
363
|
+
if(!msgpack_buffer_ensure_readable(b, n)) {
|
364
|
+
rb_raise(rb_eEOFError, "end of buffer reached");
|
365
|
+
}
|
366
|
+
|
367
|
+
MAKE_EMPTY_STRING(out);
|
368
|
+
size_t sz = msgpack_buffer_read_to_string_nonblock(b, out, n);
|
369
|
+
|
370
|
+
return out;
|
371
|
+
}
|
372
|
+
|
373
|
+
static VALUE Buffer_read(int argc, VALUE* argv, VALUE self)
|
374
|
+
{
|
375
|
+
VALUE out = Qnil;
|
376
|
+
unsigned long n = -1;
|
377
|
+
bool all = false;
|
378
|
+
|
379
|
+
switch(argc) {
|
380
|
+
case 2:
|
381
|
+
out = argv[1];
|
382
|
+
/* pass through */
|
383
|
+
case 1:
|
384
|
+
n = FIX2ULONG(argv[0]);
|
385
|
+
break;
|
386
|
+
case 0:
|
387
|
+
all = true;
|
388
|
+
break;
|
389
|
+
default:
|
390
|
+
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
|
391
|
+
}
|
392
|
+
|
393
|
+
BUFFER(self, b);
|
394
|
+
|
395
|
+
if(out != Qnil) {
|
396
|
+
CHECK_STRING_TYPE(out);
|
397
|
+
}
|
398
|
+
|
399
|
+
if(all) {
|
400
|
+
return read_all(b, out);
|
401
|
+
}
|
402
|
+
|
403
|
+
if(n == 0) {
|
404
|
+
/* do nothing */
|
405
|
+
MAKE_EMPTY_STRING(out);
|
406
|
+
return out;
|
407
|
+
}
|
408
|
+
|
409
|
+
#ifndef DISABLE_BUFFER_READ_TO_S_OPTIMIZE
|
410
|
+
if(!msgpack_buffer_has_io(b) && out == Qnil &&
|
411
|
+
msgpack_buffer_all_readable_size(b) <= n) {
|
412
|
+
/* same as to_s && clear; optimize */
|
413
|
+
VALUE str = msgpack_buffer_all_as_string(b);
|
414
|
+
msgpack_buffer_clear(b);
|
415
|
+
|
416
|
+
if(RSTRING_LEN(str) == 0) {
|
417
|
+
return Qnil;
|
418
|
+
} else {
|
419
|
+
return str;
|
420
|
+
}
|
421
|
+
}
|
422
|
+
#endif
|
423
|
+
|
424
|
+
MAKE_EMPTY_STRING(out);
|
425
|
+
read_until_eof(b, out, n);
|
426
|
+
|
427
|
+
if(RSTRING_LEN(out) == 0) {
|
428
|
+
return Qnil;
|
429
|
+
} else {
|
430
|
+
return out;
|
431
|
+
}
|
432
|
+
}
|
433
|
+
|
434
|
+
static VALUE Buffer_to_str(VALUE self)
|
435
|
+
{
|
436
|
+
BUFFER(self, b);
|
437
|
+
return msgpack_buffer_all_as_string(b);
|
438
|
+
}
|
439
|
+
|
440
|
+
static VALUE Buffer_to_a(VALUE self)
|
441
|
+
{
|
442
|
+
BUFFER(self, b);
|
443
|
+
return msgpack_buffer_all_as_string_array(b);
|
444
|
+
}
|
445
|
+
|
446
|
+
static VALUE Buffer_flush(VALUE self)
|
447
|
+
{
|
448
|
+
BUFFER(self, b);
|
449
|
+
msgpack_buffer_flush(b);
|
450
|
+
return self;
|
451
|
+
}
|
452
|
+
|
453
|
+
static VALUE Buffer_write_to(VALUE self, VALUE io)
|
454
|
+
{
|
455
|
+
BUFFER(self, b);
|
456
|
+
size_t sz = msgpack_buffer_flush_to_io(b, io, s_write, true);
|
457
|
+
return ULONG2NUM(sz);
|
458
|
+
}
|
459
|
+
|
460
|
+
extern "C"
|
461
|
+
void MessagePack_Buffer_module_init(VALUE mMessagePack)
|
462
|
+
{
|
463
|
+
s_read = rb_intern("read");
|
464
|
+
s_readpartial = rb_intern("readpartial");
|
465
|
+
s_write = rb_intern("write");
|
466
|
+
s_append = rb_intern("<<");
|
467
|
+
|
468
|
+
msgpack_buffer_static_init();
|
469
|
+
|
470
|
+
cMessagePack_Buffer = rb_define_class_under(mMessagePack, "Buffer", rb_cObject);
|
471
|
+
|
472
|
+
rb_define_alloc_func(cMessagePack_Buffer, Buffer_alloc);
|
473
|
+
|
474
|
+
rb_define_method(cMessagePack_Buffer, "initialize", (VALUE (*)(...))Buffer_initialize, -1);
|
475
|
+
rb_define_method(cMessagePack_Buffer, "clear", (VALUE (*)(...))Buffer_clear, 0);
|
476
|
+
rb_define_method(cMessagePack_Buffer, "size", (VALUE (*)(...))Buffer_size, 0);
|
477
|
+
rb_define_method(cMessagePack_Buffer, "empty?", (VALUE (*)(...))Buffer_empty_p, 0);
|
478
|
+
rb_define_method(cMessagePack_Buffer, "write", (VALUE (*)(...))Buffer_write, 1);
|
479
|
+
rb_define_method(cMessagePack_Buffer, "<<", (VALUE (*)(...))Buffer_append, 1);
|
480
|
+
rb_define_method(cMessagePack_Buffer, "skip", (VALUE (*)(...))Buffer_skip, 1);
|
481
|
+
rb_define_method(cMessagePack_Buffer, "skip_all", (VALUE (*)(...))Buffer_skip_all, 1);
|
482
|
+
rb_define_method(cMessagePack_Buffer, "read", (VALUE (*)(...))Buffer_read, -1);
|
483
|
+
rb_define_method(cMessagePack_Buffer, "read_all", (VALUE (*)(...))Buffer_read_all, -1);
|
484
|
+
rb_define_method(cMessagePack_Buffer, "flush", (VALUE (*)(...))Buffer_flush, 0);
|
485
|
+
rb_define_method(cMessagePack_Buffer, "write_to", (VALUE (*)(...))Buffer_write_to, 1);
|
486
|
+
rb_define_method(cMessagePack_Buffer, "to_str", (VALUE (*)(...))Buffer_to_str, 0);
|
487
|
+
rb_define_alias(cMessagePack_Buffer, "to_s", "to_str");
|
488
|
+
rb_define_method(cMessagePack_Buffer, "to_a", (VALUE (*)(...))Buffer_to_a, 0);
|
489
|
+
}
|
490
|
+
|