Extracting a file embedded in another binary file.
Posted on 2003-11-17
I'm somewhat of a C++ beginner although I have quite a lot of experience in other C++ type languages.
Anyway, I'm using VC++ 6 at the moment and I'm trying to write a very small console application, non MFC
to perform a little mundane task.
I have a binary file, some 500Mb in size which contains a number of different file types all concantenated together.
Apparently this file onces had a corresponding index file but thats been lost and I need to get certain files out from
within this larger one.
So far I've written my very basic program and have written a function that will extract part of the larger file
to any other file if I supply the destination filename, offset in the source file and its length in bytes.
However, now comes the problem - and I've tried searching the web using various keywords but haven't gotten
anything close to what I want to do.
The files I want to pull out are standard TGA image files. The method I figured which is best is to search the
source file a TGA header and footer, derive the offset and length from that and extract.
Now, from looking at the file with a HEX editor I know the following - the TGA files start with the HEX string
"0000 2000 0000 0000" and end with the string "TRUEVISION-XFILE" followed by 0x00. However, the same
HEX string that denotes a TGA header often occurs in the source file in other instances.
My idea was thus.
- Search the source file for the TGA "footer" and and work out at what offset the file ends
- From this point, search *backwards* through the source file until I hit the TGA header.
- Note the position of the header and from the derived length, copy out the binary data to a new file.
Now, the problem is, ahem, how on *earth* can I do this in C++? Bearing in mind that there are 0 - n
TGA files inside this large binary.
I know about using fseek to move to and offset but I really am stumped on how to go about this!
As I said searching the net for 2 days hasn't unearthed any source doing something similar or
any clues on how to go about it.
Can anyone shed some light or give me some examples?