~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/Mono.Cecil/Mono.Cecil/Mono.Cecil/SecurityDeclaration.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
// Author:
5
5
//   Jb Evain (jbevain@gmail.com)
6
6
//
7
 
// (C) 2005 Jb Evain
 
7
// Copyright (c) 2008 - 2010 Jb Evain
8
8
//
9
9
// Permission is hereby granted, free of charge, to any person obtaining
10
10
// a copy of this software and associated documentation files (the
26
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
27
//
28
28
 
 
29
using System;
 
30
 
 
31
using Mono.Collections.Generic;
 
32
 
29
33
namespace Mono.Cecil {
30
34
 
31
 
        using System;
32
 
        using System.Collections;
33
 
        using System.Security;
34
 
 
35
 
        public sealed class SecurityDeclaration : IRequireResolving, IAnnotationProvider, IReflectionVisitable {
36
 
 
37
 
                SecurityAction m_action;
38
 
                SecurityDeclarationReader m_reader;
39
 
                IDictionary m_annotations;
40
 
 
41
 
#if !CF_1_0 && !CF_2_0
42
 
                PermissionSet m_permSet;
43
 
#endif
44
 
 
45
 
                bool m_resolved;
46
 
                byte [] m_blob;
 
35
        public enum SecurityAction : ushort {
 
36
                Request = 1,
 
37
                Demand = 2,
 
38
                Assert = 3,
 
39
                Deny = 4,
 
40
                PermitOnly = 5,
 
41
                LinkDemand = 6,
 
42
                InheritDemand = 7,
 
43
                RequestMinimum = 8,
 
44
                RequestOptional = 9,
 
45
                RequestRefuse = 10,
 
46
                PreJitGrant = 11,
 
47
                PreJitDeny = 12,
 
48
                NonCasDemand = 13,
 
49
                NonCasLinkDemand = 14,
 
50
                NonCasInheritance = 15
 
51
        }
 
52
 
 
53
        public interface ISecurityDeclarationProvider : IMetadataTokenProvider {
 
54
 
 
55
                bool HasSecurityDeclarations { get; }
 
56
                Collection<SecurityDeclaration> SecurityDeclarations { get; }
 
57
        }
 
58
 
 
59
        public sealed class SecurityAttribute : ICustomAttribute {
 
60
 
 
61
                TypeReference attribute_type;
 
62
 
 
63
                internal Collection<CustomAttributeNamedArgument> fields;
 
64
                internal Collection<CustomAttributeNamedArgument> properties;
 
65
 
 
66
                public TypeReference AttributeType {
 
67
                        get { return attribute_type; }
 
68
                        set { attribute_type = value; }
 
69
                }
 
70
 
 
71
                public bool HasFields {
 
72
                        get { return !fields.IsNullOrEmpty (); }
 
73
                }
 
74
 
 
75
                public Collection<CustomAttributeNamedArgument> Fields {
 
76
                        get { return fields ?? (fields = new Collection<CustomAttributeNamedArgument> ()); }
 
77
                }
 
78
 
 
79
                public bool HasProperties {
 
80
                        get { return !properties.IsNullOrEmpty (); }
 
81
                }
 
82
 
 
83
                public Collection<CustomAttributeNamedArgument> Properties {
 
84
                        get { return properties ?? (properties = new Collection<CustomAttributeNamedArgument> ()); }
 
85
                }
 
86
 
 
87
                public SecurityAttribute (TypeReference attributeType)
 
88
                {
 
89
                        this.attribute_type = attributeType;
 
90
                }
 
91
        }
 
92
 
 
93
        public sealed class SecurityDeclaration {
 
94
 
 
95
                readonly internal uint signature;
 
96
                readonly ModuleDefinition module;
 
97
 
 
98
                internal bool resolved;
 
99
                SecurityAction action;
 
100
                internal Collection<SecurityAttribute> security_attributes;
47
101
 
48
102
                public SecurityAction Action {
49
 
                        get { return m_action; }
50
 
                        set { m_action = value; }
51
 
                }
52
 
 
53
 
#if !CF_1_0 && !CF_2_0
54
 
                public PermissionSet PermissionSet {
55
 
                        get { return m_permSet; }
56
 
                        set { m_permSet = value; }
57
 
                }
58
 
#endif
59
 
 
60
 
                public bool Resolved {
61
 
                        get { return m_resolved; }
62
 
                        set { m_resolved = value; }
63
 
                }
64
 
 
65
 
                public byte [] Blob {
66
 
                        get { return m_blob; }
67
 
                        set { m_blob = value; }
68
 
                }
69
 
 
70
 
                IDictionary IAnnotationProvider.Annotations {
71
 
                        get {
72
 
                                if (m_annotations == null)
73
 
                                        m_annotations = new Hashtable ();
74
 
                                return m_annotations;
75
 
                        }
 
103
                        get { return action; }
 
104
                        set { action = value; }
 
105
                }
 
106
 
 
107
                public bool HasSecurityAttributes {
 
108
                        get {
 
109
                                Resolve ();
 
110
 
 
111
                                return !security_attributes.IsNullOrEmpty ();
 
112
                        }
 
113
                }
 
114
 
 
115
                public Collection<SecurityAttribute> SecurityAttributes {
 
116
                        get {
 
117
                                Resolve ();
 
118
 
 
119
                                return security_attributes ?? (security_attributes = new Collection<SecurityAttribute> ());
 
120
                        }
 
121
                }
 
122
 
 
123
                internal bool HasImage {
 
124
                        get { return module != null && module.HasImage; }
 
125
                }
 
126
 
 
127
                internal SecurityDeclaration (SecurityAction action, uint signature, ModuleDefinition module)
 
128
                {
 
129
                        this.action = action;
 
130
                        this.signature = signature;
 
131
                        this.module = module;
76
132
                }
77
133
 
78
134
                public SecurityDeclaration (SecurityAction action)
79
135
                {
80
 
                        m_action = action;
81
 
                }
82
 
 
83
 
                internal SecurityDeclaration (SecurityAction action, SecurityDeclarationReader reader)
84
 
                {
85
 
                        m_action = action;
86
 
                        m_reader = reader;
87
 
                }
88
 
 
89
 
                public SecurityDeclaration Clone ()
90
 
                {
91
 
                        return Clone (this);
92
 
                }
93
 
 
94
 
                internal static SecurityDeclaration Clone (SecurityDeclaration sec)
95
 
                {
96
 
                        SecurityDeclaration sd = new SecurityDeclaration (sec.Action);
97
 
                        if (!sec.Resolved) {
98
 
                                sd.Resolved = false;
99
 
                                sd.Blob = sec.Blob;
100
 
                                return sd;
101
 
                        }
102
 
 
103
 
#if !CF_1_0 && !CF_2_0
104
 
            sd.PermissionSet = sec.PermissionSet.Copy ();
105
 
#endif
106
 
                        return sd;
107
 
                }
108
 
 
109
 
                public bool Resolve ()
110
 
                {
111
 
                        if (m_resolved)
112
 
                                return true;
113
 
 
114
 
                        if (m_reader == null)
115
 
                                return false;
116
 
 
117
 
                        SecurityDeclaration clone = m_reader.FromByteArray (m_action, m_blob, true);
118
 
                        if (!clone.Resolved)
119
 
                                return false;
120
 
 
121
 
                        m_action = clone.Action;
122
 
#if !CF_1_0 && !CF_2_0
123
 
                        m_permSet = clone.PermissionSet.Copy ();
124
 
#endif
125
 
                        m_resolved = true;
126
 
 
127
 
                        return true;
128
 
                }
129
 
 
130
 
                public void Accept (IReflectionVisitor visitor)
131
 
                {
132
 
                        visitor.VisitSecurityDeclaration (this);
 
136
                        this.action = action;
 
137
                        this.resolved = true;
 
138
                }
 
139
 
 
140
                public byte [] GetBlob ()
 
141
                {
 
142
                        if (!HasImage || signature == 0)
 
143
                                throw new NotSupportedException ();
 
144
 
 
145
                        return module.Read (this, (declaration, reader) => reader.ReadSecurityDeclarationBlob (declaration.signature)); ;
 
146
                }
 
147
 
 
148
                void Resolve ()
 
149
                {
 
150
                        if (resolved || !HasImage)
 
151
                                return;
 
152
 
 
153
                        module.Read (this, (declaration, reader) => {
 
154
                                reader.ReadSecurityDeclarationSignature (declaration);
 
155
                                return this;
 
156
                        });
 
157
 
 
158
                        resolved = true;
 
159
                }
 
160
        }
 
161
 
 
162
        static partial class Mixin {
 
163
 
 
164
                public static bool GetHasSecurityDeclarations (
 
165
                        this ISecurityDeclarationProvider self,
 
166
                        ModuleDefinition module)
 
167
                {
 
168
                        return module.HasImage ()
 
169
                                ? module.Read (self, (provider, reader) => reader.HasSecurityDeclarations (provider))
 
170
                                : false;
 
171
                }
 
172
 
 
173
                public static Collection<SecurityDeclaration> GetSecurityDeclarations (
 
174
                        this ISecurityDeclarationProvider self,
 
175
                        ModuleDefinition module)
 
176
                {
 
177
                        return module.HasImage ()
 
178
                                ? module.Read (self, (provider, reader) => reader.ReadSecurityDeclarations (provider))
 
179
                                : new Collection<SecurityDeclaration> ();
133
180
                }
134
181
        }
135
182
}
136