[APPHELP][APPHELP_APITEST] Add SdbGetFileAttributes + tests, based on the work of...
[reactos.git] / reactos / dll / appcompat / apphelp / sdbapi.c
1 /*
2 * Copyright 2011 André Hentschel
3 * Copyright 2013 Mislav Blažević
4 * Copyright 2015 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 HANDLE SdbpHeap(void);
31
32 #if SDBAPI_DEBUG_ALLOC
33
34 typedef struct SHIM_ALLOC_ENTRY
35 {
36 PVOID Address;
37 SIZE_T Size;
38 int Line;
39 const char* File;
40 PVOID Next;
41 PVOID Prev;
42 } SHIM_ALLOC_ENTRY, *PSHIM_ALLOC_ENTRY;
43
44
45 static RTL_AVL_TABLE g_SdbpAllocationTable;
46
47
48 static RTL_GENERIC_COMPARE_RESULTS
49 NTAPI ShimAllocCompareRoutine(_In_ PRTL_AVL_TABLE Table, _In_ PVOID FirstStruct, _In_ PVOID SecondStruct)
50 {
51 PVOID First = ((PSHIM_ALLOC_ENTRY)FirstStruct)->Address;
52 PVOID Second = ((PSHIM_ALLOC_ENTRY)SecondStruct)->Address;
53
54 if (First < Second)
55 return GenericLessThan;
56 else if (First == Second)
57 return GenericEqual;
58 return GenericGreaterThan;
59 }
60
61 static PVOID NTAPI ShimAllocAllocateRoutine(_In_ PRTL_AVL_TABLE Table, _In_ CLONG ByteSize)
62 {
63 return HeapAlloc(SdbpHeap(), HEAP_ZERO_MEMORY, ByteSize);
64 }
65
66 static VOID NTAPI ShimAllocFreeRoutine(_In_ PRTL_AVL_TABLE Table, _In_ PVOID Buffer)
67 {
68 HeapFree(SdbpHeap(), 0, Buffer);
69 }
70
71 static void SdbpInsertAllocation(PVOID address, SIZE_T size, int line, const char* file)
72 {
73 SHIM_ALLOC_ENTRY Entry = {0};
74
75 Entry.Address = address;
76 Entry.Size = size;
77 Entry.Line = line;
78 Entry.File = file;
79 RtlInsertElementGenericTableAvl(&g_SdbpAllocationTable, &Entry, sizeof(Entry), NULL);
80 }
81
82 static void SdbpUpdateAllocation(PVOID address, PVOID newaddress, SIZE_T size, int line, const char* file)
83 {
84 SHIM_ALLOC_ENTRY Lookup = {0};
85 PSHIM_ALLOC_ENTRY Entry;
86 Lookup.Address = address;
87 Entry = RtlLookupElementGenericTableAvl(&g_SdbpAllocationTable, &Lookup);
88
89 if (address == newaddress)
90 {
91 Entry->Size = size;
92 }
93 else
94 {
95 Lookup.Address = newaddress;
96 Lookup.Size = size;
97 Lookup.Line = line;
98 Lookup.File = file;
99 Lookup.Prev = address;
100 RtlInsertElementGenericTableAvl(&g_SdbpAllocationTable, &Lookup, sizeof(Lookup), NULL);
101 Entry->Next = newaddress;
102 }
103 }
104
105 static void SdbpRemoveAllocation(PVOID address, int line, const char* file)
106 {
107 char buf[512];
108 SHIM_ALLOC_ENTRY Lookup = {0};
109 PSHIM_ALLOC_ENTRY Entry;
110
111 sprintf(buf, "\r\n===============\r\n%s(%d): SdbpFree called, tracing alloc:\r\n", file, line);
112 OutputDebugStringA(buf);
113
114 Lookup.Address = address;
115 while (Lookup.Address)
116 {
117 Entry = RtlLookupElementGenericTableAvl(&g_SdbpAllocationTable, &Lookup);
118 if (Entry)
119 {
120 Lookup = *Entry;
121 RtlDeleteElementGenericTableAvl(&g_SdbpAllocationTable, Entry);
122
123 sprintf(buf, " > %s(%d): %s%sAlloc( %d ) ==> %p\r\n", Lookup.File, Lookup.Line,
124 Lookup.Next ? "Invalidated " : "", Lookup.Prev ? "Re" : "", Lookup.Size, Lookup.Address);
125 OutputDebugStringA(buf);
126 Lookup.Address = Lookup.Prev;
127 }
128 else
129 {
130 Lookup.Address = NULL;
131 }
132 }
133 sprintf(buf, "\r\n===============\r\n");
134 OutputDebugStringA(buf);
135 }
136
137 #endif
138
139 static HANDLE g_Heap;
140 void SdbpHeapInit(void)
141 {
142 #if SDBAPI_DEBUG_ALLOC
143 RtlInitializeGenericTableAvl(&g_SdbpAllocationTable, ShimAllocCompareRoutine,
144 ShimAllocAllocateRoutine, ShimAllocFreeRoutine, NULL);
145 #endif
146 g_Heap = HeapCreate(0, 0x10000, 0);
147 }
148
149 void SdbpHeapDeinit(void)
150 {
151 #if SDBAPI_DEBUG_ALLOC
152 if (g_SdbpAllocationTable.NumberGenericTableElements != 0)
153 __debugbreak();
154 #endif
155 HeapDestroy(g_Heap);
156 }
157
158 static HANDLE SdbpHeap(void)
159 {
160 return g_Heap;
161 }
162
163 LPVOID SdbpAlloc(SIZE_T size
164 #if SDBAPI_DEBUG_ALLOC
165 , int line, const char* file
166 #endif
167 )
168 {
169 LPVOID mem = HeapAlloc(SdbpHeap(), HEAP_ZERO_MEMORY, size);
170 #if SDBAPI_DEBUG_ALLOC
171 SdbpInsertAllocation(mem, size, line, file);
172 #endif
173 return mem;
174 }
175
176 LPVOID SdbpReAlloc(LPVOID mem, SIZE_T size
177 #if SDBAPI_DEBUG_ALLOC
178 , int line, const char* file
179 #endif
180 )
181 {
182 LPVOID newmem = HeapReAlloc(SdbpHeap(), HEAP_ZERO_MEMORY, mem, size);
183 #if SDBAPI_DEBUG_ALLOC
184 SdbpUpdateAllocation(mem, newmem, size, line, file);
185 #endif
186 return newmem;
187 }
188
189 void SdbpFree(LPVOID mem
190 #if SDBAPI_DEBUG_ALLOC
191 , int line, const char* file
192 #endif
193 )
194 {
195 #if SDBAPI_DEBUG_ALLOC
196 SdbpRemoveAllocation(mem, line, file);
197 #endif
198 HeapFree(SdbpHeap(), 0, mem);
199 }
200
201 DWORD SdbpStrlen(PCWSTR string)
202 {
203 return (lstrlenW(string) + 1) * sizeof(WCHAR);
204 }
205
206 PWSTR SdbpStrDup(LPCWSTR string)
207 {
208 PWSTR ret = SdbpAlloc(SdbpStrlen(string));
209 lstrcpyW(ret, string);
210 return ret;
211 }
212
213
214 BOOL WINAPI SdbpOpenMemMappedFile(LPCWSTR path, PMEMMAPPED mapping)
215 {
216 NTSTATUS Status;
217 OBJECT_ATTRIBUTES ObjectAttributes;
218 IO_STATUS_BLOCK IoStatusBlock;
219 FILE_STANDARD_INFORMATION FileStandard;
220 UNICODE_STRING FileName;
221
222 RtlZeroMemory(mapping, sizeof(*mapping));
223
224 if(!RtlDosPathNameToNtPathName_U(path, &FileName, NULL, NULL))
225 {
226 RtlFreeUnicodeString(&FileName);
227 return FALSE;
228 }
229
230 InitializeObjectAttributes(&ObjectAttributes, &FileName, OBJ_CASE_INSENSITIVE, NULL, NULL);
231 Status = NtOpenFile(&mapping->file, GENERIC_READ | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT);
232 RtlFreeUnicodeString(&FileName);
233
234 if (!NT_SUCCESS(Status))
235 {
236 SHIM_ERR("Failed to open file %S: 0x%lx\n", path, Status);
237 return FALSE;
238 }
239
240 Status = NtCreateSection(&mapping->section, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ, 0, 0, PAGE_READONLY, SEC_COMMIT, mapping->file);
241 if (!NT_SUCCESS(Status))
242 {
243 /* Special case */
244 if (Status == STATUS_MAPPED_FILE_SIZE_ZERO)
245 {
246 NtClose(mapping->file);
247 mapping->file = mapping->section = NULL;
248 return TRUE;
249 }
250 SHIM_ERR("Failed to create mapping for file: 0x%lx\n", Status);
251 goto err_out;
252 }
253
254 Status = NtQueryInformationFile(mapping->file, &IoStatusBlock, &FileStandard, sizeof(FileStandard), FileStandardInformation);
255 if (!NT_SUCCESS(Status))
256 {
257 SHIM_ERR("Failed to read file info for file: 0x%lx\n", Status);
258 goto err_out;
259 }
260
261 mapping->mapped_size = mapping->size = FileStandard.EndOfFile.LowPart;
262 Status = NtMapViewOfSection(mapping->section, NtCurrentProcess(), (PVOID*)&mapping->view, 0, 0, 0, &mapping->mapped_size, ViewUnmap, 0, PAGE_READONLY);
263 if (!NT_SUCCESS(Status))
264 {
265 SHIM_ERR("Failed to map view of file: 0x%lx\n", Status);
266 goto err_out;
267 }
268
269 return TRUE;
270
271 err_out:
272 if (!mapping->view)
273 {
274 if (mapping->section)
275 NtClose(mapping->section);
276 NtClose(mapping->file);
277 }
278 return FALSE;
279 }
280
281 void WINAPI SdbpCloseMemMappedFile(PMEMMAPPED mapping)
282 {
283 NtUnmapViewOfSection(NtCurrentProcess(), mapping->view);
284 NtClose(mapping->section);
285 NtClose(mapping->file);
286 RtlZeroMemory(mapping, sizeof(*mapping));
287 }
288
289 /**
290 * Converts specified tag into a string.
291 *
292 * @param [in] tag The tag which will be converted to a string.
293 *
294 * @return Success: Pointer to the string matching specified tag, or L"InvalidTag" on failure.
295 */
296 LPCWSTR WINAPI SdbTagToString(TAG tag)
297 {
298 /* lookup tables for tags in range 0x1 -> 0xFF | TYPE */
299 static const WCHAR table[9][0x32][25] = {
300 { /* TAG_TYPE_NULL */
301 {'I','N','C','L','U','D','E',0},
302 {'G','E','N','E','R','A','L',0},
303 {'M','A','T','C','H','_','L','O','G','I','C','_','N','O','T',0},
304 {'A','P','P','L','Y','_','A','L','L','_','S','H','I','M','S',0},
305 {'U','S','E','_','S','E','R','V','I','C','E','_','P','A','C','K','_','F','I','L','E','S',0},
306 {'M','I','T','I','G','A','T','I','O','N','_','O','S',0},
307 {'B','L','O','C','K','_','U','P','G','R','A','D','E',0},
308 {'I','N','C','L','U','D','E','E','X','C','L','U','D','E','D','L','L',0},
309 {'R','A','C','_','E','V','E','N','T','_','O','F','F',0},
310 {'T','E','L','E','M','E','T','R','Y','_','O','F','F',0},
311 {'S','H','I','M','_','E','N','G','I','N','E','_','O','F','F',0},
312 {'L','A','Y','E','R','_','P','R','O','P','A','G','A','T','I','O','N','_','O','F','F',0},
313 {'R','E','I','N','S','T','A','L','L','_','U','P','G','R','A','D','E',0}
314 },
315 { /* TAG_TYPE_BYTE */
316 {'I','n','v','a','l','i','d','T','a','g',0}
317 },
318 { /* TAG_TYPE_WORD */
319 {'M','A','T','C','H','_','M','O','D','E',0}
320 },
321 { /* TAG_TYPE_DWORD */
322 {'S','I','Z','E',0},
323 {'O','F','F','S','E','T',0},
324 {'C','H','E','C','K','S','U','M',0},
325 {'S','H','I','M','_','T','A','G','I','D',0},
326 {'P','A','T','C','H','_','T','A','G','I','D',0},
327 {'M','O','D','U','L','E','_','T','Y','P','E',0},
328 {'V','E','R','D','A','T','E','H','I',0},
329 {'V','E','R','D','A','T','E','L','O',0},
330 {'V','E','R','F','I','L','E','O','S',0},
331 {'V','E','R','F','I','L','E','T','Y','P','E',0},
332 {'P','E','_','C','H','E','C','K','S','U','M',0},
333 {'P','R','E','V','O','S','M','A','J','O','R','V','E','R',0},
334 {'P','R','E','V','O','S','M','I','N','O','R','V','E','R',0},
335 {'P','R','E','V','O','S','P','L','A','T','F','O','R','M','I','D',0},
336 {'P','R','E','V','O','S','B','U','I','L','D','N','O',0},
337 {'P','R','O','B','L','E','M','S','E','V','E','R','I','T','Y',0},
338 {'L','A','N','G','I','D',0},
339 {'V','E','R','_','L','A','N','G','U','A','G','E',0},
340 {'I','n','v','a','l','i','d','T','a','g',0},
341 {'E','N','G','I','N','E',0},
342 {'H','T','M','L','H','E','L','P','I','D',0},
343 {'I','N','D','E','X','_','F','L','A','G','S',0},
344 {'F','L','A','G','S',0},
345 {'D','A','T','A','_','V','A','L','U','E','T','Y','P','E',0},
346 {'D','A','T','A','_','D','W','O','R','D',0},
347 {'L','A','Y','E','R','_','T','A','G','I','D',0},
348 {'M','S','I','_','T','R','A','N','S','F','O','R','M','_','T','A','G','I','D',0},
349 {'L','I','N','K','E','R','_','V','E','R','S','I','O','N',0},
350 {'L','I','N','K','_','D','A','T','E',0},
351 {'U','P','T','O','_','L','I','N','K','_','D','A','T','E',0},
352 {'O','S','_','S','E','R','V','I','C','E','_','P','A','C','K',0},
353 {'F','L','A','G','_','T','A','G','I','D',0},
354 {'R','U','N','T','I','M','E','_','P','L','A','T','F','O','R','M',0},
355 {'O','S','_','S','K','U',0},
356 {'O','S','_','P','L','A','T','F','O','R','M',0},
357 {'A','P','P','_','N','A','M','E','_','R','C','_','I','D',0},
358 {'V','E','N','D','O','R','_','N','A','M','E','_','R','C','_','I','D',0},
359 {'S','U','M','M','A','R','Y','_','M','S','G','_','R','C','_','I','D',0},
360 {'V','I','S','T','A','_','S','K','U',0},
361 {'D','E','S','C','R','I','P','T','I','O','N','_','R','C','_','I','D',0},
362 {'P','A','R','A','M','E','T','E','R','1','_','R','C','_','I','D',0},
363 {'I','n','v','a','l','i','d','T','a','g',0},
364 {'I','n','v','a','l','i','d','T','a','g',0},
365 {'I','n','v','a','l','i','d','T','a','g',0},
366 {'I','n','v','a','l','i','d','T','a','g',0},
367 {'I','n','v','a','l','i','d','T','a','g',0},
368 {'I','n','v','a','l','i','d','T','a','g',0},
369 {'C','O','N','T','E','X','T','_','T','A','G','I','D',0},
370 {'E','X','E','_','W','R','A','P','P','E','R',0},
371 {'U','R','L','_','I','D',0}
372 },
373 { /* TAG_TYPE_QWORD */
374 {'T','I','M','E',0},
375 {'B','I','N','_','F','I','L','E','_','V','E','R','S','I','O','N',0},
376 {'B','I','N','_','P','R','O','D','U','C','T','_','V','E','R','S','I','O','N',0},
377 {'M','O','D','T','I','M','E',0},
378 {'F','L','A','G','_','M','A','S','K','_','K','E','R','N','E','L',0},
379 {'U','P','T','O','_','B','I','N','_','P','R','O','D','U','C','T','_','V','E','R','S','I','O','N',0},
380 {'D','A','T','A','_','Q','W','O','R','D',0},
381 {'F','L','A','G','_','M','A','S','K','_','U','S','E','R',0},
382 {'F','L','A','G','S','_','N','T','V','D','M','1',0},
383 {'F','L','A','G','S','_','N','T','V','D','M','2',0},
384 {'F','L','A','G','S','_','N','T','V','D','M','3',0},
385 {'F','L','A','G','_','M','A','S','K','_','S','H','E','L','L',0},
386 {'U','P','T','O','_','B','I','N','_','F','I','L','E','_','V','E','R','S','I','O','N',0},
387 {'F','L','A','G','_','M','A','S','K','_','F','U','S','I','O','N',0},
388 {'F','L','A','G','_','P','R','O','C','E','S','S','P','A','R','A','M',0},
389 {'F','L','A','G','_','L','U','A',0},
390 {'F','L','A','G','_','I','N','S','T','A','L','L',0}
391 },
392 { /* TAG_TYPE_STRINGREF */
393 {'N','A','M','E',0},
394 {'D','E','S','C','R','I','P','T','I','O','N',0},
395 {'M','O','D','U','L','E',0},
396 {'A','P','I',0},
397 {'V','E','N','D','O','R',0},
398 {'A','P','P','_','N','A','M','E',0},
399 {'I','n','v','a','l','i','d','T','a','g',0},
400 {'C','O','M','M','A','N','D','_','L','I','N','E',0},
401 {'C','O','M','P','A','N','Y','_','N','A','M','E',0},
402 {'D','L','L','F','I','L','E',0},
403 {'W','I','L','D','C','A','R','D','_','N','A','M','E',0},
404 {'I','n','v','a','l','i','d','T','a','g',0},
405 {'I','n','v','a','l','i','d','T','a','g',0},
406 {'I','n','v','a','l','i','d','T','a','g',0},
407 {'I','n','v','a','l','i','d','T','a','g',0},
408 {'P','R','O','D','U','C','T','_','N','A','M','E',0},
409 {'P','R','O','D','U','C','T','_','V','E','R','S','I','O','N',0},
410 {'F','I','L','E','_','D','E','S','C','R','I','P','T','I','O','N',0},
411 {'F','I','L','E','_','V','E','R','S','I','O','N',0},
412 {'O','R','I','G','I','N','A','L','_','F','I','L','E','N','A','M','E',0},
413 {'I','N','T','E','R','N','A','L','_','N','A','M','E',0},
414 {'L','E','G','A','L','_','C','O','P','Y','R','I','G','H','T',0},
415 {'1','6','B','I','T','_','D','E','S','C','R','I','P','T','I','O','N',0},
416 {'A','P','P','H','E','L','P','_','D','E','T','A','I','L','S',0},
417 {'L','I','N','K','_','U','R','L',0},
418 {'L','I','N','K','_','T','E','X','T',0},
419 {'A','P','P','H','E','L','P','_','T','I','T','L','E',0},
420 {'A','P','P','H','E','L','P','_','C','O','N','T','A','C','T',0},
421 {'S','X','S','_','M','A','N','I','F','E','S','T',0},
422 {'D','A','T','A','_','S','T','R','I','N','G',0},
423 {'M','S','I','_','T','R','A','N','S','F','O','R','M','_','F','I','L','E',0},
424 {'1','6','B','I','T','_','M','O','D','U','L','E','_','N','A','M','E',0},
425 {'L','A','Y','E','R','_','D','I','S','P','L','A','Y','N','A','M','E',0},
426 {'C','O','M','P','I','L','E','R','_','V','E','R','S','I','O','N',0},
427 {'A','C','T','I','O','N','_','T','Y','P','E',0},
428 {'E','X','P','O','R','T','_','N','A','M','E',0},
429 {'U','R','L',0}
430 },
431 { /* TAG_TYPE_LIST */
432 {'D','A','T','A','B','A','S','E',0},
433 {'L','I','B','R','A','R','Y',0},
434 {'I','N','E','X','C','L','U','D','E',0},
435 {'S','H','I','M',0},
436 {'P','A','T','C','H',0},
437 {'A','P','P',0},
438 {'E','X','E',0},
439 {'M','A','T','C','H','I','N','G','_','F','I','L','E',0},
440 {'S','H','I','M','_','R','E','F',0},
441 {'P','A','T','C','H','_','R','E','F',0},
442 {'L','A','Y','E','R',0},
443 {'F','I','L','E',0},
444 {'A','P','P','H','E','L','P',0},
445 {'L','I','N','K',0},
446 {'D','A','T','A',0},
447 {'M','S','I','_','T','R','A','N','S','F','O','R','M',0},
448 {'M','S','I','_','T','R','A','N','S','F','O','R','M','_','R','E','F',0},
449 {'M','S','I','_','P','A','C','K','A','G','E',0},
450 {'F','L','A','G',0},
451 {'M','S','I','_','C','U','S','T','O','M','_','A','C','T','I','O','N',0},
452 {'F','L','A','G','_','R','E','F',0},
453 {'A','C','T','I','O','N',0},
454 {'L','O','O','K','U','P',0},
455 {'C','O','N','T','E','X','T',0},
456 {'C','O','N','T','E','X','T','_','R','E','F',0},
457 {'I','n','v','a','l','i','d','T','a','g',0},
458 {'I','n','v','a','l','i','d','T','a','g',0},
459 {'I','n','v','a','l','i','d','T','a','g',0},
460 {'I','n','v','a','l','i','d','T','a','g',0},
461 {'I','n','v','a','l','i','d','T','a','g',0},
462 {'I','n','v','a','l','i','d','T','a','g',0},
463 {'S','P','C',0}
464 },
465 { /* TAG_TYPE_STRING */
466 {'I','n','v','a','l','i','d','T','a','g',0}
467 },
468 { /* TAG_TYPE_BINARY */
469 {'I','n','v','a','l','i','d','T','a','g',0},
470 {'P','A','T','C','H','_','B','I','T','S',0},
471 {'F','I','L','E','_','B','I','T','S',0},
472 {'E','X','E','_','I','D',0},
473 {'D','A','T','A','_','B','I','T','S',0},
474 {'M','S','I','_','P','A','C','K','A','G','E','_','I','D',0},
475 {'D','A','T','A','B','A','S','E','_','I','D',0},
476 {'C','O','N','T','E','X','T','_','P','L','A','T','F','O','R','M','_','I','D',0},
477 {'C','O','N','T','E','X','T','_','B','R','A','N','C','H','_','I','D',0},
478 {'I','n','v','a','l','i','d','T','a','g',0},
479 {'I','n','v','a','l','i','d','T','a','g',0},
480 {'I','n','v','a','l','i','d','T','a','g',0},
481 {'I','n','v','a','l','i','d','T','a','g',0},
482 {'I','n','v','a','l','i','d','T','a','g',0},
483 {'I','n','v','a','l','i','d','T','a','g',0},
484 {'F','I','X','_','I','D',0},
485 {'A','P','P','_','I','D',0}
486 }
487 };
488
489 /* sizes of tables in above array (# strings per type) */
490 static const WORD limits[9] = {
491 /* switch off TYPE_* nibble of last tag for each type */
492 TAG_REINSTALL_UPGRADE & 0xFF,
493 1,
494 TAG_MATCH_MODE & 0xFF,
495 TAG_URL_ID & 0xFF,
496 TAG_FLAG_INSTALL & 0xFF,
497 TAG_URL & 0xFF,
498 TAG_SPC & 0xFF,
499 1,
500 TAG_APP_ID & 0xFF
501 };
502
503 /* lookup tables for tags in range 0x800 + (0x1 -> 0xFF) | TYPE */
504 static const WCHAR table2[9][3][17] = {
505 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_NULL */
506 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_BYTE */
507 {
508 {'T','A','G',0}, /* TAG_TYPE_WORD */
509 {'I','N','D','E','X','_','T','A','G',0},
510 {'I','N','D','E','X','_','K','E','Y',0}
511 },
512 { {'T','A','G','I','D',0} }, /* TAG_TYPE_DWORD */
513 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_QWORD */
514 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_STRINGREF */
515 {
516 {'S','T','R','I','N','G','T','A','B','L','E',0}, /* TAG_TYPE_LIST */
517 {'I','N','D','E','X','E','S',0},
518 {'I','N','D','E','X',0}
519 },
520 { {'S','T','R','I','N','G','T','A','B','L','E','_','I','T','E','M',0}, }, /* TAG_TYPE_STRING */
521 { {'I','N','D','E','X','_','B','I','T','S',0} } /* TAG_TYPE_BINARY */
522 };
523
524 /* sizes of tables in above array, hardcoded for simplicity */
525 static const WORD limits2[9] = { 0, 0, 3, 1, 0, 0, 3, 1, 1 };
526
527 static const WCHAR null[] = {'N','U','L','L',0};
528 static const WCHAR invalid[] = {'I','n','v','a','l','i','d','T','a','g',0};
529
530 BOOL switch_table; /* should we use table2 and limits2? */
531 WORD index, type_index;
532
533 /* special case: null tag */
534 if (tag == TAG_NULL)
535 return null;
536
537 /* tags with only type mask or no type mask are invalid */
538 if ((tag & ~TAG_TYPE_MASK) == 0 || (tag & TAG_TYPE_MASK) == 0)
539 return invalid;
540
541 /* some valid tags are in range 0x800 + (0x1 -> 0xF) | TYPE */
542 if ((tag & 0xF00) == 0x800)
543 switch_table = TRUE;
544 else if ((tag & 0xF00) == 0)
545 switch_table = FALSE;
546 else return invalid;
547
548 /* index of table in array is type nibble */
549 type_index = (tag >> 12) - 1;
550
551 /* index of string in table is low byte */
552 index = (tag & 0xFF) - 1;
553
554 /* bound check */
555 if (type_index >= 9 || index >= (switch_table ? limits2[type_index] : limits[type_index]))
556 return invalid;
557
558 /* tag is valid */
559 return switch_table ? table2[type_index][index] : table[type_index][index];
560 }