package kr;

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

/* loaded from: classes4.dex */
public final class l implements a0 {

    /* renamed from: p, reason: collision with root package name */
    private byte f26660p;

    /* renamed from: q, reason: collision with root package name */
    private final u f26661q;

    /* renamed from: r, reason: collision with root package name */
    private final Inflater f26662r;

    /* renamed from: s, reason: collision with root package name */
    private final m f26663s;

    /* renamed from: t, reason: collision with root package name */
    private final CRC32 f26664t;

    public l(a0 source) {
        kotlin.jvm.internal.t.g(source, "source");
        u uVar = new u(source);
        this.f26661q = uVar;
        Inflater inflater = new Inflater(true);
        this.f26662r = inflater;
        this.f26663s = new m((f) uVar, inflater);
        this.f26664t = new CRC32();
    }

    private final void a(String str, int i10, int i11) {
        String r02;
        String r03;
        if (i11 == i10) {
            return;
        }
        StringBuilder sb2 = new StringBuilder();
        sb2.append(str);
        sb2.append(": actual 0x");
        r02 = kotlin.text.z.r0(b.k(i11), 8, '0');
        sb2.append(r02);
        sb2.append(" != expected 0x");
        r03 = kotlin.text.z.r0(b.k(i10), 8, '0');
        sb2.append(r03);
        throw new IOException(sb2.toString());
    }

    private final void b() {
        this.f26661q.L0(10L);
        byte p10 = this.f26661q.f26681q.p(3L);
        boolean z10 = ((p10 >> 1) & 1) == 1;
        if (z10) {
            d(this.f26661q.f26681q, 0L, 10L);
        }
        a("ID1ID2", 8075, this.f26661q.readShort());
        this.f26661q.skip(8L);
        if (((p10 >> 2) & 1) == 1) {
            this.f26661q.L0(2L);
            if (z10) {
                d(this.f26661q.f26681q, 0L, 2L);
            }
            long K = this.f26661q.f26681q.K() & 65535;
            this.f26661q.L0(K);
            if (z10) {
                d(this.f26661q.f26681q, 0L, K);
            }
            this.f26661q.skip(K);
        }
        if (((p10 >> 3) & 1) == 1) {
            long a10 = this.f26661q.a((byte) 0);
            if (a10 == -1) {
                throw new EOFException();
            }
            if (z10) {
                d(this.f26661q.f26681q, 0L, a10 + 1);
            }
            this.f26661q.skip(a10 + 1);
        }
        if (((p10 >> 4) & 1) == 1) {
            long a11 = this.f26661q.a((byte) 0);
            if (a11 == -1) {
                throw new EOFException();
            }
            if (z10) {
                d(this.f26661q.f26681q, 0L, a11 + 1);
            }
            this.f26661q.skip(a11 + 1);
        }
        if (z10) {
            a("FHCRC", this.f26661q.g(), (short) this.f26664t.getValue());
            this.f26664t.reset();
        }
    }

    private final void c() {
        a("CRC", this.f26661q.f(), (int) this.f26664t.getValue());
        a("ISIZE", this.f26661q.f(), (int) this.f26662r.getBytesWritten());
    }

    private final void d(d dVar, long j10, long j11) {
        v vVar = dVar.f26638p;
        kotlin.jvm.internal.t.d(vVar);
        while (true) {
            int i10 = vVar.f26687c;
            int i11 = vVar.f26686b;
            if (j10 < i10 - i11) {
                break;
            }
            j10 -= i10 - i11;
            vVar = vVar.f26690f;
            kotlin.jvm.internal.t.d(vVar);
        }
        while (j11 > 0) {
            int min = (int) Math.min(vVar.f26687c - r6, j11);
            this.f26664t.update(vVar.f26685a, (int) (vVar.f26686b + j10), min);
            j11 -= min;
            vVar = vVar.f26690f;
            kotlin.jvm.internal.t.d(vVar);
            j10 = 0;
        }
    }

    @Override // kr.a0
    public long J0(d sink, long j10) {
        kotlin.jvm.internal.t.g(sink, "sink");
        if (!(j10 >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j10).toString());
        }
        if (j10 == 0) {
            return 0L;
        }
        if (this.f26660p == 0) {
            b();
            this.f26660p = (byte) 1;
        }
        if (this.f26660p == 1) {
            long X = sink.X();
            long J0 = this.f26663s.J0(sink, j10);
            if (J0 != -1) {
                d(sink, X, J0);
                return J0;
            }
            this.f26660p = (byte) 2;
        }
        if (this.f26660p == 2) {
            c();
            this.f26660p = (byte) 3;
            if (!this.f26661q.E()) {
                throw new IOException("gzip finished without exhausting source");
            }
        }
        return -1L;
    }

    @Override // kr.a0, java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        this.f26663s.close();
    }

    @Override // kr.a0
    public b0 l() {
        return this.f26661q.l();
    }
}
