public static MemoryStream Decompress(Stream data) { int comptype = data.ReadByte(); if (comptype == 0) throw new NotImplementedException("Compression type 0 is not currently supported"); LinkedNode tail = BuildList(sPrime[comptype]); LinkedNode head = BuildTree(tail); MemoryStream outputstream = new MemoryStream(); BitStream bitstream = new BitStream(data); int decoded; do { LinkedNode node = Decode(bitstream, head); decoded = node.DecompressedValue; switch(decoded) { case 256: break; case 257: int newvalue = bitstream.ReadBits(8); outputstream.WriteByte((byte)newvalue); tail = InsertNode(tail, newvalue); break; default: outputstream.WriteByte((byte)decoded); break; } } while (decoded != 256); outputstream.Seek(0, SeekOrigin.Begin); return outputstream; }
private static LinkedNode Decode(BitStream input, LinkedNode head) { LinkedNode node = head; while(node.Child0 != null) { int bit = input.ReadBits(1); if (bit == -1) throw new Exception("Unexpected end of file"); node = bit == 0 ? node.Child0 : node.Child1; } return node; }
public PKLibDecompress(Stream input) { _bitstream = new BitStream(input); _compressionType = (CompressionType)input.ReadByte(); if (_compressionType != CompressionType.Binary && _compressionType != CompressionType.Ascii) throw new InvalidDataException("Invalid compression type: " + _compressionType); _dictSizeBits = input.ReadByte(); // This is 6 in test cases if(4 > _dictSizeBits || _dictSizeBits > 6) throw new InvalidDataException("Invalid dictionary size: " + _dictSizeBits); }