// Core code from Hooking.Patterns // https://github.com/ThirteenAG/Hooking.Patterns #pragma once #include "../common/stdinc.h" class memory_pointer { public: memory_pointer() : _address(0) {} memory_pointer(const void *pointer) : _address(reinterpret_cast(pointer)) {} explicit memory_pointer(std::intptr_t address) : _address(address) {} template T *p(std::intptr_t offset = 0) const { return reinterpret_cast(i(offset)); } std::intptr_t i(std::intptr_t offset = 0) const { return (_address + offset); } memory_pointer &operator=(const void *pointer) { _address = reinterpret_cast(pointer); return *this; } memory_pointer &operator=(std::intptr_t address) { _address = address; return *this; } template operator T *() const { return p(); } operator std::intptr_t() const { return _address; } private: std::intptr_t _address; }; class byte_pattern { public: typedef std::vector result_type; byte_pattern(); byte_pattern &set_pattern(const char *pattern_literal); byte_pattern &set_pattern(const void *pattern_binary, std::size_t size); byte_pattern &reset_module(); byte_pattern &set_module(memory_pointer module); byte_pattern &set_range(memory_pointer beg, memory_pointer end); byte_pattern &search(); byte_pattern &find_pattern(const char *pattern_literal); byte_pattern &find_pattern(const void *pattern_binary, std::size_t size); memory_pointer get(std::size_t index) const; result_type get() const; memory_pointer get_first() const; std::size_t count() const; bool has_size(std::size_t expected) const; bool empty() const; void clear(); private: std::pair _range; std::vector _bpattern; std::vector _mask; result_type _results; std::string _pattern; std::ptrdiff_t _bmbc[256]; static std::vector split_pattern(const char *literal); static std::pair parse_sub_pattern(const std::string &sub); void transform_pattern(const char *literal); void get_module_range(memory_pointer module); void bm_preprocess(); void bm_search(); static std::string make_bytes_literal(memory_pointer pointer, std::size_t length); };