Fixed Hex Viewer

This fixes the hex viewer as a decompiler option, it also attempts to solve #315 by allowing the contents in the hex editor to draw dynamically

This is just temporary until the hex editor is replaced
This commit is contained in:
Konloch 2021-07-16 18:41:33 -07:00
parent 80c4b4fb72
commit 37df1802a9
4 changed files with 101 additions and 57 deletions

View file

@ -14,15 +14,18 @@ import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.*;
/**
* Created by IntelliJ IDEA. User: laullon Date: 08-abr-2003 Time: 13:21:09
* @author laullon
* @since 08/04/2003
*/
public class JHexEditor extends JPanel implements FocusListener,
AdjustmentListener, MouseWheelListener {
private static final long serialVersionUID = 2289328616534802372L;
public class JHexEditor extends JPanel implements FocusListener, AdjustmentListener, MouseWheelListener
{
protected int textLength = 16;
protected int lastWidth;
byte[] buff;
public int cursor;
protected static Font font = new Font("Monospaced", Font.PLAIN, 12);
@ -31,10 +34,14 @@ public class JHexEditor extends JPanel implements FocusListener,
private final JScrollBar sb;
private int inicio = 0;
private int lineas = 10;
public JHexEditor(byte[] buff) {
private JHexEditorHEX hex;
private JHexEditorASCII ascii;
public JHexEditor(byte[] buff)
{
super();
this.buff = buff;
checkSize();
this.addMouseWheelListener(this);
@ -45,8 +52,9 @@ public class JHexEditor extends JPanel implements FocusListener,
JPanel p1, p2, p3;
// centro
hex = new JHexEditorHEX(this);
p1 = new JPanel(new BorderLayout(1, 1));
p1.add(new JHexEditorHEX(this), BorderLayout.CENTER);
p1.add(hex, BorderLayout.CENTER);
p1.add(new Columnas(), BorderLayout.NORTH);
// izq.
@ -55,9 +63,10 @@ public class JHexEditor extends JPanel implements FocusListener,
p2.add(new Caja(), BorderLayout.NORTH);
// der
ascii = new JHexEditorASCII(this);
p3 = new JPanel(new BorderLayout(1, 1));
p3.add(sb, BorderLayout.EAST);
p3.add(new JHexEditorASCII(this), BorderLayout.CENTER);
p3.add(ascii, BorderLayout.CENTER);
p3.add(new Caja(), BorderLayout.NORTH);
JPanel panel = new JPanel();
@ -71,17 +80,20 @@ public class JHexEditor extends JPanel implements FocusListener,
}
@Override
public void paint(Graphics g) {
public void paint(Graphics g)
{
checkSize();
FontMetrics fn = getFontMetrics(font);
Rectangle rec = this.getBounds();
lineas = (rec.height / fn.getHeight()) - 1;
int n = (buff.length / 16) - 1;
int n = (buff.length / textLength) - 1;
if (lineas > n) {
lineas = n;
inicio = 0;
}
sb.setValues(getInicio(), +getLineas(), 0, buff.length / 16);
sb.setValues(getInicio(), +getLineas(), 0, buff.length / textLength);
sb.setValueIsAdjusting(true);
super.paint(g);
}
@ -135,8 +147,8 @@ public class JHexEditor extends JPanel implements FocusListener,
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
inicio += (e.getUnitsToScroll());
if ((inicio + lineas) >= buff.length / 16)
inicio = (buff.length / 16) - lineas;
if ((inicio + lineas) >= buff.length / textLength)
inicio = (buff.length / textLength) - lineas;
if (inicio < 0)
inicio = 0;
repaint();
@ -175,7 +187,7 @@ public class JHexEditor extends JPanel implements FocusListener,
FontMetrics fn = getFontMetrics(font);
int h = fn.getHeight();
int nl = 1;
d.setSize(((fn.stringWidth(" ") + 1) * +((16 * 3) - 1))
d.setSize(((fn.stringWidth(" ") + 1) * +((textLength * 3) - 1))
+ (border * 2) + 1, h * nl + (border * 2) + 1);
return d;
}
@ -188,8 +200,8 @@ public class JHexEditor extends JPanel implements FocusListener,
g.setColor(Color.black);
g.setFont(font);
for (int n = 0; n < 16; n++) {
if (n == (cursor % 16))
for (int n = 0; n < textLength; n++) {
if (n == (cursor % textLength))
cuadro(g, n * 3, 0, 2);
String s = "00" + Integer.toHexString(n);
s = s.substring(s.length() - 2);
@ -242,7 +254,8 @@ public class JHexEditor extends JPanel implements FocusListener,
}
@Override
public void paint(Graphics g) {
public void paint(Graphics g)
{
Dimension d = getMinimumSize();
g.setColor(Color.white);
g.fillRect(0, 0, d.width, d.height);
@ -252,8 +265,9 @@ public class JHexEditor extends JPanel implements FocusListener,
int ini = getInicio();
int fin = ini + getLineas();
int y = 0;
for (int n = ini; n < fin; n++) {
if (n == (cursor / 16))
for (int n = ini; n < fin; n++)
{
if (n == (cursor / textLength))
cuadro(g, 0, y, 8);
String s = "0000000000000" + Integer.toHexString(n);
s = s.substring(s.length() - 8);
@ -261,4 +275,21 @@ public class JHexEditor extends JPanel implements FocusListener,
}
}
}
public void checkSize()
{
int width = getWidth();
if(lastWidth != width)
{
double spacer = 1.5;
textLength = (int) ((int) (width / 28.4)/spacer);
lastWidth = width;
hex.revalidate();
ascii.revalidate();
revalidate();
}
}
private static final long serialVersionUID = 2289328616534802372L;
}

View file

@ -11,11 +11,12 @@ import java.awt.event.MouseListener;
import javax.swing.JComponent;
/**
* Created by IntelliJ IDEA. User: laullon Date: 09-abr-2003 Time: 12:47:18
* @author laullon
* @since 09/04/2003
*/
public class JHexEditorASCII extends JComponent implements MouseListener,
KeyListener {
private static final long serialVersionUID = 5505374841731053461L;
public class JHexEditorASCII extends JComponent implements MouseListener, KeyListener
{
private final JHexEditor he;
public JHexEditorASCII(JHexEditor he) {
@ -32,15 +33,30 @@ public class JHexEditorASCII extends JComponent implements MouseListener,
}
@Override
public Dimension getMinimumSize() {
public Dimension getMinimumSize()
{
debug("getMinimumSize()");
Dimension d = new Dimension();
FontMetrics fn = getFontMetrics(JHexEditor.font);
int w = fn.stringWidth(" ");
int h = fn.getHeight();
int nl = he.getLineas();
d.setSize((fn.stringWidth(" ") + 1) * (16) + (he.border * 2) + 1, h
* nl + (he.border * 2) + 1);
int len = he.textLength + 1;
int width = (len * w) + (he.border * 2) + 5;
//trim inaccuracy
if(len > 16)
{
int diff = 16-len;
width += ((len * w) / (diff) * diff);
}
//System.out.println("Values: " + w + " and " + nl + " vs " + len + " ["+width+"]");
d.setSize(width, h * nl + (he.border * 2) + 1);
return d;
}
@ -56,8 +72,8 @@ public class JHexEditorASCII extends JComponent implements MouseListener,
g.setFont(JHexEditor.font);
// datos ascii
int ini = he.getInicio() * 16;
int fin = ini + (he.getLineas() * 16);
int ini = he.getInicio() * he.textLength;
int fin = ini + (he.getLineas() * he.textLength);
if (fin > he.buff.length)
fin = he.buff.length;
@ -82,7 +98,7 @@ public class JHexEditorASCII extends JComponent implements MouseListener,
if ((he.buff[n] < 20) || (he.buff[n] > 126))
s = ".";//"" + (char) 16;
he.printString(g, s, (x++), y);
if (x == 16) {
if (x == he.textLength) {
x = 0;
y++;
}
@ -101,7 +117,7 @@ public class JHexEditorASCII extends JComponent implements MouseListener,
x = x / (fn.stringWidth(" ") + 1);
y = y / fn.getHeight();
debug("x=" + x + " ,y=" + y);
return x + ((y + he.getInicio()) * 16);
return x + ((y + he.getInicio()) * he.textLength);
}
// mouselistener
@ -162,4 +178,6 @@ public class JHexEditorASCII extends JComponent implements MouseListener,
public boolean isFocusable() {
return true;
}
private static final long serialVersionUID = 5505374841731053461L;
}

View file

@ -11,42 +11,32 @@ import java.awt.event.MouseListener;
import javax.swing.JComponent;
/**
* Created by IntelliJ IDEA. User: laullon Date: 09-abr-2003 Time: 12:47:32
* @author laullon
* @since 09/04/2003
*/
public class JHexEditorHEX extends JComponent implements MouseListener,
KeyListener {
private static final long serialVersionUID = 1481995655372014571L;
public class JHexEditorHEX extends JComponent implements MouseListener, KeyListener
{
private final JHexEditor he;
public JHexEditorHEX(JHexEditor he) {
public JHexEditorHEX(JHexEditor he)
{
this.he = he;
addMouseListener(this);
addKeyListener(this);
addFocusListener(he);
}
/*
* public Dimension getPreferredSize() { debug("getPreferredSize()"); return
* getMinimumSize(); }
*/
@Override
public Dimension getMaximumSize() {
public Dimension getMaximumSize()
{
debug("getMaximumSize()");
return getMinimumSize();
}
/*
* public Dimension getMinimumSize() { debug("getMinimumSize()");
*
* Dimension d=new Dimension(); FontMetrics fn=getFontMetrics(he.font); int
* h=fn.getHeight(); int nl=he.getLineas();
* d.setSize(((fn.stringWidth(" ")+1
* )*+((16*3)-1))+(he.border*2)+1,h*nl+(he.border*2)+1); return d; }
*/
@Override
public void paint(Graphics g) {
public void paint(Graphics g)
{
debug("paint(" + g + ")");
debug("cursor=" + he.cursor + " buff.length=" + he.buff.length);
Dimension d = getMinimumSize();
@ -56,8 +46,8 @@ public class JHexEditorHEX extends JComponent implements MouseListener,
g.setFont(JHexEditor.font);
int ini = he.getInicio() * 16;
int fin = ini + (he.getLineas() * 16);
int ini = he.getInicio() * he.textLength;
int fin = ini + (he.getLineas() * he.textLength);
if (fin > he.buff.length)
fin = he.buff.length;
@ -88,7 +78,7 @@ public class JHexEditorHEX extends JComponent implements MouseListener,
String s = ("0" + Integer.toHexString(he.buff[n]));
s = s.substring(s.length() - 2);
he.printString(g, s, ((x++) * 3), y);
if (x == 16) {
if (x == he.textLength) {
x = 0;
y++;
}
@ -106,7 +96,7 @@ public class JHexEditorHEX extends JComponent implements MouseListener,
x = x / ((fn.stringWidth(" ") + 1) * 3);
y = y / fn.getHeight();
debug("x=" + x + " ,y=" + y);
return x + ((y + he.getInicio()) * 16);
return x + ((y + he.getInicio()) * he.textLength);
}
// mouselistener
@ -174,4 +164,6 @@ public class JHexEditorHEX extends JComponent implements MouseListener,
public boolean isFocusable() {
return true;
}
private static final long serialVersionUID = 1481995655372014571L;
}

View file

@ -158,6 +158,9 @@ public class BytecodeViewPanelUpdater implements Runnable
return;
processDisplay();
if(bytecodeViewPanel.decompiler == Decompiler.HEXCODE_VIEWER)
return;
//nullcheck broken pane
if(updateUpdaterTextArea == null || updateUpdaterTextArea.getScrollPane() == null