multi idat write support
This commit is contained in:
157
cmd/crc.c
157
cmd/crc.c
@@ -13,6 +13,9 @@
|
|||||||
#include "crc.h"
|
#include "crc.h"
|
||||||
#include "compress_util.h"
|
#include "compress_util.h"
|
||||||
|
|
||||||
|
#define MAX_IDAT_SIZE 16384
|
||||||
|
//#define MAX_IDAT_SIZE 8192
|
||||||
|
|
||||||
const long idat_signature = 1229209940;
|
const long idat_signature = 1229209940;
|
||||||
const long iend_signature = 1229278788;
|
const long iend_signature = 1229278788;
|
||||||
|
|
||||||
@@ -87,36 +90,43 @@ static int verify_crc_chunks(unsigned char *data, size_t data_length, int crc_de
|
|||||||
unsigned char* testing_chunk;
|
unsigned char* testing_chunk;
|
||||||
unsigned int crc_check = 0;
|
unsigned int crc_check = 0;
|
||||||
unsigned int test_data_length = 0;
|
unsigned int test_data_length = 0;
|
||||||
|
unsigned int crc_check_length;
|
||||||
|
int rounds = 0;
|
||||||
|
|
||||||
for(int i = 0; i < crc_depth; i++) {
|
for(int i = 0; i < crc_depth; i++) {
|
||||||
if((max_data_length * (i+1)) > data_length) {
|
if((max_data_length * (i+1)) > data_length) {
|
||||||
test_data_length = (data_length - (max_data_length * i));
|
test_data_length = (data_length - (max_data_length * i));
|
||||||
testing_chunk = calloc(test_data_length+4, sizeof(unsigned char));
|
testing_chunk = calloc(test_data_length+4, sizeof(unsigned char));
|
||||||
|
// If this is met we need to stop verification
|
||||||
|
i = crc_depth;
|
||||||
} else {
|
} else {
|
||||||
test_data_length = max_data_length;
|
test_data_length = max_data_length;
|
||||||
testing_chunk = calloc(test_data_length+4, sizeof(unsigned char));
|
testing_chunk = calloc(test_data_length+4, sizeof(unsigned char));
|
||||||
}
|
}
|
||||||
|
// Setting first chunk to IDAT
|
||||||
testing_chunk[0] = 0x49;
|
testing_chunk[0] = 0x49;
|
||||||
testing_chunk[1] = 0x44;
|
testing_chunk[1] = 0x44;
|
||||||
testing_chunk[2] = 0x41;
|
testing_chunk[2] = 0x41;
|
||||||
testing_chunk[3] = 0x54;
|
testing_chunk[3] = 0x54;
|
||||||
for(int j = 0; j < test_data_length; j++) {
|
for(int j = 0; j < test_data_length; j++) {
|
||||||
testing_chunk[j+4] = data[(max_data_length*i)+j];
|
testing_chunk[j+4] = data[(test_data_length*rounds)+j];
|
||||||
|
}
|
||||||
|
crc_check_length = test_data_length+4;
|
||||||
|
crc_check = crc(testing_chunk, crc_check_length);
|
||||||
|
|
||||||
|
if(i == 1) {
|
||||||
|
printf("ATTEMPT: %08X\n", crc_check);
|
||||||
}
|
}
|
||||||
crc_check = crc(testing_chunk, test_data_length);
|
|
||||||
|
|
||||||
if(i == 2) {
|
|
||||||
printf("THIRD: %08X\n", crc_check);
|
|
||||||
} else if(i == 1) {
|
|
||||||
printf("SECOND: %08X\n", crc_check);
|
|
||||||
}
|
|
||||||
if ((crc_check >> (8*3)) != 10 ) {
|
if ((crc_check >> (8*3)) != 10 ) {
|
||||||
free(testing_chunk);
|
free(testing_chunk);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
// Used as an alternative to i
|
||||||
|
// Since i needs to be changed it max data length is met
|
||||||
|
rounds++;
|
||||||
}
|
}
|
||||||
free(testing_chunk);
|
free(testing_chunk);
|
||||||
printf("FOUND: %08X\n", crc_check);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,9 +139,10 @@ static int crc_embed_data(unsigned char *data, unsigned int data_length, int bit
|
|||||||
// Compressing data for test
|
// Compressing data for test
|
||||||
zlib_compress_data(data, data_length, &check_data, &check_data_length);
|
zlib_compress_data(data, data_length, &check_data, &check_data_length);
|
||||||
|
|
||||||
int match_crc = verify_crc_chunks(check_data, check_data_length, 3, 8192);
|
int match_crc = verify_crc_chunks(check_data, check_data_length, 1, MAX_IDAT_SIZE);
|
||||||
|
|
||||||
if(match_crc == 0) {
|
if(match_crc == 0) {
|
||||||
|
printf("COM SIZE: %zu\n", check_data_length);
|
||||||
free(check_data);
|
free(check_data);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -147,8 +158,9 @@ void *random_data_change_thread_call(void *w) {
|
|||||||
// Creating temporary data set
|
// Creating temporary data set
|
||||||
memcpy(temp_color_data, data->data, data->uncom_data_len);
|
memcpy(temp_color_data, data->data, data->uncom_data_len);
|
||||||
|
|
||||||
|
// Look into testing these values more
|
||||||
if(data->uncom_data_len > 800000) {
|
if(data->uncom_data_len > 800000) {
|
||||||
data->data_len = 150000;
|
data->data_len = 90000;
|
||||||
} else {
|
} else {
|
||||||
data->data_len = data->uncom_data_len;
|
data->data_len = data->uncom_data_len;
|
||||||
}
|
}
|
||||||
@@ -160,22 +172,23 @@ void *random_data_change_thread_call(void *w) {
|
|||||||
|
|
||||||
searching = crc_embed_data(temp_color_data, data->data_len, data->width, data->color_range, data->win_size);
|
searching = crc_embed_data(temp_color_data, data->data_len, data->width, data->color_range, data->win_size);
|
||||||
|
|
||||||
if (searching == 0) {
|
pthread_mutex_lock(&data->mutex_lock);
|
||||||
pthread_mutex_lock(data->mutex_lock);
|
if (searching == 0 && *data->searching == 1) {
|
||||||
//pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
|
||||||
*data->searching = searching;
|
*data->searching = searching;
|
||||||
memcpy(data->data, temp_color_data, data->uncom_data_len);
|
memcpy(data->data, temp_color_data, data->uncom_data_len);
|
||||||
//pthread_cancel(data->thread_id);
|
pthread_mutex_unlock(&data->mutex_lock);
|
||||||
pthread_mutex_unlock(data->mutex_lock);
|
break;
|
||||||
}
|
}
|
||||||
|
pthread_mutex_unlock(&data->mutex_lock);
|
||||||
|
|
||||||
} while(*data->searching == 1);
|
} while(*data->searching == 1);
|
||||||
free(temp_color_data);
|
free(temp_color_data);
|
||||||
return NULL;
|
pthread_exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void random_data_change(unsigned char *color_data, unsigned char *width, size_t length) {
|
void random_data_change(unsigned char *color_data, unsigned char *width, size_t length) {
|
||||||
int searching = 1;
|
int searching = 1;
|
||||||
|
int core_count = 16;
|
||||||
unsigned int sliding_window = 0;
|
unsigned int sliding_window = 0;
|
||||||
unsigned int compress_data_length = 0;
|
unsigned int compress_data_length = 0;
|
||||||
// Needs to be turned into a variable
|
// Needs to be turned into a variable
|
||||||
@@ -187,24 +200,31 @@ void random_data_change(unsigned char *color_data, unsigned char *width, size_t
|
|||||||
}w;
|
}w;
|
||||||
memcpy(w.width_array, width, 4);
|
memcpy(w.width_array, width, 4);
|
||||||
|
|
||||||
// Temp data array for crc testing
|
struct EMBED_THREAD_STRUCT *t_data = malloc(sizeof(struct EMBED_THREAD_STRUCT));
|
||||||
//unsigned char* temp_color_data = calloc(length, sizeof(unsigned char));
|
|
||||||
|
|
||||||
//int num_threads = 1;
|
if(pthread_mutex_init(&t_data->mutex_lock, NULL) != 0) {
|
||||||
struct EMBED_THREAD_STRUCT t_data;
|
printf("Mutex Lock Error\n");
|
||||||
t_data.searching = &searching;
|
return;
|
||||||
t_data.data = color_data;
|
|
||||||
t_data.data_len = compress_data_length;
|
|
||||||
t_data.uncom_data_len = length;
|
|
||||||
t_data.width = be32toh(w.width_int);
|
|
||||||
t_data.color_range = color_range;
|
|
||||||
t_data.win_size = sliding_window;
|
|
||||||
pthread_t tid;
|
|
||||||
t_data.thread_id = tid;
|
|
||||||
for(int i = 0; i < 28; i++) {
|
|
||||||
pthread_create(&tid, NULL, random_data_change_thread_call, &t_data);
|
|
||||||
}
|
}
|
||||||
pthread_join(tid, NULL);
|
t_data->searching = &searching;
|
||||||
|
t_data->data = color_data;
|
||||||
|
t_data->data_len = compress_data_length;
|
||||||
|
t_data->uncom_data_len = length;
|
||||||
|
t_data->width = be32toh(w.width_int);
|
||||||
|
t_data->color_range = color_range;
|
||||||
|
t_data->win_size = sliding_window;
|
||||||
|
pthread_t tid;
|
||||||
|
t_data->thread_id = tid;
|
||||||
|
for(int i = 0; i < core_count; i++) {
|
||||||
|
pthread_create(&tid, NULL, random_data_change_thread_call, t_data);
|
||||||
|
}
|
||||||
|
// Waiting for all threads to complete
|
||||||
|
int waiting = 0;
|
||||||
|
do {
|
||||||
|
pthread_join(tid, NULL);
|
||||||
|
waiting++;
|
||||||
|
} while(waiting!=core_count);
|
||||||
|
pthread_mutex_destroy(&t_data->mutex_lock);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -227,32 +247,57 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) {
|
|||||||
for(int i = 0; i < sizeof(start_data.data); i++) {
|
for(int i = 0; i < sizeof(start_data.data); i++) {
|
||||||
fputc(start_data.data[i], fp);
|
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));
|
// Generating IDAT CHUNKS
|
||||||
unsigned char new_crc[4];
|
int idat_loop = 0;
|
||||||
|
uint32_t current_len = 0;
|
||||||
|
do {
|
||||||
|
// Setting IDAT length chunk variable
|
||||||
|
if ((be32toh(png_file->png_idat_data.idat_data_length)-(MAX_IDAT_SIZE*idat_loop)) > MAX_IDAT_SIZE) {
|
||||||
|
current_len = MAX_IDAT_SIZE;
|
||||||
|
} else {
|
||||||
|
current_len = (be32toh(png_file->png_idat_data.idat_data_length)-(MAX_IDAT_SIZE*idat_loop));
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDAT LENGTH WRITE
|
||||||
|
for(int i = 0; i < 4; i++) {
|
||||||
|
//fputc(png_file->png_idat_data.idat_length[i], fp);
|
||||||
|
fputc(current_len >> (8*(3-i)), fp);
|
||||||
|
}
|
||||||
|
// IDAT HEADER WRITE
|
||||||
|
for(int i = 0; i < 4; i++) {
|
||||||
|
fputc(png_file->png_idat_data.idat_header[i], fp);
|
||||||
|
}
|
||||||
|
// IDAT DATA WRITE
|
||||||
|
//for(int i = 0; i < be32toh(png_file->png_idat_data.idat_data_length); i++) {
|
||||||
|
for(int i = 0; i < current_len; i++) {
|
||||||
|
fputc(png_file->png_idat_data.idat_data[i+(MAX_IDAT_SIZE*idat_loop)], fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
for(int i = 0; i < 4; i++) {
|
||||||
|
full_data[i] = png_file->png_idat_data.idat_header[i];
|
||||||
|
}
|
||||||
|
for(int i = 0; i < current_len; i++) {
|
||||||
|
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];
|
||||||
|
|
||||||
|
// IDAT CRC WRITE
|
||||||
|
for(int i = 0; i < 4; i++) {
|
||||||
|
new_crc[i] = int_crc >> (8*(3-i)) & 0xFF;
|
||||||
|
fputc(new_crc[i], fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
|
||||||
for(int i = 0; i < 4; i++) {
|
|
||||||
new_crc[i] = int_crc >> (8*(3-i)) & 0xFF;
|
|
||||||
fputc(new_crc[i], fp);
|
|
||||||
}
|
|
||||||
// IEND Data
|
// IEND Data
|
||||||
unsigned char IEND_DATA[12] = { 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82};
|
unsigned char IEND_DATA[12] = { 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82};
|
||||||
for(int i = 0; i < 12; i++) {
|
for(int i = 0; i < 12; i++) {
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ struct PNG_FILE_STRUCT {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct EMBED_THREAD_STRUCT {
|
struct EMBED_THREAD_STRUCT {
|
||||||
pthread_mutex_t *mutex_lock;
|
pthread_mutex_t mutex_lock;
|
||||||
pthread_t thread_id;
|
pthread_t thread_id;
|
||||||
int *searching;
|
int *searching;
|
||||||
unsigned char *data;
|
unsigned char *data;
|
||||||
|
|||||||
Reference in New Issue
Block a user