#include // printf #include // memcpy #include // getopt #ifdef USE_SYSLOG #include #endif #include "hasher.h" #include "log.h" #include "main.h" #define PATHBUF 4097 #define BUFLEN 2048 int run_verbose = 0; int run_terse = 0; int force_print_parameters = 0; void print_help(void){ printf("%s %s\n", TITLE, VERSION); printf("%s %s %s\n", USAGE0, PROGNAME, USAGE1); printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", OPTIONS0, OPTIONS1, OPTIONS2, OPTIONS3, OPTIONS4, OPTIONS5, OPTIONS6, OPTIONS7, OPTIONS8, OPTIONS9, OPTIONS10); } int main(int argc, char *const argv[]){ #ifdef USE_SYSLOG openlog(PROGNAME, 0, LOG_USER); #endif char *filepath = (char*)malloc(sizeof(char) * PATHBUF); int opt; while((opt = getopt(argc, argv, "vhtpf:")) != -1) { switch(opt){ case 'v': run_verbose = 1; break; case 'h': print_help(); return 0; case 't': run_terse = 1; break; case 'p': force_print_parameters = 1; break; case 'f': memcpy(filepath, optarg, strlen(optarg)); break; default: printf("Literally how could you pass an unspecified argument to this program"); } } /** Print run parameters at startup */ if(force_print_parameters) { if(run_terse && run_verbose){ printf(PROGNAME " called with arguments to run both tersely and verbosely, the author legitimately cannot tell what you want her to do\n"); } else if(run_terse) { printf(PROGNAME " will run tersely\n"); } else if(run_verbose) { printf(PROGNAME " will run verbosely\n"); } if(!run_terse){ if(filepath != NULL){ printf(PROGNAME " will read input from file %s\n", filepath); } else { printf(PROGNAME " will read input from standard input\n"); } } } if(init_hasher()){ log(LOG_INFO, "Error encountered while initializing hasher"); } begin_digest(); /* Set up input buffers */ // unsigned char * in stores the unsigned version of signin unsigned char * in = (unsigned char*)malloc(sizeof(unsigned char) * BUFLEN); char * signin = (char*)malloc(sizeof(char) * BUFLEN); /* End set up buffers*/ /* Set up the input stream Try to open file pointed to by filepath, or else use stdin */ FILE * infile; if(strlen(filepath) >= 0){ log(LOG_DEBUG, "Trying to open file %s for input", filepath); infile = fopen(filepath, "r"); } if(infile == NULL){ infile = stdin; } /* End set up the input stream */ /* Give blocks of data to the message digest algorithm. */ size_t bytesdigested = 0; unsigned int round = 0; while(!feof(infile) && !ferror(infile)){ round++; size_t bytesread = fread(signin, sizeof(char), sizeof(char) * BUFLEN, infile); if(bytesread==0){ printf("0 bytes read, breaking"); break; } memcpy(in, signin, bytesread); digest_part(in, bytesread); bytesdigested += bytesread; verb("Passed %lu bytes to digest algorithm in round %u, %lu bytes passed so far", bytesread, round, bytesdigested); } /* End give blocks of data to the message digest algorithm */ printf("Input:\n\t"); for(int i = 0; i < bytesdigested; i++){ printf("%02x ", in[i]); } printf("\n\t"); for(int i = 0; i < bytesdigested; i++){ printf(" %c ", in[i]); } printf("\n"); unsigned int len; unsigned char * a = get_digest(&len); char * aa = (char*)malloc(sizeof(char) * 2 * len); printf("Output:\n\t"); for(int i = 0; i < len; i++){ sprintf(aa + (2 * i), "%02x", a[i]); } printf("%s\n", aa); OPENSSL_free(a); free(aa); if(deinit_hasher()){ log(LOG_INFO, "Error encountered while deinitializing hasher"); } log(LOG_INFO, "hi"); #ifdef USE_SYSLOG closelog(); #endif if(infile != stdin){ fclose(infile); } return 0; }