package kotlin.reflect.jvm.internal.impl.protobuf;

import com.google.android.gms.common.api.Api;
import com.google.android.gms.dynamite.descriptors.com.google.android.gms.measurement.dynamite.ModuleDescriptor;
import java.io.InputStream;
import java.util.ArrayList;
import kotlin.reflect.jvm.internal.impl.protobuf.p;

/* loaded from: classes3.dex */
public final class e {

    /* renamed from: d, reason: collision with root package name */
    private int f26253d;

    /* renamed from: f, reason: collision with root package name */
    private final InputStream f26255f;

    /* renamed from: g, reason: collision with root package name */
    private int f26256g;

    /* renamed from: k, reason: collision with root package name */
    private int f26260k;

    /* renamed from: h, reason: collision with root package name */
    private boolean f26257h = false;

    /* renamed from: j, reason: collision with root package name */
    private int f26259j = Api.BaseClientBuilder.API_PRIORITY_OTHER;

    /* renamed from: l, reason: collision with root package name */
    private int f26261l = 64;

    /* renamed from: m, reason: collision with root package name */
    private int f26262m = 67108864;

    /* renamed from: a, reason: collision with root package name */
    private final byte[] f26250a = new byte[4096];

    /* renamed from: c, reason: collision with root package name */
    private int f26252c = 0;

    /* renamed from: e, reason: collision with root package name */
    private int f26254e = 0;

    /* renamed from: i, reason: collision with root package name */
    private int f26258i = 0;

    /* renamed from: b, reason: collision with root package name */
    private final boolean f26251b = false;

    private e(InputStream inputStream) {
        this.f26255f = inputStream;
    }

    public static int A(int i10, InputStream inputStream) {
        if ((i10 & 128) == 0) {
            return i10;
        }
        int i11 = i10 & ModuleDescriptor.MODULE_VERSION;
        int i12 = 7;
        while (i12 < 32) {
            int read = inputStream.read();
            if (read == -1) {
                throw k.k();
            }
            i11 |= (read & ModuleDescriptor.MODULE_VERSION) << i12;
            if ((read & 128) == 0) {
                return i11;
            }
            i12 += 7;
        }
        while (i12 < 64) {
            int read2 = inputStream.read();
            if (read2 == -1) {
                throw k.k();
            }
            if ((read2 & 128) == 0) {
                return i11;
            }
            i12 += 7;
        }
        throw k.f();
    }

    private void M() {
        int i10 = this.f26252c + this.f26253d;
        this.f26252c = i10;
        int i11 = this.f26258i + i10;
        int i12 = this.f26259j;
        if (i11 <= i12) {
            this.f26253d = 0;
            return;
        }
        int i13 = i11 - i12;
        this.f26253d = i13;
        this.f26252c = i10 - i13;
    }

    private void N(int i10) {
        if (!S(i10)) {
            throw k.k();
        }
    }

    private void R(int i10) {
        if (i10 < 0) {
            throw k.g();
        }
        int i11 = this.f26258i;
        int i12 = this.f26254e;
        int i13 = i11 + i12 + i10;
        int i14 = this.f26259j;
        if (i13 > i14) {
            Q((i14 - i11) - i12);
            throw k.k();
        }
        int i15 = this.f26252c;
        int i16 = i15 - i12;
        this.f26254e = i15;
        N(1);
        while (true) {
            int i17 = i10 - i16;
            int i18 = this.f26252c;
            if (i17 <= i18) {
                this.f26254e = i17;
                return;
            } else {
                i16 += i18;
                this.f26254e = i18;
                N(1);
            }
        }
    }

    private boolean S(int i10) {
        int i11 = this.f26254e;
        int i12 = i11 + i10;
        int i13 = this.f26252c;
        if (i12 <= i13) {
            StringBuilder sb2 = new StringBuilder(77);
            sb2.append("refillBuffer() called when ");
            sb2.append(i10);
            sb2.append(" bytes were already available in buffer");
            throw new IllegalStateException(sb2.toString());
        }
        if (this.f26258i + i11 + i10 <= this.f26259j && this.f26255f != null) {
            if (i11 > 0) {
                if (i13 > i11) {
                    byte[] bArr = this.f26250a;
                    System.arraycopy(bArr, i11, bArr, 0, i13 - i11);
                }
                this.f26258i += i11;
                this.f26252c -= i11;
                this.f26254e = 0;
            }
            InputStream inputStream = this.f26255f;
            byte[] bArr2 = this.f26250a;
            int i14 = this.f26252c;
            int read = inputStream.read(bArr2, i14, bArr2.length - i14);
            if (read == 0 || read < -1 || read > this.f26250a.length) {
                StringBuilder sb3 = new StringBuilder(102);
                sb3.append("InputStream#read(byte[]) returned invalid result: ");
                sb3.append(read);
                sb3.append("\nThe InputStream implementation is buggy.");
                throw new IllegalStateException(sb3.toString());
            }
            if (read > 0) {
                this.f26252c += read;
                if ((this.f26258i + i10) - this.f26262m > 0) {
                    throw k.j();
                }
                M();
                if (this.f26252c >= i10) {
                    return true;
                }
                return S(i10);
            }
        }
        return false;
    }

    public static int b(int i10) {
        return (-(i10 & 1)) ^ (i10 >>> 1);
    }

    public static long c(long j10) {
        return (-(j10 & 1)) ^ (j10 >>> 1);
    }

    private void d(int i10) {
        if (this.f26252c - this.f26254e < i10) {
            N(i10);
        }
    }

    public static e g(InputStream inputStream) {
        return new e(inputStream);
    }

    private byte[] w(int i10) {
        if (i10 <= 0) {
            if (i10 == 0) {
                return j.f26298a;
            }
            throw k.g();
        }
        int i11 = this.f26258i;
        int i12 = this.f26254e;
        int i13 = i11 + i12 + i10;
        int i14 = this.f26259j;
        if (i13 > i14) {
            Q((i14 - i11) - i12);
            throw k.k();
        }
        if (i10 < 4096) {
            byte[] bArr = new byte[i10];
            int i15 = this.f26252c - i12;
            System.arraycopy(this.f26250a, i12, bArr, 0, i15);
            this.f26254e = this.f26252c;
            int i16 = i10 - i15;
            d(i16);
            System.arraycopy(this.f26250a, 0, bArr, i15, i16);
            this.f26254e = i16;
            return bArr;
        }
        int i17 = this.f26252c;
        this.f26258i = i11 + i17;
        this.f26254e = 0;
        this.f26252c = 0;
        int i18 = i17 - i12;
        int i19 = i10 - i18;
        ArrayList<byte[]> arrayList = new ArrayList();
        while (i19 > 0) {
            int min = Math.min(i19, 4096);
            byte[] bArr2 = new byte[min];
            int i20 = 0;
            while (i20 < min) {
                InputStream inputStream = this.f26255f;
                int read = inputStream == null ? -1 : inputStream.read(bArr2, i20, min - i20);
                if (read == -1) {
                    throw k.k();
                }
                this.f26258i += read;
                i20 += read;
            }
            i19 -= min;
            arrayList.add(bArr2);
        }
        byte[] bArr3 = new byte[i10];
        System.arraycopy(this.f26250a, i12, bArr3, 0, i18);
        for (byte[] bArr4 : arrayList) {
            System.arraycopy(bArr4, 0, bArr3, i18, bArr4.length);
            i18 += bArr4.length;
        }
        return bArr3;
    }

    /* JADX WARN: Code restructure failed: missing block: B:38:0x00b6, code lost:
    
        if (r2[r0] < 0) goto L39;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public long B() {
        long j10;
        long j11;
        long j12;
        int i10 = this.f26254e;
        int i11 = this.f26252c;
        if (i11 != i10) {
            byte[] bArr = this.f26250a;
            int i12 = i10 + 1;
            byte b10 = bArr[i10];
            if (b10 >= 0) {
                this.f26254e = i12;
                return b10;
            }
            if (i11 - i12 >= 9) {
                int i13 = i12 + 1;
                long j13 = b10 ^ (bArr[i12] << 7);
                if (j13 >= 0) {
                    int i14 = i13 + 1;
                    long j14 = j13 ^ (bArr[i13] << 14);
                    if (j14 >= 0) {
                        j12 = 16256;
                    } else {
                        i13 = i14 + 1;
                        j13 = j14 ^ (bArr[i14] << 21);
                        if (j13 < 0) {
                            j11 = -2080896;
                        } else {
                            i14 = i13 + 1;
                            j14 = j13 ^ (bArr[i13] << 28);
                            if (j14 >= 0) {
                                j12 = 266354560;
                            } else {
                                i13 = i14 + 1;
                                j13 = j14 ^ (bArr[i14] << 35);
                                if (j13 < 0) {
                                    j11 = -34093383808L;
                                } else {
                                    i14 = i13 + 1;
                                    j14 = j13 ^ (bArr[i13] << 42);
                                    if (j14 >= 0) {
                                        j12 = 4363953127296L;
                                    } else {
                                        i13 = i14 + 1;
                                        j13 = j14 ^ (bArr[i14] << 49);
                                        if (j13 >= 0) {
                                            int i15 = i13 + 1;
                                            long j15 = (j13 ^ (bArr[i13] << 56)) ^ 71499008037633920L;
                                            i13 = j15 < 0 ? i15 + 1 : i15;
                                            j10 = j15;
                                            this.f26254e = i13;
                                            return j10;
                                        }
                                        j11 = -558586000294016L;
                                    }
                                }
                            }
                        }
                    }
                    j10 = j14 ^ j12;
                    i13 = i14;
                    this.f26254e = i13;
                    return j10;
                }
                j11 = -128;
                j10 = j13 ^ j11;
                this.f26254e = i13;
                return j10;
            }
        }
        return C();
    }

    long C() {
        long j10 = 0;
        for (int i10 = 0; i10 < 64; i10 += 7) {
            j10 |= (r3 & Byte.MAX_VALUE) << i10;
            if ((v() & 128) == 0) {
                return j10;
            }
        }
        throw k.f();
    }

    public int D() {
        return x();
    }

    public long E() {
        return y();
    }

    public int F() {
        return b(z());
    }

    public long G() {
        return c(B());
    }

    public String H() {
        int z10 = z();
        int i10 = this.f26252c;
        int i11 = this.f26254e;
        if (z10 > i10 - i11 || z10 <= 0) {
            return z10 == 0 ? "" : new String(w(z10), "UTF-8");
        }
        String str = new String(this.f26250a, i11, z10, "UTF-8");
        this.f26254e += z10;
        return str;
    }

    public String I() {
        byte[] w10;
        int z10 = z();
        int i10 = this.f26254e;
        if (z10 <= this.f26252c - i10 && z10 > 0) {
            w10 = this.f26250a;
            this.f26254e = i10 + z10;
        } else {
            if (z10 == 0) {
                return "";
            }
            w10 = w(z10);
            i10 = 0;
        }
        if (x.f(w10, i10, i10 + z10)) {
            return new String(w10, i10, z10, "UTF-8");
        }
        throw k.d();
    }

    public int J() {
        if (f()) {
            this.f26256g = 0;
            return 0;
        }
        int z10 = z();
        this.f26256g = z10;
        if (y.a(z10) != 0) {
            return this.f26256g;
        }
        throw k.c();
    }

    public int K() {
        return z();
    }

    public long L() {
        return B();
    }

    public boolean O(int i10, f fVar) {
        int b10 = y.b(i10);
        if (b10 == 0) {
            long s10 = s();
            fVar.n0(i10);
            fVar.y0(s10);
            return true;
        }
        if (b10 == 1) {
            long y10 = y();
            fVar.n0(i10);
            fVar.U(y10);
            return true;
        }
        if (b10 == 2) {
            d k10 = k();
            fVar.n0(i10);
            fVar.O(k10);
            return true;
        }
        if (b10 == 3) {
            fVar.n0(i10);
            P(fVar);
            int c10 = y.c(y.a(i10), 4);
            a(c10);
            fVar.n0(c10);
            return true;
        }
        if (b10 == 4) {
            return false;
        }
        if (b10 != 5) {
            throw k.e();
        }
        int x10 = x();
        fVar.n0(i10);
        fVar.T(x10);
        return true;
    }

    public void P(f fVar) {
        int J;
        do {
            J = J();
            if (J == 0) {
                return;
            }
        } while (O(J, fVar));
    }

    public void Q(int i10) {
        int i11 = this.f26252c;
        int i12 = this.f26254e;
        if (i10 > i11 - i12 || i10 < 0) {
            R(i10);
        } else {
            this.f26254e = i12 + i10;
        }
    }

    public void a(int i10) {
        if (this.f26256g != i10) {
            throw k.b();
        }
    }

    public int e() {
        int i10 = this.f26259j;
        if (i10 == Integer.MAX_VALUE) {
            return -1;
        }
        return i10 - (this.f26258i + this.f26254e);
    }

    public boolean f() {
        return this.f26254e == this.f26252c && !S(1);
    }

    public void h(int i10) {
        this.f26259j = i10;
        M();
    }

    public int i(int i10) {
        if (i10 < 0) {
            throw k.g();
        }
        int i11 = i10 + this.f26258i + this.f26254e;
        int i12 = this.f26259j;
        if (i11 > i12) {
            throw k.k();
        }
        this.f26259j = i11;
        M();
        return i12;
    }

    public boolean j() {
        return B() != 0;
    }

    public d k() {
        int z10 = z();
        int i10 = this.f26252c;
        int i11 = this.f26254e;
        if (z10 > i10 - i11 || z10 <= 0) {
            return z10 == 0 ? d.f26243p : new o(w(z10));
        }
        d cVar = (this.f26251b && this.f26257h) ? new c(this.f26250a, this.f26254e, z10) : d.l(this.f26250a, i11, z10);
        this.f26254e += z10;
        return cVar;
    }

    public double l() {
        return Double.longBitsToDouble(y());
    }

    public int m() {
        return z();
    }

    public int n() {
        return x();
    }

    public long o() {
        return y();
    }

    public float p() {
        return Float.intBitsToFloat(x());
    }

    public void q(int i10, p.a aVar, g gVar) {
        int i11 = this.f26260k;
        if (i11 >= this.f26261l) {
            throw k.h();
        }
        this.f26260k = i11 + 1;
        aVar.I(this, gVar);
        a(y.c(i10, 4));
        this.f26260k--;
    }

    public int r() {
        return z();
    }

    public long s() {
        return B();
    }

    public p t(r rVar, g gVar) {
        int z10 = z();
        if (this.f26260k >= this.f26261l) {
            throw k.h();
        }
        int i10 = i(z10);
        this.f26260k++;
        p pVar = (p) rVar.b(this, gVar);
        a(0);
        this.f26260k--;
        h(i10);
        return pVar;
    }

    public void u(p.a aVar, g gVar) {
        int z10 = z();
        if (this.f26260k >= this.f26261l) {
            throw k.h();
        }
        int i10 = i(z10);
        this.f26260k++;
        aVar.I(this, gVar);
        a(0);
        this.f26260k--;
        h(i10);
    }

    public byte v() {
        if (this.f26254e == this.f26252c) {
            N(1);
        }
        byte[] bArr = this.f26250a;
        int i10 = this.f26254e;
        this.f26254e = i10 + 1;
        return bArr[i10];
    }

    public int x() {
        int i10 = this.f26254e;
        if (this.f26252c - i10 < 4) {
            N(4);
            i10 = this.f26254e;
        }
        byte[] bArr = this.f26250a;
        this.f26254e = i10 + 4;
        return (bArr[i10] & 255) | ((bArr[i10 + 1] & 255) << 8) | ((bArr[i10 + 2] & 255) << 16) | ((bArr[i10 + 3] & 255) << 24);
    }

    public long y() {
        int i10 = this.f26254e;
        if (this.f26252c - i10 < 8) {
            N(8);
            i10 = this.f26254e;
        }
        byte[] bArr = this.f26250a;
        this.f26254e = i10 + 8;
        return ((bArr[i10 + 7] & 255) << 56) | (bArr[i10] & 255) | ((bArr[i10 + 1] & 255) << 8) | ((bArr[i10 + 2] & 255) << 16) | ((bArr[i10 + 3] & 255) << 24) | ((bArr[i10 + 4] & 255) << 32) | ((bArr[i10 + 5] & 255) << 40) | ((bArr[i10 + 6] & 255) << 48);
    }

    /* JADX WARN: Code restructure failed: missing block: B:34:0x007a, code lost:
    
        if (r2[r3] < 0) goto L35;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public int z() {
        int i10;
        long j10;
        int i11 = this.f26254e;
        int i12 = this.f26252c;
        if (i12 != i11) {
            byte[] bArr = this.f26250a;
            int i13 = i11 + 1;
            byte b10 = bArr[i11];
            if (b10 >= 0) {
                this.f26254e = i13;
                return b10;
            }
            if (i12 - i13 >= 9) {
                int i14 = i13 + 1;
                int i15 = b10 ^ (bArr[i13] << 7);
                long j11 = i15;
                if (j11 >= 0) {
                    int i16 = i14 + 1;
                    int i17 = i15 ^ (bArr[i14] << 14);
                    long j12 = i17;
                    if (j12 >= 0) {
                        i10 = (int) (16256 ^ j12);
                    } else {
                        i14 = i16 + 1;
                        j11 = i17 ^ (bArr[i16] << 21);
                        if (j11 < 0) {
                            j10 = -2080896;
                        } else {
                            i16 = i14 + 1;
                            i10 = (int) ((r0 ^ (r1 << 28)) ^ 266354560);
                            if (bArr[i14] < 0) {
                                i14 = i16 + 1;
                                if (bArr[i16] < 0) {
                                    i16 = i14 + 1;
                                    if (bArr[i14] < 0) {
                                        i14 = i16 + 1;
                                        if (bArr[i16] < 0) {
                                            i16 = i14 + 1;
                                            if (bArr[i14] < 0) {
                                                i14 = i16 + 1;
                                            }
                                        }
                                    }
                                }
                                this.f26254e = i14;
                                return i10;
                            }
                        }
                    }
                    i14 = i16;
                    this.f26254e = i14;
                    return i10;
                }
                j10 = -128;
                i10 = (int) (j11 ^ j10);
                this.f26254e = i14;
                return i10;
            }
        }
        return (int) C();
    }
}
