mochilo 1.3.0 → 2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +2 -5
- data/Gemfile +0 -4
- data/Gemfile.lock +1 -1
- data/LICENSE +21 -0
- data/README.md +12 -26
- data/docs/format-spec.md +23 -54
- data/docs/why-banana-pack.md +5 -3
- data/ext/mochilo/buffer.c +11 -14
- data/ext/mochilo/buffer.h +4 -5
- data/ext/mochilo/mochilo.h +26 -27
- data/ext/mochilo/mochilo.rb.c +15 -42
- data/ext/mochilo/mochilo_api.h +0 -39
- data/ext/mochilo/mochilo_pack.c +65 -130
- data/ext/mochilo/mochilo_unpack.c +51 -32
- data/lib/mochilo/version.rb +1 -1
- data/mochilo.gemspec +3 -1
- data/script/bootstrap +1 -1
- data/test/pack_test.rb +162 -80
- data/test/setup.rb +0 -9
- data/test/unpack_test.rb +206 -48
- metadata +24 -13
- data/MIT-LICENSE +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c4c966c5decea51dbb2f53f1af2dc07a12daef8b
|
4
|
+
data.tar.gz: cbe013f48186036d3b4c3b4fcad9763bf9876139
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 09471508db65b21a62d5aaaa602667c087245fd8cb4cd4987d2e9402d15b5dd9baedc2dfdc664f76a13fc10f1dbf583d70c3e71a567953f3936915738147d026
|
7
|
+
data.tar.gz: 75df812168f9ba8abcfcb8e5326c8b466b6ce1b1bd480373debde2b8886f87dc01cdcc95209079b85c5fa59340a23acf3892ed8ec1e23b2e3ea666f1859fb42a
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Brian Lopez
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -1,43 +1,30 @@
|
|
1
1
|
# Mochilo
|
2
2
|
|
3
|
-
Mochilo is a Ruby library implementing the BananaPack protocol. BananaPack is a superset of MessagePack. It
|
3
|
+
Mochilo is a Ruby library implementing the BananaPack protocol. BananaPack is a superset of MessagePack. It takes advantage of the new `ext` types to extend the protocol, adding 3 new
|
4
|
+
types which are used for serializing strings with an encoding other than UTF-8.
|
4
5
|
|
5
|
-
The
|
6
|
+
The mapping of the `ext` types are:
|
6
7
|
|
7
|
-
|
8
|
+
* `ext8` - A String in an encoding other than UTF-8 who's length is between 0 and (2^8)-1 bytes.
|
9
|
+
* `ext16` - A String in an encoding other than UTF-8 who's length is between 0 and (2^16)-1 bytes.
|
10
|
+
* `ext32` - A String in an encoding other than UTF-8 who's length is between 0 and (2^32)-1 bytes.
|
8
11
|
|
9
|
-
|
12
|
+
Also check out [docs/format-spec.md](docs/format-spec.md) for more detailed information on these types.
|
13
|
+
|
14
|
+
Strings tagged as `ASCII-8BIT` are encoded as the `bin` types in the MessagePack spec. The type that's used depends on the length of the string. Check out the spec [here](https://github.com/msgpack/msgpack/blob/master/spec.md) for more information on those types.
|
10
15
|
|
11
16
|
## Usage
|
12
17
|
|
13
18
|
``` ruby
|
14
19
|
require 'mochilo'
|
15
|
-
obj = {key
|
16
|
-
|
20
|
+
obj = {"key" => "value"}
|
21
|
+
packed = Mochilo.pack(obj)
|
17
22
|
#=> "\x81\xD8\x00\x03\x01key\xD8\x00\x05\x00value"
|
18
23
|
|
19
|
-
hash = Mochilo.unpack(
|
24
|
+
hash = Mochilo.unpack(packed)
|
20
25
|
#=> {"key"=>"value"}
|
21
26
|
```
|
22
27
|
|
23
|
-
Notice how `key` came back into Ruby as a String instead of a Symbol? This is because the `pack` method of Mochilo only generates "safe" bpack.
|
24
|
-
|
25
|
-
bpack without Symbols is considered "safe" for Ruby because in Ruby, Symbols aren't garbage collected. So if you unpack arbitrary bpack from an untrusted or malicious source, you could end up potentially exhausting the memory of your server.
|
26
|
-
|
27
|
-
To generate "unsafe" bpack, use `pack_unsafe` and `unpack_unsafe` methods instead:
|
28
|
-
|
29
|
-
``` ruby
|
30
|
-
require 'mochilo'
|
31
|
-
obj = {key: "value"}
|
32
|
-
bpack = Mochilo.pack_unsafe(obj)
|
33
|
-
#=> "\x81\xD4\x03key\xD8\x00\x05\x00value"
|
34
|
-
|
35
|
-
hash = Mochilo.unpack_unsafe(bpack)
|
36
|
-
#=> {:key=>"value"}
|
37
|
-
```
|
38
|
-
|
39
|
-
If you attempt to unpack "unsafe" bpack without using `unpack_unsafe`, an exception is raised.
|
40
|
-
|
41
28
|
## Supported Ruby Types
|
42
29
|
|
43
30
|
The following Ruby types are supported. Meaning they will be deserialized into the same Ruby type they were before serialization.
|
@@ -47,7 +34,6 @@ If any other object type is encountered during serialization, an exception is ra
|
|
47
34
|
* Fixnum
|
48
35
|
* Bignum
|
49
36
|
* Float
|
50
|
-
* Symbol
|
51
37
|
* String (with encoding)
|
52
38
|
* nil
|
53
39
|
* true
|
data/docs/format-spec.md
CHANGED
@@ -1,72 +1,41 @@
|
|
1
|
-
##
|
1
|
+
## Enc
|
2
2
|
|
3
|
-
The
|
4
|
-
aimed at marking a set of bytes as text.
|
3
|
+
The Enc type is nearly identical to the Str type from the MessagePack spec but has a flag for specifying an encoding other than UTF-8.
|
5
4
|
|
6
5
|
### Format Specification
|
7
6
|
|
8
|
-
####
|
7
|
+
#### Enc8
|
9
8
|
|
10
|
-
For
|
11
|
-
Length is stored in unsigned 8-bit integer.
|
9
|
+
For serializing text up to (2^8)-1 bytes.
|
12
10
|
|
13
11
|
```
|
14
|
-
|
15
|
-
|
|
16
|
-
|
17
|
-
=> XXXXXXXX
|
12
|
+
+--------+--------+--------+========+
|
13
|
+
| 0xc7 |XXXXXXXX| type | data |
|
14
|
+
+--------+--------+--------+========+
|
15
|
+
=> XXXXXXXX - a 8-bit unsigned integer which represents the legth of data.
|
16
|
+
=> type - encoding flag, a signed 8-bit signed integer
|
18
17
|
```
|
19
18
|
|
20
|
-
####
|
19
|
+
#### Enc16
|
21
20
|
|
22
|
-
|
21
|
+
For serializing text up to (2^16)-1 bytes.
|
23
22
|
|
24
23
|
```
|
25
|
-
|
26
|
-
|
|
27
|
-
|
28
|
-
=>
|
29
|
-
=>
|
30
|
-
=> ZZZZZZZZ encoding flag
|
24
|
+
+--------+--------+--------+--------+========+
|
25
|
+
| 0xc8 |YYYYYYYY|YYYYYYYY| type | data |
|
26
|
+
+--------+--------+--------+--------+========+
|
27
|
+
=> YYYYYYYY_YYYYYYYY - a 16-bit big-endian unsigned integer which represents the legth of data.
|
28
|
+
=> type - encoding flag, a signed 8-bit signed integer
|
31
29
|
```
|
32
30
|
|
33
|
-
####
|
31
|
+
#### Enc32
|
34
32
|
|
35
|
-
|
33
|
+
For serializing text up to (2^32)-1 bytes.
|
36
34
|
|
37
35
|
```
|
38
|
-
|
39
|
-
|
|
40
|
-
|
41
|
-
=>
|
42
|
-
=>
|
43
|
-
=> ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ 32-bit signed seconds offset from UTC
|
44
|
-
```
|
45
|
-
|
46
|
-
#### String16
|
47
|
-
|
48
|
-
For storing text up to (2^16)-1 bytes.
|
49
|
-
Length is stored in unsigned 16-bit big-endian integer.
|
50
|
-
Encoding is stored as an int8
|
51
|
-
|
52
|
-
```
|
53
|
-
+--------+--------+--------+--------+----------
|
54
|
-
| 0xd8 |XXXXXXXX|XXXXXXXX|YYYYYYYY|...N bytes
|
55
|
-
+--------+--------+--------+--------+----------
|
56
|
-
=> XXXXXXXX_XXXXXXXX (=N) bytes of raw bytes.
|
57
|
-
=> YYYYYYYY encoding flag
|
58
|
-
```
|
59
|
-
|
60
|
-
#### String32
|
61
|
-
|
62
|
-
For storing text up to (2^32)-1 bytes.
|
63
|
-
Length is stored in unsigned 32-bit big-endian integer.
|
64
|
-
Encoding is stored as an int8
|
65
|
-
|
66
|
-
```
|
67
|
-
+--------+--------+--------+--------+--------+--------+----------
|
68
|
-
| 0xd9 |XXXXXXXX|XXXXXXXX|XXXXXXXX|XXXXXXXX|YYYYYYYY|...N bytes
|
69
|
-
+--------+--------+--------+--------+--------+--------+----------
|
70
|
-
=> XXXXXXXX_XXXXXXXX_XXXXXXXX_XXXXXXXX (=N) bytes of raw bytes.
|
71
|
-
=> YYYYYYYY encoding flag
|
36
|
+
+--------+--------+--------+--------+--------+--------+========+
|
37
|
+
| 0xc9 |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ| type | data |
|
38
|
+
+--------+--------+--------+--------+--------+--------+========+
|
39
|
+
=> ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ - a big-endian 32-bit unsigned integer which represents the legth of data.
|
40
|
+
=> type - encoding flag, a signed 8-bit signed integer
|
72
41
|
```
|
data/docs/why-banana-pack.md
CHANGED
@@ -10,11 +10,13 @@ A couple of them that are comparable to BananaPack are MessagePack and JSON. To
|
|
10
10
|
|
11
11
|
JSON has the basic principles of a serialized format that we want. It's only capable of representing primitive data types in common with almost every language. That makes it great for cross-language communication.
|
12
12
|
|
13
|
-
The problem is that it's just text. Unicode text to be precise. That means you can't serialize arbitrary binary data without encoding it first. Common practice is to Base64 encode the data first. But then deserialization side *has* to know to
|
13
|
+
The problem is that it's just text. Unicode text to be precise. That means you can't serialize arbitrary binary data without encoding it first. Common practice is to Base64 encode the data first. But then deserialization side *has* to know to decode it after parsing. There's no need for hacks like this.
|
14
14
|
|
15
15
|
## Why not MessagePack?
|
16
16
|
|
17
|
-
MessagePack is nearly perfect for what we want. But
|
17
|
+
MessagePack is nearly perfect for what we want. But it can only differentiate between binary data
|
18
|
+
and text that is encoded as UTF-8. We need to support more encodings than that so we can serialize
|
19
|
+
it over the wire without the need to put it through potentially lossy transcoding.
|
18
20
|
|
19
21
|
## BananaPack
|
20
22
|
|
@@ -22,4 +24,4 @@ We wanted something compact and easy to parse. But it also needed to have native
|
|
22
24
|
|
23
25
|
Knowing what encoding text is in is just as important as knowing the timezone of a timestamp. This has been true since the birth of the Internet.
|
24
26
|
|
25
|
-
So we used
|
27
|
+
So we used the new MessagePack spec to add Enc8, Enc16 and Enc32 types. The encoding of the string is stored as a flag in the `type` field of the `ext` type.
|
data/ext/mochilo/buffer.c
CHANGED
@@ -19,7 +19,7 @@ static mochilo_buf_chunk *init_cur_chunk(mochilo_buf *buf, size_t chunk_size)
|
|
19
19
|
|
20
20
|
buf->last_alloc = chunk->ptr = malloc(chunk_size);
|
21
21
|
if (!chunk->ptr)
|
22
|
-
|
22
|
+
return NULL;
|
23
23
|
|
24
24
|
chunk->end = chunk->ptr + chunk_size;
|
25
25
|
return chunk;
|
@@ -38,7 +38,7 @@ static void skip_last_chunk(mochilo_buf *buf)
|
|
38
38
|
|
39
39
|
static void free_buf(mochilo_buf *buf)
|
40
40
|
{
|
41
|
-
|
41
|
+
uint16_t i;
|
42
42
|
|
43
43
|
for (i = 0; i < buf->cur_chunk; ++i)
|
44
44
|
free(buf->chunks[i].ptr);
|
@@ -61,7 +61,7 @@ VALUE mochilo_buf_flush(mochilo_buf *buf)
|
|
61
61
|
{
|
62
62
|
VALUE rb_str;
|
63
63
|
char *ptr;
|
64
|
-
|
64
|
+
uint16_t i;
|
65
65
|
|
66
66
|
skip_last_chunk(buf);
|
67
67
|
|
@@ -95,20 +95,15 @@ VALUE mochilo_buf_flush(mochilo_buf *buf)
|
|
95
95
|
|
96
96
|
mochilo_buf_chunk *mochilo_buf_rechunk2(mochilo_buf *buf, size_t chunk_size)
|
97
97
|
{
|
98
|
-
mochilo_buf_chunk *chunks;
|
99
|
-
|
100
98
|
skip_last_chunk(buf);
|
101
99
|
|
102
100
|
if (buf->cur_chunk == buf->chunk_count) {
|
103
|
-
|
104
|
-
rb_raise(rb_eArgError, "Too many chunks required to serialize");
|
101
|
+
buf->chunk_count *= 2;
|
105
102
|
|
106
|
-
chunks = realloc(buf->chunks, buf->chunk_count *
|
107
|
-
if (!chunks)
|
108
|
-
|
103
|
+
buf->chunks = realloc(buf->chunks, buf->chunk_count * sizeof(mochilo_buf_chunk));
|
104
|
+
if (!buf->chunks)
|
105
|
+
return NULL;
|
109
106
|
|
110
|
-
buf->chunks = chunks;
|
111
|
-
buf->chunk_count *= 2;
|
112
107
|
}
|
113
108
|
|
114
109
|
return init_cur_chunk(buf, chunk_size);
|
@@ -123,8 +118,10 @@ void mochilo_buf_put(mochilo_buf *buf, const char *data, size_t len)
|
|
123
118
|
{
|
124
119
|
mochilo_buf_chunk *chunk = &buf->chunks[buf->cur_chunk];
|
125
120
|
|
126
|
-
if (unlikely(chunk->ptr + len > chunk->end))
|
127
|
-
chunk = mochilo_buf_rechunk2(buf, len)
|
121
|
+
if (unlikely(chunk->ptr + len > chunk->end)) {
|
122
|
+
if (!(chunk = mochilo_buf_rechunk2(buf, len)))
|
123
|
+
return;
|
124
|
+
}
|
128
125
|
|
129
126
|
memmove(chunk->ptr, data, len);
|
130
127
|
chunk->ptr += len;
|
data/ext/mochilo/buffer.h
CHANGED
@@ -48,7 +48,7 @@ static inline void swap64(const uint8_t *buffer, void *out)
|
|
48
48
|
}
|
49
49
|
|
50
50
|
/**
|
51
|
-
* Buffer code
|
51
|
+
* Buffer code
|
52
52
|
*/
|
53
53
|
typedef struct {
|
54
54
|
char *ptr;
|
@@ -59,13 +59,12 @@ typedef struct {
|
|
59
59
|
mochilo_buf_chunk *chunks;
|
60
60
|
char *last_alloc;
|
61
61
|
size_t total_size;
|
62
|
-
|
62
|
+
uint16_t chunk_count, cur_chunk;
|
63
63
|
} mochilo_buf;
|
64
64
|
|
65
65
|
typedef struct {
|
66
66
|
const char *ptr;
|
67
67
|
const char *end;
|
68
|
-
int trusted;
|
69
68
|
} mochilo_src;
|
70
69
|
|
71
70
|
void mochilo_buf_init(mochilo_buf *buf);
|
@@ -81,9 +80,9 @@ const char *mochilo_src_peek(mochilo_src *buf, size_t need);
|
|
81
80
|
#define BUF_ENSURE_AVAIL(b, d) \
|
82
81
|
mochilo_buf_chunk *chunk = &b->chunks[b->cur_chunk]; \
|
83
82
|
if (unlikely(chunk->ptr + (d) > chunk->end)) { \
|
84
|
-
chunk = mochilo_buf_rechunk(b); };
|
83
|
+
if ((chunk = mochilo_buf_rechunk(b)) == NULL) return; };
|
85
84
|
|
86
|
-
#define SRC_CHECK_AVAIL(src, bytes) (src->ptr + bytes <= src->end)
|
85
|
+
#define SRC_CHECK_AVAIL(src, bytes) (src->ptr + bytes <= src->end)
|
87
86
|
|
88
87
|
#define SRC_ENSURE_AVAIL(src, bytes) \
|
89
88
|
if (unlikely(src->ptr + bytes > src->end)) \
|
data/ext/mochilo/mochilo.h
CHANGED
@@ -14,30 +14,32 @@
|
|
14
14
|
#define MOAPI static inline
|
15
15
|
|
16
16
|
enum msgpack_t {
|
17
|
-
MSGPACK_T_NIL
|
18
|
-
MSGPACK_T_FALSE
|
19
|
-
MSGPACK_T_TRUE
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
17
|
+
MSGPACK_T_NIL = 0xc0,
|
18
|
+
MSGPACK_T_FALSE = 0xc2,
|
19
|
+
MSGPACK_T_TRUE = 0xc3,
|
20
|
+
MSGPACK_T_BIN8 = 0xc4,
|
21
|
+
MSGPACK_T_BIN16 = 0xc5,
|
22
|
+
MSGPACK_T_BIN32 = 0xc6,
|
23
|
+
MSGPACK_T_ENC8 = 0xc7,
|
24
|
+
MSGPACK_T_ENC16 = 0xc8,
|
25
|
+
MSGPACK_T_ENC32 = 0xc9,
|
26
|
+
MSGPACK_T_FLOAT = 0xca,
|
27
|
+
MSGPACK_T_DOUBLE = 0xcb,
|
28
|
+
MSGPACK_T_UINT8 = 0xcc,
|
29
|
+
MSGPACK_T_UINT16 = 0xcd,
|
30
|
+
MSGPACK_T_UINT32 = 0xce,
|
31
|
+
MSGPACK_T_UINT64 = 0xcf,
|
32
|
+
MSGPACK_T_INT8 = 0xd0,
|
33
|
+
MSGPACK_T_INT16 = 0xd1,
|
34
|
+
MSGPACK_T_INT32 = 0xd2,
|
35
|
+
MSGPACK_T_INT64 = 0xd3,
|
36
|
+
MSGPACK_T_STR8 = 0xd9,
|
37
|
+
MSGPACK_T_STR16 = 0xda,
|
38
|
+
MSGPACK_T_STR32 = 0xdb,
|
37
39
|
MSGPACK_T_ARRAY16 = 0xdc,
|
38
40
|
MSGPACK_T_ARRAY32 = 0xdd,
|
39
|
-
MSGPACK_T_MAP16
|
40
|
-
MSGPACK_T_MAP32
|
41
|
+
MSGPACK_T_MAP16 = 0xde,
|
42
|
+
MSGPACK_T_MAP32 = 0xdf
|
41
43
|
};
|
42
44
|
|
43
45
|
enum msgpack_enc_t {
|
@@ -145,16 +147,13 @@ enum msgpack_err_t {
|
|
145
147
|
MSGPACK_EEOF = -1,
|
146
148
|
MSGPACK_EINVALID = -2,
|
147
149
|
MSGPACK_ENOTHING = -3,
|
148
|
-
MSGPACK_EUNSAFE = -4,
|
149
150
|
};
|
150
151
|
|
151
152
|
typedef void * mo_value;
|
152
153
|
typedef uint64_t mo_integer;
|
153
154
|
int mochilo_unpack_one(mo_value *_value, mochilo_src *src);
|
154
155
|
|
155
|
-
#
|
156
|
-
#
|
157
|
-
# include "encodings.h"
|
158
|
-
#endif
|
156
|
+
#include <ruby/encoding.h>
|
157
|
+
#include "encodings.h"
|
159
158
|
|
160
159
|
#endif
|
data/ext/mochilo/mochilo.rb.c
CHANGED
@@ -23,9 +23,9 @@ static VALUE rb_eMochiloError;
|
|
23
23
|
VALUE rb_eMochiloPackError;
|
24
24
|
VALUE rb_eMochiloUnpackError;
|
25
25
|
|
26
|
-
extern void mochilo_pack_one(mochilo_buf *buf, VALUE rb_object
|
26
|
+
extern void mochilo_pack_one(mochilo_buf *buf, VALUE rb_object);
|
27
27
|
|
28
|
-
static VALUE rb_mochilo__unpack(VALUE rb_buffer
|
28
|
+
static VALUE rb_mochilo__unpack(VALUE rb_buffer)
|
29
29
|
{
|
30
30
|
VALUE rb_result;
|
31
31
|
int error = -1;
|
@@ -35,11 +35,19 @@ static VALUE rb_mochilo__unpack(VALUE rb_buffer, int trusted)
|
|
35
35
|
|
36
36
|
source.ptr = RSTRING_PTR(rb_buffer);
|
37
37
|
source.end = source.ptr + RSTRING_LEN(rb_buffer);
|
38
|
-
source.trusted = trusted;
|
39
38
|
|
40
39
|
error = mochilo_unpack_one((mo_value)&rb_result, &source);
|
41
|
-
if (error < 0)
|
42
|
-
|
40
|
+
if (error < 0) {
|
41
|
+
switch (error) {
|
42
|
+
case MSGPACK_EEOF:
|
43
|
+
rb_raise(rb_eMochiloUnpackError, "more data needed");
|
44
|
+
case MSGPACK_EINVALID:
|
45
|
+
rb_raise(rb_eMochiloUnpackError, "invalid or corrupt data");
|
46
|
+
case MSGPACK_ENOTHING:
|
47
|
+
default:
|
48
|
+
rb_raise(rb_eMochiloUnpackError, "unpack failed");
|
49
|
+
}
|
50
|
+
}
|
43
51
|
|
44
52
|
return rb_result;
|
45
53
|
}
|
@@ -53,21 +61,7 @@ static VALUE rb_mochilo__unpack(VALUE rb_buffer, int trusted)
|
|
53
61
|
*/
|
54
62
|
static VALUE rb_mochilo_unpack(VALUE self, VALUE rb_buffer)
|
55
63
|
{
|
56
|
-
return rb_mochilo__unpack(rb_buffer
|
57
|
-
}
|
58
|
-
|
59
|
-
/* Document-method: unpack_unsafe
|
60
|
-
*
|
61
|
-
* call-seq:
|
62
|
-
* Mochilo.unpack_unsafe(banana_pack_str) -> Object
|
63
|
-
*
|
64
|
-
* Unpacks a BananaPack stream into a Ruby object, in unsafe mode.
|
65
|
-
* Only use this function if +banana_pack_str+ is trusted; otherwise
|
66
|
-
* symbol DoS attacks are possible.
|
67
|
-
*/
|
68
|
-
static VALUE rb_mochilo_unpack_unsafe(VALUE self, VALUE rb_buffer)
|
69
|
-
{
|
70
|
-
return rb_mochilo__unpack(rb_buffer, 1);
|
64
|
+
return rb_mochilo__unpack(rb_buffer);
|
71
65
|
}
|
72
66
|
|
73
67
|
/* Document-method: pack
|
@@ -82,25 +76,7 @@ static VALUE rb_mochilo_pack(VALUE self, VALUE rb_obj)
|
|
82
76
|
mochilo_buf buf;
|
83
77
|
|
84
78
|
mochilo_buf_init(&buf);
|
85
|
-
mochilo_pack_one(&buf, rb_obj
|
86
|
-
return mochilo_buf_flush(&buf);
|
87
|
-
}
|
88
|
-
|
89
|
-
/* Document-method: pack
|
90
|
-
*
|
91
|
-
* call-seq:
|
92
|
-
* Mochilo.pack_unsafe(obj) -> String
|
93
|
-
*
|
94
|
-
* Packs a Ruby object into BananaPack format, in unsafe mode.
|
95
|
-
* This enables the Symbol type durring serialization and will
|
96
|
-
* have to be deserialized in unsafe mode as well.
|
97
|
-
*/
|
98
|
-
static VALUE rb_mochilo_pack_unsafe(VALUE self, VALUE rb_obj)
|
99
|
-
{
|
100
|
-
mochilo_buf buf;
|
101
|
-
|
102
|
-
mochilo_buf_init(&buf);
|
103
|
-
mochilo_pack_one(&buf, rb_obj, 1);
|
79
|
+
mochilo_pack_one(&buf, rb_obj);
|
104
80
|
return mochilo_buf_flush(&buf);
|
105
81
|
}
|
106
82
|
|
@@ -108,12 +84,9 @@ void Init_mochilo()
|
|
108
84
|
{
|
109
85
|
rb_mMochilo = rb_define_module("Mochilo");
|
110
86
|
rb_define_method(rb_mMochilo, "unpack", rb_mochilo_unpack, 1);
|
111
|
-
rb_define_method(rb_mMochilo, "unpack_unsafe", rb_mochilo_unpack_unsafe, 1);
|
112
87
|
rb_define_method(rb_mMochilo, "pack", rb_mochilo_pack, 1);
|
113
|
-
rb_define_method(rb_mMochilo, "pack_unsafe", rb_mochilo_pack_unsafe, 1);
|
114
88
|
|
115
89
|
rb_eMochiloError = rb_define_class_under(rb_mMochilo, "Error", rb_eStandardError);
|
116
90
|
rb_eMochiloPackError = rb_define_class_under(rb_mMochilo, "PackError", rb_eMochiloError);
|
117
91
|
rb_eMochiloUnpackError = rb_define_class_under(rb_mMochilo, "UnpackError", rb_eMochiloError);
|
118
92
|
}
|
119
|
-
|
data/ext/mochilo/mochilo_api.h
CHANGED
@@ -4,44 +4,6 @@ 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_sym_new(const char *src, size_t len)
|
8
|
-
{
|
9
|
-
char *symbol;
|
10
|
-
|
11
|
-
if (len > 0xFF)
|
12
|
-
rb_raise(rb_eArgError, "Symbol too long to encode in BananaPack");
|
13
|
-
|
14
|
-
symbol = alloca(len + 1);
|
15
|
-
memcpy(symbol, src, len);
|
16
|
-
symbol[len] = '\0';
|
17
|
-
|
18
|
-
return (mo_value)ID2SYM(rb_intern(symbol));
|
19
|
-
}
|
20
|
-
|
21
|
-
#ifdef HAVE_RUBY_ENCODING_H
|
22
|
-
MOAPI mo_value moapi_regexp_new(const char *src, size_t len, enum msgpack_enc_t encoding, int reg_options)
|
23
|
-
{
|
24
|
-
int index = 0;
|
25
|
-
VALUE re;
|
26
|
-
|
27
|
-
if (encoding < sizeof(mochilo_enc_lookup)/sizeof(mochilo_enc_lookup[0]))
|
28
|
-
index = rb_enc_find_index(mochilo_enc_lookup[encoding]);
|
29
|
-
|
30
|
-
re = rb_reg_new(src, len, reg_options);
|
31
|
-
rb_enc_set_index(re, index);
|
32
|
-
|
33
|
-
return (mo_value)re;
|
34
|
-
}
|
35
|
-
#endif
|
36
|
-
|
37
|
-
MOAPI mo_value moapi_time_new(uint64_t sec, uint64_t usec, int32_t utc_offset)
|
38
|
-
{
|
39
|
-
VALUE utc_time = rb_time_new(sec, usec);
|
40
|
-
return (mo_value)rb_funcall(utc_time,
|
41
|
-
rb_intern("getlocal"), 1, INT2FIX(utc_offset));
|
42
|
-
}
|
43
|
-
|
44
|
-
#ifdef HAVE_RUBY_ENCODING_H
|
45
7
|
MOAPI mo_value moapi_str_new(const char *src, size_t len, enum msgpack_enc_t encoding)
|
46
8
|
{
|
47
9
|
int index = 0;
|
@@ -55,7 +17,6 @@ MOAPI mo_value moapi_str_new(const char *src, size_t len, enum msgpack_enc_t enc
|
|
55
17
|
|
56
18
|
return (mo_value)str;
|
57
19
|
}
|
58
|
-
#endif
|
59
20
|
|
60
21
|
MOAPI mo_value moapi_array_new(size_t array_size)
|
61
22
|
{
|