之前写的 PEdump 的改进版,添加了停止和继续

时间:2021-11-08 17:05:52

效果如下:


之前写的 PEdump 的改进版,添加了停止和继续



之前写的 PEdump 的改进版,添加了停止和继续




改进后的源代码:

pedump.java

package com.PEdump;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.math.BigInteger;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.DefaultCaret;

public class pedump extends JFrame implements ActionListener {

	// 文件
	File file=null;
	
	JMenuBar bar;
	JMenu menuFile, menuShow;
	JMenuItem itemOpen, itemExit, itemSourceFile, itemScale, itemOpacity,
			itemWidth, itemStop, itemResume;
	JTextArea textarea;
	JScrollPane scroll;
	
	pedumpthread pthread=null;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new pedump();
	}

	public pedump() {

		// 创建菜单
		bar = new JMenuBar();
		menuFile = new JMenu("文件");
		menuShow = new JMenu("查看");
		itemOpen = new JMenuItem("打开文件");
		itemExit = new JMenuItem("退出");

		itemStop = new JMenuItem("停止Dump...");
		itemResume  = new JMenuItem("继续Dump...");
		itemSourceFile = new JMenuItem("源文件");
		itemScale = new JMenuItem("大小");
		itemOpacity = new JMenuItem("窗口透明度");
		itemWidth = new JMenuItem("宽度");
		textarea = new JTextArea();
		scroll = new JScrollPane(textarea);

		itemStop.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		itemResume.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		menuFile.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		menuShow.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		itemOpen.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		itemExit.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		itemSourceFile.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		itemScale.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		itemOpacity.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		itemWidth.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		textarea.setFont(new Font("Courier New", Font.BOLD, 17));

		// textarea.setEditable(false);
		textarea.setLineWrap(true);
		textarea.setWrapStyleWord(true);
		scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		itemOpen.addActionListener(this);
		itemResume.addActionListener(this);
		itemStop.addActionListener(this);
		
		DefaultCaret caret = (DefaultCaret) textarea.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

		menuFile.add(itemOpen);
		menuFile.addSeparator();
		menuFile.add(itemExit);
		menuShow.add(itemStop);
		menuShow.add(itemResume);
		menuShow.add(itemSourceFile);
		menuShow.add(itemOpacity);
		menuShow.addSeparator();
		menuShow.add(itemScale);
		menuShow.add(itemWidth);

		bar.add(menuFile);
		bar.add(menuShow);

		this.setJMenuBar(bar);
		this.add(scroll);

		this.setTitle("PEdump By SmithJack");
		this.setBounds(50, 50, 844, 550);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource().equals(itemOpen)) {
			JFileChooser chooser = new JFileChooser();
			FileNameExtensionFilter filter = new FileNameExtensionFilter(
					"exe & dll files", "exe", "dll");
			chooser.setFileFilter(filter);
			int returnVal = chooser.showOpenDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				textarea.setText("");
				file = chooser.getSelectedFile();
				pthread=new pedumpthread(file,textarea);
				pthread.start();
			}
		} else if (e.getSource().equals(itemStop)) {
			pthread.suspend();
		} else if (e.getSource().equals(itemResume)) {
			pthread.resume();
		}
		
	}
}


pedumpthread.java

package com.PEdump;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

import javax.swing.JTextArea;

public class pedumpthread implements Runnable {

	// 文件大小
	private long length = 0;
	// 循环计数
	private int count = 0;
	// 余数
	private int remainder = 0;
	// 起始位置
	private int startpos = 0;

	public Thread t;
	boolean suspended = false;

	JTextArea textarea = null;
	File file = null;

	public pedumpthread(File file, JTextArea textarea) {
		this.file = file;
		this.textarea = textarea;
	}

	public void run() {
		// Let the thread sleep for a while.
		long start = System.currentTimeMillis();// 开始时间

		// 处理文件 start
		length = file.length();
		String[] tempbyte = new String[16];
		int tempindex = 0;

		try {
			@SuppressWarnings("resource")
			MappedByteBuffer mappedByteBuffer = new RandomAccessFile(file, "r")
					.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, length);
			for (int offset = 0; offset < length; offset++) {
				if (offset == 0) {
					textarea.append(this.addZero(String.format("%02x\t",
							mappedByteBuffer.position())));
				}

				byte b = mappedByteBuffer.get();

				String hex = Integer.toHexString(b & 0xFF);
				if (hex.length() == 1)
					hex = '0' + hex;

				if (offset != 0 && offset % 16 == 0) {
					textarea.append("  ");
					tempindex = 0;

					for (int i = 0; i < tempbyte.length; i++) {
						textarea.append(this.toStringHex1(tempbyte[i]));
					}

					textarea.append("\r\n");
					textarea.append(this.addZero(String.format("%02x\t",
							mappedByteBuffer.position())));
				}

				tempbyte[tempindex] = hex;

				textarea.append(String.format("%3s", hex.toUpperCase()));

				if (offset == length - 1) {

					for (int i = 0; i < 16 - offset % 16 - 1; i++) {
						textarea.append(String.format("%3s", " "));
					}
					textarea.append("  ");
					for (int i = 0; i < offset % 16; i++) {
						textarea.append(this.toStringHex1(tempbyte[i]));
					}
					for (int i = 0; i < 16 - offset % 16; i++) {
						textarea.append(".");
					}
				}
				tempindex++;
				// ds[offset] = b;
				
				try {
					Thread.sleep(1);
					synchronized (this) {
						while (suspended) {
							wait();
						}
					}
				} catch (InterruptedException e) {
					System.out.println("Thread interrupted.");
					e.printStackTrace();
				}
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {
		}

		// System.out.println("Thread exiting.");

		long end = System.currentTimeMillis();// 结束时间
		textarea.append("\r\n\r\nNIO Memory mapped reads large files, a total of time consuming: "
				+ (end - start) + "ms");
	}

	public String toStringHex1(String s) {
		int tempindex = Integer.parseInt(s, 16);
		if (tempindex > 32 && tempindex < 127) {
			byte[] baKeyword = new byte[s.length() / 2];
			for (int i = 0; i < baKeyword.length; i++) {
				try {
					baKeyword[i] = (byte) (0xff & Integer.parseInt(
							s.substring(i * 2, i * 2 + 2), 16));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			try {
				s = new String(baKeyword, "ASCII");
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			return s;
		} else {
			return ".";
		}
	}

	public String addZero(String str) {
		String zero = "";
		if (str.length() < 8) {
			for (int i = 0; i < 8 - str.length(); i++) {
				zero += "0";
			}
		}
		return zero + str;
	}

	/**
	 * 开始
	 */
	public void start() {
		// System.out.println("Starting thread");
		if (t == null) {
			t = new Thread(this);
			t.start();
		}
	}

	/**
	 * 暂停
	 */
	public void suspend() {
		suspended = true;
	}

	/**
	 * 继续
	 */
	synchronized void resume() {
		suspended = false;
		notify();
	}
}