b3e 0.0.1 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7d71340c46c91802c482c1d1f02ac1dc9ab5c773d3dc5c1e914a1e4868e44a4e
4
- data.tar.gz: daff629b6799d641aa6af2815f1ad4ff3e17e77992d04f5b3065692a8cd473e8
3
+ metadata.gz: 0cd149f56587ff9427299de891867d6ad1f9406ae5c5215e9b8f7aed94a3d5b0
4
+ data.tar.gz: a8acc827c9503d044c4cb4ce8a759fb0a7aced68d364bdb71e0dfc6a22a06de1
5
5
  SHA512:
6
- metadata.gz: e5550386d986d56fce5b5080972ea50c764f0ce3b0ecf60a6ddcdd6cc56430f402a2d511bcad682318c6c431602e1dd505023eed19dfec9378ac8e0ddb252c8c
7
- data.tar.gz: f0c7349d32f39f5e74c8baf4480c3668c8ea2b8ca5fe25681abce38870d4bd0a5fba1938f33289fcf8915af1c4f5979d38316a4a1eab3f7af8d066c70a5634b3
6
+ metadata.gz: 1535d132baf62e5a79d26daa8ae112b43a957419917ffb0d52bc9664385cbcc24f11bde802c8731815c96a424906b43951b5a3a8c759de42e72146066fb25216
7
+ data.tar.gz: 930b3df33c918db2dba685a242e91d9528d03067bd9eee20549b293a1da6192ea26a01a0a1df22e89b9343f2c7b4e602a2af2fade470d31003e0780e48ebd4ad
data/CHANGELOG.md CHANGED
@@ -1,6 +1,12 @@
1
+ ## [v0.1.0](https://github.com/metabahn/b3e/releases/tag/v0.1.0)
2
+
3
+ *released on 2021-03-13*
4
+
5
+ * `fix` [#2](https://github.com/metabahn/b3e/pull/2) Include the ext directory in the gem ([bryanp](https://github.com/bryanp))
6
+
1
7
  ## [v0.0.0](https://github.com/metabahn/b3e/releases/tag/v0.0.0)
2
8
 
3
- *released on v0.0.0*
9
+ *released on 2021-03-13*
4
10
 
5
11
  * `add` [#1](https://github.com/metabahn/b3e/pull/1) Initial b3e implementation ([bryanp](https://github.com/bryanp))
6
12
 
data/ext/b3e/b3e_ext.c ADDED
@@ -0,0 +1,154 @@
1
+ /*
2
+ This software is licensed under the MPL-2.0 License.
3
+
4
+ Copyright Bryan Powell, 2021.
5
+ */
6
+
7
+ #include <math.h>
8
+ #include <ruby/ruby.h>
9
+
10
+ static const char ALPHABET[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
11
+
12
+ static const int INDEX[] = {
13
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
14
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
15
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
16
+ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
17
+ -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
18
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
19
+ -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
20
+ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
21
+ };
22
+
23
+ int COMPACT_MASK = 0x1E;
24
+ int MASK_5BITS = 0x1F;
25
+
26
+ VALUE cInvalid;
27
+
28
+ static VALUE rb_b3e_encode(VALUE self, VALUE rString) {
29
+ char* string = StringValuePtr(rString);
30
+ char encoded[RSTRING_LEN(rString) * 8 / 5 + 1];
31
+
32
+ int64_t cursor1 = RSTRING_LEN(rString) * 8;
33
+ int64_t cursor2;
34
+ int64_t i;
35
+
36
+ char j;
37
+ char blen1;
38
+ char blen2;
39
+ char shift;
40
+ unsigned char byte;
41
+ int written = 0;
42
+
43
+ while (1) {
44
+ j = 0;
45
+ i = 0;
46
+
47
+ cursor2 = cursor1 - 6;
48
+ if (cursor2 <= 0) {
49
+ cursor2 = 0;
50
+ blen1 = cursor1;
51
+ } else {
52
+ i = cursor2 / 8;
53
+ j = cursor2 % 8;
54
+ blen1 = (i + 1) * 8 - cursor2;
55
+ if (blen1 > 6) {
56
+ blen1 = 6;
57
+ }
58
+ }
59
+
60
+ shift = 8 - j - blen1;
61
+ byte = (unsigned char)string[i] >> shift & ((1 << blen1) - 1);
62
+ if (blen1 < 6 && cursor2 > 0) {
63
+ blen2 = 6 - blen1;
64
+ byte = (byte << blen2) | ((unsigned char)string[i + 1] >> (8 - blen2));
65
+ }
66
+
67
+ if ((byte & COMPACT_MASK) == COMPACT_MASK) {
68
+ if (cursor2 > 0 || byte > MASK_5BITS) {
69
+ cursor2++;
70
+ }
71
+
72
+ byte &= MASK_5BITS;
73
+ }
74
+
75
+ cursor1 = cursor2;
76
+ encoded[written] = ALPHABET[byte];
77
+ written++;
78
+
79
+ if (cursor2 <= 0) {
80
+ break;
81
+ }
82
+ }
83
+
84
+ return rb_str_new(encoded, written);
85
+ }
86
+
87
+ static VALUE rb_b3e_decode(VALUE self, VALUE rString) {
88
+ char* string = StringValuePtr(rString);
89
+
90
+ u_int64_t length = RSTRING_LEN(rString);
91
+ u_int64_t size = length * 6 / 8;
92
+ u_int64_t index;
93
+
94
+ char decoded[size];
95
+ u_int64_t cursor = size;
96
+ unsigned char character;
97
+ int x;
98
+ int byte = 0;
99
+ int position = 0;
100
+ int cb;
101
+
102
+ for (index = 0; index < length; index++) {
103
+ character = string[index];
104
+ x = INDEX[character];
105
+
106
+ if (x == -1) {
107
+ rb_raise(cInvalid, "encountered a character that is not in the base62 alphabet");
108
+ }
109
+
110
+ if (index == length - 1) {
111
+ byte |= x << position;
112
+ cb = position % 8;
113
+ position += (cb == 0 ? 0 : 8 - cb);
114
+ } else if ((x & COMPACT_MASK) == COMPACT_MASK) {
115
+ byte |= x << position;
116
+ position += 5;
117
+ } else {
118
+ byte |= x << position;
119
+ position += 6;
120
+ }
121
+
122
+ if (position >= 8) {
123
+ cursor--;
124
+ decoded[cursor] = byte;
125
+ position %= 8;
126
+ byte >>= 8;
127
+ }
128
+ }
129
+
130
+ if (position > 0) {
131
+ cursor--;
132
+ decoded[cursor] = byte;
133
+ }
134
+
135
+ if (cursor > 0) {
136
+ char resized[size];
137
+
138
+ for (index = 0; index < size; index++) {
139
+ resized[index] = decoded[cursor + index];
140
+ }
141
+
142
+ return rb_str_new(resized, size - cursor);
143
+ } else {
144
+ return rb_str_new(decoded, size);
145
+ }
146
+ }
147
+
148
+ void Init_b3e_ext(void) {
149
+ VALUE mB3e = rb_const_get(rb_cObject, rb_intern("B3e"));
150
+ cInvalid = rb_const_get(mB3e, rb_intern("Invalid"));
151
+
152
+ rb_define_singleton_method(mB3e, "c_encode", rb_b3e_encode, 1);
153
+ rb_define_singleton_method(mB3e, "c_decode", rb_b3e_decode, 1);
154
+ }
data/lib/b3e/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module B3e
4
- VERSION = "0.0.1"
4
+ VERSION = "0.1.0"
5
5
 
6
6
  def self.version
7
7
  VERSION
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: b3e
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Powell
@@ -20,6 +20,7 @@ files:
20
20
  - CHANGELOG.md
21
21
  - LICENSE
22
22
  - README.md
23
+ - ext/b3e/b3e_ext.c
23
24
  - ext/b3e/extconf.rb
24
25
  - lib/b3e.rb
25
26
  - lib/b3e/version.rb