示例#1
0
        public void ReadTwoMSB()
        {
            var buff = new byte[1] {
                0x40
            };
            var          mem    = new MemoryStream(buff);
            BitReaderMSB reader = new BitReaderMSB(mem);

            reader.GetBits(1);
            var val = reader.GetBits(1);

            Assert.AreEqual(1, val);
        }
示例#2
0
        public void ReadWriteMSB()
        {
            for (int n = 4; n <= 10; n++)
            {
                var max = (1 << n) - 1;

                byte[] buff = new byte[max * n / 8 + 2];
                var    mem  = new MemoryStream(buff);

                BitWriterMSB writer = new BitWriterMSB(mem);
                for (int i = 0; i <= max; i++)
                {
                    writer.WriteBits(i, n);
                }
                writer.Flush();

                mem.Seek(0, SeekOrigin.Begin);
                BitReaderMSB reader = new BitReaderMSB(mem);
                for (int i = 0; i <= max; i++)
                {
                    var val = reader.GetBits((ushort)n);
                    Assert.AreEqual(i, val);
                }
            }
        }
示例#3
0
        public void ReadNineMSB()
        {
            var          buff   = new byte[] { 0, 0x80 };
            var          mem    = new MemoryStream(buff);
            BitReaderMSB reader = new BitReaderMSB(mem);
            var          val    = reader.GetBits(9);

            Assert.AreEqual(1, val);
        }
示例#4
0
        protected short Getc2()
        {
            int   i = 0;
            short next;

            while (_nodes[i + 1] > 0)
            {
                if (reader.GetBits(1) > 0)
                {
                    next = (short)(_nodes[i + 1] & 0x0F); // use lower 4 bits
                    if (next == 0)
                    {
                        return((short)(reader.GetByte() | 0x100));
                    }
                }
                else
                {
                    next = (short)(_nodes[i + 1] >> 4); // use higher 4 bits
                }
                i += next << 1;
            }
            return((short)(_nodes[i] | (_nodes[i + 1] << 8)));
        }
示例#5
0
        protected override void GoUnpack()
        {
            var reader = new BitReaderMSB(_stream);

            BitReaderMSB.DEBUG = DEBUG;

            ushort numbits  = 9;
            ushort curtoken = 0x102;
            ushort endtoken = 0x1ff;

            byte   lastchar = 0;
            ushort lastbits = 0;

            bool   firstChar = true;
            ushort bitstring;

            while (!IsFinished())
            {
                if (DEBUG)
                {
                    Console.WriteLine();
                }
                if (DEBUG)
                {
                    Console.WriteLine(_dwWrote);
                }

                bitstring = (ushort)reader.GetBits(numbits);

                if (bitstring == 0x101) // found end-of-data signal
                {
                    if (DEBUG)
                    {
                        Console.WriteLine("End");
                    }
                    return;
                }

                if (bitstring == 0x100) // start-over signal
                {
                    if (DEBUG)
                    {
                        Console.WriteLine("Reset");
                    }
                    numbits   = 9;
                    curtoken  = 0x102;
                    endtoken  = 0x1ff;
                    firstChar = true;
                    continue;
                }

                if (firstChar)
                {
                    if (DEBUG)
                    {
                        Console.WriteLine("First");
                    }
                    lastchar  = (byte)(bitstring & 0xff);
                    lastbits  = bitstring;
                    firstChar = false;

                    PutByte(lastchar);
                }
                else
                {
                    ushort token = bitstring;
                    if (token == curtoken) // index past current point
                    {
                        if (DEBUG)
                        {
                            Console.WriteLine($"Push lastchar {lastchar:X02}");
                        }
                        token = lastbits;
                        stack.Push(lastchar);
                    }

                    while ((token > 0xff) && (token < 0x1004)) // follow links back in data
                    {
                        if (DEBUG)
                        {
                            Console.WriteLine($"Push token[{token:X4}] {tokens[token]}");
                        }
                        stack.Push(tokens[token].data);
                        token = tokens[token].next;
                    }
                    lastchar = (byte)(token & 0xff);
                    stack.Push(lastchar);

                    WriteStack();

                    // put token into record
                    if (curtoken <= endtoken)
                    {
                        tokens[curtoken].data = lastchar;
                        tokens[curtoken].next = lastbits;
                        if (DEBUG)
                        {
                            Console.WriteLine($"tokens[{curtoken:X4}] = {tokens[curtoken]}");
                        }
                        curtoken++;
                        if (curtoken == endtoken && numbits < 12)
                        {
                            numbits++;
                            endtoken = (ushort)((endtoken << 1) + 1);
                            if (DEBUG)
                            {
                                Console.WriteLine($"New endtoken = {endtoken:X4} numbits = {numbits}");
                            }
                        }
                    }
                    lastbits = bitstring;
                }
            }
        }