ryujinx/Ryujinx.HLE/Loaders/Compression/Lz4.cs

78 lines
1.9 KiB
C#
Raw Normal View History

2018-02-04 18:08:20 -05:00
using System;
namespace Ryujinx.HLE.Loaders.Compression
2018-02-04 18:08:20 -05:00
{
static class Lz4
{
public static byte[] Decompress(byte[] cmp, int decLength)
2018-02-04 18:08:20 -05:00
{
byte[] dec = new byte[decLength];
2018-02-04 18:08:20 -05:00
int cmpPos = 0;
int decPos = 0;
2018-02-04 18:08:20 -05:00
int GetLength(int length)
2018-02-04 18:08:20 -05:00
{
byte sum;
2018-02-04 18:08:20 -05:00
if (length == 0xf)
2018-02-04 18:08:20 -05:00
{
do
{
length += (sum = cmp[cmpPos++]);
2018-02-04 18:08:20 -05:00
}
while (sum == 0xff);
2018-02-04 18:08:20 -05:00
}
return length;
2018-02-04 18:08:20 -05:00
}
do
{
byte token = cmp[cmpPos++];
2018-02-04 18:08:20 -05:00
int encCount = (token >> 0) & 0xf;
int litCount = (token >> 4) & 0xf;
2018-02-04 18:08:20 -05:00
//Copy literal chunck
litCount = GetLength(litCount);
2018-02-04 18:08:20 -05:00
Buffer.BlockCopy(cmp, cmpPos, dec, decPos, litCount);
2018-02-04 18:08:20 -05:00
cmpPos += litCount;
decPos += litCount;
2018-02-04 18:08:20 -05:00
if (cmpPos >= cmp.Length)
2018-02-04 18:08:20 -05:00
{
break;
}
//Copy compressed chunck
int back = cmp[cmpPos++] << 0 |
cmp[cmpPos++] << 8;
2018-02-04 18:08:20 -05:00
encCount = GetLength(encCount) + 4;
2018-02-04 18:08:20 -05:00
int encPos = decPos - back;
2018-02-04 18:08:20 -05:00
if (encCount <= back)
2018-02-04 18:08:20 -05:00
{
Buffer.BlockCopy(dec, encPos, dec, decPos, encCount);
2018-02-04 18:08:20 -05:00
decPos += encCount;
2018-02-04 18:08:20 -05:00
}
else
{
while (encCount-- > 0)
2018-02-04 18:08:20 -05:00
{
dec[decPos++] = dec[encPos++];
2018-02-04 18:08:20 -05:00
}
}
}
while (cmpPos < cmp.Length &&
decPos < dec.Length);
2018-02-04 18:08:20 -05:00
return dec;
2018-02-04 18:08:20 -05:00
}
}
}