diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index b8234a5b..91d6579c 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -41,7 +41,7 @@ jobs: java-version: ${{ matrix.java }} check-latest: true - name: Test with Maven - run: mvn -B -P coverage verify -Denforcer.skip=true -DskipITs=false --file pom.xml + run: mvn -B -P coverage verify -Denforcer.skip=true -Dmaven.resources.skip=true -Dmaven.main.skip=true -Dassembly.skipAssembly=true -Dmaven.javadoc.skip=true -DskipITs=false --file pom.xml - uses: actions/upload-artifact@v2 with: name: java-${{ matrix.java }}-testresults diff --git a/.gitignore b/.gitignore index 68510312..606ae2fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # IDE files # *.geany nb-configuration.xml +.flattened-pom.xml # Created by https://www.toptal.com/developers/gitignore/api/intellij+all,netbeans,eclipse,visualstudiocode,vim,emacs,macos,windows,linux,java,maven # Edit at https://www.toptal.com/developers/gitignore?templates=intellij+all,netbeans,eclipse,visualstudiocode,vim,emacs,macos,windows,linux,java,maven diff --git a/ChangeLog.md b/ChangeLog.md index 66126dce..bc7c8638 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,3 +1,16 @@ +* [0.2.0](https://github.com/mwiede/jsch/releases/tag/jsch-0.2.0) + * Disable RSA/SHA1 signature algorithm by default [#75](https://github.com/mwiede/jsch/issues/75) + * Add basic Logger implementations that can be optionally utilized with `JSch.setLogger()`: + * JulLogger, using `java.util.logging.Logger` + * JplLogger, using [Java 9's JEP 264](https://openjdk.java.net/jeps/264) + * Log4j2Logger, using [Apache Log4j 2](https://logging.apache.org/log4j/2.x/) + * Slf4jLogger, using [SLF4J](https://www.slf4j.org/) + * Fix client version to be compliant with [RFC 4253 section 4.2](https://datatracker.ietf.org/doc/html/rfc4253#section-4.2) by not including minus sign characters [#115](https://github.com/mwiede/jsch/issues/115) + * Add `java.util.zip` based compression implementation [#114](https://github.com/mwiede/jsch/issues/114) + * This is based upon the [CompressionJUZ implementation](http://www.jcraft.com/jsch/examples/CompressionJUZ.java) posted to the [JSch-users mailing list](https://sourceforge.net/p/jsch/mailman/jsch-users/thread/201202031343.WAA19979%40jcraft.com/#msg28781313) in 2012 by the original JSch author + * The existing JZlib implementation remains the default to maintain strict [RFC 4253 section 6.2](https://datatracker.ietf.org/doc/html/rfc4253#section-6.2) compliance + * To use the new implementation globally, execute `JSch.setConfig("zlib@openssh.com", "com.jcraft.jsch.juz.Compression")` + `JSch.setConfig("zlib", "com.jcraft.jsch.juz.Compression")` + * To use the new implementation per session, execute `session.setConfig("zlib@openssh.com", "com.jcraft.jsch.juz.Compression")` + `session.setConfig("zlib", "com.jcraft.jsch.juz.Compression")` * [0.1.72](https://github.com/mwiede/jsch/releases/tag/jsch-0.1.72) * Switch chacha20-poly1305@openssh.com algorithm to a pure [Bouncy Castle](https://www.bouncycastle.org/java.html) based implementation * implement openssh config behavior to handle append, prepend and removal of algorithms [#104](https://github.com/mwiede/jsch/pull/104) diff --git a/Readme.md b/Readme.md index 49ff4d44..dded0687 100644 --- a/Readme.md +++ b/Readme.md @@ -67,6 +67,16 @@ As I explained in a [blog post](http://www.matez.de/index.php/2020/06/22/the-fut * In order to use chacha20-poly1305@openssh.com, you must add [Bouncy Castle](https://www.bouncycastle.org/java.html) (bcprov-jdk15on) to the classpath. * As of the [0.1.66](https://github.com/mwiede/jsch/releases/tag/jsch-0.1.66) release, these algorithms can now be used with older Java releases if [Bouncy Castle](https://www.bouncycastle.org/java.html) (bcprov-jdk15on) is added to the classpath. * As of the [0.1.72](https://github.com/mwiede/jsch/releases/tag/jsch-0.1.72) release, chacha20-poly1305@openssh.com can only be used if [Bouncy Castle](https://www.bouncycastle.org/java.html) (bcprov-jdk15on) is added to the classpath. +* Why do ssh-rsa type keys not work with this JSch fork and my server? + * As of the [0.2.0](https://github.com/mwiede/jsch/releases/tag/jsch-0.2.0) release, the RSA/SHA1 signature algorithm is disabled by default. + * SHA1 is no longer considered secure by the general cryptographic community and this JSch fork strives to maintain secure choices for default algorithms that it will utilize. + * This also follows the lead of the OpenSSH project in which they disabled RSA/SHA1 signatures by default as of [OpenSSH release 8.8](https://www.openssh.com/txt/release-8.8). + * ssh-rsa type keys continue to function by default with the RSA/SHA256 (rsa-sha2-256) & RSA/SHA512 (rsa-sha2-512) signature algorithms defined by (RFC 8332)[https://datatracker.ietf.org/doc/html/rfc8332]. + * If your server only supports RSA/SHA1 signatures and you require their use in your application, then you will need to manually reenable them by one of the following means: + * Globally by adding "ssh-rsa" to the `jsch.server_host_key` + `jsch.client_pubkey` properties. + * Globally by executing something similar to `JSch.setConfig("server_host_key", JSch.getConfig("server_host_key") + ",ssh-rsa")` + `JSch.setConfig("PubkeyAcceptedAlgorithms", JSch.getConfig("PubkeyAcceptedAlgorithms") + ",ssh-rsa")`. + * On a per-session basis by executing something similar to `session.setConfig("server_host_key", session.getConfig("server_host_key") + ",ssh-rsa")` + `session.setConfig("PubkeyAcceptedAlgorithms", session.getConfig("PubkeyAcceptedAlgorithms") + ",ssh-rsa")`. + * Adding "ssh-rsa" to your OpenSSH type config file with the "HostKeyAlgorithms" + "PubkeyAcceptedAlgorithms" keywords & then utilizing the `OpenSSHConfig` class. ## Changes since fork: See [ChangeLog.md](ChangeLog.md) diff --git a/examples/AES.java b/examples/AES.java index 0b3e80ea..4f2fadad 100644 --- a/examples/AES.java +++ b/examples/AES.java @@ -137,7 +137,7 @@ public String[] promptKeyboardInteractive(String destination, for(int i=0; i "); - cmds.removeAllElements(); + cmds.removeAllElements(); i=in.read(buf, 0, 1024); - if(i<=0)break; + if(i<=0)break; i--; if(i>0 && buf[i-1]==0x0d)i--; //str=new String(buf, 0, i); //System.out.println("|"+str+"|"); - int s=0; - for(int ii=0; ii0){ cmds.addElement(new String(buf, s, ii-s)); } - while(ii'7'){foo=-1; break;} - foo<<=3; - foo|=(k-'0'); - } - if(foo==-1)continue; - } - else{ - try{foo=Integer.parseInt((String)cmds.elementAt(1));} - catch(Exception e){continue;} - } - try{ - if(cmd.equals("chgrp")){ c.chgrp(foo, path); } - else if(cmd.equals("chown")){ c.chown(foo, path); } - else if(cmd.equals("chmod")){ c.chmod(foo, path); } - } - catch(SftpException e){ - System.out.println(e.toString()); - } - continue; - } - if(cmd.equals("pwd") || cmd.equals("lpwd")){ + if(k<'0'||k>'7'){foo=-1; break;} + foo<<=3; + foo|=(k-'0'); + } + if(foo==-1)continue; + } + else{ + try{foo=Integer.parseInt((String)cmds.elementAt(1));} + catch(Exception e){continue;} + } + try{ + if(cmd.equals("chgrp")){ c.chgrp(foo, path); } + else if(cmd.equals("chown")){ c.chown(foo, path); } + else if(cmd.equals("chmod")){ c.chmod(foo, path); } + } + catch(SftpException e){ + System.out.println(e.toString()); + } + continue; + } + if(cmd.equals("pwd") || cmd.equals("lpwd")){ str=(cmd.equals("pwd")?"Remote":"Local"); - str+=" working directory: "; + str+=" working directory: "; if(cmd.equals("pwd")) str+=c.pwd(); - else str+=c.lpwd(); - out.println(str); - continue; - } - if(cmd.equals("ls") || cmd.equals("dir")){ - String path="."; - if(cmds.size()==2) path=(String)cmds.elementAt(1); - try{ - java.util.Vector vv=c.ls(path); - if(vv!=null){ - for(int ii=0; ii2) continue; String p1 = cmds.size()==1 ? ".": (String)cmds.elementAt(1); SftpStatVFS stat = c.statVFS(p1); @@ -279,58 +279,58 @@ public static void main(String[] arg){ continue; } - if(cmd.equals("stat") || cmd.equals("lstat")){ + if(cmd.equals("stat") || cmd.equals("lstat")){ if(cmds.size()!=2) continue; - String p1=(String)cmds.elementAt(1); - SftpATTRS attrs=null; - try{ - if(cmd.equals("stat")) attrs=c.stat(p1); - else attrs=c.lstat(p1); - } - catch(SftpException e){ - System.out.println(e.toString()); - } - if(attrs!=null){ + String p1=(String)cmds.elementAt(1); + SftpATTRS attrs=null; + try{ + if(cmd.equals("stat")) attrs=c.stat(p1); + else attrs=c.lstat(p1); + } + catch(SftpException e){ + System.out.println(e.toString()); + } + if(attrs!=null){ out.println(attrs); - } - else{ - } - continue; - } - if(cmd.equals("readlink")){ + } + else{ + } + continue; + } + if(cmd.equals("readlink")){ if(cmds.size()!=2) continue; - String p1=(String)cmds.elementAt(1); - String filename=null; - try{ - filename=c.readlink(p1); + String p1=(String)cmds.elementAt(1); + String filename=null; + try{ + filename=c.readlink(p1); out.println(filename); - } - catch(SftpException e){ - System.out.println(e.toString()); - } - continue; - } - if(cmd.equals("realpath")){ + } + catch(SftpException e){ + System.out.println(e.toString()); + } + continue; + } + if(cmd.equals("realpath")){ if(cmds.size()!=2) continue; - String p1=(String)cmds.elementAt(1); - String filename=null; - try{ - filename=c.realpath(p1); + String p1=(String)cmds.elementAt(1); + String filename=null; + try{ + filename=c.realpath(p1); out.println(filename); - } - catch(SftpException e){ - System.out.println(e.toString()); - } - continue; - } - if(cmd.equals("version")){ - out.println("SFTP protocol version "+c.version()); - continue; - } - if(cmd.equals("help") || cmd.equals("?")){ - out.println(help); - continue; - } + } + catch(SftpException e){ + System.out.println(e.toString()); + } + continue; + } + if(cmd.equals("version")){ + out.println("SFTP protocol version "+c.version()); + continue; + } + if(cmd.equals("help") || cmd.equals("?")){ + out.println(help); + continue; + } out.println("unimplemented command: "+cmd); } session.disconnect(); @@ -362,11 +362,11 @@ public boolean promptYesNo(String str){ public boolean promptPassword(String message){ Object[] ob={passwordField}; int result= - JOptionPane.showConfirmDialog(null, ob, message, - JOptionPane.OK_CANCEL_OPTION); + JOptionPane.showConfirmDialog(null, ob, message, + JOptionPane.OK_CANCEL_OPTION); if(result==JOptionPane.OK_OPTION){ - passwd=passwordField.getText(); - return true; + passwd=passwordField.getText(); + return true; } else{ return false; } } @@ -424,7 +424,7 @@ public String[] promptKeyboardInteractive(String destination, for(int i=0; icom.github.mwiede jsch jar - 0.1.73-SNAPSHOT + 0.2.0-SNAPSHOT JSch https://github.com/mwiede/jsch JSch is a pure Java implementation of SSH2 - scm:git:https://github.com/mwiede/jsch.git - scm:git:https://github.com/mwiede/jsch.git - HEAD + scm:git:https://github.com/mwiede/jsch.git + scm:git:https://github.com/mwiede/jsch.git + https://github.com/mwiede/jsch + HEAD + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + ossrh + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + GitHub + https://github.com/mwiede/jsch/issues + + + GitHub + https://github.com/mwiede/jsch/actions + ymnk Atsuhiko Yamanaka ymnk at jcraft D0t com - http://github.com/ymnk + https://github.com/ymnk JCraft,Inc. http://www.jcraft.com/ @@ -43,8 +62,18 @@ Revised BSD https://github.com/mwiede/jsch/blob/master/LICENSE.txt + + BSD + https://github.com/mwiede/jsch/blob/master/LICENSE.JZlib.txt + + + ISC + https://github.com/mwiede/jsch/blob/master/LICENSE.jBCrypt.txt + + UTF-8 + UTF-8 true 2.4.0 5.10.0 @@ -72,7 +101,7 @@ com.kohlschutter compiler-annotations 1.4.2 - provided + true net.java.dev.jna @@ -86,6 +115,24 @@ ${jna.version} true + + org.apache.logging.log4j + log4j-api + 2.17.1 + true + + + org.osgi + org.osgi.core + + + + + org.slf4j + slf4j-api + 2.0.0-alpha6 + true + org.junit.jupiter junit-jupiter @@ -95,7 +142,7 @@ org.testcontainers junit-jupiter - 1.16.2 + 1.16.3 test @@ -103,6 +150,10 @@ net.java.dev.jna jna + + slf4j-api + org.slf4j + @@ -111,28 +162,28 @@ 1.15 test - - org.slf4j - slf4j-api - 1.7.32 - test - ch.qos.logback logback-classic - 1.2.9 + 1.3.0-alpha13 test + + + slf4j-api + org.slf4j + + org.scala-lang scala-library - 2.13.7 + 2.13.6 test org.scalatest scalatest_2.13 - 3.2.10 + 3.2.11 test @@ -142,7 +193,6 @@ test - @@ -160,11 +210,56 @@ 16 + + + org.codehaus.mojo + flatten-maven-plugin + 1.2.7 + + ossrh + all + + + + flatten + process-resources + + flatten + + + + flatten.clean + clean + + clean + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 3.3.0 + + + + regex-property + + + versionWithoutMinus + ${project.version} + - + _ + false + + + + org.codehaus.mojo templating-maven-plugin @@ -180,7 +275,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.8.1 + 3.9.0 true 8 @@ -301,11 +396,32 @@ org.apache.maven.plugins maven-resources-plugin 3.2.0 + + + copy-licenses + process-resources + + copy-resources + + + ${project.build.outputDirectory}/META-INF + + + ${project.basedir} + false + + LICENSE* + + + + + + org.apache.maven.plugins maven-jar-plugin - 3.2.0 + 3.2.2 @@ -318,26 +434,98 @@ - + + org.apache.maven.plugins + maven-assembly-plugin + 3.3.0 + + + attach-sources + package + + single + + + + ${project.basedir}/src/main/assembly/sources.xml + + + + + org.apache.maven.plugins maven-javadoc-plugin 3.3.1 - false - src/main/java + true + none + com.jcraft.jsch + com.jcraft.jsch.* + ${project.build.sourceDirectory}:${project.build.directory}/generated-sources/java-templates:${project.basedir}/src/main/java9:${project.basedir}/src/main/java11:${project.basedir}/src/main/java15:${project.basedir}/src/main/java16 + + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-release-plugin + 2.5.3 + + false + release + + org.apache.maven.plugins + maven-deploy-plugin + 2.8.2 + + + org.apache.maven.plugins + maven-install-plugin + 2.5.2 + + + org.apache.maven.plugins + maven-site-plugin + 3.10.0 + + + org.apache.maven.plugins + maven-dependency-plugin + 3.2.0 + - - - org.apache.maven.wagon - wagon-ssh-external - 3.4.3 - - + + release + + + + org.apache.maven.plugins + maven-gpg-plugin + 3.0.1 + + + sign-artifacts + verify + + sign + + + + + + + coverage @@ -395,7 +583,7 @@ com.google.errorprone error_prone_core - 2.10.0 + 2.11.0 @@ -404,9 +592,4 @@ - - org.sonatype.oss - oss-parent - 9 - diff --git a/src/main/assembly/sources.xml b/src/main/assembly/sources.xml new file mode 100644 index 00000000..370e3be5 --- /dev/null +++ b/src/main/assembly/sources.xml @@ -0,0 +1,69 @@ + + sources + false + + jar + + + + ${project.build.outputDirectory}/META-INF + META-INF + + LICENSE* + + + + ${project.build.sourceDirectory} + + + **/*.java + + + + ${project.build.directory}/generated-sources/java-templates + + + **/*.java + + + + ${project.basedir}/src/main/java9 + META-INF/versions/9 + + **/*.java + + + + ${project.basedir}/src/main/java11 + META-INF/versions/11 + + **/*.java + + + + ${project.basedir}/src/main/java15 + META-INF/versions/15 + + **/*.java + + + + ${project.basedir}/src/main/java16 + META-INF/versions/16 + + **/*.java + + + + + + ${project.basedir}/.flattened-pom.xml + META-INF/maven/${project.groupId}/${project.artifactId} + pom.xml + + + ${project.build.directory}/maven-archiver/pom.properties + META-INF/maven/${project.groupId}/${project.artifactId} + + + diff --git a/src/main/java-templates/com/jcraft/jsch/Version.java b/src/main/java-templates/com/jcraft/jsch/Version.java index 9d464fde..2e897e02 100644 --- a/src/main/java-templates/com/jcraft/jsch/Version.java +++ b/src/main/java-templates/com/jcraft/jsch/Version.java @@ -2,7 +2,7 @@ final class Version { - private static final String VERSION = "${project.version}"; + private static final String VERSION = "${versionWithoutMinus}"; static String getVersion() { return VERSION; diff --git a/src/main/java/com/jcraft/jsch/Channel.java b/src/main/java/com/jcraft/jsch/Channel.java index 95e6445f..479bf74a 100644 --- a/src/main/java/com/jcraft/jsch/Channel.java +++ b/src/main/java/com/jcraft/jsch/Channel.java @@ -550,14 +550,14 @@ static void disconnect(Session session){ synchronized(pool){ channels=new Channel[pool.size()]; for(int i=0; i c=Class.forName(_config.target); - daemon=(ForwardedTCPIPDaemon)c.getDeclaredConstructor().newInstance(); + Class c=Class.forName(_config.target).asSubclass(ForwardedTCPIPDaemon.class); + daemon=c.getDeclaredConstructor().newInstance(); PipedOutputStream out=new PipedOutputStream(); io.setInputStream(new PassiveInputStream(out diff --git a/src/main/java/com/jcraft/jsch/ChannelSession.java b/src/main/java/com/jcraft/jsch/ChannelSession.java index c7721418..cac9be6e 100644 --- a/src/main/java/com/jcraft/jsch/ChannelSession.java +++ b/src/main/java/com/jcraft/jsch/ChannelSession.java @@ -74,7 +74,7 @@ public void setXForwarding(boolean enable){ } /** - * @deprecated Use {@link #setEnv(String, String)} or {@link #setEnv(byte[], byte[])} instead. + * @deprecated Use #setEnv(String, String) or #setEnv(byte[], byte[]) instead. * @see #setEnv(String, String) * @see #setEnv(byte[], byte[]) */ @@ -242,27 +242,27 @@ public void run(){ int i=-1; try{ while(isConnected() && - thread!=null && + thread!=null && io!=null && io.in!=null){ i=io.in.read(buf.buffer, 14, buf.buffer.length-14 -Session.buffer_margin - ); - if(i==0)continue; - if(i==-1){ - eof(); - break; - } - if(close)break; + ); + if(i==0)continue; + if(i==-1){ + eof(); + break; + } + if(close)break; //System.out.println("write: "+i); packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA); buf.putInt(recipient); buf.putInt(i); buf.skip(i); - getSession().write(packet, this, i); + getSession().write(packet, this, i); } } catch(Exception e){ diff --git a/src/main/java/com/jcraft/jsch/ChannelSftp.java b/src/main/java/com/jcraft/jsch/ChannelSftp.java index 477059ea..09353989 100644 --- a/src/main/java/com/jcraft/jsch/ChannelSftp.java +++ b/src/main/java/com/jcraft/jsch/ChannelSftp.java @@ -324,7 +324,7 @@ public void lcd(String path) throws SftpException{ path=localAbsolutePath(path); if((new File(path)).isDirectory()){ try{ - path=(new File(path)).getCanonicalPath(); + path=(new File(path)).getCanonicalPath(); } catch(Exception e){} lcwd=path; @@ -367,7 +367,7 @@ public void put(String src, String dst, int mode) throws SftpException{ put(src, dst, null, mode); } public void put(String src, String dst, - SftpProgressMonitor monitor) throws SftpException{ + SftpProgressMonitor monitor) throws SftpException{ put(src, dst, monitor, OVERWRITE); } @@ -382,7 +382,7 @@ public void put(String src, String dst, * @param mode how data should be added to dst */ public void put(String src, String dst, - SftpProgressMonitor monitor, int mode) throws SftpException{ + SftpProgressMonitor monitor, int mode) throws SftpException{ try{ ((MyPipedInputStream)io_in).updateReadSide(); @@ -413,7 +413,7 @@ public void put(String src, String dst, StringBuilder dstsb=null; if(isRemoteDir){ if(!dst.endsWith("/")){ - dst+="/"; + dst+="/"; } dstsb=new StringBuilder(dst); } @@ -423,61 +423,61 @@ else if(vsize>1){ } for(int j=0; ji) i=ii; } - if(i==-1) dstsb.append(_src); - else dstsb.append(_src.substring(i + 1)); + if(i==-1) dstsb.append(_src); + else dstsb.append(_src.substring(i + 1)); _dst=dstsb.toString(); dstsb.delete(dst.length(), _dst.length()); - } + } else{ _dst=dst; } //System.err.println("_dst "+_dst); - long size_of_dst=0; - if(mode==RESUME){ - try{ - SftpATTRS attr=_stat(_dst); - size_of_dst=attr.getSize(); - } - catch(Exception eee){ - //System.err.println(eee); - } - long size_of_src=new File(_src).length(); - if(size_of_src0){ - long skipped=src.skip(skip); - if(skipped1){ } for(int j=0; j1){ } } dstsb.delete(dst.length(), _dst.length()); - } + } else{ _dst=dst; } File _dstFile=new File(_dst); - if(mode==RESUME){ - long size_of_src=attr.getSize(); - long size_of_dst=_dstFile.length(); - if(size_of_dst>size_of_src){ - throw new SftpException(SSH_FX_FAILURE, + if(mode==RESUME){ + long size_of_src=attr.getSize(); + long size_of_dst=_dstFile.length(); + if(size_of_dst>size_of_src){ + throw new SftpException(SSH_FX_FAILURE, "failed to resume for "+_dst); - } - if(size_of_dst==size_of_src){ - return; - } - } - - if(monitor!=null){ - monitor.init(SftpProgressMonitor.GET, _src, _dst, attr.getSize()); - if(mode==RESUME){ - monitor.count(_dstFile.length()); - } - } + } + if(size_of_dst==size_of_src){ + return; + } + } + + if(monitor!=null){ + monitor.init(SftpProgressMonitor.GET, _src, _dst, attr.getSize()); + if(mode==RESUME){ + monitor.count(_dstFile.length()); + } + } FileOutputStream fos=null; _dstExist = _dstFile.exists(); @@ -1002,11 +1002,11 @@ public void get(String src, OutputStream dst) throws SftpException{ get(src, dst, null, OVERWRITE, 0); } public void get(String src, OutputStream dst, - SftpProgressMonitor monitor) throws SftpException{ + SftpProgressMonitor monitor) throws SftpException{ get(src, dst, monitor, OVERWRITE, 0); } public void get(String src, OutputStream dst, - SftpProgressMonitor monitor, int mode, long skip) throws SftpException{ + SftpProgressMonitor monitor, int mode, long skip) throws SftpException{ //System.err.println("get: "+src+", "+dst); try{ ((MyPipedInputStream)io_in).updateReadSide(); @@ -1015,7 +1015,7 @@ public void get(String src, OutputStream dst, src=isUnique(src); if(monitor!=null){ - SftpATTRS attr=_stat(src); + SftpATTRS attr=_stat(src); monitor.init(SftpProgressMonitor.GET, src, "??", attr.getSize()); if(mode==RESUME){ monitor.count(skip); @@ -1045,7 +1045,7 @@ private void _get(String src, OutputStream dst, fill(buf, length); if(type!=SSH_FXP_STATUS && type!=SSH_FXP_HANDLE){ - throw new SftpException(SSH_FX_FAILURE, ""); + throw new SftpException(SSH_FX_FAILURE, ""); } if(type==SSH_FXP_STATUS){ @@ -1057,7 +1057,7 @@ private void _get(String src, OutputStream dst, long offset=0; if(mode==RESUME){ - offset+=skip; + offset+=skip; } int request_max=1; @@ -1100,7 +1100,7 @@ private void _get(String src, OutputStream dst, } if(type!=SSH_FXP_DATA){ - break loop; + break loop; } buf.rewind(); @@ -1128,7 +1128,7 @@ private void _get(String src, OutputStream dst, int data_len = io_in.read(buf.buffer, 0, bar); if(data_len<0){ break loop; - } + } dst.write(buf.buffer, 0, data_len); @@ -1146,7 +1146,7 @@ private void _get(String src, OutputStream dst, } } - //System.err.println("length: "+length); // length should be 0 + //System.err.println("length: "+length); // length should be 0 if(optional_data>0){ skip(optional_data); @@ -1323,7 +1323,7 @@ public InputStream get(String src, final SftpProgressMonitor monitor, final long fill(buf, length); if(type!=SSH_FXP_STATUS && type!=SSH_FXP_HANDLE){ - throw new SftpException(SSH_FX_FAILURE, ""); + throw new SftpException(SSH_FX_FAILURE, ""); } if(type==SSH_FXP_STATUS){ int i=buf.getInt(); @@ -1954,7 +1954,7 @@ public void rm(String path) throws SftpException{ Header header=new Header(); for(int j=0; j v=glob_remote(path); int vsize=v.size(); for(int j=0; j v=glob_remote(path); int vsize=v.size(); for(int j=0; j v=glob_remote(path); int vsize=v.size(); for(int j=0; j v=glob_remote(path); int vsize=v.size(); for(int j=0; j v=glob_remote(path); int vsize=v.size(); for(int j=0; j glob_remote(String _path) throws Exception{ type=header.type; if(type!=SSH_FXP_STATUS && type!=SSH_FXP_NAME){ - throw new SftpException(SSH_FX_FAILURE, ""); + throw new SftpException(SSH_FX_FAILURE, ""); } if(type==SSH_FXP_STATUS){ fill(buf, length); - break; + break; } buf.rewind(); @@ -2700,21 +2700,21 @@ private Vector glob_remote(String _path) throws Exception{ buf.reset(); while(count>0){ - if(length>0){ - buf.shift(); + if(length>0){ + buf.shift(); int j=(buf.buffer.length>(buf.index+length)) ? length : (buf.buffer.length-buf.index); - i=io_in.read(buf.buffer, buf.index, j); - if(i<=0)break; - buf.index+=i; - length-=i; - } - - byte[] filename=buf.getString(); - //System.err.println("filename: "+new String(filename)); + i=io_in.read(buf.buffer, buf.index, j); + if(i<=0)break; + buf.index+=i; + length-=i; + } + + byte[] filename=buf.getString(); + //System.err.println("filename: "+new String(filename)); if(server_version<=3){ str=buf.getString(); // longname } - SftpATTRS attrs=SftpATTRS.getATTR(buf); + SftpATTRS attrs=SftpATTRS.getATTR(buf); byte[] _filename=filename; String f=null; @@ -2726,7 +2726,7 @@ private Vector glob_remote(String _path) throws Exception{ } found=Util.glob(pattern, _filename); - if(found){ + if(found){ if(f==null){ f=Util.byte2str(filename, fEncoding); } @@ -2736,9 +2736,9 @@ private Vector glob_remote(String _path) throws Exception{ pdir+="/"; } } - v.addElement(pdir+f); - } - count--; + v.addElement(pdir+f); + } + count--; } } if(_sendCLOSE(handle, header)) @@ -2809,9 +2809,9 @@ private Vector glob_local(String _path) throws Exception{ String pdir=Util.byte2str(dir)+file_separator; for(int j=0; j>>4)&0xf]; - bar[2*i+1]=table[(foo[i])&0xf]; - } - faked_cookie_hex_pool.put(session, bar); - foo=bar; + faked_cookie_pool.put(session, foo); + byte[] bar=new byte[32]; + for(int i=0; i<16; i++){ + bar[2*i]=table[(foo[i]>>>4)&0xf]; + bar[2*i+1]=table[(foo[i])&0xf]; + } + faked_cookie_hex_pool.put(session, bar); + foo=bar; } return foo; } @@ -159,19 +159,19 @@ public void run(){ io!=null && io.in!=null){ i=io.in.read(buf.buffer, - 14, - buf.buffer.length-14-Session.buffer_margin); - if(i<=0){ - eof(); + 14, + buf.buffer.length-14-Session.buffer_margin); + if(i<=0){ + eof(); break; - } - if(close)break; + } + if(close)break; packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA); buf.putInt(recipient); buf.putInt(i); buf.skip(i); - getSession().write(packet, this, i); + getSession().write(packet, this, i); } } catch(Exception e){ @@ -221,7 +221,7 @@ else if((foo[s]&0xff)==0x6c){ dlen=((dlen>>>8)&0xff)|((dlen<<8)&0xff00); } else{ - // ?? + // ?? } if(l<12+plen+((-plen)&3)+dlen) @@ -232,7 +232,7 @@ else if((foo[s]&0xff)==0x6c){ byte[] faked_cookie=null; synchronized(faked_cookie_pool){ - faked_cookie=faked_cookie_pool.get(_session); + faked_cookie=faked_cookie_pool.get(_session); } /* @@ -253,7 +253,7 @@ else if((foo[s]&0xff)==0x6c){ System.arraycopy(cookie, 0, foo, s+12+plen+((-plen)&3), dlen); } else{ - //System.err.println("wrong cookie"); + //System.err.println("wrong cookie"); thread=null; eof(); io.close(); diff --git a/src/main/java/com/jcraft/jsch/DHECN.java b/src/main/java/com/jcraft/jsch/DHECN.java index a0276c26..d7aa59d2 100644 --- a/src/main/java/com/jcraft/jsch/DHECN.java +++ b/src/main/java/com/jcraft/jsch/DHECN.java @@ -54,7 +54,7 @@ public abstract class DHECN extends KeyExchange{ @Override public void init(Session session, - byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception{ + byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception{ this.session=session; this.V_S=V_S; this.V_C=V_C; @@ -62,8 +62,8 @@ public void init(Session session, this.I_C=I_C; try{ - Class c=Class.forName(session.getConfig(sha_name)); - sha=(HASH)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(session.getConfig(sha_name)).asSubclass(HASH.class); + sha=c.getDeclaredConstructor().newInstance(); sha.init(); } catch(Exception e){ @@ -77,8 +77,8 @@ public void init(Session session, buf.putByte((byte)SSH_MSG_KEX_ECDH_INIT); try{ - Class c=Class.forName(session.getConfig("ecdh-sha2-nistp")); - ecdh=(ECDH)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(session.getConfig("ecdh-sha2-nistp")).asSubclass(ECDH.class); + ecdh=c.getDeclaredConstructor().newInstance(); ecdh.init(key_size); Q_C = ecdh.getQ(); @@ -118,8 +118,8 @@ public boolean next(Buffer _buf) throws Exception{ j=_buf.getByte(); j=_buf.getByte(); if(j!=SSH_MSG_KEX_ECDH_REPLY){ - System.err.println("type: must be SSH_MSG_KEX_ECDH_REPLY "+j); - return false; + System.err.println("type: must be SSH_MSG_KEX_ECDH_REPLY "+j); + return false; } K_S=_buf.getString(); @@ -135,7 +135,7 @@ public boolean next(Buffer _buf) throws Exception{ // Section 3.2.2 of [SEC1]. If a key fails validation, // the key exchange MUST fail. if(!ecdh.validate(r_s[0], r_s[1])){ - return false; + return false; } K = ecdh.getSecret(r_s[0], r_s[1]); @@ -171,7 +171,7 @@ public boolean next(Buffer _buf) throws Exception{ i=0; j=0; j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)| - ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); + ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); String alg=Util.byte2str(K_S, i, j); i+=j; diff --git a/src/main/java/com/jcraft/jsch/DHGEX.java b/src/main/java/com/jcraft/jsch/DHGEX.java index c0fe64db..7f6ce943 100644 --- a/src/main/java/com/jcraft/jsch/DHGEX.java +++ b/src/main/java/com/jcraft/jsch/DHGEX.java @@ -60,7 +60,7 @@ public class DHGEX extends KeyExchange{ @Override public void init(Session session, - byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception{ + byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception{ this.session=session; this.V_S=V_S; this.V_C=V_C; @@ -68,8 +68,8 @@ public void init(Session session, this.I_C=I_C; try{ - Class c=Class.forName(session.getConfig(hash)); - sha=(HASH)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(session.getConfig(hash)).asSubclass(HASH.class); + sha=c.getDeclaredConstructor().newInstance(); sha.init(); } catch(Exception e){ @@ -80,14 +80,14 @@ public void init(Session session, packet=new Packet(buf); try{ - Class c=Class.forName(session.getConfig("dh")); + Class c=Class.forName(session.getConfig("dh")).asSubclass(DH.class); min=Integer.parseInt(session.getConfig("dhgex_min")); max=Integer.parseInt(session.getConfig("dhgex_max")); preferred=Integer.parseInt(session.getConfig("dhgex_preferred")); if(checkInvalidSize(min) || checkInvalidSize(max) || checkInvalidSize(preferred) || preferred < min || max < preferred){ throw new JSchException("Invalid DHGEX sizes: min=" + min + " max=" + max + " preferred=" + preferred); } - dh=(com.jcraft.jsch.DH)(c.getDeclaredConstructor().newInstance()); + dh=c.getDeclaredConstructor().newInstance(); dh.init(); } catch(Exception e){ @@ -123,8 +123,8 @@ public boolean next(Buffer _buf) throws Exception{ _buf.getByte(); j=_buf.getByte(); if(j!=SSH_MSG_KEX_DH_GEX_GROUP){ - System.err.println("type: must be SSH_MSG_KEX_DH_GEX_GROUP "+j); - return false; + System.err.println("type: must be SSH_MSG_KEX_DH_GEX_GROUP "+j); + return false; } p=_buf.getMPInt(); @@ -165,8 +165,8 @@ public boolean next(Buffer _buf) throws Exception{ j=_buf.getByte(); j=_buf.getByte(); if(j!=SSH_MSG_KEX_DH_GEX_REPLY){ - System.err.println("type: must be SSH_MSG_KEX_DH_GEX_REPLY "+j); - return false; + System.err.println("type: must be SSH_MSG_KEX_DH_GEX_REPLY "+j); + return false; } K_S=_buf.getString(); @@ -217,7 +217,7 @@ public boolean next(Buffer _buf) throws Exception{ i=0; j=0; j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)| - ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); + ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); String alg=Util.byte2str(K_S, i, j); i+=j; diff --git a/src/main/java/com/jcraft/jsch/DHGN.java b/src/main/java/com/jcraft/jsch/DHGN.java index 51baaf15..4e7e7cc7 100644 --- a/src/main/java/com/jcraft/jsch/DHGN.java +++ b/src/main/java/com/jcraft/jsch/DHGN.java @@ -54,7 +54,7 @@ public abstract class DHGN extends KeyExchange{ @Override public void init(Session session, - byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception{ + byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception{ this.session=session; this.V_S=V_S; this.V_C=V_C; @@ -62,8 +62,8 @@ public void init(Session session, this.I_C=I_C; try{ - Class c=Class.forName(session.getConfig(sha_name())); - sha=(HASH)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(session.getConfig(sha_name())).asSubclass(HASH.class); + sha=c.getDeclaredConstructor().newInstance(); sha.init(); } catch(Exception e){ @@ -74,8 +74,8 @@ public void init(Session session, packet=new Packet(buf); try{ - Class c=Class.forName(session.getConfig("dh")); - dh=(DH)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(session.getConfig("dh")).asSubclass(DH.class); + dh=c.getDeclaredConstructor().newInstance(); dh.init(); } catch(Exception e){ @@ -126,8 +126,8 @@ public boolean next(Buffer _buf) throws Exception{ j=_buf.getByte(); j=_buf.getByte(); if(j!=31){ - System.err.println("type: must be 31 "+j); - return false; + System.err.println("type: must be 31 "+j); + return false; } K_S=_buf.getString(); @@ -168,7 +168,7 @@ public boolean next(Buffer _buf) throws Exception{ i=0; j=0; j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)| - ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); + ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); String alg=Util.byte2str(K_S, i, j); i+=j; diff --git a/src/main/java/com/jcraft/jsch/DHXEC.java b/src/main/java/com/jcraft/jsch/DHXEC.java index b1100207..c64911a1 100644 --- a/src/main/java/com/jcraft/jsch/DHXEC.java +++ b/src/main/java/com/jcraft/jsch/DHXEC.java @@ -59,7 +59,7 @@ public abstract class DHXEC extends KeyExchange{ @Override public void init(Session session, - byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception{ + byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception{ this.session=session; this.V_S=V_S; this.V_C=V_C; @@ -67,8 +67,8 @@ public void init(Session session, this.I_C=I_C; try{ - Class c=Class.forName(session.getConfig(sha_name)); - sha=(HASH)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(session.getConfig(sha_name)).asSubclass(HASH.class); + sha=c.getDeclaredConstructor().newInstance(); sha.init(); } catch(Exception e){ @@ -82,8 +82,8 @@ public void init(Session session, buf.putByte((byte)SSH_MSG_KEX_ECDH_INIT); try{ - Class c=Class.forName(session.getConfig("xdh")); - xdh=(XDH)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(session.getConfig("xdh")).asSubclass(XDH.class); + xdh=c.getDeclaredConstructor().newInstance(); xdh.init(curve_name, key_len); Q_C = xdh.getQ(); @@ -123,8 +123,8 @@ public boolean next(Buffer _buf) throws Exception{ j=_buf.getByte(); j=_buf.getByte(); if(j!=SSH_MSG_KEX_ECDH_REPLY){ - System.err.println("type: must be SSH_MSG_KEX_ECDH_REPLY "+j); - return false; + System.err.println("type: must be SSH_MSG_KEX_ECDH_REPLY "+j); + return false; } K_S=_buf.getString(); @@ -138,7 +138,7 @@ public boolean next(Buffer _buf) throws Exception{ // Section 3.2.2 of [SEC1]. If a key fails validation, // the key exchange MUST fail. if(!xdh.validate(Q_S)){ - return false; + return false; } K = xdh.getSecret(Q_S); @@ -188,7 +188,7 @@ public boolean next(Buffer _buf) throws Exception{ i=0; j=0; j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)| - ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); + ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); String alg=Util.byte2str(K_S, i, j); i+=j; diff --git a/src/main/java/com/jcraft/jsch/HostKey.java b/src/main/java/com/jcraft/jsch/HostKey.java index 63144194..c3d5ccd2 100644 --- a/src/main/java/com/jcraft/jsch/HostKey.java +++ b/src/main/java/com/jcraft/jsch/HostKey.java @@ -115,8 +115,8 @@ public String getFingerPrint(JSch jsch){ HASH hash=null; try{ String _c=JSch.getConfig("FingerprintHash").toLowerCase(); - Class c=Class.forName(JSch.getConfig(_c)); - hash=(HASH)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig(_c)).asSubclass(HASH.class); + hash=c.getDeclaredConstructor().newInstance(); } catch(Exception e){ System.err.println("getFingerPrint: "+e); } return Util.getFingerPrint(hash, key, false, true); @@ -141,7 +141,7 @@ private boolean isIncluded(String _host){ return hosts.regionMatches(true, i, _host, 0, hostlen); } if(hostlen==(j-i)){ - if(hosts.regionMatches(true, i, _host, 0, hostlen)) return true; + if(hosts.regionMatches(true, i, _host, 0, hostlen)) return true; } i=j+1; } diff --git a/src/main/java/com/jcraft/jsch/IO.java b/src/main/java/com/jcraft/jsch/IO.java index 1b87281e..b16bdf78 100644 --- a/src/main/java/com/jcraft/jsch/IO.java +++ b/src/main/java/com/jcraft/jsch/IO.java @@ -82,7 +82,7 @@ void getByte(byte[] array, int begin, int length) throws IOException { do{ int completed = in.read(array, begin, length); if(completed<0){ - throw new IOException("End of IO Stream Read"); + throw new IOException("End of IO Stream Read"); } begin+=completed; length-=completed; diff --git a/src/main/java/com/jcraft/jsch/JSch.java b/src/main/java/com/jcraft/jsch/JSch.java index fb74db27..cd619f75 100644 --- a/src/main/java/com/jcraft/jsch/JSch.java +++ b/src/main/java/com/jcraft/jsch/JSch.java @@ -43,7 +43,7 @@ public class JSch{ static Hashtable config=new Hashtable<>(); static{ config.put("kex", Util.getSystemProperty("jsch.kex", "curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256")); - config.put("server_host_key", Util.getSystemProperty("jsch.server_host_key", "ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,rsa-sha2-512,rsa-sha2-256,ssh-rsa")); + config.put("server_host_key", Util.getSystemProperty("jsch.server_host_key", "ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,rsa-sha2-512,rsa-sha2-256")); config.put("prefer_known_host_key_types", Util.getSystemProperty("jsch.prefer_known_host_key_types", "yes")); config.put("enable_server_sig_algs", Util.getSystemProperty("jsch.enable_server_sig_algs", "yes")); config.put("cipher.s2c", Util.getSystemProperty("jsch.cipher", "aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com")); @@ -65,7 +65,7 @@ public class JSch{ config.put("diffie-hellman-group-exchange-sha1", "com.jcraft.jsch.DHGEX"); config.put("diffie-hellman-group1-sha1", - "com.jcraft.jsch.DHG1"); + "com.jcraft.jsch.DHG1"); config.put("diffie-hellman-group14-sha1", "com.jcraft.jsch.DHG14"); config.put("diffie-hellman-group-exchange-sha256", @@ -227,7 +227,7 @@ public class JSch{ config.put("HashKnownHosts", "no"); config.put("PreferredAuthentications", Util.getSystemProperty("jsch.preferred_authentications", "gssapi-with-mic,publickey,keyboard-interactive,password")); - config.put("PubkeyAcceptedAlgorithms", Util.getSystemProperty("jsch.client_pubkey", "ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,rsa-sha2-512,rsa-sha2-256,ssh-rsa")); + config.put("PubkeyAcceptedAlgorithms", Util.getSystemProperty("jsch.client_pubkey", "ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,rsa-sha2-512,rsa-sha2-256")); config.put("CheckCiphers", Util.getSystemProperty("jsch.check_ciphers", "chacha20-poly1305@openssh.com")); config.put("CheckMacs", Util.getSystemProperty("jsch.check_macs", "")); @@ -401,7 +401,7 @@ public void setKnownHosts(String filename) throws JSchException{ if(known_hosts==null) known_hosts=new KnownHosts(this); if(known_hosts instanceof KnownHosts){ synchronized(known_hosts){ - ((KnownHosts)known_hosts).setKnownHosts(filename); + ((KnownHosts)known_hosts).setKnownHosts(filename); } } } @@ -421,7 +421,7 @@ public void setKnownHosts(InputStream stream) throws JSchException{ if(known_hosts==null) known_hosts=new KnownHosts(this); if(known_hosts instanceof KnownHosts){ synchronized(known_hosts){ - ((KnownHosts)known_hosts).setKnownHosts(stream); + ((KnownHosts)known_hosts).setKnownHosts(stream); } } } @@ -648,9 +648,9 @@ public static String getConfig(String key){ public static void setConfig(Hashtable newconf){ synchronized(config){ for(Enumeration e=newconf.keys() ; e.hasMoreElements() ;) { - String newkey=e.nextElement(); - String key=(newkey.equals("PubkeyAcceptedKeyTypes") ? "PubkeyAcceptedAlgorithms" : newkey); - config.put(key, newconf.get(newkey)); + String newkey=e.nextElement(); + String key=(newkey.equals("PubkeyAcceptedKeyTypes") ? "PubkeyAcceptedAlgorithms" : newkey); + config.put(key, newconf.get(newkey)); } } } @@ -682,7 +682,7 @@ public static void setLogger(Logger logger){ JSch.logger=logger; } - static Logger getLogger(){ + public static Logger getLogger(){ return logger; } } diff --git a/src/main/java/com/jcraft/jsch/JulLogger.java b/src/main/java/com/jcraft/jsch/JulLogger.java new file mode 100644 index 00000000..85782f47 --- /dev/null +++ b/src/main/java/com/jcraft/jsch/JulLogger.java @@ -0,0 +1,36 @@ +package com.jcraft.jsch; + +import java.util.logging.Level; + +public class JulLogger implements com.jcraft.jsch.Logger { + + private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(JSch.class.getName()); + + public JulLogger() {} + + @Override + public boolean isEnabled(int level) { + return logger.isLoggable(getLevel(level)); + } + + @Override + public void log(int level, String message) { + logger.log(getLevel(level), message); + } + + private static Level getLevel(int level) { + switch (level) { + case com.jcraft.jsch.Logger.DEBUG: + return Level.FINE; + case com.jcraft.jsch.Logger.INFO: + return Level.INFO; + case com.jcraft.jsch.Logger.WARN: + return Level.WARNING; + case com.jcraft.jsch.Logger.ERROR: + case com.jcraft.jsch.Logger.FATAL: + return Level.SEVERE; + default: + return Level.FINER; + } + } +} diff --git a/src/main/java/com/jcraft/jsch/KeyExchange.java b/src/main/java/com/jcraft/jsch/KeyExchange.java index cb611909..efc8f2bf 100644 --- a/src/main/java/com/jcraft/jsch/KeyExchange.java +++ b/src/main/java/com/jcraft/jsch/KeyExchange.java @@ -68,7 +68,7 @@ public abstract class KeyExchange{ protected byte[] K_S=null; public abstract void init(Session session, - byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception; + byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception; public abstract boolean next(Buffer buf) throws Exception; public abstract int getState(); @@ -117,44 +117,44 @@ protected static String[] guess(Session session, byte[]I_S, byte[]I_C) throws Ex loop: while(j _s2cclazz=Class.forName(session.getConfig(guess[PROPOSAL_ENC_ALGS_STOC])); - Cipher _s2ccipher=(Cipher)(_s2cclazz.getDeclaredConstructor().newInstance()); + Class _s2cclazz=Class.forName(session.getConfig(guess[PROPOSAL_ENC_ALGS_STOC])).asSubclass(Cipher.class); + Cipher _s2ccipher=_s2cclazz.getDeclaredConstructor().newInstance(); _s2cAEAD=_s2ccipher.isAEAD(); if(_s2cAEAD){ guess[PROPOSAL_MAC_ALGS_STOC]=null; } - Class _c2sclazz=Class.forName(session.getConfig(guess[PROPOSAL_ENC_ALGS_CTOS])); - Cipher _c2scipher=(Cipher)(_c2sclazz.getDeclaredConstructor().newInstance()); + Class _c2sclazz=Class.forName(session.getConfig(guess[PROPOSAL_ENC_ALGS_CTOS])).asSubclass(Cipher.class); + Cipher _c2scipher=_c2sclazz.getDeclaredConstructor().newInstance(); _c2sAEAD=_c2scipher.isAEAD(); if(_c2sAEAD){ guess[PROPOSAL_MAC_ALGS_CTOS]=null; @@ -188,8 +188,8 @@ public String getFingerPrint(){ HASH hash=null; try{ String _c=session.getConfig("FingerprintHash").toLowerCase(); - Class c=Class.forName(session.getConfig(_c)); - hash=(HASH)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(session.getConfig(_c)).asSubclass(HASH.class); + hash=c.getDeclaredConstructor().newInstance(); } catch(Exception e){ System.err.println("getFingerPrint: "+e); } return Util.getFingerPrint(hash, getHostKey(), true, false); @@ -239,13 +239,13 @@ protected boolean verify(String alg, byte[] K_S, int index, ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j; n=tmp; - + SignatureRSA sig=null; Buffer buf=new Buffer(sig_of_H); String foo=Util.byte2str(buf.getString()); try{ - Class c=Class.forName(session.getConfig(foo)); - sig=(SignatureRSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(session.getConfig(foo)).asSubclass(SignatureRSA.class); + sig=c.getDeclaredConstructor().newInstance(); sig.init(); } catch(Exception e){ @@ -271,7 +271,7 @@ else if(alg.equals("ssh-dss")){ key_alg_name=alg; j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)| - ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); + ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j; p=tmp; j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)| @@ -279,7 +279,7 @@ else if(alg.equals("ssh-dss")){ tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j; q=tmp; j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)| - ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); + ((K_S[i++]<<8)&0x0000ff00)|((K_S[i++])&0x000000ff); tmp=new byte[j]; System.arraycopy(K_S, i, tmp, 0, j); i+=j; g=tmp; j=((K_S[i++]<<24)&0xff000000)|((K_S[i++]<<16)&0x00ff0000)| @@ -289,8 +289,8 @@ else if(alg.equals("ssh-dss")){ SignatureDSA sig=null; try{ - Class c=Class.forName(session.getConfig("signature.dss")); - sig=(SignatureDSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(session.getConfig("signature.dss")).asSubclass(SignatureDSA.class); + sig=c.getDeclaredConstructor().newInstance(); sig.init(); } catch(Exception e){ @@ -331,8 +331,8 @@ else if(alg.equals("ecdsa-sha2-nistp256") || SignatureECDSA sig=null; try{ - Class c=Class.forName(session.getConfig(alg)); - sig=(SignatureECDSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(session.getConfig(alg)).asSubclass(SignatureECDSA.class); + sig=c.getDeclaredConstructor().newInstance(); sig.init(); } catch(Exception e){ @@ -364,8 +364,8 @@ else if(alg.equals("ssh-ed25519") || SignatureEdDSA sig=null; try{ - Class c=Class.forName(session.getConfig(alg)); - sig=(SignatureEdDSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(session.getConfig(alg)).asSubclass(SignatureEdDSA.class); + sig=c.getDeclaredConstructor().newInstance(); sig.init(); } catch(Exception | NoClassDefFoundError e){ @@ -385,7 +385,7 @@ else if(alg.equals("ssh-ed25519") || } else{ System.err.println("unknown alg"); - } + } return result; } diff --git a/src/main/java/com/jcraft/jsch/KeyPair.java b/src/main/java/com/jcraft/jsch/KeyPair.java index cf06af74..889d4dd1 100644 --- a/src/main/java/com/jcraft/jsch/KeyPair.java +++ b/src/main/java/com/jcraft/jsch/KeyPair.java @@ -143,26 +143,26 @@ public void writePrivateKey(OutputStream out, byte[] passphrase){ try{ out.write(getBegin()); out.write(cr); if(passphrase!=null){ - out.write(header[0]); out.write(cr); - out.write(header[1]); - for(int i=0; i>>4)&0x0f))); - out.write(b2a((byte)(iv[i]&0x0f))); - } + out.write(header[0]); out.write(cr); + out.write(header[1]); + for(int i=0; i>>4)&0x0f))); + out.write(b2a((byte)(iv[i]&0x0f))); + } + out.write(cr); out.write(cr); - out.write(cr); } int i=0; while(i c=Class.forName(JSch.getConfig("random")); - random=(Random)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("random")).asSubclass(Random.class); + random=c.getDeclaredConstructor().newInstance(); } catch(Exception e){ System.err.println("connect: random "+e); } } @@ -409,8 +409,8 @@ private Random genRandom(){ private HASH genHash(){ try{ - Class c=Class.forName(JSch.getConfig("md5")); - hash=(HASH)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("md5")).asSubclass(HASH.class); + hash=c.getDeclaredConstructor().newInstance(); hash.init(); } catch(Exception e){ @@ -419,8 +419,8 @@ private HASH genHash(){ } private Cipher genCipher(){ try{ - Class c=Class.forName(JSch.getConfig("3des-cbc")); - cipher=(Cipher)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("3des-cbc")).asSubclass(Cipher.class); + cipher=c.getDeclaredConstructor().newInstance(); } catch(Exception e){ } @@ -440,33 +440,33 @@ synchronized byte[] genKey(byte[] passphrase, byte[] iv){ byte[] key=new byte[cipher.getBlockSize()]; int hsize=hash.getBlockSize(); byte[] hn=new byte[key.length/hsize*hsize+ - (key.length%hsize==0?0:hsize)]; + (key.length%hsize==0?0:hsize)]; try{ byte[] tmp=null; if(vendor==VENDOR_OPENSSH){ - for(int index=0; index+hsize<=hn.length;){ - if(tmp!=null){ hash.update(tmp, 0, tmp.length); } - hash.update(passphrase, 0, passphrase.length); + for(int index=0; index+hsize<=hn.length;){ + if(tmp!=null){ hash.update(tmp, 0, tmp.length); } + hash.update(passphrase, 0, passphrase.length); hash.update(iv, 0, iv.length > 8 ? 8: iv.length); - tmp=hash.digest(); - System.arraycopy(tmp, 0, hn, index, tmp.length); - index+=tmp.length; - } - System.arraycopy(hn, 0, key, 0, key.length); + tmp=hash.digest(); + System.arraycopy(tmp, 0, hn, index, tmp.length); + index+=tmp.length; + } + System.arraycopy(hn, 0, key, 0, key.length); } else if(vendor==VENDOR_FSECURE){ - for(int index=0; index+hsize<=hn.length;){ - if(tmp!=null){ hash.update(tmp, 0, tmp.length); } - hash.update(passphrase, 0, passphrase.length); - tmp=hash.digest(); - System.arraycopy(tmp, 0, hn, index, tmp.length); - index+=tmp.length; - } - System.arraycopy(hn, 0, key, 0, key.length); + for(int index=0; index+hsize<=hn.length;){ + if(tmp!=null){ hash.update(tmp, 0, tmp.length); } + hash.update(passphrase, 0, passphrase.length); + tmp=hash.digest(); + System.arraycopy(tmp, 0, hn, index, tmp.length); + index+=tmp.length; + } + System.arraycopy(hn, 0, key, 0, key.length); } else if(vendor==VENDOR_PUTTY){ - Class c=Class.forName(JSch.getConfig("sha-1")); - HASH sha1=(HASH)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("sha-1")).asSubclass(HASH.class); + HASH sha1=c.getDeclaredConstructor().newInstance(); tmp = new byte[4]; key = new byte[20*2]; for(int i = 0; i < 2; i++){ @@ -656,47 +656,47 @@ else if(_type.equals("ssh-ed448")){ if(buf[i]=='B'&& i+3= len) - throw new JSchException("invalid privatekey"); + throw new JSchException("invalid privatekey"); if(buf[i]=='D'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=DSA; } - else if(buf[i]=='R'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=RSA; } - else if(buf[i]=='E'&& buf[i+1]=='C'){ type=ECDSA; } - else if(buf[i]=='S'&& buf[i+1]=='S'&& buf[i+2]=='H'){ // FSecure - type=UNKNOWN; - vendor=VENDOR_FSECURE; - } - else if(i+6 < len && + else if(buf[i]=='R'&& buf[i+1]=='S'&& buf[i+2]=='A'){ type=RSA; } + else if(buf[i]=='E'&& buf[i+1]=='C'){ type=ECDSA; } + else if(buf[i]=='S'&& buf[i+1]=='S'&& buf[i+2]=='H'){ // FSecure + type=UNKNOWN; + vendor=VENDOR_FSECURE; + } + else if(i+6 < len && buf[i]=='P' && buf[i+1]=='R' && buf[i+2]=='I' && buf[i+3]=='V' && buf[i+4]=='A' && buf[i+5]=='T' && buf[i+6]=='E'){ - type=UNKNOWN; - vendor=VENDOR_PKCS8; + type=UNKNOWN; + vendor=VENDOR_PKCS8; encrypted=false; i+=3; - } - else if(i+8 < len && + } + else if(i+8 < len && buf[i]=='E' && buf[i+1]=='N' && buf[i+2]=='C' && buf[i+3]=='R' && buf[i+4]=='Y' && buf[i+5]=='P' && buf[i+6]=='T' && buf[i+7]=='E' && buf[i+8]=='D'){ - type=UNKNOWN; - vendor=VENDOR_PKCS8; + type=UNKNOWN; + vendor=VENDOR_PKCS8; i+=5; } else if (isOpenSSHPrivateKey(buf, i, len)) { type = UNKNOWN; vendor = VENDOR_OPENSSH_V1; } else { - throw new JSchException("invalid privatekey"); - } + throw new JSchException("invalid privatekey"); + } i+=3; - continue; - } + continue; + } if(buf[i]=='A'&& i+7 c=Class.forName(JSch.getConfig("aes256-cbc")); - cipher=(Cipher)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("aes256-cbc")).asSubclass(Cipher.class); + cipher=c.getDeclaredConstructor().newInstance(); // key=new byte[cipher.getBlockSize()]; iv=new byte[cipher.getIVSize()]; } @@ -709,8 +709,8 @@ else if(i+8 < len && buf[i+4]=='1'&& buf[i+5]=='9'&& buf[i+6]=='2'&& buf[i+7]=='-'){ i+=8; if(Session.checkCipher(JSch.getConfig("aes192-cbc"))){ - Class c=Class.forName(JSch.getConfig("aes192-cbc")); - cipher=(Cipher)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("aes192-cbc")).asSubclass(Cipher.class); + cipher=c.getDeclaredConstructor().newInstance(); // key=new byte[cipher.getBlockSize()]; iv=new byte[cipher.getIVSize()]; } @@ -723,8 +723,8 @@ else if(i+8 < len && buf[i+4]=='1'&& buf[i+5]=='2'&& buf[i+6]=='8'&& buf[i+7]=='-'){ i+=8; if(Session.checkCipher(JSch.getConfig("aes128-cbc"))){ - Class c=Class.forName(JSch.getConfig("aes128-cbc")); - cipher=(Cipher)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("aes128-cbc")).asSubclass(Cipher.class); + cipher=c.getDeclaredConstructor().newInstance(); // key=new byte[cipher.getBlockSize()]; iv=new byte[cipher.getIVSize()]; } @@ -735,35 +735,35 @@ else if(i+8 < len && } if(buf[i]=='C'&& i+34 && // FSecure - data[0]==(byte)0x3f && - data[1]==(byte)0x6f && - data[2]==(byte)0xf9 && - data[3]==(byte)0xeb){ - - Buffer _buf=new Buffer(data); - _buf.getInt(); // 0x3f6ff9be - _buf.getInt(); - byte[]_type=_buf.getString(); - //System.err.println("type: "+Util.byte2str(_type)); - String _cipher=Util.byte2str(_buf.getString()); - //System.err.println("cipher: "+_cipher); - if(_cipher.equals("3des-cbc")){ - _buf.getInt(); - byte[] foo=new byte[data.length-_buf.getOffSet()]; - _buf.getByte(foo); - data=foo; - encrypted=true; - throw new JSchException("unknown privatekey format"); - } - else if(_cipher.equals("none")){ - _buf.getInt(); - _buf.getInt(); + data[0]==(byte)0x3f && + data[1]==(byte)0x6f && + data[2]==(byte)0xf9 && + data[3]==(byte)0xeb){ + + Buffer _buf=new Buffer(data); + _buf.getInt(); // 0x3f6ff9be + _buf.getInt(); + byte[]_type=_buf.getString(); + //System.err.println("type: "+Util.byte2str(_type)); + String _cipher=Util.byte2str(_buf.getString()); + //System.err.println("cipher: "+_cipher); + if(_cipher.equals("3des-cbc")){ + _buf.getInt(); + byte[] foo=new byte[data.length-_buf.getOffSet()]; + _buf.getByte(foo); + data=foo; + encrypted=true; + throw new JSchException("unknown privatekey format"); + } + else if(_cipher.equals("none")){ + _buf.getInt(); + _buf.getInt(); encrypted=false; - byte[] foo=new byte[data.length-_buf.getOffSet()]; - _buf.getByte(foo); - data=foo; - } + byte[] foo=new byte[data.length-_buf.getOffSet()]; + _buf.getByte(foo); + data=foo; + } } // OPENSSH V1 PRIVATE KEY else if (data != null && @@ -869,8 +869,8 @@ else if (data != null && type = readOpenSSHKeyv1(data); } else if (Session.checkCipher(JSch.getConfig(cipherName))) { encrypted = true; - Class c = Class.forName(JSch.getConfig(cipherName)); - cipher = (Cipher) c.getDeclaredConstructor().newInstance(); + Class c = Class.forName(JSch.getConfig(cipherName)).asSubclass(Cipher.class); + cipher = c.getDeclaredConstructor().newInstance(); data = buffer.getString(); // the type can only be determined after encryption, so we take this intermediate here: type = DEFERRED; @@ -880,67 +880,67 @@ else if (data != null && } if(pubkey!=null){ - try{ - buf=pubkey; + try{ + buf=pubkey; len=buf.length; - if(buf.length>4 && // FSecure's public key - buf[0]=='-' && buf[1]=='-' && buf[2]=='-' && buf[3]=='-'){ - - boolean valid=true; - i=0; - do{i++;}while(buf.length>i && buf[i]!=0x0a); - if(buf.length<=i) {valid=false;} - - while(valid){ - if(buf[i]==0x0a){ - boolean inheader=false; - for(int j=i+1; j4 && // FSecure's public key + buf[0]=='-' && buf[1]=='-' && buf[2]=='-' && buf[3]=='-'){ + + boolean valid=true; + i=0; + do{i++;}while(buf.length>i && buf[i]!=0x0a); + if(buf.length<=i) {valid=false;} + + while(valid){ + if(buf[i]==0x0a){ + boolean inheader=false; + for(int j=i+1; j7){ - if(buf[4]=='d'){ type=DSA; } - else if(buf[4]=='r'){ type=RSA; } - else if(buf[4]=='e' && buf[6]=='2'){ type=ED25519; } - else if(buf[4]=='e' && buf[6]=='4'){ type=ED448; } + if(buf[4]=='d'){ type=DSA; } + else if(buf[4]=='r'){ type=RSA; } + else if(buf[4]=='e' && buf[6]=='2'){ type=ED25519; } + else if(buf[4]=='e' && buf[6]=='4'){ type=ED448; } + } + i=0; + while(i7){ type=ECDSA; @@ -970,10 +970,10 @@ else if(buf[0]=='e'&& buf[1]=='c'&& buf[2]=='d' && buf[3]=='s'){ } } } - } - } - catch(Exception ee){ - } + } + } + catch(Exception ee){ + } } } catch(Exception e){ @@ -1183,8 +1183,8 @@ else if(typ.equals("ssh-dss")){ if(kpair.encrypted){ if(Session.checkCipher(JSch.getConfig("aes256-cbc"))){ try { - Class c=Class.forName(JSch.getConfig("aes256-cbc")); - kpair.cipher=(Cipher)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("aes256-cbc")).asSubclass(Cipher.class); + kpair.cipher=c.getDeclaredConstructor().newInstance(); kpair.iv=new byte[kpair.cipher.getIVSize()]; } catch(Exception e){ diff --git a/src/main/java/com/jcraft/jsch/KeyPairDSA.java b/src/main/java/com/jcraft/jsch/KeyPairDSA.java index 02791c1a..85a0c22f 100644 --- a/src/main/java/com/jcraft/jsch/KeyPairDSA.java +++ b/src/main/java/com/jcraft/jsch/KeyPairDSA.java @@ -65,8 +65,8 @@ public KeyPairDSA(JSch jsch, void generate(int key_size) throws JSchException{ this.key_size=key_size; try{ - Class c=Class.forName(JSch.getConfig("keypairgen.dsa")); - KeyPairGenDSA keypairgen=(KeyPairGenDSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("keypairgen.dsa")).asSubclass(KeyPairGenDSA.class); + KeyPairGenDSA keypairgen=c.getDeclaredConstructor().newInstance(); keypairgen.init(key_size); P_array=keypairgen.getP(); Q_array=keypairgen.getQ(); @@ -120,19 +120,19 @@ boolean parse(byte[] plain){ try{ if(vendor==VENDOR_FSECURE){ - if(plain[0]!=0x30){ // FSecure - Buffer buf=new Buffer(plain); - buf.getInt(); - P_array=buf.getMPIntBits(); - G_array=buf.getMPIntBits(); - Q_array=buf.getMPIntBits(); - pub_array=buf.getMPIntBits(); - prv_array=buf.getMPIntBits(); + if(plain[0]!=0x30){ // FSecure + Buffer buf=new Buffer(plain); + buf.getInt(); + P_array=buf.getMPIntBits(); + G_array=buf.getMPIntBits(); + Q_array=buf.getMPIntBits(); + pub_array=buf.getMPIntBits(); + prv_array=buf.getMPIntBits(); if(P_array!=null) key_size = (new BigInteger(P_array)).bitLength(); - return true; - } - return false; + return true; + } + return false; } else if(vendor==VENDOR_PUTTY){ Buffer buf=new Buffer(plain); @@ -282,8 +282,8 @@ public int getKeySize(){ @Override public byte[] getSignature(byte[] data){ try{ - Class c=Class.forName(JSch.getConfig("signature.dss")); - SignatureDSA dsa=(SignatureDSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("signature.dss")).asSubclass(SignatureDSA.class); + SignatureDSA dsa=c.getDeclaredConstructor().newInstance(); dsa.init(); dsa.setPrvKey(prv_array, P_array, Q_array, G_array); @@ -308,8 +308,8 @@ public byte[] getSignature(byte[] data, String alg){ @Override public Signature getVerifier(){ try{ - Class c=Class.forName(JSch.getConfig("signature.dss")); - SignatureDSA dsa=(SignatureDSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("signature.dss")).asSubclass(SignatureDSA.class); + SignatureDSA dsa=c.getDeclaredConstructor().newInstance(); dsa.init(); if(pub_array == null && P_array == null && getPublicKeyBlob()!=null){ diff --git a/src/main/java/com/jcraft/jsch/KeyPairECDSA.java b/src/main/java/com/jcraft/jsch/KeyPairECDSA.java index 229fc7e9..1234f9b3 100644 --- a/src/main/java/com/jcraft/jsch/KeyPairECDSA.java +++ b/src/main/java/com/jcraft/jsch/KeyPairECDSA.java @@ -94,8 +94,8 @@ public KeyPairECDSA(JSch jsch, void generate(int key_size) throws JSchException{ this.key_size=key_size; try{ - Class c=Class.forName(JSch.getConfig("keypairgen.ecdsa")); - KeyPairGenECDSA keypairgen=(KeyPairGenECDSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("keypairgen.ecdsa")).asSubclass(KeyPairGenECDSA.class); + KeyPairGenECDSA keypairgen=c.getDeclaredConstructor().newInstance(); keypairgen.init(key_size); prv_array=keypairgen.getD(); r_array=keypairgen.getR(); @@ -170,12 +170,12 @@ boolean parse(byte[] plain){ if(vendor==VENDOR_FSECURE){ /* - if(plain[0]!=0x30){ // FSecure - return true; - } - return false; + if(plain[0]!=0x30){ // FSecure + return true; + } + return false; */ - return false; + return false; } else if(vendor==VENDOR_PUTTY){ /* @@ -192,7 +192,7 @@ else if(vendor==VENDOR_PUTTY){ return true; */ - return false; + return false; } // OPENSSH Key v1 Format @@ -347,8 +347,8 @@ public int getKeySize(){ @Override public byte[] getSignature(byte[] data){ try{ - Class c=Class.forName(JSch.getConfig("ecdsa-sha2-"+Util.byte2str(name))); - SignatureECDSA ecdsa=(SignatureECDSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("ecdsa-sha2-"+Util.byte2str(name))).asSubclass(SignatureECDSA.class); + SignatureECDSA ecdsa=c.getDeclaredConstructor().newInstance(); ecdsa.init(); ecdsa.setPrvKey(prv_array); @@ -374,8 +374,8 @@ public byte[] getSignature(byte[] data, String al){ @Override public Signature getVerifier(){ try{ - Class c=Class.forName(JSch.getConfig("ecdsa-sha2-"+Util.byte2str(name))); - final SignatureECDSA ecdsa=(SignatureECDSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("ecdsa-sha2-"+Util.byte2str(name))).asSubclass(SignatureECDSA.class); + final SignatureECDSA ecdsa=c.getDeclaredConstructor().newInstance(); ecdsa.init(); if(r_array == null && s_array == null && getPublicKeyBlob()!=null){ diff --git a/src/main/java/com/jcraft/jsch/KeyPairEdDSA.java b/src/main/java/com/jcraft/jsch/KeyPairEdDSA.java index 92d6a43f..2103c018 100644 --- a/src/main/java/com/jcraft/jsch/KeyPairEdDSA.java +++ b/src/main/java/com/jcraft/jsch/KeyPairEdDSA.java @@ -49,8 +49,8 @@ public KeyPairEdDSA(JSch jsch, @Override void generate(int key_size) throws JSchException{ try{ - Class c=Class.forName(JSch.getConfig("keypairgen.eddsa")); - KeyPairGenEdDSA keypairgen=(KeyPairGenEdDSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("keypairgen.eddsa")).asSubclass(KeyPairGenEdDSA.class); + KeyPairGenEdDSA keypairgen=c.getDeclaredConstructor().newInstance(); keypairgen.init(getJceName(), getKeySize()); pub_array=keypairgen.getPub(); prv_array=keypairgen.getPrv(); @@ -122,8 +122,8 @@ public byte[] getSignature(byte[] data){ @Override public byte[] getSignature(byte[] data, String alg){ try{ - Class c=Class.forName(JSch.getConfig(alg)); - SignatureEdDSA eddsa=(SignatureEdDSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig(alg)).asSubclass(SignatureEdDSA.class); + SignatureEdDSA eddsa=c.getDeclaredConstructor().newInstance(); eddsa.init(); eddsa.setPrvKey(prv_array); @@ -147,8 +147,8 @@ public Signature getVerifier(){ @Override public Signature getVerifier(String alg){ try{ - Class c=Class.forName(JSch.getConfig(alg)); - SignatureEdDSA eddsa=(SignatureEdDSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig(alg)).asSubclass(SignatureEdDSA.class); + SignatureEdDSA eddsa=c.getDeclaredConstructor().newInstance(); eddsa.init(); if(pub_array == null && getPublicKeyBlob()!=null){ diff --git a/src/main/java/com/jcraft/jsch/KeyPairPKCS8.java b/src/main/java/com/jcraft/jsch/KeyPairPKCS8.java index cc5a6661..9ad72401 100644 --- a/src/main/java/com/jcraft/jsch/KeyPairPKCS8.java +++ b/src/main/java/com/jcraft/jsch/KeyPairPKCS8.java @@ -323,8 +323,8 @@ else if(Util.array_equals(pbesid, pbeWithMD5AndDESCBC)){ byte[] key=null; try{ - Class c=Class.forName(JSch.getConfig("pbkdf")); - PBKDF tmp=(PBKDF)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("pbkdf")).asSubclass(PBKDF.class); + PBKDF tmp=c.getDeclaredConstructor().newInstance(); key = tmp.getKey(_passphrase, salt, iterations, cipher.getBlockSize()); } catch(Exception ee){ @@ -366,8 +366,8 @@ else if(Util.array_equals(id, aes192cbc)){ else if(Util.array_equals(id, aes256cbc)){ name="aes256-cbc"; } - Class c=Class.forName(JSch.getConfig(name)); - cipher=(Cipher)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig(name)).asSubclass(Cipher.class); + cipher=c.getDeclaredConstructor().newInstance(); } catch(Exception e){ if(JSch.getLogger().isEnabled(Logger.FATAL)){ diff --git a/src/main/java/com/jcraft/jsch/KeyPairRSA.java b/src/main/java/com/jcraft/jsch/KeyPairRSA.java index c7fd0a47..dae0f4c9 100644 --- a/src/main/java/com/jcraft/jsch/KeyPairRSA.java +++ b/src/main/java/com/jcraft/jsch/KeyPairRSA.java @@ -65,8 +65,8 @@ public KeyPairRSA(JSch jsch, void generate(int key_size) throws JSchException{ this.key_size=key_size; try{ - Class c=Class.forName(JSch.getConfig("keypairgen.rsa")); - KeyPairGenRSA keypairgen=(KeyPairGenRSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("keypairgen.rsa")).asSubclass(KeyPairGenRSA.class); + KeyPairGenRSA keypairgen=c.getDeclaredConstructor().newInstance(); keypairgen.init(key_size); pub_array=keypairgen.getE(); prv_array=keypairgen.getD(); @@ -154,14 +154,14 @@ boolean parse(byte [] plain){ } if(vendor==VENDOR_FSECURE){ - if(plain[index]!=0x30){ // FSecure - Buffer buf=new Buffer(plain); - pub_array=buf.getMPIntBits(); - prv_array=buf.getMPIntBits(); - n_array=buf.getMPIntBits(); - byte[] u_array=buf.getMPIntBits(); - p_array=buf.getMPIntBits(); - q_array=buf.getMPIntBits(); + if(plain[index]!=0x30){ // FSecure + Buffer buf=new Buffer(plain); + pub_array=buf.getMPIntBits(); + prv_array=buf.getMPIntBits(); + n_array=buf.getMPIntBits(); + byte[] u_array=buf.getMPIntBits(); + p_array=buf.getMPIntBits(); + q_array=buf.getMPIntBits(); if(n_array!=null){ key_size = (new BigInteger(n_array)).bitLength(); } @@ -170,9 +170,9 @@ boolean parse(byte [] plain){ getEQArray(); getCArray(); - return true; - } - return false; + return true; + } + return false; } // OPENSSH Key v1 Format @@ -353,8 +353,8 @@ public byte[] getSignature(byte[] data){ @Override public byte[] getSignature(byte[] data, String alg){ try{ - Class c=Class.forName(JSch.getConfig(alg)); - SignatureRSA rsa=(SignatureRSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig(alg)).asSubclass(SignatureRSA.class); + SignatureRSA rsa=c.getDeclaredConstructor().newInstance(); rsa.init(); rsa.setPrvKey(prv_array, n_array); @@ -378,8 +378,8 @@ public Signature getVerifier(){ @Override public Signature getVerifier(String alg){ try{ - Class c=Class.forName(JSch.getConfig(alg)); - SignatureRSA rsa=(SignatureRSA)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig(alg)).asSubclass(SignatureRSA.class); + SignatureRSA rsa=c.getDeclaredConstructor().newInstance(); rsa.init(); if(pub_array == null && n_array == null && getPublicKeyBlob()!=null){ diff --git a/src/main/java/com/jcraft/jsch/KnownHosts.java b/src/main/java/com/jcraft/jsch/KnownHosts.java index 8b25a1a7..29a7a0f9 100644 --- a/src/main/java/com/jcraft/jsch/KnownHosts.java +++ b/src/main/java/com/jcraft/jsch/KnownHosts.java @@ -76,15 +76,15 @@ void setKnownHosts(InputStream input) throws JSchException{ int bufl=0; loop: while(true){ - bufl=0; + bufl=0; while(true){ j=fis.read(); if(j==-1){ if(bufl==0){ break loop; } else{ break; } } - if(j==0x0d){ continue; } - if(j==0x0a){ break; } + if(j==0x0d){ continue; } + if(j==0x0a){ break; } if(buf.length<=bufl){ if(bufl>1024*10) break; // too long... byte[] newbuf=new byte[buf.length*2]; @@ -92,38 +92,38 @@ void setKnownHosts(InputStream input) throws JSchException{ buf=newbuf; } buf[bufl++]=(byte)j; - } + } - j=0; + j=0; while(j=bufl){ - addInvalidLine(Util.byte2str(buf, 0, bufl)); - continue loop; - } + if(i==' '||i=='\t'){ j++; continue; } + if(i=='#'){ + addInvalidLine(Util.byte2str(buf, 0, bufl)); + continue loop; + } + break; + } + if(j>=bufl){ + addInvalidLine(Util.byte2str(buf, 0, bufl)); + continue loop; + } sb.setLength(0); while(j=bufl || host.length()==0){ - addInvalidLine(Util.byte2str(buf, 0, bufl)); - continue loop; - } + } + host=sb.toString(); + if(j>=bufl || host.length()==0){ + addInvalidLine(Util.byte2str(buf, 0, bufl)); + continue loop; + } while(j=bufl){ - addInvalidLine(Util.byte2str(buf, 0, bufl)); - continue loop; - } + } + String tmp = sb.toString(); + if(HostKey.name2type(tmp)!=HostKey.UNKNOWN){ + type=HostKey.name2type(tmp); + } + else { j=bufl; } + if(j>=bufl){ + addInvalidLine(Util.byte2str(buf, 0, bufl)); + continue loop; + } while(j v = new ArrayList<>(); for(int i=0; i c=Class.forName(JSch.getConfig("hmac-sha1")); - hmacsha1=(MAC)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig("hmac-sha1")).asSubclass(MAC.class); + hmacsha1=c.getDeclaredConstructor().newInstance(); } catch(Exception e){ System.err.println("hmacsha1: "+e); diff --git a/src/main/java/com/jcraft/jsch/Log4j2Logger.java b/src/main/java/com/jcraft/jsch/Log4j2Logger.java new file mode 100644 index 00000000..5b4f22d8 --- /dev/null +++ b/src/main/java/com/jcraft/jsch/Log4j2Logger.java @@ -0,0 +1,38 @@ +package com.jcraft.jsch; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; + +public class Log4j2Logger implements com.jcraft.jsch.Logger { + + private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(JSch.class); + + public Log4j2Logger() {} + + @Override + public boolean isEnabled(int level) { + return logger.isEnabled(getLevel(level)); + } + + @Override + public void log(int level, String message) { + logger.log(getLevel(level), message); + } + + private static Level getLevel(int level) { + switch (level) { + case com.jcraft.jsch.Logger.DEBUG: + return Level.DEBUG; + case com.jcraft.jsch.Logger.INFO: + return Level.INFO; + case com.jcraft.jsch.Logger.WARN: + return Level.WARN; + case com.jcraft.jsch.Logger.ERROR: + return Level.ERROR; + case com.jcraft.jsch.Logger.FATAL: + return Level.FATAL; + default: + return Level.TRACE; + } + } +} diff --git a/src/main/java/com/jcraft/jsch/Packet.java b/src/main/java/com/jcraft/jsch/Packet.java index 063d0d6a..8d12be46 100644 --- a/src/main/java/com/jcraft/jsch/Packet.java +++ b/src/main/java/com/jcraft/jsch/Packet.java @@ -97,8 +97,8 @@ int shift(int len, int bsize, int mac){ // System.err.println("buffer.buffer.length="+buffer.buffer.length+" s="+(s)); System.arraycopy(buffer.buffer, - len+5+9, - buffer.buffer, s, buffer.index-5-9-len); + len+5+9, + buffer.buffer, s, buffer.index-5-9-len); buffer.index=10; buffer.putInt(len); @@ -107,8 +107,8 @@ int shift(int len, int bsize, int mac){ } void unshift(byte command, int recipient, int s, int len){ System.arraycopy(buffer.buffer, - s, - buffer.buffer, 5+9, len); + s, + buffer.buffer, 5+9, len); buffer.buffer[5]=command; buffer.index=6; buffer.putInt(recipient); diff --git a/src/main/java/com/jcraft/jsch/PortWatcher.java b/src/main/java/com/jcraft/jsch/PortWatcher.java index b93b46d0..fc244341 100644 --- a/src/main/java/com/jcraft/jsch/PortWatcher.java +++ b/src/main/java/com/jcraft/jsch/PortWatcher.java @@ -92,10 +92,10 @@ static String[] getPortForwarding(Session session){ Vector foo=new Vector<>(); synchronized(pool){ for(int i=0; i0){ diff --git a/src/main/java/com/jcraft/jsch/ProxyHTTP.java b/src/main/java/com/jcraft/jsch/ProxyHTTP.java index b538c26b..f5b5994b 100644 --- a/src/main/java/com/jcraft/jsch/ProxyHTTP.java +++ b/src/main/java/com/jcraft/jsch/ProxyHTTP.java @@ -48,8 +48,8 @@ public ProxyHTTP(String proxy_host){ String host=proxy_host; if(proxy_host.indexOf(':')!=-1){ try{ - host=proxy_host.substring(0, proxy_host.indexOf(':')); - port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1)); + host=proxy_host.substring(0, proxy_host.indexOf(':')); + port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1)); } catch(Exception e){ } @@ -86,11 +86,11 @@ public void connect(SocketFactory socket_factory, String host, int port, int tim out.write(Util.str2byte("CONNECT "+host+":"+port+" HTTP/1.0\r\n")); if(user!=null && passwd!=null){ - byte[] code=Util.str2byte(user+":"+passwd); - code=Util.toBase64(code, 0, code.length, true); - out.write(Util.str2byte("Proxy-Authorization: Basic ")); - out.write(code); - out.write(Util.str2byte("\r\n")); + byte[] code=Util.str2byte(user+":"+passwd); + code=Util.toBase64(code, 0, code.length, true); + out.write(Util.str2byte("Proxy-Authorization: Basic ")); + out.write(code); + out.write(Util.str2byte("\r\n")); } out.write(Util.str2byte("\r\n")); diff --git a/src/main/java/com/jcraft/jsch/ProxySOCKS4.java b/src/main/java/com/jcraft/jsch/ProxySOCKS4.java index 840c925f..1c0d6e36 100644 --- a/src/main/java/com/jcraft/jsch/ProxySOCKS4.java +++ b/src/main/java/com/jcraft/jsch/ProxySOCKS4.java @@ -53,8 +53,8 @@ public ProxySOCKS4(String proxy_host){ String host=proxy_host; if(proxy_host.indexOf(':')!=-1){ try{ - host=proxy_host.substring(0, proxy_host.indexOf(':')); - port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1)); + host=proxy_host.substring(0, proxy_host.indexOf(':')); + port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1)); } catch(Exception e){ } @@ -176,8 +176,8 @@ public void connect(SocketFactory socket_factory, String host, int port, int tim } if(buf[1]!=90){ try{ socket.close(); } - catch(Exception eee){ - } + catch(Exception eee){ + } String message="ProxySOCKS4: server returns CD "+buf[1]; throw new JSchException(message); } diff --git a/src/main/java/com/jcraft/jsch/ProxySOCKS5.java b/src/main/java/com/jcraft/jsch/ProxySOCKS5.java index dff403a7..4f255e11 100644 --- a/src/main/java/com/jcraft/jsch/ProxySOCKS5.java +++ b/src/main/java/com/jcraft/jsch/ProxySOCKS5.java @@ -53,8 +53,8 @@ public ProxySOCKS5(String proxy_host){ String host=proxy_host; if(proxy_host.indexOf(':')!=-1){ try{ - host=proxy_host.substring(0, proxy_host.indexOf(':')); - port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1)); + host=proxy_host.substring(0, proxy_host.indexOf(':')); + port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1)); } catch(Exception e){ } @@ -164,11 +164,11 @@ public void connect(SocketFactory socket_factory, String host, int port, int tim index=0; buf[index++]=1; buf[index++]=(byte)(user.length()); - System.arraycopy(Util.str2byte(user), 0, buf, index, user.length()); - index+=user.length(); + System.arraycopy(Util.str2byte(user), 0, buf, index, user.length()); + index+=user.length(); buf[index++]=(byte)(passwd.length()); - System.arraycopy(Util.str2byte(passwd), 0, buf, index, passwd.length()); - index+=passwd.length(); + System.arraycopy(Util.str2byte(passwd), 0, buf, index, passwd.length()); + index+=passwd.length(); out.write(buf, 0, index); @@ -196,8 +196,8 @@ public void connect(SocketFactory socket_factory, String host, int port, int tim if(!check){ try{ socket.close(); } - catch(Exception eee){ - } + catch(Exception eee){ + } throw new JSchException("fail in SOCKS5 proxy"); } @@ -283,8 +283,8 @@ public void connect(SocketFactory socket_factory, String host, int port, int tim if(buf[1]!=0){ try{ socket.close(); } - catch(Exception eee){ - } + catch(Exception eee){ + } throw new JSchException("ProxySOCKS5: server returns "+buf[1]); } @@ -292,13 +292,13 @@ public void connect(SocketFactory socket_factory, String host, int port, int tim case 1: //in.read(buf, 0, 6); fill(in, buf, 6); - break; + break; case 3: //in.read(buf, 0, 1); fill(in, buf, 1); //in.read(buf, 0, buf[0]+2); fill(in, buf, (buf[0]&0xff)+2); - break; + break; case 4: //in.read(buf, 0, 18); fill(in, buf, 18); diff --git a/src/main/java/com/jcraft/jsch/Request.java b/src/main/java/com/jcraft/jsch/Request.java index a3bca8f8..38cd98a9 100644 --- a/src/main/java/com/jcraft/jsch/Request.java +++ b/src/main/java/com/jcraft/jsch/Request.java @@ -51,9 +51,9 @@ void write(Packet packet) throws Exception{ long start=System.currentTimeMillis(); long timeout=channel.connectTimeout; while(channel.isConnected() && channel.reply==-1){ - try{Thread.sleep(10);} - catch(Exception ee){ - } + try{Thread.sleep(10);} + catch(Exception ee){ + } if(timeout>0L && (System.currentTimeMillis()-start)>timeout){ channel.reply=0; @@ -62,7 +62,7 @@ void write(Packet packet) throws Exception{ } if(channel.reply==0){ - throw new JSchException("failed to send channel request"); + throw new JSchException("failed to send channel request"); } } } diff --git a/src/main/java/com/jcraft/jsch/Session.java b/src/main/java/com/jcraft/jsch/Session.java index 3b5dabb6..671e1888 100644 --- a/src/main/java/com/jcraft/jsch/Session.java +++ b/src/main/java/com/jcraft/jsch/Session.java @@ -73,7 +73,7 @@ public class Session implements Runnable{ private static final int PACKET_MAX_SIZE = 256 * 1024; private byte[] V_S; // server version - private byte[] V_C=Util.str2byte("SSH-2.0-JSCH-"+Version.getVersion()); // client version + private byte[] V_C=Util.str2byte("SSH-2.0-JSCH_"+Version.getVersion()); // client version private byte[] I_C; // the payload of the client's SSH_MSG_KEXINIT private byte[] I_S; // the payload of the server's SSH_MSG_KEXINIT @@ -193,8 +193,8 @@ public void connect(int connectTimeout) throws JSchException{ io=new IO(); if(random==null){ try{ - Class c=Class.forName(getConfig("random")); - random=(Random)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(getConfig("random")).asSubclass(Random.class); + random=c.getDeclaredConstructor().newInstance(); } catch(Exception e){ throw new JSchException(e.toString(), e); @@ -207,34 +207,34 @@ public void connect(int connectTimeout) throws JSchException{ "Connecting to "+host+" port "+port); } - try { + try { int i, j; if(proxy==null){ InputStream in; OutputStream out; - if(socket_factory==null){ + if(socket_factory==null){ socket=Util.createSocket(host, port, connectTimeout); - in=socket.getInputStream(); - out=socket.getOutputStream(); - } - else{ + in=socket.getInputStream(); + out=socket.getOutputStream(); + } + else{ socket=socket_factory.createSocket(host, port); - in=socket_factory.getInputStream(socket); - out=socket_factory.getOutputStream(socket); - } - //if(timeout>0){ socket.setSoTimeout(timeout); } + in=socket_factory.getInputStream(socket); + out=socket_factory.getOutputStream(socket); + } + //if(timeout>0){ socket.setSoTimeout(timeout); } socket.setTcpNoDelay(true); io.setInputStream(in); io.setOutputStream(out); } else{ - synchronized(proxy){ + synchronized(proxy){ proxy.connect(socket_factory, host, port, connectTimeout); - io.setInputStream(proxy.getInputStream()); - io.setOutputStream(proxy.getOutputStream()); + io.setInputStream(proxy.getInputStream()); + io.setOutputStream(proxy.getOutputStream()); socket=proxy.getSocket(); - } + } } if(connectTimeout>0 && socket!=null){ @@ -251,12 +251,12 @@ public void connect(int connectTimeout) throws JSchException{ jsch.addSession(this); { - // Some Cisco devices will miss to read '\n' if it is sent separately. - byte[] foo=new byte[V_C.length+2]; - System.arraycopy(V_C, 0, foo, 0, V_C.length); - foo[foo.length-2]=(byte)'\r'; - foo[foo.length-1]=(byte)'\n'; - io.put(foo, 0, foo.length); + // Some Cisco devices will miss to read '\n' if it is sent separately. + byte[] foo=new byte[V_C.length+2]; + System.arraycopy(V_C, 0, foo, 0, V_C.length); + foo[foo.length-2]=(byte)'\r'; + foo[foo.length-1]=(byte)'\n'; + io.put(foo, 0, foo.length); } while(true){ @@ -314,7 +314,7 @@ public void connect(int connectTimeout) throws JSchException{ buf=read(buf); if(buf.getCommand()!=SSH_MSG_KEXINIT){ in_kex=false; - throw new JSchException("invalid protocol: "+buf.getCommand()); + throw new JSchException("invalid protocol: "+buf.getCommand()); } if(JSch.getLogger().isEnabled(Logger.INFO)){ @@ -325,23 +325,23 @@ public void connect(int connectTimeout) throws JSchException{ KeyExchange kex=receive_kexinit(buf); while(true){ - buf=read(buf); - if(kex.getState()==buf.getCommand()){ + buf=read(buf); + if(kex.getState()==buf.getCommand()){ kex_start_time=System.currentTimeMillis(); boolean result=kex.next(buf); - if(!result){ - //System.err.println("verify: "+result); + if(!result){ + //System.err.println("verify: "+result); in_kex=false; - throw new JSchException("verify: "+result); - } - } - else{ + throw new JSchException("verify: "+result); + } + } + else{ in_kex=false; - throw new JSchException("invalid protocol(kex): "+buf.getCommand()); - } - if(kex.getState()==KeyExchange.STATE_END){ - break; - } + throw new JSchException("invalid protocol(kex): "+buf.getCommand()); + } + if(kex.getState()==KeyExchange.STATE_END){ + break; + } } try{ @@ -369,11 +369,11 @@ public void connect(int connectTimeout) throws JSchException{ "SSH_MSG_NEWKEYS received"); } - receive_newkeys(buf, kex); + receive_newkeys(buf, kex); } else{ in_kex=false; - throw new JSchException("invalid protocol(newkyes): "+buf.getCommand()); + throw new JSchException("invalid protocol(newkyes): "+buf.getCommand()); } try{ @@ -391,8 +391,8 @@ public void connect(int connectTimeout) throws JSchException{ UserAuth ua=null; try{ - Class c=Class.forName(getConfig("userauth.none")); - ua=(UserAuth)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(getConfig("userauth.none")).asSubclass(UserAuth.class); + ua=c.getDeclaredConstructor().newInstance(); } catch(Exception e){ throw new JSchException(e.toString(), e); @@ -424,8 +424,8 @@ public void connect(int connectTimeout) throws JSchException{ loop: while(true){ - while(!auth && - cmethoda!=null && methodi c=null; + Class c=null; if(getConfig("userauth."+method)!=null){ - c=Class.forName(getConfig("userauth."+method)); - ua=(UserAuth)(c.getDeclaredConstructor().newInstance()); + c=Class.forName(getConfig("userauth."+method)).asSubclass(UserAuth.class); + ua=c.getDeclaredConstructor().newInstance(); } } catch(Exception e){ @@ -469,46 +469,46 @@ public void connect(int connectTimeout) throws JSchException{ } } - if(ua!=null){ + if(ua!=null){ auth_cancel=false; - try{ - auth=ua.start(this); + try{ + auth=ua.start(this); if(auth && JSch.getLogger().isEnabled(Logger.INFO)){ JSch.getLogger().log(Logger.INFO, "Authentication succeeded ("+method+")."); } - } - catch(JSchAuthCancelException ee){ - auth_cancel=true; - } - catch(JSchPartialAuthException ee){ + } + catch(JSchAuthCancelException ee){ + auth_cancel=true; + } + catch(JSchPartialAuthException ee){ String tmp = smethods; smethods=ee.getMethods(); smethoda=Util.split(smethods, ","); if(!tmp.equals(smethods)){ methodi=0; } - //System.err.println("PartialAuth: "+methods); - auth_cancel=false; - continue loop; - } - catch(RuntimeException ee){ - throw ee; - } - catch(JSchException ee){ + //System.err.println("PartialAuth: "+methods); + auth_cancel=false; + continue loop; + } + catch(RuntimeException ee){ + throw ee; + } + catch(JSchException ee){ throw ee; - } - catch(Exception ee){ - //System.err.println("ee: "+ee); // SSH_MSG_DISCONNECT: 2 Too many authentication failures + } + catch(Exception ee){ + //System.err.println("ee: "+ee); // SSH_MSG_DISCONNECT: 2 Too many authentication failures if(JSch.getLogger().isEnabled(Logger.WARN)){ JSch.getLogger().log(Logger.WARN, "an exception during authentication\n"+ee.toString()); } break loop; - } - } - } + } + } + } break; } @@ -608,8 +608,8 @@ private KeyExchange receive_kexinit(Buffer buf) throws Exception { KeyExchange kex=null; try{ - Class c=Class.forName(getConfig(guess[KeyExchange.PROPOSAL_KEX_ALGS])); - kex=(KeyExchange)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(getConfig(guess[KeyExchange.PROPOSAL_KEX_ALGS])).asSubclass(KeyExchange.class); + kex=c.getDeclaredConstructor().newInstance(); } catch(Exception | NoClassDefFoundError e){ throw new JSchException(e.toString(), e); @@ -872,7 +872,7 @@ private void checkHost(String chost, int port, KeyExchange kex) throws JSchExcep i==HostKeyRepository.CHANGED){ String file=null; synchronized(hkr){ - file=hkr.getKnownHostsRepositoryID(); + file=hkr.getKnownHostsRepositoryID(); } if(file==null){file="known_hosts";} @@ -913,24 +913,24 @@ private void checkHost(String chost, int port, KeyExchange kex) throws JSchExcep if((shkc.equals("ask") || shkc.equals("yes")) && (i!=HostKeyRepository.OK) && !insert){ if(shkc.equals("yes")){ - throw new JSchException("reject HostKey: "+host); + throw new JSchException("reject HostKey: "+host); } //System.err.println("finger-print: "+key_fprint); if(userinfo!=null){ - boolean foo=userinfo.promptYesNo( + boolean foo=userinfo.promptYesNo( "The authenticity of host '"+host+"' can't be established.\n"+ key_type+" key fingerprint is "+key_fprint+".\n"+ "Are you sure you want to continue connecting?" - ); - if(!foo){ - throw new JSchException("reject HostKey: "+host); - } - insert=true; + ); + if(!foo){ + throw new JSchException("reject HostKey: "+host); + } + insert=true; } else{ - if(i==HostKeyRepository.NOT_INCLUDED) - throw new JSchException("UnknownHostKey: "+host+". "+key_type+" key fingerprint is "+key_fprint); - else + if(i==HostKeyRepository.NOT_INCLUDED) + throw new JSchException("UnknownHostKey: "+host+". "+key_type+" key fingerprint is "+key_fprint); + else throw new JSchException("HostKey has been changed: "+host); } } @@ -976,7 +976,7 @@ private void checkHost(String chost, int port, KeyExchange kex) throws JSchExcep if(insert){ synchronized(hkr){ - hkr.add(hostkey, userinfo); + hkr.add(hostkey, userinfo); } } } @@ -1103,7 +1103,7 @@ public Buffer read(Buffer buf) throws Exception{ if(JSch.getLogger().isEnabled(Logger.FATAL)){ JSch.getLogger().log(Logger.FATAL, message); } - start_discard(buf, s2ccipher, s2cmac, j, PACKET_MAX_SIZE-4); + start_discard(buf, s2ccipher, s2cmac, j, PACKET_MAX_SIZE-s2ccipher_size); } io.getByte(buf.buffer, buf.index, j); @@ -1144,7 +1144,7 @@ else if(isAEAD || isEtM){ if(JSch.getLogger().isEnabled(Logger.FATAL)){ JSch.getLogger().log(Logger.FATAL, message); } - start_discard(buf, s2ccipher, s2cmac, j, PACKET_MAX_SIZE-4); + start_discard(buf, s2ccipher, s2cmac, j, PACKET_MAX_SIZE-s2ccipher_size); } io.getByte(buf.buffer, buf.index, j); buf.index+=(j); @@ -1233,17 +1233,17 @@ else if(isAEAD || isEtM){ if(inflater!=null){ //inflater.uncompress(buf); - int pad=buf.buffer[4]; - uncompress_len[0]=buf.index-5-pad; - byte[] foo=inflater.uncompress(buf.buffer, 5, uncompress_len); - if(foo!=null){ - buf.buffer=foo; - buf.index=5+uncompress_len[0]; - } - else{ - System.err.println("fail in inflater"); - break; - } + int pad=buf.buffer[4]; + uncompress_len[0]=buf.index-5-pad; + byte[] foo=inflater.uncompress(buf.buffer, 5, uncompress_len); + if(foo!=null){ + buf.buffer=foo; + buf.index=5+uncompress_len[0]; + } + else{ + System.err.println("fail in inflater"); + break; + } } int type=buf.getCommand()&0xff; @@ -1251,21 +1251,21 @@ else if(isAEAD || isEtM){ if(type==SSH_MSG_DISCONNECT){ buf.rewind(); buf.getInt();buf.getShort(); - int reason_code=buf.getInt(); - byte[] description=buf.getString(); - byte[] language_tag=buf.getString(); - throw new JSchException("SSH_MSG_DISCONNECT: "+ - reason_code+ - " "+Util.byte2str(description)+ - " "+Util.byte2str(language_tag)); - //break; + int reason_code=buf.getInt(); + byte[] description=buf.getString(); + byte[] language_tag=buf.getString(); + throw new JSchException("SSH_MSG_DISCONNECT: "+ + reason_code+ + " "+Util.byte2str(description)+ + " "+Util.byte2str(language_tag)); + //break; } else if(type==SSH_MSG_IGNORE){ } else if(type==SSH_MSG_UNIMPLEMENTED){ buf.rewind(); buf.getInt();buf.getShort(); - int reason_id=buf.getInt(); + int reason_id=buf.getInt(); if(JSch.getLogger().isEnabled(Logger.INFO)){ JSch.getLogger().log(Logger.INFO, "Received SSH_MSG_UNIMPLEMENTED for "+reason_id); @@ -1275,23 +1275,23 @@ else if(type==SSH_MSG_DEBUG){ buf.rewind(); buf.getInt();buf.getShort(); /* - byte always_display=(byte)buf.getByte(); - byte[] message=buf.getString(); - byte[] language_tag=buf.getString(); - System.err.println("SSH_MSG_DEBUG:"+ - " "+Util.byte2str(message)+ - " "+Util.byte2str(language_tag)); + byte always_display=(byte)buf.getByte(); + byte[] message=buf.getString(); + byte[] language_tag=buf.getString(); + System.err.println("SSH_MSG_DEBUG:"+ + " "+Util.byte2str(message)+ + " "+Util.byte2str(language_tag)); */ } else if(type==SSH_MSG_CHANNEL_WINDOW_ADJUST){ buf.rewind(); buf.getInt();buf.getShort(); - Channel c=Channel.getChannel(buf.getInt(), this); - if(c==null){ - } - else{ - c.addRemoteWindowSize(buf.getUInt()); - } + Channel c=Channel.getChannel(buf.getInt(), this); + if(c==null){ + } + else{ + c.addRemoteWindowSize(buf.getUInt()); + } } else if(type==SSH_MSG_EXT_INFO){ buf.rewind(); @@ -1365,7 +1365,6 @@ else if(type==UserAuth.SSH_MSG_USERAUTH_SUCCESS){ } private void start_discard(Buffer buf, Cipher cipher, MAC mac, - int packet_length, int discard) throws JSchException, IOException{ start_discard(buf, cipher, mac, packet_length, discard, null); } @@ -1374,7 +1373,7 @@ private void start_discard(Buffer buf, Cipher cipher, MAC mac, int packet_length, int discard, Throwable t) throws JSchException, IOException{ MAC discard_mac = null; - if(!cipher.isCBC()){ + if(!cipher.isCBC() || (mac != null && mac.isEtM())){ if(t!=null){ throw new JSchException("Packet corrupt", t); } @@ -1465,12 +1464,13 @@ private void updateKeys(KeyExchange kex) throws Exception{ MACs2c=hash.digest(); try{ - Class c; + Class cc; + Class cm; String method; method=guess[KeyExchange.PROPOSAL_ENC_ALGS_STOC]; - c=Class.forName(getConfig(method)); - s2ccipher=(Cipher)(c.getDeclaredConstructor().newInstance()); + cc=Class.forName(getConfig(method)).asSubclass(Cipher.class); + s2ccipher=cc.getDeclaredConstructor().newInstance(); while(s2ccipher.getBlockSize()>Es2c.length){ buf.reset(); buf.putMPInt(K); @@ -1479,17 +1479,17 @@ private void updateKeys(KeyExchange kex) throws Exception{ hash.update(buf.buffer, 0, buf.index); byte[] foo=hash.digest(); byte[] bar=new byte[Es2c.length+foo.length]; - System.arraycopy(Es2c, 0, bar, 0, Es2c.length); - System.arraycopy(foo, 0, bar, Es2c.length, foo.length); - Es2c=bar; + System.arraycopy(Es2c, 0, bar, 0, Es2c.length); + System.arraycopy(foo, 0, bar, Es2c.length, foo.length); + Es2c=bar; } s2ccipher.init(Cipher.DECRYPT_MODE, Es2c, IVs2c); s2ccipher_size=s2ccipher.getIVSize(); if(!s2ccipher.isAEAD()){ method=guess[KeyExchange.PROPOSAL_MAC_ALGS_STOC]; - c=Class.forName(getConfig(method)); - s2cmac=(MAC)(c.getDeclaredConstructor().newInstance()); + cm=Class.forName(getConfig(method)).asSubclass(MAC.class); + s2cmac=cm.getDeclaredConstructor().newInstance(); MACs2c = expandKey(buf, K, H, MACs2c, hash, s2cmac.getBlockSize()); s2cmac.init(MACs2c); //mac_buf=new byte[s2cmac.getBlockSize()]; @@ -1498,8 +1498,8 @@ private void updateKeys(KeyExchange kex) throws Exception{ } method=guess[KeyExchange.PROPOSAL_ENC_ALGS_CTOS]; - c=Class.forName(getConfig(method)); - c2scipher=(Cipher)(c.getDeclaredConstructor().newInstance()); + cc=Class.forName(getConfig(method)).asSubclass(Cipher.class); + c2scipher=cc.getDeclaredConstructor().newInstance(); while(c2scipher.getBlockSize()>Ec2s.length){ buf.reset(); buf.putMPInt(K); @@ -1508,17 +1508,17 @@ private void updateKeys(KeyExchange kex) throws Exception{ hash.update(buf.buffer, 0, buf.index); byte[] foo=hash.digest(); byte[] bar=new byte[Ec2s.length+foo.length]; - System.arraycopy(Ec2s, 0, bar, 0, Ec2s.length); - System.arraycopy(foo, 0, bar, Ec2s.length, foo.length); - Ec2s=bar; + System.arraycopy(Ec2s, 0, bar, 0, Ec2s.length); + System.arraycopy(foo, 0, bar, Ec2s.length, foo.length); + Ec2s=bar; } c2scipher.init(Cipher.ENCRYPT_MODE, Ec2s, IVc2s); c2scipher_size=c2scipher.getIVSize(); if(!c2scipher.isAEAD()){ method=guess[KeyExchange.PROPOSAL_MAC_ALGS_CTOS]; - c=Class.forName(getConfig(method)); - c2smac=(MAC)(c.getDeclaredConstructor().newInstance()); + cm=Class.forName(getConfig(method)).asSubclass(MAC.class); + c2smac=cm.getDeclaredConstructor().newInstance(); MACc2s = expandKey(buf, K, H, MACc2s, hash, c2smac.getBlockSize()); c2smac.init(MACc2s); } @@ -1607,7 +1607,7 @@ private byte[] expandKey(Buffer buf, byte[] K, byte[] H, byte[] key, } if(c.close || !c.isConnected()){ - throw new IOException("channel is broken"); + throw new IOException("channel is broken"); } boolean sendit=false; @@ -1615,8 +1615,8 @@ private byte[] expandKey(Buffer buf, byte[] K, byte[] H, byte[] key, byte command=0; int recipient=-1; synchronized(c){ - if(c.rwsize>0){ - long len=c.rwsize; + if(c.rwsize>0){ + long len=c.rwsize; if(len>length){ len=length; } @@ -1625,19 +1625,19 @@ private byte[] expandKey(Buffer buf, byte[] K, byte[] H, byte[] key, (c2scipher!=null ? c2scipher_size : 8), (c2smac!=null ? c2smac.getBlockSize() : 0)); } - command=packet.buffer.getCommand(); - recipient=c.getRecipient(); - length-=len; - c.rwsize-=len; - sendit=true; - } + command=packet.buffer.getCommand(); + recipient=c.getRecipient(); + length-=len; + c.rwsize-=len; + sendit=true; + } } if(sendit){ - _write(packet); + _write(packet); if(length==0){ return; } - packet.unshift(command, recipient, s, length); + packet.unshift(command, recipient, s, length); } synchronized(c){ @@ -1720,7 +1720,7 @@ public void run(){ int stimeout=0; try{ while(isConnected && - thread!=null){ + thread!=null){ try{ buf=read(buf); stimeout=0; @@ -1738,149 +1738,149 @@ else if(in_kex && stimeout c=Class.forName(foo); - deflater=(Compression)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(foo).asSubclass(Compression.class); + deflater=c.getDeclaredConstructor().newInstance(); int level=6; try{ level=Integer.parseInt(getConfig("compression_level"));} catch(Exception ee){ } @@ -2592,13 +2592,13 @@ private void initInflater(String method) throws JSchException{ if(method.equals("zlib") || (isAuthed && method.equals("zlib@openssh.com"))){ try{ - Class c=Class.forName(foo); - inflater=(Compression)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(foo).asSubclass(Compression.class); + inflater=c.getDeclaredConstructor().newInstance(); inflater.init(Compression.INFLATER, 0); } catch(Exception ee){ throw new JSchException(ee.toString(), ee); - //System.err.println(foo+" isn't accessible."); + //System.err.println(foo+" isn't accessible."); } } } @@ -2846,8 +2846,8 @@ private String[] checkCiphers(String ciphers){ static boolean checkCipher(String cipher){ try{ - Class c=Class.forName(cipher); - Cipher _c=(Cipher)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(cipher).asSubclass(Cipher.class); + Cipher _c=c.getDeclaredConstructor().newInstance(); _c.init(Cipher.ENCRYPT_MODE, new byte[_c.getBlockSize()], new byte[_c.getIVSize()]); @@ -2897,8 +2897,8 @@ private String[] checkMacs(String macs){ static boolean checkMac(String mac){ try{ - Class c=Class.forName(mac); - MAC _c=(MAC)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(mac).asSubclass(MAC.class); + MAC _c=c.getDeclaredConstructor().newInstance(); _c.init(new byte[_c.getBlockSize()]); return true; } @@ -2940,8 +2940,8 @@ private String[] checkKexes(String kexes){ static boolean checkKex(Session s, String kex){ try{ - Class c=Class.forName(kex); - KeyExchange _c=(KeyExchange)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(kex).asSubclass(KeyExchange.class); + KeyExchange _c=c.getDeclaredConstructor().newInstance(); _c.init(s ,null, null, null, null); return true; } @@ -2961,8 +2961,8 @@ private String[] checkSignatures(String sigs){ String[] _sigs=Util.split(sigs, ","); for(int i=0; i<_sigs.length; i++){ try{ - Class c=Class.forName(JSch.getConfig(_sigs[i])); - final Signature sig=(Signature)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(JSch.getConfig(_sigs[i])).asSubclass(Signature.class); + final Signature sig=c.getDeclaredConstructor().newInstance(); sig.init(); } catch(Exception | NoClassDefFoundError e){ diff --git a/src/main/java/com/jcraft/jsch/SftpATTRS.java b/src/main/java/com/jcraft/jsch/SftpATTRS.java index 42d48aa1..c98877b2 100644 --- a/src/main/java/com/jcraft/jsch/SftpATTRS.java +++ b/src/main/java/com/jcraft/jsch/SftpATTRS.java @@ -145,7 +145,7 @@ private SftpATTRS(){ } static SftpATTRS getATTR(Buffer buf){ - SftpATTRS attr=new SftpATTRS(); + SftpATTRS attr=new SftpATTRS(); attr.flags=buf.getInt(); if((attr.flags&SSH_FILEXFER_ATTR_SIZE)!=0){ attr.size=buf.getLong(); } if((attr.flags&SSH_FILEXFER_ATTR_UIDGID)!=0){ @@ -163,11 +163,11 @@ static SftpATTRS getATTR(Buffer buf){ if((attr.flags&SSH_FILEXFER_ATTR_EXTENDED)!=0){ int count=buf.getInt(); if(count>0){ - attr.extended=new String[count*2]; - for(int i=0; i0){ - for(int i=0; i0){ - for(int i=0; i c=Class.forName(session.getConfig(method)); - context=(GSSContext)(c.getDeclaredConstructor().newInstance()); + Class c=Class.forName(session.getConfig(method)).asSubclass(GSSContext.class); + context=c.getDeclaredConstructor().newInstance(); } catch(Exception e){ return false; diff --git a/src/main/java/com/jcraft/jsch/UserAuthKeyboardInteractive.java b/src/main/java/com/jcraft/jsch/UserAuthKeyboardInteractive.java index 7bb63d2e..1efe03a9 100644 --- a/src/main/java/com/jcraft/jsch/UserAuthKeyboardInteractive.java +++ b/src/main/java/com/jcraft/jsch/UserAuthKeyboardInteractive.java @@ -52,7 +52,7 @@ public boolean start(Session session) throws Exception{ while(true){ if(session.auth_failures >= session.max_auth_tries){ - return false; + return false; } // send @@ -78,53 +78,53 @@ public boolean start(Session session) throws Exception{ buf=session.read(buf); int command=buf.getCommand()&0xff; - if(command==SSH_MSG_USERAUTH_SUCCESS){ - return true; - } - if(command==SSH_MSG_USERAUTH_BANNER){ - buf.getInt(); buf.getByte(); buf.getByte(); - byte[] _message=buf.getString(); - byte[] lang=buf.getString(); - String message=Util.byte2str(_message); - if(userinfo!=null){ - userinfo.showMessage(message); - } - continue loop; - } - if(command==SSH_MSG_USERAUTH_FAILURE){ - buf.getInt(); buf.getByte(); buf.getByte(); - byte[] foo=buf.getString(); - int partial_success=buf.getByte(); -// System.err.println(new String(foo)+ -// " partial_success:"+(partial_success!=0)); - - if(partial_success!=0){ - throw new JSchPartialAuthException(Util.byte2str(foo)); - } - - if(firsttime){ - return false; - //throw new JSchException("USERAUTH KI is not supported"); - //cancel=true; // ?? - } + if(command==SSH_MSG_USERAUTH_SUCCESS){ + return true; + } + if(command==SSH_MSG_USERAUTH_BANNER){ + buf.getInt(); buf.getByte(); buf.getByte(); + byte[] _message=buf.getString(); + byte[] lang=buf.getString(); + String message=Util.byte2str(_message); + if(userinfo!=null){ + userinfo.showMessage(message); + } + continue loop; + } + if(command==SSH_MSG_USERAUTH_FAILURE){ + buf.getInt(); buf.getByte(); buf.getByte(); + byte[] foo=buf.getString(); + int partial_success=buf.getByte(); +// System.err.println(new String(foo)+ +// " partial_success:"+(partial_success!=0)); + + if(partial_success!=0){ + throw new JSchPartialAuthException(Util.byte2str(foo)); + } + + if(firsttime){ + return false; + //throw new JSchException("USERAUTH KI is not supported"); + //cancel=true; // ?? + } session.auth_failures++; - break; - } - if(command==SSH_MSG_USERAUTH_INFO_REQUEST){ - firsttime=false; - buf.getInt(); buf.getByte(); buf.getByte(); - String name=Util.byte2str(buf.getString()); - String instruction=Util.byte2str(buf.getString()); - String languate_tag=Util.byte2str(buf.getString()); - int num=buf.getInt(); - String[] prompt=new String[num]; - boolean[] echo=new boolean[num]; - for(int i=0; i0 - ||(name.length()>0 || instruction.length()>0) - ){ - if(userinfo!=null){ + ||(name.length()>0 || instruction.length()>0) + ){ + if(userinfo!=null){ UIKeyboardInteractive kbi=(UIKeyboardInteractive)userinfo; String[] _response=kbi.promptKeyboardInteractive(dest, name, @@ -150,19 +150,19 @@ else if(num>0 response[i]=Util.str2byte(_response[i]); } } - } - } - - // byte SSH_MSG_USERAUTH_INFO_RESPONSE(61) - // int num-responses - // string response[1] (ISO-10646 UTF-8) - // ... - // string response[num-responses] (ISO-10646 UTF-8) - packet.reset(); - buf.putByte((byte)SSH_MSG_USERAUTH_INFO_RESPONSE); - if(num>0 && - (response==null || // cancel - num!=response.length)){ + } + } + + // byte SSH_MSG_USERAUTH_INFO_RESPONSE(61) + // int num-responses + // string response[1] (ISO-10646 UTF-8) + // ... + // string response[num-responses] (ISO-10646 UTF-8) + packet.reset(); + buf.putByte((byte)SSH_MSG_USERAUTH_INFO_RESPONSE); + if(num>0 && + (response==null || // cancel + num!=response.length)){ if(response==null){ // working around the bug in OpenSSH ;-< @@ -175,28 +175,28 @@ else if(num>0 buf.putInt(0); } - if(response==null) - cancel=true; - } - else{ - buf.putInt(num); - for(int i=0; i=0){ - bar.addElement(Util.byte2str(buf, start, index-start)); - start=index+1; - continue; + bar.addElement(Util.byte2str(buf, start, index-start)); + start=index+1; + continue; } bar.addElement(Util.byte2str(buf, start, buf.length-start)); break; @@ -140,7 +140,7 @@ static boolean glob(byte[] pattern, byte[] name){ return glob0(pattern, 0, name, 0); } static private boolean glob0(byte[] pattern, int pattern_index, - byte[] name, int name_index){ + byte[] name, int name_index){ if(name.length>0 && name[0]=='.'){ if(pattern.length>0 && pattern[0]=='.'){ if(pattern.length==2 && pattern[1]=='*') return true; @@ -151,7 +151,7 @@ static private boolean glob0(byte[] pattern, int pattern_index, return glob(pattern, pattern_index, name, name_index); } static private boolean glob(byte[] pattern, int pattern_index, - byte[] name, int name_index){ + byte[] name, int name_index){ //System.err.println("glob: "+new String(pattern)+", "+pattern_index+" "+new String(name)+", "+name_index); int patternlen=pattern.length; @@ -164,14 +164,14 @@ static private boolean glob(byte[] pattern, int pattern_index, while(i0); + } + catch(java.util.zip.DataFormatException e){ + if(JSch.getLogger().isEnabled(Logger.WARN)){ + JSch.getLogger().log(Logger.WARN, + "an exception during uncompress\n"+e.toString()); + } + } + + if(buf.length0){ - s1+=buf[index++]&0xff; s2+=s1; + s1+=buf[index++]&0xff; s2+=s1; } s1%=BASE; s2%=BASE; diff --git a/src/main/java/com/jcraft/jsch/jzlib/CRC32.java b/src/main/java/com/jcraft/jsch/jzlib/CRC32.java index 94af2ef7..969030bc 100644 --- a/src/main/java/com/jcraft/jsch/jzlib/CRC32.java +++ b/src/main/java/com/jcraft/jsch/jzlib/CRC32.java @@ -47,7 +47,7 @@ final class CRC32 implements Checksum { int c = n; for (int k = 8; --k >= 0; ) { if ((c & 1) != 0) - c = 0xedb88320 ^ (c >>> 1); + c = 0xedb88320 ^ (c >>> 1); else c = c >>> 1; } diff --git a/src/main/java/com/jcraft/jsch/jzlib/Compression.java b/src/main/java/com/jcraft/jsch/jzlib/Compression.java index 348cb0b5..6e966577 100644 --- a/src/main/java/com/jcraft/jsch/jzlib/Compression.java +++ b/src/main/java/com/jcraft/jsch/jzlib/Compression.java @@ -36,6 +36,7 @@ public class Compression implements com.jcraft.jsch.Compression { private Deflater deflater; private Inflater inflater; private byte[] tmpbuf=new byte[BUF_SIZE]; + private byte[] inflated_buf; public Compression(){ } @@ -49,10 +50,12 @@ else if(type==INFLATER){ inflater=new Inflater(); inflated_buf=new byte[BUF_SIZE]; } + if(JSch.getLogger().isEnabled(Logger.DEBUG)){ + JSch.getLogger().log(Logger.DEBUG, + "zlib using "+this.getClass().getCanonicalName()); + } } - private byte[] inflated_buf; - @Override public byte[] compress(byte[] buf, int start, int[] len){ deflater.next_in=buf; @@ -80,7 +83,11 @@ public byte[] compress(byte[] buf, int start, int[] len){ outputlen+=tmp; break; default: - System.err.println("compress: deflate returnd "+status); + if(JSch.getLogger().isEnabled(Logger.WARN)){ + JSch.getLogger().log(Logger.WARN, + "compress: deflate returnd "+status); + } + } } while(deflater.avail_out==0); @@ -104,34 +111,37 @@ public byte[] uncompress(byte[] buffer, int start, int[] length){ int status=inflater.inflate(JZlib.Z_PARTIAL_FLUSH); switch(status){ case JZlib.Z_OK: - if(inflated_buf.lengthbuffer.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{ + 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); + if(JSch.getLogger().isEnabled(Logger.WARN)){ + JSch.getLogger().log(Logger.WARN, + "uncompress: inflate returnd "+status); + } return null; } } diff --git a/src/main/java/com/jcraft/jsch/jzlib/Deflate.java b/src/main/java/com/jcraft/jsch/jzlib/Deflate.java index 7d1e5e6b..f30f274e 100644 --- a/src/main/java/com/jcraft/jsch/jzlib/Deflate.java +++ b/src/main/java/com/jcraft/jsch/jzlib/Deflate.java @@ -50,7 +50,7 @@ static class Config{ int max_chain; int func; Config(int good_length, int max_lazy, - int nice_length, int max_chain, int func){ + int nice_length, int max_chain, int func){ this.good_length=good_length; this.max_lazy=max_lazy; this.nice_length=nice_length; @@ -385,15 +385,15 @@ void init_block(){ // when the heap property is re-established (each father smaller than its // two sons). void pqdownheap(short[] tree, // the tree to restore - int k // node to move down - ){ + int k // node to move down + ){ int v = heap[k]; int j = k << 1; // left son of k while (j <= heap_len) { // Set j to the smallest of the two sons: if (j < heap_len && - smaller(tree, heap[j+1], heap[j], depth)){ - j++; + smaller(tree, heap[j+1], heap[j], depth)){ + j++; } // Exit if v is smaller than both sons if(smaller(tree, v, heap[j], depth)) break; @@ -410,14 +410,14 @@ static boolean smaller(short[] tree, int n, int m, byte[] depth){ short tn2=tree[n*2]; short tm2=tree[m*2]; return (tn2>>8); pending_buf[d_buf+last_lit*2+1] = (byte)dist; @@ -651,8 +651,8 @@ boolean _tr_tally (int dist, // distance of matched string int in_length = strstart - block_start; int dcode; for (dcode = 0; dcode < D_CODES; dcode++) { - out_length += (int)dyn_dtree[dcode*2] * - (5L+Tree.extra_dbits[dcode]); + out_length += (int)dyn_dtree[dcode*2] * + (5L+Tree.extra_dbits[dcode]); } out_length >>>= 3; if ((matches < (last_lit/2)) && out_length < in_length/2) return true; @@ -674,35 +674,35 @@ void compress_block(short[] ltree, short[] dtree){ if (last_lit != 0){ do{ - dist=((pending_buf[d_buf+lx*2]<<8)&0xff00)| - (pending_buf[d_buf+lx*2+1]&0xff); - lc=(l_buf[lx])&0xff; lx++; - - if(dist == 0){ - send_code(lc, ltree); // send a literal byte - } - else{ - // Here, lc is the match length - MIN_MATCH - code = Tree._length_code[lc]; - - send_code(code+LITERALS+1, ltree); // send the length code - extra = Tree.extra_lbits[code]; - if(extra != 0){ - lc -= Tree.base_length[code]; - send_bits(lc, extra); // send the extra length bits - } - dist--; // dist is now the match distance - 1 - code = Tree.d_code(dist); - - send_code(code, dtree); // send the distance code - extra = Tree.extra_dbits[code]; - if (extra != 0) { - dist -= Tree.base_dist[code]; - send_bits(dist, extra); // send the extra distance bits - } - } // literal or match pair ? - - // Check that the overlay between pending_buf and d_buf+l_buf is ok: + dist=((pending_buf[d_buf+lx*2]<<8)&0xff00)| + (pending_buf[d_buf+lx*2+1]&0xff); + lc=(l_buf[lx])&0xff; lx++; + + if(dist == 0){ + send_code(lc, ltree); // send a literal byte + } + else{ + // Here, lc is the match length - MIN_MATCH + code = Tree._length_code[lc]; + + send_code(code+LITERALS+1, ltree); // send the length code + extra = Tree.extra_lbits[code]; + if(extra != 0){ + lc -= Tree.base_length[code]; + send_bits(lc, extra); // send the extra length bits + } + dist--; // dist is now the match distance - 1 + code = Tree.d_code(dist); + + send_code(code, dtree); // send the distance code + extra = Tree.extra_dbits[code]; + if (extra != 0) { + dist -= Tree.base_dist[code]; + send_bits(dist, extra); // send the extra distance bits + } + } // literal or match pair ? + + // Check that the overlay between pending_buf and d_buf+l_buf is ok: } while (lx < last_lit); } @@ -753,9 +753,9 @@ void bi_windup(){ // Copy a stored block, storing first the length and its // one's complement if requested. void copy_block(int buf, // the input data - int len, // its length - boolean header // true if block header must be written - ){ + int len, // its length + boolean header // true if block header must be written + ){ int index=0; bi_windup(); // align on byte boundary last_eob_len = 8; // enough lookahead for inflate @@ -774,8 +774,8 @@ void copy_block(int buf, // the input data void flush_block_only(boolean eof){ _tr_flush_block(block_start>=0 ? block_start : -1, - strstart-block_start, - eof); + strstart-block_start, + eof); block_start=strstart; strm.flush_pending(); } @@ -802,9 +802,9 @@ int deflate_stored(int flush){ while(true){ // Fill the window as much as possible: if(lookahead<=1){ - fill_window(); - if(lookahead==0 && flush==Z_NO_FLUSH) return NeedMore; - if(lookahead==0) break; // flush the current block + fill_window(); + if(lookahead==0 && flush==Z_NO_FLUSH) return NeedMore; + if(lookahead==0) break; // flush the current block } strstart+=lookahead; @@ -813,20 +813,20 @@ int deflate_stored(int flush){ // Emit a stored block if pending_buf will be full: max_start=block_start+max_block_size; if(strstart==0|| strstart>=max_start) { - // strstart == 0 is possible when wraparound on 16-bit machine - lookahead = strstart-max_start; - strstart = max_start; + // strstart == 0 is possible when wraparound on 16-bit machine + lookahead = strstart-max_start; + strstart = max_start; - flush_block_only(false); - if(strm.avail_out==0) return NeedMore; + flush_block_only(false); + if(strm.avail_out==0) return NeedMore; } // Flush if we may have to slide, otherwise block_start may become // negative and the data will be gone: if(strstart-block_start >= w_size-MIN_LOOKAHEAD) { - flush_block_only(false); - if(strm.avail_out==0) return NeedMore; + flush_block_only(false); + if(strm.avail_out==0) return NeedMore; } } @@ -839,9 +839,9 @@ int deflate_stored(int flush){ // Send a stored block void _tr_stored_block(int buf, // input block - int stored_len, // length of input block - boolean eof // true if this is the last block for a file - ){ + int stored_len, // length of input block + boolean eof // true if this is the last block for a file + ){ send_bits((STORED_BLOCK<<1)+(eof?1:0), 3); // send block type copy_block(buf, stored_len, true); // with header } @@ -849,9 +849,9 @@ void _tr_stored_block(int buf, // input block // Determine the best encoding for the current block: dynamic trees, static // trees or store, and output the encoded block to the zip file. void _tr_flush_block(int buf, // input block, or NULL if too old - int stored_len, // length of input block - boolean eof // true if this is the last block for a file - ) { + int stored_len, // length of input block + boolean eof // true if this is the last block for a file + ) { int opt_lenb, static_lenb;// opt_len and static_len in bytes int max_blindex = 0; // index of last bit length code of non zero freq @@ -929,46 +929,46 @@ void fill_window(){ // Deal with !@#$% 64K limit: if(more==0 && strstart==0 && lookahead==0){ - more = w_size; + more = w_size; } else if(more==-1) { - // Very unlikely, but possible on 16 bit machine if strstart == 0 - // and lookahead == 1 (input done one byte at time) - more--; + // Very unlikely, but possible on 16 bit machine if strstart == 0 + // and lookahead == 1 (input done one byte at time) + more--; - // If the window is almost full and there is insufficient lookahead, - // move the upper half to the lower one to make room in the upper half. + // If the window is almost full and there is insufficient lookahead, + // move the upper half to the lower one to make room in the upper half. } else if(strstart >= w_size+ w_size-MIN_LOOKAHEAD) { - System.arraycopy(window, w_size, window, 0, w_size); - match_start-=w_size; - strstart-=w_size; // we now have strstart >= MAX_DIST - block_start-=w_size; - - // Slide the hash table (could be avoided with 32 bit values - // at the expense of memory usage). We slide even when level == 0 - // to keep the hash table consistent if we switch back to level > 0 - // later. (Using level 0 permanently is not an optimal usage of - // zlib, so we don't care about this pathological case.) - - n = hash_size; - p=n; - do { - m = (head[--p]&0xffff); - head[p]=(m>=w_size ? (short)(m-w_size) : 0); - } - while (--n != 0); - - n = w_size; - p = n; - do { - m = (prev[--p]&0xffff); - prev[p] = (m >= w_size ? (short)(m-w_size) : 0); - // If n is not on any hash chain, prev[n] is garbage but - // its value will never be used. - } - while (--n!=0); - more += w_size; + System.arraycopy(window, w_size, window, 0, w_size); + match_start-=w_size; + strstart-=w_size; // we now have strstart >= MAX_DIST + block_start-=w_size; + + // Slide the hash table (could be avoided with 32 bit values + // at the expense of memory usage). We slide even when level == 0 + // to keep the hash table consistent if we switch back to level > 0 + // later. (Using level 0 permanently is not an optimal usage of + // zlib, so we don't care about this pathological case.) + + n = hash_size; + p=n; + do { + m = (head[--p]&0xffff); + head[p]=(m>=w_size ? (short)(m-w_size) : 0); + } + while (--n != 0); + + n = w_size; + p = n; + do { + m = (prev[--p]&0xffff); + prev[p] = (m >= w_size ? (short)(m-w_size) : 0); + // If n is not on any hash chain, prev[n] is garbage but + // its value will never be used. + } + while (--n!=0); + more += w_size; } if (strm.avail_in == 0) return; @@ -989,8 +989,8 @@ else if(strstart >= w_size+ w_size-MIN_LOOKAHEAD) { // Initialize the hash value now that we have some input: if(lookahead >= MIN_MATCH) { - ins_h = window[strstart]&0xff; - ins_h=(((ins_h)<= MIN_MATCH){ - ins_h=(((ins_h)<=MIN_MATCH){ - // check_match(strstart, match_start, match_length); - - bflush=_tr_tally(strstart-match_start, match_length-MIN_MATCH); - - lookahead -= match_length; - - // Insert new strings in the hash table only if the match length - // is not too large. This saves time but degrades compression. - if(match_length <= max_lazy_match && - lookahead >= MIN_MATCH) { - match_length--; // string at strstart already in hash table - do{ - strstart++; - - ins_h=((ins_h<= MIN_MATCH) { + match_length--; // string at strstart already in hash table + do{ + strstart++; + + ins_h=((ins_h<= MIN_MATCH) { - ins_h=(((ins_h)< 4096))) { - - // If prev_match is also MIN_MATCH, match_start is garbage - // but we will ignore the current match anyway. - match_length = MIN_MATCH-1; - } + ((strstart-hash_head)&0xffff) <= w_size-MIN_LOOKAHEAD + ){ + // To simplify the code, we prevent matches with the string + // of window index 0 (in particular we have to avoid a match + // of the string with itself at the start of the input file). + + if(strategy != Z_HUFFMAN_ONLY) { + match_length = longest_match(hash_head); + } + // longest_match() sets match_start + + if (match_length <= 5 && (strategy == Z_FILTERED || + (match_length == MIN_MATCH && + strstart - match_start > 4096))) { + + // If prev_match is also MIN_MATCH, match_start is garbage + // but we will ignore the current match anyway. + match_length = MIN_MATCH-1; + } } // If there was a match at the previous step and the current // match is not better, output the previous match: if(prev_length >= MIN_MATCH && match_length <= prev_length) { - int max_insert = strstart + lookahead - MIN_MATCH; - // Do not insert strings in hash table beyond this. - - // check_match(strstart-1, prev_match, prev_length); - - bflush=_tr_tally(strstart-1-prev_match, prev_length - MIN_MATCH); - - // Insert in hash table all strings up to the end of the match. - // strstart-1 and strstart are already inserted. If there is not - // enough lookahead, the last two strings are not inserted in - // the hash table. - lookahead -= prev_length-1; - prev_length -= 2; - do{ - if(++strstart <= max_insert) { - ins_h=(((ins_h)<best_len) { - match_start = cur_match; - best_len = len; - if (len >= nice_match) break; - scan_end1 = window[scan+best_len-1]; - scan_end = window[scan+best_len]; + match_start = cur_match; + best_len = len; + if (len >= nice_match) break; + scan_end1 = window[scan+best_len-1]; + scan_end = window[scan+best_len]; } } while ((cur_match = (prev[cur_match & wmask]&0xffff)) > limit - && --chain_length != 0); + && --chain_length != 0); if (best_len <= lookahead) return best_len; return lookahead; @@ -1318,18 +1318,18 @@ int longest_match(int cur_match){ int deflateInit(int level, int bits, int memlevel){ return deflateInit(level, Z_DEFLATED, bits, memlevel, - Z_DEFAULT_STRATEGY); + Z_DEFAULT_STRATEGY); } int deflateInit(int level, int bits){ return deflateInit(level, Z_DEFLATED, bits, DEF_MEM_LEVEL, - Z_DEFAULT_STRATEGY); + Z_DEFAULT_STRATEGY); } int deflateInit(int level){ return deflateInit(level, MAX_WBITS); } private int deflateInit(int level, int method, int windowBits, - int memLevel, int strategy){ + int memLevel, int strategy){ int wrap = 1; // byte[] my_version=ZLIB_VERSION; @@ -1354,8 +1354,8 @@ else if(windowBits > 15){ } if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || - method != Z_DEFLATED || - windowBits < 9 || windowBits > 15 || level < 0 || level > 9 || + method != Z_DEFLATED || + windowBits < 9 || windowBits > 15 || level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) { return Z_STREAM_ERROR; } @@ -1546,13 +1546,13 @@ int deflate(int flush){ if(pending != 0) { strm.flush_pending(); if(strm.avail_out == 0) { - // Since avail_out is 0, deflate will be called again with - // more output space, but possibly with both pending and - // avail_in equal to zero. There won't be anything to do, - // but this is not an error situation so make sure we - // return OK instead of BUF_ERROR at next call of deflate: - last_flush = -1; - return Z_OK; + // Since avail_out is 0, deflate will be called again with + // more output space, but possibly with both pending and + // avail_in equal to zero. There won't be anything to do, + // but this is not an error situation so make sure we + // return OK instead of BUF_ERROR at next call of deflate: + last_flush = -1; + return Z_OK; } // Make sure there is something to do and avoid duplicate consecutive @@ -1560,7 +1560,7 @@ int deflate(int flush){ // returning Z_STREAM_END instead of Z_BUFF_ERROR. } else if(strm.avail_in==0 && flush <= old_flush && - flush != Z_FINISH) { + flush != Z_FINISH) { strm.msg=z_errmsg[Z_NEED_DICT-(Z_BUF_ERROR)]; return Z_BUF_ERROR; } @@ -1577,52 +1577,52 @@ else if(strm.avail_in==0 && flush <= old_flush && int bstate=-1; switch(config_table[level].func){ case STORED: - bstate = deflate_stored(flush); - break; + bstate = deflate_stored(flush); + break; case FAST: - bstate = deflate_fast(flush); - break; + bstate = deflate_fast(flush); + break; case SLOW: - bstate = deflate_slow(flush); - break; + bstate = deflate_slow(flush); + break; default: } if (bstate==FinishStarted || bstate==FinishDone) { - status = FINISH_STATE; + status = FINISH_STATE; } if (bstate==NeedMore || bstate==FinishStarted) { - if(strm.avail_out == 0) { - last_flush = -1; // avoid BUF_ERROR next call, see above - } - return Z_OK; - // If flush != Z_NO_FLUSH && avail_out == 0, the next call - // of deflate should use the same flush parameter to make sure - // that the flush is complete. So we don't have to output an - // empty block here, this will be done at next call. This also - // ensures that for a very small output buffer, we emit at most - // one empty block. + if(strm.avail_out == 0) { + last_flush = -1; // avoid BUF_ERROR next call, see above + } + return Z_OK; + // If flush != Z_NO_FLUSH && avail_out == 0, the next call + // of deflate should use the same flush parameter to make sure + // that the flush is complete. So we don't have to output an + // empty block here, this will be done at next call. This also + // ensures that for a very small output buffer, we emit at most + // one empty block. } if (bstate==BlockDone) { - if(flush == Z_PARTIAL_FLUSH) { - _tr_align(); - } - else { // FULL_FLUSH or SYNC_FLUSH - _tr_stored_block(0, 0, false); - // For a full flush, this empty block will be recognized - // as a special marker by inflate_sync(). - if(flush == Z_FULL_FLUSH) { - //state.head[s.hash_size-1]=0; - for(int i=0; i>> 1){ + while(k<(3)){ + if(n!=0){ + r=Z_OK; + } + else{ + bitb=b; bitk=k; + z.avail_in=n; + z.total_in+=p-z.next_in_index;z.next_in_index=p; + write=q; + return inflate_flush(r); + }; + n--; + b|=(z.next_in[p++]&0xff)<>> 1){ case 0: // stored {b>>>=(3);k-=(3);} t = k & 7; // go to byte boundary @@ -199,334 +199,334 @@ int proc(int r){ z.msg = "invalid block type"; r = Z_DATA_ERROR; - bitb=b; bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - write=q; - return inflate_flush(r); - } - break; + bitb=b; bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + write=q; + return inflate_flush(r); + } + break; case LENS: - while(k<(32)){ - if(n!=0){ - r=Z_OK; - } - else{ - bitb=b; bitk=k; - z.avail_in=n; - z.total_in+=p-z.next_in_index;z.next_in_index=p; - write=q; - return inflate_flush(r); - }; - n--; - b|=(z.next_in[p++]&0xff)<>> 16) & 0xffff) != (b & 0xffff)){ - mode = BAD; - z.msg = "invalid stored block lengths"; - r = Z_DATA_ERROR; - - bitb=b; bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - write=q; - return inflate_flush(r); - } - left = (b & 0xffff); - b = k = 0; // dump bits - mode = left!=0 ? STORED : (last!=0 ? DRY : TYPE); - break; + while(k<(32)){ + if(n!=0){ + r=Z_OK; + } + else{ + bitb=b; bitk=k; + z.avail_in=n; + z.total_in+=p-z.next_in_index;z.next_in_index=p; + write=q; + return inflate_flush(r); + }; + n--; + b|=(z.next_in[p++]&0xff)<>> 16) & 0xffff) != (b & 0xffff)){ + mode = BAD; + z.msg = "invalid stored block lengths"; + r = Z_DATA_ERROR; + + bitb=b; bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + write=q; + return inflate_flush(r); + } + left = (b & 0xffff); + b = k = 0; // dump bits + mode = left!=0 ? STORED : (last!=0 ? DRY : TYPE); + break; case STORED: - if (n == 0){ - bitb=b; bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - write=q; - return inflate_flush(r); - } - - if(m==0){ - if(q==end&&read!=0){ - q=0; m=(qn) t = n; - if(t>m) t = m; - System.arraycopy(z.next_in, p, window, q, t); - p += t; n -= t; - q += t; m -= t; - if ((left -= t) != 0) - break; - mode = last!=0 ? DRY : TYPE; - break; + if (n == 0){ + bitb=b; bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + write=q; + return inflate_flush(r); + } + + if(m==0){ + if(q==end&&read!=0){ + q=0; m=(qn) t = n; + if(t>m) t = m; + System.arraycopy(z.next_in, p, window, q, t); + p += t; n -= t; + q += t; m -= t; + if ((left -= t) != 0) + break; + mode = last!=0 ? DRY : TYPE; + break; case TABLE: - while(k<(14)){ - if(n!=0){ - r=Z_OK; - } - else{ - bitb=b; bitk=k; - z.avail_in=n; - z.total_in+=p-z.next_in_index;z.next_in_index=p; - write=q; - return inflate_flush(r); - }; - n--; - b|=(z.next_in[p++]&0xff)< 29 || ((t >> 5) & 0x1f) > 29) - { - mode = BAD; - z.msg = "too many length or distance symbols"; - r = Z_DATA_ERROR; - - bitb=b; bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - write=q; - return inflate_flush(r); - } - t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f); - if(blens==null || blens.length>>=(14);k-=(14);} - - index = 0; - mode = BTREE; + while(k<(14)){ + if(n!=0){ + r=Z_OK; + } + else{ + bitb=b; bitk=k; + z.avail_in=n; + z.total_in+=p-z.next_in_index;z.next_in_index=p; + write=q; + return inflate_flush(r); + }; + n--; + b|=(z.next_in[p++]&0xff)< 29 || ((t >> 5) & 0x1f) > 29) + { + mode = BAD; + z.msg = "too many length or distance symbols"; + r = Z_DATA_ERROR; + + bitb=b; bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + write=q; + return inflate_flush(r); + } + t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f); + if(blens==null || blens.length>>=(14);k-=(14);} + + index = 0; + mode = BTREE; case BTREE: - while (index < 4 + (table >>> 10)){ - while(k<(3)){ - if(n!=0){ - r=Z_OK; - } - else{ - bitb=b; bitk=k; - z.avail_in=n; - z.total_in+=p-z.next_in_index;z.next_in_index=p; - write=q; - return inflate_flush(r); - }; - n--; - b|=(z.next_in[p++]&0xff)<>>=(3);k-=(3);} - } - - while(index < 19){ - blens[border[index++]] = 0; - } - - bb[0] = 7; - t = inftree.inflate_trees_bits(blens, bb, tb, hufts, z); - if (t != Z_OK){ - r = t; - if (r == Z_DATA_ERROR){ - blens=null; - mode = BAD; - } - - bitb=b; bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - write=q; - return inflate_flush(r); - } - - index = 0; - mode = DTREE; + while (index < 4 + (table >>> 10)){ + while(k<(3)){ + if(n!=0){ + r=Z_OK; + } + else{ + bitb=b; bitk=k; + z.avail_in=n; + z.total_in+=p-z.next_in_index;z.next_in_index=p; + write=q; + return inflate_flush(r); + }; + n--; + b|=(z.next_in[p++]&0xff)<>>=(3);k-=(3);} + } + + while(index < 19){ + blens[border[index++]] = 0; + } + + bb[0] = 7; + t = inftree.inflate_trees_bits(blens, bb, tb, hufts, z); + if (t != Z_OK){ + r = t; + if (r == Z_DATA_ERROR){ + blens=null; + mode = BAD; + } + + bitb=b; bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + write=q; + return inflate_flush(r); + } + + index = 0; + mode = DTREE; case DTREE: - while (true){ - t = table; - if(!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))){ - break; - } - - int[] h; - int i, j, c; - - t = bb[0]; - - while(k<(t)){ - if(n!=0){ - r=Z_OK; - } - else{ - bitb=b; bitk=k; - z.avail_in=n; - z.total_in+=p-z.next_in_index;z.next_in_index=p; - write=q; - return inflate_flush(r); - }; - n--; - b|=(z.next_in[p++]&0xff)<> 5) & 0x1f))){ + break; + } + + int[] h; + int i, j, c; + + t = bb[0]; + + while(k<(t)){ + if(n!=0){ + r=Z_OK; + } + else{ + bitb=b; bitk=k; + z.avail_in=n; + z.total_in+=p-z.next_in_index;z.next_in_index=p; + write=q; + return inflate_flush(r); + }; + n--; + b|=(z.next_in[p++]&0xff)<>>=(t);k-=(t); - blens[index++] = c; - } - else { // c == 16..18 - i = c == 18 ? 7 : c - 14; - j = c == 18 ? 11 : 3; - - while(k<(t+i)){ - if(n!=0){ - r=Z_OK; - } - else{ - bitb=b; bitk=k; - z.avail_in=n; - z.total_in+=p-z.next_in_index;z.next_in_index=p; - write=q; - return inflate_flush(r); - }; - n--; - b|=(z.next_in[p++]&0xff)<>>=(t);k-=(t); - - j += (b & inflate_mask[i]); - - b>>>=(i);k-=(i); - - i = index; - t = table; - if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || - (c == 16 && i < 1)){ - blens=null; - mode = BAD; - z.msg = "invalid bit length repeat"; - r = Z_DATA_ERROR; - - bitb=b; bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - write=q; - return inflate_flush(r); - } - - c = c == 16 ? blens[i-1] : 0; - do{ - blens[i++] = c; - } - while (--j!=0); - index = i; - } - } - - tb[0]=-1; - { - bl[0] = 9; // must be <= 9 for lookahead assumptions - bd[0] = 6; // must be <= 9 for lookahead assumptions - t = table; - t = inftree.inflate_trees_dynamic(257 + (t & 0x1f), - 1 + ((t >> 5) & 0x1f), - blens, bl, bd, tli, tdi, hufts, z); - - if (t != Z_OK){ - if (t == Z_DATA_ERROR){ - blens=null; - mode = BAD; - } - r = t; - - bitb=b; bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - write=q; - return inflate_flush(r); - } - codes.init(bl[0], bd[0], hufts, tli[0], hufts, tdi[0]); - } - mode = CODES; + } + + t=hufts[(tb[0]+(b&inflate_mask[t]))*3+1]; + c=hufts[(tb[0]+(b&inflate_mask[t]))*3+2]; + + if (c < 16){ + b>>>=(t);k-=(t); + blens[index++] = c; + } + else { // c == 16..18 + i = c == 18 ? 7 : c - 14; + j = c == 18 ? 11 : 3; + + while(k<(t+i)){ + if(n!=0){ + r=Z_OK; + } + else{ + bitb=b; bitk=k; + z.avail_in=n; + z.total_in+=p-z.next_in_index;z.next_in_index=p; + write=q; + return inflate_flush(r); + }; + n--; + b|=(z.next_in[p++]&0xff)<>>=(t);k-=(t); + + j += (b & inflate_mask[i]); + + b>>>=(i);k-=(i); + + i = index; + t = table; + if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || + (c == 16 && i < 1)){ + blens=null; + mode = BAD; + z.msg = "invalid bit length repeat"; + r = Z_DATA_ERROR; + + bitb=b; bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + write=q; + return inflate_flush(r); + } + + c = c == 16 ? blens[i-1] : 0; + do{ + blens[i++] = c; + } + while (--j!=0); + index = i; + } + } + + tb[0]=-1; + { + bl[0] = 9; // must be <= 9 for lookahead assumptions + bd[0] = 6; // must be <= 9 for lookahead assumptions + t = table; + t = inftree.inflate_trees_dynamic(257 + (t & 0x1f), + 1 + ((t >> 5) & 0x1f), + blens, bl, bd, tli, tdi, hufts, z); + + if (t != Z_OK){ + if (t == Z_DATA_ERROR){ + blens=null; + mode = BAD; + } + r = t; + + bitb=b; bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + write=q; + return inflate_flush(r); + } + codes.init(bl[0], bd[0], hufts, tli[0], hufts, tdi[0]); + } + mode = CODES; case CODES: - bitb=b; bitk=k; - z.avail_in=n; z.total_in+=p-z.next_in_index;z.next_in_index=p; - write=q; - - if ((r = codes.proc(r)) != Z_STREAM_END){ - return inflate_flush(r); - } - r = Z_OK; - codes.free(z); - - p=z.next_in_index; n=z.avail_in;b=bitb;k=bitk; - q=write;m=(q0){ - z.adler.update(window, q, n); + z.adler.update(window, q, n); } // copy diff --git a/src/main/java/com/jcraft/jsch/jzlib/InfCodes.java b/src/main/java/com/jcraft/jsch/jzlib/InfCodes.java index db61b9cf..4f411759 100644 --- a/src/main/java/com/jcraft/jsch/jzlib/InfCodes.java +++ b/src/main/java/com/jcraft/jsch/jzlib/InfCodes.java @@ -97,8 +97,8 @@ final class InfCodes{ } void init(int bl, int bd, - int[] tl, int tl_index, - int[] td, int td_index){ + int[] tl, int tl_index, + int[] td, int td_index){ mode=START; lbits=(byte)bl; dbits=(byte)bd; @@ -130,267 +130,267 @@ int proc(int r){ // process input and output based on current state while (true){ switch (mode){ - // waiting for "i:"=input, "o:"=output, "x:"=nothing + // waiting for "i:"=input, "o:"=output, "x:"=nothing case START: // x: set up for LEN - if (m >= 258 && n >= 10){ - - s.bitb=b;s.bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - s.write=q; - r = inflate_fast(lbits, dbits, - ltree, ltree_index, - dtree, dtree_index, - s, z); - - p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk; - q=s.write;m=q= 258 && n >= 10){ + + s.bitb=b;s.bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + s.write=q; + r = inflate_fast(lbits, dbits, + ltree, ltree_index, + dtree, dtree_index, + s, z); + + p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk; + q=s.write;m=q>>=(tree[tindex+1]); - k-=(tree[tindex+1]); - - e=tree[tindex]; - - if(e == 0){ // literal - lit = tree[tindex+2]; - mode = LIT; - break; - } - if((e & 16)!=0 ){ // length - get = e & 15; - len = tree[tindex+2]; - mode = LENEXT; - break; - } - if ((e & 64) == 0){ // next table - need = e; - tree_index = tindex/3+tree[tindex+2]; - break; - } - if ((e & 32)!=0){ // end of block - mode = WASH; - break; - } - mode = BADCODE; // invalid code - z.msg = "invalid literal/length code"; - r = Z_DATA_ERROR; - - s.bitb=b;s.bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - s.write=q; - return s.inflate_flush(r); + j = need; + + while(k<(j)){ + if(n!=0)r=Z_OK; + else{ + + s.bitb=b;s.bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + s.write=q; + return s.inflate_flush(r); + } + n--; + b|=(z.next_in[p++]&0xff)<>>=(tree[tindex+1]); + k-=(tree[tindex+1]); + + e=tree[tindex]; + + if(e == 0){ // literal + lit = tree[tindex+2]; + mode = LIT; + break; + } + if((e & 16)!=0 ){ // length + get = e & 15; + len = tree[tindex+2]; + mode = LENEXT; + break; + } + if ((e & 64) == 0){ // next table + need = e; + tree_index = tindex/3+tree[tindex+2]; + break; + } + if ((e & 32)!=0){ // end of block + mode = WASH; + break; + } + mode = BADCODE; // invalid code + z.msg = "invalid literal/length code"; + r = Z_DATA_ERROR; + + s.bitb=b;s.bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + s.write=q; + return s.inflate_flush(r); case LENEXT: // i: getting length extra (have base) - j = get; - - while(k<(j)){ - if(n!=0)r=Z_OK; - else{ - - s.bitb=b;s.bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - s.write=q; - return s.inflate_flush(r); - } - n--; b|=(z.next_in[p++]&0xff)<>=j; - k-=j; - - need = dbits; - tree = dtree; - tree_index=dtree_index; - mode = DIST; + j = get; + + while(k<(j)){ + if(n!=0)r=Z_OK; + else{ + + s.bitb=b;s.bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + s.write=q; + return s.inflate_flush(r); + } + n--; b|=(z.next_in[p++]&0xff)<>=j; + k-=j; + + need = dbits; + tree = dtree; + tree_index=dtree_index; + mode = DIST; case DIST: // i: get distance next - j = need; - - while(k<(j)){ - if(n!=0)r=Z_OK; - else{ - - s.bitb=b;s.bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - s.write=q; - return s.inflate_flush(r); - } - n--; b|=(z.next_in[p++]&0xff)<>=tree[tindex+1]; - k-=tree[tindex+1]; - - e = (tree[tindex]); - if((e & 16)!=0){ // distance - get = e & 15; - dist = tree[tindex+2]; - mode = DISTEXT; - break; - } - if ((e & 64) == 0){ // next table - need = e; - tree_index = tindex/3 + tree[tindex+2]; - break; - } - mode = BADCODE; // invalid code - z.msg = "invalid distance code"; - r = Z_DATA_ERROR; - - s.bitb=b;s.bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - s.write=q; - return s.inflate_flush(r); + j = need; + + while(k<(j)){ + if(n!=0)r=Z_OK; + else{ + + s.bitb=b;s.bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + s.write=q; + return s.inflate_flush(r); + } + n--; b|=(z.next_in[p++]&0xff)<>=tree[tindex+1]; + k-=tree[tindex+1]; + + e = (tree[tindex]); + if((e & 16)!=0){ // distance + get = e & 15; + dist = tree[tindex+2]; + mode = DISTEXT; + break; + } + if ((e & 64) == 0){ // next table + need = e; + tree_index = tindex/3 + tree[tindex+2]; + break; + } + mode = BADCODE; // invalid code + z.msg = "invalid distance code"; + r = Z_DATA_ERROR; + + s.bitb=b;s.bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + s.write=q; + return s.inflate_flush(r); case DISTEXT: // i: getting distance extra - j = get; + j = get; - while(k<(j)){ - if(n!=0)r=Z_OK; - else{ + while(k<(j)){ + if(n!=0)r=Z_OK; + else{ - s.bitb=b;s.bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - s.write=q; - return s.inflate_flush(r); - } - n--; b|=(z.next_in[p++]&0xff)<>=j; - k-=j; + b>>=j; + k-=j; - mode = COPY; + mode = COPY; case COPY: // o: copying bytes in window, waiting for space f = q - dist; while(f < 0){ // modulo window size-"while" instead f += s.end; // of "if" handles invalid distances - } - while (len!=0){ + } + while (len!=0){ - if(m==0){ - if(q==s.end&&s.read!=0){q=0;m=q 7){ // return unused byte, if any - k -= 8; - n++; - p--; // can always return one - } - - s.write=q; r=s.inflate_flush(r); - q=s.write;m=q 7){ // return unused byte, if any + k -= 8; + n++; + p--; // can always return one + } + + s.write=q; r=s.inflate_flush(r); + q=s.write;m=q= 258 && n >= 10 // get literal/length code while(k<(20)){ // max bits for literal/length code - n--; - b|=(z.next_in[p++]&0xff)<>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); + b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); - s.window[q++] = (byte)tp[tp_index_t_3+2]; - m--; - continue; + s.window[q++] = (byte)tp[tp_index_t_3+2]; + m--; + continue; } do { - b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); + b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); - if((e&16)!=0){ - e &= 15; - c = tp[tp_index_t_3+2] + (b & inflate_mask[e]); + if((e&16)!=0){ + e &= 15; + c = tp[tp_index_t_3+2] + (b & inflate_mask[e]); - b>>=e; k-=e; + b>>=e; k-=e; - // decode distance base of block to copy - while(k<(15)){ // max bits for distance code - n--; - b|=(z.next_in[p++]&0xff)<>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); - - if((e&16)!=0){ - // get extra bits to add to distance base - e &= 15; - while(k<(e)){ // get extra bits (up to 13) - n--; - b|=(z.next_in[p++]&0xff)<>=(e); k-=(e); - - // do the copy - m -= c; - if (q >= d){ // offset before dest - // just copy - r=q-d; - if(q-r>0 && 2>(q-r)){ - s.window[q++]=s.window[r++]; // minimum count is three, - s.window[q++]=s.window[r++]; // so unroll loop a little - c-=2; - } - else{ - System.arraycopy(s.window, r, s.window, q, 2); - q+=2; r+=2; c-=2; - } - } - else{ // else offset after destination + e = tp[tp_index_t_3]; + + do { + + b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); + + if((e&16)!=0){ + // get extra bits to add to distance base + e &= 15; + while(k<(e)){ // get extra bits (up to 13) + n--; + b|=(z.next_in[p++]&0xff)<>=(e); k-=(e); + + // do the copy + m -= c; + if (q >= d){ // offset before dest + // just copy + r=q-d; + if(q-r>0 && 2>(q-r)){ + s.window[q++]=s.window[r++]; // minimum count is three, + s.window[q++]=s.window[r++]; // so unroll loop a little + c-=2; + } + else{ + System.arraycopy(s.window, r, s.window, q, 2); + q+=2; r+=2; c-=2; + } + } + else{ // else offset after destination r=q-d; do{ r+=s.end; // force pointer in window }while(r<0); // covers invalid distances - e=s.end-r; - if(c>e){ // if source crosses, - c-=e; // wrapped copy - if(q-r>0 && e>(q-r)){ - do{s.window[q++] = s.window[r++];} - while(--e!=0); - } - else{ - System.arraycopy(s.window, r, s.window, q, e); - q+=e; r+=e; e=0; - } - r = 0; // copy rest from start of window - } - - } - - // copy all or what's left - if(q-r>0 && c>(q-r)){ - do{s.window[q++] = s.window[r++];} - while(--c!=0); - } - else{ - System.arraycopy(s.window, r, s.window, q, c); - q+=c; r+=c; c=0; - } - break; - } - else if((e&64)==0){ - t+=tp[tp_index_t_3+2]; - t+=(b&inflate_mask[e]); - tp_index_t_3=(tp_index+t)*3; - e=tp[tp_index_t_3]; - } - else{ - z.msg = "invalid distance code"; - - c=z.avail_in-n;c=(k>>3)>3:c;n+=c;p-=c;k-=c<<3; - - s.bitb=b;s.bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - s.write=q; - - return Z_DATA_ERROR; - } - } - while(true); - break; - } - - if((e&64)==0){ - t+=tp[tp_index_t_3+2]; - t+=(b&inflate_mask[e]); - tp_index_t_3=(tp_index+t)*3; - if((e=tp[tp_index_t_3])==0){ - - b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); - - s.window[q++]=(byte)tp[tp_index_t_3+2]; - m--; - break; - } - } - else if((e&32)!=0){ - - c=z.avail_in-n;c=(k>>3)>3:c;n+=c;p-=c;k-=c<<3; + e=s.end-r; + if(c>e){ // if source crosses, + c-=e; // wrapped copy + if(q-r>0 && e>(q-r)){ + do{s.window[q++] = s.window[r++];} + while(--e!=0); + } + else{ + System.arraycopy(s.window, r, s.window, q, e); + q+=e; r+=e; e=0; + } + r = 0; // copy rest from start of window + } + + } + + // copy all or what's left + if(q-r>0 && c>(q-r)){ + do{s.window[q++] = s.window[r++];} + while(--c!=0); + } + else{ + System.arraycopy(s.window, r, s.window, q, c); + q+=c; r+=c; c=0; + } + break; + } + else if((e&64)==0){ + t+=tp[tp_index_t_3+2]; + t+=(b&inflate_mask[e]); + tp_index_t_3=(tp_index+t)*3; + e=tp[tp_index_t_3]; + } + else{ + z.msg = "invalid distance code"; + + c=z.avail_in-n;c=(k>>3)>3:c;n+=c;p-=c;k-=c<<3; + + s.bitb=b;s.bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + s.write=q; + + return Z_DATA_ERROR; + } + } + while(true); + break; + } + + if((e&64)==0){ + t+=tp[tp_index_t_3+2]; + t+=(b&inflate_mask[e]); + tp_index_t_3=(tp_index+t)*3; + if((e=tp[tp_index_t_3])==0){ + + b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); + + s.window[q++]=(byte)tp[tp_index_t_3+2]; + m--; + break; + } + } + else if((e&32)!=0){ + + c=z.avail_in-n;c=(k>>3)>3:c;n+=c;p-=c;k-=c<<3; - s.bitb=b;s.bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - s.write=q; + s.bitb=b;s.bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + s.write=q; - return Z_STREAM_END; - } - else{ - z.msg="invalid literal/length code"; + return Z_STREAM_END; + } + else{ + z.msg="invalid literal/length code"; - c=z.avail_in-n;c=(k>>3)>3:c;n+=c;p-=c;k-=c<<3; + c=z.avail_in-n;c=(k>>3)>3:c;n+=c;p-=c;k-=c<<3; - s.bitb=b;s.bitk=k; - z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; - s.write=q; + s.bitb=b;s.bitk=k; + z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; + s.write=q; - return Z_DATA_ERROR; - } + return Z_DATA_ERROR; + } } while(true); } diff --git a/src/main/java/com/jcraft/jsch/jzlib/InfTree.java b/src/main/java/com/jcraft/jsch/jzlib/InfTree.java index 07c2e576..409672cf 100644 --- a/src/main/java/com/jcraft/jsch/jzlib/InfTree.java +++ b/src/main/java/com/jcraft/jsch/jzlib/InfTree.java @@ -334,12 +334,12 @@ private int huft_build(int[] b, // code lengths in bits (all assumed <= BMAX) for (; k <= g; k++){ a = c[k]; while (a--!=0){ - // here i is the Huffman code of length k bits for value *p - // make tables up to required level + // here i is the Huffman code of length k bits for value *p + // make tables up to required level while (k > w + l){ h++; w += l; // previous table always l bits - // compute minimum size table less than or equal to l bits + // compute minimum size table less than or equal to l bits z = g - w; z = (z > l) ? l : z; // table size upper limit if((f=1<<(j=k-w))>a+1){ // try a k-w bit table @@ -352,19 +352,19 @@ private int huft_build(int[] b, // code lengths in bits (all assumed <= BMAX) break; // enough codes to use up j bits f -= c[xp]; // else deduct codes from patterns } - } + } } z = 1 << j; // table entries for j-bit table - // allocate new table + // allocate new table if (hn[0] + z > MANY){ // (note: doesn't matter for fixed) return Z_DATA_ERROR; // overflow of MANY } u[h] = q = /*hp+*/ hn[0]; // DEBUG hn[0] += z; - // connect to last table, if there is one - if(h!=0){ + // connect to last table, if there is one + if(h!=0){ x[h]=i; // save pattern for backing up r[0]=(byte)j; // bits in this table r[1]=(byte)l; // bits to dump before this table @@ -374,14 +374,14 @@ private int huft_build(int[] b, // code lengths in bits (all assumed <= BMAX) } else{ t[0] = q; // first table is returned result - } + } } - // set up table entry in r + // set up table entry in r r[1] = (byte)(k - w); if (p >= n){ r[0] = 128 + 64; // out of values--invalid code - } + } else if (v[p] < s){ r[0] = (byte)(v[p] < 256 ? 0 : 32 + 64); // 256 is end-of-block r[2] = v[p++]; // simple code is just the value @@ -395,15 +395,15 @@ else if (v[p] < s){ f=1<<(k-w); for (j=i>>>w;j>>= 1){ i ^= j; - } + } i ^= j; - // backup over finished tables + // backup over finished tables mask = (1 << w) - 1; // needed on HP, cc -O bug while ((i & mask) != x[h]){ h--; // don't need to update q @@ -491,7 +491,7 @@ static int inflate_trees_fixed(int[] bl, //literal desired/actual bit depth int[][] tl,//literal/length tree result int[][] td,//distance tree result ZStream z //for memory allocation - ){ + ){ bl[0]=fixed_bl; bd[0]=fixed_bd; tl[0]=fixed_tl; diff --git a/src/main/java/com/jcraft/jsch/jzlib/Inflate.java b/src/main/java/com/jcraft/jsch/jzlib/Inflate.java index 90e31395..51ba9ca0 100644 --- a/src/main/java/com/jcraft/jsch/jzlib/Inflate.java +++ b/src/main/java/com/jcraft/jsch/jzlib/Inflate.java @@ -208,7 +208,7 @@ int inflate(int f){ switch (this.mode){ case HEAD: if(wrap==0){ - this.mode = BLOCKS; + this.mode = BLOCKS; break; } @@ -220,7 +220,7 @@ int inflate(int f){ if(wrap == 4){ wrap = 2; } - z.adler=new CRC32(); + z.adler=new CRC32(); checksum(2, this.need); if(gheader==null) @@ -265,9 +265,9 @@ int inflate(int f){ this.mode = BAD; z.msg="unknown compression method"; // since zlib 1.2, it is allowted to inflateSync for this case. - /* + /* this.marker = 5; // can't try inflateSync - */ + */ break; } @@ -279,9 +279,9 @@ int inflate(int f){ this.mode = BAD; z.msg="invalid window size"; // since zlib 1.2, it is allowted to inflateSync for this case. - /* + /* this.marker = 5; // can't try inflateSync - */ + */ break; } @@ -390,7 +390,7 @@ int inflate(int f){ this.mode = BAD; this.marker = 5; // can't try inflateSync break; - */ + */ } else if(flags!=0 && gheader!=null){ gheader.crc = this.need; @@ -500,22 +500,22 @@ else if(gheader!=null){ tmp_string=null; if(foo.length == gheader.extra.length){ System.arraycopy(foo, 0, gheader.extra, 0, foo.length); - } + } else{ z.msg = "bad extra field length"; this.mode = BAD; break; - } + } } } catch(Return e){ return e.r; } } else if(gheader!=null){ gheader.extra=null; - } - this.mode = NAME; + } + this.mode = NAME; case NAME: - if ((flags & 0x0800)!=0) { + if ((flags & 0x0800)!=0) { try { r=readString(r, f); if(gheader!=null){ @@ -527,7 +527,7 @@ else if(gheader!=null){ } else if(gheader!=null){ gheader.name=null; - } + } this.mode = COMMENT; case COMMENT: if ((flags & 0x1000)!=0) { @@ -542,10 +542,10 @@ else if(gheader!=null){ } else if(gheader!=null){ gheader.comment=null; - } + } this.mode = HCRC; case HCRC: - if ((flags & 0x0200)!=0) { + if ((flags & 0x0200)!=0) { try { r=readBytes(2, r, f); } catch(Return e){ return e.r; } if(gheader!=null){ @@ -669,7 +669,7 @@ private int readBytes(int n, int r, int f) throws Return{ if(z.avail_in==0){ throw new Return(r); }; r=f; z.avail_in--; z.total_in++; this.need = this.need | - ((z.next_in[z.next_in_index++]&0xff)<<((n-need_bytes)*8)); + ((z.next_in[z.next_in_index++]&0xff)<<((n-need_bytes)*8)); need_bytes--; } if(n==2){ @@ -748,9 +748,9 @@ boolean inParsingHeader(){ case NAME: case COMMENT: case HCRC: - return true; + return true; default: - return false; + return false; } } } diff --git a/src/main/java/com/jcraft/jsch/jzlib/StaticTree.java b/src/main/java/com/jcraft/jsch/jzlib/StaticTree.java index 56047afd..219c72b4 100644 --- a/src/main/java/com/jcraft/jsch/jzlib/StaticTree.java +++ b/src/main/java/com/jcraft/jsch/jzlib/StaticTree.java @@ -118,15 +118,15 @@ final class StaticTree{ static StaticTree static_l_desc = new StaticTree(static_ltree, Tree.extra_lbits, - LITERALS+1, L_CODES, MAX_BITS); + LITERALS+1, L_CODES, MAX_BITS); static StaticTree static_d_desc = new StaticTree(static_dtree, Tree.extra_dbits, - 0, D_CODES, MAX_BITS); + 0, D_CODES, MAX_BITS); static StaticTree static_bl_desc = new StaticTree(null, Tree.extra_blbits, - 0, BL_CODES, MAX_BL_BITS); + 0, BL_CODES, MAX_BL_BITS); short[] static_tree; // static tree or null int[] extra_bits; // extra bits for each code or null diff --git a/src/main/java/com/jcraft/jsch/jzlib/Tree.java b/src/main/java/com/jcraft/jsch/jzlib/Tree.java index fcdb39e0..1bb87be2 100644 --- a/src/main/java/com/jcraft/jsch/jzlib/Tree.java +++ b/src/main/java/com/jcraft/jsch/jzlib/Tree.java @@ -215,13 +215,13 @@ void gen_bitlen(Deflate s){ for (bits = max_length; bits != 0; bits--) { n = s.bl_count[bits]; while (n != 0) { - m = s.heap[--h]; - if (m > max_code) continue; - if (tree[m*2+1] != bits) { - s.opt_len += ((long)bits - (long)tree[m*2+1])*(long)tree[m*2]; - tree[m*2+1] = (short)bits; - } - n--; + m = s.heap[--h]; + if (m > max_code) continue; + if (tree[m*2+1] != bits) { + s.opt_len += ((long)bits - (long)tree[m*2+1])*(long)tree[m*2]; + tree[m*2+1] = (short)bits; + } + n--; } } } @@ -248,11 +248,11 @@ void build_tree(Deflate s){ for(n=0; n keyArgs() { diff --git a/src/test/java/com/jcraft/jsch/OpenSSH74ServerSigAlgsIT.java b/src/test/java/com/jcraft/jsch/OpenSSH74ServerSigAlgsIT.java index 1d345428..db1e6d96 100644 --- a/src/test/java/com/jcraft/jsch/OpenSSH74ServerSigAlgsIT.java +++ b/src/test/java/com/jcraft/jsch/OpenSSH74ServerSigAlgsIT.java @@ -71,7 +71,7 @@ public class OpenSSH74ServerSigAlgsIT { @BeforeAll public static void beforeAll() { - JSch.setLogger(Slf4jLogger.getInstance()); + JSch.setLogger(new Slf4jLogger()); } @BeforeEach diff --git a/src/test/java/com/jcraft/jsch/SSHAgentIT.java b/src/test/java/com/jcraft/jsch/SSHAgentIT.java index 2a13f4e4..31daa774 100644 --- a/src/test/java/com/jcraft/jsch/SSHAgentIT.java +++ b/src/test/java/com/jcraft/jsch/SSHAgentIT.java @@ -93,7 +93,7 @@ public class SSHAgentIT { @BeforeAll public static void beforeAll() throws IOException { - JSch.setLogger(Slf4jLogger.getInstance()); + JSch.setLogger(new Slf4jLogger()); LibC libc = LibC.INSTANCE; testuid = Integer.toString(libc.getuid()); testgid = Integer.toString(libc.getgid()); diff --git a/src/test/java/com/jcraft/jsch/ServerSigAlgsIT.java b/src/test/java/com/jcraft/jsch/ServerSigAlgsIT.java index 5b1fb298..095d9789 100644 --- a/src/test/java/com/jcraft/jsch/ServerSigAlgsIT.java +++ b/src/test/java/com/jcraft/jsch/ServerSigAlgsIT.java @@ -71,7 +71,7 @@ public class ServerSigAlgsIT { @BeforeAll public static void beforeAll() { - JSch.setLogger(Slf4jLogger.getInstance()); + JSch.setLogger(new Slf4jLogger()); } @BeforeEach