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);
41 /**************************************************************************
44 * Frees an allocated memory block from the process heap.
47 * lpMem [I] pointer to memory block which will be freed
52 VOID WINAPI MyFree(LPVOID lpMem)
54 HeapFree(GetProcessHeap(), 0, lpMem);
58 /**************************************************************************
59 * MyMalloc [SETUPAPI.@]
61 * Allocates memory block from the process heap.
64 * dwSize [I] size of the allocated memory block
67 * Success: pointer to allocated memory block
70 LPVOID WINAPI MyMalloc(DWORD dwSize)
72 return HeapAlloc(GetProcessHeap(), 0, dwSize);
76 /**************************************************************************
77 * MyRealloc [SETUPAPI.@]
79 * Changes the size of an allocated memory block or allocates a memory
80 * block from the process heap.
83 * lpSrc [I] pointer to memory block which will be resized
84 * dwSize [I] new size of the memory block
87 * Success: pointer to the resized memory block
91 * If lpSrc is a NULL-pointer, then MyRealloc allocates a memory
92 * block like MyMalloc.
94 LPVOID WINAPI MyRealloc(LPVOID lpSrc, DWORD dwSize)
97 return HeapAlloc(GetProcessHeap(), 0, dwSize);
99 return HeapReAlloc(GetProcessHeap(), 0, lpSrc, dwSize);
103 /**************************************************************************
104 * DuplicateString [SETUPAPI.@]
106 * Duplicates a unicode string.
109 * lpSrc [I] pointer to the unicode string that will be duplicated
112 * Success: pointer to the duplicated unicode string
116 * Call MyFree() to release the duplicated string.
118 LPWSTR WINAPI DuplicateString(LPCWSTR lpSrc)
122 lpDst = MyMalloc((lstrlenW(lpSrc) + 1) * sizeof(WCHAR));
126 strcpyW(lpDst, lpSrc);
132 /**************************************************************************
133 * QueryRegistryValue [SETUPAPI.@]
135 * Retrieves value data from the registry and allocates memory for the
139 * hKey [I] Handle of the key to query
140 * lpValueName [I] Name of value under hkey to query
141 * lpData [O] Destination for the values contents,
142 * lpType [O] Destination for the value type
143 * lpcbData [O] Destination for the size of data
146 * Success: ERROR_SUCCESS
150 * Use MyFree to release the lpData buffer.
152 LONG WINAPI QueryRegistryValue(HKEY hKey,
160 TRACE("%p %s %p %p %p\n",
161 hKey, debugstr_w(lpValueName), lpData, lpType, lpcbData);
163 /* Get required buffer size */
165 lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, NULL, lpcbData);
166 if (lError != ERROR_SUCCESS)
169 /* Allocate buffer */
170 *lpData = MyMalloc(*lpcbData);
172 return ERROR_NOT_ENOUGH_MEMORY;
174 /* Query registry value */
175 lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, *lpData, lpcbData);
176 if (lError != ERROR_SUCCESS)
183 /**************************************************************************
184 * IsUserAdmin [SETUPAPI.@]
186 * Checks whether the current user is a member of the Administrators group.
195 BOOL WINAPI IsUserAdmin(VOID)
197 SID_IDENTIFIER_AUTHORITY Authority = {SECURITY_NT_AUTHORITY};
200 PTOKEN_GROUPS lpGroups;
203 BOOL bResult = FALSE;
207 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
212 if (!GetTokenInformation(hToken, TokenGroups, NULL, 0, &dwSize))
214 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
221 lpGroups = MyMalloc(dwSize);
222 if (lpGroups == NULL)
228 if (!GetTokenInformation(hToken, TokenGroups, lpGroups, dwSize, &dwSize))
237 if (!AllocateAndInitializeSid(&Authority, 2, SECURITY_BUILTIN_DOMAIN_RID,
238 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0,
245 for (i = 0; i < lpGroups->GroupCount; i++)
247 if (EqualSid(lpSid, lpGroups->Groups[i].Sid))
261 /**************************************************************************
262 * MultiByteToUnicode [SETUPAPI.@]
264 * Converts a multi-byte string to a Unicode string.
267 * lpMultiByteStr [I] Multi-byte string to be converted
268 * uCodePage [I] Code page
271 * Success: pointer to the converted Unicode string
275 * Use MyFree to release the returned Unicode string.
277 LPWSTR WINAPI MultiByteToUnicode(LPCSTR lpMultiByteStr, UINT uCodePage)
282 nLength = MultiByteToWideChar(uCodePage, 0, lpMultiByteStr,
287 lpUnicodeStr = MyMalloc(nLength * sizeof(WCHAR));
288 if (lpUnicodeStr == NULL)
291 if (!MultiByteToWideChar(uCodePage, 0, lpMultiByteStr,
292 nLength, lpUnicodeStr, nLength))
294 MyFree(lpUnicodeStr);
302 /**************************************************************************
303 * UnicodeToMultiByte [SETUPAPI.@]
305 * Converts a Unicode string to a multi-byte string.
308 * lpUnicodeStr [I] Unicode string to be converted
309 * uCodePage [I] Code page
312 * Success: pointer to the converted multi-byte string
316 * Use MyFree to release the returned multi-byte string.
318 LPSTR WINAPI UnicodeToMultiByte(LPCWSTR lpUnicodeStr, UINT uCodePage)
320 LPSTR lpMultiByteStr;
323 nLength = WideCharToMultiByte(uCodePage, 0, lpUnicodeStr, -1,
324 NULL, 0, NULL, NULL);
328 lpMultiByteStr = MyMalloc(nLength);
329 if (lpMultiByteStr == NULL)
332 if (!WideCharToMultiByte(uCodePage, 0, lpUnicodeStr, -1,
333 lpMultiByteStr, nLength, NULL, NULL))
335 MyFree(lpMultiByteStr);
339 return lpMultiByteStr;
343 /**************************************************************************
344 * DoesUserHavePrivilege [SETUPAPI.@]
346 * Check whether the current user has got a given privilege.
349 * lpPrivilegeName [I] Name of the privilege to be checked
355 BOOL WINAPI DoesUserHavePrivilege(LPCWSTR lpPrivilegeName)
359 PTOKEN_PRIVILEGES lpPrivileges;
362 BOOL bResult = FALSE;
364 TRACE("%s\n", debugstr_w(lpPrivilegeName));
366 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
369 if (!GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &dwSize))
371 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
378 lpPrivileges = MyMalloc(dwSize);
379 if (lpPrivileges == NULL)
385 if (!GetTokenInformation(hToken, TokenPrivileges, lpPrivileges, dwSize, &dwSize))
387 MyFree(lpPrivileges);
394 if (!LookupPrivilegeValueW(NULL, lpPrivilegeName, &PrivilegeLuid))
396 MyFree(lpPrivileges);
400 for (i = 0; i < lpPrivileges->PrivilegeCount; i++)
402 if (lpPrivileges->Privileges[i].Luid.HighPart == PrivilegeLuid.HighPart &&
403 lpPrivileges->Privileges[i].Luid.LowPart == PrivilegeLuid.LowPart)
409 MyFree(lpPrivileges);
415 /**************************************************************************
416 * EnablePrivilege [SETUPAPI.@]
418 * Enables or disables one of the current users privileges.
421 * lpPrivilegeName [I] Name of the privilege to be changed
422 * bEnable [I] TRUE: Enables the privilege
423 * FALSE: Disables the privilege
429 BOOL WINAPI EnablePrivilege(LPCWSTR lpPrivilegeName, BOOL bEnable)
431 TOKEN_PRIVILEGES Privileges;
435 TRACE("%s %s\n", debugstr_w(lpPrivilegeName), bEnable ? "TRUE" : "FALSE");
437 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
440 Privileges.PrivilegeCount = 1;
441 Privileges.Privileges[0].Attributes = (bEnable) ? SE_PRIVILEGE_ENABLED : 0;
443 if (!LookupPrivilegeValueW(NULL, lpPrivilegeName,
444 &Privileges.Privileges[0].Luid))
450 bResult = AdjustTokenPrivileges(hToken, FALSE, &Privileges, 0, NULL, NULL);
458 /**************************************************************************
459 * DelayedMove [SETUPAPI.@]
461 * Moves a file upon the next reboot.
464 * lpExistingFileName [I] Current file name
465 * lpNewFileName [I] New file name
471 BOOL WINAPI DelayedMove(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName)
473 return MoveFileExW(lpExistingFileName, lpNewFileName,
474 MOVEFILE_REPLACE_EXISTING | MOVEFILE_DELAY_UNTIL_REBOOT);
478 /**************************************************************************
479 * FileExists [SETUPAPI.@]
481 * Checks whether a file exists.
484 * lpFileName [I] Name of the file to check
485 * lpNewFileName [O] Optional information about the existing file
491 BOOL WINAPI FileExists(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFileFindData)
493 WIN32_FIND_DATAW FindData;
498 uErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
500 hFind = FindFirstFileW(lpFileName, &FindData);
501 if (hFind == INVALID_HANDLE_VALUE)
503 dwError = GetLastError();
504 SetErrorMode(uErrorMode);
505 SetLastError(dwError);
512 memcpy(lpFileFindData, &FindData, sizeof(WIN32_FIND_DATAW));
514 SetErrorMode(uErrorMode);
520 /**************************************************************************
521 * CaptureStringArg [SETUPAPI.@]
523 * Captures a UNICODE string.
526 * lpSrc [I] UNICODE string to be captured
527 * lpDst [O] Pointer to the captured UNICODE string
530 * Success: ERROR_SUCCESS
531 * Failure: ERROR_INVALID_PARAMETER
534 * Call MyFree to release the captured UNICODE string.
536 DWORD WINAPI CaptureStringArg(LPCWSTR pSrc, LPWSTR *pDst)
539 return ERROR_INVALID_PARAMETER;
541 *pDst = DuplicateString(pSrc);
543 return ERROR_SUCCESS;
547 /**************************************************************************
548 * CaptureAndConvertAnsiArg [SETUPAPI.@]
550 * Captures an ANSI string and converts it to a UNICODE string.
553 * lpSrc [I] ANSI string to be captured
554 * lpDst [O] Pointer to the captured UNICODE string
557 * Success: ERROR_SUCCESS
558 * Failure: ERROR_INVALID_PARAMETER
561 * Call MyFree to release the captured UNICODE string.
563 DWORD WINAPI CaptureAndConvertAnsiArg(LPCSTR pSrc, LPWSTR *pDst)
566 return ERROR_INVALID_PARAMETER;
568 *pDst = MultiByteToUnicode(pSrc, CP_ACP);
570 return ERROR_SUCCESS;
574 /**************************************************************************
575 * OpenAndMapFileForRead [SETUPAPI.@]
577 * Open and map a file to a buffer.
580 * lpFileName [I] Name of the file to be opened
581 * lpSize [O] Pointer to the file size
582 * lpFile [0] Pointer to the file handle
583 * lpMapping [0] Pointer to the mapping handle
584 * lpBuffer [0] Pointer to the file buffer
587 * Success: ERROR_SUCCESS
591 * Call UnmapAndCloseFile to release the file.
593 DWORD WINAPI OpenAndMapFileForRead(LPCWSTR lpFileName,
601 TRACE("%s %p %p %p %p\n",
602 debugstr_w(lpFileName), lpSize, lpFile, lpMapping, lpBuffer);
604 *lpFile = CreateFileW(lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
605 OPEN_EXISTING, 0, NULL);
606 if (*lpFile == INVALID_HANDLE_VALUE)
607 return GetLastError();
609 *lpSize = GetFileSize(*lpFile, NULL);
610 if (*lpSize == INVALID_FILE_SIZE)
612 dwError = GetLastError();
613 CloseHandle(*lpFile);
617 *lpMapping = CreateFileMappingW(*lpFile, NULL, PAGE_READONLY, 0,
619 if (*lpMapping == NULL)
621 dwError = GetLastError();
622 CloseHandle(*lpFile);
626 *lpBuffer = MapViewOfFile(*lpMapping, FILE_MAP_READ, 0, 0, *lpSize);
627 if (*lpBuffer == NULL)
629 dwError = GetLastError();
630 CloseHandle(*lpMapping);
631 CloseHandle(*lpFile);
635 return ERROR_SUCCESS;
639 /**************************************************************************
640 * UnmapAndCloseFile [SETUPAPI.@]
642 * Unmap and close a mapped file.
645 * hFile [I] Handle to the file
646 * hMapping [I] Handle to the file mapping
647 * lpBuffer [I] Pointer to the file buffer
653 BOOL WINAPI UnmapAndCloseFile(HANDLE hFile, HANDLE hMapping, LPVOID lpBuffer)
656 hFile, hMapping, lpBuffer);
658 if (!UnmapViewOfFile(lpBuffer))
661 if (!CloseHandle(hMapping))
664 if (!CloseHandle(hFile))
671 /**************************************************************************
672 * StampFileSecurity [SETUPAPI.@]
674 * Assign a new security descriptor to the given file.
677 * lpFileName [I] Name of the file
678 * pSecurityDescriptor [I] New security descriptor
681 * Success: ERROR_SUCCESS
684 DWORD WINAPI StampFileSecurity(LPCWSTR lpFileName, PSECURITY_DESCRIPTOR pSecurityDescriptor)
686 TRACE("%s %p\n", debugstr_w(lpFileName), pSecurityDescriptor);
688 if (!SetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
689 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
690 pSecurityDescriptor))
691 return GetLastError();
693 return ERROR_SUCCESS;
697 /**************************************************************************
698 * TakeOwnershipOfFile [SETUPAPI.@]
700 * Takes the ownership of the given file.
703 * lpFileName [I] Name of the file
706 * Success: ERROR_SUCCESS
709 DWORD WINAPI TakeOwnershipOfFile(LPCWSTR lpFileName)
711 SECURITY_DESCRIPTOR SecDesc;
712 HANDLE hToken = NULL;
713 PTOKEN_OWNER pOwner = NULL;
717 TRACE("%s\n", debugstr_w(lpFileName));
719 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
720 return GetLastError();
722 if (!GetTokenInformation(hToken, TokenOwner, NULL, 0, &dwSize))
727 pOwner = (PTOKEN_OWNER)MyMalloc(dwSize);
731 return ERROR_NOT_ENOUGH_MEMORY;
734 if (!GetTokenInformation(hToken, TokenOwner, pOwner, dwSize, &dwSize))
739 if (!InitializeSecurityDescriptor(&SecDesc, SECURITY_DESCRIPTOR_REVISION))
744 if (!SetSecurityDescriptorOwner(&SecDesc, pOwner->Owner, FALSE))
749 if (!SetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION, &SecDesc))
757 return ERROR_SUCCESS;
760 dwError = GetLastError();
771 /**************************************************************************
772 * RetreiveFileSecurity [SETUPAPI.@]
774 * Retrieve the security descriptor that is associated with the given file.
777 * lpFileName [I] Name of the file
780 * Success: ERROR_SUCCESS
783 DWORD WINAPI RetreiveFileSecurity(LPCWSTR lpFileName,
784 PSECURITY_DESCRIPTOR *pSecurityDescriptor)
786 PSECURITY_DESCRIPTOR SecDesc;
787 DWORD dwSize = 0x100;
790 SecDesc = (PSECURITY_DESCRIPTOR)MyMalloc(dwSize);
792 return ERROR_NOT_ENOUGH_MEMORY;
794 if (GetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
795 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
796 SecDesc, dwSize, &dwSize))
798 *pSecurityDescriptor = SecDesc;
799 return ERROR_SUCCESS;
802 dwError = GetLastError();
803 if (dwError != ERROR_INSUFFICIENT_BUFFER)
809 SecDesc = (PSECURITY_DESCRIPTOR)MyRealloc(SecDesc, dwSize);
811 return ERROR_NOT_ENOUGH_MEMORY;
813 if (GetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
814 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
815 SecDesc, dwSize, &dwSize))
817 *pSecurityDescriptor = SecDesc;
818 return ERROR_SUCCESS;
821 dwError = GetLastError();
828 static DWORD global_flags = 0; /* FIXME: what should be in here? */
830 /***********************************************************************
831 * pSetupGetGlobalFlags (SETUPAPI.@)
833 DWORD WINAPI pSetupGetGlobalFlags(void)
840 /***********************************************************************
841 * pSetupSetGlobalFlags (SETUPAPI.@)
843 void WINAPI pSetupSetGlobalFlags( DWORD flags )
845 global_flags = flags;
848 /***********************************************************************
849 * CMP_WaitNoPendingInstallEvents (SETUPAPI.@)
851 DWORD WINAPI CMP_WaitNoPendingInstallEvents( DWORD dwTimeout )
853 FIXME("%d\n", dwTimeout);
854 return WAIT_OBJECT_0;
857 /***********************************************************************
858 * AssertFail (SETUPAPI.@)
860 * Shows an assert fail error messagebox
863 * lpFile [I] file where assert failed
864 * uLine [I] line number in file
865 * lpMessage [I] assert message
868 void WINAPI AssertFail(LPCSTR lpFile, UINT uLine, LPCSTR lpMessage)
870 FIXME("%s %u %s\n", lpFile, uLine, lpMessage);
873 /***********************************************************************
874 * SetupCopyOEMInfA (SETUPAPI.@)
876 BOOL WINAPI SetupCopyOEMInfA( PCSTR source, PCSTR location,
877 DWORD media_type, DWORD style, PSTR dest,
878 DWORD buffer_size, PDWORD required_size, PSTR *component )
881 LPWSTR destW = NULL, sourceW = NULL, locationW = NULL;
884 TRACE("%s, %s, %d, %d, %p, %d, %p, %p\n", debugstr_a(source), debugstr_a(location),
885 media_type, style, dest, buffer_size, required_size, component);
887 if (dest && !(destW = MyMalloc( buffer_size * sizeof(WCHAR) ))) return FALSE;
888 if (source && !(sourceW = strdupAtoW( source ))) goto done;
889 if (location && !(locationW = strdupAtoW( location ))) goto done;
891 if (!(ret = SetupCopyOEMInfW( sourceW, locationW, media_type, style, destW,
892 buffer_size, &size, NULL )))
894 if (required_size) *required_size = size;
900 if (buffer_size >= size)
902 WideCharToMultiByte( CP_ACP, 0, destW, -1, dest, buffer_size, NULL, NULL );
903 if (component) *component = strrchr( dest, '\\' ) + 1;
907 SetLastError( ERROR_INSUFFICIENT_BUFFER );
914 HeapFree( GetProcessHeap(), 0, sourceW );
915 HeapFree( GetProcessHeap(), 0, locationW );
916 if (ret) SetLastError(ERROR_SUCCESS);
920 /***********************************************************************
921 * SetupCopyOEMInfW (SETUPAPI.@)
923 BOOL WINAPI SetupCopyOEMInfW( PCWSTR source, PCWSTR location,
924 DWORD media_type, DWORD style, PWSTR dest,
925 DWORD buffer_size, PDWORD required_size, PWSTR *component )
928 WCHAR target[MAX_PATH], *p;
929 static const WCHAR inf_oem[] = { '\\','i','n','f','\\','O','E','M',0 };
932 TRACE("%s, %s, %d, %d, %p, %d, %p, %p\n", debugstr_w(source), debugstr_w(location),
933 media_type, style, dest, buffer_size, required_size, component);
937 SetLastError(ERROR_INVALID_PARAMETER);
941 /* check for a relative path */
942 if (!(*source == '\\' || (*source && source[1] == ':')))
944 SetLastError(ERROR_FILE_NOT_FOUND);
948 if (!GetWindowsDirectoryW( target, sizeof(target)/sizeof(WCHAR) )) return FALSE;
950 strcatW( target, inf_oem );
951 if ((p = strrchrW( source, '\\' )))
952 strcatW( target, p + 1 );
954 if (!(ret = CopyFileW( source, target, (style & SP_COPY_NOOVERWRITE) != 0 )))
957 if (style & SP_COPY_DELETESOURCE)
958 DeleteFileW( source );
960 size = strlenW( target ) + 1;
963 if (buffer_size >= size)
965 strcpyW( dest, target );
969 SetLastError( ERROR_INSUFFICIENT_BUFFER );
974 if (component) *component = p + 1;
975 if (required_size) *required_size = size;
976 if (ret) SetLastError(ERROR_SUCCESS);
981 /***********************************************************************
982 * InstallCatalog (SETUPAPI.@)
984 DWORD WINAPI InstallCatalog( LPCSTR catalog, LPCSTR basename, LPSTR fullname )
986 FIXME("%s, %s, %p\n", debugstr_a(catalog), debugstr_a(basename), fullname);
990 static UINT detect_compression_type( LPCWSTR file )
994 UINT type = FILE_COMPRESSION_NONE;
995 static const BYTE LZ_MAGIC[] = { 0x53, 0x5a, 0x44, 0x44, 0x88, 0xf0, 0x27, 0x33 };
996 static const BYTE MSZIP_MAGIC[] = { 0x4b, 0x57, 0x41, 0x4a };
997 static const BYTE NTCAB_MAGIC[] = { 0x4d, 0x53, 0x43, 0x46 };
1000 handle = CreateFileW( file, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL );
1001 if (handle == INVALID_HANDLE_VALUE)
1003 ERR("cannot open file %s\n", debugstr_w(file));
1004 return FILE_COMPRESSION_NONE;
1006 if (!ReadFile( handle, buffer, sizeof(buffer), &size, NULL ) || size != sizeof(buffer))
1008 CloseHandle( handle );
1009 return FILE_COMPRESSION_NONE;
1011 if (!memcmp( buffer, LZ_MAGIC, sizeof(LZ_MAGIC) )) type = FILE_COMPRESSION_WINLZA;
1012 else if (!memcmp( buffer, MSZIP_MAGIC, sizeof(MSZIP_MAGIC) )) type = FILE_COMPRESSION_MSZIP;
1013 else if (!memcmp( buffer, NTCAB_MAGIC, sizeof(NTCAB_MAGIC) )) type = FILE_COMPRESSION_MSZIP; /* not a typo */
1015 CloseHandle( handle );
1019 static BOOL get_file_size( LPCWSTR file, DWORD *size )
1023 handle = CreateFileW( file, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL );
1024 if (handle == INVALID_HANDLE_VALUE)
1026 ERR("cannot open file %s\n", debugstr_w(file));
1029 *size = GetFileSize( handle, NULL );
1030 CloseHandle( handle );
1034 static BOOL get_file_sizes_none( LPCWSTR source, DWORD *source_size, DWORD *target_size )
1038 if (!get_file_size( source, &size )) return FALSE;
1039 if (source_size) *source_size = size;
1040 if (target_size) *target_size = size;
1044 static BOOL get_file_sizes_lz( LPCWSTR source, DWORD *source_size, DWORD *target_size )
1051 if (!get_file_size( source, &size )) ret = FALSE;
1052 else *source_size = size;
1059 if ((file = LZOpenFileW( (LPWSTR)source, &of, OF_READ )) < 0)
1061 ERR("cannot open source file for reading\n");
1064 *target_size = LZSeek( file, 0, 2 );
1070 static UINT CALLBACK file_compression_info_callback( PVOID context, UINT notification, UINT_PTR param1, UINT_PTR param2 )
1072 DWORD *size = context;
1073 FILE_IN_CABINET_INFO_W *info = (FILE_IN_CABINET_INFO_W *)param1;
1075 switch (notification)
1077 case SPFILENOTIFY_FILEINCABINET:
1079 *size = info->FileSize;
1082 default: return NO_ERROR;
1086 static BOOL get_file_sizes_cab( LPCWSTR source, DWORD *source_size, DWORD *target_size )
1093 if (!get_file_size( source, &size )) ret = FALSE;
1094 else *source_size = size;
1098 ret = SetupIterateCabinetW( source, 0, file_compression_info_callback, target_size );
1103 /***********************************************************************
1104 * SetupGetFileCompressionInfoExA (SETUPAPI.@)
1106 * See SetupGetFileCompressionInfoExW.
1108 BOOL WINAPI SetupGetFileCompressionInfoExA( PCSTR source, PSTR name, DWORD len, PDWORD required,
1109 PDWORD source_size, PDWORD target_size, PUINT type )
1112 WCHAR *nameW = NULL, *sourceW = NULL;
1116 TRACE("%s, %p, %d, %p, %p, %p, %p\n", debugstr_a(source), name, len, required,
1117 source_size, target_size, type);
1119 if (!source || !(sourceW = MultiByteToUnicode( source, CP_ACP ))) return FALSE;
1123 ret = SetupGetFileCompressionInfoExW( sourceW, NULL, 0, &nb_chars, NULL, NULL, NULL );
1124 if (!(nameW = HeapAlloc( GetProcessHeap(), 0, nb_chars * sizeof(WCHAR) )))
1130 ret = SetupGetFileCompressionInfoExW( sourceW, nameW, nb_chars, &nb_chars, source_size, target_size, type );
1133 if ((nameA = UnicodeToMultiByte( nameW, CP_ACP )))
1135 if (name && len >= nb_chars) lstrcpyA( name, nameA );
1138 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1144 if (required) *required = nb_chars;
1145 HeapFree( GetProcessHeap(), 0, nameW );
1151 /***********************************************************************
1152 * SetupGetFileCompressionInfoExW (SETUPAPI.@)
1154 * Get compression type and compressed/uncompressed sizes of a given file.
1157 * source [I] File to examine.
1158 * name [O] Actual filename used.
1159 * len [I] Length in characters of 'name' buffer.
1160 * required [O] Number of characters written to 'name'.
1161 * source_size [O] Size of compressed file.
1162 * target_size [O] Size of uncompressed file.
1163 * type [O] Compression type.
1169 BOOL WINAPI SetupGetFileCompressionInfoExW( PCWSTR source, PWSTR name, DWORD len, PDWORD required,
1170 PDWORD source_size, PDWORD target_size, PUINT type )
1176 TRACE("%s, %p, %d, %p, %p, %p, %p\n", debugstr_w(source), name, len, required,
1177 source_size, target_size, type);
1179 if (!source) return FALSE;
1181 source_len = lstrlenW( source ) + 1;
1182 if (required) *required = source_len;
1183 if (name && len >= source_len)
1185 lstrcpyW( name, source );
1190 comp = detect_compression_type( source );
1191 if (type) *type = comp;
1195 case FILE_COMPRESSION_MSZIP:
1196 case FILE_COMPRESSION_NTCAB: ret = get_file_sizes_cab( source, source_size, target_size ); break;
1197 case FILE_COMPRESSION_NONE: ret = get_file_sizes_none( source, source_size, target_size ); break;
1198 case FILE_COMPRESSION_WINLZA: ret = get_file_sizes_lz( source, source_size, target_size ); break;
1204 /***********************************************************************
1205 * SetupGetFileCompressionInfoA (SETUPAPI.@)
1207 * See SetupGetFileCompressionInfoW.
1209 DWORD WINAPI SetupGetFileCompressionInfoA( PCSTR source, PSTR *name, PDWORD source_size,
1210 PDWORD target_size, PUINT type )
1213 DWORD error, required;
1216 TRACE("%s, %p, %p, %p, %p\n", debugstr_a(source), name, source_size, target_size, type);
1218 if (!source || !name || !source_size || !target_size || !type)
1219 return ERROR_INVALID_PARAMETER;
1221 ret = SetupGetFileCompressionInfoExA( source, NULL, 0, &required, NULL, NULL, NULL );
1222 if (!(actual_name = MyMalloc( required ))) return ERROR_NOT_ENOUGH_MEMORY;
1224 ret = SetupGetFileCompressionInfoExA( source, actual_name, required, &required,
1225 source_size, target_size, type );
1228 error = GetLastError();
1229 MyFree( actual_name );
1232 *name = actual_name;
1233 return ERROR_SUCCESS;
1236 /***********************************************************************
1237 * SetupGetFileCompressionInfoW (SETUPAPI.@)
1239 * Get compression type and compressed/uncompressed sizes of a given file.
1242 * source [I] File to examine.
1243 * name [O] Actual filename used.
1244 * source_size [O] Size of compressed file.
1245 * target_size [O] Size of uncompressed file.
1246 * type [O] Compression type.
1249 * Success: ERROR_SUCCESS
1250 * Failure: Win32 error code.
1252 DWORD WINAPI SetupGetFileCompressionInfoW( PCWSTR source, PWSTR *name, PDWORD source_size,
1253 PDWORD target_size, PUINT type )
1256 DWORD error, required;
1259 TRACE("%s, %p, %p, %p, %p\n", debugstr_w(source), name, source_size, target_size, type);
1261 if (!source || !name || !source_size || !target_size || !type)
1262 return ERROR_INVALID_PARAMETER;
1264 ret = SetupGetFileCompressionInfoExW( source, NULL, 0, &required, NULL, NULL, NULL );
1265 if (!(actual_name = MyMalloc( required ))) return ERROR_NOT_ENOUGH_MEMORY;
1267 ret = SetupGetFileCompressionInfoExW( source, actual_name, required, &required,
1268 source_size, target_size, type );
1271 error = GetLastError();
1272 MyFree( actual_name );
1275 *name = actual_name;
1276 return ERROR_SUCCESS;
1279 static DWORD decompress_file_lz( LPCWSTR source, LPCWSTR target )
1286 if ((src = LZOpenFileW( (LPWSTR)source, &sof, OF_READ )) < 0)
1288 ERR("cannot open source file for reading\n");
1289 return ERROR_FILE_NOT_FOUND;
1291 if ((dst = LZOpenFileW( (LPWSTR)target, &dof, OF_CREATE )) < 0)
1293 ERR("cannot open target file for writing\n");
1295 return ERROR_FILE_NOT_FOUND;
1297 if ((error = LZCopy( src, dst )) >= 0) ret = ERROR_SUCCESS;
1300 WARN("failed to decompress file %d\n", error);
1301 ret = ERROR_INVALID_DATA;
1309 static UINT CALLBACK decompress_or_copy_callback( PVOID context, UINT notification, UINT_PTR param1, UINT_PTR param2 )
1311 FILE_IN_CABINET_INFO_W *info = (FILE_IN_CABINET_INFO_W *)param1;
1313 switch (notification)
1315 case SPFILENOTIFY_FILEINCABINET:
1317 LPCWSTR filename, targetname = context;
1320 if ((p = strrchrW( targetname, '\\' ))) filename = p + 1;
1321 else filename = targetname;
1323 if (!lstrcmpiW( filename, info->NameInCabinet ))
1325 strcpyW( info->FullTargetName, targetname );
1330 default: return NO_ERROR;
1334 static DWORD decompress_file_cab( LPCWSTR source, LPCWSTR target )
1338 ret = SetupIterateCabinetW( source, 0, decompress_or_copy_callback, (PVOID)target );
1340 if (ret) return ERROR_SUCCESS;
1341 else return GetLastError();
1344 /***********************************************************************
1345 * SetupDecompressOrCopyFileA (SETUPAPI.@)
1347 * See SetupDecompressOrCopyFileW.
1349 DWORD WINAPI SetupDecompressOrCopyFileA( PCSTR source, PCSTR target, PUINT type )
1352 WCHAR *sourceW = NULL, *targetW = NULL;
1354 if (source && !(sourceW = MultiByteToUnicode( source, CP_ACP ))) return FALSE;
1355 if (target && !(targetW = MultiByteToUnicode( target, CP_ACP )))
1358 return ERROR_NOT_ENOUGH_MEMORY;
1361 ret = SetupDecompressOrCopyFileW( sourceW, targetW, type );
1369 /***********************************************************************
1370 * SetupDecompressOrCopyFileW (SETUPAPI.@)
1372 * Copy a file and decompress it if needed.
1375 * source [I] File to copy.
1376 * target [I] Filename of the copy.
1377 * type [I] Compression type.
1380 * Success: ERROR_SUCCESS
1381 * Failure: Win32 error code.
1383 DWORD WINAPI SetupDecompressOrCopyFileW( PCWSTR source, PCWSTR target, PUINT type )
1386 DWORD ret = ERROR_INVALID_PARAMETER;
1388 if (!source || !target) return ERROR_INVALID_PARAMETER;
1390 if (!type) comp = detect_compression_type( source );
1395 case FILE_COMPRESSION_NONE:
1396 if (CopyFileW( source, target, FALSE )) ret = ERROR_SUCCESS;
1397 else ret = GetLastError();
1399 case FILE_COMPRESSION_WINLZA:
1400 ret = decompress_file_lz( source, target );
1402 case FILE_COMPRESSION_NTCAB:
1403 case FILE_COMPRESSION_MSZIP:
1404 ret = decompress_file_cab( source, target );
1407 WARN("unknown compression type %d\n", comp);
1411 TRACE("%s -> %s %d\n", debugstr_w(source), debugstr_w(target), comp);