2012-01-08 03:27:07 +08:00
|
|
|
|
/*
|
2012-01-10 06:04:52 +08:00
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
2012-01-08 03:27:07 +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;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
using dot10.DotNet;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Babel_NET {
|
|
|
|
|
// Converts type references/definitions in one module to this module
|
|
|
|
|
class MemberReferenceConverter {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
public ModuleDefMD Module {
|
2012-01-08 03:27:07 +08:00
|
|
|
|
get { return module; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
public MemberReferenceConverter(ModuleDefMD module) {
|
2012-01-08 03:27:07 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
bool isInOurModule(IMemberRef memberRef) {
|
|
|
|
|
return memberRef.OwnerModule == module;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
Importer createImporter() {
|
|
|
|
|
return new Importer(module, ImporterOptions.TryToUseTypeDefs);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
public TypeSig convert(TypeRef typeRef) {
|
|
|
|
|
return createImporter().Import(typeRef).ToTypeSig();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
ITypeDefOrRef convert(ITypeDefOrRef tdr) {
|
|
|
|
|
return (ITypeDefOrRef)createImporter().Import(tdr);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
TypeSig convert2(TypeSig ts) {
|
|
|
|
|
return createImporter().Import(ts);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
public TypeSig convert(TypeSig ts) {
|
|
|
|
|
return createImporter().Import(ts);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
public IField convert(IField fieldRef) {
|
|
|
|
|
if (isInOurModule(fieldRef))
|
|
|
|
|
return tryGetFieldDefinition(fieldRef);
|
|
|
|
|
return createImporter().Import(fieldRef);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
public IMethodDefOrRef convert(IMethod methodRef) {
|
|
|
|
|
if (!(methodRef is MemberRef || methodRef is MethodDef) || methodRef.MethodSig == null)
|
|
|
|
|
throw new ApplicationException("Invalid method reference type");
|
|
|
|
|
if (isInOurModule(methodRef))
|
|
|
|
|
return (IMethodDefOrRef)tryGetMethodDefinition(methodRef);
|
|
|
|
|
return (IMethodDefOrRef)createImporter().Import(methodRef);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
public IField tryGetFieldDefinition(IField fieldRef) {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var fieldDef = fieldRef as FieldDef;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (fieldDef != null)
|
|
|
|
|
return fieldDef;
|
|
|
|
|
|
|
|
|
|
var declaringType = DotNetUtils.getType(module, fieldRef.DeclaringType);
|
|
|
|
|
if (declaringType == null)
|
|
|
|
|
return fieldRef;
|
2012-01-14 18:53:38 +08:00
|
|
|
|
return DotNetUtils.getField(declaringType, fieldRef);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
public IMethod tryGetMethodDefinition(IMethod methodRef) {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var methodDef = methodRef as MethodDef;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (methodDef != null)
|
|
|
|
|
return methodDef;
|
|
|
|
|
|
|
|
|
|
var declaringType = DotNetUtils.getType(module, methodRef.DeclaringType);
|
|
|
|
|
if (declaringType == null)
|
|
|
|
|
return methodRef;
|
2012-01-14 18:53:38 +08:00
|
|
|
|
return DotNetUtils.getMethod(declaringType, methodRef);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|