IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
MetadataReader.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2009-2011 Jeroen Frijters
3
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.
7
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:
11
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.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.IO;
26
28
30{
31
33 {
34
35 const int bufferLength = 2048;
36
37 readonly Stream stream;
38 readonly byte[] buffer = new byte[bufferLength];
39
40 int pos = bufferLength;
41
48 internal MetadataReader(ModuleReader module, Stream stream, byte heapSizes) :
49 base(module, (heapSizes & 0x01) != 0, (heapSizes & 0x02) != 0, (heapSizes & 0x04) != 0)
50 {
51 this.stream = stream;
52 }
53
54 void FillBuffer(int needed)
55 {
56 int count = bufferLength - pos;
57 if (count != 0)
58 {
59 // move remaining bytes to the front of the buffer
60 Buffer.BlockCopy(buffer, pos, buffer, 0, count);
61 }
62
63 pos = 0;
64
65 while (count < needed)
66 {
67 var len = stream.Read(buffer, count, bufferLength - count);
68 if (len == 0)
69 throw new BadImageFormatException();
70
71 count += len;
72 }
73
74 if (count != bufferLength)
75 {
76 // we didn't fill the buffer completely, so have to restore the invariant
77 // that all data from pos up until the end of the buffer is valid
78 Buffer.BlockCopy(buffer, 0, buffer, bufferLength - count, count);
79 pos = bufferLength - count;
80 }
81 }
82
83 internal ushort ReadUInt16()
84 {
85 return (ushort)ReadInt16();
86 }
87
88 internal short ReadInt16()
89 {
90 if (pos > bufferLength - 2)
91 FillBuffer(2);
92
93 var b1 = buffer[pos++];
94 var b2 = buffer[pos++];
95 return (short)(b1 | (b2 << 8));
96 }
97
98 internal int ReadInt32()
99 {
100 if (pos > bufferLength - 4)
101 FillBuffer(4);
102
103 var b1 = buffer[pos++];
104 var b2 = buffer[pos++];
105 var b3 = buffer[pos++];
106 var b4 = buffer[pos++];
107 return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
108 }
109
110 int ReadIndex(bool big)
111 {
112 return big ? ReadInt32() : ReadUInt16();
113 }
114
115 internal int ReadStringIndex()
116 {
117 return ReadIndex(bigStrings);
118 }
119
120 internal int ReadGuidIndex()
121 {
122 return ReadIndex(bigGuids);
123 }
124
125 internal int ReadBlobIndex()
126 {
127 return ReadIndex(bigBlobs);
128 }
129
130 internal int ReadResolutionScope()
131 {
132 var codedIndex = ReadIndex(bigResolutionScope);
133 return (codedIndex & 3) switch
134 {
135 0 => (ModuleTable.Index << 24) + (codedIndex >> 2),
136 1 => (ModuleRefTable.Index << 24) + (codedIndex >> 2),
137 2 => (AssemblyRefTable.Index << 24) + (codedIndex >> 2),
138 3 => (TypeRefTable.Index << 24) + (codedIndex >> 2),
139 _ => throw new BadImageFormatException(),
140 };
141 }
142
143 internal int ReadTypeDefOrRef()
144 {
145 var codedIndex = ReadIndex(bigTypeDefOrRef);
146 return (codedIndex & 3) switch
147 {
148 0 => (TypeDefTable.Index << 24) + (codedIndex >> 2),
149 1 => (TypeRefTable.Index << 24) + (codedIndex >> 2),
150 2 => (TypeSpecTable.Index << 24) + (codedIndex >> 2),
151 _ => throw new BadImageFormatException(),
152 };
153 }
154
155 internal int ReadMemberRefParent()
156 {
157 var codedIndex = ReadIndex(bigMemberRefParent);
158 return (codedIndex & 7) switch
159 {
160 0 => (TypeDefTable.Index << 24) + (codedIndex >> 3),
161 1 => (TypeRefTable.Index << 24) + (codedIndex >> 3),
162 2 => (ModuleRefTable.Index << 24) + (codedIndex >> 3),
163 3 => (MethodDefTable.Index << 24) + (codedIndex >> 3),
164 4 => (TypeSpecTable.Index << 24) + (codedIndex >> 3),
165 _ => throw new BadImageFormatException(),
166 };
167 }
168
169 internal int ReadHasCustomAttribute()
170 {
171 var codedIndex = ReadIndex(bigHasCustomAttribute);
172 return (codedIndex & 31) switch
173 {
174 0 => (MethodDefTable.Index << 24) + (codedIndex >> 5),
175 1 => (FieldTable.Index << 24) + (codedIndex >> 5),
176 2 => (TypeRefTable.Index << 24) + (codedIndex >> 5),
177 3 => (TypeDefTable.Index << 24) + (codedIndex >> 5),
178 4 => (ParamTable.Index << 24) + (codedIndex >> 5),
179 5 => (InterfaceImplTable.Index << 24) + (codedIndex >> 5),
180 6 => (MemberRefTable.Index << 24) + (codedIndex >> 5),
181 7 => (ModuleTable.Index << 24) + (codedIndex >> 5),
182 8 => (DeclSecurityTable.Index << 24) + (codedIndex >> 5),
183 9 => (PropertyTable.Index << 24) + (codedIndex >> 5),
184 10 => (EventTable.Index << 24) + (codedIndex >> 5),
185 11 => (StandAloneSigTable.Index << 24) + (codedIndex >> 5),
186 12 => (ModuleRefTable.Index << 24) + (codedIndex >> 5),
187 13 => (TypeSpecTable.Index << 24) + (codedIndex >> 5),
188 14 => (AssemblyTable.Index << 24) + (codedIndex >> 5),
189 15 => (AssemblyRefTable.Index << 24) + (codedIndex >> 5),
190 16 => (FileTable.Index << 24) + (codedIndex >> 5),
191 17 => (ExportedTypeTable.Index << 24) + (codedIndex >> 5),
192 18 => (ManifestResourceTable.Index << 24) + (codedIndex >> 5),
193 19 => (GenericParamTable.Index << 24) + (codedIndex >> 5),
194 20 => (GenericParamConstraintTable.Index << 24) + (codedIndex >> 5),
195 21 => (MethodSpecTable.Index << 24) + (codedIndex >> 5),
196 _ => throw new BadImageFormatException(),
197 };
198 }
199
200 internal int ReadCustomAttributeType()
201 {
202 var codedIndex = ReadIndex(bigCustomAttributeType);
203 return (codedIndex & 7) switch
204 {
205 2 => (MethodDefTable.Index << 24) + (codedIndex >> 3),
206 3 => (MemberRefTable.Index << 24) + (codedIndex >> 3),
207 _ => throw new BadImageFormatException(),
208 };
209 }
210
211 internal int ReadMethodDefOrRef()
212 {
213 var codedIndex = ReadIndex(bigMethodDefOrRef);
214 return (codedIndex & 1) switch
215 {
216 0 => (MethodDefTable.Index << 24) + (codedIndex >> 1),
217 1 => (MemberRefTable.Index << 24) + (codedIndex >> 1),
218 _ => throw new BadImageFormatException(),
219 };
220 }
221
222 internal int ReadHasConstant()
223 {
224 var codedIndex = ReadIndex(bigHasConstant);
225 return (codedIndex & 3) switch
226 {
227 0 => (FieldTable.Index << 24) + (codedIndex >> 2),
228 1 => (ParamTable.Index << 24) + (codedIndex >> 2),
229 2 => (PropertyTable.Index << 24) + (codedIndex >> 2),
230 _ => throw new BadImageFormatException(),
231 };
232 }
233
234 internal int ReadHasSemantics()
235 {
236 var codedIndex = ReadIndex(bigHasSemantics);
237 return (codedIndex & 1) switch
238 {
239 0 => (EventTable.Index << 24) + (codedIndex >> 1),
240 1 => (PropertyTable.Index << 24) + (codedIndex >> 1),
241 _ => throw new BadImageFormatException(),
242 };
243 }
244
245 internal int ReadHasFieldMarshal()
246 {
247 var codedIndex = ReadIndex(bigHasFieldMarshal);
248 return (codedIndex & 1) switch
249 {
250 0 => (FieldTable.Index << 24) + (codedIndex >> 1),
251 1 => (ParamTable.Index << 24) + (codedIndex >> 1),
252 _ => throw new BadImageFormatException(),
253 };
254 }
255
256 internal int ReadHasDeclSecurity()
257 {
258 var codedIndex = ReadIndex(bigHasDeclSecurity);
259 return (codedIndex & 3) switch
260 {
261 0 => (TypeDefTable.Index << 24) + (codedIndex >> 2),
262 1 => (MethodDefTable.Index << 24) + (codedIndex >> 2),
263 2 => (AssemblyTable.Index << 24) + (codedIndex >> 2),
264 _ => throw new BadImageFormatException(),
265 };
266 }
267
268 internal int ReadTypeOrMethodDef()
269 {
270 var codedIndex = ReadIndex(bigTypeOrMethodDef);
271 return (codedIndex & 1) switch
272 {
273 0 => (TypeDefTable.Index << 24) + (codedIndex >> 1),
274 1 => (MethodDefTable.Index << 24) + (codedIndex >> 1),
275 _ => throw new BadImageFormatException(),
276 };
277 }
278
279 internal int ReadMemberForwarded()
280 {
281 var codedIndex = ReadIndex(bigMemberForwarded);
282 return (codedIndex & 1) switch
283 {
284 0 => (FieldTable.Index << 24) + (codedIndex >> 1),
285 1 => (MethodDefTable.Index << 24) + (codedIndex >> 1),
286 _ => throw new BadImageFormatException(),
287 };
288 }
289
290 internal int ReadImplementation()
291 {
292 var codedIndex = ReadIndex(bigImplementation);
293 return (codedIndex & 3) switch
294 {
295 0 => (FileTable.Index << 24) + (codedIndex >> 2),
296 1 => (AssemblyRefTable.Index << 24) + (codedIndex >> 2),
297 2 => (ExportedTypeTable.Index << 24) + (codedIndex >> 2),
298 _ => throw new BadImageFormatException(),
299 };
300 }
301
302 internal int ReadField()
303 {
304 return ReadIndex(bigField);
305 }
306
307 internal int ReadMethodDef()
308 {
309 return ReadIndex(bigMethodDef);
310 }
311
312 internal int ReadParam()
313 {
314 return ReadIndex(bigParam);
315 }
316
317 internal int ReadProperty()
318 {
319 return ReadIndex(bigProperty);
320 }
321
322 internal int ReadEvent()
323 {
324 return ReadIndex(bigEvent);
325 }
326
327 internal int ReadTypeDef()
328 {
329 return ReadIndex(bigTypeDef) | (TypeDefTable.Index << 24);
330 }
331
332 internal int ReadGenericParam()
333 {
334 return ReadIndex(bigGenericParam) | (GenericParamTable.Index << 24);
335 }
336
337 internal int ReadModuleRef()
338 {
339 return ReadIndex(bigModuleRef) | (ModuleRefTable.Index << 24);
340 }
341
342 }
343
344}
Base class for MetadataReader and MetadataWriter.
Definition MetadataRW.cs:31