z85 0.4 → 0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/z85/z85.c +22 -22
- data/lib/z85.rb +4 -4
- data/lib/z85/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc3a06cd55838caabe94e5669cb0a28d4279f12b9b26e4e78aaf692789b2a24a
|
4
|
+
data.tar.gz: 6b3662ea29821bbb38b0f813b20790f22ad08da30b5bc3a51fe80038b289bf7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a0674ef043853bdf233ccef0dd6536d92469a7f6bad6fdc8929c5dfcd6991dd03dc8f3d377f64f7c98035059ed0abebf85b0c1980e014548f6beba34c10a68f
|
7
|
+
data.tar.gz: c1a1cdacb3348191524c0ce6ee376282abca80c26067639a50329765cf25226758067d575362c740063f7ee8f721ebebd1e4994cbd86af2bd797212d6e6c0849
|
data/ext/z85/z85.c
CHANGED
@@ -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
|
66
|
+
long data_len = RSTRING_LEN(string);
|
67
67
|
|
68
|
-
if (
|
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
|
72
|
-
char *encoded = malloc(
|
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 <
|
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
|
99
|
+
long data_len = RSTRING_LEN(string) - padding;
|
100
100
|
|
101
|
-
if (
|
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 =
|
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 <
|
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 =
|
123
|
-
|
124
|
-
|
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
|
-
|
9
|
-
|
10
|
-
string += "\0" *
|
11
|
-
encode(string) +
|
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
|
data/lib/z85/version.rb
CHANGED
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
|
+
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-
|
11
|
+
date: 2019-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: fxn@hashref.com
|