LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
ResourceStringTable.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_PE_RESOURCE_STRING_TABLE_H
17#define LIEF_PE_RESOURCE_STRING_TABLE_H
18#include <string_view>
19#include <cstdint>
20#include <ostream>
21#include <string>
22
23#include "LIEF/Object.hpp"
24#include "LIEF/errors.hpp"
25#include "LIEF/iterators.hpp"
26#include "LIEF/visibility.h"
27#include <optional>
28
29namespace LIEF {
30class BinaryStream;
31namespace PE {
32
37 public:
40 struct entry_t {
41 std::u16string key;
42 std::u16string value;
43
45 std::string key_u8() const;
46
48 std::string value_u8() const;
49
50 bool is_defined() const {
51 return !key.empty() || !value.empty();
52 }
53
54 operator bool() const {
55 return is_defined();
56 }
57
58 std::string to_string() const {
59 return key_u8() + ": " + value_u8();
60 }
61
62 friend LIEF_API std::ostream& operator<<(std::ostream& os,
63 const entry_t& entry) {
64 os << entry.to_string();
65 return os;
66 }
67 };
68
69 using entries_t = std::vector<entry_t>;
72
74
76
79
82
83 ~ResourceStringTable() override = default;
84
91 const std::u16string& key() const {
92 return key_;
93 }
94
98 uint16_t type() const {
99 return type_;
100 }
101
103 std::string key_u8() const;
104
107 return entries_;
108 }
109
111 return entries_;
112 }
113
114 std::optional<std::u16string> get(const std::u16string& key) const {
115 auto it =
116 std::find_if(entries_.begin(), entries_.end(),
117 [&key](const entry_t& entry) { return entry.key == key; });
118 if (it == entries_.end()) {
119 return std::nullopt;
120 }
121
122 return it->value;
123 }
124
125 std::optional<std::string> get(std::string_view key) const;
126
127 ResourceStringTable& key(std::u16string value) {
128 key_ = std::move(value);
129 return *this;
130 }
131
132 ResourceStringTable& type(uint16_t value) {
133 type_ = value;
134 return *this;
135 }
136
137 void add_entry(entry_t entry) {
138 entries_.push_back(std::move(entry));
139 }
140
141 void add_entry(std::u16string key, std::u16string value) {
142 entries_.push_back(entry_t{std::move(key), std::move(value)});
143 }
144
145 void accept(Visitor& visitor) const override;
146
147 std::optional<std::string> operator[](std::string_view str) const {
148 return get(str);
149 }
150
151 std::optional<std::u16string> operator[](const std::u16string& str) const {
152 return get(str);
153 }
154
155 LIEF_API friend std::ostream& operator<<(std::ostream& os,
156 const ResourceStringTable& table);
157
158 private:
159 uint16_t type_ = 0;
160 std::u16string key_;
161 std::vector<entry_t> entries_;
162};
163
164}
165}
166
167#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:35
std::optional< std::string > get(std::string_view key) const
std::optional< std::u16string > operator[](const std::u16string &str) const
Definition ResourceStringTable.hpp:151
ResourceStringTable & key(std::u16string value)
Definition ResourceStringTable.hpp:127
ref_iterator< entries_t & > it_entries
Definition ResourceStringTable.hpp:70
ResourceStringTable & operator=(const ResourceStringTable &)=default
ResourceStringTable & type(uint16_t value)
Definition ResourceStringTable.hpp:132
ResourceStringTable(const ResourceStringTable &)=default
~ResourceStringTable() override=default
std::optional< std::string > operator[](std::string_view str) const
Definition ResourceStringTable.hpp:147
std::string key_u8() const
The key as an utf8 string.
const_ref_iterator< const entries_t & > it_const_entries
Definition ResourceStringTable.hpp:71
ResourceStringTable(ResourceStringTable &&)=default
void add_entry(entry_t entry)
Definition ResourceStringTable.hpp:137
void add_entry(std::u16string key, std::u16string value)
Definition ResourceStringTable.hpp:141
friend std::ostream & operator<<(std::ostream &os, const ResourceStringTable &table)
const std::u16string & key() const
An 8-digit hexadecimal number stored as a Unicode string. The four most significant digits represent ...
Definition ResourceStringTable.hpp:91
ResourceStringTable & operator=(ResourceStringTable &&)=default
std::vector< entry_t > entries_t
Definition ResourceStringTable.hpp:69
void accept(Visitor &visitor) const override
it_const_entries entries() const
Definition ResourceStringTable.hpp:110
static result< ResourceStringTable > parse(BinaryStream &stream)
std::optional< std::u16string > get(const std::u16string &key) const
Definition ResourceStringTable.hpp:114
uint16_t type() const
The type of data in the version resource:
Definition ResourceStringTable.hpp:98
it_entries entries()
Iterator over the different entry_t element of this table.
Definition ResourceStringTable.hpp:106
Definition Visitor.hpp:212
Iterator which returns reference on container's values.
Definition iterators.hpp:121
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
LIEF namespace.
Definition Abstract/Binary.hpp:42
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which returns a const ref on container's values.
Definition iterators.hpp:386
An entry in this table which is composed of an UTF-16 key and an UTF-16 value.
Definition ResourceStringTable.hpp:40
std::string value_u8() const
Value in utf8 representation.
std::string key_u8() const
Key in utf8 representation.
friend std::ostream & operator<<(std::ostream &os, const entry_t &entry)
Definition ResourceStringTable.hpp:62
std::u16string value
Definition ResourceStringTable.hpp:42
std::string to_string() const
Definition ResourceStringTable.hpp:58
bool is_defined() const
Definition ResourceStringTable.hpp:50
std::u16string key
Definition ResourceStringTable.hpp:41
#define LIEF_API
Definition visibility.h:45