2021-05-24 01:24:22 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-12-21 18:02:01 +08:00
|
|
|
module Zip
|
|
|
|
class DecryptedIo #:nodoc:all
|
|
|
|
CHUNK_SIZE = 32_768
|
|
|
|
|
|
|
|
def initialize(io, decrypter)
|
|
|
|
@io = io
|
|
|
|
@decrypter = decrypter
|
|
|
|
end
|
|
|
|
|
2020-02-05 10:30:30 +08:00
|
|
|
def read(length = nil, outbuf = +'')
|
2020-02-10 06:44:08 +08:00
|
|
|
return (length.nil? || length.zero? ? '' : nil) if eof
|
2019-12-21 18:02:01 +08:00
|
|
|
|
|
|
|
while length.nil? || (buffer.bytesize < length)
|
|
|
|
break if input_finished?
|
2020-02-09 21:13:21 +08:00
|
|
|
|
2019-12-21 18:02:01 +08:00
|
|
|
buffer << produce_input
|
|
|
|
end
|
|
|
|
|
|
|
|
outbuf.replace(buffer.slice!(0...(length || output_buffer.bytesize)))
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def eof
|
|
|
|
buffer.empty? && input_finished?
|
|
|
|
end
|
|
|
|
|
|
|
|
def buffer
|
2020-02-05 10:40:56 +08:00
|
|
|
@buffer ||= +''
|
2019-12-21 18:02:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def input_finished?
|
|
|
|
@io.eof
|
|
|
|
end
|
|
|
|
|
|
|
|
def produce_input
|
|
|
|
@decrypter.decrypt(@io.read(CHUNK_SIZE))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|