PageRenderTime 36ms CodeModel.GetById 26ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/Mono.Cecil/SecurityDeclaration.cs

http://github.com/jbevain/cecil
C# | 201 lines | 150 code | 42 blank | 9 comment | 18 complexity | f82e680363bcd72d1e63f1a3735fcb83 MD5 | raw file
  1//
  2// Author:
  3//   Jb Evain (jbevain@gmail.com)
  4//
  5// Copyright (c) 2008 - 2015 Jb Evain
  6// Copyright (c) 2008 - 2011 Novell, Inc.
  7//
  8// Licensed under the MIT/X11 license.
  9//
 10
 11using System;
 12using System.Diagnostics;
 13using System.Threading;
 14using Mono.Collections.Generic;
 15
 16namespace Mono.Cecil {
 17
 18	public enum SecurityAction : ushort {
 19		Request = 1,
 20		Demand = 2,
 21		Assert = 3,
 22		Deny = 4,
 23		PermitOnly = 5,
 24		LinkDemand = 6,
 25		InheritDemand = 7,
 26		RequestMinimum = 8,
 27		RequestOptional = 9,
 28		RequestRefuse = 10,
 29		PreJitGrant = 11,
 30		PreJitDeny = 12,
 31		NonCasDemand = 13,
 32		NonCasLinkDemand = 14,
 33		NonCasInheritance = 15
 34	}
 35
 36	public interface ISecurityDeclarationProvider : IMetadataTokenProvider {
 37
 38		bool HasSecurityDeclarations { get; }
 39		Collection<SecurityDeclaration> SecurityDeclarations { get; }
 40	}
 41
 42	[DebuggerDisplay ("{AttributeType}")]
 43	public sealed class SecurityAttribute : ICustomAttribute {
 44
 45		TypeReference attribute_type;
 46
 47		internal Collection<CustomAttributeNamedArgument> fields;
 48		internal Collection<CustomAttributeNamedArgument> properties;
 49
 50		public TypeReference AttributeType {
 51			get { return attribute_type; }
 52			set { attribute_type = value; }
 53		}
 54
 55		public bool HasFields {
 56			get { return !fields.IsNullOrEmpty (); }
 57		}
 58
 59		public Collection<CustomAttributeNamedArgument> Fields {
 60			get {
 61				if (fields == null)
 62					Interlocked.CompareExchange (ref fields, new Collection<CustomAttributeNamedArgument> (), null);
 63
 64				return fields;
 65			}
 66		}
 67
 68		public bool HasProperties {
 69			get { return !properties.IsNullOrEmpty (); }
 70		}
 71		
 72		public Collection<CustomAttributeNamedArgument> Properties { 
 73			get {
 74				if (properties == null)
 75					Interlocked.CompareExchange (ref properties, new Collection<CustomAttributeNamedArgument> (), null);
 76
 77				return properties;
 78			}
 79		}
 80
 81		public SecurityAttribute (TypeReference attributeType)
 82		{
 83			this.attribute_type = attributeType;
 84		}
 85
 86		bool ICustomAttribute.HasConstructorArguments {
 87			get { return false; }
 88		}
 89
 90		Collection<CustomAttributeArgument> ICustomAttribute.ConstructorArguments {
 91			get { throw new NotSupportedException (); }
 92		}
 93	}
 94
 95	public sealed class SecurityDeclaration {
 96
 97		readonly internal uint signature;
 98		byte [] blob;
 99		readonly ModuleDefinition module;
100
101		internal bool resolved;
102		SecurityAction action;
103		internal Collection<SecurityAttribute> security_attributes;
104
105		public SecurityAction Action {
106			get { return action; }
107			set { action = value; }
108		}
109
110		public bool HasSecurityAttributes {
111			get {
112				Resolve ();
113
114				return !security_attributes.IsNullOrEmpty ();
115			}
116		}
117
118		public Collection<SecurityAttribute> SecurityAttributes {
119			get {
120				Resolve ();
121
122				if (security_attributes == null) 
123					Interlocked.CompareExchange (ref security_attributes, new Collection<SecurityAttribute> (), null);
124
125				return security_attributes;
126			}
127		}
128
129		internal bool HasImage {
130			get { return module != null && module.HasImage; }
131		}
132
133		internal SecurityDeclaration (SecurityAction action, uint signature, ModuleDefinition module)
134		{
135			this.action = action;
136			this.signature = signature;
137			this.module = module;
138		}
139
140		public SecurityDeclaration (SecurityAction action)
141		{
142			this.action = action;
143			this.resolved = true;
144		}
145
146		public SecurityDeclaration (SecurityAction action, byte [] blob)
147		{
148			this.action = action;
149			this.resolved = false;
150			this.blob = blob;
151		}
152
153		public byte [] GetBlob ()
154		{
155			if (blob != null)
156				return blob;
157
158			if (!HasImage || signature == 0)
159				throw new NotSupportedException ();
160
161			return module.Read (ref blob, this, (declaration, reader) => reader.ReadSecurityDeclarationBlob (declaration.signature));
162		}
163
164		void Resolve ()
165		{
166			if (resolved || !HasImage)
167				return;
168
169			lock (module.SyncRoot) {
170
171				if (resolved)
172					return;
173
174				module.Read (this, (declaration, reader) => reader.ReadSecurityDeclarationSignature (declaration));
175				resolved = true;
176			}
177		}
178	}
179
180	static partial class Mixin {
181
182		public static bool GetHasSecurityDeclarations (
183			this ISecurityDeclarationProvider self,
184			ModuleDefinition module)
185		{
186			return module.HasImage () && module.Read (self, (provider, reader) => reader.HasSecurityDeclarations (provider));
187		}
188
189		public static Collection<SecurityDeclaration> GetSecurityDeclarations (
190			this ISecurityDeclarationProvider self,
191			ref Collection<SecurityDeclaration> variable,
192			ModuleDefinition module)
193		{
194			if (module.HasImage)
195				return module.Read (ref variable, self, (provider, reader) => reader.ReadSecurityDeclarations (provider));
196
197			Interlocked.CompareExchange (ref variable, new Collection<SecurityDeclaration> (), null);
198			return variable;
199		}
200	}
201}