SshDataWorker.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Diagnostics.Contracts;
  3. using System.IO;
  4. using System.Text;
  5. namespace FxSsh
  6. {
  7. public class SshDataWorker : IDisposable
  8. {
  9. private readonly MemoryStream _ms;
  10. public SshDataWorker()
  11. {
  12. _ms = new MemoryStream(512);
  13. }
  14. public SshDataWorker(byte[] buffer)
  15. {
  16. Contract.Requires(buffer != null);
  17. _ms = new MemoryStream(buffer);
  18. }
  19. public long DataAvailable
  20. {
  21. get
  22. {
  23. return _ms.Length - _ms.Position;
  24. }
  25. }
  26. public void Write(bool value)
  27. {
  28. _ms.WriteByte(value ? (byte)1 : (byte)0);
  29. }
  30. public void Write(byte value)
  31. {
  32. _ms.WriteByte(value);
  33. }
  34. public void Write(uint value)
  35. {
  36. var bytes = new[] { (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)(value & 0xFF) };
  37. _ms.Write(bytes, 0, 4);
  38. }
  39. public void Write(ulong value)
  40. {
  41. var bytes = new[] {
  42. (byte)(value >> 56), (byte)(value >> 48), (byte)(value >> 40), (byte)(value >> 32),
  43. (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)(value & 0xFF)
  44. };
  45. _ms.Write(bytes, 0, 8);
  46. }
  47. public void Write(string str, Encoding encoding)
  48. {
  49. Contract.Requires(str != null);
  50. Contract.Requires(encoding != null);
  51. var bytes = encoding.GetBytes(str);
  52. WriteBinary(bytes);
  53. }
  54. public void WriteMpint(byte[] data)
  55. {
  56. Contract.Requires(data != null);
  57. if (data.Length == 1 && data[0] == 0)
  58. {
  59. Write(new byte[4]);
  60. }
  61. else
  62. {
  63. var length = (uint)data.Length;
  64. var high = ((data[0] & 0x80) != 0);
  65. if (high)
  66. {
  67. Write(length + 1);
  68. Write((byte)0);
  69. Write(data);
  70. }
  71. else
  72. {
  73. Write(length);
  74. Write(data);
  75. }
  76. }
  77. }
  78. public void Write(byte[] data)
  79. {
  80. Contract.Requires(data != null);
  81. _ms.Write(data, 0, data.Length);
  82. }
  83. public void WriteBinary(byte[] buffer)
  84. {
  85. Contract.Requires(buffer != null);
  86. Write((uint)buffer.Length);
  87. _ms.Write(buffer, 0, buffer.Length);
  88. }
  89. public void WriteBinary(byte[] buffer, int offset, int count)
  90. {
  91. Contract.Requires(buffer != null);
  92. Write((uint)count);
  93. _ms.Write(buffer, offset, count);
  94. }
  95. public bool ReadBoolean()
  96. {
  97. var num = _ms.ReadByte();
  98. if (num == -1)
  99. throw new EndOfStreamException();
  100. return num != 0;
  101. }
  102. public byte ReadByte()
  103. {
  104. var data = ReadBinary(1);
  105. return data[0];
  106. }
  107. public uint ReadUInt32()
  108. {
  109. var data = ReadBinary(4);
  110. return (uint)(data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]);
  111. }
  112. public ulong ReadUInt64()
  113. {
  114. var data = ReadBinary(8);
  115. return ((ulong)data[0] << 56 | (ulong)data[1] << 48 | (ulong)data[2] << 40 | (ulong)data[3] << 32 |
  116. (ulong)data[4] << 24 | (ulong)data[5] << 16 | (ulong)data[6] << 8 | data[7]);
  117. }
  118. public string ReadString(Encoding encoding)
  119. {
  120. Contract.Requires(encoding != null);
  121. var bytes = ReadBinary();
  122. return encoding.GetString(bytes);
  123. }
  124. public byte[] ReadMpint()
  125. {
  126. var data = ReadBinary();
  127. if (data.Length == 0)
  128. return new byte[1];
  129. if (data[0] == 0)
  130. {
  131. var output = new byte[data.Length - 1];
  132. Array.Copy(data, 1, output, 0, output.Length);
  133. return output;
  134. }
  135. return data;
  136. }
  137. public byte[] ReadBinary(int length)
  138. {
  139. var data = new byte[length];
  140. var bytesRead = _ms.Read(data, 0, length);
  141. if (bytesRead < length)
  142. throw new ArgumentOutOfRangeException("length");
  143. return data;
  144. }
  145. public byte[] ReadBinary()
  146. {
  147. var length = ReadUInt32();
  148. return ReadBinary((int)length);
  149. }
  150. public byte[] ToByteArray()
  151. {
  152. return _ms.ToArray();
  153. }
  154. public void Dispose()
  155. {
  156. _ms.Dispose();
  157. }
  158. }
  159. }