package C;

import b0.AbstractC0399d;
import java.util.Stack;
import n.z;
import okhttp3.HttpUrl;

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final Stack f192b = new Stack();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final f f193c = new f();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private d f194d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private int f195e;

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private long f197g;

    /* JADX INFO: renamed from: C.a$a, reason: collision with other inner class name */
    private static final class C0001a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final int f198a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final long f199b;

        private C0001a(int i2, long j2) {
            this.f198a = i2;
            this.f199b = j2;
        }
    }

    a() {
    }

    private long c(v.f fVar) {
        fVar.g();
        while (true) {
            fVar.i(this.f191a, 0, 4);
            int iC = f.c(this.f191a[0]);
            if (iC != -1 && iC <= 4) {
                int iA = (int) f.a(this.f191a, iC, false);
                if (this.f194d.c(iA)) {
                    fVar.h(iC);
                    return iA;
                }
            }
            fVar.h(1);
        }
    }

    private double d(v.f fVar, int i2) {
        return i2 == 4 ? Float.intBitsToFloat((int) r0) : Double.longBitsToDouble(e(fVar, i2));
    }

    private long e(v.f fVar, int i2) {
        fVar.readFully(this.f191a, 0, i2);
        long j2 = 0;
        for (int i3 = 0; i3 < i2; i3++) {
            j2 = (j2 << 8) | ((long) (this.f191a[i3] & 255));
        }
        return j2;
    }

    private String f(v.f fVar, int i2) {
        if (i2 == 0) {
            return HttpUrl.FRAGMENT_ENCODE_SET;
        }
        byte[] bArr = new byte[i2];
        fVar.readFully(bArr, 0, i2);
        return new String(bArr);
    }

    @Override // C.c
    public void a(d dVar) {
        this.f194d = dVar;
    }

    @Override // C.c
    public boolean b(v.f fVar) throws z {
        AbstractC0399d.e(this.f194d != null);
        while (true) {
            if (!this.f192b.isEmpty() && fVar.j() >= ((C0001a) this.f192b.peek()).f199b) {
                this.f194d.a(((C0001a) this.f192b.pop()).f198a);
                return true;
            }
            if (this.f195e == 0) {
                long jD = this.f193c.d(fVar, true, false, 4);
                if (jD == -2) {
                    jD = c(fVar);
                }
                if (jD == -1) {
                    return false;
                }
                this.f196f = (int) jD;
                this.f195e = 1;
            }
            if (this.f195e == 1) {
                this.f197g = this.f193c.d(fVar, false, true, 8);
                this.f195e = 2;
            }
            int iB = this.f194d.b(this.f196f);
            if (iB != 0) {
                if (iB == 1) {
                    long j2 = fVar.j();
                    this.f192b.add(new C0001a(this.f196f, this.f197g + j2));
                    this.f194d.g(this.f196f, j2, this.f197g);
                    this.f195e = 0;
                    return true;
                }
                if (iB == 2) {
                    long j3 = this.f197g;
                    if (j3 <= 8) {
                        this.f194d.h(this.f196f, e(fVar, (int) j3));
                        this.f195e = 0;
                        return true;
                    }
                    throw new z("Invalid integer size: " + this.f197g);
                }
                if (iB == 3) {
                    long j4 = this.f197g;
                    if (j4 <= 2147483647L) {
                        this.f194d.e(this.f196f, f(fVar, (int) j4));
                        this.f195e = 0;
                        return true;
                    }
                    throw new z("String element size: " + this.f197g);
                }
                if (iB == 4) {
                    this.f194d.d(this.f196f, (int) this.f197g, fVar);
                    this.f195e = 0;
                    return true;
                }
                if (iB != 5) {
                    throw new z("Invalid element type " + iB);
                }
                long j5 = this.f197g;
                if (j5 == 4 || j5 == 8) {
                    this.f194d.f(this.f196f, d(fVar, (int) j5));
                    this.f195e = 0;
                    return true;
                }
                throw new z("Invalid float size: " + this.f197g);
            }
            fVar.h((int) this.f197g);
            this.f195e = 0;
        }
    }

    @Override // C.c
    public void reset() {
        this.f195e = 0;
        this.f192b.clear();
        this.f193c.e();
    }
}
