Compare commits
1 Commits
3b8fd9c901
...
v0.0.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c86b56286c |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,4 +2,6 @@
|
||||
*.out
|
||||
*.woo
|
||||
*.wow
|
||||
*.gch
|
||||
vgcore.*
|
||||
bin/
|
||||
|
||||
3
Makefile
3
Makefile
@@ -14,3 +14,6 @@ output_dir:
|
||||
|
||||
clean:
|
||||
rm -rf $(OUTPUT_DIR) **.h.gch
|
||||
|
||||
memcheck: debug
|
||||
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes -s ${OUTPUT_DIR}/crc
|
||||
|
||||
166
cmd/crc.c
166
cmd/crc.c
@@ -1,8 +1,12 @@
|
||||
#pragma GCC optimize("Ofast")
|
||||
|
||||
#include <endian.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sodium.h>
|
||||
#include <getopt.h>
|
||||
#include "crc_util.h"
|
||||
#include "CRCLib.h"
|
||||
#include "crc.h"
|
||||
@@ -66,8 +70,6 @@ void random_data_change(unsigned char *color_data, int width, int length) {
|
||||
int color_range = 3;
|
||||
unsigned char temp_color_data[length];
|
||||
|
||||
memcpy(temp_color_data, color_data, length);
|
||||
|
||||
do {
|
||||
rounds++;
|
||||
// Creating temporary data set
|
||||
@@ -96,6 +98,7 @@ void random_data_change(unsigned char *color_data, int width, int length) {
|
||||
unsigned int temp_crc = crc(full_data, check_data_length);
|
||||
if ((temp_crc >> (8*3)) == 10 ) {
|
||||
printf("Found in %zu rounds!\n", rounds);
|
||||
memcpy(color_data, temp_color_data, length);
|
||||
searching = 0;
|
||||
}
|
||||
free(check_data_buff);
|
||||
@@ -104,8 +107,57 @@ void random_data_change(unsigned char *color_data, int width, int length) {
|
||||
} while(searching == 1);
|
||||
}
|
||||
|
||||
int change_idat_content(unsigned char *addr, unsigned char *message, int accuracy, unsigned long offset) {
|
||||
//printf("Starting IDAT Tranform\n");
|
||||
void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) {
|
||||
FILE *fp;
|
||||
|
||||
fp = fopen(out_file_name, "w");
|
||||
|
||||
union{
|
||||
unsigned char data[sizeof(struct PNG_START_FILE_STRUCT)];
|
||||
struct PNG_START_FILE_STRUCT png_data;
|
||||
}start_data;
|
||||
|
||||
start_data.png_data = png_file->png_start_data;
|
||||
|
||||
// IHDR Data
|
||||
for(int i = 0; i < sizeof(start_data.data); i++) {
|
||||
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));
|
||||
unsigned char new_crc[4];
|
||||
|
||||
for(int i = 0; i < 4; i++) {
|
||||
new_crc[i] = int_crc >> (8*(3-i)) & 0xFF;
|
||||
fputc(new_crc[i], fp);
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
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) {
|
||||
if(accuracy > 4) {
|
||||
printf("Warning, accuracy cannot be larger than 4");
|
||||
return EXIT_FAILURE;
|
||||
@@ -113,17 +165,15 @@ int change_idat_content(unsigned char *addr, unsigned char *message, int accurac
|
||||
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;
|
||||
if(total_idat(addr) < strlen((char*)message)) {
|
||||
printf("Warning, message exceeds IDAT amount\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int idat_length = check_header_length(addr, offset);
|
||||
printf("IDAT Length: %d\n", idat_length);
|
||||
|
||||
int prop_found = 0;
|
||||
long size = 1;
|
||||
long rounds = 0;
|
||||
size_t idat_byte_length = 0;
|
||||
unsigned char* idat_data = calloc(size, sizeof(unsigned char));
|
||||
for(size_t i = 0; i <= idat_length; i++) {
|
||||
@@ -135,7 +185,6 @@ int change_idat_content(unsigned char *addr, unsigned char *message, int accurac
|
||||
idat_byte_length = i;
|
||||
}
|
||||
unsigned char temp_idat_data[idat_byte_length];
|
||||
while(prop_found == 0) {
|
||||
for(int i = 0; i <= idat_length; i++) {
|
||||
temp_idat_data[i] = idat_data[i];
|
||||
}
|
||||
@@ -146,45 +195,110 @@ int change_idat_content(unsigned char *addr, unsigned char *message, int accurac
|
||||
|
||||
random_data_change(uncom_data_buff, 16, uncom_data_size);
|
||||
|
||||
free(uncom_data_buff);
|
||||
// Compress Data
|
||||
unsigned char *com_data_buff;
|
||||
size_t com_data_size = 0;
|
||||
zlib_compress_data(uncom_data_buff, uncom_data_size, &com_data_buff, &com_data_size);
|
||||
|
||||
//printf("Found %d in %d rounds\n", checked_crc ,rounds);
|
||||
//printf("Full CRC: %08X\n", crcnum);
|
||||
//printf("Original: %02X\n", idat_data[j]);
|
||||
//printf("Change offset: %d to hex: %02X\n", j, temp_idat_data[j]);
|
||||
//addr[offset+8+j] = temp_idat_data[j];
|
||||
//update_file_crc(addr, offset, crcnum);
|
||||
prop_found = 1;
|
||||
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);
|
||||
|
||||
for(size_t i = 0; i < com_data_size; i++) {
|
||||
png_file->png_idat_data.idat_data[i] = com_data_buff[i];
|
||||
}
|
||||
|
||||
// Build PNG File
|
||||
build_png_file(png_file, out_file_name);
|
||||
|
||||
free(uncom_data_buff);
|
||||
free(com_data_buff);
|
||||
free(idat_data);
|
||||
free(png_file->png_idat_data.idat_data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// This is where it all starts
|
||||
int main() {
|
||||
int main(int argc, char **argv) {
|
||||
FILE *fp;
|
||||
size_t i = 0;
|
||||
unsigned long offset = 0;
|
||||
unsigned char *message = malloc(sizeof(char));
|
||||
message[0] = '\0';
|
||||
struct PNG_FILE_STRUCT png_file_data;
|
||||
char *in_file_name = NULL;
|
||||
char *out_file_name = NULL;
|
||||
char *message = NULL;
|
||||
|
||||
static const struct option long_options[] = {
|
||||
{"help", no_argument, NULL, 'h'},
|
||||
{"file", required_argument, NULL, 'f'},
|
||||
{"outfile", required_argument, NULL, 'o'},
|
||||
{"message", required_argument, NULL, 'm'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
const char* usage =
|
||||
"Usage: crc [options]\n"
|
||||
" -h, --help Shows help message\n"
|
||||
" -f, --file Denotes input file\n"
|
||||
" -o, --outfile Denotes output file\n"
|
||||
" -m, --message Encoded message\n"
|
||||
"\n";
|
||||
|
||||
int c;
|
||||
while (1) {
|
||||
int option_index = 0;
|
||||
c = getopt_long(argc, argv, "hf:o:m:", long_options ,&option_index);
|
||||
if(c == -1) {
|
||||
break;
|
||||
}
|
||||
switch(c) {
|
||||
case 'h':
|
||||
printf("%s", usage);
|
||||
exit(EXIT_SUCCESS);
|
||||
case 'f':
|
||||
in_file_name = optarg;
|
||||
break;
|
||||
case 'o':
|
||||
out_file_name = optarg;
|
||||
break;
|
||||
case 'm':
|
||||
message = optarg;
|
||||
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(sodium_init() == -1) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
fp = fopen("./1.png", "rt");
|
||||
fp = fopen(in_file_name, "rt");
|
||||
if (fp == NULL) {
|
||||
return EXIT_FAILURE;
|
||||
printf("File error\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
unsigned char *file_data = file_to_char_array(fp, &i);
|
||||
fclose(fp);
|
||||
|
||||
populate_start_png(file_data, &png_file_data.png_start_data);
|
||||
|
||||
offset = first_idat(file_data);
|
||||
change_idat_content(file_data, message, 1, offset);
|
||||
|
||||
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);
|
||||
free(message);
|
||||
//create_cc_file(file_data, i);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
/*
|
||||
* Code Implemented From: https://www.w3.org/TR/PNG/#D-CRCAppendix
|
||||
*/
|
||||
|
||||
/* Table of CRCs of all 8-bit messages. */
|
||||
unsigned long crc_table[256];
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#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);
|
||||
int change_idat_content(unsigned char *addr, unsigned char *message, int accuracy, unsigned long offset);
|
||||
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);
|
||||
|
||||
@@ -1,7 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#pragma once
|
||||
|
||||
// PNG File Struct
|
||||
struct PNG
|
||||
struct PNG_START_FILE_STRUCT {
|
||||
unsigned char file_sig[8];
|
||||
unsigned char ihdr_length[4];
|
||||
unsigned char ihdr_header[4];
|
||||
unsigned char file_width[4];
|
||||
unsigned char file_height[4];
|
||||
unsigned char bit_depth[1];
|
||||
unsigned char color_type[1];
|
||||
unsigned char compression_method[1];
|
||||
unsigned char filter_method[1];
|
||||
unsigned char interlace_method[1];
|
||||
unsigned char ihdr_crc[4];
|
||||
};
|
||||
|
||||
struct PNG_IDAT_FILE_STRUCT {
|
||||
union{
|
||||
unsigned char idat_length[4];
|
||||
uint32_t idat_data_length;
|
||||
};
|
||||
unsigned char idat_header[4];
|
||||
unsigned char idat_crc[4];
|
||||
unsigned char *idat_data;
|
||||
};
|
||||
|
||||
struct PNG_FILE_STRUCT {
|
||||
struct PNG_START_FILE_STRUCT png_start_data;
|
||||
struct PNG_IDAT_FILE_STRUCT png_idat_data;
|
||||
};
|
||||
|
||||
extern const long png_signature[8];
|
||||
|
||||
@@ -9,3 +39,5 @@ int 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);
|
||||
void populate_start_png(unsigned char *addr, struct PNG_START_FILE_STRUCT *png_data);
|
||||
void populate_idat_png(unsigned char *addr, struct PNG_IDAT_FILE_STRUCT *png_data, unsigned long offset);
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
|
||||
#define CHUNK 1024
|
||||
|
||||
/*
|
||||
* Built from exmaple provided in: https://zlib.net/zlib_how.html
|
||||
*/
|
||||
void zlib_decompress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz) {
|
||||
int ret;
|
||||
unsigned int have;
|
||||
@@ -68,6 +71,9 @@ void zlib_decompress_data(unsigned char *data_chunk, size_t file_length, unsigne
|
||||
fclose(of);
|
||||
}
|
||||
|
||||
/*
|
||||
* Built from exmaple provided in: https://zlib.net/zlib_how.html
|
||||
*/
|
||||
void zlib_compress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz) {
|
||||
int ret, flush;
|
||||
unsigned int have;
|
||||
|
||||
@@ -27,6 +27,34 @@ int check_file_header(char *addr) {
|
||||
|
||||
}
|
||||
|
||||
void populate_idat_png(unsigned char *addr, struct PNG_IDAT_FILE_STRUCT *png_data, unsigned long offset) {
|
||||
unsigned long cur_idat_length = 0;
|
||||
for(int i = 0; i < 4; i++){
|
||||
png_data->idat_length[i] = addr[i+offset];
|
||||
}
|
||||
|
||||
for(int i = 0; i < 4; i++){
|
||||
cur_idat_length += (png_data->idat_length[i] << (24-(8*i)));
|
||||
}
|
||||
|
||||
for(int i = 0; i < 4; i++){
|
||||
png_data->idat_header[i] = addr[i+offset+4];
|
||||
}
|
||||
}
|
||||
|
||||
void populate_start_png(unsigned char *addr, struct PNG_START_FILE_STRUCT *png_data) {
|
||||
union{
|
||||
unsigned char *data;
|
||||
struct PNG_START_FILE_STRUCT *png_data;
|
||||
}png;
|
||||
|
||||
png.png_data = png_data;
|
||||
|
||||
for(int i = 0; i < sizeof(struct PNG_START_FILE_STRUCT); i++) {
|
||||
png.data[i] = addr[i];
|
||||
}
|
||||
}
|
||||
|
||||
int create_cc_file(unsigned char *addr, unsigned long file_length) {
|
||||
FILE *fp;
|
||||
fp = fopen("png2.png", "w");
|
||||
|
||||
Reference in New Issue
Block a user