]> Joshua Wise's Git repositories - dumload.git/blob - src/com/jcraft/jsch/jcraft/Compression.java
Initial commit.
[dumload.git] / src / com / jcraft / jsch / jcraft / Compression.java
1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
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.
14
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.
17
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.
28 */
29
30 package com.jcraft.jsch.jcraft;
31 import com.jcraft.jzlib.*;
32 import com.jcraft.jsch.*;
33
34 public class Compression implements com.jcraft.jsch.Compression {
35   static private final int BUF_SIZE=4096;
36
37   private int type;
38   private ZStream stream;
39   private byte[] tmpbuf=new byte[BUF_SIZE];
40
41   public Compression(){
42     stream=new ZStream();
43   }
44
45   public void init(int type, int level){
46     if(type==DEFLATER){
47       stream.deflateInit(level);
48       this.type=DEFLATER;
49     }
50     else if(type==INFLATER){
51       stream.inflateInit();
52       inflated_buf=new byte[BUF_SIZE];
53       this.type=INFLATER;
54     }
55   }
56   /*
57   static Compression getDeflater(int level){
58     Compression foo=new Compression();
59     foo.stream.deflateInit(level);
60     foo.type=DEFLATER;
61     return foo;
62   }
63   */
64   private byte[] inflated_buf;
65   /*
66   static Compression getInflater(){
67     Compression foo=new Compression();
68     foo.stream.inflateInit();
69     foo.inflated_buf=new byte[BUF_SIZE];
70     foo.type=INFLATER;
71     return foo;
72   }
73   */
74
75   public int compress(byte[] buf, int start, int len){
76     stream.next_in=buf;
77     stream.next_in_index=start;
78     stream.avail_in=len-start;
79     int status;
80     int outputlen=start;
81
82     do{
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);
87       switch(status){
88         case JZlib.Z_OK:
89             System.arraycopy(tmpbuf, 0,
90                              buf, outputlen,
91                              BUF_SIZE-stream.avail_out);
92             outputlen+=(BUF_SIZE-stream.avail_out);
93             break;
94         default:
95             System.err.println("compress: deflate returnd "+status);
96       }
97     }
98     while(stream.avail_out==0);
99     return outputlen;
100   }
101
102   public byte[] uncompress(byte[] buffer, int start, int[] length){
103     int inflated_end=0;
104
105     stream.next_in=buffer;
106     stream.next_in_index=start;
107     stream.avail_in=length[0];
108
109     while(true){
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);
114       switch(status){
115         case JZlib.Z_OK:
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);
119             inflated_buf=foo;
120           }
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;
126           break;
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);
132             buffer=foo;
133           }
134           else{
135             System.arraycopy(inflated_buf, 0, buffer, start, inflated_end);
136           }
137           length[0]=inflated_end;
138           return buffer;
139         default:
140           System.err.println("uncompress: inflate returnd "+status);
141           return null;
142       }
143     }
144   }
145 }
This page took 0.030941 seconds and 4 git commands to generate.