Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs
This commit is contained in:
+111
@@ -0,0 +1,111 @@
|
||||
use crate::DecodeV2;
|
||||
use brotli::{enc::StandardAlloc, BrotliDecompressStream, BrotliResult};
|
||||
use compression_core::util::{PartialBuffer, WriteBuffer};
|
||||
use std::{fmt, io};
|
||||
|
||||
type BrotliState = brotli::BrotliState<StandardAlloc, StandardAlloc, StandardAlloc>;
|
||||
|
||||
pub struct BrotliDecoder {
|
||||
// `BrotliState` is very large (over 2kb) which is why we're boxing it.
|
||||
state: Box<BrotliState>,
|
||||
}
|
||||
|
||||
impl Default for BrotliDecoder {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
state: Box::new(Self::new_brotli_state()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BrotliDecoder {
|
||||
fn new_brotli_state() -> BrotliState {
|
||||
BrotliState::new(
|
||||
StandardAlloc::default(),
|
||||
StandardAlloc::default(),
|
||||
StandardAlloc::default(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
fn decode(
|
||||
&mut self,
|
||||
input: &mut PartialBuffer<&[u8]>,
|
||||
output: &mut WriteBuffer<'_>,
|
||||
) -> io::Result<BrotliResult> {
|
||||
let in_buf = input.unwritten();
|
||||
let out_buf = output.initialize_unwritten();
|
||||
|
||||
let mut input_len = 0;
|
||||
let mut output_len = 0;
|
||||
|
||||
let status = match BrotliDecompressStream(
|
||||
&mut in_buf.len(),
|
||||
&mut input_len,
|
||||
in_buf,
|
||||
&mut out_buf.len(),
|
||||
&mut output_len,
|
||||
out_buf,
|
||||
&mut 0,
|
||||
&mut self.state,
|
||||
) {
|
||||
BrotliResult::ResultFailure => return Err(io::Error::other("brotli error")),
|
||||
status => status,
|
||||
};
|
||||
|
||||
input.advance(input_len);
|
||||
output.advance(output_len);
|
||||
|
||||
Ok(status)
|
||||
}
|
||||
}
|
||||
|
||||
impl DecodeV2 for BrotliDecoder {
|
||||
fn reinit(&mut self) -> io::Result<()> {
|
||||
*self.state = Self::new_brotli_state();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode(
|
||||
&mut self,
|
||||
input: &mut PartialBuffer<&[u8]>,
|
||||
output: &mut WriteBuffer<'_>,
|
||||
) -> io::Result<bool> {
|
||||
match self.decode(input, output)? {
|
||||
BrotliResult::ResultSuccess => Ok(true),
|
||||
BrotliResult::NeedsMoreOutput | BrotliResult::NeedsMoreInput => Ok(false),
|
||||
BrotliResult::ResultFailure => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
|
||||
match self.decode(&mut PartialBuffer::new(&[][..]), output)? {
|
||||
BrotliResult::ResultSuccess | BrotliResult::NeedsMoreInput => Ok(true),
|
||||
BrotliResult::NeedsMoreOutput => Ok(false),
|
||||
BrotliResult::ResultFailure => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
|
||||
match self.decode(&mut PartialBuffer::new(&[][..]), output)? {
|
||||
BrotliResult::ResultSuccess => Ok(true),
|
||||
BrotliResult::NeedsMoreOutput => Ok(false),
|
||||
BrotliResult::NeedsMoreInput => Err(io::Error::new(
|
||||
io::ErrorKind::UnexpectedEof,
|
||||
"reached unexpected EOF",
|
||||
)),
|
||||
BrotliResult::ResultFailure => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for BrotliDecoder {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("BrotliDecoder")
|
||||
.field("decompress", &"<no debug>")
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
use crate::{brotli::params::EncoderParams, EncodeV2};
|
||||
use brotli::enc::{
|
||||
backward_references::BrotliEncoderParams,
|
||||
encode::{BrotliEncoderOperation, BrotliEncoderStateStruct},
|
||||
StandardAlloc,
|
||||
};
|
||||
use compression_core::util::{PartialBuffer, WriteBuffer};
|
||||
use std::{fmt, io};
|
||||
|
||||
pub struct BrotliEncoder {
|
||||
state: BrotliEncoderStateStruct<StandardAlloc>,
|
||||
}
|
||||
|
||||
impl BrotliEncoder {
|
||||
pub fn new(params: EncoderParams) -> Self {
|
||||
let params = BrotliEncoderParams::from(params);
|
||||
let mut state = BrotliEncoderStateStruct::new(StandardAlloc::default());
|
||||
state.params = params;
|
||||
Self { state }
|
||||
}
|
||||
|
||||
fn encode(
|
||||
&mut self,
|
||||
input: &mut PartialBuffer<&[u8]>,
|
||||
output: &mut WriteBuffer<'_>,
|
||||
op: BrotliEncoderOperation,
|
||||
) -> io::Result<()> {
|
||||
let in_buf = input.unwritten();
|
||||
let out_buf = output.initialize_unwritten();
|
||||
|
||||
let mut input_len = 0;
|
||||
let mut output_len = 0;
|
||||
|
||||
if !self.state.compress_stream(
|
||||
op,
|
||||
&mut in_buf.len(),
|
||||
in_buf,
|
||||
&mut input_len,
|
||||
&mut out_buf.len(),
|
||||
out_buf,
|
||||
&mut output_len,
|
||||
&mut None,
|
||||
&mut |_, _, _, _| (),
|
||||
) {
|
||||
return Err(io::Error::other("brotli error"));
|
||||
}
|
||||
|
||||
input.advance(input_len);
|
||||
output.advance(output_len);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl EncodeV2 for BrotliEncoder {
|
||||
fn encode(
|
||||
&mut self,
|
||||
input: &mut PartialBuffer<&[u8]>,
|
||||
output: &mut WriteBuffer<'_>,
|
||||
) -> io::Result<()> {
|
||||
self.encode(
|
||||
input,
|
||||
output,
|
||||
BrotliEncoderOperation::BROTLI_OPERATION_PROCESS,
|
||||
)
|
||||
}
|
||||
|
||||
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
|
||||
self.encode(
|
||||
&mut PartialBuffer::new(&[][..]),
|
||||
output,
|
||||
BrotliEncoderOperation::BROTLI_OPERATION_FLUSH,
|
||||
)?;
|
||||
|
||||
Ok(!self.state.has_more_output())
|
||||
}
|
||||
|
||||
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
|
||||
self.encode(
|
||||
&mut PartialBuffer::new(&[][..]),
|
||||
output,
|
||||
BrotliEncoderOperation::BROTLI_OPERATION_FINISH,
|
||||
)?;
|
||||
|
||||
Ok(self.state.is_finished())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for BrotliEncoder {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("BrotliEncoder")
|
||||
.field("compress", &"<no debug>")
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
mod decoder;
|
||||
mod encoder;
|
||||
pub mod params;
|
||||
|
||||
pub use self::{decoder::BrotliDecoder, encoder::BrotliEncoder};
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
//! This module contains Brotli-specific types for async-compression.
|
||||
|
||||
use brotli::enc::backward_references::{BrotliEncoderMode, BrotliEncoderParams};
|
||||
use compression_core::Level;
|
||||
|
||||
/// Brotli compression parameters builder. This is a stable wrapper around Brotli's own encoder
|
||||
/// params type, to abstract over different versions of the Brotli library.
|
||||
///
|
||||
/// See the [Brotli documentation](https://www.brotli.org/encode.html#a9a8) for more information on
|
||||
/// these parameters.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use compression_codecs::brotli;
|
||||
///
|
||||
/// let params = brotli::params::EncoderParams::default()
|
||||
/// .window_size(12)
|
||||
/// .text_mode();
|
||||
/// ```
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct EncoderParams {
|
||||
inner: BrotliEncoderParams,
|
||||
}
|
||||
|
||||
impl From<EncoderParams> for BrotliEncoderParams {
|
||||
fn from(value: EncoderParams) -> Self {
|
||||
value.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl EncoderParams {
|
||||
pub fn quality(mut self, level: Level) -> Self {
|
||||
let quality = match level {
|
||||
Level::Fastest => Some(0),
|
||||
Level::Best => Some(11),
|
||||
Level::Precise(quality) => Some(quality.clamp(0, 11)),
|
||||
_ => None,
|
||||
};
|
||||
match quality {
|
||||
Some(quality) => self.inner.quality = quality,
|
||||
None => {
|
||||
let default_params = BrotliEncoderParams::default();
|
||||
self.inner.quality = default_params.quality;
|
||||
}
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets window size in bytes (as a power of two).
|
||||
///
|
||||
/// Used as Brotli's `lgwin` parameter.
|
||||
///
|
||||
/// `window_size` is clamped to `0 <= window_size <= 24`.
|
||||
pub fn window_size(mut self, window_size: i32) -> Self {
|
||||
self.inner.lgwin = window_size.clamp(0, 24);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets input block size in bytes (as a power of two).
|
||||
///
|
||||
/// Used as Brotli's `lgblock` parameter.
|
||||
///
|
||||
/// `block_size` is clamped to `16 <= block_size <= 24`.
|
||||
pub fn block_size(mut self, block_size: i32) -> Self {
|
||||
self.inner.lgblock = block_size.clamp(16, 24);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets hint for size of data to be compressed.
|
||||
pub fn size_hint(mut self, size_hint: usize) -> Self {
|
||||
self.inner.size_hint = size_hint;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets encoder to text mode.
|
||||
///
|
||||
/// If input data is known to be UTF-8 text, this allows the compressor to make assumptions and
|
||||
/// optimizations.
|
||||
///
|
||||
/// Used as Brotli's `mode` parameter.
|
||||
pub fn text_mode(mut self) -> Self {
|
||||
self.inner.mode = BrotliEncoderMode::BROTLI_MODE_TEXT;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn mode(mut self, mode: BrotliEncoderMode) -> Self {
|
||||
self.inner.mode = mode;
|
||||
self
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user