Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 102 additions & 86 deletions src/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ public BZip2InputStream(Stream stream)
bsBuff = 0;
Initialize();
InitBlock();
SetupBlock();
if (!streamEnd)
{
SetupBlock();
}
}

/// <summary>
Expand Down Expand Up @@ -351,7 +354,7 @@ private void Initialize()
if (magic1 != 'B' || magic2 != 'Z' || magic3 != 'h' || magic4 < '1' || magic4 > '9')
{
streamEnd = true;
return;
throw new BZip2Exception("BZip2 input stream bad header");
}

SetDecompressStructureSizes(magic4 - '0');
Expand Down Expand Up @@ -469,7 +472,7 @@ private int BsGetInt32()
return result;
}

private void RecvDecodingTables()
private int RecvDecodingTables()
{
char[][] len = new char[BZip2Constants.GroupCount][];
for (int i = 0; i < BZip2Constants.GroupCount; ++i)
Expand Down Expand Up @@ -504,18 +507,35 @@ private void RecvDecodingTables()
}

MakeMaps();
if (nInUse == 0)
{
throw new BZip2Exception("Bzip data error");
}
int alphaSize = nInUse + 2;

//--- Now the selectors ---
int nGroups = BsR(3);
if (nGroups < 2 || nGroups > BZip2Constants.GroupCount)
{
throw new BZip2Exception("Bzip data error");
}

int nSelectors = BsR(15);
if (nSelectors < 1 || nSelectors > BZip2Constants.MaximumSelectors)
{
throw new BZip2Exception("Bzip data error");
}

for (int i = 0; i < nSelectors; i++)
{
int j = 0;
while (BsR(1) == 1)
{
j++;
if (j >= nGroups)
{
throw new BZip2Exception("Bzip data error");
}
}
selectorMtf[i] = (byte)j;
}
Expand Down Expand Up @@ -546,8 +566,16 @@ private void RecvDecodingTables()
int curr = BsR(5);
for (int i = 0; i < alphaSize; i++)
{
while (BsR(1) == 1)
while (true)
{
if (curr < 1 || curr > 20)
{
throw new BZip2Exception("Bzip data error");
}
if (BsR(1) == 0)
{
break;
}
if (BsR(1) == 0)
{
curr++;
Expand All @@ -574,6 +602,8 @@ private void RecvDecodingTables()
HbCreateDecodeTables(limit[t], baseArray[t], perm[t], len[t], minLen, maxLen, alphaSize);
minLens[t] = minLen;
}

return nSelectors;
}

private void GetAndMoveToFrontDecode()
Expand All @@ -584,11 +614,52 @@ private void GetAndMoveToFrontDecode()
int limitLast = BZip2Constants.BaseBlockSize * blockSize100k;
origPtr = BsGetIntVS(24);

RecvDecodingTables();
int nSelectors = RecvDecodingTables();
int alphaSize = nInUse + 2;
int EOB = nInUse + 1;
int groupNo = -1;
int groupPos = 0;

int DecodeNextSym()
{
if (groupPos == 0)
{
groupNo++;
if (groupNo >= nSelectors)
{
throw new BZip2Exception("Bzip data error");
}
groupPos = BZip2Constants.GroupSize;
}

groupPos--;
int zt = selector[groupNo];
int zn = minLens[zt];
int zvec = BsR(zn);

while (zvec > limit[zt][zn])
{
if (zn >= 20)
{ // the longest code
throw new BZip2Exception("Bzip data error");
}
zn++;
while (bsLive < 1)
{
FillBuffer();
}
int zj = (bsBuff >> (bsLive - 1)) & 1;
bsLive--;
zvec = (zvec << 1) | zj;
}
int idx = zvec - baseArray[zt][zn];
if (idx < 0 || idx >= alphaSize)
{
throw new BZip2Exception("Bzip data error");
}
return perm[zt][idx];
}

/*--
Setting up the unzftab entries here is not strictly
necessary, but it does save having to do it later
Expand All @@ -607,38 +678,7 @@ cache misses.

last = -1;

if (groupPos == 0)
{
groupNo++;
groupPos = BZip2Constants.GroupSize;
}

groupPos--;
int zt = selector[groupNo];
int zn = minLens[zt];
int zvec = BsR(zn);
int zj;

while (zvec > limit[zt][zn])
{
if (zn > 20)
{ // the longest code
throw new BZip2Exception("Bzip data error");
}
zn++;
while (bsLive < 1)
{
FillBuffer();
}
zj = (bsBuff >> (bsLive - 1)) & 1;
bsLive--;
zvec = (zvec << 1) | zj;
}
if (zvec - baseArray[zt][zn] < 0 || zvec - baseArray[zt][zn] >= BZip2Constants.MaximumAlphaSize)
{
throw new BZip2Exception("Bzip data error");
}
nextSym = perm[zt][zvec - baseArray[zt][zn]];
nextSym = DecodeNextSym();

while (true)
{
Expand All @@ -662,35 +702,21 @@ cache misses.
s += (1 + 1) * n;
}

n <<= 1;

if (groupPos == 0)
if (s >= limitLast)
{
groupNo++;
groupPos = BZip2Constants.GroupSize;
BlockOverrun();
}

groupPos--;

zt = selector[groupNo];
zn = minLens[zt];
zvec = BsR(zn);

while (zvec > limit[zt][zn])
{
zn++;
while (bsLive < 1)
{
FillBuffer();
}
zj = (bsBuff >> (bsLive - 1)) & 1;
bsLive--;
zvec = (zvec << 1) | zj;
}
nextSym = perm[zt][zvec - baseArray[zt][zn]];
n <<= 1;
nextSym = DecodeNextSym();
} while (nextSym == BZip2Constants.RunA || nextSym == BZip2Constants.RunB);

s++;
if (last + s >= limitLast)
{
BlockOverrun();
}

byte ch = seqToUnseq[yy[0]];
unzftab[ch] += s;

Expand All @@ -701,10 +727,6 @@ cache misses.
s--;
}

if (last >= limitLast)
{
BlockOverrun();
}
continue;
}
else
Expand Down Expand Up @@ -742,35 +764,24 @@ cache misses.

yy[0] = tmp;

if (groupPos == 0)
{
groupNo++;
groupPos = BZip2Constants.GroupSize;
}

groupPos--;
zt = selector[groupNo];
zn = minLens[zt];
zvec = BsR(zn);
while (zvec > limit[zt][zn])
{
zn++;
while (bsLive < 1)
{
FillBuffer();
}
zj = (bsBuff >> (bsLive - 1)) & 1;
bsLive--;
zvec = (zvec << 1) | zj;
}
nextSym = perm[zt][zvec - baseArray[zt][zn]];
nextSym = DecodeNextSym();
continue;
}
}
}

private void SetupBlock()
{
if (streamEnd || tt == null || ll8 == null)
{
return;
}

if (origPtr < 0 || origPtr > last || origPtr >= tt.Length)
{
throw new BZip2Exception("Bzip data error");
}

int[] cftab = new int[257];

cftab[0] = 0;
Expand Down Expand Up @@ -1001,6 +1012,11 @@ private static void CrcError()

private static void HbCreateDecodeTables(int[] limit, int[] baseArray, int[] perm, char[] length, int minLen, int maxLen, int alphaSize)
{
if (minLen < 1 || maxLen >= BZip2Constants.MaximumCodeLength || minLen > maxLen || alphaSize > BZip2Constants.MaximumAlphaSize)
{
throw new BZip2Exception("Bzip data error");
}

int pp = 0;

for (int i = minLen; i <= maxLen; ++i)
Expand Down
24 changes: 24 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/BZip2/Bzip2Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,30 @@ public void CreateEmptyArchive()
}
}

[Test]
[Category("BZip2")]
[TestCase("425a6839314159265359c1c08044a00030cd00c346299717724538e2000001410000100244a00030cd00c3462997177245385090c1c086e2", TestName = "MalformedInput_GetAndMoveToFrontDecode")]
[TestCase("425a6839314159265359c1c080e20003e8410000100244a000303200c3462997177245385090c1c080e2", TestName = "MalformedInput_HbCreateDecodeTables")]
[TestCase("425a6839314159265359c1c080e2a90001410000100244a00030cd00c3462997177245384290c1c080e2", TestName = "MalformedInput_SetupBlock_OrigPtr")]
[TestCase("425a6839314159265359c1c080e2000001410000100244a000bfcd00c346299717724538500010c080e2", TestName = "MalformedInput_RecvDecodingTables")]
[TestCase("425a68c6314159265359c1c080e2000001410000100244a00030cd00c3672997177245385090c1c080e2", TestName = "MalformedInput_InvalidHeaderBlockSize")]
public void MalformedInputThrowsBZip2Exception(string hexData)
{
var data = new byte[hexData.Length / 2];
for (int i = 0; i < data.Length; i++)
{
data[i] = System.Convert.ToByte(hexData.Substring(i * 2, 2), 16);
}

Assert.Throws<BZip2Exception>(() =>
{
using var stream = new MemoryStream(data);
using var bz2 = new BZip2InputStream(stream);
var buffer = new byte[4096];
while (bz2.Read(buffer, 0, buffer.Length) > 0) { }
});
}

[Test]
[Category("BZip2")]
[Category("Performance")]
Expand Down