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.j, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0744j extends J3.e {

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

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

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

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

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

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

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

    public C0744j(ByteArrayInputStream byteArrayInputStream) {
        Charset charset = AbstractC0759z.f10544a;
        this.f10507c = byteArrayInputStream;
        this.f10508d = new byte[4096];
        this.f10509e = 0;
        this.f10511g = 0;
        this.i = 0;
    }

    @Override // J3.e
    public final String A() throws B {
        int iM = M();
        byte[] bArr = this.f10508d;
        if (iM > 0) {
            int i = this.f10509e;
            int i5 = this.f10511g;
            if (iM <= i - i5) {
                String str = new String(bArr, i5, iM, AbstractC0759z.f10544a);
                this.f10511g += iM;
                return str;
            }
        }
        if (iM == 0) {
            return "";
        }
        if (iM > this.f10509e) {
            return new String(H(iM), AbstractC0759z.f10544a);
        }
        Q(iM);
        String str2 = new String(bArr, this.f10511g, iM, AbstractC0759z.f10544a);
        this.f10511g += iM;
        return str2;
    }

    @Override // J3.e
    public final String B() throws IOException {
        int iM = M();
        int i = this.f10511g;
        int i5 = this.f10509e;
        int i8 = i5 - i;
        byte[] bArrH = this.f10508d;
        if (iM <= i8 && iM > 0) {
            this.f10511g = i + iM;
        } else {
            if (iM == 0) {
                return "";
            }
            i = 0;
            if (iM <= i5) {
                Q(iM);
                this.f10511g = iM;
            } else {
                bArrH = H(iM);
            }
        }
        return q0.f10539a.f(bArrH, i, iM);
    }

    @Override // J3.e
    public final int C() throws B {
        if (g()) {
            this.f10512h = 0;
            return 0;
        }
        int iM = M();
        this.f10512h = iM;
        if ((iM >>> 3) != 0) {
            return iM;
        }
        throw B.a();
    }

    @Override // J3.e
    public final int D() {
        return M();
    }

    @Override // J3.e
    public final long E() {
        return N();
    }

    public final byte[] H(int i) throws IOException {
        byte[] bArrI = I(i);
        if (bArrI != null) {
            return bArrI;
        }
        int i5 = this.f10511g;
        int i8 = this.f10509e;
        int length = i8 - i5;
        this.i += i8;
        this.f10511g = 0;
        this.f10509e = 0;
        ArrayList arrayListJ = J(i - length);
        byte[] bArr = new byte[i];
        System.arraycopy(this.f10508d, i5, bArr, 0, length);
        int size = arrayListJ.size();
        int i9 = 0;
        while (i9 < size) {
            Object obj = arrayListJ.get(i9);
            i9++;
            byte[] bArr2 = (byte[]) obj;
            System.arraycopy(bArr2, 0, bArr, length, bArr2.length);
            length += bArr2.length;
        }
        return bArr;
    }

    public final byte[] I(int i) throws IOException {
        if (i == 0) {
            return AbstractC0759z.f10545b;
        }
        if (i < 0) {
            throw B.e();
        }
        int i5 = this.i;
        int i8 = this.f10511g;
        int i9 = i5 + i8 + i;
        if (i9 - Integer.MAX_VALUE > 0) {
            throw new B("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
        }
        int i10 = this.f10513j;
        if (i9 > i10) {
            R((i10 - i5) - i8);
            throw B.g();
        }
        int i11 = this.f10509e - i8;
        int i12 = i - i11;
        ByteArrayInputStream byteArrayInputStream = this.f10507c;
        if (i12 >= 4096) {
            try {
                if (i12 > byteArrayInputStream.available()) {
                    return null;
                }
            } catch (B e8) {
                e8.f10424q = true;
                throw e8;
            }
        }
        byte[] bArr = new byte[i];
        System.arraycopy(this.f10508d, this.f10511g, bArr, 0, i11);
        this.i += this.f10509e;
        this.f10511g = 0;
        this.f10509e = 0;
        while (i11 < i) {
            try {
                int i13 = byteArrayInputStream.read(bArr, i11, i - i11);
                if (i13 == -1) {
                    throw B.g();
                }
                this.i += i13;
                i11 += i13;
            } catch (B e9) {
                e9.f10424q = true;
                throw e9;
            }
        }
        return bArr;
    }

    public final ArrayList J(int i) throws IOException {
        ArrayList arrayList = new ArrayList();
        while (i > 0) {
            int iMin = Math.min(i, 4096);
            byte[] bArr = new byte[iMin];
            int i5 = 0;
            while (i5 < iMin) {
                int i8 = this.f10507c.read(bArr, i5, iMin - i5);
                if (i8 == -1) {
                    throw B.g();
                }
                this.i += i8;
                i5 += i8;
            }
            i -= iMin;
            arrayList.add(bArr);
        }
        return arrayList;
    }

    public final int K() throws B {
        int i = this.f10511g;
        if (this.f10509e - i < 4) {
            Q(4);
            i = this.f10511g;
        }
        this.f10511g = i + 4;
        byte[] bArr = this.f10508d;
        return ((bArr[i + 3] & 255) << 24) | (bArr[i] & 255) | ((bArr[i + 1] & 255) << 8) | ((bArr[i + 2] & 255) << 16);
    }

    public final long L() throws B {
        int i = this.f10511g;
        if (this.f10509e - i < 8) {
            Q(8);
            i = this.f10511g;
        }
        this.f10511g = i + 8;
        byte[] bArr = this.f10508d;
        return ((((long) bArr[i + 7]) & 255) << 56) | (((long) bArr[i]) & 255) | ((((long) bArr[i + 1]) & 255) << 8) | ((((long) bArr[i + 2]) & 255) << 16) | ((((long) bArr[i + 3]) & 255) << 24) | ((((long) bArr[i + 4]) & 255) << 32) | ((((long) bArr[i + 5]) & 255) << 40) | ((((long) bArr[i + 6]) & 255) << 48);
    }

    public final int M() {
        int i;
        int i5 = this.f10511g;
        int i8 = this.f10509e;
        if (i8 != i5) {
            int i9 = i5 + 1;
            byte[] bArr = this.f10508d;
            byte b8 = bArr[i5];
            if (b8 >= 0) {
                this.f10511g = i9;
                return b8;
            }
            if (i8 - i9 >= 9) {
                int i10 = i5 + 2;
                int i11 = (bArr[i9] << 7) ^ b8;
                if (i11 < 0) {
                    i = i11 ^ (-128);
                } else {
                    int i12 = i5 + 3;
                    int i13 = (bArr[i10] << 14) ^ i11;
                    if (i13 >= 0) {
                        i = i13 ^ 16256;
                    } else {
                        int i14 = i5 + 4;
                        int i15 = i13 ^ (bArr[i12] << 21);
                        if (i15 < 0) {
                            i = (-2080896) ^ i15;
                        } else {
                            i12 = i5 + 5;
                            byte b9 = bArr[i14];
                            int i16 = (i15 ^ (b9 << 28)) ^ 266354560;
                            if (b9 < 0) {
                                i14 = i5 + 6;
                                if (bArr[i12] < 0) {
                                    i12 = i5 + 7;
                                    if (bArr[i14] < 0) {
                                        i14 = i5 + 8;
                                        if (bArr[i12] < 0) {
                                            i12 = i5 + 9;
                                            if (bArr[i14] < 0) {
                                                int i17 = i5 + 10;
                                                if (bArr[i12] >= 0) {
                                                    i10 = i17;
                                                    i = i16;
                                                }
                                            }
                                        }
                                    }
                                }
                                i = i16;
                            }
                            i = i16;
                        }
                        i10 = i14;
                    }
                    i10 = i12;
                }
                this.f10511g = i10;
                return i;
            }
        }
        return (int) O();
    }

    public final long N() {
        long j8;
        long j9;
        long j10;
        long j11;
        int i = this.f10511g;
        int i5 = this.f10509e;
        if (i5 != i) {
            int i8 = i + 1;
            byte[] bArr = this.f10508d;
            byte b8 = bArr[i];
            if (b8 >= 0) {
                this.f10511g = i8;
                return b8;
            }
            if (i5 - i8 >= 9) {
                int i9 = i + 2;
                int i10 = (bArr[i8] << 7) ^ b8;
                if (i10 < 0) {
                    j8 = i10 ^ (-128);
                } else {
                    int i11 = i + 3;
                    int i12 = (bArr[i9] << 14) ^ i10;
                    if (i12 >= 0) {
                        j8 = i12 ^ 16256;
                        i9 = i11;
                    } else {
                        int i13 = i + 4;
                        int i14 = i12 ^ (bArr[i11] << 21);
                        if (i14 < 0) {
                            j11 = (-2080896) ^ i14;
                        } else {
                            long j12 = i14;
                            i9 = i + 5;
                            long j13 = j12 ^ (((long) bArr[i13]) << 28);
                            if (j13 >= 0) {
                                j10 = 266354560;
                            } else {
                                i13 = i + 6;
                                long j14 = j13 ^ (((long) bArr[i9]) << 35);
                                if (j14 < 0) {
                                    j9 = -34093383808L;
                                } else {
                                    i9 = i + 7;
                                    j13 = j14 ^ (((long) bArr[i13]) << 42);
                                    if (j13 >= 0) {
                                        j10 = 4363953127296L;
                                    } else {
                                        i13 = i + 8;
                                        j14 = j13 ^ (((long) bArr[i9]) << 49);
                                        if (j14 < 0) {
                                            j9 = -558586000294016L;
                                        } else {
                                            i9 = i + 9;
                                            long j15 = (j14 ^ (((long) bArr[i13]) << 56)) ^ 71499008037633920L;
                                            if (j15 < 0) {
                                                int i15 = i + 10;
                                                if (bArr[i9] >= 0) {
                                                    i9 = i15;
                                                }
                                            }
                                            j8 = j15;
                                        }
                                    }
                                }
                                j11 = j9 ^ j14;
                            }
                            j8 = j10 ^ j13;
                        }
                        i9 = i13;
                        j8 = j11;
                    }
                }
                this.f10511g = i9;
                return j8;
            }
        }
        return O();
    }

    public final long O() throws B {
        long j8 = 0;
        for (int i = 0; i < 64; i += 7) {
            if (this.f10511g == this.f10509e) {
                Q(1);
            }
            int i5 = this.f10511g;
            this.f10511g = i5 + 1;
            byte b8 = this.f10508d[i5];
            j8 |= ((long) (b8 & 127)) << i;
            if ((b8 & 128) == 0) {
                return j8;
            }
        }
        throw B.d();
    }

    public final void P() {
        int i = this.f10509e + this.f10510f;
        this.f10509e = i;
        int i5 = this.i + i;
        int i8 = this.f10513j;
        if (i5 <= i8) {
            this.f10510f = 0;
            return;
        }
        int i9 = i5 - i8;
        this.f10510f = i9;
        this.f10509e = i - i9;
    }

    public final void Q(int i) throws B {
        if (S(i)) {
            return;
        }
        if (i <= (Integer.MAX_VALUE - this.i) - this.f10511g) {
            throw B.g();
        }
        throw new B("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
    }

    public final void R(int i) throws B {
        int i5 = this.f10509e;
        int i8 = this.f10511g;
        int i9 = i5 - i8;
        if (i <= i9 && i >= 0) {
            this.f10511g = i8 + i;
            return;
        }
        ByteArrayInputStream byteArrayInputStream = this.f10507c;
        if (i < 0) {
            throw B.e();
        }
        int i10 = this.i;
        int i11 = i10 + i8;
        int i12 = i11 + i;
        int i13 = this.f10513j;
        if (i12 > i13) {
            R((i13 - i10) - i8);
            throw B.g();
        }
        this.i = i11;
        this.f10509e = 0;
        this.f10511g = 0;
        while (i9 < i) {
            long j8 = i - i9;
            try {
                try {
                    long jSkip = byteArrayInputStream.skip(j8);
                    if (jSkip < 0 || jSkip > j8) {
                        throw new IllegalStateException(byteArrayInputStream.getClass() + "#skip returned invalid result: " + jSkip + "\nThe InputStream implementation is buggy.");
                    }
                    if (jSkip == 0) {
                        break;
                    } else {
                        i9 += (int) jSkip;
                    }
                } catch (B e8) {
                    e8.f10424q = true;
                    throw e8;
                }
            } catch (Throwable th) {
                this.i += i9;
                P();
                throw th;
            }
        }
        this.i += i9;
        P();
        if (i9 >= i) {
            return;
        }
        int i14 = this.f10509e;
        int i15 = i14 - this.f10511g;
        this.f10511g = i14;
        Q(1);
        while (true) {
            int i16 = i - i15;
            int i17 = this.f10509e;
            if (i16 <= i17) {
                this.f10511g = i16;
                return;
            } else {
                i15 += i17;
                this.f10511g = i17;
                Q(1);
            }
        }
    }

    public final boolean S(int i) throws IOException {
        int i5 = this.f10511g;
        int i8 = i5 + i;
        int i9 = this.f10509e;
        if (i8 <= i9) {
            throw new IllegalStateException(I1.a.k(i, "refillBuffer() called when ", " bytes were already available in buffer"));
        }
        int i10 = this.i;
        if (i <= (Integer.MAX_VALUE - i10) - i5 && i10 + i5 + i <= this.f10513j) {
            byte[] bArr = this.f10508d;
            if (i5 > 0) {
                if (i9 > i5) {
                    System.arraycopy(bArr, i5, bArr, 0, i9 - i5);
                }
                this.i += i5;
                this.f10509e -= i5;
                this.f10511g = 0;
            }
            int i11 = this.f10509e;
            int iMin = Math.min(bArr.length - i11, (Integer.MAX_VALUE - this.i) - i11);
            ByteArrayInputStream byteArrayInputStream = this.f10507c;
            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.f10509e += i12;
                    P();
                    if (this.f10509e >= i) {
                        return true;
                    }
                    return S(i);
                }
            } catch (B e8) {
                e8.f10424q = true;
                throw e8;
            }
        }
        return false;
    }

    @Override // J3.e
    public final void b(int i) throws B {
        if (this.f10512h != i) {
            throw new B("Protocol message end-group tag did not match expected tag.");
        }
    }

    @Override // J3.e
    public final int f() {
        return this.i + this.f10511g;
    }

    @Override // J3.e
    public final boolean g() {
        return this.f10511g == this.f10509e && !S(1);
    }

    @Override // J3.e
    public final void j(int i) {
        this.f10513j = i;
        P();
    }

    @Override // J3.e
    public final int l(int i) throws B {
        if (i < 0) {
            throw B.e();
        }
        int i5 = this.i + this.f10511g + i;
        int i8 = this.f10513j;
        if (i5 > i8) {
            throw B.g();
        }
        this.f10513j = i5;
        P();
        return i8;
    }

    @Override // J3.e
    public final boolean m() {
        return N() != 0;
    }

    @Override // J3.e
    public final C0741g o() throws IOException {
        int iM = M();
        int i = this.f10509e;
        int i5 = this.f10511g;
        int i8 = i - i5;
        byte[] bArr = this.f10508d;
        if (iM <= i8 && iM > 0) {
            C0741g c0741gC = AbstractC0742h.c(bArr, i5, iM);
            this.f10511g += iM;
            return c0741gC;
        }
        if (iM == 0) {
            return AbstractC0742h.f10496r;
        }
        byte[] bArrI = I(iM);
        if (bArrI != null) {
            return AbstractC0742h.c(bArrI, 0, bArrI.length);
        }
        int i9 = this.f10511g;
        int i10 = this.f10509e;
        int length = i10 - i9;
        this.i += i10;
        this.f10511g = 0;
        this.f10509e = 0;
        ArrayList arrayListJ = J(iM - length);
        byte[] bArr2 = new byte[iM];
        System.arraycopy(bArr, i9, bArr2, 0, length);
        int size = arrayListJ.size();
        int i11 = 0;
        while (i11 < size) {
            Object obj = arrayListJ.get(i11);
            i11++;
            byte[] bArr3 = (byte[]) obj;
            System.arraycopy(bArr3, 0, bArr2, length, bArr3.length);
            length += bArr3.length;
        }
        C0741g c0741g = AbstractC0742h.f10496r;
        return new C0741g(bArr2);
    }

    @Override // J3.e
    public final double p() {
        return Double.longBitsToDouble(L());
    }

    @Override // J3.e
    public final int q() {
        return M();
    }

    @Override // J3.e
    public final int r() {
        return K();
    }

    @Override // J3.e
    public final long s() {
        return L();
    }

    @Override // J3.e
    public final float t() {
        return Float.intBitsToFloat(K());
    }

    @Override // J3.e
    public final int u() {
        return M();
    }

    @Override // J3.e
    public final long v() {
        return N();
    }

    @Override // J3.e
    public final int w() {
        return K();
    }

    @Override // J3.e
    public final long x() {
        return L();
    }

    @Override // J3.e
    public final int y() {
        return J3.e.d(M());
    }

    @Override // J3.e
    public final long z() {
        return J3.e.e(N());
    }
}
