Mortal Kombat 4 filesys.dat

The PC version of the game Mortal Kombat 4 uses a single filesys.dat archive file to store all of its game data. The format is not publicly documented, but has been reverse-engineered.

Format details
The filesys.dat archive consists of two sections: a hash table listing file locations and sizes, and file data.

Within the file, all strings are ASCII (the behavior of high-bit strings is unknown, as it is not used), all numbers are stored as little-endian 32-bit unsigned integers, and all sizes are in bytes.

File Hash Table
The filesys.dat archive begins with a header of 1024 entries, which are sorted by their hash and accessed using a binary search algorithm. The end of the header is padded with zeros, and the game stops reading when the first 0 hash is detected.

Each entry in the hash table has the following structure:

Hash algorithm
The hash for a given file name is calculated via the following steps:
 * Convert the the file name to uppercase
 * Nul-pad the string to a multiple of 4 bytes
 * Treat resulting string the file name as an array of 32-bit little-endian unsigned integers and add them all up
 * Adding the length of the original file name

Sample Python code: def filesys_hash(fileName): a = fileName.upper.encode('ascii') return (sum( a[i:i+4] for i in range(0, len(a), 4) ) + len(fileName)) & 0xFFFFFFFF

Sample C code (with shortcuts simliar to original code):
 * 1) include 
 * 2) include 

uint32_t filesys_hash(const char *fileName){ uint32_t hash; size_t i;

for(i = 0; fileName[i] != 0; i++){ hash += toupper(fileName[i]) * (8 * (i % 4)) }

return hash + i; }