From 9877d2c2ff8f4f41a85757c0dbe5b2e70743ddab Mon Sep 17 00:00:00 2001 From: yck1509 Date: Sun, 22 Nov 2015 00:29:47 +0800 Subject: [PATCH] Supports pattern in ObfuscationAttributes Feature="@name('TypeName'):+rename" --- Confuser.Core/ObfAttrMarker.cs | 65 ++++++++++++++++++++++++++++++++++ Confuser.Core/ObfAttrParser.cs | 2 +- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/Confuser.Core/ObfAttrMarker.cs b/Confuser.Core/ObfAttrMarker.cs index 35e8d22b7..ebb72ca2e 100644 --- a/Confuser.Core/ObfAttrMarker.cs +++ b/Confuser.Core/ObfAttrMarker.cs @@ -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> { + Rule rule; + public RuleAdaptor(string pattern) { + this.rule = new Rule(pattern, ProtectionPreset.None, true); + } + + public Rule Rule { get { return rule; } } + + bool IDictionary>.ContainsKey(ConfuserComponent key) { + return true; + } + + void IDictionary>.Add(ConfuserComponent key, Dictionary value) { + var item = new SettingItem(key.Id, SettingItemAction.Add); + foreach (var entry in value) + item.Add(entry.Key, entry.Value); + rule.Add(item); + } + + bool IDictionary>.Remove(ConfuserComponent key) { + var item = new SettingItem(key.Id, SettingItemAction.Remove); + rule.Add(item); + return true; + } + + Dictionary IDictionary>.this[ConfuserComponent key] { + get { return null; } + set { + rule.RemoveWhere(i => i.Id == key.Id); + var item = new SettingItem(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(); string snKeyPath = projModule.SNKeyPath, snKeyPass = projModule.SNKeyPassword; @@ -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) diff --git a/Confuser.Core/ObfAttrParser.cs b/Confuser.Core/ObfAttrParser.cs index 5d7dbe61c..20c4ed0b9 100644 --- a/Confuser.Core/ObfAttrParser.cs +++ b/Confuser.Core/ObfAttrParser.cs @@ -83,7 +83,7 @@ bool IsEnd() { return index == str.Length; } - public void ParseProtectionString(ProtectionSettings settings, string str) { + public void ParseProtectionString(IDictionary> settings, string str) { if (str == null) return;