sile / libflate

A Rust implementation of DEFLATE algorithm and related formats (ZLIB, GZIP)
https://docs.rs/libflate
MIT License
177 stars 35 forks source link

panic with binary file #1

Closed jsen- closed 7 years ago

jsen- commented 7 years ago
[package]
name = "compression_test"
version = "0.1.0"

[dependencies]
libflate = "0.1"
extern crate libflate;

use std::fs::File;
use std::io::copy;
use libflate::gzip::Encoder;

fn main() {

    let mut out_file = File::create("mingw-w64-install.exe.gz").unwrap();
    let mut encoder = Encoder::new(&mut out_file).unwrap();
    let mut in_file = File::open("mingw-w64-install.exe").unwrap();
    copy(&mut in_file, &mut encoder).unwrap();
    encoder.finish().into_result().unwrap();
}
Running "cargo run":
    Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target\debug\compression_test.exe`
thread 'main' panicked at 'symbol:17, table:17', C:\Users\root\.cargo\registry\src\github.com-1ecc6299db9ec823\libflate-0.1.0\src\huffman.rs:188
stack backtrace:
   0:           0x479c64 - <unknown>
   1:           0x476529 - <unknown>
...
  17:           0x4014e7 - <unknown>
  18:     0x7ffa91a08363 - <unknown>
error: process didn't exit successfully: `target\debug\compression_test.exe` (exit code: 101)

"cargo run" completed with code 101
It took approximately 0.901 seconds

Please ignore the <unknown> stack frames, this is known issue with *-pc-windows-gnu target. The file I'm testing with is mingw-w64-install.exe but happened on basically any binary file I tried.

jsen- commented 7 years ago

Looks like it's not the compression ... I tried something crazy and used a super naive replacement of compression algo

[package]
name = "compression_test2"
version = "0.1.0"

[dependencies]
libflate = "0.1"
deflate = "0.5"
extern crate libflate;
extern crate deflate;

use std::fs::File;
use std::io::copy;
use libflate::deflate::{Encoder, EncodeOptions};

struct MyEncoder { vec: Vec<u8> }

impl libflate::lz77::Lz77Encode for MyEncoder {
    fn encode<S>(&mut self, buf: &[u8], mut _sink: S)
        where S: libflate::lz77::Sink
    {
        self.vec.extend_from_slice(buf);
    }

    fn flush<S>(&mut self, mut sink: S)
        where S: libflate::lz77::Sink
    {
        for c in &self.vec {
            sink.consume(libflate::lz77::Code::Literal(*c));
        }
    }
}

fn main() {
    let out_file = File::create("foo.tar.gz").unwrap();
    let mut encoder = Encoder::with_options(out_file, EncodeOptions::with_lz77(MyEncoder { vec: Vec::new() }));
    let mut in_file = File::open("mingw-w64-install.exe").unwrap();
    copy(&mut in_file, &mut encoder).unwrap();
    encoder.finish().into_result().unwrap();
}

but ...

Running "cargo run":
    Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target\debug\compression_test2.exe`
thread 'main' panicked at 'symbol:17, table:17', C:\Users\root\.cargo\registry\src\github.com-1ecc6299db9ec823\libflate-0.1.0\src\huffman.rs:188
stack backtrace:
   0:           0x46d914 - <unknown>
   1:           0x46a1d9 - <unknown>
...
  16:           0x4014e7 - <unknown>
  17:     0x7ffa91a08363 - <unknown>
error: process didn't exit successfully: `target\debug\compression_test2.exe` (exit code: 101)

"cargo run" completed with code 101
It took approximately 0.354 seconds
sile commented 7 years ago

Thank you for your report. I fixed the bug in https://github.com/sile/libflate/commit/bc68c99af4663be979e9bf1c6da1fa2c2ccf58ae .