Example #1
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);
                        }
                    }
                }
            }
Example #2
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>());
         }
     }
 }
Example #3
0
        /// <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));
            }
        }
Example #4
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.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);
            }
Example #5
0
        /// <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(DotProtectContext 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 DotProtectception(null);
                }
                return parameters.ContainsKey(phase.Parent);
            }).ToList());
        }
Example #6
0
 /// <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(
     DotProtectContext context, IDnlibDef target, ProtectionSettings parameters)
 {
     context.Annotations.Set(target, ParametersKey, parameters);
 }
Example #7
0
 void Pop()
 {
     settings = stack.Pop().Item1;
 }
Example #8
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);

                    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;
                }
            }
        }
Example #9
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(DotProtectContext 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);
        }