|
| 1 | +// |
| 2 | +// DynLibUtils |
| 3 | +// Copyright (C) 2023-2025 Vladimir Ezhikov (Wend4r), Borys Komashchenko (Phoenix), Nikita Ushakov (qubka) |
| 4 | +// Licensed under the MIT license. See LICENSE file in the project root for details. |
| 5 | +// |
| 6 | + |
| 7 | +#ifndef DYNLIBUTILS_MEMACCESSOR_H |
| 8 | +#define DYNLIBUTILS_MEMACCESSOR_H |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include "memaddr.hpp" |
| 12 | +#include "protflag.hpp" |
| 13 | + |
| 14 | +namespace DynLibUtils |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @class MemAccessor |
| 18 | + * @brief A class providing various memory access routines. |
| 19 | + */ |
| 20 | + class CMemAccessor |
| 21 | + { |
| 22 | + public: |
| 23 | + /** |
| 24 | + * @brief Defines a memory read/write routine that may fail ungracefully. |
| 25 | + * It's expected this library will only ever use this routine in cases that are expected to succeed. |
| 26 | + * @param dest The destination memory address. |
| 27 | + * @param src The source memory address. |
| 28 | + * @param size The number of bytes to copy. |
| 29 | + * @return True if the memory copy succeeds, false otherwise. |
| 30 | + */ |
| 31 | + static bool MemCopy(CMemory dest, CMemory src, size_t size); |
| 32 | + |
| 33 | + /** |
| 34 | + * @brief Defines a memory write routine that will not throw exceptions, and can handle potential |
| 35 | + * writes to NO_ACCESS or otherwise inaccessible memory pages. Defaults to WriteProcessMemory. |
| 36 | + * Must fail gracefully. |
| 37 | + * @param dest The destination memory address. |
| 38 | + * @param src The source memory address. |
| 39 | + * @param size The number of bytes to copy. |
| 40 | + * @param written A reference to the variable that will hold the number of bytes successfully written. |
| 41 | + * @return True if the memory copy succeeds, false otherwise. |
| 42 | + */ |
| 43 | + static bool SafeMemCopy(CMemory dest, CMemory src, size_t size, size_t& written) noexcept; |
| 44 | + |
| 45 | + /** |
| 46 | + * @brief Defines a memory read routine that will not throw exceptions, and can handle potential |
| 47 | + * reads from NO_ACCESS or otherwise inaccessible memory pages. Defaults to ReadProcessMemory. |
| 48 | + * Must fail gracefully. |
| 49 | + * @param src The source memory address. |
| 50 | + * @param dest The destination memory address. |
| 51 | + * @param size The number of bytes to read. |
| 52 | + * @param read A reference to the variable that will hold the number of bytes successfully read. |
| 53 | + * @return True if the memory read succeeds, false otherwise. |
| 54 | + */ |
| 55 | + static bool SafeMemRead(CMemory src, CMemory dest, size_t size, size_t& read) noexcept; |
| 56 | + |
| 57 | + /** |
| 58 | + * @brief Defines a memory protection set/unset routine that may fail ungracefully. |
| 59 | + * @param dest The memory address to change protection for. |
| 60 | + * @param size The number of bytes to change protection for. |
| 61 | + * @param newProtection The new protection flags to set. |
| 62 | + * @param status A reference to a variable that will hold the success status of the operation. |
| 63 | + * @return The previous protection flags if the operation succeeds, otherwise an appropriate error code. |
| 64 | + */ |
| 65 | + static ProtFlag MemProtect(CMemory dest, size_t size, ProtFlag newProtection, bool& status); |
| 66 | + }; |
| 67 | + |
| 68 | + static constexpr size_t MemoryRound(size_t numToRound, size_t multiple) |
| 69 | + { |
| 70 | + return numToRound & (static_cast<size_t>(-1) ^ (multiple - 1)); |
| 71 | + } |
| 72 | + |
| 73 | + static constexpr size_t MemoryRoundUp(size_t numToRound, size_t multiple) |
| 74 | + { |
| 75 | + return (numToRound + (multiple - 1)) & (static_cast<size_t>(-1) ^ (multiple - 1)); |
| 76 | + } |
| 77 | +} // namespace DynLibUtils |
| 78 | + |
| 79 | +#endif //DYNLIBUTILS_MEMACCESSOR_H |
0 commit comments