Skip to content

Commit 1b2a153

Browse files
committed
Fix #39 . added into_inner
1 parent de92a5e commit 1b2a153

File tree

6 files changed

+58
-177
lines changed

6 files changed

+58
-177
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "brotli"
3-
version = "3.1.8"
3+
version = "3.2.0"
44
authors = ["Daniel Reiter Horn <danielrh@dropbox.com>", "The Brotli Authors"]
55
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."
66
license = "BSD-3-Clause/MIT"
@@ -27,7 +27,7 @@ incremental=false
2727

2828
[dependencies]
2929
"alloc-no-stdlib" = {version="2.0"}
30-
"brotli-decompressor" = {version="~2.1", default-features=false}
30+
"brotli-decompressor" = {version="~2.2", default-features=false}
3131
"alloc-stdlib" = {version="~0.2", optional=true}
3232
"packed_simd" = {version="0.3", optional=true}
3333
"sha2" = {version="~0.8", optional=true}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
[![crates.io](http://meritbadge.herokuapp.com/brotli)](https://crates.io/crates/brotli)
44
[![Build Status](https://travis-ci.org/dropbox/rust-brotli.svg?branch=master)](https://travis-ci.org/dropbox/rust-brotli)
55

6+
7+
## What's new in 3.2
8+
* into_inner conversions for both Reader and Writer classes
9+
610
## What's new in 3.0
711
* A fully compatible FFI for drop-in compatibiltiy with the https://github.com/google/brotli binaries
812
* custom allocators fully supported

src/bin/integration_tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ fn decompress_internal<InputType, OutputType, Run: Runner>(r: &mut InputType,
183183
available_out = output.slice().len()
184184
}
185185
}
186-
brotli_state.BrotliStateCleanup();
187186
});
188187
if timing_error {
189188
let _r = super::writeln0(&mut io::stderr(), "Timing error");

src/enc/reader.rs

Lines changed: 36 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ impl<R: Read,
5959
pub fn get_ref(&self) -> &R {
6060
&self.0.get_ref().0
6161
}
62+
pub fn into_inner(self) -> R {
63+
self.0.into_inner().0
64+
}
6265
}
6366

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

9497
pub fn with_params(r: R, buffer_size: usize, params: &BrotliEncoderParams) -> Self {
9598
let mut reader = Self::new(r, buffer_size, params.quality as u32, params.lgwin as u32);
96-
(reader.0).0.state.params = params.clone();
99+
(reader.0).0.state.0.params = params.clone();
97100
reader
98101
}
99102

100103
pub fn get_ref(&self) -> &R {
101104
self.0.get_ref()
102105
}
106+
pub fn into_inner(self) -> R {
107+
self.0.into_inner()
108+
}
103109
}
104110

105111

@@ -123,7 +129,14 @@ pub struct CompressorReaderCustomIo<ErrType,
123129
input: R,
124130
input_eof: bool,
125131
error_if_invalid_data: Option<ErrType>,
126-
state: BrotliEncoderStateStruct<Alloc>,
132+
state: StateWrapper<Alloc>,
133+
}
134+
struct StateWrapper<Alloc:BrotliAlloc>(BrotliEncoderStateStruct<Alloc>);
135+
136+
impl<Alloc: BrotliAlloc> Drop for StateWrapper <Alloc> {
137+
fn drop(&mut self) {
138+
BrotliEncoderDestroyInstance(&mut self.0);
139+
}
127140
}
128141

129142
impl<ErrType,
@@ -145,13 +158,13 @@ CompressorReaderCustomIo<ErrType, R, BufferType, Alloc>
145158
input_len : 0,
146159
input_eof : false,
147160
input: r,
148-
state : BrotliEncoderCreateInstance(alloc),
161+
state : StateWrapper(BrotliEncoderCreateInstance(alloc)),
149162
error_if_invalid_data : Some(invalid_data_error_type),
150163
};
151-
BrotliEncoderSetParameter(&mut ret.state,
164+
BrotliEncoderSetParameter(&mut ret.state.0,
152165
BrotliEncoderParameter::BROTLI_PARAM_QUALITY,
153166
q as (u32));
154-
BrotliEncoderSetParameter(&mut ret.state,
167+
BrotliEncoderSetParameter(&mut ret.state.0,
155168
BrotliEncoderParameter::BROTLI_PARAM_LGWIN,
156169
lgwin as (u32));
157170

@@ -169,21 +182,26 @@ CompressorReaderCustomIo<ErrType, R, BufferType, Alloc>
169182
self.input_offset = 0;
170183
}
171184
}
172-
185+
pub fn into_inner(self) -> R{
186+
match self {
187+
CompressorReaderCustomIo {
188+
input_buffer:_ib,
189+
total_out:_to,
190+
input_offset: _io,
191+
input_len: _len,
192+
input,
193+
input_eof:_ieof,
194+
error_if_invalid_data: _eiid,
195+
state: _state,
196+
} => {
197+
input
198+
}
199+
}
200+
}
173201
pub fn get_ref(&self) -> &R {
174202
&self.input
175203
}
176204
}
177-
178-
impl<ErrType,
179-
R: CustomRead<ErrType>,
180-
BufferType : SliceWrapperMut<u8>,
181-
Alloc: BrotliAlloc> Drop for
182-
CompressorReaderCustomIo<ErrType, R, BufferType, Alloc> {
183-
fn drop(&mut self) {
184-
BrotliEncoderDestroyInstance(&mut self.state);
185-
}
186-
}
187205
impl<ErrType,
188206
R: CustomRead<ErrType>,
189207
BufferType : SliceWrapperMut<u8>,
@@ -215,7 +233,7 @@ CompressorReaderCustomIo<ErrType, R, BufferType, Alloc> {
215233
op = BrotliEncoderOperation::BROTLI_OPERATION_PROCESS;
216234
}
217235
let ret = BrotliEncoderCompressStream(
218-
&mut self.state,
236+
&mut self.state.0,
219237
op,
220238
&mut avail_in,
221239
&self.input_buffer.slice_mut()[..],
@@ -231,7 +249,7 @@ CompressorReaderCustomIo<ErrType, R, BufferType, Alloc> {
231249
if ret <= 0 {
232250
return Err(self.error_if_invalid_data.take().unwrap());
233251
}
234-
let fin = BrotliEncoderIsFinished(&mut self.state);
252+
let fin = BrotliEncoderIsFinished(&mut self.state.0);
235253
if fin != 0 {
236254
break;
237255
}
@@ -241,152 +259,3 @@ CompressorReaderCustomIo<ErrType, R, BufferType, Alloc> {
241259
}
242260

243261

244-
245-
/*
246-
247-
/////////////////BOGUS////////////////////////////////////
248-
pub struct SimpleReader<R: Read,
249-
BufferType: SliceWrapperMut<u8>>
250-
{
251-
input_buffer: BufferType,
252-
total_out: Option<usize>,
253-
input_offset: usize,
254-
input_len: usize,
255-
input_eof: bool,
256-
input: R,
257-
error_if_invalid_data: Option<io::Error>,
258-
read_error: Option<io::Error>,
259-
state: BrotliEncoderStateStruct<HeapAlloc<u8>, HeapAlloc<u16>, HeapAlloc<u32>, HeapAlloc<i32>, HeapAlloc<Command>>,
260-
}
261-
262-
impl<R: Read,
263-
BufferType : SliceWrapperMut<u8>>
264-
SimpleReader<R, BufferType>
265-
{
266-
267-
pub fn new(r: R, buffer : BufferType,
268-
q: u32,
269-
lgwin: u32) -> Option<Self> {
270-
let mut ret = SimpleReader{
271-
input_buffer : buffer,
272-
total_out : Some(0),
273-
input_offset : 0,
274-
input_len : 0,
275-
input_eof : false,
276-
input: r,
277-
state : BrotliEncoderCreateInstance(HeapAlloc{default_value:0},
278-
HeapAlloc{default_value:0},
279-
HeapAlloc{default_value:0},
280-
HeapAlloc{default_value:0},
281-
HeapAlloc{default_value:Command::default()}),
282-
error_if_invalid_data : Some(Error::new(ErrorKind::InvalidData,
283-
"Invalid Data")),
284-
read_error : None,
285-
};
286-
BrotliEncoderSetParameter(&mut ret.state,
287-
BrotliEncoderParameter::BROTLI_PARAM_QUALITY,
288-
q as (u32));
289-
BrotliEncoderSetParameter(&mut ret.state,
290-
BrotliEncoderParameter::BROTLI_PARAM_LGWIN,
291-
lgwin as (u32));
292-
293-
Some(ret)
294-
}
295-
pub fn copy_to_front(&mut self) {
296-
let avail_in = self.input_len - self.input_offset;
297-
if self.input_offset == self.input_buffer.slice_mut().len() {
298-
self.input_offset = 0;
299-
self.input_len = 0;
300-
} else if self.input_offset + 256 > self.input_buffer.slice_mut().len() && avail_in < self.input_offset {
301-
let (first, second) = self.input_buffer.slice_mut().split_at_mut(self.input_offset);
302-
first[0..avail_in].clone_from_slice(&second[0..avail_in]);
303-
self.input_len -= self.input_offset;
304-
self.input_offset = 0;
305-
}
306-
}
307-
308-
pub fn get_ref(&self) -> &R {
309-
&self.input
310-
}
311-
}
312-
313-
impl<R: Read,
314-
BufferType : SliceWrapperMut<u8>> Drop for
315-
SimpleReader<R, BufferType> {
316-
fn drop(&mut self) {
317-
BrotliEncoderDestroyInstance(&mut self.state);
318-
}
319-
}
320-
impl<R: Read,
321-
BufferType : SliceWrapperMut<u8>> Read for
322-
SimpleReader<R, BufferType> {
323-
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
324-
let mut nop_callback = |_data:&[interface::Command<input_pair::InputReferenceMut>]|();
325-
let mut output_offset : usize = 0;
326-
let mut avail_out = buf.len() - output_offset;
327-
let mut avail_in = self.input_len - self.input_offset;
328-
let mut needs_input = false;
329-
while avail_out == buf.len() && (!needs_input || !self.input_eof) {
330-
if self.input_len < self.input_buffer.slice_mut().len() && !self.input_eof {
331-
match self.input.read(&mut self.input_buffer.slice_mut()[self.input_len..]) {
332-
Err(e) => {
333-
self.read_error = Some(e);
334-
self.input_eof = true;
335-
},
336-
Ok(size) => if size == 0 {
337-
self.input_eof = true;
338-
}else {
339-
needs_input = false;
340-
self.input_len += size;
341-
avail_in = self.input_len - self.input_offset;
342-
},
343-
}
344-
}
345-
let op : BrotliEncoderOperation;
346-
if avail_in == 0 {
347-
op = BrotliEncoderOperation::BROTLI_OPERATION_FINISH;
348-
} else {
349-
op = BrotliEncoderOperation::BROTLI_OPERATION_PROCESS;
350-
}
351-
let ret = BrotliEncoderCompressStream(
352-
&mut self.state,
353-
&mut HeapAlloc{default_value:0},
354-
&mut HeapAlloc{default_value:0.0},
355-
&mut HeapAlloc{default_value:Mem256f::default()},
356-
&mut HeapAlloc{default_value:HistogramLiteral::default()},
357-
&mut HeapAlloc{default_value:HistogramCommand::default()},
358-
&mut HeapAlloc{default_value:HistogramDistance::default()},
359-
&mut HeapAlloc{default_value:HistogramPair::default()},
360-
&mut HeapAlloc{default_value:ContextType::default()},
361-
&mut HeapAlloc{default_value:HuffmanTree::default()},
362-
&mut HeapAlloc{default_value:ZopfliNode::default()},
363-
op,
364-
&mut avail_in,
365-
&self.input_buffer.slice_mut()[..],
366-
&mut self.input_offset,
367-
&mut avail_out,
368-
buf,
369-
&mut output_offset,
370-
&mut self.total_out,
371-
&mut nop_callback);
372-
if avail_in == 0 {
373-
match self.read_error.take() {
374-
Some(err) => return Err(err),
375-
None => {
376-
needs_input = true;
377-
self.copy_to_front();
378-
},
379-
}
380-
}
381-
if ret <= 0 {
382-
return Err(self.error_if_invalid_data.take().unwrap());
383-
}
384-
let fin = BrotliEncoderIsFinished(&mut self.state);
385-
if fin != 0 {
386-
break;
387-
}
388-
}
389-
Ok(output_offset)
390-
}
391-
}
392-
*/

src/enc/test.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ fn oneshot_decompress(compressed: &[u8], mut output: &mut [u8]) -> (BrotliResult
219219
&mut output,
220220
&mut written,
221221
&mut brotli_state);
222-
brotli_state.BrotliStateCleanup();
223222
return (result, input_offset, output_offset);
224223

225224
}

src/enc/writer.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![cfg_attr(not(feature="std"), allow(unused_imports))]
2+
use core;
23
use super::encode::{BrotliEncoderCreateInstance, BrotliEncoderDestroyInstance,
34
BrotliEncoderParameter, BrotliEncoderSetParameter, BrotliEncoderOperation,
45
BrotliEncoderStateStruct, BrotliEncoderCompressStream, BrotliEncoderIsFinished};
@@ -56,6 +57,9 @@ impl<W: Write,
5657
pub fn get_ref(&self) -> &W {
5758
&self.0.get_ref().0
5859
}
60+
pub fn into_inner(self) -> W {
61+
self.0.into_inner().0
62+
}
5963
}
6064

6165
#[cfg(feature="std")]
@@ -101,6 +105,9 @@ impl<W: Write> CompressorWriter<W> {
101105
pub fn get_ref(&self) -> &W {
102106
self.0.get_ref()
103107
}
108+
pub fn into_inner(self) -> W {
109+
self.0.into_inner()
110+
}
104111
}
105112

106113

@@ -122,7 +129,7 @@ pub struct CompressorWriterCustomIo<ErrType,
122129
{
123130
output_buffer: BufferType,
124131
total_out: Option<usize>,
125-
output: W,
132+
output: Option<W>,
126133
error_if_invalid_data: Option<ErrType>,
127134
state: BrotliEncoderStateStruct<Alloc>,
128135
}
@@ -150,7 +157,7 @@ CompressorWriterCustomIo<ErrType, W, BufferType, Alloc>
150157
let mut ret = CompressorWriterCustomIo{
151158
output_buffer : buffer,
152159
total_out : Some(0),
153-
output: w,
160+
output: Some(w),
154161
state : BrotliEncoderCreateInstance(alloc),
155162
error_if_invalid_data : Some(invalid_data_error_type),
156163
};
@@ -185,7 +192,7 @@ CompressorWriterCustomIo<ErrType, W, BufferType, Alloc>
185192
&mut self.total_out,
186193
&mut nop_callback);
187194
if output_offset > 0 {
188-
match write_all(&mut self.output, &self.output_buffer.slice_mut()[..output_offset]) {
195+
match write_all(self.output.as_mut().unwrap(), &self.output_buffer.slice_mut()[..output_offset]) {
189196
Ok(_) => {},
190197
Err(e) => return Err(e),
191198
}
@@ -203,7 +210,10 @@ CompressorWriterCustomIo<ErrType, W, BufferType, Alloc>
203210
}
204211

205212
pub fn get_ref(&self) -> &W {
206-
&self.output
213+
self.output.as_ref().unwrap()
214+
}
215+
pub fn into_inner(mut self) -> W {
216+
core::mem::replace(&mut self.output, None).unwrap()
207217
}
208218
}
209219

@@ -246,7 +256,7 @@ CompressorWriterCustomIo<ErrType, W, BufferType, Alloc> {
246256
&mut self.total_out,
247257
&mut nop_callback);
248258
if output_offset > 0 {
249-
match write_all(&mut self.output, &self.output_buffer.slice_mut()[..output_offset]) {
259+
match write_all(self.output.as_mut().unwrap(), &self.output_buffer.slice_mut()[..output_offset]) {
250260
Ok(_) => {},
251261
Err(e) => return Err(e),
252262
}
@@ -262,6 +272,6 @@ CompressorWriterCustomIo<ErrType, W, BufferType, Alloc> {
262272
Ok(_) => {},
263273
Err(e) => return Err(e),
264274
}
265-
self.output.flush()
275+
self.output.as_mut().unwrap().flush()
266276
}
267277
}

0 commit comments

Comments
 (0)