LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
FileStream.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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 {
28class LIEF_API FileStream : public BinaryStream {
31 public:
32 static result<FileStream> from_file(const std::string& file);
33 FileStream(std::ifstream fs, uint64_t size) :
34 BinaryStream(STREAM_TYPE::FILE),
35 ifs_(std::move(fs)),
36 size_(size)
37 {}
38
39 FileStream() = delete;
40
41 FileStream(const FileStream&) = delete;
42 FileStream& operator=(const FileStream&) = delete;
43
44 FileStream(FileStream&& other) noexcept = default;
45 FileStream& operator=(FileStream&& other) noexcept = default;
46
47 uint64_t size() const override {
48 return size_;
49 }
50
51 std::vector<uint8_t> content() const;
52 ~FileStream() override = default;
53
54 static bool classof(const BinaryStream& stream) {
55 return stream.type() == STREAM_TYPE::FILE;
56 }
57
58 ok_error_t peek_in(void* dst, uint64_t offset, uint64_t size,
59 uint64_t /* virtual_address */= 0) const override {
60 if (offset > size_ || offset + size > size_) {
62 }
63 const auto pos = ifs_.tellg();
64 ifs_.seekg(offset);
65 ifs_.read(static_cast<char*>(dst), size);
66 ifs_.seekg(pos);
67 return ok();
68 }
69
70 result<const void*> read_at(uint64_t, uint64_t, uint64_t) const override {
72 }
73
74 protected:
75 mutable std::ifstream ifs_;
76 uint64_t size_ = 0;
77};
78}
79
80#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
STREAM_TYPE type() const
Definition BinaryStream.hpp:53
Stream interface over a std::ifstream
Definition FileStream.hpp:30
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:58
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:54
result< const void * > read_at(uint64_t, uint64_t, uint64_t) const override
Definition FileStream.hpp:70
uint64_t size() const override
Definition FileStream.hpp:47
FileStream & operator=(const FileStream &)=delete
@ not_supported
Definition errors.hpp:27
@ read_error
Definition errors.hpp:24
tl::unexpected< lief_errors > make_error_code(lief_errors e)
Create an standard error code from lief_errors.
Definition errors.hpp:52
LIEF namespace.
Definition Abstract/Binary.hpp:36
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:109
ok_t ok()
Return success for function with return type ok_error_t.
Definition errors.hpp:93
#define LIEF_API
Definition visibility.h:41