2018-06-11 00:46:42 +00:00
|
|
|
using Ryujinx.HLE.Loaders.Compression;
|
2018-02-04 23:08:20 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
|
2018-06-11 00:46:42 +00:00
|
|
|
namespace Ryujinx.HLE.Loaders.Executables
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
class NxStaticObject : IExecutable
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
public byte[] Text { get; private set; }
|
2018-12-06 11:16:24 +00:00
|
|
|
public byte[] Ro { get; private set; }
|
2018-12-05 00:52:39 +00:00
|
|
|
public byte[] Data { get; private set; }
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public int TextOffset { get; private set; }
|
2018-12-06 11:16:24 +00:00
|
|
|
public int RoOffset { get; private set; }
|
2018-12-05 00:52:39 +00:00
|
|
|
public int DataOffset { get; private set; }
|
|
|
|
public int BssSize { get; private set; }
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public int BssOffset => DataOffset + Data.Length;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
[Flags]
|
|
|
|
private enum NsoFlags
|
|
|
|
{
|
|
|
|
IsTextCompressed = 1 << 0,
|
2018-12-06 11:16:24 +00:00
|
|
|
IsRoCompressed = 1 << 1,
|
2018-02-04 23:08:20 +00:00
|
|
|
IsDataCompressed = 1 << 2,
|
|
|
|
HasTextHash = 1 << 3,
|
2018-12-06 11:16:24 +00:00
|
|
|
HasRoHash = 1 << 4,
|
2018-02-04 23:08:20 +00:00
|
|
|
HasDataHash = 1 << 5
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public NxStaticObject(Stream input)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00: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);
|
|
|
|
|
|
|
|
int textSize = reader.ReadInt32();
|
|
|
|
int roSize = reader.ReadInt32();
|
|
|
|
int dataSize = reader.ReadInt32();
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
TextOffset = textMemOffset;
|
|
|
|
RoOffset = roMemOffset;
|
|
|
|
DataOffset = dataMemOffset;
|
|
|
|
BssSize = bssSize;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
//Text segment
|
2018-12-06 11:16:24 +00:00
|
|
|
input.Seek(textOffset, SeekOrigin.Begin);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
Text = reader.ReadBytes(textSize);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (flags.HasFlag(NsoFlags.IsTextCompressed))
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
Text = Lz4.Decompress(Text, textDecSize);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Read-only data segment
|
2018-12-06 11:16:24 +00:00
|
|
|
input.Seek(roOffset, SeekOrigin.Begin);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
Ro = reader.ReadBytes(roSize);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (flags.HasFlag(NsoFlags.IsRoCompressed))
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
Ro = Lz4.Decompress(Ro, roDecSize);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Data segment
|
2018-12-06 11:16:24 +00:00
|
|
|
input.Seek(dataOffset, SeekOrigin.Begin);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
Data = reader.ReadBytes(dataSize);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (flags.HasFlag(NsoFlags.IsDataCompressed))
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
Data = Lz4.Decompress(Data, dataDecSize);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|