private static LinkedNode Decode(BitStream Input, LinkedNode Head) { LinkedNode node = Head; while (node.Child0 != null) { int num = Input.ReadBits(1); if (num == -1) { throw new Exception("Unexpected end of file"); } node = (num == 0) ? node.Child0 : node.Child1; } return node; }
public static byte[] Decompress(Stream Data) { int decompressedValue; int index = Data.ReadByte(); if (index == 0) { throw new NotImplementedException("Compression type 0 is not currently supported"); } LinkedNode tail = BuildList(sPrime[index]); LinkedNode head = BuildTree(tail); MemoryStream stream = new MemoryStream(); BitStream input = new BitStream(Data); do { decompressedValue = Decode(input, head).DecompressedValue; switch (decompressedValue) { case 0x100: break; case 0x101: { int decomp = input.ReadBits(8); stream.WriteByte((byte) decomp); tail = InsertNode(tail, decomp); break; } default: stream.WriteByte((byte) decompressedValue); break; } } while (decompressedValue != 0x100); return stream.ToArray(); }