Ascii85 2.0.0 → 2.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e134217a95580db89dd446ac40511345e8c0a317be617b9b04e2ac8ca5db9670
4
- data.tar.gz: bcc1f3f71f2f5748958825602ef58c7365ef52f7fe3265a9409fd1b84b5c8d4f
3
+ metadata.gz: 6866b24599936a3644853d934a6df51cd4c010e6ae2dd74bd1e0136b930a20bb
4
+ data.tar.gz: d2a7ca24ff02f36e287b9171df96c4f0d31f2377b312185fe4ea637dcb23ea3c
5
5
  SHA512:
6
- metadata.gz: 4c15434410e46485ada5dcc04929092f77da1533861d22ffe65ab0e2f1e8300a821b16524fbeb5ad137cc6a139232a5684dfe6fd34c5ad33e19d68a95d3f604e
7
- data.tar.gz: a397048e6009d3adf0c2582cfe9dbd913f6b8888c19fbf48939b1a7ab48de704fa7fb76253d5d3a05d105215fb9e9f2da299d91c12cf5e6c2a3cb2636a2b6f8a
6
+ metadata.gz: 88c6434f72cd175bc15d258bcf21e863fc2fcfdc05cd5cecbffeb2d4a6265734be9e327bd0d64d1c3e4ce0a83fffab396df55c33f2f8f80e7ad9ba7c5301502c
7
+ data.tar.gz: 7834017172e9aefc7ade9d9bcc4b3ba90cc4f4a2472a7cdeea31ce95ade21166adbb1e9e54c78e1bb587386404952c3c61118e0b35b9cd670fac612a72c472c5
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Ascii85 Changelog
2
2
 
3
+ ## [2.0.2] - 2026-11-21
4
+
5
+ ### Fixed
6
+
7
+ - Decoding a partial, trailing 5-tuple to a value >= 2**32 raised `RangeError` instead of `DecodingError` (Issue #9)
8
+
9
+ ## [2.0.1] - 2024-09-15
10
+
11
+ ### Fixed
12
+
13
+ - Decoding binary data could lead to Encoding errors (Issue #8)
14
+
3
15
  ## [2.0.0] - 2024-08-20
4
16
 
5
17
  ### BREAKING CHANGES
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ascii85
4
- VERSION = '2.0.0'
4
+ VERSION = '2.0.2'
5
5
  end
data/lib/ascii85.rb CHANGED
@@ -14,6 +14,11 @@ require 'stringio'
14
14
  #
15
15
  module Ascii85
16
16
  class << self
17
+ EMPTY_STRING = ''.dup.force_encoding(Encoding::ASCII_8BIT)
18
+ START_MARKER = '<~'.dup.force_encoding(Encoding::ASCII_8BIT)
19
+ ENDING_MARKER = '~>'.dup.force_encoding(Encoding::ASCII_8BIT)
20
+ LINE_BREAK = "\n".dup.force_encoding(Encoding::ASCII_8BIT)
21
+
17
22
  #
18
23
  # Encodes the bytes of the given String or IO-like object as Ascii85.
19
24
  #
@@ -55,22 +60,24 @@ module Ascii85
55
60
  StringIO.new(str_or_io.to_s, 'rb')
56
61
  end
57
62
 
58
- return ''.dup if reader.eof?
63
+ return EMPTY_STRING.dup if reader.eof?
59
64
 
60
65
  # Setup buffered Reader and Writers
61
66
  bufreader = BufferedReader.new(reader, unencoded_chunk_size)
62
67
  bufwriter = BufferedWriter.new(out || StringIO.new(String.new, 'wb'), encoded_chunk_size)
63
68
  writer = wrap_lines ? Wrapper.new(bufwriter, wrap_lines) : DummyWrapper.new(bufwriter)
64
69
 
65
- padding = "\0\0\0\0"
66
- tuplebuf = '!!!!!'.dup
70
+ padding = unfrozen_binary_copy("\0\0\0\0")
71
+ tuplebuf = unfrozen_binary_copy('!!!!!')
72
+ exclamations = unfrozen_binary_copy('!!!!!')
73
+ z = unfrozen_binary_copy('z')
67
74
 
68
75
  bufreader.each_chunk do |chunk|
69
76
  chunk.unpack('N*').each do |word|
70
77
  # Encode each big-endian 32-bit word into a 5-character tuple (except
71
78
  # for 0, which encodes to 'z')
72
79
  if word.zero?
73
- writer.write('z')
80
+ writer.write(z)
74
81
  else
75
82
  word, b0 = word.divmod(85)
76
83
  word, b1 = word.divmod(85)
@@ -98,7 +105,7 @@ module Ascii85
98
105
 
99
106
  # Encode the last word and cut off any padding
100
107
  if word.zero?
101
- writer.write('!!!!!'[0..(4 - padding_length)])
108
+ writer.write(exclamations[0..(4 - padding_length)])
102
109
  else
103
110
  word, b0 = word.divmod(85)
104
111
  word, b1 = word.divmod(85)
@@ -119,7 +126,7 @@ module Ascii85
119
126
  # If no output IO-object was provided, extract the encoded String from the
120
127
  # default StringIO writer. We force the encoding to 'ASCII-8BIT' to work
121
128
  # around a TruffleRuby bug.
122
- return writer.finish.io.string.force_encoding('ASCII-8BIT') if out.nil?
129
+ return writer.finish.io.string.force_encoding(Encoding::ASCII_8BIT) if out.nil?
123
130
 
124
131
  # Otherwise we make sure to flush the output writer, and then return it.
125
132
  writer.finish.io
@@ -151,8 +158,8 @@ module Ascii85
151
158
 
152
159
  # Get the positions of the opening/closing delimiters. If there is no pair
153
160
  # of opening/closing delimiters, return an unfrozen empty String.
154
- (start_pos = input.index(opening_delim)) or return ''.dup
155
- (end_pos = input.index(closing_delim, start_pos + 2)) or return ''.dup
161
+ (start_pos = input.index(opening_delim)) or return EMPTY_STRING.dup
162
+ (end_pos = input.index(closing_delim, start_pos + 2)) or return EMPTY_STRING.dup
156
163
 
157
164
  # Get the String inside the delimiter-pair
158
165
  input[(start_pos + 2)...end_pos]
@@ -226,7 +233,7 @@ module Ascii85
226
233
  end
227
234
 
228
235
  # Return an unfrozen String on empty input
229
- return ''.dup if reader.eof?
236
+ return EMPTY_STRING.dup if reader.eof?
230
237
 
231
238
  # Setup buffered Reader and Writers
232
239
  bufreader = BufferedReader.new(reader, encoded_chunk_size)
@@ -238,7 +245,8 @@ module Ascii85
238
245
  # Decode
239
246
  word = 0
240
247
  count = 0
241
- wordbuf = "\0\0\0\0".dup
248
+ zeroes = unfrozen_binary_copy("\0\0\0\0")
249
+ wordbuf = zeroes.dup
242
250
 
243
251
  bufreader.each_chunk do |chunk|
244
252
  chunk.each_byte do |c|
@@ -251,7 +259,7 @@ module Ascii85
251
259
  raise(Ascii85::DecodingError, "Found 'z' inside Ascii85 5-tuple") unless count.zero?
252
260
 
253
261
  # Expand z to 0-word
254
- bufwriter.write("\0\0\0\0")
262
+ bufwriter.write(zeroes)
255
263
 
256
264
  when '!'..'u'
257
265
  # Decode 5 characters into a 4-byte word
@@ -286,7 +294,7 @@ module Ascii85
286
294
  # We're done if all 5-tuples have been consumed
287
295
  if count.zero?
288
296
  bufwriter.flush
289
- return out || bufwriter.io.string.force_encoding('ASCII-8BIT')
297
+ return out || bufwriter.io.string.force_encoding(Encoding::ASCII_8BIT)
290
298
  end
291
299
 
292
300
  raise(Ascii85::DecodingError, 'Last 5-tuple consists of single character') if count == 1
@@ -295,16 +303,24 @@ module Ascii85
295
303
  count -= 1
296
304
  word += lut[count]
297
305
 
306
+ raise(Ascii85::DecodingError, "Invalid Ascii85 5-tuple (#{word} >= 2**32)") if word > 0xffffffff
307
+
298
308
  bufwriter.write((word >> 24).chr) if count >= 1
299
309
  bufwriter.write(((word >> 16) & 0xff).chr) if count >= 2
300
310
  bufwriter.write(((word >> 8) & 0xff).chr) if count == 3
301
311
  bufwriter.flush
302
312
 
303
- out || bufwriter.io.string.force_encoding('ASCII-8BIT')
313
+ out || bufwriter.io.string.force_encoding(Encoding::ASCII_8BIT)
304
314
  end
305
315
 
306
316
  private
307
317
 
318
+ # Copies the given String and forces the encoding of the returned copy to
319
+ # be Encoding::ASCII_8BIT.
320
+ def unfrozen_binary_copy(str)
321
+ str.dup.force_encoding(Encoding::ASCII_8BIT)
322
+ end
323
+
308
324
  # Buffers an underlying IO object to increase efficiency. You do not need
309
325
  # to use this directly.
310
326
  #
@@ -337,7 +353,7 @@ module Ascii85
337
353
  def initialize(io, buffer_size)
338
354
  @io = io
339
355
  @buffer_size = buffer_size
340
- @buffer = String.new(capacity: buffer_size)
356
+ @buffer = String.new(capacity: buffer_size, encoding: Encoding::ASCII_8BIT)
341
357
  end
342
358
 
343
359
  def write(tuple)
@@ -360,7 +376,7 @@ module Ascii85
360
376
  class DummyWrapper
361
377
  def initialize(out)
362
378
  @out = out
363
- @out.write('<~')
379
+ @out.write(START_MARKER)
364
380
  end
365
381
 
366
382
  def write(buffer)
@@ -368,7 +384,7 @@ module Ascii85
368
384
  end
369
385
 
370
386
  def finish
371
- @out.write('~>')
387
+ @out.write(ENDING_MARKER)
372
388
  @out.flush
373
389
 
374
390
  @out
@@ -385,7 +401,7 @@ module Ascii85
385
401
  @line_length = [2, wrap_lines.to_i].max
386
402
 
387
403
  @out = out
388
- @out.write('<~')
404
+ @out.write(START_MARKER)
389
405
 
390
406
  @cur_len = 2
391
407
  end
@@ -402,7 +418,7 @@ module Ascii85
402
418
 
403
419
  remaining = @line_length - @cur_len
404
420
  @out.write(buffer[0...remaining])
405
- @out.write("\n")
421
+ @out.write(LINE_BREAK)
406
422
  @cur_len = 0
407
423
  buffer = buffer[remaining..]
408
424
  return if buffer.empty?
@@ -411,8 +427,8 @@ module Ascii85
411
427
 
412
428
  def finish
413
429
  # Add the closing delimiter (may need to be pushed to the next line)
414
- @out.write("\n") if @cur_len + 2 > @line_length
415
- @out.write('~>')
430
+ @out.write(LINE_BREAK) if @cur_len + 2 > @line_length
431
+ @out.write(ENDING_MARKER)
416
432
 
417
433
  @out.flush
418
434
  @out
@@ -35,7 +35,11 @@ TEST_CASES = {
35
35
  '<~j+42iJVN3:K&_E6j+<0KJW/W?W8iG`j+EuaK"9on^Z0sZj+FJoK:LtSKB%T?~>',
36
36
 
37
37
  [Math::PI].pack('G') => '<~5RAV2<(&;T~>',
38
- [Math::E].pack('G') => '<~5R"n0M\\K6,~>'
38
+ [Math::E].pack('G') => '<~5R"n0M\\K6,~>',
39
+
40
+ # Minified example from Github issue 8.
41
+ # Note that OT and OU as the trailing characters are equivalent.
42
+ "\x9B\xB6\xB9+\x91" => '<~S$ojXOT~>'
39
43
  }.freeze
40
44
 
41
45
  describe Ascii85 do
@@ -59,6 +63,12 @@ describe Ascii85 do
59
63
  end
60
64
  end
61
65
 
66
+ it 'should always return unfrozen Strings' do
67
+ TEST_CASES.each_pair do |input, encoded|
68
+ assert_equal false, Ascii85.encode(input).frozen?
69
+ end
70
+ end
71
+
62
72
  it 'should encode Strings in different encodings correctly' do
63
73
  input_euc_jp = 'どうもありがとうミスターロボット'.encode('EUC-JP')
64
74
  input_binary = input_euc_jp.force_encoding('ASCII-8BIT')
@@ -139,6 +149,12 @@ describe Ascii85 do
139
149
  end
140
150
  end
141
151
 
152
+ it 'should always return unfrozen Strings' do
153
+ TEST_CASES.each_pair do |input, encoded|
154
+ assert_equal false, Ascii85.decode(encoded).frozen?
155
+ end
156
+ end
157
+
142
158
  it 'should accept valid input in encodings other than the default' do
143
159
  input = 'Ragnarök τέχνη русский язык I ♥ Ruby'
144
160
  input_ascii85 = Ascii85.encode(input)
@@ -195,6 +211,12 @@ describe Ascii85 do
195
211
  assert_raises(Ascii85::DecodingError) { Ascii85.decode('<~s8W-#~>') }
196
212
  end
197
213
 
214
+ it 'should raise DecodingError if the last, partial tuple is >= 2**32' do
215
+ assert_raises(Ascii85::DecodingError) { Ascii85.decode('<~s8W-~>') }
216
+ assert_raises(Ascii85::DecodingError) { Ascii85.decode('<~!!!!!uu~>') }
217
+ assert_equal "\xFF\xFF\xFF".b, Ascii85.decode('<~s8W*~>')
218
+ end
219
+
198
220
  it 'should raise DecodingError if it encounters an invalid character' do
199
221
  assert_raises(Ascii85::DecodingError) { Ascii85.decode('<~!!y!!~>') }
200
222
  end
@@ -217,6 +239,13 @@ describe Ascii85 do
217
239
  end
218
240
  end
219
241
 
242
+ it 'should always return unfrozen Strings' do
243
+ TEST_CASES.each_pair do |decoded, input|
244
+ raw_input = input[2...-2] # Remove '<~' and '~>'
245
+ assert_equal false, Ascii85.decode_raw(raw_input).frozen?
246
+ end
247
+ end
248
+
220
249
  it 'should decode from an IO object' do
221
250
  input = StringIO.new(';KZGo')
222
251
  result = Ascii85.decode_raw(input)
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Ascii85
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johannes Holzfuß
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-08-20 00:00:00.000000000 Z
10
+ date: 1980-01-01 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: minitest
@@ -51,8 +50,8 @@ executables:
51
50
  - ascii85
52
51
  extensions: []
53
52
  extra_rdoc_files:
54
- - README.md
55
53
  - LICENSE
54
+ - README.md
56
55
  files:
57
56
  - Ascii85.gemspec
58
57
  - CHANGELOG.md
@@ -69,7 +68,6 @@ homepage: https://github.com/DataWraith/ascii85gem/
69
68
  licenses:
70
69
  - MIT
71
70
  metadata: {}
72
- post_install_message:
73
71
  rdoc_options: []
74
72
  require_paths:
75
73
  - lib
@@ -84,8 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
82
  - !ruby/object:Gem::Version
85
83
  version: '0'
86
84
  requirements: []
87
- rubygems_version: 3.5.15
88
- signing_key:
85
+ rubygems_version: 3.7.2
89
86
  specification_version: 4
90
87
  summary: Ascii85 encoder/decoder
91
88
  test_files: