Current News
Archived News
Search News
Discussion Forum


Old Forum
Install Programs More Downloads...
Troubleshooting
Source Code
Format Specs.
Misc. Information
Non-SF Stuff
Links




1 Attribute VB_Name = "SFmpqapi"
2 Option Explicit
4 '  ShadowFlare MPQ API Library. (c) ShadowFlare Software 2002-2008
5 '  License information for this code is in license.txt
7 '  All functions below are actual functions that are part of this
8 '  library and do not need any additional dll files.  It does not
9 '  even require Storm to be able to decompress or compress files.
11 '  This library emulates the interface of Lmpqapi and Storm MPQ
12 '  functions, so it may be used as a replacement for them in
13 '  MPQ extractors/archivers without even needing to recompile
14 '  the program that uses Lmpqapi or Storm.  It has a few features
15 '  not included in Lmpqapi and Storm, such as extra flags for some
16 '  functions, setting the locale ID of existing files, and adding
17 '  files without having to write them somewhere else first.  Also,
18 '  MPQ handles used by functions prefixed with "SFile" and "Mpq"
19 '  can be used interchangably; all functions use the same type
20 '  of MPQ handles.  You cannot, however, use handles from this
21 '  library with storm or lmpqapi or vice-versa.  Doing so will
22 '  most likely result in a crash.
24 '  Revision History:
25 '  06/12/2002 1.07 (ShadowFlare)
26 '  - No longer requires Storm.dll to compress or decompress
27 '    Warcraft III files
28 '  - Added SFileListFiles for getting names and information
29 '    about all of the files in an archive
30 '  - Fixed a bug with renaming and deleting files
31 '  - Fixed a bug with adding wave compressed files with
32 '    low compression setting
33 '  - Added a check in MpqOpenArchiveForUpdate for proper
34 '    dwMaximumFilesInArchive values (should be a number that
35 '    is a power of 2).  If it is not a proper value, it will
36 '    be rounded up to the next higher power of 2
38 '  05/09/2002 1.06 (ShadowFlare)
39 '  - Compresses files without Storm.dll!
40 '  - If Warcraft III is installed, this library will be able to
41 '    find Storm.dll on its own. (Storm.dll is needed to
42 '    decompress Warcraft III files)
43 '  - Fixed a bug where an embedded archive and the file that
44 '    contains it would be corrupted if the archive was modified
45 '  - Able to open all .w3m maps now
47 '  29/06/2002 1.05 (ShadowFlare)
48 '  - Supports decompressing files from Warcraft III MPQ archives
49 '    if using Storm.dll from Warcraft III
50 '  - Added MpqAddFileToArchiveEx and MpqAddFileFromBufferEx for
51 '    using extra compression types
53 '  29/05/2002 1.04 (ShadowFlare)
54 '  - Files can be compressed now!
55 '  - Fixed a bug in SFileReadFile when reading data not aligned
56 '    to the block size
57 '  - Optimized some of SFileReadFile's code.  It can read files
58 '    faster now
59 '  - SFile functions may now be used to access files not in mpq
60 '    archives as you can with the real storm functions
61 '  - MpqCompactArchive will no longer corrupt files with the
62 '    MODCRYPTKEY flag as long as the file is either compressed,
63 '    listed in "(listfile)", is "(listfile)", or is located in
64 '    the same place in the compacted archive; so it is safe
65 '    enough to use it on almost any archive
66 '  - Added MpqAddWaveFromBuffer
67 '  - Better handling of archives with no files
68 '  - Fixed compression with COMPRESS2 flag
70 '  15/05/2002 1.03 (ShadowFlare)
71 '  - Supports adding files with the compression attribute (does
72 '    not actually compress files).  Now archives created with
73 '    this dll can have files added to them through lmpqapi
74 '    without causing staredit to crash
75 '  - SFileGetBasePath and SFileSetBasePath work more like their
76 '    Storm equivalents now
77 '  - Implemented MpqCompactArchive, but it is not finished yet.
78 '    In its current state, I would recommend against using it
79 '    on archives that contain files with the MODCRYPTKEY flag,
80 '    since it will corrupt any files with that flag
81 '  - Added SFMpqGetVersionString2 which may be used in Visual
82 '    Basic to get the version string
84 '  07/05/2002 1.02 (ShadowFlare)
85 '  - SFileReadFile no longer passes the lpOverlapped parameter it
86 '    receives to ReadFile.  This is what was causing the function
87 '    to fail when used in Visual Basic
88 '  - Added support for more Storm MPQ functions
89 '  - GetLastError may now be used to get information about why a
90 '    function failed
92 '  01/05/2002 1.01 (ShadowFlare)
93 '  - Added ordinals for Storm MPQ functions
94 '  - Fixed MPQ searching functionality of SFileOpenFileEx
95 '  - Added a check for whether a valid handle is given when
96 '    SFileCloseArchive is called
97 '  - Fixed functionality of SFileSetArchivePriority when multiple
98 '    files are open
99 '  - File renaming works for all filenames now
100 '  - SFileReadFile no longer reallocates the buffer for each block
101 '    that is decompressed.  This should make SFileReadFile at least
102 '    a little faster
104 '  30/04/2002 1.00 (ShadowFlare)
105 '  - First version.
106 '  - Compression not yet supported
107 '  - Does not use SetLastError yet, so GetLastError will not return any
108 '    errors that have to do with this library
109 '  - MpqCompactArchive not implemented
111 '  Any comments or suggestions are accepted at blakflare@hotmail.com (ShadowFlare)
113 Type SFMPQVERSION
114     Major As Integer
115     Minor As Integer
116     Revision As Integer
117     Subrevision As Integer
118 End Type
120 ' MpqInitialize does nothing.  It is only provided for
121 ' compatibility with MPQ archivers that use lmpqapi.
122 Declare Function MpqInitialize Lib "SFmpq.dll" () As Boolean
124 Declare Function MpqGetVersionString Lib "SFmpq.dll" () As String
125 Declare Function MpqGetVersion Lib "SFmpq.dll" () As Single
127 Declare Sub SFMpqDestroy Lib "SFmpq.dll" () ' This no longer needs to be called.  It is only provided for compatibility with older versions
129 ' SFMpqGetVersionString2's return value is the required length of the buffer plus
130 ' the terminating null, so use SFMpqGetVersionString2(ByVal 0&, 0) to get the length.
131 Declare Function SFMpqGetVersionString Lib "SFmpq.dll" () As String
132 Declare Function SFMpqGetVersionString2 Lib "SFmpq.dll" (ByVal lpBuffer As String, ByVal dwBufferLength As Long) As Long
133 Declare Function SFMpqGetVersion Lib "SFmpq.dll" () As SFMPQVERSION
135 ' General error codes
136 Public Const MPQ_ERROR_MPQ_INVALID As Long = &H85200065
137 Public Const MPQ_ERROR_FILE_NOT_FOUND As Long = &H85200066
138 Public Const MPQ_ERROR_DISK_FULL As Long = &H85200068 'Physical write file to MPQ failed. Not sure of exact meaning
139 Public Const MPQ_ERROR_HASH_TABLE_FULL As Long = &H85200069
140 Public Const MPQ_ERROR_ALREADY_EXISTS As Long = &H8520006A
141 Public Const MPQ_ERROR_BAD_OPEN_MODE As Long = &H8520006C 'When MOAU_READ_ONLY is used without MOAU_OPEN_EXISTING
143 Public Const MPQ_ERROR_COMPACT_ERROR As Long = &H85300001
145 ' MpqOpenArchiveForUpdate flags
146 Public Const MOAU_CREATE_NEW As Long = &H0
147 Public Const MOAU_CREATE_ALWAYS As Long = &H8 'Was wrongly named MOAU_CREATE_NEW
148 Public Const MOAU_OPEN_EXISTING As Long = &H4
149 Public Const MOAU_OPEN_ALWAYS As Long = &H20
150 Public Const MOAU_READ_ONLY As Long = &H10 'Must be used with MOAU_OPEN_EXISTING
151 Public Const MOAU_MAINTAIN_LISTFILE As Long = &H1
153 ' MpqAddFileToArchive flags
154 Public Const MAFA_EXISTS As Long = &H80000000 'Will be added if not present
155 Public Const MAFA_UNKNOWN40000000 As Long = &H40000000
156 Public Const MAFA_MODCRYPTKEY As Long = &H20000
157 Public Const MAFA_ENCRYPT As Long = &H10000
158 Public Const MAFA_COMPRESS As Long = &H200
159 Public Const MAFA_COMPRESS2 As Long = &H100
160 Public Const MAFA_REPLACE_EXISTING As Long = &H1
162 ' MpqAddFileToArchiveEx compression flags
163 Public Const MAFA_COMPRESS_STANDARD As Long = &H8 'Standard PKWare DCL compression
164 Public Const MAFA_COMPRESS_DEFLATE  As Long = &H2 'ZLib's deflate compression
165 Public Const MAFA_COMPRESS_WAVE    As Long = &H81 'Standard wave compression
166 Public Const MAFA_COMPRESS_WAVE2   As Long = &H41 'Unused wave compression
168 ' Flags for individual compression types used for wave compression
169 Public Const MAFA_COMPRESS_WAVECOMP1 As Long = &H80 'Main compressor for standard wave compression
170 Public Const MAFA_COMPRESS_WAVECOMP2 As Long = &H40 'Main compressor for unused wave compression
171 Public Const MAFA_COMPRESS_WAVECOMP3  As Long = &H1 'Secondary compressor for wave compression
173 ' ZLib deflate compression level constants (used with MpqAddFileToArchiveEx and MpqAddFileFromBufferEx)
174 Public Const Z_NO_COMPRESSION         As Long = 0
175 Public Const Z_BEST_SPEED             As Long = 1
176 Public Const Z_BEST_COMPRESSION       As Long = 9
177 Public Const Z_DEFAULT_COMPRESSION  As Long = (-1)
179 ' MpqAddWAVToArchive quality flags
180 Public Const MAWA_QUALITY_HIGH As Long = 1
181 Public Const MAWA_QUALITY_MEDIUM As Long = 0
182 Public Const MAWA_QUALITY_LOW As Long = 2
184 ' SFileGetFileInfo flags
185 Public Const SFILE_INFO_BLOCK_SIZE As Long = &H1 'Block size in MPQ
186 Public Const SFILE_INFO_HASH_TABLE_SIZE As Long = &H2 'Hash table size in MPQ
187 Public Const SFILE_INFO_NUM_FILES As Long = &H3 'Number of files in MPQ
188 Public Const SFILE_INFO_TYPE As Long = &H4 'Is Long a file or an MPQ?
189 Public Const SFILE_INFO_SIZE As Long = &H5 'Size of MPQ or uncompressed file
190 Public Const SFILE_INFO_COMPRESSED_SIZE As Long = &H6 'Size of compressed file
191 Public Const SFILE_INFO_FLAGS As Long = &H7 'File flags (compressed, etc.), file attributes if a file not in an archive
192 Public Const SFILE_INFO_PARENT As Long = &H8 'Handle of MPQ that file is in
193 Public Const SFILE_INFO_POSITION As Long = &H9 'Position of file pointer in files
194 Public Const SFILE_INFO_LOCALEID As Long = &HA 'Locale ID of file in MPQ
195 Public Const SFILE_INFO_PRIORITY As Long = &HB 'Priority of open MPQ
196 Public Const SFILE_INFO_HASH_INDEX As Long = &HC 'Hash index of file in MPQ
198 ' SFileListFiles flags
199 Public Const SFILE_LIST_MEMORY_LIST  As Long = &H1 ' Specifies that lpFilelists is a file list from memory, rather than being a list of file lists
200 Public Const SFILE_LIST_ONLY_KNOWN   As Long = &H2 ' Only list files that the function finds a name for
201 Public Const SFILE_LIST_ONLY_UNKNOWN As Long = &H4 ' Only list files that the function does not find a name for
203 Public Const SFILE_TYPE_MPQ As Long = &H1
204 Public Const SFILE_TYPE_FILE As Long = &H2
206 Public Const INVALID_HANDLE_VALUE As Long = -1
208 Public Const FILE_BEGIN   As Long = 0
209 Public Const FILE_CURRENT As Long = 1
210 Public Const FILE_END     As Long = 2
212 Public Const SFILE_OPEN_HARD_DISK_FILE As Long = &H0 'Open archive without regard to the drive type it resides on
213 Public Const SFILE_OPEN_CD_ROM_FILE As Long = &H1 'Open the archive only if it is on a CD-ROM
214 Public Const SFILE_OPEN_ALLOW_WRITE As Long = &H8000 'Open file with write access
216 Public Const SFILE_SEARCH_CURRENT_ONLY As Long = &H0 'Used with SFileOpenFileEx; only the archive with the handle specified will be searched for the file
217 Public Const SFILE_SEARCH_ALL_OPEN As Long = &H1 'SFileOpenFileEx will look through all open archives for the file
219 Type FILELISTENTRY
220     dwFileExists As Long ' Nonzero if this entry is used
221     lcLocale As Long ' Locale ID of file
222     dwCompressedSize As Long ' Compressed size of file
223     dwFullSize As Long ' Uncompressed size of file
224     dwFlags As Long ' Flags for file
225     szFileName(259) As Byte
226 End Type
228 ' Storm functions implemented by this library
229 Declare Function SFileOpenArchive Lib "SFmpq.dll" (ByVal lpFileName As String, ByVal dwPriority As Long, ByVal dwFlags As Long, ByRef hMPQ As Long) As Boolean
230 Declare Function SFileCloseArchive Lib "SFmpq.dll" (ByVal hMPQ As Long) As Boolean
231 Declare Function SFileGetArchiveName Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpBuffer As String, ByVal dwBufferLength As Long) As Boolean
232 Declare Function SFileOpenFile Lib "SFmpq.dll" (ByVal lpFileName As String, ByRef hFile As Long) As Boolean
233 Declare Function SFileOpenFileEx Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpFileName As String, ByVal dwSearchScope As Long, ByRef hFile As Long) As Boolean
234 Declare Function SFileCloseFile Lib "SFmpq.dll" (ByVal hFile As Long) As Boolean
235 Declare Function SFileGetFileSize Lib "SFmpq.dll" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
236 Declare Function SFileGetFileArchive Lib "SFmpq.dll" (ByVal hFile As Long, ByRef hMPQ As Long) As Boolean
237 Declare Function SFileGetFileName Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpBuffer As String, ByVal dwBufferLength As Long) As Boolean
238 Declare Function SFileSetFilePointer Lib "SFmpq.dll" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lplDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
239 Declare Function SFileReadFile Lib "SFmpq.dll" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Boolean
240 Declare Function SFileSetLocale Lib "SFmpq.dll" (ByVal nNewLocale As Long) As Long
241 Declare Function SFileGetBasePath Lib "SFmpq.dll" (ByVal lpBuffer As String, ByVal dwBufferLength As Long) As Boolean
242 Declare Function SFileSetBasePath Lib "SFmpq.dll" (ByVal lpNewBasePath As String) As Boolean
244 ' Extra storm-related functions
245 Declare Function SFileGetFileInfo Lib "SFmpq.dll" (ByVal hFile As Long, ByVal dwInfoType As Long) As Long
246 Declare Function SFileSetArchivePriority Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal dwPriority As Long) As Boolean
247 Declare Function SFileFindMpqHeader Lib "SFmpq.dll" (ByVal hFile As Long) As Long
248 Declare Function SFileListFiles Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpFileLists As String, ByRef lpListBuffer As FILELISTENTRY, ByVal dwFlags As Long) As Boolean
250 ' Archive editing functions implemented by this library
251 Declare Function MpqOpenArchiveForUpdate Lib "SFmpq.dll" (ByVal lpFileName As String, ByVal dwFlags As Long, ByVal dwMaximumFilesInArchive As Long) As Long
252 Declare Function MpqCloseUpdatedArchive Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal dwUnknown2 As Long) As Long
253 Declare Function MpqAddFileToArchive Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpSourceFileName As String, ByVal lpDestFileName As String, ByVal dwFlags As Long) As Boolean
254 Declare Function MpqAddWaveToArchive Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpSourceFileName As String, ByVal lpDestFileName As String, ByVal dwFlags As Long, ByVal dwQuality As Long) As Boolean
255 Declare Function MpqRenameFile Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpcOldFileName As String, ByVal lpcNewFileName As String) As Boolean
256 Declare Function MpqDeleteFile Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpFileName As String) As Boolean
257 Declare Function MpqCompactArchive Lib "SFmpq.dll" (ByVal hMPQ As Long) As Boolean
259 ' Extra archive editing functions
260 Declare Function MpqAddFileToArchiveEx Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpSourceFileName As String, ByVal lpDestFileName As String, ByVal dwFlags As Long, ByVal dwCompressionType As Long, ByVal dwCompressLevel As Long) As Boolean
261 Declare Function MpqAddFileFromBufferEx Lib "SFmpq.dll" (ByVal hMPQ As Long, lpBuffer As Any, ByVal dwLength As Long, ByVal lpFileName As String, ByVal dwFlags As Long, ByVal dwCompressionType As Long, ByVal dwCompressLevel As Long) As Boolean
262 Declare Function MpqAddFileFromBuffer Lib "SFmpq.dll" (ByVal hMPQ As Long, lpBuffer As Any, ByVal dwLength As Long, ByVal lpFileName As String, ByVal dwFlags As Long) As Boolean
263 Declare Function MpqAddWaveFromBuffer Lib "SFmpq.dll" (ByVal hMPQ As Long, lpBuffer As Any, ByVal dwLength As Long, ByVal lpFileName As String, ByVal dwFlags As Long, ByVal dwQuality As Long) As Boolean
264 Declare Function MpqSetFileLocale Lib "SFmpq.dll" (ByVal hMPQ As Long, ByVal lpFileName As String, ByVal nOldLocale As Long, ByVal nNewLocale As Long) As Boolean
266 ' These functions do nothing.  They are only provided for
267 ' compatibility with MPQ extractors that use storm.
268 Declare Function SFileDestroy Lib "SFmpq.dll" () As Boolean
269 Declare Sub StormDestroy Lib "SFmpq.dll" ()
271 ' Returns 0 if the dll version is equal to the version your program was compiled
272 ' with, 1 if the dll is newer, -1 if the dll is older.
273 Function SFMpqCompareVersion() As Long
274     Dim ExeVersion As SFMPQVERSION, DllVersion As SFMPQVERSION
275     With ExeVersion
276         .Major = 1
277         .Minor = 0
278         .Revision = 7
279         .Subrevision = 4
280     End With
281     DllVersion = SFMpqGetVersion()
282     If DllVersion.Major > ExeVersion.Major Then
283         SFMpqCompareVersion = 1
284         Exit Function
285     ElseIf DllVersion.Major < ExeVersion.Major Then
286         SFMpqCompareVersion = -1
287         Exit Function
288     End If
289     If DllVersion.Minor > ExeVersion.Minor Then
290         SFMpqCompareVersion = 1
291         Exit Function
292     ElseIf DllVersion.Minor < ExeVersion.Minor Then
293         SFMpqCompareVersion = -1
294         Exit Function
295     End If
296     If DllVersion.Revision > ExeVersion.Revision Then
297         SFMpqCompareVersion = 1
298         Exit Function
299     ElseIf DllVersion.Revision < ExeVersion.Revision Then
300         SFMpqCompareVersion = -1
301         Exit Function
302     End If
303     If DllVersion.Subrevision > ExeVersion.Subrevision Then
304         SFMpqCompareVersion = 1
305         Exit Function
306     ElseIf DllVersion.Subrevision < ExeVersion.Subrevision Then
307         SFMpqCompareVersion = -1
308         Exit Function
309     End If
310     SFMpqCompareVersion = 0
311 End Function