package com.google.crypto.tink.shaded.protobuf;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;

/* JADX INFO: renamed from: com.google.crypto.tink.shaded.protobuf.k, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes3.dex */
public final class C1263k extends F1.L {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final ByteArrayInputStream f13801c;

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

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public int f13806k;
    public int l;

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

    public C1263k(ByteArrayInputStream byteArrayInputStream) {
        Charset charset = A.f13714a;
        this.f13801c = byteArrayInputStream;
        this.f13802d = new byte[4096];
        this.f13803e = 0;
        this.f13805j = 0;
        this.l = 0;
    }

    @Override // F1.L
    public final long A() {
        return R();
    }

    @Override // F1.L
    public final int B() {
        return O();
    }

    @Override // F1.L
    public final long C() {
        return P();
    }

    @Override // F1.L
    public final int D() {
        return F1.L.e(Q());
    }

    @Override // F1.L
    public final long E() {
        return F1.L.f(R());
    }

    @Override // F1.L
    public final String F() throws C {
        int iQ = Q();
        byte[] bArr = this.f13802d;
        if (iQ > 0) {
            int i6 = this.f13803e;
            int i7 = this.f13805j;
            if (iQ <= i6 - i7) {
                String str = new String(bArr, i7, iQ, A.f13714a);
                this.f13805j += iQ;
                return str;
            }
        }
        if (iQ == 0) {
            return "";
        }
        if (iQ > this.f13803e) {
            return new String(L(iQ), A.f13714a);
        }
        U(iQ);
        String str2 = new String(bArr, this.f13805j, iQ, A.f13714a);
        this.f13805j += iQ;
        return str2;
    }

    @Override // F1.L
    public final String G() throws IOException {
        int iQ = Q();
        int i6 = this.f13805j;
        int i7 = this.f13803e;
        int i8 = i7 - i6;
        byte[] bArrL = this.f13802d;
        if (iQ <= i8 && iQ > 0) {
            this.f13805j = i6 + iQ;
        } else {
            if (iQ == 0) {
                return "";
            }
            i6 = 0;
            if (iQ <= i7) {
                U(iQ);
                this.f13805j = iQ;
            } else {
                bArrL = L(iQ);
            }
        }
        return r0.f13834a.W(bArrL, i6, iQ);
    }

    @Override // F1.L
    public final int H() throws C {
        if (h()) {
            this.f13806k = 0;
            return 0;
        }
        int iQ = Q();
        this.f13806k = iQ;
        if ((iQ >>> 3) != 0) {
            return iQ;
        }
        throw C.a();
    }

    @Override // F1.L
    public final int I() {
        return Q();
    }

    @Override // F1.L
    public final long J() {
        return R();
    }

    public final byte[] L(int i6) throws IOException {
        byte[] bArrM = M(i6);
        if (bArrM != null) {
            return bArrM;
        }
        int i7 = this.f13805j;
        int i8 = this.f13803e;
        int length = i8 - i7;
        this.l += i8;
        this.f13805j = 0;
        this.f13803e = 0;
        ArrayList<byte[]> arrayListN = N(i6 - length);
        byte[] bArr = new byte[i6];
        System.arraycopy(this.f13802d, i7, bArr, 0, length);
        for (byte[] bArr2 : arrayListN) {
            System.arraycopy(bArr2, 0, bArr, length, bArr2.length);
            length += bArr2.length;
        }
        return bArr;
    }

    public final byte[] M(int i6) throws IOException {
        if (i6 == 0) {
            return A.f13715b;
        }
        if (i6 < 0) {
            throw C.e();
        }
        int i7 = this.l;
        int i8 = this.f13805j;
        int i9 = i7 + i8 + i6;
        if (i9 - Integer.MAX_VALUE > 0) {
            throw new C("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
        }
        int i10 = this.f13807m;
        if (i9 > i10) {
            V((i10 - i7) - i8);
            throw C.g();
        }
        int i11 = this.f13803e - i8;
        int i12 = i6 - i11;
        ByteArrayInputStream byteArrayInputStream = this.f13801c;
        if (i12 >= 4096) {
            try {
                if (i12 > byteArrayInputStream.available()) {
                    return null;
                }
            } catch (C e6) {
                e6.f13716a = true;
                throw e6;
            }
        }
        byte[] bArr = new byte[i6];
        System.arraycopy(this.f13802d, this.f13805j, bArr, 0, i11);
        this.l += this.f13803e;
        this.f13805j = 0;
        this.f13803e = 0;
        while (i11 < i6) {
            try {
                int i13 = byteArrayInputStream.read(bArr, i11, i6 - i11);
                if (i13 == -1) {
                    throw C.g();
                }
                this.l += i13;
                i11 += i13;
            } catch (C e7) {
                e7.f13716a = true;
                throw e7;
            }
        }
        return bArr;
    }

    public final ArrayList N(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.f13801c.read(bArr, i7, iMin - i7);
                if (i8 == -1) {
                    throw C.g();
                }
                this.l += i8;
                i7 += i8;
            }
            i6 -= iMin;
            arrayList.add(bArr);
        }
        return arrayList;
    }

    public final int O() throws C {
        int i6 = this.f13805j;
        if (this.f13803e - i6 < 4) {
            U(4);
            i6 = this.f13805j;
        }
        this.f13805j = i6 + 4;
        byte[] bArr = this.f13802d;
        return ((bArr[i6 + 3] & 255) << 24) | (bArr[i6] & 255) | ((bArr[i6 + 1] & 255) << 8) | ((bArr[i6 + 2] & 255) << 16);
    }

    public final long P() throws C {
        int i6 = this.f13805j;
        if (this.f13803e - i6 < 8) {
            U(8);
            i6 = this.f13805j;
        }
        this.f13805j = i6 + 8;
        byte[] bArr = this.f13802d;
        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 Q() {
        int i6;
        int i7 = this.f13805j;
        int i8 = this.f13803e;
        if (i8 != i7) {
            int i9 = i7 + 1;
            byte[] bArr = this.f13802d;
            byte b5 = bArr[i7];
            if (b5 >= 0) {
                this.f13805j = 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.f13805j = i10;
                return i6;
            }
        }
        return (int) S();
    }

    public final long R() {
        long j4;
        long j6;
        long j7;
        long j8;
        int i6 = this.f13805j;
        int i7 = this.f13803e;
        if (i7 != i6) {
            int i8 = i6 + 1;
            byte[] bArr = this.f13802d;
            byte b5 = bArr[i6];
            if (b5 >= 0) {
                this.f13805j = 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.f13805j = i9;
                return j4;
            }
        }
        return S();
    }

    public final long S() throws C {
        long j4 = 0;
        for (int i6 = 0; i6 < 64; i6 += 7) {
            if (this.f13805j == this.f13803e) {
                U(1);
            }
            int i7 = this.f13805j;
            this.f13805j = i7 + 1;
            byte b5 = this.f13802d[i7];
            j4 |= ((long) (b5 & 127)) << i6;
            if ((b5 & 128) == 0) {
                return j4;
            }
        }
        throw C.d();
    }

    public final void T() {
        int i6 = this.f13803e + this.f13804f;
        this.f13803e = i6;
        int i7 = this.l + i6;
        int i8 = this.f13807m;
        if (i7 <= i8) {
            this.f13804f = 0;
            return;
        }
        int i9 = i7 - i8;
        this.f13804f = i9;
        this.f13803e = i6 - i9;
    }

    public final void U(int i6) throws C {
        if (W(i6)) {
            return;
        }
        if (i6 <= (Integer.MAX_VALUE - this.l) - this.f13805j) {
            throw C.g();
        }
        throw new C("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
    }

    public final void V(int i6) throws C {
        int i7 = this.f13803e;
        int i8 = this.f13805j;
        int i9 = i7 - i8;
        if (i6 <= i9 && i6 >= 0) {
            this.f13805j = i8 + i6;
            return;
        }
        ByteArrayInputStream byteArrayInputStream = this.f13801c;
        if (i6 < 0) {
            throw C.e();
        }
        int i10 = this.l;
        int i11 = i10 + i8;
        int i12 = i11 + i6;
        int i13 = this.f13807m;
        if (i12 > i13) {
            V((i13 - i10) - i8);
            throw C.g();
        }
        this.l = i11;
        this.f13803e = 0;
        this.f13805j = 0;
        while (i9 < i6) {
            long j4 = i6 - i9;
            try {
                try {
                    long jSkip = byteArrayInputStream.skip(j4);
                    if (jSkip < 0 || jSkip > j4) {
                        throw new IllegalStateException(byteArrayInputStream.getClass() + "#skip returned invalid result: " + jSkip + "\nThe InputStream implementation is buggy.");
                    }
                    if (jSkip == 0) {
                        break;
                    } else {
                        i9 += (int) jSkip;
                    }
                } catch (C e6) {
                    e6.f13716a = true;
                    throw e6;
                }
            } catch (Throwable th) {
                this.l += i9;
                T();
                throw th;
            }
        }
        this.l += i9;
        T();
        if (i9 >= i6) {
            return;
        }
        int i14 = this.f13803e;
        int i15 = i14 - this.f13805j;
        this.f13805j = i14;
        U(1);
        while (true) {
            int i16 = i6 - i15;
            int i17 = this.f13803e;
            if (i16 <= i17) {
                this.f13805j = i16;
                return;
            } else {
                i15 += i17;
                this.f13805j = i17;
                U(1);
            }
        }
    }

    public final boolean W(int i6) throws IOException {
        int i7 = this.f13805j;
        int i8 = i7 + i6;
        int i9 = this.f13803e;
        if (i8 <= i9) {
            throw new IllegalStateException(Z0.o.j(i6, "refillBuffer() called when ", " bytes were already available in buffer"));
        }
        int i10 = this.l;
        if (i6 <= (Integer.MAX_VALUE - i10) - i7 && i10 + i7 + i6 <= this.f13807m) {
            byte[] bArr = this.f13802d;
            if (i7 > 0) {
                if (i9 > i7) {
                    System.arraycopy(bArr, i7, bArr, 0, i9 - i7);
                }
                this.l += i7;
                this.f13803e -= i7;
                this.f13805j = 0;
            }
            int i11 = this.f13803e;
            int iMin = Math.min(bArr.length - i11, (Integer.MAX_VALUE - this.l) - i11);
            ByteArrayInputStream byteArrayInputStream = this.f13801c;
            try {
                int i12 = byteArrayInputStream.read(bArr, i11, iMin);
                if (i12 == 0 || i12 < -1 || i12 > bArr.length) {
                    throw new IllegalStateException(byteArrayInputStream.getClass() + "#read(byte[]) returned invalid result: " + i12 + "\nThe InputStream implementation is buggy.");
                }
                if (i12 > 0) {
                    this.f13803e += i12;
                    T();
                    if (this.f13803e >= i6) {
                        return true;
                    }
                    return W(i6);
                }
            } catch (C e6) {
                e6.f13716a = true;
                throw e6;
            }
        }
        return false;
    }

    @Override // F1.L
    public final void c(int i6) throws C {
        if (this.f13806k != i6) {
            throw new C("Protocol message end-group tag did not match expected tag.");
        }
    }

    @Override // F1.L
    public final int g() {
        return this.l + this.f13805j;
    }

    @Override // F1.L
    public final boolean h() {
        return this.f13805j == this.f13803e && !W(1);
    }

    @Override // F1.L
    public final void o(int i6) {
        this.f13807m = i6;
        T();
    }

    @Override // F1.L
    public final int q(int i6) throws C {
        if (i6 < 0) {
            throw C.e();
        }
        int i7 = this.l + this.f13805j + i6;
        int i8 = this.f13807m;
        if (i7 > i8) {
            throw C.g();
        }
        this.f13807m = i7;
        T();
        return i8;
    }

    @Override // F1.L
    public final boolean r() {
        return R() != 0;
    }

    @Override // F1.L
    public final C1260h t() throws IOException {
        int iQ = Q();
        int i6 = this.f13803e;
        int i7 = this.f13805j;
        int i8 = i6 - i7;
        byte[] bArr = this.f13802d;
        if (iQ <= i8 && iQ > 0) {
            C1260h c1260hH = AbstractC1261i.h(bArr, i7, iQ);
            this.f13805j += iQ;
            return c1260hH;
        }
        if (iQ == 0) {
            return AbstractC1261i.f13790b;
        }
        byte[] bArrM = M(iQ);
        if (bArrM != null) {
            return AbstractC1261i.h(bArrM, 0, bArrM.length);
        }
        int i9 = this.f13805j;
        int i10 = this.f13803e;
        int length = i10 - i9;
        this.l += i10;
        this.f13805j = 0;
        this.f13803e = 0;
        ArrayList<byte[]> arrayListN = N(iQ - length);
        byte[] bArr2 = new byte[iQ];
        System.arraycopy(bArr, i9, bArr2, 0, length);
        for (byte[] bArr3 : arrayListN) {
            System.arraycopy(bArr3, 0, bArr2, length, bArr3.length);
            length += bArr3.length;
        }
        C1260h c1260h = AbstractC1261i.f13790b;
        return new C1260h(bArr2);
    }

    @Override // F1.L
    public final double u() {
        return Double.longBitsToDouble(P());
    }

    @Override // F1.L
    public final int v() {
        return Q();
    }

    @Override // F1.L
    public final int w() {
        return O();
    }

    @Override // F1.L
    public final long x() {
        return P();
    }

    @Override // F1.L
    public final float y() {
        return Float.intBitsToFloat(O());
    }

    @Override // F1.L
    public final int z() {
        return Q();
    }
}
