diff --git a/dump-pe/main.cpp b/dump-pe/main.cpp index e6982fd..2a42851 100644 --- a/dump-pe/main.cpp +++ b/dump-pe/main.cpp @@ -28,7 +28,6 @@ THE SOFTWARE. #include <parser-library/parse.h> -using namespace std; using namespace peparse; int printExps(void *N, VA funcAddr, std::string &mod, std::string &func) { @@ -41,19 +40,22 @@ int printExps(void *N, VA funcAddr, std::string &mod, std::string &func) { std::cout << "!"; std::cout << func; std::cout << ": 0x"; - std::cout << to_string<decltype(address)>(address, hex); - std::cout << endl; + std::cout << to_string<decltype(address)>(address, std::hex); + std::cout << "\n"; return 0; } -int printImports(void *N, VA impAddr, string &modName, string &symName) { +int printImports(void *N, + VA impAddr, + const std::string &modName, + const std::string &symName) { static_cast<void>(N); auto address = static_cast<std::uint32_t>(impAddr); - std::cout << "0x" << to_string<decltype(address)>(address, hex); + std::cout << "0x" << to_string<decltype(address)>(address, std::hex); std::cout << " " << modName << "!" << symName; - std::cout << endl; + std::cout << "\n"; return 0; } @@ -91,7 +93,7 @@ int printRelocs(void *N, VA relocAddr, reloc_type type) { break; } - std::cout << " VA: 0x" << to_string<VA>(relocAddr, hex) << endl; + std::cout << " VA: 0x" << to_string<VA>(relocAddr, std::hex) << "\n"; return 0; } @@ -105,8 +107,9 @@ int printSymbols(void *N, uint8_t &numberOfAuxSymbols) { static_cast<void>(N); - std::cout << "Symbol Name: " << strName << endl; - std::cout << "Symbol Value: 0x" << to_string<uint32_t>(value, hex) << endl; + std::cout << "Symbol Name: " << strName << "\n"; + std::cout << "Symbol Value: 0x" + << to_string<std::uint32_t>(value, std::hex) << "\n"; std::cout << "Symbol Section Number: "; switch (sectionNumber) { @@ -123,7 +126,7 @@ int printSymbols(void *N, std::cout << sectionNumber; break; } - std::cout << endl; + std::cout << "\n"; std::cout << "Symbol Type: "; switch (type) { @@ -179,7 +182,7 @@ int printSymbols(void *N, std::cout << "UNKNOWN"; break; } - std::cout << endl; + std::cout << "\n"; std::cout << "Symbol Storage Class: "; switch (storageClass) { @@ -217,10 +220,10 @@ int printSymbols(void *N, std::cout << "UNKNOWN"; break; } - std::cout << endl; + std::cout << "\n"; std::cout << "Symbol Number of Aux Symbols: " - << static_cast<uint32_t>(numberOfAuxSymbols) << endl; + << static_cast<std::uint32_t>(numberOfAuxSymbols) << "\n"; return 0; } @@ -229,37 +232,43 @@ int printRsrc(void *N, resource r) { static_cast<void>(N); if (r.type_str.length()) - std::cout << "Type (string): " << r.type_str << endl; + std::cout << "Type (string): " << r.type_str << "\n"; else - std::cout << "Type: 0x" << to_string<uint32_t>(r.type, hex) << endl; + std::cout << "Type: 0x" + << to_string<std::uint32_t>(r.type, std::hex) << "\n"; if (r.name_str.length()) - std::cout << "Name (string): " << r.name_str << endl; + std::cout << "Name (string): " << r.name_str << "\n"; else - std::cout << "Name: 0x" << to_string<uint32_t>(r.name, hex) << endl; + std::cout << "Name: 0x" + << to_string<std::uint32_t>(r.name, std::hex) << "\n"; if (r.lang_str.length()) - std::cout << "Lang (string): " << r.lang_str << endl; + std::cout << "Lang (string): " << r.lang_str << "\n"; else - std::cout << "Lang: 0x" << to_string<uint32_t>(r.lang, hex) << endl; - std::cout << "Codepage: 0x" << to_string<uint32_t>(r.codepage, hex) << endl; - std::cout << "RVA: " << to_string<uint32_t>(r.RVA, dec) << endl; - std::cout << "Size: " << to_string<uint32_t>(r.size, dec) << endl; + std::cout << "Lang: 0x" + << to_string<std::uint32_t>(r.lang, std::hex) << "\n"; + std::cout << "Codepage: 0x" + << to_string<std::uint32_t>(r.codepage, std::hex) << "\n"; + std::cout << "RVA: " << to_string<std::uint32_t>(r.RVA, std::dec) << "\n"; + std::cout << "Size: " << to_string<std::uint32_t>(r.size, std::dec) << "\n"; return 0; } int printSecs(void *N, VA secBase, - string &secName, + std::string &secName, image_section_header s, bounded_buffer *data) { static_cast<void>(N); static_cast<void>(s); - std::cout << "Sec Name: " << secName << endl; - std::cout << "Sec Base: 0x" << to_string<uint64_t>(secBase, hex) << endl; + std::cout << "Sec Name: " << secName << "\n"; + std::cout << "Sec Base: 0x" + << to_string<std::uint64_t>(secBase, std::hex) << "\n"; if (data) - std::cout << "Sec Size: " << to_string<uint64_t>(data->bufLen, dec) << endl; + std::cout << "Sec Size: " + << to_string<std::uint64_t>(data->bufLen, std::dec) << "\n"; else - std::cout << "Sec Size: 0" << endl; + std::cout << "Sec Size: 0" << "\n"; return 0; } @@ -267,12 +276,12 @@ int printSecs(void *N, std::cout << "" #x << ": 0x"; \ std::cout << to_string<std::uint32_t>( \ static_cast<std::uint32_t>(p->peHeader.nt.x), std::hex) \ - << endl; + << "\n"; #define DUMP_DEC_FIELD(x) \ std::cout << "" #x << ": "; \ - std::cout << to_string<uint32_t>( \ + std::cout << to_string<std::uint32_t>( \ static_cast<std::uint32_t>(p->peHeader.nt.x), std::dec) \ - << endl; + << "\n"; int main(int argc, char *argv[]) { if (argc != 2 || (argc == 2 && std::strcmp(argv[1], "--help") == 0)) { @@ -352,15 +361,15 @@ int main(int argc, char *argv[]) { #undef DUMP_FIELD #undef DUMP_DEC_FIELD - std::cout << "Imports: " << endl; + std::cout << "Imports: " << "\n"; IterImpVAString(p, printImports, NULL); - std::cout << "Relocations: " << endl; + std::cout << "Relocations: " << "\n"; IterRelocs(p, printRelocs, NULL); - std::cout << "Symbols (symbol table): " << endl; + std::cout << "Symbols (symbol table): " << "\n"; IterSymbols(p, printSymbols, NULL); - std::cout << "Sections: " << endl; + std::cout << "Sections: " << "\n"; IterSec(p, printSecs, NULL); - std::cout << "Exports: " << endl; + std::cout << "Exports: " << "\n"; IterExpVA(p, printExps, NULL); // read the first 8 bytes from the entry point and print them @@ -368,24 +377,24 @@ int main(int argc, char *argv[]) { if (GetEntryPoint(p, entryPoint)) { std::cout << "First 8 bytes from entry point (0x"; - std::cout << to_string<VA>(entryPoint, hex); - std::cout << "):" << endl; + std::cout << to_string<VA>(entryPoint, std::hex); + std::cout << "):" << "\n"; for (std::size_t i = 0; i < 8; i++) { - ::uint8_t b; + std::uint8_t b; ReadByteAtVA(p, i + entryPoint, b); - std::cout << " 0x" << to_string<uint32_t>(b, hex); + std::cout << " 0x" << to_string<std::uint32_t>(b, std::hex); } - std::cout << endl; + std::cout << "\n"; } - std::cout << "Resources: " << endl; + std::cout << "Resources: " << "\n"; IterRsrc(p, printRsrc, NULL); DestructParsedPE(p); } else { std::cout << "Error: " << GetPEErr() << " (" << GetPEErrString() << ")" - << endl; - std::cout << "Location: " << GetPEErrLoc() << endl; + << "\n"; + std::cout << "Location: " << GetPEErrLoc() << "\n"; } return 0; diff --git a/pe-parser-library/include/parser-library/nt-headers.h b/pe-parser-library/include/parser-library/nt-headers.h index db538b3..c284297 100644 --- a/pe-parser-library/include/parser-library/nt-headers.h +++ b/pe-parser-library/include/parser-library/nt-headers.h @@ -29,7 +29,7 @@ THE SOFTWARE. #define _offset(t, f) \ static_cast<std::uint32_t>( \ - reinterpret_cast<ptrdiff_t>(&static_cast<t *>(nullptr)->f)) + reinterpret_cast<std::ptrdiff_t>(&static_cast<t *>(nullptr)->f)) // need to pack these structure definitions diff --git a/pe-parser-library/include/parser-library/parse.h b/pe-parser-library/include/parser-library/parse.h index 194af11..ed6d4c6 100644 --- a/pe-parser-library/include/parser-library/parse.h +++ b/pe-parser-library/include/parser-library/parse.h @@ -181,7 +181,7 @@ typedef int (*iterRsrc)(void *, resource); void IterRsrc(parsed_pe *pe, iterRsrc cb, void *cbd); // iterate over the imports by RVA and string -typedef int (*iterVAStr)(void *, VA, std::string &, std::string &); +typedef int (*iterVAStr)(void *, VA, const std::string &, const std::string &); void IterImpVAString(parsed_pe *pe, iterVAStr cb, void *cbd); // iterate over relocations in the PE file diff --git a/pe-parser-library/src/parse.cpp b/pe-parser-library/src/parse.cpp index acf7047..3210712 100644 --- a/pe-parser-library/src/parse.cpp +++ b/pe-parser-library/src/parse.cpp @@ -25,19 +25,17 @@ THE SOFTWARE. #include <algorithm> #include <cstring> #include <iostream> -#include <list> +#include <vector> #include <stdexcept> #include <parser-library/nt-headers.h> #include <parser-library/parse.h> #include <parser-library/to_string.h> -using namespace std; - namespace peparse { struct section { - string sectionName; + std::string sectionName; std::uint64_t sectionBase; bounded_buffer *sectionData; image_section_header sec; @@ -45,14 +43,14 @@ struct section { struct importent { VA addr; - string symbolName; - string moduleName; + std::string symbolName; + std::string moduleName; }; struct exportent { VA addr; - string symbolName; - string moduleName; + std::string symbolName; + std::string moduleName; }; struct reloc { @@ -64,64 +62,64 @@ struct reloc { #define SYMBOL_TYPE_HI(x) (x.type >> 8) union symbol_name { - uint8_t shortName[NT_SHORT_NAME_LEN]; - uint32_t zeroes; - uint64_t data; + std::uint8_t shortName[NT_SHORT_NAME_LEN]; + std::uint32_t zeroes; + std::uint64_t data; }; struct aux_symbol_f1 { - uint32_t tagIndex; - uint32_t totalSize; - uint32_t pointerToLineNumber; - uint32_t pointerToNextFunction; + std::uint32_t tagIndex; + std::uint32_t totalSize; + std::uint32_t pointerToLineNumber; + std::uint32_t pointerToNextFunction; }; struct aux_symbol_f2 { - uint16_t lineNumber; - uint32_t pointerToNextFunction; + std::uint16_t lineNumber; + std::uint32_t pointerToNextFunction; }; struct aux_symbol_f3 { - uint32_t tagIndex; - uint32_t characteristics; + std::uint32_t tagIndex; + std::uint32_t characteristics; }; struct aux_symbol_f4 { - uint8_t filename[SYMTAB_RECORD_LEN]; - string strFilename; + std::uint8_t filename[SYMTAB_RECORD_LEN]; + std::string strFilename; }; struct aux_symbol_f5 { - uint32_t length; - uint16_t numberOfRelocations; - uint16_t numberOfLineNumbers; - uint32_t checkSum; - uint16_t number; - uint8_t selection; + std::uint32_t length; + std::uint16_t numberOfRelocations; + std::uint16_t numberOfLineNumbers; + std::uint32_t checkSum; + std::uint16_t number; + std::uint8_t selection; }; struct symbol { - string strName; + std::string strName; symbol_name name; - uint32_t value; - int16_t sectionNumber; - uint16_t type; - uint8_t storageClass; - uint8_t numberOfAuxSymbols; - list<aux_symbol_f1> aux_symbols_f1; - list<aux_symbol_f2> aux_symbols_f2; - list<aux_symbol_f3> aux_symbols_f3; - list<aux_symbol_f4> aux_symbols_f4; - list<aux_symbol_f5> aux_symbols_f5; + std::uint32_t value; + std::int16_t sectionNumber; + std::uint16_t type; + std::uint8_t storageClass; + std::uint8_t numberOfAuxSymbols; + std::vector<aux_symbol_f1> aux_symbols_f1; + std::vector<aux_symbol_f2> aux_symbols_f2; + std::vector<aux_symbol_f3> aux_symbols_f3; + std::vector<aux_symbol_f4> aux_symbols_f4; + std::vector<aux_symbol_f5> aux_symbols_f5; }; struct parsed_pe_internal { - list<section> secs; - list<resource> rsrcs; - list<importent> imports; - list<reloc> relocs; - list<exportent> exports; - list<symbol> symbols; + std::vector<section> secs; + std::vector<resource> rsrcs; + std::vector<importent> imports; + std::vector<reloc> relocs; + std::vector<exportent> exports; + std::vector<symbol> symbols; }; std::uint32_t err = 0; @@ -144,11 +142,11 @@ std::uint32_t GetPEErr() { return err; } -string GetPEErrString() { +std::string GetPEErrString() { return pe_err_str[err]; } -string GetPEErrLoc() { +std::string GetPEErrLoc() { return err_loc; } @@ -214,7 +212,7 @@ const char *GetSymbolTableStorageClassName(std::uint8_t id) { } static bool -readCString(const bounded_buffer &buffer, std::uint32_t off, string &result) { +readCString(const bounded_buffer &buffer, std::uint32_t off, std::string &result) { if (off < buffer.bufLen) { std::uint8_t *p = buffer.buf; std::uint32_t n = buffer.bufLen; @@ -231,10 +229,10 @@ readCString(const bounded_buffer &buffer, std::uint32_t off, string &result) { return false; } -bool getSecForVA(list<section> &secs, VA v, section &sec) { +bool getSecForVA(const std::vector<section> &secs, VA v, section &sec) { for (section s : secs) { - ::uint64_t low = s.sectionBase; - ::uint64_t high = low + s.sec.Misc.VirtualSize; + std::uint64_t low = s.sectionBase; + std::uint64_t high = low + s.sec.Misc.VirtualSize; if (v >= low && v < high) { sec = s; @@ -257,7 +255,7 @@ void IterRsrc(parsed_pe *pe, iterRsrc cb, void *cbd) { return; } -bool parse_resource_id(bounded_buffer *data, std::uint32_t id, string &result) { +bool parse_resource_id(bounded_buffer *data, std::uint32_t id, std::string &result) { std::uint8_t c; std::uint16_t len; @@ -279,7 +277,7 @@ bool parse_resource_table(bounded_buffer *sectionData, std::uint32_t virtaddr, std::uint32_t depth, resource_dir_entry *dirent, - list<resource> &rsrcs) { + std::vector<resource> &rsrcs) { resource_dir_table rdt; if (sectionData == nullptr) { @@ -492,8 +490,8 @@ bool parse_resource_table(bounded_buffer *sectionData, bool getResources(bounded_buffer *b, bounded_buffer *fileBegin, - list<section> secs, - list<resource> &rsrcs) { + const std::vector<section> secs, + std::vector<resource> &rsrcs) { static_cast<void>(fileBegin); if (b == nullptr) @@ -518,17 +516,17 @@ bool getResources(bounded_buffer *b, bool getSections(bounded_buffer *b, bounded_buffer *fileBegin, nt_header_32 &nthdr, - list<section> &secs) { + std::vector<section> &secs) { if (b == nullptr) { return false; } // get each of the sections... - for (::uint32_t i = 0; i < nthdr.FileHeader.NumberOfSections; i++) { + for (std::uint32_t i = 0; i < nthdr.FileHeader.NumberOfSections; i++) { image_section_header curSec; - ::uint32_t o = i * sizeof(image_section_header); - for (::uint32_t k = 0; k < NT_SHORT_NAME_LEN; k++) { + std::uint32_t o = i * sizeof(image_section_header); + for (std::uint32_t k = 0; k < NT_SHORT_NAME_LEN; k++) { if (!readByte(b, o + k, curSec.Name[k])) { return false; } @@ -547,8 +545,8 @@ bool getSections(bounded_buffer *b, // now we have the section header information, so fill in a section // object appropriately section thisSec; - for (::uint32_t charIndex = 0; charIndex < NT_SHORT_NAME_LEN; charIndex++) { - ::uint8_t c = curSec.Name[charIndex]; + for (std::uint32_t charIndex = 0; charIndex < NT_SHORT_NAME_LEN; charIndex++) { + std::uint8_t c = curSec.Name[charIndex]; if (c == 0) { break; } @@ -567,8 +565,8 @@ bool getSections(bounded_buffer *b, } thisSec.sec = curSec; - ::uint32_t lowOff = curSec.PointerToRawData; - ::uint32_t highOff = lowOff + curSec.SizeOfRawData; + std::uint32_t lowOff = curSec.PointerToRawData; + std::uint32_t highOff = lowOff + curSec.SizeOfRawData; thisSec.sectionData = splitBuffer(fileBegin, lowOff, highOff); secs.push_back(thisSec); @@ -614,10 +612,10 @@ bool readOptionalHeader(bounded_buffer *b, optional_header_32 &header) { header.NumberOfRvaAndSizes = NUM_DIR_ENTRIES; } - for (::uint32_t i = 0; i < header.NumberOfRvaAndSizes; i++) { - ::uint32_t c = (i * sizeof(data_directory)); + for (std::uint32_t i = 0; i < header.NumberOfRvaAndSizes; i++) { + std::uint32_t c = (i * sizeof(data_directory)); c += _offset(optional_header_32, DataDirectory[0]); - ::uint32_t o; + std::uint32_t o; o = c + _offset(data_directory, VirtualAddress); if (!readDword(b, o, header.DataDirectory[i].VirtualAddress)) { @@ -669,10 +667,10 @@ bool readOptionalHeader64(bounded_buffer *b, optional_header_64 &header) { header.NumberOfRvaAndSizes = NUM_DIR_ENTRIES; } - for (::uint32_t i = 0; i < header.NumberOfRvaAndSizes; i++) { - ::uint32_t c = (i * sizeof(data_directory)); + for (std::uint32_t i = 0; i < header.NumberOfRvaAndSizes; i++) { + std::uint32_t c = (i * sizeof(data_directory)); c += _offset(optional_header_64, DataDirectory[0]); - ::uint32_t o; + std::uint32_t o; o = c + _offset(data_directory, VirtualAddress); if (!readDword(b, o, header.DataDirectory[i].VirtualAddress)) { @@ -705,8 +703,8 @@ bool readNtHeader(bounded_buffer *b, nt_header_32 &header) { return false; } - ::uint32_t pe_magic; - ::uint32_t curOffset = 0; + std::uint32_t pe_magic; + std::uint32_t curOffset = 0; if (!readDword(b, curOffset, pe_magic) || pe_magic != NT_MAGIC) { PE_ERR(PEERR_READ); return false; @@ -803,8 +801,8 @@ bool getHeader(bounded_buffer *file, pe_header &p, bounded_buffer *&rem) { } // start by reading MZ - ::uint16_t tmp = 0; - ::uint32_t curOffset = 0; + std::uint16_t tmp = 0; + std::uint32_t curOffset = 0; if (!readWord(file, curOffset, tmp)) { PE_ERR(PEERR_READ); return false; @@ -815,7 +813,7 @@ bool getHeader(bounded_buffer *file, pe_header &p, bounded_buffer *&rem) { } // read the offset to the NT headers - ::uint32_t offset; + std::uint32_t offset; if (!readDword(file, _offset(dos_header, e_lfanew), offset)) { PE_ERR(PEERR_READ); return false; @@ -837,15 +835,15 @@ bool getHeader(bounded_buffer *file, pe_header &p, bounded_buffer *&rem) { * Need to determine if this is a PE32 or PE32+ binary and use the # correct size. */ - ::uint32_t rem_size; + std::uint32_t rem_size; if (p.nt.OptionalMagic == NT_OPTIONAL_32_MAGIC) { // signature + file_header + optional_header_32 rem_size = - sizeof(::uint32_t) + sizeof(file_header) + sizeof(optional_header_32); + sizeof(std::uint32_t) + sizeof(file_header) + sizeof(optional_header_32); } else if (p.nt.OptionalMagic == NT_OPTIONAL_64_MAGIC) { // signature + file_header + optional_header_64 rem_size = - sizeof(::uint32_t) + sizeof(file_header) + sizeof(optional_header_64); + sizeof(std::uint32_t) + sizeof(file_header) + sizeof(optional_header_64); } else { PE_ERR(PEERR_MAGIC); deleteBuffer(ntBuf); @@ -910,7 +908,7 @@ bool getExports(parsed_pe *p) { } auto nameOff = static_cast<std::uint32_t>(nameVA - nameSec.sectionBase); - string modName; + std::string modName; if (!readCString(*nameSec.sectionData, nameOff, modName)) { return false; } @@ -925,7 +923,7 @@ bool getExports(parsed_pe *p) { if (numNames > 0) { // get the names section - ::uint32_t namesRVA; + std::uint32_t namesRVA; if (!readDword(s.sectionData, rvaofft + _offset(export_dir_table, NamePointerRVA), namesRVA)) { @@ -950,7 +948,7 @@ bool getExports(parsed_pe *p) { static_cast<std::uint32_t>(namesVA - namesSec.sectionBase); // get the EAT section - ::uint32_t eatRVA; + std::uint32_t eatRVA; if (!readDword(s.sectionData, rvaofft + _offset(export_dir_table, ExportAddressTableRVA), eatRVA)) { @@ -974,7 +972,7 @@ bool getExports(parsed_pe *p) { auto eatOff = static_cast<std::uint32_t>(eatVA - eatSec.sectionBase); // get the ordinal base - ::uint32_t ordinalBase; + std::uint32_t ordinalBase; if (!readDword(s.sectionData, rvaofft + _offset(export_dir_table, OrdinalBase), ordinalBase)) { @@ -1008,10 +1006,10 @@ bool getExports(parsed_pe *p) { auto ordinalOff = static_cast<std::uint32_t>(ordinalTableVA - ordinalTableSec.sectionBase); - for (::uint32_t i = 0; i < numNames; i++) { - ::uint32_t curNameRVA; + for (std::uint32_t i = 0; i < numNames; i++) { + std::uint32_t curNameRVA; if (!readDword(namesSec.sectionData, - namesOff + (i * sizeof(::uint32_t)), + namesOff + (i * sizeof(std::uint32_t)), curNameRVA)) { return false; } @@ -1033,7 +1031,7 @@ bool getExports(parsed_pe *p) { auto curNameOff = static_cast<std::uint32_t>(curNameVA - curNameSec.sectionBase); - string symName; + std::string symName; std::uint8_t d; do { @@ -1050,17 +1048,17 @@ bool getExports(parsed_pe *p) { } while (true); // now, for this i, look it up in the ExportOrdinalTable - ::uint16_t ordinal; + std::uint16_t ordinal; if (!readWord(ordinalTableSec.sectionData, - ordinalOff + (i * sizeof(uint16_t)), + ordinalOff + (i * sizeof(std::uint16_t)), ordinal)) { return false; } //::uint32_t eatIdx = ordinal - ordinalBase; - ::uint32_t eatIdx = (ordinal * sizeof(uint32_t)); + std::uint32_t eatIdx = (ordinal * sizeof(std::uint32_t)); - ::uint32_t symRVA; + std::uint32_t symRVA; if (!readDword(eatSec.sectionData, eatOff + eatIdx, symRVA)) { return false; } @@ -1149,9 +1147,9 @@ bool getRelocations(parsed_pe *p) { // Iterate over all of the block Type/Offset entries while (entryCount != 0) { - ::uint16_t entry; - ::uint8_t type; - ::uint16_t offset; + std::uint16_t entry; + std::uint8_t type; + std::uint16_t offset; if (!readWord(d.sectionData, rvaofft, entry)) { return false; @@ -1181,7 +1179,7 @@ bool getRelocations(parsed_pe *p) { p->internal->relocs.push_back(r); entryCount--; - rvaofft += sizeof(::uint16_t); + rvaofft += sizeof(std::uint16_t); } } } @@ -1254,7 +1252,7 @@ bool getImports(parsed_pe *p) { } auto nameOff = static_cast<std::uint32_t>(name - nameSec.sectionBase); - string modName; + std::string modName; if (!readCString(*nameSec.sectionData, nameOff, modName)) { return false; } @@ -1303,13 +1301,13 @@ bool getImports(parsed_pe *p) { auto lookupOff = static_cast<std::uint32_t>(lookupVA - lookupSec.sectionBase); - ::uint32_t offInTable = 0; + std::uint32_t offInTable = 0; do { VA valVA = 0; - ::uint8_t ord = 0; - ::uint16_t oval = 0; - ::uint32_t val32 = 0; - ::uint64_t val64 = 0; + std::uint8_t ord = 0; + std::uint16_t oval = 0; + std::uint32_t val32 = 0; + std::uint64_t val64 = 0; if (p->peHeader.nt.OptionalMagic == NT_OPTIONAL_32_MAGIC) { if (!readDword(lookupSec.sectionData, lookupOff, val32)) { return false; @@ -1336,7 +1334,7 @@ bool getImports(parsed_pe *p) { if (ord == 0) { // import by name - string symName; + std::string symName; section symNameSec; if (!getSecForVA(p->internal->secs, valVA, symNameSec)) { @@ -1345,7 +1343,7 @@ bool getImports(parsed_pe *p) { std::uint32_t nameOffset = static_cast<std::uint32_t>(valVA - symNameSec.sectionBase) + - sizeof(::uint16_t); + sizeof(std::uint16_t); do { std::uint8_t chr; if (!readByte(symNameSec.sectionData, nameOffset, chr)) { @@ -1377,8 +1375,8 @@ bool getImports(parsed_pe *p) { ent.moduleName = modName; p->internal->imports.push_back(ent); } else { - string symName = - "ORDINAL_" + modName + "_" + to_string<uint32_t>(oval, dec); + std::string symName = + "ORDINAL_" + modName + "_" + to_string<std::uint32_t>(oval, std::dec); importent ent; @@ -1399,11 +1397,11 @@ bool getImports(parsed_pe *p) { } if (p->peHeader.nt.OptionalMagic == NT_OPTIONAL_32_MAGIC) { - lookupOff += sizeof(::uint32_t); - offInTable += sizeof(::uint32_t); + lookupOff += sizeof(std::uint32_t); + offInTable += sizeof(std::uint32_t); } else if (p->peHeader.nt.OptionalMagic == NT_OPTIONAL_64_MAGIC) { - lookupOff += sizeof(::uint64_t); - offInTable += sizeof(::uint64_t); + lookupOff += sizeof(std::uint64_t); + offInTable += sizeof(std::uint64_t); } else { return false; } @@ -1421,13 +1419,13 @@ bool getSymbolTable(parsed_pe *p) { return true; } - uint32_t strTableOffset = + std::uint32_t strTableOffset = p->peHeader.nt.FileHeader.PointerToSymbolTable + (p->peHeader.nt.FileHeader.NumberOfSymbols * SYMTAB_RECORD_LEN); - uint32_t offset = p->peHeader.nt.FileHeader.PointerToSymbolTable; + std::uint32_t offset = p->peHeader.nt.FileHeader.PointerToSymbolTable; - for (uint32_t i = 0; i < p->peHeader.nt.FileHeader.NumberOfSymbols; i++) { + for (std::uint32_t i = 0; i < p->peHeader.nt.FileHeader.NumberOfSymbols; i++) { symbol sym; // Read name @@ -1452,16 +1450,16 @@ bool getSymbolTable(parsed_pe *p) { break; } sym.strName.push_back(static_cast<char>(ch)); - strOffset += sizeof(uint8_t); + strOffset += sizeof(std::uint8_t); } } else { - for (uint8_t n = 0; n < NT_SHORT_NAME_LEN && sym.name.shortName[n] != 0; + for (std::uint8_t n = 0; n < NT_SHORT_NAME_LEN && sym.name.shortName[n] != 0; n++) { sym.strName.push_back(static_cast<char>(sym.name.shortName[n])); } } - offset += sizeof(uint64_t); + offset += sizeof(std::uint64_t); // Read value if (!readDword(p->fileBuffer, offset, sym.value)) { @@ -1469,7 +1467,7 @@ bool getSymbolTable(parsed_pe *p) { return false; } - offset += sizeof(uint32_t); + offset += sizeof(std::uint32_t); // Read section number uint16_t secNum; @@ -1477,9 +1475,9 @@ bool getSymbolTable(parsed_pe *p) { PE_ERR(PEERR_MAGIC); return false; } - sym.sectionNumber = static_cast<int16_t>(secNum); + sym.sectionNumber = static_cast<std::int16_t>(secNum); - offset += sizeof(uint16_t); + offset += sizeof(std::uint16_t); // Read type if (!readWord(p->fileBuffer, offset, sym.type)) { @@ -1487,7 +1485,7 @@ bool getSymbolTable(parsed_pe *p) { return false; } - offset += sizeof(uint16_t); + offset += sizeof(std::uint16_t); // Read storage class if (!readByte(p->fileBuffer, offset, sym.storageClass)) { @@ -1495,7 +1493,7 @@ bool getSymbolTable(parsed_pe *p) { return false; } - offset += sizeof(uint8_t); + offset += sizeof(std::uint8_t); // Read number of auxiliary symbols if (!readByte(p->fileBuffer, offset, sym.numberOfAuxSymbols)) { @@ -1504,7 +1502,7 @@ bool getSymbolTable(parsed_pe *p) { } // Set offset to next symbol - offset += sizeof(uint8_t); + offset += sizeof(std::uint8_t); // Save the symbol p->internal->symbols.push_back(sym); @@ -1524,7 +1522,7 @@ bool getSymbolTable(parsed_pe *p) { SYMBOL_TYPE_HI(sym) == 0x20 && sym.sectionNumber > 0) { // Auxiliary Format 1: Function Definitions - for (uint8_t n = 0; n < sym.numberOfAuxSymbols; n++) { + for (std::uint8_t n = 0; n < sym.numberOfAuxSymbols; n++) { aux_symbol_f1 asym; // Read tag index @@ -1533,7 +1531,7 @@ bool getSymbolTable(parsed_pe *p) { return false; } - offset += sizeof(uint32_t); + offset += sizeof(std::uint32_t); // Read total size if (!readDword(p->fileBuffer, offset, asym.totalSize)) { @@ -1541,7 +1539,7 @@ bool getSymbolTable(parsed_pe *p) { return false; } - offset += sizeof(uint32_t); + offset += sizeof(std::uint32_t); // Read pointer to line number if (!readDword(p->fileBuffer, offset, asym.pointerToLineNumber)) { @@ -1549,7 +1547,7 @@ bool getSymbolTable(parsed_pe *p) { return false; } - offset += sizeof(uint32_t); + offset += sizeof(std::uint32_t); // Read pointer to next function if (!readDword(p->fileBuffer, offset, asym.pointerToNextFunction)) { @@ -1558,7 +1556,7 @@ bool getSymbolTable(parsed_pe *p) { } // Skip the processed 4 bytes + unused 2 bytes - offset += sizeof(uint8_t) * 6; + offset += sizeof(std::uint8_t) * 6; // Save the record sym.aux_symbols_f1.push_back(asym); @@ -1567,10 +1565,10 @@ bool getSymbolTable(parsed_pe *p) { } else if (sym.storageClass == IMAGE_SYM_CLASS_FUNCTION) { // Auxiliary Format 2: .bf and .ef Symbols - for (uint8_t n = 0; n < sym.numberOfAuxSymbols; n++) { + for (std::uint8_t n = 0; n < sym.numberOfAuxSymbols; n++) { aux_symbol_f2 asym; // Skip unused 4 bytes - offset += sizeof(uint32_t); + offset += sizeof(std::uint32_t); // Read line number if (!readWord(p->fileBuffer, offset, asym.lineNumber)) { @@ -1578,10 +1576,10 @@ bool getSymbolTable(parsed_pe *p) { return false; } - offset += sizeof(uint16_t); + offset += sizeof(std::uint16_t); // Skip unused 6 bytes - offset += sizeof(uint8_t) * 6; + offset += sizeof(std::uint8_t) * 6; // Read pointer to next function if (!readDword(p->fileBuffer, offset, asym.pointerToNextFunction)) { @@ -1590,7 +1588,7 @@ bool getSymbolTable(parsed_pe *p) { } // Skip the processed 4 bytes + unused 2 bytes - offset += sizeof(uint8_t) * 6; + offset += sizeof(std::uint8_t) * 6; // Save the record sym.aux_symbols_f2.push_back(asym); @@ -1600,7 +1598,7 @@ bool getSymbolTable(parsed_pe *p) { sym.sectionNumber == IMAGE_SYM_UNDEFINED && sym.value == 0) { // Auxiliary Format 3: Weak Externals - for (uint8_t n = 0; n < sym.numberOfAuxSymbols; n++) { + for (std::uint8_t n = 0; n < sym.numberOfAuxSymbols; n++) { aux_symbol_f3 asym; // Read line number @@ -1616,7 +1614,7 @@ bool getSymbolTable(parsed_pe *p) { } // Skip unused 10 bytes - offset += sizeof(uint8_t) * 10; + offset += sizeof(std::uint8_t) * 10; // Save the record sym.aux_symbols_f3.push_back(asym); @@ -1625,13 +1623,13 @@ bool getSymbolTable(parsed_pe *p) { } else if (sym.storageClass == IMAGE_SYM_CLASS_FILE) { // Auxiliary Format 4: Files - for (uint8_t n = 0; n < sym.numberOfAuxSymbols; n++) { + for (std::uint8_t n = 0; n < sym.numberOfAuxSymbols; n++) { aux_symbol_f4 asym; // Read filename bool terminatorFound = false; - for (uint16_t j = 0; j < SYMTAB_RECORD_LEN; j++) { + for (std::uint16_t j = 0; j < SYMTAB_RECORD_LEN; j++) { // Save the raw field if (!readByte(p->fileBuffer, offset, asym.filename[j])) { PE_ERR(PEERR_MAGIC); @@ -1656,7 +1654,7 @@ bool getSymbolTable(parsed_pe *p) { } else if (sym.storageClass == IMAGE_SYM_CLASS_STATIC) { // Auxiliary Format 5: Section Definitions - for (uint8_t n = 0; n < sym.numberOfAuxSymbols; n++) { + for (std::uint8_t n = 0; n < sym.numberOfAuxSymbols; n++) { aux_symbol_f5 asym; // Read length @@ -1665,7 +1663,7 @@ bool getSymbolTable(parsed_pe *p) { return false; } - offset += sizeof(uint32_t); + offset += sizeof(std::uint32_t); // Read number of relocations if (!readWord(p->fileBuffer, offset, asym.numberOfRelocations)) { @@ -1673,7 +1671,7 @@ bool getSymbolTable(parsed_pe *p) { return false; } - offset += sizeof(uint16_t); + offset += sizeof(std::uint16_t); // Read number of line numbers if (!readWord(p->fileBuffer, offset, asym.numberOfLineNumbers)) { @@ -1681,7 +1679,7 @@ bool getSymbolTable(parsed_pe *p) { return false; } - offset += sizeof(uint16_t); + offset += sizeof(std::uint16_t); // Read checksum if (!readDword(p->fileBuffer, offset, asym.checkSum)) { @@ -1689,7 +1687,7 @@ bool getSymbolTable(parsed_pe *p) { return false; } - offset += sizeof(uint32_t); + offset += sizeof(std::uint32_t); // Read number if (!readWord(p->fileBuffer, offset, asym.number)) { @@ -1697,7 +1695,7 @@ bool getSymbolTable(parsed_pe *p) { return false; } - offset += sizeof(uint16_t); + offset += sizeof(std::uint16_t); // Read selection if (!readByte(p->fileBuffer, offset, asym.selection)) { @@ -1705,10 +1703,10 @@ bool getSymbolTable(parsed_pe *p) { return false; } - offset += sizeof(uint8_t); + offset += sizeof(std::uint8_t); // Skip unused 3 bytes - offset += sizeof(uint8_t) * 3; + offset += sizeof(std::uint8_t) * 3; // Save the record sym.aux_symbols_f5.push_back(asym); @@ -1866,7 +1864,7 @@ void DestructParsedPE(parsed_pe *p) { // iterate over the imports by VA and string void IterImpVAString(parsed_pe *pe, iterVAStr cb, void *cbd) { - list<importent> &l = pe->internal->imports; + std::vector<importent> &l = pe->internal->imports; for (importent i : l) { if (cb(cbd, i.addr, i.moduleName, i.symbolName) != 0) { @@ -1879,7 +1877,7 @@ void IterImpVAString(parsed_pe *pe, iterVAStr cb, void *cbd) { // iterate over relocations in the PE file void IterRelocs(parsed_pe *pe, iterReloc cb, void *cbd) { - list<reloc> &l = pe->internal->relocs; + std::vector<reloc> &l = pe->internal->relocs; for (reloc r : l) { if (cb(cbd, r.shiftedAddr, r.type) != 0) { @@ -1892,7 +1890,7 @@ void IterRelocs(parsed_pe *pe, iterReloc cb, void *cbd) { // Iterate over symbols (symbol table) in the PE file void IterSymbols(parsed_pe *pe, iterSymbol cb, void *cbd) { - list<symbol> &l = pe->internal->symbols; + std::vector<symbol> &l = pe->internal->symbols; for (symbol s : l) { if (cb(cbd, @@ -1911,7 +1909,7 @@ void IterSymbols(parsed_pe *pe, iterSymbol cb, void *cbd) { // iterate over the exports by VA void IterExpVA(parsed_pe *pe, iterExp cb, void *cbd) { - list<exportent> &l = pe->internal->exports; + std::vector<exportent> &l = pe->internal->exports; for (exportent i : l) { if (cb(cbd, i.addr, i.moduleName, i.symbolName) != 0) { @@ -1935,7 +1933,7 @@ void IterSec(parsed_pe *pe, iterSec cb, void *cbd) { return; } -bool ReadByteAtVA(parsed_pe *pe, VA v, ::uint8_t &b) { +bool ReadByteAtVA(parsed_pe *pe, VA v, std::uint8_t &b) { // find this VA in a section section s; diff --git a/python/pepy.cpp b/python/pepy.cpp index c022fd2..ce11d57 100644 --- a/python/pepy.cpp +++ b/python/pepy.cpp @@ -945,7 +945,10 @@ static PyObject *pepy_parsed_get_resources(PyObject *self, PyObject *args) { return ret; } -int import_callback(void *cbd, VA addr, std::string &name, std::string &sym) { +int import_callback(void *cbd, + VA addr, + const std::string &name, + const std::string &sym) { PyObject *imp; PyObject *tuple; PyObject *list = (PyObject *) cbd;