2012-05-03 22:47:34 +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;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
using dot10.DotNet;
|
|
|
|
|
using dot10.DotNet.Emit;
|
2012-11-04 07:50:24 +08:00
|
|
|
|
using dot10.IO;
|
2012-05-03 22:47:34 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
using de4dot.code.resources;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.renamer {
|
|
|
|
|
class ResourceKeysRenamer {
|
2012-05-09 23:28:52 +08:00
|
|
|
|
const int RESOURCE_KEY_MAX_LEN = 50;
|
|
|
|
|
const string DEFAULT_KEY_NAME = "Key";
|
|
|
|
|
|
2012-11-04 07:50:24 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-05-03 22:47:34 +08:00
|
|
|
|
INameChecker nameChecker;
|
|
|
|
|
Dictionary<string, bool> newNames = new Dictionary<string, bool>();
|
|
|
|
|
|
2012-11-04 07:50:24 +08:00
|
|
|
|
public ResourceKeysRenamer(ModuleDefMD module, INameChecker nameChecker) {
|
2012-05-03 22:47:34 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
this.nameChecker = nameChecker;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void rename() {
|
2012-05-03 23:24:59 +08:00
|
|
|
|
Log.v("Renaming resource keys ({0})", module);
|
2012-05-03 22:47:34 +08:00
|
|
|
|
Log.indent();
|
|
|
|
|
foreach (var type in module.GetTypes()) {
|
|
|
|
|
string resourceName = getResourceName(type);
|
|
|
|
|
if (resourceName == null)
|
|
|
|
|
continue;
|
2012-05-03 23:24:59 +08:00
|
|
|
|
var resource = getResource(resourceName);
|
2012-05-03 22:47:34 +08:00
|
|
|
|
if (resource == null) {
|
2012-05-03 23:24:59 +08:00
|
|
|
|
Log.w("Could not find resource {0}", Utils.removeNewlines(resourceName));
|
2012-05-03 22:47:34 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Log.v("Resource: {0}", Utils.toCsharpString(resource.Name));
|
|
|
|
|
Log.indent();
|
|
|
|
|
rename(type, resource);
|
|
|
|
|
Log.deIndent();
|
|
|
|
|
}
|
|
|
|
|
Log.deIndent();
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-03 23:24:59 +08:00
|
|
|
|
EmbeddedResource getResource(string resourceName) {
|
|
|
|
|
var resource = DotNetUtils.getResource(module, resourceName + ".resources") as EmbeddedResource;
|
|
|
|
|
if (resource != null)
|
|
|
|
|
return resource;
|
|
|
|
|
|
|
|
|
|
string name = "";
|
|
|
|
|
var pieces = resourceName.Split('.');
|
|
|
|
|
Array.Reverse(pieces);
|
|
|
|
|
foreach (var piece in pieces) {
|
|
|
|
|
name = piece + name;
|
|
|
|
|
resource = DotNetUtils.getResource(module, name + ".resources") as EmbeddedResource;
|
|
|
|
|
if (resource != null)
|
|
|
|
|
return resource;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
static string getResourceName(TypeDef type) {
|
2012-05-03 22:47:34 +08:00
|
|
|
|
foreach (var method in type.Methods) {
|
2012-11-04 07:50:24 +08:00
|
|
|
|
if (method.CilBody == null)
|
2012-05-03 22:47:34 +08:00
|
|
|
|
continue;
|
2012-11-04 07:50:24 +08:00
|
|
|
|
var instrs = method.CilBody.Instructions;
|
2012-05-03 22:47:34 +08:00
|
|
|
|
string resourceName = null;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
|
|
|
|
var instr = instrs[i];
|
|
|
|
|
if (instr.OpCode.Code == Code.Ldstr) {
|
|
|
|
|
resourceName = instr.Operand as string;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (instr.OpCode.Code == Code.Newobj) {
|
2012-11-04 07:50:24 +08:00
|
|
|
|
var ctor = instr.Operand as IMethod;
|
2012-05-03 22:47:34 +08:00
|
|
|
|
if (ctor.FullName != "System.Void System.Resources.ResourceManager::.ctor(System.String,System.Reflection.Assembly)")
|
|
|
|
|
continue;
|
|
|
|
|
if (resourceName == null) {
|
|
|
|
|
Log.w("Could not find resource name");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-03 23:24:59 +08:00
|
|
|
|
return resourceName;
|
2012-05-03 22:47:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class RenameInfo {
|
|
|
|
|
public readonly ResourceElement element;
|
|
|
|
|
public string newName;
|
|
|
|
|
public bool foundInCode;
|
|
|
|
|
public RenameInfo(ResourceElement element, string newName) {
|
|
|
|
|
this.element = element;
|
|
|
|
|
this.newName = newName;
|
|
|
|
|
this.foundInCode = false;
|
|
|
|
|
}
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
return string.Format("{0} => {1}", element, newName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
void rename(TypeDef type, EmbeddedResource resource) {
|
2012-05-03 22:47:34 +08:00
|
|
|
|
newNames.Clear();
|
2012-11-04 07:50:24 +08:00
|
|
|
|
var resourceSet = ResourceReader.read(module, resource.Data);
|
2012-05-03 22:47:34 +08:00
|
|
|
|
var renamed = new List<RenameInfo>();
|
|
|
|
|
foreach (var elem in resourceSet.ResourceElements) {
|
2012-05-11 00:41:21 +08:00
|
|
|
|
if (nameChecker.isValidResourceKeyName(elem.Name)) {
|
|
|
|
|
newNames.Add(elem.Name, true);
|
2012-05-03 22:47:34 +08:00
|
|
|
|
continue;
|
2012-05-11 00:41:21 +08:00
|
|
|
|
}
|
2012-05-03 22:47:34 +08:00
|
|
|
|
|
|
|
|
|
renamed.Add(new RenameInfo(elem, getNewName(elem)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (renamed.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
rename(type, renamed);
|
|
|
|
|
|
|
|
|
|
var outStream = new MemoryStream();
|
|
|
|
|
ResourceWriter.write(module, outStream, resourceSet);
|
2012-11-04 07:50:24 +08:00
|
|
|
|
var newResource = new EmbeddedResource(resource.Name, outStream.ToArray(), resource.Flags);
|
2012-05-03 22:47:34 +08:00
|
|
|
|
int resourceIndex = module.Resources.IndexOf(resource);
|
|
|
|
|
if (resourceIndex < 0)
|
|
|
|
|
throw new ApplicationException("Could not find index of resource");
|
|
|
|
|
module.Resources[resourceIndex] = newResource;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
void rename(TypeDef type, List<RenameInfo> renamed) {
|
2012-05-03 22:47:34 +08:00
|
|
|
|
var nameToInfo = new Dictionary<string, RenameInfo>(StringComparer.Ordinal);
|
|
|
|
|
foreach (var info in renamed)
|
|
|
|
|
nameToInfo[info.element.Name] = info;
|
|
|
|
|
|
|
|
|
|
foreach (var method in type.Methods) {
|
2012-11-04 07:50:24 +08:00
|
|
|
|
if (method.CilBody == null)
|
2012-05-03 22:47:34 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-04 07:50:24 +08:00
|
|
|
|
var instrs = method.CilBody.Instructions;
|
2012-05-03 22:47:34 +08:00
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
|
|
|
|
var call = instrs[i];
|
|
|
|
|
if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt)
|
|
|
|
|
continue;
|
2012-11-04 07:50:24 +08:00
|
|
|
|
var calledMethod = call.Operand as IMethod;
|
2012-05-03 22:47:34 +08:00
|
|
|
|
if (calledMethod == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
int ldstrIndex;
|
|
|
|
|
switch (calledMethod.FullName) {
|
|
|
|
|
case "System.String System.Resources.ResourceManager::GetString(System.String,System.Globalization.CultureInfo)":
|
2012-05-09 23:28:52 +08:00
|
|
|
|
case "System.IO.UnmanagedMemoryStream System.Resources.ResourceManager::GetStream(System.String,System.Globalization.CultureInfo)":
|
2012-05-03 22:47:34 +08:00
|
|
|
|
case "System.Object System.Resources.ResourceManager::GetObject(System.String,System.Globalization.CultureInfo)":
|
|
|
|
|
ldstrIndex = i - 2;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "System.String System.Resources.ResourceManager::GetString(System.String)":
|
2012-05-09 23:28:52 +08:00
|
|
|
|
case "System.IO.UnmanagedMemoryStream System.Resources.ResourceManager::GetStream(System.String)":
|
2012-05-03 22:47:34 +08:00
|
|
|
|
case "System.Object System.Resources.ResourceManager::GetObject(System.String)":
|
|
|
|
|
ldstrIndex = i - 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Instruction ldstr = null;
|
|
|
|
|
string name = null;
|
|
|
|
|
if (ldstrIndex >= 0)
|
|
|
|
|
ldstr = instrs[ldstrIndex];
|
|
|
|
|
if (ldstr == null || (name = ldstr.Operand as string) == null) {
|
|
|
|
|
Log.w("Could not find string argument to method {0}", calledMethod);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RenameInfo info;
|
2012-05-03 23:24:59 +08:00
|
|
|
|
if (!nameToInfo.TryGetValue(name, out info))
|
|
|
|
|
continue; // should not be renamed
|
2012-05-03 22:47:34 +08:00
|
|
|
|
|
|
|
|
|
ldstr.Operand = info.newName;
|
|
|
|
|
Log.v("Renamed resource key {0} => {1}", Utils.toCsharpString(info.element.Name), Utils.toCsharpString(info.newName));
|
|
|
|
|
info.element.Name = info.newName;
|
|
|
|
|
info.foundInCode = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var info in renamed) {
|
|
|
|
|
if (!info.foundInCode)
|
|
|
|
|
Log.w("Could not find resource key {0} in code", Utils.removeNewlines(info.element.Name));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string getNewName(ResourceElement elem) {
|
|
|
|
|
if (elem.ResourceData.Code != ResourceTypeCode.String)
|
|
|
|
|
return createDefaultName();
|
|
|
|
|
var stringData = (BuiltInResourceData)elem.ResourceData;
|
2012-05-09 23:28:52 +08:00
|
|
|
|
var name = createPrefixFromStringData((string)stringData.Data);
|
|
|
|
|
return createName(counter => counter == 0 ? name : string.Format("{0}_{1}", name, counter));
|
2012-05-03 22:47:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string createPrefixFromStringData(string data) {
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
data = data.Substring(0, Math.Min(data.Length, 100));
|
|
|
|
|
data = Regex.Replace(data, "[`'\"]", "");
|
2012-05-09 23:28:52 +08:00
|
|
|
|
data = Regex.Replace(data, @"[^\w]+", " ");
|
2012-05-03 22:47:34 +08:00
|
|
|
|
foreach (var piece in data.Split(' ')) {
|
|
|
|
|
if (piece.Length == 0)
|
|
|
|
|
continue;
|
|
|
|
|
var piece2 = piece.Substring(0, 1).ToUpperInvariant() + piece.Substring(1).ToLowerInvariant();
|
2012-05-09 23:28:52 +08:00
|
|
|
|
int maxLen = RESOURCE_KEY_MAX_LEN - sb.Length;
|
2012-05-03 22:47:34 +08:00
|
|
|
|
if (maxLen <= 0)
|
|
|
|
|
break;
|
|
|
|
|
if (piece2.Length > maxLen)
|
|
|
|
|
piece2 = piece2.Substring(0, maxLen);
|
|
|
|
|
sb.Append(piece2);
|
|
|
|
|
}
|
|
|
|
|
if (sb.Length <= 3)
|
|
|
|
|
return createDefaultName();
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string createDefaultName() {
|
2012-05-09 23:28:52 +08:00
|
|
|
|
return createName(counter => string.Format("{0}{1}", DEFAULT_KEY_NAME, counter));
|
2012-05-03 22:47:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-05-09 23:28:52 +08:00
|
|
|
|
string createName(Func<int, string> create) {
|
2012-05-03 22:47:34 +08:00
|
|
|
|
for (int counter = 0; ; counter++) {
|
2012-05-09 23:28:52 +08:00
|
|
|
|
string newName = create(counter);
|
2012-05-03 22:47:34 +08:00
|
|
|
|
if (!newNames.ContainsKey(newName)) {
|
|
|
|
|
newNames[newName] = true;
|
|
|
|
|
return newName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|