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