Chore: make signatuure cleaner

This commit is contained in:
Dorian Zedler 2022-06-29 22:02:18 +02:00
parent f7e2dfe133
commit 2d9d051c0c
Signed by: dorian
GPG key ID: 989DE36109AFA354
4 changed files with 19 additions and 13 deletions

View file

@ -14,10 +14,9 @@
* @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
* @return pointer to the raw data or NULL on errors
*/
bool read_file(const char* path, uint8_t** data, size_t* size);
uint8_t* read_file(const char* path, size_t* size);
#endif // IO_H

View file

@ -25,6 +25,12 @@ void md2_hash(size_t len, const uint8_t buf[], uint8_t out[16]);
*/
void md2_checksum(size_t len, uint8_t* buf);
/**
* @brief Takes a uint8_t array and converts it into a string
*
* @param hash the array containing the hash
* @param stringHash pointer to store the string, has to have 32 bytes space
*/
void encodeHash(uint8_t hash[16], char* stringHash);
#endif // MD2_H

View file

@ -1,13 +1,13 @@
#include "../lib/io.h"
bool read_file(const char* path, uint8_t** data, size_t* size) {
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 false;
return NULL;
}
struct stat statOfFile;
@ -15,28 +15,28 @@ bool read_file(const char* path, uint8_t** data, size_t* size) {
if (status == -1) {
printf("Fstat error: %d\n", errno);
fclose(f);
return false;
return NULL;
};
if ((statOfFile.st_mode & S_IFMT) != S_IFREG) {
printf("File is not a regular file!\n");
fclose(f);
return false;
return NULL;
}
(*data) = malloc(statOfFile.st_size);
uint8_t* data = malloc(statOfFile.st_size);
size_t bytesRead =
fread((*data), statOfFile.st_blksize, statOfFile.st_blocks, 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 false;
return NULL;
}
fclose(f);
(*size) = statOfFile.st_size;
return true;
return data;
}

View file

@ -67,11 +67,12 @@ int main(int argc, char** argv) {
// runTests();
size_t len;
uint8_t* data;
if (!read_file(c.filename, &data, &len)) {
uint8_t* data = read_file(c.filename, &len);
if (data == NULL) {
printf("Error reading file %s!", c.filename);
return EXIT_FAILURE;
}
printf("File read with size: %zu\n", len);
printf("\n");