vox-etf 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 38c4afc3d3f48eb9687bfc79372892ee356c7e9f4d70211c71ddedf0b59d4318
4
+ data.tar.gz: 2f1b5060659fc9a35fa8d0b1ee1f5665c5c37d4254864961c14ead7d07048b9b
5
+ SHA512:
6
+ metadata.gz: 8fd31825a494ea4b863be1f3a5c252951598987cca09137f7eb8c71e62a01992011dfaff5c72f123845667ece12c96db6ab5663276d47f59b70d17cd3e9c2d06
7
+ data.tar.gz: de91811cddb61a4523cf66501c86893fe4e1c6dd16fd9ca07dd6984a06d64fce3bab2d8daa82050f67bb1b3ee8287fcd3cdc6282c23039f80d2a445bd8333e48
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,22 @@
1
+ require:
2
+ - rubocop-rspec
3
+ - rubocop-performance
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.5
7
+ Exclude:
8
+ - bin/*
9
+ - vendor/**/*
10
+ NewCops: enable
11
+
12
+ Style/FormatStringToken:
13
+ Enabled: false
14
+
15
+ Style/RaiseArgs:
16
+ Enabled: false
17
+
18
+ RSpec/FilePath:
19
+ SpecSuffixOnly: true
20
+
21
+ Metrics:
22
+ Enabled: false
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.3
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at matthew.b.carey@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source('https://rubygems.org')
4
+
5
+ # Specify your gem's dependencies in vox-etf.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Matthew Carey
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ # Vox::Etf
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/vox/etf`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'vox-etf'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install vox-etf
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/vox-etf. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/vox-etf/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Code of Conduct
43
+
44
+ Everyone interacting in the Vox::Etf project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/vox-etf/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require('bundler/gem_tasks')
4
+ require('rspec/core/rake_task')
5
+ require('rake/extensiontask')
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ Rake::ExtensionTask.new('etf') do |ext|
10
+ ext.ext_dir = 'ext/vox'
11
+ ext.lib_dir = 'lib/vox'
12
+ end
13
+
14
+ task(default: :compile)
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require('bundler/setup')
5
+ require('vox/etf')
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require('irb')
15
+ IRB.start(__FILE__)
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require('pathname')
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path(
13
+ '../../Gemfile',
14
+ Pathname.new(__FILE__).realpath
15
+ )
16
+
17
+ bundle_binstub = File.expand_path('bundle', __dir__)
18
+
19
+ if File.file?(bundle_binstub)
20
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
21
+ load(bundle_binstub)
22
+ else
23
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
24
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
25
+ end
26
+ end
27
+
28
+ require('rubygems')
29
+ require('bundler/setup')
30
+
31
+ load(Gem.bin_path('rake', 'rake'))
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,356 @@
1
+ #pragma once
2
+ #include <zlib.h>
3
+ #include "ruby.h"
4
+ #include "erlpack/sysdep.h"
5
+ #include "erlpack/constants.h"
6
+
7
+ namespace etf
8
+ {
9
+ class decoder
10
+ {
11
+ public:
12
+ decoder(VALUE str)
13
+ : data((const uint8_t *)RSTRING_PTR(str)), size(RSTRING_LEN(str)), offset(0)
14
+ {
15
+ if (read8() != FORMAT_VERSION)
16
+ rb_raise(rb_eArgError, "Invalid version: %i", ETF_VERSION);
17
+ }
18
+
19
+ decoder(const uint8_t *str, size_t data_size, bool skip_version = false) : data(str), size(data_size), offset(0)
20
+ {
21
+ if (skip_version)
22
+ return;
23
+ if (read8() != FORMAT_VERSION)
24
+ rb_raise(rb_eArgError, "Invalid version: %i", ETF_VERSION);
25
+ }
26
+
27
+ VALUE decode_term()
28
+ {
29
+ if (offset >= size)
30
+ {
31
+ rb_raise(rb_eRangeError, "Decoding beyond the end of the buffer");
32
+ return Qnil;
33
+ }
34
+
35
+ const uint8_t type = read8();
36
+ switch (type)
37
+ {
38
+ case SMALL_INTEGER_EXT:
39
+ return decode_small_integer();
40
+ case INTEGER_EXT:
41
+ return decode_integer();
42
+ case FLOAT_EXT:
43
+ return decode_float();
44
+ case NEW_FLOAT_EXT:
45
+ return decode_new_float();
46
+ case ATOM_EXT:
47
+ case ATOM_UTF8_EXT:
48
+ return decode_atom();
49
+ case SMALL_ATOM_EXT:
50
+ case SMALL_ATOM_UTF8_EXT:
51
+ return decode_small_atom();
52
+ case SMALL_TUPLE_EXT:
53
+ return decode_small_tuple();
54
+ case LARGE_TUPLE_EXT:
55
+ return decode_large_tuple();
56
+ case NIL_EXT:
57
+ return decode_nil();
58
+ case STRING_EXT:
59
+ return decode_string_as_list();
60
+ case LIST_EXT:
61
+ return decode_list();
62
+ case MAP_EXT:
63
+ return decode_map();
64
+ case BINARY_EXT:
65
+ return decode_binary_as_string();
66
+ case SMALL_BIG_EXT:
67
+ return decode_small_bignum();
68
+ case LARGE_BIG_EXT:
69
+ return decode_large_bignum();
70
+ case COMPRESSED:
71
+ return decode_compressed();
72
+ default:
73
+ rb_raise(rb_eArgError, "Unsupported type identifier `%i' found", type);
74
+ return Qnil;
75
+ }
76
+ }
77
+
78
+ private:
79
+ const uint8_t *data;
80
+ const size_t size;
81
+ size_t offset;
82
+
83
+ uint8_t read8(void)
84
+ {
85
+ if (offset + sizeof(uint8_t) > size)
86
+ {
87
+ rb_raise(rb_eRangeError, "Reading a byte passes the end of the buffer");
88
+ return 0;
89
+ }
90
+ uint8_t val = *reinterpret_cast<const uint8_t *>(data + offset);
91
+ offset += sizeof(uint8_t);
92
+ return val;
93
+ }
94
+
95
+ uint16_t read16()
96
+ {
97
+ if (offset + sizeof(uint16_t) > size)
98
+ {
99
+ rb_raise(rb_eRangeError, "Reading two bytes passes the end of the buffer");
100
+ return 0;
101
+ }
102
+
103
+ uint16_t val = _erlpack_be16(*reinterpret_cast<const uint16_t *>(data + offset));
104
+ offset += sizeof(uint16_t);
105
+ return val;
106
+ }
107
+
108
+ uint32_t read32()
109
+ {
110
+ if (offset + sizeof(uint32_t) > size)
111
+ {
112
+ rb_raise(rb_eRangeError, "Reading four bytes passes the end of the buffer");
113
+ return 0;
114
+ }
115
+
116
+ uint32_t val = _erlpack_be32(*reinterpret_cast<const uint32_t *>(data + offset));
117
+ offset += sizeof(uint32_t);
118
+ return val;
119
+ }
120
+
121
+ uint64_t read64()
122
+ {
123
+ if (offset + sizeof(uint64_t) > size)
124
+ {
125
+ rb_raise(rb_eRangeError, "Reading eight bytes passes the end of the buffer");
126
+ return 0;
127
+ }
128
+
129
+ uint64_t val = _erlpack_be64(*reinterpret_cast<const uint64_t *>(data + offset));
130
+ offset += sizeof(uint64_t);
131
+ return val;
132
+ }
133
+
134
+ VALUE decode_small_integer(void)
135
+ {
136
+ return INT2FIX(read8());
137
+ }
138
+
139
+ VALUE decode_integer(void)
140
+ {
141
+ return INT2NUM(read32());
142
+ }
143
+
144
+ VALUE decode_array(int32_t length)
145
+ {
146
+ VALUE values[length];
147
+
148
+ for (int32_t index = 0; index < length; index++)
149
+ values[index] = decode_term();
150
+ return rb_ary_new_from_values(length, values);
151
+ }
152
+
153
+ VALUE decode_list()
154
+ {
155
+ const uint32_t length = read32();
156
+ VALUE array = decode_array(length);
157
+ const uint8_t tail = read8();
158
+
159
+ if (tail != term::nil)
160
+ {
161
+ rb_raise(rb_eArgError, "List doesn't end with `NIL`, but it must!");
162
+ return Qnil;
163
+ }
164
+
165
+ return array;
166
+ }
167
+
168
+ VALUE decode_tuple(uint32_t length)
169
+ {
170
+ return decode_array(length);
171
+ }
172
+
173
+ VALUE decode_nil()
174
+ {
175
+ return rb_ary_new();
176
+ }
177
+
178
+ VALUE decode_map()
179
+ {
180
+ const uint32_t length = read32();
181
+ VALUE hash = rb_hash_new();
182
+
183
+ for (uint32_t index = 0; index < length; index++)
184
+ {
185
+ const VALUE key = decode_term();
186
+ const VALUE value = decode_term();
187
+
188
+ rb_hash_aset(hash, key, value);
189
+ }
190
+
191
+ return hash;
192
+ }
193
+
194
+ const char *read_string(uint32_t length)
195
+ {
196
+ if (offset + length > size)
197
+ {
198
+ rb_raise(rb_eRangeError, "Reading sequence past the end of the buffer");
199
+ return 0;
200
+ }
201
+
202
+ const uint8_t *str = data + offset;
203
+ offset += length;
204
+ return (const char *)str;
205
+ }
206
+
207
+ VALUE process_atom(const char *atom, uint16_t length)
208
+ {
209
+ if (atom == NULL)
210
+ {
211
+ return Qnil;
212
+ }
213
+
214
+ if (length >= 3 && length <= 5)
215
+ {
216
+ if (length == 3 && strncmp(atom, "nil", 3) == 0)
217
+ return Qnil;
218
+ // Is this actually a possible to receive from discord?
219
+ // It's defined in the js erlpack decoder but I'm not sure.
220
+ else if (length == 4 && strncmp(atom, "null", 4) == 0)
221
+ return Qnil;
222
+ else if (length == 4 && strncmp(atom, "true", 4) == 0)
223
+ return Qtrue;
224
+ else if (length == 5 && strncmp(atom, "false", 5) == 0)
225
+ return Qfalse;
226
+ }
227
+
228
+ return ID2SYM(rb_intern2(atom, length));
229
+ }
230
+
231
+ VALUE decode_atom()
232
+ {
233
+ uint16_t length = read16();
234
+ const char *atom = read_string(length);
235
+ return process_atom(atom, length);
236
+ }
237
+
238
+ VALUE decode_small_atom()
239
+ {
240
+ uint8_t length = read8();
241
+ const char *atom = read_string(length);
242
+ return process_atom(atom, length);
243
+ }
244
+
245
+ VALUE decode_float()
246
+ {
247
+ const uint8_t FLOAT_LENGTH = 31;
248
+ const char *float_string = read_string(FLOAT_LENGTH);
249
+
250
+ if (float_string == NULL)
251
+ return Qnil;
252
+
253
+ double number;
254
+ char buff[FLOAT_LENGTH + 1] = {0};
255
+ memcpy(buff, float_string, FLOAT_LENGTH);
256
+
257
+ uint32_t count = sscanf(buff, "%lf", &number);
258
+ if (count != 1)
259
+ {
260
+ rb_raise(rb_eArgError, "Invalid float encoded.");
261
+ return Qnil;
262
+ }
263
+
264
+ return DBL2NUM(number);
265
+ }
266
+
267
+ VALUE decode_new_float()
268
+ {
269
+ uint64_t u64 = read64();
270
+ double dbl;
271
+ memcpy(&dbl, &u64, sizeof(double));
272
+
273
+ return DBL2NUM(dbl);
274
+ }
275
+
276
+ VALUE decode_big(uint32_t length)
277
+ {
278
+ const uint8_t sign = read8();
279
+ const char *buff = read_string(length);
280
+
281
+ auto flags = INTEGER_PACK_LITTLE_ENDIAN | (sign * INTEGER_PACK_NEGATIVE);
282
+ return rb_integer_unpack(buff, length, 1, 0, flags);
283
+ }
284
+
285
+ VALUE decode_small_bignum()
286
+ {
287
+ const uint8_t length = read8();
288
+ return decode_big(length);
289
+ }
290
+
291
+ VALUE decode_large_bignum()
292
+ {
293
+ const uint32_t length = read32();
294
+ return decode_big(length);
295
+ }
296
+
297
+ VALUE decode_binary_as_string()
298
+ {
299
+ const uint32_t length = read32();
300
+ const char *str = read_string(length);
301
+
302
+ if (str == NULL)
303
+ return Qnil;
304
+
305
+ return rb_str_new(str, length);
306
+ }
307
+
308
+ VALUE decode_string_as_list()
309
+ {
310
+ const uint16_t length = read16();
311
+
312
+ if (offset + length > size)
313
+ {
314
+ rb_raise(rb_eRangeError, "Reading sequence past the end of the buffer");
315
+ return Qnil;
316
+ }
317
+
318
+ VALUE values[length];
319
+ for (uint16_t index = 0; index < length; index++)
320
+ values[index] = INT2FIX(read8());
321
+ return rb_ary_new_from_values(length, values);
322
+ }
323
+
324
+ VALUE decode_small_tuple()
325
+ {
326
+ return decode_tuple(read8());
327
+ }
328
+
329
+ VALUE decode_large_tuple()
330
+ {
331
+ return decode_tuple(read32());
332
+ }
333
+
334
+ VALUE decode_compressed()
335
+ {
336
+ const uint32_t decompressed_size = read32();
337
+
338
+ uint64_t source_size = decompressed_size;
339
+ 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));
341
+
342
+ offset += source_size;
343
+ if (ret != Z_OK)
344
+ {
345
+ free(out_buffer);
346
+ rb_raise(rb_eArgError, "Failed to uncompress compressed item");
347
+ return Qnil;
348
+ }
349
+
350
+ decoder decompressed(out_buffer, decompressed_size, true);
351
+ VALUE value = decompressed.decode_term();
352
+ free(out_buffer);
353
+ return value;
354
+ }
355
+ };
356
+ } // namespace etf