Files
PNG_CC/crc.c
2021-09-22 23:44:13 -04:00

145 lines
3.2 KiB
C

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sodium.h>
#include "CRCLib.h"
const int png_signature[8] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
const int idat_signature = 1229209940;
const int iend_signature = 1229278788;
const int working = 1;
int check_file_header(int *addr) {
int signature_match = 0;
for( int i = 0; i < 8; i++ ) {
if (addr[i] != png_signature[i]) {
signature_match = 1;
}
}
printf("Sig Match: %d\n", signature_match);
return signature_match;
}
int check_header_length(int *addr, int offset) {
unsigned int res = 0;
for( int i = 0; i < 4; i++ ) {
res |= addr[offset+i];
if (i < 3) {
res <<= 8;
}
}
return res;
}
int first_idat(int *addr) {
int idat_found = 0;
int 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(int *addr) {
int iend_found = 0;
int found_idat = 0;
int 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 change_idat_content(int *addr, char *message, int accuracy, int offset) {
printf("Starting IDAT Tranform");
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(message)) {
printf("Warning, message exceeds IDAT amount");
return EXIT_FAILURE;
}
// Comment
int i = total_idat(addr);
printf("Total IDAT %d\n", i);
int idat_length = check_header_length(addr, offset);
int prop_found = 0;
int size = 1;
int* idat_data = malloc(size * sizeof(int));
while(prop_found == 0) {
for(i = 0; i < idat_length; i++) {
idat_data[i] = addr[i+offset+8];
size++;
int* new_idat_data = realloc(idat_data, size * sizeof(int));
}
int r = randombytes_uniform(5) + 1;
int j = randombytes_uniform(idat_length);
idat_data[j] = (idat_data[j] + r) % 255;
unsigned char crc_check[size+4];
int idat_header[] = { 0x49, 0x44, 0x41, 0x54 };
for(i = 0; i < 4; i++) {
crc_check[i] = idat_header[i];
}
for(i = 0; i < size; i++) {
crc_check[i] = idat_data[i+4];
}
int crcnum = crc(crc_check, idat_length);
printf("New CRC: %08X\n", crcnum);
printf("Test: %X\n", crcnum >> (8*3));
prop_found = 1;
}
return 0;
}
int main() {
FILE *fp;
int c;
int myArray[255] = {};
int i = 0;
int offset = 0;
char message[1];
if(sodium_init() == -1) {
return EXIT_FAILURE;
}
fp = fopen("./1.png", "rt");
while((c = fgetc(fp)) != EOF) {
myArray[i] = c;
i++;
}
fclose(fp);
offset = first_idat(myArray);
change_idat_content(myArray, message, 2, offset);
//int crcnum = crc(myArray, 19);
//printf("%08X\n", crcnum);
}