1 /* zenutils - Utilities for working with creative firmwares.
2 * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 shared::pe_file::pe_file(PeLib::PeFile
* pef
) : _pef(pef
)
25 shared::pe_file::~pe_file()
31 bool shared::pe_file::is_valid() const
33 if (_pef
->getBits() == 32)
35 PeLib::PeHeader32
& pef32
= static_cast<PeLib::PeFile32
*>(_pef
)->peHeader();
40 else if (_pef
->getBits() == 64)
42 PeLib::PeHeader64
& pef64
= static_cast<PeLib::PeFile64
*>(_pef
)->peHeader();
50 bool shared::pe_file::read(const std::string
& filename
)
58 _pef
= PeLib::openPeFile(filename
);
63 if (_pef
->readMzHeader())
68 if (!_pef
->mzHeader().isValid())
73 if (_pef
->readPeHeader())
86 bool shared::pe_file::find_section(const std::string
& name
, section_info
& info
) const
88 if (_pef
->getBits() == 32)
89 return find_section(static_cast<PeLib::PeFile32
*>(_pef
),
91 else if (_pef
->getBits() == 64)
92 return find_section(static_cast<PeLib::PeFile64
*>(_pef
),
97 bool shared::pe_file::add_section(const std::string
& name
,
98 const bytes
& buffer
, section_info
& info
)
100 if (_pef
->getBits() == 32)
102 return add_section(static_cast<PeLib::PeFile32
*>(_pef
),
105 else if (_pef
->getBits() == 64)
107 return add_section(static_cast<PeLib::PeFile64
*>(_pef
),
113 dword
shared::pe_file::get_image_base() const
115 if (_pef
->getBits() == 32)
116 return static_cast<PeLib::PeFile32
*>(_pef
)->peHeader().getImageBase();
118 return static_cast<PeLib::PeFile64
*>(_pef
)->peHeader().getImageBase();
121 dword
shared::pe_file::pa_to_va(dword pa
) const
123 if (_pef
->getBits() == 32)
124 return static_cast<PeLib::PeFile32
*>(_pef
)->peHeader().offsetToVa(pa
);
126 return static_cast<PeLib::PeFile64
*>(_pef
)->peHeader().offsetToVa(pa
);