Skip to content

Commit 638ec75

Browse files
committed
refactor: refactoring map find
1 parent 411e6ea commit 638ec75

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

src/pl/SymbolProvider.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void* pl_resolve_symbol(const char* symbolName) {
151151
Error("pl_resolve_symbol called before init");
152152
return nullptr;
153153
}
154-
auto iter = funcMap->find(std::string(symbolName));
154+
auto iter = funcMap->find(std::string_view(symbolName));
155155
if (iter == funcMap->end()) {
156156
Error("Could not find function in memory: {}", symbolName);
157157
Error("Plugin: {}", pl::utils::GetCallerModuleFileName());
@@ -162,7 +162,14 @@ void* pl_resolve_symbol(const char* symbolName) {
162162

163163
void* pl_resolve_symbol_silent(const char* symbolName) {
164164
if (!funcMap) { return nullptr; }
165-
auto iter = funcMap->find(std::string(symbolName));
165+
auto iter = funcMap->find(std::string_view(symbolName));
166+
if (iter == funcMap->end()) { return nullptr; }
167+
return (void*)(imageBaseAddr + iter->second);
168+
}
169+
170+
void* pl_resolve_symbol_silent_n(const char* symbolName, size_t size) {
171+
if (!funcMap) { return nullptr; }
172+
auto iter = funcMap->find(std::string_view(symbolName, size));
166173
if (iter == funcMap->end()) { return nullptr; }
167174
return (void*)(imageBaseAddr + iter->second);
168175
}

src/pl/SymbolProvider.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ PLCAPI void* pl_resolve_symbol(const char* symbolName);
3232
*/
3333
PLCAPI void* pl_resolve_symbol_silent(const char* symbolName);
3434

35+
/**
36+
* @brief Resolve a symbol name to a function address.
37+
*
38+
* @param symbolName [in] The symbol name.
39+
* @param symbolName [in] The name size.
40+
* @return result The function address. nullptr if the function is not found.
41+
*
42+
* @note This function will not print error message if the function is not found.
43+
*/
44+
PLCAPI void* pl_resolve_symbol_silent_n(const char* symbolName, size_t size);
45+
3546
/**
3647
* @brief Get the symbol name of a function address.
3748
*

src/pl/internal/StringUtils.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@
1212
#include <winnls.h>
1313

1414
std::wstring pl::utils::str2wstr(const std::string& str, UINT codePage) {
15-
auto len = MultiByteToWideChar(codePage, 0, str.c_str(), -1, nullptr, 0);
16-
auto* buffer = new wchar_t[len];
17-
MultiByteToWideChar(codePage, 0, str.c_str(), -1, buffer, len);
18-
std::wstring result = std::wstring(buffer);
19-
delete[] buffer;
20-
return result;
15+
int len = MultiByteToWideChar(codePage, 0, str.data(), (int)str.size(), nullptr, 0);
16+
std::wstring wstr;
17+
if (len == 0) { return wstr; }
18+
wstr.reserve(len);
19+
MultiByteToWideChar(codePage, 0, str.data(), (int)str.size(), wstr.data(), len);
20+
return wstr;
2121
}
2222

2323
std::wstring pl::utils::str2wstr(const std::string& str) { return str2wstr(str, CP_UTF8); }
2424

2525
std::string pl::utils::wstr2str(const std::wstring& wstr) {
26-
auto len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
27-
char* buffer = new char[len];
28-
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, buffer, len, nullptr, nullptr);
29-
std::string result = std::string(buffer);
30-
delete[] buffer;
31-
return result;
26+
int len = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), nullptr, 0, nullptr, nullptr);
27+
std::string ret;
28+
if (len == 0) { return ret; }
29+
ret.reserve(len);
30+
WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), ret.data(), (int)ret.size(), nullptr, nullptr);
31+
return ret;
3232
}
3333

3434
std::vector<std::string_view> pl::utils::split(std::string_view s, std::string_view delimiter) {

xmake.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ add_requires("demangler ~17")
1818
target("PreLoader")
1919
set_kind("shared")
2020
set_languages("c++20")
21-
set_symbols("debug")
21+
set_symbols("debug")
22+
set_exceptions("none")
2223
add_headerfiles("src/(**.h)")
2324
add_includedirs("./src")
2425
add_defines("PRELOADER_EXPORT", "UNICODE")
25-
add_cxflags("/utf-8")
26+
add_cxflags("/utf-8", "/EHa")
2627
add_files("src/**.cpp")
2728
add_packages("raw_pdb", "nlohmann_json", "parallel-hashmap", "demangler", "detours", "fmt", "pe_bliss")

0 commit comments

Comments
 (0)