]> Joshua Wise's Git repositories - dumload.git/blob - src/com/jcraft/jsch/Buffer.java
Initial commit.
[dumload.git] / src / com / jcraft / jsch / Buffer.java
1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
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
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 package com.jcraft.jsch;
31
32 public class Buffer{
33   final byte[] tmp=new byte[4];
34   byte[] buffer;
35   int index;
36   int s;
37   public Buffer(int size){
38     buffer=new byte[size];
39     index=0;
40     s=0;
41   }
42   public Buffer(byte[] buffer){
43     this.buffer=buffer;
44     index=0;
45     s=0;
46   }
47   public Buffer(){ this(1024*10*2); }
48   public void putByte(byte foo){
49     buffer[index++]=foo;
50   }
51   public void putByte(byte[] foo) {
52     putByte(foo, 0, foo.length);
53   }
54   public void putByte(byte[] foo, int begin, int length) {
55     System.arraycopy(foo, begin, buffer, index, length);
56     index+=length;
57   }
58   public void putString(byte[] foo){
59     putString(foo, 0, foo.length);
60   }
61   public void putString(byte[] foo, int begin, int length) {
62     putInt(length);
63     putByte(foo, begin, length);
64   }
65   public void putInt(int val) {
66     tmp[0]=(byte)(val >>> 24);
67     tmp[1]=(byte)(val >>> 16);
68     tmp[2]=(byte)(val >>> 8);
69     tmp[3]=(byte)(val);
70     System.arraycopy(tmp, 0, buffer, index, 4);
71     index+=4;
72   }
73   public void putLong(long val) {
74     tmp[0]=(byte)(val >>> 56);
75     tmp[1]=(byte)(val >>> 48);
76     tmp[2]=(byte)(val >>> 40);
77     tmp[3]=(byte)(val >>> 32);
78     System.arraycopy(tmp, 0, buffer, index, 4);
79     tmp[0]=(byte)(val >>> 24);
80     tmp[1]=(byte)(val >>> 16);
81     tmp[2]=(byte)(val >>> 8);
82     tmp[3]=(byte)(val);
83     System.arraycopy(tmp, 0, buffer, index+4, 4);
84     index+=8;
85   }
86   void skip(int n) {
87     index+=n;
88   }
89   void putPad(int n) {
90     while(n>0){
91       buffer[index++]=(byte)0;
92       n--;
93     }
94   }
95   public void putMPInt(byte[] foo){
96     int i=foo.length;
97     if((foo[0]&0x80)!=0){
98       i++;
99       putInt(i);
100       putByte((byte)0);
101     }
102     else{
103       putInt(i);
104     }
105     putByte(foo);
106   }
107   public int getLength(){
108     return index-s;
109   }
110   public int getOffSet(){
111     return s;
112   }
113   public void setOffSet(int s){
114     this.s=s;
115   }
116   public long getLong(){
117     long foo = getInt()&0xffffffffL;
118     foo = ((foo<<32)) | (getInt()&0xffffffffL);
119     return foo;
120   }
121   public int getInt(){
122     int foo = getShort();
123     foo = ((foo<<16)&0xffff0000) | (getShort()&0xffff);
124     return foo;
125   }
126   public long getUInt(){
127     long foo = 0L;
128     long bar = 0L;
129     foo = getByte();
130     foo = ((foo<<8)&0xff00)|(getByte()&0xff);
131     bar = getByte();
132     bar = ((bar<<8)&0xff00)|(getByte()&0xff);
133     foo = ((foo<<16)&0xffff0000) | (bar&0xffff);
134     return foo;
135   }
136   int getShort() {
137     int foo = getByte();
138     foo = ((foo<<8)&0xff00)|(getByte()&0xff);
139     return foo;
140   }
141   public int getByte() {
142     return (buffer[s++]&0xff);
143   }
144   public void getByte(byte[] foo) {
145     getByte(foo, 0, foo.length);
146   }
147   void getByte(byte[] foo, int start, int len) {
148     System.arraycopy(buffer, s, foo, start, len); 
149     s+=len;
150   }
151   public int getByte(int len) {
152     int foo=s;
153     s+=len;
154     return foo;
155   }
156   public byte[] getMPInt() {
157     int i=getInt();
158     byte[] foo=new byte[i];
159     getByte(foo, 0, i);
160     return foo;
161   }
162   public byte[] getMPIntBits() {
163     int bits=getInt();
164     int bytes=(bits+7)/8;
165     byte[] foo=new byte[bytes];
166     getByte(foo, 0, bytes);
167     if((foo[0]&0x80)!=0){
168       byte[] bar=new byte[foo.length+1];
169       bar[0]=0; // ??
170       System.arraycopy(foo, 0, bar, 1, foo.length);
171       foo=bar;
172     }
173     return foo;
174   }
175   public byte[] getString() {
176     int i = getInt();  // uint32
177     /*
178     if(i<0 ||  // bigger than 0x7fffffff
179        s+i>index){
180       //throw new java.io.IOException("invalid string length: "+(((long)i)&0xffffffffL));
181       i = index-s; // the session will be broken, but working around OOME.
182     }
183     */
184     byte[] foo=new byte[i];
185     getByte(foo, 0, i);
186     return foo;
187   }
188   byte[] getString(int[]start, int[]len) {
189     int i=getInt();
190     start[0]=getByte(i);
191     len[0]=i;
192     return buffer;
193   }
194   public void reset(){
195     index=0;
196     s=0;
197   }
198   public void shift(){
199     if(s==0)return;
200     System.arraycopy(buffer, s, buffer, 0, index-s);
201     index=index-s;
202     s=0;
203   }
204   void rewind(){
205     s=0;
206   }
207
208   byte getCommand(){
209     return buffer[5];
210   }
211
212 /*
213   static String[] chars={
214     "0","1","2","3","4","5","6","7","8","9", "a","b","c","d","e","f"
215   };
216   static void dump_buffer(){
217     int foo;
218     for(int i=0; i<tmp_buffer_index; i++){
219         foo=tmp_buffer[i]&0xff;
220         System.err.print(chars[(foo>>>4)&0xf]);
221         System.err.print(chars[foo&0xf]);
222         if(i%16==15){
223           System.err.println("");
224           continue;
225         }
226         if(i>0 && i%2==1){
227           System.err.print(" ");
228         }
229     }
230     System.err.println("");
231   }
232   static void dump(byte[] b){
233     dump(b, 0, b.length);
234   }
235   static void dump(byte[] b, int s, int l){
236     for(int i=s; i<s+l; i++){
237       System.err.print(Integer.toHexString(b[i]&0xff)+":");
238     }
239     System.err.println("");
240   }
241 */
242
243 }
This page took 0.036908 seconds and 4 git commands to generate.