/// <summary> /// Initializes a new instance of the <see cref="ProtectionSettings" /> class /// from an existing <see cref="ProtectionSettings" />. /// </summary> /// <param name="settings">The settings to copy from.</param> public ProtectionSettings(ProtectionSettings settings) { foreach (var i in settings) { Add(i.Key, new Dictionary <string, string>(i.Value)); } }
/// <summary> /// Applies the rules to the target definition. /// </summary> /// <param name="context">The working context.</param> /// <param name="target">The target definition.</param> /// <param name="rules">The rules.</param> /// <param name="baseSettings">The base settings.</param> // Token: 0x06000196 RID: 406 RVA: 0x0000D45C File Offset: 0x0000B65C protected void ApplyRules(ConfuserContext context, IDnlibDef target, Dictionary <Rule, PatternExpression> rules, ProtectionSettings baseSettings = null) { ProtectionSettings ret = (baseSettings == null) ? new ProtectionSettings() : new ProtectionSettings(baseSettings); foreach (KeyValuePair <Rule, PatternExpression> i in rules) { if ((bool)i.Value.Evaluate(target)) { if (!i.Key.Inherit) { ret.Clear(); } this.FillPreset(i.Key.Preset, ret); foreach (SettingItem <Protection> prot in i.Key) { if (prot.Action == SettingItemAction.Add) { ret[this.protections[prot.Id]] = new Dictionary <string, string>(prot, StringComparer.OrdinalIgnoreCase); } else { ret.Remove(this.protections[prot.Id]); } } } } ProtectionParameters.SetParameters(context, target, ret); }
/// <summary> /// Applies the rules to the target definition. /// </summary> /// <param name="context">The working context.</param> /// <param name="target">The target definition.</param> /// <param name="rules">The rules.</param> protected void ApplyRules(ConfuserContext context, IDnlibDef target, Rules rules) { var ret = new ProtectionSettings(); foreach (var i in rules) { if (!(bool)i.Value.Evaluate(target)) { continue; } if (!i.Key.Inherit) { ret.Clear(); } FillPreset(i.Key.Preset, ret); foreach (var prot in i.Key) { if (prot.Action == SettingItemAction.Add) { ret[protections[prot.Id]] = new Dictionary <string, string>(prot, StringComparer.OrdinalIgnoreCase); } else { ret.Remove(protections[prot.Id]); } } } ProtectionParameters.SetParameters(context, target, ret); }
public IDisposable Apply(IDnlibDef target, IEnumerable<ProtectionSettingsInfo> infos) { ProtectionSettings settings; if (this.settings == null) settings = new ProtectionSettings(); else settings = new ProtectionSettings(this.settings); var infoArray = infos.ToArray(); if (stack.Count > 0) { foreach (var i in stack.Reverse()) ApplyInfo(target, settings, i.Item2, ApplyInfoType.ParentInfo); } IDisposable result; if (infoArray.Length != 0) { var originalSettings = this.settings; // the settings that would apply to members ApplyInfo(target, settings, infoArray, ApplyInfoType.CurrentInfoInherits); this.settings = new ProtectionSettings(settings); // the settings that would apply to itself ApplyInfo(target, settings, infoArray, ApplyInfoType.CurrentInfoOnly); stack.Push(Tuple.Create(originalSettings, infoArray)); result = new PopHolder(this); } else result = null; ProtectionParameters.SetParameters(context, target, settings); return result; }
/// <summary> /// Initializes a new instance of the <see cref="T:Confuser.Core.ProtectionSettings" /> class /// from an existing <see cref="T:Confuser.Core.ProtectionSettings" />. /// </summary> /// <param name="settings">The settings to copy from.</param> // Token: 0x060002C5 RID: 709 RVA: 0x0001231C File Offset: 0x0001051C public ProtectionSettings(ProtectionSettings settings) { foreach (KeyValuePair <ConfuserComponent, Dictionary <string, string> > i in settings) { base.Add(i.Key, new Dictionary <string, string>(i.Value)); } }
void ApplyInfo(IDnlibDef context, ProtectionSettings settings, IEnumerable <ProtectionSettingsInfo> infos, ApplyInfoType type) { foreach (var info in infos) { if (info.Condition != null && !(bool)info.Condition.Evaluate(context)) { continue; } if (info.Condition == null && info.Exclude) { if (type == ApplyInfoType.CurrentInfoOnly || (type == ApplyInfoType.CurrentInfoInherits && info.ApplyToMember)) { settings.Clear(); } } if (!string.IsNullOrEmpty(info.Settings)) { if ((type == ApplyInfoType.ParentInfo && info.ApplyToMember) || type == ApplyInfoType.CurrentInfoOnly || (type == ApplyInfoType.CurrentInfoInherits && info.Condition == null && info.ApplyToMember)) { parser.ParseProtectionString(settings, info.Settings); } } } }
/// <summary> /// Returns only the targets with the specified type and used by specified component. /// </summary> /// <param name="context">The working context.</param> /// <param name="targets">List of targets.</param> /// <param name="phase">The component phase.</param> /// <returns>Filtered targets.</returns> static IList<IDnlibDef> Filter(ConfuserContext context, IList<IDnlibDef> targets, ProtectionPhase phase) { ProtectionTargets targetType = phase.Targets; IEnumerable<IDnlibDef> filter = targets; if ((targetType & ProtectionTargets.Modules) == 0) filter = filter.Where(def => !(def is ModuleDef)); if ((targetType & ProtectionTargets.Types) == 0) filter = filter.Where(def => !(def is TypeDef)); if ((targetType & ProtectionTargets.Methods) == 0) filter = filter.Where(def => !(def is MethodDef)); if ((targetType & ProtectionTargets.Fields) == 0) filter = filter.Where(def => !(def is FieldDef)); if ((targetType & ProtectionTargets.Properties) == 0) filter = filter.Where(def => !(def is PropertyDef)); if ((targetType & ProtectionTargets.Events) == 0) filter = filter.Where(def => !(def is EventDef)); if (phase.ProcessAll) return filter.ToList(); return filter.Where(def => { ProtectionSettings parameters = ProtectionParameters.GetParameters(context, def); Debug.Assert(parameters != null); if (parameters == null) { context.Logger.ErrorFormat("'{0}' not marked for obfuscation, possibly a bug.", def); throw new ConfuserException(null); } return parameters.ContainsKey(phase.Parent); }).ToList(); }
void ApplyInfo(IDnlibDef context, ProtectionSettings settings, IEnumerable <ProtectionSettingsInfo> infos, bool current) { foreach (var info in infos) { if (info.Condition != null && !(bool)info.Condition.Evaluate(context)) { continue; } if (info.Exclude) { if (current) { settings.Clear(); } else if (info.ApplyToMember) { settings.Clear(); } continue; } if ((info.ApplyToMember || current || info.Condition != null) && !string.IsNullOrEmpty(info.Settings)) { parser.ParseProtectionString(settings, info.Settings); } } }
/// <summary> /// Returns only the targets with the specified type and used by specified component. /// </summary> /// <param name="context">The working context.</param> /// <param name="targets">List of targets.</param> /// <param name="phase">The component phase.</param> /// <returns>Filtered targets.</returns> // Token: 0x060002FE RID: 766 RVA: 0x00012B84 File Offset: 0x00010D84 private static IList <IDnlibDef> Filter(ConfuserContext context, IList <IDnlibDef> targets, ProtectionPhase phase) { ProtectionTargets targetType = phase.Targets; IEnumerable <IDnlibDef> filter = targets; if ((targetType & ProtectionTargets.Modules) == (ProtectionTargets)0) { filter = from def in filter where !(def is ModuleDef) select def; } if ((targetType & ProtectionTargets.Types) == (ProtectionTargets)0) { filter = from def in filter where !(def is TypeDef) select def; } if ((targetType & ProtectionTargets.Methods) == (ProtectionTargets)0) { filter = from def in filter where !(def is MethodDef) select def; } if ((targetType & ProtectionTargets.Fields) == (ProtectionTargets)0) { filter = from def in filter where !(def is FieldDef) select def; } if ((targetType & ProtectionTargets.Properties) == (ProtectionTargets)0) { filter = from def in filter where !(def is PropertyDef) select def; } if ((targetType & ProtectionTargets.Events) == (ProtectionTargets)0) { filter = from def in filter where !(def is EventDef) select def; } if (phase.ProcessAll) { return(filter.ToList <IDnlibDef>()); } return(filter.Where(delegate(IDnlibDef def) { ProtectionSettings parameters = ProtectionParameters.GetParameters(context, def); if (parameters == null) { context.Logger.ErrorFormat("'{0}' not marked for obfuscation, possibly a bug.", new object[] { def }); throw new ConfuserException(null); } return parameters.ContainsKey(phase.Parent); }).ToList <IDnlibDef>()); }
/// <summary> /// Fills the protection settings with the specified preset. /// </summary> /// <param name="preset">The preset.</param> /// <param name="settings">The settings.</param> void FillPreset(ProtectionPreset preset, ProtectionSettings settings) { foreach (Protection prot in protections.Values) { if (prot.Preset <= preset && !settings.ContainsKey(prot)) { settings.Add(prot, new Dictionary <string, string>()); } } }
/// <summary> /// Initializes a new instance of the <see cref="ProtectionSettings" /> class /// from an existing <see cref="ProtectionSettings" />. /// </summary> /// <param name="settings">The settings to copy from.</param> public ProtectionSettings(ProtectionSettings settings) { if (settings == null) { return; } foreach (var i in settings) { Add(i.Key, new Dictionary <string, string>(i.Value)); } }
public IDisposable Apply(IDnlibDef target, IEnumerable <ProtectionSettingsInfo> infos) { ProtectionSettings settings; if (this.settings == null) { settings = new ProtectionSettings(); } else { settings = new ProtectionSettings(this.settings); } var infoArray = infos.ToArray(); if (stack.Count > 0) { foreach (var i in stack.Skip(1).Reverse()) { ApplyInfo(target, settings, i.Item2.Where(info => info.Condition != null), false); } ApplyInfo(target, settings, stack.Peek().Item2, false); } IDisposable result; if (infoArray.Length != 0) { var originalSettings = this.settings; // the settings that would apply to members ApplyInfo(target, settings, infoArray, false); this.settings = new ProtectionSettings(settings); // the settings that would apply to itself ApplyInfo(target, settings, infoArray, true); stack.Push(Tuple.Create(originalSettings, infoArray)); result = new PopHolder(this); } else { result = null; } ProtectionParameters.SetParameters(context, target, settings); return(result); }
/// <summary> /// Obtains the value of a parameter of the specified target. /// </summary> /// <typeparam name="T">The type of the parameter value.</typeparam> /// <param name="context">The working context.</param> /// <param name="target">The protection target.</param> /// <param name="name">The name of the parameter.</param> /// <param name="defValue">Default value if the parameter does not exist.</param> /// <returns>The value of the parameter.</returns> // Token: 0x060002F5 RID: 757 RVA: 0x0001266C File Offset: 0x0001086C public T GetParameter <T>(ConfuserContext context, IDnlibDef target, string name, T defValue = default(T)) { if (this.comp == null) { return(defValue); } if (this.comp is Packer && target == null) { target = context.Modules[0]; } ProtectionSettings objParams = context.Annotations.Get <ProtectionSettings>(target, ProtectionParameters.ParametersKey, null); if (objParams == null) { return(defValue); } Dictionary <string, string> parameters; if (!objParams.TryGetValue(this.comp, out parameters)) { return(defValue); } string ret; if (!parameters.TryGetValue(name, out ret)) { return(defValue); } Type paramType = typeof(T); Type nullable = Nullable.GetUnderlyingType(paramType); if (nullable != null) { paramType = nullable; } if (paramType.IsEnum) { return((T)((object)Enum.Parse(paramType, ret, true))); } return((T)((object)Convert.ChangeType(ret, paramType))); }
void ApplySettings(IDnlibDef def, Rules rules, IEnumerable <ProtectionSettingsInfo> infos, ProtectionSettings settings = null) { if (settings == null) { settings = new ProtectionSettings(); } else { settings = new ProtectionSettings(settings); } ApplyRules(context, def, rules, settings); settings = ProtectionParameters.GetParameters(context, def); ProtectionSettingsInfo?last = null; var parser = new ObfAttrParser(protections); foreach (var info in infos) { if (info.Exclude) { if (info.ApplyToMember) { settings.Clear(); } continue; } last = info; if (info.ApplyToMember && !string.IsNullOrEmpty(info.Settings)) { parser.ParseProtectionString(settings, info.Settings); } } if (last != null && !last.Value.ApplyToMember && !string.IsNullOrEmpty(last.Value.Settings)) { parser.ParseProtectionString(settings, last.Value.Settings); } }
// Token: 0x060001B4 RID: 436 RVA: 0x0000E008 File Offset: 0x0000C208 private void ApplySettings(IDnlibDef def, Dictionary <Rule, PatternExpression> rules, IEnumerable <ObfAttrMarker.ProtectionSettingsInfo> infos, ProtectionSettings settings = null) { if (settings == null) { settings = new ProtectionSettings(); } else { settings = new ProtectionSettings(settings); } base.ApplyRules(this.context, def, rules, settings); settings = ProtectionParameters.GetParameters(this.context, def); ObfAttrMarker.ProtectionSettingsInfo?last = null; ObfAttrParser parser = new ObfAttrParser(this.protections); foreach (ObfAttrMarker.ProtectionSettingsInfo info in infos) { if (info.Exclude) { if (info.ApplyToMember) { settings.Clear(); } } else { last = new ObfAttrMarker.ProtectionSettingsInfo?(info); if (info.ApplyToMember && !string.IsNullOrEmpty(info.Settings)) { parser.ParseProtectionString(settings, info.Settings); } } } if (last.HasValue && !last.Value.ApplyToMember && !string.IsNullOrEmpty(last.Value.Settings)) { parser.ParseProtectionString(settings, last.Value.Settings); } }
void Pop() { settings = stack.Pop().Item1; }
public void ParsePackerString(string str, out Packer packer, out Dictionary <string, string> packerParams) { packer = null; packerParams = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); if (str == null) { return; } this.str = str; index = 0; var state = ParseState.ReadItemName; var buffer = new StringBuilder(); var ret = new ProtectionSettings(); while (state != ParseState.End) { switch (state) { case ParseState.ReadItemName: ReadId(buffer); var packerId = buffer.ToString(); if (!items.Contains(packerId)) { throw new KeyNotFoundException("Cannot find packer with id '" + packerId + "'."); } packer = (Packer)items[packerId]; buffer.Length = 0; if (IsEnd() || Peek() == ';') { state = ParseState.EndItem; } else if (Peek() == '(') { Next(); state = ParseState.ReadParam; } else { throw new ArgumentException("Unexpected character in ReadItemName state at " + index + "."); } break; case ParseState.ReadParam: string paramName, paramValue; if (!ReadId(buffer)) { throw new ArgumentException("Unexpected end of string in ReadParam state."); } paramName = buffer.ToString(); buffer.Length = 0; Expect('='); if (!ReadId(buffer)) { throw new ArgumentException("Unexpected end of string in ReadParam state."); } paramValue = buffer.ToString(); buffer.Length = 0; packerParams.Add(paramName, paramValue); if (Peek() == ',') { Next(); state = ParseState.ReadParam; } else if (Peek() == ')') { Next(); state = ParseState.EndItem; } else { throw new ArgumentException("Unexpected character in ReadParam state at " + index + "."); } break; case ParseState.EndItem: if (IsEnd()) { state = ParseState.End; } else { Expect(';'); if (!IsEnd()) { throw new ArgumentException("Unexpected character in EndItem state at " + index + "."); } state = ParseState.End; } break; } } }
// Token: 0x060001CE RID: 462 RVA: 0x0000EFA0 File Offset: 0x0000D1A0 public void ParseProtectionString(ProtectionSettings settings, string str) { if (str == null) { return; } this.str = str; this.index = 0; ObfAttrParser.ParseState state = ObfAttrParser.ParseState.Init; StringBuilder buffer = new StringBuilder(); bool protAct = true; string protId = null; Dictionary <string, string> protParams = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); while (state != ObfAttrParser.ParseState.End) { switch (state) { case ObfAttrParser.ParseState.Init: this.ReadId(buffer); if (buffer.ToString().Equals("preset", StringComparison.OrdinalIgnoreCase)) { if (this.IsEnd()) { throw new ArgumentException("Unexpected end of string in Init state."); } this.Expect('('); buffer.Length = 0; state = ObfAttrParser.ParseState.ReadPreset; } else if (buffer.Length == 0) { if (this.IsEnd()) { throw new ArgumentException("Unexpected end of string in Init state."); } state = ObfAttrParser.ParseState.ReadItemName; } else { protAct = true; state = ObfAttrParser.ParseState.ProcessItemName; } break; case ObfAttrParser.ParseState.ReadPreset: { if (!this.ReadId(buffer)) { throw new ArgumentException("Unexpected end of string in ReadPreset state."); } this.Expect(')'); ProtectionPreset preset = (ProtectionPreset)Enum.Parse(typeof(ProtectionPreset), buffer.ToString(), true); foreach (Protection item in from prot in this.items.Values.OfType <Protection>() where prot.Preset <= preset select prot) { if (settings != null && !settings.ContainsKey(item)) { settings.Add(item, new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)); } } buffer.Length = 0; if (this.IsEnd()) { state = ObfAttrParser.ParseState.End; } else { this.Expect(';'); if (this.IsEnd()) { state = ObfAttrParser.ParseState.End; } else { state = ObfAttrParser.ParseState.ReadItemName; } } break; } case ObfAttrParser.ParseState.ReadItemName: protAct = true; if (this.Peek() == '+') { protAct = true; this.Next(); } else if (this.Peek() == '-') { protAct = false; this.Next(); } this.ReadId(buffer); state = ObfAttrParser.ParseState.ProcessItemName; break; case ObfAttrParser.ParseState.ProcessItemName: protId = buffer.ToString(); buffer.Length = 0; if (this.IsEnd() || this.Peek() == ';') { state = ObfAttrParser.ParseState.EndItem; } else { if (this.Peek() != '(') { throw new ArgumentException("Unexpected character in ProcessItemName state at " + this.index + "."); } if (!protAct) { throw new ArgumentException("No parameters is allowed when removing protection."); } this.Next(); state = ObfAttrParser.ParseState.ReadParam; } break; case ObfAttrParser.ParseState.ReadParam: { if (!this.ReadId(buffer)) { throw new ArgumentException("Unexpected end of string in ReadParam state."); } string paramName = buffer.ToString(); buffer.Length = 0; this.Expect('='); if (!((this.Peek() == '\'') ? this.ReadString(buffer) : this.ReadId(buffer))) { throw new ArgumentException("Unexpected end of string in ReadParam state."); } string paramValue = buffer.ToString(); buffer.Length = 0; protParams.Add(paramName, paramValue); if (this.Peek() == ',') { this.Next(); state = ObfAttrParser.ParseState.ReadParam; } else { if (this.Peek() != ')') { throw new ArgumentException("Unexpected character in ReadParam state at " + this.index + "."); } this.Next(); state = ObfAttrParser.ParseState.EndItem; } break; } case ObfAttrParser.ParseState.EndItem: if (settings != null) { if (!this.items.Contains(protId)) { throw new KeyNotFoundException("Cannot find protection with id '" + protId + "'."); } if (protAct) { settings[(Protection)this.items[protId]] = protParams; } else { settings.Remove((Protection)this.items[protId]); } } protParams = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); if (this.IsEnd()) { state = ObfAttrParser.ParseState.End; } else { this.Expect(';'); if (this.IsEnd()) { state = ObfAttrParser.ParseState.End; } else { state = ObfAttrParser.ParseState.ReadItemName; } } break; } } }
public void ParseProtectionString(ProtectionSettings settings, string str) { if (str == null) { return; } this.str = str; index = 0; var state = ParseState.Init; var buffer = new StringBuilder(); bool protAct = true; string protId = null; var protParams = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); while (state != ParseState.End) { switch (state) { case ParseState.Init: ReadId(buffer); if (buffer.ToString().Equals("preset", StringComparison.OrdinalIgnoreCase)) { if (IsEnd()) { throw new ArgumentException("Unexpected end of string in Init state."); } Expect('('); buffer.Length = 0; state = ParseState.ReadPreset; } else if (buffer.Length == 0) { if (IsEnd()) { throw new ArgumentException("Unexpected end of string in Init state."); } state = ParseState.ReadItemName; } else { protAct = true; state = ParseState.ProcessItemName; } break; case ParseState.ReadPreset: if (!ReadId(buffer)) { throw new ArgumentException("Unexpected end of string in ReadPreset state."); } Expect(')'); var preset = (ProtectionPreset)Enum.Parse(typeof(ProtectionPreset), buffer.ToString(), true); foreach (var item in items.Values.OfType <Protection>().Where(prot => prot.Preset <= preset)) { if (!settings.ContainsKey(item)) { settings.Add(item, new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)); } } buffer.Length = 0; if (IsEnd()) { state = ParseState.End; } else { Expect(';'); if (IsEnd()) { state = ParseState.End; } else { state = ParseState.ReadItemName; } } break; case ParseState.ReadItemName: protAct = true; if (Peek() == '+') { protAct = true; Next(); } else if (Peek() == '-') { protAct = false; Next(); } ReadId(buffer); state = ParseState.ProcessItemName; break; case ParseState.ProcessItemName: protId = buffer.ToString(); buffer.Length = 0; if (IsEnd() || Peek() == ';') { state = ParseState.EndItem; } else if (Peek() == '(') { if (!protAct) { throw new ArgumentException("No parameters is allowed when removing protection."); } Next(); state = ParseState.ReadParam; } else { throw new ArgumentException("Unexpected character in ProcessItemName state at " + index + "."); } break; case ParseState.ReadParam: string paramName, paramValue; if (!ReadId(buffer)) { throw new ArgumentException("Unexpected end of string in ReadParam state."); } paramName = buffer.ToString(); buffer.Length = 0; Expect('='); if (!ReadId(buffer)) { throw new ArgumentException("Unexpected end of string in ReadParam state."); } paramValue = buffer.ToString(); buffer.Length = 0; protParams.Add(paramName, paramValue); if (Peek() == ',') { Next(); state = ParseState.ReadParam; } else if (Peek() == ')') { Next(); state = ParseState.EndItem; } else { throw new ArgumentException("Unexpected character in ReadParam state at " + index + "."); } break; case ParseState.EndItem: if (protAct) { settings[(Protection)items[protId]] = protParams; protParams = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); } else { settings.Remove((Protection)items[protId]); } if (IsEnd()) { state = ParseState.End; } else { Expect(';'); if (IsEnd()) { state = ParseState.End; } else { state = ParseState.ReadItemName; } } break; } } }
/// <summary> /// Sets the protection parameters of the specified target. /// </summary> /// <param name="context">The context.</param> /// <param name="target">The protection target.</param> /// <param name="parameters">The parameters.</param> public static void SetParameters( ConfuserContext context, IDnlibDef target, ProtectionSettings parameters) { context.Annotations.Set(target, ParametersKey, parameters); }