LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
errors.hpp
1/* Copyright 2021 - 2024 R. Thomas
2 * Copyright 2021 - 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_ERROR_H
17#define LIEF_ERROR_H
18#include <cstdint>
19#include <LIEF/third-party/expected.hpp>
20
22enum class lief_errors : uint32_t {
23 read_error = 1,
24 not_found,
25 not_implemented,
26 not_supported,
27
28 corrupted,
29 conversion_error,
30
31 read_out_of_bound,
32 asn1_bad_tag,
33 file_error,
34
35 file_format_error,
36 parsing_error,
37 build_error,
38
39 data_too_large,
40 /*
41 * When adding a new error, do not forget
42 * to update the Python bindings as well (pyErr.cpp) and Rust bindings:
43 * lief/src/error.rs
44 */
45};
46
47const char* to_string(lief_errors err);
48
50inline tl::unexpected<lief_errors> make_error_code(lief_errors e) {
51 return tl::make_unexpected(e);
52}
53
54
55namespace LIEF {
72template<typename T>
73using result = tl::expected<T, lief_errors>;
74
76template<class T>
77lief_errors get_error(result<T>& err) {
78 return err.error();
79}
80
82template<class T>
83lief_errors as_lief_err(result<T>& err) {
84 return err.error();
85}
86
88struct ok_t {};
89
91inline ok_t ok() {
92 return ok_t{};
93}
94
108
109inline bool is_ok(const ok_error_t& val) {
110 return val.has_value();
111}
112
113inline bool is_err(const ok_error_t& val) {
114 return !is_ok(val);
115}
116
117}
118
119
120
121
122
123#endif
LIEF namespace.
Definition Abstract/Binary.hpp:31
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:107
lief_errors get_error(result< T > &err)
Get the error code associated with the result.
Definition errors.hpp:77
ok_t ok()
Return success for function with return type ok_error_t.
Definition errors.hpp:91
lief_errors as_lief_err(result< T > &err)
Return the lief_errors when the provided result<T> is an error.
Definition errors.hpp:83
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:73
Opaque structure used by ok_error_t.
Definition errors.hpp:88