d3dx8: Implement D3DX*Normalize.
[wine] / dlls / setupapi / setupx_main.c
1 /*
2  *      SETUPX library
3  *
4  *      Copyright 1998,2000  Andreas Mohr
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  * FIXME: Rather non-functional functions for now.
21  *
22  * See:
23  * http://www.geocities.com/SiliconValley/Network/5317/drivers.html
24  * http://willemer.de/informatik/windows/inf_info.htm (German)
25  * http://www.microsoft.com/ddk/ddkdocs/win98ddk/devinst_12uw.htm
26  * DDK: setupx.h
27  * http://mmatrix.tripod.com/customsystemfolder/infsysntaxfull.html
28  * http://www.rdrop.com/~cary/html/inf_faq.html
29  * http://support.microsoft.com/support/kb/articles/q194/6/40.asp
30  *
31  * Stuff tested with:
32  * - rs405deu.exe (German Acroread 4.05 setup)
33  * - ie5setup.exe
34  * - Netmeeting
35  *
36  * FIXME:
37  * - string handling is... weird ;) (buflen etc.)
38  * - memory leaks ?
39  * - separate that mess (but probably only when it's done completely)
40  *
41  * SETUPX consists of several parts with the following acronyms/prefixes:
42  * Di   device installer (devinst.c ?)
43  * Gen  generic installer (geninst.c ?)
44  * Ip   .INF parsing (infparse.c)
45  * LDD  logical device descriptor (ldd.c ?)
46  * LDID logical device ID
47  * SU   setup (setup.c ?)
48  * Tp   text processing (textproc.c ?)
49  * Vcp  virtual copy module (vcp.c ?)
50  * ...
51  *
52  * The SETUPX DLL is NOT thread-safe. That's why many installers urge you to
53  * "close all open applications".
54  * All in all the design of it seems to be a bit weak.
55  * Not sure whether my implementation of it is better, though ;-)
56  */
57
58 #include <stdlib.h>
59 #include <stdarg.h>
60 #include <stdio.h>
61 #include <string.h>
62 #include "windef.h"
63 #include "winbase.h"
64 #include "winreg.h"
65 #include "winerror.h"
66 #include "wownt32.h"
67 #include "winuser.h"
68 #include "winnls.h"
69 #include "setupapi.h"
70 #include "setupx16.h"
71 #include "setupapi_private.h"
72 #include "winerror.h"
73 #include "wine/debug.h"
74
75 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
76
77 #define HINSTANCE_32(h16) ((HINSTANCE)(ULONG_PTR)(h16))
78
79 /***********************************************************************
80  *              SURegOpenKey (SETUPX.47)
81  */
82 DWORD WINAPI SURegOpenKey( HKEY hkey, LPCSTR lpszSubKey, PHKEY retkey )
83 {
84     FIXME("(%p,%s,%p), semi-stub.\n",hkey,debugstr_a(lpszSubKey),retkey);
85     return RegOpenKeyA( hkey, lpszSubKey, retkey );
86 }
87
88 /***********************************************************************
89  *              SURegQueryValueEx (SETUPX.50)
90  */
91 DWORD WINAPI SURegQueryValueEx( HKEY hkey, LPSTR lpszValueName,
92                                 LPDWORD lpdwReserved, LPDWORD lpdwType,
93                                 LPBYTE lpbData, LPDWORD lpcbData )
94 {
95     FIXME("(%p,%s,%p,%p,%p,%d), semi-stub.\n",hkey,debugstr_a(lpszValueName),
96           lpdwReserved,lpdwType,lpbData,lpcbData?*lpcbData:0);
97     return RegQueryValueExA( hkey, lpszValueName, lpdwReserved, lpdwType,
98                                lpbData, lpcbData );
99 }
100
101
102 /***********************************************************************
103  *              InstallHinfSection (SETUPX.527)
104  *
105  * hwnd = parent window
106  * hinst = instance of SETUPX.DLL
107  * lpszCmdLine = e.g. "DefaultInstall 132 C:\MYINSTALL\MYDEV.INF"
108  * Here "DefaultInstall" is the .inf file section to be installed (optional).
109  * The 132 value is made of the HOW_xxx flags and sometimes 128 (-> setupx16.h).
110  *
111  * nCmdShow = nCmdShow of CreateProcess
112  */
113 RETERR16 WINAPI InstallHinfSection16( HWND16 hwnd, HINSTANCE16 hinst, LPCSTR lpszCmdLine, INT16 nCmdShow)
114 {
115     InstallHinfSectionA( HWND_32(hwnd), HINSTANCE_32(hinst), lpszCmdLine, nCmdShow );
116     return OK;
117 }
118
119 typedef struct
120 {
121     LPCSTR RegValName;
122     LPCSTR StdString; /* fallback string; sub dir of windows directory */
123 } LDID_DATA;
124
125 static const LDID_DATA LDID_Data[34] =
126 {
127     { /* 0 (LDID_NULL) -- not defined */
128         NULL,
129         NULL
130     },
131     { /* 1 (LDID_SRCPATH) = source of installation. hmm, what to do here ? */
132         "SourcePath", /* hmm, does SETUPX have to care about updating it ?? */
133         NULL
134     },
135     { /* 2 (LDID_SETUPTEMP) = setup temp dir */
136         "SetupTempDir",
137         NULL
138     },
139     { /* 3 (LDID_UNINSTALL) = uninstall backup dir */
140         "UninstallDir",
141         NULL
142     },
143     { /* 4 (LDID_BACKUP) = backup dir */
144         "BackupDir",
145         NULL
146     },
147     { /* 5 (LDID_SETUPSCRATCH) = setup scratch dir */
148         "SetupScratchDir",
149         NULL
150     },
151     { /* 6 -- not defined */
152         NULL,
153         NULL
154     },
155     { /* 7 -- not defined */
156         NULL,
157         NULL
158     },
159     { /* 8 -- not defined */
160         NULL,
161         NULL
162     },
163     { /* 9 -- not defined */
164         NULL,
165         NULL
166     },
167     { /* 10 (LDID_WIN) = windows dir */
168         "WinDir",
169         ""
170     },
171     { /* 11 (LDID_SYS) = system dir */
172         "SysDir",
173         NULL /* call GetSystemDirectory() instead */
174     },
175     { /* 12 (LDID_IOS) = IOSubSys dir */
176         NULL, /* FIXME: registry string ? */
177         "SYSTEM\\IOSUBSYS"
178     },
179     { /* 13 (LDID_CMD) = COMMAND dir */
180         NULL, /* FIXME: registry string ? */
181         "COMMAND"
182     },
183     { /* 14 (LDID_CPL) = control panel dir */
184         NULL,
185         ""
186     },
187     { /* 15 (LDID_PRINT) = windows printer dir */
188         NULL,
189         "SYSTEM" /* correct ?? */
190     },
191     { /* 16 (LDID_MAIL) = destination mail dir */
192         NULL,
193         ""
194     },
195     { /* 17 (LDID_INF) = INF dir */
196         "SetupScratchDir", /* correct ? */
197         "INF"
198     },
199     { /* 18 (LDID_HELP) = HELP dir */
200         NULL, /* ??? */
201         "HELP"
202     },
203     { /* 19 (LDID_WINADMIN) = Admin dir */
204         "WinAdminDir",
205         ""
206     },
207     { /* 20 (LDID_FONTS) = Fonts dir */
208         NULL, /* ??? */
209         "FONTS"
210     },
211     { /* 21 (LDID_VIEWERS) = Viewers */
212         NULL, /* ??? */
213         "SYSTEM\\VIEWERS"
214     },
215     { /* 22 (LDID_VMM32) = VMM32 dir */
216         NULL, /* ??? */
217         "SYSTEM\\VMM32"
218     },
219     { /* 23 (LDID_COLOR) = ICM dir */
220         "ICMPath",
221         "SYSTEM\\COLOR"
222     },
223     { /* 24 (LDID_APPS) = root of boot drive ? */
224         "AppsDir",
225         "C:\\"
226     },
227     { /* 25 (LDID_SHARED) = shared dir */
228         "SharedDir",
229         ""
230     },
231     { /* 26 (LDID_WINBOOT) = Windows boot dir */
232         "WinBootDir",
233         ""
234     },
235     { /* 27 (LDID_MACHINE) = machine specific files */
236         "MachineDir",
237         NULL
238     },
239     { /* 28 (LDID_HOST_WINBOOT) = Host Windows boot dir */
240         "HostWinBootDir",
241         NULL
242     },
243     { /* 29 -- not defined */
244         NULL,
245         NULL
246     },
247     { /* 30 (LDID_BOOT) = Root of boot drive */
248         "BootDir",
249         NULL
250     },
251     { /* 31 (LDID_BOOT_HOST) = Root of boot drive host */
252         "BootHost",
253         NULL
254     },
255     { /* 32 (LDID_OLD_WINBOOT) = subdir of root */
256         "OldWinBootDir",
257         NULL
258     },
259     { /* 33 (LDID_OLD_WIN) = old win dir */
260         "OldWinDir",
261         NULL
262     }
263     /* the rest (34-38) isn't too interesting, so I'll forget about it */
264 };
265
266 /*
267  * LDD  == Logical Device Descriptor
268  * LDID == Logical Device ID
269  *
270  * The whole LDD/LDID business might go into a separate file named
271  * ldd.c.
272  * At the moment I don't know what the hell these functions are really doing.
273  * That's why I added reporting stubs.
274  * The only thing I do know is that I need them for the LDD/LDID infrastructure.
275  * That's why I implemented them in a way that's suitable for my purpose.
276  */
277 static LDD_LIST *pFirstLDD = NULL;
278
279 static BOOL std_LDDs_done = FALSE;
280
281 static void SETUPX_CreateStandardLDDs(void)
282 {
283     HKEY hKey = 0;
284     WORD n;
285     DWORD type, len;
286     LOGDISKDESC_S ldd;
287     char buffer[MAX_PATH];
288
289     /* has to be here, otherwise loop */
290     std_LDDs_done = TRUE;
291
292     RegOpenKeyA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup", &hKey);
293
294     for (n=0; n < sizeof(LDID_Data)/sizeof(LDID_DATA); n++)
295     {
296         buffer[0] = '\0';
297
298         len = MAX_PATH;
299         if ( (hKey) && (LDID_Data[n].RegValName)
300         &&   (RegQueryValueExA(hKey, LDID_Data[n].RegValName,
301                 NULL, &type, (LPBYTE)buffer, &len) == ERROR_SUCCESS)
302         &&   (type == REG_SZ) )
303         {
304             TRACE("found value '%s' for LDID %d\n", buffer, n);
305         }
306         else
307         switch(n)
308         {
309             case LDID_SRCPATH:
310                 FIXME("LDID_SRCPATH: what exactly do we have to do here ?\n");
311                 strcpy(buffer, "X:\\FIXME");
312                 break;
313             case LDID_SYS:
314                 GetSystemDirectoryA(buffer, MAX_PATH);
315                 break;
316             case LDID_APPS:
317             case LDID_MACHINE:
318             case LDID_HOST_WINBOOT:
319             case LDID_BOOT:
320             case LDID_BOOT_HOST:
321                 strcpy(buffer, "C:\\");
322                 break;
323             default:
324                 if (LDID_Data[n].StdString)
325                 {
326                     DWORD len = GetWindowsDirectoryA(buffer, MAX_PATH);
327                     LPSTR p;
328                     p = buffer + len;
329                     *p++ = '\\';
330                     strcpy(p, LDID_Data[n].StdString);
331                 }
332                 break;
333         }
334         if (buffer[0])
335         {
336             INIT_LDD(ldd, n);
337             ldd.pszPath = buffer;
338             TRACE("LDID %d -> '%s'\n", ldd.ldid, ldd.pszPath);
339             CtlSetLdd16(&ldd);
340         }
341     }
342     if (hKey) RegCloseKey(hKey);
343 }
344
345 /***********************************************************************
346  * CtlDelLdd            (SETUPX.37)
347  *
348  * RETURN
349  *   ERR_VCP_LDDINVALID if ldid < LDID_ASSIGN_START.
350  */
351 static RETERR16 SETUPX_DelLdd(LOGDISKID16 ldid)
352 {
353     LDD_LIST *pCurr, *pPrev = NULL;
354
355     TRACE("(%d)\n", ldid);
356
357     if (!std_LDDs_done)
358         SETUPX_CreateStandardLDDs();
359
360     if (ldid < LDID_ASSIGN_START)
361         return ERR_VCP_LDDINVALID;
362
363     pCurr = pFirstLDD;
364     /* search until we find the appropriate LDD or hit the end */
365     while ((pCurr != NULL) && (ldid > pCurr->pldd->ldid))
366     {
367          pPrev = pCurr;
368          pCurr = pCurr->next;
369     }
370     if ( (pCurr == NULL) /* hit end of list */
371       || (ldid != pCurr->pldd->ldid) )
372         return ERR_VCP_LDDFIND; /* correct ? */
373
374     /* ok, found our victim: eliminate it */
375
376     if (pPrev)
377         pPrev->next = pCurr->next;
378
379     if (pCurr == pFirstLDD)
380         pFirstLDD = NULL;
381     HeapFree(GetProcessHeap(), 0, pCurr);
382
383     return OK;
384 }
385
386 /***********************************************************************
387  *              CtlDelLdd (SETUPX.37)
388  */
389 RETERR16 WINAPI CtlDelLdd16(LOGDISKID16 ldid)
390 {
391     FIXME("(%d); - please report this!\n", ldid);
392     return SETUPX_DelLdd(ldid);
393 }
394
395 /***********************************************************************
396  * CtlFindLdd           (SETUPX.35)
397  *
398  * doesn't check pldd ptr validity: crash (W98SE)
399  *
400  * RETURN
401  *   ERR_VCP_LDDINVALID if pldd->cbSize != structsize
402  *   1 in all other cases ??
403  *
404  */
405 RETERR16 WINAPI CtlFindLdd16(LPLOGDISKDESC pldd)
406 {
407     LDD_LIST *pCurr, *pPrev = NULL;
408
409     TRACE("(%p)\n", pldd);
410
411     if (!std_LDDs_done)
412         SETUPX_CreateStandardLDDs();
413
414     if (pldd->cbSize != sizeof(LOGDISKDESC_S))
415         return ERR_VCP_LDDINVALID;
416
417     pCurr = pFirstLDD;
418     /* search until we find the appropriate LDD or hit the end */
419     while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
420     {
421         pPrev = pCurr;
422         pCurr = pCurr->next;
423     }
424     if ( (pCurr == NULL) /* hit end of list */
425       || (pldd->ldid != pCurr->pldd->ldid) )
426         return ERR_VCP_LDDFIND; /* correct ? */
427
428     memcpy(pldd, pCurr->pldd, pldd->cbSize);
429     /* hmm, we probably ought to strcpy() the string ptrs here */
430
431     return 1; /* what is this ?? */
432 }
433
434 /***********************************************************************
435  * CtlSetLdd                    (SETUPX.33)
436  *
437  * Set an LDD entry.
438  *
439  * RETURN
440  *   ERR_VCP_LDDINVALID if pldd.cbSize != sizeof(LOGDISKDESC_S)
441  *
442  */
443 RETERR16 WINAPI CtlSetLdd16(LPLOGDISKDESC pldd)
444 {
445     LDD_LIST *pCurr, *pPrev = NULL;
446     LPLOGDISKDESC pCurrLDD;
447     HANDLE heap;
448     BOOL is_new = FALSE;
449
450     TRACE("(%p)\n", pldd);
451
452     if (!std_LDDs_done)
453         SETUPX_CreateStandardLDDs();
454
455     if (pldd->cbSize != sizeof(LOGDISKDESC_S))
456         return ERR_VCP_LDDINVALID;
457
458     heap = GetProcessHeap();
459     pCurr = pFirstLDD;
460     /* search until we find the appropriate LDD or hit the end */
461     while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
462     {
463          pPrev = pCurr;
464          pCurr = pCurr->next;
465     }
466     if (!pCurr || pldd->ldid != pCurr->pldd->ldid)
467     {
468         is_new = TRUE;
469         pCurr = HeapAlloc(heap, 0, sizeof(LDD_LIST));
470         pCurr->pldd = HeapAlloc(heap, 0, sizeof(LOGDISKDESC_S));
471         pCurr->next = NULL;
472         pCurrLDD = pCurr->pldd;
473     }
474     else
475     {
476         pCurrLDD = pCurr->pldd;
477         HeapFree(heap, 0, pCurrLDD->pszPath);
478         HeapFree(heap, 0, pCurrLDD->pszVolLabel);
479         HeapFree(heap, 0, pCurrLDD->pszDiskName);
480     }
481
482     memcpy(pCurrLDD, pldd, sizeof(LOGDISKDESC_S));
483
484     if (pldd->pszPath)
485     {
486         pCurrLDD->pszPath = HeapAlloc( heap, 0, strlen(pldd->pszPath)+1 );
487         strcpy( pCurrLDD->pszPath, pldd->pszPath );
488     }
489     if (pldd->pszVolLabel)
490     {
491         pCurrLDD->pszVolLabel = HeapAlloc( heap, 0, strlen(pldd->pszVolLabel)+1 );
492         strcpy( pCurrLDD->pszVolLabel, pldd->pszVolLabel );
493     }
494     if (pldd->pszDiskName)
495     {
496         pCurrLDD->pszDiskName = HeapAlloc( heap, 0, strlen(pldd->pszDiskName)+1 );
497         strcpy( pCurrLDD->pszDiskName, pldd->pszDiskName );
498     }
499
500     if (is_new) /* link into list */
501     {
502         if (pPrev)
503         {
504             pCurr->next = pPrev->next;
505             pPrev->next = pCurr;
506         }
507         if (!pFirstLDD)
508             pFirstLDD = pCurr;
509     }
510
511     return OK;
512 }
513
514
515 /***********************************************************************
516  * CtlAddLdd            (SETUPX.36)
517  *
518  * doesn't check pldd ptr validity: crash (W98SE)
519  *
520  */
521 static LOGDISKID16 ldid_to_add = LDID_ASSIGN_START;
522 RETERR16 WINAPI CtlAddLdd16(LPLOGDISKDESC pldd)
523 {
524     pldd->ldid = ldid_to_add++;
525     return CtlSetLdd16(pldd);
526 }
527
528 /***********************************************************************
529  * CtlGetLdd            (SETUPX.34)
530  *
531  * doesn't check pldd ptr validity: crash (W98SE)
532  * What the !@#$%&*( is the difference between CtlFindLdd() and CtlGetLdd() ??
533  *
534  * RETURN
535  *   ERR_VCP_LDDINVALID if pldd->cbSize != structsize
536  *
537  */
538 static RETERR16 SETUPX_GetLdd(LPLOGDISKDESC pldd)
539 {
540     LDD_LIST *pCurr, *pPrev = NULL;
541
542     if (!std_LDDs_done)
543         SETUPX_CreateStandardLDDs();
544
545     if (pldd->cbSize != sizeof(LOGDISKDESC_S))
546         return ERR_VCP_LDDINVALID;
547
548     pCurr = pFirstLDD;
549     /* search until we find the appropriate LDD or hit the end */
550     while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
551     {
552          pPrev = pCurr;
553          pCurr = pCurr->next;
554     }
555     if (pCurr == NULL) /* hit end of list */
556         return ERR_VCP_LDDFIND; /* correct ? */
557
558     memcpy(pldd, pCurr->pldd, pldd->cbSize);
559     /* hmm, we probably ought to strcpy() the string ptrs here */
560
561     return OK;
562 }
563
564 /**********************************************************************/
565
566 RETERR16 WINAPI CtlGetLdd16(LPLOGDISKDESC pldd)
567 {
568     FIXME("(%p); - please report this!\n", pldd);
569     return SETUPX_GetLdd(pldd);
570 }
571
572 /***********************************************************************
573  *              CtlGetLddPath           (SETUPX.38)
574  *
575  * Gets the path of an LDD.
576  * No crash if szPath == NULL.
577  * szPath has to be at least MAX_PATH_LEN bytes long.
578  * RETURN
579  *   ERR_VCP_LDDUNINIT if LDD for LDID not found.
580  */
581 RETERR16 WINAPI CtlGetLddPath16(LOGDISKID16 ldid, LPSTR szPath)
582 {
583     TRACE("(%d, %p);\n", ldid, szPath);
584
585     if (szPath)
586     {
587         LOGDISKDESC_S ldd;
588         INIT_LDD(ldd, ldid);
589         if (CtlFindLdd16(&ldd) == ERR_VCP_LDDFIND)
590             return ERR_VCP_LDDUNINIT;
591         SETUPX_GetLdd(&ldd);
592         strcpy(szPath, ldd.pszPath);
593         TRACE("ret '%s' for LDID %d\n", szPath, ldid);
594     }
595     return OK;
596 }
597
598 /***********************************************************************
599  *              CtlSetLddPath           (SETUPX.508)
600  *
601  * Sets the path of an LDD.
602  * Creates LDD for LDID if not existing yet.
603  */
604 RETERR16 WINAPI CtlSetLddPath16(LOGDISKID16 ldid, LPSTR szPath)
605 {
606     LOGDISKDESC_S ldd;
607     TRACE("(%d, '%s');\n", ldid, szPath);
608
609     SetupSetDirectoryIdA( 0, ldid, szPath );
610     INIT_LDD(ldd, ldid);
611     ldd.pszPath = szPath;
612     return CtlSetLdd16(&ldd);
613 }