package g.i.a.c.h;

import android.content.Context;
import g.h.a.i;
import io.netty.handler.codec.dns.DnsRecord;
import io.netty.handler.codec.memcache.binary.BinaryMemcacheOpcodes;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.BufferUnderflowException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

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

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

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

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

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public boolean f5494o;

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

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

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public byte[] f5497r;

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

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

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public ArrayList<g.i.a.c.l.d.a> f5500u;

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

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public long f5502w;

    public a(Context context, String str, InputStream inputStream, long j2) throws IOException {
        super(inputStream);
        byte b2 = 1;
        this.f5493n = 1;
        if (available() < 44) {
            throw new IOException("The input file size is less to 44, please check!");
        }
        this.f5501v = str;
        this.f5502w = j2;
        h();
        int i2 = 0;
        this.f5499t = 0;
        this.f5500u = new ArrayList<>();
        byte[] bArr = this.f5497r;
        int length = bArr.length;
        for (byte b3 : bArr) {
            for (int i3 = 0; i3 < 8; i3++) {
                if (((byte) (((byte) (b3 >> i3)) & 1)) == 1) {
                    this.f5499t++;
                }
            }
        }
        long j3 = (((long) this.f5499t) * 12) + this.f5502w;
        byte[] bArr2 = this.f5497r;
        int length2 = bArr2.length;
        int i4 = 0;
        int i5 = 0;
        while (i5 < length2) {
            byte b4 = bArr2[i5];
            long j4 = j3;
            int i6 = i4;
            int i7 = i2;
            while (i7 < 8) {
                if (((byte) (((byte) (b4 >> i7)) & b2)) == b2) {
                    byte[] bArr3 = new byte[12];
                    read(bArr3, i2, 12);
                    int i8 = this.f5496q;
                    g.i.a.c.l.d.a aVar = new g.i.a.c.l.d.a(i8, 1, this.f5501v, i6, this.f5498s * 4, j4, (bArr3[i2] & 255) | ((bArr3[3] << BinaryMemcacheOpcodes.FLUSHQ) & (-16777216)) | ((bArr3[2] << 16) & 16711680) | ((bArr3[b2] << 8) & 65280), ((bArr3[7] << BinaryMemcacheOpcodes.FLUSHQ) & (-16777216)) | ((bArr3[6] << 16) & 16711680) | ((bArr3[5] << 8) & 65280) | (bArr3[4] & 255), 0);
                    try {
                        g.i.a.c.l.e.a aVarA = aVar.a(context, i8);
                        if (aVarA != null) {
                            aVarA.e();
                            aVar.f5558i = aVarA.f5575z;
                            aVar.f5559j = aVarA.f5570u;
                            aVar.f5560k = aVarA.H;
                            aVarA.close();
                        }
                    } catch (IOException | BufferUnderflowException e2) {
                        i.W3(e2.toString());
                    }
                    i.U3(aVar.toString());
                    this.f5500u.add(aVar);
                    j4 += (long) aVar.f5556g;
                }
                i6++;
                i7++;
                b2 = 1;
                i2 = 0;
            }
            i5++;
            i4 = i6;
            j3 = j4;
            b2 = 1;
            i2 = 0;
        }
        close();
    }

    public static a a(Context context, String str, InputStream inputStream, long j2) {
        a aVar = null;
        if (inputStream == null) {
            i.W3("InputStream can not be null");
            return null;
        }
        try {
            aVar = new a(context, str, inputStream, j2);
        } catch (IOException unused) {
        }
        try {
            inputStream.close();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        return aVar;
    }

    public static a c(String str) {
        a aVar = null;
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(str));
            try {
                aVar = new a(str, 0L, bufferedInputStream);
            } catch (IOException unused) {
            }
            try {
                bufferedInputStream.close();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
            try {
                bufferedInputStream.close();
            } catch (IOException e3) {
                e3.printStackTrace();
            }
        } catch (IOException unused2) {
        }
        return aVar;
    }

    public boolean d(int i2, int i3) {
        ArrayList<g.i.a.c.l.d.a> arrayList = this.f5500u;
        g.i.a.c.l.e.a aVarB = null;
        if (arrayList != null && arrayList.size() > 0) {
            Iterator<g.i.a.c.l.d.a> it = this.f5500u.iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                g.i.a.c.l.d.a next = it.next();
                if (next.f5552c == i2) {
                    aVarB = next.b(next.a);
                    break;
                }
            }
        }
        if (aVarB == null) {
            return false;
        }
        try {
            aVarB.close();
            return true;
        } catch (IOException e2) {
            e2.printStackTrace();
            return true;
        }
    }

    public g.i.a.c.l.d.a e(int i2) {
        for (g.i.a.c.l.d.a aVar : this.f5500u) {
            if (aVar.f5552c == i2) {
                return aVar;
            }
        }
        return null;
    }

    public List<g.i.a.c.l.d.a> g(int i2) {
        ArrayList arrayList = new ArrayList();
        ArrayList<g.i.a.c.l.d.a> arrayList2 = this.f5500u;
        if (arrayList2 != null && arrayList2.size() > 0) {
            int i3 = this.f5493n >= 2 ? 128 : 16;
            if (i2 == 1) {
                for (g.i.a.c.l.d.a aVar : this.f5500u) {
                    int i4 = aVar.f5552c;
                    if (i4 >= i3 || (this.f5496q == 11 && i4 == 24)) {
                        arrayList.add(aVar);
                    }
                }
            } else {
                for (g.i.a.c.l.d.a aVar2 : this.f5500u) {
                    if (aVar2.f5552c < i3) {
                        arrayList.add(aVar2);
                    }
                }
            }
        }
        return arrayList;
    }

    public final void h() throws IOException {
        byte[] bArr = new byte[40];
        read(bArr, 0, 40);
        boolean z2 = true;
        int i2 = (((bArr[1] << 8) & 65280) | (bArr[0] & 255)) & 65535;
        this.f5491f = i2;
        if (i2 != 19783) {
            throw new IOException(String.format("The signature(0x%04X) is not right", Integer.valueOf(this.f5491f)));
        }
        this.f5492m = ((bArr[5] << BinaryMemcacheOpcodes.FLUSHQ) & (-16777216)) | ((bArr[4] << 16) & 16711680) | ((bArr[3] << 8) & 65280) | (bArr[2] & 255);
        System.arraycopy(bArr, 6, new byte[32], 0, 32);
        int i3 = ((bArr[38] & 255) | (65280 & (bArr[39] << 8))) & 65535;
        int i4 = i3 & 15;
        this.f5493n = i4;
        this.f5495p = ((byte) ((i3 >> 7) & 1)) == 1;
        this.f5496q = (i3 >> 8) & DnsRecord.CLASS_ANY;
        if (i4 <= 1) {
            this.f5494o = true;
            this.f5498s = 4;
        } else {
            if (i4 != 2 && ((byte) ((i3 >> 6) & 1)) != 1) {
                z2 = false;
            }
            this.f5494o = z2;
            this.f5498s = 32;
        }
        int i5 = this.f5498s;
        byte[] bArr2 = new byte[i5];
        this.f5497r = bArr2;
        read(bArr2, 0, i5);
        this.f5502w += (long) (40 + this.f5498s);
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format(Locale.US, "size=0x%08x(%d)", Integer.valueOf(this.f5492m), Integer.valueOf(this.f5492m)) + String.format("packVersion=0x%02X, dualBank=%b", Integer.valueOf(this.f5493n), Boolean.valueOf(this.f5495p)) + String.format(", icType=0x%02X", Integer.valueOf(this.f5496q)) + String.format(", subFileNum=0x%04X, subFileIndicator=%s", Integer.valueOf(this.f5499t), i.y(this.f5497r)));
        return sb.toString();
    }

    public a(String str, long j2, InputStream inputStream) throws IOException {
        int i2;
        super(inputStream);
        byte b2 = 1;
        this.f5493n = 1;
        if (available() < 44) {
            throw new IOException("The input file size is less to 44, please check!");
        }
        this.f5501v = str;
        this.f5502w = j2;
        h();
        int i3 = 0;
        this.f5499t = 0;
        this.f5500u = new ArrayList<>();
        byte[] bArr = this.f5497r;
        int length = bArr.length;
        for (byte b3 : bArr) {
            for (int i4 = 0; i4 < 8; i4++) {
                if (((byte) (((byte) (b3 >> i4)) & 1)) == 1) {
                    this.f5499t++;
                }
            }
        }
        long j3 = (((long) this.f5499t) * 12) + this.f5502w;
        byte[] bArr2 = this.f5497r;
        int length2 = bArr2.length;
        int i5 = 0;
        int i6 = 0;
        while (i5 < length2) {
            byte b4 = bArr2[i5];
            int i7 = i3;
            while (i7 < 8) {
                if (((byte) (((byte) (b4 >> i7)) & b2)) == b2) {
                    byte[] bArr3 = new byte[12];
                    read(bArr3, i3, 12);
                    int i8 = this.f5496q;
                    i2 = i7;
                    g.i.a.c.l.d.a aVar = new g.i.a.c.l.d.a(i8, 0, this.f5501v, i6, this.f5498s * 4, j3, ((bArr3[3] << BinaryMemcacheOpcodes.FLUSHQ) & (-16777216)) | ((bArr3[2] << 16) & 16711680) | ((bArr3[b2] << 8) & 65280) | (bArr3[i3] & 255), ((bArr3[7] << BinaryMemcacheOpcodes.FLUSHQ) & (-16777216)) | ((bArr3[6] << 16) & 16711680) | ((bArr3[5] << 8) & 65280) | (bArr3[4] & 255), 0);
                    g.i.a.c.l.e.a aVarB = aVar.b(i8);
                    if (aVarB != null) {
                        aVarB.e();
                        aVar.f5558i = aVarB.f5575z;
                        aVar.f5559j = aVarB.f5570u;
                        aVar.f5560k = aVarB.H;
                        try {
                            aVarB.close();
                        } catch (Exception unused) {
                        }
                    }
                    this.f5500u.add(aVar);
                    j3 += (long) aVar.f5556g;
                } else {
                    i2 = i7;
                }
                i6++;
                i7 = i2 + 1;
                b2 = 1;
                i3 = 0;
            }
            i5++;
            b2 = 1;
            i3 = 0;
        }
        close();
    }
}
