00001 using System;
00002
00003 namespace Tamir.SharpSsh.jsch
00004 {
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 public class Buffer
00035 {
00036 static byte[] tmp=new byte[4];
00037 internal byte[] buffer;
00038 internal int index;
00039 internal int s;
00040 public Buffer(int size)
00041 {
00042 buffer=new byte[size];
00043 index=0;
00044 s=0;
00045 }
00046 public Buffer(byte[] buffer)
00047 {
00048 this.buffer=buffer;
00049 index=0;
00050 s=0;
00051 }
00052 public Buffer():this(1024*10*2){ }
00053 public void putByte(byte foo)
00054 {
00055 buffer[index++]=foo;
00056 }
00057 public void putByte(byte[] foo)
00058 {
00059 putByte(foo, 0, foo.Length);
00060 }
00061 public void putByte(byte[] foo, int begin, int length)
00062 {
00063 Array.Copy(foo, begin, buffer, index, length);
00064 index+=length;
00065 }
00066 public void putString(byte[] foo)
00067 {
00068 putString(foo, 0, foo.Length);
00069 }
00070 public void putString(byte[] foo, int begin, int length)
00071 {
00072 putInt(length);
00073 putByte(foo, begin, length);
00074 }
00075 public void putInt(int v)
00076 {
00077 uint val = (uint)v;
00078 tmp[0]=(byte)(val >> 24);
00079 tmp[1]=(byte)(val >> 16);
00080 tmp[2]=(byte)(val >> 8);
00081 tmp[3]=(byte)(val);
00082 Array.Copy(tmp, 0, buffer, index, 4);
00083 index+=4;
00084 }
00085 public void putLong(long v)
00086 {
00087 ulong val = (ulong)v;
00088 tmp[0]=(byte)(val >> 56);
00089 tmp[1]=(byte)(val >> 48);
00090 tmp[2]=(byte)(val >>40);
00091 tmp[3]=(byte)(val >> 32);
00092 Array.Copy(tmp, 0, buffer, index, 4);
00093 tmp[0]=(byte)(val >> 24);
00094 tmp[1]=(byte)(val >> 16);
00095 tmp[2]=(byte)(val >> 8);
00096 tmp[3]=(byte)(val);
00097 Array.Copy(tmp, 0, buffer, index+4, 4);
00098 index+=8;
00099 }
00100 internal void skip(int n)
00101 {
00102 index+=n;
00103 }
00104 internal void putPad(int n)
00105 {
00106 while(n>0)
00107 {
00108 buffer[index++]=(byte)0;
00109 n--;
00110 }
00111 }
00112 public void putMPInt(byte[] foo)
00113 {
00114 int i=foo.Length;
00115 if((foo[0]&0x80)!=0)
00116 {
00117 i++;
00118 putInt(i);
00119 putByte((byte)0);
00120 }
00121 else
00122 {
00123 putInt(i);
00124 }
00125 putByte(foo);
00126 }
00127 public int getLength()
00128 {
00129 return index-s;
00130 }
00131 public int getOffSet()
00132 {
00133 return s;
00134 }
00135 public void setOffSet(int s)
00136 {
00137 this.s=s;
00138 }
00139 public long getLong()
00140 {
00141 long foo = getInt()&0xffffffffL;
00142 foo = ((foo<<32)) | (getInt()&0xffffffffL);
00143 return foo;
00144 }
00145 public int getInt()
00146 {
00147 uint foo = (uint) getShort();
00148 foo = ((foo<<16)&0xffff0000) | ((uint)getShort()&0xffff);
00149 return (int)foo;
00150 }
00151 internal int getShort()
00152 {
00153 int foo = getByte();
00154 foo = ((foo<<8)&0xff00)|(getByte()&0xff);
00155 return foo;
00156 }
00157 public int getByte()
00158 {
00159 return (buffer[s++]&0xff);
00160 }
00161 public void getByte(byte[] foo)
00162 {
00163 getByte(foo, 0, foo.Length);
00164 }
00165 void getByte(byte[] foo, int start, int len)
00166 {
00167 Array.Copy(buffer, s, foo, start, len);
00168 s+=len;
00169 }
00170 public int getByte(int len)
00171 {
00172 int foo=s;
00173 s+=len;
00174 return foo;
00175 }
00176 public byte[] getMPInt()
00177 {
00178 int i=getInt();
00179 byte[] foo=new byte[i];
00180 getByte(foo, 0, i);
00181 return foo;
00182 }
00183 public byte[] getMPIntBits()
00184 {
00185 int bits=getInt();
00186 int bytes=(bits+7)/8;
00187 byte[] foo=new byte[bytes];
00188 getByte(foo, 0, bytes);
00189 if((foo[0]&0x80)!=0)
00190 {
00191 byte[] bar=new byte[foo.Length+1];
00192 bar[0]=0;
00193 Array.Copy(foo, 0, bar, 1, foo.Length);
00194 foo=bar;
00195 }
00196 return foo;
00197 }
00198 public byte[] getString()
00199 {
00200 int i=getInt();
00201 byte[] foo=new byte[i];
00202 getByte(foo, 0, i);
00203 return foo;
00204 }
00205 internal byte[] getString(int[]start, int[]len)
00206 {
00207 int i=getInt();
00208 start[0]=getByte(i);
00209 len[0]=i;
00210 return buffer;
00211 }
00212 public void reset()
00213 {
00214 index=0;
00215 s=0;
00216 }
00217 public void shift()
00218 {
00219 if(s==0)return;
00220 Array.Copy(buffer, s, buffer, 0, index-s);
00221 index=index-s;
00222 s=0;
00223 }
00224 internal void rewind()
00225 {
00226 s=0;
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 }
00261
00262 }