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.jcraft;
31 import com.jcraft.jzlib.*;
32 import com.jcraft.jsch.*;
34 public class Compression implements com.jcraft.jsch.Compression {
35 static private final int BUF_SIZE=4096;
38 private ZStream stream;
39 private byte[] tmpbuf=new byte[BUF_SIZE];
45 public void init(int type, int level){
47 stream.deflateInit(level);
50 else if(type==INFLATER){
52 inflated_buf=new byte[BUF_SIZE];
57 static Compression getDeflater(int level){
58 Compression foo=new Compression();
59 foo.stream.deflateInit(level);
64 private byte[] inflated_buf;
66 static Compression getInflater(){
67 Compression foo=new Compression();
68 foo.stream.inflateInit();
69 foo.inflated_buf=new byte[BUF_SIZE];
75 public int compress(byte[] buf, int start, int len){
77 stream.next_in_index=start;
78 stream.avail_in=len-start;
83 stream.next_out=tmpbuf;
84 stream.next_out_index=0;
85 stream.avail_out=BUF_SIZE;
86 status=stream.deflate(JZlib.Z_PARTIAL_FLUSH);
89 System.arraycopy(tmpbuf, 0,
91 BUF_SIZE-stream.avail_out);
92 outputlen+=(BUF_SIZE-stream.avail_out);
95 System.err.println("compress: deflate returnd "+status);
98 while(stream.avail_out==0);
102 public byte[] uncompress(byte[] buffer, int start, int[] length){
105 stream.next_in=buffer;
106 stream.next_in_index=start;
107 stream.avail_in=length[0];
110 stream.next_out=tmpbuf;
111 stream.next_out_index=0;
112 stream.avail_out=BUF_SIZE;
113 int status=stream.inflate(JZlib.Z_PARTIAL_FLUSH);
116 if(inflated_buf.length<inflated_end+BUF_SIZE-stream.avail_out){
117 byte[] foo=new byte[inflated_end+BUF_SIZE-stream.avail_out];
118 System.arraycopy(inflated_buf, 0, foo, 0, inflated_end);
121 System.arraycopy(tmpbuf, 0,
122 inflated_buf, inflated_end,
123 BUF_SIZE-stream.avail_out);
124 inflated_end+=(BUF_SIZE-stream.avail_out);
125 length[0]=inflated_end;
127 case JZlib.Z_BUF_ERROR:
128 if(inflated_end>buffer.length-start){
129 byte[] foo=new byte[inflated_end+start];
130 System.arraycopy(buffer, 0, foo, 0, start);
131 System.arraycopy(inflated_buf, 0, foo, start, inflated_end);
135 System.arraycopy(inflated_buf, 0, buffer, start, inflated_end);
137 length[0]=inflated_end;
140 System.err.println("uncompress: inflate returnd "+status);