fixed segfault
This commit is contained in:
140
cmd/crc.c
140
cmd/crc.c
@@ -13,8 +13,9 @@
|
||||
#include "crc.h"
|
||||
#include "compress_util.h"
|
||||
|
||||
#define MAX_IDAT_SIZE 16384
|
||||
//#define MAX_IDAT_SIZE 8192
|
||||
//#define MAX_IDAT_SIZE 16384
|
||||
#define MAX_WINDOW_SIZE 32768
|
||||
#define MAX_IDAT_SIZE 8192
|
||||
|
||||
const long idat_signature = 1229209940;
|
||||
const long iend_signature = 1229278788;
|
||||
@@ -77,6 +78,7 @@ static void random_window_bit_change(unsigned char *data, int width, int rounds,
|
||||
random_num = randombytes_uniform(length);
|
||||
} while((random_num % ((width * color_range) + 1)) == 0);
|
||||
|
||||
|
||||
if(data[random_num] == 255) {
|
||||
data[random_num]--;
|
||||
} else {
|
||||
@@ -114,10 +116,6 @@ static int verify_crc_chunks(unsigned char *data, size_t data_length, int crc_de
|
||||
crc_check_length = test_data_length+4;
|
||||
crc_check = crc(testing_chunk, crc_check_length);
|
||||
|
||||
if(i == 1) {
|
||||
printf("ATTEMPT: %08X\n", crc_check);
|
||||
}
|
||||
|
||||
if ((crc_check >> (8*3)) != 10 ) {
|
||||
free(testing_chunk);
|
||||
return 1;
|
||||
@@ -134,7 +132,7 @@ static int crc_embed_data(unsigned char *data, unsigned int data_length, int bit
|
||||
unsigned char *check_data;
|
||||
size_t check_data_length = 0;
|
||||
|
||||
random_window_bit_change(data, bit_width, 3, color_range, sliding_window);
|
||||
random_window_bit_change(data, bit_width, 2, color_range, sliding_window);
|
||||
|
||||
// Compressing data for test
|
||||
zlib_compress_data(data, data_length, &check_data, &check_data_length);
|
||||
@@ -175,6 +173,11 @@ void *random_data_change_thread_call(void *w) {
|
||||
pthread_mutex_lock(&data->mutex_lock);
|
||||
if (searching == 0 && *data->searching == 1) {
|
||||
*data->searching = searching;
|
||||
for(size_t i = 0; i < data->uncom_data_len; i++) {
|
||||
if (temp_color_data[i] != data->data[i]) {
|
||||
printf("LEN: %zu\nDIFF: %zu\nNEW: %02X\nOLD: %02X\n", data->uncom_data_len, i, temp_color_data[i], data->data[i]);
|
||||
}
|
||||
}
|
||||
memcpy(data->data, temp_color_data, data->uncom_data_len);
|
||||
pthread_mutex_unlock(&data->mutex_lock);
|
||||
break;
|
||||
@@ -188,7 +191,7 @@ void *random_data_change_thread_call(void *w) {
|
||||
|
||||
void random_data_change(unsigned char *color_data, unsigned char *width, size_t length) {
|
||||
int searching = 1;
|
||||
int core_count = 16;
|
||||
int core_count = 1;
|
||||
unsigned int sliding_window = 0;
|
||||
unsigned int compress_data_length = 0;
|
||||
// Needs to be turned into a variable
|
||||
@@ -231,6 +234,10 @@ void random_data_change(unsigned char *color_data, unsigned char *width, size_t
|
||||
|
||||
void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) {
|
||||
FILE *fp;
|
||||
unsigned char *full_data;
|
||||
unsigned int int_crc;
|
||||
unsigned char new_crc[4];
|
||||
unsigned char IEND_DATA[12] = { 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82};
|
||||
|
||||
fp = fopen(out_file_name, "w");
|
||||
|
||||
@@ -276,7 +283,7 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) {
|
||||
|
||||
// Generating CRC
|
||||
//unsigned char *full_data = malloc(be32toh(png_file->png_idat_data.idat_data_length)+4);
|
||||
unsigned char *full_data = malloc(current_len+4);
|
||||
full_data = malloc(current_len+4);
|
||||
for(int i = 0; i < 4; i++) {
|
||||
full_data[i] = png_file->png_idat_data.idat_header[i];
|
||||
}
|
||||
@@ -284,8 +291,7 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) {
|
||||
full_data[i+4] = png_file->png_idat_data.idat_data[i+(MAX_IDAT_SIZE*idat_loop)];
|
||||
}
|
||||
|
||||
unsigned int int_crc = crc(full_data, current_len+4);
|
||||
unsigned char new_crc[4];
|
||||
int_crc = crc(full_data, current_len+4);
|
||||
|
||||
// IDAT CRC WRITE
|
||||
for(int i = 0; i < 4; i++) {
|
||||
@@ -294,20 +300,41 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) {
|
||||
}
|
||||
|
||||
// Adding to loop count
|
||||
printf("Loop: %d\n", idat_loop);
|
||||
idat_loop++;
|
||||
} while(idat_loop < (be32toh(png_file->png_idat_data.idat_data_length) / MAX_IDAT_SIZE));
|
||||
} while((idat_loop-1) < (be32toh(png_file->png_idat_data.idat_data_length) / MAX_IDAT_SIZE));
|
||||
|
||||
// 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);
|
||||
}
|
||||
free(full_data);
|
||||
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) {
|
||||
unsigned char* populate_idat_array(unsigned char *addr, unsigned long offset, size_t *idat_byte_length) {
|
||||
size_t size = 8;
|
||||
//size_t idat_byte_length = 0;
|
||||
size_t idat_length = 0;
|
||||
//unsigned char* idat_data = calloc(size, sizeof(unsigned char));
|
||||
unsigned char* idat_data = calloc(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 += 1;
|
||||
}
|
||||
// Offset 3 bytes for 2 length fields and 1 crc
|
||||
offset = offset + idat_length + 12;
|
||||
}
|
||||
return idat_data;
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -320,23 +347,8 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
size_t size = 8;
|
||||
size_t idat_byte_length = 0;
|
||||
unsigned char* idat_data = calloc(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++;
|
||||
}
|
||||
// Offset 3 bytes for 2 length fields and 1 crc
|
||||
offset = offset + idat_length + 12;
|
||||
|
||||
}
|
||||
unsigned char *idat_data = populate_idat_array(addr, offset, &idat_byte_length);
|
||||
|
||||
unsigned char* temp_idat_data = calloc(idat_byte_length, sizeof(unsigned char));
|
||||
memcpy(temp_idat_data, idat_data, idat_byte_length);
|
||||
@@ -346,6 +358,8 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c
|
||||
size_t uncom_data_size = 0;
|
||||
zlib_decompress_data(temp_idat_data, idat_byte_length, &uncom_data_buff, &uncom_data_size);
|
||||
|
||||
printf("ORIG UNCOM LEN: %zu\n", uncom_data_size);
|
||||
|
||||
// Start data testing
|
||||
random_data_change(uncom_data_buff, png_file->png_start_data.file_width, uncom_data_size);
|
||||
|
||||
@@ -355,6 +369,8 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c
|
||||
size_t com_data_size = 0;
|
||||
zlib_compress_data(uncom_data_buff, uncom_data_size, &com_data_buff, &com_data_size);
|
||||
|
||||
printf("COM LEN: %zu\n", 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);
|
||||
@@ -381,6 +397,8 @@ int main(int argc, char **argv) {
|
||||
FILE *fp;
|
||||
size_t i = 0;
|
||||
unsigned long offset = 0;
|
||||
int uncompress_call = 0;
|
||||
int compress_call = 0;
|
||||
struct PNG_FILE_STRUCT png_file_data;
|
||||
char *in_file_name = NULL;
|
||||
char *out_file_name = NULL;
|
||||
@@ -391,11 +409,14 @@ int main(int argc, char **argv) {
|
||||
{"file", required_argument, NULL, 'f'},
|
||||
{"outfile", required_argument, NULL, 'o'},
|
||||
{"message", required_argument, NULL, 'm'},
|
||||
{"compress", no_argument, NULL, 'c'},
|
||||
{"uncompress", no_argument, NULL, 'h'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
const char* usage =
|
||||
"Usage: crc [options]\n"
|
||||
" -c --compress Compress Message\n"
|
||||
" -h, --help Shows help message\n"
|
||||
" -f, --file Denotes input file\n"
|
||||
" -o, --outfile Denotes output file\n"
|
||||
@@ -405,7 +426,7 @@ int main(int argc, char **argv) {
|
||||
int c;
|
||||
while (1) {
|
||||
int option_index = 0;
|
||||
c = getopt_long(argc, argv, "hf:o:m:", long_options ,&option_index);
|
||||
c = getopt_long(argc, argv, "hf:o:m:uc", long_options ,&option_index);
|
||||
if(c == -1) {
|
||||
break;
|
||||
}
|
||||
@@ -422,18 +443,28 @@ int main(int argc, char **argv) {
|
||||
case 'm':
|
||||
message = optarg;
|
||||
break;
|
||||
case 'c':
|
||||
compress_call = 1;
|
||||
break;
|
||||
case 'u':
|
||||
uncompress_call = 1;
|
||||
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(compress_call == 1) {
|
||||
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) {
|
||||
@@ -446,6 +477,7 @@ int main(int argc, char **argv) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
unsigned char *file_data = file_to_char_array(fp, &i);
|
||||
fclose(fp);
|
||||
|
||||
@@ -453,9 +485,37 @@ int main(int argc, char **argv) {
|
||||
|
||||
offset = first_idat(file_data);
|
||||
|
||||
populate_idat_png(file_data, &png_file_data.png_idat_data, offset);
|
||||
if(uncompress_call == 1) {
|
||||
size_t idat_byte_length = 0;
|
||||
unsigned char* idat_data = populate_idat_array(file_data, offset, &idat_byte_length);
|
||||
|
||||
change_idat_content(file_data, &png_file_data, message, 1, offset, out_file_name);
|
||||
for(int i = 0; i < idat_byte_length; i++) {
|
||||
printf("%02X ", idat_data[i]);
|
||||
}
|
||||
printf("\nDecompressed Data:\n\n");
|
||||
// Decompressing data
|
||||
unsigned char *uncom_data_buff = NULL;
|
||||
size_t uncom_data_size = 0;
|
||||
zlib_decompress_data(idat_data, idat_byte_length, &uncom_data_buff, &uncom_data_size);
|
||||
for(int i = 0; i < uncom_data_size; i++) {
|
||||
printf("%02X ", uncom_data_buff[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
free(idat_data);
|
||||
free(file_data);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
if(compress_call == 1) {
|
||||
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);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
free(file_data);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user