1 | /*****************************************************************************/
|
---|
2 | /* StormLib.h Copyright (c) Ladislav Zezula 1999-2010 */
|
---|
3 | /*---------------------------------------------------------------------------*/
|
---|
4 | /* StormLib library v 7.02 */
|
---|
5 | /* */
|
---|
6 | /* Author : Ladislav Zezula */
|
---|
7 | /* E-mail : ladik@zezula.net */
|
---|
8 | /* WWW : http://www.zezula.net */
|
---|
9 | /*---------------------------------------------------------------------------*/
|
---|
10 | /* Date Ver Who Comment */
|
---|
11 | /* -------- ---- --- ------- */
|
---|
12 | /* xx.xx.99 1.00 Lad Created */
|
---|
13 | /* 24.03.03 2.50 Lad Version 2.50 */
|
---|
14 | /* 02.04.03 3.00 Lad Version 3.00 with compression */
|
---|
15 | /* 11.04.03 3.01 Lad Renamed to StormLib.h for compatibility with */
|
---|
16 | /* original headers for Storm.dll */
|
---|
17 | /* 10.05.03 3.02 Lad Added Pkware DCL compression */
|
---|
18 | /* 26.05.03 4.00 Lad Completed all compressions */
|
---|
19 | /* 18.06.03 4.01 Lad Added SFileSetFileLocale */
|
---|
20 | /* Added SFileExtractFile */
|
---|
21 | /* 26.07.03 4.02 Lad Implemented nameless rename and delete */
|
---|
22 | /* 26.07.03 4.03 Lad Added support for protected MPQs */
|
---|
23 | /* 28.08.03 4.10 Lad Fixed bugs that caused StormLib incorrectly work */
|
---|
24 | /* with Diablo I savegames and with files having full */
|
---|
25 | /* hash table */
|
---|
26 | /* 08.12.03 4.11 DCH Fixed bug in reading file sector larger than 0x1000 */
|
---|
27 | /* on certain files. */
|
---|
28 | /* Fixed bug in AddFile with MPQ_FILE_REPLACE_EXISTING */
|
---|
29 | /* (Thanx Daniel Chiamarello, dchiamarello@madvawes.com)*/
|
---|
30 | /* 21.12.03 4.50 Lad Completed port for Mac */
|
---|
31 | /* Fixed bug in compacting (if fsize is mul of 0x1000) */
|
---|
32 | /* Fixed bug in SCompCompress */
|
---|
33 | /* 27.05.04 4.51 Lad Changed memory management from new/delete to our */
|
---|
34 | /* own macros */
|
---|
35 | /* 22.06.04 4.60 Lad Optimized search. Support for multiple listfiles. */
|
---|
36 | /* 30.09.04 4.61 Lad Fixed some bugs (Aaargh !!!) */
|
---|
37 | /* Correctly works if HashTableSize > BlockTableSize */
|
---|
38 | /* 29.12.04 4.70 Lad Fixed compatibility problem with MPQs from WoW */
|
---|
39 | /* 14.07.05 5.00 Lad Added the BZLIB compression support */
|
---|
40 | /* Added suport of files stored as single unit */
|
---|
41 | /* 17.04.06 5.01 Lad Converted to MS Visual Studio 8.0 */
|
---|
42 | /* Fixed issue with protected Warcraft 3 protected maps */
|
---|
43 | /* 15.05.06 5.02 Lad Fixed issue with WoW 1.10+ */
|
---|
44 | /* 07.09.06 5.10 Lad Fixed processing files longer than 2GB */
|
---|
45 | /* 22.11.06 6.00 Lad Support for MPQ archives V2 */
|
---|
46 | /* 12.06.07 6.10 Lad Support for (attributes) file */
|
---|
47 | /* 10.09.07 6.12 Lad Support for MPQs protected by corrupting hash table */
|
---|
48 | /* 03.12.07 6.13 Lad Support for MPQs with hash tbl size > block tbl size */
|
---|
49 | /* 07.04.08 6.20 Lad Added SFileFlushArchive */
|
---|
50 | /* 09.04.08 Lad Removed FilePointer variable from MPQ handle */
|
---|
51 | /* structure, as it caused more problems than benefits */
|
---|
52 | /* 12.05.08 6.22 Lad Support for w3xMaster map protector */
|
---|
53 | /* 05.10.08 6.23 Lad Support for protectors who set negative values in */
|
---|
54 | /* the table of file blocks */
|
---|
55 | /* 26.05.09 6.24 Lad Fixed search for multiple lang files with deleted */
|
---|
56 | /* entries */
|
---|
57 | /* 03.09.09 6.25 Lad Fixed decompression bug in huffmann decompression */
|
---|
58 | /* 22.03.10 6.50 Lad New compressions in Starcraft II (LZMA, sparse) */
|
---|
59 | /* Fixed compacting MPQs that contain single unit files */
|
---|
60 | /* 26.04.10 7.00 Lad Major rewrite */
|
---|
61 | /* 08.06.10 7.10 Lad Support for partial MPQs */
|
---|
62 | /* 08.07.10 7.11 Lad Support for MPQs v 3.0 */
|
---|
63 | /* 20.08.10 7.20 Lad Support for opening multiple MPQs in patch mode */
|
---|
64 | /* 20.09.10 8.00 Lad MPQs v 4, HET and BET tables */
|
---|
65 | /* 07.01.11 8.01 Lad Write support for MPQs v 3 and 4 */
|
---|
66 | /* 15.09.11 8.04 Lad Bug fixes, testing for Diablo III MPQs */
|
---|
67 | /* 26.04.12 8.10 Lad Support for data map, added SFileGetArchiveBitmap */
|
---|
68 | /* 29.05.12 8.20 Lad C-only interface */
|
---|
69 | /* 14.01.13 8.21 Lad ADPCM and Huffmann (de)compression refactored */
|
---|
70 | /*****************************************************************************/
|
---|
71 |
|
---|
72 | #ifndef __STORMLIB_H__
|
---|
73 | #define __STORMLIB_H__
|
---|
74 |
|
---|
75 | #ifdef _MSC_VER
|
---|
76 | #pragma warning(disable:4668) // 'XXX' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
|
---|
77 | #pragma warning(disable:4820) // 'XXX' : '2' bytes padding added after data member 'XXX::yyy'
|
---|
78 | #endif
|
---|
79 |
|
---|
80 | #include "StormPort.h"
|
---|
81 |
|
---|
82 | #ifdef __cplusplus
|
---|
83 | extern "C" {
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | //-----------------------------------------------------------------------------
|
---|
87 | // Use the apropriate library
|
---|
88 | //
|
---|
89 | // The library type is encoded in the library name as the following
|
---|
90 | // StormLibXYZ.lib
|
---|
91 | //
|
---|
92 | // X - D for Debug version, R for Release version
|
---|
93 | // Y - A for ANSI version, U for Unicode version
|
---|
94 | // Z - S for static-linked CRT library, D for multithreaded DLL CRT library
|
---|
95 | //
|
---|
96 |
|
---|
97 | #if defined(_MSC_VER) && !defined(__STORMLIB_SELF__)
|
---|
98 |
|
---|
99 | #ifdef _DEBUG // DEBUG VERSIONS
|
---|
100 | #ifndef _UNICODE
|
---|
101 | #ifdef _DLL
|
---|
102 | #pragma comment(lib, "StormLibDAD.lib") // Debug Ansi CRT-DLL version
|
---|
103 | #else
|
---|
104 | #pragma comment(lib, "StormLibDAS.lib") // Debug Ansi CRT-LIB version
|
---|
105 | #endif
|
---|
106 | #else
|
---|
107 | #ifdef _DLL
|
---|
108 | #pragma comment(lib, "StormLibDUD.lib") // Debug Unicode CRT-DLL version
|
---|
109 | #else
|
---|
110 | #pragma comment(lib, "StormLibDUS.lib") // Debug Unicode CRT-LIB version
|
---|
111 | #endif
|
---|
112 | #endif
|
---|
113 | #else // RELEASE VERSIONS
|
---|
114 | #ifndef _UNICODE
|
---|
115 | #ifdef _DLL
|
---|
116 | #pragma comment(lib, "StormLibRAD.lib") // Release Ansi CRT-DLL version
|
---|
117 | #else
|
---|
118 | #pragma comment(lib, "StormLibRAS.lib") // Release Ansi CRT-LIB version
|
---|
119 | #endif
|
---|
120 | #else
|
---|
121 | #ifdef _DLL
|
---|
122 | #pragma comment(lib, "StormLibRUD.lib") // Release Unicode CRT-DLL version
|
---|
123 | #else
|
---|
124 | #pragma comment(lib, "StormLibRUS.lib") // Release Unicode CRT-LIB version
|
---|
125 | #endif
|
---|
126 | #endif
|
---|
127 | #endif
|
---|
128 |
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | //-----------------------------------------------------------------------------
|
---|
132 | // Defines
|
---|
133 |
|
---|
134 | #define STORMLIB_VERSION 0x0815 // Current version of StormLib (8.21)
|
---|
135 | #define STORMLIB_VERSION_STRING "8.21" // String version of StormLib version
|
---|
136 |
|
---|
137 | #define ID_MPQ 0x1A51504D // MPQ archive header ID ('MPQ\x1A')
|
---|
138 | #define ID_MPQ_USERDATA 0x1B51504D // MPQ userdata entry ('MPQ\x1B')
|
---|
139 |
|
---|
140 | #define ERROR_AVI_FILE 10000 // No MPQ file, but AVI file.
|
---|
141 | #define ERROR_UNKNOWN_FILE_KEY 10001 // Returned by SFileReadFile when can't find file key
|
---|
142 | #define ERROR_CHECKSUM_ERROR 10002 // Returned by SFileReadFile when sector CRC doesn't match
|
---|
143 | #define ERROR_INTERNAL_FILE 10003 // The given operation is not allowed on internal file
|
---|
144 | #define ERROR_BASE_FILE_MISSING 10004 // The file is present as incremental patch file, but base file is missing
|
---|
145 | #define ERROR_MARKED_FOR_DELETE 10005 // The file was marked as "deleted" in the MPQ
|
---|
146 |
|
---|
147 | // Values for SFileCreateArchive
|
---|
148 | #define HASH_TABLE_SIZE_MIN 0x00000004 // Minimum acceptable hash table size
|
---|
149 | #define HASH_TABLE_SIZE_DEFAULT 0x00001000 // Default hash table size for empty MPQs
|
---|
150 | #define HASH_TABLE_SIZE_MAX 0x00080000 // Maximum acceptable hash table size
|
---|
151 |
|
---|
152 | #define HASH_ENTRY_DELETED 0xFFFFFFFE // Block index for deleted entry in the hash table
|
---|
153 | #define HASH_ENTRY_FREE 0xFFFFFFFF // Block index for free entry in the hash table
|
---|
154 |
|
---|
155 | #define HET_ENTRY_DELETED 0x80 // HET hash value for a deleted entry
|
---|
156 | #define HET_ENTRY_FREE 0x00 // HET hash value for free entry
|
---|
157 |
|
---|
158 | #define HASH_STATE_SIZE 0x60 // Size of LibTomCrypt's hash_state structure
|
---|
159 |
|
---|
160 | #define MPQ_PATCH_PREFIX_LEN 0x20 // Maximum length of the patch prefix
|
---|
161 |
|
---|
162 | // Values for SFileOpenArchive
|
---|
163 | #define SFILE_OPEN_HARD_DISK_FILE 2 // Open the archive on HDD
|
---|
164 | #define SFILE_OPEN_CDROM_FILE 3 // Open the archive only if it is on CDROM
|
---|
165 |
|
---|
166 | // Values for SFileOpenFile
|
---|
167 | #define SFILE_OPEN_FROM_MPQ 0x00000000 // Open the file from the MPQ archive
|
---|
168 | #define SFILE_OPEN_BASE_FILE 0xFFFFFFFD // Reserved for StormLib internal use
|
---|
169 | #define SFILE_OPEN_ANY_LOCALE 0xFFFFFFFE // Reserved for StormLib internal use
|
---|
170 | #define SFILE_OPEN_LOCAL_FILE 0xFFFFFFFF // Open a local file
|
---|
171 |
|
---|
172 | // Flags for TMPQArchive::dwFlags
|
---|
173 | #define MPQ_FLAG_READ_ONLY 0x00000001 // If set, the MPQ has been open for read-only access
|
---|
174 | #define MPQ_FLAG_CHANGED 0x00000002 // If set, the MPQ tables have been changed
|
---|
175 | #define MPQ_FLAG_PROTECTED 0x00000004 // Set on protected MPQs (like W3M maps)
|
---|
176 | #define MPQ_FLAG_CHECK_SECTOR_CRC 0x00000008 // Checking sector CRC when reading files
|
---|
177 | #define MPQ_FLAG_NEED_FIX_SIZE 0x00000010 // Used during opening the archive
|
---|
178 | #define MPQ_FLAG_INV_LISTFILE 0x00000020 // If set, it means that the (listfile) has been invalidated
|
---|
179 | #define MPQ_FLAG_INV_ATTRIBUTES 0x00000040 // If set, it means that the (attributes) has been invalidated
|
---|
180 |
|
---|
181 | // Return value for SFileGetFileSize and SFileSetFilePointer
|
---|
182 | #define SFILE_INVALID_SIZE 0xFFFFFFFF
|
---|
183 | #define SFILE_INVALID_POS 0xFFFFFFFF
|
---|
184 | #define SFILE_INVALID_ATTRIBUTES 0xFFFFFFFF
|
---|
185 |
|
---|
186 | // Flags for SFileAddFile
|
---|
187 | #define MPQ_FILE_IMPLODE 0x00000100 // Implode method (By PKWARE Data Compression Library)
|
---|
188 | #define MPQ_FILE_COMPRESS 0x00000200 // Compress methods (By multiple methods)
|
---|
189 | #define MPQ_FILE_COMPRESSED 0x0000FF00 // File is compressed
|
---|
190 | #define MPQ_FILE_ENCRYPTED 0x00010000 // Indicates whether file is encrypted
|
---|
191 | #define MPQ_FILE_FIX_KEY 0x00020000 // File decryption key has to be fixed
|
---|
192 | #define MPQ_FILE_PATCH_FILE 0x00100000 // The file is a patch file. Raw file data begin with TPatchInfo structure
|
---|
193 | #define MPQ_FILE_SINGLE_UNIT 0x01000000 // File is stored as a single unit, rather than split into sectors (Thx, Quantam)
|
---|
194 | #define MPQ_FILE_DELETE_MARKER 0x02000000 // File is a deletion marker. Used in MPQ patches, indicating that the file no longer exists.
|
---|
195 | #define MPQ_FILE_SECTOR_CRC 0x04000000 // File has checksums for each sector.
|
---|
196 | // Ignored if file is not compressed or imploded.
|
---|
197 | #define MPQ_FILE_EXISTS 0x80000000 // Set if file exists, reset when the file was deleted
|
---|
198 | #define MPQ_FILE_REPLACEEXISTING 0x80000000 // Replace when the file exist (SFileAddFile)
|
---|
199 |
|
---|
200 | #define MPQ_FILE_VALID_FLAGS (MPQ_FILE_IMPLODE | \
|
---|
201 | MPQ_FILE_COMPRESS | \
|
---|
202 | MPQ_FILE_ENCRYPTED | \
|
---|
203 | MPQ_FILE_FIX_KEY | \
|
---|
204 | MPQ_FILE_PATCH_FILE | \
|
---|
205 | MPQ_FILE_SINGLE_UNIT | \
|
---|
206 | MPQ_FILE_DELETE_MARKER | \
|
---|
207 | MPQ_FILE_SECTOR_CRC | \
|
---|
208 | MPQ_FILE_EXISTS)
|
---|
209 |
|
---|
210 | // Compression types for multiple compressions
|
---|
211 | #define MPQ_COMPRESSION_HUFFMANN 0x01 // Huffmann compression (used on WAVE files only)
|
---|
212 | #define MPQ_COMPRESSION_ZLIB 0x02 // ZLIB compression
|
---|
213 | #define MPQ_COMPRESSION_PKWARE 0x08 // PKWARE DCL compression
|
---|
214 | #define MPQ_COMPRESSION_BZIP2 0x10 // BZIP2 compression (added in Warcraft III)
|
---|
215 | #define MPQ_COMPRESSION_SPARSE 0x20 // Sparse compression (added in Starcraft 2)
|
---|
216 | #define MPQ_COMPRESSION_ADPCM_MONO 0x40 // IMA ADPCM compression (mono)
|
---|
217 | #define MPQ_COMPRESSION_ADPCM_STEREO 0x80 // IMA ADPCM compression (stereo)
|
---|
218 | #define MPQ_COMPRESSION_LZMA 0x12 // LZMA compression. Added in Starcraft 2. This value is NOT a combination of flags.
|
---|
219 | #define MPQ_COMPRESSION_NEXT_SAME 0xFFFFFFFF // Same compression
|
---|
220 |
|
---|
221 | // Constants for SFileAddWave
|
---|
222 | #define MPQ_WAVE_QUALITY_HIGH 0 // Best quality, the worst compression
|
---|
223 | #define MPQ_WAVE_QUALITY_MEDIUM 1 // Medium quality, medium compression
|
---|
224 | #define MPQ_WAVE_QUALITY_LOW 2 // Low quality, the best compression
|
---|
225 |
|
---|
226 | // Signatures for HET and BET table
|
---|
227 | #define HET_TABLE_SIGNATURE 0x1A544548 // 'HET\x1a'
|
---|
228 | #define BET_TABLE_SIGNATURE 0x1A544542 // 'BET\x1a'
|
---|
229 |
|
---|
230 | // Decryption keys for MPQ tables
|
---|
231 | #define MPQ_KEY_HASH_TABLE 0xC3AF3770 // Obtained by HashString("(hash table)", MPQ_HASH_FILE_KEY)
|
---|
232 | #define MPQ_KEY_BLOCK_TABLE 0xEC83B3A3 // Obtained by HashString("(block table)", MPQ_HASH_FILE_KEY)
|
---|
233 |
|
---|
234 | // Block map defines
|
---|
235 | #define MPQ_DATA_BITMAP_SIGNATURE 0x33767470 // Signature of the MPQ data bitmap ('ptv3')
|
---|
236 |
|
---|
237 | // Constants for SFileGetFileInfo
|
---|
238 | #define SFILE_INFO_ARCHIVE_NAME 1 // MPQ size (value from header)
|
---|
239 | #define SFILE_INFO_ARCHIVE_SIZE 2 // MPQ size (value from header)
|
---|
240 | #define SFILE_INFO_MAX_FILE_COUNT 3 // Max number of files in the MPQ
|
---|
241 | #define SFILE_INFO_HASH_TABLE_SIZE 4 // Size of hash table, in entries
|
---|
242 | #define SFILE_INFO_BLOCK_TABLE_SIZE 5 // Number of entries in the block table
|
---|
243 | #define SFILE_INFO_SECTOR_SIZE 6 // Size of file sector (in bytes)
|
---|
244 | #define SFILE_INFO_HASH_TABLE 7 // Pointer to Hash table (TMPQHash *)
|
---|
245 | #define SFILE_INFO_BLOCK_TABLE 8 // Pointer to Block Table (TMPQBlock *)
|
---|
246 | #define SFILE_INFO_NUM_FILES 9 // Real number of files within archive
|
---|
247 | #define SFILE_INFO_STREAM_FLAGS 10 // Stream flags for the MPQ. See STREAM_FLAG_XXX
|
---|
248 | #define SFILE_INFO_IS_READ_ONLY 11 // TRUE of the MPQ was open as read only
|
---|
249 | //------
|
---|
250 | #define SFILE_INFO_HASH_INDEX 100 // Hash index of file in MPQ
|
---|
251 | #define SFILE_INFO_CODENAME1 101 // The first codename of the file
|
---|
252 | #define SFILE_INFO_CODENAME2 102 // The second codename of the file
|
---|
253 | #define SFILE_INFO_LOCALEID 103 // Locale ID of file in MPQ
|
---|
254 | #define SFILE_INFO_BLOCKINDEX 104 // Index to Block Table
|
---|
255 | #define SFILE_INFO_FILE_SIZE 105 // Original file size (from the block table)
|
---|
256 | #define SFILE_INFO_COMPRESSED_SIZE 106 // Compressed file size (from the block table)
|
---|
257 | #define SFILE_INFO_FLAGS 107 // File flags
|
---|
258 | #define SFILE_INFO_POSITION 108 // File position within archive
|
---|
259 | // Note: for current pointer in open MPQ file,
|
---|
260 | // use SFileSetFilePointer(hFile, 0, NULL, FILE_CURRENT);
|
---|
261 | #define SFILE_INFO_KEY 109 // File decryption key
|
---|
262 | #define SFILE_INFO_KEY_UNFIXED 110 // Decryption key not fixed to file pos and size
|
---|
263 | #define SFILE_INFO_FILETIME 111 // TMPQFileTime
|
---|
264 | #define SFILE_INFO_PATCH_CHAIN 112 // Chain of patches
|
---|
265 |
|
---|
266 | #define LISTFILE_NAME "(listfile)" // Name of internal listfile
|
---|
267 | #define SIGNATURE_NAME "(signature)" // Name of internal signature
|
---|
268 | #define ATTRIBUTES_NAME "(attributes)" // Name of internal attributes file
|
---|
269 | #define PATCH_METADATA_NAME "(patch_metadata)"
|
---|
270 |
|
---|
271 | #define MPQ_FORMAT_VERSION_1 0 // Up to The Burning Crusade
|
---|
272 | #define MPQ_FORMAT_VERSION_2 1 // The Burning Crusade and newer
|
---|
273 | #define MPQ_FORMAT_VERSION_3 2 // WoW Cataclysm Beta
|
---|
274 | #define MPQ_FORMAT_VERSION_4 3 // WoW Cataclysm and newer
|
---|
275 |
|
---|
276 | // Flags for MPQ attributes
|
---|
277 | #define MPQ_ATTRIBUTE_CRC32 0x00000001 // The "(attributes)" contains CRC32 for each file
|
---|
278 | #define MPQ_ATTRIBUTE_FILETIME 0x00000002 // The "(attributes)" contains file time for each file
|
---|
279 | #define MPQ_ATTRIBUTE_MD5 0x00000004 // The "(attributes)" contains MD5 for each file
|
---|
280 | #define MPQ_ATTRIBUTE_PATCH_BIT 0x00000008 // The "(attributes)" contains a patch bit for each file
|
---|
281 | #define MPQ_ATTRIBUTE_ALL 0x0000000F // Summary mask
|
---|
282 |
|
---|
283 | #define MPQ_ATTRIBUTES_V1 100 // (attributes) format version 1.00
|
---|
284 |
|
---|
285 | // Flags for SFileOpenArchive
|
---|
286 | #define BASE_PROVIDER_FILE 0x00000000 // Base data source is a file
|
---|
287 | #define BASE_PROVIDER_MAP 0x00000001 // Base data source is memory-mapped file
|
---|
288 | #define BASE_PROVIDER_HTTP 0x00000002 // Base data source is a file on web server
|
---|
289 | #define BASE_PROVIDER_MASK 0x0000000F // Mask for base provider value
|
---|
290 |
|
---|
291 | #define STREAM_PROVIDER_LINEAR 0x00000000 // Stream is linear with no offset mapping
|
---|
292 | #define STREAM_PROVIDER_PARTIAL 0x00000010 // Stream is partial file (.part)
|
---|
293 | #define STREAM_PROVIDER_ENCRYPTED 0x00000020 // Stream is an encrypted MPQ
|
---|
294 | #define STREAM_PROVIDER_MASK 0x000000F0 // Mask for stream provider value
|
---|
295 |
|
---|
296 | #define STREAM_FLAG_READ_ONLY 0x00000100 // Stream is read only
|
---|
297 | #define STREAM_FLAG_WRITE_SHARE 0x00000200 // Allow write sharing when open for write
|
---|
298 | #define STREAM_FLAG_MASK 0x0000FF00 // Mask for stream flags
|
---|
299 | #define STREAM_OPTIONS_MASK 0x0000FFFF // Mask for all stream options
|
---|
300 |
|
---|
301 | #define MPQ_OPEN_NO_LISTFILE 0x00010000 // Don't load the internal listfile
|
---|
302 | #define MPQ_OPEN_NO_ATTRIBUTES 0x00020000 // Don't open the attributes
|
---|
303 | #define MPQ_OPEN_FORCE_MPQ_V1 0x00040000 // Always open the archive as MPQ v 1.00, ignore the "wFormatVersion" variable in the header
|
---|
304 | #define MPQ_OPEN_CHECK_SECTOR_CRC 0x00080000 // On files with MPQ_FILE_SECTOR_CRC, the CRC will be checked when reading file
|
---|
305 |
|
---|
306 | // Deprecated
|
---|
307 | #define MPQ_OPEN_READ_ONLY STREAM_FLAG_READ_ONLY
|
---|
308 | #define MPQ_OPEN_ENCRYPTED STREAM_PROVIDER_ENCRYPTED
|
---|
309 |
|
---|
310 | // Flags for SFileCreateArchive
|
---|
311 | #define MPQ_CREATE_ATTRIBUTES 0x00100000 // Also add the (attributes) file
|
---|
312 | #define MPQ_CREATE_ARCHIVE_V1 0x00000000 // Creates archive of version 1 (size up to 4GB)
|
---|
313 | #define MPQ_CREATE_ARCHIVE_V2 0x01000000 // Creates archive of version 2 (larger than 4 GB)
|
---|
314 | #define MPQ_CREATE_ARCHIVE_V3 0x02000000 // Creates archive of version 3
|
---|
315 | #define MPQ_CREATE_ARCHIVE_V4 0x03000000 // Creates archive of version 4
|
---|
316 | #define MPQ_CREATE_ARCHIVE_VMASK 0x0F000000 // Mask for archive version
|
---|
317 |
|
---|
318 | #define FLAGS_TO_FORMAT_SHIFT 24 // (MPQ_CREATE_ARCHIVE_V4 >> FLAGS_TO_FORMAT_SHIFT) => MPQ_FORMAT_VERSION_4
|
---|
319 |
|
---|
320 | // Flags for SFileVerifyFile
|
---|
321 | #define SFILE_VERIFY_SECTOR_CRC 0x00000001 // Verify sector checksum for the file, if available
|
---|
322 | #define SFILE_VERIFY_FILE_CRC 0x00000002 // Verify file CRC, if available
|
---|
323 | #define SFILE_VERIFY_FILE_MD5 0x00000004 // Verify file MD5, if available
|
---|
324 | #define SFILE_VERIFY_RAW_MD5 0x00000008 // Verify raw file MD5, if available
|
---|
325 | #define SFILE_VERIFY_ALL 0x0000000F // Verify every checksum possible
|
---|
326 |
|
---|
327 | // Return values for SFileVerifyFile
|
---|
328 | #define VERIFY_OPEN_ERROR 0x0001 // Failed to open the file
|
---|
329 | #define VERIFY_READ_ERROR 0x0002 // Failed to read all data from the file
|
---|
330 | #define VERIFY_FILE_HAS_SECTOR_CRC 0x0004 // File has sector CRC
|
---|
331 | #define VERIFY_FILE_SECTOR_CRC_ERROR 0x0008 // Sector CRC check failed
|
---|
332 | #define VERIFY_FILE_HAS_CHECKSUM 0x0010 // File has CRC32
|
---|
333 | #define VERIFY_FILE_CHECKSUM_ERROR 0x0020 // CRC32 check failed
|
---|
334 | #define VERIFY_FILE_HAS_MD5 0x0040 // File has data MD5
|
---|
335 | #define VERIFY_FILE_MD5_ERROR 0x0080 // MD5 check failed
|
---|
336 | #define VERIFY_FILE_HAS_RAW_MD5 0x0100 // File has raw data MD5
|
---|
337 | #define VERIFY_FILE_RAW_MD5_ERROR 0x0200 // Raw MD5 check failed
|
---|
338 | #define VERIFY_FILE_ERROR_MASK (VERIFY_OPEN_ERROR | VERIFY_READ_ERROR | VERIFY_FILE_SECTOR_CRC_ERROR | VERIFY_FILE_CHECKSUM_ERROR | VERIFY_FILE_MD5_ERROR | VERIFY_FILE_RAW_MD5_ERROR)
|
---|
339 |
|
---|
340 | // Flags for SFileVerifyRawData (for MPQs version 4.0 or higher)
|
---|
341 | #define SFILE_VERIFY_MPQ_HEADER 0x0001 // Verify raw MPQ header
|
---|
342 | #define SFILE_VERIFY_HET_TABLE 0x0002 // Verify raw data of the HET table
|
---|
343 | #define SFILE_VERIFY_BET_TABLE 0x0003 // Verify raw data of the BET table
|
---|
344 | #define SFILE_VERIFY_HASH_TABLE 0x0004 // Verify raw data of the hash table
|
---|
345 | #define SFILE_VERIFY_BLOCK_TABLE 0x0005 // Verify raw data of the block table
|
---|
346 | #define SFILE_VERIFY_HIBLOCK_TABLE 0x0006 // Verify raw data of the hi-block table
|
---|
347 | #define SFILE_VERIFY_FILE 0x0007 // Verify raw data of a file
|
---|
348 |
|
---|
349 | // Return values for SFileVerifyArchive
|
---|
350 | #define ERROR_NO_SIGNATURE 0 // There is no signature in the MPQ
|
---|
351 | #define ERROR_VERIFY_FAILED 1 // There was an error during verifying signature (like no memory)
|
---|
352 | #define ERROR_WEAK_SIGNATURE_OK 2 // There is a weak signature and sign check passed
|
---|
353 | #define ERROR_WEAK_SIGNATURE_ERROR 3 // There is a weak signature but sign check failed
|
---|
354 | #define ERROR_STRONG_SIGNATURE_OK 4 // There is a strong signature and sign check passed
|
---|
355 | #define ERROR_STRONG_SIGNATURE_ERROR 5 // There is a strong signature but sign check failed
|
---|
356 |
|
---|
357 | #ifndef MD5_DIGEST_SIZE
|
---|
358 | #define MD5_DIGEST_SIZE 0x10
|
---|
359 | #endif
|
---|
360 |
|
---|
361 | #ifndef SHA1_DIGEST_SIZE
|
---|
362 | #define SHA1_DIGEST_SIZE 0x14 // 160 bits
|
---|
363 | #endif
|
---|
364 |
|
---|
365 | #ifndef LANG_NEUTRAL
|
---|
366 | #define LANG_NEUTRAL 0x00 // Neutral locale
|
---|
367 | #endif
|
---|
368 |
|
---|
369 | //-----------------------------------------------------------------------------
|
---|
370 | // Callback functions
|
---|
371 |
|
---|
372 | // Values for compact callback
|
---|
373 | #define CCB_CHECKING_FILES 1 // Checking archive (dwParam1 = current, dwParam2 = total)
|
---|
374 | #define CCB_CHECKING_HASH_TABLE 2 // Checking hash table (dwParam1 = current, dwParam2 = total)
|
---|
375 | #define CCB_COPYING_NON_MPQ_DATA 3 // Copying non-MPQ data: No params used
|
---|
376 | #define CCB_COMPACTING_FILES 4 // Compacting archive (dwParam1 = current, dwParam2 = total)
|
---|
377 | #define CCB_CLOSING_ARCHIVE 5 // Closing archive: No params used
|
---|
378 |
|
---|
379 | typedef void (WINAPI * SFILE_ADDFILE_CALLBACK)(void * pvUserData, DWORD dwBytesWritten, DWORD dwTotalBytes, bool bFinalCall);
|
---|
380 | typedef void (WINAPI * SFILE_COMPACT_CALLBACK)(void * pvUserData, DWORD dwWorkType, ULONGLONG BytesProcessed, ULONGLONG TotalBytes);
|
---|
381 |
|
---|
382 | typedef struct TFileStream TFileStream;
|
---|
383 |
|
---|
384 | //-----------------------------------------------------------------------------
|
---|
385 | // Structure for bit arrays used for HET and BET tables
|
---|
386 |
|
---|
387 | typedef struct _TBitArray
|
---|
388 | {
|
---|
389 | DWORD NumberOfBits; // Total number of bits that are available
|
---|
390 | BYTE Elements[1]; // Array of elements (variable length)
|
---|
391 | } TBitArray;
|
---|
392 |
|
---|
393 | void GetBits(TBitArray * array, unsigned int nBitPosition, unsigned int nBitLength, void * pvBuffer, int nResultSize);
|
---|
394 | void SetBits(TBitArray * array, unsigned int nBitPosition, unsigned int nBitLength, void * pvBuffer, int nResultSize);
|
---|
395 |
|
---|
396 | // Structure for file bitmap. Used by SFileGetArchiveBitmap
|
---|
397 | typedef struct _TFileBitmap
|
---|
398 | {
|
---|
399 | ULONGLONG StartOffset; // Starting offset of the file, covered by bitmap
|
---|
400 | ULONGLONG EndOffset; // Ending offset of the file, covered by bitmap
|
---|
401 | DWORD IsComplete; // If nonzero, no blocks are missing
|
---|
402 | DWORD BitmapSize; // Size of the file bitmap (in bytes)
|
---|
403 | DWORD BlockSize; // Size of one block, in bytes
|
---|
404 | DWORD Reserved; // Alignment
|
---|
405 |
|
---|
406 | // Followed by file bitmap (variable length), array of BYTEs)
|
---|
407 | } TFileBitmap;
|
---|
408 |
|
---|
409 | //-----------------------------------------------------------------------------
|
---|
410 | // Structures related to MPQ format
|
---|
411 | //
|
---|
412 | // Note: All structures in this header file are supposed to remain private
|
---|
413 | // to StormLib. The structures may (and will) change over time, as the MPQ
|
---|
414 | // file format evolves. Programmers directly using these structures need to
|
---|
415 | // be aware of this. And the last, but not least, NEVER do any modifications
|
---|
416 | // to those structures directly, always use SFile* functions.
|
---|
417 | //
|
---|
418 |
|
---|
419 | #define MPQ_HEADER_SIZE_V1 0x20
|
---|
420 | #define MPQ_HEADER_SIZE_V2 0x2C
|
---|
421 | #define MPQ_HEADER_SIZE_V3 0x44
|
---|
422 | #define MPQ_HEADER_SIZE_V4 0xD0
|
---|
423 |
|
---|
424 | typedef struct _TMPQUserData
|
---|
425 | {
|
---|
426 | // The ID_MPQ_USERDATA ('MPQ\x1B') signature
|
---|
427 | DWORD dwID;
|
---|
428 |
|
---|
429 | // Maximum size of the user data
|
---|
430 | DWORD cbUserDataSize;
|
---|
431 |
|
---|
432 | // Offset of the MPQ header, relative to the begin of this header
|
---|
433 | DWORD dwHeaderOffs;
|
---|
434 |
|
---|
435 | // Appears to be size of user data header (Starcraft II maps)
|
---|
436 | DWORD cbUserDataHeader;
|
---|
437 | } TMPQUserData;
|
---|
438 |
|
---|
439 | // MPQ file header
|
---|
440 | //
|
---|
441 | // We have to make sure that the header is packed OK.
|
---|
442 | // Reason: A 64-bit integer at the beginning of 3.0 part,
|
---|
443 | // which is offset 0x2C
|
---|
444 | #pragma pack(push, 1)
|
---|
445 | typedef struct _TMPQHeader
|
---|
446 | {
|
---|
447 | // The ID_MPQ ('MPQ\x1A') signature
|
---|
448 | DWORD dwID;
|
---|
449 |
|
---|
450 | // Size of the archive header
|
---|
451 | DWORD dwHeaderSize;
|
---|
452 |
|
---|
453 | // 32-bit size of MPQ archive
|
---|
454 | // This field is deprecated in the Burning Crusade MoPaQ format, and the size of the archive
|
---|
455 | // is calculated as the size from the beginning of the archive to the end of the hash table,
|
---|
456 | // block table, or hi-block table (whichever is largest).
|
---|
457 | DWORD dwArchiveSize;
|
---|
458 |
|
---|
459 | // 0 = Format 1 (up to The Burning Crusade)
|
---|
460 | // 1 = Format 2 (The Burning Crusade and newer)
|
---|
461 | // 2 = Format 3 (WoW - Cataclysm beta or newer)
|
---|
462 | // 3 = Format 4 (WoW - Cataclysm beta or newer)
|
---|
463 | USHORT wFormatVersion;
|
---|
464 |
|
---|
465 | // Power of two exponent specifying the number of 512-byte disk sectors in each file sector
|
---|
466 | // in the archive. The size of each file sector in the archive is 512 * 2 ^ wSectorSize.
|
---|
467 | USHORT wSectorSize;
|
---|
468 |
|
---|
469 | // Offset to the beginning of the hash table, relative to the beginning of the archive.
|
---|
470 | DWORD dwHashTablePos;
|
---|
471 |
|
---|
472 | // Offset to the beginning of the block table, relative to the beginning of the archive.
|
---|
473 | DWORD dwBlockTablePos;
|
---|
474 |
|
---|
475 | // Number of entries in the hash table. Must be a power of two, and must be less than 2^16 for
|
---|
476 | // the original MoPaQ format, or less than 2^20 for the Burning Crusade format.
|
---|
477 | DWORD dwHashTableSize;
|
---|
478 |
|
---|
479 | // Number of entries in the block table
|
---|
480 | DWORD dwBlockTableSize;
|
---|
481 |
|
---|
482 | //-- MPQ HEADER v 2 -------------------------------------------
|
---|
483 |
|
---|
484 | // Offset to the beginning of array of 16-bit high parts of file offsets.
|
---|
485 | ULONGLONG HiBlockTablePos64;
|
---|
486 |
|
---|
487 | // High 16 bits of the hash table offset for large archives.
|
---|
488 | USHORT wHashTablePosHi;
|
---|
489 |
|
---|
490 | // High 16 bits of the block table offset for large archives.
|
---|
491 | USHORT wBlockTablePosHi;
|
---|
492 |
|
---|
493 | //-- MPQ HEADER v 3 -------------------------------------------
|
---|
494 |
|
---|
495 | // 64-bit version of the archive size
|
---|
496 | ULONGLONG ArchiveSize64;
|
---|
497 |
|
---|
498 | // 64-bit position of the BET table
|
---|
499 | ULONGLONG BetTablePos64;
|
---|
500 |
|
---|
501 | // 64-bit position of the HET table
|
---|
502 | ULONGLONG HetTablePos64;
|
---|
503 |
|
---|
504 | //-- MPQ HEADER v 4 -------------------------------------------
|
---|
505 |
|
---|
506 | // Compressed size of the hash table
|
---|
507 | ULONGLONG HashTableSize64;
|
---|
508 |
|
---|
509 | // Compressed size of the block table
|
---|
510 | ULONGLONG BlockTableSize64;
|
---|
511 |
|
---|
512 | // Compressed size of the hi-block table
|
---|
513 | ULONGLONG HiBlockTableSize64;
|
---|
514 |
|
---|
515 | // Compressed size of the HET block
|
---|
516 | ULONGLONG HetTableSize64;
|
---|
517 |
|
---|
518 | // Compressed size of the BET block
|
---|
519 | ULONGLONG BetTableSize64;
|
---|
520 |
|
---|
521 | // Size of raw data chunk to calculate MD5.
|
---|
522 | // MD5 of each data chunk follows the raw file data.
|
---|
523 | DWORD dwRawChunkSize;
|
---|
524 |
|
---|
525 | // MD5 of MPQ tables
|
---|
526 | unsigned char MD5_BlockTable[MD5_DIGEST_SIZE]; // MD5 of the block table before decryption
|
---|
527 | unsigned char MD5_HashTable[MD5_DIGEST_SIZE]; // MD5 of the hash table before decryption
|
---|
528 | unsigned char MD5_HiBlockTable[MD5_DIGEST_SIZE]; // MD5 of the hi-block table
|
---|
529 | unsigned char MD5_BetTable[MD5_DIGEST_SIZE]; // MD5 of the BET table before decryption
|
---|
530 | unsigned char MD5_HetTable[MD5_DIGEST_SIZE]; // MD5 of the HET table before decryption
|
---|
531 | unsigned char MD5_MpqHeader[MD5_DIGEST_SIZE]; // MD5 of the MPQ header from signature to (including) MD5_HetTable
|
---|
532 | } TMPQHeader;
|
---|
533 | #pragma pack(pop)
|
---|
534 |
|
---|
535 |
|
---|
536 | // Hash entry. All files in the archive are searched by their hashes.
|
---|
537 | typedef struct _TMPQHash
|
---|
538 | {
|
---|
539 | // The hash of the file path, using method A.
|
---|
540 | DWORD dwName1;
|
---|
541 |
|
---|
542 | // The hash of the file path, using method B.
|
---|
543 | DWORD dwName2;
|
---|
544 |
|
---|
545 | #ifdef PLATFORM_LITTLE_ENDIAN
|
---|
546 |
|
---|
547 | // The language of the file. This is a Windows LANGID data type, and uses the same values.
|
---|
548 | // 0 indicates the default language (American English), or that the file is language-neutral.
|
---|
549 | USHORT lcLocale;
|
---|
550 |
|
---|
551 | // The platform the file is used for. 0 indicates the default platform.
|
---|
552 | // No other values have been observed.
|
---|
553 | // Note: wPlatform is actually just BYTE, but since it has never been used, we don't care.
|
---|
554 | USHORT wPlatform;
|
---|
555 |
|
---|
556 | #else
|
---|
557 |
|
---|
558 | USHORT wPlatform;
|
---|
559 | USHORT lcLocale;
|
---|
560 |
|
---|
561 | #endif
|
---|
562 |
|
---|
563 | // If the hash table entry is valid, this is the index into the block table of the file.
|
---|
564 | // Otherwise, one of the following two values:
|
---|
565 | // - FFFFFFFFh: Hash table entry is empty, and has always been empty.
|
---|
566 | // Terminates searches for a given file.
|
---|
567 | // - FFFFFFFEh: Hash table entry is empty, but was valid at some point (a deleted file).
|
---|
568 | // Does not terminate searches for a given file.
|
---|
569 | DWORD dwBlockIndex;
|
---|
570 | } TMPQHash;
|
---|
571 |
|
---|
572 |
|
---|
573 | // File description block contains informations about the file
|
---|
574 | typedef struct _TMPQBlock
|
---|
575 | {
|
---|
576 | // Offset of the beginning of the file, relative to the beginning of the archive.
|
---|
577 | DWORD dwFilePos;
|
---|
578 |
|
---|
579 | // Compressed file size
|
---|
580 | DWORD dwCSize;
|
---|
581 |
|
---|
582 | // Only valid if the block is a file; otherwise meaningless, and should be 0.
|
---|
583 | // If the file is compressed, this is the size of the uncompressed file data.
|
---|
584 | DWORD dwFSize;
|
---|
585 |
|
---|
586 | // Flags for the file. See MPQ_FILE_XXXX constants
|
---|
587 | DWORD dwFlags;
|
---|
588 | } TMPQBlock;
|
---|
589 |
|
---|
590 | // Patch file information, preceding the sector offset table
|
---|
591 | typedef struct _TPatchInfo
|
---|
592 | {
|
---|
593 | DWORD dwLength; // Length of patch info header, in bytes
|
---|
594 | DWORD dwFlags; // Flags. 0x80000000 = MD5 (?)
|
---|
595 | DWORD dwDataSize; // Uncompressed size of the patch file
|
---|
596 | BYTE md5[0x10]; // MD5 of the entire patch file after decompression
|
---|
597 |
|
---|
598 | // Followed by the sector table (variable length)
|
---|
599 | } TPatchInfo;
|
---|
600 |
|
---|
601 | // Header for PTCH files
|
---|
602 | typedef struct _TPatchHeader
|
---|
603 | {
|
---|
604 | //-- PATCH header -----------------------------------
|
---|
605 | DWORD dwSignature; // 'PTCH'
|
---|
606 | DWORD dwSizeOfPatchData; // Size of the entire patch (decompressed)
|
---|
607 | DWORD dwSizeBeforePatch; // Size of the file before patch
|
---|
608 | DWORD dwSizeAfterPatch; // Size of file after patch
|
---|
609 |
|
---|
610 | //-- MD5 block --------------------------------------
|
---|
611 | DWORD dwMD5; // 'MD5_'
|
---|
612 | DWORD dwMd5BlockSize; // Size of the MD5 block, including the signature and size itself
|
---|
613 | BYTE md5_before_patch[0x10]; // MD5 of the original (unpached) file
|
---|
614 | BYTE md5_after_patch[0x10]; // MD5 of the patched file
|
---|
615 |
|
---|
616 | //-- XFRM block -------------------------------------
|
---|
617 | DWORD dwXFRM; // 'XFRM'
|
---|
618 | DWORD dwXfrmBlockSize; // Size of the XFRM block, includes XFRM header and patch data
|
---|
619 | DWORD dwPatchType; // Type of patch ('BSD0' or 'COPY')
|
---|
620 |
|
---|
621 | // Followed by the patch data
|
---|
622 | } TPatchHeader;
|
---|
623 |
|
---|
624 | #define SIZE_OF_XFRM_HEADER 0x0C
|
---|
625 |
|
---|
626 | // This is the combined file entry for maintaining file list in the MPQ.
|
---|
627 | // This structure is combined from block table, hi-block table,
|
---|
628 | // (attributes) file and from (listfile).
|
---|
629 | typedef struct _TFileEntry
|
---|
630 | {
|
---|
631 | ULONGLONG ByteOffset; // Position of the file content in the MPQ, relative to the MPQ header
|
---|
632 | ULONGLONG FileTime; // FileTime from the (attributes) file. 0 if not present.
|
---|
633 | ULONGLONG BetHash; // Lower part of the file name hash. Only used when the MPQ has BET table.
|
---|
634 | DWORD dwHashIndex; // Index to the hash table. Only used when the MPQ has classic hash table
|
---|
635 | DWORD dwHetIndex; // Index to the HET table. Only used when the MPQ has HET table
|
---|
636 | DWORD dwFileSize; // Decompressed size of the file
|
---|
637 | DWORD dwCmpSize; // Compressed size of the file (i.e., size of the file data in the MPQ)
|
---|
638 | DWORD dwFlags; // File flags (from block table)
|
---|
639 | USHORT lcLocale; // Locale ID for the file
|
---|
640 | USHORT wPlatform; // Platform ID for the file
|
---|
641 | DWORD dwCrc32; // CRC32 from (attributes) file. 0 if not present.
|
---|
642 | unsigned char md5[MD5_DIGEST_SIZE]; // File MD5 from the (attributes) file. 0 if not present.
|
---|
643 | char * szFileName; // File name. NULL if not known.
|
---|
644 | } TFileEntry;
|
---|
645 |
|
---|
646 | // Common header for HET and BET tables
|
---|
647 | typedef struct _TMPQExtTable
|
---|
648 | {
|
---|
649 | DWORD dwSignature; // 'HET\x1A' or 'BET\x1A'
|
---|
650 | DWORD dwVersion; // Version. Seems to be always 1
|
---|
651 | DWORD dwDataSize; // Size of the contained table
|
---|
652 |
|
---|
653 | // Followed by the table header
|
---|
654 | // Followed by the table data
|
---|
655 |
|
---|
656 | } TMPQExtTable;
|
---|
657 |
|
---|
658 | //
|
---|
659 | // MPQ data bitmap, can be found at (FileSize - sizeof(TMPQBlockMap))
|
---|
660 | //
|
---|
661 | // There is bit map of the entire MPQ before TMPQBitmap. Each 0x4000-byte
|
---|
662 | // block is represented by one bit (including the last, eventually incomplete block).
|
---|
663 | //
|
---|
664 | typedef struct _TMPQBitmap
|
---|
665 | {
|
---|
666 | DWORD dwSignature; // 'ptv3' (MPQ_BLOCK_MAP_SIGNATURE)
|
---|
667 | DWORD dwAlways3; // Unknown, seems to always have value of 3
|
---|
668 | DWORD dwBuildNumber; // Game build number for that MPQ
|
---|
669 | DWORD dwMapOffsetLo; // Low 32-bits of the offset of the bit map
|
---|
670 | DWORD dwMapOffsetHi; // High 32-bits of the offset of the bit map
|
---|
671 | DWORD dwBlockSize; // Size of one block (usually 0x4000 bytes)
|
---|
672 | } TMPQBitmap;
|
---|
673 |
|
---|
674 | // Structure for parsed HET table
|
---|
675 | typedef struct _TMPQHetTable
|
---|
676 | {
|
---|
677 | TBitArray * pBetIndexes; // Bit array of indexes to BET tables
|
---|
678 | LPBYTE pHetHashes; // Array of HET hashes. Each entry has size of 1 byte
|
---|
679 | ULONGLONG AndMask64; // AND mask used for calculating file name hash
|
---|
680 | ULONGLONG OrMask64; // OR mask used for setting the highest bit of the file name hash
|
---|
681 |
|
---|
682 | DWORD dwIndexSizeTotal; // Total size of one entry in pBetIndexes (in bits)
|
---|
683 | DWORD dwIndexSizeExtra; // Extra bits in the entry in pBetIndexes
|
---|
684 | DWORD dwIndexSize; // Effective size of one entry in pBetIndexes (in bits)
|
---|
685 | DWORD dwMaxFileCount; // Maximum number of files in the MPQ
|
---|
686 | DWORD dwHashTableSize; // Number of entries in pBetHashes
|
---|
687 | DWORD dwHashBitSize; // Effective number of bits in the hash
|
---|
688 | } TMPQHetTable;
|
---|
689 |
|
---|
690 | // Structure for parsed BET table
|
---|
691 | typedef struct _TMPQBetTable
|
---|
692 | {
|
---|
693 | TBitArray * pBetHashes; // Array of BET hashes
|
---|
694 | TBitArray * pFileTable; // Bit-based file table
|
---|
695 | LPDWORD pFileFlags; // Array of file flags
|
---|
696 |
|
---|
697 | DWORD dwTableEntrySize; // Size of one table entry, in bits
|
---|
698 | DWORD dwBitIndex_FilePos; // Bit index of the file position in the table entry
|
---|
699 | DWORD dwBitIndex_FileSize; // Bit index of the file size in the table entry
|
---|
700 | DWORD dwBitIndex_CmpSize; // Bit index of the compressed size in the table entry
|
---|
701 | DWORD dwBitIndex_FlagIndex; // Bit index of the flag index in the table entry
|
---|
702 | DWORD dwBitIndex_Unknown; // Bit index of ??? in the table entry
|
---|
703 | DWORD dwBitCount_FilePos; // Size of file offset (in bits) within table entry
|
---|
704 | DWORD dwBitCount_FileSize; // Size of file size (in bits) within table entry
|
---|
705 | DWORD dwBitCount_CmpSize; // Size of compressed file size (in bits) within table entry
|
---|
706 | DWORD dwBitCount_FlagIndex; // Size of flag index (in bits) within table entry
|
---|
707 | DWORD dwBitCount_Unknown; // Size of ??? (in bits) within table entry
|
---|
708 | DWORD dwBetHashSizeTotal; // Total size of bet hash
|
---|
709 | DWORD dwBetHashSizeExtra; // Extra bits in the bet hash
|
---|
710 | DWORD dwBetHashSize; // Effective size of the bet hash
|
---|
711 | DWORD dwFileCount; // Number of files (usually equal to maximum number of files)
|
---|
712 | DWORD dwFlagCount; // Number of entries in pFileFlags
|
---|
713 | } TMPQBetTable;
|
---|
714 |
|
---|
715 | // Archive handle structure
|
---|
716 | typedef struct _TMPQArchive
|
---|
717 | {
|
---|
718 | TFileStream * pStream; // Open stream for the MPQ
|
---|
719 |
|
---|
720 | ULONGLONG UserDataPos; // Position of user data (relative to the begin of the file)
|
---|
721 | ULONGLONG MpqPos; // MPQ header offset (relative to the begin of the file)
|
---|
722 |
|
---|
723 | struct _TMPQArchive * haPatch; // Pointer to patch archive, if any
|
---|
724 | struct _TMPQArchive * haBase; // Pointer to base ("previous version") archive, if any
|
---|
725 | char szPatchPrefix[MPQ_PATCH_PREFIX_LEN]; // Prefix for file names in patch MPQs
|
---|
726 | size_t cchPatchPrefix; // Length of the patch prefix, in characters
|
---|
727 |
|
---|
728 | TMPQUserData * pUserData; // MPQ user data (NULL if not present in the file)
|
---|
729 | TMPQHeader * pHeader; // MPQ file header
|
---|
730 | TMPQBitmap * pBitmap; // MPQ bitmap
|
---|
731 | TMPQHash * pHashTable; // Hash table
|
---|
732 | TMPQHetTable * pHetTable; // Het table
|
---|
733 | TFileEntry * pFileTable; // File table
|
---|
734 |
|
---|
735 | TMPQUserData UserData; // MPQ user data. Valid only when ID_MPQ_USERDATA has been found
|
---|
736 | BYTE HeaderData[MPQ_HEADER_SIZE_V4]; // Storage for MPQ header
|
---|
737 |
|
---|
738 | DWORD dwHETBlockSize;
|
---|
739 | DWORD dwBETBlockSize;
|
---|
740 | DWORD dwFileTableSize; // Current size of the file table, e.g. index of the entry past the last occupied one
|
---|
741 | DWORD dwMaxFileCount; // Maximum number of files in the MPQ
|
---|
742 | DWORD dwSectorSize; // Default size of one file sector
|
---|
743 | DWORD dwFileFlags1; // Flags for (listfile)
|
---|
744 | DWORD dwFileFlags2; // Flags for (attributes)
|
---|
745 | DWORD dwAttrFlags; // Flags for the (attributes) file, see MPQ_ATTRIBUTE_XXX
|
---|
746 | DWORD dwFlags; // See MPQ_FLAG_XXXXX
|
---|
747 |
|
---|
748 | SFILE_ADDFILE_CALLBACK pfnAddFileCB; // Callback function for adding files
|
---|
749 | void * pvAddFileUserData; // User data thats passed to the callback
|
---|
750 |
|
---|
751 | SFILE_COMPACT_CALLBACK pfnCompactCB; // Callback function for compacting the archive
|
---|
752 | ULONGLONG CompactBytesProcessed; // Amount of bytes that have been processed during a particular compact call
|
---|
753 | ULONGLONG CompactTotalBytes; // Total amount of bytes to be compacted
|
---|
754 | void * pvCompactUserData; // User data thats passed to the callback
|
---|
755 | } TMPQArchive;
|
---|
756 |
|
---|
757 | // File handle structure
|
---|
758 | typedef struct _TMPQFile
|
---|
759 | {
|
---|
760 | TFileStream * pStream; // File stream. Only used on local files
|
---|
761 | TMPQArchive * ha; // Archive handle
|
---|
762 | TFileEntry * pFileEntry; // File entry for the file
|
---|
763 | DWORD dwFileKey; // Decryption key
|
---|
764 | DWORD dwFilePos; // Current file position
|
---|
765 | ULONGLONG RawFilePos; // Offset in MPQ archive (relative to file begin)
|
---|
766 | ULONGLONG MpqFilePos; // Offset in MPQ archive (relative to MPQ header)
|
---|
767 | DWORD dwMagic; // 'FILE'
|
---|
768 |
|
---|
769 | struct _TMPQFile * hfPatchFile; // Pointer to opened patch file
|
---|
770 | TPatchHeader * pPatchHeader; // Patch header. Only used if the file is a patch file
|
---|
771 | LPBYTE pbFileData; // Loaded and patched file data. Only used if the file is a patch file
|
---|
772 | DWORD cbFileData; // Size of loaded patched data
|
---|
773 |
|
---|
774 | TPatchInfo * pPatchInfo; // Patch info block, preceding the sector table
|
---|
775 | DWORD * SectorOffsets; // Position of each file sector, relative to the begin of the file. Only for compressed files.
|
---|
776 | DWORD * SectorChksums; // Array of sector checksums (either ADLER32 or MD5) values for each file sector
|
---|
777 | DWORD dwSectorCount; // Number of sectors in the file
|
---|
778 | DWORD dwPatchedFileSize; // Size of patched file. Used when saving patch file to the MPQ
|
---|
779 | DWORD dwDataSize; // Size of data in the file (on patch files, this differs from file size in block table entry)
|
---|
780 |
|
---|
781 | LPBYTE pbFileSector; // Last loaded file sector. For single unit files, entire file content
|
---|
782 | DWORD dwSectorOffs; // File position of currently loaded file sector
|
---|
783 | DWORD dwSectorSize; // Size of the file sector. For single unit files, this is equal to the file size
|
---|
784 |
|
---|
785 | unsigned char hctx[HASH_STATE_SIZE];// Hash state for MD5. Used when saving file to MPQ
|
---|
786 | DWORD dwCrc32; // CRC32 value, used when saving file to MPQ
|
---|
787 |
|
---|
788 | bool bLoadedSectorCRCs; // If true, we already tried to load sector CRCs
|
---|
789 | bool bCheckSectorCRCs; // If true, then SFileReadFile will check sector CRCs when reading the file
|
---|
790 | bool bIsWriteHandle; // If true, this handle has been created by SFileCreateFile
|
---|
791 | bool bErrorOccured; // If true, then at least one error occured during saving the file to the archive
|
---|
792 | } TMPQFile;
|
---|
793 |
|
---|
794 | // Structure for SFileFindFirstFile and SFileFindNextFile
|
---|
795 | typedef struct _SFILE_FIND_DATA
|
---|
796 | {
|
---|
797 | char cFileName[MAX_PATH]; // Full name of the found file
|
---|
798 | char * szPlainName; // Plain name of the found file
|
---|
799 | DWORD dwHashIndex; // Hash table index for the file
|
---|
800 | DWORD dwBlockIndex; // Block table index for the file
|
---|
801 | DWORD dwFileSize; // File size in bytes
|
---|
802 | DWORD dwFileFlags; // MPQ file flags
|
---|
803 | DWORD dwCompSize; // Compressed file size
|
---|
804 | DWORD dwFileTimeLo; // Low 32-bits of the file time (0 if not present)
|
---|
805 | DWORD dwFileTimeHi; // High 32-bits of the file time (0 if not present)
|
---|
806 | LCID lcLocale; // Locale version
|
---|
807 |
|
---|
808 | } SFILE_FIND_DATA, *PSFILE_FIND_DATA;
|
---|
809 |
|
---|
810 | typedef struct _SFILE_CREATE_MPQ
|
---|
811 | {
|
---|
812 | DWORD cbSize; // Size of this structure, in bytes
|
---|
813 | DWORD dwMpqVersion; // Version of the MPQ to be created
|
---|
814 | void *pvUserData; // Reserved, must be NULL
|
---|
815 | DWORD cbUserData; // Reserved, must be 0
|
---|
816 | DWORD dwStreamFlags; // Stream flags for creating the MPQ
|
---|
817 | DWORD dwFileFlags1; // File flags for (listfile). 0 = default
|
---|
818 | DWORD dwFileFlags2; // File flags for (attributes). 0 = default
|
---|
819 | DWORD dwAttrFlags; // Flags for the (attributes) file. If 0, no attributes will be created
|
---|
820 | DWORD dwSectorSize; // Sector size for compressed files
|
---|
821 | DWORD dwRawChunkSize; // Size of raw data chunk
|
---|
822 | DWORD dwMaxFileCount; // File limit for the MPQ
|
---|
823 |
|
---|
824 | } SFILE_CREATE_MPQ, *PSFILE_CREATE_MPQ;
|
---|
825 |
|
---|
826 | //-----------------------------------------------------------------------------
|
---|
827 | // Stream support - functions
|
---|
828 |
|
---|
829 | TFileStream * FileStream_CreateFile(const TCHAR * szFileName, DWORD dwStreamFlags);
|
---|
830 | TFileStream * FileStream_OpenFile(const TCHAR * szFileName, DWORD dwStreamFlags);
|
---|
831 | TCHAR * FileStream_GetFileName(TFileStream * pStream);
|
---|
832 | bool FileStream_IsReadOnly(TFileStream * pStream);
|
---|
833 | bool FileStream_Read(TFileStream * pStream, ULONGLONG * pByteOffset, void * pvBuffer, DWORD dwBytesToRead);
|
---|
834 | bool FileStream_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const void * pvBuffer, DWORD dwBytesToWrite);
|
---|
835 | bool FileStream_GetPos(TFileStream * pStream, ULONGLONG * pByteOffset);
|
---|
836 | bool FileStream_SetPos(TFileStream * pStream, ULONGLONG ByteOffset);
|
---|
837 | bool FileStream_GetSize(TFileStream * pStream, ULONGLONG * pFileSize);
|
---|
838 | bool FileStream_SetSize(TFileStream * pStream, ULONGLONG NewFileSize);
|
---|
839 | bool FileStream_GetTime(TFileStream * pStream, ULONGLONG * pFT);
|
---|
840 | bool FileStream_GetFlags(TFileStream * pStream, LPDWORD pdwStreamFlags);
|
---|
841 | bool FileStream_Switch(TFileStream * pStream, TFileStream * pTempStream);
|
---|
842 | bool FileStream_SetBitmap(TFileStream * pStream, TFileBitmap * pBitmap);
|
---|
843 | bool FileStream_GetBitmap(TFileStream * pStream, TFileBitmap * pBitmap, DWORD Length, LPDWORD LengthNeeded);
|
---|
844 | void FileStream_Close(TFileStream * pStream);
|
---|
845 |
|
---|
846 | //-----------------------------------------------------------------------------
|
---|
847 | // Functions prototypes for Storm.dll
|
---|
848 |
|
---|
849 | // Typedefs for functions exported by Storm.dll
|
---|
850 | typedef LCID (WINAPI * SFILESETLOCALE)(LCID);
|
---|
851 | typedef bool (WINAPI * SFILEOPENARCHIVE)(const char *, DWORD, DWORD, HANDLE *);
|
---|
852 | typedef bool (WINAPI * SFILECLOSEARCHIVE)(HANDLE);
|
---|
853 | typedef bool (WINAPI * SFILEOPENFILEEX)(HANDLE, const char *, DWORD, HANDLE *);
|
---|
854 | typedef bool (WINAPI * SFILECLOSEFILE)(HANDLE);
|
---|
855 | typedef DWORD (WINAPI * SFILEGETFILESIZE)(HANDLE, LPDWORD);
|
---|
856 | typedef DWORD (WINAPI * SFILESETFILEPOINTER)(HANDLE, LONG, LONG *, DWORD);
|
---|
857 | typedef bool (WINAPI * SFILEREADFILE)(HANDLE, void *, DWORD, LPDWORD, LPOVERLAPPED);
|
---|
858 |
|
---|
859 | //-----------------------------------------------------------------------------
|
---|
860 | // Functions for manipulation with StormLib global flags
|
---|
861 |
|
---|
862 | LCID WINAPI SFileGetLocale();
|
---|
863 | LCID WINAPI SFileSetLocale(LCID lcNewLocale);
|
---|
864 |
|
---|
865 | //-----------------------------------------------------------------------------
|
---|
866 | // Functions for archive manipulation
|
---|
867 |
|
---|
868 | bool WINAPI SFileOpenArchive(const TCHAR * szMpqName, DWORD dwPriority, DWORD dwFlags, HANDLE * phMpq);
|
---|
869 | bool WINAPI SFileCreateArchive(const TCHAR * szMpqName, DWORD dwFlags, DWORD dwMaxFileCount, HANDLE * phMpq);
|
---|
870 | bool WINAPI SFileCreateArchive2(const TCHAR * szMpqName, PSFILE_CREATE_MPQ pCreateInfo, HANDLE * phMpq);
|
---|
871 |
|
---|
872 | bool WINAPI SFileGetArchiveBitmap(HANDLE hMpq, TFileBitmap * pBitmap, DWORD Length, LPDWORD LengthNeeded);
|
---|
873 | bool WINAPI SFileFlushArchive(HANDLE hMpq);
|
---|
874 | bool WINAPI SFileCloseArchive(HANDLE hMpq);
|
---|
875 |
|
---|
876 | // Adds another listfile into MPQ. The currently added listfile(s) remain,
|
---|
877 | // so you can use this API to combining more listfiles.
|
---|
878 | // Note that this function is internally called by SFileFindFirstFile
|
---|
879 | int WINAPI SFileAddListFile(HANDLE hMpq, const char * szListFile);
|
---|
880 |
|
---|
881 | // Archive compacting
|
---|
882 | bool WINAPI SFileSetCompactCallback(HANDLE hMpq, SFILE_COMPACT_CALLBACK CompactCB, void * pvUserData);
|
---|
883 | bool WINAPI SFileCompactArchive(HANDLE hMpq, const char * szListFile, bool bReserved);
|
---|
884 |
|
---|
885 | // Changing the maximum file count
|
---|
886 | DWORD WINAPI SFileGetMaxFileCount(HANDLE hMpq);
|
---|
887 | bool WINAPI SFileSetMaxFileCount(HANDLE hMpq, DWORD dwMaxFileCount);
|
---|
888 |
|
---|
889 | // Changing (attributes) file
|
---|
890 | DWORD WINAPI SFileGetAttributes(HANDLE hMpq);
|
---|
891 | bool WINAPI SFileSetAttributes(HANDLE hMpq, DWORD dwFlags);
|
---|
892 | bool WINAPI SFileUpdateFileAttributes(HANDLE hMpq, const char * szFileName);
|
---|
893 |
|
---|
894 | //-----------------------------------------------------------------------------
|
---|
895 | // Functions for manipulation with patch archives
|
---|
896 |
|
---|
897 | bool WINAPI SFileOpenPatchArchive(HANDLE hMpq, const TCHAR * szPatchMpqName, const char * szPatchPathPrefix, DWORD dwFlags);
|
---|
898 | bool WINAPI SFileIsPatchedArchive(HANDLE hMpq);
|
---|
899 |
|
---|
900 | //-----------------------------------------------------------------------------
|
---|
901 | // Functions for file manipulation
|
---|
902 |
|
---|
903 | // Reading from MPQ file
|
---|
904 | bool WINAPI SFileOpenFileEx(HANDLE hMpq, const char * szFileName, DWORD dwSearchScope, HANDLE * phFile);
|
---|
905 | DWORD WINAPI SFileGetFileSize(HANDLE hFile, LPDWORD pdwFileSizeHigh);
|
---|
906 | DWORD WINAPI SFileSetFilePointer(HANDLE hFile, LONG lFilePos, LONG * plFilePosHigh, DWORD dwMoveMethod);
|
---|
907 | bool WINAPI SFileReadFile(HANDLE hFile, void * lpBuffer, DWORD dwToRead, LPDWORD pdwRead, LPOVERLAPPED lpOverlapped);
|
---|
908 | bool WINAPI SFileCloseFile(HANDLE hFile);
|
---|
909 |
|
---|
910 | // Retrieving info about the file
|
---|
911 | bool WINAPI SFileHasFile(HANDLE hMpq, const char * szFileName);
|
---|
912 | bool WINAPI SFileGetFileName(HANDLE hFile, char * szFileName);
|
---|
913 | bool WINAPI SFileGetFileInfo(HANDLE hMpqOrFile, DWORD dwInfoType, void * pvFileInfo, DWORD cbFileInfo, LPDWORD pcbLengthNeeded);
|
---|
914 |
|
---|
915 | // High-level extract function
|
---|
916 | bool WINAPI SFileExtractFile(HANDLE hMpq, const char * szToExtract, const TCHAR * szExtracted, DWORD dwSearchScope);
|
---|
917 |
|
---|
918 | //-----------------------------------------------------------------------------
|
---|
919 | // Functions for file and archive verification
|
---|
920 |
|
---|
921 | // Generates file CRC32
|
---|
922 | bool WINAPI SFileGetFileChecksums(HANDLE hMpq, const char * szFileName, LPDWORD pdwCrc32, char * pMD5);
|
---|
923 |
|
---|
924 | // Verifies file against its checksums stored in (attributes) attributes (depending on dwFlags).
|
---|
925 | // For dwFlags, use one or more of MPQ_ATTRIBUTE_MD5
|
---|
926 | DWORD WINAPI SFileVerifyFile(HANDLE hMpq, const char * szFileName, DWORD dwFlags);
|
---|
927 |
|
---|
928 | // Verifies raw data of the archive. Only works for MPQs version 4 or newer
|
---|
929 | int WINAPI SFileVerifyRawData(HANDLE hMpq, DWORD dwWhatToVerify, const char * szFileName);
|
---|
930 |
|
---|
931 | // Verifies the signature, if present
|
---|
932 | DWORD WINAPI SFileVerifyArchive(HANDLE hMpq);
|
---|
933 |
|
---|
934 | //-----------------------------------------------------------------------------
|
---|
935 | // Functions for file searching
|
---|
936 |
|
---|
937 | HANDLE WINAPI SFileFindFirstFile(HANDLE hMpq, const char * szMask, SFILE_FIND_DATA * lpFindFileData, const char * szListFile);
|
---|
938 | bool WINAPI SFileFindNextFile(HANDLE hFind, SFILE_FIND_DATA * lpFindFileData);
|
---|
939 | bool WINAPI SFileFindClose(HANDLE hFind);
|
---|
940 |
|
---|
941 | HANDLE WINAPI SListFileFindFirstFile(HANDLE hMpq, const char * szListFile, const char * szMask, SFILE_FIND_DATA * lpFindFileData);
|
---|
942 | bool WINAPI SListFileFindNextFile(HANDLE hFind, SFILE_FIND_DATA * lpFindFileData);
|
---|
943 | bool WINAPI SListFileFindClose(HANDLE hFind);
|
---|
944 |
|
---|
945 | // Locale support
|
---|
946 | int WINAPI SFileEnumLocales(HANDLE hMpq, const char * szFileName, LCID * plcLocales, LPDWORD pdwMaxLocales, DWORD dwSearchScope);
|
---|
947 |
|
---|
948 | //-----------------------------------------------------------------------------
|
---|
949 | // Support for adding files to the MPQ
|
---|
950 |
|
---|
951 | bool WINAPI SFileCreateFile(HANDLE hMpq, const char * szArchivedName, ULONGLONG FileTime, DWORD dwFileSize, LCID lcLocale, DWORD dwFlags, HANDLE * phFile);
|
---|
952 | bool WINAPI SFileWriteFile(HANDLE hFile, const void * pvData, DWORD dwSize, DWORD dwCompression);
|
---|
953 | bool WINAPI SFileFinishFile(HANDLE hFile);
|
---|
954 |
|
---|
955 | bool WINAPI SFileAddFileEx(HANDLE hMpq, const TCHAR * szFileName, const char * szArchivedName, DWORD dwFlags, DWORD dwCompression, DWORD dwCompressionNext);
|
---|
956 | bool WINAPI SFileAddFile(HANDLE hMpq, const TCHAR * szFileName, const char * szArchivedName, DWORD dwFlags);
|
---|
957 | bool WINAPI SFileAddWave(HANDLE hMpq, const TCHAR * szFileName, const char * szArchivedName, DWORD dwFlags, DWORD dwQuality);
|
---|
958 | bool WINAPI SFileRemoveFile(HANDLE hMpq, const char * szFileName, DWORD dwSearchScope);
|
---|
959 | bool WINAPI SFileRenameFile(HANDLE hMpq, const char * szOldFileName, const char * szNewFileName);
|
---|
960 | bool WINAPI SFileSetFileLocale(HANDLE hFile, LCID lcNewLocale);
|
---|
961 | bool WINAPI SFileSetDataCompression(DWORD DataCompression);
|
---|
962 |
|
---|
963 | bool WINAPI SFileSetAddFileCallback(HANDLE hMpq, SFILE_ADDFILE_CALLBACK AddFileCB, void * pvUserData);
|
---|
964 |
|
---|
965 | //-----------------------------------------------------------------------------
|
---|
966 | // Compression and decompression
|
---|
967 |
|
---|
968 | int WINAPI SCompImplode (void * pvOutBuffer, int * pcbOutBuffer, void * pvInBuffer, int cbInBuffer);
|
---|
969 | int WINAPI SCompExplode (void * pvOutBuffer, int * pcbOutBuffer, void * pvInBuffer, int cbInBuffer);
|
---|
970 | int WINAPI SCompCompress (void * pvOutBuffer, int * pcbOutBuffer, void * pvInBuffer, int cbInBuffer, unsigned uCompressionMask, int nCmpType, int nCmpLevel);
|
---|
971 | int WINAPI SCompDecompress (void * pvOutBuffer, int * pcbOutBuffer, void * pvInBuffer, int cbInBuffer);
|
---|
972 | int WINAPI SCompDecompress2(void * pvOutBuffer, int * pcbOutBuffer, void * pvInBuffer, int cbInBuffer);
|
---|
973 |
|
---|
974 | //-----------------------------------------------------------------------------
|
---|
975 | // Non-Windows support for SetLastError/GetLastError
|
---|
976 |
|
---|
977 | #ifndef PLATFORM_WINDOWS
|
---|
978 |
|
---|
979 | void SetLastError(int err);
|
---|
980 | int GetLastError();
|
---|
981 |
|
---|
982 | #endif
|
---|
983 |
|
---|
984 | //-----------------------------------------------------------------------------
|
---|
985 | // Functions from Storm.dll. They use slightly different names for keeping
|
---|
986 | // possibility to use them together with StormLib (StormXXX instead of SFileXXX)
|
---|
987 |
|
---|
988 | #ifdef __LINK_STORM_DLL__
|
---|
989 | #define STORM_ALTERNATE_NAMES // Force storm_dll.h to use alternate fnc names
|
---|
990 | #include "..\storm_dll\storm_dll.h"
|
---|
991 | #endif // __LINK_STORM_DLL__
|
---|
992 |
|
---|
993 | #ifdef __cplusplus
|
---|
994 | } // extern "C"
|
---|
995 | #endif
|
---|
996 |
|
---|
997 | #endif // __STORMLIB_H__
|
---|