From c6064b05ad4b67f3b38bd7da499e928c3a9d6744 Mon Sep 17 00:00:00 2001 From: UnfamiliarLegacy <74633542+UnfamiliarLegacy@users.noreply.github.com> Date: Tue, 25 Jun 2024 05:50:35 +0200 Subject: [PATCH] Fix packet string encoding when using expressions --- .../shockwave/packets/ShockPacket.java | 4 +- .../packets/ShockPacketIncoming.java | 10 ---- .../packets/ShockPacketOutgoing.java | 5 -- .../PacketStringUtils.java | 2 +- .../test/java/TestPacketStringEncoding.java | 51 +++++++++++++++++++ 5 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 G-Earth/src/test/java/TestPacketStringEncoding.java diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacket.java b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacket.java index 8fd613d..4b8e963 100644 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacket.java +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacket.java @@ -87,7 +87,7 @@ public abstract class ShockPacket extends HPacket { @Override public HPacket appendString(String s) { - throw new ShockPacketUnsupported(); + return appendString(s, StandardCharsets.ISO_8859_1); } @Override @@ -202,7 +202,7 @@ public abstract class ShockPacket extends HPacket { @Override public String readString() { - throw new ShockPacketUnsupported(); + return this.readString(StandardCharsets.ISO_8859_1); } @Override diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketIncoming.java b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketIncoming.java index 9812a08..c06640d 100644 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketIncoming.java +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketIncoming.java @@ -53,11 +53,6 @@ public class ShockPacketIncoming extends ShockPacket { return this; } - @Override - public HPacket appendString(String value) { - return appendString(value, StandardCharsets.ISO_8859_1); - } - @Override public HPacket appendString(String value, Charset charset) { isEdited = true; @@ -93,11 +88,6 @@ public class ShockPacketIncoming extends ShockPacket { return value; } - @Override - public String readString() { - return this.readString(StandardCharsets.UTF_8); - } - @Override public String readString(Charset charset) { String result; diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketOutgoing.java b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketOutgoing.java index ebf9465..37b96aa 100644 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketOutgoing.java +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/shockwave/packets/ShockPacketOutgoing.java @@ -50,11 +50,6 @@ public class ShockPacketOutgoing extends ShockPacket { return this; } - @Override - public HPacket appendString(String value) { - return appendString(value, StandardCharsets.ISO_8859_1); - } - @Override public HPacket appendString(String value, Charset charset) { isEdited = true; diff --git a/G-Earth/src/main/java/gearth/services/packet_representation/PacketStringUtils.java b/G-Earth/src/main/java/gearth/services/packet_representation/PacketStringUtils.java index 828eead..90e5bb7 100644 --- a/G-Earth/src/main/java/gearth/services/packet_representation/PacketStringUtils.java +++ b/G-Earth/src/main/java/gearth/services/packet_representation/PacketStringUtils.java @@ -161,7 +161,7 @@ public class PacketStringUtils { } actualString.append(match); - String latin = new String(actualString.toString().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1); + String latin = new String(actualString.toString().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.ISO_8859_1); HPacket temp = format.createPacket(0); int sizeBefore = temp.getBytesLength(); diff --git a/G-Earth/src/test/java/TestPacketStringEncoding.java b/G-Earth/src/test/java/TestPacketStringEncoding.java new file mode 100644 index 0000000..d041183 --- /dev/null +++ b/G-Earth/src/test/java/TestPacketStringEncoding.java @@ -0,0 +1,51 @@ +import gearth.protocol.HPacket; +import gearth.protocol.HPacketFormat; +import gearth.services.packet_representation.PacketStringUtils; +import org.bouncycastle.util.encoders.Hex; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestPacketStringEncoding { + + private static final int OUT_CHAT = 52; + + @Test + public void testWrite() throws Exception { + // {h:OUT_CHAT}{s:"ç"} 40 40 45 40 74 40 41 e7 + checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@Aç", "40744041e7"); + checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@A[231]", "40744041e7"); + checkExpression(HPacketFormat.WEDGIE_OUTGOING, String.format("{h:%d}{s:\"ç\"}", OUT_CHAT), "40744041e7"); + // {h:OUT_CHAT}{s:"ççç"} 40 40 47 40 74 40 43 e7 e7 e7 + checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@Aççç", "40744041e7e7e7"); + checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@A[231][231][231]", "40744041e7e7e7"); + checkExpression(HPacketFormat.WEDGIE_OUTGOING, String.format("{h:%d}{s:\"ççç\"}", OUT_CHAT), "40744043e7e7e7"); + // {h:OUT_CHAT}{s:"çãâéèä"} 40 40 47 40 74 40 46 e7 e3 e2 e9 e8 e4 + checkExpression(HPacketFormat.WEDGIE_OUTGOING, String.format("{h:%d}{s:\"çãâéèä\"}", OUT_CHAT), "40744046e7e3e2e9e8e4"); + } + + @Test + public void testRead() throws Exception { + final String expectedString = "çãâéèä"; + + final String packetData = "40585045e7e3e2e9e8e40201"; + final HPacket packet = HPacketFormat.WEDGIE_INCOMING.createPacket(Hex.decode(packetData)); + + assertEquals(24, packet.headerId()); + assertEquals(20, packet.readInteger()); + assertEquals(expectedString, packet.readString()); + } + + /** + * @param format Packet format. + * @param expression Handwritten expression. + * @param expectedHex Captured packets from the real client, or server. + */ + private void checkExpression(HPacketFormat format, String expression, String expectedHex) throws Exception { + final HPacket packet = PacketStringUtils.fromString(expression, format); + final String packetData = Hex.toHexString(packet.toBytes()); + + assertEquals(expectedHex, packetData); + } + +}