Fix: File reading
This commit is contained in:
parent
9133601088
commit
9f91eb3962
5 changed files with 46 additions and 39 deletions
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
|
@ -9,7 +9,8 @@
|
|||
"optional": "c",
|
||||
"string_view": "c",
|
||||
"system_error": "c",
|
||||
"variant": "c"
|
||||
"variant": "c",
|
||||
"array": "c"
|
||||
},
|
||||
"editor.formatOnSave": true
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define IO_H
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -11,9 +12,10 @@
|
|||
* @brief reads a file at a path
|
||||
*
|
||||
* @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
|
||||
* @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
|
|
@ -1,13 +1,13 @@
|
|||
#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
|
||||
// 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;
|
||||
return false;
|
||||
}
|
||||
|
||||
struct stat statOfFile;
|
||||
|
@ -15,28 +15,28 @@ uint8_t* read_file(const char* path, size_t* size) {
|
|||
if (status == -1) {
|
||||
printf("Fstat error: %d\n", errno);
|
||||
fclose(f);
|
||||
return NULL;
|
||||
return false;
|
||||
};
|
||||
|
||||
if ((statOfFile.st_mode & S_IFMT) != S_IFREG) {
|
||||
printf("File is not a regular file!\n");
|
||||
fclose(f);
|
||||
return NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t* data = malloc(statOfFile.st_size);
|
||||
(*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);
|
||||
fread((*data), statOfFile.st_blksize, statOfFile.st_blocks, f);
|
||||
// size_t bytesRead = fread(data, sizeof(uint8_t), statOfFile.st_size, f);
|
||||
if (bytesRead != 0 && !feof(f)) {
|
||||
printf("Error reading file!\n");
|
||||
fclose(f);
|
||||
return NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
|
||||
(*size) = bytesRead;
|
||||
return data;
|
||||
(*size) = statOfFile.st_size;
|
||||
return true;
|
||||
}
|
|
@ -65,19 +65,23 @@ int main(int argc, char** argv) {
|
|||
"benchmark cycles: %d\n",
|
||||
c.filename, c.implementationToUse, c.doBenchmark, c.benchmarkingCycles);
|
||||
|
||||
runTests();
|
||||
/*size_t len;
|
||||
uint8_t* data = read_file(c.filename, &len);
|
||||
// runTests();
|
||||
size_t 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];
|
||||
const char* test = "abcdefghijklmnopqrstuvwxyz";
|
||||
len = strlen(test);
|
||||
md2_hash(len, test, out);
|
||||
md2_hash(len, data, out);
|
||||
|
||||
printf("Hash: ");
|
||||
const char hash[16];
|
||||
encodeHash(out, &hash);
|
||||
printf("%s\n", hash);*/
|
||||
char hash[16];
|
||||
encodeHash(out, hash);
|
||||
printf("%s\n", hash);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
memcpy(newBuf, buf, len - paddingNeeded);
|
||||
|
||||
printBuf(len + 16, newBuf);
|
||||
// printBuf(len + 16, newBuf);
|
||||
|
||||
while (paddingNeeded > 0) {
|
||||
newBuf[len - paddingNeeded] = originalPadding;
|
||||
|
|
Loading…
Reference in a new issue