|
|
1.1 root 1: // This is a part of the Microsoft Foundation Classes C++ library.
2: // Copyright (C) 1992 Microsoft Corporation
3: // All rights reserved.
4: //
5: // This source code is only intended as a supplement to the
6: // Microsoft Foundation Classes Reference and Microsoft
7: // QuickHelp documentation provided with the library.
8: // See these sources for detailed information regarding the
9: // Microsoft Foundation Classes product.
10:
11: #include "afx.h"
12: #ifdef _WINDOWS
13: #include "afxwin.h"
14: #endif
15: #pragma hdrstop
16:
17: #ifdef AFX_CORE_SEG
18: #pragma code_seg(AFX_CORE_SEG)
19: #endif
20:
21: #pragma optimize("qgel", off)
22: #pragma warning(disable:4100)
23:
24: extern "C" BOOL FAR PASCAL
25: AfxIsValidAddress(const void FAR* lp, UINT nBytes, BOOL bReadWrite /* = TRUE */)
26: {
27: #ifdef _NTWIN
1.1.1.2 ! root 28: // simple version using Win-32 APIs for pointer validation.
! 29: return (lp != NULL && !IsBadReadPtr(lp, nBytes) &&
! 30: (!bReadWrite || !IsBadWritePtr((LPVOID)lp, nBytes)));
1.1 root 31: #else
32: BOOL bValid = FALSE;
33:
34: #ifdef _WINDOWS
35: BOOL b286 = (WF_CPU286 & GetWinFlags()) ? TRUE : FALSE;
36:
37: if ( !b286 )
38: {
39:
40: _asm {
41: _asm _emit 0x66
42: xor bx, bx ; clear out ebx
43: mov bx, WORD PTR lp + 2
44: or bx,bx
45: jz done ; zero segments are always bad
46: _asm _emit 0x0F ; lar cx, bx
47: _asm _emit 0x02
48: _asm _emit 0xCB
49: jnz done
50:
51: test ch,128 ; if (!present(bx))
52: jz skip_range_test ; return fTrue;
53:
54: cmp bReadWrite, 0
55: je skip_write_test
56:
57: _asm _emit 0x0F ; verw bx
58: _asm _emit 0x00
59: _asm _emit 0xEB
60: ; if (!writeable(bx))
61: jnz done
62:
63: skip_write_test:
64:
65: _asm _emit 0x66
66: _asm _emit 0x0F ; lsl, edx, bx
67: _asm _emit 0x03
68: _asm _emit 0xD3
69: jnz done
70: ;* dx = last valid address
71:
72: mov bx, WORD PTR lp
73: mov cx, nBytes
74: jcxz skip_range_test ; if length == 0, ignore offset part
75:
76: ;* make sure start address is valid
77: _asm _emit 0x66
78: cmp bx,dx ; if (offsetof(lp) > dx)
79: ja done
80:
81: ;* make sure last address is also valid
82: dec cx ; bias for last byte
83: add bx,cx
84: jc done ; overflow => no good
85: _asm _emit 0x66
86: cmp dx,bx ; if (offsetof(lp) + nBytes <= dx)
87: jb done
88: skip_range_test:
89: inc bValid
90: done:
91: }
92: }
93: else
94: #endif // !_WINDOWS
95: _asm {
96: mov bx, WORD PTR lp + 2
97: or bx,bx
98: jz done2 ; zero segments are always bad
99: #ifndef _DOS
100: _asm _emit 0x0F ; lar cx, bx
101: _asm _emit 0x02
102: _asm _emit 0xCB
103: jnz done2
104:
105: test ch,128 ; if (!present(bx))
106: jz skip_range_test2 ; return fTrue;
107:
108: cmp bReadWrite, 0
109: je skip_write_test2
110:
111: _asm _emit 0x0F ; verw bx
112: _asm _emit 0x00
113: _asm _emit 0xEB
114: ; if (!writeable(bx))
115: jnz done2
116:
117: skip_write_test2:
118:
119: _asm _emit 0x0F ; lsl, dx, bx
120: _asm _emit 0x03
121: _asm _emit 0xD3
122: jnz done2
123: ;* dx = last valid address
124:
125: mov bx, WORD PTR lp
126: mov cx, nBytes
127: jcxz skip_range_test2 ; if length == 0, ignore offset part
128:
129: ;* make sure start address is valid
130: cmp bx,dx ; if (offsetof(lp) > dx)
131: ja done2
132:
133: ;* make sure last address is also valid
134: dec cx ; bias for last byte
135: add bx,cx
136: jc done2 ; overflow => no good
137: cmp dx,bx ; if (offsetof(lp) + nBytes <= dx)
138: jb done2
139: skip_range_test2:
140:
141: #endif //!_DOS
142: inc bValid
143: done2:
144: }
145: return bValid;
146: #endif // _NTWIN
147: }
148:
149: #pragma warning(default:4100)
150: #pragma optimize("", on)
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.