-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.c
310 lines (287 loc) · 10.1 KB
/
encode.c
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
#include <stdio.h>
#include "encode.h"
#include "types.h"
#include <string.h>
#include "common.h"
/* Function definition for check operation type */
OperationType check_operation_type(char *argv[])
{
if (strcmp(argv[1], "-e") == 0)
return e_encode;
if (strcmp(argv[1], "-d") == 0)
return e_decode;
else
return e_unsupported;
}
/* Function Definitions */
/* Get image size
* Input: Image file ptr
* Output: width * height * bytes per pixel (3 in our case)
* Description: In BMP Image, width is stored in offset 18,
* and height after that. size is 4 bytes
*/
uint 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);
printf("width = %u\n", width);
// Read the height (an int)
fread(&height, sizeof(int), 1, fptr_image);
printf("height = %u\n", height);
// Return image capacity
return width * height * 3;
}
/*
* Get File pointers for i/p and o/p files
* Inputs: Src Image file, Secret file and
* Stego Image file
* Output: FILE pointer for above files
* Return Value: e_success or e_failure, on file errors
*/
Status open_files(EncodeInfo *encInfo)
{
// Src Image file
encInfo->fptr_src_image = fopen(encInfo->src_image_fname, "r");
// Do Error handling
if (encInfo->fptr_src_image == NULL)
{
perror("fopen");
fprintf(stderr, "ERROR: Unable to open file %s\n", encInfo->src_image_fname);
return e_failure;
}
// Secret file
encInfo->fptr_secret = fopen(encInfo->secret_fname, "r");
// Do Error handling
if (encInfo->fptr_secret == NULL)
{
perror("fopen");
fprintf(stderr, "ERROR: Unable to open file %s\n", encInfo->secret_fname);
return e_failure;
}
// Stego Image file
encInfo->fptr_stego_image = fopen(encInfo->stego_image_fname, "w");
// Do Error handling
if (encInfo->fptr_stego_image == NULL)
{
perror("fopen");
fprintf(stderr, "ERROR: Unable to open file %s\n", encInfo->stego_image_fname);
return e_failure;
}
// No failure return e_success
return e_success;
}
// Function definition for read and validate encode args
Status read_and_validate_encode_args(char *argv[], EncodeInfo *encInfo)
{
// If condition to check argv[2] is .bmp or not
if (strcmp(strstr(argv[2], "."), ".bmp") == 0)
encInfo->src_image_fname = argv[2];
else
return e_failure;
if (strcmp(strstr(argv[3], "."), ".txt") == 0)
encInfo->secret_fname = argv[3];
else
return e_failure;
if (argv[4] != NULL)
encInfo->stego_image_fname = argv[4];
else
encInfo->stego_image_fname = "stego.bmp";
return e_success;
}
// Function definition for check capacity
Status check_capacity(EncodeInfo *encInfo)
{
encInfo->image_capacity = get_image_size_for_bmp(encInfo->fptr_src_image);
encInfo->size_secret_file = get_file_size(encInfo->fptr_secret);
if (encInfo->image_capacity > ((strlen(MAGIC_STRING)*8+32+strlen(encInfo->extn_secret_file)*8+32+encInfo->size_secret_file)*8))
return e_success;
else
return e_failure;
}
// Function definition for getting file size
long get_file_size(FILE *fptr)
{
fseek(fptr, 0, SEEK_END);
return ftell(fptr);
}
// Function definition for copying 1st 54 bytes header file
Status copy_bmp_header(FILE *fptr_src_image, FILE *fptr_dest_image)
{
char str[54];
// Setting pointer to point to 0th position
fseek(fptr_src_image, 0, SEEK_SET);
// Reading 54 bytes from beautiful.bmp
fread(str, 54, 1, fptr_src_image);
// Writing 54 bytes to str
fwrite(str, 54, 1, fptr_dest_image);
return e_success;
}
// Function definition for encoding magic string
Status encode_magic_string(char *magic_string, EncodeInfo *encInfo)
{
encode_data_to_image(magic_string, 2, encInfo->fptr_src_image, encInfo->fptr_stego_image, encInfo);
return e_success;
}
// Function definition for Encode data to image
Status encode_data_to_image(char *data, int size, FILE *fptr_src_image, FILE *fptr_stego_image, EncodeInfo *encInfo)
{
for (int i = 0; i < size; i++)
{
fread(encInfo->image_data, 8, 1, fptr_src_image);
encode_byte_to_lsb(data[i], encInfo->image_data);
fwrite(encInfo->image_data, 8, 1, fptr_stego_image);
}
}
// Function definition for encode byte to lsb
Status encode_byte_to_lsb(char data, char *image_buffer)
{
unsigned int mask = 0x80, i;
for (i = 0; i < 8; i++)
{
image_buffer[i] = (image_buffer[i] & 0xFE) | ((data & mask) >> (7 - i));
mask = mask >> 1;
}
}
// Function definition for encode secret file extn size
Status encode_secret_file_extn_size(int size, FILE *fptr_src_image, FILE *fptr_stego_image)
{
char str[32];
fread(str, 32, 1, fptr_src_image);
encode_size_to_lsb(size, str);
fwrite(str, 32, 1, fptr_stego_image);
return e_success;
}
// Function definition to encode size to lsb
Status encode_size_to_lsb(int size, char *image_buffer)
{
unsigned int mask = 1 << 31, i;
for (i = 0; i < 32; i++)
{
image_buffer[i] = (image_buffer[i] & 0xFE) | ((size & mask) >> (31 - i));
mask = mask >> 1;
}
}
// Function definition to encode secret file extn
Status encode_secret_file_extn(char *file_extn, EncodeInfo *encInfo)
{
encode_data_to_image(file_extn, strlen(file_extn), encInfo->fptr_src_image, encInfo->fptr_stego_image, encInfo);
return e_success;
}
// Function definition for encode secret file size
Status encode_secret_file_size(int size, EncodeInfo *encInfo)
{
char str[32];
fread(str, 32, 1, encInfo->fptr_src_image);
encode_size_to_lsb(size, str);
fwrite(str, 32, 1, encInfo->fptr_stego_image);
return e_success;
}
// Function definition for encode secret file data
Status encode_secret_file_data(EncodeInfo *encInfo)
{
fseek(encInfo->fptr_secret, 0, SEEK_SET);
char str[encInfo->size_secret_file];
fread(str, encInfo->size_secret_file, 1, encInfo->fptr_secret);
encode_data_to_image(str, strlen(str), encInfo->fptr_src_image, encInfo->fptr_stego_image, encInfo);
return e_success;
}
// Function definition for copying remaining data as it is
Status copy_remaining_img_data(FILE *fptr_src, FILE *fptr_dest)
{
char ch;
while ((fread(&ch, 1, 1, fptr_src)) > 0)
{
fwrite(&ch, 1, 1, fptr_dest);
}
return e_success;
}
// Function definition for do encoding called in main function
Status do_encoding(EncodeInfo *encInfo)
{
if (open_files(encInfo) == e_success)
{
printf("Open files is a successfully\n");
if (check_capacity(encInfo) == e_success)
{
printf("Check capacity is successfully\n");
if (copy_bmp_header(encInfo->fptr_src_image, encInfo->fptr_stego_image) == e_success)
{
printf("Copied bmp header successfully\n");
if (encode_magic_string(MAGIC_STRING, encInfo) == e_success)
{
printf("Encoded magic string successfully\n");
strcpy(encInfo->extn_secret_file, strstr(encInfo->secret_fname, "."));
if (encode_secret_file_extn_size(strlen(encInfo->extn_secret_file), encInfo->fptr_src_image, encInfo->fptr_stego_image) == e_success)
{
printf("Encoded secret file extn size successfully\n");
if (encode_secret_file_extn(encInfo->extn_secret_file, encInfo) == e_success)
{
printf("Encoded secret file extn successfully\n");
if (encode_secret_file_size(encInfo->size_secret_file, encInfo) == e_success)
{
printf("Encoded secret file size successfully\n");
if (encode_secret_file_data(encInfo) == e_success)
{
printf("Encoded secret file data successfully\n");
if (copy_remaining_img_data(encInfo->fptr_src_image, encInfo->fptr_stego_image) == e_success)
{
printf("Copied remaining data successfully\n");
}
else
{
printf("Failed to copy remaining data successfully\n");
return e_failure;
}
}
else
{
printf("Failed to encode secret file data\n");
return e_failure;
}
}
else
{
printf("Failed to encode secret file size\n");
return e_failure;
}
}
else
{
printf("Failed to encode secret file extn\n");
return e_failure;
}
}
else
{
printf("Failed to encoded secret file extn size\n");
return e_failure;
}
}
else
{
printf("Failed to encode magic string\n");
return e_failure;
}
}
else
{
printf("Failed to copy bmp header\n");
return e_failure;
}
}
else
{
printf("Check capacity is a failure\n");
return e_failure;
}
}
else
{
printf("Open files is a failure\n");
return e_failure;
}
return e_success;
}