From 98342f2a0c13ae893cbc4922eb50dbcc5dcd2d16 Mon Sep 17 00:00:00 2001 From: de4dot Date: Wed, 30 Nov 2011 18:21:01 +0100 Subject: [PATCH] Move read file code to Utils --- de4dot.code/Utils.cs | 18 ++++++++++++++++++ de4dot.code/deobfuscators/DeobUtils.cs | 16 +--------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/de4dot.code/Utils.cs b/de4dot.code/Utils.cs index 0bb01126..6b943de3 100644 --- a/de4dot.code/Utils.cs +++ b/de4dot.code/Utils.cs @@ -236,5 +236,23 @@ namespace de4dot { if (a > b) return 1; return 0; } + + public static byte[] readFile(string filename) { + // 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; + + using (var fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { + var fileData = new byte[(int)fileStream.Length]; + + int bytes, offset = 0, length = fileData.Length; + while ((bytes = fileStream.Read(fileData, offset, Math.Min(MAX_BYTES_READ, length - offset))) > 0) + offset += bytes; + if (offset != length) + throw new ApplicationException("Could not read all bytes"); + + return fileData; + } + } } } diff --git a/de4dot.code/deobfuscators/DeobUtils.cs b/de4dot.code/deobfuscators/DeobUtils.cs index 8980eadc..670b1688 100644 --- a/de4dot.code/deobfuscators/DeobUtils.cs +++ b/de4dot.code/deobfuscators/DeobUtils.cs @@ -47,22 +47,8 @@ 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]; - - 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; - } + return Utils.readFile(module.FullyQualifiedName); } } }