LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
errors.hpp
Go to the documentation of this file.
1/* Copyright 2021 - 2026 R. Thomas
2 * Copyright 2021 - 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_ERROR_H
17#define LIEF_ERROR_H
18#include <cstdint>
19#include <string>
21#include <LIEF/third-party/expected.hpp>
22
24enum class lief_errors : uint32_t {
29
32
36
40
44 /*
45 * When adding a new error, do not forget
46 * to update the Python bindings as well (pyErr.cpp) and Rust bindings:
47 * lief/src/error.rs
48 */
49};
50
51const char* to_string(lief_errors err);
52
54inline tl::unexpected<lief_errors> make_error_code(lief_errors e) {
55 return tl::make_unexpected(e);
56}
57
58
59namespace LIEF {
77template<typename T>
78class LIEF_MAYBE_UNUSED result : public tl::expected<T, lief_errors> {
79 public:
80 using ExpectedType = T;
81 using tl::expected<T, lief_errors>::expected;
82 result(tl::expected<T, lief_errors> e) :
83 tl::expected<T, lief_errors>::expected(std::move(e)) {}
84};
85
87template<class T>
89 return err.error();
90}
91
93template<class T>
95 return err.error();
96}
97
99struct ok_t {};
100
102inline ok_t ok() {
103 return ok_t{};
104}
105
118class LIEF_MAYBE_UNUSED ok_error_t : public result<ok_t> {
119 public:
120 using result<ok_t>::result;
121};
122
123inline bool is_ok(const ok_error_t& val) {
124 return val.has_value();
125}
126
127inline bool is_err(const ok_error_t& val) {
128 return !is_ok(val);
129}
130
131}
132
133extern template class tl::expected<LIEF::ok_t, lief_errors>;
134extern template class LIEF::result<LIEF::ok_t>;
135extern template class tl::expected<uint64_t, lief_errors>;
136extern template class LIEF::result<uint64_t>;
137extern template class tl::expected<uint32_t, lief_errors>;
138extern template class LIEF::result<uint32_t>;
139extern template class tl::expected<std::string, lief_errors>;
140extern template class LIEF::result<std::string>;
141
142#endif
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:118
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:78
result(tl::expected< T, lief_errors > e)
Definition errors.hpp:82
T ExpectedType
Definition errors.hpp:80
#define LIEF_MAYBE_UNUSED
Definition compiler_attributes.hpp:62
lief_errors
LIEF error codes definition.
Definition errors.hpp:24
@ require_extended_version
Definition errors.hpp:42
@ file_format_error
Definition errors.hpp:37
@ data_too_large
Definition errors.hpp:41
@ read_out_of_bound
Definition errors.hpp:33
@ conversion_error
Definition errors.hpp:31
@ build_error
Definition errors.hpp:39
@ not_implemented
Definition errors.hpp:27
@ not_found
Definition errors.hpp:26
@ corrupted
Definition errors.hpp:30
@ asn1_bad_tag
Definition errors.hpp:34
@ not_supported
Definition errors.hpp:28
@ read_error
Definition errors.hpp:25
@ parsing_error
Definition errors.hpp:38
@ inconsistent
Definition errors.hpp:43
@ file_error
Definition errors.hpp:35
tl::unexpected< lief_errors > make_error_code(lief_errors e)
Create a standard error code from lief_errors.
Definition errors.hpp:54
const char * to_string(lief_errors err)
LIEF namespace.
Definition Abstract/Binary.hpp:41
bool is_ok(const ok_error_t &val)
Definition errors.hpp:123
lief_errors get_error(result< T > &err)
Get the error code associated with the result.
Definition errors.hpp:88
ok_t ok()
Return success for function with return type ok_error_t.
Definition errors.hpp:102
bool is_err(const ok_error_t &val)
Definition errors.hpp:127
lief_errors as_lief_err(result< T > &err)
Return the lief_errors when the provided result<T> is an error.
Definition errors.hpp:94
Opaque structure used by ok_error_t.
Definition errors.hpp:99