Create the AHCI branch for Aman's work
[reactos.git] / 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 DWORD SdbpStrlen(PCWSTR string)
159 {
160 return (lstrlenW(string) + 1) * sizeof(WCHAR);
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
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 /**
207 * Converts specified tag into a string.
208 *
209 * @param [in] tag The tag which will be converted to a string.
210 *
211 * @return Success: Pointer to the string matching specified tag, or L"InvalidTag" on failure.
212 */
213 LPCWSTR WINAPI SdbTagToString(TAG tag)
214 {
215 /* lookup tables for tags in range 0x1 -> 0xFF | TYPE */
216 static const WCHAR table[9][0x32][25] = {
217 { /* TAG_TYPE_NULL */
218 {'I','N','C','L','U','D','E',0},
219 {'G','E','N','E','R','A','L',0},
220 {'M','A','T','C','H','_','L','O','G','I','C','_','N','O','T',0},
221 {'A','P','P','L','Y','_','A','L','L','_','S','H','I','M','S',0},
222 {'U','S','E','_','S','E','R','V','I','C','E','_','P','A','C','K','_','F','I','L','E','S',0},
223 {'M','I','T','I','G','A','T','I','O','N','_','O','S',0},
224 {'B','L','O','C','K','_','U','P','G','R','A','D','E',0},
225 {'I','N','C','L','U','D','E','E','X','C','L','U','D','E','D','L','L',0},
226 {'R','A','C','_','E','V','E','N','T','_','O','F','F',0},
227 {'T','E','L','E','M','E','T','R','Y','_','O','F','F',0},
228 {'S','H','I','M','_','E','N','G','I','N','E','_','O','F','F',0},
229 {'L','A','Y','E','R','_','P','R','O','P','A','G','A','T','I','O','N','_','O','F','F',0},
230 {'R','E','I','N','S','T','A','L','L','_','U','P','G','R','A','D','E',0}
231 },
232 { /* TAG_TYPE_BYTE */
233 {'I','n','v','a','l','i','d','T','a','g',0}
234 },
235 { /* TAG_TYPE_WORD */
236 {'M','A','T','C','H','_','M','O','D','E',0}
237 },
238 { /* TAG_TYPE_DWORD */
239 {'S','I','Z','E',0},
240 {'O','F','F','S','E','T',0},
241 {'C','H','E','C','K','S','U','M',0},
242 {'S','H','I','M','_','T','A','G','I','D',0},
243 {'P','A','T','C','H','_','T','A','G','I','D',0},
244 {'M','O','D','U','L','E','_','T','Y','P','E',0},
245 {'V','E','R','D','A','T','E','H','I',0},
246 {'V','E','R','D','A','T','E','L','O',0},
247 {'V','E','R','F','I','L','E','O','S',0},
248 {'V','E','R','F','I','L','E','T','Y','P','E',0},
249 {'P','E','_','C','H','E','C','K','S','U','M',0},
250 {'P','R','E','V','O','S','M','A','J','O','R','V','E','R',0},
251 {'P','R','E','V','O','S','M','I','N','O','R','V','E','R',0},
252 {'P','R','E','V','O','S','P','L','A','T','F','O','R','M','I','D',0},
253 {'P','R','E','V','O','S','B','U','I','L','D','N','O',0},
254 {'P','R','O','B','L','E','M','S','E','V','E','R','I','T','Y',0},
255 {'L','A','N','G','I','D',0},
256 {'V','E','R','_','L','A','N','G','U','A','G','E',0},
257 {'I','n','v','a','l','i','d','T','a','g',0},
258 {'E','N','G','I','N','E',0},
259 {'H','T','M','L','H','E','L','P','I','D',0},
260 {'I','N','D','E','X','_','F','L','A','G','S',0},
261 {'F','L','A','G','S',0},
262 {'D','A','T','A','_','V','A','L','U','E','T','Y','P','E',0},
263 {'D','A','T','A','_','D','W','O','R','D',0},
264 {'L','A','Y','E','R','_','T','A','G','I','D',0},
265 {'M','S','I','_','T','R','A','N','S','F','O','R','M','_','T','A','G','I','D',0},
266 {'L','I','N','K','E','R','_','V','E','R','S','I','O','N',0},
267 {'L','I','N','K','_','D','A','T','E',0},
268 {'U','P','T','O','_','L','I','N','K','_','D','A','T','E',0},
269 {'O','S','_','S','E','R','V','I','C','E','_','P','A','C','K',0},
270 {'F','L','A','G','_','T','A','G','I','D',0},
271 {'R','U','N','T','I','M','E','_','P','L','A','T','F','O','R','M',0},
272 {'O','S','_','S','K','U',0},
273 {'O','S','_','P','L','A','T','F','O','R','M',0},
274 {'A','P','P','_','N','A','M','E','_','R','C','_','I','D',0},
275 {'V','E','N','D','O','R','_','N','A','M','E','_','R','C','_','I','D',0},
276 {'S','U','M','M','A','R','Y','_','M','S','G','_','R','C','_','I','D',0},
277 {'V','I','S','T','A','_','S','K','U',0},
278 {'D','E','S','C','R','I','P','T','I','O','N','_','R','C','_','I','D',0},
279 {'P','A','R','A','M','E','T','E','R','1','_','R','C','_','I','D',0},
280 {'I','n','v','a','l','i','d','T','a','g',0},
281 {'I','n','v','a','l','i','d','T','a','g',0},
282 {'I','n','v','a','l','i','d','T','a','g',0},
283 {'I','n','v','a','l','i','d','T','a','g',0},
284 {'I','n','v','a','l','i','d','T','a','g',0},
285 {'I','n','v','a','l','i','d','T','a','g',0},
286 {'C','O','N','T','E','X','T','_','T','A','G','I','D',0},
287 {'E','X','E','_','W','R','A','P','P','E','R',0},
288 {'U','R','L','_','I','D',0}
289 },
290 { /* TAG_TYPE_QWORD */
291 {'T','I','M','E',0},
292 {'B','I','N','_','F','I','L','E','_','V','E','R','S','I','O','N',0},
293 {'B','I','N','_','P','R','O','D','U','C','T','_','V','E','R','S','I','O','N',0},
294 {'M','O','D','T','I','M','E',0},
295 {'F','L','A','G','_','M','A','S','K','_','K','E','R','N','E','L',0},
296 {'U','P','T','O','_','B','I','N','_','P','R','O','D','U','C','T','_','V','E','R','S','I','O','N',0},
297 {'D','A','T','A','_','Q','W','O','R','D',0},
298 {'F','L','A','G','_','M','A','S','K','_','U','S','E','R',0},
299 {'F','L','A','G','S','_','N','T','V','D','M','1',0},
300 {'F','L','A','G','S','_','N','T','V','D','M','2',0},
301 {'F','L','A','G','S','_','N','T','V','D','M','3',0},
302 {'F','L','A','G','_','M','A','S','K','_','S','H','E','L','L',0},
303 {'U','P','T','O','_','B','I','N','_','F','I','L','E','_','V','E','R','S','I','O','N',0},
304 {'F','L','A','G','_','M','A','S','K','_','F','U','S','I','O','N',0},
305 {'F','L','A','G','_','P','R','O','C','E','S','S','P','A','R','A','M',0},
306 {'F','L','A','G','_','L','U','A',0},
307 {'F','L','A','G','_','I','N','S','T','A','L','L',0}
308 },
309 { /* TAG_TYPE_STRINGREF */
310 {'N','A','M','E',0},
311 {'D','E','S','C','R','I','P','T','I','O','N',0},
312 {'M','O','D','U','L','E',0},
313 {'A','P','I',0},
314 {'V','E','N','D','O','R',0},
315 {'A','P','P','_','N','A','M','E',0},
316 {'I','n','v','a','l','i','d','T','a','g',0},
317 {'C','O','M','M','A','N','D','_','L','I','N','E',0},
318 {'C','O','M','P','A','N','Y','_','N','A','M','E',0},
319 {'D','L','L','F','I','L','E',0},
320 {'W','I','L','D','C','A','R','D','_','N','A','M','E',0},
321 {'I','n','v','a','l','i','d','T','a','g',0},
322 {'I','n','v','a','l','i','d','T','a','g',0},
323 {'I','n','v','a','l','i','d','T','a','g',0},
324 {'I','n','v','a','l','i','d','T','a','g',0},
325 {'P','R','O','D','U','C','T','_','N','A','M','E',0},
326 {'P','R','O','D','U','C','T','_','V','E','R','S','I','O','N',0},
327 {'F','I','L','E','_','D','E','S','C','R','I','P','T','I','O','N',0},
328 {'F','I','L','E','_','V','E','R','S','I','O','N',0},
329 {'O','R','I','G','I','N','A','L','_','F','I','L','E','N','A','M','E',0},
330 {'I','N','T','E','R','N','A','L','_','N','A','M','E',0},
331 {'L','E','G','A','L','_','C','O','P','Y','R','I','G','H','T',0},
332 {'1','6','B','I','T','_','D','E','S','C','R','I','P','T','I','O','N',0},
333 {'A','P','P','H','E','L','P','_','D','E','T','A','I','L','S',0},
334 {'L','I','N','K','_','U','R','L',0},
335 {'L','I','N','K','_','T','E','X','T',0},
336 {'A','P','P','H','E','L','P','_','T','I','T','L','E',0},
337 {'A','P','P','H','E','L','P','_','C','O','N','T','A','C','T',0},
338 {'S','X','S','_','M','A','N','I','F','E','S','T',0},
339 {'D','A','T','A','_','S','T','R','I','N','G',0},
340 {'M','S','I','_','T','R','A','N','S','F','O','R','M','_','F','I','L','E',0},
341 {'1','6','B','I','T','_','M','O','D','U','L','E','_','N','A','M','E',0},
342 {'L','A','Y','E','R','_','D','I','S','P','L','A','Y','N','A','M','E',0},
343 {'C','O','M','P','I','L','E','R','_','V','E','R','S','I','O','N',0},
344 {'A','C','T','I','O','N','_','T','Y','P','E',0},
345 {'E','X','P','O','R','T','_','N','A','M','E',0},
346 {'U','R','L',0}
347 },
348 { /* TAG_TYPE_LIST */
349 {'D','A','T','A','B','A','S','E',0},
350 {'L','I','B','R','A','R','Y',0},
351 {'I','N','E','X','C','L','U','D','E',0},
352 {'S','H','I','M',0},
353 {'P','A','T','C','H',0},
354 {'A','P','P',0},
355 {'E','X','E',0},
356 {'M','A','T','C','H','I','N','G','_','F','I','L','E',0},
357 {'S','H','I','M','_','R','E','F',0},
358 {'P','A','T','C','H','_','R','E','F',0},
359 {'L','A','Y','E','R',0},
360 {'F','I','L','E',0},
361 {'A','P','P','H','E','L','P',0},
362 {'L','I','N','K',0},
363 {'D','A','T','A',0},
364 {'M','S','I','_','T','R','A','N','S','F','O','R','M',0},
365 {'M','S','I','_','T','R','A','N','S','F','O','R','M','_','R','E','F',0},
366 {'M','S','I','_','P','A','C','K','A','G','E',0},
367 {'F','L','A','G',0},
368 {'M','S','I','_','C','U','S','T','O','M','_','A','C','T','I','O','N',0},
369 {'F','L','A','G','_','R','E','F',0},
370 {'A','C','T','I','O','N',0},
371 {'L','O','O','K','U','P',0},
372 {'C','O','N','T','E','X','T',0},
373 {'C','O','N','T','E','X','T','_','R','E','F',0},
374 {'I','n','v','a','l','i','d','T','a','g',0},
375 {'I','n','v','a','l','i','d','T','a','g',0},
376 {'I','n','v','a','l','i','d','T','a','g',0},
377 {'I','n','v','a','l','i','d','T','a','g',0},
378 {'I','n','v','a','l','i','d','T','a','g',0},
379 {'I','n','v','a','l','i','d','T','a','g',0},
380 {'S','P','C',0}
381 },
382 { /* TAG_TYPE_STRING */
383 {'I','n','v','a','l','i','d','T','a','g',0}
384 },
385 { /* TAG_TYPE_BINARY */
386 {'I','n','v','a','l','i','d','T','a','g',0},
387 {'P','A','T','C','H','_','B','I','T','S',0},
388 {'F','I','L','E','_','B','I','T','S',0},
389 {'E','X','E','_','I','D',0},
390 {'D','A','T','A','_','B','I','T','S',0},
391 {'M','S','I','_','P','A','C','K','A','G','E','_','I','D',0},
392 {'D','A','T','A','B','A','S','E','_','I','D',0},
393 {'C','O','N','T','E','X','T','_','P','L','A','T','F','O','R','M','_','I','D',0},
394 {'C','O','N','T','E','X','T','_','B','R','A','N','C','H','_','I','D',0},
395 {'I','n','v','a','l','i','d','T','a','g',0},
396 {'I','n','v','a','l','i','d','T','a','g',0},
397 {'I','n','v','a','l','i','d','T','a','g',0},
398 {'I','n','v','a','l','i','d','T','a','g',0},
399 {'I','n','v','a','l','i','d','T','a','g',0},
400 {'I','n','v','a','l','i','d','T','a','g',0},
401 {'F','I','X','_','I','D',0},
402 {'A','P','P','_','I','D',0}
403 }
404 };
405
406 /* sizes of tables in above array (# strings per type) */
407 static const WORD limits[9] = {
408 /* switch off TYPE_* nibble of last tag for each type */
409 TAG_REINSTALL_UPGRADE & 0xFF,
410 1,
411 TAG_MATCH_MODE & 0xFF,
412 TAG_URL_ID & 0xFF,
413 TAG_FLAG_INSTALL & 0xFF,
414 TAG_URL & 0xFF,
415 TAG_SPC & 0xFF,
416 1,
417 TAG_APP_ID & 0xFF
418 };
419
420 /* lookup tables for tags in range 0x800 + (0x1 -> 0xFF) | TYPE */
421 static const WCHAR table2[9][3][17] = {
422 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_NULL */
423 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_BYTE */
424 {
425 {'T','A','G',0}, /* TAG_TYPE_WORD */
426 {'I','N','D','E','X','_','T','A','G',0},
427 {'I','N','D','E','X','_','K','E','Y',0}
428 },
429 { {'T','A','G','I','D',0} }, /* TAG_TYPE_DWORD */
430 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_QWORD */
431 { {'I','n','v','a','l','i','d','T','a','g',0} }, /* TAG_TYPE_STRINGREF */
432 {
433 {'S','T','R','I','N','G','T','A','B','L','E',0}, /* TAG_TYPE_LIST */
434 {'I','N','D','E','X','E','S',0},
435 {'I','N','D','E','X',0}
436 },
437 { {'S','T','R','I','N','G','T','A','B','L','E','_','I','T','E','M',0}, }, /* TAG_TYPE_STRING */
438 { {'I','N','D','E','X','_','B','I','T','S',0} } /* TAG_TYPE_BINARY */
439 };
440
441 /* sizes of tables in above array, hardcoded for simplicity */
442 static const WORD limits2[9] = { 0, 0, 3, 1, 0, 0, 3, 1, 1 };
443
444 static const WCHAR null[] = {'N','U','L','L',0};
445 static const WCHAR invalid[] = {'I','n','v','a','l','i','d','T','a','g',0};
446
447 BOOL switch_table; /* should we use table2 and limits2? */
448 WORD index, type_index;
449
450 /* special case: null tag */
451 if (tag == TAG_NULL)
452 return null;
453
454 /* tags with only type mask or no type mask are invalid */
455 if ((tag & ~TAG_TYPE_MASK) == 0 || (tag & TAG_TYPE_MASK) == 0)
456 return invalid;
457
458 /* some valid tags are in range 0x800 + (0x1 -> 0xF) | TYPE */
459 if ((tag & 0xF00) == 0x800)
460 switch_table = TRUE;
461 else if ((tag & 0xF00) == 0)
462 switch_table = FALSE;
463 else return invalid;
464
465 /* index of table in array is type nibble */
466 type_index = (tag >> 12) - 1;
467
468 /* index of string in table is low byte */
469 index = (tag & 0xFF) - 1;
470
471 /* bound check */
472 if (type_index >= 9 || index >= (switch_table ? limits2[type_index] : limits[type_index]))
473 return invalid;
474
475 /* tag is valid */
476 return switch_table ? table2[type_index][index] : table[type_index][index];
477 }