Search in sources :

Example 1 with HexTable

use of com.wave.hextractor.object.HexTable 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 2 with HexTable

use of com.wave.hextractor.object.HexTable in project hextractor by sewave.

the class FileUtils method insertHex4To3Data.

/**
 * Inserts ascii as hex from a 4 to 3 data.
 */
public static void insertHex4To3Data(String table, String inputFile, String outputFile) throws IOException {
    Utils.log("Inserting ascii as hex 4 to 3 from \"" + inputFile + FILES_SEPARATOR + outputFile + "\"\n using table: \"" + table + "\"");
    byte[] outFileBytes = Files.readAllBytes(Paths.get(outputFile));
    HexTable hexTable = new HexTable(table);
    for (String entry : Utils.removeCommentsAndJoin(getAsciiFile(inputFile))) {
        String[] entryDataAndOffset = entry.split(Constants.ADDR_STR);
        OffsetEntry offEntry = OffsetEntry.fromHexRange(entryDataAndOffset[1]);
        byte[] compData = Utils.getCompressed4To3Data(hexTable.toHex(entryDataAndOffset[0]));
        System.arraycopy(compData, 0, outFileBytes, offEntry.getStart(), compData.length);
    }
    Files.write(Paths.get(outputFile), outFileBytes);
}
Also used : OffsetEntry(com.wave.hextractor.pojo.OffsetEntry) HexTable(com.wave.hextractor.object.HexTable)

Example 3 with HexTable

use of com.wave.hextractor.object.HexTable in project hextractor by sewave.

the class FileUtils method extractAsciiFile.

/**
 * Extracts the ascii from secondFile using table firstFile to thirdFile.
 *
 * @param firstFile the first file
 * @param secondFile the second file
 * @param thirdFile the third file
 * @param offsetsArg the offsets arg
 * @throws IOException the exception
 */
public static void extractAsciiFile(String firstFile, String secondFile, String thirdFile, String offsetsArg) throws IOException {
    Utils.log("Extracting ascii file from \"" + secondFile + "\"\n using table \"" + firstFile + FILES_SEPARATOR + thirdFile + "\".");
    extractAsciiFile(new HexTable(firstFile), Files.readAllBytes(Paths.get(secondFile)), thirdFile, offsetsArg, true);
}
Also used : HexTable(com.wave.hextractor.object.HexTable)

Example 4 with HexTable

use of com.wave.hextractor.object.HexTable in project hextractor by sewave.

the class FileUtils method extractAscii3To4Data.

/**
 * Extracts Ascii data on packed 3 bytes to 4 characters</br>
 * using the entries.
 * @param inputFile .
 * @param outputFile .
 * @param entries .
 * @throws IOException .
 */
public static void extractAscii3To4Data(String table, String inputFile, String outputFile, String entries) throws IOException {
    Utils.log("Extracting ascii 3 to 4 from \"" + inputFile + FILES_SEPARATOR + outputFile + "\" \n using \"" + entries + "\" and table: \"" + table + "\"");
    StringBuilder dataString = new StringBuilder();
    byte[] inputFileBytes = Files.readAllBytes(Paths.get(inputFile));
    HexTable hexTable = new HexTable(table);
    for (OffsetEntry entry : Utils.getOffsets(entries)) {
        byte[] origData = Arrays.copyOfRange(inputFileBytes, entry.getStart(), entry.getEnd() + 1);
        byte[] expData = Utils.getExpanded3To4Data(origData);
        byte[] compData = Utils.getCompressed4To3Data(expData);
        if (Utils.isDebug()) {
            Utils.log("Original data     " + entry.getHexTarget() + " - " + Utils.getHexArea(0, origData.length, origData));
            Utils.log("Expanded data     " + entry.getHexTarget() + " - " + Utils.getHexArea(0, expData.length, expData));
            Utils.log("Recompressed data " + entry.getHexTarget() + " - " + Utils.getHexArea(0, compData.length, compData));
        }
        if (!Arrays.equals(origData, compData)) {
            Utils.log("ERROR! RECOMPRESSED DATA IS DIFFERENT!!!");
        }
        String line = hexTable.toAscii(expData, false, true);
        dataString.append(Constants.COMMENT_LINE);
        dataString.append(line);
        dataString.append(Constants.S_NEWLINE);
        dataString.append(line);
        dataString.append(entry.getHexTarget());
        dataString.append(Constants.S_NEWLINE);
    }
    writeFileAscii(outputFile, dataString.toString());
}
Also used : OffsetEntry(com.wave.hextractor.pojo.OffsetEntry) HexTable(com.wave.hextractor.object.HexTable)

Example 5 with HexTable

use of com.wave.hextractor.object.HexTable in project hextractor by sewave.

the class FileUtilsTest method separateCharLength.

@Test
public void separateCharLength() throws IOException {
    File tableFile = File.createTempFile("table", "insertAsciiAsHex.tbl");
    tableFile.deleteOnExit();
    HexTable table = new HexTable(0);
    FileUtils.writeFileAscii(tableFile.getAbsolutePath(), table.toAsciiTable());
    String asciiFile = "abcd~FF~";
    File asciiDataFile = File.createTempFile("origin", "insertAsciiAsHex.tst");
    asciiDataFile.deleteOnExit();
    FileUtils.writeFileAscii(asciiDataFile.getAbsolutePath(), asciiFile);
    File asciiFileOut = File.createTempFile("dest", "insertAsciiAsHex.tst");
    asciiFileOut.deleteOnExit();
    FileUtils.separateCharLength(asciiDataFile.getAbsolutePath(), tableFile.getAbsolutePath(), asciiFileOut.getAbsolutePath());
    Assert.assertEquals("\na\nbcd~FF~", FileUtils.getAsciiFile(asciiFileOut.getAbsolutePath()));
}
Also used : File(java.io.File) HexTable(com.wave.hextractor.object.HexTable) Test(org.junit.Test)

Aggregations

HexTable (com.wave.hextractor.object.HexTable)15 Test (org.junit.Test)5 File (java.io.File)4 OffsetEntry (com.wave.hextractor.pojo.OffsetEntry)3 TableSearchResult (com.wave.hextractor.pojo.TableSearchResult)2 IOException (java.io.IOException)1 BadLocationException (javax.swing.text.BadLocationException)1