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