Created
October 27, 2016 12:46
-
-
Save warriordog/c6d25bf8f6a55fe0cc82acb7d2a9edaa to your computer and use it in GitHub Desktop.
Extractor for a custom archvie format hidden in a firmware blob
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 java.io.*; | |
public class Extractor { | |
public static void main(String[] args) { | |
if (args.length != 2) { | |
System.out.println("Specify a file and output dir!"); | |
} else { | |
File in = new File(args[0]); | |
File out = new File(args[1]); | |
extract(in, out); | |
} | |
} | |
private static void extract(File inFile, File out) { | |
try { | |
DataInputStream in = new DataInputStream(new FileInputStream(inFile)); | |
byte[] filenameBytes = new byte[60]; | |
while (in.available() > 0) { | |
int nameLen = in.read(filenameBytes); | |
char[] chars = new char[nameLen]; | |
for (int i = 0; i < nameLen; i++) { | |
if (filenameBytes[i] == 0) { | |
break; | |
} | |
chars[i] = (char)filenameBytes[i]; | |
} | |
String name = new String(chars).trim(); | |
int length = in.readInt(); | |
extractFile(out, name, length, in); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void extractFile(File outDir, String name, int length, DataInputStream in) throws IOException { | |
File outFile = new File(outDir, name); | |
makeDirs(outFile); | |
System.out.println("Extracting " + name + " to " + outFile.getPath()); | |
OutputStream out = new FileOutputStream(outFile); | |
byte[] buff = new byte[length]; | |
int read = in.read(buff); | |
if (read != length) { | |
System.err.println("Number of read bytes did not match header!"); | |
} | |
out.write(buff); | |
out.close(); | |
} | |
private static void makeDirs(File file) { | |
String path = file.getPath(); | |
int idx = path.lastIndexOf('/'); | |
if (idx != -1) { | |
File dir = new File(path.substring(0, idx + 1)); | |
dir.mkdirs(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment