~ubuntu-branches/ubuntu/trusty/mono-addins/trusty-proposed

« back to all changes in this revision

Viewing changes to Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/SecurityDeclarationReader.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-04-25 11:11:33 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110425111133-t05u5p7o5fxx70fu
Tags: 0.6-2
Upload to Unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// SecurityDeclarationReader.cs
3
 
//
4
 
// Author:
5
 
//      Sebastien Pouliot <sebastien@ximian.com>
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 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);
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
 
                                        IPermission p = sa.CreatePermission ();
99
 
                                        dec.PermissionSet.AddPermission (p);
100
 
                                }
101
 
                        } else {
102
 
                                Parser.LoadXml (Encoding.Unicode.GetString (declaration));
103
 
                                try {
104
 
                                        dec.PermissionSet.FromXml (Parser.ToXml ());
105
 
                                        dec.PermissionSet.ToXml ();
106
 
                                } catch {
107
 
                                        dec.Resolved = false;
108
 
                                        dec.Blob = declaration;
109
 
                                }
110
 
                        }
111
 
#endif
112
 
                        return dec;
113
 
                }
114
 
 
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)
117
 
                {
118
 
                        string cname = SignatureReader.ReadUTF8String (permset, pos, out start);
119
 
                        Type secattr = null;
120
 
 
121
 
                        // note: the SecurityAction parameter isn't important to generate the XML
122
 
                        SSP.SecurityAttribute sa = null;
123
 
                        try {
124
 
                                secattr = Type.GetType (cname, false);
125
 
                                if (secattr == null)
126
 
                                        return null;
127
 
 
128
 
                                sa = Activator.CreateInstance (secattr, new object [] {(SSP.SecurityAction) action}) as SSP.SecurityAttribute;
129
 
                        } catch {}
130
 
 
131
 
                        if (sa == null)
132
 
                                return null;
133
 
 
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);
137
 
                        if (numparams == 0)
138
 
                                return sa;
139
 
 
140
 
                        br.BaseStream.Position = start;
141
 
                        for (int j = 0; j < numparams; j++) {
142
 
                                bool read = false;
143
 
                                CustomAttrib.NamedArg na = sr.ReadNamedArg (permset, br, ref read, resolve);
144
 
                                if (!read)
145
 
                                        return null;
146
 
 
147
 
                                if (na.Field) {
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);
153
 
                                }
154
 
                        }
155
 
 
156
 
                        return sa;
157
 
                }
158
 
#endif
159
 
        }
160
 
}