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)
55 HeapFree(GetProcessHeap(), 0, lpMem);
59 /**************************************************************************
60 * MyMalloc [SETUPAPI.@]
62 * Allocates memory block from the process heap.
65 * dwSize [I] size of the allocated memory block
68 * Success: pointer to allocated memory block
71 LPVOID WINAPI MyMalloc(DWORD dwSize)
73 TRACE("%u\n", 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)
98 TRACE("%p %u\n", lpSrc, 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 TRACE("%s\n", debugstr_w(lpSrc));
128 lpDst = MyMalloc((lstrlenW(lpSrc) + 1) * sizeof(WCHAR));
132 strcpyW(lpDst, lpSrc);
138 /**************************************************************************
139 * QueryRegistryValue [SETUPAPI.@]
141 * Retrieves value data from the registry and allocates memory for the
145 * hKey [I] Handle of the key to query
146 * lpValueName [I] Name of value under hkey to query
147 * lpData [O] Destination for the values contents,
148 * lpType [O] Destination for the value type
149 * lpcbData [O] Destination for the size of data
152 * Success: ERROR_SUCCESS
156 * Use MyFree to release the lpData buffer.
158 LONG WINAPI QueryRegistryValue(HKEY hKey,
166 TRACE("%p %s %p %p %p\n",
167 hKey, debugstr_w(lpValueName), lpData, lpType, lpcbData);
169 /* Get required buffer size */
171 lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, NULL, lpcbData);
172 if (lError != ERROR_SUCCESS)
175 /* Allocate buffer */
176 *lpData = MyMalloc(*lpcbData);
178 return ERROR_NOT_ENOUGH_MEMORY;
180 /* Query registry value */
181 lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, *lpData, lpcbData);
182 if (lError != ERROR_SUCCESS)
189 /**************************************************************************
190 * IsUserAdmin [SETUPAPI.@]
192 * Checks whether the current user is a member of the Administrators group.
201 BOOL WINAPI IsUserAdmin(VOID)
203 SID_IDENTIFIER_AUTHORITY Authority = {SECURITY_NT_AUTHORITY};
206 PTOKEN_GROUPS lpGroups;
209 BOOL bResult = FALSE;
213 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
218 if (!GetTokenInformation(hToken, TokenGroups, NULL, 0, &dwSize))
220 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
227 lpGroups = MyMalloc(dwSize);
228 if (lpGroups == NULL)
234 if (!GetTokenInformation(hToken, TokenGroups, lpGroups, dwSize, &dwSize))
243 if (!AllocateAndInitializeSid(&Authority, 2, SECURITY_BUILTIN_DOMAIN_RID,
244 DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0,
251 for (i = 0; i < lpGroups->GroupCount; i++)
253 if (EqualSid(lpSid, lpGroups->Groups[i].Sid))
267 /**************************************************************************
268 * MultiByteToUnicode [SETUPAPI.@]
270 * Converts a multi-byte string to a Unicode string.
273 * lpMultiByteStr [I] Multi-byte string to be converted
274 * uCodePage [I] Code page
277 * Success: pointer to the converted Unicode string
281 * Use MyFree to release the returned Unicode string.
283 LPWSTR WINAPI MultiByteToUnicode(LPCSTR lpMultiByteStr, UINT uCodePage)
288 TRACE("%s %d\n", debugstr_a(lpMultiByteStr), uCodePage);
290 nLength = MultiByteToWideChar(uCodePage, 0, lpMultiByteStr,
295 lpUnicodeStr = MyMalloc(nLength * sizeof(WCHAR));
296 if (lpUnicodeStr == NULL)
299 if (!MultiByteToWideChar(uCodePage, 0, lpMultiByteStr,
300 nLength, lpUnicodeStr, nLength))
302 MyFree(lpUnicodeStr);
310 /**************************************************************************
311 * UnicodeToMultiByte [SETUPAPI.@]
313 * Converts a Unicode string to a multi-byte string.
316 * lpUnicodeStr [I] Unicode string to be converted
317 * uCodePage [I] Code page
320 * Success: pointer to the converted multi-byte string
324 * Use MyFree to release the returned multi-byte string.
326 LPSTR WINAPI UnicodeToMultiByte(LPCWSTR lpUnicodeStr, UINT uCodePage)
328 LPSTR lpMultiByteStr;
331 TRACE("%s %d\n", debugstr_w(lpUnicodeStr), uCodePage);
333 nLength = WideCharToMultiByte(uCodePage, 0, lpUnicodeStr, -1,
334 NULL, 0, NULL, NULL);
338 lpMultiByteStr = MyMalloc(nLength);
339 if (lpMultiByteStr == NULL)
342 if (!WideCharToMultiByte(uCodePage, 0, lpUnicodeStr, -1,
343 lpMultiByteStr, nLength, NULL, NULL))
345 MyFree(lpMultiByteStr);
349 return lpMultiByteStr;
353 /**************************************************************************
354 * DoesUserHavePrivilege [SETUPAPI.@]
356 * Check whether the current user has got a given privilege.
359 * lpPrivilegeName [I] Name of the privilege to be checked
365 BOOL WINAPI DoesUserHavePrivilege(LPCWSTR lpPrivilegeName)
369 PTOKEN_PRIVILEGES lpPrivileges;
372 BOOL bResult = FALSE;
374 TRACE("%s\n", debugstr_w(lpPrivilegeName));
376 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
379 if (!GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &dwSize))
381 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
388 lpPrivileges = MyMalloc(dwSize);
389 if (lpPrivileges == NULL)
395 if (!GetTokenInformation(hToken, TokenPrivileges, lpPrivileges, dwSize, &dwSize))
397 MyFree(lpPrivileges);
404 if (!LookupPrivilegeValueW(NULL, lpPrivilegeName, &PrivilegeLuid))
406 MyFree(lpPrivileges);
410 for (i = 0; i < lpPrivileges->PrivilegeCount; i++)
412 if (lpPrivileges->Privileges[i].Luid.HighPart == PrivilegeLuid.HighPart &&
413 lpPrivileges->Privileges[i].Luid.LowPart == PrivilegeLuid.LowPart)
419 MyFree(lpPrivileges);
425 /**************************************************************************
426 * EnablePrivilege [SETUPAPI.@]
428 * Enables or disables one of the current users privileges.
431 * lpPrivilegeName [I] Name of the privilege to be changed
432 * bEnable [I] TRUE: Enables the privilege
433 * FALSE: Disables the privilege
439 BOOL WINAPI EnablePrivilege(LPCWSTR lpPrivilegeName, BOOL bEnable)
441 TOKEN_PRIVILEGES Privileges;
445 TRACE("%s %s\n", debugstr_w(lpPrivilegeName), bEnable ? "TRUE" : "FALSE");
447 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
450 Privileges.PrivilegeCount = 1;
451 Privileges.Privileges[0].Attributes = (bEnable) ? SE_PRIVILEGE_ENABLED : 0;
453 if (!LookupPrivilegeValueW(NULL, lpPrivilegeName,
454 &Privileges.Privileges[0].Luid))
460 bResult = AdjustTokenPrivileges(hToken, FALSE, &Privileges, 0, NULL, NULL);
468 /**************************************************************************
469 * DelayedMove [SETUPAPI.@]
471 * Moves a file upon the next reboot.
474 * lpExistingFileName [I] Current file name
475 * lpNewFileName [I] New file name
481 BOOL WINAPI DelayedMove(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName)
483 return MoveFileExW(lpExistingFileName, lpNewFileName,
484 MOVEFILE_REPLACE_EXISTING | MOVEFILE_DELAY_UNTIL_REBOOT);
488 /**************************************************************************
489 * FileExists [SETUPAPI.@]
491 * Checks whether a file exists.
494 * lpFileName [I] Name of the file to check
495 * lpNewFileName [O] Optional information about the existing file
501 BOOL WINAPI FileExists(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFileFindData)
503 WIN32_FIND_DATAW FindData;
508 uErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
510 hFind = FindFirstFileW(lpFileName, &FindData);
511 if (hFind == INVALID_HANDLE_VALUE)
513 dwError = GetLastError();
514 SetErrorMode(uErrorMode);
515 SetLastError(dwError);
522 memcpy(lpFileFindData, &FindData, sizeof(WIN32_FIND_DATAW));
524 SetErrorMode(uErrorMode);
530 /**************************************************************************
531 * CaptureStringArg [SETUPAPI.@]
533 * Captures a UNICODE string.
536 * lpSrc [I] UNICODE string to be captured
537 * lpDst [O] Pointer to the captured UNICODE string
540 * Success: ERROR_SUCCESS
541 * Failure: ERROR_INVALID_PARAMETER
544 * Call MyFree to release the captured UNICODE string.
546 DWORD WINAPI CaptureStringArg(LPCWSTR pSrc, LPWSTR *pDst)
549 return ERROR_INVALID_PARAMETER;
551 *pDst = DuplicateString(pSrc);
553 return ERROR_SUCCESS;
557 /**************************************************************************
558 * CaptureAndConvertAnsiArg [SETUPAPI.@]
560 * Captures an ANSI string and converts it to a UNICODE string.
563 * lpSrc [I] ANSI string to be captured
564 * lpDst [O] Pointer to the captured UNICODE string
567 * Success: ERROR_SUCCESS
568 * Failure: ERROR_INVALID_PARAMETER
571 * Call MyFree to release the captured UNICODE string.
573 DWORD WINAPI CaptureAndConvertAnsiArg(LPCSTR pSrc, LPWSTR *pDst)
576 return ERROR_INVALID_PARAMETER;
578 *pDst = MultiByteToUnicode(pSrc, CP_ACP);
580 return ERROR_SUCCESS;
584 /**************************************************************************
585 * OpenAndMapFileForRead [SETUPAPI.@]
587 * Open and map a file to a buffer.
590 * lpFileName [I] Name of the file to be opened
591 * lpSize [O] Pointer to the file size
592 * lpFile [0] Pointer to the file handle
593 * lpMapping [0] Pointer to the mapping handle
594 * lpBuffer [0] Pointer to the file buffer
597 * Success: ERROR_SUCCESS
601 * Call UnmapAndCloseFile to release the file.
603 DWORD WINAPI OpenAndMapFileForRead(LPCWSTR lpFileName,
611 TRACE("%s %p %p %p %p\n",
612 debugstr_w(lpFileName), lpSize, lpFile, lpMapping, lpBuffer);
614 *lpFile = CreateFileW(lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
615 OPEN_EXISTING, 0, NULL);
616 if (*lpFile == INVALID_HANDLE_VALUE)
617 return GetLastError();
619 *lpSize = GetFileSize(*lpFile, NULL);
620 if (*lpSize == INVALID_FILE_SIZE)
622 dwError = GetLastError();
623 CloseHandle(*lpFile);
627 *lpMapping = CreateFileMappingW(*lpFile, NULL, PAGE_READONLY, 0,
629 if (*lpMapping == NULL)
631 dwError = GetLastError();
632 CloseHandle(*lpFile);
636 *lpBuffer = MapViewOfFile(*lpMapping, FILE_MAP_READ, 0, 0, *lpSize);
637 if (*lpBuffer == NULL)
639 dwError = GetLastError();
640 CloseHandle(*lpMapping);
641 CloseHandle(*lpFile);
645 return ERROR_SUCCESS;
649 /**************************************************************************
650 * UnmapAndCloseFile [SETUPAPI.@]
652 * Unmap and close a mapped file.
655 * hFile [I] Handle to the file
656 * hMapping [I] Handle to the file mapping
657 * lpBuffer [I] Pointer to the file buffer
663 BOOL WINAPI UnmapAndCloseFile(HANDLE hFile, HANDLE hMapping, LPVOID lpBuffer)
666 hFile, hMapping, lpBuffer);
668 if (!UnmapViewOfFile(lpBuffer))
671 if (!CloseHandle(hMapping))
674 if (!CloseHandle(hFile))
681 /**************************************************************************
682 * StampFileSecurity [SETUPAPI.@]
684 * Assign a new security descriptor to the given file.
687 * lpFileName [I] Name of the file
688 * pSecurityDescriptor [I] New security descriptor
691 * Success: ERROR_SUCCESS
694 DWORD WINAPI StampFileSecurity(LPCWSTR lpFileName, PSECURITY_DESCRIPTOR pSecurityDescriptor)
696 TRACE("%s %p\n", debugstr_w(lpFileName), pSecurityDescriptor);
698 if (!SetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
699 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
700 pSecurityDescriptor))
701 return GetLastError();
703 return ERROR_SUCCESS;
707 /**************************************************************************
708 * TakeOwnershipOfFile [SETUPAPI.@]
710 * Takes the ownership of the given file.
713 * lpFileName [I] Name of the file
716 * Success: ERROR_SUCCESS
719 DWORD WINAPI TakeOwnershipOfFile(LPCWSTR lpFileName)
721 SECURITY_DESCRIPTOR SecDesc;
722 HANDLE hToken = NULL;
723 PTOKEN_OWNER pOwner = NULL;
727 TRACE("%s\n", debugstr_w(lpFileName));
729 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
730 return GetLastError();
732 if (!GetTokenInformation(hToken, TokenOwner, NULL, 0, &dwSize))
737 pOwner = (PTOKEN_OWNER)MyMalloc(dwSize);
741 return ERROR_NOT_ENOUGH_MEMORY;
744 if (!GetTokenInformation(hToken, TokenOwner, pOwner, dwSize, &dwSize))
749 if (!InitializeSecurityDescriptor(&SecDesc, SECURITY_DESCRIPTOR_REVISION))
754 if (!SetSecurityDescriptorOwner(&SecDesc, pOwner->Owner, FALSE))
759 if (!SetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION, &SecDesc))
767 return ERROR_SUCCESS;
770 dwError = GetLastError();
781 /**************************************************************************
782 * RetreiveFileSecurity [SETUPAPI.@]
784 * Retrieve the security descriptor that is associated with the given file.
787 * lpFileName [I] Name of the file
790 * Success: ERROR_SUCCESS
793 DWORD WINAPI RetreiveFileSecurity(LPCWSTR lpFileName,
794 PSECURITY_DESCRIPTOR *pSecurityDescriptor)
796 PSECURITY_DESCRIPTOR SecDesc;
797 DWORD dwSize = 0x100;
800 SecDesc = (PSECURITY_DESCRIPTOR)MyMalloc(dwSize);
802 return ERROR_NOT_ENOUGH_MEMORY;
804 if (GetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
805 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
806 SecDesc, dwSize, &dwSize))
808 *pSecurityDescriptor = SecDesc;
809 return ERROR_SUCCESS;
812 dwError = GetLastError();
813 if (dwError != ERROR_INSUFFICIENT_BUFFER)
819 SecDesc = (PSECURITY_DESCRIPTOR)MyRealloc(SecDesc, dwSize);
821 return ERROR_NOT_ENOUGH_MEMORY;
823 if (GetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
824 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
825 SecDesc, dwSize, &dwSize))
827 *pSecurityDescriptor = SecDesc;
828 return ERROR_SUCCESS;
831 dwError = GetLastError();
838 static DWORD global_flags = 0; /* FIXME: what should be in here? */
840 /***********************************************************************
841 * pSetupGetGlobalFlags (SETUPAPI.@)
843 DWORD WINAPI pSetupGetGlobalFlags(void)
850 /***********************************************************************
851 * pSetupSetGlobalFlags (SETUPAPI.@)
853 void WINAPI pSetupSetGlobalFlags( DWORD flags )
855 global_flags = flags;
858 /***********************************************************************
859 * CMP_WaitNoPendingInstallEvents (SETUPAPI.@)
861 DWORD WINAPI CMP_WaitNoPendingInstallEvents( DWORD dwTimeout )
863 FIXME("%d\n", dwTimeout);
864 return WAIT_OBJECT_0;
867 /***********************************************************************
868 * AssertFail (SETUPAPI.@)
870 * Shows an assert fail error messagebox
873 * lpFile [I] file where assert failed
874 * uLine [I] line number in file
875 * lpMessage [I] assert message
878 void WINAPI AssertFail(LPCSTR lpFile, UINT uLine, LPCSTR lpMessage)
880 FIXME("%s %u %s\n", lpFile, uLine, lpMessage);
883 /***********************************************************************
884 * SetupCopyOEMInfA (SETUPAPI.@)
886 BOOL WINAPI SetupCopyOEMInfA( PCSTR source, PCSTR location,
887 DWORD media_type, DWORD style, PSTR dest,
888 DWORD buffer_size, PDWORD required_size, PSTR *component )
891 LPWSTR destW = NULL, sourceW = NULL, locationW = NULL;
894 TRACE("%s, %s, %d, %d, %p, %d, %p, %p\n", debugstr_a(source), debugstr_a(location),
895 media_type, style, dest, buffer_size, required_size, component);
897 if (dest && !(destW = MyMalloc( buffer_size * sizeof(WCHAR) ))) return FALSE;
898 if (source && !(sourceW = strdupAtoW( source ))) goto done;
899 if (location && !(locationW = strdupAtoW( location ))) goto done;
901 if (!(ret = SetupCopyOEMInfW( sourceW, locationW, media_type, style, destW,
902 buffer_size, &size, NULL )))
904 if (required_size) *required_size = size;
910 if (buffer_size >= size)
912 WideCharToMultiByte( CP_ACP, 0, destW, -1, dest, buffer_size, NULL, NULL );
913 if (component) *component = strrchr( dest, '\\' ) + 1;
917 SetLastError( ERROR_INSUFFICIENT_BUFFER );
924 HeapFree( GetProcessHeap(), 0, sourceW );
925 HeapFree( GetProcessHeap(), 0, locationW );
926 if (ret) SetLastError(ERROR_SUCCESS);
930 /***********************************************************************
931 * SetupCopyOEMInfW (SETUPAPI.@)
933 BOOL WINAPI SetupCopyOEMInfW( PCWSTR source, PCWSTR location,
934 DWORD media_type, DWORD style, PWSTR dest,
935 DWORD buffer_size, PDWORD required_size, PWSTR *component )
938 WCHAR target[MAX_PATH], *p;
939 static const WCHAR inf_oem[] = { '\\','i','n','f','\\','O','E','M',0 };
942 TRACE("%s, %s, %d, %d, %p, %d, %p, %p\n", debugstr_w(source), debugstr_w(location),
943 media_type, style, dest, buffer_size, required_size, component);
947 SetLastError(ERROR_INVALID_PARAMETER);
951 /* check for a relative path */
952 if (!(*source == '\\' || (*source && source[1] == ':')))
954 SetLastError(ERROR_FILE_NOT_FOUND);
958 if (!GetWindowsDirectoryW( target, sizeof(target)/sizeof(WCHAR) )) return FALSE;
960 strcatW( target, inf_oem );
961 if ((p = strrchrW( source, '\\' )))
962 strcatW( target, p + 1 );
964 if (!(ret = CopyFileW( source, target, (style & SP_COPY_NOOVERWRITE) != 0 )))
967 if (style & SP_COPY_DELETESOURCE)
968 DeleteFileW( source );
970 size = strlenW( target ) + 1;
973 if (buffer_size >= size)
975 strcpyW( dest, target );
979 SetLastError( ERROR_INSUFFICIENT_BUFFER );
984 if (component) *component = p + 1;
985 if (required_size) *required_size = size;
986 if (ret) SetLastError(ERROR_SUCCESS);
991 /***********************************************************************
992 * InstallCatalog (SETUPAPI.@)
994 DWORD WINAPI InstallCatalog( LPCSTR catalog, LPCSTR basename, LPSTR fullname )
996 FIXME("%s, %s, %p\n", debugstr_a(catalog), debugstr_a(basename), fullname);
1000 static UINT detect_compression_type( LPCWSTR file )
1004 UINT type = FILE_COMPRESSION_NONE;
1005 static const BYTE LZ_MAGIC[] = { 0x53, 0x5a, 0x44, 0x44, 0x88, 0xf0, 0x27, 0x33 };
1006 static const BYTE MSZIP_MAGIC[] = { 0x4b, 0x57, 0x41, 0x4a };
1007 static const BYTE NTCAB_MAGIC[] = { 0x4d, 0x53, 0x43, 0x46 };
1010 handle = CreateFileW( file, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL );
1011 if (handle == INVALID_HANDLE_VALUE)
1013 ERR("cannot open file %s\n", debugstr_w(file));
1014 return FILE_COMPRESSION_NONE;
1016 if (!ReadFile( handle, buffer, sizeof(buffer), &size, NULL ) || size != sizeof(buffer))
1018 CloseHandle( handle );
1019 return FILE_COMPRESSION_NONE;
1021 if (!memcmp( buffer, LZ_MAGIC, sizeof(LZ_MAGIC) )) type = FILE_COMPRESSION_WINLZA;
1022 else if (!memcmp( buffer, MSZIP_MAGIC, sizeof(MSZIP_MAGIC) )) type = FILE_COMPRESSION_MSZIP;
1023 else if (!memcmp( buffer, NTCAB_MAGIC, sizeof(NTCAB_MAGIC) )) type = FILE_COMPRESSION_MSZIP; /* not a typo */
1025 CloseHandle( handle );
1029 static BOOL get_file_size( LPCWSTR file, DWORD *size )
1033 handle = CreateFileW( file, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL );
1034 if (handle == INVALID_HANDLE_VALUE)
1036 ERR("cannot open file %s\n", debugstr_w(file));
1039 *size = GetFileSize( handle, NULL );
1040 CloseHandle( handle );
1044 static BOOL get_file_sizes_none( LPCWSTR source, DWORD *source_size, DWORD *target_size )
1048 if (!get_file_size( source, &size )) return FALSE;
1049 if (source_size) *source_size = size;
1050 if (target_size) *target_size = size;
1054 static BOOL get_file_sizes_lz( LPCWSTR source, DWORD *source_size, DWORD *target_size )
1061 if (!get_file_size( source, &size )) ret = FALSE;
1062 else *source_size = size;
1069 if ((file = LZOpenFileW( (LPWSTR)source, &of, OF_READ )) < 0)
1071 ERR("cannot open source file for reading\n");
1074 *target_size = LZSeek( file, 0, 2 );
1080 static UINT CALLBACK file_compression_info_callback( PVOID context, UINT notification, UINT_PTR param1, UINT_PTR param2 )
1082 DWORD *size = context;
1083 FILE_IN_CABINET_INFO_W *info = (FILE_IN_CABINET_INFO_W *)param1;
1085 switch (notification)
1087 case SPFILENOTIFY_FILEINCABINET:
1089 *size = info->FileSize;
1092 default: return NO_ERROR;
1096 static BOOL get_file_sizes_cab( LPCWSTR source, DWORD *source_size, DWORD *target_size )
1103 if (!get_file_size( source, &size )) ret = FALSE;
1104 else *source_size = size;
1108 ret = SetupIterateCabinetW( source, 0, file_compression_info_callback, target_size );
1113 /***********************************************************************
1114 * SetupGetFileCompressionInfoExA (SETUPAPI.@)
1116 * See SetupGetFileCompressionInfoExW.
1118 BOOL WINAPI SetupGetFileCompressionInfoExA( PCSTR source, PSTR name, DWORD len, PDWORD required,
1119 PDWORD source_size, PDWORD target_size, PUINT type )
1122 WCHAR *nameW = NULL, *sourceW = NULL;
1126 TRACE("%s, %p, %d, %p, %p, %p, %p\n", debugstr_a(source), name, len, required,
1127 source_size, target_size, type);
1129 if (!source || !(sourceW = MultiByteToUnicode( source, CP_ACP ))) return FALSE;
1133 ret = SetupGetFileCompressionInfoExW( sourceW, NULL, 0, &nb_chars, NULL, NULL, NULL );
1134 if (!(nameW = HeapAlloc( GetProcessHeap(), 0, nb_chars * sizeof(WCHAR) )))
1140 ret = SetupGetFileCompressionInfoExW( sourceW, nameW, nb_chars, &nb_chars, source_size, target_size, type );
1143 if ((nameA = UnicodeToMultiByte( nameW, CP_ACP )))
1145 if (name && len >= nb_chars) lstrcpyA( name, nameA );
1148 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1154 if (required) *required = nb_chars;
1155 HeapFree( GetProcessHeap(), 0, nameW );
1161 /***********************************************************************
1162 * SetupGetFileCompressionInfoExW (SETUPAPI.@)
1164 * Get compression type and compressed/uncompressed sizes of a given file.
1167 * source [I] File to examine.
1168 * name [O] Actual filename used.
1169 * len [I] Length in characters of 'name' buffer.
1170 * required [O] Number of characters written to 'name'.
1171 * source_size [O] Size of compressed file.
1172 * target_size [O] Size of uncompressed file.
1173 * type [O] Compression type.
1179 BOOL WINAPI SetupGetFileCompressionInfoExW( PCWSTR source, PWSTR name, DWORD len, PDWORD required,
1180 PDWORD source_size, PDWORD target_size, PUINT type )
1186 TRACE("%s, %p, %d, %p, %p, %p, %p\n", debugstr_w(source), name, len, required,
1187 source_size, target_size, type);
1189 if (!source) return FALSE;
1191 source_len = lstrlenW( source ) + 1;
1192 if (required) *required = source_len;
1193 if (name && len >= source_len)
1195 lstrcpyW( name, source );
1200 comp = detect_compression_type( source );
1201 if (type) *type = comp;
1205 case FILE_COMPRESSION_MSZIP:
1206 case FILE_COMPRESSION_NTCAB: ret = get_file_sizes_cab( source, source_size, target_size ); break;
1207 case FILE_COMPRESSION_NONE: ret = get_file_sizes_none( source, source_size, target_size ); break;
1208 case FILE_COMPRESSION_WINLZA: ret = get_file_sizes_lz( source, source_size, target_size ); break;