2020-07-09 00:31:15 -04:00
|
|
|
using LibHac.Common;
|
2020-04-07 18:41:02 -04:00
|
|
|
using LibHac.Fs;
|
2020-07-03 19:58:01 -04:00
|
|
|
using LibHac.FsSystem;
|
|
|
|
using LibHac.Loader;
|
2020-07-09 00:31:15 -04:00
|
|
|
using System;
|
2020-04-07 18:41:02 -04:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.Loaders.Executables
|
|
|
|
{
|
2020-07-03 19:58:01 -04:00
|
|
|
class NsoExecutable : IExecutable
|
2020-04-07 18:41:02 -04:00
|
|
|
{
|
2020-07-09 00:31:15 -04:00
|
|
|
public byte[] Program { get; }
|
|
|
|
public Span<byte> Text => Program.AsSpan().Slice(TextOffset, TextSize);
|
|
|
|
public Span<byte> Ro => Program.AsSpan().Slice(RoOffset, RoSize);
|
|
|
|
public Span<byte> Data => Program.AsSpan().Slice(DataOffset, DataSize);
|
2020-04-07 18:41:02 -04:00
|
|
|
|
2020-07-03 19:58:01 -04:00
|
|
|
public int TextOffset { get; }
|
|
|
|
public int RoOffset { get; }
|
|
|
|
public int DataOffset { get; }
|
2020-04-07 18:41:02 -04:00
|
|
|
public int BssOffset => DataOffset + Data.Length;
|
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
public int TextSize { get; }
|
|
|
|
public int RoSize { get; }
|
|
|
|
public int DataSize { get; }
|
2020-07-03 19:58:01 -04:00
|
|
|
public int BssSize { get; }
|
2020-04-07 18:41:02 -04:00
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
public string Name;
|
|
|
|
public Buffer32 BuildId;
|
|
|
|
|
|
|
|
public NsoExecutable(IStorage inStorage, string name = null)
|
2020-04-07 18:41:02 -04:00
|
|
|
{
|
2020-07-03 19:58:01 -04:00
|
|
|
NsoReader reader = new NsoReader();
|
|
|
|
|
|
|
|
reader.Initialize(inStorage.AsFile(OpenMode.Read)).ThrowIfFailure();
|
|
|
|
|
|
|
|
TextOffset = (int)reader.Header.Segments[0].MemoryOffset;
|
|
|
|
RoOffset = (int)reader.Header.Segments[1].MemoryOffset;
|
|
|
|
DataOffset = (int)reader.Header.Segments[2].MemoryOffset;
|
|
|
|
BssSize = (int)reader.Header.BssSize;
|
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
reader.GetSegmentSize(NsoReader.SegmentType.Data, out uint uncompressedSize).ThrowIfFailure();
|
|
|
|
Program = new byte[DataOffset + uncompressedSize];
|
|
|
|
|
|
|
|
TextSize = DecompressSection(reader, NsoReader.SegmentType.Text, TextOffset, Program);
|
|
|
|
RoSize = DecompressSection(reader, NsoReader.SegmentType.Ro, RoOffset, Program);
|
|
|
|
DataSize = DecompressSection(reader, NsoReader.SegmentType.Data, DataOffset, Program);
|
|
|
|
|
|
|
|
Name = name;
|
|
|
|
BuildId = reader.Header.ModuleId;
|
2020-07-03 19:58:01 -04:00
|
|
|
}
|
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
private static int DecompressSection(NsoReader reader, NsoReader.SegmentType segmentType, int offset, byte[] Program)
|
2020-07-03 19:58:01 -04:00
|
|
|
{
|
|
|
|
reader.GetSegmentSize(segmentType, out uint uncompressedSize).ThrowIfFailure();
|
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
var span = Program.AsSpan().Slice(offset, (int)uncompressedSize);
|
2020-07-03 19:58:01 -04:00
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
reader.ReadSegment(segmentType, span).ThrowIfFailure();
|
2020-07-03 19:58:01 -04:00
|
|
|
|
2020-07-09 00:31:15 -04:00
|
|
|
return (int)uncompressedSize;
|
2020-04-07 18:41:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|