/Mono.Cecil/Mono.Cecil/SecurityDeclaration.cs

http://github.com/icsharpcode/ILSpy · C# · 192 lines · 130 code · 35 blank · 27 comment · 10 complexity · a673c801259babf412e1e83a113a73a3 MD5 · raw file

  1. //
  2. // SecurityDeclaration.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 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. using System;
  29. using System.Threading;
  30. using Mono.Collections.Generic;
  31. namespace Mono.Cecil {
  32. public enum SecurityAction : ushort {
  33. Request = 1,
  34. Demand = 2,
  35. Assert = 3,
  36. Deny = 4,
  37. PermitOnly = 5,
  38. LinkDemand = 6,
  39. InheritDemand = 7,
  40. RequestMinimum = 8,
  41. RequestOptional = 9,
  42. RequestRefuse = 10,
  43. PreJitGrant = 11,
  44. PreJitDeny = 12,
  45. NonCasDemand = 13,
  46. NonCasLinkDemand = 14,
  47. NonCasInheritance = 15
  48. }
  49. public interface ISecurityDeclarationProvider : IMetadataTokenProvider {
  50. bool HasSecurityDeclarations { get; }
  51. Collection<SecurityDeclaration> SecurityDeclarations { get; }
  52. }
  53. public sealed class SecurityAttribute : ICustomAttribute {
  54. TypeReference attribute_type;
  55. internal Collection<CustomAttributeNamedArgument> fields;
  56. internal Collection<CustomAttributeNamedArgument> properties;
  57. public TypeReference AttributeType {
  58. get { return attribute_type; }
  59. set { attribute_type = value; }
  60. }
  61. public bool HasFields {
  62. get { return !fields.IsNullOrEmpty (); }
  63. }
  64. public Collection<CustomAttributeNamedArgument> Fields {
  65. get { return fields ?? (fields = new Collection<CustomAttributeNamedArgument> ()); }
  66. }
  67. public bool HasProperties {
  68. get { return !properties.IsNullOrEmpty (); }
  69. }
  70. public Collection<CustomAttributeNamedArgument> Properties {
  71. get { return properties ?? (properties = new Collection<CustomAttributeNamedArgument> ()); }
  72. }
  73. public SecurityAttribute (TypeReference attributeType)
  74. {
  75. this.attribute_type = attributeType;
  76. }
  77. }
  78. public sealed class SecurityDeclaration {
  79. readonly internal uint signature;
  80. byte [] blob;
  81. readonly ModuleDefinition module;
  82. internal bool resolved;
  83. SecurityAction action;
  84. internal Collection<SecurityAttribute> security_attributes;
  85. public SecurityAction Action {
  86. get { return action; }
  87. set { action = value; }
  88. }
  89. public bool HasSecurityAttributes {
  90. get {
  91. Resolve ();
  92. return !security_attributes.IsNullOrEmpty ();
  93. }
  94. }
  95. public Collection<SecurityAttribute> SecurityAttributes {
  96. get {
  97. Resolve ();
  98. return security_attributes ?? (security_attributes = new Collection<SecurityAttribute> ());
  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 blob = module.Read (this, (declaration, reader) => reader.ReadSecurityDeclarationBlob (declaration.signature));
  128. }
  129. void Resolve ()
  130. {
  131. if (resolved || !HasImage)
  132. return;
  133. module.Read (this, (declaration, reader) => {
  134. reader.ReadSecurityDeclarationSignature (declaration);
  135. return this;
  136. });
  137. resolved = true;
  138. }
  139. }
  140. static partial class Mixin {
  141. public static bool GetHasSecurityDeclarations (
  142. this ISecurityDeclarationProvider self,
  143. ModuleDefinition module)
  144. {
  145. return module.HasImage () && module.Read (self, (provider, reader) => reader.HasSecurityDeclarations (provider));
  146. }
  147. public static Collection<SecurityDeclaration> GetSecurityDeclarations (
  148. this ISecurityDeclarationProvider self,
  149. ref Collection<SecurityDeclaration> variable,
  150. ModuleDefinition module)
  151. {
  152. return module.HasImage ()
  153. ? module.Read (ref variable, self, (provider, reader) => reader.ReadSecurityDeclarations (provider))
  154. : variable = new Collection<SecurityDeclaration>();
  155. }
  156. }
  157. }