From 3e803ef6d84dde7cd51d360196d05ddb7803b6b4 Mon Sep 17 00:00:00 2001 From: de4dot Date: Thu, 10 Nov 2011 00:44:37 +0100 Subject: [PATCH] Read at most 2MB at a time from files --- de4dot.code/deobfuscators/DeobUtils.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/de4dot.code/deobfuscators/DeobUtils.cs b/de4dot.code/deobfuscators/DeobUtils.cs index 9f74ac54..8980eadc 100644 --- a/de4dot.code/deobfuscators/DeobUtils.cs +++ b/de4dot.code/deobfuscators/DeobUtils.cs @@ -47,10 +47,20 @@ namespace de4dot.deobfuscators { return newDef; } + // If the file is on the network, and we read more than 2MB, we'll read from the wrong + // offset in the file! Tested: VMware 8, Win7 x64. + const int MAX_BYTES_READ = 0x200000; + public static byte[] readModule(ModuleDefinition module) { using (var fileStream = new FileStream(module.FullyQualifiedName, FileMode.Open, FileAccess.Read, FileShare.Read)) { var fileData = new byte[(int)fileStream.Length]; - fileStream.Read(fileData, 0, fileData.Length); + + int bytes, offset = 0, length = fileData.Length; + while ((bytes = fileStream.Read(fileData, offset, System.Math.Min(MAX_BYTES_READ, length - offset))) > 0) + offset += bytes; + if (offset != length) + throw new ApplicationException("Could not read all bytes"); + return fileData; } }