diff --git a/.gitignore b/.gitignore index dcfc80f..961fffd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ *.png *.out +*.woo +*.wow +bin/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b06726e --- /dev/null +++ b/Makefile @@ -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 diff --git a/crc.c b/cmd/crc.c similarity index 62% rename from crc.c rename to cmd/crc.c index 50f3b8b..3cee0f5 100644 --- a/crc.c +++ b/cmd/crc.c @@ -1,7 +1,4 @@ #include -#include -#include -#include #include #include #include @@ -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; diff --git a/CRCLib.h b/include/CRCLib.h similarity index 100% rename from CRCLib.h rename to include/CRCLib.h diff --git a/include/compress_util.h b/include/compress_util.h new file mode 100644 index 0000000..f0c623f --- /dev/null +++ b/include/compress_util.h @@ -0,0 +1,8 @@ +#include +#include +#include +#include +#include + +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); diff --git a/crc.h b/include/crc.h similarity index 62% rename from crc.h rename to include/crc.h index 3673550..acaba82 100644 --- a/crc.h +++ b/include/crc.h @@ -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); diff --git a/crc_util.h b/include/crc_util.h similarity index 100% rename from crc_util.h rename to include/crc_util.h diff --git a/src/compress_util.c b/src/compress_util.c new file mode 100644 index 0000000..ebf3663 --- /dev/null +++ b/src/compress_util.c @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#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); +} diff --git a/crc_util.c b/src/crc_util.c similarity index 99% rename from crc_util.c rename to src/crc_util.c index 037578c..5df74ca 100644 --- a/crc_util.c +++ b/src/crc_util.c @@ -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) {