// ****************************************************************** // * // * .,-::::: .,:: .::::::::. .,:: .: // * ,;;;'````' `;;;, .,;; ;;;'';;' `;;;, .,;; // * [[[ '[[,,[[' [[[__[[\. '[[,,[[' // * $$$ Y$$$P $$""""Y$$ Y$$$P // * `88bo,__,o, oP"``"Yo, _88o,,od8P oP"``"Yo, // * "YUMMMMMP",m" "Mm,""YUMMMP" ,m" "Mm, // * // * Cxbx->Core->Exe.cpp // * // * This file is part of the Cxbx project. // * // * Cxbx and Cxbe are free software; you can redistribute them // * and/or modify them under the terms of the GNU General Public // * License as published by the Free Software Foundation; either // * version 2 of the license, or (at your option) any later version. // * // * This program is distributed in the hope that it will be useful, // * but WITHOUT ANY WARRANTY; without even the implied warranty of // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // * GNU General Public License for more details. // * // * You should have recieved a copy of the GNU General Public License // * along with this program; see the file COPYING. // * If not, write to the Free Software Foundation, Inc., // * 59 Temple Place - Suite 330, Bostom, MA 02111-1307, USA. // * // * (c) 2002-2003 Aaron Robinson // * // * All rights reserved // * // ****************************************************************** #include "Cxbx.h" #include #include // ****************************************************************** // * constructor // ****************************************************************** Exe::Exe(const char *x_szFilename) { ConstructorInit(); FILE *ExeFile = fopen(x_szFilename, "rb"); // ****************************************************************** // * verify exe file was opened // ****************************************************************** if(ExeFile == 0) { SetError("could not open .exe file.", true); return; } // ****************************************************************** // * ignore dos stub (if it exists) // ****************************************************************** { if(fread(&m_DOSHeader.m_magic, sizeof(m_DOSHeader.m_magic), 1, ExeFile) != 1) { SetError("unexpected read error while reading magic number", true); goto cleanup; } if(m_DOSHeader.m_magic == *(uint16*)"MZ") { if(fread(&m_DOSHeader.m_cblp, sizeof(m_DOSHeader)-2, 1, ExeFile) != 1) { SetError("unexpected read error while reading dos header", true); goto cleanup; } fseek(ExeFile, m_DOSHeader.m_lfanew, SEEK_SET); } } // ****************************************************************** // * read pe header // ****************************************************************** { if(fread(&m_Header, sizeof(m_Header), 1, ExeFile) != 1) { SetError("unexpected read error while reading pe header", true); goto cleanup; } if(m_Header.m_magic != *(uint32*)"PE\0\0") { SetError("invalid file, could not locate PE header", true); goto cleanup; } } // ****************************************************************** // * read optional header // ****************************************************************** { if(fread(&m_OptionalHeader, sizeof(m_OptionalHeader), 1, ExeFile) != 1) { SetError("unexpected read error while reading optional header", true); goto cleanup; } if(m_OptionalHeader.m_magic != 0x010B) { SetError("invalid file, could not locate optional header", true); goto cleanup; } } // ****************************************************************** // * read section headers // ****************************************************************** { m_SectionHeader = new SectionHeader[m_Header.m_sections]; for(uint32 v=0;v 0) // ****************************************************************** { fseek(ExeFile, raw_addr, SEEK_SET); if(fread(m_bzSection[v], raw_size, 1, ExeFile) != 1) { char buffer[255]; sprintf(buffer, "could not read pe section %d (%Xh)", v, v); SetError(buffer, true); goto cleanup; } } } } cleanup: fclose(ExeFile); } // ****************************************************************** // * ConstructorInit // ****************************************************************** void Exe::ConstructorInit() { m_SectionHeader = 0; m_bzSection = 0; } // ****************************************************************** // * deconstructor // ****************************************************************** Exe::~Exe() { if(m_bzSection != 0) { for(uint32 v=0;v= virt_addr) && (x_dwVirtualAddress < (virt_addr + virt_size)) ) return &m_bzSection[v][x_dwVirtualAddress - virt_addr]; } return 0; }