-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.c
More file actions
45 lines (38 loc) · 881 Bytes
/
Copy pathutils.c
File metadata and controls
45 lines (38 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "malloc.h"
#include "types.h"
#include "fatfs/ff.h"
#include "log.h"
void *load_file(const char *fname, u32 max_allowed, u32 *read) {
FRESULT res;
FIL fd;
FILINFO stat;
res = f_stat(fname, &stat);
if (res != FR_OK) {
log_printf("failed to stat %s: %d\n", fname, res);
return NULL;
}
res = f_open(&fd, fname, FA_READ);
if (res != FR_OK) {
log_printf("failed to open %s: %d\n", fname, res);
return NULL;
}
DWORD fsize = stat.fsize;
if (fsize > max_allowed) {
fsize = max_allowed;
log_printf("truncating %s to %d bytes\n", fname, fsize);
}
void *mem = malloc(fsize);
if (!mem) {
log_printf("failed to allocate %d bytes\n", fsize);
return NULL;
}
res = f_read(&fd, mem, fsize, read);
if (res != FR_OK) {
log_printf("failed to read %s: %d\n", fname, res);
free(mem);
f_close(&fd);
return NULL;
}
f_close(&fd);
return mem;
}