Example #1
0
        public void GetValTest1()
        {
            int offset = 0;

            byte[] data = new byte[] { 1, 2, 3, 4 };
            BReader.GetValue(typeof(UserSpecUintType), 1, ref offset, ref data);
        }
Example #2
0
        protected IMAGE_EXPORT_DIRECTORY getExports(DWORD virtualAddress)
        {
            var stream = reader.BaseStream;

            DWORD offset = rva2Offset(virtualAddress);

            stream.Seek(offset, SeekOrigin.Begin);

            byte[] data = new byte[IMAGE_EXPORT_DIRECTORY.SIZEOF_EXPORT_DIRECTORY];
            reader.Read(data, 0, data.Length);

            var br = new BReader(data);

            return(new IMAGE_EXPORT_DIRECTORY()
            {
                Characteristics = br.next <DWORD>(),
                TimeDateStamp = br.next <DWORD>(),
                MajorVersion = br.next <WORD>(),
                MinorVersion = br.next <WORD>(),
                Name = br.next <DWORD>(),
                Base = br.next <DWORD>(),
                NumberOfFunctions = br.next <DWORD>(),
                NumberOfNames = br.next <DWORD>(),
                AddressOfFunctions = br.next <DWORD>(),
                AddressOfNames = br.next <DWORD>(),
                AddressOfNameOrdinals = br.next <DWORD>(),
            });
        }
Example #3
0
        protected IMAGE_SECTION_HEADER[] getSectionHeaders(int count)
        {
            /* IMAGE_SECTION_HEADER */
            // https://msdn.microsoft.com/en-us/library/windows/desktop/ms680341.aspx

            if (count < 1)
            {
                return(new IMAGE_SECTION_HEADER[0]);
            }

            byte[] data = new byte[count * IMAGE_SECTION_HEADER.IMAGE_SIZEOF_SECTION_HEADER];
            reader.Read(data, 0, data.Length);

            var br       = new BReader(data);
            var sections = new IMAGE_SECTION_HEADER[count];

            for (int i = 0; i < count; ++i)
            {
                sections[i].Name                 = br.next <BYTE[]>(8);
                sections[i].Misc                 = br.next <DWORD>();
                sections[i].VirtualAddress       = br.next <DWORD>();
                sections[i].SizeOfRawData        = br.next <DWORD>();
                sections[i].PointerToRawData     = br.next <DWORD>();
                sections[i].PointerToRelocations = br.next <DWORD>();
                sections[i].PointerToLinenumbers = br.next <DWORD>();
                sections[i].NumberOfRelocations  = br.next <WORD>();
                sections[i].NumberOfLinenumbers  = br.next <WORD>();
                sections[i].Characteristics      = br.next <DWORD>();
            }

            return(sections);
        }
Example #4
0
        public void TestGetSingleByte()
        {
            BReader br = new BReader(new byte[] { 0x11, 0x22, 0x33, 0x44 });

            br[1].Should().Be(0x22);
            br[2].Should().Be(0x33);
            br.Invoking(_ => _[4]).Should().Throw <ArgumentOutOfRangeException>();
        }
Example #5
0
        public void TestGetSingleBit()
        {
            BReader br = new BReader(new byte[] { 0x20, 0x22, 0x33, 0x44 });

            0.Should().Be(br[0, 4]);
            1.Should().Be(br[0, 5]);
            1.Should().Be(br[3, 2]);
            br.Invoking(_ => _[0, 32]).Should().Throw <ArgumentOutOfRangeException>();
        }
Example #6
0
 public void GetValTest1()
 {
     Assert.Throws <NotSupportedException>(() =>
     {
         int offset  = 0;
         byte[] data = new byte[] { 1, 2, 3, 4 };
         BReader.GetValue(typeof(UserSpecUintType), 1, ref offset, ref data);
     });
 }
Example #7
0
        public void TestReadStringByByteIndex()
        {
            BReader br = new BReader(Encoding.Default.GetBytes("Hello world!"));

            br.ReadStringByByteIndex(0, 5).Should().Be("Hello");
            br.ReadStringByByteIndex(6, 5).Should().Be("world");

            br.Invoking(_ => _.ReadStringByByteIndex(8, 100)).Should().Throw <ArgumentOutOfRangeException>();
            new BReader(Encoding.Default.GetBytes("Hello world!")).ReadStringByByteIndex(6, 5, Endian.SmallEndian).Should().Be("dlrow");
        }
Example #8
0
        public void nextValTest2()
        {
            var br = new BReader(new byte[] {
                5, 0, 0, 0, // Int32 = 5
                31          // SByte = 31
            });

            br.next(typeof(Int32), 4);
            br.next(typeof(Int32), 4);
        }
Example #9
0
        public void TestReadValueByByteIndex()
        {
            BReader br = new BReader(new byte[] { 0x12, 0x34, 0x56, 0x78 });

            br.ReadValueByByteIndex <long>(0, 4).Should().Be(0x12345678);
            br.ReadValueByByteIndex <long>(1, 3).Should().Be(0x345678);
            br.Invoking(_ => _.ReadValueByByteIndex <short>(0, 4)).Should().Throw <ArgumentOutOfRangeException>();
            br.Invoking(_ => _.ReadValueByByteIndex <string>(0, 4)).Should().Throw <NotSupportedException>();
            br.Invoking(_ => _.ReadValueByByteIndex <bool>(0, 4)).Should().Throw <NotSupportedException>();
            br.Invoking(_ => _.ReadValueByByteIndex <Foo>(0, 4)).Should().Throw <NotSupportedException>();
            new BReader(new byte[] { 0x12, 0x34, 0x56, 0x78 }).ReadValueByByteIndex <long>(0, 4, Endian.SmallEndian).Should().Be(0x78563412);
        }
Example #10
0
        public void nextValTest1()
        {
            var br = new BReader(new byte[] {
                5, 0, 0, 0, // Int32 = 5
                7, 0, 0, 0, // Int32 = 7
                31          // SByte = 31
            });

            Assert.AreEqual(5, br.next(typeof(Int32), 4));
            Assert.AreEqual(7, br.next(typeof(Int32), 4));
            Assert.AreEqual(31, br.next(typeof(sbyte), 1));
        }
Example #11
0
        public void nextValTest3()
        {
            var br = new BReader(new byte[] {
                5, 0, 0, 0, // Int32 = 5
            });

            Assert.Equal(5, br.next(typeof(Int32), 4));
            br.reset();
            Assert.Equal(5, br.next(typeof(Int32), 4));

            Assert.False(br.tryNext(typeof(Int32), 4, out _));
        }
Example #12
0
        public void nextValTest2()
        {
            Assert.Throws <IndexOutOfRangeException>(() =>
            {
                var br = new BReader(new byte[] {
                    5, 0, 0, 0, // Int32 = 5
                    31          // SByte = 31
                });

                br.next(typeof(Int32), 4);
                br.next(typeof(Int32), 4);
            });
        }
Example #13
0
        public void TestCreate()
        {
            BReader br = new BReader(new byte[] { 0x55, 0x55, 0x55, 0x55 });

            br.BytesCount.Should().Be(4);
            br.BitsCount.Should().Be(br.BytesCount * 8);
            br = new BReader(new byte[] { 0x00, 0x55, 0x55, 0x55, 0x55 }, 1, 4);
            br.BytesCount.Should().Be(4);
            br.BitsCount.Should().Be(br.BytesCount * 8);
            br = new BReader(new byte[] { 0x00, 0x55, 0x55, 0x55, 0x55 }, 4);
            br.BytesCount.Should().Be(4);
            br.BitsCount.Should().Be(br.BytesCount * 8);
        }
Example #14
0
        public void TestReadStringByBitIndex()
        {
            BReader br = new BReader(Encoding.Default.GetBytes("Hello world!"));

            br.ReadStringByBitIndex(0, 96).Should().Be("Hello world!");
            br.ReadStringByBitIndex(48, 48).Should().Be("world!");
            br.Invoking(_ => _.ReadStringByBitIndex(0, 42)).Should().Throw <FormatException>();
            byte[] bixetBytes = Encoding.Default.GetBytes("bixet");
            br = new BReader(bixetBytes);
            br.ReadStringByBitIndex(0, 40).Should().Be("bixet");
            byte[] bixetComplexBytes = new byte[] { 0x7F, 0b10110001, 0b00110100, 0b10111100, 0b00110010, 0b10111010 };
            br = new BReader(bixetComplexBytes);
            br.ReadStringByBitIndex(7, 40).Should().Be("bixet");
        }
Example #15
0
        public void nextValTest3()
        {
            var br = new BReader(new byte[] {
                5, 0, 0, 0, // Int32 = 5
            });

            Assert.AreEqual(5, br.next(typeof(Int32), 4));
            br.reset();
            Assert.AreEqual(5, br.next(typeof(Int32), 4));

            dynamic value;

            Assert.AreEqual(false, br.tryNext(typeof(Int32), 4, out value));
        }
Example #16
0
        public void TestReadValueByBitIndex()
        {
            BReader br = new BReader(new byte[] { 0x12, 0x34 });

            br.ReadValueByBitIndex <int>(0, 16).Should().Be(0x3412);
            br.ReadValueByBitIndex <long>(4, 8).Should().Be(0x41);
            br.ReadValueByBitIndex <bool>(13, 1).Should().Be(true);
            br.ReadValueByBitIndex <byte>(9, 5).Should().Be(0x1A);
            br.ReadValueByBitIndex <byte>(1, 1, 5).Should().Be(0x1A);
            br.Invoking(_ => _.ReadValueByBitIndex <byte>(0, 9)).Should().Throw <ArgumentOutOfRangeException>();
            br.Invoking(_ => _.ReadValueByBitIndex <string>(0, 4)).Should().Throw <NotSupportedException>();
            br.Invoking(_ => _.ReadValueByBitIndex <Foo>(0, 4)).Should().Throw <NotSupportedException>();
            br = new BReader(new byte[] { 0x12, 0x34 });
            br.ReadValueByBitIndex <int>(0, 16, Endian.BigEndian).Should().Be(0x482C);
            br.ReadValueByBitIndex <byte>(9, 6, Endian.BigEndian).Should().Be(0x16);
        }
Example #17
0
        public void TestFunction()
        {
            byte[]  message = new byte[] { 0x01, 0x12, 0x34, 0x56, 0x78, 0b10101010, 0b11100100, (byte)'S', (byte)'u', (byte)'c', (byte)'c', (byte)'e', (byte)'s', (byte)'s', };
            BReader br      = new BReader(message);

            br.ReadValueByByteIndex <byte>(0, 1).Should().Be(0x01);
            br.ReadValueByByteIndex <int>(1, 4).Should().Be(0x12345678);
            for (int i = 0; i < 8; ++i)
            {
                br.ReadValueByBitIndex <bool>(5, i, 1).Should().Be(i % 2 == 1);
            }
            for (byte i = 0; i < 4; ++i)
            {
                br.ReadValueByBitIndex <byte>(6, 2 * i, 2).Should().Be(i);
            }
            br.ReadStringByByteIndex(7, 7).Should().Be("Success");
        }
Example #18
0
 public void readV2(BReader br)
 {
 }
Example #19
0
 protected void readV2_opacity(BReader br)
 {
 }
Example #20
0
 public virtual void readV2(BReader br)
 {
 }
Example #21
0
        public void complexTest1()
        {
            using (var l = new ConariL(UNLIB_DLL))
            {
                IntPtr ptr1 = l.DLR.get_TSpec <IntPtr>();
                IntPtr ptr2 = l.bind <Func <IntPtr> >("get_TSpec")();

                var    dyn  = l.bind(Dynamic.GetMethodInfo(typeof(IntPtr)), "get_TSpec");
                IntPtr ptr3 = (IntPtr)dyn.dynamic.Invoke(null, new object[0]);

                Assert.AreNotEqual(IntPtr.Zero, ptr1);
                Assert.IsTrue(ptr1 == ptr2 && ptr2 == ptr3);

                /*
                 *  struct TSpec
                 *  {
                 *      BYTE a;
                 *      int b;
                 *      char* name;
                 *  };
                 *
                 *  s->a    = 2;
                 *  s->b    = 4;
                 *  s->name = "Conari";
                 *
                 */
                var TSpecPtr = NativeData
                               ._(ptr1)
                               .t <int, int>("a", "b")
                               .t <IntPtr>("name")
                               .AlignSizeByMax;

                byte[]  bytes  = TSpecPtr.Raw.Values;
                dynamic dlr    = TSpecPtr.Raw.Type;
                var     fields = TSpecPtr.Raw.Type.Fields;

                Assert.AreEqual(3, fields.Count);

                int    expA    = 2;
                int    expB    = 4;
                string expName = "Conari";

                // a
                Assert.AreEqual("a", fields[0].name);
                Assert.AreEqual(NativeData.SizeOf <int>(), fields[0].tsize);
                Assert.AreEqual(typeof(int), fields[0].type);
                Assert.AreEqual(expA, fields[0].value);

                // b
                Assert.AreEqual("b", fields[1].name);
                Assert.AreEqual(NativeData.SizeOf <int>(), fields[1].tsize);
                Assert.AreEqual(typeof(int), fields[1].type);
                Assert.AreEqual(expB, fields[1].value);

                // name
                Assert.AreEqual("name", fields[2].name);
                Assert.AreEqual(IntPtr.Size, fields[2].tsize);
                Assert.AreEqual(typeof(IntPtr), fields[2].type);
                Assert.AreEqual(expName, (CharPtr)fields[2].value);

                // DLR
                Assert.AreEqual(expA, dlr.a);
                Assert.AreEqual(expB, dlr.b);
                Assert.AreEqual(expName, (CharPtr)dlr.name);

                // byte-seq
                var br = new BReader(bytes);
                Assert.AreEqual(expA, br.next <int>(NativeData.SizeOf <int>()));
                Assert.AreEqual(expB, br.next <int>(NativeData.SizeOf <int>()));
                Assert.AreEqual(expName, (CharPtr)br.next <IntPtr>(NativeData.SizeOf <IntPtr>()));
            }
        }