Open the OLD file and the NEW file in binary mode.
Read the OLD file filling a dynamically created buffer to hold it.
Compare the size of OLD file and NEW file
You need to know this because if the NEW file is shorter or longer this will have an impact on the "patched" file (especially if it is an .exe)
Open a new DATA file in binary mode
Read in a buffer from the NEW file
Compare byte for byte NEW to OLD
ok here's where we need some code... After some trial and error (doesn't everyone code like that) I created a struct to hold my offset and the bytes that were different, here's the struct:
static int BLOCKSIZE = 18;typedef struct tagPatchBlock{ DWORD offset; // how far into the file to write new data. WORD count; // the number of values that are to be written @ offset. BYTE values[BLOCKSIZE]; // the values to write.} sPatchBlock
ok now:
When a difference is detected store the offset in the sPatchBlock struct
If you fill up the values (18 in this case) you save that block to your DATA file like so:
fwrite(&fileblock,sizeof(sPatchBlock),1,PatchFile);
Also if the delta of the next offset is more than one you will save off that fileblock and create a new one.
At first glance it may look like your wasting space in the DATA file... and you would be correct. The solution is to compress the DATA file using zlib or another compression alogo.
The only thing left is a header in the DATA file and creating a new file is easy...
If you have any questions feel free to email me - I'll gladly send you some of my test code.
Dave "Dak Lozar" Loeser †
[edited the fixed text]
[edited by - Dak Lozar on June 2, 2002 11:38:37 AM]