mirror of
https://github.com/mumble-voip/mumble.git
synced 2025-10-26 11:19:16 +00:00
Since most of the functions are already using C++, why not use classes as well? This commit introduces the following classes: - HostLinux: can only be compiled on Linux. Implements peek() and module(). - HostWindows: can only be compiled on Windows. Implements peek() and module(). - Process: abstract (cannot be instantiated directly). Inherits from HostLinux on Linux and from HostWindows on Windows. Provides functions that can be used with both Linux and Windows processes. Pure virtual functions are implemented in the following classes: - ProcessLinux: meant to be used with Linux processes, inherits from Process. Only implements exportedSymbol(), due to the other functions being universal. The constructor detects the architecture through the ELF header. - ProcessWindows: meant to be used with Windows processes, inherits from Process. Only implements exportedSymbol(), due to the other functions being universal. The constructor detects the architecture through the NT header.
78 lines
1.7 KiB
C++
78 lines
1.7 KiB
C++
// Copyright 2020 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 "Process.h"
|
|
|
|
#include "mumble_plugin_utils.h"
|
|
|
|
#include <chrono>
|
|
|
|
Process::Process(const procid_t id, const std::string &name) : Host(id), m_name(name), m_pointerSize(0) {
|
|
}
|
|
|
|
Process::~Process() {
|
|
}
|
|
|
|
procptr_t Process::peekPtr(const procptr_t address) const {
|
|
procptr_t v = 0;
|
|
|
|
if (!peek(address, &v, m_pointerSize)) {
|
|
return 0;
|
|
}
|
|
|
|
return v;
|
|
}
|
|
|
|
std::string Process::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 Process::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));
|
|
}
|
|
|
|
procid_t Process::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 static_cast< procid_t >(iter->second);
|
|
}
|