Skip to content
This repository has been archived by the owner on Jan 27, 2019. It is now read-only.

Commit

Permalink
Supports pattern in ObfuscationAttributes
Browse files Browse the repository at this point in the history
Feature="@name('TypeName'):+rename"
  • Loading branch information
yck1509 committed Nov 21, 2015
1 parent d4b35d2 commit 9877d2c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
65 changes: 65 additions & 0 deletions Confuser.Core/ObfAttrMarker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,66 @@ protected internal override MarkerResult MarkProject(ConfuserProject proj, Confu
return new MarkerResult(modules.Select(module => module.Item2).ToList(), packer, extModules);
}

class RuleAdaptor : ProtectionSettings, IDictionary<ConfuserComponent, Dictionary<string, string>> {
Rule rule;
public RuleAdaptor(string pattern) {
this.rule = new Rule(pattern, ProtectionPreset.None, true);
}

public Rule Rule { get { return rule; } }

bool IDictionary<ConfuserComponent, Dictionary<string, string>>.ContainsKey(ConfuserComponent key) {
return true;
}

void IDictionary<ConfuserComponent, Dictionary<string, string>>.Add(ConfuserComponent key, Dictionary<string, string> value) {
var item = new SettingItem<Protection>(key.Id, SettingItemAction.Add);
foreach (var entry in value)
item.Add(entry.Key, entry.Value);
rule.Add(item);
}

bool IDictionary<ConfuserComponent, Dictionary<string, string>>.Remove(ConfuserComponent key) {
var item = new SettingItem<Protection>(key.Id, SettingItemAction.Remove);
rule.Add(item);
return true;
}

Dictionary<string, string> IDictionary<ConfuserComponent, Dictionary<string, string>>.this[ConfuserComponent key] {
get { return null; }
set {
rule.RemoveWhere(i => i.Id == key.Id);
var item = new SettingItem<Protection>(key.Id, SettingItemAction.Add);
foreach (var entry in value)
item.Add(entry.Key, entry.Value);
rule.Add(item);
}
}
}

void AddRule(ObfuscationAttributeInfo attr, Rules rules) {
Debug.Assert(attr.FeatureName != null && attr.FeatureName.StartsWith("@"));

var pattern = attr.FeatureName.Substring(1);
PatternExpression expr;
try {
expr = new PatternParser().Parse(pattern);
}
catch (Exception ex) {
throw new Exception("Error when parsing pattern " + pattern + " in ObfuscationAttribute", ex);
}

var ruleAdaptor = new RuleAdaptor(pattern);
try {
new ObfAttrParser(protections).ParseProtectionString(ruleAdaptor, attr.FeatureValue);
}
catch (Exception ex) {
throw new Exception("Error when parsing rule " + attr.FeatureValue + " in ObfuscationAttribute", ex);
}

rules.Add(ruleAdaptor.Rule, expr);
}

void MarkModule(ProjectModule projModule, ModuleDefMD module, Rules rules, bool isMain) {
var settingAttrs = new List<ObfuscationAttributeInfo>();
string snKeyPath = projModule.SNKeyPath, snKeyPass = projModule.SNKeyPassword;
Expand Down Expand Up @@ -283,6 +343,11 @@ void MarkModule(ProjectModule projModule, ModuleDefMD module, Rules rules, bool
extModules.Add(rawModule);
}
else {
if (attr.FeatureName.StartsWith("@")) {
AddRule(attr, rules);
continue;
}

var match = NSInModulePattern.Match(attr.FeatureName);
if (match.Success) {
if (!isMain)
Expand Down
2 changes: 1 addition & 1 deletion Confuser.Core/ObfAttrParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ bool IsEnd() {
return index == str.Length;
}

public void ParseProtectionString(ProtectionSettings settings, string str) {
public void ParseProtectionString(IDictionary<ConfuserComponent, Dictionary<string, string>> settings, string str) {
if (str == null)
return;

Expand Down

0 comments on commit 9877d2c

Please sign in to comment.