Created
November 12, 2018 21:17
-
-
Save nfalliere/4c7079335fb1c7e169eb40dfda3e27e9 to your computer and use it in GitHub Desktop.
Sample code showing how to reparse unit's binary data as another format
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
// sample code, needs adjusting/customizing (see TODO tags) | |
// call reparse() with input information set in a ReparseInformation object | |
public class ReparseInformation { | |
IUnit unit; | |
String subUnitName; | |
String wantedType; | |
long offset; | |
long size; | |
} | |
// return the reparsed unit (sub-unit) or null on error | |
public IUnit reparse(ReparseInformation info) | |
IUnit unit = info.unit; | |
IInput subinput = null; | |
if(unit instanceof IBinaryUnit) { | |
IInput input = ((IBinaryUnit)unit).getInput(); | |
long offset = info.getOffset(); | |
long size = info.getSize(); | |
long maxsize = input.getCurrentSize(); | |
if((offset + size) > maxsize) { | |
// sanitize size - or we could bail, because sanitizing was done by ReparseDialog again | |
size = maxsize - offset; | |
} | |
if(offset >= 0 && size >= 0 && offset <= maxsize) { | |
try { | |
subinput = new SubInput(input, offset, size); | |
} | |
catch(IOException e) { | |
logger.catching(e); | |
return null; | |
} | |
} | |
} | |
IUnitProcessor processor = unit.getUnitProcessor(); | |
IUnitIdentifier ident = processor.getUnitIdentifier(info.getWantedType()); | |
if(ident == null) { | |
return null; | |
} | |
boolean successfullyIdentified = ident.canIdentify(subinput, unit); | |
if(!successfullyIdentified) { | |
// TODO: data was not identified as what it should be; can decide to bail or proceed | |
// return null; | |
} | |
IUnit subunit = null; | |
try { | |
subunit = ident.prepare(info.getSubUnitName(), subinput, processor, unit); | |
} | |
catch(Exception e) { | |
// do not throw if the unit was not identified - we knew failure was near | |
if(!successfullyIdentified) { | |
logger.catching(e); | |
} | |
else { | |
throw e; | |
} | |
} | |
if(subunit == null) { | |
logger.error("Failed to reparse"); | |
return null; | |
} | |
if(subunit instanceof INativeCodeUnit) { | |
// TODO: may decide to provide additional info. for native code units, eg, a specific image base or endianness info for headless binary blobs | |
//INativeCodeUnit<?> pbcu = (INativeCodeUnit<?>)subunit; | |
//pbcu.setVirtualImageBase(...); | |
//pbcu.getProcessor().setEndianness(...); | |
} | |
unit.addChild(subunit); | |
logger.info("A reparsed sub-unit was created"); | |
return subunit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment