package io.netty.handler.codec.http2;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.dns.DnsRecord;
import io.netty.util.AsciiString;
import io.netty.util.ByteProcessor;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.ThrowableUtil;

/* JADX INFO: loaded from: classes.dex */
public final class HpackHuffmanDecoder {
    private static final Http2Exception EOS_DECODED;
    private static final Http2Exception INVALID_PADDING;
    private static final Node ROOT;
    private final DecoderProcessor processor;

    public static final class DecoderProcessor implements ByteProcessor {
        private byte[] bytes;
        private int current;
        private int currentBits;
        private int index;
        private final int initialCapacity;
        private Node node;
        private int symbolBits;

        public DecoderProcessor(int i2) {
            this.initialCapacity = ObjectUtil.checkPositive(i2, "initialCapacity");
        }

        private void append(int i2) {
            byte[] bArr = this.bytes;
            if (bArr.length == this.index) {
                byte[] bArr2 = new byte[bArr.length >= 1024 ? bArr.length + this.initialCapacity : bArr.length << 1];
                System.arraycopy(bArr, 0, bArr2, 0, bArr.length);
                this.bytes = bArr2;
            }
            byte[] bArr3 = this.bytes;
            int i3 = this.index;
            this.index = i3 + 1;
            bArr3[i3] = (byte) i2;
        }

        public AsciiString end() throws Http2Exception {
            while (this.currentBits > 0) {
                Node node = this.node.children[(this.current << (8 - this.currentBits)) & DnsRecord.CLASS_ANY];
                this.node = node;
                if (!node.isTerminal() || this.node.bits > this.currentBits) {
                    break;
                }
                if (this.node.symbol == 256) {
                    throw HpackHuffmanDecoder.EOS_DECODED;
                }
                this.currentBits -= this.node.bits;
                append(this.node.symbol);
                this.node = HpackHuffmanDecoder.ROOT;
                this.symbolBits = this.currentBits;
            }
            int i2 = this.symbolBits;
            int i3 = (1 << i2) - 1;
            if (i2 > 7 || (this.current & i3) != i3) {
                throw HpackHuffmanDecoder.INVALID_PADDING;
            }
            return new AsciiString(this.bytes, 0, this.index, false);
        }

        @Override // io.netty.util.ByteProcessor
        public boolean process(byte b2) throws Http2Exception {
            this.current = (b2 & 255) | (this.current << 8);
            this.currentBits += 8;
            this.symbolBits += 8;
            do {
                Node[] nodeArr = this.node.children;
                int i2 = this.current;
                int i3 = this.currentBits;
                Node node = nodeArr[(i2 >>> (i3 - 8)) & DnsRecord.CLASS_ANY];
                this.node = node;
                this.currentBits = i3 - node.bits;
                if (this.node.isTerminal()) {
                    if (this.node.symbol == 256) {
                        throw HpackHuffmanDecoder.EOS_DECODED;
                    }
                    append(this.node.symbol);
                    this.node = HpackHuffmanDecoder.ROOT;
                    this.symbolBits = this.currentBits;
                }
            } while (this.currentBits >= 8);
            return true;
        }

        public void reset() {
            this.node = HpackHuffmanDecoder.ROOT;
            this.current = 0;
            this.currentBits = 0;
            this.symbolBits = 0;
            this.bytes = new byte[this.initialCapacity];
            this.index = 0;
        }
    }

    public static final class Node {
        public static final /* synthetic */ boolean $assertionsDisabled = false;
        private final int bits;
        private final Node[] children;
        private final int symbol;

        public Node() {
            this.symbol = 0;
            this.bits = 8;
            this.children = new Node[256];
        }

        public Node(int i2, int i3) {
            this.symbol = i2;
            this.bits = i3;
            this.children = null;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public boolean isTerminal() {
            return this.children == null;
        }
    }

    static {
        Http2Error http2Error = Http2Error.COMPRESSION_ERROR;
        EOS_DECODED = (Http2Exception) ThrowableUtil.unknownStackTrace(Http2Exception.connectionError(http2Error, "HPACK - EOS Decoded", new Object[0]), HpackHuffmanDecoder.class, "decode(..)");
        INVALID_PADDING = (Http2Exception) ThrowableUtil.unknownStackTrace(Http2Exception.connectionError(http2Error, "HPACK - Invalid Padding", new Object[0]), HpackHuffmanDecoder.class, "decode(..)");
        ROOT = buildTree(HpackUtil.HUFFMAN_CODES, HpackUtil.HUFFMAN_CODE_LENGTHS);
    }

    public HpackHuffmanDecoder(int i2) {
        this.processor = new DecoderProcessor(i2);
    }

    private static Node buildTree(int[] iArr, byte[] bArr) {
        Node node = new Node();
        for (int i2 = 0; i2 < iArr.length; i2++) {
            insert(node, i2, iArr[i2], bArr[i2]);
        }
        return node;
    }

    private static void insert(Node node, int i2, int i3, byte b2) {
        while (b2 > 8) {
            if (node.isTerminal()) {
                throw new IllegalStateException("invalid Huffman code: prefix not unique");
            }
            b2 = (byte) (b2 - 8);
            int i4 = (i3 >>> b2) & DnsRecord.CLASS_ANY;
            if (node.children[i4] == null) {
                node.children[i4] = new Node();
            }
            node = node.children[i4];
        }
        Node node2 = new Node(i2, b2);
        int i5 = 8 - b2;
        int i6 = (i3 << i5) & DnsRecord.CLASS_ANY;
        int i7 = 1 << i5;
        for (int i8 = i6; i8 < i6 + i7; i8++) {
            node.children[i8] = node2;
        }
    }

    public AsciiString decode(ByteBuf byteBuf, int i2) {
        this.processor.reset();
        byteBuf.forEachByte(byteBuf.readerIndex(), i2, this.processor);
        byteBuf.skipBytes(i2);
        return this.processor.end();
    }
}
