~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/SecurityDeclaration.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// SecurityDeclaration.cs
 
3
//
 
4
// Author:
 
5
//   Jb Evain (jbevain@gmail.com)
 
6
//
 
7
// Copyright (c) 2008 - 2010 Jb Evain
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
//
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
//
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
 
 
31
using Mono.Collections.Generic;
 
32
 
 
33
namespace Mono.Cecil {
 
34
 
 
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;
 
101
 
 
102
                public SecurityAction Action {
 
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;
 
132
                }
 
133
 
 
134
                public SecurityDeclaration (SecurityAction action)
 
135
                {
 
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> ();
 
180
                }
 
181
        }
 
182
}