package h;

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

/* JADX INFO: loaded from: classes.dex */
public final class m implements x {

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

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final CRC32 f6543f = new CRC32();

    public m(x xVar) {
        if (xVar == null) {
            throw new IllegalArgumentException("source == null");
        }
        Inflater inflater = new Inflater(true);
        this.f6541d = inflater;
        Logger logger = p.f6550a;
        s sVar = new s(xVar);
        this.f6540c = sVar;
        this.f6542e = new n(sVar, inflater);
    }

    public final void a(String str, int i2, int i3) throws IOException {
        if (i3 != i2) {
            throw new IOException(String.format("%s: actual 0x%08x != expected 0x%08x", str, Integer.valueOf(i3), Integer.valueOf(i2)));
        }
    }

    @Override // h.x, java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        this.f6542e.close();
    }

    @Override // h.x
    public y e() {
        return this.f6540c.e();
    }

    public final void i(f fVar, long j2, long j3) {
        t tVar = fVar.f6528c;
        while (true) {
            int i2 = tVar.f6562c;
            int i3 = tVar.f6561b;
            if (j2 < i2 - i3) {
                break;
            }
            j2 -= (long) (i2 - i3);
            tVar = tVar.f6565f;
        }
        while (j3 > 0) {
            int i4 = (int) (((long) tVar.f6561b) + j2);
            int iMin = (int) Math.min(tVar.f6562c - i4, j3);
            this.f6543f.update(tVar.f6560a, i4, iMin);
            j3 -= (long) iMin;
            tVar = tVar.f6565f;
            j2 = 0;
        }
    }

    @Override // h.x
    public long v(f fVar, long j2) throws IOException {
        long j3;
        if (j2 < 0) {
            throw new IllegalArgumentException("byteCount < 0: " + j2);
        }
        if (j2 == 0) {
            return 0L;
        }
        if (this.f6539b == 0) {
            this.f6540c.A(10L);
            byte bN = this.f6540c.b().N(3L);
            boolean z = ((bN >> 1) & 1) == 1;
            if (z) {
                i(this.f6540c.b(), 0L, 10L);
            }
            a("ID1ID2", 8075, this.f6540c.y());
            this.f6540c.l(8L);
            if (((bN >> 2) & 1) == 1) {
                this.f6540c.A(2L);
                if (z) {
                    i(this.f6540c.b(), 0L, 2L);
                }
                long jU = this.f6540c.b().u();
                this.f6540c.A(jU);
                if (z) {
                    j3 = jU;
                    i(this.f6540c.b(), 0L, jU);
                } else {
                    j3 = jU;
                }
                this.f6540c.l(j3);
            }
            if (((bN >> 3) & 1) == 1) {
                long jF = this.f6540c.F((byte) 0);
                if (jF == -1) {
                    throw new EOFException();
                }
                if (z) {
                    i(this.f6540c.b(), 0L, jF + 1);
                }
                this.f6540c.l(jF + 1);
            }
            if (((bN >> 4) & 1) == 1) {
                long jF2 = this.f6540c.F((byte) 0);
                if (jF2 == -1) {
                    throw new EOFException();
                }
                if (z) {
                    i(this.f6540c.b(), 0L, jF2 + 1);
                }
                this.f6540c.l(jF2 + 1);
            }
            if (z) {
                a("FHCRC", this.f6540c.u(), (short) this.f6543f.getValue());
                this.f6543f.reset();
            }
            this.f6539b = 1;
        }
        if (this.f6539b == 1) {
            long j4 = fVar.f6529d;
            long jV = this.f6542e.v(fVar, j2);
            if (jV != -1) {
                i(fVar, j4, jV);
                return jV;
            }
            this.f6539b = 2;
        }
        if (this.f6539b == 2) {
            a("CRC", this.f6540c.p(), (int) this.f6543f.getValue());
            a("ISIZE", this.f6540c.p(), (int) this.f6541d.getBytesWritten());
            this.f6539b = 3;
            if (!this.f6540c.q()) {
                throw new IOException("gzip finished without exhausting source");
            }
        }
        return -1L;
    }
}
