SshData.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Globalization;
  6. namespace Renci.SshNet.Common
  7. {
  8. /// <summary>
  9. /// Base ssh data serialization type
  10. /// </summary>
  11. public abstract class SshData
  12. {
  13. /// <summary>
  14. /// Data byte array that hold message unencrypted data
  15. /// </summary>
  16. private List<byte> _data;
  17. private int _readerIndex;
  18. /// <summary>
  19. /// Gets a value indicating whether all data from the buffer has been read.
  20. /// </summary>
  21. /// <value>
  22. /// <c>true</c> if this instance is end of data; otherwise, <c>false</c>.
  23. /// </value>
  24. public bool IsEndOfData
  25. {
  26. get
  27. {
  28. return this._readerIndex >= this._data.Count();
  29. }
  30. }
  31. private byte[] _loadedData;
  32. /// <summary>
  33. /// Gets the index that represents zero in current data type.
  34. /// </summary>
  35. /// <value>
  36. /// The index of the zero reader.
  37. /// </value>
  38. protected virtual int ZeroReaderIndex
  39. {
  40. get
  41. {
  42. return 0;
  43. }
  44. }
  45. /// <summary>
  46. /// Gets data bytes array
  47. /// </summary>
  48. /// <returns></returns>
  49. public virtual byte[] GetBytes()
  50. {
  51. this._data = new List<byte>();
  52. this.SaveData();
  53. return this._data.ToArray();
  54. }
  55. internal T OfType<T>() where T : SshData, new()
  56. {
  57. var result = new T();
  58. result.LoadBytes(this._loadedData);
  59. result.LoadData();
  60. return result;
  61. }
  62. /// <summary>
  63. /// Loads data from specified bytes.
  64. /// </summary>
  65. /// <param name="value">Bytes array.</param>
  66. /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
  67. public void Load(byte[] value)
  68. {
  69. if (value == null)
  70. throw new ArgumentNullException("value");
  71. this.LoadBytes(value);
  72. this.LoadData();
  73. }
  74. /// <summary>
  75. /// Called when type specific data need to be loaded.
  76. /// </summary>
  77. protected abstract void LoadData();
  78. /// <summary>
  79. /// Called when type specific data need to be saved.
  80. /// </summary>
  81. protected abstract void SaveData();
  82. /// <summary>
  83. /// Loads data bytes into internal buffer.
  84. /// </summary>
  85. /// <param name="bytes">The bytes.</param>
  86. /// <exception cref="ArgumentNullException"><paramref name="bytes"/> is null.</exception>
  87. protected void LoadBytes(byte[] bytes)
  88. {
  89. // Note about why I check for null here, and in Load(byte[]) in this class.
  90. // This method is called by several other classes, such as SshNet.Messages.Message, SshNet.Sftp.SftpMessage.
  91. if (bytes == null)
  92. throw new ArgumentNullException("bytes");
  93. this.ResetReader();
  94. this._loadedData = bytes;
  95. this._data = new List<byte>(bytes);
  96. }
  97. /// <summary>
  98. /// Resets internal data reader index.
  99. /// </summary>
  100. protected void ResetReader()
  101. {
  102. this._readerIndex = this.ZeroReaderIndex; // Set to 1 to skip first byte which specifies message type
  103. }
  104. /// <summary>
  105. /// Reads all data left in internal buffer at current position.
  106. /// </summary>
  107. /// <returns>An array of bytes containing the remaining data in the internal buffer.</returns>
  108. protected byte[] ReadBytes()
  109. {
  110. var data = new byte[this._data.Count - this._readerIndex];
  111. this._data.CopyTo(this._readerIndex, data, 0, data.Length);
  112. return data;
  113. }
  114. /// <summary>
  115. /// Reads next specified number of bytes data type from internal buffer.
  116. /// </summary>
  117. /// <param name="length">Number of bytes to read.</param>
  118. /// <returns>An array of bytes that was read from the internal buffer.</returns>
  119. /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is greater than the internal buffer size.</exception>
  120. protected byte[] ReadBytes(int length)
  121. {
  122. // Note that this also prevents allocating non-relevant lengths, such as if length is greater than _data.Count but less than int.MaxValue.
  123. // For the nerds, the condition translates to: if (length > data.Count && length < int.MaxValue)
  124. // Which probably would cause all sorts of exception, most notably OutOfMemoryException.
  125. if (length > this._data.Count)
  126. throw new ArgumentOutOfRangeException("length");
  127. var result = new byte[length];
  128. this._data.CopyTo(this._readerIndex, result, 0, length);
  129. this._readerIndex += length;
  130. return result;
  131. }
  132. /// <summary>
  133. /// Reads next byte data type from internal buffer.
  134. /// </summary>
  135. /// <returns>Byte read.</returns>
  136. protected byte ReadByte()
  137. {
  138. return this.ReadBytes(1).FirstOrDefault();
  139. }
  140. /// <summary>
  141. /// Reads next boolean data type from internal buffer.
  142. /// </summary>
  143. /// <returns>Boolean read.</returns>
  144. protected bool ReadBoolean()
  145. {
  146. return this.ReadByte() == 0 ? false : true;
  147. }
  148. /// <summary>
  149. /// Reads next uint16 data type from internal buffer.
  150. /// </summary>
  151. /// <returns>uint16 read</returns>
  152. protected UInt16 ReadUInt16()
  153. {
  154. var data = this.ReadBytes(2);
  155. return (ushort)(data[0] << 8 | data[1]);
  156. }
  157. /// <summary>
  158. /// Reads next uint32 data type from internal buffer.
  159. /// </summary>
  160. /// <returns>uint32 read</returns>
  161. protected UInt32 ReadUInt32()
  162. {
  163. var data = this.ReadBytes(4);
  164. return (uint)(data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]);
  165. }
  166. /// <summary>
  167. /// Reads next uint64 data type from internal buffer.
  168. /// </summary>
  169. /// <returns>uint64 read</returns>
  170. protected UInt64 ReadUInt64()
  171. {
  172. var data = this.ReadBytes(8);
  173. return (uint)(data[0] << 56 | data[1] << 48 | data[2] << 40 | data[3] << 32 | data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]);
  174. }
  175. /// <summary>
  176. /// Reads next int64 data type from internal buffer.
  177. /// </summary>
  178. /// <returns>int64 read</returns>
  179. protected Int64 ReadInt64()
  180. {
  181. var data = this.ReadBytes(8);
  182. return (int)(data[0] << 56 | data[1] << 48 | data[2] << 40 | data[3] << 32 | data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]);
  183. }
  184. /// <summary>
  185. /// Reads next string data type from internal buffer.
  186. /// </summary>
  187. /// <returns>string read</returns>
  188. protected string ReadString()
  189. {
  190. var length = this.ReadUInt32();
  191. if (length > (uint)int.MaxValue)
  192. {
  193. throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Strings longer than {0} is not supported.", int.MaxValue));
  194. }
  195. return Encoding.UTF8.GetString(this.ReadBytes((int)length), 0, (int)length);
  196. }
  197. /// <summary>
  198. /// Reads next string data type from internal buffer.
  199. /// </summary>
  200. /// <returns>string read</returns>
  201. protected byte[] ReadBinaryString()
  202. {
  203. var length = this.ReadUInt32();
  204. if (length > (uint)int.MaxValue)
  205. {
  206. throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Strings longer than {0} is not supported.", int.MaxValue));
  207. }
  208. return this.ReadBytes((int)length);
  209. }
  210. /// <summary>
  211. /// Reads next mpint data type from internal buffer.
  212. /// </summary>
  213. /// <returns>mpint read.</returns>
  214. protected BigInteger ReadBigInt()
  215. {
  216. var length = this.ReadUInt32();
  217. var data = this.ReadBytes((int)length);
  218. return new BigInteger(data.Reverse().ToArray());
  219. }
  220. /// <summary>
  221. /// Reads next name-list data type from internal buffer.
  222. /// </summary>
  223. /// <returns></returns>
  224. protected string[] ReadNamesList()
  225. {
  226. var namesList = this.ReadString();
  227. return namesList.Split(',');
  228. }
  229. /// <summary>
  230. /// Reads next extension-pair data type from internal buffer.
  231. /// </summary>
  232. /// <returns></returns>
  233. protected IDictionary<string, string> ReadExtensionPair()
  234. {
  235. Dictionary<string, string> result = new Dictionary<string, string>();
  236. while (this._readerIndex < this._data.Count)
  237. {
  238. var extensionName = this.ReadString();
  239. var extensionData = this.ReadString();
  240. result.Add(extensionName, extensionData);
  241. }
  242. return result;
  243. }
  244. /// <summary>
  245. /// Writes bytes array data into internal buffer.
  246. /// </summary>
  247. /// <param name="data">Byte array data to write.</param>
  248. /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception>
  249. protected void Write(IEnumerable<byte> data)
  250. {
  251. this._data.AddRange(data);
  252. }
  253. /// <summary>
  254. /// Writes byte data into internal buffer.
  255. /// </summary>
  256. /// <param name="data">Byte data to write.</param>
  257. protected void Write(byte data)
  258. {
  259. this._data.Add(data);
  260. }
  261. /// <summary>
  262. /// Writes boolean data into internal buffer.
  263. /// </summary>
  264. /// <param name="data">Boolean data to write.</param>
  265. protected void Write(bool data)
  266. {
  267. if (data)
  268. {
  269. this.Write(1);
  270. }
  271. else
  272. {
  273. this.Write(0);
  274. }
  275. }
  276. /// <summary>
  277. /// Writes uint16 data into internal buffer.
  278. /// </summary>
  279. /// <param name="data">uint16 data to write.</param>
  280. protected void Write(UInt16 data)
  281. {
  282. this.Write(data.GetBytes());
  283. }
  284. /// <summary>
  285. /// Writes uint32 data into internal buffer.
  286. /// </summary>
  287. /// <param name="data">uint32 data to write.</param>
  288. protected void Write(UInt32 data)
  289. {
  290. this.Write(data.GetBytes());
  291. }
  292. /// <summary>
  293. /// Writes uint64 data into internal buffer.
  294. /// </summary>
  295. /// <param name="data">uint64 data to write.</param>
  296. protected void Write(UInt64 data)
  297. {
  298. this.Write(data.GetBytes());
  299. }
  300. /// <summary>
  301. /// Writes int64 data into internal buffer.
  302. /// </summary>
  303. /// <param name="data">int64 data to write.</param>
  304. protected void Write(Int64 data)
  305. {
  306. this.Write(data.GetBytes());
  307. }
  308. /// <summary>
  309. /// Writes string data into internal buffer.
  310. /// </summary>
  311. /// <param name="data">string data to write.</param>
  312. /// <param name="encoding">String text encoding to use.</param>
  313. /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception>
  314. protected void Write(string data, Encoding encoding)
  315. {
  316. if (data == null)
  317. throw new ArgumentNullException("data");
  318. this.Write((uint)data.Length);
  319. this.Write(encoding.GetBytes(data));
  320. }
  321. /// <summary>
  322. /// Writes string data into internal buffer.
  323. /// </summary>
  324. /// <param name="data">string data to write.</param>
  325. protected void Write(string data)
  326. {
  327. this.Write(data, Encoding.UTF8);
  328. }
  329. /// <summary>
  330. /// Writes string data into internal buffer.
  331. /// </summary>
  332. /// <param name="data">string data to write.</param>
  333. /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception>
  334. protected void WriteBinaryString(byte[] data)
  335. {
  336. if (data == null)
  337. throw new ArgumentNullException("data");
  338. this.Write((uint)data.Length);
  339. this._data.AddRange(data);
  340. }
  341. /// <summary>
  342. /// Writes mpint data into internal buffer.
  343. /// </summary>
  344. /// <param name="data">mpint data to write.</param>
  345. protected void Write(BigInteger data)
  346. {
  347. var bytes = data.ToByteArray().Reverse().ToList();
  348. this.Write((uint)bytes.Count);
  349. this.Write(bytes);
  350. }
  351. /// <summary>
  352. /// Writes name-list data into internal buffer.
  353. /// </summary>
  354. /// <param name="data">name-list data to write.</param>
  355. protected void Write(string[] data)
  356. {
  357. this.Write(string.Join(",", data));
  358. }
  359. /// <summary>
  360. /// Writes extension-pair data into internal buffer.
  361. /// </summary>
  362. /// <param name="data">extension-pair data to write.</param>
  363. protected void Write(IDictionary<string, string> data)
  364. {
  365. foreach (var item in data)
  366. {
  367. this.Write(item.Key);
  368. this.Write(item.Value);
  369. }
  370. }
  371. }
  372. }