summaryrefslogtreecommitdiff
path: root/cmd/crc.c
blob: 4aedef56bc83791149489b9932d4ddd11fa0b841 (plain)
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
#include <endian.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sodium.h>
#include <getopt.h>
#include "crc_util.h"
#include "CRCLib.h"
#include "crc.h"
#include "compress_util.h"

const long idat_signature = 1229209940;
const long iend_signature = 1229278788;

unsigned long first_idat(unsigned char *addr) {
	int idat_found = 0;
	unsigned long offset = 8;
	int jump_offset = 0;
	int header_type = 0;
	while(idat_found == 0) {
		jump_offset = check_header_length(addr, offset);
		header_type = check_header_length(addr, offset+4);
		if(header_type == idat_signature) {
			idat_found = 1;
		} else {
			offset = offset + jump_offset + 12;
		}
	}
	return offset;
}

int total_idat(unsigned char *addr) {
	int iend_found = 0;
	int found_idat = 0;
	unsigned long offset = 8;
	int jump_offset = 0;
	int header_type = 0;
	while(iend_found == 0) {
		jump_offset = check_header_length(addr, offset);
		header_type = check_header_length(addr, offset+4);
		if(header_type == iend_signature) {
			iend_found = 1;
		} else {
			if(header_type == idat_signature) {
				found_idat++;
			}
			offset = offset + jump_offset + 12;
		}
	}
	return found_idat;
}

int update_file_crc(unsigned char *addr, unsigned long offset , unsigned int crc_num) {
	int startCRC = 8 + offset + check_header_length(addr, offset);
	unsigned char new_crc;
	for(int i = 0; i < 4; i++) {
		new_crc = crc_num >> (8*(3-i)) & 0xFF;
		addr[startCRC+i] = new_crc;
	}
	return 0;
}

void random_data_change(unsigned char *color_data, int width, int length) {
	int searching = 1;
	size_t rounds = 0;
	width = 16;
	int color_range = 3;	
	unsigned char temp_color_data[length];

	do {
		rounds++;
		// Creating temporary data set
		memcpy(temp_color_data, color_data, length);
		// Generating random byte to change
		int random_num = randombytes_uniform(length);
		// Checking for index break
		if(random_num % ((width * color_range) + 1)) {
			if(color_data[random_num] == 255) {
				temp_color_data[random_num]--;
			} else {
				temp_color_data[random_num]++;
			}
			unsigned char *check_data_buff = NULL;
			size_t check_data_length = 0;
			zlib_compress_data(temp_color_data, length, &check_data_buff, &check_data_length);

			unsigned char full_data[check_data_length+4];
			full_data[0] = 0x49;
			full_data[1] = 0x44;
			full_data[2] = 0x41;
			full_data[3] = 0x54;
			for(int i = 0; i < check_data_length; i++) {
				full_data[i+4] = check_data_buff[i];
			}
			unsigned int temp_crc = crc(full_data, check_data_length);
			if ((temp_crc >> (8*3)) == 10 ) {
				printf("Found in %zu rounds!\n", rounds);
				memcpy(color_data, temp_color_data, length);
				searching = 0;
			}
			free(check_data_buff);
		}
	
	} while(searching == 1);
}

void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) {
	FILE *fp;

	fp = fopen(out_file_name, "w");

	union{
		unsigned char data[sizeof(struct PNG_START_FILE_STRUCT)];
		struct PNG_START_FILE_STRUCT png_data;
	}start_data;

	start_data.png_data = png_file->png_start_data;

	// IHDR Data
	for(int i = 0; i < sizeof(start_data.data); i++) {
		fputc(start_data.data[i], fp);
	}
	// IDAT Data
	for(int i = 0; i < 4; i++) {
		fputc(png_file->png_idat_data.idat_length[i], fp);
	}
	for(int i = 0; i < 4; i++) {
		fputc(png_file->png_idat_data.idat_header[i], fp);
	}
	for(int i = 0; i < be32toh(png_file->png_idat_data.idat_data_length); i++) {
		fputc(png_file->png_idat_data.idat_data[i], fp);
	}
	// Generating CRC
	unsigned char full_data[be32toh(png_file->png_idat_data.idat_data_length)+4];
	for(int i = 0; i < 4; i++) {
		full_data[i] = png_file->png_idat_data.idat_header[i];
	}
	for(int i = 0; i < be32toh(png_file->png_idat_data.idat_data_length); i++) {
		full_data[i+4] = png_file->png_idat_data.idat_data[i];
	}

	unsigned int int_crc = crc(full_data, be32toh(png_file->png_idat_data.idat_data_length));
	unsigned char new_crc[4];

	for(int i = 0; i < 4; i++) {
		new_crc[i] = int_crc >> (8*(3-i)) & 0xFF;
		fputc(new_crc[i], fp);
	}
	// IEND Data
	unsigned char IEND_DATA[12] = { 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82};
	for(int i = 0; i < 12; i++) {
		fputc(IEND_DATA[i], fp);
	}
	fclose(fp);
}

int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, char *message, int accuracy, unsigned long offset, char *out_file_name) {
	if(accuracy > 4) {
		printf("Warning, accuracy cannot be larger than 4");
		return EXIT_FAILURE;
	}
	if(accuracy > 2) {
		printf("Notice, this could take a long time...");
	}
	if(total_idat(addr) < strlen((char*)message)) {
		printf("Warning, message exceeds IDAT amount\n");
		exit(EXIT_FAILURE);
	}

	int idat_length = check_header_length(addr, offset);
	printf("IDAT Length: %d\n", idat_length);

	long size = 1;
	size_t idat_byte_length = 0;
	unsigned char* idat_data = calloc(size, sizeof(unsigned char));
	for(size_t i = 0; i <= idat_length; i++) {
		if(i == size) {
			size *= 2;
			idat_data = reallocarray(idat_data, size, sizeof(unsigned char));
		}
		idat_data[i] = addr[i+offset+8];
		idat_byte_length = i;
	}
	unsigned char temp_idat_data[idat_byte_length];
	for(int i = 0; i <= idat_length; i++) {
		temp_idat_data[i] = idat_data[i];
	}
	// Decompressing Data
	unsigned char *uncom_data_buff = NULL;
	size_t uncom_data_size = 0;
	zlib_decompress_data(temp_idat_data, idat_byte_length, &uncom_data_buff, &uncom_data_size);

	random_data_change(uncom_data_buff, 16, uncom_data_size);
	
	// Compress Data
	unsigned char *com_data_buff;
	size_t com_data_size = 0;
	zlib_compress_data(uncom_data_buff, uncom_data_size, &com_data_buff, &com_data_size);

	png_file->png_idat_data.idat_data = calloc(com_data_size, sizeof(unsigned char));

	png_file->png_idat_data.idat_data_length = be32toh(com_data_size);

	for(size_t i = 0; i < com_data_size; i++) {
		png_file->png_idat_data.idat_data[i] = com_data_buff[i];
	}

	// Build PNG File
	build_png_file(png_file, out_file_name);

	free(uncom_data_buff);
	free(com_data_buff);
	free(idat_data);
	free(png_file->png_idat_data.idat_data);

	return 0;
}

// This is where it all starts
int main(int argc, char **argv) {
	FILE *fp;
	size_t i = 0;
	unsigned long offset = 0;
	struct PNG_FILE_STRUCT png_file_data;
	char *in_file_name = NULL;
	char *out_file_name = NULL;
	char *message = NULL;

	static const struct option long_options[] = {
		{"help", no_argument, NULL, 'h'},
		{"file", required_argument, NULL, 'f'},
		{"outfile", required_argument, NULL, 'o'},
		{"message", required_argument, NULL, 'm'},
		{0, 0, 0, 0}
	};

	const char* usage =
		"Usage: crc [options]\n"
		"  -h, --help		Shows help message\n"
		"  -f, --file		Denotes input file\n"
		"  -o, --outfile	Denotes output file\n"
		"  -m, --message	Encoded message\n"
		"\n";

	int c;
	while (1) {
		int option_index = 0;
		c = getopt_long(argc, argv, "hf:o:m:", long_options ,&option_index);
		if(c == -1) {
			break;
		}
		switch(c) {
			case 'h':
				printf("%s", usage);
				exit(EXIT_SUCCESS);
			case 'f':
				in_file_name = optarg;
				break;
			case 'o':
				out_file_name = optarg;
				break;
			case 'm':
				message = optarg;
				break;
		}
	}

	if(in_file_name == NULL) {
		printf("Input file required!\n");
		exit(EXIT_FAILURE);
	} else if(out_file_name == NULL) {
		printf("Output file required!\n");
		exit(EXIT_FAILURE);
	} else if(message == NULL) {
		printf("Message required!\n");
		exit(EXIT_FAILURE);
	}

	if(sodium_init() == -1) {
		return EXIT_FAILURE;
	}

	fp = fopen(in_file_name, "rt");
	if (fp == NULL) {
		printf("File error\n");
		exit(EXIT_FAILURE);
	}

	unsigned char *file_data = file_to_char_array(fp, &i);
	fclose(fp);

	populate_start_png(file_data, &png_file_data.png_start_data);

	offset = first_idat(file_data);

	populate_idat_png(file_data, &png_file_data.png_idat_data, offset);

	change_idat_content(file_data, &png_file_data, message, 1, offset, out_file_name);

	free(file_data);
}