Support LZ4 compression

This commit is contained in:
Jaby 2022-12-06 04:56:40 +01:00
parent 166e889162
commit 5de6efe2c1
6 changed files with 22 additions and 20 deletions

View File

@ -1,2 +0,0 @@
[target.x86_64-unknown-linux-musl]
linker = "rust-lld"

View File

@ -117,7 +117,7 @@ fn parse_configuration(config: config_reader::Configuration) -> Result<CDDesc, E
let mut desc_file = {
match file.kind {
config_reader::FileKind::Regular => types::File::new_regular(file.common.name.as_str(), read_file(&file.path)?)?,
config_reader::FileKind::Overlay => types::overlay::load_from(file.path)?,
config_reader::FileKind::Overlay => types::overlay::load_from(file.common.name.as_str(), file.path)?,
}
};

View File

@ -1,7 +1,6 @@
use super::File;
use std::path::PathBuf;
use byteorder::{ByteOrder, BigEndian, LittleEndian};
use lz4::EncoderBuilder;
use byteorder::{ByteOrder, LittleEndian};
use tool_helper::{Error, format_if_error, read_file};
#[repr(packed)]
@ -17,7 +16,7 @@ impl OverlayHeader {
}
}
pub fn load_from(file_path: PathBuf) -> Result<File, Error> {
pub fn load_from(file_name: &str, file_path: PathBuf) -> Result<File, Error> {
let overlay_header_size = std::mem::size_of::<OverlayHeader>();
let mut content = read_file(&file_path)?;
@ -39,17 +38,6 @@ pub fn load_from(file_path: PathBuf) -> Result<File, Error> {
count += 1;
}
let mut lz4_encoder = EncoderBuilder::new().level(16).build(Vec::<u8>::new())?;
std::io::copy(&mut&content[..], &mut lz4_encoder)?;
let (output, result) = lz4_encoder.finish();
match result {
Ok(()) => println!("Wuff: {}", output.len()),
Err(error) => {
return Err(Error::from_error(error));
}
}
Err(Error::not_implemented("load_from overlay"))
let content = format_if_error!(tool_helper::compress::lz4(content, 16), "Compressing {} failed with \"{error_text}\"", file_path.to_string_lossy())?;
Ok(File::new_overlay(file_name, content)?)
}

View File

@ -1,10 +1,11 @@
[package]
name = "tool_helper"
version = "0.5.0"
version = "0.6.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
cdtypes = {path = "../cdtypes"}
lz4 = "*"
paste = "*"

View File

@ -0,0 +1,14 @@
use super::Error;
use lz4::EncoderBuilder;
pub fn lz4(data: Vec<u8>, compression_level: u32) -> Result<Vec<u8>, Error> {
let mut lz4_encoder = EncoderBuilder::new().level(compression_level).build(Vec::<u8>::new())?;
std::io::copy(&mut&data[..], &mut lz4_encoder)?;
let (output, result) = lz4_encoder.finish();
match result {
Ok(()) => Ok(output),
Err(error) => Err(Error::from_error(error))
}
}

View File

@ -1,6 +1,7 @@
use std::{boxed::Box, io::{BufReader, BufWriter, Read, Write}, path::PathBuf};
pub mod bits;
pub mod compress;
pub mod raw;
pub type BufferedInputFile = BufReader<std::fs::File>;