- implemented passive FTP transfers (PASV, needed for firewalls)
[wine] / dlls / setupapi / setupx_main.c
1 /*
2  *      SETUPX library
3  *
4  *      Copyright 1998,2000  Andreas Mohr
5  *
6  * FIXME: Rather non-functional functions for now.
7  *
8  * See:
9  * http://www.geocities.com/SiliconValley/Network/5317/drivers.html
10  * http://willemer.de/informatik/windows/inf_info.htm (German)
11  * http://www.microsoft.com/ddk/ddkdocs/win98ddk/devinst_12uw.htm
12  * DDK: setupx.h
13  * http://mmatrix.tripod.com/customsystemfolder/infsysntaxfull.html
14  * http://www.rdrop.com/~cary/html/inf_faq.html
15  * http://support.microsoft.com/support/kb/articles/q194/6/40.asp
16  *
17  * Stuff tested with:
18  * - rs405deu.exe (German Acroread 4.05 setup)
19  * - ie5setup.exe
20  * - Netmeeting
21  *
22  * FIXME:
23  * - string handling is... weird ;) (buflen etc.)
24  * - memory leaks ?
25  * - separate that mess (but probably only when it's done completely)
26  *
27  * SETUPX consists of several parts with the following acronyms/prefixes:
28  * Di   device installer (devinst.c ?)
29  * Gen  generic installer (geninst.c ?)
30  * Ip   .INF parsing (infparse.c)
31  * LDD  logical device descriptor (ldd.c ?)
32  * LDID logical device ID 
33  * SU   setup (setup.c ?)
34  * Tp   text processing (textproc.c ?)
35  * Vcp  virtual copy module (vcp.c ?)
36  * ...
37  *
38  * The SETUPX DLL is NOT thread-safe. That's why many installers urge you to
39  * "close all open applications".
40  * All in all the design of it seems to be a bit weak.
41  * Not sure whether my implementation of it is better, though ;-)
42  */
43
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include "winreg.h"
47 #include "wine/winuser16.h"
48 #include "setupx16.h"
49 #include "setupx_private.h"
50 #include "winerror.h"
51 #include "heap.h"
52 #include "debugtools.h"
53
54 DEFAULT_DEBUG_CHANNEL(setupx);
55
56 /***********************************************************************
57  *              SURegOpenKey
58  */
59 DWORD WINAPI SURegOpenKey( HKEY hkey, LPCSTR lpszSubKey, LPHKEY retkey )
60 {
61     FIXME("(%x,%s,%p), semi-stub.\n",hkey,debugstr_a(lpszSubKey),retkey);
62     return RegOpenKeyA( hkey, lpszSubKey, retkey );
63 }
64
65 /***********************************************************************
66  *              SURegQueryValueEx
67  */
68 DWORD WINAPI SURegQueryValueEx( HKEY hkey, LPSTR lpszValueName,
69                                 LPDWORD lpdwReserved, LPDWORD lpdwType,
70                                 LPBYTE lpbData, LPDWORD lpcbData )
71 {
72     FIXME("(%x,%s,%p,%p,%p,%ld), semi-stub.\n",hkey,debugstr_a(lpszValueName),
73           lpdwReserved,lpdwType,lpbData,lpcbData?*lpcbData:0);
74     return RegQueryValueExA( hkey, lpszValueName, lpdwReserved, lpdwType,
75                                lpbData, lpcbData );
76 }
77
78 /*
79  * Returns pointer to a string list with the first entry being number
80  * of strings.
81  *
82  * Hmm. Should this be InitSubstrData(), GetFirstSubstr() and GetNextSubstr()
83  * instead?
84  */
85 static LPSTR *SETUPX_GetSubStrings(LPSTR start, char delimiter)
86 {
87     LPSTR p, q;
88     LPSTR *res = NULL;
89     DWORD count = 0;
90     int len;
91
92     p = start;
93
94     while (1)
95     {
96         /* find beginning of real substring */
97         while ( (*p == ' ') || (*p == '\t') || (*p == '"') ) p++;
98
99         /* find end of real substring */
100         q = p;
101         while ( (*q)
102              && (*q != ' ') && (*q != '\t') && (*q != '"')
103              && (*q != ';') && (*q != delimiter) ) q++;
104         if (q == p)
105             break;
106         len = (int)q - (int)p;
107
108         /* alloc entry for new substring in steps of 32 units and copy over */
109         if (count % 32 == 0)
110         { /* 1 for count field + current count + 32 */
111             res = HeapReAlloc(GetProcessHeap(), 0, res, (1+count+32)*sizeof(LPSTR));
112         }
113         *(res+1+count) = HeapAlloc(GetProcessHeap(), 0, len+1);
114         strncpy(*(res+1+count), p, len);
115         (*(res+1+count))[len] = '\0';
116         count++;
117
118         /* we are still within last substring (before delimiter),
119          * so get out of it */
120         while ((*q) && (*q != ';') && (*q != delimiter)) q++;
121         if ((!*q) || (*q == ';'))
122             break;
123         p = q+1;
124     }
125     
126     /* put number of entries at beginning of list */
127     *(DWORD *)res = count;
128     return res;
129 }
130
131 static void SETUPX_FreeSubStrings(LPSTR *substr)
132 {
133     DWORD count = *(DWORD *)substr;
134     LPSTR *pStrings = substr+1;
135     DWORD n;
136
137     for (n=0; n < count; n++)
138         HeapFree(GetProcessHeap(), 0, *pStrings++);
139
140     HeapFree(GetProcessHeap(), 0, substr);
141 }
142
143 static void SETUPX_IsolateSubString(LPSTR *begin, LPSTR *end)
144 {
145     LPSTR p, q;
146
147     p = *begin;
148     q = *end;
149
150     while ((p < q) && ((*p == ' ') || (*p == '\t'))) p++;
151     while ((p < q) && (*p == '"')) p++;
152
153     while ((q-1 >= p) && ((*(q-1) == ' ') || (*(q-1) == '\t'))) q--;
154     while ((q-1 >= p) && (*(q-1) == '"')) q--;
155
156     *begin = p;
157     *end = q;
158 }
159
160 /*
161  * Example: HKLM,"Software\Microsoft\Windows\CurrentVersion","ProgramFilesDir",,"C:\"
162  * FIXME: use SETUPX_GetSubStrings() instead.
163  *        Hmm, but on the other hand SETUPX_GetSubStrings() will probably
164  *        soon be replaced by InitSubstrData() etc. anyway.
165  * 
166  */
167 static BOOL SETUPX_LookupRegistryString(LPSTR regstr, LPSTR buffer, DWORD buflen)
168 {
169     HANDLE heap = GetProcessHeap();
170     LPSTR items[5];
171     LPSTR p, q, next;
172     int len, n;
173     HKEY hkey, hsubkey;
174     DWORD dwType;
175
176     TRACE("retrieving '%s'\n", regstr);
177
178     p = regstr;
179
180     /* isolate root key, subkey, value, flag, defval */
181     for (n=0; n < 5; n++)
182     {
183         q = strchr(p, ',');
184         if (!q)
185         {
186             if (n == 4)
187                 q = p+strlen(p);
188             else
189                 return FALSE;
190         }
191         next = q+1;
192         if (q < regstr)
193             return FALSE;
194         SETUPX_IsolateSubString(&p, &q);
195         len = (int)q - (int)p;
196         items[n] = HeapAlloc(heap, 0, len+1);
197         strncpy(items[n], p, len);
198         items[n][len] = '\0';
199         p = next;
200     }
201     TRACE("got '%s','%s','%s','%s','%s'\n",
202                         items[0], items[1], items[2], items[3], items[4]);
203     
204     /* check root key */
205     if (!strcasecmp(items[0], "HKCR"))
206         hkey = HKEY_CLASSES_ROOT;
207     else
208     if (!strcasecmp(items[0], "HKCU"))
209         hkey = HKEY_CURRENT_USER;
210     else
211     if (!strcasecmp(items[0], "HKLM"))
212         hkey = HKEY_LOCAL_MACHINE;
213     else
214     if (!strcasecmp(items[0], "HKU"))
215         hkey = HKEY_USERS;
216     else
217     { /* HKR ? -> relative to key passed to GenInstallEx */
218         FIXME("unsupported regkey '%s'\n", items[0]);
219         goto regfailed;
220     }
221
222     if (RegOpenKeyA(hkey, items[1], &hsubkey) != ERROR_SUCCESS)
223         goto regfailed;
224
225     if (RegQueryValueExA(hsubkey, items[2], NULL, &dwType, buffer, &buflen)
226                 != ERROR_SUCCESS)
227         goto regfailed;
228     goto done;
229
230 regfailed:
231     if (buffer) strcpy(buffer, items[4]); /* I don't care about buflen */
232 done:
233     for (n=0; n < 5; n++)
234         HeapFree(heap, 0, items[n]);
235     if (buffer)
236         TRACE("return '%s'\n", buffer);
237     return TRUE;
238 }
239
240 static LPSTR SETUPX_GetSections(LPCSTR filename)
241 {
242     LPSTR buf = NULL;
243     DWORD len = 1024, res;
244
245     do {
246         buf = HeapReAlloc(GetProcessHeap(), 0, buf, len);
247         res = GetPrivateProfileStringA(NULL, NULL, NULL, buf, len, filename);
248         len *= 2;
249     } while ((!res) && (len < 1048576));
250     if (!res)
251     {
252         HeapFree(GetProcessHeap(), 0, buf);
253         return NULL;
254     }
255     return buf;
256 }
257
258 static LPSTR SETUPX_GetSectionEntries(LPCSTR filename, LPCSTR section)
259 {
260     LPSTR buf = NULL;
261     DWORD len = 1024, res;
262
263     do {
264         buf = HeapReAlloc(GetProcessHeap(), 0, buf, len);
265         res = GetPrivateProfileSectionA(section, buf, len, filename);
266         len *= 2;
267     } while ((!res) && (len < 1048576));
268     if (!res)
269     {
270         HeapFree(GetProcessHeap(), 0, buf);
271         return NULL;
272     }
273     return buf;
274 }
275
276
277 /***********************************************************************
278  *              InstallHinfSection
279  *
280  * hwnd = parent window
281  * hinst = instance of SETUPX.DLL
282  * lpszCmdLine = e.g. "DefaultInstall 132 C:\MYINSTALL\MYDEV.INF"
283  * Here "DefaultInstall" is the .inf file section to be installed (optional).
284  * The 132 value is made of the HOW_xxx flags and sometimes 128 (-> setupx16.h).
285  * 
286  * nCmdShow = nCmdShow of CreateProcess
287  */
288 typedef INT WINAPI (*MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
289 RETERR16 WINAPI InstallHinfSection16( HWND16 hwnd, HINSTANCE16 hinst, LPCSTR lpszCmdLine, INT16 nCmdShow)
290 {
291     LPSTR *pSub;
292     DWORD count;
293     HINF16 hInf = 0;
294     RETERR16 res = OK;
295     WORD wFlags;
296     BOOL reboot = FALSE;
297     HMODULE hMod;
298     MSGBOX_PROC pMessageBoxA;
299     
300     TRACE("(%04x, %04x, %s, %d);\n", hwnd, hinst, lpszCmdLine, nCmdShow);
301     
302     pSub = SETUPX_GetSubStrings((LPSTR)lpszCmdLine, ' ');
303
304     count = *(DWORD *)pSub;
305     if (count < 2) /* invalid number of arguments ? */
306         goto end;
307     if (IpOpen16(*(pSub+count), &hInf) != OK)
308     {
309         res = ERROR_FILE_NOT_FOUND; /* yes, correct */
310         goto end;
311     }
312     if (GenInstall16(hInf, *(pSub+count-2), GENINSTALL_DO_ALL) != OK)
313         goto end;
314     wFlags = atoi(*(pSub+count-1)) & ~128;
315     switch (wFlags)
316     {
317         case HOW_ALWAYS_SILENT_REBOOT:
318         case HOW_SILENT_REBOOT:
319             reboot = TRUE;
320             break;
321         case HOW_ALWAYS_PROMPT_REBOOT:
322         case HOW_PROMPT_REBOOT:
323             if ((hMod = GetModuleHandleA("user32.dll")))
324             {
325               if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( hMod, "MessageBoxA" )))
326               {
327                  
328                 if (pMessageBoxA(hwnd, "You must restart Wine before the new settings will take effect.\n\nDo you want to exit Wine now ?", "Systems Settings Change", MB_YESNO|MB_ICONQUESTION) == IDYES)
329                   reboot = TRUE;
330               }
331             }
332             break;
333         default:
334             ERR("invalid flags %d !\n", wFlags);
335             goto end;
336     }
337     
338     res = OK;
339 end:
340     IpClose16(hInf);
341     SETUPX_FreeSubStrings(pSub);
342     if (reboot)
343     {
344         /* FIXME: we should have a means of terminating all wine + wineserver */
345         MESSAGE("Program or user told me to restart. Exiting Wine...\n");
346         ExitProcess(1);
347     }
348
349     return res;
350 }
351
352 typedef struct
353 {
354     LPCSTR RegValName;
355     LPCSTR StdString; /* fallback string; sub dir of windows directory */
356 } LDID_DATA;
357
358 static const LDID_DATA LDID_Data[34] =
359 {
360     { /* 0 (LDID_NULL) -- not defined */
361         NULL,
362         NULL
363     },
364     { /* 1 (LDID_SRCPATH) = source of installation. hmm, what to do here ? */
365         "SourcePath", /* hmm, does SETUPX have to care about updating it ?? */
366         NULL
367     },
368     { /* 2 (LDID_SETUPTEMP) = setup temp dir */
369         "SetupTempDir",
370         NULL
371     },
372     { /* 3 (LDID_UNINSTALL) = uninstall backup dir */
373         "UninstallDir",
374         NULL
375     },
376     { /* 4 (LDID_BACKUP) = backup dir */
377         "BackupDir",
378         NULL
379     },
380     { /* 5 (LDID_SETUPSCRATCH) = setup scratch dir */
381         "SetupScratchDir",
382         NULL
383     },
384     { /* 6 -- not defined */
385         NULL,
386         NULL
387     },
388     { /* 7 -- not defined */
389         NULL,
390         NULL
391     },
392     { /* 8 -- not defined */
393         NULL,
394         NULL
395     },
396     { /* 9 -- not defined */
397         NULL,
398         NULL
399     },
400     { /* 10 (LDID_WIN) = windows dir */
401         "WinDir",
402         ""
403     },
404     { /* 11 (LDID_SYS) = system dir */
405         "SysDir",
406         NULL /* call GetSystemDirectory() instead */
407     },
408     { /* 12 (LDID_IOS) = IOSubSys dir */
409         NULL, /* FIXME: registry string ? */
410         "SYSTEM\\IOSUBSYS"
411     },
412     { /* 13 (LDID_CMD) = COMMAND dir */
413         NULL, /* FIXME: registry string ? */
414         "COMMAND"
415     },
416     { /* 14 (LDID_CPL) = control panel dir */
417         NULL,
418         ""
419     },
420     { /* 15 (LDID_PRINT) = windows printer dir */
421         NULL,
422         "SYSTEM" /* correct ?? */
423     },
424     { /* 16 (LDID_MAIL) = destination mail dir */
425         NULL,
426         ""
427     },
428     { /* 17 (LDID_INF) = INF dir */
429         "SetupScratchDir", /* correct ? */
430         "INF"
431     },
432     { /* 18 (LDID_HELP) = HELP dir */
433         NULL, /* ??? */
434         "HELP"
435     },
436     { /* 19 (LDID_WINADMIN) = Admin dir */
437         "WinAdminDir",
438         ""
439     },
440     { /* 20 (LDID_FONTS) = Fonts dir */
441         NULL, /* ??? */
442         "FONTS"
443     },
444     { /* 21 (LDID_VIEWERS) = Viewers */
445         NULL, /* ??? */
446         "SYSTEM\\VIEWERS"
447     },
448     { /* 22 (LDID_VMM32) = VMM32 dir */
449         NULL, /* ??? */
450         "SYSTEM\\VMM32"
451     },
452     { /* 23 (LDID_COLOR) = ICM dir */
453         "ICMPath",
454         "SYSTEM\\COLOR"
455     },
456     { /* 24 (LDID_APPS) = root of boot drive ? */
457         "AppsDir",
458         "C:\\"
459     },
460     { /* 25 (LDID_SHARED) = shared dir */
461         "SharedDir",
462         ""
463     },
464     { /* 26 (LDID_WINBOOT) = Windows boot dir */
465         "WinBootDir",
466         ""
467     },
468     { /* 27 (LDID_MACHINE) = machine specific files */
469         "MachineDir",
470         NULL
471     },
472     { /* 28 (LDID_HOST_WINBOOT) = Host Windows boot dir */
473         "HostWinBootDir",
474         NULL
475     },
476     { /* 29 -- not defined */
477         NULL,
478         NULL
479     },
480     { /* 30 (LDID_BOOT) = Root of boot drive */
481         "BootDir",
482         NULL
483     },
484     { /* 31 (LDID_BOOT_HOST) = Root of boot drive host */
485         "BootHost",
486         NULL
487     },
488     { /* 32 (LDID_OLD_WINBOOT) = subdir of root */
489         "OldWinBootDir",
490         NULL
491     },
492     { /* 33 (LDID_OLD_WIN) = old win dir */
493         "OldWinDir",
494         NULL
495     }
496     /* the rest (34-38) isn't too interesting, so I'll forget about it */
497 };
498
499 /* 
500  * LDD  == Logical Device Descriptor
501  * LDID == Logical Device ID
502  *
503  * The whole LDD/LDID business might go into a separate file named
504  * ldd.c or logdevice.c.
505  * At the moment I don't know what the hell these functions are really doing.
506  * That's why I added reporting stubs.
507  * The only thing I do know is that I need them for the LDD/LDID infrastructure.
508  * That's why I implemented them in a way that's suitable for my purpose.
509  */
510 static LDD_LIST *pFirstLDD = NULL;
511
512 static BOOL std_LDDs_done = FALSE;
513
514 void SETUPX_CreateStandardLDDs(void)
515 {
516     HKEY hKey = 0;
517     WORD n;
518     DWORD type, len;
519     LOGDISKDESC_S ldd;
520     char buffer[MAX_PATH];
521
522     /* has to be here, otherwise loop */
523     std_LDDs_done = TRUE;
524
525     RegOpenKeyA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup", &hKey);
526
527     for (n=0; n < sizeof(LDID_Data)/sizeof(LDID_DATA); n++)
528     {
529         buffer[0] = '\0';
530
531         len = MAX_PATH;
532         if ( (hKey) && (LDID_Data[n].RegValName)
533         &&   (RegQueryValueExA(hKey, LDID_Data[n].RegValName,
534                 NULL, &type, buffer, &len) == ERROR_SUCCESS)
535         &&   (type == REG_SZ) )
536         {
537             TRACE("found value '%s' for LDID %d\n", buffer, n);
538         }
539         else
540         switch(n)
541         {
542             case LDID_SRCPATH:
543                 FIXME("LDID_SRCPATH: what exactly do we have to do here ?\n");
544                 strcpy(buffer, "X:\\FIXME");
545                 break;
546             case LDID_SYS:
547                 GetSystemDirectoryA(buffer, MAX_PATH);
548                 break;
549             case LDID_APPS:
550             case LDID_MACHINE:
551             case LDID_HOST_WINBOOT:
552             case LDID_BOOT:
553             case LDID_BOOT_HOST:
554                 strcpy(buffer, "C:\\");
555                 break;
556             default:
557                 if (LDID_Data[n].StdString)
558                 {
559                     DWORD len = GetWindowsDirectoryA(buffer, MAX_PATH);
560                     LPSTR p;
561                     p = buffer + len;
562                     *p++ = '\\';
563                     strcpy(p, LDID_Data[n].StdString);
564                 }
565                 break;
566         }
567         if (buffer[0])
568         {
569             INIT_LDD(ldd, n);
570             ldd.pszPath = buffer;
571             TRACE("LDID %d -> '%s'\n", ldd.ldid, ldd.pszPath);
572             CtlSetLdd16(&ldd);
573         }
574     }
575     if (hKey) RegCloseKey(hKey);
576 }
577         
578 /***********************************************************************
579  * CtlDelLdd            (SETUPX.37)
580  *
581  * RETURN
582  *   ERR_VCP_LDDINVALID if ldid < LDID_ASSIGN_START.
583  */
584 RETERR16 SETUPX_DelLdd(LOGDISKID16 ldid)
585 {
586     LDD_LIST *pCurr, *pPrev = NULL;
587
588     TRACE("(%d)\n", ldid);
589
590     if (!std_LDDs_done)
591         SETUPX_CreateStandardLDDs();
592
593     if (ldid < LDID_ASSIGN_START)
594         return ERR_VCP_LDDINVALID;
595
596     pCurr = pFirstLDD;
597     /* search until we find the appropriate LDD or hit the end */
598     while ((pCurr != NULL) && (ldid > pCurr->pldd->ldid))
599     {
600          pPrev = pCurr;
601          pCurr = pCurr->next;
602     }
603     if ( (pCurr == NULL) /* hit end of list */
604       || (ldid != pCurr->pldd->ldid) )
605         return ERR_VCP_LDDFIND; /* correct ? */
606
607     /* ok, found our victim: eliminate it */
608
609     if (pPrev)
610         pPrev->next = pCurr->next;
611
612     if (pCurr == pFirstLDD)
613         pFirstLDD = NULL;
614     HeapFree(GetProcessHeap(), 0, pCurr);
615     
616     return OK;
617 }
618
619 /***********************************************************************
620  *              CtlDelLdd (SETUPX.37)
621  */
622 RETERR16 WINAPI CtlDelLdd16(LOGDISKID16 ldid)
623 {
624     FIXME("(%d); - please report to a.mohr@mailto.de !!!\n", ldid);
625     return SETUPX_DelLdd(ldid);
626 }
627
628 /***********************************************************************
629  * CtlFindLdd           (SETUPX.35)
630  *
631  * doesn't check pldd ptr validity: crash (W98SE)
632  *
633  * RETURN
634  *   ERR_VCP_LDDINVALID if pldd->cbSize != structsize
635  *   1 in all other cases ??
636  * 
637  */
638 RETERR16 WINAPI CtlFindLdd16(LPLOGDISKDESC pldd)
639 {
640     LDD_LIST *pCurr, *pPrev = NULL;
641
642     TRACE("(%p)\n", pldd);
643    
644     if (!std_LDDs_done)
645         SETUPX_CreateStandardLDDs();
646
647     if (pldd->cbSize != sizeof(LOGDISKDESC_S))
648         return ERR_VCP_LDDINVALID;
649
650     pCurr = pFirstLDD;
651     /* search until we find the appropriate LDD or hit the end */
652     while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
653     {
654         pPrev = pCurr;
655         pCurr = pCurr->next;
656     }
657     if ( (pCurr == NULL) /* hit end of list */
658       || (pldd->ldid != pCurr->pldd->ldid) )
659         return ERR_VCP_LDDFIND; /* correct ? */
660
661     memcpy(pldd, pCurr->pldd, pldd->cbSize);
662     /* hmm, we probably ought to strcpy() the string ptrs here */
663     
664     return 1; /* what is this ?? */
665 }
666
667 /***********************************************************************
668  * CtlSetLdd                    (SETUPX.33)
669  *
670  * Set an LDD entry.
671  *
672  * RETURN
673  *   ERR_VCP_LDDINVALID if pldd.cbSize != sizeof(LOGDISKDESC_S)
674  *
675  */
676 RETERR16 WINAPI CtlSetLdd16(LPLOGDISKDESC pldd)
677 {
678     LDD_LIST *pCurr, *pPrev = NULL;
679     LPLOGDISKDESC pCurrLDD;
680     HANDLE heap;
681     BOOL is_new = FALSE;
682
683     TRACE("(%p)\n", pldd);
684
685     if (!std_LDDs_done)
686         SETUPX_CreateStandardLDDs();
687
688     if (pldd->cbSize != sizeof(LOGDISKDESC_S))
689         return ERR_VCP_LDDINVALID;
690
691     heap = GetProcessHeap();
692     pCurr = pFirstLDD;
693     /* search until we find the appropriate LDD or hit the end */
694     while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
695     {
696          pPrev = pCurr;
697          pCurr = pCurr->next;
698     }
699     if (pCurr == NULL) /* hit end of list */
700     {
701         is_new = TRUE;
702         pCurr = HeapAlloc(heap, 0, sizeof(LDD_LIST));
703         pCurr->pldd = HeapAlloc(heap, 0, sizeof(LOGDISKDESC_S));
704         pCurr->next = NULL;
705         pCurrLDD = pCurr->pldd;
706     }
707     else
708     {
709         pCurrLDD = pCurr->pldd;
710         if (pCurrLDD->pszPath)          HeapFree(heap, 0, pCurrLDD->pszPath);
711         if (pCurrLDD->pszVolLabel)      HeapFree(heap, 0, pCurrLDD->pszVolLabel);
712         if (pCurrLDD->pszDiskName)      HeapFree(heap, 0, pCurrLDD->pszDiskName);
713     }
714
715     memcpy(pCurrLDD, pldd, sizeof(LOGDISKDESC_S));
716
717     if (pldd->pszPath)
718         pCurrLDD->pszPath       = HEAP_strdupA(heap, 0, pldd->pszPath);
719     if (pldd->pszVolLabel)
720         pCurrLDD->pszVolLabel   = HEAP_strdupA(heap, 0, pldd->pszVolLabel);
721     if (pldd->pszDiskName)
722         pCurrLDD->pszDiskName   = HEAP_strdupA(heap, 0, pldd->pszDiskName);
723
724     if (is_new) /* link into list */
725     {
726         if (pPrev)
727         {
728             pCurr->next = pPrev->next;
729             pPrev->next = pCurr;
730         }
731         if (!pFirstLDD)
732             pFirstLDD = pCurr;
733     }
734     
735     return OK;
736 }
737
738
739 /***********************************************************************
740  * CtlAddLdd            (SETUPX.36)
741  *
742  * doesn't check pldd ptr validity: crash (W98SE)
743  *
744  */
745 static LOGDISKID16 ldid_to_add = LDID_ASSIGN_START;
746 RETERR16 WINAPI CtlAddLdd16(LPLOGDISKDESC pldd)
747 {
748     pldd->ldid = ldid_to_add++;
749     return CtlSetLdd16(pldd);
750 }
751
752 /***********************************************************************
753  * CtlGetLdd            (SETUPX.34)
754  *
755  * doesn't check pldd ptr validity: crash (W98SE)
756  * What the !@#$%&*( is the difference between CtlFindLdd() and CtlGetLdd() ??
757  *
758  * RETURN
759  *   ERR_VCP_LDDINVALID if pldd->cbSize != structsize
760  * 
761  */
762 static RETERR16 SETUPX_GetLdd(LPLOGDISKDESC pldd)
763 {
764     LDD_LIST *pCurr, *pPrev = NULL;
765
766     if (!std_LDDs_done)
767         SETUPX_CreateStandardLDDs();
768
769     if (pldd->cbSize != sizeof(LOGDISKDESC_S))
770         return ERR_VCP_LDDINVALID;
771
772     pCurr = pFirstLDD;
773     /* search until we find the appropriate LDD or hit the end */
774     while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
775     {
776          pPrev = pCurr;
777          pCurr = pCurr->next;
778     }
779     if (pCurr == NULL) /* hit end of list */
780         return ERR_VCP_LDDFIND; /* correct ? */
781
782     memcpy(pldd, pCurr->pldd, pldd->cbSize);
783     /* hmm, we probably ought to strcpy() the string ptrs here */
784
785     return OK;
786 }
787
788 /**********************************************************************/
789
790 RETERR16 WINAPI CtlGetLdd16(LPLOGDISKDESC pldd)
791 {
792     FIXME("(%p); - please report to a.mohr@mailto.de !!!\n", pldd);
793     return SETUPX_GetLdd(pldd);
794 }
795
796 /***********************************************************************
797  *              CtlGetLddPath           (SETUPX.38)
798  *
799  * Gets the path of an LDD.
800  * No crash if szPath == NULL.
801  * szPath has to be at least MAX_PATH_LEN bytes long.
802  * RETURN
803  *   ERR_VCP_LDDUNINIT if LDD for LDID not found.
804  */
805 RETERR16 WINAPI CtlGetLddPath16(LOGDISKID16 ldid, LPSTR szPath)
806 {
807     TRACE("(%d, %p);\n", ldid, szPath);
808
809     if (szPath)
810     {
811         LOGDISKDESC_S ldd;
812         INIT_LDD(ldd, ldid);
813         if (CtlFindLdd16(&ldd) == ERR_VCP_LDDFIND)
814             return ERR_VCP_LDDUNINIT;
815         SETUPX_GetLdd(&ldd);
816         strcpy(szPath, ldd.pszPath);
817         TRACE("ret '%s' for LDID %d\n", szPath, ldid);
818     }
819     return OK;
820 }
821
822 /***********************************************************************
823  *              CtlSetLddPath           (SETUPX.508)
824  *
825  * Sets the path of an LDD.
826  * Creates LDD for LDID if not existing yet.
827  */
828 RETERR16 WINAPI CtlSetLddPath16(LOGDISKID16 ldid, LPSTR szPath)
829 {
830     LOGDISKDESC_S ldd;
831     TRACE("(%d, '%s');\n", ldid, szPath);
832     
833     INIT_LDD(ldd, ldid);
834     ldd.pszPath = szPath;
835     return CtlSetLdd16(&ldd);
836 }
837
838 /*
839  * Find the value of a custom LDID in a .inf file
840  * e.g. for 49301:
841  * 49300,49301=ProgramFilesDir,5
842  * -- profile section lookup -->
843  * [ProgramFilesDir]
844  * HKLM,"Software\Microsoft\Windows\CurrentVersion","ProgramFilesDir",,"%24%"
845  * -- GenFormStrWithoutPlaceHolders16 -->
846  * HKLM,"Software\Microsoft\Windows\CurrentVersion","ProgramFilesDir",,"C:\"
847  * -- registry lookup -->
848  * C:\Program Files (or C:\ if not found in registry)
849  * 
850  * FIXME:
851  * - maybe we ought to add a caching array for speed ? - I don't care :)
852  * - not sure whether the processing is correct - sometimes there are equal
853  *   LDIDs for both install and removal sections.
854  * - probably the whole function can be removed as installers add that on their
855  *   own 
856  */
857 static BOOL SETUPX_AddCustomLDID(int ldid, INT16 hInf)
858 {
859     char ldidstr[6];
860     LPSTR sectionbuf = NULL, entrybuf = NULL, regsectionbuf = NULL;
861     LPCSTR filename;
862     LPSTR pSec, pEnt, pEqual, p, *pSub = NULL;
863     BOOL ret = FALSE;
864     char buffer[MAX_PATH];
865     LOGDISKDESC_S ldd;
866
867     sprintf(ldidstr, "%d", ldid);
868     filename = IP_GetFileName(hInf);
869     if (!(sectionbuf = SETUPX_GetSections(filename)))
870     {
871         ERR("couldn't get sections !\n");
872         return FALSE;
873     }
874     for (pSec=sectionbuf; *pSec; pSec += strlen(pSec)+1)
875     {
876         if (!(entrybuf = SETUPX_GetSectionEntries(filename, pSec)))
877         {
878             ERR("couldn't get section entries !\n");
879             goto end;
880         }
881         for (pEnt=entrybuf; *pEnt; pEnt += strlen(pEnt)+1)
882         {
883             if (strstr(pEnt, ldidstr))
884             {
885                 pEqual = strchr(pEnt, '=');
886                 if (!pEqual) /* crippled entry ?? */
887                     continue;
888
889                 /* make sure we found the LDID on left side of the equation */
890                 if (pEnt+strlen(ldidstr) <= pEqual)
891                 { /* found */
892
893                     /* but we don't want entries in the strings section */
894                     if (!strcasecmp(pSec, "Strings"))
895                         goto next_section;
896                     p = pEqual+1;
897                     goto found;
898                 }
899             }
900         }
901 next_section:
902     }
903     goto end;
904 found:
905     TRACE("found entry '%s'\n", p);
906     pSub = SETUPX_GetSubStrings(p, ',');
907     if (*(DWORD *)pSub > 2)
908     {
909         ERR("malformed entry '%s' ?\n", p);
910         goto end;
911     }
912     TRACE("found section '%s'\n", *(pSub+1));
913     /* FIXME: what are the optional flags at the end of an entry used for ?? */
914
915     /* get the location of the registry key from that section */
916     if (!(regsectionbuf = SETUPX_GetSectionEntries(filename, *(pSub+1))))
917     {
918         ERR("couldn't get registry section entries !\n");
919         goto end;
920     }
921     /* sectionbuf is > 1024 bytes anyway, so use it */
922     GenFormStrWithoutPlaceHolders16(sectionbuf, regsectionbuf, hInf);
923     ret = SETUPX_LookupRegistryString(sectionbuf, buffer, MAX_PATH);
924     TRACE("return '%s'\n", buffer);
925     INIT_LDD(ldd, ldid);
926     ldd.pszPath = buffer;
927     CtlSetLdd16(&ldd);
928 end:
929     SETUPX_FreeSubStrings(pSub);
930     if (sectionbuf)     HeapFree(GetProcessHeap(), 0, sectionbuf);
931     if (entrybuf)       HeapFree(GetProcessHeap(), 0, entrybuf);
932     if (regsectionbuf)  HeapFree(GetProcessHeap(), 0, regsectionbuf);
933     return ret;
934 }
935
936 /*
937  * Translate a logical disk identifier (LDID) into its string representation
938  * I'm afraid this can be totally replaced by CtlGetLddPath().
939  */
940 static BOOL SETUPX_IP_TranslateLDID(int ldid, LPSTR *p, HINF16 hInf)
941 {
942     BOOL handled = FALSE;
943     LOGDISKDESC_S ldd;
944
945     ldd.cbSize = sizeof(LOGDISKDESC_S);
946     ldd.ldid = ldid;
947     if (CtlFindLdd16(&ldd) == ERR_VCP_LDDFIND)
948     {
949         /* hmm, it seems the installers already do the work for us
950          * (by calling CtlSetLddPath) that SETUPX_AddCustomLDID
951          * is supposed to do. Grmbl ;-)
952          * Well, I'll leave it here anyway, but print error... */
953         ERR("hmm, LDID %d not registered yet !?\n", ldid);
954         handled = SETUPX_AddCustomLDID(ldid, hInf);
955     }
956     else
957         handled = TRUE;
958     
959     SETUPX_GetLdd(&ldd);
960     
961     if (!handled)
962     {
963         FIXME("What is LDID %d ??\n", ldid);
964         *p = "LDID_FIXME";
965     }
966     else
967         *p = ldd.pszPath;
968
969     return handled;
970 }
971
972 /***********************************************************************
973  *              GenFormStrWithoutPlaceHolders
974  *
975  * ought to be pretty much implemented, I guess...
976  */
977 void WINAPI GenFormStrWithoutPlaceHolders16( LPSTR szDst, LPCSTR szSrc, HINF16 hInf)
978 {
979     LPCSTR pSrc = szSrc, pSrcEnd = szSrc + strlen(szSrc);
980     LPSTR pDst = szDst, p, pPHBegin;
981     int count;
982     
983     TRACE("(%p, '%s', %04x);\n", szDst, szSrc, hInf);
984     while (pSrc < pSrcEnd)
985     {
986         p = strchr(pSrc, '%');
987         if (p)
988         {
989             count = (int)p - (int)pSrc;
990             strncpy(pDst, pSrc, count);
991             pSrc += count;
992             pDst += count;
993             pPHBegin = p+1;
994             p = strchr(pPHBegin, '%');
995             if (p)
996             {
997                 char placeholder[80]; /* that really ought to be enough ;) */
998                 int ldid;
999                 BOOL done = TRUE;
1000                 count = (int)p - (int)pPHBegin;
1001                 strncpy(placeholder, pPHBegin, count);
1002                 placeholder[count] = '\0';
1003                 ldid = atoi(placeholder);
1004                 if (ldid)
1005                 {
1006                     LPSTR p;
1007                     done = SETUPX_IP_TranslateLDID(ldid, &p, hInf);
1008                     strcpy(pDst, p);
1009                     if (done)
1010                         pDst += strlen(pDst);
1011                 }
1012                 else
1013                 { /* hmm, string placeholder. Need to look up
1014                      in the [strings] section of the hInf */
1015                     DWORD ret;
1016                     char buf[256]; /* long enough ? */
1017                     
1018                     ret = GetPrivateProfileStringA("strings", placeholder, "",
1019                                         buf, 256, IP_GetFileName(hInf));
1020                     if (ret)
1021                     {
1022                         strcpy(pDst, buf);
1023                         pDst += strlen(buf);
1024                     }
1025                     else
1026                     {
1027                         ERR("placeholder string '%s' not found !\n", placeholder);
1028                         done = FALSE;
1029                     }
1030                 }
1031                 if (!done)
1032                 { /* copy raw placeholder string over */
1033                     count = (int)p - (int)pPHBegin + 2;
1034                     strncpy(pDst, pPHBegin-1, count);
1035                     pDst += count;
1036                     
1037                 }
1038                 pSrc = p+1;
1039                 continue;
1040             }
1041         }
1042
1043         /* copy the remaining source string over */
1044         strncpy(pDst, pSrc, (int)pSrcEnd - (int)pSrc + 1);
1045         break;
1046     }
1047     TRACE("ret '%s'\n", szDst);
1048 }
1049
1050 /***********************************************************************
1051  *              VcpOpen
1052  *
1053  * No idea what to do here.
1054  */
1055 RETERR16 WINAPI VcpOpen16(VIFPROC vifproc, LPARAM lparamMsgRef)
1056 {
1057     FIXME("(%p, %08lx), stub.\n", vifproc, lparamMsgRef);
1058     return OK;
1059 }
1060
1061 /***********************************************************************
1062  *              VcpClose
1063  *
1064  * Is fl related to VCPDISKINFO.fl ?
1065  */
1066 RETERR16 WINAPI VcpClose16(WORD fl, LPCSTR lpszBackupDest)
1067 {
1068     FIXME("(%04x, '%s'), stub.\n", fl, lpszBackupDest);
1069     return OK;
1070 }
1071
1072 /*
1073  * Copy all items in a CopyFiles entry over to the destination
1074  *
1075  * - VNLP_xxx is what is given as flags for a .INF CopyFiles section
1076  */
1077 static BOOL SETUPX_CopyFiles(LPSTR *pSub, HINF16 hInf)
1078 {
1079     BOOL res = FALSE;
1080     unsigned int n;
1081     LPCSTR filename = IP_GetFileName(hInf);
1082     LPSTR pCopyEntry;
1083     char pDestStr[MAX_PATH];
1084     LPSTR pSrcDir, pDstDir;
1085     LPSTR pFileEntries, p;
1086     WORD ldid;
1087     LOGDISKDESC_S ldd;
1088     LPSTR *pSubFile;
1089     LPSTR pSrcFile, pDstFile;
1090
1091     for (n=0; n < *(DWORD *)pSub; n++)
1092     {
1093         pCopyEntry = *(pSub+1+n);
1094         if (*pCopyEntry == '@')
1095         {
1096             ERR("single file not handled yet !\n");
1097             continue;
1098         }
1099
1100         /* get source directory for that entry */
1101         INIT_LDD(ldd, LDID_SRCPATH);
1102         SETUPX_GetLdd(&ldd);
1103         pSrcDir = ldd.pszPath;
1104         
1105         /* get destination directory for that entry */
1106         if (!(GetPrivateProfileStringA("DestinationDirs", pCopyEntry, "",
1107                                         pDestStr, sizeof(pDestStr), filename)))
1108             continue;
1109
1110         /* translate destination dir if given as LDID */
1111         ldid = atoi(pDestStr);
1112         if (ldid)
1113         {
1114             if (!(SETUPX_IP_TranslateLDID(ldid, &pDstDir, hInf)))
1115                 continue;
1116         }
1117         else
1118             pDstDir = pDestStr;
1119         
1120         /* now that we have the destination dir, iterate over files to copy */
1121         pFileEntries = SETUPX_GetSectionEntries(filename, pCopyEntry);
1122         for (p=pFileEntries; *p; p +=strlen(p)+1)
1123         {
1124             pSubFile = SETUPX_GetSubStrings(p, ',');
1125             pSrcFile = *(pSubFile+1);
1126             pDstFile = (*(DWORD *)pSubFile > 1) ? *(pSubFile+2) : pSrcFile;
1127             TRACE("copying file '%s\\%s' to '%s\\%s'\n", pSrcDir, pSrcFile, pDstDir, pDstFile);
1128             if (*(DWORD *)pSubFile > 2)
1129             {
1130                 WORD flag;
1131                 if ((flag = atoi(*(pSubFile+3)))) /* ah, flag */
1132                 {
1133                     if (flag & 0x2c)
1134                     FIXME("VNLP_xxx flag %d not handled yet.\n", flag);
1135                 }
1136                 else
1137                     FIXME("temp file name '%s' given. Need to register in wininit.ini !\n", *(pSubFile+3)); /* strong guess that this is VcpQueueCopy() */
1138             }
1139             SETUPX_FreeSubStrings(pSubFile);
1140             /* we don't copy ANYTHING yet ! (I'm too lazy and want to verify
1141              * this first before destroying whole partitions ;-) */
1142         }
1143     }
1144             
1145     return res;
1146 }
1147
1148 /***********************************************************************
1149  *              GenInstall
1150  *
1151  * general install function for .INF file sections
1152  *
1153  * This is not perfect - patch whenever you can !
1154  * 
1155  * wFlags == GENINSTALL_DO_xxx
1156  * e.g. NetMeeting:
1157  * first call GENINSTALL_DO_REGSRCPATH | GENINSTALL_DO_FILES,
1158  * second call GENINSTALL_DO_LOGCONFIG | CFGAUTO | INI2REG | REG | INI
1159  */
1160 RETERR16 WINAPI GenInstall16(HINF16 hInfFile, LPCSTR szInstallSection, WORD wFlags)
1161 {
1162     LPCSTR filename = IP_GetFileName(hInfFile);
1163     LPSTR pEntries, p, pEnd;
1164     DWORD len;
1165     LPSTR *pSub;
1166
1167     FIXME("(%04x, '%s', %04x), semi-stub. Please implement additional operations here !\n", hInfFile, szInstallSection, wFlags);
1168     pEntries = SETUPX_GetSectionEntries(filename, szInstallSection);
1169     if (!pEntries)
1170     {
1171         ERR("couldn't find entries for section '%s' !\n", szInstallSection);
1172         return ERR_IP_SECT_NOT_FOUND;
1173     }
1174     for (p=pEntries; *p; p +=strlen(p)+1)
1175     {
1176         pEnd = strchr(p, '=');
1177         if (!pEnd) continue;
1178         pSub = SETUPX_GetSubStrings(pEnd+1, ','); /* split entries after the '=' */
1179         SETUPX_IsolateSubString(&p, &pEnd);
1180         len = (int)pEnd - (int)p;
1181
1182         if (wFlags & GENINSTALL_DO_FILES)
1183         {
1184             if (!strncasecmp(p, "CopyFiles", len))
1185             {
1186                 SETUPX_CopyFiles(pSub, hInfFile);
1187                 continue;
1188             }
1189 #if IMPLEMENT_THAT
1190             else
1191             if (!strncasecmp(p, "DelFiles", len))
1192             {
1193                 SETUPX_DelFiles(filename, szInstallSection, pSub);
1194                 continue;
1195             }
1196 #endif
1197         }
1198         if (wFlags & GENINSTALL_DO_INI)
1199         {
1200 #if IMPLEMENT_THAT
1201             if (!strncasecmp(p, "UpdateInis", len))
1202             {
1203                 SETUPX_UpdateInis(filename, szInstallSection, pSub);
1204                 continue;
1205             }
1206 #endif
1207         }
1208         if (wFlags & GENINSTALL_DO_REG)
1209         {
1210 #if IMPLEMENT_THAT
1211             /* probably use SUReg*() functions here */
1212             if (!strncasecmp(p, "AddReg", len))
1213             {
1214                 SETUPX_AddReg(filename, szInstallSection, pSub);
1215                 continue;
1216             }
1217             else
1218             if (!strncasecmp(p, "DelReg", len))
1219             {
1220                 SETUPX_DelReg(filename, szInstallSection, pSub);
1221                 continue;
1222             }
1223 #endif
1224         }
1225         
1226         SETUPX_FreeSubStrings(pSub);
1227     }
1228     HeapFree(GetProcessHeap(), 0, pEntries);
1229     return OK;
1230 }