Search in sources :

Example 1 with TableSearchResult

use of com.wave.hextractor.pojo.TableSearchResult in project hextractor by sewave.

the class HexViewer method createFrame.

/**
 * Creates the frame.
 */
private void createFrame() {
    setVisible(false);
    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    setLookAndFeel();
    setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
    createMenu();
    JPanel firstRow = new JPanel();
    firstRow.setLayout(new FlowLayout());
    JPanel secondRow = new JPanel();
    secondRow.setLayout(new FlowLayout(FlowLayout.LEADING));
    add(firstRow);
    add(secondRow);
    offsetsTextArea = new JTextArea(visibleRows, Constants.HEX_ADDR_SIZE + HEX_STARTS.length() + Constants.SPACE_STR.length());
    offsetsTextArea.setPreferredSize(DIMENSION_0_0);
    offsetsTextArea.setLineWrap(true);
    offsetsTextArea.setBackground(Color.BLACK);
    offsetsTextArea.setForeground(Color.WHITE);
    offsetsTextArea.setEditable(false);
    offsetsTextArea.setDisabledTextColor(Color.WHITE);
    offsetsTextArea.setEnabled(false);
    offsetsTextArea.setFont(BASE_FONT);
    firstRow.add(offsetsTextArea);
    offsetsTextArea.setText(getVisibleOffsets(offset, visibleColumns, visibleRows));
    hexTextArea = new JTextArea(visibleRows, visibleColumns * Constants.HEX_VALUE_SIZE);
    hexTextArea.setLineWrap(true);
    hexTextArea.setBackground(Color.BLACK);
    hexTextArea.setForeground(Color.WHITE);
    hexTextArea.setEditable(false);
    hexTextArea.setDisabledTextColor(Color.WHITE);
    hexTextArea.setEnabled(false);
    hexTextArea.setCaretColor(Color.GRAY);
    hexTextArea.setFont(BASE_FONT);
    firstRow.add(hexTextArea);
    hexTextArea.setText(Utils.getHexAreaFixedWidth(offset, getViewSize(), fileBytes, visibleColumns));
    asciiTextArea = new JTextArea(visibleRows, visibleColumns);
    asciiTextArea.setLineWrap(true);
    asciiTextArea.setBackground(Color.BLACK);
    asciiTextArea.setForeground(Color.WHITE);
    asciiTextArea.setDisabledTextColor(Color.WHITE);
    asciiTextArea.setEnabled(false);
    asciiTextArea.setEditable(false);
    asciiTextArea.setCaretColor(Color.GRAY);
    asciiTextArea.setFont(BASE_FONT);
    firstRow.add(asciiTextArea);
    asciiTextArea.setText(Utils.getTextArea(offset, getViewSize(), fileBytes, hexTable));
    resultsWindow = new JFrame(rb.getString(KeyConstants.KEY_SEARCH_RESULT_TITLE));
    resultsWindow.setLayout(new FlowLayout());
    searchResults = new JList<>(new TableSearchResult[0]);
    searchResults.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    searchResults.setLayoutOrientation(JList.VERTICAL);
    searchResults.setVisibleRowCount(8);
    searchResults.setFont(new Font(Font.MONOSPACED, Font.PLAIN, SEARCHRES_FONT_SIZE));
    JScrollPane listScroller = new JScrollPane(searchResults);
    listScroller.setPreferredSize(SEARCH_RES_DIMENSION);
    resultsWindow.add(listScroller);
    resultsWindow.pack();
    resultsWindow.setResizable(Boolean.FALSE);
    createNewPrjWin();
    vsb = new JScrollBar();
    firstRow.add(vsb);
    JLabel offsetLabel = new JLabel(rb.getString(KeyConstants.KEY_OFFSET_LABEL));
    secondRow.add(offsetLabel);
    offsetLabelValue = new JTextField(getOffsetLabelValue(), OFFSET_LABEL_LENGTH);
    offsetLabelValue.setEnabled(false);
    secondRow.add(offsetLabelValue);
    offsetLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
    offsetLabelValue.setAlignmentX(Component.LEFT_ALIGNMENT);
    offEntries = new ArrayList<>();
    currEntry = new OffsetEntry();
    currEntry.setStart(-1);
    currEntry.setEnd(-1);
    createSearchAllWin();
    setResizable(Boolean.FALSE);
    addAllListeners();
    pack();
    vsb.setPreferredSize(new Dimension((int) vsb.getPreferredSize().getWidth(), (int) (getSize().getHeight() * 0.85)));
    refreshViewMode();
    setIcons();
    setVisible(true);
    refreshAll();
}
Also used : TableSearchResult(com.wave.hextractor.pojo.TableSearchResult) OffsetEntry(com.wave.hextractor.pojo.OffsetEntry)

Example 2 with TableSearchResult

use of com.wave.hextractor.pojo.TableSearchResult in project hextractor by sewave.

the class HexViewer method addAllListeners.

/**
 * Adds all the program listeners.
 */
private void addAllListeners() {
    addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent we) {
            closeApp();
        }
    });
    asciiTextArea.addCaretListener(e -> refreshSelection());
    asciiTextArea.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()) {
                doPop(e);
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                doPop(e);
            }
        }

        private void doPop(MouseEvent e) {
            asciiTextArea.setCaretPosition(e.getX() / 8 + e.getY() / 18 * visibleColumns);
            PopUpOffsetEntry menu = new PopUpOffsetEntry(getCaretEntry());
            menu.show(e.getComponent(), e.getX(), e.getY());
        }
    });
    /* (non-Javadoc)
         * @see java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event.AdjustmentEvent)
         */
    vsb.addAdjustmentListener(evt -> {
        Adjustable source = evt.getAdjustable();
        if (evt.getValueIsAdjusting()) {
            return;
        }
        int orient = source.getOrientation();
        if (orient == Adjustable.VERTICAL) {
            int type = evt.getAdjustmentType();
            int value = evt.getValue();
            switch(type) {
                case AdjustmentEvent.UNIT_INCREMENT:
                    offset += visibleColumns;
                    break;
                case AdjustmentEvent.UNIT_DECREMENT:
                    offset -= visibleColumns;
                    break;
                case AdjustmentEvent.BLOCK_INCREMENT:
                    offset += getOffsetBlock();
                    break;
                case AdjustmentEvent.BLOCK_DECREMENT:
                    offset -= getOffsetBlock();
                    break;
                case AdjustmentEvent.TRACK:
                    offset = value;
                    break;
                default:
                    break;
            }
            asciiTextArea.setText(Utils.getTextArea(offset, getViewSize(), fileBytes, hexTable));
            hexTextArea.setText(Utils.getHexAreaFixedWidth(offset, getViewSize(), fileBytes, visibleColumns));
            asciiTextArea.setCaretPosition(asciiTextArea.getText().length());
        }
    });
    vsb.getModel().addChangeListener(e -> {
        offset = vsb.getValue();
        refreshAll();
    });
    // Drop on new project, batch creation
    new FileDrop(newPrjWin, files -> {
        if (files != null) {
            if (files.length > 1) {
                newPrjWin.setCursor(new Cursor(Cursor.WAIT_CURSOR));
                disableProjectWindow();
                newPrjWin.repaint();
                for (File file : files) {
                    try {
                        ProjectUtils.createProject(file);
                    } catch (Exception e) {
                        Utils.logException(e);
                    }
                }
                newPrjWin.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                newPrjWin.setVisible(false);
                JOptionPane.showMessageDialog(asciiTextArea.getParent(), rb.getString(KeyConstants.KEY_NEW_PRJ_GENERATING_MSG), rb.getString(KeyConstants.KEY_NEW_PRJ_GENERATING_MSG), JOptionPane.INFORMATION_MESSAGE);
            } else {
                for (File file : files) {
                    newPrjWinFileInput.setText(file.getName());
                    projectFile = file;
                    selectProjectFileType(projectFile);
                    newPrjWinNameInput.setText(ProjectUtils.getProjectName(projectFile.getName()));
                }
            }
        }
    });
    newPrjWinFileInput.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void changedUpdate(DocumentEvent e) {
            cleanFile();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            cleanFile();
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            cleanFile();
        }

        void cleanFile() {
            projectFile = null;
        }
    });
    searchResults.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent evt) {
            @SuppressWarnings("unchecked") JList<TableSearchResult> list = (JList<TableSearchResult>) evt.getSource();
            TableSearchResult tsr = list.getSelectedValue();
            if (evt.getClickCount() == 1 && tsr != null) {
                if (SwingUtilities.isLeftMouseButton(evt)) {
                    offset = tsr.getOffset();
                    asciiTextArea.setCaretPosition(0);
                    hexTextArea.setCaretPosition(0);
                    refreshAll();
                }
                if (SwingUtilities.isRightMouseButton(evt)) {
                    int selectedOption = JOptionPane.showConfirmDialog(null, rb.getString(KeyConstants.KEY_SEARCH_RESULT_TABLE), rb.getString(KeyConstants.KEY_SEARCH_RESULT_TABLE_TITLE), JOptionPane.YES_NO_OPTION);
                    if (selectedOption == JOptionPane.YES_OPTION) {
                        offset = tsr.getOffset();
                        hexTable = tsr.getHexTable();
                        refreshAll();
                    }
                }
            }
        }
    });
    new FileDrop(this, files -> {
        if (files != null && files.length > 0) {
            requestFocus();
            requestFocusInWindow();
            for (File file : files) {
                if (file.getAbsolutePath().endsWith(EXTENSION_TABLE)) {
                    reloadTableFile(file);
                } else {
                    if (file.getAbsolutePath().endsWith(EXTENSION_OFFSET)) {
                        reloadOffsetsFile(file);
                    } else {
                        if (file.getAbsolutePath().endsWith(EXTENSION_EXTRACTION)) {
                            reloadExtAsOffsetsFile(file);
                        } else {
                            reloadHexFile(file);
                        }
                    }
                }
            }
        }
    });
    addMouseWheelListener(e -> {
        if (e.getWheelRotation() < 0) {
            if (offset > visibleColumns) {
                offset -= visibleColumns;
            }
        } else {
            if (offset < fileBytes.length - getViewSize()) {
                offset += visibleColumns;
            }
        }
        refreshAll();
    });
    addListenerForKeys();
}
Also used : DocumentListener(javax.swing.event.DocumentListener) TableSearchResult(com.wave.hextractor.pojo.TableSearchResult) DocumentEvent(javax.swing.event.DocumentEvent) IOException(java.io.IOException) BadLocationException(javax.swing.text.BadLocationException) FileDrop(com.wave.hextractor.object.FileDrop) File(java.io.File)

Example 3 with TableSearchResult

use of com.wave.hextractor.pojo.TableSearchResult in project hextractor by sewave.

the class FileUtils method searchRelative8Bits.

/**
 * Searches tables that meet the letter correlation for the target phrase.
 *
 * @param fileBytes the file bytes
 * @param searchString the search string
 * @return list of tables.
 */
private static List<TableSearchResult> searchRelative8Bits(byte[] fileBytes, String searchString) {
    List<TableSearchResult> res = new ArrayList<>();
    int wordLength = searchString.length();
    if (wordLength < Constants.MIN_SEARCH_WORD_LENGTH) {
        throw new IllegalArgumentException("Minimal word length / Longitud minima de palabra : " + Constants.MIN_SEARCH_WORD_LENGTH);
    }
    byte[] searchBytes = searchString.getBytes(StandardCharsets.US_ASCII);
    int i = 0;
    while (i < fileBytes.length - wordLength) {
        int displacement = searchBytes[0] - fileBytes[i] & Constants.MASK_8BIT;
        if (equivalentChars(displacement, searchBytes, Arrays.copyOfRange(fileBytes, i, i + wordLength))) {
            TableSearchResult tr = new TableSearchResult();
            HexTable ht = new HexTable(displacement);
            tr.setHexTable(ht);
            tr.setOffset(i);
            tr.setWord(searchString);
            if (!res.contains(tr)) {
                res.add(tr);
            }
            i += wordLength - 1;
        }
        if (res.size() > 999) {
            break;
        }
        i++;
    }
    return res;
}
Also used : TableSearchResult(com.wave.hextractor.pojo.TableSearchResult) HexTable(com.wave.hextractor.object.HexTable)

Example 4 with TableSearchResult

use of com.wave.hextractor.pojo.TableSearchResult in project hextractor by sewave.

the class FileUtils method searchRelative8Bits.

/**
 * Generates a table file for the input string if found on the rom.
 *
 * @param firstFile .
 * @param outFilePrefix .
 * @param searchString .
 * @throws IOException the exception
 */
public static void searchRelative8Bits(String firstFile, String outFilePrefix, String searchString) throws IOException {
    Utils.log("Searching relative string \"" + searchString + "\"\n in \"" + firstFile + "\" " + "\n Generating table files from  \"" + outFilePrefix + ".001\"");
    List<TableSearchResult> hexTables = searchRelative8Bits(Files.readAllBytes(Paths.get(firstFile)), searchString);
    int tablesFound = 1;
    List<HexTable> usedTables = new ArrayList<>();
    for (TableSearchResult t : hexTables) {
        if (!usedTables.contains(t.getHexTable())) {
            writeFileAscii(outFilePrefix + "." + Utils.fillLeft(valueOf(tablesFound), 3), t.getHexTable().toAsciiTable());
            usedTables.add(t.getHexTable());
        }
        tablesFound++;
    }
}
Also used : TableSearchResult(com.wave.hextractor.pojo.TableSearchResult) HexTable(com.wave.hextractor.object.HexTable)

Example 5 with TableSearchResult

use of com.wave.hextractor.pojo.TableSearchResult in project hextractor by sewave.

the class FileUtils method toTableResults.

/**
 * To table results.
 * @return the list
 */
private static List<TableSearchResult> toTableResults(HexTable hexTable, String searchString, List<Integer> list) {
    List<TableSearchResult> searchRes = new ArrayList<>();
    for (Integer res : list) {
        TableSearchResult tsr = new TableSearchResult();
        tsr.setHexTable(hexTable);
        tsr.setOffset(res);
        tsr.setWord(searchString);
        searchRes.add(tsr);
    }
    return searchRes;
}
Also used : TableSearchResult(com.wave.hextractor.pojo.TableSearchResult)

Aggregations

TableSearchResult (com.wave.hextractor.pojo.TableSearchResult)5 HexTable (com.wave.hextractor.object.HexTable)2 FileDrop (com.wave.hextractor.object.FileDrop)1 OffsetEntry (com.wave.hextractor.pojo.OffsetEntry)1 File (java.io.File)1 IOException (java.io.IOException)1 DocumentEvent (javax.swing.event.DocumentEvent)1 DocumentListener (javax.swing.event.DocumentListener)1 BadLocationException (javax.swing.text.BadLocationException)1