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 <cstdint>
19#include <ostream>
20#include <string>
21
22#include "LIEF/Object.hpp"
23#include "LIEF/errors.hpp"
24#include "LIEF/iterators.hpp"
25#include "LIEF/visibility.h"
26#include <optional>
27
28namespace LIEF {
29class BinaryStream;
30namespace PE {
31
36 public:
39 struct entry_t {
40 std::u16string key;
41 std::u16string value;
42
44 std::string key_u8() const;
45
47 std::string value_u8() const;
48
49 bool is_defined() const {
50 return !key.empty() || !value.empty();
51 }
52
53 operator bool() const {
54 return is_defined();
55 }
56
57 std::string to_string() const {
58 return key_u8() + ": " + value_u8();
59 }
60
61 friend LIEF_API std::ostream& operator<<(std::ostream& os,
62 const entry_t& entry) {
63 os << entry.to_string();
64 return os;
65 }
66 };
67
68 using entries_t = std::vector<entry_t>;
71
73
75
78
81
82 ~ResourceStringTable() override = default;
83
90 const std::u16string& key() const {
91 return key_;
92 }
93
97 uint16_t type() const {
98 return type_;
99 }
100
102 std::string key_u8() const;
103
106 return entries_;
107 }
108
110 return entries_;
111 }
112
113 std::optional<std::u16string> get(const std::u16string& key) const {
114 auto it =
115 std::find_if(entries_.begin(), entries_.end(),
116 [&key](const entry_t& entry) { return entry.key == key; });
117 if (it == entries_.end()) {
118 return std::nullopt;
119 }
120
121 return it->value;
122 }
123
124 std::optional<std::string> get(const std::string& key) const;
125
126 ResourceStringTable& key(std::u16string value) {
127 key_ = std::move(value);
128 return *this;
129 }
130
131 ResourceStringTable& type(uint16_t value) {
132 type_ = value;
133 return *this;
134 }
135
136 void add_entry(entry_t entry) {
137 entries_.push_back(std::move(entry));
138 }
139
140 void add_entry(std::u16string key, std::u16string value) {
141 entries_.push_back(entry_t{std::move(key), std::move(value)});
142 }
143
144 void accept(Visitor& visitor) const override;
145
146 std::optional<std::string> operator[](const std::string& str) const {
147 return get(str);
148 }
149
150 std::optional<std::u16string> operator[](const std::u16string& str) const {
151 return get(str);
152 }
153
154 LIEF_API friend std::ostream& operator<<(std::ostream& os,
155 const ResourceStringTable& table);
156
157 private:
158 uint16_t type_ = 0;
159 std::u16string key_;
160 std::vector<entry_t> entries_;
161};
162
163}
164}
165
166#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:35
std::optional< std::u16string > operator[](const std::u16string &str) const
Definition ResourceStringTable.hpp:150
ResourceStringTable & key(std::u16string value)
Definition ResourceStringTable.hpp:126
ref_iterator< entries_t & > it_entries
Definition ResourceStringTable.hpp:69
ResourceStringTable & operator=(const ResourceStringTable &)=default
std::optional< std::string > get(const std::string &key) const
ResourceStringTable & type(uint16_t value)
Definition ResourceStringTable.hpp:131
ResourceStringTable(const ResourceStringTable &)=default
~ResourceStringTable() override=default
std::string key_u8() const
The key as an utf8 string.
const_ref_iterator< const entries_t & > it_const_entries
Definition ResourceStringTable.hpp:70
ResourceStringTable(ResourceStringTable &&)=default
void add_entry(entry_t entry)
Definition ResourceStringTable.hpp:136
void add_entry(std::u16string key, std::u16string value)
Definition ResourceStringTable.hpp:140
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:90
ResourceStringTable & operator=(ResourceStringTable &&)=default
std::optional< std::string > operator[](const std::string &str) const
Definition ResourceStringTable.hpp:146
std::vector< entry_t > entries_t
Definition ResourceStringTable.hpp:68
void accept(Visitor &visitor) const override
it_const_entries entries() const
Definition ResourceStringTable.hpp:109
static result< ResourceStringTable > parse(BinaryStream &stream)
std::optional< std::u16string > get(const std::u16string &key) const
Definition ResourceStringTable.hpp:113
uint16_t type() const
The type of data in the version resource:
Definition ResourceStringTable.hpp:97
it_entries entries()
Iterator over the different entry_t element of this table.
Definition ResourceStringTable.hpp:105
Definition Visitor.hpp:212
Iterator which returns reference on container's values.
Definition iterators.hpp:47
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:41
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:317
An entry in this table which is composed of an UTF-16 key and an UTF-16 value.
Definition ResourceStringTable.hpp:39
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:61
std::u16string value
Definition ResourceStringTable.hpp:41
std::string to_string() const
Definition ResourceStringTable.hpp:57
bool is_defined() const
Definition ResourceStringTable.hpp:49
std::u16string key
Definition ResourceStringTable.hpp:40
#define LIEF_API
Definition visibility.h:45