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.

274 lines
6.7 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. STRM.H
  5. Abstract:
  6. CMemStream implementation.
  7. CMemStream implementation for WBEM.
  8. This is a thread-safe generic data stream which can be used
  9. with memory objects, pipes, mailslots, or files. This is the basic
  10. object for interface & call marshaling.
  11. a-raymcc 04-Apr-96 Created.
  12. a-raymcc 06-Jun-96 CArena support.
  13. a-raymcc 11-Sep-96 Support NULL pointers
  14. Supported types:
  15. VT_NULL
  16. VT_UI1, VT_I1, VT_UI2, VT_I2, VT_UI4, VT_I4, VT_I8, VT_UI8
  17. VT_R4, VT_R8, VT_BOOL
  18. VT_LPSTR, VT_LPWSTR, VT_BSTR
  19. VT_CLSID, VT_UNKNOWN, VT_FILETIME, VT_ERROR, VT_BLOB, VT_PTR
  20. VT_EMPTY = End of stream
  21. VT_USERDEFINED
  22. VT_EX_VAR
  23. VT_EX_VARVECTOR
  24. History:
  25. --*/
  26. #ifndef _STRM_H_
  27. #define _STRM_H_
  28. #include "corepol.h"
  29. #include <arena.h>
  30. #include <var.h>
  31. #include <wbemutil.h>
  32. #define SIGNATURE_STREAM 0x80F6A003
  33. #pragma warning(disable: 4275)
  34. class POLARITY CMemStream : public IStream
  35. {
  36. protected:
  37. // Types, constants.
  38. // =================
  39. enum { stack_size = 32 };
  40. struct STREAM_HEADER
  41. {
  42. DWORD dwSignature;
  43. DWORD dwLength;
  44. STREAM_HEADER() { dwSignature = SIGNATURE_STREAM; }
  45. BOOL Verify() { return SIGNATURE_STREAM == dwSignature; }
  46. };
  47. // Member variables.
  48. // =================
  49. int m_nStatus;
  50. DWORD m_dwSize;
  51. DWORD m_dwGrowBy;
  52. DWORD m_dwCurrentPos;
  53. DWORD m_dwEndOfStream;
  54. BYTE *m_pBuffer;
  55. int m_nFlags;
  56. DWORD m_dwStack[stack_size];
  57. int m_nStackPtr;
  58. long m_lRef;
  59. // protected functions.
  60. // ==================
  61. void Empty();
  62. int Resize(DWORD dwNewSize);
  63. void UpdateHdr() { ((STREAM_HEADER *)m_pBuffer)->dwLength = m_dwEndOfStream; }
  64. public:
  65. enum { no_error, failed, type_mismatch, end_of_stream,
  66. out_of_memory, critical_error, invalid_parameter , timeout };
  67. enum { auto_delete, no_delete };
  68. CMemStream(
  69. int nFlags = auto_delete,
  70. CArena *pArena = 0,
  71. int nInitialSize = 512,
  72. int nGrowBy = 512
  73. );
  74. CMemStream(
  75. LPVOID pBindingAddress,
  76. CArena *pArena,
  77. int nFlags = auto_delete,
  78. int nGrowBy = 512
  79. );
  80. CMemStream(CMemStream &Src);
  81. CMemStream &operator =(CMemStream &Src);
  82. ~CMemStream();
  83. // Releases the arena
  84. void Push() { m_dwStack[++m_nStackPtr] = m_dwCurrentPos; }
  85. void Pop(BOOL bDiscard);
  86. BOOL FreeMemory(void *pBlock) { return free(pBlock); }
  87. void Unbind() { m_nFlags = no_delete; }
  88. int Append(CMemStream *pSrc);
  89. int Deserialize(HANDLE hFile);
  90. int Deserialize(FILE *fStream);
  91. int Deserialize(LPBYTE pBlock, DWORD dwSize);
  92. int Serialize(HANDLE hFile);
  93. int Serialize(FILE *fStream);
  94. int Serialize(BYTE **pBlock, DWORD *pdwSize, CArena *pArena = 0);
  95. // Use HeapFree() if no arena present
  96. void Trim() { UpdateHdr(); Resize(m_dwEndOfStream); } // Reduce excess of internal block.
  97. int Status() { return m_nStatus; }
  98. DWORD GetCurrentPos() { return m_dwCurrentPos; }
  99. void SetCurrentPos(DWORD dwPos) { m_dwCurrentPos = dwPos; }
  100. DWORD Size() { return m_dwEndOfStream; }
  101. DWORD BufferSize() { return m_dwSize; }
  102. LPVOID GetPtr() { UpdateHdr(); return m_pBuffer; }
  103. void Reset() { m_dwCurrentPos = sizeof(STREAM_HEADER); }
  104. int NextType();
  105. int WriteType(DWORD dwType) { return WriteBytes(&dwType, sizeof(DWORD)); }
  106. // Write operations.
  107. // ==================
  108. int WriteBytes(LPVOID, DWORD);
  109. int WriteNull() { return WriteType(VT_NULL); }
  110. int WriteChar(IN char c);
  111. int WriteByte(IN BYTE b);
  112. int WriteShort(IN SHORT iVal);
  113. int WriteWORD(IN WORD wVal);
  114. int WriteLong(IN LONG l);
  115. int WriteDWORD(IN DWORD dwVal);
  116. int WriteFloat(IN float fltVal);
  117. int WriteDouble(IN double dblVal);
  118. int WriteBool(IN VARIANT_BOOL b);
  119. int WriteLPSTR(IN LPSTR pStr);
  120. int WriteLPWSTR(IN LPWSTR pStr);
  121. int WriteBSTR(IN BSTR pStr);
  122. int WriteCLSID(IN CLSID *pClsId);
  123. int WriteUnknown(IN IUnknown *pObj);
  124. int WriteFILETIME(IN FILETIME *pTime);
  125. int WriteError(IN SCODE sVal);
  126. int WriteBlob(IN BLOB *pBlob);
  127. int WritePtr(IN LPVOID p);
  128. int WriteCVar(IN CVar *pObj);
  129. int WriteCVarVector(IN CVarVector *pObj);
  130. // Read operations.
  131. // ================
  132. int ReadBytes(LPVOID, DWORD);
  133. int ReadNull();
  134. int ReadByte(OUT BYTE *pByte);
  135. int ReadChar(OUT char *pc);
  136. int ReadShort(OUT SHORT *piVal);
  137. int ReadWORD(OUT WORD *pwVal);
  138. int ReadLong(OUT LONG *plVal);
  139. int ReadDWORD(OUT DWORD *pdwVal);
  140. int ReadFloat(OUT float *pfltVal);
  141. int ReadDouble(OUT double *pdlbVal);
  142. int ReadBool(OUT VARIANT_BOOL *pBool);
  143. int ReadLPSTR(OUT LPSTR *pStr);
  144. int ReadLPWSTR(OUT LPWSTR *pStr);
  145. int ReadBSTR(OUT BSTR *pStr);
  146. int ReadCLSID(OUT CLSID *pClsId);
  147. int ReadUnknown(IUnknown **pObj);
  148. int ReadFILETIME(OUT FILETIME *pTime);
  149. int ReadError(OUT SCODE *pVal);
  150. int ReadBlob(OUT BLOB *pBlob);
  151. int ReadPtr(OUT LPVOID *p);
  152. int ReadCVar(OUT CVar **pObj);
  153. int ReadCVarVector(OUT CVarVector **pObj);
  154. int ReadType();
  155. BOOL EndOfStream() { return m_dwCurrentPos == m_dwEndOfStream; }
  156. // IStream implementation
  157. STDMETHOD_(ULONG, AddRef)()
  158. {
  159. return InterlockedIncrement(&m_lRef);
  160. }
  161. STDMETHOD_(ULONG, Release)()
  162. {
  163. long lRef = InterlockedDecrement(&m_lRef);
  164. if(lRef == 0) delete this;
  165. return lRef;
  166. }
  167. STDMETHOD(QueryInterface)(REFIID riid, void** ppv);
  168. STDMETHOD(Read)(
  169. void *pv,
  170. ULONG cb,
  171. ULONG *pcbRead);
  172. STDMETHOD(Write)(
  173. const void *pv,
  174. ULONG cb,
  175. ULONG *pcbWritten);
  176. STDMETHOD(Seek)(
  177. LARGE_INTEGER dlibMove,
  178. DWORD dwOrigin,
  179. ULARGE_INTEGER *plibNewPosition);
  180. STDMETHOD(SetSize)(
  181. ULARGE_INTEGER libNewSize);
  182. STDMETHOD(CopyTo)(
  183. IStream *pstm,
  184. ULARGE_INTEGER cb,
  185. ULARGE_INTEGER *pcbRead,
  186. ULARGE_INTEGER *pcbWritten);
  187. STDMETHOD(Commit)(
  188. DWORD grfCommitFlags);
  189. STDMETHOD(Revert)( void);
  190. STDMETHOD(LockRegion)(
  191. ULARGE_INTEGER libOffset,
  192. ULARGE_INTEGER cb,
  193. DWORD dwLockType);
  194. STDMETHOD(UnlockRegion)(
  195. ULARGE_INTEGER libOffset,
  196. ULARGE_INTEGER cb,
  197. DWORD dwLockType);
  198. STDMETHOD(Stat)(
  199. STATSTG *pstatstg,
  200. DWORD grfStatFlag);
  201. STDMETHOD(Clone)(
  202. IStream **ppstm);
  203. };
  204. #endif