Miscellaneous implementation of kernel32 stubs.
[reactos.git] / reactos / lib / kernel32 / mem / isbad.c
1 #include <windows.h>
2
3 WINBOOL
4 STDCALL
5 IsBadReadPtr( CONST VOID *lp, UINT ucb )
6 {
7 MEMORY_BASIC_INFORMATION MemoryInformation;
8
9 if ( ucb == 0 )
10 return FALSE;
11
12 VirtualQuery( lp, &MemoryInformation, sizeof(MEMORY_BASIC_INFORMATION) );
13
14 if ( MemoryInformation.State != MEM_COMMIT )
15 return FALSE;
16
17 if ( MemoryInformation.RegionSize < ucb )
18 return FALSE;
19
20 if ( MemoryInformation.Protect == PAGE_EXECUTE )
21 return FALSE;
22
23 if ( MemoryInformation.Protect == PAGE_NOACCESS )
24 return FALSE;
25
26 return TRUE;
27
28 }
29 WINBOOL
30 STDCALL
31 IsBadHugeReadPtr( CONST VOID *lp, UINT ucb )
32 {
33 return IsBadReadPtr(lp,ucb);
34 }
35
36 WINBOOL
37 STDCALL
38 IsBadCodePtr( FARPROC lpfn )
39 {
40 MEMORY_BASIC_INFORMATION MemoryInformation;
41
42
43 VirtualQuery( lpfn, &MemoryInformation, sizeof(MEMORY_BASIC_INFORMATION) );
44
45 if ( MemoryInformation.State != MEM_COMMIT )
46 return FALSE;
47
48
49 if ( MemoryInformation.Protect == PAGE_EXECUTE || MemoryInformation.Protect == PAGE_EXECUTE_READ)
50 return TRUE;
51
52
53 return FALSE;
54 }
55
56 WINBOOL
57 STDCALL IsBadWritePtr( LPVOID lp, UINT ucb )
58 {
59 MEMORY_BASIC_INFORMATION MemoryInformation;
60
61 if ( ucb == 0 )
62 return FALSE;
63
64 VirtualQuery( lp, &MemoryInformation, sizeof(MEMORY_BASIC_INFORMATION) );
65
66 if ( MemoryInformation.State != MEM_COMMIT )
67 return FALSE;
68
69
70 if ( MemoryInformation.RegionSize < ucb )
71 return FALSE;
72
73
74 if ( MemoryInformation.Protect == PAGE_READONLY)
75 return FALSE;
76
77 if ( MemoryInformation.Protect == PAGE_EXECUTE || MemoryInformation.Protect == PAGE_EXECUTE_READ)
78 return FALSE;
79
80 if ( MemoryInformation.Protect == PAGE_NOACCESS )
81 return FALSE;
82
83 return TRUE;
84 }
85
86 WINBOOL
87 STDCALL
88 IsBadHugeWritePtr(
89 LPVOID lp,
90 UINT ucb
91 )
92 {
93 return IsBadWritePtr(lp,ucb);
94 }
95
96
97 WINBOOL
98 STDCALL
99 IsBadStringPtrW( LPCWSTR lpsz, UINT ucchMax )
100 {
101 UINT Len = wcsnlen(lpsz+1,ucchMax>>1);
102 return IsBadReadPtr(lpsz,Len<<1);
103 }
104
105 WINBOOL
106 STDCALL
107 IsBadStringPtrA( LPCSTR lpsz, UINT ucchMax )
108 {
109 UINT Len = strnlen(lpsz+1,ucchMax);
110 return IsBadReadPtr(lpsz,Len);
111 }