rdbo / libmem

Advanced Game Hacking Library for C, Modern C++, Rust and Python (Windows/Linux/FreeBSD) (Process/Memory Hacking) (Hooking/Detouring) (Cross Platform) (x86/x64/ARM/ARM64) (DLL/SO Injection) (Internal/External) (Assembler/Disassembler)
GNU Affero General Public License v3.0
784 stars 91 forks source link

Regular Expression support for search functions #61

Closed rdbo closed 1 year ago

rdbo commented 1 year ago

Functions like LM_GetModuleEx and LM_GetProcessIdEx could have regex support for more complex searching.

rdbo commented 1 year ago

This library seems to be the tool for the job: https://github.com/laurikari/tre It supports group capturing, has POSIX compliance and wide char support.

rdbo commented 1 year ago

Example:

#include <tre/tre.h>
#include <locale.h>
#include <stdio.h>

int
main()
{
        const char *text = "Hello, test";
        const wchar_t *wtext = L"Привет, узер";
        regex_t regex;
        regmatch_t matches[2];

        setlocale(LC_ALL, "");

        tre_regcomp(&regex, "Hello, (.*)$", REG_EXTENDED);
        tre_regexec(&regex, text, 2, matches, 0);
        wprintf(L"%s\n", text);
        wprintf(L"Username: %s\n", &text[matches[1].rm_so]);
        tre_regfree(&regex);

        tre_regwcomp(&regex, L"Привет, (.*)$", REG_EXTENDED);
        tre_regwexec(&regex, wtext, 2, matches, 0);
        wprintf(L"%ls\n", wtext);
        wprintf(L"Username: %ls\n", &wtext[matches[1].rm_so]);
        tre_regfree(&regex);

        return 0;
}

Output:

Hello, test
Username: test
Привет, узер
Username: узер
rdbo commented 1 year ago

Example CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)

project(test-tre)

file(GLOB TRE_SRC "${PROJECT_SOURCE_DIR}/lib/*.c")
add_library(tre STATIC ${TRE_SRC})
target_compile_definitions(tre PUBLIC HAVE_ALLOCA=1 HAVE_ALLOCA_H=1 HAVE_MBRTOWC=1 HAVE_SYS_TYPES_H=0 HAVE_WCHAR_H=1 HAVE_MBSTATE_T=1 TRE_APPROX=1 TRE_MULTIBYTE=1 TRE_USE_ALLOCA=1 TRE_WCHAR=1 TRE_REGEX_T_FIELD=value)

add_executable(test test.c)
target_link_libraries(test PUBLIC tre)
target_include_directories(tre PUBLIC "${PROJECT_SOURCE_DIR}/include")
rdbo commented 1 year ago

Had some problems adapting the already present regex functions to TRE's, the results seem to differ.

rdbo commented 1 year ago

This may be a little too overkill for libmem. After all, how many times has someone needed to find a module through a regex, instead of just the module name? Closing for now.