Implemented file reader function
This commit is contained in:
43
src/utils.c
43
src/utils.c
@@ -2,9 +2,12 @@
|
||||
// Created by marcsello on 05/11/2020.
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
bool contains(const uint8_t* data, uint64_t data_len, uint8_t what) {
|
||||
|
||||
bool contains(const uint8_t *data, uint64_t data_len, uint8_t what) {
|
||||
for (uint64_t i = 0; i < data_len; i++) {
|
||||
if (data[i] == what) {
|
||||
return true;
|
||||
@@ -12,3 +15,41 @@ bool contains(const uint8_t* data, uint64_t data_len, uint8_t what) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t read_file_to_mem(mm_ctx context, const char *filename, uint64_t max_len, uint8_t **data, uint64_t *data_len) {
|
||||
FILE *fp = fopen(filename, "rb");
|
||||
|
||||
if (fp == NULL) {
|
||||
return FILE_READ_COULD_NOT_OPEN;
|
||||
}
|
||||
|
||||
fseek(fp, 0L, SEEK_END);
|
||||
uint64_t fsize = ftell(fp);
|
||||
|
||||
if (fsize > max_len) {
|
||||
fclose(fp);
|
||||
return FILE_READ_TOO_BIG;
|
||||
}
|
||||
|
||||
fseek(fp, 0L, SEEK_SET);
|
||||
|
||||
uint8_t *contents = (uint8_t *) magic_malloc(context, fsize);
|
||||
|
||||
if (contents == NULL) {
|
||||
fclose(fp);
|
||||
return FILE_READ_COULD_NOT_ALLOCATE;
|
||||
}
|
||||
|
||||
uint64_t bytes_read = fread(contents, 1, fsize, fp);
|
||||
|
||||
if (bytes_read != fsize) {
|
||||
fclose(fp);
|
||||
return FILE_READ_ERROR;
|
||||
}
|
||||
|
||||
*data = contents;
|
||||
*data_len = fsize;
|
||||
|
||||
fclose(fp);
|
||||
return FILE_READ_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user