diff --git a/Implementierung/Makefile b/Implementierung/Makefile index d6b448b..ac2efc2 100644 --- a/Implementierung/Makefile +++ b/Implementierung/Makefile @@ -1,14 +1,14 @@ FLAGS=-O3 -std=c11 -g -Wall -Wextra -no-pie .PHONY: all - all: main -main: main.c toupper.S +all: main +main: main.c $(CC) $(CFLAGS) -o $@ $^ .PHONY: clean - clean: - rm -f main +clean: + rm -f main .PHONY: run - run: - ./main +run: + ./main diff --git a/Implementierung/main b/Implementierung/main new file mode 100755 index 0000000..c930f60 Binary files /dev/null and b/Implementierung/main differ diff --git a/Implementierung/main.c b/Implementierung/main.c index 0eafbf0..6b751f0 100644 --- a/Implementierung/main.c +++ b/Implementierung/main.c @@ -1,3 +1,151 @@ -int main(int argc, char* argv) { +#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) { + // 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); + + 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, 0, 'h'}, + {0, 0, 0, 0} + }; + 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; } diff --git a/Implementierung/testfile b/Implementierung/testfile new file mode 100644 index 0000000..d72e1a3 Binary files /dev/null and b/Implementierung/testfile differ