-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTMsiDatabase.cpp
486 lines (417 loc) · 13.4 KB
/
TMsiDatabase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*****************************************************************************/
/* TMsiDatabase.cpp Copyright (c) Ladislav Zezula 2023 */
/*---------------------------------------------------------------------------*/
/* Implementation of the TMsiDatabase class methods */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 23.07.23 1.00 Lad Created */
/*****************************************************************************/
#include "wcx_msi.h"
//-----------------------------------------------------------------------------
// Local (non-class) functions
static bool FindStringInList(MSI_STRING_LIST & MsiTablesList, const std::tstring & strTableName)
{
for(size_t i = 0; i < MsiTablesList.size(); i++)
{
if(!_tcsicmp(MsiTablesList[i].c_str(), strTableName.c_str()))
{
return true;
}
}
return false;
}
//-----------------------------------------------------------------------------
// Constructor and destructor
TMsiDatabase::TMsiDatabase(MSIHANDLE hMsiDb, FILETIME & ft)
{
InitializeCriticalSection(&m_Lock);
m_MagicSignature = MSI_MAGIC_SIGNATURE;
m_pFileEntry = NULL;
m_pLastFile = NULL;
m_FileTime = ft;
m_hMsiDb = hMsiDb;
m_dwTables = 0;
m_dwFiles = 0;
m_dwRefs = 1;
// The list head is empty
InitializeListHead(&m_Tables);
InitializeListHead(&m_Files);
}
TMsiDatabase::~TMsiDatabase()
{
#ifdef _DEBUG
UINT nHandleCount;
// Only one handle should be open now
if((nHandleCount = MSI_DUMP_HANDLES()) > 1)
{
Dbg(_T("Handle leak detected (%u handles)\n"), nHandleCount);
__debugbreak();
}
// Ask MSI.dll about unclosed handles
if((nHandleCount = MsiCloseAllHandles()) != 1)
{
Dbg(_T("Handle leak detected (%u handles)\n"), nHandleCount);
__debugbreak();
}
// Reference count check
if(m_dwRefs > 0)
{
Dbg(_T("Non-zero reference count detected (%u)\n"), m_dwRefs);
__debugbreak();
}
#endif
// Free the MSI handle
if(m_hMsiDb != NULL)
MSI_CLOSE_HANDLE(m_hMsiDb);
m_hMsiDb = NULL;
// Delete the lock
DeleteCriticalSection(&m_Lock);
}
//-----------------------------------------------------------------------------
// Public functions
TMsiDatabase * TMsiDatabase::FromHandle(HANDLE hHandle)
{
TMsiDatabase * pMsiDb;
if(hHandle != NULL && hHandle != INVALID_HANDLE_VALUE)
{
pMsiDb = static_cast<TMsiDatabase *>(hHandle);
if(pMsiDb->m_MagicSignature == MSI_MAGIC_SIGNATURE)
{
EnterCriticalSection(&pMsiDb->m_Lock);
pMsiDb->AddRef();
return pMsiDb;
}
}
return NULL;
}
DWORD TMsiDatabase::AddRef()
{
return InterlockedIncrement((LONG *)(&m_dwRefs));
}
DWORD TMsiDatabase::Release()
{
if(InterlockedDecrement((LONG *)(&m_dwRefs)) == 0)
{
delete this;
return 0;
}
return m_dwRefs;
}
void TMsiDatabase::CloseAllFiles()
{
// Free the last file, if any
ReleaseLastFile();
// Free the list of files
DeleteLinkedList<TMsiFile>(m_Files);
m_dwFiles = 0;
// Free list of tables
DeleteLinkedList<TMsiTable>(m_Tables);
m_dwTables = 0;
}
TMsiFile * TMsiDatabase::ReleaseLastFile(TMsiFile * pMsiFile)
{
// Release the old last file
if(m_pLastFile != NULL)
m_pLastFile->Release();
m_pLastFile = NULL;
// Set the new last file
if(pMsiFile != NULL)
pMsiFile->AddRef();
m_pLastFile = pMsiFile;
// Return the addded file
return pMsiFile;
}
TMsiFile * TMsiDatabase::FindReferencedFile(TMsiTable * pMsiTable, LPCTSTR szStreamName, LPTSTR szFileName, size_t ccFileName)
{
TMsiFile * pRefFile = NULL;
LPCTSTR szDot;
TCHAR szRefFile[MAX_PATH];
// References to other files are only in the "_Streams" table
if(pMsiTable->m_bIsStreamsTable)
{
// There must be dot in the name, like "Binary.bannrbmp"
if((szDot = _tcschr(szStreamName, _T('.'))) != NULL)
{
// Create the referenced file name
StringCchCopy(szRefFile, _countof(szRefFile), szStreamName);
szRefFile[szDot - szStreamName] = _T('\\');
// Is it in the database?
if((pRefFile = IsFilePresent(szRefFile)) != NULL)
{
StringCchPrintf(szFileName, ccFileName, _T("%s\\%s"), pMsiTable->Name(), szDot + 1);
}
}
}
return pRefFile;
}
void TMsiDatabase::UnlockAndRelease()
{
LeaveCriticalSection(&m_Lock);
Release();
}
TMsiFile * TMsiDatabase::GetNextFile()
{
PLIST_ENTRY pHeadEntry = &m_Files;
TMsiFile * pMsiFile;
DWORD dwErrCode = ERROR_SUCCESS;
// Files are not loaded yet
if(m_pFileEntry == NULL)
{
// Shall we load the tables?
if(dwErrCode == ERROR_SUCCESS && m_TableNames.size() == 0)
dwErrCode = LoadTableNames();
// Shall we load all tables?
if(dwErrCode == ERROR_SUCCESS && m_TableNames.size() && IsListEmpty(&m_Tables))
dwErrCode = LoadTables();
// Shall we load all files?
if(dwErrCode == ERROR_SUCCESS && !IsListEmpty(&m_Tables) && IsListEmpty(&m_Files))
dwErrCode = LoadFiles();
// Setup the file iteration
m_pFileEntry = m_Files.Flink;
}
// Do we have some files?
if(m_pFileEntry != pHeadEntry)
{
// Get the next file
pMsiFile = CONTAINING_RECORD(m_pFileEntry, TMsiFile, m_Entry);
m_pFileEntry = m_pFileEntry->Flink;
// Make sure that we have the file size
pMsiFile->LoadFileInternal(NULL);
pMsiFile->AddRef();
// Set the new last file
return ReleaseLastFile(pMsiFile);
}
return NULL;
}
DWORD TMsiDatabase::LoadTableNameIfExists(LPCTSTR szTableName)
{
MSIHANDLE hMsiView = NULL;
DWORD dwErrCode;
TCHAR szQuery[256];
StringCchPrintf(szQuery, _countof(szQuery), _T("SELECT * from %s"), szTableName);
if((dwErrCode = MsiDatabaseOpenView(m_hMsiDb, szQuery, &hMsiView)) == ERROR_SUCCESS)
{
// Log the handle for diagnostics
MSI_LOG_OPEN_HANDLE(hMsiView);
// Insert the table to the table list
if(!FindStringInList(m_TableNames, szTableName))
{
m_TableNames.push_back(szTableName);
}
MSI_CLOSE_HANDLE(hMsiView);
}
return dwErrCode;
}
DWORD TMsiDatabase::LoadTableNames()
{
std::tstring strTableName;
MSIHANDLE hMsiRecord = NULL;
MSIHANDLE hMsiView = NULL;
DWORD dwErrCode;
// The "_Validation" table contain list of all tables in the MSI
if((dwErrCode = MsiDatabaseOpenView(m_hMsiDb, _T("SELECT * from _Validation"), &hMsiView)) == ERROR_SUCCESS)
{
// Log the handle for diagnostics
MSI_LOG_OPEN_HANDLE(hMsiView);
// Execute the query
if((dwErrCode = MsiViewExecute(hMsiView, NULL)) == ERROR_SUCCESS)
{
// Dump all records
while(MsiViewFetch(hMsiView, &hMsiRecord) == ERROR_SUCCESS)
{
// Log the handle for diagnostics
MSI_LOG_OPEN_HANDLE(hMsiRecord);
// Retrieve the table name
if(MsiRecordGetString(hMsiRecord, 0, strTableName))
{
if(!FindStringInList(m_TableNames, strTableName))
{
m_TableNames.push_back(strTableName);
}
}
MSI_CLOSE_HANDLE(hMsiRecord);
}
// Finalize the view
MsiViewClose(hMsiView);
}
MSI_CLOSE_HANDLE(hMsiView);
}
// Some tables are not always in the list of tables. Check them hardcoded
LoadTableNameIfExists(_T("_ForceCodePage"));
LoadTableNameIfExists(_T("_Streams"));
// Return success if there is at least one table
return m_TableNames.size() ? ERROR_SUCCESS : ERROR_NO_MORE_ITEMS;
}
DWORD TMsiDatabase::LoadTables()
{
TMsiTable * pMsiTable;
DWORD dwErrCode = ERROR_SUCCESS;
// Enumerate table names
for(size_t i = 0; i < m_TableNames.size(); i++)
{
const std::tstring & strTableName = m_TableNames[i];
MSIHANDLE hMsiView = NULL;
TCHAR szQuery[256];
// Load the list of columns of the table
StringCchPrintf(szQuery, _countof(szQuery), _T("SELECT * FROM %s"), strTableName.c_str());
if(MsiDatabaseOpenView(m_hMsiDb, szQuery, &hMsiView) == ERROR_SUCCESS)
{
// Log the handle for diagnostics
MSI_LOG_OPEN_HANDLE(hMsiView);
// Create the TMsiTable object
if((pMsiTable = new TMsiTable(this, strTableName, hMsiView)) != NULL)
{
if(pMsiTable->Load() == ERROR_SUCCESS)
{
InsertTailList(&m_Tables, &pMsiTable->m_Entry);
InterlockedIncrement((LONG *)(&m_dwTables));
pMsiTable = NULL;
}
else
{
pMsiTable->Release();
pMsiTable = NULL;
}
}
else
{
dwErrCode = ERROR_NOT_ENOUGH_MEMORY;
MSI_CLOSE_HANDLE(hMsiView);
break;
}
}
}
return dwErrCode;
}
DWORD TMsiDatabase::LoadFiles()
{
PLIST_ENTRY pHeadEntry = &m_Tables;
PLIST_ENTRY pListEntry;
MSIHANDLE hMsiSummary = NULL;
UINT nPropertyCount = 0;
// Load the summary information
if(MsiGetSummaryInformation(m_hMsiDb, NULL, 0, &hMsiSummary) == ERROR_SUCCESS)
{
// Log the handle for diagnostics
MSI_LOG_OPEN_HANDLE(hMsiSummary);
// Get the number of items
if(MsiSummaryInfoGetPropertyCount(hMsiSummary, &nPropertyCount) == ERROR_SUCCESS)
{
if(LoadSummaryFile(hMsiSummary) == ERROR_SUCCESS)
{
hMsiSummary = NULL;
}
}
if(hMsiSummary != NULL)
{
MSI_CLOSE_HANDLE(hMsiSummary);
}
}
// Load the tables
for(pListEntry = pHeadEntry->Flink; pListEntry != pHeadEntry; pListEntry = pListEntry->Flink)
{
TMsiTable * pMsiTable = CONTAINING_RECORD(pListEntry, TMsiTable, m_Entry);
// Is it a database table with stream field?
if(pMsiTable->m_nStreamColumn != INVALID_SIZE_T && pMsiTable->m_nNameColumn != INVALID_SIZE_T)
{
LoadMultipleStreamFiles(pMsiTable);
}
// Simple database table - we simulate it as simple CSV file
else
{
LoadSimpleCsvFile(pMsiTable);
}
}
return ERROR_SUCCESS;
}
DWORD TMsiDatabase::LoadMultipleStreamFiles(TMsiTable * pMsiTable)
{
TMsiFile * pMsiFile;
MSIHANDLE hMsiRecord;
MSIHANDLE hMsiView = pMsiTable->m_hMsiView;
DWORD dwErrCode;
// Execute the query
if((dwErrCode = MsiViewExecute(hMsiView, NULL)) == ERROR_SUCCESS)
{
// Dump all records
while(MsiViewFetch(hMsiView, &hMsiRecord) == ERROR_SUCCESS)
{
// Log the handle for diagnostics
MSI_LOG_OPEN_HANDLE(hMsiRecord);
// Create the TMsiFile object
if((pMsiFile = new TMsiFile(pMsiTable)) != NULL)
{
if((dwErrCode = pMsiFile->SetBinaryFile(this, hMsiRecord)) == ERROR_SUCCESS)
{
InsertTailList(&m_Files, &pMsiFile->m_Entry);
InterlockedIncrement((LONG *)(&m_dwFiles));
}
else
{
MSI_CLOSE_HANDLE(hMsiRecord);
pMsiFile->Release();
}
}
}
// Finalize the executed view
MsiViewClose(hMsiView);
}
return dwErrCode;
}
DWORD TMsiDatabase::LoadSimpleCsvFile(TMsiTable * pMsiTable)
{
TMsiFile * pMsiFile;
DWORD dwErrCode = ERROR_NOT_ENOUGH_MEMORY;
if((pMsiFile = new TMsiFile(pMsiTable)) != NULL)
{
if((dwErrCode = pMsiFile->SetCsvFile(this)) == ERROR_SUCCESS)
{
InsertTailList(&m_Files, &pMsiFile->m_Entry);
InterlockedIncrement((LONG *)(&m_dwFiles));
}
else
{
pMsiFile->Release();
}
}
return dwErrCode;
}
DWORD TMsiDatabase::LoadSummaryFile(MSIHANDLE hMsiSummary)
{
TMsiFile* pMsiFile;
DWORD dwErrCode = ERROR_NOT_ENOUGH_MEMORY;
if((pMsiFile = new TMsiFile(NULL)) != NULL)
{
if((dwErrCode = pMsiFile->SetSummaryFile(this, hMsiSummary)) == ERROR_SUCCESS)
{
InsertTailList(&m_Files, &pMsiFile->m_Entry);
InterlockedIncrement((LONG*)(&m_dwFiles));
}
else
{
pMsiFile->Release();
}
}
return dwErrCode;
}
TMsiFile * TMsiDatabase::IsFilePresent(LPCTSTR szFileName)
{
PLIST_ENTRY pHeadEntry = &m_Files;
PLIST_ENTRY pListEntry;
for(pListEntry = pHeadEntry->Flink; pListEntry != pHeadEntry; pListEntry = pListEntry->Flink)
{
TMsiFile * pMsiFile = CONTAINING_RECORD(pListEntry, TMsiFile, m_Entry);
if(!_tcsicmp(pMsiFile->Name(), szFileName))
{
return pMsiFile;
}
}
return NULL;
}
TMsiFile * TMsiDatabase::LastFile()
{
if(m_pLastFile != NULL)
m_pLastFile->AddRef();
return m_pLastFile;
}