Source code of Windows XP (NT5)
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.

171 lines
3.8 KiB

  1. #include "stdwin.h"
  2. #include "mfr.h"
  3. /////////////////////////////////////////////////////////////////////////////
  4. //
  5. // CMappedFileRead class
  6. //
  7. /////////////////////////////////////////////////////////////////////////////
  8. CMappedFileRead::CMappedFileRead()
  9. {
  10. m_szPath[0] = L'\0';
  11. m_dwSize = 0;
  12. m_hFile = INVALID_HANDLE_VALUE;
  13. m_hMap = INVALID_HANDLE_VALUE;
  14. m_pBuf = NULL;
  15. }
  16. CMappedFileRead::~CMappedFileRead()
  17. {
  18. Close();
  19. }
  20. /////////////////////////////////////////////////////////////////////////////
  21. BOOL CMappedFileRead::Open( LPCWSTR cszPath )
  22. {
  23. BOOL fRet = FALSE;
  24. Close();
  25. m_hFile = ::CreateFile( cszPath, GENERIC_READ,
  26. FILE_SHARE_READ|FILE_SHARE_WRITE,
  27. NULL, OPEN_EXISTING, 0, NULL );
  28. if ( m_hFile == INVALID_HANDLE_VALUE )
  29. {
  30. fprintf(stderr, "CMappedFileRead::Open\n ::CreateFile failed, err=%u\n", ::GetLastError());
  31. goto Exit;
  32. }
  33. m_dwSize = ::GetFileSize( m_hFile, NULL );
  34. if ( m_dwSize == 0xFFFFFFFF )
  35. {
  36. fprintf(stderr, "CMappedFileRead::Open\n ::GetFileSize failed, err=%u\n", ::GetLastError());
  37. goto Exit;
  38. }
  39. m_hMap = ::CreateFileMapping( m_hFile, NULL, PAGE_READONLY, 0, 0, NULL );
  40. if ( m_hFile == INVALID_HANDLE_VALUE )
  41. {
  42. fprintf(stderr, "CMappedFileRead::Open\n ::CreateFileMapping failed, err=%u\n", ::GetLastError());
  43. goto Exit;
  44. }
  45. m_pBuf = (LPBYTE)::MapViewOfFile( m_hMap, FILE_MAP_READ, 0, 0, 0 );
  46. if ( m_pBuf == NULL )
  47. {
  48. fprintf(stderr, "CMappedFileRead::Open\n ::MapViewOfFile failed, err=%u\n", ::GetLastError());
  49. goto Exit;
  50. }
  51. ::lstrcpy( m_szPath, cszPath );
  52. m_pCur = m_pBuf;
  53. m_dwAvail = m_dwSize;
  54. fRet = TRUE;
  55. Exit:
  56. if ( !fRet )
  57. Close();
  58. return( fRet );
  59. }
  60. /////////////////////////////////////////////////////////////////////////////
  61. void CMappedFileRead::Close()
  62. {
  63. if ( m_pBuf != NULL )
  64. {
  65. ::UnmapViewOfFile( m_pBuf );
  66. m_pBuf = NULL;
  67. }
  68. if ( m_hMap != INVALID_HANDLE_VALUE )
  69. {
  70. ::CloseHandle( m_hMap );
  71. m_hMap = INVALID_HANDLE_VALUE;
  72. }
  73. if ( m_hFile != INVALID_HANDLE_VALUE )
  74. {
  75. ::CloseHandle( m_hFile );
  76. m_hFile = INVALID_HANDLE_VALUE;
  77. }
  78. }
  79. /////////////////////////////////////////////////////////////////////////////
  80. BOOL CMappedFileRead::Read( LPVOID pBuf, DWORD cbBuf )
  81. {
  82. BOOL fRet = FALSE;
  83. if ( cbBuf > m_dwAvail )
  84. {
  85. fprintf(stderr, "CMappedFileRead::Read(LPVOID,DWORD)\n Insufficient data - %d bytes (need=%d bytes)\n", m_dwAvail, cbBuf);
  86. goto Exit;
  87. }
  88. ::CopyMemory( pBuf, m_pCur, cbBuf );
  89. m_pCur += cbBuf;
  90. m_dwAvail -= cbBuf;
  91. fRet = TRUE;
  92. Exit:
  93. return( fRet );
  94. }
  95. /////////////////////////////////////////////////////////////////////////////
  96. BOOL CMappedFileRead::Read( DWORD *pdw )
  97. {
  98. BOOL fRet = FALSE;
  99. if ( sizeof(DWORD) > m_dwAvail )
  100. {
  101. fprintf(stderr, "CMappedFileRead::Read(DWORD)\n Insufficient data - %d bytes (need=%d bytes)\n", m_dwAvail, sizeof(DWORD));
  102. goto Exit;
  103. }
  104. *pdw = *((LPDWORD)m_pCur);
  105. m_pCur += sizeof(DWORD);
  106. m_dwAvail -= sizeof(DWORD);
  107. fRet = TRUE;
  108. Exit:
  109. return( fRet );
  110. }
  111. /////////////////////////////////////////////////////////////////////////////
  112. BOOL CMappedFileRead::ReadDynStrW( LPWSTR szBuf, DWORD cchMax )
  113. {
  114. BOOL fRet = FALSE;
  115. DWORD dwLen;
  116. if ( !Read( &dwLen ) )
  117. goto Exit;
  118. if ( dwLen == 0 )
  119. {
  120. szBuf[0] = L'\0';
  121. goto Done;
  122. }
  123. if ( dwLen > cchMax*sizeof(WCHAR) )
  124. {
  125. fprintf(stderr, "CMappedFileRead::ReadDynStrW\n Invalid string length - %d (max=%d)\n", dwLen, cchMax);
  126. goto Exit;
  127. }
  128. if ( !Read( szBuf, dwLen ) )
  129. goto Exit;
  130. Done:
  131. fRet = TRUE;
  132. Exit:
  133. return( fRet );
  134. }
  135. // end of file