Skip to content

Commit

Permalink
Fix #39 . added into_inner
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrh committed Feb 11, 2019
1 parent de92a5e commit 1b2a153
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 177 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "brotli"
version = "3.1.8"
version = "3.2.0"
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 @@ -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}
Expand Down
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/bin/integration_tests.rs
Expand Up @@ -183,7 +183,6 @@ fn decompress_internal<InputType, OutputType, Run: Runner>(r: &mut InputType,
available_out = output.slice().len()
}
}
brotli_state.BrotliStateCleanup();
});
if timing_error {
let _r = super::writeln0(&mut io::stderr(), "Timing error");
Expand Down
203 changes: 36 additions & 167 deletions src/enc/reader.rs
Expand Up @@ -59,6 +59,9 @@ impl<R: Read,
pub fn get_ref(&self) -> &R {
&self.0.get_ref().0
}
pub fn into_inner(self) -> R {
self.0.into_inner().0
}
}

#[cfg(feature="std")]
Expand Down Expand Up @@ -93,13 +96,16 @@ impl<R: Read> CompressorReader<R> {

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()
}
}


Expand All @@ -123,7 +129,14 @@ pub struct CompressorReaderCustomIo<ErrType,
input: R,
input_eof: bool,
error_if_invalid_data: Option<ErrType>,
state: BrotliEncoderStateStruct<Alloc>,
state: StateWrapper<Alloc>,
}
struct StateWrapper<Alloc:BrotliAlloc>(BrotliEncoderStateStruct<Alloc>);

impl<Alloc: BrotliAlloc> Drop for StateWrapper <Alloc> {
fn drop(&mut self) {
BrotliEncoderDestroyInstance(&mut self.0);
}
}

impl<ErrType,
Expand All @@ -145,13 +158,13 @@ CompressorReaderCustomIo<ErrType, R, BufferType, Alloc>
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));

Expand All @@ -169,21 +182,26 @@ CompressorReaderCustomIo<ErrType, R, BufferType, Alloc>
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<ErrType,
R: CustomRead<ErrType>,
BufferType : SliceWrapperMut<u8>,
Alloc: BrotliAlloc> Drop for
CompressorReaderCustomIo<ErrType, R, BufferType, Alloc> {
fn drop(&mut self) {
BrotliEncoderDestroyInstance(&mut self.state);
}
}
impl<ErrType,
R: CustomRead<ErrType>,
BufferType : SliceWrapperMut<u8>,
Expand Down Expand Up @@ -215,7 +233,7 @@ CompressorReaderCustomIo<ErrType, R, BufferType, Alloc> {
op = BrotliEncoderOperation::BROTLI_OPERATION_PROCESS;
}
let ret = BrotliEncoderCompressStream(
&mut self.state,
&mut self.state.0,
op,
&mut avail_in,
&self.input_buffer.slice_mut()[..],
Expand All @@ -231,7 +249,7 @@ CompressorReaderCustomIo<ErrType, R, BufferType, Alloc> {
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;
}
Expand All @@ -241,152 +259,3 @@ CompressorReaderCustomIo<ErrType, R, BufferType, Alloc> {
}



/*
/////////////////BOGUS////////////////////////////////////
pub struct SimpleReader<R: Read,
BufferType: SliceWrapperMut<u8>>
{
input_buffer: BufferType,
total_out: Option<usize>,
input_offset: usize,
input_len: usize,
input_eof: bool,
input: R,
error_if_invalid_data: Option<io::Error>,
read_error: Option<io::Error>,
state: BrotliEncoderStateStruct<HeapAlloc<u8>, HeapAlloc<u16>, HeapAlloc<u32>, HeapAlloc<i32>, HeapAlloc<Command>>,
}
impl<R: Read,
BufferType : SliceWrapperMut<u8>>
SimpleReader<R, BufferType>
{
pub fn new(r: R, buffer : BufferType,
q: u32,
lgwin: u32) -> Option<Self> {
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<R: Read,
BufferType : SliceWrapperMut<u8>> Drop for
SimpleReader<R, BufferType> {
fn drop(&mut self) {
BrotliEncoderDestroyInstance(&mut self.state);
}
}
impl<R: Read,
BufferType : SliceWrapperMut<u8>> Read for
SimpleReader<R, BufferType> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
let mut nop_callback = |_data:&[interface::Command<input_pair::InputReferenceMut>]|();
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)
}
}
*/
1 change: 0 additions & 1 deletion src/enc/test.rs
Expand Up @@ -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);

}
Expand Down
22 changes: 16 additions & 6 deletions 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};
Expand Down Expand Up @@ -56,6 +57,9 @@ impl<W: Write,
pub fn get_ref(&self) -> &W {
&self.0.get_ref().0
}
pub fn into_inner(self) -> W {
self.0.into_inner().0
}
}

#[cfg(feature="std")]
Expand Down Expand Up @@ -101,6 +105,9 @@ impl<W: Write> CompressorWriter<W> {
pub fn get_ref(&self) -> &W {
self.0.get_ref()
}
pub fn into_inner(self) -> W {
self.0.into_inner()
}
}


Expand All @@ -122,7 +129,7 @@ pub struct CompressorWriterCustomIo<ErrType,
{
output_buffer: BufferType,
total_out: Option<usize>,
output: W,
output: Option<W>,
error_if_invalid_data: Option<ErrType>,
state: BrotliEncoderStateStruct<Alloc>,
}
Expand Down Expand Up @@ -150,7 +157,7 @@ CompressorWriterCustomIo<ErrType, W, BufferType, Alloc>
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),
};
Expand Down Expand Up @@ -185,7 +192,7 @@ CompressorWriterCustomIo<ErrType, W, BufferType, Alloc>
&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),
}
Expand All @@ -203,7 +210,10 @@ CompressorWriterCustomIo<ErrType, W, BufferType, Alloc>
}

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()
}
}

Expand Down Expand Up @@ -246,7 +256,7 @@ CompressorWriterCustomIo<ErrType, W, BufferType, Alloc> {
&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),
}
Expand All @@ -262,6 +272,6 @@ CompressorWriterCustomIo<ErrType, W, BufferType, Alloc> {
Ok(_) => {},
Err(e) => return Err(e),
}
self.output.flush()
self.output.as_mut().unwrap().flush()
}
}

0 comments on commit 1b2a153

Please sign in to comment.