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 #ifndef SHARED_PE_H_INCLUDED
20 #define SHARED_PE_H_INCLUDED
23 #include <pelib/PeLib.h>
30 dword virtual_address
;
34 dword characteristics
;
35 }; //struct section_info
40 pe_file(PeLib::PeFile
* pef
= NULL
);
43 bool is_valid() const;
44 bool read(const std::string
& filename
);
45 bool find_section(const std::string
& name
, section_info
& info
) const;
46 bool add_section(const std::string
& name
, const bytes
& buffer
, section_info
& info
);
47 dword
get_image_base() const;
48 dword
pa_to_va(PeLib::dword pa
) const;
52 static bool find_section(const PeLib::PeFileT
<_Bits
>* pef
,
53 const std::string
& name
, section_info
& info
);
55 static bool add_section(PeLib::PeFileT
<_Bits
>* pef
,
56 const std::string
& name
, const bytes
& buffer
,
65 bool pe_file::find_section(const PeLib::PeFileT
<_Bits
>* pef
,
66 const std::string
& name
, section_info
& info
)
68 for (PeLib::word i
= 0; i
< pef
->peHeader().getNumberOfSections(); i
++)
70 if (pef
->peHeader().getSectionName(i
) == name
)
73 info
.virtual_address
= pef
->peHeader().getVirtualAddress(i
);
74 info
.virtual_size
= pef
->peHeader().getVirtualSize(i
);
75 info
.raw_address
= pef
->peHeader().getPointerToRawData(i
);
76 info
.raw_size
= pef
->peHeader().getSizeOfRawData(i
);
77 info
.characteristics
= pef
->peHeader().getCharacteristics(i
);
85 bool pe_file::add_section(PeLib::PeFileT
<_Bits
>* pef
,
86 const std::string
& name
, const bytes
& buffer
,
89 using namespace PeLib
;
91 // Check if the last section has the same name as the one being added.
92 PeLib::word secnum
= pef
->peHeader().getNumberOfSections();
93 if (pef
->peHeader().getSectionName(secnum
-1) == name
)
95 // If it is, we change the attributes of the existing section.
97 pef
->peHeader().setSizeOfRawData(secnum
,
98 alignOffset(buffer
.size(),
99 pef
->peHeader().getFileAlignment()));
100 pef
->peHeader().setVirtualSize(secnum
,
101 alignOffset(buffer
.size(),
102 pef
->peHeader().getSectionAlignment()));
103 PeLib::dword chars
= pef
->peHeader().getCharacteristics(secnum
-1);
104 pef
->peHeader().setCharacteristics(secnum
,
105 chars
| PELIB_IMAGE_SCN_MEM_WRITE
| PELIB_IMAGE_SCN_MEM_READ
);
109 // Otherwise we add a new section.
110 if (pef
->peHeader().addSection(name
, buffer
.size()) != NO_ERROR
)
114 pef
->peHeader().makeValid(pef
->mzHeader().getAddressOfPeHeader());
115 pef
->peHeader().write(pef
->getFileName(), pef
->mzHeader().getAddressOfPeHeader());
118 // Save the section headers to the file.
119 if (pef
->peHeader().writeSections(pef
->getFileName()) != NO_ERROR
)
124 // Save the section data to the file.
125 if (pef
->peHeader().writeSectionData(pef
->getFileName(), secnum
, buffer
) != NO_ERROR
)
130 // Fill out the section information.
132 info
.virtual_address
= pef
->peHeader().getVirtualAddress(secnum
);
133 info
.virtual_size
= pef
->peHeader().getVirtualSize(secnum
);
134 info
.raw_address
= pef
->peHeader().getPointerToRawData(secnum
);
135 info
.raw_size
= pef
->peHeader().getSizeOfRawData(secnum
);
136 info
.characteristics
= pef
->peHeader().getCharacteristics(secnum
);
140 }; //namespace shared
142 #endif //SHARED_PE_H_INCLUDED