package io.netty.handler.codec.http2;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.dns.DnsRecord;
import io.netty.handler.codec.http2.HpackUtil;
import io.netty.handler.codec.http2.Http2HeadersEncoder;
import io.netty.util.AsciiString;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.MathUtil;
import java.util.Arrays;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public final class HpackEncoder {
    public static final /* synthetic */ boolean $assertionsDisabled = false;
    private final byte hashMask;
    private final HeaderEntry head;
    private final HeaderEntry[] headerFields;
    private final HpackHuffmanEncoder hpackHuffmanEncoder;
    private final boolean ignoreMaxHeaderListSize;
    private long maxHeaderListSize;
    private long maxHeaderTableSize;
    private long size;

    /* JADX INFO: renamed from: io.netty.handler.codec.http2.HpackEncoder$1, reason: invalid class name */
    public static /* synthetic */ class AnonymousClass1 {
        public static final /* synthetic */ int[] $SwitchMap$io$netty$handler$codec$http2$HpackUtil$IndexType;

        static {
            HpackUtil.IndexType.values();
            int[] iArr = new int[3];
            $SwitchMap$io$netty$handler$codec$http2$HpackUtil$IndexType = iArr;
            try {
                iArr[HpackUtil.IndexType.INCREMENTAL.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$http2$HpackUtil$IndexType[HpackUtil.IndexType.NONE.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$http2$HpackUtil$IndexType[HpackUtil.IndexType.NEVER.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
        }
    }

    public static final class HeaderEntry extends HpackHeaderField {
        public HeaderEntry after;
        public HeaderEntry before;
        public int hash;
        public int index;
        public HeaderEntry next;

        public HeaderEntry(int i2, CharSequence charSequence, CharSequence charSequence2, int i3, HeaderEntry headerEntry) {
            super(charSequence, charSequence2);
            this.index = i3;
            this.hash = i2;
            this.next = headerEntry;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public void addBefore(HeaderEntry headerEntry) {
            this.after = headerEntry;
            HeaderEntry headerEntry2 = headerEntry.before;
            this.before = headerEntry2;
            headerEntry2.after = this;
            this.after.before = this;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public void remove() {
            HeaderEntry headerEntry = this.before;
            headerEntry.after = this.after;
            this.after.before = headerEntry;
            this.before = null;
            this.after = null;
            this.next = null;
        }
    }

    public HpackEncoder() {
        this(false);
    }

    public HpackEncoder(boolean z2) {
        this(z2, 16);
    }

    public HpackEncoder(boolean z2, int i2) {
        AsciiString asciiString = AsciiString.EMPTY_STRING;
        HeaderEntry headerEntry = new HeaderEntry(-1, asciiString, asciiString, Integer.MAX_VALUE, null);
        this.head = headerEntry;
        this.hpackHuffmanEncoder = new HpackHuffmanEncoder();
        this.ignoreMaxHeaderListSize = z2;
        this.maxHeaderTableSize = 4096L;
        this.maxHeaderListSize = Http2CodecUtil.DEFAULT_HEADER_LIST_SIZE;
        this.headerFields = new HeaderEntry[MathUtil.findNextPositivePowerOfTwo(Math.max(2, Math.min(i2, 128)))];
        this.hashMask = (byte) (r8.length - 1);
        headerEntry.after = headerEntry;
        headerEntry.before = headerEntry;
    }

    private void add(CharSequence charSequence, CharSequence charSequence2, long j2) {
        if (j2 > this.maxHeaderTableSize) {
            clear();
            return;
        }
        while (this.maxHeaderTableSize - this.size < j2) {
            remove();
        }
        int iHashCode = AsciiString.hashCode(charSequence);
        int iIndex = index(iHashCode);
        HeaderEntry headerEntry = new HeaderEntry(iHashCode, charSequence, charSequence2, this.head.before.index - 1, this.headerFields[iIndex]);
        this.headerFields[iIndex] = headerEntry;
        headerEntry.addBefore(this.head);
        this.size += j2;
    }

    private void clear() {
        Arrays.fill(this.headerFields, (Object) null);
        HeaderEntry headerEntry = this.head;
        headerEntry.after = headerEntry;
        headerEntry.before = headerEntry;
        this.size = 0L;
    }

    private void encodeHeader(ByteBuf byteBuf, CharSequence charSequence, CharSequence charSequence2, boolean z2, long j2) {
        int index;
        int nameIndex;
        HpackUtil.IndexType indexType;
        if (z2) {
            nameIndex = getNameIndex(charSequence);
            indexType = HpackUtil.IndexType.NEVER;
        } else {
            long j3 = this.maxHeaderTableSize;
            if (j3 == 0) {
                int index2 = HpackStaticTable.getIndex(charSequence, charSequence2);
                if (index2 != -1) {
                    encodeInteger(byteBuf, 128, 7, index2);
                    return;
                } else {
                    encodeLiteral(byteBuf, charSequence, charSequence2, HpackUtil.IndexType.NONE, HpackStaticTable.getIndex(charSequence));
                    return;
                }
            }
            if (j2 <= j3) {
                HeaderEntry entry = getEntry(charSequence, charSequence2);
                if (entry != null) {
                    index = getIndex(entry.index) + HpackStaticTable.length;
                } else {
                    index = HpackStaticTable.getIndex(charSequence, charSequence2);
                    if (index == -1) {
                        ensureCapacity(j2);
                        encodeLiteral(byteBuf, charSequence, charSequence2, HpackUtil.IndexType.INCREMENTAL, getNameIndex(charSequence));
                        add(charSequence, charSequence2, j2);
                        return;
                    }
                }
                encodeInteger(byteBuf, 128, 7, index);
                return;
            }
            nameIndex = getNameIndex(charSequence);
            indexType = HpackUtil.IndexType.NONE;
        }
        encodeLiteral(byteBuf, charSequence, charSequence2, indexType, nameIndex);
    }

    private void encodeHeadersEnforceMaxHeaderListSize(int i2, ByteBuf byteBuf, Http2Headers http2Headers, Http2HeadersEncoder.SensitivityDetector sensitivityDetector) throws Http2Exception {
        long jSizeOf = 0;
        for (Map.Entry<CharSequence, CharSequence> entry : http2Headers) {
            jSizeOf += HpackHeaderField.sizeOf(entry.getKey(), entry.getValue());
            long j2 = this.maxHeaderListSize;
            if (jSizeOf > j2) {
                Http2CodecUtil.headerListSizeExceeded(i2, j2, false);
            }
        }
        encodeHeadersIgnoreMaxHeaderListSize(byteBuf, http2Headers, sensitivityDetector);
    }

    private void encodeHeadersIgnoreMaxHeaderListSize(ByteBuf byteBuf, Http2Headers http2Headers, Http2HeadersEncoder.SensitivityDetector sensitivityDetector) {
        for (Map.Entry<CharSequence, CharSequence> entry : http2Headers) {
            CharSequence key = entry.getKey();
            CharSequence value = entry.getValue();
            encodeHeader(byteBuf, key, value, sensitivityDetector.isSensitive(key, value), HpackHeaderField.sizeOf(key, value));
        }
    }

    private static void encodeInteger(ByteBuf byteBuf, int i2, int i3, int i4) {
        encodeInteger(byteBuf, i2, i3, i4);
    }

    private static void encodeInteger(ByteBuf byteBuf, int i2, int i3, long j2) {
        int i4;
        int i5 = DnsRecord.CLASS_ANY >>> (8 - i3);
        long j3 = i5;
        if (j2 < j3) {
            i4 = (int) (((long) i2) | j2);
        } else {
            byteBuf.writeByte(i2 | i5);
            long j4 = j2 - j3;
            while (((-128) & j4) != 0) {
                byteBuf.writeByte((int) ((127 & j4) | 128));
                j4 >>>= 7;
            }
            i4 = (int) j4;
        }
        byteBuf.writeByte(i4);
    }

    private void encodeLiteral(ByteBuf byteBuf, CharSequence charSequence, CharSequence charSequence2, HpackUtil.IndexType indexType, int i2) {
        boolean z2 = i2 != -1;
        int iOrdinal = indexType.ordinal();
        if (iOrdinal == 0) {
            if (!z2) {
                i2 = 0;
            }
            encodeInteger(byteBuf, 64, 6, i2);
        } else if (iOrdinal == 1) {
            if (!z2) {
                i2 = 0;
            }
            encodeInteger(byteBuf, 0, 4, i2);
        } else {
            if (iOrdinal != 2) {
                throw new Error("should not reach here");
            }
            if (!z2) {
                i2 = 0;
            }
            encodeInteger(byteBuf, 16, 4, i2);
        }
        if (!z2) {
            encodeStringLiteral(byteBuf, charSequence);
        }
        encodeStringLiteral(byteBuf, charSequence2);
    }

    private void encodeStringLiteral(ByteBuf byteBuf, CharSequence charSequence) {
        int encodedLength = this.hpackHuffmanEncoder.getEncodedLength(charSequence);
        if (encodedLength < charSequence.length()) {
            encodeInteger(byteBuf, 128, 7, encodedLength);
            this.hpackHuffmanEncoder.encode(byteBuf, charSequence);
            return;
        }
        encodeInteger(byteBuf, 0, 7, charSequence.length());
        if (!(charSequence instanceof AsciiString)) {
            byteBuf.writeCharSequence(charSequence, CharsetUtil.ISO_8859_1);
        } else {
            AsciiString asciiString = (AsciiString) charSequence;
            byteBuf.writeBytes(asciiString.array(), asciiString.arrayOffset(), asciiString.length());
        }
    }

    private void ensureCapacity(long j2) {
        while (this.maxHeaderTableSize - this.size < j2 && length() != 0) {
            remove();
        }
    }

    private HeaderEntry getEntry(CharSequence charSequence, CharSequence charSequence2) {
        if (length() != 0 && charSequence != null && charSequence2 != null) {
            int iHashCode = AsciiString.hashCode(charSequence);
            for (HeaderEntry headerEntry = this.headerFields[index(iHashCode)]; headerEntry != null; headerEntry = headerEntry.next) {
                if (headerEntry.hash == iHashCode && (HpackUtil.equalsConstantTime(charSequence, headerEntry.name) & HpackUtil.equalsConstantTime(charSequence2, headerEntry.value)) != 0) {
                    return headerEntry;
                }
            }
        }
        return null;
    }

    private int getIndex(int i2) {
        if (i2 == -1) {
            return -1;
        }
        return (i2 - this.head.before.index) + 1;
    }

    private int getIndex(CharSequence charSequence) {
        if (length() != 0 && charSequence != null) {
            int iHashCode = AsciiString.hashCode(charSequence);
            for (HeaderEntry headerEntry = this.headerFields[index(iHashCode)]; headerEntry != null; headerEntry = headerEntry.next) {
                if (headerEntry.hash == iHashCode && HpackUtil.equalsConstantTime(charSequence, headerEntry.name) != 0) {
                    return getIndex(headerEntry.index);
                }
            }
        }
        return -1;
    }

    private int getNameIndex(CharSequence charSequence) {
        int index = HpackStaticTable.getIndex(charSequence);
        if (index != -1) {
            return index;
        }
        int index2 = getIndex(charSequence);
        return index2 >= 0 ? index2 + HpackStaticTable.length : index2;
    }

    private int index(int i2) {
        return i2 & this.hashMask;
    }

    private HpackHeaderField remove() {
        if (this.size == 0) {
            return null;
        }
        HeaderEntry headerEntry = this.head.after;
        int iIndex = index(headerEntry.hash);
        HeaderEntry headerEntry2 = this.headerFields[iIndex];
        HeaderEntry headerEntry3 = headerEntry2;
        while (headerEntry2 != null) {
            HeaderEntry headerEntry4 = headerEntry2.next;
            if (headerEntry2 == headerEntry) {
                if (headerEntry3 == headerEntry) {
                    this.headerFields[iIndex] = headerEntry4;
                } else {
                    headerEntry3.next = headerEntry4;
                }
                headerEntry.remove();
                this.size -= (long) headerEntry.size();
                return headerEntry;
            }
            headerEntry3 = headerEntry2;
            headerEntry2 = headerEntry4;
        }
        return null;
    }

    public void encodeHeaders(int i2, ByteBuf byteBuf, Http2Headers http2Headers, Http2HeadersEncoder.SensitivityDetector sensitivityDetector) throws Http2Exception {
        if (this.ignoreMaxHeaderListSize) {
            encodeHeadersIgnoreMaxHeaderListSize(byteBuf, http2Headers, sensitivityDetector);
        } else {
            encodeHeadersEnforceMaxHeaderListSize(i2, byteBuf, http2Headers, sensitivityDetector);
        }
    }

    public HpackHeaderField getHeaderField(int i2) {
        HeaderEntry headerEntry = this.head;
        while (true) {
            int i3 = i2 - 1;
            if (i2 < 0) {
                return headerEntry;
            }
            headerEntry = headerEntry.before;
            i2 = i3;
        }
    }

    public long getMaxHeaderListSize() {
        return this.maxHeaderListSize;
    }

    public long getMaxHeaderTableSize() {
        return this.maxHeaderTableSize;
    }

    public int length() {
        if (this.size == 0) {
            return 0;
        }
        HeaderEntry headerEntry = this.head;
        return (headerEntry.after.index - headerEntry.before.index) + 1;
    }

    public void setMaxHeaderListSize(long j2) throws Http2Exception {
        if (j2 < 0 || j2 > 4294967295L) {
            throw Http2Exception.connectionError(Http2Error.PROTOCOL_ERROR, "Header List Size must be >= %d and <= %d but was %d", 0L, 4294967295L, Long.valueOf(j2));
        }
        this.maxHeaderListSize = j2;
    }

    public void setMaxHeaderTableSize(ByteBuf byteBuf, long j2) throws Http2Exception {
        if (j2 < 0 || j2 > 4294967295L) {
            throw Http2Exception.connectionError(Http2Error.PROTOCOL_ERROR, "Header Table Size must be >= %d and <= %d but was %d", 0L, 4294967295L, Long.valueOf(j2));
        }
        if (this.maxHeaderTableSize == j2) {
            return;
        }
        this.maxHeaderTableSize = j2;
        ensureCapacity(0L);
        encodeInteger(byteBuf, 32, 5, j2);
    }

    public long size() {
        return this.size;
    }
}
