1 //
2 // MetadataRowReader.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // Generated by /CodeGen/cecil-gen.rb do not edit
8 // Sat Feb 16 23:24:17 +0100 2008
9 //
10 // (C) 2005 Jb Evain
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 
32 namespace Mono.Cecil.Metadata {
33 
34 	using System;
35 	using System.Collections;
36 	using System.IO;
37 
38 	using Mono.Cecil.Binary;
39 
40 	sealed class MetadataRowReader : BaseMetadataRowVisitor {
41 
42 		MetadataTableReader m_mtrv;
43 		BinaryReader m_binaryReader;
44 		MetadataRoot m_metadataRoot;
45 
46 		Utilities.TableRowCounter m_counter;
47 		int [] m_ciCache;
48 
49 		int m_blobHeapIdxSz;
50 		int m_stringsHeapIdxSz;
51 		int m_guidHeapIdxSz;
52 
MetadataRowReader(MetadataTableReader mtrv)53 		public MetadataRowReader (MetadataTableReader mtrv)
54 		{
55 			m_mtrv = mtrv;
56 			m_binaryReader = mtrv.GetReader ();
57 			m_metadataRoot = mtrv.GetMetadataRoot ();
58 			m_ciCache = new int [13];
59 			m_counter = new Utilities.TableRowCounter (m_mtrv.GetNumberOfRows);
60 		}
61 
GetIndexSize(int rid)62 		int GetIndexSize (int rid)
63 		{
64 			return m_mtrv.GetNumberOfRows (rid) < (1 << 16) ? 2 : 4;
65 		}
66 
GetCodedIndexSize(CodedIndex ci)67 		int GetCodedIndexSize (CodedIndex ci)
68 		{
69 			return Utilities.GetCodedIndexSize (ci, m_counter, m_ciCache);
70 		}
71 
ReadByIndexSize(int size)72 		uint ReadByIndexSize (int size)
73 		{
74 			if (size == 2) {
75 				return (uint) m_binaryReader.ReadUInt16 ();
76 			} else if (size == 4) {
77 				return m_binaryReader.ReadUInt32 ();
78 			} else {
79 				throw new MetadataFormatException ("Non valid size for indexing");
80 			}
81 		}
82 
VisitRowCollection(RowCollection coll)83 		public override void VisitRowCollection (RowCollection coll)
84 		{
85 			m_blobHeapIdxSz = m_metadataRoot.Streams.BlobHeap != null ?
86 				m_metadataRoot.Streams.BlobHeap.IndexSize : 2;
87 			m_stringsHeapIdxSz = m_metadataRoot.Streams.StringsHeap != null ?
88 				m_metadataRoot.Streams.StringsHeap.IndexSize : 2;
89 			m_guidHeapIdxSz = m_metadataRoot.Streams.GuidHeap != null ?
90 				m_metadataRoot.Streams.GuidHeap.IndexSize : 2;
91 		}
92 
VisitAssemblyRow(AssemblyRow row)93 		public override void VisitAssemblyRow (AssemblyRow row)
94 		{
95 			row.HashAlgId = (Mono.Cecil.AssemblyHashAlgorithm) m_binaryReader.ReadUInt32 ();
96 			row.MajorVersion = m_binaryReader.ReadUInt16 ();
97 			row.MinorVersion = m_binaryReader.ReadUInt16 ();
98 			row.BuildNumber = m_binaryReader.ReadUInt16 ();
99 			row.RevisionNumber = m_binaryReader.ReadUInt16 ();
100 			row.Flags = (Mono.Cecil.AssemblyFlags) m_binaryReader.ReadUInt32 ();
101 			row.PublicKey = ReadByIndexSize (m_blobHeapIdxSz);
102 			row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
103 			row.Culture = ReadByIndexSize (m_stringsHeapIdxSz);
104 		}
VisitAssemblyOSRow(AssemblyOSRow row)105 		public override void VisitAssemblyOSRow (AssemblyOSRow row)
106 		{
107 			row.OSPlatformID = m_binaryReader.ReadUInt32 ();
108 			row.OSMajorVersion = m_binaryReader.ReadUInt32 ();
109 			row.OSMinorVersion = m_binaryReader.ReadUInt32 ();
110 		}
VisitAssemblyProcessorRow(AssemblyProcessorRow row)111 		public override void VisitAssemblyProcessorRow (AssemblyProcessorRow row)
112 		{
113 			row.Processor = m_binaryReader.ReadUInt32 ();
114 		}
VisitAssemblyRefRow(AssemblyRefRow row)115 		public override void VisitAssemblyRefRow (AssemblyRefRow row)
116 		{
117 			row.MajorVersion = m_binaryReader.ReadUInt16 ();
118 			row.MinorVersion = m_binaryReader.ReadUInt16 ();
119 			row.BuildNumber = m_binaryReader.ReadUInt16 ();
120 			row.RevisionNumber = m_binaryReader.ReadUInt16 ();
121 			row.Flags = (Mono.Cecil.AssemblyFlags) m_binaryReader.ReadUInt32 ();
122 			row.PublicKeyOrToken = ReadByIndexSize (m_blobHeapIdxSz);
123 			row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
124 			row.Culture = ReadByIndexSize (m_stringsHeapIdxSz);
125 			row.HashValue = ReadByIndexSize (m_blobHeapIdxSz);
126 		}
VisitAssemblyRefOSRow(AssemblyRefOSRow row)127 		public override void VisitAssemblyRefOSRow (AssemblyRefOSRow row)
128 		{
129 			row.OSPlatformID = m_binaryReader.ReadUInt32 ();
130 			row.OSMajorVersion = m_binaryReader.ReadUInt32 ();
131 			row.OSMinorVersion = m_binaryReader.ReadUInt32 ();
132 			row.AssemblyRef = ReadByIndexSize (GetIndexSize (AssemblyRefTable.RId));
133 		}
VisitAssemblyRefProcessorRow(AssemblyRefProcessorRow row)134 		public override void VisitAssemblyRefProcessorRow (AssemblyRefProcessorRow row)
135 		{
136 			row.Processor = m_binaryReader.ReadUInt32 ();
137 			row.AssemblyRef = ReadByIndexSize (GetIndexSize (AssemblyRefTable.RId));
138 		}
VisitClassLayoutRow(ClassLayoutRow row)139 		public override void VisitClassLayoutRow (ClassLayoutRow row)
140 		{
141 			row.PackingSize = m_binaryReader.ReadUInt16 ();
142 			row.ClassSize = m_binaryReader.ReadUInt32 ();
143 			row.Parent = ReadByIndexSize (GetIndexSize (TypeDefTable.RId));
144 		}
VisitConstantRow(ConstantRow row)145 		public override void VisitConstantRow (ConstantRow row)
146 		{
147 			row.Type = (Mono.Cecil.Metadata.ElementType) m_binaryReader.ReadUInt16 ();
148 			row.Parent = Utilities.GetMetadataToken (CodedIndex.HasConstant,
149 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.HasConstant)));
150 			row.Value = ReadByIndexSize (m_blobHeapIdxSz);
151 		}
VisitCustomAttributeRow(CustomAttributeRow row)152 		public override void VisitCustomAttributeRow (CustomAttributeRow row)
153 		{
154 			row.Parent = Utilities.GetMetadataToken (CodedIndex.HasCustomAttribute,
155 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.HasCustomAttribute)));
156 			row.Type = Utilities.GetMetadataToken (CodedIndex.CustomAttributeType,
157 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.CustomAttributeType)));
158 			row.Value = ReadByIndexSize (m_blobHeapIdxSz);
159 		}
VisitDeclSecurityRow(DeclSecurityRow row)160 		public override void VisitDeclSecurityRow (DeclSecurityRow row)
161 		{
162 			row.Action = (Mono.Cecil.SecurityAction) m_binaryReader.ReadInt16 ();
163 			row.Parent = Utilities.GetMetadataToken (CodedIndex.HasDeclSecurity,
164 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.HasDeclSecurity)));
165 			row.PermissionSet = ReadByIndexSize (m_blobHeapIdxSz);
166 		}
VisitEventRow(EventRow row)167 		public override void VisitEventRow (EventRow row)
168 		{
169 			row.EventFlags = (Mono.Cecil.EventAttributes) m_binaryReader.ReadUInt16 ();
170 			row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
171 			row.EventType = Utilities.GetMetadataToken (CodedIndex.TypeDefOrRef,
172 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.TypeDefOrRef)));
173 		}
VisitEventMapRow(EventMapRow row)174 		public override void VisitEventMapRow (EventMapRow row)
175 		{
176 			row.Parent = ReadByIndexSize (GetIndexSize (TypeDefTable.RId));
177 			row.EventList = ReadByIndexSize (GetIndexSize (EventTable.RId));
178 		}
VisitEventPtrRow(EventPtrRow row)179 		public override void VisitEventPtrRow (EventPtrRow row)
180 		{
181 			row.Event = ReadByIndexSize (GetIndexSize (EventTable.RId));
182 		}
VisitExportedTypeRow(ExportedTypeRow row)183 		public override void VisitExportedTypeRow (ExportedTypeRow row)
184 		{
185 			row.Flags = (Mono.Cecil.TypeAttributes) m_binaryReader.ReadUInt32 ();
186 			row.TypeDefId = m_binaryReader.ReadUInt32 ();
187 			row.TypeName = ReadByIndexSize (m_stringsHeapIdxSz);
188 			row.TypeNamespace = ReadByIndexSize (m_stringsHeapIdxSz);
189 			row.Implementation = Utilities.GetMetadataToken (CodedIndex.Implementation,
190 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.Implementation)));
191 		}
VisitFieldRow(FieldRow row)192 		public override void VisitFieldRow (FieldRow row)
193 		{
194 			row.Flags = (Mono.Cecil.FieldAttributes) m_binaryReader.ReadUInt16 ();
195 			row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
196 			row.Signature = ReadByIndexSize (m_blobHeapIdxSz);
197 		}
VisitFieldLayoutRow(FieldLayoutRow row)198 		public override void VisitFieldLayoutRow (FieldLayoutRow row)
199 		{
200 			row.Offset = m_binaryReader.ReadUInt32 ();
201 			row.Field = ReadByIndexSize (GetIndexSize (FieldTable.RId));
202 		}
VisitFieldMarshalRow(FieldMarshalRow row)203 		public override void VisitFieldMarshalRow (FieldMarshalRow row)
204 		{
205 			row.Parent = Utilities.GetMetadataToken (CodedIndex.HasFieldMarshal,
206 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.HasFieldMarshal)));
207 			row.NativeType = ReadByIndexSize (m_blobHeapIdxSz);
208 		}
VisitFieldPtrRow(FieldPtrRow row)209 		public override void VisitFieldPtrRow (FieldPtrRow row)
210 		{
211 			row.Field = ReadByIndexSize (GetIndexSize (FieldTable.RId));
212 		}
VisitFieldRVARow(FieldRVARow row)213 		public override void VisitFieldRVARow (FieldRVARow row)
214 		{
215 			row.RVA = new RVA (m_binaryReader.ReadUInt32 ());
216 			row.Field = ReadByIndexSize (GetIndexSize (FieldTable.RId));
217 		}
VisitFileRow(FileRow row)218 		public override void VisitFileRow (FileRow row)
219 		{
220 			row.Flags = (Mono.Cecil.FileAttributes) m_binaryReader.ReadUInt32 ();
221 			row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
222 			row.HashValue = ReadByIndexSize (m_blobHeapIdxSz);
223 		}
VisitGenericParamRow(GenericParamRow row)224 		public override void VisitGenericParamRow (GenericParamRow row)
225 		{
226 			row.Number = m_binaryReader.ReadUInt16 ();
227 			row.Flags = (Mono.Cecil.GenericParameterAttributes) m_binaryReader.ReadUInt16 ();
228 			row.Owner = Utilities.GetMetadataToken (CodedIndex.TypeOrMethodDef,
229 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.TypeOrMethodDef)));
230 			row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
231 		}
VisitGenericParamConstraintRow(GenericParamConstraintRow row)232 		public override void VisitGenericParamConstraintRow (GenericParamConstraintRow row)
233 		{
234 			row.Owner = ReadByIndexSize (GetIndexSize (GenericParamTable.RId));
235 			row.Constraint = Utilities.GetMetadataToken (CodedIndex.TypeDefOrRef,
236 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.TypeDefOrRef)));
237 		}
VisitImplMapRow(ImplMapRow row)238 		public override void VisitImplMapRow (ImplMapRow row)
239 		{
240 			row.MappingFlags = (Mono.Cecil.PInvokeAttributes) m_binaryReader.ReadUInt16 ();
241 			row.MemberForwarded = Utilities.GetMetadataToken (CodedIndex.MemberForwarded,
242 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.MemberForwarded)));
243 			row.ImportName = ReadByIndexSize (m_stringsHeapIdxSz);
244 			row.ImportScope = ReadByIndexSize (GetIndexSize (ModuleRefTable.RId));
245 		}
VisitInterfaceImplRow(InterfaceImplRow row)246 		public override void VisitInterfaceImplRow (InterfaceImplRow row)
247 		{
248 			row.Class = ReadByIndexSize (GetIndexSize (TypeDefTable.RId));
249 			row.Interface = Utilities.GetMetadataToken (CodedIndex.TypeDefOrRef,
250 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.TypeDefOrRef)));
251 		}
VisitManifestResourceRow(ManifestResourceRow row)252 		public override void VisitManifestResourceRow (ManifestResourceRow row)
253 		{
254 			row.Offset = m_binaryReader.ReadUInt32 ();
255 			row.Flags = (Mono.Cecil.ManifestResourceAttributes) m_binaryReader.ReadUInt32 ();
256 			row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
257 			row.Implementation = Utilities.GetMetadataToken (CodedIndex.Implementation,
258 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.Implementation)));
259 		}
VisitMemberRefRow(MemberRefRow row)260 		public override void VisitMemberRefRow (MemberRefRow row)
261 		{
262 			row.Class = Utilities.GetMetadataToken (CodedIndex.MemberRefParent,
263 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.MemberRefParent)));
264 			row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
265 			row.Signature = ReadByIndexSize (m_blobHeapIdxSz);
266 		}
VisitMethodRow(MethodRow row)267 		public override void VisitMethodRow (MethodRow row)
268 		{
269 			row.RVA = new RVA (m_binaryReader.ReadUInt32 ());
270 			row.ImplFlags = (Mono.Cecil.MethodImplAttributes) m_binaryReader.ReadUInt16 ();
271 			row.Flags = (Mono.Cecil.MethodAttributes) m_binaryReader.ReadUInt16 ();
272 			row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
273 			row.Signature = ReadByIndexSize (m_blobHeapIdxSz);
274 			row.ParamList = ReadByIndexSize (GetIndexSize (ParamTable.RId));
275 		}
VisitMethodImplRow(MethodImplRow row)276 		public override void VisitMethodImplRow (MethodImplRow row)
277 		{
278 			row.Class = ReadByIndexSize (GetIndexSize (TypeDefTable.RId));
279 			row.MethodBody = Utilities.GetMetadataToken (CodedIndex.MethodDefOrRef,
280 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.MethodDefOrRef)));
281 			row.MethodDeclaration = Utilities.GetMetadataToken (CodedIndex.MethodDefOrRef,
282 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.MethodDefOrRef)));
283 		}
VisitMethodPtrRow(MethodPtrRow row)284 		public override void VisitMethodPtrRow (MethodPtrRow row)
285 		{
286 			row.Method = ReadByIndexSize (GetIndexSize (MethodTable.RId));
287 		}
VisitMethodSemanticsRow(MethodSemanticsRow row)288 		public override void VisitMethodSemanticsRow (MethodSemanticsRow row)
289 		{
290 			row.Semantics = (Mono.Cecil.MethodSemanticsAttributes) m_binaryReader.ReadUInt16 ();
291 			row.Method = ReadByIndexSize (GetIndexSize (MethodTable.RId));
292 			row.Association = Utilities.GetMetadataToken (CodedIndex.HasSemantics,
293 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.HasSemantics)));
294 		}
VisitMethodSpecRow(MethodSpecRow row)295 		public override void VisitMethodSpecRow (MethodSpecRow row)
296 		{
297 			row.Method = Utilities.GetMetadataToken (CodedIndex.MethodDefOrRef,
298 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.MethodDefOrRef)));
299 			row.Instantiation = ReadByIndexSize (m_blobHeapIdxSz);
300 		}
VisitModuleRow(ModuleRow row)301 		public override void VisitModuleRow (ModuleRow row)
302 		{
303 			row.Generation = m_binaryReader.ReadUInt16 ();
304 			row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
305 			row.Mvid = ReadByIndexSize (m_guidHeapIdxSz);
306 			row.EncId = ReadByIndexSize (m_guidHeapIdxSz);
307 			row.EncBaseId = ReadByIndexSize (m_guidHeapIdxSz);
308 		}
VisitModuleRefRow(ModuleRefRow row)309 		public override void VisitModuleRefRow (ModuleRefRow row)
310 		{
311 			row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
312 		}
VisitNestedClassRow(NestedClassRow row)313 		public override void VisitNestedClassRow (NestedClassRow row)
314 		{
315 			row.NestedClass = ReadByIndexSize (GetIndexSize (TypeDefTable.RId));
316 			row.EnclosingClass = ReadByIndexSize (GetIndexSize (TypeDefTable.RId));
317 		}
VisitParamRow(ParamRow row)318 		public override void VisitParamRow (ParamRow row)
319 		{
320 			row.Flags = (Mono.Cecil.ParameterAttributes) m_binaryReader.ReadUInt16 ();
321 			row.Sequence = m_binaryReader.ReadUInt16 ();
322 			row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
323 		}
VisitParamPtrRow(ParamPtrRow row)324 		public override void VisitParamPtrRow (ParamPtrRow row)
325 		{
326 			row.Param = ReadByIndexSize (GetIndexSize (ParamTable.RId));
327 		}
VisitPropertyRow(PropertyRow row)328 		public override void VisitPropertyRow (PropertyRow row)
329 		{
330 			row.Flags = (Mono.Cecil.PropertyAttributes) m_binaryReader.ReadUInt16 ();
331 			row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
332 			row.Type = ReadByIndexSize (m_blobHeapIdxSz);
333 		}
VisitPropertyMapRow(PropertyMapRow row)334 		public override void VisitPropertyMapRow (PropertyMapRow row)
335 		{
336 			row.Parent = ReadByIndexSize (GetIndexSize (TypeDefTable.RId));
337 			row.PropertyList = ReadByIndexSize (GetIndexSize (PropertyTable.RId));
338 		}
VisitPropertyPtrRow(PropertyPtrRow row)339 		public override void VisitPropertyPtrRow (PropertyPtrRow row)
340 		{
341 			row.Property = ReadByIndexSize (GetIndexSize (PropertyTable.RId));
342 		}
VisitStandAloneSigRow(StandAloneSigRow row)343 		public override void VisitStandAloneSigRow (StandAloneSigRow row)
344 		{
345 			row.Signature = ReadByIndexSize (m_blobHeapIdxSz);
346 		}
VisitTypeDefRow(TypeDefRow row)347 		public override void VisitTypeDefRow (TypeDefRow row)
348 		{
349 			row.Flags = (Mono.Cecil.TypeAttributes) m_binaryReader.ReadUInt32 ();
350 			row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
351 			row.Namespace = ReadByIndexSize (m_stringsHeapIdxSz);
352 			row.Extends = Utilities.GetMetadataToken (CodedIndex.TypeDefOrRef,
353 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.TypeDefOrRef)));
354 			row.FieldList = ReadByIndexSize (GetIndexSize (FieldTable.RId));
355 			row.MethodList = ReadByIndexSize (GetIndexSize (MethodTable.RId));
356 		}
VisitTypeRefRow(TypeRefRow row)357 		public override void VisitTypeRefRow (TypeRefRow row)
358 		{
359 			row.ResolutionScope = Utilities.GetMetadataToken (CodedIndex.ResolutionScope,
360 				ReadByIndexSize (GetCodedIndexSize (CodedIndex.ResolutionScope)));
361 			row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
362 			row.Namespace = ReadByIndexSize (m_stringsHeapIdxSz);
363 		}
VisitTypeSpecRow(TypeSpecRow row)364 		public override void VisitTypeSpecRow (TypeSpecRow row)
365 		{
366 			row.Signature = ReadByIndexSize (m_blobHeapIdxSz);
367 		}
368 	}
369 }
370