Changes

Jump to navigation Jump to search
774 bytes added ,  26 February
m
readability improvements
Line 1: Line 1:  
TrackMania '''.pak files''' are archives that contain a collection of other files, much like .zip archives. They are found in the "Packs" folder in the game installation directory. In ManiaPlanet, there are also '''.Pack.Gbx''' files with the same purpose.
 
TrackMania '''.pak files''' are archives that contain a collection of other files, much like .zip archives. They are found in the "Packs" folder in the game installation directory. In ManiaPlanet, there are also '''.Pack.Gbx''' files with the same purpose.
   −
In older TM versions, .pak files are both compressed and encrypted (version 3); even the file index containing the file names and directory structure is encrypted. In ManiaPlanet, they include an uncompressed/uncrypted section as well (versions 6+).
+
In older TM versions, .pak files are both zlib-compressed and encrypted (version 3); even the file index containing the file names and directory structure is encrypted. In ManiaPlanet, they include an uncompressed/uncrypted section as well (versions 6+). Since version 18, compression has switched to {{wp|LZ4 (compression algorithm)|LZ4}} with a specific dictionary.
    
== Encryption ==
 
== Encryption ==
 
.pak files are encrypted using {{wp|Blowfish (cipher)|Blowfish}} in {{wp|Block cipher mode of operation#Cipher Block_Chaining .28CBC.29|CBC mode}} with a 16-byte key. When an encrypted block begins, decryption is initialized by reading an 8-byte, plaintext IV ({{wp|initialization vector}}) from the file. From then on, Blowfish decryption commences.
 
.pak files are encrypted using {{wp|Blowfish (cipher)|Blowfish}} in {{wp|Block cipher mode of operation#Cipher Block_Chaining .28CBC.29|CBC mode}} with a 16-byte key. When an encrypted block begins, decryption is initialized by reading an 8-byte, plaintext IV ({{wp|initialization vector}}) from the file. From then on, Blowfish decryption commences.
   −
Each .pak file has its own encryption key. The keys for the different packs are found in [[packlist.dat]].
+
Each .pak file has its own encryption key. For older games, keys for the different packs are found in [[packlist.dat]]. Since ManiaPlanet, these keys are sent from the master server as "sub-keys", then stored in profile chunks for offline purpose, and calculated into final keys during runtime. ManiaPlanet.pak and Resource.pak are the only two that have these sub-keys hardcoded in a function. ManiaPlanet also added an additional key for decrypting the files inside that is calculated differently by hashing the sub-key:
 +
 
 +
md5("[sub-key]" + "NadeoPak")
    
There is one gotcha where Nadeo deviates from regular CBC: on the first read and after every 256 bytes read, the current IV is xor'd with a value we'll call ivXor (this happens before the IV is applied to the Blowfish-decrypted block). ivXor is initialized to zero and is also reset to zero every time it is applied, so most times it doesn't have an effect. Crucially though it does get assigned a non-zero value while reading the .pak header, of which the effect usually kicks in somewhere in the middle of the file list. So if you don't take this into account, half your file table will be messed up (which is likely what Nadeo was intending with this trick). The same goes for [[GBX|.gbx files]] embedded in packs.
 
There is one gotcha where Nadeo deviates from regular CBC: on the first read and after every 256 bytes read, the current IV is xor'd with a value we'll call ivXor (this happens before the IV is applied to the Blowfish-decrypted block). ivXor is initialized to zero and is also reset to zero every time it is applied, so most times it doesn't have an effect. Crucially though it does get assigned a non-zero value while reading the .pak header, of which the effect usually kicks in somewhere in the middle of the file list. So if you don't take this into account, half your file table will be messed up (which is likely what Nadeo was intending with this trick). The same goes for [[GBX|.gbx files]] embedded in packs.
Line 71: Line 73:  
  }
 
  }
   −
This function gets called in two several cases: when reading the .pak header, and when reading the contents of a .gbx file embedded in the pak.
+
This function gets called in two cases: when reading the .pak header, and when reading the contents of a .gbx file embedded in the pak.
    
For the header, the ivXor setup only happens if there are three or more folders, and the name of the third folder is 4 or more characters in length. The folder's name is converted to UTF16 and CalcIVXor(&wszName[2], 4) is called (i.e. it runs on the third and fourth character of the name, both characters being two bytes). If the one of the folder conditions is not met, ivXor stays zero.
 
For the header, the ivXor setup only happens if there are three or more folders, and the name of the third folder is 4 or more characters in length. The folder's name is converted to UTF16 and CalcIVXor(&wszName[2], 4) is called (i.e. it runs on the third and fourth character of the name, both characters being two bytes). If the one of the folder conditions is not met, ivXor stays zero.
   −
For .gbx files, CalcIVXor(&specificParentClassID, 4) is called every time [[GBX#Reading_the_body|ReadNode]] is called. First, the parent class of the current node's class is determined. Then, the specific class ID corresponding to this is determined (which is the exact opposite process of finding the generalized class ID as described in [[Class IDs#Class ID mappings|Class IDs]]). This ID, stored as little endian bytes, is then used as input for CalcIVXor. A few chunks also do ivXor additionally - Plug::CPlugSurfaceGeom 0900F004 and Scene::CSceneVehicleTuning 0A02E000 / Plug::CPlugVehiclePhyTuning 090EB000.
+
For .gbx files, CalcIVXor(&specificParentClassID, 4) is called every time [[GBX#Reading_the_body|ReadNode]] is called. First, the parent class of the current node's class is determined. Then, the specific class ID corresponding to this is determined (which is the exact opposite process of finding the generalized class ID as described in [[Class IDs#Class ID mappings|Class IDs]]). This ID, stored as little endian bytes, is then used as input for CalcIVXor.
    
There is one special exception with .gbx files. If the specific class ID is 0x07031000 (Control::CControlText), 0x07001000 (Control::CControlBase) is used as input instead.
 
There is one special exception with .gbx files. If the specific class ID is 0x07031000 (Control::CControlText), 0x07001000 (Control::CControlBase) is used as input instead.
   −
In the code, the ivXor execution is referred to as m_DummyWrite.
+
A few chunks also do ivXor additionally - Plug::CPlugSurfaceGeom 0900F004 and Scene::CSceneVehicleTuning 0A02E000 / Plug::CPlugVehiclePhyTuning 090EB000. In the code, the ivXor execution is referred to as m_DummyWrite.
    
===Header versions 6+===
 
===Header versions 6+===
Line 213: Line 215:     
====GBX compression intricacies====
 
====GBX compression intricacies====
There are some not-so-obvious details about compressed .gbx files, which may not seem important at first sight but actually make a world of difference when extracting them. Not addressing these ''will'' result in corrupt data. (Note: this is about the deflate compression specific to .pak files, not the LZO compression specific to .gbx data sections. .gbx files in a .pak don't have LZO-compressed data sections (BUUR header)).
+
There are some not-so-obvious details about compressed .gbx files, which may not seem important at first sight but actually make a world of difference when extracting them. Not addressing these ''will'' result in corrupt data. (Note: this is about the deflate compression specific to .pak files, not the {{wp|Lempel–Ziv–Oberhumer|LZO}} compression specific to .gbx data sections. .gbx files in a .pak don't have LZO-compressed data sections (BUUR header)).
 
* Files from a .pak are not decrypted and then decompressed in their entirety before they are parsed. Instead, the parser requests data from the decompressor as it goes along, which in turn requests data from the decrypter. Whenever the decompressor's buffer is empty and new data is requested, it requests blocks of 0x100 bytes from the decrypter and decompresses each block into its buffer of 0x400 bytes, until the buffer is full. Then, whenever the parser requests more data, it is simply copied over from the buffer – until the buffer is empty again.
 
* Files from a .pak are not decrypted and then decompressed in their entirety before they are parsed. Instead, the parser requests data from the decompressor as it goes along, which in turn requests data from the decrypter. Whenever the decompressor's buffer is empty and new data is requested, it requests blocks of 0x100 bytes from the decrypter and decompresses each block into its buffer of 0x400 bytes, until the buffer is full. Then, whenever the parser requests more data, it is simply copied over from the buffer – until the buffer is empty again.
 
* You ''must'' use the [http://www.zlib.net zlib library]. Don't use a different zlib-compatible implementation, and also don't skip over the zlib header and decompress the data with a deflate implementation. TrackMania very much depends on zlib's behaviour: how soon it starts returning decompressed data after compressed data has been put into it, and how much decompressed data it returns on every iteration.
 
* You ''must'' use the [http://www.zlib.net zlib library]. Don't use a different zlib-compatible implementation, and also don't skip over the zlib header and decompress the data with a deflate implementation. TrackMania very much depends on zlib's behaviour: how soon it starts returning decompressed data after compressed data has been put into it, and how much decompressed data it returns on every iteration.
Line 237: Line 239:  
== Tools ==
 
== Tools ==
    +
* [https://io.gbx.tools/pak-to-zip Pak to ZIP] - an online client-side tool to extract TMUF .pak files, based on GBX.NET.PAK ([https://io.gbx.tools/pak-to-zip-vsk5 VSK5 .pak variant])
 
* {{ArchiveOrg|http://forum.mania-creative.com/thread-2379.html|TMPakTool|https://web.archive.org/web/20121007000654/http://forum.mania-creative.com/thread-2379.html}} - an open source tool which can open and edit .pak files in an Explorer-like interface. Comes with a C# library which you can use in your own applications to work with .pak files. [http://www.mediafire.com/file/y4klvl2op6cpd4p/TMPakTool-v0.2.zip Download] & [http://www.mediafire.com/file/lq3j99k4tk6uav9/TMPakTool-v0.2+src.zip Source code]
 
* {{ArchiveOrg|http://forum.mania-creative.com/thread-2379.html|TMPakTool|https://web.archive.org/web/20121007000654/http://forum.mania-creative.com/thread-2379.html}} - an open source tool which can open and edit .pak files in an Explorer-like interface. Comes with a C# library which you can use in your own applications to work with .pak files. [http://www.mediafire.com/file/y4klvl2op6cpd4p/TMPakTool-v0.2.zip Download] & [http://www.mediafire.com/file/lq3j99k4tk6uav9/TMPakTool-v0.2+src.zip Source code]
 
* [[GbxDump]] - a Windows tool to dump and analyze the headers of .Challenge|Map.Gbx, .Replay.Gbx, .Pack.Gbx|.pak, .ObjectInfo.Gbx and .Item.Gbx files.
 
* [[GbxDump]] - a Windows tool to dump and analyze the headers of .Challenge|Map.Gbx, .Replay.Gbx, .Pack.Gbx|.pak, .ObjectInfo.Gbx and .Item.Gbx files.
44

edits

Navigation menu