-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.cpp
385 lines (330 loc) · 8.22 KB
/
encode.cpp
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <iostream>
#include <cstring>
#include <string>
#include "encode.h"
#include "common.h"
#include "types.h"
#include "decode.h"
using std::cout;
using std::endl;
Status EncodeInfo::read_and_validate_encode_args(char *argv[])
{
//validation for .bmp file
if(strstr(argv[2],".bmp")!=NULL)
{
src_image_fname=argv[2];
}
else
{
return e_failure;
}
//validation for secret_file
if(argv[3]!=NULL)
{
int flag=1,len,num=0;
secret_fname=argv[3];
len=strlen(argv[3]);
for(int i=len-1;i>=0;i-- )
{
if(argv[3][i]=='.')
{
flag=0;
for(int j=i;argv[3][j]!='\0';j++)
{
extn_secret_file[num++]=argv[3][j];
}
extn_secret_file[num]='\0';
break;
}
}
if(flag==1)
return e_failure;
}
else
{
return e_failure;
}
//validation for output image
if(argv[4]==NULL)
{
stego_image_fname="stego.bmp";
}
else
{
if(strstr(argv[4],".bmp")!=NULL)
{
stego_image_fname=argv[4];
}
else
{
int len=strlen(argv[4]);
argv[4][len]='.';
argv[4][len+1]='b';
argv[4][len+2]='m';
argv[4][len+3]='p';
argv[4][len+4]='\0';
stego_image_fname=argv[4];
// cout<<encInfo.stego_image_fname<<endl;
}
}
return e_success;
}
Status EncodeInfo::open_files()
{
// Src Image file
fptr_src_image = fopen(src_image_fname, "r");
// Do Error handling
if (fptr_src_image == NULL)
{
perror("fopen");
fprintf(stderr, "ERROR: Unable to open file %s\n",src_image_fname);
return e_failure;
}
// Secret file
fptr_secret = fopen(secret_fname, "r");
// Do Error handling
if (fptr_secret == NULL)
{
perror("fopen");
fprintf(stderr, "ERROR: Unable to open file %s\n",secret_fname);
return e_failure;
}
// else
// {
// printf("NOT NULL\n");
// }
// Stego Image file
fptr_stego_image = fopen(stego_image_fname, "w");
// Do Error handling
if (fptr_stego_image == NULL)
{
perror("fopen");
fprintf(stderr, "ERROR: Unable to open file %s\n",stego_image_fname);
return e_failure;
}
// No failure return e_success
return e_success;
}
uint EncodeInfo::get_image_size_for_bmp(FILE *fptr_image)
{
uint width, height;
// Seek to 18th byte
fseek(fptr_image, 18, SEEK_SET);
// Read the width (an int)
fread(&width, sizeof(int), 1, fptr_image);
std::cout<<"width = "<<width<<std::endl;
// Read the height (an int)
fread(&height, sizeof(int), 1, fptr_image);
std::cout<<"height = "<<height<<std::endl;
// Return image capacity
return width * height * 3;
}
uint EncodeInfo::get_file_size(FILE *ptr)
{
fseek(ptr,0,SEEK_END);
//returing size of a file
return ftell(ptr);
}
Status EncodeInfo::check_capacity()
{
//checking size of a src image is enough to hold the data of the secret file data
if(get_image_size_for_bmp(fptr_src_image) >= (54+(strlen(MAGIC_STRING))+4+strlen(extn_secret_file)+4+get_file_size(fptr_secret))*8)
{
return e_success;
}
else
{
return e_failure;
}
}
Status EncodeInfo::copy_bmp_header(FILE *fptr_src_image, FILE *fptr_dest_image)
{
char arr[54],ch1,ch2;
fseek(fptr_src_image,0,SEEK_SET);
//reading starting 54 bytes of data from src image
fread(arr,54,1,fptr_src_image);
fseek(fptr_dest_image,0,SEEK_SET);
//Writing starting 54 bytes of data to stego image
fwrite(arr,54,1,fptr_dest_image);
return e_success;
}
Status EncodeInfo::encode_magic_string(const char *magic_string)
{
int size=strlen(magic_string);
// const char *data;
// data=magic_string;
encode_data_to_image(magic_string, size,fptr_src_image,fptr_stego_image);
return e_success;
}
Status EncodeInfo::encode_data_to_image(const char *data, int size, FILE *fptr_src_image, FILE *fptr_stego_image)
{
char image_buffer[8];
for(int i=0;i<size;i++)
{
//reading 8 bytes data from src image every iteration
fread(image_buffer,8,1,fptr_src_image);
//calling byte to lsb function
encode_byte_to_lsb(data[i], image_buffer);
//writinig 8 bytes data to stego image
fwrite(image_buffer,8,1,fptr_stego_image);
}
return e_success;
}
Status EncodeInfo::encode_byte_to_lsb(char data, char *image_buffer)
{
for(int i=0;i<8;i++)
{
//clearing 0th bit
image_buffer[i]=(image_buffer[i]&~(1));
//geting a bit
if(data&(1<<i))
image_buffer[i]|=1; //replaceing a bit if 1
else
image_buffer[i]|=0; //replacing a bit if 0
}
return e_success;
}
//Definition for encode secret file extension
Status EncodeInfo::encode_secret_file_extn( char *file_extn)
{
int size=strlen(file_extn);
encode_data_to_image(file_extn, size,fptr_src_image,fptr_stego_image);
return e_success;
}
Status EncodeInfo::encode_secret_file_size(long file_size)
{
char image_buffer[32];
//reading 32 bytes data from src image
fread(image_buffer,32,1,fptr_src_image);
//calling size to lsb function
encode_size_to_lsb(file_size,image_buffer);
//reading 32 bytes data to stego image
fwrite(image_buffer,32,1,fptr_stego_image);
return e_success;
}
Status EncodeInfo::encode_size_to_lsb(int data, char *image_buffer)
{
for(int i=0;i<32;i++)
{
//clearing 0th bit
image_buffer[i]=(image_buffer[i]&~(1));
//geting a bit
if(data&(1<<i))
image_buffer[i]|=1; //replaceing a bit if 1
else
image_buffer[i]|=0; //replaceing a bit if 0
}
return e_success;
}
Status EncodeInfo::encode_secret_file_data()
{
// int size=strlen(extn_secret_file);
int size=size_secret_file;
char image_buffer[8];
char data[size];
//making file pointer to point to the 0th position
fseek(fptr_secret,0,SEEK_SET);
//reading data size times from the secret file
fread(data,size,1,fptr_secret);
for(int i=0;i<size;i++)
{
//reading 8 bytes data from src image every iteration
fread(image_buffer,8,1,fptr_src_image);
//calling byte to lsb function
encode_byte_to_lsb(data[i], image_buffer);
//reading 8 bytes data to stego image every iteration
fwrite(image_buffer,8,1,fptr_stego_image);
}
return e_success;
}
Status EncodeInfo::copy_remaining_img_data(FILE *fptr_src, FILE *fptr_dest)
{
char ch;
//reading each byte data from src image untill it reaches EOF
while(fread(&ch,1,1,fptr_src)!=0)
{
fwrite(&ch,1,1,fptr_dest);
}
return e_success;
}
Status EncodeInfo::do_encoding()
{
printf("Started Encoding\n");
if(open_files()==e_success)
{
cout<<"Open files successful\n";
}
else
{
cout<<"Open files failure\n";
return e_failure;
}
if(check_capacity()==e_success)
{
cout<<"Check capacity successful\n";
}
else
{
cout<<"check capacity failure\n";
return e_failure;
}
if(copy_bmp_header(fptr_src_image,fptr_stego_image)==e_success)
{
cout<<"Copy header successfull\n";
}
else
{
cout<<"Copy header failure\n";
return e_failure;
}
if(encode_magic_string(password)==e_success)
{
cout<<"Encoding of Magic_String Successfull\n";
}
else
{
cout<<"Encoding of Magic_String is unSuccessfull\n";
return e_failure;
}
if(encode_secret_file_size(strlen(extn_secret_file))==e_success)
{
cout<<"Encoding of secret file extension size successful\n";
}
else
{
cout<<"Encoding of secret file extension size unsuccessful\n";
}
if(encode_secret_file_extn(extn_secret_file)==e_success)
{
cout<<"Encoding of secret file extension is successful\n";
}
else
{
cout<<"Encoding of secret file extension is unsuccessful\n";
}
if(encode_secret_file_size(size_secret_file=get_file_size(fptr_secret))==e_success)
{
printf("Encoding of secret file size is successful\n");
}
else
{
printf("Encoding of secret file size is unsuccessful\n");
}
if(encode_secret_file_data()==e_success)
{
cout<<"Encoding of secret file data is successful\n";
}
else
{
cout<<"Encoding of secret file data is unsuccessful\n";
}
if(copy_remaining_img_data(fptr_src_image,fptr_stego_image)==e_success)
{
cout<<"Copying remaining image data is successfull\n";
}
else
{
cout<<"Copying remaining image data is unsuccessfull\n";
}
return e_success;
}