예제 #1
0
        public void ReadBytes_Gradual_EOF()
        {
            byte[] input  = new byte[10];
            var    stream = new GradualReadMemoryStream(input, 3, 7);
            var    reader = new BinaryStreamReader(stream, new byte[8]);

            byte[] output = new byte[input.Length + 5];
            reader.ReadBytes(output, 0, output.Length);
        }
예제 #2
0
 public void ReadBytes_123()
 {
     var reader = new BinaryStreamReader(new MemoryStream(new byte[] { 1, 2, 3, }), new byte[20]);
     byte[] result = new byte[3];
     reader.ReadBytes(result, 0, 3);
     Assert.AreEqual(1, result[0]);
     Assert.AreEqual(2, result[1]);
     Assert.AreEqual(3, result[2]);
 }
예제 #3
0
        public void ReadBytes_123()
        {
            var reader = new BinaryStreamReader(new MemoryStream(new byte[] { 1, 2, 3, }), new byte[20]);

            byte[] result = new byte[3];
            reader.ReadBytes(result, 0, 3);
            Assert.AreEqual(1, result[0]);
            Assert.AreEqual(2, result[1]);
            Assert.AreEqual(3, result[2]);
        }
예제 #4
0
        public void ReadBytes_Gradual()
        {
            byte[] input = new byte[10];
            for (int i = 0; i < input.Length; i++)
            {
                input[i] = (byte)i;
            }
            var stream = new GradualReadMemoryStream(input, 3, 7);
            var reader = new BinaryStreamReader(stream, new byte[8]);

            byte[] output = new byte[input.Length];
            reader.ReadBytes(output, 0, output.Length);

            Assert.AreEqual(input.Length, output.Length);
            for (int i = 0; i < output.Length; i++)
            {
                Assert.AreEqual(input[i], output[i], "output[" + i + "]");
            }
        }
예제 #5
0
        static void Main(string[] args)
        {
            var pe = new PEFile();
            var stream = new MemoryStream(Properties.Resources.console_anycpu);
            var reader = new BinaryStreamReader(stream, new byte[1024]);
            pe.ReadFrom(reader);

            uint lowestPointerToRawData = uint.MaxValue;
            uint lowestVirtualAddress = uint.MaxValue;
            uint highestVirtualAddress = uint.MinValue;

            foreach (var s in pe.SectionHeaders)
            {
                lowestPointerToRawData = Math.Min(lowestPointerToRawData, s.PointerToRawData);
                lowestVirtualAddress = Math.Min(lowestVirtualAddress, s.VirtualAddress);
                highestVirtualAddress = Math.Max(highestVirtualAddress, s.VirtualAddress + (uint)s.VirtualSize);
            }

            byte[] allSectionContent = new byte[highestVirtualAddress - lowestVirtualAddress];
            foreach (var s in pe.SectionHeaders)
            {
                reader.Position = s.PointerToRawData;
                reader.ReadBytes(allSectionContent, (int)(s.VirtualAddress - lowestVirtualAddress), (int)s.VirtualSize);
            }

            pe.PEHeader.NumberOfSections = 1;
            var singleSection = pe.SectionHeaders[0];
            singleSection.VirtualSize = (uint)allSectionContent.Length;
            pe.SectionHeaders = new[] { singleSection };

            using (var peFileStream = File.Create("console.anycpu.insane.exe"))
            {
                var writer = new BinaryStreamWriter(peFileStream);
                pe.WriteTo(writer);
                writer.Position = lowestPointerToRawData;
                writer.WriteBytes(allSectionContent, 0, allSectionContent.Length);
            }
        }
예제 #6
0
        public void ReadBytes_NegativeLength()
        {
            var reader = new BinaryStreamReader(new MemoryStream(), new byte[20]);

            reader.ReadBytes(new byte[3], 0, -1);
        }
예제 #7
0
        public void ReadBytes_TooLargeOffset()
        {
            var reader = new BinaryStreamReader(new MemoryStream(), new byte[20]);

            reader.ReadBytes(new byte[3], 10, 1);
        }
예제 #8
0
        public void ReadBytes_NegativeOffset()
        {
            var reader = new BinaryStreamReader(new MemoryStream(), new byte[20]);

            reader.ReadBytes(new byte[3], -1, 1);
        }
예제 #9
0
        public void ReadBytes_NullBuffer()
        {
            var reader = new BinaryStreamReader(new MemoryStream(), new byte[20]);

            reader.ReadBytes(null, 0, 1);
        }
예제 #10
0
 public void ReadBytes_NegativeLength()
 {
     var reader = new BinaryStreamReader(new MemoryStream(), new byte[20]);
     reader.ReadBytes(new byte[3], 0, -1);
 }
예제 #11
0
        public void ReadBytes_Gradual()
        {
            byte[] input =new byte[10];
            for (int i = 0; i < input.Length; i++)
            {
                input[i] = (byte)i;
            }
            var stream = new GradualReadMemoryStream(input, 3, 7);
            var reader = new BinaryStreamReader(stream, new byte[8]);
            byte[] output = new byte[input.Length];
            reader.ReadBytes(output, 0, output.Length);

            Assert.AreEqual(input.Length, output.Length);
            for (int i = 0; i < output.Length; i++)
            {
                Assert.AreEqual(input[i], output[i], "output[" + i + "]");
            }
        }
예제 #12
0
 public void ReadBytes_TooLargeOffset()
 {
     var reader = new BinaryStreamReader(new MemoryStream(), new byte[20]);
     reader.ReadBytes(new byte[3], 10, 1);
 }
예제 #13
0
 public void ReadBytes_TooLargeLength()
 {
     var reader = new BinaryStreamReader(new MemoryStream(), new byte[20]);
     reader.ReadBytes(new byte[3], 0, 10);
 }
예제 #14
0
 public void ReadBytes_NullBuffer()
 {
     var reader = new BinaryStreamReader(new MemoryStream(), new byte[20]);
     reader.ReadBytes(null, 0, 1);
 }
예제 #15
0
 public void ReadBytes_NegativeOffset()
 {
     var reader = new BinaryStreamReader(new MemoryStream(), new byte[20]);
     reader.ReadBytes(new byte[3], -1, 1);
 }
예제 #16
0
        public void ReadBytes_TooLargeLength()
        {
            var reader = new BinaryStreamReader(new MemoryStream(), new byte[20]);

            reader.ReadBytes(new byte[3], 0, 10);
        }
예제 #17
0
    private static void MeasureManyFilesLoad(string[] dllFiles)
    {
        var buffer = new byte[1024];

        var start = DateTime.UtcNow;
        foreach (var dll in dllFiles)
        {
            using (var dllStream = File.OpenRead(dll))
            {
                var pe = new PEFile();
                pe.ReadFrom(new BinaryStreamReader(dllStream, buffer));
            }
        }

        TimeSpan headersOnly = DateTime.UtcNow - start;

        byte[] buf = new byte[1024];
        byte[] contentBuf = new byte[1024*1024];
        start = DateTime.UtcNow;
        foreach (var dll in dllFiles)
        {
            using (var dllStream = File.OpenRead(dll))
            {
                var pe = new PEFile();
                var reader = new BinaryStreamReader(dllStream, buf);
                pe.ReadFrom(reader);

                while(reader.Position<dllStream.Length)
                {
                    reader.ReadBytes(contentBuf, 0, (int)Math.Min(contentBuf.Length, dllStream.Length - reader.Position));
                }
            }
        }

        TimeSpan headersAndContent = DateTime.UtcNow - start;

        start = DateTime.UtcNow;
        foreach (var dll in dllFiles)
        {
            System.Reflection.Assembly.Load(File.ReadAllBytes(dll));
        }

        TimeSpan reflectionLoad = DateTime.UtcNow - start;

        Console.WriteLine(
            dllFiles.Length + " dlls\t" +
            "Headers only: " + headersOnly.TotalSeconds.ToString("#0.000") + " sec." +
            "  " +
            "Headers and content: " + headersAndContent.TotalSeconds.ToString("#0.000") + " sec." +
            "  " +
            "Reflection: " + reflectionLoad.TotalSeconds.ToString("#0.000") + " sec." +
            "");
    }
예제 #18
0
        void ReadDosHeader(BinaryStreamReader reader)
        {
            if (this.DosHeader == null)
                this.DosHeader = new DosHeader();

            var signature = (MZSignature)reader.ReadInt16();
            if (signature != MZSignature.MZ)
                throw new BadImageFormatException("MZ signature expected, " + ((ushort)signature).ToString("X4") + "h found.");

            this.DosHeader.cblp = reader.ReadUInt16();
            this.DosHeader.cp = reader.ReadUInt16();
            this.DosHeader.crlc = reader.ReadUInt16();
            this.DosHeader.cparhdr = reader.ReadUInt16();
            this.DosHeader.minalloc = reader.ReadUInt16();
            this.DosHeader.maxalloc = reader.ReadUInt16();
            this.DosHeader.ss = reader.ReadUInt16();
            this.DosHeader.sp = reader.ReadUInt16();
            this.DosHeader.csum = reader.ReadUInt16();
            this.DosHeader.ip = reader.ReadUInt16();
            this.DosHeader.cs = reader.ReadUInt16();
            this.DosHeader.lfarlc = reader.ReadUInt16();
            this.DosHeader.ovno = reader.ReadUInt16();

            this.DosHeader.res1 = reader.ReadUInt64();

            this.DosHeader.oemid = reader.ReadUInt16();
            this.DosHeader.oeminfo = reader.ReadUInt16();

            this.DosHeader.ReservedNumber0 = reader.ReadUInt32();
            this.DosHeader.ReservedNumber1 = reader.ReadUInt32();
            this.DosHeader.ReservedNumber2 = reader.ReadUInt32();
            this.DosHeader.ReservedNumber3 = reader.ReadUInt32();
            this.DosHeader.ReservedNumber4 = reader.ReadUInt32();
            this.DosHeader.lfanew = reader.ReadUInt32();

            if (this.DosHeader.lfanew > DosHeader.Size)
            {
                this.DosStub = new byte[this.DosHeader.lfanew - DosHeader.Size];
                reader.ReadBytes(this.DosStub, 0, this.DosStub.Length);
            }
        }
예제 #19
0
 private string ReadName(BinaryStreamReader reader)
 {
     ushort length = reader.ReadUInt16();
     byte[] buf = new byte[length * 2]; // two-byte Unicode characters
     reader.ReadBytes(buf, 0, buf.Length);
     string result = Encoding.Unicode.GetString(buf, 0, buf.Length);
     return result;
 }
예제 #20
0
 public void ReadBytes_Gradual_EOF()
 {
     byte[] input = new byte[10];
     var stream = new GradualReadMemoryStream(input, 3, 7);
     var reader = new BinaryStreamReader(stream, new byte[8]);
     byte[] output = new byte[input.Length + 5];
     reader.ReadBytes(output, 0, output.Length);
 }