Add extra ShockPacket constructors and fix more charset issues

This commit is contained in:
UnfamiliarLegacy 2024-06-25 06:08:17 +02:00
parent c6064b05ad
commit b5deb920bd
6 changed files with 105 additions and 18 deletions

View File

@ -49,8 +49,7 @@ public class HPacket implements StringifyAble {
} }
public HPacket(int header) { public HPacket(int header) {
packetInBytes = new byte[]{0,0,0,2,0,0}; initPacket(header);
replacePacketId((short)header);
isEdited = false; isEdited = false;
} }
@ -72,7 +71,7 @@ public class HPacket implements StringifyAble {
} }
public HPacket(String identifier, HMessage.Direction direction) throws InvalidParameterException { public HPacket(String identifier, HMessage.Direction direction) throws InvalidParameterException {
packetInBytes = new byte[]{0,0,0,2,-1,-1}; initPacket(0);
this.identifier = identifier; this.identifier = identifier;
this.identifierDirection = direction; this.identifierDirection = direction;
} }
@ -83,6 +82,11 @@ public class HPacket implements StringifyAble {
isEdited = false; isEdited = false;
} }
protected void initPacket(int header) {
packetInBytes = new byte[]{0,0,0,2,0,0};
replacePacketId((short)header);
}
public HPacketFormat getFormat() { public HPacketFormat getFormat() {
return packetFormat; return packetFormat;
} }
@ -650,7 +654,7 @@ public class HPacket implements StringifyAble {
appendShort((Short)o); appendShort((Short)o);
} }
else if (o instanceof String) { else if (o instanceof String) {
appendString((String)o, StandardCharsets.UTF_8); appendString((String)o, StandardCharsets.ISO_8859_1);
} }
else if (o instanceof Boolean) { else if (o instanceof Boolean) {
appendBoolean((Boolean) o); appendBoolean((Boolean) o);

View File

@ -30,11 +30,40 @@ public abstract class ShockPacket extends HPacket {
} }
public ShockPacket(HPacketFormat format, int header) { public ShockPacket(HPacketFormat format, int header) {
super(Base64Encoding.encode(header, 2)); super(header);
packetFormat = format; packetFormat = format;
readIndex = 2; readIndex = 2;
} }
public ShockPacket(HPacketFormat format, int header, byte[] bytes) {
super(header, bytes);
packetFormat = format;
readIndex = 2;
}
public ShockPacket(HPacketFormat format, int header, Object... objects) throws InvalidParameterException {
super(header, objects);
packetFormat = format;
readIndex = 2;
}
public ShockPacket(HPacketFormat format, String identifier, HMessage.Direction direction) throws InvalidParameterException {
super(identifier, direction);
packetFormat = format;
readIndex = 2;
}
public ShockPacket(HPacketFormat format, String identifier, HMessage.Direction direction, Object... objects) throws InvalidParameterException {
super(identifier, direction, objects);
packetFormat = format;
readIndex = 2;
}
@Override
protected void initPacket(int header) {
packetInBytes = Base64Encoding.encode(header, 2);
}
@Override @Override
public HPacket appendBoolean(boolean b) { public HPacket appendBoolean(boolean b) {
throw new ShockPacketUnsupported(); throw new ShockPacketUnsupported();
@ -102,12 +131,12 @@ public abstract class ShockPacket extends HPacket {
@Override @Override
public HPacket appendObjects(Object... objects) { public HPacket appendObjects(Object... objects) {
throw new ShockPacketUnsupported(); return super.appendObjects(objects);
} }
@Override @Override
public HPacket appendObject(Object o) throws InvalidParameterException { public HPacket appendObject(Object o) throws InvalidParameterException {
throw new ShockPacketUnsupported(); return super.appendObject(o);
} }
@Override @Override

View File

@ -2,11 +2,12 @@ package gearth.protocol.packethandler.shockwave.packets;
import gearth.encoding.VL64Encoding; import gearth.encoding.VL64Encoding;
import gearth.misc.ArrayUtils; import gearth.misc.ArrayUtils;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.protocol.HPacketFormat; import gearth.protocol.HPacketFormat;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.security.InvalidParameterException;
import java.util.Arrays; import java.util.Arrays;
// Server to Client // Server to Client
@ -27,6 +28,22 @@ public class ShockPacketIncoming extends ShockPacket {
super(HPacketFormat.WEDGIE_INCOMING, header); super(HPacketFormat.WEDGIE_INCOMING, header);
} }
public ShockPacketIncoming(int header, byte[] bytes) {
super(HPacketFormat.WEDGIE_INCOMING, header, bytes);
}
public ShockPacketIncoming(int header, Object... objects) throws InvalidParameterException {
super(HPacketFormat.WEDGIE_INCOMING, header, objects);
}
public ShockPacketIncoming(String identifier, HMessage.Direction direction) throws InvalidParameterException {
super(HPacketFormat.WEDGIE_INCOMING, identifier, direction);
}
public ShockPacketIncoming(String identifier, HMessage.Direction direction, Object... objects) throws InvalidParameterException {
super(HPacketFormat.WEDGIE_INCOMING, identifier, direction, objects);
}
@Override @Override
public HPacket appendBoolean(boolean value) { public HPacket appendBoolean(boolean value) {
isEdited = true; isEdited = true;

View File

@ -2,11 +2,12 @@ package gearth.protocol.packethandler.shockwave.packets;
import gearth.encoding.Base64Encoding; import gearth.encoding.Base64Encoding;
import gearth.encoding.VL64Encoding; import gearth.encoding.VL64Encoding;
import gearth.protocol.HMessage;
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.protocol.HPacketFormat; import gearth.protocol.HPacketFormat;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.security.InvalidParameterException;
// Client to Server // Client to Server
public class ShockPacketOutgoing extends ShockPacket { public class ShockPacketOutgoing extends ShockPacket {
@ -26,6 +27,22 @@ public class ShockPacketOutgoing extends ShockPacket {
super(HPacketFormat.WEDGIE_OUTGOING, header); super(HPacketFormat.WEDGIE_OUTGOING, header);
} }
public ShockPacketOutgoing(int header, byte[] bytes) {
super(HPacketFormat.WEDGIE_INCOMING, header, bytes);
}
public ShockPacketOutgoing(int header, Object... objects) throws InvalidParameterException {
super(HPacketFormat.WEDGIE_INCOMING, header, objects);
}
public ShockPacketOutgoing(String identifier, HMessage.Direction direction) throws InvalidParameterException {
super(HPacketFormat.WEDGIE_INCOMING, identifier, direction);
}
public ShockPacketOutgoing(String identifier, HMessage.Direction direction, Object... objects) throws InvalidParameterException {
super(HPacketFormat.WEDGIE_INCOMING, identifier, direction, objects);
}
@Override @Override
public HPacket appendBoolean(boolean value) { public HPacket appendBoolean(boolean value) {
return appendInt(value ? 1 : 0); return appendInt(value ? 1 : 0);
@ -85,11 +102,6 @@ public class ShockPacketOutgoing extends ShockPacket {
return value; return value;
} }
@Override
public String readString() {
return this.readString(StandardCharsets.UTF_8);
}
@Override @Override
public String readString(Charset charset) { public String readString(Charset charset) {
int length = readUshort(); int length = readUshort();

View File

@ -266,7 +266,7 @@ public class PacketStringUtils {
} }
else if (c == 'i') builder.append("{i:").append(prevInt = p.readInteger()).append('}'); else if (c == 'i') builder.append("{i:").append(prevInt = p.readInteger()).append('}');
else if (c == 's') builder.append("{s:\"").append( else if (c == 's') builder.append("{s:\"").append(
p.readString(StandardCharsets.UTF_8) p.readString()
.replace("\\", "\\\\") // \ -> \\ .replace("\\", "\\\\") // \ -> \\
.replace("\"", "\\\"") // " -> \" .replace("\"", "\\\"") // " -> \"
.replace("\r", "\\r") // CR -> \r .replace("\r", "\\r") // CR -> \r

View File

@ -1,5 +1,6 @@
import gearth.protocol.HPacket; import gearth.protocol.HPacket;
import gearth.protocol.HPacketFormat; import gearth.protocol.HPacketFormat;
import gearth.protocol.packethandler.shockwave.packets.ShockPacketOutgoing;
import gearth.services.packet_representation.PacketStringUtils; import gearth.services.packet_representation.PacketStringUtils;
import org.bouncycastle.util.encoders.Hex; import org.bouncycastle.util.encoders.Hex;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -13,19 +14,22 @@ public class TestPacketStringEncoding {
@Test @Test
public void testWrite() throws Exception { public void testWrite() throws Exception {
// {h:OUT_CHAT}{s:"ç"} 40 40 45 40 74 40 41 e7 // {h:OUT_CHAT}{s:"ç"} 40 40 45 40 74 40 41 e7
checkPacket(new ShockPacketOutgoing(OUT_CHAT, "ç"), "40744041e7");
checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@Aç", "40744041e7"); checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@Aç", "40744041e7");
checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@A[231]", "40744041e7"); checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@A[231]", "40744041e7");
checkExpression(HPacketFormat.WEDGIE_OUTGOING, String.format("{h:%d}{s:\"ç\"}", OUT_CHAT), "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 // {h:OUT_CHAT}{s:"ççç"} 40 40 47 40 74 40 43 e7 e7 e7
checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@Aççç", "40744041e7e7e7"); checkPacket(new ShockPacketOutgoing(OUT_CHAT, "ççç"), "40744043e7e7e7");
checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@A[231][231][231]", "40744041e7e7e7"); checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@Cççç", "40744043e7e7e7");
checkExpression(HPacketFormat.WEDGIE_OUTGOING, "@t@C[231][231][231]", "40744043e7e7e7");
checkExpression(HPacketFormat.WEDGIE_OUTGOING, String.format("{h:%d}{s:\"ççç\"}", OUT_CHAT), "40744043e7e7e7"); 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 // {h:OUT_CHAT}{s:"çãâéèä"} 40 40 47 40 74 40 46 e7 e3 e2 e9 e8 e4
checkPacket(new ShockPacketOutgoing(OUT_CHAT, "çãâéèä"), "40744046e7e3e2e9e8e4");
checkExpression(HPacketFormat.WEDGIE_OUTGOING, String.format("{h:%d}{s:\"çãâéèä\"}", OUT_CHAT), "40744046e7e3e2e9e8e4"); checkExpression(HPacketFormat.WEDGIE_OUTGOING, String.format("{h:%d}{s:\"çãâéèä\"}", OUT_CHAT), "40744046e7e3e2e9e8e4");
} }
@Test @Test
public void testRead() throws Exception { public void testReadIncoming() throws Exception {
final String expectedString = "çãâéèä"; final String expectedString = "çãâéèä";
final String packetData = "40585045e7e3e2e9e8e40201"; final String packetData = "40585045e7e3e2e9e8e40201";
@ -36,6 +40,17 @@ public class TestPacketStringEncoding {
assertEquals(expectedString, packet.readString()); assertEquals(expectedString, packet.readString());
} }
@Test
public void testReadOutgoing() throws Exception {
final String expectedString = "çãâéèä";
final String packetData = "40744046e7e3e2e9e8e4";
final HPacket packet = HPacketFormat.WEDGIE_OUTGOING.createPacket(Hex.decode(packetData));
assertEquals(OUT_CHAT, packet.headerId());
assertEquals(expectedString, packet.readString());
}
/** /**
* @param format Packet format. * @param format Packet format.
* @param expression Handwritten expression. * @param expression Handwritten expression.
@ -48,4 +63,14 @@ public class TestPacketStringEncoding {
assertEquals(expectedHex, packetData); assertEquals(expectedHex, packetData);
} }
/**
* @param packet The packet.
* @param expectedHex Captured packets from the real client, or server.
*/
private void checkPacket(HPacket packet, String expectedHex) throws Exception {
final String packetData = Hex.toHexString(packet.toBytes());
assertEquals(expectedHex, packetData);
}
} }