package com.google.protobuf;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/* JADX INFO: renamed from: com.google.protobuf.q, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes3.dex */
public final class C1326q extends AbstractC1329s {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final InputStream f14333d;

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

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

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

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public int f14340k = Integer.MAX_VALUE;

    public C1326q(InputStream inputStream) {
        Z.a(inputStream, "input");
        this.f14333d = inputStream;
        this.f14334e = new byte[4096];
        this.f14335f = 0;
        this.f14337h = 0;
        this.f14339j = 0;
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final int A() {
        return I();
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final long B() {
        return J();
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final boolean C(int i6) throws C1297b0 {
        int iZ;
        int i7 = i6 & 7;
        int i8 = 0;
        if (i7 == 0) {
            int i9 = this.f14335f - this.f14337h;
            byte[] bArr = this.f14334e;
            if (i9 >= 10) {
                while (i8 < 10) {
                    int i10 = this.f14337h;
                    this.f14337h = i10 + 1;
                    if (bArr[i10] < 0) {
                        i8++;
                    }
                }
                throw C1297b0.e();
            }
            while (i8 < 10) {
                if (this.f14337h == this.f14335f) {
                    M(1);
                }
                int i11 = this.f14337h;
                this.f14337h = i11 + 1;
                if (bArr[i11] < 0) {
                    i8++;
                }
            }
            throw C1297b0.e();
            return true;
        }
        if (i7 == 1) {
            N(8);
            return true;
        }
        if (i7 == 2) {
            N(I());
            return true;
        }
        if (i7 != 3) {
            if (i7 == 4) {
                return false;
            }
            if (i7 != 5) {
                throw C1297b0.d();
            }
            N(4);
            return true;
        }
        do {
            iZ = z();
            if (iZ == 0) {
                break;
            }
        } while (C(iZ));
        a(((i6 >>> 3) << 3) | 4);
        return true;
    }

    public final byte[] D(int i6) throws IOException {
        byte[] bArrE = E(i6);
        if (bArrE != null) {
            return bArrE;
        }
        int i7 = this.f14337h;
        int i8 = this.f14335f;
        int length = i8 - i7;
        this.f14339j += i8;
        this.f14337h = 0;
        this.f14335f = 0;
        ArrayList<byte[]> arrayListF = F(i6 - length);
        byte[] bArr = new byte[i6];
        System.arraycopy(this.f14334e, i7, bArr, 0, length);
        for (byte[] bArr2 : arrayListF) {
            System.arraycopy(bArr2, 0, bArr, length, bArr2.length);
            length += bArr2.length;
        }
        return bArr;
    }

    public final byte[] E(int i6) throws IOException {
        if (i6 == 0) {
            return Z.f14235b;
        }
        if (i6 < 0) {
            throw C1297b0.f();
        }
        int i7 = this.f14339j;
        int i8 = this.f14337h;
        int i9 = i7 + i8 + i6;
        if (i9 - Integer.MAX_VALUE > 0) {
            throw new C1297b0("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
        }
        int i10 = this.f14340k;
        if (i9 > i10) {
            N((i10 - i7) - i8);
            throw C1297b0.h();
        }
        int i11 = this.f14335f - i8;
        int i12 = i6 - i11;
        InputStream inputStream = this.f14333d;
        if (i12 >= 4096) {
            try {
                if (i12 > inputStream.available()) {
                    return null;
                }
            } catch (C1297b0 e6) {
                e6.f14239a = true;
                throw e6;
            }
        }
        byte[] bArr = new byte[i6];
        System.arraycopy(this.f14334e, this.f14337h, bArr, 0, i11);
        this.f14339j += this.f14335f;
        this.f14337h = 0;
        this.f14335f = 0;
        while (i11 < i6) {
            try {
                int i13 = inputStream.read(bArr, i11, i6 - i11);
                if (i13 == -1) {
                    throw C1297b0.h();
                }
                this.f14339j += i13;
                i11 += i13;
            } catch (C1297b0 e7) {
                e7.f14239a = true;
                throw e7;
            }
        }
        return bArr;
    }

    public final ArrayList F(int i6) throws IOException {
        ArrayList arrayList = new ArrayList();
        while (i6 > 0) {
            int iMin = Math.min(i6, 4096);
            byte[] bArr = new byte[iMin];
            int i7 = 0;
            while (i7 < iMin) {
                int i8 = this.f14333d.read(bArr, i7, iMin - i7);
                if (i8 == -1) {
                    throw C1297b0.h();
                }
                this.f14339j += i8;
                i7 += i8;
            }
            i6 -= iMin;
            arrayList.add(bArr);
        }
        return arrayList;
    }

    public final int G() throws C1297b0 {
        int i6 = this.f14337h;
        if (this.f14335f - i6 < 4) {
            M(4);
            i6 = this.f14337h;
        }
        this.f14337h = i6 + 4;
        byte[] bArr = this.f14334e;
        return ((bArr[i6 + 3] & 255) << 24) | (bArr[i6] & 255) | ((bArr[i6 + 1] & 255) << 8) | ((bArr[i6 + 2] & 255) << 16);
    }

    public final long H() throws C1297b0 {
        int i6 = this.f14337h;
        if (this.f14335f - i6 < 8) {
            M(8);
            i6 = this.f14337h;
        }
        this.f14337h = i6 + 8;
        byte[] bArr = this.f14334e;
        return ((((long) bArr[i6 + 7]) & 255) << 56) | (((long) bArr[i6]) & 255) | ((((long) bArr[i6 + 1]) & 255) << 8) | ((((long) bArr[i6 + 2]) & 255) << 16) | ((((long) bArr[i6 + 3]) & 255) << 24) | ((((long) bArr[i6 + 4]) & 255) << 32) | ((((long) bArr[i6 + 5]) & 255) << 40) | ((((long) bArr[i6 + 6]) & 255) << 48);
    }

    public final int I() {
        int i6;
        int i7 = this.f14337h;
        int i8 = this.f14335f;
        if (i8 != i7) {
            int i9 = i7 + 1;
            byte[] bArr = this.f14334e;
            byte b5 = bArr[i7];
            if (b5 >= 0) {
                this.f14337h = i9;
                return b5;
            }
            if (i8 - i9 >= 9) {
                int i10 = i7 + 2;
                int i11 = (bArr[i9] << 7) ^ b5;
                if (i11 < 0) {
                    i6 = i11 ^ (-128);
                } else {
                    int i12 = i7 + 3;
                    int i13 = (bArr[i10] << 14) ^ i11;
                    if (i13 >= 0) {
                        i6 = i13 ^ 16256;
                    } else {
                        int i14 = i7 + 4;
                        int i15 = i13 ^ (bArr[i12] << 21);
                        if (i15 < 0) {
                            i6 = (-2080896) ^ i15;
                        } else {
                            i12 = i7 + 5;
                            byte b6 = bArr[i14];
                            int i16 = (i15 ^ (b6 << 28)) ^ 266354560;
                            if (b6 < 0) {
                                i14 = i7 + 6;
                                if (bArr[i12] < 0) {
                                    i12 = i7 + 7;
                                    if (bArr[i14] < 0) {
                                        i14 = i7 + 8;
                                        if (bArr[i12] < 0) {
                                            i12 = i7 + 9;
                                            if (bArr[i14] < 0) {
                                                int i17 = i7 + 10;
                                                if (bArr[i12] >= 0) {
                                                    i10 = i17;
                                                    i6 = i16;
                                                }
                                            }
                                        }
                                    }
                                }
                                i6 = i16;
                            }
                            i6 = i16;
                        }
                        i10 = i14;
                    }
                    i10 = i12;
                }
                this.f14337h = i10;
                return i6;
            }
        }
        return (int) K();
    }

    public final long J() {
        long j4;
        long j6;
        long j7;
        long j8;
        int i6 = this.f14337h;
        int i7 = this.f14335f;
        if (i7 != i6) {
            int i8 = i6 + 1;
            byte[] bArr = this.f14334e;
            byte b5 = bArr[i6];
            if (b5 >= 0) {
                this.f14337h = i8;
                return b5;
            }
            if (i7 - i8 >= 9) {
                int i9 = i6 + 2;
                int i10 = (bArr[i8] << 7) ^ b5;
                if (i10 < 0) {
                    j4 = i10 ^ (-128);
                } else {
                    int i11 = i6 + 3;
                    int i12 = (bArr[i9] << 14) ^ i10;
                    if (i12 >= 0) {
                        j4 = i12 ^ 16256;
                        i9 = i11;
                    } else {
                        int i13 = i6 + 4;
                        int i14 = i12 ^ (bArr[i11] << 21);
                        if (i14 < 0) {
                            j8 = (-2080896) ^ i14;
                        } else {
                            long j9 = i14;
                            i9 = i6 + 5;
                            long j10 = j9 ^ (((long) bArr[i13]) << 28);
                            if (j10 >= 0) {
                                j7 = 266354560;
                            } else {
                                i13 = i6 + 6;
                                long j11 = j10 ^ (((long) bArr[i9]) << 35);
                                if (j11 < 0) {
                                    j6 = -34093383808L;
                                } else {
                                    i9 = i6 + 7;
                                    j10 = j11 ^ (((long) bArr[i13]) << 42);
                                    if (j10 >= 0) {
                                        j7 = 4363953127296L;
                                    } else {
                                        i13 = i6 + 8;
                                        j11 = j10 ^ (((long) bArr[i9]) << 49);
                                        if (j11 < 0) {
                                            j6 = -558586000294016L;
                                        } else {
                                            i9 = i6 + 9;
                                            long j12 = (j11 ^ (((long) bArr[i13]) << 56)) ^ 71499008037633920L;
                                            if (j12 < 0) {
                                                int i15 = i6 + 10;
                                                if (bArr[i9] >= 0) {
                                                    i9 = i15;
                                                }
                                            }
                                            j4 = j12;
                                        }
                                    }
                                }
                                j8 = j6 ^ j11;
                            }
                            j4 = j7 ^ j10;
                        }
                        i9 = i13;
                        j4 = j8;
                    }
                }
                this.f14337h = i9;
                return j4;
            }
        }
        return K();
    }

    public final long K() throws C1297b0 {
        long j4 = 0;
        for (int i6 = 0; i6 < 64; i6 += 7) {
            if (this.f14337h == this.f14335f) {
                M(1);
            }
            int i7 = this.f14337h;
            this.f14337h = i7 + 1;
            byte b5 = this.f14334e[i7];
            j4 |= ((long) (b5 & 127)) << i6;
            if ((b5 & 128) == 0) {
                return j4;
            }
        }
        throw C1297b0.e();
    }

    public final void L() {
        int i6 = this.f14335f + this.f14336g;
        this.f14335f = i6;
        int i7 = this.f14339j + i6;
        int i8 = this.f14340k;
        if (i7 <= i8) {
            this.f14336g = 0;
            return;
        }
        int i9 = i7 - i8;
        this.f14336g = i9;
        this.f14335f = i6 - i9;
    }

    public final void M(int i6) throws C1297b0 {
        if (O(i6)) {
            return;
        }
        if (i6 <= (Integer.MAX_VALUE - this.f14339j) - this.f14337h) {
            throw C1297b0.h();
        }
        throw new C1297b0("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
    }

    public final void N(int i6) throws C1297b0 {
        int i7 = this.f14335f;
        int i8 = this.f14337h;
        if (i6 <= i7 - i8 && i6 >= 0) {
            this.f14337h = i8 + i6;
            return;
        }
        InputStream inputStream = this.f14333d;
        if (i6 < 0) {
            throw C1297b0.f();
        }
        int i9 = this.f14339j;
        int i10 = i9 + i8;
        int i11 = i10 + i6;
        int i12 = this.f14340k;
        if (i11 > i12) {
            N((i12 - i9) - i8);
            throw C1297b0.h();
        }
        this.f14339j = i10;
        int i13 = i7 - i8;
        this.f14335f = 0;
        this.f14337h = 0;
        while (i13 < i6) {
            long j4 = i6 - i13;
            try {
                try {
                    long jSkip = inputStream.skip(j4);
                    if (jSkip < 0 || jSkip > j4) {
                        throw new IllegalStateException(inputStream.getClass() + "#skip returned invalid result: " + jSkip + "\nThe InputStream implementation is buggy.");
                    }
                    if (jSkip == 0) {
                        break;
                    } else {
                        i13 += (int) jSkip;
                    }
                } catch (C1297b0 e6) {
                    e6.f14239a = true;
                    throw e6;
                }
            } catch (Throwable th) {
                this.f14339j += i13;
                L();
                throw th;
            }
        }
        this.f14339j += i13;
        L();
        if (i13 >= i6) {
            return;
        }
        int i14 = this.f14335f;
        int i15 = i14 - this.f14337h;
        this.f14337h = i14;
        M(1);
        while (true) {
            int i16 = i6 - i15;
            int i17 = this.f14335f;
            if (i16 <= i17) {
                this.f14337h = i16;
                return;
            } else {
                i15 += i17;
                this.f14337h = i17;
                M(1);
            }
        }
    }

    public final boolean O(int i6) throws IOException {
        int i7 = this.f14337h;
        int i8 = i7 + i6;
        int i9 = this.f14335f;
        if (i8 <= i9) {
            throw new IllegalStateException(Z0.o.j(i6, "refillBuffer() called when ", " bytes were already available in buffer"));
        }
        int i10 = this.f14339j;
        if (i6 <= (Integer.MAX_VALUE - i10) - i7 && i10 + i7 + i6 <= this.f14340k) {
            byte[] bArr = this.f14334e;
            if (i7 > 0) {
                if (i9 > i7) {
                    System.arraycopy(bArr, i7, bArr, 0, i9 - i7);
                }
                this.f14339j += i7;
                this.f14335f -= i7;
                this.f14337h = 0;
            }
            int i11 = this.f14335f;
            int iMin = Math.min(bArr.length - i11, (Integer.MAX_VALUE - this.f14339j) - i11);
            InputStream inputStream = this.f14333d;
            try {
                int i12 = inputStream.read(bArr, i11, iMin);
                if (i12 == 0 || i12 < -1 || i12 > bArr.length) {
                    throw new IllegalStateException(inputStream.getClass() + "#read(byte[]) returned invalid result: " + i12 + "\nThe InputStream implementation is buggy.");
                }
                if (i12 > 0) {
                    this.f14335f += i12;
                    L();
                    if (this.f14335f >= i6) {
                        return true;
                    }
                    return O(i6);
                }
            } catch (C1297b0 e6) {
                e6.f14239a = true;
                throw e6;
            }
        }
        return false;
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final void a(int i6) throws C1297b0 {
        if (this.f14338i != i6) {
            throw C1297b0.a();
        }
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final int d() {
        return this.f14339j + this.f14337h;
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final boolean e() {
        return this.f14337h == this.f14335f && !O(1);
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final void h(int i6) {
        this.f14340k = i6;
        L();
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final int i(int i6) throws C1297b0 {
        if (i6 < 0) {
            throw C1297b0.f();
        }
        int i7 = this.f14339j + this.f14337h + i6;
        int i8 = this.f14340k;
        if (i7 > i8) {
            throw C1297b0.h();
        }
        this.f14340k = i7;
        L();
        return i8;
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final boolean j() {
        return J() != 0;
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final C1318m k() throws IOException {
        int I5 = I();
        int i6 = this.f14335f;
        int i7 = this.f14337h;
        int i8 = i6 - i7;
        byte[] bArr = this.f14334e;
        if (I5 <= i8 && I5 > 0) {
            C1318m c1318mL = AbstractC1320n.l(bArr, i7, I5);
            this.f14337h += I5;
            return c1318mL;
        }
        if (I5 == 0) {
            return AbstractC1320n.f14310b;
        }
        byte[] bArrE = E(I5);
        if (bArrE != null) {
            return AbstractC1320n.l(bArrE, 0, bArrE.length);
        }
        int i9 = this.f14337h;
        int i10 = this.f14335f;
        int length = i10 - i9;
        this.f14339j += i10;
        this.f14337h = 0;
        this.f14335f = 0;
        ArrayList<byte[]> arrayListF = F(I5 - length);
        byte[] bArr2 = new byte[I5];
        System.arraycopy(bArr, i9, bArr2, 0, length);
        for (byte[] bArr3 : arrayListF) {
            System.arraycopy(bArr3, 0, bArr2, length, bArr3.length);
            length += bArr3.length;
        }
        C1318m c1318m = AbstractC1320n.f14310b;
        return new C1318m(bArr2);
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final double l() {
        return Double.longBitsToDouble(H());
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final int m() {
        return I();
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final int n() {
        return G();
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final long o() {
        return H();
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final float p() {
        return Float.intBitsToFloat(G());
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final int q() {
        return I();
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final long r() {
        return J();
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final int t() {
        return G();
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final long u() {
        return H();
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final int v() {
        return AbstractC1329s.b(I());
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final long w() {
        return AbstractC1329s.c(J());
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final String x() throws C1297b0 {
        int I5 = I();
        byte[] bArr = this.f14334e;
        if (I5 > 0) {
            int i6 = this.f14335f;
            int i7 = this.f14337h;
            if (I5 <= i6 - i7) {
                String str = new String(bArr, i7, I5, Z.f14234a);
                this.f14337h += I5;
                return str;
            }
        }
        if (I5 == 0) {
            return "";
        }
        if (I5 > this.f14335f) {
            return new String(D(I5), Z.f14234a);
        }
        M(I5);
        String str2 = new String(bArr, this.f14337h, I5, Z.f14234a);
        this.f14337h += I5;
        return str2;
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final String y() throws IOException {
        int I5 = I();
        int i6 = this.f14337h;
        int i7 = this.f14335f;
        int i8 = i7 - i6;
        byte[] bArrD = this.f14334e;
        if (I5 <= i8 && I5 > 0) {
            this.f14337h = i6 + I5;
        } else {
            if (I5 == 0) {
                return "";
            }
            i6 = 0;
            if (I5 <= i7) {
                M(I5);
                this.f14337h = I5;
            } else {
                bArrD = D(I5);
            }
        }
        return f1.f14272a.j(bArrD, i6, I5);
    }

    @Override // com.google.protobuf.AbstractC1329s
    public final int z() throws C1297b0 {
        if (e()) {
            this.f14338i = 0;
            return 0;
        }
        int I5 = I();
        this.f14338i = I5;
        if ((I5 >>> 3) != 0) {
            return I5;
        }
        throw C1297b0.b();
    }
}
