package com.google.zxing.aztec.decoder;

import androidx.media3.exoplayer.rtsp.SessionDescription;
import androidx.media3.exoplayer.upstream.CmcdData;
import com.google.zxing.FormatException;
import com.google.zxing.ReaderException;
import com.google.zxing.common.CharacterSetECI;
import com.google.zxing.common.reedsolomon.ReedSolomonException;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import l2.h;
import tb.b;
import tb.c;
import xyz.stream.utils.j0;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final String[] f24352b = {"CTRL_PS", " ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "CTRL_LL", "CTRL_ML", "CTRL_DL", "CTRL_BS"};

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final String[] f24353c = {"CTRL_PS", " ", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "b", "c", "d", "e", "f", "g", CmcdData.STREAMING_FORMAT_HLS, CmcdData.OBJECT_TYPE_INIT_SEGMENT, "j", "k", CmcdData.STREAM_TYPE_LIVE, CmcdData.OBJECT_TYPE_MANIFEST, "n", "o", "p", "q", "r", CmcdData.STREAMING_FORMAT_SS, "t", "u", "v", "w", "x", "y", "z", "CTRL_US", "CTRL_ML", "CTRL_DL", "CTRL_BS"};

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final String[] f24354d = {"CTRL_PS", " ", "\u0001", "\u0002", "\u0003", "\u0004", "\u0005", "\u0006", "\u0007", "\b", "\t", "\n", "\u000b", "\f", "\r", "\u001b", "\u001c", "\u001d", "\u001e", "\u001f", "@", "\\", "^", "_", "`", "|", "~", "\u007f", "CTRL_LL", "CTRL_UL", "CTRL_PL", "CTRL_BS"};

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final String[] f24355e = {"FLG(n)", "\r", "\r\n", ". ", ", ", ": ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "<", "=", ">", "?", "[", "]", "{", "}", "CTRL_UL"};

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final String[] f24356f = {"CTRL_PS", " ", SessionDescription.SUPPORTED_SDP_VERSION, "1", "2", "3", "4", "5", "6", "7", "8", "9", ",", ".", "CTRL_UL", "CTRL_US"};

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final Charset f24357g = StandardCharsets.ISO_8859_1;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public pb.a f24358a;

    public static int b(boolean[] zArr, int i10, int i11) {
        int i12 = 0;
        for (int i13 = i10; i13 < i10 + i11; i13++) {
            i12 <<= 1;
            if (zArr[i13]) {
                i12 |= 1;
            }
        }
        return i12;
    }

    public final c a(pb.a aVar) throws FormatException {
        int i10;
        ub.a aVar2;
        String str;
        char c6;
        this.f24358a = aVar;
        b bVar = (b) aVar.f36128b;
        boolean z10 = aVar.f31921e;
        int i11 = z10 ? 11 : 14;
        int i12 = aVar.f31923g;
        int i13 = (i12 * 4) + i11;
        int[] iArr = new int[i13];
        int i14 = ((i12 * 16) + (z10 ? 88 : 112)) * i12;
        boolean[] zArr = new boolean[i14];
        int i15 = 2;
        if (z10) {
            for (int i16 = 0; i16 < i13; i16++) {
                iArr[i16] = i16;
            }
        } else {
            int i17 = i13 / 2;
            int i18 = ((((i17 - 1) / 15) * 2) + (i13 + 1)) / 2;
            for (int i19 = 0; i19 < i17; i19++) {
                iArr[(i17 - i19) - 1] = (i18 - r15) - 1;
                iArr[i17 + i19] = (i19 / 15) + i19 + i18 + 1;
            }
        }
        int i20 = 0;
        int i21 = 0;
        while (true) {
            if (i20 >= i12) {
                break;
            }
            int i22 = ((i12 - i20) * 4) + (z10 ? 9 : 12);
            int i23 = i20 * 2;
            int i24 = (i13 - 1) - i23;
            int i25 = 0;
            while (i25 < i22) {
                int i26 = i25 * 2;
                int i27 = 0;
                while (i27 < i15) {
                    int i28 = i23 + i27;
                    int i29 = i23 + i25;
                    zArr[i21 + i26 + i27] = bVar.b(iArr[i28], iArr[i29]);
                    int i30 = i24 - i27;
                    zArr[(i22 * 2) + i21 + i26 + i27] = bVar.b(iArr[i29], iArr[i30]);
                    int i31 = i24 - i25;
                    zArr[(i22 * 4) + i21 + i26 + i27] = bVar.b(iArr[i30], iArr[i31]);
                    zArr[(i22 * 6) + i21 + i26 + i27] = bVar.b(iArr[i31], iArr[i28]);
                    i27++;
                    z10 = z10;
                    i12 = i12;
                    i15 = 2;
                }
                i25++;
                i15 = 2;
            }
            i21 += i22 * 8;
            i20++;
            i15 = 2;
        }
        pb.a aVar3 = this.f24358a;
        int i32 = aVar3.f31923g;
        int i33 = 8;
        if (i32 <= 2) {
            aVar2 = ub.a.f33424j;
            i10 = 6;
        } else if (i32 <= 8) {
            aVar2 = ub.a.f33428n;
            i10 = 8;
        } else if (i32 <= 22) {
            aVar2 = ub.a.f33423i;
            i10 = 10;
        } else {
            aVar2 = ub.a.f33422h;
        }
        int i34 = i14 / i10;
        int i35 = aVar3.f31922f;
        if (i34 < i35) {
            throw FormatException.a();
        }
        int i36 = i14 % i10;
        int[] iArr2 = new int[i34];
        int i37 = 0;
        while (i37 < i34) {
            iArr2[i37] = b(zArr, i36, i10);
            i37++;
            i36 += i10;
        }
        try {
            j0 j0Var = new j0(aVar2, 23);
            int i38 = i34 - i35;
            int iJ = j0Var.j(i38, iArr2);
            int i39 = 1;
            int i40 = 1 << i10;
            int i41 = i40 - 1;
            int i42 = 0;
            int i43 = 0;
            while (i42 < i35) {
                int i44 = iArr2[i42];
                if (i44 == 0 || i44 == i41) {
                    throw FormatException.a();
                }
                if (i44 == i39 || i44 == i40 - 2) {
                    i43++;
                }
                i42++;
                i39 = 1;
            }
            int i45 = (i35 * i10) - i43;
            boolean[] zArr2 = new boolean[i45];
            int i46 = 0;
            for (int i47 = 0; i47 < i35; i47++) {
                int i48 = iArr2[i47];
                int i49 = 1;
                if (i48 == 1 || i48 == i40 - 2) {
                    Arrays.fill(zArr2, i46, (i46 + i10) - 1, i48 > 1);
                    i46 = (i10 - 1) + i46;
                } else {
                    int i50 = i10 - 1;
                    while (i50 >= 0) {
                        int i51 = i46 + 1;
                        zArr2[i46] = (i48 & (i49 << i50)) != 0;
                        i50--;
                        i46 = i51;
                        i49 = 1;
                    }
                }
            }
            h hVar = new h(zArr2, iJ, (i38 * 100) / i34);
            int i52 = (i45 + 7) / 8;
            byte[] bArr = new byte[i52];
            for (int i53 = 0; i53 < i52; i53++) {
                int i54 = i53 * 8;
                int i55 = i45 - i54;
                bArr[i53] = (byte) (i55 >= 8 ? b(zArr2, i54, 8) : b(zArr2, i54, i55) << (8 - i55));
            }
            boolean[] zArr3 = (boolean[]) hVar.f29998d;
            int length = zArr3.length;
            Decoder$Table decoder$Table = Decoder$Table.f24345a;
            int i56 = 5;
            StringBuilder sb = new StringBuilder((zArr3.length - 5) / 4);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            Charset charsetForName = f24357g;
            Decoder$Table decoder$Table2 = decoder$Table;
            Decoder$Table decoder$Table3 = decoder$Table2;
            int i57 = 0;
            loop9: while (i57 < length) {
                Decoder$Table decoder$Table4 = Decoder$Table.f24350f;
                if (decoder$Table2 != decoder$Table4) {
                    Decoder$Table decoder$Table5 = Decoder$Table.f24348d;
                    int i58 = decoder$Table2 == decoder$Table5 ? 4 : i56;
                    if (length - i57 >= i58) {
                        int iB = b(zArr3, i57, i58);
                        i57 += i58;
                        int iOrdinal = decoder$Table2.ordinal();
                        if (iOrdinal == 0) {
                            str = f24352b[iB];
                        } else if (iOrdinal == 1) {
                            str = f24353c[iB];
                        } else if (iOrdinal == 2) {
                            str = f24354d[iB];
                        } else if (iOrdinal == 3) {
                            str = f24356f[iB];
                        } else {
                            if (iOrdinal != 4) {
                                throw new IllegalStateException("Bad table");
                            }
                            str = f24355e[iB];
                        }
                        if ("FLG(n)".equals(str)) {
                            if (length - i57 >= 3) {
                                int iB2 = b(zArr3, i57, 3);
                                i57 += 3;
                                try {
                                    sb.append(byteArrayOutputStream.toString(charsetForName.name()));
                                    byteArrayOutputStream.reset();
                                    if (iB2 == 0) {
                                        c6 = 4;
                                        sb.append((char) 29);
                                    } else {
                                        if (iB2 == 7) {
                                            throw FormatException.a();
                                        }
                                        if (length - i57 >= iB2 * 4) {
                                            int i59 = 0;
                                            while (true) {
                                                int i60 = iB2 - 1;
                                                if (iB2 > 0) {
                                                    int iB3 = b(zArr3, i57, 4);
                                                    i57 += 4;
                                                    if (iB3 < 2 || iB3 > 11) {
                                                        break loop9;
                                                    }
                                                    i59 = (i59 * 10) + (iB3 - 2);
                                                    iB2 = i60;
                                                } else {
                                                    c6 = 4;
                                                    CharacterSetECI characterSetECIA = CharacterSetECI.a(i59);
                                                    if (characterSetECIA == null) {
                                                        throw FormatException.a();
                                                    }
                                                    charsetForName = Charset.forName(characterSetECIA.name());
                                                }
                                            }
                                            throw FormatException.a();
                                        }
                                        c6 = 4;
                                    }
                                    decoder$Table4 = decoder$Table3;
                                } catch (UnsupportedEncodingException e10) {
                                    throw new IllegalStateException(e10);
                                }
                            }
                        } else if (str.startsWith("CTRL_")) {
                            char cCharAt = str.charAt(5);
                            if (cCharAt != 'B') {
                                decoder$Table4 = cCharAt != 'D' ? cCharAt != 'P' ? cCharAt != 'L' ? cCharAt != 'M' ? decoder$Table : Decoder$Table.f24347c : Decoder$Table.f24346b : Decoder$Table.f24349e : decoder$Table5;
                            }
                            decoder$Table3 = str.charAt(6) == 'L' ? decoder$Table4 : decoder$Table2;
                        } else {
                            byte[] bytes = str.getBytes(StandardCharsets.US_ASCII);
                            byteArrayOutputStream.write(bytes, 0, bytes.length);
                            decoder$Table4 = decoder$Table3;
                            decoder$Table2 = decoder$Table4;
                            i33 = 8;
                            i56 = 5;
                        }
                        decoder$Table2 = decoder$Table4;
                        i33 = 8;
                        i56 = 5;
                    }
                } else if (length - i57 >= i56) {
                    int iB4 = b(zArr3, i57, i56);
                    int i61 = i57 + 5;
                    if (iB4 == 0) {
                        if (length - i61 >= 11) {
                            iB4 = b(zArr3, i61, 11) + 31;
                            i61 = i57 + 16;
                        }
                    }
                    int i62 = 0;
                    while (true) {
                        if (i62 >= iB4) {
                            i57 = i61;
                            break;
                        }
                        if (length - i61 < i33) {
                            i57 = length;
                            break;
                        }
                        byteArrayOutputStream.write((byte) b(zArr3, i61, i33));
                        i61 += 8;
                        i62++;
                    }
                    decoder$Table2 = decoder$Table3;
                }
            }
            try {
                sb.append(byteArrayOutputStream.toString(charsetForName.name()));
                c cVar = new c(bArr, sb.toString(), String.format("%d%%", Integer.valueOf(hVar.f29997c)));
                int length2 = zArr3.length;
                cVar.f33013e = Integer.valueOf(hVar.f29996b);
                return cVar;
            } catch (UnsupportedEncodingException e11) {
                throw new IllegalStateException(e11);
            }
        } catch (ReedSolomonException e12) {
            FormatException formatException = FormatException.f24328c;
            if (ReaderException.f24330a) {
                throw new FormatException(e12);
            }
            throw FormatException.f24328c;
        }
    }
}
