[APPHELP][APPHELP_APITEST] Implement most of sdbread and sdbwrite, mainly the work...
[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
27 #include "wine/unicode.h"
28
29
30 static const GUID GUID_DATABASE_MSI = {0xd8ff6d16,0x6a3a,0x468a, {0x8b,0x44,0x01,0x71,0x4d,0xdc,0x49,0xea}};
31 static const GUID GUID_DATABASE_SHIM = {0x11111111,0x1111,0x1111, {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11}};
32 static const GUID GUID_DATABASE_DRIVERS = {0xf9ab2228,0x3312,0x4a73, {0xb6,0xf9,0x93,0x6d,0x70,0xe1,0x12,0xef}};
33
34 static HANDLE SdbpHeap(void);
35
36 #if SDBAPI_DEBUG_ALLOC
37
38 typedef struct SHIM_ALLOC_ENTRY
39 {
40 PVOID Address;
41 SIZE_T Size;
42 int Line;
43 const char* File;
44 PVOID Next;
45 PVOID Prev;
46 } SHIM_ALLOC_ENTRY, *PSHIM_ALLOC_ENTRY;
47
48
49 static RTL_AVL_TABLE g_SdbpAllocationTable;
50
51
52 static RTL_GENERIC_COMPARE_RESULTS
53 NTAPI ShimAllocCompareRoutine(_In_ PRTL_AVL_TABLE Table, _In_ PVOID FirstStruct, _In_ PVOID SecondStruct)
54 {
55 PVOID First = ((PSHIM_ALLOC_ENTRY)FirstStruct)->Address;
56 PVOID Second = ((PSHIM_ALLOC_ENTRY)SecondStruct)->Address;
57
58 if (First < Second)
59 return GenericLessThan;
60 else if (First == Second)
61 return GenericEqual;
62 return GenericGreaterThan;
63 }
64
65 static PVOID NTAPI ShimAllocAllocateRoutine(_In_ PRTL_AVL_TABLE Table, _In_ CLONG ByteSize)
66 {
67 return HeapAlloc(SdbpHeap(), HEAP_ZERO_MEMORY, ByteSize);
68 }
69
70 static VOID NTAPI ShimAllocFreeRoutine(_In_ PRTL_AVL_TABLE Table, _In_ PVOID Buffer)
71 {
72 HeapFree(SdbpHeap(), 0, Buffer);
73 }
74
75 static void SdbpInsertAllocation(PVOID address, SIZE_T size, int line, const char* file)
76 {
77 SHIM_ALLOC_ENTRY Entry = {0};
78
79 Entry.Address = address;
80 Entry.Size = size;
81 Entry.Line = line;
82 Entry.File = file;
83 RtlInsertElementGenericTableAvl(&g_SdbpAllocationTable, &Entry, sizeof(Entry), NULL);
84 }
85
86 static void SdbpUpdateAllocation(PVOID address, PVOID newaddress, SIZE_T size, int line, const char* file)
87 {
88 SHIM_ALLOC_ENTRY Lookup = {0};
89 PSHIM_ALLOC_ENTRY Entry;
90 Lookup.Address = address;
91 Entry = RtlLookupElementGenericTableAvl(&g_SdbpAllocationTable, &Lookup);
92
93 if (address == newaddress)
94 {
95 Entry->Size = size;
96 }
97 else
98 {
99 Lookup.Address = newaddress;
100 Lookup.Size = size;
101 Lookup.Line = line;
102 Lookup.File = file;
103 Lookup.Prev = address;
104 RtlInsertElementGenericTableAvl(&g_SdbpAllocationTable, &Lookup, sizeof(Lookup), NULL);
105 Entry->Next = newaddress;
106 }
107 }
108
109 static void SdbpRemoveAllocation(PVOID address, int line, const char* file)
110 {
111 char buf[512];
112 SHIM_ALLOC_ENTRY Lookup = {0};
113 PSHIM_ALLOC_ENTRY Entry;
114
115 sprintf(buf, "\r\n===============\r\n%s(%d): SdbpFree called, tracing alloc:\r\n", file, line);
116 OutputDebugStringA(buf);
117
118 Lookup.Address = address;
119 while (Lookup.Address)
120 {
121 Entry = RtlLookupElementGenericTableAvl(&g_SdbpAllocationTable, &Lookup);
122 if (Entry)
123 {
124 Lookup = *Entry;
125 RtlDeleteElementGenericTableAvl(&g_SdbpAllocationTable, Entry);
126
127 sprintf(buf, " > %s(%d): %s%sAlloc( %d ) ==> %p\r\n", Lookup.File, Lookup.Line,
128 Lookup.Next ? "Invalidated " : "", Lookup.Prev ? "Re" : "", Lookup.Size, Lookup.Address);
129 OutputDebugStringA(buf);
130 Lookup.Address = Lookup.Prev;
131 }
132 else
133 {
134 Lookup.Address = NULL;
135 }
136 }
137 sprintf(buf, "\r\n===============\r\n");
138 OutputDebugStringA(buf);
139 }
140
141 #endif
142
143 static HANDLE g_Heap;
144 void SdbpHeapInit(void)
145 {
146 #if SDBAPI_DEBUG_ALLOC
147 RtlInitializeGenericTableAvl(&g_SdbpAllocationTable, ShimAllocCompareRoutine,
148 ShimAllocAllocateRoutine, ShimAllocFreeRoutine, NULL);
149 #endif
150 g_Heap = HeapCreate(0, 0x10000, 0);
151 }
152
153 void SdbpHeapDeinit(void)
154 {
155 #if SDBAPI_DEBUG_ALLOC
156 if (g_SdbpAllocationTable.NumberGenericTableElements != 0)
157 __debugbreak();
158 #endif
159 HeapDestroy(g_Heap);
160 }
161
162 static HANDLE SdbpHeap(void)
163 {
164 return g_Heap;
165 }
166
167 LPVOID SdbpAlloc(SIZE_T size
168 #if SDBAPI_DEBUG_ALLOC
169 , int line, const char* file
170 #endif
171 )
172 {
173 LPVOID mem = HeapAlloc(SdbpHeap(), HEAP_ZERO_MEMORY, size);
174 #if SDBAPI_DEBUG_ALLOC
175 SdbpInsertAllocation(mem, size, line, file);
176 #endif
177 return mem;
178 }
179
180 LPVOID SdbpReAlloc(LPVOID mem, SIZE_T size
181 #if SDBAPI_DEBUG_ALLOC
182 , int line, const char* file
183 #endif
184 )
185 {
186 LPVOID newmem = HeapReAlloc(SdbpHeap(), HEAP_ZERO_MEMORY, mem, size);
187 #if SDBAPI_DEBUG_ALLOC
188 SdbpUpdateAllocation(mem, newmem, size, line, file);
189 #endif
190 return newmem;
191 }
192
193 void SdbpFree(LPVOID mem
194 #if SDBAPI_DEBUG_ALLOC
195 , int line, const char* file
196 #endif
197 )
198 {
199 #if SDBAPI_DEBUG_ALLOC
200 SdbpRemoveAllocation(mem, line, file);
201 #endif
202 HeapFree(SdbpHeap(), 0, mem);
203 }
204
205 PDB WINAPI SdbpCreate(void)
206 {
207 PDB db = (PDB)SdbAlloc(sizeof(DB));
208 /* SdbAlloc zeroes the memory. */
209 return db;
210 }
211 DWORD SdbpStrlen(PCWSTR string)
212 {
213 return (lstrlenW(string) + 1) * sizeof(WCHAR);
214 }
215
216 PWSTR SdbpStrDup(LPCWSTR string)
217 {
218 PWSTR ret = SdbpAlloc(SdbpStrlen(string));
219 lstrcpyW(ret, string);
220 return ret;
221 }
222
223
224 BOOL WINAPI SdbpOpenMemMappedFile(LPCWSTR path, PMEMMAPPED mapping)
225 {
226 NTSTATUS Status;
227 OBJECT_ATTRIBUTES ObjectAttributes;
228 IO_STATUS_BLOCK IoStatusBlock;
229 FILE_STANDARD_INFORMATION FileStandard;
230 UNICODE_STRING FileName;
231
232 RtlZeroMemory(mapping, sizeof(*mapping));
233
234 if(!RtlDosPathNameToNtPathName_U(path, &FileName, NULL, NULL))
235 {
236 RtlFreeUnicodeString(&FileName);
237 return FALSE;
238 }
239
240 InitializeObjectAttributes(&ObjectAttributes, &FileName, OBJ_CASE_INSENSITIVE, NULL, NULL);
241 Status = NtOpenFile(&mapping->file, GENERIC_READ | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT);
242 RtlFreeUnicodeString(&FileName);
243
244 if (!NT_SUCCESS(Status))
245 {
246 SHIM_ERR("Failed to open file %S: 0x%lx\n", path, Status);
247 return FALSE;
248 }
249
250 Status = NtCreateSection(&mapping->section, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ, 0, 0, PAGE_READONLY, SEC_COMMIT, mapping->file);
251 if (!NT_SUCCESS(Status))
252 {
253 /* Special case */
254 if (Status == STATUS_MAPPED_FILE_SIZE_ZERO)
255 {
256 NtClose(mapping->file);
257 mapping->file = mapping->section = NULL;
258 return TRUE;
259 }
260 SHIM_ERR("Failed to create mapping for file: 0x%lx\n", Status);
261 goto err_out;
262 }
263
264 Status = NtQueryInformationFile(mapping->file, &IoStatusBlock, &FileStandard, sizeof(FileStandard), FileStandardInformation);
265 if (!NT_SUCCESS(Status))
266 {
267 SHIM_ERR("Failed to read file info for file: 0x%lx\n", Status);
268 goto err_out;
269 }
270
271 mapping->mapped_size = mapping->size = FileStandard.EndOfFile.LowPart;
272 Status = NtMapViewOfSection(mapping->section, NtCurrentProcess(), (PVOID*)&mapping->view, 0, 0, 0, &mapping->mapped_size, ViewUnmap, 0, PAGE_READONLY);
273 if (!NT_SUCCESS(Status))
274 {
275 SHIM_ERR("Failed to map view of file: 0x%lx\n", Status);
276 goto err_out;
277 }
278
279 return TRUE;
280
281 err_out:
282 if (!mapping->view)
283 {
284 if (mapping->section)
285 NtClose(mapping->section);
286 NtClose(mapping->file);
287 }
288 return FALSE;
289 }
290
291 void WINAPI SdbpCloseMemMappedFile(PMEMMAPPED mapping)
292 {
293 /* Prevent a VAD warning */
294 if (mapping->view)
295 NtUnmapViewOfSection(NtCurrentProcess(), mapping->view);
296 NtClose(mapping->section);
297 NtClose(mapping->file);
298 RtlZeroMemory(mapping, sizeof(*mapping));
299 }
300
301 BOOL WINAPI SdbpCheckTagType(TAG tag, WORD type)
302 {
303 if ((tag & TAG_TYPE_MASK) != type)
304 return FALSE;
305 return TRUE;
306 }
307
308 BOOL WINAPI SdbpCheckTagIDType(PDB db, TAGID tagid, WORD type)
309 {
310 TAG tag = SdbGetTagFromTagID(db, tagid);
311 if (tag == TAG_NULL)
312 return FALSE;
313 return SdbpCheckTagType(tag, type);
314 }
315
316 /**
317 * Opens specified shim database file.
318 *
319 * @param [in] path Path to the shim database.
320 * @param [in] type Type of path. Either DOS_PATH or NT_PATH.
321 *
322 * @return Success: Handle to the shim database, NULL otherwise.
323 */
324 PDB WINAPI SdbOpenDatabase(LPCWSTR path, PATH_TYPE type)
325 {
326 NTSTATUS Status;
327 IO_STATUS_BLOCK io;
328 OBJECT_ATTRIBUTES attr;
329 UNICODE_STRING str;
330 PDB db;
331 BYTE header[12];
332 DWORD dwRead = 0;
333
334 if (type == DOS_PATH)
335 {
336 if (!RtlDosPathNameToNtPathName_U(path, &str, NULL, NULL))
337 return NULL;
338 }
339 else
340 RtlInitUnicodeString(&str, path);
341
342 db = SdbpCreate();
343 if (!db)
344 {
345 SHIM_ERR("Failed to allocate memory for shim database\n");
346 return NULL;
347 }
348
349 InitializeObjectAttributes(&attr, &str, OBJ_CASE_INSENSITIVE, NULL, NULL);
350
351 Status = NtCreateFile(&db->file, FILE_GENERIC_READ | SYNCHRONIZE,
352 &attr, &io, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
353 FILE_OPEN, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
354
355 if (type == DOS_PATH)
356 RtlFreeUnicodeString(&str);
357
358 if (!NT_SUCCESS(Status))
359 {
360 SdbCloseDatabase(db);
361 SHIM_ERR("Failed to open shim database file: 0x%lx\n", Status);
362 return NULL;
363 }
364
365 db->size = GetFileSize(db->file, NULL);
366 db->data = SdbAlloc(db->size);
367 ReadFile(db->file, db->data, db->size, &dwRead, NULL);
368
369 if (!SdbpReadData(db, &header, 0, 12))
370 {
371 SdbCloseDatabase(db);
372 SHIM_ERR("Failed to read shim database header\n");
373 return NULL;
374 }
375
376 if (memcmp(&header[8], "sdbf", 4) != 0)
377 {
378 SdbCloseDatabase(db);
379 SHIM_ERR("Shim database header is invalid\n");
380 return NULL;
381 }
382
383 if (*(DWORD*)&header[0] != (DWORD)2)
384 {
385 SdbCloseDatabase(db);
386 SHIM_ERR("Invalid shim database version\n");
387 return NULL;
388 }
389
390 db->stringtable = SdbFindFirstTag(db, TAGID_ROOT, TAG_STRINGTABLE);
391 if(!SdbGetDatabaseID(db, &db->database_id))
392 {
393 SHIM_INFO("Failed to get the database id\n");
394 }
395 return db;
396 }
397
398 /**
399 * Closes specified database and frees its memory.
400 *
401 * @param [in] db Handle to the shim database.
402 */
403 void WINAPI SdbCloseDatabase(PDB db)
404 {
405 if (!db)
406 return;
407
408 if (db->file)
409 NtClose(db->file);
410 SdbFree(db->data);
411 SdbFree(db);
412 }
413
414 /**
415 * Parses a string to retrieve a GUID.
416 *
417 * @param [in] GuidString The string to parse.
418 * @param [out] Guid The resulting GUID.
419 *
420 * @return TRUE if it succeeds, FALSE if it fails.
421 */
422 BOOL WINAPI SdbGUIDFromString(PCWSTR GuidString, GUID *Guid)
423 {
424 UNICODE_STRING GuidString_u;
425 RtlInitUnicodeString(&GuidString_u, GuidString);
426 return NT_SUCCESS(RtlGUIDFromString(&GuidString_u, Guid));
427 }
428
429 /**
430 * Converts a GUID to a string.
431 *
432 * @param [in] Guid The GUID to convert.
433 * @param [out] GuidString The resulting string representation of Guid.
434 * @param [in] Length The length of GuidString.
435 *
436 * @return TRUE if it succeeds, FALSE if it fails.
437 */
438 BOOL WINAPI SdbGUIDToString(CONST GUID *Guid, PWSTR GuidString, SIZE_T Length)
439 {
440 UNICODE_STRING GuidString_u;
441 if(NT_SUCCESS(RtlStringFromGUID(Guid, &GuidString_u)))
442 {
443 HRESULT hr = StringCchCopyNW(GuidString, Length, GuidString_u.Buffer, GuidString_u.Length / 2);
444 RtlFreeUnicodeString(&GuidString_u);
445 return SUCCEEDED(hr);
446 }
447 return FALSE;
448 }
449
450 /**
451 * Checks if the specified GUID is a NULL GUID
452 *
453 * @param [in] Guid The GUID to check.
454 *
455 * @return TRUE if it is a NULL GUID.
456 */
457 BOOL WINAPI SdbIsNullGUID(CONST GUID *Guid)
458 {
459 static GUID NullGuid = { 0 };
460 return !Guid || IsEqualGUID(&NullGuid, Guid);
461 }
462
463 /**
464 * Get the GUID from one of the standard databases.
465 *
466 * @param [in] Flags The ID to retrieve the guid from. (See SDB_DATABASE_MAIN_[xxx])
467 * @param [out] Guid The resulting GUID.
468 *
469 * @return TRUE if a known database ID.
470 */
471 BOOL WINAPI SdbGetStandardDatabaseGUID(DWORD Flags, GUID* Guid)
472 {
473 const GUID* copy_from = NULL;
474 switch(Flags & HID_DATABASE_TYPE_MASK)
475 {
476 case SDB_DATABASE_MAIN_MSI:
477 copy_from = &GUID_DATABASE_MSI;
478 break;
479 case SDB_DATABASE_MAIN_SHIM:
480 copy_from = &GUID_DATABASE_SHIM;
481 break;
482 case SDB_DATABASE_MAIN_DRIVERS:
483 copy_from = &GUID_DATABASE_DRIVERS;
484 break;
485 default:
486 SHIM_ERR("Cannot obtain database guid for databases other than main\n");
487 return FALSE;
488 }
489 if(Guid)
490 {
491 memcpy(Guid, copy_from, sizeof(GUID));
492 }
493 return TRUE;
494 }
495
496 /**
497 * Converts specified tag into a string.
498 *
499 * @param [in] tag The tag which will be converted to a string.
500 *
501 * @return Success: Pointer to the string matching specified tag, or L"InvalidTag" on failure.
502 *
503 * @todo: Convert this into a lookup table, this is wasting alot of space.
504 */
505 LPCWSTR WINAPI SdbTagToString(TAG tag)
506 {
507 /* lookup tables for tags in range 0x1 -> 0xFF | TYPE */
508 static const WCHAR table[9][0x32][25] = {
509 { /* TAG_TYPE_NULL */
510 {'I','N','C','L','U','D','E',0},
511 {'G','E','N','E','R','A','L',0},
512 {'M','A','T','C','H','_','L','O','G','I','C','_','N','O','T',0},
513 {'A','P','P','L','Y','_','A','L','L','_','S','H','I','M','S',0},
514 {'U','S','E','_','S','E','R','V','I','C','E','_','P','A','C','K','_','F','I','L','E','S',0},
515 {'M','I','T','I','G','A','T','I','O','N','_','O','S',0},
516 {'B','L','O','C','K','_','U','P','G','R','A','D','E',0},
517 {'I','N','C','L','U','D','E','E','X','C','L','U','D','E','D','L','L',0},
518 {'R','A','C','_','E','V','E','N','T','_','O','F','F',0},
519 {'T','E','L','E','M','E','T','R','Y','_','O','F','F',0},
520 {'S','H','I','M','_','E','N','G','I','N','E','_','O','F','F',0},
521 {'L','A','Y','E','R','_','P','R','O','P','A','G','A','T','I','O','N','_','O','F','F',0},
522 {'R','E','I','N','S','T','A','L','L','_','U','P','G','R','A','D','E',0}
523 },
524 { /* TAG_TYPE_BYTE */
525 {'I','n','v','a','l','i','d','T','a','g',0}
526 },
527 { /* TAG_TYPE_WORD */
528 {'M','A','T','C','H','_','M','O','D','E',0}
529 },
530 { /* TAG_TYPE_DWORD */
531 {'S','I','Z','E',0},
532 {'O','F','F','S','E','T',0},
533 {'C','H','E','C','K','S','U','M',0},
534 {'S','H','I','M','_','T','A','G','I','D',0},
535 {'P','A','T','C','H','_','T','A','G','I','D',0},
536 {'M','O','D','U','L','E','_','T','Y','P','E',0},
537 {'V','E','R','D','A','T','E','H','I',0},
538 {'V','E','R','D','A','T','E','L','O',0},
539 {'V','E','R','F','I','L','E','O','S',0},
540 {'V','E','R','F','I','L','E','T','Y','P','E',0},
541 {'P','E','_','C','H','E','C','K','S','U','M',0},
542 {'P','R','E','V','O','S','M','A','J','O','R','V','E','R',0},
543 {'P','R','E','V','O','S','M','I','N','O','R','V','E','R',0},
544 {'P','R','E','V','O','S','P','L','A','T','F','O','R','M','I','D',0},
545 {'P','R','E','V','O','S','B','U','I','L','D','N','O',0},
546 {'P','R','O','B','L','E','M','S','E','V','E','R','I','T','Y',0},
547 {'L','A','N','G','I','D',0},
548 {'V','E','R','_','L','A','N','G','U','A','G','E',0},
549 {'I','n','v','a','l','i','d','T','a','g',0},
550 {'E','N','G','I','N','E',0},
551 {'H','T','M','L','H','E','L','P','I','D',0},
552 {'I','N','D','E','X','_','F','L','A','G','S',0},
553 {'F','L','A','G','S',0},
554 {'D','A','T','A','_','V','A','L','U','E','T','Y','P','E',0},
555 {'D','A','T','A','_','D','W','O','R','D',0},
556 {'L','A','Y','E','R','_','T','A','G','I','D',0},
557 {'M','S','I','_','T','R','A','N','S','F','O','R','M','_','T','A','G','I','D',0},
558 {'L','I','N','K','E','R','_','V','E','R','S','I','O','N',0},
559 {'L','I','N','K','_','D','A','T','E',0},
560 {'U','P','T','O','_','L','I','N','K','_','D','A','T','E',0},
561 {'O','S','_','S','E','R','V','I','C','E','_','P','A','C','K',0},
562 {'F','L','A','G','_','T','A','G','I','D',0},
563 {'R','U','N','T','I','M','E','_','P','L','A','T','F','O','R','M',0},
564 {'O','S','_','S','K','U',0},
565 {'O','S','_','P','L','A','T','F','O','R','M',0},
566 {'A','P','P','_','N','A','M','E','_','R','C','_','I','D',0},
567 {'V','E','N','D','O','R','_','N','A','M','E','_','R','C','_','I','D',0},
568 {'S','U','M','M','A','R','Y','_','M','S','G','_','R','C','_','I','D',0},
569 {'V','I','S','T','A','_','S','K','U',0},
570 {'D','E','S','C','R','I','P','T','I','O','N','_','R','C','_','I','D',0},
571 {'P','A','R','A','M','E','T','E','R','1','_','R','C','_','I','D',0},
572 {'I','n','v','a','l','i','d','T','a','g',0},
573 {'I','n','v','a','l','i','d','T','a','g',0},
574 {'I','n','v','a','l','i','d','T','a','g',0},
575 {'I','n','v','a','l','i','d','T','a','g',0},
576 {'I','n','v','a','l','i','d','T','a','g',0},
577 {'I','n','v','a','l','i','d','T','a','g',0},
578 {'C','O','N','T','E','X','T','_','T','A','G','I','D',0},
579 {'E','X','E','_','W','R','A','P','P','E','R',0},
580 {'U','R','L','_','I','D',0}
581 },
582 { /* TAG_TYPE_QWORD */
583 {'T','I','M','E',0},
584 {'B','I','N','_','F','I','L','E','_','V','E','R','S','I','O','N',0},
585 {'B','I','N','_','P','R','O','D','U','C','T','_','V','E','R','S','I','O','N',0},
586 {'M','O','D','T','I','M','E',0},
587 {'F','L','A','G','_','M','A','S','K','_','K','E','R','N','E','L',0},
588 {'U','P','T','O','_','B','I','N','_','P','R','O','D','U','C','T','_','V','E','R','S','I','O','N',0},
589 {'D','A','T','A','_','Q','W','O','R','D',0},
590 {'F','L','A','G','_','M','A','S','K','_','U','S','E','R',0},
591 {'F','L','A','G','S','_','N','T','V','D','M','1',0},
592 {'F','L','A','G','S','_','N','T','V','D','M','2',0},
593 {'F','L','A','G','S','_','N','T','V','D','M','3',0},
594 {'F','L','A','G','_','M','A','S','K','_','S','H','E','L','L',0},
595 {'U','P','T','O','_','B','I','N','_','F','I','L','E','_','V','E','R','S','I','O','N',0},
596 {'F','L','A','G','_','M','A','S','K','_','F','U','S','I','O','N',0},
597 {'F','L','A','G','_','P','R','O','C','E','S','S','P','A','R','A','M',0},
598 {'F','L','A','G','_','L','U','A',0},
599 {'F','L','A','G','_','I','N','S','T','A','L','L',0}
600 },
601 { /* TAG_TYPE_STRINGREF */
602 {'N','A','M','E',0},
603 {'D','E','S','C','R','I','P','T','I','O','N',0},
604 {'M','O','D','U','L','E',0},
605 {'A','P','I',0},
606 {'V','E','N','D','O','R',0},
607 {'A','P','P','_','N','A','M','E',0},
608 {'I','n','v','a','l','i','d','T','a','g',0},
609 {'C','O','M','M','A','N','D','_','L','I','N','E',0},
610 {'C','O','M','P','A','N','Y','_','N','A','M','E',0},
611 {'D','L','L','F','I','L','E',0},
612 {'W','I','L','D','C','A','R','D','_','N','A','M','E',0},
613 {'I','n','v','a','l','i','d','T','a','g',0},
614 {'I','n','v','a','l','i','d','T','a','g',0},
615 {'I','n','v','a','l','i','d','T','a','g',0},
616 {'I','n','v','a','l','i','d','T','a','g',0},
617 {'P','R','O','D','U','C','T','_','N','A','M','E',0},
618 {'P','R','O','D','U','C','T','_','V','E','R','S','I','O','N',0},
619 {'F','I','L','E','_','D','E','S','C','R','I','P','T','I','O','N',0},
620 {'F','I','L','E','_','V','E','R','S','I','O','N',0},
621 {'O','R','I','G','I','N','A','L','_','F','I','L','E','N','A','M','E',0},
622 {'I','N','T','E','R','N','A','L','_','N','A','M','E',0},
623 {'L','E','G','A','L','_','C','O','P','Y','R','I','G','H','T',0},
624 {'1','6','B','I','T','_','D','E','S','C','R','I','P','T','I','O','N',0},
625 {'A','P','P','H','E','L','P','_','D','E','T','A','I','L','S',0},
626 {'L','I','N','K','_','U','R','L',0},
627 {'L','I','N','K','_','T','E','X','T',0},
628 {'A','P','P','H','E','L','P','_','T','I','T','L','E',0},
629 {'A','P','P','H','E','L','P','_','C','O','N','T','A','C','T',0},
630 {'S','X','S','_','M','A','N','I','F','E','S','T',0},
631 {'D','A','T','A','_','S','T','R','I','N','G',0},
632 {'M','S','I','_','T','R','A','N','S','F','O','R','M','_','F','I','L','E',0},
633 {'1','6','B','I','T','_','M','O','D','U','L','E','_','N','A','M','E',0},
634 {'L','A','Y','E','R','_','D','I','S','P','L','A','Y','N','A','M','E',0},
635 {'C','O','M','P','I','L','E','R','_','V','E','R','S','I','O','N',0},
636 {'A','C','T','I','O','N','_','T','Y','P','E',0},
637 {'E','X','P','O','R','T','_','N','A','M','E',0},
638 {'U','R','L',0}
639 },
640 { /* TAG_TYPE_LIST */
641 {'D','A','T','A','B','A','S','E',0},
642 {'L','I','B','R','A','R','Y',0},
643 {'I','N','E','X','C','L','U','D','E',0},
644 {'S','H','I','M',0},
645 {'P','A','T','C','H',0},
646 {'A','P','P',0},
647 {'E','X','E',0},
648 {'M','A','T','C','H','I','N','G','_','F','I','L','E',0},
649 {'S','H','I','M','_','R','E','F',0},
650 {'P','A','T','C','H','_','R','E','F',0},
651 {'L','A','Y','E','R',0},
652 {'F','I','L','E',0},
653 {'A','P','P','H','E','L','P',0},
654 {'L','I','N','K',0},
655 {'D','A','T','A',0},
656 {'M','S','I','_','T','R','A','N','S','F','O','R','M',0},
657 {'M','S','I','_','T','R','A','N','S','F','O','R','M','_','R','E','F',0},
658 {'M','S','I','_','P','A','C','K','A','G','E',0},
659 {'F','L','A','G',0},
660 {'M','S','I','_','C','U','S','T','O','M','_','A','C','T','I','O','N',0},
661 {'F','L','A','G','_','R','E','F',0},
662 {'A','C','T','I','O','N',0},
663 {'L','O','O','K','U','P',0},
664 {'C','O','N','T','E','X','T',0},
665 {'C','O','N','T','E','X','T','_','R','E','F',0},
666 {'I','n','v','a','l','i','d','T','a','g',0},
667 {'I','n','v','a','l','i','d','T','a','g',0},
668 {'I','n','v','a','l','i','d','T','a','g',0},
669 {'I','n','v','a','l','i','d','T','a','g',0},
670 {'I','n','v','a','l','i','d','T','a','g',0},
671 {'I','n','v','a','l','i','d','T','a','g',0},
672 {'S','P','C',0}
673 },
674 { /* TAG_TYPE_STRING */
675 {'I','n','v','a','l','i','d','T','a','g',0}
676 },
677 { /* TAG_TYPE_BINARY */
678 {'I','n','v','a','l','i','d','T','a','g',0},
679 {'P','A','T','C','H','_','B','I','T','S',0},
680 {'F','I','L','E','_','B','I','T','S',0},
681 {'E','X','E','_','I','D',0},
682 {'D','A','T','A','_','B','I','T','S',0},
683 {'M','S','I','_','P','A','C','K','A','G','E','_','I','D',0},
684 {'D','A','T','A','B','A','S','E','_','I','D',0},
685 {'C','O','N','T','E','X','T','_','P','L','A','T','F','O','R','M','_','I','D',0},
686 {'C','O','N','T','E','X','T','_','B','R','A','N','C','H','_','I','D',0},
687 {'I','n','v','a','l','i','d','T','a','g',0},
688 {'I','n','v','a','l','i','d','T','a','g',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 {'F','I','X','_','I','D',0},
694 {'A','P','P','_','I','D',0}
695 }
696 };
697
698 /* sizes of tables in above array (# strings per type) */
699 static const WORD limits[9] = {
700 /* switch off TYPE_* nibble of last tag for each type */
701 TAG_REINSTALL_UPGRADE & 0xFF,
702 1,
703 TAG_MATCH_MODE & 0xFF,
704 TAG_URL_ID & 0xFF,
705 TAG_FLAG_INSTALL & 0xFF,
706 TAG_URL & 0xFF,
707 TAG_SPC & 0xFF,
708 1,
709 TAG_APP_ID & 0xFF
710 };
711
712 /* lookup tables for tags in range 0x800 + (0x1 -> 0xFF) | TYPE */
713 static const WCHAR table2[9][3][17] = {
714 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_NULL */
715 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_BYTE */
716 {
717 {'T','A','G',0}, /* TAG_TYPE_WORD */
718 {'I','N','D','E','X','_','T','A','G',0},
719 {'I','N','D','E','X','_','K','E','Y',0}
720 },
721 { {'T','A','G','I','D',0} }, /* TAG_TYPE_DWORD */
722 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_QWORD */
723 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_STRINGREF */
724 {
725 {'S','T','R','I','N','G','T','A','B','L','E',0}, /* TAG_TYPE_LIST */
726 {'I','N','D','E','X','E','S',0},
727 {'I','N','D','E','X',0}
728 },
729 { {'S','T','R','I','N','G','T','A','B','L','E','_','I','T','E','M',0}, }, /* TAG_TYPE_STRING */
730 { {'I','N','D','E','X','_','B','I','T','S',0} } /* TAG_TYPE_BINARY */
731 };
732
733 /* sizes of tables in above array, hardcoded for simplicity */
734 static const WORD limits2[9] = { 0, 0, 3, 1, 0, 0, 3, 1, 1 };
735
736 static const WCHAR null[] = {'N','U','L','L',0};
737 static const WCHAR invalid[] = {'I','n','v','a','l','i','d','T','a','g',0};
738
739 BOOL switch_table; /* should we use table2 and limits2? */
740 WORD index, type_index;
741
742 /* special case: null tag */
743 if (tag == TAG_NULL)
744 return null;
745
746 /* tags with only type mask or no type mask are invalid */
747 if ((tag & ~TAG_TYPE_MASK) == 0 || (tag & TAG_TYPE_MASK) == 0)
748 return invalid;
749
750 /* some valid tags are in range 0x800 + (0x1 -> 0xF) | TYPE */
751 if ((tag & 0xF00) == 0x800)
752 switch_table = TRUE;
753 else if ((tag & 0xF00) == 0)
754 switch_table = FALSE;
755 else return invalid;
756
757 /* index of table in array is type nibble */
758 type_index = (tag >> 12) - 1;
759
760 /* index of string in table is low byte */
761 index = (tag & 0xFF) - 1;
762
763 /* bound check */
764 if (type_index >= 9 || index >= (switch_table ? limits2[type_index] : limits[type_index]))
765 return invalid;
766
767 /* tag is valid */
768 return switch_table ? table2[type_index][index] : table[type_index][index];
769 }