2
0

SshData.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Renci.SshNet.Common
  5. {
  6. /// <summary>
  7. /// Base ssh data serialization type.
  8. /// </summary>
  9. #pragma warning disable CA1001 // Types that own disposable fields should be disposable
  10. public abstract class SshData
  11. #pragma warning restore CA1001 // Types that own disposable fields should be disposable
  12. {
  13. internal const int DefaultCapacity = 64;
  14. internal static readonly Encoding Ascii = Encoding.ASCII;
  15. internal static readonly Encoding Utf8 = Encoding.UTF8;
  16. private SshDataStream _stream;
  17. /// <summary>
  18. /// Gets the underlying <see cref="SshDataStream"/> that is used for reading and writing SSH data.
  19. /// </summary>
  20. /// <value>
  21. /// The underlying <see cref="SshDataStream"/> that is used for reading and writing SSH data.
  22. /// </value>
  23. protected SshDataStream DataStream
  24. {
  25. get { return _stream; }
  26. }
  27. /// <summary>
  28. /// Gets a value indicating whether all data from the buffer has been read.
  29. /// </summary>
  30. /// <value>
  31. /// <see langword="true"/> if this instance is end of data; otherwise, <see langword="false"/>.
  32. /// </value>
  33. protected bool IsEndOfData
  34. {
  35. get
  36. {
  37. return _stream.Position >= _stream.Length;
  38. }
  39. }
  40. /// <summary>
  41. /// Gets the size of the message in bytes.
  42. /// </summary>
  43. /// <value>
  44. /// The size of the messages in bytes.
  45. /// </value>
  46. protected virtual int BufferCapacity
  47. {
  48. get { return 0; }
  49. }
  50. /// <summary>
  51. /// Gets data bytes array.
  52. /// </summary>
  53. /// <returns>
  54. /// A <see cref="byte"/> array representation of data structure.
  55. /// </returns>
  56. public byte[] GetBytes()
  57. {
  58. var messageLength = BufferCapacity;
  59. var capacity = messageLength != -1 ? messageLength : DefaultCapacity;
  60. using (var dataStream = new SshDataStream(capacity))
  61. {
  62. WriteBytes(dataStream);
  63. return dataStream.ToArray();
  64. }
  65. }
  66. /// <summary>
  67. /// Writes the current message to the specified <see cref="SshDataStream"/>.
  68. /// </summary>
  69. /// <param name="stream">The <see cref="SshDataStream"/> to write the message to.</param>
  70. protected virtual void WriteBytes(SshDataStream stream)
  71. {
  72. _stream = stream;
  73. SaveData();
  74. }
  75. /// <summary>
  76. /// Loads data from specified bytes.
  77. /// </summary>
  78. /// <param name="data">Bytes array.</param>
  79. /// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception>
  80. public void Load(byte[] data)
  81. {
  82. if (data is null)
  83. {
  84. throw new ArgumentNullException(nameof(data));
  85. }
  86. LoadInternal(data, 0, data.Length);
  87. }
  88. /// <summary>
  89. /// Loads data from the specified buffer.
  90. /// </summary>
  91. /// <param name="data">Bytes array.</param>
  92. /// <param name="offset">The zero-based offset in <paramref name="data"/> at which to begin reading SSH data.</param>
  93. /// <param name="count">The number of bytes to load.</param>
  94. /// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception>
  95. public void Load(byte[] data, int offset, int count)
  96. {
  97. if (data is null)
  98. {
  99. throw new ArgumentNullException(nameof(data));
  100. }
  101. LoadInternal(data, offset, count);
  102. }
  103. private void LoadInternal(byte[] value, int offset, int count)
  104. {
  105. _stream = new SshDataStream(value, offset, count);
  106. LoadData();
  107. }
  108. /// <summary>
  109. /// Called when type specific data need to be loaded.
  110. /// </summary>
  111. protected abstract void LoadData();
  112. /// <summary>
  113. /// Called when type specific data need to be saved.
  114. /// </summary>
  115. protected abstract void SaveData();
  116. /// <summary>
  117. /// Reads all data left in internal buffer at current position.
  118. /// </summary>
  119. /// <returns>
  120. /// An array of bytes containing the remaining data in the internal buffer.
  121. /// </returns>
  122. protected byte[] ReadBytes()
  123. {
  124. var bytesLength = (int)(_stream.Length - _stream.Position);
  125. var data = new byte[bytesLength];
  126. _ = _stream.Read(data, 0, bytesLength);
  127. return data;
  128. }
  129. /// <summary>
  130. /// Reads next specified number of bytes data type from internal buffer.
  131. /// </summary>
  132. /// <param name="length">Number of bytes to read.</param>
  133. /// <returns>
  134. /// An array of bytes that was read from the internal buffer.
  135. /// </returns>
  136. /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is greater than the number of bytes available to be read.</exception>
  137. protected byte[] ReadBytes(int length)
  138. {
  139. return _stream.ReadBytes(length);
  140. }
  141. /// <summary>
  142. /// Reads next byte data type from internal buffer.
  143. /// </summary>
  144. /// <returns>
  145. /// The <see cref="byte"/> read.
  146. /// </returns>
  147. /// <exception cref="InvalidOperationException">Attempt to read past the end of the stream.</exception>
  148. protected byte ReadByte()
  149. {
  150. var byteRead = _stream.ReadByte();
  151. if (byteRead == -1)
  152. {
  153. throw new InvalidOperationException("Attempt to read past the end of the SSH data stream.");
  154. }
  155. return (byte)byteRead;
  156. }
  157. /// <summary>
  158. /// Reads the next <see cref="bool"/> from the internal buffer.
  159. /// </summary>
  160. /// <returns>
  161. /// The <see cref="bool"/> that was read.
  162. /// </returns>
  163. /// <exception cref="InvalidOperationException">Attempt to read past the end of the stream.</exception>
  164. protected bool ReadBoolean()
  165. {
  166. return ReadByte() != 0;
  167. }
  168. /// <summary>
  169. /// Reads the next <see cref="ushort"/> from the internal buffer.
  170. /// </summary>
  171. /// <returns>
  172. /// The <see cref="ushort"/> that was read.
  173. /// </returns>
  174. /// <exception cref="InvalidOperationException">Attempt to read past the end of the stream.</exception>
  175. protected ushort ReadUInt16()
  176. {
  177. return _stream.ReadUInt16();
  178. }
  179. /// <summary>
  180. /// Reads the next <see cref="uint"/> from the internal buffer.
  181. /// </summary>
  182. /// <returns>
  183. /// The <see cref="uint"/> that was read.
  184. /// </returns>
  185. /// <exception cref="InvalidOperationException">Attempt to read past the end of the stream.</exception>
  186. protected uint ReadUInt32()
  187. {
  188. return _stream.ReadUInt32();
  189. }
  190. /// <summary>
  191. /// Reads the next <see cref="ulong"/> from the internal buffer.
  192. /// </summary>
  193. /// <returns>
  194. /// The <see cref="ulong"/> that was read.
  195. /// </returns>
  196. /// <exception cref="InvalidOperationException">Attempt to read past the end of the stream.</exception>
  197. protected ulong ReadUInt64()
  198. {
  199. return _stream.ReadUInt64();
  200. }
  201. /// <summary>
  202. /// Reads the next <see cref="string"/> from the internal buffer using the specified encoding.
  203. /// </summary>
  204. /// <param name="encoding">The character encoding to use.</param>
  205. /// <returns>
  206. /// The <see cref="string"/> that was read.
  207. /// </returns>
  208. protected string ReadString(Encoding encoding = null)
  209. {
  210. return _stream.ReadString(encoding);
  211. }
  212. /// <summary>
  213. /// Reads next data type as byte array from internal buffer.
  214. /// </summary>
  215. /// <returns>
  216. /// The bytes read.
  217. /// </returns>
  218. protected byte[] ReadBinary()
  219. {
  220. return _stream.ReadBinary();
  221. }
  222. /// <summary>
  223. /// Reads next name-list data type from internal buffer.
  224. /// </summary>
  225. /// <returns>
  226. /// String array or read data.
  227. /// </returns>
  228. protected string[] ReadNamesList()
  229. {
  230. var namesList = ReadString(Ascii);
  231. return namesList.Split(',');
  232. }
  233. /// <summary>
  234. /// Reads next extension-pair data type from internal buffer.
  235. /// </summary>
  236. /// <returns>
  237. /// Extensions pair dictionary.
  238. /// </returns>
  239. protected Dictionary<string, string> ReadExtensionPair()
  240. {
  241. var result = new Dictionary<string, string>();
  242. while (!IsEndOfData)
  243. {
  244. var extensionName = ReadString(Ascii);
  245. var extensionData = ReadString(Ascii);
  246. result.Add(extensionName, extensionData);
  247. }
  248. return result;
  249. }
  250. /// <summary>
  251. /// Writes bytes array data into internal buffer.
  252. /// </summary>
  253. /// <param name="data">Byte array data to write.</param>
  254. /// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception>
  255. protected void Write(byte[] data)
  256. {
  257. _stream.Write(data);
  258. }
  259. /// <summary>
  260. /// Writes a sequence of bytes to the current SSH data stream and advances the current position
  261. /// within this stream by the number of bytes written.
  262. /// </summary>
  263. /// <param name="buffer">An array of bytes. This method write <paramref name="count"/> bytes from buffer to the current SSH data stream.</param>
  264. /// <param name="offset">The zero-based offset in <paramref name="buffer"/> at which to begin writing bytes to the SSH data stream.</param>
  265. /// <param name="count">The number of bytes to be written to the current SSH data stream.</param>
  266. /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception>
  267. /// <exception cref="ArgumentException">The sum of <paramref name="offset"/> and <paramref name="count"/> is greater than the buffer length.</exception>
  268. /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="count"/> is negative.</exception>
  269. protected void Write(byte[] buffer, int offset, int count)
  270. {
  271. _stream.Write(buffer, offset, count);
  272. }
  273. /// <summary>
  274. /// Writes <see cref="byte"/> data into internal buffer.
  275. /// </summary>
  276. /// <param name="data"><see cref="byte"/> data to write.</param>
  277. protected void Write(byte data)
  278. {
  279. _stream.WriteByte(data);
  280. }
  281. /// <summary>
  282. /// Writes <see cref="bool"/> into internal buffer.
  283. /// </summary>
  284. /// <param name="data"><see cref="bool" /> data to write.</param>
  285. protected void Write(bool data)
  286. {
  287. Write(data ? (byte)1 : (byte)0);
  288. }
  289. /// <summary>
  290. /// Writes <see cref="uint"/> data into internal buffer.
  291. /// </summary>
  292. /// <param name="data"><see cref="uint"/> data to write.</param>
  293. protected void Write(uint data)
  294. {
  295. _stream.Write(data);
  296. }
  297. /// <summary>
  298. /// Writes <see cref="ulong" /> data into internal buffer.
  299. /// </summary>
  300. /// <param name="data"><see cref="ulong"/> data to write.</param>
  301. protected void Write(ulong data)
  302. {
  303. _stream.Write(data);
  304. }
  305. /// <summary>
  306. /// Writes <see cref="string"/> data into internal buffer using default encoding.
  307. /// </summary>
  308. /// <param name="data"><see cref="string"/> data to write.</param>
  309. /// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception>
  310. protected void Write(string data)
  311. {
  312. Write(data, Utf8);
  313. }
  314. /// <summary>
  315. /// Writes <see cref="string"/> data into internal buffer using the specified encoding.
  316. /// </summary>
  317. /// <param name="data"><see cref="string"/> data to write.</param>
  318. /// <param name="encoding">The character encoding to use.</param>
  319. /// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception>
  320. /// <exception cref="ArgumentNullException"><paramref name="encoding"/> is <see langword="null"/>.</exception>
  321. protected void Write(string data, Encoding encoding)
  322. {
  323. _stream.Write(data, encoding);
  324. }
  325. /// <summary>
  326. /// Writes mpint data into internal buffer.
  327. /// </summary>
  328. /// <param name="data">mpint data to write.</param>
  329. protected void Write(BigInteger data)
  330. {
  331. _stream.Write(data);
  332. }
  333. /// <summary>
  334. /// Writes name-list data into internal buffer.
  335. /// </summary>
  336. /// <param name="data">name-list data to write.</param>
  337. protected void Write(string[] data)
  338. {
  339. #if NET || NETSTANDARD2_1_OR_GREATER
  340. Write(string.Join(',', data), Ascii);
  341. #else
  342. Write(string.Join(",", data), Ascii);
  343. #endif // NET || NETSTANDARD2_1_OR_GREATER
  344. }
  345. /// <summary>
  346. /// Writes extension-pair data into internal buffer.
  347. /// </summary>
  348. /// <param name="data">extension-pair data to write.</param>
  349. protected void Write(IDictionary<string, string> data)
  350. {
  351. foreach (var item in data)
  352. {
  353. Write(item.Key, Ascii);
  354. Write(item.Value, Ascii);
  355. }
  356. }
  357. /// <summary>
  358. /// Writes data into internal buffer.
  359. /// </summary>
  360. /// <param name="buffer">The data to write.</param>
  361. /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception>
  362. protected void WriteBinaryString(byte[] buffer)
  363. {
  364. _stream.WriteBinary(buffer);
  365. }
  366. /// <summary>
  367. /// Writes data into internal buffer.
  368. /// </summary>
  369. /// <param name="buffer">An array of bytes. This method write <paramref name="count"/> bytes from buffer to the current SSH data stream.</param>
  370. /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin writing bytes to the SSH data stream.</param>
  371. /// <param name="count">The number of bytes to be written to the current SSH data stream.</param>
  372. /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception>
  373. /// <exception cref="ArgumentException">The sum of <paramref name="offset"/> and <paramref name="count"/> is greater than the buffer length.</exception>
  374. /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="count"/> is negative.</exception>
  375. protected void WriteBinary(byte[] buffer, int offset, int count)
  376. {
  377. _stream.WriteBinary(buffer, offset, count);
  378. }
  379. }
  380. }