package j;

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 b0 {

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

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final o f4622g;

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

    public m(b0 b0Var) {
        g.h0.c.l.f(b0Var, "source");
        v vVar = new v(b0Var);
        this.f4620e = vVar;
        Inflater inflater = new Inflater(true);
        this.f4621f = inflater;
        this.f4622g = new o((g) vVar, inflater);
        this.f4623h = new CRC32();
    }

    public final void a(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));
        g.h0.c.l.e(str2, "java.lang.String.format(this, *args)");
        throw new IOException(str2);
    }

    public final void b(d dVar, long j2, long j3) {
        w wVar = dVar.f4600b;
        g.h0.c.l.c(wVar);
        while (true) {
            int i2 = wVar.f4650c;
            int i3 = wVar.f4649b;
            if (j2 < i2 - i3) {
                break;
            }
            j2 -= (long) (i2 - i3);
            wVar = wVar.f4653f;
            g.h0.c.l.c(wVar);
        }
        while (j3 > 0) {
            int i4 = (int) (((long) wVar.f4649b) + j2);
            int iMin = (int) Math.min(wVar.f4650c - i4, j3);
            this.f4623h.update(wVar.a, i4, iMin);
            j3 -= (long) iMin;
            wVar = wVar.f4653f;
            g.h0.c.l.c(wVar);
            j2 = 0;
        }
    }

    @Override // j.b0, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        this.f4622g.close();
    }

    @Override // j.b0
    public long read(d dVar, long j2) throws IOException {
        long j3;
        g.h0.c.l.f(dVar, "sink");
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(d.b.a.a.a.l("byteCount < 0: ", j2).toString());
        }
        if (j2 == 0) {
            return 0L;
        }
        if (this.f4619b == 0) {
            this.f4620e.e0(10L);
            byte bE = this.f4620e.f4645b.e(3L);
            boolean z = ((bE >> 1) & 1) == 1;
            if (z) {
                b(this.f4620e.f4645b, 0L, 10L);
            }
            a("ID1ID2", 8075, this.f4620e.Z());
            this.f4620e.K(8L);
            if (((bE >> 2) & 1) == 1) {
                this.f4620e.e0(2L);
                if (z) {
                    b(this.f4620e.f4645b, 0L, 2L);
                }
                long jP = this.f4620e.f4645b.p();
                this.f4620e.e0(jP);
                if (z) {
                    j3 = jP;
                    b(this.f4620e.f4645b, 0L, jP);
                } else {
                    j3 = jP;
                }
                this.f4620e.K(j3);
            }
            if (((bE >> 3) & 1) == 1) {
                long jA = this.f4620e.a((byte) 0, 0L, Long.MAX_VALUE);
                if (jA == -1) {
                    throw new EOFException();
                }
                if (z) {
                    b(this.f4620e.f4645b, 0L, jA + 1);
                }
                this.f4620e.K(jA + 1);
            }
            if (((bE >> 4) & 1) == 1) {
                long jA2 = this.f4620e.a((byte) 0, 0L, Long.MAX_VALUE);
                if (jA2 == -1) {
                    throw new EOFException();
                }
                if (z) {
                    b(this.f4620e.f4645b, 0L, jA2 + 1);
                }
                this.f4620e.K(jA2 + 1);
            }
            if (z) {
                v vVar = this.f4620e;
                vVar.e0(2L);
                a("FHCRC", vVar.f4645b.p(), (short) this.f4623h.getValue());
                this.f4623h.reset();
            }
            this.f4619b = (byte) 1;
        }
        if (this.f4619b == 1) {
            long j4 = dVar.f4601e;
            long j5 = this.f4622g.read(dVar, j2);
            if (j5 != -1) {
                b(dVar, j4, j5);
                return j5;
            }
            this.f4619b = (byte) 2;
        }
        if (this.f4619b == 2) {
            a("CRC", this.f4620e.d(), (int) this.f4623h.getValue());
            a("ISIZE", this.f4620e.d(), (int) this.f4621f.getBytesWritten());
            this.f4619b = (byte) 3;
            if (!this.f4620e.n()) {
                throw new IOException("gzip finished without exhausting source");
            }
        }
        return -1L;
    }

    @Override // j.b0
    public c0 timeout() {
        return this.f4620e.timeout();
    }
}
