advpack: Cast-qual warnings fix.
[wine] / dlls / advpack / files.c
1 /*
2  * Advpack file functions
3  *
4  * Copyright 2006 James Hawkins
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <stdlib.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "winver.h"
29 #include "winternl.h"
30 #include "setupapi.h"
31 #include "advpub.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34 #include "advpack_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(advpack);
37
38 /* converts an ansi double null-terminated list to a unicode list */
39 static LPWSTR ansi_to_unicode_list(LPCSTR ansi_list)
40 {
41     DWORD len, wlen = 0;
42     LPWSTR list;
43     LPCSTR ptr = ansi_list;
44
45     while (*ptr) ptr += lstrlenA(ptr) + 1;
46     len = ptr + 1 - ansi_list;
47     wlen = MultiByteToWideChar(CP_ACP, 0, ansi_list, len, NULL, 0);
48     list = HeapAlloc(GetProcessHeap(), 0, wlen * sizeof(WCHAR));
49     MultiByteToWideChar(CP_ACP, 0, ansi_list, len, list, wlen);
50     return list;
51 }
52
53 /***********************************************************************
54  *      AddDelBackupEntryA (ADVPACK.@)
55  *
56  * See AddDelBackupEntryW.
57  */
58 HRESULT WINAPI AddDelBackupEntryA(LPCSTR lpcszFileList, LPCSTR lpcszBackupDir,
59                                   LPCSTR lpcszBaseName, DWORD dwFlags)
60 {
61     UNICODE_STRING backupdir, basename;
62     LPWSTR filelist, backup;
63     HRESULT res;
64
65     TRACE("(%s, %s, %s, %ld)\n", debugstr_a(lpcszFileList),
66           debugstr_a(lpcszBackupDir), debugstr_a(lpcszBaseName), dwFlags);
67
68     if (lpcszFileList)
69         filelist = ansi_to_unicode_list(lpcszFileList);
70     else
71         filelist = NULL;
72
73     RtlCreateUnicodeStringFromAsciiz(&backupdir, lpcszBackupDir);
74     RtlCreateUnicodeStringFromAsciiz(&basename, lpcszBaseName);
75
76     if (lpcszBackupDir)
77         backup = backupdir.Buffer;
78     else
79         backup = NULL;
80
81     res = AddDelBackupEntryW(filelist, backup, basename.Buffer, dwFlags);
82
83     HeapFree(GetProcessHeap(), 0, filelist);
84
85     RtlFreeUnicodeString(&backupdir);
86     RtlFreeUnicodeString(&basename);
87
88     return res;
89 }
90
91 /***********************************************************************
92  *      AddDelBackupEntryW (ADVPACK.@)
93  *
94  * Either appends the files in the file list to the backup section of
95  * the specified INI, or deletes the entries from the INI file.
96  *
97  * PARAMS
98  *   lpcszFileList  [I] NULL-separated list of filenames.
99  *   lpcszBackupDir [I] Path of the backup directory.
100  *   lpcszBaseName  [I] Basename of the INI file.
101  *   dwFlags        [I] AADBE_ADD_ENTRY adds the entries in the file list
102  *                      to the INI file, while AADBE_DEL_ENTRY removes
103  *                      the entries from the INI file.
104  *
105  * RETURNS
106  *   S_OK in all cases.
107  *
108  * NOTES
109  *   If the INI file does not exist before adding entries to it, the file
110  *   will be created.
111  * 
112  *   If lpcszBackupDir is NULL, the INI file is assumed to exist in
113  *   c:\windows or created there if it does not exist.
114  */
115 HRESULT WINAPI AddDelBackupEntryW(LPCWSTR lpcszFileList, LPCWSTR lpcszBackupDir,
116                                   LPCWSTR lpcszBaseName, DWORD dwFlags)
117 {
118     WCHAR szIniPath[MAX_PATH];
119     LPCWSTR szString = NULL;
120
121     static const WCHAR szBackupEntry[] = {
122         '-','1',',','0',',','0',',','0',',','0',',','0',',','-','1',0
123     };
124     
125     static const WCHAR backslash[] = {'\\',0};
126     static const WCHAR ini[] = {'.','i','n','i',0};
127     static const WCHAR backup[] = {'b','a','c','k','u','p',0};
128
129     TRACE("(%s, %s, %s, %ld)\n", debugstr_w(lpcszFileList),
130           debugstr_w(lpcszBackupDir), debugstr_w(lpcszBaseName), dwFlags);
131
132     if (!lpcszFileList || !*lpcszFileList)
133         return S_OK;
134
135     if (lpcszBackupDir)
136         lstrcpyW(szIniPath, lpcszBackupDir);
137     else
138         GetWindowsDirectoryW(szIniPath, MAX_PATH);
139
140     lstrcatW(szIniPath, backslash);
141     lstrcatW(szIniPath, lpcszBaseName);
142     lstrcatW(szIniPath, ini);
143
144     SetFileAttributesW(szIniPath, FILE_ATTRIBUTE_NORMAL);
145
146     if (dwFlags & AADBE_ADD_ENTRY)
147         szString = szBackupEntry;
148     else if (dwFlags & AADBE_DEL_ENTRY)
149         szString = NULL;
150
151     /* add or delete the INI entries */
152     while (*lpcszFileList)
153     {
154         WritePrivateProfileStringW(backup, lpcszFileList, szString, szIniPath);
155         lpcszFileList += lstrlenW(lpcszFileList) + 1;
156     }
157
158     /* hide the INI file */
159     SetFileAttributesW(szIniPath, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN);
160
161     return S_OK;
162 }
163
164 /* FIXME: this is only for the local case, X:\ */
165 #define ROOT_LENGTH 3
166
167 UINT CALLBACK pQuietQueueCallback(PVOID Context, UINT Notification,
168                                   UINT_PTR Param1, UINT_PTR Param2)
169 {
170     return 1;
171 }
172
173 UINT CALLBACK pQueueCallback(PVOID Context, UINT Notification,
174                              UINT_PTR Param1, UINT_PTR Param2)
175 {
176     /* only be verbose for error notifications */
177     if (!Notification ||
178         Notification == SPFILENOTIFY_RENAMEERROR ||
179         Notification == SPFILENOTIFY_DELETEERROR ||
180         Notification == SPFILENOTIFY_COPYERROR)
181     {
182         return SetupDefaultQueueCallbackW(Context, Notification,
183                                           Param1, Param2);
184     }
185
186     return 1;
187 }
188
189 /***********************************************************************
190  *      AdvInstallFileA (ADVPACK.@)
191  *
192  * See AdvInstallFileW.
193  */
194 HRESULT WINAPI AdvInstallFileA(HWND hwnd, LPCSTR lpszSourceDir, LPCSTR lpszSourceFile,
195                                LPCSTR lpszDestDir, LPCSTR lpszDestFile,
196                                DWORD dwFlags, DWORD dwReserved)
197 {
198     UNICODE_STRING sourcedir, sourcefile;
199     UNICODE_STRING destdir, destfile;
200     HRESULT res;
201
202     TRACE("(%p, %s, %s, %s, %s, %ld, %ld)\n", hwnd, debugstr_a(lpszSourceDir),
203           debugstr_a(lpszSourceFile), debugstr_a(lpszDestDir),
204           debugstr_a(lpszDestFile), dwFlags, dwReserved);
205
206     if (!lpszSourceDir || !lpszSourceFile || !lpszDestDir)
207         return E_INVALIDARG;
208
209     RtlCreateUnicodeStringFromAsciiz(&sourcedir, lpszSourceDir);
210     RtlCreateUnicodeStringFromAsciiz(&sourcefile, lpszSourceFile);
211     RtlCreateUnicodeStringFromAsciiz(&destdir, lpszDestDir);
212     RtlCreateUnicodeStringFromAsciiz(&destfile, lpszDestFile);
213
214     res = AdvInstallFileW(hwnd, sourcedir.Buffer, sourcefile.Buffer,
215                           destdir.Buffer, destfile.Buffer, dwFlags, dwReserved);
216
217     RtlFreeUnicodeString(&sourcedir);
218     RtlFreeUnicodeString(&sourcefile);
219     RtlFreeUnicodeString(&destdir);
220     RtlFreeUnicodeString(&destfile);
221
222     return res;
223 }
224
225 /***********************************************************************
226  *      AdvInstallFileW (ADVPACK.@)
227  *
228  * Copies a file from the source to a destination.
229  *
230  * PARAMS
231  *   hwnd           [I] Handle to the window used for messages.
232  *   lpszSourceDir  [I] Source directory.
233  *   lpszSourceFile [I] Source filename.
234  *   lpszDestDir    [I] Destination directory.
235  *   lpszDestFile   [I] Optional destination filename.
236  *   dwFlags        [I] See advpub.h.
237  *   dwReserved     [I] Reserved.  Must be 0.
238  *
239  * RETURNS
240  *   Success: S_OK.
241  *   Failure: E_FAIL.
242  *
243  * NOTES
244  *   If lpszDestFile is NULL, the destination filename is the same as
245  *   lpszSourceFIle.
246  */
247 HRESULT WINAPI AdvInstallFileW(HWND hwnd, LPCWSTR lpszSourceDir, LPCWSTR lpszSourceFile,
248                                LPCWSTR lpszDestDir, LPCWSTR lpszDestFile,
249                                DWORD dwFlags, DWORD dwReserved)
250 {
251     PSP_FILE_CALLBACK_W pFileCallback;
252     LPWSTR szDestFilename;
253     LPCWSTR szPath;
254     WCHAR szRootPath[ROOT_LENGTH];
255     DWORD dwLen, dwLastError;
256     HSPFILEQ fileQueue;
257     PVOID pContext;
258
259     TRACE("(%p, %s, %s, %s, %s, %ld, %ld)\n", hwnd, debugstr_w(lpszSourceDir),
260           debugstr_w(lpszSourceFile), debugstr_w(lpszDestDir),
261           debugstr_w(lpszDestFile), dwFlags, dwReserved);
262
263     if (!lpszSourceDir || !lpszSourceFile || !lpszDestDir)
264         return E_INVALIDARG;
265         
266     fileQueue = SetupOpenFileQueue();
267     if (fileQueue == INVALID_HANDLE_VALUE)
268         return HRESULT_FROM_WIN32(GetLastError());
269
270     pContext = NULL;
271     dwLastError = ERROR_SUCCESS;
272
273     lstrcpynW(szRootPath, lpszSourceDir, ROOT_LENGTH);
274     szPath = lpszSourceDir + ROOT_LENGTH;
275
276     /* use lpszSourceFile as destination filename if lpszDestFile is NULL */
277     if (lpszDestFile)
278     {
279         dwLen = lstrlenW(lpszDestFile);
280         szDestFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
281         lstrcpyW(szDestFilename, lpszDestFile);
282     }
283     else
284     {
285         dwLen = lstrlenW(lpszSourceFile);
286         szDestFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
287         lstrcpyW(szDestFilename, lpszSourceFile);
288     }
289
290     /* add the file copy operation to the setup queue */
291     if (!SetupQueueCopyW(fileQueue, szRootPath, szPath, lpszSourceFile, NULL,
292                          NULL, lpszDestDir, szDestFilename, dwFlags))
293     {
294         dwLastError = GetLastError();
295         goto done;
296     }
297
298     pContext = SetupInitDefaultQueueCallbackEx(hwnd, INVALID_HANDLE_VALUE,
299                                                0, 0, NULL);
300     if (!pContext)
301     {
302         dwLastError = GetLastError();
303         goto done;
304     }
305
306     /* don't output anything for AIF_QUIET */
307     if (dwFlags & AIF_QUIET)
308         pFileCallback = pQuietQueueCallback;
309     else
310         pFileCallback = pQueueCallback;
311
312     /* perform the file copy */
313     if (!SetupCommitFileQueueW(hwnd, fileQueue, pFileCallback, pContext))
314     {
315         dwLastError = GetLastError();
316         goto done;
317     }
318
319 done:
320     SetupTermDefaultQueueCallback(pContext);
321     SetupCloseFileQueue(fileQueue);
322     
323     HeapFree(GetProcessHeap(), 0, szDestFilename);
324     
325     return HRESULT_FROM_WIN32(dwLastError);
326 }
327
328 static HRESULT DELNODE_recurse_dirtree(LPWSTR fname, DWORD flags)
329 {
330     DWORD fattrs = GetFileAttributesW(fname);
331     HRESULT ret = E_FAIL;
332
333     static const WCHAR backslash[] = {'\\',0};
334     static const WCHAR asterisk[] = {'*',0};
335     static const WCHAR dot[] = {'.',0};
336     static const WCHAR dotdot[] = {'.','.',0};
337
338     if (fattrs & FILE_ATTRIBUTE_DIRECTORY)
339     {
340         HANDLE hFindFile;
341         WIN32_FIND_DATAW w32fd;
342         BOOL done = TRUE;
343         int fname_len = lstrlenW(fname);
344
345         /* Generate a path with wildcard suitable for iterating */
346         if (lstrcmpW(CharPrevW(fname, fname + fname_len), backslash))
347         {
348             lstrcpyW(fname + fname_len, backslash);
349             ++fname_len;
350         }
351         lstrcpyW(fname + fname_len, asterisk);
352
353         if ((hFindFile = FindFirstFileW(fname, &w32fd)) != INVALID_HANDLE_VALUE)
354         {
355             /* Iterate through the files in the directory */
356             for (done = FALSE; !done; done = !FindNextFileW(hFindFile, &w32fd))
357             {
358                 TRACE("%s\n", debugstr_w(w32fd.cFileName));
359                 if (lstrcmpW(dot, w32fd.cFileName) != 0 &&
360                     lstrcmpW(dotdot, w32fd.cFileName) != 0)
361                 {
362                     lstrcpyW(fname + fname_len, w32fd.cFileName);
363                     if (DELNODE_recurse_dirtree(fname, flags) != S_OK)
364                     {
365                         break; /* Failure */
366                     }
367                 }
368             }
369             FindClose(hFindFile);
370         }
371
372         /* We're done with this directory, so restore the old path without wildcard */
373         *(fname + fname_len) = '\0';
374
375         if (done)
376         {
377             TRACE("%s: directory\n", debugstr_w(fname));
378             if (SetFileAttributesW(fname, FILE_ATTRIBUTE_NORMAL) && RemoveDirectoryW(fname))
379             {
380                 ret = S_OK;
381             }
382         }
383     }
384     else
385     {
386         TRACE("%s: file\n", debugstr_w(fname));
387         if (SetFileAttributesW(fname, FILE_ATTRIBUTE_NORMAL) && DeleteFileW(fname))
388         {
389             ret = S_OK;
390         }
391     }
392     
393     return ret;
394 }
395
396 /***********************************************************************
397  *              DelNodeA   (ADVPACK.@)
398  *
399  * See DelNodeW.
400  */
401 HRESULT WINAPI DelNodeA(LPCSTR pszFileOrDirName, DWORD dwFlags)
402 {
403     UNICODE_STRING fileordirname;
404     HRESULT res;
405
406     TRACE("(%s, %ld)\n", debugstr_a(pszFileOrDirName), dwFlags);
407
408     RtlCreateUnicodeStringFromAsciiz(&fileordirname, pszFileOrDirName);
409
410     res = DelNodeW(fileordirname.Buffer, dwFlags);
411
412     RtlFreeUnicodeString(&fileordirname);
413
414     return res;
415 }
416
417 /***********************************************************************
418  *              DelNodeW   (ADVPACK.@)
419  *
420  * Deletes a file or directory
421  *
422  * PARAMS
423  *   pszFileOrDirName   [I] Name of file or directory to delete
424  *   dwFlags            [I] Flags; see include/advpub.h
425  *
426  * RETURNS 
427  *   Success: S_OK
428  *   Failure: E_FAIL
429  *
430  * BUGS
431  *   - Ignores flags
432  *   - Native version apparently does a lot of checking to make sure
433  *     we're not trying to delete a system directory etc.
434  */
435 HRESULT WINAPI DelNodeW(LPCWSTR pszFileOrDirName, DWORD dwFlags)
436 {
437     WCHAR fname[MAX_PATH];
438     HRESULT ret = E_FAIL;
439     
440     TRACE("(%s, %ld)\n", debugstr_w(pszFileOrDirName), dwFlags);
441     
442     if (dwFlags)
443         FIXME("Flags ignored!\n");
444
445     if (pszFileOrDirName && *pszFileOrDirName)
446     {
447         lstrcpyW(fname, pszFileOrDirName);
448
449         /* TODO: Should check for system directory deletion etc. here */
450
451         ret = DELNODE_recurse_dirtree(fname, dwFlags);
452     }
453
454     return ret;
455 }
456
457 /***********************************************************************
458  *             DelNodeRunDLL32A   (ADVPACK.@)
459  *
460  * See DelNodeRunDLL32W.
461  */
462 HRESULT WINAPI DelNodeRunDLL32A(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
463 {
464     UNICODE_STRING params;
465     HRESULT hr;
466
467     TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_a(cmdline), show);
468
469     RtlCreateUnicodeStringFromAsciiz(&params, cmdline);
470
471     hr = DelNodeRunDLL32W(hWnd, hInst, params.Buffer, show);
472
473     RtlFreeUnicodeString(&params);
474
475     return hr;
476 }
477
478 /***********************************************************************
479  *             DelNodeRunDLL32W   (ADVPACK.@)
480  *
481  * Deletes a file or directory, WinMain style.
482  *
483  * PARAMS
484  *   hWnd    [I] Handle to the window used for the display.
485  *   hInst   [I] Instance of the process.
486  *   cmdline [I] Contains parameters in the order FileOrDirName,Flags.
487  *   show    [I] How the window should be shown.
488  *
489  * RETURNS
490  *   Success: S_OK.
491  *   Failure: E_FAIL.
492  */
493 HRESULT WINAPI DelNodeRunDLL32W(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
494 {
495     LPWSTR szFilename, szFlags;
496     LPWSTR cmdline_copy, cmdline_ptr;
497     DWORD dwFlags = 0;
498     HRESULT res;
499
500     TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_w(cmdline), show);
501
502     cmdline_copy = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline) + 1) * sizeof(WCHAR));
503     cmdline_ptr = cmdline_copy;
504     lstrcpyW(cmdline_copy, cmdline);
505
506     /* get the parameters at indexes 0 and 1 respectively */
507     szFilename = get_parameter(&cmdline_ptr, ',');
508     szFlags = get_parameter(&cmdline_ptr, ',');
509
510     if (szFlags)
511         dwFlags = atolW(szFlags);
512
513     res = DelNodeW(szFilename, dwFlags);
514
515     HeapFree(GetProcessHeap(), 0, cmdline_copy);
516
517     return res;
518 }
519
520 /* The following defintions were copied from dlls/cabinet/cabinet.h */
521
522 /* EXTRACTdest flags */
523 #define EXTRACT_FILLFILELIST  0x00000001
524 #define EXTRACT_EXTRACTFILES  0x00000002
525
526 struct ExtractFileList {
527         LPSTR  filename;
528         struct ExtractFileList *next;
529         BOOL   unknown;  /* always 1L */
530 } ;
531
532 /* the first parameter of the function Extract */
533 typedef struct {
534         long  result1;          /* 0x000 */
535         long  unknown1[3];      /* 0x004 */
536         struct ExtractFileList *filelist; /* 0x010 */
537         long  filecount;        /* 0x014 */
538         DWORD flags;            /* 0x018 */
539         char  directory[0x104]; /* 0x01c */
540         char  lastfile[0x20c];  /* 0x120 */
541 } EXTRACTdest;
542
543 static HRESULT (WINAPI *pExtract)(EXTRACTdest*, LPCSTR);
544
545 /* removes legal characters before and after file list, and
546  * converts the file list to a NULL-separated list
547  */
548 static LPSTR convert_file_list(LPCSTR FileList, DWORD *dwNumFiles)
549 {
550     DWORD dwLen;
551     char *first = (char *)FileList;
552     char *last = (char *)FileList + strlen(FileList) - 1;
553     LPSTR szConvertedList, temp;
554     
555     /* any number of these chars before the list is OK */
556     while (first < last && (*first == ' ' || *first == '\t' || *first == ':'))
557         first++;
558
559     /* any number of these chars after the list is OK */
560     while (last > first && (*last == ' ' || *last == '\t' || *last == ':'))
561         last--;
562
563     if (first == last)
564         return NULL;
565
566     dwLen = last - first + 3; /* room for double-null termination */
567     szConvertedList = HeapAlloc(GetProcessHeap(), 0, dwLen);
568     lstrcpynA(szConvertedList, first, dwLen - 1);
569
570     szConvertedList[dwLen - 1] = '\0';
571     szConvertedList[dwLen] = '\0';
572
573     /* empty list */
574     if (!lstrlenA(szConvertedList))
575         return NULL;
576         
577     *dwNumFiles = 1;
578
579     /* convert the colons to double-null termination */
580     temp = szConvertedList;
581     while (*temp)
582     {
583         if (*temp == ':')
584         {
585             *temp = '\0';
586             (*dwNumFiles)++;
587         }
588
589         temp++;
590     }
591
592     return szConvertedList;
593 }
594
595 static void free_file_node(struct ExtractFileList *pNode)
596 {
597     HeapFree(GetProcessHeap(), 0, pNode->filename);
598     HeapFree(GetProcessHeap(), 0, pNode);
599 }
600
601 /* determines whether szFile is in the NULL-separated szFileList */
602 static BOOL file_in_list(LPSTR szFile, LPSTR szFileList)
603 {
604     DWORD dwLen = lstrlenA(szFile);
605     DWORD dwTestLen;
606
607     while (*szFileList)
608     {
609         dwTestLen = lstrlenA(szFileList);
610
611         if (dwTestLen == dwLen)
612         {
613             if (!lstrcmpiA(szFile, szFileList))
614                 return TRUE;
615         }
616
617         szFileList += dwTestLen + 1;
618     }
619
620     return FALSE;
621 }
622
623 /* removes nodes from the linked list that aren't specified in szFileList
624  * returns the number of files that are in both the linked list and szFileList
625  */
626 static DWORD fill_file_list(EXTRACTdest *extractDest, LPCSTR szCabName, LPSTR szFileList)
627 {
628     DWORD dwNumFound = 0;
629     struct ExtractFileList *pNode;
630     struct ExtractFileList *prev = NULL;
631
632     extractDest->flags |= EXTRACT_FILLFILELIST;
633     if (pExtract(extractDest, szCabName))
634     {
635         extractDest->flags &= ~EXTRACT_FILLFILELIST;
636         return -1;
637     }
638
639     pNode = extractDest->filelist;
640     while (pNode)
641     {
642         if (file_in_list(pNode->filename, szFileList))
643         {
644             prev = pNode;
645             pNode = pNode->next;
646             dwNumFound++;
647         }
648         else if (prev)
649         {
650             prev->next = pNode->next;
651             free_file_node(pNode);
652             pNode = prev->next;
653         }
654         else
655         {
656             extractDest->filelist = pNode->next;
657             free_file_node(pNode);
658             pNode = extractDest->filelist;
659         }
660     }
661
662     extractDest->flags &= ~EXTRACT_FILLFILELIST;
663     return dwNumFound;
664 }
665
666 /***********************************************************************
667  *             ExtractFilesA    (ADVPACK.@)
668  *
669  * Extracts the specified files from a cab archive into
670  * a destination directory.
671  *
672  * PARAMS
673  *   CabName   [I] Filename of the cab archive.
674  *   ExpandDir [I] Destination directory for the extracted files.
675  *   Flags     [I] Reserved.
676  *   FileList  [I] Optional list of files to extract.  See NOTES.
677  *   LReserved [I] Reserved.  Must be NULL.
678  *   Reserved  [I] Reserved.  Must be 0.
679  *
680  * RETURNS
681  *   Success: S_OK.
682  *   Failure: E_FAIL.
683  *
684  * NOTES
685  *   FileList is a colon-separated list of filenames.  If FileList is
686  *   non-NULL, only the files in the list will be extracted from the
687  *   cab file, otherwise all files will be extracted.  Any number of
688  *   spaces, tabs, or colons can be before or after the list, but
689  *   the list itself must only be separated by colons.
690  */
691 HRESULT WINAPI ExtractFilesA(LPCSTR CabName, LPCSTR ExpandDir, DWORD Flags,
692                              LPCSTR FileList, LPVOID LReserved, DWORD Reserved)
693 {   
694     EXTRACTdest extractDest;
695     HMODULE hCabinet;
696     HRESULT res = S_OK;
697     DWORD dwFileCount = 0;
698     DWORD dwFilesFound = 0;
699     LPSTR szConvertedList = NULL;
700
701     TRACE("(%s, %s, %ld, %s, %p, %ld)\n", debugstr_a(CabName), debugstr_a(ExpandDir),
702           Flags, debugstr_a(FileList), LReserved, Reserved);
703
704     if (!CabName || !ExpandDir)
705         return E_INVALIDARG;
706
707     if (GetFileAttributesA(ExpandDir) == INVALID_FILE_ATTRIBUTES)
708         return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
709
710     hCabinet = LoadLibraryA("cabinet.dll");
711     if (!hCabinet)
712         return E_FAIL;
713
714     pExtract = (void *)GetProcAddress(hCabinet, "Extract");
715     if (!pExtract)
716     {
717         res = E_FAIL;
718         goto done;
719     }
720
721     ZeroMemory(&extractDest, sizeof(EXTRACTdest));
722     lstrcpyA(extractDest.directory, ExpandDir);
723
724     if (FileList)
725     {
726         szConvertedList = convert_file_list(FileList, &dwFileCount);
727         if (!szConvertedList || dwFileCount == -1)
728         {
729             res = E_FAIL;
730             goto done;
731         }
732
733         dwFilesFound = fill_file_list(&extractDest, CabName, szConvertedList);
734         if (dwFilesFound != dwFileCount)
735         {
736             res = E_FAIL;
737             goto done;
738         }
739     }
740     else
741         extractDest.flags |= EXTRACT_FILLFILELIST;
742
743     extractDest.flags |= EXTRACT_EXTRACTFILES;
744     res = pExtract(&extractDest, CabName);
745
746 done:
747     FreeLibrary(hCabinet);
748     HeapFree(GetProcessHeap(), 0, szConvertedList);
749
750     return res;
751 }
752
753 /***********************************************************************
754  *      FileSaveMarkNotExistA (ADVPACK.@)
755  *
756  * See FileSaveMarkNotExistW.
757  */
758 HRESULT WINAPI FileSaveMarkNotExistA(LPSTR pszFileList, LPSTR pszDir, LPSTR pszBaseName)
759 {
760     TRACE("(%s, %s, %s)\n", debugstr_a(pszFileList),
761           debugstr_a(pszDir), debugstr_a(pszBaseName));
762
763     return AddDelBackupEntryA(pszFileList, pszDir, pszBaseName, AADBE_DEL_ENTRY);
764 }
765
766 /***********************************************************************
767  *      FileSaveMarkNotExistW (ADVPACK.@)
768  *
769  * Marks the files in the file list as not existing so they won't be
770  * backed up during a save.
771  *
772  * PARAMS
773  *   pszFileList [I] NULL-separated list of filenames.
774  *   pszDir      [I] Path of the backup directory.
775  *   pszBaseName [I] Basename of the INI file.
776  *
777  * RETURNS
778  *   Success: S_OK.
779  *   Failure: E_FAIL.
780  */
781 HRESULT WINAPI FileSaveMarkNotExistW(LPWSTR pszFileList, LPWSTR pszDir, LPWSTR pszBaseName)
782 {
783     TRACE("(%s, %s, %s)\n", debugstr_w(pszFileList),
784           debugstr_w(pszDir), debugstr_w(pszBaseName));
785
786     return AddDelBackupEntryW(pszFileList, pszDir, pszBaseName, AADBE_DEL_ENTRY);
787 }
788
789 /***********************************************************************
790  *      FileSaveRestoreA (ADVPACK.@)
791  *
792  * See FileSaveRestoreW.
793  */
794 HRESULT WINAPI FileSaveRestoreA(HWND hDlg, LPSTR pszFileList, LPSTR pszDir,
795                                 LPSTR pszBaseName, DWORD dwFlags)
796 {
797     UNICODE_STRING filelist, dir, basename;
798     HRESULT hr;
799
800     TRACE("(%p, %s, %s, %s, %ld)\n", hDlg, debugstr_a(pszFileList),
801           debugstr_a(pszDir), debugstr_a(pszBaseName), dwFlags);
802
803     RtlCreateUnicodeStringFromAsciiz(&filelist, pszFileList);
804     RtlCreateUnicodeStringFromAsciiz(&dir, pszDir);
805     RtlCreateUnicodeStringFromAsciiz(&basename, pszBaseName);
806
807     hr = FileSaveRestoreW(hDlg, filelist.Buffer, dir.Buffer,
808                           basename.Buffer, dwFlags);
809
810     RtlFreeUnicodeString(&filelist);
811     RtlFreeUnicodeString(&dir);
812     RtlFreeUnicodeString(&basename);
813
814     return hr;
815 }                         
816
817 /***********************************************************************
818  *      FileSaveRestoreW (ADVPACK.@)
819  *
820  * Saves or restores the files in the specified file list.
821  *
822  * PARAMS
823  *   hDlg        [I] Handle to the dialog used for the display.
824  *   pszFileList [I] NULL-separated list of filenames.
825  *   pszDir      [I] Path of the backup directory.
826  *   pszBaseName [I] Basename of the backup files.
827  *   dwFlags     [I] See advpub.h.
828  *
829  * RETURNS
830  *   Success: S_OK.
831  *   Failure: E_FAIL.
832  *
833  * NOTES
834  *   If pszFileList is NULL on restore, all files will be restored.
835  *
836  * BUGS
837  *   Unimplemented.
838  */
839 HRESULT WINAPI FileSaveRestoreW(HWND hDlg, LPWSTR pszFileList, LPWSTR pszDir,
840                                 LPWSTR pszBaseName, DWORD dwFlags)
841 {
842     FIXME("(%p, %s, %s, %s, %ld) stub\n", hDlg, debugstr_w(pszFileList),
843           debugstr_w(pszDir), debugstr_w(pszBaseName), dwFlags);
844
845     return E_FAIL;
846 }
847
848 /***********************************************************************
849  *      FileSaveRestoreOnINFA (ADVPACK.@)
850  *
851  * See FileSaveRestoreOnINFW.
852  */
853 HRESULT WINAPI FileSaveRestoreOnINFA(HWND hWnd, LPCSTR pszTitle, LPCSTR pszINF,
854                                     LPCSTR pszSection, LPCSTR pszBackupDir,
855                                     LPCSTR pszBaseBackupFile, DWORD dwFlags)
856 {
857     UNICODE_STRING title, inf, section;
858     UNICODE_STRING backupdir, backupfile;
859     HRESULT hr;
860
861     TRACE("(%p, %s, %s, %s, %s, %s, %ld)\n", hWnd, debugstr_a(pszTitle),
862           debugstr_a(pszINF), debugstr_a(pszSection), debugstr_a(pszBackupDir),
863           debugstr_a(pszBaseBackupFile), dwFlags);
864
865     RtlCreateUnicodeStringFromAsciiz(&title, pszTitle);
866     RtlCreateUnicodeStringFromAsciiz(&inf, pszINF);
867     RtlCreateUnicodeStringFromAsciiz(&section, pszSection);
868     RtlCreateUnicodeStringFromAsciiz(&backupdir, pszBackupDir);
869     RtlCreateUnicodeStringFromAsciiz(&backupfile, pszBaseBackupFile);
870
871     hr = FileSaveRestoreOnINFW(hWnd, title.Buffer, inf.Buffer, section.Buffer,
872                                backupdir.Buffer, backupfile.Buffer, dwFlags);
873
874     RtlFreeUnicodeString(&title);
875     RtlFreeUnicodeString(&inf);
876     RtlFreeUnicodeString(&section);
877     RtlFreeUnicodeString(&backupdir);
878     RtlFreeUnicodeString(&backupfile);
879
880     return hr;
881 }
882
883 /***********************************************************************
884  *      FileSaveRestoreOnINFW (ADVPACK.@)
885  *
886  *
887  * PARAMS
888  *   hWnd              [I] Handle to the window used for the display.
889  *   pszTitle          [I] Title of the window.
890  *   pszINF            [I] Fully-qualified INF filename.
891  *   pszSection        [I] GenInstall INF section name.
892  *   pszBackupDir      [I] Directory to store the backup file.
893  *   pszBaseBackupFile [I] Basename of the backup files.
894  *   dwFlags           [I] See advpub.h
895  *
896  * RETURNS
897  *   Success: S_OK.
898  *   Failure: E_FAIL.
899  *
900  * NOTES
901  *   If pszSection is NULL, the default section will be used.
902  *
903  * BUGS
904  *   Unimplemented.
905  */
906 HRESULT WINAPI FileSaveRestoreOnINFW(HWND hWnd, LPCWSTR pszTitle, LPCWSTR pszINF,
907                                      LPCWSTR pszSection, LPCWSTR pszBackupDir,
908                                      LPCWSTR pszBaseBackupFile, DWORD dwFlags)
909 {
910     FIXME("(%p, %s, %s, %s, %s, %s, %ld): stub\n", hWnd, debugstr_w(pszTitle),
911           debugstr_w(pszINF), debugstr_w(pszSection), debugstr_w(pszBackupDir),
912           debugstr_w(pszBaseBackupFile), dwFlags);
913
914     return E_FAIL;
915 }
916
917 /***********************************************************************
918  *             GetVersionFromFileA     (ADVPACK.@)
919  *
920  * See GetVersionFromFileExW.
921  */
922 HRESULT WINAPI GetVersionFromFileA(LPCSTR Filename, LPDWORD MajorVer,
923                                    LPDWORD MinorVer, BOOL Version )
924 {
925     TRACE("(%s, %p, %p, %d)\n", debugstr_a(Filename), MajorVer, MinorVer, Version);
926     return GetVersionFromFileExA(Filename, MajorVer, MinorVer, Version);
927 }
928
929 /***********************************************************************
930  *             GetVersionFromFileW     (ADVPACK.@)
931  *
932  * See GetVersionFromFileExW.
933  */
934 HRESULT WINAPI GetVersionFromFileW(LPCWSTR Filename, LPDWORD MajorVer,
935                                    LPDWORD MinorVer, BOOL Version )
936 {
937     TRACE("(%s, %p, %p, %d)\n", debugstr_w(Filename), MajorVer, MinorVer, Version);
938     return GetVersionFromFileExW(Filename, MajorVer, MinorVer, Version);
939 }
940
941 /* data for GetVersionFromFileEx */
942 typedef struct tagLANGANDCODEPAGE
943 {
944     WORD wLanguage;
945     WORD wCodePage;
946 } LANGANDCODEPAGE;
947
948 /***********************************************************************
949  *             GetVersionFromFileExA   (ADVPACK.@)
950  *
951  * See GetVersionFromFileExW.
952  */
953 HRESULT WINAPI GetVersionFromFileExA(LPCSTR lpszFilename, LPDWORD pdwMSVer,
954                                      LPDWORD pdwLSVer, BOOL bVersion )
955 {
956     UNICODE_STRING filename;
957     HRESULT res;
958
959     TRACE("(%s, %p, %p, %d)\n", debugstr_a(lpszFilename),
960           pdwMSVer, pdwLSVer, bVersion);
961
962     RtlCreateUnicodeStringFromAsciiz(&filename, lpszFilename);
963
964     res = GetVersionFromFileExW(filename.Buffer, pdwMSVer, pdwLSVer, bVersion);
965
966     RtlFreeUnicodeString(&filename);
967
968     return res;
969 }
970
971 /***********************************************************************
972  *             GetVersionFromFileExW   (ADVPACK.@)
973  *
974  * Gets the files version or language information.
975  *
976  * PARAMS
977  *   lpszFilename [I] The file to get the info from.
978  *   pdwMSVer     [O] Major version.
979  *   pdwLSVer     [O] Minor version.
980  *   bVersion     [I] Whether to retrieve version or language info.
981  *
982  * RETURNS
983  *   Always returns S_OK.
984  *
985  * NOTES
986  *   If bVersion is TRUE, version information is retrieved, else
987  *   pdwMSVer gets the language ID and pdwLSVer gets the codepage ID.
988  */
989 HRESULT WINAPI GetVersionFromFileExW(LPCWSTR lpszFilename, LPDWORD pdwMSVer,
990                                      LPDWORD pdwLSVer, BOOL bVersion )
991 {
992     VS_FIXEDFILEINFO *pFixedVersionInfo;
993     LANGANDCODEPAGE *pLangAndCodePage;
994     DWORD dwHandle, dwInfoSize;
995     WCHAR szWinDir[MAX_PATH];
996     WCHAR szFile[MAX_PATH];
997     LPVOID pVersionInfo = NULL;
998     BOOL bFileCopied = FALSE;
999     UINT uValueLen;
1000
1001     static WCHAR backslash[] = {'\\',0};
1002     static WCHAR translation[] = {
1003         '\\','V','a','r','F','i','l','e','I','n','f','o',
1004         '\\','T','r','a','n','s','l','a','t','i','o','n',0
1005     };
1006
1007     TRACE("(%s, %p, %p, %d)\n", debugstr_w(lpszFilename),
1008           pdwMSVer, pdwLSVer, bVersion);
1009
1010     *pdwLSVer = 0;
1011     *pdwMSVer = 0;
1012
1013     lstrcpynW(szFile, lpszFilename, MAX_PATH);
1014
1015     dwInfoSize = GetFileVersionInfoSizeW(szFile, &dwHandle);
1016     if (!dwInfoSize)
1017     {
1018         /* check that the file exists */
1019         if (GetFileAttributesW(szFile) == INVALID_FILE_ATTRIBUTES)
1020             return S_OK;
1021
1022         /* file exists, but won't be found by GetFileVersionInfoSize,
1023         * so copy it to the temp dir where it will be found.
1024         */
1025         GetWindowsDirectoryW(szWinDir, MAX_PATH);
1026         GetTempFileNameW(szWinDir, NULL, 0, szFile);
1027         CopyFileW(lpszFilename, szFile, FALSE);
1028         bFileCopied = TRUE;
1029
1030         dwInfoSize = GetFileVersionInfoSizeW(szFile, &dwHandle);
1031         if (!dwInfoSize)
1032             goto done;
1033     }
1034
1035     pVersionInfo = HeapAlloc(GetProcessHeap(), 0, dwInfoSize);
1036     if (!pVersionInfo)
1037         goto done;
1038
1039     if (!GetFileVersionInfoW(szFile, dwHandle, dwInfoSize, pVersionInfo))
1040         goto done;
1041
1042     if (bVersion)
1043     {
1044         if (!VerQueryValueW(pVersionInfo, backslash,
1045             (LPVOID *)&pFixedVersionInfo, &uValueLen))
1046             goto done;
1047
1048         if (!uValueLen)
1049             goto done;
1050
1051         *pdwMSVer = pFixedVersionInfo->dwFileVersionMS;
1052         *pdwLSVer = pFixedVersionInfo->dwFileVersionLS;
1053     }
1054     else
1055     {
1056         if (!VerQueryValueW(pVersionInfo, translation,
1057              (LPVOID *)&pLangAndCodePage, &uValueLen))
1058             goto done;
1059
1060         if (!uValueLen)
1061             goto done;
1062
1063         *pdwMSVer = pLangAndCodePage->wLanguage;
1064         *pdwLSVer = pLangAndCodePage->wCodePage;
1065     }
1066
1067 done:
1068     HeapFree(GetProcessHeap(), 0, pVersionInfo);
1069
1070     if (bFileCopied)
1071         DeleteFileW(szFile);
1072
1073     return S_OK;
1074 }