I write a Portable Executable (PE) library that also provides finding the starting offset of the overlay (appended data to the PE that is not mapped into memory).
My algorithm finding the overlay offset looks like this so far:
public long getOverlayOffset() throws IOException {
if (offset == null) {
SectionTable table = data.getSectionTable();
offset = 0L;
for (SectionTableEntry section : table.getSectionEntries()) {
long pointerToRaw = section.get(POINTER_TO_RAW_DATA);
long sizeOfRaw = section.get(SIZE_OF_RAW_DATA);
long virtSize = section.get(VIRTUAL_SIZE);
//see https://code.google.com/p/corkami/wiki/PE#section_table: "if bigger than virtual size, then virtual size is taken. "
//and: "a section can have a null VirtualSize: in this case, only the SizeOfRawData is taken into consideration. "
if(virtSize != 0 && sizeOfRaw > virtSize) {
sizeOfRaw = virtSize;
}
long endPoint = pointerToRaw + sizeOfRaw;
if (offset < endPoint) {
offset = endPoint;
}
}
}
if(offset > file.length()) {
offset = file.length();
}
return offset;
}
I used corkami as a source to get to know some of the odds in calculating the overlay offset. I do not only want it to be robust, but also accurate. Did I miss something? What else do I have to put into consideration?
Note: There was a similar question here: How can one extract the appended data of a Portable Executable? But it doesn't cover a reliable algorithm so far. As I understand it, using a tool suffices in that question.