diff --git a/cmd/crc.c b/cmd/crc.c index b872e6c..0708625 100644 --- a/cmd/crc.c +++ b/cmd/crc.c @@ -1,4 +1,4 @@ -#pragma GCC optimize("Ofast") +#pragma GCC optimize("O0") #include #include @@ -18,8 +18,8 @@ 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; + long jump_offset = 0; + unsigned long header_type = 0; while(idat_found == 0) { jump_offset = check_header_length(addr, offset); header_type = check_header_length(addr, offset+4); @@ -33,21 +33,23 @@ unsigned long first_idat(unsigned char *addr) { } int total_idat(unsigned char *addr) { - int iend_found = 0; + int searching = 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); + unsigned long offset = 0; + unsigned long idat_length = 0; + unsigned long header_type = 0; + + unsigned long first_idat_offset = first_idat(addr); + offset = first_idat_offset; + + while(searching == 0) { + found_idat++; + idat_length = check_header_length(addr, offset); + // Jumping Offset + IDAT LENGTH + 3 bytes for 2 CRCs and 1 LEN fields + offset = offset+12+idat_length; 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; + if(header_type != idat_signature) { + searching = 1; } } return found_idat; @@ -63,40 +65,62 @@ int update_file_crc(unsigned char *addr, unsigned long offset , unsigned int crc return 0; } -void random_data_change(unsigned char *color_data, int width, int length) { +void random_data_change(unsigned char *color_data, unsigned char *width, size_t length) { int searching = 1; size_t rounds = 0; - width = 16; - int color_range = 3; - unsigned char temp_color_data[length]; + unsigned char* full_data; + int data_array_size = 0; + // Needs to be turned into a variable + int color_range = 3; + // Union for width type cast + union { + uint32_t width_int; + unsigned char width_array[4]; + }w; + // Temp data array for crc testing + unsigned char* temp_color_data = calloc(length, sizeof(unsigned char)); + + memcpy(w.width_array, width, 4); do { rounds++; // Creating temporary data set memcpy(temp_color_data, color_data, length); // Generating random byte to change - int random_num = randombytes_uniform(length); + size_t random_num = randombytes_uniform(100000); + //size_t random_num = randombytes_uniform(length); // Checking for index break - if(random_num % ((width * color_range) + 1)) { + if(random_num % ((be32toh(w.width_int) * color_range) + 1)) { if(color_data[random_num] == 255) { temp_color_data[random_num]--; } else { temp_color_data[random_num]++; } + // Compressing data for test 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); + zlib_compress_data(temp_color_data, 50000, &check_data_buff, &check_data_length); + //zlib_compress_data(temp_color_data, length, &check_data_buff, &check_data_length); + + if(check_data_length > 8192) { + data_array_size = 8192; + full_data = calloc(8196, sizeof(unsigned char)); + } else { + data_array_size = check_data_length; + full_data = calloc(check_data_length+4, sizeof(unsigned char)); + } - 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++) { + for(int i = 0; i < data_array_size; i++) { full_data[i+4] = check_data_buff[i]; } - unsigned int temp_crc = crc(full_data, check_data_length); + unsigned int temp_crc = crc(full_data, data_array_size); + printf("%08X\n", temp_crc); if ((temp_crc >> (8*3)) == 10 ) { + printf("RAND Key: %zu\n", random_num); printf("Found in %zu rounds!\n", rounds); memcpy(color_data, temp_color_data, length); searching = 0; @@ -105,6 +129,9 @@ void random_data_change(unsigned char *color_data, int width, int length) { } } while(searching == 1); + + free(temp_color_data); + return; } void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) { @@ -119,6 +146,8 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) { start_data.png_data = png_file->png_start_data; + printf("IHDR SIZE: %zu\n", sizeof(struct PNG_START_FILE_STRUCT)); + // IHDR Data for(int i = 0; i < sizeof(start_data.data); i++) { fputc(start_data.data[i], fp); @@ -158,6 +187,7 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) { } int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, char *message, int accuracy, unsigned long offset, char *out_file_name) { + size_t idat_length = 0; if(accuracy > 4) { printf("Warning, accuracy cannot be larger than 4"); return EXIT_FAILURE; @@ -170,31 +200,36 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c exit(EXIT_FAILURE); } - int idat_length = check_header_length(addr, offset); - printf("IDAT Length: %d\n", idat_length); - - long size = 1; + size_t 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)); + for(size_t i = 0; i < total_idat(addr); i++) { + idat_length = check_header_length(addr, offset); + for(size_t j = 0; j < idat_length; j++) { + if(idat_byte_length == size) { + size *= 2; + idat_data = reallocarray(idat_data, size, sizeof(unsigned char)); + } + idat_data[idat_byte_length] = addr[offset+8+j]; + idat_byte_length++; } - 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]; + // Offset 3 bytes for 2 length fields and 1 crc + offset = offset + idat_length + 12; + } + + unsigned char* temp_idat_data = calloc(idat_byte_length, sizeof(unsigned char)); + memcpy(temp_idat_data, idat_data, idat_byte_length); + // 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); + // Start data testing + random_data_change(uncom_data_buff, png_file->png_start_data.file_width, uncom_data_size); + return 0; // Compress Data unsigned char *com_data_buff; size_t com_data_size = 0; @@ -209,11 +244,13 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c } // Build PNG File - build_png_file(png_file, out_file_name); + //build_png_file(png_file, out_file_name); + // Freeing used memory free(uncom_data_buff); free(com_data_buff); free(idat_data); + free(temp_idat_data); free(png_file->png_idat_data.idat_data); return 0; diff --git a/include/crc.h b/include/crc.h index bf3b74a..645c63b 100644 --- a/include/crc.h +++ b/include/crc.h @@ -1,7 +1,6 @@ #pragma once int check_file_header(char *addr); -int check_header_length(unsigned char *addr, long offset); unsigned long first_idat(unsigned char *addr); int total_idat(unsigned char *addr); void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name); diff --git a/include/crc_util.h b/include/crc_util.h index 5670cdb..695146b 100644 --- a/include/crc_util.h +++ b/include/crc_util.h @@ -35,7 +35,7 @@ struct PNG_FILE_STRUCT { extern const long png_signature[8]; -int check_header_length(unsigned char *addr, long offset); +unsigned long check_header_length(unsigned char *addr, long offset); int check_file_header(char *addr); int create_cc_file(unsigned char *addr, unsigned long file_length); unsigned char* file_to_char_array(FILE *in_file, size_t* size); diff --git a/src/crc_util.c b/src/crc_util.c index 2b99d11..bd59941 100644 --- a/src/crc_util.c +++ b/src/crc_util.c @@ -4,8 +4,8 @@ const long png_signature[8] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }; -int check_header_length(unsigned char *addr, long offset) { - unsigned int res = 0; +unsigned long check_header_length(unsigned char *addr, long offset) { + unsigned long res = 0; for( int i = 0; i < 4; i++ ) { res |= addr[offset+i]; if (i < 3) { @@ -71,7 +71,7 @@ int create_cc_file(unsigned char *addr, unsigned long file_length) { } unsigned char* file_to_char_array(FILE *in_file, size_t* size) { - unsigned int c; + unsigned long c; unsigned long file_data_cap = 8; unsigned char* file_data = calloc(file_data_cap, sizeof(unsigned char));