package com.google.android.gms.internal.measurement;

/* JADX INFO: loaded from: classes.dex */
public abstract class J2 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final /* synthetic */ int f9620a = 0;

    static {
        if (H2.f9602e && H2.f9601d) {
            int i = O1.f9656a;
        }
    }

    public static /* bridge */ /* synthetic */ int a(byte[] bArr, int i, int i5) {
        int i8 = i5 - i;
        byte b8 = bArr[i - 1];
        if (i8 == 0) {
            if (b8 <= -12) {
                return b8;
            }
            return -1;
        }
        if (i8 == 1) {
            byte b9 = bArr[i];
            if (b8 > -12 || b9 > -65) {
                return -1;
            }
            return (b9 << 8) ^ b8;
        }
        if (i8 != 2) {
            throw new AssertionError();
        }
        byte b10 = bArr[i];
        byte b11 = bArr[i + 1];
        if (b8 > -12 || b10 > -65 || b11 > -65) {
            return -1;
        }
        return (b11 << 16) ^ ((b10 << 8) ^ b8);
    }

    public static int b(String str, byte[] bArr, int i, int i5) {
        int i8;
        int i9;
        int i10;
        char cCharAt;
        int length = str.length();
        int i11 = 0;
        while (true) {
            i8 = i + i5;
            if (i11 >= length || (i10 = i11 + i) >= i8 || (cCharAt = str.charAt(i11)) >= 128) {
                break;
            }
            bArr[i10] = (byte) cCharAt;
            i11++;
        }
        if (i11 == length) {
            return i + length;
        }
        int i12 = i + i11;
        while (i11 < length) {
            char cCharAt2 = str.charAt(i11);
            if (cCharAt2 < 128 && i12 < i8) {
                bArr[i12] = (byte) cCharAt2;
                i12++;
            } else if (cCharAt2 < 2048 && i12 <= i8 - 2) {
                bArr[i12] = (byte) ((cCharAt2 >>> 6) | 960);
                bArr[i12 + 1] = (byte) ((cCharAt2 & '?') | 128);
                i12 += 2;
            } else {
                if ((cCharAt2 >= 55296 && cCharAt2 <= 57343) || i12 > i8 - 3) {
                    if (i12 > i8 - 4) {
                        if (cCharAt2 >= 55296 && cCharAt2 <= 57343 && ((i9 = i11 + 1) == str.length() || !Character.isSurrogatePair(cCharAt2, str.charAt(i9)))) {
                            throw new I2(i11, length);
                        }
                        throw new ArrayIndexOutOfBoundsException("Failed writing " + cCharAt2 + " at index " + i12);
                    }
                    int i13 = i11 + 1;
                    if (i13 != str.length()) {
                        char cCharAt3 = str.charAt(i13);
                        if (Character.isSurrogatePair(cCharAt2, cCharAt3)) {
                            int i14 = i12 + 3;
                            int codePoint = Character.toCodePoint(cCharAt2, cCharAt3);
                            bArr[i12] = (byte) ((codePoint >>> 18) | 240);
                            bArr[i12 + 1] = (byte) (((codePoint >>> 12) & 63) | 128);
                            bArr[i12 + 2] = (byte) (((codePoint >>> 6) & 63) | 128);
                            i12 += 4;
                            bArr[i14] = (byte) ((codePoint & 63) | 128);
                            i11 = i13;
                        } else {
                            i11 = i13;
                        }
                    }
                    throw new I2(i11 - 1, length);
                }
                bArr[i12] = (byte) ((cCharAt2 >>> '\f') | 480);
                bArr[i12 + 1] = (byte) (((cCharAt2 >>> 6) & 63) | 128);
                bArr[i12 + 2] = (byte) ((cCharAt2 & '?') | 128);
                i12 += 3;
            }
            i11++;
        }
        return i12;
    }

    public static int c(String str) {
        int length = str.length();
        int i = 0;
        int i5 = 0;
        while (i5 < length && str.charAt(i5) < 128) {
            i5++;
        }
        int i8 = length;
        while (true) {
            if (i5 >= length) {
                break;
            }
            char cCharAt = str.charAt(i5);
            if (cCharAt < 2048) {
                i8 += (127 - cCharAt) >>> 31;
                i5++;
            } else {
                int length2 = str.length();
                while (i5 < length2) {
                    char cCharAt2 = str.charAt(i5);
                    if (cCharAt2 < 2048) {
                        i += (127 - cCharAt2) >>> 31;
                    } else {
                        i += 2;
                        if (cCharAt2 >= 55296 && cCharAt2 <= 57343) {
                            if (Character.codePointAt(str, i5) < 65536) {
                                throw new I2(i5, length2);
                            }
                            i5++;
                        }
                    }
                    i5++;
                }
                i8 += i;
            }
        }
        if (i8 >= length) {
            return i8;
        }
        throw new IllegalArgumentException("UTF-8 length does not fit in int: " + (((long) i8) + 4294967296L));
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:50:0x0076 A[ORIG_RETURN, RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:53:0x007a A[ORIG_RETURN, RETURN] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static boolean d(byte[] bArr, int i, int i5) {
        while (i < i5 && bArr[i] >= 0) {
            i++;
        }
        if (i >= i5) {
            return true;
        }
        while (i < i5) {
            int i8 = i + 1;
            int iA = bArr[i];
            if (iA >= 0) {
                i = i8;
            } else if (iA < -32) {
                if (i8 >= i5) {
                    return iA == 0;
                }
                if (iA < -62) {
                    return false;
                }
                i += 2;
                if (bArr[i8] > -65) {
                    return false;
                }
            } else if (iA < -16) {
                if (i8 >= i5 - 1) {
                    iA = a(bArr, i8, i5);
                    if (iA == 0) {
                    }
                } else {
                    int i9 = i + 2;
                    char c3 = bArr[i8];
                    if (c3 > -65) {
                        return false;
                    }
                    if (iA == -32 && c3 < -96) {
                        return false;
                    }
                    if (iA == -19 && c3 >= -96) {
                        return false;
                    }
                    i += 3;
                    if (bArr[i9] > -65) {
                        return false;
                    }
                }
            } else if (i8 >= i5 - 2) {
                iA = a(bArr, i8, i5);
                if (iA == 0) {
                }
            } else {
                int i10 = i + 2;
                int i11 = bArr[i8];
                if (i11 > -65) {
                    return false;
                }
                if ((((i11 + 112) + (iA << 28)) >> 30) != 0) {
                    return false;
                }
                int i12 = i + 3;
                if (bArr[i10] > -65) {
                    return false;
                }
                i += 4;
                if (bArr[i12] > -65) {
                    return false;
                }
            }
        }
        return true;
    }
}
