|
1 | 1 | pe-file
|
2 | 2 | =======
|
3 | 3 |
|
4 |
| -Basic utility code for reading and modifying PE files (EXE, DLL, ...) |
| 4 | +Basic utility code for reading and modifying PE files (EXE, DLL, ...) |
| 5 | + |
| 6 | +This is simply code and not a program. It could easily be compiled into a DLL and expose the PEFile class. |
| 7 | + |
| 8 | +When compiling commenting out EXPOSE_DIRECT_RESOURCES causes direct resource access to be completely blocked and not expose the underlying resource classes. However resources are still accessible though other functions. |
| 9 | + |
| 10 | +The interface is as follows: |
| 11 | + |
| 12 | + class PEFile { |
| 13 | + PEFile(LPVOID data, size_t size, bool readonly = false); // data is freed when the PEFile is deleted |
| 14 | + PEFile(LPCWSTR filename, bool readonly = false); |
| 15 | + ~PEFile(); |
| 16 | + bool isLoaded() const; |
| 17 | + bool isReadOnly() const; |
| 18 | + |
| 19 | + bool save(); // flushes |
| 20 | + |
| 21 | + bool is32bit() const; |
| 22 | + bool is64bit() const; |
| 23 | + ULONGLONG getImageBase() const; |
| 24 | + |
| 25 | + IMAGE_FILE_HEADER *getFileHeader(); // pointer can modify the file |
| 26 | + IMAGE_NT_HEADERS32 *getNtHeaders32(); // pointer can modify the file |
| 27 | + IMAGE_NT_HEADERS64 *getNtHeaders64(); // pointer can modify the file |
| 28 | + const IMAGE_FILE_HEADER *getFileHeader() const; |
| 29 | + const IMAGE_NT_HEADERS32 *getNtHeaders32() const; |
| 30 | + const IMAGE_NT_HEADERS64 *getNtHeaders64() const; |
| 31 | + |
| 32 | + DWORD getDataDirectoryCount() const; |
| 33 | + IMAGE_DATA_DIRECTORY *getDataDirectory(int i); // pointer can modify the file |
| 34 | + const IMAGE_DATA_DIRECTORY *getDataDirectory(int i) const; |
| 35 | + |
| 36 | + IMAGE_SECTION_HEADER *getSectionHeader(int i); // pointer can modify the file |
| 37 | + IMAGE_SECTION_HEADER *getSectionHeader(const char *str, int *i = NULL); // pointer can modify the file |
| 38 | + IMAGE_SECTION_HEADER *getSectionHeaderByRVA(DWORD rva, int *i); // pointer can modify the file |
| 39 | + IMAGE_SECTION_HEADER *getSectionHeaderByVA(ULONGLONG va, int *i); // pointer can modify the file |
| 40 | + const IMAGE_SECTION_HEADER *getSectionHeader(int i) const; |
| 41 | + const IMAGE_SECTION_HEADER *getSectionHeader(const char *str, int *i = NULL) const; |
| 42 | + const IMAGE_SECTION_HEADER *getSectionHeaderByRVA(DWORD rva, int *i) const; |
| 43 | + const IMAGE_SECTION_HEADER *getSectionHeaderByVA(ULONGLONG va, int *i) const; |
| 44 | + int getSectionHeaderCount() const; |
| 45 | + |
| 46 | + IMAGE_SECTION_HEADER *getExpandedSectionHdr(int i, DWORD room); // pointer can modify the file, invalidates all pointers returned by functions, flushes |
| 47 | + IMAGE_SECTION_HEADER *getExpandedSectionHdr(char *str, DWORD room); // as above |
| 48 | + |
| 49 | + #define CHARS_CODE_SECTION IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
| 50 | + #define CHARS_INIT_DATA_SECTION_R IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
| 51 | + #define CHARS_INIT_DATA_SECTION_RW IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE |
| 52 | + |
| 53 | + IMAGE_SECTION_HEADER *createSection(int i, const char *name, DWORD room, DWORD chars); // pointer can modify the file, invalidates all pointers returned by functions, flushes |
| 54 | + IMAGE_SECTION_HEADER *createSection(const char *str, const char *name, DWORD room, DWORD chars); // as above, adds before the section named str |
| 55 | + IMAGE_SECTION_HEADER *createSection(const char *name, DWORD room, DWORD chars); // as above, adds before ".reloc" if exists or at the very end |
| 56 | + |
| 57 | + size_t getSize() const; |
| 58 | + bool setSize(size_t dwSize, bool grow_only = true); // invalidates all pointers returned by functions, flushes |
| 59 | + |
| 60 | + LPBYTE get(DWORD dwOffset = 0, DWORD *dwSize = NULL); // pointer can modify the file |
| 61 | + const LPBYTE get(DWORD dwOffset = 0, DWORD *dwSize = NULL) const; |
| 62 | + bool set(const LPVOID lpBuffer, DWORD dwSize, DWORD dwOffset); // shorthand for memcpy(f->get(dwOffset), lpBuffer, dwSize) with bounds checking |
| 63 | + bool zero(DWORD dwSize, DWORD dwOffset); // shorthand for memset(f->get(dwOffset), 0, dwSize) with bounds checking |
| 64 | + bool move(DWORD dwOffset, DWORD dwSize, int dwDistanceToMove); // shorthand for x = f->get(dwOffset); memmove(x+dwDistanceToMove, x, dwSize) with bounds checking |
| 65 | + bool shift(DWORD dwOffset, int dwDistanceToMove); // shorthand for f->move(dwOffset, f->getSize() - dwOffset - dwDistanceToMove, dwDistanceToMove) |
| 66 | + bool flush(); |
| 67 | + |
| 68 | + bool updatePEChkSum(); // flushes |
| 69 | + bool hasExtraData() const; |
| 70 | + LPVOID getExtraData(DWORD *size); // pointer can modify the file, when first enabling it will flush |
| 71 | + bool clearCertificateTable(); // may invalidate all pointers returned by functions, flushes |
| 72 | + ULONGLONG getFileVersion() const; |
| 73 | + bool isAlreadyModified() const; |
| 74 | + bool setModifiedFlag(); // flushes |
| 75 | + bool removeRelocs(DWORD start, DWORD end, bool reverse = false); |
| 76 | + |
| 77 | + #ifdef EXPOSE_DIRECT_RESOURCES |
| 78 | + Rsrc *getResources(); |
| 79 | + const Rsrc *getResources() const; |
| 80 | + #endif |
| 81 | + bool resourceExists(LPCWSTR type, LPCWSTR name, WORD lang) const; |
| 82 | + bool resourceExists(LPCWSTR type, LPCWSTR name, WORD* lang) const; |
| 83 | + LPVOID getResource (LPCWSTR type, LPCWSTR name, WORD lang, size_t* size) const; // must be freed |
| 84 | + LPVOID getResource (LPCWSTR type, LPCWSTR name, WORD* lang, size_t* size) const; // must be freed |
| 85 | + bool removeResource(LPCWSTR type, LPCWSTR name, WORD lang); |
| 86 | + bool addResource (LPCWSTR type, LPCWSTR name, WORD lang, const LPVOID data, size_t size, DWORD overwrite = OVERWRITE_ALWAYS); |
| 87 | + |
| 88 | + static const LPVOID GetResourceDirect(const LPVOID data, LPCWSTR type, LPCWSTR name); // must be freed, massively performance enhanced for a single retrieval and no editing // lang? size? |
| 89 | + static bool UpdatePEChkSum(LPBYTE data, size_t dwSize, size_t peOffset, DWORD dwOldCheck); |
| 90 | + static bool GetVersionInfo(const LPVOID ver, LPCWSTR query, LPVOID *buffer, PUINT len); |
| 91 | + static VS_FIXEDFILEINFO *GetVersionInfo(const LPVOID ver); |
| 92 | + static void UnmapAllViewsOfFile(LPCWSTR file); |
| 93 | + }; |
0 commit comments