Compare commits

..

No commits in common. "98794a6dfc3f92d34f9c49c5317f9bc19248fd88" and "a50b93d0268a1531f6fb03db4f05848e3f8a381a" have entirely different histories.

3 changed files with 30 additions and 28 deletions

2
Cargo.lock generated
View file

@ -171,7 +171,7 @@ dependencies = [
[[package]]
name = "rpfixer"
version = "0.3.0"
version = "0.2.0"
dependencies = [
"adler",
"clap",

View file

@ -1,9 +1,10 @@
use std::{
fmt::Debug,
io::{Read, Write},
fs::File,
io::{BufReader, BufWriter, Read, Write},
usize,
};
use std::io::Cursor;
use std::path::PathBuf;
use adler::Adler32;
use crc32fast::Hasher;
use miniz_oxide::inflate::core::{
@ -37,8 +38,9 @@ impl Chunk {
}
}
pub fn fix(mut bytes: Vec<u8>) -> Vec<u8> {
let mut bufread = Cursor::new(bytes.clone());
pub fn encoder(inpath: PathBuf, outpath: PathBuf) {
let file = File::open(inpath).unwrap();
let mut bufread = BufReader::new(file);
// read the png header
let mut header = [0; 8];
@ -63,11 +65,11 @@ pub fn fix(mut bytes: Vec<u8>) -> Vec<u8> {
bufread.read_exact(&mut crc).unwrap();
let mut chunk = Chunk { kind, data, crc };
// println!("{:?}", chunk);
println!("{:?}", chunk);
// recode the compressed image data
if chunk.kind == *b"IDAT" {
// println!("Decompressing IDAT chunk");
println!("Decompressing IDAT chunk");
let mut decompressor = DecompressorOxide::new();
decompressor.init();
let mut buf = vec![0; 1024 * 1024 * 1024]; // this could probably be smaller
@ -81,10 +83,10 @@ pub fn fix(mut bytes: Vec<u8>) -> Vec<u8> {
| TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF,
);
// println!(
// "Decompressed IDAT chunk status {:?}, bytes read {}, bytes outputted {}",
// data.0, data.1, data.2
// );
println!(
"Decompressed IDAT chunk status {:?}, bytes read {}, bytes outputted {}",
data.0, data.1, data.2
);
let _ = buf.split_off(data.2);
@ -96,7 +98,7 @@ pub fn fix(mut bytes: Vec<u8>) -> Vec<u8> {
// replace the last 4 bytes of the data with the new checksum
let data_len = chunk.data.len();
chunk.data[data_len - 4..].copy_from_slice(&csum);
// println!("Corrected Adler32 checksum");
println!("Corrected Adler32 checksum");
}
let mut hasher = Hasher::new();
@ -105,29 +107,28 @@ pub fn fix(mut bytes: Vec<u8>) -> Vec<u8> {
let checksum = hasher.finalize();
if checksum != u32::from_be_bytes(chunk.crc) {
// println!("CRC error in chunk {:?}", chunk.kind_to_string());
println!("CRC error in chunk {:?}", chunk.kind_to_string());
chunk.crc = checksum.to_be_bytes();
// println!("Corrected CRC");
println!("Corrected CRC");
}
chunks.push(chunk);
}
// new cursor
let mut newcursor = Cursor::new(Vec::new());
let ofile = File::create(outpath).unwrap();
let mut writer = BufWriter::new(ofile);
// write a new header
newcursor
writer
.write_all(&[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])
.unwrap();
for chunk in chunks {
newcursor
writer
.write_all(&u32::to_be_bytes(chunk.data.len() as u32))
.unwrap();
newcursor.write_all(&chunk.kind).unwrap();
newcursor.write_all(&chunk.data).unwrap();
newcursor.write_all(&chunk.crc).unwrap();
writer.write_all(&chunk.kind).unwrap();
writer.write_all(&chunk.data).unwrap();
writer.write_all(&chunk.crc).unwrap();
}
let finalres: Vec<u8> = newcursor.into_inner();
finalres
}

View file

@ -1,8 +1,11 @@
mod idk;
use clap::Parser;
use glob::glob;
use std::{fs::{metadata}, path::{PathBuf}};
use std::fs::{read, write, File};
use std::io::{Read};
use std::{
fs::{metadata},
path::{PathBuf},
};
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
@ -77,8 +80,6 @@ fn main() {
for thatpath in filelist {
println!("Filename: {:?}",thatpath);
let frfr = read(thatpath.to_owned()).expect("wtf the path doesnt exist");
let mut fr = idk::fix(frfr);
write(thatpath, fr).expect("file could not write btw");
idk::encoder(thatpath.to_owned(), thatpath)
}
}