/*
 * ************************************************************************************************************
 * Author: Marcin Załęczny
 * Copyright (C)2011 Marcin Załęczny <mzaleczny@hotmail.com>
 * License: The GNU Lesser General Public License, version 3.0 (LGPLv3)
 * Library Name: mzstddebug
 * Version: 1.02
 * First release date:   2011-02-03
 * Current release date: 2011-03-04
 *
 *
 * This file is part of mzstddebug library
 * 
 * mzstddebug library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * mzstddebug library 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************************************************
 */

#ifndef __MZSTDDEBUG_PE_FILE_H__
#define __MZSTDDEBUG_PE_FILE_H__

#include "mzstddebugCommon.h"
#include "mzObject.h"
#include "mzString.h"
#include "mzArrayList.h"
#include "mzStringList.h"
#include "mzPEDosExeData.h"


class DECLSPEC_MZSTDDEBUG CPEFile : public CObject {
public:
    PIMAGE_DOS_HEADER imageDosHeader;
    PDOSEXE_DATA imageDosExeData;
    PIMAGE_NT_HEADERS imageNtHeaders;
    PIMAGE_SECTION_HEADER imageSectionHeaders;

    WINAPI CPEFile();
    WINAPI ~CPEFile();
    VOID WINAPI cleanup();
    //VOID WINAPI cleanupSectionHeadersArray();

    BOOL WINAPI loadFileInfo(CString filename);
    VOID WINAPI stdoutImageHeadersDetails(CString indentation, BOOL dumpAsBytes = FALSE, BOOL dumpAsWords = FALSE, BOOL dumpAsDwords = FALSE);
    VOID WINAPI stdoutImageDosHeader(CString indentation, BOOL dumpAsBytes = FALSE, BOOL dumpAsWords = FALSE, BOOL dumpAsDwords = FALSE);
    VOID WINAPI stdoutImageDosExeData(CString indentation, BOOL dumpAsBytes = FALSE, BOOL dumpAsWords = FALSE, BOOL dumpAsDwords = FALSE);
    VOID WINAPI stdoutImageNtHeaders(CString indentation, BOOL dumpAsBytes = FALSE, BOOL dumpAsWords = FALSE, BOOL dumpAsDwords = FALSE);
    VOID WINAPI stdoutImageSectionHeaders(CString indentation, BOOL dumpAsBytes = FALSE, BOOL dumpAsWords = FALSE, BOOL dumpAsDwords = FALSE);
    
    inline DWORD WINAPI dosHeaderFieldOffset(BYTE* field) { return field - (BYTE*)this->imageDosHeader; }
    inline DWORD WINAPI ntHeaderFieldOffset(BYTE* field) { return field - (BYTE*)this->imageNtHeaders; }
    inline DWORD WINAPI sectionHeaderFieldOffset(BYTE* field) { return field - (BYTE*)this->imageSectionHeaders; }
    inline BOOL WINAPI isValid() { return this->_isValid; }
    
private:
    DWORD _readDosHeaderSize;
    DWORD _dosHeaderOffset;
    
    DWORD _readDosExeDataSize;
    DWORD _dosExeDataOffset;
    
    DWORD _readNtHeadersSize;
    DWORD _ntHeadersOffset;
    
    DWORD _readSectionHeadersSize;
    DWORD _sectionHeadersOffset;
    
    // if file is valid PE file. The field is set in loadFileInfo method.
    BOOL _isValid;
    
    VOID WINAPI _commonInit();
};
typedef CPEFile* PBinaryFile;

#endif
