-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathEmbedder.cc
284 lines (244 loc) · 8.13 KB
/
Embedder.cc
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* steghide 0.5.1 - a steganography program
* Copyright (C) 1999-2003 Stefan Hetzl <shetzl@chello.at>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <cfloat>
#include <cstdlib>
#include "DFSAPHeuristic.h"
#include "BitString.h"
#include "CvrStgFile.h"
#include "DMDConstructionHeuristic.h"
#include "EmbData.h"
#include "Embedder.h"
#include "Edge.h"
#include "Graph.h"
#include "Matching.h"
#include "ProgressOutput.h"
#include "Selector.h"
#include "Vertex.h"
#include "WKSConstructionHeuristic.h"
#include "common.h"
#include "error.h"
#include "msg.h"
Globals Globs ;
Embedder::Embedder ()
{
// read embfile
VerboseMessage vrs ;
if (Args.EmbFn.getValue() == "") {
vrs.setMessage (_("reading secret data from standard input...")) ;
}
else {
vrs.setMessage (_("reading secret file \"%s\"..."), Args.EmbFn.getValue().c_str()) ;
vrs.setNewline (false) ;
}
vrs.printMessage() ;
std::vector<BYTE> emb ;
BinaryIO embio (Args.EmbFn.getValue(), BinaryIO::READ) ;
while (!embio.eof()) {
emb.push_back (embio.read8()) ;
}
embio.close() ;
VerboseMessage vdone (_(" done")) ;
if (Args.EmbFn.getValue() != "") {
vdone.printMessage() ;
}
// create bitstring to be embedded
std::string fn = "" ;
if (Args.EmbedEmbFn.getValue()) {
fn = Args.EmbFn.getValue() ;
}
EmbData embdata (EmbData::EMBED, Args.Passphrase.getValue(), fn) ;
embdata.setEncAlgo (Args.EncAlgo.getValue()) ;
embdata.setEncMode (Args.EncMode.getValue()) ;
embdata.setCompression (Args.Compression.getValue()) ;
embdata.setChecksum (Args.Checksum.getValue()) ;
embdata.setData (emb) ;
ToEmbed = embdata.getBitString() ;
// read cover-/stego-file
VerboseMessage vrc ;
if (Args.CvrFn.getValue() == "") {
vrc.setMessage (_("reading cover file from standard input...")) ;
}
else {
vrc.setMessage (_("reading cover file \"%s\"..."), Args.CvrFn.getValue().c_str()) ;
}
vrc.setNewline (false) ;
vrc.printMessage() ;
CvrStgFile::readFile (Args.CvrFn.getValue()) ;
vdone.printMessage() ;
ToEmbed.setArity (Globs.TheCvrStgFile->getEmbValueModulus()) ;
if ((ToEmbed.getNAryLength() * Globs.TheCvrStgFile->getSamplesPerVertex()) > Globs.TheCvrStgFile->getNumSamples()) {
throw SteghideError (_("the cover file is too short to embed the data.")) ;
}
// create graph
Selector sel (Globs.TheCvrStgFile->getNumSamples(), Args.Passphrase.getValue()) ;
VerboseMessage v (_("creating the graph...")) ;
v.setNewline (false) ;
v.printMessage() ;
new Graph (Globs.TheCvrStgFile, ToEmbed, sel) ;
Globs.TheGraph->printVerboseInfo() ;
if (Args.Check.getValue()) {
if (!Globs.TheGraph->check()) {
CriticalWarning w ("integrity checking of graph data structures failed!") ; // TODO: internationalize this
w.printMessage() ;
}
}
#ifdef DEBUG
if (Args.DebugCommand.getValue() == PRINTGRAPH) {
Globs.TheGraph->print() ;
exit (EXIT_SUCCESS) ;
}
else if (Args.DebugCommand.getValue() == PRINTGMLGRAPH) {
Globs.TheGraph->print_gml (std::cout) ;
exit (EXIT_SUCCESS) ;
}
else if (Args.DebugCommand.getValue() == PRINTGMLVERTEX) {
std::vector<bool> nodeprinted (Globs.TheGraph->getNumVertices()) ;
std::vector<bool> edgesprinted (Globs.TheGraph->getNumVertices()) ;
Globs.TheGraph->printPrologue_gml(std::cout) ;
Globs.TheGraph->printVertex_gml (std::cout, Globs.TheGraph->getVertex(Args.GmlStartVertex.getValue()), Args.GmlGraphRecDepth.getValue(), nodeprinted, edgesprinted) ;
Globs.TheGraph->printEpilogue_gml(std::cout) ;
exit (EXIT_SUCCESS) ;
}
#endif
}
Embedder::~Embedder ()
{
delete Globs.TheGraph ;
delete Globs.TheCvrStgFile ;
}
void Embedder::embed ()
{
ProgressOutput* prout = NULL ;
if (Args.Verbosity.getValue() == NORMAL) {
std::string embstring, cvrstring ;
if (Args.EmbFn.getValue() == "") {
embstring = _("standard input") ;
}
else {
embstring = "\"" + Args.EmbFn.getValue() + "\"" ;
}
if (Args.CvrFn.getValue() == "") {
cvrstring = _("standard input") ;
}
else {
cvrstring = "\"" + Args.CvrFn.getValue() + "\"" ;
}
char buf[200] ;
sprintf (buf, _("embedding %s in %s..."), embstring.c_str(), cvrstring.c_str()) ;
prout = new ProgressOutput (std::string(buf)) ;
}
else if (Args.Verbosity.getValue() == VERBOSE) {
prout = new ProgressOutput () ;
}
const Matching* M = calculateMatching (prout) ;
// embed matched edges
const std::list<Edge*> medges = M->getEdges() ;
for (std::list<Edge*>::const_iterator it = medges.begin() ; it != medges.end() ; it++) {
embedEdge (*it) ;
}
// embed exposed vertices
const std::list<Vertex*> *expvertices = M->getExposedVerticesLink() ;
for (std::list<Vertex*>::const_iterator it = expvertices->begin() ; it != expvertices->end() ; it++) {
embedExposedVertex (*it) ;
}
delete M ;
// write stego file
Globs.TheCvrStgFile->transform (Args.StgFn.getValue()) ;
bool displaydone = false ;
if (Globs.TheCvrStgFile->is_std()) {
Message ws (_("writing stego file to standard output... ")) ;
ws.printMessage() ;
}
else {
if (Args.StgFn.getValue() != Args.CvrFn.getValue()) {
Message ws (_("writing stego file \"%s\"... "), Globs.TheCvrStgFile->getName().c_str()) ;
ws.setNewline (false) ;
ws.printMessage() ;
displaydone = true ;
}
}
Globs.TheCvrStgFile->write() ;
if (displaydone) {
Message wsd (_("done")) ;
wsd.printMessage() ;
}
}
const Matching* Embedder::calculateMatching (ProgressOutput* prout)
{
Matching* matching = new Matching (Globs.TheGraph, prout) ;
std::vector<MatchingAlgorithm*> MatchingAlgos = Globs.TheCvrStgFile->getMatchingAlgorithms (Globs.TheGraph, matching) ;
for (std::vector<MatchingAlgorithm*>::const_iterator ait = MatchingAlgos.begin() ; ait != MatchingAlgos.end() ; ait++) {
if (Args.Verbosity.getValue() == VERBOSE) {
prout->setMessage (_("executing %s..."), (*ait)->getName()) ;
}
(*ait)->setGoal (Args.Goal.getValue()) ;
(*ait)->run() ;
delete *ait ;
if (Args.Verbosity.getValue() == VERBOSE) {
prout->done (matching->getMatchedRate(), matching->getAvgEdgeWeight()) ;
}
}
if (Args.Check.getValue()) {
if (!matching->check()) {
CriticalWarning w ("integrity checking of matching data structures failed!") ; // TODO: internationalize this
w.printMessage() ;
}
}
matching->printVerboseInfo() ; // only print info for best matching
if (Args.Verbosity.getValue() == NORMAL) {
prout->done() ;
}
if (prout) {
delete prout ;
}
return matching ;
}
void Embedder::embedEdge (Edge *e)
{
Vertex* v1 = e->getVertex1() ;
Vertex* v2 = e->getVertex2() ;
Globs.TheCvrStgFile->replaceSample (e->getSamplePos(v1), e->getReplacingSampleValue (v1)) ;
Globs.TheCvrStgFile->replaceSample (e->getSamplePos(v2), e->getReplacingSampleValue (v2)) ;
}
void Embedder::embedExposedVertex (Vertex *v)
{
SamplePos samplepos = 0 ;
SampleValue *newsample = NULL ;
float mindistance = FLT_MAX ;
for (unsigned short i = 0 ; i < Globs.TheCvrStgFile->getSamplesPerVertex() ; i++) {
SampleValue *curold = v->getSampleValue(i) ;
SampleValue *curnew = v->getSampleValue(i)->getNearestTargetSampleValue(v->getTargetValue(i)) ;
if (curold->calcDistance (curnew) < mindistance) {
samplepos = v->getSamplePos(i) ;
newsample = curnew ;
mindistance = curold->calcDistance (curnew) ;
}
else {
delete curnew ;
}
}
#ifdef DEBUG
printDebug (1, "embedding vertex with label %lu by changing sample position %lu.", v->getLabel(), samplepos) ;
#endif
EmbValue oldev = Globs.TheCvrStgFile->getEmbeddedValue (samplepos) ;
Globs.TheCvrStgFile->replaceSample (samplepos, newsample) ;
myassert (oldev != Globs.TheCvrStgFile->getEmbeddedValue (samplepos)) ;
delete newsample ;
}