1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 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.
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.
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.
30 package com.jcraft.jsch;
33 final byte[] tmp=new byte[4];
37 public Buffer(int size){
38 buffer=new byte[size];
42 public Buffer(byte[] buffer){
47 public Buffer(){ this(1024*10*2); }
48 public void putByte(byte foo){
51 public void putByte(byte[] foo) {
52 putByte(foo, 0, foo.length);
54 public void putByte(byte[] foo, int begin, int length) {
55 System.arraycopy(foo, begin, buffer, index, length);
58 public void putString(byte[] foo){
59 putString(foo, 0, foo.length);
61 public void putString(byte[] foo, int begin, int length) {
63 putByte(foo, begin, length);
65 public void putInt(int val) {
66 tmp[0]=(byte)(val >>> 24);
67 tmp[1]=(byte)(val >>> 16);
68 tmp[2]=(byte)(val >>> 8);
70 System.arraycopy(tmp, 0, buffer, index, 4);
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);
83 System.arraycopy(tmp, 0, buffer, index+4, 4);
91 buffer[index++]=(byte)0;
95 public void putMPInt(byte[] foo){
107 public int getLength(){
110 public int getOffSet(){
113 public void setOffSet(int s){
116 public long getLong(){
117 long foo = getInt()&0xffffffffL;
118 foo = ((foo<<32)) | (getInt()&0xffffffffL);
122 int foo = getShort();
123 foo = ((foo<<16)&0xffff0000) | (getShort()&0xffff);
126 public long getUInt(){
130 foo = ((foo<<8)&0xff00)|(getByte()&0xff);
132 bar = ((bar<<8)&0xff00)|(getByte()&0xff);
133 foo = ((foo<<16)&0xffff0000) | (bar&0xffff);
138 foo = ((foo<<8)&0xff00)|(getByte()&0xff);
141 public int getByte() {
142 return (buffer[s++]&0xff);
144 public void getByte(byte[] foo) {
145 getByte(foo, 0, foo.length);
147 void getByte(byte[] foo, int start, int len) {
148 System.arraycopy(buffer, s, foo, start, len);
151 public int getByte(int len) {
156 public byte[] getMPInt() {
158 byte[] foo=new byte[i];
162 public byte[] getMPIntBits() {
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];
170 System.arraycopy(foo, 0, bar, 1, foo.length);
175 public byte[] getString() {
176 int i = getInt(); // uint32
178 if(i<0 || // bigger than 0x7fffffff
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.
184 byte[] foo=new byte[i];
188 byte[] getString(int[]start, int[]len) {
200 System.arraycopy(buffer, s, buffer, 0, index-s);
213 static String[] chars={
214 "0","1","2","3","4","5","6","7","8","9", "a","b","c","d","e","f"
216 static void dump_buffer(){
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]);
223 System.err.println("");
227 System.err.print(" ");
230 System.err.println("");
232 static void dump(byte[] b){
233 dump(b, 0, b.length);
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)+":");
239 System.err.println("");