2
// SecurityDeclarationReader.cs
5
// Sebastien Pouliot <sebastien@ximian.com>
7
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
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.
29
namespace Mono.Cecil {
33
using System.Reflection;
34
using System.Security;
35
using SSP = System.Security.Permissions;
38
using Mono.Cecil.Metadata;
39
using Mono.Cecil.Signatures;
42
internal class SecurityDeclarationReader {
44
private SecurityParser m_parser;
45
private SignatureReader sr;
47
public SecurityDeclarationReader (MetadataRoot root, ReflectionReader reader)
49
sr = new SignatureReader (root, reader);
52
public SecurityParser Parser {
55
m_parser = new SecurityParser ();
60
public SecurityDeclaration FromByteArray (SecurityAction action, byte [] declaration)
62
return FromByteArray (action, declaration, false);
65
static bool IsEmptyDeclaration (byte [] declaration)
67
return declaration == null || declaration.Length == 0 ||
68
(declaration.Length == 1 && declaration [0] == 0);
71
public SecurityDeclaration FromByteArray (SecurityAction action, byte [] declaration, bool resolve)
73
SecurityDeclaration dec = new SecurityDeclaration (action);
74
#if !CF_1_0 && !CF_2_0
75
dec.PermissionSet = new PermissionSet (SSP.PermissionState.None);
77
if (IsEmptyDeclaration (declaration))
80
if (declaration[0] == 0x2e) {
81
// new binary format introduced in 2.0
84
int numattr = Utilities.ReadCompressedInteger (declaration, pos, out start);
88
BinaryReader br = new BinaryReader (new MemoryStream (declaration));
89
for (int i = 0; i < numattr; i++) {
91
SSP.SecurityAttribute sa = CreateSecurityAttribute (action, br, declaration, pos, out start, resolve);
94
dec.Blob = declaration;
98
IPermission p = sa.CreatePermission ();
99
dec.PermissionSet.AddPermission (p);
102
Parser.LoadXml (Encoding.Unicode.GetString (declaration));
104
dec.PermissionSet.FromXml (Parser.ToXml ());
105
dec.PermissionSet.ToXml ();
107
dec.Resolved = false;
108
dec.Blob = declaration;
115
#if !CF_1_0 && !CF_2_0
116
private SSP.SecurityAttribute CreateSecurityAttribute (SecurityAction action, BinaryReader br, byte [] permset, int pos, out int start, bool resolve)
118
string cname = SignatureReader.ReadUTF8String (permset, pos, out start);
121
// note: the SecurityAction parameter isn't important to generate the XML
122
SSP.SecurityAttribute sa = null;
124
secattr = Type.GetType (cname, false);
128
sa = Activator.CreateInstance (secattr, new object [] {(SSP.SecurityAction) action}) as SSP.SecurityAttribute;
134
// encoded length of all parameters (we don't need the value - except the updated pos)
135
Utilities.ReadCompressedInteger (permset, start, out pos);
136
int numparams = Utilities.ReadCompressedInteger (permset, pos, out start);
140
br.BaseStream.Position = start;
141
for (int j = 0; j < numparams; j++) {
143
CustomAttrib.NamedArg na = sr.ReadNamedArg (permset, br, ref read, resolve);
148
FieldInfo fi = secattr.GetField (na.FieldOrPropName);
149
fi.SetValue (sa, na.FixedArg.Elems[0].Value);
150
} else if (na.Property) {
151
PropertyInfo pi = secattr.GetProperty (na.FieldOrPropName);
152
pi.SetValue (sa, na.FixedArg.Elems[0].Value, null);