forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSection.cpp
More file actions
155 lines (123 loc) · 3.78 KB
/
Copy pathSection.cpp
File metadata and controls
155 lines (123 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* Copyright 2017 - 2025 R. Thomas
* Copyright 2017 - 2025 Quarkslab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <array>
#include <ostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <utility>
#include "LIEF/Visitor.hpp"
#include "logging.hpp"
#include "LIEF/Abstract/hash.hpp"
#include "LIEF/Abstract/Section.hpp"
#include "Section.tcc"
namespace LIEF {
// Search functions
// ================
size_t Section::search(uint64_t integer, size_t pos, size_t size) const {
if (size > sizeof(integer)) {
return npos;
}
size_t minimal_size = size;
if (size == 0) {
if (integer < std::numeric_limits<uint8_t>::max()) {
minimal_size = sizeof(uint8_t);
}
else if (integer < std::numeric_limits<uint16_t>::max()) {
minimal_size = sizeof(uint16_t);
}
else if (integer < std::numeric_limits<uint32_t>::max()) {
minimal_size = sizeof(uint32_t);
}
else if (integer < std::numeric_limits<uint64_t>::max()) {
minimal_size = sizeof(uint64_t);
} else {
return npos;
}
}
std::vector<uint8_t> pattern(minimal_size, 0);
memcpy(pattern.data(), &integer, minimal_size);
return search(pattern, pos);
}
size_t Section::search(const std::vector<uint8_t>& pattern, size_t pos) const {
span<const uint8_t> content = this->content();
const auto* it_found = std::search(
std::begin(content) + pos, std::end(content),
std::begin(pattern), std::end(pattern));
if (it_found == std::end(content)) {
return npos;
}
return std::distance(std::begin(content), it_found);
}
size_t Section::search(const std::string& pattern, size_t pos) const {
std::vector<uint8_t> pattern_formated = {std::begin(pattern), std::end(pattern)};
return search(pattern_formated, pos);
}
size_t Section::search(uint64_t integer, size_t pos) const {
return search(integer, pos, 0);
}
// Search all functions
// ====================
std::vector<size_t> Section::search_all(uint64_t v, size_t size) const {
std::vector<size_t> result;
size_t pos = search(v, 0, size);
if (pos == Section::npos) {
return result;
}
do {
result.push_back(pos);
pos = search(v, pos + 1, size);
} while(pos != Section::npos);
return result;
}
std::vector<size_t> Section::search_all(uint64_t v) const {
return search_all(v, 0);
}
std::vector<size_t> Section::search_all(const std::string& v) const {
return search_all_<std::string>(v);
}
double Section::entropy() const {
std::array<uint64_t, 256> frequencies = { {0} };
span<const uint8_t> content = this->content();
if (content.empty() || content.size() == 1) {
return 0.;
}
for (uint8_t x : content) {
frequencies[x]++;
}
double entropy = 0.0;
for (uint64_t p : frequencies) {
if (p > 0) {
double freq = static_cast<double>(p) / static_cast<double>(content.size());
entropy += freq * std::log2l(freq) ;
}
}
return (-entropy);
}
void Section::accept(Visitor& visitor) const {
visitor.visit(*this);
}
std::ostream& operator<<(std::ostream& os, const Section& entry) {
os << std::hex;
os << std::left
<< std::setw(30) << entry.name()
<< std::setw(10) << entry.virtual_address()
<< std::setw(10) << entry.size()
<< std::setw(10) << entry.offset()
<< std::setw(10) << entry.entropy();
return os;
}
}