commit a600f6ad1f51b93db38f08f725da91bc1742a520 Author: Unbewohnte Date: Sun Mar 13 12:34:50 2022 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40d9aca --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/.idea \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..fbf6f1e --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "pngrip" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a873f22 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "pngrip" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3af9b3f --- /dev/null +++ b/LICENSE @@ -0,0 +1,10 @@ +The MIT License (MIT) + +Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..3dc78ba --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# PNGRIP +## A tool to extract embedded PNG images from files + +- This Visual Novel doesn`t encrypt its images ?! - *(evil laugh) +- This god forsaken image-archive format doesn`t have a documentation ?! - *(evil laugh) +- This 25 years old game has some cool sprites, but they are embedded in the executable ?! - *(evil laugh) + + +## Usage +pngrip [DESTINATION] [PATH]... + +where DESTINATION is the path to the existing or non-existing directory where ripped images will be delivered + +and + +PATH... are paths to the files to rip images from + + +## Examples +- pngrip ripped_images bgimages.xp3 data.xp3 +- pngrip \\Pictures \\some\\vn.exe \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..9d74d68 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,136 @@ +/* +The MIT License (MIT) + +Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +use std::io::{Read, Write}; +use std::path::Path; + +fn main() { + let args: Vec = std::env::args().collect(); + if args.len() < 3 { + println!("USAGE: pngrip [DESTINATION] [PATH]..."); + std::process::exit(0); + } + + // handle DESTINATION argument + let mut destination = Path::new(&args[1]); + match destination.exists() { + true => { + if !destination.is_dir() { + // destination exists, but is not a directory + destination = Path::new("."); + eprintln!("[ERROR] Provided destination path is not a directory ! Saving to current directory..."); + } + } + false => { + // destination does not exist, create it + match std::fs::create_dir_all(&destination) { + Ok(_) => {} + Err(e) => { + destination = Path::new("."); + eprintln!("[ERROR] Could not create destination directory: {}. Saving to current directory...", e); + } + } + } + } + + + // go through all files and try to rip all PNGs + let png_identifier: [u8; 8] = [137, 80, 78, 71, 13, 10, 26, 10]; + let iend_identifier: [u8; 4] = [73, 69, 78, 68]; + for file_to_check in &args[2..] { + let path = Path::new(file_to_check); + if !path.exists() { + eprintln!("[ERROR] \"{}\" does not exist", file_to_check); + continue; + } + + let filename; + match path.file_name() { + Some(name) => { + filename = name.to_string_lossy(); + } + None => { + eprintln!("[ERROR] Could not get filename from \"{}\"", file_to_check); + continue; + } + } + + println!("[INFO] Ripping PNGs from \"{}\"...", filename); + + let mut file; + match std::fs::File::open(path) { + Ok(f) => { + file = f; + } + + Err(e) => { + eprintln!("[ERROR] On opening \"{}\": {}", filename, e); + continue; + } + } + + let mut file_bytes: Vec = Vec::new(); + match file.read_to_end(&mut file_bytes) { + Ok(_) => {} + Err(e) => { + eprintln!("[ERROR] On reading \"{}\": {}", filename, e); + continue; + } + } + + let mut image_count: u64 = 0; + + let mut start_pos: usize = 0; + let mut last_pos: usize = 0; + let mut end_pos: usize = 0; + for i in 0..file_bytes.len()-8 { + + if file_bytes[i..i + png_identifier.len()] == png_identifier { + start_pos = i; + } + + if file_bytes[i..i + iend_identifier.len()] == iend_identifier { + end_pos = i + iend_identifier.len(); + } + + if start_pos < end_pos && start_pos != last_pos { + last_pos = start_pos; + image_count += 1; + // println!("[INFO] Found PNG at {}->{} ({} bytes)", start_pos, end_pos, end_pos - start_pos); + + let mut ripped_image_file; + let ripped_image_filename = format!("{}_{}.png", filename, image_count); + match std::fs::File::create(destination.join(&ripped_image_filename)) { + Ok(f) => { + ripped_image_file = f; + } + Err(e) => { + eprintln!("[ERROR] On creating \"{}\": {}", &ripped_image_filename, e); + continue; + } + } + + match ripped_image_file.write_all(&mut file_bytes[start_pos..end_pos]) { + Ok(_) => {} + Err(e) => { + eprintln!("[ERROR] On writing to \"{}\": {}", ripped_image_filename, e); + continue; + } + } + } + } + + + println!("[INFO] Ripped {} images in total", image_count); + } + +} \ No newline at end of file