#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // The file "testfile" should lead to this hash: fc982e558db259f298b43cd4c1241c66 void md2_checksum(size_t len, uint8_t* buf) { } void md2_hash(size_t len, const uint8_t buf[len], uint8_t out[16]) { } static uint8_t* read_file(const char* path, size_t* size) { // Read the contents of the file specified by path into a heap-allocated // buffer and return a pointer to that buffer. FILE* f = fopen(path, "r"); if(f == NULL) { printf("Fopen error: %d\n", errno); fclose(f); return NULL; } struct stat statOfFile; int status = fstat(fileno(f), &statOfFile); if(status == -1) { printf("Fstat error: %d\n", errno); fclose(f); return NULL; }; if((statOfFile.st_mode & S_IFMT) != S_IFREG){ printf("File is not a regular file!\n"); fclose(f); return NULL; } uint8_t* data = malloc(statOfFile.st_size); size_t bytesRead = fread(data, statOfFile.st_blksize, statOfFile.st_blocks, f); // Or: size_t bytesRead = fread(data, sizeof(char), statOfFile.st_size, f); if(bytesRead != 0 && !feof(f)) { printf("Error reading file!\n"); fclose(f); return NULL; } fclose(f); (*size) = bytesRead; return data; } int stringToInt(char* string) { char* leftover = ""; errno = 0; int x = strtold(string, &leftover); if(*leftover != '\0' || leftover == string) { errno = EINVAL; return -1; } if(errno == ERANGE) { printf("Number is too large or too small!\n"); errno = EINVAL; return -1; } errno = 0; return x; } void help(char* progname) { fprintf(stderr, "usage: %s [-V implementation] [-B [repetitons]] [-h] [--help] file\n", progname); } int main(int argc, char** argv) { int opt; int implementationTosUse = 0; int doBenchmark = 0; int benchmarkingCycles = 1; while (1) { static struct option longOptions[] = { /* These options set a flag. */ {"help", no_argument, NULL, 'h'}, NULL }; int longOptionIndex = 0; opt = getopt_long(argc, argv, "V:B::h", longOptions, &longOptionIndex); if(-1 == opt) break; switch (opt) { case 'B': doBenchmark = 1; if(optarg != NULL) { benchmarkingCycles = stringToInt(optarg); if(errno == 0) break; fprintf(stderr, "%s: invalid argument, has to be int -- 'B' got '%s'\n", argv[0], optarg); help(argv[0]); return EXIT_FAILURE; } break; case 'V': implementationTosUse = stringToInt(optarg); if(errno == 0) break; fprintf(stderr, "%s: invalid argument, has to be int -- 'V' got '%s'\n", argv[0], optarg); help(argv[0]); return EXIT_FAILURE; case 'h': help(argv[0]); return EXIT_SUCCESS; default: help(argv[0]); return EXIT_FAILURE; } } char* filename = ""; if(argc > optind) { filename = argv[optind]; } else { fprintf(stderr, "%s: missing poisional argument -- 'file'\n", argv[0]); help(argv[0]); return EXIT_FAILURE; } printf("Hashing file: %s\nUsing implementation: %d, doing benchmark: %d, benchmark cycles: %d\n", filename, implementationTosUse, doBenchmark, benchmarkingCycles); return 0; }