public TypeInfo(MTypeDef typeDef, MemberInfos memberInfos) : base(typeDef) { this.Type = typeDef; this._memberInfos = memberInfos; OldNamespace = typeDef.TypeDef.Namespace.String; }
public void AddInterface(MTypeDef ifaceDef, ITypeDefOrRef iface) { if (ifaceDef == null || iface == null) { return; } interfaces.Add(new TypeInfo(iface, ifaceDef)); }
public void AddBaseType(MTypeDef baseDef, ITypeDefOrRef baseRef) { if (baseDef == null || baseRef == null) { return; } baseType = new TypeInfo(baseRef, baseDef); }
public MMethodDef(MethodDef methodDef, MTypeDef owner, int index) : base(methodDef, owner, index) { genericParams = MGenericParamDef.CreateGenericParamDefList(MethodDef.GenericParameters); visibleBaseIndex = methodDef.MethodSig != null && methodDef.MethodSig.HasThis ? 1 : 0; for (int i = 0; i < methodDef.Parameters.Count; i++) { var param = methodDef.Parameters[i]; if (param.IsNormalMethodParameter) visibleParamCount++; paramDefs.Add(new MParamDef(param, i)); } returnParamDef = new MParamDef(methodDef.Parameters.ReturnParameter, -1); }
public MMethodDef(MethodDef methodDef, MTypeDef owner, int index) : base(methodDef, owner, index) { genericParams = MGenericParamDef.CreateGenericParamDefList(MethodDef.GenericParameters); visibleBaseIndex = methodDef.MethodSig != null && methodDef.MethodSig.HasThis ? 1 : 0; for (int i = 0; i < methodDef.Parameters.Count; i++) { var param = methodDef.Parameters[i]; if (param.IsNormalMethodParameter) { visibleParamCount++; } paramDefs.Add(new MParamDef(param, i)); } returnParamDef = new MParamDef(methodDef.Parameters.ReturnParameter, -1); }
public void FindAllMemberRefs(ref int typeIndex) { memberRefFinder = new MemberRefFinder(); memberRefFinder.FindAll(ModuleDefMD); allMethods = new List <MethodDef>(memberRefFinder.MethodDefs.Keys); var allTypesList = new List <MTypeDef>(); foreach (var type in memberRefFinder.TypeDefs.Keys) { var typeDef = new MTypeDef(type, this, typeIndex++); types.Add(typeDef); allTypesList.Add(typeDef); typeDef.AddMembers(); } var allTypesCopy = new List <MTypeDef>(allTypesList); var typeToIndex = new Dictionary <TypeDef, int>(); for (int i = 0; i < allTypesList.Count; i++) { typeToIndex[allTypesList[i].TypeDef] = i; } foreach (var typeDef in allTypesList) { if (typeDef.TypeDef.NestedTypes == null) { continue; } foreach (var nestedTypeDef2 in typeDef.TypeDef.NestedTypes) { int index = typeToIndex[nestedTypeDef2]; var nestedTypeDef = allTypesCopy[index]; allTypesCopy[index] = null; if (nestedTypeDef == null) // Impossible { throw new ApplicationException("Nested type belongs to two or more types"); } typeDef.Add(nestedTypeDef); nestedTypeDef.NestingType = typeDef; } } }
public bool Check(MTypeDef type) { if (results.ContainsKey(type)) return results[type]; bool val; if (classNames.ContainsKey(type.TypeDef.FullName)) val = true; else if (type.baseType == null) { if (type.TypeDef.BaseType != null) val = classNames.ContainsKey(type.TypeDef.BaseType.FullName); else val = false; } else val = Check(type.baseType.typeDef); results[type] = val; return val; }
static MPropertyDef FindProperty(MTypeDef typeDef, UTF8String name, TypeSig propType) { while (typeDef != null) { foreach (var propDef in typeDef.AllProperties) { if (propDef.PropertyDef.Name != name) { continue; } if (new SigComparer().Equals(propDef.PropertyDef.PropertySig.GetRetType(), propType)) { return(propDef); } } if (typeDef.baseType == null) { break; } typeDef = typeDef.baseType.typeDef; } return(null); }
static MFieldDef FindField(MTypeDef typeDef, UTF8String name, TypeSig fieldType) { while (typeDef != null) { foreach (var fieldDef in typeDef.AllFields) { if (fieldDef.FieldDef.Name != name) { continue; } if (new SigComparer().Equals(fieldDef.FieldDef.FieldSig.GetFieldType(), fieldType)) { return(fieldDef); } } if (typeDef.baseType == null) { break; } typeDef = typeDef.baseType.typeDef; } return(null); }
string FindWindowsFormsClassName(MTypeDef type) { foreach (var methodDef in type.AllMethods) { if (methodDef.MethodDef.Body == null) continue; if (methodDef.MethodDef.IsStatic || methodDef.MethodDef.IsVirtual) continue; var instructions = methodDef.MethodDef.Body.Instructions; for (int i = 2; i < instructions.Count; i++) { var call = instructions[i]; if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt) continue; if (!IsWindowsFormsSetNameMethod(call.Operand as IMethod)) continue; var ldstr = instructions[i - 1]; if (ldstr.OpCode.Code != Code.Ldstr) continue; var className = ldstr.Operand as string; if (className == null) continue; if (instructions[i - 2].GetParameterIndex() != 0) continue; FindInitializeComponentMethod(type, methodDef); return className; } } return null; }
public void add(MTypeDef t) { types.add(t); }
void FixClsTypeNames(MTypeDef nesting, MTypeDef nested) { int nestingCount = nesting == null ? 0 : nesting.GenericParams.Count; int arity = nested.GenericParams.Count - nestingCount; var nestedInfo = memberInfos.Type(nested); if (nestedInfo.renamed && arity > 0) nestedInfo.newName += "`" + arity; foreach (var nestedType in nested.NestedTypes) FixClsTypeNames(nested, nestedType); }
void Rename(MTypeDef type) { var typeDef = type.TypeDef; var info = memberInfos.Type(type); if (isVerbose) Logger.v("Type: {0} ({1:X8})", Utils.RemoveNewlines(typeDef.FullName), typeDef.MDToken.ToUInt32()); Logger.Instance.Indent(); renameGenericParams2(type.GenericParams); if (RenameTypes && info.GotNewName()) { var old = typeDef.Name; typeDef.Name = info.newName; if (isVerbose) Logger.v("Name: {0} => {1}", Utils.RemoveNewlines(old), Utils.RemoveNewlines(typeDef.Name)); } if (RenameNamespaces && info.newNamespace != null) { var old = typeDef.Namespace; typeDef.Namespace = info.newNamespace; if (isVerbose) Logger.v("Namespace: {0} => {1}", Utils.RemoveNewlines(old), Utils.RemoveNewlines(typeDef.Namespace)); } Logger.Instance.DeIndent(); }
void Merge(MTypeDef type) { if (visited.ContainsKey(type)) return; visited[type] = true; TypeInfo info; if (!memberInfos.TryGetType(type, out info)) return; if (type.baseType != null) Merge(type.baseType.typeDef); foreach (var ifaceInfo in type.interfaces) Merge(ifaceInfo.typeDef); if (type.baseType != null) Merge(info, type.baseType.typeDef); foreach (var ifaceInfo in type.interfaces) Merge(info, ifaceInfo.typeDef); }
void Prepare(MTypeDef type) { if (prepareMethodCalled.ContainsKey(type)) return; prepareMethodCalled[type] = true; foreach (var ifaceInfo in type.interfaces) Prepare(ifaceInfo.typeDef); if (type.baseType != null) Prepare(type.baseType.typeDef); TypeInfo info; if (memberInfos.TryGetType(type, out info)) func(info); }
public bool Check(MTypeDef type) { return Check(type, 0); }
public void AddBaseType(MTypeDef baseDef, ITypeDefOrRef baseRef) { if (baseDef == null || baseRef == null) return; baseType = new TypeInfo(baseRef, baseDef); }
public TypeInfo(TypeInfo other, GenericInstSig git) { this.typeRef = GenericArgsSubstitutor.Create(other.typeRef, git); this.typeDef = other.typeDef; }
public bool TryGetType(MTypeDef t, out TypeInfo info) { return allTypeInfos.TryGetValue(t, out info); }
public TypeInfo Type(MTypeDef t) { return allTypeInfos[t]; }
public bool IsWinFormsClass(MTypeDef type) { return checkWinFormsClass.Check(type); }
public MEventDef(EventDef eventDef, MTypeDef owner, int index) : base(eventDef, owner, index) { }
void FindInitializeComponentMethod(MTypeDef type, MMethodDef possibleInitMethod) { foreach (var methodDef in type.AllMethods) { if (methodDef.MethodDef.Name != ".ctor") continue; if (methodDef.MethodDef.Body == null) continue; foreach (var instr in methodDef.MethodDef.Body.Instructions) { if (instr.OpCode.Code != Code.Call && instr.OpCode.Code != Code.Callvirt) continue; if (!MethodEqualityComparer.CompareDeclaringTypes.Equals(possibleInitMethod.MethodDef, instr.Operand as IMethod)) continue; memberInfos.Method(possibleInitMethod).suggestedName = "InitializeComponent"; return; } } }
public MFieldDef(FieldDef fieldDef, MTypeDef owner, int index) : base(fieldDef, owner, index) { }
protected Ref(IMemberRef memberRef, MTypeDef owner, int index) { this.memberRef = memberRef; Owner = owner; Index = index; }
MEventDef CreateEvent(MTypeDef ownerType, string name, TypeSig eventType) { if (string.IsNullOrEmpty(name) || eventType == null || eventType.ElementType == ElementType.Void) return null; var newEvent = ownerType.Module.ModuleDefMD.UpdateRowId(new EventDefUser(name, eventType.ToTypeDefOrRef(), 0)); var eventDef = ownerType.FindAny(newEvent); if (eventDef != null) return eventDef; eventDef = ownerType.Create(newEvent); memberInfos.Add(eventDef); if (isVerbose) Logger.v("Restoring event: {0}", Utils.RemoveNewlines(newEvent)); return eventDef; }
public TypeInfo(ITypeDefOrRef typeRef, MTypeDef typeDef) { this.typeRef = typeRef; this.typeDef = typeDef; }
void Visit(MTypeDef type) { if (visited.ContainsKey(type)) return; visited[type] = true; if (type.baseType != null) Visit(type.baseType.typeDef); foreach (var ifaceInfo in type.interfaces) Visit(ifaceInfo.typeDef); TypeInfo info; if (!memberInfos.TryGetType(type, out info)) return; foreach (var method in type.AllMethodsSorted) { MethodNameGroup group; if (!methodToGroup.TryGetValue(method, out group)) continue; foreach (var m in group.Methods) methodToGroup.Remove(m); func(group); } }
public void AddInterface(MTypeDef ifaceDef, ITypeDefOrRef iface) { if (ifaceDef == null || iface == null) return; interfaces.Add(new TypeInfo(iface, ifaceDef)); }
void Merge(TypeInfo info, MTypeDef other) { TypeInfo otherInfo; if (!memberInfos.TryGetType(other, out otherInfo)) return; if ((flags & MergeStateFlags.Methods) != MergeStateFlags.None) info.variableNameState.MergeMethods(otherInfo.variableNameState); if ((flags & MergeStateFlags.Properties) != MergeStateFlags.None) info.variableNameState.MergeProperties(otherInfo.variableNameState); if ((flags & MergeStateFlags.Events) != MergeStateFlags.None) info.variableNameState.MergeEvents(otherInfo.variableNameState); }
public void Add(MTypeDef t) { types.Add(t); }
void RenameMembers(MTypeDef type) { var info = memberInfos.Type(type); if (isVerbose) Logger.v("Type: {0}", Utils.RemoveNewlines(info.type.TypeDef.FullName)); Logger.Instance.Indent(); RenameFields2(info); RenameProperties2(info); RenameEvents2(info); RenameMethods2(info); Logger.Instance.DeIndent(); }
MPropertyDef CreateProperty(MTypeDef ownerType, string name, TypeSig propType, MethodDef getter, MethodDef setter) { if (string.IsNullOrEmpty(name) || propType.ElementType == ElementType.Void) return null; var newSig = CreatePropertySig(getter, propType, true) ?? CreatePropertySig(setter, propType, false); if (newSig == null) return null; var newProp = ownerType.Module.ModuleDefMD.UpdateRowId(new PropertyDefUser(name, newSig, 0)); newProp.GetMethod = getter; newProp.SetMethod = setter; var propDef = ownerType.FindAny(newProp); if (propDef != null) return propDef; propDef = ownerType.Create(newProp); memberInfos.Add(propDef); if (isVerbose) Logger.v("Restoring property: {0}", Utils.RemoveNewlines(newProp)); return propDef; }
public MPropertyDef(PropertyDef propertyDef, MTypeDef owner, int index) : base(propertyDef, owner, index) { }
void MergeState(MTypeDef other) { if (other == null) return; TypeInfo otherInfo; if (!memberInfos.TryGetType(other, out otherInfo)) return; variableNameState.Merge(otherInfo.variableNameState); }
public MTypeDef ResolveOther(ITypeDefOrRef type) { if (type == null) { return(null); } type = type.ScopeType; if (type == null) { return(null); } MTypeDef typeDef; if (typeToTypeDefDict.TryGetValue(type, out typeDef)) { return(typeDef); } var typeDef2 = deobfuscatorContext.ResolveType(type); if (typeDef2 == null) { typeToTypeDefDict.TryGetSimilarValue(type, out typeDef); typeToTypeDefDict[type] = typeDef; return(typeDef); } if (typeToTypeDefDict.TryGetValue(typeDef2, out typeDef)) { typeToTypeDefDict[type] = typeDef; return(typeDef); } typeToTypeDefDict[type] = null; // In case of a circular reference typeToTypeDefDict[typeDef2] = null; typeDef = new MTypeDef(typeDef2, null, 0); typeDef.AddMembers(); foreach (var iface in typeDef.TypeDef.Interfaces) { var ifaceDef = ResolveOther(iface.Interface); if (ifaceDef == null) { continue; } typeDef.AddInterface(ifaceDef, iface.Interface); } var baseDef = ResolveOther(typeDef.TypeDef.BaseType); if (baseDef != null) { typeDef.AddBaseType(baseDef, typeDef.TypeDef.BaseType); } typeToTypeDefDict[type] = typeDef; if (type != typeDef2) { typeToTypeDefDict[typeDef2] = typeDef; } return(typeDef); }