package h1;

import C3.D;
import C3.G;
import C3.W;
import T0.H;
import c1.C0576a;
import d5.AbstractC0844a;
import e4.C0878i;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import n4.u0;
import u0.E;
import x0.AbstractC1709a;
import x0.v;

/* JADX INFO: loaded from: classes.dex */
public final class h extends u0 {

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public static final C0878i f13336w = new C0878i(2);

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public final C0878i f13337v;

    public h(C0878i c0878i) {
        this.f13337v = c0878i;
    }

    public static C1026a D(x0.o oVar, int i, int i5) {
        int iV;
        String strConcat;
        int iT = oVar.t();
        Charset charsetS = S(iT);
        int i8 = i - 1;
        byte[] bArr = new byte[i8];
        oVar.e(bArr, 0, i8);
        if (i5 == 2) {
            strConcat = "image/" + s7.d.F(new String(bArr, 0, 3, StandardCharsets.ISO_8859_1));
            if ("image/jpg".equals(strConcat)) {
                strConcat = "image/jpeg";
            }
            iV = 2;
        } else {
            iV = V(0, bArr);
            String strF = s7.d.F(new String(bArr, 0, iV, StandardCharsets.ISO_8859_1));
            strConcat = strF.indexOf(47) == -1 ? "image/".concat(strF) : strF;
        }
        int i9 = bArr[iV + 1] & 255;
        int i10 = iV + 2;
        int iU = U(bArr, i10, iT);
        String str = new String(bArr, i10, iU - i10, charsetS);
        int iR = R(iT) + iU;
        return new C1026a(strConcat, str, i9, i8 <= iR ? v.f18555c : Arrays.copyOfRange(bArr, iR, i8));
    }

    public static c E(x0.o oVar, int i, int i5, boolean z8, int i8, C0878i c0878i) throws Throwable {
        int i9 = oVar.f18540b;
        int iV = V(i9, oVar.f18539a);
        String str = new String(oVar.f18539a, i9, iV - i9, StandardCharsets.ISO_8859_1);
        oVar.F(iV + 1);
        int iG = oVar.g();
        int iG2 = oVar.g();
        long jV = oVar.v();
        if (jV == 4294967295L) {
            jV = -1;
        }
        long jV2 = oVar.v();
        long j8 = jV2 == 4294967295L ? -1L : jV2;
        ArrayList arrayList = new ArrayList();
        int i10 = i9 + i;
        while (oVar.f18540b < i10) {
            i iVarH = H(i5, oVar, z8, i8, c0878i);
            if (iVarH != null) {
                arrayList.add(iVarH);
            }
        }
        return new c(str, iG, iG2, jV, j8, (i[]) arrayList.toArray(new i[0]));
    }

    public static d F(x0.o oVar, int i, int i5, boolean z8, int i8, C0878i c0878i) throws Throwable {
        int i9 = oVar.f18540b;
        int iV = V(i9, oVar.f18539a);
        String str = new String(oVar.f18539a, i9, iV - i9, StandardCharsets.ISO_8859_1);
        oVar.F(iV + 1);
        int iT = oVar.t();
        boolean z9 = (iT & 2) != 0;
        boolean z10 = (iT & 1) != 0;
        int iT2 = oVar.t();
        String[] strArr = new String[iT2];
        for (int i10 = 0; i10 < iT2; i10++) {
            int i11 = oVar.f18540b;
            int iV2 = V(i11, oVar.f18539a);
            strArr[i10] = new String(oVar.f18539a, i11, iV2 - i11, StandardCharsets.ISO_8859_1);
            oVar.F(iV2 + 1);
        }
        ArrayList arrayList = new ArrayList();
        int i12 = i9 + i;
        while (oVar.f18540b < i12) {
            i iVarH = H(i5, oVar, z8, i8, c0878i);
            if (iVarH != null) {
                arrayList.add(iVarH);
            }
        }
        return new d(str, z9, z10, strArr, (i[]) arrayList.toArray(new i[0]));
    }

    public static e G(int i, x0.o oVar) {
        if (i < 4) {
            return null;
        }
        int iT = oVar.t();
        Charset charsetS = S(iT);
        byte[] bArr = new byte[3];
        oVar.e(bArr, 0, 3);
        String str = new String(bArr, 0, 3);
        int i5 = i - 4;
        byte[] bArr2 = new byte[i5];
        oVar.e(bArr2, 0, i5);
        int iU = U(bArr2, 0, iT);
        String str2 = new String(bArr2, 0, iU, charsetS);
        int iR = R(iT) + iU;
        return new e(str, str2, L(bArr2, iR, U(bArr2, iR, iT), charsetS));
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:161:0x01bc  */
    /* JADX WARN: Removed duplicated region for block: B:183:0x01f9  */
    /* JADX WARN: Removed duplicated region for block: B:191:0x020e A[Catch: all -> 0x012e, Exception -> 0x0131, OutOfMemoryError -> 0x0134, TRY_LEAVE, TryCatch #3 {Exception -> 0x0131, OutOfMemoryError -> 0x0134, all -> 0x012e, blocks: (B:107:0x0128, B:115:0x0139, B:122:0x014f, B:124:0x0157, B:132:0x0171, B:141:0x0189, B:152:0x01a4, B:159:0x01b6, B:182:0x01f4, B:190:0x0209, B:191:0x020e), top: B:205:0x011e }] */
    /* JADX WARN: Removed duplicated region for block: B:198:0x0230  */
    /* JADX WARN: Type inference failed for: r0v32 */
    /* JADX WARN: Type inference failed for: r0v44 */
    /* JADX WARN: Type inference failed for: r0v8, types: [java.lang.Throwable] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static i H(int i, x0.o oVar, boolean z8, int i5, C0878i c0878i) throws Throwable {
        int iX;
        i iVar;
        boolean z9;
        boolean z10;
        boolean z11;
        boolean z12;
        boolean z13;
        Object obj;
        i iVar2;
        ?? r02;
        i c1027b;
        boolean z14;
        int i8 = i;
        x0.o oVar2 = oVar;
        int iT = oVar2.t();
        int iT2 = oVar2.t();
        int iT3 = oVar2.t();
        int iT4 = i8 >= 3 ? oVar2.t() : 0;
        if (i8 == 4) {
            iX = oVar2.x();
            if (!z8) {
                iX = (((iX >> 24) & 255) << 21) | (iX & 255) | (((iX >> 8) & 255) << 7) | (((iX >> 16) & 255) << 14);
            }
        } else {
            iX = i8 == 3 ? oVar2.x() : oVar2.w();
        }
        int iZ = i8 >= 3 ? oVar2.z() : 0;
        if (iT == 0 && iT2 == 0 && iT3 == 0 && iT4 == 0 && iX == 0 && iZ == 0) {
            oVar2.F(oVar2.f18541c);
            return null;
        }
        int i9 = oVar2.f18540b + iX;
        if (i9 > oVar2.f18541c) {
            AbstractC1709a.B("Id3Decoder", "Frame size exceeds remaining tag data");
            oVar2.F(oVar2.f18541c);
            return null;
        }
        if (c0878i != null) {
            switch (c0878i.f12462q) {
                case 2:
                    iVar = null;
                    break;
                default:
                    iVar = null;
                    z14 = (iT == 67 && iT2 == 79 && iT3 == 77 && (iT4 == 77 || i8 == 2)) || (iT == 77 && iT2 == 76 && iT3 == 76 && (iT4 == 84 || i8 == 2));
                    break;
            }
            if (!z14) {
                oVar2.F(i9);
                return iVar;
            }
        } else {
            iVar = null;
        }
        if (i8 == 3) {
            z9 = (iZ & 128) != 0;
            z12 = (iZ & 64) != 0;
            z13 = false;
            z11 = (iZ & 32) != 0;
            z10 = z9;
        } else if (i8 == 4) {
            boolean z15 = (iZ & 64) != 0;
            boolean z16 = (iZ & 8) != 0;
            z12 = (iZ & 4) != 0;
            z13 = (iZ & 2) != 0;
            z10 = (iZ & 1) != 0;
            boolean z17 = z16;
            z11 = z15;
            z9 = z17;
        } else {
            z9 = false;
            z10 = false;
            z11 = false;
            z12 = false;
            z13 = false;
        }
        if (z9 || z12) {
            AbstractC1709a.B("Id3Decoder", "Skipping unsupported compressed or encrypted frame");
            oVar2.F(i9);
            return iVar;
        }
        if (z11) {
            iX--;
            oVar2.G(1);
        }
        if (z10) {
            iX -= 4;
            oVar2.G(4);
        }
        if (z13) {
            iX = W(iX, oVar2);
        }
        int i10 = iX;
        try {
        } catch (Exception e8) {
            obj = e8;
        } catch (OutOfMemoryError e9) {
            obj = e9;
        } catch (Throwable th) {
            th = th;
        }
        if (iT == 84 && iT2 == 88 && iT3 == 88 && (i8 == 2 || iT4 == 88)) {
            c1027b = O(i10, oVar2);
        } else if (iT == 84) {
            c1027b = M(i10, T(i8, iT, iT2, iT3, iT4), oVar2);
        } else if (iT == 87 && iT2 == 88 && iT3 == 88 && (i8 == 2 || iT4 == 88)) {
            c1027b = Q(i10, oVar2);
        } else if (iT == 87) {
            c1027b = P(i10, T(i8, iT, iT2, iT3, iT4), oVar2);
        } else if (iT == 80 && iT2 == 82 && iT3 == 73 && iT4 == 86) {
            c1027b = K(i10, oVar2);
        } else {
            if (iT != 71 || iT2 != 69 || iT3 != 79 || (iT4 != 66 && i8 != 2)) {
                if (i8 == 2) {
                    if (iT == 80 && iT2 == 73 && iT3 == 67) {
                        c1027b = D(oVar2, i10, i8);
                    }
                    if (iT != 67 && iT2 == 79 && iT3 == 77 && (iT4 == 77 || i8 == 2)) {
                        c1027b = G(i10, oVar2);
                    } else if (iT != 67 && iT2 == 72 && iT3 == 65 && iT4 == 80) {
                        try {
                            c1027b = E(oVar2, i10, i8, z8, i5, c0878i);
                            i8 = i;
                            oVar2 = oVar;
                            oVar2.F(i9);
                            iVar2 = c1027b;
                            r02 = iVar;
                        } catch (Exception e10) {
                            e = e10;
                            i8 = i;
                            oVar2 = oVar;
                            obj = e;
                            oVar2.F(i9);
                            iVar2 = iVar;
                            r02 = obj;
                        } catch (OutOfMemoryError e11) {
                            e = e11;
                            i8 = i;
                            oVar2 = oVar;
                            obj = e;
                            oVar2.F(i9);
                            iVar2 = iVar;
                            r02 = obj;
                        } catch (Throwable th2) {
                            th = th2;
                            oVar2 = oVar;
                            oVar2.F(i9);
                            throw th;
                        }
                    } else if (iT != 67 && iT2 == 84 && iT3 == 79 && iT4 == 67) {
                        i8 = i;
                        oVar2 = oVar;
                        c1027b = F(oVar2, i10, i8, z8, i5, c0878i);
                    } else {
                        i8 = i;
                        oVar2 = oVar;
                        if (iT != 77 && iT2 == 76 && iT3 == 76 && iT4 == 84) {
                            c1027b = J(i10, oVar2);
                        } else {
                            String strT = T(i8, iT, iT2, iT3, iT4);
                            byte[] bArr = new byte[i10];
                            oVar2.e(bArr, 0, i10);
                            c1027b = new C1027b(strT, bArr);
                        }
                    }
                } else if (iT == 65 && iT2 == 80 && iT3 == 73 && iT4 == 67) {
                    c1027b = D(oVar2, i10, i8);
                } else if (iT != 67) {
                    if (iT != 67) {
                    }
                    if (iT != 67) {
                        i8 = i;
                        oVar2 = oVar;
                        if (iT != 77) {
                            String strT2 = T(i8, iT, iT2, iT3, iT4);
                            byte[] bArr2 = new byte[i10];
                            oVar2.e(bArr2, 0, i10);
                            c1027b = new C1027b(strT2, bArr2);
                        }
                    }
                }
                if (iVar2 == null) {
                    AbstractC1709a.C("Id3Decoder", "Failed to decode frame: id=" + T(i8, iT, iT2, iT3, iT4) + ", frameSize=" + i10, r02);
                }
                return iVar2;
            }
            c1027b = I(i10, oVar2);
        }
        oVar2.F(i9);
        iVar2 = c1027b;
        r02 = iVar;
        if (iVar2 == null) {
        }
        return iVar2;
    }

    public static f I(int i, x0.o oVar) {
        int iT = oVar.t();
        Charset charsetS = S(iT);
        int i5 = i - 1;
        byte[] bArr = new byte[i5];
        oVar.e(bArr, 0, i5);
        int iV = V(0, bArr);
        String strM = E.m(new String(bArr, 0, iV, StandardCharsets.ISO_8859_1));
        int i8 = iV + 1;
        int iU = U(bArr, i8, iT);
        String strL = L(bArr, i8, iU, charsetS);
        int iR = R(iT) + iU;
        int iU2 = U(bArr, iR, iT);
        String strL2 = L(bArr, iR, iU2, charsetS);
        int iR2 = R(iT) + iU2;
        return new f(strM, strL, strL2, i5 <= iR2 ? v.f18555c : Arrays.copyOfRange(bArr, iR2, i5));
    }

    public static l J(int i, x0.o oVar) {
        int iZ = oVar.z();
        int iW = oVar.w();
        int iW2 = oVar.w();
        int iT = oVar.t();
        int iT2 = oVar.t();
        H h8 = new H();
        h8.p(oVar);
        int i5 = ((i - 10) * 8) / (iT + iT2);
        int[] iArr = new int[i5];
        int[] iArr2 = new int[i5];
        for (int i8 = 0; i8 < i5; i8++) {
            int i9 = h8.i(iT);
            int i10 = h8.i(iT2);
            iArr[i8] = i9;
            iArr2[i8] = i10;
        }
        return new l(iZ, iW, iW2, iArr, iArr2);
    }

    public static m K(int i, x0.o oVar) {
        byte[] bArr = new byte[i];
        oVar.e(bArr, 0, i);
        int iV = V(0, bArr);
        String str = new String(bArr, 0, iV, StandardCharsets.ISO_8859_1);
        int i5 = iV + 1;
        return new m(str, i <= i5 ? v.f18555c : Arrays.copyOfRange(bArr, i5, i));
    }

    public static String L(byte[] bArr, int i, int i5, Charset charset) {
        return (i5 <= i || i5 > bArr.length) ? "" : new String(bArr, i, i5 - i, charset);
    }

    public static n M(int i, String str, x0.o oVar) {
        if (i < 1) {
            return null;
        }
        int iT = oVar.t();
        int i5 = i - 1;
        byte[] bArr = new byte[i5];
        oVar.e(bArr, 0, i5);
        return new n(str, null, N(bArr, iT, 0));
    }

    public static W N(byte[] bArr, int i, int i5) {
        if (i5 >= bArr.length) {
            return G.p("");
        }
        D dJ = G.j();
        int iU = U(bArr, i5, i);
        while (i5 < iU) {
            dJ.a(new String(bArr, i5, iU - i5, S(i)));
            i5 = R(i) + iU;
            iU = U(bArr, i5, i);
        }
        W wG = dJ.g();
        return wG.isEmpty() ? G.p("") : wG;
    }

    public static n O(int i, x0.o oVar) {
        if (i < 1) {
            return null;
        }
        int iT = oVar.t();
        int i5 = i - 1;
        byte[] bArr = new byte[i5];
        oVar.e(bArr, 0, i5);
        int iU = U(bArr, 0, iT);
        return new n("TXXX", new String(bArr, 0, iU, S(iT)), N(bArr, iT, R(iT) + iU));
    }

    public static o P(int i, String str, x0.o oVar) {
        byte[] bArr = new byte[i];
        oVar.e(bArr, 0, i);
        return new o(str, null, new String(bArr, 0, V(0, bArr), StandardCharsets.ISO_8859_1));
    }

    public static o Q(int i, x0.o oVar) {
        if (i < 1) {
            return null;
        }
        int iT = oVar.t();
        int i5 = i - 1;
        byte[] bArr = new byte[i5];
        oVar.e(bArr, 0, i5);
        int iU = U(bArr, 0, iT);
        String str = new String(bArr, 0, iU, S(iT));
        int iR = R(iT) + iU;
        return new o("WXXX", str, L(bArr, iR, V(iR, bArr), StandardCharsets.ISO_8859_1));
    }

    public static int R(int i) {
        return (i == 0 || i == 3) ? 1 : 2;
    }

    public static Charset S(int i) {
        return i != 1 ? i != 2 ? i != 3 ? StandardCharsets.ISO_8859_1 : StandardCharsets.UTF_8 : StandardCharsets.UTF_16BE : StandardCharsets.UTF_16;
    }

    public static String T(int i, int i5, int i8, int i9, int i10) {
        return i == 2 ? String.format(Locale.US, "%c%c%c", Integer.valueOf(i5), Integer.valueOf(i8), Integer.valueOf(i9)) : String.format(Locale.US, "%c%c%c%c", Integer.valueOf(i5), Integer.valueOf(i8), Integer.valueOf(i9), Integer.valueOf(i10));
    }

    public static int U(byte[] bArr, int i, int i5) {
        int iV = V(i, bArr);
        if (i5 == 0 || i5 == 3) {
            return iV;
        }
        while (iV < bArr.length - 1) {
            if ((iV - i) % 2 == 0 && bArr[iV + 1] == 0) {
                return iV;
            }
            iV = V(iV + 1, bArr);
        }
        return bArr.length;
    }

    public static int V(int i, byte[] bArr) {
        while (i < bArr.length) {
            if (bArr[i] == 0) {
                return i;
            }
            i++;
        }
        return bArr.length;
    }

    public static int W(int i, x0.o oVar) {
        byte[] bArr = oVar.f18539a;
        int i5 = oVar.f18540b;
        int i8 = i5;
        while (true) {
            int i9 = i8 + 1;
            if (i9 >= i5 + i) {
                return i;
            }
            if ((bArr[i8] & 255) == 255 && bArr[i9] == 0) {
                System.arraycopy(bArr, i8 + 2, bArr, i9, (i - (i8 - i5)) - 2);
                i--;
            }
            i8 = i9;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:35:0x007a A[PHI: r3
      0x007a: PHI (r3v16 int) = (r3v5 int), (r3v19 int) binds: [B:42:0x0087, B:33:0x0077] A[DONT_GENERATE, DONT_INLINE]] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static boolean X(x0.o oVar, int i, int i5, boolean z8) {
        int iW;
        long jW;
        int iZ;
        int i8;
        int i9 = oVar.f18540b;
        while (true) {
            try {
                boolean z9 = true;
                if (oVar.a() < i5) {
                    oVar.F(i9);
                    return true;
                }
                if (i >= 3) {
                    iW = oVar.g();
                    jW = oVar.v();
                    iZ = oVar.z();
                } else {
                    iW = oVar.w();
                    jW = oVar.w();
                    iZ = 0;
                }
                if (iW == 0 && jW == 0 && iZ == 0) {
                    oVar.F(i9);
                    return true;
                }
                if (i == 4 && !z8) {
                    if ((8421504 & jW) != 0) {
                        oVar.F(i9);
                        return false;
                    }
                    jW = (((jW >> 24) & 255) << 21) | (jW & 255) | (((jW >> 8) & 255) << 7) | (((jW >> 16) & 255) << 14);
                }
                if (i == 4) {
                    i8 = (iZ & 64) != 0 ? 1 : 0;
                    if ((iZ & 1) == 0) {
                        z9 = false;
                    }
                } else if (i == 3) {
                    i8 = (iZ & 32) != 0 ? 1 : 0;
                    if ((iZ & 128) == 0) {
                    }
                } else {
                    i8 = 0;
                    z9 = false;
                }
                if (z9) {
                    i8 += 4;
                }
                if (jW < i8) {
                    oVar.F(i9);
                    return false;
                }
                if (oVar.a() < jW) {
                    oVar.F(i9);
                    return false;
                }
                oVar.G((int) jW);
            } catch (Throwable th) {
                oVar.F(i9);
                throw th;
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:30:0x008c  */
    /* JADX WARN: Removed duplicated region for block: B:34:0x009b A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:35:0x009c  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final u0.D C(int i, byte[] bArr) {
        g gVar;
        boolean z8 = true;
        ArrayList arrayList = new ArrayList();
        x0.o oVar = new x0.o(i, bArr);
        if (oVar.a() < 10) {
            AbstractC1709a.B("Id3Decoder", "Data too short to be an ID3 tag");
        } else {
            int iW = oVar.w();
            if (iW == 4801587) {
                int iT = oVar.t();
                oVar.G(1);
                int iT2 = oVar.t();
                int iS = oVar.s();
                if (iT == 2) {
                    if ((iT2 & 64) != 0) {
                        AbstractC1709a.B("Id3Decoder", "Skipped ID3 tag with majorVersion=2 and undefined compression scheme");
                    }
                    gVar = new g(iT, iS, iT >= 4 && (iT2 & 128) != 0);
                } else {
                    if (iT == 3) {
                        if ((iT2 & 64) != 0) {
                            int iG = oVar.g();
                            oVar.G(iG);
                            iS -= iG + 4;
                        }
                    } else if (iT == 4) {
                        if ((iT2 & 64) != 0) {
                            int iS2 = oVar.s();
                            oVar.G(iS2 - 4);
                            iS -= iS2;
                        }
                        if ((iT2 & 16) != 0) {
                            iS -= 10;
                        }
                    } else {
                        AbstractC0844a.s(iT, "Skipped ID3 tag with unsupported majorVersion=", "Id3Decoder");
                    }
                    if (iT >= 4) {
                        gVar = new g(iT, iS, iT >= 4 && (iT2 & 128) != 0);
                    }
                }
                if (gVar != null) {
                    return null;
                }
                int i5 = oVar.f18540b;
                int i8 = gVar.f13333a;
                int i9 = i8 == 2 ? 6 : 10;
                boolean z9 = gVar.f13334b;
                int iW2 = gVar.f13335c;
                if (z9) {
                    iW2 = W(iW2, oVar);
                }
                oVar.E(i5 + iW2);
                if (X(oVar, i8, i9, false)) {
                    z8 = false;
                } else if (i8 != 4 || !X(oVar, 4, i9, true)) {
                    AbstractC0844a.s(i8, "Failed to validate ID3 tag with majorVersion=", "Id3Decoder");
                    return null;
                }
                while (oVar.a() >= i9) {
                    i iVarH = H(i8, oVar, z8, i9, this.f13337v);
                    if (iVarH != null) {
                        arrayList.add(iVarH);
                    }
                }
                return new u0.D(arrayList);
            }
            AbstractC1709a.B("Id3Decoder", "Unexpected first three bytes of ID3 tag header: 0x".concat(String.format("%06X", Integer.valueOf(iW))));
        }
        gVar = null;
        if (gVar != null) {
        }
    }

    @Override // n4.u0
    public final u0.D q(C0576a c0576a, ByteBuffer byteBuffer) {
        return C(byteBuffer.limit(), byteBuffer.array());
    }
}
