From 2cbcbe5697321bc8598a2b75dfedf46ebb37f905 Mon Sep 17 00:00:00 2001 From: rusefillc Date: Thu, 8 Oct 2020 00:35:40 -0400 Subject: [PATCH] remote proxy bugfix: the magic of signed bytes --- .../io/src/main/java/com/rusefi/binaryprotocol/IoHelper.java | 4 ++++ .../src/main/java/com/rusefi/io/tcp/BinaryProtocolProxy.java | 3 ++- .../main/java/com/rusefi/io/tcp/BinaryProtocolServer.java | 3 ++- .../java/com/rusefi/binaryprotocol/test/IoHelperTest.java | 5 +++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/java_console/io/src/main/java/com/rusefi/binaryprotocol/IoHelper.java b/java_console/io/src/main/java/com/rusefi/binaryprotocol/IoHelper.java index a350c53c41..2990f50c35 100644 --- a/java_console/io/src/main/java/com/rusefi/binaryprotocol/IoHelper.java +++ b/java_console/io/src/main/java/com/rusefi/binaryprotocol/IoHelper.java @@ -65,4 +65,8 @@ public class IoHelper { public static boolean checkResponseCode(byte[] response, byte code) { return response != null && response.length > 0 && response[0] == code; } + + public static int getInt(byte firstByte, byte secondByte) { + return firstByte * 256 + (secondByte & 0xFF); + } } diff --git a/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolProxy.java b/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolProxy.java index 6d765af04f..ffc7a46b59 100644 --- a/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolProxy.java +++ b/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolProxy.java @@ -6,6 +6,7 @@ import com.rusefi.Listener; import com.rusefi.Timeouts; import com.rusefi.binaryprotocol.BinaryProtocol; import com.rusefi.binaryprotocol.IncomingDataBuffer; +import com.rusefi.binaryprotocol.IoHelper; import com.rusefi.config.generated.Fields; import com.rusefi.io.IoStream; import com.rusefi.proxy.NetworkConnector; @@ -80,7 +81,7 @@ public class BinaryProtocolProxy { @NotNull private static BinaryProtocolServer.Packet readClientRequest(IncomingDataBuffer in, byte firstByte) throws IOException { byte secondByte = in.readByte(); - int length = firstByte * 256 + secondByte; + int length = IoHelper.getInt(firstByte, secondByte); return BinaryProtocolServer.readPromisedBytes(in, length); } diff --git a/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java b/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java index aba413ce21..1b70ff0e00 100644 --- a/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java +++ b/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java @@ -367,7 +367,8 @@ public class BinaryProtocolServer { protocolCommandHandler.handle(); return 0; } - return first * 256 + in.readByte(ioTimeout); + byte secondByte = in.readByte(ioTimeout); + return IoHelper.getInt(first, secondByte); } public static Packet readPromisedBytes(DataInputStream in, int length) throws IOException { diff --git a/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/IoHelperTest.java b/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/IoHelperTest.java index deeb7014b0..0e0ca31d1a 100644 --- a/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/IoHelperTest.java +++ b/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/IoHelperTest.java @@ -10,6 +10,11 @@ import java.io.IOException; import static org.junit.Assert.assertEquals; public class IoHelperTest { + @Test + public void test255() { + assertEquals(255, IoHelper.getInt((byte) 0, (byte) -1)); + } + @Test public void testSwap16() throws IOException { byte packet[] = {-12, 0};