package okhttp3.internal.http2;

import P4.e;
import com.google.android.recaptcha.internal.a;
import java.io.Closeable;
import java.io.EOFException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import kotlin.jvm.internal.C;
import kotlin.jvm.internal.o;
import okhttp3.internal.Util;
import okhttp3.internal.concurrent.Task;
import okhttp3.internal.concurrent.TaskQueue;
import okhttp3.internal.http2.Hpack;
import okhttp3.internal.http2.Http2Connection;
import okhttp3.internal.http2.PushObserver;
import p5.C2740j;
import p5.C2743m;
import p5.J;
import p5.P;
import p5.S;
import x4.AbstractC3250p;
import x4.AbstractC3251q;

/* JADX INFO: loaded from: classes3.dex */
public final class Http2Reader implements Closeable {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final Companion f22542d = new Companion(0);

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final Logger f22543e;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final J f22544a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final ContinuationSource f22545b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Hpack.Reader f22546c;

    public static final class Companion {
        public /* synthetic */ Companion(int i6) {
            this();
        }

        public static int a(int i6, int i7, int i8) throws IOException {
            if ((i7 & 8) != 0) {
                i6--;
            }
            if (i8 <= i6) {
                return i6 - i8;
            }
            throw new IOException(a.i(i8, i6, "PROTOCOL_ERROR padding ", " > remaining length "));
        }

        private Companion() {
        }
    }

    public static final class ContinuationSource implements P {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final J f22547a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public int f22548b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public int f22549c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public int f22550d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public int f22551e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public int f22552f;

        public ContinuationSource(J source) {
            o.h(source, "source");
            this.f22547a = source;
        }

        @Override // java.io.Closeable, java.lang.AutoCloseable
        public final void close() {
        }

        @Override // p5.P
        public final long read(C2740j sink, long j4) throws IOException {
            int i6;
            int iD;
            o.h(sink, "sink");
            do {
                int i7 = this.f22551e;
                J j6 = this.f22547a;
                if (i7 == 0) {
                    j6.B(this.f22552f);
                    this.f22552f = 0;
                    if ((this.f22549c & 4) == 0) {
                        i6 = this.f22550d;
                        int iT = Util.t(j6);
                        this.f22551e = iT;
                        this.f22548b = iT;
                        int iB = j6.b() & 255;
                        this.f22549c = j6.b() & 255;
                        Http2Reader.f22542d.getClass();
                        Logger logger = Http2Reader.f22543e;
                        if (logger.isLoggable(Level.FINE)) {
                            Http2 http2 = Http2.f22463a;
                            int i8 = this.f22550d;
                            int i9 = this.f22548b;
                            int i10 = this.f22549c;
                            http2.getClass();
                            logger.fine(Http2.a(true, i8, i9, iB, i10));
                        }
                        iD = j6.d() & Integer.MAX_VALUE;
                        this.f22550d = iD;
                        if (iB != 9) {
                            throw new IOException(Z0.o.i(iB, " != TYPE_CONTINUATION"));
                        }
                    }
                } else {
                    long j7 = j6.read(sink, Math.min(j4, i7));
                    if (j7 != -1) {
                        this.f22551e -= (int) j7;
                        return j7;
                    }
                }
                return -1L;
            } while (iD == i6);
            throw new IOException("TYPE_CONTINUATION streamId changed");
        }

        @Override // p5.P
        public final S timeout() {
            return this.f22547a.f23058a.timeout();
        }
    }

    public interface Handler {
    }

    static {
        Logger logger = Logger.getLogger(Http2.class.getName());
        o.g(logger, "getLogger(Http2::class.java.name)");
        f22543e = logger;
    }

    public Http2Reader(J source) {
        o.h(source, "source");
        this.f22544a = source;
        ContinuationSource continuationSource = new ContinuationSource(source);
        this.f22545b = continuationSource;
        this.f22546c = new Hpack.Reader(continuationSource);
    }

    /* JADX WARN: Code restructure failed: missing block: B:146:0x025a, code lost:
    
        throw new java.io.IOException(Z0.o.l("PROTOCOL_ERROR SETTINGS_MAX_FRAME_SIZE: ", r11));
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean a(boolean z6, final Http2Connection.ReaderRunnable readerRunnable) throws IOException {
        final ErrorCode errorCode;
        Object[] array;
        J j4 = this.f22544a;
        try {
            j4.A(9L);
            int iT = Util.t(j4);
            if (iT > 16384) {
                throw new IOException(Z0.o.l("FRAME_SIZE_ERROR: ", iT));
            }
            int iB = j4.b() & 255;
            byte b5 = j4.b();
            int i6 = b5 & 255;
            int iD = j4.d();
            final int i7 = iD & Integer.MAX_VALUE;
            Level level = Level.FINE;
            Logger logger = f22543e;
            if (logger.isLoggable(level)) {
                Http2.f22463a.getClass();
                logger.fine(Http2.a(true, i7, iT, iB, i6));
            }
            if (z6 && iB != 4) {
                StringBuilder sb = new StringBuilder("Expected a SETTINGS frame but was ");
                Http2.f22463a.getClass();
                String[] strArr = Http2.f22465c;
                sb.append(iB < strArr.length ? strArr[iB] : Util.h("0x%02x", Integer.valueOf(iB)));
                throw new IOException(sb.toString());
            }
            Companion companion = f22542d;
            ErrorCode errorCode2 = null;
            switch (iB) {
                case 0:
                    if (i7 == 0) {
                        throw new IOException("PROTOCOL_ERROR: TYPE_DATA streamId == 0");
                    }
                    boolean z7 = (b5 & 1) != 0;
                    if ((b5 & 32) != 0) {
                        throw new IOException("PROTOCOL_ERROR: FLAG_COMPRESSED without SETTINGS_COMPRESS_DATA");
                    }
                    int iB2 = (8 & b5) != 0 ? j4.b() & 255 : 0;
                    companion.getClass();
                    readerRunnable.a(z7, i7, j4, Companion.a(iT, i6, iB2));
                    j4.B(iB2);
                    return true;
                case 1:
                    if (i7 == 0) {
                        throw new IOException("PROTOCOL_ERROR: TYPE_HEADERS streamId == 0");
                    }
                    boolean z8 = (b5 & 1) != 0;
                    int iB3 = (8 & b5) != 0 ? j4.b() & 255 : 0;
                    if ((b5 & 32) != 0) {
                        c(readerRunnable, i7);
                        iT -= 5;
                    }
                    companion.getClass();
                    readerRunnable.b(i7, b(Companion.a(iT, i6, iB3), iB3, i6, i7), z8);
                    return true;
                case 2:
                    if (iT != 5) {
                        throw new IOException(Z0.o.j(iT, "TYPE_PRIORITY length: ", " != 5"));
                    }
                    if (i7 == 0) {
                        throw new IOException("TYPE_PRIORITY streamId == 0");
                    }
                    c(readerRunnable, i7);
                    return true;
                case 3:
                    if (iT != 4) {
                        throw new IOException(Z0.o.j(iT, "TYPE_RST_STREAM length: ", " != 4"));
                    }
                    if (i7 == 0) {
                        throw new IOException("TYPE_RST_STREAM streamId == 0");
                    }
                    int iD2 = j4.d();
                    ErrorCode.f22428b.getClass();
                    ErrorCode[] errorCodeArrValues = ErrorCode.values();
                    int length = errorCodeArrValues.length;
                    int i8 = 0;
                    while (true) {
                        if (i8 < length) {
                            errorCode = errorCodeArrValues[i8];
                            if (errorCode.f22435a != iD2) {
                                i8++;
                            }
                        } else {
                            errorCode = null;
                        }
                    }
                    if (errorCode == null) {
                        throw new IOException(Z0.o.l("TYPE_RST_STREAM unexpected error code: ", iD2));
                    }
                    final Http2Connection http2Connection = Http2Connection.this;
                    http2Connection.getClass();
                    if (i7 == 0 || (iD & 1) != 0) {
                        Http2Stream http2StreamD = http2Connection.d(i7);
                        if (http2StreamD != null) {
                            http2StreamD.j(errorCode);
                        }
                        return true;
                    }
                    final String str = http2Connection.f22475c + '[' + i7 + "] onReset";
                    http2Connection.l.c(new Task(str, http2Connection, i7, errorCode) { // from class: okhttp3.internal.http2.Http2Connection$pushResetLater$$inlined$execute$default$1

                        /* JADX INFO: renamed from: e, reason: collision with root package name */
                        public final /* synthetic */ Http2Connection f22524e;

                        /* JADX INFO: renamed from: f, reason: collision with root package name */
                        public final /* synthetic */ int f22525f;

                        @Override // okhttp3.internal.concurrent.Task
                        public final long a() {
                            ((PushObserver.Companion.PushObserverCancel) this.f22524e.f22482n).getClass();
                            synchronized (this.f22524e) {
                                this.f22524e.f22472C.remove(Integer.valueOf(this.f22525f));
                            }
                            return -1L;
                        }
                    }, 0L);
                    return true;
                case 4:
                    if (i7 != 0) {
                        throw new IOException("TYPE_SETTINGS streamId != 0");
                    }
                    if ((b5 & 1) != 0) {
                        if (iT != 0) {
                            throw new IOException("FRAME_SIZE_ERROR ack frame should be empty!");
                        }
                        return true;
                    }
                    if (iT % 6 != 0) {
                        throw new IOException(Z0.o.l("TYPE_SETTINGS length % 6 != 0: ", iT));
                    }
                    final Settings settings = new Settings();
                    e eVarI0 = L4.a.i0(6, L4.a.l0(0, iT));
                    int i9 = eVarI0.f5676a;
                    int i10 = eVarI0.f5677b;
                    int i11 = eVarI0.f5678c;
                    if ((i11 > 0 && i9 <= i10) || (i11 < 0 && i10 <= i9)) {
                        while (true) {
                            short sI = j4.i();
                            byte[] bArr = Util.f22179a;
                            int i12 = sI & 65535;
                            int iD3 = j4.d();
                            if (i12 != 2) {
                                if (i12 == 3) {
                                    i12 = 4;
                                } else if (i12 == 4) {
                                    if (iD3 < 0) {
                                        throw new IOException("PROTOCOL_ERROR SETTINGS_INITIAL_WINDOW_SIZE > 2^31 - 1");
                                    }
                                    i12 = 7;
                                } else if (i12 == 5 && (iD3 < 16384 || iD3 > 16777215)) {
                                }
                            } else if (iD3 != 0 && iD3 != 1) {
                                throw new IOException("PROTOCOL_ERROR SETTINGS_ENABLE_PUSH != 0 or 1");
                            }
                            settings.c(i12, iD3);
                            if (i9 != i10) {
                                i9 += i11;
                            }
                            break;
                        }
                    }
                    Http2Connection http2Connection2 = Http2Connection.this;
                    TaskQueue taskQueue = http2Connection2.f22480k;
                    final String strN = a.n(new StringBuilder(), http2Connection2.f22475c, " applyAndAckSettings");
                    taskQueue.c(new Task(strN) { // from class: okhttp3.internal.http2.Http2Connection$ReaderRunnable$settings$$inlined$execute$default$1
                        @Override // okhttp3.internal.concurrent.Task
                        public final long a() {
                            long jA;
                            int i13;
                            Http2Stream[] http2StreamArr;
                            Http2Connection.ReaderRunnable readerRunnable2 = readerRunnable;
                            Settings settings2 = settings;
                            readerRunnable2.getClass();
                            final C c6 = new C();
                            final Http2Connection http2Connection3 = Http2Connection.this;
                            synchronized (http2Connection3.f22470A) {
                                synchronized (http2Connection3) {
                                    try {
                                        Settings settings3 = http2Connection3.f22489u;
                                        Settings settings4 = new Settings();
                                        settings4.b(settings3);
                                        settings4.b(settings2);
                                        c6.f20115a = settings4;
                                        jA = ((long) settings4.a()) - ((long) settings3.a());
                                        http2StreamArr = (jA == 0 || http2Connection3.f22474b.isEmpty()) ? null : (Http2Stream[]) http2Connection3.f22474b.values().toArray(new Http2Stream[0]);
                                        Settings settings5 = (Settings) c6.f20115a;
                                        o.h(settings5, "<set-?>");
                                        http2Connection3.f22489u = settings5;
                                        http2Connection3.f22481m.c(new Task(http2Connection3.f22475c + " onSettings") { // from class: okhttp3.internal.http2.Http2Connection$ReaderRunnable$applyAndAckSettings$lambda$7$lambda$6$$inlined$execute$default$1
                                            @Override // okhttp3.internal.concurrent.Task
                                            public final long a() {
                                                Http2Connection http2Connection4 = http2Connection3;
                                                http2Connection4.f22473a.a(http2Connection4, (Settings) c6.f20115a);
                                                return -1L;
                                            }
                                        }, 0L);
                                    } catch (Throwable th) {
                                        throw th;
                                    }
                                }
                                try {
                                    http2Connection3.f22470A.a((Settings) c6.f20115a);
                                } catch (IOException e6) {
                                    http2Connection3.b(e6);
                                }
                            }
                            if (http2StreamArr == null) {
                                return -1L;
                            }
                            for (Http2Stream http2Stream : http2StreamArr) {
                                synchronized (http2Stream) {
                                    http2Stream.f22558f += jA;
                                    if (jA > 0) {
                                        http2Stream.notifyAll();
                                    }
                                }
                            }
                            return -1L;
                        }
                    }, 0L);
                    return true;
                case 5:
                    if (i7 == 0) {
                        throw new IOException("PROTOCOL_ERROR: TYPE_PUSH_PROMISE streamId == 0");
                    }
                    int iB4 = (b5 & 8) != 0 ? j4.b() & 255 : 0;
                    int iD4 = j4.d() & Integer.MAX_VALUE;
                    companion.getClass();
                    readerRunnable.c(iD4, b(Companion.a(iT - 4, i6, iB4), iB4, i6, i7));
                    return true;
                case 6:
                    if (iT != 8) {
                        throw new IOException(Z0.o.l("TYPE_PING length != 8: ", iT));
                    }
                    if (i7 != 0) {
                        throw new IOException("TYPE_PING streamId != 0");
                    }
                    final int iD5 = j4.d();
                    final int iD6 = j4.d();
                    if ((b5 & 1) == 0) {
                        TaskQueue taskQueue2 = Http2Connection.this.f22480k;
                        final String strN2 = a.n(new StringBuilder(), Http2Connection.this.f22475c, " ping");
                        final Http2Connection http2Connection3 = Http2Connection.this;
                        taskQueue2.c(new Task(strN2) { // from class: okhttp3.internal.http2.Http2Connection$ReaderRunnable$ping$$inlined$execute$default$1
                            @Override // okhttp3.internal.concurrent.Task
                            public final long a() {
                                int i13 = iD5;
                                int i14 = iD6;
                                Http2Connection http2Connection4 = http2Connection3;
                                http2Connection4.getClass();
                                try {
                                    http2Connection4.f22470A.g(i13, i14, true);
                                    return -1L;
                                } catch (IOException e6) {
                                    http2Connection4.b(e6);
                                    return -1L;
                                }
                            }
                        }, 0L);
                        return true;
                    }
                    Http2Connection http2Connection4 = Http2Connection.this;
                    synchronized (http2Connection4) {
                        try {
                            if (iD5 == 1) {
                                http2Connection4.f22484p++;
                            } else if (iD5 == 2) {
                                http2Connection4.f22486r++;
                            } else if (iD5 == 3) {
                                http2Connection4.notifyAll();
                            }
                        } catch (Throwable th) {
                            throw th;
                        }
                    }
                    return true;
                case 7:
                    if (iT < 8) {
                        throw new IOException(Z0.o.l("TYPE_GOAWAY length < 8: ", iT));
                    }
                    if (i7 != 0) {
                        throw new IOException("TYPE_GOAWAY streamId != 0");
                    }
                    int iD7 = j4.d();
                    int iD8 = j4.d();
                    int i13 = iT - 8;
                    ErrorCode.f22428b.getClass();
                    ErrorCode[] errorCodeArrValues2 = ErrorCode.values();
                    int length2 = errorCodeArrValues2.length;
                    int i14 = 0;
                    while (true) {
                        if (i14 < length2) {
                            ErrorCode errorCode3 = errorCodeArrValues2[i14];
                            if (errorCode3.f22435a == iD8) {
                                errorCode2 = errorCode3;
                            } else {
                                i14++;
                            }
                        }
                    }
                    if (errorCode2 == null) {
                        throw new IOException(Z0.o.l("TYPE_GOAWAY unexpected error code: ", iD8));
                    }
                    C2743m debugData = C2743m.f23101d;
                    if (i13 > 0) {
                        debugData = j4.c(i13);
                    }
                    o.h(debugData, "debugData");
                    debugData.d();
                    Http2Connection http2Connection5 = Http2Connection.this;
                    synchronized (http2Connection5) {
                        array = http2Connection5.f22474b.values().toArray(new Http2Stream[0]);
                        http2Connection5.f22478f = true;
                    }
                    for (Http2Stream http2Stream : (Http2Stream[]) array) {
                        if (http2Stream.f22553a > iD7 && http2Stream.g()) {
                            http2Stream.j(ErrorCode.REFUSED_STREAM);
                            Http2Connection.this.d(http2Stream.f22553a);
                        }
                    }
                    return true;
                case 8:
                    if (iT != 4) {
                        throw new IOException(Z0.o.l("TYPE_WINDOW_UPDATE length !=4: ", iT));
                    }
                    long jD = ((long) j4.d()) & 2147483647L;
                    if (jD == 0) {
                        throw new IOException("windowSizeIncrement was 0");
                    }
                    if (i7 == 0) {
                        Http2Connection http2Connection6 = Http2Connection.this;
                        synchronized (http2Connection6) {
                            http2Connection6.f22493y += jD;
                            http2Connection6.notifyAll();
                        }
                        return true;
                    }
                    Http2Stream http2StreamC = Http2Connection.this.c(i7);
                    if (http2StreamC != null) {
                        synchronized (http2StreamC) {
                            http2StreamC.f22558f += jD;
                            if (jD > 0) {
                                http2StreamC.notifyAll();
                            }
                            break;
                        }
                        return true;
                    }
                    return true;
                default:
                    j4.B(iT);
                    return true;
            }
        } catch (EOFException unused) {
            return false;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:20:0x0074, code lost:
    
        throw new java.io.IOException(Z0.o.l("Header index too large ", r8));
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final List b(int i6, int i7, int i8, int i9) throws IOException {
        Hpack.Reader reader;
        ContinuationSource continuationSource = this.f22545b;
        continuationSource.f22551e = i6;
        continuationSource.f22548b = i6;
        continuationSource.f22552f = i7;
        continuationSource.f22549c = i8;
        continuationSource.f22550d = i9;
        while (true) {
            reader = this.f22546c;
            J j4 = reader.f22450c;
            boolean zA = j4.a();
            ArrayList arrayList = reader.f22449b;
            if (zA) {
                List listC0 = AbstractC3251q.C0(arrayList);
                arrayList.clear();
                return listC0;
            }
            byte b5 = j4.b();
            byte[] bArr = Util.f22179a;
            int i10 = b5 & 255;
            if (i10 == 128) {
                throw new IOException("index == 0");
            }
            if ((b5 & 128) == 128) {
                int iE = reader.e(i10, 127);
                int i11 = iE - 1;
                if (i11 >= 0) {
                    Hpack hpack = Hpack.f22445a;
                    hpack.getClass();
                    Header[] headerArr = Hpack.f22446b;
                    if (i11 <= headerArr.length - 1) {
                        hpack.getClass();
                        arrayList.add(headerArr[i11]);
                    }
                }
                Hpack.f22445a.getClass();
                int length = reader.f22452e + 1 + (i11 - Hpack.f22446b.length);
                if (length < 0) {
                    break;
                }
                Header[] headerArr2 = reader.f22451d;
                if (length >= headerArr2.length) {
                    break;
                }
                Header header = headerArr2[length];
                o.e(header);
                arrayList.add(header);
            } else if (i10 == 64) {
                Hpack hpack2 = Hpack.f22445a;
                C2743m c2743mD = reader.d();
                hpack2.getClass();
                Hpack.a(c2743mD);
                reader.c(new Header(c2743mD, reader.d()));
            } else if ((b5 & 64) == 64) {
                reader.c(new Header(reader.b(reader.e(i10, 63) - 1), reader.d()));
            } else if ((b5 & 32) == 32) {
                int iE2 = reader.e(i10, 31);
                reader.f22448a = iE2;
                if (iE2 < 0 || iE2 > 4096) {
                    break;
                }
                int i12 = reader.f22454g;
                if (iE2 < i12) {
                    if (iE2 == 0) {
                        AbstractC3250p.J(reader.f22451d, null);
                        reader.f22452e = reader.f22451d.length - 1;
                        reader.f22453f = 0;
                        reader.f22454g = 0;
                    } else {
                        reader.a(i12 - iE2);
                    }
                }
            } else if (i10 == 16 || i10 == 0) {
                Hpack hpack3 = Hpack.f22445a;
                C2743m c2743mD2 = reader.d();
                hpack3.getClass();
                Hpack.a(c2743mD2);
                arrayList.add(new Header(c2743mD2, reader.d()));
            } else {
                arrayList.add(new Header(reader.b(reader.e(i10, 15) - 1), reader.d()));
            }
        }
        throw new IOException("Invalid dynamic table size update " + reader.f22448a);
    }

    public final void c(Http2Connection.ReaderRunnable readerRunnable, int i6) {
        J j4 = this.f22544a;
        j4.d();
        j4.b();
        byte[] bArr = Util.f22179a;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        this.f22544a.close();
    }
}
