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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 0b97afc4fd9e2920c58c61e96065c377681018aefe07c15cc072e59922157997
4
- data.tar.gz: ab5665e26e73884dd6e6faf2fe4df2ca1ca9ba421283f996d59c9b16dfa9509b
2
+ SHA1:
3
+ metadata.gz: c4c966c5decea51dbb2f53f1af2dc07a12daef8b
4
+ data.tar.gz: cbe013f48186036d3b4c3b4fcad9763bf9876139
5
5
  SHA512:
6
- metadata.gz: 05f5f210fcd4052a278ca9344f37ee1c00a9e9a966ac843843d6deeaacd61aac8d58fbf83886719e241f90d12d9cc852d3a5819b2f3cc10124c105d71d761e5e
7
- data.tar.gz: f129d81959c7173d1eb2fa82e14f7271c2cbb585d9f18728fe1fb05cbcaaf3b6fbc10a871139674427c5c252d631ed457d82ce9201039f7495aa6d5e021a9e27
6
+ metadata.gz: 09471508db65b21a62d5aaaa602667c087245fd8cb4cd4987d2e9402d15b5dd9baedc2dfdc664f76a13fc10f1dbf583d70c3e71a567953f3936915738147d026
7
+ data.tar.gz: 75df812168f9ba8abcfcb8e5326c8b466b6ce1b1bd480373debde2b8886f87dc01cdcc95209079b85c5fa59340a23acf3892ed8ec1e23b2e3ea666f1859fb42a
data/.travis.yml CHANGED
@@ -1,10 +1,7 @@
1
1
  language: ruby
2
2
  script:
3
3
  - bundle exec rake test
4
- install: ./script/bootstrap
5
4
  rvm:
6
- - 2.3.0
7
- - 2.2
8
- - 2.1
5
+ - 1.9.3
9
6
  - 2.0.0
10
- - ruby-head
7
+ - 2.1.2
data/Gemfile CHANGED
@@ -3,7 +3,3 @@ source "https://rubygems.org"
3
3
  gemspec
4
4
 
5
5
  gem "pry"
6
-
7
- group :benchmark do
8
- gem "msgpack"
9
- end
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mochilo (1.2.1)
4
+ mochilo (2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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 adds three new types to the protocol: Symbol and String in 16 and 32 bit lengths.
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 Symbol type is a String of text composed of ASCII characters with a maximum length of (2^8)-1 bytes.
6
+ The mapping of the `ext` types are:
6
7
 
7
- The String16 and String32 types are exactly the same as the [Raw16](http://wiki.msgpack.org/display/MSGPACK/Format+specification#Formatspecification-raw16) and [Raw32](http://wiki.msgpack.org/display/MSGPACK/Format+specification#Formatspecification-raw32) types from MessagePack except there is an encoding flag stored along with the bytes. This allows the format to differentiate binary from text.
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
- Check out docs/format-spec.md for more detailed information on the differences between BananaPack and MessagePack.
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: "value"}
16
- bpack = Mochilo.pack(obj)
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(bpack)
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
- ## String
1
+ ## Enc
2
2
 
3
- The String type is nearly identical to the Raw type from MessagePack but is specifically
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
- #### Symbol
7
+ #### Enc8
9
8
 
10
- For storing symbol names as ASCII text up to (2^8)-1 bytes.
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
- | 0xd4 |XXXXXXXX|...N bytes
16
- +--------+--------+----------
17
- => XXXXXXXX (=N) bytes of raw bytes.
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
- #### Regexp
19
+ #### Enc16
21
20
 
22
- Stores a regular expression, with options and encoding, up to (2^16)-1 bytes long.
21
+ For serializing text up to (2^16)-1 bytes.
23
22
 
24
23
  ```
25
- +--------+--------+--------+--------+--------+--------+--------+--------+----------
26
- | 0xd5 |XXXXXXXX|XXXXXXXX|YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY|ZZZZZZZZ|...N bytes
27
- +--------+--------+--------+--------+--------+--------+--------+--------+----------
28
- => XXXXXXXX_XXXXXXXX (=N) bytes of raw bytes.
29
- => YYYYYYYY_YYYYYYYY_YYYYYYYY_YYYYYYYY 32-bit int options
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
- #### Time
31
+ #### Enc32
34
32
 
35
- Stores a Ruby time, with usec resolution and UTC offset.
33
+ For serializing text up to (2^32)-1 bytes.
36
34
 
37
35
  ```
38
- +--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
39
- | 0xd6 |XXXXXXXX|XXXXXXXX|XXXXXXXX|XXXXXXXX|XXXXXXXX|XXXXXXXX|XXXXXXXX|XXXXXXXX|YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY|YYYYYYYY|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|
40
- +--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
41
- => XXXXXXXX_XXXXXXXX_XXXXXXXX_XXXXXXXX_XXXXXXXX_XXXXXXXX_XXXXXXXX_XXXXXXXX 64-bit unsigned seconds since the epoch
42
- => YYYYYYYY_YYYYYYYY_YYYYYYYY_YYYYYYYY_YYYYYYYY_YYYYYYYY_YYYYYYYY_YYYYYYYY 64-bit unsigned microseconds
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
  ```
@@ -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 unencode it after parsing. There's no need for hacks like this.
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 text and binary share a single type in the spec, "Raw". That means when deserializing the data you could be reading a contents of a Markdown file, or the contents of a JPEG. There are countless hacks to get around this but there shouldn't be a need for them either.
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 a couple of the "reserved" data type slots in the MessagePack spec to add a String16, String32 and Symbol type. String16 and String32 are exactly the same as the Raw16 and Raw32 types from the MessagePack spec except they have an encoding flag as well. This means that it's possible to serialize text data without hacks. This also means it's possible to serialize the contents of a JPEG without first needing to encode or escape it.
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
- rb_raise(rb_eNoMemError, "Failed to allocate new chunk");
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
- uint32_t i;
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
- uint32_t i;
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
- if ((buf->chunk_count * 2) < buf->chunk_count)
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 * 2 * sizeof(mochilo_buf_chunk));
107
- if (!chunks)
108
- rb_raise(rb_eNoMemError, "Failed to realloc chunks");
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
- uint32_t chunk_count, cur_chunk;
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)) \
@@ -14,30 +14,32 @@
14
14
  #define MOAPI static inline
15
15
 
16
16
  enum msgpack_t {
17
- MSGPACK_T_NIL = 0xc0,
18
- MSGPACK_T_FALSE = 0xc2,
19
- MSGPACK_T_TRUE = 0xc3,
20
- MSGPACK_T_FLOAT = 0xca,
21
- MSGPACK_T_DOUBLE = 0xcb,
22
- MSGPACK_T_UINT8 = 0xcc,
23
- MSGPACK_T_UINT16 = 0xcd,
24
- MSGPACK_T_UINT32 = 0xce,
25
- MSGPACK_T_UINT64 = 0xcf,
26
- MSGPACK_T_INT8 = 0xd0,
27
- MSGPACK_T_INT16 = 0xd1,
28
- MSGPACK_T_INT32 = 0xd2,
29
- MSGPACK_T_INT64 = 0xd3,
30
- MSGPACK_T_SYM = 0xd4,
31
- MSGPACK_T_REGEXP = 0xd5,
32
- MSGPACK_T_TIME = 0xd6,
33
- MSGPACK_T_STR16 = 0xd8, /* reserved in the spec */
34
- MSGPACK_T_STR32 = 0xd9, /* reserved in the spec */
35
- MSGPACK_T_RAW16 = 0xda,
36
- MSGPACK_T_RAW32 = 0xdb,
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 = 0xde,
40
- MSGPACK_T_MAP32 = 0xdf
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
- #ifdef HAVE_RUBY_ENCODING_H
156
- # include <ruby/encoding.h>
157
- # include "encodings.h"
158
- #endif
156
+ #include <ruby/encoding.h>
157
+ #include "encodings.h"
159
158
 
160
159
  #endif
@@ -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, int trusted);
26
+ extern void mochilo_pack_one(mochilo_buf *buf, VALUE rb_object);
27
27
 
28
- static VALUE rb_mochilo__unpack(VALUE rb_buffer, int trusted)
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
- rb_raise(rb_eMochiloUnpackError, "unpack failed (%d)", error);
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, 0);
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, 0);
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
-
@@ -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
  {