forked from wine-mirror/wine
/
misc.c
1753 lines (1481 loc) · 46.7 KB
/
misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Setupapi miscellaneous functions
*
* Copyright 2005 Eric Kohl
* Copyright 2007 Hans Leidekker
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "winreg.h"
#include "setupapi.h"
#include "lzexpand.h"
#include "softpub.h"
#include "mscat.h"
#include "shlobj.h"
#include "wine/unicode.h"
#include "wine/debug.h"
#include "setupapi_private.h"
WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
/* arbitrary limit not related to what native actually uses */
#define OEM_INDEX_LIMIT 999
/* Handles and critical sections for the SetupLog API */
static HANDLE setupact = INVALID_HANDLE_VALUE;
static HANDLE setuperr = INVALID_HANDLE_VALUE;
static CRITICAL_SECTION setupapi_cs;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
0, 0, &setupapi_cs,
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
0, 0, { (DWORD_PTR)(__FILE__ ": setupapi_cs") }
};
static CRITICAL_SECTION setupapi_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
/**************************************************************************
* MyFree [SETUPAPI.@]
*
* Frees an allocated memory block from the process heap.
*
* PARAMS
* lpMem [I] pointer to memory block which will be freed
*
* RETURNS
* None
*/
VOID WINAPI MyFree(LPVOID lpMem)
{
HeapFree(GetProcessHeap(), 0, lpMem);
}
/**************************************************************************
* MyMalloc [SETUPAPI.@]
*
* Allocates memory block from the process heap.
*
* PARAMS
* dwSize [I] size of the allocated memory block
*
* RETURNS
* Success: pointer to allocated memory block
* Failure: NULL
*/
LPVOID WINAPI MyMalloc(DWORD dwSize)
{
return HeapAlloc(GetProcessHeap(), 0, dwSize);
}
/**************************************************************************
* MyRealloc [SETUPAPI.@]
*
* Changes the size of an allocated memory block or allocates a memory
* block from the process heap.
*
* PARAMS
* lpSrc [I] pointer to memory block which will be resized
* dwSize [I] new size of the memory block
*
* RETURNS
* Success: pointer to the resized memory block
* Failure: NULL
*
* NOTES
* If lpSrc is a NULL-pointer, then MyRealloc allocates a memory
* block like MyMalloc.
*/
LPVOID WINAPI MyRealloc(LPVOID lpSrc, DWORD dwSize)
{
if (lpSrc == NULL)
return HeapAlloc(GetProcessHeap(), 0, dwSize);
return HeapReAlloc(GetProcessHeap(), 0, lpSrc, dwSize);
}
/**************************************************************************
* DuplicateString [SETUPAPI.@]
*
* Duplicates a unicode string.
*
* PARAMS
* lpSrc [I] pointer to the unicode string that will be duplicated
*
* RETURNS
* Success: pointer to the duplicated unicode string
* Failure: NULL
*
* NOTES
* Call MyFree() to release the duplicated string.
*/
LPWSTR WINAPI DuplicateString(LPCWSTR lpSrc)
{
LPWSTR lpDst;
lpDst = MyMalloc((lstrlenW(lpSrc) + 1) * sizeof(WCHAR));
if (lpDst == NULL)
return NULL;
strcpyW(lpDst, lpSrc);
return lpDst;
}
/**************************************************************************
* QueryRegistryValue [SETUPAPI.@]
*
* Retrieves value data from the registry and allocates memory for the
* value data.
*
* PARAMS
* hKey [I] Handle of the key to query
* lpValueName [I] Name of value under hkey to query
* lpData [O] Destination for the values contents,
* lpType [O] Destination for the value type
* lpcbData [O] Destination for the size of data
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: Otherwise
*
* NOTES
* Use MyFree to release the lpData buffer.
*/
LONG WINAPI QueryRegistryValue(HKEY hKey,
LPCWSTR lpValueName,
LPBYTE *lpData,
LPDWORD lpType,
LPDWORD lpcbData)
{
LONG lError;
TRACE("%p %s %p %p %p\n",
hKey, debugstr_w(lpValueName), lpData, lpType, lpcbData);
/* Get required buffer size */
*lpcbData = 0;
lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, NULL, lpcbData);
if (lError != ERROR_SUCCESS)
return lError;
/* Allocate buffer */
*lpData = MyMalloc(*lpcbData);
if (*lpData == NULL)
return ERROR_NOT_ENOUGH_MEMORY;
/* Query registry value */
lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, *lpData, lpcbData);
if (lError != ERROR_SUCCESS)
MyFree(*lpData);
return lError;
}
/**************************************************************************
* IsUserAdmin [SETUPAPI.@]
*
* Checks whether the current user is a member of the Administrators group.
*
* PARAMS
* None
*
* RETURNS
* Success: TRUE
* Failure: FALSE
*/
BOOL WINAPI IsUserAdmin(VOID)
{
TRACE("\n");
return IsUserAnAdmin();
}
/**************************************************************************
* MultiByteToUnicode [SETUPAPI.@]
*
* Converts a multi-byte string to a Unicode string.
*
* PARAMS
* lpMultiByteStr [I] Multi-byte string to be converted
* uCodePage [I] Code page
*
* RETURNS
* Success: pointer to the converted Unicode string
* Failure: NULL
*
* NOTE
* Use MyFree to release the returned Unicode string.
*/
LPWSTR WINAPI MultiByteToUnicode(LPCSTR lpMultiByteStr, UINT uCodePage)
{
LPWSTR lpUnicodeStr;
int nLength;
nLength = MultiByteToWideChar(uCodePage, 0, lpMultiByteStr,
-1, NULL, 0);
if (nLength == 0)
return NULL;
lpUnicodeStr = MyMalloc(nLength * sizeof(WCHAR));
if (lpUnicodeStr == NULL)
return NULL;
if (!MultiByteToWideChar(uCodePage, 0, lpMultiByteStr,
nLength, lpUnicodeStr, nLength))
{
MyFree(lpUnicodeStr);
return NULL;
}
return lpUnicodeStr;
}
/**************************************************************************
* UnicodeToMultiByte [SETUPAPI.@]
*
* Converts a Unicode string to a multi-byte string.
*
* PARAMS
* lpUnicodeStr [I] Unicode string to be converted
* uCodePage [I] Code page
*
* RETURNS
* Success: pointer to the converted multi-byte string
* Failure: NULL
*
* NOTE
* Use MyFree to release the returned multi-byte string.
*/
LPSTR WINAPI UnicodeToMultiByte(LPCWSTR lpUnicodeStr, UINT uCodePage)
{
LPSTR lpMultiByteStr;
int nLength;
nLength = WideCharToMultiByte(uCodePage, 0, lpUnicodeStr, -1,
NULL, 0, NULL, NULL);
if (nLength == 0)
return NULL;
lpMultiByteStr = MyMalloc(nLength);
if (lpMultiByteStr == NULL)
return NULL;
if (!WideCharToMultiByte(uCodePage, 0, lpUnicodeStr, -1,
lpMultiByteStr, nLength, NULL, NULL))
{
MyFree(lpMultiByteStr);
return NULL;
}
return lpMultiByteStr;
}
/**************************************************************************
* DoesUserHavePrivilege [SETUPAPI.@]
*
* Check whether the current user has got a given privilege.
*
* PARAMS
* lpPrivilegeName [I] Name of the privilege to be checked
*
* RETURNS
* Success: TRUE
* Failure: FALSE
*/
BOOL WINAPI DoesUserHavePrivilege(LPCWSTR lpPrivilegeName)
{
HANDLE hToken;
DWORD dwSize;
PTOKEN_PRIVILEGES lpPrivileges;
LUID PrivilegeLuid;
DWORD i;
BOOL bResult = FALSE;
TRACE("%s\n", debugstr_w(lpPrivilegeName));
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
return FALSE;
if (!GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &dwSize))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
CloseHandle(hToken);
return FALSE;
}
}
lpPrivileges = MyMalloc(dwSize);
if (lpPrivileges == NULL)
{
CloseHandle(hToken);
return FALSE;
}
if (!GetTokenInformation(hToken, TokenPrivileges, lpPrivileges, dwSize, &dwSize))
{
MyFree(lpPrivileges);
CloseHandle(hToken);
return FALSE;
}
CloseHandle(hToken);
if (!LookupPrivilegeValueW(NULL, lpPrivilegeName, &PrivilegeLuid))
{
MyFree(lpPrivileges);
return FALSE;
}
for (i = 0; i < lpPrivileges->PrivilegeCount; i++)
{
if (lpPrivileges->Privileges[i].Luid.HighPart == PrivilegeLuid.HighPart &&
lpPrivileges->Privileges[i].Luid.LowPart == PrivilegeLuid.LowPart)
{
bResult = TRUE;
}
}
MyFree(lpPrivileges);
return bResult;
}
/**************************************************************************
* EnablePrivilege [SETUPAPI.@]
*
* Enables or disables one of the current users privileges.
*
* PARAMS
* lpPrivilegeName [I] Name of the privilege to be changed
* bEnable [I] TRUE: Enables the privilege
* FALSE: Disables the privilege
*
* RETURNS
* Success: TRUE
* Failure: FALSE
*/
BOOL WINAPI EnablePrivilege(LPCWSTR lpPrivilegeName, BOOL bEnable)
{
TOKEN_PRIVILEGES Privileges;
HANDLE hToken;
BOOL bResult;
TRACE("%s %s\n", debugstr_w(lpPrivilegeName), bEnable ? "TRUE" : "FALSE");
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
return FALSE;
Privileges.PrivilegeCount = 1;
Privileges.Privileges[0].Attributes = (bEnable) ? SE_PRIVILEGE_ENABLED : 0;
if (!LookupPrivilegeValueW(NULL, lpPrivilegeName,
&Privileges.Privileges[0].Luid))
{
CloseHandle(hToken);
return FALSE;
}
bResult = AdjustTokenPrivileges(hToken, FALSE, &Privileges, 0, NULL, NULL);
CloseHandle(hToken);
return bResult;
}
/**************************************************************************
* DelayedMove [SETUPAPI.@]
*
* Moves a file upon the next reboot.
*
* PARAMS
* lpExistingFileName [I] Current file name
* lpNewFileName [I] New file name
*
* RETURNS
* Success: TRUE
* Failure: FALSE
*/
BOOL WINAPI DelayedMove(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName)
{
return MoveFileExW(lpExistingFileName, lpNewFileName,
MOVEFILE_REPLACE_EXISTING | MOVEFILE_DELAY_UNTIL_REBOOT);
}
/**************************************************************************
* FileExists [SETUPAPI.@]
*
* Checks whether a file exists.
*
* PARAMS
* lpFileName [I] Name of the file to check
* lpNewFileName [O] Optional information about the existing file
*
* RETURNS
* Success: TRUE
* Failure: FALSE
*/
BOOL WINAPI FileExists(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFileFindData)
{
WIN32_FIND_DATAW FindData;
HANDLE hFind;
UINT uErrorMode;
DWORD dwError;
uErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
hFind = FindFirstFileW(lpFileName, &FindData);
if (hFind == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
SetErrorMode(uErrorMode);
SetLastError(dwError);
return FALSE;
}
FindClose(hFind);
if (lpFileFindData)
*lpFileFindData = FindData;
SetErrorMode(uErrorMode);
return TRUE;
}
/**************************************************************************
* CaptureStringArg [SETUPAPI.@]
*
* Captures a UNICODE string.
*
* PARAMS
* lpSrc [I] UNICODE string to be captured
* lpDst [O] Pointer to the captured UNICODE string
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: ERROR_INVALID_PARAMETER
*
* NOTE
* Call MyFree to release the captured UNICODE string.
*/
DWORD WINAPI CaptureStringArg(LPCWSTR pSrc, LPWSTR *pDst)
{
if (pDst == NULL)
return ERROR_INVALID_PARAMETER;
*pDst = DuplicateString(pSrc);
return ERROR_SUCCESS;
}
/**************************************************************************
* CaptureAndConvertAnsiArg [SETUPAPI.@]
*
* Captures an ANSI string and converts it to a UNICODE string.
*
* PARAMS
* lpSrc [I] ANSI string to be captured
* lpDst [O] Pointer to the captured UNICODE string
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: ERROR_INVALID_PARAMETER
*
* NOTE
* Call MyFree to release the captured UNICODE string.
*/
DWORD WINAPI CaptureAndConvertAnsiArg(LPCSTR pSrc, LPWSTR *pDst)
{
if (pDst == NULL)
return ERROR_INVALID_PARAMETER;
*pDst = MultiByteToUnicode(pSrc, CP_ACP);
return ERROR_SUCCESS;
}
/**************************************************************************
* OpenAndMapFileForRead [SETUPAPI.@]
*
* Open and map a file to a buffer.
*
* PARAMS
* lpFileName [I] Name of the file to be opened
* lpSize [O] Pointer to the file size
* lpFile [0] Pointer to the file handle
* lpMapping [0] Pointer to the mapping handle
* lpBuffer [0] Pointer to the file buffer
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: Other
*
* NOTE
* Call UnmapAndCloseFile to release the file.
*/
DWORD WINAPI OpenAndMapFileForRead(LPCWSTR lpFileName,
LPDWORD lpSize,
LPHANDLE lpFile,
LPHANDLE lpMapping,
LPVOID *lpBuffer)
{
DWORD dwError;
TRACE("%s %p %p %p %p\n",
debugstr_w(lpFileName), lpSize, lpFile, lpMapping, lpBuffer);
*lpFile = CreateFileW(lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL);
if (*lpFile == INVALID_HANDLE_VALUE)
return GetLastError();
*lpSize = GetFileSize(*lpFile, NULL);
if (*lpSize == INVALID_FILE_SIZE)
{
dwError = GetLastError();
CloseHandle(*lpFile);
return dwError;
}
*lpMapping = CreateFileMappingW(*lpFile, NULL, PAGE_READONLY, 0,
*lpSize, NULL);
if (*lpMapping == NULL)
{
dwError = GetLastError();
CloseHandle(*lpFile);
return dwError;
}
*lpBuffer = MapViewOfFile(*lpMapping, FILE_MAP_READ, 0, 0, *lpSize);
if (*lpBuffer == NULL)
{
dwError = GetLastError();
CloseHandle(*lpMapping);
CloseHandle(*lpFile);
return dwError;
}
return ERROR_SUCCESS;
}
/**************************************************************************
* UnmapAndCloseFile [SETUPAPI.@]
*
* Unmap and close a mapped file.
*
* PARAMS
* hFile [I] Handle to the file
* hMapping [I] Handle to the file mapping
* lpBuffer [I] Pointer to the file buffer
*
* RETURNS
* Success: TRUE
* Failure: FALSE
*/
BOOL WINAPI UnmapAndCloseFile(HANDLE hFile, HANDLE hMapping, LPVOID lpBuffer)
{
TRACE("%p %p %p\n",
hFile, hMapping, lpBuffer);
if (!UnmapViewOfFile(lpBuffer))
return FALSE;
if (!CloseHandle(hMapping))
return FALSE;
if (!CloseHandle(hFile))
return FALSE;
return TRUE;
}
/**************************************************************************
* StampFileSecurity [SETUPAPI.@]
*
* Assign a new security descriptor to the given file.
*
* PARAMS
* lpFileName [I] Name of the file
* pSecurityDescriptor [I] New security descriptor
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: other
*/
DWORD WINAPI StampFileSecurity(LPCWSTR lpFileName, PSECURITY_DESCRIPTOR pSecurityDescriptor)
{
TRACE("%s %p\n", debugstr_w(lpFileName), pSecurityDescriptor);
if (!SetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
pSecurityDescriptor))
return GetLastError();
return ERROR_SUCCESS;
}
/**************************************************************************
* TakeOwnershipOfFile [SETUPAPI.@]
*
* Takes the ownership of the given file.
*
* PARAMS
* lpFileName [I] Name of the file
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: other
*/
DWORD WINAPI TakeOwnershipOfFile(LPCWSTR lpFileName)
{
SECURITY_DESCRIPTOR SecDesc;
HANDLE hToken = NULL;
PTOKEN_OWNER pOwner = NULL;
DWORD dwError;
DWORD dwSize;
TRACE("%s\n", debugstr_w(lpFileName));
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
return GetLastError();
if (!GetTokenInformation(hToken, TokenOwner, NULL, 0, &dwSize))
{
goto fail;
}
pOwner = MyMalloc(dwSize);
if (pOwner == NULL)
{
CloseHandle(hToken);
return ERROR_NOT_ENOUGH_MEMORY;
}
if (!GetTokenInformation(hToken, TokenOwner, pOwner, dwSize, &dwSize))
{
goto fail;
}
if (!InitializeSecurityDescriptor(&SecDesc, SECURITY_DESCRIPTOR_REVISION))
{
goto fail;
}
if (!SetSecurityDescriptorOwner(&SecDesc, pOwner->Owner, FALSE))
{
goto fail;
}
if (!SetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION, &SecDesc))
{
goto fail;
}
MyFree(pOwner);
CloseHandle(hToken);
return ERROR_SUCCESS;
fail:;
dwError = GetLastError();
MyFree(pOwner);
if (hToken != NULL)
CloseHandle(hToken);
return dwError;
}
/**************************************************************************
* RetreiveFileSecurity [SETUPAPI.@]
*
* Retrieve the security descriptor that is associated with the given file.
*
* PARAMS
* lpFileName [I] Name of the file
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: other
*/
DWORD WINAPI RetreiveFileSecurity(LPCWSTR lpFileName,
PSECURITY_DESCRIPTOR *pSecurityDescriptor)
{
PSECURITY_DESCRIPTOR SecDesc;
DWORD dwSize = 0x100;
DWORD dwError;
SecDesc = MyMalloc(dwSize);
if (SecDesc == NULL)
return ERROR_NOT_ENOUGH_MEMORY;
if (GetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
SecDesc, dwSize, &dwSize))
{
*pSecurityDescriptor = SecDesc;
return ERROR_SUCCESS;
}
dwError = GetLastError();
if (dwError != ERROR_INSUFFICIENT_BUFFER)
{
MyFree(SecDesc);
return dwError;
}
SecDesc = MyRealloc(SecDesc, dwSize);
if (SecDesc == NULL)
return ERROR_NOT_ENOUGH_MEMORY;
if (GetFileSecurityW(lpFileName, OWNER_SECURITY_INFORMATION |
GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
SecDesc, dwSize, &dwSize))
{
*pSecurityDescriptor = SecDesc;
return ERROR_SUCCESS;
}
dwError = GetLastError();
MyFree(SecDesc);
return dwError;
}
static DWORD global_flags = 0; /* FIXME: what should be in here? */
/***********************************************************************
* pSetupGetGlobalFlags (SETUPAPI.@)
*/
DWORD WINAPI pSetupGetGlobalFlags(void)
{
FIXME( "stub\n" );
return global_flags;
}
/***********************************************************************
* pSetupSetGlobalFlags (SETUPAPI.@)
*/
void WINAPI pSetupSetGlobalFlags( DWORD flags )
{
global_flags = flags;
}
/***********************************************************************
* CMP_WaitNoPendingInstallEvents (SETUPAPI.@)
*/
DWORD WINAPI CMP_WaitNoPendingInstallEvents( DWORD dwTimeout )
{
static BOOL warned = FALSE;
if (!warned)
{
FIXME("%d\n", dwTimeout);
warned = TRUE;
}
return WAIT_OBJECT_0;
}
/***********************************************************************
* AssertFail (SETUPAPI.@)
*
* Shows an assert fail error messagebox
*
* PARAMS
* lpFile [I] file where assert failed
* uLine [I] line number in file
* lpMessage [I] assert message
*
*/
void WINAPI AssertFail(LPCSTR lpFile, UINT uLine, LPCSTR lpMessage)
{
FIXME("%s %u %s\n", lpFile, uLine, lpMessage);
}
/***********************************************************************
* SetupCopyOEMInfA (SETUPAPI.@)
*/
BOOL WINAPI SetupCopyOEMInfA( PCSTR source, PCSTR location,
DWORD media_type, DWORD style, PSTR dest,
DWORD buffer_size, PDWORD required_size, PSTR *component )
{
BOOL ret = FALSE;
LPWSTR destW = NULL, sourceW = NULL, locationW = NULL;
DWORD size;
TRACE("%s, %s, %d, %d, %p, %d, %p, %p\n", debugstr_a(source), debugstr_a(location),
media_type, style, dest, buffer_size, required_size, component);
if (dest && !(destW = MyMalloc( buffer_size * sizeof(WCHAR) ))) return FALSE;
if (source && !(sourceW = strdupAtoW( source ))) goto done;
if (location && !(locationW = strdupAtoW( location ))) goto done;
if (!(ret = SetupCopyOEMInfW( sourceW, locationW, media_type, style, destW,
buffer_size, &size, NULL )))
{
if (required_size) *required_size = size;
goto done;
}
if (dest)
{
if (buffer_size >= size)
{
WideCharToMultiByte( CP_ACP, 0, destW, -1, dest, buffer_size, NULL, NULL );
if (component) *component = strrchr( dest, '\\' ) + 1;
}
else
{
SetLastError( ERROR_INSUFFICIENT_BUFFER );
goto done;
}
}
done:
MyFree( destW );
HeapFree( GetProcessHeap(), 0, sourceW );
HeapFree( GetProcessHeap(), 0, locationW );
if (ret) SetLastError(ERROR_SUCCESS);
return ret;
}
static int compare_files( HANDLE file1, HANDLE file2 )
{
char buffer1[2048];
char buffer2[2048];
DWORD size1;
DWORD size2;
while( ReadFile(file1, buffer1, sizeof(buffer1), &size1, NULL) &&
ReadFile(file2, buffer2, sizeof(buffer2), &size2, NULL) )
{
int ret;
if (size1 != size2)
return size1 > size2 ? 1 : -1;
if (!size1)
return 0;
ret = memcmp( buffer1, buffer2, size1 );
if (ret)
return ret;
}
return 0;
}
/***********************************************************************
* SetupCopyOEMInfW (SETUPAPI.@)
*/
BOOL WINAPI SetupCopyOEMInfW( PCWSTR source, PCWSTR location,
DWORD media_type, DWORD style, PWSTR dest,
DWORD buffer_size, PDWORD required_size, PWSTR *component )
{
BOOL ret = FALSE;
WCHAR target[MAX_PATH], catalog_file[MAX_PATH], *p;
static const WCHAR inf[] = { '\\','i','n','f','\\',0 };
static const WCHAR wszVersion[] = { 'V','e','r','s','i','o','n',0 };
static const WCHAR wszCatalogFile[] = { 'C','a','t','a','l','o','g','F','i','l','e',0 };
DWORD size;
HINF hinf;
TRACE("%s, %s, %d, %d, %p, %d, %p, %p\n", debugstr_w(source), debugstr_w(location),
media_type, style, dest, buffer_size, required_size, component);
if (!source)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
/* check for a relative path */
if (!(*source == '\\' || (*source && source[1] == ':')))
{
SetLastError(ERROR_FILE_NOT_FOUND);
return FALSE;
}
if (!GetWindowsDirectoryW( target, sizeof(target)/sizeof(WCHAR) )) return FALSE;
strcatW( target, inf );
if ((p = strrchrW( source, '\\' )))
strcatW( target, p + 1 );
/* does the file exist already? */
if ((GetFileAttributesW( target ) != INVALID_FILE_ATTRIBUTES) &&
!(style & SP_COPY_NOOVERWRITE))
{
static const WCHAR oem[] = { 'o','e','m',0 };
unsigned int i;
LARGE_INTEGER source_file_size;
HANDLE source_file;
source_file = CreateFileW( source, FILE_READ_DATA | FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, 0, NULL );
if (source_file == INVALID_HANDLE_VALUE)
return FALSE;
if (!GetFileSizeEx( source_file, &source_file_size ))
{
CloseHandle( source_file );
return FALSE;
}
p = strrchrW( target, '\\' ) + 1;
memcpy( p, oem, sizeof(oem) );
p += sizeof(oem)/sizeof(oem[0]) - 1;
/* generate OEMnnn.inf ending */
for (i = 0; i < OEM_INDEX_LIMIT; i++)
{
static const WCHAR format[] = { '%','u','.','i','n','f',0 };
HANDLE dest_file;
LARGE_INTEGER dest_file_size;
wsprintfW( p, format, i );
dest_file = CreateFileW( target, FILE_READ_DATA | FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, 0, NULL );
/* if we found a file name that doesn't exist then we're done */
if (dest_file == INVALID_HANDLE_VALUE)
break;
/* now check if the same inf file has already been copied to the inf
* directory. if so, use that file and don't create a new one */
if (!GetFileSizeEx( dest_file, &dest_file_size ) ||
(dest_file_size.QuadPart != source_file_size.QuadPart) ||
compare_files( source_file, dest_file ))
{
CloseHandle( dest_file );
continue;
}
CloseHandle( dest_file );
break;
}
CloseHandle( source_file );
if (i == OEM_INDEX_LIMIT)
{
SetLastError( ERROR_FILENAME_EXCED_RANGE );
return FALSE;
}