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