LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
FileStream.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_FILE_STREAM_H
17#define LIEF_FILE_STREAM_H
18
19#include <vector>
20#include <string>
21#include <fstream>
22
23#include "LIEF/errors.hpp"
25#include "LIEF/visibility.h"
26
27namespace LIEF {
28
31 public:
32 static result<FileStream> from_file(const std::string& file);
33 FileStream(std::ifstream fs, uint64_t size) :
35 ifs_(std::move(fs)),
36 size_(size) {}
37
38 FileStream() = delete;
39
40 FileStream(const FileStream&) = delete;
41 FileStream& operator=(const FileStream&) = delete;
42
43 FileStream(FileStream&& other) noexcept = default;
44 FileStream& operator=(FileStream&& other) noexcept = default;
45
46 uint64_t size() const override {
47 return size_;
48 }
49
50 std::vector<uint8_t> content() const;
51 ~FileStream() override = default;
52
53 static bool classof(const BinaryStream& stream) {
54 return stream.type() == STREAM_TYPE::FILE;
55 }
56
57 ok_error_t peek_in(void* dst, uint64_t offset, uint64_t size,
58 uint64_t /* virtual_address */ = 0) const override {
59 if (offset > size_ || offset + size > size_) {
61 }
62 const auto pos = ifs_.tellg();
63 ifs_.seekg(offset);
64 ifs_.read(static_cast<char*>(dst), size);
65 ifs_.seekg(pos);
66 return ok();
67 }
68
69 result<const void*> read_at(uint64_t, uint64_t, uint64_t) const override {
71 }
72
73 protected:
74 mutable std::ifstream ifs_;
75 uint64_t size_ = 0;
76};
77}
78
79#endif
STREAM_TYPE
Definition BinaryStream.hpp:37
@ FILE
Definition BinaryStream.hpp:42
size_t pos() const
Definition BinaryStream.hpp:181
BinaryStream(STREAM_TYPE type)
Definition BinaryStream.hpp:47
STREAM_TYPE type() const
Definition BinaryStream.hpp:52
static result< FileStream > from_file(const std::string &file)
FileStream(FileStream &&other) noexcept=default
FileStream()=delete
FileStream(std::ifstream fs, uint64_t size)
Definition FileStream.hpp:33
ok_error_t peek_in(void *dst, uint64_t offset, uint64_t size, uint64_t=0) const override
Definition FileStream.hpp:57
std::vector< uint8_t > content() const
FileStream & operator=(FileStream &&other) noexcept=default
FileStream(const FileStream &)=delete
~FileStream() override=default
static bool classof(const BinaryStream &stream)
Definition FileStream.hpp:53
result< const void * > read_at(uint64_t, uint64_t, uint64_t) const override
Definition FileStream.hpp:69
uint64_t size() const override
Definition FileStream.hpp:46
FileStream & operator=(const FileStream &)=delete
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:114
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:77
@ not_supported
Definition errors.hpp:28
@ read_error
Definition errors.hpp:25
tl::unexpected< lief_errors > make_error_code(lief_errors e)
Create an standard error code from lief_errors.
Definition errors.hpp:53
LIEF namespace.
Definition Abstract/Binary.hpp:40
ok_t ok()
Return success for function with return type ok_error_t.
Definition errors.hpp:98
Definition string.h:155
#define LIEF_API
Definition visibility.h:43