Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        // Return values:
        // 0x000 - 0x0FF : One byte from compressed file.
        // 0x100 - 0x305 : Copy previous block (0x100 = 1 byte)
        // -1            : EOF
        private int DecodeLit()
        {
            switch (_bitstream.ReadBits(1))
            {
            case -1:
                return(-1);

            case 1:
                // The next bits are position in buffers
                int pos = sPosition2[_bitstream.PeekByte()];

                // Skip the bits we just used
                if (_bitstream.ReadBits(sLenBits[pos]) == -1)
                {
                    return(-1);
                }

                int nbits = sExLenBits[pos];
                if (nbits != 0)
                {
                    // TODO: Verify this conversion
                    int val2 = _bitstream.ReadBits(nbits);
                    if (val2 == -1 && (pos + val2 != 0x10e))
                    {
                        return(-1);
                    }

                    pos = sLenBase[pos] + val2;
                }
                return(pos + 0x100);                        // Return number of bytes to repeat

            case 0:
                if (_compressionType == CompressionType.Binary)
                {
                    return(_bitstream.ReadBits(8));
                }

                // TODO: Text mode
                throw new NotImplementedException("Text mode is not yet implemented");

            default:
                return(0);
            }
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        public static MemoryStream 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);
            stream.Seek(0L, SeekOrigin.Begin);
            return(stream);
        }
Ejemplo n.º 5
0
        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;
        }
Ejemplo n.º 6
0
        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;
        }