use of com.wave.hextractor.pojo.OffsetEntry in project hextractor by sewave.
the class HexTable method getAllEntries.
/**
* Get all entries from the file.
*
* @param secondFileBytes the second file bytes
* @param numMinChars the num min chars
* @param numIgnoredChars the num ignored chars
* @param endCharsList the end chars list
* @param dictFile the dict file
* @return the all entries
* @throws IOException the exception
*/
public String getAllEntries(byte[] secondFileBytes, int numMinChars, int numIgnoredChars, List<String> endCharsList, String dictFile) throws IOException {
searchPercent = 0;
List<OffsetEntry> offsetEntryList = new ArrayList<>();
HashSet<String> dict = new HashSet<>(Arrays.asList(FileUtils.getAsciiFile(dictFile).split(Constants.S_NEWLINE)));
int entryStart = 0;
boolean validString = false;
StringBuilder word = new StringBuilder();
StringBuilder sentence = new StringBuilder();
String dataChar;
List<String> skippedChars = new ArrayList<>();
ENTRIES_STATUS status = ENTRIES_STATUS.SEARCHING_START_OF_STRING;
long lastTime = System.currentTimeMillis();
for (int i = 0; i < secondFileBytes.length - numMinChars && !Thread.currentThread().isInterrupted(); i++) {
searchPercent = i * 100f / secondFileBytes.length;
if (System.currentTimeMillis() - lastTime > 1000) {
lastTime = System.currentTimeMillis();
Utils.log(searchPercent + "% completed.");
}
Byte readedByteObj = secondFileBytes[i];
String dataCharHex = String.format(Constants.HEX_16_FORMAT, readedByteObj);
dataChar = table.getOrDefault(readedByteObj, null);
switch(status) {
case SEARCHING_START_OF_STRING:
if (dataChar != null) {
entryStart = i;
word.setLength(0);
sentence.setLength(0);
sentence.append(dataChar);
word.append(dataChar);
validString = false;
status = ENTRIES_STATUS.SEARCHING_END_OF_STRING;
}
break;
case SEARCHING_END_OF_STRING:
if (dataChar != null) {
sentence.append(dataChar);
word.append(dataChar);
} else {
if (Utils.getCleanedString(word.toString()).length() > 1) {
if (!validString) {
validString = Utils.stringHasWords(dict, word.toString());
}
sentence.append(Constants.SPACE_STR);
word.append(Constants.SPACE_STR);
skippedChars.clear();
skippedChars.add(dataCharHex);
status = ENTRIES_STATUS.SKIPPING_CHARS;
} else {
if (validString) {
offsetEntryList.add(new OffsetEntry(entryStart, i, endCharsList));
}
entryStart = 0;
status = ENTRIES_STATUS.SEARCHING_START_OF_STRING;
}
}
break;
case SKIPPING_CHARS:
if (dataChar != null) {
word.setLength(0);
sentence.append(dataChar);
word.append(dataChar);
status = ENTRIES_STATUS.SEARCHING_END_OF_STRING;
} else {
skippedChars.add(dataCharHex);
boolean skippedAreEndings = endCharsList.stream().anyMatch(skippedChars::contains);
if (skippedChars.size() > numIgnoredChars) {
if (sentence.length() > numMinChars) {
if (Utils.stringHasWords(dict, word.toString()) || validString && skippedAreEndings) {
offsetEntryList.add(new OffsetEntry(entryStart, i, endCharsList));
} else {
if (validString) {
offsetEntryList.add(new OffsetEntry(entryStart, i, endCharsList));
}
}
}
entryStart = 0;
status = ENTRIES_STATUS.SEARCHING_START_OF_STRING;
}
}
break;
default:
break;
}
}
if (entryStart > 0) {
offsetEntryList.add(new OffsetEntry(entryStart, secondFileBytes.length - 1, endCharsList));
}
word.setLength(0);
for (OffsetEntry oe : offsetEntryList) {
word.append(oe.toEntryString()).append(Constants.OFFSET_STR_SEPARATOR);
}
return word.toString();
}
use of com.wave.hextractor.pojo.OffsetEntry in project hextractor by sewave.
the class FileUtils method cleanExtractedFile.
/**
* Extracts all the offsets of a given extraction file, useful after cleaning invalid entries of
* search all strings.
* @param extractFile file to search.
* @throws IOException io error.
*/
public static String cleanExtractedFile(String extractFile) throws IOException {
Utils.log("Getting offsets from \"" + extractFile);
StringBuilder fileArgs = new StringBuilder();
String[] lines = getAsciiFile(extractFile).split(Constants.S_NEWLINE);
for (String line : lines) {
line = line.trim();
if (line.startsWith(Constants.ADDR_STR)) {
fileArgs.append(line.substring(Constants.ADDR_STR.length()));
fileArgs.append(Constants.OFFSET_STR_SEPARATOR);
}
}
List<OffsetEntry> entries = Utils.getOffsets(fileArgs.toString());
Collections.sort(entries);
fileArgs.setLength(0);
for (OffsetEntry entry : entries) {
if (fileArgs.length() > 0) {
fileArgs.append(Constants.OFFSET_STR_SEPARATOR);
}
fileArgs.append(entry.toEntryString());
}
return fileArgs.toString();
}
use of com.wave.hextractor.pojo.OffsetEntry in project hextractor by sewave.
the class FileUtils method insertAsciiAsHex.
/**
* Insert ascii as hex.
*
* @param firstFile the first file
* @param secondFile the second file
* @param thirdFile the third file
* @throws IOException the exception
*/
public static void insertAsciiAsHex(String firstFile, String secondFile, String thirdFile) throws IOException {
Utils.log("Inserting ascii file \"" + secondFile + "\"\n using table \"" + firstFile + FILES_SEPARATOR + thirdFile + "\".");
HexTable hexTable = new HexTable(firstFile);
String input = getAsciiFile(secondFile);
byte[] outFileBytes = Files.readAllBytes(Paths.get(thirdFile));
String[] lines = input.split(Constants.S_NEWLINE);
int totalBytesWritten = 0;
int line = 0;
while (line < lines.length) {
if (lines[line] != null && lines[line].contains(Constants.ADDR_STR)) {
// Read entry
OffsetEntry entry = new OffsetEntry(lines[line]);
line++;
// Read content (including end)
StringBuilder content = new StringBuilder();
// Put lines not starting with |
while (!lines[line].contains(Constants.S_MAX_BYTES)) {
if (lines[line] != null && lines[line].length() > 0 && !lines[line].contains(Constants.S_COMMENT_LINE)) {
content.append(lines[line]);
if (lines[line].contains(Constants.S_STR_NUM_CHARS)) {
content.append(Constants.S_NEWLINE);
}
}
line++;
}
// End line
content.append(lines[line]).append(Constants.S_NEWLINE);
// Process
byte[] hex = hexTable.toHex(content.toString(), entry);
if (Utils.isDebug()) {
Utils.log(" TO OFFSET: " + Utils.intToHexString(entry.getStart(), Constants.HEX_ADDR_SIZE));
}
System.arraycopy(hex, 0, outFileBytes, entry.getStart(), hex.length);
totalBytesWritten += hex.length;
}
line++;
}
Utils.log("TOTAL BYTES WRITTEN: " + Utils.fillLeft(valueOf(totalBytesWritten), Constants.HEX_ADDR_SIZE) + " / " + Utils.intToHexString(totalBytesWritten, Constants.HEX_ADDR_SIZE) + " Hex");
Files.write(Paths.get(thirdFile), outFileBytes);
}
use of com.wave.hextractor.pojo.OffsetEntry in project hextractor by sewave.
the class FileUtils method extractAsciiFile.
/**
* Extracts the ascii file.
*/
public static void extractAsciiFile(HexTable hexTable, byte[] fileBytes, String outFile, List<OffsetEntry> offsets, boolean showExtractions) throws IOException {
StringBuilder fileOut = new StringBuilder();
if (offsets != null && !offsets.isEmpty()) {
for (OffsetEntry entry : offsets) {
fileOut.append(hexTable.toAscii(fileBytes, entry, showExtractions));
}
}
writeFileAscii(outFile, fileOut.toString());
}
use of com.wave.hextractor.pojo.OffsetEntry in project hextractor by sewave.
the class HexTableTest method toHex1.
@Test
public void toHex1() {
HexTable table = new HexTable(LINES);
byte[] data = new byte[] { 0, 1, 2, 3, 4 };
OffsetEntry entry = new OffsetEntry(0, 4, Collections.singletonList("FF"));
Assert.assertArrayEquals(data, table.toHex("abc{ab}~04~#005\n|5\n", entry));
}
Aggregations