Skip to content

Commit

Permalink
Clean up LZ stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
widberg committed Oct 17, 2023
1 parent fb739be commit 49ce3a4
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 36 deletions.
27 changes: 24 additions & 3 deletions bff-cli/src/lz.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io::{self, Cursor, Read, Write};

use bff::lz::{compress_data_with_header_writer, lz4_compress, lzo_compress};
use bff::Endian;
use bff::lz::{compress_data_with_header_writer, decompress_data_with_header_parser, lz4_compress_data_with_header_writer, lz4_decompress_data_with_header_parser, lzo_compress, lzo_decompress};
use bff::{Endian, BufReader};
use clap::ValueEnum;

use crate::error::BffCliResult;
Expand Down Expand Up @@ -35,10 +35,31 @@ pub fn lz(endian: &LzEndian, algorithm: &LzAlgorithm) -> BffCliResult<()> {
match algorithm {
LzAlgorithm::Lzrs => compress_data_with_header_writer(&buf, &mut writer, endian)?,
LzAlgorithm::Lzo => lzo_compress(&buf, &mut writer, endian)?,
LzAlgorithm::Lz4 => lz4_compress(&buf, &mut writer, endian)?,
LzAlgorithm::Lz4 => lz4_compress_data_with_header_writer(&buf, &mut writer, endian)?,
};

let stdout = io::stdout();
stdout.lock().write_all(writer.into_inner())?;
Ok(())
}

pub fn unlz(endian: &LzEndian, algorithm: &LzAlgorithm) -> BffCliResult<()> {
let endian = match endian {
LzEndian::Big => Endian::Big,
LzEndian::Little => Endian::Little,
};

let stdin = io::stdin();
let mut buf: Vec<u8> = Vec::new();
stdin.lock().read_to_end(&mut buf)?;
let mut reader = BufReader::new(Cursor::new(buf));

let decompressed = match algorithm {
LzAlgorithm::Lzrs => decompress_data_with_header_parser(&mut reader, endian)?,
LzAlgorithm::Lzo => lzo_decompress(&mut reader, endian)?,
LzAlgorithm::Lz4 => lz4_decompress_data_with_header_parser(&mut reader, endian)?,
};

let stdout = io::stdout();
Ok(stdout.lock().write_all(&decompressed)?)
}
10 changes: 8 additions & 2 deletions bff-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod info;
mod lz;
mod reverse_crc32;
mod round_trip;
mod unlz;
mod psc;

#[derive(Subcommand)]
enum Commands {
Expand Down Expand Up @@ -89,6 +89,11 @@ enum Commands {
algorithm: LzAlgorithm,
},
Csc {},
#[clap(alias = "xpsc")]
ExtractPsc {
psc: PathBuf,
directory: PathBuf,
},
}

#[derive(Parser)]
Expand Down Expand Up @@ -116,7 +121,7 @@ fn main() -> BffCliResult<()> {
mode,
format,
} => crc::crc(string, starting, algorithm, mode, format),
Commands::Unlz { endian, algorithm } => unlz::unlz(endian, algorithm),
Commands::Unlz { endian, algorithm } => lz::unlz(endian, algorithm),
Commands::Lz { endian, algorithm } => lz::lz(endian, algorithm),
Commands::ReverseCrc32 {
string,
Expand All @@ -135,5 +140,6 @@ fn main() -> BffCliResult<()> {
),
Commands::RoundTrip { bigfile } => round_trip::round_trip(bigfile),
Commands::Csc {} => csc::csc(),
Commands::ExtractPsc { psc, directory } => psc::extract_psc(psc, directory),
}
}
9 changes: 9 additions & 0 deletions bff-cli/src/psc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::path::Path;
use crate::error::BffCliResult;

pub fn extract_psc(
_psc: &Path,
_directory: &Path,
) -> BffCliResult<()> {
todo!()
}
28 changes: 0 additions & 28 deletions bff-cli/src/unlz.rs

This file was deleted.

48 changes: 45 additions & 3 deletions bff/src/lz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cmp::{max, min};
use std::io::{Read, Seek, SeekFrom, Write};
use std::ptr::null_mut;

use binrw::{BinReaderExt, BinResult, BinWriterExt};
use binrw::{BinRead, BinReaderExt, BinResult, BinWrite, BinWriterExt};
use lz4::{Decoder, EncoderBuilder};
use minilzo_rs::LZO;

Expand Down Expand Up @@ -63,6 +63,8 @@ pub fn decompress_data_parser(decompressed_size: u32, _compressed_size: u32) ->
let start = decompressed_buffer.len() - (temp & temp_mask) as usize - 1;
let end = start + (temp >> temp_shift) as usize + 3;

// We don't want to use `extend_from_within` here because it requires the entire
// range to exist at the time of the call
for i in start..end {
decompressed_buffer.push(decompressed_buffer[i]);
}
Expand Down Expand Up @@ -415,15 +417,55 @@ pub fn lzo_decompress<R: Read>(reader: &mut R, _endian: Endian) -> BffResult<Vec
Ok(decompressed)
}

pub fn lz4_compress<W: Write>(data: &[u8], writer: &mut W, _endian: Endian) -> BffResult<()> {
#[binrw::writer(writer, endian)]
pub fn lz4_compress_data_with_header_writer_internal(data: &[u8]) -> BinResult<()> {
let begin = writer.stream_position()?;
writer.seek(SeekFrom::Current(8))?;
let mut encoder = EncoderBuilder::new().build(writer)?;
encoder.write_all(data)?;
let (writer, result) = encoder.finish();
result?;
let end = writer.stream_position()?;

writer.seek(SeekFrom::Start(begin))?;
(data.len() as u32).write_options(writer, endian, ())?;
((end - begin - 8) as u32).write_options(writer, endian, ())?;
writer.seek(SeekFrom::Start(end))?;
Ok(())
}

pub fn lz4_decompress<R: Read>(reader: &mut R, _endian: Endian) -> BffResult<Vec<u8>> {
pub fn lz4_compress_data_with_header_writer<W: Write + Seek>(
data: &[u8],
writer: &mut W,
endian: Endian,
) -> BffResult<()> {
Ok(lz4_compress_data_with_header_writer_internal(
data,
writer,
endian,
(),
)?)
}

#[binrw::parser(reader, endian)]
pub fn lz4_decompress_data_with_header_parser_internal() -> BinResult<Vec<u8>> {
let _decompressed_size = u32::read_options(reader, endian, ())?;
let _compressed_size = u32::read_options(reader, endian, ())?;
let mut decoder = Decoder::new(reader)?;
let mut decompressed: Vec<u8> = Vec::new();
decoder.read_to_end(&mut decompressed)?;
let (_reader, result) = decoder.finish();
result?;
Ok(decompressed)
}

pub fn lz4_decompress_data_with_header_parser<R: Read + Seek>(
reader: &mut R,
endian: Endian,
) -> BffResult<Vec<u8>> {
Ok(lz4_decompress_data_with_header_parser_internal(
reader,
endian,
(),
)?)
}

0 comments on commit 49ce3a4

Please sign in to comment.