Class: PDF::Reader::Filter::Flate

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader/filter/flate.rb

Overview

implementation of the Flate (zlib) stream filter

Constant Summary collapse

ZLIB_AUTO_DETECT_ZLIB_OR_GZIP =

Zlib::MAX_WBITS + 32

47

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Flate

Returns a new instance of Flate.



13
14
15
# File 'lib/pdf/reader/filter/flate.rb', line 13

def initialize(options = {})
  @options = options
end

Instance Method Details

#filter(data) ⇒ Object

Decode the specified data with the Zlib compression algorithm



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pdf/reader/filter/flate.rb', line 19

def filter(data)
  deflated = nil
  begin
    deflated = Zlib::Inflate.new(ZLIB_AUTO_DETECT_ZLIB_OR_GZIP).inflate(data)
  rescue Zlib::DataError => e
    # by default, Ruby's Zlib assumes the data it's inflating
    # is RFC1951 deflated data, wrapped in a RFC1950 zlib container. If that
    # fails, then use a lightly-documented 'feature' to attempt to inflate
    # the data as a raw RFC1951 stream.
    #
    # See
    # - http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/243545
    deflated = Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(data)
  end
  Depredict.new(@options).filter(deflated)
rescue Exception => e
  # Oops, there was a problem inflating the stream
  raise MalformedPDFError,
    "Error occured while inflating a compressed stream (#{e.class.to_s}: #{e.to_s})"
end