pub struct Decompressor { /* private fields */ }Expand description
Decompresses permessage-deflate messages.
The output budget is checked per output chunk while inflating, never
after, so a compressed bomb stops at the budget; the error is
ErrorKind::OutputLimit (WebSocket close code 1009). Undecodable data
is any other kind (close code 1007).
§Examples
use compress_lib::{ErrorKind, message::{Decompressor, MessageConfig}};
let mut rx = Decompressor::new(MessageConfig::new())?;
let mut text = Vec::new();
rx.decompress_message(&[0xF2, 0x48, 0xCD, 0xC9, 0xC9, 0x07, 0x00], &mut text, 1024)?;
assert_eq!(text, b"Hello");
let err = rx.decompress_message(&[0xF2, 0x48, 0xCD, 0xC9, 0xC9, 0x07, 0x00], &mut Vec::new(), 4).unwrap_err();
assert_eq!(err.kind(), ErrorKind::OutputLimit);Implementations§
Source§impl Decompressor
impl Decompressor
Sourcepub fn new(config: MessageConfig) -> Result<Self, Error>
pub fn new(config: MessageConfig) -> Result<Self, Error>
A decompressor for one direction of a connection.
§Errors
ErrorKind::InvalidWindowBits outside 8 to 15.
§Examples
use compress_lib::message::{Decompressor, MessageConfig};
assert!(Decompressor::new(MessageConfig::new().with_window_bits(8)).is_ok());Sourcepub fn decompress_message(
&mut self,
payload: &[u8],
out: &mut Vec<u8>,
max: usize,
) -> Result<usize, Error>
pub fn decompress_message( &mut self, payload: &[u8], out: &mut Vec<u8>, max: usize, ) -> Result<usize, Error>
Decompresses one whole message payload, appending to out, with at
most max bytes of output. Returns the bytes appended.
§Errors
OutputLimit past max; TrailingData for nonzero bytes after a
final block; any decoding error. After an error the decompressor is reset
(the connection should be failed anyway).
§Examples
use compress_lib::message::{Decompressor, MessageConfig};
let mut rx = Decompressor::new(MessageConfig::new())?;
let mut out = Vec::new();
assert_eq!(rx.decompress_message(&[0x00], &mut out, 16)?, 0); // empty messageSourcepub fn decompress_fragment(
&mut self,
fragment: &[u8],
last: bool,
out: &mut Vec<u8>,
max: usize,
) -> Result<usize, Error>
pub fn decompress_fragment( &mut self, fragment: &[u8], last: bool, out: &mut Vec<u8>, max: usize, ) -> Result<usize, Error>
Decompresses one frame of a message, appending to out; last marks
the final frame. Every fragment of a message must append to the same
vector, untouched in between: the vector is the message’s history.
max caps the whole message. Returns the bytes appended by this
fragment.
§Errors
As decompress_message, plus
InvalidState when out was changed between fragments.
§Examples
use compress_lib::message::{Decompressor, MessageConfig};
let payload = [0xF2, 0x48, 0xCD, 0xC9, 0xC9, 0x07, 0x00];
let mut rx = Decompressor::new(MessageConfig::new())?;
let mut out = Vec::new();
rx.decompress_fragment(&payload[..3], false, &mut out, 1024)?;
rx.decompress_fragment(&payload[3..], true, &mut out, 1024)?;
assert_eq!(out, b"Hello");Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Forgets the window and any open message.
§Examples
use compress_lib::message::{Decompressor, MessageConfig};
let mut rx = Decompressor::new(MessageConfig::new())?;
rx.reset();Sourcepub fn memory_in_use(&self) -> usize
pub fn memory_in_use(&self) -> usize
Heap bytes held: the decode tables, plus the window with takeover.
§Examples
use compress_lib::message::{Decompressor, MessageConfig};
let lean = Decompressor::new(MessageConfig::new())?;
let takeover = Decompressor::new(MessageConfig::new().with_context_takeover(true))?;
assert!(lean.memory_in_use() + 32 * 1024 <= takeover.memory_in_use());