Files
NDecrypt/NDecrypt.N3DS/Headers/ExeFSFileHeader.cs
2024-07-20 22:00:54 -04:00

57 lines
1.7 KiB
C#

using System.IO;
using System.Linq;
using System.Text;
namespace NDecrypt.N3DS.Headers
{
internal class ExeFSFileHeader
{
// .code\0\0\0
private readonly byte[] codeSegmentBytes = [0x2e, 0x63, 0x6f, 0x64, 0x65, 0x00, 0x00, 0x00];
/// <summary>
/// File name
/// </summary>
public byte[]? FileName { get; private set; }
public string ReadableFileName { get { return Encoding.ASCII.GetString(FileName!); } }
public bool IsCodeBinary { get { return Enumerable.SequenceEqual(FileName!, codeSegmentBytes); } }
/// <summary>
/// File offset
/// </summary>
public uint FileOffset { get; private set; }
/// <summary>
/// File size
/// </summary>
public uint FileSize { get; private set; }
/// <summary>
/// SHA256 hash calculated over the entire file contents
/// </summary>
public byte[]? FileHash { get; set; }
/// <summary>
/// Read from a stream and get an ExeFS file header, if possible
/// </summary>
/// <param name="reader">BinaryReader representing the input stream</param>
/// <returns>ExeFS file header object, null on error</returns>
public static ExeFSFileHeader? Read(BinaryReader reader)
{
var header = new ExeFSFileHeader();
try
{
header.FileName = reader.ReadBytes(8);
header.FileOffset = reader.ReadUInt32();
header.FileSize = reader.ReadUInt32();
return header;
}
catch
{
return null;
}
}
}
}