package packets;

import java.nio.ByteBuffer;
import java.util.Arrays;

import conquerServer.PasswordCrypter;

public abstract class IncommingPacket extends Packet {
	protected byte[] data;

	public IncommingPacket(short packetSize, PacketType type) {
		super(packetSize, type);
	}
	
	public IncommingPacket(short packetSize, PacketType type, byte[] data) {
		super(packetSize, type);
		this.receive(data);
	}
	
	public void receive(byte[] data) {
		this.data = data;
	}
	
	protected int getInt(int offset, int length) {
		return ByteBuffer.wrap(Arrays.copyOfRange(data, offset, offset + length)).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
	}

	protected short getShort(int offset, int length) {
		return ByteBuffer.wrap(Arrays.copyOfRange(data, offset, offset + length)).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
	}
	
	/**
	 * Function to fetch a String from a package
	 * @param offset
	 * @param length
	 * @return 
	 */
	protected String getString(int offset, int length) {
		char[] output = new char[length];
		for ( int i = 0; i < length; i++ )
			if ( data[i+offset] != 0 )
				output[i] = (char) data[i+offset];
		return new String(output);
	}

	/**
	 * Function to fetch and decrypt the password from the Auth package
	 * @param {int} offset at which the password is at
	 * @return {String} password
	 */
	protected String getPassword(int offset) {
		// Convert split 16 bytes into 4 longs 
		long[] encrypted = new long[4];
		for ( int i = 0; i < 4; i++ )
			for ( int j = 0; j < 4; j++ )
				encrypted[i] += ((long) data[20+i*4+j] & 0xffL) << ( 8 * j);
		// Decrypt password
		PasswordCrypter.decrypt(encrypted);
		// Convert decrypted long[4] array to chars
		char[] decrypted = new char[16];
		for ( int i = 0; i < 4; i++ )
			for ( int j = 0; j < 4; j++ )
				if ((encrypted[i] >> ( 4 - j - 1 << 3) & 0xFF) != 0) 
					decrypted[i*4+3-j] = (char) (encrypted[i] >> ( 4 - j - 1 << 3) & 0xFF);
		// Put char[] to string
		return new String(decrypted);
	}
}

@@ -42,16 +42,44 @@
 uffer;%0D%0A
+import java.nio.ByteOrder;%0D%0A
 import j
@@ -1447,16 +1447,95 @@
 set) %7B%0D%0A
+%09%09// String builder for our output%0D%0A%09%09StringBuilder sb = new StringBuilder();%0D%0A
 %09%09// Con
@@ -1708,10 +1708,14 @@
 ata%5B
-20
+offset
 +i*4
@@ -1728,16 +1728,14 @@
 0xff
-L
 ) %3C%3C (
- 
 8 * 
@@ -1748,10 +1748,10 @@
 %09// 
-De
+En
 cryp
@@ -1814,16 +1814,18 @@
 Convert 
+4 
 decrypte
@@ -1834,66 +1834,57 @@
 long
-%5B4%5D array to chars%0D%0A%09%09char%5B%5D decrypted = new char%5B16%5D;%0D%0A%09%09
+s back to chars and put them in char%5B%5D%0D%0A%09%09longs: 
 for 
@@ -1900,32 +1900,34 @@
  0; i %3C 4; i++ )
+ %7B
 %0D%0A%09%09%09for ( int j
@@ -1948,165 +1948,247 @@
 ++ )
+ %7B
 %0D%0A%09%09%09%09
-if ((encrypted%5Bi%5D %3E%3E ( 4 - j - 1 %3C%3C 3) & 0xFF) !
+long singleByte = encrypted%5Bi%5D %3E%3E (j %3C%3C 3) & 0xff;%0D%0A%09%09%09%09// Look for end of string%0D%0A%09%09%09%09if(singleByte =
 = 0
-)
  
+)
 %0D%0A%09%09%09%09%09
-decrypted%5Bi*4+3-j%5D = (char) (encrypted%5Bi%5D %3E%3E ( 4 - j - 1 %3C%3C 3) & 0xFF);%0D%0A%09%09// Put char%5B%5D to
+break longs;%0D%0A%09%09%09%09// Else, append char to string%0D%0A%09%09%09%09sb.append((char) singleByte);%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09// Return decrypted
  str
@@ -2205,28 +2205,20 @@
 urn 
-new 
+sb.to
 String(
-decrypted
 );%0D%0A

Fix overflow and optimization password decryption