in src/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs [472:577]
private void RecvDecodingTables()
{
char[][] len = new char[BZip2Constants.GroupCount][];
for (int i = 0; i < BZip2Constants.GroupCount; ++i)
{
len[i] = new char[BZip2Constants.MaximumAlphaSize];
}
bool[] inUse16 = new bool[16];
//--- Receive the mapping table ---
for (int i = 0; i < 16; i++)
{
inUse16[i] = (BsR(1) == 1);
}
for (int i = 0; i < 16; i++)
{
if (inUse16[i])
{
for (int j = 0; j < 16; j++)
{
inUse[i * 16 + j] = (BsR(1) == 1);
}
}
else
{
for (int j = 0; j < 16; j++)
{
inUse[i * 16 + j] = false;
}
}
}
MakeMaps();
int alphaSize = nInUse + 2;
//--- Now the selectors ---
int nGroups = BsR(3);
int nSelectors = BsR(15);
for (int i = 0; i < nSelectors; i++)
{
int j = 0;
while (BsR(1) == 1)
{
j++;
}
selectorMtf[i] = (byte)j;
}
//--- Undo the MTF values for the selectors. ---
byte[] pos = new byte[BZip2Constants.GroupCount];
for (int v = 0; v < nGroups; v++)
{
pos[v] = (byte)v;
}
for (int i = 0; i < nSelectors; i++)
{
int v = selectorMtf[i];
byte tmp = pos[v];
while (v > 0)
{
pos[v] = pos[v - 1];
v--;
}
pos[0] = tmp;
selector[i] = tmp;
}
//--- Now the coding tables ---
for (int t = 0; t < nGroups; t++)
{
int curr = BsR(5);
for (int i = 0; i < alphaSize; i++)
{
while (BsR(1) == 1)
{
if (BsR(1) == 0)
{
curr++;
}
else
{
curr--;
}
}
len[t][i] = (char)curr;
}
}
//--- Create the Huffman decoding tables ---
for (int t = 0; t < nGroups; t++)
{
int minLen = 32;
int maxLen = 0;
for (int i = 0; i < alphaSize; i++)
{
maxLen = Math.Max(maxLen, len[t][i]);
minLen = Math.Min(minLen, len[t][i]);
}
HbCreateDecodeTables(limit[t], baseArray[t], perm[t], len[t], minLen, maxLen, alphaSize);
minLens[t] = minLen;
}
}