Skip to content

Commit

Permalink
fix #37
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrh committed Jan 31, 2019
1 parent 3014337 commit 41f8e9f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "brotli"
version = "3.1.7"
version = "3.1.8"
authors = ["Daniel Reiter Horn <danielrh@dropbox.com>", "The Brotli Authors"]
description = "A brotli compressor and decompressor that with an interface avoiding the rust stdlib. This makes it suitable for embedded devices and kernels. It is designed with a pluggable allocator so that the standard lib's allocator may be employed. The default build also includes a stdlib allocator and stream interface. Disable this with --features=no-stdlib. All included code is safe."
license = "BSD-3-Clause/MIT"
Expand All @@ -23,6 +23,7 @@ name = "catbrotli"

[profile.release]
lto=true
incremental=false

[dependencies]
"alloc-no-stdlib" = {version="2.0"}
Expand Down
35 changes: 35 additions & 0 deletions src/bin/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1238,3 +1238,38 @@ fn test_empty18() {
65536,
65536);
}

pub struct SoonErrorReader(&'static[u8], bool);
impl std::io::Read for SoonErrorReader {
fn read(&mut self, data:&mut [u8]) -> std::io::Result<usize> {
let first = self.1;
self.1 = false;
if first {
let len = core::cmp::min(self.0.len(),data.len());
data[..len].clone_from_slice(&self.0[..len]);
return Ok(len);
}
Err(io::Error::new(io::ErrorKind::PermissionDenied, "err"))
}
}
#[test]
fn test_error_returned() {
static onetwothreefourfive: [u8;5] = [1,2,3,4,5];
let params = super::brotli::enc::BrotliEncoderParams::default();
let mut erroring = SoonErrorReader(&onetwothreefourfive[..], true);
let mut br = UnlimitedBuffer::new(&[]);
let dict = &[];
let err = super::compress(&mut erroring, &mut br, 4096, &params, dict, 1);
if let Ok(_x) = err {
panic!("Should have errored {:?}\n", err);
}
let mut output = UnlimitedBuffer::new(&[]);
let mut br_copy = Buffer::new(&br.data[..]);
assert_eq!(br.data.len(),9);
match decompress_internal(&mut br_copy, &mut output, 1, 1, &mut Passthrough{}) {
Ok(_) => {}
Err(e) => panic!("Error {:?}", e),
}
assert_eq!(output.data, &onetwothreefourfive[..]);
}

1 change: 1 addition & 0 deletions src/bin/test_custom_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ static RANDOM_THEN_UNICODE : &'static [u8] = include_bytes!("../../testdata/rand
static ALICE: &'static[u8] = include_bytes!("../../testdata/alice29.txt");
use super::Rebox;


#[test]
fn test_custom_dict() {
let mut raw = UnlimitedBuffer::new(ALICE);
Expand Down
20 changes: 17 additions & 3 deletions src/enc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ pub fn BrotliCompressCustomIoCustomDict<ErrType,
let mut next_in_offset: usize = 0;
let mut next_out_offset: usize = 0;
let mut total_out = Some(0usize);
let mut read_err: Result<(), ErrType> = Ok(());
{
let s = &mut s_orig;

Expand All @@ -246,7 +247,8 @@ pub fn BrotliCompressCustomIoCustomDict<ErrType,
if available_in == 0 && !eof {
next_in_offset = 0;
match r.read(input_buffer) {
Err(_) => {
Err(e) => {
read_err = Err(e);
available_in = 0;
eof = true;
},
Expand Down Expand Up @@ -281,7 +283,13 @@ pub fn BrotliCompressCustomIoCustomDict<ErrType,
next_out_offset = 0;
while next_out_offset < lim {
match w.write(&mut output_buffer[next_out_offset..lim]) {
Err(e) => return Err(e),
Err(e) => {
BrotliEncoderDestroyInstance(s);
if let Err(err) = read_err {
return Err(err);
}
return Err(e);
}
Ok(size) => {
next_out_offset += size;
}
Expand All @@ -291,14 +299,20 @@ pub fn BrotliCompressCustomIoCustomDict<ErrType,
next_out_offset = 0;
}
if result <= 0 {
return Err(unexpected_eof_error_constant);
if let Ok(_) = read_err {
read_err = Err(unexpected_eof_error_constant);
}
break;
}
if fin != 0 {
break;
}
}
BrotliEncoderDestroyInstance(s);
}
if let Err(err) = read_err {
return Err(err)
}
Ok(total_out.unwrap())
}

0 comments on commit 41f8e9f

Please sign in to comment.