class PDF::Reader::Filter::Flate

implementation of the Flate (zlib) stream filter

Constants

ZLIB_AUTO_DETECT_ZLIB_OR_GZIP
ZLIB_RAW_DEFLATE

Public Class Methods

new(options = {}) click to toggle source
# File lib/pdf/reader/filter/flate.rb, line 14
def initialize(options = {})
  @options = options
end

Public Instance Methods

filter(data) click to toggle source

Decode the specified data with the Zlib compression algorithm

# File lib/pdf/reader/filter/flate.rb, line 20
def filter(data)
  deflated = zlib_inflate(data) || zlib_inflate(data[0, data.bytesize-1])

  if deflated.nil?
    raise MalformedPDFError,
      "Error while inflating a compressed stream (no suitable inflation algorithm found)"
  end
  Depredict.new(@options).filter(deflated)
end

Private Instance Methods

zlib_inflate(data) click to toggle source
# File lib/pdf/reader/filter/flate.rb, line 32
def zlib_inflate(data)
  begin
    return 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, swallow the exception and attempt to inflate the data as a raw
    # RFC1951 stream.
  end

  begin
    return Zlib::Inflate.new(ZLIB_RAW_DEFLATE).inflate(data)
  rescue StandardError => e
    # swallow this one too, so we can try some other fallback options
  end

  nil
end