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

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

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public int f9216d = 0;

    public ap1(byte[] bArr, int i10, int i11) {
        this.f9213a = bArr;
        this.f9215c = i10;
        this.f9214b = i11;
        k();
    }

    public final void a() {
        int i10 = this.f9216d + 1;
        this.f9216d = i10;
        if (i10 == 8) {
            this.f9216d = 0;
            int i11 = this.f9215c;
            this.f9215c = i11 + (true == j(i11 + 1) ? 2 : 1);
        }
        k();
    }

    public final void b(int i10) {
        int i11 = this.f9215c;
        int i12 = i10 / 8;
        int i13 = i11 + i12;
        this.f9215c = i13;
        int i14 = (i10 - (i12 * 8)) + this.f9216d;
        this.f9216d = i14;
        if (i14 > 7) {
            this.f9215c = i13 + 1;
            this.f9216d = i14 - 8;
        }
        while (true) {
            i11++;
            if (i11 > this.f9215c) {
                k();
                return;
            } else if (j(i11)) {
                this.f9215c++;
                i11 += 2;
            }
        }
    }

    public final void c() {
        int i10 = this.f9216d;
        if (i10 > 0) {
            b(8 - i10);
        }
    }

    public final boolean d(int i10) {
        int i11;
        int i12 = this.f9215c;
        int i13 = i10 / 8;
        int i14 = i12 + i13;
        int i15 = (this.f9216d + i10) - (i13 * 8);
        if (i15 > 7) {
            i14++;
            i15 -= 8;
        }
        while (true) {
            i12++;
            i11 = this.f9214b;
            if (i12 > i14 || i14 >= i11) {
                break;
            }
            if (j(i12)) {
                i14++;
                i12 += 2;
            }
        }
        if (i14 >= i11) {
            return i14 == i11 && i15 == 0;
        }
        return true;
    }

    public final boolean e() {
        int i10 = this.f9213a[this.f9215c] & (128 >> this.f9216d);
        a();
        return i10 != 0;
    }

    public final int f(int i10) {
        int i11;
        byte[] bArr;
        this.f9216d += i10;
        int i12 = 0;
        while (true) {
            i11 = this.f9216d;
            bArr = this.f9213a;
            if (i11 <= 8) {
                break;
            }
            int i13 = i11 - 8;
            this.f9216d = i13;
            int i14 = this.f9215c;
            i12 |= (bArr[i14] & 255) << i13;
            if (true != j(i14 + 1)) {
                i = 1;
            }
            this.f9215c = i14 + i;
        }
        int i15 = this.f9215c;
        int i16 = i12 | ((bArr[i15] & 255) >> (8 - i11));
        int i17 = 32 - i10;
        if (i11 == 8) {
            this.f9216d = 0;
            this.f9215c = i15 + (true != j(i15 + 1) ? 1 : 2);
        }
        int i18 = ((-1) >>> i17) & i16;
        k();
        return i18;
    }

    public final int g() {
        return i();
    }

    public final int h() {
        int i10 = i();
        int i11 = i10 % 2;
        return ((i10 + 1) / 2) * (i11 == 0 ? -1 : 1);
    }

    public final int i() {
        int i10 = 0;
        while (!e()) {
            i10++;
        }
        return ((1 << i10) - 1) + (i10 > 0 ? f(i10) : 0);
    }

    public final boolean j(int i10) {
        if (i10 < 2 || i10 >= this.f9214b) {
            return false;
        }
        byte[] bArr = this.f9213a;
        return bArr[i10] == 3 && bArr[i10 + (-2)] == 0 && bArr[i10 + (-1)] == 0;
    }

    public final void k() {
        int i10;
        int i11 = this.f9215c;
        boolean z10 = false;
        if (i11 >= 0 && (i11 < (i10 = this.f9214b) || (i11 == i10 && this.f9216d == 0))) {
            z10 = true;
        }
        ih1.b1(z10);
    }
}
