Merge branch 'master' into dnr

This commit is contained in:
de4dot 2011-11-02 04:26:12 +01:00
commit b8879e74e6
5 changed files with 205 additions and 84 deletions

View File

@ -44,8 +44,54 @@ namespace de4dot.blocks {
TypeReference,
}
public class ScopeAndTokenKey {
readonly IMetadataScope scope;
readonly int token;
public ScopeAndTokenKey(TypeReference type)
: this(type.Scope, type.MetadataToken.ToInt32()) {
}
public ScopeAndTokenKey(FieldReference field)
: this(field.DeclaringType == null ? null : field.DeclaringType.Scope, field.MetadataToken.ToInt32()) {
}
public ScopeAndTokenKey(MethodReference method)
: this(method.DeclaringType == null ? null : method.DeclaringType.Scope, method.MetadataToken.ToInt32()) {
}
public ScopeAndTokenKey(PropertyReference prop)
: this(prop.DeclaringType == null ? null : prop.DeclaringType.Scope, prop.MetadataToken.ToInt32()) {
}
public ScopeAndTokenKey(EventReference evt)
: this(evt.DeclaringType == null ? null : evt.DeclaringType.Scope, evt.MetadataToken.ToInt32()) {
}
public ScopeAndTokenKey(IMetadataScope scope, int token) {
this.scope = scope;
this.token = token;
}
public override int GetHashCode() {
return token + MemberReferenceHelper.scopeHashCode(scope);
}
public override bool Equals(object obj) {
var other = obj as ScopeAndTokenKey;
if (other == null)
return false;
return token == other.token &&
MemberReferenceHelper.compareScope(scope, other.scope);
}
public override string ToString() {
return string.Format("{0:X8} {1}", token, scope);
}
}
public class TypeReferenceKey {
TypeReference typeRef;
readonly TypeReference typeRef;
public TypeReference TypeReference {
get { return typeRef; }
@ -72,7 +118,7 @@ namespace de4dot.blocks {
}
public class FieldReferenceKey {
FieldReference fieldRef;
readonly FieldReference fieldRef;
public FieldReference FieldReference {
get { return fieldRef; }
@ -99,7 +145,7 @@ namespace de4dot.blocks {
}
public class PropertyReferenceKey {
PropertyReference propRef;
readonly PropertyReference propRef;
public PropertyReference PropertyReference {
get { return propRef; }
@ -126,7 +172,7 @@ namespace de4dot.blocks {
}
public class EventReferenceKey {
EventReference eventRef;
readonly EventReference eventRef;
public EventReference EventReference {
get { return eventRef; }
@ -153,7 +199,7 @@ namespace de4dot.blocks {
}
public class MethodReferenceKey {
MethodReference methodRef;
readonly MethodReference methodRef;
public MethodReference MethodReference {
get { return methodRef; }
@ -180,7 +226,7 @@ namespace de4dot.blocks {
}
public class FieldReferenceAndDeclaringTypeKey {
FieldReference fieldRef;
readonly FieldReference fieldRef;
public FieldReference FieldReference {
get { return fieldRef; }
@ -209,7 +255,7 @@ namespace de4dot.blocks {
}
public class MethodReferenceAndDeclaringTypeKey {
MethodReference methodRef;
readonly MethodReference methodRef;
public MethodReference MethodReference {
get { return methodRef; }
@ -296,7 +342,7 @@ namespace de4dot.blocks {
return name;
}
static bool compareScope(IMetadataScope a, IMetadataScope b) {
public static bool compareScope(IMetadataScope a, IMetadataScope b) {
if (ReferenceEquals(a, b))
return true;
if (a == null || b == null)
@ -304,7 +350,7 @@ namespace de4dot.blocks {
return getCanonicalizedScopeName(a) == getCanonicalizedScopeName(b);
}
static int scopeHashCode(IMetadataScope a) {
public static int scopeHashCode(IMetadataScope a) {
if (a == null)
return 0;
return getCanonicalizedScopeName(a).GetHashCode();

View File

@ -190,11 +190,12 @@ namespace de4dot.renamer {
void findAllMemberReferences() {
Log.v("Finding all MemberReferences");
int index = 0;
foreach (var module in modules) {
if (modules.Count > 1)
Log.v("Finding all MemberReferences ({0})", module.Filename);
Log.indent();
module.findAllMemberReferences();
module.findAllMemberReferences(ref index);
Log.deIndent();
}
}

View File

@ -289,11 +289,11 @@ namespace de4dot.renamer {
public Module module;
string newNamespace = null;
DefDict<EventDef> events = new DefDict<EventDef>();
DefDict<FieldDef> fields = new DefDict<FieldDef>();
DefDict<MethodDef> methods = new DefDict<MethodDef>();
DefDict<PropertyDef> properties = new DefDict<PropertyDef>();
DefDict<TypeDef> types = new DefDict<TypeDef>();
EventDefDict events = new EventDefDict();
FieldDefDict fields = new FieldDefDict();
MethodDefDict methods = new MethodDefDict();
PropertyDefDict properties = new PropertyDefDict();
TypeDefDict types = new TypeDefDict();
IList<GenericParamDef> genericParams;
public TypeDefinition TypeDefinition {
get { return (TypeDefinition)MemberReference; }
@ -316,11 +316,6 @@ namespace de4dot.renamer {
: base(typeDefinition, null, index) {
this.module = module;
genericParams = createGenericParamDefList(TypeDefinition.GenericParameters);
fields.HandleDupe = (newOne, oldOne) => {
if (oldOne.FieldDefinition.IsLiteral && !newOne.FieldDefinition.IsLiteral)
fields.replaceOldWithNew(oldOne, newOne);
};
}
public override bool isSame(MemberReference mr) {

View File

@ -20,6 +20,7 @@
using System;
using System.Collections.Generic;
using Mono.Cecil;
using de4dot.blocks;
namespace de4dot.renamer {
interface IResolver {
@ -34,39 +35,23 @@ namespace de4dot.renamer {
EventDef findEvent(MethodReference methodReference);
}
class MyDict<T> {
Dictionary<string, T> dict = new Dictionary<string, T>(StringComparer.Ordinal);
public T this[string key] {
get {
T t;
if (dict.TryGetValue(key, out t))
return t;
return default(T);
}
set {
dict[key] = value;
}
}
public IEnumerable<T> getAll() {
foreach (var elem in dict.Values)
yield return elem;
}
interface RefDict<TRef, TMRef> where TRef : Ref where TMRef : MemberReference {
IEnumerable<TRef> getAll();
IEnumerable<TRef> getSorted();
TRef find(TMRef tmref);
void add(TRef tref);
}
class DefDict<T> where T : Ref {
MyDict<IList<T>> dict = new MyDict<IList<T>>();
public Action<T, T> HandleDupe { get; set; }
class TypeDefDict : RefDict<TypeDef, TypeReference> {
Dictionary<ScopeAndTokenKey, TypeDef> tokenToTypeDef = new Dictionary<ScopeAndTokenKey, TypeDef>();
Dictionary<TypeReferenceKey, TypeDef> typeRefToDef = new Dictionary<TypeReferenceKey, TypeDef>();
public IEnumerable<T> getAll() {
foreach (var list in dict.getAll()) {
foreach (var t in list)
yield return t;
}
public IEnumerable<TypeDef> getAll() {
return tokenToTypeDef.Values;
}
public IEnumerable<T> getSorted() {
var list = new List<T>(getAll());
public IEnumerable<TypeDef> getSorted() {
var list = new List<TypeDef>(getAll());
list.Sort((a, b) => {
if (a.Index < b.Index) return -1;
if (a.Index > b.Index) return 1;
@ -75,46 +60,140 @@ namespace de4dot.renamer {
return list;
}
public T find(MemberReference mr) {
var list = dict[mr.Name];
if (list == null)
return null;
public TypeDef find(TypeReference typeReference) {
TypeDef typeDef;
if (tokenToTypeDef.TryGetValue(new ScopeAndTokenKey(typeReference), out typeDef))
return typeDef;
foreach (T t in list) {
if (t.isSame(mr))
return t;
}
return null;
typeRefToDef.TryGetValue(new TypeReferenceKey(typeReference), out typeDef);
return typeDef;
}
public void add(T t) {
var other = find(t.MemberReference);
if (other != null) {
handleDupe(t, other);
return;
}
public void add(TypeDef typeDef) {
tokenToTypeDef[new ScopeAndTokenKey(typeDef.TypeDefinition)] = typeDef;
typeRefToDef[new TypeReferenceKey(typeDef.TypeDefinition)] = typeDef;
}
}
var list = dict[t.MemberReference.Name];
if (list == null)
dict[t.MemberReference.Name] = list = new List<T>();
list.Add(t);
return;
class FieldDefDict : RefDict<FieldDef, FieldReference> {
Dictionary<ScopeAndTokenKey, FieldDef> tokenToFieldDef = new Dictionary<ScopeAndTokenKey, FieldDef>();
Dictionary<FieldReferenceKey, FieldDef> fieldRefToDef = new Dictionary<FieldReferenceKey, FieldDef>();
public IEnumerable<FieldDef> getAll() {
return tokenToFieldDef.Values;
}
public void replaceOldWithNew(T oldOne, T newOne) {
if (find(newOne.MemberReference) != oldOne)
throw new ApplicationException("Not same member reference");
var list = dict[oldOne.MemberReference.Name];
if (!list.Remove(oldOne))
throw new ApplicationException("Could not remove old one");
list.Add(newOne);
public IEnumerable<FieldDef> getSorted() {
var list = new List<FieldDef>(getAll());
list.Sort((a, b) => {
if (a.Index < b.Index) return -1;
if (a.Index > b.Index) return 1;
return 0;
});
return list;
}
void handleDupe(T newOne, T oldOne) {
Log.v("Duplicate MemberReference found: {0} ({1:X8} vs {2:X8})", newOne.MemberReference, newOne.MemberReference.MetadataToken.ToUInt32(), oldOne.MemberReference.MetadataToken.ToUInt32());
if (HandleDupe != null)
HandleDupe(newOne, oldOne);
public FieldDef find(FieldReference fieldReference) {
FieldDef fieldDef;
if (tokenToFieldDef.TryGetValue(new ScopeAndTokenKey(fieldReference), out fieldDef))
return fieldDef;
fieldRefToDef.TryGetValue(new FieldReferenceKey(fieldReference), out fieldDef);
return fieldDef;
}
public void add(FieldDef fieldDef) {
tokenToFieldDef[new ScopeAndTokenKey(fieldDef.FieldDefinition)] = fieldDef;
fieldRefToDef[new FieldReferenceKey(fieldDef.FieldDefinition)] = fieldDef;
}
}
class MethodDefDict : RefDict<MethodDef, MethodReference> {
Dictionary<ScopeAndTokenKey, MethodDef> tokenToMethodDef = new Dictionary<ScopeAndTokenKey, MethodDef>();
Dictionary<MethodReferenceKey, MethodDef> methodRefToDef = new Dictionary<MethodReferenceKey, MethodDef>();
public IEnumerable<MethodDef> getAll() {
return tokenToMethodDef.Values;
}
public IEnumerable<MethodDef> getSorted() {
var list = new List<MethodDef>(getAll());
list.Sort((a, b) => {
if (a.Index < b.Index) return -1;
if (a.Index > b.Index) return 1;
return 0;
});
return list;
}
public MethodDef find(MethodReference methodReference) {
MethodDef methodDef;
if (tokenToMethodDef.TryGetValue(new ScopeAndTokenKey(methodReference), out methodDef))
return methodDef;
methodRefToDef.TryGetValue(new MethodReferenceKey(methodReference), out methodDef);
return methodDef;
}
public void add(MethodDef methodDef) {
tokenToMethodDef[new ScopeAndTokenKey(methodDef.MethodDefinition)] = methodDef;
methodRefToDef[new MethodReferenceKey(methodDef.MethodDefinition)] = methodDef;
}
}
class PropertyDefDict : RefDict<PropertyDef, PropertyReference> {
Dictionary<ScopeAndTokenKey, PropertyDef> tokenToPropDef = new Dictionary<ScopeAndTokenKey, PropertyDef>();
public IEnumerable<PropertyDef> getAll() {
return tokenToPropDef.Values;
}
public IEnumerable<PropertyDef> getSorted() {
var list = new List<PropertyDef>(getAll());
list.Sort((a, b) => {
if (a.Index < b.Index) return -1;
if (a.Index > b.Index) return 1;
return 0;
});
return list;
}
public PropertyDef find(PropertyReference propertyReference) {
PropertyDef propDef;
tokenToPropDef.TryGetValue(new ScopeAndTokenKey(propertyReference), out propDef);
return propDef;
}
public void add(PropertyDef propDef) {
tokenToPropDef[new ScopeAndTokenKey(propDef.PropertyDefinition)] = propDef;
}
}
class EventDefDict : RefDict<EventDef, EventReference> {
Dictionary<ScopeAndTokenKey, EventDef> tokenToEventDef = new Dictionary<ScopeAndTokenKey, EventDef>();
public IEnumerable<EventDef> getAll() {
return tokenToEventDef.Values;
}
public IEnumerable<EventDef> getSorted() {
var list = new List<EventDef>(getAll());
list.Sort((a, b) => {
if (a.Index < b.Index) return -1;
if (a.Index > b.Index) return 1;
return 0;
});
return list;
}
public EventDef find(EventReference eventReference) {
EventDef propDef;
tokenToEventDef.TryGetValue(new ScopeAndTokenKey(eventReference), out propDef);
return propDef;
}
public void add(EventDef eventDef) {
tokenToEventDef[new ScopeAndTokenKey(eventDef.EventDefinition)] = eventDef;
}
}

View File

@ -27,7 +27,7 @@ namespace de4dot.renamer {
class Module : IResolver {
IObfuscatedFile obfuscatedFile;
MemberRefFinder memberRefFinder;
DefDict<TypeDef> allTypes = new DefDict<TypeDef>();
TypeDefDict allTypes = new TypeDefDict();
IList<RefToDef<TypeReference, TypeDefinition>> typeRefsToRename = new List<RefToDef<TypeReference, TypeDefinition>>();
IList<RefToDef<MethodReference, MethodDefinition>> methodRefsToRename = new List<RefToDef<MethodReference, MethodDefinition>>();
IList<RefToDef<FieldReference, FieldDefinition>> fieldRefsToRename = new List<RefToDef<FieldReference, FieldDefinition>>();
@ -187,7 +187,7 @@ namespace de4dot.renamer {
}
}
public void findAllMemberReferences() {
public void findAllMemberReferences(ref int typeIndex) {
memberRefFinder = new MemberRefFinder();
memberRefFinder.findAll(ModuleDefinition, ModuleDefinition.Types);
allMethods = new List<MethodDefinition>(memberRefFinder.methodDefinitions.Keys);
@ -195,7 +195,7 @@ namespace de4dot.renamer {
var allTypesList = new List<TypeDef>();
foreach (var type in new List<TypeDefinition>(memberRefFinder.typeDefinitions.Keys)) {
memberRefFinder.removeTypeDefinition(type);
var typeDef = new TypeDef(type, this);
var typeDef = new TypeDef(type, this, typeIndex++);
allTypes.add(typeDef);
allTypesList.Add(typeDef);
@ -302,7 +302,7 @@ namespace de4dot.renamer {
}
void rebuildAllTypesDict() {
var newAllTypes = new DefDict<TypeDef>();
var newAllTypes = new TypeDefDict();
foreach (var typeDef in allTypes.getAll())
newAllTypes.add(typeDef);
allTypes = newAllTypes;