Skip to content

Commit ea72a9e

Browse files
committed
[SSHD-339] Remove JCraft Jzlib dependency by implementing the Compression(s) in pure Java
1 parent 041b751 commit ea72a9e

File tree

2 files changed

+23
-49
lines changed

2 files changed

+23
-49
lines changed

sshd-core/pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@
4848
<artifactId>tomcat-apr</artifactId>
4949
<optional>true</optional>
5050
</dependency>
51-
<dependency>
52-
<groupId>com.jcraft</groupId>
53-
<artifactId>jzlib</artifactId>
54-
<optional>true</optional>
55-
</dependency>
5651
<dependency>
5752
<groupId>org.bouncycastle</groupId>
5853
<artifactId>bcpg-jdk15on</artifactId>
@@ -73,6 +68,11 @@
7368
<artifactId>jsch</artifactId>
7469
<scope>test</scope>
7570
</dependency>
71+
<dependency>
72+
<groupId>com.jcraft</groupId>
73+
<artifactId>jzlib</artifactId>
74+
<scope>test</scope>
75+
</dependency>
7676
<dependency>
7777
<groupId>org.springframework</groupId>
7878
<artifactId>spring-context</artifactId>

sshd-core/src/main/java/org/apache/sshd/common/compression/CompressionZlib.java

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@
1919
package org.apache.sshd.common.compression;
2020

2121
import java.io.IOException;
22+
import java.util.zip.DataFormatException;
23+
import java.util.zip.Deflater;
24+
import java.util.zip.Inflater;
2225

23-
import com.jcraft.jzlib.JZlib;
24-
import com.jcraft.jzlib.ZStream;
2526
import org.apache.sshd.common.Compression;
2627
import org.apache.sshd.common.NamedFactory;
27-
import org.apache.sshd.common.SshConstants;
28-
import org.apache.sshd.common.SshException;
2928
import org.apache.sshd.common.util.Buffer;
3029

3130
/**
@@ -50,8 +49,9 @@ public Compression create() {
5049

5150
static private final int BUF_SIZE = 4096;
5251

53-
private ZStream stream;
5452
private byte[] tmpbuf = new byte[BUF_SIZE];
53+
private Deflater compresser;
54+
private Inflater decompresser;
5555

5656
/**
5757
* Create a new instance of a ZLib base compression
@@ -64,54 +64,28 @@ public boolean isDelayed() {
6464
}
6565

6666
public void init(Type type, int level) {
67-
stream = new ZStream();
68-
if (type == Type.Deflater) {
69-
stream.deflateInit(level);
70-
} else {
71-
stream.inflateInit();
72-
}
67+
compresser = new Deflater(level);
68+
decompresser = new Inflater();
7369
}
7470

7571
public void compress(Buffer buffer) throws IOException {
76-
stream.next_in = buffer.array();
77-
stream.next_in_index = buffer.rpos();
78-
stream.avail_in = buffer.available();
72+
compresser.setInput(buffer.array(), buffer.rpos(), buffer.available());
7973
buffer.wpos(buffer.rpos());
80-
do {
81-
stream.next_out = tmpbuf;
82-
stream.next_out_index = 0;
83-
stream.avail_out = BUF_SIZE;
84-
int status = stream.deflate(JZlib.Z_PARTIAL_FLUSH);
85-
switch (status) {
86-
case JZlib.Z_OK:
87-
buffer.putRawBytes(tmpbuf, 0, BUF_SIZE - stream.avail_out);
88-
break;
89-
default:
90-
throw new SshException(SshConstants.SSH2_DISCONNECT_COMPRESSION_ERROR, "compress: deflate returned " + status);
91-
}
74+
int len;
75+
while ((len = compresser.deflate(tmpbuf, 0, tmpbuf.length, Deflater.SYNC_FLUSH)) > 0) {
76+
buffer.putRawBytes(tmpbuf, 0, len);
9277
}
93-
while (stream.avail_out == 0);
9478
}
9579

9680
public void uncompress(Buffer from, Buffer to) throws IOException {
97-
stream.next_in = from.array();
98-
stream.next_in_index = from.rpos();
99-
stream.avail_in = from.available();
100-
101-
while (true) {
102-
stream.next_out = tmpbuf;
103-
stream.next_out_index = 0;
104-
stream.avail_out = BUF_SIZE;
105-
int status = stream.inflate(JZlib.Z_PARTIAL_FLUSH);
106-
switch (status) {
107-
case JZlib.Z_OK:
108-
to.putRawBytes(tmpbuf, 0, BUF_SIZE - stream.avail_out);
109-
break;
110-
case JZlib.Z_BUF_ERROR:
111-
return;
112-
default:
113-
throw new SshException(SshConstants.SSH2_DISCONNECT_COMPRESSION_ERROR, "uncompress: inflate returned " + status);
81+
decompresser.setInput(from.array(), from.rpos(), from.available());
82+
int len;
83+
try {
84+
while ((len = decompresser.inflate(tmpbuf)) > 0) {
85+
to.putRawBytes(tmpbuf, 0, len);
11486
}
87+
} catch (DataFormatException e) {
88+
throw new IOException("Error decompressing data", e);
11589
}
11690
}
11791

0 commit comments

Comments
 (0)