- Revert r52573
[reactos.git] / dll / win32 / kernel32 / mem / isbad.c
1 /*
2 * PROJECT: ReactOS Win32 Base API
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/kernel32/mem/isbad.c
5 * PURPOSE: Handles probing of memory addresses
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 * Thomas Weidenmueller (w3seek@reactos.org)
8 */
9
10 /* INCLUDES ******************************************************************/
11
12 #include <k32.h>
13
14 #define NDEBUG
15 #include <debug.h>
16
17 extern SYSTEM_BASIC_INFORMATION BaseCachedSysInfo;
18
19 /* FUNCTIONS *****************************************************************/
20
21 /*
22 * @implemented
23 */
24 BOOL
25 WINAPI
26 IsBadReadPtr(IN LPCVOID lp,
27 IN UINT_PTR ucb)
28 {
29 ULONG PageSize;
30 BOOLEAN Result = FALSE;
31 volatile CHAR *Current;
32 PCHAR Last;
33
34 /* Quick cases */
35 if (!ucb) return FALSE;
36 if (!lp) return TRUE;
37
38 /* Get the page size */
39 PageSize = BaseCachedSysInfo.PageSize;
40
41 /* Calculate the last page */
42 Last = (PCHAR)((ULONG_PTR)lp + ucb - 1);
43
44 /* Another quick failure case */
45 if ((ULONG_PTR)Last < (ULONG_PTR)lp) return TRUE;
46
47 /* Enter SEH */
48 _SEH2_TRY
49 {
50 /* Probe the entire range */
51 Current = (volatile CHAR*)lp;
52 Last = (PCHAR)(PAGE_ROUND_DOWN(Last));
53 do
54 {
55 *Current;
56 Current = (volatile CHAR*)(PAGE_ROUND_DOWN(Current) + PAGE_SIZE);
57 } while (Current <= Last);
58 }
59 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
60 {
61 /* We hit an exception, so return true */
62 Result = TRUE;
63 }
64 _SEH2_END
65
66 /* Return exception status */
67 return Result;
68 }
69
70 /*
71 * @implemented
72 */
73 BOOL
74 NTAPI
75 IsBadHugeReadPtr(LPCVOID lp,
76 UINT_PTR ucb)
77 {
78 /* Implementation is the same on 32-bit */
79 return IsBadReadPtr(lp, ucb);
80 }
81
82 /*
83 * @implemented
84 */
85 BOOL
86 NTAPI
87 IsBadCodePtr(FARPROC lpfn)
88 {
89 /* Executing has the same privileges as reading */
90 return IsBadReadPtr((LPVOID)lpfn, 1);
91 }
92
93 /*
94 * @implemented
95 */
96 BOOL
97 NTAPI
98 IsBadWritePtr(LPVOID lp,
99 UINT_PTR ucb)
100 {
101 ULONG PageSize;
102 BOOLEAN Result = FALSE;
103 volatile CHAR *Current;
104 PCHAR Last;
105
106 /* Quick cases */
107 if (!ucb) return FALSE;
108 if (!lp) return TRUE;
109
110 /* Get the page size */
111 PageSize = BaseCachedSysInfo.PageSize;
112
113 /* Calculate the last page */
114 Last = (PCHAR)((ULONG_PTR)lp + ucb - 1);
115
116 /* Another quick failure case */
117 if ((ULONG_PTR)Last < (ULONG_PTR)lp) return TRUE;
118
119 /* Enter SEH */
120 _SEH2_TRY
121 {
122 /* Probe the entire range */
123 Current = (volatile CHAR*)lp;
124 Last = (PCHAR)(PAGE_ROUND_DOWN(Last));
125 do
126 {
127 *Current = *Current;
128 Current = (volatile CHAR*)(PAGE_ROUND_DOWN(Current) + PAGE_SIZE);
129 } while (Current <= Last);
130 }
131 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
132 {
133 /* We hit an exception, so return true */
134 Result = TRUE;
135 }
136 _SEH2_END
137
138 /* Return exception status */
139 return Result;
140 }
141
142 /*
143 * @implemented
144 */
145 BOOL
146 NTAPI
147 IsBadHugeWritePtr(LPVOID lp,
148 UINT_PTR ucb)
149 {
150 /* Implementation is the same on 32-bit */
151 return IsBadWritePtr(lp, ucb);
152 }
153
154 /*
155 * @implemented
156 */
157 BOOL
158 NTAPI
159 IsBadStringPtrW(IN LPCWSTR lpsz,
160 UINT_PTR ucchMax)
161 {
162 BOOLEAN Result = FALSE;
163 volatile WCHAR *Current;
164 PWCHAR Last;
165 WCHAR Char;
166
167 /* Quick cases */
168 if (!ucchMax) return FALSE;
169 if (!lpsz) return TRUE;
170
171 /* Calculate the last page */
172 Last = (PWCHAR)((ULONG_PTR)lpsz + (ucchMax * 2) - 2);
173
174 /* Enter SEH */
175 _SEH2_TRY
176 {
177 /* Probe the entire range */
178 Current = (volatile WCHAR*)lpsz;
179 Last = (PWCHAR)(PAGE_ROUND_DOWN(Last));
180 do
181 {
182 Char = *Current;
183 Current++;
184 } while (Char && (Current != Last + 1));
185 }
186 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
187 {
188 /* We hit an exception, so return true */
189 Result = TRUE;
190 }
191 _SEH2_END
192
193 /* Return exception status */
194 return Result;
195 }
196
197 /*
198 * @implemented
199 */
200 BOOL
201 NTAPI
202 IsBadStringPtrA(IN LPCSTR lpsz,
203 UINT_PTR ucchMax)
204 {
205 BOOLEAN Result = FALSE;
206 volatile CHAR *Current;
207 PCHAR Last;
208 CHAR Char;
209
210 /* Quick cases */
211 if (!ucchMax) return FALSE;
212 if (!lpsz) return TRUE;
213
214 /* Calculate the last page */
215 Last = (PCHAR)((ULONG_PTR)lpsz + ucchMax - 1);
216
217 /* Enter SEH */
218 _SEH2_TRY
219 {
220 /* Probe the entire range */
221 Current = (volatile CHAR*)lpsz;
222 Last = (PCHAR)(PAGE_ROUND_DOWN(Last));
223 do
224 {
225 Char = *Current;
226 Current++;
227 } while (Char && (Current != Last + 1));
228 }
229 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
230 {
231 /* We hit an exception, so return true */
232 Result = TRUE;
233 }
234 _SEH2_END
235
236 /* Return exception status */
237 return Result;
238 }
239
240 /* EOF */