From 65bfba3f6234bebc406e47487b765160054ff56f Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 7 Aug 2012 09:49:21 -0400 Subject: [PATCH] making the ruby unpacker have the same outcome as unpack.c Unfortunately the ruby Zlib::ZStream internals are not really accessible like the C functions --- unpack.rb | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/unpack.rb b/unpack.rb index e798ee7..9c05cff 100755 --- a/unpack.rb +++ b/unpack.rb @@ -1,13 +1,17 @@ #!/usr/bin/env ruby -$:.unshift(File.dirname(__FILE__)) -require 'huffman.rb' -require 'thing.rb' +# stdlib +require 'stringio' +require 'zlib' def inflate(data) Zlib::Inflate.inflate(data) end +def deflate(data) + Zlib::Deflate.deflate(data) +end + # there is not a difference for us, in these two def inflate2(data) zlib = Zlib::Inflate.new(15) @@ -27,14 +31,21 @@ if $0 == __FILE__ ARGV.each do |arg| file = File.open(arg) - z_data = file.read() - data = inflate(z_data) + z_data_io = StringIO.new(file.read()) + data = inflate(z_data_io.read()) + e_pos = deflate(data).bytesize() + z_data_io.seek(e_pos) + puts "data is:" puts load_dictionary(data).map {|x| "\t#{x}" } puts "dictionary stats:" - puts "\tcompressed size: %d" % z_data.bytesize() + puts "\tcompressed size: %d" % deflate(data).bytesize() puts "\tuncompressed size: %d" % data.bytesize() + + buf = z_data_io.read() + puts "Read %d bytes\n" % buf.bytesize() + end end