xMcacutt.Binft 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package xMcacutt.Binft --version 1.0.1
NuGet\Install-Package xMcacutt.Binft -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="xMcacutt.Binft" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add xMcacutt.Binft --version 1.0.1
#r "nuget: xMcacutt.Binft, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install xMcacutt.Binft as a Cake Addin
#addin nuget:?package=xMcacutt.Binft&version=1.0.1

// Install xMcacutt.Binft as a Cake Tool
#tool nuget:?package=xMcacutt.Binft&version=1.0.1

Binft

Binft - Binary File Tools

Binft was created to assist with reading and writing data to filestreams when working with binary files for reverse engineering and researching file formats. This library provides a very high level api to make repetitive coding patterns in reading and writing data significantly shorter.

There are three main classes involved in the function of the API.

Binft

The <span style="color:mediumseagreen">Binft</span> class provides static methods for creating and opening filestreams wrapped in <span style="color:mediumseagreen">Binf</span> objects which can then be accessed through the public methods belonging to the <span style="color:mediumseagreen">Binf</span> object.

//CREATE NEW FILE AT FILEPATH WITH BINF OBJECT FOR ACCESS
public static void Main(string[] args){
    bool isLittleEndian = true;
    if(!File.Exists(args[0])) return;
    string filePath = args[0];
    Binf binf = Binft.CreateFile(filePath, isLittleEndian);
}


//OPEN EXISTING FILE FROM FILEPATH WITH BINF OBJECT FOR ACCESS
public static void Main(string[] args){
    bool isLittleEndian = false;
    string filePath = args[0];
    Binf binf = Binft.OpenFile(filePath, isLittleEndian);
}

DataRead

The <span style="color:mediumseagreen">DataRead</span> class is not visible to the end user. It provides a variable endian implementation of the <span style="color:mediumseagreen">BitConverter</span> class. The endianness of the binary file is specified in the <span style="color:khaki">OpenFile</span> or <span style="color:khaki">CreateFile</span> methods belonging to <span style="color:mediumseagreen">Binft</span>. These methods access the <span style="color:mediumseagreen">Binf</span> constructor which sets the endiannes of the file. The endianness boolean is passed to all of the <span style="color:mediumseagreen">DataRead</span> methods so after the create or open method, no attention needs to be given to the endianness when processing data.

When writing data, the following data types can be provided to the <span style="color:khaki">Write</span> method:

int
uint
short
ushort
long
ulong
string
fixedstring //custom type with a fixed length
byte
double
float
int[]
short[]
long[]
float[]
double[]
byte[]

FixedString

The <span style="color:mediumturquoise">fixedstring</span> is a custom struct designed to deal with files which have a set capacity for strings such as in a name table. When defining a <span style="color:mediumturquoise">fixedstring</span>, the size of the string must be given along with the text. The fixed string exposes two methods to the user, the <span style="color:khaki">GetBytes</span> method provides the byte representation of the string while the <span style="color:khaki">ToString</span> override gives the string representation. The length can be set dynamically using the <span style="color:plum">Length</span> property and will resize the byte array for the byte representation. The text can be set dynamically using the <span style="color:plum">Text</span> property but can only be gotten with the <span style="color:khaki">ToString</span> override which is used implicitly if the <span style="color:mediumturquoise">fixedstring</span> is passed when a string is expected.

Example:

//READ FIXED STRINGS FROM FILE
int stringLength = 0x20;
for(int i = 0; i < stringCount; i++){
    fixedstring fString = binf.ReadString(stringLength);
}

//CREATE FIXED STRING, EDIT PROPERTIES, PRINT, AND WRITE TO FILE
fixedstring fString = new fixedstring("Hello World", 0x18);
fString.Length = 0x20;
fString.Text = "I Changed My Mind";
Console.WriteLine(fString);
byte[] bytes = fString.GetBytes();
binf.Write(fString);

Binf

The <span style="color:mediumseagreen">Binf</span> class is the general purpose binary file class. Its constructor is not exposed so the object must be created through the <span style="color:mediumseagreen">Binft</span> methods described above. The <span style="color:mediumseagreen">Binf</span> instance methods are exposed and can be used to shorten common coding patterns associated with reading and writing data to a binary file. When a <span style="color:mediumseagreen">Binf</span> is instantiated, a <span style="color:mediumseagreen">FileStream</span> is created and associated with the <span style="color:mediumseagreen">Binf</span> object. The <span style="color:mediumseagreen">FileStream</span> is not directly exposed but the following methods allow for manipulation of the stream:

Methods
Method Description
<span style="color:khaki">Close</span>() Closes the binary file stream.
<span style="color:khaki">GoTo</span>(int) Seeks to a position in the binary stream from the start of the file.
<span style="color:khaki">GoTo</span>(long) Seeks to a position in the binary stream from the start of the file.
<span style="color:khaki">ReadByte</span>() Reads the next byte from the file, advancing the stream by 1 byte.
<span style="color:khaki">ReadBytes</span>(int) Reads a byte array from the file, advancing the stream by the specified length.
<span style="color:khaki">ReadDouble</span>() Reads a double from the file, advancing the stream by 8 bytes.
<span style="color:khaki">ReadFloat</span>() Reads a float from the file, advancing the stream by 4 bytes.
<span style="color:khaki">ReadFloatArray</span>(int) Reads a float array from the file, advancing the stream by the specified count multiplied by 4.
<span style="color:khaki">ReadInt</span>() Reads an integer from the file, advancing the stream by 4 bytes.
<span style="color:khaki">ReadIntArray</span>(int) Reads an int array from the file, advancing the stream by the specified count multiplied by 4.
<span style="color:khaki">ReadLong</span>() Reads a long from the file, advancing the stream by 8 bytes.
<span style="color:khaki">ReadShort</span>() Reads a short from the file, advancing the stream by 2 bytes.
<span style="color:khaki">ReadString</span>() Reads a null-terminated string from the file. The number of bytes advanced through the stream matches the length of the string found.
<span style="color:khaki">ReadString</span>(char) Reads a char-terminated string from the file. The number of bytes advanced through the stream matches the length of the string found.
<span style="color:khaki">ReadString</span>(int) Reads a null-terminated string from the file. The number of bytes advanced through the stream is set by the length parameter.
<span style="color:khaki">ReadString</span>(int, char) Reads a null-terminated string from the file. The number of bytes advanced through the stream is set by the length parameter.
<span style="color:khaki">ReadUInt</span>() Reads an unsigned integer from the file, advancing the stream by 4 bytes.
<span style="color:khaki">ReadULong</span>() Reads an unsigned long from the file, advancing the stream by 8 bytes.
<span style="color:khaki">ReadUShort</span>() Reads an unsigned short from the file, advancing the stream by 2 bytes.
<span style="color:khaki">Skip</span>(int) Seeks to a position in the binary stream from the current position in the file.
<span style="color:khaki">Write<T></span>(T) Writes the byte representation of data to the binary file.
<span style="color:khaki">WriteToPosition<T></span>(T, int) Writes the byte representation of data to the binary file at a specified position and then returns to the current position in the stream.
Properties

The <span style="color:mediumseagreen">Binf</span> class only contains one property, <span style="color:plum">Position</span>, which can be used to get the current position of the stream in the file.

Future Plans

The goal with binf is to begin implementing common file format structures to aid in their reversal. The primary format goals are

  • Archives

  • Images

  • 3D Models

  • Audio

  • Video

These will come as separate packages but will also all be pulled into the main binft nuget. For example, binft.arch (binary file tools - acrhive).

Author

This package was created by Matthew Cacutt.

You can find me on Github or join my Discord.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 152 11/6/2023
1.0.2 81 11/6/2023
1.0.1 88 11/6/2023
1.0.0 84 11/6/2023