setupapi: Improve parameter validation for SetupCreateDiskSpaceListA/W.
[wine] / dlls / setupapi / diskspace.c
1 /*
2  * SetupAPI DiskSpace functions
3  *
4  * Copyright 2004 CodeWeavers (Aric Stewart)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winnls.h"
28 #include "winreg.h"
29 #include "setupapi.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
33
34 typedef struct {
35     WCHAR   lpzName[20];
36     LONGLONG dwFreeSpace;
37     LONGLONG dwWantedSpace;
38 } DRIVE_ENTRY, *LPDRIVE_ENTRY;
39
40 typedef struct {
41     DWORD   dwDriveCount;
42     DRIVE_ENTRY Drives[26];
43 } DISKSPACELIST, *LPDISKSPACELIST;
44
45
46 /***********************************************************************
47  *              SetupCreateDiskSpaceListW  (SETUPAPI.@)
48  */
49 HDSKSPC WINAPI SetupCreateDiskSpaceListW(PVOID Reserved1, DWORD Reserved2, UINT Flags)
50 {
51     WCHAR drives[255];
52     DWORD rc;
53     WCHAR *ptr;
54     LPDISKSPACELIST list=NULL;
55
56     TRACE("(%p, %u, 0x%08x)\n", Reserved1, Reserved2, Flags);
57
58     if (Reserved1 || Reserved2 || Flags & ~SPDSL_IGNORE_DISK)
59     {
60         SetLastError(ERROR_INVALID_PARAMETER);
61         return NULL;
62     }
63
64     rc = GetLogicalDriveStringsW(255,drives);
65
66     if (rc == 0)
67         return NULL;
68
69     list = HeapAlloc(GetProcessHeap(),0,sizeof(DISKSPACELIST));
70
71     list->dwDriveCount = 0;
72     
73     ptr = drives;
74     
75     while (*ptr)
76     {
77         DWORD type = GetDriveTypeW(ptr);
78         if (type == DRIVE_FIXED)
79         {
80             DWORD clusters;
81             DWORD sectors;
82             DWORD bytes;
83             DWORD total;
84             lstrcpyW(list->Drives[list->dwDriveCount].lpzName,ptr);
85             GetDiskFreeSpaceW(ptr,&sectors,&bytes,&clusters,&total);
86             list->Drives[list->dwDriveCount].dwFreeSpace = clusters * sectors *
87                                                            bytes;
88             list->Drives[list->dwDriveCount].dwWantedSpace = 0;
89             list->dwDriveCount++;
90         }
91        ptr += lstrlenW(ptr) + 1;
92     }
93     return list;
94 }
95
96
97 /***********************************************************************
98  *              SetupCreateDiskSpaceListA  (SETUPAPI.@)
99  */
100 HDSKSPC WINAPI SetupCreateDiskSpaceListA(PVOID Reserved1, DWORD Reserved2, UINT Flags)
101 {
102     return SetupCreateDiskSpaceListW( Reserved1, Reserved2, Flags );
103 }
104
105
106 /***********************************************************************
107  *              SetupAddInstallSectionToDiskSpaceListA  (SETUPAPI.@)
108  */
109 BOOL WINAPI SetupAddInstallSectionToDiskSpaceListA(HDSKSPC DiskSpace, 
110                         HINF InfHandle, HINF LayoutInfHandle, 
111                         LPCSTR SectionName, PVOID Reserved1, UINT Reserved2)
112 {
113     FIXME ("Stub\n");
114     return TRUE;
115 }
116
117 /***********************************************************************
118 *               SetupQuerySpaceRequiredOnDriveA  (SETUPAPI.@)
119 */
120 BOOL WINAPI SetupQuerySpaceRequiredOnDriveA(HDSKSPC DiskSpace, 
121                         LPCSTR DriveSpec, LONGLONG* SpaceRequired, 
122                         PVOID Reserved1, UINT Reserved2)
123 {
124     WCHAR driveW[20];
125     unsigned int i;
126     LPDISKSPACELIST list = DiskSpace;
127     BOOL rc = FALSE;
128     static const WCHAR bkslsh[]= {'\\',0};
129
130     MultiByteToWideChar(CP_ACP,0,DriveSpec,-1,driveW,20);
131
132     lstrcatW(driveW,bkslsh);
133
134     TRACE("Looking for drive %s\n",debugstr_w(driveW));
135  
136     for (i = 0; i < list->dwDriveCount; i++)
137     {
138         TRACE("checking drive %s\n",debugstr_w(list->Drives[i].lpzName));
139         if (lstrcmpW(driveW,list->Drives[i].lpzName)==0)
140         {
141             rc = TRUE;
142             *SpaceRequired = list->Drives[i].dwWantedSpace;
143             break;
144         }
145     }
146
147     return rc;
148 }
149
150 /***********************************************************************
151 *               SetupDestroyDiskSpaceList  (SETUPAPI.@)
152 */
153 BOOL WINAPI SetupDestroyDiskSpaceList(HDSKSPC DiskSpace)
154 {
155     LPDISKSPACELIST list = DiskSpace;
156     HeapFree(GetProcessHeap(),0,list);
157     return TRUE; 
158 }