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

103 lines
3.4 KiB
C#
Raw Normal View History

2012-01-08 03:27:07 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 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;
2012-11-08 14:06:46 +08:00
using System.Collections.Generic;
2012-01-08 03:27:07 +08:00
using System.IO;
using dnlib.IO;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-01-08 03:27:07 +08:00
namespace de4dot.code.deobfuscators.Babel_NET {
class MethodBodyReader : MethodBodyReaderBase {
ImageReader imageReader;
public int Flags2 { get; set; }
2012-11-08 14:06:46 +08:00
public ushort MaxStack { get; set; }
2012-01-08 03:27:07 +08:00
2012-11-08 14:06:46 +08:00
public MethodBodyReader(ImageReader imageReader, IBinaryReader reader)
2012-01-08 03:27:07 +08:00
: base(reader) {
this.imageReader = imageReader;
}
2013-01-19 20:03:57 +08:00
public void Read(IList<Parameter> parameters) {
2012-01-08 03:27:07 +08:00
this.parameters = parameters;
Flags2 = reader.ReadInt16();
2012-11-08 14:06:46 +08:00
MaxStack = reader.ReadUInt16();
2013-01-19 20:03:57 +08:00
SetLocals(imageReader.ReadTypeSigs());
ReadInstructions(imageReader.ReadVariableLengthInt32());
ReadExceptionHandlers(imageReader.ReadVariableLengthInt32());
2012-01-08 03:27:07 +08:00
}
2012-11-08 14:06:46 +08:00
protected override IField ReadInlineField(Instruction instr) {
2013-01-19 20:03:57 +08:00
return imageReader.ReadFieldRef();
2012-01-08 03:27:07 +08:00
}
2012-11-08 14:06:46 +08:00
protected override IMethod ReadInlineMethod(Instruction instr) {
2013-01-19 20:03:57 +08:00
return imageReader.ReadMethodRef();
2012-01-08 03:27:07 +08:00
}
2012-11-08 14:06:46 +08:00
protected override MethodSig ReadInlineSig(Instruction instr) {
2013-01-19 20:03:57 +08:00
return imageReader.ReadCallSite();
2012-01-08 03:27:07 +08:00
}
2012-11-08 14:06:46 +08:00
protected override string ReadInlineString(Instruction instr) {
2013-01-19 20:03:57 +08:00
return imageReader.ReadString();
2012-01-08 03:27:07 +08:00
}
2012-11-08 14:06:46 +08:00
protected override ITokenOperand ReadInlineTok(Instruction instr) {
2012-01-08 03:27:07 +08:00
switch (reader.ReadByte()) {
2013-01-19 20:03:57 +08:00
case 0: return imageReader.ReadTypeSig().ToTypeDefOrRef();
case 1: return imageReader.ReadFieldRef();
case 2: return imageReader.ReadMethodRef();
2012-01-08 03:27:07 +08:00
default: throw new ApplicationException("Unknown token type");
}
}
2012-11-08 14:06:46 +08:00
protected override ITypeDefOrRef ReadInlineType(Instruction instr) {
2013-01-19 20:03:57 +08:00
return imageReader.ReadTypeSig().ToTypeDefOrRef();
2012-01-08 03:27:07 +08:00
}
2013-01-19 20:03:57 +08:00
void ReadExceptionHandlers(int numExceptionHandlers) {
2012-11-08 14:06:46 +08:00
exceptionHandlers = new List<ExceptionHandler>(numExceptionHandlers);
for (int i = 0; i < numExceptionHandlers; i++)
2013-01-19 20:03:57 +08:00
Add(ReadExceptionHandler());
2012-11-08 14:06:46 +08:00
}
2013-01-19 20:03:57 +08:00
ExceptionHandler ReadExceptionHandler() {
2012-01-08 03:27:07 +08:00
var ehType = (ExceptionHandlerType)reader.ReadByte();
2013-01-19 20:03:57 +08:00
uint tryOffset = imageReader.ReadVariableLengthUInt32();
uint tryLength = imageReader.ReadVariableLengthUInt32();
uint handlerOffset = imageReader.ReadVariableLengthUInt32();
uint handlerLength = imageReader.ReadVariableLengthUInt32();
var catchType = imageReader.ReadTypeSig().ToTypeDefOrRef();
uint filterOffset = imageReader.ReadVariableLengthUInt32();
2012-01-08 03:27:07 +08:00
var eh = new ExceptionHandler(ehType);
2012-11-08 14:06:46 +08:00
eh.TryStart = GetInstructionThrow(tryOffset);
eh.TryEnd = GetInstruction(tryOffset + tryLength);
2012-01-08 03:27:07 +08:00
if (ehType == ExceptionHandlerType.Filter)
2012-11-08 14:06:46 +08:00
eh.FilterStart = GetInstructionThrow(filterOffset);
eh.HandlerStart = GetInstructionThrow(handlerOffset);
eh.HandlerEnd = GetInstruction(handlerOffset + handlerLength);
2012-01-08 03:27:07 +08:00
eh.CatchType = catchType;
return eh;
}
}
}