searchInBuffer() searches for the specified pattern in the specified buffer. "?" is used as wildcard.
findPattern(), given a start address and the size of the area, reads memory in chunks of 32 KiB.
It stops when a match is found, the end is reached or an error is encountered (peek() fails).
There's also an overload which iterates through the specified module's readable regions.
Previously, only module() was present: it retrieved the base address of the specified module.
It worked fine, but it iterated through the process' modules every time it was called.
This commit replaces it with modules(), which returns an std::unordered_map containing all modules.
The map uses the module name as key and Module as value.
Aside from the performance improvement, the new code also provides info for each module region:
- Start address.
- Size.
- Whether it's readable, writable and/or executable.
"wchar_t" is usually 4 bytes big. That's not the case on Windows, where it's 2 bytes instead.
We cannot rely on that type on Linux because the target process could be native or running through Wine.
Also, it's possible that a game uses the same size for "wchar_t" on both Linux and Windows.
This commit introduces two variants of utf16ToUtf8():
1. Accepts std::u16string, for Windows processes.
2. Accepts std::u32string, for Linux processes.
The old variant, which accepted std::wstring, is removed to prevent misuses.
The new function is going to be used in a later commit.
readAll() is moved to mumble_plugin_utils.h and is renamed to readFile(), to make its purpose clearer.
This commit also moves the <math.h> include to mumble_plugin_utils.h, because that's where it's required.
Also, it's now included as <cmath>, as recommended for C++.
- peekProcString(): reads the specified amount of data at the specified address and returns it as std::string. An empty std::string is returned in case of error.
If length is 0, the function reads one byte at a time and stops when either '\0' is found or 3 seconds have passed.
The successfully read data is returned, also in case of error.
- peekProcVector(): can be used to read a dynamic array (size known at runtime) from the process' memory.
Dynamic arrays are part of C99, which should be supported by most compilers nowadays, but std::vector provides many advantages (e.g. easy resize) over a raw array.
- getVirtualFunction(): gets the address of a virtual function given its index and the class object's base address.
A macro called GET_POINTER_SIZE is added because "is64Bit ? 8 : 4" is used in two places now. Also, it's useful and intuitive for plugin(s) programmers.
- sinCos(): calculates sine and cosine of the specified value. On Linux the calculation is guaranteed to be simultaneous.
- degreesToRadians(): converts degrees to radians.
- isBigEndian(): returns whether the architecture is big-endian, by checking how a 4-byte value is stored in memory.
- networkToHost() converts from network byte order to host byte order.
I decided to create our own function because ntohs() is part of winsock(2).h on Windows, which means we would have to link to "ws2_32".
This commit also adds and makes use of the new "OS_WINDOWS" and "OS_LINUX" definitions.
The reason behind their existence is the "_WIN32" and "__linux__" definitions not being intuitive, mainly because they don't follow the same naming convention.
Fixes the following warning:
../mumble_plugin_utils.h:33:13: error: 'void escape(char*, size_t)' defined but not used [-Werror=unused-function]
static void escape(char *str, size_t size) {
^
It appeared after bb248cc, due to "ut99.cpp" not using escape().
This commit also:
- Changes the "size" argument so that it is passed as reference.
- Indents the function using tabs.