vox-etf 0.1.2 → 0.1.7

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
2
  SHA256:
3
- metadata.gz: cd3946973c5609f6a44d6f1bc0ea10d3f5ed7095dd0009ddaaa64593c90de538
4
- data.tar.gz: 6e50761784880810734201a0d6e4684c2def7b6f4e7de0464ac8e42491768a12
3
+ metadata.gz: 5a6a2cc3e5abff452aed1b87b83f3c9906fa673cdac9251b92051492d7de0ff2
4
+ data.tar.gz: 2caddaa1b5163345f676d9649c624e9ec2580e770d32dddecd2325800e9845d0
5
5
  SHA512:
6
- metadata.gz: 111a3cc908087690d58e0ac939c3b3d1576d05bfc50fdb457e55eaa0ee54b71f1ae29bfb804e234c98bae047c8809803f5268ab1fddb5808cfff81ed5a492007
7
- data.tar.gz: c176eb3b67e1d83d6972eb50e5263f57286a37c1093b47ad3c92f3513f4e1c2902ec4b218dce4e813f004cd4e6d8e8917cdef223f40d1650db4e115c78a85fb6
6
+ metadata.gz: 56ff3b112058ec393869a0532905bad5f27adcd533a7427cacea86ffdbae49b261ca4a258dc33683af52bf0bce0c44fb6554a08b11d99a6c2a1c3862deb1ae8c
7
+ data.tar.gz: 96482eb0de9485c5938724b8d06d136028907ec99270a0bf46239d33b6313b1ad7207df021206099c11528f3dcbde8faeabdf0ba18f4cb007dad9061dc01f21c
@@ -4,6 +4,33 @@
4
4
  #include "erlpack/sysdep.h"
5
5
  #include "erlpack/constants.h"
6
6
 
7
+ /* This code is highly derivative of discord's erlpack decoder
8
+ * targeting Javascript.
9
+ *
10
+ *
11
+ * MIT License
12
+ *
13
+ * Copyright (c) 2017 Discord
14
+ *
15
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
16
+ * of this software and associated documentation files (the "Software"), to deal
17
+ * in the Software without restriction, including without limitation the rights
18
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
+ * copies of the Software, and to permit persons to whom the Software is
20
+ * furnished to do so, subject to the following conditions:
21
+ *
22
+ * The above copyright notice and this permission notice shall be included in all
23
+ * copies or substantial portions of the Software.
24
+ *
25
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
+ * SOFTWARE.
32
+ */
33
+
7
34
  namespace etf
8
35
  {
9
36
  class decoder
@@ -131,12 +158,12 @@ namespace etf
131
158
  return val;
132
159
  }
133
160
 
134
- VALUE decode_small_integer(void)
161
+ VALUE decode_small_integer()
135
162
  {
136
163
  return INT2FIX(read8());
137
164
  }
138
165
 
139
- VALUE decode_integer(void)
166
+ VALUE decode_integer()
140
167
  {
141
168
  return INT2NUM(read32());
142
169
  }
@@ -335,9 +362,9 @@ namespace etf
335
362
  {
336
363
  const uint32_t decompressed_size = read32();
337
364
 
338
- uint64_t source_size = decompressed_size;
365
+ unsigned long source_size = decompressed_size;
339
366
  uint8_t *out_buffer = (uint8_t *)malloc(decompressed_size);
340
- const int ret = uncompress(out_buffer, &source_size, (const unsigned char *)(data + offset), (uint64_t)(size - offset));
367
+ const int ret = uncompress(out_buffer, &source_size, (const unsigned char *)(data + offset), (uLong)(size - offset));
341
368
 
342
369
  offset += source_size;
343
370
  if (ret != Z_OK)
@@ -1,6 +1,7 @@
1
1
  #include "erlpack/encoder.h"
2
2
  #include "erlpack/constants.h"
3
3
  #include "etf.hpp"
4
+ #include "ruby.h"
4
5
 
5
6
  namespace etf
6
7
  {
@@ -57,13 +58,23 @@ namespace etf
57
58
  encode_hash(input);
58
59
  break;
59
60
  default:
60
- rb_raise(rb_eArgError, "Unsupported serialization type");
61
+ if (rb_respond_to(input, rb_intern("to_hash")))
62
+ {
63
+ VALUE hash = rb_funcall(input, rb_intern("to_hash"), 0);
64
+ Check_Type(hash, T_HASH);
65
+ encode_hash(hash);
66
+ }
67
+ else
68
+ {
69
+ rb_raise(rb_eArgError, "Unsupported serialization type");
70
+ }
71
+
61
72
  break;
62
73
  }
63
74
  }
64
75
 
65
76
  VALUE
66
- r_string(void)
77
+ r_string()
67
78
  {
68
79
  return rb_str_new(erl_buff->buf, erl_buff->length);
69
80
  }
@@ -88,11 +99,11 @@ namespace etf
88
99
 
89
100
  void encode_fixnum(VALUE fixnum)
90
101
  {
91
- long l = FIX2LONG(fixnum);
92
- if (l > 0 && l <= 0xFF)
93
- erlpack_append_small_integer(erl_buff, (unsigned char)l);
102
+ uint32_t n = NUM2UINT(fixnum);
103
+ if (n > 0 && n <= UINT8_MAX)
104
+ erlpack_append_small_integer(erl_buff, (uint8_t)n);
94
105
  else
95
- erlpack_append_integer(erl_buff, l);
106
+ erlpack_append_integer(erl_buff, n);
96
107
  }
97
108
 
98
109
  void encode_bignum(VALUE bignum)
@@ -127,18 +138,30 @@ namespace etf
127
138
 
128
139
  void encode_array(VALUE array)
129
140
  {
130
- erlpack_append_list_header(erl_buff, RARRAY_LEN(array));
131
- uint32_t size = RARRAY_LEN(array);
132
- for (uint32_t index = 0; index < size; index++)
141
+ uint64_t size = RARRAY_LEN(array);
142
+ if (size == 0)
143
+ {
144
+ erlpack_append_nil_ext(erl_buff);
145
+ return;
146
+ }
147
+ else if (size > UINT32_MAX)
148
+ {
149
+ rb_raise(rb_eRangeError, "Array size is too large to fit into a 32 bit integer.");
150
+ return;
151
+ }
152
+
153
+ erlpack_append_list_header(erl_buff, size);
154
+ for (size_t index = 0; index < size; index++)
133
155
  {
134
156
  encode_object(RARRAY_AREF(array, index));
135
157
  }
158
+
159
+ erlpack_append_nil_ext(erl_buff);
136
160
  }
137
161
 
138
162
  void encode_symbol(VALUE symbol)
139
163
  {
140
- VALUE str = rb_sym2str(symbol);
141
- erlpack_append_atom_utf8(erl_buff, RSTRING_PTR(str), RSTRING_LEN(str));
164
+ encode_string(rb_sym2str(symbol));
142
165
  }
143
166
 
144
167
  void encode_string(VALUE string)
@@ -148,13 +171,20 @@ namespace etf
148
171
 
149
172
  void encode_hash(VALUE hash)
150
173
  {
151
- uint32_t size = RHASH_SIZE(hash);
174
+ uint64_t size = RHASH_SIZE(hash);
175
+ if (size > UINT32_MAX)
176
+ {
177
+ rb_raise(rb_eRangeError, "Hash size is too large to fit into a 32 bit integer");
178
+ return;
179
+ }
180
+
152
181
  erlpack_append_map_header(erl_buff, size);
153
182
  VALUE keys = rb_funcall(hash, rb_intern("keys"), 0);
154
183
 
155
184
  for (uint32_t index = 0; index < size * 2; index += 2)
156
185
  {
157
186
  VALUE key = RARRAY_AREF(keys, index / 2);
187
+ // Normalize keys to strings because discord
158
188
  encode_object(key);
159
189
  encode_object(rb_hash_aref(hash, key));
160
190
  }
@@ -3,10 +3,6 @@
3
3
  #include "decoder.hpp"
4
4
  #include "etf.hpp"
5
5
 
6
- // Decode an ETF term from a ruby string
7
- // @param self [Object] Vox::ETF
8
- // @param input [Rice::String] The ETF term to be decoded.
9
- // @return [Rice::Object] Whatever the term is, as a ruby object.
10
6
  VALUE decode(VALUE self, VALUE input)
11
7
  {
12
8
  Check_Type(input, T_STRING);
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require('mkmf-rice')
3
+ require('mkmf')
4
4
 
5
5
  find_header('ruby.h')
6
6
  find_header('zlib.h')
@@ -1,7 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Container for Vox components.
3
4
  module Vox
5
+ # Default ETF adapter for vox's gateway component.
4
6
  module ETF
5
- VERSION = '0.1.2'
7
+ # @!parse [ruby]
8
+ # # Encode an object to an ETF term. This method accepts, `Integer`, `Float`,
9
+ # # `String`, `Symbol`, `Hash`, `Array`, `nil`, `true`, and `false` objects.
10
+ # # It also allows any object that responds to `#to_hash => Hash`.
11
+ # # @param input [Object, #to_hash] The object to be encoded as an ETF term.
12
+ # # @return [String] The ETF term encoded as a packed string.
13
+ # def self.encode(input)
14
+ # end
15
+
16
+ # @!parse [ruby]
17
+ # # Decode an ETF term from a string.
18
+ # # @param input [String] The ETF term to be decoded.
19
+ # # @return [Object] The ETF term decoded to an object.
20
+ # def self.decode(input)
21
+ # end
22
+
23
+ # Gem version
24
+ VERSION = '0.1.7'
6
25
  end
7
26
  end
@@ -30,7 +30,6 @@ Gem::Specification.new do |spec|
30
30
  spec.require_paths = ['lib']
31
31
  spec.extensions << 'ext/vox/extconf.rb'
32
32
 
33
- spec.add_dependency 'rice', '~> 2.2.0'
34
33
  spec.add_development_dependency 'rake', '~> 12.3.3'
35
34
  spec.add_development_dependency 'rake-compiler', '~> 1.1.1'
36
35
  spec.add_development_dependency 'rspec', '~> 3.9.0'
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vox-etf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Carey
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-11 00:00:00.000000000 Z
11
+ date: 2020-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: rice
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 2.2.0
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: 2.2.0
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rake
29
15
  requirement: !ruby/object:Gem::Requirement