package am;

import com.google.android.gms.common.api.Api;
import com.google.firebase.perf.util.Constants;
import com.google.zxing.qrcode.decoder.h;
import com.google.zxing.qrcode.decoder.j;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import yk.g;
import yk.v;

/* loaded from: classes3.dex */
public abstract class c {

    /* renamed from: a, reason: collision with root package name */
    private static final int[] f427a = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1};

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes3.dex */
    public static /* synthetic */ class a {

        /* renamed from: a, reason: collision with root package name */
        static final /* synthetic */ int[] f428a;

        static {
            int[] iArr = new int[h.values().length];
            f428a = iArr;
            try {
                iArr[h.NUMERIC.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                f428a[h.ALPHANUMERIC.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                f428a[h.BYTE.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                f428a[h.KANJI.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
        }
    }

    static void a(String str, fl.a aVar, String str2) {
        try {
            for (byte b10 : str.getBytes(str2)) {
                aVar.c(b10, 8);
            }
        } catch (UnsupportedEncodingException e10) {
            throw new v(e10);
        }
    }

    static void b(CharSequence charSequence, fl.a aVar) {
        int length = charSequence.length();
        int i10 = 0;
        while (i10 < length) {
            int p10 = p(charSequence.charAt(i10));
            if (p10 == -1) {
                throw new v();
            }
            int i11 = i10 + 1;
            if (i11 < length) {
                int p11 = p(charSequence.charAt(i11));
                if (p11 == -1) {
                    throw new v();
                }
                aVar.c((p10 * 45) + p11, 11);
                i10 += 2;
            } else {
                aVar.c(p10, 6);
                i10 = i11;
            }
        }
    }

    static void c(String str, h hVar, fl.a aVar, String str2) {
        int i10 = a.f428a[hVar.ordinal()];
        if (i10 == 1) {
            h(str, aVar);
            return;
        }
        if (i10 == 2) {
            b(str, aVar);
        } else if (i10 == 3) {
            a(str, aVar, str2);
        } else {
            if (i10 != 4) {
                throw new v("Invalid mode: ".concat(String.valueOf(hVar)));
            }
            e(str, aVar);
        }
    }

    private static void d(fl.d dVar, fl.a aVar) {
        aVar.c(h.ECI.getBits(), 4);
        aVar.c(dVar.getValue(), 8);
    }

    /* JADX WARN: Removed duplicated region for block: B:13:0x003c A[LOOP:0: B:6:0x000f->B:13:0x003c, LOOP_END] */
    /* JADX WARN: Removed duplicated region for block: B:14:0x004b A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    static void e(String str, fl.a aVar) {
        int i10;
        try {
            byte[] bytes = str.getBytes("Shift_JIS");
            if (bytes.length % 2 != 0) {
                throw new v("Kanji byte size not even");
            }
            int length = bytes.length - 1;
            for (int i11 = 0; i11 < length; i11 += 2) {
                int i12 = ((bytes[i11] & 255) << 8) | (bytes[i11 + 1] & 255);
                int i13 = 33088;
                if (i12 < 33088 || i12 > 40956) {
                    if (i12 < 57408 || i12 > 60351) {
                        i10 = -1;
                        if (i10 != -1) {
                            throw new v("Invalid byte sequence");
                        }
                        aVar.c(((i10 >> 8) * 192) + (i10 & Constants.MAX_HOST_LENGTH), 13);
                    } else {
                        i13 = 49472;
                    }
                }
                i10 = i12 - i13;
                if (i10 != -1) {
                }
            }
        } catch (UnsupportedEncodingException e10) {
            throw new v(e10);
        }
    }

    static void f(int i10, j jVar, h hVar, fl.a aVar) {
        int characterCountBits = hVar.getCharacterCountBits(jVar);
        int i11 = 1 << characterCountBits;
        if (i10 < i11) {
            aVar.c(i10, characterCountBits);
            return;
        }
        throw new v(i10 + " is bigger than " + (i11 - 1));
    }

    static void g(h hVar, fl.a aVar) {
        aVar.c(hVar.getBits(), 4);
    }

    static void h(CharSequence charSequence, fl.a aVar) {
        int length = charSequence.length();
        int i10 = 0;
        while (i10 < length) {
            int charAt = charSequence.charAt(i10) - '0';
            int i11 = i10 + 2;
            if (i11 < length) {
                aVar.c((charAt * 100) + ((charSequence.charAt(i10 + 1) - '0') * 10) + (charSequence.charAt(i11) - '0'), 10);
                i10 += 3;
            } else {
                i10++;
                if (i10 < length) {
                    aVar.c((charAt * 10) + (charSequence.charAt(i10) - '0'), 7);
                    i10 = i11;
                } else {
                    aVar.c(charAt, 4);
                }
            }
        }
    }

    private static int i(h hVar, fl.a aVar, fl.a aVar2, j jVar) {
        return aVar.n() + hVar.getCharacterCountBits(jVar) + aVar2.n();
    }

    private static int j(b bVar) {
        return d.a(bVar) + d.c(bVar) + d.d(bVar) + d.e(bVar);
    }

    private static int k(fl.a aVar, com.google.zxing.qrcode.decoder.f fVar, j jVar, b bVar) {
        int i10 = Api.BaseClientBuilder.API_PRIORITY_OTHER;
        int i11 = -1;
        for (int i12 = 0; i12 < 8; i12++) {
            e.a(aVar, fVar, jVar, i12, bVar);
            int j10 = j(bVar);
            if (j10 < i10) {
                i11 = i12;
                i10 = j10;
            }
        }
        return i11;
    }

    private static h l(String str, String str2) {
        if ("Shift_JIS".equals(str2) && s(str)) {
            return h.KANJI;
        }
        boolean z10 = false;
        boolean z11 = false;
        for (int i10 = 0; i10 < str.length(); i10++) {
            char charAt = str.charAt(i10);
            if (charAt >= '0' && charAt <= '9') {
                z11 = true;
            } else {
                if (p(charAt) == -1) {
                    return h.BYTE;
                }
                z10 = true;
            }
        }
        return z10 ? h.ALPHANUMERIC : z11 ? h.NUMERIC : h.BYTE;
    }

    private static j m(int i10, com.google.zxing.qrcode.decoder.f fVar) {
        for (int i11 = 1; i11 <= 40; i11++) {
            j i12 = j.i(i11);
            if (v(i10, i12, fVar)) {
                return i12;
            }
        }
        throw new v("Data too big");
    }

    /* JADX WARN: Code restructure failed: missing block: B:38:0x00fc, code lost:
    
        if (am.f.b(r9) != false) goto L48;
     */
    /* JADX WARN: Removed duplicated region for block: B:32:0x009f  */
    /* JADX WARN: Removed duplicated region for block: B:35:0x00e4  */
    /* JADX WARN: Removed duplicated region for block: B:40:0x0102  */
    /* JADX WARN: Removed duplicated region for block: B:44:0x00a4  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static f n(String str, com.google.zxing.qrcode.decoder.f fVar, Map map) {
        j t10;
        int i10;
        fl.d characterSetECIByName;
        boolean z10 = map != null && map.containsKey(g.CHARACTER_SET);
        String obj = z10 ? map.get(g.CHARACTER_SET).toString() : "ISO-8859-1";
        h l10 = l(str, obj);
        fl.a aVar = new fl.a();
        h hVar = h.BYTE;
        if (l10 == hVar && z10 && (characterSetECIByName = fl.d.getCharacterSetECIByName(obj)) != null) {
            d(characterSetECIByName, aVar);
        }
        if ((map != null && map.containsKey(g.GS1_FORMAT)) && Boolean.parseBoolean(map.get(g.GS1_FORMAT).toString())) {
            g(h.FNC1_FIRST_POSITION, aVar);
        }
        g(l10, aVar);
        fl.a aVar2 = new fl.a();
        c(str, l10, aVar2, obj);
        if (map != null) {
            g gVar = g.QR_VERSION;
            if (map.containsKey(gVar)) {
                t10 = j.i(Integer.parseInt(map.get(gVar).toString()));
                if (!v(i(l10, aVar, aVar2, t10), t10, fVar)) {
                    throw new v("Data too big for requested version");
                }
                fl.a aVar3 = new fl.a();
                aVar3.b(aVar);
                f(l10 != hVar ? aVar2.o() : str.length(), t10, l10, aVar3);
                aVar3.b(aVar2);
                j.b f10 = t10.f(fVar);
                int h10 = t10.h() - f10.d();
                u(h10, aVar3);
                fl.a r10 = r(aVar3, t10.h(), h10, f10.c());
                f fVar2 = new f();
                fVar2.c(fVar);
                fVar2.f(l10);
                fVar2.g(t10);
                int e10 = t10.e();
                b bVar = new b(e10, e10);
                if (map != null) {
                    g gVar2 = g.QR_MASK_PATTERN;
                    if (map.containsKey(gVar2)) {
                        i10 = Integer.parseInt(map.get(gVar2).toString());
                    }
                }
                i10 = -1;
                if (i10 == -1) {
                    i10 = k(r10, fVar, t10, bVar);
                }
                fVar2.d(i10);
                e.a(r10, fVar, t10, i10, bVar);
                fVar2.e(bVar);
                return fVar2;
            }
        }
        t10 = t(fVar, l10, aVar, aVar2);
        fl.a aVar32 = new fl.a();
        aVar32.b(aVar);
        f(l10 != hVar ? aVar2.o() : str.length(), t10, l10, aVar32);
        aVar32.b(aVar2);
        j.b f102 = t10.f(fVar);
        int h102 = t10.h() - f102.d();
        u(h102, aVar32);
        fl.a r102 = r(aVar32, t10.h(), h102, f102.c());
        f fVar22 = new f();
        fVar22.c(fVar);
        fVar22.f(l10);
        fVar22.g(t10);
        int e102 = t10.e();
        b bVar2 = new b(e102, e102);
        if (map != null) {
        }
        i10 = -1;
        if (i10 == -1) {
        }
        fVar22.d(i10);
        e.a(r102, fVar, t10, i10, bVar2);
        fVar22.e(bVar2);
        return fVar22;
    }

    static byte[] o(byte[] bArr, int i10) {
        int length = bArr.length;
        int[] iArr = new int[length + i10];
        for (int i11 = 0; i11 < length; i11++) {
            iArr[i11] = bArr[i11] & 255;
        }
        new hl.d(hl.a.f21853l).b(iArr, i10);
        byte[] bArr2 = new byte[i10];
        for (int i12 = 0; i12 < i10; i12++) {
            bArr2[i12] = (byte) iArr[length + i12];
        }
        return bArr2;
    }

    static int p(int i10) {
        int[] iArr = f427a;
        if (i10 < iArr.length) {
            return iArr[i10];
        }
        return -1;
    }

    static void q(int i10, int i11, int i12, int i13, int[] iArr, int[] iArr2) {
        if (i13 >= i12) {
            throw new v("Block ID too large");
        }
        int i14 = i10 % i12;
        int i15 = i12 - i14;
        int i16 = i10 / i12;
        int i17 = i16 + 1;
        int i18 = i11 / i12;
        int i19 = i18 + 1;
        int i20 = i16 - i18;
        int i21 = i17 - i19;
        if (i20 != i21) {
            throw new v("EC bytes mismatch");
        }
        if (i12 != i15 + i14) {
            throw new v("RS blocks mismatch");
        }
        if (i10 != ((i18 + i20) * i15) + ((i19 + i21) * i14)) {
            throw new v("Total bytes mismatch");
        }
        if (i13 < i15) {
            iArr[0] = i18;
            iArr2[0] = i20;
        } else {
            iArr[0] = i19;
            iArr2[0] = i21;
        }
    }

    static fl.a r(fl.a aVar, int i10, int i11, int i12) {
        if (aVar.o() != i11) {
            throw new v("Number of bits and data bytes does not match");
        }
        ArrayList arrayList = new ArrayList(i12);
        int i13 = 0;
        int i14 = 0;
        int i15 = 0;
        for (int i16 = 0; i16 < i12; i16++) {
            int[] iArr = new int[1];
            int[] iArr2 = new int[1];
            q(i10, i11, i12, i16, iArr, iArr2);
            int i17 = iArr[0];
            byte[] bArr = new byte[i17];
            aVar.v(i13 << 3, bArr, 0, i17);
            byte[] o10 = o(bArr, iArr2[0]);
            arrayList.add(new am.a(bArr, o10));
            i14 = Math.max(i14, i17);
            i15 = Math.max(i15, o10.length);
            i13 += iArr[0];
        }
        if (i11 != i13) {
            throw new v("Data bytes does not match offset");
        }
        fl.a aVar2 = new fl.a();
        for (int i18 = 0; i18 < i14; i18++) {
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                byte[] a10 = ((am.a) it.next()).a();
                if (i18 < a10.length) {
                    aVar2.c(a10[i18], 8);
                }
            }
        }
        for (int i19 = 0; i19 < i15; i19++) {
            Iterator it2 = arrayList.iterator();
            while (it2.hasNext()) {
                byte[] b10 = ((am.a) it2.next()).b();
                if (i19 < b10.length) {
                    aVar2.c(b10[i19], 8);
                }
            }
        }
        if (i10 == aVar2.o()) {
            return aVar2;
        }
        throw new v("Interleaving error: " + i10 + " and " + aVar2.o() + " differ.");
    }

    private static boolean s(String str) {
        try {
            byte[] bytes = str.getBytes("Shift_JIS");
            int length = bytes.length;
            if (length % 2 != 0) {
                return false;
            }
            for (int i10 = 0; i10 < length; i10 += 2) {
                int i11 = bytes[i10] & 255;
                if ((i11 < 129 || i11 > 159) && (i11 < 224 || i11 > 235)) {
                    return false;
                }
            }
            return true;
        } catch (UnsupportedEncodingException unused) {
            return false;
        }
    }

    private static j t(com.google.zxing.qrcode.decoder.f fVar, h hVar, fl.a aVar, fl.a aVar2) {
        return m(i(hVar, aVar, aVar2, m(i(hVar, aVar, aVar2, j.i(1)), fVar)), fVar);
    }

    static void u(int i10, fl.a aVar) {
        int i11 = i10 << 3;
        if (aVar.n() > i11) {
            throw new v("data bits cannot fit in the QR Code" + aVar.n() + " > " + i11);
        }
        for (int i12 = 0; i12 < 4 && aVar.n() < i11; i12++) {
            aVar.a(false);
        }
        int n10 = aVar.n() & 7;
        if (n10 > 0) {
            while (n10 < 8) {
                aVar.a(false);
                n10++;
            }
        }
        int o10 = i10 - aVar.o();
        for (int i13 = 0; i13 < o10; i13++) {
            aVar.c((i13 & 1) == 0 ? 236 : 17, 8);
        }
        if (aVar.n() != i11) {
            throw new v("Bits size does not equal capacity");
        }
    }

    private static boolean v(int i10, j jVar, com.google.zxing.qrcode.decoder.f fVar) {
        return jVar.h() - jVar.f(fVar).d() >= (i10 + 7) / 8;
    }
}
