diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..5a9a397 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "ms-vscode.cpptools" + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..8aae49a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "C_Cpp.clang_format_fallbackStyle": "Google", + "files.associations": { + "functional": "c" + } +} \ No newline at end of file diff --git a/Implementierung/.gitignore b/Implementierung/.gitignore new file mode 100644 index 0000000..a811ad3 --- /dev/null +++ b/Implementierung/.gitignore @@ -0,0 +1,5 @@ +Makefile +cmake_install.cmake +CMakeCache.txt +md2 +CMakeFiles \ No newline at end of file diff --git a/Implementierung/CMakeLists.txt b/Implementierung/CMakeLists.txt new file mode 100644 index 0000000..91d978d --- /dev/null +++ b/Implementierung/CMakeLists.txt @@ -0,0 +1,17 @@ +cmake_minimum_required(VERSION 2.6.0) + +# here we specify that the project is C language only, so the default +# C compiler on the system will be used +project(md2 C) +add_executable(md2 "") + +set(SOURCE_FILES + src/main.c + src/helper.c +) + +target_sources(md2 PRIVATE ${SOURCE_FILES}) + +target_include_directories(md2 PUBLIC + lib +) \ No newline at end of file diff --git a/Implementierung/Makefile b/Implementierung/Makefile deleted file mode 100644 index ac2efc2..0000000 --- a/Implementierung/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -FLAGS=-O3 -std=c11 -g -Wall -Wextra -no-pie - -.PHONY: all -all: main -main: main.c - $(CC) $(CFLAGS) -o $@ $^ - -.PHONY: clean -clean: - rm -f main - -.PHONY: run -run: - ./main diff --git a/Implementierung/lib/helper.h b/Implementierung/lib/helper.h new file mode 100644 index 0000000..5495952 --- /dev/null +++ b/Implementierung/lib/helper.h @@ -0,0 +1,20 @@ +#include +#include +#include +#include +#include + +enum argumentParseResult { + RESULT_OK = 0, + RESULT_EXIT_SUCCESS, + RESULT_EXIT_FAILURE +}; + +struct configuration { + char* filename; + int implementationToUse; + int doBenchmark; + int benchmarkingCycles; +}; + +enum argumentParseResult parseArguments(int argc, char** argv, struct configuration* c); \ No newline at end of file diff --git a/Implementierung/main b/Implementierung/main deleted file mode 100755 index 0f5f143..0000000 Binary files a/Implementierung/main and /dev/null differ diff --git a/Implementierung/main.c b/Implementierung/main.c deleted file mode 100644 index e70fa4c..0000000 --- a/Implementierung/main.c +++ /dev/null @@ -1,152 +0,0 @@ -#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; -} diff --git a/Implementierung/src/helper.c b/Implementierung/src/helper.c new file mode 100644 index 0000000..2c9a519 --- /dev/null +++ b/Implementierung/src/helper.c @@ -0,0 +1,87 @@ +#include "helper.h" + +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); +} + +enum argumentParseResult parseArguments(int argc, char **argv, struct configuration* c) { + if(c == NULL) { + return RESULT_EXIT_FAILURE; + } + + c->implementationToUse = 0; + c->doBenchmark = 0; + c->benchmarkingCycles = 1; + + int opt; + + while (1) { + static struct option longOptions[] = {{"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': + c->doBenchmark = 1; + if (optarg != NULL) { + c->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 RESULT_EXIT_FAILURE; + } + break; + case 'V': + c->implementationToUse = 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 RESULT_EXIT_FAILURE; + + case 'h': + help(argv[0]); + return RESULT_EXIT_SUCCESS; + + default: + help(argv[0]); + return RESULT_EXIT_FAILURE; + } + } + + if (argc > optind) { + c->filename = argv[optind]; + } else { + fprintf(stderr, "%s: missing poisional argument -- 'file'\n", argv[0]); + help(argv[0]); + return RESULT_EXIT_FAILURE; + } + + return RESULT_OK; +} \ No newline at end of file diff --git a/Implementierung/src/main.c b/Implementierung/src/main.c new file mode 100644 index 0000000..b20a89a --- /dev/null +++ b/Implementierung/src/main.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "helper.h" + +// 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 main(int argc, char** argv) { + + struct configuration c; + enum argumentParseResult result = parseArguments(argc, argv, &c); + + switch (result) + { + case RESULT_EXIT_SUCCESS: + return EXIT_SUCCESS; + case RESULT_EXIT_FAILURE: + return EXIT_FAILURE; + default: + break; + } + + printf("Hashing file: %s\nUsing implementation: %d, doing benchmark: %d, benchmark cycles: %d\n", c.filename, c.implementationToUse, c.doBenchmark, c.benchmarkingCycles); + + return 0; +}