Chore: make signatuure cleaner
This commit is contained in:
parent
f7e2dfe133
commit
2d9d051c0c
4 changed files with 19 additions and 13 deletions
|
@ -14,10 +14,9 @@
|
||||||
* @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 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
|
#endif // IO_H
|
|
@ -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);
|
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);
|
void encodeHash(uint8_t hash[16], char* stringHash);
|
||||||
|
|
||||||
#endif // MD2_H
|
#endif // MD2_H
|
|
@ -1,13 +1,13 @@
|
||||||
#include "../lib/io.h"
|
#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
|
// 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 false;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct stat statOfFile;
|
struct stat statOfFile;
|
||||||
|
@ -15,28 +15,28 @@ bool read_file(const char* path, uint8_t** data, 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 false;
|
return NULL;
|
||||||
};
|
};
|
||||||
|
|
||||||
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 false;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
(*data) = malloc(statOfFile.st_size);
|
uint8_t* 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);
|
||||||
// size_t bytesRead = fread(data, sizeof(uint8_t), 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 false;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
(*size) = statOfFile.st_size;
|
(*size) = statOfFile.st_size;
|
||||||
return true;
|
return data;
|
||||||
}
|
}
|
|
@ -67,11 +67,12 @@ int main(int argc, char** argv) {
|
||||||
|
|
||||||
// runTests();
|
// runTests();
|
||||||
size_t len;
|
size_t len;
|
||||||
uint8_t* data;
|
uint8_t* data = read_file(c.filename, &len);
|
||||||
if (!read_file(c.filename, &data, &len)) {
|
if (data == NULL) {
|
||||||
printf("Error reading file %s!", c.filename);
|
printf("Error reading file %s!", c.filename);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("File read with size: %zu\n", len);
|
printf("File read with size: %zu\n", len);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue