|
|
02e59c |
/**************************************************************/
|
|
|
02e59c |
/* treebnf: a tree oriented bnf library */
|
|
|
02e59c |
/* Copyright (C) 2024 SysDeer Technologies, LLC */
|
|
|
02e59c |
/* Released under GPLv2 and GPLv3; see COPYING.TREEBNF. */
|
|
|
02e59c |
/**************************************************************/
|
|
|
02e59c |
|
|
|
02e59c |
#include <stdint.h>
|
|
|
02e59c |
#include <stdbool.h>
|
|
|
02e59c |
#include <unistd.h>
|
|
|
02e59c |
#include <fcntl.h>
|
|
|
02e59c |
#include <sys/mman.h>
|
|
|
02e59c |
#include <sys/types.h>
|
|
|
02e59c |
#include <sys/stat.h>
|
|
|
02e59c |
|
|
|
02e59c |
#include <treebnf/treebnf.h>
|
|
|
02e59c |
#include "treebnf_driver_impl.h"
|
|
|
02e59c |
#include "treebnf_errinfo_impl.h"
|
|
|
02e59c |
|
|
|
02e59c |
int tbnf_lib_map_raw_input(
|
|
|
02e59c |
const struct tbnf_driver_ctx * dctx,
|
|
|
02e59c |
int fd,
|
|
|
02e59c |
const char * path,
|
|
|
02e59c |
int prot,
|
|
|
02e59c |
struct tbnf_raw_input * map)
|
|
|
02e59c |
{
|
|
|
02e59c |
int ret;
|
|
|
02e59c |
struct stat st;
|
|
|
02e59c |
bool fnew;
|
|
|
02e59c |
int fdcwd;
|
|
|
02e59c |
|
|
|
02e59c |
fdcwd = tbnf_driver_fdcwd(dctx);
|
|
|
02e59c |
|
|
|
02e59c |
if ((fnew = (fd < 0)))
|
|
|
02e59c |
fd = openat(fdcwd,path,O_RDONLY | O_CLOEXEC);
|
|
|
02e59c |
|
|
|
02e59c |
if (fd < 0)
|
|
|
02e59c |
return TBNF_SYSTEM_ERROR(dctx,path);
|
|
|
02e59c |
|
|
|
02e59c |
if ((ret = fstat(fd,&st) < 0) && fnew)
|
|
|
02e59c |
close(fd);
|
|
|
02e59c |
|
|
|
02e59c |
else if ((st.st_size == 0) && fnew)
|
|
|
02e59c |
close(fd);
|
|
|
02e59c |
|
|
|
02e59c |
if (ret < 0)
|
|
|
02e59c |
return TBNF_SYSTEM_ERROR(dctx,path);
|
|
|
02e59c |
|
|
|
02e59c |
else if (st.st_size == 0)
|
|
|
02e59c |
return TBNF_CUSTOM_ERROR(
|
|
|
02e59c |
dctx,TBNF_ERR_IMAGE_SIZE_ZERO);
|
|
|
02e59c |
|
|
|
02e59c |
map->map_size = st.st_size;
|
|
|
02e59c |
map->map_addr = mmap(0,map->map_size,prot,MAP_PRIVATE,fd,0);
|
|
|
02e59c |
|
|
|
02e59c |
if (fnew)
|
|
|
02e59c |
close(fd);
|
|
|
02e59c |
|
|
|
02e59c |
return (map->map_addr == MAP_FAILED)
|
|
|
02e59c |
? TBNF_SYSTEM_ERROR(dctx,0)
|
|
|
02e59c |
: 0;
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
int tbnf_lib_unmap_raw_input(struct tbnf_raw_input * map)
|
|
|
02e59c |
{
|
|
|
02e59c |
return munmap(map->map_addr,map->map_size);
|
|
|
02e59c |
}
|