Skip to content

Commit

Permalink
Replace old Hashtable with modern data structures.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitrii Beliakov committed Aug 19, 2024
1 parent d064f44 commit fbc39bd
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 48 deletions.
24 changes: 10 additions & 14 deletions src/main/java/com/jcraft/jsch/ChannelSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@

package com.jcraft.jsch;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

class ChannelSession extends Channel {
private static byte[] _session = Util.str2byte("session");

protected boolean agent_forwarding = false;
protected boolean xforwading = false;
protected Hashtable<byte[], byte[]> env = null;
protected Map<byte[], byte[]> env = null;

protected boolean pty = false;

Expand Down Expand Up @@ -78,7 +79,7 @@ public void setXForwarding(boolean enable) {
@Deprecated
public void setEnv(Hashtable<byte[], byte[]> env) {
synchronized (this) {
this.env = env;
this.env = new ConcurrentHashMap<>(env);
}
}

Expand All @@ -103,16 +104,13 @@ public void setEnv(String name, String value) {
*/
public void setEnv(byte[] name, byte[] value) {
synchronized (this) {
getEnv().put(name, value);
if (env == null) {
env = new ConcurrentHashMap<>();
}
env.put(name, value);
}
}

private Hashtable<byte[], byte[]> getEnv() {
if (env == null)
env = new Hashtable<>();
return env;
}

/**
* Allocate a Pseudo-Terminal. Refer to RFC4254 6.2. Requesting a Pseudo-Terminal.
*
Expand Down Expand Up @@ -205,11 +203,9 @@ protected void sendRequests() throws Exception {
}

if (env != null) {
for (Enumeration<byte[]> _env = env.keys(); _env.hasMoreElements();) {
byte[] name = _env.nextElement();
byte[] value = env.get(name);
for (Map.Entry<byte[], byte[]> e : env.entrySet()) {
request = new RequestEnv();
((RequestEnv) request).setEnv(toByteArray(name), toByteArray(value));
((RequestEnv) request).setEnv(toByteArray(e.getKey()), toByteArray(e.getValue()));
request.request(_session, this);
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/jcraft/jsch/ChannelSftp.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
import java.io.PipedOutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class ChannelSftp extends ChannelSession {
Expand Down Expand Up @@ -135,7 +136,7 @@ public class ChannelSftp extends ChannelSession {
private int server_version = 3;
private String version = String.valueOf(client_version);

private Hashtable<String, String> extensions = null;
private Map<String, String> extensions = null;
private InputStream io_in = null;

private boolean extension_posix_rename = false;
Expand Down Expand Up @@ -255,7 +256,7 @@ public void start() throws JSchException {
type = header.type; // 2 -> SSH_FXP_VERSION
server_version = header.rid;
// System.err.println("SFTP protocol server-version="+server_version);
extensions = new Hashtable<>();
extensions = new HashMap<>();
if (length > 0) {
// extension data
fill(buf, length);
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/com/jcraft/jsch/ChannelX11.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@

import java.io.IOException;
import java.net.Socket;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

class ChannelX11 extends Channel {

Expand All @@ -45,8 +46,8 @@ class ChannelX11 extends Channel {
static byte[] cookie = null;
private static byte[] cookie_hex = null;

private static Hashtable<Session, byte[]> faked_cookie_pool = new Hashtable<>();
private static Hashtable<Session, byte[]> faked_cookie_hex_pool = new Hashtable<>();
private static final Map<Session, byte[]> faked_cookie_pool = new ConcurrentHashMap<>();
private static final Map<Session, byte[]> faked_cookie_hex_pool = new ConcurrentHashMap<>();

private static byte[] table = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61,
0x62, 0x63, 0x64, 0x65, 0x66};
Expand Down Expand Up @@ -219,11 +220,8 @@ void write(byte[] foo, int s, int l) throws IOException {

byte[] bar = new byte[dlen];
System.arraycopy(foo, s + 12 + plen + ((-plen) & 3), bar, 0, dlen);
byte[] faked_cookie = null;

synchronized (faked_cookie_pool) {
faked_cookie = faked_cookie_pool.get(_session);
}
byte[] faked_cookie = faked_cookie_pool.get(_session);

/*
* System.err.print("faked_cookie: "); for(int i=0; i<faked_cookie.length; i++){
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/com/jcraft/jsch/JSch.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
package com.jcraft.jsch;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

public class JSch {
/** The version number. */
public static final String VERSION = Version.getVersion();

static Hashtable<String, String> config = new Hashtable<>();
static Map<String, String> config = new ConcurrentHashMap<>();

static {
config.put("kex", Util.getSystemProperty("jsch.kex",
Expand Down Expand Up @@ -608,14 +608,10 @@ public static String getConfig(String key) {
*
* @param newconf configurations
*/
public static void setConfig(Hashtable<String, String> newconf) {
public static void setConfig(Map<String, String> newconf) {
synchronized (config) {
for (Enumeration<String> e = newconf.keys(); e.hasMoreElements();) {
String newkey = e.nextElement();
String key =
(newkey.equals("PubkeyAcceptedKeyTypes") ? "PubkeyAcceptedAlgorithms" : newkey);
config.put(key, newconf.get(newkey));
}
newconf.forEach((key, value) -> config
.put(key.equals("PubkeyAcceptedKeyTypes") ? "PubkeyAcceptedAlgorithms" : key, value));
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/jcraft/jsch/OpenSSHConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -113,7 +114,7 @@ public static OpenSSHConfig parseFile(String file) throws IOException {
_parse(br);
}

private final Hashtable<String, Vector<String[]>> config = new Hashtable<>();
private final Map<String, Vector<String[]>> config = new HashMap<>();
private final Vector<String> hosts = new Vector<>();

private void _parse(BufferedReader br) throws IOException {
Expand Down Expand Up @@ -156,11 +157,11 @@ public Config getConfig(String host) {
*
* @return map
*/
static Hashtable<String, String> getKeymap() {
static Map<String, String> getKeymap() {
return keymap;
}

private static final Hashtable<String, String> keymap = new Hashtable<>();
private static final Map<String, String> keymap = new HashMap<>();

static {
keymap.put("kex", "KexAlgorithms");
Expand Down
28 changes: 24 additions & 4 deletions src/main/java/com/jcraft/jsch/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import javax.crypto.AEADBadTagException;

public class Session {
Expand Down Expand Up @@ -143,7 +146,7 @@ public class Session {

SocketFactory socket_factory = null;

private Hashtable<String, String> config = null;
private Map<String, String> config = null;

private Proxy proxy = null;
private UserInfo userinfo;
Expand Down Expand Up @@ -2752,17 +2755,23 @@ public void setPassword(byte[] password) {
}

public void setConfig(Properties newconf) {
Hashtable<String, String> foo = new Hashtable<>();
Map<String, String> foo = new HashMap<>();
for (String key : newconf.stringPropertyNames()) {
foo.put(key, newconf.getProperty(key));
}
setConfig(foo);
}

/**
* Deprecated. Please use the {@link #setConfig(Map)}
*
* @param newconf
*/
@Deprecated
public void setConfig(Hashtable<String, String> newconf) {
synchronized (lock) {
if (config == null)
config = new Hashtable<>();
config = new ConcurrentHashMap<>();
for (Enumeration<String> e = newconf.keys(); e.hasMoreElements();) {
String newkey = e.nextElement();
String key =
Expand All @@ -2773,10 +2782,21 @@ public void setConfig(Hashtable<String, String> newconf) {
}
}

public void setConfig(Map<String, String> newconf) {
synchronized (lock) {
if (config == null) {
config = new ConcurrentHashMap<>();
}
newconf.forEach((key, value) -> {
config.put(key.equals("PubkeyAcceptedKeyTypes") ? "PubkeyAcceptedAlgorithms" : key, value);
});
}
}

public void setConfig(String key, String value) {
synchronized (lock) {
if (config == null) {
config = new Hashtable<>();
config = new ConcurrentHashMap<>();
}
if (key.equals("PubkeyAcceptedKeyTypes")) {
config.put("PubkeyAcceptedAlgorithms", value);
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/com/jcraft/jsch/JSchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;

import java.util.Hashtable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

class JSchTest {

@BeforeEach
Expand All @@ -29,8 +31,8 @@ void setPubkeyAcceptedKeyTypes() throws JSchException {
}

@Test
void setPubkeyAcceptedKeyTypesHashtable() throws JSchException {
Hashtable<String, String> newconf = new Hashtable<>();
void setPubkeyAcceptedKeyTypesHashtable() {
Map<String, String> newconf = new HashMap<>();
newconf.put("PubkeyAcceptedKeyTypes", "JSchTest333");
JSch.setConfig(newconf);
assertEquals("JSchTest333", JSch.getConfig("PubkeyAcceptedKeyTypes"));
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/com/jcraft/jsch/KnownHostsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -54,15 +55,15 @@ class KnownHostsTest {
private static final byte[] rsaKeyBytes = Util.str2byte(" ssh-rsa");
private LinkedList<String> messages;
private JSch jsch;
private Hashtable<String, String> orgConfig;
private Map<String, String> orgConfig;
private Properties orgProps;

@BeforeEach
void setupTest() {
orgProps = System.getProperties();
Properties myProps = new Properties(orgProps);
System.setProperties(myProps);
orgConfig = new Hashtable<>(JSch.config);
orgConfig = new HashMap<>(JSch.config);
messages = new LinkedList<>();
jsch = new JSch();
jsch.setInstanceLogger(new TestLogger(messages));
Expand Down

0 comments on commit fbc39bd

Please sign in to comment.