Skip to content

Commit

Permalink
Location support interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkinsella committed Feb 2, 2016
1 parent 84b7bbe commit 99896a9
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 172 deletions.
2 changes: 1 addition & 1 deletion Src/Workshell.PE/DOSHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Workshell.PE
{

public class DOSHeader
public class DOSHeader : ISupportsLocation
{

public const ushort DOS_MAGIC_MZ = 23117;
Expand Down
2 changes: 1 addition & 1 deletion Src/Workshell.PE/DOSStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Workshell.PE
{

public class DOSStub
public class DOSStub : ISupportsLocation
{

private ImageReader reader;
Expand Down
2 changes: 1 addition & 1 deletion Src/Workshell.PE/FileHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public enum CharacteristicsType : int
BytesOfWordReserved = 0x8000
}

public class FileHeader
public class FileHeader : ISupportsLocation
{

internal static readonly int Size = Utils.SizeOf<IMAGE_FILE_HEADER>();
Expand Down
47 changes: 46 additions & 1 deletion Src/Workshell.PE/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Workshell.PE
{

public sealed class Location
public sealed class Location : IEquatable<Location>
{

internal Location(ulong fileOffset, uint rva, ulong va, uint fileSize, uint virtualSize)
Expand All @@ -19,6 +19,51 @@ internal Location(ulong fileOffset, uint rva, ulong va, uint fileSize, uint virt
VirtualSize = virtualSize;
}

#region Methods

public override bool Equals(object other)
{
return Equals(other as Location);
}

public bool Equals(Location other)
{
if (other == null)
return false;

if (FileOffset != other.FileOffset)
return false;

if (FileSize != other.FileSize)
return false;

if (RelativeVirtualAddress != other.RelativeVirtualAddress)
return false;

if (VirtualAddress != other.VirtualAddress)
return false;

if (VirtualSize != other.VirtualSize)
return false;

return true;
}

public override int GetHashCode()
{
int hash = 13;

hash = (hash * 7) + FileOffset.GetHashCode();
hash = (hash * 7) + FileSize.GetHashCode();
hash = (hash * 7) + RelativeVirtualAddress.GetHashCode();
hash = (hash * 7) + VirtualAddress.GetHashCode();
hash = (hash * 7) + VirtualSize.GetHashCode();

return hash;
}

#endregion

#region Properties

public ulong FileOffset
Expand Down
2 changes: 1 addition & 1 deletion Src/Workshell.PE/NTHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Workshell.PE
{

public class NTHeaders
public class NTHeaders : ISupportsLocation
{

public const uint PE_MAGIC_MZ = 17744;
Expand Down
2 changes: 1 addition & 1 deletion Src/Workshell.PE/OptionalHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public enum DllCharacteristicsType : int
TerminalServerAware = 0x8000
}

public abstract class OptionalHeader
public abstract class OptionalHeader : ISupportsLocation
{

public static readonly int Size32 = Utils.SizeOf<IMAGE_OPTIONAL_HEADER32>();
Expand Down
2 changes: 1 addition & 1 deletion Src/Workshell.PE/Section.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Workshell.PE
{

public class Section
public class Section : ISupportsLocation
{

private Sections _sections;
Expand Down
46 changes: 23 additions & 23 deletions Src/Workshell.PE/SectionTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public enum SectionCharacteristicsType : uint
MemoryWrite = 0x80000000
}

public class SectionTableEntry : IEquatable<SectionTableEntry>
public class SectionTableEntry : IEquatable<SectionTableEntry>, ISupportsLocation
{

private static readonly uint size = Convert.ToUInt32(Utils.SizeOf<IMAGE_SECTION_HEADER>());
Expand Down Expand Up @@ -132,28 +132,9 @@ public override string ToString()
}
}

public override int GetHashCode()
{
int prime = 397;
int result = 0;

result = (result * prime) ^ location.GetHashCode();
result = (result * prime) ^ name.GetHashCode();
result = (result * prime) ^ header.VirtualSize.GetHashCode();
result = (result * prime) ^ header.SizeOfRawData.GetHashCode();
result = (result * prime) ^ header.PointerToRawData.GetHashCode();
result = (result * prime) ^ header.PointerToRelocations.GetHashCode();
result = (result * prime) ^ header.PointerToLineNumbers.GetHashCode();
result = (result * prime) ^ header.NumberOfRelocations.GetHashCode();
result = (result * prime) ^ header.NumberOfLineNumbers.GetHashCode();
result = (result * prime) ^ header.Characteristics.GetHashCode();

return result;
}

public override bool Equals(object obj)
public override bool Equals(object other)
{
return Equals(obj as SectionTableEntry);
return Equals(other as SectionTableEntry);
}

public bool Equals(SectionTableEntry other)
Expand Down Expand Up @@ -200,6 +181,25 @@ public bool Equals(SectionTableEntry other)
return true;
}

public override int GetHashCode()
{
int prime = 397;
int result = 0;

result = (result * prime) ^ location.GetHashCode();
result = (result * prime) ^ name.GetHashCode();
result = (result * prime) ^ header.VirtualSize.GetHashCode();
result = (result * prime) ^ header.SizeOfRawData.GetHashCode();
result = (result * prime) ^ header.PointerToRawData.GetHashCode();
result = (result * prime) ^ header.PointerToRelocations.GetHashCode();
result = (result * prime) ^ header.PointerToLineNumbers.GetHashCode();
result = (result * prime) ^ header.NumberOfRelocations.GetHashCode();
result = (result * prime) ^ header.NumberOfLineNumbers.GetHashCode();
result = (result * prime) ^ header.Characteristics.GetHashCode();

return result;
}

public byte[] GetBytes()
{
return null;
Expand Down Expand Up @@ -329,7 +329,7 @@ public uint Characteristics

}

public class SectionTable : IEnumerable<SectionTableEntry>
public class SectionTable : IEnumerable<SectionTableEntry>, ISupportsLocation
{

private ImageReader reader;
Expand Down
97 changes: 0 additions & 97 deletions Src/Workshell.PE/StreamLocation.cs

This file was deleted.

46 changes: 4 additions & 42 deletions Src/Workshell.PE/SupportsLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
namespace Workshell.PE
{

public interface ILocationSupport
public interface ISupportsLocation
{

#region Properties

StreamLocation Location
Location Location
{
get;
}
Expand All @@ -21,12 +21,12 @@ StreamLocation Location

}

public abstract class LocationSupport : ILocationSupport
public abstract class SupportsLocation : ISupportsLocation
{

#region Properties

public abstract StreamLocation Location
public abstract Location Location
{
get;
}
Expand All @@ -35,42 +35,4 @@ public abstract StreamLocation Location

}

public sealed class GenericLocationSupport : LocationSupport
{

private StreamLocation location;
private object parent;

public GenericLocationSupport(long offset, long size) : this(offset,size,null)
{
}

public GenericLocationSupport(long offset, long size, object parentObj)
{
location = new StreamLocation(offset,size);
parent = parentObj;
}

#region Properties

public override StreamLocation Location
{
get
{
return location;
}
}

public object Parent
{
get
{
return parent;
}
}

#endregion

}

}
4 changes: 2 additions & 2 deletions Src/Workshell.PE/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,9 @@ public static byte[] ReadBytes(Stream stream, long offset, long size)
return buffer;
}

public static byte[] ReadBytes(Stream stream, StreamLocation location)
public static byte[] ReadBytes(Stream stream, Location location)
{
return ReadBytes(stream,location.Offset,location.Size);
return ReadBytes(stream,Convert.ToInt64(location.FileOffset),location.FileSize);
}

public static void Write<T>(T structure, byte[] buffer, int startIndex, int count) where T : struct
Expand Down
1 change: 0 additions & 1 deletion Src/Workshell.PE/Workshell.PE.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
<Compile Include="Section.cs" />
<Compile Include="SectionContent.cs" />
<Compile Include="SectionTable.cs" />
<Compile Include="StreamLocation.cs" />
<Compile Include="Utils.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down

0 comments on commit 99896a9

Please sign in to comment.