-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 90b6b3c
Showing
46 changed files
with
6,470 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Visual Studio | ||
**/.vs/** | ||
**/bin/ | ||
**/obj/ |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Text file which is not an ELF binary. |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.