Module: ZSteg::Checker::Zlib

Defined in:
lib/zsteg/checker/zlib.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

MIN_UNPACKED_SIZE =
4

Class Method Summary collapse

Class Method Details

.check_data(data) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zsteg/checker/zlib.rb', line 23

def self.check_data data
  return unless idx = data.index(/\x78[\x9c\xda\x01]/n)

  zi = ::Zlib::Inflate.new
  x = zi.inflate data[idx..-1]
  # decompress OK
  return Result.new x, idx if x.size >= MIN_UNPACKED_SIZE
rescue ::Zlib::BufError
  # tried to decompress, but got EOF - need more data
  return Result.new x, idx
rescue ::Zlib::DataError, ::Zlib::NeedDict
  # not a zlib
ensure
  zi.close if zi && !zi.closed?
end