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]] [[package]]
name = "rpfixer" name = "rpfixer"
version = "0.3.0" version = "0.2.0"
dependencies = [ dependencies = [
"adler", "adler",
"clap", "clap",

View file

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

View file

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