Skip to main content

Decompressor

Struct Decompressor 

Source
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

Source

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());
Source

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; UnexpectedEnd when the message, with the 00 00 FF FF tail appended, does not end on a block boundary (a block cut short, or a sender that left the tail in place); 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 message
Source

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");
Source

pub fn wipe(&mut self)

Zeroes the decompressor’s window and decode tables and forgets any open message, keeping the allocations; see Inflater::wipe. The message vectors are the caller’s to wipe.

§Examples
use compress_lib::message::{Decompressor, MessageConfig};

let mut rx = Decompressor::new(MessageConfig::new().with_context_takeover(true))?;
rx.wipe();
Source

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();
Source

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());

Trait Implementations§

Source§

impl Debug for Decompressor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.