-
Notifications
You must be signed in to change notification settings - Fork 0
/
steganography_util.c
264 lines (232 loc) · 5.25 KB
/
steganography_util.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
#include <stdlib.h>
#include <stdio.h>
#include "3rdparty/lodepng/lodepng.h"
#include "steganography_common.h"
/*
* Function: decode_image
* ----------------------
* Decodes the image and stores the image info in the struct
*
* Parameters:
* inputFileName: the name of the input file
* imageInfo: the struct to store the image info
*
* returns: void
*/
void
decode_image (char *inputFileName, struct ImageInfo *imageInfo)
{
unsigned error;
unsigned char *image = 0;
unsigned width, height;
// decode
error = lodepng_decode32_file (&image, &width, &height, inputFileName);
// if there's an error, display it
if (error)
printf ("error %u: %s\n", error, lodepng_error_text (error));
// store the image info
imageInfo->image = 0;
imageInfo->image = image;
imageInfo->width = width;
imageInfo->height = height;
//free(image); // free memory used by image
return;
}
/*
* Function: encode_image
* ----------------------
* Encodes the image and stores the image info in the struct
*
* Parameters:
* outputFileName: the name of the output file
* imageInfo: the struct to store the image info
*
* returns: void
*/
void
encode_image (char *outputFileName, struct ImageInfo *imageInfo)
{
unsigned error;
// encode
error =
lodepng_encode32_file (outputFileName, imageInfo->image, imageInfo->width,
imageInfo->height);
// if there's an error, display it
if (error)
printf ("error %u: %s\n", error, lodepng_error_text (error));
}
/*
* Function: decimal2binary
* ------------------------
* Converts a decimal number to a binary number
*
* Parameters:
* decimal: the decimal number to be converted
* binary: the binary number to store the result
*
* returns: void
*/
void
decimal2binary (int decimal, int *binary)
{
int i = 0;
while (decimal > 0)
{
binary[i] = decimal % 2;
decimal = decimal / 2;
i++;
}
if (i < BYTE)
{
for (int j = i; j < BYTE; j++)
{
binary[j] = 0;
}
}
}
/*
* Function: binary2decimal
* ------------------------
* Converts a binary number to a decimal number
*
* Parameters:
* binary: the binary number to be converted
* decimal: the decimal number to store the result
*
* returns: void
*/
void
binary2decimal (int *binary, int *decimal)
{
int i = 0;
while (i < BYTE)
{
// shift left by i bits and add to decimal
*decimal += binary[i] * (1 << i);
i++;
}
}
/*
* Function: reverse
* -----------------
* Reverses the binary number
*
* Parameters:
* binary: the binary number to be reversed
* reversedBinary: the reversed binary number to store the result
*
* returns: void
*/
void
reverse (int *binary, int *reversedBinary)
{
int j = 7;
for (int i = 0; i < BYTE; i++)
{
reversedBinary[j] = binary[i];
j--;
}
}
/*
* Function: getTotalChars
* -----------------------
* Gets the total number of characters in a file
*
* Parameters:
* filename: the name of the file
*
* returns: int - the total number of characters in a file
*/
unsigned int
getTotalChars (char *filename)
{
// read file
FILE *file = fopen (filename, "r");
// read character by character
char c;
unsigned int j = 0;
while ((c = fgetc (file)) != EOF)
{
j++;
}
fclose (file);
return j;
}
/*
* Function: char2binary
* ---------------------
* Converts a character to a binary number
*
* Parameters:
* filename: the name of the file
* binaries: the binary number to store the result
*
* returns: void
*/
void
char2binary (char *filename, int *binaries)
{
char c;
int k = 0, *bin = malloc (sizeof (int) * BYTE);
FILE *file = fopen (filename, "r");
while ((c = fgetc (file)) != EOF)
{
for (int i = 0; i < BYTE; i++)
{
bin[i] = ((c << i) & 0x80) ? 1 : 0;
binaries[k] = bin[i];
k++;
}
}
fclose (file);
}
/*
* Function: LSBAlgorithm
* ----------------------
* Implements the LSB algorithm
*
* Parameters:
* decodedImageInfo: the struct that contains the decoded image info
* messageFile: the name of the file that contains the message
* encodedImageInfo: the struct that contains the encoded image info
*
* returns: void
*/
void
LSBAlgorithm (struct ImageInfo decodedImageInfo, char *messageFile,
struct ImageInfo *encodedImageInfo)
{
int
j = getTotalChars (messageFile),
totalBits = BYTE * j,
*binaries = malloc (sizeof (int) * totalBits), count = 0;
size_t imageSize = decodedImageInfo.width * decodedImageInfo.height * 4;
char2binary (messageFile, binaries);
//modify each byte of the image
for (int i = 0; i < imageSize; i++)
{
// allocate memory for binary number
int *binary = malloc (sizeof (int) * BYTE);
// convert decimal to binary
decimal2binary (decodedImageInfo.image[i], binary);
if (i < totalBits)
{
binary[0] = binaries[i];
}
else if (count < BYTE)
{
binary[0] = 0;
count++;
}
// allocate memory for decimal number
int *decimal = malloc (sizeof (int));
*decimal = 0;
// convert binary to decimal
binary2decimal (binary, decimal);
// modify the image
encodedImageInfo->image[i] = *decimal;
// free memory
free (decimal);
free (binary);
}
}