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: * PESection.java
023: *
024: * Created on 29 juillet 2003, 21:34
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 PESection implements Cloneable {
038: byte[] ANSI_Name; // Name of the Section. Can be anything (0)(8BYTES)
039: long VirtualSize; // The size of the section when it is mapped to memory. Must be a multiple of 4096. (8)(DWORD)
040: long VirtualAddress; // An rva to where it should be mapped in memory. (12)(DWORD)
041: long SizeOfRawData; // The size of the section in the PE file. Must be a multiple of 512 (16)(DWORD)
042: long PointerToRawData; // A file based offset which points to the location of this sections data (20)(DWORD)
043: long PointerToRelocations; // In EXE's this field is meaningless, and is set 0 (24)(DWORD)
044: long PointerToLinenumbers; // This is the file-based offset of the line number table. This field is only used for debug purposes, and is usualy set to 0 (28)(DWORD)
045: int NumberOfRelocations; // In EXE's this field is meaningless, and is set 0 (32)(WORD)
046: int NumberOfLinenumbers; // The number of line numbers in the line number table for this section. This field is only used for debug purposes, and is usualy set to 0 (34)(WORD)
047: long Characteristics; // The kind of data stored in this section ie. Code, Data, Import data, Relocation data (36)(DWORD)
048:
049: private long m_baseoffset;
050: private PEFile m_pe;
051:
052: /** Creates a new instance of PESection */
053: public PESection(PEFile pef, long baseoffset) {
054: m_pe = pef;
055: m_baseoffset = baseoffset;
056: }
057:
058: public Object clone() throws CloneNotSupportedException {
059: return super .clone();
060: }
061:
062: public String getName() {
063: StringBuffer buffer = new StringBuffer();
064: for (int i = 0; i < 8; i++)
065: buffer.append((char) ANSI_Name[i]);
066: return buffer.toString();
067: }
068:
069: public void read() throws IOException {
070: FileChannel ch = m_pe.getChannel();
071: ByteBuffer head = ByteBuffer.allocate(40);
072: head.order(ByteOrder.LITTLE_ENDIAN);
073: ch.position(m_baseoffset);
074: ch.read(head);
075: head.position(0);
076:
077: ANSI_Name = new byte[8];
078: for (int i = 0; i < 8; i++)
079: ANSI_Name[i] = head.get();
080:
081: VirtualSize = head.getInt();
082: VirtualAddress = head.getInt();
083: SizeOfRawData = head.getInt();
084: PointerToRawData = head.getInt();
085: PointerToRelocations = head.getInt();
086: PointerToLinenumbers = head.getInt();
087: NumberOfRelocations = head.getShort();
088: NumberOfLinenumbers = head.getShort();
089: Characteristics = head.getInt();
090: }
091:
092: public void dump(PrintStream out) {
093: out.println("SECTION:");
094: out.print(" Name= ");
095: for (int i = 0; i < 8; i++)
096: out.print((char) ANSI_Name[i]);
097: out.println("");
098: out
099: .println(" VirtualSize= "
100: + VirtualSize
101: + " // The size of the section when it is mapped to memory. Must be a multiple of 4096. (8)(DWORD)");
102: out
103: .println(" VirtualAddress= "
104: + VirtualAddress
105: + " // An rva to where it should be mapped in memory. (12)(DWORD)");
106: out
107: .println(" SizeOfRawData= "
108: + SizeOfRawData
109: + " // The size of the section in the PE file. Must be a multiple of 512 (16)(DWORD)");
110: out
111: .println(" PointerToRawData= "
112: + PointerToRawData
113: + " // A file based offset which points to the location of this sections data (20)(DWORD)");
114: out
115: .println(" PointerToRelocations= "
116: + PointerToRelocations
117: + " // In EXE's this field is meaningless, and is set 0 (24)(DWORD)");
118: out
119: .println(" PointerToLinenumbers= "
120: + PointerToLinenumbers
121: + " // This is the file-based offset of the line number table. This field is only used for debug purposes, and is usualy set to 0 (28)(DWORD)");
122: out
123: .println(" NumberOfRelocations= "
124: + NumberOfRelocations
125: + " // In EXE's this field is meaningless, and is set 0 (32)(WORD)");
126: out
127: .println(" NumberOfLinenumbers= "
128: + NumberOfLinenumbers
129: + " // The number of line numbers in the line number table for this section. This field is only used for debug purposes, and is usualy set to 0 (34)(WORD)");
130: out
131: .println(" Characteristics= "
132: + Characteristics
133: + " // The kind of data stored in this section ie. Code, Data, Import data, Relocation data (36)(DWORD)");
134:
135: }
136:
137: public ByteBuffer get() {
138: ByteBuffer head = ByteBuffer.allocate(40);
139: head.order(ByteOrder.LITTLE_ENDIAN);
140: head.position(0);
141:
142: for (int i = 0; i < 8; i++)
143: head.put((byte) ANSI_Name[i]);
144:
145: head.putInt((int) VirtualSize);
146: head.putInt((int) VirtualAddress);
147: head.putInt((int) SizeOfRawData);
148: head.putInt((int) PointerToRawData);
149: head.putInt((int) PointerToRelocations);
150: head.putInt((int) PointerToLinenumbers);
151: head.putShort((short) NumberOfRelocations);
152: head.putShort((short) NumberOfLinenumbers);
153: head.putInt((int) Characteristics);
154:
155: head.position(0);
156: return head;
157: }
158:
159: }
|