Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fjeremic committed Dec 18, 2019
0 parents commit 90b6b3c
Show file tree
Hide file tree
Showing 46 changed files with 6,470 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Visual Studio
**/.vs/**
**/bin/
**/obj/
Binary file added BinaryTools.Elf.Tests/Binaries/base32
Binary file not shown.
Binary file added BinaryTools.Elf.Tests/Binaries/base64
Binary file not shown.
Binary file added BinaryTools.Elf.Tests/Binaries/calendar
Binary file not shown.
Binary file added BinaryTools.Elf.Tests/Binaries/cksum
Binary file not shown.
Binary file added BinaryTools.Elf.Tests/Binaries/du
Binary file not shown.
Binary file added BinaryTools.Elf.Tests/Binaries/helloworld32le
Binary file not shown.
Binary file added BinaryTools.Elf.Tests/Binaries/helloworld64le
Binary file not shown.
Binary file added BinaryTools.Elf.Tests/Binaries/hexdump
Binary file not shown.
1,964 changes: 1,964 additions & 0 deletions BinaryTools.Elf.Tests/Binaries/invalidclass

Large diffs are not rendered by default.

1,964 changes: 1,964 additions & 0 deletions BinaryTools.Elf.Tests/Binaries/invalidendianness

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions BinaryTools.Elf.Tests/Binaries/invalidmagicbytes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Text file which is not an ELF binary.
Binary file added BinaryTools.Elf.Tests/Binaries/last
Binary file not shown.
Binary file added BinaryTools.Elf.Tests/Binaries/less
Binary file not shown.
Binary file added BinaryTools.Elf.Tests/Binaries/man
Binary file not shown.
Binary file added BinaryTools.Elf.Tests/Binaries/rcp
Binary file not shown.
Binary file added BinaryTools.Elf.Tests/Binaries/sudo
Binary file not shown.
Binary file added BinaryTools.Elf.Tests/Binaries/unzip
Binary file not shown.
Binary file added BinaryTools.Elf.Tests/Binaries/whoami
Binary file not shown.
Binary file added BinaryTools.Elf.Tests/Binaries/yes
Binary file not shown.
90 changes: 90 additions & 0 deletions BinaryTools.Elf.Tests/BinaryTools.Elf.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BinaryTools.Elf\BinaryTools.Elf.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Binaries\" />
</ItemGroup>

<ItemGroup>
<None Update="Binaries\base32">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\base64">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\calendar">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\cksum">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\du">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\helloworld32le">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\helloworld64le">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\hexdump">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\invalidclass">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\invalidendianness">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\invalidmagicbytes">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\last">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\less">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\man">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\objdump">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\rcp">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\sudo">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\textfile">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\unzip">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\whoami">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Binaries\yes">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
66 changes: 66 additions & 0 deletions BinaryTools.Elf.Tests/TestElfFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using BinaryTools.Elf.Io;
using System;
using System.IO;
using Xunit;

namespace BinaryTools.Elf.Tests
{
public class TestElfFile
{
[Fact]
public void FileExists()
{
var stream = new FileStream("Binaries/base32", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
_ = ElfFile.ReadElfFile(reader);
}

[Fact]
public void ClosedStream()
{
var stream = new FileStream("Binaries/base32", FileMode.Open, FileAccess.Read);
stream.Close();

void action() => new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
Assert.Throws<ArgumentException>(action);
}

[Fact]
public void NativeEnidanness()
{
var stream = new FileStream("Binaries/base32", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
Assert.Equal(reader.Endianness, EndianBitConverter.NativeEndianness);
}

[Fact]
public void InvalidMagicBytes()
{
var stream = new FileStream("Binaries/invalidmagicbytes", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
void action() => ElfFile.ReadElfFile(reader);

Assert.Throws<FileFormatException>(action);
}

[Fact]
public void InvalidClass()
{
var stream = new FileStream("Binaries/invalidclass", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
void action() => ElfFile.ReadElfFile(reader);

Assert.Throws<FileFormatException>(action);
}

[Fact]
public void InvalidEndianness()
{
var stream = new FileStream("Binaries/invalidendianness", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
void action() => ElfFile.ReadElfFile(reader);

Assert.Throws<FileFormatException>(action);
}
}
}
59 changes: 59 additions & 0 deletions BinaryTools.Elf.Tests/TestElfHeader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using BinaryTools.Elf.Io;
using System.IO;
using Xunit;

namespace BinaryTools.Elf.Tests
{
public class TestElfHeader
{
[Fact]
public void CorrectEntryOffset()
{
var stream = new FileStream("Binaries/base32", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
ElfFile elfFile = ElfFile.ReadElfFile(reader);

Assert.Equal(0x00000000004019e0UL, elfFile.Header.EntryOffset);
}

[Fact]
public void CorrectClass32()
{
var stream = new FileStream("Binaries/helloworld32le", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
ElfFile elfFile = ElfFile.ReadElfFile(reader);

Assert.Equal(ElfHeader.ELFCLASS32, elfFile.Header.Class);
}

[Fact]
public void CorrectClass64()
{
var stream = new FileStream("Binaries/helloworld64le", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
ElfFile elfFile = ElfFile.ReadElfFile(reader);

Assert.Equal(ElfHeader.ELFCLASS64, elfFile.Header.Class);
}

[Fact]
public void CorrectEndiannessLE()
{
var stream = new FileStream("Binaries/helloworld64le", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
ElfFile elfFile = ElfFile.ReadElfFile(reader);

Assert.Equal(ElfHeader.ELFDATA2LSB, elfFile.Header.Data);
}

[Fact]
public void CorrectEndiannessBE()
{
var stream = new FileStream("Binaries/helloworld64be", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
ElfFile elfFile = ElfFile.ReadElfFile(reader);

Assert.Equal(ElfHeader.ELFDATA2MSB, elfFile.Header.Data);
}
}
}
31 changes: 31 additions & 0 deletions BinaryTools.Elf.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29519.87
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BinaryTools.Elf", "BinaryTools.Elf\BinaryTools.Elf.csproj", "{C552CA5B-18A2-41D6-A368-43DDA40DB62F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BinaryTools.Elf.Tests", "BinaryTools.Elf.Tests\BinaryTools.Elf.Tests.csproj", "{BF0C32E4-9551-4EB9-AF3F-B3545BACEF07}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C552CA5B-18A2-41D6-A368-43DDA40DB62F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C552CA5B-18A2-41D6-A368-43DDA40DB62F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C552CA5B-18A2-41D6-A368-43DDA40DB62F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C552CA5B-18A2-41D6-A368-43DDA40DB62F}.Release|Any CPU.Build.0 = Release|Any CPU
{BF0C32E4-9551-4EB9-AF3F-B3545BACEF07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BF0C32E4-9551-4EB9-AF3F-B3545BACEF07}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BF0C32E4-9551-4EB9-AF3F-B3545BACEF07}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BF0C32E4-9551-4EB9-AF3F-B3545BACEF07}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {37AEE0FB-B6FC-4E09-9D2A-73A8EC485BF2}
EndGlobalSection
EndGlobal
84 changes: 84 additions & 0 deletions BinaryTools.Elf/BinaryReaderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.IO;
using System.Text;

namespace BinaryTools.Elf
{
/// <summary>
/// An extension class providing <see cref="BinaryReader"/> utility methods for extracting ELF specific data.
/// </summary>
public static class BinaryReaderExtensions
{
/// <summary>
/// Reads a string value from the binary reader.
/// </summary>
///
/// <param name="reader">
/// The binary reader used to extract data.
/// </param>
///
/// <returns>
/// The string value.
/// </returns>
///
/// <remarks>
/// The string is defined as a null terminated sequence of character values.
/// </remarks>
public static String ReadELFString(this BinaryReader reader)
{
StringBuilder builder = new StringBuilder();

Char c = (Char)reader.ReadByte();

// Read the entire null terminated string
while (c != 0)
{
builder.Append(c); c = (Char)reader.ReadByte();
}

return builder.ToString();
}

/// <summary>
/// Reads a string value from an index into the ELF string table section.
/// </summary>
///
/// <param name="reader">
/// The binary reader used to extract data.
/// </param>
///
/// <param name="section">
/// The ELF string table section.
/// </param>
///
/// <param name="offset">
/// The offset of the string in the ELF string table section.
/// </param>
///
/// <returns>
/// The string value extracted from the ELF string table section at the specified offset.
/// </returns>
///
/// <remarks>
/// The string is defined as a null terminated sequence of character values.
/// </remarks>
public static String ReadELFString(this BinaryReader reader, ElfSection section, UInt64 offset)
{
String value = null;

if (section != null && offset < section.Size)
{
Int64 savedPosition = reader.BaseStream.Position;

reader.BaseStream.Position = (Int64)(section.Offset + offset);

// Read the entire null terminated string
value = reader.ReadELFString();

reader.BaseStream.Position = savedPosition;
}

return value;
}
}
}
13 changes: 13 additions & 0 deletions BinaryTools.Elf/BinaryTools.Elf.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="Bit32\" />
<Folder Include="Bit64\" />
<Folder Include="Io\" />
</ItemGroup>

</Project>
Loading

0 comments on commit 90b6b3c

Please sign in to comment.