changeset 326:97da8c5fb40a ganymed

pickup compression from trilead
author Carl Byington <carl@five-ten-sg.com>
date Thu, 31 Jul 2014 11:15:15 -0700
parents fe127b3c4b88
children 9a657362519c
files src/ch/ethz/ssh2/compression/CompressionFactory.java src/ch/ethz/ssh2/compression/Compressor.java src/ch/ethz/ssh2/compression/ICompressor.java src/ch/ethz/ssh2/compression/Zlib.java src/ch/ethz/ssh2/compression/ZlibOpenSSH.java
diffstat 5 files changed, 237 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/src/ch/ethz/ssh2/compression/CompressionFactory.java	Thu Jul 31 09:34:31 2014 -0700
+++ b/src/ch/ethz/ssh2/compression/CompressionFactory.java	Thu Jul 31 11:15:15 2014 -0700
@@ -1,11 +1,27 @@
+/*
+ * 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 ch.ethz.ssh2.compression;
 
-import java.util.ArrayList;
-import java.util.List;
+import java.util.Vector;
 
 /**
  * @author Kenny Root
- * @version $Id: CompressionFactory.java 156 2014-04-29 12:02:35Z dkocher@sudo.ch $
+ *
  */
 public class CompressionFactory {
     static class CompressorEntry {
@@ -18,41 +34,41 @@
         }
     }
 
-    private static final List<CompressorEntry> compressors
-        = new ArrayList<CompressorEntry>();
+    static Vector<CompressorEntry> compressors = new Vector<CompressorEntry>();
 
     static {
-        // Higher priority first
-        compressors.add(new CompressorEntry("none", null));
+        /* 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.get(i);
-            list[i] = ce.type;
+            CompressorEntry ce = compressors.elementAt(i);
+            list[i] = new String(ce.type);
         }
 
         return list;
     }
 
-    public static void checkCompressorList(String[] list) {
-        for (final String candidate : list) {
-            getEntry(candidate);
-        }
+    public static void checkCompressorList(String[] compressorCandidates) {
+        for (int i = 0; i < compressorCandidates.length; i++)
+            getEntry(compressorCandidates[i]);
     }
 
-    public static Compressor createCompressor(String type) {
+    public static ICompressor createCompressor(String type) {
         try {
             CompressorEntry ce = getEntry(type);
 
-            if (null == ce.compressorClass) {
+            if ("".equals(ce.compressorClass))
                 return null;
-            }
 
             Class<?> cc = Class.forName(ce.compressorClass);
-            return (Compressor) cc.newInstance();
+            ICompressor cmp = (ICompressor) cc.newInstance();
+            return cmp;
         }
         catch (Exception e) {
             throw new IllegalArgumentException("Cannot instantiate " + type);
@@ -60,12 +76,13 @@
     }
 
     private static CompressorEntry getEntry(String type) {
-        for (CompressorEntry ce : compressors) {
-            if (ce.type.equals(type)) {
+        for (int i = 0; i < compressors.size(); i++) {
+            CompressorEntry ce = compressors.elementAt(i);
+
+            if (ce.type.equals(type))
                 return ce;
-            }
         }
 
-        throw new IllegalArgumentException("Unknown algorithm " + type);
+        throw new IllegalArgumentException("Unkown algorithm " + type);
     }
-}
\ No newline at end of file
+}
--- a/src/ch/ethz/ssh2/compression/Compressor.java	Thu Jul 31 09:34:31 2014 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-package ch.ethz.ssh2.compression;
-
-import java.io.IOException;
-
-/**
- * @author Kenny Root
- * @version $Id: Compressor.java 151 2014-04-28 10:03:39Z dkocher@sudo.ch $
- */
-public interface Compressor {
-    int getBufferSize();
-
-    int compress(byte[] buf, int start, int len, byte[] output) throws IOException;
-
-    byte[] uncompress(byte[] buf, int start, int[] len) throws IOException;
-}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ch/ethz/ssh2/compression/ICompressor.java	Thu Jul 31 11:15:15 2014 -0700
@@ -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 ch.ethz.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();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ch/ethz/ssh2/compression/Zlib.java	Thu Jul 31 11:15:15 2014 -0700
@@ -0,0 +1,131 @@
+/*
+ * 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 ch.ethz.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;
+            }
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ch/ethz/ssh2/compression/ZlibOpenSSH.java	Thu Jul 31 11:15:15 2014 -0700
@@ -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 ch.ethz.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;
+    }
+
+}