[APPHELP][APPHELP_APITEST] Partially implement SdbMakeIndexKeyFromString
[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)
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 * Retrieves AppPatch directory.
465 *
466 * @param [in] db Handle to the shim database.
467 * @param [out] path Pointer to memory in which path shall be written.
468 * @param [in] size Size of the buffer in characters.
469 */
470 BOOL WINAPI SdbGetAppPatchDir(HSDB db, LPWSTR path, DWORD size)
471 {
472 static WCHAR* default_dir = NULL;
473 static CONST WCHAR szAppPatch[] = {'\\','A','p','p','P','a','t','c','h',0};
474
475 /* In case function fails, path holds empty string */
476 if (size > 0)
477 *path = 0;
478
479 if (!default_dir)
480 {
481 WCHAR* tmp = NULL;
482 UINT len = GetSystemWindowsDirectoryW(NULL, 0) + lstrlenW(szAppPatch);
483 tmp = SdbAlloc((len + 1)* sizeof(WCHAR));
484 if (tmp)
485 {
486 UINT r = GetSystemWindowsDirectoryW(tmp, len+1);
487 if (r && r < len)
488 {
489 if (SUCCEEDED(StringCchCatW(tmp, len+1, szAppPatch)))
490 {
491 if (InterlockedCompareExchangePointer((void**)&default_dir, tmp, NULL) == NULL)
492 tmp = NULL;
493 }
494 }
495 if (tmp)
496 SdbFree(tmp);
497 }
498 if (!default_dir)
499 {
500 SHIM_ERR("Unable to obtain default AppPatch directory\n");
501 return FALSE;
502 }
503 }
504
505 if (!db)
506 {
507 return SUCCEEDED(StringCchCopyW(path, size, default_dir));
508 }
509 else
510 {
511 /* fixme */
512 return FALSE;
513 }
514 }
515
516 /**
517 * Parses a string to retrieve a GUID.
518 *
519 * @param [in] GuidString The string to parse.
520 * @param [out] Guid The resulting GUID.
521 *
522 * @return TRUE if it succeeds, FALSE if it fails.
523 */
524 BOOL WINAPI SdbGUIDFromString(PCWSTR GuidString, GUID *Guid)
525 {
526 UNICODE_STRING GuidString_u;
527 RtlInitUnicodeString(&GuidString_u, GuidString);
528 return NT_SUCCESS(RtlGUIDFromString(&GuidString_u, Guid));
529 }
530
531 /**
532 * Converts a GUID to a string.
533 *
534 * @param [in] Guid The GUID to convert.
535 * @param [out] GuidString The resulting string representation of Guid.
536 * @param [in] Length The length of GuidString.
537 *
538 * @return TRUE if it succeeds, FALSE if it fails.
539 */
540 BOOL WINAPI SdbGUIDToString(CONST GUID *Guid, PWSTR GuidString, SIZE_T Length)
541 {
542 UNICODE_STRING GuidString_u;
543 if(NT_SUCCESS(RtlStringFromGUID(Guid, &GuidString_u)))
544 {
545 HRESULT hr = StringCchCopyNW(GuidString, Length, GuidString_u.Buffer, GuidString_u.Length / 2);
546 RtlFreeUnicodeString(&GuidString_u);
547 return SUCCEEDED(hr);
548 }
549 return FALSE;
550 }
551
552 /**
553 * Checks if the specified GUID is a NULL GUID
554 *
555 * @param [in] Guid The GUID to check.
556 *
557 * @return TRUE if it is a NULL GUID.
558 */
559 BOOL WINAPI SdbIsNullGUID(CONST GUID *Guid)
560 {
561 static GUID NullGuid = { 0 };
562 return !Guid || IsEqualGUID(&NullGuid, Guid);
563 }
564
565 /**
566 * Get the GUID from one of the standard databases.
567 *
568 * @param [in] Flags The ID to retrieve the guid from. (See SDB_DATABASE_MAIN_[xxx])
569 * @param [out] Guid The resulting GUID.
570 *
571 * @return TRUE if a known database ID.
572 */
573 BOOL WINAPI SdbGetStandardDatabaseGUID(DWORD Flags, GUID* Guid)
574 {
575 const GUID* copy_from = NULL;
576 switch(Flags & HID_DATABASE_TYPE_MASK)
577 {
578 case SDB_DATABASE_MAIN_MSI:
579 copy_from = &GUID_DATABASE_MSI;
580 break;
581 case SDB_DATABASE_MAIN_SHIM:
582 copy_from = &GUID_DATABASE_SHIM;
583 break;
584 case SDB_DATABASE_MAIN_DRIVERS:
585 copy_from = &GUID_DATABASE_DRIVERS;
586 break;
587 default:
588 SHIM_ERR("Cannot obtain database guid for databases other than main\n");
589 return FALSE;
590 }
591 if(Guid)
592 {
593 memcpy(Guid, copy_from, sizeof(GUID));
594 }
595 return TRUE;
596 }
597
598 /**
599 * Read the database version from the specified database.
600 *
601 * @param [in] database The database.
602 * @param [out] VersionHi The first part of the version number.
603 * @param [out] VersionLo The second part of the version number.
604 *
605 * @return TRUE if it succeeds or fails, FALSE if ???
606 */
607 BOOL WINAPI SdbGetDatabaseVersion(LPCWSTR database, PDWORD VersionHi, PDWORD VersionLo)
608 {
609 PDB db;
610
611 db = SdbpOpenDatabase(database, DOS_PATH, VersionHi, VersionLo);
612 if (db)
613 SdbCloseDatabase(db);
614
615 return TRUE;
616 }
617
618
619 /**
620 * Converts the specified string to an index key.
621 *
622 * @param [in] str The string which will be converted.
623 *
624 * @return The resulting index key
625 *
626 * @todo: Fix this for unicode strings.
627 */
628 LONGLONG WINAPI SdbMakeIndexKeyFromString(LPCWSTR str)
629 {
630 LONGLONG result = 0;
631 int shift = 56;
632
633 while (*str && shift >= 0)
634 {
635 WCHAR c = toupper(*(str++));
636
637 if (c & 0xff)
638 {
639 result |= (((LONGLONG)(c & 0xff)) << shift);
640 shift -= 8;
641 }
642
643 if (shift < 0)
644 break;
645
646 c >>= 8;
647
648 if (c & 0xff)
649 {
650 result |= (((LONGLONG)(c & 0xff)) << shift);
651 shift -= 8;
652 }
653 }
654
655 return result;
656 }
657
658
659 /**
660 * Converts specified tag into a string.
661 *
662 * @param [in] tag The tag which will be converted to a string.
663 *
664 * @return Success: Pointer to the string matching specified tag, or L"InvalidTag" on failure.
665 *
666 * @todo: Convert this into a lookup table, this is wasting alot of space.
667 */
668 LPCWSTR WINAPI SdbTagToString(TAG tag)
669 {
670 /* lookup tables for tags in range 0x1 -> 0xFF | TYPE */
671 static const WCHAR table[9][0x32][25] = {
672 { /* TAG_TYPE_NULL */
673 {'I','N','C','L','U','D','E',0},
674 {'G','E','N','E','R','A','L',0},
675 {'M','A','T','C','H','_','L','O','G','I','C','_','N','O','T',0},
676 {'A','P','P','L','Y','_','A','L','L','_','S','H','I','M','S',0},
677 {'U','S','E','_','S','E','R','V','I','C','E','_','P','A','C','K','_','F','I','L','E','S',0},
678 {'M','I','T','I','G','A','T','I','O','N','_','O','S',0},
679 {'B','L','O','C','K','_','U','P','G','R','A','D','E',0},
680 {'I','N','C','L','U','D','E','E','X','C','L','U','D','E','D','L','L',0},
681 {'R','A','C','_','E','V','E','N','T','_','O','F','F',0},
682 {'T','E','L','E','M','E','T','R','Y','_','O','F','F',0},
683 {'S','H','I','M','_','E','N','G','I','N','E','_','O','F','F',0},
684 {'L','A','Y','E','R','_','P','R','O','P','A','G','A','T','I','O','N','_','O','F','F',0},
685 {'R','E','I','N','S','T','A','L','L','_','U','P','G','R','A','D','E',0}
686 },
687 { /* TAG_TYPE_BYTE */
688 {'I','n','v','a','l','i','d','T','a','g',0}
689 },
690 { /* TAG_TYPE_WORD */
691 {'M','A','T','C','H','_','M','O','D','E',0}
692 },
693 { /* TAG_TYPE_DWORD */
694 {'S','I','Z','E',0},
695 {'O','F','F','S','E','T',0},
696 {'C','H','E','C','K','S','U','M',0},
697 {'S','H','I','M','_','T','A','G','I','D',0},
698 {'P','A','T','C','H','_','T','A','G','I','D',0},
699 {'M','O','D','U','L','E','_','T','Y','P','E',0},
700 {'V','E','R','D','A','T','E','H','I',0},
701 {'V','E','R','D','A','T','E','L','O',0},
702 {'V','E','R','F','I','L','E','O','S',0},
703 {'V','E','R','F','I','L','E','T','Y','P','E',0},
704 {'P','E','_','C','H','E','C','K','S','U','M',0},
705 {'P','R','E','V','O','S','M','A','J','O','R','V','E','R',0},
706 {'P','R','E','V','O','S','M','I','N','O','R','V','E','R',0},
707 {'P','R','E','V','O','S','P','L','A','T','F','O','R','M','I','D',0},
708 {'P','R','E','V','O','S','B','U','I','L','D','N','O',0},
709 {'P','R','O','B','L','E','M','S','E','V','E','R','I','T','Y',0},
710 {'L','A','N','G','I','D',0},
711 {'V','E','R','_','L','A','N','G','U','A','G','E',0},
712 {'I','n','v','a','l','i','d','T','a','g',0},
713 {'E','N','G','I','N','E',0},
714 {'H','T','M','L','H','E','L','P','I','D',0},
715 {'I','N','D','E','X','_','F','L','A','G','S',0},
716 {'F','L','A','G','S',0},
717 {'D','A','T','A','_','V','A','L','U','E','T','Y','P','E',0},
718 {'D','A','T','A','_','D','W','O','R','D',0},
719 {'L','A','Y','E','R','_','T','A','G','I','D',0},
720 {'M','S','I','_','T','R','A','N','S','F','O','R','M','_','T','A','G','I','D',0},
721 {'L','I','N','K','E','R','_','V','E','R','S','I','O','N',0},
722 {'L','I','N','K','_','D','A','T','E',0},
723 {'U','P','T','O','_','L','I','N','K','_','D','A','T','E',0},
724 {'O','S','_','S','E','R','V','I','C','E','_','P','A','C','K',0},
725 {'F','L','A','G','_','T','A','G','I','D',0},
726 {'R','U','N','T','I','M','E','_','P','L','A','T','F','O','R','M',0},
727 {'O','S','_','S','K','U',0},
728 {'O','S','_','P','L','A','T','F','O','R','M',0},
729 {'A','P','P','_','N','A','M','E','_','R','C','_','I','D',0},
730 {'V','E','N','D','O','R','_','N','A','M','E','_','R','C','_','I','D',0},
731 {'S','U','M','M','A','R','Y','_','M','S','G','_','R','C','_','I','D',0},
732 {'V','I','S','T','A','_','S','K','U',0},
733 {'D','E','S','C','R','I','P','T','I','O','N','_','R','C','_','I','D',0},
734 {'P','A','R','A','M','E','T','E','R','1','_','R','C','_','I','D',0},
735 {'I','n','v','a','l','i','d','T','a','g',0},
736 {'I','n','v','a','l','i','d','T','a','g',0},
737 {'I','n','v','a','l','i','d','T','a','g',0},
738 {'I','n','v','a','l','i','d','T','a','g',0},
739 {'I','n','v','a','l','i','d','T','a','g',0},
740 {'I','n','v','a','l','i','d','T','a','g',0},
741 {'C','O','N','T','E','X','T','_','T','A','G','I','D',0},
742 {'E','X','E','_','W','R','A','P','P','E','R',0},
743 {'U','R','L','_','I','D',0}
744 },
745 { /* TAG_TYPE_QWORD */
746 {'T','I','M','E',0},
747 {'B','I','N','_','F','I','L','E','_','V','E','R','S','I','O','N',0},
748 {'B','I','N','_','P','R','O','D','U','C','T','_','V','E','R','S','I','O','N',0},
749 {'M','O','D','T','I','M','E',0},
750 {'F','L','A','G','_','M','A','S','K','_','K','E','R','N','E','L',0},
751 {'U','P','T','O','_','B','I','N','_','P','R','O','D','U','C','T','_','V','E','R','S','I','O','N',0},
752 {'D','A','T','A','_','Q','W','O','R','D',0},
753 {'F','L','A','G','_','M','A','S','K','_','U','S','E','R',0},
754 {'F','L','A','G','S','_','N','T','V','D','M','1',0},
755 {'F','L','A','G','S','_','N','T','V','D','M','2',0},
756 {'F','L','A','G','S','_','N','T','V','D','M','3',0},
757 {'F','L','A','G','_','M','A','S','K','_','S','H','E','L','L',0},
758 {'U','P','T','O','_','B','I','N','_','F','I','L','E','_','V','E','R','S','I','O','N',0},
759 {'F','L','A','G','_','M','A','S','K','_','F','U','S','I','O','N',0},
760 {'F','L','A','G','_','P','R','O','C','E','S','S','P','A','R','A','M',0},
761 {'F','L','A','G','_','L','U','A',0},
762 {'F','L','A','G','_','I','N','S','T','A','L','L',0}
763 },
764 { /* TAG_TYPE_STRINGREF */
765 {'N','A','M','E',0},
766 {'D','E','S','C','R','I','P','T','I','O','N',0},
767 {'M','O','D','U','L','E',0},
768 {'A','P','I',0},
769 {'V','E','N','D','O','R',0},
770 {'A','P','P','_','N','A','M','E',0},
771 {'I','n','v','a','l','i','d','T','a','g',0},
772 {'C','O','M','M','A','N','D','_','L','I','N','E',0},
773 {'C','O','M','P','A','N','Y','_','N','A','M','E',0},
774 {'D','L','L','F','I','L','E',0},
775 {'W','I','L','D','C','A','R','D','_','N','A','M','E',0},
776 {'I','n','v','a','l','i','d','T','a','g',0},
777 {'I','n','v','a','l','i','d','T','a','g',0},
778 {'I','n','v','a','l','i','d','T','a','g',0},
779 {'I','n','v','a','l','i','d','T','a','g',0},
780 {'P','R','O','D','U','C','T','_','N','A','M','E',0},
781 {'P','R','O','D','U','C','T','_','V','E','R','S','I','O','N',0},
782 {'F','I','L','E','_','D','E','S','C','R','I','P','T','I','O','N',0},
783 {'F','I','L','E','_','V','E','R','S','I','O','N',0},
784 {'O','R','I','G','I','N','A','L','_','F','I','L','E','N','A','M','E',0},
785 {'I','N','T','E','R','N','A','L','_','N','A','M','E',0},
786 {'L','E','G','A','L','_','C','O','P','Y','R','I','G','H','T',0},
787 {'1','6','B','I','T','_','D','E','S','C','R','I','P','T','I','O','N',0},
788 {'A','P','P','H','E','L','P','_','D','E','T','A','I','L','S',0},
789 {'L','I','N','K','_','U','R','L',0},
790 {'L','I','N','K','_','T','E','X','T',0},
791 {'A','P','P','H','E','L','P','_','T','I','T','L','E',0},
792 {'A','P','P','H','E','L','P','_','C','O','N','T','A','C','T',0},
793 {'S','X','S','_','M','A','N','I','F','E','S','T',0},
794 {'D','A','T','A','_','S','T','R','I','N','G',0},
795 {'M','S','I','_','T','R','A','N','S','F','O','R','M','_','F','I','L','E',0},
796 {'1','6','B','I','T','_','M','O','D','U','L','E','_','N','A','M','E',0},
797 {'L','A','Y','E','R','_','D','I','S','P','L','A','Y','N','A','M','E',0},
798 {'C','O','M','P','I','L','E','R','_','V','E','R','S','I','O','N',0},
799 {'A','C','T','I','O','N','_','T','Y','P','E',0},
800 {'E','X','P','O','R','T','_','N','A','M','E',0},
801 {'U','R','L',0}
802 },
803 { /* TAG_TYPE_LIST */
804 {'D','A','T','A','B','A','S','E',0},
805 {'L','I','B','R','A','R','Y',0},
806 {'I','N','E','X','C','L','U','D','E',0},
807 {'S','H','I','M',0},
808 {'P','A','T','C','H',0},
809 {'A','P','P',0},
810 {'E','X','E',0},
811 {'M','A','T','C','H','I','N','G','_','F','I','L','E',0},
812 {'S','H','I','M','_','R','E','F',0},
813 {'P','A','T','C','H','_','R','E','F',0},
814 {'L','A','Y','E','R',0},
815 {'F','I','L','E',0},
816 {'A','P','P','H','E','L','P',0},
817 {'L','I','N','K',0},
818 {'D','A','T','A',0},
819 {'M','S','I','_','T','R','A','N','S','F','O','R','M',0},
820 {'M','S','I','_','T','R','A','N','S','F','O','R','M','_','R','E','F',0},
821 {'M','S','I','_','P','A','C','K','A','G','E',0},
822 {'F','L','A','G',0},
823 {'M','S','I','_','C','U','S','T','O','M','_','A','C','T','I','O','N',0},
824 {'F','L','A','G','_','R','E','F',0},
825 {'A','C','T','I','O','N',0},
826 {'L','O','O','K','U','P',0},
827 {'C','O','N','T','E','X','T',0},
828 {'C','O','N','T','E','X','T','_','R','E','F',0},
829 {'I','n','v','a','l','i','d','T','a','g',0},
830 {'I','n','v','a','l','i','d','T','a','g',0},
831 {'I','n','v','a','l','i','d','T','a','g',0},
832 {'I','n','v','a','l','i','d','T','a','g',0},
833 {'I','n','v','a','l','i','d','T','a','g',0},
834 {'I','n','v','a','l','i','d','T','a','g',0},
835 {'S','P','C',0}
836 },
837 { /* TAG_TYPE_STRING */
838 {'I','n','v','a','l','i','d','T','a','g',0}
839 },
840 { /* TAG_TYPE_BINARY */
841 {'I','n','v','a','l','i','d','T','a','g',0},
842 {'P','A','T','C','H','_','B','I','T','S',0},
843 {'F','I','L','E','_','B','I','T','S',0},
844 {'E','X','E','_','I','D',0},
845 {'D','A','T','A','_','B','I','T','S',0},
846 {'M','S','I','_','P','A','C','K','A','G','E','_','I','D',0},
847 {'D','A','T','A','B','A','S','E','_','I','D',0},
848 {'C','O','N','T','E','X','T','_','P','L','A','T','F','O','R','M','_','I','D',0},
849 {'C','O','N','T','E','X','T','_','B','R','A','N','C','H','_','I','D',0},
850 {'I','n','v','a','l','i','d','T','a','g',0},
851 {'I','n','v','a','l','i','d','T','a','g',0},
852 {'I','n','v','a','l','i','d','T','a','g',0},
853 {'I','n','v','a','l','i','d','T','a','g',0},
854 {'I','n','v','a','l','i','d','T','a','g',0},
855 {'I','n','v','a','l','i','d','T','a','g',0},
856 {'F','I','X','_','I','D',0},
857 {'A','P','P','_','I','D',0}
858 }
859 };
860
861 /* sizes of tables in above array (# strings per type) */
862 static const WORD limits[9] = {
863 /* switch off TYPE_* nibble of last tag for each type */
864 TAG_REINSTALL_UPGRADE & 0xFF,
865 1,
866 TAG_MATCH_MODE & 0xFF,
867 TAG_URL_ID & 0xFF,
868 TAG_FLAG_INSTALL & 0xFF,
869 TAG_URL & 0xFF,
870 TAG_SPC & 0xFF,
871 1,
872 TAG_APP_ID & 0xFF
873 };
874
875 /* lookup tables for tags in range 0x800 + (0x1 -> 0xFF) | TYPE */
876 static const WCHAR table2[9][3][17] = {
877 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_NULL */
878 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_BYTE */
879 {
880 {'T','A','G',0}, /* TAG_TYPE_WORD */
881 {'I','N','D','E','X','_','T','A','G',0},
882 {'I','N','D','E','X','_','K','E','Y',0}
883 },
884 { {'T','A','G','I','D',0} }, /* TAG_TYPE_DWORD */
885 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_QWORD */
886 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_STRINGREF */
887 {
888 {'S','T','R','I','N','G','T','A','B','L','E',0}, /* TAG_TYPE_LIST */
889 {'I','N','D','E','X','E','S',0},
890 {'I','N','D','E','X',0}
891 },
892 { {'S','T','R','I','N','G','T','A','B','L','E','_','I','T','E','M',0}, }, /* TAG_TYPE_STRING */
893 { {'I','N','D','E','X','_','B','I','T','S',0} } /* TAG_TYPE_BINARY */
894 };
895
896 /* sizes of tables in above array, hardcoded for simplicity */
897 static const WORD limits2[9] = { 0, 0, 3, 1, 0, 0, 3, 1, 1 };
898
899 static const WCHAR null[] = {'N','U','L','L',0};
900 static const WCHAR invalid[] = {'I','n','v','a','l','i','d','T','a','g',0};
901
902 BOOL switch_table; /* should we use table2 and limits2? */
903 WORD index, type_index;
904
905 /* special case: null tag */
906 if (tag == TAG_NULL)
907 return null;
908
909 /* tags with only type mask or no type mask are invalid */
910 if ((tag & ~TAG_TYPE_MASK) == 0 || (tag & TAG_TYPE_MASK) == 0)
911 return invalid;
912
913 /* some valid tags are in range 0x800 + (0x1 -> 0xF) | TYPE */
914 if ((tag & 0xF00) == 0x800)
915 switch_table = TRUE;
916 else if ((tag & 0xF00) == 0)
917 switch_table = FALSE;
918 else return invalid;
919
920 /* index of table in array is type nibble */
921 type_index = (tag >> 12) - 1;
922
923 /* index of string in table is low byte */
924 index = (tag & 0xFF) - 1;
925
926 /* bound check */
927 if (type_index >= 9 || index >= (switch_table ? limits2[type_index] : limits[type_index]))
928 return invalid;
929
930 /* tag is valid */
931 return switch_table ? table2[type_index][index] : table[type_index][index];
932 }