Exemplo n.º 1
0
        private static void DecodeInternal(Stream source, Stream destination)
        {
            UInt16BE_NE_H_InputBitStream bitStream = new UInt16BE_NE_H_InputBitStream(source);

            for (;;)
            {
                if (!bitStream.Pop())
                {
                    // Symbolwise match
                    ushort word = BigEndian.Read2(source);
                    BigEndian.Write2(destination, word);
                }
                else
                {
                    // Dictionary match
                    int distance = (0x100 - NeutralEndian.Read1(source)) * 2;
                    int length   = NeutralEndian.Read1(source);
                    if (length == 0)
                    {
                        // End-of-stream marker
                        break;
                    }

                    for (long i = 0; i <= length; i++)
                    {
                        long writePosition = destination.Position;
                        destination.Seek(writePosition - distance, SeekOrigin.Begin);
                        ushort s = BigEndian.Read2(destination);
                        destination.Seek(writePosition, SeekOrigin.Begin);
                        BigEndian.Write2(destination, s);
                    }
                }
            }
        }
Exemplo n.º 2
0
 private void CheckBuffer()
 {
     if (this.remainingBits == 0)
     {
         this.byteBuffer    = BigEndian.Read2(stream);
         this.remainingBits = 16;
     }
 }
Exemplo n.º 3
0
        private static void Decode(Stream input, Stream output)
        {
            DecodingCodeTreeNode codeTree = new DecodingCodeTreeNode();
            ushort numberOfTiles          = BigEndian.Read2(input);
            bool   xorOutput = (numberOfTiles & 0x8000) != 0;

            numberOfTiles &= 0x7fff;
            DecodeHeader(input, output, codeTree);
            DecodeInternal(input, output, codeTree, numberOfTiles, xorOutput);
        }
Exemplo n.º 4
0
        public UInt16BEInputBitStream(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            this.stream        = stream;
            this.remainingBits = 16;
            this.byteBuffer    = BigEndian.Read2(stream);
        }
Exemplo n.º 5
0
        internal static void DecodeModuled(Stream source, Stream destination)
        {
            long decompressedBytes = 0;
            long fullSize          = BigEndian.Read2(source);

            for (; ;)
            {
                DecodeInternal(source, destination, ref decompressedBytes);
                if (decompressedBytes >= fullSize)
                {
                    break;
                }
            }
        }
Exemplo n.º 6
0
        public override ushort Read(int count)
        {
            this.CheckBuffer();
            if (this.remainingBits < count)
            {
                int    delta   = count - this.remainingBits;
                ushort lowBits = (ushort)(this.byteBuffer << delta);
                this.byteBuffer    = BigEndian.Read2(stream);
                this.remainingBits = 16 - delta;
                ushort highBits = (ushort)(this.byteBuffer >> this.remainingBits);
                this.byteBuffer ^= (ushort)(highBits << this.remainingBits);
                return((ushort)(lowBits | highBits));
            }

            this.remainingBits -= count;
            ushort bits = (ushort)(this.byteBuffer >> this.remainingBits);

            this.byteBuffer ^= (ushort)(bits << this.remainingBits);
            return(bits);
        }
Exemplo n.º 7
0
        internal static void Decode(Stream source, Stream destination, Endianness headerEndianness)
        {
            long decompressedBytes = 0;
            long fullSize;

            if (headerEndianness == Endianness.BigEndian)
            {
                fullSize = BigEndian.Read2(source);
            }
            else
            {
                fullSize = LittleEndian.Read2(source);
            }

            for (;;)
            {
                DecodeInternal(source, destination, ref decompressedBytes);
                if (decompressedBytes >= fullSize)
                {
                    break;
                }

                // Skip the padding between modules
                int  b;
                long paddingEnd = (((source.Position - 2) + 0xF) & ~0xF) + 2;
                while (source.Position < paddingEnd)
                {
                    b = source.ReadByte();

                    if (b == -1)
                    {
                        throw new EndOfStreamException();
                    }
                }
            }
        }
Exemplo n.º 8
0
        private static void Decode(Stream input, Stream output, Endianness endianness)
        {
            using (PaddedStream paddedInput = new PaddedStream(input, 2, PaddedStreamMode.Read))
            {
                byte packetLength = NeutralEndian.Read1(paddedInput);
                var  readBitfield = GetBitfieldReader(NeutralEndian.Read1(paddedInput));

                ushort incrementingValue;
                ushort commonValue;
                InputBitStream <ushort> bitStream;
                Action <Stream, ushort> write2;

                if (endianness == Endianness.BigEndian)
                {
                    incrementingValue = BigEndian.Read2(paddedInput);
                    commonValue       = BigEndian.Read2(paddedInput);
                    bitStream         = new UInt16BE_E_L_InputBitStream(paddedInput);
                    write2            = Write2BE;
                }
                else
                {
                    incrementingValue = LittleEndian.Read2(paddedInput);
                    commonValue       = LittleEndian.Read2(paddedInput);
                    bitStream         = new UInt16LE_E_L_InputBitStream(paddedInput);
                    write2            = Write2LE;
                }

                // Loop until the end-of-data marker is found (if it is not found before the end of the stream, UInt8InputBitStream
                // will throw an exception)
                for (; ;)
                {
                    if (bitStream.Get())
                    {
                        int mode  = bitStream.Read(2);
                        int count = bitStream.Read(4);
                        switch (mode)
                        {
                        case 0:
                        case 1:
                        {
                            ushort flags = readBitfield(bitStream);
                            ushort outv  = (ushort)(bitStream.Read(packetLength) | flags);

                            do
                            {
                                write2(output, outv);
                                outv += (ushort)mode;
                            } while (--count >= 0);
                        }

                        break;

                        case 2:
                            mode = -1;
                            goto case 0;

                        case 3:
                        {
                            // End of compressed data
                            if (count == 0xf)
                            {
                                return;
                            }

                            do
                            {
                                ushort flags = readBitfield(bitStream);
                                ushort outv  = bitStream.Read(packetLength);
                                write2(output, (ushort)(outv | flags));
                            } while (--count >= 0);
                        }

                        break;
                        }
                    }
                    else
                    {
                        bool mode  = bitStream.Get();
                        int  count = bitStream.Read(4);
                        if (mode)
                        {
                            do
                            {
                                write2(output, commonValue);
                            } while (--count >= 0);
                        }
                        else
                        {
                            do
                            {
                                write2(output, incrementingValue++);
                            } while (--count >= 0);
                        }
                    }
                }
            }
        }