Blame src/logic/pe_map_raw_image.c

e2e2c2
/***************************************************************/
e2e2c2
/*  perk: PE Resource Kit                                      */
e2e2c2
/*  Copyright (C) 2015--2016  Z. Gilboa                        */
e2e2c2
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
e2e2c2
/***************************************************************/
e2e2c2
c0fbae
#include <stdint.h>
fa22b4
#include <stdbool.h>
c0fbae
#include <unistd.h>
c0fbae
#include <fcntl.h>
c0fbae
#include <sys/mman.h>
c0fbae
#include <sys/types.h>
c0fbae
#include <sys/stat.h>
c0fbae
c0fbae
#include <perk/perk.h>
c0fbae
2b7f36
int pe_map_raw_image(
2b7f36
	int			fd,
2b7f36
	const char *		path,
2b7f36
	int			prot,
2b7f36
	struct pe_raw_image *	map)
c0fbae
{
fa22b4
	struct stat	stat;
fa22b4
	bool		fnew;
b46e6f
	int		ret;
c0fbae
e62c17
	if ((fnew = (fd < 0)))
e02651
		fd  = open(path,O_RDONLY | O_CLOEXEC);
c0fbae
b46e6f
	if (fd < 0)
b46e6f
		return -1;
b46e6f
b46e6f
	if ((ret = fstat(fd,&stat) < 0) && fnew)
b46e6f
		close(fd);
b46e6f
b46e6f
	if (ret < 0)
fa22b4
		return -1;
c0fbae
c0fbae
	map->size = stat.st_size;
664d5b
	map->addr = mmap(0,map->size,prot,MAP_PRIVATE,fd,0);
c0fbae
fa22b4
	if (fnew)
fa22b4
		close(fd);
c0fbae
fa22b4
	return (map->addr == MAP_FAILED) ? -1 : 0;
c0fbae
}
c0fbae
2b7f36
int pe_unmap_raw_image(struct pe_raw_image * map)
c0fbae
{
fa22b4
	return munmap(map->addr,map->size);
b5f7f5
}