Fixed buffer overflow.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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     rc = GetLogicalDriveStringsW(255,drives);
57
58     if (rc == 0)
59         return NULL;
60
61     list = (LPDISKSPACELIST)HeapAlloc(GetProcessHeap(),0,sizeof(DISKSPACELIST));
62
63     list->dwDriveCount = 0;
64     
65     ptr = drives;
66     
67     while (*ptr)
68     {
69         DWORD type = GetDriveTypeW(ptr);
70         DWORD len;
71         if (type == DRIVE_FIXED)
72         {
73             DWORD clusters;
74             DWORD sectors;
75             DWORD bytes;
76             DWORD total;
77             lstrcpyW(list->Drives[list->dwDriveCount].lpzName,ptr);
78             GetDiskFreeSpaceW(ptr,&sectors,&bytes,&clusters,&total);
79             list->Drives[list->dwDriveCount].dwFreeSpace = clusters * sectors *
80                                                            bytes;
81             list->Drives[list->dwDriveCount].dwWantedSpace = 0;
82             list->dwDriveCount++;
83         }
84        len = lstrlenW(ptr);
85        len++;
86        ptr+=sizeof(WCHAR)*len;
87     }
88     return  (HANDLE)list;
89 }
90
91
92 /***********************************************************************
93  *              SetupCreateDiskSpaceListA  (SETUPAPI.@)
94  */
95 HDSKSPC WINAPI SetupCreateDiskSpaceListA(PVOID Reserved1, DWORD Reserved2, UINT Flags)
96 {
97     return SetupCreateDiskSpaceListW( Reserved1, Reserved2, Flags );
98 }
99
100
101 /***********************************************************************
102  *              SetupAddInstallSectionToDiskSpaceListA  (SETUPAPI.@)
103  */
104 BOOL WINAPI SetupAddInstallSectionToDiskSpaceListA(HDSKSPC DiskSpace, 
105                         HINF InfHandle, HINF LayoutInfHandle, 
106                         LPSTR SectionName, PVOID Reserved1, UINT Reserved2)
107 {
108     FIXME ("Stub\n");
109     return TRUE;
110 }
111
112 /***********************************************************************
113 *               SetupQuerySpaceRequiredOnDriveA  (SETUPAPI.@)
114 */
115 BOOL WINAPI SetupQuerySpaceRequiredOnDriveA(HDSKSPC DiskSpace, 
116                         LPSTR DriveSpec, LONGLONG* SpaceRequired, 
117                         PVOID Reserved1, UINT Reserved2)
118 {
119     WCHAR driveW[20];
120     int i;
121     LPDISKSPACELIST list = (LPDISKSPACELIST)DiskSpace;
122     BOOL rc = FALSE;
123     static const WCHAR bkslsh[]= {'\\',0};
124
125     MultiByteToWideChar(CP_ACP,0,DriveSpec,-1,driveW,20);
126
127     lstrcatW(driveW,bkslsh);
128
129     TRACE("Looking for drive %s\n",debugstr_w(driveW));
130  
131     for (i = 0; i < list->dwDriveCount; i++)
132     {
133         TRACE("checking drive %s\n",debugstr_w(list->Drives[i].lpzName));
134         if (lstrcmpW(driveW,list->Drives[i].lpzName)==0)
135         {
136             rc = TRUE;
137             *SpaceRequired = list->Drives[i].dwWantedSpace;
138             break;
139         }
140     }
141
142     return rc;
143 }
144
145 /***********************************************************************
146 *               SetupDestroyDiskSpaceList  (SETUPAPI.@)
147 */
148 BOOL WINAPI SetupDestroyDiskSpaceList(HDSKSPC DiskSpace)
149 {
150     LPDISKSPACELIST list = (LPDISKSPACELIST)DiskSpace;
151     HeapFree(GetProcessHeap(),0,list);
152     return TRUE; 
153 }