The (extremely) hard way of extracting data…

Anybody familiar with Java will have a good laugh at this.

Today I was messing with Minecraft‘s protocol. The client sends a login packet with a crapload of spaces that looks like this:

07 PlayerName            asdfpasswordstring               0

Now, to authenticate a player, you need to extract the PlayerName and password hash (asdfpasswordstring above. :P ) from the horde of spaces.

Well, I started fiddling and came up with this after a little while (omitting the socket assigner, etc.):

char[] charbuffer = new char[135];

sin.read(charbuffer, 0, 135);
sout.println("Bye.");

System.out.println(socket.getInetAddress().toString() + " disconnected.");

System.out.println("Got following data: (total chars: " + charbuffer.length + ")");

System.out.print((int)charbuffer[0]);
System.out.print((int)charbuffer[1]);

System.out.println("Compressing array..");

String compressedArray = "";

for(int iii = 2; iii < (charbuffer.length - 2); iii++)
{
	compressedArray = compressedArray + charbuffer[iii];
}

System.out.println("Compress succeeded.\nSplitting compressed array into new array..");

String[] clutteredArray = compressedArray.split(" ");

System.out.println("Split succeeded.\nArray length: " + clutteredArray.length);

System.out.println("Attempting removal of spaces from cluttered array.");

String[] playerInfo = new String[2];

int writeDex = 0;

for(int iii = 0; iii < clutteredArray.length; iii++)
{
	if(!(clutteredArray[iii].equals("")) && writeDex < 2)
	{
		System.out.println("Found data.");

		playerInfo[writeDex] = clutteredArray[iii];
		writeDex++;
	}
}

System.out.println("Data extracted from cluttered array. New array length: " + playerInfo.length + "\nContents: ");

for(int iii = 0; iii < playerInfo.length; iii++)
{
	System.out.print("[" + iii + "]" + playerInfo[iii]);
}

And then I suddenly realized I could just use substring() and replace() then did an epic facepalm.

I did this and the above became this:

char[] charbuffer = new char[134];

sin.read(charbuffer, 0, 134);
sout.println("Bye.");

System.out.println("");

System.out.println(socket.getInetAddress().toString() + " disconnected.");

System.out.println("Got total chars: " + charbuffer.length + "\nCompressing array.");

String compressedArray = "";

for(int iii = 0; iii < charbuffer.length; iii++)
{
	compressedArray = compressedArray + charbuffer[iii];
}

System.out.println("Array compressed.\nExtracting data.");

String[] playerInfo = new String[2];

playerInfo[0] = compressedArray.substring(2,63).replace(" ", "");
playerInfo[1] = compressedArray.substring(66,130).replace(" ", "");

System.out.println("Got data: " + playerInfo[0] + ", " + playerInfo[1]);

Leave a Reply

Spam protection by WP Captcha-Free