Last active
August 31, 2017 01:20
-
-
Save Lucina/9df71488b8b32e094125e91608adf0a7 to your computer and use it in GitHub Desktop.
Pack/unpack matching test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.arxcin.RnRBinAPI; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.ArrayList; | |
import java.nio.ByteBuffer; | |
import java.nio.ByteOrder; | |
public class RnRBinTest{ | |
//https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java | |
private final static char[] hexArray = "0123456789ABCDEF".toCharArray(); | |
public static void main(String[] args) throws Exception{//FORGIVE ME | |
if(args.length != 2){ | |
System.err.println("Need 2 args!\n[baseDir] [targetFile]"); | |
System.exit(1); | |
} | |
File dir = new File(args[0]); | |
if(!dir.exists()){ | |
System.err.println("Error: path " + dir + " does not exist!"); | |
System.exit(2); | |
}else if(!dir.isDirectory()){ | |
System.err.println("Error: path " + dir + " is not a directory!"); | |
System.exit(3); | |
} | |
File[] files = dir.listFiles(); | |
File outFile = new File(args[1]); | |
outFile.getAbsoluteFile().getParentFile().mkdirs(); | |
try{ | |
outFile.createNewFile(); | |
}catch(IOException e){ | |
System.err.println("Error creating new file at path \"" + outFile + "\" Details:\n" + e); | |
System.exit(4); | |
} | |
PrintWriter pw = null; | |
try{ | |
pw = new PrintWriter(outFile); | |
}catch(FileNotFoundException e){ | |
System.err.println("Hail hydra."); | |
System.exit(1131); | |
} | |
for(int j = 0; j < files.length; j++){ | |
if(files[j].isFile()){ | |
FileInputStream stream = null; | |
byte[] data = new byte[(int)files[j].length()]; | |
try{ | |
stream = new FileInputStream(files[j]); | |
}catch(FileNotFoundException e){ | |
System.err.println("Error: File \"" + files[j] + "\" does not exist... Impossible... Details:\n" + e); | |
System.exit(5); | |
} | |
try{ | |
stream.read(data); | |
}catch(IOException e){ | |
System.err.println("Error: IOException while reading file \"" + files[j] + "\" Details:\n" + e); | |
System.exit(6); | |
} | |
try{ | |
stream.close(); | |
}catch(IOException e){ | |
System.err.println("Error: IOException while trying to close FileInputStream! Details:\n" + e); | |
System.exit(7); | |
} | |
List<byte[]> unpacked = RnRBinAPI.unpack(data); | |
byte[] packed = RnRBinAPI.pack(unpacked); | |
if(!Arrays.equals(data, packed)){ | |
int countWrong = 0; | |
for(int i = 0; i < packed.length; i++){ | |
if(data[i] != packed[i]) | |
countWrong++; | |
} | |
String dang = "[!!] FILE>>" + files[j].getName() + " wrong: " + countWrong; | |
System.out.println(dang); | |
pw.println(dang); | |
for(int i = 0; i < packed.length; i++){ | |
if(data[i] != packed[i]){ | |
byte[] d1 = new byte[1]; | |
d1[0] = data[i]; | |
byte[] d2 = new byte[1]; | |
d2[0] = packed[i]; | |
String d3 = "DiffPos (base 10): " + i + ", vals: " + bytesToHex(d1) + ":" + bytesToHex(d2); | |
System.out.println(d3); | |
pw.println(d3); | |
} | |
} | |
}else{ | |
String chill = "[OK] FILE>>" + files[j].getName(); | |
System.out.println(chill); | |
pw.println(chill); | |
} | |
} | |
} | |
pw.flush(); | |
pw.close(); | |
} | |
//https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java | |
public static String bytesToHex(byte[] bytes) { | |
char[] hexChars = new char[bytes.length * 2]; | |
for ( int j = 0; j < bytes.length; j++ ) { | |
int v = bytes[j] & 0xFF; | |
hexChars[j * 2] = hexArray[v >>> 4]; | |
hexChars[j * 2 + 1] = hexArray[v & 0x0F]; | |
} | |
return new String(hexChars); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment