<commit_before>package protocolsupport.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.CorruptedFrameException;
import io.netty.util.ReferenceCountUtil;

import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.EnumMap;
import java.util.Timer;
import java.util.TimerTask;

import protocolsupport.injector.ProtocolLibFixer;
import protocolsupport.protocol.IPipeLineBuilder.DecoderEncoderTuple;
import protocolsupport.protocol.storage.ProtocolStorage;
import protocolsupport.protocol.storage.ProtocolStorage.ProtocolVersion;

public class InitialPacketDecoder extends ChannelInboundHandlerAdapter {

	private final Timer pingTimeout = new Timer();

	private ByteBuf receivedData = Unpooled.buffer();

	@Override
	public void channelRead(final ChannelHandlerContext ctx, final Object inputObj) throws Exception {
		try {
			final ByteBuf input = (ByteBuf) inputObj;
			if (!input.isReadable()) {
				return;
			}
			receivedData.writeBytes(input.readBytes(input.readableBytes()));
			//detect protocol
			ProtocolVersion handshakeversion = ProtocolVersion.UNKNOWN;
			//reset reader index to 0
			receivedData.readerIndex(0);
			int firstbyte = receivedData.readUnsignedByte();
			switch (firstbyte) {
				case 0xFE: { //1.6 ping or 1.5 ping (should we check if FE is actually a part of a varint length?)
					try {
						if (receivedData.readUnsignedByte() == 1) {
							if (receivedData.readableBytes() == 0) {
								//1.5.2 or maybe we still didn't receive it all
								pingTimeout.schedule(new TimerTask() {
									@Override
									public void run() {
										try {
											SocketAddress remoteAddress = ctx.channel().remoteAddress();
											if (ProtocolStorage.getVersion(remoteAddress) == ProtocolVersion.UNKNOWN) {
												setProtocol(ctx, receivedData, ProtocolVersion.MINECRAFT_1_5_2);
											}
										} catch (Throwable t) {
											t.printStackTrace();
											ctx.channel().close();
										}
									}
								}, 500);
							} else if (
								(receivedData.readUnsignedByte() == 0xFA) &&
								"MC|PingHost".equals(new String(receivedData.readBytes(receivedData.readUnsignedShort() * 2).array(), StandardCharsets.UTF_16BE))
							) { //1.6.*
								receivedData.readUnsignedShort();
								handshakeversion = ProtocolVersion.fromId(receivedData.readUnsignedByte());
							}
						}
					} catch (IndexOutOfBoundsException ex) {
					}
					break;
				}
				case 0x02: { //1.6 or 1.5.2 handshake
					try {
						handshakeversion = ProtocolVersion.fromId(receivedData.readUnsignedByte());
					} catch (IndexOutOfBoundsException ex) {
					}
					break;
				}
				default: { //1.7 or 1.8 handshake
					receivedData.readerIndex(0);
					ByteBuf data = getVarIntPrefixedData(receivedData);
					if (data != null) {<commit_msg>Don't wait on 1.5.2 ping<commit_after>package protocolsupport.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.CorruptedFrameException;
import io.netty.util.ReferenceCountUtil;

import java.nio.charset.StandardCharsets;
import java.util.EnumMap;

import protocolsupport.injector.ProtocolLibFixer;
import protocolsupport.protocol.IPipeLineBuilder.DecoderEncoderTuple;
import protocolsupport.protocol.storage.ProtocolStorage;
import protocolsupport.protocol.storage.ProtocolStorage.ProtocolVersion;

public class InitialPacketDecoder extends ChannelInboundHandlerAdapter {

	private ByteBuf receivedData = Unpooled.buffer();

	@Override
	public void channelRead(final ChannelHandlerContext ctx, final Object inputObj) throws Exception {
		try {
			final ByteBuf input = (ByteBuf) inputObj;
			if (!input.isReadable()) {
				return;
			}
			receivedData.writeBytes(input.readBytes(input.readableBytes()));
			//detect protocol
			ProtocolVersion handshakeversion = ProtocolVersion.UNKNOWN;
			//reset reader index to 0
			receivedData.readerIndex(0);
			int firstbyte = receivedData.readUnsignedByte();
			switch (firstbyte) {
				case 0xFE: { //1.6 ping or 1.5 ping (should we check if FE is actually a part of a varint length?)
					try {
						if (receivedData.readUnsignedByte() == 1) {
							if (receivedData.readableBytes() == 0) { //1.5.2
								handshakeversion = ProtocolVersion.MINECRAFT_1_5_2;
							} else if (
								(receivedData.readUnsignedByte() == 0xFA) &&
								"MC|PingHost".equals(new String(receivedData.readBytes(receivedData.readUnsignedShort() * 2).array(), StandardCharsets.UTF_16BE))
							) { //1.6.*
								receivedData.readUnsignedShort();
								handshakeversion = ProtocolVersion.fromId(receivedData.readUnsignedByte());
							}
						}
					} catch (IndexOutOfBoundsException ex) {
					}
					break;
				}
				case 0x02: { //1.6 or 1.5.2 handshake
					try {
						handshakeversion = ProtocolVersion.fromId(receivedData.readUnsignedByte());
					} catch (IndexOutOfBoundsException ex) {
					}
					break;
				}
				default: { //1.7 or 1.8 handshake
					receivedData.readerIndex(0);
					ByteBuf data = getVarIntPrefixedData(receivedData);
					if (data != null) {
{'fraction_non_alphanumeric': 0.10324152145400037, 'fraction_numerical': 0.013865467491099868, 'mean_word_length': 3.5819742489270388, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3628458', 'n_tokens_mistral': 2045, 'n_tokens_neox': 1551, 'n_words': 330}