aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/Loaders/Executables/Nso.cs
blob: c7b48a5f350b9c7d3ff6ac4d8a474eda6ef4f110 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using Ryujinx.HLE.Loaders.Compression;
using System;
using System.IO;

namespace Ryujinx.HLE.Loaders.Executables
{
    class Nso : IExecutable
    {
        public string FilePath { get; private set; }

        public byte[] Text { get; private set; }
        public byte[] RO   { get; private set; }
        public byte[] Data { get; private set; }

        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; }

        public long SourceAddress { get; private set; }
        public long BssAddress    { get; private set; }

        [Flags]
        private enum NsoFlags
        {
            IsTextCompressed = 1 << 0,
            IsROCompressed   = 1 << 1,
            IsDataCompressed = 1 << 2,
            HasTextHash      = 1 << 3,
            HasROHash        = 1 << 4,
            HasDataHash      = 1 << 5
        }

        public Nso(Stream Input, string FilePath)
        {
            this.FilePath = FilePath;

            SourceAddress = 0;
            BssAddress    = 0;

            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;

            this.TextOffset = TextMemOffset;
            this.ROOffset   = ROMemOffset;
            this.DataOffset = DataMemOffset;
            this.BssSize    = BssSize;

            //Text segment
            Input.Seek(TextOffset, SeekOrigin.Begin);

            Text = Reader.ReadBytes(TextSize);

            if (Flags.HasFlag(NsoFlags.IsTextCompressed) || true)
            {
                Text = Lz4.Decompress(Text, TextDecSize);
            }

            //Read-only data segment
            Input.Seek(ROOffset, SeekOrigin.Begin);

            RO = Reader.ReadBytes(ROSize);

            if (Flags.HasFlag(NsoFlags.IsROCompressed) || true)
            {
                RO = Lz4.Decompress(RO, RODecSize);
            }

            //Data segment
            Input.Seek(DataOffset, SeekOrigin.Begin);

            Data = Reader.ReadBytes(DataSize);

            if (Flags.HasFlag(NsoFlags.IsDataCompressed) || true)
            {
                Data = Lz4.Decompress(Data, DataDecSize);
            }

            using (MemoryStream TextMS = new MemoryStream(Text))
            {
                BinaryReader TextReader = new BinaryReader(TextMS);

                TextMS.Seek(4, SeekOrigin.Begin);

                Mod0Offset = TextReader.ReadInt32();
            }
        }
    }
}