package l;

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

/* JADX INFO: compiled from: GzipSource.kt */
/* JADX INFO: loaded from: classes.dex */
public final class m implements a0 {
    private byte b;
    private final u d;
    private final Inflater e;
    private final n f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final CRC32 f943g;

    public m(a0 a0Var) {
        j.p0.d.r.g(a0Var, "source");
        this.d = new u(a0Var);
        Inflater inflater = new Inflater(true);
        this.e = inflater;
        this.f = new n(this.d, inflater);
        this.f943g = new CRC32();
    }

    private final void c(String str, int i2, int i3) throws IOException {
        if (i3 == i2) {
            return;
        }
        String str2 = String.format("%s: actual 0x%08x != expected 0x%08x", Arrays.copyOf(new Object[]{str, Integer.valueOf(i3), Integer.valueOf(i2)}, 3));
        j.p0.d.r.b(str2, "java.lang.String.format(this, *args)");
        throw new IOException(str2);
    }

    private final void f() throws IOException {
        this.d.e0(10L);
        byte bR = this.d.b.r(3L);
        boolean z = ((bR >> 1) & 1) == 1;
        if (z) {
            j(this.d.b, 0L, 10L);
        }
        c("ID1ID2", 8075, this.d.readShort());
        this.d.k(8L);
        if (((bR >> 2) & 1) == 1) {
            this.d.e0(2L);
            if (z) {
                j(this.d.b, 0L, 2L);
            }
            long jB = this.d.b.B();
            this.d.e0(jB);
            if (z) {
                j(this.d.b, 0L, jB);
            }
            this.d.k(jB);
        }
        if (((bR >> 3) & 1) == 1) {
            long jC = this.d.c((byte) 0);
            if (jC == -1) {
                throw new EOFException();
            }
            if (z) {
                j(this.d.b, 0L, jC + 1);
            }
            this.d.k(jC + 1);
        }
        if (((bR >> 4) & 1) == 1) {
            long jC2 = this.d.c((byte) 0);
            if (jC2 == -1) {
                throw new EOFException();
            }
            if (z) {
                j(this.d.b, 0L, jC2 + 1);
            }
            this.d.k(jC2 + 1);
        }
        if (z) {
            c("FHCRC", this.d.j(), (short) this.f943g.getValue());
            this.f943g.reset();
        }
    }

    private final void i() throws IOException {
        c("CRC", this.d.i(), (int) this.f943g.getValue());
        c("ISIZE", this.d.i(), (int) this.e.getBytesWritten());
    }

    private final void j(f fVar, long j2, long j3) {
        v vVar = fVar.b;
        if (vVar == null) {
            j.p0.d.r.p();
            throw null;
        }
        do {
            int i2 = vVar.c;
            int i3 = vVar.b;
            if (j2 < i2 - i3) {
                while (j3 > 0) {
                    int i4 = (int) (((long) vVar.b) + j2);
                    int iMin = (int) Math.min(vVar.c - i4, j3);
                    this.f943g.update(vVar.a, i4, iMin);
                    j3 -= (long) iMin;
                    vVar = vVar.f;
                    if (vVar == null) {
                        j.p0.d.r.p();
                        throw null;
                    }
                    j2 = 0;
                }
                return;
            }
            j2 -= (long) (i2 - i3);
            vVar = vVar.f;
        } while (vVar != null);
        j.p0.d.r.p();
        throw null;
    }

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

    @Override // l.a0
    public long read(f fVar, long j2) throws IOException {
        j.p0.d.r.g(fVar, "sink");
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j2).toString());
        }
        if (j2 == 0) {
            return 0L;
        }
        if (this.b == 0) {
            f();
            this.b = (byte) 1;
        }
        if (this.b == 1) {
            long jK = fVar.K();
            long j3 = this.f.read(fVar, j2);
            if (j3 != -1) {
                j(fVar, jK, j3);
                return j3;
            }
            this.b = (byte) 2;
        }
        if (this.b == 2) {
            i();
            this.b = (byte) 3;
            if (!this.d.E()) {
                throw new IOException("gzip finished without exhausting source");
            }
        }
        return -1L;
    }

    @Override // l.a0
    public b0 timeout() {
        return this.d.timeout();
    }
}
