From 184fc5fac459d0ca4e98280e0dc9b9fa73912604 Mon Sep 17 00:00:00 2001 From: Pin Date: Fri, 15 Oct 2021 23:30:29 -0400 Subject: [PATCH] wip --- Makefile | 3 ++ cmd/crc.c | 80 +++++++++++++++++++++++++++++++++++++++++---------- include/crc.h | 4 +-- 3 files changed, 70 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index b06726e..9d3cb0d 100644 --- a/Makefile +++ b/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 diff --git a/cmd/crc.c b/cmd/crc.c index 5fc9efb..197b4c3 100644 --- a/cmd/crc.c +++ b/cmd/crc.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "crc_util.h" #include "CRCLib.h" #include "crc.h" @@ -106,10 +107,10 @@ void random_data_change(unsigned char *color_data, int width, int length) { } while(searching == 1); } -void build_png_file(struct PNG_FILE_STRUCT *png_file) { +void build_png_file(struct PNG_FILE_STRUCT *png_file, char *out_file_name) { FILE *fp; - fp = fopen("TESTPNG.png", "w"); + fp = fopen(out_file_name, "w"); union{ unsigned char data[sizeof(struct PNG_START_FILE_STRUCT)]; @@ -158,7 +159,7 @@ void build_png_file(struct PNG_FILE_STRUCT *png_file) { fclose(fp); } -int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, unsigned char *message, int accuracy, unsigned long offset) { +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; @@ -167,8 +168,8 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, u printf("Notice, this could take a long time..."); } if(total_idat(addr) < strlen((char*)message)) { - printf("Warning, message exceeds IDAT amount"); - return EXIT_FAILURE; + printf("Warning, message exceeds IDAT amount\n"); + exit(EXIT_FAILURE); } int idat_length = check_header_length(addr, offset); @@ -211,7 +212,7 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, u } // Build PNG File - build_png_file(png_file); + build_png_file(png_file, out_file_name); free(uncom_data_buff); free(com_data_buff); @@ -222,24 +223,74 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, u } // This is where it all starts -int main() { +int main(int argc, char **argv) { FILE *fp; size_t i = 0; unsigned long offset = 0; struct PNG_FILE_STRUCT png_file_data; - unsigned char *message = malloc(sizeof(char)); - message[0] = '\0'; + 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"); - // Written by Robert Zambito + fp = fopen(in_file_name, "rt"); if (fp == NULL) { - return EXIT_FAILURE; + printf("File error\n"); + exit(EXIT_FAILURE); } - // No longer written by Robert Zambito unsigned char *file_data = file_to_char_array(fp, &i); fclose(fp); @@ -251,8 +302,7 @@ int main() { populate_idat_png(file_data, &png_file_data.png_idat_data, offset); - change_idat_content(file_data, &png_file_data, message, 1, offset); + change_idat_content(file_data, &png_file_data, message, 1, offset, out_file_name); free(file_data); - free(message); } diff --git a/include/crc.h b/include/crc.h index 64ef786..d33f013 100644 --- a/include/crc.h +++ b/include/crc.h @@ -3,5 +3,5 @@ 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); -void build_png_file(struct PNG_FILE_STRUCT *png_file);;; -int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, 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);