Rabbit-R1/switch port/java/sources/okhttp3/internal/http2/Hpack.java
2024-05-21 17:08:36 -04:00

552 lines
32 KiB
Java

package okhttp3.internal.http2;
import io.sentry.protocol.TransactionInfo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import kotlin.Metadata;
import kotlin.collections.ArraysKt;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;
import okhttp3.internal.Util;
import okio.Buffer;
import okio.BufferedSource;
import okio.ByteString;
import okio.Okio;
import okio.Source;
/* compiled from: Hpack.kt */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000(\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010$\n\u0002\u0018\u0002\n\u0002\u0010\b\n\u0002\b\t\n\u0002\u0010\u0011\n\u0002\u0018\u0002\n\u0002\b\t\\u0002\u0018\u00002\u00020\u0001:\u0002\u0018\u0019B\u0007\b\u0002¢\u0006\u0002\u0010\u0002J\u000e\u0010\u0015\u001a\u00020\u00052\u0006\u0010\u0016\u001a\u00020\u0005J\u0014\u0010\u0017\u001a\u000e\u0012\u0004\u0012\u00020\u0005\u0012\u0004\u0012\u00020\u00060\u0004H\u0002R\u001d\u0010\u0003\u001a\u000e\u0012\u0004\u0012\u00020\u0005\u0012\u0004\u0012\u00020\u00060\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0007\u0010\bR\u000e\u0010\t\u001a\u00020\u0006X\u0082T¢\u0006\u0002\n\u0000R\u000e\u0010\n\u001a\u00020\u0006X\u0082T¢\u0006\u0002\n\u0000R\u000e\u0010\u000b\u001a\u00020\u0006X\u0082T¢\u0006\u0002\n\u0000R\u000e\u0010\f\u001a\u00020\u0006X\u0082T¢\u0006\u0002\n\u0000R\u000e\u0010\r\u001a\u00020\u0006X\u0082T¢\u0006\u0002\n\u0000R\u000e\u0010\u000e\u001a\u00020\u0006X\u0082T¢\u0006\u0002\n\u0000R\u0019\u0010\u000f\u001a\b\u0012\u0004\u0012\u00020\u00110\u0010¢\u0006\n\n\u0002\u0010\u0014\u001a\u0004\b\u0012\u0010\u0013¨\u0006\u001a"}, d2 = {"Lokhttp3/internal/http2/Hpack;", "", "()V", "NAME_TO_FIRST_INDEX", "", "Lokio/ByteString;", "", "getNAME_TO_FIRST_INDEX", "()Ljava/util/Map;", "PREFIX_4_BITS", "PREFIX_5_BITS", "PREFIX_6_BITS", "PREFIX_7_BITS", "SETTINGS_HEADER_TABLE_SIZE", "SETTINGS_HEADER_TABLE_SIZE_LIMIT", "STATIC_HEADER_TABLE", "", "Lokhttp3/internal/http2/Header;", "getSTATIC_HEADER_TABLE", "()[Lokhttp3/internal/http2/Header;", "[Lokhttp3/internal/http2/Header;", "checkLowercase", "name", "nameToFirstIndex", "Reader", "Writer", "okhttp"}, k = 1, mv = {1, 4, 0})
/* loaded from: classes3.dex */
public final class Hpack {
public static final Hpack INSTANCE;
private static final Map<ByteString, Integer> NAME_TO_FIRST_INDEX;
private static final int PREFIX_4_BITS = 15;
private static final int PREFIX_5_BITS = 31;
private static final int PREFIX_6_BITS = 63;
private static final int PREFIX_7_BITS = 127;
private static final int SETTINGS_HEADER_TABLE_SIZE = 4096;
private static final int SETTINGS_HEADER_TABLE_SIZE_LIMIT = 16384;
private static final Header[] STATIC_HEADER_TABLE;
public final Map<ByteString, Integer> getNAME_TO_FIRST_INDEX() {
return NAME_TO_FIRST_INDEX;
}
public final Header[] getSTATIC_HEADER_TABLE() {
return STATIC_HEADER_TABLE;
}
static {
Hpack hpack = new Hpack();
INSTANCE = hpack;
STATIC_HEADER_TABLE = new Header[]{new Header(Header.TARGET_AUTHORITY, ""), new Header(Header.TARGET_METHOD, "GET"), new Header(Header.TARGET_METHOD, "POST"), new Header(Header.TARGET_PATH, "/"), new Header(Header.TARGET_PATH, "/index.html"), new Header(Header.TARGET_SCHEME, "http"), new Header(Header.TARGET_SCHEME, "https"), new Header(Header.RESPONSE_STATUS, "200"), new Header(Header.RESPONSE_STATUS, "204"), new Header(Header.RESPONSE_STATUS, "206"), new Header(Header.RESPONSE_STATUS, "304"), new Header(Header.RESPONSE_STATUS, "400"), new Header(Header.RESPONSE_STATUS, "404"), new Header(Header.RESPONSE_STATUS, "500"), new Header("accept-charset", ""), new Header("accept-encoding", "gzip, deflate"), new Header("accept-language", ""), new Header("accept-ranges", ""), new Header("accept", ""), new Header("access-control-allow-origin", ""), new Header("age", ""), new Header("allow", ""), new Header("authorization", ""), new Header("cache-control", ""), new Header("content-disposition", ""), new Header("content-encoding", ""), new Header("content-language", ""), new Header("content-length", ""), new Header("content-location", ""), new Header("content-range", ""), new Header("content-type", ""), new Header("cookie", ""), new Header("date", ""), new Header("etag", ""), new Header("expect", ""), new Header("expires", ""), new Header("from", ""), new Header("host", ""), new Header("if-match", ""), new Header("if-modified-since", ""), new Header("if-none-match", ""), new Header("if-range", ""), new Header("if-unmodified-since", ""), new Header("last-modified", ""), new Header("link", ""), new Header("location", ""), new Header("max-forwards", ""), new Header("proxy-authenticate", ""), new Header("proxy-authorization", ""), new Header("range", ""), new Header("referer", ""), new Header("refresh", ""), new Header("retry-after", ""), new Header("server", ""), new Header("set-cookie", ""), new Header("strict-transport-security", ""), new Header("transfer-encoding", ""), new Header("user-agent", ""), new Header("vary", ""), new Header("via", ""), new Header("www-authenticate", "")};
NAME_TO_FIRST_INDEX = hpack.nameToFirstIndex();
}
private Hpack() {
}
/* compiled from: Hpack.kt */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000N\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\b\n\u0002\b\u0003\n\u0002\u0010\u0011\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0010!\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0006\n\u0002\u0010 \n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0002\b\r\u0018\u00002\u00020\u0001B!\b\u0007\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\b\b\u0002\u0010\u0006\u001a\u00020\u0005¢\u0006\u0002\u0010\u0007J\b\u0010\u0012\u001a\u00020\u0013H\u0002J\b\u0010\u0014\u001a\u00020\u0013H\u0002J\u0010\u0010\u0015\u001a\u00020\u00052\u0006\u0010\u0016\u001a\u00020\u0005H\u0002J\u0010\u0010\u0017\u001a\u00020\u00052\u0006\u0010\u0018\u001a\u00020\u0005H\u0002J\f\u0010\u0019\u001a\b\u0012\u0004\u0012\u00020\n0\u001aJ\u0010\u0010\u001b\u001a\u00020\u001c2\u0006\u0010\u0016\u001a\u00020\u0005H\u0002J\u0018\u0010\u001d\u001a\u00020\u00132\u0006\u0010\u0016\u001a\u00020\u00052\u0006\u0010\u001e\u001a\u00020\nH\u0002J\u0010\u0010\u001f\u001a\u00020 2\u0006\u0010\u0016\u001a\u00020\u0005H\u0002J\u0006\u0010\u0006\u001a\u00020\u0005J\b\u0010!\u001a\u00020\u0005H\u0002J\u0006\u0010\"\u001a\u00020\u001cJ\u0006\u0010#\u001a\u00020\u0013J\u0010\u0010$\u001a\u00020\u00132\u0006\u0010\u0016\u001a\u00020\u0005H\u0002J\u0016\u0010%\u001a\u00020\u00052\u0006\u0010&\u001a\u00020\u00052\u0006\u0010'\u001a\u00020\u0005J\u0010\u0010(\u001a\u00020\u00132\u0006\u0010)\u001a\u00020\u0005H\u0002J\b\u0010*\u001a\u00020\u0013H\u0002J\u0010\u0010+\u001a\u00020\u00132\u0006\u0010\u0016\u001a\u00020\u0005H\u0002J\b\u0010,\u001a\u00020\u0013H\u0002R\u001c\u0010\b\u001a\n\u0012\u0006\u0012\u0004\u0018\u00010\n0\t8\u0006@\u0006X\u0087\u000e¢\u0006\u0004\n\u0002\u0010\u000bR\u0012\u0010\f\u001a\u00020\u00058\u0006@\u0006X\u0087\u000e¢\u0006\u0002\n\u0000R\u0012\u0010\r\u001a\u00020\u00058\u0006@\u0006X\u0087\u000e¢\u0006\u0002\n\u0000R\u0014\u0010\u000e\u001a\b\u0012\u0004\u0012\u00020\n0\u000fX\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0004\u001a\u00020\u0005X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0005X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0010\u001a\u00020\u0005X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0002\u001a\u00020\u0011X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006-"}, d2 = {"Lokhttp3/internal/http2/Hpack$Reader;", "", TransactionInfo.JsonKeys.SOURCE, "Lokio/Source;", "headerTableSizeSetting", "", "maxDynamicTableByteCount", "(Lokio/Source;II)V", "dynamicTable", "", "Lokhttp3/internal/http2/Header;", "[Lokhttp3/internal/http2/Header;", "dynamicTableByteCount", "headerCount", "headerList", "", "nextHeaderIndex", "Lokio/BufferedSource;", "adjustDynamicTableByteCount", "", "clearDynamicTable", "dynamicTableIndex", "index", "evictToRecoverBytes", "bytesToRecover", "getAndResetHeaderList", "", "getName", "Lokio/ByteString;", "insertIntoDynamicTable", "entry", "isStaticHeader", "", "readByte", "readByteString", "readHeaders", "readIndexedHeader", "readInt", "firstByte", "prefixMask", "readLiteralHeaderWithIncrementalIndexingIndexedName", "nameIndex", "readLiteralHeaderWithIncrementalIndexingNewName", "readLiteralHeaderWithoutIndexingIndexedName", "readLiteralHeaderWithoutIndexingNewName", "okhttp"}, k = 1, mv = {1, 4, 0})
/* loaded from: classes3.dex */
public static final class Reader {
public Header[] dynamicTable;
public int dynamicTableByteCount;
public int headerCount;
private final List<Header> headerList;
private final int headerTableSizeSetting;
private int maxDynamicTableByteCount;
private int nextHeaderIndex;
private final BufferedSource source;
public Reader(Source source, int i) {
this(source, i, 0, 4, null);
}
private final int dynamicTableIndex(int index) {
return this.nextHeaderIndex + 1 + index;
}
/* renamed from: maxDynamicTableByteCount, reason: from getter */
public final int getMaxDynamicTableByteCount() {
return this.maxDynamicTableByteCount;
}
public Reader(Source source, int i, int i2) {
Intrinsics.checkNotNullParameter(source, "source");
this.headerTableSizeSetting = i;
this.maxDynamicTableByteCount = i2;
this.headerList = new ArrayList();
this.source = Okio.buffer(source);
this.dynamicTable = new Header[8];
this.nextHeaderIndex = r2.length - 1;
}
public /* synthetic */ Reader(Source source, int i, int i2, int i3, DefaultConstructorMarker defaultConstructorMarker) {
this(source, i, (i3 & 4) != 0 ? i : i2);
}
public final List<Header> getAndResetHeaderList() {
List<Header> list = CollectionsKt.toList(this.headerList);
this.headerList.clear();
return list;
}
private final void adjustDynamicTableByteCount() {
int i = this.maxDynamicTableByteCount;
int i2 = this.dynamicTableByteCount;
if (i < i2) {
if (i == 0) {
clearDynamicTable();
} else {
evictToRecoverBytes(i2 - i);
}
}
}
private final void clearDynamicTable() {
ArraysKt.fill$default(this.dynamicTable, (Object) null, 0, 0, 6, (Object) null);
this.nextHeaderIndex = this.dynamicTable.length - 1;
this.headerCount = 0;
this.dynamicTableByteCount = 0;
}
private final int evictToRecoverBytes(int bytesToRecover) {
int i;
int i2 = 0;
if (bytesToRecover > 0) {
int length = this.dynamicTable.length;
while (true) {
length--;
i = this.nextHeaderIndex;
if (length < i || bytesToRecover <= 0) {
break;
}
Header header = this.dynamicTable[length];
Intrinsics.checkNotNull(header);
bytesToRecover -= header.hpackSize;
this.dynamicTableByteCount -= header.hpackSize;
this.headerCount--;
i2++;
}
Header[] headerArr = this.dynamicTable;
System.arraycopy(headerArr, i + 1, headerArr, i + 1 + i2, this.headerCount);
this.nextHeaderIndex += i2;
}
return i2;
}
public final void readHeaders() throws IOException {
while (!this.source.exhausted()) {
int and = Util.and(this.source.readByte(), 255);
if (and == 128) {
throw new IOException("index == 0");
}
if ((and & 128) == 128) {
readIndexedHeader(readInt(and, 127) - 1);
} else if (and == 64) {
readLiteralHeaderWithIncrementalIndexingNewName();
} else if ((and & 64) == 64) {
readLiteralHeaderWithIncrementalIndexingIndexedName(readInt(and, 63) - 1);
} else if ((and & 32) == 32) {
int readInt = readInt(and, 31);
this.maxDynamicTableByteCount = readInt;
if (readInt < 0 || readInt > this.headerTableSizeSetting) {
throw new IOException("Invalid dynamic table size update " + this.maxDynamicTableByteCount);
}
adjustDynamicTableByteCount();
} else if (and == 16 || and == 0) {
readLiteralHeaderWithoutIndexingNewName();
} else {
readLiteralHeaderWithoutIndexingIndexedName(readInt(and, 15) - 1);
}
}
}
private final void readIndexedHeader(int index) throws IOException {
if (isStaticHeader(index)) {
this.headerList.add(Hpack.INSTANCE.getSTATIC_HEADER_TABLE()[index]);
return;
}
int dynamicTableIndex = dynamicTableIndex(index - Hpack.INSTANCE.getSTATIC_HEADER_TABLE().length);
if (dynamicTableIndex >= 0) {
Header[] headerArr = this.dynamicTable;
if (dynamicTableIndex < headerArr.length) {
List<Header> list = this.headerList;
Header header = headerArr[dynamicTableIndex];
Intrinsics.checkNotNull(header);
list.add(header);
return;
}
}
throw new IOException("Header index too large " + (index + 1));
}
private final void readLiteralHeaderWithoutIndexingIndexedName(int index) throws IOException {
this.headerList.add(new Header(getName(index), readByteString()));
}
private final void readLiteralHeaderWithoutIndexingNewName() throws IOException {
this.headerList.add(new Header(Hpack.INSTANCE.checkLowercase(readByteString()), readByteString()));
}
private final void readLiteralHeaderWithIncrementalIndexingIndexedName(int nameIndex) throws IOException {
insertIntoDynamicTable(-1, new Header(getName(nameIndex), readByteString()));
}
private final void readLiteralHeaderWithIncrementalIndexingNewName() throws IOException {
insertIntoDynamicTable(-1, new Header(Hpack.INSTANCE.checkLowercase(readByteString()), readByteString()));
}
private final ByteString getName(int index) throws IOException {
if (isStaticHeader(index)) {
return Hpack.INSTANCE.getSTATIC_HEADER_TABLE()[index].name;
}
int dynamicTableIndex = dynamicTableIndex(index - Hpack.INSTANCE.getSTATIC_HEADER_TABLE().length);
if (dynamicTableIndex >= 0) {
Header[] headerArr = this.dynamicTable;
if (dynamicTableIndex < headerArr.length) {
Header header = headerArr[dynamicTableIndex];
Intrinsics.checkNotNull(header);
return header.name;
}
}
throw new IOException("Header index too large " + (index + 1));
}
private final boolean isStaticHeader(int index) {
return index >= 0 && index <= Hpack.INSTANCE.getSTATIC_HEADER_TABLE().length - 1;
}
private final void insertIntoDynamicTable(int index, Header entry) {
this.headerList.add(entry);
int i = entry.hpackSize;
if (index != -1) {
Header header = this.dynamicTable[dynamicTableIndex(index)];
Intrinsics.checkNotNull(header);
i -= header.hpackSize;
}
int i2 = this.maxDynamicTableByteCount;
if (i > i2) {
clearDynamicTable();
return;
}
int evictToRecoverBytes = evictToRecoverBytes((this.dynamicTableByteCount + i) - i2);
if (index == -1) {
int i3 = this.headerCount + 1;
Header[] headerArr = this.dynamicTable;
if (i3 > headerArr.length) {
Header[] headerArr2 = new Header[headerArr.length * 2];
System.arraycopy(headerArr, 0, headerArr2, headerArr.length, headerArr.length);
this.nextHeaderIndex = this.dynamicTable.length - 1;
this.dynamicTable = headerArr2;
}
int i4 = this.nextHeaderIndex;
this.nextHeaderIndex = i4 - 1;
this.dynamicTable[i4] = entry;
this.headerCount++;
} else {
this.dynamicTable[index + dynamicTableIndex(index) + evictToRecoverBytes] = entry;
}
this.dynamicTableByteCount += i;
}
private final int readByte() throws IOException {
return Util.and(this.source.readByte(), 255);
}
public final int readInt(int firstByte, int prefixMask) throws IOException {
int i = firstByte & prefixMask;
if (i < prefixMask) {
return i;
}
int i2 = 0;
while (true) {
int readByte = readByte();
if ((readByte & 128) == 0) {
return prefixMask + (readByte << i2);
}
prefixMask += (readByte & 127) << i2;
i2 += 7;
}
}
public final ByteString readByteString() throws IOException {
int readByte = readByte();
boolean z = (readByte & 128) == 128;
long readInt = readInt(readByte, 127);
if (z) {
Buffer buffer = new Buffer();
Huffman.INSTANCE.decode(this.source, readInt, buffer);
return buffer.readByteString();
}
return this.source.readByteString(readInt);
}
}
private final Map<ByteString, Integer> nameToFirstIndex() {
Header[] headerArr = STATIC_HEADER_TABLE;
LinkedHashMap linkedHashMap = new LinkedHashMap(headerArr.length);
int length = headerArr.length;
for (int i = 0; i < length; i++) {
Header[] headerArr2 = STATIC_HEADER_TABLE;
if (!linkedHashMap.containsKey(headerArr2[i].name)) {
linkedHashMap.put(headerArr2[i].name, Integer.valueOf(i));
}
}
Map<ByteString, Integer> unmodifiableMap = Collections.unmodifiableMap(linkedHashMap);
Intrinsics.checkNotNullExpressionValue(unmodifiableMap, "Collections.unmodifiableMap(result)");
return unmodifiableMap;
}
/* compiled from: Hpack.kt */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000B\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000b\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0011\n\u0002\u0018\u0002\n\u0002\b\b\n\u0002\u0010\u0002\n\u0002\b\b\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010 \n\u0002\b\u0005\u0018\u00002\u00020\u0001B#\b\u0007\u0012\b\b\u0002\u0010\u0002\u001a\u00020\u0003\u0012\b\b\u0002\u0010\u0004\u001a\u00020\u0005\u0012\u0006\u0010\u0006\u001a\u00020\u0007¢\u0006\u0002\u0010\bJ\b\u0010\u0013\u001a\u00020\u0014H\u0002J\b\u0010\u0015\u001a\u00020\u0014H\u0002J\u0010\u0010\u0016\u001a\u00020\u00032\u0006\u0010\u0017\u001a\u00020\u0003H\u0002J\u0010\u0010\u0018\u001a\u00020\u00142\u0006\u0010\u0019\u001a\u00020\u000bH\u0002J\u000e\u0010\u001a\u001a\u00020\u00142\u0006\u0010\u0002\u001a\u00020\u0003J\u000e\u0010\u001b\u001a\u00020\u00142\u0006\u0010\u001c\u001a\u00020\u001dJ\u0014\u0010\u001e\u001a\u00020\u00142\f\u0010\u001f\u001a\b\u0012\u0004\u0012\u00020\u000b0 J\u001e\u0010!\u001a\u00020\u00142\u0006\u0010\"\u001a\u00020\u00032\u0006\u0010#\u001a\u00020\u00032\u0006\u0010$\u001a\u00020\u0003R\u001c\u0010\t\u001a\n\u0012\u0006\u0012\u0004\u0018\u00010\u000b0\n8\u0006@\u0006X\u0087\u000e¢\u0006\u0004\n\u0002\u0010\fR\u0012\u0010\r\u001a\u00020\u00038\u0006@\u0006X\u0087\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u000e\u001a\u00020\u0005X\u0082\u000e¢\u0006\u0002\n\u0000R\u0012\u0010\u000f\u001a\u00020\u00038\u0006@\u0006X\u0087\u000e¢\u0006\u0002\n\u0000R\u0012\u0010\u0002\u001a\u00020\u00038\u0006@\u0006X\u0087\u000e¢\u0006\u0002\n\u0000R\u0012\u0010\u0010\u001a\u00020\u00038\u0006@\u0006X\u0087\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0011\u001a\u00020\u0003X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0007X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0012\u001a\u00020\u0003X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0004\u001a\u00020\u0005X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006%"}, d2 = {"Lokhttp3/internal/http2/Hpack$Writer;", "", "headerTableSizeSetting", "", "useCompression", "", "out", "Lokio/Buffer;", "(IZLokio/Buffer;)V", "dynamicTable", "", "Lokhttp3/internal/http2/Header;", "[Lokhttp3/internal/http2/Header;", "dynamicTableByteCount", "emitDynamicTableSizeUpdate", "headerCount", "maxDynamicTableByteCount", "nextHeaderIndex", "smallestHeaderTableSizeSetting", "adjustDynamicTableByteCount", "", "clearDynamicTable", "evictToRecoverBytes", "bytesToRecover", "insertIntoDynamicTable", "entry", "resizeHeaderTable", "writeByteString", "data", "Lokio/ByteString;", "writeHeaders", "headerBlock", "", "writeInt", "value", "prefixMask", "bits", "okhttp"}, k = 1, mv = {1, 4, 0})
/* loaded from: classes3.dex */
public static final class Writer {
public Header[] dynamicTable;
public int dynamicTableByteCount;
private boolean emitDynamicTableSizeUpdate;
public int headerCount;
public int headerTableSizeSetting;
public int maxDynamicTableByteCount;
private int nextHeaderIndex;
private final Buffer out;
private int smallestHeaderTableSizeSetting;
private final boolean useCompression;
public Writer(int i, Buffer buffer) {
this(i, false, buffer, 2, null);
}
public Writer(Buffer buffer) {
this(0, false, buffer, 3, null);
}
public Writer(int i, boolean z, Buffer out) {
Intrinsics.checkNotNullParameter(out, "out");
this.headerTableSizeSetting = i;
this.useCompression = z;
this.out = out;
this.smallestHeaderTableSizeSetting = Integer.MAX_VALUE;
this.maxDynamicTableByteCount = i;
this.dynamicTable = new Header[8];
this.nextHeaderIndex = r2.length - 1;
}
public /* synthetic */ Writer(int i, boolean z, Buffer buffer, int i2, DefaultConstructorMarker defaultConstructorMarker) {
this((i2 & 1) != 0 ? 4096 : i, (i2 & 2) != 0 ? true : z, buffer);
}
private final void clearDynamicTable() {
ArraysKt.fill$default(this.dynamicTable, (Object) null, 0, 0, 6, (Object) null);
this.nextHeaderIndex = this.dynamicTable.length - 1;
this.headerCount = 0;
this.dynamicTableByteCount = 0;
}
private final int evictToRecoverBytes(int bytesToRecover) {
int i;
int i2 = 0;
if (bytesToRecover > 0) {
int length = this.dynamicTable.length;
while (true) {
length--;
i = this.nextHeaderIndex;
if (length < i || bytesToRecover <= 0) {
break;
}
Header header = this.dynamicTable[length];
Intrinsics.checkNotNull(header);
bytesToRecover -= header.hpackSize;
int i3 = this.dynamicTableByteCount;
Header header2 = this.dynamicTable[length];
Intrinsics.checkNotNull(header2);
this.dynamicTableByteCount = i3 - header2.hpackSize;
this.headerCount--;
i2++;
}
Header[] headerArr = this.dynamicTable;
System.arraycopy(headerArr, i + 1, headerArr, i + 1 + i2, this.headerCount);
Header[] headerArr2 = this.dynamicTable;
int i4 = this.nextHeaderIndex;
Arrays.fill(headerArr2, i4 + 1, i4 + 1 + i2, (Object) null);
this.nextHeaderIndex += i2;
}
return i2;
}
private final void insertIntoDynamicTable(Header entry) {
int i = entry.hpackSize;
int i2 = this.maxDynamicTableByteCount;
if (i > i2) {
clearDynamicTable();
return;
}
evictToRecoverBytes((this.dynamicTableByteCount + i) - i2);
int i3 = this.headerCount + 1;
Header[] headerArr = this.dynamicTable;
if (i3 > headerArr.length) {
Header[] headerArr2 = new Header[headerArr.length * 2];
System.arraycopy(headerArr, 0, headerArr2, headerArr.length, headerArr.length);
this.nextHeaderIndex = this.dynamicTable.length - 1;
this.dynamicTable = headerArr2;
}
int i4 = this.nextHeaderIndex;
this.nextHeaderIndex = i4 - 1;
this.dynamicTable[i4] = entry;
this.headerCount++;
this.dynamicTableByteCount += i;
}
public final void writeHeaders(List<Header> headerBlock) throws IOException {
int i;
int i2;
Intrinsics.checkNotNullParameter(headerBlock, "headerBlock");
if (this.emitDynamicTableSizeUpdate) {
int i3 = this.smallestHeaderTableSizeSetting;
if (i3 < this.maxDynamicTableByteCount) {
writeInt(i3, 31, 32);
}
this.emitDynamicTableSizeUpdate = false;
this.smallestHeaderTableSizeSetting = Integer.MAX_VALUE;
writeInt(this.maxDynamicTableByteCount, 31, 32);
}
int size = headerBlock.size();
for (int i4 = 0; i4 < size; i4++) {
Header header = headerBlock.get(i4);
ByteString asciiLowercase = header.name.toAsciiLowercase();
ByteString byteString = header.value;
Integer num = Hpack.INSTANCE.getNAME_TO_FIRST_INDEX().get(asciiLowercase);
if (num != null) {
int intValue = num.intValue();
i2 = intValue + 1;
if (2 <= i2 && 7 >= i2) {
if (Intrinsics.areEqual(Hpack.INSTANCE.getSTATIC_HEADER_TABLE()[intValue].value, byteString)) {
i = i2;
} else if (Intrinsics.areEqual(Hpack.INSTANCE.getSTATIC_HEADER_TABLE()[i2].value, byteString)) {
i2 = intValue + 2;
i = i2;
}
}
i = i2;
i2 = -1;
} else {
i = -1;
i2 = -1;
}
if (i2 == -1) {
int i5 = this.nextHeaderIndex + 1;
int length = this.dynamicTable.length;
while (true) {
if (i5 >= length) {
break;
}
Header header2 = this.dynamicTable[i5];
Intrinsics.checkNotNull(header2);
if (Intrinsics.areEqual(header2.name, asciiLowercase)) {
Header header3 = this.dynamicTable[i5];
Intrinsics.checkNotNull(header3);
if (Intrinsics.areEqual(header3.value, byteString)) {
i2 = Hpack.INSTANCE.getSTATIC_HEADER_TABLE().length + (i5 - this.nextHeaderIndex);
break;
} else if (i == -1) {
i = (i5 - this.nextHeaderIndex) + Hpack.INSTANCE.getSTATIC_HEADER_TABLE().length;
}
}
i5++;
}
}
if (i2 != -1) {
writeInt(i2, 127, 128);
} else if (i == -1) {
this.out.writeByte(64);
writeByteString(asciiLowercase);
writeByteString(byteString);
insertIntoDynamicTable(header);
} else if (asciiLowercase.startsWith(Header.PSEUDO_PREFIX) && (!Intrinsics.areEqual(Header.TARGET_AUTHORITY, asciiLowercase))) {
writeInt(i, 15, 0);
writeByteString(byteString);
} else {
writeInt(i, 63, 64);
writeByteString(byteString);
insertIntoDynamicTable(header);
}
}
}
public final void writeInt(int value, int prefixMask, int bits) {
if (value < prefixMask) {
this.out.writeByte(value | bits);
return;
}
this.out.writeByte(bits | prefixMask);
int i = value - prefixMask;
while (i >= 128) {
this.out.writeByte(128 | (i & 127));
i >>>= 7;
}
this.out.writeByte(i);
}
public final void writeByteString(ByteString data) throws IOException {
Intrinsics.checkNotNullParameter(data, "data");
if (this.useCompression && Huffman.INSTANCE.encodedLength(data) < data.size()) {
Buffer buffer = new Buffer();
Huffman.INSTANCE.encode(data, buffer);
ByteString readByteString = buffer.readByteString();
writeInt(readByteString.size(), 127, 128);
this.out.write(readByteString);
return;
}
writeInt(data.size(), 127, 0);
this.out.write(data);
}
public final void resizeHeaderTable(int headerTableSizeSetting) {
this.headerTableSizeSetting = headerTableSizeSetting;
int min = Math.min(headerTableSizeSetting, 16384);
int i = this.maxDynamicTableByteCount;
if (i == min) {
return;
}
if (min < i) {
this.smallestHeaderTableSizeSetting = Math.min(this.smallestHeaderTableSizeSetting, min);
}
this.emitDynamicTableSizeUpdate = true;
this.maxDynamicTableByteCount = min;
adjustDynamicTableByteCount();
}
private final void adjustDynamicTableByteCount() {
int i = this.maxDynamicTableByteCount;
int i2 = this.dynamicTableByteCount;
if (i < i2) {
if (i == 0) {
clearDynamicTable();
} else {
evictToRecoverBytes(i2 - i);
}
}
}
}
public final ByteString checkLowercase(ByteString name) throws IOException {
Intrinsics.checkNotNullParameter(name, "name");
int size = name.size();
for (int i = 0; i < size; i++) {
byte b = (byte) 65;
byte b2 = (byte) 90;
byte b3 = name.getByte(i);
if (b <= b3 && b2 >= b3) {
throw new IOException("PROTOCOL_ERROR response malformed: mixed case name: " + name.utf8());
}
}
return name;
}
}