2 * Setupapi miscellaneous functions
4 * Copyright 2005 Eric Kohl
5 * Copyright 2007 Hans Leidekker
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.
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.
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
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
35 #include "setupapi_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(setupapi
);
40 /* arbitrary limit not related to what native actually uses */
41 #define OEM_INDEX_LIMIT 999
43 /**************************************************************************
46 * Frees an allocated memory block from the process heap.
49 * lpMem [I] pointer to memory block which will be freed
54 VOID WINAPI
MyFree(LPVOID lpMem
)
56 HeapFree(GetProcessHeap(), 0, lpMem
);
60 /**************************************************************************
61 * MyMalloc [SETUPAPI.@]
63 * Allocates memory block from the process heap.
66 * dwSize [I] size of the allocated memory block
69 * Success: pointer to allocated memory block
72 LPVOID WINAPI
MyMalloc(DWORD dwSize
)
74 return HeapAlloc(GetProcessHeap(), 0, dwSize
);
78 /**************************************************************************
79 * MyRealloc [SETUPAPI.@]
81 * Changes the size of an allocated memory block or allocates a memory
82 * block from the process heap.
85 * lpSrc [I] pointer to memory block which will be resized
86 * dwSize [I] new size of the memory block
89 * Success: pointer to the resized memory block
93 * If lpSrc is a NULL-pointer, then MyRealloc allocates a memory
94 * block like MyMalloc.
96 LPVOID WINAPI
MyRealloc(LPVOID lpSrc
, DWORD dwSize
)
99 return HeapAlloc(GetProcessHeap(), 0, dwSize
);
101 return HeapReAlloc(GetProcessHeap(), 0, lpSrc
, dwSize
);
105 /**************************************************************************
106 * DuplicateString [SETUPAPI.@]
108 * Duplicates a unicode string.
111 * lpSrc [I] pointer to the unicode string that will be duplicated
114 * Success: pointer to the duplicated unicode string
118 * Call MyFree() to release the duplicated string.
120 LPWSTR WINAPI
DuplicateString(LPCWSTR lpSrc
)
124 lpDst
= MyMalloc((lstrlenW(lpSrc
) + 1) * sizeof(WCHAR
));
128 strcpyW(lpDst
, lpSrc
);
134 /**************************************************************************
135 * QueryRegistryValue [SETUPAPI.@]
137 * Retrieves value data from the registry and allocates memory for the
141 * hKey [I] Handle of the key to query
142 * lpValueName [I] Name of value under hkey to query
143 * lpData [O] Destination for the values contents,
144 * lpType [O] Destination for the value type
145 * lpcbData [O] Destination for the size of data
148 * Success: ERROR_SUCCESS
152 * Use MyFree to release the lpData buffer.
154 LONG WINAPI
QueryRegistryValue(HKEY hKey
,
162 TRACE("%p %s %p %p %p\n",
163 hKey
, debugstr_w(lpValueName
), lpData
, lpType
, lpcbData
);
165 /* Get required buffer size */
167 lError
= RegQueryValueExW(hKey
, lpValueName
, 0, lpType
, NULL
, lpcbData
);
168 if (lError
!= ERROR_SUCCESS
)
171 /* Allocate buffer */
172 *lpData
= MyMalloc(*lpcbData
);
174 return ERROR_NOT_ENOUGH_MEMORY
;
176 /* Query registry value */
177 lError
= RegQueryValueExW(hKey
, lpValueName
, 0, lpType
, *lpData
, lpcbData
);
178 if (lError
!= ERROR_SUCCESS
)
185 /**************************************************************************
186 * IsUserAdmin [SETUPAPI.@]
188 * Checks whether the current user is a member of the Administrators group.
197 BOOL WINAPI
IsUserAdmin(VOID
)
199 SID_IDENTIFIER_AUTHORITY Authority
= {SECURITY_NT_AUTHORITY
};
202 PTOKEN_GROUPS lpGroups
;
205 BOOL bResult
= FALSE
;
209 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &hToken
))
214 if (!GetTokenInformation(hToken
, TokenGroups
, NULL
, 0, &dwSize
))
216 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
223 lpGroups
= MyMalloc(dwSize
);
224 if (lpGroups
== NULL
)
230 if (!GetTokenInformation(hToken
, TokenGroups
, lpGroups
, dwSize
, &dwSize
))
239 if (!AllocateAndInitializeSid(&Authority
, 2, SECURITY_BUILTIN_DOMAIN_RID
,
240 DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0, 0,
247 for (i
= 0; i
< lpGroups
->GroupCount
; i
++)
249 if (EqualSid(lpSid
, lpGroups
->Groups
[i
].Sid
))
263 /**************************************************************************
264 * MultiByteToUnicode [SETUPAPI.@]
266 * Converts a multi-byte string to a Unicode string.
269 * lpMultiByteStr [I] Multi-byte string to be converted
270 * uCodePage [I] Code page
273 * Success: pointer to the converted Unicode string
277 * Use MyFree to release the returned Unicode string.
279 LPWSTR WINAPI
MultiByteToUnicode(LPCSTR lpMultiByteStr
, UINT uCodePage
)
284 nLength
= MultiByteToWideChar(uCodePage
, 0, lpMultiByteStr
,
289 lpUnicodeStr
= MyMalloc(nLength
* sizeof(WCHAR
));
290 if (lpUnicodeStr
== NULL
)
293 if (!MultiByteToWideChar(uCodePage
, 0, lpMultiByteStr
,
294 nLength
, lpUnicodeStr
, nLength
))
296 MyFree(lpUnicodeStr
);
304 /**************************************************************************
305 * UnicodeToMultiByte [SETUPAPI.@]
307 * Converts a Unicode string to a multi-byte string.
310 * lpUnicodeStr [I] Unicode string to be converted
311 * uCodePage [I] Code page
314 * Success: pointer to the converted multi-byte string
318 * Use MyFree to release the returned multi-byte string.
320 LPSTR WINAPI
UnicodeToMultiByte(LPCWSTR lpUnicodeStr
, UINT uCodePage
)
322 LPSTR lpMultiByteStr
;
325 nLength
= WideCharToMultiByte(uCodePage
, 0, lpUnicodeStr
, -1,
326 NULL
, 0, NULL
, NULL
);
330 lpMultiByteStr
= MyMalloc(nLength
);
331 if (lpMultiByteStr
== NULL
)
334 if (!WideCharToMultiByte(uCodePage
, 0, lpUnicodeStr
, -1,
335 lpMultiByteStr
, nLength
, NULL
, NULL
))
337 MyFree(lpMultiByteStr
);
341 return lpMultiByteStr
;
345 /**************************************************************************
346 * DoesUserHavePrivilege [SETUPAPI.@]
348 * Check whether the current user has got a given privilege.
351 * lpPrivilegeName [I] Name of the privilege to be checked
357 BOOL WINAPI
DoesUserHavePrivilege(LPCWSTR lpPrivilegeName
)
361 PTOKEN_PRIVILEGES lpPrivileges
;
364 BOOL bResult
= FALSE
;
366 TRACE("%s\n", debugstr_w(lpPrivilegeName
));
368 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &hToken
))
371 if (!GetTokenInformation(hToken
, TokenPrivileges
, NULL
, 0, &dwSize
))
373 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
380 lpPrivileges
= MyMalloc(dwSize
);
381 if (lpPrivileges
== NULL
)
387 if (!GetTokenInformation(hToken
, TokenPrivileges
, lpPrivileges
, dwSize
, &dwSize
))
389 MyFree(lpPrivileges
);
396 if (!LookupPrivilegeValueW(NULL
, lpPrivilegeName
, &PrivilegeLuid
))
398 MyFree(lpPrivileges
);
402 for (i
= 0; i
< lpPrivileges
->PrivilegeCount
; i
++)
404 if (lpPrivileges
->Privileges
[i
].Luid
.HighPart
== PrivilegeLuid
.HighPart
&&
405 lpPrivileges
->Privileges
[i
].Luid
.LowPart
== PrivilegeLuid
.LowPart
)
411 MyFree(lpPrivileges
);
417 /**************************************************************************
418 * EnablePrivilege [SETUPAPI.@]
420 * Enables or disables one of the current users privileges.
423 * lpPrivilegeName [I] Name of the privilege to be changed
424 * bEnable [I] TRUE: Enables the privilege
425 * FALSE: Disables the privilege
431 BOOL WINAPI
EnablePrivilege(LPCWSTR lpPrivilegeName
, BOOL bEnable
)
433 TOKEN_PRIVILEGES Privileges
;
437 TRACE("%s %s\n", debugstr_w(lpPrivilegeName
), bEnable
? "TRUE" : "FALSE");
439 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &hToken
))
442 Privileges
.PrivilegeCount
= 1;
443 Privileges
.Privileges
[0].Attributes
= (bEnable
) ? SE_PRIVILEGE_ENABLED
: 0;
445 if (!LookupPrivilegeValueW(NULL
, lpPrivilegeName
,
446 &Privileges
.Privileges
[0].Luid
))
452 bResult
= AdjustTokenPrivileges(hToken
, FALSE
, &Privileges
, 0, NULL
, NULL
);
460 /**************************************************************************
461 * DelayedMove [SETUPAPI.@]
463 * Moves a file upon the next reboot.
466 * lpExistingFileName [I] Current file name
467 * lpNewFileName [I] New file name
473 BOOL WINAPI
DelayedMove(LPCWSTR lpExistingFileName
, LPCWSTR lpNewFileName
)
475 return MoveFileExW(lpExistingFileName
, lpNewFileName
,
476 MOVEFILE_REPLACE_EXISTING
| MOVEFILE_DELAY_UNTIL_REBOOT
);
480 /**************************************************************************
481 * FileExists [SETUPAPI.@]
483 * Checks whether a file exists.
486 * lpFileName [I] Name of the file to check
487 * lpNewFileName [O] Optional information about the existing file
493 BOOL WINAPI
FileExists(LPCWSTR lpFileName
, LPWIN32_FIND_DATAW lpFileFindData
)
495 WIN32_FIND_DATAW FindData
;
500 uErrorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
);
502 hFind
= FindFirstFileW(lpFileName
, &FindData
);
503 if (hFind
== INVALID_HANDLE_VALUE
)
505 dwError
= GetLastError();
506 SetErrorMode(uErrorMode
);
507 SetLastError(dwError
);
514 *lpFileFindData
= FindData
;
516 SetErrorMode(uErrorMode
);
522 /**************************************************************************
523 * CaptureStringArg [SETUPAPI.@]
525 * Captures a UNICODE string.
528 * lpSrc [I] UNICODE string to be captured
529 * lpDst [O] Pointer to the captured UNICODE string
532 * Success: ERROR_SUCCESS
533 * Failure: ERROR_INVALID_PARAMETER
536 * Call MyFree to release the captured UNICODE string.
538 DWORD WINAPI
CaptureStringArg(LPCWSTR pSrc
, LPWSTR
*pDst
)
541 return ERROR_INVALID_PARAMETER
;
543 *pDst
= DuplicateString(pSrc
);
545 return ERROR_SUCCESS
;
549 /**************************************************************************
550 * CaptureAndConvertAnsiArg [SETUPAPI.@]
552 * Captures an ANSI string and converts it to a UNICODE string.
555 * lpSrc [I] ANSI string to be captured
556 * lpDst [O] Pointer to the captured UNICODE string
559 * Success: ERROR_SUCCESS
560 * Failure: ERROR_INVALID_PARAMETER
563 * Call MyFree to release the captured UNICODE string.
565 DWORD WINAPI
CaptureAndConvertAnsiArg(LPCSTR pSrc
, LPWSTR
*pDst
)
568 return ERROR_INVALID_PARAMETER
;
570 *pDst
= MultiByteToUnicode(pSrc
, CP_ACP
);
572 return ERROR_SUCCESS
;
576 /**************************************************************************
577 * OpenAndMapFileForRead [SETUPAPI.@]
579 * Open and map a file to a buffer.
582 * lpFileName [I] Name of the file to be opened
583 * lpSize [O] Pointer to the file size
584 * lpFile [0] Pointer to the file handle
585 * lpMapping [0] Pointer to the mapping handle
586 * lpBuffer [0] Pointer to the file buffer
589 * Success: ERROR_SUCCESS
593 * Call UnmapAndCloseFile to release the file.
595 DWORD WINAPI
OpenAndMapFileForRead(LPCWSTR lpFileName
,
603 TRACE("%s %p %p %p %p\n",
604 debugstr_w(lpFileName
), lpSize
, lpFile
, lpMapping
, lpBuffer
);
606 *lpFile
= CreateFileW(lpFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
607 OPEN_EXISTING
, 0, NULL
);
608 if (*lpFile
== INVALID_HANDLE_VALUE
)
609 return GetLastError();
611 *lpSize
= GetFileSize(*lpFile
, NULL
);
612 if (*lpSize
== INVALID_FILE_SIZE
)
614 dwError
= GetLastError();
615 CloseHandle(*lpFile
);
619 *lpMapping
= CreateFileMappingW(*lpFile
, NULL
, PAGE_READONLY
, 0,
621 if (*lpMapping
== NULL
)
623 dwError
= GetLastError();
624 CloseHandle(*lpFile
);
628 *lpBuffer
= MapViewOfFile(*lpMapping
, FILE_MAP_READ
, 0, 0, *lpSize
);
629 if (*lpBuffer
== NULL
)
631 dwError
= GetLastError();
632 CloseHandle(*lpMapping
);
633 CloseHandle(*lpFile
);
637 return ERROR_SUCCESS
;
641 /**************************************************************************
642 * UnmapAndCloseFile [SETUPAPI.@]
644 * Unmap and close a mapped file.
647 * hFile [I] Handle to the file
648 * hMapping [I] Handle to the file mapping
649 * lpBuffer [I] Pointer to the file buffer
655 BOOL WINAPI
UnmapAndCloseFile(HANDLE hFile
, HANDLE hMapping
, LPVOID lpBuffer
)
658 hFile
, hMapping
, lpBuffer
);
660 if (!UnmapViewOfFile(lpBuffer
))
663 if (!CloseHandle(hMapping
))
666 if (!CloseHandle(hFile
))
673 /**************************************************************************
674 * StampFileSecurity [SETUPAPI.@]
676 * Assign a new security descriptor to the given file.
679 * lpFileName [I] Name of the file
680 * pSecurityDescriptor [I] New security descriptor
683 * Success: ERROR_SUCCESS
686 DWORD WINAPI
StampFileSecurity(LPCWSTR lpFileName
, PSECURITY_DESCRIPTOR pSecurityDescriptor
)
688 TRACE("%s %p\n", debugstr_w(lpFileName
), pSecurityDescriptor
);
690 if (!SetFileSecurityW(lpFileName
, OWNER_SECURITY_INFORMATION
|
691 GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
692 pSecurityDescriptor
))
693 return GetLastError();
695 return ERROR_SUCCESS
;
699 /**************************************************************************
700 * TakeOwnershipOfFile [SETUPAPI.@]
702 * Takes the ownership of the given file.
705 * lpFileName [I] Name of the file
708 * Success: ERROR_SUCCESS
711 DWORD WINAPI
TakeOwnershipOfFile(LPCWSTR lpFileName
)
713 SECURITY_DESCRIPTOR SecDesc
;
714 HANDLE hToken
= NULL
;
715 PTOKEN_OWNER pOwner
= NULL
;
719 TRACE("%s\n", debugstr_w(lpFileName
));
721 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
, &hToken
))
722 return GetLastError();
724 if (!GetTokenInformation(hToken
, TokenOwner
, NULL
, 0, &dwSize
))
729 pOwner
= (PTOKEN_OWNER
)MyMalloc(dwSize
);
733 return ERROR_NOT_ENOUGH_MEMORY
;
736 if (!GetTokenInformation(hToken
, TokenOwner
, pOwner
, dwSize
, &dwSize
))
741 if (!InitializeSecurityDescriptor(&SecDesc
, SECURITY_DESCRIPTOR_REVISION
))
746 if (!SetSecurityDescriptorOwner(&SecDesc
, pOwner
->Owner
, FALSE
))
751 if (!SetFileSecurityW(lpFileName
, OWNER_SECURITY_INFORMATION
, &SecDesc
))
759 return ERROR_SUCCESS
;
762 dwError
= GetLastError();
773 /**************************************************************************
774 * RetreiveFileSecurity [SETUPAPI.@]
776 * Retrieve the security descriptor that is associated with the given file.
779 * lpFileName [I] Name of the file
782 * Success: ERROR_SUCCESS
785 DWORD WINAPI
RetreiveFileSecurity(LPCWSTR lpFileName
,
786 PSECURITY_DESCRIPTOR
*pSecurityDescriptor
)
788 PSECURITY_DESCRIPTOR SecDesc
;
789 DWORD dwSize
= 0x100;
792 SecDesc
= MyMalloc(dwSize
);
794 return ERROR_NOT_ENOUGH_MEMORY
;
796 if (GetFileSecurityW(lpFileName
, OWNER_SECURITY_INFORMATION
|
797 GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
798 SecDesc
, dwSize
, &dwSize
))
800 *pSecurityDescriptor
= SecDesc
;
801 return ERROR_SUCCESS
;
804 dwError
= GetLastError();
805 if (dwError
!= ERROR_INSUFFICIENT_BUFFER
)
811 SecDesc
= MyRealloc(SecDesc
, dwSize
);
813 return ERROR_NOT_ENOUGH_MEMORY
;
815 if (GetFileSecurityW(lpFileName
, OWNER_SECURITY_INFORMATION
|
816 GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
,
817 SecDesc
, dwSize
, &dwSize
))
819 *pSecurityDescriptor
= SecDesc
;
820 return ERROR_SUCCESS
;
823 dwError
= GetLastError();
830 static DWORD global_flags
= 0; /* FIXME: what should be in here? */
832 /***********************************************************************
833 * pSetupGetGlobalFlags (SETUPAPI.@)
835 DWORD WINAPI
pSetupGetGlobalFlags(void)
842 /***********************************************************************
843 * pSetupSetGlobalFlags (SETUPAPI.@)
845 void WINAPI
pSetupSetGlobalFlags( DWORD flags
)
847 global_flags
= flags
;
850 /***********************************************************************
851 * CMP_WaitNoPendingInstallEvents (SETUPAPI.@)
853 DWORD WINAPI
CMP_WaitNoPendingInstallEvents( DWORD dwTimeout
)
855 static BOOL warned
= FALSE
;
859 FIXME("%d\n", dwTimeout
);
862 return WAIT_OBJECT_0
;
865 /***********************************************************************
866 * AssertFail (SETUPAPI.@)
868 * Shows an assert fail error messagebox
871 * lpFile [I] file where assert failed
872 * uLine [I] line number in file
873 * lpMessage [I] assert message
876 void WINAPI
AssertFail(LPCSTR lpFile
, UINT uLine
, LPCSTR lpMessage
)
878 FIXME("%s %u %s\n", lpFile
, uLine
, lpMessage
);
881 /***********************************************************************
882 * SetupCopyOEMInfA (SETUPAPI.@)
884 BOOL WINAPI
SetupCopyOEMInfA( PCSTR source
, PCSTR location
,
885 DWORD media_type
, DWORD style
, PSTR dest
,
886 DWORD buffer_size
, PDWORD required_size
, PSTR
*component
)
889 LPWSTR destW
= NULL
, sourceW
= NULL
, locationW
= NULL
;
892 TRACE("%s, %s, %d, %d, %p, %d, %p, %p\n", debugstr_a(source
), debugstr_a(location
),
893 media_type
, style
, dest
, buffer_size
, required_size
, component
);
895 if (dest
&& !(destW
= MyMalloc( buffer_size
* sizeof(WCHAR
) ))) return FALSE
;
896 if (source
&& !(sourceW
= strdupAtoW( source
))) goto done
;
897 if (location
&& !(locationW
= strdupAtoW( location
))) goto done
;
899 if (!(ret
= SetupCopyOEMInfW( sourceW
, locationW
, media_type
, style
, destW
,
900 buffer_size
, &size
, NULL
)))
902 if (required_size
) *required_size
= size
;
908 if (buffer_size
>= size
)
910 WideCharToMultiByte( CP_ACP
, 0, destW
, -1, dest
, buffer_size
, NULL
, NULL
);
911 if (component
) *component
= strrchr( dest
, '\\' ) + 1;
915 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
922 HeapFree( GetProcessHeap(), 0, sourceW
);
923 HeapFree( GetProcessHeap(), 0, locationW
);
924 if (ret
) SetLastError(ERROR_SUCCESS
);
928 static int compare_files( HANDLE file1
, HANDLE file2
)
935 while( ReadFile(file1
, buffer1
, sizeof(buffer1
), &size1
, NULL
) &&
936 ReadFile(file2
, buffer2
, sizeof(buffer2
), &size2
, NULL
) )
940 return size1
> size2
? 1 : -1;
943 ret
= memcmp( buffer1
, buffer2
, size1
);
951 /***********************************************************************
952 * SetupCopyOEMInfW (SETUPAPI.@)
954 BOOL WINAPI
SetupCopyOEMInfW( PCWSTR source
, PCWSTR location
,
955 DWORD media_type
, DWORD style
, PWSTR dest
,
956 DWORD buffer_size
, PDWORD required_size
, PWSTR
*component
)
959 WCHAR target
[MAX_PATH
], catalog_file
[MAX_PATH
], *p
;
960 static const WCHAR inf
[] = { '\\','i','n','f','\\',0 };
961 static const WCHAR wszVersion
[] = { 'V','e','r','s','i','o','n',0 };
962 static const WCHAR wszCatalogFile
[] = { 'C','a','t','a','l','o','g','F','i','l','e',0 };
966 TRACE("%s, %s, %d, %d, %p, %d, %p, %p\n", debugstr_w(source
), debugstr_w(location
),
967 media_type
, style
, dest
, buffer_size
, required_size
, component
);
971 SetLastError(ERROR_INVALID_PARAMETER
);
975 /* check for a relative path */
976 if (!(*source
== '\\' || (*source
&& source
[1] == ':')))
978 SetLastError(ERROR_FILE_NOT_FOUND
);
982 if (!GetWindowsDirectoryW( target
, sizeof(target
)/sizeof(WCHAR
) )) return FALSE
;
984 strcatW( target
, inf
);
985 if ((p
= strrchrW( source
, '\\' )))
986 strcatW( target
, p
+ 1 );
988 /* does the file exist already? */
989 if ((GetFileAttributesW( target
) != INVALID_FILE_ATTRIBUTES
) &&
990 !(style
& SP_COPY_NOOVERWRITE
))
992 static const WCHAR oem
[] = { 'o','e','m',0 };
994 LARGE_INTEGER source_file_size
;
997 source_file
= CreateFileW( source
, FILE_READ_DATA
| FILE_READ_ATTRIBUTES
,
998 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
999 NULL
, OPEN_EXISTING
, 0, NULL
);
1000 if (source_file
== INVALID_HANDLE_VALUE
)
1003 if (!GetFileSizeEx( source_file
, &source_file_size
))
1005 CloseHandle( source_file
);
1009 p
= strrchrW( target
, '\\' ) + 1;
1010 memcpy( p
, oem
, sizeof(oem
) );
1011 p
+= sizeof(oem
)/sizeof(oem
[0]) - 1;
1013 /* generate OEMnnn.inf ending */
1014 for (i
= 0; i
< OEM_INDEX_LIMIT
; i
++)
1016 static const WCHAR format
[] = { '%','u','.','i','n','f',0 };
1018 LARGE_INTEGER dest_file_size
;
1020 wsprintfW( p
, format
, i
);
1021 dest_file
= CreateFileW( target
, FILE_READ_DATA
| FILE_READ_ATTRIBUTES
,
1022 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1023 NULL
, OPEN_EXISTING
, 0, NULL
);
1024 /* if we found a file name that doesn't exist then we're done */
1025 if (dest_file
== INVALID_HANDLE_VALUE
)
1027 /* now check if the same inf file has already been copied to the inf
1028 * directory. if so, use that file and don't create a new one */
1029 if (!GetFileSizeEx( dest_file
, &dest_file_size
) ||
1030 (dest_file_size
.QuadPart
!= source_file_size
.QuadPart
) ||
1031 compare_files( source_file
, dest_file
))
1033 CloseHandle( dest_file
);
1036 CloseHandle( dest_file
);
1040 CloseHandle( source_file
);
1041 if (i
== OEM_INDEX_LIMIT
)
1043 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1048 hinf
= SetupOpenInfFileW( source
, NULL
, INF_STYLE_WIN4
, NULL
);
1049 if (hinf
== INVALID_HANDLE_VALUE
) return FALSE
;
1051 if (SetupGetLineTextW( NULL
, hinf
, wszVersion
, wszCatalogFile
, catalog_file
,
1052 sizeof(catalog_file
)/sizeof(catalog_file
[0]), NULL
))
1054 WCHAR source_cat
[MAX_PATH
];
1055 strcpyW( source_cat
, source
);
1057 p
= strrchrW( source_cat
, '\\' );
1059 else p
= source_cat
;
1061 strcpyW( p
, catalog_file
);
1063 FIXME("install catalog file %s\n", debugstr_w( source_cat
));
1065 SetupCloseInfFile( hinf
);
1067 if (!(ret
= CopyFileW( source
, target
, (style
& SP_COPY_NOOVERWRITE
) != 0 )))
1070 if (style
& SP_COPY_DELETESOURCE
)
1071 DeleteFileW( source
);
1073 size
= strlenW( target
) + 1;
1076 if (buffer_size
>= size
)
1078 strcpyW( dest
, target
);
1082 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1087 if (component
) *component
= p
+ 1;
1088 if (required_size
) *required_size
= size
;
1089 if (ret
) SetLastError(ERROR_SUCCESS
);
1094 /***********************************************************************
1095 * InstallCatalog (SETUPAPI.@)
1097 DWORD WINAPI
InstallCatalog( LPCSTR catalog
, LPCSTR basename
, LPSTR fullname
)
1099 FIXME("%s, %s, %p\n", debugstr_a(catalog
), debugstr_a(basename
), fullname
);
1103 static UINT
detect_compression_type( LPCWSTR file
)
1107 UINT type
= FILE_COMPRESSION_NONE
;
1108 static const BYTE LZ_MAGIC
[] = { 0x53, 0x5a, 0x44, 0x44, 0x88, 0xf0, 0x27, 0x33 };
1109 static const BYTE MSZIP_MAGIC
[] = { 0x4b, 0x57, 0x41, 0x4a };
1110 static const BYTE NTCAB_MAGIC
[] = { 0x4d, 0x53, 0x43, 0x46 };
1113 handle
= CreateFileW( file
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
1114 if (handle
== INVALID_HANDLE_VALUE
)
1116 ERR("cannot open file %s\n", debugstr_w(file
));
1117 return FILE_COMPRESSION_NONE
;
1119 if (!ReadFile( handle
, buffer
, sizeof(buffer
), &size
, NULL
) || size
!= sizeof(buffer
))
1121 CloseHandle( handle
);
1122 return FILE_COMPRESSION_NONE
;
1124 if (!memcmp( buffer
, LZ_MAGIC
, sizeof(LZ_MAGIC
) )) type
= FILE_COMPRESSION_WINLZA
;
1125 else if (!memcmp( buffer
, MSZIP_MAGIC
, sizeof(MSZIP_MAGIC
) )) type
= FILE_COMPRESSION_MSZIP
;
1126 else if (!memcmp( buffer
, NTCAB_MAGIC
, sizeof(NTCAB_MAGIC
) )) type
= FILE_COMPRESSION_MSZIP
; /* not a typo */
1128 CloseHandle( handle
);
1132 static BOOL
get_file_size( LPCWSTR file
, DWORD
*size
)
1136 handle
= CreateFileW( file
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
1137 if (handle
== INVALID_HANDLE_VALUE
)
1139 ERR("cannot open file %s\n", debugstr_w(file
));
1142 *size
= GetFileSize( handle
, NULL
);
1143 CloseHandle( handle
);
1147 static BOOL
get_file_sizes_none( LPCWSTR source
, DWORD
*source_size
, DWORD
*target_size
)
1151 if (!get_file_size( source
, &size
)) return FALSE
;
1152 if (source_size
) *source_size
= size
;
1153 if (target_size
) *target_size
= size
;
1157 static BOOL
get_file_sizes_lz( LPCWSTR source
, DWORD
*source_size
, DWORD
*target_size
)
1164 if (!get_file_size( source
, &size
)) ret
= FALSE
;
1165 else *source_size
= size
;
1172 if ((file
= LZOpenFileW( (LPWSTR
)source
, &of
, OF_READ
)) < 0)
1174 ERR("cannot open source file for reading\n");
1177 *target_size
= LZSeek( file
, 0, 2 );
1183 static UINT CALLBACK
file_compression_info_callback( PVOID context
, UINT notification
, UINT_PTR param1
, UINT_PTR param2
)
1185 DWORD
*size
= context
;
1186 FILE_IN_CABINET_INFO_W
*info
= (FILE_IN_CABINET_INFO_W
*)param1
;
1188 switch (notification
)
1190 case SPFILENOTIFY_FILEINCABINET
:
1192 *size
= info
->FileSize
;
1195 default: return NO_ERROR
;
1199 static BOOL
get_file_sizes_cab( LPCWSTR source
, DWORD
*source_size
, DWORD
*target_size
)
1206 if (!get_file_size( source
, &size
)) ret
= FALSE
;
1207 else *source_size
= size
;
1211 ret
= SetupIterateCabinetW( source
, 0, file_compression_info_callback
, target_size
);
1216 /***********************************************************************
1217 * SetupGetFileCompressionInfoExA (SETUPAPI.@)
1219 * See SetupGetFileCompressionInfoExW.
1221 BOOL WINAPI
SetupGetFileCompressionInfoExA( PCSTR source
, PSTR name
, DWORD len
, PDWORD required
,
1222 PDWORD source_size
, PDWORD target_size
, PUINT type
)
1225 WCHAR
*nameW
= NULL
, *sourceW
= NULL
;
1229 TRACE("%s, %p, %d, %p, %p, %p, %p\n", debugstr_a(source
), name
, len
, required
,
1230 source_size
, target_size
, type
);
1232 if (!source
|| !(sourceW
= MultiByteToUnicode( source
, CP_ACP
))) return FALSE
;
1236 ret
= SetupGetFileCompressionInfoExW( sourceW
, NULL
, 0, &nb_chars
, NULL
, NULL
, NULL
);
1237 if (!(nameW
= HeapAlloc( GetProcessHeap(), 0, nb_chars
* sizeof(WCHAR
) )))
1243 ret
= SetupGetFileCompressionInfoExW( sourceW
, nameW
, nb_chars
, &nb_chars
, source_size
, target_size
, type
);
1246 if ((nameA
= UnicodeToMultiByte( nameW
, CP_ACP
)))
1248 if (name
&& len
>= nb_chars
) lstrcpyA( name
, nameA
);
1251 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1257 if (required
) *required
= nb_chars
;
1258 HeapFree( GetProcessHeap(), 0, nameW
);
1264 /***********************************************************************
1265 * SetupGetFileCompressionInfoExW (SETUPAPI.@)
1267 * Get compression type and compressed/uncompressed sizes of a given file.
1270 * source [I] File to examine.
1271 * name [O] Actual filename used.
1272 * len [I] Length in characters of 'name' buffer.
1273 * required [O] Number of characters written to 'name'.
1274 * source_size [O] Size of compressed file.
1275 * target_size [O] Size of uncompressed file.
1276 * type [O] Compression type.
1282 BOOL WINAPI
SetupGetFileCompressionInfoExW( PCWSTR source
, PWSTR name
, DWORD len
, PDWORD required
,
1283 PDWORD source_size
, PDWORD target_size
, PUINT type
)
1289 TRACE("%s, %p, %d, %p, %p, %p, %p\n", debugstr_w(source
), name
, len
, required
,
1290 source_size
, target_size
, type
);
1292 if (!source
) return FALSE
;
1294 source_len
= lstrlenW( source
) + 1;
1295 if (required
) *required
= source_len
;
1296 if (name
&& len
>= source_len
)
1298 lstrcpyW( name
, source
);
1303 comp
= detect_compression_type( source
);
1304 if (type
) *type
= comp
;
1308 case FILE_COMPRESSION_MSZIP
:
1309 case FILE_COMPRESSION_NTCAB
: ret
= get_file_sizes_cab( source
, source_size
, target_size
); break;
1310 case FILE_COMPRESSION_NONE
: ret
= get_file_sizes_none( source
, source_size
, target_size
); break;
1311 case FILE_COMPRESSION_WINLZA
: ret
= get_file_sizes_lz( source
, source_size
, target_size
); break;
1317 /***********************************************************************
1318 * SetupGetFileCompressionInfoA (SETUPAPI.@)
1320 * See SetupGetFileCompressionInfoW.
1322 DWORD WINAPI
SetupGetFileCompressionInfoA( PCSTR source
, PSTR
*name
, PDWORD source_size
,
1323 PDWORD target_size
, PUINT type
)
1326 DWORD error
, required
;
1329 TRACE("%s, %p, %p, %p, %p\n", debugstr_a(source
), name
, source_size
, target_size
, type
);
1331 if (!source
|| !name
|| !source_size
|| !target_size
|| !type
)
1332 return ERROR_INVALID_PARAMETER
;
1334 ret
= SetupGetFileCompressionInfoExA( source
, NULL
, 0, &required
, NULL
, NULL
, NULL
);
1335 if (!(actual_name
= MyMalloc( required
))) return ERROR_NOT_ENOUGH_MEMORY
;
1337 ret
= SetupGetFileCompressionInfoExA( source
, actual_name
, required
, &required
,
1338 source_size
, target_size
, type
);
1341 error
= GetLastError();
1342 MyFree( actual_name
);
1345 *name
= actual_name
;
1346 return ERROR_SUCCESS
;
1349 /***********************************************************************
1350 * SetupGetFileCompressionInfoW (SETUPAPI.@)
1352 * Get compression type and compressed/uncompressed sizes of a given file.
1355 * source [I] File to examine.
1356 * name [O] Actual filename used.
1357 * source_size [O] Size of compressed file.
1358 * target_size [O] Size of uncompressed file.
1359 * type [O] Compression type.
1362 * Success: ERROR_SUCCESS
1363 * Failure: Win32 error code.
1365 DWORD WINAPI
SetupGetFileCompressionInfoW( PCWSTR source
, PWSTR
*name
, PDWORD source_size
,
1366 PDWORD target_size
, PUINT type
)
1369 DWORD error
, required
;
1372 TRACE("%s, %p, %p, %p, %p\n", debugstr_w(source
), name
, source_size
, target_size
, type
);
1374 if (!source
|| !name
|| !source_size
|| !target_size
|| !type
)
1375 return ERROR_INVALID_PARAMETER
;
1377 ret
= SetupGetFileCompressionInfoExW( source
, NULL
, 0, &required
, NULL
, NULL
, NULL
);
1378 if (!(actual_name
= MyMalloc( required
))) return ERROR_NOT_ENOUGH_MEMORY
;
1380 ret
= SetupGetFileCompressionInfoExW( source
, actual_name
, required
, &required
,
1381 source_size
, target_size
, type
);
1384 error
= GetLastError();
1385 MyFree( actual_name
);
1388 *name
= actual_name
;
1389 return ERROR_SUCCESS
;
1392 static DWORD
decompress_file_lz( LPCWSTR source
, LPCWSTR target
)
1399 if ((src
= LZOpenFileW( (LPWSTR
)source
, &sof
, OF_READ
)) < 0)
1401 ERR("cannot open source file for reading\n");
1402 return ERROR_FILE_NOT_FOUND
;
1404 if ((dst
= LZOpenFileW( (LPWSTR
)target
, &dof
, OF_CREATE
)) < 0)
1406 ERR("cannot open target file for writing\n");
1408 return ERROR_FILE_NOT_FOUND
;
1410 if ((error
= LZCopy( src
, dst
)) >= 0) ret
= ERROR_SUCCESS
;
1413 WARN("failed to decompress file %d\n", error
);
1414 ret
= ERROR_INVALID_DATA
;
1422 static UINT CALLBACK
decompress_or_copy_callback( PVOID context
, UINT notification
, UINT_PTR param1
, UINT_PTR param2
)
1424 FILE_IN_CABINET_INFO_W
*info
= (FILE_IN_CABINET_INFO_W
*)param1
;
1426 switch (notification
)
1428 case SPFILENOTIFY_FILEINCABINET
:
1430 LPCWSTR filename
, targetname
= context
;
1433 if ((p
= strrchrW( targetname
, '\\' ))) filename
= p
+ 1;
1434 else filename
= targetname
;
1436 if (!lstrcmpiW( filename
, info
->NameInCabinet
))
1438 strcpyW( info
->FullTargetName
, targetname
);
1443 default: return NO_ERROR
;
1447 static DWORD
decompress_file_cab( LPCWSTR source
, LPCWSTR target
)
1451 ret
= SetupIterateCabinetW( source
, 0, decompress_or_copy_callback
, (PVOID
)target
);
1453 if (ret
) return ERROR_SUCCESS
;
1454 else return GetLastError();
1457 /***********************************************************************
1458 * SetupDecompressOrCopyFileA (SETUPAPI.@)
1460 * See SetupDecompressOrCopyFileW.
1462 DWORD WINAPI
SetupDecompressOrCopyFileA( PCSTR source
, PCSTR target
, PUINT type
)
1465 WCHAR
*sourceW
= NULL
, *targetW
= NULL
;
1467 if (source
&& !(sourceW
= MultiByteToUnicode( source
, CP_ACP
))) return FALSE
;
1468 if (target
&& !(targetW
= MultiByteToUnicode( target
, CP_ACP
)))
1471 return ERROR_NOT_ENOUGH_MEMORY
;
1474 ret
= SetupDecompressOrCopyFileW( sourceW
, targetW
, type
);
1482 /***********************************************************************
1483 * SetupDecompressOrCopyFileW (SETUPAPI.@)
1485 * Copy a file and decompress it if needed.
1488 * source [I] File to copy.
1489 * target [I] Filename of the copy.
1490 * type [I] Compression type.
1493 * Success: ERROR_SUCCESS
1494 * Failure: Win32 error code.
1496 DWORD WINAPI
SetupDecompressOrCopyFileW( PCWSTR source
, PCWSTR target
, PUINT type
)
1499 DWORD ret
= ERROR_INVALID_PARAMETER
;
1501 if (!source
|| !target
) return ERROR_INVALID_PARAMETER
;
1503 if (!type
) comp
= detect_compression_type( source
);
1508 case FILE_COMPRESSION_NONE
:
1509 if (CopyFileW( source
, target
, FALSE
)) ret
= ERROR_SUCCESS
;
1510 else ret
= GetLastError();
1512 case FILE_COMPRESSION_WINLZA
:
1513 ret
= decompress_file_lz( source
, target
);
1515 case FILE_COMPRESSION_NTCAB
:
1516 case FILE_COMPRESSION_MSZIP
:
1517 ret
= decompress_file_cab( source
, target
);
1520 WARN("unknown compression type %d\n", comp
);
1524 TRACE("%s -> %s %d\n", debugstr_w(source
), debugstr_w(target
), comp
);