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.

266 lines
6.3 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997 - 1998
  5. //
  6. // File: mmistrm.hxx
  7. //
  8. // Contents: Memory Mapped IStream
  9. //
  10. // Classes: CMmIStream
  11. //
  12. // History: 11-Feb-97 KyleP Created
  13. //
  14. //----------------------------------------------------------------------------
  15. #include <pch.cxx>
  16. #pragma hdrstop
  17. #include <mmistrm.hxx>
  18. unsigned const cbMaxMappable = 1024 * 1024;
  19. //+-------------------------------------------------------------------------
  20. //
  21. // Member: CMmIStream::CMMIStream, public
  22. //
  23. // Synopsis: Constructor
  24. //
  25. // History: 11-Feb-97 KyleP Created
  26. //
  27. //--------------------------------------------------------------------------
  28. CMmIStream::CMmIStream()
  29. : _pStream( 0 ),
  30. _pBuf( 0 )
  31. {
  32. }
  33. //+-------------------------------------------------------------------------
  34. //
  35. // Member: CMmIStream::~CMMIStream, public
  36. //
  37. // Synopsis: Destructor
  38. //
  39. // History: 11-Feb-97 KyleP Created
  40. //
  41. //--------------------------------------------------------------------------
  42. CMmIStream::~CMmIStream()
  43. {
  44. Close();
  45. }
  46. //+-------------------------------------------------------------------------
  47. //
  48. // Member: CMmIStream::Open, public
  49. //
  50. // Synopsis: Opens a stream
  51. //
  52. // History: 11-Feb-97 KyleP Created
  53. //
  54. //--------------------------------------------------------------------------
  55. void CMmIStream::Open( IStream * pStream )
  56. {
  57. Win4Assert( 0 == _pStream );
  58. Win4Assert( 0 == _pBuf );
  59. _pStream = pStream;
  60. _pStream->AddRef();
  61. //
  62. // Get stream stats.
  63. //
  64. SCODE sc = pStream->Stat( &_statstg, STATFLAG_NONAME );
  65. if ( FAILED(sc) )
  66. {
  67. THROW( CException( sc ) );
  68. }
  69. }
  70. //+-------------------------------------------------------------------------
  71. //
  72. // Member: CMmIStream::Close, public
  73. //
  74. // Synopsis: Close a stream
  75. //
  76. // History: 11-Feb-97 KyleP Created
  77. //
  78. //--------------------------------------------------------------------------
  79. void CMmIStream::Close()
  80. {
  81. if ( 0 != _pStream )
  82. _pStream->Release();
  83. delete [] _pBuf;
  84. _pStream = 0;
  85. _pBuf = 0;
  86. }
  87. //+-------------------------------------------------------------------------
  88. //
  89. // Member: CMmIStream::MapAll, public
  90. //
  91. // Synopsis: Map all of a stream
  92. //
  93. // Arguments: [sbuf] -- Stream buffer to fill in
  94. //
  95. // History: 11-Feb-97 KyleP Created
  96. //
  97. //--------------------------------------------------------------------------
  98. void CMmIStream::MapAll( CMmStreamBuf& sbuf )
  99. {
  100. //
  101. // Only 'map' up to a reasonable size.
  102. //
  103. if ( 0 != SizeHigh() )
  104. THROW( CException( STATUS_SECTION_TOO_BIG ) );
  105. Map( sbuf, SizeLow(), 0, 0 );
  106. }
  107. //+-------------------------------------------------------------------------
  108. //
  109. // Member: CMmIStream::Map, public
  110. //
  111. // Synopsis: Map part of a stream
  112. //
  113. // Arguments: [sbuf] -- Stream buffer to fill in
  114. // [cb] -- Size to map
  115. // [offLow] -- Offset in stream
  116. // [offHigh] -- Offset in stream
  117. // [fMapForWrite] -- TRUE --> Writeable (invalid option here)
  118. //
  119. // History: 11-Feb-97 KyleP Created
  120. //
  121. //--------------------------------------------------------------------------
  122. void CMmIStream::Map ( CMmStreamBuf& sbuf,
  123. ULONG cb,
  124. ULONG offLow,
  125. ULONG offHigh,
  126. BOOL fMapForWrite )
  127. {
  128. //
  129. // Only 'map' up to a reasonable size.
  130. //
  131. if ( cb > cbMaxMappable )
  132. THROW( CException( STATUS_SECTION_TOO_BIG ) );
  133. //
  134. // Now, allocate the memory.
  135. //
  136. Win4Assert( 0 == _pBuf );
  137. _pBuf = new BYTE [ cb ];
  138. //
  139. // Then seek and read.
  140. //
  141. LARGE_INTEGER off = { offLow, offHigh };
  142. SCODE sc = _pStream->Seek( off,
  143. STREAM_SEEK_SET, // From beginning of file
  144. 0 ); // Don't return new seek pointer
  145. if ( FAILED(sc) )
  146. {
  147. THROW( CException( sc ) );
  148. }
  149. ULONG cbRead = 0;
  150. sc = _pStream->Read( _pBuf, cb, &cbRead );
  151. if ( FAILED(sc) )
  152. {
  153. THROW( CException( sc ) );
  154. }
  155. if ( cb > cbRead )
  156. {
  157. THROW( CException( E_FAIL ) );
  158. }
  159. //
  160. // Finally, set up the buffer.
  161. //
  162. sbuf.SetBuf( _pBuf );
  163. sbuf.SetSize ( cbRead );
  164. sbuf.SetStream ( this );
  165. }
  166. //+-------------------------------------------------------------------------
  167. //
  168. // Member: CMmIStream::Unmap, public
  169. //
  170. // Synopsis: Unmap stream
  171. //
  172. // Arguments: [sbuf] -- Stream buffer to unmap
  173. //
  174. // History: 11-Feb-97 KyleP Created
  175. //
  176. //--------------------------------------------------------------------------
  177. void CMmIStream::Unmap ( CMmStreamBuf& sbuf )
  178. {
  179. Win4Assert( sbuf.Get() == _pBuf );
  180. sbuf.SetBuf( 0 );
  181. sbuf.SetSize( 0 );
  182. delete [] _pBuf;
  183. _pBuf = 0;
  184. }
  185. //+-------------------------------------------------------------------------
  186. //
  187. // Member: CMmIStream::Flush, public
  188. //
  189. // Synopsis: Flush stream
  190. //
  191. // Arguments: [sbuf] -- Stream buffer to flush
  192. // [cb] -- # bytes to flush
  193. //
  194. // History: 11-Feb-97 KyleP Created
  195. //
  196. // Notes: Only r/o streams supported. Flush has no meaning.
  197. //
  198. //--------------------------------------------------------------------------
  199. void CMmIStream::Flush ( CMmStreamBuf& sbuf, ULONG cb, BOOL fThrowOnFailure )
  200. {
  201. Win4Assert( !"Not implemented" );
  202. THROW( CException( E_FAIL ) );
  203. }
  204. //+-------------------------------------------------------------------------
  205. //
  206. // Member: CMmIStream::FlushMetaData, public
  207. //
  208. // Synopsis: Flush stream
  209. //
  210. // Arguments: [sbuf] -- Stream buffer to flush
  211. // [cb] -- # bytes to flush
  212. //
  213. // History: 11-Feb-97 KyleP Created
  214. //
  215. // Notes: Only r/o streams supported. Flush has no meaning.
  216. //
  217. //--------------------------------------------------------------------------
  218. void CMmIStream::FlushMetaData( BOOL fThrowOnFailure )
  219. {
  220. Win4Assert( !"Not implemented" );
  221. THROW( CException( E_FAIL ) );
  222. }