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;

/* JADX INFO: renamed from: ᵻᵢˇ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C5217 extends InputStream {

    /* JADX INFO: renamed from: ˌᴵ, reason: contains not printable characters */
    public InputStream f19647;

    /* JADX INFO: renamed from: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public final int f19650;

    /* JADX INFO: renamed from: ᵜᵪʾ, reason: contains not printable characters */
    public final C2470 f19651;

    /* JADX INFO: renamed from: ᵨᵼⁱ, reason: contains not printable characters */
    public final C0655 f19652;

    /* JADX INFO: renamed from: ᵯᵷʽ, reason: contains not printable characters */
    public final AbstractC3852 f19653;

    /* JADX INFO: renamed from: ᵶˇᵱ, reason: contains not printable characters */
    public final boolean f19654;

    /* JADX INFO: renamed from: ˈᵲᵢ, reason: contains not printable characters */
    public C0101 f19646 = null;

    /* JADX INFO: renamed from: ˌᵨʠᵫ, reason: contains not printable characters */
    public final C4528 f19648 = new C4528();

    /* JADX INFO: renamed from: ᵸˆˁ, reason: contains not printable characters */
    public boolean f19655 = false;

    /* JADX INFO: renamed from: ˑˊᵲ, reason: contains not printable characters */
    public IOException f19649 = null;

    /* JADX INFO: renamed from: ʿٴˇ, reason: contains not printable characters */
    public final byte[] f19645 = new byte[1];

    public C5217(InputStream inputStream, int i, boolean z, byte[] bArr, C2470 c2470) throws CorruptedInputException, UnsupportedOptionsException, XZFormatException {
        AbstractC3852 c1432;
        this.f19651 = c2470;
        this.f19647 = inputStream;
        this.f19650 = i;
        this.f19654 = 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 {
                    C0655 c0655 = ᵽˊᵘᵬ.ᵻˈᵦʼ(6, bArr);
                    this.f19652 = c0655;
                    int i3 = c0655.f3092;
                    if (i3 == 0) {
                        c1432 = new C1432();
                        c1432.f14716 = 0;
                        c1432.f14715 = "None";
                    } else if (i3 == 1) {
                        c1432 = new C3406(0);
                    } else {
                        if (i3 != 4) {
                            if (i3 == 10) {
                                try {
                                    c1432 = new C3406(1);
                                } catch (NoSuchAlgorithmException unused) {
                                }
                            }
                            throw new UnsupportedOptionsException(AbstractC2704.m6517(i3, "Unsupported Check ID "));
                        }
                        C4481 c4481 = new C4481();
                        c4481.f17098 = -1L;
                        c4481.f14716 = 8;
                        c4481.f14715 = "CRC64";
                        c1432 = c4481;
                    }
                    this.f19653 = c1432;
                    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.f19647 == null) {
            throw new XZIOException("Stream closed");
        }
        IOException iOException = this.f19649;
        if (iOException != null) {
            throw iOException;
        }
        C0101 c0101 = this.f19646;
        if (c0101 == null) {
            return 0;
        }
        return c0101.f1369.available();
    }

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

    @Override // java.io.InputStream
    public final int read() {
        byte[] bArr = this.f19645;
        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) {
            C4468.m9514();
            return 0;
        }
        if (i2 == 0) {
            return 0;
        }
        if (this.f19647 == null) {
            throw new XZIOException("Stream closed");
        }
        IOException iOException = this.f19649;
        if (iOException != null) {
            throw iOException;
        }
        if (this.f19655) {
            return -1;
        }
        int i4 = 0;
        while (i2 > 0) {
            try {
                C0101 c0101 = this.f19646;
                C4528 c4528 = this.f19648;
                if (c0101 == null) {
                    try {
                        this.f19646 = new C0101(this.f19647, this.f19653, this.f19654, this.f19650, this.f19651);
                    } catch (C0276 unused) {
                        c4528.m9569(this.f19647);
                        m10750();
                        this.f19655 = true;
                        if (i4 > 0) {
                            return i4;
                        }
                        return -1;
                    }
                }
                int i5 = this.f19646.read(bArr, i, i2);
                if (i5 > 0) {
                    i4 += i5;
                    i += i5;
                    i2 -= i5;
                } else if (i5 == -1) {
                    C0101 c01012 = this.f19646;
                    c4528.m9570(((long) c01012.f1375) + c01012.f1370.f9156 + ((long) c01012.f1372.f14716), c01012.f1368);
                    this.f19646 = null;
                }
            } catch (IOException e) {
                this.f19649 = e;
                if (i4 == 0) {
                    throw e;
                }
            }
        }
        return i4;
    }

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final void m10750() throws IOException {
        long j;
        byte[] bArr = new byte[12];
        new DataInputStream(this.f19647).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 {
            C0655 c0655 = ᵽˊᵘᵬ.ᵻˈᵦʼ(8, bArr);
            c0655.f3093 = 0L;
            while (true) {
                j = c0655.f3093;
                if (i >= 4) {
                    break;
                }
                c0655.f3093 = j | ((long) ((bArr[i + 4] & 255) << (i * 8)));
                i++;
            }
            c0655.f3093 = (j + 1) * 4;
            if (this.f19652.f3092 == c0655.f3092) {
                C4528 c4528 = this.f19648;
                if (((((long) (ᵽˊᵘᵬ.ᵝᵶ(c4528.f17230) + 1)) + c4528.f17232 + 7) & (-4)) == c0655.f3093) {
                    return;
                }
            }
            throw new CorruptedInputException("XZ Stream Footer does not match Stream Header");
        } catch (UnsupportedOptionsException unused) {
            throw new UnsupportedOptionsException("Unsupported options in XZ Stream Footer");
        }
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final void m10751(boolean z) {
        if (this.f19647 != null) {
            C0101 c0101 = this.f19646;
            if (c0101 != null) {
                c0101.close();
                this.f19646 = null;
            }
            if (z) {
                try {
                    this.f19647.close();
                } finally {
                    this.f19647 = null;
                }
            }
        }
    }
}
