Esempio n. 1
0
        protected override Variant GenerateInternalValue()
        {
            BitStream bits = new BitStream();

            bits.BigEndian();

            int shift;

            if (_isLittleEndian && lengthAsBits < 64)
            {
                // Expand to 64 bits and skip remaining bits at the beginning
                shift = 64 - (int)lengthAsBits;
                bits.WriteBits(0, 64);
            }
            else
            {
                // Expand to 'size' bits
                shift = 0;
                bits.WriteBits(0, (int)lengthAsBits);
            }

            foreach (DataElement child in this)
            {
                if (child is Flag)
                {
                    bits.SeekBits(((Flag)child).position + shift, System.IO.SeekOrigin.Begin);
                    bits.Write(child.Value, child);
                }
                else
                {
                    throw new ApplicationException("Flag has child thats not a flag!");
                }
            }

            if (!_isLittleEndian)
            {
                return(new Variant(bits));
            }

            bits.SeekBits(0, System.IO.SeekOrigin.Begin);
            ulong val   = bits.ReadUInt64();
            ulong final = LittleBitWriter.GetBits(val, (int)lengthAsBits);

            BitStream bs = new BitStream();

            bs.WriteBits(final, (int)lengthAsBits);
            return(new Variant(bs));
        }
Esempio n. 2
0
        public void CrackTokenBlob()
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Peach>\n" +
                         "	<DataModel name=\"TheDataModel\">"+
                         "		<Blob name=\"Blob1\" value=\"300\" token=\"true\" />"+
                         "		<String name=\"String1\" value=\"Foo Bar\" />"+
                         "	</DataModel>"+
                         "</Peach>";
            {
                // Positive test

                PitParser parser = new PitParser();
                Dom.Dom   dom    = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

                BitStream data = new BitStream();
                //data.LittleEndian();
                data.BigEndian();
                data.WriteBytes(ASCIIEncoding.ASCII.GetBytes("300"));
                data.WriteBytes(ASCIIEncoding.ASCII.GetBytes("Hello World"));
                data.SeekBits(0, SeekOrigin.Begin);

                DataCracker cracker = new DataCracker();
                cracker.CrackData(dom.dataModels[0], data);

                Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("300"), (byte[])dom.dataModels[0][0].DefaultValue);
                Assert.AreEqual("Hello World", (string)dom.dataModels[0][1].DefaultValue);
            }
            {
                // Negative test

                PitParser parser = new PitParser();
                Dom.Dom   dom    = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

                BitStream data = new BitStream();
                data.LittleEndian();
                data.WriteBytes(ASCIIEncoding.ASCII.GetBytes("200"));
                data.WriteBytes(ASCIIEncoding.ASCII.GetBytes("Hello World"));
                data.SeekBits(0, SeekOrigin.Begin);

                DataCracker  cracker        = new DataCracker();
                TestDelegate myTestDelegate = () => cracker.CrackData(dom.dataModels[0], data);
                Assert.Throws <CrackingFailure>(myTestDelegate);

                Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("300"), (byte[])dom.dataModels[0][0].DefaultValue);
                Assert.AreEqual("Foo Bar", (string)dom.dataModels[0][1].DefaultValue);
            }
        }
Esempio n. 3
0
        public void ReadingBites()
        {
            BitStream bs = new BitStream(new byte[] { 0x41, 0x41 });

            //bs.LittleEndian();

            //Assert.AreEqual(1, bs.ReadBit()); // 0
            //Assert.AreEqual(0, bs.ReadBit()); // 1
            //Assert.AreEqual(0, bs.ReadBit()); // 2
            //Assert.AreEqual(0, bs.ReadBit()); // 3
            //Assert.AreEqual(0, bs.ReadBit()); // 4
            //Assert.AreEqual(0, bs.ReadBit()); // 5
            //Assert.AreEqual(1, bs.ReadBit()); // 6
            //Assert.AreEqual(0, bs.ReadBit()); // 7

            //Assert.AreEqual(1, bs.ReadBit()); // 0
            //Assert.AreEqual(0, bs.ReadBit()); // 1
            //Assert.AreEqual(0, bs.ReadBit()); // 2
            //Assert.AreEqual(0, bs.ReadBit()); // 3
            //Assert.AreEqual(0, bs.ReadBit()); // 4
            //Assert.AreEqual(0, bs.ReadBit()); // 5
            //Assert.AreEqual(1, bs.ReadBit()); // 6
            //Assert.AreEqual(0, bs.ReadBit()); // 7

            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            bs.BigEndian();

            Assert.AreEqual(0, bs.ReadBit());             // 0
            Assert.AreEqual(1, bs.ReadBit());             // 1
            Assert.AreEqual(0, bs.ReadBit());             // 2
            Assert.AreEqual(0, bs.ReadBit());             // 3
            Assert.AreEqual(0, bs.ReadBit());             // 4
            Assert.AreEqual(0, bs.ReadBit());             // 5
            Assert.AreEqual(0, bs.ReadBit());             // 6
            Assert.AreEqual(1, bs.ReadBit());             // 7

            Assert.AreEqual(0, bs.ReadBit());             // 0
            Assert.AreEqual(1, bs.ReadBit());             // 1
            Assert.AreEqual(0, bs.ReadBit());             // 2
            Assert.AreEqual(0, bs.ReadBit());             // 3
            Assert.AreEqual(0, bs.ReadBit());             // 4
            Assert.AreEqual(0, bs.ReadBit());             // 5
            Assert.AreEqual(0, bs.ReadBit());             // 6
            Assert.AreEqual(1, bs.ReadBit());             // 7
        }
Esempio n. 4
0
        public void ReadWriteNumbers()
        {
            BitStream bs = new BitStream();

            bs.LittleEndian();

            //Max
            bs.WriteInt8(sbyte.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);

            Console.WriteLine(Byte2String((byte)sbyte.MaxValue));
            foreach (byte b in bs.Value)
            {
                Console.WriteLine(Byte2String(b));
            }
            Console.WriteLine(Byte2String((byte)bs.ReadInt8()));

            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(sbyte.MaxValue, bs.ReadInt8());

            bs.Clear();
            bs.WriteInt16(short.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);

            Console.WriteLine(Short2String(short.MaxValue));
            foreach (byte b in bs.Value)
            {
                Console.Write(Byte2String(b));
            }
            Console.WriteLine("\n" + Short2String(bs.ReadInt16()));

            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(short.MaxValue, bs.ReadInt16());

            bs.Clear();
            bs.WriteInt32(67305985);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);

            Console.WriteLine(Int2String(67305985));
            foreach (byte b in bs.Value)
            {
                Console.Write(Byte2String(b));
            }
            Console.WriteLine("\n" + Int2String(bs.ReadInt32()));

            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(67305985, bs.ReadInt32());

            bs.Clear();
            bs.WriteInt32(Int32.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(Int32.MaxValue, bs.ReadInt32());

            bs.Clear();
            bs.WriteInt64(Int64.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(Int64.MaxValue, bs.ReadInt64());

            bs.Clear();
            bs.WriteUInt8(byte.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(byte.MaxValue, bs.ReadUInt8());

            bs.Clear();
            bs.WriteUInt16(ushort.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(ushort.MaxValue, bs.ReadUInt16());

            bs.Clear();
            bs.WriteUInt32(UInt32.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(UInt32.MaxValue, bs.ReadUInt32());

            bs.Clear();
            bs.WriteUInt64(UInt64.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(UInt64.MaxValue, bs.ReadUInt64());


            //Min
            bs.Clear();
            bs.WriteInt8(sbyte.MinValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(sbyte.MinValue, bs.ReadInt8());

            bs.Clear();
            bs.WriteInt16(short.MinValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);

            Console.WriteLine(Short2String(short.MinValue));
            foreach (byte b in bs.Value)
            {
                Console.Write(Byte2String(b));
            }
            Console.WriteLine("\n" + Short2String(bs.ReadInt16()));

            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(short.MinValue, bs.ReadInt16());

            bs.Clear();
            bs.WriteInt32(Int32.MinValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(Int32.MinValue, bs.ReadInt32());

            bs.Clear();
            bs.WriteInt64(Int64.MinValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(Int64.MinValue, bs.ReadInt64());

            // BIG ENDIAN //////////////////////////////////////////

            bs = new BitStream();
            bs.BigEndian();

            //Max
            bs.WriteInt8(sbyte.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(sbyte.MaxValue, bs.ReadInt8());

            bs.Clear();
            bs.WriteInt16(short.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(short.MaxValue, bs.ReadInt16());

            bs.Clear();
            bs.WriteInt32(67305985);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(67305985, bs.ReadInt32());

            bs.Clear();
            bs.WriteInt32(Int32.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(Int32.MaxValue, bs.ReadInt32());

            bs.Clear();
            bs.WriteInt64(Int64.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(Int64.MaxValue, bs.ReadInt64());

            bs.Clear();
            bs.WriteUInt8(byte.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(byte.MaxValue, bs.ReadUInt8());

            bs.Clear();
            bs.WriteUInt16(ushort.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(ushort.MaxValue, bs.ReadUInt16());

            bs.Clear();
            bs.WriteUInt32(UInt32.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(UInt32.MaxValue, bs.ReadUInt32());

            bs.Clear();
            bs.WriteUInt64(UInt64.MaxValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(UInt64.MaxValue, bs.ReadUInt64());

            //Min
            bs.Clear();
            bs.WriteInt8(sbyte.MinValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(sbyte.MinValue, bs.ReadInt8());

            bs.Clear();
            bs.WriteInt16(short.MinValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(short.MinValue, bs.ReadInt16());

            bs.Clear();
            bs.WriteInt32(Int32.MinValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(Int32.MinValue, bs.ReadInt32());

            bs.Clear();
            bs.WriteInt64(Int64.MinValue);
            bs.SeekBits(0, System.IO.SeekOrigin.Begin);
            Assert.AreEqual(Int64.MinValue, bs.ReadInt64());
        }
Esempio n. 5
0
        public void SizedArrayPlacement()
        {
            string xml = @"
<Peach>
	<Defaults>
		<Number endian='big' signed='false'/>
	</Defaults>
			
	<DataModel name='DM'>
		<Number name='NumEntries' size='16'>
			<Relation type='count' of='Entries'/>
		</Number>
		
		<Block name='Entries' minOccurs='1'>
			<Number name='Offset' size='16'>
				<Relation type='offset' of='Data'/>
			</Number>
			<Number name='Size' size='16'>
				<Relation type='size' of='Data'/>
			</Number>
			<String name='Data'>
				<Placement before='Marker'/>
			</String>
		</Block>

		<Block name='Marker'/>
	</DataModel>
</Peach>
";

            PitParser parser = new PitParser();

            Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)));

            BitStream data = new BitStream();

            data.BigEndian();
            data.WriteUInt16(2);
            data.WriteUInt16(14);
            data.WriteUInt16(5);
            data.WriteUInt16(27);
            data.WriteUInt16(7);
            data.WriteBytes(Encoding.ASCII.GetBytes("junk"));
            data.WriteBytes(Encoding.ASCII.GetBytes("peach"));
            data.WriteBytes(Encoding.ASCII.GetBytes("morejunk"));
            data.WriteBytes(Encoding.ASCII.GetBytes("!fuzzer"));
            data.WriteBytes(Encoding.ASCII.GetBytes("evenmorejunk"));
            data.SeekBits(0, SeekOrigin.Begin);

            var expected = data.Value;

            Assert.NotNull(expected);

            DataCracker cracker = new DataCracker();

            cracker.CrackData(dom.dataModels[0], data);

            Assert.AreEqual(5, dom.dataModels[0].Count);
            Assert.AreEqual("peach", (string)dom.dataModels[0][2].DefaultValue);
            Assert.AreEqual("!fuzzer", (string)dom.dataModels[0][3].DefaultValue);
        }