Merge pull request #381 from ThexXTURBOXx/master

Better binary file detection
This commit is contained in:
Konloch 2022-02-03 11:21:45 -08:00 committed by GitHub
commit 0e83cf46f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 10 deletions

View file

@ -32,7 +32,7 @@
<darklaf-extensions-rsta.version>0.3.4</darklaf-extensions-rsta.version>
<decompiler-fernflower.version>5.2.1.Final</decompiler-fernflower.version>
<dex2jar.version>v42</dex2jar.version>
<fernflower.version>eda981d</fernflower.version>
<fernflower.version>5f33b55</fernflower.version>
<gson.version>2.8.9</gson.version>
<guava.version>31.0.1-jre</guava.version>
<imgscalr-lib.version>4.2</imgscalr-lib.version>
@ -48,7 +48,7 @@
<procyon-snapshot.version>10b32a4</procyon-snapshot.version>
<rsyntaxtextarea.version>3.1.6</rsyntaxtextarea.version>
<semantic-version.version>2.1.1</semantic-version.version>
<slf4j.version>1.7.32</slf4j.version>
<slf4j.version>1.7.35</slf4j.version>
<smali.version>2.5.2</smali.version>
<snakeyaml.version>1.30</snakeyaml.version>
<treelayout.version>1.0.3</treelayout.version>
@ -401,7 +401,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.9.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>

View file

@ -73,13 +73,12 @@ public class FileViewer extends ResourceViewer
final byte[] contents = resource.getResourceBytes();
final String nameLowerCase = this.resource.name.toLowerCase();
final String onlyName = FilenameUtils.getName(nameLowerCase);
final String contentsAsString = new String(contents);
final boolean hexViewerOnly = BytecodeViewer.viewer.viewPane1.getSelectedDecompiler() == Decompiler.HEXCODE_VIEWER &&
BytecodeViewer.viewer.viewPane2.getSelectedDecompiler() == Decompiler.NONE &&
BytecodeViewer.viewer.viewPane3.getSelectedDecompiler() == Decompiler.NONE;
//image viewer
if (!MiscUtils.isPureAscii(contentsAsString) || hexViewerOnly)
if (MiscUtils.guessIfBinary(contents) || hexViewerOnly)
{
//TODO:
// + Add file header checks
@ -144,7 +143,7 @@ public class FileViewer extends ResourceViewer
textArea.setCodeFoldingEnabled(true);
SyntaxLanguage.setLanguage(textArea, nameLowerCase);
textArea.setText(contentsAsString);
textArea.setText(new String(contents));
textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, (int) BytecodeViewer.viewer.fontSpinner.getValue()));
textArea.setCaretPosition(0);

View file

@ -54,7 +54,6 @@ import static the.bytecode.club.bytecodeviewer.BytecodeViewer.gson;
public class MiscUtils
{
private static final CharsetEncoder asciiEncoder = StandardCharsets.US_ASCII.newEncoder(); // or "ISO-8859-1" for ISO Latin 1
private static final String AB = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private static final String AN = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
private static final Random rnd = new Random();
@ -247,9 +246,19 @@ public class MiscUtils
while(list.size() > maxLength)
list.remove(list.size() - 1);
}
public static boolean isPureAscii(String v) {
return asciiEncoder.canEncode(v);
/**
* Returns whether the bytes most likely represent binary data.
* Based on https://stackoverflow.com/a/13533390/5894824
*/
public static boolean guessIfBinary(byte[] data) {
double ascii = 0;
double other = 0;
for (byte b : data) {
if (b == 0x09 || b == 0x0A || b == 0x0C || b == 0x0D || (b >= 0x20 && b <= 0x7E)) ascii++;
else other++;
}
return other != 0 && other / (ascii + other) > 0.25;
}
public static Language guessLanguage()