test compression is now working. you can compress a test and decompress it.
needs to be rolled into the test util suite
This commit is contained in:
@@ -1,13 +1,36 @@
|
||||
/// TestCompression
|
||||
///
|
||||
/// Utility for [de]compressing tests for Gemma
|
||||
///
|
||||
/// Usage: testcompression <COMMAND>
|
||||
//
|
||||
// Commands:
|
||||
// compress Compress the input string
|
||||
// decompress Decompress the input string (must be compressed format)
|
||||
// help Print this message or the help of the given subcommand(s)
|
||||
//
|
||||
// Options:
|
||||
// -h, --help Print help
|
||||
// -V, --version Print version
|
||||
///
|
||||
/// Limitations:
|
||||
/// - Uncompressed files must only contain printable ASCII characters
|
||||
/// - Stuff I don't know about yet
|
||||
|
||||
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use clap::{Parser, Subcommand};
|
||||
use flate2::{write::{GzEncoder, GzDecoder}, Compression};
|
||||
use std::path::PathBuf;
|
||||
use std::io::prelude::*;
|
||||
use std::io::Read;
|
||||
use std::io::Write;
|
||||
use flate2::write::ZlibEncoder;
|
||||
use gemma::constants::TEST_ROM_ROOT;
|
||||
use flate2::Compression;
|
||||
use flate2::write::{GzEncoder, ZlibEncoder};
|
||||
use gemma::constants::{TESTS_ROOT, TEST_ROM_ROOT};
|
||||
use flate2::read::{DeflateDecoder, DeflateEncoder, GzDecoder};
|
||||
use std::io::prelude::*;
|
||||
use std::io;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about = "Compress or decompress a string", long_about = None)]
|
||||
@@ -19,67 +42,46 @@ struct Cli {
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
/// Compress the input string
|
||||
/// Compress the input file
|
||||
Compress {
|
||||
/// The string to compress
|
||||
/// The string to compress, in plain text
|
||||
input: PathBuf,
|
||||
},
|
||||
/// Decompress the input string (must be compressed format)
|
||||
/// Decompress the input file
|
||||
Decompress {
|
||||
/// The compressed string to decompress, in hex format
|
||||
input: PathBuf,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Compresses raw binary data using Gzip and returns the compressed bytes.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `data` - A `Vec<u8>` containing the uncompressed binary data.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A `Vec<u8>` with the compressed data. Panics if compression fails.
|
||||
pub fn compress_data(data: Vec<u8>) -> Vec<u8> {
|
||||
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
|
||||
encoder
|
||||
.write_all(&data)
|
||||
.expect("Failed to write data to encoder");
|
||||
encoder.finish().expect("Failed to finish compression")
|
||||
}
|
||||
|
||||
/// Decompresses Gzip-compressed binary data and returns the uncompressed bytes.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `data` - A `Vec<u8>` containing Gzip-compressed data.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A `Vec<u8>` with the decompressed data. Panics if decompression fails.
|
||||
// pub fn decompress_data(data: Vec<u8>) -> Vec<u8> {
|
||||
// let mut decoder = GzDecoder::new( &data[..]);
|
||||
// let mut decompressed = Vec::new();
|
||||
// decoder.write_all(&mut decompressed)
|
||||
// .expect("Failed to decompress data");
|
||||
// decompressed.clone().to_vec()
|
||||
// }
|
||||
|
||||
fn compress_file(input_path: &PathBuf) {
|
||||
let target_file_name = format!("{}.tflt", input_path.display());
|
||||
println!("TARGET_FILE_NAME: {}", target_file_name);
|
||||
let input_data = fs::read(input_path).expect("Failed to read input file");
|
||||
let file = File::open(&input_path).unwrap();
|
||||
let mut compressed_bytes= Vec::new();
|
||||
|
||||
let compressed_data = compress_data(input_data);
|
||||
let target_file = File::create(target_file_name);
|
||||
target_file.unwrap().write_all(&compressed_data).unwrap()
|
||||
let mut deflater = DeflateEncoder::new(file, Compression::fast());
|
||||
deflater.read_to_end(&mut compressed_bytes).expect("Unable to write compressed data to buffer ");
|
||||
|
||||
let output_filename = format!("{}.deflated", input_path.display());
|
||||
let output_file = File::create(&output_filename);
|
||||
output_file.unwrap().write_all(&compressed_bytes).expect("Unable to write compressed version");
|
||||
}
|
||||
|
||||
fn decompress_file(input_path: &PathBuf) {
|
||||
let target_file_name = format!("{}.uncompressed", input_path.display());
|
||||
let file = File::open(&input_path).unwrap();
|
||||
let mut uncompressed_bytes = Vec::new();
|
||||
|
||||
let mut inflater = DeflateDecoder::new(file);
|
||||
inflater.read_to_end(&mut uncompressed_bytes).expect("Unable to inflate.");
|
||||
|
||||
let target_file_name = format!("{}.inflated", input_path.display());
|
||||
println!("Writing decompressed data to {}", target_file_name);
|
||||
let input_data = fs::read(input_path).expect("Failed to read compressed file.");
|
||||
|
||||
let mut target_file = File::create(&target_file_name)
|
||||
.expect("Unable to create uncompressed output file");
|
||||
target_file
|
||||
.write_all(&uncompressed_bytes)
|
||||
.expect("Unable to write decompressed file");
|
||||
|
||||
// let decompressed_data = decompress_data(input_data);
|
||||
// let mut target_file = File::create(&target_file_name).expect(format!("Unable to create uncompressed file -> {}", target_file_name.clone()).as_str());
|
||||
//target_file.write_all(&decompressed_data).expect(format!("Unable to write uncompressed file -> {}", target_file_name).as_str());
|
||||
@@ -87,21 +89,19 @@ fn decompress_file(input_path: &PathBuf) {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let filename = format!("{}/gemma/{}/2-ibm-logo.ch8", std::env::current_dir().unwrap().display(), TEST_ROM_ROOT);
|
||||
println!("READING {} from {}", filename, std::env::current_dir().unwrap().display());
|
||||
let mut file = File::open(filename).unwrap();
|
||||
let mut file_data = Vec::new();
|
||||
file.read_to_end(&mut file_data).expect("unable to read rom");
|
||||
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
||||
let raw_file = format!("{}/gemma/{}test_scroll_down_10_hd.asc", std::env::current_dir().unwrap().display(), TESTS_ROOT);
|
||||
let compressed_file = format!("{}.deflated", &raw_file);
|
||||
let uncompressed_file = format!("{}.inflated", &compressed_file);
|
||||
println!("[ + ] Writing compressed file from {}", &raw_file);
|
||||
compress_file(&raw_file.clone().into());
|
||||
|
||||
e.write_all(b"foo");
|
||||
e.write_all(b"bar");
|
||||
let compressed_bytes = e.finish();
|
||||
decompress_file(&compressed_file.into());
|
||||
///////// COMPRESSION COMPLETE. TIME TO DECOMPRESS AND COMPARE
|
||||
|
||||
println!("COMPRESSED: {compressed_bytes:?}");
|
||||
//
|
||||
// let cli = Cli::parse();
|
||||
//
|
||||
//
|
||||
// match cli.command {
|
||||
// Commands::Compress { input } => {
|
||||
|
||||
Reference in New Issue
Block a user