package okhttp3.internal.http2;

import d.b.a.a.a;
import g.h0.c.l;
import g.j0.f;
import j.b0;
import j.c0;
import j.d;
import j.g;
import j.h;
import java.io.Closeable;
import java.io.EOFException;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.SheetSettings;
import okhttp3.internal.Util;
import okhttp3.internal.http2.Hpack;

/* JADX INFO: compiled from: Http2Reader.kt */
/* JADX INFO: loaded from: classes.dex */
public final class Http2Reader implements Closeable {
    public static final Companion Companion = new Companion(null);
    private static final Logger logger;
    private final boolean client;
    private final ContinuationSource continuation;
    private final Hpack.Reader hpackReader;
    private final g source;

    /* JADX INFO: compiled from: Http2Reader.kt */
    public static final class Companion {
        private Companion() {
        }

        public final Logger getLogger() {
            return Http2Reader.logger;
        }

        public final int lengthWithoutPadding(int i2, int i3, int i4) throws IOException {
            if ((i3 & 8) != 0) {
                i2--;
            }
            if (i4 <= i2) {
                return i2 - i4;
            }
            throw new IOException(a.k("PROTOCOL_ERROR padding ", i4, " > remaining length ", i2));
        }

        public /* synthetic */ Companion(g.h0.c.g gVar) {
            this();
        }
    }

    /* JADX INFO: compiled from: Http2Reader.kt */
    public static final class ContinuationSource implements b0 {
        private int flags;
        private int left;
        private int length;
        private int padding;
        private final g source;
        private int streamId;

        public ContinuationSource(g gVar) {
            l.f(gVar, "source");
            this.source = gVar;
        }

        private final void readContinuationHeader() throws IOException {
            int i2 = this.streamId;
            int medium = Util.readMedium(this.source);
            this.left = medium;
            this.length = medium;
            int iAnd = Util.and(this.source.F(), SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT);
            this.flags = Util.and(this.source.F(), SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT);
            Companion companion = Http2Reader.Companion;
            if (companion.getLogger().isLoggable(Level.FINE)) {
                companion.getLogger().fine(Http2.INSTANCE.frameLog(true, this.streamId, this.length, iAnd, this.flags));
            }
            int iH = this.source.h() & Integer.MAX_VALUE;
            this.streamId = iH;
            if (iAnd == 9) {
                if (iH != i2) {
                    throw new IOException("TYPE_CONTINUATION streamId changed");
                }
            } else {
                throw new IOException(iAnd + " != TYPE_CONTINUATION");
            }
        }

        @Override // j.b0, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
        }

        public final int getFlags() {
            return this.flags;
        }

        public final int getLeft() {
            return this.left;
        }

        public final int getLength() {
            return this.length;
        }

        public final int getPadding() {
            return this.padding;
        }

        public final int getStreamId() {
            return this.streamId;
        }

        @Override // j.b0
        public long read(d dVar, long j2) throws IOException {
            l.f(dVar, "sink");
            while (true) {
                int i2 = this.left;
                if (i2 != 0) {
                    long j3 = this.source.read(dVar, Math.min(j2, i2));
                    if (j3 == -1) {
                        return -1L;
                    }
                    this.left -= (int) j3;
                    return j3;
                }
                this.source.K(this.padding);
                this.padding = 0;
                if ((this.flags & 4) != 0) {
                    return -1L;
                }
                readContinuationHeader();
            }
        }

        public final void setFlags(int i2) {
            this.flags = i2;
        }

        public final void setLeft(int i2) {
            this.left = i2;
        }

        public final void setLength(int i2) {
            this.length = i2;
        }

        public final void setPadding(int i2) {
            this.padding = i2;
        }

        public final void setStreamId(int i2) {
            this.streamId = i2;
        }

        @Override // j.b0
        public c0 timeout() {
            return this.source.timeout();
        }
    }

    /* JADX INFO: compiled from: Http2Reader.kt */
    public interface Handler {
        void ackSettings();

        void alternateService(int i2, String str, h hVar, String str2, int i3, long j2);

        void data(boolean z, int i2, g gVar, int i3) throws IOException;

        void goAway(int i2, ErrorCode errorCode, h hVar);

        void headers(boolean z, int i2, int i3, List<Header> list);

        void ping(boolean z, int i2, int i3);

        void priority(int i2, int i3, int i4, boolean z);

        void pushPromise(int i2, int i3, List<Header> list) throws IOException;

        void rstStream(int i2, ErrorCode errorCode);

        void settings(boolean z, Settings settings);

        void windowUpdate(int i2, long j2);
    }

    static {
        Logger logger2 = Logger.getLogger(Http2.class.getName());
        l.e(logger2, "Logger.getLogger(Http2::class.java.name)");
        logger = logger2;
    }

    public Http2Reader(g gVar, boolean z) {
        l.f(gVar, "source");
        this.source = gVar;
        this.client = z;
        ContinuationSource continuationSource = new ContinuationSource(gVar);
        this.continuation = continuationSource;
        this.hpackReader = new Hpack.Reader(continuationSource, 4096, 0, 4, null);
    }

    private final void readData(Handler handler, int i2, int i3, int i4) throws IOException {
        if (i4 == 0) {
            throw new IOException("PROTOCOL_ERROR: TYPE_DATA streamId == 0");
        }
        boolean z = (i3 & 1) != 0;
        if ((i3 & 32) != 0) {
            throw new IOException("PROTOCOL_ERROR: FLAG_COMPRESSED without SETTINGS_COMPRESS_DATA");
        }
        int iAnd = (i3 & 8) != 0 ? Util.and(this.source.F(), SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT) : 0;
        handler.data(z, i4, this.source, Companion.lengthWithoutPadding(i2, i3, iAnd));
        this.source.K(iAnd);
    }

    private final void readGoAway(Handler handler, int i2, int i3, int i4) throws IOException {
        if (i2 < 8) {
            throw new IOException(a.j("TYPE_GOAWAY length < 8: ", i2));
        }
        if (i4 != 0) {
            throw new IOException("TYPE_GOAWAY streamId != 0");
        }
        int iH = this.source.h();
        int iH2 = this.source.h();
        int i5 = i2 - 8;
        ErrorCode errorCodeFromHttp2 = ErrorCode.Companion.fromHttp2(iH2);
        if (errorCodeFromHttp2 == null) {
            throw new IOException(a.j("TYPE_GOAWAY unexpected error code: ", iH2));
        }
        h hVarC = h.f4611b;
        if (i5 > 0) {
            hVarC = this.source.c(i5);
        }
        handler.goAway(iH, errorCodeFromHttp2, hVarC);
    }

    private final List<Header> readHeaderBlock(int i2, int i3, int i4, int i5) throws IOException {
        this.continuation.setLeft(i2);
        ContinuationSource continuationSource = this.continuation;
        continuationSource.setLength(continuationSource.getLeft());
        this.continuation.setPadding(i3);
        this.continuation.setFlags(i4);
        this.continuation.setStreamId(i5);
        this.hpackReader.readHeaders();
        return this.hpackReader.getAndResetHeaderList();
    }

    private final void readHeaders(Handler handler, int i2, int i3, int i4) throws IOException {
        if (i4 == 0) {
            throw new IOException("PROTOCOL_ERROR: TYPE_HEADERS streamId == 0");
        }
        boolean z = (i3 & 1) != 0;
        int iAnd = (i3 & 8) != 0 ? Util.and(this.source.F(), SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT) : 0;
        if ((i3 & 32) != 0) {
            readPriority(handler, i4);
            i2 -= 5;
        }
        handler.headers(z, i4, -1, readHeaderBlock(Companion.lengthWithoutPadding(i2, i3, iAnd), iAnd, i3, i4));
    }

    private final void readPing(Handler handler, int i2, int i3, int i4) throws IOException {
        if (i2 != 8) {
            throw new IOException(a.j("TYPE_PING length != 8: ", i2));
        }
        if (i4 != 0) {
            throw new IOException("TYPE_PING streamId != 0");
        }
        handler.ping((i3 & 1) != 0, this.source.h(), this.source.h());
    }

    private final void readPriority(Handler handler, int i2, int i3, int i4) throws IOException {
        if (i2 == 5) {
            if (i4 == 0) {
                throw new IOException("TYPE_PRIORITY streamId == 0");
            }
            readPriority(handler, i4);
        } else {
            throw new IOException("TYPE_PRIORITY length: " + i2 + " != 5");
        }
    }

    private final void readPushPromise(Handler handler, int i2, int i3, int i4) throws IOException {
        if (i4 == 0) {
            throw new IOException("PROTOCOL_ERROR: TYPE_PUSH_PROMISE streamId == 0");
        }
        int iAnd = (i3 & 8) != 0 ? Util.and(this.source.F(), SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT) : 0;
        handler.pushPromise(i4, this.source.h() & Integer.MAX_VALUE, readHeaderBlock(Companion.lengthWithoutPadding(i2 - 4, i3, iAnd), iAnd, i3, i4));
    }

    private final void readRstStream(Handler handler, int i2, int i3, int i4) throws IOException {
        if (i2 != 4) {
            throw new IOException("TYPE_RST_STREAM length: " + i2 + " != 4");
        }
        if (i4 == 0) {
            throw new IOException("TYPE_RST_STREAM streamId == 0");
        }
        int iH = this.source.h();
        ErrorCode errorCodeFromHttp2 = ErrorCode.Companion.fromHttp2(iH);
        if (errorCodeFromHttp2 == null) {
            throw new IOException(a.j("TYPE_RST_STREAM unexpected error code: ", iH));
        }
        handler.rstStream(i4, errorCodeFromHttp2);
    }

    private final void readSettings(Handler handler, int i2, int i3, int i4) throws IOException {
        int iH;
        if (i4 != 0) {
            throw new IOException("TYPE_SETTINGS streamId != 0");
        }
        if ((i3 & 1) != 0) {
            if (i2 != 0) {
                throw new IOException("FRAME_SIZE_ERROR ack frame should be empty!");
            }
            handler.ackSettings();
            return;
        }
        if (i2 % 6 != 0) {
            throw new IOException(a.j("TYPE_SETTINGS length % 6 != 0: ", i2));
        }
        Settings settings = new Settings();
        f fVarC = g.j0.l.c(g.j0.l.d(0, i2), 6);
        int i5 = fVarC.f4123b;
        int i6 = fVarC.f4124e;
        int i7 = fVarC.f4125f;
        if (i7 < 0 ? i5 >= i6 : i5 <= i6) {
            while (true) {
                int iAnd = Util.and(this.source.Z(), Settings.DEFAULT_INITIAL_WINDOW_SIZE);
                iH = this.source.h();
                if (iAnd != 2) {
                    if (iAnd == 3) {
                        iAnd = 4;
                    } else if (iAnd == 4) {
                        iAnd = 7;
                        if (iH < 0) {
                            throw new IOException("PROTOCOL_ERROR SETTINGS_INITIAL_WINDOW_SIZE > 2^31 - 1");
                        }
                    } else if (iAnd == 5 && (iH < 16384 || iH > 16777215)) {
                        break;
                    }
                } else if (iH != 0 && iH != 1) {
                    throw new IOException("PROTOCOL_ERROR SETTINGS_ENABLE_PUSH != 0 or 1");
                }
                settings.set(iAnd, iH);
                if (i5 == i6) {
                    break;
                } else {
                    i5 += i7;
                }
            }
            throw new IOException(a.j("PROTOCOL_ERROR SETTINGS_MAX_FRAME_SIZE: ", iH));
        }
        handler.settings(false, settings);
    }

    private final void readWindowUpdate(Handler handler, int i2, int i3, int i4) throws IOException {
        if (i2 != 4) {
            throw new IOException(a.j("TYPE_WINDOW_UPDATE length !=4: ", i2));
        }
        long jAnd = Util.and(this.source.h(), 2147483647L);
        if (jAnd == 0) {
            throw new IOException("windowSizeIncrement was 0");
        }
        handler.windowUpdate(i4, jAnd);
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        this.source.close();
    }

    public final boolean nextFrame(boolean z, Handler handler) throws IOException {
        l.f(handler, "handler");
        try {
            this.source.e0(9L);
            int medium = Util.readMedium(this.source);
            if (medium > 16384) {
                throw new IOException(a.j("FRAME_SIZE_ERROR: ", medium));
            }
            int iAnd = Util.and(this.source.F(), SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT);
            int iAnd2 = Util.and(this.source.F(), SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT);
            int iH = this.source.h() & Integer.MAX_VALUE;
            Logger logger2 = logger;
            if (logger2.isLoggable(Level.FINE)) {
                logger2.fine(Http2.INSTANCE.frameLog(true, iH, medium, iAnd, iAnd2));
            }
            if (z && iAnd != 4) {
                StringBuilder sbB = a.B("Expected a SETTINGS frame but was ");
                sbB.append(Http2.INSTANCE.formattedType$okhttp(iAnd));
                throw new IOException(sbB.toString());
            }
            switch (iAnd) {
                case 0:
                    readData(handler, medium, iAnd2, iH);
                    return true;
                case 1:
                    readHeaders(handler, medium, iAnd2, iH);
                    return true;
                case 2:
                    readPriority(handler, medium, iAnd2, iH);
                    return true;
                case 3:
                    readRstStream(handler, medium, iAnd2, iH);
                    return true;
                case 4:
                    readSettings(handler, medium, iAnd2, iH);
                    return true;
                case 5:
                    readPushPromise(handler, medium, iAnd2, iH);
                    return true;
                case 6:
                    readPing(handler, medium, iAnd2, iH);
                    return true;
                case 7:
                    readGoAway(handler, medium, iAnd2, iH);
                    return true;
                case 8:
                    readWindowUpdate(handler, medium, iAnd2, iH);
                    return true;
                default:
                    this.source.K(medium);
                    return true;
            }
        } catch (EOFException unused) {
            return false;
        }
    }

    public final void readConnectionPreface(Handler handler) throws IOException {
        l.f(handler, "handler");
        if (this.client) {
            if (!nextFrame(true, handler)) {
                throw new IOException("Required SETTINGS preface not received");
            }
            return;
        }
        g gVar = this.source;
        h hVar = Http2.CONNECTION_PREFACE;
        h hVarC = gVar.c(hVar.c());
        Logger logger2 = logger;
        if (logger2.isLoggable(Level.FINE)) {
            StringBuilder sbB = a.B("<< CONNECTION ");
            sbB.append(hVarC.d());
            logger2.fine(Util.format(sbB.toString(), new Object[0]));
        }
        if (!l.a(hVar, hVarC)) {
            StringBuilder sbB2 = a.B("Expected a connection header but was ");
            sbB2.append(hVarC.l());
            throw new IOException(sbB2.toString());
        }
    }

    private final void readPriority(Handler handler, int i2) throws IOException {
        int iH = this.source.h();
        handler.priority(i2, iH & Integer.MAX_VALUE, Util.and(this.source.F(), SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT) + 1, (((int) 2147483648L) & iH) != 0);
    }
}
