2011-09-22 10:55:30 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
|
|
|
|
This file is part of de4dot.
|
|
|
|
|
|
|
|
|
|
de4dot is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
de4dot is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Writer;
|
2011-11-02 09:25:07 +08:00
|
|
|
|
using de4dot.blocks;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
class AssemblyModule {
|
|
|
|
|
string filename;
|
2012-11-01 21:39:39 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-11-05 03:06:58 +08:00
|
|
|
|
ModuleContext moduleContext;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2012-11-05 03:06:58 +08:00
|
|
|
|
public AssemblyModule(string filename, ModuleContext moduleContext) {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
this.filename = Utils.getFullPath(filename);
|
2012-11-05 03:06:58 +08:00
|
|
|
|
this.moduleContext = moduleContext;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public ModuleDefMD load() {
|
2012-11-05 03:06:58 +08:00
|
|
|
|
return setModule(ModuleDefMD.Load(filename, moduleContext));
|
2011-12-01 01:26:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public ModuleDefMD load(byte[] fileData) {
|
2012-11-05 03:06:58 +08:00
|
|
|
|
return setModule(ModuleDefMD.Load(fileData, moduleContext));
|
2011-12-01 01:26:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
ModuleDefMD setModule(ModuleDefMD newModule) {
|
2011-12-01 01:26:36 +08:00
|
|
|
|
module = newModule;
|
2012-11-02 15:23:20 +08:00
|
|
|
|
TheAssemblyResolver.Instance.addModule(module);
|
2012-11-05 02:40:36 +08:00
|
|
|
|
module.EnableTypeDefFindCache = true;
|
2012-11-01 21:39:39 +08:00
|
|
|
|
module.Location = filename;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
return module;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-01 10:24:12 +08:00
|
|
|
|
public void save(string newFilename, MetaDataFlags mdFlags, IModuleWriterListener writerListener) {
|
2012-11-13 14:42:35 +08:00
|
|
|
|
if (module.IsILOnly) {
|
|
|
|
|
var writerOptions = new ModuleWriterOptions(module, writerListener);
|
|
|
|
|
writerOptions.MetaDataOptions.Flags |= mdFlags;
|
|
|
|
|
writerOptions.Logger = Logger.Instance;
|
|
|
|
|
module.Write(newFilename, writerOptions);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
var writerOptions = new NativeModuleWriterOptions(module, writerListener);
|
|
|
|
|
writerOptions.MetaDataOptions.Flags |= mdFlags;
|
|
|
|
|
writerOptions.Logger = Logger.Instance;
|
2012-11-13 14:45:34 +08:00
|
|
|
|
writerOptions.KeepExtraPEData = true;
|
|
|
|
|
writerOptions.KeepWin32Resources = true;
|
2012-11-13 14:42:35 +08:00
|
|
|
|
module.NativeWrite(newFilename, writerOptions);
|
|
|
|
|
}
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-07 07:53:16 +08:00
|
|
|
|
public ModuleDefMD reload(byte[] newModuleData, DumpedMethodsRestorer dumpedMethodsRestorer, IStringDecrypter stringDecrypter) {
|
2012-11-20 08:13:18 +08:00
|
|
|
|
TheAssemblyResolver.Instance.Remove(module);
|
2012-11-07 07:53:16 +08:00
|
|
|
|
var mod = ModuleDefMD.Load(newModuleData, moduleContext);
|
|
|
|
|
if (dumpedMethodsRestorer != null)
|
|
|
|
|
dumpedMethodsRestorer.Module = mod;
|
|
|
|
|
mod.StringDecrypter = stringDecrypter;
|
|
|
|
|
mod.MethodDecrypter = dumpedMethodsRestorer;
|
|
|
|
|
mod.TablesStream.ColumnReader = dumpedMethodsRestorer;
|
|
|
|
|
mod.TablesStream.MethodRowReader = dumpedMethodsRestorer;
|
|
|
|
|
return setModule(mod);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
return filename;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|