class RHC::Vendor::Zlib::Inflate

DEFLATE Decompression

Implements decompression of a RFC-1951 compatible stream.

Public Class Methods

new(window_bits=MAX_WBITS) click to toggle source
# File lib/rhc/vendor/zliby.rb, line 192
def initialize window_bits=MAX_WBITS
  @w_bits = window_bits  
  if @w_bits < 0 then 
    @rawdeflate = true
    @w_bits *= -1 
  end  
  super()
  @zstring = ""
end

Public Instance Methods

<<(string) click to toggle source

Appends data to the input stream

# File lib/rhc/vendor/zliby.rb, line 203
def <<(string)
  @zstring << string
  inflate
end
finish() click to toggle source

Finishes inflating and flushes the buffer

# File lib/rhc/vendor/zliby.rb, line 268
def finish
  output = ""
  inflate unless @output_buffer.length > 0
  @output_buffer.each {|c| output << c } 
  super
  output
end
inflate(zstring=nil) click to toggle source

Example

f = File.open "example.z"
i = Inflate.new
i.inflate f.read
# File lib/rhc/vendor/zliby.rb, line 218
def inflate zstring=nil
  @zstring = zstring unless zstring.nil?
  #We can't use unpack, IronRuby doesn't have it yet.
  @zstring.each_byte {|b| @input_buffer << b}
  
   unless @rawdeflate then

  compression_method_and_flags = @input_buffer[@in_pos+=1]
  flags = @input_buffer[@in_pos+=1]
  
  #CMF and FLG, when viewed as a 16-bit unsigned integer stored inMSB order (CMF*256 + FLG), is a multiple of 31
  if ((compression_method_and_flags << 0x08) + flags) % 31 != 0 then raise Zlib::DataError.new("incorrect header check") end
  
  #CM = 8 denotes the "deflate" compression method with a window size up to 32K. (RFC's only specify CM 8)
  compression_method = compression_method_and_flags & 0x0F 
  
  if compression_method != Z_DEFLATED then raise Zlib::DataError.new("unknown compression method") end
  
  #For CM = 8, CINFO is the base-2 logarithm of the LZ77 window size,minus eight (CINFO=7 indicates a 32K window size)
  compression_info = compression_method_and_flags >> 0x04 
  
  if (compression_info + 8) > @w_bits then raise Zlib::DataError.new("invalid window size") end
  
  preset_dictionary_flag = ((flags & 0x20) >> 0x05) == 1
  compression_level = (flags & 0xC0) >> 0x06
  
  if preset_dictionary_flag and @dict.nil? then raise Zlib::NeedDict.new "Preset dictionary needed!" end
  
  #TODO:  Add Preset dictionary support
  if preset_dictionary_flag then 
    @dict_crc = @input_buffer[@in_pos+=1] << 24 | @input_buffer[@in_pos+=1] << 16 | @input_buffer[@in_pos+=1] << 8 | @input_buffer[@in_pos+=1]
   end
  
  end
  last_block = false
  #Begin processing DEFLATE stream
  until last_block
    last_block = (get_bits(1) == 1)
    block_type = get_bits(2)
    case block_type
      when 0 then no_compression
      when 1 then fixed_codes
      when 2 then dynamic_codes
       when 3 then raise Zlib::DataError.new("invalid block type")   
    end  
  end
  finish
end
set_dictionary(string) click to toggle source

Sets the inflate dictionary

# File lib/rhc/vendor/zliby.rb, line 209
def set_dictionary string
  @dict = string
  reset
end