1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use std::{error::Error, fs::File, io::Read, sync::mpsc::channel};
use clap::Clap;
use log::{info, trace};
use crate::{
binary::BitIterator,
context::PivotByLineContext,
encoder::Encoder,
method::complex::{eluv::ELUVMethod, extended_line::ExtendedLineMethod},
};
use super::{
progress::{new_progress_bar, spawn_progress_thread, ProgressStatus},
writer::Writer,
};
#[derive(Clap)]
pub struct EncodeSubCommand {
#[clap(short, long)]
cover: String,
#[clap(short, long)]
data: String,
#[clap(long)]
pivot: Option<usize>,
#[clap(long, group = "method_args")]
eluv: bool,
#[clap(long = "eline", group = "method_args")]
#[allow(dead_code)]
extended_line: bool,
}
impl EncodeSubCommand {
pub fn run(&self) -> Result<Vec<u8>, Box<dyn Error>> {
let cover_file_input = File::open(&self.cover)?;
let data_file_input = File::open(&self.data)?;
self.do_encode(cover_file_input, data_file_input)
}
pub(crate) fn do_encode(
&self,
mut cover_input: impl Read,
mut data_input: impl Read,
) -> Result<Vec<u8>, Box<dyn Error>> {
let mut cover_text = String::new();
let mut data = vec![];
cover_input.read_to_string(&mut cover_text)?;
data_input.read_to_end(&mut data)?;
trace!("text: {:?}", data);
let pivot = pick_pivot_from(
self.pivot,
determine_pivot_size(cover_text.split_whitespace()),
)?;
let capacity_msg = format!(
"Required cover text capacity: {}",
BitIterator::new(&data).count()
);
Writer::warn(&capacity_msg);
info!("Encoding secret data");
let mut data_iterator = BitIterator::new(&data);
let progress_bar = new_progress_bar(data_iterator.count() as u64);
let (tx, rx) = channel::<ProgressStatus>();
progress_bar.set_message("Encoding..");
spawn_progress_thread(progress_bar.clone(), rx);
let method = self.get_method();
let mut context = PivotByLineContext::new(&cover_text, pivot);
let stego_result = method.encode(&mut context, &mut data_iterator, Some(&tx));
tx.send(ProgressStatus::Finished).ok();
progress_bar.finish_with_message("Finished encoding");
Ok(stego_result?.as_bytes().into())
}
pub(crate) fn get_method(&self) -> Box<dyn Encoder<PivotByLineContext>> {
if self.eluv {
Box::new(ELUVMethod::default())
} else {
Box::new(ExtendedLineMethod::default())
}
}
}
pub(crate) fn determine_pivot_size<'a>(words: impl Iterator<Item = &'a str>) -> usize {
words
.into_iter()
.map(|string| string.chars().count() + 1)
.max()
.unwrap_or(0)
}
pub(crate) fn pick_pivot_from(
user_pivot: Option<usize>,
calculated_pivot: usize,
) -> Result<usize, Box<dyn Error>> {
Ok(if let Some(value) = user_pivot {
if value < calculated_pivot {
return Err("Provided pivot is smaller than the largest word in text! Cannot guarantee encoding will succeed.".into());
}
Writer::info(&format!("Using user provided pivot: {}", value));
value
} else {
Writer::info(&format!(
"Using pivot based on the cover text: {}",
calculated_pivot
));
calculated_pivot
})
}
#[allow(unused_imports)]
mod test {
use std::{error::Error, io::Read};
use super::EncodeSubCommand;
#[test]
fn fails_when_there_is_not_enough_cover_text() -> Result<(), Box<dyn Error>> {
let cover_input = "a b c ".repeat(2);
let data_input: Vec<u8> = vec![0b11111111];
let command = EncodeSubCommand {
cover: "stub".into(),
data: "stub".into(),
pivot: Some(3),
eluv: false,
extended_line: true,
};
let result = command.do_encode(cover_input.as_bytes(), data_input.as_slice());
assert!(result.is_err());
Ok(())
}
#[test]
fn fails_when_pivot_is_too_small() -> Result<(), Box<dyn Error>> {
let cover_input = "aaaaa ".repeat(2);
let data_input: Vec<u8> = vec![0b11111111];
let command = EncodeSubCommand {
cover: "stub".into(),
data: "stub".into(),
pivot: Some(3),
eluv: false,
extended_line: true,
};
let result = command.do_encode(cover_input.as_bytes(), data_input.as_slice());
assert!(result.is_err());
Ok(())
}
}