public byte[] Decompress(byte[] Data)
{
MemoryStream MemData = new MemoryStream(Data);
BinaryReader Reader = new BinaryReader(MemData);
if (Data.Length > 6)
{
byte[] DecompressedData = new byte[(int)m_DecompressedSize];
int DataPos = 0;
int Pos = 0;
long Control1 = 0;
while (Control1 != 0xFC && Pos < Data.Length)
{
Control1 = Data[Pos];
Pos++;
if (Pos == Data.Length)
break;
if (Control1 >= 0 && Control1 <= 127)
{
// 0x00 - 0x7F
long control2 = Data[Pos];
Pos++;
long numberOfPlainText = (Control1 & 0x03);
ArrayCopy2(Data, Pos, ref DecompressedData, DataPos, numberOfPlainText);
DataPos += (int)numberOfPlainText;
Pos += (int)numberOfPlainText;
if (DataPos == (DecompressedData.Length))
break;
int offset = (int)(((Control1 & 0x60) << 3) + (control2) + 1);
long numberToCopyFromOffset = ((Control1 & 0x1C) >> 2) + 3;
OffsetCopy(ref DecompressedData, offset, DataPos, numberToCopyFromOffset);
DataPos += (int)numberToCopyFromOffset;
if (DataPos == (DecompressedData.Length))
break;
}
else if ((Control1 >= 128 && Control1 <= 191))
{
// 0x80 - 0xBF
long control2 = Data[Pos];
Pos++;
long control3 = Data[Pos];
Pos++;
long numberOfPlainText = (control2 >> 6) & 0x03;
ArrayCopy2(Data, Pos, ref DecompressedData, DataPos, numberOfPlainText);
DataPos += (int)numberOfPlainText;
Pos += (int)numberOfPlainText;
if (DataPos == (DecompressedData.Length))
break;
int offset = (int)(((control2 & 0x3F) << 8) + (control3) + 1);
long numberToCopyFromOffset = (Control1 & 0x3F) + 4;
OffsetCopy(ref DecompressedData, offset, DataPos, numberToCopyFromOffset);
DataPos += (int)numberToCopyFromOffset;
if (DataPos == (DecompressedData.Length))
break;
}
else if (Control1 >= 192 && Control1 <= 223)
{
// 0xC0 - 0xDF
long numberOfPlainText = (Control1 & 0x03);
long control2 = Data[Pos];
Pos++;
long control3 = Data[Pos];
Pos++;
long control4 = Data[Pos];
Pos++;
ArrayCopy2(Data, Pos, ref DecompressedData, DataPos, numberOfPlainText);
DataPos += (int)numberOfPlainText;
Pos += (int)numberOfPlainText;
if (DataPos == (DecompressedData.Length))
break;
int offset = (int)(((Control1 & 0x10) << 12) + (control2 << 8) + (control3) + 1);
long numberToCopyFromOffset = ((Control1 & 0x0C) << 6) + (control4) + 5;
OffsetCopy(ref DecompressedData, offset, DataPos, numberToCopyFromOffset);
DataPos += (int)numberToCopyFromOffset;
if (DataPos == (DecompressedData.Length))
break;
}
else if (Control1 >= 224 && Control1 <= 251)
{
// 0xE0 - 0xFB
long numberOfPlainText = ((Control1 & 0x1F) << 2) + 4;
ArrayCopy2(Data, Pos, ref DecompressedData, DataPos, numberOfPlainText);
DataPos += (int)numberOfPlainText;
Pos += (int)numberOfPlainText;
if (DataPos == (DecompressedData.Length))
break;
}
else
{
long numberOfPlainText = (Control1 & 0x03);
ArrayCopy2(Data, Pos, ref DecompressedData, DataPos, numberOfPlainText);
DataPos += (int)numberOfPlainText;
Pos += (int)numberOfPlainText;
if (DataPos == (DecompressedData.Length))
break;
}
}
return DecompressedData;
}
//No data to decompress
return Data;
}