Advertisement
SforzandoCF

LJDGX gui

Sep 23rd, 2024 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.61 KB | None | 0 0
  1. package lx01.projects.jd;
  2.  
  3. public class GUI {
  4.     public static void runGUI () {
  5.         try {
  6.             UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
  7.         } catch (ClassNotFoundException cnfe) {
  8.             System.err.println("Could not find the Nimbus platform look and feel");
  9.         } catch (InstantiationException ie) {
  10.             System.err.println("Could not instantiate the Nimbus platform look and feel");
  11.         } catch (IllegalAccessException iae) {
  12.             System.err.println("Could not access the Nimbus platform look and feel");
  13.         } catch (UnsupportedLookAndFeelException ulafe) {
  14.             System.err.println("Nimbus platform look and feel isn't supported");
  15.         }
  16.         JFileChooser chooser = new JFileChooser();
  17.         chooser.setDialogTitle("LJDGX Java Decompiler - Class selection dialog");
  18.         chooser.setFileFilter(new FileNameExtensionFilter("Java JARs and classes", "jar", "class"));
  19.         chooser.setMultiSelectionEnabled(false);
  20.         chooser.addActionListener((e) -> {
  21.             if (chooser.getSelectedFile() != null) {
  22.                 DecompiledClass c = JavaDecompiler.decompile(new FileInputStream(chooser.getSelectedFile()));
  23.                 openViewport(c, Files.readAllBytes(new File(chooser.getSelectedFile()).toPath()));
  24.             }
  25.         });
  26.         chooser.showOpenDialog();
  27.     }
  28.    
  29.     public void openViewport (DecompiledClass c, byte[] data) {
  30.         JFrame gui = new JFrame("LJDGX Java Decompiler");
  31.         gui.setLayout(new GridLayout(1, 2, 5, 5));
  32.         try {
  33.             UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
  34.         } catch (ClassNotFoundException cnfe) {
  35.             System.err.println("Could not find the Nimbus platform look and feel");
  36.         } catch (InstantiationException ie) {
  37.             System.err.println("Could not instantiate the Nimbus platform look and feel");
  38.         } catch (IllegalAccessException iae) {
  39.             System.err.println("Could not access the Nimbus platform look and feel");
  40.         } catch (UnsupportedLookAndFeelException ulafe) {
  41.             System.err.println("Nimbus platform look and feel isn't supported");
  42.         }
  43.         boolean[] modeSwitch = new boolean[2];
  44.         JTextArea area = new JTextArea(c.toFormattedString(true), 47, 24);
  45.         area.setEditable(false);
  46.         JPanel buttons = new JPanel(new GridLayout(2, 1, 5, 5));
  47.         JButton textView = new JButton("Enter text view");
  48.         textView.addActionListener((e) -> {
  49.             if (!modeSwitch[0]) {
  50.                 area.setText(c.toFormattedString(true));
  51.                 modeSwitch[0] = true;
  52.                 modeSwitch[1] = false;
  53.             }
  54.         });
  55.         buttons.add(textView);
  56.         JButton hexView = new JButton("Enter hex view");
  57.         textView.addActionListener((e) -> {
  58.             if (!modeSwitch[1]) {
  59.                 area.setText(formatHex(data));
  60.                 modeSwitch[0] = false;
  61.                 modeSwitch[1] = true;
  62.             }
  63.         });
  64.         buttons.add(hexView);
  65.         gui.add(buttons);
  66.         gui.add(area);
  67.     }
  68.    
  69.     public static String formatHex (byte[] data) {
  70.         String formattedHex = "<html>";
  71.         int constantPoolEnd = getConstantPoolByteLength((data[8] << 8) | data[9], data) + 10;
  72.         int fieldsEnd = getFieldsByteLength(data, constantPoolEnd + 8 + (((data[constantPoolEnd] << 8) | data[constantPoolEnd + 1]) * 2)) + constantPoolEnd + 4 + ((data[constantPoolEnd] << 8) | data[constantPoolEnd + 1]) * 2;
  73.         int methodsEnd = getMethodsByteLength((data, fieldsEnd + 8 + (((data[constantPoolEnd] << 8) | data[constantPoolEnd + 1]) * 2))) + fieldsEnd + 4 + ((data[fieldsEnd] << 8) | data[fieldsEnd + 1]) * 2;
  74.         for (int i = 0; i <= data.length; i++) {
  75.             if (i == methodsEnd + 2) formattedHex = formattedHex.concat("<font color=blue>");
  76.             if (i == methodsEnd) formattedHex = formattedHex.concat("<font color=darkblue>");
  77.             if (i == fieldsEnd + 2) formattedHex = formattedHex.concat("<font color=lime>");
  78.             if (i == constantPoolEnd + (((data[constantPoolEnd] << 8) | data[constantPoolEnd + 1]) * 2) + 8) formattedHex = formattedHex.concat("<font color=green>");
  79.             if (i == constantPoolEnd + 8) formattedHex = formattedHex.concat("<font color=magenta>");
  80.             if (i == constantPoolEnd + 2) formattedHex = formattedHex.concat("<font color=purple>");
  81.             if (i == constantPoolEnd) formattedHex = formattedHex.concat("<font color=cyan>");
  82.             if (i == 10) formattedHex = formattedHex.concat("<font color=red>");
  83.             if (i == 8) formattedHex = formattedHex.concat("<font color=maroon>");
  84.             if (i == 0) formattedHex = formattedHex.concat("<font color=silver>");
  85.             String snipped = Integer.toHexString(data[i]).substring(6);
  86.             formattedHex = formattedHex.concat(snipped + " ");
  87.             if (i == fieldsEnd - 1) + 9) formattedHex = formattedHex.concat("</font>");
  88.             if (i == constantPoolEnd + (((data[constantPoolEnd] << 8) | data[constantPoolEnd + 1]) * 2) + 9) formattedHex = formattedHex.concat("</font>");
  89.             if (i == constantPoolEnd + 7) formattedHex = formattedHex.concat("</font>");
  90.             if (i == constantPoolEnd + 1) formattedHex = formattedHex.concat("</font>");
  91.             if (i == constantPoolEnd - 1) formattedHex = formattedHex.concat("</font>");
  92.             if (i == 9) formattedHex = formattedHex.concat("</font>");
  93.             if (i == 7) formattedHex = formattedHex.concat("</font>");
  94.         }
  95.         return formattedHex.substring(0, formattedHex.length() - 2).concat("</font></html>");
  96.     }
  97.    
  98.     public static int getConstantPoolByteLength (int length, byte[] data) {
  99.         byte[] constantPoolAndOn = new byte[data.length - 10];
  100.         System.arraycopy(data, 10, constantPoolAndOn, 0, data.length - 10);
  101.         int totalLength = 0;
  102.         for (int i = 0; i <= length; i++) {
  103.             int entryLength;
  104.             switch (constantPoolAndOn[0]) {
  105.                 case 1:
  106.                     entryLength = (constantPoolAndOn[1] << 8) | constantPoolAndOn[2];
  107.                     break;
  108.                 case 3:
  109.                     entryLength = 4;
  110.                     break;
  111.                 case 4:
  112.                     entryLength = 4;
  113.                     break;
  114.                 case 5:
  115.                     entryLength = 8;
  116.                     break;
  117.                 case 6:
  118.                     entryLength = 8;
  119.                     break;
  120.                 case 7:
  121.                     entryLength = 2;
  122.                     break;
  123.                 case 8:
  124.                     entryLength = 2;
  125.                     break;
  126.                 case 9:
  127.                     entryLength = 4;
  128.                     break;
  129.                 case 10:
  130.                     entryLength = 4;
  131.                     break;
  132.                 case 11:
  133.                     entryLength = 4;
  134.                     break;
  135.                 case 12:
  136.                     entryLength = 4;
  137.                     break;
  138.                 case 15:
  139.                     entryLength = 3;
  140.                     break;
  141.                 case 16:
  142.                     entryLength = 2;
  143.                     break;
  144.                 case 18:
  145.                     entryLength = 4;
  146.                     break;
  147.                 default:
  148.                     entryLength = 0;
  149.             }
  150.             totalLength += 1 + entryLength;
  151.             byte[] temp = new byte[constantPoolAndOn.length];
  152.             System.arraycopy(constantPoolAndOn, 1 + entryLength, temp, 0, constantPoolAndOn.length);
  153.             System.arraycopy(temp, 0, constantPoolAndOn, 0, constantPoolAndOn.length);
  154.         }
  155.         return totalLength;
  156.     }
  157.    
  158.     public static int getFieldsByteLength (byte[] data, int start) {
  159.         int length = (data[start] << 8) | data[start + 1];
  160.         byte[] fieldsAndOn = new byte[data.length - start + 2];
  161.         System.arraycopy(data, 10, constantPoolAndOn, 0, data.length - start + 2);
  162.         int totalLength = 0;
  163.         for (int i = 0; i <= length; i++) {
  164.             int entryLength = 0;
  165.             for (int i = 0; i <= (fieldsAndOn[6] << 8) | fieldsAndOn[7]; i++)
  166.                 entryLength += getAttributeByteLength(fieldsAndOn, 8);
  167.             totalLength += 8 + entryLength;
  168.             byte[] temp = new byte[fieldsAndOn.length];
  169.             System.arraycopy(fieldsAndOn, 8 + entryLength, temp, 0, fieldsAndOn.length);
  170.             System.arraycopy(temp, 0, fieldsAndOn, 0, fieldsAndOn.length);
  171.         }
  172.         return totalLength;
  173.     }
  174.    
  175.     public static int getMethodsByteLength (byte[] data, int start) {
  176.         int length = (data[start] << 8) | data[start + 1];
  177.         byte[] methodsAndOn = new byte[data.length - start + 2];
  178.         System.arraycopy(data, 10, constantPoolAndOn, 0, data.length - start + 2);
  179.         int totalLength = 0;
  180.         for (int i = 0; i <= length; i++) {
  181.             int entryLength = 0;
  182.             for (int i = 0; i <= (methodsAndOn[6] << 8) | methodsAndOn[7]; i++)
  183.                 entryLength += getAttributeByteLength(methodsAndOn, 8);
  184.             totalLength += 8 + entryLength;
  185.             byte[] temp = new byte[methodsAndOn.length];
  186.             System.arraycopy(methodsAndOn, 8 + entryLength, temp, 0, methodsAndOn.length);
  187.             System.arraycopy(temp, 0, methodsAndOn, 0, methodsAndOn.length);
  188.         }
  189.         return totalLength;
  190.     }
  191.    
  192.     public static int getAttributeByteLength(byte[] data, int start) {
  193.         return 6 + ((data[start + 2] << 24) | (data[start + 3] << 16)) | ((data[start + 4] << 8) | data[start + 5]);
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement