package x7;

import d5.AbstractC0844a;
import java.io.EOFException;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.Inflater;

/* JADX INFO: loaded from: classes.dex */
public final class q implements H, AutoCloseable {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public byte f18780q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final B f18781r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final Inflater f18782s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final r f18783t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final CRC32 f18784u;

    public q(H h8) {
        P6.g.e(h8, "source");
        B b8 = new B(h8);
        this.f18781r = b8;
        Inflater inflater = new Inflater(true);
        this.f18782s = inflater;
        this.f18783t = new r(b8, inflater);
        this.f18784u = new CRC32();
    }

    public static void a(int i, int i5, String str) throws IOException {
        if (i5 == i) {
            return;
        }
        throw new IOException(str + ": actual 0x" + X6.e.X(8, s7.d.E(i5)) + " != expected 0x" + X6.e.X(8, s7.d.E(i)));
    }

    @Override // x7.H
    public final long M(C1731g c1731g, long j8) throws IOException {
        q qVar = this;
        P6.g.e(c1731g, "sink");
        if (j8 < 0) {
            throw new IllegalArgumentException(AbstractC0844a.i(j8, "byteCount < 0: ").toString());
        }
        if (j8 == 0) {
            return 0L;
        }
        byte b8 = qVar.f18780q;
        CRC32 crc32 = qVar.f18784u;
        B b9 = qVar.f18781r;
        if (b8 == 0) {
            b9.O(10L);
            C1731g c1731g2 = b9.f18725r;
            byte bA = c1731g2.A(3L);
            boolean z8 = ((bA >> 1) & 1) == 1;
            if (z8) {
                qVar.b(c1731g2, 0L, 10L);
            }
            a(8075, b9.u(), "ID1ID2");
            b9.y(8L);
            if (((bA >> 2) & 1) == 1) {
                b9.O(2L);
                if (z8) {
                    b(c1731g2, 0L, 2L);
                }
                long jM0 = c1731g2.m0() & 65535;
                b9.O(jM0);
                if (z8) {
                    b(c1731g2, 0L, jM0);
                }
                b9.y(jM0);
            }
            if (((bA >> 3) & 1) == 1) {
                long jB = b9.b((byte) 0, 0L, Long.MAX_VALUE);
                if (jB == -1) {
                    throw new EOFException();
                }
                if (z8) {
                    b(c1731g2, 0L, jB + 1);
                }
                b9.y(jB + 1);
            }
            if (((bA >> 4) & 1) == 1) {
                long jB2 = b9.b((byte) 0, 0L, Long.MAX_VALUE);
                if (jB2 == -1) {
                    throw new EOFException();
                }
                if (z8) {
                    qVar = this;
                    qVar.b(c1731g2, 0L, jB2 + 1);
                } else {
                    qVar = this;
                }
                b9.y(jB2 + 1);
            } else {
                qVar = this;
            }
            if (z8) {
                a(b9.A(), (short) crc32.getValue(), "FHCRC");
                crc32.reset();
            }
            qVar.f18780q = (byte) 1;
        }
        if (qVar.f18780q == 1) {
            long j9 = c1731g.f18765r;
            long jM = qVar.f18783t.M(c1731g, j8);
            if (jM != -1) {
                qVar.b(c1731g, j9, jM);
                return jM;
            }
            qVar.f18780q = (byte) 2;
        }
        if (qVar.f18780q == 2) {
            a(b9.n(), (int) crc32.getValue(), "CRC");
            a(b9.n(), (int) qVar.f18782s.getBytesWritten(), "ISIZE");
            qVar.f18780q = (byte) 3;
            if (!b9.a()) {
                throw new IOException("gzip finished without exhausting source");
            }
        }
        return -1L;
    }

    public final void b(C1731g c1731g, long j8, long j9) {
        C c3 = c1731g.f18764q;
        P6.g.b(c3);
        while (true) {
            int i = c3.f18729c;
            int i5 = c3.f18728b;
            if (j8 < i - i5) {
                break;
            }
            j8 -= (long) (i - i5);
            c3 = c3.f18732f;
            P6.g.b(c3);
        }
        while (j9 > 0) {
            int i8 = (int) (((long) c3.f18728b) + j8);
            int iMin = (int) Math.min(c3.f18729c - i8, j9);
            this.f18784u.update(c3.f18727a, i8, iMin);
            j9 -= (long) iMin;
            c3 = c3.f18732f;
            P6.g.b(c3);
            j8 = 0;
        }
    }

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

    @Override // x7.H
    public final J f() {
        return this.f18781r.f18724q.f();
    }
}
