forked from linggan/C-Steganography-Framework
/
main.c
executable file
·370 lines (292 loc) · 11.7 KB
/
main.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
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
//
// main.c
// f5+dct
//
// Created by Gwendolyn Weston on 7/20/13.
// Copyright (c) 2013 Gwendolyn. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include "node.h"
#include "f5algorithm.h"
#include "jpeglib.h"
struct arguments {
char *inputname;
char *outputname;
int embedFlag;
int extractFlag;
char *embedMessage;
char *userPassword;
size_t message_size;
};
struct coefficients {
size_t size;
buffer_coefficient *array;
};
static void parse_arguments(int argc, char *argv[], struct arguments *args)
{
if (argc < 5 || argc > 7) {
fprintf(stderr, "wrong number of arguments\n");
exit(EXIT_FAILURE);
}
args->inputname = argv[1];
args->outputname = argv[2];
args->embedFlag = !strcmp(argv[3], "e");
args->extractFlag = !strcmp(argv[3], "x");
if (args->embedFlag) {
args->userPassword = argv[4];
args->embedMessage = argv[5];
args->message_size = strlen(args->embedMessage)*8; // size in bits
printf("We are embedding \ninput pic is %s \noutput pic is %s \nembedMessageis %s \nuserPassword is %s \n\nmessageSize is %ld", args->inputname, args->outputname, args->embedMessage, args->userPassword, args->message_size);
}
else if (args->extractFlag) {
args->userPassword = argv[4];
args->message_size = 88;
printf("We are extracting \ninput pic is %s \nmessageSize is %ld \nuserPassword is %s \n", args->inputname, args->message_size, args->userPassword);
}
else {
printf("What is %s?\n", argv[3]);
exit(1);
}
}
// jpeg data
static struct jpeg_error_mgr jerr;
static jvirt_barray_ptr *coef_arrays;
static size_t block_row_size[MAX_COMPONENTS];
static JDIMENSION width_in_blocks[MAX_COMPONENTS];
static JDIMENSION height_in_blocks[MAX_COMPONENTS];
static int num_components;
static JBLOCKARRAY row_ptrs[MAX_COMPONENTS];
static void read_DCT(const char *inputname, JBLOCKARRAY *coef_buffers, j_compress_ptr outputinfo)
{
FILE * input_file = fopen(inputname, "rb");
if (input_file == NULL) {
perror(inputname);
exit(EXIT_FAILURE);
}
struct jpeg_decompress_struct inputinfo;
/* Initialize the JPEG compression and decompression objects with default error handling. */
inputinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&inputinfo);
/* Specify data source for decompression and recompression */
jpeg_stdio_src(&inputinfo, input_file);
/* Read file header */
(void) jpeg_read_header(&inputinfo, TRUE);
num_components = inputinfo.num_components;
j_common_ptr common = (j_common_ptr) &inputinfo;
/* Allocate memory for reading out DCT coeffs */
for (int compnum = 0; compnum < num_components; compnum++) {
jpeg_component_info *comp = &inputinfo.comp_info[compnum];
coef_buffers[compnum] = inputinfo.mem->alloc_barray(common,
JPOOL_IMAGE,
comp->width_in_blocks,
comp->height_in_blocks);
}
/* Read input file as DCT coeffs */
coef_arrays = jpeg_read_coefficients(&inputinfo);
/* Copy compression parameters from the input file to the output file */
jpeg_copy_critical_parameters(&inputinfo, outputinfo);
/* Copy DCT coeffs to a new array */
for (int compnum = 0; compnum<num_components; compnum++)
{
height_in_blocks[compnum] = inputinfo.comp_info[compnum].height_in_blocks;
width_in_blocks[compnum] = inputinfo.comp_info[compnum].width_in_blocks;
block_row_size[compnum] = (size_t) sizeof(JCOEF)*DCTSIZE2*width_in_blocks[compnum];
for (JDIMENSION rownum=0; rownum<height_in_blocks[compnum]; rownum++)
{
row_ptrs[compnum] = inputinfo.mem->access_virt_barray(common, coef_arrays[compnum], rownum, 1, FALSE);
for (JDIMENSION blocknum=0; blocknum<width_in_blocks[compnum]; blocknum++)
{
for (int i=0; i<DCTSIZE2; i++)
{
coef_buffers[compnum][rownum][blocknum][i] = row_ptrs[compnum][0][blocknum][i];
}
}
}
}
jpeg_finish_decompress(&inputinfo);
jpeg_destroy_decompress(&inputinfo);
fclose(input_file);
}
static void init_usable_coeffs(const JBLOCKARRAY *coef_buffers, struct coefficients *usable)
{
//First, get an array of all usable coefficients from DCT, which in our case are modes 1-4
//buffer_coefficient usable_coefficient_array[height_in_blocks[0]*width_in_blocks[0]*4];
usable->size = 0;
//only doing component = 0 so as to only embed in luma coefficients
for (JDIMENSION rownum=0; rownum<height_in_blocks[0]; rownum++)
{
for (JDIMENSION blocknum=0; blocknum<width_in_blocks[0]; blocknum++)
{
//printf("\n\nRow:%i, Column: %i\n", rownum, blocknum);
for (unsigned int i=0; i<4; i++)
{
JCOEF this_iterations_coefficient = coef_buffers[0][rownum][blocknum][i];
if (this_iterations_coefficient){
//make struct of usable coefficient with indices
buffer_coefficient usable_coefficient;
usable_coefficient.row_index = rownum;
usable_coefficient.column_index = blocknum;
usable_coefficient.block_index = i;
usable_coefficient.coefficient = this_iterations_coefficient;
usable->array[usable->size] = usable_coefficient;
usable->size++;
}
}
}
}
}
static node *instantiate_permutation(unsigned seed, struct coefficients *usable)
{
srand(seed);
//instantiate a permutation of DCT coefficients, using Fisher-Yates algorithm
size_t permutationArray[usable->size];
for (size_t index = 0; index < usable->size; index++) {
permutationArray[index]=index;
}
printf("running permutation\n");
size_t randomIndex, temp;
size_t maxRandom = usable->size;
for (size_t index = 0; index < usable->size; index++) {
randomIndex = ((unsigned int) rand()) % maxRandom--;
temp = permutationArray[randomIndex];
permutationArray[randomIndex] = permutationArray[index];
permutationArray[index] = temp;
}
printf("done permuting\n");
node *root = malloc(sizeof(node));
node *current_node = root;
printf("creating linked list...\n");
for (size_t linked_list_index = 0; linked_list_index < usable->size; linked_list_index++) {
//creates linked list of the coefficients, as shuffled by the permutation
node *new_node = malloc(sizeof(node));
new_node->coeff_struct = usable->array[permutationArray[linked_list_index]];
add_to_linked_list(new_node, current_node);
current_node = current_node->next;
}
printf("done creating linked list\n");
current_node = root->next;
for (int j = 0; j < 30; j++) {
printf("%i ", current_node->coeff_struct.coefficient);
current_node = current_node->next;
}
//print_linked_list(root);
return root;
}
static size_t make_linked_list(const JBLOCKARRAY *coef_buffers, unsigned int seed, node **root)
{
struct coefficients usable;
usable.array = malloc(sizeof(buffer_coefficient)*height_in_blocks[0]*width_in_blocks[0]*4);
if (!usable.array) {
perror("malloc");
exit(EXIT_FAILURE);
}
init_usable_coeffs(coef_buffers, &usable);
printf("\n\n");
printf("buffer size is %i\n", (int) usable.size);
//openSSL hasn't been recreatable
//RAND_seed(userPassword, strlen(userPassword));
printf("seed is %u\n", seed);
*root = instantiate_permutation(seed, &usable);
free(usable.array);
return usable.size;
}
static FILE *setup_output(const char *outputname, j_compress_ptr outputinfo)
{
FILE * output_file = fopen(outputname, "wb");
if (output_file == NULL) {
perror(outputname);
exit(EXIT_FAILURE);
}
outputinfo->err = jpeg_std_error(&jerr);
jpeg_create_compress(outputinfo);
jpeg_stdio_dest(outputinfo, output_file);
return output_file;
}
static void write_DCT(const char *outputname, JBLOCKARRAY *coef_buffers, j_compress_ptr outputinfo)
{
{
printf("writing to new picture\n");
j_common_ptr common = (j_common_ptr) outputinfo;
/* Output the new DCT coeffs to a JPEG file */
for (int compnum=0; compnum<num_components; compnum++)
{
for (JDIMENSION rownum=0; rownum<height_in_blocks[compnum]; rownum++)
{
row_ptrs[compnum] = outputinfo->mem->access_virt_barray(common, coef_arrays[compnum], rownum, (JDIMENSION) 1, TRUE);
memcpy(row_ptrs[compnum][0][0],
coef_buffers[compnum][rownum][0],
block_row_size[compnum]);
}
}
/* Write to the output file */
jpeg_write_coefficients(outputinfo, coef_arrays);
/* Finish compression and release memory */
jpeg_finish_compress(outputinfo);
jpeg_destroy_compress(outputinfo);
/* All done. */
printf("New DCT coefficients successfully written to %s\n\n", outputname);
exit(jerr.num_warnings ? EXIT_FAILURE : EXIT_SUCCESS);
}
}
static void embed(JBLOCKARRAY *coef_buffers, const char *embedMessage, node *root, size_t root_len)
{
{
//going along random walk as produced by the CPRNG, embed/extract message into coefficients using binary hamming matrix code
embedMessageIntoCoefficients(embedMessage, root, root_len);
//then go back and update JPEG's coefficients with the changed coefficients accordingly, using the indices data stored in the coefficient struct
printf("changing coefficients\n");
node *current_node = root->next;
while (current_node != NULL) {
JCOEF coefficient = current_node->coeff_struct.coefficient;
coef_buffers[0][current_node->coeff_struct.row_index][current_node->coeff_struct.column_index][current_node->coeff_struct.block_index] = coefficient;
current_node = current_node->next;
}
printf("done embedding\n");
}
}
static void extract(size_t message_size, node *root, size_t usable_size)
{
{
node *current_node = root;
int j = 0;
while (j < 10) {
printf("%i ", current_node->coeff_struct.coefficient);
current_node = current_node->next;
j++;
}
}
printf("began extracting...\n");
char *extractedMessage = malloc(message_size + 1);
if (!extractedMessage) {
perror("malloc");
exit(1);
}
extractMessageFromCoefficients(root, usable_size, message_size, extractedMessage);
printf("extractedMessage is %s\n", extractedMessage);
free(extractedMessage);
}
int main(int argc, char * argv[])
{
struct arguments args;
parse_arguments(argc, argv, &args);
struct jpeg_compress_struct outputinfo;
JBLOCKARRAY coef_buffers[MAX_COMPONENTS];
FILE *output = setup_output(args.outputname, &outputinfo);
read_DCT(args.inputname, coef_buffers, &outputinfo);
unsigned int seed = (unsigned) strtoul(args.userPassword, NULL, 10);
node *root;
size_t usable_size = make_linked_list(coef_buffers, seed, &root);
if (args.embedFlag) {
embed(coef_buffers, args.embedMessage, root, usable_size);
write_DCT(args.outputname, coef_buffers, &outputinfo);
}
else if (args.extractFlag) {
extract(args.message_size, root, usable_size);
}
fclose(output);
return 0;
}