2012-04-29 06:01:50 +08:00
|
|
|
|
/*
|
2014-03-12 05:15:43 +08:00
|
|
|
|
Copyright (C) 2011-2014 de4dot@gmail.com
|
2012-04-29 06:01:50 +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.Collections.Generic;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
2012-04-29 06:01:50 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators {
|
2015-05-17 19:29:48 +08:00
|
|
|
|
public class MemberRefBuilder {
|
2012-11-01 21:39:39 +08:00
|
|
|
|
ModuleDefMD module;
|
|
|
|
|
Dictionary<TypeSig, TypeSig> createdTypes = new Dictionary<TypeSig, TypeSig>(TypeEqualityComparer.Instance);
|
2012-04-29 06:01:50 +08:00
|
|
|
|
|
2012-11-22 16:14:51 +08:00
|
|
|
|
public MemberRefBuilder(ModuleDefMD module) {
|
2012-04-29 06:01:50 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public AssemblyRef CorLib {
|
|
|
|
|
get { return module.CorLibTypes.AssemblyRef; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig Object {
|
|
|
|
|
get { return module.CorLibTypes.Object; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig Void {
|
|
|
|
|
get { return module.CorLibTypes.Void; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig Boolean {
|
|
|
|
|
get { return module.CorLibTypes.Boolean; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig Char {
|
|
|
|
|
get { return module.CorLibTypes.Char; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig SByte {
|
|
|
|
|
get { return module.CorLibTypes.SByte; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig Byte {
|
|
|
|
|
get { return module.CorLibTypes.Byte; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig Int16 {
|
|
|
|
|
get { return module.CorLibTypes.Int16; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig UInt16 {
|
|
|
|
|
get { return module.CorLibTypes.UInt16; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig Int32 {
|
|
|
|
|
get { return module.CorLibTypes.Int32; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig UInt32 {
|
|
|
|
|
get { return module.CorLibTypes.UInt32; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig Int64 {
|
|
|
|
|
get { return module.CorLibTypes.Int64; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig UInt64 {
|
|
|
|
|
get { return module.CorLibTypes.UInt64; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig Single {
|
|
|
|
|
get { return module.CorLibTypes.Single; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig Double {
|
|
|
|
|
get { return module.CorLibTypes.Double; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig IntPtr {
|
|
|
|
|
get { return module.CorLibTypes.IntPtr; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig UIntPtr {
|
|
|
|
|
get { return module.CorLibTypes.UIntPtr; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig String {
|
|
|
|
|
get { return module.CorLibTypes.String; }
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 21:39:39 +08:00
|
|
|
|
public CorLibTypeSig TypedReference {
|
|
|
|
|
get { return module.CorLibTypes.TypedReference; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public ClassSig Type(string ns, string name, string asmSimpleName) {
|
|
|
|
|
return Type(ns, name, FindAssemblyRef(asmSimpleName));
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public ClassSig Type(string ns, string name) {
|
|
|
|
|
return Type(ns, name, CorLib);
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public ClassSig Type(string ns, string name, AssemblyRef asmRef) {
|
|
|
|
|
return (ClassSig)Type(false, ns, name, asmRef);
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public ValueTypeSig ValueType(string ns, string name, string asmSimpleName) {
|
|
|
|
|
return ValueType(ns, name, FindAssemblyRef(asmSimpleName));
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public ValueTypeSig ValueType(string ns, string name) {
|
|
|
|
|
return ValueType(ns, name, CorLib);
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public ValueTypeSig ValueType(string ns, string name, AssemblyRef asmRef) {
|
|
|
|
|
return (ValueTypeSig)Type(true, ns, name, asmRef);
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public ClassOrValueTypeSig Type(bool isValueType, string ns, string name, IResolutionScope resolutionScope) {
|
2012-11-01 21:39:39 +08:00
|
|
|
|
var typeRef = module.UpdateRowId(new TypeRefUser(module, ns, name, resolutionScope));
|
|
|
|
|
ClassOrValueTypeSig type;
|
|
|
|
|
if (isValueType)
|
|
|
|
|
type = new ValueTypeSig(typeRef);
|
|
|
|
|
else
|
|
|
|
|
type = new ClassSig(typeRef);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
return (ClassOrValueTypeSig)Add(type);
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public SZArraySig Array(TypeSig typeRef) {
|
|
|
|
|
return (SZArraySig)Add(new SZArraySig(typeRef));
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
TypeSig Add(TypeSig typeRef) {
|
2012-11-01 21:39:39 +08:00
|
|
|
|
TypeSig createdTypeRef;
|
|
|
|
|
if (createdTypes.TryGetValue(typeRef, out createdTypeRef)) {
|
|
|
|
|
if (createdTypeRef.ElementType != typeRef.ElementType)
|
2012-04-29 06:01:50 +08:00
|
|
|
|
throw new ApplicationException(string.Format("Type {0}'s IsValueType is not correct", createdTypeRef));
|
|
|
|
|
return createdTypeRef;
|
|
|
|
|
}
|
2012-11-01 21:39:39 +08:00
|
|
|
|
createdTypes[typeRef] = typeRef;
|
2012-04-29 06:01:50 +08:00
|
|
|
|
return typeRef;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public MemberRef InstanceMethod(string name, IMemberRefParent declaringType, TypeSig returnType, params TypeSig[] args) {
|
|
|
|
|
return Method(true, name, declaringType, returnType, args);
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public MemberRef StaticMethod(string name, IMemberRefParent declaringType, TypeSig returnType, params TypeSig[] args) {
|
|
|
|
|
return Method(false, name, declaringType, returnType, args);
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public MemberRef Method(bool isInstance, string name, IMemberRefParent declaringType, TypeSig returnType, params TypeSig[] args) {
|
2012-11-01 21:39:39 +08:00
|
|
|
|
MethodSig sig;
|
|
|
|
|
if (isInstance)
|
|
|
|
|
sig = MethodSig.CreateInstance(returnType, args);
|
|
|
|
|
else
|
|
|
|
|
sig = MethodSig.CreateStatic(returnType, args);
|
|
|
|
|
return module.UpdateRowId(new MemberRefUser(module, name, sig, declaringType));
|
2012-04-29 06:01:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
AssemblyRef FindAssemblyRef(string asmSimpleName) {
|
2012-11-08 18:26:14 +08:00
|
|
|
|
var asmRef = module.GetAssemblyRef(asmSimpleName);
|
2012-04-29 06:01:50 +08:00
|
|
|
|
if (asmRef == null)
|
|
|
|
|
throw new ApplicationException(string.Format("Could not find assembly {0} in assembly references", asmSimpleName));
|
|
|
|
|
return asmRef;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|