home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / VALIDADD.CP_ / VALIDADD.CP
Encoding:
Text File  |  1993-02-08  |  4.2 KB  |  168 lines

  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 and/or WinHelp documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_AUX_SEG
  14. #pragma code_seg(AFX_AUX_SEG)
  15. #endif
  16.  
  17. BOOL AFXAPI AfxIsValidString(LPCSTR lpsz, int nLength /* = -1 */)
  18. {
  19.     if (lpsz == NULL)
  20.         return FALSE;
  21. #ifdef _WINDOWS
  22.     if (afxData.bWin31)
  23.         return ::IsBadStringPtr(lpsz, nLength) == 0;
  24.     else
  25. #endif
  26.     // Under MS-DOS or Windows 3.0, we only check the first character
  27.     // and not the length of the string
  28.     return AfxIsValidAddress(lpsz, 1, FALSE);
  29. }
  30.  
  31. #ifdef _NEARDATA
  32. BOOL AFXAPI 
  33. AfxIsValidAddress(const void NEAR*np, UINT nBytes, BOOL bReadWrite /* = TRUE */)
  34.     return np != NULL && 
  35.         AfxIsValidAddress((const void FAR*)np, nBytes, bReadWrite); 
  36. }
  37. #endif
  38.  
  39. #pragma optimize("qgel", off)
  40. #pragma warning(disable:4100)
  41.  
  42. extern "C" BOOL AFXAPI
  43. AfxIsValidAddress(const void FAR* lp, UINT nBytes, BOOL bReadWrite /* = TRUE */)
  44. {
  45. #ifdef _PORTABLE
  46.     return lp != NULL;
  47. #else
  48.     BOOL bValid = FALSE;
  49.     
  50. #ifdef _WINDOWS
  51.     BOOL b286 = (WF_CPU286 & GetWinFlags()) ? TRUE : FALSE;
  52.     
  53. if ( !b286 )
  54. {
  55.         
  56.     _asm {
  57.             _asm _emit  0x66
  58.             xor     bx, bx                  ; clear out ebx
  59.             mov     bx, WORD PTR lp + 2
  60.             or      bx,bx
  61.             jz      done                    ; zero segments are always bad
  62.             _asm _emit 0x0F                 ; lar cx, bx
  63.             _asm _emit 0x02
  64.             _asm _emit 0xCB                     
  65.             jnz     done
  66.  
  67.             test    ch,128                  ; if (!present(bx))
  68.             jz      skip_range_test         ;   return fTrue;
  69.  
  70.             cmp     bReadWrite, 0
  71.             je      skip_write_test
  72.  
  73.             _asm _emit 0x0F                 ; verw bx
  74.             _asm _emit 0x00
  75.             _asm _emit 0xEB
  76.                                             ; if (!writeable(bx))
  77.             jnz     done
  78.  
  79.     skip_write_test:
  80.  
  81.             _asm _emit  0x66
  82.             _asm _emit  0x0F                ; lsl, edx, bx
  83.             _asm _emit  0x03
  84.             _asm _emit  0xD3
  85.             jnz     done
  86.                                             ;* dx = last valid address
  87.  
  88.             mov     bx, WORD PTR lp
  89.             mov     cx, nBytes
  90.             jcxz    skip_range_test         ; if length == 0, ignore offset part
  91.  
  92.             ;* make sure start address is valid
  93.             _asm _emit  0x66
  94.             cmp     bx,dx                   ;  if (offsetof(lp) > dx)
  95.             ja      done
  96.  
  97.             ;* make sure last address is also valid
  98.             dec     cx                      ; bias for last byte
  99.             add     bx,cx
  100.             jc      done                    ; overflow => no good
  101.             _asm _emit  0x66
  102.             cmp     dx,bx                   ;  if (offsetof(lp) + nBytes <= dx)
  103.             jb      done
  104.     skip_range_test:
  105.             inc     bValid
  106.     done:
  107.     }
  108. }
  109. else
  110. #endif  // !_WINDOWS
  111.     _asm {
  112.             mov     bx, WORD PTR lp + 2
  113.             or      bx,bx
  114.             jz      done2                   ; zero segments are always bad
  115. #ifndef _DOS
  116.             _asm _emit 0x0F                 ; lar cx, bx
  117.             _asm _emit 0x02
  118.             _asm _emit 0xCB                     
  119.             jnz     done2
  120.  
  121.             test    ch,128                  ; if (!present(bx))
  122.             jz      skip_range_test2        ;   return fTrue;
  123.  
  124.             cmp     bReadWrite, 0
  125.             je      skip_write_test2
  126.  
  127.             _asm _emit 0x0F                 ; verw bx
  128.             _asm _emit 0x00
  129.             _asm _emit 0xEB
  130.                                             ; if (!writeable(bx))
  131.             jnz     done2
  132.  
  133.     skip_write_test2:
  134.  
  135.             _asm _emit  0x0F                ; lsl, dx, bx
  136.             _asm _emit  0x03
  137.             _asm _emit  0xD3
  138.             jnz     done2
  139.                                             ;* dx = last valid address
  140.  
  141.             mov     bx, WORD PTR lp
  142.             mov     cx, nBytes
  143.             jcxz    skip_range_test2            ; if length == 0, ignore offset part
  144.  
  145.             ;* make sure start address is valid
  146.             cmp     bx,dx                   ; if (offsetof(lp) > dx)
  147.             ja      done2
  148.  
  149.             ;* make sure last address is also valid
  150.             dec     cx                      ; bias for last byte
  151.             add     bx,cx
  152.             jc      done2                   ; overflow => no good
  153.             cmp     dx,bx                   ; if (offsetof(lp) + nBytes <= dx)
  154.             jb      done2
  155.     skip_range_test2:
  156.  
  157. #endif //!_DOS
  158.             inc     bValid
  159.     done2:
  160.     }
  161.     return bValid;
  162. #endif  // !_PORTABLE
  163. }
  164.  
  165. #pragma warning(default:4100)
  166. #pragma optimize("", on)
  167.