• R/O
  • SSH
  • HTTPS

peframework: Commit


Commit MetaInfo

Revision9 (tree)
Time2017-07-28 05:15:59
Authorquiret

Log Message

- added support for Linux makefile compilation
- Linux compatibility improvements

Change Summary

Incremental Difference

--- include/peframework.h (revision 8)
+++ include/peframework.h (revision 9)
@@ -3,6 +3,10 @@
33 #ifndef _PEFRAMEWORK_MAIN_INCLUDE_
44 #define _PEFRAMEWORK_MAIN_INCLUDE_
55
6+// Basic dependencies.
7+#include <cstddef>
8+#include <sdk/MacroUtils.h>
9+
610 // Exception info needs first.
711 #include "peexcept.h"
812
--- include/peloader.h (revision 8)
+++ include/peloader.h (revision 9)
@@ -673,7 +673,7 @@
673673
674674 // Write helpers for native numbers.
675675 #define PESECT_WRITEHELPER( typeName, type ) \
676- inline void Write##typeName##( type value, std::int32_t dataOff = 0 ) \
676+ inline void Write##typeName( type value, std::int32_t dataOff = 0 ) \
677677 { \
678678 this->WriteToSection( &value, sizeof(value), dataOff ); \
679679 }
@@ -1582,7 +1582,7 @@
15821582 }
15831583
15841584 template <typename callbackType>
1585- inline void ForAllChildren( callbackType& cb ) const
1585+ inline void ForAllChildren( const callbackType& cb ) const
15861586 {
15871587 for ( const PEResourceItem *childItem : this->namedChildren )
15881588 {
@@ -1699,7 +1699,7 @@
16991699 struct item
17001700 {
17011701 std::uint16_t offset : 12;
1702- eRelocType type : 4;
1702+ std::uint16_t type : 4; // had to change this away from enum because GCC is being a bitch
17031703 };
17041704 static_assert( sizeof(item) == sizeof(std::uint16_t), "invalid item size" );
17051705
--- include/pestream.h (revision 8)
+++ include/pestream.h (revision 9)
@@ -1,6 +1,10 @@
11 #ifndef _PEFRAMEWORK_STREAM_
22 #define _PEFRAMEWORK_STREAM_
33
4+#include <cstddef>
5+
6+#include <sdk/MacroUtils.h>
7+
48 typedef long long pe_file_ptr_t;
59
610 struct PEStream abstract
--- include/peloader.serialize.h (revision 8)
+++ include/peloader.serialize.h (revision 9)
@@ -12,6 +12,7 @@
1212 {
1313
1414 // Main PE headers.
15+#define PEL_IMAGE_DOS_SIGNATURE 0x5A4D
1516
1617 struct IMAGE_DOS_HEADER // DOS .EXE header
1718 {
@@ -136,6 +137,8 @@
136137 #define PEL_IMAGE_SCN_MEM_READ 0x40000000 // Section is readable.
137138 #define PEL_IMAGE_SCN_MEM_WRITE 0x80000000 // Section is writeable.
138139
140+#define PEL_IMAGE_PE_HEADER_SIGNATURE 0x4550
141+
139142 struct IMAGE_PE_HEADER
140143 {
141144 std::uint32_t Signature;
@@ -151,6 +154,9 @@
151154
152155 #define PEL_IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
153156
157+#define PEL_IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x010B
158+#define PEL_IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x020B
159+
154160 #pragma pack(1)
155161 struct IMAGE_OPTIONAL_HEADER32
156162 {
--- src/peloader.imports.cpp (revision 8)
+++ src/peloader.imports.cpp (revision 9)
@@ -1,10 +1,16 @@
11 #include "peloader.h"
22
3+#include <sdk/UniChar.h>
4+
35 PEFile::PEImportDesc* PEFile::FindImportDescriptor( const char *moduleName )
46 {
57 for ( PEImportDesc& impDesc : this->imports )
68 {
7- if ( stricmp( moduleName, impDesc.DLLName.c_str() ) == 0 )
9+ if ( UniversalCompareStrings(
10+ moduleName, strlen(moduleName),
11+ impDesc.DLLName.c_str(), impDesc.DLLName.size(),
12+ false
13+ ) )
814 {
915 return &impDesc;
1016 }
--- src/peloader.basereloc.cpp (revision 8)
+++ src/peloader.basereloc.cpp (revision 9)
@@ -32,7 +32,7 @@
3232 std::uint32_t insideChunkOff = ( rva % baserelocChunkSize );
3333
3434 PEBaseReloc::item newItem;
35- newItem.type = relocType;
35+ newItem.type = (std::uint16_t)relocType;
3636 newItem.offset = insideChunkOff;
3737
3838 relocDict.items.push_back( std::move( newItem ) );
--- src/peloader.cpp (revision 8)
+++ src/peloader.cpp (revision 9)
@@ -1,5 +1,7 @@
11 #include "peframework.h"
22
3+#include <vector>
4+
35 #include "peloader.internal.hxx"
46
57 PEFile::PEFile( void ) : resourceRoot( false, std::u16string(), 0 ), sections( 0x1000, 0x10000 )
@@ -777,7 +779,7 @@
777779 if ( !foundSpace )
778780 {
779781 // In very critical scenarios the executable may be full!
780- return false;
782+ return NULL;
781783 }
782784
783785 // We need to move the section into memory we control.
--- src/peloader.write.cpp (revision 8)
+++ src/peloader.write.cpp (revision 9)
@@ -103,11 +103,26 @@
103103 {
104104 inline item_allocInfo( void )
105105 {
106+ entry_off = 0;
107+ name_off = 0;
106108 dataitem_off = 0;
107109 }
108110 inline item_allocInfo( const item_allocInfo& right ) = delete;
109111 inline item_allocInfo( item_allocInfo&& right ) = default;
110112
113+ inline ~item_allocInfo( void )
114+ {
115+ // Delete all possible child entries.
116+ for ( const auto& pairItem : this->children )
117+ {
118+ item_allocInfo *childItem = pairItem.second;
119+
120+ delete childItem;
121+ }
122+
123+ this->children.clear();
124+ }
125+
111126 inline item_allocInfo& operator = ( const item_allocInfo& right ) = delete;
112127 inline item_allocInfo& operator = ( item_allocInfo&& right ) = default;
113128
@@ -116,21 +131,21 @@
116131 std::uint32_t name_off; // Offset to the name string (unicode); only valid if child
117132 std::uint32_t dataitem_off; // Offset to the resource data item info; only valid if leaf
118133
119- std::unordered_map <const PEFile::PEResourceItem*, item_allocInfo> children;
134+ std::unordered_map <const PEFile::PEResourceItem*, item_allocInfo*> children;
120135 };
121136
122137 template <typename callbackType>
123-static AINLINE void ForAllResourceItems( const PEFile::PEResourceDir& resDir, item_allocInfo& allocItem, callbackType& cb )
138+static AINLINE void ForAllResourceItems( const PEFile::PEResourceDir& resDir, item_allocInfo& allocItem, const callbackType& cb )
124139 {
125140 // Named children.
126141 resDir.ForAllChildren(
127142 [&]( const PEFile::PEResourceItem *childItem, bool hasIdentiferName )
128143 {
129- auto& childAllocItemNode = allocItem.children.find( childItem );
144+ const auto& childAllocItemNode = allocItem.children.find( childItem );
130145
131146 assert( childAllocItemNode != allocItem.children.end() );
132147
133- item_allocInfo& childAllocItem = childAllocItemNode->second;
148+ item_allocInfo& childAllocItem = *childAllocItemNode->second;
134149
135150 // Execute for us.
136151 cb( childItem, childAllocItem );
@@ -689,8 +704,22 @@
689704 // We allocate name strings later.
690705 childAlloc.name_off = 0;
691706
692- // Register this child alloc item.
693- infoOut.children.insert( std::make_pair( childItem, std::move( childAlloc ) ) );
707+ // Create a memory copy of it.
708+ item_allocInfo *memChildAlloc = new item_allocInfo( std::move( childAlloc ) );
709+
710+ assert( memChildAlloc != NULL );
711+
712+ try
713+ {
714+ // Register this child alloc item.
715+ infoOut.children.insert( std::make_pair( childItem, memChildAlloc ) );
716+ }
717+ catch( ... )
718+ {
719+ delete memChildAlloc;
720+
721+ throw;
722+ }
694723 }
695724
696725 // Now allocate all ID based entries.
@@ -703,7 +732,21 @@
703732 // Not named.
704733 childAlloc.name_off = 0;
705734
706- infoOut.children.insert( std::make_pair( childItem, std::move( childAlloc ) ) );
735+ // Create a memory item.
736+ item_allocInfo *memChildAlloc = new item_allocInfo( std::move( childAlloc ) );
737+
738+ assert( memChildAlloc != NULL );
739+
740+ try
741+ {
742+ infoOut.children.insert( std::make_pair( childItem, memChildAlloc ) );
743+ }
744+ catch( ... )
745+ {
746+ delete memChildAlloc;
747+
748+ throw;
749+ }
707750 }
708751 }
709752 else if ( itemType == PEResourceItem::eType::DATA )
@@ -843,11 +886,11 @@
843886
844887 for ( const PEResourceItem *childItem : writeNode.namedChildren )
845888 {
846- auto& childAllocInfoNode = allocNode.children.find( childItem );
889+ const auto& childAllocInfoNode = allocNode.children.find( childItem );
847890
848891 assert( childAllocInfoNode != allocNode.children.end() );
849892
850- const item_allocInfo& childAllocInfo = childAllocInfoNode->second;
893+ const item_allocInfo& childAllocInfo = *childAllocInfoNode->second;
851894
852895 // We write a link entry for this child.
853896 PEStructures::IMAGE_RESOURCE_DIRECTORY_ENTRY lnkEntry = { 0 };
@@ -907,11 +950,11 @@
907950 // Now all ID ones.
908951 for ( const PEResourceItem *childItem : writeNode.idChildren )
909952 {
910- auto& childAllocInfoNode = allocNode.children.find( childItem );
953+ const auto& childAllocInfoNode = allocNode.children.find( childItem );
911954
912955 assert( childAllocInfoNode != allocNode.children.end() );
913956
914- const item_allocInfo& childAllocInfo = childAllocInfoNode->second;
957+ const item_allocInfo& childAllocInfo = *childAllocInfoNode->second;
915958
916959 // We write a link entry for this child.
917960 PEStructures::IMAGE_RESOURCE_DIRECTORY_ENTRY lnkEntry = { 0 };
@@ -1612,7 +1655,7 @@
16121655 allocMan.AllocateAt( 0, sizeof( PEStructures::IMAGE_DOS_HEADER ) + (std::uint32_t)this->dos_data.progData.size() );
16131656
16141657 PEStructures::IMAGE_DOS_HEADER dos_header;
1615- dos_header.e_magic = 'ZM';
1658+ dos_header.e_magic = PEL_IMAGE_DOS_SIGNATURE;
16161659 dos_header.e_cblp = this->dos_data.cblp;
16171660 dos_header.e_cp = this->dos_data.cp;
16181661 dos_header.e_crlc = this->dos_data.crlc;
@@ -1671,7 +1714,7 @@
16711714 const std::uint32_t dosAllocSize = ( peDataPos );
16721715
16731716 PEStructures::IMAGE_PE_HEADER pe_data;
1674- pe_data.Signature = 'EP';
1717+ pe_data.Signature = PEL_IMAGE_PE_HEADER_SIGNATURE;
16751718 pe_data.FileHeader.Machine = this->pe_finfo.machine_id;
16761719 pe_data.FileHeader.NumberOfSections = (std::uint16_t)this->sections.numSections;
16771720 pe_data.FileHeader.TimeDateStamp = this->pe_finfo.timeDateStamp;
@@ -1723,7 +1766,7 @@
17231766 }
17241767
17251768 PEStructures::IMAGE_SECTION_HEADER header;
1726- strncpy( (char*)header.Name, item->shortName.c_str(), _countof(header.Name) );
1769+ strncpy( (char*)header.Name, item->shortName.c_str(), countof(header.Name) );
17271770 header.VirtualAddress = sectVirtAddr;
17281771 header.Misc.VirtualSize = allocVirtualSize;
17291772 header.SizeOfRawData = rawDataSize;
@@ -2021,7 +2064,7 @@
20212064 optHeader.SizeOfHeapReserve = this->peOptHeader.sizeOfHeapReserve;
20222065 optHeader.SizeOfHeapCommit = this->peOptHeader.sizeOfHeapCommit;
20232066 optHeader.LoaderFlags = this->peOptHeader.loaderFlags;
2024- optHeader.NumberOfRvaAndSizes = _countof(peDataDirs);
2067+ optHeader.NumberOfRvaAndSizes = countof(peDataDirs);
20252068 memcpy( headerData.dataDirs, peDataDirs, sizeof( peDataDirs ) );
20262069
20272070 PEWrite( peStream, peOptHeaderOffset, sizeof(headerData), &headerData );
@@ -2068,7 +2111,7 @@
20682111 optHeader.SizeOfHeapReserve = (std::uint32_t)this->peOptHeader.sizeOfHeapReserve;
20692112 optHeader.SizeOfHeapCommit = (std::uint32_t)this->peOptHeader.sizeOfHeapCommit;
20702113 optHeader.LoaderFlags = this->peOptHeader.loaderFlags;
2071- optHeader.NumberOfRvaAndSizes = _countof(peDataDirs);
2114+ optHeader.NumberOfRvaAndSizes = countof(peDataDirs);
20722115 memcpy( headerData.dataDirs, peDataDirs, sizeof( peDataDirs ) );
20732116
20742117 PEWrite( peStream, peOptHeaderOffset, sizeof(headerData), &headerData );
--- src/peloader.read.cpp (revision 8)
+++ src/peloader.read.cpp (revision 9)
@@ -203,7 +203,7 @@
203203
204204 // Verify DOS header (light).
205205 bool isValidDOSHeader =
206- ( dosHeader.e_magic == 'ZM' );
206+ ( dosHeader.e_magic == PEL_IMAGE_DOS_SIGNATURE );
207207
208208 if ( !isValidDOSHeader )
209209 {
@@ -288,7 +288,7 @@
288288 }
289289
290290 // Validate some things.
291- if ( peHeader.Signature != 'EP' )
291+ if ( peHeader.Signature != PEL_IMAGE_PE_HEADER_SIGNATURE )
292292 {
293293 throw peframework_exception(
294294 ePEExceptCode::CORRUPT_PE_STRUCTURE,
@@ -392,7 +392,7 @@
392392
393393 std::uint32_t numberOfDataDirs;
394394
395- if ( headerMagic == 0x020B )
395+ if ( headerMagic == PEL_IMAGE_NT_OPTIONAL_HDR64_MAGIC )
396396 {
397397 // We are a PE32+ file.
398398 isExtendedFormat = true;
@@ -460,7 +460,7 @@
460460 // Decrease remaining size.
461461 optHeaderSizeRemain -= sizeof(optHeaderType);
462462 }
463- else if ( headerMagic == 0x010B )
463+ else if ( headerMagic == PEL_IMAGE_NT_OPTIONAL_HDR32_MAGIC )
464464 {
465465 // We are a plain PE32 file.
466466 isExtendedFormat = false;
@@ -551,7 +551,7 @@
551551
552552 // After the optional header main information we find the data directories.
553553 // Just read them and zero out the remaining ones which we do not have available.
554- std::uint32_t canReadEntries = std::min( (std::uint32_t)_countof(dataDirs), numberOfDataDirs );
554+ std::uint32_t canReadEntries = std::min( (std::uint32_t)countof(dataDirs), numberOfDataDirs );
555555
556556 if ( canReadEntries > 0 )
557557 {
@@ -583,7 +583,7 @@
583583 }
584584
585585 // If we read less entries than we support then we have to zero out the remaining ones.
586- std::uint32_t remEntryCount = ( _countof(dataDirs) - canReadEntries );
586+ std::uint32_t remEntryCount = ( countof(dataDirs) - canReadEntries );
587587
588588 if ( remEntryCount > 0 )
589589 {
@@ -630,7 +630,7 @@
630630 pe_file_ptr_t sectHeaderOff = peStream->Tell();
631631
632632 PESection section;
633- section.shortName = std::string( (const char*)sectHeader.Name, strnlen( (const char*)sectHeader.Name, _countof(sectHeader.Name) ) );
633+ section.shortName = std::string( (const char*)sectHeader.Name, strnlen( (const char*)sectHeader.Name, countof(sectHeader.Name) ) );
634634 section.SetPlacementInfo( sectHeader.VirtualAddress, sectHeader.Misc.VirtualSize );
635635
636636 // Save characteristics flags.
@@ -1481,7 +1481,7 @@
14811481
14821482 PEBaseReloc::item itemInfo;
14831483 itemInfo.offset = reloc.offset;
1484- itemInfo.type = (PEBaseReloc::eRelocType)reloc.type;
1484+ itemInfo.type = reloc.type;
14851485
14861486 info.items.push_back( std::move( itemInfo ) );
14871487
Show on old repository browser