Skip to content

Commit

Permalink
Syntax clean up, remove the macros since those are no longer needed h…
Browse files Browse the repository at this point in the history
…ere.
  • Loading branch information
sciguyryan committed Jan 22, 2025
1 parent bfe2a19 commit 3a61a46
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 130 deletions.
19 changes: 0 additions & 19 deletions psistega3-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,6 @@ enum ActionType {
fn main() {
SimpleLogger::new().init().unwrap();

use std::time::Instant;
let now = Instant::now();

{
for _ in 0..50 {
let mut s = StegaV1::new("aaaa");
let _ = s.encode(
"D:\\Pictures\\Jeff's Wallpapers\\RJ 1.png",
"aaaaaa".to_string(),
"aaaaaaaaaa",
"D:\\Temp\\output.png",
);
}
}

let elapsed = now.elapsed();
println!("Elapsed: {:.2?}", elapsed / 50);
return;

let mut args: Vec<String> = env::args().collect();
if args.len() == 1 {
show_help();
Expand Down
27 changes: 11 additions & 16 deletions psistega3-core/src/codecs/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{
image_wrapper::ImageWrapper,
locker::Locker,
logger::Logger,
macros::unwrap_res_or_return,
utilities::{png_utils::PngChunkType, *},
};

Expand Down Expand Up @@ -143,10 +142,11 @@ impl StegaV1 {
// the SHA3-512 has of the file here.
let mut enc_hash = vec![];
if self.is_file_locker_enabled() {
enc_hash = unwrap_res_or_return!(
hashers::sha3_512_file(encoded_img_path),
Err(Error::FileHashingError)
);
if let Ok(v) = hashers::sha3_512_file(encoded_img_path) {
enc_hash = v;
} else {
return Err(Error::FileHashingError);
}

// The first thing we need to do is to check whether the file hash
// exists within the locker file index.
Expand Down Expand Up @@ -354,10 +354,9 @@ impl StegaV1 {

// We will convert the input data byte vector into a base64 string.
let plaintext = misc_utils::encode_u8_slice_to_base64_str(data);
let ct_bytes = unwrap_res_or_return!(
cipher.encrypt(nonce, plaintext.as_bytes()),
Err(Error::EncryptionFailed)
);
let Ok(ct_bytes) = cipher.encrypt(nonce, plaintext.as_bytes()) else {
return Err(Error::EncryptionFailed);
};

/*
4 cells for the total number of stored cipher-text cells,
Expand All @@ -377,7 +376,7 @@ impl StegaV1 {
let total_cells_needed = (4 /* number of cipher-text cells (u32) */
+ 12 /* the length of the Argon2 salt (12 * u8) */
+ 12 /* the length of the AES-256 nonce (12 * u8) */
+ ct_bytes.len() as usize)
+ ct_bytes.len())
* 2; /* 2 subcells per cell */

// In total we can never store more than 0xFFFFFFFF bytes of data to
Expand Down Expand Up @@ -582,17 +581,13 @@ impl StegaV1 {
/// * `path` - The path to the PNG file.
///
pub fn process_bkgd_chunk(&mut self, path: &str) -> bool {
let chunk = if let Some(c) = png_utils::read_chunk_raw(path, PngChunkType::Bkgd) {
c
} else {
let Some(chunk) = png_utils::read_chunk_raw(path, PngChunkType::Bkgd) else {
// This is an error as we should always have a bKGD chunk.
return false;
};

// We have a bKGD chunk to process!
let data = if let Some(d) = png_utils::get_chunk_data(&chunk) {
d
} else {
let Some(data) = png_utils::get_chunk_data(&chunk) else {
// This is an error as we should always have a bKGD chunk.
return false;
};
Expand Down
28 changes: 15 additions & 13 deletions psistega3-core/src/hashers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
error::{Error, Result},
macros::*,
};
use crate::error::{Error, Result};

use argon2::Argon2;
use memmap2::Mmap;
Expand Down Expand Up @@ -29,9 +26,7 @@ pub fn argon2_string(
version: argon2::Version,
) -> Result<[u8; 128]> {
// Return an error if any of supplied parameters are incorrect.
let params = if let Ok(p) = argon2::Params::new(m_cost, p_cost, t_cost, None) {
p
} else {
let Ok(params) = argon2::Params::new(m_cost, p_cost, t_cost, None) else {
return Err(Error::Argon2InvalidParams);
};

Expand All @@ -40,10 +35,9 @@ pub fn argon2_string(

// Nom!
let mut hashed_bytes = [0u8; 128];
unwrap_res_or_return!(
hasher.hash_password_into(key_bytes, &salt, &mut hashed_bytes),
Err(Error::Argon2NoHash)
);
let Ok(_) = hasher.hash_password_into(key_bytes, &salt, &mut hashed_bytes) else {
return Err(Error::Argon2NoHash);
};

Ok(hashed_bytes)
}
Expand All @@ -69,11 +63,19 @@ pub fn crc32_slice(slice: &[u8]) -> u32 {
///
#[inline]
pub fn sha3_512_file(path: &str) -> Result<Vec<u8>> {
let file = unwrap_res_or_return!(File::open(path), Err(Error::FileHashingError));
let Ok(file) = File::open(path) else {
return Err(Error::FileHashingError);
};

// Create a read-only memory map of the file as it should improve
// the performance of this function.
let mmap = unsafe { unwrap_res_or_return!(Mmap::map(&file), Err(Error::FileHashingError)) };
let mmap = unsafe {
if let Ok(m) = Mmap::map(&file) {
m
} else {
return Err(Error::FileHashingError);
}
};

let mut hasher = Sha3_512::new();
for c in mmap.chunks(16 * 1024) {
Expand Down
9 changes: 4 additions & 5 deletions psistega3-core/src/image_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
error::{Error, Result},
macros::*,
};
use crate::error::{Error, Result};

use image::{ColorType, ImageFormat};

Expand Down Expand Up @@ -115,7 +112,9 @@ impl ImageWrapper {
return Err(Error::PathInvalid);
}

let image = unwrap_res_or_return!(image::open(path), Err(Error::ImageOpening));
let Ok(image) = image::open(path) else {
return Err(Error::ImageOpening);
};

let colour_type = match &image {
ImageLuma8(_) => ColorType::L8,
Expand Down
1 change: 0 additions & 1 deletion psistega3-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ mod hashers;
mod image_wrapper;
mod locker;
mod logger;
mod macros;
pub mod utilities;
pub mod version;
22 changes: 16 additions & 6 deletions psistega3-core/src/locker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::{

use crate::{
error::*,
macros::*,
utilities::{file_utils, png_utils},
};

Expand Down Expand Up @@ -253,7 +252,9 @@ impl Locker {
// Now we need to ensure that the file can never be decoded.
// This will happen regardless of whether the image ever contained
// encoded data or not.
let mut img = unwrap_res_or_return!(ImageWrapper::load_from_file(path, false), false);
let Ok(mut img) = ImageWrapper::load_from_file(path, false) else {
return false;
};

// Scramble the image.
img.scramble();
Expand Down Expand Up @@ -306,7 +307,9 @@ impl Locker {
}

// The file will automatically be closed when it goes out of scope.
let mut file = unwrap_res_or_return!(File::open(path), Err(Error::LockerFileRead));
let Ok(mut file) = File::open(path) else {
return Err(Error::LockerFileRead);
};

// This will hold the chunk of data that is being read.
let mut buffer = [0u8; Locker::ENTRY_SIZE];
Expand Down Expand Up @@ -402,7 +405,10 @@ impl Drop for Locker {
// If we are running a test, most of the time we will want to clear
// the locker data file upon exit.
if self.clear_on_exit {
let locker_path = unwrap_res_or_return!(self.get_locker_file_path());
let Ok(locker_path) = self.get_locker_file_path() else {
return;
};

if file_utils::path_exists(&locker_path) {
// We will ignore any errors here as there is nothing
// that can be done to delete the file.
Expand All @@ -414,8 +420,12 @@ impl Drop for Locker {

// If the file is read-only then we need to unset that
// option, otherwise we will be unable to write to the file.
let locker_path = unwrap_res_or_return!(self.get_locker_file_path());
let state = unwrap_res_or_return!(file_utils::get_file_read_only_state(&locker_path));
let Ok(locker_path) = self.get_locker_file_path() else {
return;
};
let Ok(state) = file_utils::get_file_read_only_state(&locker_path) else {
return;
};
if state {
let _ = file_utils::toggle_file_read_only_state(&locker_path);
}
Expand Down
14 changes: 0 additions & 14 deletions psistega3-core/src/macros.rs

This file was deleted.

Loading

0 comments on commit 3a61a46

Please sign in to comment.