Chore: split io and md2

This commit is contained in:
Dorian Zedler 2022-06-29 10:58:49 +02:00
parent 9ea5de7c07
commit 4d2e3e16e3
Signed by: dorian
GPG key ID: 989DE36109AFA354
9 changed files with 85 additions and 68 deletions

View file

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

View file

@ -8,6 +8,8 @@ add_executable(md2 "")
set(SOURCE_FILES
src/main.c
src/helper.c
src/io.c
src/md2.c
)
target_sources(md2 PRIVATE ${SOURCE_FILES})

View file

@ -1,3 +1,6 @@
#ifndef HELPER_H
#define HELPER_H
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
@ -17,4 +20,6 @@ struct configuration {
int benchmarkingCycles;
};
enum argumentParseResult parseArguments(int argc, char** argv, struct configuration* c);
enum argumentParseResult parseArguments(int argc, char** argv, struct configuration* c);
#endif // HELPER_H

10
Implementierung/lib/io.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef IO_H
#define IO_H
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#endif // IO_H

View file

@ -0,0 +1,6 @@
#ifndef MD2_H
#define MD2_H
#include <stdint.h>
#endif // MD2_H

41
Implementierung/src/io.c Normal file
View file

@ -0,0 +1,41 @@
#include "io.h"
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;
}

View file

@ -1,70 +1,6 @@
#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;
}
#include "io.h"
#include "md2.h"
int main(int argc, char** argv) {

11
Implementierung/src/md2.c Normal file
View file

@ -0,0 +1,11 @@
#include "io.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]) {
}

View file

@ -7,3 +7,8 @@ Beachten Sie, dass die äußere Form und die Einhaltung der Formalitäten wichti
- `Implementierung/`: Ihre Implementierung
- `Implementierung/Makefile`: Makefile für Ihre Implementierung, welches durch einen Aufruf von `make` Ihre Implementierung kompiliert
- `Vortrag/Vortrag.pdf`: Folien für Ihre Abschlusspräsentation
# Kompilieren
- CMake ausführen: `cmake .`
- Make ausführen: `make`