Esempio n. 1
0
        bool ToInfo(ObfuscationAttributeInfo attr, out ProtectionSettingsInfo info)
        {
            info = new ProtectionSettingsInfo();

            info.Condition = null;

            info.Exclude       = (attr.Exclude ?? true);
            info.ApplyToMember = (attr.ApplyToMembers ?? true);
            info.Settings      = attr.FeatureValue;

            bool ok = true;

            try {
                new ObfAttrParser(protections).ParseProtectionString(null, info.Settings);
            }
            catch {
                ok = false;
            }

            if (!ok)
            {
                context.Logger.WarnFormat("Ignoring rule '{0}' in {1}.", info.Settings, attr.Owner);
                return(false);
            }

            if (!string.IsNullOrEmpty(attr.FeatureName))
            {
                throw new ArgumentException("Feature name must not be set. Owner=" + attr.Owner);
            }
            if (info.Exclude && (!string.IsNullOrEmpty(attr.FeatureName) || !string.IsNullOrEmpty(attr.FeatureValue)))
            {
                throw new ArgumentException("Feature property cannot be set when Exclude is true. Owner=" + attr.Owner);
            }
            return(true);
        }
Esempio n. 2
0
		ProtectionSettingsInfo AddRule(ObfuscationAttributeInfo attr, List<ProtectionSettingsInfo> infos) {
			Debug.Assert(attr.FeatureName != null);

			var pattern = attr.FeatureName;
			PatternExpression expr;
			try {
				expr = new PatternParser().Parse(pattern);
			}
			catch (Exception ex) {
				throw new Exception("Error when parsing pattern " + pattern + " in ObfuscationAttribute. Owner=" + attr.Owner, ex);
			}

			var info = new ProtectionSettingsInfo();
			info.Condition = expr;

			info.Exclude = (attr.Exclude ?? true);
			info.ApplyToMember = (attr.ApplyToMembers ?? true);
			info.Settings = attr.FeatureValue;

			bool ok = true;
			try {
				new ObfAttrParser(protections).ParseProtectionString(null, info.Settings);
			}
			catch {
				ok = false;
			}

			if (!ok)
				context.Logger.ErrorFormat("Ignoring rule '{0}' in {1}.", info.Settings, attr.Owner);
			else if (infos != null)
				infos.Add(info);
			return info;
		}
Esempio n. 3
0
        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);
        }
        bool ToInfo(ObfuscationAttributeInfo attr, out ProtectionSettingsInfo info)
        {
            info = new ProtectionSettingsInfo();

            info.Condition = null;

            info.Exclude       = (attr.Exclude ?? true);
            info.ApplyToMember = (attr.ApplyToMembers ?? true);
            info.Settings      = attr.FeatureValue;

            bool ok = true;

            try {
                new ObfAttrParser(protections).ParseProtectionString(null, info.Settings);
            }
            catch {
                ok = false;
            }

            if (!ok)
            {
                context.Logger.WarnFormat("忽略 {1} 中的规则“{0}”。", info.Settings, attr.Owner);
                return(false);
            }

            if (!string.IsNullOrEmpty(attr.FeatureName))
            {
                throw new ArgumentException("不得设置功能名称。 所有者=" + attr.Owner);
            }
            if (info.Exclude && (!string.IsNullOrEmpty(attr.FeatureName) || !string.IsNullOrEmpty(attr.FeatureValue)))
            {
                throw new ArgumentException("当Exclude为true时,无法设置要素属性。 所有者=" + attr.Owner);
            }
            return(true);
        }
        ProtectionSettingsInfo AddRule(ObfuscationAttributeInfo attr, List <ProtectionSettingsInfo> infos)
        {
            Debug.Assert(attr.FeatureName != null);

            var pattern = attr.FeatureName;
            PatternExpression expr;

            try {
                expr = new PatternParser().Parse(pattern);
            }
            catch (Exception ex) {
                throw new Exception("解析模式时出错 " + pattern + " 在ObfuscationAttribute中。 所有者=" + attr.Owner, ex);
            }

            var info = new ProtectionSettingsInfo();

            info.Condition = expr;

            info.Exclude       = (attr.Exclude ?? true);
            info.ApplyToMember = (attr.ApplyToMembers ?? true);
            info.Settings      = attr.FeatureValue;

            bool ok = true;

            try {
                new ObfAttrParser(protections).ParseProtectionString(null, info.Settings);
            }
            catch {
                ok = false;
            }

            if (!ok)
            {
                context.Logger.WarnFormat("忽略规则 '{0}' in {1}.", info.Settings, attr.Owner);
            }
            else if (infos != null)
            {
                infos.Add(info);
            }
            return(info);
        }
Esempio n. 6
0
        static IEnumerable<ObfuscationAttributeInfo> ReadObfuscationAttributes(IHasCustomAttribute item)
        {
            var ret = new List<ObfuscationAttributeInfo>();
            for (int i = item.CustomAttributes.Count - 1; i >= 0; i--) {
                var ca = item.CustomAttributes[i];
                if (ca.TypeFullName != "System.Reflection.ObfuscationAttribute")
                    continue;

                var info = new ObfuscationAttributeInfo();
                bool strip = true;
                foreach (var prop in ca.Properties) {
                    switch (prop.Name) {
                        case "ApplyToMembers":
                            Debug.Assert(prop.Type.ElementType == ElementType.Boolean);
                            info.ApplyToMembers = (bool)prop.Value;
                            break;

                        case "Exclude":
                            Debug.Assert(prop.Type.ElementType == ElementType.Boolean);
                            info.Exclude = (bool)prop.Value;
                            break;

                        case "StripAfterObfuscation":
                            Debug.Assert(prop.Type.ElementType == ElementType.Boolean);
                            strip = (bool)prop.Value;
                            break;

                        case "Feature":
                            Debug.Assert(prop.Type.ElementType == ElementType.String);
                            string feature = (UTF8String)prop.Value;
                            int sepIndex = feature.IndexOf(':');
                            if (sepIndex == -1) {
                                info.FeatureName = "";
                                info.FeatureValue = feature;
                            }
                            else {
                                info.FeatureName = feature.Substring(0, sepIndex);
                                info.FeatureValue = feature.Substring(sepIndex + 1);
                            }
                            break;

                        default:
                            throw new NotSupportedException("Unsupported property: " + prop.Name);
                    }
                }
                if (strip)
                    item.CustomAttributes.RemoveAt(i);

                ret.Add(info);
            }
            ret.Reverse();
            return ret;
        }
Esempio n. 7
0
        static IEnumerable <ObfuscationAttributeInfo> ReadObfuscationAttributes(IHasCustomAttribute item)
        {
            var ret = new List <Tuple <int?, ObfuscationAttributeInfo> >();

            for (int i = item.CustomAttributes.Count - 1; i >= 0; i--)
            {
                var ca = item.CustomAttributes[i];
                if (ca.TypeFullName != "System.Reflection.ObfuscationAttribute")
                {
                    continue;
                }

                var info  = new ObfuscationAttributeInfo();
                int?order = null;

                info.Owner = item;
                bool strip = true;
                foreach (var prop in ca.Properties)
                {
                    switch (prop.Name)
                    {
                    case "ApplyToMembers":
                        Debug.Assert(prop.Type.ElementType == ElementType.Boolean);
                        info.ApplyToMembers = (bool)prop.Value;
                        break;

                    case "Exclude":
                        Debug.Assert(prop.Type.ElementType == ElementType.Boolean);
                        info.Exclude = (bool)prop.Value;
                        break;

                    case "StripAfterObfuscation":
                        Debug.Assert(prop.Type.ElementType == ElementType.Boolean);
                        strip = (bool)prop.Value;
                        break;

                    case "Feature":
                        Debug.Assert(prop.Type.ElementType == ElementType.String);
                        string feature = (UTF8String)prop.Value;

                        var match = OrderPattern.Match(feature);
                        if (match.Success)
                        {
                            var orderStr = match.Groups[1].Value;
                            var f        = match.Groups[2].Value;
                            int o;
                            if (!int.TryParse(orderStr, out o))
                            {
                                throw new NotSupportedException(string.Format("Failed to parse feature '{0}' in {1} ", feature, item));
                            }
                            order   = o;
                            feature = f;
                        }

                        int sepIndex = feature.IndexOf(':');
                        if (sepIndex == -1)
                        {
                            info.FeatureName  = "";
                            info.FeatureValue = feature;
                        }
                        else
                        {
                            info.FeatureName  = feature.Substring(0, sepIndex);
                            info.FeatureValue = feature.Substring(sepIndex + 1);
                        }
                        break;

                    default:
                        throw new NotSupportedException("Unsupported property: " + prop.Name);
                    }
                }
                if (strip)
                {
                    item.CustomAttributes.RemoveAt(i);
                }

                if (item is IMemberRef && !(item is ITypeDefOrRef))
                {
                    info.ApplyToMembers = false;
                }

                ret.Add(Tuple.Create(order, info));
            }
            ret.Reverse();
            return(ret.OrderBy(pair => pair.Item1).Select(pair => pair.Item2));
        }
Esempio n. 8
0
        static IEnumerable <ObfuscationAttributeInfo> ReadObfuscationAttributes(IHasCustomAttribute item)
        {
            var ret = new List <ObfuscationAttributeInfo>();

            for (int i = item.CustomAttributes.Count - 1; i >= 0; i--)
            {
                var ca = item.CustomAttributes[i];
                if (ca.TypeFullName != "System.Reflection.ObfuscationAttribute")
                {
                    continue;
                }

                var  info  = new ObfuscationAttributeInfo();
                bool strip = true;
                foreach (var prop in ca.Properties)
                {
                    switch (prop.Name)
                    {
                    case "ApplyToMembers":
                        Debug.Assert(prop.Type.ElementType == ElementType.Boolean);
                        info.ApplyToMembers = (bool)prop.Value;
                        break;

                    case "Exclude":
                        Debug.Assert(prop.Type.ElementType == ElementType.Boolean);
                        info.Exclude = (bool)prop.Value;
                        break;

                    case "StripAfterObfuscation":
                        Debug.Assert(prop.Type.ElementType == ElementType.Boolean);
                        strip = (bool)prop.Value;
                        break;

                    case "Feature":
                        Debug.Assert(prop.Type.ElementType == ElementType.String);
                        string feature  = (UTF8String)prop.Value;
                        int    sepIndex = feature.IndexOf(':');
                        if (sepIndex == -1)
                        {
                            info.FeatureName  = "";
                            info.FeatureValue = feature;
                        }
                        else
                        {
                            info.FeatureName  = feature.Substring(0, sepIndex);
                            info.FeatureValue = feature.Substring(sepIndex + 1);
                        }
                        break;

                    default:
                        throw new NotSupportedException("Unsupported property: " + prop.Name);
                    }
                }
                if (strip)
                {
                    item.CustomAttributes.RemoveAt(i);
                }

                ret.Add(info);
            }
            ret.Reverse();
            return(ret);
        }
Esempio n. 9
0
        static IEnumerable<ObfuscationAttributeInfo> ReadObfuscationAttributes(IHasCustomAttribute item)
        {
            var ret = new List<Tuple<int?, ObfuscationAttributeInfo>>();
            for (int i = item.CustomAttributes.Count - 1; i >= 0; i--) {
                var ca = item.CustomAttributes[i];
                if (ca.TypeFullName != "System.Reflection.ObfuscationAttribute")
                    continue;

                var info = new ObfuscationAttributeInfo();
                int? order = null;

                info.Owner = item;
                bool strip = true;
                foreach (var prop in ca.Properties) {
                    switch (prop.Name) {
                        case "ApplyToMembers":
                            Debug.Assert(prop.Type.ElementType == ElementType.Boolean);
                            info.ApplyToMembers = (bool)prop.Value;
                            break;

                        case "Exclude":
                            Debug.Assert(prop.Type.ElementType == ElementType.Boolean);
                            info.Exclude = (bool)prop.Value;
                            break;

                        case "StripAfterObfuscation":
                            Debug.Assert(prop.Type.ElementType == ElementType.Boolean);
                            strip = (bool)prop.Value;
                            break;

                        case "Feature":
                            Debug.Assert(prop.Type.ElementType == ElementType.String);
                            string feature = (UTF8String)prop.Value;

                            var match = OrderPattern.Match(feature);
                            if (match.Success) {
                                var orderStr = match.Groups[1].Value;
                                var f = match.Groups[2].Value;
                                int o;
                                if (!int.TryParse(orderStr, out o))
                                    throw new NotSupportedException(string.Format("Failed to parse feature '{0}' in {1} ", feature, item));
                                order = o;
                                feature = f;
                            }

                            int sepIndex = feature.IndexOf(':');
                            if (sepIndex == -1) {
                                info.FeatureName = "";
                                info.FeatureValue = feature;
                            }
                            else {
                                info.FeatureName = feature.Substring(0, sepIndex);
                                info.FeatureValue = feature.Substring(sepIndex + 1);
                            }
                            break;

                        default:
                            throw new NotSupportedException("Unsupported property: " + prop.Name);
                    }
                }
                if (strip)
                    item.CustomAttributes.RemoveAt(i);

                ret.Add(Tuple.Create(order, info));
            }
            ret.Reverse();
            return ret.OrderBy(pair => pair.Item1).Select(pair => pair.Item2);
        }
Esempio n. 10
0
        bool ToInfo(ObfuscationAttributeInfo attr, out ProtectionSettingsInfo info)
        {
            info = new ProtectionSettingsInfo();

            info.Condition = null;

            info.Exclude = (attr.Exclude ?? true);
            info.ApplyToMember = (attr.ApplyToMembers ?? true);
            info.Settings = attr.FeatureValue;

            bool ok = true;
            try {
                new ObfAttrParser(protections).ParseProtectionString(null, info.Settings);
            }
            catch {
                ok = false;
            }

            if (!ok) {
                context.Logger.WarnFormat("Ignoring rule '{0}' in {1}.", info.Settings, attr.Owner);
                return false;
            }

            if (!string.IsNullOrEmpty(attr.FeatureName))
                throw new ArgumentException("Feature name must not be set. Owner=" + attr.Owner);
            if (info.Exclude && (!string.IsNullOrEmpty(attr.FeatureName) || !string.IsNullOrEmpty(attr.FeatureValue))) {
                throw new ArgumentException("Feature property cannot be set when Exclude is true. Owner=" + attr.Owner);
            }
            return true;
        }
Esempio n. 11
0
        ProtectionSettingsInfo AddRule(ObfuscationAttributeInfo attr, List<ProtectionSettingsInfo> infos)
        {
            Debug.Assert(attr.FeatureName != null);

            var pattern = attr.FeatureName;
            PatternExpression expr;
            try {
                expr = new PatternParser().Parse(pattern);
            }
            catch (Exception ex) {
                throw new Exception("Error when parsing pattern " + pattern + " in ObfuscationAttribute. Owner=" + attr.Owner, ex);
            }

            var info = new ProtectionSettingsInfo();
            info.Condition = expr;

            info.Exclude = (attr.Exclude ?? true);
            info.ApplyToMember = (attr.ApplyToMembers ?? true);
            info.Settings = attr.FeatureValue;

            bool ok = true;
            try {
                new ObfAttrParser(protections).ParseProtectionString(null, info.Settings);
            }
            catch {
                ok = false;
            }

            if (!ok)
                context.Logger.WarnFormat("Ignoring rule '{0}' in {1}.", info.Settings, attr.Owner);
            else if (infos != null)
                infos.Add(info);
            return info;
        }