Blame src/logic/pe_map_raw_image.c
|
|
e2e2c2 |
/***************************************************************/
|
|
|
e2e2c2 |
/* perk: PE Resource Kit */
|
|
|
ced38a |
/* Copyright (C) 2015--2017 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>
|
|
|
9a46b6 |
#include "perk_errinfo_impl.h"
|
|
|
c0fbae |
|
|
|
2b7f36 |
int pe_map_raw_image(
|
|
|
9a46b6 |
const struct pe_driver_ctx * dctx,
|
|
|
9a46b6 |
int fd,
|
|
|
9a46b6 |
const char * path,
|
|
|
9a46b6 |
int prot,
|
|
|
9a46b6 |
struct pe_raw_image * map)
|
|
|
c0fbae |
{
|
|
|
ce59ae |
struct stat st;
|
|
|
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)
|
|
|
9a46b6 |
return PERK_SYSTEM_ERROR(dctx);
|
|
|
b46e6f |
|
|
|
ce59ae |
if ((ret = fstat(fd,&st) < 0) && fnew)
|
|
|
b46e6f |
close(fd);
|
|
|
b46e6f |
|
|
|
841928 |
else if ((st.st_size == 0) && fnew)
|
|
|
841928 |
close(fd);
|
|
|
841928 |
|
|
|
b46e6f |
if (ret < 0)
|
|
|
9a46b6 |
return PERK_SYSTEM_ERROR(dctx);
|
|
|
c0fbae |
|
|
|
841928 |
else if (st.st_size == 0)
|
|
|
acad6b |
return PERK_CUSTOM_ERROR(
|
|
|
acad6b |
dctx,PERK_ERR_IMAGE_SIZE_ZERO);
|
|
|
841928 |
|
|
|
ce59ae |
map->size = st.st_size;
|
|
|
664d5b |
map->addr = mmap(0,map->size,prot,MAP_PRIVATE,fd,0);
|
|
|
c0fbae |
|
|
|
fa22b4 |
if (fnew)
|
|
|
fa22b4 |
close(fd);
|
|
|
c0fbae |
|
|
|
9a46b6 |
return (map->addr == MAP_FAILED)
|
|
|
9a46b6 |
? PERK_SYSTEM_ERROR(dctx)
|
|
|
9a46b6 |
: 0;
|
|
|
c0fbae |
}
|
|
|
c0fbae |
|
|
|
2b7f36 |
int pe_unmap_raw_image(struct pe_raw_image * map)
|
|
|
c0fbae |
{
|
|
|
fa22b4 |
return munmap(map->addr,map->size);
|
|
|
b5f7f5 |
}
|