package okio;

import g.a.a.a.a;
import io.netty.handler.codec.rtsp.RtspHeaders;
import java.io.EOFException;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.Inflater;
import kotlin.Metadata;
import kotlin.jvm.internal.o;

/* JADX INFO: renamed from: z.k, reason: from Kotlin metadata */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000R\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0005\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u000e\n\u0000\n\u0002\u0010\b\n\u0002\b\u0005\n\u0002\u0010\t\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0004\u0018\u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0001¢\u0006\u0002\u0010\u0003J \u0010\r\u001a\u00020\u000e2\u0006\u0010\u000f\u001a\u00020\u00102\u0006\u0010\u0011\u001a\u00020\u00122\u0006\u0010\u0013\u001a\u00020\u0012H\u0002J\b\u0010\u0014\u001a\u00020\u000eH\u0016J\b\u0010\u0015\u001a\u00020\u000eH\u0002J\b\u0010\u0016\u001a\u00020\u000eH\u0002J\u0018\u0010\u0017\u001a\u00020\u00182\u0006\u0010\u0019\u001a\u00020\u001a2\u0006\u0010\u001b\u001a\u00020\u0018H\u0016J\b\u0010\u001c\u001a\u00020\u001dH\u0016J \u0010\u001e\u001a\u00020\u000e2\u0006\u0010\u001f\u001a\u00020\u001a2\u0006\u0010 \u001a\u00020\u00182\u0006\u0010\u001b\u001a\u00020\u0018H\u0002R\u000e\u0010\u0004\u001a\u00020\u0005X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0007X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\tX\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\n\u001a\u00020\u000bX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0002\u001a\u00020\fX\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006!"}, d2 = {"Lokio/GzipSource;", "Lokio/Source;", "source", "(Lokio/Source;)V", "crc", "Ljava/util/zip/CRC32;", "inflater", "Ljava/util/zip/Inflater;", "inflaterSource", "Lokio/InflaterSource;", "section", "", "Lokio/RealBufferedSource;", "checkEqual", "", "name", "", "expected", "", "actual", "close", "consumeHeader", "consumeTrailer", "read", "", "sink", "Lokio/Buffer;", "byteCount", RtspHeaders.Values.TIMEOUT, "Lokio/Timeout;", "updateCrc", "buffer", "offset", "okio"}, k = 1, mv = {1, 6, 0}, xi = 48)
public final class GzipSource implements Source {

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final RealBufferedSource f10129m;

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

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

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

    public GzipSource(Source source) {
        o.f(source, "source");
        RealBufferedSource realBufferedSource = new RealBufferedSource(source);
        this.f10129m = realBufferedSource;
        Inflater inflater = new Inflater(true);
        this.f10130n = inflater;
        this.f10131o = new InflaterSource(realBufferedSource, inflater);
        this.f10132p = new CRC32();
    }

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

    public final void c(Buffer buffer, long j2, long j3) {
        Segment segment = buffer.f10117f;
        while (true) {
            o.c(segment);
            int i2 = segment.f10152c;
            int i3 = segment.f10151b;
            if (j2 < i2 - i3) {
                break;
            }
            j2 -= (long) (i2 - i3);
            segment = segment.f10155f;
        }
        while (j3 > 0) {
            int i4 = (int) (((long) segment.f10151b) + j2);
            int iMin = (int) Math.min(segment.f10152c - i4, j3);
            this.f10132p.update(segment.a, i4, iMin);
            j3 -= (long) iMin;
            segment = segment.f10155f;
            o.c(segment);
            j2 = 0;
        }
    }

    @Override // okio.Source, java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        this.f10131o.close();
    }

    @Override // okio.Source
    public long r(Buffer buffer, long j2) throws IOException {
        long j3;
        o.f(buffer, "sink");
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(a.q("byteCount < 0: ", j2).toString());
        }
        if (j2 == 0) {
            return 0L;
        }
        if (this.f10128f == 0) {
            this.f10129m.t(10L);
            byte bD = this.f10129m.f10148m.d(3L);
            boolean z2 = ((bD >> 1) & 1) == 1;
            if (z2) {
                c(this.f10129m.f10148m, 0L, 10L);
            }
            RealBufferedSource realBufferedSource = this.f10129m;
            realBufferedSource.t(2L);
            a("ID1ID2", 8075, realBufferedSource.f10148m.readShort());
            this.f10129m.skip(8L);
            if (((bD >> 2) & 1) == 1) {
                this.f10129m.t(2L);
                if (z2) {
                    c(this.f10129m.f10148m, 0L, 2L);
                }
                long jM = this.f10129m.f10148m.m();
                this.f10129m.t(jM);
                if (z2) {
                    j3 = jM;
                    c(this.f10129m.f10148m, 0L, jM);
                } else {
                    j3 = jM;
                }
                this.f10129m.skip(j3);
            }
            if (((bD >> 3) & 1) == 1) {
                long jA = this.f10129m.a((byte) 0, 0L, Long.MAX_VALUE);
                if (jA == -1) {
                    throw new EOFException();
                }
                if (z2) {
                    c(this.f10129m.f10148m, 0L, jA + 1);
                }
                this.f10129m.skip(jA + 1);
            }
            if (((bD >> 4) & 1) == 1) {
                long jA2 = this.f10129m.a((byte) 0, 0L, Long.MAX_VALUE);
                if (jA2 == -1) {
                    throw new EOFException();
                }
                if (z2) {
                    c(this.f10129m.f10148m, 0L, jA2 + 1);
                }
                this.f10129m.skip(jA2 + 1);
            }
            if (z2) {
                RealBufferedSource realBufferedSource2 = this.f10129m;
                realBufferedSource2.t(2L);
                a("FHCRC", realBufferedSource2.f10148m.m(), (short) this.f10132p.getValue());
                this.f10132p.reset();
            }
            this.f10128f = (byte) 1;
        }
        if (this.f10128f == 1) {
            long j4 = buffer.f10118m;
            long jR = this.f10131o.r(buffer, j2);
            if (jR != -1) {
                c(buffer, j4, jR);
                return jR;
            }
            this.f10128f = (byte) 2;
        }
        if (this.f10128f == 2) {
            a("CRC", this.f10129m.c(), (int) this.f10132p.getValue());
            a("ISIZE", this.f10129m.c(), (int) this.f10130n.getBytesWritten());
            this.f10128f = (byte) 3;
            if (!this.f10129m.f()) {
                throw new IOException("gzip finished without exhausting source");
            }
        }
        return -1L;
    }

    @Override // okio.Source
    /* JADX INFO: renamed from: timeout */
    public Timeout getF10138m() {
        return this.f10129m.getF10138m();
    }
}
