import java.math.BigInteger; import java.nio.charset.StandardCharsets; /** * A {@link Packet} encapsulates a finite amount of data and provides methods for reading and writing to that data. * The put() and get() methods describe the type of data they use by attributes in their * suffix. The naming convention is put[type][U:unsigned][endianness][modifier]. *

* Types:
* # — the number of bytes
* Op — 1 byte which is modified by the {@link #cipher}
* Smart — 1 or 2 byte value [-16384...16383]
* USmart — 1 or 2 byte value [0...32768]
* String — {@link StandardCharsets#ISO_8859_1} encoding delimited by a newline character (\n)
*

* Endianness:
* LE — little endian (DCBA)
* ME — middle endian (CDAB)
* RME — reversed middle endian (BADC)
* Note: ME and RME are not standard, and are a form of obfuscation similar to the modifier attribute.
*

* Modifiers:
* A — put(v + 128) — get: v - 128
* C — put(-v) — get: -v
* S — put(128 - v) — get: 128 - v
*/ public class Packet extends DoublyLinkedListNode { public static final LinkedList pool0 = new LinkedList(); public static final LinkedList pool1 = new LinkedList(); public static final LinkedList pool2 = new LinkedList(); private static final int[] BITMASK = {0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 0x1ffff, 0x3ffff, 0x7ffff, 0xfffff, 0x1fffff, 0x3fffff, 0x7fffff, 0xffffff, 0x1ffffff, 0x3ffffff, 0x7ffffff, 0xfffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff, -1}; public static int poolSize0; public static int poolSize1; public static int poolSize2; public static Packet create(int sizeType) { synchronized (pool1) { Packet packet = null; if ((sizeType == 0) && (poolSize0 > 0)) { poolSize0--; packet = (Packet) pool0.method251(); } else if ((sizeType == 1) && (poolSize1 > 0)) { poolSize1--; packet = (Packet) pool1.method251(); } else if ((sizeType == 2) && (poolSize2 > 0)) { poolSize2--; packet = (Packet) pool2.method251(); } if (packet != null) { packet.position = 0; return packet; } } Packet packet = new Packet(); packet.position = 0; if (sizeType == 0) { packet.data = new byte[100]; } else if (sizeType == 1) { packet.data = new byte[5000]; } else { packet.data = new byte[30000]; } return packet; } public byte[] data; public int position; public int bitPosition; public ISAACCipher cipher; public Packet() { } public Packet(byte[] src) { data = src; position = 0; } public void putOp(int i) { data[position++] = (byte) (i + cipher.method246()); } public void putSize1(int i) { data[position - i - 1] = (byte) i; } public void put1(int i) { data[position++] = (byte) i; } public void put1C(int i) { data[position++] = (byte) (-i); } public void put1S(int j) { data[position++] = (byte) (128 - j); } public void put2(int i) { data[position++] = (byte) (i >> 8); data[position++] = (byte) i; } public void put2A(int j) { data[position++] = (byte) (j >> 8); data[position++] = (byte) (j + 128); } public void put2LE(int i) { data[position++] = (byte) i; data[position++] = (byte) (i >> 8); } public void put2LEA(int j) { data[position++] = (byte) (j + 128); data[position++] = (byte) (j >> 8); } public void put3(int i) { data[position++] = (byte) (i >> 16); data[position++] = (byte) (i >> 8); data[position++] = (byte) i; } public void put4(int i) { data[position++] = (byte) (i >> 24); data[position++] = (byte) (i >> 16); data[position++] = (byte) (i >> 8); data[position++] = (byte) i; } public void put4LE(int j) { data[position++] = (byte) j; data[position++] = (byte) (j >> 8); data[position++] = (byte) (j >> 16); data[position++] = (byte) (j >> 24); } public void put8(long l) { data[position++] = (byte) (int) (l >> 56); data[position++] = (byte) (int) (l >> 48); data[position++] = (byte) (int) (l >> 40); data[position++] = (byte) (int) (l >> 32); data[position++] = (byte) (int) (l >> 24); data[position++] = (byte) (int) (l >> 16); data[position++] = (byte) (int) (l >> 8); data[position++] = (byte) (int) l; } public void put(String s) { put(s.getBytes(StandardCharsets.ISO_8859_1)); put1('\n'); } public void put(byte[] src, int len, int off) { System.arraycopy(src, off, data, position, len); position += len; } public void put(byte[] src) { put(src, 0, src.length); } public void putA(byte[] src, int off, int len) { for (int i = (off + len) - 1; i >= off; i--) { data[position++] = (byte) (src[i] + 128); } } public byte get1() { return data[position++]; } public byte get1C() { return (byte) (-data[position++]); } public byte get1S() { return (byte) (128 - data[position++]); } public int get1U() { return data[position++] & 0xff; } public int get1UA() { return (data[position++] - 128) & 0xff; } public int getU1C() { return -data[position++] & 0xff; } public int get1US() { return (128 - data[position++]) & 0xff; } /** * Gets a 1 or 2 byte varint which has the range [-16384...16383]. * * @return the value. */ public int getSmart() { int i = data[position] & 0xff; if (i < 0x80) { return get1U() - 0x40; } else { return get2U() - 0xC000; } } /** * Gets a 1 or 2 byte varint which has the range [0...32768]. * * @return the value. */ public int getUSmart() { int i = data[position] & 0xff; if (i < 0x80) { return get1U(); } else { return get2U() - 0x7fff; } } public int get2U() { position += 2; return ((data[position - 2] & 0xff) << 8) + (data[position - 1] & 0xff); } public int get2UA() { position += 2; return ((data[position - 2] & 0xff) << 8) + ((data[position - 1] - 128) & 0xff); } public int get2ULE() { position += 2; return ((data[position - 1] & 0xff) << 8) + (data[position - 2] & 0xff); } public int get2ULEA() { position += 2; return ((data[position - 1] & 0xff) << 8) + ((data[position - 2] - 128) & 0xff); } public int get2() { position += 2; int i = ((data[position - 2] & 0xff) << 8) + (data[position - 1] & 0xff); if (i > 0x7fff) { i -= 0x10000; } return i; } public int get2LE() { position += 2; int j = ((data[position - 1] & 0xff) << 8) + (data[position - 2] & 0xff); if (j > 0x7fff) { j -= 0x10000; } return j; } public int get2LEA() { position += 2; int j = ((data[position - 1] & 0xff) << 8) + ((data[position - 2] - 128) & 0xff); if (j > 0x7fff) { j -= 0x10000; } return j; } public int get3() { position += 3; return ((data[position - 3] & 0xff) << 16) + ((data[position - 2] & 0xff) << 8) + (data[position - 1] & 0xff); } public int get4() { position += 4; return ((data[position - 4] & 0xff) << 24) + ((data[position - 3] & 0xff) << 16) + ((data[position - 2] & 0xff) << 8) + (data[position - 1] & 0xff); } public int get4ME() { position += 4; return ((data[position - 3] & 0xff) << 24) + ((data[position - 4] & 0xff) << 16) + ((data[position - 1] & 0xff) << 8) + (data[position - 2] & 0xff); } public int get4RME() { position += 4; return ((data[position - 2] & 0xff) << 24) + ((data[position - 1] & 0xff) << 16) + ((data[position - 4] & 0xff) << 8) + (data[position - 3] & 0xff); } public long get8() { long l = (long) get4() & 0xffffffffL; long l1 = (long) get4() & 0xffffffffL; return (l << 32) + l1; } public String getString() { int start = position; while (data[position++] != '\n') { } return new String(data, start, position - start - 1); } public byte[] getStringRaw() { int start = position; while (data[position++] != '\n') { } byte[] raw = new byte[position - start - 1]; for (int j = start; j < (position - 1); j++) { raw[j - start] = data[j]; } return raw; } public void get(byte[] dst, int off, int len) { System.arraycopy(data, position, dst, off, len); position += len; } public void getReversed(byte[] dst, int off, int len) { for (int i = (off + len) - 1; i >= off; i--) { dst[i] = data[position++]; } } public void accessBits() { bitPosition = position * 8; } public int getBits(int n) { int bytePos = bitPosition >> 3; int remaining = 8 - (bitPosition & 7); int value = 0; bitPosition += n; for (; n > remaining; remaining = 8) { value += (data[bytePos++] & BITMASK[remaining]) << (n - remaining); n -= remaining; } if (n == remaining) { value += data[bytePos] & BITMASK[remaining]; } else { value += (data[bytePos] >> (remaining - n)) & BITMASK[n]; } return value; } public void accessBytes() { position = (bitPosition + 7) / 8; } public void encrypt(BigInteger exponent, BigInteger modulus) { int length = position; position = 0; byte[] raw = new byte[length]; get(raw, 0, length); byte[] encrypted = new BigInteger(raw).modPow(exponent, modulus).toByteArray(); position = 0; put1(encrypted.length); put(encrypted, encrypted.length, 0); } }