Compare commits

...

1 Commits

Author SHA1 Message Date
Pin
702c44ad94 wip 2021-10-11 22:59:18 -04:00
9 changed files with 162 additions and 124 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,5 @@
*.png
*.out
*.woo
*.wow
bin/

16
Makefile Normal file
View File

@@ -0,0 +1,16 @@
LIBRARIES = `pkg-config --libs zlib libsodium` -Iinclude
SOURCES = ./src/* ./cmd/crc.c
OUTPUT_DIR = ./bin
OUTPUT = -o ${OUTPUT_DIR}/PROG
build: output_dir
gcc -Wall ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=crc}
debug: output_dir
gcc -Wall -g ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=crc}
output_dir:
mkdir -p ${OUTPUT_DIR}
clean:
rm -rf $(OUTPUT_DIR) **.h.gch

View File

@@ -1,7 +1,4 @@
#include <stdio.h>
#include <zlib.h>
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
@@ -9,129 +6,11 @@
#include "crc_util.h"
#include "CRCLib.h"
#include "crc.h"
#include "compress_util.h"
#define CHUNK 1024
const long idat_signature = 1229209940;
const long iend_signature = 1229278788;
void zlib_decompress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz) {
int ret;
unsigned int have;
z_stream strm;
unsigned char out[CHUNK];
unsigned char in[CHUNK];
errno=0;
FILE *data_stream = fmemopen(data_chunk, file_length, "r");
FILE *of = open_memstream((char**)buff, sz);
if(data_stream == NULL) {
perror("F MEM OPEN");
}
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit(&strm);
if(ret != Z_OK) {
return;
}
do {
strm.avail_in = fread(in, 1, CHUNK, data_stream);
if(ferror(data_stream)) {
(void)inflateEnd(&strm);
return;
}
if(strm.avail_in == 0) {
break;
}
strm.next_in = in;
do {
strm.avail_out = CHUNK;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR);
switch(ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR;
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
printf("Error: %d\n", ret);
printf("MSG: %s\n", (char*)strm.msg);
return;
}
have = CHUNK - strm.avail_out;
fwrite(out, 1, have, of);
} while(strm.avail_out == 0);
} while(ret != Z_STREAM_END);
(void)inflateEnd(&strm);
fclose(data_stream);
fclose(of);
}
void zlib_compress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz) {
int ret, flush;
unsigned int have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];
int level = 9;
int method = Z_DEFLATED;
int windowBits = 10;
int memLevel = 9;
//int strategy = Z_DEFAULT_STRATEGY;
int strategy = Z_FILTERED;
FILE *data_stream = fmemopen(data_chunk, file_length, "r");
FILE *out_data_stream = NULL;
out_data_stream = open_memstream((char**)buff, sz);
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy);
if (ret != Z_OK) {
return;
}
do {
strm.avail_in = fread(in, 1, CHUNK, data_stream);
if (ferror(data_stream)) {
(void)deflateEnd(&strm);
return;
}
flush = feof(data_stream) ? Z_FINISH : Z_NO_FLUSH;
strm.next_in = in;
do {
strm.avail_out = CHUNK;
strm.next_out = out;
ret = deflate(&strm, flush);
assert(ret != Z_STREAM_ERROR);
have = CHUNK - strm.avail_out;
if(fwrite(out, 1, have, out_data_stream) != have || ferror(out_data_stream)) {
(void)deflateEnd(&strm);
return;
}
} while(strm.avail_out == 0);
assert(strm.avail_in == 0);
} while(flush != Z_FINISH);
assert(ret == Z_STREAM_END);
fclose(data_stream);
fclose(out_data_stream);
(void)deflateEnd(&strm);
}
unsigned long first_idat(unsigned char *addr) {
int idat_found = 0;
unsigned long offset = 8;
@@ -177,6 +56,7 @@ int update_file_crc(unsigned char *addr, unsigned long offset , unsigned int crc
new_crc = crc_num >> (8*(3-i)) & 0xFF;
addr[startCRC+i] = new_crc;
}
return 0;
}
void random_data_change(unsigned char *color_data, int width, int length) {
@@ -224,7 +104,7 @@ void random_data_change(unsigned char *color_data, int width, int length) {
} while(searching == 1);
}
int change_idat_content(unsigned char *addr, char *message, int accuracy, unsigned long offset) {
int change_idat_content(unsigned char *addr, unsigned char *message, int accuracy, unsigned long offset) {
//printf("Starting IDAT Tranform\n");
if(accuracy > 4) {
printf("Warning, accuracy cannot be larger than 4");
@@ -282,6 +162,7 @@ int change_idat_content(unsigned char *addr, char *message, int accuracy, unsign
return 0;
}
// This is where it all starts
int main() {
FILE *fp;
size_t i = 0;
@@ -294,6 +175,9 @@ int main() {
}
fp = fopen("./1.png", "rt");
if (fp == NULL) {
return EXIT_FAILURE;
}
unsigned char *file_data = file_to_char_array(fp, &i);
fclose(fp);

8
include/compress_util.h Normal file
View File

@@ -0,0 +1,8 @@
#include <stdio.h>
#include <zlib.h>
#include <stddef.h>
#include <assert.h>
#include <errno.h>
void zlib_decompress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz);
void zlib_compress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz);

View File

@@ -3,4 +3,4 @@ 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, char *message, int accuracy, unsigned long offset);
int change_idat_content(unsigned char *addr, unsigned char *message, int accuracy, unsigned long offset);

126
src/compress_util.c Normal file
View File

@@ -0,0 +1,126 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include "compress_util.h"
#define CHUNK 1024
void zlib_decompress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz) {
int ret;
unsigned int have;
z_stream strm;
// Adding one to zero here
unsigned char out[CHUNK];
unsigned char in[CHUNK];
errno=0;
FILE *data_stream = fmemopen(data_chunk, file_length, "r");
FILE *of = open_memstream((char**)buff, sz);
if(data_stream == NULL) {
perror("F MEM OPEN");
}
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit(&strm);
if(ret != Z_OK) {
return;
}
do {
strm.avail_in = fread(in, 1, CHUNK, data_stream);
if(ferror(data_stream)) {
(void)inflateEnd(&strm);
return;
}
if(strm.avail_in == 0) {
break;
}
strm.next_in = in;
do {
strm.avail_out = CHUNK;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR);
switch(ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR;
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
printf("Error: %d\n", ret);
printf("MSG: %s\n", (char*)strm.msg);
return;
}
have = CHUNK - strm.avail_out;
fwrite(out, 1, have, of);
} while(strm.avail_out == 0);
} while(ret != Z_STREAM_END);
(void)inflateEnd(&strm);
fclose(data_stream);
fclose(of);
}
void zlib_compress_data(unsigned char *data_chunk, size_t file_length, unsigned char **buff, size_t *sz) {
int ret, flush;
unsigned int have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];
int level = 9;
int method = Z_DEFLATED;
int windowBits = 10;
int memLevel = 9;
//int strategy = Z_DEFAULT_STRATEGY;
int strategy = Z_FILTERED;
FILE *data_stream = fmemopen(data_chunk, file_length, "r");
FILE *out_data_stream = NULL;
out_data_stream = open_memstream((char**)buff, sz);
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy);
if (ret != Z_OK) {
return;
}
do {
strm.avail_in = fread(in, 1, CHUNK, data_stream);
if (ferror(data_stream)) {
(void)deflateEnd(&strm);
return;
}
flush = feof(data_stream) ? Z_FINISH : Z_NO_FLUSH;
strm.next_in = in;
do {
strm.avail_out = CHUNK;
strm.next_out = out;
ret = deflate(&strm, flush);
assert(ret != Z_STREAM_ERROR);
have = CHUNK - strm.avail_out;
if(fwrite(out, 1, have, out_data_stream) != have || ferror(out_data_stream)) {
(void)deflateEnd(&strm);
return;
}
} while(strm.avail_out == 0);
assert(strm.avail_in == 0);
} while(flush != Z_FINISH);
assert(ret == Z_STREAM_END);
fclose(data_stream);
fclose(out_data_stream);
(void)deflateEnd(&strm);
}

View File

@@ -39,6 +39,7 @@ int create_cc_file(unsigned char *addr, unsigned long file_length) {
fputc(addr[i], fp);
}
fclose(fp);
return 0;
}
unsigned char* file_to_char_array(FILE *in_file, size_t* size) {