-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.c
217 lines (205 loc) · 6.52 KB
/
decode.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
#include <stdio.h>
#include "decode.h"
#include "types.h"
#include <string.h>
#include "common.h"
#include <stdlib.h>
// Function definition for read and validate decode args
Status_d read_and_validate_decode_args(char *argv[], DecodeInfo *decInfo)
{
if (strcmp(strstr(argv[2], "."), ".bmp") == 0)
decInfo->d_src_image_fname = argv[2];
else
return d_failure;
if (argv[3] != NULL)
decInfo->d_secret_fname = argv[3];
else
decInfo->d_secret_fname = "decode.txt";
return d_success;
}
// Function definition for open files for decoding
Status_d open_files_dec(DecodeInfo *decInfo)
{
// Stego Image file
decInfo->fptr_d_src_image = fopen(decInfo->d_src_image_fname, "r");
// Do Error handling
if (decInfo->fptr_d_src_image == NULL)
{
perror("fopen");
fprintf(stderr, "ERROR: Unable to open file %s\n", decInfo->d_src_image_fname);
return d_failure;
}
// Dest file
decInfo->fptr_d_secret = fopen(decInfo->d_secret_fname, "w");
// Do Error handling
if (decInfo->fptr_d_secret == NULL)
{
perror("fopen");
fprintf(stderr, "ERROR: Unable to open file %s\n", decInfo->d_secret_fname);
return d_failure;
}
// If no failure then return d_success
return d_success;
}
// Function definition for decode magic string
Status_d decode_magic_string(DecodeInfo *decInfo)
{
fseek(decInfo->fptr_d_src_image, 54, SEEK_SET);
int i = strlen(MAGIC_STRING);
decInfo->magic_data = malloc(strlen(MAGIC_STRING) + 1);
decode_data_from_image(strlen(MAGIC_STRING), decInfo->fptr_d_src_image, decInfo);
decInfo->magic_data[i] = '\0';
if (strcmp(decInfo->magic_data, MAGIC_STRING) == 0)
return d_success;
else
return d_failure;
}
// Function definition for decoding data fom image
Status_d decode_data_from_image(int size, FILE *fptr_d_src_image, DecodeInfo *decInfo)
{
int i;
char str[8];
for (i = 0; i < size; i++)
{
fread(str, 8, sizeof(char), fptr_d_src_image);
decode_byte_from_lsb(&decInfo->magic_data[i], str);
}
return d_success;
}
// Function definition for decode byte from lsb
Status_d decode_byte_from_lsb(char *data, char *image_buffer)
{
int bit = 7;
unsigned char ch = 0x00;
for (int i = 0; i < 8; i++)
{
ch = ((image_buffer[i] & 0x01) << bit--) | ch;
}
*data = ch;
return d_success;
}
// Function definition for decode file extn size
Status_d decode_file_extn_size(int size, FILE *fptr_d_src_image)
{
char str[32];
int length;
fread(str, 32, sizeof(char), fptr_d_src_image);
decode_size_from_lsb(str, &length);
if (length == size)
return d_success;
else
return d_failure;
}
// Function definition decode size from lsb
Status_d decode_size_from_lsb(char *buffer, int *size)
{
int j = 31;
int num = 0x00;
for (int i = 0; i < 32; i++)
{
num = ((buffer[i] & 0x01) << j--) | num;
}
*size = num;
}
// Function definition for decode secret file extn
Status_d decode_secret_file_extn(char *file_ext, DecodeInfo *decInfo)
{
file_ext = ".txt";
int i = strlen(file_ext);
decInfo->d_extn_secret_file = malloc(i + 1);
decode_extension_data_from_image(strlen(file_ext), decInfo->fptr_d_src_image, decInfo);
decInfo->d_extn_secret_file[i] = '\0';
if (strcmp(decInfo->d_extn_secret_file, file_ext) == 0)
return d_success;
else
return d_failure;
}
// Function definition decode extension data from image
Status_d decode_extension_data_from_image(int size, FILE *fptr_d_src_image, DecodeInfo *decInfo)
{
for (int i = 0; i < size; i++)
{
fread(decInfo->d_src_image_fname, 8, 1, fptr_d_src_image);
decode_byte_from_lsb(&decInfo->d_extn_secret_file[i], decInfo->d_src_image_fname);
}
return d_success;
}
// Function definition for decode secret file size
Status_d decode_secret_file_size(int file_size, DecodeInfo *decInfo)
{
char str[32];
fread(str, 32, sizeof(char), decInfo->fptr_d_src_image);
decode_size_from_lsb(str, &file_size);
decInfo->size_secret_file = file_size;
return d_success;
}
// Function definition for decode secret file data
Status_d decode_secret_file_data(DecodeInfo *decInfo)
{
char ch;
for (int i = 0; i < decInfo->size_secret_file; i++)
{
fread(decInfo->d_src_image_fname, 8, sizeof(char), decInfo->fptr_d_src_image);
decode_byte_from_lsb(&ch, decInfo->d_src_image_fname);
fputc(ch, decInfo->fptr_d_secret);
}
return d_success;
}
// Function definition for do decoding
Status_d do_decoding(DecodeInfo *decInfo)
{
if (open_files_dec(decInfo) == d_success)
{
printf("Open files is a successfully\n");
if (decode_magic_string(decInfo) == d_success)
{
printf("Decoded magic string Successfully\n");
if (decode_file_extn_size(strlen(".txt"), decInfo->fptr_d_src_image) == d_success)
{
printf("Decoded file extension size Succesfully\n");
if (decode_secret_file_extn(decInfo->d_extn_secret_file, decInfo) == d_success)
{
printf("Decoded Secret File Extension Succesfully\n");
if (decode_secret_file_size(decInfo->size_secret_file, decInfo) == d_success)
{
printf("Decoded secret file size Successfully\n");
if (decode_secret_file_data(decInfo) == d_success)
{
printf("Decoded secret file data Succuessfully\n");
}
else
{
printf("Decoding of secret file data is a failure\n");
}
}
else
{
printf("Decode of secret file size is a failure\n");
return d_failure;
}
}
else
{
printf("Decode of Secret file extension is a failure\n");
return d_failure;
}
}
else
{
printf("Decoded of file extension size is a failure\n");
return d_failure;
}
}
else
{
printf("Decoding of magic string is a failure\n");
return d_failure;
}
}
else
{
printf("Open files is a failure\n");
return d_failure;
}
return d_success;
}