z85 0.4 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/ext/z85/z85.c +22 -22
  3. data/lib/z85.rb +4 -4
  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: 1409814fc8e3091281bec7c6bda76f7179c27f6f3e95644d668584772de3a057
4
- data.tar.gz: b2f6d1afec64d8db0c7fac818359c5174093b15498fb7b80fbc59bdfb2121cb9
3
+ metadata.gz: fc3a06cd55838caabe94e5669cb0a28d4279f12b9b26e4e78aaf692789b2a24a
4
+ data.tar.gz: 6b3662ea29821bbb38b0f813b20790f22ad08da30b5bc3a51fe80038b289bf7e
5
5
  SHA512:
6
- metadata.gz: df15929a429850f26bcaebab4fb7690d47625a8e5fa92eb8acb8952017e9a6ba52fda003977b712249a127a6dfbd0b00e05709f7b59800339ac3e1a1b8f103a5
7
- data.tar.gz: abdbac913d2db8aded52cfd67a31a54a6d3e8f9bb1eaf8ed29a1ac8df96a442fc919642399a27b15ca49b156884feb36859686c9f3a17ac068a1da82966c2574
6
+ metadata.gz: 6a0674ef043853bdf233ccef0dd6536d92469a7f6bad6fdc8929c5dfcd6991dd03dc8f3d377f64f7c98035059ed0abebf85b0c1980e014548f6beba34c10a68f
7
+ data.tar.gz: c1a1cdacb3348191524c0ce6ee376282abca80c26067639a50329765cf25226758067d575362c740063f7ee8f721ebebd1e4994cbd86af2bd797212d6e6c0849
@@ -63,18 +63,18 @@ static byte decoder[96] = {
63
63
  static VALUE encode(VALUE _mod, VALUE string)
64
64
  {
65
65
  byte* data = (byte*) StringValuePtr(string);
66
- long size = RSTRING_LEN(string);
66
+ long data_len = RSTRING_LEN(string);
67
67
 
68
- if (size % 4)
68
+ if (data_len % 4)
69
69
  rb_raise(rb_eRuntimeError, "Invalid string, number of bytes must be a multiple of 4");
70
70
 
71
- size_t encoded_size = size * 5 / 4;
72
- char *encoded = malloc(encoded_size + 1);
71
+ size_t encoded_len = data_len * 5 / 4;
72
+ char *encoded = malloc(encoded_len + 1);
73
73
  uint char_nbr = 0;
74
74
  uint byte_nbr = 0;
75
75
  uint32_t value = 0;
76
76
 
77
- while (byte_nbr < size) {
77
+ while (byte_nbr < data_len) {
78
78
  value = value * 256 + data[byte_nbr++];
79
79
  if (byte_nbr % 4 == 0) {
80
80
  uint divisor = 85 * 85 * 85 * 85;
@@ -96,18 +96,28 @@ static VALUE encode(VALUE _mod, VALUE string)
96
96
  static VALUE _decode(VALUE string, int padding)
97
97
  {
98
98
  char* data = StringValuePtr(string);
99
- long size = RSTRING_LEN(string) - padding;
99
+ long data_len = RSTRING_LEN(string) - padding;
100
100
 
101
- if (size % 5)
101
+ if (data_len % 5)
102
102
  rb_raise(rb_eRuntimeError, "Invalid string, number of bytes must be a multiple of 5");
103
103
 
104
- size_t decoded_size = size * 4 / 5;
104
+ size_t decoded_size = data_len * 4 / 5;
105
+
106
+ /* Verify the counter looks legit before even allocating memory. */
107
+ if (padding) {
108
+ int counter = data[data_len] - '0';
109
+ if (counter < 0 || counter > 3)
110
+ rb_raise(rb_eRuntimeError, "Invalid padding length");
111
+ else if (decoded_size < (size_t) counter)
112
+ rb_raise(rb_eRuntimeError, "Invalid padded string");
113
+ }
114
+
105
115
  byte* decoded = malloc(decoded_size);
106
116
 
107
117
  uint byte_nbr = 0;
108
118
  uint char_nbr = 0;
109
119
  uint32_t value = 0;
110
- while (char_nbr < size) {
120
+ while (char_nbr < data_len) {
111
121
  value = value * 85 + decoder[(byte) data[char_nbr++] - 32];
112
122
  if (char_nbr % 5 == 0) {
113
123
  uint divisor = 256 * 256 * 256;
@@ -119,19 +129,9 @@ static VALUE _decode(VALUE string, int padding)
119
129
  }
120
130
  }
121
131
 
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
- }
132
+ VALUE out = padding ?
133
+ rb_str_new((const char*) decoded, decoded_size - (data[data_len] - '0')) :
134
+ rb_str_new((const char*) decoded, decoded_size);
135
135
 
136
136
  free(decoded);
137
137
 
data/lib/z85.rb CHANGED
@@ -5,9 +5,9 @@ require "z85/version"
5
5
 
6
6
  module Z85
7
7
  def self.encode_with_padding(string)
8
- padding_length = 4 - (string.bytesize % 4)
9
- padding_length = 0 if padding_length == 4
10
- string += "\0" * padding_length if padding_length > 0
11
- encode(string) + padding_length.to_s
8
+ counter = 4 - (string.bytesize % 4)
9
+ counter = 0 if counter == 4
10
+ string += "\0" * counter if counter != 0
11
+ encode(string) + counter.to_s
12
12
  end
13
13
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Z85
4
- VERSION = "0.4"
4
+ VERSION = "0.5"
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.4'
4
+ version: '0.5'
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-07-31 00:00:00.000000000 Z
11
+ date: 2019-08-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: fxn@hashref.com