2018-06-10 20:46:42 -04:00
|
|
|
using Ryujinx.HLE.Loaders.Compression;
|
2018-02-04 18:08:20 -05:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
|
2018-06-10 20:46:42 -04:00
|
|
|
namespace Ryujinx.HLE.Loaders.Executables
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-06 18:28:32 -05:00
|
|
|
class Nso : IExecutable
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-07-17 15:14:27 -04:00
|
|
|
public string FilePath { get; private set; }
|
2018-04-22 00:21:49 -04:00
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
public byte[] Text { get; private set; }
|
|
|
|
public byte[] RO { get; private set; }
|
|
|
|
public byte[] Data { get; private set; }
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
public int Mod0Offset { get; private set; }
|
|
|
|
public int TextOffset { get; private set; }
|
|
|
|
public int ROOffset { get; private set; }
|
|
|
|
public int DataOffset { get; private set; }
|
|
|
|
public int BssSize { get; private set; }
|
|
|
|
|
2018-10-09 19:01:49 -04:00
|
|
|
public long SourceAddress { get; private set; }
|
|
|
|
public long BssAddress { get; private set; }
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
[Flags]
|
|
|
|
private enum NsoFlags
|
|
|
|
{
|
|
|
|
IsTextCompressed = 1 << 0,
|
|
|
|
IsROCompressed = 1 << 1,
|
|
|
|
IsDataCompressed = 1 << 2,
|
|
|
|
HasTextHash = 1 << 3,
|
|
|
|
HasROHash = 1 << 4,
|
|
|
|
HasDataHash = 1 << 5
|
|
|
|
}
|
|
|
|
|
2018-07-17 16:28:41 -04:00
|
|
|
public Nso(Stream Input, string FilePath)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-07-17 15:14:27 -04:00
|
|
|
this.FilePath = FilePath;
|
2018-04-22 00:21:49 -04:00
|
|
|
|
2018-10-09 19:01:49 -04:00
|
|
|
SourceAddress = 0;
|
|
|
|
BssAddress = 0;
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
BinaryReader Reader = new BinaryReader(Input);
|
|
|
|
|
|
|
|
Input.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
int NsoMagic = Reader.ReadInt32();
|
|
|
|
int Version = Reader.ReadInt32();
|
|
|
|
int Reserved = Reader.ReadInt32();
|
|
|
|
int FlagsMsk = Reader.ReadInt32();
|
|
|
|
int TextOffset = Reader.ReadInt32();
|
|
|
|
int TextMemOffset = Reader.ReadInt32();
|
|
|
|
int TextDecSize = Reader.ReadInt32();
|
|
|
|
int ModNameOffset = Reader.ReadInt32();
|
|
|
|
int ROOffset = Reader.ReadInt32();
|
|
|
|
int ROMemOffset = Reader.ReadInt32();
|
|
|
|
int RODecSize = Reader.ReadInt32();
|
|
|
|
int ModNameSize = Reader.ReadInt32();
|
|
|
|
int DataOffset = Reader.ReadInt32();
|
|
|
|
int DataMemOffset = Reader.ReadInt32();
|
|
|
|
int DataDecSize = Reader.ReadInt32();
|
|
|
|
int BssSize = Reader.ReadInt32();
|
|
|
|
|
|
|
|
byte[] BuildId = Reader.ReadBytes(0x20);
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
int TextSize = Reader.ReadInt32();
|
|
|
|
int ROSize = Reader.ReadInt32();
|
|
|
|
int DataSize = Reader.ReadInt32();
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
Input.Seek(0x24, SeekOrigin.Current);
|
|
|
|
|
|
|
|
int DynStrOffset = Reader.ReadInt32();
|
|
|
|
int DynStrSize = Reader.ReadInt32();
|
|
|
|
int DynSymOffset = Reader.ReadInt32();
|
|
|
|
int DynSymSize = Reader.ReadInt32();
|
|
|
|
|
|
|
|
byte[] TextHash = Reader.ReadBytes(0x20);
|
|
|
|
byte[] ROHash = Reader.ReadBytes(0x20);
|
|
|
|
byte[] DataHash = Reader.ReadBytes(0x20);
|
|
|
|
|
|
|
|
NsoFlags Flags = (NsoFlags)FlagsMsk;
|
|
|
|
|
|
|
|
this.TextOffset = TextMemOffset;
|
|
|
|
this.ROOffset = ROMemOffset;
|
|
|
|
this.DataOffset = DataMemOffset;
|
|
|
|
this.BssSize = BssSize;
|
|
|
|
|
|
|
|
//Text segment
|
|
|
|
Input.Seek(TextOffset, SeekOrigin.Begin);
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
Text = Reader.ReadBytes(TextSize);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
if (Flags.HasFlag(NsoFlags.IsTextCompressed) || true)
|
|
|
|
{
|
2018-03-10 18:39:16 -05:00
|
|
|
Text = Lz4.Decompress(Text, TextDecSize);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//Read-only data segment
|
|
|
|
Input.Seek(ROOffset, SeekOrigin.Begin);
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
RO = Reader.ReadBytes(ROSize);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
if (Flags.HasFlag(NsoFlags.IsROCompressed) || true)
|
|
|
|
{
|
2018-03-10 18:39:16 -05:00
|
|
|
RO = Lz4.Decompress(RO, RODecSize);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//Data segment
|
|
|
|
Input.Seek(DataOffset, SeekOrigin.Begin);
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
Data = Reader.ReadBytes(DataSize);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
if (Flags.HasFlag(NsoFlags.IsDataCompressed) || true)
|
|
|
|
{
|
2018-03-10 18:39:16 -05:00
|
|
|
Data = Lz4.Decompress(Data, DataDecSize);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
using (MemoryStream TextMS = new MemoryStream(Text))
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-10 18:39:16 -05:00
|
|
|
BinaryReader TextReader = new BinaryReader(TextMS);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
TextMS.Seek(4, SeekOrigin.Begin);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
Mod0Offset = TextReader.ReadInt32();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|