aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/Loaders/Compression/BackwardsLz.cs
blob: 0a76dc950cfa422ad14581d5a9098d80801d992d (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
using System;

namespace Ryujinx.HLE.Loaders.Compression
{
    static class BackwardsLz
    {
        private class BackwardsReader
        {
            private byte[] _data;

            private int _position;

            public int Position => _position;

            public BackwardsReader(byte[] data, int end)
            {
                _data     = data;
                _position = end;
            }

            public void SeekCurrent(int offset)
            {
                _position += offset;
            }

            public byte ReadByte()
            {
                return _data[--_position];
            }

            public short ReadInt16()
            {
                return (short)((ReadByte() << 8) | (ReadByte() << 0));
            }

            public int ReadInt32()
            {
                return ((ReadByte() << 24) |
                        (ReadByte() << 16) |
                        (ReadByte() << 8)  |
                        (ReadByte() << 0));
            }
        }

        public static void DecompressInPlace(byte[] buffer, int headerEnd)
        {
            BackwardsReader reader = new BackwardsReader(buffer, headerEnd);

            int additionalDecLength = reader.ReadInt32();
            int startOffset         = reader.ReadInt32();
            int compressedLength    = reader.ReadInt32();

            reader.SeekCurrent(12 - startOffset);

            int decBase = headerEnd - compressedLength;

            int decPos = compressedLength + additionalDecLength;

            byte mask   = 0;
            byte header = 0;

            while (decPos > 0)
            {
                if ((mask >>= 1) == 0)
                {
                    header = reader.ReadByte();
                    mask   = 0x80;
                }

                if ((header & mask) == 0)
                {
                    buffer[decBase + --decPos] = reader.ReadByte();
                }
                else
                {
                    ushort pair = (ushort)reader.ReadInt16();

                    int length   = (pair >> 12)   + 3;
                    int position = (pair & 0xfff) + 3;

                    if (length > decPos)
                    {
                        length = decPos;
                    }

                    decPos -= length;

                    int dstPos = decBase + decPos;

                    if (length <= position)
                    {
                        int srcPos = dstPos + position;

                        Buffer.BlockCopy(buffer, srcPos, buffer, dstPos, length);
                    }
                    else
                    {
                        for (int offset = 0; offset < length; offset++)
                        {
                            buffer[dstPos + offset] = buffer[dstPos + position + offset];
                        }
                    }
                }
            }
        }
    }
}