2012-01-08 03:27:07 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 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;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.IO;
|
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
using CR = System.Runtime.InteropServices;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using DR = dnlib.DotNet;
|
2012-11-08 14:06:46 +08:00
|
|
|
|
|
2012-01-08 03:27:07 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.Babel_NET {
|
2012-04-05 03:06:10 +08:00
|
|
|
|
class ImageReader {
|
2012-01-08 03:27:07 +08:00
|
|
|
|
static int METHODS_SIG = 0x0000BEBA;
|
|
|
|
|
static int METADATA_SIG = 0x0100BEBA;
|
|
|
|
|
static int METHOD_NAMES_SIG = 0x0200BEBA;
|
|
|
|
|
static int ASSEMBLY_NAMES_SIG = 0x0201BEBA;
|
|
|
|
|
static int TYPEREFS_SIG = 0x0202BEBA;
|
|
|
|
|
static int STRINGS_SIG = 0x0203BEBA;
|
|
|
|
|
|
|
|
|
|
enum TypeId : byte {
|
|
|
|
|
TypeRef = 0,
|
|
|
|
|
GenericInstance = 1,
|
|
|
|
|
Pointer = 2,
|
|
|
|
|
Array = 3,
|
|
|
|
|
ByRef = 4,
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
ModuleDefMD module;
|
|
|
|
|
IBinaryReader reader;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
string[] strings;
|
2012-11-08 14:06:46 +08:00
|
|
|
|
AssemblyRef[] assemblyNames;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
Dictionary<string, int> methodOffsets;
|
2012-11-22 16:14:51 +08:00
|
|
|
|
List<TypeSig> typeRefs;
|
|
|
|
|
MemberRefConverter memberRefConverter;
|
2012-04-05 03:06:10 +08:00
|
|
|
|
IDeobfuscatorContext deobfuscatorContext;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
public ImageReader(IDeobfuscatorContext deobfuscatorContext, ModuleDefMD module, byte[] data) {
|
2012-04-05 03:06:10 +08:00
|
|
|
|
this.deobfuscatorContext = deobfuscatorContext;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
this.module = module;
|
2012-11-08 14:06:46 +08:00
|
|
|
|
this.reader = MemoryImageStream.Create(data);
|
2012-11-22 16:14:51 +08:00
|
|
|
|
this.memberRefConverter = new MemberRefConverter(module);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool Initialize() {
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (reader.ReadInt32() != METHODS_SIG)
|
|
|
|
|
return false;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
int metadataOffset = GetMetadataOffset();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (metadataOffset < 0)
|
|
|
|
|
return false;
|
2012-06-07 03:16:18 +08:00
|
|
|
|
long pos = metadataOffset + 4;
|
2012-11-08 14:06:46 +08:00
|
|
|
|
reader.Position = pos;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
int version = reader.ReadInt16(); // major, minor
|
2012-06-07 03:16:18 +08:00
|
|
|
|
if (version == 0x0001) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
InitializeV10();
|
2012-06-07 03:16:18 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
reader.Position = pos;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
InitializeV55();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void InitializeV10() {
|
2012-01-08 03:27:07 +08:00
|
|
|
|
reader.ReadInt16();
|
|
|
|
|
int methodNamesOffset = (int)reader.ReadInt64();
|
2012-11-22 16:14:51 +08:00
|
|
|
|
int typeRefsOffset = (int)reader.ReadInt64();
|
|
|
|
|
int assemblyRefsOffset = (int)reader.ReadInt64();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
int stringsOffset = (int)reader.ReadInt64();
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
InitializeStrings(stringsOffset);
|
|
|
|
|
InitializeAssemblyNames(assemblyRefsOffset);
|
|
|
|
|
InitializeMethodNames(methodNamesOffset);
|
|
|
|
|
InitializeTypeRefs(typeRefsOffset);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void InitializeV55() {
|
2012-06-07 03:16:18 +08:00
|
|
|
|
int methodNamesOffset = (int)reader.ReadInt64() ^ METADATA_SIG;
|
2012-11-22 16:14:51 +08:00
|
|
|
|
int typeRefsOffset = (int)reader.ReadInt64() ^ (METADATA_SIG << 1);
|
|
|
|
|
int assemblyRefsOffset = (int)reader.ReadInt64() ^ ((METADATA_SIG << 1) + 1);
|
2012-06-07 03:16:18 +08:00
|
|
|
|
int stringsOffset = (int)reader.ReadInt64() ^ (((METADATA_SIG << 1) + 1) << 1);
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
InitializeStrings(stringsOffset);
|
|
|
|
|
InitializeAssemblyNames(assemblyRefsOffset);
|
|
|
|
|
InitializeMethodNames(methodNamesOffset);
|
|
|
|
|
InitializeTypeRefs(typeRefsOffset);
|
2012-06-07 03:16:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Restore(string name, MethodDef method) {
|
|
|
|
|
var babelMethod = GetMethod(name);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
var body = method.Body;
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
body.MaxStack = babelMethod.MaxStack;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
body.InitLocals = babelMethod.InitLocals;
|
|
|
|
|
|
2012-11-20 00:58:34 +08:00
|
|
|
|
body.Variables.Clear();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
foreach (var local in babelMethod.Locals)
|
2012-11-20 00:58:34 +08:00
|
|
|
|
body.Variables.Add(local);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
|
|
|
|
var toNewOperand = new Dictionary<object, object>();
|
|
|
|
|
if (babelMethod.ThisParameter != null)
|
2012-11-08 14:06:46 +08:00
|
|
|
|
toNewOperand[babelMethod.ThisParameter] = method.Parameters[0];
|
|
|
|
|
for (int i = 0; i < babelMethod.Parameters.Length; i++)
|
|
|
|
|
toNewOperand[babelMethod.Parameters[i]] = method.Parameters[i + method.Parameters.MethodSigIndexBase];
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
|
|
|
|
body.Instructions.Clear();
|
|
|
|
|
foreach (var instr in babelMethod.Instructions) {
|
|
|
|
|
object newOperand;
|
|
|
|
|
if (instr.Operand != null && toNewOperand.TryGetValue(instr.Operand, out newOperand))
|
|
|
|
|
instr.Operand = newOperand;
|
|
|
|
|
body.Instructions.Add(instr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body.ExceptionHandlers.Clear();
|
|
|
|
|
foreach (var eh in babelMethod.ExceptionHandlers)
|
|
|
|
|
body.ExceptionHandlers.Add(eh);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
BabelMethodDef GetMethod(string name) {
|
2012-01-08 03:27:07 +08:00
|
|
|
|
int offset = methodOffsets[name];
|
|
|
|
|
methodOffsets.Remove(name);
|
2012-11-08 14:06:46 +08:00
|
|
|
|
reader.Position = offset;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
return new MethodDefReader(this, reader).Read();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public string ReadString() {
|
|
|
|
|
return strings[ReadVariableLengthInt32()];
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public TypeSig ReadTypeSig() {
|
|
|
|
|
return typeRefs[ReadVariableLengthInt32()];
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public TypeSig[] ReadTypeSigs() {
|
|
|
|
|
var refs = new TypeSig[ReadVariableLengthInt32()];
|
2012-01-08 03:27:07 +08:00
|
|
|
|
for (int i = 0; i < refs.Length; i++)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
refs[i] = ReadTypeSig();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
return refs;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public IField ReadFieldRef() {
|
|
|
|
|
var name = ReadString();
|
|
|
|
|
var declaringType = ReadTypeSig();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var fields = GetFields(Resolve(declaringType), name);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (fields == null || fields.Count != 1) {
|
|
|
|
|
throw new ApplicationException(string.Format("Couldn't find one field named '{0}' in type {1}",
|
|
|
|
|
name,
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Utils.RemoveNewlines(declaringType)));
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
return memberRefConverter.Convert(fields[0]);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static List<FieldDef> GetFields(TypeDef type, string name) {
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (type == null)
|
|
|
|
|
return null;
|
2012-11-08 14:06:46 +08:00
|
|
|
|
return new List<FieldDef>(type.FindFields(name));
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public IMethod ReadMethodRef() {
|
|
|
|
|
var babelMethodRef = new MethodRefReader(this, reader).Read();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var method = GetMethodRef(babelMethodRef);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (method == null) {
|
|
|
|
|
throw new ApplicationException(string.Format("Could not find method '{0}' in type '{1}'",
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Utils.RemoveNewlines(babelMethodRef.Name),
|
|
|
|
|
Utils.RemoveNewlines(babelMethodRef.DeclaringType)));
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
var git = babelMethodRef.DeclaringType.ToGenericInstSig();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (git == null)
|
|
|
|
|
return method;
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
var mr = new MemberRefUser(module, method.Name, method.MethodSig.Clone(), babelMethodRef.DeclaringType.ToTypeDefOrRef());
|
|
|
|
|
return module.UpdateRowId(mr);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
IMethod GetMethodRef(BabelMethodreference babelMethodRef) {
|
|
|
|
|
var declaringType = Resolve(babelMethodRef.DeclaringType);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (declaringType == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var methods = GetMethods(declaringType, babelMethodRef);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (methods.Count != 1) {
|
|
|
|
|
throw new ApplicationException(string.Format("Couldn't find one method named '{0}' in type {1}",
|
|
|
|
|
babelMethodRef.Name,
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Utils.RemoveNewlines(declaringType)));
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return methods[0];
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
List<IMethod> GetMethods(TypeDef declaringType, BabelMethodreference babelMethodRef) {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
var methods = new List<IMethod>();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
var gis = babelMethodRef.DeclaringType as GenericInstSig;
|
|
|
|
|
var gim = babelMethodRef.GenericArguments;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
foreach (var method in declaringType.Methods) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (CompareMethod(GenericArgsSubstitutor.Create(method, gis, gim), babelMethodRef)) {
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (!babelMethodRef.IsGenericMethod)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
methods.Add(memberRefConverter.Convert(method));
|
2012-01-08 03:27:07 +08:00
|
|
|
|
else {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
var gim2 = new GenericInstMethodSig(babelMethodRef.GenericArguments);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var ms = module.UpdateRowId(new MethodSpecUser(memberRefConverter.Convert(method), gim2));
|
2012-11-08 14:06:46 +08:00
|
|
|
|
methods.Add(ms);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return methods;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool CompareMethod(IMethod method, BabelMethodreference babelMethodRef) {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
var sig = method.MethodSig;
|
|
|
|
|
if (sig.Params.Count != babelMethodRef.Parameters.Length)
|
2012-01-08 03:27:07 +08:00
|
|
|
|
return false;
|
|
|
|
|
if (method.Name != babelMethodRef.Name)
|
|
|
|
|
return false;
|
2012-11-08 14:06:46 +08:00
|
|
|
|
if (sig.HasThis != babelMethodRef.HasThis)
|
2012-01-08 03:27:07 +08:00
|
|
|
|
return false;
|
2012-11-08 14:06:46 +08:00
|
|
|
|
if (sig.GenParamCount != babelMethodRef.GenericArguments.Length)
|
2012-01-08 03:27:07 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
if (!new SigComparer().Equals(sig.RetType, babelMethodRef.ReturnType))
|
2012-01-08 03:27:07 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < babelMethodRef.Parameters.Length; i++) {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
if (!new SigComparer().Equals(sig.Params[i], babelMethodRef.Parameters[i].Type))
|
2012-01-08 03:27:07 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
TypeDef Resolve(TypeSig type) {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
type = type.RemovePinnedAndModifiers();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
var gis = type as GenericInstSig;
|
|
|
|
|
if (gis != null)
|
|
|
|
|
type = gis.GenericType;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
var tdrs = type as TypeDefOrRefSig;
|
|
|
|
|
if (tdrs == null)
|
|
|
|
|
return null;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
var td = tdrs.TypeDef;
|
|
|
|
|
if (td != null)
|
|
|
|
|
return td;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
var tr = tdrs.TypeRef;
|
|
|
|
|
if (tr != null)
|
|
|
|
|
return tr.Resolve();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2012-11-08 14:06:46 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public MethodSig ReadCallSite() {
|
|
|
|
|
var returnType = ReadTypeSig();
|
|
|
|
|
var paramTypes = ReadTypeSigs();
|
2012-11-08 14:06:46 +08:00
|
|
|
|
var callingConvention = (CR.CallingConvention)reader.ReadInt32();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
return new MethodSig(ConvertCallingConvention(callingConvention), 0, returnType, paramTypes);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static DR.CallingConvention ConvertCallingConvention(CR.CallingConvention callingConvention) {
|
2012-01-08 03:27:07 +08:00
|
|
|
|
switch (callingConvention) {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
case CR.CallingConvention.Winapi: return DR.CallingConvention.Default;
|
|
|
|
|
case CR.CallingConvention.Cdecl: return DR.CallingConvention.C;
|
|
|
|
|
case CR.CallingConvention.StdCall: return DR.CallingConvention.StdCall;
|
|
|
|
|
case CR.CallingConvention.ThisCall: return DR.CallingConvention.ThisCall;
|
|
|
|
|
case CR.CallingConvention.FastCall: return DR.CallingConvention.FastCall;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
default: throw new ApplicationException(string.Format("Unknown CallingConvention {0}", callingConvention));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void InitializeStrings(int headerOffset) {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
reader.Position = headerOffset;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (reader.ReadInt32() != STRINGS_SIG)
|
|
|
|
|
throw new ApplicationException("Invalid strings sig");
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
strings = new string[ReadVariableLengthInt32()];
|
2012-01-08 03:27:07 +08:00
|
|
|
|
for (int i = 0; i < strings.Length; i++)
|
|
|
|
|
strings[i] = reader.ReadString();
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void InitializeAssemblyNames(int headerOffset) {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
reader.Position = headerOffset;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (reader.ReadInt32() != ASSEMBLY_NAMES_SIG)
|
|
|
|
|
throw new ApplicationException("Invalid assembly names sig");
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
assemblyNames = new AssemblyRef[ReadVariableLengthInt32()];
|
2012-01-08 03:27:07 +08:00
|
|
|
|
for (int i = 0; i < assemblyNames.Length; i++)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
assemblyNames[i] = module.UpdateRowId(new AssemblyRefUser(new AssemblyNameInfo(ReadString())));
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void InitializeMethodNames(int headerOffset) {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
reader.Position = headerOffset;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (reader.ReadInt32() != METHOD_NAMES_SIG)
|
|
|
|
|
throw new ApplicationException("Invalid methods sig");
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
int numMethods = ReadVariableLengthInt32();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
methodOffsets = new Dictionary<string, int>(numMethods, StringComparer.Ordinal);
|
|
|
|
|
for (int i = 0; i < numMethods; i++) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var methodName = ReadString();
|
|
|
|
|
methodOffsets[methodName] = ReadVariableLengthInt32();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void InitializeTypeRefs(int headerOffset) {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
reader.Position = headerOffset;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (reader.ReadInt32() != TYPEREFS_SIG)
|
|
|
|
|
throw new ApplicationException("Invalid typerefs sig");
|
|
|
|
|
|
|
|
|
|
int numTypeRefs = reader.ReadInt32();
|
2012-11-22 16:14:51 +08:00
|
|
|
|
typeRefs = new List<TypeSig>(numTypeRefs + 1);
|
|
|
|
|
typeRefs.Add(null);
|
2012-11-08 14:06:46 +08:00
|
|
|
|
var genericArgFixes = new Dictionary<GenericInstSig, List<int>>();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
for (int i = 0; i < numTypeRefs; i++) {
|
|
|
|
|
TypeId typeId = (TypeId)reader.ReadByte();
|
|
|
|
|
switch (typeId) {
|
|
|
|
|
case TypeId.TypeRef:
|
2013-01-19 20:03:57 +08:00
|
|
|
|
typeRefs.Add(ReadTypeRef());
|
2012-01-08 03:27:07 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TypeId.GenericInstance:
|
|
|
|
|
List<int> genericArgs;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var git = ReadGenericInstanceType(out genericArgs);
|
2012-11-22 16:14:51 +08:00
|
|
|
|
typeRefs.Add(git);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
genericArgFixes[git] = genericArgs;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TypeId.Pointer:
|
2013-01-19 20:03:57 +08:00
|
|
|
|
typeRefs.Add(ReadPointerType());
|
2012-01-08 03:27:07 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TypeId.Array:
|
2013-01-19 20:03:57 +08:00
|
|
|
|
typeRefs.Add(ReadArrayType());
|
2012-01-08 03:27:07 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TypeId.ByRef:
|
2013-01-19 20:03:57 +08:00
|
|
|
|
typeRefs.Add(ReadByRefType());
|
2012-01-08 03:27:07 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new ApplicationException(string.Format("Unknown type id {0}", (int)typeId));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var kv in genericArgFixes) {
|
|
|
|
|
var git = kv.Key;
|
|
|
|
|
foreach (var typeNum in kv.Value)
|
2012-11-22 16:14:51 +08:00
|
|
|
|
git.GenericArguments.Add(typeRefs[typeNum]);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
TypeSig ReadTypeRef() {
|
2012-01-08 03:27:07 +08:00
|
|
|
|
string ns, name;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
ParseReflectionTypeName(ReadString(), out ns, out name);
|
|
|
|
|
var asmRef = assemblyNames[ReadVariableLengthInt32()];
|
|
|
|
|
var declaringType = ReadTypeSig();
|
2012-11-22 16:14:51 +08:00
|
|
|
|
var typeRef = new TypeRefUser(module, ns, name);
|
2012-11-08 14:06:46 +08:00
|
|
|
|
if (declaringType != null)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
typeRef.ResolutionScope = GetTypeRef(declaringType);
|
2012-11-08 14:06:46 +08:00
|
|
|
|
else
|
2012-11-22 16:14:51 +08:00
|
|
|
|
typeRef.ResolutionScope = asmRef;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
return memberRefConverter.Convert(typeRef);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
TypeRef GetTypeRef(TypeSig type) {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
var tdr = type as TypeDefOrRefSig;
|
|
|
|
|
if (tdr == null)
|
|
|
|
|
throw new ApplicationException("Not a type ref");
|
|
|
|
|
if (tdr.TypeRef != null)
|
|
|
|
|
return tdr.TypeRef;
|
|
|
|
|
var td = tdr.TypeDef;
|
|
|
|
|
if (td != null)
|
|
|
|
|
return new Importer(module).Import(td) as TypeRef;
|
|
|
|
|
throw new ApplicationException("Not a type ref");
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static void ParseReflectionTypeName(string fullName, out string ns, out string name) {
|
|
|
|
|
int index = GetLastChar(fullName, '.');
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (index < 0) {
|
|
|
|
|
ns = "";
|
|
|
|
|
name = fullName;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
ns = UnEscape(fullName.Substring(0, index));
|
2012-01-08 03:27:07 +08:00
|
|
|
|
name = fullName.Substring(index + 1);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
index = GetLastChar(name, '+');
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (index < 0)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
name = UnEscape(name);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
else {
|
|
|
|
|
ns = "";
|
2013-01-19 20:03:57 +08:00
|
|
|
|
name = UnEscape(name.Substring(index + 1));
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static int GetLastChar(string name, char c) {
|
2012-01-08 03:27:07 +08:00
|
|
|
|
if (string.IsNullOrEmpty(name))
|
|
|
|
|
return -1;
|
|
|
|
|
int index = name.Length - 1;
|
|
|
|
|
while (true) {
|
|
|
|
|
index = name.LastIndexOf(c, index);
|
|
|
|
|
if (index < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
if (index == 0)
|
|
|
|
|
return index;
|
|
|
|
|
if (name[index - 1] != '\\')
|
|
|
|
|
return index;
|
|
|
|
|
index--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static string UnEscape(string s) {
|
2012-01-08 03:27:07 +08:00
|
|
|
|
var sb = new StringBuilder(s.Length);
|
|
|
|
|
for (int i = 0; i < s.Length; i++) {
|
|
|
|
|
if (s[i] == '\\' && i + 1 < s.Length)
|
|
|
|
|
i++;
|
|
|
|
|
sb.Append(s[i]);
|
|
|
|
|
}
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
GenericInstSig ReadGenericInstanceType(out List<int> genericArgs) {
|
|
|
|
|
var git = new GenericInstSig(ReadTypeSig() as ClassOrValueTypeSig);
|
|
|
|
|
int numArgs = ReadVariableLengthInt32();
|
2012-01-08 03:27:07 +08:00
|
|
|
|
genericArgs = new List<int>(numArgs);
|
|
|
|
|
for (int i = 0; i < numArgs; i++)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
genericArgs.Add(ReadVariableLengthInt32());
|
2012-01-08 03:27:07 +08:00
|
|
|
|
return git;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
PtrSig ReadPointerType() {
|
|
|
|
|
return new PtrSig(ReadTypeSig());
|
2012-11-08 14:06:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
TypeSig ReadArrayType() {
|
|
|
|
|
var typeSig = ReadTypeSig();
|
|
|
|
|
int rank = ReadVariableLengthInt32();
|
2012-11-08 14:06:46 +08:00
|
|
|
|
if (rank == 1)
|
|
|
|
|
return new SZArraySig(typeSig);
|
|
|
|
|
return new ArraySig(typeSig, rank);
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
ByRefSig ReadByRefType() {
|
|
|
|
|
return new ByRefSig(ReadTypeSig());
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public uint ReadVariableLengthUInt32() {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
uint val;
|
|
|
|
|
reader.ReadCompressedUInt32(out val);
|
|
|
|
|
return val;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public int ReadVariableLengthInt32() {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
uint val;
|
|
|
|
|
reader.ReadCompressedUInt32(out val);
|
|
|
|
|
return (int)val;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
int GetMetadataOffset() {
|
2012-11-08 14:06:46 +08:00
|
|
|
|
reader.Position = reader.Length - 4;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
for (int i = 0; i < 30; i++) {
|
|
|
|
|
if (reader.ReadInt32() == METADATA_SIG)
|
2012-11-08 14:06:46 +08:00
|
|
|
|
return (int)reader.Position - 4;
|
|
|
|
|
reader.Position -= 8;
|
2012-01-08 03:27:07 +08:00
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|