[Solved]Reading certain Address locations from file.
Hey Everyone,
I've run into a spot of trouble... I am developing an application in java and I need to open an executable file, starting at memory location 0x000081C0 read and convert the next 24 hex digits to readable ASCII format and store this new string into a variable...
Can anyone help me with this, would be much appreciated.
Thanks,
Kriogenic.
Find nth location in a file
Your solution is similar to one of several that
could be used. I started to work on something like
that, then I looked up some string methods in the
API and decided to modify another application I
already have in my files.
The following is a complete working java application.
I found it on the internet, and do not want to take
credit for its design, which I think is very well
organized and easy to analyze.
Normally it can be used to open any file, allow some
elementary editing of its contents, then save it as
another file.
By adding the following variables and one line of
code..
Code:
private int nthValue = 32;
private int length = 5;
private String embeddedString;
embeddedString = new String(b, nthValue, length);
..I modified this application to extract 5 bytes
starting from location 32 of the file.
I hope this is enough of an example to give you
more ideas on how to accomplish your goal.
Code:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class RandomAccessDemo extends JFrame implements ActionListener{
private JMenuItem jmiOpen;
private JMenuItem jmiSave;
private JMenuItem jmiExit;
private JMenuItem jmiAbout;
private JTextArea jta = new JTextArea();
private JLabel jlblStatus = new JLabel();
private JFileChooser jFileChooser = new JFileChooser();
private int nthValue = 32;
private int length = 5;
private String embeddedString;
public static void main(String [] args){
RandomAccessDemo frame = new RandomAccessDemo();
frame.setLocation(100, 200);
frame.setTitle("Test JFileChooser");
frame.setSize(300,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public RandomAccessDemo(){
JMenuBar mb = new JMenuBar();
setJMenuBar(mb);
JMenu fileMenu = new JMenu("File");
mb.add(fileMenu);
JMenu helpMenu = new JMenu("Help");
mb.add(helpMenu);
fileMenu.add(jmiOpen = new JMenuItem("Open"));
fileMenu.add(jmiSave = new JMenuItem("Save"));
fileMenu.addSeparator();
fileMenu.add(jmiExit = new JMenuItem("Exit"));
helpMenu.add(jmiAbout = new JMenuItem("About"));
jFileChooser.setCurrentDirectory(new File("."));
getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);
getContentPane().add(jlblStatus, BorderLayout.SOUTH);
jmiOpen.addActionListener(this);
jmiSave.addActionListener(this);
jmiExit.addActionListener(this);
jmiAbout.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String actionCommand = e.getActionCommand();
if (e.getSource() instanceof JMenuItem){
if ("Open" .equals(actionCommand)){
open();
}
else if ("Save" .equals(actionCommand)){
save();
}
else if ("About" .equals(actionCommand)){
JOptionPane.showMessageDialog(this, "Demonstrate Using File Dialogs",
"About this Demo", JOptionPane.INFORMATION_MESSAGE);
}
else if ("Exit" .equals(actionCommand)){
System.exit(0);
}
}
}
private void open(){
if (jFileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
open(jFileChooser.getSelectedFile());
}
}
private void open(File file){
try{
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
byte [] b = new byte[in.available()];
in.read(b, 0, b.length);
jta.append(new String(b, 0, b.length));
in.close();
embeddedString = new String(b, nthValue, 5);
jlblStatus.setText("\"" + embeddedString + "\" extracted from location " + nthValue);
}
catch(IOException ex){
jlblStatus.setText("Error opening file " + file.getName());
}
}
private void save(){
if (jFileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
save(jFileChooser.getSelectedFile());
}
}
private void save(File file){
try{
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
byte [] b = (jta.getText()).getBytes();
out.write(b, 0, b.length);
out.close();
jlblStatus.setText(file.getName() + " saved ");
}
catch (IOException ex){
jlblStatus.setText("Error saving " + file.getName());
}
}
}