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:
+123
@@ -0,0 +1,123 @@
|
||||
use crate::DecodeV2;
|
||||
use bzip2::{Decompress, Status};
|
||||
use compression_core::util::{PartialBuffer, WriteBuffer};
|
||||
use std::{fmt, io};
|
||||
|
||||
pub struct BzDecoder {
|
||||
decompress: Decompress,
|
||||
stream_ended: bool,
|
||||
}
|
||||
|
||||
impl fmt::Debug for BzDecoder {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"BzDecoder {{total_in: {}, total_out: {}}}",
|
||||
self.decompress.total_in(),
|
||||
self.decompress.total_out()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for BzDecoder {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
decompress: Decompress::new(false),
|
||||
stream_ended: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BzDecoder {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
fn decode(
|
||||
&mut self,
|
||||
input: &mut PartialBuffer<&[u8]>,
|
||||
output: &mut WriteBuffer<'_>,
|
||||
) -> io::Result<Status> {
|
||||
let prior_in = self.decompress.total_in();
|
||||
let prior_out = self.decompress.total_out();
|
||||
|
||||
let status = self
|
||||
.decompress
|
||||
// Safety: We **trust** bzip2 to only write initialized data to it
|
||||
.decompress_uninit(input.unwritten(), unsafe { output.unwritten_mut() })
|
||||
.map_err(io::Error::other)?;
|
||||
|
||||
input.advance((self.decompress.total_in() - prior_in) as usize);
|
||||
// Safety: We **trust** bzip2 to write bytes properly
|
||||
unsafe {
|
||||
output.assume_init_and_advance((self.decompress.total_out() - prior_out) as usize)
|
||||
};
|
||||
|
||||
// Track when stream has properly ended
|
||||
if status == Status::StreamEnd {
|
||||
self.stream_ended = true;
|
||||
}
|
||||
|
||||
Ok(status)
|
||||
}
|
||||
}
|
||||
|
||||
impl DecodeV2 for BzDecoder {
|
||||
fn reinit(&mut self) -> io::Result<()> {
|
||||
self.decompress = Decompress::new(false);
|
||||
self.stream_ended = false;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn decode(
|
||||
&mut self,
|
||||
input: &mut PartialBuffer<&[u8]>,
|
||||
output: &mut WriteBuffer<'_>,
|
||||
) -> io::Result<bool> {
|
||||
match self.decode(input, output)? {
|
||||
// Decompression went fine, nothing much to report.
|
||||
Status::Ok => Ok(false),
|
||||
|
||||
// The Flush action on a compression went ok.
|
||||
Status::FlushOk => unreachable!(),
|
||||
|
||||
// THe Run action on compression went ok.
|
||||
Status::RunOk => unreachable!(),
|
||||
|
||||
// The Finish action on compression went ok.
|
||||
Status::FinishOk => unreachable!(),
|
||||
|
||||
// The stream's end has been met, meaning that no more data can be input.
|
||||
Status::StreamEnd => Ok(true),
|
||||
|
||||
// There was insufficient memory in the input or output buffer to complete
|
||||
// the request, but otherwise everything went normally.
|
||||
Status::MemNeeded => Err(io::ErrorKind::OutOfMemory.into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
|
||||
self.decode(&mut PartialBuffer::new(&[][..]), output)?;
|
||||
|
||||
loop {
|
||||
let old_len = output.written_len();
|
||||
self.decode(&mut PartialBuffer::new(&[][..]), output)?;
|
||||
if output.written_len() == old_len {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(!output.has_no_spare_space())
|
||||
}
|
||||
|
||||
fn finish(&mut self, _output: &mut WriteBuffer<'_>) -> io::Result<bool> {
|
||||
if self.stream_ended {
|
||||
Ok(true)
|
||||
} else {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::UnexpectedEof,
|
||||
"bzip2 stream did not finish",
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
use crate::{bzip2::params::Bzip2EncoderParams, EncodeV2};
|
||||
use bzip2::{Action, Compress, Compression, Status};
|
||||
use compression_core::util::{PartialBuffer, WriteBuffer};
|
||||
use std::{fmt, io};
|
||||
|
||||
pub struct BzEncoder {
|
||||
compress: Compress,
|
||||
}
|
||||
|
||||
impl fmt::Debug for BzEncoder {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"BzEncoder {{total_in: {}, total_out: {}}}",
|
||||
self.compress.total_in(),
|
||||
self.compress.total_out()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl BzEncoder {
|
||||
/// Creates a new stream prepared for compression.
|
||||
///
|
||||
/// The `work_factor` parameter controls how the compression phase behaves
|
||||
/// when presented with worst case, highly repetitive, input data. If
|
||||
/// compression runs into difficulties caused by repetitive data, the
|
||||
/// library switches from the standard sorting algorithm to a fallback
|
||||
/// algorithm. The fallback is slower than the standard algorithm by perhaps
|
||||
/// a factor of three, but always behaves reasonably, no matter how bad the
|
||||
/// input.
|
||||
///
|
||||
/// Lower values of `work_factor` reduce the amount of effort the standard
|
||||
/// algorithm will expend before resorting to the fallback. You should set
|
||||
/// this parameter carefully; too low, and many inputs will be handled by
|
||||
/// the fallback algorithm and so compress rather slowly, too high, and your
|
||||
/// average-to-worst case compression times can become very large. The
|
||||
/// default value of 30 gives reasonable behaviour over a wide range of
|
||||
/// circumstances.
|
||||
///
|
||||
/// Allowable values range from 0 to 250 inclusive. 0 is a special case,
|
||||
/// equivalent to using the default value of 30.
|
||||
pub fn new(params: Bzip2EncoderParams, work_factor: u32) -> Self {
|
||||
let params = Compression::from(params);
|
||||
Self {
|
||||
compress: Compress::new(params, work_factor),
|
||||
}
|
||||
}
|
||||
|
||||
fn encode(
|
||||
&mut self,
|
||||
input: &mut PartialBuffer<&[u8]>,
|
||||
output: &mut WriteBuffer<'_>,
|
||||
action: Action,
|
||||
) -> io::Result<Status> {
|
||||
let prior_in = self.compress.total_in();
|
||||
let prior_out = self.compress.total_out();
|
||||
|
||||
let status = self
|
||||
.compress
|
||||
// Safety: We **trust** bzip2 to only write initialized bytes into it
|
||||
.compress_uninit(input.unwritten(), unsafe { output.unwritten_mut() }, action)
|
||||
.map_err(io::Error::other)?;
|
||||
|
||||
input.advance((self.compress.total_in() - prior_in) as usize);
|
||||
// Safety: We **trust** bzip2 to properly write bytes into it
|
||||
unsafe { output.assume_init_and_advance((self.compress.total_out() - prior_out) as usize) };
|
||||
|
||||
Ok(status)
|
||||
}
|
||||
}
|
||||
|
||||
impl EncodeV2 for BzEncoder {
|
||||
fn encode(
|
||||
&mut self,
|
||||
input: &mut PartialBuffer<&[u8]>,
|
||||
output: &mut WriteBuffer<'_>,
|
||||
) -> io::Result<()> {
|
||||
match self.encode(input, output, Action::Run)? {
|
||||
// Decompression went fine, nothing much to report.
|
||||
Status::Ok => Ok(()),
|
||||
|
||||
// The Flush action on a compression went ok.
|
||||
Status::FlushOk => unreachable!(),
|
||||
|
||||
// The Run action on compression went ok.
|
||||
Status::RunOk => Ok(()),
|
||||
|
||||
// The Finish action on compression went ok.
|
||||
Status::FinishOk => unreachable!(),
|
||||
|
||||
// The stream's end has been met, meaning that no more data can be input.
|
||||
Status::StreamEnd => unreachable!(),
|
||||
|
||||
// There was insufficient memory in the input or output buffer to complete
|
||||
// the request, but otherwise everything went normally.
|
||||
Status::MemNeeded => Err(io::ErrorKind::OutOfMemory.into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
|
||||
match self.encode(&mut PartialBuffer::new(&[][..]), output, Action::Flush)? {
|
||||
// Decompression went fine, nothing much to report.
|
||||
Status::Ok => unreachable!(),
|
||||
|
||||
// The Flush action on a compression went ok.
|
||||
Status::FlushOk => Ok(false),
|
||||
|
||||
// The Run action on compression went ok.
|
||||
Status::RunOk => Ok(true),
|
||||
|
||||
// The Finish action on compression went ok.
|
||||
Status::FinishOk => unreachable!(),
|
||||
|
||||
// The stream's end has been met, meaning that no more data can be input.
|
||||
Status::StreamEnd => unreachable!(),
|
||||
|
||||
// There was insufficient memory in the input or output buffer to complete
|
||||
// the request, but otherwise everything went normally.
|
||||
Status::MemNeeded => Err(io::ErrorKind::OutOfMemory.into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
|
||||
match self.encode(&mut PartialBuffer::new(&[][..]), output, Action::Finish)? {
|
||||
// Decompression went fine, nothing much to report.
|
||||
Status::Ok => Ok(false),
|
||||
|
||||
// The Flush action on a compression went ok.
|
||||
Status::FlushOk => unreachable!(),
|
||||
|
||||
// The Run action on compression went ok.
|
||||
Status::RunOk => unreachable!(),
|
||||
|
||||
// The Finish action on compression went ok.
|
||||
Status::FinishOk => Ok(false),
|
||||
|
||||
// The stream's end has been met, meaning that no more data can be input.
|
||||
Status::StreamEnd => Ok(true),
|
||||
|
||||
// There was insufficient memory in the input or output buffer to complete
|
||||
// the request, but otherwise everything went normally.
|
||||
Status::MemNeeded => Err(io::ErrorKind::OutOfMemory.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
mod decoder;
|
||||
mod encoder;
|
||||
pub mod params;
|
||||
|
||||
pub use self::{decoder::BzDecoder, encoder::BzEncoder};
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
use std::convert::TryInto;
|
||||
|
||||
use bzip2::Compression;
|
||||
use compression_core::Level;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Bzip2EncoderParams {
|
||||
inner: Compression,
|
||||
}
|
||||
|
||||
impl From<Bzip2EncoderParams> for Compression {
|
||||
fn from(value: Bzip2EncoderParams) -> Self {
|
||||
value.inner
|
||||
}
|
||||
}
|
||||
impl From<Level> for Bzip2EncoderParams {
|
||||
fn from(value: Level) -> Self {
|
||||
let fastest = bzip2::Compression::fast();
|
||||
let best = bzip2::Compression::best();
|
||||
|
||||
let inner = match value {
|
||||
Level::Fastest => fastest,
|
||||
Level::Best => best,
|
||||
Level::Precise(quality) => bzip2::Compression::new(
|
||||
quality
|
||||
.try_into()
|
||||
.unwrap_or(0)
|
||||
.clamp(fastest.level(), best.level()),
|
||||
),
|
||||
_ => bzip2::Compression::default(),
|
||||
};
|
||||
Self { inner }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user