brotli2/lib.rs
1//! Brotli Compression/Decompression for Rust
2//!
3//! This crate is a binding to the [official brotli implementation][brotli] and
4//! provides in-memory and I/O streams for Rust wrappers.
5//!
6//! [brotli]: https://github.com/google/brotli
7//!
8//! # Examples
9//!
10//! ```
11//! use std::io::prelude::*;
12//! use brotli2::read::{BrotliEncoder, BrotliDecoder};
13//!
14//! // Round trip some bytes from a byte source, into a compressor, into a
15//! // decompressor, and finally into a vector.
16//! let data = "Hello, World!".as_bytes();
17//! let compressor = BrotliEncoder::new(data, 9);
18//! let mut decompressor = BrotliDecoder::new(compressor);
19//!
20//! let mut contents = String::new();
21//! decompressor.read_to_string(&mut contents).unwrap();
22//! assert_eq!(contents, "Hello, World!");
23//! ```
24
25#![deny(missing_docs)]
26#![doc(html_root_url = "https://docs.rs/brotli2/0.2")]
27
28extern crate brotli_sys;
29extern crate libc;
30
31#[cfg(test)]
32extern crate rand;
33#[cfg(test)]
34extern crate quickcheck;
35
36pub mod raw;
37pub mod bufread;
38pub mod read;
39pub mod write;
40
41/// Possible choices for modes of compression
42#[repr(isize)]
43#[derive(Copy,Clone,Debug,PartialEq,Eq)]
44pub enum CompressMode {
45 /// Default compression mode, the compressor does not know anything in
46 /// advance about the properties of the input.
47 Generic = brotli_sys::BROTLI_MODE_GENERIC as isize,
48 /// Compression mode for utf-8 formatted text input.
49 Text = brotli_sys::BROTLI_MODE_TEXT as isize,
50 /// Compression mode in WOFF 2.0.
51 Font = brotli_sys::BROTLI_MODE_FONT as isize,
52}
53
54/// Parameters passed to various compression routines.
55#[derive(Clone,Debug)]
56pub struct CompressParams {
57 /// Compression mode.
58 mode: u32,
59 /// Controls the compression-speed vs compression-density tradeoffs. The higher the `quality`,
60 /// the slower the compression. Range is 0 to 11.
61 quality: u32,
62 /// Base 2 logarithm of the sliding window size. Range is 10 to 24.
63 lgwin: u32,
64 /// Base 2 logarithm of the maximum input block size. Range is 16 to 24. If set to 0, the value
65 /// will be set based on the quality.
66 lgblock: u32,
67}
68
69impl CompressParams {
70 /// Creates a new default set of compression parameters.
71 pub fn new() -> CompressParams {
72 CompressParams {
73 mode: brotli_sys::BROTLI_DEFAULT_MODE,
74 quality: brotli_sys::BROTLI_DEFAULT_QUALITY,
75 lgwin: brotli_sys::BROTLI_DEFAULT_WINDOW,
76 lgblock: 0,
77 }
78 }
79
80 /// Set the mode of this compression.
81 pub fn mode(&mut self, mode: CompressMode) -> &mut CompressParams {
82 self.mode = mode as u32;
83 self
84 }
85
86 /// Controls the compression-speed vs compression-density tradeoffs.
87 ///
88 /// The higher the quality, the slower the compression. Currently the range
89 /// for the quality is 0 to 11.
90 pub fn quality(&mut self, quality: u32) -> &mut CompressParams {
91 self.quality = quality;
92 self
93 }
94
95 /// Sets the base 2 logarithm of the sliding window size.
96 ///
97 /// Currently the range is 10 to 24.
98 pub fn lgwin(&mut self, lgwin: u32) -> &mut CompressParams {
99 self.lgwin = lgwin;
100 self
101 }
102
103 /// Sets the base 2 logarithm of the maximum input block size.
104 ///
105 /// Currently the range is 16 to 24, and if set to 0 the value will be set
106 /// based on the quality.
107 pub fn lgblock(&mut self, lgblock: u32) -> &mut CompressParams {
108 self.lgblock = lgblock;
109 self
110 }
111
112 /// Get the current block size
113 #[inline]
114 pub fn get_lgblock_readable(&self) -> usize {
115 1usize << self.lgblock
116 }
117
118 /// Get the native lgblock size
119 #[inline]
120 pub fn get_lgblock(&self) -> u32 {
121 self.lgblock.clone()
122 }
123 /// Get the current window size
124 #[inline]
125 pub fn get_lgwin_readable(&self) -> usize {
126 1usize << self.lgwin
127 }
128 /// Get the native lgwin value
129 #[inline]
130 pub fn get_lgwin(&self) -> u32 {
131 self.lgwin.clone()
132 }
133}