FileIOHelper.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // FileIOHelper.h //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Provides utitlity functions to work with files. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. #include "Global.h"
  13. #ifndef _ATL_DECLSPEC_ALLOCATOR
  14. #define _ATL_DECLSPEC_ALLOCATOR
  15. #endif
  16. // Forward declarations.
  17. struct IDxcBlob;
  18. struct IDxcBlobEncoding;
  19. struct IDxcBlobUtf8;
  20. struct IDxcBlobUtf16;
  21. namespace hlsl {
  22. IMalloc *GetGlobalHeapMalloc() throw();
  23. class CDxcThreadMallocAllocator {
  24. public:
  25. _Ret_maybenull_ _Post_writable_byte_size_(nBytes) _ATL_DECLSPEC_ALLOCATOR
  26. static void *Reallocate(_In_ void *p, _In_ size_t nBytes) throw() {
  27. return DxcGetThreadMallocNoRef()->Realloc(p, nBytes);
  28. }
  29. _Ret_maybenull_ _Post_writable_byte_size_(nBytes) _ATL_DECLSPEC_ALLOCATOR
  30. static void *Allocate(_In_ size_t nBytes) throw() {
  31. return DxcGetThreadMallocNoRef()->Alloc(nBytes);
  32. }
  33. static void Free(_In_ void *p) throw() {
  34. return DxcGetThreadMallocNoRef()->Free(p);
  35. }
  36. };
  37. // Like CComHeapPtr, but with CDxcThreadMallocAllocator.
  38. template <typename T>
  39. class CDxcTMHeapPtr :
  40. public CHeapPtr<T, CDxcThreadMallocAllocator>
  41. {
  42. public:
  43. CDxcTMHeapPtr() throw()
  44. {
  45. }
  46. explicit CDxcTMHeapPtr(_In_ T* pData) throw() :
  47. CHeapPtr<T, CDxcThreadMallocAllocator>(pData)
  48. {
  49. }
  50. };
  51. // Like CComHeapPtr, but with a stateful allocator.
  52. template <typename T>
  53. class CDxcMallocHeapPtr
  54. {
  55. private:
  56. CComPtr<IMalloc> m_pMalloc;
  57. public:
  58. T *m_pData;
  59. CDxcMallocHeapPtr(IMalloc *pMalloc) throw()
  60. : m_pMalloc(pMalloc), m_pData(nullptr) {}
  61. ~CDxcMallocHeapPtr() {
  62. if (m_pData)
  63. m_pMalloc->Free(m_pData);
  64. }
  65. operator T *() const throw() { return m_pData; }
  66. IMalloc* GetMallocNoRef() const throw() { return m_pMalloc.p; }
  67. bool Allocate(_In_ SIZE_T ElementCount) throw() {
  68. ATLASSERT(m_pData == NULL);
  69. SIZE_T nBytes = ElementCount * sizeof(T);
  70. m_pData = static_cast<T *>(m_pMalloc->Alloc(nBytes));
  71. if (m_pData == NULL)
  72. return false;
  73. return true;
  74. }
  75. void AllocateBytes(_In_ SIZE_T ByteCount) throw() {
  76. if (m_pData)
  77. m_pMalloc->Free(m_pData);
  78. m_pData = static_cast<T *>(m_pMalloc->Alloc(ByteCount));
  79. }
  80. // Attach to an existing pointer (takes ownership)
  81. void Attach(_In_ T *pData) throw() {
  82. m_pMalloc->Free(m_pData);
  83. m_pData = pData;
  84. }
  85. // Detach the pointer (releases ownership)
  86. T *Detach() throw() {
  87. T *pTemp = m_pData;
  88. m_pData = NULL;
  89. return pTemp;
  90. }
  91. // Free the memory pointed to, and set the pointer to NULL
  92. void Free() throw() {
  93. m_pMalloc->Free(m_pData);
  94. m_pData = NULL;
  95. }
  96. };
  97. void ReadBinaryFile(_In_opt_ IMalloc *pMalloc,
  98. _In_z_ LPCWSTR pFileName,
  99. _Outptr_result_bytebuffer_(*pDataSize) void **ppData,
  100. _Out_ DWORD *pDataSize);
  101. void ReadBinaryFile(_In_z_ LPCWSTR pFileName,
  102. _Outptr_result_bytebuffer_(*pDataSize) void **ppData,
  103. _Out_ DWORD *pDataSize);
  104. void WriteBinaryFile(_In_z_ LPCWSTR pFileName,
  105. _In_reads_bytes_(DataSize) const void *pData,
  106. _In_ DWORD DataSize);
  107. ///////////////////////////////////////////////////////////////////////////////
  108. // Blob and encoding manipulation functions.
  109. UINT32 DxcCodePageFromBytes(_In_count_(byteLen) const char *bytes,
  110. size_t byteLen) throw();
  111. // More general create blob functions, used by other functions
  112. // Null pMalloc means use current thread malloc.
  113. // bPinned will point to existing memory without managing it;
  114. // bCopy will copy to heap; bPinned and bCopy are mutually exclusive.
  115. // If encodingKnown, UTF-8 or UTF-16, and null-termination possible,
  116. // an IDxcBlobUtf8 or IDxcBlobUtf16 will be constructed.
  117. // If text, it's best if size includes null terminator when not copying,
  118. // otherwise IDxcBlobUtf8 or IDxcBlobUtf16 will not be constructed.
  119. HRESULT DxcCreateBlob(
  120. LPCVOID pPtr, SIZE_T size, bool bPinned, bool bCopy,
  121. bool encodingKnown, UINT32 codePage,
  122. IMalloc *pMalloc, IDxcBlobEncoding **ppBlobEncoding) throw();
  123. // Create from blob references original blob.
  124. // Pass nonzero for offset or length for sub-blob reference.
  125. HRESULT DxcCreateBlobEncodingFromBlob(
  126. IDxcBlob *pFromBlob, UINT32 offset, UINT32 length,
  127. bool encodingKnown, UINT32 codePage,
  128. IMalloc *pMalloc, IDxcBlobEncoding **ppBlobEncoding) throw();
  129. // Load files
  130. HRESULT
  131. DxcCreateBlobFromFile(_In_opt_ IMalloc *pMalloc, LPCWSTR pFileName,
  132. _In_opt_ UINT32 *pCodePage,
  133. _COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
  134. HRESULT DxcCreateBlobFromFile(LPCWSTR pFileName, _In_opt_ UINT32 *pCodePage,
  135. _COM_Outptr_ IDxcBlobEncoding **ppBlobEncoding) throw();
  136. // Given a blob, creates a subrange view.
  137. HRESULT DxcCreateBlobFromBlob(_In_ IDxcBlob *pBlob, UINT32 offset,
  138. UINT32 length,
  139. _COM_Outptr_ IDxcBlob **ppResult) throw();
  140. // Creates a blob wrapping a buffer to be freed with the provided IMalloc
  141. HRESULT
  142. DxcCreateBlobOnMalloc(_In_bytecount_(size) LPCVOID pData, _In_ IMalloc* pIMalloc,
  143. UINT32 size, _COM_Outptr_ IDxcBlob **ppResult) throw();
  144. // Creates a blob with a copy of the provided data
  145. HRESULT
  146. DxcCreateBlobOnHeapCopy(_In_bytecount_(size) LPCVOID pData, UINT32 size,
  147. _COM_Outptr_ IDxcBlob **ppResult) throw();
  148. // Given a blob, creates a new instance with a specific code page set.
  149. HRESULT
  150. DxcCreateBlobWithEncodingSet(_In_ IDxcBlob *pBlob, UINT32 codePage,
  151. _COM_Outptr_ IDxcBlobEncoding **ppBlobEncoding) throw();
  152. HRESULT
  153. DxcCreateBlobWithEncodingSet(
  154. _In_ IMalloc *pMalloc, _In_ IDxcBlob *pBlob, UINT32 codePage,
  155. _COM_Outptr_ IDxcBlobEncoding **ppBlobEncoding) throw();
  156. // Creates a blob around encoded text without ownership transfer
  157. HRESULT DxcCreateBlobWithEncodingFromPinned(
  158. _In_bytecount_(size) LPCVOID pText, UINT32 size, UINT32 codePage,
  159. _COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
  160. HRESULT
  161. DxcCreateBlobWithEncodingFromStream(
  162. IStream *pStream, bool newInstanceAlways, UINT32 codePage,
  163. _COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
  164. // Creates a blob with a copy of the encoded text
  165. HRESULT
  166. DxcCreateBlobWithEncodingOnHeapCopy(
  167. _In_bytecount_(size) LPCVOID pText, UINT32 size, UINT32 codePage,
  168. _COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
  169. // Creates a blob wrapping encoded text to be freed with the provided IMalloc
  170. HRESULT
  171. DxcCreateBlobWithEncodingOnMalloc(
  172. _In_bytecount_(size) LPCVOID pText, _In_ IMalloc *pIMalloc, UINT32 size, UINT32 codePage,
  173. _COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
  174. // Creates a blob with a copy of encoded text, allocated using the provided IMalloc
  175. HRESULT
  176. DxcCreateBlobWithEncodingOnMallocCopy(
  177. _In_ IMalloc *pIMalloc, _In_bytecount_(size) LPCVOID pText, UINT32 size, UINT32 codePage,
  178. _COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
  179. HRESULT DxcGetBlobAsUtf8(_In_ IDxcBlob *pBlob, _In_ IMalloc *pMalloc,
  180. _COM_Outptr_ IDxcBlobUtf8 **pBlobEncoding) throw();
  181. HRESULT
  182. DxcGetBlobAsUtf16(_In_ IDxcBlob *pBlob, _In_ IMalloc *pMalloc,
  183. _COM_Outptr_ IDxcBlobUtf16 **pBlobEncoding) throw();
  184. bool IsBlobNullOrEmpty(_In_opt_ IDxcBlob *pBlob) throw();
  185. ///////////////////////////////////////////////////////////////////////////////
  186. // Stream implementations.
  187. class AbstractMemoryStream : public IStream {
  188. public:
  189. virtual LPBYTE GetPtr() throw() = 0;
  190. virtual ULONG GetPtrSize() throw() = 0;
  191. virtual LPBYTE Detach() throw() = 0;
  192. virtual UINT64 GetPosition() throw() = 0;
  193. virtual HRESULT Reserve(ULONG targetSize) throw() = 0;
  194. };
  195. HRESULT CreateMemoryStream(_In_ IMalloc *pMalloc, _COM_Outptr_ AbstractMemoryStream** ppResult) throw();
  196. HRESULT CreateReadOnlyBlobStream(_In_ IDxcBlob *pSource, _COM_Outptr_ IStream** ppResult) throw();
  197. template <typename T>
  198. HRESULT WriteStreamValue(AbstractMemoryStream *pStream, const T& value) {
  199. ULONG cb;
  200. return pStream->Write(&value, sizeof(value), &cb);
  201. }
  202. } // namespace hlsl