de4dot-cex/de4dot.code/DeobfuscatorContext.cs

114 lines
2.7 KiB
C#
Raw Normal View History

2012-04-05 03:06:10 +08:00
/*
Copyright (C) 2011-2012 de4dot@gmail.com
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-11-01 23:42:02 +08:00
using dot10.DotNet;
2012-04-05 03:06:10 +08:00
using de4dot.blocks;
namespace de4dot.code {
// "global" data and methods that is shared between all deobfuscators that deobfuscate
// assemblies at the same time.
public class DeobfuscatorContext : IDeobfuscatorContext {
Dictionary<string, object> dataDict = new Dictionary<string, object>(StringComparer.Ordinal);
public void clear() {
dataDict.Clear();
}
public void setData(string name, object data) {
dataDict[name] = data;
}
public object getData(string name) {
object value;
dataDict.TryGetValue(name, out value);
return value;
}
public void clearData(string name) {
dataDict.Remove(name);
}
2012-11-22 16:14:51 +08:00
static ITypeDefOrRef getNonGenericTypeRef(ITypeDefOrRef typeRef) {
2012-11-03 03:10:34 +08:00
var ts = typeRef as TypeSpec;
if (ts == null)
return typeRef;
2012-12-20 01:14:47 +08:00
var gis = ts.TryGetGenericInstSig();
2012-11-03 03:10:34 +08:00
if (gis == null || gis.GenericType == null)
return typeRef;
return gis.GenericType.TypeDefOrRef;
2012-04-24 17:25:39 +08:00
}
2012-11-03 03:10:34 +08:00
public TypeDef resolveType(ITypeDefOrRef type) {
2012-04-05 03:06:10 +08:00
if (type == null)
return null;
2012-11-22 16:14:51 +08:00
type = getNonGenericTypeRef(type);
2012-11-03 03:10:34 +08:00
var typeDef = type as TypeDef;
2012-04-05 03:06:10 +08:00
if (typeDef != null)
return typeDef;
2012-11-03 03:10:34 +08:00
var tr = type as TypeRef;
if (tr != null)
return tr.Resolve();
return null;
2012-04-05 03:06:10 +08:00
}
public MethodDef resolveMethod(IMethod method) {
2012-04-05 03:06:10 +08:00
if (method == null)
return null;
var md = method as MethodDef;
if (md != null)
return md;
var mr = method as MemberRef;
if (mr == null || !mr.IsMethodRef)
return null;
var type = resolveType(mr.DeclaringType);
2012-04-05 03:06:10 +08:00
if (type == null)
return null;
return type.Resolve(mr) as MethodDef;
2012-04-05 03:06:10 +08:00
}
public FieldDef resolveField(IField field) {
2012-04-05 03:06:10 +08:00
if (field == null)
return null;
var fd = field as FieldDef;
if (fd != null)
return fd;
var mr = field as MemberRef;
if (mr == null || !mr.IsFieldRef)
return null;
var type = resolveType(mr.DeclaringType);
2012-04-05 03:06:10 +08:00
if (type == null)
return null;
return type.Resolve(mr) as FieldDef;
2012-04-05 03:06:10 +08:00
}
}
}