diff --git a/crc.c b/crc.c index 30a0747..ac480de 100644 --- a/crc.c +++ b/crc.c @@ -10,7 +10,7 @@ #include "CRCLib.h" #include "crc.h" -#define CHUNK 8 +#define CHUNK 1024 const long idat_signature = 1229209940; const long iend_signature = 1229278788; @@ -71,10 +71,63 @@ FILE *zlib_decompress_data(unsigned char *data_chunk, size_t file_length) { (void)inflateEnd(&strm); - return of; } +FILE *zlib_compress_data(unsigned char *data_chunk, size_t file_length) { + int ret, flush; + unsigned int have; + z_stream strm; + unsigned char in[CHUNK]; + unsigned char out[CHUNK]; + int level = 9; + int method = Z_DEFLATED; + int windowBits = 10; + int memLevel = 9; + //int strategy = Z_DEFAULT_STRATEGY; + int strategy = Z_FILTERED; + + FILE *data_stream = fmemopen(data_chunk, file_length, "r"); + FILE *out_data_stream = open_memstream(NULL, NULL); + + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy); + if (ret != Z_OK) { + return NULL; + } + + do { + strm.avail_in = fread(in, 1, CHUNK, data_stream); + if (ferror(data_stream)) { + (void)deflateEnd(&strm); + return NULL; + } + flush = feof(data_stream) ? Z_FINISH : Z_NO_FLUSH; + strm.next_in = in; + + do { + strm.avail_out = CHUNK; + strm.next_out = out; + + ret = deflate(&strm, flush); + assert(ret != Z_STREAM_ERROR); + have = CHUNK - strm.avail_out; + if(fwrite(out, 1, have, out_data_stream) != have || ferror(out_data_stream)) { + (void)deflateEnd(&strm); + return NULL; + } + } while(strm.avail_out == 0); + assert(strm.avail_in == 0); + + } while(flush != Z_FINISH); + assert(ret == Z_STREAM_END); + + (void)deflateEnd(&strm); + return out_data_stream; +} + unsigned long first_idat(unsigned char *addr) { int idat_found = 0; unsigned long offset = 8; @@ -124,6 +177,7 @@ int update_file_crc(unsigned char *addr, unsigned long offset , unsigned int crc 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]; @@ -131,6 +185,7 @@ void random_data_change(unsigned char *color_data, int width, int length) { memcpy(temp_color_data, color_data, length); do { + rounds++; // Creating temporary data set memcpy(temp_color_data, color_data, length); // Generating random byte to change @@ -138,10 +193,28 @@ void random_data_change(unsigned char *color_data, int width, int length) { // Checking for index break if(random_num % ((width * color_range) + 1)) { if(color_data[random_num] == 255) { - //temp_color_data[random_num]--; + temp_color_data[random_num]--; } else { - //temp_color_data[random_num]++; + temp_color_data[random_num]++; } + FILE *check_data = zlib_compress_data(temp_color_data, length); + size_t check_data_length = 0; + + unsigned char *check_data_array = file_to_char_array(check_data, &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_array[i]; + } + unsigned int temp_crc = crc(full_data, check_data_length); + if ((temp_crc >> (8*3)) == 61 ) { + printf("Found in %zu rounds!\n", rounds); + searching = 0; + } + } } while(searching == 1);