SshData.cs 15 KB

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