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.

141 lines
2.4 KiB

  1. /*****************************************************************************\
  2. * MODULE: stream.cxx
  3. *
  4. * The module contains the stream interface for file and memory access
  5. *
  6. * Copyright (C) 1997-1998 Microsoft Corporation
  7. *
  8. * History:
  9. * 09/20/00 Weihaic Created
  10. *
  11. \*****************************************************************************/
  12. #include "precomp.h"
  13. #include "priv.h"
  14. CStream::CStream ():
  15. m_dwCurPos (0),
  16. m_bValid(FALSE)
  17. {
  18. }
  19. BOOL
  20. CStream::Reset (
  21. VOID)
  22. {
  23. return SetPtr(0);
  24. }
  25. BOOL
  26. CStream::GetTotalSize (
  27. PDWORD pdwSize) CONST
  28. {
  29. BOOL bRet = FALSE;
  30. if (m_bValid && pdwSize) {
  31. *pdwSize = m_dwTotalSize;
  32. bRet = TRUE;
  33. }
  34. return bRet;
  35. }
  36. CMemStream::CMemStream (
  37. PBYTE pMem,
  38. DWORD dwTotalSize):
  39. m_pMem (pMem)
  40. {
  41. m_dwTotalSize = dwTotalSize;
  42. m_bValid = TRUE;
  43. }
  44. BOOL
  45. CMemStream::SetPtr (
  46. DWORD dwPos)
  47. {
  48. BOOL bRet = FALSE;
  49. if (m_dwCurPos < m_dwTotalSize)
  50. {
  51. m_dwCurPos = dwPos;
  52. bRet = TRUE;
  53. }
  54. return bRet;
  55. }
  56. BOOL
  57. CMemStream::Read (
  58. PBYTE pBuf,
  59. DWORD dwBufSize,
  60. PDWORD pdwSizeRead)
  61. {
  62. DWORD dwSizeLeft = m_dwTotalSize - m_dwCurPos;
  63. *pdwSizeRead = (dwSizeLeft > dwBufSize)?dwBufSize:dwSizeLeft;
  64. CopyMemory (pBuf, m_pMem + m_dwCurPos, *pdwSizeRead);
  65. return TRUE;
  66. }
  67. CFileStream::CFileStream (
  68. HANDLE hFile):
  69. m_hFile (hFile)
  70. {
  71. BOOL bRet = FALSE;
  72. LARGE_INTEGER LargeSize;
  73. if (GetFileSizeEx (m_hFile, &LargeSize) && LargeSize.HighPart == 0) {
  74. m_dwTotalSize = LargeSize.LowPart;
  75. bRet = TRUE;
  76. }
  77. m_bValid = bRet;
  78. }
  79. BOOL
  80. CFileStream::SetPtr (
  81. DWORD dwPos)
  82. {
  83. BOOL bRet = FALSE;
  84. LARGE_INTEGER LargePos;
  85. LargePos.LowPart = dwPos;
  86. LargePos.HighPart = 0;
  87. if (m_bValid) {
  88. if (SetFilePointerEx (m_hFile, LargePos, NULL, FILE_BEGIN)) {
  89. m_dwCurPos = dwPos;
  90. bRet = TRUE;
  91. }
  92. }
  93. return bRet;
  94. }
  95. BOOL
  96. CFileStream::Read (
  97. PBYTE pBuf,
  98. DWORD dwBufSize,
  99. PDWORD pdwSizeRead)
  100. {
  101. BOOL bRet = FALSE;
  102. if (m_bValid) {
  103. if (ReadFile (m_hFile, pBuf, dwBufSize, pdwSizeRead, NULL)) {
  104. m_dwCurPos+= *pdwSizeRead;
  105. bRet = TRUE;
  106. }
  107. }
  108. return bRet;
  109. }