annotate app/src/main/java/ch/ethz/ssh2/compression/Zlib.java @ 519:609c9990c9fb

add more debug code
author Carl Byington <carl@five-ten-sg.com>
date Fri, 31 May 2024 14:35:00 -0600
parents d29cce60f393
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
326
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1 /*
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2 * ConnectBot: simple, powerful, open-source SSH client for Android
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
3 * Copyright 2007 Kenny Root, Jeffrey Sharkey
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
4 *
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5 * Licensed under the Apache License, Version 2.0 (the "License");
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6 * you may not use this file except in compliance with the License.
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7 * You may obtain a copy of the License at
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8 *
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9 * http://www.apache.org/licenses/LICENSE-2.0
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10 *
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11 * Unless required by applicable law or agreed to in writing, software
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 * distributed under the License is distributed on an "AS IS" BASIS,
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14 * See the License for the specific language governing permissions and
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15 * limitations under the License.
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16 */
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18 package ch.ethz.ssh2.compression;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20 import com.jcraft.jzlib.JZlib;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21 import com.jcraft.jzlib.ZStream;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
23 /**
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24 * @author Kenny Root
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
25 *
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
26 */
327
9a657362519c pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents: 326
diff changeset
27 public class Zlib implements Compressor {
326
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28 static private final int DEFAULT_BUF_SIZE = 4096;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29 static private final int LEVEL = 5;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31 private ZStream deflate;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32 private byte[] deflate_tmpbuf;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
33
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
34 private ZStream inflate;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
35 private byte[] inflate_tmpbuf;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
36 private byte[] inflated_buf;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
38 public Zlib() {
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39 deflate = new ZStream();
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
40 inflate = new ZStream();
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
41 deflate.deflateInit(LEVEL);
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
42 inflate.inflateInit();
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
43 deflate_tmpbuf = new byte[DEFAULT_BUF_SIZE];
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
44 inflate_tmpbuf = new byte[DEFAULT_BUF_SIZE];
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
45 inflated_buf = new byte[DEFAULT_BUF_SIZE];
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
46 }
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
47
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
48 public boolean canCompressPreauth() {
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
49 return true;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
50 }
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
51
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
52 public int getBufferSize() {
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53 return DEFAULT_BUF_SIZE;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
54 }
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
55
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
56 public int compress(byte[] buf, int start, int len, byte[] output) {
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
57 deflate.next_in = buf;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
58 deflate.next_in_index = start;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
59 deflate.avail_in = len - start;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
61 if ((buf.length + 1024) > deflate_tmpbuf.length) {
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
62 deflate_tmpbuf = new byte[buf.length + 1024];
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
63 }
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
64
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
65 deflate.next_out = deflate_tmpbuf;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
66 deflate.next_out_index = 0;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
67 deflate.avail_out = output.length;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
68
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
69 if (deflate.deflate(JZlib.Z_PARTIAL_FLUSH) != JZlib.Z_OK) {
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
70 System.err.println("compress: compression failure");
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
71 }
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
72
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
73 if (deflate.avail_in > 0) {
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
74 System.err.println("compress: deflated data too large");
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
75 }
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
76
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
77 int outputlen = output.length - deflate.avail_out;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
78 System.arraycopy(deflate_tmpbuf, 0, output, 0, outputlen);
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
79 return outputlen;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
80 }
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
81
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
82 public byte[] uncompress(byte[] buffer, int start, int[] length) {
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
83 int inflated_end = 0;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
84 inflate.next_in = buffer;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
85 inflate.next_in_index = start;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
86 inflate.avail_in = length[0];
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
87
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
88 while (true) {
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
89 inflate.next_out = inflate_tmpbuf;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
90 inflate.next_out_index = 0;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
91 inflate.avail_out = DEFAULT_BUF_SIZE;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
92 int status = inflate.inflate(JZlib.Z_PARTIAL_FLUSH);
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
93
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
94 switch (status) {
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
95 case JZlib.Z_OK:
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
96 if (inflated_buf.length < inflated_end + DEFAULT_BUF_SIZE
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
97 - inflate.avail_out) {
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
98 byte[] foo = new byte[inflated_end + DEFAULT_BUF_SIZE
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
99 - inflate.avail_out];
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
100 System.arraycopy(inflated_buf, 0, foo, 0, inflated_end);
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
101 inflated_buf = foo;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
102 }
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
103
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
104 System.arraycopy(inflate_tmpbuf, 0, inflated_buf, inflated_end,
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
105 DEFAULT_BUF_SIZE - inflate.avail_out);
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
106 inflated_end += (DEFAULT_BUF_SIZE - inflate.avail_out);
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
107 length[0] = inflated_end;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
108 break;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
109
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
110 case JZlib.Z_BUF_ERROR:
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
111 if (inflated_end > buffer.length - start) {
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
112 byte[] foo = new byte[inflated_end + start];
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
113 System.arraycopy(buffer, 0, foo, 0, start);
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
114 System.arraycopy(inflated_buf, 0, foo, start, inflated_end);
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
115 buffer = foo;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
116 }
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
117 else {
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
118 System.arraycopy(inflated_buf, 0, buffer, start,
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
119 inflated_end);
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
120 }
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
121
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
122 length[0] = inflated_end;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
123 return buffer;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
124
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
125 default:
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
126 System.err.println("uncompress: inflate returnd " + status);
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
127 return null;
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
128 }
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
129 }
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
130 }
97da8c5fb40a pickup compression from trilead
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
131 }