2 Copyright (C) 2009 Jeroen Frijters
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
25 using System.Collections.Generic;
28 using IKVM.Reflection.Metadata;
30 namespace IKVM.Reflection.Reader
32 sealed class MetadataReader : MetadataRW
34 private readonly BinaryReader br;
36 internal MetadataReader(ModuleReader module, BinaryReader br, byte heapSizes)
37 : base(module, (heapSizes & 0x01) != 0, (heapSizes & 0x02) != 0, (heapSizes & 0x04) != 0)
42 internal short ReadInt16()
44 return br.ReadInt16();
47 internal ushort ReadUInt16()
49 return br.ReadUInt16();
52 internal int ReadInt32()
54 return br.ReadInt32();
57 internal int ReadStringIndex()
61 return br.ReadInt32();
65 return br.ReadUInt16();
69 internal int ReadGuidIndex()
73 return br.ReadInt32();
77 return br.ReadUInt16();
81 internal int ReadBlobIndex()
85 return br.ReadInt32();
89 return br.ReadUInt16();
93 internal int ReadResolutionScope()
96 if (bigResolutionScope)
98 codedIndex = br.ReadInt32();
102 codedIndex = br.ReadUInt16();
104 switch (codedIndex & 3)
107 return (ModuleTable.Index << 24) + (codedIndex >> 2);
109 return (ModuleRefTable.Index << 24) + (codedIndex >> 2);
111 return (AssemblyRefTable.Index << 24) + (codedIndex >> 2);
113 return (TypeRefTable.Index << 24) + (codedIndex >> 2);
115 throw new BadImageFormatException();
119 internal int ReadTypeDefOrRef()
124 codedIndex = br.ReadInt32();
128 codedIndex = br.ReadUInt16();
130 switch (codedIndex & 3)
133 return (TypeDefTable.Index << 24) + (codedIndex >> 2);
135 return (TypeRefTable.Index << 24) + (codedIndex >> 2);
137 return (TypeSpecTable.Index << 24) + (codedIndex >> 2);
139 throw new BadImageFormatException();
143 internal int ReadMemberRefParent()
146 if (bigMemberRefParent)
148 codedIndex = br.ReadInt32();
152 codedIndex = br.ReadUInt16();
154 switch (codedIndex & 7)
157 return (TypeDefTable.Index << 24) + (codedIndex >> 3);
159 return (TypeRefTable.Index << 24) + (codedIndex >> 3);
161 return (ModuleRefTable.Index << 24) + (codedIndex >> 3);
163 return (MethodDefTable.Index << 24) + (codedIndex >> 3);
165 return (TypeSpecTable.Index << 24) + (codedIndex >> 3);
167 throw new BadImageFormatException();
171 internal int ReadHasCustomAttribute()
174 if (bigHasCustomAttribute)
176 codedIndex = br.ReadInt32();
180 codedIndex = br.ReadUInt16();
182 switch (codedIndex & 31)
185 return (MethodDefTable.Index << 24) + (codedIndex >> 5);
187 return (FieldTable.Index << 24) + (codedIndex >> 5);
189 return (TypeRefTable.Index << 24) + (codedIndex >> 5);
191 return (TypeDefTable.Index << 24) + (codedIndex >> 5);
193 return (ParamTable.Index << 24) + (codedIndex >> 5);
195 return (InterfaceImplTable.Index << 24) + (codedIndex >> 5);
197 return (MemberRefTable.Index << 24) + (codedIndex >> 5);
199 return (ModuleTable.Index << 24) + (codedIndex >> 5);
201 throw new BadImageFormatException();
203 return (PropertyTable.Index << 24) + (codedIndex >> 5);
205 return (EventTable.Index << 24) + (codedIndex >> 5);
207 return (StandAloneSigTable.Index << 24) + (codedIndex >> 5);
209 return (ModuleRefTable.Index << 24) + (codedIndex >> 5);
211 return (TypeSpecTable.Index << 24) + (codedIndex >> 5);
213 return (AssemblyTable.Index << 24) + (codedIndex >> 5);
215 return (AssemblyRefTable.Index << 24) + (codedIndex >> 5);
217 return (FileTable.Index << 24) + (codedIndex >> 5);
219 return (ExportedTypeTable.Index << 24) + (codedIndex >> 5);
221 return (ManifestResourceTable.Index << 24) + (codedIndex >> 5);
223 return (GenericParamTable.Index << 24) + (codedIndex >> 5);
225 throw new BadImageFormatException();
229 internal int ReadCustomAttributeType()
232 if (bigCustomAttributeType)
234 codedIndex = br.ReadInt32();
238 codedIndex = br.ReadUInt16();
240 switch (codedIndex & 7)
243 return (MethodDefTable.Index << 24) + (codedIndex >> 3);
245 return (MemberRefTable.Index << 24) + (codedIndex >> 3);
247 throw new BadImageFormatException();
251 internal int ReadMethodDefOrRef()
254 if (bigMethodDefOrRef)
256 codedIndex = br.ReadInt32();
260 codedIndex = br.ReadUInt16();
262 switch (codedIndex & 1)
265 return (MethodDefTable.Index << 24) + (codedIndex >> 1);
267 return (MemberRefTable.Index << 24) + (codedIndex >> 1);
269 throw new BadImageFormatException();
273 internal int ReadHasConstant()
278 codedIndex = br.ReadInt32();
282 codedIndex = br.ReadUInt16();
284 switch (codedIndex & 3)
287 return (FieldTable.Index << 24) + (codedIndex >> 2);
289 return (ParamTable.Index << 24) + (codedIndex >> 2);
291 return (PropertyTable.Index << 24) + (codedIndex >> 2);
293 throw new BadImageFormatException();
297 internal int ReadHasSemantics()
302 codedIndex = br.ReadInt32();
306 codedIndex = br.ReadUInt16();
308 switch (codedIndex & 1)
311 return (EventTable.Index << 24) + (codedIndex >> 1);
313 return (PropertyTable.Index << 24) + (codedIndex >> 1);
315 throw new BadImageFormatException();
319 internal int ReadHasFieldMarshal()
322 if (bigHasFieldMarshal)
324 codedIndex = br.ReadInt32();
328 codedIndex = br.ReadUInt16();
330 switch (codedIndex & 1)
333 return (FieldTable.Index << 24) + (codedIndex >> 1);
335 return (ParamTable.Index << 24) + (codedIndex >> 1);
337 throw new BadImageFormatException();
341 internal int ReadHasDeclSecurity()
344 if (bigHasDeclSecurity)
346 codedIndex = br.ReadInt32();
350 codedIndex = br.ReadUInt16();
352 switch (codedIndex & 3)
355 return (TypeDefTable.Index << 24) + (codedIndex >> 2);
357 return (MethodDefTable.Index << 24) + (codedIndex >> 2);
359 return (AssemblyTable.Index << 24) + (codedIndex >> 2);
361 throw new BadImageFormatException();
365 internal int ReadTypeOrMethodDef()
368 if (bigTypeOrMethodDef)
370 codedIndex = br.ReadInt32();
374 codedIndex = br.ReadUInt16();
376 switch (codedIndex & 1)
379 return (TypeDefTable.Index << 24) + (codedIndex >> 1);
381 return (MethodDefTable.Index << 24) + (codedIndex >> 1);
383 throw new BadImageFormatException();
387 internal int ReadMemberForwarded()
390 if (bigMemberForwarded)
392 codedIndex = br.ReadInt32();
396 codedIndex = br.ReadUInt16();
398 switch (codedIndex & 1)
401 return (FieldTable.Index << 24) + (codedIndex >> 1);
403 return (MethodDefTable.Index << 24) + (codedIndex >> 1);
405 throw new BadImageFormatException();
409 internal int ReadImplementation()
412 if (bigImplementation)
414 codedIndex = br.ReadInt32();
418 codedIndex = br.ReadUInt16();
420 switch (codedIndex & 3)
423 return (FileTable.Index << 24) + (codedIndex >> 2);
425 return (AssemblyRefTable.Index << 24) + (codedIndex >> 2);
427 return (ExportedTypeTable.Index << 24) + (codedIndex >> 2);
429 throw new BadImageFormatException();
433 private int ReadToken(int table, bool big)
438 rid = br.ReadInt32();
442 rid = br.ReadUInt16();
444 return rid | (table << 24);
447 internal int ReadField()
451 return br.ReadInt32();
455 return br.ReadUInt16();
459 internal int ReadMethodDef()
463 return br.ReadInt32();
467 return br.ReadUInt16();
471 internal int ReadParam()
475 return br.ReadInt32();
479 return br.ReadUInt16();
483 internal int ReadProperty()
487 return br.ReadInt32();
491 return br.ReadUInt16();
495 internal int ReadEvent()
499 return br.ReadInt32();
503 return br.ReadUInt16();
507 internal int ReadTypeDef()
509 return ReadToken(TypeDefTable.Index, bigTypeDef);
512 internal int ReadGenericParam()
514 return ReadToken(GenericParamTable.Index, bigGenericParam);
517 internal int ReadModuleRef()
519 return ReadToken(ModuleRefTable.Index, bigModuleRef);