de4dot-cex/de4dot.code/deobfuscators/CodeVeil/ResourceConverter.cs

151 lines
3.8 KiB
C#
Raw Normal View History

2012-02-11 23:46:39 +08:00
/*
2014-03-12 05:15:43 +08:00
Copyright (C) 2011-2014 de4dot@gmail.com
2012-02-11 23:46:39 +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.IO;
using dnlib.IO;
using dnlib.DotNet;
2012-02-11 23:46:39 +08:00
using de4dot.code.resources;
namespace de4dot.code.deobfuscators.CodeVeil {
class ResourceConverter {
2012-11-08 16:48:05 +08:00
ModuleDefMD module;
2012-02-11 23:46:39 +08:00
ResourceInfo[] infos;
ResourceDataCreator dataCreator;
2012-11-08 16:48:05 +08:00
public ResourceConverter(ModuleDefMD module, ResourceInfo[] infos) {
2012-02-11 23:46:39 +08:00
this.module = module;
this.dataCreator = new ResourceDataCreator(module);
this.infos = infos;
}
2013-01-19 20:03:57 +08:00
public byte[] Convert() {
2012-02-11 23:46:39 +08:00
var resources = new ResourceElementSet();
foreach (var info in infos)
2013-01-19 20:03:57 +08:00
resources.Add(Convert(info));
2012-02-11 23:46:39 +08:00
var memStream = new MemoryStream();
2013-01-19 20:03:57 +08:00
ResourceWriter.Write(module, memStream, resources);
2012-02-11 23:46:39 +08:00
return memStream.ToArray();
}
2013-01-19 20:03:57 +08:00
ResourceElement Convert(ResourceInfo info) {
2012-02-11 23:46:39 +08:00
var reader = info.dataReader;
2012-11-08 16:48:05 +08:00
reader.Position = info.offset;
2012-02-11 23:46:39 +08:00
IResourceData resourceData;
int type = (info.flags & 0x7F);
switch (type) {
case 1: // bool
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadBoolean());
2012-02-11 23:46:39 +08:00
break;
case 2: // byte
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadByte());
2012-02-11 23:46:39 +08:00
break;
case 3: // byte[]
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadBytes(info.length));
2012-02-11 23:46:39 +08:00
break;
case 4: // char[]
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadChars(info.length));
2012-02-11 23:46:39 +08:00
break;
case 5: // sbyte
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadSByte());
2012-02-11 23:46:39 +08:00
break;
case 6: // char
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadChar());
2012-02-11 23:46:39 +08:00
break;
case 7: // decimal
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadDecimal());
2012-02-11 23:46:39 +08:00
break;
case 8: // double
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadDouble());
2012-02-11 23:46:39 +08:00
break;
case 9: // short
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadInt16());
2012-02-11 23:46:39 +08:00
break;
case 10: // int
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadInt32());
2012-02-11 23:46:39 +08:00
break;
case 11: // long
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadInt64());
2012-02-11 23:46:39 +08:00
break;
case 12: // float
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadSingle());
2012-02-11 23:46:39 +08:00
break;
case 13: // string
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadString());
2012-02-11 23:46:39 +08:00
break;
case 14: // ushort
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadUInt16());
2012-02-11 23:46:39 +08:00
break;
case 15: // uint
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadUInt32());
2012-02-11 23:46:39 +08:00
break;
case 16: // ulong
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(reader.ReadUInt64());
2012-02-11 23:46:39 +08:00
break;
case 17: // DateTime
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(DateTime.FromBinary(reader.ReadInt64()));
2012-02-11 23:46:39 +08:00
break;
case 18: // TimeSpan
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.Create(TimeSpan.FromTicks(reader.ReadInt64()));
2012-02-11 23:46:39 +08:00
break;
case 19: // Icon
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.CreateIcon(reader.ReadBytes(info.length));
2012-02-11 23:46:39 +08:00
break;
case 20: // Image
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.CreateImage(reader.ReadBytes(info.length));
2012-02-11 23:46:39 +08:00
break;
case 31: // binary
2013-01-19 20:03:57 +08:00
resourceData = dataCreator.CreateSerialized(reader.ReadBytes(info.length));
2012-02-11 23:46:39 +08:00
break;
case 21: // Point (CV doesn't restore this type)
default:
throw new Exception("Unknown type");
}
return new ResourceElement() {
Name = info.name,
ResourceData = resourceData,
};
}
}
}