gitlab-cbor 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +25 -0
- data/.gitlab-ci.yml +44 -0
- data/.travis.yml +24 -0
- data/CHANGELOG.md +30 -0
- data/ChangeLog +106 -0
- data/Gemfile +11 -0
- data/README.rdoc +191 -0
- data/Rakefile +97 -0
- data/ci/test.rb +31 -0
- data/doclib/cbor/buffer.rb +193 -0
- data/doclib/cbor/core_ext.rb +133 -0
- data/doclib/cbor/error.rb +14 -0
- data/doclib/cbor/packer.rb +133 -0
- data/doclib/cbor/simple.rb +15 -0
- data/doclib/cbor/tagged.rb +16 -0
- data/doclib/cbor/unpacker.rb +138 -0
- data/doclib/cbor.rb +80 -0
- data/ext/cbor/buffer.c +693 -0
- data/ext/cbor/buffer.h +484 -0
- data/ext/cbor/buffer_class.c +561 -0
- data/ext/cbor/buffer_class.h +41 -0
- data/ext/cbor/cbor.h +69 -0
- data/ext/cbor/compat.h +147 -0
- data/ext/cbor/core_ext.c +199 -0
- data/ext/cbor/core_ext.h +35 -0
- data/ext/cbor/extconf.rb +29 -0
- data/ext/cbor/packer.c +169 -0
- data/ext/cbor/packer.h +370 -0
- data/ext/cbor/packer_class.c +345 -0
- data/ext/cbor/packer_class.h +40 -0
- data/ext/cbor/rbinit.c +57 -0
- data/ext/cbor/renamer.h +56 -0
- data/ext/cbor/rmem.c +103 -0
- data/ext/cbor/rmem.h +118 -0
- data/ext/cbor/sysdep.h +139 -0
- data/ext/cbor/sysdep_endian.h +59 -0
- data/ext/cbor/sysdep_types.h +55 -0
- data/ext/cbor/unpacker.c +787 -0
- data/ext/cbor/unpacker.h +135 -0
- data/ext/cbor/unpacker_class.c +483 -0
- data/ext/cbor/unpacker_class.h +39 -0
- data/gitlab-cbor.gemspec +27 -0
- data/lib/cbor/version.rb +3 -0
- data/lib/cbor.rb +6 -0
- data/spec/buffer_io_spec.rb +260 -0
- data/spec/buffer_spec.rb +576 -0
- data/spec/cases.cbor +0 -0
- data/spec/cases.cbor_stream +0 -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 +603 -0
- data/spec/packer_spec.rb +127 -0
- data/spec/random_compat.rb +24 -0
- data/spec/spec_helper.rb +68 -0
- data/spec/unpacker_spec.rb +260 -0
- metadata +189 -0
data/ext/cbor/packer.h
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* CBOR for Ruby
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2013 Carsten Bormann
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License").
|
|
7
|
+
*
|
|
8
|
+
* Based on:
|
|
9
|
+
***********/
|
|
10
|
+
/*
|
|
11
|
+
* MessagePack for Ruby
|
|
12
|
+
*
|
|
13
|
+
* Copyright (C) 2008-2013 Sadayuki Furuhashi
|
|
14
|
+
*
|
|
15
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
16
|
+
* you may not use this file except in compliance with the License.
|
|
17
|
+
* You may obtain a copy of the License at
|
|
18
|
+
*
|
|
19
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
20
|
+
*
|
|
21
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
22
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
23
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
24
|
+
* See the License for the specific language governing permissions and
|
|
25
|
+
* limitations under the License.
|
|
26
|
+
*/
|
|
27
|
+
#ifndef MSGPACK_RUBY_PACKER_H__
|
|
28
|
+
#define MSGPACK_RUBY_PACKER_H__
|
|
29
|
+
|
|
30
|
+
#include "buffer.h"
|
|
31
|
+
|
|
32
|
+
#ifndef MSGPACK_PACKER_IO_FLUSH_THRESHOLD_TO_WRITE_STRING_BODY
|
|
33
|
+
#define MSGPACK_PACKER_IO_FLUSH_THRESHOLD_TO_WRITE_STRING_BODY (1024)
|
|
34
|
+
#endif
|
|
35
|
+
|
|
36
|
+
struct msgpack_packer_t;
|
|
37
|
+
typedef struct msgpack_packer_t msgpack_packer_t;
|
|
38
|
+
|
|
39
|
+
struct msgpack_packer_t {
|
|
40
|
+
msgpack_buffer_t buffer;
|
|
41
|
+
|
|
42
|
+
VALUE io;
|
|
43
|
+
ID io_write_all_method;
|
|
44
|
+
|
|
45
|
+
ID to_msgpack_method;
|
|
46
|
+
VALUE to_msgpack_arg;
|
|
47
|
+
|
|
48
|
+
VALUE buffer_ref;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
#define PACKER_BUFFER_(pk) (&(pk)->buffer)
|
|
52
|
+
|
|
53
|
+
void msgpack_packer_static_init();
|
|
54
|
+
|
|
55
|
+
void msgpack_packer_static_destroy();
|
|
56
|
+
|
|
57
|
+
void msgpack_packer_init(msgpack_packer_t* pk);
|
|
58
|
+
|
|
59
|
+
void msgpack_packer_destroy(msgpack_packer_t* pk);
|
|
60
|
+
|
|
61
|
+
void msgpack_packer_mark(msgpack_packer_t* pk);
|
|
62
|
+
|
|
63
|
+
static inline void msgpack_packer_set_to_msgpack_method(msgpack_packer_t* pk,
|
|
64
|
+
ID to_msgpack_method, VALUE to_msgpack_arg)
|
|
65
|
+
{
|
|
66
|
+
pk->to_msgpack_method = to_msgpack_method;
|
|
67
|
+
pk->to_msgpack_arg = to_msgpack_arg;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static inline void msgpack_packer_set_io(msgpack_packer_t* pk, VALUE io, ID io_write_all_method)
|
|
71
|
+
{
|
|
72
|
+
pk->io = io;
|
|
73
|
+
pk->io_write_all_method = io_write_all_method;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
void msgpack_packer_reset(msgpack_packer_t* pk);
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
static inline void cbor_encoder_write_head(msgpack_packer_t* pk, unsigned int ib, uint64_t n)
|
|
80
|
+
{
|
|
81
|
+
if (n < 24) {
|
|
82
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
|
|
83
|
+
msgpack_buffer_write_1(PACKER_BUFFER_(pk), ib + (int)n);
|
|
84
|
+
} else if (n < 256) {
|
|
85
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 3);
|
|
86
|
+
msgpack_buffer_write_2(PACKER_BUFFER_(pk), ib + 24, n);
|
|
87
|
+
} else if (n < 65536) {
|
|
88
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 3);
|
|
89
|
+
uint16_t be = _msgpack_be16(n);
|
|
90
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), ib + 25, (const void*)&be, 2);
|
|
91
|
+
} else if (n < 0x100000000LU) {
|
|
92
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 5);
|
|
93
|
+
uint32_t be = _msgpack_be32(n);
|
|
94
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), ib + 26, (const void*)&be, 4);
|
|
95
|
+
} else {
|
|
96
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 9);
|
|
97
|
+
uint64_t be = _msgpack_be64(n);
|
|
98
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), ib + 27, (const void*)&be, 8);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
static inline void msgpack_packer_write_nil(msgpack_packer_t* pk)
|
|
103
|
+
{
|
|
104
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
|
|
105
|
+
msgpack_buffer_write_1(PACKER_BUFFER_(pk), IB_NIL);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
static inline void msgpack_packer_write_true(msgpack_packer_t* pk)
|
|
109
|
+
{
|
|
110
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
|
|
111
|
+
msgpack_buffer_write_1(PACKER_BUFFER_(pk), IB_TRUE);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
static inline void msgpack_packer_write_false(msgpack_packer_t* pk)
|
|
115
|
+
{
|
|
116
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
|
|
117
|
+
msgpack_buffer_write_1(PACKER_BUFFER_(pk), IB_FALSE);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
static inline void _msgpack_packer_write_long_long64(msgpack_packer_t* pk, long long v)
|
|
121
|
+
{
|
|
122
|
+
uint64_t ui = v >> 63; // extend sign to whole length
|
|
123
|
+
int mt = ui & IB_NEGFLAG; // extract major type
|
|
124
|
+
ui ^= v; // complement negatives
|
|
125
|
+
cbor_encoder_write_head(pk, mt, ui);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
static inline void msgpack_packer_write_long(msgpack_packer_t* pk, long v)
|
|
129
|
+
{
|
|
130
|
+
_msgpack_packer_write_long_long64(pk, v);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
static inline void msgpack_packer_write_u64(msgpack_packer_t* pk, uint64_t v)
|
|
134
|
+
{
|
|
135
|
+
cbor_encoder_write_head(pk, IB_UNSIGNED, v);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
static inline void msgpack_packer_write_double(msgpack_packer_t* pk, double v)
|
|
139
|
+
{
|
|
140
|
+
float fv = v;
|
|
141
|
+
if (fv == v) { /* 32 bits is enough and we aren't NaN */
|
|
142
|
+
union {
|
|
143
|
+
float f;
|
|
144
|
+
uint32_t u32;
|
|
145
|
+
char mem[4];
|
|
146
|
+
} castbuf = { fv };
|
|
147
|
+
int b32 = castbuf.u32;
|
|
148
|
+
if ((b32 & 0x1FFF) == 0) { /* worth trying half */
|
|
149
|
+
int s16 = (b32 >> 16) & 0x8000;
|
|
150
|
+
int exp = (b32 >> 23) & 0xff;
|
|
151
|
+
int mant = b32 & 0x7fffff;
|
|
152
|
+
if (exp == 0 && mant == 0)
|
|
153
|
+
; /* 0.0, -0.0 */
|
|
154
|
+
else if (exp >= 113 && exp <= 142) /* normalized */
|
|
155
|
+
s16 += ((exp - 112) << 10) + (mant >> 13);
|
|
156
|
+
else if (exp >= 103 && exp < 113) { /* denorm, exp16 = 0 */
|
|
157
|
+
if (mant & ((1 << (126 - exp)) - 1))
|
|
158
|
+
goto float32; /* loss of precision */
|
|
159
|
+
s16 += ((mant + 0x800000) >> (126 - exp));
|
|
160
|
+
} else if (exp == 255 && mant == 0) { /* Inf */
|
|
161
|
+
s16 += 0x7c00;
|
|
162
|
+
} else
|
|
163
|
+
goto float32; /* loss of range */
|
|
164
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 3);
|
|
165
|
+
uint16_t be = _msgpack_be16(s16);
|
|
166
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), IB_FLOAT2, (const void*)&be, 2);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
float32:
|
|
170
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 5);
|
|
171
|
+
castbuf.u32 = _msgpack_be_float(castbuf.u32);
|
|
172
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), IB_FLOAT4, castbuf.mem, 4);
|
|
173
|
+
} else {
|
|
174
|
+
union {
|
|
175
|
+
double d;
|
|
176
|
+
uint64_t u64;
|
|
177
|
+
char mem[8];
|
|
178
|
+
} castbuf = { v };
|
|
179
|
+
if (v != v && (castbuf.u64 & 0x1fffffffUL) == 0) { /* NaN && can narrow */
|
|
180
|
+
uint64_t sign = castbuf.u64 >> 63;
|
|
181
|
+
uint64_t mant = castbuf.u64 & 0xfffffffffffff; /* 52 bits */
|
|
182
|
+
if ((mant & 0x3ffffffffffUL) == 0) { /* 42 zero bits: narrow to f16 */
|
|
183
|
+
cbor_encoder_write_head(pk, 0xe0, sign << 15 | 0x7c00 | mant >> 42);
|
|
184
|
+
} else { /* 29 zero bits (checked above): narrow to f32 */
|
|
185
|
+
cbor_encoder_write_head(pk, 0xe0, sign << 31 | 0x7f800000 | mant >> 29);
|
|
186
|
+
}
|
|
187
|
+
} else { /* can't narrow */
|
|
188
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 9);
|
|
189
|
+
castbuf.u64 = _msgpack_be_double(castbuf.u64);
|
|
190
|
+
msgpack_buffer_write_byte_and_data(PACKER_BUFFER_(pk), IB_FLOAT8, castbuf.mem, 8);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
static inline void msgpack_packer_write_array_header(msgpack_packer_t* pk, uint64_t n)
|
|
196
|
+
{
|
|
197
|
+
cbor_encoder_write_head(pk, IB_ARRAY, n);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
static inline void msgpack_packer_write_map_header(msgpack_packer_t* pk, uint64_t n)
|
|
201
|
+
{
|
|
202
|
+
cbor_encoder_write_head(pk, IB_MAP, n);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
void _msgpack_packer_write_string_to_io(msgpack_packer_t* pk, VALUE string);
|
|
207
|
+
|
|
208
|
+
static inline void msgpack_packer_write_string_value(msgpack_packer_t* pk, VALUE v)
|
|
209
|
+
{
|
|
210
|
+
int mt = IB_TEXT; /* text string */
|
|
211
|
+
#ifdef COMPAT_HAVE_ENCODING
|
|
212
|
+
int enc = ENCODING_GET(v);
|
|
213
|
+
if (enc == s_enc_ascii8bit) {
|
|
214
|
+
mt = IB_BYTES;
|
|
215
|
+
} else if (enc != s_enc_utf8 && enc != s_enc_usascii) {
|
|
216
|
+
if(!ENC_CODERANGE_ASCIIONLY(v)) {
|
|
217
|
+
v = rb_str_encode(v, s_enc_utf8_value, 0, Qnil);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
#endif
|
|
221
|
+
cbor_encoder_write_head(pk, mt, RSTRING_LEN(v));
|
|
222
|
+
msgpack_buffer_append_string(PACKER_BUFFER_(pk), v);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
static inline void msgpack_packer_write_symbol_value(msgpack_packer_t* pk, VALUE v)
|
|
226
|
+
{
|
|
227
|
+
#ifdef HAVE_RB_SYM2STR
|
|
228
|
+
/* rb_sym2str is added since MRI 2.2.0 */
|
|
229
|
+
msgpack_packer_write_string_value(pk, rb_sym2str(v));
|
|
230
|
+
#else
|
|
231
|
+
const char* name = rb_id2name(SYM2ID(v));
|
|
232
|
+
/* actual return type of strlen is size_t */
|
|
233
|
+
unsigned long len = strlen(name);
|
|
234
|
+
if(len > 0xffffffffUL) {
|
|
235
|
+
// TODO rb_eArgError?
|
|
236
|
+
rb_raise(rb_eArgError, "size of symbol is too long to pack: %lu bytes should be <= %lu", len, 0xffffffffUL);
|
|
237
|
+
}
|
|
238
|
+
cbor_encoder_write_head(pk, IB_TEXT, len);
|
|
239
|
+
msgpack_buffer_append(PACKER_BUFFER_(pk), name, len);
|
|
240
|
+
#endif
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
static inline void msgpack_packer_write_fixnum_value(msgpack_packer_t* pk, VALUE v)
|
|
244
|
+
{
|
|
245
|
+
#ifdef JRUBY
|
|
246
|
+
msgpack_packer_write_long(pk, FIXNUM_P(v) ? FIX2LONG(v) : rb_num2ll(v));
|
|
247
|
+
#else
|
|
248
|
+
msgpack_packer_write_long(pk, FIX2LONG(v));
|
|
249
|
+
#endif
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
static inline void msgpack_packer_write_bignum_value(msgpack_packer_t* pk, VALUE v)
|
|
253
|
+
{
|
|
254
|
+
long len;
|
|
255
|
+
int ib = IB_UNSIGNED;
|
|
256
|
+
if (!RBIGNUM_POSITIVE_P(v)) {
|
|
257
|
+
v = rb_funcall(v, rb_intern("~"), 0); /* should be rb_big_neg(), but that is static. */
|
|
258
|
+
ib = IB_NEGATIVE;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
#ifdef HAVE_RB_INTEGER_UNPACK
|
|
263
|
+
len = rb_absint_size(v, NULL);
|
|
264
|
+
|
|
265
|
+
if (len > SIZEOF_LONG_LONG) { /* i.e., need real bignum */
|
|
266
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
|
|
267
|
+
msgpack_buffer_write_1(PACKER_BUFFER_(pk), IB_BIGNUM + IB_NEGFLAG_AS_BIT(ib));
|
|
268
|
+
cbor_encoder_write_head(pk, IB_BYTES, len);
|
|
269
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), len);
|
|
270
|
+
|
|
271
|
+
char buf[len]; /* XXX */
|
|
272
|
+
if (rb_integer_pack(v, buf, len, 1, 0, INTEGER_PACK_BIG_ENDIAN) != 1)
|
|
273
|
+
rb_raise(rb_eRangeError, "cbor rb_integer_pack() error");
|
|
274
|
+
|
|
275
|
+
msgpack_buffer_append(PACKER_BUFFER_(pk), buf, len);
|
|
276
|
+
|
|
277
|
+
#else
|
|
278
|
+
|
|
279
|
+
len = RBIGNUM_LEN(v);
|
|
280
|
+
if (len > SIZEOF_LONG_LONG/SIZEOF_BDIGITS) {
|
|
281
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), 1);
|
|
282
|
+
msgpack_buffer_write_1(PACKER_BUFFER_(pk), IB_BIGNUM + IB_NEGFLAG_AS_BIT(ib));
|
|
283
|
+
{
|
|
284
|
+
#ifndef CANT_DO_BIGNUMS_FAST_ON_THIS_PLATFORM
|
|
285
|
+
BDIGIT *dp = RBIGNUM_DIGITS(v);
|
|
286
|
+
BDIGIT *de = dp + len;
|
|
287
|
+
int nbyte = (len - 1) * SIZEOF_BDIGITS;
|
|
288
|
+
int nbmsdig = 0;
|
|
289
|
+
BDIGIT msdig;
|
|
290
|
+
int i;
|
|
291
|
+
if ((msdig = de[-1]) == 0) /* todo: check whether that occurs */
|
|
292
|
+
rb_raise(rb_eRangeError, "cbor writing unnormalized bignum");
|
|
293
|
+
while (msdig) { /* get number of significant bytes in msdig */
|
|
294
|
+
nbmsdig++;
|
|
295
|
+
msdig >>= 8;
|
|
296
|
+
}
|
|
297
|
+
nbyte += nbmsdig;
|
|
298
|
+
cbor_encoder_write_head(pk, IB_BYTES, nbyte);
|
|
299
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), nbyte);
|
|
300
|
+
/* first digit: */
|
|
301
|
+
msdig = de[-1];
|
|
302
|
+
while (nbmsdig) {
|
|
303
|
+
--nbmsdig;
|
|
304
|
+
msgpack_buffer_write_1(PACKER_BUFFER_(pk), msdig >> (nbmsdig << 3));
|
|
305
|
+
}
|
|
306
|
+
/* rest of the digits: */
|
|
307
|
+
for (i = 1; i < len; ++i) {
|
|
308
|
+
BDIGIT be = NTOHBDIGIT(de[-1-i]);
|
|
309
|
+
msgpack_buffer_append(PACKER_BUFFER_(pk), (const void*)&be, SIZEOF_BDIGITS);
|
|
310
|
+
}
|
|
311
|
+
#else
|
|
312
|
+
/* This is a slow workaround only... But a working one.*/
|
|
313
|
+
size_t nbyte;
|
|
314
|
+
VALUE hexval = rb_funcall(v, rb_intern("to_s"), 1, INT2FIX(16));
|
|
315
|
+
char *hp;
|
|
316
|
+
int i;
|
|
317
|
+
if (RSTRING_LEN(hexval) & 1)
|
|
318
|
+
rb_funcall(hexval, rb_intern("[]="), 3, INT2FIX(0), INT2FIX(0), rb_str_new("0", 1));
|
|
319
|
+
nbyte = RSTRING_LEN(hexval) >> 1;
|
|
320
|
+
hp = RSTRING_PTR(hexval);
|
|
321
|
+
cbor_encoder_write_head(pk, IB_BYTES, nbyte);
|
|
322
|
+
msgpack_buffer_ensure_writable(PACKER_BUFFER_(pk), nbyte);
|
|
323
|
+
for (i = 0; i < nbyte; i++) {
|
|
324
|
+
int c;
|
|
325
|
+
sscanf(hp, "%2x", &c);
|
|
326
|
+
hp += 2;
|
|
327
|
+
msgpack_buffer_write_1(PACKER_BUFFER_(pk), c);
|
|
328
|
+
}
|
|
329
|
+
#endif
|
|
330
|
+
}
|
|
331
|
+
#endif
|
|
332
|
+
} else {
|
|
333
|
+
cbor_encoder_write_head(pk, ib, rb_big2ull(v));
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
#ifdef RB_GC_GUARD
|
|
337
|
+
RB_GC_GUARD(v);
|
|
338
|
+
#endif
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
static inline void msgpack_packer_write_float_value(msgpack_packer_t* pk, VALUE v)
|
|
342
|
+
{
|
|
343
|
+
msgpack_packer_write_double(pk, rb_num2dbl(v));
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
static inline void msgpack_packer_write_simple_value(msgpack_packer_t* pk, VALUE v)
|
|
347
|
+
{
|
|
348
|
+
cbor_encoder_write_head(pk, IB_PRIM, FIX2LONG(rb_struct_aref(v, INT2FIX(0))));
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
void msgpack_packer_write_array_value(msgpack_packer_t* pk, VALUE v);
|
|
352
|
+
|
|
353
|
+
void msgpack_packer_write_hash_value(msgpack_packer_t* pk, VALUE v);
|
|
354
|
+
|
|
355
|
+
void msgpack_packer_write_value(msgpack_packer_t* pk, VALUE v);
|
|
356
|
+
|
|
357
|
+
static inline void msgpack_packer_write_tagged_value(msgpack_packer_t* pk, VALUE v)
|
|
358
|
+
{
|
|
359
|
+
cbor_encoder_write_head(pk, IB_TAG, rb_num2ulong(rb_struct_aref(v, INT2FIX(0))));
|
|
360
|
+
msgpack_packer_write_value(pk, rb_struct_aref(v, INT2FIX(1)));
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
static inline void msgpack_packer_write_processed_value(msgpack_packer_t* pk, VALUE v, ID method, int tag)
|
|
364
|
+
{
|
|
365
|
+
cbor_encoder_write_head(pk, IB_TAG, tag);
|
|
366
|
+
msgpack_packer_write_value(pk, rb_funcall(v, method, 0));
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
#endif
|
|
370
|
+
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* CBOR for Ruby
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2013 Carsten Bormann
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License").
|
|
7
|
+
*
|
|
8
|
+
* Based on:
|
|
9
|
+
***********/
|
|
10
|
+
/*
|
|
11
|
+
* MessagePack for Ruby
|
|
12
|
+
*
|
|
13
|
+
* Copyright (C) 2008-2013 Sadayuki Furuhashi
|
|
14
|
+
*
|
|
15
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
16
|
+
* you may not use this file except in compliance with the License.
|
|
17
|
+
* You may obtain a copy of the License at
|
|
18
|
+
*
|
|
19
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
20
|
+
*
|
|
21
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
22
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
23
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
24
|
+
* See the License for the specific language governing permissions and
|
|
25
|
+
* limitations under the License.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
#include "compat.h"
|
|
29
|
+
#include "ruby.h"
|
|
30
|
+
#include "packer.h"
|
|
31
|
+
#include "packer_class.h"
|
|
32
|
+
#include "buffer_class.h"
|
|
33
|
+
|
|
34
|
+
VALUE cMessagePack_Packer;
|
|
35
|
+
|
|
36
|
+
static ID s_to_msgpack;
|
|
37
|
+
static ID s_write;
|
|
38
|
+
|
|
39
|
+
//static VALUE s_packer_value;
|
|
40
|
+
//static msgpack_packer_t* s_packer;
|
|
41
|
+
|
|
42
|
+
static msgpack_packer_t* Packer_get(VALUE self);
|
|
43
|
+
|
|
44
|
+
#define PACKER(from, name) \
|
|
45
|
+
msgpack_packer_t* name = Packer_get(from); \
|
|
46
|
+
if(name == NULL) { \
|
|
47
|
+
rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static void Packer_mark(void* data)
|
|
51
|
+
{
|
|
52
|
+
msgpack_packer_t* pk = data;
|
|
53
|
+
msgpack_packer_mark(pk);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static void Packer_free(void* data)
|
|
57
|
+
{
|
|
58
|
+
msgpack_packer_t* pk = data;
|
|
59
|
+
if(pk == NULL) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
msgpack_packer_destroy(pk);
|
|
63
|
+
xfree(pk);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
#ifdef TypedData_Make_Struct
|
|
67
|
+
static size_t Packer_memsize(const void* data)
|
|
68
|
+
{
|
|
69
|
+
return data == NULL ? 0 : sizeof(msgpack_packer_t);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static const rb_data_type_t packer_data_type = {
|
|
73
|
+
"CBOR::Packer",
|
|
74
|
+
{Packer_mark, Packer_free, Packer_memsize,},
|
|
75
|
+
NULL, NULL, 0
|
|
76
|
+
};
|
|
77
|
+
#endif
|
|
78
|
+
|
|
79
|
+
static msgpack_packer_t* Packer_get(VALUE self)
|
|
80
|
+
{
|
|
81
|
+
msgpack_packer_t* pk = NULL;
|
|
82
|
+
#ifdef TypedData_Make_Struct
|
|
83
|
+
TypedData_Get_Struct(self, msgpack_packer_t, &packer_data_type, pk);
|
|
84
|
+
#else
|
|
85
|
+
Data_Get_Struct(self, msgpack_packer_t, pk);
|
|
86
|
+
#endif
|
|
87
|
+
return pk;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
msgpack_packer_t* MessagePack_Packer_get(VALUE self)
|
|
91
|
+
{
|
|
92
|
+
PACKER(self, pk);
|
|
93
|
+
return pk;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
static VALUE Packer_alloc(VALUE klass)
|
|
97
|
+
{
|
|
98
|
+
msgpack_packer_t* pk;
|
|
99
|
+
VALUE self;
|
|
100
|
+
#ifdef TypedData_Make_Struct
|
|
101
|
+
self = TypedData_Make_Struct(klass, msgpack_packer_t, &packer_data_type, pk);
|
|
102
|
+
#else
|
|
103
|
+
self = Data_Make_Struct(klass, msgpack_packer_t, Packer_mark, Packer_free, pk);
|
|
104
|
+
#endif
|
|
105
|
+
msgpack_packer_init(pk);
|
|
106
|
+
|
|
107
|
+
msgpack_packer_set_to_msgpack_method(pk, s_to_msgpack, self);
|
|
108
|
+
pk->buffer_ref = MessagePack_Buffer_wrap(PACKER_BUFFER_(pk), self);
|
|
109
|
+
|
|
110
|
+
return self;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static VALUE Packer_initialize(int argc, VALUE* argv, VALUE self)
|
|
114
|
+
{
|
|
115
|
+
VALUE io = Qnil;
|
|
116
|
+
VALUE options = Qnil;
|
|
117
|
+
|
|
118
|
+
if(argc == 0 || (argc == 1 && argv[0] == Qnil)) {
|
|
119
|
+
/* Qnil */
|
|
120
|
+
|
|
121
|
+
} else if(argc == 1) {
|
|
122
|
+
VALUE v = argv[0];
|
|
123
|
+
if(rb_type(v) == T_HASH) {
|
|
124
|
+
options = v;
|
|
125
|
+
} else {
|
|
126
|
+
io = v;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
} else if(argc == 2) {
|
|
130
|
+
io = argv[0];
|
|
131
|
+
options = argv[1];
|
|
132
|
+
if(rb_type(options) != T_HASH) {
|
|
133
|
+
rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(io));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
} else {
|
|
137
|
+
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
PACKER(self, pk);
|
|
141
|
+
if(io != Qnil || options != Qnil) {
|
|
142
|
+
MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, options);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// TODO options
|
|
146
|
+
|
|
147
|
+
return self;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
static VALUE Packer_buffer(VALUE self)
|
|
151
|
+
{
|
|
152
|
+
PACKER(self, pk);
|
|
153
|
+
return pk->buffer_ref;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
static VALUE Packer_write(VALUE self, VALUE v)
|
|
157
|
+
{
|
|
158
|
+
PACKER(self, pk);
|
|
159
|
+
msgpack_packer_write_value(pk, v);
|
|
160
|
+
return self;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
static VALUE Packer_write_nil(VALUE self)
|
|
164
|
+
{
|
|
165
|
+
PACKER(self, pk);
|
|
166
|
+
msgpack_packer_write_nil(pk);
|
|
167
|
+
return self;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
static VALUE Packer_write_array_header(VALUE self, VALUE n)
|
|
171
|
+
{
|
|
172
|
+
PACKER(self, pk);
|
|
173
|
+
msgpack_packer_write_array_header(pk, NUM2UINT(n));
|
|
174
|
+
return self;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
static VALUE Packer_write_map_header(VALUE self, VALUE n)
|
|
178
|
+
{
|
|
179
|
+
PACKER(self, pk);
|
|
180
|
+
msgpack_packer_write_map_header(pk, NUM2UINT(n));
|
|
181
|
+
return self;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
static VALUE Packer_flush(VALUE self)
|
|
185
|
+
{
|
|
186
|
+
PACKER(self, pk);
|
|
187
|
+
msgpack_buffer_flush(PACKER_BUFFER_(pk));
|
|
188
|
+
return self;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
static VALUE Packer_clear(VALUE self)
|
|
192
|
+
{
|
|
193
|
+
PACKER(self, pk);
|
|
194
|
+
msgpack_buffer_clear(PACKER_BUFFER_(pk));
|
|
195
|
+
return Qnil;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
static VALUE Packer_size(VALUE self)
|
|
199
|
+
{
|
|
200
|
+
PACKER(self, pk);
|
|
201
|
+
size_t size = msgpack_buffer_all_readable_size(PACKER_BUFFER_(pk));
|
|
202
|
+
return SIZET2NUM(size);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
static VALUE Packer_empty_p(VALUE self)
|
|
206
|
+
{
|
|
207
|
+
PACKER(self, pk);
|
|
208
|
+
if(msgpack_buffer_top_readable_size(PACKER_BUFFER_(pk)) == 0) {
|
|
209
|
+
return Qtrue;
|
|
210
|
+
} else {
|
|
211
|
+
return Qfalse;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
static VALUE Packer_to_str(VALUE self)
|
|
216
|
+
{
|
|
217
|
+
PACKER(self, pk);
|
|
218
|
+
return msgpack_buffer_all_as_string(PACKER_BUFFER_(pk));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
static VALUE Packer_to_a(VALUE self)
|
|
222
|
+
{
|
|
223
|
+
PACKER(self, pk);
|
|
224
|
+
return msgpack_buffer_all_as_string_array(PACKER_BUFFER_(pk));
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
static VALUE Packer_write_to(VALUE self, VALUE io)
|
|
228
|
+
{
|
|
229
|
+
PACKER(self, pk);
|
|
230
|
+
size_t sz = msgpack_buffer_flush_to_io(PACKER_BUFFER_(pk), io, s_write, true);
|
|
231
|
+
return ULONG2NUM(sz);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
//static VALUE Packer_append(VALUE self, VALUE string_or_buffer)
|
|
235
|
+
//{
|
|
236
|
+
// PACKER(self, pk);
|
|
237
|
+
//
|
|
238
|
+
// // TODO if string_or_buffer is a Buffer
|
|
239
|
+
// VALUE string = string_or_buffer;
|
|
240
|
+
//
|
|
241
|
+
// msgpack_buffer_append_string(PACKER_BUFFER_(pk), string);
|
|
242
|
+
//
|
|
243
|
+
// return self;
|
|
244
|
+
//}
|
|
245
|
+
|
|
246
|
+
VALUE MessagePack_pack(int argc, VALUE* argv)
|
|
247
|
+
{
|
|
248
|
+
// TODO options
|
|
249
|
+
|
|
250
|
+
VALUE v;
|
|
251
|
+
VALUE io = Qnil;
|
|
252
|
+
|
|
253
|
+
switch(argc) {
|
|
254
|
+
case 2:
|
|
255
|
+
io = argv[1];
|
|
256
|
+
/* pass-through */
|
|
257
|
+
case 1:
|
|
258
|
+
v = argv[0];
|
|
259
|
+
break;
|
|
260
|
+
default:
|
|
261
|
+
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
VALUE self = Packer_alloc(cMessagePack_Packer);
|
|
265
|
+
PACKER(self, pk);
|
|
266
|
+
//msgpack_packer_reset(s_packer);
|
|
267
|
+
//msgpack_buffer_reset_io(PACKER_BUFFER_(s_packer));
|
|
268
|
+
|
|
269
|
+
if(io != Qnil) {
|
|
270
|
+
MessagePack_Buffer_initialize(PACKER_BUFFER_(pk), io, Qnil);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
msgpack_packer_write_value(pk, v);
|
|
274
|
+
|
|
275
|
+
VALUE retval;
|
|
276
|
+
if(io != Qnil) {
|
|
277
|
+
msgpack_buffer_flush(PACKER_BUFFER_(pk));
|
|
278
|
+
retval = Qnil;
|
|
279
|
+
} else {
|
|
280
|
+
retval = msgpack_buffer_all_as_string(PACKER_BUFFER_(pk));
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
msgpack_buffer_clear(PACKER_BUFFER_(pk)); /* to free rmem before GC */
|
|
284
|
+
|
|
285
|
+
#ifdef RB_GC_GUARD
|
|
286
|
+
/* This prevents compilers from optimizing out the `self` variable
|
|
287
|
+
* from stack. Otherwise GC free()s it. */
|
|
288
|
+
RB_GC_GUARD(self);
|
|
289
|
+
#endif
|
|
290
|
+
|
|
291
|
+
return retval;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
static VALUE MessagePack_dump_module_method(int argc, VALUE* argv, VALUE mod)
|
|
295
|
+
{
|
|
296
|
+
UNUSED(mod);
|
|
297
|
+
return MessagePack_pack(argc, argv);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
static VALUE MessagePack_pack_module_method(int argc, VALUE* argv, VALUE mod)
|
|
301
|
+
{
|
|
302
|
+
UNUSED(mod);
|
|
303
|
+
return MessagePack_pack(argc, argv);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
void MessagePack_Packer_module_init(VALUE mMessagePack)
|
|
307
|
+
{
|
|
308
|
+
s_to_msgpack = rb_intern("to_cbor");
|
|
309
|
+
s_write = rb_intern("write");
|
|
310
|
+
|
|
311
|
+
msgpack_packer_static_init();
|
|
312
|
+
|
|
313
|
+
cMessagePack_Packer = rb_define_class_under(mMessagePack, "Packer", rb_cObject);
|
|
314
|
+
|
|
315
|
+
rb_define_alloc_func(cMessagePack_Packer, Packer_alloc);
|
|
316
|
+
|
|
317
|
+
rb_define_method(cMessagePack_Packer, "initialize", Packer_initialize, -1);
|
|
318
|
+
rb_define_method(cMessagePack_Packer, "buffer", Packer_buffer, 0);
|
|
319
|
+
rb_define_method(cMessagePack_Packer, "write", Packer_write, 1);
|
|
320
|
+
rb_define_alias(cMessagePack_Packer, "pack", "write");
|
|
321
|
+
rb_define_method(cMessagePack_Packer, "write_nil", Packer_write_nil, 0);
|
|
322
|
+
rb_define_method(cMessagePack_Packer, "write_array_header", Packer_write_array_header, 1);
|
|
323
|
+
rb_define_method(cMessagePack_Packer, "write_map_header", Packer_write_map_header, 1);
|
|
324
|
+
rb_define_method(cMessagePack_Packer, "flush", Packer_flush, 0);
|
|
325
|
+
|
|
326
|
+
/* delegation methods */
|
|
327
|
+
rb_define_method(cMessagePack_Packer, "clear", Packer_clear, 0);
|
|
328
|
+
rb_define_method(cMessagePack_Packer, "size", Packer_size, 0);
|
|
329
|
+
rb_define_method(cMessagePack_Packer, "empty?", Packer_empty_p, 0);
|
|
330
|
+
rb_define_method(cMessagePack_Packer, "write_to", Packer_write_to, 1);
|
|
331
|
+
rb_define_method(cMessagePack_Packer, "to_str", Packer_to_str, 0);
|
|
332
|
+
rb_define_alias(cMessagePack_Packer, "to_s", "to_str");
|
|
333
|
+
rb_define_method(cMessagePack_Packer, "to_a", Packer_to_a, 0);
|
|
334
|
+
//rb_define_method(cMessagePack_Packer, "append", Packer_append, 1);
|
|
335
|
+
//rb_define_alias(cMessagePack_Packer, "<<", "append");
|
|
336
|
+
|
|
337
|
+
//s_packer_value = Packer_alloc(cMessagePack_Packer);
|
|
338
|
+
//rb_gc_register_address(&s_packer_value);
|
|
339
|
+
//Data_Get_Struct(s_packer_value, msgpack_packer_t, s_packer);
|
|
340
|
+
|
|
341
|
+
/* MessagePack.pack(x) */
|
|
342
|
+
rb_define_module_function(mMessagePack, "pack", MessagePack_pack_module_method, -1);
|
|
343
|
+
rb_define_module_function(mMessagePack, "encode", MessagePack_pack_module_method, -1);
|
|
344
|
+
rb_define_module_function(mMessagePack, "dump", MessagePack_dump_module_method, -1);
|
|
345
|
+
}
|