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

/* JADX INFO: loaded from: classes2.dex */
public final class i6 {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final long[] f12384d = {128, 64, 32, 16, 8, 4, 2, 1};

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final byte[] f12385a = new byte[8];

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

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

    public static long b(byte[] bArr, int i10, boolean z10) {
        long j10 = ((long) bArr[0]) & 255;
        if (z10) {
            j10 &= ~f12384d[i10 - 1];
        }
        for (int i11 = 1; i11 < i10; i11++) {
            j10 = (j10 << 8) | (((long) bArr[i11]) & 255);
        }
        return j10;
    }

    public final long a(p2 p2Var, boolean z10, boolean z11, int i10) {
        int i11;
        int i12 = this.f12386b;
        byte[] bArr = this.f12385a;
        if (i12 == 0) {
            if (!p2Var.i(bArr, 0, 1, z10)) {
                return -1L;
            }
            int i13 = bArr[0] & 255;
            int i14 = 0;
            while (true) {
                if (i14 >= 8) {
                    i11 = -1;
                    break;
                }
                i11 = i14 + 1;
                if ((f12384d[i14] & ((long) i13)) != 0) {
                    break;
                }
                i14 = i11;
            }
            this.f12387c = i11;
            if (i11 == -1) {
                throw new IllegalStateException("No valid varint length mask found");
            }
            this.f12386b = 1;
        }
        int i15 = this.f12387c;
        if (i15 > i10) {
            this.f12386b = 0;
            return -2L;
        }
        if (i15 != 1) {
            p2Var.c(bArr, 1, i15 - 1);
        }
        this.f12386b = 0;
        return b(bArr, this.f12387c, z11);
    }
}
