Chore: Add cmake, split helper, add .vscode

This commit is contained in:
Dorian Zedler 2022-06-29 10:50:55 +02:00
parent 3abf1b9b7e
commit 9ea5de7c07
Signed by: dorian
GPG Key ID: 989DE36109AFA354
10 changed files with 227 additions and 166 deletions

5
.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"recommendations": [
"ms-vscode.cpptools"
]
}

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"C_Cpp.clang_format_fallbackStyle": "Google",
"files.associations": {
"functional": "c"
}
}

5
Implementierung/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
Makefile
cmake_install.cmake
CMakeCache.txt
md2
CMakeFiles

View File

@ -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
)

View File

@ -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

View File

@ -0,0 +1,20 @@
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
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);

Binary file not shown.

View File

@ -1,152 +0,0 @@
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <xmmintrin.h>
#include <errno.h>
#include <immintrin.h>
#include <stdint.h>
#include <getopt.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 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;
}

View File

@ -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;
}

View File

@ -0,0 +1,87 @@
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <xmmintrin.h>
#include <errno.h>
#include <immintrin.h>
#include <stdint.h>
#include <getopt.h>
#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;
}