package io.grpc.okhttp.internal.framed; import com.google.common.primitives.SignedBytes; import com.google.common.primitives.UnsignedBytes; import io.beid.beidk.definitions.LivenessStatus; import io.beid.beidk.definitions.SCBHeader; import io.grpc.internal.GrpcUtil; 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 o.C14957gcv; import o.C15111ghZ; import o.C15155gij; import o.C15160gio; import o.InterfaceC15149gid; import o.gdP; import o.giD; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public final class Hpack { 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 C15155gij PSEUDO_PREFIX = C15155gij.a(":"); private static final Header[] STATIC_HEADER_TABLE = {new Header(Header.TARGET_AUTHORITY, ""), new Header(Header.TARGET_METHOD, "GET"), new Header(Header.TARGET_METHOD, GrpcUtil.HTTP_METHOD), 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, LivenessStatus.ERROR_UNKNOW), new Header("accept-charset", ""), new Header(GrpcUtil.CONTENT_ACCEPT_ENCODING, "gzip, deflate"), new Header(SCBHeader.SCB_REQ_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(GrpcUtil.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", "")}; private static final Map NAME_TO_FIRST_INDEX = nameToFirstIndex(); private Hpack() { } /* loaded from: classes6.dex */ static final class Reader { Header[] dynamicTable; int dynamicTableByteCount; int dynamicTableHeaderCount; private final List
headerList; private int headerTableSizeSetting; private int maxDynamicTableByteCount; int nextDynamicTableIndex; private final InterfaceC15149gid source; /* JADX INFO: Access modifiers changed from: package-private */ public Reader(int i, giD gid) { this(i, i, gid); } Reader(int i, int i2, giD gid) { this.headerList = new ArrayList(); this.dynamicTable = new Header[8]; this.nextDynamicTableIndex = 7; this.dynamicTableHeaderCount = 0; this.dynamicTableByteCount = 0; this.headerTableSizeSetting = i; this.maxDynamicTableByteCount = i2; this.source = C15160gio.c(gid); } /* JADX INFO: Access modifiers changed from: package-private */ public final void headerTableSizeSetting(int i) { this.headerTableSizeSetting = i; this.maxDynamicTableByteCount = i; adjustDynamicTableByteCount(); } private void adjustDynamicTableByteCount() { int i = this.maxDynamicTableByteCount; int i2 = this.dynamicTableByteCount; if (i < i2) { if (i == 0) { clearDynamicTable(); } else { evictToRecoverBytes(i2 - i); } } } private void clearDynamicTable() { Arrays.fill(this.dynamicTable, (Object) null); this.nextDynamicTableIndex = this.dynamicTable.length - 1; this.dynamicTableHeaderCount = 0; this.dynamicTableByteCount = 0; } private int evictToRecoverBytes(int i) { int i2; int i3 = 0; if (i > 0) { int length = this.dynamicTable.length; while (true) { length--; i2 = this.nextDynamicTableIndex; if (length < i2 || i <= 0) { break; } i -= this.dynamicTable[length].hpackSize; this.dynamicTableByteCount -= this.dynamicTable[length].hpackSize; this.dynamicTableHeaderCount--; i3++; } Header[] headerArr = this.dynamicTable; int i4 = i2 + 1; System.arraycopy(headerArr, i4, headerArr, i4 + i3, this.dynamicTableHeaderCount); this.nextDynamicTableIndex += i3; } return i3; } /* JADX INFO: Access modifiers changed from: package-private */ public final void readHeaders() throws IOException { while (!this.source.j()) { byte f = this.source.f(); int i = f & UnsignedBytes.MAX_VALUE; if (i == 128) { throw new IOException("index == 0"); } if ((f & 128) == 128) { readIndexedHeader(readInt(i, 127) - 1); } else if (i == 64) { readLiteralHeaderWithIncrementalIndexingNewName(); } else if ((f & SignedBytes.MAX_POWER_OF_TWO) == 64) { readLiteralHeaderWithIncrementalIndexingIndexedName(readInt(i, 63) - 1); } else if ((f & 32) == 32) { int readInt = readInt(i, 31); this.maxDynamicTableByteCount = readInt; if (readInt < 0 || readInt > this.headerTableSizeSetting) { StringBuilder sb = new StringBuilder("Invalid dynamic table size update "); sb.append(this.maxDynamicTableByteCount); throw new IOException(sb.toString()); } adjustDynamicTableByteCount(); } else if (i == 16 || i == 0) { readLiteralHeaderWithoutIndexingNewName(); } else { readLiteralHeaderWithoutIndexingIndexedName(readInt(i, 15) - 1); } } } public final List
getAndResetHeaderList() { ArrayList arrayList = new ArrayList(this.headerList); this.headerList.clear(); return arrayList; } private void readIndexedHeader(int i) throws IOException { if (isStaticHeader(i)) { this.headerList.add(Hpack.STATIC_HEADER_TABLE[i]); return; } int dynamicTableIndex = dynamicTableIndex(i - Hpack.STATIC_HEADER_TABLE.length); if (dynamicTableIndex >= 0) { Header[] headerArr = this.dynamicTable; if (dynamicTableIndex <= headerArr.length - 1) { this.headerList.add(headerArr[dynamicTableIndex]); return; } } StringBuilder sb = new StringBuilder("Header index too large "); sb.append(i + 1); throw new IOException(sb.toString()); } private void readLiteralHeaderWithoutIndexingIndexedName(int i) throws IOException { this.headerList.add(new Header(getName(i), readByteString())); } private void readLiteralHeaderWithoutIndexingNewName() throws IOException { this.headerList.add(new Header(Hpack.checkLowercase(readByteString()), readByteString())); } private void readLiteralHeaderWithIncrementalIndexingIndexedName(int i) throws IOException { insertIntoDynamicTable(-1, new Header(getName(i), readByteString())); } private void readLiteralHeaderWithIncrementalIndexingNewName() throws IOException { insertIntoDynamicTable(-1, new Header(Hpack.checkLowercase(readByteString()), readByteString())); } private C15155gij getName(int i) throws IOException { if (isStaticHeader(i)) { return Hpack.STATIC_HEADER_TABLE[i].name; } int dynamicTableIndex = dynamicTableIndex(i - Hpack.STATIC_HEADER_TABLE.length); if (dynamicTableIndex >= 0) { Header[] headerArr = this.dynamicTable; if (dynamicTableIndex < headerArr.length) { return headerArr[dynamicTableIndex].name; } } StringBuilder sb = new StringBuilder("Header index too large "); sb.append(i + 1); throw new IOException(sb.toString()); } private boolean isStaticHeader(int i) { return i >= 0 && i <= Hpack.STATIC_HEADER_TABLE.length - 1; } private void insertIntoDynamicTable(int i, Header header) { this.headerList.add(header); int i2 = header.hpackSize; if (i != -1) { i2 -= this.dynamicTable[dynamicTableIndex(i)].hpackSize; } int i3 = this.maxDynamicTableByteCount; if (i2 > i3) { clearDynamicTable(); return; } int evictToRecoverBytes = evictToRecoverBytes((this.dynamicTableByteCount + i2) - i3); if (i == -1) { int i4 = this.dynamicTableHeaderCount; Header[] headerArr = this.dynamicTable; if (i4 + 1 > headerArr.length) { Header[] headerArr2 = new Header[headerArr.length << 1]; System.arraycopy(headerArr, 0, headerArr2, headerArr.length, headerArr.length); this.nextDynamicTableIndex = this.dynamicTable.length - 1; this.dynamicTable = headerArr2; } int i5 = this.nextDynamicTableIndex; this.nextDynamicTableIndex = i5 - 1; this.dynamicTable[i5] = header; this.dynamicTableHeaderCount++; } else { this.dynamicTable[i + dynamicTableIndex(i) + evictToRecoverBytes] = header; } this.dynamicTableByteCount += i2; } private int readByte() throws IOException { return this.source.f() & UnsignedBytes.MAX_VALUE; } final int readInt(int i, int i2) throws IOException { int i3 = i & i2; if (i3 < i2) { return i3; } int i4 = 0; while (true) { int readByte = readByte(); if ((readByte & 128) == 0) { return i2 + (readByte << i4); } i2 += (readByte & 127) << i4; i4 += 7; } } final C15155gij readByteString() throws IOException { int readByte = readByte(); boolean z = (readByte & 128) == 128; int readInt = readInt(readByte, 127); if (z) { return C15155gij.e(Huffman.get().decode(this.source.e(readInt))); } return this.source.c(readInt); } final int maxDynamicTableByteCount() { return this.maxDynamicTableByteCount; } private int dynamicTableIndex(int i) { return this.nextDynamicTableIndex + 1 + i; } } private static Map nameToFirstIndex() { LinkedHashMap linkedHashMap = new LinkedHashMap(STATIC_HEADER_TABLE.length); int i = 0; while (true) { Header[] headerArr = STATIC_HEADER_TABLE; if (i < headerArr.length) { if (!linkedHashMap.containsKey(headerArr[i].name)) { linkedHashMap.put(headerArr[i].name, Integer.valueOf(i)); } i++; } else { return Collections.unmodifiableMap(linkedHashMap); } } } /* loaded from: classes6.dex */ static final class Writer { Header[] dynamicTable; private int dynamicTableByteCount; int dynamicTableHeaderCount; private boolean emitDynamicTableSizeUpdate; int headerTableSizeSetting; private int maxDynamicTableByteCount; private int nextDynamicTableIndex; private final C15111ghZ out; private int smallestHeaderTableSizeSetting; private boolean useCompression; /* JADX INFO: Access modifiers changed from: package-private */ public Writer(C15111ghZ c15111ghZ) { this(4096, false, c15111ghZ); } Writer(int i, boolean z, C15111ghZ c15111ghZ) { this.smallestHeaderTableSizeSetting = Integer.MAX_VALUE; this.dynamicTable = new Header[8]; this.nextDynamicTableIndex = 7; this.headerTableSizeSetting = i; this.maxDynamicTableByteCount = i; this.useCompression = z; this.out = c15111ghZ; } /* JADX INFO: Access modifiers changed from: package-private */ public final void writeHeaders(List
list) throws IOException { int i; int i2; 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 = list.size(); for (int i4 = 0; i4 < size; i4++) { Header header = list.get(i4); C15155gij g = header.name.g(); C15155gij c15155gij = header.value; Integer num = (Integer) Hpack.NAME_TO_FIRST_INDEX.get(g); if (num != null) { int intValue = num.intValue(); i2 = intValue + 1; if (i2 >= 2 && i2 <= 7) { if (Hpack.STATIC_HEADER_TABLE[intValue].value.equals(c15155gij)) { i = i2; } else if (Hpack.STATIC_HEADER_TABLE[i2].value.equals(c15155gij)) { i = intValue + 2; } } i = -1; } else { i = -1; i2 = -1; } if (i == -1) { int i5 = this.nextDynamicTableIndex; while (true) { i5++; Header[] headerArr = this.dynamicTable; if (i5 >= headerArr.length) { break; } if (headerArr[i5].name.equals(g)) { if (this.dynamicTable[i5].value.equals(c15155gij)) { i = Hpack.STATIC_HEADER_TABLE.length + (i5 - this.nextDynamicTableIndex); break; } else if (i2 == -1) { i2 = (i5 - this.nextDynamicTableIndex) + Hpack.STATIC_HEADER_TABLE.length; } } } } if (i != -1) { writeInt(i, 127, 128); } else if (i2 == -1) { this.out.b(64); writeByteString(g); writeByteString(c15155gij); insertIntoDynamicTable(header); } else { C15155gij c15155gij2 = Hpack.PSEUDO_PREFIX; C14957gcv.e(c15155gij2, ""); if (g.d(c15155gij2, 0, c15155gij2.a()) && !Header.TARGET_AUTHORITY.equals(g)) { writeInt(i2, 15, 0); writeByteString(c15155gij); } else { writeInt(i2, 63, 64); writeByteString(c15155gij); insertIntoDynamicTable(header); } } } } final void writeInt(int i, int i2, int i3) throws IOException { if (i < i2) { this.out.b(i | i3); return; } this.out.b(i3 | i2); int i4 = i - i2; while (i4 >= 128) { this.out.b(128 | (i4 & 127)); i4 >>>= 7; } this.out.b(i4); } final void writeByteString(C15155gij c15155gij) throws IOException { if (this.useCompression && Huffman.get().encodedLength(c15155gij.j()) < c15155gij.a()) { C15111ghZ c15111ghZ = new C15111ghZ(); Huffman.get().encode(c15155gij.j(), new C15111ghZ.Sts(c15111ghZ)); C15155gij c = c15111ghZ.c(c15111ghZ.c); writeInt(c.a(), 127, 128); C15111ghZ c15111ghZ2 = this.out; C14957gcv.e(c, ""); c.a(c15111ghZ2, c.a()); return; } writeInt(c15155gij.a(), 127, 0); C15111ghZ c15111ghZ3 = this.out; C14957gcv.e(c15155gij, ""); c15155gij.a(c15111ghZ3, c15155gij.a()); } private void clearDynamicTable() { Arrays.fill(this.dynamicTable, (Object) null); this.nextDynamicTableIndex = this.dynamicTable.length - 1; this.dynamicTableHeaderCount = 0; this.dynamicTableByteCount = 0; } private int evictToRecoverBytes(int i) { int i2; int i3 = 0; if (i > 0) { int length = this.dynamicTable.length; while (true) { length--; i2 = this.nextDynamicTableIndex; if (length < i2 || i <= 0) { break; } i -= this.dynamicTable[length].hpackSize; this.dynamicTableByteCount -= this.dynamicTable[length].hpackSize; this.dynamicTableHeaderCount--; i3++; } Header[] headerArr = this.dynamicTable; int i4 = i2 + 1; System.arraycopy(headerArr, i4, headerArr, i4 + i3, this.dynamicTableHeaderCount); this.nextDynamicTableIndex += i3; } return i3; } private void insertIntoDynamicTable(Header header) { int i = header.hpackSize; int i2 = this.maxDynamicTableByteCount; if (i > i2) { clearDynamicTable(); return; } evictToRecoverBytes((this.dynamicTableByteCount + i) - i2); int i3 = this.dynamicTableHeaderCount; Header[] headerArr = this.dynamicTable; if (i3 + 1 > headerArr.length) { Header[] headerArr2 = new Header[headerArr.length << 1]; System.arraycopy(headerArr, 0, headerArr2, headerArr.length, headerArr.length); this.nextDynamicTableIndex = this.dynamicTable.length - 1; this.dynamicTable = headerArr2; } int i4 = this.nextDynamicTableIndex; this.nextDynamicTableIndex = i4 - 1; this.dynamicTable[i4] = header; this.dynamicTableHeaderCount++; this.dynamicTableByteCount += i; } final void resizeHeaderTable(int i) { this.headerTableSizeSetting = i; int min = Math.min(i, 16384); int i2 = this.maxDynamicTableByteCount; if (i2 == min) { return; } if (min < i2) { this.smallestHeaderTableSizeSetting = Math.min(this.smallestHeaderTableSizeSetting, min); } this.emitDynamicTableSizeUpdate = true; this.maxDynamicTableByteCount = min; adjustDynamicTableByteCount(); } private void adjustDynamicTableByteCount() { int i = this.maxDynamicTableByteCount; int i2 = this.dynamicTableByteCount; if (i < i2) { if (i == 0) { clearDynamicTable(); } else { evictToRecoverBytes(i2 - i); } } } final int maxDynamicTableByteCount() { return this.maxDynamicTableByteCount; } } /* JADX INFO: Access modifiers changed from: private */ public static C15155gij checkLowercase(C15155gij c15155gij) throws IOException { int a = c15155gij.a(); for (int i = 0; i < a; i++) { byte c = c15155gij.c(i); if (c >= 65 && c <= 90) { StringBuilder sb = new StringBuilder("PROTOCOL_ERROR response malformed: mixed case name: "); String str = c15155gij.d; if (str == null) { byte[] b = c15155gij.b(); C14957gcv.e(b, ""); String str2 = new String(b, gdP.a); c15155gij.d = str2; str = str2; } sb.append(str); throw new IOException(sb.toString()); } } return c15155gij; } }