1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-11-14 06:16:43 +00:00
Ryujinx/Ryujinx.HLE/Loaders/Compression/Lz4.cs

78 lines
1.9 KiB
C#
Raw Normal View History

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