package defpackage;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import org.tukaani.xz.CorruptedInputException;
import org.tukaani.xz.UnsupportedOptionsException;
import org.tukaani.xz.XZFormatException;
import org.tukaani.xz.XZIOException;

/* renamed from: ᵵᵼᵬ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C4758 extends InputStream {

    /* renamed from: ᵡⁱᵥ, reason: contains not printable characters */
    public final boolean f17668;

    /* renamed from: ᵢˇᵭ, reason: contains not printable characters */
    public final C0471 f17669;

    /* renamed from: ᵤᵙᵷ, reason: contains not printable characters */
    public final AbstractC2482 f17670;

    /* renamed from: ᵧᵾˀˈ, reason: contains not printable characters */
    public InputStream f17671;

    /* renamed from: ᵾᵬ, reason: contains not printable characters */
    public final C3160 f17673;

    /* renamed from: ᵿʗᵝʽ, reason: contains not printable characters */
    public final int f17674;

    /* renamed from: ˀᵗᵝᵭ, reason: contains not printable characters */
    public C2830 f17664 = null;

    /* renamed from: ᵡˎ, reason: contains not printable characters */
    public final C2539 f17667 = new C2539();

    /* renamed from: ˑᵥˁ, reason: contains not printable characters */
    public boolean f17666 = false;

    /* renamed from: ˎˑː, reason: contains not printable characters */
    public IOException f17665 = null;

    /* renamed from: ᵮᵠـˌ, reason: contains not printable characters */
    public final byte[] f17672 = new byte[1];

    public C4758(InputStream inputStream, int i, boolean z, byte[] bArr, C0471 c0471) throws CorruptedInputException, UnsupportedOptionsException, XZFormatException {
        AbstractC2482 c5317;
        this.f17669 = c0471;
        this.f17671 = inputStream;
        this.f17674 = i;
        this.f17668 = z;
        int i2 = 0;
        while (true) {
            byte[] bArr2 = ˆᴵ.ᵢˇᵭ;
            if (i2 >= 6) {
                if (!ᵿᵧ.ᵾᵬ(6, 2, 8, bArr)) {
                    throw new CorruptedInputException("XZ Stream Header is corrupt");
                }
                try {
                    C3160 c3160 = ᵿᵧ.ᵹᵝᵶ(6, bArr);
                    this.f17673 = c3160;
                    int i3 = c3160.f11993;
                    if (i3 == 0) {
                        c5317 = new C5317();
                        c5317.f9701 = 0;
                        c5317.f9702 = "None";
                    } else if (i3 == 1) {
                        c5317 = new C2499(0);
                    } else {
                        if (i3 != 4) {
                            if (i3 == 10) {
                                try {
                                    c5317 = new C2499(1);
                                } catch (NoSuchAlgorithmException unused) {
                                }
                            }
                            throw new UnsupportedOptionsException(AbstractC2990.m7313(i3, "Unsupported Check ID "));
                        }
                        C3602 c3602 = new C3602();
                        c3602.f13436 = -1L;
                        c3602.f9701 = 8;
                        c3602.f9702 = "CRC64";
                        c5317 = c3602;
                    }
                    this.f17670 = c5317;
                    return;
                } catch (UnsupportedOptionsException unused2) {
                    throw new UnsupportedOptionsException("Unsupported options in XZ Stream Header");
                }
            }
            if (bArr[i2] != bArr2[i2]) {
                throw new XZFormatException("Input is not in the XZ format");
            }
            i2++;
        }
    }

    @Override // java.io.InputStream
    public final int available() throws IOException {
        if (this.f17671 == null) {
            throw new XZIOException("Stream closed");
        }
        IOException iOException = this.f17665;
        if (iOException != null) {
            throw iOException;
        }
        C2830 c2830 = this.f17664;
        if (c2830 == null) {
            return 0;
        }
        return c2830.f10904.available();
    }

    @Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
    public final void close() throws IOException {
        m10043(true);
    }

    @Override // java.io.InputStream
    public final int read() {
        byte[] bArr = this.f17672;
        if (read(bArr, 0, 1) == -1) {
            return -1;
        }
        return bArr[0] & 255;
    }

    @Override // java.io.InputStream
    public final int read(byte[] bArr, int i, int i2) throws IOException {
        int i3;
        if (i < 0 || i2 < 0 || (i3 = i + i2) < 0 || i3 > bArr.length) {
            C0190.m1431();
            return 0;
        }
        if (i2 == 0) {
            return 0;
        }
        if (this.f17671 == null) {
            throw new XZIOException("Stream closed");
        }
        IOException iOException = this.f17665;
        if (iOException != null) {
            throw iOException;
        }
        if (this.f17666) {
            return -1;
        }
        int i4 = 0;
        while (i2 > 0) {
            try {
                C2830 c2830 = this.f17664;
                C2539 c2539 = this.f17667;
                if (c2830 == null) {
                    try {
                        this.f17664 = new C2830(this.f17671, this.f17670, this.f17668, this.f17674, this.f17669);
                    } catch (C4187 unused) {
                        c2539.m6395(this.f17671);
                        m10042();
                        this.f17666 = true;
                        if (i4 > 0) {
                            return i4;
                        }
                        return -1;
                    }
                }
                int i5 = this.f17664.read(bArr, i, i2);
                if (i5 > 0) {
                    i4 += i5;
                    i += i5;
                    i2 -= i5;
                } else if (i5 == -1) {
                    C2830 c28302 = this.f17664;
                    c2539.m6394(c28302.f10895 + c28302.f10898.f10626 + c28302.f10903.f9701, c28302.f10894);
                    this.f17664 = null;
                }
            } catch (IOException e) {
                this.f17665 = e;
                if (i4 == 0) {
                    throw e;
                }
            }
        }
        return i4;
    }

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final void m10042() throws IOException {
        long j;
        byte[] bArr = new byte[12];
        new DataInputStream(this.f17671).readFully(bArr);
        byte b = bArr[10];
        byte[] bArr2 = ˆᴵ.ᵿʗᵝʽ;
        int i = 0;
        if (b != bArr2[0] || bArr[11] != bArr2[1]) {
            throw new CorruptedInputException("XZ Stream Footer is corrupt");
        }
        if (!ᵿᵧ.ᵾᵬ(4, 6, 0, bArr)) {
            throw new CorruptedInputException("XZ Stream Footer is corrupt");
        }
        try {
            C3160 c3160 = ᵿᵧ.ᵹᵝᵶ(8, bArr);
            c3160.f11992 = 0L;
            while (true) {
                j = c3160.f11992;
                if (i >= 4) {
                    break;
                }
                c3160.f11992 = j | ((bArr[i + 4] & 255) << (i * 8));
                i++;
            }
            c3160.f11992 = (j + 1) * 4;
            if (this.f17673.f11993 == c3160.f11993) {
                if (((ᵿᵧ.ᵢˇᵭ(r9.f9895) + 1 + this.f17667.f9900 + 7) & (-4)) == c3160.f11992) {
                    return;
                }
            }
            throw new CorruptedInputException("XZ Stream Footer does not match Stream Header");
        } catch (UnsupportedOptionsException unused) {
            throw new UnsupportedOptionsException("Unsupported options in XZ Stream Footer");
        }
    }

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final void m10043(boolean z) throws IOException {
        if (this.f17671 != null) {
            C2830 c2830 = this.f17664;
            if (c2830 != null) {
                c2830.close();
                this.f17664 = null;
            }
            if (z) {
                try {
                    this.f17671.close();
                } finally {
                    this.f17671 = null;
                }
            }
        }
    }
}
