Move PE code to a common assembly

This commit is contained in:
de4dot 2012-04-10 15:09:59 +02:00
parent 634e9ec023
commit ffa61e6a89
18 changed files with 56 additions and 41 deletions

View File

@ -20,7 +20,7 @@
using System.IO;
namespace de4dot.code.PE {
class Cor20Header : IFileLocation {
public class Cor20Header : IFileLocation {
public uint cb;
public ushort majorRuntimeVersion;
public ushort minorRuntimeVersion;
@ -70,7 +70,7 @@ namespace de4dot.code.PE {
managedNativeHeader.read(reader);
}
public void initMetadataTable() {
internal void initMetadataTable() {
metadata = new Metadata(reader);
}

View File

@ -20,7 +20,7 @@
using System.IO;
namespace de4dot.code.PE {
struct DataDirectory {
public struct DataDirectory {
public uint virtualAddress;
public uint size;

View File

@ -20,7 +20,7 @@
using System.IO;
namespace de4dot.code.PE {
class DotNetStream : IFileLocation {
public class DotNetStream : IFileLocation {
public string name;
public uint fileOffset;
public uint length;

View File

@ -20,13 +20,13 @@
using System.IO;
namespace de4dot.code.PE {
enum Machine : ushort {
public enum Machine : ushort {
i386 = 0x14C,
ia64 = 0x200,
amd64 = 0x8664,
}
class FileHeader : IFileLocation {
public class FileHeader : IFileLocation {
public Machine machine;
public ushort numberOfSections;
public uint timeDateStamp;

View File

@ -18,7 +18,7 @@
*/
namespace de4dot.code.PE {
interface IFileLocation {
public interface IFileLocation {
uint Offset { get; }
uint Length { get; }
}

View File

@ -22,7 +22,7 @@ using System.IO;
using System.Text;
namespace de4dot.code.PE {
class Metadata : IFileLocation {
public class Metadata : IFileLocation {
uint magic;
ushort majorVersion, minorVersion;
uint reserved;

View File

@ -23,7 +23,7 @@ using System.IO;
namespace de4dot.code.PE {
using MVT = MetadataVarType;
class MetadataTables {
public class MetadataTables {
BinaryReader reader;
Metadata metadata;
byte heapOffsetSizes;

View File

@ -20,7 +20,7 @@
using System.Collections.Generic;
namespace de4dot.code.PE {
enum MetadataIndex {
public enum MetadataIndex {
iModule = 0,
iTypeRef = 1,
iTypeDef = 2,
@ -60,7 +60,7 @@ namespace de4dot.code.PE {
iGenericParamConstraint = 44,
};
class MetadataType {
public class MetadataType {
public uint fileOffset;
public uint rows;
public uint totalSize;
@ -78,7 +78,7 @@ namespace de4dot.code.PE {
}
}
struct MetadataField {
public struct MetadataField {
public int offset;
public int size;

View File

@ -20,7 +20,7 @@
using System.IO;
namespace de4dot.code.PE {
class OptionalHeader : IFileLocation {
public class OptionalHeader : IFileLocation {
public ushort magic;
public byte majorLinkerVersion;
public byte minorLinkerVersion;

View File

@ -39,15 +39,15 @@ namespace de4dot.code.PE {
get { return (uint)reader.BaseStream.Length; }
}
internal Cor20Header Cor20Header {
public Cor20Header Cor20Header {
get { return cor20Header; }
}
internal Resources Resources {
public Resources Resources {
get { return resources; }
}
internal SectionHeader[] Sections {
public SectionHeader[] Sections {
get { return sectionHeaders; }
}
@ -99,7 +99,7 @@ namespace de4dot.code.PE {
if (netRva != 0) {
seekRva(netRva);
cor20Header = new Cor20Header(reader);
dotNetSection = getSectionHeader(netRva);
dotNetSection = getSectionHeaderRva(netRva);
seekRva(cor20Header.metadataDirectory.virtualAddress);
cor20Header.initMetadataTable();
}
@ -111,7 +111,7 @@ namespace de4dot.code.PE {
resources = new Resources(reader, resourceOffset, optionalHeader.dataDirectories[2].size);
}
SectionHeader getSectionHeader(uint rva) {
SectionHeader getSectionHeaderRva(uint rva) {
for (int i = 0; i < sectionHeaders.Length; i++) {
var section = sectionHeaders[i];
if (section.virtualAddress <= rva && rva < section.virtualAddress + Math.Max(section.virtualSize, section.sizeOfRawData))
@ -120,13 +120,29 @@ namespace de4dot.code.PE {
return null;
}
SectionHeader getSectionHeaderOffset(uint offset) {
for (int i = 0; i < sectionHeaders.Length; i++) {
var section = sectionHeaders[i];
if (section.pointerToRawData <= offset && offset < section.pointerToRawData + section.sizeOfRawData)
return section;
}
return null;
}
public uint rvaToOffset(uint rva) {
var section = getSectionHeader(rva);
var section = getSectionHeaderRva(rva);
if (section == null)
throw new ApplicationException(string.Format("Invalid RVA {0:X8}", rva));
return rva - section.virtualAddress + section.pointerToRawData;
}
public uint offsetToRva(uint offset) {
var section = getSectionHeaderOffset(offset);
if (section == null)
throw new ApplicationException(string.Format("Invalid offset {0:X8}", offset));
return offset - section.pointerToRawData + section.virtualAddress;
}
bool intersect(uint offset1, uint length1, uint offset2, uint length2) {
return !(offset1 + length1 <= offset2 || offset2 + length2 <= offset1);
}

View File

@ -18,7 +18,7 @@
*/
namespace de4dot.code.PE {
class ResourceData : ResourceDirectoryEntry {
public class ResourceData : ResourceDirectoryEntry {
uint rva;
uint size;

View File

@ -20,7 +20,7 @@
using System.Collections.Generic;
namespace de4dot.code.PE {
class ResourceDirectory : ResourceDirectoryEntry {
public class ResourceDirectory : ResourceDirectoryEntry {
Resources resources;
int offset;
List<ResourceData> resourceDataList = new List<ResourceData>();

View File

@ -20,7 +20,7 @@
using System.Collections.Generic;
namespace de4dot.code.PE {
abstract class ResourceDirectoryEntry {
public abstract class ResourceDirectoryEntry {
protected readonly string name;
protected readonly int id;

View File

@ -21,7 +21,7 @@ using System.IO;
using System.Text;
namespace de4dot.code.PE {
class Resources {
public class Resources {
BinaryReader reader;
uint startOffset;
uint totalSize;

View File

@ -21,7 +21,7 @@ using System.IO;
using System.Text;
namespace de4dot.code.PE {
class SectionHeader : IFileLocation {
public class SectionHeader : IFileLocation {
public byte[] name;
public uint virtualSize;
public uint virtualAddress;

View File

@ -68,6 +68,22 @@
<Compile Include="MemberRefInstance.cs" />
<Compile Include="MemberReferenceHelper.cs" />
<Compile Include="MethodBlocks.cs" />
<Compile Include="PE\Cor20Header.cs" />
<Compile Include="PE\DataDirectory.cs" />
<Compile Include="PE\DotNetStream.cs" />
<Compile Include="PE\FileHeader.cs" />
<Compile Include="PE\IFileLocation.cs" />
<Compile Include="PE\Metadata.cs" />
<Compile Include="PE\MetadataTables.cs" />
<Compile Include="PE\MetadataType.cs" />
<Compile Include="PE\MetadataTypeBuilder.cs" />
<Compile Include="PE\OptionalHeader.cs" />
<Compile Include="PE\PeImage.cs" />
<Compile Include="PE\ResourceData.cs" />
<Compile Include="PE\ResourceDirectory.cs" />
<Compile Include="PE\ResourceDirectoryEntry.cs" />
<Compile Include="PE\Resources.cs" />
<Compile Include="PE\SectionHeader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScopeBlock.cs" />
<Compile Include="TryBlock.cs" />

View File

@ -12,7 +12,6 @@
<AssemblyName>de4dot.code</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\de4dot.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
@ -225,22 +224,6 @@
<Compile Include="NameRegexes.cs" />
<Compile Include="ObfuscatedFile.cs" />
<Compile Include="Option.cs" />
<Compile Include="PE\Cor20Header.cs" />
<Compile Include="PE\DataDirectory.cs" />
<Compile Include="PE\FileHeader.cs" />
<Compile Include="PE\IFileLocation.cs" />
<Compile Include="PE\Metadata.cs" />
<Compile Include="PE\MetadataTables.cs" />
<Compile Include="PE\MetadataType.cs" />
<Compile Include="PE\MetadataTypeBuilder.cs" />
<Compile Include="PE\OptionalHeader.cs" />
<Compile Include="PE\PeImage.cs" />
<Compile Include="PE\ResourceData.cs" />
<Compile Include="PE\ResourceDirectory.cs" />
<Compile Include="PE\ResourceDirectoryEntry.cs" />
<Compile Include="PE\Resources.cs" />
<Compile Include="PE\SectionHeader.cs" />
<Compile Include="PE\DotNetStream.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="renamer\asmmodules\EventDef.cs" />
<Compile Include="ExternalAssemblies.cs" />