Class: RHC::Vendor::Zlib::Inflate

Inherits:
ZStream show all
Defined in:
lib/rhc/vendor/zliby.rb

Overview

DEFLATE Decompression

Implements decompression of a RFC-1951 compatible stream.

Defined Under Namespace

Classes: HuffmanTree

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ZStream

#adler, #avail_in, #avail_out, #avail_out=, #close, #closed?, #data_type, #end, #ended?, #finished?, #flush_next_in, #flush_next_out, #reset, #stream_end?, #total_in, #total_out

Constructor Details

#initialize(window_bits = MAX_WBITS) ⇒ Inflate

Returns a new instance of Inflate.



192
193
194
195
196
197
198
199
200
# 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

Class Method Details

.inflate(zstring) ⇒ Object



485
486
487
488
# File 'lib/rhc/vendor/zliby.rb', line 485

def inflate zstring
  d = self.new
  d.inflate zstring
end

Instance Method Details

#<<(string) ⇒ Object

Appends data to the input stream



203
204
205
206
# File 'lib/rhc/vendor/zliby.rb', line 203

def <<(string)
  @zstring << string
  inflate
end

#finishObject

Finishes inflating and flushes the buffer



268
269
270
271
272
273
274
# 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) ⇒ Object

Example

f = File.open “example.z” i = Inflate.new i.inflate f.read



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# 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) ⇒ Object

Sets the inflate dictionary



209
210
211
212
# File 'lib/rhc/vendor/zliby.rb', line 209

def set_dictionary string
  @dict = string
  reset
end