z85 0.3 → 0.4

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 +31 -6
  3. data/lib/z85.rb +2 -11
  4. data/lib/z85/version.rb +1 -1
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db559c519963849320043f3560f8a7971fed6d07a0cecee132d5286ce9d8a484
4
- data.tar.gz: c394a65cefbb48fd7d6954816005bc325a26358a86b6b3eca88dad7e25db8714
3
+ metadata.gz: 1409814fc8e3091281bec7c6bda76f7179c27f6f3e95644d668584772de3a057
4
+ data.tar.gz: b2f6d1afec64d8db0c7fac818359c5174093b15498fb7b80fbc59bdfb2121cb9
5
5
  SHA512:
6
- metadata.gz: 80169c9ecf71b235a45ebe7c0113b13fa699849cc7276f26b3c1e965e0e7a1598b3e4e7bf72b729dbc442862030bdc5046a59c36c8808e4f92a817995a5f9274
7
- data.tar.gz: 5797ca4ed1c69a193ac1546434943cfd87400b651721a79ee23dc1120fbcd17bcbaf31ba478b2080768317804f76e01e6954fcd0b05e5ff8133364c4b8bd1924
6
+ metadata.gz: df15929a429850f26bcaebab4fb7690d47625a8e5fa92eb8acb8952017e9a6ba52fda003977b712249a127a6dfbd0b00e05709f7b59800339ac3e1a1b8f103a5
7
+ data.tar.gz: abdbac913d2db8aded52cfd67a31a54a6d3e8f9bb1eaf8ed29a1ac8df96a442fc919642399a27b15ca49b156884feb36859686c9f3a17ac068a1da82966c2574
@@ -60,7 +60,7 @@ static byte decoder[96] = {
60
60
  0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00
61
61
  };
62
62
 
63
- static VALUE z85_encode(VALUE _mod, VALUE string)
63
+ static VALUE encode(VALUE _mod, VALUE string)
64
64
  {
65
65
  byte* data = (byte*) StringValuePtr(string);
66
66
  long size = RSTRING_LEN(string);
@@ -93,10 +93,10 @@ static VALUE z85_encode(VALUE _mod, VALUE string)
93
93
  return out;
94
94
  }
95
95
 
96
- static VALUE z85_decode(VALUE _mod, VALUE string)
96
+ static VALUE _decode(VALUE string, int padding)
97
97
  {
98
98
  char* data = StringValuePtr(string);
99
- long size = RSTRING_LEN(string);
99
+ long size = RSTRING_LEN(string) - padding;
100
100
 
101
101
  if (size % 5)
102
102
  rb_raise(rb_eRuntimeError, "Invalid string, number of bytes must be a multiple of 5");
@@ -119,17 +119,42 @@ static VALUE z85_decode(VALUE _mod, VALUE string)
119
119
  }
120
120
  }
121
121
 
122
- VALUE out = rb_str_new((const char*) decoded, decoded_size);
122
+ VALUE out = 0;
123
+ if (padding) {
124
+ int padding_length = data[size] - '0';
125
+ if (padding_length < 0 || padding_length > 3) {
126
+ rb_raise(rb_eRuntimeError, "Invalid padding length");
127
+ } else if (decoded_size >= (size_t) padding_length) {
128
+ out = rb_str_new((const char*) decoded, decoded_size - padding_length);
129
+ } else {
130
+ rb_raise(rb_eRuntimeError, "Invalid padded string");
131
+ }
132
+ } else {
133
+ out = rb_str_new((const char*) decoded, decoded_size);
134
+ }
135
+
123
136
  free(decoded);
124
137
 
125
138
  return out;
126
139
  }
127
140
 
141
+ static VALUE decode(VALUE _mod, VALUE string)
142
+ {
143
+ return _decode(string, 0);
144
+ }
145
+
146
+ static VALUE decode_with_padding(VALUE _mod, VALUE string)
147
+ {
148
+ return _decode(string, 1);
149
+ }
150
+
128
151
  /* This function has a special name and it is invoked by Ruby to initialize the extension. */
129
152
  void Init_z85()
130
153
  {
131
154
  VALUE z85 = rb_define_module("Z85");
132
155
 
133
- rb_define_singleton_method(z85, "encode", z85_encode, 1);
134
- rb_define_singleton_method(z85, "decode", z85_decode, 1);
156
+ rb_define_singleton_method(z85, "encode", encode, 1);
157
+ rb_define_singleton_method(z85, "decode", decode, 1);
158
+
159
+ rb_define_singleton_method(z85, "decode_with_padding", decode_with_padding, 1);
135
160
  }
data/lib/z85.rb CHANGED
@@ -6,17 +6,8 @@ require "z85/version"
6
6
  module Z85
7
7
  def self.encode_with_padding(string)
8
8
  padding_length = 4 - (string.bytesize % 4)
9
- string += "\0" * padding_length unless padding_length == 4
9
+ padding_length = 0 if padding_length == 4
10
+ string += "\0" * padding_length if padding_length > 0
10
11
  encode(string) + padding_length.to_s
11
12
  end
12
-
13
- def self.decode_with_padding(encoded)
14
- padding_length = encoded[-1].to_i
15
- encoded.chop!
16
-
17
- decoded = decode(encoded)
18
- padding_length.times { decoded.chop! } unless padding_length == 4
19
-
20
- decoded
21
- end
22
13
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Z85
4
- VERSION = "0.3"
4
+ VERSION = "0.4"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: z85
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: '0.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xavier Noria