-
Notifications
You must be signed in to change notification settings - Fork 0
/
steganography.c
106 lines (87 loc) · 2.27 KB
/
steganography.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "steganography_util.h"
#include "steganography_common.h"
/*
* Function: encode
* ----------------------
* Encodes the image with the message
*
* Parameters:
* inputFile: the name of the input file
* messageFile: the name of the message file
* outputFile: the name of the output file
*
* returns: void
*/
void
encode (char *inputFile, char *messageFile, char *outputFile)
{
struct ImageInfo decodedImageInfo, encodedImageInfo;
// decode image
decode_image (inputFile, &decodedImageInfo);
// copy decoded image info to encoded image info
memcpy (&encodedImageInfo, &decodedImageInfo, sizeof (decodedImageInfo));
// apply LSB algorithm to encode image
LSBAlgorithm (decodedImageInfo, messageFile, &encodedImageInfo);
// encode image
encode_image (outputFile, &encodedImageInfo);
}
/*
* Function: decode
* ----------------------
* Decodes the image and stores the message in the output file
*
* Parameters:
* inputFile: the name of the input file
* outputFile: the name of the output file
*
* returns: void
*/
void
decode (char *inputFile, char *outputFile)
{
struct ImageInfo decodedImageInfo;
decode_image (inputFile, &decodedImageInfo);
size_t imageSize = decodedImageInfo.width * decodedImageInfo.height * 4;
// allocate memory for binary number
int
*binary = malloc (sizeof (int) * BYTE),
*binBuffer = malloc (sizeof (int) * BYTE),
*decimal = malloc (sizeof (int)),
*reversedBinary = malloc (sizeof (int) * BYTE), count = 0;
FILE *file = fopen (outputFile, "w");
if (file == NULL)
{
printf ("Error opening file!\n");
exit (EXIT_FAILURE);
}
for (int i = 0; i < imageSize; i++)
{
// convert decimal to binary
decimal2binary (decodedImageInfo.image[i], binary);
if (count < BYTE)
{
binBuffer[count] = binary[0];
if (count == 7)
{
*decimal = 0;
reverse (binBuffer, reversedBinary);
binary2decimal (reversedBinary, decimal);
if (*decimal != 0)
{
fprintf (file, "%c", *decimal);
}
else
{
exit (EXIT_SUCCESS);
}
}
count++;
}
if (count == BYTE)
count = 0;
}
fclose (file);
}