예제 #1
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);
        }
예제 #2
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);
        }
예제 #3
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));
        }
예제 #4
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 _));
        }
예제 #5
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);
            });
        }
예제 #6
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));
        }
예제 #7
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>(),
            });
        }
예제 #8
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>()));
            }
        }