FS#8961 - Anti-Aliased Fonts.
[kugel-rb/myfork.git] / utils / zenutils / source / shared / pe.cpp
blob10070074dd34609e08ab4c072921dc1f27353936
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
19 #include "pe.h"
22 shared::pe_file::pe_file(PeLib::PeFile* pef) : _pef(pef)
25 shared::pe_file::~pe_file()
27 if (_pef != NULL)
28 delete _pef;
31 bool shared::pe_file::is_valid() const
33 if (_pef->getBits() == 32)
35 PeLib::PeHeader32& pef32 = static_cast<PeLib::PeFile32*>(_pef)->peHeader();
36 if (!pef32.isValid())
37 return false;
38 return true;
40 else if (_pef->getBits() == 64)
42 PeLib::PeHeader64& pef64 = static_cast<PeLib::PeFile64*>(_pef)->peHeader();
43 if (!pef64.isValid())
44 return false;
45 return true;
47 return false;
50 bool shared::pe_file::read(const std::string& filename)
52 if (_pef != NULL)
54 delete _pef;
55 _pef = NULL;
58 _pef = PeLib::openPeFile(filename);
59 if (!_pef)
61 return false;
63 if (_pef->readMzHeader())
65 delete _pef;
66 return false;
68 if (!_pef->mzHeader().isValid())
70 delete _pef;
71 return false;
73 if (_pef->readPeHeader())
75 delete _pef;
76 return false;
78 if (!is_valid())
80 delete _pef;
81 return false;
83 return true;
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),
90 name, info);
91 else if (_pef->getBits() == 64)
92 return find_section(static_cast<PeLib::PeFile64*>(_pef),
93 name, info);
94 return false;
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),
103 name, buffer, info);
105 else if (_pef->getBits() == 64)
107 return add_section(static_cast<PeLib::PeFile64*>(_pef),
108 name, buffer, info);
110 return false;
113 dword shared::pe_file::get_image_base() const
115 if (_pef->getBits() == 32)
116 return static_cast<PeLib::PeFile32*>(_pef)->peHeader().getImageBase();
117 else
118 return static_cast<PeLib::PeFile64*>(_pef)->peHeader().getImageBase();
119 return 0;
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);
125 else
126 return static_cast<PeLib::PeFile64*>(_pef)->peHeader().offsetToVa(pa);
127 return 0;