package r7;

import androidx.leanback.widget.Q0;
import java.io.IOException;
import java.util.ArrayList;
import x7.C1731g;
import x7.C1734j;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final x7.B f16593c;

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

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public int f16591a = 4096;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final ArrayList f16592b = new ArrayList();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public C1512b[] f16594d = new C1512b[8];

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public int f16595e = 7;

    public c(r rVar) {
        this.f16593c = s7.l.c(rVar);
    }

    public final int a(int i) {
        int i5;
        int i8 = 0;
        if (i > 0) {
            int length = this.f16594d.length;
            while (true) {
                length--;
                i5 = this.f16595e;
                if (length < i5 || i <= 0) {
                    break;
                }
                C1512b c1512b = this.f16594d[length];
                P6.g.b(c1512b);
                int i9 = c1512b.f16590c;
                i -= i9;
                this.f16597g -= i9;
                this.f16596f--;
                i8++;
            }
            C1512b[] c1512bArr = this.f16594d;
            System.arraycopy(c1512bArr, i5 + 1, c1512bArr, i5 + 1 + i8, this.f16596f);
            this.f16595e += i8;
        }
        return i8;
    }

    public final C1734j b(int i) throws IOException {
        if (i >= 0) {
            C1512b[] c1512bArr = e.f16606a;
            if (i <= c1512bArr.length - 1) {
                return c1512bArr[i].f16588a;
            }
        }
        int length = this.f16595e + 1 + (i - e.f16606a.length);
        if (length >= 0) {
            C1512b[] c1512bArr2 = this.f16594d;
            if (length < c1512bArr2.length) {
                C1512b c1512b = c1512bArr2[length];
                P6.g.b(c1512b);
                return c1512b.f16588a;
            }
        }
        throw new IOException("Header index too large " + (i + 1));
    }

    public final void c(C1512b c1512b) {
        this.f16592b.add(c1512b);
        int i = this.f16591a;
        int i5 = c1512b.f16590c;
        if (i5 > i) {
            C1512b[] c1512bArr = this.f16594d;
            B6.j.L(c1512bArr, 0, c1512bArr.length);
            this.f16595e = this.f16594d.length - 1;
            this.f16596f = 0;
            this.f16597g = 0;
            return;
        }
        a((this.f16597g + i5) - i);
        int i8 = this.f16596f + 1;
        C1512b[] c1512bArr2 = this.f16594d;
        if (i8 > c1512bArr2.length) {
            C1512b[] c1512bArr3 = new C1512b[c1512bArr2.length * 2];
            System.arraycopy(c1512bArr2, 0, c1512bArr3, c1512bArr2.length, c1512bArr2.length);
            this.f16595e = this.f16594d.length - 1;
            this.f16594d = c1512bArr3;
        }
        int i9 = this.f16595e;
        this.f16595e = i9 - 1;
        this.f16594d[i9] = c1512b;
        this.f16596f++;
        this.f16597g += i5;
    }

    public final C1734j d() {
        int i;
        x7.B b8 = this.f16593c;
        byte bG = b8.g();
        byte[] bArr = l7.b.f14412a;
        int i5 = bG & 255;
        int i8 = 0;
        boolean z8 = (bG & 128) == 128;
        long jE = e(i5, 127);
        if (!z8) {
            return b8.j(jE);
        }
        C1731g c1731g = new C1731g();
        int[] iArr = y.f16704a;
        P6.g.e(b8, "source");
        Q0 q0 = y.f16706c;
        Q0 q02 = q0;
        int i9 = 0;
        for (long j8 = 0; j8 < jE; j8++) {
            byte bG2 = b8.g();
            byte[] bArr2 = l7.b.f14412a;
            i8 = (i8 << 8) | (bG2 & 255);
            i9 += 8;
            while (i9 >= 8) {
                Q0[] q0Arr = (Q0[]) q02.f8514s;
                P6.g.b(q0Arr);
                q02 = q0Arr[(i8 >>> (i9 - 8)) & 255];
                P6.g.b(q02);
                if (((Q0[]) q02.f8514s) == null) {
                    c1731g.u0(q02.f8512q);
                    i9 -= q02.f8513r;
                    q02 = q0;
                } else {
                    i9 -= 8;
                }
            }
        }
        while (i9 > 0) {
            Q0[] q0Arr2 = (Q0[]) q02.f8514s;
            P6.g.b(q0Arr2);
            Q0 q03 = q0Arr2[(i8 << (8 - i9)) & 255];
            P6.g.b(q03);
            if (((Q0[]) q03.f8514s) != null || (i = q03.f8513r) > i9) {
                break;
            }
            c1731g.u0(q03.f8512q);
            i9 -= i;
            q02 = q0;
        }
        return c1731g.f0(c1731g.f18765r);
    }

    public final int e(int i, int i5) {
        int i8 = i & i5;
        if (i8 < i5) {
            return i8;
        }
        int i9 = 0;
        while (true) {
            byte bG = this.f16593c.g();
            byte[] bArr = l7.b.f14412a;
            int i10 = bG & 255;
            if ((bG & 128) == 0) {
                return i5 + (i10 << i9);
            }
            i5 += (bG & 127) << i9;
            i9 += 7;
        }
    }
}
