PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/SharpSSH/jsch/Buffer.cs

https://bitbucket.org/sign42/sharpssh
C# | 261 lines | 201 code | 32 blank | 28 comment | 7 complexity | e657b00e4e6e3a3637cf5262e1991613 MD5 | raw file
  1. using System;
  2. namespace Tamir.SharpSsh.jsch
  3. {
  4. /* -*-mode:java; c-basic-offset:2; -*- */
  5. /*
  6. Copyright (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9. 1. Redistributions of source code must retain the above copyright notice,
  10. this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in
  13. the documentation and/or other materials provided with the distribution.
  14. 3. The names of the authors may not be used to endorse or promote products
  15. derived from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  17. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  19. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. public class Buffer
  28. {
  29. private readonly byte[] tmp = new byte[4];
  30. internal byte[] buffer;
  31. internal int index;
  32. internal int s;
  33. public Buffer(int size)
  34. {
  35. buffer = new byte[size];
  36. index = 0;
  37. s = 0;
  38. }
  39. public Buffer(byte[] buffer)
  40. {
  41. this.buffer = buffer;
  42. index = 0;
  43. s = 0;
  44. }
  45. public Buffer() : this(1024*10*2)
  46. {
  47. }
  48. public void putByte(byte foo)
  49. {
  50. buffer[index++] = foo;
  51. }
  52. public void putByte(byte[] foo)
  53. {
  54. putByte(foo, 0, foo.Length);
  55. }
  56. public void putByte(byte[] foo, int begin, int length)
  57. {
  58. Array.Copy(foo, begin, buffer, index, length);
  59. index += length;
  60. }
  61. public void putString(byte[] foo)
  62. {
  63. putString(foo, 0, foo.Length);
  64. }
  65. public void putString(byte[] foo, int begin, int length)
  66. {
  67. putInt(length);
  68. putByte(foo, begin, length);
  69. }
  70. public void putInt(int v)
  71. {
  72. uint val = (uint) v;
  73. tmp[0] = (byte) (val >> 24);
  74. tmp[1] = (byte) (val >> 16);
  75. tmp[2] = (byte) (val >> 8);
  76. tmp[3] = (byte) (val);
  77. Array.Copy(tmp, 0, buffer, index, 4);
  78. index += 4;
  79. }
  80. public void putLong(long v)
  81. {
  82. ulong val = (ulong) v;
  83. tmp[0] = (byte) (val >> 56);
  84. tmp[1] = (byte) (val >> 48);
  85. tmp[2] = (byte) (val >> 40);
  86. tmp[3] = (byte) (val >> 32);
  87. Array.Copy(tmp, 0, buffer, index, 4);
  88. tmp[0] = (byte) (val >> 24);
  89. tmp[1] = (byte) (val >> 16);
  90. tmp[2] = (byte) (val >> 8);
  91. tmp[3] = (byte) (val);
  92. Array.Copy(tmp, 0, buffer, index + 4, 4);
  93. index += 8;
  94. }
  95. internal void skip(int n)
  96. {
  97. index += n;
  98. }
  99. internal void putPad(int n)
  100. {
  101. while (n > 0)
  102. {
  103. buffer[index++] = (byte) 0;
  104. n--;
  105. }
  106. }
  107. public void putMPInt(byte[] foo)
  108. {
  109. int i = foo.Length;
  110. if ((foo[0] & 0x80) != 0)
  111. {
  112. i++;
  113. putInt(i);
  114. putByte((byte) 0);
  115. }
  116. else
  117. {
  118. putInt(i);
  119. }
  120. putByte(foo);
  121. }
  122. public int getLength()
  123. {
  124. return index - s;
  125. }
  126. public int getOffSet()
  127. {
  128. return s;
  129. }
  130. public void setOffSet(int s)
  131. {
  132. this.s = s;
  133. }
  134. public long getLong()
  135. {
  136. long foo = getInt() & 0xffffffffL;
  137. foo = ((foo << 32)) | (getInt() & 0xffffffffL);
  138. return foo;
  139. }
  140. public int getInt()
  141. {
  142. uint foo = (uint) getShort();
  143. foo = ((foo << 16) & 0xffff0000) | ((uint) getShort() & 0xffff);
  144. return (int) foo;
  145. }
  146. internal int getShort()
  147. {
  148. int foo = getByte();
  149. foo = ((foo << 8) & 0xff00) | (getByte() & 0xff);
  150. return foo;
  151. }
  152. public int getByte()
  153. {
  154. return (buffer[s++] & 0xff);
  155. }
  156. public void getByte(byte[] foo)
  157. {
  158. getByte(foo, 0, foo.Length);
  159. }
  160. private void getByte(byte[] foo, int start, int len)
  161. {
  162. Array.Copy(buffer, s, foo, start, len);
  163. s += len;
  164. }
  165. public int getByte(int len)
  166. {
  167. int foo = s;
  168. s += len;
  169. return foo;
  170. }
  171. public byte[] getMPInt()
  172. {
  173. int i = getInt();
  174. byte[] foo = new byte[i];
  175. getByte(foo, 0, i);
  176. return foo;
  177. }
  178. public byte[] getMPIntBits()
  179. {
  180. int bits = getInt();
  181. int bytes = (bits + 7)/8;
  182. byte[] foo = new byte[bytes];
  183. getByte(foo, 0, bytes);
  184. if ((foo[0] & 0x80) != 0)
  185. {
  186. byte[] bar = new byte[foo.Length + 1];
  187. bar[0] = 0; // ??
  188. Array.Copy(foo, 0, bar, 1, foo.Length);
  189. foo = bar;
  190. }
  191. return foo;
  192. }
  193. public byte[] getString()
  194. {
  195. int i = getInt();
  196. byte[] foo = new byte[i];
  197. getByte(foo, 0, i);
  198. return foo;
  199. }
  200. internal byte[] getString(int[] start, int[] len)
  201. {
  202. int i = getInt();
  203. start[0] = getByte(i);
  204. len[0] = i;
  205. return buffer;
  206. }
  207. public void reset()
  208. {
  209. index = 0;
  210. s = 0;
  211. }
  212. public void shift()
  213. {
  214. if (s == 0) return;
  215. Array.Copy(buffer, s, buffer, 0, index - s);
  216. index = index - s;
  217. s = 0;
  218. }
  219. internal void rewind()
  220. {
  221. s = 0;
  222. }
  223. }
  224. }