package d;

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 {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private byte f1578b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final u f1579c;

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

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

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

    public m(a0 a0Var) {
        c.q.d.j.c(a0Var, "source");
        this.f1579c = new u(a0Var);
        Inflater inflater = new Inflater(true);
        this.f1580d = inflater;
        this.f1581e = new n(this.f1579c, inflater);
        this.f1582f = new CRC32();
    }

    private final void F() throws IOException {
        this.f1579c.u(10L);
        byte bK = this.f1579c.f1602b.K(3L);
        boolean z = ((bK >> 1) & 1) == 1;
        if (z) {
            H(this.f1579c.f1602b, 0L, 10L);
        }
        v("ID1ID2", 8075, this.f1579c.readShort());
        this.f1579c.l(8L);
        if (((bK >> 2) & 1) == 1) {
            this.f1579c.u(2L);
            if (z) {
                H(this.f1579c.f1602b, 0L, 2L);
            }
            long jT = this.f1579c.f1602b.T();
            this.f1579c.u(jT);
            if (z) {
                H(this.f1579c.f1602b, 0L, jT);
            }
            this.f1579c.l(jT);
        }
        if (((bK >> 3) & 1) == 1) {
            long jV = this.f1579c.v((byte) 0);
            if (jV == -1) {
                throw new EOFException();
            }
            if (z) {
                H(this.f1579c.f1602b, 0L, jV + 1);
            }
            this.f1579c.l(jV + 1);
        }
        if (((bK >> 4) & 1) == 1) {
            long jV2 = this.f1579c.v((byte) 0);
            if (jV2 == -1) {
                throw new EOFException();
            }
            if (z) {
                H(this.f1579c.f1602b, 0L, jV2 + 1);
            }
            this.f1579c.l(jV2 + 1);
        }
        if (z) {
            v("FHCRC", this.f1579c.H(), (short) this.f1582f.getValue());
            this.f1582f.reset();
        }
    }

    private final void G() throws IOException {
        v("CRC", this.f1579c.G(), (int) this.f1582f.getValue());
        v("ISIZE", this.f1579c.G(), (int) this.f1580d.getBytesWritten());
    }

    private final void H(f fVar, long j, long j2) {
        v vVar = fVar.f1563b;
        if (vVar == null) {
            c.q.d.j.g();
            throw null;
        }
        do {
            int i = vVar.f1608c;
            int i2 = vVar.f1607b;
            if (j < i - i2) {
                while (j2 > 0) {
                    int i3 = (int) (((long) vVar.f1607b) + j);
                    int iMin = (int) Math.min(vVar.f1608c - i3, j2);
                    this.f1582f.update(vVar.f1606a, i3, iMin);
                    j2 -= (long) iMin;
                    vVar = vVar.f1611f;
                    if (vVar == null) {
                        c.q.d.j.g();
                        throw null;
                    }
                    j = 0;
                }
                return;
            }
            j -= (long) (i - i2);
            vVar = vVar.f1611f;
        } while (vVar != null);
        c.q.d.j.g();
        throw null;
    }

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

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

    @Override // d.a0
    public long read(f fVar, long j) throws IOException {
        c.q.d.j.c(fVar, "sink");
        if (!(j >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j).toString());
        }
        if (j == 0) {
            return 0L;
        }
        if (this.f1578b == 0) {
            F();
            this.f1578b = (byte) 1;
        }
        if (this.f1578b == 1) {
            long jB0 = fVar.b0();
            long j2 = this.f1581e.read(fVar, j);
            if (j2 != -1) {
                H(fVar, jB0, j2);
                return j2;
            }
            this.f1578b = (byte) 2;
        }
        if (this.f1578b == 2) {
            G();
            this.f1578b = (byte) 3;
            if (!this.f1579c.y()) {
                throw new IOException("gzip finished without exhausting source");
            }
        }
        return -1L;
    }

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