z85 0.9 → 0.10

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/ext/z85/z85.c +29 -0
  3. data/lib/z85.rb +2 -23
  4. data/lib/z85/version.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b8a8f710b44f09a0f49dcd06e762772a7be52afe545c9704c9bfc3b8fb18686
4
- data.tar.gz: 0ca0ddb2f4a4acb520be1ff60e5ccb1e2c570db10ec7c1cc2dc94635b68c05ae
3
+ metadata.gz: 2b160adbdea010f5e0545cd033922819550b2b8f435422ff3ba363000345e2ca
4
+ data.tar.gz: 1703dd4ddbf8c142be0b6c22fa658814ecbd413127c39fc09e2f6ca75d2fc050
5
5
  SHA512:
6
- metadata.gz: 288828a461c5de1c20ae44c03b275dfa5c21264517c397be2bb03bf7da5c4bfd59d4d882afe5d3c258f8279836513a1508b712d6225e981813f690fcf8fa76ea
7
- data.tar.gz: 782da75afcaeaca47cddc7dfd85893b96ca8ddec4d9f32721e39027803f236d754a80d88c8f3114e9787df70a61f3d12bd6c3cd582a2d7f9bbdb8ceb1f8558df
6
+ metadata.gz: '0894598b2ce5a09fba76ec6122c2bd57571b73f03947a979b3450595e741875083cbbfebeb3413b5713c07dd65648228c2e4356b501e661a99048e422ce94a5d'
7
+ data.tar.gz: 4a0d167f327f6c2409a04a0d2cc680da05fece160eeba3d126639e725413c764469c01b032eec9b693a9b3c239b2751f983671d135db4e447142d5e6b4d87c10
@@ -33,6 +33,8 @@
33
33
 
34
34
  typedef unsigned char byte;
35
35
 
36
+ static VALUE z85_error;
37
+
36
38
  static char encoder[85 + 1] = {
37
39
  "0123456789"
38
40
  "abcdefghij"
@@ -119,12 +121,39 @@ static VALUE z85_decode(VALUE _mod, VALUE rstring)
119
121
  return out;
120
122
  }
121
123
 
124
+ static VALUE z85_extract_counter(VALUE _mod, VALUE encoded)
125
+ {
126
+ char* string = StringValuePtr(encoded);
127
+ char counter = string[RSTRING_LEN(encoded) - 1] - '0';
128
+ if (0 <= counter && counter <= 3) {
129
+ return INT2FIX(counter);
130
+ } else {
131
+ rb_raise(z85_error, "Invalid counter: %c", counter + '0');
132
+ }
133
+ }
134
+
135
+ static VALUE z85_trim_padding(VALUE _mod, VALUE padded_string, VALUE counter)
136
+ {
137
+ int padding_size = FIX2INT(counter);
138
+ long trimmed_len = RSTRING_LEN(padded_string) - padding_size;
139
+
140
+ if (trimmed_len < 0)
141
+ rb_raise(z85_error, "String too short for counter %d", padding_size);
142
+ rb_str_resize(padded_string, trimmed_len);
143
+
144
+ return Qnil;
145
+ }
146
+
122
147
  /* This function has a special name and it is invoked by Ruby to initialize the extension. */
123
148
  void Init_z85()
124
149
  {
125
150
  VALUE z85 = rb_define_module("Z85");
126
151
  VALUE z85_singleton_class = rb_singleton_class(z85);
127
152
 
153
+ z85_error = rb_define_class_under(z85, "Error", rb_eStandardError);
154
+
128
155
  rb_define_private_method(z85_singleton_class, "_encode", z85_encode, 1);
129
156
  rb_define_private_method(z85_singleton_class, "_decode", z85_decode, 1);
157
+ rb_define_private_method(z85_singleton_class, "extract_counter", z85_extract_counter, 1);
158
+ rb_define_private_method(z85_singleton_class, "trim_padding", z85_trim_padding, 2);
130
159
  }
data/lib/z85.rb CHANGED
@@ -7,8 +7,6 @@ module Z85
7
7
  PADDINGS = ["", "\0", "\0\0", "\0\0\0"].freeze
8
8
  private_constant :PADDINGS
9
9
 
10
- class Error < StandardError; end
11
-
12
10
  class << self
13
11
  private def err(message)
14
12
  raise Error, message
@@ -24,7 +22,7 @@ module Z85
24
22
 
25
23
  def encode_with_padding(string)
26
24
  n = 4 - (string.bytesize % 4)
27
- n == 4 ? _encode(string) + "0" : _encode(string + PADDINGS[n]) + n.to_s
25
+ n == 4 ? _encode(string) << "0" : _encode(string + PADDINGS[n]) << n.to_s
28
26
  end
29
27
 
30
28
  def decode(string)
@@ -42,28 +40,9 @@ module Z85
42
40
 
43
41
  counter = extract_counter(string)
44
42
  decoded = _decode(string)
45
-
46
- begin
47
- decoded[-counter, counter] = ""
48
- rescue IndexError
49
- err "String too short for counter #{counter}"
50
- end
43
+ trim_padding(decoded, counter)
51
44
 
52
45
  decoded
53
46
  end
54
-
55
- private def extract_counter(string)
56
- begin
57
- counter = Integer(string[-1])
58
- rescue ArgumentError
59
- err "Invalid counter: #{string[-1]}"
60
- end
61
-
62
- if counter <= 3
63
- counter
64
- else
65
- err "Invalid counter: #{counter}"
66
- end
67
- end
68
47
  end
69
48
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Z85
4
- VERSION = "0.9"
4
+ VERSION = "0.10"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: z85
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.9'
4
+ version: '0.10'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xavier Noria
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-03 00:00:00.000000000 Z
11
+ date: 2019-08-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: fxn@hashref.com