HPacket charset argument for string utils (incomplete)

This commit is contained in:
sirjonasxx 2020-08-19 17:27:01 +02:00
parent 3d59ccbab4
commit cde38f5f0d
2 changed files with 56 additions and 26 deletions

View File

@ -196,7 +196,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(
new String(p.readString().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8) p.readString(StandardCharsets.UTF_8)
.replace("\\", "\\\\") // \ -> \\ .replace("\\", "\\\\") // \ -> \\
.replace("\"", "\\\"") // " -> \" .replace("\"", "\\\"") // " -> \"
.replace("\r", "\\r") // CR -> \r .replace("\r", "\\r") // CR -> \r

View File

@ -8,6 +8,7 @@ import gearth.misc.packetrepresentation.PacketStringUtils;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
import java.util.Arrays; import java.util.Arrays;
@ -180,34 +181,49 @@ public class HPacket implements StringifyAble {
return java.nio.ByteBuffer.wrap(btarray).getLong(); return java.nio.ByteBuffer.wrap(btarray).getLong();
} }
public String readString() {
String r = readString(readIndex); public String readString(Charset charset) {
readIndex += (2 + r.length()); String r = readString(readIndex, charset);
readIndex += (2 + readUshort(readIndex));
return r; return r;
} }
public String readString(int index) { public String readString(int index, Charset charset) {
int length = readUshort(index); int length = readUshort(index);
index+=2; index+=2;
return readString(index, length, charset);
return readString(index, length);
} }
private String readString(int index, int length) { public String readString() {
return readString(StandardCharsets.ISO_8859_1);
}
public String readString(int index) {
return readString(index, StandardCharsets.ISO_8859_1);
}
private String readString(int index, int length, Charset charset) {
byte[] x = new byte[length]; byte[] x = new byte[length];
for (int i = 0; i < x.length; i++) { x[i] = readByte(index); index++; } for (int i = 0; i < x.length; i++) { x[i] = readByte(index); index++; }
return new String(x, StandardCharsets.ISO_8859_1); return new String(x, charset);
} }
public String readLongString() {
String r = readLongString(readIndex); public String readLongString(Charset charset) {
readIndex += (4 + r.length()); String r = readLongString(readIndex, charset);
readIndex += (4 + readInteger(readIndex));
return r; return r;
} }
public String readLongString(int index) { public String readLongString(int index, Charset charset) {
int length = readInteger(index); int length = readInteger(index);
index += 4; index += 4;
return readString(index, length); return readString(index, length, charset);
}
public String readLongString() {
return readLongString(StandardCharsets.ISO_8859_1);
}
public String readLongString(int index) {
return readLongString(index, StandardCharsets.ISO_8859_1);
} }
public boolean readBoolean() { public boolean readBoolean() {
@ -267,10 +283,11 @@ public class HPacket implements StringifyAble {
packetInBytes[index + 1] = b.array()[1]; packetInBytes[index + 1] = b.array()[1];
return this; return this;
} }
public HPacket replaceString(int index, String s) {
public HPacket replaceString(int index, String s, Charset charset) {
isEdited = true; isEdited = true;
byte[] sbytes = s.getBytes(StandardCharsets.ISO_8859_1); byte[] sbytes = s.getBytes(charset);
int mover = s.length() - readUshort(index); int mover = sbytes.length - readUshort(index);
if (mover != 0) { if (mover != 0) {
byte[] newPacket = Arrays.copyOf(packetInBytes, packetInBytes.length + mover); byte[] newPacket = Arrays.copyOf(packetInBytes, packetInBytes.length + mover);
@ -283,7 +300,7 @@ public class HPacket implements StringifyAble {
} }
} }
else { else {
int i = index + 2 + s.length(); int i = index + 2 + sbytes.length;
while (i < newPacket.length) { while (i < newPacket.length) {
newPacket[i] = packetInBytes[i - mover]; newPacket[i] = packetInBytes[i - mover];
i++; i++;
@ -294,13 +311,17 @@ public class HPacket implements StringifyAble {
fixLength(); fixLength();
} }
replaceUShort(index, s.length()); replaceUShort(index, sbytes.length);
for (int i = 0; i < s.length(); i++) { for (int i = 0; i < sbytes.length; i++) {
packetInBytes[index + 2 + i] = sbytes[i]; packetInBytes[index + 2 + i] = sbytes[i];
} }
return this; return this;
} }
public HPacket replaceString(int index, String s) {
return replaceString(index, s, StandardCharsets.ISO_8859_1);
}
private boolean canReadString(int index) { private boolean canReadString(int index) {
if (index < packetInBytes.length - 1) { if (index < packetInBytes.length - 1) {
int l = readUshort(index); int l = readUshort(index);
@ -445,18 +466,27 @@ public class HPacket implements StringifyAble {
fixLength(); fixLength();
return this; return this;
} }
public HPacket appendString(String s, Charset charset) {
isEdited = true;
appendUShort(s.getBytes(charset).length);
appendBytes(s.getBytes(charset));
return this;
}
public HPacket appendString(String s) { public HPacket appendString(String s) {
return appendString(s, StandardCharsets.ISO_8859_1);
}
public HPacket appendLongString(String s, Charset charset) {
isEdited = true; isEdited = true;
appendUShort(s.length()); appendInt(s.getBytes(charset).length);
appendBytes(s.getBytes(StandardCharsets.ISO_8859_1)); appendBytes(s.getBytes(charset));
return this; return this;
} }
public HPacket appendLongString(String s) { public HPacket appendLongString(String s) {
isEdited = true; return appendLongString(s, StandardCharsets.ISO_8859_1);
appendInt(s.length());
appendBytes(s.getBytes(StandardCharsets.ISO_8859_1));
return this;
} }
public HPacket appendObject(Object o) throws InvalidParameterException { public HPacket appendObject(Object o) throws InvalidParameterException {
isEdited = true; isEdited = true;