diff --git a/Mono.Cecil.PE/ImageReader.cs b/Mono.Cecil.PE/ImageReader.cs index 24b811f54..71bd67259 100644 --- a/Mono.Cecil.PE/ImageReader.cs +++ b/Mono.Cecil.PE/ImageReader.cs @@ -289,13 +289,33 @@ void ReadCLIHeader () // ManagedNativeHeader 8 } - void ReadMetadata () + void CopyTo(ref NativeMemory memory) { - MoveTo (metadata); + var destination = memory.Pointer; + var size = memory.Length; + var buffer = new byte[Math.Min(81920, size)]; + while (size > 0) { + int readSize = Math.Min(size, buffer.Length); + int bytesRead = Read(buffer, 0, readSize); + + if (bytesRead <= 0 || bytesRead > readSize) { + throw new IOException(); + } + + Marshal.Copy(buffer, 0, (IntPtr) destination, bytesRead); + + destination += bytesRead; + size -= bytesRead; + } + } + + void ReadMetadata () + { image.Metadata = new NativeMemory((int)metadata.Size); - var bytes = ReadBytes ((int)metadata.Size); - Marshal.Copy (bytes, 0, (IntPtr)image.Metadata.Pointer, image.Metadata.Length); + + MoveTo (metadata); + CopyTo (ref image.Metadata); var buffer = new PByteBuffer (image.Metadata.Pointer, (uint) image.Metadata.Length);