~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/StructureReader.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
 
// StructureReader.cs
3
 
//
4
 
// Author:
5
 
//   Jb Evain (jbevain@gmail.com)
6
 
//
7
 
// (C) 2005 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
 
 
29
 
namespace Mono.Cecil {
30
 
 
31
 
        using System;
32
 
        using System.IO;
33
 
 
34
 
        using Mono.Cecil.Binary;
35
 
        using Mono.Cecil.Metadata;
36
 
 
37
 
        internal sealed class StructureReader : BaseStructureVisitor {
38
 
 
39
 
                ImageReader m_ir;
40
 
                Image m_img;
41
 
                bool m_manifestOnly;
42
 
                AssemblyDefinition m_asmDef;
43
 
                ModuleDefinition m_module;
44
 
                MetadataStreamCollection m_streams;
45
 
                TablesHeap m_tHeap;
46
 
                MetadataTableReader m_tableReader;
47
 
 
48
 
                public bool ManifestOnly {
49
 
                        get { return m_manifestOnly; }
50
 
                }
51
 
 
52
 
                public ImageReader ImageReader {
53
 
                        get { return m_ir; }
54
 
                }
55
 
 
56
 
                public Image Image {
57
 
                        get { return m_img; }
58
 
                }
59
 
 
60
 
                public StructureReader (ImageReader ir)
61
 
                {
62
 
                        if (ir.Image.CLIHeader == null)
63
 
                                throw new ImageFormatException ("The image is not a managed assembly");
64
 
 
65
 
                        m_ir = ir;
66
 
                        m_img = ir.Image;
67
 
                        m_streams = m_img.MetadataRoot.Streams;
68
 
                        m_tHeap = m_streams.TablesHeap;
69
 
                        m_tableReader = ir.MetadataReader.TableReader;
70
 
                }
71
 
 
72
 
                public StructureReader (ImageReader ir, bool manifestOnly) : this (ir)
73
 
                {
74
 
                        m_manifestOnly = manifestOnly;
75
 
                }
76
 
 
77
 
                byte [] ReadBlob (uint pointer)
78
 
                {
79
 
                        if (pointer == 0)
80
 
                                return new byte [0];
81
 
 
82
 
                        return m_streams.BlobHeap.Read (pointer);
83
 
                }
84
 
 
85
 
                string ReadString (uint pointer)
86
 
                {
87
 
                        return m_streams.StringsHeap [pointer];
88
 
                }
89
 
 
90
 
                public override void VisitAssemblyDefinition (AssemblyDefinition asm)
91
 
                {
92
 
                        if (!m_tHeap.HasTable (AssemblyTable.RId))
93
 
                                throw new ReflectionException ("No assembly manifest");
94
 
 
95
 
                        asm.MetadataToken = new MetadataToken (TokenType.Assembly, 1);
96
 
                        m_asmDef = asm;
97
 
 
98
 
                        switch (m_img.MetadataRoot.Header.Version) {
99
 
                        case "v1.0.3705" :
100
 
                                asm.Runtime = TargetRuntime.NET_1_0;
101
 
                                break;
102
 
                        case "v1.1.4322" :
103
 
                                asm.Runtime = TargetRuntime.NET_1_1;
104
 
                                break;
105
 
                        default :
106
 
                                asm.Runtime = TargetRuntime.NET_2_0;
107
 
                                break;
108
 
                        }
109
 
 
110
 
                        if ((m_img.PEFileHeader.Characteristics & ImageCharacteristics.Dll) != 0)
111
 
                                asm.Kind = AssemblyKind.Dll;
112
 
                        else if (m_img.PEOptionalHeader.NTSpecificFields.SubSystem == SubSystem.WindowsGui ||
113
 
                                m_img.PEOptionalHeader.NTSpecificFields.SubSystem == SubSystem.WindowsCeGui)
114
 
                                asm.Kind = AssemblyKind.Windows;
115
 
                        else
116
 
                                asm.Kind = AssemblyKind.Console;
117
 
                }
118
 
 
119
 
                public override void VisitAssemblyNameDefinition (AssemblyNameDefinition name)
120
 
                {
121
 
                        AssemblyTable atable = m_tableReader.GetAssemblyTable ();
122
 
                        AssemblyRow arow = atable [0];
123
 
                        name.Name = ReadString (arow.Name);
124
 
                        name.Flags = arow.Flags;
125
 
                        name.PublicKey = ReadBlob (arow.PublicKey);
126
 
 
127
 
                        name.Culture = ReadString (arow.Culture);
128
 
                        name.Version = new Version (
129
 
                                arow.MajorVersion, arow.MinorVersion,
130
 
                                arow.BuildNumber, arow.RevisionNumber);
131
 
                        name.HashAlgorithm = arow.HashAlgId;
132
 
                        name.MetadataToken = new MetadataToken (TokenType.Assembly, 1);
133
 
                }
134
 
 
135
 
                public override void VisitAssemblyNameReferenceCollection (AssemblyNameReferenceCollection names)
136
 
                {
137
 
                        if (!m_tHeap.HasTable (AssemblyRefTable.RId))
138
 
                                return;
139
 
 
140
 
                        AssemblyRefTable arTable = m_tableReader.GetAssemblyRefTable ();
141
 
                        for (int i = 0; i < arTable.Rows.Count; i++) {
142
 
                                AssemblyRefRow arRow = arTable [i];
143
 
                                AssemblyNameReference aname = new AssemblyNameReference (
144
 
                                        ReadString (arRow.Name),
145
 
                                        ReadString (arRow.Culture),
146
 
                                        new Version (arRow.MajorVersion, arRow.MinorVersion,
147
 
                                                                 arRow.BuildNumber, arRow.RevisionNumber));
148
 
                                aname.PublicKeyToken = ReadBlob (arRow.PublicKeyOrToken);
149
 
                                aname.Hash = ReadBlob (arRow.HashValue);
150
 
                                aname.Flags = arRow.Flags;
151
 
                                aname.MetadataToken = new MetadataToken (TokenType.AssemblyRef, (uint) i + 1);
152
 
                                names.Add (aname);
153
 
                        }
154
 
                }
155
 
 
156
 
                public override void VisitResourceCollection (ResourceCollection resources)
157
 
                {
158
 
                        if (!m_tHeap.HasTable (ManifestResourceTable.RId))
159
 
                                return;
160
 
 
161
 
                        ManifestResourceTable mrTable = m_tableReader.GetManifestResourceTable ();
162
 
                        FileTable fTable = m_tableReader.GetFileTable ();
163
 
 
164
 
                        for (int i = 0; i < mrTable.Rows.Count; i++) {
165
 
                                ManifestResourceRow mrRow = mrTable [i];
166
 
                                if (mrRow.Implementation.RID == 0) {
167
 
                                        EmbeddedResource eres = new EmbeddedResource (
168
 
                                                ReadString (mrRow.Name), mrRow.Flags);
169
 
 
170
 
                                        BinaryReader br = m_ir.MetadataReader.GetDataReader (
171
 
                                                m_img.CLIHeader.Resources.VirtualAddress);
172
 
                                        br.BaseStream.Position += mrRow.Offset;
173
 
 
174
 
                                        eres.Data = br.ReadBytes (br.ReadInt32 ());
175
 
 
176
 
                                        resources.Add (eres);
177
 
                                        continue;
178
 
                                }
179
 
 
180
 
                                switch (mrRow.Implementation.TokenType) {
181
 
                                case TokenType.File :
182
 
                                        FileRow fRow = fTable [(int) mrRow.Implementation.RID - 1];
183
 
                                        LinkedResource lres = new LinkedResource (
184
 
                                                ReadString (mrRow.Name), mrRow.Flags,
185
 
                                                ReadString (fRow.Name));
186
 
                                        lres.Hash = ReadBlob (fRow.HashValue);
187
 
                                        resources.Add (lres);
188
 
                                        break;
189
 
                                case TokenType.AssemblyRef :
190
 
                                        AssemblyNameReference asm =
191
 
                                                m_module.AssemblyReferences [(int) mrRow.Implementation.RID - 1];
192
 
                                        AssemblyLinkedResource alr = new AssemblyLinkedResource (
193
 
                                                ReadString (mrRow.Name),
194
 
                                                mrRow.Flags, asm);
195
 
                                        resources.Add (alr);
196
 
                                        break;
197
 
                                }
198
 
                        }
199
 
                }
200
 
 
201
 
                public override void VisitModuleDefinitionCollection (ModuleDefinitionCollection modules)
202
 
                {
203
 
                        ModuleTable mt = m_tableReader.GetModuleTable ();
204
 
                        if (mt == null || mt.Rows.Count != 1)
205
 
                                throw new ReflectionException ("Can not read main module");
206
 
 
207
 
                        ModuleRow mr = mt [0];
208
 
                        string name = ReadString (mr.Name);
209
 
                        ModuleDefinition main = new ModuleDefinition (name, m_asmDef, this, true);
210
 
                        main.Mvid = m_streams.GuidHeap [mr.Mvid];
211
 
                        main.MetadataToken = new MetadataToken (TokenType.Module, 1);
212
 
                        modules.Add (main);
213
 
                        m_module = main;
214
 
                        m_module.Accept (this);
215
 
 
216
 
                        FileTable ftable = m_tableReader.GetFileTable ();
217
 
                        if (ftable == null || ftable.Rows.Count == 0)
218
 
                                return;
219
 
 
220
 
                        foreach (FileRow frow in ftable.Rows) {
221
 
                                if (frow.Flags != FileAttributes.ContainsMetaData)
222
 
                                        continue;
223
 
 
224
 
                                name = ReadString (frow.Name);
225
 
                                FileInfo location = new FileInfo (
226
 
                                        m_img.FileInformation != null ? Path.Combine (m_img.FileInformation.DirectoryName, name) : name);
227
 
                                if (!File.Exists (location.FullName))
228
 
                                        throw new FileNotFoundException ("Module not found : " + name);
229
 
 
230
 
                                try {
231
 
                                        ImageReader module = ImageReader.Read (location.FullName);
232
 
                                        mt = module.Image.MetadataRoot.Streams.TablesHeap [ModuleTable.RId] as ModuleTable;
233
 
                                        if (mt == null || mt.Rows.Count != 1)
234
 
                                                throw new ReflectionException ("Can not read module : " + name);
235
 
 
236
 
                                        mr = mt [0];
237
 
                                        ModuleDefinition modext = new ModuleDefinition (name, m_asmDef,
238
 
                                                new StructureReader (module, m_manifestOnly), false);
239
 
                                        modext.Mvid = module.Image.MetadataRoot.Streams.GuidHeap [mr.Mvid];
240
 
 
241
 
                                        modules.Add (modext);
242
 
                                        modext.Accept (this);
243
 
                                } catch (ReflectionException) {
244
 
                                        throw;
245
 
                                } catch (Exception e) {
246
 
                                        throw new ReflectionException ("Can not read module : " + name, e);
247
 
                                }
248
 
                        }
249
 
                }
250
 
 
251
 
                public override void VisitModuleReferenceCollection (ModuleReferenceCollection modules)
252
 
                {
253
 
                        if (!m_tHeap.HasTable (ModuleRefTable.RId))
254
 
                                return;
255
 
 
256
 
                        ModuleRefTable mrTable = m_tableReader.GetModuleRefTable ();
257
 
                        for (int i = 0; i < mrTable.Rows.Count; i++) {
258
 
                                ModuleRefRow mrRow = mrTable [i];
259
 
                                ModuleReference mod = new ModuleReference (ReadString (mrRow.Name));
260
 
                                mod.MetadataToken = MetadataToken.FromMetadataRow (TokenType.ModuleRef, i);
261
 
                                modules.Add (mod);
262
 
                        }
263
 
                }
264
 
 
265
 
                public override void TerminateAssemblyDefinition (AssemblyDefinition asm)
266
 
                {
267
 
                        if (m_manifestOnly)
268
 
                                return;
269
 
 
270
 
                        foreach (ModuleDefinition mod in asm.Modules)
271
 
                                mod.Controller.Reader.VisitModuleDefinition (mod);
272
 
                }
273
 
        }
274
 
}