| 1 | package net.minecraft.network;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import io.netty.channel.ChannelHandlerContext;
|
| 5 | import io.netty.handler.codec.ByteToMessageDecoder;
|
| 6 | import io.netty.handler.codec.DecoderException;
|
| 7 | import java.nio.ByteBuffer;
|
| 8 | import java.util.List;
|
| 9 | import java.util.zip.DataFormatException;
|
| 10 | import java.util.zip.Inflater;
|
| 11 |
|
| 12 | public class CompressionDecoder extends ByteToMessageDecoder {
|
| 13 | public static final int MAXIMUM_COMPRESSED_LENGTH = 2097152;
|
| 14 | public static final int MAXIMUM_UNCOMPRESSED_LENGTH = 8388608;
|
| 15 | private final Inflater inflater;
|
| 16 | private int threshold;
|
| 17 | private boolean validateDecompressed;
|
| 18 |
|
| 19 | public CompressionDecoder(final int threshold, final boolean validateDecompressed) {
|
| 20 | this.threshold = threshold;
|
| 21 | this.validateDecompressed = validateDecompressed;
|
| 22 | this.inflater = new Inflater();
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws Exception {
|
| 27 | int uncompressedLength = VarInt.read(in);
|
| 28 | if (uncompressedLength == 0) {
|
| 29 | out.add(in.readBytes(in.readableBytes()));
|
| 30 | } else {
|
| 31 | if (this.validateDecompressed) {
|
| 32 | if (uncompressedLength < this.threshold) {
|
| 33 | throw new DecoderException("Badly compressed packet - size of " + uncompressedLength + " is below server threshold of " + this.threshold);
|
| 34 | }
|
| 35 |
|
| 36 | if (uncompressedLength > 8388608) {
|
| 37 | throw new DecoderException("Badly compressed packet - size of " + uncompressedLength + " is larger than protocol maximum of 8388608");
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | this.setupInflaterInput(in);
|
| 42 | ByteBuf output = this.inflate(ctx, uncompressedLength);
|
| 43 | this.inflater.reset();
|
| 44 | out.add(output);
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | private void setupInflaterInput(final ByteBuf in) {
|
| 49 | ByteBuffer input;
|
| 50 | if (in.nioBufferCount() > 0) {
|
| 51 | input = in.nioBuffer();
|
| 52 | in.skipBytes(in.readableBytes());
|
| 53 | } else {
|
| 54 | input = ByteBuffer.allocateDirect(in.readableBytes());
|
| 55 | in.readBytes(input);
|
| 56 | input.flip();
|
| 57 | }
|
| 58 |
|
| 59 | this.inflater.setInput(input);
|
| 60 | }
|
| 61 |
|
| 62 | private ByteBuf inflate(final ChannelHandlerContext ctx, final int uncompressedLength) throws DataFormatException {
|
| 63 | ByteBuf output = ctx.alloc().directBuffer(uncompressedLength);
|
| 64 |
|
| 65 | try {
|
| 66 | ByteBuffer nioBuffer = output.internalNioBuffer(0, uncompressedLength);
|
| 67 | int pos = nioBuffer.position();
|
| 68 | this.inflater.inflate(nioBuffer);
|
| 69 | int actualUncompressedLength = nioBuffer.position() - pos;
|
| 70 | if (actualUncompressedLength != uncompressedLength) {
|
| 71 | throw new DecoderException(
|
| 72 | "Badly compressed packet - actual length of uncompressed payload " + actualUncompressedLength + " is does not match declared size " + uncompressedLength
|
| 73 | );
|
| 74 | }
|
| 75 |
|
| 76 | output.writerIndex(output.writerIndex() + actualUncompressedLength);
|
| 77 | return output;
|
| 78 | } catch (Exception e) {
|
| 79 | output.release();
|
| 80 | throw e;
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | public void setThreshold(final int threshold, final boolean validateDecompressed) {
|
| 85 | this.threshold = threshold;
|
| 86 | this.validateDecompressed = validateDecompressed;
|
| 87 | }
|
| 88 | }
|
| 89 |
|