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
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
38 #include "setupapi_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
42 /* arbitrary limit not related to what native actually uses */
43 #define OEM_INDEX_LIMIT 999
45 /**************************************************************************
48 * Frees an allocated memory block from the process heap.
51 * lpMem [I] pointer to memory block which will be freed
56 VOID WINAPI MyFree(LPVOID lpMem)
58 HeapFree(GetProcessHeap(), 0, lpMem);
62 /**************************************************************************
63 * MyMalloc [SETUPAPI.@]
65 * Allocates memory block from the process heap.
68 * dwSize [I] size of the allocated memory block
71 * Success: pointer to allocated memory block
74 LPVOID WINAPI MyMalloc(DWORD dwSize)
76 return HeapAlloc(GetProcessHeap(), 0, dwSize);
80 /**************************************************************************
81 * MyRealloc [SETUPAPI.@]
83 * Changes the size of an allocated memory block or allocates a memory
84 * block from the process heap.
87 * lpSrc [I] pointer to memory block which will be resized
88 * dwSize [I] new size of the memory block
91 * Success: pointer to the resized memory block
95 * If lpSrc is a NULL-pointer, then MyRealloc allocates a memory
96 * block like MyMalloc.
98 LPVOID WINAPI MyRealloc(LPVOID lpSrc, DWORD dwSize)
101 return HeapAlloc(GetProcessHeap(), 0, dwSize);
103 return HeapReAlloc(GetProcessHeap(), 0, lpSrc, dwSize);
107 /**************************************************************************
108 * DuplicateString [SETUPAPI.@]
110 * Duplicates a unicode string.
113 * lpSrc [I] pointer to the unicode string that will be duplicated
116 * Success: pointer to the duplicated unicode string
120 * Call MyFree() to release the duplicated string.
122 LPWSTR WINAPI DuplicateString(LPCWSTR lpSrc)
126 lpDst = MyMalloc((lstrlenW(lpSrc) + 1) * sizeof(WCHAR));
130 strcpyW(lpDst, lpSrc);
136 /**************************************************************************
137 * QueryRegistryValue [SETUPAPI.@]
139 * Retrieves value data from the registry and allocates memory for the
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
150 * Success: ERROR_SUCCESS
154 * Use MyFree to release the lpData buffer.
156 LONG WINAPI QueryRegistryValue(HKEY hKey,
164 TRACE("%p %s %p %p %p\n",
165 hKey, debugstr_w(lpValueName), lpData, lpType, lpcbData);
167 /* Get required buffer size */
169 lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, NULL, lpcbData);
170 if (lError != ERROR_SUCCESS)
173 /* Allocate buffer */
174 *lpData = MyMalloc(*lpcbData);
176 return ERROR_NOT_ENOUGH_MEMORY;
178 /* Query registry value */
179 lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, *lpData, lpcbData);
180 if (lError != ERROR_SUCCESS)
187 /**************************************************************************
188 * IsUserAdmin [SETUPAPI.@]
190 * Checks whether the current user is a member of the Administrators group.
199 BOOL WINAPI IsUserAdmin(VOID)
202 return IsUserAnAdmin();
206 /**************************************************************************
207 * MultiByteToUnicode [SETUPAPI.@]
209 * Converts a multi-byte string to a Unicode string.
212 * lpMultiByteStr [I] Multi-byte string to be converted
213 * uCodePage [I] Code page
216 * Success: pointer to the converted Unicode string
220 * Use MyFree to release the returned Unicode string.
222 LPWSTR WINAPI MultiByteToUnicode(LPCSTR lpMultiByteStr, UINT uCodePage)
227 nLength = MultiByteToWideChar(uCodePage, 0, lpMultiByteStr,
232 lpUnicodeStr = MyMalloc(nLength * sizeof(WCHAR));
233 if (lpUnicodeStr == NULL)
236 if (!MultiByteToWideChar(uCodePage, 0, lpMultiByteStr,
237 nLength, lpUnicodeStr, nLength))
239 MyFree(lpUnicodeStr);
247 /**************************************************************************
248 * UnicodeToMultiByte [SETUPAPI.@]
250 * Converts a Unicode string to a multi-byte string.
253 * lpUnicodeStr [I] Unicode string to be converted
254 * uCodePage [I] Code page
257 * Success: pointer to the converted multi-byte string
261 * Use MyFree to release the returned multi-byte string.
263 LPSTR WINAPI UnicodeToMultiByte(LPCWSTR lpUnicodeStr, UINT uCodePage)
265 LPSTR lpMultiByteStr;
268 nLength = WideCharToMultiByte(uCodePage, 0, lpUnicodeStr, -1,
269 NULL, 0, NULL, NULL);
273 lpMultiByteStr = MyMalloc(nLength);
274 if (lpMultiByteStr == NULL)
277 if (!WideCharToMultiByte(uCodePage, 0, lpUnicodeStr, -1,
278 lpMultiByteStr, nLength, NULL, NULL))
280 MyFree(lpMultiByteStr);
284 return lpMultiByteStr;
288 /**************************************************************************
289 * DoesUserHavePrivilege [SETUPAPI.@]
291 * Check whether the current user has got a given privilege.
294 * lpPrivilegeName [I] Name of the privilege to be checked
300 BOOL WINAPI DoesUserHavePrivilege(LPCWSTR lpPrivilegeName)
304 PTOKEN_PRIVILEGES lpPrivileges;
307 BOOL bResult = FALSE;
309 TRACE("%s\n", debugstr_w(lpPrivilegeName));
311 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
314 if (!GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &dwSize))
316 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
323 lpPrivileges = MyMalloc(dwSize);
324 if (lpPrivileges == NULL)
330 if (!GetTokenInformation(hToken, TokenPrivileges, lpPrivileges, dwSize, &dwSize))
332 MyFree(lpPrivileges);
339 if (!LookupPrivilegeValueW(NULL, lpPrivilegeName, &PrivilegeLuid))
341 MyFree(lpPrivileges);
345 for (i = 0; i < lpPrivileges->PrivilegeCount; i++)
347 if (lpPrivileges->Privileges[i].Luid.HighPart == PrivilegeLuid.HighPart &&
348 lpPrivileges->Privileges[i].Luid.LowPart == PrivilegeLuid.LowPart)
354 MyFree(lpPrivileges);
360 /**************************************************************************
361 * EnablePrivilege [SETUPAPI.@]
363 * Enables or disables one of the current users privileges.
366 * lpPrivilegeName [I] Name of the privilege to be changed
367 * bEnable [I] TRUE: Enables the privilege
368 * FALSE: Disables the privilege
374 BOOL WINAPI EnablePrivilege(LPCWSTR lpPrivilegeName, BOOL bEnable)
376 TOKEN_PRIVILEGES Privileges;
380 TRACE("%s %s\n", debugstr_w(lpPrivilegeName), bEnable ? "TRUE" : "FALSE");
382 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
385 Privileges.PrivilegeCount = 1;
386 Privileges.Privileges[0].Attributes = (bEnable) ? SE_PRIVILEGE_ENABLED : 0;
388 if (!LookupPrivilegeValueW(NULL, lpPrivilegeName,
389 &Privileges.Privileges[0].Luid))
395 bResult = AdjustTokenPrivileges(hToken, FALSE, &Privileges, 0, NULL, NULL);
403 /**************************************************************************
404 * DelayedMove [SETUPAPI.@]
406 * Moves a file upon the next reboot.
409 * lpExistingFileName [I] Current file name
410 * lpNewFileName [I] New file name
416 BOOL WINAPI DelayedMove(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName)
418 return MoveFileExW(lpExistingFileName, lpNewFileName,
419 MOVEFILE_REPLACE_EXISTING | MOVEFILE_DELAY_UNTIL_REBOOT);
423 /**************************************************************************
424 * FileExists [SETUPAPI.@]
426 * Checks whether a file exists.
429 * lpFileName [I] Name of the file to check
430 * lpNewFileName [O] Optional information about the existing file
436 BOOL WINAPI FileExists(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFileFindData)
438 WIN32_FIND_DATAW FindData;
443 uErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
445 hFind = FindFirstFileW(lpFileName, &FindData);
446 if (hFind == INVALID_HANDLE_VALUE)
448 dwError = GetLastError();
449 SetErrorMode(uErrorMode);
450 SetLastError(dwError);
457 *lpFileFindData = FindData;
459 SetErrorMode(uErrorMode);
465 /**************************************************************************
466 * CaptureStringArg [SETUPAPI.@]
468 * Captures a UNICODE string.
471 * lpSrc [I] UNICODE string to be captured
472 * lpDst [O] Pointer to the captured UNICODE string
475 * Success: ERROR_SUCCESS
476 * Failure: ERROR_INVALID_PARAMETER
479 * Call MyFree to release the captured UNICODE string.
481 DWORD WINAPI CaptureStringArg(LPCWSTR pSrc, LPWSTR *pDst)
484 return ERROR_INVALID_PARAMETER;
486 *pDst = DuplicateString(pSrc);
488 return ERROR_SUCCESS;
492 /**************************************************************************
493 * CaptureAndConvertAnsiArg [SETUPAPI.@]
495 * Captures an ANSI string and converts it to a UNICODE string.
498 * lpSrc [I] ANSI string to be captured
499 * lpDst [O] Pointer to the captured UNICODE string
502 * Success: ERROR_SUCCESS
503 * Failure: ERROR_INVALID_PARAMETER
506 * Call MyFree to release the captured UNICODE string.
508 DWORD WINAPI CaptureAndConvertAnsiArg(LPCSTR pSrc, LPWSTR *pDst)
511 return ERROR_INVALID_PARAMETER;
513 *pDst = MultiByteToUnicode(pSrc, CP_ACP);
515 return ERROR_SUCCESS;
519 /**************************************************************************
520 * OpenAndMapFileForRead [SETUPAPI.@]
522 * Open and map a file to a buffer.
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
532 * Success: ERROR_SUCCESS
536 * Call UnmapAndCloseFile to release the file.
538 DWORD WINAPI OpenAndMapFileForRead(LPCWSTR lpFileName,
546 TRACE("%s %p %p %p %p\n",
547 debugstr_w(lpFileName), lpSize, lpFile, lpMapping, lpBuffer);
549 *lpFile = CreateFileW(lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
550 OPEN_EXISTING, 0, NULL);
551 if (*lpFile == INVALID_HANDLE_VALUE)
552 return GetLastError();
554 *lpSize = GetFileSize(*lpFile, NULL);
555 if (*lpSize == INVALID_FILE_SIZE)
557 dwError = GetLastError();
558 CloseHandle(*lpFile);
562 *lpMapping = CreateFileMappingW(*lpFile, NULL, PAGE_READONLY, 0,
564 if (*lpMapping == NULL)
566 dwError = GetLastError();
567 CloseHandle(*lpFile);
571 *lpBuffer = MapViewOfFile(*lpMapping, FILE_MAP_READ, 0, 0, *lpSize);
572 if (*lpBuffer == NULL)
574 dwError = GetLastError();
575 CloseHandle(*lpMapping);
576 CloseHandle(*lpFile);
580 return ERROR_SUCCESS;
584 /**************************************************************************
585 * UnmapAndCloseFile [SETUPAPI.@]
587 * Unmap and close a mapped file.
590 * hFile [I] Handle to the file
591 * hMapping [I] Handle to the file mapping
592 * lpBuffer [I] Pointer to the file buffer
598 BOOL WINAPI UnmapAndCloseFile(HANDLE hFile, HANDLE hMapping, LPVOID lpBuffer)
601 hFile, hMapping, lpBuffer);
603 if (!UnmapViewOfFile(lpBuffer))
606 if (!CloseHandle(hMapping))
609 if (!CloseHandle(hFile))
616 /**************************************************************************
617 * StampFileSecurity [SETUPAPI.@]
619 * Assign a new security descriptor to the given file.
622 * lpFileName [I] Name of the file
623 * pSecurityDescriptor [I] New security descriptor
626 * Success: ERROR_SUCCESS
629 DWORD WINAPI StampFileSecurity(LPCWSTR lpFileName, PSECURITY_DESCRIPTOR pSecurityDescriptor)
631 TRACE("%s %p\n", debugstr_w(lpFileName), pSecurityDescriptor);
633 if (!SetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
634 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
635 pSecurityDescriptor))
636 return GetLastError();
638 return ERROR_SUCCESS;
642 /**************************************************************************
643 * TakeOwnershipOfFile [SETUPAPI.@]
645 * Takes the ownership of the given file.
648 * lpFileName [I] Name of the file
651 * Success: ERROR_SUCCESS
654 DWORD WINAPI TakeOwnershipOfFile(LPCWSTR lpFileName)
656 SECURITY_DESCRIPTOR SecDesc;
657 HANDLE hToken = NULL;
658 PTOKEN_OWNER pOwner = NULL;
662 TRACE("%s\n", debugstr_w(lpFileName));
664 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
665 return GetLastError();
667 if (!GetTokenInformation(hToken, TokenOwner, NULL, 0, &dwSize))
672 pOwner = MyMalloc(dwSize);
676 return ERROR_NOT_ENOUGH_MEMORY;
679 if (!GetTokenInformation(hToken, TokenOwner, pOwner, dwSize, &dwSize))
684 if (!InitializeSecurityDescriptor(&SecDesc, SECURITY_DESCRIPTOR_REVISION))
689 if (!SetSecurityDescriptorOwner(&SecDesc, pOwner->Owner, FALSE))
694 if (!SetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION, &SecDesc))
702 return ERROR_SUCCESS;
705 dwError = GetLastError();
716 /**************************************************************************
717 * RetreiveFileSecurity [SETUPAPI.@]
719 * Retrieve the security descriptor that is associated with the given file.
722 * lpFileName [I] Name of the file
725 * Success: ERROR_SUCCESS
728 DWORD WINAPI RetreiveFileSecurity(LPCWSTR lpFileName,
729 PSECURITY_DESCRIPTOR *pSecurityDescriptor)
731 PSECURITY_DESCRIPTOR SecDesc;
732 DWORD dwSize = 0x100;
735 SecDesc = MyMalloc(dwSize);
737 return ERROR_NOT_ENOUGH_MEMORY;
739 if (GetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
740 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
741 SecDesc, dwSize, &dwSize))
743 *pSecurityDescriptor = SecDesc;
744 return ERROR_SUCCESS;
747 dwError = GetLastError();
748 if (dwError != ERROR_INSUFFICIENT_BUFFER)
754 SecDesc = MyRealloc(SecDesc, dwSize);
756 return ERROR_NOT_ENOUGH_MEMORY;
758 if (GetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
759 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
760 SecDesc, dwSize, &dwSize))
762 *pSecurityDescriptor = SecDesc;
763 return ERROR_SUCCESS;
766 dwError = GetLastError();
773 static DWORD global_flags = 0; /* FIXME: what should be in here? */
775 /***********************************************************************
776 * pSetupGetGlobalFlags (SETUPAPI.@)
778 DWORD WINAPI pSetupGetGlobalFlags(void)
785 /***********************************************************************
786 * pSetupSetGlobalFlags (SETUPAPI.@)
788 void WINAPI pSetupSetGlobalFlags( DWORD flags )
790 global_flags = flags;
793 /***********************************************************************
794 * CMP_WaitNoPendingInstallEvents (SETUPAPI.@)
796 DWORD WINAPI CMP_WaitNoPendingInstallEvents( DWORD dwTimeout )
798 static BOOL warned = FALSE;
802 FIXME("%d\n", dwTimeout);
805 return WAIT_OBJECT_0;
808 /***********************************************************************
809 * AssertFail (SETUPAPI.@)
811 * Shows an assert fail error messagebox
814 * lpFile [I] file where assert failed
815 * uLine [I] line number in file
816 * lpMessage [I] assert message
819 void WINAPI AssertFail(LPCSTR lpFile, UINT uLine, LPCSTR lpMessage)
821 FIXME("%s %u %s\n", lpFile, uLine, lpMessage);
824 /***********************************************************************
825 * SetupCopyOEMInfA (SETUPAPI.@)
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 )
832 LPWSTR destW = NULL, sourceW = NULL, locationW = NULL;
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);
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;
842 if (!(ret = SetupCopyOEMInfW( sourceW, locationW, media_type, style, destW,
843 buffer_size, &size, NULL )))
845 if (required_size) *required_size = size;
851 if (buffer_size >= size)
853 WideCharToMultiByte( CP_ACP, 0, destW, -1, dest, buffer_size, NULL, NULL );
854 if (component) *component = strrchr( dest, '\\' ) + 1;
858 SetLastError( ERROR_INSUFFICIENT_BUFFER );
865 HeapFree( GetProcessHeap(), 0, sourceW );
866 HeapFree( GetProcessHeap(), 0, locationW );
867 if (ret) SetLastError(ERROR_SUCCESS);
871 static int compare_files( HANDLE file1, HANDLE file2 )
878 while( ReadFile(file1, buffer1, sizeof(buffer1), &size1, NULL) &&
879 ReadFile(file2, buffer2, sizeof(buffer2), &size2, NULL) )
883 return size1 > size2 ? 1 : -1;
886 ret = memcmp( buffer1, buffer2, size1 );
894 /***********************************************************************
895 * SetupCopyOEMInfW (SETUPAPI.@)
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 )
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 };
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);
914 SetLastError(ERROR_INVALID_PARAMETER);
918 /* check for a relative path */
919 if (!(*source == '\\' || (*source && source[1] == ':')))
921 SetLastError(ERROR_FILE_NOT_FOUND);
925 if (!GetWindowsDirectoryW( target, sizeof(target)/sizeof(WCHAR) )) return FALSE;
927 strcatW( target, inf );
928 if ((p = strrchrW( source, '\\' )))
929 strcatW( target, p + 1 );
931 /* does the file exist already? */
932 if ((GetFileAttributesW( target ) != INVALID_FILE_ATTRIBUTES) &&
933 !(style & SP_COPY_NOOVERWRITE))
935 static const WCHAR oem[] = { 'o','e','m',0 };
937 LARGE_INTEGER source_file_size;
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)
946 if (!GetFileSizeEx( source_file, &source_file_size ))
948 CloseHandle( source_file );
952 p = strrchrW( target, '\\' ) + 1;
953 memcpy( p, oem, sizeof(oem) );
954 p += sizeof(oem)/sizeof(oem[0]) - 1;
956 /* generate OEMnnn.inf ending */
957 for (i = 0; i < OEM_INDEX_LIMIT; i++)
959 static const WCHAR format[] = { '%','u','.','i','n','f',0 };
961 LARGE_INTEGER dest_file_size;
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)
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 ))
976 CloseHandle( dest_file );
979 CloseHandle( dest_file );
983 CloseHandle( source_file );
984 if (i == OEM_INDEX_LIMIT)
986 SetLastError( ERROR_FILENAME_EXCED_RANGE );
991 hinf = SetupOpenInfFileW( source, NULL, INF_STYLE_WIN4, NULL );
992 if (hinf == INVALID_HANDLE_VALUE) return FALSE;
994 if (SetupGetLineTextW( NULL, hinf, wszVersion, wszCatalogFile, catalog_file,
995 sizeof(catalog_file)/sizeof(catalog_file[0]), NULL ))
997 WCHAR source_cat[MAX_PATH];
1000 GUID msguid = DRIVER_ACTION_VERIFY;
1002 SetupCloseInfFile( hinf );
1004 strcpyW( source_cat, source );
1005 p = strrchrW( source_cat, '\\' );
1007 else p = source_cat;
1008 strcpyW( p, catalog_file );
1010 TRACE("installing catalog file %s\n", debugstr_w( source_cat ));
1012 if (!CryptCATAdminAcquireContext(&handle, &msguid, 0))
1014 ERR("Could not acquire security context\n");
1018 if (!(cat = CryptCATAdminAddCatalog(handle, source_cat, catalog_file, 0)))
1020 ERR("Could not add catalog\n");
1021 CryptCATAdminReleaseContext(handle, 0);
1025 CryptCATAdminReleaseCatalogContext(handle, cat, 0);
1026 CryptCATAdminReleaseContext(handle, 0);
1029 SetupCloseInfFile( hinf );
1031 if (!(ret = CopyFileW( source, target, (style & SP_COPY_NOOVERWRITE) != 0 )))
1034 if (style & SP_COPY_DELETESOURCE)
1035 DeleteFileW( source );
1037 size = strlenW( target ) + 1;
1040 if (buffer_size >= size)
1042 strcpyW( dest, target );
1046 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1051 if (component) *component = p + 1;
1052 if (required_size) *required_size = size;
1053 if (ret) SetLastError(ERROR_SUCCESS);
1058 /***********************************************************************
1059 * SetupUninstallOEMInfA (SETUPAPI.@)
1061 BOOL WINAPI SetupUninstallOEMInfA( PCSTR inf_file, DWORD flags, PVOID reserved )
1064 WCHAR *inf_fileW = NULL;
1066 TRACE("%s, 0x%08x, %p\n", debugstr_a(inf_file), flags, reserved);
1068 if (inf_file && !(inf_fileW = strdupAtoW( inf_file ))) return FALSE;
1069 ret = SetupUninstallOEMInfW( inf_fileW, flags, reserved );
1070 HeapFree( GetProcessHeap(), 0, inf_fileW );
1074 /***********************************************************************
1075 * SetupUninstallOEMInfW (SETUPAPI.@)
1077 BOOL WINAPI SetupUninstallOEMInfW( PCWSTR inf_file, DWORD flags, PVOID reserved )
1079 static const WCHAR infW[] = {'\\','i','n','f','\\',0};
1080 WCHAR target[MAX_PATH];
1082 TRACE("%s, 0x%08x, %p\n", debugstr_w(inf_file), flags, reserved);
1086 SetLastError(ERROR_INVALID_PARAMETER);
1090 if (!GetWindowsDirectoryW( target, sizeof(target)/sizeof(WCHAR) )) return FALSE;
1092 strcatW( target, infW );
1093 strcatW( target, inf_file );
1095 if (flags & SUOI_FORCEDELETE)
1096 return DeleteFileW(target);
1098 FIXME("not deleting %s\n", debugstr_w(target));
1103 /***********************************************************************
1104 * InstallCatalog (SETUPAPI.@)
1106 DWORD WINAPI InstallCatalog( LPCSTR catalog, LPCSTR basename, LPSTR fullname )
1108 FIXME("%s, %s, %p\n", debugstr_a(catalog), debugstr_a(basename), fullname);
1112 /***********************************************************************
1113 * pSetupInstallCatalog (SETUPAPI.@)
1115 DWORD WINAPI pSetupInstallCatalog( LPCWSTR catalog, LPCWSTR basename, LPWSTR fullname )
1120 TRACE ("%s, %s, %p\n", debugstr_w(catalog), debugstr_w(basename), fullname);
1122 if (!CryptCATAdminAcquireContext(&admin,NULL,0))
1123 return GetLastError();
1125 if (!(cat = CryptCATAdminAddCatalog( admin, (PWSTR)catalog, (PWSTR)basename, 0 )))
1127 DWORD rc = GetLastError();
1128 CryptCATAdminReleaseContext(admin, 0);
1131 CryptCATAdminReleaseCatalogContext(admin, cat, 0);
1132 CryptCATAdminReleaseContext(admin,0);
1135 FIXME("not returning full installed catalog path\n");
1140 static UINT detect_compression_type( LPCWSTR file )
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 };
1150 handle = CreateFileW( file, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL );
1151 if (handle == INVALID_HANDLE_VALUE)
1153 ERR("cannot open file %s\n", debugstr_w(file));
1154 return FILE_COMPRESSION_NONE;
1156 if (!ReadFile( handle, buffer, sizeof(buffer), &size, NULL ) || size != sizeof(buffer))
1158 CloseHandle( handle );
1159 return FILE_COMPRESSION_NONE;
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 */
1165 CloseHandle( handle );
1169 static BOOL get_file_size( LPCWSTR file, DWORD *size )
1173 handle = CreateFileW( file, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL );
1174 if (handle == INVALID_HANDLE_VALUE)
1176 ERR("cannot open file %s\n", debugstr_w(file));
1179 *size = GetFileSize( handle, NULL );
1180 CloseHandle( handle );
1184 static BOOL get_file_sizes_none( LPCWSTR source, DWORD *source_size, DWORD *target_size )
1188 if (!get_file_size( source, &size )) return FALSE;
1189 if (source_size) *source_size = size;
1190 if (target_size) *target_size = size;
1194 static BOOL get_file_sizes_lz( LPCWSTR source, DWORD *source_size, DWORD *target_size )
1201 if (!get_file_size( source, &size )) ret = FALSE;
1202 else *source_size = size;
1209 if ((file = LZOpenFileW( (LPWSTR)source, &of, OF_READ )) < 0)
1211 ERR("cannot open source file for reading\n");
1214 *target_size = LZSeek( file, 0, 2 );
1220 static UINT CALLBACK file_compression_info_callback( PVOID context, UINT notification, UINT_PTR param1, UINT_PTR param2 )
1222 DWORD *size = context;
1223 FILE_IN_CABINET_INFO_W *info = (FILE_IN_CABINET_INFO_W *)param1;
1225 switch (notification)
1227 case SPFILENOTIFY_FILEINCABINET:
1229 *size = info->FileSize;
1232 default: return NO_ERROR;
1236 static BOOL get_file_sizes_cab( LPCWSTR source, DWORD *source_size, DWORD *target_size )
1243 if (!get_file_size( source, &size )) ret = FALSE;
1244 else *source_size = size;
1248 ret = SetupIterateCabinetW( source, 0, file_compression_info_callback, target_size );
1253 /***********************************************************************
1254 * SetupGetFileCompressionInfoExA (SETUPAPI.@)
1256 * See SetupGetFileCompressionInfoExW.
1258 BOOL WINAPI SetupGetFileCompressionInfoExA( PCSTR source, PSTR name, DWORD len, PDWORD required,
1259 PDWORD source_size, PDWORD target_size, PUINT type )
1262 WCHAR *nameW = NULL, *sourceW = NULL;
1266 TRACE("%s, %p, %d, %p, %p, %p, %p\n", debugstr_a(source), name, len, required,
1267 source_size, target_size, type);
1269 if (!source || !(sourceW = MultiByteToUnicode( source, CP_ACP ))) return FALSE;
1273 ret = SetupGetFileCompressionInfoExW( sourceW, NULL, 0, &nb_chars, NULL, NULL, NULL );
1274 if (!(nameW = HeapAlloc( GetProcessHeap(), 0, nb_chars * sizeof(WCHAR) )))
1280 ret = SetupGetFileCompressionInfoExW( sourceW, nameW, nb_chars, &nb_chars, source_size, target_size, type );
1283 if ((nameA = UnicodeToMultiByte( nameW, CP_ACP )))
1285 if (name && len >= nb_chars) lstrcpyA( name, nameA );
1288 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1294 if (required) *required = nb_chars;
1295 HeapFree( GetProcessHeap(), 0, nameW );
1301 /***********************************************************************
1302 * SetupGetFileCompressionInfoExW (SETUPAPI.@)
1304 * Get compression type and compressed/uncompressed sizes of a given file.
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.
1319 BOOL WINAPI SetupGetFileCompressionInfoExW( PCWSTR source, PWSTR name, DWORD len, PDWORD required,
1320 PDWORD source_size, PDWORD target_size, PUINT type )
1326 TRACE("%s, %p, %d, %p, %p, %p, %p\n", debugstr_w(source), name, len, required,
1327 source_size, target_size, type);
1329 if (!source) return FALSE;
1331 source_len = lstrlenW( source ) + 1;
1332 if (required) *required = source_len;
1333 if (name && len >= source_len)
1335 lstrcpyW( name, source );
1340 comp = detect_compression_type( source );
1341 if (type) *type = comp;
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;
1354 /***********************************************************************
1355 * SetupGetFileCompressionInfoA (SETUPAPI.@)
1357 * See SetupGetFileCompressionInfoW.
1359 DWORD WINAPI SetupGetFileCompressionInfoA( PCSTR source, PSTR *name, PDWORD source_size,
1360 PDWORD target_size, PUINT type )
1363 DWORD error, required;
1366 TRACE("%s, %p, %p, %p, %p\n", debugstr_a(source), name, source_size, target_size, type);
1368 if (!source || !name || !source_size || !target_size || !type)
1369 return ERROR_INVALID_PARAMETER;
1371 ret = SetupGetFileCompressionInfoExA( source, NULL, 0, &required, NULL, NULL, NULL );
1372 if (!(actual_name = MyMalloc( required ))) return ERROR_NOT_ENOUGH_MEMORY;
1374 ret = SetupGetFileCompressionInfoExA( source, actual_name, required, &required,
1375 source_size, target_size, type );
1378 error = GetLastError();
1379 MyFree( actual_name );
1382 *name = actual_name;
1383 return ERROR_SUCCESS;
1386 /***********************************************************************
1387 * SetupGetFileCompressionInfoW (SETUPAPI.@)
1389 * Get compression type and compressed/uncompressed sizes of a given file.
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.
1399 * Success: ERROR_SUCCESS
1400 * Failure: Win32 error code.
1402 DWORD WINAPI SetupGetFileCompressionInfoW( PCWSTR source, PWSTR *name, PDWORD source_size,
1403 PDWORD target_size, PUINT type )
1406 DWORD error, required;
1409 TRACE("%s, %p, %p, %p, %p\n", debugstr_w(source), name, source_size, target_size, type);
1411 if (!source || !name || !source_size || !target_size || !type)
1412 return ERROR_INVALID_PARAMETER;
1414 ret = SetupGetFileCompressionInfoExW( source, NULL, 0, &required, NULL, NULL, NULL );
1415 if (!(actual_name = MyMalloc( required ))) return ERROR_NOT_ENOUGH_MEMORY;
1417 ret = SetupGetFileCompressionInfoExW( source, actual_name, required, &required,
1418 source_size, target_size, type );
1421 error = GetLastError();
1422 MyFree( actual_name );
1425 *name = actual_name;
1426 return ERROR_SUCCESS;
1429 static DWORD decompress_file_lz( LPCWSTR source, LPCWSTR target )
1436 if ((src = LZOpenFileW( (LPWSTR)source, &sof, OF_READ )) < 0)
1438 ERR("cannot open source file for reading\n");
1439 return ERROR_FILE_NOT_FOUND;
1441 if ((dst = LZOpenFileW( (LPWSTR)target, &dof, OF_CREATE )) < 0)
1443 ERR("cannot open target file for writing\n");
1445 return ERROR_FILE_NOT_FOUND;
1447 if ((error = LZCopy( src, dst )) >= 0) ret = ERROR_SUCCESS;
1450 WARN("failed to decompress file %d\n", error);
1451 ret = ERROR_INVALID_DATA;
1459 struct callback_context
1465 static UINT CALLBACK decompress_or_copy_callback( PVOID context, UINT notification, UINT_PTR param1, UINT_PTR param2 )
1467 struct callback_context *context_info = context;
1468 FILE_IN_CABINET_INFO_W *info = (FILE_IN_CABINET_INFO_W *)param1;
1470 switch (notification)
1472 case SPFILENOTIFY_FILEINCABINET:
1474 if (context_info->has_extracted)
1475 return FILEOP_ABORT;
1477 TRACE("Requesting extraction of cabinet file %s\n",
1478 wine_dbgstr_w(info->NameInCabinet));
1479 strcpyW( info->FullTargetName, context_info->target );
1480 context_info->has_extracted = 1;
1483 default: return NO_ERROR;
1487 static DWORD decompress_file_cab( LPCWSTR source, LPCWSTR target )
1489 struct callback_context context = {0, target};
1492 ret = SetupIterateCabinetW( source, 0, decompress_or_copy_callback, &context );
1494 if (ret) return ERROR_SUCCESS;
1495 else return GetLastError();
1498 /***********************************************************************
1499 * SetupDecompressOrCopyFileA (SETUPAPI.@)
1501 * See SetupDecompressOrCopyFileW.
1503 DWORD WINAPI SetupDecompressOrCopyFileA( PCSTR source, PCSTR target, PUINT type )
1506 WCHAR *sourceW = NULL, *targetW = NULL;
1508 if (source && !(sourceW = MultiByteToUnicode( source, CP_ACP ))) return FALSE;
1509 if (target && !(targetW = MultiByteToUnicode( target, CP_ACP )))
1512 return ERROR_NOT_ENOUGH_MEMORY;
1515 ret = SetupDecompressOrCopyFileW( sourceW, targetW, type );
1523 /***********************************************************************
1524 * SetupDecompressOrCopyFileW (SETUPAPI.@)
1526 * Copy a file and decompress it if needed.
1529 * source [I] File to copy.
1530 * target [I] Filename of the copy.
1531 * type [I] Compression type.
1534 * Success: ERROR_SUCCESS
1535 * Failure: Win32 error code.
1537 DWORD WINAPI SetupDecompressOrCopyFileW( PCWSTR source, PCWSTR target, PUINT type )
1540 DWORD ret = ERROR_INVALID_PARAMETER;
1542 TRACE("(%s, %s, %p)\n", debugstr_w(source), debugstr_w(target), type);
1544 if (!source || !target) return ERROR_INVALID_PARAMETER;
1548 comp = detect_compression_type( source );
1549 TRACE("Detected compression type %u\n", comp);
1554 TRACE("Using specified compression type %u\n", comp);
1559 case FILE_COMPRESSION_NONE:
1560 if (CopyFileW( source, target, FALSE )) ret = ERROR_SUCCESS;
1561 else ret = GetLastError();
1563 case FILE_COMPRESSION_WINLZA:
1564 ret = decompress_file_lz( source, target );
1566 case FILE_COMPRESSION_NTCAB:
1567 case FILE_COMPRESSION_MSZIP:
1568 ret = decompress_file_cab( source, target );
1571 WARN("unknown compression type %d\n", comp);
1575 TRACE("%s -> %s %d\n", debugstr_w(source), debugstr_w(target), comp);
1579 static BOOL non_interactive_mode;
1581 /***********************************************************************
1582 * SetupGetNonInteractiveMode (SETUPAPI.@)
1584 BOOL WINAPI SetupGetNonInteractiveMode( void )
1587 return non_interactive_mode;
1590 /***********************************************************************
1591 * SetupSetNonInteractiveMode (SETUPAPI.@)
1593 BOOL WINAPI SetupSetNonInteractiveMode( BOOL flag )
1595 BOOL ret = non_interactive_mode;
1597 FIXME("%d\n", flag);
1599 non_interactive_mode = flag;