LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
ResourceStringTable.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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>
19#include <ostream>
20
21#include "LIEF/visibility.h"
22#include "LIEF/Object.hpp"
23#include "LIEF/errors.hpp"
24#include "LIEF/iterators.hpp"
25#include "LIEF/optional.hpp"
26
27namespace LIEF {
28class BinaryStream;
29namespace PE {
30class LIEF_API ResourceStringTable : public Object {
35 public: struct entry_t {
39 std::u16string key;
40 std::u16string value;
41 std::string key_u8() const;
44 std::string value_u8() const;
47
48 bool is_defined() const {
49 return !key.empty() || !value.empty();
50 }
51
52 operator bool() const {
53 return is_defined();
54 }
55
56 std::string to_string() const {
57 return key_u8() + ": " + value_u8();
58 }
59
60 friend LIEF_API
61 std::ostream& operator<<(std::ostream& os, const entry_t& entry)
62 {
63 os << entry.to_string();
64 return os;
65 }
66 };
67
68 using entries_t = std::vector<entry_t>;
69 using it_entries = ref_iterator<entries_t&>;
70 using it_const_entries = const_ref_iterator<const entries_t&>;
71
73
74 static result<ResourceStringTable> parse(BinaryStream& stream);
75
76 ResourceStringTable(const ResourceStringTable&) = default;
77 ResourceStringTable& operator=(const ResourceStringTable&) = default;
78
79 ResourceStringTable(ResourceStringTable&&) = default;
80 ResourceStringTable& operator=(ResourceStringTable&&) = default;
81
82 ~ResourceStringTable() override = default;
83 const std::u16string& key() const {
91 return key_;
92 }
93 uint16_t type() const {
98 return type_;
99 }
100 std::string key_u8() const;
103 it_entries entries() {
106 return entries_;
107 }
108
109 it_const_entries entries() const {
110 return entries_;
111 }
112
113 optional<std::u16string> get(const std::u16string& key) const {
114 auto it = std::find_if(entries_.begin(), entries_.end(),
115 [&key] (const entry_t& entry) {
116 return entry.key == key;
117 }
118 );
119 if (it == entries_.end()) {
120 return nullopt();
121 }
122
123 return it->value;
124 }
125
126 optional<std::string> get(const std::string& key) const;
127
128 ResourceStringTable& key(std::u16string value) {
129 key_ = std::move(value);
130 return *this;
131 }
132
133 ResourceStringTable& type(uint16_t value) {
134 type_ = value;
135 return *this;
136 }
137
138 void add_entry(entry_t entry) {
139 entries_.push_back(std::move(entry));
140 }
141
142 void add_entry(std::u16string key, std::u16string value) {
143 entries_.push_back(entry_t{std::move(key), std::move(value)});
144 }
145
146 void accept(Visitor& visitor) const override;
147
148 optional<std::string> operator[](const std::string& str) const {
149 return get(str);
150 }
151
152 optional<std::u16string> operator[](const std::u16string& str) const {
153 return get(str);
154 }
155
156 LIEF_API friend
157 std::ostream& operator<<(std::ostream& os, const ResourceStringTable& table);
158
159 private:
160 uint16_t type_ = 0;
161 std::u16string key_;
162 std::vector<entry_t> entries_;
163};
164
165}
166}
167
168#endif
Object.hpp
LIEF::BinaryStream
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
LIEF::PE::ResourceStringTable
This class represents the StringTable structure. This structure can be seen as a dictionary of key,...
Definition ResourceStringTable.hpp:34
LIEF::PE::ResourceStringTable::operator[]
optional< std::u16string > operator[](const std::u16string &str) const
Definition ResourceStringTable.hpp:152
LIEF::PE::ResourceStringTable::ResourceStringTable
ResourceStringTable()=default
LIEF::PE::ResourceStringTable::key
ResourceStringTable & key(std::u16string value)
Definition ResourceStringTable.hpp:128
LIEF::PE::ResourceStringTable::operator=
ResourceStringTable & operator=(const ResourceStringTable &)=default
LIEF::PE::ResourceStringTable::type
ResourceStringTable & type(uint16_t value)
Definition ResourceStringTable.hpp:133
LIEF::PE::ResourceStringTable::ResourceStringTable
ResourceStringTable(const ResourceStringTable &)=default
LIEF::PE::ResourceStringTable::~ResourceStringTable
~ResourceStringTable() override=default
LIEF::PE::ResourceStringTable::key_u8
std::string key_u8() const
The key as an utf8 string.
LIEF::PE::ResourceStringTable::ResourceStringTable
ResourceStringTable(ResourceStringTable &&)=default
LIEF::PE::ResourceStringTable::add_entry
void add_entry(entry_t entry)
Definition ResourceStringTable.hpp:138
LIEF::PE::ResourceStringTable::add_entry
void add_entry(std::u16string key, std::u16string value)
Definition ResourceStringTable.hpp:142
LIEF::PE::ResourceStringTable::operator<<
friend std::ostream & operator<<(std::ostream &os, const ResourceStringTable &table)
LIEF::PE::ResourceStringTable::key
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
LIEF::PE::ResourceStringTable::operator=
ResourceStringTable & operator=(ResourceStringTable &&)=default
LIEF::PE::ResourceStringTable::operator[]
optional< std::string > operator[](const std::string &str) const
Definition ResourceStringTable.hpp:148
LIEF::PE::ResourceStringTable::accept
void accept(Visitor &visitor) const override
LIEF::PE::ResourceStringTable::entries
it_const_entries entries() const
Definition ResourceStringTable.hpp:109
LIEF::PE::ResourceStringTable::parse
static result< ResourceStringTable > parse(BinaryStream &stream)
LIEF::PE::ResourceStringTable::get
optional< std::u16string > get(const std::u16string &key) const
Definition ResourceStringTable.hpp:113
LIEF::PE::ResourceStringTable::type
uint16_t type() const
The type of data in the version resource:
Definition ResourceStringTable.hpp:97
LIEF::PE::ResourceStringTable::get
optional< std::string > get(const std::string &key) const
LIEF::PE::ResourceStringTable::entries
it_entries entries()
Iterator over the different entry_t element of this table.
Definition ResourceStringTable.hpp:105
errors.hpp
iterators.hpp
LIEF::PE
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
LIEF::nullopt
tl::unexpected< lief_errors > nullopt()
Definition optional.hpp:36
optional.hpp
LIEF::PE::ResourceStringTable::entry_t::value_u8
std::string value_u8() const
Value in utf8 representation.
LIEF::PE::ResourceStringTable::entry_t::key_u8
std::string key_u8() const
Key in utf8 representation.
LIEF::PE::ResourceStringTable::entry_t::operator<<
friend std::ostream & operator<<(std::ostream &os, const entry_t &entry)
Definition ResourceStringTable.hpp:61
LIEF::PE::ResourceStringTable::entry_t::to_string
std::string to_string() const
Definition ResourceStringTable.hpp:56
LIEF::PE::ResourceStringTable::entry_t::is_defined
bool is_defined() const
Definition ResourceStringTable.hpp:48
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41