Skip to content

Commit

Permalink
Implement the ApiReader class.
Browse files Browse the repository at this point in the history
  • Loading branch information
BeneficialCode committed Jan 8, 2025
1 parent 35c09a5 commit 477cb30
Show file tree
Hide file tree
Showing 6 changed files with 1,026 additions and 1 deletion.
15 changes: 14 additions & 1 deletion PEParser/PEParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,26 @@ std::vector<ExportedSymbol> PEParser::GetExports() const {
auto ordinalBase = data->Base;

std::unordered_map<uint32_t, std::string> functionNamesMap;
std::unordered_map<uint32_t, uint32_t> hintsMap;
for (uint32_t idx = 0; idx < data->NumberOfNames; idx++) {
uint16_t ordinal;
ordinal = *(USHORT*)(ordinals + idx * 2) + (USHORT)ordinalBase;
uint32_t name;
auto offset = *(ULONG*)(names + idx * 4);
functionNamesMap[ordinal] = (PCSTR)GetAddress(offset);
hintsMap[ordinal] = idx;
}

for (DWORD i = 0; i < data->NumberOfFunctions; i++) {
ExportedSymbol symbol;
int ordinal = i + (USHORT)ordinalBase;
symbol.Ordinal = ordinal;

std::unordered_map<uint32_t,uint32_t>::iterator iter = hintsMap.find(ordinal);
if (iter != hintsMap.end()) {
symbol.Hint = iter->second;
}

bool hasName = false;
auto pos = functionNamesMap.find(ordinal);
if (pos != functionNamesMap.end()) {
Expand All @@ -163,10 +171,13 @@ std::vector<ExportedSymbol> PEParser::GetExports() const {
}
DWORD address = *(functions + symbol.Ordinal - ordinalBase);
symbol.Address = address;
//auto offset = RvaToFileOffset(address);
symbol.IsForward = false;
symbol.HasName = false;
if (hasName) {
symbol.HasName = true;
if (address > dir->VirtualAddress && address < dir->VirtualAddress + dir->Size) {
symbol.ForwardName = (PCSTR)GetAddress(address);
symbol.IsForward = true;
}
}
exports.push_back(std::move(symbol));
Expand Down Expand Up @@ -409,6 +420,8 @@ ULONG PEParser::GetEAT() const {
return eat;
}



DWORD PEParser::GetImageSize() const {
return IsPe64() ? GetOptionalHeader64().SizeOfImage : GetOptionalHeader32().SizeOfImage;
}
Expand Down
4 changes: 4 additions & 0 deletions PEParser/PEParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ struct ExportedSymbol {
std::string ForwardName;
DWORD Address;
unsigned short Ordinal;
bool IsForward;
WORD Hint;
bool HasName;
};

struct ImportedSymbol {
Expand Down Expand Up @@ -224,6 +227,7 @@ class PEParser final {
std::vector<RelocInfo> GetRelocs(void* imageBase);
static void RelocateImageByDelta(std::vector<RelocInfo>& relocs, const uint64_t delta);


private:
bool IsObjectPe64() const;
void CheckValidity();
Expand Down
Loading

0 comments on commit 477cb30

Please sign in to comment.