aboutsummaryrefslogtreecommitdiffstats
path: root/sshlib/src/main/java/com/trilead/ssh2/compression
diff options
context:
space:
mode:
authorKenny Root <kenny@the-b.org>2015-07-18 11:30:18 -0700
committerKenny Root <kenny@the-b.org>2015-07-18 11:30:18 -0700
commit2f5f3754dce85212a71138fd80c2300b73461908 (patch)
treeac51a1c9f143bc77a0cfec2e7da1b7c184394a98 /sshlib/src/main/java/com/trilead/ssh2/compression
parent0cf7ac30faecc82e04b080f418b08758624b07f5 (diff)
downloadsshlib-2f5f3754dce85212a71138fd80c2300b73461908.tar.gz
sshlib-2f5f3754dce85212a71138fd80c2300b73461908.tar.bz2
sshlib-2f5f3754dce85212a71138fd80c2300b73461908.zip
Rename project to sshlib
Diffstat (limited to 'sshlib/src/main/java/com/trilead/ssh2/compression')
-rw-r--r--sshlib/src/main/java/com/trilead/ssh2/compression/CompressionFactory.java96
-rw-r--r--sshlib/src/main/java/com/trilead/ssh2/compression/ICompressor.java32
-rw-r--r--sshlib/src/main/java/com/trilead/ssh2/compression/Zlib.java130
-rw-r--r--sshlib/src/main/java/com/trilead/ssh2/compression/ZlibOpenSSH.java35
4 files changed, 293 insertions, 0 deletions
diff --git a/sshlib/src/main/java/com/trilead/ssh2/compression/CompressionFactory.java b/sshlib/src/main/java/com/trilead/ssh2/compression/CompressionFactory.java
new file mode 100644
index 0000000..9f8d7ef
--- /dev/null
+++ b/sshlib/src/main/java/com/trilead/ssh2/compression/CompressionFactory.java
@@ -0,0 +1,96 @@
+/*
+ * ConnectBot: simple, powerful, open-source SSH client for Android
+ * Copyright 2007 Kenny Root, Jeffrey Sharkey
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.trilead.ssh2.compression;
+
+import java.util.Vector;
+
+/**
+ * @author Kenny Root
+ *
+ */
+public class CompressionFactory {
+ static class CompressorEntry
+ {
+ String type;
+ String compressorClass;
+
+ public CompressorEntry(String type, String compressorClass)
+ {
+ this.type = type;
+ this.compressorClass = compressorClass;
+ }
+ }
+
+ static Vector<CompressorEntry> compressors = new Vector<CompressorEntry>();
+
+ static
+ {
+ /* Higher Priority First */
+
+ compressors.addElement(new CompressorEntry("zlib", "com.trilead.ssh2.compression.Zlib"));
+ compressors.addElement(new CompressorEntry("zlib@openssh.com", "com.trilead.ssh2.compression.ZlibOpenSSH"));
+ compressors.addElement(new CompressorEntry("none", ""));
+ }
+
+ public static String[] getDefaultCompressorList()
+ {
+ String list[] = new String[compressors.size()];
+ for (int i = 0; i < compressors.size(); i++)
+ {
+ CompressorEntry ce = compressors.elementAt(i);
+ list[i] = new String(ce.type);
+ }
+ return list;
+ }
+
+ public static void checkCompressorList(String[] compressorCandidates)
+ {
+ for (int i = 0; i < compressorCandidates.length; i++)
+ getEntry(compressorCandidates[i]);
+ }
+
+ public static ICompressor createCompressor(String type)
+ {
+ try
+ {
+ CompressorEntry ce = getEntry(type);
+ if ("".equals(ce.compressorClass))
+ return null;
+
+ Class<?> cc = Class.forName(ce.compressorClass);
+ ICompressor cmp = (ICompressor) cc.newInstance();
+
+ return cmp;
+ }
+ catch (Exception e)
+ {
+ throw new IllegalArgumentException("Cannot instantiate " + type);
+ }
+ }
+
+ private static CompressorEntry getEntry(String type)
+ {
+ for (int i = 0; i < compressors.size(); i++)
+ {
+ CompressorEntry ce = compressors.elementAt(i);
+ if (ce.type.equals(type))
+ return ce;
+ }
+ throw new IllegalArgumentException("Unkown algorithm " + type);
+ }
+}
diff --git a/sshlib/src/main/java/com/trilead/ssh2/compression/ICompressor.java b/sshlib/src/main/java/com/trilead/ssh2/compression/ICompressor.java
new file mode 100644
index 0000000..0b435b9
--- /dev/null
+++ b/sshlib/src/main/java/com/trilead/ssh2/compression/ICompressor.java
@@ -0,0 +1,32 @@
+/*
+ * ConnectBot: simple, powerful, open-source SSH client for Android
+ * Copyright 2007 Kenny Root, Jeffrey Sharkey
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.trilead.ssh2.compression;
+
+/**
+ * @author Kenny Root
+ *
+ */
+public interface ICompressor {
+ int getBufferSize();
+
+ int compress(byte[] buf, int start, int len, byte[] output);
+
+ byte[] uncompress(byte[] buf, int start, int[] len);
+
+ boolean canCompressPreauth();
+}
diff --git a/sshlib/src/main/java/com/trilead/ssh2/compression/Zlib.java b/sshlib/src/main/java/com/trilead/ssh2/compression/Zlib.java
new file mode 100644
index 0000000..c1203a3
--- /dev/null
+++ b/sshlib/src/main/java/com/trilead/ssh2/compression/Zlib.java
@@ -0,0 +1,130 @@
+/*
+ * ConnectBot: simple, powerful, open-source SSH client for Android
+ * Copyright 2007 Kenny Root, Jeffrey Sharkey
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.trilead.ssh2.compression;
+
+import com.jcraft.jzlib.JZlib;
+import com.jcraft.jzlib.ZStream;
+
+/**
+ * @author Kenny Root
+ *
+ */
+public class Zlib implements ICompressor {
+ static private final int DEFAULT_BUF_SIZE = 4096;
+ static private final int LEVEL = 5;
+
+ private ZStream deflate;
+ private byte[] deflate_tmpbuf;
+
+ private ZStream inflate;
+ private byte[] inflate_tmpbuf;
+ private byte[] inflated_buf;
+
+ public Zlib() {
+ deflate = new ZStream();
+ inflate = new ZStream();
+
+ deflate.deflateInit(LEVEL);
+ inflate.inflateInit();
+
+ deflate_tmpbuf = new byte[DEFAULT_BUF_SIZE];
+ inflate_tmpbuf = new byte[DEFAULT_BUF_SIZE];
+ inflated_buf = new byte[DEFAULT_BUF_SIZE];
+ }
+
+ public boolean canCompressPreauth() {
+ return true;
+ }
+
+ public int getBufferSize() {
+ return DEFAULT_BUF_SIZE;
+ }
+
+ public int compress(byte[] buf, int start, int len, byte[] output) {
+ deflate.next_in = buf;
+ deflate.next_in_index = start;
+ deflate.avail_in = len - start;
+
+ if ((buf.length + 1024) > deflate_tmpbuf.length) {
+ deflate_tmpbuf = new byte[buf.length + 1024];
+ }
+
+ deflate.next_out = deflate_tmpbuf;
+ deflate.next_out_index = 0;
+ deflate.avail_out = output.length;
+
+ if (deflate.deflate(JZlib.Z_PARTIAL_FLUSH) != JZlib.Z_OK) {
+ System.err.println("compress: compression failure");
+ }
+
+ if (deflate.avail_in > 0) {
+ System.err.println("compress: deflated data too large");
+ }
+
+ int outputlen = output.length - deflate.avail_out;
+
+ System.arraycopy(deflate_tmpbuf, 0, output, 0, outputlen);
+
+ return outputlen;
+ }
+
+ public byte[] uncompress(byte[] buffer, int start, int[] length) {
+ int inflated_end = 0;
+
+ inflate.next_in = buffer;
+ inflate.next_in_index = start;
+ inflate.avail_in = length[0];
+
+ while (true) {
+ inflate.next_out = inflate_tmpbuf;
+ inflate.next_out_index = 0;
+ inflate.avail_out = DEFAULT_BUF_SIZE;
+ int status = inflate.inflate(JZlib.Z_PARTIAL_FLUSH);
+ switch (status) {
+ case JZlib.Z_OK:
+ if (inflated_buf.length < inflated_end + DEFAULT_BUF_SIZE
+ - inflate.avail_out) {
+ byte[] foo = new byte[inflated_end + DEFAULT_BUF_SIZE
+ - inflate.avail_out];
+ System.arraycopy(inflated_buf, 0, foo, 0, inflated_end);
+ inflated_buf = foo;
+ }
+ System.arraycopy(inflate_tmpbuf, 0, inflated_buf, inflated_end,
+ DEFAULT_BUF_SIZE - inflate.avail_out);
+ inflated_end += (DEFAULT_BUF_SIZE - inflate.avail_out);
+ length[0] = inflated_end;
+ break;
+ case JZlib.Z_BUF_ERROR:
+ if (inflated_end > buffer.length - start) {
+ byte[] foo = new byte[inflated_end + start];
+ System.arraycopy(buffer, 0, foo, 0, start);
+ System.arraycopy(inflated_buf, 0, foo, start, inflated_end);
+ buffer = foo;
+ } else {
+ System.arraycopy(inflated_buf, 0, buffer, start,
+ inflated_end);
+ }
+ length[0] = inflated_end;
+ return buffer;
+ default:
+ System.err.println("uncompress: inflate returnd " + status);
+ return null;
+ }
+ }
+ }
+}
diff --git a/sshlib/src/main/java/com/trilead/ssh2/compression/ZlibOpenSSH.java b/sshlib/src/main/java/com/trilead/ssh2/compression/ZlibOpenSSH.java
new file mode 100644
index 0000000..266fff9
--- /dev/null
+++ b/sshlib/src/main/java/com/trilead/ssh2/compression/ZlibOpenSSH.java
@@ -0,0 +1,35 @@
+/*
+ * ConnectBot: simple, powerful, open-source SSH client for Android
+ * Copyright 2007 Kenny Root, Jeffrey Sharkey
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.trilead.ssh2.compression;
+
+/**
+ * Defines how zlib@openssh.org compression works.
+ * See
+ * http://www.openssh.org/txt/draft-miller-secsh-compression-delayed-00.txt
+ * compression is disabled until userauth has occurred.
+ *
+ * @author Matt Johnston
+ *
+ */
+public class ZlibOpenSSH extends Zlib {
+
+ public boolean canCompressPreauth() {
+ return false;
+ }
+
+}