001: /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
002: /*
003: Copyright (c) 2002-2008 ymnk, JCraft,Inc. All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions are met:
007:
008: 1. Redistributions of source code must retain the above copyright notice,
009: this list of conditions and the following disclaimer.
010:
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in
013: the documentation and/or other materials provided with the distribution.
014:
015: 3. The names of the authors may not be used to endorse or promote products
016: derived from this software without specific prior written permission.
017:
018: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
019: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
020: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
021: INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
022: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
024: OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
027: EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jcraft.jsch;
031:
032: public class Buffer {
033: final byte[] tmp = new byte[4];
034: byte[] buffer;
035: int index;
036: int s;
037:
038: public Buffer(int size) {
039: buffer = new byte[size];
040: index = 0;
041: s = 0;
042: }
043:
044: public Buffer(byte[] buffer) {
045: this .buffer = buffer;
046: index = 0;
047: s = 0;
048: }
049:
050: public Buffer() {
051: this (1024 * 10 * 2);
052: }
053:
054: public void putByte(byte foo) {
055: buffer[index++] = foo;
056: }
057:
058: public void putByte(byte[] foo) {
059: putByte(foo, 0, foo.length);
060: }
061:
062: public void putByte(byte[] foo, int begin, int length) {
063: System.arraycopy(foo, begin, buffer, index, length);
064: index += length;
065: }
066:
067: public void putString(byte[] foo) {
068: putString(foo, 0, foo.length);
069: }
070:
071: public void putString(byte[] foo, int begin, int length) {
072: putInt(length);
073: putByte(foo, begin, length);
074: }
075:
076: public void putInt(int val) {
077: tmp[0] = (byte) (val >>> 24);
078: tmp[1] = (byte) (val >>> 16);
079: tmp[2] = (byte) (val >>> 8);
080: tmp[3] = (byte) (val);
081: System.arraycopy(tmp, 0, buffer, index, 4);
082: index += 4;
083: }
084:
085: public void putLong(long val) {
086: tmp[0] = (byte) (val >>> 56);
087: tmp[1] = (byte) (val >>> 48);
088: tmp[2] = (byte) (val >>> 40);
089: tmp[3] = (byte) (val >>> 32);
090: System.arraycopy(tmp, 0, buffer, index, 4);
091: tmp[0] = (byte) (val >>> 24);
092: tmp[1] = (byte) (val >>> 16);
093: tmp[2] = (byte) (val >>> 8);
094: tmp[3] = (byte) (val);
095: System.arraycopy(tmp, 0, buffer, index + 4, 4);
096: index += 8;
097: }
098:
099: void skip(int n) {
100: index += n;
101: }
102:
103: void putPad(int n) {
104: while (n > 0) {
105: buffer[index++] = (byte) 0;
106: n--;
107: }
108: }
109:
110: public void putMPInt(byte[] foo) {
111: int i = foo.length;
112: if ((foo[0] & 0x80) != 0) {
113: i++;
114: putInt(i);
115: putByte((byte) 0);
116: } else {
117: putInt(i);
118: }
119: putByte(foo);
120: }
121:
122: public int getLength() {
123: return index - s;
124: }
125:
126: public int getOffSet() {
127: return s;
128: }
129:
130: public void setOffSet(int s) {
131: this .s = s;
132: }
133:
134: public long getLong() {
135: long foo = getInt() & 0xffffffffL;
136: foo = ((foo << 32)) | (getInt() & 0xffffffffL);
137: return foo;
138: }
139:
140: public int getInt() {
141: int foo = getShort();
142: foo = ((foo << 16) & 0xffff0000) | (getShort() & 0xffff);
143: return foo;
144: }
145:
146: int getShort() {
147: int foo = getByte();
148: foo = ((foo << 8) & 0xff00) | (getByte() & 0xff);
149: return foo;
150: }
151:
152: public int getByte() {
153: return (buffer[s++] & 0xff);
154: }
155:
156: public void getByte(byte[] foo) {
157: getByte(foo, 0, foo.length);
158: }
159:
160: void getByte(byte[] foo, int start, int len) {
161: System.arraycopy(buffer, s, foo, start, len);
162: s += len;
163: }
164:
165: public int getByte(int len) {
166: int foo = s;
167: s += len;
168: return foo;
169: }
170:
171: public byte[] getMPInt() {
172: int i = getInt();
173: byte[] foo = new byte[i];
174: getByte(foo, 0, i);
175: return foo;
176: }
177:
178: public byte[] getMPIntBits() {
179: int bits = getInt();
180: int bytes = (bits + 7) / 8;
181: byte[] foo = new byte[bytes];
182: getByte(foo, 0, bytes);
183: if ((foo[0] & 0x80) != 0) {
184: byte[] bar = new byte[foo.length + 1];
185: bar[0] = 0; // ??
186: System.arraycopy(foo, 0, bar, 1, foo.length);
187: foo = bar;
188: }
189: return foo;
190: }
191:
192: public byte[] getString() {
193: int i = getInt();
194: byte[] foo = new byte[i];
195: getByte(foo, 0, i);
196: return foo;
197: }
198:
199: byte[] getString(int[] start, int[] len) {
200: int i = getInt();
201: start[0] = getByte(i);
202: len[0] = i;
203: return buffer;
204: }
205:
206: public void reset() {
207: index = 0;
208: s = 0;
209: }
210:
211: public void shift() {
212: if (s == 0)
213: return;
214: System.arraycopy(buffer, s, buffer, 0, index - s);
215: index = index - s;
216: s = 0;
217: }
218:
219: void rewind() {
220: s = 0;
221: }
222:
223: byte getCommand() {
224: return buffer[5];
225: }
226:
227: /*
228: static String[] chars={
229: "0","1","2","3","4","5","6","7","8","9", "a","b","c","d","e","f"
230: };
231: static void dump_buffer(){
232: int foo;
233: for(int i=0; i<tmp_buffer_index; i++){
234: foo=tmp_buffer[i]&0xff;
235: System.err.print(chars[(foo>>>4)&0xf]);
236: System.err.print(chars[foo&0xf]);
237: if(i%16==15){
238: System.err.println("");
239: continue;
240: }
241: if(i>0 && i%2==1){
242: System.err.print(" ");
243: }
244: }
245: System.err.println("");
246: }
247: static void dump(byte[] b){
248: dump(b, 0, b.length);
249: }
250: static void dump(byte[] b, int s, int l){
251: for(int i=s; i<s+l; i++){
252: System.err.print(Integer.toHexString(b[i]&0xff)+":");
253: }
254: System.err.println("");
255: }
256: */
257:
258: }
|