mochilo 2.0 → 2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +1 -0
- data/Gemfile +4 -0
- data/docs/format-spec.md +50 -0
- data/ext/mochilo/mochilo.h +7 -0
- data/ext/mochilo/mochilo_api.h +26 -0
- data/ext/mochilo/mochilo_pack.c +91 -18
- data/ext/mochilo/mochilo_unpack.c +74 -0
- data/lib/mochilo/version.rb +1 -1
- data/mochilo.gemspec +1 -2
- data/script/bootstrap +2 -1
- data/test/pack_test.rb +26 -3
- data/test/setup.rb +4 -0
- data/test/unpack_test.rb +37 -0
- metadata +9 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: df296f6ca5b95ba44fc8491a1737be6f19c058f27425487541f38db4a5336fd5
|
4
|
+
data.tar.gz: 1850f2005b819cb30c012ecf60fc014539878ddf35eb04ac0a59e1cb5258f2ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bafe3c03a82ea87299b27797f5ebba3fc0f1f6296c07a2f679b96d315512ff4f745d40398258cc68ada959a82ff3f99c922d6790ebde635d022a9bb521194f7b
|
7
|
+
data.tar.gz: 3435b0be8504e8901eefb423490d2d98c448e6d812faf0fd5b8dc002c858831f71c69896ca2cf5e7576461ff6feed56efab0c6484c53d660f7b648aad2bfe82e
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/docs/format-spec.md
CHANGED
@@ -39,3 +39,53 @@ For serializing text up to (2^32)-1 bytes.
|
|
39
39
|
=> ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ - a big-endian 32-bit unsigned integer which represents the legth of data.
|
40
40
|
=> type - encoding flag, a signed 8-bit signed integer
|
41
41
|
```
|
42
|
+
|
43
|
+
#### Extensions
|
44
|
+
|
45
|
+
If the encoding flag is `0xff`, the data is not a string. The first byte of the data field defines the type, and the rest of the data field is defined per type.
|
46
|
+
The length of the enclosing ENCx type is the combined length of ext type and ext data.
|
47
|
+
|
48
|
+
```
|
49
|
+
----+-----------+----------+==========+
|
50
|
+
... | type=0xff | ext type | ext data |
|
51
|
+
----+-----------+----------+==========+
|
52
|
+
```
|
53
|
+
|
54
|
+
* Symbol, ext type = 0
|
55
|
+
* Regexp, ext type = 1
|
56
|
+
* Time, ext type = 2
|
57
|
+
|
58
|
+
##### Symbol
|
59
|
+
|
60
|
+
Symbols are encoded as the symbol's name, without a NUL terminator.
|
61
|
+
|
62
|
+
```
|
63
|
+
----+-----------+----------+=============+
|
64
|
+
... | type=0xff | 0x00 | symbol name |
|
65
|
+
----+-----------+----------+=============+
|
66
|
+
```
|
67
|
+
|
68
|
+
##### Regexp
|
69
|
+
|
70
|
+
Regexps are encoded as 32-bit integer from ruby's `Regexp#options`, an 8-bit encoding type, and the text of the regexp.
|
71
|
+
|
72
|
+
```
|
73
|
+
----+-----------+----------+--------+--------+--------+--------+--------+======+
|
74
|
+
... | type=0xff | 0x01 |XXXXXXXX|XXXXXXXX|XXXXXXXX|XXXXXXXX|YYYYYYYY| data |
|
75
|
+
----+-----------+----------+--------+--------+--------+--------+--------+======+
|
76
|
+
XXXXXXXX_XXXXXXXX_XXXXXXXX_XXXXXXXX 32-bit unsigned options
|
77
|
+
YYYYYYYY encoding flag
|
78
|
+
```
|
79
|
+
|
80
|
+
##### Time
|
81
|
+
|
82
|
+
Time values are encoded as a 64-bit unsigned number of seconds since the epoch, a 64-bit unsigned number of usec, and a 32-bit signed seconds of offset from UTC.
|
83
|
+
|
84
|
+
```
|
85
|
+
----+-----------+----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
|
86
|
+
... | type=0xff | 0x02 |XXXXXXXX|XXXXXXXX|XXXXXXXX|XXXXXXXX|XXXXXXXX|XXXXXXXX|XXXXXXXX|XXXXXXXX|YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|
|
87
|
+
----+-----------+----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
|
88
|
+
XXXXXXXX... whole seconds since epoch
|
89
|
+
YYYYYYYY... usec fraction to add to X
|
90
|
+
ZZZZZZZZ... (signed) seconds offset from UTC
|
91
|
+
```
|
data/ext/mochilo/mochilo.h
CHANGED
@@ -149,6 +149,13 @@ enum msgpack_err_t {
|
|
149
149
|
MSGPACK_ENOTHING = -3,
|
150
150
|
};
|
151
151
|
|
152
|
+
#define MOCHILO_EXT_TYPE 0xff
|
153
|
+
enum mochilo_ext_types_t {
|
154
|
+
MOCHILO_T_SYMBOL,
|
155
|
+
MOCHILO_T_REGEXP,
|
156
|
+
MOCHILO_T_TIME,
|
157
|
+
};
|
158
|
+
|
152
159
|
typedef void * mo_value;
|
153
160
|
typedef uint64_t mo_integer;
|
154
161
|
int mochilo_unpack_one(mo_value *_value, mochilo_src *src);
|
data/ext/mochilo/mochilo_api.h
CHANGED
@@ -4,6 +4,32 @@ MOAPI mo_value moapi_bytes_new(const char *src, size_t len)
|
|
4
4
|
return (mo_value)rb_str_new(src, len);
|
5
5
|
}
|
6
6
|
|
7
|
+
MOAPI mo_value moapi_symbol_new(const char *src, size_t len)
|
8
|
+
{
|
9
|
+
return ID2SYM(rb_intern2(src, len));
|
10
|
+
}
|
11
|
+
|
12
|
+
MOAPI mo_value moapi_regexp_new(const char *src, size_t len, enum msgpack_enc_t encoding, int reg_options)
|
13
|
+
{
|
14
|
+
int index = 0;
|
15
|
+
VALUE re;
|
16
|
+
|
17
|
+
if (encoding < sizeof(mochilo_enc_lookup)/sizeof(mochilo_enc_lookup[0]))
|
18
|
+
index = rb_enc_find_index(mochilo_enc_lookup[encoding]);
|
19
|
+
|
20
|
+
re = rb_reg_new(src, len, reg_options);
|
21
|
+
rb_enc_set_index(re, index);
|
22
|
+
|
23
|
+
return (mo_value)re;
|
24
|
+
}
|
25
|
+
|
26
|
+
MOAPI mo_value moapi_time_new(uint64_t sec, uint64_t usec, int32_t utc_offset)
|
27
|
+
{
|
28
|
+
VALUE utc_time = rb_time_new(sec, usec);
|
29
|
+
return (mo_value)rb_funcall(utc_time,
|
30
|
+
rb_intern("getlocal"), 1, INT2FIX(utc_offset));
|
31
|
+
}
|
32
|
+
|
7
33
|
MOAPI mo_value moapi_str_new(const char *src, size_t len, enum msgpack_enc_t encoding)
|
8
34
|
{
|
9
35
|
int index = 0;
|
data/ext/mochilo/mochilo_pack.c
CHANGED
@@ -9,6 +9,26 @@ extern VALUE rb_eMochiloPackError;
|
|
9
9
|
|
10
10
|
void mochilo_pack_one(mochilo_buf *buf, VALUE rb_object);
|
11
11
|
|
12
|
+
static void mochilo_buf_put_ext_size(mochilo_buf *buf, long size)
|
13
|
+
{
|
14
|
+
if (size < 0x100) {
|
15
|
+
uint8_t lead = size;
|
16
|
+
mochilo_buf_putc(buf, MSGPACK_T_ENC8);
|
17
|
+
mochilo_buf_putc(buf, lead);
|
18
|
+
} else if (size < 0x10000) {
|
19
|
+
uint16_t lead = size;
|
20
|
+
mochilo_buf_putc(buf, MSGPACK_T_ENC16);
|
21
|
+
mochilo_buf_put16be(buf, &lead);
|
22
|
+
} else if (size < 0x100000000) {
|
23
|
+
mochilo_buf_putc(buf, MSGPACK_T_ENC32);
|
24
|
+
mochilo_buf_put32be(buf, &size);
|
25
|
+
} else {
|
26
|
+
// there is no ext 64
|
27
|
+
rb_raise(rb_eMochiloPackError,
|
28
|
+
"String cannot be larger than %ld bytes", 0x100000000);
|
29
|
+
}
|
30
|
+
}
|
31
|
+
|
12
32
|
void mochilo_pack_fixnum(mochilo_buf *buf, VALUE rb_fixnum)
|
13
33
|
{
|
14
34
|
long fixnum = NUM2LONG(rb_fixnum);
|
@@ -73,6 +93,63 @@ void mochilo_pack_bignum(mochilo_buf *buf, VALUE rb_bignum)
|
|
73
93
|
}
|
74
94
|
}
|
75
95
|
|
96
|
+
static void mochilo_put_ext_size_and_type(mochilo_buf *buf, size_t size, enum mochilo_ext_types_t type)
|
97
|
+
{
|
98
|
+
mochilo_buf_put_ext_size(buf, size + 1);
|
99
|
+
mochilo_buf_putc(buf, MOCHILO_EXT_TYPE);
|
100
|
+
mochilo_buf_putc(buf, type);
|
101
|
+
}
|
102
|
+
|
103
|
+
void mochilo_pack_symbol(mochilo_buf *buf, VALUE rb_symbol)
|
104
|
+
{
|
105
|
+
char *symbol_name;
|
106
|
+
size_t size;
|
107
|
+
|
108
|
+
symbol_name = rb_id2name(SYM2ID(rb_symbol));
|
109
|
+
size = strlen(symbol_name);
|
110
|
+
|
111
|
+
mochilo_put_ext_size_and_type(buf, size, MOCHILO_T_SYMBOL);
|
112
|
+
mochilo_buf_put(buf, symbol_name, size);
|
113
|
+
}
|
114
|
+
|
115
|
+
void mochilo_pack_regexp(mochilo_buf *buf, VALUE rb_regexp)
|
116
|
+
{
|
117
|
+
uint32_t options;
|
118
|
+
size_t size;
|
119
|
+
rb_encoding *encoding;
|
120
|
+
char *enc_name;
|
121
|
+
const struct mochilo_enc_map *enc2id;
|
122
|
+
|
123
|
+
encoding = rb_enc_get(rb_regexp);
|
124
|
+
enc_name = rb_enc_name(encoding);
|
125
|
+
enc2id = mochilo_encoding_to_id(enc_name, (unsigned int)strlen(enc_name));
|
126
|
+
|
127
|
+
options = rb_reg_options(rb_regexp);
|
128
|
+
size = RREGEXP_SRC_LEN(rb_regexp);
|
129
|
+
|
130
|
+
mochilo_put_ext_size_and_type(buf, 4 + 1 + size, MOCHILO_T_REGEXP);
|
131
|
+
mochilo_buf_put32be(buf, &options);
|
132
|
+
mochilo_buf_putc(buf, enc2id ? enc2id->id : 0);
|
133
|
+
mochilo_buf_put(buf, RREGEXP_SRC_PTR(rb_regexp), size);
|
134
|
+
}
|
135
|
+
|
136
|
+
void mochilo_pack_time(mochilo_buf *buf, VALUE rb_time)
|
137
|
+
{
|
138
|
+
struct time_object *tobj;
|
139
|
+
uint64_t sec;
|
140
|
+
uint64_t usec;
|
141
|
+
int32_t utc_offset;
|
142
|
+
|
143
|
+
sec = NUM2ULONG(rb_funcall(rb_time, rb_intern("to_i"), 0));
|
144
|
+
usec = NUM2ULONG(rb_funcall(rb_time, rb_intern("usec"), 0));
|
145
|
+
utc_offset = NUM2INT(rb_funcall(rb_time, rb_intern("utc_offset"), 0));
|
146
|
+
|
147
|
+
mochilo_put_ext_size_and_type(buf, 8+8+4, MOCHILO_T_TIME);
|
148
|
+
mochilo_buf_put64be(buf, &sec);
|
149
|
+
mochilo_buf_put64be(buf, &usec);
|
150
|
+
mochilo_buf_put32be(buf, &utc_offset);
|
151
|
+
}
|
152
|
+
|
76
153
|
struct mochilo_hash_pack {
|
77
154
|
mochilo_buf *buf;
|
78
155
|
};
|
@@ -176,23 +253,7 @@ void mochilo_pack_str(mochilo_buf *buf, VALUE rb_str)
|
|
176
253
|
}
|
177
254
|
} else {
|
178
255
|
// if another encoding is used we need to use our custom types
|
179
|
-
|
180
|
-
uint8_t lead = size;
|
181
|
-
mochilo_buf_putc(buf, MSGPACK_T_ENC8);
|
182
|
-
mochilo_buf_putc(buf, lead);
|
183
|
-
} else if (size < 0x10000) {
|
184
|
-
uint16_t lead = size;
|
185
|
-
mochilo_buf_putc(buf, MSGPACK_T_ENC16);
|
186
|
-
mochilo_buf_put16be(buf, &lead);
|
187
|
-
} else if (size < 0x100000000) {
|
188
|
-
mochilo_buf_putc(buf, MSGPACK_T_ENC32);
|
189
|
-
mochilo_buf_put32be(buf, &size);
|
190
|
-
} else {
|
191
|
-
// there is no ext 64
|
192
|
-
rb_raise(rb_eMochiloPackError,
|
193
|
-
"String cannot be larger than %ld bytes", 0x100000000);
|
194
|
-
}
|
195
|
-
|
256
|
+
mochilo_buf_put_ext_size(buf, size);
|
196
257
|
enc2id = mochilo_encoding_to_id(enc_name, (unsigned int)strlen(enc_name));
|
197
258
|
mochilo_buf_putc(buf, enc2id ? enc2id->id : 0);
|
198
259
|
}
|
@@ -267,9 +328,21 @@ void mochilo_pack_one(mochilo_buf *buf, VALUE rb_object)
|
|
267
328
|
mochilo_pack_bignum(buf, rb_object);
|
268
329
|
return;
|
269
330
|
|
331
|
+
case T_SYMBOL:
|
332
|
+
mochilo_pack_symbol(buf, rb_object);
|
333
|
+
return;
|
334
|
+
|
335
|
+
case T_REGEXP:
|
336
|
+
mochilo_pack_regexp(buf, rb_object);
|
337
|
+
return;
|
338
|
+
|
270
339
|
default:
|
271
|
-
|
340
|
+
if (rb_cTime == rb_obj_class(rb_object)) {
|
341
|
+
mochilo_pack_time(buf, rb_object);
|
342
|
+
} else {
|
343
|
+
rb_raise(rb_eMochiloPackError,
|
272
344
|
"Unsupported object type: %s", rb_obj_classname(rb_object));
|
345
|
+
}
|
273
346
|
return;
|
274
347
|
}
|
275
348
|
}
|
@@ -6,6 +6,8 @@
|
|
6
6
|
#include "mochilo.h"
|
7
7
|
#include "mochilo_api.h"
|
8
8
|
|
9
|
+
extern VALUE rb_eMochiloUnpackError;
|
10
|
+
|
9
11
|
static inline int unpack_array(mo_value *_array, size_t elements, mochilo_src *buf)
|
10
12
|
{
|
11
13
|
size_t i;
|
@@ -57,6 +59,69 @@ static inline int unpack_hash(mo_value *_hash, size_t elements, mochilo_src *buf
|
|
57
59
|
return 0; \
|
58
60
|
}
|
59
61
|
|
62
|
+
static int mochilo_unpack_custom(mo_value *_value, mochilo_src *src, size_t length)
|
63
|
+
{
|
64
|
+
uint8_t custom_type;
|
65
|
+
|
66
|
+
SRC_ENSURE_AVAIL(src, length);
|
67
|
+
|
68
|
+
if (length < 1)
|
69
|
+
return -1;
|
70
|
+
length--;
|
71
|
+
mochilo_src_get8be(src, &custom_type);
|
72
|
+
|
73
|
+
switch (custom_type) {
|
74
|
+
case MOCHILO_T_SYMBOL:
|
75
|
+
{
|
76
|
+
const char *ptr;
|
77
|
+
if (!(ptr = mochilo_src_peek(src, length)))
|
78
|
+
return -1;
|
79
|
+
*_value = moapi_symbol_new(ptr, length);
|
80
|
+
return 0;
|
81
|
+
}
|
82
|
+
|
83
|
+
case MOCHILO_T_REGEXP:
|
84
|
+
{
|
85
|
+
uint32_t options;
|
86
|
+
uint8_t encoding;
|
87
|
+
const char *ptr;
|
88
|
+
|
89
|
+
if (length < 5)
|
90
|
+
return -1;
|
91
|
+
mochilo_src_get32be(src, &options);
|
92
|
+
mochilo_src_get8be(src, &encoding);
|
93
|
+
length -= 5;
|
94
|
+
|
95
|
+
if (!(ptr = mochilo_src_peek(src, length)))
|
96
|
+
return -1;
|
97
|
+
|
98
|
+
*_value = moapi_regexp_new(ptr, length, encoding, options);
|
99
|
+
return 0;
|
100
|
+
}
|
101
|
+
|
102
|
+
case MOCHILO_T_TIME:
|
103
|
+
{
|
104
|
+
uint64_t sec;
|
105
|
+
uint64_t usec;
|
106
|
+
int32_t utc_offset;
|
107
|
+
|
108
|
+
if (length != 8+8+4)
|
109
|
+
return -1;
|
110
|
+
|
111
|
+
mochilo_src_get64be(src, &sec);
|
112
|
+
mochilo_src_get64be(src, &usec);
|
113
|
+
mochilo_src_get32be(src, &utc_offset);
|
114
|
+
|
115
|
+
*_value = moapi_time_new(sec, usec, utc_offset);
|
116
|
+
return 0;
|
117
|
+
}
|
118
|
+
|
119
|
+
default:
|
120
|
+
rb_raise(rb_eMochiloUnpackError, "unknown custom type 0x%02x", custom_type);
|
121
|
+
return -1;
|
122
|
+
}
|
123
|
+
}
|
124
|
+
|
60
125
|
int mochilo_unpack_one(mo_value *_value, mochilo_src *src)
|
61
126
|
{
|
62
127
|
uint8_t leader;
|
@@ -214,6 +279,9 @@ int mochilo_unpack_one(mo_value *_value, mochilo_src *src)
|
|
214
279
|
mochilo_src_get8be(src, &length);
|
215
280
|
mochilo_src_get8be(src, &encoding);
|
216
281
|
|
282
|
+
if (encoding == MOCHILO_EXT_TYPE)
|
283
|
+
return mochilo_unpack_custom(_value, src, length);
|
284
|
+
|
217
285
|
if (!(ptr = mochilo_src_peek(src, length)))
|
218
286
|
return -1;
|
219
287
|
|
@@ -231,6 +299,9 @@ int mochilo_unpack_one(mo_value *_value, mochilo_src *src)
|
|
231
299
|
mochilo_src_get16be(src, &length);
|
232
300
|
mochilo_src_get8be(src, &encoding);
|
233
301
|
|
302
|
+
if (encoding == MOCHILO_EXT_TYPE)
|
303
|
+
return mochilo_unpack_custom(_value, src, length);
|
304
|
+
|
234
305
|
if (!(ptr = mochilo_src_peek(src, length)))
|
235
306
|
return -1;
|
236
307
|
|
@@ -248,6 +319,9 @@ int mochilo_unpack_one(mo_value *_value, mochilo_src *src)
|
|
248
319
|
mochilo_src_get32be(src, &length);
|
249
320
|
mochilo_src_get8be(src, &encoding);
|
250
321
|
|
322
|
+
if (encoding == MOCHILO_EXT_TYPE)
|
323
|
+
return mochilo_unpack_custom(_value, src, length);
|
324
|
+
|
251
325
|
if (!(ptr = mochilo_src_peek(src, length)))
|
252
326
|
return -1;
|
253
327
|
|
data/lib/mochilo/version.rb
CHANGED
data/mochilo.gemspec
CHANGED
@@ -15,10 +15,9 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.summary = %q{A ruby library for BananaPack}
|
16
16
|
s.test_files = `git ls-files spec`.split("\n")
|
17
17
|
s.required_ruby_version = ">= 1.9.3"
|
18
|
+
s.licenses = ["MIT"]
|
18
19
|
|
19
20
|
# tests
|
20
21
|
s.add_development_dependency 'rake-compiler', ">= 0.8.1"
|
21
22
|
s.add_development_dependency 'minitest', ">= 4.1.0"
|
22
|
-
# benchmarks
|
23
|
-
s.add_development_dependency 'msgpack'
|
24
23
|
end
|
data/script/bootstrap
CHANGED
@@ -12,5 +12,6 @@ cd "$(dirname "$0")/.."
|
|
12
12
|
if bundle check 1>/dev/null 2>&1; then
|
13
13
|
echo "Gem environment up-to-date"
|
14
14
|
else
|
15
|
-
|
15
|
+
gem update bundler
|
16
|
+
exec bundle install --binstubs --path vendor/gems --without benchmark "$@"
|
16
17
|
fi
|
data/test/pack_test.rb
CHANGED
@@ -276,9 +276,32 @@ class MochiloPackTest < Minitest::Test
|
|
276
276
|
end
|
277
277
|
end
|
278
278
|
|
279
|
-
def
|
280
|
-
|
281
|
-
|
279
|
+
def test_pack_symbol
|
280
|
+
expected = "\xC7\x07\xFF\x00symbol"
|
281
|
+
assert_equal expected, Mochilo.pack(:symbol)
|
282
|
+
assert_equal :symbol, Mochilo.unpack(expected)
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_pack_regexp
|
286
|
+
expected = "\xC7\x0D\xFF\x01\x00\x00\x00\x00\x01pa.tern"
|
287
|
+
assert_equal expected, Mochilo.pack(/pa.tern/)
|
288
|
+
[
|
289
|
+
/pa.tern/,
|
290
|
+
/thing/im,
|
291
|
+
].each do |re|
|
292
|
+
assert_equal re, Mochilo.unpack(Mochilo.pack(re))
|
282
293
|
end
|
283
294
|
end
|
295
|
+
|
296
|
+
def test_time
|
297
|
+
offset = -13*60*60 # I don't know if this is possible. There shouldn't be anything with a greater absolute value.
|
298
|
+
t = Time.gm(2042, 7, 21, 3, 32, 37, 974010).getlocal(offset)
|
299
|
+
expected = "\xC7\x15\xFF\x02" +
|
300
|
+
"\x00\x00\x00\x00\x88\x77\x66\x55" + # sec
|
301
|
+
"\x00\x00\x00\x00\x00\x0E\xDC\xBA" + # usec
|
302
|
+
"\xFF\xFF\x49\x30" # utc_offset
|
303
|
+
assert_equal expected, Mochilo.pack(t)
|
304
|
+
assert_equal t, Mochilo.unpack(expected)
|
305
|
+
assert_equal offset, Mochilo.unpack(expected).utc_offset
|
306
|
+
end
|
284
307
|
end
|
data/test/setup.rb
CHANGED
@@ -10,6 +10,10 @@ require 'bundler/setup'
|
|
10
10
|
# bring in minitest
|
11
11
|
require 'minitest/autorun'
|
12
12
|
|
13
|
+
if !defined?(MiniTest::Test)
|
14
|
+
MiniTest::Test = MiniTest::Unit::TestCase
|
15
|
+
end
|
16
|
+
|
13
17
|
# put lib and test dirs directly on load path
|
14
18
|
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
15
19
|
$LOAD_PATH.unshift File.expand_path('..', __FILE__)
|
data/test/unpack_test.rb
CHANGED
@@ -314,4 +314,41 @@ class MochiloUnpackTest < Minitest::Test
|
|
314
314
|
# arr.each {|i| data << (Mochilo.pack(i) + Mochilo.pack(i))}
|
315
315
|
# assert_equal hash, Mochilo.unpack(data)
|
316
316
|
end
|
317
|
+
|
318
|
+
def test_unpack_custom_types_with_errors
|
319
|
+
# regexp has all the options bits
|
320
|
+
re = Mochilo.unpack("\xC7\x0D\xFF\x01\xFF\xFF\xFF\xFF\x01pa.tern")
|
321
|
+
refute_equal 0, re.options, "unpacked regexp options"
|
322
|
+
|
323
|
+
# time is missing the last char
|
324
|
+
assert_raises Mochilo::UnpackError do
|
325
|
+
Mochilo.unpack("\xC7\x11\xFF\x02\x00\x00\x00\x00\x88\x77\x66\x55\x00\x00\x00\x00\x00\x0E\xDC")
|
326
|
+
end
|
327
|
+
|
328
|
+
# time overflows
|
329
|
+
assert_raises RangeError do
|
330
|
+
# set the top bit of the time.....v ...and of usec................v
|
331
|
+
Mochilo.unpack("\xC7\x15\xFF\x02\x80\x00\x00\x00\x88\x77\x66\x55\x80\x00\x00\x00\x00\x0E\xDC\xBA\xFF\xFF\xFF\xFF")
|
332
|
+
end
|
333
|
+
|
334
|
+
# unknown field
|
335
|
+
assert_raises Mochilo::UnpackError do
|
336
|
+
Mochilo.unpack("\xC7\x07\xFF\x10unknowncustomtype")
|
337
|
+
end
|
338
|
+
end
|
339
|
+
|
340
|
+
# Make sure that the custom parsers never read past the end
|
341
|
+
# of their buffers.
|
342
|
+
def test_unpack_custom_types_with_missing_char
|
343
|
+
assert_error_on_truncated_unpack "\xC7\x07\xFF\x00symbol"
|
344
|
+
assert_error_on_truncated_unpack "\xC7\x0D\xFF\x01\x00\x00\x00\x00\x01pa.tern"
|
345
|
+
assert_error_on_truncated_unpack "\xC7\x15\xFF\x02\x00\x00\x00\x00\x88\x77\x66\x55\x00\x00\x00\x00\x00\x0E\xDC\xBA\x00\x00\x00\x00"
|
346
|
+
end
|
347
|
+
|
348
|
+
def assert_error_on_truncated_unpack(packed)
|
349
|
+
refute_nil Mochilo.unpack(packed)
|
350
|
+
assert_raises Mochilo::UnpackError do
|
351
|
+
Mochilo.unpack(packed.chop)
|
352
|
+
end
|
353
|
+
end
|
317
354
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mochilo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '2.
|
4
|
+
version: '2.1'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vicent Martí
|
8
8
|
- Brian Lopez
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-10-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake-compiler
|
@@ -39,21 +39,7 @@ dependencies:
|
|
39
39
|
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: 4.1.0
|
42
|
-
|
43
|
-
name: msgpack
|
44
|
-
requirement: !ruby/object:Gem::Requirement
|
45
|
-
requirements:
|
46
|
-
- - ">="
|
47
|
-
- !ruby/object:Gem::Version
|
48
|
-
version: '0'
|
49
|
-
type: :development
|
50
|
-
prerelease: false
|
51
|
-
version_requirements: !ruby/object:Gem::Requirement
|
52
|
-
requirements:
|
53
|
-
- - ">="
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
version: '0'
|
56
|
-
description:
|
42
|
+
description:
|
57
43
|
email: vicent@github.com seniorlopez@gmail.com
|
58
44
|
executables: []
|
59
45
|
extensions:
|
@@ -92,9 +78,10 @@ files:
|
|
92
78
|
- test/setup.rb
|
93
79
|
- test/unpack_test.rb
|
94
80
|
homepage: http://github.com/brianmario/mochilo
|
95
|
-
licenses:
|
81
|
+
licenses:
|
82
|
+
- MIT
|
96
83
|
metadata: {}
|
97
|
-
post_install_message:
|
84
|
+
post_install_message:
|
98
85
|
rdoc_options:
|
99
86
|
- "--charset=UTF-8"
|
100
87
|
require_paths:
|
@@ -110,9 +97,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
97
|
- !ruby/object:Gem::Version
|
111
98
|
version: '0'
|
112
99
|
requirements: []
|
113
|
-
|
114
|
-
|
115
|
-
signing_key:
|
100
|
+
rubygems_version: 3.3.10
|
101
|
+
signing_key:
|
116
102
|
specification_version: 4
|
117
103
|
summary: A ruby library for BananaPack
|
118
104
|
test_files: []
|