Skip to content

Commit

Permalink
chore(cleanup) (#49)
Browse files Browse the repository at this point in the history
* feat(lto): enable fat link time optimisation
* chore(cleanup): clean up unused code
* chore(rename): rename struct
  • Loading branch information
sassman authored Nov 27, 2021
1 parent 1749b39 commit ee64c1b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 82 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ members = [
"stegano-core",
"stegano-cli",
]

[profile.release]
lto = "fat"
4 changes: 2 additions & 2 deletions stegano-core/src/media/audio/lsb_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i16>, WavSpec) {
Expand Down Expand Up @@ -69,7 +69,7 @@ impl LsbCodec {
/// .expect("Cannot write to codec");
/// ```
pub fn encoder<'i>(input: &'i mut Vec<i16>) -> Box<dyn Write + 'i> {
Box::new(Encoder2::new(AudioWavIterMut::new(input.iter_mut())))
Box::new(Encoder::new(AudioWavIterMut::new(input.iter_mut())))
}
}

Expand Down
4 changes: 2 additions & 2 deletions stegano-core/src/media/image/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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");
/// }
Expand Down
10 changes: 2 additions & 8 deletions stegano-core/src/media/image/lsb_codec.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -59,12 +59,6 @@ impl LsbCodec {
/// assert_eq!(msg, "Hello World!");
/// ```
pub fn encoder<'i>(carrier: &'i mut RgbaImage) -> Box<dyn Write + 'i> {
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
}
75 changes: 5 additions & 70 deletions stegano-core/src/universal_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@ pub trait HideAlgorithm<T> {
fn encode(&self, carrier: T, information: &Result<bool>) -> T;
}

pub struct Encoder2<'c, C>
/// generic stegano encoder
pub struct Encoder<'c, C>
where
C: Iterator<Item = MediaPrimitiveMut<'c>>,
{
pub carrier: C,
}

impl<'c, C> Encoder2<'c, C>
impl<'c, C> Encoder<'c, C>
where
C: Iterator<Item = MediaPrimitiveMut<'c>>,
{
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<Item = MediaPrimitiveMut<'c>>,
{
Expand All @@ -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;
}

Expand All @@ -66,59 +54,6 @@ where
}
}

/// generic stegano encoder
pub struct Encoder<I, O, A>
where
I: Iterator<Item = MediaPrimitive>,
O: WriteCarrierItem,
A: HideAlgorithm<MediaPrimitive>,
{
pub input: I,
pub output: O,
pub algorithm: A,
}

/// generic stegano encoder constructor method
impl<I, O, A> Encoder<I, O, A>
where
I: Iterator<Item = MediaPrimitive>,
O: WriteCarrierItem,
A: HideAlgorithm<MediaPrimitive>,
{
pub fn new(input: I, output: O, algorithm: A) -> Self {
Encoder {
input,
output,
algorithm,
}
}
}

impl<I, O, A> Write for Encoder<I, O, A>
where
I: Iterator<Item = MediaPrimitive>,
O: WriteCarrierItem,
A: HideAlgorithm<MediaPrimitive>,
{
fn write(&mut self, buf: &[u8]) -> Result<usize> {
// 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<MediaPrimitive> for OneBitHide {
Expand Down

0 comments on commit ee64c1b

Please sign in to comment.