z85 0.7 → 0.8
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 +22 -6
- data/lib/z85.rb +30 -10
- 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: 6e683619c908d319f091ecaadc76c7e59786da2d7226063e7ea876dd14fffbd8
|
4
|
+
data.tar.gz: e4b648c8f706318a8c47bd150e713308da299d02e4814a604ef9bbe9cabe0a80
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ece12a6308cb55fd09ba0f698b15090037e512487e2d70c510ec0132d6c38c062a47707f32ef3939628790774405fb3dc36664a2d914b054e1e915f6deaba378
|
7
|
+
data.tar.gz: 1aa6f0004fb069b400ff31306ed5a035ddceb96a8efb1c8c16514c5bdc4cf1b331205556d2a70edc9dfe7340d73e3af161ba9824f45efd87a331042563fce9b6
|
data/ext/z85/z85.c
CHANGED
@@ -60,13 +60,28 @@ static byte decoder[96] = {
|
|
60
60
|
0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00
|
61
61
|
};
|
62
62
|
|
63
|
-
static
|
63
|
+
static void* z85_malloc(size_t size)
|
64
|
+
{
|
65
|
+
void* ptr = malloc(size);
|
66
|
+
|
67
|
+
/*
|
68
|
+
malloc(0) is implementation-defined and could return NULL, so if ptr is
|
69
|
+
NULL, we need to check size too before raising. Note that free(NULL) is
|
70
|
+
valid (it does nothing).
|
71
|
+
*/
|
72
|
+
if (!ptr && size)
|
73
|
+
rb_raise(rb_eNoMemError, "Out of memory");
|
74
|
+
|
75
|
+
return ptr;
|
76
|
+
}
|
77
|
+
|
78
|
+
static VALUE z85_encode(VALUE _mod, VALUE string)
|
64
79
|
{
|
65
80
|
byte* data = (byte*) StringValuePtr(string);
|
66
81
|
long size = RSTRING_LEN(string);
|
67
82
|
|
68
83
|
size_t encoded_len = size * 5 / 4;
|
69
|
-
char
|
84
|
+
char* encoded = z85_malloc(encoded_len + 1);
|
70
85
|
uint char_nbr = 0;
|
71
86
|
uint byte_nbr = 0;
|
72
87
|
uint32_t value = 0;
|
@@ -89,7 +104,7 @@ static VALUE Z85_encode(VALUE _mod, VALUE string)
|
|
89
104
|
return out;
|
90
105
|
}
|
91
106
|
|
92
|
-
static VALUE
|
107
|
+
static VALUE z85_decode(VALUE _mod, VALUE rstring)
|
93
108
|
{
|
94
109
|
char* string = StringValuePtr(rstring);
|
95
110
|
long strlen = RSTRING_LEN(rstring);
|
@@ -97,7 +112,7 @@ static VALUE Z85_decode(VALUE _mod, VALUE rstring)
|
|
97
112
|
strlen--; /* It is padded, ignore the counter */
|
98
113
|
|
99
114
|
size_t decoded_size = strlen * 4 / 5;
|
100
|
-
byte* decoded =
|
115
|
+
byte* decoded = z85_malloc(decoded_size);
|
101
116
|
|
102
117
|
uint byte_nbr = 0;
|
103
118
|
uint char_nbr = 0;
|
@@ -123,7 +138,8 @@ static VALUE Z85_decode(VALUE _mod, VALUE rstring)
|
|
123
138
|
void Init_z85()
|
124
139
|
{
|
125
140
|
VALUE z85 = rb_define_module("Z85");
|
141
|
+
VALUE z85_singleton_class = rb_singleton_class(z85);
|
126
142
|
|
127
|
-
|
128
|
-
|
143
|
+
rb_define_private_method(z85_singleton_class, "_encode", z85_encode, 1);
|
144
|
+
rb_define_private_method(z85_singleton_class, "_decode", z85_decode, 1);
|
129
145
|
}
|
data/lib/z85.rb
CHANGED
@@ -16,34 +16,54 @@ module Z85
|
|
16
16
|
|
17
17
|
def encode(string)
|
18
18
|
if string.bytesize % 4 != 0
|
19
|
-
err "
|
19
|
+
err "Number of bytes should be 0 mod 4. Please, check Z85.encode_with_padding."
|
20
20
|
end
|
21
|
+
|
21
22
|
_encode(string)
|
22
23
|
end
|
23
24
|
|
24
25
|
def encode_with_padding(string)
|
25
|
-
|
26
|
-
|
26
|
+
n = 4 - (string.bytesize % 4)
|
27
|
+
n == 4 ? _encode(string) + "0" : _encode(string + PADDINGS[n]) + n.to_s
|
27
28
|
end
|
28
29
|
|
29
30
|
def decode(string)
|
30
31
|
if string.bytesize % 5 != 0
|
31
32
|
err "Input length should be 0 mod 5. Please, check Z85.decode_with_padding."
|
32
33
|
end
|
34
|
+
|
33
35
|
_decode(string)
|
34
36
|
end
|
35
37
|
|
36
38
|
def decode_with_padding(string)
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
err "Invalid counter: #{counter}" if counter < "0" || counter > "3"
|
39
|
+
if string.bytesize % 5 != 1
|
40
|
+
err "Input length should be 1 mod 5"
|
41
|
+
end
|
41
42
|
|
43
|
+
counter = extract_counter(string)
|
42
44
|
decoded = _decode(string)
|
43
|
-
size = decoded.bytesize - counter.to_i
|
44
|
-
err "String too short for counter #{counter}" if size < 0
|
45
45
|
|
46
|
-
|
46
|
+
begin
|
47
|
+
decoded[-counter, counter] = ""
|
48
|
+
rescue IndexError
|
49
|
+
err "String too short for counter #{counter}"
|
50
|
+
end
|
51
|
+
|
52
|
+
decoded
|
53
|
+
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
|
47
67
|
end
|
48
68
|
end
|
49
69
|
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.8'
|
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-
|
11
|
+
date: 2019-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: fxn@hashref.com
|