diff --git a/src/deflate.rs b/src/deflate.rs deleted file mode 100644 index c8f567c2e..000000000 --- a/src/deflate.rs +++ /dev/null @@ -1,1058 +0,0 @@ -//! DEFLATE compression and decompression of streams - -use std::io::prelude::*; -use std::io; -use std::mem; - -#[cfg(feature = "tokio")] -use futures::Poll; -#[cfg(feature = "tokio")] -use tokio_io::{AsyncRead, AsyncWrite}; - -use bufreader::BufReader; -use zio; -use {Compress, Decompress}; - -/// A DEFLATE encoder, or compressor. -/// -/// This structure implements a [`Write`] interface and takes a stream of -/// uncompressed data, writing the compressed data to the wrapped writer. -/// -/// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use flate2::Compression; -/// use flate2::write::DeflateEncoder; -/// -/// // Vec implements Write to print the compressed bytes of sample string -/// # fn main() { -/// -/// let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); -/// e.write(b"Hello World").unwrap(); -/// println!("{:?}", e.finish().unwrap()); -/// # } -/// ``` -#[derive(Debug)] -pub struct EncoderWriter { - inner: zio::Writer, -} - -/// A DEFLATE encoder, or compressor. -/// -/// This structure implements a [`Read`] interface and will read uncompressed -/// data from an underlying stream and emit a stream of compressed data. -/// -/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// use flate2::Compression; -/// use flate2::read::DeflateEncoder; -/// -/// # fn main() { -/// # println!("{:?}", deflateencoder_read_hello_world().unwrap()); -/// # } -/// # -/// // Return a vector containing the Deflate compressed version of hello world -/// fn deflateencoder_read_hello_world() -> io::Result> { -/// let mut ret_vec = [0;100]; -/// let c = b"hello world"; -/// let mut deflater = DeflateEncoder::new(&c[..], Compression::Fast); -/// let count = deflater.read(&mut ret_vec)?; -/// Ok(ret_vec[0..count].to_vec()) -/// } -/// ``` - -#[derive(Debug)] -pub struct EncoderReader { - inner: EncoderReaderBuf>, -} - -/// A DEFLATE encoder, or compressor. -/// -/// This structure implements a [`BufRead`] interface and will read uncompressed -/// data from an underlying stream and emit a stream of compressed data. -/// -/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// use flate2::Compression; -/// use flate2::bufread::DeflateEncoder; -/// use std::fs::File; -/// use std::io::BufReader; -/// -/// # fn main() { -/// # println!("{:?}", open_hello_world().unwrap()); -/// # } -/// # -/// // Opens sample file, compresses the contents and returns a Vector -/// fn open_hello_world() -> io::Result> { -/// let f = File::open("examples/hello_world.txt")?; -/// let b = BufReader::new(f); -/// let mut deflater = DeflateEncoder::new(b, Compression::Fast); -/// let mut buffer = Vec::new(); -/// deflater.read_to_end(&mut buffer)?; -/// Ok(buffer) -/// } -/// ``` -#[derive(Debug)] -pub struct EncoderReaderBuf { - obj: R, - data: Compress, -} - -/// A DEFLATE decoder, or decompressor. -/// -/// This structure implements a [`Read`] interface and takes a stream of -/// compressed data as input, providing the decompressed data when read from. -/// -/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// # use flate2::Compression; -/// # use flate2::write::DeflateEncoder; -/// use flate2::read::DeflateDecoder; -/// -/// # fn main() { -/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); -/// # e.write(b"Hello World").unwrap(); -/// # let bytes = e.finish().unwrap(); -/// # println!("{}", decode_reader(bytes).unwrap()); -/// # } -/// // Uncompresses a Deflate Encoded vector of bytes and returns a string or error -/// // Here &[u8] implements Read -/// fn decode_reader(bytes: Vec) -> io::Result { -/// let mut deflater = DeflateDecoder::new(&bytes[..]); -/// let mut s = String::new(); -/// deflater.read_to_string(&mut s)?; -/// Ok(s) -/// } -/// ``` -#[derive(Debug)] -pub struct DecoderReader { - inner: DecoderReaderBuf>, -} - -/// A DEFLATE decoder, or decompressor. -/// -/// This structure implements a [`BufRead`] interface and takes a stream of -/// compressed data as input, providing the decompressed data when read from. -/// -/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// # use flate2::Compression; -/// # use flate2::write::DeflateEncoder; -/// use flate2::bufread::DeflateDecoder; -/// -/// # fn main() { -/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); -/// # e.write(b"Hello World").unwrap(); -/// # let bytes = e.finish().unwrap(); -/// # println!("{}", decode_reader(bytes).unwrap()); -/// # } -/// // Uncompresses a Deflate Encoded vector of bytes and returns a string or error -/// // Here &[u8] implements Read -/// fn decode_reader(bytes: Vec) -> io::Result { -/// let mut deflater = DeflateDecoder::new(&bytes[..]); -/// let mut s = String::new(); -/// deflater.read_to_string(&mut s)?; -/// Ok(s) -/// } -/// ``` -#[derive(Debug)] -pub struct DecoderReaderBuf { - obj: R, - data: Decompress, -} - -/// A DEFLATE decoder, or decompressor. -/// -/// This structure implements a [`Write`] and will emit a stream of decompressed -/// data when fed a stream of compressed data. -/// -/// [`Write`]: https://doc.rust-lang.org/std/io/trait.Read.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// # use flate2::Compression; -/// # use flate2::write::DeflateEncoder; -/// use flate2::write::DeflateDecoder; -/// -/// # fn main() { -/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); -/// # e.write(b"Hello World").unwrap(); -/// # let bytes = e.finish().unwrap(); -/// # println!("{}", decode_writer(bytes).unwrap()); -/// # } -/// // Uncompresses a Deflate Encoded vector of bytes and returns a string or error -/// // Here Vec implements Write -/// fn decode_writer(bytes: Vec) -> io::Result { -/// let mut writer = Vec::new(); -/// let mut deflater = DeflateDecoder::new(writer); -/// deflater.write(&bytes[..])?; -/// writer = deflater.finish()?; -/// let return_string = String::from_utf8(writer).expect("String parsing error"); -/// Ok(return_string) -/// } -/// ``` -#[derive(Debug)] -pub struct DecoderWriter { - inner: zio::Writer, -} - -impl EncoderWriter { - /// Creates a new encoder which will write compressed data to the stream - /// given at the given compression level. - /// - /// When this encoder is dropped or unwrapped the final pieces of data will - /// be flushed. - pub fn new(w: W, level: ::Compression) -> EncoderWriter { - EncoderWriter { - inner: zio::Writer::new(w, Compress::new(level, false)), - } - } - - /// Acquires a reference to the underlying writer. - pub fn get_ref(&self) -> &W { - self.inner.get_ref() - } - - /// Acquires a mutable reference to the underlying writer. - /// - /// Note that mutating the output/input state of the stream may corrupt this - /// object, so care must be taken when using this method. - pub fn get_mut(&mut self) -> &mut W { - self.inner.get_mut() - } - - /// Resets the state of this encoder entirely, swapping out the output - /// stream for another. - /// - /// This function will finish encoding the current stream into the current - /// output stream before swapping out the two output streams. If the stream - /// cannot be finished an error is returned. - /// - /// After the current stream has been finished, this will reset the internal - /// state of this encoder and replace the output stream with the one - /// provided, returning the previous output stream. Future data written to - /// this encoder will be the compressed into the stream `w` provided. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn reset(&mut self, w: W) -> io::Result { - try!(self.inner.finish()); - self.inner.data.reset(); - Ok(self.inner.replace(w)) - } - - /// Attempt to finish this output stream, writing out final chunks of data. - /// - /// Note that this function can only be used once data has finished being - /// written to the output stream. After this function is called then further - /// calls to `write` may result in a panic. - /// - /// # Panics - /// - /// Attempts to write data to this stream may result in a panic after this - /// function is called. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn try_finish(&mut self) -> io::Result<()> { - self.inner.finish() - } - - /// Consumes this encoder, flushing the output stream. - /// - /// This will flush the underlying data stream, close off the compressed - /// stream and, if successful, return the contained writer. - /// - /// Note that this function may not be suitable to call in a situation where - /// the underlying stream is an asynchronous I/O stream. To finish a stream - /// the `try_finish` (or `shutdown`) method should be used instead. To - /// re-acquire ownership of a stream it is safe to call this method after - /// `try_finish` or `shutdown` has returned `Ok`. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn finish(mut self) -> io::Result { - try!(self.inner.finish()); - Ok(self.inner.take_inner()) - } - - /// Consumes this encoder, flushing the output stream. - /// - /// This will flush the underlying data stream and then return the contained - /// writer if the flush succeeded. - /// The compressed stream will not closed but only flushed. This - /// means that obtained byte array can by extended by another deflated - /// stream. To close the stream add the two bytes 0x3 and 0x0. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn flush_finish(mut self) -> io::Result { - try!(self.inner.flush()); - Ok(self.inner.take_inner()) - } - - /// Returns the number of bytes that have been written to this compresor. - /// - /// Note that not all bytes written to this object may be accounted for, - /// there may still be some active buffering. - pub fn total_in(&self) -> u64 { - self.inner.data.total_in() - } - - /// Returns the number of bytes that the compressor has produced. - /// - /// Note that not all bytes may have been written yet, some may still be - /// buffered. - pub fn total_out(&self) -> u64 { - self.inner.data.total_out() - } -} - -impl Write for EncoderWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.inner.write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.inner.flush() - } -} - -#[cfg(feature = "tokio")] -impl AsyncWrite for EncoderWriter { - fn shutdown(&mut self) -> Poll<(), io::Error> { - try_nb!(self.inner.finish()); - self.inner.get_mut().shutdown() - } -} - -impl Read for EncoderWriter { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - self.inner.get_mut().read(buf) - } -} - -#[cfg(feature = "tokio")] -impl AsyncRead for EncoderWriter { -} - -impl EncoderReader { - /// Creates a new encoder which will read uncompressed data from the given - /// stream and emit the compressed stream. - pub fn new(r: R, level: ::Compression) -> EncoderReader { - EncoderReader { - inner: EncoderReaderBuf::new(BufReader::new(r), level), - } - } -} - -impl EncoderReader { - /// Resets the state of this encoder entirely, swapping out the input - /// stream for another. - /// - /// This function will reset the internal state of this encoder and replace - /// the input stream with the one provided, returning the previous input - /// stream. Future data read from this encoder will be the compressed - /// version of `r`'s data. - /// - /// Note that there may be currently buffered data when this function is - /// called, and in that case the buffered data is discarded. - pub fn reset(&mut self, r: R) -> R { - self.inner.data.reset(); - self.inner.obj.reset(r) - } - - /// Acquires a reference to the underlying reader - pub fn get_ref(&self) -> &R { - self.inner.get_ref().get_ref() - } - - /// Acquires a mutable reference to the underlying stream - /// - /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - self.inner.get_mut().get_mut() - } - - /// Consumes this encoder, returning the underlying reader. - /// - /// Note that there may be buffered bytes which are not re-acquired as part - /// of this transition. It's recommended to only call this function after - /// EOF has been reached. - pub fn into_inner(self) -> R { - self.inner.into_inner().into_inner() - } - - /// Returns the number of bytes that have been read into this compressor. - /// - /// Note that not all bytes read from the underlying object may be accounted - /// for, there may still be some active buffering. - pub fn total_in(&self) -> u64 { - self.inner.data.total_in() - } - - /// Returns the number of bytes that the compressor has produced. - /// - /// Note that not all bytes may have been read yet, some may still be - /// buffered. - pub fn total_out(&self) -> u64 { - self.inner.data.total_out() - } -} - -impl Read for EncoderReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - self.inner.read(buf) - } -} - -#[cfg(feature = "tokio")] -impl AsyncRead for EncoderReader { -} - -impl Write for EncoderReader { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -#[cfg(feature = "tokio")] -impl AsyncWrite for EncoderReader { - fn shutdown(&mut self) -> Poll<(), io::Error> { - self.get_mut().shutdown() - } -} - -impl EncoderReaderBuf { - /// Creates a new encoder which will read uncompressed data from the given - /// stream and emit the compressed stream. - pub fn new(r: R, level: ::Compression) -> EncoderReaderBuf { - EncoderReaderBuf { - obj: r, - data: Compress::new(level, false), - } - } -} - -impl EncoderReaderBuf { - /// Resets the state of this encoder entirely, swapping out the input - /// stream for another. - /// - /// This function will reset the internal state of this encoder and replace - /// the input stream with the one provided, returning the previous input - /// stream. Future data read from this encoder will be the compressed - /// version of `r`'s data. - pub fn reset(&mut self, r: R) -> R { - self.data.reset(); - mem::replace(&mut self.obj, r) - } - - /// Acquires a reference to the underlying reader - pub fn get_ref(&self) -> &R { - &self.obj - } - - /// Acquires a mutable reference to the underlying stream - /// - /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - &mut self.obj - } - - /// Consumes this encoder, returning the underlying reader. - pub fn into_inner(self) -> R { - self.obj - } - - /// Returns the number of bytes that have been read into this compressor. - /// - /// Note that not all bytes read from the underlying object may be accounted - /// for, there may still be some active buffering. - pub fn total_in(&self) -> u64 { - self.data.total_in() - } - - /// Returns the number of bytes that the compressor has produced. - /// - /// Note that not all bytes may have been read yet, some may still be - /// buffered. - pub fn total_out(&self) -> u64 { - self.data.total_out() - } -} - -impl Read for EncoderReaderBuf { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - zio::read(&mut self.obj, &mut self.data, buf) - } -} - -#[cfg(feature = "tokio")] -impl AsyncRead for EncoderReaderBuf { -} - -impl Write for EncoderReaderBuf { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -#[cfg(feature = "tokio")] -impl AsyncWrite for EncoderReaderBuf { - fn shutdown(&mut self) -> Poll<(), io::Error> { - self.get_mut().shutdown() - } -} - -impl DecoderReader { - /// Creates a new decoder which will decompress data read from the given - /// stream. - pub fn new(r: R) -> DecoderReader { - DecoderReader::new_with_buf(r, vec![0; 32 * 1024]) - } - - /// Same as `new`, but the intermediate buffer for data is specified. - /// - /// Note that the capacity of the intermediate buffer is never increased, - /// and it is recommended for it to be large. - pub fn new_with_buf(r: R, buf: Vec) -> DecoderReader { - DecoderReader { - inner: DecoderReaderBuf::new(BufReader::with_buf(buf, r)) - } - } -} - -impl DecoderReader { - /// Resets the state of this decoder entirely, swapping out the input - /// stream for another. - /// - /// This will reset the internal state of this decoder and replace the - /// input stream with the one provided, returning the previous input - /// stream. Future data read from this decoder will be the decompressed - /// version of `r`'s data. - /// - /// Note that there may be currently buffered data when this function is - /// called, and in that case the buffered data is discarded. - pub fn reset(&mut self, r: R) -> R { - self.inner.data = Decompress::new(false); - self.inner.obj.reset(r) - } - - /// Acquires a reference to the underlying stream - pub fn get_ref(&self) -> &R { - self.inner.get_ref().get_ref() - } - - /// Acquires a mutable reference to the underlying stream - /// - /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - self.inner.get_mut().get_mut() - } - - /// Consumes this decoder, returning the underlying reader. - /// - /// Note that there may be buffered bytes which are not re-acquired as part - /// of this transition. It's recommended to only call this function after - /// EOF has been reached. - pub fn into_inner(self) -> R { - self.inner.into_inner().into_inner() - } - - /// Returns the number of bytes that the decompressor has consumed. - /// - /// Note that this will likely be smaller than what the decompressor - /// actually read from the underlying stream due to buffering. - pub fn total_in(&self) -> u64 { - self.inner.total_in() - } - - /// Returns the number of bytes that the decompressor has produced. - pub fn total_out(&self) -> u64 { - self.inner.total_out() - } -} - -impl Read for DecoderReader { - fn read(&mut self, into: &mut [u8]) -> io::Result { - self.inner.read(into) - } -} - -#[cfg(feature = "tokio")] -impl AsyncRead for DecoderReader { -} - -impl Write for DecoderReader { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -#[cfg(feature = "tokio")] -impl AsyncWrite for DecoderReader { - fn shutdown(&mut self) -> Poll<(), io::Error> { - self.get_mut().shutdown() - } -} - -impl DecoderReaderBuf { - /// Creates a new decoder which will decompress data read from the given - /// stream. - pub fn new(r: R) -> DecoderReaderBuf { - DecoderReaderBuf { - obj: r, - data: Decompress::new(false), - } - } -} - -impl DecoderReaderBuf { - /// Resets the state of this decoder entirely, swapping out the input - /// stream for another. - /// - /// This will reset the internal state of this decoder and replace the - /// input stream with the one provided, returning the previous input - /// stream. Future data read from this decoder will be the decompressed - /// version of `r`'s data. - pub fn reset(&mut self, r: R) -> R { - self.data = Decompress::new(false); - mem::replace(&mut self.obj, r) - } - - /// Resets the state of this decoder's data - /// - /// This will reset the internal state of this decoder. It will continue - /// reading from the same stream. - pub fn reset_data(&mut self) { - self.data = Decompress::new(false); - } - - /// Acquires a reference to the underlying stream - pub fn get_ref(&self) -> &R { - &self.obj - } - - /// Acquires a mutable reference to the underlying stream - /// - /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - &mut self.obj - } - - /// Consumes this decoder, returning the underlying reader. - pub fn into_inner(self) -> R { - self.obj - } - - /// Returns the number of bytes that the decompressor has consumed. - /// - /// Note that this will likely be smaller than what the decompressor - /// actually read from the underlying stream due to buffering. - pub fn total_in(&self) -> u64 { - self.data.total_in() - } - - /// Returns the number of bytes that the decompressor has produced. - pub fn total_out(&self) -> u64 { - self.data.total_out() - } -} - -impl Read for DecoderReaderBuf { - fn read(&mut self, into: &mut [u8]) -> io::Result { - zio::read(&mut self.obj, &mut self.data, into) - } -} - -#[cfg(feature = "tokio")] -impl AsyncRead for DecoderReaderBuf { -} - -impl Write for DecoderReaderBuf { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -#[cfg(feature = "tokio")] -impl AsyncWrite for DecoderReaderBuf { - fn shutdown(&mut self) -> Poll<(), io::Error> { - self.get_mut().shutdown() - } -} - -impl DecoderWriter { - /// Creates a new decoder which will write uncompressed data to the stream. - /// - /// When this encoder is dropped or unwrapped the final pieces of data will - /// be flushed. - pub fn new(w: W) -> DecoderWriter { - DecoderWriter { - inner: zio::Writer::new(w, Decompress::new(false)), - } - } - - /// Acquires a reference to the underlying writer. - pub fn get_ref(&self) -> &W { - self.inner.get_ref() - } - - /// Acquires a mutable reference to the underlying writer. - /// - /// Note that mutating the output/input state of the stream may corrupt this - /// object, so care must be taken when using this method. - pub fn get_mut(&mut self) -> &mut W { - self.inner.get_mut() - } - - /// Resets the state of this decoder entirely, swapping out the output - /// stream for another. - /// - /// This function will finish encoding the current stream into the current - /// output stream before swapping out the two output streams. - /// - /// This will then reset the internal state of this decoder and replace the - /// output stream with the one provided, returning the previous output - /// stream. Future data written to this decoder will be decompressed into - /// the output stream `w`. - /// - /// # Errors - /// - /// This function will perform I/O to finish the stream, and if that I/O - /// returns an error then that will be returned from this function. - pub fn reset(&mut self, w: W) -> io::Result { - try!(self.inner.finish()); - self.inner.data = Decompress::new(false); - Ok(self.inner.replace(w)) - } - - /// Attempt to finish this output stream, writing out final chunks of data. - /// - /// Note that this function can only be used once data has finished being - /// written to the output stream. After this function is called then further - /// calls to `write` may result in a panic. - /// - /// # Panics - /// - /// Attempts to write data to this stream may result in a panic after this - /// function is called. - /// - /// # Errors - /// - /// This function will perform I/O to finish the stream, returning any - /// errors which happen. - pub fn try_finish(&mut self) -> io::Result<()> { - self.inner.finish() - } - - /// Consumes this encoder, flushing the output stream. - /// - /// This will flush the underlying data stream and then return the contained - /// writer if the flush succeeded. - /// - /// Note that this function may not be suitable to call in a situation where - /// the underlying stream is an asynchronous I/O stream. To finish a stream - /// the `try_finish` (or `shutdown`) method should be used instead. To - /// re-acquire ownership of a stream it is safe to call this method after - /// `try_finish` or `shutdown` has returned `Ok`. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn finish(mut self) -> io::Result { - try!(self.inner.finish()); - Ok(self.inner.take_inner()) - } - - /// Returns the number of bytes that the decompressor has consumed for - /// decompression. - /// - /// Note that this will likely be smaller than the number of bytes - /// successfully written to this stream due to internal buffering. - pub fn total_in(&self) -> u64 { - self.inner.data.total_in() - } - - /// Returns the number of bytes that the decompressor has written to its - /// output stream. - pub fn total_out(&self) -> u64 { - self.inner.data.total_out() - } -} - -impl Write for DecoderWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.inner.write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.inner.flush() - } -} - -#[cfg(feature = "tokio")] -impl AsyncWrite for DecoderWriter { - fn shutdown(&mut self) -> Poll<(), io::Error> { - try_nb!(self.inner.finish()); - self.inner.get_mut().shutdown() - } -} - -impl Read for DecoderWriter { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - self.inner.get_mut().read(buf) - } -} - -#[cfg(feature = "tokio")] -impl AsyncRead for DecoderWriter { -} - -#[cfg(test)] -mod tests { - use std::io::prelude::*; - - use rand::{thread_rng, Rng}; - - use deflate::{EncoderWriter, EncoderReader, DecoderReader, DecoderWriter}; - use Compression::Default; - - #[test] - fn roundtrip() { - let mut real = Vec::new(); - let mut w = EncoderWriter::new(Vec::new(), Default); - let v = thread_rng().gen_iter::().take(1024).collect::>(); - for _ in 0..200 { - let to_write = &v[..thread_rng().gen_range(0, v.len())]; - real.extend(to_write.iter().map(|x| *x)); - w.write_all(to_write).unwrap(); - } - let result = w.finish().unwrap(); - let mut r = DecoderReader::new(&result[..]); - let mut ret = Vec::new(); - r.read_to_end(&mut ret).unwrap(); - assert!(ret == real); - } - - #[test] - fn drop_writes() { - let mut data = Vec::new(); - EncoderWriter::new(&mut data, Default).write_all(b"foo").unwrap(); - let mut r = DecoderReader::new(&data[..]); - let mut ret = Vec::new(); - r.read_to_end(&mut ret).unwrap(); - assert!(ret == b"foo"); - } - - #[test] - fn total_in() { - let mut real = Vec::new(); - let mut w = EncoderWriter::new(Vec::new(), Default); - let v = thread_rng().gen_iter::().take(1024).collect::>(); - for _ in 0..200 { - let to_write = &v[..thread_rng().gen_range(0, v.len())]; - real.extend(to_write.iter().map(|x| *x)); - w.write_all(to_write).unwrap(); - } - let mut result = w.finish().unwrap(); - - let result_len = result.len(); - - for _ in 0..200 { - result.extend(v.iter().map(|x| *x)); - } - - let mut r = DecoderReader::new(&result[..]); - let mut ret = Vec::new(); - r.read_to_end(&mut ret).unwrap(); - assert!(ret == real); - assert_eq!(r.total_in(), result_len as u64); - } - - #[test] - fn roundtrip2() { - let v = thread_rng() - .gen_iter::() - .take(1024 * 1024) - .collect::>(); - let mut r = DecoderReader::new(EncoderReader::new(&v[..], Default)); - let mut ret = Vec::new(); - r.read_to_end(&mut ret).unwrap(); - assert_eq!(ret, v); - } - - #[test] - fn roundtrip3() { - let v = thread_rng() - .gen_iter::() - .take(1024 * 1024) - .collect::>(); - let mut w = EncoderWriter::new(DecoderWriter::new(Vec::new()), Default); - w.write_all(&v).unwrap(); - let w = w.finish().unwrap().finish().unwrap(); - assert!(w == v); - } - - #[test] - fn reset_writer() { - let v = thread_rng() - .gen_iter::() - .take(1024 * 1024) - .collect::>(); - let mut w = EncoderWriter::new(Vec::new(), Default); - w.write_all(&v).unwrap(); - let a = w.reset(Vec::new()).unwrap(); - w.write_all(&v).unwrap(); - let b = w.finish().unwrap(); - - let mut w = EncoderWriter::new(Vec::new(), Default); - w.write_all(&v).unwrap(); - let c = w.finish().unwrap(); - assert!(a == b && b == c); - } - - #[test] - fn reset_reader() { - let v = thread_rng() - .gen_iter::() - .take(1024 * 1024) - .collect::>(); - let (mut a, mut b, mut c) = (Vec::new(), Vec::new(), Vec::new()); - let mut r = EncoderReader::new(&v[..], Default); - r.read_to_end(&mut a).unwrap(); - r.reset(&v[..]); - r.read_to_end(&mut b).unwrap(); - - let mut r = EncoderReader::new(&v[..], Default); - r.read_to_end(&mut c).unwrap(); - assert!(a == b && b == c); - } - - #[test] - fn reset_decoder() { - let v = thread_rng() - .gen_iter::() - .take(1024 * 1024) - .collect::>(); - let mut w = EncoderWriter::new(Vec::new(), Default); - w.write_all(&v).unwrap(); - let data = w.finish().unwrap(); - - { - let (mut a, mut b, mut c) = (Vec::new(), Vec::new(), Vec::new()); - let mut r = DecoderReader::new(&data[..]); - r.read_to_end(&mut a).unwrap(); - r.reset(&data); - r.read_to_end(&mut b).unwrap(); - - let mut r = DecoderReader::new(&data[..]); - r.read_to_end(&mut c).unwrap(); - assert!(a == b && b == c && c == v); - } - - { - let mut w = DecoderWriter::new(Vec::new()); - w.write_all(&data).unwrap(); - let a = w.reset(Vec::new()).unwrap(); - w.write_all(&data).unwrap(); - let b = w.finish().unwrap(); - - let mut w = DecoderWriter::new(Vec::new()); - w.write_all(&data).unwrap(); - let c = w.finish().unwrap(); - assert!(a == b && b == c && c == v); - } - } - - #[test] - fn zero_length_read_with_data() { - let m = vec![3u8; 128 * 1024 + 1]; - let mut c = EncoderReader::new(&m[..], ::Compression::Default); - - let mut result = Vec::new(); - c.read_to_end(&mut result).unwrap(); - - let mut d = DecoderReader::new(&result[..]); - let mut data = Vec::new(); - assert!(d.read(&mut data).unwrap() == 0); - } - - #[test] - fn qc_reader() { - ::quickcheck::quickcheck(test as fn(_) -> _); - - fn test(v: Vec) -> bool { - let mut r = DecoderReader::new(EncoderReader::new(&v[..], Default)); - let mut v2 = Vec::new(); - r.read_to_end(&mut v2).unwrap(); - v == v2 - } - } - - #[test] - fn qc_writer() { - ::quickcheck::quickcheck(test as fn(_) -> _); - - fn test(v: Vec) -> bool { - let mut w = EncoderWriter::new(DecoderWriter::new(Vec::new()), Default); - w.write_all(&v).unwrap(); - v == w.finish().unwrap().finish().unwrap() - } - } -} diff --git a/src/deflate/bufread.rs b/src/deflate/bufread.rs new file mode 100644 index 000000000..1d12cd401 --- /dev/null +++ b/src/deflate/bufread.rs @@ -0,0 +1,262 @@ +use std::io::prelude::*; +use std::io; +use std::mem; + +#[cfg(feature = "tokio")] +use futures::Poll; +#[cfg(feature = "tokio")] +use tokio_io::{AsyncRead, AsyncWrite}; + +use zio; +use {Compress, Decompress}; + +/// A DEFLATE encoder, or compressor. +/// +/// This structure implements a [`BufRead`] interface and will read uncompressed +/// data from an underlying stream and emit a stream of compressed data. +/// +/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use std::io; +/// use flate2::Compression; +/// use flate2::bufread::DeflateEncoder; +/// use std::fs::File; +/// use std::io::BufReader; +/// +/// # fn main() { +/// # println!("{:?}", open_hello_world().unwrap()); +/// # } +/// # +/// // Opens sample file, compresses the contents and returns a Vector +/// fn open_hello_world() -> io::Result> { +/// let f = File::open("examples/hello_world.txt")?; +/// let b = BufReader::new(f); +/// let mut deflater = DeflateEncoder::new(b, Compression::Fast); +/// let mut buffer = Vec::new(); +/// deflater.read_to_end(&mut buffer)?; +/// Ok(buffer) +/// } +/// ``` +#[derive(Debug)] +pub struct DeflateEncoder { + pub(crate) obj: R, + pub(crate) data: Compress, +} + + +impl DeflateEncoder { + /// Creates a new encoder which will read uncompressed data from the given + /// stream and emit the compressed stream. + pub fn new(r: R, level: ::Compression) -> DeflateEncoder { + DeflateEncoder { + obj: r, + data: Compress::new(level, false), + } + } +} + +impl DeflateEncoder { + /// Resets the state of this encoder entirely, swapping out the input + /// stream for another. + /// + /// This function will reset the internal state of this encoder and replace + /// the input stream with the one provided, returning the previous input + /// stream. Future data read from this encoder will be the compressed + /// version of `r`'s data. + pub fn reset(&mut self, r: R) -> R { + self.data.reset(); + mem::replace(&mut self.obj, r) + } + + /// Acquires a reference to the underlying reader + pub fn get_ref(&self) -> &R { + &self.obj + } + + /// Acquires a mutable reference to the underlying stream + /// + /// Note that mutation of the stream may result in surprising results if + /// this encoder is continued to be used. + pub fn get_mut(&mut self) -> &mut R { + &mut self.obj + } + + /// Consumes this encoder, returning the underlying reader. + pub fn into_inner(self) -> R { + self.obj + } + + /// Returns the number of bytes that have been read into this compressor. + /// + /// Note that not all bytes read from the underlying object may be accounted + /// for, there may still be some active buffering. + pub fn total_in(&self) -> u64 { + self.data.total_in() + } + + /// Returns the number of bytes that the compressor has produced. + /// + /// Note that not all bytes may have been read yet, some may still be + /// buffered. + pub fn total_out(&self) -> u64 { + self.data.total_out() + } +} + +impl Read for DeflateEncoder { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + zio::read(&mut self.obj, &mut self.data, buf) + } +} + +#[cfg(feature = "tokio")] +impl AsyncRead for DeflateEncoder {} + +impl Write for DeflateEncoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.get_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.get_mut().flush() + } +} + +#[cfg(feature = "tokio")] +impl AsyncWrite for DeflateEncoder { + fn shutdown(&mut self) -> Poll<(), io::Error> { + self.get_mut().shutdown() + } +} + +/// A DEFLATE decoder, or decompressor. +/// +/// This structure implements a [`BufRead`] interface and takes a stream of +/// compressed data as input, providing the decompressed data when read from. +/// +/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use std::io; +/// # use flate2::Compression; +/// # use flate2::write::DeflateEncoder; +/// use flate2::bufread::DeflateDecoder; +/// +/// # fn main() { +/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); +/// # e.write(b"Hello World").unwrap(); +/// # let bytes = e.finish().unwrap(); +/// # println!("{}", decode_reader(bytes).unwrap()); +/// # } +/// // Uncompresses a Deflate Encoded vector of bytes and returns a string or error +/// // Here &[u8] implements Read +/// fn decode_reader(bytes: Vec) -> io::Result { +/// let mut deflater = DeflateDecoder::new(&bytes[..]); +/// let mut s = String::new(); +/// deflater.read_to_string(&mut s)?; +/// Ok(s) +/// } +/// ``` +#[derive(Debug)] +pub struct DeflateDecoder { + pub(crate) obj: R, + pub(crate) data: Decompress, +} + + +impl DeflateDecoder { + /// Creates a new decoder which will decompress data read from the given + /// stream. + pub fn new(r: R) -> DeflateDecoder { + DeflateDecoder { + obj: r, + data: Decompress::new(false), + } + } +} + +impl DeflateDecoder { + /// Resets the state of this decoder entirely, swapping out the input + /// stream for another. + /// + /// This will reset the internal state of this decoder and replace the + /// input stream with the one provided, returning the previous input + /// stream. Future data read from this decoder will be the decompressed + /// version of `r`'s data. + pub fn reset(&mut self, r: R) -> R { + self.data = Decompress::new(false); + mem::replace(&mut self.obj, r) + } + + /// Resets the state of this decoder's data + /// + /// This will reset the internal state of this decoder. It will continue + /// reading from the same stream. + pub fn reset_data(&mut self) { + self.data = Decompress::new(false); + } + + /// Acquires a reference to the underlying stream + pub fn get_ref(&self) -> &R { + &self.obj + } + + /// Acquires a mutable reference to the underlying stream + /// + /// Note that mutation of the stream may result in surprising results if + /// this encoder is continued to be used. + pub fn get_mut(&mut self) -> &mut R { + &mut self.obj + } + + /// Consumes this decoder, returning the underlying reader. + pub fn into_inner(self) -> R { + self.obj + } + + /// Returns the number of bytes that the decompressor has consumed. + /// + /// Note that this will likely be smaller than what the decompressor + /// actually read from the underlying stream due to buffering. + pub fn total_in(&self) -> u64 { + self.data.total_in() + } + + /// Returns the number of bytes that the decompressor has produced. + pub fn total_out(&self) -> u64 { + self.data.total_out() + } +} + +impl Read for DeflateDecoder { + fn read(&mut self, into: &mut [u8]) -> io::Result { + zio::read(&mut self.obj, &mut self.data, into) + } +} + +#[cfg(feature = "tokio")] +impl AsyncRead for DeflateDecoder {} + +impl Write for DeflateDecoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.get_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.get_mut().flush() + } +} + +#[cfg(feature = "tokio")] +impl AsyncWrite for DeflateDecoder { + fn shutdown(&mut self) -> Poll<(), io::Error> { + self.get_mut().shutdown() + } +} diff --git a/src/deflate/mod.rs b/src/deflate/mod.rs new file mode 100644 index 000000000..7bacfa7c8 --- /dev/null +++ b/src/deflate/mod.rs @@ -0,0 +1,198 @@ +pub mod bufread; +pub mod read; +pub mod write; + +#[cfg(test)] +mod tests { + use std::io::prelude::*; + + use rand::{thread_rng, Rng}; + + use super::{read, write}; + use Compression::Default; + + #[test] + fn roundtrip() { + let mut real = Vec::new(); + let mut w = write::DeflateEncoder::new(Vec::new(), Default); + let v = thread_rng().gen_iter::().take(1024).collect::>(); + for _ in 0..200 { + let to_write = &v[..thread_rng().gen_range(0, v.len())]; + real.extend(to_write.iter().map(|x| *x)); + w.write_all(to_write).unwrap(); + } + let result = w.finish().unwrap(); + let mut r = read::DeflateDecoder::new(&result[..]); + let mut ret = Vec::new(); + r.read_to_end(&mut ret).unwrap(); + assert!(ret == real); + } + + #[test] + fn drop_writes() { + let mut data = Vec::new(); + write::DeflateEncoder::new(&mut data, Default) + .write_all(b"foo") + .unwrap(); + let mut r = read::DeflateDecoder::new(&data[..]); + let mut ret = Vec::new(); + r.read_to_end(&mut ret).unwrap(); + assert!(ret == b"foo"); + } + + #[test] + fn total_in() { + let mut real = Vec::new(); + let mut w = write::DeflateEncoder::new(Vec::new(), Default); + let v = thread_rng().gen_iter::().take(1024).collect::>(); + for _ in 0..200 { + let to_write = &v[..thread_rng().gen_range(0, v.len())]; + real.extend(to_write.iter().map(|x| *x)); + w.write_all(to_write).unwrap(); + } + let mut result = w.finish().unwrap(); + + let result_len = result.len(); + + for _ in 0..200 { + result.extend(v.iter().map(|x| *x)); + } + + let mut r = read::DeflateDecoder::new(&result[..]); + let mut ret = Vec::new(); + r.read_to_end(&mut ret).unwrap(); + assert!(ret == real); + assert_eq!(r.total_in(), result_len as u64); + } + + #[test] + fn roundtrip2() { + let v = thread_rng() + .gen_iter::() + .take(1024 * 1024) + .collect::>(); + let mut r = read::DeflateDecoder::new(read::DeflateEncoder::new(&v[..], Default)); + let mut ret = Vec::new(); + r.read_to_end(&mut ret).unwrap(); + assert_eq!(ret, v); + } + + #[test] + fn roundtrip3() { + let v = thread_rng() + .gen_iter::() + .take(1024 * 1024) + .collect::>(); + let mut w = write::DeflateEncoder::new(write::DeflateDecoder::new(Vec::new()), Default); + w.write_all(&v).unwrap(); + let w = w.finish().unwrap().finish().unwrap(); + assert!(w == v); + } + + #[test] + fn reset_writer() { + let v = thread_rng() + .gen_iter::() + .take(1024 * 1024) + .collect::>(); + let mut w = write::DeflateEncoder::new(Vec::new(), Default); + w.write_all(&v).unwrap(); + let a = w.reset(Vec::new()).unwrap(); + w.write_all(&v).unwrap(); + let b = w.finish().unwrap(); + + let mut w = write::DeflateEncoder::new(Vec::new(), Default); + w.write_all(&v).unwrap(); + let c = w.finish().unwrap(); + assert!(a == b && b == c); + } + + #[test] + fn reset_reader() { + let v = thread_rng() + .gen_iter::() + .take(1024 * 1024) + .collect::>(); + let (mut a, mut b, mut c) = (Vec::new(), Vec::new(), Vec::new()); + let mut r = read::DeflateEncoder::new(&v[..], Default); + r.read_to_end(&mut a).unwrap(); + r.reset(&v[..]); + r.read_to_end(&mut b).unwrap(); + + let mut r = read::DeflateEncoder::new(&v[..], Default); + r.read_to_end(&mut c).unwrap(); + assert!(a == b && b == c); + } + + #[test] + fn reset_decoder() { + let v = thread_rng() + .gen_iter::() + .take(1024 * 1024) + .collect::>(); + let mut w = write::DeflateEncoder::new(Vec::new(), Default); + w.write_all(&v).unwrap(); + let data = w.finish().unwrap(); + + { + let (mut a, mut b, mut c) = (Vec::new(), Vec::new(), Vec::new()); + let mut r = read::DeflateDecoder::new(&data[..]); + r.read_to_end(&mut a).unwrap(); + r.reset(&data); + r.read_to_end(&mut b).unwrap(); + + let mut r = read::DeflateDecoder::new(&data[..]); + r.read_to_end(&mut c).unwrap(); + assert!(a == b && b == c && c == v); + } + + { + let mut w = write::DeflateDecoder::new(Vec::new()); + w.write_all(&data).unwrap(); + let a = w.reset(Vec::new()).unwrap(); + w.write_all(&data).unwrap(); + let b = w.finish().unwrap(); + + let mut w = write::DeflateDecoder::new(Vec::new()); + w.write_all(&data).unwrap(); + let c = w.finish().unwrap(); + assert!(a == b && b == c && c == v); + } + } + + #[test] + fn zero_length_read_with_data() { + let m = vec![3u8; 128 * 1024 + 1]; + let mut c = read::DeflateEncoder::new(&m[..], ::Compression::Default); + + let mut result = Vec::new(); + c.read_to_end(&mut result).unwrap(); + + let mut d = read::DeflateDecoder::new(&result[..]); + let mut data = Vec::new(); + assert!(d.read(&mut data).unwrap() == 0); + } + + #[test] + fn qc_reader() { + ::quickcheck::quickcheck(test as fn(_) -> _); + + fn test(v: Vec) -> bool { + let mut r = read::DeflateDecoder::new(read::DeflateEncoder::new(&v[..], Default)); + let mut v2 = Vec::new(); + r.read_to_end(&mut v2).unwrap(); + v == v2 + } + } + + #[test] + fn qc_writer() { + ::quickcheck::quickcheck(test as fn(_) -> _); + + fn test(v: Vec) -> bool { + let mut w = write::DeflateEncoder::new(write::DeflateDecoder::new(Vec::new()), Default); + w.write_all(&v).unwrap(); + v == w.finish().unwrap().finish().unwrap() + } + } +} diff --git a/src/deflate/read.rs b/src/deflate/read.rs new file mode 100644 index 000000000..b86c2bf08 --- /dev/null +++ b/src/deflate/read.rs @@ -0,0 +1,269 @@ +use std::io::prelude::*; +use std::io; + +#[cfg(feature = "tokio")] +use futures::Poll; +#[cfg(feature = "tokio")] +use tokio_io::{AsyncRead, AsyncWrite}; + +use bufreader::BufReader; +use super::bufread; +use Decompress; + +/// A DEFLATE encoder, or compressor. +/// +/// This structure implements a [`Read`] interface and will read uncompressed +/// data from an underlying stream and emit a stream of compressed data. +/// +/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use std::io; +/// use flate2::Compression; +/// use flate2::read::DeflateEncoder; +/// +/// # fn main() { +/// # println!("{:?}", deflateencoder_read_hello_world().unwrap()); +/// # } +/// # +/// // Return a vector containing the Deflate compressed version of hello world +/// fn deflateencoder_read_hello_world() -> io::Result> { +/// let mut ret_vec = [0;100]; +/// let c = b"hello world"; +/// let mut deflater = DeflateEncoder::new(&c[..], Compression::Fast); +/// let count = deflater.read(&mut ret_vec)?; +/// Ok(ret_vec[0..count].to_vec()) +/// } +/// ``` +#[derive(Debug)] +pub struct DeflateEncoder { + inner: bufread::DeflateEncoder>, +} + +impl DeflateEncoder { + /// Creates a new encoder which will read uncompressed data from the given + /// stream and emit the compressed stream. + pub fn new(r: R, level: ::Compression) -> DeflateEncoder { + DeflateEncoder { + inner: bufread::DeflateEncoder::new(BufReader::new(r), level), + } + } +} + +impl DeflateEncoder { + /// Resets the state of this encoder entirely, swapping out the input + /// stream for another. + /// + /// This function will reset the internal state of this encoder and replace + /// the input stream with the one provided, returning the previous input + /// stream. Future data read from this encoder will be the compressed + /// version of `r`'s data. + /// + /// Note that there may be currently buffered data when this function is + /// called, and in that case the buffered data is discarded. + pub fn reset(&mut self, r: R) -> R { + self.inner.data.reset(); + self.inner.obj.reset(r) + } + + /// Acquires a reference to the underlying reader + pub fn get_ref(&self) -> &R { + self.inner.get_ref().get_ref() + } + + /// Acquires a mutable reference to the underlying stream + /// + /// Note that mutation of the stream may result in surprising results if + /// this encoder is continued to be used. + pub fn get_mut(&mut self) -> &mut R { + self.inner.get_mut().get_mut() + } + + /// Consumes this encoder, returning the underlying reader. + /// + /// Note that there may be buffered bytes which are not re-acquired as part + /// of this transition. It's recommended to only call this function after + /// EOF has been reached. + pub fn into_inner(self) -> R { + self.inner.into_inner().into_inner() + } + + /// Returns the number of bytes that have been read into this compressor. + /// + /// Note that not all bytes read from the underlying object may be accounted + /// for, there may still be some active buffering. + pub fn total_in(&self) -> u64 { + self.inner.data.total_in() + } + + /// Returns the number of bytes that the compressor has produced. + /// + /// Note that not all bytes may have been read yet, some may still be + /// buffered. + pub fn total_out(&self) -> u64 { + self.inner.data.total_out() + } +} + +impl Read for DeflateEncoder { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + self.inner.read(buf) + } +} + +#[cfg(feature = "tokio")] +impl AsyncRead for DeflateEncoder {} + +impl Write for DeflateEncoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.get_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.get_mut().flush() + } +} + +#[cfg(feature = "tokio")] +impl AsyncWrite for DeflateEncoder { + fn shutdown(&mut self) -> Poll<(), io::Error> { + self.get_mut().shutdown() + } +} + +/// A DEFLATE decoder, or decompressor. +/// +/// This structure implements a [`Read`] interface and takes a stream of +/// compressed data as input, providing the decompressed data when read from. +/// +/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use std::io; +/// # use flate2::Compression; +/// # use flate2::write::DeflateEncoder; +/// use flate2::read::DeflateDecoder; +/// +/// # fn main() { +/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); +/// # e.write(b"Hello World").unwrap(); +/// # let bytes = e.finish().unwrap(); +/// # println!("{}", decode_reader(bytes).unwrap()); +/// # } +/// // Uncompresses a Deflate Encoded vector of bytes and returns a string or error +/// // Here &[u8] implements Read +/// fn decode_reader(bytes: Vec) -> io::Result { +/// let mut deflater = DeflateDecoder::new(&bytes[..]); +/// let mut s = String::new(); +/// deflater.read_to_string(&mut s)?; +/// Ok(s) +/// } +/// ``` +#[derive(Debug)] +pub struct DeflateDecoder { + inner: bufread::DeflateDecoder>, +} + + + +impl DeflateDecoder { + /// Creates a new decoder which will decompress data read from the given + /// stream. + pub fn new(r: R) -> DeflateDecoder { + DeflateDecoder::new_with_buf(r, vec![0; 32 * 1024]) + } + + /// Same as `new`, but the intermediate buffer for data is specified. + /// + /// Note that the capacity of the intermediate buffer is never increased, + /// and it is recommended for it to be large. + pub fn new_with_buf(r: R, buf: Vec) -> DeflateDecoder { + DeflateDecoder { + inner: bufread::DeflateDecoder::new(BufReader::with_buf(buf, r)), + } + } +} + +impl DeflateDecoder { + /// Resets the state of this decoder entirely, swapping out the input + /// stream for another. + /// + /// This will reset the internal state of this decoder and replace the + /// input stream with the one provided, returning the previous input + /// stream. Future data read from this decoder will be the decompressed + /// version of `r`'s data. + /// + /// Note that there may be currently buffered data when this function is + /// called, and in that case the buffered data is discarded. + pub fn reset(&mut self, r: R) -> R { + self.inner.data = Decompress::new(false); + self.inner.obj.reset(r) + } + + /// Acquires a reference to the underlying stream + pub fn get_ref(&self) -> &R { + self.inner.get_ref().get_ref() + } + + /// Acquires a mutable reference to the underlying stream + /// + /// Note that mutation of the stream may result in surprising results if + /// this encoder is continued to be used. + pub fn get_mut(&mut self) -> &mut R { + self.inner.get_mut().get_mut() + } + + /// Consumes this decoder, returning the underlying reader. + /// + /// Note that there may be buffered bytes which are not re-acquired as part + /// of this transition. It's recommended to only call this function after + /// EOF has been reached. + pub fn into_inner(self) -> R { + self.inner.into_inner().into_inner() + } + + /// Returns the number of bytes that the decompressor has consumed. + /// + /// Note that this will likely be smaller than what the decompressor + /// actually read from the underlying stream due to buffering. + pub fn total_in(&self) -> u64 { + self.inner.total_in() + } + + /// Returns the number of bytes that the decompressor has produced. + pub fn total_out(&self) -> u64 { + self.inner.total_out() + } +} + +impl Read for DeflateDecoder { + fn read(&mut self, into: &mut [u8]) -> io::Result { + self.inner.read(into) + } +} + +#[cfg(feature = "tokio")] +impl AsyncRead for DeflateDecoder {} + +impl Write for DeflateDecoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.get_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.get_mut().flush() + } +} + +#[cfg(feature = "tokio")] +impl AsyncWrite for DeflateDecoder { + fn shutdown(&mut self) -> Poll<(), io::Error> { + self.get_mut().shutdown() + } +} diff --git a/src/deflate/write.rs b/src/deflate/write.rs new file mode 100644 index 000000000..596450348 --- /dev/null +++ b/src/deflate/write.rs @@ -0,0 +1,350 @@ +use std::io::prelude::*; +use std::io; + +#[cfg(feature = "tokio")] +use futures::Poll; +#[cfg(feature = "tokio")] +use tokio_io::{AsyncRead, AsyncWrite}; + +use zio; +use {Compress, Decompress}; + +/// A DEFLATE encoder, or compressor. +/// +/// This structure implements a [`Write`] interface and takes a stream of +/// uncompressed data, writing the compressed data to the wrapped writer. +/// +/// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use flate2::Compression; +/// use flate2::write::DeflateEncoder; +/// +/// // Vec implements Write to print the compressed bytes of sample string +/// # fn main() { +/// +/// let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); +/// e.write(b"Hello World").unwrap(); +/// println!("{:?}", e.finish().unwrap()); +/// # } +/// ``` +#[derive(Debug)] +pub struct DeflateEncoder { + inner: zio::Writer, +} + +impl DeflateEncoder { + /// Creates a new encoder which will write compressed data to the stream + /// given at the given compression level. + /// + /// When this encoder is dropped or unwrapped the final pieces of data will + /// be flushed. + pub fn new(w: W, level: ::Compression) -> DeflateEncoder { + DeflateEncoder { + inner: zio::Writer::new(w, Compress::new(level, false)), + } + } + + /// Acquires a reference to the underlying writer. + pub fn get_ref(&self) -> &W { + self.inner.get_ref() + } + + /// Acquires a mutable reference to the underlying writer. + /// + /// Note that mutating the output/input state of the stream may corrupt this + /// object, so care must be taken when using this method. + pub fn get_mut(&mut self) -> &mut W { + self.inner.get_mut() + } + + /// Resets the state of this encoder entirely, swapping out the output + /// stream for another. + /// + /// This function will finish encoding the current stream into the current + /// output stream before swapping out the two output streams. If the stream + /// cannot be finished an error is returned. + /// + /// After the current stream has been finished, this will reset the internal + /// state of this encoder and replace the output stream with the one + /// provided, returning the previous output stream. Future data written to + /// this encoder will be the compressed into the stream `w` provided. + /// + /// # Errors + /// + /// This function will perform I/O to complete this stream, and any I/O + /// errors which occur will be returned from this function. + pub fn reset(&mut self, w: W) -> io::Result { + try!(self.inner.finish()); + self.inner.data.reset(); + Ok(self.inner.replace(w)) + } + + /// Attempt to finish this output stream, writing out final chunks of data. + /// + /// Note that this function can only be used once data has finished being + /// written to the output stream. After this function is called then further + /// calls to `write` may result in a panic. + /// + /// # Panics + /// + /// Attempts to write data to this stream may result in a panic after this + /// function is called. + /// + /// # Errors + /// + /// This function will perform I/O to complete this stream, and any I/O + /// errors which occur will be returned from this function. + pub fn try_finish(&mut self) -> io::Result<()> { + self.inner.finish() + } + + /// Consumes this encoder, flushing the output stream. + /// + /// This will flush the underlying data stream, close off the compressed + /// stream and, if successful, return the contained writer. + /// + /// Note that this function may not be suitable to call in a situation where + /// the underlying stream is an asynchronous I/O stream. To finish a stream + /// the `try_finish` (or `shutdown`) method should be used instead. To + /// re-acquire ownership of a stream it is safe to call this method after + /// `try_finish` or `shutdown` has returned `Ok`. + /// + /// # Errors + /// + /// This function will perform I/O to complete this stream, and any I/O + /// errors which occur will be returned from this function. + pub fn finish(mut self) -> io::Result { + try!(self.inner.finish()); + Ok(self.inner.take_inner()) + } + + /// Consumes this encoder, flushing the output stream. + /// + /// This will flush the underlying data stream and then return the contained + /// writer if the flush succeeded. + /// The compressed stream will not closed but only flushed. This + /// means that obtained byte array can by extended by another deflated + /// stream. To close the stream add the two bytes 0x3 and 0x0. + /// + /// # Errors + /// + /// This function will perform I/O to complete this stream, and any I/O + /// errors which occur will be returned from this function. + pub fn flush_finish(mut self) -> io::Result { + try!(self.inner.flush()); + Ok(self.inner.take_inner()) + } + + /// Returns the number of bytes that have been written to this compresor. + /// + /// Note that not all bytes written to this object may be accounted for, + /// there may still be some active buffering. + pub fn total_in(&self) -> u64 { + self.inner.data.total_in() + } + + /// Returns the number of bytes that the compressor has produced. + /// + /// Note that not all bytes may have been written yet, some may still be + /// buffered. + pub fn total_out(&self) -> u64 { + self.inner.data.total_out() + } +} + +impl Write for DeflateEncoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.inner.write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } +} + +#[cfg(feature = "tokio")] +impl AsyncWrite for DeflateEncoder { + fn shutdown(&mut self) -> Poll<(), io::Error> { + try_nb!(self.inner.finish()); + self.inner.get_mut().shutdown() + } +} + +impl Read for DeflateEncoder { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + self.inner.get_mut().read(buf) + } +} + +#[cfg(feature = "tokio")] +impl AsyncRead for DeflateEncoder {} + +/// A DEFLATE decoder, or decompressor. +/// +/// This structure implements a [`Write`] and will emit a stream of decompressed +/// data when fed a stream of compressed data. +/// +/// [`Write`]: https://doc.rust-lang.org/std/io/trait.Read.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use std::io; +/// # use flate2::Compression; +/// # use flate2::write::DeflateEncoder; +/// use flate2::write::DeflateDecoder; +/// +/// # fn main() { +/// # let mut e = DeflateEncoder::new(Vec::new(), Compression::Default); +/// # e.write(b"Hello World").unwrap(); +/// # let bytes = e.finish().unwrap(); +/// # println!("{}", decode_writer(bytes).unwrap()); +/// # } +/// // Uncompresses a Deflate Encoded vector of bytes and returns a string or error +/// // Here Vec implements Write +/// fn decode_writer(bytes: Vec) -> io::Result { +/// let mut writer = Vec::new(); +/// let mut deflater = DeflateDecoder::new(writer); +/// deflater.write(&bytes[..])?; +/// writer = deflater.finish()?; +/// let return_string = String::from_utf8(writer).expect("String parsing error"); +/// Ok(return_string) +/// } +/// ``` +#[derive(Debug)] +pub struct DeflateDecoder { + inner: zio::Writer, +} + + +impl DeflateDecoder { + /// Creates a new decoder which will write uncompressed data to the stream. + /// + /// When this encoder is dropped or unwrapped the final pieces of data will + /// be flushed. + pub fn new(w: W) -> DeflateDecoder { + DeflateDecoder { + inner: zio::Writer::new(w, Decompress::new(false)), + } + } + + /// Acquires a reference to the underlying writer. + pub fn get_ref(&self) -> &W { + self.inner.get_ref() + } + + /// Acquires a mutable reference to the underlying writer. + /// + /// Note that mutating the output/input state of the stream may corrupt this + /// object, so care must be taken when using this method. + pub fn get_mut(&mut self) -> &mut W { + self.inner.get_mut() + } + + /// Resets the state of this decoder entirely, swapping out the output + /// stream for another. + /// + /// This function will finish encoding the current stream into the current + /// output stream before swapping out the two output streams. + /// + /// This will then reset the internal state of this decoder and replace the + /// output stream with the one provided, returning the previous output + /// stream. Future data written to this decoder will be decompressed into + /// the output stream `w`. + /// + /// # Errors + /// + /// This function will perform I/O to finish the stream, and if that I/O + /// returns an error then that will be returned from this function. + pub fn reset(&mut self, w: W) -> io::Result { + try!(self.inner.finish()); + self.inner.data = Decompress::new(false); + Ok(self.inner.replace(w)) + } + + /// Attempt to finish this output stream, writing out final chunks of data. + /// + /// Note that this function can only be used once data has finished being + /// written to the output stream. After this function is called then further + /// calls to `write` may result in a panic. + /// + /// # Panics + /// + /// Attempts to write data to this stream may result in a panic after this + /// function is called. + /// + /// # Errors + /// + /// This function will perform I/O to finish the stream, returning any + /// errors which happen. + pub fn try_finish(&mut self) -> io::Result<()> { + self.inner.finish() + } + + /// Consumes this encoder, flushing the output stream. + /// + /// This will flush the underlying data stream and then return the contained + /// writer if the flush succeeded. + /// + /// Note that this function may not be suitable to call in a situation where + /// the underlying stream is an asynchronous I/O stream. To finish a stream + /// the `try_finish` (or `shutdown`) method should be used instead. To + /// re-acquire ownership of a stream it is safe to call this method after + /// `try_finish` or `shutdown` has returned `Ok`. + /// + /// # Errors + /// + /// This function will perform I/O to complete this stream, and any I/O + /// errors which occur will be returned from this function. + pub fn finish(mut self) -> io::Result { + try!(self.inner.finish()); + Ok(self.inner.take_inner()) + } + + /// Returns the number of bytes that the decompressor has consumed for + /// decompression. + /// + /// Note that this will likely be smaller than the number of bytes + /// successfully written to this stream due to internal buffering. + pub fn total_in(&self) -> u64 { + self.inner.data.total_in() + } + + /// Returns the number of bytes that the decompressor has written to its + /// output stream. + pub fn total_out(&self) -> u64 { + self.inner.data.total_out() + } +} + +impl Write for DeflateDecoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.inner.write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } +} + +#[cfg(feature = "tokio")] +impl AsyncWrite for DeflateDecoder { + fn shutdown(&mut self) -> Poll<(), io::Error> { + try_nb!(self.inner.finish()); + self.inner.get_mut().shutdown() + } +} + +impl Read for DeflateDecoder { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + self.inner.get_mut().read(buf) + } +} + +#[cfg(feature = "tokio")] +impl AsyncRead for DeflateDecoder {} diff --git a/src/gz.rs b/src/gz.rs deleted file mode 100644 index 0d1465384..000000000 --- a/src/gz.rs +++ /dev/null @@ -1,1325 +0,0 @@ -//! gzip compression/decompression -//! -//! [1]: http://www.gzip.org/zlib/rfc-gzip.html - -use std::cmp; -use std::env; -use std::ffi::CString; -use std::io::prelude::*; -use std::io; -use std::mem; -use std::time; - -#[cfg(feature = "tokio")] -use futures::Poll; -#[cfg(feature = "tokio")] -use tokio_io::{AsyncRead, AsyncWrite}; - -use {Compression, Compress}; -use bufreader::BufReader; -use crc::{CrcReader, Crc}; -use deflate; -use zio; - -static FHCRC: u8 = 1 << 1; -static FEXTRA: u8 = 1 << 2; -static FNAME: u8 = 1 << 3; -static FCOMMENT: u8 = 1 << 4; - -/// A gzip streaming encoder -/// -/// This structure exposes a [`Write`] interface that will emit compressed data -/// to the underlying writer `W`. -/// -/// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use flate2::Compression; -/// use flate2::write::GzEncoder; -/// -/// // Vec implements Write to print the compressed bytes of sample string -/// # fn main() { -/// -/// let mut e = GzEncoder::new(Vec::new(), Compression::Default); -/// e.write(b"Hello World").unwrap(); -/// println!("{:?}", e.finish().unwrap()); -/// # } -/// ``` -#[derive(Debug)] -pub struct EncoderWriter { - inner: zio::Writer, - crc: Crc, - crc_bytes_written: usize, - header: Vec, -} - -/// A gzip streaming encoder -/// -/// This structure exposes a [`Read`] interface that will read uncompressed data -/// from the underlying reader and expose the compressed version as a [`Read`] -/// interface. -/// -/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// use flate2::Compression; -/// use flate2::read::GzEncoder; -/// -/// // Return a vector containing the GZ compressed version of hello world -/// -/// fn gzencode_hello_world() -> io::Result> { -/// let mut ret_vec = [0;100]; -/// let bytestring = b"hello world"; -/// let mut gz = GzEncoder::new(&bytestring[..], Compression::Fast); -/// let count = gz.read(&mut ret_vec)?; -/// Ok(ret_vec[0..count].to_vec()) -/// } -/// ``` -#[derive(Debug)] -pub struct EncoderReader { - inner: EncoderReaderBuf>, -} - -/// A gzip streaming encoder -/// -/// This structure exposes a [`BufRead`] interface that will read uncompressed data -/// from the underlying reader and expose the compressed version as a [`BufRead`] -/// interface. -/// -/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// use flate2::Compression; -/// use flate2::bufread::GzEncoder; -/// use std::fs::File; -/// use std::io::BufReader; -/// -/// // Opens sample file, compresses the contents and returns a Vector or error -/// // File wrapped in a BufReader implements BufRead -/// -/// fn open_hello_world() -> io::Result> { -/// let f = File::open("examples/hello_world.txt")?; -/// let b = BufReader::new(f); -/// let mut gz = GzEncoder::new(b, Compression::Fast); -/// let mut buffer = Vec::new(); -/// gz.read_to_end(&mut buffer)?; -/// Ok(buffer) -/// } -/// ``` -#[derive(Debug)] -pub struct EncoderReaderBuf { - inner: deflate::EncoderReaderBuf>, - header: Vec, - pos: usize, - eof: bool, -} - -/// A builder structure to create a new gzip Encoder. -/// -/// This structure controls header configuration options such as the filename. -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// # use std::io; -/// use std::fs::File; -/// use flate2::GzBuilder; -/// use flate2::Compression; -/// -/// // GzBuilder opens a file and writes a sample string using Builder pattern -/// -/// # fn sample_builder() -> Result<(), io::Error> { -/// let f = File::create("examples/hello_world.gz")?; -/// let mut gz = GzBuilder::new() -/// .filename("hello_world.txt") -/// .comment("test file, please delete") -/// .write(f, Compression::Default); -/// gz.write(b"hello world")?; -/// gz.finish()?; -/// # Ok(()) -/// # } -/// ``` -#[derive(Debug)] -pub struct Builder { - extra: Option>, - filename: Option, - comment: Option, - mtime: u32, -} - -/// A gzip streaming decoder -/// -/// This structure exposes a [`Read`] interface that will consume compressed -/// data from the underlying reader and emit uncompressed data. -/// -/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html -/// -/// # Examples -/// -/// ``` -/// -/// use std::io::prelude::*; -/// use std::io; -/// # use flate2::Compression; -/// # use flate2::write::GzEncoder; -/// use flate2::read::GzDecoder; -/// -/// # fn main() { -/// # let mut e = GzEncoder::new(Vec::new(), Compression::Default); -/// # e.write(b"Hello World").unwrap(); -/// # let bytes = e.finish().unwrap(); -/// # println!("{}", decode_reader(bytes).unwrap()); -/// # } -/// # -/// // Uncompresses a Gz Encoded vector of bytes and returns a string or error -/// // Here &[u8] implements Read -/// -/// fn decode_reader(bytes: Vec) -> io::Result { -/// let mut gz = GzDecoder::new(&bytes[..])?; -/// let mut s = String::new(); -/// gz.read_to_string(&mut s)?; -/// Ok(s) -/// } -/// ``` -#[derive(Debug)] -pub struct DecoderReader { - inner: DecoderReaderBuf>, -} - -/// A gzip streaming decoder that decodes all members of a multistream -/// -/// A gzip member consists of a header, compressed data and a trailer. The [gzip -/// specification](https://tools.ietf.org/html/rfc1952), however, allows multiple -/// gzip members to be joined in a single stream. `MultiDecoderReader` will -/// decode all consecutive members while `DecoderReader` will only decompress the -/// first gzip member. The multistream format is commonly used in bioinformatics, -/// for example when using the BGZF compressed data. -/// -/// This structure exposes a [`Read`] interface that will consume all gzip members -/// from the underlying reader and emit uncompressed data. -/// -/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// # use flate2::Compression; -/// # use flate2::write::GzEncoder; -/// use flate2::read::MultiGzDecoder; -/// -/// # fn main() { -/// # let mut e = GzEncoder::new(Vec::new(), Compression::Default); -/// # e.write(b"Hello World").unwrap(); -/// # let bytes = e.finish().unwrap(); -/// # println!("{}", decode_reader(bytes).unwrap()); -/// # } -/// # -/// // Uncompresses a Gz Encoded vector of bytes and returns a string or error -/// // Here &[u8] implements Read -/// -/// fn decode_reader(bytes: Vec) -> io::Result { -/// let mut gz = MultiGzDecoder::new(&bytes[..])?; -/// let mut s = String::new(); -/// gz.read_to_string(&mut s)?; -/// Ok(s) -/// } -/// ``` -#[derive(Debug)] -pub struct MultiDecoderReader { - inner: MultiDecoderReaderBuf>, -} - -/// A gzip streaming decoder -/// -/// This structure exposes a [`ReadBuf`] interface that will consume compressed -/// data from the underlying reader and emit uncompressed data. -/// -/// [`ReadBuf`]: https://doc.rust-lang.org/std/io/trait.BufRead.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// # use flate2::Compression; -/// # use flate2::write::GzEncoder; -/// use flate2::bufread::GzDecoder; -/// -/// # fn main() { -/// # let mut e = GzEncoder::new(Vec::new(), Compression::Default); -/// # e.write(b"Hello World").unwrap(); -/// # let bytes = e.finish().unwrap(); -/// # println!("{}", decode_reader(bytes).unwrap()); -/// # } -/// # -/// // Uncompresses a Gz Encoded vector of bytes and returns a string or error -/// // Here &[u8] implements BufRead -/// -/// fn decode_reader(bytes: Vec) -> io::Result { -/// let mut gz = GzDecoder::new(&bytes[..])?; -/// let mut s = String::new(); -/// gz.read_to_string(&mut s)?; -/// Ok(s) -/// } -/// ``` -#[derive(Debug)] -pub struct DecoderReaderBuf { - inner: CrcReader>, - header: Header, - finished: bool, -} - -/// A gzip streaming decoder that decodes all members of a multistream -/// -/// A gzip member consists of a header, compressed data and a trailer. The [gzip -/// specification](https://tools.ietf.org/html/rfc1952), however, allows multiple -/// gzip members to be joined in a single stream. `MultiDecoderReaderBuf` will -/// decode all consecutive members while `DecoderReaderBuf` will only decompress -/// the first gzip member. The multistream format is commonly used in -/// bioinformatics, for example when using the BGZF compressed data. -/// -/// This structure exposes a [`BufRead`] interface that will consume all gzip members -/// from the underlying reader and emit uncompressed data. -/// -/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// # use flate2::Compression; -/// # use flate2::write::GzEncoder; -/// use flate2::bufread::MultiGzDecoder; -/// -/// # fn main() { -/// # let mut e = GzEncoder::new(Vec::new(), Compression::Default); -/// # e.write(b"Hello World").unwrap(); -/// # let bytes = e.finish().unwrap(); -/// # println!("{}", decode_reader(bytes).unwrap()); -/// # } -/// # -/// // Uncompresses a Gz Encoded vector of bytes and returns a string or error -/// // Here &[u8] implements BufRead -/// -/// fn decode_reader(bytes: Vec) -> io::Result { -/// let mut gz = MultiGzDecoder::new(&bytes[..])?; -/// let mut s = String::new(); -/// gz.read_to_string(&mut s)?; -/// Ok(s) -/// } -/// ``` -#[derive(Debug)] -pub struct MultiDecoderReaderBuf { - inner: CrcReader>, - header: Header, - finished: bool, -} - -/// A structure representing the header of a gzip stream. -/// -/// The header can contain metadata about the file that was compressed, if -/// present. -#[derive(PartialEq, Debug)] -pub struct Header { - extra: Option>, - filename: Option>, - comment: Option>, - mtime: u32, -} - -impl Builder { - /// Create a new blank builder with no header by default. - pub fn new() -> Builder { - Builder { - extra: None, - filename: None, - comment: None, - mtime: 0, - } - } - - /// Configure the `mtime` field in the gzip header. - pub fn mtime(mut self, mtime: u32) -> Builder { - self.mtime = mtime; - self - } - - /// Configure the `extra` field in the gzip header. - pub fn extra>>(mut self, extra: T) -> Builder { - self.extra = Some(extra.into()); - self - } - - /// Configure the `filename` field in the gzip header. - /// - /// # Panics - /// - /// Panics if the `filename` slice contains a zero. - pub fn filename>>(mut self, filename: T) -> Builder { - self.filename = Some(CString::new(filename.into()).unwrap()); - self - } - - /// Configure the `comment` field in the gzip header. - /// - /// # Panics - /// - /// Panics if the `comment` slice contains a zero. - pub fn comment>>(mut self, comment: T) -> Builder { - self.comment = Some(CString::new(comment.into()).unwrap()); - self - } - - /// Consume this builder, creating a writer encoder in the process. - /// - /// The data written to the returned encoder will be compressed and then - /// written out to the supplied parameter `w`. - pub fn write(self, w: W, lvl: Compression) -> EncoderWriter { - EncoderWriter { - inner: zio::Writer::new(w, Compress::new(lvl, false)), - crc: Crc::new(), - header: self.into_header(lvl), - crc_bytes_written: 0, - } - } - - /// Consume this builder, creating a reader encoder in the process. - /// - /// Data read from the returned encoder will be the compressed version of - /// the data read from the given reader. - pub fn read(self, r: R, lvl: Compression) -> EncoderReader { - EncoderReader { - inner: self.buf_read(BufReader::new(r), lvl), - } - } - - /// Consume this builder, creating a reader encoder in the process. - /// - /// Data read from the returned encoder will be the compressed version of - /// the data read from the given reader. - pub fn buf_read(self, r: R, lvl: Compression) -> EncoderReaderBuf - where R: BufRead, - { - let crc = CrcReader::new(r); - EncoderReaderBuf { - inner: deflate::EncoderReaderBuf::new(crc, lvl), - header: self.into_header(lvl), - pos: 0, - eof: false, - } - } - - fn into_header(self, lvl: Compression) -> Vec { - let Builder { extra, filename, comment, mtime } = self; - let mut flg = 0; - let mut header = vec![0u8; 10]; - match extra { - Some(v) => { - flg |= FEXTRA; - header.push((v.len() >> 0) as u8); - header.push((v.len() >> 8) as u8); - header.extend(v); - } - None => {} - } - match filename { - Some(filename) => { - flg |= FNAME; - header.extend(filename.as_bytes_with_nul().iter().map(|x| *x)); - } - None => {} - } - match comment { - Some(comment) => { - flg |= FCOMMENT; - header.extend(comment.as_bytes_with_nul().iter().map(|x| *x)); - } - None => {} - } - header[0] = 0x1f; - header[1] = 0x8b; - header[2] = 8; - header[3] = flg; - header[4] = (mtime >> 0) as u8; - header[5] = (mtime >> 8) as u8; - header[6] = (mtime >> 16) as u8; - header[7] = (mtime >> 24) as u8; - header[8] = match lvl { - Compression::Best => 2, - Compression::Fast => 4, - _ => 0, - }; - header[9] = match env::consts::OS { - "linux" => 3, - "macos" => 7, - "win32" => 0, - _ => 255, - }; - return header; - } -} - -impl EncoderWriter { - /// Creates a new encoder which will use the given compression level. - /// - /// The encoder is not configured specially for the emitted header. For - /// header configuration, see the `Builder` type. - /// - /// The data written to the returned encoder will be compressed and then - /// written to the stream `w`. - pub fn new(w: W, level: Compression) -> EncoderWriter { - Builder::new().write(w, level) - } - - /// Acquires a reference to the underlying writer. - pub fn get_ref(&self) -> &W { - self.inner.get_ref() - } - - /// Acquires a mutable reference to the underlying writer. - /// - /// Note that mutation of the writer may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut W { - self.inner.get_mut() - } - - /// Attempt to finish this output stream, writing out final chunks of data. - /// - /// Note that this function can only be used once data has finished being - /// written to the output stream. After this function is called then further - /// calls to `write` may result in a panic. - /// - /// # Panics - /// - /// Attempts to write data to this stream may result in a panic after this - /// function is called. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn try_finish(&mut self) -> io::Result<()> { - try!(self.write_header()); - try!(self.inner.finish()); - - while self.crc_bytes_written < 8 { - let (sum, amt) = (self.crc.sum() as u32, self.crc.amount()); - let buf = [(sum >> 0) as u8, - (sum >> 8) as u8, - (sum >> 16) as u8, - (sum >> 24) as u8, - (amt >> 0) as u8, - (amt >> 8) as u8, - (amt >> 16) as u8, - (amt >> 24) as u8]; - let inner = self.inner.get_mut(); - let n = try!(inner.write(&buf[self.crc_bytes_written..])); - self.crc_bytes_written += n; - } - Ok(()) - } - - /// Finish encoding this stream, returning the underlying writer once the - /// encoding is done. - /// - /// Note that this function may not be suitable to call in a situation where - /// the underlying stream is an asynchronous I/O stream. To finish a stream - /// the `try_finish` (or `shutdown`) method should be used instead. To - /// re-acquire ownership of a stream it is safe to call this method after - /// `try_finish` or `shutdown` has returned `Ok`. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn finish(mut self) -> io::Result { - try!(self.try_finish()); - Ok(self.inner.take_inner()) - } - - fn write_header(&mut self) -> io::Result<()> { - while self.header.len() > 0 { - let n = try!(self.inner.get_mut().write(&self.header)); - self.header.drain(..n); - } - Ok(()) - } -} - -impl Write for EncoderWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - assert_eq!(self.crc_bytes_written, 0); - try!(self.write_header()); - let n = try!(self.inner.write(buf)); - self.crc.update(&buf[..n]); - Ok(n) - } - - fn flush(&mut self) -> io::Result<()> { - assert_eq!(self.crc_bytes_written, 0); - try!(self.write_header()); - self.inner.flush() - } -} - -#[cfg(feature = "tokio")] -impl AsyncWrite for EncoderWriter { - fn shutdown(&mut self) -> Poll<(), io::Error> { - try_nb!(self.try_finish()); - self.get_mut().shutdown() - } -} - -impl Read for EncoderWriter { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - self.get_mut().read(buf) - } -} - -#[cfg(feature = "tokio")] -impl AsyncRead for EncoderWriter { -} - -impl Drop for EncoderWriter { - fn drop(&mut self) { - if self.inner.is_present() { - let _ = self.try_finish(); - } - } -} - -impl EncoderReader { - /// Creates a new encoder which will use the given compression level. - /// - /// The encoder is not configured specially for the emitted header. For - /// header configuration, see the `Builder` type. - /// - /// The data read from the stream `r` will be compressed and available - /// through the returned reader. - pub fn new(r: R, level: Compression) -> EncoderReader { - Builder::new().read(r, level) - } -} - -impl EncoderReader { - /// Acquires a reference to the underlying reader. - pub fn get_ref(&self) -> &R { - self.inner.get_ref().get_ref() - } - - /// Acquires a mutable reference to the underlying reader. - /// - /// Note that mutation of the reader may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - self.inner.get_mut().get_mut() - } - - /// Returns the underlying stream, consuming this encoder - pub fn into_inner(self) -> R { - self.inner.into_inner().into_inner() - } -} - -fn copy(into: &mut [u8], from: &[u8], pos: &mut usize) -> usize { - let min = cmp::min(into.len(), from.len() - *pos); - for (slot, val) in into.iter_mut().zip(from[*pos..*pos + min].iter()) { - *slot = *val; - } - *pos += min; - return min; -} - -impl Read for EncoderReader { - fn read(&mut self, into: &mut [u8]) -> io::Result { - self.inner.read(into) - } -} - -impl Write for EncoderReader { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -impl EncoderReaderBuf { - /// Creates a new encoder which will use the given compression level. - /// - /// The encoder is not configured specially for the emitted header. For - /// header configuration, see the `Builder` type. - /// - /// The data read from the stream `r` will be compressed and available - /// through the returned reader. - pub fn new(r: R, level: Compression) -> EncoderReaderBuf { - Builder::new().buf_read(r, level) - } - - fn read_footer(&mut self, into: &mut [u8]) -> io::Result { - if self.pos == 8 { - return Ok(0); - } - let crc = self.inner.get_ref().crc(); - let ref arr = [(crc.sum() >> 0) as u8, - (crc.sum() >> 8) as u8, - (crc.sum() >> 16) as u8, - (crc.sum() >> 24) as u8, - (crc.amount() >> 0) as u8, - (crc.amount() >> 8) as u8, - (crc.amount() >> 16) as u8, - (crc.amount() >> 24) as u8]; - Ok(copy(into, arr, &mut self.pos)) - } -} - -impl EncoderReaderBuf { - /// Acquires a reference to the underlying reader. - pub fn get_ref(&self) -> &R { - self.inner.get_ref().get_ref() - } - - /// Acquires a mutable reference to the underlying reader. - /// - /// Note that mutation of the reader may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - self.inner.get_mut().get_mut() - } - - /// Returns the underlying stream, consuming this encoder - pub fn into_inner(self) -> R { - self.inner.into_inner().into_inner() - } -} - -impl Read for EncoderReaderBuf { - fn read(&mut self, mut into: &mut [u8]) -> io::Result { - let mut amt = 0; - if self.eof { - return self.read_footer(into); - } else if self.pos < self.header.len() { - amt += copy(into, &self.header, &mut self.pos); - if amt == into.len() { - return Ok(amt); - } - let tmp = into; - into = &mut tmp[amt..]; - } - match try!(self.inner.read(into)) { - 0 => { - self.eof = true; - self.pos = 0; - self.read_footer(into) - } - n => Ok(amt + n), - } - } -} - -impl Write for EncoderReaderBuf { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -impl DecoderReader { - /// Creates a new decoder from the given reader, immediately parsing the - /// gzip header. - /// - /// # Errors - /// - /// If an error is encountered when parsing the gzip header, an error is - /// returned. - pub fn new(r: R) -> io::Result> { - DecoderReaderBuf::new(BufReader::new(r)).map(|r| { - DecoderReader { inner: r } - }) - } -} - -impl DecoderReader { - /// Returns the header associated with this stream. - pub fn header(&self) -> &Header { - self.inner.header() - } - - /// Acquires a reference to the underlying reader. - pub fn get_ref(&self) -> &R { - self.inner.get_ref().get_ref() - } - - /// Acquires a mutable reference to the underlying stream. - /// - /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - self.inner.get_mut().get_mut() - } - - /// Consumes this decoder, returning the underlying reader. - pub fn into_inner(self) -> R { - self.inner.into_inner().into_inner() - } -} - -impl Read for DecoderReader { - fn read(&mut self, into: &mut [u8]) -> io::Result { - self.inner.read(into) - } -} - -impl Write for DecoderReader { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -impl MultiDecoderReader { - /// Creates a new decoder from the given reader, immediately parsing the - /// (first) gzip header. If the gzip stream contains multiple members all will - /// be decoded. - /// - /// # Errors - /// - /// If an error is encountered when parsing the gzip header, an error is - /// returned. - pub fn new(r: R) -> io::Result> { - MultiDecoderReaderBuf::new(BufReader::new(r)).map(|r| { - MultiDecoderReader { inner: r } - }) - } -} - -impl MultiDecoderReader { - /// Returns the current header associated with this stream. - pub fn header(&self) -> &Header { - self.inner.header() - } - - /// Acquires a reference to the underlying reader. - pub fn get_ref(&self) -> &R { - self.inner.get_ref().get_ref() - } - - /// Acquires a mutable reference to the underlying stream. - /// - /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - self.inner.get_mut().get_mut() - } - - /// Consumes this decoder, returning the underlying reader. - pub fn into_inner(self) -> R { - self.inner.into_inner().into_inner() - } -} - -impl Read for MultiDecoderReader { - fn read(&mut self, into: &mut [u8]) -> io::Result { - self.inner.read(into) - } -} - -impl Write for MultiDecoderReader { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -impl DecoderReaderBuf { - /// Creates a new decoder from the given reader, immediately parsing the - /// gzip header. - /// - /// # Errors - /// - /// If an error is encountered when parsing the gzip header, an error is - /// returned. - pub fn new(mut r: R) -> io::Result> { - let header = try!(read_gz_header(&mut r)); - - let flate = deflate::DecoderReaderBuf::new(r); - return Ok(DecoderReaderBuf { - inner: CrcReader::new(flate), - header: header, - finished: false, - }); - } - - fn finish(&mut self) -> io::Result<()> { - if self.finished { - return Ok(()); - } - let ref mut buf = [0u8; 8]; - { - let mut len = 0; - - while len < buf.len() { - match try!(self.inner.get_mut().get_mut().read(&mut buf[len..])) { - 0 => return Err(corrupt()), - n => len += n, - } - } - } - - let crc = ((buf[0] as u32) << 0) | ((buf[1] as u32) << 8) | - ((buf[2] as u32) << 16) | - ((buf[3] as u32) << 24); - let amt = ((buf[4] as u32) << 0) | ((buf[5] as u32) << 8) | - ((buf[6] as u32) << 16) | - ((buf[7] as u32) << 24); - if crc != self.inner.crc().sum() as u32 { - return Err(corrupt()); - } - if amt != self.inner.crc().amount() { - return Err(corrupt()); - } - self.finished = true; - Ok(()) - } -} - -impl DecoderReaderBuf { - /// Returns the header associated with this stream. - pub fn header(&self) -> &Header { - &self.header - } - - /// Acquires a reference to the underlying reader. - pub fn get_ref(&self) -> &R { - self.inner.get_ref().get_ref() - } - - /// Acquires a mutable reference to the underlying stream. - /// - /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - self.inner.get_mut().get_mut() - } - - /// Consumes this decoder, returning the underlying reader. - pub fn into_inner(self) -> R { - self.inner.into_inner().into_inner() - } -} - -impl Read for DecoderReaderBuf { - fn read(&mut self, into: &mut [u8]) -> io::Result { - match try!(self.inner.read(into)) { - 0 => { - try!(self.finish()); - Ok(0) - } - n => Ok(n), - } - } -} - -impl Write for DecoderReaderBuf { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -impl MultiDecoderReaderBuf { - /// Creates a new decoder from the given reader, immediately parsing the - /// (first) gzip header. If the gzip stream contains multiple members all will - /// be decoded. - /// - /// # Errors - /// - /// If an error is encountered when parsing the gzip header, an error is - /// returned. - pub fn new(mut r: R) -> io::Result> { - let header = try!(read_gz_header(&mut r)); - - let flate = deflate::DecoderReaderBuf::new(r); - return Ok(MultiDecoderReaderBuf { - inner: CrcReader::new(flate), - header: header, - finished: false, - }); - } - - fn finish_member(&mut self) -> io::Result { - if self.finished { - return Ok(0); - } - let ref mut buf = [0u8; 8]; - { - let mut len = 0; - - while len < buf.len() { - match try!(self.inner.get_mut().get_mut().read(&mut buf[len..])) { - 0 => return Err(corrupt()), - n => len += n, - } - } - } - - let crc = ((buf[0] as u32) << 0) | ((buf[1] as u32) << 8) | - ((buf[2] as u32) << 16) | - ((buf[3] as u32) << 24); - let amt = ((buf[4] as u32) << 0) | ((buf[5] as u32) << 8) | - ((buf[6] as u32) << 16) | - ((buf[7] as u32) << 24); - if crc != self.inner.crc().sum() as u32 { - return Err(corrupt()); - } - if amt != self.inner.crc().amount() { - return Err(corrupt()); - } - let remaining = match self.inner.get_mut().get_mut().fill_buf() { - Ok(b) => { - if b.is_empty() { - self.finished = true; - return Ok(0); - } else { - b.len() - } - }, - Err(e) => return Err(e) - }; - - let next_header = try!(read_gz_header(self.inner.get_mut().get_mut())); - mem::replace(&mut self.header, next_header); - self.inner.reset(); - self.inner.get_mut().reset_data(); - - Ok(remaining) - } -} - -impl MultiDecoderReaderBuf { - /// Returns the current header associated with this stream. - pub fn header(&self) -> &Header { - &self.header - } - - /// Acquires a reference to the underlying reader. - pub fn get_ref(&self) -> &R { - self.inner.get_ref().get_ref() - } - - /// Acquires a mutable reference to the underlying stream. - /// - /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - self.inner.get_mut().get_mut() - } - - /// Consumes this decoder, returning the underlying reader. - pub fn into_inner(self) -> R { - self.inner.into_inner().into_inner() - } -} - -impl Read for MultiDecoderReaderBuf { - fn read(&mut self, into: &mut [u8]) -> io::Result { - match try!(self.inner.read(into)) { - 0 => { - match self.finish_member() { - Ok(0) => Ok(0), - Ok(_) => self.read(into), - Err(e) => Err(e) - } - }, - n => Ok(n), - } - } -} - -impl Write for MultiDecoderReaderBuf { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -impl Header { - /// Returns the `filename` field of this gzip stream's header, if present. - pub fn filename(&self) -> Option<&[u8]> { - self.filename.as_ref().map(|s| &s[..]) - } - - /// Returns the `extra` field of this gzip stream's header, if present. - pub fn extra(&self) -> Option<&[u8]> { - self.extra.as_ref().map(|s| &s[..]) - } - - /// Returns the `comment` field of this gzip stream's header, if present. - pub fn comment(&self) -> Option<&[u8]> { - self.comment.as_ref().map(|s| &s[..]) - } - - /// This gives the most recent modification time of the original file being compressed. - /// - /// The time is in Unix format, i.e., seconds since 00:00:00 GMT, Jan. 1, 1970. - /// (Note that this may cause problems for MS-DOS and other systems that use local - /// rather than Universal time.) If the compressed data did not come from a file, - /// `mtime` is set to the time at which compression started. - /// `mtime` = 0 means no time stamp is available. - /// - /// The usage of `mtime` is discouraged because of Year 2038 problem. - pub fn mtime(&self) -> u32 { - self.mtime - } - - /// Returns the most recent modification time represented by a date-time type. - /// Returns `None` if the value of the underlying counter is 0, - /// indicating no time stamp is available. - /// - /// - /// The time is measured as seconds since 00:00:00 GMT, Jan. 1 1970. - /// See [`mtime`](#method.mtime) for more detail. - pub fn mtime_as_datetime(&self) -> Option { - if self.mtime == 0 { - None - } else { - let duration = time::Duration::new(u64::from(self.mtime), 0); - let datetime = time::UNIX_EPOCH + duration; - Some(datetime) - } - } -} - -fn corrupt() -> io::Error { - io::Error::new(io::ErrorKind::InvalidInput, - "corrupt gzip stream does not have a matching checksum") -} - -fn bad_header() -> io::Error { - io::Error::new(io::ErrorKind::InvalidInput, "invalid gzip header") -} - -fn read_le_u16(r: &mut R) -> io::Result { - let mut b = [0; 2]; - try!(r.read_exact(&mut b)); - Ok((b[0] as u16) | ((b[1] as u16) << 8)) -} - -fn read_gz_header(r: &mut R) -> io::Result
{ - let mut crc_reader = CrcReader::new(r); - let mut header = [0; 10]; - try!(crc_reader.read_exact(&mut header)); - - let id1 = header[0]; - let id2 = header[1]; - if id1 != 0x1f || id2 != 0x8b { - return Err(bad_header()); - } - let cm = header[2]; - if cm != 8 { - return Err(bad_header()); - } - - let flg = header[3]; - let mtime = ((header[4] as u32) << 0) | ((header[5] as u32) << 8) | - ((header[6] as u32) << 16) | - ((header[7] as u32) << 24); - let _xfl = header[8]; - let _os = header[9]; - - let extra = if flg & FEXTRA != 0 { - let xlen = try!(read_le_u16(&mut crc_reader)); - let mut extra = vec![0; xlen as usize]; - try!(crc_reader.read_exact(&mut extra)); - Some(extra) - } else { - None - }; - let filename = if flg & FNAME != 0 { - // wow this is slow - let mut b = Vec::new(); - for byte in crc_reader.by_ref().bytes() { - let byte = try!(byte); - if byte == 0 { - break; - } - b.push(byte); - } - Some(b) - } else { - None - }; - let comment = if flg & FCOMMENT != 0 { - // wow this is slow - let mut b = Vec::new(); - for byte in crc_reader.by_ref().bytes() { - let byte = try!(byte); - if byte == 0 { - break; - } - b.push(byte); - } - Some(b) - } else { - None - }; - - if flg & FHCRC != 0 { - let calced_crc = crc_reader.crc().sum() as u16; - let stored_crc = try!(read_le_u16(&mut crc_reader)); - if calced_crc != stored_crc { - return Err(corrupt()); - } - } - - Ok(Header { - extra: extra, - filename: filename, - comment: comment, - mtime: mtime, - }) -} - -#[cfg(test)] -mod tests { - use std::io::prelude::*; - - use super::{EncoderWriter, EncoderReader, DecoderReader, Builder}; - use Compression::Default; - use rand::{thread_rng, Rng}; - - #[test] - fn roundtrip() { - let mut e = EncoderWriter::new(Vec::new(), Default); - e.write_all(b"foo bar baz").unwrap(); - let inner = e.finish().unwrap(); - let mut d = DecoderReader::new(&inner[..]).unwrap(); - let mut s = String::new(); - d.read_to_string(&mut s).unwrap(); - assert_eq!(s, "foo bar baz"); - } - - #[test] - fn roundtrip_zero() { - let e = EncoderWriter::new(Vec::new(), Default); - let inner = e.finish().unwrap(); - let mut d = DecoderReader::new(&inner[..]).unwrap(); - let mut s = String::new(); - d.read_to_string(&mut s).unwrap(); - assert_eq!(s, ""); - } - - #[test] - fn roundtrip_big() { - let mut real = Vec::new(); - let mut w = EncoderWriter::new(Vec::new(), Default); - let v = thread_rng().gen_iter::().take(1024).collect::>(); - for _ in 0..200 { - let to_write = &v[..thread_rng().gen_range(0, v.len())]; - real.extend(to_write.iter().map(|x| *x)); - w.write_all(to_write).unwrap(); - } - let result = w.finish().unwrap(); - let mut r = DecoderReader::new(&result[..]).unwrap(); - let mut v = Vec::new(); - r.read_to_end(&mut v).unwrap(); - assert!(v == real); - } - - #[test] - fn roundtrip_big2() { - let v = thread_rng() - .gen_iter::() - .take(1024 * 1024) - .collect::>(); - let mut r = DecoderReader::new(EncoderReader::new(&v[..], Default)) - .unwrap(); - let mut res = Vec::new(); - r.read_to_end(&mut res).unwrap(); - assert!(res == v); - } - - #[test] - fn fields() { - let r = vec![0, 2, 4, 6]; - let e = Builder::new() - .filename("foo.rs") - .comment("bar") - .extra(vec![0, 1, 2, 3]) - .read(&r[..], Default); - let mut d = DecoderReader::new(e).unwrap(); - assert_eq!(d.header().filename(), Some(&b"foo.rs"[..])); - assert_eq!(d.header().comment(), Some(&b"bar"[..])); - assert_eq!(d.header().extra(), Some(&b"\x00\x01\x02\x03"[..])); - let mut res = Vec::new(); - d.read_to_end(&mut res).unwrap(); - assert_eq!(res, vec![0, 2, 4, 6]); - - } - - #[test] - fn keep_reading_after_end() { - let mut e = EncoderWriter::new(Vec::new(), Default); - e.write_all(b"foo bar baz").unwrap(); - let inner = e.finish().unwrap(); - let mut d = DecoderReader::new(&inner[..]).unwrap(); - let mut s = String::new(); - d.read_to_string(&mut s).unwrap(); - assert_eq!(s, "foo bar baz"); - d.read_to_string(&mut s).unwrap(); - assert_eq!(s, "foo bar baz"); - } - - #[test] - fn qc_reader() { - ::quickcheck::quickcheck(test as fn(_) -> _); - - fn test(v: Vec) -> bool { - let r = EncoderReader::new(&v[..], Default); - let mut r = DecoderReader::new(r).unwrap(); - let mut v2 = Vec::new(); - r.read_to_end(&mut v2).unwrap(); - v == v2 - } - } - - #[test] - fn flush_after_write() { - let mut f = EncoderWriter::new(Vec::new(), Default); - write!(f, "Hello world").unwrap(); - f.flush().unwrap(); - } -} diff --git a/src/gz/bufread.rs b/src/gz/bufread.rs new file mode 100644 index 000000000..c630777a1 --- /dev/null +++ b/src/gz/bufread.rs @@ -0,0 +1,535 @@ +use std::cmp; +use std::io::prelude::*; +use std::io; +use std::mem; + +use super::{Builder, Header}; +use super::{FCOMMENT, FEXTRA, FHCRC, FNAME}; +use Compression; +use crc::CrcReader; +use deflate; + +fn copy(into: &mut [u8], from: &[u8], pos: &mut usize) -> usize { + let min = cmp::min(into.len(), from.len() - *pos); + for (slot, val) in into.iter_mut().zip(from[*pos..*pos + min].iter()) { + *slot = *val; + } + *pos += min; + return min; +} +fn corrupt() -> io::Error { + io::Error::new( + io::ErrorKind::InvalidInput, + "corrupt gzip stream does not have a matching checksum", + ) +} + +fn bad_header() -> io::Error { + io::Error::new(io::ErrorKind::InvalidInput, "invalid gzip header") +} + +fn read_le_u16(r: &mut R) -> io::Result { + let mut b = [0; 2]; + try!(r.read_exact(&mut b)); + Ok((b[0] as u16) | ((b[1] as u16) << 8)) +} + +fn read_gz_header(r: &mut R) -> io::Result
{ + let mut crc_reader = CrcReader::new(r); + let mut header = [0; 10]; + try!(crc_reader.read_exact(&mut header)); + + let id1 = header[0]; + let id2 = header[1]; + if id1 != 0x1f || id2 != 0x8b { + return Err(bad_header()); + } + let cm = header[2]; + if cm != 8 { + return Err(bad_header()); + } + + let flg = header[3]; + let mtime = ((header[4] as u32) << 0) | ((header[5] as u32) << 8) | ((header[6] as u32) << 16) | + ((header[7] as u32) << 24); + let _xfl = header[8]; + let _os = header[9]; + + let extra = if flg & FEXTRA != 0 { + let xlen = try!(read_le_u16(&mut crc_reader)); + let mut extra = vec![0; xlen as usize]; + try!(crc_reader.read_exact(&mut extra)); + Some(extra) + } else { + None + }; + let filename = if flg & FNAME != 0 { + // wow this is slow + let mut b = Vec::new(); + for byte in crc_reader.by_ref().bytes() { + let byte = try!(byte); + if byte == 0 { + break; + } + b.push(byte); + } + Some(b) + } else { + None + }; + let comment = if flg & FCOMMENT != 0 { + // wow this is slow + let mut b = Vec::new(); + for byte in crc_reader.by_ref().bytes() { + let byte = try!(byte); + if byte == 0 { + break; + } + b.push(byte); + } + Some(b) + } else { + None + }; + + if flg & FHCRC != 0 { + let calced_crc = crc_reader.crc().sum() as u16; + let stored_crc = try!(read_le_u16(&mut crc_reader)); + if calced_crc != stored_crc { + return Err(corrupt()); + } + } + + Ok(Header { + extra: extra, + filename: filename, + comment: comment, + mtime: mtime, + }) +} + + +/// A gzip streaming encoder +/// +/// This structure exposes a [`BufRead`] interface that will read uncompressed data +/// from the underlying reader and expose the compressed version as a [`BufRead`] +/// interface. +/// +/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use std::io; +/// use flate2::Compression; +/// use flate2::bufread::GzEncoder; +/// use std::fs::File; +/// use std::io::BufReader; +/// +/// // Opens sample file, compresses the contents and returns a Vector or error +/// // File wrapped in a BufReader implements BufRead +/// +/// fn open_hello_world() -> io::Result> { +/// let f = File::open("examples/hello_world.txt")?; +/// let b = BufReader::new(f); +/// let mut gz = GzEncoder::new(b, Compression::Fast); +/// let mut buffer = Vec::new(); +/// gz.read_to_end(&mut buffer)?; +/// Ok(buffer) +/// } +/// ``` +#[derive(Debug)] +pub struct GzEncoder { + pub(crate) inner: deflate::bufread::DeflateEncoder>, + pub(crate) header: Vec, + pub(crate) pos: usize, + pub(crate) eof: bool, +} + +impl GzEncoder { + /// Creates a new encoder which will use the given compression level. + /// + /// The encoder is not configured specially for the emitted header. For + /// header configuration, see the `Builder` type. + /// + /// The data read from the stream `r` will be compressed and available + /// through the returned reader. + pub fn new(r: R, level: Compression) -> GzEncoder { + Builder::new().buf_read(r, level) + } + + fn read_footer(&mut self, into: &mut [u8]) -> io::Result { + if self.pos == 8 { + return Ok(0); + } + let crc = self.inner.get_ref().crc(); + let ref arr = [ + (crc.sum() >> 0) as u8, + (crc.sum() >> 8) as u8, + (crc.sum() >> 16) as u8, + (crc.sum() >> 24) as u8, + (crc.amount() >> 0) as u8, + (crc.amount() >> 8) as u8, + (crc.amount() >> 16) as u8, + (crc.amount() >> 24) as u8, + ]; + Ok(copy(into, arr, &mut self.pos)) + } +} + +impl GzEncoder { + /// Acquires a reference to the underlying reader. + pub fn get_ref(&self) -> &R { + self.inner.get_ref().get_ref() + } + + /// Acquires a mutable reference to the underlying reader. + /// + /// Note that mutation of the reader may result in surprising results if + /// this encoder is continued to be used. + pub fn get_mut(&mut self) -> &mut R { + self.inner.get_mut().get_mut() + } + + /// Returns the underlying stream, consuming this encoder + pub fn into_inner(self) -> R { + self.inner.into_inner().into_inner() + } +} + +impl Read for GzEncoder { + fn read(&mut self, mut into: &mut [u8]) -> io::Result { + let mut amt = 0; + if self.eof { + return self.read_footer(into); + } else if self.pos < self.header.len() { + amt += copy(into, &self.header, &mut self.pos); + if amt == into.len() { + return Ok(amt); + } + let tmp = into; + into = &mut tmp[amt..]; + } + match try!(self.inner.read(into)) { + 0 => { + self.eof = true; + self.pos = 0; + self.read_footer(into) + } + n => Ok(amt + n), + } + } +} + +impl Write for GzEncoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.get_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.get_mut().flush() + } +} + + +/// A gzip streaming decoder +/// +/// This structure exposes a [`ReadBuf`] interface that will consume compressed +/// data from the underlying reader and emit uncompressed data. +/// +/// [`ReadBuf`]: https://doc.rust-lang.org/std/io/trait.BufRead.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use std::io; +/// # use flate2::Compression; +/// # use flate2::write::GzEncoder; +/// use flate2::bufread::GzDecoder; +/// +/// # fn main() { +/// # let mut e = GzEncoder::new(Vec::new(), Compression::Default); +/// # e.write(b"Hello World").unwrap(); +/// # let bytes = e.finish().unwrap(); +/// # println!("{}", decode_reader(bytes).unwrap()); +/// # } +/// # +/// // Uncompresses a Gz Encoded vector of bytes and returns a string or error +/// // Here &[u8] implements BufRead +/// +/// fn decode_reader(bytes: Vec) -> io::Result { +/// let mut gz = GzDecoder::new(&bytes[..])?; +/// let mut s = String::new(); +/// gz.read_to_string(&mut s)?; +/// Ok(s) +/// } +/// ``` +#[derive(Debug)] +pub struct GzDecoder { + inner: CrcReader>, + header: Header, + finished: bool, +} + + +impl GzDecoder { + /// Creates a new decoder from the given reader, immediately parsing the + /// gzip header. + /// + /// # Errors + /// + /// If an error is encountered when parsing the gzip header, an error is + /// returned. + pub fn new(mut r: R) -> io::Result> { + let header = try!(read_gz_header(&mut r)); + + let flate = deflate::bufread::DeflateDecoder::new(r); + return Ok(GzDecoder { + inner: CrcReader::new(flate), + header: header, + finished: false, + }); + } + + fn finish(&mut self) -> io::Result<()> { + if self.finished { + return Ok(()); + } + let ref mut buf = [0u8; 8]; + { + let mut len = 0; + + while len < buf.len() { + match try!(self.inner.get_mut().get_mut().read(&mut buf[len..])) { + 0 => return Err(corrupt()), + n => len += n, + } + } + } + + let crc = ((buf[0] as u32) << 0) | ((buf[1] as u32) << 8) | ((buf[2] as u32) << 16) | + ((buf[3] as u32) << 24); + let amt = ((buf[4] as u32) << 0) | ((buf[5] as u32) << 8) | ((buf[6] as u32) << 16) | + ((buf[7] as u32) << 24); + if crc != self.inner.crc().sum() as u32 { + return Err(corrupt()); + } + if amt != self.inner.crc().amount() { + return Err(corrupt()); + } + self.finished = true; + Ok(()) + } +} + +impl GzDecoder { + /// Returns the header associated with this stream. + pub fn header(&self) -> &Header { + &self.header + } + + /// Acquires a reference to the underlying reader. + pub fn get_ref(&self) -> &R { + self.inner.get_ref().get_ref() + } + + /// Acquires a mutable reference to the underlying stream. + /// + /// Note that mutation of the stream may result in surprising results if + /// this encoder is continued to be used. + pub fn get_mut(&mut self) -> &mut R { + self.inner.get_mut().get_mut() + } + + /// Consumes this decoder, returning the underlying reader. + pub fn into_inner(self) -> R { + self.inner.into_inner().into_inner() + } +} + +impl Read for GzDecoder { + fn read(&mut self, into: &mut [u8]) -> io::Result { + match try!(self.inner.read(into)) { + 0 => { + try!(self.finish()); + Ok(0) + } + n => Ok(n), + } + } +} + +impl Write for GzDecoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.get_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.get_mut().flush() + } +} + + + +/// A gzip streaming decoder that decodes all members of a multistream +/// +/// A gzip member consists of a header, compressed data and a trailer. The [gzip +/// specification](https://tools.ietf.org/html/rfc1952), however, allows multiple +/// gzip members to be joined in a single stream. `MultiGzDecoder` will +/// decode all consecutive members while `GzDecoder` will only decompress +/// the first gzip member. The multistream format is commonly used in +/// bioinformatics, for example when using the BGZF compressed data. +/// +/// This structure exposes a [`BufRead`] interface that will consume all gzip members +/// from the underlying reader and emit uncompressed data. +/// +/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use std::io; +/// # use flate2::Compression; +/// # use flate2::write::GzEncoder; +/// use flate2::bufread::MultiGzDecoder; +/// +/// # fn main() { +/// # let mut e = GzEncoder::new(Vec::new(), Compression::Default); +/// # e.write(b"Hello World").unwrap(); +/// # let bytes = e.finish().unwrap(); +/// # println!("{}", decode_reader(bytes).unwrap()); +/// # } +/// # +/// // Uncompresses a Gz Encoded vector of bytes and returns a string or error +/// // Here &[u8] implements BufRead +/// +/// fn decode_reader(bytes: Vec) -> io::Result { +/// let mut gz = MultiGzDecoder::new(&bytes[..])?; +/// let mut s = String::new(); +/// gz.read_to_string(&mut s)?; +/// Ok(s) +/// } +/// ``` +#[derive(Debug)] +pub struct MultiGzDecoder { + inner: CrcReader>, + header: Header, + finished: bool, +} + + +impl MultiGzDecoder { + /// Creates a new decoder from the given reader, immediately parsing the + /// (first) gzip header. If the gzip stream contains multiple members all will + /// be decoded. + /// + /// # Errors + /// + /// If an error is encountered when parsing the gzip header, an error is + /// returned. + pub fn new(mut r: R) -> io::Result> { + let header = try!(read_gz_header(&mut r)); + + let flate = deflate::bufread::DeflateDecoder::new(r); + return Ok(MultiGzDecoder { + inner: CrcReader::new(flate), + header: header, + finished: false, + }); + } + + fn finish_member(&mut self) -> io::Result { + if self.finished { + return Ok(0); + } + let ref mut buf = [0u8; 8]; + { + let mut len = 0; + + while len < buf.len() { + match try!(self.inner.get_mut().get_mut().read(&mut buf[len..])) { + 0 => return Err(corrupt()), + n => len += n, + } + } + } + + let crc = ((buf[0] as u32) << 0) | ((buf[1] as u32) << 8) | ((buf[2] as u32) << 16) | + ((buf[3] as u32) << 24); + let amt = ((buf[4] as u32) << 0) | ((buf[5] as u32) << 8) | ((buf[6] as u32) << 16) | + ((buf[7] as u32) << 24); + if crc != self.inner.crc().sum() as u32 { + return Err(corrupt()); + } + if amt != self.inner.crc().amount() { + return Err(corrupt()); + } + let remaining = match self.inner.get_mut().get_mut().fill_buf() { + Ok(b) => if b.is_empty() { + self.finished = true; + return Ok(0); + } else { + b.len() + }, + Err(e) => return Err(e), + }; + + let next_header = try!(read_gz_header(self.inner.get_mut().get_mut())); + mem::replace(&mut self.header, next_header); + self.inner.reset(); + self.inner.get_mut().reset_data(); + + Ok(remaining) + } +} + +impl MultiGzDecoder { + /// Returns the current header associated with this stream. + pub fn header(&self) -> &Header { + &self.header + } + + /// Acquires a reference to the underlying reader. + pub fn get_ref(&self) -> &R { + self.inner.get_ref().get_ref() + } + + /// Acquires a mutable reference to the underlying stream. + /// + /// Note that mutation of the stream may result in surprising results if + /// this encoder is continued to be used. + pub fn get_mut(&mut self) -> &mut R { + self.inner.get_mut().get_mut() + } + + /// Consumes this decoder, returning the underlying reader. + pub fn into_inner(self) -> R { + self.inner.into_inner().into_inner() + } +} + +impl Read for MultiGzDecoder { + fn read(&mut self, into: &mut [u8]) -> io::Result { + match try!(self.inner.read(into)) { + 0 => match self.finish_member() { + Ok(0) => Ok(0), + Ok(_) => self.read(into), + Err(e) => Err(e), + }, + n => Ok(n), + } + } +} + +impl Write for MultiGzDecoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.get_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.get_mut().flush() + } +} diff --git a/src/gz/mod.rs b/src/gz/mod.rs new file mode 100644 index 000000000..4c542a722 --- /dev/null +++ b/src/gz/mod.rs @@ -0,0 +1,360 @@ +use std::env; +use std::ffi::CString; +use std::io::prelude::*; +use std::time; + +use {Compress, Compression}; +use bufreader::BufReader; +use crc::{Crc, CrcReader}; +use deflate; +use zio; + +pub static FHCRC: u8 = 1 << 1; +pub static FEXTRA: u8 = 1 << 2; +pub static FNAME: u8 = 1 << 3; +pub static FCOMMENT: u8 = 1 << 4; + +pub mod bufread; +pub mod read; +pub mod write; + + +/// A structure representing the header of a gzip stream. +/// +/// The header can contain metadata about the file that was compressed, if +/// present. +#[derive(PartialEq, Debug)] +pub struct Header { + extra: Option>, + filename: Option>, + comment: Option>, + mtime: u32, +} + +impl Header { + /// Returns the `filename` field of this gzip stream's header, if present. + pub fn filename(&self) -> Option<&[u8]> { + self.filename.as_ref().map(|s| &s[..]) + } + + /// Returns the `extra` field of this gzip stream's header, if present. + pub fn extra(&self) -> Option<&[u8]> { + self.extra.as_ref().map(|s| &s[..]) + } + + /// Returns the `comment` field of this gzip stream's header, if present. + pub fn comment(&self) -> Option<&[u8]> { + self.comment.as_ref().map(|s| &s[..]) + } + + /// This gives the most recent modification time of the original file being compressed. + /// + /// The time is in Unix format, i.e., seconds since 00:00:00 GMT, Jan. 1, 1970. + /// (Note that this may cause problems for MS-DOS and other systems that use local + /// rather than Universal time.) If the compressed data did not come from a file, + /// `mtime` is set to the time at which compression started. + /// `mtime` = 0 means no time stamp is available. + /// + /// The usage of `mtime` is discouraged because of Year 2038 problem. + pub fn mtime(&self) -> u32 { + self.mtime + } + + /// Returns the most recent modification time represented by a date-time type. + /// Returns `None` if the value of the underlying counter is 0, + /// indicating no time stamp is available. + /// + /// + /// The time is measured as seconds since 00:00:00 GMT, Jan. 1 1970. + /// See [`mtime`](#method.mtime) for more detail. + pub fn mtime_as_datetime(&self) -> Option { + if self.mtime == 0 { + None + } else { + let duration = time::Duration::new(u64::from(self.mtime), 0); + let datetime = time::UNIX_EPOCH + duration; + Some(datetime) + } + } +} + +/// A builder structure to create a new gzip Encoder. +/// +/// This structure controls header configuration options such as the filename. +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// # use std::io; +/// use std::fs::File; +/// use flate2::GzBuilder; +/// use flate2::Compression; +/// +/// // GzBuilder opens a file and writes a sample string using Builder pattern +/// +/// # fn sample_builder() -> Result<(), io::Error> { +/// let f = File::create("examples/hello_world.gz")?; +/// let mut gz = GzBuilder::new() +/// .filename("hello_world.txt") +/// .comment("test file, please delete") +/// .write(f, Compression::Default); +/// gz.write(b"hello world")?; +/// gz.finish()?; +/// # Ok(()) +/// # } +/// ``` +#[derive(Debug)] +pub struct Builder { + extra: Option>, + filename: Option, + comment: Option, + mtime: u32, +} + +impl Builder { + /// Create a new blank builder with no header by default. + pub fn new() -> Builder { + Builder { + extra: None, + filename: None, + comment: None, + mtime: 0, + } + } + + /// Configure the `mtime` field in the gzip header. + pub fn mtime(mut self, mtime: u32) -> Builder { + self.mtime = mtime; + self + } + + /// Configure the `extra` field in the gzip header. + pub fn extra>>(mut self, extra: T) -> Builder { + self.extra = Some(extra.into()); + self + } + + /// Configure the `filename` field in the gzip header. + /// + /// # Panics + /// + /// Panics if the `filename` slice contains a zero. + pub fn filename>>(mut self, filename: T) -> Builder { + self.filename = Some(CString::new(filename.into()).unwrap()); + self + } + + /// Configure the `comment` field in the gzip header. + /// + /// # Panics + /// + /// Panics if the `comment` slice contains a zero. + pub fn comment>>(mut self, comment: T) -> Builder { + self.comment = Some(CString::new(comment.into()).unwrap()); + self + } + + /// Consume this builder, creating a writer encoder in the process. + /// + /// The data written to the returned encoder will be compressed and then + /// written out to the supplied parameter `w`. + pub fn write(self, w: W, lvl: Compression) -> write::GzEncoder { + write::GzEncoder { + inner: zio::Writer::new(w, Compress::new(lvl, false)), + crc: Crc::new(), + header: self.into_header(lvl), + crc_bytes_written: 0, + } + } + + /// Consume this builder, creating a reader encoder in the process. + /// + /// Data read from the returned encoder will be the compressed version of + /// the data read from the given reader. + pub fn read(self, r: R, lvl: Compression) -> read::GzEncoder { + read::GzEncoder { + inner: self.buf_read(BufReader::new(r), lvl), + } + } + + /// Consume this builder, creating a reader encoder in the process. + /// + /// Data read from the returned encoder will be the compressed version of + /// the data read from the given reader. + pub fn buf_read(self, r: R, lvl: Compression) -> bufread::GzEncoder + where + R: BufRead, + { + let crc = CrcReader::new(r); + bufread::GzEncoder { + inner: deflate::bufread::DeflateEncoder::new(crc, lvl), + header: self.into_header(lvl), + pos: 0, + eof: false, + } + } + + fn into_header(self, lvl: Compression) -> Vec { + let Builder { + extra, + filename, + comment, + mtime, + } = self; + let mut flg = 0; + let mut header = vec![0u8; 10]; + match extra { + Some(v) => { + flg |= FEXTRA; + header.push((v.len() >> 0) as u8); + header.push((v.len() >> 8) as u8); + header.extend(v); + } + None => {} + } + match filename { + Some(filename) => { + flg |= FNAME; + header.extend(filename.as_bytes_with_nul().iter().map(|x| *x)); + } + None => {} + } + match comment { + Some(comment) => { + flg |= FCOMMENT; + header.extend(comment.as_bytes_with_nul().iter().map(|x| *x)); + } + None => {} + } + header[0] = 0x1f; + header[1] = 0x8b; + header[2] = 8; + header[3] = flg; + header[4] = (mtime >> 0) as u8; + header[5] = (mtime >> 8) as u8; + header[6] = (mtime >> 16) as u8; + header[7] = (mtime >> 24) as u8; + header[8] = match lvl { + Compression::Best => 2, + Compression::Fast => 4, + _ => 0, + }; + header[9] = match env::consts::OS { + "linux" => 3, + "macos" => 7, + "win32" => 0, + _ => 255, + }; + return header; + } +} + +#[cfg(test)] +mod tests { + use std::io::prelude::*; + + use super::{read, write, Builder}; + use Compression::Default; + use rand::{thread_rng, Rng}; + + #[test] + fn roundtrip() { + let mut e = write::GzEncoder::new(Vec::new(), Default); + e.write_all(b"foo bar baz").unwrap(); + let inner = e.finish().unwrap(); + let mut d = read::GzDecoder::new(&inner[..]).unwrap(); + let mut s = String::new(); + d.read_to_string(&mut s).unwrap(); + assert_eq!(s, "foo bar baz"); + } + + #[test] + fn roundtrip_zero() { + let e = write::GzEncoder::new(Vec::new(), Default); + let inner = e.finish().unwrap(); + let mut d = read::GzDecoder::new(&inner[..]).unwrap(); + let mut s = String::new(); + d.read_to_string(&mut s).unwrap(); + assert_eq!(s, ""); + } + + #[test] + fn roundtrip_big() { + let mut real = Vec::new(); + let mut w = write::GzEncoder::new(Vec::new(), Default); + let v = thread_rng().gen_iter::().take(1024).collect::>(); + for _ in 0..200 { + let to_write = &v[..thread_rng().gen_range(0, v.len())]; + real.extend(to_write.iter().map(|x| *x)); + w.write_all(to_write).unwrap(); + } + let result = w.finish().unwrap(); + let mut r = read::GzDecoder::new(&result[..]).unwrap(); + let mut v = Vec::new(); + r.read_to_end(&mut v).unwrap(); + assert!(v == real); + } + + #[test] + fn roundtrip_big2() { + let v = thread_rng() + .gen_iter::() + .take(1024 * 1024) + .collect::>(); + let mut r = read::GzDecoder::new(read::GzEncoder::new(&v[..], Default)).unwrap(); + let mut res = Vec::new(); + r.read_to_end(&mut res).unwrap(); + assert!(res == v); + } + + #[test] + fn fields() { + let r = vec![0, 2, 4, 6]; + let e = Builder::new() + .filename("foo.rs") + .comment("bar") + .extra(vec![0, 1, 2, 3]) + .read(&r[..], Default); + let mut d = read::GzDecoder::new(e).unwrap(); + assert_eq!(d.header().filename(), Some(&b"foo.rs"[..])); + assert_eq!(d.header().comment(), Some(&b"bar"[..])); + assert_eq!(d.header().extra(), Some(&b"\x00\x01\x02\x03"[..])); + let mut res = Vec::new(); + d.read_to_end(&mut res).unwrap(); + assert_eq!(res, vec![0, 2, 4, 6]); + } + + #[test] + fn keep_reading_after_end() { + let mut e = write::GzEncoder::new(Vec::new(), Default); + e.write_all(b"foo bar baz").unwrap(); + let inner = e.finish().unwrap(); + let mut d = read::GzDecoder::new(&inner[..]).unwrap(); + let mut s = String::new(); + d.read_to_string(&mut s).unwrap(); + assert_eq!(s, "foo bar baz"); + d.read_to_string(&mut s).unwrap(); + assert_eq!(s, "foo bar baz"); + } + + #[test] + fn qc_reader() { + ::quickcheck::quickcheck(test as fn(_) -> _); + + fn test(v: Vec) -> bool { + let r = read::GzEncoder::new(&v[..], Default); + let mut r = read::GzDecoder::new(r).unwrap(); + let mut v2 = Vec::new(); + r.read_to_end(&mut v2).unwrap(); + v == v2 + } + } + + #[test] + fn flush_after_write() { + let mut f = write::GzEncoder::new(Vec::new(), Default); + write!(f, "Hello world").unwrap(); + f.flush().unwrap(); + } +} diff --git a/src/gz/read.rs b/src/gz/read.rs new file mode 100644 index 000000000..b4fd7dcfe --- /dev/null +++ b/src/gz/read.rs @@ -0,0 +1,280 @@ +use std::io::prelude::*; +use std::io; + +use super::{Builder, Header}; +use Compression; +use bufreader::BufReader; +use super::bufread; + +/// A gzip streaming encoder +/// +/// This structure exposes a [`Read`] interface that will read uncompressed data +/// from the underlying reader and expose the compressed version as a [`Read`] +/// interface. +/// +/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use std::io; +/// use flate2::Compression; +/// use flate2::read::GzEncoder; +/// +/// // Return a vector containing the GZ compressed version of hello world +/// +/// fn gzencode_hello_world() -> io::Result> { +/// let mut ret_vec = [0;100]; +/// let bytestring = b"hello world"; +/// let mut gz = GzEncoder::new(&bytestring[..], Compression::Fast); +/// let count = gz.read(&mut ret_vec)?; +/// Ok(ret_vec[0..count].to_vec()) +/// } +/// ``` +#[derive(Debug)] +pub struct GzEncoder { + pub(crate) inner: bufread::GzEncoder>, +} + +impl GzEncoder { + /// Creates a new encoder which will use the given compression level. + /// + /// The encoder is not configured specially for the emitted header. For + /// header configuration, see the `Builder` type. + /// + /// The data read from the stream `r` will be compressed and available + /// through the returned reader. + pub fn new(r: R, level: Compression) -> GzEncoder { + Builder::new().read(r, level) + } +} + +impl GzEncoder { + /// Acquires a reference to the underlying reader. + pub fn get_ref(&self) -> &R { + self.inner.get_ref().get_ref() + } + + /// Acquires a mutable reference to the underlying reader. + /// + /// Note that mutation of the reader may result in surprising results if + /// this encoder is continued to be used. + pub fn get_mut(&mut self) -> &mut R { + self.inner.get_mut().get_mut() + } + + /// Returns the underlying stream, consuming this encoder + pub fn into_inner(self) -> R { + self.inner.into_inner().into_inner() + } +} + +impl Read for GzEncoder { + fn read(&mut self, into: &mut [u8]) -> io::Result { + self.inner.read(into) + } +} + +impl Write for GzEncoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.get_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.get_mut().flush() + } +} + +/// A gzip streaming decoder +/// +/// This structure exposes a [`Read`] interface that will consume compressed +/// data from the underlying reader and emit uncompressed data. +/// +/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html +/// +/// # Examples +/// +/// ``` +/// +/// use std::io::prelude::*; +/// use std::io; +/// # use flate2::Compression; +/// # use flate2::write::GzEncoder; +/// use flate2::read::GzDecoder; +/// +/// # fn main() { +/// # let mut e = GzEncoder::new(Vec::new(), Compression::Default); +/// # e.write(b"Hello World").unwrap(); +/// # let bytes = e.finish().unwrap(); +/// # println!("{}", decode_reader(bytes).unwrap()); +/// # } +/// # +/// // Uncompresses a Gz Encoded vector of bytes and returns a string or error +/// // Here &[u8] implements Read +/// +/// fn decode_reader(bytes: Vec) -> io::Result { +/// let mut gz = GzDecoder::new(&bytes[..])?; +/// let mut s = String::new(); +/// gz.read_to_string(&mut s)?; +/// Ok(s) +/// } +/// ``` +#[derive(Debug)] +pub struct GzDecoder { + inner: bufread::GzDecoder>, +} + +impl GzDecoder { + /// Creates a new decoder from the given reader, immediately parsing the + /// gzip header. + /// + /// # Errors + /// + /// If an error is encountered when parsing the gzip header, an error is + /// returned. + pub fn new(r: R) -> io::Result> { + bufread::GzDecoder::new(BufReader::new(r)).map(|r| GzDecoder { inner: r }) + } +} + +impl GzDecoder { + /// Returns the header associated with this stream. + pub fn header(&self) -> &Header { + self.inner.header() + } + + /// Acquires a reference to the underlying reader. + pub fn get_ref(&self) -> &R { + self.inner.get_ref().get_ref() + } + + /// Acquires a mutable reference to the underlying stream. + /// + /// Note that mutation of the stream may result in surprising results if + /// this encoder is continued to be used. + pub fn get_mut(&mut self) -> &mut R { + self.inner.get_mut().get_mut() + } + + /// Consumes this decoder, returning the underlying reader. + pub fn into_inner(self) -> R { + self.inner.into_inner().into_inner() + } +} + +impl Read for GzDecoder { + fn read(&mut self, into: &mut [u8]) -> io::Result { + self.inner.read(into) + } +} + +impl Write for GzDecoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.get_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.get_mut().flush() + } +} + +/// A gzip streaming decoder that decodes all members of a multistream +/// +/// A gzip member consists of a header, compressed data and a trailer. The [gzip +/// specification](https://tools.ietf.org/html/rfc1952), however, allows multiple +/// gzip members to be joined in a single stream. `MultiGzDecoder` will +/// decode all consecutive members while `GzDecoder` will only decompress the +/// first gzip member. The multistream format is commonly used in bioinformatics, +/// for example when using the BGZF compressed data. +/// +/// This structure exposes a [`Read`] interface that will consume all gzip members +/// from the underlying reader and emit uncompressed data. +/// +/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use std::io; +/// # use flate2::Compression; +/// # use flate2::write::GzEncoder; +/// use flate2::read::MultiGzDecoder; +/// +/// # fn main() { +/// # let mut e = GzEncoder::new(Vec::new(), Compression::Default); +/// # e.write(b"Hello World").unwrap(); +/// # let bytes = e.finish().unwrap(); +/// # println!("{}", decode_reader(bytes).unwrap()); +/// # } +/// # +/// // Uncompresses a Gz Encoded vector of bytes and returns a string or error +/// // Here &[u8] implements Read +/// +/// fn decode_reader(bytes: Vec) -> io::Result { +/// let mut gz = MultiGzDecoder::new(&bytes[..])?; +/// let mut s = String::new(); +/// gz.read_to_string(&mut s)?; +/// Ok(s) +/// } +/// ``` +#[derive(Debug)] +pub struct MultiGzDecoder { + inner: bufread::MultiGzDecoder>, +} + +impl MultiGzDecoder { + /// Creates a new decoder from the given reader, immediately parsing the + /// (first) gzip header. If the gzip stream contains multiple members all will + /// be decoded. + /// + /// # Errors + /// + /// If an error is encountered when parsing the gzip header, an error is + /// returned. + pub fn new(r: R) -> io::Result> { + bufread::MultiGzDecoder::new(BufReader::new(r)).map(|r| MultiGzDecoder { inner: r }) + } +} + +impl MultiGzDecoder { + /// Returns the current header associated with this stream. + pub fn header(&self) -> &Header { + self.inner.header() + } + + /// Acquires a reference to the underlying reader. + pub fn get_ref(&self) -> &R { + self.inner.get_ref().get_ref() + } + + /// Acquires a mutable reference to the underlying stream. + /// + /// Note that mutation of the stream may result in surprising results if + /// this encoder is continued to be used. + pub fn get_mut(&mut self) -> &mut R { + self.inner.get_mut().get_mut() + } + + /// Consumes this decoder, returning the underlying reader. + pub fn into_inner(self) -> R { + self.inner.into_inner().into_inner() + } +} + +impl Read for MultiGzDecoder { + fn read(&mut self, into: &mut [u8]) -> io::Result { + self.inner.read(into) + } +} + +impl Write for MultiGzDecoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.get_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.get_mut().flush() + } +} diff --git a/src/gz/write.rs b/src/gz/write.rs new file mode 100644 index 000000000..79a8ec835 --- /dev/null +++ b/src/gz/write.rs @@ -0,0 +1,173 @@ +use std::io::prelude::*; +use std::io; + +#[cfg(feature = "tokio")] +use futures::Poll; +#[cfg(feature = "tokio")] +use tokio_io::{AsyncRead, AsyncWrite}; + +use super::Builder; +use {Compress, Compression}; +use crc::Crc; +use zio; + +/// A gzip streaming encoder +/// +/// This structure exposes a [`Write`] interface that will emit compressed data +/// to the underlying writer `W`. +/// +/// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use flate2::Compression; +/// use flate2::write::GzEncoder; +/// +/// // Vec implements Write to print the compressed bytes of sample string +/// # fn main() { +/// +/// let mut e = GzEncoder::new(Vec::new(), Compression::Default); +/// e.write(b"Hello World").unwrap(); +/// println!("{:?}", e.finish().unwrap()); +/// # } +/// ``` +#[derive(Debug)] +pub struct GzEncoder { + pub(crate) inner: zio::Writer, + pub(crate) crc: Crc, + pub(crate) crc_bytes_written: usize, + pub(crate) header: Vec, +} + +impl GzEncoder { + /// Creates a new encoder which will use the given compression level. + /// + /// The encoder is not configured specially for the emitted header. For + /// header configuration, see the `Builder` type. + /// + /// The data written to the returned encoder will be compressed and then + /// written to the stream `w`. + pub fn new(w: W, level: Compression) -> GzEncoder { + Builder::new().write(w, level) + } + + /// Acquires a reference to the underlying writer. + pub fn get_ref(&self) -> &W { + self.inner.get_ref() + } + + /// Acquires a mutable reference to the underlying writer. + /// + /// Note that mutation of the writer may result in surprising results if + /// this encoder is continued to be used. + pub fn get_mut(&mut self) -> &mut W { + self.inner.get_mut() + } + + /// Attempt to finish this output stream, writing out final chunks of data. + /// + /// Note that this function can only be used once data has finished being + /// written to the output stream. After this function is called then further + /// calls to `write` may result in a panic. + /// + /// # Panics + /// + /// Attempts to write data to this stream may result in a panic after this + /// function is called. + /// + /// # Errors + /// + /// This function will perform I/O to complete this stream, and any I/O + /// errors which occur will be returned from this function. + pub fn try_finish(&mut self) -> io::Result<()> { + try!(self.write_header()); + try!(self.inner.finish()); + + while self.crc_bytes_written < 8 { + let (sum, amt) = (self.crc.sum() as u32, self.crc.amount()); + let buf = [ + (sum >> 0) as u8, + (sum >> 8) as u8, + (sum >> 16) as u8, + (sum >> 24) as u8, + (amt >> 0) as u8, + (amt >> 8) as u8, + (amt >> 16) as u8, + (amt >> 24) as u8, + ]; + let inner = self.inner.get_mut(); + let n = try!(inner.write(&buf[self.crc_bytes_written..])); + self.crc_bytes_written += n; + } + Ok(()) + } + + /// Finish encoding this stream, returning the underlying writer once the + /// encoding is done. + /// + /// Note that this function may not be suitable to call in a situation where + /// the underlying stream is an asynchronous I/O stream. To finish a stream + /// the `try_finish` (or `shutdown`) method should be used instead. To + /// re-acquire ownership of a stream it is safe to call this method after + /// `try_finish` or `shutdown` has returned `Ok`. + /// + /// # Errors + /// + /// This function will perform I/O to complete this stream, and any I/O + /// errors which occur will be returned from this function. + pub fn finish(mut self) -> io::Result { + try!(self.try_finish()); + Ok(self.inner.take_inner()) + } + + fn write_header(&mut self) -> io::Result<()> { + while self.header.len() > 0 { + let n = try!(self.inner.get_mut().write(&self.header)); + self.header.drain(..n); + } + Ok(()) + } +} + +impl Write for GzEncoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + assert_eq!(self.crc_bytes_written, 0); + try!(self.write_header()); + let n = try!(self.inner.write(buf)); + self.crc.update(&buf[..n]); + Ok(n) + } + + fn flush(&mut self) -> io::Result<()> { + assert_eq!(self.crc_bytes_written, 0); + try!(self.write_header()); + self.inner.flush() + } +} + +#[cfg(feature = "tokio")] +impl AsyncWrite for GzEncoder { + fn shutdown(&mut self) -> Poll<(), io::Error> { + try_nb!(self.try_finish()); + self.get_mut().shutdown() + } +} + +impl Read for GzEncoder { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + self.get_mut().read(buf) + } +} + +#[cfg(feature = "tokio")] +impl AsyncRead for GzEncoder {} + +impl Drop for GzEncoder { + fn drop(&mut self) { + if self.inner.is_present() { + let _ = self.try_finish(); + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 6757d2fa6..66aa9c7dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -96,33 +96,32 @@ //! flushed/written when they are dropped, and this is not always a suitable //! time to perform I/O. If I/O streams are flushed before drop, however, then //! these operations will be a noop. - #![doc(html_root_url = "https://docs.rs/flate2/0.2")] #![deny(missing_docs)] #![deny(missing_debug_implementations)] #![allow(trivial_numeric_casts)] #![cfg_attr(test, deny(warnings))] +#[cfg(feature = "tokio")] +extern crate futures; extern crate libc; #[cfg(test)] -extern crate rand; -#[cfg(test)] extern crate quickcheck; +#[cfg(test)] +extern crate rand; #[cfg(feature = "tokio")] #[macro_use] extern crate tokio_io; -#[cfg(feature = "tokio")] -extern crate futures; use std::io::prelude::*; use std::io; pub use gz::Builder as GzBuilder; pub use gz::Header as GzHeader; -pub use mem::{Compress, Decompress, DataError, Status, Flush}; +pub use mem::{Compress, DataError, Decompress, Flush, Status}; pub use crc::{Crc, CrcReader}; -mod bufreader; +pub(crate) mod bufreader; mod crc; mod deflate; mod ffi; @@ -136,13 +135,13 @@ mod zlib; /// /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html pub mod read { - pub use deflate::EncoderReader as DeflateEncoder; - pub use deflate::DecoderReader as DeflateDecoder; - pub use zlib::EncoderReader as ZlibEncoder; - pub use zlib::DecoderReader as ZlibDecoder; - pub use gz::EncoderReader as GzEncoder; - pub use gz::DecoderReader as GzDecoder; - pub use gz::MultiDecoderReader as MultiGzDecoder; + pub use deflate::read::DeflateEncoder; + pub use deflate::read::DeflateDecoder; + pub use zlib::read::ZlibEncoder; + pub use zlib::read::ZlibDecoder; + pub use gz::read::GzEncoder; + pub use gz::read::GzDecoder; + pub use gz::read::MultiGzDecoder; } /// Types which operate over [`Write`] streams, both encoders and decoders for @@ -150,11 +149,11 @@ pub mod read { /// /// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html pub mod write { - pub use deflate::EncoderWriter as DeflateEncoder; - pub use deflate::DecoderWriter as DeflateDecoder; - pub use zlib::EncoderWriter as ZlibEncoder; - pub use zlib::DecoderWriter as ZlibDecoder; - pub use gz::EncoderWriter as GzEncoder; + pub use deflate::write::DeflateEncoder; + pub use deflate::write::DeflateDecoder; + pub use zlib::write::ZlibEncoder; + pub use zlib::write::ZlibDecoder; + pub use gz::write::GzEncoder; } /// Types which operate over [`BufRead`] streams, both encoders and decoders for @@ -162,13 +161,13 @@ pub mod write { /// /// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html pub mod bufread { - pub use deflate::EncoderReaderBuf as DeflateEncoder; - pub use deflate::DecoderReaderBuf as DeflateDecoder; - pub use zlib::EncoderReaderBuf as ZlibEncoder; - pub use zlib::DecoderReaderBuf as ZlibDecoder; - pub use gz::EncoderReaderBuf as GzEncoder; - pub use gz::DecoderReaderBuf as GzDecoder; - pub use gz::MultiDecoderReaderBuf as MultiGzDecoder; + pub use deflate::bufread::DeflateEncoder; + pub use deflate::bufread::DeflateDecoder; + pub use zlib::bufread::ZlibEncoder; + pub use zlib::bufread::ZlibDecoder; + pub use gz::bufread::GzEncoder; + pub use gz::bufread::GzDecoder; + pub use gz::bufread::MultiGzDecoder; } fn _assert_send_sync() { @@ -289,21 +288,21 @@ impl FlateWriteExt for T {} #[cfg(test)] mod test { use std::io::prelude::*; - use {FlateReadExt, Compression}; + use {Compression, FlateReadExt}; #[test] fn crazy() { let rdr = &mut b"foobar"; let mut res = Vec::new(); rdr.gz_encode(Compression::Default) - .deflate_encode(Compression::Default) - .zlib_encode(Compression::Default) - .zlib_decode() - .deflate_decode() - .gz_decode() - .unwrap() - .read_to_end(&mut res) - .unwrap(); + .deflate_encode(Compression::Default) + .zlib_encode(Compression::Default) + .zlib_decode() + .deflate_decode() + .gz_decode() + .unwrap() + .read_to_end(&mut res) + .unwrap(); assert_eq!(res, b"foobar"); } } diff --git a/src/zlib.rs b/src/zlib.rs deleted file mode 100644 index 3a81f2fad..000000000 --- a/src/zlib.rs +++ /dev/null @@ -1,1010 +0,0 @@ -//! ZLIB compression and decompression of streams - -use std::io::prelude::*; -use std::io; -use std::mem; - -#[cfg(feature = "tokio")] -use futures::Poll; -#[cfg(feature = "tokio")] -use tokio_io::{AsyncRead, AsyncWrite}; - -use bufreader::BufReader; -use zio; -use {Compress, Decompress}; - -/// A ZLIB encoder, or compressor. -/// -/// This structure implements a [`Write`] interface and takes a stream of -/// uncompressed data, writing the compressed data to the wrapped writer. -/// -/// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use flate2::Compression; -/// use flate2::write::ZlibEncoder; -/// -/// // Vec implements Write, assigning the compressed bytes of sample string -/// -/// # fn zlib_encoding() -> std::io::Result<()> { -/// let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); -/// e.write(b"Hello World")?; -/// let compressed = e.finish()?; -/// # Ok(()) -/// # } -/// ``` -#[derive(Debug)] -pub struct EncoderWriter { - inner: zio::Writer, -} - -/// A ZLIB encoder, or compressor. -/// -/// This structure implements a [`Read`] interface and will read uncompressed -/// data from an underlying stream and emit a stream of compressed data. -/// -/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use flate2::Compression; -/// use flate2::read::ZlibEncoder; -/// use std::fs::File; -/// -/// // Open example file and compress the contents using Read interface -/// -/// # fn open_hello_world() -> std::io::Result> { -/// let f = File::open("examples/hello_world.txt")?; -/// let mut z = ZlibEncoder::new(f, Compression::Fast); -/// let mut buffer = [0;50]; -/// let byte_count = z.read(&mut buffer)?; -/// # Ok(buffer[0..byte_count].to_vec()) -/// # } -/// ``` -#[derive(Debug)] -pub struct EncoderReader { - inner: EncoderReaderBuf>, -} - -/// A ZLIB encoder, or compressor. -/// -/// This structure implements a [`BufRead`] interface and will read uncompressed -/// data from an underlying stream and emit a stream of compressed data. -/// -/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use flate2::Compression; -/// use flate2::bufread::ZlibEncoder; -/// use std::fs::File; -/// use std::io::BufReader; -/// -/// // Use a buffered file to compress contents into a Vec -/// -/// # fn open_hello_world() -> std::io::Result> { -/// let f = File::open("examples/hello_world.txt")?; -/// let b = BufReader::new(f); -/// let mut z = ZlibEncoder::new(b, Compression::Fast); -/// let mut buffer = Vec::new(); -/// z.read_to_end(&mut buffer)?; -/// # Ok(buffer) -/// # } -/// ``` -#[derive(Debug)] -pub struct EncoderReaderBuf { - obj: R, - data: Compress, -} - -/// A ZLIB decoder, or decompressor. -/// -/// This structure implements a [`Read`] interface and takes a stream of -/// compressed data as input, providing the decompressed data when read from. -/// -/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// # use flate2::Compression; -/// # use flate2::write::ZlibEncoder; -/// use flate2::read::ZlibDecoder; -/// -/// # fn main() { -/// # let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); -/// # e.write(b"Hello World").unwrap(); -/// # let bytes = e.finish().unwrap(); -/// # println!("{}", decode_reader(bytes).unwrap()); -/// # } -/// # -/// // Uncompresses a Zlib Encoded vector of bytes and returns a string or error -/// // Here &[u8] implements Read -/// -/// fn decode_reader(bytes: Vec) -> io::Result { -/// let mut z = ZlibDecoder::new(&bytes[..]); -/// let mut s = String::new(); -/// z.read_to_string(&mut s)?; -/// Ok(s) -/// } -/// ``` -#[derive(Debug)] -pub struct DecoderReader { - inner: DecoderReaderBuf>, -} - -/// A ZLIB decoder, or decompressor. -/// -/// This structure implements a [`BufRead`] interface and takes a stream of -/// compressed data as input, providing the decompressed data when read from. -/// -/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// # use flate2::Compression; -/// # use flate2::write::ZlibEncoder; -/// use flate2::bufread::ZlibDecoder; -/// -/// # fn main() { -/// # let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); -/// # e.write(b"Hello World").unwrap(); -/// # let bytes = e.finish().unwrap(); -/// # println!("{}", decode_bufreader(bytes).unwrap()); -/// # } -/// # -/// // Uncompresses a Zlib Encoded vector of bytes and returns a string or error -/// // Here &[u8] implements BufRead -/// -/// fn decode_bufreader(bytes: Vec) -> io::Result { -/// let mut z = ZlibDecoder::new(&bytes[..]); -/// let mut s = String::new(); -/// z.read_to_string(&mut s)?; -/// Ok(s) -/// } -/// ``` -#[derive(Debug)] -pub struct DecoderReaderBuf { - obj: R, - data: Decompress, -} - -/// A ZLIB decoder, or decompressor. -/// -/// This structure implements a [`Write`] and will emit a stream of decompressed -/// data when fed a stream of compressed data. -/// -/// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html -/// -/// # Examples -/// -/// ``` -/// use std::io::prelude::*; -/// use std::io; -/// # use flate2::Compression; -/// # use flate2::write::ZlibEncoder; -/// use flate2::write::ZlibDecoder; -/// -/// # fn main() { -/// # let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); -/// # e.write(b"Hello World").unwrap(); -/// # let bytes = e.finish().unwrap(); -/// # println!("{}", decode_reader(bytes).unwrap()); -/// # } -/// # -/// // Uncompresses a Zlib Encoded vector of bytes and returns a string or error -/// // Here Vec implements Write -/// -/// fn decode_reader(bytes: Vec) -> io::Result { -/// let mut writer = Vec::new(); -/// let mut z = ZlibDecoder::new(writer); -/// z.write(&bytes[..])?; -/// writer = z.finish()?; -/// let return_string = String::from_utf8(writer).expect("String parsing error"); -/// Ok(return_string) -/// } -/// ``` -#[derive(Debug)] -pub struct DecoderWriter { - inner: zio::Writer, -} - -impl EncoderWriter { - /// Creates a new encoder which will write compressed data to the stream - /// given at the given compression level. - /// - /// When this encoder is dropped or unwrapped the final pieces of data will - /// be flushed. - pub fn new(w: W, level: ::Compression) -> EncoderWriter { - EncoderWriter { - inner: zio::Writer::new(w, Compress::new(level, true)), - } - } - - /// Acquires a reference to the underlying writer. - pub fn get_ref(&self) -> &W { - self.inner.get_ref() - } - - /// Acquires a mutable reference to the underlying writer. - /// - /// Note that mutating the output/input state of the stream may corrupt this - /// object, so care must be taken when using this method. - pub fn get_mut(&mut self) -> &mut W { - self.inner.get_mut() - } - - /// Resets the state of this encoder entirely, swapping out the output - /// stream for another. - /// - /// This function will finish encoding the current stream into the current - /// output stream before swapping out the two output streams. - /// - /// After the current stream has been finished, this will reset the internal - /// state of this encoder and replace the output stream with the one - /// provided, returning the previous output stream. Future data written to - /// this encoder will be the compressed into the stream `w` provided. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn reset(&mut self, w: W) -> io::Result { - try!(self.inner.finish()); - self.inner.data.reset(); - Ok(self.inner.replace(w)) - } - - /// Attempt to finish this output stream, writing out final chunks of data. - /// - /// Note that this function can only be used once data has finished being - /// written to the output stream. After this function is called then further - /// calls to `write` may result in a panic. - /// - /// # Panics - /// - /// Attempts to write data to this stream may result in a panic after this - /// function is called. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn try_finish(&mut self) -> io::Result<()> { - self.inner.finish() - } - - /// Consumes this encoder, flushing the output stream. - /// - /// This will flush the underlying data stream, close off the compressed - /// stream and, if successful, return the contained writer. - /// - /// Note that this function may not be suitable to call in a situation where - /// the underlying stream is an asynchronous I/O stream. To finish a stream - /// the `try_finish` (or `shutdown`) method should be used instead. To - /// re-acquire ownership of a stream it is safe to call this method after - /// `try_finish` or `shutdown` has returned `Ok`. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn finish(mut self) -> io::Result { - try!(self.inner.finish()); - Ok(self.inner.take_inner()) - } - - /// Consumes this encoder, flushing the output stream. - /// - /// This will flush the underlying data stream and then return the contained - /// writer if the flush succeeded. - /// The compressed stream will not closed but only flushed. This - /// means that obtained byte array can by extended by another deflated - /// stream. To close the stream add the two bytes 0x3 and 0x0. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn flush_finish(mut self) -> io::Result { - try!(self.inner.flush()); - Ok(self.inner.take_inner()) - } - - /// Returns the number of bytes that have been written to this compresor. - /// - /// Note that not all bytes written to this object may be accounted for, - /// there may still be some active buffering. - pub fn total_in(&self) -> u64 { - self.inner.data.total_in() - } - - /// Returns the number of bytes that the compressor has produced. - /// - /// Note that not all bytes may have been written yet, some may still be - /// buffered. - pub fn total_out(&self) -> u64 { - self.inner.data.total_out() - } -} - -impl Write for EncoderWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.inner.write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.inner.flush() - } -} - -#[cfg(feature = "tokio")] -impl AsyncWrite for EncoderWriter { - fn shutdown(&mut self) -> Poll<(), io::Error> { - try_nb!(self.try_finish()); - self.get_mut().shutdown() - } -} - -impl Read for EncoderWriter { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - self.get_mut().read(buf) - } -} - -#[cfg(feature = "tokio")] -impl AsyncRead for EncoderWriter { -} - -impl EncoderReader { - /// Creates a new encoder which will read uncompressed data from the given - /// stream and emit the compressed stream. - pub fn new(r: R, level: ::Compression) -> EncoderReader { - EncoderReader { - inner: EncoderReaderBuf::new(BufReader::new(r), level), - } - } -} - -impl EncoderReader { - /// Resets the state of this encoder entirely, swapping out the input - /// stream for another. - /// - /// This function will reset the internal state of this encoder and replace - /// the input stream with the one provided, returning the previous input - /// stream. Future data read from this encoder will be the compressed - /// version of `r`'s data. - /// - /// Note that there may be currently buffered data when this function is - /// called, and in that case the buffered data is discarded. - pub fn reset(&mut self, r: R) -> R { - self.inner.data.reset(); - self.inner.obj.reset(r) - } - - /// Acquires a reference to the underlying stream - pub fn get_ref(&self) -> &R { - self.inner.get_ref().get_ref() - } - - /// Acquires a mutable reference to the underlying stream - /// - /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - self.inner.get_mut().get_mut() - } - - /// Consumes this encoder, returning the underlying reader. - /// - /// Note that there may be buffered bytes which are not re-acquired as part - /// of this transition. It's recommended to only call this function after - /// EOF has been reached. - pub fn into_inner(self) -> R { - self.inner.into_inner().into_inner() - } - - /// Returns the number of bytes that have been read into this compressor. - /// - /// Note that not all bytes read from the underlying object may be accounted - /// for, there may still be some active buffering. - pub fn total_in(&self) -> u64 { - self.inner.data.total_in() - } - - /// Returns the number of bytes that the compressor has produced. - /// - /// Note that not all bytes may have been read yet, some may still be - /// buffered. - pub fn total_out(&self) -> u64 { - self.inner.data.total_out() - } -} - -impl Read for EncoderReader { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - self.inner.read(buf) - } -} - -#[cfg(feature = "tokio")] -impl AsyncRead for EncoderReader { -} - -impl Write for EncoderReader { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -#[cfg(feature = "tokio")] -impl AsyncWrite for EncoderReader { - fn shutdown(&mut self) -> Poll<(), io::Error> { - self.get_mut().shutdown() - } -} - -impl EncoderReaderBuf { - /// Creates a new encoder which will read uncompressed data from the given - /// stream and emit the compressed stream. - pub fn new(r: R, level: ::Compression) -> EncoderReaderBuf { - EncoderReaderBuf { - obj: r, - data: Compress::new(level, true), - } - } -} - -impl EncoderReaderBuf { - /// Resets the state of this encoder entirely, swapping out the input - /// stream for another. - /// - /// This function will reset the internal state of this encoder and replace - /// the input stream with the one provided, returning the previous input - /// stream. Future data read from this encoder will be the compressed - /// version of `r`'s data. - pub fn reset(&mut self, r: R) -> R { - self.data.reset(); - mem::replace(&mut self.obj, r) - } - - /// Acquires a reference to the underlying reader - pub fn get_ref(&self) -> &R { - &self.obj - } - - /// Acquires a mutable reference to the underlying stream - /// - /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - &mut self.obj - } - - /// Consumes this encoder, returning the underlying reader. - pub fn into_inner(self) -> R { - self.obj - } - - /// Returns the number of bytes that have been read into this compressor. - /// - /// Note that not all bytes read from the underlying object may be accounted - /// for, there may still be some active buffering. - pub fn total_in(&self) -> u64 { - self.data.total_in() - } - - /// Returns the number of bytes that the compressor has produced. - /// - /// Note that not all bytes may have been read yet, some may still be - /// buffered. - pub fn total_out(&self) -> u64 { - self.data.total_out() - } -} - -impl Read for EncoderReaderBuf { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - zio::read(&mut self.obj, &mut self.data, buf) - } -} - -#[cfg(feature = "tokio")] -impl AsyncRead for EncoderReaderBuf { -} - -impl Write for EncoderReaderBuf { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -#[cfg(feature = "tokio")] -impl AsyncWrite for EncoderReaderBuf { - fn shutdown(&mut self) -> Poll<(), io::Error> { - self.get_mut().shutdown() - } -} - -impl DecoderReader { - /// Creates a new decoder which will decompress data read from the given - /// stream. - pub fn new(r: R) -> DecoderReader { - DecoderReader::new_with_buf(r, vec![0; 32 * 1024]) - } - - /// Same as `new`, but the intermediate buffer for data is specified. - /// - /// Note that the specified buffer will only be used up to its current - /// length. The buffer's capacity will also not grow over time. - pub fn new_with_buf(r: R, buf: Vec) -> DecoderReader { - DecoderReader { - inner: DecoderReaderBuf::new(BufReader::with_buf(buf, r)), - } - } -} - -impl DecoderReader { - /// Resets the state of this decoder entirely, swapping out the input - /// stream for another. - /// - /// This will reset the internal state of this decoder and replace the - /// input stream with the one provided, returning the previous input - /// stream. Future data read from this decoder will be the decompressed - /// version of `r`'s data. - /// - /// Note that there may be currently buffered data when this function is - /// called, and in that case the buffered data is discarded. - pub fn reset(&mut self, r: R) -> R { - self.inner.data = Decompress::new(true); - self.inner.obj.reset(r) - } - - /// Acquires a reference to the underlying stream - pub fn get_ref(&self) -> &R { - self.inner.get_ref().get_ref() - } - - /// Acquires a mutable reference to the underlying stream - /// - /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - self.inner.get_mut().get_mut() - } - - /// Consumes this decoder, returning the underlying reader. - /// - /// Note that there may be buffered bytes which are not re-acquired as part - /// of this transition. It's recommended to only call this function after - /// EOF has been reached. - pub fn into_inner(self) -> R { - self.inner.into_inner().into_inner() - } - - /// Returns the number of bytes that the decompressor has consumed. - /// - /// Note that this will likely be smaller than what the decompressor - /// actually read from the underlying stream due to buffering. - pub fn total_in(&self) -> u64 { - self.inner.total_in() - } - - /// Returns the number of bytes that the decompressor has produced. - pub fn total_out(&self) -> u64 { - self.inner.total_out() - } -} - -impl Read for DecoderReader { - fn read(&mut self, into: &mut [u8]) -> io::Result { - self.inner.read(into) - } -} - -#[cfg(feature = "tokio")] -impl AsyncRead for DecoderReader { -} - -impl Write for DecoderReader { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -#[cfg(feature = "tokio")] -impl AsyncWrite for DecoderReader { - fn shutdown(&mut self) -> Poll<(), io::Error> { - self.get_mut().shutdown() - } -} - -impl DecoderReaderBuf { - /// Creates a new decoder which will decompress data read from the given - /// stream. - pub fn new(r: R) -> DecoderReaderBuf { - DecoderReaderBuf { - obj: r, - data: Decompress::new(true), - } - } -} - -impl DecoderReaderBuf { - /// Resets the state of this decoder entirely, swapping out the input - /// stream for another. - /// - /// This will reset the internal state of this decoder and replace the - /// input stream with the one provided, returning the previous input - /// stream. Future data read from this decoder will be the decompressed - /// version of `r`'s data. - pub fn reset(&mut self, r: R) -> R { - self.data = Decompress::new(true); - mem::replace(&mut self.obj, r) - } - - /// Acquires a reference to the underlying stream - pub fn get_ref(&self) -> &R { - &self.obj - } - - /// Acquires a mutable reference to the underlying stream - /// - /// Note that mutation of the stream may result in surprising results if - /// this encoder is continued to be used. - pub fn get_mut(&mut self) -> &mut R { - &mut self.obj - } - - /// Consumes this decoder, returning the underlying reader. - pub fn into_inner(self) -> R { - self.obj - } - - /// Returns the number of bytes that the decompressor has consumed. - /// - /// Note that this will likely be smaller than what the decompressor - /// actually read from the underlying stream due to buffering. - pub fn total_in(&self) -> u64 { - self.data.total_in() - } - - /// Returns the number of bytes that the decompressor has produced. - pub fn total_out(&self) -> u64 { - self.data.total_out() - } -} - -impl Read for DecoderReaderBuf { - fn read(&mut self, into: &mut [u8]) -> io::Result { - zio::read(&mut self.obj, &mut self.data, into) - } -} - -#[cfg(feature = "tokio")] -impl AsyncRead for DecoderReaderBuf { -} - -impl Write for DecoderReaderBuf { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.get_mut().write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.get_mut().flush() - } -} - -#[cfg(feature = "tokio")] -impl AsyncWrite for DecoderReaderBuf { - fn shutdown(&mut self) -> Poll<(), io::Error> { - self.get_mut().shutdown() - } -} - -impl DecoderWriter { - /// Creates a new decoder which will write uncompressed data to the stream. - /// - /// When this decoder is dropped or unwrapped the final pieces of data will - /// be flushed. - pub fn new(w: W) -> DecoderWriter { - DecoderWriter { - inner: zio::Writer::new(w, Decompress::new(true)), - } - } - - /// Acquires a reference to the underlying writer. - pub fn get_ref(&self) -> &W { - self.inner.get_ref() - } - - /// Acquires a mutable reference to the underlying writer. - /// - /// Note that mutating the output/input state of the stream may corrupt this - /// object, so care must be taken when using this method. - pub fn get_mut(&mut self) -> &mut W { - self.inner.get_mut() - } - - /// Resets the state of this decoder entirely, swapping out the output - /// stream for another. - /// - /// This will reset the internal state of this decoder and replace the - /// output stream with the one provided, returning the previous output - /// stream. Future data written to this decoder will be decompressed into - /// the output stream `w`. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn reset(&mut self, w: W) -> io::Result { - try!(self.inner.finish()); - self.inner.data = Decompress::new(true); - Ok(self.inner.replace(w)) - } - - /// Attempt to finish this output stream, writing out final chunks of data. - /// - /// Note that this function can only be used once data has finished being - /// written to the output stream. After this function is called then further - /// calls to `write` may result in a panic. - /// - /// # Panics - /// - /// Attempts to write data to this stream may result in a panic after this - /// function is called. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn try_finish(&mut self) -> io::Result<()> { - self.inner.finish() - } - - /// Consumes this encoder, flushing the output stream. - /// - /// This will flush the underlying data stream and then return the contained - /// writer if the flush succeeded. - /// - /// Note that this function may not be suitable to call in a situation where - /// the underlying stream is an asynchronous I/O stream. To finish a stream - /// the `try_finish` (or `shutdown`) method should be used instead. To - /// re-acquire ownership of a stream it is safe to call this method after - /// `try_finish` or `shutdown` has returned `Ok`. - /// - /// # Errors - /// - /// This function will perform I/O to complete this stream, and any I/O - /// errors which occur will be returned from this function. - pub fn finish(mut self) -> io::Result { - try!(self.inner.finish()); - Ok(self.inner.take_inner()) - } - - /// Returns the number of bytes that the decompressor has consumed for - /// decompression. - /// - /// Note that this will likely be smaller than the number of bytes - /// successfully written to this stream due to internal buffering. - pub fn total_in(&self) -> u64 { - self.inner.data.total_in() - } - - /// Returns the number of bytes that the decompressor has written to its - /// output stream. - pub fn total_out(&self) -> u64 { - self.inner.data.total_out() - } -} - -impl Write for DecoderWriter { - fn write(&mut self, buf: &[u8]) -> io::Result { - self.inner.write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.inner.flush() - } -} - -#[cfg(feature = "tokio")] -impl AsyncWrite for DecoderWriter { - fn shutdown(&mut self) -> Poll<(), io::Error> { - try_nb!(self.inner.finish()); - self.inner.get_mut().shutdown() - } -} - -impl Read for DecoderWriter { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - self.inner.get_mut().read(buf) - } -} - -#[cfg(feature = "tokio")] -impl AsyncRead for DecoderWriter { -} - -#[cfg(test)] -mod tests { - use std::io::prelude::*; - use std::io; - - use rand::{thread_rng, Rng}; - - use zlib::{EncoderWriter, EncoderReader, DecoderReader, DecoderWriter}; - use Compression::Default; - - #[test] - fn roundtrip() { - let mut real = Vec::new(); - let mut w = EncoderWriter::new(Vec::new(), Default); - let v = thread_rng().gen_iter::().take(1024).collect::>(); - for _ in 0..200 { - let to_write = &v[..thread_rng().gen_range(0, v.len())]; - real.extend(to_write.iter().map(|x| *x)); - w.write_all(to_write).unwrap(); - } - let result = w.finish().unwrap(); - let mut r = DecoderReader::new(&result[..]); - let mut ret = Vec::new(); - r.read_to_end(&mut ret).unwrap(); - assert!(ret == real); - } - - #[test] - fn drop_writes() { - let mut data = Vec::new(); - EncoderWriter::new(&mut data, Default).write_all(b"foo").unwrap(); - let mut r = DecoderReader::new(&data[..]); - let mut ret = Vec::new(); - r.read_to_end(&mut ret).unwrap(); - assert!(ret == b"foo"); - } - - #[test] - fn total_in() { - let mut real = Vec::new(); - let mut w = EncoderWriter::new(Vec::new(), Default); - let v = thread_rng().gen_iter::().take(1024).collect::>(); - for _ in 0..200 { - let to_write = &v[..thread_rng().gen_range(0, v.len())]; - real.extend(to_write.iter().map(|x| *x)); - w.write_all(to_write).unwrap(); - } - let mut result = w.finish().unwrap(); - - let result_len = result.len(); - - for _ in 0..200 { - result.extend(v.iter().map(|x| *x)); - } - - let mut r = DecoderReader::new(&result[..]); - let mut ret = Vec::new(); - r.read_to_end(&mut ret).unwrap(); - assert!(ret == real); - assert_eq!(r.total_in(), result_len as u64); - } - - #[test] - fn roundtrip2() { - let v = thread_rng() - .gen_iter::() - .take(1024 * 1024) - .collect::>(); - let mut r = DecoderReader::new(EncoderReader::new(&v[..], Default)); - let mut ret = Vec::new(); - r.read_to_end(&mut ret).unwrap(); - assert_eq!(ret, v); - } - - #[test] - fn roundtrip3() { - let v = thread_rng() - .gen_iter::() - .take(1024 * 1024) - .collect::>(); - let mut w = EncoderWriter::new(DecoderWriter::new(Vec::new()), Default); - w.write_all(&v).unwrap(); - let w = w.finish().unwrap().finish().unwrap(); - assert!(w == v); - } - - #[test] - fn reset_decoder() { - let v = thread_rng() - .gen_iter::() - .take(1024 * 1024) - .collect::>(); - let mut w = EncoderWriter::new(Vec::new(), Default); - w.write_all(&v).unwrap(); - let data = w.finish().unwrap(); - - { - let (mut a, mut b, mut c) = (Vec::new(), Vec::new(), Vec::new()); - let mut r = DecoderReader::new(&data[..]); - r.read_to_end(&mut a).unwrap(); - r.reset(&data); - r.read_to_end(&mut b).unwrap(); - - let mut r = DecoderReader::new(&data[..]); - r.read_to_end(&mut c).unwrap(); - assert!(a == b && b == c && c == v); - } - - { - let mut w = DecoderWriter::new(Vec::new()); - w.write_all(&data).unwrap(); - let a = w.reset(Vec::new()).unwrap(); - w.write_all(&data).unwrap(); - let b = w.finish().unwrap(); - - let mut w = DecoderWriter::new(Vec::new()); - w.write_all(&data).unwrap(); - let c = w.finish().unwrap(); - assert!(a == b && b == c && c == v); - } - } - - #[test] - fn bad_input() { - // regress tests: previously caused a panic on drop - let mut out: Vec = Vec::new(); - let data: Vec = (0..255).cycle().take(1024).collect(); - let mut w = DecoderWriter::new(&mut out); - match w.write_all(&data[..]) { - Ok(_) => panic!("Expected an error to be returned!"), - Err(e) => assert_eq!(e.kind(), io::ErrorKind::InvalidInput), - } - } - - #[test] - fn qc_reader() { - ::quickcheck::quickcheck(test as fn(_) -> _); - - fn test(v: Vec) -> bool { - let mut r = DecoderReader::new(EncoderReader::new(&v[..], Default)); - let mut v2 = Vec::new(); - r.read_to_end(&mut v2).unwrap(); - v == v2 - } - } - - #[test] - fn qc_writer() { - ::quickcheck::quickcheck(test as fn(_) -> _); - - fn test(v: Vec) -> bool { - let mut w = EncoderWriter::new(DecoderWriter::new(Vec::new()), Default); - w.write_all(&v).unwrap(); - v == w.finish().unwrap().finish().unwrap() - } - } -} diff --git a/src/zlib/bufread.rs b/src/zlib/bufread.rs new file mode 100644 index 000000000..10f07c67f --- /dev/null +++ b/src/zlib/bufread.rs @@ -0,0 +1,251 @@ +use std::io::prelude::*; +use std::io; +use std::mem; + +#[cfg(feature = "tokio")] +use futures::Poll; +#[cfg(feature = "tokio")] +use tokio_io::{AsyncRead, AsyncWrite}; + +use zio; +use {Compress, Decompress}; + +/// A ZLIB encoder, or compressor. +/// +/// This structure implements a [`BufRead`] interface and will read uncompressed +/// data from an underlying stream and emit a stream of compressed data. +/// +/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use flate2::Compression; +/// use flate2::bufread::ZlibEncoder; +/// use std::fs::File; +/// use std::io::BufReader; +/// +/// // Use a buffered file to compress contents into a Vec +/// +/// # fn open_hello_world() -> std::io::Result> { +/// let f = File::open("examples/hello_world.txt")?; +/// let b = BufReader::new(f); +/// let mut z = ZlibEncoder::new(b, Compression::Fast); +/// let mut buffer = Vec::new(); +/// z.read_to_end(&mut buffer)?; +/// # Ok(buffer) +/// # } +/// ``` +#[derive(Debug)] +pub struct ZlibEncoder { + pub(crate) obj: R, + pub(crate) data: Compress, +} + + +impl ZlibEncoder { + /// Creates a new encoder which will read uncompressed data from the given + /// stream and emit the compressed stream. + pub fn new(r: R, level: ::Compression) -> ZlibEncoder { + ZlibEncoder { + obj: r, + data: Compress::new(level, true), + } + } +} + +impl ZlibEncoder { + /// Resets the state of this encoder entirely, swapping out the input + /// stream for another. + /// + /// This function will reset the internal state of this encoder and replace + /// the input stream with the one provided, returning the previous input + /// stream. Future data read from this encoder will be the compressed + /// version of `r`'s data. + pub fn reset(&mut self, r: R) -> R { + self.data.reset(); + mem::replace(&mut self.obj, r) + } + + /// Acquires a reference to the underlying reader + pub fn get_ref(&self) -> &R { + &self.obj + } + + /// Acquires a mutable reference to the underlying stream + /// + /// Note that mutation of the stream may result in surprising results if + /// this encoder is continued to be used. + pub fn get_mut(&mut self) -> &mut R { + &mut self.obj + } + + /// Consumes this encoder, returning the underlying reader. + pub fn into_inner(self) -> R { + self.obj + } + + /// Returns the number of bytes that have been read into this compressor. + /// + /// Note that not all bytes read from the underlying object may be accounted + /// for, there may still be some active buffering. + pub fn total_in(&self) -> u64 { + self.data.total_in() + } + + /// Returns the number of bytes that the compressor has produced. + /// + /// Note that not all bytes may have been read yet, some may still be + /// buffered. + pub fn total_out(&self) -> u64 { + self.data.total_out() + } +} + +impl Read for ZlibEncoder { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + zio::read(&mut self.obj, &mut self.data, buf) + } +} + +#[cfg(feature = "tokio")] +impl AsyncRead for ZlibEncoder {} + +impl Write for ZlibEncoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.get_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.get_mut().flush() + } +} + +#[cfg(feature = "tokio")] +impl AsyncWrite for ZlibEncoder { + fn shutdown(&mut self) -> Poll<(), io::Error> { + self.get_mut().shutdown() + } +} + +/// A ZLIB decoder, or decompressor. +/// +/// This structure implements a [`BufRead`] interface and takes a stream of +/// compressed data as input, providing the decompressed data when read from. +/// +/// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use std::io; +/// # use flate2::Compression; +/// # use flate2::write::ZlibEncoder; +/// use flate2::bufread::ZlibDecoder; +/// +/// # fn main() { +/// # let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); +/// # e.write(b"Hello World").unwrap(); +/// # let bytes = e.finish().unwrap(); +/// # println!("{}", decode_bufreader(bytes).unwrap()); +/// # } +/// # +/// // Uncompresses a Zlib Encoded vector of bytes and returns a string or error +/// // Here &[u8] implements BufRead +/// +/// fn decode_bufreader(bytes: Vec) -> io::Result { +/// let mut z = ZlibDecoder::new(&bytes[..]); +/// let mut s = String::new(); +/// z.read_to_string(&mut s)?; +/// Ok(s) +/// } +/// ``` +#[derive(Debug)] +pub struct ZlibDecoder { + pub(crate) obj: R, + pub(crate) data: Decompress, +} + +impl ZlibDecoder { + /// Creates a new decoder which will decompress data read from the given + /// stream. + pub fn new(r: R) -> ZlibDecoder { + ZlibDecoder { + obj: r, + data: Decompress::new(true), + } + } +} + +impl ZlibDecoder { + /// Resets the state of this decoder entirely, swapping out the input + /// stream for another. + /// + /// This will reset the internal state of this decoder and replace the + /// input stream with the one provided, returning the previous input + /// stream. Future data read from this decoder will be the decompressed + /// version of `r`'s data. + pub fn reset(&mut self, r: R) -> R { + self.data = Decompress::new(true); + mem::replace(&mut self.obj, r) + } + + /// Acquires a reference to the underlying stream + pub fn get_ref(&self) -> &R { + &self.obj + } + + /// Acquires a mutable reference to the underlying stream + /// + /// Note that mutation of the stream may result in surprising results if + /// this encoder is continued to be used. + pub fn get_mut(&mut self) -> &mut R { + &mut self.obj + } + + /// Consumes this decoder, returning the underlying reader. + pub fn into_inner(self) -> R { + self.obj + } + + /// Returns the number of bytes that the decompressor has consumed. + /// + /// Note that this will likely be smaller than what the decompressor + /// actually read from the underlying stream due to buffering. + pub fn total_in(&self) -> u64 { + self.data.total_in() + } + + /// Returns the number of bytes that the decompressor has produced. + pub fn total_out(&self) -> u64 { + self.data.total_out() + } +} + +impl Read for ZlibDecoder { + fn read(&mut self, into: &mut [u8]) -> io::Result { + zio::read(&mut self.obj, &mut self.data, into) + } +} + +#[cfg(feature = "tokio")] +impl AsyncRead for ZlibDecoder {} + +impl Write for ZlibDecoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.get_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.get_mut().flush() + } +} + +#[cfg(feature = "tokio")] +impl AsyncWrite for ZlibDecoder { + fn shutdown(&mut self) -> Poll<(), io::Error> { + self.get_mut().shutdown() + } +} diff --git a/src/zlib/mod.rs b/src/zlib/mod.rs new file mode 100644 index 000000000..b04e40b82 --- /dev/null +++ b/src/zlib/mod.rs @@ -0,0 +1,164 @@ +pub mod bufread; +pub mod read; +pub mod write; + + +#[cfg(test)] +mod tests { + use std::io::prelude::*; + use std::io; + + use rand::{thread_rng, Rng}; + + use zlib::{read, write}; + use Compression::Default; + + #[test] + fn roundtrip() { + let mut real = Vec::new(); + let mut w = write::ZlibEncoder::new(Vec::new(), Default); + let v = thread_rng().gen_iter::().take(1024).collect::>(); + for _ in 0..200 { + let to_write = &v[..thread_rng().gen_range(0, v.len())]; + real.extend(to_write.iter().map(|x| *x)); + w.write_all(to_write).unwrap(); + } + let result = w.finish().unwrap(); + let mut r = read::ZlibDecoder::new(&result[..]); + let mut ret = Vec::new(); + r.read_to_end(&mut ret).unwrap(); + assert!(ret == real); + } + + #[test] + fn drop_writes() { + let mut data = Vec::new(); + write::ZlibEncoder::new(&mut data, Default) + .write_all(b"foo") + .unwrap(); + let mut r = read::ZlibDecoder::new(&data[..]); + let mut ret = Vec::new(); + r.read_to_end(&mut ret).unwrap(); + assert!(ret == b"foo"); + } + + #[test] + fn total_in() { + let mut real = Vec::new(); + let mut w = write::ZlibEncoder::new(Vec::new(), Default); + let v = thread_rng().gen_iter::().take(1024).collect::>(); + for _ in 0..200 { + let to_write = &v[..thread_rng().gen_range(0, v.len())]; + real.extend(to_write.iter().map(|x| *x)); + w.write_all(to_write).unwrap(); + } + let mut result = w.finish().unwrap(); + + let result_len = result.len(); + + for _ in 0..200 { + result.extend(v.iter().map(|x| *x)); + } + + let mut r = read::ZlibDecoder::new(&result[..]); + let mut ret = Vec::new(); + r.read_to_end(&mut ret).unwrap(); + assert!(ret == real); + assert_eq!(r.total_in(), result_len as u64); + } + + #[test] + fn roundtrip2() { + let v = thread_rng() + .gen_iter::() + .take(1024 * 1024) + .collect::>(); + let mut r = read::ZlibDecoder::new(read::ZlibEncoder::new(&v[..], Default)); + let mut ret = Vec::new(); + r.read_to_end(&mut ret).unwrap(); + assert_eq!(ret, v); + } + + #[test] + fn roundtrip3() { + let v = thread_rng() + .gen_iter::() + .take(1024 * 1024) + .collect::>(); + let mut w = write::ZlibEncoder::new(write::ZlibDecoder::new(Vec::new()), Default); + w.write_all(&v).unwrap(); + let w = w.finish().unwrap().finish().unwrap(); + assert!(w == v); + } + + #[test] + fn reset_decoder() { + let v = thread_rng() + .gen_iter::() + .take(1024 * 1024) + .collect::>(); + let mut w = write::ZlibEncoder::new(Vec::new(), Default); + w.write_all(&v).unwrap(); + let data = w.finish().unwrap(); + + { + let (mut a, mut b, mut c) = (Vec::new(), Vec::new(), Vec::new()); + let mut r = read::ZlibDecoder::new(&data[..]); + r.read_to_end(&mut a).unwrap(); + r.reset(&data); + r.read_to_end(&mut b).unwrap(); + + let mut r = read::ZlibDecoder::new(&data[..]); + r.read_to_end(&mut c).unwrap(); + assert!(a == b && b == c && c == v); + } + + { + let mut w = write::ZlibDecoder::new(Vec::new()); + w.write_all(&data).unwrap(); + let a = w.reset(Vec::new()).unwrap(); + w.write_all(&data).unwrap(); + let b = w.finish().unwrap(); + + let mut w = write::ZlibDecoder::new(Vec::new()); + w.write_all(&data).unwrap(); + let c = w.finish().unwrap(); + assert!(a == b && b == c && c == v); + } + } + + #[test] + fn bad_input() { + // regress tests: previously caused a panic on drop + let mut out: Vec = Vec::new(); + let data: Vec = (0..255).cycle().take(1024).collect(); + let mut w = write::ZlibDecoder::new(&mut out); + match w.write_all(&data[..]) { + Ok(_) => panic!("Expected an error to be returned!"), + Err(e) => assert_eq!(e.kind(), io::ErrorKind::InvalidInput), + } + } + + #[test] + fn qc_reader() { + ::quickcheck::quickcheck(test as fn(_) -> _); + + fn test(v: Vec) -> bool { + let mut r = read::ZlibDecoder::new(read::ZlibEncoder::new(&v[..], Default)); + let mut v2 = Vec::new(); + r.read_to_end(&mut v2).unwrap(); + v == v2 + } + } + + #[test] + fn qc_writer() { + ::quickcheck::quickcheck(test as fn(_) -> _); + + fn test(v: Vec) -> bool { + let mut w = write::ZlibEncoder::new(write::ZlibDecoder::new(Vec::new()), Default); + w.write_all(&v).unwrap(); + v == w.finish().unwrap().finish().unwrap() + } + } +} diff --git a/src/zlib/read.rs b/src/zlib/read.rs new file mode 100644 index 000000000..3d3fa19fa --- /dev/null +++ b/src/zlib/read.rs @@ -0,0 +1,267 @@ +use std::io::prelude::*; +use std::io; + +#[cfg(feature = "tokio")] +use futures::Poll; +#[cfg(feature = "tokio")] +use tokio_io::{AsyncRead, AsyncWrite}; + +use bufreader::BufReader; +use super::bufread; +use Decompress; + +/// A ZLIB encoder, or compressor. +/// +/// This structure implements a [`Read`] interface and will read uncompressed +/// data from an underlying stream and emit a stream of compressed data. +/// +/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use flate2::Compression; +/// use flate2::read::ZlibEncoder; +/// use std::fs::File; +/// +/// // Open example file and compress the contents using Read interface +/// +/// # fn open_hello_world() -> std::io::Result> { +/// let f = File::open("examples/hello_world.txt")?; +/// let mut z = ZlibEncoder::new(f, Compression::Fast); +/// let mut buffer = [0;50]; +/// let byte_count = z.read(&mut buffer)?; +/// # Ok(buffer[0..byte_count].to_vec()) +/// # } +/// ``` +#[derive(Debug)] +pub struct ZlibEncoder { + inner: bufread::ZlibEncoder>, +} + +impl ZlibEncoder { + /// Creates a new encoder which will read uncompressed data from the given + /// stream and emit the compressed stream. + pub fn new(r: R, level: ::Compression) -> ZlibEncoder { + ZlibEncoder { + inner: bufread::ZlibEncoder::new(BufReader::new(r), level), + } + } +} + +impl ZlibEncoder { + /// Resets the state of this encoder entirely, swapping out the input + /// stream for another. + /// + /// This function will reset the internal state of this encoder and replace + /// the input stream with the one provided, returning the previous input + /// stream. Future data read from this encoder will be the compressed + /// version of `r`'s data. + /// + /// Note that there may be currently buffered data when this function is + /// called, and in that case the buffered data is discarded. + pub fn reset(&mut self, r: R) -> R { + self.inner.data.reset(); + self.inner.obj.reset(r) + } + + /// Acquires a reference to the underlying stream + pub fn get_ref(&self) -> &R { + self.inner.get_ref().get_ref() + } + + /// Acquires a mutable reference to the underlying stream + /// + /// Note that mutation of the stream may result in surprising results if + /// this encoder is continued to be used. + pub fn get_mut(&mut self) -> &mut R { + self.inner.get_mut().get_mut() + } + + /// Consumes this encoder, returning the underlying reader. + /// + /// Note that there may be buffered bytes which are not re-acquired as part + /// of this transition. It's recommended to only call this function after + /// EOF has been reached. + pub fn into_inner(self) -> R { + self.inner.into_inner().into_inner() + } + + /// Returns the number of bytes that have been read into this compressor. + /// + /// Note that not all bytes read from the underlying object may be accounted + /// for, there may still be some active buffering. + pub fn total_in(&self) -> u64 { + self.inner.data.total_in() + } + + /// Returns the number of bytes that the compressor has produced. + /// + /// Note that not all bytes may have been read yet, some may still be + /// buffered. + pub fn total_out(&self) -> u64 { + self.inner.data.total_out() + } +} + +impl Read for ZlibEncoder { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + self.inner.read(buf) + } +} + +#[cfg(feature = "tokio")] +impl AsyncRead for ZlibEncoder {} + +impl Write for ZlibEncoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.get_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.get_mut().flush() + } +} + +#[cfg(feature = "tokio")] +impl AsyncWrite for ZlibEncoder { + fn shutdown(&mut self) -> Poll<(), io::Error> { + self.get_mut().shutdown() + } +} + +/// A ZLIB decoder, or decompressor. +/// +/// This structure implements a [`Read`] interface and takes a stream of +/// compressed data as input, providing the decompressed data when read from. +/// +/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use std::io; +/// # use flate2::Compression; +/// # use flate2::write::ZlibEncoder; +/// use flate2::read::ZlibDecoder; +/// +/// # fn main() { +/// # let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); +/// # e.write(b"Hello World").unwrap(); +/// # let bytes = e.finish().unwrap(); +/// # println!("{}", decode_reader(bytes).unwrap()); +/// # } +/// # +/// // Uncompresses a Zlib Encoded vector of bytes and returns a string or error +/// // Here &[u8] implements Read +/// +/// fn decode_reader(bytes: Vec) -> io::Result { +/// let mut z = ZlibDecoder::new(&bytes[..]); +/// let mut s = String::new(); +/// z.read_to_string(&mut s)?; +/// Ok(s) +/// } +/// ``` +#[derive(Debug)] +pub struct ZlibDecoder { + inner: bufread::ZlibDecoder>, +} + + +impl ZlibDecoder { + /// Creates a new decoder which will decompress data read from the given + /// stream. + pub fn new(r: R) -> ZlibDecoder { + ZlibDecoder::new_with_buf(r, vec![0; 32 * 1024]) + } + + /// Same as `new`, but the intermediate buffer for data is specified. + /// + /// Note that the specified buffer will only be used up to its current + /// length. The buffer's capacity will also not grow over time. + pub fn new_with_buf(r: R, buf: Vec) -> ZlibDecoder { + ZlibDecoder { + inner: bufread::ZlibDecoder::new(BufReader::with_buf(buf, r)), + } + } +} + +impl ZlibDecoder { + /// Resets the state of this decoder entirely, swapping out the input + /// stream for another. + /// + /// This will reset the internal state of this decoder and replace the + /// input stream with the one provided, returning the previous input + /// stream. Future data read from this decoder will be the decompressed + /// version of `r`'s data. + /// + /// Note that there may be currently buffered data when this function is + /// called, and in that case the buffered data is discarded. + pub fn reset(&mut self, r: R) -> R { + self.inner.data = Decompress::new(true); + self.inner.obj.reset(r) + } + + /// Acquires a reference to the underlying stream + pub fn get_ref(&self) -> &R { + self.inner.get_ref().get_ref() + } + + /// Acquires a mutable reference to the underlying stream + /// + /// Note that mutation of the stream may result in surprising results if + /// this encoder is continued to be used. + pub fn get_mut(&mut self) -> &mut R { + self.inner.get_mut().get_mut() + } + + /// Consumes this decoder, returning the underlying reader. + /// + /// Note that there may be buffered bytes which are not re-acquired as part + /// of this transition. It's recommended to only call this function after + /// EOF has been reached. + pub fn into_inner(self) -> R { + self.inner.into_inner().into_inner() + } + + /// Returns the number of bytes that the decompressor has consumed. + /// + /// Note that this will likely be smaller than what the decompressor + /// actually read from the underlying stream due to buffering. + pub fn total_in(&self) -> u64 { + self.inner.total_in() + } + + /// Returns the number of bytes that the decompressor has produced. + pub fn total_out(&self) -> u64 { + self.inner.total_out() + } +} + +impl Read for ZlibDecoder { + fn read(&mut self, into: &mut [u8]) -> io::Result { + self.inner.read(into) + } +} + +#[cfg(feature = "tokio")] +impl AsyncRead for ZlibDecoder {} + +impl Write for ZlibDecoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.get_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.get_mut().flush() + } +} + +#[cfg(feature = "tokio")] +impl AsyncWrite for ZlibDecoder { + fn shutdown(&mut self) -> Poll<(), io::Error> { + self.get_mut().shutdown() + } +} diff --git a/src/zlib/write.rs b/src/zlib/write.rs new file mode 100644 index 000000000..52e4fc28e --- /dev/null +++ b/src/zlib/write.rs @@ -0,0 +1,351 @@ +use std::io::prelude::*; +use std::io; + +#[cfg(feature = "tokio")] +use futures::Poll; +#[cfg(feature = "tokio")] +use tokio_io::{AsyncRead, AsyncWrite}; + +use zio; +use {Compress, Decompress}; + +/// A ZLIB encoder, or compressor. +/// +/// This structure implements a [`Write`] interface and takes a stream of +/// uncompressed data, writing the compressed data to the wrapped writer. +/// +/// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use flate2::Compression; +/// use flate2::write::ZlibEncoder; +/// +/// // Vec implements Write, assigning the compressed bytes of sample string +/// +/// # fn zlib_encoding() -> std::io::Result<()> { +/// let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); +/// e.write(b"Hello World")?; +/// let compressed = e.finish()?; +/// # Ok(()) +/// # } +/// ``` +#[derive(Debug)] +pub struct ZlibEncoder { + inner: zio::Writer, +} + + +impl ZlibEncoder { + /// Creates a new encoder which will write compressed data to the stream + /// given at the given compression level. + /// + /// When this encoder is dropped or unwrapped the final pieces of data will + /// be flushed. + pub fn new(w: W, level: ::Compression) -> ZlibEncoder { + ZlibEncoder { + inner: zio::Writer::new(w, Compress::new(level, true)), + } + } + + /// Acquires a reference to the underlying writer. + pub fn get_ref(&self) -> &W { + self.inner.get_ref() + } + + /// Acquires a mutable reference to the underlying writer. + /// + /// Note that mutating the output/input state of the stream may corrupt this + /// object, so care must be taken when using this method. + pub fn get_mut(&mut self) -> &mut W { + self.inner.get_mut() + } + + /// Resets the state of this encoder entirely, swapping out the output + /// stream for another. + /// + /// This function will finish encoding the current stream into the current + /// output stream before swapping out the two output streams. + /// + /// After the current stream has been finished, this will reset the internal + /// state of this encoder and replace the output stream with the one + /// provided, returning the previous output stream. Future data written to + /// this encoder will be the compressed into the stream `w` provided. + /// + /// # Errors + /// + /// This function will perform I/O to complete this stream, and any I/O + /// errors which occur will be returned from this function. + pub fn reset(&mut self, w: W) -> io::Result { + try!(self.inner.finish()); + self.inner.data.reset(); + Ok(self.inner.replace(w)) + } + + /// Attempt to finish this output stream, writing out final chunks of data. + /// + /// Note that this function can only be used once data has finished being + /// written to the output stream. After this function is called then further + /// calls to `write` may result in a panic. + /// + /// # Panics + /// + /// Attempts to write data to this stream may result in a panic after this + /// function is called. + /// + /// # Errors + /// + /// This function will perform I/O to complete this stream, and any I/O + /// errors which occur will be returned from this function. + pub fn try_finish(&mut self) -> io::Result<()> { + self.inner.finish() + } + + /// Consumes this encoder, flushing the output stream. + /// + /// This will flush the underlying data stream, close off the compressed + /// stream and, if successful, return the contained writer. + /// + /// Note that this function may not be suitable to call in a situation where + /// the underlying stream is an asynchronous I/O stream. To finish a stream + /// the `try_finish` (or `shutdown`) method should be used instead. To + /// re-acquire ownership of a stream it is safe to call this method after + /// `try_finish` or `shutdown` has returned `Ok`. + /// + /// # Errors + /// + /// This function will perform I/O to complete this stream, and any I/O + /// errors which occur will be returned from this function. + pub fn finish(mut self) -> io::Result { + try!(self.inner.finish()); + Ok(self.inner.take_inner()) + } + + /// Consumes this encoder, flushing the output stream. + /// + /// This will flush the underlying data stream and then return the contained + /// writer if the flush succeeded. + /// The compressed stream will not closed but only flushed. This + /// means that obtained byte array can by extended by another deflated + /// stream. To close the stream add the two bytes 0x3 and 0x0. + /// + /// # Errors + /// + /// This function will perform I/O to complete this stream, and any I/O + /// errors which occur will be returned from this function. + pub fn flush_finish(mut self) -> io::Result { + try!(self.inner.flush()); + Ok(self.inner.take_inner()) + } + + /// Returns the number of bytes that have been written to this compresor. + /// + /// Note that not all bytes written to this object may be accounted for, + /// there may still be some active buffering. + pub fn total_in(&self) -> u64 { + self.inner.data.total_in() + } + + /// Returns the number of bytes that the compressor has produced. + /// + /// Note that not all bytes may have been written yet, some may still be + /// buffered. + pub fn total_out(&self) -> u64 { + self.inner.data.total_out() + } +} + +impl Write for ZlibEncoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.inner.write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } +} + +#[cfg(feature = "tokio")] +impl AsyncWrite for ZlibEncoder { + fn shutdown(&mut self) -> Poll<(), io::Error> { + try_nb!(self.try_finish()); + self.get_mut().shutdown() + } +} + +impl Read for ZlibEncoder { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + self.get_mut().read(buf) + } +} + +#[cfg(feature = "tokio")] +impl AsyncRead for ZlibEncoder {} + + +/// A ZLIB decoder, or decompressor. +/// +/// This structure implements a [`Write`] and will emit a stream of decompressed +/// data when fed a stream of compressed data. +/// +/// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html +/// +/// # Examples +/// +/// ``` +/// use std::io::prelude::*; +/// use std::io; +/// # use flate2::Compression; +/// # use flate2::write::ZlibEncoder; +/// use flate2::write::ZlibDecoder; +/// +/// # fn main() { +/// # let mut e = ZlibEncoder::new(Vec::new(), Compression::Default); +/// # e.write(b"Hello World").unwrap(); +/// # let bytes = e.finish().unwrap(); +/// # println!("{}", decode_reader(bytes).unwrap()); +/// # } +/// # +/// // Uncompresses a Zlib Encoded vector of bytes and returns a string or error +/// // Here Vec implements Write +/// +/// fn decode_reader(bytes: Vec) -> io::Result { +/// let mut writer = Vec::new(); +/// let mut z = ZlibDecoder::new(writer); +/// z.write(&bytes[..])?; +/// writer = z.finish()?; +/// let return_string = String::from_utf8(writer).expect("String parsing error"); +/// Ok(return_string) +/// } +/// ``` +#[derive(Debug)] +pub struct ZlibDecoder { + inner: zio::Writer, +} + + +impl ZlibDecoder { + /// Creates a new decoder which will write uncompressed data to the stream. + /// + /// When this decoder is dropped or unwrapped the final pieces of data will + /// be flushed. + pub fn new(w: W) -> ZlibDecoder { + ZlibDecoder { + inner: zio::Writer::new(w, Decompress::new(true)), + } + } + + /// Acquires a reference to the underlying writer. + pub fn get_ref(&self) -> &W { + self.inner.get_ref() + } + + /// Acquires a mutable reference to the underlying writer. + /// + /// Note that mutating the output/input state of the stream may corrupt this + /// object, so care must be taken when using this method. + pub fn get_mut(&mut self) -> &mut W { + self.inner.get_mut() + } + + /// Resets the state of this decoder entirely, swapping out the output + /// stream for another. + /// + /// This will reset the internal state of this decoder and replace the + /// output stream with the one provided, returning the previous output + /// stream. Future data written to this decoder will be decompressed into + /// the output stream `w`. + /// + /// # Errors + /// + /// This function will perform I/O to complete this stream, and any I/O + /// errors which occur will be returned from this function. + pub fn reset(&mut self, w: W) -> io::Result { + try!(self.inner.finish()); + self.inner.data = Decompress::new(true); + Ok(self.inner.replace(w)) + } + + /// Attempt to finish this output stream, writing out final chunks of data. + /// + /// Note that this function can only be used once data has finished being + /// written to the output stream. After this function is called then further + /// calls to `write` may result in a panic. + /// + /// # Panics + /// + /// Attempts to write data to this stream may result in a panic after this + /// function is called. + /// + /// # Errors + /// + /// This function will perform I/O to complete this stream, and any I/O + /// errors which occur will be returned from this function. + pub fn try_finish(&mut self) -> io::Result<()> { + self.inner.finish() + } + + /// Consumes this encoder, flushing the output stream. + /// + /// This will flush the underlying data stream and then return the contained + /// writer if the flush succeeded. + /// + /// Note that this function may not be suitable to call in a situation where + /// the underlying stream is an asynchronous I/O stream. To finish a stream + /// the `try_finish` (or `shutdown`) method should be used instead. To + /// re-acquire ownership of a stream it is safe to call this method after + /// `try_finish` or `shutdown` has returned `Ok`. + /// + /// # Errors + /// + /// This function will perform I/O to complete this stream, and any I/O + /// errors which occur will be returned from this function. + pub fn finish(mut self) -> io::Result { + try!(self.inner.finish()); + Ok(self.inner.take_inner()) + } + + /// Returns the number of bytes that the decompressor has consumed for + /// decompression. + /// + /// Note that this will likely be smaller than the number of bytes + /// successfully written to this stream due to internal buffering. + pub fn total_in(&self) -> u64 { + self.inner.data.total_in() + } + + /// Returns the number of bytes that the decompressor has written to its + /// output stream. + pub fn total_out(&self) -> u64 { + self.inner.data.total_out() + } +} + +impl Write for ZlibDecoder { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.inner.write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } +} + +#[cfg(feature = "tokio")] +impl AsyncWrite for ZlibDecoder { + fn shutdown(&mut self) -> Poll<(), io::Error> { + try_nb!(self.inner.finish()); + self.inner.get_mut().shutdown() + } +} + +impl Read for ZlibDecoder { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + self.inner.get_mut().read(buf) + } +} + +#[cfg(feature = "tokio")] +impl AsyncRead for ZlibDecoder {}