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.jcraft;
031:
032: import com.jcraft.jzlib.*;
033: import com.jcraft.jsch.*;
034:
035: public class Compression implements com.jcraft.jsch.Compression {
036: static private final int BUF_SIZE = 4096;
037:
038: private int type;
039: private ZStream stream;
040: private byte[] tmpbuf = new byte[BUF_SIZE];
041:
042: public Compression() {
043: stream = new ZStream();
044: }
045:
046: public void init(int type, int level) {
047: if (type == DEFLATER) {
048: stream.deflateInit(level);
049: this .type = DEFLATER;
050: } else if (type == INFLATER) {
051: stream.inflateInit();
052: inflated_buf = new byte[BUF_SIZE];
053: this .type = INFLATER;
054: }
055: }
056:
057: /*
058: static Compression getDeflater(int level){
059: Compression foo=new Compression();
060: foo.stream.deflateInit(level);
061: foo.type=DEFLATER;
062: return foo;
063: }
064: */
065: private byte[] inflated_buf;
066:
067: /*
068: static Compression getInflater(){
069: Compression foo=new Compression();
070: foo.stream.inflateInit();
071: foo.inflated_buf=new byte[BUF_SIZE];
072: foo.type=INFLATER;
073: return foo;
074: }
075: */
076:
077: public int compress(byte[] buf, int start, int len) {
078: stream.next_in = buf;
079: stream.next_in_index = start;
080: stream.avail_in = len - start;
081: int status;
082: int outputlen = start;
083:
084: do {
085: stream.next_out = tmpbuf;
086: stream.next_out_index = 0;
087: stream.avail_out = BUF_SIZE;
088: status = stream.deflate(JZlib.Z_PARTIAL_FLUSH);
089: switch (status) {
090: case JZlib.Z_OK:
091: System.arraycopy(tmpbuf, 0, buf, outputlen, BUF_SIZE
092: - stream.avail_out);
093: outputlen += (BUF_SIZE - stream.avail_out);
094: break;
095: default:
096: System.err.println("compress: deflate returnd "
097: + status);
098: }
099: } while (stream.avail_out == 0);
100: return outputlen;
101: }
102:
103: public byte[] uncompress(byte[] buffer, int start, int[] length) {
104: int inflated_end = 0;
105:
106: stream.next_in = buffer;
107: stream.next_in_index = start;
108: stream.avail_in = length[0];
109:
110: while (true) {
111: stream.next_out = tmpbuf;
112: stream.next_out_index = 0;
113: stream.avail_out = BUF_SIZE;
114: int status = stream.inflate(JZlib.Z_PARTIAL_FLUSH);
115: switch (status) {
116: case JZlib.Z_OK:
117: if (inflated_buf.length < inflated_end + BUF_SIZE
118: - stream.avail_out) {
119: byte[] foo = new byte[inflated_end + BUF_SIZE
120: - stream.avail_out];
121: System.arraycopy(inflated_buf, 0, foo, 0,
122: inflated_end);
123: inflated_buf = foo;
124: }
125: System.arraycopy(tmpbuf, 0, inflated_buf, inflated_end,
126: BUF_SIZE - stream.avail_out);
127: inflated_end += (BUF_SIZE - stream.avail_out);
128: length[0] = inflated_end;
129: break;
130: case JZlib.Z_BUF_ERROR:
131: if (inflated_end > buffer.length - start) {
132: byte[] foo = new byte[inflated_end + start];
133: System.arraycopy(buffer, 0, foo, 0, start);
134: System.arraycopy(inflated_buf, 0, foo, start,
135: inflated_end);
136: buffer = foo;
137: } else {
138: System.arraycopy(inflated_buf, 0, buffer, start,
139: inflated_end);
140: }
141: length[0] = inflated_end;
142: return buffer;
143: default:
144: System.err.println("uncompress: inflate returnd "
145: + status);
146: return null;
147: }
148: }
149: }
150: }
|