GPStream.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Security.Permissions;
  6. using HandyControl.Tools.Interop;
  7. namespace HandyControl.Data;
  8. // ReSharper disable once InconsistentNaming
  9. internal class GPStream : InteropValues.IStream
  10. {
  11. protected Stream DataStream;
  12. // to support seeking ahead of the stream length...
  13. long _virtualPosition = -1;
  14. internal GPStream(Stream stream)
  15. {
  16. if (!stream.CanSeek)
  17. {
  18. const int readBlock = 256;
  19. var bytes = new byte[readBlock];
  20. int readLen;
  21. var current = 0;
  22. do
  23. {
  24. if (bytes.Length < current + readBlock)
  25. {
  26. var newData = new byte[bytes.Length * 2];
  27. Array.Copy(bytes, newData, bytes.Length);
  28. bytes = newData;
  29. }
  30. readLen = stream.Read(bytes, current, readBlock);
  31. current += readLen;
  32. } while (readLen != 0);
  33. DataStream = new MemoryStream(bytes);
  34. }
  35. else
  36. {
  37. DataStream = stream;
  38. }
  39. }
  40. private void ActualizeVirtualPosition()
  41. {
  42. if (_virtualPosition == -1) return;
  43. if (_virtualPosition > DataStream.Length)
  44. DataStream.SetLength(_virtualPosition);
  45. DataStream.Position = _virtualPosition;
  46. _virtualPosition = -1;
  47. }
  48. public virtual InteropValues.IStream Clone()
  49. {
  50. NotImplemented();
  51. return null;
  52. }
  53. public virtual void Commit(int grfCommitFlags)
  54. {
  55. DataStream.Flush();
  56. // Extend the length of the file if needed.
  57. ActualizeVirtualPosition();
  58. }
  59. [
  60. UIPermission(SecurityAction.Demand, Window = UIPermissionWindow.AllWindows),
  61. SecurityPermission(SecurityAction.Assert, Flags = SecurityPermissionFlag.UnmanagedCode)
  62. ]
  63. public virtual long CopyTo(InteropValues.IStream pstm, long cb, long[] pcbRead)
  64. {
  65. const int bufsize = 4096;
  66. var buffer = Marshal.AllocHGlobal(bufsize);
  67. if (buffer == IntPtr.Zero) throw new OutOfMemoryException();
  68. long written = 0;
  69. try
  70. {
  71. while (written < cb)
  72. {
  73. var toRead = bufsize;
  74. if (written + toRead > cb) toRead = (int) (cb - written);
  75. var read = Read(buffer, toRead);
  76. if (read == 0) break;
  77. if (pstm.Write(buffer, read) != read)
  78. {
  79. throw EFail("Wrote an incorrect number of bytes");
  80. }
  81. written += read;
  82. }
  83. }
  84. finally
  85. {
  86. Marshal.FreeHGlobal(buffer);
  87. }
  88. if (pcbRead != null && pcbRead.Length > 0)
  89. {
  90. pcbRead[0] = written;
  91. }
  92. return written;
  93. }
  94. public virtual Stream GetDataStream()
  95. {
  96. return DataStream;
  97. }
  98. public virtual void LockRegion(long libOffset, long cb, int dwLockType)
  99. {
  100. }
  101. protected static ExternalException EFail(string msg)
  102. {
  103. throw new ExternalException(msg, InteropMethods.E_FAIL);
  104. }
  105. protected static void NotImplemented()
  106. {
  107. throw new ExternalException("NotImplemented");
  108. }
  109. public virtual int Read(IntPtr buf, int length)
  110. {
  111. var buffer = new byte[length];
  112. var count = Read(buffer, length);
  113. Marshal.Copy(buffer, 0, buf, length);
  114. return count;
  115. }
  116. public virtual int Read(byte[] buffer, int length)
  117. {
  118. ActualizeVirtualPosition();
  119. return DataStream.Read(buffer, 0, length);
  120. }
  121. public virtual void Revert()
  122. {
  123. NotImplemented();
  124. }
  125. public virtual long Seek(long offset, int origin)
  126. {
  127. var pos = _virtualPosition;
  128. if (_virtualPosition == -1)
  129. {
  130. pos = DataStream.Position;
  131. }
  132. var len = DataStream.Length;
  133. switch (origin)
  134. {
  135. case InteropValues.StreamConsts.STREAM_SEEK_SET:
  136. if (offset <= len)
  137. {
  138. DataStream.Position = offset;
  139. _virtualPosition = -1;
  140. }
  141. else
  142. {
  143. _virtualPosition = offset;
  144. }
  145. break;
  146. case InteropValues.StreamConsts.STREAM_SEEK_END:
  147. if (offset <= 0)
  148. {
  149. DataStream.Position = len + offset;
  150. _virtualPosition = -1;
  151. }
  152. else
  153. {
  154. _virtualPosition = len + offset;
  155. }
  156. break;
  157. case InteropValues.StreamConsts.STREAM_SEEK_CUR:
  158. if (offset + pos <= len)
  159. {
  160. DataStream.Position = pos + offset;
  161. _virtualPosition = -1;
  162. }
  163. else
  164. {
  165. _virtualPosition = offset + pos;
  166. }
  167. break;
  168. }
  169. return _virtualPosition != -1 ? _virtualPosition : DataStream.Position;
  170. }
  171. public virtual void SetSize(long value)
  172. {
  173. DataStream.SetLength(value);
  174. }
  175. public void Stat(IntPtr pstatstg, int grfStatFlag)
  176. {
  177. var stats = new STATSTG { cbSize = DataStream.Length };
  178. Marshal.StructureToPtr(stats, pstatstg, true);
  179. }
  180. public virtual void UnlockRegion(long libOffset, long cb, int dwLockType)
  181. {
  182. }
  183. public virtual int Write(IntPtr buf, int length)
  184. {
  185. var buffer = new byte[length];
  186. Marshal.Copy(buf, buffer, 0, length);
  187. return Write(buffer, length);
  188. }
  189. public virtual int Write(byte[] buffer, int length)
  190. {
  191. ActualizeVirtualPosition();
  192. DataStream.Write(buffer, 0, length);
  193. return length;
  194. }
  195. [StructLayout(LayoutKind.Sequential)]
  196. [SuppressMessage("ReSharper", "InconsistentNaming")]
  197. public class STATSTG
  198. {
  199. [SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
  200. public IntPtr pwcsName = IntPtr.Zero;
  201. public int type;
  202. [MarshalAs(UnmanagedType.I8)]
  203. public long cbSize;
  204. [MarshalAs(UnmanagedType.I8)]
  205. public long mtime;
  206. [MarshalAs(UnmanagedType.I8)]
  207. public long ctime;
  208. [MarshalAs(UnmanagedType.I8)]
  209. public long atime;
  210. [MarshalAs(UnmanagedType.I4)]
  211. public int grfMode;
  212. [MarshalAs(UnmanagedType.I4)]
  213. public int grfLocksSupported;
  214. public int clsid_data1;
  215. [MarshalAs(UnmanagedType.I2)]
  216. public short clsid_data2;
  217. [MarshalAs(UnmanagedType.I2)]
  218. public short clsid_data3;
  219. [MarshalAs(UnmanagedType.U1)]
  220. public byte clsid_b0;
  221. [MarshalAs(UnmanagedType.U1)]
  222. public byte clsid_b1;
  223. [MarshalAs(UnmanagedType.U1)]
  224. public byte clsid_b2;
  225. [MarshalAs(UnmanagedType.U1)]
  226. public byte clsid_b3;
  227. [MarshalAs(UnmanagedType.U1)]
  228. public byte clsid_b4;
  229. [MarshalAs(UnmanagedType.U1)]
  230. public byte clsid_b5;
  231. [MarshalAs(UnmanagedType.U1)]
  232. public byte clsid_b6;
  233. [MarshalAs(UnmanagedType.U1)]
  234. public byte clsid_b7;
  235. [MarshalAs(UnmanagedType.I4)]
  236. public int grfStateBits;
  237. [MarshalAs(UnmanagedType.I4)]
  238. public int reserved;
  239. }
  240. }