Dev/Spec/PackedFiles.odc

HostPackedFiles

DEFINITION HostPackedFiles;

	CONST magicnum = 4711;

	PROCEDURE IsInstalled;
	PROCEDURE RestoreFilesDir;
	PROCEDURE SetFilesDir;

END HostPackedFiles.


Module HostPackedFiles is an implementation of Files.Directory. The implementation is basically a wrapper around the HostFiles implementation with one major differnce: when a file is not present in the file system HostPackedFiles searches the current executable for files packed in there by DevPacker. 

Since HostPackedFiles wraps HostFiles it cannot work unless HostFiles is installed first. For HostPackedFiles to work properly it is also important that the exe-file has the right format. This format is explained below.

Whenever HostPackedFiles is loaded it trys to install itself as the Files.Directory. There are two preconditions for HostPackedFiles to be installed; first the current Files.Directory must be a HostFiles implementation and secondly there must be some files packed into the current directory.

CONST magicnum
Used as a magic number in the exe-file to mark the position of the address to the file table. This constant is only exported to make it possible for DevPacker to write the appropriate magic number into the exe-file.

PROCEDURE SetFilesDir
If no files are found in the current executable a call to this procedure does nothing. Otherwise it checks if HostFiles is the current directory of Files. In that case it holds a reference to this directory and calls Files.SetDir with the HostPackedFiles.Directory. This procedure is called the first time the module is loaded.

PROCEDURE RestoreFilesDir
Calls Files.SetDir with the original directory saved by SetFilesDir. Thus, uninstalling HostPackedFiles as the current Files directory.

PROCEDURE IsInstalled
Checks wether HostPackedFiles.Directory is the directory used by Files or not. This is not always a reliable test, since HostPackedFiles installs itself when it is loaded. A call to IsInstalled may cause HostPackedFiles to be installed. If, however, the result is that HostPackedFiles is not installed then this is a reliable result.


Executable File Format Required by HostPackedFiles
(* Should this really be here? *)
When DevPacker is used to pack files into an executable file it simply appends its information at the end of the file. This means that the first part of such an executable is exactly the same as a normal Windows executable. This text therefore only explains the appended information that DevPacker adds to the file.

The format of the exe-file can be separated into four parts:

	1) A Windows executable part
	2) Contents of files packed in by DevPacker
	3) File table of the files packed in by DevPacker
	4) Address of the file table
	
The four parts appear in this order in the executable, which makes the executable look like Figure 1.



Figure 1. Executable file structure.

The first part contains the executable generated by the linker command and all Windows resources, such as icons and cursors. Since DevPacker only appends information to the file, it is important that all resources are added before files are packed into the executable. Otherwise the information about how to find the files within the executable will be destroyed.

The second part is the content of all the files packed in by DevPacker. The contents are simply an array of bytes. To know where a file starts and how long it is the third part, the file table, is needed. The file table has the structure described in Figure 2.



Figure 2. File table structure.

To avoid sorting each time the HostPackedFiles is loaded, DevPacker does the sorting when files are packed. HostPackedFiles therefore assumes that the files are listed in reversed alphabetical order.	

The last eight bytes of the executable are interpreted as two integers, having the meaning shown in Figure 3.



Figure 3. Address of the file table.

If the first integer does not equal HostPackedFiles.magicnum it is assumed that no files are packed into the executable. Otherwise the next integer is interpreted as the address of the file table within the executable.