[APPHELP] Fix warnings
[reactos.git] / dll / appcompat / apphelp / sdbapi.c
1 /*
2 * PROJECT: ReactOS Application compatibility module
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Sdb low level glue layer
5 * COPYRIGHT: Copyright 2011 André Hentschel
6 * Copyright 2013 Mislav Blaževic
7 * Copyright 2015-2019 Mark Jansen (mark.jansen@reactos.org)
8 */
9
10 #include "ntndk.h"
11 #include "strsafe.h"
12 #include "apphelp.h"
13 #include "sdbstringtable.h"
14
15
16 static const GUID GUID_DATABASE_MSI = {0xd8ff6d16,0x6a3a,0x468a, {0x8b,0x44,0x01,0x71,0x4d,0xdc,0x49,0xea}};
17 static const GUID GUID_DATABASE_SHIM = {0x11111111,0x1111,0x1111, {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11}};
18 static const GUID GUID_DATABASE_DRIVERS = {0xf9ab2228,0x3312,0x4a73, {0xb6,0xf9,0x93,0x6d,0x70,0xe1,0x12,0xef}};
19
20 static HANDLE SdbpHeap(void);
21
22 #if SDBAPI_DEBUG_ALLOC
23
24 /* dbgheap.c */
25 void SdbpInsertAllocation(PVOID address, SIZE_T size, int line, const char* file);
26 void SdbpUpdateAllocation(PVOID address, PVOID newaddress, SIZE_T size, int line, const char* file);
27 void SdbpRemoveAllocation(PVOID address, int line, const char* file);
28 void SdbpDebugHeapInit(HANDLE privateHeapPtr);
29 void SdbpDebugHeapDeinit(void);
30
31 #endif
32
33 static HANDLE g_Heap;
34 void SdbpHeapInit(void)
35 {
36 g_Heap = RtlCreateHeap(HEAP_GROWABLE, NULL, 0, 0x10000, NULL, NULL);
37 #if SDBAPI_DEBUG_ALLOC
38 SdbpDebugHeapInit(g_Heap);
39 #endif
40 }
41
42 void SdbpHeapDeinit(void)
43 {
44 #if SDBAPI_DEBUG_ALLOC
45 SdbpDebugHeapDeinit();
46 #endif
47 RtlDestroyHeap(g_Heap);
48 }
49
50 static HANDLE SdbpHeap(void)
51 {
52 return g_Heap;
53 }
54
55 LPVOID SdbpAlloc(SIZE_T size
56 #if SDBAPI_DEBUG_ALLOC
57 , int line, const char* file
58 #endif
59 )
60 {
61 LPVOID mem = RtlAllocateHeap(SdbpHeap(), HEAP_ZERO_MEMORY, size);
62 #if SDBAPI_DEBUG_ALLOC
63 SdbpInsertAllocation(mem, size, line, file);
64 #endif
65 return mem;
66 }
67
68 LPVOID SdbpReAlloc(LPVOID mem, SIZE_T size, SIZE_T oldSize
69 #if SDBAPI_DEBUG_ALLOC
70 , int line, const char* file
71 #endif
72 )
73 {
74 LPVOID newmem = RtlReAllocateHeap(SdbpHeap(), HEAP_ZERO_MEMORY, mem, size);
75 #if SDBAPI_DEBUG_ALLOC
76 SdbpUpdateAllocation(mem, newmem, size, line, file);
77 #endif
78 return newmem;
79 }
80
81 void SdbpFree(LPVOID mem
82 #if SDBAPI_DEBUG_ALLOC
83 , int line, const char* file
84 #endif
85 )
86 {
87 #if SDBAPI_DEBUG_ALLOC
88 SdbpRemoveAllocation(mem, line, file);
89 #endif
90 RtlFreeHeap(SdbpHeap(), 0, mem);
91 }
92
93 PDB WINAPI SdbpCreate(LPCWSTR path, PATH_TYPE type, BOOL write)
94 {
95 NTSTATUS Status;
96 IO_STATUS_BLOCK io;
97 OBJECT_ATTRIBUTES attr;
98 UNICODE_STRING str;
99 PDB pdb;
100
101 if (type == DOS_PATH)
102 {
103 if (!RtlDosPathNameToNtPathName_U(path, &str, NULL, NULL))
104 return NULL;
105 }
106 else
107 {
108 RtlInitUnicodeString(&str, path);
109 }
110
111 /* SdbAlloc zeroes the memory. */
112 pdb = (PDB)SdbAlloc(sizeof(DB));
113 if (!pdb)
114 {
115 SHIM_ERR("Failed to allocate memory for shim database\n");
116 return NULL;
117 }
118
119 InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, NULL, NULL);
120
121 Status = NtCreateFile(&pdb->file, (write ? FILE_GENERIC_WRITE : FILE_GENERIC_READ )| SYNCHRONIZE,
122 &attr, &io, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
123 write ? FILE_SUPERSEDE : FILE_OPEN, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
124
125 pdb->for_write = write;
126
127 if (type == DOS_PATH)
128 RtlFreeUnicodeString(&str);
129
130 if (!NT_SUCCESS(Status))
131 {
132 SdbCloseDatabase(pdb);
133 SHIM_ERR("Failed to create shim database file: %lx\n", Status);
134 return NULL;
135 }
136
137 return pdb;
138 }
139
140 void WINAPI SdbpFlush(PDB pdb)
141 {
142 IO_STATUS_BLOCK io;
143 NTSTATUS Status;
144
145 ASSERT(pdb->for_write);
146 Status = NtWriteFile(pdb->file, NULL, NULL, NULL, &io,
147 pdb->data, pdb->write_iter, NULL, NULL);
148 if (!NT_SUCCESS(Status))
149 SHIM_WARN("failed with 0x%lx\n", Status);
150 }
151
152 DWORD SdbpStrlen(PCWSTR string)
153 {
154 return (DWORD)wcslen(string);
155 }
156
157 DWORD SdbpStrsize(PCWSTR string)
158 {
159 return (SdbpStrlen(string) + 1) * sizeof(WCHAR);
160 }
161
162 PWSTR SdbpStrDup(LPCWSTR string)
163 {
164 PWSTR ret = SdbAlloc(SdbpStrsize(string));
165 wcscpy(ret, string);
166 return ret;
167 }
168
169
170 BOOL WINAPI SdbpOpenMemMappedFile(LPCWSTR path, PMEMMAPPED mapping)
171 {
172 NTSTATUS Status;
173 OBJECT_ATTRIBUTES ObjectAttributes;
174 IO_STATUS_BLOCK IoStatusBlock;
175 FILE_STANDARD_INFORMATION FileStandard;
176 UNICODE_STRING FileName;
177
178 RtlZeroMemory(mapping, sizeof(*mapping));
179
180 RtlInitUnicodeString(&FileName, path);
181
182 InitializeObjectAttributes(&ObjectAttributes, &FileName, OBJ_CASE_INSENSITIVE, NULL, NULL);
183 Status = NtOpenFile(&mapping->file, GENERIC_READ | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT);
184
185 if (Status == STATUS_OBJECT_NAME_INVALID || Status == STATUS_OBJECT_PATH_SYNTAX_BAD)
186 {
187 if (!RtlDosPathNameToNtPathName_U(path, &FileName, NULL, NULL))
188 {
189 SHIM_ERR("Failed to convert %S to Nt path: 0x%lx\n", path, Status);
190 return FALSE;
191 }
192 InitializeObjectAttributes(&ObjectAttributes, &FileName, OBJ_CASE_INSENSITIVE, NULL, NULL);
193 Status = NtOpenFile(&mapping->file, GENERIC_READ | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT);
194 RtlFreeUnicodeString(&FileName);
195 }
196
197 if (!NT_SUCCESS(Status))
198 {
199 SHIM_ERR("Failed to open file %S: 0x%lx\n", path, Status);
200 return FALSE;
201 }
202
203 Status = NtCreateSection(&mapping->section, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ, 0, 0, PAGE_READONLY, SEC_COMMIT, mapping->file);
204 if (!NT_SUCCESS(Status))
205 {
206 /* Special case */
207 if (Status == STATUS_MAPPED_FILE_SIZE_ZERO)
208 {
209 NtClose(mapping->file);
210 mapping->file = mapping->section = NULL;
211 return TRUE;
212 }
213 SHIM_ERR("Failed to create mapping for file: 0x%lx\n", Status);
214 goto err_out;
215 }
216
217 Status = NtQueryInformationFile(mapping->file, &IoStatusBlock, &FileStandard, sizeof(FileStandard), FileStandardInformation);
218 if (!NT_SUCCESS(Status))
219 {
220 SHIM_ERR("Failed to read file info for file: 0x%lx\n", Status);
221 goto err_out;
222 }
223
224 mapping->mapped_size = mapping->size = FileStandard.EndOfFile.LowPart;
225 Status = NtMapViewOfSection(mapping->section, NtCurrentProcess(), (PVOID*)&mapping->view, 0, 0, 0, &mapping->mapped_size, ViewUnmap, 0, PAGE_READONLY);
226 if (!NT_SUCCESS(Status))
227 {
228 SHIM_ERR("Failed to map view of file: 0x%lx\n", Status);
229 goto err_out;
230 }
231
232 return TRUE;
233
234 err_out:
235 if (!mapping->view)
236 {
237 if (mapping->section)
238 NtClose(mapping->section);
239 NtClose(mapping->file);
240 }
241 return FALSE;
242 }
243
244 void WINAPI SdbpCloseMemMappedFile(PMEMMAPPED mapping)
245 {
246 /* Prevent a VAD warning */
247 if (mapping->view)
248 NtUnmapViewOfSection(NtCurrentProcess(), mapping->view);
249 NtClose(mapping->section);
250 NtClose(mapping->file);
251 RtlZeroMemory(mapping, sizeof(*mapping));
252 }
253
254 BOOL WINAPI SdbpCheckTagType(TAG tag, WORD type)
255 {
256 if ((tag & TAG_TYPE_MASK) != type)
257 return FALSE;
258 return TRUE;
259 }
260
261 BOOL WINAPI SdbpCheckTagIDType(PDB pdb, TAGID tagid, WORD type)
262 {
263 TAG tag = SdbGetTagFromTagID(pdb, tagid);
264 if (tag == TAG_NULL)
265 return FALSE;
266 return SdbpCheckTagType(tag, type);
267 }
268
269 PDB SdbpOpenDatabase(LPCWSTR path, PATH_TYPE type)
270 {
271 IO_STATUS_BLOCK io;
272 FILE_STANDARD_INFORMATION fsi;
273 PDB pdb;
274 NTSTATUS Status;
275 BYTE header[12];
276
277 pdb = SdbpCreate(path, type, FALSE);
278 if (!pdb)
279 return NULL;
280
281 Status = NtQueryInformationFile(pdb->file, &io, &fsi, sizeof(FILE_STANDARD_INFORMATION), FileStandardInformation);
282 if (!NT_SUCCESS(Status))
283 {
284 SdbCloseDatabase(pdb);
285 SHIM_ERR("Failed to get shim database size: 0x%lx\n", Status);
286 return NULL;
287 }
288
289 pdb->size = fsi.EndOfFile.u.LowPart;
290 pdb->data = SdbAlloc(pdb->size);
291 Status = NtReadFile(pdb->file, NULL, NULL, NULL, &io, pdb->data, pdb->size, NULL, NULL);
292
293 if (!NT_SUCCESS(Status))
294 {
295 SdbCloseDatabase(pdb);
296 SHIM_ERR("Failed to open shim database file: 0x%lx\n", Status);
297 return NULL;
298 }
299
300 if (!SdbpReadData(pdb, &header, 0, 12))
301 {
302 SdbCloseDatabase(pdb);
303 SHIM_ERR("Failed to read shim database header\n");
304 return NULL;
305 }
306
307 if (memcmp(&header[8], "sdbf", 4) != 0)
308 {
309 SdbCloseDatabase(pdb);
310 SHIM_ERR("Shim database header is invalid\n");
311 return NULL;
312 }
313
314 pdb->major = *(DWORD*)&header[0];
315 pdb->minor = *(DWORD*)&header[4];
316
317 return pdb;
318 }
319
320
321 /**
322 * Opens specified shim database file.
323 *
324 * @param [in] path Path to the shim database.
325 * @param [in] type Type of path. Either DOS_PATH or NT_PATH.
326 *
327 * @return Success: Handle to the shim database, NULL otherwise.
328 */
329 PDB WINAPI SdbOpenDatabase(LPCWSTR path, PATH_TYPE type)
330 {
331 PDB pdb;
332 TAGID root, name;
333
334 pdb = SdbpOpenDatabase(path, type);
335 if (!pdb)
336 return NULL;
337
338 if (pdb->major != 2 && pdb->major != 3)
339 {
340 SdbCloseDatabase(pdb);
341 SHIM_ERR("Invalid shim database version\n");
342 return NULL;
343 }
344
345 pdb->stringtable = SdbFindFirstTag(pdb, TAGID_ROOT, TAG_STRINGTABLE);
346 if (!SdbGetDatabaseID(pdb, &pdb->database_id))
347 {
348 SHIM_INFO("Failed to get the database id\n");
349 }
350
351 root = SdbFindFirstTag(pdb, TAGID_ROOT, TAG_DATABASE);
352 if (root != TAGID_NULL)
353 {
354 name = SdbFindFirstTag(pdb, root, TAG_NAME);
355 if (name != TAGID_NULL)
356 {
357 pdb->database_name = SdbGetStringTagPtr(pdb, name);
358 }
359 }
360 if (!pdb->database_name)
361 {
362 SHIM_INFO("Failed to get the database name\n");
363 }
364
365 return pdb;
366 }
367
368 /**
369 * Closes specified database and frees its memory.
370 *
371 * @param [in] pdb Handle to the shim database.
372 */
373 void WINAPI SdbCloseDatabase(PDB pdb)
374 {
375 if (!pdb)
376 return;
377
378 if (pdb->file)
379 NtClose(pdb->file);
380 if (pdb->string_buffer)
381 SdbCloseDatabase(pdb->string_buffer);
382 if (pdb->string_lookup)
383 SdbpTableDestroy(&pdb->string_lookup);
384 SdbFree(pdb->data);
385 SdbFree(pdb);
386 }
387
388 /**
389 * Parses a string to retrieve a GUID.
390 *
391 * @param [in] GuidString The string to parse.
392 * @param [out] Guid The resulting GUID.
393 *
394 * @return TRUE if it succeeds, FALSE if it fails.
395 */
396 BOOL WINAPI SdbGUIDFromString(PCWSTR GuidString, GUID *Guid)
397 {
398 UNICODE_STRING GuidString_u;
399 RtlInitUnicodeString(&GuidString_u, GuidString);
400 return NT_SUCCESS(RtlGUIDFromString(&GuidString_u, Guid));
401 }
402
403 /**
404 * Converts a GUID to a string.
405 *
406 * @param [in] Guid The GUID to convert.
407 * @param [out] GuidString The resulting string representation of Guid.
408 * @param [in] Length The length of GuidString.
409 *
410 * @return TRUE if it succeeds, FALSE if it fails.
411 */
412 BOOL WINAPI SdbGUIDToString(CONST GUID *Guid, PWSTR GuidString, SIZE_T Length)
413 {
414 UNICODE_STRING GuidString_u;
415 if (NT_SUCCESS(RtlStringFromGUID(Guid, &GuidString_u)))
416 {
417 HRESULT hr = StringCchCopyNW(GuidString, Length, GuidString_u.Buffer, GuidString_u.Length / 2);
418 RtlFreeUnicodeString(&GuidString_u);
419 return SUCCEEDED(hr);
420 }
421 return FALSE;
422 }
423
424 /**
425 * Checks if the specified GUID is a NULL GUID
426 *
427 * @param [in] Guid The GUID to check.
428 *
429 * @return TRUE if it is a NULL GUID.
430 */
431 BOOL WINAPI SdbIsNullGUID(CONST GUID *Guid)
432 {
433 static GUID NullGuid = { 0 };
434 return !Guid || IsEqualGUID(&NullGuid, Guid);
435 }
436
437 /**
438 * Get the GUID from one of the standard databases.
439 *
440 * @param [in] Flags The ID to retrieve the guid from. (See SDB_DATABASE_MAIN_[xxx])
441 * @param [out] Guid The resulting GUID.
442 *
443 * @return TRUE if a known database ID.
444 */
445 BOOL WINAPI SdbGetStandardDatabaseGUID(DWORD Flags, GUID* Guid)
446 {
447 const GUID* copy_from = NULL;
448 switch(Flags & HID_DATABASE_TYPE_MASK)
449 {
450 case SDB_DATABASE_MAIN_MSI:
451 copy_from = &GUID_DATABASE_MSI;
452 break;
453 case SDB_DATABASE_MAIN_SHIM:
454 copy_from = &GUID_DATABASE_SHIM;
455 break;
456 case SDB_DATABASE_MAIN_DRIVERS:
457 copy_from = &GUID_DATABASE_DRIVERS;
458 break;
459 default:
460 SHIM_ERR("Cannot obtain database guid for databases other than main\n");
461 return FALSE;
462 }
463 if (Guid)
464 {
465 memcpy(Guid, copy_from, sizeof(GUID));
466 }
467 return TRUE;
468 }
469
470 /**
471 * Read the database version from the specified database.
472 *
473 * @param [in] database The database.
474 * @param [out] VersionHi The first part of the version number.
475 * @param [out] VersionLo The second part of the version number.
476 *
477 * @return TRUE if it succeeds or fails, FALSE if ???
478 */
479 BOOL WINAPI SdbGetDatabaseVersion(LPCWSTR database, PDWORD VersionHi, PDWORD VersionLo)
480 {
481 PDB pdb;
482
483 pdb = SdbpOpenDatabase(database, DOS_PATH);
484 if (pdb)
485 {
486 *VersionHi = pdb->major;
487 *VersionLo = pdb->minor;
488 SdbCloseDatabase(pdb);
489 }
490
491 return TRUE;
492 }
493
494 /**
495 * @name SdbGetDatabaseInformation
496 * Get information about the database
497 *
498 * @param pdb The database
499 * @param information The returned information
500 * @return TRUE on success
501 */
502 BOOL WINAPI SdbGetDatabaseInformation(PDB pdb, PDB_INFORMATION information)
503 {
504 if (pdb && information)
505 {
506 information->dwFlags = 0;
507 information->dwMajor = pdb->major;
508 information->dwMinor = pdb->minor;
509 information->Description = pdb->database_name;
510 if (!SdbIsNullGUID(&pdb->database_id))
511 {
512 information->dwFlags |= DB_INFO_FLAGS_VALID_GUID;
513 information->Id = pdb->database_id;
514 }
515 return TRUE;
516 }
517
518 return FALSE;
519 }
520
521 /**
522 * @name SdbFreeDatabaseInformation
523 * Free up resources allocated in SdbGetDatabaseInformation
524 *
525 * @param information The information retrieved from SdbGetDatabaseInformation
526 */
527 VOID WINAPI SdbFreeDatabaseInformation(PDB_INFORMATION information)
528 {
529 // No-op
530 }
531
532
533 /**
534 * Find the first named child tag.
535 *
536 * @param [in] pdb The database.
537 * @param [in] root The tag to start at
538 * @param [in] find The tag type to find
539 * @param [in] nametag The child of 'find' that contains the name
540 * @param [in] find_name The name to find
541 *
542 * @return The found tag, or TAGID_NULL on failure
543 */
544 TAGID WINAPI SdbFindFirstNamedTag(PDB pdb, TAGID root, TAGID find, TAGID nametag, LPCWSTR find_name)
545 {
546 TAGID iter;
547
548 iter = SdbFindFirstTag(pdb, root, find);
549
550 while (iter != TAGID_NULL)
551 {
552 TAGID tmp = SdbFindFirstTag(pdb, iter, nametag);
553 if (tmp != TAGID_NULL)
554 {
555 LPCWSTR name = SdbGetStringTagPtr(pdb, tmp);
556 if (name && !wcsicmp(name, find_name))
557 return iter;
558 }
559 iter = SdbFindNextTag(pdb, root, iter);
560 }
561 return TAGID_NULL;
562 }
563
564
565 /**
566 * Find a named layer in a multi-db.
567 *
568 * @param [in] hsdb The multi-database.
569 * @param [in] layerName The named tag to find.
570 *
571 * @return The layer, or TAGREF_NULL on failure
572 */
573 TAGREF WINAPI SdbGetLayerTagRef(HSDB hsdb, LPCWSTR layerName)
574 {
575 PDB pdb = hsdb->pdb;
576
577 TAGID database = SdbFindFirstTag(pdb, TAGID_ROOT, TAG_DATABASE);
578 if (database != TAGID_NULL)
579 {
580 TAGID layer = SdbFindFirstNamedTag(pdb, database, TAG_LAYER, TAG_NAME, layerName);
581 if (layer != TAGID_NULL)
582 {
583 TAGREF tr;
584 if (SdbTagIDToTagRef(hsdb, pdb, layer, &tr))
585 {
586 return tr;
587 }
588 }
589 }
590 return TAGREF_NULL;
591 }
592
593
594 #ifndef REG_SZ
595 #define REG_SZ 1
596 #define REG_DWORD 4
597 #define REG_QWORD 11
598 #endif
599
600
601 /**
602 * Retrieve a Data entry
603 *
604 * @param [in] pdb The database.
605 * @param [in] tiExe The tagID to start at
606 * @param [in,opt] lpszDataName The name of the Data entry to find, or NULL to return all.
607 * @param [out,opt] lpdwDataType Any of REG_SZ, REG_QWORD, REG_DWORD, ...
608 * @param [out] lpBuffer The output buffer
609 * @param [in,out,opt] lpcbBufferSize The size of lpBuffer in bytes
610 * @param [out,opt] ptiData The tagID of the data
611 *
612 * @return ERROR_SUCCESS
613 */
614 DWORD WINAPI SdbQueryDataExTagID(PDB pdb, TAGID tiExe, LPCWSTR lpszDataName, LPDWORD lpdwDataType, LPVOID lpBuffer, LPDWORD lpcbBufferSize, TAGID *ptiData)
615 {
616 TAGID tiData, tiValueType, tiValue;
617 DWORD dwDataType, dwSizeRequired, dwInputSize;
618 LPCWSTR lpStringData;
619 /* Not supported yet */
620 if (!lpszDataName)
621 return ERROR_INVALID_PARAMETER;
622
623 tiData = SdbFindFirstNamedTag(pdb, tiExe, TAG_DATA, TAG_NAME, lpszDataName);
624 if (tiData == TAGID_NULL)
625 {
626 SHIM_INFO("No data tag found\n");
627 return ERROR_NOT_FOUND;
628 }
629
630 if (ptiData)
631 *ptiData = tiData;
632
633 tiValueType = SdbFindFirstTag(pdb, tiData, TAG_DATA_VALUETYPE);
634 if (tiValueType == TAGID_NULL)
635 {
636 SHIM_WARN("Data tag (0x%x) without valuetype\n", tiData);
637 return ERROR_INTERNAL_DB_CORRUPTION;
638 }
639
640 dwDataType = SdbReadDWORDTag(pdb, tiValueType, 0);
641 switch (dwDataType)
642 {
643 case REG_SZ:
644 tiValue = SdbFindFirstTag(pdb, tiData, TAG_DATA_STRING);
645 break;
646 case REG_DWORD:
647 tiValue = SdbFindFirstTag(pdb, tiData, TAG_DATA_DWORD);
648 break;
649 case REG_QWORD:
650 tiValue = SdbFindFirstTag(pdb, tiData, TAG_DATA_QWORD);
651 break;
652 default:
653 /* Not supported (yet) */
654 SHIM_WARN("Unsupported dwDataType=0x%x\n", dwDataType);
655 return ERROR_INVALID_PARAMETER;
656 }
657
658 if (lpdwDataType)
659 *lpdwDataType = dwDataType;
660
661 if (tiValue == TAGID_NULL)
662 {
663 SHIM_WARN("Data tag (0x%x) without data\n", tiData);
664 return ERROR_INTERNAL_DB_CORRUPTION;
665 }
666
667 if (dwDataType != REG_SZ)
668 {
669 dwSizeRequired = SdbGetTagDataSize(pdb, tiValue);
670 }
671 else
672 {
673 lpStringData = SdbpGetString(pdb, tiValue, &dwSizeRequired);
674 if (lpStringData == NULL)
675 {
676 return ERROR_INTERNAL_DB_CORRUPTION;
677 }
678 }
679 if (!lpcbBufferSize)
680 return ERROR_INSUFFICIENT_BUFFER;
681
682 dwInputSize = *lpcbBufferSize;
683 *lpcbBufferSize = dwSizeRequired;
684
685 if (dwInputSize < dwSizeRequired || lpBuffer == NULL)
686 {
687 SHIM_WARN("dwInputSize %u not sufficient to hold %u bytes\n", dwInputSize, dwSizeRequired);
688 return ERROR_INSUFFICIENT_BUFFER;
689 }
690
691 if (dwDataType != REG_SZ)
692 {
693 SdbpReadData(pdb, lpBuffer, tiValue + sizeof(TAG), dwSizeRequired);
694 }
695 else
696 {
697 StringCbCopyNW(lpBuffer, dwInputSize, lpStringData, dwSizeRequired);
698 }
699
700 return ERROR_SUCCESS;
701 }
702
703
704 /**
705 * Converts the specified string to an index key.
706 *
707 * @param [in] str The string which will be converted.
708 *
709 * @return The resulting index key
710 *
711 * @todo: Fix this for unicode strings.
712 */
713 LONGLONG WINAPI SdbMakeIndexKeyFromString(LPCWSTR str)
714 {
715 LONGLONG result = 0;
716 int shift = 56;
717
718 while (*str && shift >= 0)
719 {
720 WCHAR c = toupper(*(str++));
721
722 if (c & 0xff)
723 {
724 result |= (((LONGLONG)(c & 0xff)) << shift);
725 shift -= 8;
726 }
727
728 if (shift < 0)
729 break;
730
731 c >>= 8;
732
733 if (c & 0xff)
734 {
735 result |= (((LONGLONG)(c & 0xff)) << shift);
736 shift -= 8;
737 }
738 }
739
740 return result;
741 }
742
743
744 /**
745 * Converts specified tag into a string.
746 *
747 * @param [in] tag The tag which will be converted to a string.
748 *
749 * @return Success: Pointer to the string matching specified tag, or L"InvalidTag" on failure.
750 *
751 */
752 LPCWSTR WINAPI SdbTagToString(TAG tag)
753 {
754 switch (tag)
755 {
756 case TAG_NULL: return L"NULL";
757
758 /* TAG_TYPE_NULL */
759 case TAG_INCLUDE: return L"INCLUDE";
760 case TAG_GENERAL: return L"GENERAL";
761 case TAG_MATCH_LOGIC_NOT: return L"MATCH_LOGIC_NOT";
762 case TAG_APPLY_ALL_SHIMS: return L"APPLY_ALL_SHIMS";
763 case TAG_USE_SERVICE_PACK_FILES: return L"USE_SERVICE_PACK_FILES";
764 case TAG_MITIGATION_OS: return L"MITIGATION_OS";
765 case TAG_BLOCK_UPGRADE: return L"BLOCK_UPGRADE";
766 case TAG_INCLUDEEXCLUDEDLL: return L"INCLUDEEXCLUDEDLL";
767 case TAG_RAC_EVENT_OFF: return L"RAC_EVENT_OFF";
768 case TAG_TELEMETRY_OFF: return L"TELEMETRY_OFF";
769 case TAG_SHIM_ENGINE_OFF: return L"SHIM_ENGINE_OFF";
770 case TAG_LAYER_PROPAGATION_OFF: return L"LAYER_PROPAGATION_OFF";
771 case TAG_REINSTALL_UPGRADE: return L"REINSTALL_UPGRADE";
772
773 /* TAG_TYPE_WORD */
774 case TAG_MATCH_MODE: return L"MATCH_MODE";
775 case TAG_TAG: return L"TAG";
776 case TAG_INDEX_TAG: return L"INDEX_TAG";
777 case TAG_INDEX_KEY: return L"INDEX_KEY";
778
779 /* TAG_TYPE_DWORD */
780 case TAG_SIZE: return L"SIZE";
781 case TAG_OFFSET: return L"OFFSET";
782 case TAG_CHECKSUM: return L"CHECKSUM";
783 case TAG_SHIM_TAGID: return L"SHIM_TAGID";
784 case TAG_PATCH_TAGID: return L"PATCH_TAGID";
785 case TAG_MODULE_TYPE: return L"MODULE_TYPE";
786 case TAG_VERDATEHI: return L"VERDATEHI";
787 case TAG_VERDATELO: return L"VERDATELO";
788 case TAG_VERFILEOS: return L"VERFILEOS";
789 case TAG_VERFILETYPE: return L"VERFILETYPE";
790 case TAG_PE_CHECKSUM: return L"PE_CHECKSUM";
791 case TAG_PREVOSMAJORVER: return L"PREVOSMAJORVER";
792 case TAG_PREVOSMINORVER: return L"PREVOSMINORVER";
793 case TAG_PREVOSPLATFORMID: return L"PREVOSPLATFORMID";
794 case TAG_PREVOSBUILDNO: return L"PREVOSBUILDNO";
795 case TAG_PROBLEMSEVERITY: return L"PROBLEMSEVERITY";
796 case TAG_LANGID: return L"LANGID";
797 case TAG_VER_LANGUAGE: return L"VER_LANGUAGE";
798 case TAG_ENGINE: return L"ENGINE";
799 case TAG_HTMLHELPID: return L"HTMLHELPID";
800 case TAG_INDEX_FLAGS: return L"INDEX_FLAGS";
801 case TAG_FLAGS: return L"FLAGS";
802 case TAG_DATA_VALUETYPE: return L"DATA_VALUETYPE";
803 case TAG_DATA_DWORD: return L"DATA_DWORD";
804 case TAG_LAYER_TAGID: return L"LAYER_TAGID";
805 case TAG_MSI_TRANSFORM_TAGID: return L"MSI_TRANSFORM_TAGID";
806 case TAG_LINKER_VERSION: return L"LINKER_VERSION";
807 case TAG_LINK_DATE: return L"LINK_DATE";
808 case TAG_UPTO_LINK_DATE: return L"UPTO_LINK_DATE";
809 case TAG_OS_SERVICE_PACK: return L"OS_SERVICE_PACK";
810 case TAG_FLAG_TAGID: return L"FLAG_TAGID";
811 case TAG_RUNTIME_PLATFORM: return L"RUNTIME_PLATFORM";
812 case TAG_OS_SKU: return L"OS_SKU";
813 case TAG_OS_PLATFORM: return L"OS_PLATFORM";
814 case TAG_APP_NAME_RC_ID: return L"APP_NAME_RC_ID";
815 case TAG_VENDOR_NAME_RC_ID: return L"VENDOR_NAME_RC_ID";
816 case TAG_SUMMARY_MSG_RC_ID: return L"SUMMARY_MSG_RC_ID";
817 case TAG_VISTA_SKU: return L"VISTA_SKU";
818 case TAG_DESCRIPTION_RC_ID: return L"DESCRIPTION_RC_ID";
819 case TAG_PARAMETER1_RC_ID: return L"PARAMETER1_RC_ID";
820 case TAG_CONTEXT_TAGID: return L"CONTEXT_TAGID";
821 case TAG_EXE_WRAPPER: return L"EXE_WRAPPER";
822 case TAG_URL_ID: return L"URL_ID";
823 case TAG_TAGID: return L"TAGID";
824
825 /* TAG_TYPE_QWORD */
826 case TAG_TIME: return L"TIME";
827 case TAG_BIN_FILE_VERSION: return L"BIN_FILE_VERSION";
828 case TAG_BIN_PRODUCT_VERSION: return L"BIN_PRODUCT_VERSION";
829 case TAG_MODTIME: return L"MODTIME";
830 case TAG_FLAG_MASK_KERNEL: return L"FLAG_MASK_KERNEL";
831 case TAG_UPTO_BIN_PRODUCT_VERSION: return L"UPTO_BIN_PRODUCT_VERSION";
832 case TAG_DATA_QWORD: return L"DATA_QWORD";
833 case TAG_FLAG_MASK_USER: return L"FLAG_MASK_USER";
834 case TAG_FLAGS_NTVDM1: return L"FLAGS_NTVDM1";
835 case TAG_FLAGS_NTVDM2: return L"FLAGS_NTVDM2";
836 case TAG_FLAGS_NTVDM3: return L"FLAGS_NTVDM3";
837 case TAG_FLAG_MASK_SHELL: return L"FLAG_MASK_SHELL";
838 case TAG_UPTO_BIN_FILE_VERSION: return L"UPTO_BIN_FILE_VERSION";
839 case TAG_FLAG_MASK_FUSION: return L"FLAG_MASK_FUSION";
840 case TAG_FLAG_PROCESSPARAM: return L"FLAG_PROCESSPARAM";
841 case TAG_FLAG_LUA: return L"FLAG_LUA";
842 case TAG_FLAG_INSTALL: return L"FLAG_INSTALL";
843
844 /* TAG_TYPE_STRINGREF */
845 case TAG_NAME: return L"NAME";
846 case TAG_DESCRIPTION: return L"DESCRIPTION";
847 case TAG_MODULE: return L"MODULE";
848 case TAG_API: return L"API";
849 case TAG_VENDOR: return L"VENDOR";
850 case TAG_APP_NAME: return L"APP_NAME";
851 case TAG_COMMAND_LINE: return L"COMMAND_LINE";
852 case TAG_COMPANY_NAME: return L"COMPANY_NAME";
853 case TAG_DLLFILE: return L"DLLFILE";
854 case TAG_WILDCARD_NAME: return L"WILDCARD_NAME";
855 case TAG_PRODUCT_NAME: return L"PRODUCT_NAME";
856 case TAG_PRODUCT_VERSION: return L"PRODUCT_VERSION";
857 case TAG_FILE_DESCRIPTION: return L"FILE_DESCRIPTION";
858 case TAG_FILE_VERSION: return L"FILE_VERSION";
859 case TAG_ORIGINAL_FILENAME: return L"ORIGINAL_FILENAME";
860 case TAG_INTERNAL_NAME: return L"INTERNAL_NAME";
861 case TAG_LEGAL_COPYRIGHT: return L"LEGAL_COPYRIGHT";
862 case TAG_16BIT_DESCRIPTION: return L"16BIT_DESCRIPTION";
863 case TAG_APPHELP_DETAILS: return L"APPHELP_DETAILS";
864 case TAG_LINK_URL: return L"LINK_URL";
865 case TAG_LINK_TEXT: return L"LINK_TEXT";
866 case TAG_APPHELP_TITLE: return L"APPHELP_TITLE";
867 case TAG_APPHELP_CONTACT: return L"APPHELP_CONTACT";
868 case TAG_SXS_MANIFEST: return L"SXS_MANIFEST";
869 case TAG_DATA_STRING: return L"DATA_STRING";
870 case TAG_MSI_TRANSFORM_FILE: return L"MSI_TRANSFORM_FILE";
871 case TAG_16BIT_MODULE_NAME: return L"16BIT_MODULE_NAME";
872 case TAG_LAYER_DISPLAYNAME: return L"LAYER_DISPLAYNAME";
873 case TAG_COMPILER_VERSION: return L"COMPILER_VERSION";
874 case TAG_ACTION_TYPE: return L"ACTION_TYPE";
875 case TAG_EXPORT_NAME: return L"EXPORT_NAME";
876 case TAG_URL: return L"URL";
877
878 /* TAG_TYPE_LIST */
879 case TAG_DATABASE: return L"DATABASE";
880 case TAG_LIBRARY: return L"LIBRARY";
881 case TAG_INEXCLUD: return L"INEXCLUDE";
882 case TAG_SHIM: return L"SHIM";
883 case TAG_PATCH: return L"PATCH";
884 case TAG_APP: return L"APP";
885 case TAG_EXE: return L"EXE";
886 case TAG_MATCHING_FILE: return L"MATCHING_FILE";
887 case TAG_SHIM_REF: return L"SHIM_REF";
888 case TAG_PATCH_REF: return L"PATCH_REF";
889 case TAG_LAYER: return L"LAYER";
890 case TAG_FILE: return L"FILE";
891 case TAG_APPHELP: return L"APPHELP";
892 case TAG_LINK: return L"LINK";
893 case TAG_DATA: return L"DATA";
894 case TAG_MSI_TRANSFORM: return L"MSI_TRANSFORM";
895 case TAG_MSI_TRANSFORM_REF: return L"MSI_TRANSFORM_REF";
896 case TAG_MSI_PACKAGE: return L"MSI_PACKAGE";
897 case TAG_FLAG: return L"FLAG";
898 case TAG_MSI_CUSTOM_ACTION: return L"MSI_CUSTOM_ACTION";
899 case TAG_FLAG_REF: return L"FLAG_REF";
900 case TAG_ACTION: return L"ACTION";
901 case TAG_LOOKUP: return L"LOOKUP";
902 case TAG_CONTEXT: return L"CONTEXT";
903 case TAG_CONTEXT_REF: return L"CONTEXT_REF";
904 case TAG_SPC: return L"SPC";
905 case TAG_STRINGTABLE: return L"STRINGTABLE";
906 case TAG_INDEXES: return L"INDEXES";
907 case TAG_INDEX: return L"INDEX";
908
909 /* TAG_TYPE_STRING */
910 case TAG_STRINGTABLE_ITEM: return L"STRINGTABLE_ITEM";
911
912 /* TAG_TYPE_BINARY */
913 case TAG_PATCH_BITS: return L"PATCH_BITS";
914 case TAG_FILE_BITS: return L"FILE_BITS";
915 case TAG_EXE_ID: return L"EXE_ID";
916 case TAG_DATA_BITS: return L"DATA_BITS";
917 case TAG_MSI_PACKAGE_ID: return L"MSI_PACKAGE_ID";
918 case TAG_DATABASE_ID: return L"DATABASE_ID";
919 case TAG_CONTEXT_PLATFORM_ID: return L"CONTEXT_PLATFORM_ID";
920 case TAG_CONTEXT_BRANCH_ID: return L"CONTEXT_BRANCH_ID";
921 case TAG_FIX_ID: return L"FIX_ID";
922 case TAG_APP_ID: return L"APP_ID";
923 case TAG_INDEX_BITS: return L"INDEX_BITS";
924
925 break;
926 }
927 return L"InvalidTag";
928 }