5 // Jb Evain (jbevain@gmail.com)
7 // Generated by /CodeGen/cecil-gen.rb do not edit
8 // Sat Feb 16 23:23:29 +0100 2008
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 namespace Mono
.Cecil
.Metadata
{
35 using System
.Collections
;
38 sealed class Utilities
{
44 public static int ReadCompressedInteger (byte [] data
, int pos
, out int start
)
48 if ((data
[pos
] & 0x80) == 0) {
51 } else if ((data
[pos
] & 0x40) == 0) {
52 integer
= (data
[start
] & ~
0x80) << 8;
53 integer
|= data
[pos
+ 1];
56 integer
= (data
[start
] & ~
0xc0) << 24;
57 integer
|= data
[pos
+ 1] << 16;
58 integer
|= data
[pos
+ 2] << 8;
59 integer
|= data
[pos
+ 3];
65 public static int ReadCompressedSignedInteger (byte [] data
, int pos
, out int start
)
67 int integer
= ReadCompressedInteger (data
, pos
, out start
) >> 1;
68 if ((integer
& 1) == 0)
72 return integer
- 0x40;
75 return integer
- 0x2000;
77 if (integer
< 0x10000000)
78 return integer
- 0x10000000;
80 return integer
- 0x20000000;
83 public static int WriteCompressedInteger (BinaryWriter writer
, int value)
86 writer
.Write ((byte) value);
87 else if (value < 0x4000) {
88 writer
.Write ((byte) (0x80 | (value >> 8)));
89 writer
.Write ((byte) (value & 0xff));
91 writer
.Write ((byte) ((value >> 24) | 0xc0));
92 writer
.Write ((byte) ((value >> 16) & 0xff));
93 writer
.Write ((byte) ((value >> 8) & 0xff));
94 writer
.Write ((byte) (value & 0xff));
96 return (int) writer
.BaseStream
.Position
;
99 public static MetadataToken
GetMetadataToken (CodedIndex cidx
, uint data
)
103 case CodedIndex
.TypeDefOrRef
:
107 return new MetadataToken (TokenType
.TypeDef
, rid
);
109 return new MetadataToken (TokenType
.TypeRef
, rid
);
111 return new MetadataToken (TokenType
.TypeSpec
, rid
);
113 return MetadataToken
.Zero
;
115 case CodedIndex
.HasConstant
:
119 return new MetadataToken (TokenType
.Field
, rid
);
121 return new MetadataToken (TokenType
.Param
, rid
);
123 return new MetadataToken (TokenType
.Property
, rid
);
125 return MetadataToken
.Zero
;
127 case CodedIndex
.HasCustomAttribute
:
131 return new MetadataToken (TokenType
.Method
, rid
);
133 return new MetadataToken (TokenType
.Field
, rid
);
135 return new MetadataToken (TokenType
.TypeRef
, rid
);
137 return new MetadataToken (TokenType
.TypeDef
, rid
);
139 return new MetadataToken (TokenType
.Param
, rid
);
141 return new MetadataToken (TokenType
.InterfaceImpl
, rid
);
143 return new MetadataToken (TokenType
.MemberRef
, rid
);
145 return new MetadataToken (TokenType
.Module
, rid
);
147 return new MetadataToken (TokenType
.Permission
, rid
);
149 return new MetadataToken (TokenType
.Property
, rid
);
151 return new MetadataToken (TokenType
.Event
, rid
);
153 return new MetadataToken (TokenType
.Signature
, rid
);
155 return new MetadataToken (TokenType
.ModuleRef
, rid
);
157 return new MetadataToken (TokenType
.TypeSpec
, rid
);
159 return new MetadataToken (TokenType
.Assembly
, rid
);
161 return new MetadataToken (TokenType
.AssemblyRef
, rid
);
163 return new MetadataToken (TokenType
.File
, rid
);
165 return new MetadataToken (TokenType
.ExportedType
, rid
);
167 return new MetadataToken (TokenType
.ManifestResource
, rid
);
169 return new MetadataToken (TokenType
.GenericParam
, rid
);
171 return MetadataToken
.Zero
;
173 case CodedIndex
.HasFieldMarshal
:
177 return new MetadataToken (TokenType
.Field
, rid
);
179 return new MetadataToken (TokenType
.Param
, rid
);
181 return MetadataToken
.Zero
;
183 case CodedIndex
.HasDeclSecurity
:
187 return new MetadataToken (TokenType
.TypeDef
, rid
);
189 return new MetadataToken (TokenType
.Method
, rid
);
191 return new MetadataToken (TokenType
.Assembly
, rid
);
193 return MetadataToken
.Zero
;
195 case CodedIndex
.MemberRefParent
:
199 return new MetadataToken (TokenType
.TypeDef
, rid
);
201 return new MetadataToken (TokenType
.TypeRef
, rid
);
203 return new MetadataToken (TokenType
.ModuleRef
, rid
);
205 return new MetadataToken (TokenType
.Method
, rid
);
207 return new MetadataToken (TokenType
.TypeSpec
, rid
);
209 return MetadataToken
.Zero
;
211 case CodedIndex
.HasSemantics
:
215 return new MetadataToken (TokenType
.Event
, rid
);
217 return new MetadataToken (TokenType
.Property
, rid
);
219 return MetadataToken
.Zero
;
221 case CodedIndex
.MethodDefOrRef
:
225 return new MetadataToken (TokenType
.Method
, rid
);
227 return new MetadataToken (TokenType
.MemberRef
, rid
);
229 return MetadataToken
.Zero
;
231 case CodedIndex
.MemberForwarded
:
235 return new MetadataToken (TokenType
.Field
, rid
);
237 return new MetadataToken (TokenType
.Method
, rid
);
239 return MetadataToken
.Zero
;
241 case CodedIndex
.Implementation
:
245 return new MetadataToken (TokenType
.File
, rid
);
247 return new MetadataToken (TokenType
.AssemblyRef
, rid
);
249 return new MetadataToken (TokenType
.ExportedType
, rid
);
251 return MetadataToken
.Zero
;
253 case CodedIndex
.CustomAttributeType
:
257 return new MetadataToken (TokenType
.Method
, rid
);
259 return new MetadataToken (TokenType
.MemberRef
, rid
);
261 return MetadataToken
.Zero
;
263 case CodedIndex
.ResolutionScope
:
267 return new MetadataToken (TokenType
.Module
, rid
);
269 return new MetadataToken (TokenType
.ModuleRef
, rid
);
271 return new MetadataToken (TokenType
.AssemblyRef
, rid
);
273 return new MetadataToken (TokenType
.TypeRef
, rid
);
275 return MetadataToken
.Zero
;
277 case CodedIndex
.TypeOrMethodDef
:
281 return new MetadataToken (TokenType
.TypeDef
, rid
);
283 return new MetadataToken (TokenType
.Method
, rid
);
285 return MetadataToken
.Zero
;
288 return MetadataToken
.Zero
;
292 public static uint CompressMetadataToken (CodedIndex cidx
, MetadataToken token
)
298 case CodedIndex
.TypeDefOrRef
:
299 ret
= token
.RID
<< 2;
300 switch (token
.TokenType
) {
301 case TokenType
.TypeDef
:
303 case TokenType
.TypeRef
:
305 case TokenType
.TypeSpec
:
308 throw new MetadataFormatException("Non valid Token for TypeDefOrRef");
310 case CodedIndex
.HasConstant
:
311 ret
= token
.RID
<< 2;
312 switch (token
.TokenType
) {
313 case TokenType
.Field
:
315 case TokenType
.Param
:
317 case TokenType
.Property
:
320 throw new MetadataFormatException("Non valid Token for HasConstant");
322 case CodedIndex
.HasCustomAttribute
:
323 ret
= token
.RID
<< 5;
324 switch (token
.TokenType
) {
325 case TokenType
.Method
:
327 case TokenType
.Field
:
329 case TokenType
.TypeRef
:
331 case TokenType
.TypeDef
:
333 case TokenType
.Param
:
335 case TokenType
.InterfaceImpl
:
337 case TokenType
.MemberRef
:
339 case TokenType
.Module
:
341 case TokenType
.Permission
:
343 case TokenType
.Property
:
345 case TokenType
.Event
:
347 case TokenType
.Signature
:
349 case TokenType
.ModuleRef
:
351 case TokenType
.TypeSpec
:
353 case TokenType
.Assembly
:
355 case TokenType
.AssemblyRef
:
357 case TokenType
.File
:
359 case TokenType
.ExportedType
:
361 case TokenType
.ManifestResource
:
363 case TokenType
.GenericParam
:
366 throw new MetadataFormatException("Non valid Token for HasCustomAttribute");
368 case CodedIndex
.HasFieldMarshal
:
369 ret
= token
.RID
<< 1;
370 switch (token
.TokenType
) {
371 case TokenType
.Field
:
373 case TokenType
.Param
:
376 throw new MetadataFormatException("Non valid Token for HasFieldMarshal");
378 case CodedIndex
.HasDeclSecurity
:
379 ret
= token
.RID
<< 2;
380 switch (token
.TokenType
) {
381 case TokenType
.TypeDef
:
383 case TokenType
.Method
:
385 case TokenType
.Assembly
:
388 throw new MetadataFormatException("Non valid Token for HasDeclSecurity");
390 case CodedIndex
.MemberRefParent
:
391 ret
= token
.RID
<< 3;
392 switch (token
.TokenType
) {
393 case TokenType
.TypeDef
:
395 case TokenType
.TypeRef
:
397 case TokenType
.ModuleRef
:
399 case TokenType
.Method
:
401 case TokenType
.TypeSpec
:
404 throw new MetadataFormatException("Non valid Token for MemberRefParent");
406 case CodedIndex
.HasSemantics
:
407 ret
= token
.RID
<< 1;
408 switch (token
.TokenType
) {
409 case TokenType
.Event
:
411 case TokenType
.Property
:
414 throw new MetadataFormatException("Non valid Token for HasSemantics");
416 case CodedIndex
.MethodDefOrRef
:
417 ret
= token
.RID
<< 1;
418 switch (token
.TokenType
) {
419 case TokenType
.Method
:
421 case TokenType
.MemberRef
:
424 throw new MetadataFormatException("Non valid Token for MethodDefOrRef");
426 case CodedIndex
.MemberForwarded
:
427 ret
= token
.RID
<< 1;
428 switch (token
.TokenType
) {
429 case TokenType
.Field
:
431 case TokenType
.Method
:
434 throw new MetadataFormatException("Non valid Token for MemberForwarded");
436 case CodedIndex
.Implementation
:
437 ret
= token
.RID
<< 2;
438 switch (token
.TokenType
) {
439 case TokenType
.File
:
441 case TokenType
.AssemblyRef
:
443 case TokenType
.ExportedType
:
446 throw new MetadataFormatException("Non valid Token for Implementation");
448 case CodedIndex
.CustomAttributeType
:
449 ret
= token
.RID
<< 3;
450 switch (token
.TokenType
) {
451 case TokenType
.Method
:
453 case TokenType
.MemberRef
:
456 throw new MetadataFormatException("Non valid Token for CustomAttributeType");
458 case CodedIndex
.ResolutionScope
:
459 ret
= token
.RID
<< 2;
460 switch (token
.TokenType
) {
461 case TokenType
.Module
:
463 case TokenType
.ModuleRef
:
465 case TokenType
.AssemblyRef
:
467 case TokenType
.TypeRef
:
470 throw new MetadataFormatException("Non valid Token for ResolutionScope");
472 case CodedIndex
.TypeOrMethodDef
:
473 ret
= token
.RID
<< 1;
474 switch (token
.TokenType
) {
475 case TokenType
.TypeDef
:
477 case TokenType
.Method
:
480 throw new MetadataFormatException("Non valid Token for TypeOrMethodDef");
483 throw new MetadataFormatException ("Non valid CodedIndex");
487 internal static Type
GetCorrespondingTable (TokenType t
)
490 case TokenType
.Assembly
:
491 return typeof (AssemblyTable
);
492 case TokenType
.AssemblyRef
:
493 return typeof (AssemblyRefTable
);
494 case TokenType
.CustomAttribute
:
495 return typeof (CustomAttributeTable
);
496 case TokenType
.Event
:
497 return typeof (EventTable
);
498 case TokenType
.ExportedType
:
499 return typeof (ExportedTypeTable
);
500 case TokenType
.Field
:
501 return typeof (FieldTable
);
502 case TokenType
.File
:
503 return typeof (FileTable
);
504 case TokenType
.InterfaceImpl
:
505 return typeof (InterfaceImplTable
);
506 case TokenType
.MemberRef
:
507 return typeof (MemberRefTable
);
508 case TokenType
.Method
:
509 return typeof (MethodTable
);
510 case TokenType
.Module
:
511 return typeof (ModuleTable
);
512 case TokenType
.ModuleRef
:
513 return typeof (ModuleRefTable
);
514 case TokenType
.Param
:
515 return typeof (ParamTable
);
516 case TokenType
.Permission
:
517 return typeof (DeclSecurityTable
);
518 case TokenType
.Property
:
519 return typeof (PropertyTable
);
520 case TokenType
.Signature
:
521 return typeof (StandAloneSigTable
);
522 case TokenType
.TypeDef
:
523 return typeof (TypeDefTable
);
524 case TokenType
.TypeRef
:
525 return typeof (TypeRefTable
);
526 case TokenType
.TypeSpec
:
527 return typeof (TypeSpecTable
);
533 internal delegate int TableRowCounter (int rid
);
535 internal static int GetCodedIndexSize (CodedIndex ci
, TableRowCounter rowCounter
, int [] codedIndexCache
)
537 int bits
= 0, max
= 0, index
= (int) ci
;
538 if (codedIndexCache
[index
] != 0)
539 return codedIndexCache
[index
];
544 case CodedIndex
.TypeDefOrRef
:
547 rids
[0] = TypeDefTable
.RId
;
548 rids
[1] = TypeRefTable
.RId
;
549 rids
[2] = TypeSpecTable
.RId
;
551 case CodedIndex
.HasConstant
:
554 rids
[0] = FieldTable
.RId
;
555 rids
[1] = ParamTable
.RId
;
556 rids
[2] = PropertyTable
.RId
;
558 case CodedIndex
.HasCustomAttribute
:
561 rids
[0] = MethodTable
.RId
;
562 rids
[1] = FieldTable
.RId
;
563 rids
[2] = TypeRefTable
.RId
;
564 rids
[3] = TypeDefTable
.RId
;
565 rids
[4] = ParamTable
.RId
;
566 rids
[5] = InterfaceImplTable
.RId
;
567 rids
[6] = MemberRefTable
.RId
;
568 rids
[7] = ModuleTable
.RId
;
569 rids
[8] = DeclSecurityTable
.RId
;
570 rids
[9] = PropertyTable
.RId
;
571 rids
[10] = EventTable
.RId
;
572 rids
[11] = StandAloneSigTable
.RId
;
573 rids
[12] = ModuleRefTable
.RId
;
574 rids
[13] = TypeSpecTable
.RId
;
575 rids
[14] = AssemblyTable
.RId
;
576 rids
[15] = AssemblyRefTable
.RId
;
577 rids
[16] = FileTable
.RId
;
578 rids
[17] = ExportedTypeTable
.RId
;
579 rids
[18] = ManifestResourceTable
.RId
;
580 rids
[19] = GenericParamTable
.RId
;
582 case CodedIndex
.HasFieldMarshal
:
585 rids
[0] = FieldTable
.RId
;
586 rids
[1] = ParamTable
.RId
;
588 case CodedIndex
.HasDeclSecurity
:
591 rids
[0] = TypeDefTable
.RId
;
592 rids
[1] = MethodTable
.RId
;
593 rids
[2] = AssemblyTable
.RId
;
595 case CodedIndex
.MemberRefParent
:
598 rids
[0] = TypeDefTable
.RId
;
599 rids
[1] = TypeRefTable
.RId
;
600 rids
[2] = ModuleRefTable
.RId
;
601 rids
[3] = MethodTable
.RId
;
602 rids
[4] = TypeSpecTable
.RId
;
604 case CodedIndex
.HasSemantics
:
607 rids
[0] = EventTable
.RId
;
608 rids
[1] = PropertyTable
.RId
;
610 case CodedIndex
.MethodDefOrRef
:
613 rids
[0] = MethodTable
.RId
;
614 rids
[1] = MemberRefTable
.RId
;
616 case CodedIndex
.MemberForwarded
:
619 rids
[0] = FieldTable
.RId
;
620 rids
[1] = MethodTable
.RId
;
622 case CodedIndex
.Implementation
:
625 rids
[0] = FileTable
.RId
;
626 rids
[1] = AssemblyRefTable
.RId
;
627 rids
[2] = ExportedTypeTable
.RId
;
629 case CodedIndex
.CustomAttributeType
:
632 rids
[0] = MethodTable
.RId
;
633 rids
[1] = MemberRefTable
.RId
;
635 case CodedIndex
.ResolutionScope
:
638 rids
[0] = ModuleTable
.RId
;
639 rids
[1] = ModuleRefTable
.RId
;
640 rids
[2] = AssemblyRefTable
.RId
;
641 rids
[3] = TypeRefTable
.RId
;
643 case CodedIndex
.TypeOrMethodDef
:
646 rids
[0] = TypeDefTable
.RId
;
647 rids
[1] = MethodTable
.RId
;
650 throw new MetadataFormatException ("Non valid CodedIndex");
653 for (int i
= 0; i
< rids
.Length
; i
++) {
654 int rows
= rowCounter (rids
[i
]);
655 if (rows
> max
) max
= rows
;
658 res
= max
< (1 << (16 - bits
)) ? 2 : 4;
659 codedIndexCache
[index
] = res
;