2
0

SshDataStream.cs 9.4 KB

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