-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.c
52 lines (44 loc) · 916 Bytes
/
map.c
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
46
47
48
49
50
51
52
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "map.h"
/*
* map_file_ro - map a file into memory
*/
bool
map_file_ro(uint8_t **data, size_t *length, const char *fn)
{
struct stat status;
int fd;
*data = NULL;
*length = 0;
/* get and check file length */
if (stat(fn, &status) == -1)
return false;
if (status.st_size == 0)
return true;
if (status.st_size > SIZE_MAX) {
errno = EFBIG; /* file too large */
return false;
}
/* open file */
fd = open(fn, O_RDONLY);
if (fd == -1)
return false;
/* map file */
*data = mmap(NULL, (size_t)status.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
*length = (size_t)status.st_size;
close(fd);
return *data != MAP_FAILED;
}
bool
map_remove(uint8_t *data, size_t length)
{
if (length == 0)
return true;
return munmap(data, length) == 0;
}