z85 0.7 → 0.8

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 -6
  3. data/lib/z85.rb +30 -10
  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: 7c6f7125b81693238b15334de72ed53b7d5cd65042273ad901d364f4e86cedd3
4
- data.tar.gz: 56fdb2a0418c6d7a9dbfa23532df79a807592b37883ffe689e2a0e4bc4eb2dd1
3
+ metadata.gz: 6e683619c908d319f091ecaadc76c7e59786da2d7226063e7ea876dd14fffbd8
4
+ data.tar.gz: e4b648c8f706318a8c47bd150e713308da299d02e4814a604ef9bbe9cabe0a80
5
5
  SHA512:
6
- metadata.gz: a801396bddf699a1b86b6824f845446d6ff61437d43f8527e4052f40d49b72e1262f825fd284d0ee0d8a4127d11c14bf2f8c616018ae77db1030a3625e12da27
7
- data.tar.gz: e5635d024cb662493fe6134e4c35a56435f2924fb26884476f9f9409f9a0579f9df3fa733bd3727dcf8cb5e5b619520c4c15da0d1af9e9c29aef93460a56cb3f
6
+ metadata.gz: ece12a6308cb55fd09ba0f698b15090037e512487e2d70c510ec0132d6c38c062a47707f32ef3939628790774405fb3dc36664a2d914b054e1e915f6deaba378
7
+ data.tar.gz: 1aa6f0004fb069b400ff31306ed5a035ddceb96a8efb1c8c16514c5bdc4cf1b331205556d2a70edc9dfe7340d73e3af161ba9824f45efd87a331042563fce9b6
@@ -60,13 +60,28 @@ 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 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 *encoded = malloc(encoded_len + 1);
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 Z85_decode(VALUE _mod, VALUE rstring)
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 = malloc(decoded_size);
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
- rb_define_singleton_method(z85, "_encode", Z85_encode, 1);
128
- rb_define_singleton_method(z85, "_decode", Z85_decode, 1);
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 "Input length should be 0 mod 4. Please, check Z85.encode_with_padding."
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
- counter = 4 - (string.bytesize % 4)
26
- counter == 4 ? _encode(string) + "0" : _encode(string + PADDINGS[counter]) + counter.to_s
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
- err "Input length should be 1 mod 5" if string.bytesize % 5 != 1
38
-
39
- counter = string[-1]
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
- decoded.slice!(0, size)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Z85
4
- VERSION = "0.7"
4
+ VERSION = "0.8"
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.7'
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-02 00:00:00.000000000 Z
11
+ date: 2019-08-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: fxn@hashref.com