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