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
|
// Copyright 2021-2022 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
#include "ProcessBase.h"
#include "mumble_positional_audio_utils.h"
#include <chrono>
ProcessBase::ProcessBase(const procid_t id, const std::string &name)
: Host(id), m_ok(false), m_name(name), m_pointerSize(0) {
}
ProcessBase::~ProcessBase() {
}
procptr_t ProcessBase::peekPtr(const procptr_t address) const {
procptr_t v = 0;
if (!peek(address, &v, m_pointerSize)) {
return 0;
}
return v;
}
std::string ProcessBase::peekString(const procptr_t address, const size_t length) const {
std::string string;
if (length > 0) {
string.resize(length);
if (!peek(address, &string[0], length)) {
return std::string();
}
} else {
auto now = std::chrono::steady_clock::now();
const auto end = now + std::chrono::seconds(3);
for (procptr_t i = 0; now < end; ++i) {
char ch = 0;
if (!peek(address + i, &ch, sizeof(ch)) || ch == '\0') {
break;
}
string += ch;
// Update current time.
now = std::chrono::steady_clock::now();
}
}
return string;
}
procptr_t ProcessBase::virtualFunction(const procptr_t classObject, const size_t index) const {
const auto vTable = peekPtr(classObject);
if (!vTable) {
return 0;
}
return peekPtr(vTable + (index * m_pointerSize));
}
procptr_t ProcessBase::findPattern(const std::vector< uint8_t > &pattern, const Module &module) {
for (const auto ®ion : module.regions()) {
if (!region.readable) {
continue;
}
const auto ret = findPattern(pattern, region.address, region.size);
if (ret) {
return ret;
}
}
return 0;
}
procptr_t ProcessBase::findPattern(const std::vector< uint8_t > &pattern, procptr_t address, const size_t size) {
// 32 KiB appears to be a good balance
constexpr uint16_t bufferSize = 32768;
std::vector< uint8_t > buffer(bufferSize);
const auto chunks = size / buffer.size();
for (size_t i = 0; i < chunks; ++i) {
if (!peek(address, &buffer[0], buffer.size())) {
return 0;
}
const auto ret = searchInBuffer(pattern, buffer);
if (ret != SIZE_MAX) {
return address + ret;
}
address += buffer.size();
}
const auto remainder = size % buffer.size();
if (remainder >= pattern.size()) {
buffer.resize(remainder);
if (!peek(address, &buffer[0], buffer.size())) {
return 0;
}
const auto ret = searchInBuffer(pattern, buffer);
if (ret != SIZE_MAX) {
return address + ret;
}
}
return 0;
}
procid_t ProcessBase::find(const std::string &name, const std::multimap< std::wstring, unsigned long long int > &pids) {
if (pids.empty()) {
return 0;
}
const auto iter = pids.find(utf8ToUtf16(name));
if (iter == pids.cend()) {
return 0;
}
return iter->second;
}
|