TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
FileStream.h
Go to the documentation of this file.
1 /*****************************************************************************/
2 /* FileStream.h Copyright (c) Ladislav Zezula 2012 */
3 /*---------------------------------------------------------------------------*/
4 /* Description: Definitions for FileStream object */
5 /*---------------------------------------------------------------------------*/
6 /* Date Ver Who Comment */
7 /* -------- ---- --- ------- */
8 /* 14.04.12 1.00 Lad The first version of FileStream.h */
9 /*****************************************************************************/
10 
11 #ifndef __FILESTREAM_H__
12 #define __FILESTREAM_H__
13 
14 //-----------------------------------------------------------------------------
15 // Function prototypes
16 
17 typedef void (*STREAM_INIT)(
18  struct TFileStream * pStream // Pointer to an unopened stream
19 );
20 
21 typedef bool (*STREAM_CREATE)(
22  struct TFileStream * pStream // Pointer to an unopened stream
23  );
24 
25 typedef bool (*STREAM_OPEN)(
26  struct TFileStream * pStream, // Pointer to an unopened stream
27  const TCHAR * szFileName, // Pointer to file name to be open
28  DWORD dwStreamFlags // Stream flags
29  );
30 
31 typedef bool (*STREAM_READ)(
32  struct TFileStream * pStream, // Pointer to an open stream
33  ULONGLONG * pByteOffset, // Pointer to file byte offset. If NULL, it reads from the current position
34  void * pvBuffer, // Pointer to data to be read
35  DWORD dwBytesToRead // Number of bytes to read from the file
36  );
37 
38 typedef bool (*STREAM_WRITE)(
39  struct TFileStream * pStream, // Pointer to an open stream
40  ULONGLONG * pByteOffset, // Pointer to file byte offset. If NULL, it writes to the current position
41  const void * pvBuffer, // Pointer to data to be written
42  DWORD dwBytesToWrite // Number of bytes to read from the file
43  );
44 
45 typedef bool (*STREAM_RESIZE)(
46  struct TFileStream * pStream, // Pointer to an open stream
47  ULONGLONG FileSize // New size for the file, in bytes
48  );
49 
50 typedef bool (*STREAM_GETSIZE)(
51  struct TFileStream * pStream, // Pointer to an open stream
52  ULONGLONG * pFileSize // Receives the file size, in bytes
53  );
54 
55 typedef bool (*STREAM_GETPOS)(
56  struct TFileStream * pStream, // Pointer to an open stream
57  ULONGLONG * pByteOffset // Pointer to store current file position
58  );
59 
60 typedef void (*STREAM_CLOSE)(
61  struct TFileStream * pStream // Pointer to an open stream
62  );
63 
64 typedef bool (*BLOCK_READ)(
65  struct TFileStream * pStream, // Pointer to a block-oriented stream
66  ULONGLONG StartOffset, // Byte offset of start of the block array
67  ULONGLONG EndOffset, // End offset (either end of the block or end of the file)
68  LPBYTE BlockBuffer, // Pointer to block-aligned buffer
69  DWORD BytesNeeded, // Number of bytes that are really needed
70  bool bAvailable // true if the block is available
71  );
72 
73 typedef bool (*BLOCK_CHECK)(
74  struct TFileStream * pStream, // Pointer to a block-oriented stream
75  ULONGLONG BlockOffset // Offset of the file to check
76  );
77 
78 typedef void (*BLOCK_SAVEMAP)(
79  struct TFileStream * pStream // Pointer to a block-oriented stream
80  );
81 
82 //-----------------------------------------------------------------------------
83 // Local structures - partial file structure and bitmap footer
84 
85 #define ID_FILE_BITMAP_FOOTER 0x33767470 // Signature of the file bitmap footer ('ptv3')
86 #define DEFAULT_BLOCK_SIZE 0x00004000 // Default size of the stream block
87 #define DEFAULT_BUILD_NUMBER 10958 // Build number for newly created partial MPQs
88 
89 typedef struct _PART_FILE_HEADER
90 {
91  DWORD PartialVersion; // Always set to 2
92  char GameBuildNumber[0x20]; // Minimum build number of the game that can use this MPQ
93  DWORD Flags; // Flags (details unknown)
94  DWORD FileSizeLo; // Low 32 bits of the contained file size
95  DWORD FileSizeHi; // High 32 bits of the contained file size
96  DWORD BlockSize; // Size of one file block, in bytes
97 
99 
100 // Structure describing the block-to-file map entry
101 typedef struct _PART_FILE_MAP_ENTRY
102 {
103  DWORD Flags; // 3 = the block is present in the file
104  DWORD BlockOffsLo; // Low 32 bits of the block position in the file
105  DWORD BlockOffsHi; // High 32 bits of the block position in the file
106  DWORD LargeValueLo; // 64-bit value, meaning is unknown
108 
110 
111 typedef struct _FILE_BITMAP_FOOTER
112 {
113  DWORD Signature; // 'ptv3' (ID_FILE_BITMAP_FOOTER)
114  DWORD Version; // Unknown, seems to always have value of 3 (version?)
115  DWORD BuildNumber; // Game build number for that MPQ
116  DWORD MapOffsetLo; // Low 32-bits of the offset of the bit map
117  DWORD MapOffsetHi; // High 32-bits of the offset of the bit map
118  DWORD BlockSize; // Size of one block (usually 0x4000 bytes)
119 
121 
122 //-----------------------------------------------------------------------------
123 // Structure for file stream
124 
126 {
127  struct
128  {
129  ULONGLONG FileSize; // Size of the file
130  ULONGLONG FilePos; // Current file position
131  ULONGLONG FileTime; // Last write time
132  HANDLE hFile; // File handle
133  } File;
134 
135  struct
136  {
137  ULONGLONG FileSize; // Size of the file
138  ULONGLONG FilePos; // Current file position
139  ULONGLONG FileTime; // Last write time
140  LPBYTE pbFile; // Pointer to mapped view
141  } Map;
142 
143  struct
144  {
145  ULONGLONG FileSize; // Size of the file
146  ULONGLONG FilePos; // Current file position
147  ULONGLONG FileTime; // Last write time
148  HANDLE hInternet; // Internet handle
149  HANDLE hConnect; // Connection to the internet server
150  } Http;
151 };
152 
154 {
155  // Stream provider functions
156  STREAM_READ StreamRead; // Pointer to stream read function for this archive. Do not use directly.
157  STREAM_WRITE StreamWrite; // Pointer to stream write function for this archive. Do not use directly.
158  STREAM_RESIZE StreamResize; // Pointer to function changing file size
159  STREAM_GETSIZE StreamGetSize; // Pointer to function returning file size
160  STREAM_GETPOS StreamGetPos; // Pointer to function that returns current file position
161  STREAM_CLOSE StreamClose; // Pointer to function closing the stream
162 
163  // Block-oriented functions
164  BLOCK_READ BlockRead; // Pointer to function reading one or more blocks
165  BLOCK_CHECK BlockCheck; // Pointer to function checking whether the block is present
166 
167  // Base provider functions
168  STREAM_CREATE BaseCreate; // Pointer to base create function
169  STREAM_OPEN BaseOpen; // Pointer to base open function
170  STREAM_READ BaseRead; // Read from the stream
171  STREAM_WRITE BaseWrite; // Write to the stream
172  STREAM_RESIZE BaseResize; // Pointer to function changing file size
173  STREAM_GETSIZE BaseGetSize; // Pointer to function returning file size
174  STREAM_GETPOS BaseGetPos; // Pointer to function that returns current file position
175  STREAM_CLOSE BaseClose; // Pointer to function closing the stream
176 
177  // Base provider data (file size, file position)
179 
180  // Stream provider data
181  TFileStream * pMaster; // Master stream (e.g. MPQ on a web server)
182  TCHAR * szFileName; // File name (self-relative pointer)
183 
184  ULONGLONG StreamSize; // Stream size (can be less than file size)
185  ULONGLONG StreamPos; // Stream position
186  DWORD BuildNumber; // Game build number
187  DWORD dwFlags; // Stream flags
188 
189  // Followed by stream provider data, with variable length
190 };
191 
192 //-----------------------------------------------------------------------------
193 // Structures for block-oriented stream
194 
195 struct TBlockStream : public TFileStream
196 {
197  STREAM_DOWNLOAD_CALLBACK pfnCallback; // Callback for downloading
198  void * FileBitmap; // Array of bits for file blocks
199  void * UserData; // User data to be passed to the download callback
200  DWORD BitmapSize; // Size of the file bitmap (in bytes)
201  DWORD BlockSize; // Size of one block, in bytes
202  DWORD BlockCount; // Number of data blocks in the file
203  DWORD IsComplete; // If nonzero, no blocks are missing
204  DWORD IsModified; // nonzero if the bitmap has been modified
205 };
206 
207 //-----------------------------------------------------------------------------
208 // Structure for encrypted stream
209 
210 #define ENCRYPTED_CHUNK_SIZE 0x40 // Size of one chunk to be decrypted
211 
213 {
215 };
216 
217 //-----------------------------------------------------------------------------
218 // Public functions for file stream
219 
220 TFileStream * FileStream_CreateFile(const TCHAR * szFileName, DWORD dwStreamFlags);
221 TFileStream * FileStream_OpenFile(const TCHAR * szFileName, DWORD dwStreamFlags);
222 const TCHAR * FileStream_GetFileName(TFileStream * pStream);
223 size_t FileStream_Prefix(const TCHAR * szFileName, DWORD * pdwProvider);
224 
225 bool FileStream_SetCallback(TFileStream * pStream, STREAM_DOWNLOAD_CALLBACK pfnCallback, void * pvUserData);
226 
227 bool FileStream_Read(TFileStream * pStream, ULONGLONG * pByteOffset, void * pvBuffer, DWORD dwBytesToRead);
228 bool FileStream_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const void * pvBuffer, DWORD dwBytesToWrite);
229 bool FileStream_SetSize(TFileStream * pStream, ULONGLONG NewFileSize);
230 bool FileStream_GetSize(TFileStream * pStream, ULONGLONG * pFileSize);
231 bool FileStream_GetPos(TFileStream * pStream, ULONGLONG * pByteOffset);
232 bool FileStream_GetTime(TFileStream * pStream, ULONGLONG * pFT);
233 bool FileStream_GetFlags(TFileStream * pStream, PDWORD pdwStreamFlags);
234 bool FileStream_Replace(TFileStream * pStream, TFileStream * pNewStream);
235 void FileStream_Close(TFileStream * pStream);
236 
237 
238 #endif // __FILESTREAM_H__
Definition: FileStream.h:125
struct TBaseProviderData::@2 Http
struct _PART_FILE_HEADER PART_FILE_HEADER
void * UserData
Definition: FileStream.h:199
struct _FILE_BITMAP_FOOTER FILE_BITMAP_FOOTER
STREAM_GETPOS StreamGetPos
Definition: FileStream.h:160
void FileStream_Close(TFileStream *pStream)
Definition: FileStream.cpp:2707
struct TBaseProviderData::@1 Map
DWORD BitmapSize
Definition: FileStream.h:200
STREAM_CLOSE StreamClose
Definition: FileStream.h:161
bool(* STREAM_GETPOS)(struct TFileStream *pStream, ULONGLONG *pByteOffset)
Definition: FileStream.h:55
Definition: FileStream.h:212
DWORD LargeValueHi
Definition: FileStream.h:107
void * HANDLE
Definition: CascPort.h:146
void * FileBitmap
Definition: FileStream.h:198
unsigned long long ULONGLONG
Definition: CascPort.h:144
DWORD * PDWORD
Definition: CascPort.h:151
DWORD BlockOffsLo
Definition: FileStream.h:104
bool(* STREAM_OPEN)(struct TFileStream *pStream, const TCHAR *szFileName, DWORD dwStreamFlags)
Definition: FileStream.h:25
bool FileStream_Replace(TFileStream *pStream, TFileStream *pNewStream)
Definition: FileStream.cpp:2666
DWORD FileSizeHi
Definition: FileStream.h:95
DWORD Flags
Definition: FileStream.h:103
STREAM_OPEN BaseOpen
Definition: FileStream.h:169
STREAM_READ BaseRead
Definition: FileStream.h:170
ULONGLONG FileTime
Definition: FileStream.h:131
void(* BLOCK_SAVEMAP)(struct TFileStream *pStream)
Definition: FileStream.h:78
BYTE Key[ENCRYPTED_CHUNK_SIZE]
Definition: FileStream.h:214
TBaseProviderData Base
Definition: FileStream.h:178
size_t FileStream_Prefix(const TCHAR *szFileName, DWORD *pdwProvider)
Definition: FileStream.cpp:2434
char TCHAR
Definition: CascPort.h:148
BLOCK_READ BlockRead
Definition: FileStream.h:164
bool(* BLOCK_CHECK)(struct TFileStream *pStream, ULONGLONG BlockOffset)
Definition: FileStream.h:73
const TCHAR * FileStream_GetFileName(TFileStream *pStream)
Definition: FileStream.cpp:2421
Definition: FileStream.h:89
BYTE * LPBYTE
Definition: CascPort.h:152
bool(* STREAM_READ)(struct TFileStream *pStream, ULONGLONG *pByteOffset, void *pvBuffer, DWORD dwBytesToRead)
Definition: FileStream.h:31
void(* STREAM_CLOSE)(struct TFileStream *pStream)
Definition: FileStream.h:60
struct TBaseProviderData::@0 File
STREAM_RESIZE BaseResize
Definition: FileStream.h:172
STREAM_WRITE BaseWrite
Definition: FileStream.h:171
#define bool
Definition: CascPort.h:16
ULONGLONG StreamPos
Definition: FileStream.h:185
struct _FILE_BITMAP_FOOTER * PFILE_BITMAP_FOOTER
TFileStream * FileStream_CreateFile(const TCHAR *szFileName, DWORD dwStreamFlags)
Definition: FileStream.cpp:2328
ULONGLONG FileSize
Definition: FileStream.h:129
void(* STREAM_INIT)(struct TFileStream *pStream)
Definition: FileStream.h:17
Definition: FileStream.h:101
bool FileStream_GetFlags(TFileStream *pStream, PDWORD pdwStreamFlags)
Definition: FileStream.cpp:2649
bool FileStream_Read(TFileStream *pStream, ULONGLONG *pByteOffset, void *pvBuffer, DWORD dwBytesToRead)
Definition: FileStream.cpp:2551
HANDLE hInternet
Definition: FileStream.h:148
STREAM_CLOSE BaseClose
Definition: FileStream.h:175
bool FileStream_Write(TFileStream *pStream, ULONGLONG *pByteOffset, const void *pvBuffer, DWORD dwBytesToWrite)
Definition: FileStream.cpp:2577
bool FileStream_GetSize(TFileStream *pStream, ULONGLONG *pFileSize)
Definition: FileStream.cpp:2595
STREAM_DOWNLOAD_CALLBACK pfnCallback
Definition: FileStream.h:197
DWORD BlockSize
Definition: FileStream.h:96
TCHAR * szFileName
Definition: FileStream.h:182
DWORD BuildNumber
Definition: FileStream.h:186
bool FileStream_GetTime(TFileStream *pStream, ULONGLONG *pFT)
Definition: FileStream.cpp:2636
STREAM_CREATE BaseCreate
Definition: FileStream.h:168
HANDLE hConnect
Definition: FileStream.h:149
void(WINAPI * STREAM_DOWNLOAD_CALLBACK)(void *pvUserData, ULONGLONG ByteOffset, DWORD dwTotalBytes)
Definition: CascLib.h:159
DWORD IsComplete
Definition: FileStream.h:203
STREAM_GETPOS BaseGetPos
Definition: FileStream.h:174
HANDLE hFile
Definition: FileStream.h:132
bool FileStream_SetSize(TFileStream *pStream, ULONGLONG NewFileSize)
Definition: FileStream.cpp:2607
bool FileStream_SetCallback(TFileStream *pStream, STREAM_DOWNLOAD_CALLBACK pfnCallback, void *pvUserData)
Definition: FileStream.cpp:2517
STREAM_WRITE StreamWrite
Definition: FileStream.h:157
DWORD BlockCount
Definition: FileStream.h:202
bool(* STREAM_RESIZE)(struct TFileStream *pStream, ULONGLONG FileSize)
Definition: FileStream.h:45
TFileStream * pMaster
Definition: FileStream.h:181
char GameBuildNumber[0x20]
Definition: FileStream.h:92
STREAM_RESIZE StreamResize
Definition: FileStream.h:158
unsigned int DWORD
Definition: CascPort.h:139
bool(* STREAM_WRITE)(struct TFileStream *pStream, ULONGLONG *pByteOffset, const void *pvBuffer, DWORD dwBytesToWrite)
Definition: FileStream.h:38
DWORD PartialVersion
Definition: FileStream.h:91
bool FileStream_GetPos(TFileStream *pStream, ULONGLONG *pByteOffset)
Definition: FileStream.cpp:2624
bool(* STREAM_GETSIZE)(struct TFileStream *pStream, ULONGLONG *pFileSize)
Definition: FileStream.h:50
LPBYTE pbFile
Definition: FileStream.h:140
DWORD LargeValueLo
Definition: FileStream.h:106
DWORD dwFlags
Definition: FileStream.h:187
STREAM_GETSIZE StreamGetSize
Definition: FileStream.h:159
Definition: FileStream.h:153
DWORD FileSizeLo
Definition: FileStream.h:94
struct _PART_FILE_MAP_ENTRY PART_FILE_MAP_ENTRY
DWORD BlockOffsHi
Definition: FileStream.h:105
bool(* BLOCK_READ)(struct TFileStream *pStream, ULONGLONG StartOffset, ULONGLONG EndOffset, LPBYTE BlockBuffer, DWORD BytesNeeded, bool bAvailable)
Definition: FileStream.h:64
DWORD IsModified
Definition: FileStream.h:204
DWORD BlockSize
Definition: FileStream.h:201
BLOCK_CHECK BlockCheck
Definition: FileStream.h:165
unsigned char BYTE
Definition: CascPort.h:136
struct _PART_FILE_HEADER * PPART_FILE_HEADER
ULONGLONG FilePos
Definition: FileStream.h:130
DWORD Flags
Definition: FileStream.h:93
ULONGLONG StreamSize
Definition: FileStream.h:184
Definition: FileStream.h:195
struct _PART_FILE_MAP_ENTRY * PPART_FILE_MAP_ENTRY
STREAM_GETSIZE BaseGetSize
Definition: FileStream.h:173
STREAM_READ StreamRead
Definition: FileStream.h:156
bool(* STREAM_CREATE)(struct TFileStream *pStream)
Definition: FileStream.h:21
TFileStream * FileStream_OpenFile(const TCHAR *szFileName, DWORD dwStreamFlags)
Definition: FileStream.cpp:2384
#define ENCRYPTED_CHUNK_SIZE
Definition: FileStream.h:210