List 16-bit resources separately as RC_SRCS16.
[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 "setupapi_private.h"
51 #include "winerror.h"
52 #include "heap.h"
53 #include "debugtools.h"
54
55 DEFAULT_DEBUG_CHANNEL(setupapi);
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, tmp;
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 (VcpOpen16(NULL, 0))
314         goto end;
315     if (GenInstall16(hInf, *(pSub+count-2), GENINSTALL_DO_ALL) != OK)
316         goto end;
317     wFlags = atoi(*(pSub+count-1)) & ~128;
318     switch (wFlags)
319     {
320         case HOW_ALWAYS_SILENT_REBOOT:
321         case HOW_SILENT_REBOOT:
322             reboot = TRUE;
323             break;
324         case HOW_ALWAYS_PROMPT_REBOOT:
325         case HOW_PROMPT_REBOOT:
326             if ((hMod = GetModuleHandleA("user32.dll")))
327             {
328               if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( hMod, "MessageBoxA" )))
329               {
330                  
331                 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)
332                   reboot = TRUE;
333               }
334             }
335             break;
336         default:
337             ERR("invalid flags %d !\n", wFlags);
338             goto end;
339     }
340     
341     res = OK;
342 end:
343     tmp = VcpClose16(VCPFL_ALL, NULL);
344     if (tmp != OK)
345         res = tmp;
346     tmp = IpClose16(hInf);
347     if (tmp != OK)
348         res = tmp;
349     SETUPX_FreeSubStrings(pSub);
350     if (reboot)
351     {
352         /* FIXME: we should have a means of terminating all wine + wineserver */
353         MESSAGE("Program or user told me to restart. Exiting Wine...\n");
354         ExitProcess(1);
355     }
356
357     return res;
358 }
359
360 typedef struct
361 {
362     LPCSTR RegValName;
363     LPCSTR StdString; /* fallback string; sub dir of windows directory */
364 } LDID_DATA;
365
366 static const LDID_DATA LDID_Data[34] =
367 {
368     { /* 0 (LDID_NULL) -- not defined */
369         NULL,
370         NULL
371     },
372     { /* 1 (LDID_SRCPATH) = source of installation. hmm, what to do here ? */
373         "SourcePath", /* hmm, does SETUPX have to care about updating it ?? */
374         NULL
375     },
376     { /* 2 (LDID_SETUPTEMP) = setup temp dir */
377         "SetupTempDir",
378         NULL
379     },
380     { /* 3 (LDID_UNINSTALL) = uninstall backup dir */
381         "UninstallDir",
382         NULL
383     },
384     { /* 4 (LDID_BACKUP) = backup dir */
385         "BackupDir",
386         NULL
387     },
388     { /* 5 (LDID_SETUPSCRATCH) = setup scratch dir */
389         "SetupScratchDir",
390         NULL
391     },
392     { /* 6 -- not defined */
393         NULL,
394         NULL
395     },
396     { /* 7 -- not defined */
397         NULL,
398         NULL
399     },
400     { /* 8 -- not defined */
401         NULL,
402         NULL
403     },
404     { /* 9 -- not defined */
405         NULL,
406         NULL
407     },
408     { /* 10 (LDID_WIN) = windows dir */
409         "WinDir",
410         ""
411     },
412     { /* 11 (LDID_SYS) = system dir */
413         "SysDir",
414         NULL /* call GetSystemDirectory() instead */
415     },
416     { /* 12 (LDID_IOS) = IOSubSys dir */
417         NULL, /* FIXME: registry string ? */
418         "SYSTEM\\IOSUBSYS"
419     },
420     { /* 13 (LDID_CMD) = COMMAND dir */
421         NULL, /* FIXME: registry string ? */
422         "COMMAND"
423     },
424     { /* 14 (LDID_CPL) = control panel dir */
425         NULL,
426         ""
427     },
428     { /* 15 (LDID_PRINT) = windows printer dir */
429         NULL,
430         "SYSTEM" /* correct ?? */
431     },
432     { /* 16 (LDID_MAIL) = destination mail dir */
433         NULL,
434         ""
435     },
436     { /* 17 (LDID_INF) = INF dir */
437         "SetupScratchDir", /* correct ? */
438         "INF"
439     },
440     { /* 18 (LDID_HELP) = HELP dir */
441         NULL, /* ??? */
442         "HELP"
443     },
444     { /* 19 (LDID_WINADMIN) = Admin dir */
445         "WinAdminDir",
446         ""
447     },
448     { /* 20 (LDID_FONTS) = Fonts dir */
449         NULL, /* ??? */
450         "FONTS"
451     },
452     { /* 21 (LDID_VIEWERS) = Viewers */
453         NULL, /* ??? */
454         "SYSTEM\\VIEWERS"
455     },
456     { /* 22 (LDID_VMM32) = VMM32 dir */
457         NULL, /* ??? */
458         "SYSTEM\\VMM32"
459     },
460     { /* 23 (LDID_COLOR) = ICM dir */
461         "ICMPath",
462         "SYSTEM\\COLOR"
463     },
464     { /* 24 (LDID_APPS) = root of boot drive ? */
465         "AppsDir",
466         "C:\\"
467     },
468     { /* 25 (LDID_SHARED) = shared dir */
469         "SharedDir",
470         ""
471     },
472     { /* 26 (LDID_WINBOOT) = Windows boot dir */
473         "WinBootDir",
474         ""
475     },
476     { /* 27 (LDID_MACHINE) = machine specific files */
477         "MachineDir",
478         NULL
479     },
480     { /* 28 (LDID_HOST_WINBOOT) = Host Windows boot dir */
481         "HostWinBootDir",
482         NULL
483     },
484     { /* 29 -- not defined */
485         NULL,
486         NULL
487     },
488     { /* 30 (LDID_BOOT) = Root of boot drive */
489         "BootDir",
490         NULL
491     },
492     { /* 31 (LDID_BOOT_HOST) = Root of boot drive host */
493         "BootHost",
494         NULL
495     },
496     { /* 32 (LDID_OLD_WINBOOT) = subdir of root */
497         "OldWinBootDir",
498         NULL
499     },
500     { /* 33 (LDID_OLD_WIN) = old win dir */
501         "OldWinDir",
502         NULL
503     }
504     /* the rest (34-38) isn't too interesting, so I'll forget about it */
505 };
506
507 /* 
508  * LDD  == Logical Device Descriptor
509  * LDID == Logical Device ID
510  *
511  * The whole LDD/LDID business might go into a separate file named
512  * ldd.c.
513  * At the moment I don't know what the hell these functions are really doing.
514  * That's why I added reporting stubs.
515  * The only thing I do know is that I need them for the LDD/LDID infrastructure.
516  * That's why I implemented them in a way that's suitable for my purpose.
517  */
518 static LDD_LIST *pFirstLDD = NULL;
519
520 static BOOL std_LDDs_done = FALSE;
521
522 void SETUPX_CreateStandardLDDs(void)
523 {
524     HKEY hKey = 0;
525     WORD n;
526     DWORD type, len;
527     LOGDISKDESC_S ldd;
528     char buffer[MAX_PATH];
529
530     /* has to be here, otherwise loop */
531     std_LDDs_done = TRUE;
532
533     RegOpenKeyA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup", &hKey);
534
535     for (n=0; n < sizeof(LDID_Data)/sizeof(LDID_DATA); n++)
536     {
537         buffer[0] = '\0';
538
539         len = MAX_PATH;
540         if ( (hKey) && (LDID_Data[n].RegValName)
541         &&   (RegQueryValueExA(hKey, LDID_Data[n].RegValName,
542                 NULL, &type, buffer, &len) == ERROR_SUCCESS)
543         &&   (type == REG_SZ) )
544         {
545             TRACE("found value '%s' for LDID %d\n", buffer, n);
546         }
547         else
548         switch(n)
549         {
550             case LDID_SRCPATH:
551                 FIXME("LDID_SRCPATH: what exactly do we have to do here ?\n");
552                 strcpy(buffer, "X:\\FIXME");
553                 break;
554             case LDID_SYS:
555                 GetSystemDirectoryA(buffer, MAX_PATH);
556                 break;
557             case LDID_APPS:
558             case LDID_MACHINE:
559             case LDID_HOST_WINBOOT:
560             case LDID_BOOT:
561             case LDID_BOOT_HOST:
562                 strcpy(buffer, "C:\\");
563                 break;
564             default:
565                 if (LDID_Data[n].StdString)
566                 {
567                     DWORD len = GetWindowsDirectoryA(buffer, MAX_PATH);
568                     LPSTR p;
569                     p = buffer + len;
570                     *p++ = '\\';
571                     strcpy(p, LDID_Data[n].StdString);
572                 }
573                 break;
574         }
575         if (buffer[0])
576         {
577             INIT_LDD(ldd, n);
578             ldd.pszPath = buffer;
579             TRACE("LDID %d -> '%s'\n", ldd.ldid, ldd.pszPath);
580             CtlSetLdd16(&ldd);
581         }
582     }
583     if (hKey) RegCloseKey(hKey);
584 }
585         
586 /***********************************************************************
587  * CtlDelLdd            (SETUPX.37)
588  *
589  * RETURN
590  *   ERR_VCP_LDDINVALID if ldid < LDID_ASSIGN_START.
591  */
592 RETERR16 SETUPX_DelLdd(LOGDISKID16 ldid)
593 {
594     LDD_LIST *pCurr, *pPrev = NULL;
595
596     TRACE("(%d)\n", ldid);
597
598     if (!std_LDDs_done)
599         SETUPX_CreateStandardLDDs();
600
601     if (ldid < LDID_ASSIGN_START)
602         return ERR_VCP_LDDINVALID;
603
604     pCurr = pFirstLDD;
605     /* search until we find the appropriate LDD or hit the end */
606     while ((pCurr != NULL) && (ldid > pCurr->pldd->ldid))
607     {
608          pPrev = pCurr;
609          pCurr = pCurr->next;
610     }
611     if ( (pCurr == NULL) /* hit end of list */
612       || (ldid != pCurr->pldd->ldid) )
613         return ERR_VCP_LDDFIND; /* correct ? */
614
615     /* ok, found our victim: eliminate it */
616
617     if (pPrev)
618         pPrev->next = pCurr->next;
619
620     if (pCurr == pFirstLDD)
621         pFirstLDD = NULL;
622     HeapFree(GetProcessHeap(), 0, pCurr);
623     
624     return OK;
625 }
626
627 /***********************************************************************
628  *              CtlDelLdd (SETUPX.37)
629  */
630 RETERR16 WINAPI CtlDelLdd16(LOGDISKID16 ldid)
631 {
632     FIXME("(%d); - please report to a.mohr@mailto.de !!!\n", ldid);
633     return SETUPX_DelLdd(ldid);
634 }
635
636 /***********************************************************************
637  * CtlFindLdd           (SETUPX.35)
638  *
639  * doesn't check pldd ptr validity: crash (W98SE)
640  *
641  * RETURN
642  *   ERR_VCP_LDDINVALID if pldd->cbSize != structsize
643  *   1 in all other cases ??
644  * 
645  */
646 RETERR16 WINAPI CtlFindLdd16(LPLOGDISKDESC pldd)
647 {
648     LDD_LIST *pCurr, *pPrev = NULL;
649
650     TRACE("(%p)\n", pldd);
651    
652     if (!std_LDDs_done)
653         SETUPX_CreateStandardLDDs();
654
655     if (pldd->cbSize != sizeof(LOGDISKDESC_S))
656         return ERR_VCP_LDDINVALID;
657
658     pCurr = pFirstLDD;
659     /* search until we find the appropriate LDD or hit the end */
660     while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
661     {
662         pPrev = pCurr;
663         pCurr = pCurr->next;
664     }
665     if ( (pCurr == NULL) /* hit end of list */
666       || (pldd->ldid != pCurr->pldd->ldid) )
667         return ERR_VCP_LDDFIND; /* correct ? */
668
669     memcpy(pldd, pCurr->pldd, pldd->cbSize);
670     /* hmm, we probably ought to strcpy() the string ptrs here */
671     
672     return 1; /* what is this ?? */
673 }
674
675 /***********************************************************************
676  * CtlSetLdd                    (SETUPX.33)
677  *
678  * Set an LDD entry.
679  *
680  * RETURN
681  *   ERR_VCP_LDDINVALID if pldd.cbSize != sizeof(LOGDISKDESC_S)
682  *
683  */
684 RETERR16 WINAPI CtlSetLdd16(LPLOGDISKDESC pldd)
685 {
686     LDD_LIST *pCurr, *pPrev = NULL;
687     LPLOGDISKDESC pCurrLDD;
688     HANDLE heap;
689     BOOL is_new = FALSE;
690
691     TRACE("(%p)\n", pldd);
692
693     if (!std_LDDs_done)
694         SETUPX_CreateStandardLDDs();
695
696     if (pldd->cbSize != sizeof(LOGDISKDESC_S))
697         return ERR_VCP_LDDINVALID;
698
699     heap = GetProcessHeap();
700     pCurr = pFirstLDD;
701     /* search until we find the appropriate LDD or hit the end */
702     while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
703     {
704          pPrev = pCurr;
705          pCurr = pCurr->next;
706     }
707     if (pCurr == NULL) /* hit end of list */
708     {
709         is_new = TRUE;
710         pCurr = HeapAlloc(heap, 0, sizeof(LDD_LIST));
711         pCurr->pldd = HeapAlloc(heap, 0, sizeof(LOGDISKDESC_S));
712         pCurr->next = NULL;
713         pCurrLDD = pCurr->pldd;
714     }
715     else
716     {
717         pCurrLDD = pCurr->pldd;
718         if (pCurrLDD->pszPath)          HeapFree(heap, 0, pCurrLDD->pszPath);
719         if (pCurrLDD->pszVolLabel)      HeapFree(heap, 0, pCurrLDD->pszVolLabel);
720         if (pCurrLDD->pszDiskName)      HeapFree(heap, 0, pCurrLDD->pszDiskName);
721     }
722
723     memcpy(pCurrLDD, pldd, sizeof(LOGDISKDESC_S));
724
725     if (pldd->pszPath)
726         pCurrLDD->pszPath       = HEAP_strdupA(heap, 0, pldd->pszPath);
727     if (pldd->pszVolLabel)
728         pCurrLDD->pszVolLabel   = HEAP_strdupA(heap, 0, pldd->pszVolLabel);
729     if (pldd->pszDiskName)
730         pCurrLDD->pszDiskName   = HEAP_strdupA(heap, 0, pldd->pszDiskName);
731
732     if (is_new) /* link into list */
733     {
734         if (pPrev)
735         {
736             pCurr->next = pPrev->next;
737             pPrev->next = pCurr;
738         }
739         if (!pFirstLDD)
740             pFirstLDD = pCurr;
741     }
742     
743     return OK;
744 }
745
746
747 /***********************************************************************
748  * CtlAddLdd            (SETUPX.36)
749  *
750  * doesn't check pldd ptr validity: crash (W98SE)
751  *
752  */
753 static LOGDISKID16 ldid_to_add = LDID_ASSIGN_START;
754 RETERR16 WINAPI CtlAddLdd16(LPLOGDISKDESC pldd)
755 {
756     pldd->ldid = ldid_to_add++;
757     return CtlSetLdd16(pldd);
758 }
759
760 /***********************************************************************
761  * CtlGetLdd            (SETUPX.34)
762  *
763  * doesn't check pldd ptr validity: crash (W98SE)
764  * What the !@#$%&*( is the difference between CtlFindLdd() and CtlGetLdd() ??
765  *
766  * RETURN
767  *   ERR_VCP_LDDINVALID if pldd->cbSize != structsize
768  * 
769  */
770 static RETERR16 SETUPX_GetLdd(LPLOGDISKDESC pldd)
771 {
772     LDD_LIST *pCurr, *pPrev = NULL;
773
774     if (!std_LDDs_done)
775         SETUPX_CreateStandardLDDs();
776
777     if (pldd->cbSize != sizeof(LOGDISKDESC_S))
778         return ERR_VCP_LDDINVALID;
779
780     pCurr = pFirstLDD;
781     /* search until we find the appropriate LDD or hit the end */
782     while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
783     {
784          pPrev = pCurr;
785          pCurr = pCurr->next;
786     }
787     if (pCurr == NULL) /* hit end of list */
788         return ERR_VCP_LDDFIND; /* correct ? */
789
790     memcpy(pldd, pCurr->pldd, pldd->cbSize);
791     /* hmm, we probably ought to strcpy() the string ptrs here */
792
793     return OK;
794 }
795
796 /**********************************************************************/
797
798 RETERR16 WINAPI CtlGetLdd16(LPLOGDISKDESC pldd)
799 {
800     FIXME("(%p); - please report to a.mohr@mailto.de !!!\n", pldd);
801     return SETUPX_GetLdd(pldd);
802 }
803
804 /***********************************************************************
805  *              CtlGetLddPath           (SETUPX.38)
806  *
807  * Gets the path of an LDD.
808  * No crash if szPath == NULL.
809  * szPath has to be at least MAX_PATH_LEN bytes long.
810  * RETURN
811  *   ERR_VCP_LDDUNINIT if LDD for LDID not found.
812  */
813 RETERR16 WINAPI CtlGetLddPath16(LOGDISKID16 ldid, LPSTR szPath)
814 {
815     TRACE("(%d, %p);\n", ldid, szPath);
816
817     if (szPath)
818     {
819         LOGDISKDESC_S ldd;
820         INIT_LDD(ldd, ldid);
821         if (CtlFindLdd16(&ldd) == ERR_VCP_LDDFIND)
822             return ERR_VCP_LDDUNINIT;
823         SETUPX_GetLdd(&ldd);
824         strcpy(szPath, ldd.pszPath);
825         TRACE("ret '%s' for LDID %d\n", szPath, ldid);
826     }
827     return OK;
828 }
829
830 /***********************************************************************
831  *              CtlSetLddPath           (SETUPX.508)
832  *
833  * Sets the path of an LDD.
834  * Creates LDD for LDID if not existing yet.
835  */
836 RETERR16 WINAPI CtlSetLddPath16(LOGDISKID16 ldid, LPSTR szPath)
837 {
838     LOGDISKDESC_S ldd;
839     TRACE("(%d, '%s');\n", ldid, szPath);
840     
841     INIT_LDD(ldd, ldid);
842     ldd.pszPath = szPath;
843     return CtlSetLdd16(&ldd);
844 }
845
846 /*
847  * Find the value of a custom LDID in a .inf file
848  * e.g. for 49301:
849  * 49300,49301=ProgramFilesDir,5
850  * -- profile section lookup -->
851  * [ProgramFilesDir]
852  * HKLM,"Software\Microsoft\Windows\CurrentVersion","ProgramFilesDir",,"%24%"
853  * -- GenFormStrWithoutPlaceHolders16 -->
854  * HKLM,"Software\Microsoft\Windows\CurrentVersion","ProgramFilesDir",,"C:\"
855  * -- registry lookup -->
856  * C:\Program Files (or C:\ if not found in registry)
857  * 
858  * FIXME:
859  * - maybe we ought to add a caching array for speed ? - I don't care :)
860  * - not sure whether the processing is correct - sometimes there are equal
861  *   LDIDs for both install and removal sections.
862  * - probably the whole function can be removed as installers add that on their
863  *   own 
864  */
865 static BOOL SETUPX_AddCustomLDID(int ldid, INT16 hInf)
866 {
867     char ldidstr[6];
868     LPSTR sectionbuf = NULL, entrybuf = NULL, regsectionbuf = NULL;
869     LPCSTR filename;
870     LPSTR pSec, pEnt, pEqual, p, *pSub = NULL;
871     BOOL ret = FALSE;
872     char buffer[MAX_PATH];
873     LOGDISKDESC_S ldd;
874
875     sprintf(ldidstr, "%d", ldid);
876     filename = IP_GetFileName(hInf);
877     if (!(sectionbuf = SETUPX_GetSections(filename)))
878     {
879         ERR("couldn't get sections !\n");
880         return FALSE;
881     }
882     for (pSec=sectionbuf; *pSec; pSec += strlen(pSec)+1)
883     {
884         if (!(entrybuf = SETUPX_GetSectionEntries(filename, pSec)))
885         {
886             ERR("couldn't get section entries !\n");
887             goto end;
888         }
889         for (pEnt=entrybuf; *pEnt; pEnt += strlen(pEnt)+1)
890         {
891             if (strstr(pEnt, ldidstr))
892             {
893                 pEqual = strchr(pEnt, '=');
894                 if (!pEqual) /* crippled entry ?? */
895                     continue;
896
897                 /* make sure we found the LDID on left side of the equation */
898                 if (pEnt+strlen(ldidstr) <= pEqual)
899                 { /* found */
900
901                     /* but we don't want entries in the strings section */
902                     if (!strcasecmp(pSec, "Strings"))
903                         goto next_section;
904                     p = pEqual+1;
905                     goto found;
906                 }
907             }
908         }
909 next_section:
910     }
911     goto end;
912 found:
913     TRACE("found entry '%s'\n", p);
914     pSub = SETUPX_GetSubStrings(p, ',');
915     if (*(DWORD *)pSub > 2)
916     {
917         ERR("malformed entry '%s' ?\n", p);
918         goto end;
919     }
920     TRACE("found section '%s'\n", *(pSub+1));
921     /* FIXME: what are the optional flags at the end of an entry used for ?? */
922
923     /* get the location of the registry key from that section */
924     if (!(regsectionbuf = SETUPX_GetSectionEntries(filename, *(pSub+1))))
925     {
926         ERR("couldn't get registry section entries !\n");
927         goto end;
928     }
929     /* sectionbuf is > 1024 bytes anyway, so use it */
930     GenFormStrWithoutPlaceHolders16(sectionbuf, regsectionbuf, hInf);
931     ret = SETUPX_LookupRegistryString(sectionbuf, buffer, MAX_PATH);
932     TRACE("return '%s'\n", buffer);
933     INIT_LDD(ldd, ldid);
934     ldd.pszPath = buffer;
935     CtlSetLdd16(&ldd);
936 end:
937     SETUPX_FreeSubStrings(pSub);
938     if (sectionbuf)     HeapFree(GetProcessHeap(), 0, sectionbuf);
939     if (entrybuf)       HeapFree(GetProcessHeap(), 0, entrybuf);
940     if (regsectionbuf)  HeapFree(GetProcessHeap(), 0, regsectionbuf);
941     return ret;
942 }
943
944 /*
945  * Translate a logical disk identifier (LDID) into its string representation
946  * I'm afraid this can be totally replaced by CtlGetLddPath().
947  */
948 static BOOL SETUPX_IP_TranslateLDID(int ldid, LPSTR *p, HINF16 hInf)
949 {
950     BOOL handled = FALSE;
951     LOGDISKDESC_S ldd;
952
953     ldd.cbSize = sizeof(LOGDISKDESC_S);
954     ldd.ldid = ldid;
955     if (CtlFindLdd16(&ldd) == ERR_VCP_LDDFIND)
956     {
957         /* hmm, it seems the installers already do the work for us
958          * (by calling CtlSetLddPath) that SETUPX_AddCustomLDID
959          * is supposed to do. Grmbl ;-)
960          * Well, I'll leave it here anyway, but print error... */
961         ERR("hmm, LDID %d not registered yet !?\n", ldid);
962         handled = SETUPX_AddCustomLDID(ldid, hInf);
963     }
964     else
965         handled = TRUE;
966     
967     SETUPX_GetLdd(&ldd);
968     
969     if (!handled)
970     {
971         FIXME("What is LDID %d ??\n", ldid);
972         *p = "LDID_FIXME";
973     }
974     else
975         *p = ldd.pszPath;
976
977     return handled;
978 }
979
980 /***********************************************************************
981  *              GenFormStrWithoutPlaceHolders
982  *
983  * ought to be pretty much implemented, I guess...
984  */
985 void WINAPI GenFormStrWithoutPlaceHolders16( LPSTR szDst, LPCSTR szSrc, HINF16 hInf)
986 {
987     LPCSTR pSrc = szSrc, pSrcEnd = szSrc + strlen(szSrc);
988     LPSTR pDst = szDst, p, pPHBegin;
989     int count;
990     
991     TRACE("(%p, '%s', %04x);\n", szDst, szSrc, hInf);
992     while (pSrc < pSrcEnd)
993     {
994         p = strchr(pSrc, '%');
995         if (p)
996         {
997             count = (int)p - (int)pSrc;
998             strncpy(pDst, pSrc, count);
999             pSrc += count;
1000             pDst += count;
1001             pPHBegin = p+1;
1002             p = strchr(pPHBegin, '%');
1003             if (p)
1004             {
1005                 char placeholder[80]; /* that really ought to be enough ;) */
1006                 int ldid;
1007                 BOOL done = TRUE;
1008                 count = (int)p - (int)pPHBegin;
1009                 strncpy(placeholder, pPHBegin, count);
1010                 placeholder[count] = '\0';
1011                 ldid = atoi(placeholder);
1012                 if (ldid)
1013                 {
1014                     LPSTR p;
1015                     done = SETUPX_IP_TranslateLDID(ldid, &p, hInf);
1016                     strcpy(pDst, p);
1017                     if (done)
1018                         pDst += strlen(pDst);
1019                 }
1020                 else
1021                 { /* hmm, string placeholder. Need to look up
1022                      in the [strings] section of the hInf */
1023                     DWORD ret;
1024                     char buf[256]; /* long enough ? */
1025                     
1026                     ret = GetPrivateProfileStringA("strings", placeholder, "",
1027                                         buf, 256, IP_GetFileName(hInf));
1028                     if (ret)
1029                     {
1030                         strcpy(pDst, buf);
1031                         pDst += strlen(buf);
1032                     }
1033                     else
1034                     {
1035                         ERR("placeholder string '%s' not found !\n", placeholder);
1036                         done = FALSE;
1037                     }
1038                 }
1039                 if (!done)
1040                 { /* copy raw placeholder string over */
1041                     count = (int)p - (int)pPHBegin + 2;
1042                     strncpy(pDst, pPHBegin-1, count);
1043                     pDst += count;
1044                     
1045                 }
1046                 pSrc = p+1;
1047                 continue;
1048             }
1049         }
1050
1051         /* copy the remaining source string over */
1052         strncpy(pDst, pSrc, (int)pSrcEnd - (int)pSrc + 1);
1053         break;
1054     }
1055     TRACE("ret '%s'\n", szDst);
1056 }
1057
1058 /*
1059  * Copy all items in a CopyFiles entry over to the destination
1060  *
1061  * - VNLP_xxx is what is given as flags for a .INF CopyFiles section
1062  */
1063 static BOOL SETUPX_CopyFiles(LPSTR *pSub, HINF16 hInf)
1064 {
1065     BOOL bSingle = FALSE;
1066     unsigned int n;
1067     LPCSTR filename = IP_GetFileName(hInf);
1068     LPSTR pCopyEntry;
1069     char pDstStr[MAX_PATH];
1070     LPSTR pSrcDir, pDstDir;
1071     LPSTR pFileEntries, p;
1072     WORD ldid;
1073     LOGDISKDESC_S ldd;
1074     LPSTR *pSubFile;
1075     LPSTR pSrcFile, pDstFile;
1076     WORD flag;
1077
1078     for (n=0; n < *(DWORD *)pSub; n++)
1079     {
1080         pCopyEntry = *(pSub+1+n);
1081         if (*pCopyEntry == '@')
1082         {
1083             pCopyEntry++;
1084             bSingle = TRUE;
1085         }
1086         else
1087             bSingle = FALSE;
1088
1089         /* get source directory for that entry */
1090         INIT_LDD(ldd, LDID_SRCPATH);
1091         SETUPX_GetLdd(&ldd);
1092         pSrcDir = ldd.pszPath;
1093         
1094         /* get destination directory for that entry */
1095         if (!(GetPrivateProfileStringA("DestinationDirs", pCopyEntry, "",
1096                                         pDstStr, sizeof(pDstStr), filename)))
1097         {
1098             /* hmm, not found; try the default entry */
1099             if (!(GetPrivateProfileStringA("DestinationDirs", "DefaultDestDir", "", pDstStr, sizeof(pDstStr), filename)))
1100             {
1101                 WARN("DefaultDestDir not found.\n");
1102                 continue;
1103             }
1104         }
1105
1106         /* translate destination dir if given as LDID */
1107         ldid = atoi(pDstStr);
1108         if (ldid)
1109         {
1110             if (!(SETUPX_IP_TranslateLDID(ldid, &pDstDir, hInf)))
1111                 continue;
1112         }
1113         else
1114             pDstDir = pDstStr;
1115         
1116         /* now that we have the destination dir, register file copying */
1117
1118         if (bSingle)
1119         {
1120             VcpQueueCopy16(pCopyEntry, pCopyEntry, pSrcDir, pDstDir, LDID_SRCPATH, ldid ? ldid : 0xffff, 0, VFNL_COPY, 0);
1121             return TRUE;
1122         }
1123
1124         /* entry wasn't a single file, so let's iterate over section */
1125         pFileEntries = SETUPX_GetSectionEntries(filename, pCopyEntry);
1126         for (p=pFileEntries; *p; p +=strlen(p)+1)
1127         {
1128             pSubFile = SETUPX_GetSubStrings(p, ',');
1129             pSrcFile = *(pSubFile+1);
1130             pDstFile = (*(DWORD *)pSubFile > 1) ? *(pSubFile+2) : pSrcFile;
1131             TRACE("copying file '%s\\%s' to '%s\\%s'\n", pSrcDir, pSrcFile, pDstDir, pDstFile);
1132             flag = 0;
1133             if (*(DWORD *)pSubFile > 2)
1134             {
1135                 if ((flag = atoi(*(pSubFile+3)))) /* ah, flag */
1136                 {
1137                     if (flag & 0x2c)
1138                     FIXME("VNLP_xxx flag %d not handled yet.\n", flag);
1139                 }
1140                 else
1141                 {
1142                     FIXME("temp file name '%s' given. Need to register in wininit.ini !\n", *(pSubFile+3));
1143                     /* we probably need to set VIRTNODE.vhstrDstFinalName to
1144                      * the final destination name, and the temp name is merely
1145                      * the copy destination */
1146                 }
1147             }
1148             VcpQueueCopy16(pSrcFile, pDstFile, pSrcDir, pDstDir, LDID_SRCPATH, ldid ? ldid : 0xffff, 0, VFNL_COPY|flag, 0);
1149             SETUPX_FreeSubStrings(pSubFile);
1150         }
1151     }
1152             
1153     return TRUE;
1154 }
1155
1156 /***********************************************************************
1157  *              GenInstall
1158  *
1159  * generic installer function for .INF file sections
1160  *
1161  * This is not perfect - patch whenever you can !
1162  * 
1163  * wFlags == GENINSTALL_DO_xxx
1164  * e.g. NetMeeting:
1165  * first call GENINSTALL_DO_REGSRCPATH | GENINSTALL_DO_FILES,
1166  * second call GENINSTALL_DO_LOGCONFIG | CFGAUTO | INI2REG | REG | INI
1167  */
1168 RETERR16 WINAPI GenInstall16(HINF16 hInfFile, LPCSTR szInstallSection, WORD wFlags)
1169 {
1170     LPCSTR filename = IP_GetFileName(hInfFile);
1171     LPSTR pEntries, p, pEnd;
1172     DWORD len;
1173     LPSTR *pSub;
1174
1175     FIXME("(%04x, '%s', %04x), semi-stub. Please implement additional operations here !\n", hInfFile, szInstallSection, wFlags);
1176     pEntries = SETUPX_GetSectionEntries(filename, szInstallSection);
1177     if (!pEntries)
1178     {
1179         ERR("couldn't find entries for section '%s' !\n", szInstallSection);
1180         return ERR_IP_SECT_NOT_FOUND;
1181     }
1182     for (p=pEntries; *p; p +=strlen(p)+1)
1183     {
1184         pEnd = strchr(p, '=');
1185         if (!pEnd) continue;
1186         pSub = SETUPX_GetSubStrings(pEnd+1, ','); /* split entries after the '=' */
1187         SETUPX_IsolateSubString(&p, &pEnd);
1188         len = (int)pEnd - (int)p;
1189
1190         if (wFlags & GENINSTALL_DO_FILES)
1191         {
1192             if (!strncasecmp(p, "CopyFiles", len))
1193             {
1194                 SETUPX_CopyFiles(pSub, hInfFile);
1195                 continue;
1196             }
1197 #if IMPLEMENT_THAT
1198             else
1199             if (!strncasecmp(p, "DelFiles", len))
1200             {
1201                 SETUPX_DelFiles(filename, szInstallSection, pSub);
1202                 continue;
1203             }
1204 #endif
1205         }
1206         if (wFlags & GENINSTALL_DO_INI)
1207         {
1208 #if IMPLEMENT_THAT
1209             if (!strncasecmp(p, "UpdateInis", len))
1210             {
1211                 SETUPX_UpdateInis(filename, szInstallSection, pSub);
1212                 continue;
1213             }
1214 #endif
1215         }
1216         if (wFlags & GENINSTALL_DO_REG)
1217         {
1218 #if IMPLEMENT_THAT
1219             /* probably use SUReg*() functions here */
1220             if (!strncasecmp(p, "AddReg", len))
1221             {
1222                 SETUPX_AddReg(filename, szInstallSection, pSub);
1223                 continue;
1224             }
1225             else
1226             if (!strncasecmp(p, "DelReg", len))
1227             {
1228                 SETUPX_DelReg(filename, szInstallSection, pSub);
1229                 continue;
1230             }
1231 #endif
1232         }
1233         
1234         SETUPX_FreeSubStrings(pSub);
1235     }
1236     HeapFree(GetProcessHeap(), 0, pEntries);
1237     return OK;
1238 }