Ejemplo n.º 1
0
			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;
			}
Ejemplo n.º 2
0
        void ApplySettings(IDnlibDef def, IEnumerable <ProtectionSettingsInfo> infos)
        {
            var settings = new ProtectionSettings();

            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)
                {
                    parser.ParseProtectionString(settings, info.Settings);
                }
            }
            if (last != null && !last.Value.ApplyToMember)
            {
                parser.ParseProtectionString(settings, last.Value.Settings);
            }

            ProtectionParameters.SetParameters(context, def, settings);
        }
Ejemplo n.º 3
0
        public string GetParam(IDnlibDef def, string name)
        {
            ProtectionSettings param = ProtectionParameters.GetParameters(this.context, def);

            if (param == null)
            {
                return(null);
            }
            return(!param.TryGetValue(this.analyze.Parent, out Dictionary <string, string> nameParam) ? null : nameParam.GetValueOrDefault(name));
        }
Ejemplo n.º 4
0
		public bool CanRename(object obj) {
			if (obj is IDnlibDef) {
				if (analyze == null)
					analyze = context.Pipeline.FindPhase<AnalyzePhase>();

				var prot = (NameProtection)analyze.Parent;
				ProtectionSettings parameters = ProtectionParameters.GetParameters(context, (IDnlibDef)obj);
				if (parameters == null || !parameters.ContainsKey(prot))
					return false;
				return context.Annotations.Get(obj, CanRenameKey, true);
			}
			return false;
		}
Ejemplo n.º 5
0
 // Token: 0x0600025A RID: 602 RVA: 0x0001F978 File Offset: 0x0001DB78
 public bool CanRename(object obj)
 {
     if (obj is IDnlibDef)
     {
         if (this.analyze == null)
         {
             this.analyze = this.context.Pipeline.FindPhase <AnalyzePhase>();
         }
         NameProtection     prot       = (NameProtection)this.analyze.Parent;
         ProtectionSettings parameters = ProtectionParameters.GetParameters(this.context, (IDnlibDef)obj);
         return(parameters != null && parameters.ContainsKey(prot) && this.context.Annotations.Get <bool>(obj, NameService.CanRenameKey, true));
     }
     return(false);
 }
Ejemplo n.º 6
0
        public void SetParam(IDnlibDef def, string name, string value)
        {
            ProtectionSettings param = ProtectionParameters.GetParameters(this.context, def);

            if (param == null)
            {
                ProtectionParameters.SetParameters(this.context, def, param = new ProtectionSettings());
            }
            if (!param.TryGetValue(this.analyze.Parent, out Dictionary <string, string> nameParam))
            {
                param[this.analyze.Parent] = nameParam = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            }
            nameParam[name] = value;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ProtectionParameters" /> class.
        /// </summary>
        /// <param name="targets">The protection targets.</param>
        /// <param name="defaultSettings">The protection default settings</param>
        internal ProtectionParameters(IList <IDnlibDef> targets, ProtectionSettings defaultSettings)
        {
            if (targets is null)
            {
                throw new ArgumentNullException(nameof(targets));
            }
            if (defaultSettings is null)
            {
                throw new ArgumentNullException(nameof(defaultSettings));
            }

            Targets          = targets;
            DefaultSettings  = defaultSettings;
            OverrideSettings = new Dictionary <IDnlibDef, ProtectionSettings>();
        }
Ejemplo n.º 8
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 != null && !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 (!(Peek() == '\'' ? ReadString(buffer) : 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 (settings != null) {
                            if (protAct) {
                                settings[(Protection)items[protId]] = protParams;
                            }
                            else
                                settings.Remove((Protection)items[protId]);
                        }
                        protParams = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

                        if (IsEnd())
                            state = ParseState.End;
                        else {
                            Expect(';');
                            if (IsEnd())
                                state = ParseState.End;
                            else
                                state = ParseState.ReadItemName;
                        }
                        break;
                }
            }
        }
Ejemplo n.º 9
0
		/// <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 != ProtectionPreset.None && prot.Preset <= preset && !settings.ContainsKey(prot))
					settings.Add(prot, new Dictionary<string, string>());
		}
Ejemplo n.º 10
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>
		protected void ApplyRules(ConfuserContext context, IDnlibDef target, Rules rules, ProtectionSettings baseSettings = null) {
			var ret = baseSettings == null ? new ProtectionSettings() : new ProtectionSettings(baseSettings);
			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);
		}
Ejemplo n.º 11
0
        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:
                    if (!ReadId(buffer))
                    {
                        throw new ArgumentException("Unexpected end of string in ReadItemName state.");
                    }

                    packer        = (Packer)items[buffer.ToString()];
                    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;
                }
            }
        }
Ejemplo n.º 12
0
            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.Condition != null && info.ApplyToMember) ||
                            type == ApplyInfoType.CurrentInfoOnly ||
                            (type == ApplyInfoType.CurrentInfoInherits && info.Condition == null && info.ApplyToMember)) {
                            parser.ParseProtectionString(settings, info.Settings);
                        }
                    }
                }
            }
Ejemplo n.º 13
0
            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);
                    }
                }
            }
Ejemplo n.º 14
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;
                }
            }
        }
Ejemplo n.º 15
0
        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);

                        packer = (Packer)items[buffer.ToString()];
                        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;
                }
            }
        }
Ejemplo n.º 16
0
 void Pop()
 {
     settings = stack.Pop().Item1;
 }
Ejemplo n.º 17
0
        void ApplySettings(IDnlibDef def, IEnumerable<ProtectionSettingsInfo> infos, ProtectionSettings settings = null)
        {
            if (settings == null)
                settings = new ProtectionSettings();
            else
                settings = new ProtectionSettings(settings);

            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);
            }

            ProtectionParameters.SetParameters(context, def, settings);
        }
Ejemplo n.º 18
0
 public FileProtector(ProtectionSettings Settings)
 {
     this.SettingsClass = Settings;
 }