Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
4.6 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: Mfr.cxx
  7. //
  8. // Contents: The implementation of the CMappedFileRead class
  9. //
  10. //
  11. // Objects:
  12. //
  13. // Coupling:
  14. //
  15. // Notes:
  16. //
  17. // History: 03-May-2001 WeiyouC Copied from dev code and minor rewite
  18. //
  19. //----------------------------------------------------------------------------
  20. #include <nt.h>
  21. #include <ntrtl.h>
  22. #include <nturtl.h>
  23. #include <windows.h>
  24. #include <stdio.h>
  25. #include <shellapi.h>
  26. #include <srdefs.h>
  27. #include <srshell.h>
  28. #include "mfr.h"
  29. /////////////////////////////////////////////////////////////////////////////
  30. //
  31. // CMappedFileRead class
  32. //
  33. /////////////////////////////////////////////////////////////////////////////
  34. CMappedFileRead::CMappedFileRead()
  35. {
  36. m_szPath[0] = L'\0';
  37. m_dwSize = 0;
  38. m_hFile = INVALID_HANDLE_VALUE;
  39. m_hMap = INVALID_HANDLE_VALUE;
  40. m_pBuf = NULL;
  41. }
  42. CMappedFileRead::~CMappedFileRead()
  43. {
  44. Close();
  45. }
  46. /////////////////////////////////////////////////////////////////////////////
  47. BOOL CMappedFileRead::Open( LPCWSTR cszPath )
  48. {
  49. BOOL fRet = FALSE;
  50. Close();
  51. m_hFile = ::CreateFile( cszPath, GENERIC_READ,
  52. FILE_SHARE_READ|FILE_SHARE_WRITE,
  53. NULL, OPEN_EXISTING, 0, NULL );
  54. if ( m_hFile == INVALID_HANDLE_VALUE )
  55. {
  56. fprintf(stderr, "CMappedFileRead::Open\n ::CreateFile failed, err=%u\n", ::GetLastError());
  57. goto Exit;
  58. }
  59. m_dwSize = ::GetFileSize( m_hFile, NULL );
  60. if ( m_dwSize == 0xFFFFFFFF )
  61. {
  62. fprintf(stderr, "CMappedFileRead::Open\n ::GetFileSize failed, err=%u\n", ::GetLastError());
  63. goto Exit;
  64. }
  65. m_hMap = ::CreateFileMapping( m_hFile, NULL, PAGE_READONLY, 0, 0, NULL );
  66. if ( m_hFile == INVALID_HANDLE_VALUE )
  67. {
  68. fprintf(stderr, "CMappedFileRead::Open\n ::CreateFileMapping failed, err=%u\n", ::GetLastError());
  69. goto Exit;
  70. }
  71. m_pBuf = (LPBYTE)::MapViewOfFile( m_hMap, FILE_MAP_READ, 0, 0, 0 );
  72. if ( m_pBuf == NULL )
  73. {
  74. fprintf(stderr, "CMappedFileRead::Open\n ::MapViewOfFile failed, err=%u\n", ::GetLastError());
  75. goto Exit;
  76. }
  77. ::lstrcpy( m_szPath, cszPath );
  78. m_pCur = m_pBuf;
  79. m_dwAvail = m_dwSize;
  80. fRet = TRUE;
  81. Exit:
  82. if ( !fRet )
  83. Close();
  84. return( fRet );
  85. }
  86. /////////////////////////////////////////////////////////////////////////////
  87. void CMappedFileRead::Close()
  88. {
  89. if ( m_pBuf != NULL )
  90. {
  91. ::UnmapViewOfFile( m_pBuf );
  92. m_pBuf = NULL;
  93. }
  94. if ( m_hMap != INVALID_HANDLE_VALUE )
  95. {
  96. ::CloseHandle( m_hMap );
  97. m_hMap = INVALID_HANDLE_VALUE;
  98. }
  99. if ( m_hFile != INVALID_HANDLE_VALUE )
  100. {
  101. ::CloseHandle( m_hFile );
  102. m_hFile = INVALID_HANDLE_VALUE;
  103. }
  104. }
  105. /////////////////////////////////////////////////////////////////////////////
  106. BOOL CMappedFileRead::Read( LPVOID pBuf, DWORD cbBuf )
  107. {
  108. BOOL fRet = FALSE;
  109. if ( cbBuf > m_dwAvail )
  110. {
  111. fprintf(stderr, "CMappedFileRead::Read(LPVOID,DWORD)\n Insufficient data - %d bytes (need=%d bytes)\n", m_dwAvail, cbBuf);
  112. goto Exit;
  113. }
  114. ::CopyMemory( pBuf, m_pCur, cbBuf );
  115. m_pCur += cbBuf;
  116. m_dwAvail -= cbBuf;
  117. fRet = TRUE;
  118. Exit:
  119. return( fRet );
  120. }
  121. /////////////////////////////////////////////////////////////////////////////
  122. BOOL CMappedFileRead::Read( DWORD *pdw )
  123. {
  124. BOOL fRet = FALSE;
  125. if ( sizeof(DWORD) > m_dwAvail )
  126. {
  127. fprintf(stderr, "CMappedFileRead::Read(DWORD)\n Insufficient data - %d bytes (need=%d bytes)\n", m_dwAvail, sizeof(DWORD));
  128. goto Exit;
  129. }
  130. *pdw = *((LPDWORD)m_pCur);
  131. m_pCur += sizeof(DWORD);
  132. m_dwAvail -= sizeof(DWORD);
  133. fRet = TRUE;
  134. Exit:
  135. return( fRet );
  136. }
  137. /////////////////////////////////////////////////////////////////////////////
  138. BOOL CMappedFileRead::ReadDynStrW( LPWSTR szBuf, DWORD cchMax )
  139. {
  140. BOOL fRet = FALSE;
  141. DWORD dwLen;
  142. if ( !Read( &dwLen ) )
  143. goto Exit;
  144. if ( dwLen == 0 )
  145. {
  146. szBuf[0] = L'\0';
  147. goto Done;
  148. }
  149. if ( dwLen > cchMax*sizeof(WCHAR) )
  150. {
  151. fprintf(stderr, "CMappedFileRead::ReadDynStrW\n Invalid string length - %d (max=%d)\n", dwLen, cchMax);
  152. goto Exit;
  153. }
  154. if ( !Read( szBuf, dwLen ) )
  155. goto Exit;
  156. Done:
  157. fRet = TRUE;
  158. Exit:
  159. return( fRet );
  160. }
  161. // end of file