|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#define EXPOSE_DIRECT_RESOURCES |
| 4 | + |
| 5 | +#include "PEFileResources.h" |
| 6 | + |
| 7 | +#define WIN32_LEAN_AND_MEAN |
| 8 | +#include <Windows.h> |
| 9 | + |
| 10 | +#define LARGE_PATH 32767 |
| 11 | + |
| 12 | +class PEFile { |
| 13 | +protected: |
| 14 | + IMAGE_DOS_HEADER *dosh; |
| 15 | + LONG peOffset; |
| 16 | + IMAGE_NT_HEADERS32 *nth32; |
| 17 | + IMAGE_NT_HEADERS64 *nth64; |
| 18 | + IMAGE_FILE_HEADER *header; // part of nth32/nth64 header |
| 19 | + IMAGE_DATA_DIRECTORY *dataDir; // part of nth32/nth64 header |
| 20 | + IMAGE_SECTION_HEADER *sections; |
| 21 | + Rsrc *res; |
| 22 | + |
| 23 | + bool readonly; |
| 24 | + WCHAR original[LARGE_PATH]; |
| 25 | + HANDLE hFile, hMap; |
| 26 | + size_t size; |
| 27 | + LPBYTE orig_data, data; |
| 28 | + |
| 29 | + ULONGLONG version; |
| 30 | + bool modified; |
| 31 | + |
| 32 | + size_t getSizeOf(DWORD cnt, int rsrcIndx, size_t rsrcRawSize) const; |
| 33 | + |
| 34 | + bool map(); |
| 35 | + void unmap(); |
| 36 | + |
| 37 | + bool usesMemoryMappedFile() const; |
| 38 | + |
| 39 | + bool load(bool incRes); |
| 40 | + void unload(); |
| 41 | +public: |
| 42 | + PEFile(LPVOID data, size_t size, bool readonly = false); // data is freed when the PEFile is deleted |
| 43 | + PEFile(LPCWSTR filename, bool readonly = false); |
| 44 | + ~PEFile(); |
| 45 | + bool isLoaded() const; |
| 46 | + bool isReadOnly() const; |
| 47 | + |
| 48 | + bool save(); // flushes |
| 49 | + |
| 50 | + bool is32bit() const; |
| 51 | + bool is64bit() const; |
| 52 | + ULONGLONG getImageBase() const; |
| 53 | + |
| 54 | + IMAGE_FILE_HEADER *getFileHeader(); // pointer can modify the file |
| 55 | + IMAGE_NT_HEADERS32 *getNtHeaders32(); // pointer can modify the file |
| 56 | + IMAGE_NT_HEADERS64 *getNtHeaders64(); // pointer can modify the file |
| 57 | + const IMAGE_FILE_HEADER *getFileHeader() const; |
| 58 | + const IMAGE_NT_HEADERS32 *getNtHeaders32() const; |
| 59 | + const IMAGE_NT_HEADERS64 *getNtHeaders64() const; |
| 60 | + |
| 61 | + DWORD getDataDirectoryCount() const; |
| 62 | + IMAGE_DATA_DIRECTORY *getDataDirectory(int i); // pointer can modify the file |
| 63 | + const IMAGE_DATA_DIRECTORY *getDataDirectory(int i) const; |
| 64 | + |
| 65 | + IMAGE_SECTION_HEADER *getSectionHeader(int i); // pointer can modify the file |
| 66 | + IMAGE_SECTION_HEADER *getSectionHeader(const char *str, int *i = NULL); // pointer can modify the file |
| 67 | + IMAGE_SECTION_HEADER *getSectionHeaderByRVA(DWORD rva, int *i); // pointer can modify the file |
| 68 | + IMAGE_SECTION_HEADER *getSectionHeaderByVA(ULONGLONG va, int *i); // pointer can modify the file |
| 69 | + const IMAGE_SECTION_HEADER *getSectionHeader(int i) const; |
| 70 | + const IMAGE_SECTION_HEADER *getSectionHeader(const char *str, int *i = NULL) const; |
| 71 | + const IMAGE_SECTION_HEADER *getSectionHeaderByRVA(DWORD rva, int *i) const; |
| 72 | + const IMAGE_SECTION_HEADER *getSectionHeaderByVA(ULONGLONG va, int *i) const; |
| 73 | + int getSectionHeaderCount() const; |
| 74 | + |
| 75 | + IMAGE_SECTION_HEADER *getExpandedSectionHdr(int i, DWORD room); // pointer can modify the file, invalidates all pointers returned by functions, flushes |
| 76 | + IMAGE_SECTION_HEADER *getExpandedSectionHdr(char *str, DWORD room); // as above |
| 77 | + |
| 78 | +#define CHARS_CODE_SECTION IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
| 79 | +#define CHARS_INIT_DATA_SECTION_R IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
| 80 | +#define CHARS_INIT_DATA_SECTION_RW IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE |
| 81 | + |
| 82 | + 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 |
| 83 | + IMAGE_SECTION_HEADER *createSection(const char *str, const char *name, DWORD room, DWORD chars); // as above, adds before the section named str |
| 84 | + IMAGE_SECTION_HEADER *createSection(const char *name, DWORD room, DWORD chars); // as above, adds before ".reloc" if exists or at the very end |
| 85 | + |
| 86 | + size_t getSize() const; |
| 87 | + bool setSize(size_t dwSize, bool grow_only = true); // invalidates all pointers returned by functions, flushes |
| 88 | + |
| 89 | + LPBYTE get(DWORD dwOffset = 0, DWORD *dwSize = NULL); // pointer can modify the file |
| 90 | + const LPBYTE get(DWORD dwOffset = 0, DWORD *dwSize = NULL) const; |
| 91 | + bool set(const LPVOID lpBuffer, DWORD dwSize, DWORD dwOffset); // shorthand for memcpy(f->get(dwOffset), lpBuffer, dwSize) with bounds checking |
| 92 | + bool zero(DWORD dwSize, DWORD dwOffset); // shorthand for memset(f->get(dwOffset), 0, dwSize) with bounds checking |
| 93 | + bool move(DWORD dwOffset, DWORD dwSize, int dwDistanceToMove); // shorthand for x = f->get(dwOffset); memmove(x+dwDistanceToMove, x, dwSize) with bounds checking |
| 94 | + bool shift(DWORD dwOffset, int dwDistanceToMove); // shorthand for f->move(dwOffset, f->getSize() - dwOffset - dwDistanceToMove, dwDistanceToMove) |
| 95 | + bool flush(); |
| 96 | + |
| 97 | + bool updatePEChkSum(); // flushes |
| 98 | + bool hasExtraData() const; |
| 99 | + LPVOID getExtraData(DWORD *size); // pointer can modify the file, when first enabling it will flush |
| 100 | + bool clearCertificateTable(); // may invalidate all pointers returned by functions, flushes |
| 101 | + ULONGLONG getFileVersion() const; |
| 102 | + bool isAlreadyModified() const; |
| 103 | + bool setModifiedFlag(); // flushes |
| 104 | + bool removeRelocs(DWORD start, DWORD end, bool reverse = false); |
| 105 | + |
| 106 | +#ifdef EXPOSE_DIRECT_RESOURCES |
| 107 | + Rsrc *getResources(); |
| 108 | + const Rsrc *getResources() const; |
| 109 | +#endif |
| 110 | + bool resourceExists(LPCWSTR type, LPCWSTR name, WORD lang) const; |
| 111 | + bool resourceExists(LPCWSTR type, LPCWSTR name, WORD* lang) const; |
| 112 | + LPVOID getResource (LPCWSTR type, LPCWSTR name, WORD lang, size_t* size) const; // must be freed |
| 113 | + LPVOID getResource (LPCWSTR type, LPCWSTR name, WORD* lang, size_t* size) const; // must be freed |
| 114 | + bool removeResource(LPCWSTR type, LPCWSTR name, WORD lang); |
| 115 | + bool addResource (LPCWSTR type, LPCWSTR name, WORD lang, const LPVOID data, size_t size, DWORD overwrite = OVERWRITE_ALWAYS); |
| 116 | + |
| 117 | + 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? |
| 118 | + static bool UpdatePEChkSum(LPBYTE data, size_t dwSize, size_t peOffset, DWORD dwOldCheck); |
| 119 | + static bool GetVersionInfo(const LPVOID ver, LPCWSTR query, LPVOID *buffer, PUINT len); |
| 120 | + static VS_FIXEDFILEINFO *GetVersionInfo(const LPVOID ver); |
| 121 | + static void UnmapAllViewsOfFile(LPCWSTR file); |
| 122 | +}; |
0 commit comments