Data Types: Difference between revisions
From RuneWiki
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
=== | === get Methods === | ||
These buffer | <syntaxhighlight lang="java" line="1"> | ||
public final int g1() { | |||
return this.data[this.pos++] & 0xFF; | |||
} | |||
public final byte g1b() { | |||
return this.data[this.pos++]; | |||
} | |||
public final int g2() { | |||
this.pos += 2; | |||
return ((this.data[this.pos - 2] & 0xFF) << 8) + (this.data[this.pos - 1] & 0xFF); | |||
} | |||
public final int g2s() { | |||
this.pos += 2; | |||
int value = ((this.data[this.pos - 2] & 0xFF) << 8) + (this.data[this.pos - 1] & 0xFF); | |||
if (value > 32767) { | |||
value -= 65536; | |||
} | |||
return value; | |||
} | |||
public final int ig2() { | |||
this.pos += 2; | |||
return ((this.data[this.pos - 1] & 0xFF) << 8) + (this.data[this.pos - 2] & 0xFF); | |||
} | |||
public final int g3() { | |||
this.pos += 3; | |||
return ((this.data[this.pos - 3] & 0xFF) << 16) + ((this.data[this.pos - 2] & 0xFF) << 8) + (this.data[this.pos - 1] & 0xFF); | |||
} | |||
public final int g4() { | |||
this.pos += 4; | |||
return ((this.data[this.pos - 4] & 0xFF) << 24) + ((this.data[this.pos - 3] & 0xFF) << 16) + ((this.data[this.pos - 2] & 0xFF) << 8) + (this.data[this.pos - 1] & 0xFF); | |||
} | |||
public final int ig4() { | |||
this.pos += 4; | |||
return ((this.data[this.pos - 1] & 0xFF) << 24) + ((this.data[this.pos - 2] & 0xFF) << 16) + ((this.data[this.pos - 3] & 0xFF) << 8) + (this.data[this.pos - 4] & 0xFF); | |||
} | |||
public final long g5() { | |||
long high = (long) this.g1() & 0xFFFFFFFFL; | |||
long low = (long) this.g4() & 0xFFFFFFFFL; | |||
return (high << 32) + low; | |||
} | |||
public final long g8() { | |||
long high = (long) this.g4() & 0xFFFFFFFFL; | |||
long low = (long) this.g4() & 0xFFFFFFFFL; | |||
return (high << 32) + low; | |||
} | |||
public final long ig8() { | |||
long low = (long) this.ig4() & 0xFFFFFFFFL; | |||
long high = (long) this.ig4() & 0xFFFFFFFFL; | |||
return (high << 32) + low; | |||
} | |||
public final String gjstr() { | |||
int start = this.pos; | |||
// in earlier revisions, e.g. 317, this will decode until != 10 terminates the string | |||
while (this.data[this.pos++] != 0) {} | |||
int length = this.pos - start - 1; | |||
return length == 0 ? "" : Cp1252Utils.decode(this.data, start, length); | |||
} | |||
public final String fastgjstr() { | |||
if (this.data[this.pos] == 0) { | |||
this.pos++; | |||
return null; | |||
} else { | |||
return this.gjstr(); | |||
} | |||
} | |||
public final String gjstr2() { | |||
byte version = this.data[this.pos++]; | |||
if (version != 0) { | |||
throw new IllegalStateException("Bad version number in gjstr2"); | |||
} | |||
int start = this.pos; | |||
while (this.data[this.pos++] != 0) {} | |||
int length = this.pos - start - 1; | |||
return length == 0 ? "" : Cp1252.decode(this.data, start, length); | |||
} | |||
public final int gSmart1or2() { | |||
int value = this.data[this.pos] & 0xFF; | |||
return value < 128 ? this.g1() : this.g2() - 32768; | |||
} | |||
public final int gSmart1or2s() { | |||
int value = this.data[this.pos] & 0xFF; | |||
return value < 128 ? this.g1() - 64 : this.g2() - 49152; | |||
} | |||
public final int gSmart2or4null() { | |||
if (this.data[this.pos] < 0) { | |||
return this.g4() & Integer.MAX_VALUE; | |||
} else { | |||
int value = this.g2(); | |||
return value == 32767 ? -1 : value; | |||
} | |||
} | |||
public final int gExtended1or2() { | |||
int value = 0; | |||
int remainder; | |||
for (remainder = this.gSmart1or2(); remainder == 32767; remainder = this.gSmart1or2()) { | |||
value += 32767; | |||
} | |||
return value + remainder; | |||
} | |||
public final int gVarInt() { | |||
byte value = this.data[this.pos++]; | |||
int remainder = 0; | |||
while (value < 0) { | |||
remainder = (remainder | value & 0x7F) << 7; | |||
value = this.data[this.pos++]; | |||
} | |||
return value | remainder; | |||
} | |||
public final long gVarLong(int bytes) { | |||
int read = bytes - 1; | |||
if (read < 0 || read > 7) { | |||
throw new IllegalArgumentException(); | |||
} | |||
int bits = read * 8; | |||
long result = 0L; | |||
while (bits >= 0) { | |||
result |= ((long) this.data[this.pos++] & 0xFFL) << bits; | |||
bits -= 8; | |||
} | |||
return result; | |||
} | |||
</syntaxhighlight> | |||
=== put Methods === | |||
<syntaxhighlight lang="java"> | |||
//todo | |||
public final void pdata(int offset, int length, byte[] dest) { | |||
for (int i = offset; i < length + offset; i++) { | |||
dest[i] = this.data[this.pos++]; | |||
} | |||
} | |||
</syntaxhighlight> | |||
=== alt Methods === | |||
These buffer methods are used to obfuscate the network protocol - there up to 3 alt types, but their obfuscation process can strip out unused methods so there is a chance not all of them are in a specific revision. | |||
You won't find these used in the cache. | You won't find these used in the cache. | ||
==== | ==== get ==== | ||
<syntaxhighlight lang="java" line="1"> | <syntaxhighlight lang="java" line="1"> | ||
public final int g1_alt1() { | public final int g1_alt1() { | ||
Line 61: | Line 222: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==== | ==== put ==== | ||
<syntaxhighlight lang="java" line="1"> | <syntaxhighlight lang="java" line="1"> | ||
public final void p1_alt1(int value) { | public final void p1_alt1(int value) { | ||
Line 109: | Line 270: | ||
this.data[this.pos++] = (byte) value; | this.data[this.pos++] = (byte) value; | ||
this.data[this.pos++] = (byte) (value >> 8); | this.data[this.pos++] = (byte) (value >> 8); | ||
} | |||
</syntaxhighlight> | |||
=== Utility Methods === | |||
<syntaxhighlight lang="java" line="1"> | |||
public static final int[] crctable = new int[256]; | |||
static { | |||
for (int b = 0; b < 256; b++) { | |||
int remainder = b; | |||
for (int bit = 0; bit < 8; bit++) { | |||
if ((remainder & 0x1) == 1) { | |||
remainder = remainder >>> 1 ^ 0xEDB88320; | |||
} else { | |||
remainder >>>= 0x1; | |||
} | |||
} | |||
crctable[b] = remainder; | |||
} | |||
} | |||
public static int getcrc(byte[] src, int offset, int length) { | |||
int checksum = -1; | |||
for (int i = offset; i < length; i++) { | |||
checksum = checksum >>> 8 ^ crctable[(checksum ^ src[i]) & 0xFF]; | |||
} | |||
return ~checksum; | |||
} | |||
public static int getcrc(byte[] src, int length) { | |||
return getcrc(src, 0, length); | |||
} | |||
public final int addcrc(int offset) { | |||
int checksum = getcrc(this.pos, offset, this.data); | |||
this.p4(checksum); | |||
return checksum; | |||
} | |||
public final void rsaenc(BigInteger modulus, BigInteger exponent) { | |||
int length = this.pos; | |||
this.pos = 0; | |||
byte[] temp = new byte[length]; | |||
this.pdata(0, length, temp); | |||
BigInteger bigRaw = new BigInteger(temp); | |||
BigInteger bigEnc = bigRaw.modPow(exponent, modulus); | |||
byte[] rawEnc = bigEnc.toByteArray(); | |||
this.pos = 0; | |||
this.p2(rawEnc.length); | |||
this.pdata(rawEnc.length, rawEnc, 0); | |||
} | |||
public final void tinyenc(int[] key, int offset, int length) { | |||
int start = this.pos; | |||
this.pos = offset; | |||
int blocks = (length - offset) / 8; | |||
for (int i = 0; i < blocks; i++) { | |||
int v0 = this.g4(); | |||
int v1 = this.g4(); | |||
int sum = 0; | |||
int numRounds = 32; | |||
while (numRounds-- > 0) { | |||
v0 += v1 + (v1 << 4 ^ v1 >>> 5) ^ sum + key[sum & 0x3]; | |||
sum += 0x9E3779B9; | |||
v1 += (v0 >>> 5 ^ v0 << 4) + v0 ^ sum + key[sum >>> 11 & 0xE8C00003]; | |||
} | |||
this.pos -= 8; | |||
this.p4(v0); | |||
this.p4(v1); | |||
} | |||
this.pos = start; | |||
} | |||
public final void tinydec(int[] key) { | |||
int blocks = this.pos / 8; | |||
this.pos = 0; | |||
for (int i = 0; i < blocks; i++) { | |||
int v0 = this.g4(); | |||
int v1 = this.g4(); | |||
int sum = 0xC6EF3720; | |||
int num_rounds = 32; | |||
while (num_rounds-- > 0) { | |||
v1 -= (v0 >>> 5 ^ v0 << 4) + v0 ^ sum + key[sum >>> 11 & 0x3]; | |||
sum -= 0x9E3779B9; | |||
v0 -= sum + key[sum & 0x3] ^ v1 + (v1 << 4 ^ v1 >>> 5); | |||
} | |||
this.pos -= 8; | |||
this.p4(v0); | |||
this.p4(v1); | |||
} | |||
} | |||
public final void tinydec(int[] key, int offset, int length) { | |||
int start = this.pos; | |||
this.pos = offset; | |||
int blocks = (length - offset) / 8; | |||
for (int i = 0; i < blocks; i++) { | |||
int v0 = this.g4(); | |||
int v1 = this.g4(); | |||
int sum = 0xC6EF3720; | |||
int num_rounds = 32; | |||
while (num_rounds-- > 0) { | |||
v1 -= (v0 << 4 ^ v0 >>> 5) + v0 ^ key[sum >>> 11 & 0x3] + sum; | |||
sum -= 0x9E3779B9; | |||
v0 -= v1 + (v1 >>> 5 ^ v1 << 4) ^ sum + key[sum & 0x3]; | |||
} | |||
this.pos -= 8; | |||
this.p4(v0); | |||
this.p4(v1); | |||
} | |||
this.pos = start; | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 15:00, 11 February 2024
get Methods
public final int g1() {
return this.data[this.pos++] & 0xFF;
}
public final byte g1b() {
return this.data[this.pos++];
}
public final int g2() {
this.pos += 2;
return ((this.data[this.pos - 2] & 0xFF) << 8) + (this.data[this.pos - 1] & 0xFF);
}
public final int g2s() {
this.pos += 2;
int value = ((this.data[this.pos - 2] & 0xFF) << 8) + (this.data[this.pos - 1] & 0xFF);
if (value > 32767) {
value -= 65536;
}
return value;
}
public final int ig2() {
this.pos += 2;
return ((this.data[this.pos - 1] & 0xFF) << 8) + (this.data[this.pos - 2] & 0xFF);
}
public final int g3() {
this.pos += 3;
return ((this.data[this.pos - 3] & 0xFF) << 16) + ((this.data[this.pos - 2] & 0xFF) << 8) + (this.data[this.pos - 1] & 0xFF);
}
public final int g4() {
this.pos += 4;
return ((this.data[this.pos - 4] & 0xFF) << 24) + ((this.data[this.pos - 3] & 0xFF) << 16) + ((this.data[this.pos - 2] & 0xFF) << 8) + (this.data[this.pos - 1] & 0xFF);
}
public final int ig4() {
this.pos += 4;
return ((this.data[this.pos - 1] & 0xFF) << 24) + ((this.data[this.pos - 2] & 0xFF) << 16) + ((this.data[this.pos - 3] & 0xFF) << 8) + (this.data[this.pos - 4] & 0xFF);
}
public final long g5() {
long high = (long) this.g1() & 0xFFFFFFFFL;
long low = (long) this.g4() & 0xFFFFFFFFL;
return (high << 32) + low;
}
public final long g8() {
long high = (long) this.g4() & 0xFFFFFFFFL;
long low = (long) this.g4() & 0xFFFFFFFFL;
return (high << 32) + low;
}
public final long ig8() {
long low = (long) this.ig4() & 0xFFFFFFFFL;
long high = (long) this.ig4() & 0xFFFFFFFFL;
return (high << 32) + low;
}
public final String gjstr() {
int start = this.pos;
// in earlier revisions, e.g. 317, this will decode until != 10 terminates the string
while (this.data[this.pos++] != 0) {}
int length = this.pos - start - 1;
return length == 0 ? "" : Cp1252Utils.decode(this.data, start, length);
}
public final String fastgjstr() {
if (this.data[this.pos] == 0) {
this.pos++;
return null;
} else {
return this.gjstr();
}
}
public final String gjstr2() {
byte version = this.data[this.pos++];
if (version != 0) {
throw new IllegalStateException("Bad version number in gjstr2");
}
int start = this.pos;
while (this.data[this.pos++] != 0) {}
int length = this.pos - start - 1;
return length == 0 ? "" : Cp1252.decode(this.data, start, length);
}
public final int gSmart1or2() {
int value = this.data[this.pos] & 0xFF;
return value < 128 ? this.g1() : this.g2() - 32768;
}
public final int gSmart1or2s() {
int value = this.data[this.pos] & 0xFF;
return value < 128 ? this.g1() - 64 : this.g2() - 49152;
}
public final int gSmart2or4null() {
if (this.data[this.pos] < 0) {
return this.g4() & Integer.MAX_VALUE;
} else {
int value = this.g2();
return value == 32767 ? -1 : value;
}
}
public final int gExtended1or2() {
int value = 0;
int remainder;
for (remainder = this.gSmart1or2(); remainder == 32767; remainder = this.gSmart1or2()) {
value += 32767;
}
return value + remainder;
}
public final int gVarInt() {
byte value = this.data[this.pos++];
int remainder = 0;
while (value < 0) {
remainder = (remainder | value & 0x7F) << 7;
value = this.data[this.pos++];
}
return value | remainder;
}
public final long gVarLong(int bytes) {
int read = bytes - 1;
if (read < 0 || read > 7) {
throw new IllegalArgumentException();
}
int bits = read * 8;
long result = 0L;
while (bits >= 0) {
result |= ((long) this.data[this.pos++] & 0xFFL) << bits;
bits -= 8;
}
return result;
}
put Methods
//todo
public final void pdata(int offset, int length, byte[] dest) {
for (int i = offset; i < length + offset; i++) {
dest[i] = this.data[this.pos++];
}
}
alt Methods
These buffer methods are used to obfuscate the network protocol - there up to 3 alt types, but their obfuscation process can strip out unused methods so there is a chance not all of them are in a specific revision.
You won't find these used in the cache.
get
public final int g1_alt1() {
return this.data[this.pos++] - 128 & 0xFF;
}
public final int g1_alt2() {
return -this.data[this.pos++] & 0xFF;
}
public final int g1_alt3() {
return 128 - this.data[this.pos++] & 0xFF;
}
public final byte g1b_alt1() {
return (byte) (this.data[this.pos++] - 128);
}
public final byte g1b_alt2() {
return (byte) -this.data[this.pos++];
}
public final byte g1b_alt3() {
return (byte) (128 - this.data[this.pos++]);
}
public final int g2_alt2() {
this.pos += 2;
return ((this.data[this.pos - 2] & 0xFF) << 8) + (this.data[this.pos - 1] - 128 & 0xFF);
}
public final int g2_alt3() {
this.pos += 2;
return ((this.data[this.pos - 1] & 0xFF) << 8) + (this.data[this.pos - 2] - 128 & 0xFF);
}
public final int g3_alt1() {
this.pos += 3;
return ((this.data[this.pos - 3] & 0xFF) << 16) + ((this.data[this.pos - 1] & 0xFF) << 8) + (this.data[this.pos - 2] & 0xFF);
}
public final int g4_alt1() {
this.pos += 4;
return ((this.data[this.pos - 1] & 0xFF) << 24) + ((this.data[this.pos - 2] & 0xFF) << 16) + ((this.data[this.pos - 3] & 0xFF) << 8) + (this.data[this.pos - 4] & 0xFF);
}
public final int g4_alt2() {
this.pos += 4;
return ((this.data[this.pos - 2] & 0xFF) << 24) + ((this.data[this.pos - 1] & 0xFF) << 16) + ((this.data[this.pos - 4] & 0xFF) << 8) + (this.data[this.pos - 3] & 0xFF);
}
public final int g4_alt3() {
this.pos += 4;
return ((this.data[this.pos - 3] & 0xFF) << 24) + ((this.data[this.pos - 4] & 0xFF) << 16) + ((this.data[this.pos - 1] & 0xFF) << 8) + (this.data[this.pos - 2] & 0xFF);
}
put
public final void p1_alt1(int value) {
this.data[this.pos++] = (byte) (value + 128);
}
public final void p1_alt2(int value) {
this.data[this.pos++] = (byte) -value;
}
public final void p1_alt3(int value) {
this.data[this.pos++] = (byte) (128 - value);
}
public final void p2_alt1(int value) {
this.data[this.pos++] = (byte) value;
this.data[this.pos++] = (byte) (value >> 8);
}
public final void p2_alt2(int value) {
this.data[this.pos++] = (byte) (value >> 8);
this.data[this.pos++] = (byte) (value + 128);
}
public final void p2_alt3(int value) {
this.data[this.pos++] = (byte) (value + 128);
this.data[this.pos++] = (byte) (value >> 8);
}
public final void p4_alt1(int value) {
this.data[this.pos++] = (byte) value;
this.data[this.pos++] = (byte) (value >> 8);
this.data[this.pos++] = (byte) (value >> 16);
this.data[this.pos++] = (byte) (value >> 24);
}
public final void p4_alt2(int value) {
this.data[this.pos++] = (byte) (value >> 8);
this.data[this.pos++] = (byte) value;
this.data[this.pos++] = (byte) (value >> 24);
this.data[this.pos++] = (byte) (value >> 16);
}
public final void p4_alt3(int value) {
this.data[this.pos++] = (byte) (value >> 16);
this.data[this.pos++] = (byte) (value >> 24);
this.data[this.pos++] = (byte) value;
this.data[this.pos++] = (byte) (value >> 8);
}
Utility Methods
public static final int[] crctable = new int[256];
static {
for (int b = 0; b < 256; b++) {
int remainder = b;
for (int bit = 0; bit < 8; bit++) {
if ((remainder & 0x1) == 1) {
remainder = remainder >>> 1 ^ 0xEDB88320;
} else {
remainder >>>= 0x1;
}
}
crctable[b] = remainder;
}
}
public static int getcrc(byte[] src, int offset, int length) {
int checksum = -1;
for (int i = offset; i < length; i++) {
checksum = checksum >>> 8 ^ crctable[(checksum ^ src[i]) & 0xFF];
}
return ~checksum;
}
public static int getcrc(byte[] src, int length) {
return getcrc(src, 0, length);
}
public final int addcrc(int offset) {
int checksum = getcrc(this.pos, offset, this.data);
this.p4(checksum);
return checksum;
}
public final void rsaenc(BigInteger modulus, BigInteger exponent) {
int length = this.pos;
this.pos = 0;
byte[] temp = new byte[length];
this.pdata(0, length, temp);
BigInteger bigRaw = new BigInteger(temp);
BigInteger bigEnc = bigRaw.modPow(exponent, modulus);
byte[] rawEnc = bigEnc.toByteArray();
this.pos = 0;
this.p2(rawEnc.length);
this.pdata(rawEnc.length, rawEnc, 0);
}
public final void tinyenc(int[] key, int offset, int length) {
int start = this.pos;
this.pos = offset;
int blocks = (length - offset) / 8;
for (int i = 0; i < blocks; i++) {
int v0 = this.g4();
int v1 = this.g4();
int sum = 0;
int numRounds = 32;
while (numRounds-- > 0) {
v0 += v1 + (v1 << 4 ^ v1 >>> 5) ^ sum + key[sum & 0x3];
sum += 0x9E3779B9;
v1 += (v0 >>> 5 ^ v0 << 4) + v0 ^ sum + key[sum >>> 11 & 0xE8C00003];
}
this.pos -= 8;
this.p4(v0);
this.p4(v1);
}
this.pos = start;
}
public final void tinydec(int[] key) {
int blocks = this.pos / 8;
this.pos = 0;
for (int i = 0; i < blocks; i++) {
int v0 = this.g4();
int v1 = this.g4();
int sum = 0xC6EF3720;
int num_rounds = 32;
while (num_rounds-- > 0) {
v1 -= (v0 >>> 5 ^ v0 << 4) + v0 ^ sum + key[sum >>> 11 & 0x3];
sum -= 0x9E3779B9;
v0 -= sum + key[sum & 0x3] ^ v1 + (v1 << 4 ^ v1 >>> 5);
}
this.pos -= 8;
this.p4(v0);
this.p4(v1);
}
}
public final void tinydec(int[] key, int offset, int length) {
int start = this.pos;
this.pos = offset;
int blocks = (length - offset) / 8;
for (int i = 0; i < blocks; i++) {
int v0 = this.g4();
int v1 = this.g4();
int sum = 0xC6EF3720;
int num_rounds = 32;
while (num_rounds-- > 0) {
v1 -= (v0 << 4 ^ v0 >>> 5) + v0 ^ key[sum >>> 11 & 0x3] + sum;
sum -= 0x9E3779B9;
v0 -= v1 + (v1 >>> 5 ^ v1 << 4) ^ sum + key[sum & 0x3];
}
this.pos -= 8;
this.p4(v0);
this.p4(v1);
}
this.pos = start;
}