Example #1
0
        protected override Variant GenerateInternalValue()
        {
            Variant value;

            // 1. Default value

            if (_mutatedValue == null)
            {
                var stream = new BitStreamList()
                {
                    Name = fullName
                };
                foreach (var child in this)
                {
                    var val = child.Value;
                    val.Name = child.fullName;
                    stream.Add(val);
                }

                value = new Variant(stream);
            }
            else
            {
                value = MutatedValue;
            }

            // 2. Relations

            if (_mutatedValue != null && mutationFlags.HasFlag(MutateOverride.Relations))
            {
                return(MutatedValue);
            }

            foreach (var r in relations.From <Relation>())
            {
                // CalculateFromValue can return null sometimes
                // when mutations mess up the relation.
                // In that case use the exsiting value for this element.

                var relationValue = r.CalculateFromValue();
                if (relationValue != null)
                {
                    value = relationValue;
                }
            }

            // 3. Fixup

            if (_mutatedValue != null && mutationFlags.HasFlag(MutateOverride.Fixup))
            {
                return(MutatedValue);
            }

            if (_fixup != null)
            {
                value = _fixup.fixup(this);
            }

            return(value);
        }
Example #2
0
        protected override Variant fixupImpl()
        {
            var ref1 = elements["ref1"];
            var ref2 = elements["ref2"];

            var data = new BitStreamList();

            data.Add(ref1.Value);
            data.Add(ref2.Value);
            data.Seek(0, System.IO.SeekOrigin.Begin);

            CRCTool crcTool = new CRCTool();

            crcTool.Init(type);

            return(new Variant((uint)crcTool.crctablefast(data)));
        }
Example #3
0
        public void TestUnalignedRead()
        {
            BitStreamList lst = new BitStreamList();
            var           bs1 = new BitStream();

            bs1.WriteBits(0xaa, 12);
            lst.Add(bs1);
            var bs2a = new BitStream();

            bs2a.WriteBits(0x7, 3);
            lst.Add(bs2a);
            var bs2b = new BitStream();

            bs2b.WriteBits(0x7, 4);
            lst.Add(bs2b);
            var bs3 = new BitStream();

            bs3.WriteBits(0x10a, 18);
            lst.Add(bs3);

            lst.SeekBits(10, SeekOrigin.Begin);
            Assert.AreEqual(37, lst.LengthBits);

            var buf = new byte[4];
            int len = lst.Read(buf, 0, buf.Length);

            Assert.AreEqual(3, len);             // 2bit + 7bit + 18bit = 27bit = 3 bytes
            Assert.AreEqual(37, lst.LengthBits);
            Assert.AreEqual(34, lst.PositionBits);
            Assert.AreEqual(4, lst.Length);
            Assert.AreEqual(4, lst.Position);

            // 00 00 10 10 10 10 11 10 11 10 00 00 00 00 10 00 01 01 0
            //                10 11 10 11 10 00 00 00 00 10 00 01
            //                0xbb        0x80        0x21
            Assert.AreEqual(new byte[] { 0xbb, 0x80, 0x21, 0x00 }, buf);

            lst.SeekBits(0, SeekOrigin.Begin);
            int   bits = (int)lst[0].LengthBits + 1;
            ulong tmp;
            int   cnt = lst.ReadBits(out tmp, bits);

            Assert.AreEqual(bits, cnt);
        }
Example #4
0
        /// <summary>
        /// Ensures that the data stream length is in full bytes
        /// by padding with up to 7 bits of '0'
        /// </summary>
        /// <param name="data">Stream of bits</param>
        /// <returns>Stream of bits padded to a byte boundary</returns>
        protected static BitwiseStream PadBits(BitwiseStream data)
        {
            long lengthBits = data.LengthBits;

            int extra = 8 - (int)(lengthBits % 8);

            if (extra != 8)
            {
                BitStreamList lst = new BitStreamList();
                lst.Add(data);

                BitStream pad = new BitStream();
                pad.WriteBits(0, extra);
                lst.Add(pad);
                lengthBits += extra;

                data = lst;
            }

            return(data);
        }
Example #5
0
        public void TestList()
        {
            BitStreamList lst = new BitStreamList();

            lst.Add(new BitStream(new MemoryStream(Encoding.ASCII.GetBytes("Hello"))));
            lst.Add(new BitStream(new MemoryStream(Encoding.ASCII.GetBytes("World"))));

            Assert.AreEqual(0, lst.Position);
            Assert.AreEqual(0, lst.PositionBits);

            Assert.AreEqual(10, lst.Length);
            Assert.AreEqual(80, lst.LengthBits);

            long ret = lst.SeekBits(12, SeekOrigin.Begin);

            Assert.AreEqual(12, ret);
            Assert.AreEqual(1, lst.Position);
            Assert.AreEqual(12, lst.PositionBits);

            ret = lst.Seek(2, SeekOrigin.Current);
            Assert.AreEqual(3, ret);
            Assert.AreEqual(3, lst.Position);
            Assert.AreEqual(28, lst.PositionBits);

            ret = lst.SeekBits(-12, SeekOrigin.End);
            Assert.AreEqual(68, ret);
            Assert.AreEqual(8, lst.Position);
            Assert.AreEqual(68, lst.PositionBits);

            ulong bits;
            int   b = lst.ReadBits(out bits, 15);

            Assert.AreEqual(12, b);
            Assert.AreEqual(0xc64, bits);
            Assert.AreEqual(lst.Length, lst.Position);
            Assert.AreEqual(lst.LengthBits, lst.PositionBits);

            lst.Add(new BitStream(new MemoryStream(Encoding.ASCII.GetBytes("!"))));
            Assert.AreEqual(10, lst.Position);
            Assert.AreEqual(80, lst.PositionBits);
            Assert.AreEqual(11, lst.Length);
            Assert.AreEqual(88, lst.LengthBits);

            ret = lst.SeekBits(-12, SeekOrigin.End);
            Assert.AreEqual(76, ret);
            Assert.AreEqual(9, lst.Position);
            Assert.AreEqual(76, lst.PositionBits);

            b = lst.ReadBits(out bits, 15);
            Assert.AreEqual(12, b);
            Assert.AreEqual(lst.Length, lst.Position);
            Assert.AreEqual(lst.LengthBits, lst.PositionBits);
            Assert.AreEqual(0x421, bits);

            var buf = new byte[3];

            b = lst.Read(buf, 0, buf.Length);
            Assert.AreEqual(0, b);
            Assert.AreEqual(lst.Length, lst.Position);
            Assert.AreEqual(lst.LengthBits, lst.PositionBits);

            lst.Seek(-3, SeekOrigin.End);
            b = lst.Read(buf, 0, buf.Length);
            Assert.AreEqual(3, b);
            Assert.AreEqual(Encoding.ASCII.GetBytes("ld!"), buf);
            Assert.AreEqual(lst.Length, lst.Position);
            Assert.AreEqual(lst.LengthBits, lst.PositionBits);
        }
Example #6
0
        private static BitwiseStream GrowByBits(BitwiseStream data, long growBy)
        {
            var dataLen = data.LengthBits;
            var tgtLen  = dataLen + growBy;

            if (tgtLen <= 0)
            {
                // Return empty if size is negative
                data = new BitStream();
            }
            else if (data.LengthBits == 0)
            {
                // If objOf is a block, data is a BitStreamList
                data = new BitStream();

                // Fill with 'A' if we don't have any data
                while (data.LengthBits < growBy)
                {
                    data.WriteByte((byte)'A');
                }

                // Truncate to the correct bit length
                data.SetLengthBits(growBy);
            }
            else
            {
                // Loop data over and over until we get to our target length

                var lst = new BitStreamList();

                while (tgtLen > dataLen)
                {
                    lst.Add(data);
                    tgtLen -= dataLen;
                }

                var dst = new BitStream();

                data.Seek(0, System.IO.SeekOrigin.Begin);

                while (tgtLen > 0)
                {
                    ulong bits;
                    int   len = data.ReadBits(out bits, (int)Math.Min(tgtLen, 64));

                    if (len == 0)
                    {
                        data.Seek(0, System.IO.SeekOrigin.Begin);
                    }
                    else
                    {
                        dst.WriteBits(bits, len);
                    }

                    tgtLen -= len;
                }

                lst.Add(dst);

                data = lst;
            }
            return(data);
        }
Example #7
0
        private static BitwiseStream GrowByBytes(BitwiseStream data, long growBy)
        {
            var dataLen = data.Length;
            var tgtLen  = dataLen + growBy;

            if (tgtLen <= 0)
            {
                // Return empty if size is negative
                data = new BitStream();
            }
            else if (data.Length == 0)
            {
                // If objOf is a block, data is a BitStreamList
                data = new BitStream();

                // Fill with 'A' if we don't have any data
                while (--tgtLen > 0)
                {
                    data.WriteByte((byte)'A');
                }
            }
            else
            {
                // Loop data over and over until we get to our target length

                var lst = new BitStreamList();

                while (tgtLen > dataLen)
                {
                    lst.Add(data);
                    tgtLen -= dataLen;
                }

                var buf = new byte[BitwiseStream.BlockCopySize];
                var dst = new BitStream();

                data.Seek(0, System.IO.SeekOrigin.Begin);

                while (tgtLen > 0)
                {
                    int len = (int)Math.Min(tgtLen, buf.Length);
                    len = data.Read(buf, 0, len);

                    if (len == 0)
                    {
                        data.Seek(0, System.IO.SeekOrigin.Begin);
                    }
                    else
                    {
                        dst.Write(buf, 0, len);
                    }

                    tgtLen -= len;
                }

                lst.Add(dst);

                data = lst;
            }
            return(data);
        }