SshDataStream.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Text;
  5. namespace Renci.SshNet.Common
  6. {
  7. public class SshDataStream : MemoryStream
  8. {
  9. public SshDataStream(int capacity)
  10. : base(capacity)
  11. {
  12. }
  13. public SshDataStream(byte[] buffer)
  14. : base(buffer)
  15. {
  16. }
  17. /// <summary>
  18. /// Gets a value indicating whether all data from the SSH data stream has been read.
  19. /// </summary>
  20. /// <value>
  21. /// <c>true</c> if this instance is end of data; otherwise, <c>false</c>.
  22. /// </value>
  23. public bool IsEndOfData
  24. {
  25. get
  26. {
  27. return Position >= Length;
  28. }
  29. }
  30. /// <summary>
  31. /// Writes an <see cref="uint"/> to the SSH data stream.
  32. /// </summary>
  33. /// <param name="value"><see cref="uint"/> data to write.</param>
  34. public void Write(uint value)
  35. {
  36. var bytes = value.GetBytes();
  37. Write(bytes, 0, bytes.Length);
  38. }
  39. /// <summary>
  40. /// Writes an <see cref="ulong"/> to the SSH data stream.
  41. /// </summary>
  42. /// <param name="value"><see cref="ulong"/> data to write.</param>
  43. public void Write(ulong value)
  44. {
  45. var bytes = value.GetBytes();
  46. Write(bytes, 0, bytes.Length);
  47. }
  48. /// <summary>
  49. /// Writes a <see cref="BigInteger"/> into the SSH data stream.
  50. /// </summary>
  51. /// <param name="data">The <see cref="BigInteger" /> to write.</param>
  52. public void Write(BigInteger data)
  53. {
  54. var bytes = data.ToByteArray().Reverse();
  55. WriteBinary(bytes, 0, bytes.Length);
  56. }
  57. /// <summary>
  58. /// Writes bytes array data into the SSH data stream.
  59. /// </summary>
  60. /// <param name="data">Byte array data to write.</param>
  61. /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception>
  62. public void Write(byte[] data)
  63. {
  64. if (data == null)
  65. throw new ArgumentNullException("data");
  66. Write(data, 0, data.Length);
  67. }
  68. /// <summary>
  69. /// Reads a byte array from the SSH data stream.
  70. /// </summary>
  71. /// <returns>
  72. /// The byte array read from the SSH data stream.
  73. /// </returns>
  74. public byte[] ReadBinary()
  75. {
  76. var length = ReadUInt32();
  77. if (length > int.MaxValue)
  78. {
  79. throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Data longer than {0} is not supported.", int.MaxValue));
  80. }
  81. return ReadBytes((int)length);
  82. }
  83. /// <summary>
  84. /// Writes a buffer preceded by its length into the SSH data stream.
  85. /// </summary>
  86. /// <param name="buffer">The data to write.</param>
  87. /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
  88. public void WriteBinary(byte[] buffer)
  89. {
  90. if (buffer == null)
  91. throw new ArgumentNullException("buffer");
  92. WriteBinary(buffer, 0, buffer.Length);
  93. }
  94. /// <summary>
  95. /// Writes a buffer preceded by its length into the SSH data stream.
  96. /// </summary>
  97. /// <param name="buffer">An array of bytes. This method write <paramref name="count"/> bytes from buffer to the current SSH data stream.</param>
  98. /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin writing bytes to the SSH data stream.</param>
  99. /// <param name="count">The number of bytes to be written to the current SSH data stream.</param>
  100. /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
  101. /// <exception cref="ArgumentException">The sum of <paramref name="offset"/> and <paramref name="count"/> is greater than the buffer length.</exception>
  102. /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="count"/> is negative.</exception>
  103. public void WriteBinary(byte[] buffer, int offset, int count)
  104. {
  105. Write((uint) count);
  106. Write(buffer, offset, count);
  107. }
  108. /// <summary>
  109. /// Writes string data to the SSH data stream using the specified encoding.
  110. /// </summary>
  111. /// <param name="s">The string data to write.</param>
  112. /// <param name="encoding">The character encoding to use.</param>
  113. /// <exception cref="ArgumentNullException"><paramref name="s"/> is null.</exception>
  114. /// <exception cref="ArgumentNullException"><paramref name="encoding"/> is null.</exception>
  115. public void Write(string s, Encoding encoding)
  116. {
  117. if (encoding == null)
  118. throw new ArgumentNullException("encoding");
  119. var bytes = encoding.GetBytes(s);
  120. WriteBinary(bytes, 0, bytes.Length);
  121. }
  122. /// <summary>
  123. /// Reads a <see cref="BigInteger"/> from the SSH datastream.
  124. /// </summary>
  125. /// <returns>
  126. /// The <see cref="BigInteger"/> read from the SSH data stream.
  127. /// </returns>
  128. public BigInteger ReadBigInt()
  129. {
  130. var length = ReadUInt32();
  131. var data = ReadBytes((int) length);
  132. return new BigInteger(data.Reverse());
  133. }
  134. /// <summary>
  135. /// Reads the next <see cref="uint"/> data type from the SSH data stream.
  136. /// </summary>
  137. /// <returns>
  138. /// The <see cref="uint"/> read from the SSH data stream.
  139. /// </returns>
  140. public uint ReadUInt32()
  141. {
  142. var data = ReadBytes(4);
  143. return (uint) (data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]);
  144. }
  145. /// <summary>
  146. /// Reads the next <see cref="ulong"/> data type from the SSH data stream.
  147. /// </summary>
  148. /// <returns>
  149. /// The <see cref="ulong"/> read from the SSH data stream.
  150. /// </returns>
  151. public ulong ReadUInt64()
  152. {
  153. var data = ReadBytes(8);
  154. return ((ulong) data[0] << 56 | (ulong) data[1] << 48 | (ulong) data[2] << 40 | (ulong) data[3] << 32 |
  155. (ulong) data[4] << 24 | (ulong) data[5] << 16 | (ulong) data[6] << 8 | data[7]);
  156. }
  157. /// <summary>
  158. /// Reads the next <see cref="string"/> data type from the SSH data stream.
  159. /// </summary>
  160. /// <returns>
  161. /// The <see cref="string"/> read from the SSH data stream.
  162. /// </returns>
  163. public string ReadString(Encoding encoding)
  164. {
  165. var length = ReadUInt32();
  166. if (length > int.MaxValue)
  167. {
  168. throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Strings longer than {0} is not supported.", int.MaxValue));
  169. }
  170. var bytes = ReadBytes((int) length);
  171. return encoding.GetString(bytes, 0, bytes.Length);
  172. }
  173. /// <summary>
  174. /// Reads next specified number of bytes data type from internal buffer.
  175. /// </summary>
  176. /// <param name="length">Number of bytes to read.</param>
  177. /// <returns>An array of bytes that was read from the internal buffer.</returns>
  178. /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is greater than the internal buffer size.</exception>
  179. private byte[] ReadBytes(int length)
  180. {
  181. var data = new byte[length];
  182. var bytesRead = Read(data, 0, length);
  183. if (bytesRead < length)
  184. throw new ArgumentOutOfRangeException("length");
  185. return data;
  186. }
  187. /// <summary>
  188. /// Writes the stream contents to a byte array, regardless of the <see cref="MemoryStream.Position"/>.
  189. /// </summary>
  190. /// <returns>
  191. /// This method returns the contents of the <see cref="SshDataStream"/> as a byte array.
  192. /// </returns>
  193. /// <remarks>
  194. /// If the current instance was constructed on a provided byte array, a copy of the section of the array
  195. /// to which this instance has access is returned.
  196. /// </remarks>
  197. public override byte[] ToArray()
  198. {
  199. if (Capacity == Length)
  200. {
  201. #if FEATURE_MEMORYSTREAM_GETBUFFER
  202. return GetBuffer();
  203. #elif FEATURE_MEMORYSTREAM_TRYGETBUFFER
  204. ArraySegment<byte> buffer;
  205. if (TryGetBuffer(out buffer))
  206. return buffer.Array;
  207. #endif
  208. }
  209. return base.ToArray();
  210. }
  211. }
  212. }