package p5;

import java.io.EOFException;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.Inflater;
import m1.AbstractC2464a;

/* JADX INFO: loaded from: classes3.dex */
public final class y implements P {

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final z f23127d;

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

    public y(P source) {
        kotlin.jvm.internal.o.h(source, "source");
        J j4 = new J(source);
        this.f23125b = j4;
        Inflater inflater = new Inflater(true);
        this.f23126c = inflater;
        this.f23127d = new z(j4, inflater);
        this.f23128e = new CRC32();
    }

    public static void a(int i6, int i7, String str) throws IOException {
        if (i7 == i6) {
            return;
        }
        StringBuilder sbB = AbstractC2464a.b(str, ": actual 0x");
        sbB.append(S4.p.V0(8, AbstractC2732b.m(i7)));
        sbB.append(" != expected 0x");
        sbB.append(S4.p.V0(8, AbstractC2732b.m(i6)));
        throw new IOException(sbB.toString());
    }

    public final void b(C2740j c2740j, long j4, long j6) {
        K k6 = c2740j.f23099a;
        kotlin.jvm.internal.o.e(k6);
        while (true) {
            int i6 = k6.f23063c;
            int i7 = k6.f23062b;
            if (j4 < i6 - i7) {
                break;
            }
            j4 -= (long) (i6 - i7);
            k6 = k6.f23066f;
            kotlin.jvm.internal.o.e(k6);
        }
        while (j6 > 0) {
            int i8 = (int) (((long) k6.f23062b) + j4);
            int iMin = (int) Math.min(k6.f23063c - i8, j6);
            this.f23128e.update(k6.f23061a, i8, iMin);
            j6 -= (long) iMin;
            k6 = k6.f23066f;
            kotlin.jvm.internal.o.e(k6);
            j4 = 0;
        }
    }

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

    @Override // p5.P
    public final long read(C2740j sink, long j4) throws IOException {
        y yVar = this;
        kotlin.jvm.internal.o.h(sink, "sink");
        if (j4 < 0) {
            throw new IllegalArgumentException(A0.a.m("byteCount < 0: ", j4).toString());
        }
        if (j4 == 0) {
            return 0L;
        }
        byte b5 = yVar.f23124a;
        CRC32 crc32 = yVar.f23128e;
        J j6 = yVar.f23125b;
        if (b5 == 0) {
            j6.A(10L);
            C2740j c2740j = j6.f23059b;
            byte bF = c2740j.f(3L);
            boolean z6 = ((bF >> 1) & 1) == 1;
            if (z6) {
                yVar.b(c2740j, 0L, 10L);
            }
            a(8075, j6.i(), "ID1ID2");
            j6.B(8L);
            if (((bF >> 2) & 1) == 1) {
                j6.A(2L);
                if (z6) {
                    b(c2740j, 0L, 2L);
                }
                long jF = c2740j.F() & 65535;
                j6.A(jF);
                if (z6) {
                    b(c2740j, 0L, jF);
                }
                j6.B(jF);
            }
            if (((bF >> 3) & 1) == 1) {
                long jK = j6.k((byte) 0, 0L, Long.MAX_VALUE);
                if (jK == -1) {
                    throw new EOFException();
                }
                if (z6) {
                    b(c2740j, 0L, jK + 1);
                }
                j6.B(jK + 1);
            }
            if (((bF >> 4) & 1) == 1) {
                long jK2 = j6.k((byte) 0, 0L, Long.MAX_VALUE);
                if (jK2 == -1) {
                    throw new EOFException();
                }
                if (z6) {
                    yVar = this;
                    yVar.b(c2740j, 0L, jK2 + 1);
                } else {
                    yVar = this;
                }
                j6.B(jK2 + 1);
            } else {
                yVar = this;
            }
            if (z6) {
                a(j6.t(), (short) crc32.getValue(), "FHCRC");
                crc32.reset();
            }
            yVar.f23124a = (byte) 1;
        }
        if (yVar.f23124a == 1) {
            long j7 = sink.f23100b;
            long j8 = yVar.f23127d.read(sink, j4);
            if (j8 != -1) {
                yVar.b(sink, j7, j8);
                return j8;
            }
            yVar.f23124a = (byte) 2;
        }
        if (yVar.f23124a == 2) {
            a(j6.f(), (int) crc32.getValue(), "CRC");
            a(j6.f(), (int) yVar.f23126c.getBytesWritten(), "ISIZE");
            yVar.f23124a = (byte) 3;
            if (!j6.a()) {
                throw new IOException("gzip finished without exhausting source");
            }
        }
        return -1L;
    }

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