TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ListFile.cpp File Reference
#include "../CascLib.h"
#include "../CascCommon.h"
+ Include dependency graph for ListFile.cpp:

Classes

struct  _LISTFILE_CACHE
 

Macros

#define __CASCLIB_SELF__
 
#define LISTMAP_INITIAL   0x100000
 

Typedefs

typedef struct _LISTFILE_CACHE LISTFILE_CACHE
 
typedef struct _LISTFILE_CACHEPLISTFILE_CACHE
 

Functions

static PLISTFILE_CACHE CreateListFileCache (DWORD dwFileSize)
 
void * ListFile_OpenExternal (const TCHAR *szListFile)
 
void * ListFile_FromBuffer (LPBYTE pbBuffer, DWORD cbBuffer)
 
bool ListFile_VerifyMD5 (void *pvListFile, LPBYTE pbHashMD5)
 
size_t ListFile_GetNextLine (void *pvListFile, const char **pszLineBegin, const char **pszLineEnd)
 
size_t ListFile_GetNextLine (void *pvListFile, char *szBuffer, size_t nMaxChars)
 
size_t ListFile_GetNext (void *pvListFile, const char *szMask, char *szBuffer, size_t nMaxChars)
 
void ListFile_Free (void *pvListFile)
 
static PLISTFILE_MAP ListMap_Create ()
 
static PLISTFILE_MAP ListMap_InsertName (PLISTFILE_MAP pListMap, const char *szFileName, size_t nLength)
 
static PLISTFILE_MAP ListMap_Finish (PLISTFILE_MAP pListMap)
 
PLISTFILE_MAP ListFile_CreateMap (const TCHAR *szListFile)
 
const char * ListFile_FindName (PLISTFILE_MAP pListMap, ULONGLONG FileNameHash)
 
void ListFile_FreeMap (PLISTFILE_MAP pListMap)
 

Macro Definition Documentation

#define __CASCLIB_SELF__
#define LISTMAP_INITIAL   0x100000

Typedef Documentation

typedef struct _LISTFILE_CACHE * PLISTFILE_CACHE

Function Documentation

static PLISTFILE_CACHE CreateListFileCache ( DWORD  dwFileSize)
static
32 {
33  PLISTFILE_CACHE pCache;
34 
35  // Allocate cache for one file block
36  pCache = (PLISTFILE_CACHE)CASC_ALLOC(BYTE, sizeof(LISTFILE_CACHE) + dwFileSize);
37  if(pCache != NULL)
38  {
39  // Set the initial pointers
40  pCache->pBegin =
41  pCache->pPos = (char *)(pCache + 1);
42  pCache->pEnd = pCache->pBegin + dwFileSize;
43  }
44 
45  // Return the cache
46  return pCache;
47 }
#define CASC_ALLOC(type, count)
Definition: CascCommon.h:302
char * pEnd
Definition: ListFile.cpp:22
arena_t NULL
Definition: jemalloc_internal.h:624
struct _LISTFILE_CACHE * PLISTFILE_CACHE
char * pBegin
Definition: ListFile.cpp:20
char * pPos
Definition: ListFile.cpp:21
Definition: ListFile.cpp:18
unsigned char BYTE
Definition: CascPort.h:136

+ Here is the caller graph for this function:

PLISTFILE_MAP ListFile_CreateMap ( const TCHAR szListFile)
301 {
302  PLISTFILE_MAP pListMap = NULL;
303  void * pvListFile;
304  char szFileName[MAX_PATH+1];
305  size_t nLength;
306 
307  // Only if the listfile name has been given
308  if(szListFile != NULL)
309  {
310  // Create map for the listfile
311  pListMap = ListMap_Create();
312  if(pListMap != NULL)
313  {
314  // Open the external listfile
315  pvListFile = ListFile_OpenExternal(szListFile);
316  if(pvListFile != NULL)
317  {
318  // Go through the entire listfile and insert each name to the map
319  while((nLength = ListFile_GetNext(pvListFile, "*", szFileName, MAX_PATH)) != 0)
320  {
321  // Insert the file name to the map
322  pListMap = ListMap_InsertName(pListMap, szFileName, nLength);
323  if(pListMap == NULL)
324  break;
325  }
326 
327  // Finish the listfile map
328  pListMap = ListMap_Finish(pListMap);
329 
330  // Free the listfile
331  ListFile_Free(pvListFile);
332  }
333  }
334  }
335 
336  // Return the created map
337  return pListMap;
338 }
void * ListFile_OpenExternal(const TCHAR *szListFile)
Definition: ListFile.cpp:52
static PLISTFILE_MAP ListMap_Finish(PLISTFILE_MAP pListMap)
Definition: ListFile.cpp:268
void ListFile_Free(void *pvListFile)
Definition: ListFile.cpp:201
#define MAX_PATH
Definition: CascPort.h:160
arena_t NULL
Definition: jemalloc_internal.h:624
static PLISTFILE_MAP ListMap_InsertName(PLISTFILE_MAP pListMap, const char *szFileName, size_t nLength)
Definition: ListFile.cpp:234
size_t ListFile_GetNext(void *pvListFile, const char *szMask, char *szBuffer, size_t nMaxChars)
Definition: ListFile.cpp:172
Definition: ListFile.h:25
static PLISTFILE_MAP ListMap_Create()
Definition: ListFile.cpp:214

+ Here is the call graph for this function:

const char* ListFile_FindName ( PLISTFILE_MAP  pListMap,
ULONGLONG  FileNameHash 
)
341 {
342  PLISTFILE_ENTRY pListEntry = NULL;
343 
344  if(pListMap != NULL)
345  pListEntry = (PLISTFILE_ENTRY)Map_FindObject(pListMap->pNameMap, &FileNameHash, NULL);
346  return (pListEntry != NULL) ? pListEntry->szFileName : "";
347 }
void * Map_FindObject(PCASC_MAP pMap, void *pvKey, PDWORD PtrIndex)
Definition: Map.cpp:138
PCASC_MAP pNameMap
Definition: ListFile.h:27
arena_t NULL
Definition: jemalloc_internal.h:624
char szFileName[1]
Definition: ListFile.h:21
Definition: ListFile.h:17
struct _LISTFILE_ENTRY * PLISTFILE_ENTRY

+ Here is the call graph for this function:

void ListFile_Free ( void *  pvListFile)
202 {
203  if(pvListFile != NULL)
204  {
205  CASC_FREE(pvListFile);
206  }
207 }
arena_t NULL
Definition: jemalloc_internal.h:624
#define CASC_FREE(ptr)
Definition: CascCommon.h:303

+ Here is the caller graph for this function:

void ListFile_FreeMap ( PLISTFILE_MAP  pListMap)
350 {
351  if(pListMap != NULL)
352  {
353  if(pListMap->pNameMap != NULL)
354  Map_Free(pListMap->pNameMap);
355  CASC_FREE(pListMap);
356  }
357 }
PCASC_MAP pNameMap
Definition: ListFile.h:27
void Map_Free(PCASC_MAP pMap)
Definition: Map.cpp:279
arena_t NULL
Definition: jemalloc_internal.h:624
#define CASC_FREE(ptr)
Definition: CascCommon.h:303

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void* ListFile_FromBuffer ( LPBYTE  pbBuffer,
DWORD  cbBuffer 
)
87 {
88  PLISTFILE_CACHE pCache = NULL;
89 
90  // Create the in-memory cache for the entire listfile
91  // The listfile does not have any data loaded yet
92  pCache = CreateListFileCache(cbBuffer);
93  if(pCache != NULL)
94  memcpy(pCache->pBegin, pbBuffer, cbBuffer);
95 
96  return pCache;
97 }
arena_t NULL
Definition: jemalloc_internal.h:624
static PLISTFILE_CACHE CreateListFileCache(DWORD dwFileSize)
Definition: ListFile.cpp:31
char * pBegin
Definition: ListFile.cpp:20
Definition: ListFile.cpp:18

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t ListFile_GetNext ( void *  pvListFile,
const char *  szMask,
char *  szBuffer,
size_t  nMaxChars 
)
173 {
174  size_t nLength = 0;
175  int nError = ERROR_SUCCESS;
176 
177  // Check for parameters
178  for(;;)
179  {
180  // Read the (next) line
181  nLength = ListFile_GetNextLine(pvListFile, szBuffer, nMaxChars);
182  if(nLength == 0)
183  {
184  nError = ERROR_NO_MORE_FILES;
185  break;
186  }
187 
188  // If some mask entered, check it
189  if(CheckWildCard(szBuffer, szMask))
190  {
191  nError = ERROR_SUCCESS;
192  break;
193  }
194  }
195 
196  if(nError != ERROR_SUCCESS)
197  SetLastError(nError);
198  return nLength;
199 }
bool CheckWildCard(const char *szString, const char *szWildCard)
Definition: Common.cpp:426
#define ERROR_NO_MORE_FILES
Definition: CascPort.h:215
void SetLastError(int nError)
Definition: Common.cpp:75
size_t ListFile_GetNextLine(void *pvListFile, const char **pszLineBegin, const char **pszLineEnd)
Definition: ListFile.cpp:111
#define ERROR_SUCCESS
Definition: CascPort.h:204

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t ListFile_GetNextLine ( void *  pvListFile,
const char **  pszLineBegin,
const char **  pszLineEnd 
)
112 {
113  PLISTFILE_CACHE pCache = (PLISTFILE_CACHE)pvListFile;
114  char * szExtraString = NULL;
115  char * szLineBegin;
116  char * szLineEnd;
117 
118  // Skip newlines, spaces, tabs and another non-printable stuff
119  while(pCache->pPos < pCache->pEnd && pCache->pPos[0] <= 0x20)
120  pCache->pPos++;
121 
122  // Remember the begin of the line
123  szLineBegin = pCache->pPos;
124 
125  // Copy the remaining characters
126  while(pCache->pPos < pCache->pEnd)
127  {
128  // If we have found a newline, stop loading
129  if(pCache->pPos[0] == 0x0D || pCache->pPos[0] == 0x0A)
130  break;
131 
132  // Blizzard listfiles can also contain information about patch:
133  // Pass1\Files\MacOS\unconditional\user\Background Downloader.app\Contents\Info.plist~Patch(Data#frFR#base-frFR,1326)
134  if(pCache->pPos[0] == '~')
135  szExtraString = pCache->pPos;
136 
137  // Move the position by one character forward
138  pCache->pPos++;
139  }
140 
141  // Remember the end of the line
142  szLineEnd = (szExtraString != NULL && szExtraString[0] == '~' && szExtraString[1] == 'P') ? szExtraString : pCache->pPos;
143 
144  // Give the caller the positions of the begin and end of the line
145  pszLineBegin[0] = szLineBegin;
146  pszLineEnd[0] = szLineEnd;
147  return (size_t)(szLineEnd - szLineBegin);
148 }
char * pEnd
Definition: ListFile.cpp:22
arena_t NULL
Definition: jemalloc_internal.h:624
struct _LISTFILE_CACHE * PLISTFILE_CACHE
char * pPos
Definition: ListFile.cpp:21
Definition: ListFile.cpp:18

+ Here is the caller graph for this function:

size_t ListFile_GetNextLine ( void *  pvListFile,
char *  szBuffer,
size_t  nMaxChars 
)
151 {
152  const char * szLineBegin = NULL;
153  const char * szLineEnd = NULL;
154  size_t nLength;
155 
156  // Retrieve the next line
157  nLength = ListFile_GetNextLine(pvListFile, &szLineBegin, &szLineEnd);
158 
159  // Check the length
160  if(nLength > nMaxChars)
161  {
163  return 0;
164  }
165 
166  // Copy the line to the user buffer
167  memcpy(szBuffer, szLineBegin, nLength);
168  szBuffer[nLength] = 0;
169  return nLength;
170 }
#define ERROR_INSUFFICIENT_BUFFER
Definition: CascPort.h:213
arena_t NULL
Definition: jemalloc_internal.h:624
void SetLastError(int nError)
Definition: Common.cpp:75
size_t ListFile_GetNextLine(void *pvListFile, const char **pszLineBegin, const char **pszLineEnd)
Definition: ListFile.cpp:111

+ Here is the call graph for this function:

void* ListFile_OpenExternal ( const TCHAR szListFile)
53 {
54  PLISTFILE_CACHE pCache = NULL;
55  TFileStream * pStream;
56  ULONGLONG FileSize = 0;
57 
58  // Open the external listfile
59  pStream = FileStream_OpenFile(szListFile, STREAM_FLAG_READ_ONLY);
60  if(pStream != NULL)
61  {
62  // Retrieve the size of the external listfile
63  FileStream_GetSize(pStream, &FileSize);
64  if(0 < FileSize && FileSize <= 0x30000000)
65  {
66  // Create the in-memory cache for the entire listfile
67  // The listfile does not have any data loaded yet
68  pCache = CreateListFileCache((DWORD)FileSize);
69  if(pCache != NULL)
70  {
71  if(!FileStream_Read(pStream, NULL, pCache->pBegin, (DWORD)FileSize))
72  {
73  ListFile_Free(pCache);
74  pCache = NULL;
75  }
76  }
77  }
78 
79  // Close the file stream
80  FileStream_Close(pStream);
81  }
82 
83  return pCache;
84 }
unsigned long long ULONGLONG
Definition: CascPort.h:144
bool FileStream_Read(TFileStream *pStream, ULONGLONG *pByteOffset, void *pvBuffer, DWORD dwBytesToRead)
Definition: FileStream.cpp:2551
void ListFile_Free(void *pvListFile)
Definition: ListFile.cpp:201
bool FileStream_GetSize(TFileStream *pStream, ULONGLONG *pFileSize)
Definition: FileStream.cpp:2595
arena_t NULL
Definition: jemalloc_internal.h:624
static PLISTFILE_CACHE CreateListFileCache(DWORD dwFileSize)
Definition: ListFile.cpp:31
char * pBegin
Definition: ListFile.cpp:20
void FileStream_Close(TFileStream *pStream)
Definition: FileStream.cpp:2707
unsigned int DWORD
Definition: CascPort.h:139
TFileStream * FileStream_OpenFile(const TCHAR *szFileName, DWORD dwStreamFlags)
Definition: FileStream.cpp:2384
Definition: ListFile.cpp:18
Definition: FileStream.h:153
#define STREAM_FLAG_READ_ONLY
Definition: CascLib.h:56

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool ListFile_VerifyMD5 ( void *  pvListFile,
LPBYTE  pbHashMD5 
)
101 {
102  PLISTFILE_CACHE pCache = (PLISTFILE_CACHE)pvListFile;
103 
104  // Must be at the beginning
105  assert(pCache->pPos == pCache->pBegin);
106 
107  // Verify the MD5 hash for the entire block
108  return VerifyDataBlockHash(pCache->pBegin, (DWORD)(pCache->pEnd - pCache->pBegin), pbHashMD5);
109 }
char * pEnd
Definition: ListFile.cpp:22
bool VerifyDataBlockHash(void *pvDataBlock, DWORD cbDataBlock, LPBYTE expected_md5)
Definition: Common.cpp:494
struct _LISTFILE_CACHE * PLISTFILE_CACHE
char * pBegin
Definition: ListFile.cpp:20
unsigned int DWORD
Definition: CascPort.h:139
char * pPos
Definition: ListFile.cpp:21
Definition: ListFile.cpp:18

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static PLISTFILE_MAP ListMap_Create ( )
static
215 {
216  PLISTFILE_MAP pListMap;
217  size_t cbToAllocate;
218 
219  // Create buffer for the listfile
220  // Note that because the listfile is quite big and CASC_REALLOC
221  // is a costly operation, we want to have as few reallocs as possible.
222  cbToAllocate = sizeof(LISTFILE_MAP) + LISTMAP_INITIAL;
223  pListMap = (PLISTFILE_MAP)CASC_ALLOC(BYTE, cbToAllocate);
224  if(pListMap != NULL)
225  {
226  // Fill the listfile buffer
227  memset(pListMap, 0, sizeof(LISTFILE_MAP));
228  pListMap->cbBufferMax = LISTMAP_INITIAL;
229  }
230 
231  return pListMap;
232 }
#define CASC_ALLOC(type, count)
Definition: CascCommon.h:302
arena_t NULL
Definition: jemalloc_internal.h:624
#define LISTMAP_INITIAL
Definition: ListFile.cpp:212
Definition: ListFile.h:25
struct _LISTFILE_MAP LISTFILE_MAP
size_t cbBufferMax
Definition: ListFile.h:28
unsigned char BYTE
Definition: CascPort.h:136
struct _LISTFILE_MAP * PLISTFILE_MAP

+ Here is the caller graph for this function:

static PLISTFILE_MAP ListMap_Finish ( PLISTFILE_MAP  pListMap)
static
269 {
270  PLISTFILE_ENTRY pListEntry;
271  PCASC_MAP pMap;
272  LPBYTE pbEntry;
273 
274  // Sanity check
275  assert(pListMap->pNameMap == NULL);
276 
277  // Create the map
278  pListMap->pNameMap = pMap = Map_Create((DWORD)pListMap->nEntries, sizeof(ULONGLONG), 0);
279  if(pListMap->pNameMap == NULL)
280  {
281  ListFile_FreeMap(pListMap);
282  return NULL;
283  }
284 
285  // Fill the map
286  pbEntry = (LPBYTE)(pListMap + 1);
287  for(size_t i = 0; i < pListMap->nEntries; i++)
288  {
289  // Get the listfile entry
290  pListEntry = (PLISTFILE_ENTRY)pbEntry;
291  pbEntry += pListEntry->cbEntrySize;
292 
293  // Insert the entry to the map
294  Map_InsertObject(pMap, pListEntry, &pListEntry->FileNameHash);
295  }
296 
297  return pListMap;
298 }
PCASC_MAP pNameMap
Definition: ListFile.h:27
unsigned long long ULONGLONG
Definition: CascPort.h:144
PCASC_MAP Map_Create(DWORD dwMaxItems, DWORD dwKeyLength, DWORD dwKeyOffset)
Definition: Map.cpp:93
ULONGLONG FileNameHash
Definition: ListFile.h:19
arena_t NULL
Definition: jemalloc_internal.h:624
BYTE * LPBYTE
Definition: CascPort.h:152
Definition: ListFile.h:17
bool Map_InsertObject(PCASC_MAP pMap, void *pvNewObject, void *pvKey)
Definition: Map.cpp:170
struct _LISTFILE_ENTRY * PLISTFILE_ENTRY
unsigned int DWORD
Definition: CascPort.h:139
Definition: Map.h:19
size_t nEntries
Definition: ListFile.h:30
void ListFile_FreeMap(PLISTFILE_MAP pListMap)
Definition: ListFile.cpp:349
DWORD cbEntrySize
Definition: ListFile.h:20

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static PLISTFILE_MAP ListMap_InsertName ( PLISTFILE_MAP  pListMap,
const char *  szFileName,
size_t  nLength 
)
static
235 {
236  PLISTFILE_ENTRY pListEntry;
237  size_t cbToAllocate;
238  size_t cbEntrySize;
239 
240  // Make sure there is enough space in the list map
241  cbEntrySize = sizeof(LISTFILE_ENTRY) + nLength;
242  cbEntrySize = ALIGN_TO_SIZE(cbEntrySize, 8);
243  if((pListMap->cbBuffer + cbEntrySize) > pListMap->cbBufferMax)
244  {
245  cbToAllocate = sizeof(LISTFILE_MAP) + (pListMap->cbBufferMax * 3) / 2;
246  pListMap = (PLISTFILE_MAP)CASC_REALLOC(BYTE, pListMap, cbToAllocate);
247  if(pListMap == NULL)
248  return NULL;
249 
250  pListMap->cbBufferMax = (pListMap->cbBufferMax * 3) / 2;
251  }
252 
253  // Get the pointer to the first entry
254  pListEntry = (PLISTFILE_ENTRY)((LPBYTE)(pListMap + 1) + pListMap->cbBuffer);
255  pListEntry->FileNameHash = CalcFileNameHash(szFileName);
256  pListEntry->cbEntrySize = (DWORD)cbEntrySize;
257 
258  // Copy the file name to the entry
259  memcpy(pListEntry->szFileName, szFileName, nLength);
260  pListEntry->szFileName[nLength] = 0;
261 
262  // Move the next entry
263  pListMap->cbBuffer += cbEntrySize;
264  pListMap->nEntries++;
265  return pListMap;
266 }
size_t cbBuffer
Definition: ListFile.h:29
ULONGLONG CalcFileNameHash(const char *szFileName)
Definition: Common.cpp:260
ULONGLONG FileNameHash
Definition: ListFile.h:19
arena_t NULL
Definition: jemalloc_internal.h:624
#define ALIGN_TO_SIZE(x, a)
Definition: Common.h:21
char szFileName[1]
Definition: ListFile.h:21
BYTE * LPBYTE
Definition: CascPort.h:152
Definition: ListFile.h:17
struct _LISTFILE_ENTRY * PLISTFILE_ENTRY
unsigned int DWORD
Definition: CascPort.h:139
struct _LISTFILE_MAP LISTFILE_MAP
#define CASC_REALLOC(type, ptr, count)
Definition: CascCommon.h:301
size_t nEntries
Definition: ListFile.h:30
struct _LISTFILE_ENTRY LISTFILE_ENTRY
size_t cbBufferMax
Definition: ListFile.h:28
unsigned char BYTE
Definition: CascPort.h:136
struct _LISTFILE_MAP * PLISTFILE_MAP
DWORD cbEntrySize
Definition: ListFile.h:20

+ Here is the call graph for this function:

+ Here is the caller graph for this function: