summaryrefslogtreecommitdiff
path: root/src/msil/ch/epfl/lamp/compiler/msil/PEFile.java
blob: 459bb39a200c973666d2b45d470d9a971dfc5fc1 (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
/*
 * System.Reflection-like API for access to .NET assemblies (DLL & EXE)
 */

// $Id$

package ch.epfl.lamp.compiler.msil;

import ch.epfl.lamp.compiler.msil.util.*;
import ch.epfl.lamp.compiler.msil.util.Table.*;

import ch.epfl.lamp.compiler.msil.Type;
import ch.epfl.lamp.compiler.msil.Module;

import java.io.File;
import java.io.RandomAccessFile;
import java.io.PrintStream;
import java.io.IOException;
import java.io.FileNotFoundException;

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.MappedByteBuffer;

import java.util.Date;

/**
 * A class that represents a .NET PE/COFF image.
 *
 * @author Nikolay Mihaylov
 * @version 1.0
 * @see <a href="http://www.ecma-international.org/publications/standards/Ecma-335.htm">Standard ECMA-335:  Common Language Infrastructure (CLI), 4th edition (June 2006)</a>
 */
public class PEFile {

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

    public static final int INT_SIZE = 4;

    protected final int PE_SIGNATURE_OFFSET;
    protected final int COFF_HEADER_OFFSET;
    protected final int PE_HEADER_OFFSET;

    protected final int numOfSections;
    protected final int CLI_RVA;
    protected final int CLI_Length;
    public final int rvaMetadata;
    public final int posMetadata;
    protected final int numOfStreams;
    protected final int optHeaderSize;

    protected final File underlyingFile;
    protected final RandomAccessFile file;
    protected final MappedByteBuffer buf;

    protected final PESection [] sections;

    public PEStream Meta, Strings, US, Blob, GUID;

    private final Table [] tables = new Table[Table.MAX_NUMBER];

    public final boolean isDLL;

    protected final int heapSizes;
    public final boolean StringIsShort, BlobIsShort, GUIDIsShort;

    protected PEModule pemodule = null;

    //##########################################################################
    // PEFile constructor

    private static void fileFormatCheck(boolean cond, String s) {
	if (cond)
	    throw new RuntimeException(s);
    }

    /**
     */
    public PEFile(String filename) throws FileNotFoundException {
	this.underlyingFile = new File(filename);
	this.file = new RandomAccessFile(underlyingFile, "r");
	FileChannel fc = file.getChannel();
	MappedByteBuffer bb = null;
	try {
	    bb = fc.map(FileChannel.MapMode.READ_ONLY, 0L, fc.size());
 	} catch (IOException e) { throw new RuntimeException(e); }

    /** Ecma 335, 25 File format extensions to PE:
     *
     *  "Unless stated otherwise, all binary values are stored in little-endian format."
     */

	bb.order(java.nio.ByteOrder.LITTLE_ENDIAN);
	this.buf = bb;

    /** Ecma 335, 25.2.1 MS-DOS header:
     *
     *  "The PE format starts with an MS-DOS stub of exactly the following 128 bytes to
     *  be placed at the front of the module."
     *
     *  We are only checking for MZ (Mark Zbikowski)
     */

	seek(0);
	fileFormatCheck(readByte() != 0x4d, "Invalid PE file format: " + filename); // 'M'
	fileFormatCheck(readByte() != 0x5a, "Invalid PE file format: " + filename); // 'Z'

    /** Ecma 335, 25.2.1 MS-DOS header:
     *
     *  "At offset 0x3c in the DOS header is a 4-byte unsigned integer offset, lfanew,
     *  to the PE signature (shall be “PE\0\0”), immediately followed by the PE file header.
     */

	seek(0x3c);
	PE_SIGNATURE_OFFSET = readInt();
	seek(PE_SIGNATURE_OFFSET);

	fileFormatCheck(readByte() != 0x50, "Invalid PE file format: " + filename); // 'P'
	fileFormatCheck(readByte() != 0x45, "Invalid PE file format: " + filename); // 'E'
    fileFormatCheck(readByte() != 0x00, "Invalid PE file format: " + filename); //  0
    fileFormatCheck(readByte() != 0x00, "Invalid PE file format: " + filename); //  0

	//trace("PE signature offset = 0x" + Table.int2hex(PE_SIGNATURE_OFFSET));

	COFF_HEADER_OFFSET = PE_SIGNATURE_OFFSET + 4;
	PE_HEADER_OFFSET = COFF_HEADER_OFFSET + 20;

	seek(COFF_HEADER_OFFSET);
	skip(2);
    /** Ecma 335, 25.2.2: "Number of sections; indicates size of the Section Table" */
	numOfSections = readShort();
	//trace("Number of sections = " + numOfSections);

    /** Ecma 335, 25.2.2: "Time and date the file was created in seconds since
     *  January 1st 1970 00:00:00 or 0."
     */
	Date timeStamp = new Date(readInt() * 1000L);
	//trace("Time stamp = " + timeStamp);

	skip(2 * INT_SIZE);
	optHeaderSize = readShort();
	int characteristics = readShort();
	isDLL = (characteristics & 0x2000) != 0;
	//trace("Characteristics = " + Integer.toHexString(characteristics));

	seek(PE_HEADER_OFFSET + 208); // p.157, Partition II

 	CLI_RVA = readInt();
	CLI_Length = readInt();
	//trace("CLI_RVA = 0x" + Table.int2hex(CLI_RVA));
	//trace("CLI_Length = 0x" + Table.int2hex(CLI_Length));

	sections = new PESection[numOfSections];

	seek(PE_HEADER_OFFSET + optHeaderSize); // go to the sections descriptors

	for (int i = 0; i < numOfSections; i++) {
	    seek(PE_HEADER_OFFSET + optHeaderSize + i * 40);
	    sections[i] = new PESection(this);
	    //sections[i].dump(System.out);
	}

	seek(fromRVA(CLI_RVA));
	skip(8);
	rvaMetadata = readInt();
	posMetadata = fromRVA(rvaMetadata);
	//trace("rvaMetadata = 0x" + Table.int2hex(rvaMetadata));
	//trace("posMetadata = 0x" + Table.int2hex(posMetadata));

	seek(posMetadata);
	int magic = readInt();
	//trace("Magic metadata signature = 0x" + Table.int2hex(magic));
	fileFormatCheck(magic != 0x424a5342, "Invalid metadata signature!");
	skip(8);

	int strlength = readInt();
	//trace("version name string length = " + strlength);
	skip(strlength);
	align(INT_SIZE, posMetadata);
	//trace("position of flags = 0x" + Table.int2hex((int)pos()));
	skip(2); // ignore the flags
	numOfStreams = readShort();
	//trace("Number of metadata streams = " + numOfStreams);

	for (int i = 0; i < numOfStreams; i++) {
	    PEStream strm = new PEStream(this);
	    //strm.dump(System.out);
	    if (strm.name.equals("#~")
		|| strm.name.equals("#-"))    Meta = strm;
	    if (strm.name.equals("#Strings")) Strings = strm;
	    if (strm.name.equals("#US"))      US = strm;
	    if (strm.name.equals("#Blob"))    Blob = strm;
	    if (strm.name.equals("#GUID"))    GUID = strm;
	}

	seek(Meta.offset);
	skip(6);
	heapSizes = readByte();
	StringIsShort = (heapSizes & 0x01) == 0;
	GUIDIsShort   = (heapSizes & 0x02) == 0;
	BlobIsShort   = (heapSizes & 0x04) == 0;

	skip(1);
	long tablesMask = readLong();
	long nonStandardTables = tablesMask & ~Table.VALID_TABLES_MASK;
	skip(8); //go to the list of number of rows
	for (int i = 0; i < tables.length; i++) {
	    tables[i] = Table.newTable
		(this, i, ((tablesMask >> i) & 0x01) != 0 ? readInt() : 0);
	}

	initIndexSize();
	initTableRefs();
	// populate the tables from the CLI image file
	long start = pos();
	for (int i = 0; i < tables.length; i++)
	    start = tables[i].init(start);

    } // PEFile()


    public final int[] indexSize = new int[Table.TABLE_SET_LENGTH];

    private void initIndexSize() {
	for (int i = 0; i < Table.TABLE_SET_LENGTH; i++) {
	    indexSize[i] = 2;
	    int[] tableSet = Table.TableSet[i];
	    int treshold = (65536 >> Table.NoBits[i]);
	    for (int j = 0; j < tableSet.length; j++) {
		if (tableSet[j] >= 0) {
		    Table t = tables[tableSet[j]];
		    if (t.rows >= treshold) {
			indexSize[i] = 4;
			break;
		    }
		}
	    }
	}
    }

    protected void initModule(PEModule module) {
	if (pemodule != null)
	    throw new RuntimeException("File " + this
				       + " has already been assigned module "
				       + pemodule + "; new module is " + module);
	this.pemodule = module;
    }

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

    public ModuleDef ModuleDef;
    public ModuleDef ModuleDef(int i) {
	ModuleDef.readRow(i);
	return ModuleDef;
    }

    public TypeRef TypeRef;

    public TypeDef TypeDef;
    public TypeDef TypeDef(int i) {
	TypeDef.readRow(i);
	return TypeDef;
    }

    public FieldTrans FieldTrans;
    public FieldTrans FieldTrans(int i) {
	FieldTrans.readRow(i);
	return FieldTrans;
    }

    public FieldDef FieldDef;
    public FieldDef FieldDef(int i) {
	FieldDef.readRow(i);
	return FieldDef;
    }

    public MethodTrans MethodTrans;
    public MethodTrans MethodTrans(int i) {
	MethodTrans.readRow(i);
	return MethodTrans;
    }

    public MethodDef MethodDef;
    public MethodDef MethodDef(int i) { MethodDef.readRow(i); return MethodDef; }


    public ParamDef ParamDef;
    public ParamDef ParamDef(int i) { ParamDef.readRow(i); return ParamDef; }

    public InterfaceImpl InterfaceImpl;
    public MemberRef MemberRef;
    public Constant Constant;
    public CustomAttribute CustomAttribute;
    public FieldMarshal FieldMarshal;
    public DeclSecurity DeclSecurity;
    public ClassLayout ClassLayout;
    public FieldLayout FieldLayout;
    public StandAloneSig StandAloneSig;
    public EventMap EventMap;
    public EventDef EventDef;
    public PropertyMap PropertyMap;
    public PropertyDef PropertyDef;
    public MethodSemantics MethodSemantics;
    public MethodImpl MethodImpl;
    public ModuleRef ModuleRef;
    public TypeSpec TypeSpec;
    public ImplMap ImplMap;
    public FieldRVA FieldRVA;
    public AssemblyDef AssemblyDef;
    public AssemblyRef AssemblyRef;
    public FileDef FileDef;
    public ExportedType ExportedType;
    public ManifestResource ManifestResource;
    public NestedClass NestedClass;


    private void initTableRefs() {
	ModuleDef = (ModuleDef) getTable(Table.ModuleDef.ID);
	TypeRef = (TypeRef) getTable(Table.TypeRef.ID);
	TypeDef = (TypeDef) getTable(Table.TypeDef.ID);
	FieldTrans = (FieldTrans) getTable(Table.FieldTrans.ID);
	FieldDef = (FieldDef) getTable(Table.FieldDef.ID);
	MethodTrans = (MethodTrans) getTable(Table.MethodTrans.ID);
	MethodDef = (MethodDef) getTable(Table.MethodDef.ID);
	ParamDef = (ParamDef) getTable(Table.ParamDef.ID);
	InterfaceImpl = (InterfaceImpl) getTable(Table.InterfaceImpl.ID);
	MemberRef = (MemberRef) getTable(Table.MemberRef.ID);
	Constant = (Constant) getTable(Table.Constant.ID);
	CustomAttribute = (CustomAttribute) getTable(Table.CustomAttribute.ID);
	FieldMarshal = (FieldMarshal) getTable(Table.FieldMarshal.ID);
	DeclSecurity = (DeclSecurity) getTable(Table.DeclSecurity.ID);
	ClassLayout = (ClassLayout) getTable(Table.ClassLayout.ID);
	FieldLayout = (FieldLayout) getTable(Table.FieldLayout.ID);
	StandAloneSig = (StandAloneSig) getTable(Table.StandAloneSig.ID);
	EventMap = (EventMap) getTable(Table.EventMap.ID);
	EventDef = (EventDef) getTable(Table.EventDef.ID);
	PropertyMap = (PropertyMap) getTable(Table.PropertyMap.ID);
	PropertyDef = (PropertyDef) getTable(Table.PropertyDef.ID);
	MethodSemantics = (MethodSemantics) getTable(Table.MethodSemantics.ID);
	MethodImpl = (MethodImpl) getTable(Table.MethodImpl.ID);
	ModuleRef = (ModuleRef) getTable(Table.ModuleRef.ID);
	TypeSpec = (TypeSpec) getTable(Table.TypeSpec.ID);
	ImplMap = (ImplMap) getTable(Table.ImplMap.ID);
	FieldRVA = (FieldRVA) getTable(Table.FieldRVA.ID);
	AssemblyDef = (AssemblyDef) getTable(Table.AssemblyDef.ID);
	AssemblyRef = (AssemblyRef) getTable(Table.AssemblyRef.ID);
	FileDef = (FileDef) getTable(Table.FileDef.ID);
	ExportedType = (ExportedType) getTable(Table.ExportedType.ID);
	NestedClass = (NestedClass) getTable(Table.NestedClass.ID);
	ManifestResource =
	    (ManifestResource) getTable(Table.ManifestResource.ID);
    }

    public static String long2hex(long a) {
	StringBuffer str = new StringBuffer("0000000000000000");
	str.append(Long.toHexString(a));
	int l = str.length();
	return str.substring(l - 16, l);
    }

    public static String int2hex(int a) {
	StringBuffer str = new StringBuffer("00000000");
	str.append(Integer.toHexString(a));
	int l = str.length();
	return str.substring(l - 8, l);
    }

    public static String short2hex(int a) {
	StringBuffer str = new StringBuffer("0000");
	str.append(Integer.toHexString(a));
	int l = str.length();
	return str.substring(l - 4, l);
    }

    public static String byte2hex(int a) {
	StringBuffer str = new StringBuffer("00");
	str.append(Integer.toHexString(a));
	int l = str.length();
	return str.substring(l - 2, l);
    }

    public static String bytes2hex(byte[] buf) {
	StringBuffer str = new StringBuffer();
	for (int i = 0; i < buf.length; i++) {
	    str.append(byte2hex(buf[i]));
	    if (i < buf.length - 1)
		str.append(" ");
	}
	return str.toString();
    }

    //##########################################################################
    // filename

    public File getUnderlyingFile() {
	return underlyingFile;
    }

    /**
     * @return the absolute path of the file
     */
    public String getAbsolutePath() {
	return underlyingFile.getAbsolutePath();
    }

    /**
     * @return the name of this file
     */
    public String getName() {
	return underlyingFile.getName();
    }

    /**
     * @return
     */
    public String getParent() {
	return underlyingFile.getParent();
    }

    /**
     * @return the file representing the directory the file belongs to
     */
    public File getParentFile() {
	return underlyingFile.getParentFile();
    }

    public String toString() {
	return getAbsolutePath();
    }

    //##########################################################################
    // file pointer manipulation methods

    /** Returns the current position in the file. */
    public int pos() {
	return buf.position();
    }

    /** Go to the specified position in the file. */
    public void seek(int pos) {
	buf.position(pos);
    }


    /** Align the current position in the file. */
    public void align(int base) { align(base, 0); }

    /** Align the current position in a section starting at offset. */
    public void align(int base, int offset) {
	int p = pos() - offset;
	seek( offset + ((p % base) == 0 ? p : (p/base + 1) * base));
    }

    /** Computes the position in the file that corresponds to the given RVA. */
    public int fromRVA(int rva) {
	int i;
	for(i = 0; i < numOfSections; i++)
	    if(sections[i].virtAddr <= rva &&
	       rva <= (sections[i].virtAddr + sections[i].virtSize))
		return rva - sections[i].virtAddr + sections[i].realAddr;
	throw new RuntimeException("RVA 0x" + Integer.toHexString(rva) +
				   " is not within this file's sections!");
    }

    /** Go to the specified RVA (Relative Virtual Address). */
    public void gotoRVA(int rva) {
	seek(fromRVA(rva));
    }

    /** Move the forward in the file by the specified number of bytes. */
    public void skip(int n) {
	buf.position(buf.position() + n);
    }

    /**
     * Returns a memory mapped little-endian buffer
     * for the specified region of the file.
     */
    public MappedByteBuffer mapBuffer(long offset, int size) {
 	try {
	    MappedByteBuffer b = file.getChannel()
		.map(FileChannel.MapMode.READ_ONLY, offset, size);
	    b.order(java.nio.ByteOrder.LITTLE_ENDIAN);
	    return b;
 	} catch (IOException e) { throw new RuntimeException(e); }
    }

    /** Returns a buffer from the given offset to the end of the file. */
    public ByteBuffer getBuffer(long offset, int size) {
	buf.mark();
	buf.position((int)offset);
	ByteBuffer bb = buf.slice();
	buf.reset();
	bb.limit(size);
	bb.order(java.nio.ByteOrder.LITTLE_ENDIAN);
	return bb;
    }

    //##########################################################################
    // file read methods

    /**
     * Read bs.length number of bytes
     */
    public void read(byte[] bs) {
	buf.get(bs);
    }

    /**
     * Read 1-byte integer from the current position in the file.
     */
    public int readByte() {
	return buf.get();
    }

    /**
     * Read 2-byte integer from the current position in the file.
     */
    public int readShort() {
	return buf.getShort();
    }

    /**
     * Read 4-byte integer from the current position in the file.
     */
    public int readInt() {
	return buf.getInt();
    }

    /**
     * Read 8-byte integer from the current position in the file.
     */
    public long readLong() {
	return buf.getLong();
    }

    /**
     * @return the size of string indeces for this file.
     */
    public int getStringIndexSize() {
	return StringIsShort ? 2 : 4;
    }

    /**
     * @return the size of GUID indeces for this file.
     */
    public int getGUIDIndexSize() {
	return GUIDIsShort ? 2 : 4;
    }

    /**
     * @return the size of Blob indeces for this file.
     */
    public int getBlobIndexSize() {
	return BlobIsShort ? 2 : 4;
    }

    /**
     * @return the size of the index to tableID for this file;
     * @param tableID the ID of the table
     */
    public int getTableIndexSize(int tableID) {
	return tables[tableID].isShort ? 2 : 4;
    }

    /**
     * @return the size of the index to a set of tables with the given @param TableSetID
     * @param tableSetID the ID of the table set
     */
    public int getTableSetIndexSize(int tableSetID) {
	return indexSize[tableSetID];
    }

    /**
     * Read a String index from the current position in the file.
     * @return an index into the String stream
     */
    public int readStringIndex() {
	return StringIsShort ? readShort() : readInt();
    }

    /**
     * Read a GUID index from the current position in the file.
     * @return an index in to the GUID stream
     */
    public int readGUIDIndex() {
	return GUIDIsShort ? readShort() : readInt();
    }

    /**
     * Read a Blob index from the current position in the file.
     * @return an index into the Blob stream
     */
    public int readBlobIndex() {
	return BlobIsShort ? readShort() : readInt();
    }

    /** Read an entry interpreted as index into table @param tableID. */
    public int readTableIndex(int tableId) {
	return tables[tableId].isShort ? readShort() : readInt();
    }

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

    /**
     * Read a string from the String stream
     * @return the string at the given position
     * @param pos the position of the string in the String stream
     */
    public String getString(int pos) {
	String s = Strings.getString(pos);
	return s;//.length() == 0 ? null : s;
    }

    /**
     * Read a string from the US (User Strings) stream
     * @return the string at the given position
     * @param pos the position of the string in the US stream
     */
    public String getUString(int pos) {
	return US.getString(pos);
    }

    /**
     * Read a blob from the Blob Stream
     * @return the blob at the given position
     * @param pos the position of the blob in the Blob stream
     */
    public byte[] getBlob(int pos) {
	return Blob.getBlob(pos);
    }

    /***/
    public Sig getSignature(int pos) {
	//return new Sig(getBlob(pos));
	return Blob.getSignature(pos);
    }

    /***/
    public byte[] getGUID(int pos) {
	return GUID.getGUID(pos);
    }

    /**
     * @return the table with the corresponding ID.
     */
    public final Table getTable(int tableID) {
	return tables[tableID];
    }

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

    /***/
    void trace(String msg) {
	System.out.println("[trace] " + msg);
    }

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

    public Sig newSignature(ByteBuffer buf) {
	return new Sig(buf);
    }

    /**
     */
    public class Sig implements Signature {

	//######################################################################
	// instance members

	protected final ByteBuffer buf;
	protected final int pos;
	protected final int length;

	public Sig(ByteBuffer buf) {
	    this.buf = buf;
	    //int tmpPos = buf.position();
	    length = decodeInt();
	    this.pos = buf.position();
	}

	public String toString() {
	    StringBuffer b = new StringBuffer("(");
	    reset();
	    for (int i = 0; i < length; i++) {
		b.append(byte2hex(readByte()));
		if (i < length - 1)
		    b.append(" ");
	    }
	    return b.append(")").toString();
	}

	public Sig reset() { buf.position(pos); return this; }

	public int pos() { return buf.position() - pos; }

	/** @return the byte at the current position in the signature Blob.
	 *  Stay at the same position
	 */
	public int getByte() {
	    return (buf.get(buf.position()) + 0x100) & 0xff;
	}

	/** @return the byte at the current position in the signature Blob.
	 *  Move to the next byte.
	 */
	public int readByte() { return (buf.get() + 0x100) & 0xff; }

	/** Skip the current byte if equal to the given value. */
	public void skipByte(int b) { if (b == getByte()) buf.get(); }

	/** Decodes an integer from the signature Blob.
	 *  @return the decoded integer
	 */
	public int decodeInt() {
	    int res = readByte();
	    if ((res & 0x80) != 0) {
		res = ((res & 0x7f) << 8) | readByte();
		if ((res & 0x4000) != 0)
		    res = ((res & 0x3fff)<<16) | (readByte()<<8) | readByte();
	    }
	    return res;
	}

	/** @return - the type encoded at the current position in the signature
	 *  according to 22.2.12
	 */
	public Type decodeType() {
	    try { return decodeType0(); }
	    catch (RuntimeException e) {
		System.out.println("" + pos() + "@" + this);
		throw e;
	    }
	}

	public Type decodeType0() {
	    Type type = null;
	    int desc = readByte();
	    switch (desc) {
	    case ELEMENT_TYPE_BOOLEAN:type = Type.GetType("System.Boolean"); break;
	    case ELEMENT_TYPE_CHAR:   type = Type.GetType("System.Char"); break;
	    case ELEMENT_TYPE_I1:     type = Type.GetType("System.SByte"); break;
	    case ELEMENT_TYPE_U1:     type = Type.GetType("System.Byte"); break;
	    case ELEMENT_TYPE_I2:     type = Type.GetType("System.Int16"); break;
	    case ELEMENT_TYPE_U2:     type = Type.GetType("System.UInt16"); break;
	    case ELEMENT_TYPE_I4:     type = Type.GetType("System.Int32"); break;
	    case ELEMENT_TYPE_U4:     type = Type.GetType("System.UInt32"); break;
	    case ELEMENT_TYPE_I8:     type = Type.GetType("System.Int64"); break;
	    case ELEMENT_TYPE_U8:     type = Type.GetType("System.UInt64"); break;
	    case ELEMENT_TYPE_R4:     type = Type.GetType("System.Single"); break;
	    case ELEMENT_TYPE_R8:     type = Type.GetType("System.Double"); break;
	    case ELEMENT_TYPE_OBJECT: type = Type.GetType("System.Object"); break;
	    case ELEMENT_TYPE_STRING: type = Type.GetType("System.String"); break;
	    case ELEMENT_TYPE_I:      type = Type.GetType("System.IntPtr"); break;
	    case ELEMENT_TYPE_U:      type = Type.GetType("System.UIntPtr"); break;
	    case ELEMENT_TYPE_PTR:        // Followed by <type> token.
		if (getByte() == ELEMENT_TYPE_VOID) {
		    readByte();
		    type = Type.mkPtr(Type.GetType("System.Void"));
		} else type = Type.mkPtr(decodeType());
		break;
	    case ELEMENT_TYPE_BYREF:      // Followed by <type> token.
	    case ELEMENT_TYPE_VALUETYPE:  // Followed by <type> token
		//System.out.println("Signature.getType(): valuetype");
		//type = pemodule.getTypeDefOrRef(decodeInt());
	    case ELEMENT_TYPE_CLASS:
		// Followed by <type> token
		type = pemodule.getTypeDefOrRef(decodeInt());
		if (type == null) throw new RuntimeException();
		break;

	    case ELEMENT_TYPE_SZARRAY:    // Single-dim array with 0 lower bound.
		skipCustomMods();
		type = Type.mkArray(decodeType(), 1);
		break;
	    case ELEMENT_TYPE_ARRAY:
		// <type> <rank> <boundsCount> <bound1> ... <loCount> <lo1> ...
		Type elem = decodeType();
		int rank = decodeInt();
		int numSizes = decodeInt();
		for (int i = 0; i < numSizes; i++)
		    decodeInt();
		int numLoBounds = decodeInt();
		for (int i = 0; i < numLoBounds; i++)
		    decodeInt();
		type = Type.mkArray(elem, rank);
		break;

	    case ELEMENT_TYPE_FNPTR:
		// Followed by full method signature.
	    case ELEMENT_TYPE_END:
		// Marks end of a list
	    case ELEMENT_TYPE_CMOD_REQD:
		// Required modifier : followed by a TypeDef or TypeRef token.
	    case ELEMENT_TYPE_CMOD_OPT:
		// Optional modifier : followed by a TypeDef or TypeRef token.
	    case ELEMENT_TYPE_INTERNAL:
		// Implemented within the CLI.
	    case ELEMENT_TYPE_MODIFIER:
		// Or'd with following element types.
	    case ELEMENT_TYPE_SENTINEL:
		// Sentinel for varargs method signature.
	    case ELEMENT_TYPE_PINNED:
		// Denotes a local variable that points at a pinned object.
	    default:
		throw new RuntimeException(byte2hex(desc) +
					   "@" + pos() + " in " + this);

	    }
	    if (type == null) throw new RuntimeException();
	    return type;
	} // getType()

	public Type decodeFieldType() {
	    skipByte(FIELD);
	    skipCustomMods();
	    return decodeType();
	}

	/** decodes the return type of a method signature (22.2.11). */
	public Type decodeRetType() {
	    skipCustomMods();
	    switch (getByte()) {
	    case ELEMENT_TYPE_VOID:
		readByte();
		return Type.GetType("System.Void");
	    case ELEMENT_TYPE_TYPEDBYREF:
		return Type.GetType("System.TypedReference");
	    case ELEMENT_TYPE_BYREF:
		skipByte(ELEMENT_TYPE_BYREF);
		return decodeType();
	    default:
		return decodeType();
	    }
	}

	public Type decodeParamType() {
	    skipCustomMods();
	    switch (getByte()) {
	    case ELEMENT_TYPE_BYREF:
		skipByte(ELEMENT_TYPE_BYREF);
		return decodeType();
	    case ELEMENT_TYPE_TYPEDBYREF:
		return Type.GetType("System.TypedReference");
	    default:
		return decodeType();
	    }
	}

	public void skipCustomMods() {
	    while (getByte() == ELEMENT_TYPE_CMOD_OPT
		   || getByte() == ELEMENT_TYPE_CMOD_REQD)
		{
		    Type t = decodeType();
		    System.err.println("CMOD: " + t);
		    if (getByte() == ELEMENT_TYPE_CMOD_REQD)
			throw new RuntimeException("Reqired CMOD: " + t);
		}
	}

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

    }  // class Sig

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

}  // class PEFile