38ff2dab35b6203fb2b25a4d0c0f4eb3e2f2dfe6
[reactos.git] / reactos / dll / appcompat / apphelp / sdbapi.c
1 /*
2 * Copyright 2011 André Hentschel
3 * Copyright 2013 Mislav Blažević
4 * Copyright 2015,2016 Mark Jansen
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #define WIN32_NO_STATUS
22 #include "windows.h"
23 #include "ntndk.h"
24 #include "strsafe.h"
25 #include "apphelp.h"
26 #include "sdbstringtable.h"
27
28 #include "wine/unicode.h"
29
30
31 static const GUID GUID_DATABASE_MSI = {0xd8ff6d16,0x6a3a,0x468a, {0x8b,0x44,0x01,0x71,0x4d,0xdc,0x49,0xea}};
32 static const GUID GUID_DATABASE_SHIM = {0x11111111,0x1111,0x1111, {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11}};
33 static const GUID GUID_DATABASE_DRIVERS = {0xf9ab2228,0x3312,0x4a73, {0xb6,0xf9,0x93,0x6d,0x70,0xe1,0x12,0xef}};
34
35 static HANDLE SdbpHeap(void);
36
37 #if SDBAPI_DEBUG_ALLOC
38
39 typedef struct SHIM_ALLOC_ENTRY
40 {
41 PVOID Address;
42 SIZE_T Size;
43 int Line;
44 const char* File;
45 PVOID Next;
46 PVOID Prev;
47 } SHIM_ALLOC_ENTRY, *PSHIM_ALLOC_ENTRY;
48
49
50 static RTL_AVL_TABLE g_SdbpAllocationTable;
51
52
53 static RTL_GENERIC_COMPARE_RESULTS
54 NTAPI ShimAllocCompareRoutine(_In_ PRTL_AVL_TABLE Table, _In_ PVOID FirstStruct, _In_ PVOID SecondStruct)
55 {
56 PVOID First = ((PSHIM_ALLOC_ENTRY)FirstStruct)->Address;
57 PVOID Second = ((PSHIM_ALLOC_ENTRY)SecondStruct)->Address;
58
59 if (First < Second)
60 return GenericLessThan;
61 else if (First == Second)
62 return GenericEqual;
63 return GenericGreaterThan;
64 }
65
66 static PVOID NTAPI ShimAllocAllocateRoutine(_In_ PRTL_AVL_TABLE Table, _In_ CLONG ByteSize)
67 {
68 return HeapAlloc(SdbpHeap(), HEAP_ZERO_MEMORY, ByteSize);
69 }
70
71 static VOID NTAPI ShimAllocFreeRoutine(_In_ PRTL_AVL_TABLE Table, _In_ PVOID Buffer)
72 {
73 HeapFree(SdbpHeap(), 0, Buffer);
74 }
75
76 static void SdbpInsertAllocation(PVOID address, SIZE_T size, int line, const char* file)
77 {
78 SHIM_ALLOC_ENTRY Entry = {0};
79
80 Entry.Address = address;
81 Entry.Size = size;
82 Entry.Line = line;
83 Entry.File = file;
84 RtlInsertElementGenericTableAvl(&g_SdbpAllocationTable, &Entry, sizeof(Entry), NULL);
85 }
86
87 static void SdbpUpdateAllocation(PVOID address, PVOID newaddress, SIZE_T size, int line, const char* file)
88 {
89 SHIM_ALLOC_ENTRY Lookup = {0};
90 PSHIM_ALLOC_ENTRY Entry;
91 Lookup.Address = address;
92 Entry = RtlLookupElementGenericTableAvl(&g_SdbpAllocationTable, &Lookup);
93
94 if (address == newaddress)
95 {
96 Entry->Size = size;
97 }
98 else
99 {
100 Lookup.Address = newaddress;
101 Lookup.Size = size;
102 Lookup.Line = line;
103 Lookup.File = file;
104 Lookup.Prev = address;
105 RtlInsertElementGenericTableAvl(&g_SdbpAllocationTable, &Lookup, sizeof(Lookup), NULL);
106 Entry->Next = newaddress;
107 }
108 }
109
110 static void SdbpRemoveAllocation(PVOID address, int line, const char* file)
111 {
112 char buf[512];
113 SHIM_ALLOC_ENTRY Lookup = {0};
114 PSHIM_ALLOC_ENTRY Entry;
115
116 sprintf(buf, "\r\n===============\r\n%s(%d): SdbpFree called, tracing alloc:\r\n", file, line);
117 OutputDebugStringA(buf);
118
119 Lookup.Address = address;
120 while (Lookup.Address)
121 {
122 Entry = RtlLookupElementGenericTableAvl(&g_SdbpAllocationTable, &Lookup);
123 if (Entry)
124 {
125 Lookup = *Entry;
126 RtlDeleteElementGenericTableAvl(&g_SdbpAllocationTable, Entry);
127
128 sprintf(buf, " > %s(%d): %s%sAlloc( %d ) ==> %p\r\n", Lookup.File, Lookup.Line,
129 Lookup.Next ? "Invalidated " : "", Lookup.Prev ? "Re" : "", Lookup.Size, Lookup.Address);
130 OutputDebugStringA(buf);
131 Lookup.Address = Lookup.Prev;
132 }
133 else
134 {
135 Lookup.Address = NULL;
136 }
137 }
138 sprintf(buf, "\r\n===============\r\n");
139 OutputDebugStringA(buf);
140 }
141
142 #endif
143
144 static HANDLE g_Heap;
145 void SdbpHeapInit(void)
146 {
147 #if SDBAPI_DEBUG_ALLOC
148 RtlInitializeGenericTableAvl(&g_SdbpAllocationTable, ShimAllocCompareRoutine,
149 ShimAllocAllocateRoutine, ShimAllocFreeRoutine, NULL);
150 #endif
151 g_Heap = HeapCreate(0, 0x10000, 0);
152 }
153
154 void SdbpHeapDeinit(void)
155 {
156 #if SDBAPI_DEBUG_ALLOC
157 if (g_SdbpAllocationTable.NumberGenericTableElements != 0)
158 __debugbreak();
159 #endif
160 HeapDestroy(g_Heap);
161 }
162
163 static HANDLE SdbpHeap(void)
164 {
165 return g_Heap;
166 }
167
168 LPVOID SdbpAlloc(SIZE_T size
169 #if SDBAPI_DEBUG_ALLOC
170 , int line, const char* file
171 #endif
172 )
173 {
174 LPVOID mem = HeapAlloc(SdbpHeap(), HEAP_ZERO_MEMORY, size);
175 #if SDBAPI_DEBUG_ALLOC
176 SdbpInsertAllocation(mem, size, line, file);
177 #endif
178 return mem;
179 }
180
181 LPVOID SdbpReAlloc(LPVOID mem, SIZE_T size, SIZE_T oldSize
182 #if SDBAPI_DEBUG_ALLOC
183 , int line, const char* file
184 #endif
185 )
186 {
187 LPVOID newmem = HeapReAlloc(SdbpHeap(), HEAP_ZERO_MEMORY, mem, size);
188 #if SDBAPI_DEBUG_ALLOC
189 SdbpUpdateAllocation(mem, newmem, size, line, file);
190 #endif
191 return newmem;
192 }
193
194 void SdbpFree(LPVOID mem
195 #if SDBAPI_DEBUG_ALLOC
196 , int line, const char* file
197 #endif
198 )
199 {
200 #if SDBAPI_DEBUG_ALLOC
201 SdbpRemoveAllocation(mem, line, file);
202 #endif
203 HeapFree(SdbpHeap(), 0, mem);
204 }
205
206 PDB WINAPI SdbpCreate(LPCWSTR path, PATH_TYPE type, BOOL write)
207 {
208 NTSTATUS Status;
209 IO_STATUS_BLOCK io;
210 OBJECT_ATTRIBUTES attr;
211 UNICODE_STRING str;
212 PDB db;
213
214 if (type == DOS_PATH)
215 {
216 if (!RtlDosPathNameToNtPathName_U(path, &str, NULL, NULL))
217 return NULL;
218 }
219 else
220 RtlInitUnicodeString(&str, path);
221
222 /* SdbAlloc zeroes the memory. */
223 db = (PDB)SdbAlloc(sizeof(DB));
224 if (!db)
225 {
226 SHIM_ERR("Failed to allocate memory for shim database\n");
227 return NULL;
228 }
229
230 InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, NULL, NULL);
231
232 Status = NtCreateFile(&db->file, (write ? FILE_GENERIC_WRITE : FILE_GENERIC_READ )| SYNCHRONIZE,
233 &attr, &io, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
234 write ? FILE_SUPERSEDE : FILE_OPEN, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
235
236 if (type == DOS_PATH)
237 RtlFreeUnicodeString(&str);
238
239 if (!NT_SUCCESS(Status))
240 {
241 SdbCloseDatabase(db);
242 SHIM_ERR("Failed to create shim database file: %lx\n", Status);
243 return NULL;
244 }
245
246 return db;
247 }
248
249 void WINAPI SdbpFlush(PDB db)
250 {
251 IO_STATUS_BLOCK io;
252 NTSTATUS Status = NtWriteFile(db->file, NULL, NULL, NULL, &io,
253 db->data, db->write_iter, NULL, NULL);
254 if( !NT_SUCCESS(Status))
255 SHIM_WARN("failed with 0x%lx\n", Status);
256 }
257
258 DWORD SdbpStrlen(PCWSTR string)
259 {
260 return lstrlenW(string);
261 }
262
263 DWORD SdbpStrsize(PCWSTR string)
264 {
265 return (SdbpStrlen(string) + 1) * sizeof(WCHAR);
266 }
267
268 PWSTR SdbpStrDup(LPCWSTR string)
269 {
270 PWSTR ret = SdbpAlloc(SdbpStrsize(string));
271 lstrcpyW(ret, string);
272 return ret;
273 }
274
275
276 BOOL WINAPI SdbpOpenMemMappedFile(LPCWSTR path, PMEMMAPPED mapping)
277 {
278 NTSTATUS Status;
279 OBJECT_ATTRIBUTES ObjectAttributes;
280 IO_STATUS_BLOCK IoStatusBlock;
281 FILE_STANDARD_INFORMATION FileStandard;
282 UNICODE_STRING FileName;
283
284 RtlZeroMemory(mapping, sizeof(*mapping));
285
286 if(!RtlDosPathNameToNtPathName_U(path, &FileName, NULL, NULL))
287 {
288 RtlFreeUnicodeString(&FileName);
289 return FALSE;
290 }
291
292 InitializeObjectAttributes(&ObjectAttributes, &FileName, OBJ_CASE_INSENSITIVE, NULL, NULL);
293 Status = NtOpenFile(&mapping->file, GENERIC_READ | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT);
294 RtlFreeUnicodeString(&FileName);
295
296 if (!NT_SUCCESS(Status))
297 {
298 SHIM_ERR("Failed to open file %S: 0x%lx\n", path, Status);
299 return FALSE;
300 }
301
302 Status = NtCreateSection(&mapping->section, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ, 0, 0, PAGE_READONLY, SEC_COMMIT, mapping->file);
303 if (!NT_SUCCESS(Status))
304 {
305 /* Special case */
306 if (Status == STATUS_MAPPED_FILE_SIZE_ZERO)
307 {
308 NtClose(mapping->file);
309 mapping->file = mapping->section = NULL;
310 return TRUE;
311 }
312 SHIM_ERR("Failed to create mapping for file: 0x%lx\n", Status);
313 goto err_out;
314 }
315
316 Status = NtQueryInformationFile(mapping->file, &IoStatusBlock, &FileStandard, sizeof(FileStandard), FileStandardInformation);
317 if (!NT_SUCCESS(Status))
318 {
319 SHIM_ERR("Failed to read file info for file: 0x%lx\n", Status);
320 goto err_out;
321 }
322
323 mapping->mapped_size = mapping->size = FileStandard.EndOfFile.LowPart;
324 Status = NtMapViewOfSection(mapping->section, NtCurrentProcess(), (PVOID*)&mapping->view, 0, 0, 0, &mapping->mapped_size, ViewUnmap, 0, PAGE_READONLY);
325 if (!NT_SUCCESS(Status))
326 {
327 SHIM_ERR("Failed to map view of file: 0x%lx\n", Status);
328 goto err_out;
329 }
330
331 return TRUE;
332
333 err_out:
334 if (!mapping->view)
335 {
336 if (mapping->section)
337 NtClose(mapping->section);
338 NtClose(mapping->file);
339 }
340 return FALSE;
341 }
342
343 void WINAPI SdbpCloseMemMappedFile(PMEMMAPPED mapping)
344 {
345 /* Prevent a VAD warning */
346 if (mapping->view)
347 NtUnmapViewOfSection(NtCurrentProcess(), mapping->view);
348 NtClose(mapping->section);
349 NtClose(mapping->file);
350 RtlZeroMemory(mapping, sizeof(*mapping));
351 }
352
353 BOOL WINAPI SdbpCheckTagType(TAG tag, WORD type)
354 {
355 if ((tag & TAG_TYPE_MASK) != type)
356 return FALSE;
357 return TRUE;
358 }
359
360 BOOL WINAPI SdbpCheckTagIDType(PDB db, TAGID tagid, WORD type)
361 {
362 TAG tag = SdbGetTagFromTagID(db, tagid);
363 if (tag == TAG_NULL)
364 return FALSE;
365 return SdbpCheckTagType(tag, type);
366 }
367
368 PDB SdbpOpenDatabase(LPCWSTR path, PATH_TYPE type, PDWORD major, PDWORD minor)
369 {
370 IO_STATUS_BLOCK io;
371 PDB db;
372 NTSTATUS Status;
373 BYTE header[12];
374
375 db = SdbpCreate(path, type, FALSE);
376 if (!db)
377 return NULL;
378
379 db->size = GetFileSize(db->file, NULL);
380 db->data = SdbAlloc(db->size);
381 Status = NtReadFile(db->file, NULL, NULL, NULL, &io, db->data, db->size, NULL, NULL);
382
383 if (!NT_SUCCESS(Status))
384 {
385 SdbCloseDatabase(db);
386 SHIM_ERR("Failed to open shim database file: 0x%lx\n", Status);
387 return NULL;
388 }
389
390 if (!SdbpReadData(db, &header, 0, 12))
391 {
392 SdbCloseDatabase(db);
393 SHIM_ERR("Failed to read shim database header\n");
394 return NULL;
395 }
396
397 if (memcmp(&header[8], "sdbf", 4) != 0)
398 {
399 SdbCloseDatabase(db);
400 SHIM_ERR("Shim database header is invalid\n");
401 return NULL;
402 }
403
404 *major = *(DWORD*)&header[0];
405 *minor = *(DWORD*)&header[4];
406
407 return db;
408 }
409
410
411 /**
412 * Opens specified shim database file.
413 *
414 * @param [in] path Path to the shim database.
415 * @param [in] type Type of path. Either DOS_PATH or NT_PATH.
416 *
417 * @return Success: Handle to the shim database, NULL otherwise.
418 */
419 PDB WINAPI SdbOpenDatabase(LPCWSTR path, PATH_TYPE type)
420 {
421 PDB db;
422 DWORD major, minor;
423
424 db = SdbpOpenDatabase(path, type, &major, &minor);
425 if (!db)
426 return NULL;
427
428 if (major != 2 && major != 3)
429 {
430 SdbCloseDatabase(db);
431 SHIM_ERR("Invalid shim database version\n");
432 return NULL;
433 }
434
435 db->stringtable = SdbFindFirstTag(db, TAGID_ROOT, TAG_STRINGTABLE);
436 if(!SdbGetDatabaseID(db, &db->database_id))
437 {
438 SHIM_INFO("Failed to get the database id\n");
439 }
440 return db;
441 }
442
443 /**
444 * Closes specified database and frees its memory.
445 *
446 * @param [in] db Handle to the shim database.
447 */
448 void WINAPI SdbCloseDatabase(PDB db)
449 {
450 if (!db)
451 return;
452
453 if (db->file)
454 NtClose(db->file);
455 if (db->string_buffer)
456 SdbCloseDatabase(db->string_buffer);
457 if (db->string_lookup)
458 SdbpTableDestroy(&db->string_lookup);
459 SdbFree(db->data);
460 SdbFree(db);
461 }
462
463 /**
464 * Parses a string to retrieve a GUID.
465 *
466 * @param [in] GuidString The string to parse.
467 * @param [out] Guid The resulting GUID.
468 *
469 * @return TRUE if it succeeds, FALSE if it fails.
470 */
471 BOOL WINAPI SdbGUIDFromString(PCWSTR GuidString, GUID *Guid)
472 {
473 UNICODE_STRING GuidString_u;
474 RtlInitUnicodeString(&GuidString_u, GuidString);
475 return NT_SUCCESS(RtlGUIDFromString(&GuidString_u, Guid));
476 }
477
478 /**
479 * Converts a GUID to a string.
480 *
481 * @param [in] Guid The GUID to convert.
482 * @param [out] GuidString The resulting string representation of Guid.
483 * @param [in] Length The length of GuidString.
484 *
485 * @return TRUE if it succeeds, FALSE if it fails.
486 */
487 BOOL WINAPI SdbGUIDToString(CONST GUID *Guid, PWSTR GuidString, SIZE_T Length)
488 {
489 UNICODE_STRING GuidString_u;
490 if(NT_SUCCESS(RtlStringFromGUID(Guid, &GuidString_u)))
491 {
492 HRESULT hr = StringCchCopyNW(GuidString, Length, GuidString_u.Buffer, GuidString_u.Length / 2);
493 RtlFreeUnicodeString(&GuidString_u);
494 return SUCCEEDED(hr);
495 }
496 return FALSE;
497 }
498
499 /**
500 * Checks if the specified GUID is a NULL GUID
501 *
502 * @param [in] Guid The GUID to check.
503 *
504 * @return TRUE if it is a NULL GUID.
505 */
506 BOOL WINAPI SdbIsNullGUID(CONST GUID *Guid)
507 {
508 static GUID NullGuid = { 0 };
509 return !Guid || IsEqualGUID(&NullGuid, Guid);
510 }
511
512 /**
513 * Get the GUID from one of the standard databases.
514 *
515 * @param [in] Flags The ID to retrieve the guid from. (See SDB_DATABASE_MAIN_[xxx])
516 * @param [out] Guid The resulting GUID.
517 *
518 * @return TRUE if a known database ID.
519 */
520 BOOL WINAPI SdbGetStandardDatabaseGUID(DWORD Flags, GUID* Guid)
521 {
522 const GUID* copy_from = NULL;
523 switch(Flags & HID_DATABASE_TYPE_MASK)
524 {
525 case SDB_DATABASE_MAIN_MSI:
526 copy_from = &GUID_DATABASE_MSI;
527 break;
528 case SDB_DATABASE_MAIN_SHIM:
529 copy_from = &GUID_DATABASE_SHIM;
530 break;
531 case SDB_DATABASE_MAIN_DRIVERS:
532 copy_from = &GUID_DATABASE_DRIVERS;
533 break;
534 default:
535 SHIM_ERR("Cannot obtain database guid for databases other than main\n");
536 return FALSE;
537 }
538 if(Guid)
539 {
540 memcpy(Guid, copy_from, sizeof(GUID));
541 }
542 return TRUE;
543 }
544
545 /**
546 * Read the database version from the specified database.
547 *
548 * @param [in] database The database.
549 * @param [out] VersionHi The first part of the version number.
550 * @param [out] VersionLo The second part of the version number.
551 *
552 * @return TRUE if it succeeds or fails, FALSE if ???
553 */
554 BOOL WINAPI SdbGetDatabaseVersion(LPCWSTR database, PDWORD VersionHi, PDWORD VersionLo)
555 {
556 PDB db;
557
558 db = SdbpOpenDatabase(database, DOS_PATH, VersionHi, VersionLo);
559 if (db)
560 SdbCloseDatabase(db);
561
562 return TRUE;
563 }
564
565
566 /**
567 * Find the first named child tag.
568 *
569 * @param [in] database The database.
570 * @param [in] root The tag to start at
571 * @param [in] find The tag type to find
572 * @param [in] nametag The child of 'find' that contains the name
573 * @param [in] find_name The name to find
574 *
575 * @return The found tag, or TAGID_NULL on failure
576 */
577 TAGID WINAPI SdbFindFirstNamedTag(PDB db, TAGID root, TAGID find, TAGID nametag, LPCWSTR find_name)
578 {
579 TAGID iter;
580
581 iter = SdbFindFirstTag(db, root, find);
582
583 while (iter != TAGID_NULL)
584 {
585 TAGID tmp = SdbFindFirstTag(db, iter, nametag);
586 if (tmp != TAGID_NULL)
587 {
588 LPCWSTR name = SdbGetStringTagPtr(db, tmp);
589 if (name && !lstrcmpiW(name, find_name))
590 return iter;
591 }
592 iter = SdbFindNextTag(db, root, iter);
593 }
594 return TAGID_NULL;
595 }
596
597
598 /**
599 * Find a named layer in a multi-db.
600 *
601 * @param [in] hsdb The multi-database.
602 * @param [in] layerName The named tag to find.
603 *
604 * @return The layer, or TAGREF_NULL on failure
605 */
606 TAGREF WINAPI SdbGetLayerTagRef(HSDB hsdb, LPCWSTR layerName)
607 {
608 PDB db = hsdb->db;
609
610 TAGID database = SdbFindFirstTag(db, TAGID_ROOT, TAG_DATABASE);
611 if (database != TAGID_NULL)
612 {
613 TAGID layer = SdbFindFirstNamedTag(db, database, TAG_LAYER, TAG_NAME, layerName);
614 if (layer != TAGID_NULL)
615 {
616 TAGREF tr;
617 if (SdbTagIDToTagRef(hsdb, db, layer, &tr))
618 {
619 return tr;
620 }
621 }
622 }
623 return TAGREF_NULL;
624 }
625
626
627 /**
628 * Converts the specified string to an index key.
629 *
630 * @param [in] str The string which will be converted.
631 *
632 * @return The resulting index key
633 *
634 * @todo: Fix this for unicode strings.
635 */
636 LONGLONG WINAPI SdbMakeIndexKeyFromString(LPCWSTR str)
637 {
638 LONGLONG result = 0;
639 int shift = 56;
640
641 while (*str && shift >= 0)
642 {
643 WCHAR c = toupper(*(str++));
644
645 if (c & 0xff)
646 {
647 result |= (((LONGLONG)(c & 0xff)) << shift);
648 shift -= 8;
649 }
650
651 if (shift < 0)
652 break;
653
654 c >>= 8;
655
656 if (c & 0xff)
657 {
658 result |= (((LONGLONG)(c & 0xff)) << shift);
659 shift -= 8;
660 }
661 }
662
663 return result;
664 }
665
666
667 /**
668 * Converts specified tag into a string.
669 *
670 * @param [in] tag The tag which will be converted to a string.
671 *
672 * @return Success: Pointer to the string matching specified tag, or L"InvalidTag" on failure.
673 *
674 * @todo: Convert this into a lookup table, this is wasting alot of space.
675 */
676 LPCWSTR WINAPI SdbTagToString(TAG tag)
677 {
678 /* lookup tables for tags in range 0x1 -> 0xFF | TYPE */
679 static const WCHAR table[9][0x32][25] = {
680 { /* TAG_TYPE_NULL */
681 {'I','N','C','L','U','D','E',0},
682 {'G','E','N','E','R','A','L',0},
683 {'M','A','T','C','H','_','L','O','G','I','C','_','N','O','T',0},
684 {'A','P','P','L','Y','_','A','L','L','_','S','H','I','M','S',0},
685 {'U','S','E','_','S','E','R','V','I','C','E','_','P','A','C','K','_','F','I','L','E','S',0},
686 {'M','I','T','I','G','A','T','I','O','N','_','O','S',0},
687 {'B','L','O','C','K','_','U','P','G','R','A','D','E',0},
688 {'I','N','C','L','U','D','E','E','X','C','L','U','D','E','D','L','L',0},
689 {'R','A','C','_','E','V','E','N','T','_','O','F','F',0},
690 {'T','E','L','E','M','E','T','R','Y','_','O','F','F',0},
691 {'S','H','I','M','_','E','N','G','I','N','E','_','O','F','F',0},
692 {'L','A','Y','E','R','_','P','R','O','P','A','G','A','T','I','O','N','_','O','F','F',0},
693 {'R','E','I','N','S','T','A','L','L','_','U','P','G','R','A','D','E',0}
694 },
695 { /* TAG_TYPE_BYTE */
696 {'I','n','v','a','l','i','d','T','a','g',0}
697 },
698 { /* TAG_TYPE_WORD */
699 {'M','A','T','C','H','_','M','O','D','E',0}
700 },
701 { /* TAG_TYPE_DWORD */
702 {'S','I','Z','E',0},
703 {'O','F','F','S','E','T',0},
704 {'C','H','E','C','K','S','U','M',0},
705 {'S','H','I','M','_','T','A','G','I','D',0},
706 {'P','A','T','C','H','_','T','A','G','I','D',0},
707 {'M','O','D','U','L','E','_','T','Y','P','E',0},
708 {'V','E','R','D','A','T','E','H','I',0},
709 {'V','E','R','D','A','T','E','L','O',0},
710 {'V','E','R','F','I','L','E','O','S',0},
711 {'V','E','R','F','I','L','E','T','Y','P','E',0},
712 {'P','E','_','C','H','E','C','K','S','U','M',0},
713 {'P','R','E','V','O','S','M','A','J','O','R','V','E','R',0},
714 {'P','R','E','V','O','S','M','I','N','O','R','V','E','R',0},
715 {'P','R','E','V','O','S','P','L','A','T','F','O','R','M','I','D',0},
716 {'P','R','E','V','O','S','B','U','I','L','D','N','O',0},
717 {'P','R','O','B','L','E','M','S','E','V','E','R','I','T','Y',0},
718 {'L','A','N','G','I','D',0},
719 {'V','E','R','_','L','A','N','G','U','A','G','E',0},
720 {'I','n','v','a','l','i','d','T','a','g',0},
721 {'E','N','G','I','N','E',0},
722 {'H','T','M','L','H','E','L','P','I','D',0},
723 {'I','N','D','E','X','_','F','L','A','G','S',0},
724 {'F','L','A','G','S',0},
725 {'D','A','T','A','_','V','A','L','U','E','T','Y','P','E',0},
726 {'D','A','T','A','_','D','W','O','R','D',0},
727 {'L','A','Y','E','R','_','T','A','G','I','D',0},
728 {'M','S','I','_','T','R','A','N','S','F','O','R','M','_','T','A','G','I','D',0},
729 {'L','I','N','K','E','R','_','V','E','R','S','I','O','N',0},
730 {'L','I','N','K','_','D','A','T','E',0},
731 {'U','P','T','O','_','L','I','N','K','_','D','A','T','E',0},
732 {'O','S','_','S','E','R','V','I','C','E','_','P','A','C','K',0},
733 {'F','L','A','G','_','T','A','G','I','D',0},
734 {'R','U','N','T','I','M','E','_','P','L','A','T','F','O','R','M',0},
735 {'O','S','_','S','K','U',0},
736 {'O','S','_','P','L','A','T','F','O','R','M',0},
737 {'A','P','P','_','N','A','M','E','_','R','C','_','I','D',0},
738 {'V','E','N','D','O','R','_','N','A','M','E','_','R','C','_','I','D',0},
739 {'S','U','M','M','A','R','Y','_','M','S','G','_','R','C','_','I','D',0},
740 {'V','I','S','T','A','_','S','K','U',0},
741 {'D','E','S','C','R','I','P','T','I','O','N','_','R','C','_','I','D',0},
742 {'P','A','R','A','M','E','T','E','R','1','_','R','C','_','I','D',0},
743 {'I','n','v','a','l','i','d','T','a','g',0},
744 {'I','n','v','a','l','i','d','T','a','g',0},
745 {'I','n','v','a','l','i','d','T','a','g',0},
746 {'I','n','v','a','l','i','d','T','a','g',0},
747 {'I','n','v','a','l','i','d','T','a','g',0},
748 {'I','n','v','a','l','i','d','T','a','g',0},
749 {'C','O','N','T','E','X','T','_','T','A','G','I','D',0},
750 {'E','X','E','_','W','R','A','P','P','E','R',0},
751 {'U','R','L','_','I','D',0}
752 },
753 { /* TAG_TYPE_QWORD */
754 {'T','I','M','E',0},
755 {'B','I','N','_','F','I','L','E','_','V','E','R','S','I','O','N',0},
756 {'B','I','N','_','P','R','O','D','U','C','T','_','V','E','R','S','I','O','N',0},
757 {'M','O','D','T','I','M','E',0},
758 {'F','L','A','G','_','M','A','S','K','_','K','E','R','N','E','L',0},
759 {'U','P','T','O','_','B','I','N','_','P','R','O','D','U','C','T','_','V','E','R','S','I','O','N',0},
760 {'D','A','T','A','_','Q','W','O','R','D',0},
761 {'F','L','A','G','_','M','A','S','K','_','U','S','E','R',0},
762 {'F','L','A','G','S','_','N','T','V','D','M','1',0},
763 {'F','L','A','G','S','_','N','T','V','D','M','2',0},
764 {'F','L','A','G','S','_','N','T','V','D','M','3',0},
765 {'F','L','A','G','_','M','A','S','K','_','S','H','E','L','L',0},
766 {'U','P','T','O','_','B','I','N','_','F','I','L','E','_','V','E','R','S','I','O','N',0},
767 {'F','L','A','G','_','M','A','S','K','_','F','U','S','I','O','N',0},
768 {'F','L','A','G','_','P','R','O','C','E','S','S','P','A','R','A','M',0},
769 {'F','L','A','G','_','L','U','A',0},
770 {'F','L','A','G','_','I','N','S','T','A','L','L',0}
771 },
772 { /* TAG_TYPE_STRINGREF */
773 {'N','A','M','E',0},
774 {'D','E','S','C','R','I','P','T','I','O','N',0},
775 {'M','O','D','U','L','E',0},
776 {'A','P','I',0},
777 {'V','E','N','D','O','R',0},
778 {'A','P','P','_','N','A','M','E',0},
779 {'I','n','v','a','l','i','d','T','a','g',0},
780 {'C','O','M','M','A','N','D','_','L','I','N','E',0},
781 {'C','O','M','P','A','N','Y','_','N','A','M','E',0},
782 {'D','L','L','F','I','L','E',0},
783 {'W','I','L','D','C','A','R','D','_','N','A','M','E',0},
784 {'I','n','v','a','l','i','d','T','a','g',0},
785 {'I','n','v','a','l','i','d','T','a','g',0},
786 {'I','n','v','a','l','i','d','T','a','g',0},
787 {'I','n','v','a','l','i','d','T','a','g',0},
788 {'P','R','O','D','U','C','T','_','N','A','M','E',0},
789 {'P','R','O','D','U','C','T','_','V','E','R','S','I','O','N',0},
790 {'F','I','L','E','_','D','E','S','C','R','I','P','T','I','O','N',0},
791 {'F','I','L','E','_','V','E','R','S','I','O','N',0},
792 {'O','R','I','G','I','N','A','L','_','F','I','L','E','N','A','M','E',0},
793 {'I','N','T','E','R','N','A','L','_','N','A','M','E',0},
794 {'L','E','G','A','L','_','C','O','P','Y','R','I','G','H','T',0},
795 {'1','6','B','I','T','_','D','E','S','C','R','I','P','T','I','O','N',0},
796 {'A','P','P','H','E','L','P','_','D','E','T','A','I','L','S',0},
797 {'L','I','N','K','_','U','R','L',0},
798 {'L','I','N','K','_','T','E','X','T',0},
799 {'A','P','P','H','E','L','P','_','T','I','T','L','E',0},
800 {'A','P','P','H','E','L','P','_','C','O','N','T','A','C','T',0},
801 {'S','X','S','_','M','A','N','I','F','E','S','T',0},
802 {'D','A','T','A','_','S','T','R','I','N','G',0},
803 {'M','S','I','_','T','R','A','N','S','F','O','R','M','_','F','I','L','E',0},
804 {'1','6','B','I','T','_','M','O','D','U','L','E','_','N','A','M','E',0},
805 {'L','A','Y','E','R','_','D','I','S','P','L','A','Y','N','A','M','E',0},
806 {'C','O','M','P','I','L','E','R','_','V','E','R','S','I','O','N',0},
807 {'A','C','T','I','O','N','_','T','Y','P','E',0},
808 {'E','X','P','O','R','T','_','N','A','M','E',0},
809 {'U','R','L',0}
810 },
811 { /* TAG_TYPE_LIST */
812 {'D','A','T','A','B','A','S','E',0},
813 {'L','I','B','R','A','R','Y',0},
814 {'I','N','E','X','C','L','U','D','E',0},
815 {'S','H','I','M',0},
816 {'P','A','T','C','H',0},
817 {'A','P','P',0},
818 {'E','X','E',0},
819 {'M','A','T','C','H','I','N','G','_','F','I','L','E',0},
820 {'S','H','I','M','_','R','E','F',0},
821 {'P','A','T','C','H','_','R','E','F',0},
822 {'L','A','Y','E','R',0},
823 {'F','I','L','E',0},
824 {'A','P','P','H','E','L','P',0},
825 {'L','I','N','K',0},
826 {'D','A','T','A',0},
827 {'M','S','I','_','T','R','A','N','S','F','O','R','M',0},
828 {'M','S','I','_','T','R','A','N','S','F','O','R','M','_','R','E','F',0},
829 {'M','S','I','_','P','A','C','K','A','G','E',0},
830 {'F','L','A','G',0},
831 {'M','S','I','_','C','U','S','T','O','M','_','A','C','T','I','O','N',0},
832 {'F','L','A','G','_','R','E','F',0},
833 {'A','C','T','I','O','N',0},
834 {'L','O','O','K','U','P',0},
835 {'C','O','N','T','E','X','T',0},
836 {'C','O','N','T','E','X','T','_','R','E','F',0},
837 {'I','n','v','a','l','i','d','T','a','g',0},
838 {'I','n','v','a','l','i','d','T','a','g',0},
839 {'I','n','v','a','l','i','d','T','a','g',0},
840 {'I','n','v','a','l','i','d','T','a','g',0},
841 {'I','n','v','a','l','i','d','T','a','g',0},
842 {'I','n','v','a','l','i','d','T','a','g',0},
843 {'S','P','C',0}
844 },
845 { /* TAG_TYPE_STRING */
846 {'I','n','v','a','l','i','d','T','a','g',0}
847 },
848 { /* TAG_TYPE_BINARY */
849 {'I','n','v','a','l','i','d','T','a','g',0},
850 {'P','A','T','C','H','_','B','I','T','S',0},
851 {'F','I','L','E','_','B','I','T','S',0},
852 {'E','X','E','_','I','D',0},
853 {'D','A','T','A','_','B','I','T','S',0},
854 {'M','S','I','_','P','A','C','K','A','G','E','_','I','D',0},
855 {'D','A','T','A','B','A','S','E','_','I','D',0},
856 {'C','O','N','T','E','X','T','_','P','L','A','T','F','O','R','M','_','I','D',0},
857 {'C','O','N','T','E','X','T','_','B','R','A','N','C','H','_','I','D',0},
858 {'I','n','v','a','l','i','d','T','a','g',0},
859 {'I','n','v','a','l','i','d','T','a','g',0},
860 {'I','n','v','a','l','i','d','T','a','g',0},
861 {'I','n','v','a','l','i','d','T','a','g',0},
862 {'I','n','v','a','l','i','d','T','a','g',0},
863 {'I','n','v','a','l','i','d','T','a','g',0},
864 {'F','I','X','_','I','D',0},
865 {'A','P','P','_','I','D',0}
866 }
867 };
868
869 /* sizes of tables in above array (# strings per type) */
870 static const WORD limits[9] = {
871 /* switch off TYPE_* nibble of last tag for each type */
872 TAG_REINSTALL_UPGRADE & 0xFF,
873 1,
874 TAG_MATCH_MODE & 0xFF,
875 TAG_URL_ID & 0xFF,
876 TAG_FLAG_INSTALL & 0xFF,
877 TAG_URL & 0xFF,
878 TAG_SPC & 0xFF,
879 1,
880 TAG_APP_ID & 0xFF
881 };
882
883 /* lookup tables for tags in range 0x800 + (0x1 -> 0xFF) | TYPE */
884 static const WCHAR table2[9][3][17] = {
885 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_NULL */
886 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_BYTE */
887 {
888 {'T','A','G',0}, /* TAG_TYPE_WORD */
889 {'I','N','D','E','X','_','T','A','G',0},
890 {'I','N','D','E','X','_','K','E','Y',0}
891 },
892 { {'T','A','G','I','D',0} }, /* TAG_TYPE_DWORD */
893 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_QWORD */
894 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_STRINGREF */
895 {
896 {'S','T','R','I','N','G','T','A','B','L','E',0}, /* TAG_TYPE_LIST */
897 {'I','N','D','E','X','E','S',0},
898 {'I','N','D','E','X',0}
899 },
900 { {'S','T','R','I','N','G','T','A','B','L','E','_','I','T','E','M',0}, }, /* TAG_TYPE_STRING */
901 { {'I','N','D','E','X','_','B','I','T','S',0} } /* TAG_TYPE_BINARY */
902 };
903
904 /* sizes of tables in above array, hardcoded for simplicity */
905 static const WORD limits2[9] = { 0, 0, 3, 1, 0, 0, 3, 1, 1 };
906
907 static const WCHAR null[] = {'N','U','L','L',0};
908 static const WCHAR invalid[] = {'I','n','v','a','l','i','d','T','a','g',0};
909
910 BOOL switch_table; /* should we use table2 and limits2? */
911 WORD index, type_index;
912
913 /* special case: null tag */
914 if (tag == TAG_NULL)
915 return null;
916
917 /* tags with only type mask or no type mask are invalid */
918 if ((tag & ~TAG_TYPE_MASK) == 0 || (tag & TAG_TYPE_MASK) == 0)
919 return invalid;
920
921 /* some valid tags are in range 0x800 + (0x1 -> 0xF) | TYPE */
922 if ((tag & 0xF00) == 0x800)
923 switch_table = TRUE;
924 else if ((tag & 0xF00) == 0)
925 switch_table = FALSE;
926 else return invalid;
927
928 /* index of table in array is type nibble */
929 type_index = (tag >> 12) - 1;
930
931 /* index of string in table is low byte */
932 index = (tag & 0xFF) - 1;
933
934 /* bound check */
935 if (type_index >= 9 || index >= (switch_table ? limits2[type_index] : limits[type_index]))
936 return invalid;
937
938 /* tag is valid */
939 return switch_table ? table2[type_index][index] : table[type_index][index];
940 }