1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//! Brotli Compression/Decompression for Rust
//!
//! This crate is a binding to the [official brotli implementation][brotli] and
//! provides in-memory and I/O streams for Rust wrappers.
//!
//! [brotli]: https://github.com/google/brotli
//!
//! # Examples
//!
//! ```
//! use std::io::prelude::*;
//! use brotli2::read::{BrotliEncoder, BrotliDecoder};
//!
//! // Round trip some bytes from a byte source, into a compressor, into a
//! // decompressor, and finally into a vector.
//! let data = "Hello, World!".as_bytes();
//! let compressor = BrotliEncoder::new(data, 9);
//! let mut decompressor = BrotliDecoder::new(compressor);
//!
//! let mut contents = String::new();
//! decompressor.read_to_string(&mut contents).unwrap();
//! assert_eq!(contents, "Hello, World!");
//! ```

#![deny(missing_docs)]
#![doc(html_root_url = "https://docs.rs/brotli2/0.2")]

extern crate brotli_sys;
extern crate libc;

#[cfg(test)]
extern crate rand;
#[cfg(test)]
extern crate quickcheck;

pub mod raw;
pub mod bufread;
pub mod read;
pub mod write;

/// Possible choices for modes of compression
#[repr(isize)]
#[derive(Copy,Clone,Debug,PartialEq,Eq)]
pub enum CompressMode {
    /// Default compression mode, the compressor does not know anything in
    /// advance about the properties of the input.
    Generic = brotli_sys::BROTLI_MODE_GENERIC as isize,
    /// Compression mode for utf-8 formatted text input.
    Text = brotli_sys::BROTLI_MODE_TEXT as isize,
    /// Compression mode in WOFF 2.0.
    Font = brotli_sys::BROTLI_MODE_FONT as isize,
}

/// Parameters passed to various compression routines.
#[derive(Clone,Debug)]
pub struct CompressParams {
    /// Compression mode.
    mode: u32,
    /// Controls the compression-speed vs compression-density tradeoffs. The higher the `quality`,
    /// the slower the compression. Range is 0 to 11.
    quality: u32,
    /// Base 2 logarithm of the sliding window size. Range is 10 to 24.
    lgwin: u32,
    /// Base 2 logarithm of the maximum input block size. Range is 16 to 24. If set to 0, the value
    /// will be set based on the quality.
    lgblock: u32,
}

impl CompressParams {
    /// Creates a new default set of compression parameters.
    pub fn new() -> CompressParams {
        CompressParams {
            mode: brotli_sys::BROTLI_DEFAULT_MODE,
            quality: brotli_sys::BROTLI_DEFAULT_QUALITY,
            lgwin: brotli_sys::BROTLI_DEFAULT_WINDOW,
            lgblock: 0,
        }
    }

    /// Set the mode of this compression.
    pub fn mode(&mut self, mode: CompressMode) -> &mut CompressParams {
        self.mode = mode as u32;
        self
    }

    /// Controls the compression-speed vs compression-density tradeoffs.
    ///
    /// The higher the quality, the slower the compression. Currently the range
    /// for the quality is 0 to 11.
    pub fn quality(&mut self, quality: u32) -> &mut CompressParams {
        self.quality = quality;
        self
    }

    /// Sets the base 2 logarithm of the sliding window size.
    ///
    /// Currently the range is 10 to 24.
    pub fn lgwin(&mut self, lgwin: u32) -> &mut CompressParams {
        self.lgwin = lgwin;
        self
    }

    /// Sets the base 2 logarithm of the maximum input block size.
    ///
    /// Currently the range is 16 to 24, and if set to 0 the value will be set
    /// based on the quality.
    pub fn lgblock(&mut self, lgblock: u32) -> &mut CompressParams {
        self.lgblock = lgblock;
        self
    }

    /// Get the current block size
    #[inline]
    pub fn get_lgblock_readable(&self) -> usize {
        1usize << self.lgblock
    }

    /// Get the native lgblock size
    #[inline]
    pub fn get_lgblock(&self) -> u32 {
        self.lgblock.clone()
    }
    /// Get the current window size
    #[inline]
    pub fn get_lgwin_readable(&self) -> usize {
        1usize << self.lgwin
    }
    /// Get the native lgwin value
    #[inline]
    pub fn get_lgwin(&self) -> u32 {
        self.lgwin.clone()
    }
}