
                           Appending Files to EXEs.
                           
    After seeing several questions on appending files to EXEs, I decided to 
write this text.  I did NOT originate this idea.  While this text describes 
"a" way of implementing the technique it may not be the best way for your 
needs.  I have simply attempted to supply you with a basic understanding of 
the process.


                                  WHY?
    
    A couple years ago, I purchased a copy of Ultima 7.  After installing it 
I looked at the directory.  There were a lot of files and moving any one of  
them out of the directory crashed the program.  When I got Unreal by Future 
Crew all of my preconceived ideas went out the window.

      1.  You can't run a 2meg EXE, can you?!
      2.  Where are the music and graphic files?!
      3.  How'd they do that? (This includes the effects :)

The answer to #1 : "It runs; therefore, you must be able to do it. Idiot!"
The answer to #2 : "All the music and graphic files are contained IN the EXE."
Question #3 is a little harder to explain, I still don't know exactly what FC 
did, but the technique I discuss in this file gives you similar results.



                            Appending a file

    Before you append a file to the end of your EXE, ask yourself how do 
access it.  If your adding 10 files how do you know where they are?  This is 
actually really simple once you think it through.  Create a directory 
structure of your own and make it the very last file you append! Use your own 
structure if you want but feel free to use mine.

Directory structure:

       repeat
           name - string
           filepos - long int, pointer to the first byte of the file
           filesize - long int
       for each file being attach

       long int - number of entries



Since this is similar to a WAD file, we'll call is a KAD file. 
KAD = Kodiak Wad file, get it a KAD file. 
Okay, so it wasn't that good, lets move on.

To build the KAD file all you have to do is tack one file after another INTO 
a single file and add the directory to the end of it.


see packer.c

       Open output file
       repeat
           save the output's file position in directory structure
           save the input's file name, ignoring path,  in directory structure
           save the input's file size in directory structure
           open input file
           copy input file to output file
           close input file
       until all files are appended
       save directory info
       close file

Simple, ehh?

Now that you have the KAD file what do you do with it?

To access the KAD your code should read the directory into a memory array. 
Just read the last dword of the KAD multiply by 8 (2 dwords) add 4, and seek 
from the end of the file back that many bytes and fill your directory array 
from there.

Now if you want to load the first file from the KAD, get the file offset from 
your directory array, seek to the file position and load. What could be 
simpler?  How about using a pre-written function GETFILE. :)

While you are developing your program use the KAD file.  Once your code is 
done your ready for the final step. Instead of reading from the KAD file, 
change the input name your program is looking for, to itself. Then repack the 
files to the end of your EXE.

see packer.c

       Open EXE file
       seek the end of the EXE file
       repeat
           save the output's file position in directory structure
           save the input's file type in directory structure
           save the input's file size in directory structure
           open input file
           copy input file to output file
           close input file
       until all files are appended
       save directory info
       close file

That's it! YOUR DONE!


    Keep in mind that this is NOT the only way to accomplish this.  I have 
included a fully functional KAD system implemented for Watcom C.  It includes 
LZARI decompression routine.  If this file has helped you, let me know.  Feel 
free to use the code included, but if you do greet me. A postcard would be 
nice too. :)


NOTE: I have heard it said "you can't do this when using an EXE compression 
loader like Pklite." I have one thing to say......BULL!   The trick is to 
compress your EXE prior to appending the KAD to it.



Coded by Kodiak of The Apollo Project                                    
AKA Charles Jones                                                        
    1122 s 32nd St #2                                                    
    Omaha, NE 68105                                                      
    (402)-346-8974                                                       
                                                                         
Email: CAD@UnOmaha.edu                                                   
IRC  : #Coders (lo *, Bri_acid: I still want to be on OPPER's list)                                                           

