Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <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);
        }
Пример #3
0
        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;
                }
            }
        }
Пример #4
0
        // 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;
                }
            }
        }