LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
FileStream.hpp
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"
24#include "LIEF/BinaryStream/BinaryStream.hpp"
25namespace LIEF {
26
28class FileStream : public BinaryStream {
29 public:
30 static result<FileStream> from_file(const std::string& file);
31 FileStream(std::ifstream fs, uint64_t size);
32
33 FileStream() = delete;
34
35 // VectorStream should not be copyable for performances reasons
36 FileStream(const FileStream&) = delete;
37 FileStream& operator=(const FileStream&) = delete;
38
39 FileStream(FileStream&& other);
40 FileStream& operator=(FileStream&& other);
41
42 uint64_t size() const override {
43 return size_;
44 }
45
46 std::vector<uint8_t> content() const;
47 ~FileStream() override;
48
49 static bool classof(const BinaryStream& stream);
50
51 protected:
52 ok_error_t peek_in(void* dst, uint64_t offset, uint64_t size) const override {
53 if (offset > size_ || offset + size > size_) {
54 return make_error_code(lief_errors::read_error);
55 }
56 const auto pos = ifs_.tellg();
57 ifs_.seekg(offset);
58 ifs_.read(static_cast<char*>(dst), size);
59 ifs_.seekg(pos);
60 return ok();
61 }
62 result<const void*> read_at(uint64_t, uint64_t) const override {
63 return make_error_code(lief_errors::not_supported);
64 }
65 mutable std::ifstream ifs_;
66 uint64_t size_ = 0;
67};
68}
69
70#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:34
Stream interface over a std::ifstream.
Definition FileStream.hpp:28
LIEF namespace.
Definition Abstract/Binary.hpp:32
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:106
ok_t ok()
Return success for function with return type ok_error_t.
Definition errors.hpp:90
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:72