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 Packet {
033:
034: private static Random random = null;
035:
036: static void setRandom(Random foo) {
037: random = foo;
038: }
039:
040: Buffer buffer;
041: byte[] ba4 = new byte[4];
042:
043: public Packet(Buffer buffer) {
044: this .buffer = buffer;
045: }
046:
047: public void reset() {
048: buffer.index = 5;
049: }
050:
051: void padding(int bsize) {
052: int len = buffer.index;
053: int pad = (-len) & (bsize - 1);
054: if (pad < bsize) {
055: pad += bsize;
056: }
057: len = len + pad - 4;
058: ba4[0] = (byte) (len >>> 24);
059: ba4[1] = (byte) (len >>> 16);
060: ba4[2] = (byte) (len >>> 8);
061: ba4[3] = (byte) (len);
062: System.arraycopy(ba4, 0, buffer.buffer, 0, 4);
063: buffer.buffer[4] = (byte) pad;
064: synchronized (random) {
065: random.fill(buffer.buffer, buffer.index, pad);
066: }
067: buffer.skip(pad);
068: //buffer.putPad(pad);
069: /*
070: for(int i=0; i<buffer.index; i++){
071: System.err.print(Integer.toHexString(buffer.buffer[i]&0xff)+":");
072: }
073: System.err.println("");
074: */
075: }
076:
077: int shift(int len, int mac) {
078: int s = len + 5 + 9;
079: int pad = (-s) & 15;
080: if (pad < 16)
081: pad += 16;
082: s += pad;
083: s += mac;
084:
085: /**/
086: if (buffer.buffer.length < s + buffer.index - 5 - 9 - len) {
087: byte[] foo = new byte[s + buffer.index - 5 - 9 - len];
088: System.arraycopy(buffer.buffer, 0, foo, 0,
089: buffer.buffer.length);
090: buffer.buffer = foo;
091: }
092: /**/
093:
094: //if(buffer.buffer.length<len+5+9)
095: // System.err.println("buffer.buffer.length="+buffer.buffer.length+" len+5+9="+(len+5+9));
096: //if(buffer.buffer.length<s)
097: // System.err.println("buffer.buffer.length="+buffer.buffer.length+" s="+(s));
098: System.arraycopy(buffer.buffer, len + 5 + 9, buffer.buffer, s,
099: buffer.index - 5 - 9 - len);
100:
101: buffer.index = 10;
102: buffer.putInt(len);
103: buffer.index = len + 5 + 9;
104: return s;
105: }
106:
107: void unshift(byte command, int recipient, int s, int len) {
108: System.arraycopy(buffer.buffer, s, buffer.buffer, 5 + 9, len);
109: buffer.buffer[5] = command;
110: buffer.index = 6;
111: buffer.putInt(recipient);
112: buffer.putInt(len);
113: buffer.index = len + 5 + 9;
114: }
115:
116: Buffer getBuffer() {
117: return buffer;
118: }
119: }
|