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.
- checksums.yaml +4 -4
- data/ext/z85/z85.c +31 -6
- data/lib/z85.rb +2 -11
- data/lib/z85/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1409814fc8e3091281bec7c6bda76f7179c27f6f3e95644d668584772de3a057
|
4
|
+
data.tar.gz: b2f6d1afec64d8db0c7fac818359c5174093b15498fb7b80fbc59bdfb2121cb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df15929a429850f26bcaebab4fb7690d47625a8e5fa92eb8acb8952017e9a6ba52fda003977b712249a127a6dfbd0b00e05709f7b59800339ac3e1a1b8f103a5
|
7
|
+
data.tar.gz: abdbac913d2db8aded52cfd67a31a54a6d3e8f9bb1eaf8ed29a1ac8df96a442fc919642399a27b15ca49b156884feb36859686c9f3a17ac068a1da82966c2574
|
data/ext/z85/z85.c
CHANGED
@@ -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
|
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
|
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 =
|
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",
|
134
|
-
rb_define_singleton_method(z85, "decode",
|
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
|
-
|
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
|
data/lib/z85/version.rb
CHANGED