Debian Codegraph @

mcs/tools/cil-strip/Mono.Cecil/SecurityDeclarationReader.cs

raw
1 //
2 // SecurityDeclarationReader.cs
3 //
4 // Author:
5 // Sebastien Pouliot <[email protected]>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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 namespace Mono.Cecil {
30
31 using System;
32 using System.IO;
33 using System.Reflection;
34 using System.Security;
35 using SSP = System.Security.Permissions;
36 using System.Text;
37
38 using Mono.Cecil.Metadata;
39 using Mono.Cecil.Signatures;
40 using Mono.Xml;
41
42 internal sealed class SecurityDeclarationReader {
43
44 private SecurityParser m_parser;
45 private SignatureReader sr;
46
47 public SecurityDeclarationReader (MetadataRoot root, ReflectionReader reader)
48 {
49 sr = new SignatureReader (root, reader);
50 }
51
52 public SecurityParser Parser {
53 get {
54 if (m_parser == null)
55 m_parser = new SecurityParser ();
56 return m_parser;
57 }
58 }
59
60 public SecurityDeclaration FromByteArray (SecurityAction action, byte [] declaration)
61 {
62 return FromByteArray (action, declaration, false);
63 }
64
65 static bool IsEmptyDeclaration (byte [] declaration)
66 {
67 return declaration == null || declaration.Length == 0 ||
68 (declaration.Length == 1 && declaration [0] == 0);
69 }
70
71 public SecurityDeclaration FromByteArray (SecurityAction action, byte [] declaration, bool resolve)
72 {
73 SecurityDeclaration dec = new SecurityDeclaration (action, this);
74 #if !CF_1_0 && !CF_2_0
75 dec.PermissionSet = new PermissionSet (SSP.PermissionState.None);
76
77 if (IsEmptyDeclaration (declaration))
78 return dec;
79
80 if (declaration[0] == 0x2e) {
81 // new binary format introduced in 2.0
82 int pos = 1;
83 int start;
84 int numattr = Utilities.ReadCompressedInteger (declaration, pos, out start);
85 if (numattr == 0)
86 return dec;
87
88 BinaryReader br = new BinaryReader (new MemoryStream (declaration));
89 for (int i = 0; i < numattr; i++) {
90 pos = start;
91 SSP.SecurityAttribute sa = CreateSecurityAttribute (action, br, declaration, pos, out start, resolve);
92 if (sa == null) {
93 dec.Resolved = false;
94 dec.Blob = declaration;
95 return dec;
96 }
97
98 try {
99 IPermission p = sa.CreatePermission ();
100 dec.PermissionSet.AddPermission (p);
101 } catch {
102 dec.Resolved = false;
103 dec.Blob = declaration;
104 return dec;
105 }
106 }
107
108 dec.Resolved = true;
109 } else {
110 Parser.LoadXml (Encoding.Unicode.GetString (declaration));
111 try {
112 dec.PermissionSet.FromXml (Parser.ToXml ());
113 dec.PermissionSet.ToXml ();
114 dec.Resolved = true;
115 } catch {
116 dec.Resolved = false;
117 dec.Blob = declaration;
118 }
119 }
120 #endif
121 return dec;
122 }
123
124 #if !CF_1_0 && !CF_2_0
125 private SSP.SecurityAttribute CreateSecurityAttribute (SecurityAction action, BinaryReader br, byte [] permset, int pos, out int start, bool resolve)
126 {
127 string cname = SignatureReader.ReadUTF8String (permset, pos, out start);
128 Type secattr = null;
129
130 // note: the SecurityAction parameter isn't important to generate the XML
131 SSP.SecurityAttribute sa = null;
132 try {
133 secattr = Type.GetType (cname, false);
134 if (secattr == null)
135 return null;
136
137 sa = Activator.CreateInstance (secattr, new object [] {(SSP.SecurityAction) action}) as SSP.SecurityAttribute;
138 } catch {}
139
140 if (sa == null)
141 return null;
142
143 // encoded length of all parameters (we don't need the value - except the updated pos)
144 Utilities.ReadCompressedInteger (permset, start, out pos);
145 int numparams = Utilities.ReadCompressedInteger (permset, pos, out start);
146 if (numparams == 0)
147 return sa;
148
149 br.BaseStream.Position = start;
150 for (int j = 0; j < numparams; j++) {
151 bool read = true;
152 CustomAttrib.NamedArg na = sr.ReadNamedArg (permset, br, ref read, resolve);
153 if (!read)
154 return null;
155
156 if (na.Field) {
157 FieldInfo fi = secattr.GetField (na.FieldOrPropName);
158 fi.SetValue (sa, na.FixedArg.Elems[0].Value);
159 } else if (na.Property) {
160 PropertyInfo pi = secattr.GetProperty (na.FieldOrPropName);
161 pi.SetValue (sa, na.FixedArg.Elems[0].Value, null);
162 }
163 }
164
165 start = (int) br.BaseStream.Position;
166
167 return sa;
168 }
169 #endif
170 }
171 }