Fix: File reading

This commit is contained in:
Dorian Zedler 2022-06-29 20:54:41 +02:00
parent 9133601088
commit 9f91eb3962
Signed by: dorian
GPG key ID: 989DE36109AFA354
5 changed files with 46 additions and 39 deletions

View file

@ -9,7 +9,8 @@
"optional": "c", "optional": "c",
"string_view": "c", "string_view": "c",
"system_error": "c", "system_error": "c",
"variant": "c" "variant": "c",
"array": "c"
}, },
"editor.formatOnSave": true "editor.formatOnSave": true
} }

View file

@ -2,6 +2,7 @@
#define IO_H #define IO_H
#include <errno.h> #include <errno.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -11,9 +12,10 @@
* @brief reads a file at a path * @brief reads a file at a path
* *
* @param path the filepath * @param path the filepath
* @param data where to store the pointer to the data
* @param size a pointer where the size of the result should be stored * @param size a pointer where the size of the result should be stored
* @return the raw data * @return the raw data
*/ */
uint8_t* read_file(const char* path, size_t* size); bool read_file(const char* path, uint8_t** data, size_t* size);
#endif // IO_H #endif // IO_H

View file

@ -1,13 +1,13 @@
#include "io.h" #include "io.h"
uint8_t* read_file(const char* path, size_t* size) { bool read_file(const char* path, uint8_t** data, size_t* size) {
// Read the contents of the file specified by path into a heap-allocated // Read the contents of the file specified by path into a heap-allocated
// buffer and return a pointer to that buffer. // buffer and return a pointer to that buffer.
FILE* f = fopen(path, "r"); FILE* f = fopen(path, "r");
if (f == NULL) { if (f == NULL) {
printf("Fopen error: %d\n", errno); printf("Fopen error: %d\n", errno);
fclose(f); fclose(f);
return NULL; return false;
} }
struct stat statOfFile; struct stat statOfFile;
@ -15,28 +15,28 @@ uint8_t* read_file(const char* path, size_t* size) {
if (status == -1) { if (status == -1) {
printf("Fstat error: %d\n", errno); printf("Fstat error: %d\n", errno);
fclose(f); fclose(f);
return NULL; return false;
}; };
if ((statOfFile.st_mode & S_IFMT) != S_IFREG) { if ((statOfFile.st_mode & S_IFMT) != S_IFREG) {
printf("File is not a regular file!\n"); printf("File is not a regular file!\n");
fclose(f); fclose(f);
return NULL; return false;
} }
uint8_t* data = malloc(statOfFile.st_size); (*data) = malloc(statOfFile.st_size);
size_t bytesRead = size_t bytesRead =
fread(data, statOfFile.st_blksize, statOfFile.st_blocks, f); fread((*data), statOfFile.st_blksize, statOfFile.st_blocks, f);
// Or: size_t bytesRead = fread(data, sizeof(char), statOfFile.st_size, f); // size_t bytesRead = fread(data, sizeof(uint8_t), statOfFile.st_size, f);
if (bytesRead != 0 && !feof(f)) { if (bytesRead != 0 && !feof(f)) {
printf("Error reading file!\n"); printf("Error reading file!\n");
fclose(f); fclose(f);
return NULL; return false;
} }
fclose(f); fclose(f);
(*size) = bytesRead; (*size) = statOfFile.st_size;
return data; return true;
} }

View file

@ -65,19 +65,23 @@ int main(int argc, char** argv) {
"benchmark cycles: %d\n", "benchmark cycles: %d\n",
c.filename, c.implementationToUse, c.doBenchmark, c.benchmarkingCycles); c.filename, c.implementationToUse, c.doBenchmark, c.benchmarkingCycles);
runTests(); // runTests();
/*size_t len; size_t len;
uint8_t* data = read_file(c.filename, &len); uint8_t* data;
if (!read_file(c.filename, &data, &len)) {
printf("Error reading file %s!", c.filename);
return EXIT_FAILURE;
}
printf("File read with size: %zu\n", len);
printf("\n");
uint8_t out[16]; uint8_t out[16];
const char* test = "abcdefghijklmnopqrstuvwxyz"; md2_hash(len, data, out);
len = strlen(test);
md2_hash(len, test, out);
printf("Hash: "); printf("Hash: ");
const char hash[16]; char hash[16];
encodeHash(out, &hash); encodeHash(out, hash);
printf("%s\n", hash);*/ printf("%s\n", hash);
return 0; return 0;
} }

View file

@ -55,7 +55,7 @@ void md2_hash(size_t len, const uint8_t buf[len], uint8_t out[16]) {
uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t)); uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t));
memcpy(newBuf, buf, len - paddingNeeded); memcpy(newBuf, buf, len - paddingNeeded);
printBuf(len + 16, newBuf); // printBuf(len + 16, newBuf);
while (paddingNeeded > 0) { while (paddingNeeded > 0) {
newBuf[len - paddingNeeded] = originalPadding; newBuf[len - paddingNeeded] = originalPadding;