Skip to content

Commit

Permalink
Allow skipping the version checks when using the version guessing sys…
Browse files Browse the repository at this point in the history
…tem. The files will simply not decode if the version information is not correct.
  • Loading branch information
sciguyryan committed Jan 29, 2025
1 parent 0925bc2 commit 5636e1a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
6 changes: 6 additions & 0 deletions psistega3-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ fn main() {
// Attempt to extract the codec version number.
// No default codec needs to be implemented here as the statement below
// will always yield a valid codec.
let mut skip_version_checks = false;
let mut codec: Box<dyn Codec>;
if needs_codec {
let mut codec_version: Option<Version> = None;
Expand All @@ -106,6 +107,8 @@ fn main() {
} else {
show_abort_message(Error::VersionGuessingFailed);
}

skip_version_checks = true;
}

if let Ok(v) = version.parse::<u8>() {
Expand All @@ -129,6 +132,9 @@ fn main() {
codec = Box::<StegaV2>::default();
}

// When using the version guessing system, the checks are skipped.
codec.set_config_state(Config::SkipVersionChecks, skip_version_checks);

// Execute the requested action with the provided arguments.
let result = match action.as_str() {
"-e" | "-encrypt" => handle_encode(&args[4..], &mut codec),
Expand Down
14 changes: 9 additions & 5 deletions psistega3-core/src/codecs/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,26 @@ pub trait Codec {
pub enum Config {
/// Enable or disable the noise map.
///
/// Applicable to: v1.
/// Applicable to: v1, v2.
NoiseLayer,
/// Enable or disable verbose mode.
///
/// Applicable to: v1.
/// Applicable to: v1, v2.
Verbose,
/// Enable or disable the saving of files when encoding or decoding.
///
/// Applicable to: v1.
/// Applicable to: v1, v2.
OutputFiles,
/// Enable or disable the file access locking system for this file.
///
/// Applicable to: v1.
/// Applicable to: v1, v2.
Locker,
/// Enable or disable the single-read locker system.
///
/// Applicable to: v1.
/// Applicable to: v1, v2.
ReadOnce,
/// Enable or disable version checking.
///
/// Applicable to: v2. This is not applicable to v1 as checks are never performed.
SkipVersionChecks,
}
1 change: 1 addition & 0 deletions psistega3-core/src/codecs/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,7 @@ impl Codec for StegaV1 {
Config::ReadOnce => {
self.set_feature_flag_state(1, state);
}
Config::SkipVersionChecks => {}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions psistega3-core/src/codecs/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub struct StegaV2 {
noise_layer: bool,
/// If the resulting image file should be saved when encoding.
output_files: bool,
/// Should we skip version checks?
skip_version_checks: bool,
/// Flags for use when encoding and decoding.
/// Bit 0 indicates that the file locker is to be used with this file.
/// Bit 1 indicates that the file is read-once.
Expand All @@ -65,6 +67,7 @@ impl StegaV2 {
data_cell_map: HashMap::new(),
noise_layer: true,
output_files: true,
skip_version_checks: false,
flags: 0,
locker,
logger: Logger::new(false),
Expand Down Expand Up @@ -597,6 +600,11 @@ impl StegaV2 {
return false;
}

if !self.skip_version_checks && data[5] != CODED_VERSION {
// The codec version does not match.
return false;
}

// The 1st bit are be stored in byte 1.
// Bit 1 stores the flag indicating whether the file locker should be
// used with this file.
Expand Down Expand Up @@ -881,6 +889,9 @@ impl Codec for StegaV2 {
Config::ReadOnce => {
self.set_feature_flag_state(1, state);
}
Config::SkipVersionChecks => {
self.skip_version_checks = state;
}
}
}
}
Expand Down Expand Up @@ -1149,6 +1160,7 @@ mod tests_encode_decode {
data_cell_map: HashMap::new(),
noise_layer: false, // We do not need this here.
output_files: true,
skip_version_checks: false,
flags: 0,
locker,
logger: Logger::new(false),
Expand Down

0 comments on commit 5636e1a

Please sign in to comment.