mirror of
https://github.com/Ryujinx/Ryujinx.git
synced 2024-10-01 12:30:00 +02:00
fb1d9493a3
* Rename enum fields * Naming conventions * Remove unneeded ".this" * Remove unneeded semicolons * Remove unused Usings * Don't use var * Remove unneeded enum underlying types * Explicitly label class visibility * Remove unneeded @ prefixes * Remove unneeded commas * Remove unneeded if expressions * Method doesn't use unsafe code * Remove unneeded casts * Initialized objects don't need an empty constructor * Remove settings from DotSettings * Revert "Explicitly label class visibility" This reverts commit ad5eb5787cc5b27a4631cd46ef5f551c4ae95e51. * Small changes * Revert external enum renaming * Changes from feedback * Apply previous refactorings to the merged code
105 lines
No EOL
2.9 KiB
C#
105 lines
No EOL
2.9 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace Ryujinx.HLE.Loaders.Compression
|
|
{
|
|
static class BackwardsLz
|
|
{
|
|
private class BackwardsReader
|
|
{
|
|
private Stream _baseStream;
|
|
|
|
public BackwardsReader(Stream baseStream)
|
|
{
|
|
_baseStream = baseStream;
|
|
}
|
|
|
|
public byte ReadByte()
|
|
{
|
|
_baseStream.Seek(-1, SeekOrigin.Current);
|
|
|
|
byte value = (byte)_baseStream.ReadByte();
|
|
|
|
_baseStream.Seek(-1, SeekOrigin.Current);
|
|
|
|
return value;
|
|
}
|
|
|
|
public short ReadInt16()
|
|
{
|
|
return (short)((ReadByte() << 8) | (ReadByte() << 0));
|
|
}
|
|
|
|
public int ReadInt32()
|
|
{
|
|
return ((ReadByte() << 24) |
|
|
(ReadByte() << 16) |
|
|
(ReadByte() << 8) |
|
|
(ReadByte() << 0));
|
|
}
|
|
}
|
|
|
|
public static byte[] Decompress(Stream input, int decompressedLength)
|
|
{
|
|
long end = input.Position;
|
|
|
|
BackwardsReader reader = new BackwardsReader(input);
|
|
|
|
int additionalDecLength = reader.ReadInt32();
|
|
int startOffset = reader.ReadInt32();
|
|
int compressedLength = reader.ReadInt32();
|
|
|
|
input.Seek(12 - startOffset, SeekOrigin.Current);
|
|
|
|
byte[] dec = new byte[decompressedLength];
|
|
|
|
int decompressedLengthUnpadded = compressedLength + additionalDecLength;
|
|
|
|
int decompressionStart = decompressedLength - decompressedLengthUnpadded;
|
|
|
|
int decPos = dec.Length;
|
|
|
|
byte mask = 0;
|
|
byte header = 0;
|
|
|
|
while (decPos > decompressionStart)
|
|
{
|
|
if ((mask >>= 1) == 0)
|
|
{
|
|
header = reader.ReadByte();
|
|
mask = 0x80;
|
|
}
|
|
|
|
if ((header & mask) == 0)
|
|
{
|
|
dec[--decPos] = reader.ReadByte();
|
|
}
|
|
else
|
|
{
|
|
ushort pair = (ushort)reader.ReadInt16();
|
|
|
|
int length = (pair >> 12) + 3;
|
|
int position = (pair & 0xfff) + 3;
|
|
|
|
decPos -= length;
|
|
|
|
if (length <= position)
|
|
{
|
|
int srcPos = decPos + position;
|
|
|
|
Buffer.BlockCopy(dec, srcPos, dec, decPos, length);
|
|
}
|
|
else
|
|
{
|
|
for (int offset = 0; offset < length; offset++)
|
|
{
|
|
dec[decPos + offset] = dec[decPos + position + offset];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return dec;
|
|
}
|
|
}
|
|
} |