public byte[] Decode(Stream stream) { using (var ms = new MemoryStream()) { var br = new BitReader(stream); int value; int i = 0; int prevValue = -1; byte lastChar = 0; if (br.BitsRead + lookup.CodeLen <= br.BitsLength) { value = br.ReadBits(lookup.CodeLen); lastChar = lookup.OutputText(lastChar, value, ms); prevValue = value; } while (br.BitsRead < br.BitsLength) { if (lookup.Count >= 0xFFF) { lookup.Reset(); if (br.BitsRead + lookup.CodeLen <= br.BitsLength) { value = br.ReadBits(lookup.CodeLen); lastChar = lookup.OutputText(lastChar, value, ms); prevValue = value; } } if (br.BitsRead + lookup.CodeLen <= br.BitsLength) { lookup.EnsureCodeLen(); value = br.ReadBits(lookup.CodeLen); } else { break; } if (value != lookup.Count) { lastChar = lookup.OutputText(lastChar, value, ms); lookup.AppendChar(lastChar, prevValue); } else { lookup.AppendChar(lastChar, prevValue); lastChar = lookup.OutputText(lastChar, value, ms); } prevValue = value; } return(ms.ToArray()); } }