summaryrefslogtreecommitdiff
path: root/src/msil/ch/epfl/lamp/compiler/msil/util/Table.java
blob: 1f43b8c2fa7f270b5458e44cc5d97ece14bcb0bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
/*
 * System.Reflection-like API for acces to .NET Assemblies
 */


package ch.epfl.lamp.compiler.msil.util;

import ch.epfl.lamp.compiler.msil.PEFile;
import ch.epfl.lamp.compiler.msil.PEFile.Sig;

import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;

/**
 * Represents a table in a .NET assembly
 *
 * @author Nikolay Mihaylov
 * @version 1.0
 */
public abstract class Table {

    //##########################################################################

    public static final int MAX_NUMBER = 64;

    public static final long VALID_TABLES_MASK = 0x03ff3fb7ff57L;

    //##########################################################################
    // fields and methods for handling predefined sets of tables

    public static final int TABLE_SET_LENGTH = 13;

    public static final int _TypeDefOrRef = 0;
    public static final int _HasConstant = 1;
    public static final int _HasCustomAttribute = 2;
    public static final int _HasFieldMarshal = 3;
    public static final int _HasDeclSecurity = 4;
    public static final int _MemberRefParent = 5;
    public static final int _HasSemantics = 6;
    public static final int _MethodDefOrRef = 7;
    public static final int _MemberForwarded = 8;
    public static final int _Implementation = 9;
    public static final int _CustomAttributeType = 10;
    public static final int _ResolutionScope = 11;
    public static final int _TypeOrMethodDef = 12;


    public static final int[][] TableSet = new int[TABLE_SET_LENGTH][];

    static {
	TableSet[_TypeDefOrRef] =
	    new int[] {TypeDef.ID, TypeRef.ID, TypeSpec.ID};
	TableSet[_HasConstant] =
	    new int[] {FieldDef.ID, ParamDef.ID, PropertyDef.ID};
	TableSet[_HasCustomAttribute] =
	    new int[] {MethodDef.ID, FieldDef.ID, TypeRef.ID, TypeDef.ID,
		       ParamDef.ID, InterfaceImpl.ID, MemberRef.ID, ModuleDef.ID,
		       -1, PropertyDef.ID, EventDef.ID, -1, ModuleRef.ID,
		       TypeSpec.ID, AssemblyDef.ID, AssemblyRef.ID,
		       FileDef.ID, ExportedType.ID, ManifestResource.ID};
	TableSet[_HasFieldMarshal] =
	    new int[] {FieldDef.ID, ParamDef.ID};
	TableSet[_HasDeclSecurity] =
	    new int[] {TypeDef.ID, MethodDef.ID, AssemblyDef.ID};
	TableSet[_MemberRefParent] =
	    new int[] {-1, TypeRef.ID, ModuleRef.ID, MethodDef.ID, TypeSpec.ID};
	TableSet[_HasSemantics] =
	    new int[] {EventDef.ID, PropertyDef.ID};
	TableSet[_MethodDefOrRef] =
	    new int[] {MethodDef.ID, MemberRef.ID};
	TableSet[_MemberForwarded] =
	    new int[] {FieldDef.ID, MethodDef.ID};
	TableSet[_Implementation] =
	    new int[] {FileDef.ID, AssemblyRef.ID, ExportedType.ID};
	TableSet[_CustomAttributeType] =
	    new int[] {-1, -1, MethodDef.ID, MemberRef.ID, -1};
	TableSet[_ResolutionScope] =
	    new int[] {ModuleDef.ID, ModuleRef.ID, AssemblyRef.ID, TypeRef.ID};
        TableSet[_TypeOrMethodDef] =
                new int[]{TypeDef.ID, MethodDef.ID};
    }

    public static final int[] NoBits =
            new int[]{2, 2, 5, 1, 2, 3, 1, 1, 1, 2, 3, 2, 1};

    public static int getMask(int tableSetId) {
	return (1 << NoBits[tableSetId]) - 1;
    }

    public static int getTableId(int tableSet, int index) {
	return TableSet[tableSet][index & getMask(tableSet)];
    }

    public static int getTableIndex(int tableSet, int index) {
	return index >> NoBits[tableSet];
    }

    public static int encodeIndex(int index, int tableSetId, int tableId) {
	int[] tableSet = TableSet[tableSetId];
	for (int i = 0; i < tableSet.length; i++) {
	    if (tableSet[i] == tableId)
		return (index << NoBits[tableSetId]) | i;
	}
	throw new RuntimeException("Cannot find table #" + tableId +
				   " in table set #" + tableSetId);
    }

    //##########################################################################

    private static final String [] tableName = {
	"Module",              "TypeRef",          "TypeDef", "   FieldTrans",
	"Field",               "MethodTrans",      "Method",      "",
	"Param",               "InterfaceImpl",    "MemberRef",   "Constant",
	"CustomAttribute",     "FieldMarshal",     "DeclSecurity","ClassLayout",
	"FieldLayout",         "StandAloneSig",    "EventMap",    "",
	"Event",               "PropertyMap",      "",            "Property",
	"MethodSemantics",     "MethodImpl",       "ModuleRef",   "TypeSpec",
	"ImplMap",             "FieldRVA",         "",            "",
	"Assembly",            "AssemblyProcessor","AssemblyOS",  "AssemblyRef",
	"AssemblyRefProcessor","AssemblyRefOS",    "File",        "ExportedType",
            "ManifestResource", "NestedClass", "GenericParam", "MethodSpec",
            "GenericParamConstraint", "", "", "",
	"",                    "",                 "",            "",
	"",                    "",                 "",            "",//0x30-0x37
	"",                    "",                 "",            "",
	"",                    "",                 "",            "" //0x37-0x3f
    };

    /** Creates a table with the given id and number of rows.
     */
    public static Table newTable(PEFile file, int id, int rows) {
	Table table = null;
	switch(id) {
	case ModuleDef.ID:         table = new ModuleDef(file, rows); break;
 	case TypeRef.ID:           table = new TypeRef(file, rows); break;
 	case TypeDef.ID:           table = new TypeDef(file, rows); break;
	case FieldTrans.ID:        table = new FieldTrans(file, rows); break;
	case FieldDef.ID:          table = new FieldDef(file, rows); break;
	case MethodTrans.ID:       table = new MethodTrans(file, rows); break;
	case MethodDef.ID:         table = new MethodDef(file, rows); break;
	case ParamDef.ID:          table = new ParamDef(file, rows); break;
	case InterfaceImpl.ID:     table = new InterfaceImpl(file, rows); break;
	case MemberRef.ID:         table = new MemberRef(file, rows); break;
	case Constant.ID:          table = new Constant(file, rows); break;
	case CustomAttribute.ID:   table = new CustomAttribute(file, rows); break;
	case FieldMarshal.ID:      table = new FieldMarshal(file, rows); break;
	case DeclSecurity.ID:      table = new DeclSecurity(file, rows); break;
	case ClassLayout.ID:       table = new ClassLayout(file, rows); break;
	case FieldLayout.ID:       table = new FieldLayout(file, rows); break;
	case StandAloneSig.ID:     table = new StandAloneSig(file, rows); break;
	case EventMap.ID:          table = new EventMap(file, rows); break;
	case EventDef.ID:          table = new EventDef(file, rows); break;
	case PropertyMap.ID:       table = new PropertyMap(file, rows); break;
	case PropertyDef.ID:       table = new PropertyDef(file, rows); break;
	case MethodSemantics.ID:   table = new MethodSemantics(file, rows); break;
	case MethodImpl.ID:        table = new MethodImpl(file, rows); break;
	case ModuleRef.ID:         table = new ModuleRef(file, rows); break;
	case TypeSpec.ID:          table = new TypeSpec(file, rows); break;
	case ImplMap.ID:           table = new ImplMap(file, rows); break;
	case FieldRVA.ID:          table = new FieldRVA(file, rows); break;
	case AssemblyDef.ID:       table = new AssemblyDef(file, rows); break;
	case AssemblyProcessor.ID: table = new AssemblyProcessor(file, rows); break;
	case AssemblyOS.ID:        table = new AssemblyOS(file, rows); break;
	case AssemblyRef.ID:       table = new AssemblyRef(file, rows); break;
	case AssemblyRefProcessor.ID:
	    table = new AssemblyRefProcessor(file, rows); break;
	case AssemblyRefOS.ID:     table = new AssemblyRefOS(file, rows); break;
	case FileDef.ID:           table = new FileDef(file, rows); break;
	case ExportedType.ID:      table = new ExportedType(file, rows); break;
	case ManifestResource.ID:  table = new ManifestResource(file, rows); break;
	case NestedClass.ID:       table = new NestedClass(file, rows); break;
    case GenericParam.ID:
        table = new GenericParam(file, rows);
        break;
    case MethodSpec.ID:
        table = new MethodSpec(file, rows);
        break;
    case GenericParamConstraint.ID:
        table = new GenericParamConstraint(file, rows);
        break;
	default:
	    table = new Empty(id);
	}
// 	System.out.println("created table " + table.getName() + " with "
// 			   + table.rows + " rows");
	return table;
    }


    //##########################################################################
    // public fields

    /** Number of rows in the table. */
    public final int rows;

    /** Table ID as specified in Partition II. */
    public final int id;

    /** The file to which the table belongs. */
    protected final PEFile file;

    /** Memory mapped buffer wrapping the table. */
    protected ByteBuffer buffer;

    /**
     * specified wheter a new memory-mapped byte buffer should be created
     * for this table.
     */
    protected boolean newMapping = false;

    /** Tells wheter the table is indexed by 2-byte (short) integer
     *  or by 4-byte integer. */
    public final boolean isShort;

    private int rowSize = -1;

    // the starting position of the table relative to the beginning of the file
    private long start = -1;

    // the number of the row who can be accessed via the fields of the table
    private int currentRow = 0;

    //##########################################################################

    protected Table(PEFile file, int id, int rows) {
	this.file = file;
	this.id = id;
	this.rows = rows;//file.readInt();
	this.isShort = rows < (1 << 16);
// 	assert ((1L << id) & VALID_TABLES_MASK) != 0
// 	    : "Table does not have a vaid ID: " + byte2hex(id);
    }

    /**
     * Additional table initialization.
     * @return the starting position of the next table in the stream.
     */
    public final long init(long start) {
	if (rows < 1)
	    return start;
	if (this.start == -1)
	    this.start = start;
	else throw new RuntimeException
		 ("Cannot re-initialize table \'" + getTableName() + "\'");
	rowSize = getRowSize();
	int size = rows * rowSize();
	buffer = this.newMapping ? file.mapBuffer(start, size)
	    : file.getBuffer(start, size);
	return start + size;
    }


    public final String getTableName() {
	return 0 <= id && id < MAX_NUMBER ? tableName[id] : "<NoTable>";
    }

    /**
     * @return the size of the row in bytes
     */
    public final int rowSize() {
	return rowSize;
    }

    /**
     * if the underlying buffer is memory-mapped, load its contents into memory
     */
    public void load() {
	if (buffer instanceof MappedByteBuffer)
	    ((MappedByteBuffer)buffer).load();
    }

    /***/
    public final int readByte() {
	return (buffer.get() + 0x100) & 0xff;
    }

    /***/
    public final int readShort() {
	return (buffer.getShort() + 0x10000) & 0xffff;
    }

    /***/
    public final int readInt() {
	return buffer.getInt();
    }

    /***/
    public final int readStringIndex() {
	return file.StringIsShort ? readShort() : readInt();
    }

    /***/
    public final int readBlobIndex() {
	return file.BlobIsShort ? readShort() : readInt();
    }

    /***/
    public final int readGUIDIndex() {
	return file.GUIDIsShort ? readShort() : readInt();
    }

    /***/
    public final int readTableIndex(int tableId) {
	return file.getTable(tableId).isShort ? readShort() : readInt();
    }

    /***/
    public final int readTableSetIndex(int tableSetId) {
	return file.indexSize[tableSetId] == 2 ? readShort() : readInt();
    }

    /** Read the specified row and populate the fields of the instance. */
    public final void readRow(int row) {
	seekRow(row);
	int lastSeek = buffer.position();
	populateFields();
	int rowSizeRead = (int) (buffer.position() - lastSeek);
	if (rowSizeRead != rowSize())
	    throw new RuntimeException("Table ID=0x" + PEFile.byte2hex(id) +
				       ": read row size = " + rowSizeRead +
				       "; expected row size = " + rowSize());
	currentRow = row;
    }

    /** Seeks in the file the position of the specified row. */
    protected final void seekRow(int row) {
	assert row > 0 && row <= rows
	    : "Index " + row + " is not within the table with #rows = " + rows;
	buffer.position((row - 1)* rowSize());
    }

    public final int currentRow() { return currentRow; }

    public final void nextRow() { readRow(currentRow() + 1); }

    //##########################################################################
    // abstract members

    /** Assigns values to the fields of the class. */
    protected abstract void populateFields();

    /** Returns the size of a row in bytes. */
    protected abstract int getRowSize();

    //##########################################################################
    // a table with 0 rows

    private static final class Empty extends Table {
	public Empty(int id) {
	    super(null, id, 0);
	}
	protected int getRowSize() { return 0; }
	protected void populateFields() {
	    throw new RuntimeException("Table 0x" + PEFile.byte2hex(id));
	}
    }

    //##########################################################################
    // table Module; ID=0x00; p115, 21.27

    public static final class ModuleDef extends Table {
	public static final int ID = 0x00;

	/** 2-byte value; reserved - shall be 0. */
	public int Generation;

	/** Index into #String. */
	public int Name;

	/** Index into #GUID; used to distinguish between
	 *  two version of the same module. */
	public int Mvid;

	/** Index into #GUID; reserved - shall be 0. */
	public int EncId;

	/** Index into #GUID; reseved - shall be 0. */
	public int EncBaseId;

	public ModuleDef(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Generation = readShort();
	    Name = readStringIndex();
	    Mvid = readGUIDIndex();
	    EncId = readGUIDIndex();
	    EncBaseId = readGUIDIndex();
	}

	protected int getRowSize() {
	    return 2 + file.getStringIndexSize() + 3*file.getGUIDIndexSize();
	}

	public String getName() {
	    return file.getString(Name);
	}

    } // class ModuleDef

    //##########################################################################
    // table TypeRef; ID=0x01; p125, 21.35

    public static final class TypeRef extends Table {
	public static final int ID = 0x1;

	/** A ResolutionScope coded index. */
	public int ResolutionScope;

	/** Index into #String. */
	public int Name;

	/** Index into #String. */
	public int Namespace;

	public TypeRef(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    ResolutionScope = readTableSetIndex(_ResolutionScope);
	    Name = readStringIndex();
	    Namespace = readStringIndex();
	}

	protected int getRowSize() {
	    return file.getTableSetIndexSize(_ResolutionScope) +
		2 * file.getStringIndexSize();
	}

	public String getFullName() {
	    String namespace = file.getString(Namespace);
	    return namespace.length() == 0 ? file.getString(Name)
		: namespace + "." + file.getString(Name);
	}

    } // class TypeRef

    //##########################################################################
    // table TypeDef; ID=0x02; p120, 21.34

    public static final class TypeDef extends Table {
	public static final int ID = 0x02;

	/** 4-byte bitmask of type TypeAttributes (22.1.14). */
	public int Flags;

	/** Index into #String. */
	public int Name;

	/** Index into #String. */
	public int Namespace;

	/** TypeDefOrRef coded index. */
	public int Extends;

	/** Index into Field table.
	 */
	public int FieldList;

	/** Index into Method table. */
	public int MethodList;


	public TypeDef(PEFile file, int rows) {
	    super(file, ID, rows);
	    this.newMapping = true;
	}

	public String getFullName() {
	    String namespace = file.getString(Namespace);
	    return namespace.length() == 0 ? file.getString(Name)
		: namespace + "." + file.getString(Name);
	}

	protected void populateFields() {
	    Flags = readInt();
	    Name = readStringIndex();
	    Namespace = readStringIndex();
	    Extends = readTableSetIndex(_TypeDefOrRef);
	    FieldList = readTableIndex(FieldDef.ID);
	    MethodList = readTableIndex(MethodDef.ID);
	}

	protected int getRowSize() {
	    return 4 + 2*file.getStringIndexSize() +
		file.getTableSetIndexSize(_TypeDefOrRef) +
		file.getTableIndexSize(FieldDef.ID) +
		file.getTableIndexSize(MethodDef.ID);
	}

    } // class TypeDef

    //##########################################################################
    // Table FieldTrans; ID=0x03; undocumented

    /**
     * Undocumented table. Appears to be used for translating the Field entry
     * in the TypeDef(0x02) table into the real entry in the Fields(0x06) table
     */
    public static final class FieldTrans extends Table {
	public static final int ID = 0x03;

	public int Field;

	public FieldTrans(PEFile file, int rows) {
	    super(file, ID, rows);
	    newMapping = true;
	}

	protected void populateFields() {
	    Field = readTableIndex(FieldDef.ID);
	}

	protected int getRowSize() {
	    return file.getTableIndexSize(FieldDef.ID);
	}

    }

    //##########################################################################
    // table Field; ID=0x04; p102, 21.15

    public static final class FieldDef extends Table {
	public static final int ID = 0x04;

	/** 2-byte bitmask of type FieldAttributes (22.1.5). */
	public int Flags;

	/** Index into #String. */
	public int Name;

	/** Index into #Blob. */
	public int Signature;

	public FieldDef(PEFile file, int rows) {
	    super(file, ID, rows);
	    newMapping = true;
	}

	protected void populateFields() {
	    Flags = readShort();
	    Name = readStringIndex();
	    Signature = readBlobIndex();
	}

	protected int getRowSize() {
	    return 2 + file.getStringIndexSize() + file.getBlobIndexSize();
	}

	public String getName() { return file.getString(Name); }

	public Sig getSignature() { return file.getSignature(Signature); }

    } //class FieldDef

    //##########################################################################
    // Table MethodTrans; ID=0x05; undocumented

    /**
     * Undocumented table. Appears to be used for translating the Method entry
     * in the TypeDef(0x02) table into the real entry in the Methods(0x06) table
     */
    public static final class MethodTrans extends Table {
	public static final int ID = 0x05;

	public int Method;

	public MethodTrans(PEFile file, int rows) {
	    super(file, ID, rows);
	    newMapping = true;
	}

	protected void populateFields() {
	    Method = readTableIndex(FieldDef.ID);
	}

	protected int getRowSize() {
	    return file.getTableIndexSize(MethodDef.ID);
	}

    }

    //##########################################################################
    // table MethodDef; ID=0x06; p110, 21.24

    public static final class MethodDef extends Table {
	public static final int ID = 0x06;

	/** 4-byte constant. */
	public int RVA;

	/** 2-byte bitmask of type MethodImplAttributes (22.1.10). */
	public int ImplFlags;

	/** 2-byte bitmask of type MethodAttributes (22.1.9). */
	public int Flags;

	/** Index into #String. */
	public int Name;

	/** Index into #Blob. */
	public int Signature;

	/** Index into Param Table. */
	public int ParamList;

	public MethodDef(PEFile file, int rows) {
	    super(file, ID, rows);
	    newMapping = true;
	}

	protected void populateFields() {
	    RVA = readInt();
	    ImplFlags = readShort();
	    Flags = readShort();
	    Name = readStringIndex();
	    Signature = readBlobIndex();
	    ParamList = readTableIndex(ParamDef.ID);
	}

	protected int getRowSize() {
	    return 8 + file.getStringIndexSize() + file.getBlobIndexSize() +
		file.getTableIndexSize(ParamDef.ID);
	}

	public String getName() { return file.getString(Name); }

	public Sig getSignature() { return file.getSignature(Signature); }
    } // class Method

    //##########################################################################
    // table Param; ID=0x08; p116, 21.30

    public static final class ParamDef extends Table {
	public static final int ID = 0x08;

	/** 2-byte bitmask of type ParamAttributes (22.1.12). */
	public int Flags;

	/** 2-byte constant. */
	public int Sequence;

	/** Index into #String. */
	public int Name;

	public ParamDef(PEFile file, int rows) {
	    super(file, ID, rows);
	    newMapping = true;
	}

	protected void populateFields() {
	    Flags = readShort();
	    Sequence = readShort();
	    Name = readStringIndex();
	}

	protected int getRowSize() { return 4 + file.getStringIndexSize(); }

	public String getName() { return file.getString(Name); }

    } // class Param

    //##########################################################################
    // table InterfaceImpl, ID=0x09; p107, 21.21

    public static final class InterfaceImpl extends Table {
	public static final int ID = 0x09;

	/** Index into TypeDef table. */
	public int Class;

	/** Index into TypeDefOrRef table set. */
	public int Interface;

	public InterfaceImpl(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Class = readTableIndex(TypeDef.ID);
	    Interface = readTableSetIndex(_TypeDefOrRef);
	}

	protected int getRowSize() {
	    return file.getTableIndexSize(TypeDef.ID) +
		file.getTableSetIndexSize(_TypeDefOrRef);
	}

	/** finds the index of the first entry
	 * @param targetIndex - index in the TypeDef table - the type to look for
	 * @return the index of the first interface for the given type;
	 *         0 if the type doesn't implement any interfaces
	 */

	// binary search implementation
// 	public int findType(int targetIndex) {
// 	    int l = 1, h = rows;
// 	    int classIndex;
// 	    while (l <= h) {
// 		int mid = (l + h) / 2;
// 		seekRow(mid);
// 		classIndex = readTableIndex(TypeDef.ID);
// 		if (targetIndex <= classIndex) h = mid - 1;
// 		else l = mid + 1;
// 	    }
// 	    return (targetIndex == classIndex) ? h : 0;
// 	}

	//linear search implementation
	public int findType(int targetIndex) {
	    for (int i = 1; i <= rows; i++) {
		seekRow(i);
		if (targetIndex == readTableIndex(TypeDef.ID))
		    return i;
	    }
	    return 0;
	}

    } // class InterfaceImpl

    //##########################################################################
    // table MemberRef; ID=0x0a; p109, 21.23

    public static final class MemberRef extends Table {
	public static final int ID = 0x0a;

	/** Index into MemberRefParent table set. */
	public int Class;

	/** Index into #String. */
	public int Name;

	/** Index into #Blob. */
	public int Signature;

	public MemberRef(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Class = readTableSetIndex(_MemberRefParent);
	    Name = readStringIndex();
	    Signature = readBlobIndex();
	}

	protected int getRowSize() {
	    return file.getTableSetIndexSize(_MemberRefParent) +
		file.getStringIndexSize() + file.getBlobIndexSize();
	}

	public String getName() {
	    return file.getString(Name);
	}

	public Sig getSignature() {
	    return file.getSignature(Signature);
	}

    } // class MemberRef

    //##########################################################################
    // table Constant; ID=0x0b; p95, 21.9

    public static final class Constant extends Table {
	public static final int ID = 0x0b;

	/** 1-byte constant followed by 1-byte padding 0 (see 22.1.15). */
	public int Type;

	/** Index into HasConst table set. */
	public int Parent;

	/** Index into #Blob. */
	public int Value;

	public Constant(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Type = readShort();
	    Parent = readTableSetIndex(_HasConstant);
	    Value = readBlobIndex();
	}

	protected int getRowSize() {
	    return 2 + file.getTableSetIndexSize(_HasConstant) +
		file.getBlobIndexSize();
	}

	public Object getValue() {
	    if (Type == Signature.ELEMENT_TYPE_CLASS)
		return null;
	    return file.Blob.getConstant(Type, Value);
	}


    } // class Constant

    //##########################################################################
    // table CustomAttribute; ID=0x0c; p95, 21.10

    public static final class CustomAttribute extends Table {
	public static final int ID = 0x0c;

	/** Index into any metadata table, except the CustomAttribute itself;
	 *  more precisely - index into HasCustomAttribute table set.
	 */
	public int Parent;

	/** Index into the CustomAttributeType table set. */
	public int Type;

	/** Index into #Blob. */
	public int Value;

	public CustomAttribute(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Parent = readTableSetIndex(_HasCustomAttribute);
	    Type = readTableSetIndex(_CustomAttributeType);
	    Value = readBlobIndex();
	}

	protected int getRowSize() {
	    return file.getTableSetIndexSize(_HasCustomAttribute) +
		file.getTableSetIndexSize(_CustomAttributeType) +
		file.getBlobIndexSize();
	}

	public byte[] getValue() {
	    return Value == 0 ? null : file.getBlob(Value);
	}
    } // class CustomAttribute

    //##########################################################################
    // table FieldMarshal; ID=0x0d; p105, 21.17

    public static final class FieldMarshal extends Table {
	public static final int ID = 0x0d;

	/** Index into HasFieldMarshal table set. */
	public int Parent;

	/** Index into #Blob. */
	public int NativeType;

	public FieldMarshal(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Parent = readTableSetIndex(_HasFieldMarshal);
	    NativeType = readBlobIndex();
	}

	protected int getRowSize() {
	    return file.getTableSetIndexSize(_HasFieldMarshal) +
		file.getBlobIndexSize();
	}

    } // class FieldMarshal

    //##########################################################################
    // table DeclSecurity; ID=0x0e; p97, 21.11

    public static final class DeclSecurity extends Table {
	public static final int ID = 0x0e;

	/** 2-byte value. */
	public int Action;

	/** Index into HasDeclSecurity table set. */
	public int Parent;

	/** Index into #Blob. */
	public int PermissionSet;

	public DeclSecurity(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Action = readShort();
	    Parent = readTableSetIndex(_HasDeclSecurity);
	    PermissionSet = readBlobIndex();
	}

	protected int getRowSize() {
	    return 2 + file.getTableSetIndexSize(_HasDeclSecurity) +
		file.getBlobIndexSize();
	}

    } // class DeclSecurity

    //##########################################################################
    // table ClassLayout; ID=0x0f, p92, 21.8

    public static final class ClassLayout extends Table {
	public static final int ID = 0x0f;

	/** 2-byte constant. */
	public int PackingSize;

	/** 4-byte constant. */
	public int ClassSize;

	/** Index into TypeDef table. */
	public int Parent;

	public ClassLayout(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    PackingSize = readShort();
	    ClassSize = readInt();
	    Parent = readTableIndex(TypeDef.ID);
	}

	protected int getRowSize() {
	    return 6 + file.getTableIndexSize(TypeDef.ID);
	}

    } // class ClassLayout

    //##########################################################################
    // table FieldLayout; ID=0x10; p104, 21.16

    public static final class FieldLayout extends Table {
	public static final int ID = 0x10;

	/** 4-byte constant. */
	public int Offset;

	/** Index into the Field table. */
	public int Field;

	public FieldLayout(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Offset = readInt();
	    Field = readTableIndex(FieldDef.ID);
	}

	protected int getRowSize() {
	    return 4 + file.getTableIndexSize(FieldDef.ID);
	}

    } // class FieldLayout

    //##########################################################################
    // table StandAloneSig; ID=0x11; p119, 21.33

    public static final class StandAloneSig extends Table {
	public static final int ID = 0x11;

	/** Index into #Blob. */
	public int Signature;

	public StandAloneSig(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Signature = readBlobIndex();
	}

	protected int getRowSize() { return file.getBlobIndexSize(); }

    } // class StandAloneSig

    //##########################################################################
    // table EventMap; ID=0x12; p99, 21.12

    public static final class EventMap extends Table {
	public static final int ID = 0x12;

	/** Index into the TypeDef table. */
	public int Parent;

	/** Index into the Event table. */
	public int EventList;

	public EventMap(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Parent = readTableIndex(TypeDef.ID);
	    EventList = readTableIndex(EventDef.ID);
	}

	protected int getRowSize() {
	    return file.getTableIndexSize(TypeDef.ID) +
		file.getTableIndexSize(EventDef.ID);
	}

    } // class EventMap

    //##########################################################################
    // table Event; ID=0x14; p99, 21.13

    public static final class EventDef extends Table {
	public static final int ID = 0x14;

	/** 2-byte bitmask of type EventAttribute (22.1.4). */
	public int EventFlags;

	/** Index into #String. */
	public int Name;

	/** Index into TypeDefOrRef table set. [This corresponds to the Type
	 *  of the event; it is not the Type that owns the event]
	 */
	public int EventType;

	public EventDef(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    EventFlags = readShort();
	    Name = readStringIndex();
	    EventType = readTableSetIndex(_TypeDefOrRef);
	}

	protected int getRowSize() {
	    return 2 + file.getStringIndexSize() +
		file.getTableSetIndexSize(_TypeDefOrRef);
	}

	public String getName() { return file.getString(Name); }

    } // class EventDef

    //##########################################################################
    // table PropertyMap; ID=0x15; p119, 21.32

    public static final class PropertyMap extends Table {
	public static final int ID = 0x15;

	/** Index into the TypeDef table. */
	public int Parent;

	/** Index into the Property table. */
	public int PropertyList;

	public PropertyMap(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Parent = readTableIndex(TypeDef.ID);
	    PropertyList = readTableIndex(PropertyDef.ID);
	}

	protected int getRowSize() {
	    return file.getTableIndexSize(TypeDef.ID) +
		file.getTableIndexSize(PropertyDef.ID);
	}

    } // class PropertyMap

    //##########################################################################
    // table Property; ID=0x17; p117, 21.31

    public static final class PropertyDef extends Table {
	public static final int ID = 0x17;

	/** 2-byte bitmask of type PropertyAttributes (22.1.13). */
	public int Flags;

	/** Index into #String. */
	public int Name;

	/** Index into #Blob. (Indexes the signature in the #Blob) */
	public int Type;

	public PropertyDef(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Flags = readShort();
	    Name = readStringIndex();
	    Type = readBlobIndex();
	}

	protected int getRowSize() {
	    return 2 + file.getStringIndexSize() +
		file.getBlobIndexSize();
	}

	public String getName() { return file.getString(Name); }

	public Sig getSignature() { return file.getSignature(Type); }

    } // class PropertyDef

    //##########################################################################
    // table MethodSemantics; ID=0x18; p114, 21.26

    public static final class MethodSemantics extends Table {
	public static final int ID = 0x18;

	/** 2-byte bitmaks of type MethodSemanticsAttribute (22.1.11). */
	public int Semantics;

	/** Index into the Method table. */
	public int Method;

	/** Index into Event or Property table (HasSemantics table set). */
	public int Association;

	public MethodSemantics(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Semantics = readShort();
	    Method = readTableIndex(MethodDef.ID);
	    Association = readTableSetIndex(_HasSemantics);
	}

	protected int getRowSize() {
	    return 2 + file.getTableIndexSize(MethodDef.ID) +
		file.getTableSetIndexSize(_HasSemantics);
	}

        public boolean isGetter()   { return (Semantics & Getter)   != 0; }
        public boolean isSetter()   { return (Semantics & Setter)   != 0; }
        public boolean isOther()    { return (Semantics & Other)    != 0; }
        public boolean isAddOn()    { return (Semantics & AddOn)    != 0; }
        public boolean isRemoveOn() { return (Semantics & RemoveOn) != 0; }
        public boolean isFire()     { return (Semantics & Fire)     != 0; }

        private static final short Setter   = (short)0x0001;
        private static final short Getter   = (short)0x0002;
        private static final short Other    = (short)0x0004;
        private static final short AddOn    = (short)0x0008;
        private static final short RemoveOn = (short)0x0010;
        private static final short Fire     = (short)0x0020;

    } // class MethodSemantics


    //##########################################################################
    // table MethodImpl; ID=0x19; p113, 21.25

    public static final class MethodImpl extends Table {
	public static final int ID = 0x19;

	/** Index into the TypeDef table. */
	public int Class;

	/** Index into MethodDefOrRef table set. */
	public int MethodBody;

	/** Index into MethodDefOrRef table set. */
	public int MethodDeclaration;

	public MethodImpl(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Class = readTableIndex(TypeDef.ID);
	    MethodBody = readTableSetIndex(_MethodDefOrRef);
	    MethodDeclaration = readTableSetIndex(_MethodDefOrRef);
	}

	protected int getRowSize() {
	    return file.getTableIndexSize(TypeDef.ID) +
		2 * file.getTableSetIndexSize(_MethodDefOrRef);
	}

    } // class MethodImpl

    //##########################################################################
    // table ModuleRef; ID=0x1a; p116, 21.28

    public static final class ModuleRef extends Table {
	public static final int ID = 0x1a;

	/** Index into #String. */
	public int Name;

	public ModuleRef(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Name = readStringIndex();
	}

	protected int getRowSize() { return file.getStringIndexSize(); }

	public String getName() { return file.getString(Name); }

    } // class ModuleRef

    //##########################################################################
    // table TypeSpec; ID=0x1b; p126, 21.36

    public static final class TypeSpec extends Table {
	public static final int ID = 0x1b;

	/** Index into #Blob, where the blob is formatted
	 *  as specified in 22.2.15
	 */
	public int Signature;

	public TypeSpec(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Signature = readBlobIndex();
	}

	protected int getRowSize() { return file.getBlobIndexSize(); }

	public Sig getSignature() { return file.getSignature(Signature); }
    } // class TypeSpec

    //##########################################################################
    // table ImplMap; ID=0x1c; p107, 21.20

    public static final class ImplMap extends Table {
	public static final int ID = 0x1c;

	/** 2-byte bitmask of type PInvokeAttributes (22.1.7). */
	public int MappingFlags;

	/** Index into MemberForwarded table set. */
	public int MemberForwarded;

	/** Index into #String. */
	public int ImportName;

	/** Index into the ModuleRef table. */
	public int ImportScope;

	public ImplMap(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    MappingFlags = readShort();
	    MemberForwarded = readTableSetIndex(_MemberForwarded);
	    ImportName = readStringIndex();
	    ImportScope = readTableIndex(ModuleRef.ID);
	}

	protected int getRowSize() {
	    return 2 + file.getTableSetIndexSize(_MemberForwarded) +
		file.getStringIndexSize() +
		file.getTableIndexSize(ModuleRef.ID);
	}

    } // class ImplMap

    //##########################################################################
    // table FieldRVA; ID=0x1d; p106, 21.18

    public static final class FieldRVA extends Table {
	public static final int ID = 0x1d;

	/** 4-byte constant. */
	public int RVA;

	/** Index into the Field table. */
	public int Field;

	public FieldRVA(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    RVA = readInt();
	    Field = readTableIndex(Table.FieldDef.ID);
	}

	protected int getRowSize() {
	    return 4 + file.getTableIndexSize(FieldDef.ID);
	}

    }

    //##########################################################################
    // table Assembly; ID=0x20; p90, 21.2

    public static final class AssemblyDef extends Table {
	public static final int ID = 0x20;

	/** 4-byte constatnt of type AssemblyHashAlgorithm, clause 22.1.1 */
	public int HashAlgId;

	/** 2-byte constant */
	public int MajorVersion;

	/** 2-byte constant */
	public int MinorVersion;

	/** 2-byte constant */
	public int BuildNumber;

	/** 2-byte constant */
	public int RevisionNumber;

	/** 4-byte constant */
	public int Flags;

	/** index into #Blob */
	public int PublicKey;

	/** index into #String */
	public int Name;

	/** index into #String */
	public int Culture;

	public AssemblyDef(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    HashAlgId = readInt();
	    MajorVersion = readShort();
	    MinorVersion = readShort();
	    BuildNumber = readShort();
	    RevisionNumber = readShort();
	    Flags = readInt();
	    PublicKey = readBlobIndex();
	    Name = readStringIndex();
	    Culture = readStringIndex();
	}

	protected int getRowSize() {
	    return 16 + file.getBlobIndexSize() + 2*file.getStringIndexSize();
	}

    } // class AssemblyDef

    //##########################################################################
    // table AssemblyProcessor; ID=0x21; p91, 21.4

    public static final class AssemblyProcessor extends Table {
	public static final int ID = 0x21;

	/** 4-byte constant. */
	public int Processor;

	public AssemblyProcessor(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Processor = readInt();
	}

	protected int getRowSize() { return 4; }

    }

    //##########################################################################
    // table AssemblyOS; ID = 0x22; p90, 21.3

    public static final class AssemblyOS extends Table {
	public static final int ID = 0x22;

	/** 4-byte constant. */
	public int OSPlatformID;

	/** 4-byte constant. */
	public int OSMajorVersion;

	/** 4-byte constant. */
	public int OSMinorVersion;

	public AssemblyOS(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    OSPlatformID = readInt();
	    OSMajorVersion = readInt();
	    OSMinorVersion = readInt();
	}

	protected int getRowSize() { return 12; }

    }

    //##########################################################################
    // table AssemblyRef; ID = 0x23; pp91, 21.5

    public static final class AssemblyRef extends Table {
	public static final int ID = 0x23;

	/** 2-byte constant. */
	public int MajorVersion;

	/** 2-byte constant. */
	public int MinorVersion;

	/** 2-byte constant. */
	public int BuildNumber;

	/** 2-byte constant. */
	public int RevisionNumber;

	/** 4-byte bitmask of type AssemblyFlags (22.1.2). */
	public int Flags;

	/** index into #Blob. */
	public int PublicKeyOrToken;

	/** index into #String. */
	public int Name;

	/** index into #String. */
	public int Culture;

	/** index into #Blob. */
	public int HashValue;

	public AssemblyRef(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    MajorVersion = readShort();
	    MinorVersion = readShort();
	    BuildNumber = readShort();
	    RevisionNumber = readShort();
	    Flags = readInt();
	    PublicKeyOrToken = readBlobIndex();
	    Name = readStringIndex();
	    Culture = readStringIndex();
	    HashValue = readBlobIndex();
	}

	protected int getRowSize() {
	    return 12 + 2*file.getBlobIndexSize() + 2*file.getStringIndexSize();
	}

	public String getName() { return file.getString(Name); }
    }

    //##########################################################################
    // table AssemblyRefProcessor; ID=0x24; p92, 21.7

    public static final class AssemblyRefProcessor extends Table {
	public static final int ID = 0x24;

	/** 4-byte constant. */
	public int Processor;

	/** Index into the AssemblyRef table. */
	public int AssemblyRef;

	public AssemblyRefProcessor(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Processor = readInt();
	    AssemblyRef = readTableIndex(Table.AssemblyRef.ID);
	}

	protected int getRowSize() {
	    return 4 + file.getTableIndexSize(Table.AssemblyRef.ID);
	}

    } // class AssemblyRefProcessor

    //##########################################################################
    // table AssemblyRefOS; ID=0x25; p92, 21.6

    public static final class AssemblyRefOS extends Table {
	public static final int ID = 0x25;

	/** 4-byte constant. */
	public int OSPlatformId;

	/** 4-byte constant. */
	public int OSMajorVersion;

	/** 4-byte constant. */
	public int OSMinorVersion;

	/** Index into the AssemblyRef table. */
	public int AssemblyRef;

	public AssemblyRefOS(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    OSPlatformId = readInt();
	    OSMajorVersion = readInt();
	    OSMinorVersion = readInt();
	    AssemblyRef = readTableIndex(Table.AssemblyRef.ID);
	}

	protected int getRowSize() {
	    return 12 + file.getTableIndexSize(Table.AssemblyRef.ID);
	}

    } // class AssemblyRefOS

    //##########################################################################
    // table File; ID=0x26; p106, 21.19

    public static final class FileDef extends Table {
	public static final int ID = 0x26;

	/** 4-byte bitmask of type FileAttributes (22.1.6). */
	public int Flags;

	/** Index into #String. */
	public int Name;

	/** Index into #Blob. */
	public int HashValue;

	public FileDef(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Flags = readInt();
	    Name = readStringIndex();
	    HashValue = readBlobIndex();
	}

	protected int getRowSize() {
	    return 4 + file.getStringIndexSize() + file.getBlobIndexSize();
	}

	public String getName() {
	    return file.getString(Name);
	}

    } // class FileDef

    //##########################################################################
    // table ExportedType; ID=0x27; p100, 21.14

    public static final class ExportedType extends Table {
	public static final int ID = 0x27;

	/** 4-byte bitmask of type TypeAttribute (22.1.6). */
	public int Flags;

	/** 4-byte index into a TypeDef table of
	 *  another module in this assembly.
	 */
	public int TypeDefId;

	/** Index into #String. */
	public int TypeName;

	/** Index into #Stream. */
	public int TypeNamespace;

	/** Index into one of two tables as follows:
	 *   - 'File' table, where that entry says which module
	 *     in the current assembly holds the TypeDef
	 *   - 'ExportedType' table, where that entry is
	 *     the enclosing Type of the current nested Type
	 */
	public int Implementation;

	public ExportedType(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Flags = readInt();
	    TypeDefId = readInt();
	    TypeName = readStringIndex();
	    TypeNamespace = readStringIndex();
	    Implementation = readTableSetIndex(_Implementation);
	}

	protected int getRowSize() {
	    return 8 + 2*file.getStringIndexSize() +
		file.getTableSetIndexSize(_Implementation);
	}

	public String getFullName() {
	    String namespace = file.getString(TypeNamespace);
	    return namespace.length() == 0 ? file.getString(TypeName)
		: namespace + "." + file.getString(TypeName);
	}

    } // class ExportedType

    //##########################################################################
    // table ManifestResource; ID=0x28; p108, 21.22

    public static final class ManifestResource extends Table {
	public static final int ID = 0x28;

	/** 4-byte constant. */
	public int Offset;

	/** 4-byte bitmask of type ManifestResourceAttributes (22.1.8). */
	public int Flags;

	/** Index into #String. */
	public int Name;

	/** Index into the Implementation table set. */
	public int Implementation;

	public ManifestResource(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    Offset = readInt();
	    Flags = readInt();
	    Name = readStringIndex();
	    Implementation = readTableSetIndex(_Implementation);
	}

	protected int getRowSize() {
	    return  8 + file.getStringIndexSize() +
		file.getTableSetIndexSize(_Implementation);
	}

    } // class ManifestResource

    //##########################################################################
    // table NestedClass; ID=0x29; p116, 21.29

    public static final class NestedClass extends Table {
	public static final int ID = 0x29;

	/** Index into the TypeDef table. */
	public int NestedClass;

	/** Index into the TypeDef table. */
	public int EnclosingClass;

	public NestedClass(PEFile file, int rows) { super(file, ID, rows); }

	protected void populateFields() {
	    NestedClass = readTableIndex(TypeDef.ID);
	    EnclosingClass = readTableIndex(TypeDef.ID);
	}

	protected int getRowSize() {
	    return 2 * file.getTableIndexSize(TypeDef.ID);
	}

    } // class NestedClass

    //##########################################################################
    // table GenericParam; ID=0x2a; p137, 22.20

    public static final class GenericParam extends Table {
        public static final int ID = 0x2a;

        public int Number;
        public int Flags;
        public int Owner;  // a TypeOrMethodDef (Sec 24.2.6) coded index
        public int Name; // a non-null index into the String heap

        private java.util.Map /*<Integer, java.util.Set<Integer>>*/ GenericParamIdxesForMethodDefIdx =
                new java.util.HashMap();
        private java.util.Map /*<Integer, java.util.Set<Integer>>*/ GenericParamIdxesForTypeDefIdx  =
                new java.util.HashMap();

        private void addToMap(int key, int value, java.util.Map IdxesForIdx) {
            java.util.Set /*<Integer>*/ bucket = (java.util.Set)IdxesForIdx.get(Integer.valueOf(key));
            if(bucket == null) {
                bucket = new java.util.HashSet();
                IdxesForIdx.put(Integer.valueOf(key), bucket);
            }
            bucket.add(Integer.valueOf(value));
        }

        /** Indexes of rows in the GenericParam table representing type parameters defined by the type given by
         * its row index TypeDefIdx (in the TypeDef table).
         * No need to position the current record before invoking this method.  */
        public int[] getTVarIdxes(int TypeDefIdx) {
            if(!mapsPopulated) {
                initMaps();
            }
            java.util.Set bucket = (java.util.Set)GenericParamIdxesForTypeDefIdx.get(Integer.valueOf(TypeDefIdx));
            if(bucket == null) {
                bucket = java.util.Collections.EMPTY_SET;
            }
            int[] res = new int[bucket.size()];
            java.util.Iterator /*<Integer>*/ it = bucket.iterator();
            for(int i = 0; i < bucket.size(); i++) {
                res[i] = ((Integer)it.next()).intValue();
            }
            return res;
        }

        /** Indexes of rows in the GenericParam table representing type parameters defined by the method given by
         * its row index MethodDefIdx (in the MethodDef table)
         * No need to position the current record before invoking this method.  */
        public int[] getMVarIdxes(int MethodDefIdx) {
            if(!mapsPopulated) {
                initMaps();
            }
            java.util.Set bucket = (java.util.Set)GenericParamIdxesForMethodDefIdx.get(Integer.valueOf(MethodDefIdx));
            if(bucket == null) {
                bucket = java.util.Collections.EMPTY_SET;
            }
            int[] res = new int[bucket.size()];
            java.util.Iterator /*<Integer>*/ it = bucket.iterator();
            for(int i = 0; i < bucket.size(); i++) {
                res[i] = ((Integer)it.next()).intValue();
            }
            return res;
        }

        private boolean mapsPopulated = false;

        private void initMaps() {
            mapsPopulated = true;
            for (int currentParamRow = 1; currentParamRow <= rows; currentParamRow++) {
                int currentOwner = file.GenericParam(currentParamRow).Owner;
                int targetTableId = Table.getTableId(Table._TypeOrMethodDef, currentOwner);
                int targetRow = currentOwner >> Table.NoBits[Table._TypeOrMethodDef];
                if(targetTableId == TypeDef.ID){
                    addToMap(targetRow, currentParamRow, GenericParamIdxesForTypeDefIdx);
                } else if(targetTableId == MethodDef.ID) {
                    addToMap(targetRow, currentParamRow, GenericParamIdxesForMethodDefIdx);
                } else {
                    throw new RuntimeException();
                }
            }
        }

        public GenericParam(PEFile file, int rows) {
            super(file, ID, rows);
            this.newMapping = true;
        }

        protected void populateFields() {
            Number = readShort();
            Flags = readShort();
            Owner = readTableSetIndex(_TypeOrMethodDef);
            Name = readStringIndex();
        }

        /** This method assumes populateFields() has been just called to set Flags for the current record */
        public boolean isInvariant() {
            /* 23.1.7 Flags for Generic Parameters [GenericParamAttributes tributes] */
            return (Flags & 0x0003) == 0;
        }

        /** This method assumes populateFields() has been just called to set Flags for the current record */
        public boolean isCovariant() {
            /* 23.1.7 Flags for Generic Parameters [GenericParamAttributes tributes] */
            return (Flags & 0x0003) == 1;
        }

        /** This method assumes populateFields() has been just called to set Flags for the current record */
        public boolean isContravariant() {
            /* 23.1.7 Flags for Generic Parameters [GenericParamAttributes tributes] */
            return (Flags & 0x0003) == 2;
        }

        /** This method assumes populateFields() has been just called to set Flags for the current record */
        public boolean isReferenceType() {
            /* 23.1.7 Flags for Generic Parameters [GenericParamAttributes tributes] */
            return (Flags & 0x001C) == 4;
        }

        /** This method assumes populateFields() has been just called to set Flags for the current record */
        public boolean isValueType() {
            /* 23.1.7 Flags for Generic Parameters [GenericParamAttributes tributes] */
            return (Flags & 0x001C) == 8;
        }

        /** This method assumes populateFields() has been just called to set Flags for the current record */
        public boolean hasDefaultConstructor() {
            /* 23.1.7 Flags for Generic Parameters [GenericParamAttributes tributes] */
            return (Flags & 0x001C) == 0x0010;
        }

        protected int getRowSize() {
            return 2 + 2 + file.getTableSetIndexSize(_TypeOrMethodDef) + file.getStringIndexSize();
            /* Columns:
                 Number (2 bytes),
                 Flags (2 bytes),
                 Owner (coded token of type TypeOrMethodDef),
                 Name (offset in the #Strings stream).
            */
        }

        public String getName() {
            return file.getString(Name);
        }

    } // class GenericParam


    //##########################################################################
    // table GenericParamConstraint; ID=0x2c; p139, 22.20

    public static final class GenericParamConstraint extends Table {
        public static final int ID = 0x2c;

        public int Owner; // an index into the GenericParam table
        public int Constraint; // a TypeDefOrRef (Sec 24.2.6) coded index

        public GenericParamConstraint(PEFile file, int rows) {
            super(file, ID, rows);
            this.newMapping = true;
        }

        protected void populateFields() {
            Owner = readTableIndex(GenericParam.ID);
            Constraint = readTableSetIndex(_TypeDefOrRef);
        }

        protected int getRowSize() {
            return file.getTableIndexSize(GenericParam.ID) + file.getTableSetIndexSize(_TypeDefOrRef);
            /* Columns:
                 Owner (RID in the GenericParam table),
                 Constraint (coded token of type TypeDefOrRef).
            */
        }

        private boolean mapPopulated = false;

        /** Indexes of rows (in the TypeDef, TypeRef, or TypeSpec tables) denoting the base class (if any)
         * and interfaces (if any) that the generic parameter (of TVar or MVar kind) should support,  where
         * that generic parameter is represented by its index into the GenericParam table. */
        public int[] getTypeDefOrRefIdxes(int genParamIdx) {
            if(!mapPopulated) {
                initMap();
            }
            java.util.Set bucket = (java.util.Set)TypeDefOrRefIdxesForGenParamIdx.get(Integer.valueOf(genParamIdx));
            if(bucket == null) {
                bucket = java.util.Collections.EMPTY_SET;
            }
            int[] res = new int[bucket.size()];
            java.util.Iterator /*<Integer>*/ it = bucket.iterator();
            for(int i = 0; i < bucket.size(); i++) {
                res[i] = ((Integer)it.next()).intValue();
            }
            return res;
        }


        private void initMap() {
            mapPopulated = true;
            for (int currentConstraintRow = 1; currentConstraintRow <= rows; currentConstraintRow++) {
                int targetGenericParam = file.GenericParamConstraint(currentConstraintRow).Owner;
                int value = file.GenericParamConstraint.Constraint;
                addToMap(targetGenericParam, value);
            }
        }

        private java.util.Map /*<Integer, java.util.Set<Integer>>*/ TypeDefOrRefIdxesForGenParamIdx  =
                new java.util.HashMap();

        private void addToMap(int key, int value) {
            java.util.Set /*<Integer>*/ bucket = (java.util.Set)TypeDefOrRefIdxesForGenParamIdx.get(Integer.valueOf(key));
            if(bucket == null) {
                bucket = new java.util.HashSet();
                TypeDefOrRefIdxesForGenParamIdx.put(Integer.valueOf(key), bucket);
            }
            bucket.add(Integer.valueOf(value));
        }

    } // class GenericParamConstraint

    //##########################################################################
    // table MethodSpec; ID=0x2b; p149, in Sec. 22.29 of Partition II

    public static final class MethodSpec extends Table {
        public static final int ID = 0x2b;

        /* an index into the MethodDef or MemberRef table, specifying which generic method this row is an instantiation of.
           A MethodDefOrRef (Sec. 24.2.6) coded index  */
        public int Method;

        /* an index into the Blob heap (Sec. 23.2.15), holding the signature of this instantiation */
        public int Instantiation;

        public MethodSpec(PEFile file, int rows) {
            super(file, ID, rows);
            this.newMapping = true;
        }

        protected void populateFields() {
            Method = readTableSetIndex(_MethodDefOrRef);
            Instantiation = readBlobIndex();
        }

        protected int getRowSize() {
            return file.getTableSetIndexSize(_MethodDefOrRef) + file.getBlobIndexSize();
        }


    } // class MethodSpec
    //##########################################################################

}  // class Table