1 /*
2 * Setupapi miscellaneous functions
3 *
4 * Copyright 2005 Eric Kohl
5 * Copyright 2007 Hans Leidekker
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "setupapi.h"
30 #include "lzexpand.h"
31 #include "softpub.h"
32 #include "mscat.h"
33 #include "shlobj.h"
34
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37
38 #include "setupapi_private.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
41
42 /* arbitrary limit not related to what native actually uses */
43 #define OEM_INDEX_LIMIT 999
44
45 /**************************************************************************
46 * MyFree [SETUPAPI.@]
47 *
48 * Frees an allocated memory block from the process heap.
49 *
50 * PARAMS
51 * lpMem [I] pointer to memory block which will be freed
52 *
53 * RETURNS
54 * None
55 */
56 VOID WINAPI MyFree(LPVOID lpMem)
57 {
58 HeapFree(GetProcessHeap(), 0, lpMem);
59 }
60
61
62 /**************************************************************************
63 * MyMalloc [SETUPAPI.@]
64 *
65 * Allocates memory block from the process heap.
66 *
67 * PARAMS
68 * dwSize [I] size of the allocated memory block
69 *
70 * RETURNS
71 * Success: pointer to allocated memory block
72 * Failure: NULL
73 */
74 LPVOID WINAPI MyMalloc(DWORD dwSize)
75 {
76 return HeapAlloc(GetProcessHeap(), 0, dwSize);
77 }
78
79
80 /**************************************************************************
81 * MyRealloc [SETUPAPI.@]
82 *
83 * Changes the size of an allocated memory block or allocates a memory
84 * block from the process heap.
85 *
86 * PARAMS
87 * lpSrc [I] pointer to memory block which will be resized
88 * dwSize [I] new size of the memory block
89 *
90 * RETURNS
91 * Success: pointer to the resized memory block
92 * Failure: NULL
93 *
94 * NOTES
95 * If lpSrc is a NULL-pointer, then MyRealloc allocates a memory
96 * block like MyMalloc.
97 */
98 LPVOID WINAPI MyRealloc(LPVOID lpSrc, DWORD dwSize)
99 {
100 if (lpSrc == NULL)
101 return HeapAlloc(GetProcessHeap(), 0, dwSize);
102
103 return HeapReAlloc(GetProcessHeap(), 0, lpSrc, dwSize);
104 }
105
106
107 /**************************************************************************
108 * DuplicateString [SETUPAPI.@]
109 *
110 * Duplicates a unicode string.
111 *
112 * PARAMS
113 * lpSrc [I] pointer to the unicode string that will be duplicated
114 *
115 * RETURNS
116 * Success: pointer to the duplicated unicode string
117 * Failure: NULL
118 *
119 * NOTES
120 * Call MyFree() to release the duplicated string.
121 */
122 LPWSTR WINAPI DuplicateString(LPCWSTR lpSrc)
123 {
124 LPWSTR lpDst;
125
126 lpDst = MyMalloc((lstrlenW(lpSrc) + 1) * sizeof(WCHAR));
127 if (lpDst == NULL)
128 return NULL;
129
130 strcpyW(lpDst, lpSrc);
131
132 return lpDst;
133 }
134
135
136 /**************************************************************************
137 * QueryRegistryValue [SETUPAPI.@]
138 *
139 * Retrieves value data from the registry and allocates memory for the
140 * value data.
141 *
142 * PARAMS
143 * hKey [I] Handle of the key to query
144 * lpValueName [I] Name of value under hkey to query
145 * lpData [O] Destination for the values contents,
146 * lpType [O] Destination for the value type
147 * lpcbData [O] Destination for the size of data
148 *
149 * RETURNS
150 * Success: ERROR_SUCCESS
151 * Failure: Otherwise
152 *
153 * NOTES
154 * Use MyFree to release the lpData buffer.
155 */
156 LONG WINAPI QueryRegistryValue(HKEY hKey,
157 LPCWSTR lpValueName,
158 LPBYTE *lpData,
159 LPDWORD lpType,
160 LPDWORD lpcbData)
161 {
162 LONG lError;
163
164 TRACE("%p %s %p %p %p\n",
165 hKey, debugstr_w(lpValueName), lpData, lpType, lpcbData);
166
167 /* Get required buffer size */
168 *lpcbData = 0;
169 lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, NULL, lpcbData);
170 if (lError != ERROR_SUCCESS)
171 return lError;
172
173 /* Allocate buffer */
174 *lpData = MyMalloc(*lpcbData);
175 if (*lpData == NULL)
176 return ERROR_NOT_ENOUGH_MEMORY;
177
178 /* Query registry value */
179 lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, *lpData, lpcbData);
180 if (lError != ERROR_SUCCESS)
181 MyFree(*lpData);
182
183 return lError;
184 }
185
186
187 /**************************************************************************
188 * IsUserAdmin [SETUPAPI.@]
189 *
190 * Checks whether the current user is a member of the Administrators group.
191 *
192 * PARAMS
193 * None
194 *
195 * RETURNS
196 * Success: TRUE
197 * Failure: FALSE
198 */
199 BOOL WINAPI IsUserAdmin(VOID)
200 {
201 TRACE("\n");
202 return IsUserAnAdmin();
203 }
204
205
206 /**************************************************************************
207 * MultiByteToUnicode [SETUPAPI.@]
208 *
209 * Converts a multi-byte string to a Unicode string.
210 *
211 * PARAMS
212 * lpMultiByteStr [I] Multi-byte string to be converted
213 * uCodePage [I] Code page
214 *
215 * RETURNS
216 * Success: pointer to the converted Unicode string
217 * Failure: NULL
218 *
219 * NOTE
220 * Use MyFree to release the returned Unicode string.
221 */
222 LPWSTR WINAPI MultiByteToUnicode(LPCSTR lpMultiByteStr, UINT uCodePage)
223 {
224 LPWSTR lpUnicodeStr;
225 int nLength;
226
227 nLength = MultiByteToWideChar(uCodePage, 0, lpMultiByteStr,
228 -1, NULL, 0);
229 if (nLength == 0)
230 return NULL;
231
232 lpUnicodeStr = MyMalloc(nLength * sizeof(WCHAR));
233 if (lpUnicodeStr == NULL)
234 return NULL;
235
236 if (!MultiByteToWideChar(uCodePage, 0, lpMultiByteStr,
237 nLength, lpUnicodeStr, nLength))
238 {
239 MyFree(lpUnicodeStr);
240 return NULL;
241 }
242
243 return lpUnicodeStr;
244 }
245
246
247 /**************************************************************************
248 * UnicodeToMultiByte [SETUPAPI.@]
249 *
250 * Converts a Unicode string to a multi-byte string.
251 *
252 * PARAMS
253 * lpUnicodeStr [I] Unicode string to be converted
254 * uCodePage [I] Code page
255 *
256 * RETURNS
257 * Success: pointer to the converted multi-byte string
258 * Failure: NULL
259 *
260 * NOTE
261 * Use MyFree to release the returned multi-byte string.
262 */
263 LPSTR WINAPI UnicodeToMultiByte(LPCWSTR lpUnicodeStr, UINT uCodePage)
264 {
265 LPSTR lpMultiByteStr;
266 int nLength;
267
268 nLength = WideCharToMultiByte(uCodePage, 0, lpUnicodeStr, -1,
269 NULL, 0, NULL, NULL);
270 if (nLength == 0)
271 return NULL;
272
273 lpMultiByteStr = MyMalloc(nLength);
274 if (lpMultiByteStr == NULL)
275 return NULL;
276
277 if (!WideCharToMultiByte(uCodePage, 0, lpUnicodeStr, -1,
278 lpMultiByteStr, nLength, NULL, NULL))
279 {
280 MyFree(lpMultiByteStr);
281 return NULL;
282 }
283
284 return lpMultiByteStr;
285 }
286
287
288 /**************************************************************************
289 * DoesUserHavePrivilege [SETUPAPI.@]
290 *
291 * Check whether the current user has got a given privilege.
292 *
293 * PARAMS
294 * lpPrivilegeName [I] Name of the privilege to be checked
295 *
296 * RETURNS
297 * Success: TRUE
298 * Failure: FALSE
299 */
300 BOOL WINAPI DoesUserHavePrivilege(LPCWSTR lpPrivilegeName)
301 {
302 HANDLE hToken;
303 DWORD dwSize;
304 PTOKEN_PRIVILEGES lpPrivileges;
305 LUID PrivilegeLuid;
306 DWORD i;
307 BOOL bResult = FALSE;
308
309 TRACE("%s\n", debugstr_w(lpPrivilegeName));
310
311 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
312 return FALSE;
313
314 if (!GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &dwSize))
315 {
316 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
317 {
318 CloseHandle(hToken);
319 return FALSE;
320 }
321 }
322
323 lpPrivileges = MyMalloc(dwSize);
324 if (lpPrivileges == NULL)
325 {
326 CloseHandle(hToken);
327 return FALSE;
328 }
329
330 if (!GetTokenInformation(hToken, TokenPrivileges, lpPrivileges, dwSize, &dwSize))
331 {
332 MyFree(lpPrivileges);
333 CloseHandle(hToken);
334 return FALSE;
335 }
336
337 CloseHandle(hToken);
338
339 if (!LookupPrivilegeValueW(NULL, lpPrivilegeName, &PrivilegeLuid))
340 {
341 MyFree(lpPrivileges);
342 return FALSE;
343 }
344
345 for (i = 0; i < lpPrivileges->PrivilegeCount; i++)
346 {
347 if (lpPrivileges->Privileges[i].Luid.HighPart == PrivilegeLuid.HighPart &&
348 lpPrivileges->Privileges[i].Luid.LowPart == PrivilegeLuid.LowPart)
349 {
350 bResult = TRUE;
351 }
352 }
353
354 MyFree(lpPrivileges);
355
356 return bResult;
357 }
358
359
360 /**************************************************************************
361 * EnablePrivilege [SETUPAPI.@]
362 *
363 * Enables or disables one of the current users privileges.
364 *
365 * PARAMS
366 * lpPrivilegeName [I] Name of the privilege to be changed
367 * bEnable [I] TRUE: Enables the privilege
368 * FALSE: Disables the privilege
369 *
370 * RETURNS
371 * Success: TRUE
372 * Failure: FALSE
373 */
374 BOOL WINAPI EnablePrivilege(LPCWSTR lpPrivilegeName, BOOL bEnable)
375 {
376 TOKEN_PRIVILEGES Privileges;
377 HANDLE hToken;
378 BOOL bResult;
379
380 TRACE("%s %s\n", debugstr_w(lpPrivilegeName), bEnable ? "TRUE" : "FALSE");
381
382 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
383 return FALSE;
384
385 Privileges.PrivilegeCount = 1;
386 Privileges.Privileges[0].Attributes = (bEnable) ? SE_PRIVILEGE_ENABLED : 0;
387
388 if (!LookupPrivilegeValueW(NULL, lpPrivilegeName,
389 &Privileges.Privileges[0].Luid))
390 {
391 CloseHandle(hToken);
392 return FALSE;
393 }
394
395 bResult = AdjustTokenPrivileges(hToken, FALSE, &Privileges, 0, NULL, NULL);
396
397 CloseHandle(hToken);
398
399 return bResult;
400 }
401
402
403 /**************************************************************************
404 * DelayedMove [SETUPAPI.@]
405 *
406 * Moves a file upon the next reboot.
407 *
408 * PARAMS
409 * lpExistingFileName [I] Current file name
410 * lpNewFileName [I] New file name
411 *
412 * RETURNS
413 * Success: TRUE
414 * Failure: FALSE
415 */
416 BOOL WINAPI DelayedMove(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName)
417 {
418 return MoveFileExW(lpExistingFileName, lpNewFileName,
419 MOVEFILE_REPLACE_EXISTING | MOVEFILE_DELAY_UNTIL_REBOOT);
420 }
421
422
423 /**************************************************************************
424 * FileExists [SETUPAPI.@]
425 *
426 * Checks whether a file exists.
427 *
428 * PARAMS
429 * lpFileName [I] Name of the file to check
430 * lpNewFileName [O] Optional information about the existing file
431 *
432 * RETURNS
433 * Success: TRUE
434 * Failure: FALSE
435 */
436 BOOL WINAPI FileExists(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFileFindData)
437 {
438 WIN32_FIND_DATAW FindData;
439 HANDLE hFind;
440 UINT uErrorMode;
441 DWORD dwError;
442
443 uErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
444
445 hFind = FindFirstFileW(lpFileName, &FindData);
446 if (hFind == INVALID_HANDLE_VALUE)
447 {
448 dwError = GetLastError();
449 SetErrorMode(uErrorMode);
450 SetLastError(dwError);
451 return FALSE;
452 }
453
454 FindClose(hFind);
455
456 if (lpFileFindData)
457 *lpFileFindData = FindData;
458
459 SetErrorMode(uErrorMode);
460
461 return TRUE;
462 }
463
464
465 /**************************************************************************
466 * CaptureStringArg [SETUPAPI.@]
467 *
468 * Captures a UNICODE string.
469 *
470 * PARAMS
471 * lpSrc [I] UNICODE string to be captured
472 * lpDst [O] Pointer to the captured UNICODE string
473 *
474 * RETURNS
475 * Success: ERROR_SUCCESS
476 * Failure: ERROR_INVALID_PARAMETER
477 *
478 * NOTE
479 * Call MyFree to release the captured UNICODE string.
480 */
481 DWORD WINAPI CaptureStringArg(LPCWSTR pSrc, LPWSTR *pDst)
482 {
483 if (pDst == NULL)
484 return ERROR_INVALID_PARAMETER;
485
486 *pDst = DuplicateString(pSrc);
487
488 return ERROR_SUCCESS;
489 }
490
491
492 /**************************************************************************
493 * CaptureAndConvertAnsiArg [SETUPAPI.@]
494 *
495 * Captures an ANSI string and converts it to a UNICODE string.
496 *
497 * PARAMS
498 * lpSrc [I] ANSI string to be captured
499 * lpDst [O] Pointer to the captured UNICODE string
500 *
501 * RETURNS
502 * Success: ERROR_SUCCESS
503 * Failure: ERROR_INVALID_PARAMETER
504 *
505 * NOTE
506 * Call MyFree to release the captured UNICODE string.
507 */
508 DWORD WINAPI CaptureAndConvertAnsiArg(LPCSTR pSrc, LPWSTR *pDst)
509 {
510 if (pDst == NULL)
511 return ERROR_INVALID_PARAMETER;
512
513 *pDst = MultiByteToUnicode(pSrc, CP_ACP);
514
515 return ERROR_SUCCESS;
516 }
517
518
519 /**************************************************************************
520 * OpenAndMapFileForRead [SETUPAPI.@]
521 *
522 * Open and map a file to a buffer.
523 *
524 * PARAMS
525 * lpFileName [I] Name of the file to be opened
526 * lpSize [O] Pointer to the file size
527 * lpFile [0] Pointer to the file handle
528 * lpMapping [0] Pointer to the mapping handle
529 * lpBuffer [0] Pointer to the file buffer
530 *
531 * RETURNS
532 * Success: ERROR_SUCCESS
533 * Failure: Other
534 *
535 * NOTE
536 * Call UnmapAndCloseFile to release the file.
537 */
538 DWORD WINAPI OpenAndMapFileForRead(LPCWSTR lpFileName,
539 LPDWORD lpSize,
540 LPHANDLE lpFile,
541 LPHANDLE lpMapping,
542 LPVOID *lpBuffer)
543 {
544 DWORD dwError;
545
546 TRACE("%s %p %p %p %p\n",
547 debugstr_w(lpFileName), lpSize, lpFile, lpMapping, lpBuffer);
548
549 *lpFile = CreateFileW(lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
550 OPEN_EXISTING, 0, NULL);
551 if (*lpFile == INVALID_HANDLE_VALUE)
552 return GetLastError();
553
554 *lpSize = GetFileSize(*lpFile, NULL);
555 if (*lpSize == INVALID_FILE_SIZE)
556 {
557 dwError = GetLastError();
558 CloseHandle(*lpFile);
559 return dwError;
560 }
561
562 *lpMapping = CreateFileMappingW(*lpFile, NULL, PAGE_READONLY, 0,
563 *lpSize, NULL);
564 if (*lpMapping == NULL)
565 {
566 dwError = GetLastError();
567 CloseHandle(*lpFile);
568 return dwError;
569 }
570
571 *lpBuffer = MapViewOfFile(*lpMapping, FILE_MAP_READ, 0, 0, *lpSize);
572 if (*lpBuffer == NULL)
573 {
574 dwError = GetLastError();
575 CloseHandle(*lpMapping);
576 CloseHandle(*lpFile);
577 return dwError;
578 }
579
580 return ERROR_SUCCESS;
581 }
582
583
584 /**************************************************************************
585 * UnmapAndCloseFile [SETUPAPI.@]
586 *
587 * Unmap and close a mapped file.
588 *
589 * PARAMS
590 * hFile [I] Handle to the file
591 * hMapping [I] Handle to the file mapping
592 * lpBuffer [I] Pointer to the file buffer
593 *
594 * RETURNS
595 * Success: TRUE
596 * Failure: FALSE
597 */
598 BOOL WINAPI UnmapAndCloseFile(HANDLE hFile, HANDLE hMapping, LPVOID lpBuffer)
599 {
600 TRACE("%p %p %p\n",
601 hFile, hMapping, lpBuffer);
602
603 if (!UnmapViewOfFile(lpBuffer))
604 return FALSE;
605
606 if (!CloseHandle(hMapping))
607 return FALSE;
608
609 if (!CloseHandle(hFile))
610 return FALSE;
611
612 return TRUE;
613 }
614
615
616 /**************************************************************************
617 * StampFileSecurity [SETUPAPI.@]
618 *
619 * Assign a new security descriptor to the given file.
620 *
621 * PARAMS
622 * lpFileName [I] Name of the file
623 * pSecurityDescriptor [I] New security descriptor
624 *
625 * RETURNS
626 * Success: ERROR_SUCCESS
627 * Failure: other
628 */
629 DWORD WINAPI StampFileSecurity(LPCWSTR lpFileName, PSECURITY_DESCRIPTOR pSecurityDescriptor)
630 {
631 TRACE("%s %p\n", debugstr_w(lpFileName), pSecurityDescriptor);
632
633 if (!SetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
634 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
635 pSecurityDescriptor))
636 return GetLastError();
637
638 return ERROR_SUCCESS;
639 }
640
641
642 /**************************************************************************
643 * TakeOwnershipOfFile [SETUPAPI.@]
644 *
645 * Takes the ownership of the given file.
646 *
647 * PARAMS
648 * lpFileName [I] Name of the file
649 *
650 * RETURNS
651 * Success: ERROR_SUCCESS
652 * Failure: other
653 */
654 DWORD WINAPI TakeOwnershipOfFile(LPCWSTR lpFileName)
655 {
656 SECURITY_DESCRIPTOR SecDesc;
657 HANDLE hToken = NULL;
658 PTOKEN_OWNER pOwner = NULL;
659 DWORD dwError;
660 DWORD dwSize;
661
662 TRACE("%s\n", debugstr_w(lpFileName));
663
664 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
665 return GetLastError();
666
667 if (!GetTokenInformation(hToken, TokenOwner, NULL, 0, &dwSize))
668 {
669 goto fail;
670 }
671
672 pOwner = MyMalloc(dwSize);
673 if (pOwner == NULL)
674 {
675 CloseHandle(hToken);
676 return ERROR_NOT_ENOUGH_MEMORY;
677 }
678
679 if (!GetTokenInformation(hToken, TokenOwner, pOwner, dwSize, &dwSize))
680 {
681 goto fail;
682 }
683
684 if (!InitializeSecurityDescriptor(&SecDesc, SECURITY_DESCRIPTOR_REVISION))
685 {
686 goto fail;
687 }
688
689 if (!SetSecurityDescriptorOwner(&SecDesc, pOwner->Owner, FALSE))
690 {
691 goto fail;
692 }
693
694 if (!SetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION, &SecDesc))
695 {
696 goto fail;
697 }
698
699 MyFree(pOwner);
700 CloseHandle(hToken);
701
702 return ERROR_SUCCESS;
703
704 fail:;
705 dwError = GetLastError();
706
707 MyFree(pOwner);
708
709 if (hToken != NULL)
710 CloseHandle(hToken);
711
712 return dwError;
713 }
714
715
716 /**************************************************************************
717 * RetreiveFileSecurity [SETUPAPI.@]
718 *
719 * Retrieve the security descriptor that is associated with the given file.
720 *
721 * PARAMS
722 * lpFileName [I] Name of the file
723 *
724 * RETURNS
725 * Success: ERROR_SUCCESS
726 * Failure: other
727 */
728 DWORD WINAPI RetreiveFileSecurity(LPCWSTR lpFileName,
729 PSECURITY_DESCRIPTOR *pSecurityDescriptor)
730 {
731 PSECURITY_DESCRIPTOR SecDesc;
732 DWORD dwSize = 0x100;
733 DWORD dwError;
734
735 SecDesc = MyMalloc(dwSize);
736 if (SecDesc == NULL)
737 return ERROR_NOT_ENOUGH_MEMORY;
738
739 if (GetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
740 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
741 SecDesc, dwSize, &dwSize))
742 {
743 *pSecurityDescriptor = SecDesc;
744 return ERROR_SUCCESS;
745 }
746
747 dwError = GetLastError();
748 if (dwError != ERROR_INSUFFICIENT_BUFFER)
749 {
750 MyFree(SecDesc);
751 return dwError;
752 }
753
754 SecDesc = MyRealloc(SecDesc, dwSize);
755 if (SecDesc == NULL)
756 return ERROR_NOT_ENOUGH_MEMORY;
757
758 if (GetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
759 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
760 SecDesc, dwSize, &dwSize))
761 {
762 *pSecurityDescriptor = SecDesc;
763 return ERROR_SUCCESS;
764 }
765
766 dwError = GetLastError();
767 MyFree(SecDesc);
768
769 return dwError;
770 }
771
772
773 static DWORD global_flags = 0; /* FIXME: what should be in here? */
774
775 /***********************************************************************
776 * pSetupGetGlobalFlags (SETUPAPI.@)
777 */
778 DWORD WINAPI pSetupGetGlobalFlags(void)
779 {
780 FIXME( "stub\n" );
781 return global_flags;
782 }
783
784
785 /***********************************************************************
786 * pSetupSetGlobalFlags (SETUPAPI.@)
787 */
788 void WINAPI pSetupSetGlobalFlags( DWORD flags )
789 {
790 global_flags = flags;
791 }
792
793 /***********************************************************************
794 * CMP_WaitNoPendingInstallEvents (SETUPAPI.@)
795 */
796 DWORD WINAPI CMP_WaitNoPendingInstallEvents( DWORD dwTimeout )
797 {
798 static BOOL warned = FALSE;
799
800 if (!warned)
801 {
802 FIXME("%d\n", dwTimeout);
803 warned = TRUE;
804 }
805 return WAIT_OBJECT_0;
806 }
807
808 /***********************************************************************
809 * AssertFail (SETUPAPI.@)
810 *
811 * Shows an assert fail error messagebox
812 *
813 * PARAMS
814 * lpFile [I] file where assert failed
815 * uLine [I] line number in file
816 * lpMessage [I] assert message
817 *
818 */
819 void WINAPI AssertFail(LPCSTR lpFile, UINT uLine, LPCSTR lpMessage)
820 {
821 FIXME("%s %u %s\n", lpFile, uLine, lpMessage);
822 }
823
824 /***********************************************************************
825 * SetupCopyOEMInfA (SETUPAPI.@)
826 */
827 BOOL WINAPI SetupCopyOEMInfA( PCSTR source, PCSTR location,
828 DWORD media_type, DWORD style, PSTR dest,
829 DWORD buffer_size, PDWORD required_size, PSTR *component )
830 {
831 BOOL ret = FALSE;
832 LPWSTR destW = NULL, sourceW = NULL, locationW = NULL;
833 DWORD size;
834
835 TRACE("%s, %s, %d, %d, %p, %d, %p, %p\n", debugstr_a(source), debugstr_a(location),
836 media_type, style, dest, buffer_size, required_size, component);
837
838 if (dest && !(destW = MyMalloc( buffer_size * sizeof(WCHAR) ))) return FALSE;
839 if (source && !(sourceW = strdupAtoW( source ))) goto done;
840 if (location && !(locationW = strdupAtoW( location ))) goto done;
841
842 if (!(ret = SetupCopyOEMInfW( sourceW, locationW, media_type, style, destW,
843 buffer_size, &size, NULL )))
844 {
845 if (required_size) *required_size = size;
846 goto done;
847 }
848
849 if (dest)
850 {
851 if (buffer_size >= size)
852 {
853 WideCharToMultiByte( CP_ACP, 0, destW, -1, dest, buffer_size, NULL, NULL );
854 if (component) *component = strrchr( dest, '\\' ) + 1;
855 }
856 else
857 {
858 SetLastError( ERROR_INSUFFICIENT_BUFFER );
859 goto done;
860 }
861 }
862
863 done:
864 MyFree( destW );
865 HeapFree( GetProcessHeap(), 0, sourceW );
866 HeapFree( GetProcessHeap(), 0, locationW );
867 if (ret) SetLastError(ERROR_SUCCESS);
868 return ret;
869 }
870
871 static int compare_files( HANDLE file1, HANDLE file2 )
872 {
873 char buffer1[2048];
874 char buffer2[2048];
875 DWORD size1;
876 DWORD size2;
877
878 while( ReadFile(file1, buffer1, sizeof(buffer1), &size1, NULL) &&
879 ReadFile(file2, buffer2, sizeof(buffer2), &size2, NULL) )
880 {
881 int ret;
882 if (size1 != size2)
883 return size1 > size2 ? 1 : -1;
884 if (!size1)
885 return 0;
886 ret = memcmp( buffer1, buffer2, size1 );
887 if (ret)
888 return ret;
889 }
890
891 return 0;
892 }
893
894 /***********************************************************************
895 * SetupCopyOEMInfW (SETUPAPI.@)
896 */
897 BOOL WINAPI SetupCopyOEMInfW( PCWSTR source, PCWSTR location,
898 DWORD media_type, DWORD style, PWSTR dest,
899 DWORD buffer_size, PDWORD required_size, PWSTR *component )
900 {
901 BOOL ret = FALSE;
902 WCHAR target[MAX_PATH], catalog_file[MAX_PATH], *p;
903 static const WCHAR inf[] = { '\\','i','n','f','\\',0 };
904 static const WCHAR wszVersion[] = { 'V','e','r','s','i','o','n',0 };
905 static const WCHAR wszCatalogFile[] = { 'C','a','t','a','l','o','g','F','i','l','e',0 };
906 DWORD size;
907 HINF hinf;
908
909 TRACE("%s, %s, %d, %d, %p, %d, %p, %p\n", debugstr_w(source), debugstr_w(location),
910 media_type, style, dest, buffer_size, required_size, component);
911
912 if (!source)
913 {
914 SetLastError(ERROR_INVALID_PARAMETER);
915 return FALSE;
916 }
917
918 /* check for a relative path */
919 if (!(*source == '\\' || (*source && source[1] == ':')))
920 {
921 SetLastError(ERROR_FILE_NOT_FOUND);
922 return FALSE;
923 }
924
925 if (!GetWindowsDirectoryW( target, sizeof(target)/sizeof(WCHAR) )) return FALSE;
926
927 strcatW( target, inf );
928 if ((p = strrchrW( source, '\\' )))
929 strcatW( target, p + 1 );
930
931 /* does the file exist already? */
932 if ((GetFileAttributesW( target ) != INVALID_FILE_ATTRIBUTES) &&
933 !(style & SP_COPY_NOOVERWRITE))
934 {
935 static const WCHAR oem[] = { 'o','e','m',0 };
936 unsigned int i;
937 LARGE_INTEGER source_file_size;
938 HANDLE source_file;
939
940 source_file = CreateFileW( source, FILE_READ_DATA | FILE_READ_ATTRIBUTES,
941 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
942 NULL, OPEN_EXISTING, 0, NULL );
943 if (source_file == INVALID_HANDLE_VALUE)
944 return FALSE;
945
946 if (!GetFileSizeEx( source_file, &source_file_size ))
947 {
948 CloseHandle( source_file );
949 return FALSE;
950 }
951
952 p = strrchrW( target, '\\' ) + 1;
953 memcpy( p, oem, sizeof(oem) );
954 p += sizeof(oem)/sizeof(oem[0]) - 1;
955
956 /* generate OEMnnn.inf ending */
957 for (i = 0; i < OEM_INDEX_LIMIT; i++)
958 {
959 static const WCHAR format[] = { '%','u','.','i','n','f',0 };
960 HANDLE dest_file;
961 LARGE_INTEGER dest_file_size;
962
963 wsprintfW( p, format, i );
964 dest_file = CreateFileW( target, FILE_READ_DATA | FILE_READ_ATTRIBUTES,
965 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
966 NULL, OPEN_EXISTING, 0, NULL );
967 /* if we found a file name that doesn't exist then we're done */
968 if (dest_file == INVALID_HANDLE_VALUE)
969 break;
970 /* now check if the same inf file has already been copied to the inf
971 * directory. if so, use that file and don't create a new one */
972 if (!GetFileSizeEx( dest_file, &dest_file_size ) ||
973 (dest_file_size.QuadPart != source_file_size.QuadPart) ||
974 compare_files( source_file, dest_file ))
975 {
976 CloseHandle( dest_file );
977 continue;
978 }
979 CloseHandle( dest_file );
980 break;
981 }
982
983 CloseHandle( source_file );
984 if (i == OEM_INDEX_LIMIT)
985 {
986 SetLastError( ERROR_FILENAME_EXCED_RANGE );
987 return FALSE;
988 }
989 }
990
991 hinf = SetupOpenInfFileW( source, NULL, INF_STYLE_WIN4, NULL );
992 if (hinf == INVALID_HANDLE_VALUE) return FALSE;
993
994 if (SetupGetLineTextW( NULL, hinf, wszVersion, wszCatalogFile, catalog_file,
995 sizeof(catalog_file)/sizeof(catalog_file[0]), NULL ))
996 {
997 WCHAR source_cat[MAX_PATH];
998 HCATADMIN handle;
999 HCATINFO cat;
1000 GUID msguid = DRIVER_ACTION_VERIFY;
1001
1002 SetupCloseInfFile( hinf );
1003
1004 strcpyW( source_cat, source );
1005 p = strrchrW( source_cat, '\\' );
1006 if (p) p++;
1007 else p = source_cat;
1008 strcpyW( p, catalog_file );
1009
1010 TRACE("installing catalog file %s\n", debugstr_w( source_cat ));
1011
1012 if (!CryptCATAdminAcquireContext(&handle, &msguid, 0))
1013 {
1014 ERR("Could not acquire security context\n");
1015 return FALSE;
1016 }
1017
1018 if (!(cat = CryptCATAdminAddCatalog(handle, source_cat, catalog_file, 0)))
1019 {
1020 ERR("Could not add catalog\n");
1021 CryptCATAdminReleaseContext(handle, 0);
1022 return FALSE;
1023 }
1024
1025 CryptCATAdminReleaseCatalogContext(handle, cat, 0);
1026 CryptCATAdminReleaseContext(handle, 0);
1027 }
1028 else
1029 SetupCloseInfFile( hinf );
1030
1031 if (!(ret = CopyFileW( source, target, (style & SP_COPY_NOOVERWRITE) != 0 )))
1032 return ret;
1033
1034 if (style & SP_COPY_DELETESOURCE)
1035 DeleteFileW( source );
1036
1037 size = strlenW( target ) + 1;
1038 if (dest)
1039 {
1040 if (buffer_size >= size)
1041 {
1042 strcpyW( dest, target );
1043 }
1044 else
1045 {
1046 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1047 ret = FALSE;
1048 }
1049 }
1050
1051 if (component) *component = p + 1;
1052 if (required_size) *required_size = size;
1053 if (ret) SetLastError(ERROR_SUCCESS);
1054
1055 return ret;
1056 }
1057
1058 /***********************************************************************
1059 * SetupUninstallOEMInfA (SETUPAPI.@)
1060 */
1061 BOOL WINAPI SetupUninstallOEMInfA( PCSTR inf_file, DWORD flags, PVOID reserved )
1062 {
1063 BOOL ret;
1064 WCHAR *inf_fileW = NULL;
1065
1066 TRACE("%s, 0x%08x, %p\n", debugstr_a(inf_file), flags, reserved);
1067
1068 if (inf_file && !(inf_fileW = strdupAtoW( inf_file ))) return FALSE;
1069 ret = SetupUninstallOEMInfW( inf_fileW, flags, reserved );
1070 HeapFree( GetProcessHeap(), 0, inf_fileW );
1071 return ret;
1072 }
1073
1074 /***********************************************************************
1075 * SetupUninstallOEMInfW (SETUPAPI.@)
1076 */
1077 BOOL WINAPI SetupUninstallOEMInfW( PCWSTR inf_file, DWORD flags, PVOID reserved )
1078 {
1079 static const WCHAR infW[] = {'\\','i','n','f','\\',0};
1080 WCHAR target[MAX_PATH];
1081
1082 TRACE("%s, 0x%08x, %p\n", debugstr_w(inf_file), flags, reserved);
1083
1084 if (!inf_file)
1085 {
1086 SetLastError(ERROR_INVALID_PARAMETER);
1087 return FALSE;
1088 }
1089
1090 if (!GetWindowsDirectoryW( target, sizeof(target)/sizeof(WCHAR) )) return FALSE;
1091
1092 strcatW( target, infW );
1093 strcatW( target, inf_file );
1094
1095 if (flags & SUOI_FORCEDELETE)
1096 return DeleteFileW(target);
1097
1098 FIXME("not deleting %s\n", debugstr_w(target));
1099
1100 return TRUE;
1101 }
1102
1103 /***********************************************************************
1104 * InstallCatalog (SETUPAPI.@)
1105 */
1106 DWORD WINAPI InstallCatalog( LPCSTR catalog, LPCSTR basename, LPSTR fullname )
1107 {
1108 FIXME("%s, %s, %p\n", debugstr_a(catalog), debugstr_a(basename), fullname);
1109 return 0;
1110 }
1111
1112 /***********************************************************************
1113 * pSetupInstallCatalog (SETUPAPI.@)
1114 */
1115 DWORD WINAPI pSetupInstallCatalog( LPCWSTR catalog, LPCWSTR basename, LPWSTR fullname )
1116 {
1117 HCATADMIN admin;
1118 HCATINFO cat;
1119
1120 TRACE ("%s, %s, %p\n", debugstr_w(catalog), debugstr_w(basename), fullname);
1121
1122 if (!CryptCATAdminAcquireContext(&admin,NULL,0))
1123 return GetLastError();
1124
1125 if (!(cat = CryptCATAdminAddCatalog( admin, (PWSTR)catalog, (PWSTR)basename, 0 )))
1126 {
1127 DWORD rc = GetLastError();
1128 CryptCATAdminReleaseContext(admin, 0);
1129 return rc;
1130 }
1131 CryptCATAdminReleaseCatalogContext(admin, cat, 0);
1132 CryptCATAdminReleaseContext(admin,0);
1133
1134 if (fullname)
1135 FIXME("not returning full installed catalog path\n");
1136
1137 return NO_ERROR;
1138 }
1139
1140 static UINT detect_compression_type( LPCWSTR file )
1141 {
1142 DWORD size;
1143 HANDLE handle;
1144 UINT type = FILE_COMPRESSION_NONE;
1145 static const BYTE LZ_MAGIC[] = { 0x53, 0x5a, 0x44, 0x44, 0x88, 0xf0, 0x27, 0x33 };
1146 static const BYTE MSZIP_MAGIC[] = { 0x4b, 0x57, 0x41, 0x4a };
1147 static const BYTE NTCAB_MAGIC[] = { 0x4d, 0x53, 0x43, 0x46 };
1148 BYTE buffer[8];
1149
1150 handle = CreateFileW( file, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL );
1151 if (handle == INVALID_HANDLE_VALUE)
1152 {
1153 ERR("cannot open file %s\n", debugstr_w(file));
1154 return FILE_COMPRESSION_NONE;
1155 }
1156 if (!ReadFile( handle, buffer, sizeof(buffer), &size, NULL ) || size != sizeof(buffer))
1157 {
1158 CloseHandle( handle );
1159 return FILE_COMPRESSION_NONE;
1160 }
1161 if (!memcmp( buffer, LZ_MAGIC, sizeof(LZ_MAGIC) )) type = FILE_COMPRESSION_WINLZA;
1162 else if (!memcmp( buffer, MSZIP_MAGIC, sizeof(MSZIP_MAGIC) )) type = FILE_COMPRESSION_MSZIP;
1163 else if (!memcmp( buffer, NTCAB_MAGIC, sizeof(NTCAB_MAGIC) )) type = FILE_COMPRESSION_MSZIP; /* not a typo */
1164
1165 CloseHandle( handle );
1166 return type;
1167 }
1168
1169 static BOOL get_file_size( LPCWSTR file, DWORD *size )
1170 {
1171 HANDLE handle;
1172
1173 handle = CreateFileW( file, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL );
1174 if (handle == INVALID_HANDLE_VALUE)
1175 {
1176 ERR("cannot open file %s\n", debugstr_w(file));
1177 return FALSE;
1178 }
1179 *size = GetFileSize( handle, NULL );
1180 CloseHandle( handle );
1181 return TRUE;
1182 }
1183
1184 static BOOL get_file_sizes_none( LPCWSTR source, DWORD *source_size, DWORD *target_size )
1185 {
1186 DWORD size;
1187
1188 if (!get_file_size( source, &size )) return FALSE;
1189 if (source_size) *source_size = size;
1190 if (target_size) *target_size = size;
1191 return TRUE;
1192 }
1193
1194 static BOOL get_file_sizes_lz( LPCWSTR source, DWORD *source_size, DWORD *target_size )
1195 {
1196 DWORD size;
1197 BOOL ret = TRUE;
1198
1199 if (source_size)
1200 {
1201 if (!get_file_size( source, &size )) ret = FALSE;
1202 else *source_size = size;
1203 }
1204 if (target_size)
1205 {
1206 INT file;
1207 OFSTRUCT of;
1208
1209 if ((file = LZOpenFileW( (LPWSTR)source, &of, OF_READ )) < 0)
1210 {
1211 ERR("cannot open source file for reading\n");
1212 return FALSE;
1213 }
1214 *target_size = LZSeek( file, 0, 2 );
1215 LZClose( file );
1216 }
1217 return ret;
1218 }
1219
1220 static UINT CALLBACK file_compression_info_callback( PVOID context, UINT notification, UINT_PTR param1, UINT_PTR param2 )
1221 {
1222 DWORD *size = context;
1223 FILE_IN_CABINET_INFO_W *info = (FILE_IN_CABINET_INFO_W *)param1;
1224
1225 switch (notification)
1226 {
1227 case SPFILENOTIFY_FILEINCABINET:
1228 {
1229 *size = info->FileSize;
1230 return FILEOP_SKIP;
1231 }
1232 default: return NO_ERROR;
1233 }
1234 }
1235
1236 static BOOL get_file_sizes_cab( LPCWSTR source, DWORD *source_size, DWORD *target_size )
1237 {
1238 DWORD size;
1239 BOOL ret = TRUE;
1240
1241 if (source_size)
1242 {
1243 if (!get_file_size( source, &size )) ret = FALSE;
1244 else *source_size = size;
1245 }
1246 if (target_size)
1247 {
1248 ret = SetupIterateCabinetW( source, 0, file_compression_info_callback, target_size );
1249 }
1250 return ret;
1251 }
1252
1253 /***********************************************************************
1254 * SetupGetFileCompressionInfoExA (SETUPAPI.@)
1255 *
1256 * See SetupGetFileCompressionInfoExW.
1257 */
1258 BOOL WINAPI SetupGetFileCompressionInfoExA( PCSTR source, PSTR name, DWORD len, PDWORD required,
1259 PDWORD source_size, PDWORD target_size, PUINT type )
1260 {
1261 BOOL ret;
1262 WCHAR *nameW = NULL, *sourceW = NULL;
1263 DWORD nb_chars = 0;
1264 LPSTR nameA;
1265
1266 TRACE("%s, %p, %d, %p, %p, %p, %p\n", debugstr_a(source), name, len, required,
1267 source_size, target_size, type);
1268
1269 if (!source || !(sourceW = MultiByteToUnicode( source, CP_ACP ))) return FALSE;
1270
1271 if (name)
1272 {
1273 ret = SetupGetFileCompressionInfoExW( sourceW, NULL, 0, &nb_chars, NULL, NULL, NULL );
1274 if (!(nameW = HeapAlloc( GetProcessHeap(), 0, nb_chars * sizeof(WCHAR) )))
1275 {
1276 MyFree( sourceW );
1277 return FALSE;
1278 }
1279 }
1280 ret = SetupGetFileCompressionInfoExW( sourceW, nameW, nb_chars, &nb_chars, source_size, target_size, type );
1281 if (ret)
1282 {
1283 if ((nameA = UnicodeToMultiByte( nameW, CP_ACP )))
1284 {
1285 if (name && len >= nb_chars) lstrcpyA( name, nameA );
1286 else
1287 {
1288 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1289 ret = FALSE;
1290 }
1291 MyFree( nameA );
1292 }
1293 }
1294 if (required) *required = nb_chars;
1295 HeapFree( GetProcessHeap(), 0, nameW );
1296 MyFree( sourceW );
1297
1298 return ret;
1299 }
1300
1301 /***********************************************************************
1302 * SetupGetFileCompressionInfoExW (SETUPAPI.@)
1303 *
1304 * Get compression type and compressed/uncompressed sizes of a given file.
1305 *
1306 * PARAMS
1307 * source [I] File to examine.
1308 * name [O] Actual filename used.
1309 * len [I] Length in characters of 'name' buffer.
1310 * required [O] Number of characters written to 'name'.
1311 * source_size [O] Size of compressed file.
1312 * target_size [O] Size of uncompressed file.
1313 * type [O] Compression type.
1314 *
1315 * RETURNS
1316 * Success: TRUE
1317 * Failure: FALSE
1318 */
1319 BOOL WINAPI SetupGetFileCompressionInfoExW( PCWSTR source, PWSTR name, DWORD len, PDWORD required,
1320 PDWORD source_size, PDWORD target_size, PUINT type )
1321 {
1322 UINT comp;
1323 BOOL ret = FALSE;
1324 DWORD source_len;
1325
1326 TRACE("%s, %p, %d, %p, %p, %p, %p\n", debugstr_w(source), name, len, required,
1327 source_size, target_size, type);
1328
1329 if (!source) return FALSE;
1330
1331 source_len = lstrlenW( source ) + 1;
1332 if (required) *required = source_len;
1333 if (name && len >= source_len)
1334 {
1335 lstrcpyW( name, source );
1336 ret = TRUE;
1337 }
1338 else return FALSE;
1339
1340 comp = detect_compression_type( source );
1341 if (type) *type = comp;
1342
1343 switch (comp)
1344 {
1345 case FILE_COMPRESSION_MSZIP:
1346 case FILE_COMPRESSION_NTCAB: ret = get_file_sizes_cab( source, source_size, target_size ); break;
1347 case FILE_COMPRESSION_NONE: ret = get_file_sizes_none( source, source_size, target_size ); break;
1348 case FILE_COMPRESSION_WINLZA: ret = get_file_sizes_lz( source, source_size, target_size ); break;
1349 default: break;
1350 }
1351 return ret;
1352 }
1353
1354 /***********************************************************************
1355 * SetupGetFileCompressionInfoA (SETUPAPI.@)
1356 *
1357 * See SetupGetFileCompressionInfoW.
1358 */
1359 DWORD WINAPI SetupGetFileCompressionInfoA( PCSTR source, PSTR *name, PDWORD source_size,
1360 PDWORD target_size, PUINT type )
1361 {
1362 BOOL ret;
1363 DWORD error, required;
1364 LPSTR actual_name;
1365
1366 TRACE("%s, %p, %p, %p, %p\n", debugstr_a(source), name, source_size, target_size, type);
1367
1368 if (!source || !name || !source_size || !target_size || !type)
1369 return ERROR_INVALID_PARAMETER;
1370
1371 ret = SetupGetFileCompressionInfoExA( source, NULL, 0, &required, NULL, NULL, NULL );
1372 if (!(actual_name = MyMalloc( required ))) return ERROR_NOT_ENOUGH_MEMORY;
1373
1374 ret = SetupGetFileCompressionInfoExA( source, actual_name, required, &required,
1375 source_size, target_size, type );
1376 if (!ret)
1377 {
1378 error = GetLastError();
1379 MyFree( actual_name );
1380 return error;
1381 }
1382 *name = actual_name;
1383 return ERROR_SUCCESS;
1384 }
1385
1386 /***********************************************************************
1387 * SetupGetFileCompressionInfoW (SETUPAPI.@)
1388 *
1389 * Get compression type and compressed/uncompressed sizes of a given file.
1390 *
1391 * PARAMS
1392 * source [I] File to examine.
1393 * name [O] Actual filename used.
1394 * source_size [O] Size of compressed file.
1395 * target_size [O] Size of uncompressed file.
1396 * type [O] Compression type.
1397 *
1398 * RETURNS
1399 * Success: ERROR_SUCCESS
1400 * Failure: Win32 error code.
1401 */
1402 DWORD WINAPI SetupGetFileCompressionInfoW( PCWSTR source, PWSTR *name, PDWORD source_size,
1403 PDWORD target_size, PUINT type )
1404 {
1405 BOOL ret;
1406 DWORD error, required;
1407 LPWSTR actual_name;
1408
1409 TRACE("%s, %p, %p, %p, %p\n", debugstr_w(source), name, source_size, target_size, type);
1410
1411 if (!source || !name || !source_size || !target_size || !type)
1412 return ERROR_INVALID_PARAMETER;
1413
1414 ret = SetupGetFileCompressionInfoExW( source, NULL, 0, &required, NULL, NULL, NULL );
1415 if (!(actual_name = MyMalloc( required ))) return ERROR_NOT_ENOUGH_MEMORY;
1416
1417 ret = SetupGetFileCompressionInfoExW( source, actual_name, required, &required,
1418 source_size, target_size, type );
1419 if (!ret)
1420 {
1421 error = GetLastError();
1422 MyFree( actual_name );
1423 return error;
1424 }
1425 *name = actual_name;
1426 return ERROR_SUCCESS;
1427 }
1428
1429 static DWORD decompress_file_lz( LPCWSTR source, LPCWSTR target )
1430 {
1431 DWORD ret;
1432 LONG error;
1433 INT src, dst;
1434 OFSTRUCT sof, dof;
1435
1436 if ((src = LZOpenFileW( (LPWSTR)source, &sof, OF_READ )) < 0)
1437 {
1438 ERR("cannot open source file for reading\n");
1439 return ERROR_FILE_NOT_FOUND;
1440 }
1441 if ((dst = LZOpenFileW( (LPWSTR)target, &dof, OF_CREATE )) < 0)
1442 {
1443 ERR("cannot open target file for writing\n");
1444 LZClose( src );
1445 return ERROR_FILE_NOT_FOUND;
1446 }
1447 if ((error = LZCopy( src, dst )) >= 0) ret = ERROR_SUCCESS;
1448 else
1449 {
1450 WARN("failed to decompress file %d\n", error);
1451 ret = ERROR_INVALID_DATA;
1452 }
1453
1454 LZClose( src );
1455 LZClose( dst );
1456 return ret;
1457 }
1458
1459 static UINT CALLBACK decompress_or_copy_callback( PVOID context, UINT notification, UINT_PTR param1, UINT_PTR param2 )
1460 {
1461 FILE_IN_CABINET_INFO_W *info = (FILE_IN_CABINET_INFO_W *)param1;
1462
1463 switch (notification)
1464 {
1465 case SPFILENOTIFY_FILEINCABINET:
1466 {
1467 LPCWSTR filename, targetname = context;
1468 WCHAR *p;
1469
1470 if ((p = strrchrW( targetname, '\\' ))) filename = p + 1;
1471 else filename = targetname;
1472
1473 if (!lstrcmpiW( filename, info->NameInCabinet ))
1474 {
1475 strcpyW( info->FullTargetName, targetname );
1476 return FILEOP_DOIT;
1477 }
1478 return FILEOP_SKIP;
1479 }
1480 default: return NO_ERROR;
1481 }
1482 }
1483
1484 static DWORD decompress_file_cab( LPCWSTR source, LPCWSTR target )
1485 {
1486 BOOL ret;
1487
1488 ret = SetupIterateCabinetW( source, 0, decompress_or_copy_callback, (PVOID)target );
1489
1490 if (ret) return ERROR_SUCCESS;
1491 else return GetLastError();
1492 }
1493
1494 /***********************************************************************
1495 * SetupDecompressOrCopyFileA (SETUPAPI.@)
1496 *
1497 * See SetupDecompressOrCopyFileW.
1498 */
1499 DWORD WINAPI SetupDecompressOrCopyFileA( PCSTR source, PCSTR target, PUINT type )
1500 {
1501 DWORD ret = FALSE;
1502 WCHAR *sourceW = NULL, *targetW = NULL;
1503
1504 if (source && !(sourceW = MultiByteToUnicode( source, CP_ACP ))) return FALSE;
1505 if (target && !(targetW = MultiByteToUnicode( target, CP_ACP )))
1506 {
1507 MyFree( sourceW );
1508 return ERROR_NOT_ENOUGH_MEMORY;
1509 }
1510
1511 ret = SetupDecompressOrCopyFileW( sourceW, targetW, type );
1512
1513 MyFree( sourceW );
1514 MyFree( targetW );
1515
1516 return ret;
1517 }
1518
1519 /***********************************************************************
1520 * SetupDecompressOrCopyFileW (SETUPAPI.@)
1521 *
1522 * Copy a file and decompress it if needed.
1523 *
1524 * PARAMS
1525 * source [I] File to copy.
1526 * target [I] Filename of the copy.
1527 * type [I] Compression type.
1528 *
1529 * RETURNS
1530 * Success: ERROR_SUCCESS
1531 * Failure: Win32 error code.
1532 */
1533 DWORD WINAPI SetupDecompressOrCopyFileW( PCWSTR source, PCWSTR target, PUINT type )
1534 {
1535 UINT comp;
1536 DWORD ret = ERROR_INVALID_PARAMETER;
1537
1538 if (!source || !target) return ERROR_INVALID_PARAMETER;
1539
1540 if (!type) comp = detect_compression_type( source );
1541 else comp = *type;
1542
1543 switch (comp)
1544 {
1545 case FILE_COMPRESSION_NONE:
1546 if (CopyFileW( source, target, FALSE )) ret = ERROR_SUCCESS;
1547 else ret = GetLastError();
1548 break;
1549 case FILE_COMPRESSION_WINLZA:
1550 ret = decompress_file_lz( source, target );
1551 break;
1552 case FILE_COMPRESSION_NTCAB:
1553 case FILE_COMPRESSION_MSZIP:
1554 ret = decompress_file_cab( source, target );
1555 break;
1556 default:
1557 WARN("unknown compression type %d\n", comp);
1558 break;
1559 }
1560
1561 TRACE("%s -> %s %d\n", debugstr_w(source), debugstr_w(target), comp);
1562 return ret;
1563 }
1564
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.