@@ -89,6 +89,7 @@ def encode(str_or_io, wrap_lines = 80, out: nil)
89
89
next if ( chunk . bytesize & 0b11 ) . zero?
90
90
91
91
# If we have leftover bytes, we need to zero-pad to a multiple of four
92
+ # before converting to a 32-bit word.
92
93
padding_length = ( -chunk . bytesize ) % 4
93
94
trailing = chunk [ -( 4 - padding_length ) ..]
94
95
word = ( trailing + padding [ 0 ...padding_length ] ) . unpack1 ( 'N' )
@@ -234,6 +235,7 @@ def decode_raw(str_or_io, out: nil)
234
235
# Decode
235
236
word = 0
236
237
count = 0
238
+ wordbuf = "\0 \0 \0 \0 " . dup
237
239
238
240
bufreader . each_chunk do |chunk |
239
241
chunk . each_byte do |c |
@@ -256,7 +258,17 @@ def decode_raw(str_or_io, out: nil)
256
258
if count == 5 && word > 0xffffffff
257
259
raise ( Ascii85 ::DecodingError , "Invalid Ascii85 5-tuple (#{ word } >= 2**32)" )
258
260
elsif count == 5
259
- bufwriter . write ( [ word ] . pack ( 'N' ) )
261
+ b3 = word & 0xff ; word >>= 8
262
+ b2 = word & 0xff ; word >>= 8
263
+ b1 = word & 0xff ; word >>= 8
264
+ b0 = word
265
+
266
+ wordbuf . setbyte ( 0 , b0 )
267
+ wordbuf . setbyte ( 1 , b1 )
268
+ wordbuf . setbyte ( 2 , b2 )
269
+ wordbuf . setbyte ( 3 , b3 )
270
+
271
+ bufwriter . write ( wordbuf )
260
272
261
273
word = 0
262
274
count = 0
@@ -280,7 +292,7 @@ def decode_raw(str_or_io, out: nil)
280
292
count -= 1
281
293
word += lut [ count ]
282
294
283
- bufwriter . write ( ( ( word >> 24 ) & 0xff ) . chr ) if count >= 1
295
+ bufwriter . write ( ( word >> 24 ) . chr ) if count >= 1
284
296
bufwriter . write ( ( ( word >> 16 ) & 0xff ) . chr ) if count >= 2
285
297
bufwriter . write ( ( ( word >> 8 ) & 0xff ) . chr ) if count == 3
286
298
bufwriter . flush
0 commit comments