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