Blame src/main/pe_map_raw_image.c

c0fbae
#include <stdint.h>
c0fbae
#include <unistd.h>
c0fbae
#include <fcntl.h>
c0fbae
#include <limits.h>
c0fbae
#include <errno.h>
c0fbae
#include <sys/mman.h>
c0fbae
#include <sys/types.h>
c0fbae
#include <sys/stat.h>
c0fbae
c0fbae
#include <perk/perk.h>
c0fbae
2b71e8
int pe_map_raw_image (int fd, const char * fname, int prot, struct pe_raw_image * map)
c0fbae
{
c0fbae
	struct stat stat;
c0fbae
	int nfd, ret;
c0fbae
c0fbae
	if ((nfd = !fd))
c0fbae
		fd  = open(fname,O_RDONLY | O_CLOEXEC);
c0fbae
c0fbae
	if ((fd < 0) || (fstat(fd,&stat) < 0))
c0fbae
		return errno;
c0fbae
c0fbae
	map->size = stat.st_size;
664d5b
	map->addr = mmap(0,map->size,prot,MAP_PRIVATE,fd,0);
c0fbae
c0fbae
	if (map->addr == MAP_FAILED) {
c0fbae
		map->addr = 0;
c0fbae
		ret = PERK_MAP_ERROR;
c0fbae
	} else
c0fbae
		ret = 0;
c0fbae
c0fbae
	if (nfd) close(fd);
c0fbae
c0fbae
	return ret;
c0fbae
}
c0fbae
c0fbae
int pe_unmap_raw_image (struct pe_raw_image * map)
c0fbae
{
c0fbae
	return munmap(map->addr, map->size);
b5f7f5
}