de4dot-cex/de4dot.code/deobfuscators/CryptoObfuscator/MethodBodyReader.cs

127 lines
3.7 KiB
C#
Raw Normal View History

2012-08-21 21:11:14 +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-09 05:24:13 +08:00
using dot10.IO;
using dot10.DotNet;
using dot10.DotNet.Emit;
2012-08-21 21:11:14 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.CryptoObfuscator {
class MethodBodyReader : MethodBodyReaderBase {
2012-11-09 05:24:13 +08:00
ModuleDefMD module;
2012-08-21 21:11:14 +08:00
ushort maxStackSize;
2012-11-09 05:24:13 +08:00
public MethodBodyReader(ModuleDefMD module, IBinaryReader reader)
2012-08-21 21:11:14 +08:00
: base(reader) {
this.module = module;
}
public void read(MethodDef method) {
2012-11-09 05:24:13 +08:00
this.parameters = method.Parameters;
SetLocals(getLocals(method));
2012-08-21 21:11:14 +08:00
maxStackSize = (ushort)reader.ReadInt32();
2012-11-09 05:24:13 +08:00
ReadInstructionsNumBytes(reader.ReadUInt32());
2012-08-21 21:11:14 +08:00
readExceptionHandlers();
}
void readExceptionHandlers() {
int totalSize = reader.ReadInt32();
if (totalSize == 0)
return;
reader.ReadInt32();
readExceptionHandlers((totalSize - 4) / 24);
}
2012-11-09 05:24:13 +08:00
static IList<Local> getLocals(MethodDef method) {
if (method.Body == null)
return new List<Local>();
return method.Body.Variables;
2012-08-21 21:11:14 +08:00
}
2012-11-09 05:24:13 +08:00
protected override IField ReadInlineField(Instruction instr) {
return module.ResolveToken(reader.ReadUInt32()) as IField;
2012-08-21 21:11:14 +08:00
}
2012-11-09 05:24:13 +08:00
protected override IMethod ReadInlineMethod(Instruction instr) {
return module.ResolveToken(reader.ReadUInt32()) as IMethod;
2012-08-21 21:11:14 +08:00
}
2012-11-09 05:24:13 +08:00
protected override MethodSig ReadInlineSig(Instruction instr) {
var sas = module.ResolveStandAloneSig(MDToken.ToRID(reader.ReadUInt32()));
return sas == null ? null : sas.MethodSig;
2012-08-21 21:11:14 +08:00
}
2012-11-09 05:24:13 +08:00
protected override string ReadInlineString(Instruction instr) {
return module.ReadUserString(reader.ReadUInt32());
2012-08-21 21:11:14 +08:00
}
2012-11-09 05:24:13 +08:00
protected override ITokenOperand ReadInlineTok(Instruction instr) {
return module.ResolveToken(reader.ReadUInt32()) as ITokenOperand;
2012-08-21 21:11:14 +08:00
}
2012-11-09 05:24:13 +08:00
protected override ITypeDefOrRef ReadInlineType(Instruction instr) {
return module.ResolveToken(reader.ReadUInt32()) as ITypeDefOrRef;
2012-08-21 21:11:14 +08:00
}
2012-11-09 05:24:13 +08:00
void readExceptionHandlers(int numExceptionHandlers) {
exceptionHandlers = new ExceptionHandler[numExceptionHandlers];
for (int i = 0; i < exceptionHandlers.Count; i++)
exceptionHandlers[i] = readExceptionHandler();
2012-08-21 21:11:14 +08:00
}
2012-11-09 05:24:13 +08:00
ExceptionHandler readExceptionHandler() {
var eh = new ExceptionHandler((ExceptionHandlerType)reader.ReadUInt32());
2012-08-21 21:11:14 +08:00
2012-11-09 05:24:13 +08:00
uint tryOffset = reader.ReadUInt32();
eh.TryStart = GetInstructionThrow(tryOffset);
eh.TryEnd = GetInstruction(tryOffset + reader.ReadUInt32());
2012-08-21 21:11:14 +08:00
2012-11-09 05:24:13 +08:00
uint handlerOffset = reader.ReadUInt32();
eh.HandlerStart = GetInstructionThrow(handlerOffset);
eh.HandlerEnd = GetInstruction(handlerOffset + reader.ReadUInt32());
2012-08-21 21:11:14 +08:00
switch (eh.HandlerType) {
case ExceptionHandlerType.Catch:
2012-11-09 05:24:13 +08:00
eh.CatchType = module.ResolveToken(reader.ReadUInt32()) as ITypeDefOrRef;
2012-08-21 21:11:14 +08:00
break;
case ExceptionHandlerType.Filter:
2012-11-09 05:24:13 +08:00
eh.FilterStart = GetInstructionThrow(reader.ReadUInt32());
2012-08-21 21:11:14 +08:00
break;
case ExceptionHandlerType.Finally:
case ExceptionHandlerType.Fault:
default:
2012-11-09 05:24:13 +08:00
reader.ReadUInt32();
2012-08-21 21:11:14 +08:00
break;
}
return eh;
}
2012-08-22 02:17:35 +08:00
2012-11-18 15:13:51 +08:00
public override void RestoreMethod(MethodDef method) {
2012-11-09 05:24:13 +08:00
base.RestoreMethod(method);
method.Body.MaxStack = maxStackSize;
2012-08-22 02:17:35 +08:00
}
2012-08-21 21:11:14 +08:00
}
}