Пример #1
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);
        }
Пример #2
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();
                    }
                }
            }
        }
Пример #3
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);
                        }
                    }
                }
            }
        }