/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. using System;
  11. using System.Diagnostics;
  12. using System.Threading;
  13. using Mono.Collections.Generic;
  14. namespace Mono.Cecil {
  15. public enum SecurityAction : ushort {
  16. Request = 1,
  17. Demand = 2,
  18. Assert = 3,
  19. Deny = 4,
  20. PermitOnly = 5,
  21. LinkDemand = 6,
  22. InheritDemand = 7,
  23. RequestMinimum = 8,
  24. RequestOptional = 9,
  25. RequestRefuse = 10,
  26. PreJitGrant = 11,
  27. PreJitDeny = 12,
  28. NonCasDemand = 13,
  29. NonCasLinkDemand = 14,
  30. NonCasInheritance = 15
  31. }
  32. public interface ISecurityDeclarationProvider : IMetadataTokenProvider {
  33. bool HasSecurityDeclarations { get; }
  34. Collection<SecurityDeclaration> SecurityDeclarations { get; }
  35. }
  36. [DebuggerDisplay ("{AttributeType}")]
  37. public sealed class SecurityAttribute : ICustomAttribute {
  38. TypeReference attribute_type;
  39. internal Collection<CustomAttributeNamedArgument> fields;
  40. internal Collection<CustomAttributeNamedArgument> properties;
  41. public TypeReference AttributeType {
  42. get { return attribute_type; }
  43. set { attribute_type = value; }
  44. }
  45. public bool HasFields {
  46. get { return !fields.IsNullOrEmpty (); }
  47. }
  48. public Collection<CustomAttributeNamedArgument> Fields {
  49. get {
  50. if (fields == null)
  51. Interlocked.CompareExchange (ref fields, new Collection<CustomAttributeNamedArgument> (), null);
  52. return fields;
  53. }
  54. }
  55. public bool HasProperties {
  56. get { return !properties.IsNullOrEmpty (); }
  57. }
  58. public Collection<CustomAttributeNamedArgument> Properties {
  59. get {
  60. if (properties == null)
  61. Interlocked.CompareExchange (ref properties, new Collection<CustomAttributeNamedArgument> (), null);
  62. return properties;
  63. }
  64. }
  65. public SecurityAttribute (TypeReference attributeType)
  66. {
  67. this.attribute_type = attributeType;
  68. }
  69. bool ICustomAttribute.HasConstructorArguments {
  70. get { return false; }
  71. }
  72. Collection<CustomAttributeArgument> ICustomAttribute.ConstructorArguments {
  73. get { throw new NotSupportedException (); }
  74. }
  75. }
  76. public sealed class SecurityDeclaration {
  77. readonly internal uint signature;
  78. byte [] blob;
  79. readonly ModuleDefinition module;
  80. internal bool resolved;
  81. SecurityAction action;
  82. internal Collection<SecurityAttribute> security_attributes;
  83. public SecurityAction Action {
  84. get { return action; }
  85. set { action = value; }
  86. }
  87. public bool HasSecurityAttributes {
  88. get {
  89. Resolve ();
  90. return !security_attributes.IsNullOrEmpty ();
  91. }
  92. }
  93. public Collection<SecurityAttribute> SecurityAttributes {
  94. get {
  95. Resolve ();
  96. if (security_attributes == null)
  97. Interlocked.CompareExchange (ref security_attributes, new Collection<SecurityAttribute> (), null);
  98. return security_attributes;
  99. }
  100. }
  101. internal bool HasImage {
  102. get { return module != null && module.HasImage; }
  103. }
  104. internal SecurityDeclaration (SecurityAction action, uint signature, ModuleDefinition module)
  105. {
  106. this.action = action;
  107. this.signature = signature;
  108. this.module = module;
  109. }
  110. public SecurityDeclaration (SecurityAction action)
  111. {
  112. this.action = action;
  113. this.resolved = true;
  114. }
  115. public SecurityDeclaration (SecurityAction action, byte [] blob)
  116. {
  117. this.action = action;
  118. this.resolved = false;
  119. this.blob = blob;
  120. }
  121. public byte [] GetBlob ()
  122. {
  123. if (blob != null)
  124. return blob;
  125. if (!HasImage || signature == 0)
  126. throw new NotSupportedException ();
  127. return module.Read (ref blob, this, (declaration, reader) => reader.ReadSecurityDeclarationBlob (declaration.signature));
  128. }
  129. void Resolve ()
  130. {
  131. if (resolved || !HasImage)
  132. return;
  133. lock (module.SyncRoot) {
  134. if (resolved)
  135. return;
  136. module.Read (this, (declaration, reader) => reader.ReadSecurityDeclarationSignature (declaration));
  137. resolved = true;
  138. }
  139. }
  140. }
  141. static partial class Mixin {
  142. public static bool GetHasSecurityDeclarations (
  143. this ISecurityDeclarationProvider self,
  144. ModuleDefinition module)
  145. {
  146. return module.HasImage () && module.Read (self, (provider, reader) => reader.HasSecurityDeclarations (provider));
  147. }
  148. public static Collection<SecurityDeclaration> GetSecurityDeclarations (
  149. this ISecurityDeclarationProvider self,
  150. ref Collection<SecurityDeclaration> variable,
  151. ModuleDefinition module)
  152. {
  153. if (module.HasImage)
  154. return module.Read (ref variable, self, (provider, reader) => reader.ReadSecurityDeclarations (provider));
  155. Interlocked.CompareExchange (ref variable, new Collection<SecurityDeclaration> (), null);
  156. return variable;
  157. }
  158. }
  159. }