Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / setupapi / misc.c
blob08c0c794fa7cf9e1447f1dae79b24abeff8703da
1 /*
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
22 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "setupapi.h"
30 #include "lzexpand.h"
31 #include "softpub.h"
32 #include "mscat.h"
33 #include "shlobj.h"
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 /**************************************************************************
46 * MyFree [SETUPAPI.@]
48 * Frees an allocated memory block from the process heap.
50 * PARAMS
51 * lpMem [I] pointer to memory block which will be freed
53 * RETURNS
54 * None
56 VOID WINAPI MyFree(LPVOID lpMem)
58 HeapFree(GetProcessHeap(), 0, lpMem);
62 /**************************************************************************
63 * MyMalloc [SETUPAPI.@]
65 * Allocates memory block from the process heap.
67 * PARAMS
68 * dwSize [I] size of the allocated memory block
70 * RETURNS
71 * Success: pointer to allocated memory block
72 * Failure: NULL
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.
86 * PARAMS
87 * lpSrc [I] pointer to memory block which will be resized
88 * dwSize [I] new size of the memory block
90 * RETURNS
91 * Success: pointer to the resized memory block
92 * Failure: NULL
94 * NOTES
95 * If lpSrc is a NULL-pointer, then MyRealloc allocates a memory
96 * block like MyMalloc.
98 LPVOID WINAPI MyRealloc(LPVOID lpSrc, DWORD dwSize)
100 if (lpSrc == NULL)
101 return HeapAlloc(GetProcessHeap(), 0, dwSize);
103 return HeapReAlloc(GetProcessHeap(), 0, lpSrc, dwSize);
107 /**************************************************************************
108 * DuplicateString [SETUPAPI.@]
110 * Duplicates a unicode string.
112 * PARAMS
113 * lpSrc [I] pointer to the unicode string that will be duplicated
115 * RETURNS
116 * Success: pointer to the duplicated unicode string
117 * Failure: NULL
119 * NOTES
120 * Call MyFree() to release the duplicated string.
122 LPWSTR WINAPI DuplicateString(LPCWSTR lpSrc)
124 LPWSTR lpDst;
126 lpDst = MyMalloc((lstrlenW(lpSrc) + 1) * sizeof(WCHAR));
127 if (lpDst == NULL)
128 return NULL;
130 strcpyW(lpDst, lpSrc);
132 return lpDst;
136 /**************************************************************************
137 * QueryRegistryValue [SETUPAPI.@]
139 * Retrieves value data from the registry and allocates memory for the
140 * value data.
142 * PARAMS
143 * hKey [I] Handle of the key to query
144 * lpValueName [I] Name of value under hkey to query
145 * lpData [O] Destination for the values contents,
146 * lpType [O] Destination for the value type
147 * lpcbData [O] Destination for the size of data
149 * RETURNS
150 * Success: ERROR_SUCCESS
151 * Failure: Otherwise
153 * NOTES
154 * Use MyFree to release the lpData buffer.
156 LONG WINAPI QueryRegistryValue(HKEY hKey,
157 LPCWSTR lpValueName,
158 LPBYTE *lpData,
159 LPDWORD lpType,
160 LPDWORD lpcbData)
162 LONG lError;
164 TRACE("%p %s %p %p %p\n",
165 hKey, debugstr_w(lpValueName), lpData, lpType, lpcbData);
167 /* Get required buffer size */
168 *lpcbData = 0;
169 lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, NULL, lpcbData);
170 if (lError != ERROR_SUCCESS)
171 return lError;
173 /* Allocate buffer */
174 *lpData = MyMalloc(*lpcbData);
175 if (*lpData == NULL)
176 return ERROR_NOT_ENOUGH_MEMORY;
178 /* Query registry value */
179 lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, *lpData, lpcbData);
180 if (lError != ERROR_SUCCESS)
181 MyFree(*lpData);
183 return lError;
187 /**************************************************************************
188 * IsUserAdmin [SETUPAPI.@]
190 * Checks whether the current user is a member of the Administrators group.
192 * PARAMS
193 * None
195 * RETURNS
196 * Success: TRUE
197 * Failure: FALSE
199 BOOL WINAPI IsUserAdmin(VOID)
201 TRACE("\n");
202 return IsUserAnAdmin();
206 /**************************************************************************
207 * MultiByteToUnicode [SETUPAPI.@]
209 * Converts a multi-byte string to a Unicode string.
211 * PARAMS
212 * lpMultiByteStr [I] Multi-byte string to be converted
213 * uCodePage [I] Code page
215 * RETURNS
216 * Success: pointer to the converted Unicode string
217 * Failure: NULL
219 * NOTE
220 * Use MyFree to release the returned Unicode string.
222 LPWSTR WINAPI MultiByteToUnicode(LPCSTR lpMultiByteStr, UINT uCodePage)
224 LPWSTR lpUnicodeStr;
225 int nLength;
227 nLength = MultiByteToWideChar(uCodePage, 0, lpMultiByteStr,
228 -1, NULL, 0);
229 if (nLength == 0)
230 return NULL;
232 lpUnicodeStr = MyMalloc(nLength * sizeof(WCHAR));
233 if (lpUnicodeStr == NULL)
234 return NULL;
236 if (!MultiByteToWideChar(uCodePage, 0, lpMultiByteStr,
237 nLength, lpUnicodeStr, nLength))
239 MyFree(lpUnicodeStr);
240 return NULL;
243 return lpUnicodeStr;
247 /**************************************************************************
248 * UnicodeToMultiByte [SETUPAPI.@]
250 * Converts a Unicode string to a multi-byte string.
252 * PARAMS
253 * lpUnicodeStr [I] Unicode string to be converted
254 * uCodePage [I] Code page
256 * RETURNS
257 * Success: pointer to the converted multi-byte string
258 * Failure: NULL
260 * NOTE
261 * Use MyFree to release the returned multi-byte string.
263 LPSTR WINAPI UnicodeToMultiByte(LPCWSTR lpUnicodeStr, UINT uCodePage)
265 LPSTR lpMultiByteStr;
266 int nLength;
268 nLength = WideCharToMultiByte(uCodePage, 0, lpUnicodeStr, -1,
269 NULL, 0, NULL, NULL);
270 if (nLength == 0)
271 return NULL;
273 lpMultiByteStr = MyMalloc(nLength);
274 if (lpMultiByteStr == NULL)
275 return NULL;
277 if (!WideCharToMultiByte(uCodePage, 0, lpUnicodeStr, -1,
278 lpMultiByteStr, nLength, NULL, NULL))
280 MyFree(lpMultiByteStr);
281 return NULL;
284 return lpMultiByteStr;
288 /**************************************************************************
289 * DoesUserHavePrivilege [SETUPAPI.@]
291 * Check whether the current user has got a given privilege.
293 * PARAMS
294 * lpPrivilegeName [I] Name of the privilege to be checked
296 * RETURNS
297 * Success: TRUE
298 * Failure: FALSE
300 BOOL WINAPI DoesUserHavePrivilege(LPCWSTR lpPrivilegeName)
302 HANDLE hToken;
303 DWORD dwSize;
304 PTOKEN_PRIVILEGES lpPrivileges;
305 LUID PrivilegeLuid;
306 DWORD i;
307 BOOL bResult = FALSE;
309 TRACE("%s\n", debugstr_w(lpPrivilegeName));
311 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
312 return FALSE;
314 if (!GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &dwSize))
316 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
318 CloseHandle(hToken);
319 return FALSE;
323 lpPrivileges = MyMalloc(dwSize);
324 if (lpPrivileges == NULL)
326 CloseHandle(hToken);
327 return FALSE;
330 if (!GetTokenInformation(hToken, TokenPrivileges, lpPrivileges, dwSize, &dwSize))
332 MyFree(lpPrivileges);
333 CloseHandle(hToken);
334 return FALSE;
337 CloseHandle(hToken);
339 if (!LookupPrivilegeValueW(NULL, lpPrivilegeName, &PrivilegeLuid))
341 MyFree(lpPrivileges);
342 return FALSE;
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)
350 bResult = TRUE;
354 MyFree(lpPrivileges);
356 return bResult;
360 /**************************************************************************
361 * EnablePrivilege [SETUPAPI.@]
363 * Enables or disables one of the current users privileges.
365 * PARAMS
366 * lpPrivilegeName [I] Name of the privilege to be changed
367 * bEnable [I] TRUE: Enables the privilege
368 * FALSE: Disables the privilege
370 * RETURNS
371 * Success: TRUE
372 * Failure: FALSE
374 BOOL WINAPI EnablePrivilege(LPCWSTR lpPrivilegeName, BOOL bEnable)
376 TOKEN_PRIVILEGES Privileges;
377 HANDLE hToken;
378 BOOL bResult;
380 TRACE("%s %s\n", debugstr_w(lpPrivilegeName), bEnable ? "TRUE" : "FALSE");
382 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
383 return FALSE;
385 Privileges.PrivilegeCount = 1;
386 Privileges.Privileges[0].Attributes = (bEnable) ? SE_PRIVILEGE_ENABLED : 0;
388 if (!LookupPrivilegeValueW(NULL, lpPrivilegeName,
389 &Privileges.Privileges[0].Luid))
391 CloseHandle(hToken);
392 return FALSE;
395 bResult = AdjustTokenPrivileges(hToken, FALSE, &Privileges, 0, NULL, NULL);
397 CloseHandle(hToken);
399 return bResult;
403 /**************************************************************************
404 * DelayedMove [SETUPAPI.@]
406 * Moves a file upon the next reboot.
408 * PARAMS
409 * lpExistingFileName [I] Current file name
410 * lpNewFileName [I] New file name
412 * RETURNS
413 * Success: TRUE
414 * Failure: FALSE
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.
428 * PARAMS
429 * lpFileName [I] Name of the file to check
430 * lpNewFileName [O] Optional information about the existing file
432 * RETURNS
433 * Success: TRUE
434 * Failure: FALSE
436 BOOL WINAPI FileExists(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFileFindData)
438 WIN32_FIND_DATAW FindData;
439 HANDLE hFind;
440 UINT uErrorMode;
441 DWORD dwError;
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);
451 return FALSE;
454 FindClose(hFind);
456 if (lpFileFindData)
457 *lpFileFindData = FindData;
459 SetErrorMode(uErrorMode);
461 return TRUE;
465 /**************************************************************************
466 * CaptureStringArg [SETUPAPI.@]
468 * Captures a UNICODE string.
470 * PARAMS
471 * lpSrc [I] UNICODE string to be captured
472 * lpDst [O] Pointer to the captured UNICODE string
474 * RETURNS
475 * Success: ERROR_SUCCESS
476 * Failure: ERROR_INVALID_PARAMETER
478 * NOTE
479 * Call MyFree to release the captured UNICODE string.
481 DWORD WINAPI CaptureStringArg(LPCWSTR pSrc, LPWSTR *pDst)
483 if (pDst == NULL)
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.
497 * PARAMS
498 * lpSrc [I] ANSI string to be captured
499 * lpDst [O] Pointer to the captured UNICODE string
501 * RETURNS
502 * Success: ERROR_SUCCESS
503 * Failure: ERROR_INVALID_PARAMETER
505 * NOTE
506 * Call MyFree to release the captured UNICODE string.
508 DWORD WINAPI CaptureAndConvertAnsiArg(LPCSTR pSrc, LPWSTR *pDst)
510 if (pDst == NULL)
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.
524 * PARAMS
525 * lpFileName [I] Name of the file to be opened
526 * lpSize [O] Pointer to the file size
527 * lpFile [0] Pointer to the file handle
528 * lpMapping [0] Pointer to the mapping handle
529 * lpBuffer [0] Pointer to the file buffer
531 * RETURNS
532 * Success: ERROR_SUCCESS
533 * Failure: Other
535 * NOTE
536 * Call UnmapAndCloseFile to release the file.
538 DWORD WINAPI OpenAndMapFileForRead(LPCWSTR lpFileName,
539 LPDWORD lpSize,
540 LPHANDLE lpFile,
541 LPHANDLE lpMapping,
542 LPVOID *lpBuffer)
544 DWORD dwError;
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);
559 return dwError;
562 *lpMapping = CreateFileMappingW(*lpFile, NULL, PAGE_READONLY, 0,
563 *lpSize, NULL);
564 if (*lpMapping == NULL)
566 dwError = GetLastError();
567 CloseHandle(*lpFile);
568 return dwError;
571 *lpBuffer = MapViewOfFile(*lpMapping, FILE_MAP_READ, 0, 0, *lpSize);
572 if (*lpBuffer == NULL)
574 dwError = GetLastError();
575 CloseHandle(*lpMapping);
576 CloseHandle(*lpFile);
577 return dwError;
580 return ERROR_SUCCESS;
584 /**************************************************************************
585 * UnmapAndCloseFile [SETUPAPI.@]
587 * Unmap and close a mapped file.
589 * PARAMS
590 * hFile [I] Handle to the file
591 * hMapping [I] Handle to the file mapping
592 * lpBuffer [I] Pointer to the file buffer
594 * RETURNS
595 * Success: TRUE
596 * Failure: FALSE
598 BOOL WINAPI UnmapAndCloseFile(HANDLE hFile, HANDLE hMapping, LPVOID lpBuffer)
600 TRACE("%p %p %p\n",
601 hFile, hMapping, lpBuffer);
603 if (!UnmapViewOfFile(lpBuffer))
604 return FALSE;
606 if (!CloseHandle(hMapping))
607 return FALSE;
609 if (!CloseHandle(hFile))
610 return FALSE;
612 return TRUE;
616 /**************************************************************************
617 * StampFileSecurity [SETUPAPI.@]
619 * Assign a new security descriptor to the given file.
621 * PARAMS
622 * lpFileName [I] Name of the file
623 * pSecurityDescriptor [I] New security descriptor
625 * RETURNS
626 * Success: ERROR_SUCCESS
627 * Failure: other
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.
647 * PARAMS
648 * lpFileName [I] Name of the file
650 * RETURNS
651 * Success: ERROR_SUCCESS
652 * Failure: other
654 DWORD WINAPI TakeOwnershipOfFile(LPCWSTR lpFileName)
656 SECURITY_DESCRIPTOR SecDesc;
657 HANDLE hToken = NULL;
658 PTOKEN_OWNER pOwner = NULL;
659 DWORD dwError;
660 DWORD dwSize;
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))
669 goto fail;
672 pOwner = MyMalloc(dwSize);
673 if (pOwner == NULL)
675 CloseHandle(hToken);
676 return ERROR_NOT_ENOUGH_MEMORY;
679 if (!GetTokenInformation(hToken, TokenOwner, pOwner, dwSize, &dwSize))
681 goto fail;
684 if (!InitializeSecurityDescriptor(&SecDesc, SECURITY_DESCRIPTOR_REVISION))
686 goto fail;
689 if (!SetSecurityDescriptorOwner(&SecDesc, pOwner->Owner, FALSE))
691 goto fail;
694 if (!SetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION, &SecDesc))
696 goto fail;
699 MyFree(pOwner);
700 CloseHandle(hToken);
702 return ERROR_SUCCESS;
704 fail:;
705 dwError = GetLastError();
707 MyFree(pOwner);
709 if (hToken != NULL)
710 CloseHandle(hToken);
712 return dwError;
716 /**************************************************************************
717 * RetreiveFileSecurity [SETUPAPI.@]
719 * Retrieve the security descriptor that is associated with the given file.
721 * PARAMS
722 * lpFileName [I] Name of the file
724 * RETURNS
725 * Success: ERROR_SUCCESS
726 * Failure: other
728 DWORD WINAPI RetreiveFileSecurity(LPCWSTR lpFileName,
729 PSECURITY_DESCRIPTOR *pSecurityDescriptor)
731 PSECURITY_DESCRIPTOR SecDesc;
732 DWORD dwSize = 0x100;
733 DWORD dwError;
735 SecDesc = MyMalloc(dwSize);
736 if (SecDesc == NULL)
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)
750 MyFree(SecDesc);
751 return dwError;
754 SecDesc = MyRealloc(SecDesc, dwSize);
755 if (SecDesc == NULL)
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();
767 MyFree(SecDesc);
769 return dwError;
773 static DWORD global_flags = 0; /* FIXME: what should be in here? */
775 /***********************************************************************
776 * pSetupGetGlobalFlags (SETUPAPI.@)
778 DWORD WINAPI pSetupGetGlobalFlags(void)
780 FIXME( "stub\n" );
781 return global_flags;
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;
800 if (!warned)
802 FIXME("%d\n", dwTimeout);
803 warned = TRUE;
805 return WAIT_OBJECT_0;
808 /***********************************************************************
809 * AssertFail (SETUPAPI.@)
811 * Shows an assert fail error messagebox
813 * PARAMS
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 )
831 BOOL ret = FALSE;
832 LPWSTR destW = NULL, sourceW = NULL, locationW = NULL;
833 DWORD size;
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;
846 goto done;
849 if (dest)
851 if (buffer_size >= size)
853 WideCharToMultiByte( CP_ACP, 0, destW, -1, dest, buffer_size, NULL, NULL );
854 if (component) *component = strrchr( dest, '\\' ) + 1;
856 else
858 SetLastError( ERROR_INSUFFICIENT_BUFFER );
859 goto done;
863 done:
864 MyFree( destW );
865 HeapFree( GetProcessHeap(), 0, sourceW );
866 HeapFree( GetProcessHeap(), 0, locationW );
867 if (ret) SetLastError(ERROR_SUCCESS);
868 return ret;
871 static int compare_files( HANDLE file1, HANDLE file2 )
873 char buffer1[2048];
874 char buffer2[2048];
875 DWORD size1;
876 DWORD size2;
878 while( ReadFile(file1, buffer1, sizeof(buffer1), &size1, NULL) &&
879 ReadFile(file2, buffer2, sizeof(buffer2), &size2, NULL) )
881 int ret;
882 if (size1 != size2)
883 return size1 > size2 ? 1 : -1;
884 if (!size1)
885 return 0;
886 ret = memcmp( buffer1, buffer2, size1 );
887 if (ret)
888 return ret;
891 return 0;
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 )
901 BOOL ret = FALSE;
902 WCHAR target[MAX_PATH], catalog_file[MAX_PATH], *p;
903 static const WCHAR inf[] = { '\\','i','n','f','\\',0 };
904 static const WCHAR wszVersion[] = { 'V','e','r','s','i','o','n',0 };
905 static const WCHAR wszCatalogFile[] = { 'C','a','t','a','l','o','g','F','i','l','e',0 };
906 DWORD size;
907 HINF hinf;
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);
912 if (!source)
914 SetLastError(ERROR_INVALID_PARAMETER);
915 return FALSE;
918 /* check for a relative path */
919 if (!(*source == '\\' || (*source && source[1] == ':')))
921 SetLastError(ERROR_FILE_NOT_FOUND);
922 return FALSE;
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 };
936 unsigned int i;
937 LARGE_INTEGER source_file_size;
938 HANDLE source_file;
940 source_file = CreateFileW( source, FILE_READ_DATA | FILE_READ_ATTRIBUTES,
941 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
942 NULL, OPEN_EXISTING, 0, NULL );
943 if (source_file == INVALID_HANDLE_VALUE)
944 return FALSE;
946 if (!GetFileSizeEx( source_file, &source_file_size ))
948 CloseHandle( source_file );
949 return FALSE;
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 };
960 HANDLE dest_file;
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)
969 break;
970 /* now check if the same inf file has already been copied to the inf
971 * directory. if so, use that file and don't create a new one */
972 if (!GetFileSizeEx( dest_file, &dest_file_size ) ||
973 (dest_file_size.QuadPart != source_file_size.QuadPart) ||
974 compare_files( source_file, dest_file ))
976 CloseHandle( dest_file );
977 continue;
979 CloseHandle( dest_file );
980 break;
983 CloseHandle( source_file );
984 if (i == OEM_INDEX_LIMIT)
986 SetLastError( ERROR_FILENAME_EXCED_RANGE );
987 return FALSE;
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];
998 HCATADMIN handle;
999 HCATINFO cat;
1000 GUID msguid = DRIVER_ACTION_VERIFY;
1002 SetupCloseInfFile( hinf );
1004 strcpyW( source_cat, source );
1005 p = strrchrW( source_cat, '\\' );
1006 if (p) p++;
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");
1015 return FALSE;
1018 if (!(cat = CryptCATAdminAddCatalog(handle, source_cat, catalog_file, 0)))
1020 ERR("Could not add catalog\n");
1021 CryptCATAdminReleaseContext(handle, 0);
1022 return FALSE;
1025 CryptCATAdminReleaseCatalogContext(handle, cat, 0);
1026 CryptCATAdminReleaseContext(handle, 0);
1028 else
1029 SetupCloseInfFile( hinf );
1031 if (!(ret = CopyFileW( source, target, (style & SP_COPY_NOOVERWRITE) != 0 )))
1032 return ret;
1034 if (style & SP_COPY_DELETESOURCE)
1035 DeleteFileW( source );
1037 size = strlenW( target ) + 1;
1038 if (dest)
1040 if (buffer_size >= size)
1042 strcpyW( dest, target );
1044 else
1046 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1047 ret = FALSE;
1051 if (component) *component = p + 1;
1052 if (required_size) *required_size = size;
1053 if (ret) SetLastError(ERROR_SUCCESS);
1055 return ret;
1058 /***********************************************************************
1059 * SetupUninstallOEMInfA (SETUPAPI.@)
1061 BOOL WINAPI SetupUninstallOEMInfA( PCSTR inf_file, DWORD flags, PVOID reserved )
1063 BOOL ret;
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 );
1071 return ret;
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);
1084 if (!inf_file)
1086 SetLastError(ERROR_INVALID_PARAMETER);
1087 return FALSE;
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));
1100 return TRUE;
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);
1109 return 0;
1112 /***********************************************************************
1113 * pSetupInstallCatalog (SETUPAPI.@)
1115 DWORD WINAPI pSetupInstallCatalog( LPCWSTR catalog, LPCWSTR basename, LPWSTR fullname )
1117 HCATADMIN admin;
1118 HCATINFO cat;
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);
1129 return rc;
1131 CryptCATAdminReleaseCatalogContext(admin, cat, 0);
1132 CryptCATAdminReleaseContext(admin,0);
1134 if (fullname)
1135 FIXME("not returning full installed catalog path\n");
1137 return NO_ERROR;
1140 static UINT detect_compression_type( LPCWSTR file )
1142 DWORD size;
1143 HANDLE handle;
1144 UINT type = FILE_COMPRESSION_NONE;
1145 static const BYTE LZ_MAGIC[] = { 0x53, 0x5a, 0x44, 0x44, 0x88, 0xf0, 0x27, 0x33 };
1146 static const BYTE MSZIP_MAGIC[] = { 0x4b, 0x57, 0x41, 0x4a };
1147 static const BYTE NTCAB_MAGIC[] = { 0x4d, 0x53, 0x43, 0x46 };
1148 BYTE buffer[8];
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 );
1166 return type;
1169 static BOOL get_file_size( LPCWSTR file, DWORD *size )
1171 HANDLE handle;
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));
1177 return FALSE;
1179 *size = GetFileSize( handle, NULL );
1180 CloseHandle( handle );
1181 return TRUE;
1184 static BOOL get_file_sizes_none( LPCWSTR source, DWORD *source_size, DWORD *target_size )
1186 DWORD size;
1188 if (!get_file_size( source, &size )) return FALSE;
1189 if (source_size) *source_size = size;
1190 if (target_size) *target_size = size;
1191 return TRUE;
1194 static BOOL get_file_sizes_lz( LPCWSTR source, DWORD *source_size, DWORD *target_size )
1196 DWORD size;
1197 BOOL ret = TRUE;
1199 if (source_size)
1201 if (!get_file_size( source, &size )) ret = FALSE;
1202 else *source_size = size;
1204 if (target_size)
1206 INT file;
1207 OFSTRUCT of;
1209 if ((file = LZOpenFileW( (LPWSTR)source, &of, OF_READ )) < 0)
1211 ERR("cannot open source file for reading\n");
1212 return FALSE;
1214 *target_size = LZSeek( file, 0, 2 );
1215 LZClose( file );
1217 return ret;
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;
1230 return FILEOP_SKIP;
1232 default: return NO_ERROR;
1236 static BOOL get_file_sizes_cab( LPCWSTR source, DWORD *source_size, DWORD *target_size )
1238 DWORD size;
1239 BOOL ret = TRUE;
1241 if (source_size)
1243 if (!get_file_size( source, &size )) ret = FALSE;
1244 else *source_size = size;
1246 if (target_size)
1248 ret = SetupIterateCabinetW( source, 0, file_compression_info_callback, target_size );
1250 return ret;
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 )
1261 BOOL ret;
1262 WCHAR *nameW = NULL, *sourceW = NULL;
1263 DWORD nb_chars = 0;
1264 LPSTR nameA;
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;
1271 if (name)
1273 ret = SetupGetFileCompressionInfoExW( sourceW, NULL, 0, &nb_chars, NULL, NULL, NULL );
1274 if (!(nameW = HeapAlloc( GetProcessHeap(), 0, nb_chars * sizeof(WCHAR) )))
1276 MyFree( sourceW );
1277 return FALSE;
1280 ret = SetupGetFileCompressionInfoExW( sourceW, nameW, nb_chars, &nb_chars, source_size, target_size, type );
1281 if (ret)
1283 if ((nameA = UnicodeToMultiByte( nameW, CP_ACP )))
1285 if (name && len >= nb_chars) lstrcpyA( name, nameA );
1286 else
1288 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1289 ret = FALSE;
1291 MyFree( nameA );
1294 if (required) *required = nb_chars;
1295 HeapFree( GetProcessHeap(), 0, nameW );
1296 MyFree( sourceW );
1298 return ret;
1301 /***********************************************************************
1302 * SetupGetFileCompressionInfoExW (SETUPAPI.@)
1304 * Get compression type and compressed/uncompressed sizes of a given file.
1306 * PARAMS
1307 * source [I] File to examine.
1308 * name [O] Actual filename used.
1309 * len [I] Length in characters of 'name' buffer.
1310 * required [O] Number of characters written to 'name'.
1311 * source_size [O] Size of compressed file.
1312 * target_size [O] Size of uncompressed file.
1313 * type [O] Compression type.
1315 * RETURNS
1316 * Success: TRUE
1317 * Failure: FALSE
1319 BOOL WINAPI SetupGetFileCompressionInfoExW( PCWSTR source, PWSTR name, DWORD len, PDWORD required,
1320 PDWORD source_size, PDWORD target_size, PUINT type )
1322 UINT comp;
1323 BOOL ret = FALSE;
1324 DWORD source_len;
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 );
1336 ret = TRUE;
1338 else return FALSE;
1340 comp = detect_compression_type( source );
1341 if (type) *type = comp;
1343 switch (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;
1349 default: break;
1351 return ret;
1354 /***********************************************************************
1355 * SetupGetFileCompressionInfoA (SETUPAPI.@)
1357 * See SetupGetFileCompressionInfoW.
1359 DWORD WINAPI SetupGetFileCompressionInfoA( PCSTR source, PSTR *name, PDWORD source_size,
1360 PDWORD target_size, PUINT type )
1362 BOOL ret;
1363 DWORD error, required;
1364 LPSTR actual_name;
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 );
1376 if (!ret)
1378 error = GetLastError();
1379 MyFree( actual_name );
1380 return error;
1382 *name = actual_name;
1383 return ERROR_SUCCESS;
1386 /***********************************************************************
1387 * SetupGetFileCompressionInfoW (SETUPAPI.@)
1389 * Get compression type and compressed/uncompressed sizes of a given file.
1391 * PARAMS
1392 * source [I] File to examine.
1393 * name [O] Actual filename used.
1394 * source_size [O] Size of compressed file.
1395 * target_size [O] Size of uncompressed file.
1396 * type [O] Compression type.
1398 * RETURNS
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 )
1405 BOOL ret;
1406 DWORD error, required;
1407 LPWSTR actual_name;
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 );
1419 if (!ret)
1421 error = GetLastError();
1422 MyFree( actual_name );
1423 return error;
1425 *name = actual_name;
1426 return ERROR_SUCCESS;
1429 static DWORD decompress_file_lz( LPCWSTR source, LPCWSTR target )
1431 DWORD ret;
1432 LONG error;
1433 INT src, dst;
1434 OFSTRUCT sof, dof;
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");
1444 LZClose( src );
1445 return ERROR_FILE_NOT_FOUND;
1447 if ((error = LZCopy( src, dst )) >= 0) ret = ERROR_SUCCESS;
1448 else
1450 WARN("failed to decompress file %d\n", error);
1451 ret = ERROR_INVALID_DATA;
1454 LZClose( src );
1455 LZClose( dst );
1456 return ret;
1459 static UINT CALLBACK decompress_or_copy_callback( PVOID context, UINT notification, UINT_PTR param1, UINT_PTR param2 )
1461 FILE_IN_CABINET_INFO_W *info = (FILE_IN_CABINET_INFO_W *)param1;
1463 switch (notification)
1465 case SPFILENOTIFY_FILEINCABINET:
1467 LPCWSTR filename, targetname = context;
1468 WCHAR *p;
1470 if ((p = strrchrW( targetname, '\\' ))) filename = p + 1;
1471 else filename = targetname;
1473 if (!lstrcmpiW( filename, info->NameInCabinet ))
1475 strcpyW( info->FullTargetName, targetname );
1476 return FILEOP_DOIT;
1478 return FILEOP_SKIP;
1480 default: return NO_ERROR;
1484 static DWORD decompress_file_cab( LPCWSTR source, LPCWSTR target )
1486 BOOL ret;
1488 ret = SetupIterateCabinetW( source, 0, decompress_or_copy_callback, (PVOID)target );
1490 if (ret) return ERROR_SUCCESS;
1491 else return GetLastError();
1494 /***********************************************************************
1495 * SetupDecompressOrCopyFileA (SETUPAPI.@)
1497 * See SetupDecompressOrCopyFileW.
1499 DWORD WINAPI SetupDecompressOrCopyFileA( PCSTR source, PCSTR target, PUINT type )
1501 DWORD ret = FALSE;
1502 WCHAR *sourceW = NULL, *targetW = NULL;
1504 if (source && !(sourceW = MultiByteToUnicode( source, CP_ACP ))) return FALSE;
1505 if (target && !(targetW = MultiByteToUnicode( target, CP_ACP )))
1507 MyFree( sourceW );
1508 return ERROR_NOT_ENOUGH_MEMORY;
1511 ret = SetupDecompressOrCopyFileW( sourceW, targetW, type );
1513 MyFree( sourceW );
1514 MyFree( targetW );
1516 return ret;
1519 /***********************************************************************
1520 * SetupDecompressOrCopyFileW (SETUPAPI.@)
1522 * Copy a file and decompress it if needed.
1524 * PARAMS
1525 * source [I] File to copy.
1526 * target [I] Filename of the copy.
1527 * type [I] Compression type.
1529 * RETURNS
1530 * Success: ERROR_SUCCESS
1531 * Failure: Win32 error code.
1533 DWORD WINAPI SetupDecompressOrCopyFileW( PCWSTR source, PCWSTR target, PUINT type )
1535 UINT comp;
1536 DWORD ret = ERROR_INVALID_PARAMETER;
1538 if (!source || !target) return ERROR_INVALID_PARAMETER;
1540 if (!type) comp = detect_compression_type( source );
1541 else comp = *type;
1543 switch (comp)
1545 case FILE_COMPRESSION_NONE:
1546 if (CopyFileW( source, target, FALSE )) ret = ERROR_SUCCESS;
1547 else ret = GetLastError();
1548 break;
1549 case FILE_COMPRESSION_WINLZA:
1550 ret = decompress_file_lz( source, target );
1551 break;
1552 case FILE_COMPRESSION_NTCAB:
1553 case FILE_COMPRESSION_MSZIP:
1554 ret = decompress_file_cab( source, target );
1555 break;
1556 default:
1557 WARN("unknown compression type %d\n", comp);
1558 break;
1561 TRACE("%s -> %s %d\n", debugstr_w(source), debugstr_w(target), comp);
1562 return ret;