From ee64c1bb2db3f6a778bb89a2bcfe32bce04749ff Mon Sep 17 00:00:00 2001 From: Sven Assmann Date: Sat, 27 Nov 2021 18:41:58 +0100 Subject: [PATCH] chore(cleanup) (#49) * feat(lto): enable fat link time optimisation * chore(cleanup): clean up unused code * chore(rename): rename struct --- Cargo.toml | 3 + stegano-core/src/media/audio/lsb_codec.rs | 4 +- stegano-core/src/media/image/encoder.rs | 4 +- stegano-core/src/media/image/lsb_codec.rs | 10 +-- stegano-core/src/universal_encoder.rs | 75 ++--------------------- 5 files changed, 14 insertions(+), 82 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 801ed59..f81db5b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,3 +3,6 @@ members = [ "stegano-core", "stegano-cli", ] + +[profile.release] +lto = "fat" \ No newline at end of file diff --git a/stegano-core/src/media/audio/lsb_codec.rs b/stegano-core/src/media/audio/lsb_codec.rs index c315e21..3dc6507 100644 --- a/stegano-core/src/media/audio/lsb_codec.rs +++ b/stegano-core/src/media/audio/lsb_codec.rs @@ -5,7 +5,7 @@ use hound::{WavReader, WavSpec}; use crate::media::audio::wav_iter::{AudioWavIter, AudioWavIterMut}; use crate::universal_decoder::{Decoder, OneBitUnveil}; -use crate::universal_encoder::Encoder2; +use crate::universal_encoder::Encoder; /// convenient wrapper for `WavReader::open` pub fn read_samples(file: &Path) -> (Vec, WavSpec) { @@ -69,7 +69,7 @@ impl LsbCodec { /// .expect("Cannot write to codec"); /// ``` pub fn encoder<'i>(input: &'i mut Vec) -> Box { - Box::new(Encoder2::new(AudioWavIterMut::new(input.iter_mut()))) + Box::new(Encoder::new(AudioWavIterMut::new(input.iter_mut()))) } } diff --git a/stegano-core/src/media/image/encoder.rs b/stegano-core/src/media/image/encoder.rs index f139a30..aa45caa 100644 --- a/stegano-core/src/media/image/encoder.rs +++ b/stegano-core/src/media/image/encoder.rs @@ -12,7 +12,7 @@ use crate::MediaPrimitiveMut; /// use image::{RgbaImage}; /// use stegano_core::universal_decoder::{Decoder, OneBitUnveil}; /// use stegano_core::media::image::encoder::ImagePngMut; -/// use stegano_core::universal_encoder::Encoder2; +/// use stegano_core::universal_encoder::Encoder; /// /// // create a `RgbaImage` from a png image file /// let image_original = image::open("../resources/plain/carrier-image.png") @@ -23,7 +23,7 @@ use crate::MediaPrimitiveMut; /// .to_rgba8(); /// let secret_message = "Hello World!".as_bytes(); /// { -/// let mut encoder = Encoder2::new(ImagePngMut::new(&mut image).into_iter()); +/// let mut encoder = Encoder::new(ImagePngMut::new(&mut image).into_iter()); /// encoder.write_all(secret_message) /// .expect("Cannot write secret message"); /// } diff --git a/stegano-core/src/media/image/lsb_codec.rs b/stegano-core/src/media/image/lsb_codec.rs index f0810e2..2eadfee 100644 --- a/stegano-core/src/media/image/lsb_codec.rs +++ b/stegano-core/src/media/image/lsb_codec.rs @@ -1,7 +1,7 @@ use crate::media::image::decoder::ImagePngSource; use crate::media::image::encoder::ImagePngMut; use crate::universal_decoder::{Decoder, OneBitUnveil}; -use crate::universal_encoder::Encoder2; +use crate::universal_encoder::Encoder; use image::RgbaImage; use std::io::{Read, Write}; @@ -59,12 +59,6 @@ impl LsbCodec { /// assert_eq!(msg, "Hello World!"); /// ``` pub fn encoder<'i>(carrier: &'i mut RgbaImage) -> Box { - Box::new(Encoder2::new(ImagePngMut::new(carrier))) + Box::new(Encoder::new(ImagePngMut::new(carrier))) } } - -#[cfg(test)] -mod audio_e2e_tests { - // use super::*; - // TODO implement them -} diff --git a/stegano-core/src/universal_encoder.rs b/stegano-core/src/universal_encoder.rs index 9bbd900..f037359 100644 --- a/stegano-core/src/universal_encoder.rs +++ b/stegano-core/src/universal_encoder.rs @@ -15,23 +15,24 @@ pub trait HideAlgorithm { fn encode(&self, carrier: T, information: &Result) -> T; } -pub struct Encoder2<'c, C> +/// generic stegano encoder +pub struct Encoder<'c, C> where C: Iterator>, { pub carrier: C, } -impl<'c, C> Encoder2<'c, C> +impl<'c, C> Encoder<'c, C> where C: Iterator>, { pub fn new(carrier: C) -> Self { - Encoder2 { carrier } + Encoder { carrier } } } -impl<'c, C> Write for Encoder2<'c, C> +impl<'c, C> Write for Encoder<'c, C> where C: Iterator>, { @@ -41,20 +42,7 @@ where let mut bit_iter = BitReader::endian(Cursor::new(buf), LittleEndian); let mut bit_written: usize = 0; for s in self.carrier.by_ref().take(items_to_take) { - // let item: CarrierItem = self.algorithm.encode(s, &bit_iter.read_bit()); s.hide_bit(bit_iter.read_bit().unwrap()).unwrap(); - // match s { - // MediaPrimitiveMut::ImageColorChannel(c) => { - // let bit = bit_iter.read_bit().unwrap(); - // *c = (*c & (u8::MAX - 1)) | if bit { 1 } else { 0 }; - // } - // MediaPrimitiveMut::AudioSample(s) => { - // let bit = bit_iter.read_bit().unwrap(); - // *s = (*s & (i16::MAX - 1)) | if bit { 1 } else { 0 }; - // } - // MediaPrimitiveMut::None => {} - // } - // TODO the audio primitive impl is missing bit_written += 1; } @@ -66,59 +54,6 @@ where } } -/// generic stegano encoder -pub struct Encoder -where - I: Iterator, - O: WriteCarrierItem, - A: HideAlgorithm, -{ - pub input: I, - pub output: O, - pub algorithm: A, -} - -/// generic stegano encoder constructor method -impl Encoder -where - I: Iterator, - O: WriteCarrierItem, - A: HideAlgorithm, -{ - pub fn new(input: I, output: O, algorithm: A) -> Self { - Encoder { - input, - output, - algorithm, - } - } -} - -impl Write for Encoder -where - I: Iterator, - O: WriteCarrierItem, - A: HideAlgorithm, -{ - fn write(&mut self, buf: &[u8]) -> Result { - // TODO better let the algorithm determine the density of encoding - let items_to_take = buf.len() << 3; // 1 bit per sample <=> * 8 <=> << 3 - let mut bit_iter = BitReader::endian(Cursor::new(buf), LittleEndian); - let mut bit_written = 0; - for s in self.input.by_ref().take(items_to_take) { - let item: MediaPrimitive = self.algorithm.encode(s, &bit_iter.read_bit()); - self.output.write_carrier_item(&item).unwrap(); - bit_written += 1; - } - - Ok(bit_written >> 3) - } - - fn flush(&mut self) -> Result<()> { - self.output.flush() - } -} - /// default 1 bit hiding strategy pub struct OneBitHide; impl HideAlgorithm for OneBitHide {