content: package okhttp3.internal.http2;
  
  import androidx.constraintlayout.core.motion.utils.TypedValues;
  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.HttpUrl;
  import okhttp3.internal.Util;
  import okio.Buffer;
  import okio.BufferedSource;
  import okio.ByteString;
  import okio.Okio;
  import okio.Source;
  
  @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\bÆ\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\u0006XT¢\u0006\u0002\n\u0000R\u000e\u0010\n\u001a\u00020\u0006XT¢\u0006\u0002\n\u0000R\u000e\u0010\u000b\u001a\u00020\u0006XT¢\u0006\u0002\n\u0000R\u000e\u0010\f\u001a\u00020\u0006XT¢\u0006\u0002\n\u0000R\u000e\u0010\r\u001a\u00020\u0006XT¢\u0006\u0002\n\u0000R\u000e\u0010\u000e\u001a\u00020\u0006XT¢\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})
  /* compiled from: Hpack.kt */
  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 = {new Header(Header.TARGET_AUTHORITY, (String) HttpUrl.FRAGMENT_ENCODE_SET), 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", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("accept-encoding", "gzip, deflate"), new Header("accept-language", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("accept-ranges", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("accept", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("access-control-allow-origin", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("age", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("allow", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("authorization", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("cache-control", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("content-disposition", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("content-encoding", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("content-language", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("content-length", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("content-location", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("content-range", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("content-type", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("cookie", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("date", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("etag", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("expect", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("expires", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header((String) TypedValues.TransitionType.S_FROM, (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("host", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("if-match", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("if-modified-since", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("if-none-match", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("if-range", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("if-unmodified-since", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("last-modified", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("link", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("location", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("max-forwards", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("proxy-authenticate", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("proxy-authorization", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("range", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("referer", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("refresh", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("retry-after", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("server", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("set-cookie", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("strict-transport-security", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("transfer-encoding", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("user-agent", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("vary", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("via", (String) HttpUrl.FRAGMENT_ENCODE_SET), new Header("www-authenticate", (String) HttpUrl.FRAGMENT_ENCODE_SET)};
  
      @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\u000e¢\u0006\u0004\n\u0002\u0010\u000bR\u0012\u0010\f\u001a\u00020\u00058\u0006@\u0006X\u000e¢\u0006\u0002\n\u0000R\u0012\u0010\r\u001a\u00020\u00058\u0006@\u0006X\u000e¢\u0006\u0002\n\u0000R\u0014\u0010\u000e\u001a\b\u0012\u0004\u0012\u00020\n0\u000fX\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0004\u001a\u00020\u0005X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0005X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0010\u001a\u00020\u0005X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0002\u001a\u00020\u0011X\u0004¢\u0006\u0002\n\u0000¨\u0006-"}, d2 = {"Lokhttp3/internal/http2/Hpack$Reader;", "", "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})
      /* compiled from: Hpack.kt */
      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 source2, int i) {
              this(source2, i, 0, 4, (DefaultConstructorMarker) null);
          }
  
          public Reader(Source source2, int headerTableSizeSetting2, int maxDynamicTableByteCount2) {
              Intrinsics.checkNotNullParameter(source2, "source");
              this.headerTableSizeSetting = headerTableSizeSetting2;
              this.maxDynamicTableByteCount = maxDynamicTableByteCount2;
              this.headerList = new ArrayList();
              this.source = Okio.buffer(source2);
              Header[] headerArr = new Header[8];
              this.dynamicTable = headerArr;
              this.nextHeaderIndex = headerArr.length - 1;
          }
  
          /* JADX INFO: this call moved to the top of the method (can break code semantics) */
          public /* synthetic */ Reader(Source source2, int i, int i2, int i3, DefaultConstructorMarker defaultConstructorMarker) {
              this(source2, i, (i3 & 4) != 0 ? i : i2);
          }
  
          private final void adjustDynamicTableByteCount() {
              int i = this.maxDynamicTableByteCount;
              int i2 = this.dynamicTableByteCount;
              if (i >= i2) {
                  return;
              }
              if (i == 0) {
                  clearDynamicTable();
              } else {
                  evictToRecoverBytes(i2 - i);
              }
          }
  
          private final void clearDynamicTable() {
              ArraysKt.fill$default((Object[]) this.dynamicTable, (Object) null, 0, 0, 6, (Object) null);
              this.nextHeaderIndex = this.dynamicTable.length - 1;
              this.headerCount = 0;
              this.dynamicTableByteCount = 0;
          }
  
          private final int dynamicTableIndex(int index) {
              return this.nextHeaderIndex + 1 + index;
          }
  
          private final int evictToRecoverBytes(int bytesToRecover) {
              int i;
              int i2 = bytesToRecover;
              int i3 = 0;
              if (i2 > 0) {
                  int length = this.dynamicTable.length;
                  while (true) {
                      length--;
                      i = this.nextHeaderIndex;
                      if (length < i || i2 <= 0) {
                          Header[] headerArr = this.dynamicTable;
                          System.arraycopy(headerArr, i + 1, headerArr, i + 1 + i3, this.headerCount);
                          this.nextHeaderIndex += i3;
                      } else {
                          Header header = this.dynamicTable[length];
                          Intrinsics.checkNotNull(header);
                          i2 -= header.hpackSize;
                          this.dynamicTableByteCount -= header.hpackSize;
                          this.headerCount--;
                          i3++;
                      }
                  }
                  Header[] headerArr2 = this.dynamicTable;
                  System.arraycopy(headerArr2, i + 1, headerArr2, i + 1 + i3, this.headerCount);
                  this.nextHeaderIndex += i3;
              }
              return i3;
          }
  
          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 void insertIntoDynamicTable(int index, Header entry) {
              int i = index;
              this.headerList.add(entry);
              int i2 = entry.hpackSize;
              if (i != -1) {
                  Header header = this.dynamicTable[dynamicTableIndex(i)];
                  Intrinsics.checkNotNull(header);
                  i2 -= header.hpackSize;
              }
              int i3 = this.maxDynamicTableByteCount;
              if (i2 > i3) {
                  clearDynamicTable();
                  return;
              }
              int evictToRecoverBytes = evictToRecoverBytes((this.dynamicTableByteCount + i2) - i3);
              if (i == -1) {
                  int i4 = this.headerCount + 1;
                  Header[] headerArr = this.dynamicTable;
                  if (i4 > 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 i5 = this.nextHeaderIndex;
                  this.nextHeaderIndex = i5 - 1;
                  this.dynamicTable[i5] = entry;
                  this.headerCount++;
              } else {
                  this.dynamicTable[i + dynamicTableIndex(i) + evictToRecoverBytes] = entry;
              }
              this.dynamicTableByteCount += i2;
          }
  
          private final boolean isStaticHeader(int index) {
              return index >= 0 && index <= Hpack.INSTANCE.getSTATIC_HEADER_TABLE().length - 1;
          }
  
          private final int readByte() throws IOException {
              return Util.and(this.source.readByte(), 255);
          }
  
          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) {
                      Header header = headerArr[dynamicTableIndex];
                      Intrinsics.checkNotNull(header);
                      this.headerList.add(header);
                      return;
                  }
              }
              throw new IOException("Header index too large " + (index + 1));
          }
  
          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 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()));
          }
  
          public final List<Header> getAndResetHeaderList() {
              List<Header> list = CollectionsKt.toList(this.headerList);
              this.headerList.clear();
              return list;
          }
  
          public final int maxDynamicTableByteCount() {
              return this.maxDynamicTableByteCount;
          }
  
          public final ByteString readByteString() throws IOException {
              int readByte = readByte();
              boolean z = (readByte & 128) == 128;
              long readInt = (long) readInt(readByte, 127);
              if (!z) {
                  return this.source.readByteString(readInt);
              }
              Buffer buffer = new Buffer();
              Huffman.INSTANCE.decode(this.source, readInt, buffer);
              return buffer.readByteString();
          }
  
          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");
                  } else 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);
                  }
              }
          }
  
          public final int readInt(int firstByte, int prefixMask) throws IOException {
              int i = firstByte & prefixMask;
              if (i < prefixMask) {
                  return i;
              }
              int i2 = prefixMask;
              int i3 = 0;
              while (true) {
                  int readByte = readByte();
                  if ((readByte & 128) == 0) {
                      return i2 + (readByte << i3);
                  }
                  i2 += (readByte & 127) << i3;
                  i3 += 7;
              }
          }
      }
  
      @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\u000e¢\u0006\u0004\n\u0002\u0010\fR\u0012\u0010\r\u001a\u00020\u00038\u0006@\u0006X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u000e\u001a\u00020\u0005X\u000e¢\u0006\u0002\n\u0000R\u0012\u0010\u000f\u001a\u00020\u00038\u0006@\u0006X\u000e¢\u0006\u0002\n\u0000R\u0012\u0010\u0002\u001a\u00020\u00038\u0006@\u0006X\u000e¢\u0006\u0002\n\u0000R\u0012\u0010\u0010\u001a\u00020\u00038\u0006@\u0006X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0011\u001a\u00020\u0003X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0007X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0012\u001a\u00020\u0003X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0004\u001a\u00020\u0005X\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})
      /* compiled from: Hpack.kt */
      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, (DefaultConstructorMarker) null);
          }
  
          public Writer(int headerTableSizeSetting2, boolean useCompression2, Buffer out2) {
              Intrinsics.checkNotNullParameter(out2, "out");
              this.headerTableSizeSetting = headerTableSizeSetting2;
              this.useCompression = useCompression2;
              this.out = out2;
              this.smallestHeaderTableSizeSetting = Integer.MAX_VALUE;
              this.maxDynamicTableByteCount = headerTableSizeSetting2;
              Header[] headerArr = new Header[8];
              this.dynamicTable = headerArr;
              this.nextHeaderIndex = headerArr.length - 1;
          }
  
          /* JADX INFO: this call moved to the top of the method (can break code semantics) */
          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);
          }
  
          public Writer(Buffer buffer) {
              this(0, false, buffer, 3, (DefaultConstructorMarker) null);
          }
  
          private final void adjustDynamicTableByteCount() {
              int i = this.maxDynamicTableByteCount;
              int i2 = this.dynamicTableByteCount;
              if (i >= i2) {
                  return;
              }
              if (i == 0) {
                  clearDynamicTable();
              } else {
                  evictToRecoverBytes(i2 - i);
              }
          }
  
          private final void clearDynamicTable() {
              ArraysKt.fill$default((Object[]) 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 = bytesToRecover;
              int i3 = 0;
              if (i2 > 0) {
                  int length = this.dynamicTable.length;
                  while (true) {
                      length--;
                      i = this.nextHeaderIndex;
                      if (length < i || i2 <= 0) {
                          Header[] headerArr = this.dynamicTable;
                          System.arraycopy(headerArr, i + 1, headerArr, i + 1 + i3, this.headerCount);
                          Header[] headerArr2 = this.dynamicTable;
                          int i4 = this.nextHeaderIndex;
                          Arrays.fill(headerArr2, i4 + 1, i4 + 1 + i3, (Object) null);
                          this.nextHeaderIndex += i3;
                      } else {
                          Header header = this.dynamicTable[length];
                          Intrinsics.checkNotNull(header);
                          i2 -= header.hpackSize;
                          int i5 = this.dynamicTableByteCount;
                          Header header2 = this.dynamicTable[length];
                          Intrinsics.checkNotNull(header2);
                          this.dynamicTableByteCount = i5 - header2.hpackSize;
                          this.headerCount--;
                          i3++;
                      }
                  }
                  Header[] headerArr3 = this.dynamicTable;
                  System.arraycopy(headerArr3, i + 1, headerArr3, i + 1 + i3, this.headerCount);
                  Header[] headerArr22 = this.dynamicTable;
                  int i42 = this.nextHeaderIndex;
                  Arrays.fill(headerArr22, i42 + 1, i42 + 1 + i3, (Object) null);
                  this.nextHeaderIndex += i3;
              }
              return i3;
          }
  
          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 resizeHeaderTable(int headerTableSizeSetting2) {
              this.headerTableSizeSetting = headerTableSizeSetting2;
              int min = Math.min(headerTableSizeSetting2, 16384);
              int i = this.maxDynamicTableByteCount;
              if (i != min) {
                  if (min < i) {
                      this.smallestHeaderTableSizeSetting = Math.min(this.smallestHeaderTableSizeSetting, min);
                  }
                  this.emitDynamicTableSizeUpdate = true;
                  this.maxDynamicTableByteCount = min;
                  adjustDynamicTableByteCount();
              }
          }
  
          public final void writeByteString(ByteString data) throws IOException {
              Intrinsics.checkNotNullParameter(data, "data");
              if (!this.useCompression || Huffman.INSTANCE.encodedLength(data) >= data.size()) {
                  writeInt(data.size(), 127, 0);
                  this.out.write(data);
                  return;
              }
              Buffer buffer = new Buffer();
              Huffman.INSTANCE.encode(data, buffer);
              ByteString readByteString = buffer.readByteString();
              writeInt(readByteString.size(), 127, 128);
              this.out.write(readByteString);
          }
  
          public final void writeHeaders(List<Header> headerBlock) throws IOException {
              Intrinsics.checkNotNullParameter(headerBlock, "headerBlock");
              if (this.emitDynamicTableSizeUpdate) {
                  int i = this.smallestHeaderTableSizeSetting;
                  if (i < this.maxDynamicTableByteCount) {
                      writeInt(i, 31, 32);
                  }
                  this.emitDynamicTableSizeUpdate = false;
                  this.smallestHeaderTableSizeSetting = Integer.MAX_VALUE;
                  writeInt(this.maxDynamicTableByteCount, 31, 32);
              }
              int size = headerBlock.size();
              for (int i2 = 0; i2 < size; i2++) {
                  Header header = headerBlock.get(i2);
                  ByteString asciiLowercase = header.name.toAsciiLowercase();
                  ByteString byteString = header.value;
                  int i3 = -1;
                  int i4 = -1;
                  Integer num = Hpack.INSTANCE.getNAME_TO_FIRST_INDEX().get(asciiLowercase);
                  if (num != null && 2 <= (i4 = num.intValue() + 1) && 7 >= i4) {
                      if (Intrinsics.areEqual((Object) Hpack.INSTANCE.getSTATIC_HEADER_TABLE()[i4 - 1].value, (Object) byteString)) {
                          i3 = i4;
                      } else if (Intrinsics.areEqual((Object) Hpack.INSTANCE.getSTATIC_HEADER_TABLE()[i4].value, (Object) byteString)) {
                          i3 = i4 + 1;
                      }
                  }
                  if (i3 == -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((Object) header2.name, (Object) asciiLowercase)) {
                              Header header3 = this.dynamicTable[i5];
                              Intrinsics.checkNotNull(header3);
                              if (Intrinsics.areEqual((Object) header3.value, (Object) byteString)) {
                                  i3 = (i5 - this.nextHeaderIndex) + Hpack.INSTANCE.getSTATIC_HEADER_TABLE().length;
                                  break;
                              } else if (i4 == -1) {
                                  i4 = (i5 - this.nextHeaderIndex) + Hpack.INSTANCE.getSTATIC_HEADER_TABLE().length;
                              }
                          }
                          i5++;
                      }
                  }
                  if (i3 != -1) {
                      writeInt(i3, 127, 128);
                  } else if (i4 == -1) {
                      this.out.writeByte(64);
                      writeByteString(asciiLowercase);
                      writeByteString(byteString);
                      insertIntoDynamicTable(header);
                  } else if (!asciiLowercase.startsWith(Header.PSEUDO_PREFIX) || !(!Intrinsics.areEqual((Object) Header.TARGET_AUTHORITY, (Object) asciiLowercase))) {
                      writeInt(i4, 63, 64);
                      writeByteString(byteString);
                      insertIntoDynamicTable(header);
                  } else {
                      writeInt(i4, 15, 0);
                      writeByteString(byteString);
                  }
              }
          }
  
          public final void writeInt(int value, int prefixMask, int bits) {
              int i = value;
              if (i < prefixMask) {
                  this.out.writeByte(bits | i);
                  return;
              }
              this.out.writeByte(bits | prefixMask);
              int i2 = i - prefixMask;
              while (i2 >= 128) {
                  this.out.writeByte((i2 & 127) | 128);
                  i2 >>>= 7;
              }
              this.out.writeByte(i2);
          }
      }
  
      static {
          Hpack hpack = new Hpack();
          INSTANCE = hpack;
          NAME_TO_FIRST_INDEX = hpack.nameToFirstIndex();
      }
  
      private Hpack() {
      }
  
      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;
      }
  
      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;
      }
  
      public final Map<ByteString, Integer> getNAME_TO_FIRST_INDEX() {
          return NAME_TO_FIRST_INDEX;
      }
  
      public final Header[] getSTATIC_HEADER_TABLE() {
          return STATIC_HEADER_TABLE;
      }
  }
  