53 , fs::path
const& path_file_output
54 , std::pair<uint64_t,uint64_t> section)
57 std::ifstream f_in{path_file_input, std::ios::binary};
58 return_if(not f_in.is_open(), Error(
"E::Failed to open in file {}", path_file_input));
59 std::ofstream f_out{path_file_output, std::ios::binary};
60 return_if(not f_out.is_open(), Error(
"E::Failed to open out file {}", path_file_output));
62 uint64_t size = section.second - section.first;
64 f_in.seekg(section.first, std::ios::beg);
66 const size_t size_buf = 4096;
67 char buffer[size_buf];
70 std::streamsize read_size =
static_cast<std::streamsize
>(std::min(
static_cast<uint64_t
>(size_buf), size));
71 f_in.read(buffer, read_size);
72 f_out.write(buffer, read_size);
86 , uint64_t offset = 0)
91 File(FILE* ptr) : ptr(ptr) {};
92 ~File() {
if(ptr) { ::fclose(ptr); } }
97 File file(fopen(path_file_elf.c_str(),
"rb"));
98 return_if(file.ptr ==
nullptr, Error(
"E::Could not open file '{}': {}", path_file_elf, strerror(errno)));
100 return_if(fseek(file.ptr, offset, SEEK_SET) < 0
101 , Error(
"E::Could not seek in file '{}': {}", path_file_elf, strerror(errno))
104 return_if(fread(&header,
sizeof(header), 1, file.ptr) != 1
105 , Error(
"E::Could not read elf header")
108 return_if(std::memcmp(header.e_ident, ELFMAG, SELFMAG) != 0, Error(
"E::'{}' not an elf file", path_file_elf));
109 offset = header.e_shoff + (header.e_ehsize * header.e_shnum);
Value< uint64_t > skip_elf_header(fs::path const &path_file_elf, uint64_t offset=0)
Skips the elf header starting from 'offset' and returns the offset to the first byte afterwards.
Value< void > copy_binary(fs::path const &path_file_input, fs::path const &path_file_output, std::pair< uint64_t, uint64_t > section)
Copies the binary data between [offset.first, offset.second] from path_file_input to path_file_output...