From 1b2a153177b021d0fa1d32519a67b10ad97f099e Mon Sep 17 00:00:00 2001 From: Daniel Reiter Horn Date: Sun, 10 Feb 2019 22:53:16 -0800 Subject: [PATCH] Fix #39 . added into_inner --- Cargo.toml | 4 +- README.md | 4 + src/bin/integration_tests.rs | 1 - src/enc/reader.rs | 203 +++++++---------------------------- src/enc/test.rs | 1 - src/enc/writer.rs | 22 ++-- 6 files changed, 58 insertions(+), 177 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a2a134b5..42d5087e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "brotli" -version = "3.1.8" +version = "3.2.0" authors = ["Daniel Reiter Horn ", "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" @@ -27,7 +27,7 @@ incremental=false [dependencies] "alloc-no-stdlib" = {version="2.0"} -"brotli-decompressor" = {version="~2.1", default-features=false} +"brotli-decompressor" = {version="~2.2", default-features=false} "alloc-stdlib" = {version="~0.2", optional=true} "packed_simd" = {version="0.3", optional=true} "sha2" = {version="~0.8", optional=true} diff --git a/README.md b/README.md index 7d11e187..651904bf 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,10 @@ [![crates.io](http://meritbadge.herokuapp.com/brotli)](https://crates.io/crates/brotli) [![Build Status](https://travis-ci.org/dropbox/rust-brotli.svg?branch=master)](https://travis-ci.org/dropbox/rust-brotli) + +## What's new in 3.2 +* into_inner conversions for both Reader and Writer classes + ## What's new in 3.0 * A fully compatible FFI for drop-in compatibiltiy with the https://github.com/google/brotli binaries * custom allocators fully supported diff --git a/src/bin/integration_tests.rs b/src/bin/integration_tests.rs index 834204af..e85cee13 100755 --- a/src/bin/integration_tests.rs +++ b/src/bin/integration_tests.rs @@ -183,7 +183,6 @@ fn decompress_internal(r: &mut InputType, available_out = output.slice().len() } } - brotli_state.BrotliStateCleanup(); }); if timing_error { let _r = super::writeln0(&mut io::stderr(), "Timing error"); diff --git a/src/enc/reader.rs b/src/enc/reader.rs index 977cfe7a..b6aa3d0d 100755 --- a/src/enc/reader.rs +++ b/src/enc/reader.rs @@ -59,6 +59,9 @@ impl &R { &self.0.get_ref().0 } + pub fn into_inner(self) -> R { + self.0.into_inner().0 + } } #[cfg(feature="std")] @@ -93,13 +96,16 @@ impl CompressorReader { pub fn with_params(r: R, buffer_size: usize, params: &BrotliEncoderParams) -> Self { let mut reader = Self::new(r, buffer_size, params.quality as u32, params.lgwin as u32); - (reader.0).0.state.params = params.clone(); + (reader.0).0.state.0.params = params.clone(); reader } pub fn get_ref(&self) -> &R { self.0.get_ref() } + pub fn into_inner(self) -> R { + self.0.into_inner() + } } @@ -123,7 +129,14 @@ pub struct CompressorReaderCustomIo, - state: BrotliEncoderStateStruct, + state: StateWrapper, +} +struct StateWrapper(BrotliEncoderStateStruct); + +impl Drop for StateWrapper { + fn drop(&mut self) { + BrotliEncoderDestroyInstance(&mut self.0); + } } impl input_len : 0, input_eof : false, input: r, - state : BrotliEncoderCreateInstance(alloc), + state : StateWrapper(BrotliEncoderCreateInstance(alloc)), error_if_invalid_data : Some(invalid_data_error_type), }; - BrotliEncoderSetParameter(&mut ret.state, + BrotliEncoderSetParameter(&mut ret.state.0, BrotliEncoderParameter::BROTLI_PARAM_QUALITY, q as (u32)); - BrotliEncoderSetParameter(&mut ret.state, + BrotliEncoderSetParameter(&mut ret.state.0, BrotliEncoderParameter::BROTLI_PARAM_LGWIN, lgwin as (u32)); @@ -169,21 +182,26 @@ CompressorReaderCustomIo self.input_offset = 0; } } - + pub fn into_inner(self) -> R{ + match self { + CompressorReaderCustomIo { + input_buffer:_ib, + total_out:_to, + input_offset: _io, + input_len: _len, + input, + input_eof:_ieof, + error_if_invalid_data: _eiid, + state: _state, + } => { + input + } + } + } pub fn get_ref(&self) -> &R { &self.input } } - -impl, - BufferType : SliceWrapperMut, - Alloc: BrotliAlloc> Drop for -CompressorReaderCustomIo { - fn drop(&mut self) { - BrotliEncoderDestroyInstance(&mut self.state); - } -} impl, BufferType : SliceWrapperMut, @@ -215,7 +233,7 @@ CompressorReaderCustomIo { op = BrotliEncoderOperation::BROTLI_OPERATION_PROCESS; } let ret = BrotliEncoderCompressStream( - &mut self.state, + &mut self.state.0, op, &mut avail_in, &self.input_buffer.slice_mut()[..], @@ -231,7 +249,7 @@ CompressorReaderCustomIo { if ret <= 0 { return Err(self.error_if_invalid_data.take().unwrap()); } - let fin = BrotliEncoderIsFinished(&mut self.state); + let fin = BrotliEncoderIsFinished(&mut self.state.0); if fin != 0 { break; } @@ -241,152 +259,3 @@ CompressorReaderCustomIo { } - -/* - -/////////////////BOGUS//////////////////////////////////// -pub struct SimpleReader> -{ - input_buffer: BufferType, - total_out: Option, - input_offset: usize, - input_len: usize, - input_eof: bool, - input: R, - error_if_invalid_data: Option, - read_error: Option, - state: BrotliEncoderStateStruct, HeapAlloc, HeapAlloc, HeapAlloc, HeapAlloc>, -} - -impl> -SimpleReader -{ - - pub fn new(r: R, buffer : BufferType, - q: u32, - lgwin: u32) -> Option { - let mut ret = SimpleReader{ - input_buffer : buffer, - total_out : Some(0), - input_offset : 0, - input_len : 0, - input_eof : false, - input: r, - state : BrotliEncoderCreateInstance(HeapAlloc{default_value:0}, - HeapAlloc{default_value:0}, - HeapAlloc{default_value:0}, - HeapAlloc{default_value:0}, - HeapAlloc{default_value:Command::default()}), - error_if_invalid_data : Some(Error::new(ErrorKind::InvalidData, - "Invalid Data")), - read_error : None, - }; - BrotliEncoderSetParameter(&mut ret.state, - BrotliEncoderParameter::BROTLI_PARAM_QUALITY, - q as (u32)); - BrotliEncoderSetParameter(&mut ret.state, - BrotliEncoderParameter::BROTLI_PARAM_LGWIN, - lgwin as (u32)); - - Some(ret) - } - pub fn copy_to_front(&mut self) { - let avail_in = self.input_len - self.input_offset; - if self.input_offset == self.input_buffer.slice_mut().len() { - self.input_offset = 0; - self.input_len = 0; - } else if self.input_offset + 256 > self.input_buffer.slice_mut().len() && avail_in < self.input_offset { - let (first, second) = self.input_buffer.slice_mut().split_at_mut(self.input_offset); - first[0..avail_in].clone_from_slice(&second[0..avail_in]); - self.input_len -= self.input_offset; - self.input_offset = 0; - } - } - - pub fn get_ref(&self) -> &R { - &self.input - } -} - -impl> Drop for -SimpleReader { - fn drop(&mut self) { - BrotliEncoderDestroyInstance(&mut self.state); - } -} -impl> Read for -SimpleReader { - fn read(&mut self, buf: &mut [u8]) -> Result { - let mut nop_callback = |_data:&[interface::Command]|(); - let mut output_offset : usize = 0; - let mut avail_out = buf.len() - output_offset; - let mut avail_in = self.input_len - self.input_offset; - let mut needs_input = false; - while avail_out == buf.len() && (!needs_input || !self.input_eof) { - if self.input_len < self.input_buffer.slice_mut().len() && !self.input_eof { - match self.input.read(&mut self.input_buffer.slice_mut()[self.input_len..]) { - Err(e) => { - self.read_error = Some(e); - self.input_eof = true; - }, - Ok(size) => if size == 0 { - self.input_eof = true; - }else { - needs_input = false; - self.input_len += size; - avail_in = self.input_len - self.input_offset; - }, - } - } - let op : BrotliEncoderOperation; - if avail_in == 0 { - op = BrotliEncoderOperation::BROTLI_OPERATION_FINISH; - } else { - op = BrotliEncoderOperation::BROTLI_OPERATION_PROCESS; - } - let ret = BrotliEncoderCompressStream( - &mut self.state, - &mut HeapAlloc{default_value:0}, - &mut HeapAlloc{default_value:0.0}, - &mut HeapAlloc{default_value:Mem256f::default()}, - &mut HeapAlloc{default_value:HistogramLiteral::default()}, - &mut HeapAlloc{default_value:HistogramCommand::default()}, - &mut HeapAlloc{default_value:HistogramDistance::default()}, - &mut HeapAlloc{default_value:HistogramPair::default()}, - &mut HeapAlloc{default_value:ContextType::default()}, - &mut HeapAlloc{default_value:HuffmanTree::default()}, - &mut HeapAlloc{default_value:ZopfliNode::default()}, - op, - &mut avail_in, - &self.input_buffer.slice_mut()[..], - &mut self.input_offset, - &mut avail_out, - buf, - &mut output_offset, - &mut self.total_out, - &mut nop_callback); - if avail_in == 0 { - match self.read_error.take() { - Some(err) => return Err(err), - None => { - needs_input = true; - self.copy_to_front(); - }, - } - } - if ret <= 0 { - return Err(self.error_if_invalid_data.take().unwrap()); - } - let fin = BrotliEncoderIsFinished(&mut self.state); - if fin != 0 { - break; - } - } - Ok(output_offset) - } -} - */ diff --git a/src/enc/test.rs b/src/enc/test.rs index 9c5eb822..fb2a5c24 100755 --- a/src/enc/test.rs +++ b/src/enc/test.rs @@ -219,7 +219,6 @@ fn oneshot_decompress(compressed: &[u8], mut output: &mut [u8]) -> (BrotliResult &mut output, &mut written, &mut brotli_state); - brotli_state.BrotliStateCleanup(); return (result, input_offset, output_offset); } diff --git a/src/enc/writer.rs b/src/enc/writer.rs index e001d932..1d02d77f 100755 --- a/src/enc/writer.rs +++ b/src/enc/writer.rs @@ -1,4 +1,5 @@ #![cfg_attr(not(feature="std"), allow(unused_imports))] +use core; use super::encode::{BrotliEncoderCreateInstance, BrotliEncoderDestroyInstance, BrotliEncoderParameter, BrotliEncoderSetParameter, BrotliEncoderOperation, BrotliEncoderStateStruct, BrotliEncoderCompressStream, BrotliEncoderIsFinished}; @@ -56,6 +57,9 @@ impl &W { &self.0.get_ref().0 } + pub fn into_inner(self) -> W { + self.0.into_inner().0 + } } #[cfg(feature="std")] @@ -101,6 +105,9 @@ impl CompressorWriter { pub fn get_ref(&self) -> &W { self.0.get_ref() } + pub fn into_inner(self) -> W { + self.0.into_inner() + } } @@ -122,7 +129,7 @@ pub struct CompressorWriterCustomIo, - output: W, + output: Option, error_if_invalid_data: Option, state: BrotliEncoderStateStruct, } @@ -150,7 +157,7 @@ CompressorWriterCustomIo let mut ret = CompressorWriterCustomIo{ output_buffer : buffer, total_out : Some(0), - output: w, + output: Some(w), state : BrotliEncoderCreateInstance(alloc), error_if_invalid_data : Some(invalid_data_error_type), }; @@ -185,7 +192,7 @@ CompressorWriterCustomIo &mut self.total_out, &mut nop_callback); if output_offset > 0 { - match write_all(&mut self.output, &self.output_buffer.slice_mut()[..output_offset]) { + match write_all(self.output.as_mut().unwrap(), &self.output_buffer.slice_mut()[..output_offset]) { Ok(_) => {}, Err(e) => return Err(e), } @@ -203,7 +210,10 @@ CompressorWriterCustomIo } pub fn get_ref(&self) -> &W { - &self.output + self.output.as_ref().unwrap() + } + pub fn into_inner(mut self) -> W { + core::mem::replace(&mut self.output, None).unwrap() } } @@ -246,7 +256,7 @@ CompressorWriterCustomIo { &mut self.total_out, &mut nop_callback); if output_offset > 0 { - match write_all(&mut self.output, &self.output_buffer.slice_mut()[..output_offset]) { + match write_all(self.output.as_mut().unwrap(), &self.output_buffer.slice_mut()[..output_offset]) { Ok(_) => {}, Err(e) => return Err(e), } @@ -262,6 +272,6 @@ CompressorWriterCustomIo { Ok(_) => {}, Err(e) => return Err(e), } - self.output.flush() + self.output.as_mut().unwrap().flush() } }