libPeConv
A library to load, manipulate, dump PE files.
Loading...
Searching...
No Matches
function_resolver.cpp
Go to the documentation of this file.
2
3#include "peconv/logger.h"
4#include <cctype>
5
6namespace util {
7 std::string toLowercase(std::string str)
8 {
9 for (char& ch : str) {
10 ch = std::tolower(static_cast<unsigned char>(ch));
11 }
12 return str;
13 }
14}; //namespace util
15
17{
18 if (!lib_name) {
19 return nullptr;
20 }
21 const std::string modName = util::toLowercase(lib_name);
22 auto found = this->nameToModule.find(modName);
23 if (found != this->nameToModule.end()) {
24 return found->second;
25 }
26 const HMODULE mod = LoadLibraryA(lib_name);
27 if (mod) {
28 LOG_DEBUG("Loaded DLL: %s at %p.", lib_name, mod);
29 this->nameToModule[modName] = mod;
30 }
31 return mod;
32}
33
34FARPROC peconv::default_func_resolver::resolve_func(LPCSTR lib_name, LPCSTR func_name)
35{
36 HMODULE libBasePtr = load_library(lib_name);
37 if (libBasePtr == NULL) {
38 LOG_ERROR("Could not load the library: %s.", lib_name);
39 return NULL;
40 }
41 FARPROC hProc = GetProcAddress(libBasePtr, func_name);
42 if (hProc == NULL) {
43 ULONGLONG func_val = (ULONGLONG)func_name;
44 //is only the first WORD filled?
45 bool is_ord = (func_val & (0x0FFFF)) == func_val;
46 if (is_ord) {
47 LOG_ERROR("Could not load the function: %s.0x%llx.", lib_name, (unsigned long long)func_val);
48 } else {
49 LOG_ERROR("Could not load the function: %s.%s.", lib_name, func_name);
50 }
51 return NULL;
52 }
53 return hProc;
54}
virtual FARPROC resolve_func(LPCSTR lib_name, LPCSTR func_name)
std::map< std::string, HMODULE > nameToModule
virtual HMODULE load_library(LPCSTR lib_name)
Definitions of basic Imports Resolver classes. They can be used for filling imports when the PE is lo...
#define LOG_DEBUG(fmt,...)
Definition logger.h:56
#define LOG_ERROR(fmt,...)
Definition logger.h:36
std::string toLowercase(std::string str)