in src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs [42:120]
private IEnumerable<bool> CreateStateMachine()
{
// Read initial code length counts from header
while (!input.TryGetBits(5, ref litLenCodeCount, 257)) yield return false;
while (!input.TryGetBits(5, ref distanceCodeCount, 1)) yield return false;
while (!input.TryGetBits(4, ref metaCodeCount, 4)) yield return false;
var dataCodeCount = litLenCodeCount + distanceCodeCount;
if (litLenCodeCount > LITLEN_MAX) throw new ValueOutOfRangeException(nameof(litLenCodeCount));
if (distanceCodeCount > DIST_MAX) throw new ValueOutOfRangeException(nameof(distanceCodeCount));
if (metaCodeCount > META_MAX) throw new ValueOutOfRangeException(nameof(metaCodeCount));
// Load code lengths for the meta tree from the header bits
for (int i = 0; i < metaCodeCount; i++)
{
while (!input.TryGetBits(3, ref codeLengths, MetaCodeLengthIndex[i])) yield return false;
}
var metaCodeTree = new InflaterHuffmanTree(codeLengths);
// Decompress the meta tree symbols into the data table code lengths
int index = 0;
while (index < dataCodeCount)
{
byte codeLength;
int symbol;
while ((symbol = metaCodeTree.GetSymbol(input)) < 0) yield return false;
if (symbol < 16)
{
// append literal code length
codeLengths[index++] = (byte)symbol;
}
else
{
int repeatCount = 0;
if (symbol == 16) // Repeat last code length 3..6 times
{
if (index == 0)
throw new StreamDecodingException("Cannot repeat previous code length when no other code length has been read");
codeLength = codeLengths[index - 1];
// 2 bits + 3, [3..6]
while (!input.TryGetBits(2, ref repeatCount, 3)) yield return false;
}
else if (symbol == 17) // Repeat zero 3..10 times
{
codeLength = 0;
// 3 bits + 3, [3..10]
while (!input.TryGetBits(3, ref repeatCount, 3)) yield return false;
}
else // (symbol == 18), Repeat zero 11..138 times
{
codeLength = 0;
// 7 bits + 11, [11..138]
while (!input.TryGetBits(7, ref repeatCount, 11)) yield return false;
}
if (index + repeatCount > dataCodeCount)
throw new StreamDecodingException("Cannot repeat code lengths past total number of data code lengths");
while (repeatCount-- > 0)
codeLengths[index++] = codeLength;
}
}
if (codeLengths[256] == 0)
throw new StreamDecodingException("Inflater dynamic header end-of-block code missing");
litLenTree = new InflaterHuffmanTree(new ArraySegment<byte>(codeLengths, 0, litLenCodeCount));
distTree = new InflaterHuffmanTree(new ArraySegment<byte>(codeLengths, litLenCodeCount, distanceCodeCount));
yield return true;
}