001: /*
002: JSmooth: a VM wrapper toolkit for Windows
003: Copyright (C) 2003 Rodrigo Reyes <reyes@charabia.net>
004:
005: This program is free software; you can redistribute it and/or modify
006: it under the terms of the GNU General Public License as published by
007: the Free Software Foundation; either version 2 of the License, or
008: (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public License
016: along with this program; if not, write to the Free Software
017: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018:
019: */
020:
021: /*
022: * PEOldMSHeader.java
023: *
024: * Created on 28 juillet 2003, 22:05
025: */
026:
027: package net.charabia.jsmoothgen.pe;
028:
029: import java.io.*;
030: import java.nio.*;
031: import java.nio.channels.*;
032:
033: /**
034: *
035: * @author Rodrigo
036: */
037: public class PEOldMSHeader implements Cloneable {
038: int e_cblp; // Bytes on last page of file // 2
039: int e_cp; // Pages in file // 4
040: int e_crlc; // Relocations // 6
041: int e_cparhdr; // Size of header in paragraphs // 8
042: int e_minalloc; // Minimum extra paragraphs needed // A
043: int e_maxalloc; // Maximum extra paragraphs needed // C
044: int e_ss; // Initial (relative) SS value // E
045: int e_sp; // Initial SP value // 10
046: int e_csum; // Checksum // 12
047: int e_ip; // Initial IP value // 14
048: int e_cs; // Initial (relative) CS value // 16
049: int e_lfarlc; // File address of relocation table // 18
050: int e_ovno; // Overlay number // 1A
051: int[] e_res = new int[4]; // Reserved words // 1C
052: int e_oemid; // OEM identifier (for e_oeminfo) // 24
053: int e_oeminfo; // OEM information; e_oemid specific // 26
054: int[] e_res2 = new int[10]; // Reserved words // 28
055: long e_lfanew; // File address of new exe header // 3C
056:
057: private PEFile m_pe;
058:
059: /** Creates a new instance of PEOldMSHeader */
060: public PEOldMSHeader(PEFile pe) {
061: m_pe = pe;
062: }
063:
064: public Object clone() throws CloneNotSupportedException {
065: return super .clone();
066: }
067:
068: public void read() throws IOException {
069: FileChannel ch = m_pe.getChannel();
070: ByteBuffer mz = ByteBuffer.allocate(64);
071: mz.order(ByteOrder.LITTLE_ENDIAN);
072:
073: ch.read(mz, 0);
074: mz.position(0);
075:
076: byte m = mz.get();
077: byte z = mz.get();
078: if ((m == 77) && (z == 90)) {
079: // System.out.println("MZ found !");
080: }
081:
082: e_cblp = mz.getShort(); // Bytes on last page of file // 2
083: e_cp = mz.getShort(); // Pages in file // 4
084: e_crlc = mz.getShort(); // Relocations // 6
085: e_cparhdr = mz.getShort(); // Size of header in paragraphs // 8
086: e_minalloc = mz.getShort(); // Minimum extra paragraphs needed // A
087: e_maxalloc = mz.getShort(); // Maximum extra paragraphs needed // C
088: e_ss = mz.getShort(); // Initial (relative) SS value // E
089: e_sp = mz.getShort(); // Initial SP value // 10
090: e_csum = mz.getShort(); // Checksum // 12
091: e_ip = mz.getShort(); // Initial IP value // 14
092: e_cs = mz.getShort(); // Initial (relative) CS value // 16
093: e_lfarlc = mz.getShort(); // File address of relocation table // 18
094: e_ovno = mz.getShort(); // Overlay number // 1A
095:
096: for (int i = 0; i < 4; i++)
097: e_res[i] = mz.getShort();
098:
099: e_oemid = mz.getShort(); // OEM identifier (for e_oeminfo) // 24
100: e_oeminfo = mz.getShort(); // OEM information; e_oemid specific // 26
101:
102: for (int i = 0; i < 10; i++)
103: e_res2[i] = mz.getShort(); // Reserved words // 28
104:
105: e_lfanew = mz.getInt(); // File address of new exe header // 3C
106:
107: // System.out.println("exe header : " + e_lfanew);
108: }
109:
110: public void dump(PrintStream out) {
111: out.println("MSHeader:");
112:
113: out.println("e_cblp: " + e_cblp
114: + " // Bytes on last page of file // 2");
115: out.println("e_cp: " + e_cp + " // Pages in file // 4");
116: out.println("e_crlc: " + e_crlc + " // Relocations // 6");
117: out.println("e_cparhdr: " + e_cparhdr
118: + " // Size of header in paragraphs // 8");
119: out.println("e_minalloc: " + e_minalloc
120: + " // Minimum extra paragraphs needed // A");
121: out.println("e_maxalloc: " + e_maxalloc
122: + " // Maximum extra paragraphs needed // C");
123: out.println("e_ss: " + e_ss
124: + " // Initial (relative) SS value // E");
125: out.println("e_sp: " + e_sp + " // Initial SP value // 10");
126: out.println("e_csum: " + e_csum + " // Checksum // 12");
127: out.println("e_ip: " + e_ip + " // Initial IP value // 14");
128: out.println("e_cs: " + e_cs
129: + " // Initial (relative) CS value // 16");
130: out.println("e_lfarlc: " + e_lfarlc
131: + " // File address of relocation table // 18");
132: out.println("e_ovno: " + e_ovno + " // Overlay number // 1A");
133: // int[] e_res = new int[4]; // Reserved words // 1C
134: out.println("e_oemid: " + e_oemid
135: + " // OEM identifier (for e_oeminfo) // 24");
136: out.println("e_oeminfo: " + e_oeminfo
137: + " // OEM information; e_oemid specific // 26");
138: // int[] e_res2 = new int[10]; // Reserved words // 28
139: out.println("e_lfanew: " + e_lfanew
140: + " // File address of new exe header // 3C");
141: }
142:
143: public ByteBuffer get() {
144: ByteBuffer mz = ByteBuffer.allocate(64);
145: mz.order(ByteOrder.LITTLE_ENDIAN);
146: mz.position(0);
147:
148: mz.put((byte) 77);
149: mz.put((byte) 90);
150:
151: mz.putShort((short) e_cblp); // Bytes on last page of file // 2
152: mz.putShort((short) e_cp); // Pages in file // 4
153: mz.putShort((short) e_crlc); // Relocations // 6
154: mz.putShort((short) e_cparhdr); // Size of header in paragraphs // 8
155:
156: mz.putShort((short) e_minalloc); // Minimum extra paragraphs needed // A
157: mz.putShort((short) e_maxalloc); // Maximum extra paragraphs needed // C
158: mz.putShort((short) e_ss); // Initial (relative) SS value // E
159: mz.putShort((short) e_sp); // Initial SP value // 10
160: mz.putShort((short) e_csum); // Checksum // 12
161: mz.putShort((short) e_ip); // Initial IP value // 14
162: mz.putShort((short) e_cs); // Initial (relative) CS value // 16
163: mz.putShort((short) e_lfarlc); // File address of relocation table // 18
164: mz.putShort((short) e_ovno); // Overlay number // 1A
165:
166: for (int i = 0; i < 4; i++)
167: mz.putShort((short) e_res[i]);
168:
169: mz.putShort((short) e_oemid); // OEM identifier (for e_oeminfo) // 24
170: mz.putShort((short) e_oeminfo); // OEM information; e_oemid specific // 26
171:
172: for (int i = 0; i < 10; i++)
173: mz.putShort((short) e_res2[i]); // Reserved words // 28
174:
175: mz.putInt((int) e_lfanew); // File address of new exe header // 3C
176:
177: mz.position(0);
178: return mz;
179: }
180:
181: }
|