1 /*
2 * Setupapi miscellaneous functions
3 *
4 * Copyright 2005 Eric Kohl
5 * Copyright 2007 Hans Leidekker
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <stdarg.h>
23
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"
34
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37
38 #include "setupapi_private.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
41
42 /* arbitrary limit not related to what native actually uses */
43 #define OEM_INDEX_LIMIT 999
44
45 /**************************************************************************
46 * MyFree [SETUPAPI.@]
47 *
48 * Frees an allocated memory block from the process heap.
49 *
50 * PARAMS
51 * lpMem [I] pointer to memory block which will be freed
52 *
53 * RETURNS
54 * None
55 */
56 VOID WINAPI MyFree(LPVOID lpMem)
57 {
58 HeapFree(GetProcessHeap(), 0, lpMem);
59 }
60
61
62 /**************************************************************************
63 * MyMalloc [SETUPAPI.@]
64 *
65 * Allocates memory block from the process heap.
66 *
67 * PARAMS
68 * dwSize [I] size of the allocated memory block
69 *
70 * RETURNS
71 * Success: pointer to allocated memory block
72 * Failure: NULL
73 */
74 LPVOID WINAPI MyMalloc(DWORD dwSize)
75 {
76 return HeapAlloc(GetProcessHeap(), 0, dwSize);
77 }
78
79
80 /**************************************************************************
81 * MyRealloc [SETUPAPI.@]
82 *
83 * Changes the size of an allocated memory block or allocates a memory
84 * block from the process heap.
85 *
86 * PARAMS
87 * lpSrc [I] pointer to memory block which will be resized
88 * dwSize [I] new size of the memory block
89 *
90 * RETURNS
91 * Success: pointer to the resized memory block
92 * Failure: NULL
93 *
94 * NOTES
95 * If lpSrc is a NULL-pointer, then MyRealloc allocates a memory
96 * block like MyMalloc.
97 */
98 LPVOID WINAPI MyRealloc(LPVOID lpSrc, DWORD dwSize)
99 {
100 if (lpSrc == NULL)
101 return HeapAlloc(GetProcessHeap(), 0, dwSize);
102
103 return HeapReAlloc(GetProcessHeap(), 0, lpSrc, dwSize);
104 }
105
106
107 /**************************************************************************
108 * DuplicateString [SETUPAPI.@]
109 *
110 * Duplicates a unicode string.
111 *
112 * PARAMS
113 * lpSrc [I] pointer to the unicode string that will be duplicated
114 *
115 * RETURNS
116 * Success: pointer to the duplicated unicode string
117 * Failure: NULL
118 *
119 * NOTES
120 * Call MyFree() to release the duplicated string.
121 */
122 LPWSTR WINAPI DuplicateString(LPCWSTR lpSrc)
123 {
124 LPWSTR lpDst;
125
126 lpDst = MyMalloc((lstrlenW(lpSrc) + 1) * sizeof(WCHAR));
127 if (lpDst == NULL)
128 return NULL;
129
130 strcpyW(lpDst, lpSrc);
131
132 return lpDst;
133 }
134
135
136 /**************************************************************************
137 * QueryRegistryValue [SETUPAPI.@]
138 *
139 * Retrieves value data from the registry and allocates memory for the
140 * value data.
141 *
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
148 *
149 * RETURNS
150 * Success: ERROR_SUCCESS
151 * Failure: Otherwise
152 *
153 * NOTES
154 * Use MyFree to release the lpData buffer.
155 */
156 LONG WINAPI QueryRegistryValue(HKEY hKey,
157 LPCWSTR lpValueName,
158 LPBYTE *lpData,
159 LPDWORD lpType,
160 LPDWORD lpcbData)
161 {
162 LONG lError;
163
164 TRACE("%p %s %p %p %p\n",
165 hKey, debugstr_w(lpValueName), lpData, lpType, lpcbData);
166
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;
172
173 /* Allocate buffer */
174 *lpData = MyMalloc(*lpcbData);
175 if (*lpData == NULL)
176 return ERROR_NOT_ENOUGH_MEMORY;
177
178 /* Query registry value */
179 lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, *lpData, lpcbData);
180 if (lError != ERROR_SUCCESS)
181 MyFree(*lpData);
182
183 return lError;
184 }
185
186
187 /**************************************************************************
188 * IsUserAdmin [SETUPAPI.@]
189 *
190 * Checks whether the current user is a member of the Administrators group.
191 *
192 * PARAMS
193 * None
194 *
195 * RETURNS
196 * Success: TRUE
197 * Failure: FALSE
198 */
199 BOOL WINAPI IsUserAdmin(VOID)
200 {
201 TRACE("\n");
202 return IsUserAnAdmin();
203 }
204
205
206 /**************************************************************************
207 * MultiByteToUnicode [SETUPAPI.@]
208 *
209 * Converts a multi-byte string to a Unicode string.
210 *
211 * PARAMS
212 * lpMultiByteStr [I] Multi-byte string to be converted
213 * uCodePage [I] Code page
214 *
215 * RETURNS
216 * Success: pointer to the converted Unicode string
217 * Failure: NULL
218 *
219 * NOTE
220 * Use MyFree to release the returned Unicode string.
221 */
222 LPWSTR WINAPI MultiByteToUnicode(LPCSTR lpMultiByteStr, UINT uCodePage)
223 {
224 LPWSTR lpUnicodeStr;
225 int nLength;
226
227 nLength = MultiByteToWideChar(uCodePage, 0, lpMultiByteStr,
228 -1, NULL, 0);
229 if (nLength == 0)
230 return NULL;
231
232 lpUnicodeStr = MyMalloc(nLength * sizeof(WCHAR));
233 if (lpUnicodeStr == NULL)
234 return NULL;
235
236 if (!MultiByteToWideChar(uCodePage, 0, lpMultiByteStr,
237 nLength, lpUnicodeStr, nLength))
238 {
239 MyFree(lpUnicodeStr);
240 return NULL;
241 }
242
243 return lpUnicodeStr;
244 }
245
246
247 /**************************************************************************
248 * UnicodeToMultiByte [SETUPAPI.@]
249 *
250 * Converts a Unicode string to a multi-byte string.
251 *
252 * PARAMS
253 * lpUnicodeStr [I] Unicode string to be converted
254 * uCodePage [I] Code page
255 *
256 * RETURNS
257 * Success: pointer to the converted multi-byte string
258 * Failure: NULL
259 *
260 * NOTE
261 * Use MyFree to release the returned multi-byte string.
262 */
263 LPSTR WINAPI UnicodeToMultiByte(LPCWSTR lpUnicodeStr, UINT uCodePage)
264 {
265 LPSTR lpMultiByteStr;
266 int nLength;
267
268 nLength = WideCharToMultiByte(uCodePage, 0, lpUnicodeStr, -1,
269 NULL, 0, NULL, NULL);
270 if (nLength == 0)
271 return NULL;
272
273 lpMultiByteStr = MyMalloc(nLength);
274 if (lpMultiByteStr == NULL)
275 return NULL;
276
277 if (!WideCharToMultiByte(uCodePage, 0, lpUnicodeStr, -1,
278 lpMultiByteStr, nLength, NULL, NULL))
279 {
280 MyFree(lpMultiByteStr);
281 return NULL;
282 }
283
284 return lpMultiByteStr;
285 }
286
287
288 /**************************************************************************
289 * DoesUserHavePrivilege [SETUPAPI.@]
290 *
291 * Check whether the current user has got a given privilege.
292 *
293 * PARAMS
294 * lpPrivilegeName [I] Name of the privilege to be checked
295 *
296 * RETURNS
297 * Success: TRUE
298 * Failure: FALSE
299 */
300 BOOL WINAPI DoesUserHavePrivilege(LPCWSTR lpPrivilegeName)
301 {
302 HANDLE hToken;
303 DWORD dwSize;
304 PTOKEN_PRIVILEGES lpPrivileges;
305 LUID PrivilegeLuid;
306 DWORD i;
307 BOOL bResult = FALSE;
308
309 TRACE("%s\n", debugstr_w(lpPrivilegeName));
310
311 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
312 return FALSE;
313
314 if (!GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &dwSize))
315 {
316 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
317 {
318 CloseHandle(hToken);
319 return FALSE;
320 }
321 }
322
323 lpPrivileges = MyMalloc(dwSize);
324 if (lpPrivileges == NULL)
325 {
326 CloseHandle(hToken);
327 return FALSE;
328 }
329
330 if (!GetTokenInformation(hToken, TokenPrivileges, lpPrivileges, dwSize, &dwSize))
331 {
332 MyFree(lpPrivileges);
333 CloseHandle(hToken);
334 return FALSE;
335 }
336
337 CloseHandle(hToken);
338
339 if (!LookupPrivilegeValueW(NULL, lpPrivilegeName, &PrivilegeLuid))
340 {
341 MyFree(lpPrivileges);
342 return FALSE;
343 }
344
345 for (i = 0; i < lpPrivileges->PrivilegeCount; i++)
346 {
347 if (lpPrivileges->Privileges[i].Luid.HighPart == PrivilegeLuid.HighPart &&
348 lpPrivileges->Privileges[i].Luid.LowPart == PrivilegeLuid.LowPart)
349 {
350 bResult = TRUE;
351 }
352 }
353
354 MyFree(lpPrivileges);
355
356 return bResult;
357 }
358
359
360 /**************************************************************************
361 * EnablePrivilege [SETUPAPI.@]
362 *
363 * Enables or disables one of the current users privileges.
364 *
365 * PARAMS
366 * lpPrivilegeName [I] Name of the privilege to be changed
367 * bEnable [I] TRUE: Enables the privilege
368 * FALSE: Disables the privilege
369 *
370 * RETURNS
371 * Success: TRUE
372 * Failure: FALSE
373 */
374 BOOL WINAPI EnablePrivilege(LPCWSTR lpPrivilegeName, BOOL bEnable)
375 {
376 TOKEN_PRIVILEGES Privileges;
377 HANDLE hToken;
378 BOOL bResult;
379
380 TRACE("%s %s\n", debugstr_w(lpPrivilegeName), bEnable ? "TRUE" : "FALSE");
381
382 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
383 return FALSE;
384
385 Privileges.PrivilegeCount = 1;
386 Privileges.Privileges[0].Attributes = (bEnable) ? SE_PRIVILEGE_ENABLED : 0;
387
388 if (!LookupPrivilegeValueW(NULL, lpPrivilegeName,
389 &Privileges.Privileges[0].Luid))
390 {
391 CloseHandle(hToken);
392 return FALSE;
393 }
394
395 bResult = AdjustTokenPrivileges(hToken, FALSE, &Privileges, 0, NULL, NULL);
396
397 CloseHandle(hToken);
398
399 return bResult;
400 }
401
402
403 /**************************************************************************
404 * DelayedMove [SETUPAPI.@]
405 *
406 * Moves a file upon the next reboot.
407 *
408 * PARAMS
409 * lpExistingFileName [I] Current file name
410 * lpNewFileName [I] New file name
411 *
412 * RETURNS
413 * Success: TRUE
414 * Failure: FALSE
415 */
416 BOOL WINAPI DelayedMove(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName)
417 {
418 return MoveFileExW(lpExistingFileName, lpNewFileName,
419 MOVEFILE_REPLACE_EXISTING | MOVEFILE_DELAY_UNTIL_REBOOT);
420 }
421
422
423 /**************************************************************************
424 * FileExists [SETUPAPI.@]
425 *
426 * Checks whether a file exists.
427 *
428 * PARAMS
429 * lpFileName [I] Name of the file to check
430 * lpNewFileName [O] Optional information about the existing file
431 *
432 * RETURNS
433 * Success: TRUE
434 * Failure: FALSE
435 */
436 BOOL WINAPI FileExists(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFileFindData)
437 {
438 WIN32_FIND_DATAW FindData;
439 HANDLE hFind;
440 UINT uErrorMode;
441 DWORD dwError;
442
443 uErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
444
445 hFind = FindFirstFileW(lpFileName, &FindData);
446 if (hFind == INVALID_HANDLE_VALUE)
447 {
448 dwError = GetLastError();
449 SetErrorMode(uErrorMode);
450 SetLastError(dwError);
451 return FALSE;
452 }
453
454 FindClose(hFind);
455
456 if (lpFileFindData)
457 *lpFileFindData = FindData;
458
459 SetErrorMode(uErrorMode);
460
461 return TRUE;
462 }
463
464
465 /**************************************************************************
466 * CaptureStringArg [SETUPAPI.@]
467 *
468 * Captures a UNICODE string.
469 *
470 * PARAMS
471 * lpSrc [I] UNICODE string to be captured
472 * lpDst [O] Pointer to the captured UNICODE string
473 *
474 * RETURNS
475 * Success: ERROR_SUCCESS
476 * Failure: ERROR_INVALID_PARAMETER
477 *
478 * NOTE
479 * Call MyFree to release the captured UNICODE string.
480 */
481 DWORD WINAPI CaptureStringArg(LPCWSTR pSrc, LPWSTR *pDst)
482 {
483 if (pDst == NULL)
484 return ERROR_INVALID_PARAMETER;
485
486 *pDst = DuplicateString(pSrc);
487
488 return ERROR_SUCCESS;
489 }
490
491
492 /**************************************************************************
493 * CaptureAndConvertAnsiArg [SETUPAPI.@]
494 *
495 * Captures an ANSI string and converts it to a UNICODE string.
496 *
497 * PARAMS
498 * lpSrc [I] ANSI string to be captured
499 * lpDst [O] Pointer to the captured UNICODE string
500 *
501 * RETURNS
502 * Success: ERROR_SUCCESS
503 * Failure: ERROR_INVALID_PARAMETER
504 *
505 * NOTE
506 * Call MyFree to release the captured UNICODE string.
507 */
508 DWORD WINAPI CaptureAndConvertAnsiArg(LPCSTR pSrc, LPWSTR *pDst)
509 {
510 if (pDst == NULL)
511 return ERROR_INVALID_PARAMETER;
512
513 *pDst = MultiByteToUnicode(pSrc, CP_ACP);
514
515 return ERROR_SUCCESS;
516 }
517
518
519 /**************************************************************************
520 * OpenAndMapFileForRead [SETUPAPI.@]
521 *
522 * Open and map a file to a buffer.
523 *
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
530 *
531 * RETURNS
532 * Success: ERROR_SUCCESS
533 * Failure: Other
534 *
535 * NOTE
536 * Call UnmapAndCloseFile to release the file.
537 */
538 DWORD WINAPI OpenAndMapFileForRead(LPCWSTR lpFileName,
539 LPDWORD lpSize,
540 LPHANDLE lpFile,
541 LPHANDLE lpMapping,
542 LPVOID *lpBuffer)
543 {
544 DWORD dwError;
545
546 TRACE("%s %p %p %p %p\n",
547 debugstr_w(lpFileName), lpSize, lpFile, lpMapping, lpBuffer);
548
549 *lpFile = CreateFileW(lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
550 OPEN_EXISTING, 0, NULL);
551 if (*lpFile == INVALID_HANDLE_VALUE)
552 return GetLastError();
553
554 *lpSize = GetFileSize(*lpFile, NULL);
555 if (*lpSize == INVALID_FILE_SIZE)
556 {
557 dwError = GetLastError();
558 CloseHandle(*lpFile);
559 return dwError;
560 }
561
562 *lpMapping = CreateFileMappingW(*lpFile, NULL, PAGE_READONLY, 0,
563 *lpSize, NULL);
564 if (*lpMapping == NULL)
565 {
566 dwError = GetLastError();
567 CloseHandle(*lpFile);
568 return dwError;
569 }
570
571 *lpBuffer = MapViewOfFile(*lpMapping, FILE_MAP_READ, 0, 0, *lpSize);
572 if (*lpBuffer == NULL)
573 {
574 dwError = GetLastError();
575 CloseHandle(*lpMapping);
576 CloseHandle(*lpFile);
577 return dwError;
578 }
579
580 return ERROR_SUCCESS;
581 }
582
583
584 /**************************************************************************
585 * UnmapAndCloseFile [SETUPAPI.@]
586 *
587 * Unmap and close a mapped file.
588 *
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
593 *
594 * RETURNS
595 * Success: TRUE
596 * Failure: FALSE
597 */
598 BOOL WINAPI UnmapAndCloseFile(HANDLE hFile, HANDLE hMapping, LPVOID lpBuffer)
599 {
600 TRACE("%p %p %p\n",
601 hFile, hMapping, lpBuffer);
602
603 if (!UnmapViewOfFile(lpBuffer))
604 return FALSE;
605
606 if (!CloseHandle(hMapping))
607 return FALSE;
608
609 if (!CloseHandle(hFile))
610 return FALSE;
611
612 return TRUE;
613 }
614
615
616 /**************************************************************************
617 * StampFileSecurity [SETUPAPI.@]
618 *
619 * Assign a new security descriptor to the given file.
620 *
621 * PARAMS
622 * lpFileName [I] Name of the file
623 * pSecurityDescriptor [I] New security descriptor
624 *
625 * RETURNS
626 * Success: ERROR_SUCCESS
627 * Failure: other
628 */
629 DWORD WINAPI StampFileSecurity(LPCWSTR lpFileName, PSECURITY_DESCRIPTOR pSecurityDescriptor)
630 {
631 TRACE("%s %p\n", debugstr_w(lpFileName), pSecurityDescriptor);
632
633 if (!SetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
634 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
635 pSecurityDescriptor))
636 return GetLastError();
637
638 return ERROR_SUCCESS;
639 }
640
641
642 /**************************************************************************
643 * TakeOwnershipOfFile [SETUPAPI.@]
644 *
645 * Takes the ownership of the given file.
646 *
647 * PARAMS
648 * lpFileName [I] Name of the file
649 *
650 * RETURNS
651 * Success: ERROR_SUCCESS
652 * Failure: other
653 */
654 DWORD WINAPI TakeOwnershipOfFile(LPCWSTR lpFileName)
655 {
656 SECURITY_DESCRIPTOR SecDesc;
657 HANDLE hToken = NULL;
658 PTOKEN_OWNER pOwner = NULL;
659 DWORD dwError;
660 DWORD dwSize;
661
662 TRACE("%s\n", debugstr_w(lpFileName));
663
664 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
665 return GetLastError();
666
667 if (!GetTokenInformation(hToken, TokenOwner, NULL, 0, &dwSize))
668 {
669 goto fail;
670 }
671
672 pOwner = MyMalloc(dwSize);
673 if (pOwner == NULL)
674 {
675 CloseHandle(hToken);
676 return ERROR_NOT_ENOUGH_MEMORY;
677 }
678
679 if (!GetTokenInformation(hToken, TokenOwner, pOwner, dwSize, &dwSize))
680 {
681 goto fail;
682 }
683
684 if (!InitializeSecurityDescriptor(&SecDesc, SECURITY_DESCRIPTOR_REVISION))
685 {
686 goto fail;
687 }
688
689 if (!SetSecurityDescriptorOwner(&SecDesc, pOwner->Owner, FALSE))
690 {
691 goto fail;
692 }
693
694 if (!SetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION, &SecDesc))
695 {
696 goto fail;
697 }
698
699 MyFree(pOwner);
700 CloseHandle(hToken);
701
702 return ERROR_SUCCESS;
703
704 fail:;
705 dwError = GetLastError();
706
707 MyFree(pOwner);
708
709 if (hToken != NULL)
710 CloseHandle(hToken);
711
712 return dwError;
713 }
714
715
716 /**************************************************************************
717 * RetreiveFileSecurity [SETUPAPI.@]
718 *
719 * Retrieve the security descriptor that is associated with the given file.
720 *
721 * PARAMS
722 * lpFileName [I] Name of the file
723 *
724 * RETURNS
725 * Success: ERROR_SUCCESS
726 * Failure: other
727 */
728 DWORD WINAPI RetreiveFileSecurity(LPCWSTR lpFileName,
729 PSECURITY_DESCRIPTOR *pSecurityDescriptor)
730 {
731 PSECURITY_DESCRIPTOR SecDesc;
732 DWORD dwSize = 0x100;
733 DWORD dwError;
734
735 SecDesc = MyMalloc(dwSize);
736 if (SecDesc == NULL)
737 return ERROR_NOT_ENOUGH_MEMORY;
738
739 if (GetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
740 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
741 SecDesc, dwSize, &dwSize))
742 {
743 *pSecurityDescriptor = SecDesc;
744 return ERROR_SUCCESS;
745 }
746
747 dwError = GetLastError();
748 if (dwError != ERROR_INSUFFICIENT_BUFFER)
749 {
750 MyFree(SecDesc);
751 return dwError;
752 }
753
754 SecDesc = MyRealloc(SecDesc, dwSize);
755 if (SecDesc == NULL)
756 return ERROR_NOT_ENOUGH_MEMORY;
757
758 if (GetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
759 GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
760 SecDesc, dwSize, &dwSize))
761 {
762 *pSecurityDescriptor = SecDesc;
763 return ERROR_SUCCESS;
764 }
765
766 dwError = GetLastError();
767 MyFree(SecDesc);
768
769 return dwError;
770 }
771
772
773 static DWORD global_flags = 0; /* FIXME: what should be in here? */
774
775 /***********************************************************************
776 * pSetupGetGlobalFlags (SETUPAPI.@)
777 */
778 DWORD WINAPI pSetupGetGlobalFlags(void)
779 {
780 FIXME( "stub\n" );
781 return global_flags;
782 }
783
784
785 /***********************************************************************
786 * pSetupSetGlobalFlags (SETUPAPI.@)
787 */
788 void WINAPI pSetupSetGlobalFlags( DWORD flags )
789 {
790 global_flags = flags;
791 }
792
793 /***********************************************************************
794 * CMP_WaitNoPendingInstallEvents (SETUPAPI.@)
795 */
796 DWORD WINAPI CMP_WaitNoPendingInstallEvents( DWORD dwTimeout )
797 {
798 static BOOL warned = FALSE;
799
800 if (!warned)
801 {
802 FIXME("%d\n", dwTimeout);
803 warned = TRUE;
804 }
805 return WAIT_OBJECT_0;
806 }
807
808 /***********************************************************************
809 * AssertFail (SETUPAPI.@)
810 *
811 * Shows an assert fail error messagebox
812 *
813 * PARAMS
814 * lpFile [I] file where assert failed
815 * uLine [I] line number in file
816 * lpMessage [I] assert message
817 *
818 */
819 void WINAPI AssertFail(LPCSTR lpFile, UINT uLine, LPCSTR lpMessage)
820 {
821 FIXME("%s %u %s\n", lpFile, uLine, lpMessage);
822 }
823
824 /***********************************************************************
825 * SetupCopyOEMInfA (SETUPAPI.@)
826 */
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 )
830 {
831 BOOL ret = FALSE;
832 LPWSTR destW = NULL, sourceW = NULL, locationW = NULL;
833 DWORD size;
834
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);
837
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;
841
842 if (!(ret = SetupCopyOEMInfW( sourceW, locationW, media_type, style, destW,
843 buffer_size, &size, NULL )))
844 {
845 if (required_size) *required_size = size;
846 goto done;
847 }
848
849 if (dest)
850 {
851 if (buffer_size >= size)
852 {
853 WideCharToMultiByte( CP_ACP, 0, destW, -1, dest, buffer_size, NULL, NULL );
854 if (component) *component = strrchr( dest, '\\' ) + 1;
855 }
856 else
857 {
858 SetLastError( ERROR_INSUFFICIENT_BUFFER );
859 goto done;
860 }
861 }
862
863 done:
864 MyFree( destW );
865 HeapFree( GetProcessHeap(), 0, sourceW );
866 HeapFree( GetProcessHeap(), 0, locationW );
867 if (ret) SetLastError(ERROR_SUCCESS);
868 return ret;
869 }
870
871 static int compare_files( HANDLE file1, HANDLE file2 )
872 {
873 char buffer1[2048];
874 char buffer2[2048];
875 DWORD size1;
876 DWORD size2;
877
878 while( ReadFile(file1, buffer1, sizeof(buffer1), &size1, NULL) &&
879 ReadFile(file2, buffer2, sizeof(buffer2), &size2, NULL) )
880 {
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;
889 }
890
891 return 0;
892 }
893
894 /***********************************************************************
895 * SetupCopyOEMInfW (SETUPAPI.@)
896 */
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 )
900 {
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;
908
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);
911
912 if (!source)
913 {
914 SetLastError(ERROR_INVALID_PARAMETER);
915 return FALSE;
916 }
917
918 /* check for a relative path */
919 if (!(*source == '\\' || (*source && source[1] == ':')))
920 {
921 SetLastError(ERROR_FILE_NOT_FOUND);
922 return FALSE;
923 }
924
925 if (!GetWindowsDirectoryW( target, sizeof(target)/sizeof(WCHAR) )) return FALSE;
926
927 strcatW( target, inf );
928 if ((p = strrchrW( source, '\\' )))
929 strcatW( target, p + 1 );
930
931 /* does the file exist already? */
932 if ((GetFileAttributesW( target ) != INVALID_FILE_ATTRIBUTES) &&
933 !(style & SP_COPY_NOOVERWRITE))
934 {
935 static const WCHAR oem[] = { 'o','e','m',0 };
936 unsigned int i;
937 LARGE_INTEGER source_file_size;
938 HANDLE source_file;
939
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;
945
946 if (!GetFileSizeEx( source_file, &source_file_size ))
947 {
948 CloseHandle( source_file );
949 return FALSE;
950 }
951
952 p = strrchrW( target, '\\' ) + 1;
953 memcpy( p, oem, sizeof(oem) );
954 p += sizeof(oem)/sizeof(oem[0]) - 1;
955
956 /* generate OEMnnn.inf ending */
957 for (i = 0; i < OEM_INDEX_LIMIT; i++)
958 {
959 static const WCHAR format[] = { '%','u','.','i','n','f',0 };
960 HANDLE dest_file;
961 LARGE_INTEGER dest_file_size;
962
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 ))
975 {
976 CloseHandle( dest_file );
977 continue;
978 }
979 CloseHandle( dest_file );
980 break;
981 }
982
983 CloseHandle( source_file );
984 if (i == OEM_INDEX_LIMIT)
985 {
986 SetLastError( ERROR_FILENAME_EXCED_RANGE );
987 return FALSE;
988 }
989 }
990
991 hinf = SetupOpenInfFileW( source, NULL, INF_STYLE_WIN4, NULL );
992 if (hinf == INVALID_HANDLE_VALUE) return FALSE;
993
994 if (SetupGetLineTextW( NULL, hinf, wszVersion, wszCatalogFile, catalog_file,
995 sizeof(catalog_file)/sizeof(catalog_file[0]), NULL ))
996 {
997 WCHAR source_cat[MAX_PATH];
998 HCATADMIN handle;
999 HCATINFO cat;
1000 GUID msguid = DRIVER_ACTION_VERIFY;
1001
1002 SetupCloseInfFile( hinf );
1003
1004 strcpyW( source_cat, source );
1005 p = strrchrW( source_cat, '\\' );
1006 if (p) p++;
1007 else p = source_cat;
1008 strcpyW( p, catalog_file );
1009
1010 TRACE("installing catalog file %s\n", debugstr_w( source_cat ));
1011
1012 if (!CryptCATAdminAcquireContext(&handle, &msguid, 0))
1013 {
1014 ERR("Could not acquire security context\n");
1015 return FALSE;
1016 }
1017
1018 if (!(cat = CryptCATAdminAddCatalog(handle, source_cat, catalog_file, 0)))
1019 {
1020 ERR("Could not add catalog\n");
1021 CryptCATAdminReleaseContext(handle, 0);
1022 return FALSE;
1023 }
1024
1025 CryptCATAdminReleaseCatalogContext(handle, cat, 0);
1026 CryptCATAdminReleaseContext(handle, 0);
1027 }
1028 else
1029 SetupCloseInfFile( hinf );
1030
1031 if (!(ret = CopyFileW( source, target, (style & SP_COPY_NOOVERWRITE) != 0 )))
1032 return ret;
1033
1034 if (style & SP_COPY_DELETESOURCE)
1035 DeleteFileW( source );
1036
1037 size = strlenW( target ) + 1;
1038 if (dest)
1039 {
1040 if (buffer_size >= size)
1041 {
1042 strcpyW( dest, target );
1043 }
1044 else
1045 {
1046 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1047 ret = FALSE;
1048 }
1049 }
1050
1051 if (component) *component = p + 1;
1052 if (required_size) *required_size = size;
1053 if (ret) SetLastError(ERROR_SUCCESS);
1054
1055 return ret;
1056 }
1057
1058 /***********************************************************************
1059 * SetupUninstallOEMInfA (SETUPAPI.@)
1060 */
1061 BOOL WINAPI SetupUninstallOEMInfA( PCSTR inf_file, DWORD flags, PVOID reserved )
1062 {
1063 BOOL ret;
1064 WCHAR *inf_fileW = NULL;
1065
1066 TRACE("%s, 0x%08x, %p\n", debugstr_a(inf_file), flags, reserved);
1067
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;
1072 }
1073
1074 /***********************************************************************
1075 * SetupUninstallOEMInfW (SETUPAPI.@)
1076 */
1077 BOOL WINAPI SetupUninstallOEMInfW( PCWSTR inf_file, DWORD flags, PVOID reserved )
1078 {
1079 static const WCHAR infW[] = {'\\','i','n','f','\\',0};
1080 WCHAR target[MAX_PATH];
1081
1082 TRACE("%s, 0x%08x, %p\n", debugstr_w(inf_file), flags, reserved);
1083
1084 if (!inf_file)
1085 {
1086 SetLastError(ERROR_INVALID_PARAMETER);
1087 return FALSE;
1088 }
1089
1090 if (!GetWindowsDirectoryW( target, sizeof(target)/sizeof(WCHAR) )) return FALSE;
1091
1092 strcatW( target, infW );
1093 strcatW( target, inf_file );
1094
1095 if (flags & SUOI_FORCEDELETE)
1096 return DeleteFileW(target);
1097
1098 FIXME("not deleting %s\n", debugstr_w(target));
1099
1100 return TRUE;
1101 }
1102
1103 /***********************************************************************
1104 * InstallCatalog (SETUPAPI.@)
1105 */
1106 DWORD WINAPI InstallCatalog( LPCSTR catalog, LPCSTR basename, LPSTR fullname )
1107 {
1108 FIXME("%s, %s, %p\n", debugstr_a(catalog), debugstr_a(basename), fullname);
1109 return 0;
1110 }
1111
1112 /***********************************************************************
1113 * pSetupInstallCatalog (SETUPAPI.@)
1114 */
1115 DWORD WINAPI pSetupInstallCatalog( LPCWSTR catalog, LPCWSTR basename, LPWSTR fullname )
1116 {
1117 HCATADMIN admin;
1118 HCATINFO cat;
1119
1120 TRACE ("%s, %s, %p\n", debugstr_w(catalog), debugstr_w(basename), fullname);
1121
1122 if (!CryptCATAdminAcquireContext(&admin,NULL,0))
1123 return GetLastError();
1124
1125 if (!(cat = CryptCATAdminAddCatalog( admin, (PWSTR)catalog, (PWSTR)basename, 0 )))
1126 {
1127 DWORD rc = GetLastError();
1128 CryptCATAdminReleaseContext(admin, 0);
1129 return rc;
1130 }
1131 CryptCATAdminReleaseCatalogContext(admin, cat, 0);
1132 CryptCATAdminReleaseContext(admin,0);
1133
1134 if (fullname)
1135 FIXME("not returning full installed catalog path\n");
1136
1137 return NO_ERROR;
1138 }
1139
1140 static UINT detect_compression_type( LPCWSTR file )