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.

321 lines
7.5 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) 1998-1998 Microsoft Corporation
  6. //
  7. // File: aariff.h
  8. //
  9. //--------------------------------------------------------------------------
  10. //
  11. // aariff.h
  12. //
  13. #include <objbase.h>
  14. #ifndef __AARIFF__
  15. #define __AARIFF__
  16. #include <windows.h>
  17. #include <mmsystem.h>
  18. #define FixBytes(a1,a2)
  19. // {0D5057E1-8889-11CF-B9DA-00AA00C08146}
  20. DEFINE_GUID( IID_IAARIFFStream, 0xd5057e1, 0x8889, 0x11cf, 0xb9, 0xda, 0x0, 0xaa, 0x0, 0xc0, 0x81, 0x46 );
  21. #undef INTERFACE
  22. #define INTERFACE IAARIFFStream
  23. DECLARE_INTERFACE_(IAARIFFStream, IUnknown)
  24. {
  25. // IUnknown members
  26. STDMETHOD(QueryInterface)(THIS_ REFIID riid, LPVOID *ppv) PURE;
  27. STDMETHOD_(ULONG, AddRef)(THIS) PURE;
  28. STDMETHOD_(ULONG, Release)(THIS) PURE;
  29. // IMKRIFFStream members
  30. STDMETHOD_(UINT, Descend)(LPMMCKINFO lpck, LPMMCKINFO lpckParent, UINT wFlags) PURE;
  31. STDMETHOD_(UINT, Ascend)(LPMMCKINFO lpck, UINT wFlags) PURE;
  32. STDMETHOD_(UINT, CreateChunk)(LPMMCKINFO lpck, UINT wFlags) PURE;
  33. STDMETHOD(SetStream)(LPSTREAM pStream) PURE;
  34. STDMETHOD_(LPSTREAM, GetStream)() PURE;
  35. };
  36. struct CFileStream : IStream
  37. {
  38. ///// object state
  39. ULONG m_cRef; // object reference count
  40. HANDLE m_hfile; // file handle
  41. // construction and destruction
  42. CFileStream( HANDLE hfile ) : m_cRef( 1 ), m_hfile( hfile ) {}
  43. #ifdef _MAC
  44. ~CFileStream() { FSClose( (int) m_hfile ); }
  45. #else
  46. ~CFileStream() { CloseHandle( m_hfile ); }
  47. #endif
  48. ///// IUnknown methods
  49. STDMETHODIMP QueryInterface( REFIID riid, LPVOID FAR* ppvObj )
  50. {
  51. if( IsEqualIID( riid, IID_IUnknown ) ||
  52. IsEqualIID( riid, IID_IStream ) )
  53. {
  54. *ppvObj = (IStream *) this;
  55. AddRef();
  56. return NOERROR;
  57. }
  58. *ppvObj = NULL;
  59. return E_NOINTERFACE;
  60. }
  61. STDMETHODIMP_(ULONG) AddRef()
  62. {
  63. return ++m_cRef;
  64. }
  65. STDMETHODIMP_(ULONG) Release()
  66. {
  67. if( --m_cRef == 0L )
  68. {
  69. delete this;
  70. return 0;
  71. }
  72. return m_cRef;
  73. }
  74. /* IStream methods */
  75. STDMETHODIMP Read( void* pv, ULONG cb, ULONG* pcbRead )
  76. {
  77. DWORD dw;
  78. if( ReadFile( m_hfile, pv, cb, &dw, NULL ) &&
  79. dw == cb )
  80. {
  81. if( pcbRead != NULL )
  82. {
  83. *pcbRead = dw;
  84. }
  85. return S_OK;
  86. }
  87. return E_FAIL;
  88. }
  89. STDMETHODIMP Write( const void* pv, ULONG cb, ULONG* pcbWritten )
  90. {
  91. DWORD dw;
  92. if( WriteFile( m_hfile, pv, cb, &dw, NULL ) &&
  93. dw == cb )
  94. {
  95. if( pcbWritten != NULL )
  96. {
  97. *pcbWritten = dw;
  98. }
  99. return S_OK;
  100. }
  101. return E_FAIL;
  102. }
  103. STDMETHODIMP Seek( LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition )
  104. {
  105. DWORD dw;
  106. dw = SetFilePointer( m_hfile, dlibMove.LowPart, &dlibMove.HighPart, dwOrigin );
  107. if( dw == (DWORD)-1 )
  108. {
  109. return E_FAIL;
  110. }
  111. if( plibNewPosition != NULL )
  112. {
  113. plibNewPosition->LowPart = dw;
  114. plibNewPosition->HighPart = dlibMove.HighPart;
  115. }
  116. return S_OK;
  117. }
  118. STDMETHODIMP SetSize( ULARGE_INTEGER /*libNewSize*/ )
  119. { return E_NOTIMPL; }
  120. STDMETHODIMP CopyTo( IStream* /*pstm */, ULARGE_INTEGER /*cb*/,
  121. ULARGE_INTEGER* /*pcbRead*/,
  122. ULARGE_INTEGER* /*pcbWritten*/ )
  123. { return E_NOTIMPL; }
  124. STDMETHODIMP Commit( DWORD /*grfCommitFlags*/ )
  125. { return E_NOTIMPL; }
  126. STDMETHODIMP Revert()
  127. { return E_NOTIMPL; }
  128. STDMETHODIMP LockRegion( ULARGE_INTEGER /*libOffset*/, ULARGE_INTEGER /*cb*/,
  129. DWORD /*dwLockType*/ )
  130. { return E_NOTIMPL; }
  131. STDMETHODIMP UnlockRegion( ULARGE_INTEGER /*libOffset*/, ULARGE_INTEGER /*cb*/,
  132. DWORD /*dwLockType*/)
  133. { return E_NOTIMPL; }
  134. STDMETHODIMP Stat( STATSTG* /*pstatstg*/, DWORD /*grfStatFlag*/ )
  135. { return E_NOTIMPL; }
  136. STDMETHODIMP Clone( IStream** /*ppstm*/ )
  137. { return E_NOTIMPL; }
  138. };
  139. struct CRIFFStream : IAARIFFStream
  140. {
  141. ///// object state
  142. ULONG m_cRef; // object reference count
  143. IStream* m_pStream; // stream to operate on
  144. ///// construction and destruction
  145. CRIFFStream(IStream* pStream)
  146. {
  147. m_cRef = 1;
  148. // replaced a call to SetStream with the following to avoid releasing an
  149. // unallocated stream
  150. m_pStream = pStream;
  151. if( m_pStream != NULL )
  152. {
  153. m_pStream->AddRef();
  154. }
  155. }
  156. ~CRIFFStream()
  157. {
  158. if( m_pStream != NULL )
  159. {
  160. m_pStream->Release();
  161. }
  162. }
  163. ///// IUnknown methods
  164. STDMETHODIMP QueryInterface(REFIID riid, LPVOID *ppvObj)
  165. {
  166. if( IsEqualIID( riid, IID_IUnknown ) ||
  167. IsEqualIID( riid, IID_IAARIFFStream ) )
  168. {
  169. *ppvObj = (IAARIFFStream*)this;
  170. AddRef();
  171. return NOERROR;
  172. }
  173. *ppvObj = NULL;
  174. return E_NOINTERFACE;
  175. }
  176. STDMETHODIMP_(ULONG) AddRef()
  177. {
  178. return ++m_cRef;
  179. }
  180. STDMETHODIMP_(ULONG) Release()
  181. {
  182. if( --m_cRef == 0L )
  183. {
  184. delete this;
  185. return 0;
  186. }
  187. return m_cRef;
  188. }
  189. // IAARIFFStream methods
  190. STDMETHODIMP_(UINT) Descend( LPMMCKINFO lpck, LPMMCKINFO lpckParent, UINT wFlags );
  191. STDMETHODIMP_(UINT) Ascend( LPMMCKINFO lpck, UINT wFlags );
  192. STDMETHODIMP_(UINT) CreateChunk( LPMMCKINFO lpck, UINT wFlags );
  193. STDMETHOD(SetStream)(LPSTREAM pStream)
  194. {
  195. if( m_pStream != NULL )
  196. {
  197. m_pStream->Release();
  198. }
  199. m_pStream = pStream;
  200. if( m_pStream != NULL )
  201. {
  202. m_pStream->AddRef();
  203. }
  204. return S_OK;
  205. }
  206. STDMETHOD_(LPSTREAM, GetStream)()
  207. {
  208. if( m_pStream != NULL )
  209. {
  210. m_pStream->AddRef();
  211. }
  212. return m_pStream;
  213. }
  214. // private methods
  215. long MyRead( void *pv, long cb );
  216. long MyWrite( const void *pv, long cb );
  217. long MySeek( long lOffset, int iOrigin );
  218. };
  219. // seeks to a 32-bit position in a stream.
  220. HRESULT __inline StreamSeek( LPSTREAM pStream, long lSeekTo, DWORD dwOrigin )
  221. {
  222. LARGE_INTEGER li;
  223. if( lSeekTo < 0 )
  224. {
  225. li.HighPart = -1;
  226. }
  227. else
  228. {
  229. li.HighPart = 0;
  230. }
  231. li.LowPart = lSeekTo;
  232. return pStream->Seek( li, dwOrigin, NULL );
  233. }
  234. // returns the current 32-bit position in a stream.
  235. DWORD __inline StreamTell( LPSTREAM pStream )
  236. {
  237. LARGE_INTEGER li;
  238. ULARGE_INTEGER ul;
  239. #ifdef DBG
  240. HRESULT hr;
  241. #endif
  242. li.HighPart = 0;
  243. li.LowPart = 0;
  244. #ifdef DBG
  245. hr = pStream->Seek( li, STREAM_SEEK_CUR, &ul );
  246. if( FAILED( hr ) )
  247. #else
  248. if( FAILED( pStream->Seek( li, STREAM_SEEK_CUR, &ul ) ) )
  249. #endif
  250. {
  251. return 0;
  252. }
  253. return ul.LowPart;
  254. }
  255. // this function gets a long that is formatted the correct way
  256. // i.e. the motorola way as opposed to the intel way
  257. BOOL __inline GetMLong( LPSTREAM pStream, DWORD& dw )
  258. {
  259. union uLong
  260. {
  261. unsigned char buf[4];
  262. DWORD dw;
  263. } u;
  264. unsigned char ch;
  265. if( FAILED( pStream->Read( u.buf, 4, NULL ) ) )
  266. {
  267. return FALSE;
  268. }
  269. #ifndef _MAC
  270. // swap bytes
  271. ch = u.buf[0];
  272. u.buf[0] = u.buf[3];
  273. u.buf[3] = ch;
  274. ch = u.buf[1];
  275. u.buf[1] = u.buf[2];
  276. u.buf[2] = ch;
  277. #endif
  278. dw = u.dw;
  279. return TRUE;
  280. }
  281. BOOL __inline IsGUIDZero( REFGUID guid )
  282. {
  283. GUID g;
  284. memset( &g, 0, sizeof( g ) );
  285. return IsEqualGUID( g, guid );
  286. }
  287. // misc function prototypes
  288. STDAPI AllocFileStream( LPCSTR szFileName, DWORD dwDesiredAccess, IStream **ppstream );
  289. STDAPI AllocRIFFStream( IStream* pStream, IAARIFFStream** ppRiff );
  290. #endif // __AARIFF_H__