[4440] | 1 | /* $Id: pefile.cpp,v 1.10 2000-10-06 11:04:01 sandervl Exp $ */
|
---|
[100] | 2 |
|
---|
[4] | 3 | /*
|
---|
| 4 | * PE2LX PE utility functions
|
---|
| 5 | *
|
---|
| 6 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
| 7 | *
|
---|
| 8 | */
|
---|
| 9 | #define INCL_BASE
|
---|
[120] | 10 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
[4] | 11 | #include <stdio.h>
|
---|
| 12 | #include <string.h>
|
---|
| 13 | #include <stdlib.h>
|
---|
| 14 | #include <win32type.h>
|
---|
| 15 | #include <pefile.h>
|
---|
| 16 | #include <misc.h>
|
---|
| 17 |
|
---|
[2802] | 18 | #define DBG_LOCALLOG DBG_pefile
|
---|
| 19 | #include "dbglocal.h"
|
---|
| 20 |
|
---|
[4] | 21 | //******************************************************************************
|
---|
| 22 | //******************************************************************************
|
---|
| 23 | char *UnicodeToFixedAsciiString(int length, WCHAR *NameString)
|
---|
| 24 | {
|
---|
| 25 | static char asciistring[256];
|
---|
| 26 | int i;
|
---|
| 27 |
|
---|
| 28 | if(length >= 255) length = 255;
|
---|
| 29 | for(i=0;i<length;i++) {
|
---|
| 30 | asciistring[i] = NameString[i] & 0xFF;
|
---|
| 31 | }
|
---|
| 32 | asciistring[length] = 0;
|
---|
| 33 | return(asciistring);
|
---|
| 34 | }
|
---|
| 35 | //******************************************************************************
|
---|
| 36 | //******************************************************************************
|
---|
| 37 | BOOL GetPEFileHeader (LPVOID lpFile, PIMAGE_FILE_HEADER pHeader)
|
---|
| 38 | {
|
---|
| 39 | if(*(USHORT *)lpFile == IMAGE_DOS_SIGNATURE &&
|
---|
| 40 | *(DWORD *)PE_HEADER (lpFile) == IMAGE_NT_SIGNATURE)
|
---|
| 41 | {
|
---|
| 42 | memcpy ((LPVOID)pHeader, PEHEADEROFF (lpFile), sizeof (IMAGE_FILE_HEADER));
|
---|
| 43 | return TRUE;
|
---|
| 44 | }
|
---|
| 45 | else return FALSE;
|
---|
| 46 | }
|
---|
| 47 | //******************************************************************************
|
---|
| 48 | //******************************************************************************
|
---|
| 49 | BOOL GetPEOptionalHeader (LPVOID lpFile, PIMAGE_OPTIONAL_HEADER pHeader)
|
---|
| 50 | {
|
---|
| 51 | if(*(USHORT *)lpFile == IMAGE_DOS_SIGNATURE &&
|
---|
| 52 | *(DWORD *)PE_HEADER (lpFile) == IMAGE_NT_SIGNATURE)
|
---|
| 53 | {
|
---|
| 54 | memcpy ((LPVOID)pHeader, OPTHEADEROFF (lpFile), sizeof (IMAGE_OPTIONAL_HEADER));
|
---|
| 55 | return TRUE;
|
---|
| 56 | }
|
---|
| 57 | else return FALSE;
|
---|
| 58 | }
|
---|
| 59 | //******************************************************************************
|
---|
| 60 | //******************************************************************************
|
---|
[3759] | 61 | LPVOID ImageDirectoryOffset(LPVOID lpFile, DWORD dwIMAGE_DIRECTORY)
|
---|
[4] | 62 | {
|
---|
| 63 | PIMAGE_OPTIONAL_HEADER poh = (PIMAGE_OPTIONAL_HEADER)OPTHEADEROFF (lpFile);
|
---|
| 64 | IMAGE_SECTION_HEADER sh;
|
---|
| 65 |
|
---|
| 66 | if (dwIMAGE_DIRECTORY >= poh->NumberOfRvaAndSizes)
|
---|
| 67 | return NULL;
|
---|
| 68 |
|
---|
| 69 | if(GetSectionHdrByRVA(lpFile, &sh, poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress) == FALSE)
|
---|
| 70 | {
|
---|
| 71 | return NULL;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[1811] | 74 | return (LPVOID)((ULONG)lpFile + poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress);
|
---|
[4] | 75 | }
|
---|
| 76 | //******************************************************************************
|
---|
| 77 | //******************************************************************************
|
---|
[3759] | 78 | DWORD ImageDirectorySize(LPVOID lpFile, DWORD dwIMAGE_DIRECTORY)
|
---|
| 79 | {
|
---|
| 80 | PIMAGE_OPTIONAL_HEADER poh = (PIMAGE_OPTIONAL_HEADER)OPTHEADEROFF (lpFile);
|
---|
| 81 | IMAGE_SECTION_HEADER sh;
|
---|
| 82 |
|
---|
| 83 | if (dwIMAGE_DIRECTORY >= poh->NumberOfRvaAndSizes)
|
---|
| 84 | return 0;
|
---|
| 85 |
|
---|
| 86 | if(GetSectionHdrByRVA(lpFile, &sh, poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress) == FALSE)
|
---|
| 87 | {
|
---|
| 88 | return 0;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | return poh->DataDirectory[dwIMAGE_DIRECTORY].Size;
|
---|
| 92 | }
|
---|
| 93 | //******************************************************************************
|
---|
| 94 | //******************************************************************************
|
---|
[4] | 95 | BOOL GetSectionHdrByImageDir(LPVOID lpFile, DWORD dwIMAGE_DIRECTORY, PIMAGE_SECTION_HEADER pSect)
|
---|
| 96 | {
|
---|
| 97 | PIMAGE_OPTIONAL_HEADER poh = (PIMAGE_OPTIONAL_HEADER)OPTHEADEROFF (lpFile);
|
---|
| 98 |
|
---|
| 99 | if (dwIMAGE_DIRECTORY >= poh->NumberOfRvaAndSizes)
|
---|
| 100 | return FALSE;
|
---|
| 101 |
|
---|
| 102 | return GetSectionHdrByRVA(lpFile, pSect, poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress);
|
---|
| 103 | }
|
---|
| 104 | //******************************************************************************
|
---|
| 105 | //******************************************************************************
|
---|
[4440] | 106 | BOOL IsSectionType(LPVOID lpFile, PIMAGE_SECTION_HEADER psh, DWORD dwIMAGE_DIRECTORY)
|
---|
[4] | 107 | {
|
---|
| 108 | PIMAGE_OPTIONAL_HEADER poh = (PIMAGE_OPTIONAL_HEADER)OPTHEADEROFF (lpFile);
|
---|
| 109 | int i = 0;
|
---|
| 110 | DWORD ImageDirVA;
|
---|
| 111 |
|
---|
[4440] | 112 | ImageDirVA = poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress;
|
---|
[4] | 113 |
|
---|
| 114 | if(psh->VirtualAddress <= ImageDirVA &&
|
---|
[4440] | 115 | psh->VirtualAddress + max(psh->Misc.VirtualSize,psh->SizeOfRawData) > ImageDirVA)
|
---|
[4] | 116 | {
|
---|
| 117 | return TRUE;
|
---|
| 118 | }
|
---|
| 119 | return FALSE;
|
---|
| 120 | }
|
---|
| 121 | //******************************************************************************
|
---|
| 122 | //******************************************************************************
|
---|
| 123 | BOOL GetSectionHdrByName (LPVOID lpFile, IMAGE_SECTION_HEADER *sh, char *szSection)
|
---|
| 124 | {
|
---|
| 125 | PIMAGE_SECTION_HEADER psh;
|
---|
| 126 | int nSections = NR_SECTIONS (lpFile);
|
---|
| 127 | int i;
|
---|
| 128 |
|
---|
| 129 | if((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (lpFile)) != NULL)
|
---|
| 130 | {
|
---|
| 131 | for(i=0; i<nSections; i++)
|
---|
| 132 | {
|
---|
| 133 | if(strcmp (psh->Name, szSection) == 0)
|
---|
| 134 | {
|
---|
| 135 | memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
|
---|
| 136 | return TRUE;
|
---|
| 137 | }
|
---|
| 138 | else psh++;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | return FALSE;
|
---|
| 142 | }
|
---|
| 143 | //******************************************************************************
|
---|
| 144 | //******************************************************************************
|
---|
| 145 | BOOL GetSectionHdrByType (LPVOID lpFile, IMAGE_SECTION_HEADER *sh, int type)
|
---|
| 146 | {
|
---|
| 147 | PIMAGE_SECTION_HEADER psh;
|
---|
| 148 | int nSections = NR_SECTIONS (lpFile);
|
---|
| 149 | int i;
|
---|
| 150 |
|
---|
| 151 | if((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (lpFile)) != NULL)
|
---|
| 152 | {
|
---|
| 153 | for(i=0; i<nSections; i++)
|
---|
| 154 | {
|
---|
| 155 | if(psh->Characteristics & type)
|
---|
| 156 | {
|
---|
| 157 | memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
|
---|
| 158 | return TRUE;
|
---|
| 159 | }
|
---|
| 160 | else psh++;
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | return FALSE;
|
---|
| 164 | }
|
---|
| 165 | /** Get Section Header for the given RVA - returns boolean according to the result
|
---|
| 166 | *
|
---|
| 167 | * knut [Jul 22 1998 2:53am]
|
---|
| 168 | */
|
---|
| 169 | BOOL GetSectionHdrByRVA (LPVOID lpFile, IMAGE_SECTION_HEADER *sh, ULONG rva)
|
---|
| 170 | {
|
---|
| 171 | PIMAGE_SECTION_HEADER psh;
|
---|
| 172 | int nSections = NR_SECTIONS (lpFile);
|
---|
| 173 | int i;
|
---|
| 174 |
|
---|
| 175 | if ((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (lpFile)) != NULL)
|
---|
| 176 | {
|
---|
| 177 | for (i=0; i<nSections; i++)
|
---|
| 178 | {
|
---|
| 179 | if (rva >= psh->VirtualAddress && rva < psh->VirtualAddress + max(psh->Misc.VirtualSize,psh->SizeOfRawData))
|
---|
| 180 | {
|
---|
| 181 | memcpy (sh, psh, sizeof(IMAGE_SECTION_HEADER));
|
---|
| 182 | return TRUE;
|
---|
| 183 | }
|
---|
| 184 | else psh++;
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 | return FALSE;
|
---|
| 188 | }
|
---|
| 189 | //******************************************************************************
|
---|
| 190 | //******************************************************************************
|
---|