2007-07-27 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / Mono.Cecil / Mono.Cecil / StructureReader.cs
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                         string version = m_img.MetadataRoot.Header.Version;
99
100                         switch (version) {
101                         case "v1.0.3705" :
102                                 asm.Runtime = TargetRuntime.NET_1_0;
103                                 break;
104                         case "v1.1.4322" :
105                                 asm.Runtime = TargetRuntime.NET_1_1;
106                                 break;
107                         case "v2.0.50727" :
108                                 asm.Runtime = TargetRuntime.NET_2_0;
109                                 break;
110                         case "v2.1.20416" :
111                                 asm.Runtime = TargetRuntime.NET_2_1;
112                                 break;
113                         default:
114                                 if (version.StartsWith ("v1"))
115                                         asm.Runtime = TargetRuntime.NET_1_1;
116                                 else
117                                         asm.Runtime = TargetRuntime.NET_2_0;
118                                 break;
119                         }
120
121                         if ((m_img.PEFileHeader.Characteristics & ImageCharacteristics.Dll) != 0)
122                                 asm.Kind = AssemblyKind.Dll;
123                         else if (m_img.PEOptionalHeader.NTSpecificFields.SubSystem == SubSystem.WindowsGui ||
124                                 m_img.PEOptionalHeader.NTSpecificFields.SubSystem == SubSystem.WindowsCeGui)
125                                 asm.Kind = AssemblyKind.Windows;
126                         else
127                                 asm.Kind = AssemblyKind.Console;
128                 }
129
130                 public override void VisitAssemblyNameDefinition (AssemblyNameDefinition name)
131                 {
132                         AssemblyTable atable = m_tableReader.GetAssemblyTable ();
133                         AssemblyRow arow = atable [0];
134                         name.Name = ReadString (arow.Name);
135                         name.Flags = arow.Flags;
136                         name.PublicKey = ReadBlob (arow.PublicKey);
137
138                         name.Culture = ReadString (arow.Culture);
139                         name.Version = new Version (
140                                 arow.MajorVersion, arow.MinorVersion,
141                                 arow.BuildNumber, arow.RevisionNumber);
142                         name.HashAlgorithm = arow.HashAlgId;
143                         name.MetadataToken = new MetadataToken (TokenType.Assembly, 1);
144                 }
145
146                 public override void VisitAssemblyNameReferenceCollection (AssemblyNameReferenceCollection names)
147                 {
148                         if (!m_tHeap.HasTable (AssemblyRefTable.RId))
149                                 return;
150
151                         AssemblyRefTable arTable = m_tableReader.GetAssemblyRefTable ();
152                         for (int i = 0; i < arTable.Rows.Count; i++) {
153                                 AssemblyRefRow arRow = arTable [i];
154                                 AssemblyNameReference aname = new AssemblyNameReference (
155                                         ReadString (arRow.Name),
156                                         ReadString (arRow.Culture),
157                                         new Version (arRow.MajorVersion, arRow.MinorVersion,
158                                                                  arRow.BuildNumber, arRow.RevisionNumber));
159                                 aname.PublicKeyToken = ReadBlob (arRow.PublicKeyOrToken);
160                                 aname.Hash = ReadBlob (arRow.HashValue);
161                                 aname.Flags = arRow.Flags;
162                                 aname.MetadataToken = new MetadataToken (TokenType.AssemblyRef, (uint) i + 1);
163                                 names.Add (aname);
164                         }
165                 }
166
167                 public override void VisitResourceCollection (ResourceCollection resources)
168                 {
169                         if (!m_tHeap.HasTable (ManifestResourceTable.RId))
170                                 return;
171
172                         ManifestResourceTable mrTable = m_tableReader.GetManifestResourceTable ();
173                         FileTable fTable = m_tableReader.GetFileTable ();
174
175                         for (int i = 0; i < mrTable.Rows.Count; i++) {
176                                 ManifestResourceRow mrRow = mrTable [i];
177                                 if (mrRow.Implementation.RID == 0) {
178                                         EmbeddedResource eres = new EmbeddedResource (
179                                                 ReadString (mrRow.Name), mrRow.Flags);
180
181                                         BinaryReader br = m_ir.MetadataReader.GetDataReader (
182                                                 m_img.CLIHeader.Resources.VirtualAddress);
183                                         br.BaseStream.Position += mrRow.Offset;
184
185                                         eres.Data = br.ReadBytes (br.ReadInt32 ());
186
187                                         resources.Add (eres);
188                                         continue;
189                                 }
190
191                                 switch (mrRow.Implementation.TokenType) {
192                                 case TokenType.File :
193                                         FileRow fRow = fTable [(int) mrRow.Implementation.RID - 1];
194                                         LinkedResource lres = new LinkedResource (
195                                                 ReadString (mrRow.Name), mrRow.Flags,
196                                                 ReadString (fRow.Name));
197                                         lres.Hash = ReadBlob (fRow.HashValue);
198                                         resources.Add (lres);
199                                         break;
200                                 case TokenType.AssemblyRef :
201                                         AssemblyNameReference asm =
202                                                 m_module.AssemblyReferences [(int) mrRow.Implementation.RID - 1];
203                                         AssemblyLinkedResource alr = new AssemblyLinkedResource (
204                                                 ReadString (mrRow.Name),
205                                                 mrRow.Flags, asm);
206                                         resources.Add (alr);
207                                         break;
208                                 }
209                         }
210                 }
211
212                 public override void VisitModuleDefinitionCollection (ModuleDefinitionCollection modules)
213                 {
214                         ModuleTable mt = m_tableReader.GetModuleTable ();
215                         if (mt == null || mt.Rows.Count != 1)
216                                 throw new ReflectionException ("Can not read main module");
217
218                         ModuleRow mr = mt [0];
219                         string name = ReadString (mr.Name);
220                         ModuleDefinition main = new ModuleDefinition (name, m_asmDef, this, true);
221                         main.Mvid = m_streams.GuidHeap [mr.Mvid];
222                         main.MetadataToken = new MetadataToken (TokenType.Module, 1);
223                         modules.Add (main);
224                         m_module = main;
225                         m_module.Accept (this);
226
227                         FileTable ftable = m_tableReader.GetFileTable ();
228                         if (ftable == null || ftable.Rows.Count == 0)
229                                 return;
230
231                         foreach (FileRow frow in ftable.Rows) {
232                                 if (frow.Flags != FileAttributes.ContainsMetaData)
233                                         continue;
234
235                                 name = ReadString (frow.Name);
236                                 FileInfo location = new FileInfo (
237                                         m_img.FileInformation != null ? Path.Combine (m_img.FileInformation.DirectoryName, name) : name);
238                                 if (!File.Exists (location.FullName))
239                                         throw new FileNotFoundException ("Module not found : " + name);
240
241                                 try {
242                                         ImageReader module = ImageReader.Read (location.FullName);
243                                         mt = module.Image.MetadataRoot.Streams.TablesHeap [ModuleTable.RId] as ModuleTable;
244                                         if (mt == null || mt.Rows.Count != 1)
245                                                 throw new ReflectionException ("Can not read module : " + name);
246
247                                         mr = mt [0];
248                                         ModuleDefinition modext = new ModuleDefinition (name, m_asmDef,
249                                                 new StructureReader (module, m_manifestOnly), false);
250                                         modext.Mvid = module.Image.MetadataRoot.Streams.GuidHeap [mr.Mvid];
251
252                                         modules.Add (modext);
253                                         modext.Accept (this);
254                                 } catch (ReflectionException) {
255                                         throw;
256                                 } catch (Exception e) {
257                                         throw new ReflectionException ("Can not read module : " + name, e);
258                                 }
259                         }
260                 }
261
262                 public override void VisitModuleReferenceCollection (ModuleReferenceCollection modules)
263                 {
264                         if (!m_tHeap.HasTable (ModuleRefTable.RId))
265                                 return;
266
267                         ModuleRefTable mrTable = m_tableReader.GetModuleRefTable ();
268                         for (int i = 0; i < mrTable.Rows.Count; i++) {
269                                 ModuleRefRow mrRow = mrTable [i];
270                                 ModuleReference mod = new ModuleReference (ReadString (mrRow.Name));
271                                 mod.MetadataToken = MetadataToken.FromMetadataRow (TokenType.ModuleRef, i);
272                                 modules.Add (mod);
273                         }
274                 }
275
276                 public override void TerminateAssemblyDefinition (AssemblyDefinition asm)
277                 {
278                         if (m_manifestOnly)
279                                 return;
280
281                         foreach (ModuleDefinition mod in asm.Modules)
282                                 mod.Controller.Reader.VisitModuleDefinition (mod);
283                 }
284         }
285 }