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