fix compression, rename enum values

This commit is contained in:
Vinzenz Schroeter 2024-05-16 21:32:33 +02:00
parent 7b6b4ea806
commit 6834bb084b
10 changed files with 119 additions and 123 deletions

38
Cargo.lock generated
View file

@ -271,26 +271,6 @@ version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
[[package]]
name = "lz4"
version = "1.24.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e9e2dd86df36ce760a60f6ff6ad526f7ba1f14ba0356f8254fb6905e6494df1"
dependencies = [
"libc",
"lz4-sys",
]
[[package]]
name = "lz4-sys"
version = "1.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57d27b317e207b10f69f5e75494119e391a96f48861ae870d1da6edac98ca900"
dependencies = [
"cc",
"libc",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.7.2" version = "2.7.2"
@ -420,6 +400,16 @@ version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56"
[[package]]
name = "rust-lzma"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d62915608f6cee1d7f2fc00f28b4f058ff79d6e4ec3c2fe0006b09b52437c84"
dependencies = [
"pkg-config",
"vcpkg",
]
[[package]] [[package]]
name = "servicepoint-binding-cs" name = "servicepoint-binding-cs"
version = "0.1.0" version = "0.1.0"
@ -435,7 +425,7 @@ dependencies = [
"bzip2", "bzip2",
"flate2", "flate2",
"log", "log",
"lz4", "rust-lzma",
"zstd", "zstd",
] ]
@ -479,6 +469,12 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"

View file

@ -25,7 +25,7 @@ fn main() {
connection connection
.send(Command::BitmapLinearWin(Origin::top_left(), field.clone()).into()) .send(Command::BitmapLinearWin(Origin::top_left(), field.clone()).into())
.expect("could not send"); .expect("could not send");
thread::sleep(Duration::from_millis(14)); thread::sleep(Duration::from_millis(30));
field = iteration(field); field = iteration(field);
} }
} }

View file

@ -3,9 +3,7 @@ use std::time::Duration;
use clap::Parser; use clap::Parser;
use servicepoint2::{ use servicepoint2::{BitVec, Command, CompressionCode, Connection, Origin, PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid};
BitVec, Command, CompressionCode, Connection, PIXEL_HEIGHT, PIXEL_WIDTH, PixelGrid
};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
struct Cli { struct Cli {
@ -20,6 +18,12 @@ fn main() {
let cli = Cli::parse(); let cli = Cli::parse();
let connection = Connection::open(cli.destination).unwrap(); let connection = Connection::open(cli.destination).unwrap();
let mut buf = PixelGrid::max_sized();
buf.fill(true);
connection.send(Command::BitmapLinearWin(Origin(0, 0), buf).into())
.expect("send failed");
let sleep_duration = Duration::from_millis(cli.time / PIXEL_WIDTH as u64); let sleep_duration = Duration::from_millis(cli.time / PIXEL_WIDTH as u64);
let mut enabled_pixels = let mut enabled_pixels =
@ -36,7 +40,7 @@ fn main() {
let bit_vec = BitVec::from(&*pixel_data); let bit_vec = BitVec::from(&*pixel_data);
connection connection
.send(Command::BitmapLinearAnd(0, bit_vec, CompressionCode::Gz).into()) .send(Command::BitmapLinearAnd(0, bit_vec, CompressionCode::Lzma).into())
.unwrap(); .unwrap();
thread::sleep(sleep_duration); thread::sleep(sleep_duration);
} }

View file

@ -19,11 +19,10 @@ sort_by = "Name"
usize_is_size_t = true usize_is_size_t = true
[defines] [defines]
"feature = compression-gz" = "SP2_FEATURE_compression_gz" "feature = compression_zlib" = "SP2_FEATURE_compression_zlib"
"feature = compression-bz" = "SP2_FEATURE_compression_bz" "feature = compression_bzip2" = "SP2_FEATURE_compression_bzip2"
"feature = compression-lz" = "SP2_FEATURE_compression_lz" "feature = compression_lzma" = "SP2_FEATURE_compression_lzma"
"feature = compression-zs" = "SP2_FEATURE_compression_zs" "feature = compression_zstd" = "SP2_FEATURE_compression_zstd"
#"feature = c-api" = "SP2_FEATURE_c-api"
[export] [export]
prefix = "sp2_" prefix = "sp2_"

View file

@ -45,17 +45,17 @@ enum sp2_CompressionCode
#endif // __cplusplus #endif // __cplusplus
{ {
Uncompressed = 0, Uncompressed = 0,
#if defined(SP2_FEATURE_compression_gz) #if defined(SP2_FEATURE_compression_zlib)
Gz = 26490, Zlib = 26490,
#endif #endif
#if defined(SP2_FEATURE_compression_bz) #if defined(SP2_FEATURE_compression_bzip2)
Bz = 25210, Bzip2 = 25210,
#endif #endif
#if defined(SP2_FEATURE_compression_lz) #if defined(SP2_FEATURE_compression_lzma)
Lz = 27770, Lzma = 27770,
#endif #endif
#if defined(SP2_FEATURE_compression_zs) #if defined(SP2_FEATURE_compression_zstd)
Zs = 31347, Zstd = 31347,
#endif #endif
}; };
#ifndef __cplusplus #ifndef __cplusplus

View file

@ -276,10 +276,10 @@ namespace ServicePoint2.BindGen
public enum CompressionCode : ushort public enum CompressionCode : ushort
{ {
Uncompressed = 0, Uncompressed = 0,
Gz = 26490, Zlib = 26490,
Bz = 25210, Bzip2 = 25210,
Lz = 27770, Lzma = 27770,
Zs = 31347, Zstd = 31347,
} }

View file

@ -16,14 +16,14 @@ crate-type = ["staticlib", "rlib", "cdylib"]
log = "0.4" log = "0.4"
flate2 = { version = "1.0", optional = true } flate2 = { version = "1.0", optional = true }
bzip2 = { version = "0.4", optional = true } bzip2 = { version = "0.4", optional = true }
lz4 = { version = "1.24", optional = true }
zstd = { version = "0.13", optional = true } zstd = { version = "0.13", optional = true }
rust-lzma = { version = "0.6.0", optional = true }
[features] [features]
default = ["compression-gz", "compression-bz", "compression-lz", "compression-zs"] default = ["compression_zlib", "compression_bzip2", "compression_lzma", "compression_zstd"]
compression-gz = ["dep:flate2", "compression"] compression_zlib = ["dep:flate2", "compression"]
compression-bz = ["dep:bzip2", "compression"] compression_bzip2 = ["dep:bzip2", "compression"]
compression-lz = ["dep:lz4", "compression"] compression_lzma = ["dep:rust-lzma", "compression"]
compression-zs = ["dep:zstd", "compression"] compression_zstd = ["dep:zstd", "compression"]
compression = [] compression = []
c-api = [] c-api = []

View file

@ -252,12 +252,13 @@ fn bitmap_linear_into_packet(
compression: CompressionCode, compression: CompressionCode,
payload: Vec<u8>, payload: Vec<u8>,
) -> Packet { ) -> Packet {
let length = payload.len() as u16;
let payload = into_compressed(compression, payload); let payload = into_compressed(compression, payload);
Packet( Packet(
Header( Header(
command.into(), command.into(),
offset, offset,
payload.len() as u16, length,
compression.into(), compression.into(),
0, 0,
), ),
@ -290,12 +291,6 @@ fn packet_into_linear_bitmap(
if reserved != 0 { if reserved != 0 {
return Err(TryFromPacketError::ExtraneousHeaderValues); return Err(TryFromPacketError::ExtraneousHeaderValues);
} }
if payload.len() != length as usize {
return Err(TryFromPacketError::UnexpectedPayloadSize(
length as usize,
payload.len(),
));
}
let sub = match CompressionCode::try_from(sub) { let sub = match CompressionCode::try_from(sub) {
Err(_) => return Err(TryFromPacketError::InvalidCompressionCode(sub)), Err(_) => return Err(TryFromPacketError::InvalidCompressionCode(sub)),
Ok(value) => value, Ok(value) => value,
@ -304,6 +299,12 @@ fn packet_into_linear_bitmap(
None => return Err(TryFromPacketError::DecompressionFailed), None => return Err(TryFromPacketError::DecompressionFailed),
Some(value) => value, Some(value) => value,
}; };
if payload.len() != length as usize {
return Err(TryFromPacketError::UnexpectedPayloadSize(
length as usize,
payload.len(),
));
}
Ok((BitVec::from(&*payload), sub)) Ok((BitVec::from(&*payload), sub))
} }

View file

@ -1,33 +1,40 @@
#[cfg(feature = "compression-bz")]
use bzip2::read::{BzDecoder, BzEncoder};
#[cfg(feature = "compression-gz")]
use flate2::read::{GzDecoder, GzEncoder};
#[cfg(feature = "compression-lz")]
use lz4::{Decoder as Lz4Decoder, EncoderBuilder as Lz4EncoderBuilder};
#[cfg(feature = "compression")] #[cfg(feature = "compression")]
use std::io::{Read, Write}; use std::io::{Read, Write};
#[cfg(feature = "compression-zs")]
#[cfg(feature = "compression_bzip2")]
use bzip2::read::{BzDecoder, BzEncoder};
use flate2::{FlushCompress, FlushDecompress, Status};
#[cfg(feature = "compression_lzma")]
use lzma;
#[cfg(feature = "compression_zstd")]
use zstd::{Decoder as ZstdDecoder, Encoder as ZstdEncoder}; use zstd::{Decoder as ZstdDecoder, Encoder as ZstdEncoder};
use crate::{CompressionCode, Payload}; use crate::{CompressionCode, Payload};
pub(crate) fn into_decompressed( pub(crate) fn into_decompressed(
kind: CompressionCode, kind: CompressionCode,
payload: Payload, mut payload: Payload,
) -> Option<Payload> { ) -> Option<Payload> {
match kind { match kind {
CompressionCode::Uncompressed => Some(payload), CompressionCode::Uncompressed => Some(payload),
#[cfg(feature = "compression-gz")] #[cfg(feature = "compression_zlib")]
CompressionCode::Gz => { CompressionCode::Zlib => {
let mut decoder = GzDecoder::new(&*payload); let mut decompress = flate2::Decompress::new(true);
let mut decompressed = vec![]; let mut buffer = [0u8; 10000];
match decoder.read_to_end(&mut decompressed) {
Err(_) => None, let status = match decompress.decompress(&*payload, &mut buffer, FlushDecompress::Finish) {
Ok(_) => Some(decompressed), Err(_) => return None,
Ok(status) => status,
};
match status {
Status::Ok => None,
Status::BufError => None,
Status::StreamEnd => Some(buffer[0..(decompress.total_out() as usize)].to_owned()),
} }
} }
#[cfg(feature = "compression-bz")] #[cfg(feature = "compression_bzip2")]
CompressionCode::Bz => { CompressionCode::Bzip2 => {
let mut decoder = BzDecoder::new(&*payload); let mut decoder = BzDecoder::new(&*payload);
let mut decompressed = vec![]; let mut decompressed = vec![];
match decoder.read_to_end(&mut decompressed) { match decoder.read_to_end(&mut decompressed) {
@ -35,20 +42,12 @@ pub(crate) fn into_decompressed(
Ok(_) => Some(decompressed), Ok(_) => Some(decompressed),
} }
} }
#[cfg(feature = "compression-lz")] #[cfg(feature = "compression_lzma")]
CompressionCode::Lz => { CompressionCode::Lzma => {
let mut decoder = match Lz4Decoder::new(&*payload) { Some(lzma::decompress(&mut payload).unwrap())
Err(_) => return None,
Ok(value) => value,
};
let mut decompressed = vec![];
match decoder.read_to_end(&mut decompressed) {
Err(_) => None,
Ok(_) => Some(decompressed),
}
} }
#[cfg(feature = "compression-zs")] #[cfg(feature = "compression_zstd")]
CompressionCode::Zs => { CompressionCode::Zstd => {
let mut decoder = match ZstdDecoder::new(&*payload) { let mut decoder = match ZstdDecoder::new(&*payload) {
Err(_) => return None, Err(_) => return None,
Ok(value) => value, Ok(value) => value,
@ -68,18 +67,20 @@ pub(crate) fn into_compressed(
) -> Payload { ) -> Payload {
match kind { match kind {
CompressionCode::Uncompressed => payload, CompressionCode::Uncompressed => payload,
#[cfg(feature = "compression-gz")] #[cfg(feature = "compression_zlib")]
CompressionCode::Gz => { CompressionCode::Zlib => {
let mut encoder = let mut compress = flate2::Compress::new(flate2::Compression::fast(), true);
GzEncoder::new(&*payload, flate2::Compression::fast()); let mut buffer = [0u8; 10000];
let mut compressed = vec![];
match encoder.read_to_end(&mut compressed) { match compress.compress(&*payload, &mut buffer, FlushCompress::Finish).expect("compress failed") {
Err(err) => panic!("could not compress payload: {}", err), Status::Ok => panic!("buffer should be big enough"),
Ok(_) => compressed, Status::BufError => panic!("BufError"),
} Status::StreamEnd => {}
};
buffer[..compress.total_out() as usize].to_owned()
} }
#[cfg(feature = "compression-bz")] #[cfg(feature = "compression_bzip2")]
CompressionCode::Bz => { CompressionCode::Bzip2 => {
let mut encoder = let mut encoder =
BzEncoder::new(&*payload, bzip2::Compression::fast()); BzEncoder::new(&*payload, bzip2::Compression::fast());
let mut compressed = vec![]; let mut compressed = vec![];
@ -88,17 +89,12 @@ pub(crate) fn into_compressed(
Ok(_) => compressed, Ok(_) => compressed,
} }
} }
#[cfg(feature = "compression-lz")] #[cfg(feature = "compression_lzma")]
CompressionCode::Lz => { CompressionCode::Lzma => {
let mut encoder = Lz4EncoderBuilder::new() lzma::compress(&payload, 6).unwrap()
.build(vec![])
.expect("could not create encoder");
encoder.write_all(&payload).expect("could not write payload");
let (payload, _) = encoder.finish();
payload
} }
#[cfg(feature = "compression-zs")] #[cfg(feature = "compression_zstd")]
CompressionCode::Zs => { CompressionCode::Zstd => {
let mut encoder = let mut encoder =
ZstdEncoder::new(vec![], zstd::DEFAULT_COMPRESSION_LEVEL) ZstdEncoder::new(vec![], zstd::DEFAULT_COMPRESSION_LEVEL)
.expect("could not create encoder"); .expect("could not create encoder");

View file

@ -5,14 +5,14 @@ use CompressionCode::*;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum CompressionCode { pub enum CompressionCode {
Uncompressed = 0x0, Uncompressed = 0x0,
#[cfg(feature = "compression-gz")] #[cfg(feature = "compression_zlib")]
Gz = 0x677a, Zlib = 0x677a,
#[cfg(feature = "compression-bz")] #[cfg(feature = "compression_bzip2")]
Bz = 0x627a, Bzip2 = 0x627a,
#[cfg(feature = "compression-lz")] #[cfg(feature = "compression_lzma")]
Lz = 0x6c7a, Lzma = 0x6c7a,
#[cfg(feature = "compression-zs")] #[cfg(feature = "compression_zstd")]
Zs = 0x7a73, Zstd = 0x7a73,
} }
impl From<CompressionCode> for u16 { impl From<CompressionCode> for u16 {
@ -27,14 +27,14 @@ impl TryFrom<u16> for CompressionCode {
fn try_from(value: u16) -> Result<Self, Self::Error> { fn try_from(value: u16) -> Result<Self, Self::Error> {
match value { match value {
value if value == Uncompressed as u16 => Ok(Uncompressed), value if value == Uncompressed as u16 => Ok(Uncompressed),
#[cfg(feature = "compression-gz")] #[cfg(feature = "compression_zlib")]
value if value == Gz as u16 => Ok(Gz), value if value == Zlib as u16 => Ok(Zlib),
#[cfg(feature = "compression-bz")] #[cfg(feature = "compression_bzip2")]
value if value == Bz as u16 => Ok(Bz), value if value == Bzip2 as u16 => Ok(Bzip2),
#[cfg(feature = "compression-lz")] #[cfg(feature = "compression_lzma")]
value if value == Lz as u16 => Ok(Lz), value if value == Lzma as u16 => Ok(Lzma),
#[cfg(feature = "compression-zs")] #[cfg(feature = "compression_zstd")]
value if value == Zs as u16 => Ok(Zs), value if value == Zstd as u16 => Ok(Zstd),
_ => Err(()), _ => Err(()),
} }
} }