package g.i.a.c.f;

import android.content.Context;
import com.cvte.vman.instance.InvokeCmd;
import g.h.a.i;
import g.i.a.c.l.c;
import io.netty.handler.codec.memcache.binary.BinaryMemcacheOpcodes;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Locale;

/* JADX INFO: loaded from: classes.dex */
public class b extends BufferedInputStream {

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public int f5487m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public int f5488n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public ArrayList<a> f5489o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public String f5490p;

    public b(String str, InputStream inputStream) throws IOException {
        super(inputStream);
        if (available() < 12) {
            throw new IOException("The input file size is less to 12, please check!");
        }
        this.f5490p = str;
    }

    public static b c(c cVar) {
        InputStream bufferedInputStream;
        b bVar = null;
        if (cVar.f5520g == 1) {
            Context context = cVar.a;
            if (context == null) {
                i.w0("please set the context first.");
                return null;
            }
            try {
                bufferedInputStream = context.getAssets().open(cVar.f5521h);
            } catch (IOException e2) {
                i.W3(e2.toString());
                return null;
            }
        } else {
            try {
                bufferedInputStream = new BufferedInputStream(new FileInputStream(cVar.f5521h));
            } catch (FileNotFoundException e3) {
                i.W3(e3.toString());
                bufferedInputStream = null;
            }
        }
        if (bufferedInputStream == null) {
            return null;
        }
        try {
            b bVar2 = new b(cVar.f5521h, bufferedInputStream);
            if (bVar2.d()) {
                bVar = bVar2;
            } else {
                bVar2.close();
            }
            try {
                bufferedInputStream.close();
            } catch (IOException e4) {
                e4.printStackTrace();
            }
            return bVar;
        } catch (IOException e5) {
            i.U3(e5.toString());
            return null;
        }
    }

    public static g.i.a.c.h.a e(c cVar) throws g.i.a.c.g.b {
        g.i.a.c.h.a aVarA;
        Context context = cVar.a;
        String str = cVar.f5521h;
        b bVarC = c(cVar);
        InputStream inputStreamOpen = null;
        if (bVarC == null) {
            i.V3(false, "multiPackManager == null");
            try {
                aVarA = g.i.a.c.h.a.a(context, str, context.getAssets().open(str), 0L);
                if (aVarA == null) {
                    if (!((cVar.f5518e & 1) == 1)) {
                        i.W3("not support single file.");
                        throw new g.i.a.c.g.b("not support single file.", 4108);
                    }
                } else {
                    if (!aVarA.f5494o) {
                        i.W3("pack for MP can not be OTA");
                        throw new g.i.a.c.g.b("not support pack file.", InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_HDCP_CAPTURE);
                    }
                    if (!((cVar.f5518e & 2) == 2)) {
                        i.W3("not support pack file.");
                        throw new g.i.a.c.g.b("not support pack file.", 4107);
                    }
                }
            } catch (IOException e2) {
                i.W3(e2.toString());
                return null;
            }
        } else {
            if (!((cVar.f5518e & 4) == 4)) {
                i.W3("not support combine pack file.");
                throw new g.i.a.c.g.b("not support combine pack file.", 4103);
            }
            int i2 = cVar.f5519f;
            i.U3(bVarC.toString());
            a aVarA2 = bVarC.a(i2);
            if (aVarA2 == null) {
                i.W3("no bud item exist, preferredBudRole=" + i2);
                throw new g.i.a.c.g.b("no combine bud item exist.", 4105);
            }
            i.U3(aVarA2.toString());
            try {
                inputStreamOpen = context.getAssets().open(aVarA2.a);
                inputStreamOpen.skip(aVarA2.f5483b);
            } catch (FileNotFoundException e3) {
                i.W3(String.format(Locale.US, "FileNotFoundException:%s, %d", aVarA2.a, Long.valueOf(aVarA2.f5483b)));
                e3.printStackTrace();
            } catch (IOException e4) {
                e4.printStackTrace();
            }
            aVarA = g.i.a.c.h.a.a(context, str, inputStreamOpen, aVarA2.f5483b);
            if (aVarA == null) {
                i.W3("sub combined file must be a pack file, preferredBudRole=" + i2);
                throw new g.i.a.c.g.b("sub combined file must be a pack file.", 4106);
            }
            if (!aVarA.f5494o) {
                i.W3("pack for MP can not be OTA");
                throw new g.i.a.c.g.b("not support pack file.", InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_HDCP_CAPTURE);
            }
        }
        return aVarA;
    }

    public static g.i.a.c.h.a g(c cVar) throws g.i.a.c.g.b {
        FileInputStream fileInputStream;
        g.i.a.c.h.a aVar;
        g.i.a.c.h.a aVarC;
        String str = cVar.f5521h;
        b bVarC = c(cVar);
        if (bVarC == null) {
            i.V3(false, "multiPackManager == null");
            aVarC = g.i.a.c.h.a.c(str);
            if (aVarC == null) {
                if (!((cVar.f5518e & 1) == 1)) {
                    i.W3("not support single file.");
                    throw new g.i.a.c.g.b("not support single file.", 4108);
                }
                return aVarC;
            }
            if (!aVarC.f5494o) {
                i.W3("pack for MP can not be OTA");
                throw new g.i.a.c.g.b("not support pack file.", InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_HDCP_CAPTURE);
            }
            if (!((cVar.f5518e & 2) == 2)) {
                i.W3("not support pack file.");
                throw new g.i.a.c.g.b("not support pack file.", 4107);
            }
        } else {
            if (!((cVar.f5518e & 4) == 4)) {
                i.W3("not support combine pack file.");
                throw new g.i.a.c.g.b("not support combine pack file.", 4103);
            }
            int i2 = cVar.f5519f;
            i.V3(false, bVarC.toString());
            a aVarA = bVarC.a(i2);
            if (aVarA == null) {
                i.W3("no bud item exist, preferredBudRole=" + i2);
                throw new g.i.a.c.g.b("no combine bud item exist.", 4105);
            }
            i.V3(false, aVarA.toString());
            long j2 = aVarA.f5483b;
            try {
                fileInputStream = new FileInputStream(aVarA.a);
                try {
                    fileInputStream.skip(aVarA.f5483b);
                } catch (FileNotFoundException e2) {
                    e = e2;
                    i.W3(String.format(Locale.US, "FileNotFoundException:%s, %d", aVarA.a, Long.valueOf(aVarA.f5483b)));
                    e.printStackTrace();
                    fileInputStream = fileInputStream;
                } catch (IOException e3) {
                    e = e3;
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e4) {
                e = e4;
                fileInputStream = null;
            } catch (IOException e5) {
                e = e5;
                fileInputStream = null;
            }
            try {
                aVar = new g.i.a.c.h.a(str, j2, fileInputStream);
            } catch (IOException unused) {
                aVar = null;
            }
            try {
                fileInputStream.close();
            } catch (IOException e6) {
                e6.printStackTrace();
            }
            if (aVar == null) {
                i.W3("sub combined file must be a pack file, preferredBudRole=" + i2);
                throw new g.i.a.c.g.b("sub combined file must be a pack file.", 4106);
            }
            if (!aVar.f5494o) {
                i.W3("pack for MP can not be OTA");
                throw new g.i.a.c.g.b("not support pack file.", InvokeCmd.CMD_EXTENDDISPLAY_CTRL_GET_HDCP_CAPTURE);
            }
            aVarC = aVar;
        }
        i.V3(false, aVarC.toString());
        return aVarC;
    }

    public a a(int i2) {
        ArrayList<a> arrayList = this.f5489o;
        if (arrayList == null || arrayList.size() <= 0) {
            return null;
        }
        for (a aVar : this.f5489o) {
            if (aVar.f5485d == i2) {
                return aVar;
            }
        }
        return null;
    }

    public final boolean d() throws IOException {
        byte[] bArr = new byte[12];
        int i2 = 0;
        read(bArr, 0, 12);
        int i3 = ((bArr[3] << BinaryMemcacheOpcodes.FLUSHQ) & (-16777216)) | ((bArr[2] << 16) & 16711680) | ((bArr[1] << 8) & 65280) | (bArr[0] & 255);
        this.f5486f = i3;
        if (i3 != -1768442424) {
            i.V3(false, String.format("invalid multi-pack signature(0x%08X) ", Integer.valueOf(i3)));
            return false;
        }
        this.f5487m = ((bArr[7] << BinaryMemcacheOpcodes.FLUSHQ) & (-16777216)) | ((bArr[6] << 16) & 16711680) | ((bArr[5] << 8) & 65280) | (bArr[4] & 255);
        this.f5488n = (bArr[8] & 255) | ((bArr[11] << BinaryMemcacheOpcodes.FLUSHQ) & (-16777216)) | ((bArr[10] << 16) & 16711680) | ((bArr[9] << 8) & 65280);
        this.f5489o = new ArrayList<>();
        int i4 = (this.f5488n * 8) + 12;
        byte[] bArr2 = new byte[8];
        int i5 = 0;
        while (i5 < this.f5488n) {
            read(bArr2, i2, 8);
            a aVar = new a(this.f5490p, i4, ((bArr2[3] << BinaryMemcacheOpcodes.FLUSHQ) & (-16777216)) | ((bArr2[2] << 16) & 16711680) | ((bArr2[1] << 8) & 65280) | (bArr2[i2] & 255), bArr2[4] & 1);
            i.U3(aVar.toString());
            this.f5489o.add(aVar);
            i4 += aVar.f5484c;
            i5++;
            i2 = 0;
        }
        return true;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        StringBuilder sbF = g.a.a.a.a.F("Multi-pack: signature=");
        sbF.append(String.format("0x%04X", Integer.valueOf(this.f5486f)));
        sbF.append(", version=");
        sbF.append(String.format(Locale.US, "0x%08x(%d)", Integer.valueOf(this.f5487m), Integer.valueOf(this.f5487m)));
        sbF.append(", num=");
        sbF.append(String.format("0x%04x", Integer.valueOf(this.f5488n)));
        sb.append(sbF.toString());
        ArrayList<a> arrayList = this.f5489o;
        if (arrayList != null && arrayList.size() > 0) {
            for (a aVar : this.f5489o) {
                StringBuilder sbF2 = g.a.a.a.a.F("\n");
                sbF2.append(aVar.toString());
                sb.append(sbF2.toString());
            }
        }
        return sb.toString();
    }
}
