コード例 #1
0
        public unsafe void ByteArray()
        {
            var array = ImmutableArray.Create(new byte[] { 1, 2, 3, 4 });

            using (var provider = new ByteArrayMemoryProvider(array))
            {
                using (var block = provider.GetMemoryBlock())
                {
                    Assert.Equal(4, block.Size);
                    AssertEx.Equal(provider.Pointer, block.Pointer);
                    AssertEx.Equal(new byte[] { }, block.GetContentUnchecked(0, 0));
                    AssertEx.Equal(new byte[] { 3, 4 }, block.GetContentUnchecked(2, 2));
                    AssertEx.Equal(new byte[] { 1, 2, 3 }, block.GetContentUnchecked(0, 3));
                }

                using (var block = provider.GetMemoryBlock(1, 2))
                {
                    AssertEx.Equal(provider.Pointer + 1, block.Pointer);
                    Assert.Equal(2, block.Size);
                    AssertEx.Equal(new byte[] { 2, 3 }, block.GetContentUnchecked(0, 2));
                    AssertEx.Equal(new byte[] { 3 }, block.GetContentUnchecked(1, 1));
                    AssertEx.Equal(new byte[] { }, block.GetContentUnchecked(2, 0));
                }
            }
        }
コード例 #2
0
        public unsafe void GetReader_Offset()
        {
            var array    = ImmutableArray.Create(new byte[] { 0, 1, 2, 3, 4 });
            var provider = new ByteArrayMemoryProvider(array);
            var block    = provider.GetMemoryBlock();

            var peBlock = new PEMemoryBlock(block, offset: 1);

            var reader1 = peBlock.GetReader();

            Assert.Equal(4, reader1.Length);
            AssertEx.Equal(new byte[] { 1, 2, 3 }, reader1.ReadBytes(3));
            AssertEx.Equal(new byte[] { 4 }, reader1.ReadBytes(1));

            var reader2 = peBlock.GetReader(1, 2);

            Assert.Equal(2, reader2.Length);
            AssertEx.Equal(new byte[] { 2, 3 }, reader2.ReadBytes(2));

            var reader3 = peBlock.GetReader(4, 0);

            Assert.Equal(0, reader3.Length);
            AssertEx.Equal(new byte[] { }, reader3.ReadBytes(0));

            Assert.Throws <ArgumentOutOfRangeException>(() => peBlock.GetReader(0, 5));
            Assert.Throws <ArgumentOutOfRangeException>(() => peBlock.GetReader(4, 1));
            Assert.Throws <ArgumentOutOfRangeException>(() => peBlock.GetReader(5, 0));
        }
コード例 #3
0
        public unsafe void ByteArray()
        {
            var array = ImmutableArray.Create(new byte[] { 1, 2, 3 });
            using (var provider = new ByteArrayMemoryProvider(array))
            {
                using (var block = provider.GetMemoryBlock())
                {
                    Assert.Equal(3, block.Size);
                    AssertEx.Equal(provider.Pointer, block.Pointer);
                    Assert.Equal(array, block.GetContent());
                }

                using (var block = provider.GetMemoryBlock(1, 2))
                {
                    AssertEx.Equal(provider.Pointer + 1, block.Pointer);
                    Assert.Equal(2, block.Size);
                    Assert.Equal(new byte[] { 2, 3 }, block.GetContent());
                }
            }
        }
コード例 #4
0
        public void EmbeddedPortablePdb()
        {
            var b = new DebugDirectoryBuilder();

            var pdb = new BlobBuilder();

            pdb.WriteInt64(0x1122334455667788);

            b.AddEmbeddedPortablePdbEntry(pdb, portablePdbVersion: 0x0100);

            var blob = new BlobBuilder();

            b.Serialize(blob, new SectionLocation(0, 0), sectionOffset: 0);
            var bytes = blob.ToImmutableArray();

            AssertEx.Equal(new byte[]
            {
                0x00, 0x00, 0x00, 0x00,                                    // Characteristics
                0x00, 0x00, 0x00, 0x00,                                    // Stamp
                0x00, 0x01, 0x00, 0x01,                                    // Version
                0x11, 0x00, 0x00, 0x00,                                    // Type
                0x12, 0x00, 0x00, 0x00,                                    // SizeOfData
                0x1C, 0x00, 0x00, 0x00,                                    // AddressOfRawData
                0x1C, 0x00, 0x00, 0x00,                                    // PointerToRawData

                0x4D, 0x50, 0x44, 0x42,                                    // signature
                0x08, 0x00, 0x00, 0x00,                                    // uncompressed size
                0xEB, 0x28, 0x4F, 0x0B, 0x75, 0x31, 0x56, 0x12, 0x04, 0x00 // compressed data
            }, bytes);

            using (var pinned = new PinnedBlob(bytes))
            {
                var actual = PEReader.ReadDebugDirectoryEntries(pinned.CreateReader(0, DebugDirectoryEntry.Size));
                Assert.Equal(1, actual.Length);
                Assert.Equal(0u, actual[0].Stamp);
                Assert.Equal(0x0100, actual[0].MajorVersion);
                Assert.Equal(0x0100, actual[0].MinorVersion);
                Assert.Equal(DebugDirectoryEntryType.EmbeddedPortablePdb, actual[0].Type);
                Assert.False(actual[0].IsPortableCodeView);
                Assert.Equal(0x00000012, actual[0].DataSize);
                Assert.Equal(0x0000001c, actual[0].DataRelativeVirtualAddress);
                Assert.Equal(0x0000001c, actual[0].DataPointer);

                var provider = new ByteArrayMemoryProvider(bytes);
                using (var block = provider.GetMemoryBlock(actual[0].DataPointer, actual[0].DataSize))
                {
                    var decoded = PEReader.DecodeEmbeddedPortablePdbDebugDirectoryData(block);
                    AssertEx.Equal(new byte[] { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }, decoded);
                }
            }
        }
コード例 #5
0
        public unsafe void GetContent_Offset()
        {
            var array    = ImmutableArray.Create(new byte[] { 0, 1, 2, 3, 4 });
            var provider = new ByteArrayMemoryProvider(array);
            var block    = provider.GetMemoryBlock();

            var peBlock = new PEMemoryBlock(block, offset: 1);

            AssertEx.Equal(new byte[] { 1, 2, 3, 4 }, peBlock.GetContent());
            AssertEx.Equal(new byte[] { 2, 3 }, peBlock.GetContent(1, 2));
            AssertEx.Equal(new byte[] { }, peBlock.GetContent(4, 0));

            Assert.Throws <ArgumentOutOfRangeException>(() => peBlock.GetContent(0, 5));
            Assert.Throws <ArgumentOutOfRangeException>(() => peBlock.GetContent(4, 1));
            Assert.Throws <ArgumentOutOfRangeException>(() => peBlock.GetContent(5, 0));
        }