Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavide Beatrici <davidebeatrici@gmail.com>2019-07-25 08:29:19 +0300
committerDavide Beatrici <davidebeatrici@gmail.com>2019-08-09 00:51:13 +0300
commite8afc3872a09472e444b41c92a98f307b27b4efc (patch)
tree5aa17cf36b3a4139e08436cdd2cbb4635640bf6b /plugins
parent4f0a40423b3474e67458ff644f676324b0ec9984 (diff)
plugins: move common functions and variables from OS-specific headers to mumble_plugin_main.h
This is in preparation for the new Source Engine plugin which will add a few common functions. This commit also improves the functions arguments so that they are passed by reference and marked as const (when possible). A new data type called procid_t is created, intended to be a replacement for pid_t (Linux) and DWORD (Windows).
Diffstat (limited to 'plugins')
-rw-r--r--plugins/mumble_plugin.h5
-rw-r--r--plugins/mumble_plugin_linux.h43
-rw-r--r--plugins/mumble_plugin_main.h43
-rw-r--r--plugins/mumble_plugin_win32.h71
4 files changed, 75 insertions, 87 deletions
diff --git a/plugins/mumble_plugin.h b/plugins/mumble_plugin.h
index e513db5c6..a5f282ed7 100644
--- a/plugins/mumble_plugin.h
+++ b/plugins/mumble_plugin.h
@@ -6,7 +6,10 @@
#ifndef MUMBLE_MUMBLE_PLUGIN_H_
#define MUMBLE_MUMBLE_PLUGIN_H_
-typedef unsigned long long procptr_t;
+#include <stdint.h>
+
+typedef uint32_t procid_t;
+typedef uint64_t procptr_t;
#define LENGTH_OF(array) (sizeof(array) / sizeof((array)[0]))
diff --git a/plugins/mumble_plugin_linux.h b/plugins/mumble_plugin_linux.h
index fbdae1b7c..3fff04201 100644
--- a/plugins/mumble_plugin_linux.h
+++ b/plugins/mumble_plugin_linux.h
@@ -18,11 +18,7 @@
#include "mumble_plugin.h"
-pid_t pPid;
-int is64Bit;
-static procptr_t pModule;
-
-static inline std::string readAll(std::string fn) {
+static inline std::string readAll(const std::string &fn) {
std::ifstream ifs;
ifs.open(fn.c_str(), std::ifstream::binary);
@@ -42,7 +38,7 @@ static inline std::string readAll(std::string fn) {
// This function returns 0 if the process is 32-bit and 1 if it's 64-bit.
// In case of failure, it returns -1.
-static inline int checkProcessIs64Bit(const pid_t pid)
+static inline int checkProcessIs64Bit(const procid_t &pid)
{
// We can know the process architecture by looking at its ELF header.
char elf[5];
@@ -74,7 +70,7 @@ static inline int checkProcessIs64Bit(const pid_t pid)
return elf[4] != 1;
}
-static inline procptr_t getModuleAddr(pid_t pid, const wchar_t *modname) {
+static inline procptr_t getModuleAddr(const procid_t &pid, const wchar_t *modname) {
std::wstring modnameWide(modname);
std::string modnameNonWide(modnameWide.begin(), modnameWide.end());
@@ -182,13 +178,9 @@ static inline procptr_t getModuleAddr(pid_t pid, const wchar_t *modname) {
return 0;
}
-static inline procptr_t getModuleAddr(const wchar_t *modname) {
- return getModuleAddr(pPid, modname);
-}
-
-static inline bool peekProc(procptr_t base, void *dest, size_t len) {
+static inline bool peekProc(const procptr_t &addr, void *dest, const size_t &len) {
struct iovec in;
- in.iov_base = reinterpret_cast<void *>(base); // Address from target process
+ in.iov_base = reinterpret_cast<void *>(addr); // Address from target process
in.iov_len = len; // Length
struct iovec out;
@@ -200,31 +192,6 @@ static inline bool peekProc(procptr_t base, void *dest, size_t len) {
return (nread != -1 && static_cast<size_t>(nread) == in.iov_len);
}
-template<class T>
-bool peekProc(procptr_t base, T &dest) {
- struct iovec in;
- in.iov_base = reinterpret_cast<void *>(base); // Address from target process
- in.iov_len = sizeof(T); // Length
-
- struct iovec out;
- out.iov_base = &dest;
- out.iov_len = sizeof(T);
-
- ssize_t nread = process_vm_readv(pPid, &out, 1, &in, 1, 0);
-
- return (nread != -1 && static_cast<size_t>(nread) == in.iov_len);
-}
-
-static inline procptr_t peekProcPtr(procptr_t base) {
- procptr_t v = 0;
-
- if (!peekProc(base, &v, is64Bit ? 8 : 4)) {
- return 0;
- }
-
- return v;
-}
-
static bool inline initialize(const std::multimap<std::wstring, unsigned long long int> &pids, const wchar_t *procname, const wchar_t *modname = NULL) {
pModule = 0;
diff --git a/plugins/mumble_plugin_main.h b/plugins/mumble_plugin_main.h
new file mode 100644
index 000000000..26539a6a6
--- /dev/null
+++ b/plugins/mumble_plugin_main.h
@@ -0,0 +1,43 @@
+// Copyright 2005-2019 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>.
+
+#ifndef MUMBLE_PLUGIN_MAIN_H_
+#define MUMBLE_PLUGIN_MAIN_H_
+
+#include "mumble_plugin.h"
+
+static procid_t pPid;
+static bool is64Bit;
+static procptr_t pModule;
+
+static inline bool peekProc(const procptr_t &addr, void *dest, const size_t &len);
+static inline procptr_t getModuleAddr(const procid_t &pid, const wchar_t *modname);
+
+static inline procptr_t getModuleAddr(const wchar_t *modname) {
+ return getModuleAddr(pPid, modname);
+}
+
+template<class T>
+static inline bool peekProc(const procptr_t &addr, T &dest) {
+ return peekProc(addr, &dest, sizeof(T));
+}
+
+static inline procptr_t peekProcPtr(const procptr_t &addr) {
+ procptr_t v = 0;
+
+ if (!peekProc(addr, &v, is64Bit ? 8 : 4)) {
+ return 0;
+ }
+
+ return v;
+}
+
+#ifdef WIN32
+# include "../mumble_plugin_win32.h"
+#else
+# include "../mumble_plugin_linux.h"
+#endif
+
+#endif
diff --git a/plugins/mumble_plugin_win32.h b/plugins/mumble_plugin_win32.h
index 6ed9b2f3e..d2cf22043 100644
--- a/plugins/mumble_plugin_win32.h
+++ b/plugins/mumble_plugin_win32.h
@@ -6,6 +6,8 @@
#ifndef MUMBLE_MUMBLE_PLUGIN_WIN32_H_
#define MUMBLE_MUMBLE_PLUGIN_WIN32_H_
+#include "mumble_plugin_main.h"
+
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <stdlib.h>
@@ -21,16 +23,11 @@
#include <iostream>
#include <stdint.h>
-#include "mumble_plugin.h"
-
-DWORD dwPid;
-int is64Bit;
static HANDLE hProcess;
-static procptr_t pModule;
-static inline DWORD getProcess(const wchar_t *exename) {
+static inline procid_t getProcess(const wchar_t *exename) {
PROCESSENTRY32 pe;
- DWORD pid = 0;
+ procid_t pid = 0;
pe.dwSize = sizeof(pe);
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
@@ -49,8 +46,7 @@ static inline DWORD getProcess(const wchar_t *exename) {
return pid;
}
-static inline int checkProcessIs64Bit(const DWORD pid)
-{
+static inline int checkProcessIs64Bit(const procid_t &pid) {
// This function returns 0 if the process is 32-bit and 1 if it's 64-bit.
// In case of failure, it returns -1.
@@ -75,7 +71,7 @@ static inline int checkProcessIs64Bit(const DWORD pid)
return !is32;
}
-static inline procptr_t getModuleAddr(DWORD pid, const wchar_t *modname) {
+static inline procptr_t getModuleAddr(const procid_t &pid, const wchar_t *modname) {
MODULEENTRY32 me;
procptr_t ret = 0;
me.dwSize = sizeof(me);
@@ -96,70 +92,49 @@ static inline procptr_t getModuleAddr(DWORD pid, const wchar_t *modname) {
return ret;
}
-static inline procptr_t getModuleAddr(const wchar_t *modname) {
- return getModuleAddr(dwPid, modname);
-}
-
-static inline bool peekProc(procptr_t base, VOID *dest, SIZE_T len) {
+static inline bool peekProc(const procptr_t &addr, void *dest, const size_t &len) {
SIZE_T r;
- uintptr_t addr = static_cast<uintptr_t>(base);
- BOOL ok=ReadProcessMemory(hProcess, reinterpret_cast<VOID *>(addr), dest, len, &r);
+ const BOOL ok = ReadProcessMemory(hProcess, reinterpret_cast<void *>(addr), dest, len, &r);
return (ok && (r == len));
}
-template<class T>
-bool peekProc(procptr_t base, T &dest) {
- SIZE_T r;
- uintptr_t addr = static_cast<uintptr_t>(base);
- BOOL ok=ReadProcessMemory(hProcess, reinterpret_cast<VOID *>(addr), reinterpret_cast<VOID *>(& dest), sizeof(T), &r);
- return (ok && (r == sizeof(T)));
-}
-
-static inline procptr_t peekProcPtr(procptr_t base) {
- procptr_t v = 0;
-
- if (!peekProc(base, &v, is64Bit ? 8 : 4)) {
- return 0;
- }
-
- return v;
-}
-
static bool inline initialize(const std::multimap<std::wstring, unsigned long long int> &pids, const wchar_t *procname, const wchar_t *modname = NULL) {
hProcess = NULL;
pModule = 0;
- if (! pids.empty()) {
+ if (!pids.empty()) {
std::multimap<std::wstring, unsigned long long int>::const_iterator iter = pids.find(std::wstring(procname));
if (iter != pids.end())
- dwPid = static_cast<DWORD>(iter->second);
+ pPid = static_cast<procid_t>(iter->second);
else
- dwPid = 0;
+ pPid = 0;
} else {
- dwPid=getProcess(procname);
+ pPid = getProcess(procname);
}
- if (!dwPid)
+ if (!pPid)
return false;
- int result = checkProcessIs64Bit(dwPid);
+ const int result = checkProcessIs64Bit(pPid);
if (result == -1) {
- dwPid = 0;
+ pPid = 0;
return false;
}
- is64Bit = result;
+ // We compare to 1 to prevent the following warning:
+ // C4800: 'BOOL': forcing value to bool 'true' or 'false' (performance warning)
+ is64Bit = (result == 1);
- pModule=getModuleAddr(modname ? modname : procname);
+ pModule = getModuleAddr(modname ? modname : procname);
if (!pModule) {
- dwPid = 0;
+ pPid = 0;
return false;
}
- hProcess=OpenProcess(PROCESS_VM_READ, false, dwPid);
+ hProcess = OpenProcess(PROCESS_VM_READ, false, pPid);
if (!hProcess) {
- dwPid = 0;
+ pPid = 0;
pModule = 0;
return false;
}
@@ -172,7 +147,7 @@ static void generic_unlock() {
CloseHandle(hProcess);
hProcess = NULL;
pModule = 0;
- dwPid = 0;
+ pPid = 0;
}
}