Recently, I wanted to create an "intelligent" binary patcher, which not
only replaces some chunk of binary data at a file's predefined offset,
but instead performs search and replace. BinaryPatcher.java is the result.
The usage is pretty straight forward. Let's have a look at an basic example:
class Tester {
public static void main(String[] args) {
try {
String filename = "patchme";
BinaryPatcher p = new BinaryPatcher(filename, "AB00FF14", "AB11FF14");
if (p.isPatchable()) {
p.createBackup(filename + ".org");
p.patch();
}
} catch(IOException e) {
System.out.println("ERROR: " + e.getMessage());
}
}
}We try to patch the file patchme. The search and replace binary data is given by hex sequences, in this example AB00FF14 is replaced by AB11FF14. If the file is patchable, i.e. it contains the search pattern, a backup named patchme.org is created an finally, the original file is modified.
That's all. Pretty simple but useful.