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