2018-11-28 14:18:09 -08:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.Loaders.Compression
|
|
|
|
{
|
|
|
|
static class BackwardsLz
|
|
|
|
{
|
|
|
|
private class BackwardsReader
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
private Stream BaseStream;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
public BackwardsReader(Stream BaseStream)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
this.BaseStream = BaseStream;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public byte ReadByte()
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
BaseStream.Seek(-1, SeekOrigin.Current);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
byte Value = (byte)BaseStream.ReadByte();
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
BaseStream.Seek(-1, SeekOrigin.Current);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
return Value;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public short ReadInt16()
|
|
|
|
{
|
|
|
|
return (short)((ReadByte() << 8) | (ReadByte() << 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
public int ReadInt32()
|
|
|
|
{
|
|
|
|
return ((ReadByte() << 24) |
|
|
|
|
(ReadByte() << 16) |
|
|
|
|
(ReadByte() << 8) |
|
|
|
|
(ReadByte() << 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
public static byte[] Decompress(Stream Input, int DecompressedLength)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
long End = Input.Position;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
BackwardsReader Reader = new BackwardsReader(Input);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
int AdditionalDecLength = Reader.ReadInt32();
|
|
|
|
int StartOffset = Reader.ReadInt32();
|
|
|
|
int CompressedLength = Reader.ReadInt32();
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
Input.Seek(12 - StartOffset, SeekOrigin.Current);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
byte[] Dec = new byte[DecompressedLength];
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
int DecompressedLengthUnpadded = CompressedLength + AdditionalDecLength;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
int DecompressionStart = DecompressedLength - DecompressedLengthUnpadded;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
int DecPos = Dec.Length;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
byte Mask = 0;
|
|
|
|
byte Header = 0;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
while (DecPos > DecompressionStart)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
if ((Mask >>= 1) == 0)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
Header = Reader.ReadByte();
|
|
|
|
Mask = 0x80;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
if ((Header & Mask) == 0)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
Dec[--DecPos] = Reader.ReadByte();
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
ushort Pair = (ushort)Reader.ReadInt16();
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
int Length = (Pair >> 12) + 3;
|
|
|
|
int Position = (Pair & 0xfff) + 3;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
DecPos -= Length;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
if (Length <= Position)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
int SrcPos = DecPos + Position;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
Buffer.BlockCopy(Dec, SrcPos, Dec, DecPos, Length);
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
for (int Offset = 0; Offset < Length; Offset++)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
Dec[DecPos + Offset] = Dec[DecPos + Position + Offset];
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
return Dec;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|