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